authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-05-01 13:12:38-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-05-01 13:12:38-04:00
log17b935325e7c315304952f38037c7200595c5f10
tree748a728084493ea3faf645039bb96b263273c880
parentc5dd536845cffdf9f0c22de0a67a89d84d078e24

`@import("builtin")` instead of `@compileVar`

See #226 Closes #220

33 files changed, 315 insertions(+), 419 deletions(-)

doc/langref.md-17
......@@ -446,23 +446,6 @@ This function can only occur inside `@c_import`.
446446
447447This appends `#undef $name` to the `c_import` temporary buffer.
448448
449### @compileVar(comptime name: []u8) -> (varying type)
450
451This function returns a compile-time variable. There are built in compile
452variables:
453
454 * "is_big_endian" `bool` - either `true` for big endian or `false` for little endian.
455 * "is_release" `bool`- either `true` for release mode builds or `false` for debug mode builds.
456 * "is_test" `bool`- either `true` for test builds or `false` otherwise.
457 * "os" `Os` - use `zig targets` to see what enum values are possible here.
458 * "arch" `Arch` - use `zig targets` to see what enum values are possible here.
459 * "environ" `Environ` - use `zig targets` to see what enum values are possible here.
460
461Build scripts can set additional compile variables of any name and type.
462
463The result of this function is a compile time constant that is marked as
464depending on a compile variable.
465
466449### @generatedCode(expression) -> @typeOf(expression)
467450
468451This function wraps an expression and returns the result of the expression
src/all_types.hpp+3-9
......@@ -1173,7 +1173,6 @@ enum BuiltinFnId {
11731173 BuiltinFnIdCInclude,
11741174 BuiltinFnIdCDefine,
11751175 BuiltinFnIdCUndef,
1176 BuiltinFnIdCompileVar,
11771176 BuiltinFnIdCompileErr,
11781177 BuiltinFnIdCompileLog,
11791178 BuiltinFnIdGeneratedCode,
......@@ -1323,7 +1322,6 @@ struct CodeGen {
13231322 HashMap<GenericFnTypeId *, FnTableEntry *, generic_fn_type_id_hash, generic_fn_type_id_eql> generic_table;
13241323 HashMap<Scope *, IrInstruction *, fn_eval_hash, fn_eval_eql> memoized_fn_eval_table;
13251324 HashMap<ZigLLVMFnKey, LLVMValueRef, zig_llvm_fn_key_hash, zig_llvm_fn_key_eql> llvm_fn_table;
1326 HashMap<Buf *, ConstExprValue *, buf_hash, buf_eql_buf> compile_vars;
13271325 HashMap<Buf *, Tld *, buf_hash, buf_eql_buf> exported_symbol_names;
13281326 HashMap<Buf *, Tld *, buf_hash, buf_eql_buf> external_prototypes;
13291327
......@@ -1407,6 +1405,8 @@ struct CodeGen {
14071405 PackageTableEntry *std_package;
14081406 PackageTableEntry *zigrt_package;
14091407 PackageTableEntry *test_runner_package;
1408 PackageTableEntry *compile_var_package;
1409 ImportTableEntry *compile_var_import;
14101410 Buf *root_out_name;
14111411 bool windows_subsystem_windows;
14121412 bool windows_subsystem_console;
......@@ -1635,6 +1635,7 @@ struct ScopeFnDef {
16351635 FnTableEntry *fn_entry;
16361636};
16371637
1638// synchronized with code in define_builtin_compile_vars
16381639enum AtomicOrder {
16391640 AtomicOrderUnordered,
16401641 AtomicOrderMonotonic,
......@@ -1706,7 +1707,6 @@ enum IrInstructionId {
17061707 IrInstructionIdArrayType,
17071708 IrInstructionIdSliceType,
17081709 IrInstructionIdAsm,
1709 IrInstructionIdCompileVar,
17101710 IrInstructionIdSizeOf,
17111711 IrInstructionIdTestNonNull,
17121712 IrInstructionIdUnwrapMaybe,
......@@ -2085,12 +2085,6 @@ struct IrInstructionAsm {
20852085 bool has_side_effects;
20862086};
20872087
2088struct IrInstructionCompileVar {
2089 IrInstruction base;
2090
2091 IrInstruction *name;
2092};
2093
20942088struct IrInstructionSizeOf {
20952089 IrInstruction base;
20962090
src/analyze.cpp+9-1
......@@ -2084,6 +2084,14 @@ void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source
20842084 tld->parent_scope = parent_scope;
20852085}
20862086
2087void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value) {
2088 Tld *tld = g->compile_var_import->decls_scope->decl_table.get(name);
2089 resolve_top_level_decl(g, tld, false);
2090 assert(tld->id == TldIdVar);
2091 TldVar *tld_var = (TldVar *)tld;
2092 tld_var->var->value = value;
2093}
2094
20872095void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
20882096 switch (node->type) {
20892097 case NodeTypeRoot:
......@@ -2122,7 +2130,7 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
21222130 if (import == g->root_import && scope_is_root_decls(&decls_scope->base) &&
21232131 buf_eql_str(fn_name, "panic"))
21242132 {
2125 g->compile_vars.put(buf_create_from_str("panic_implementation_provided"),
2133 update_compile_var(g, buf_create_from_str("__zig_panic_implementation_provided"),
21262134 create_const_bool(g, true));
21272135 }
21282136
src/analyze.hpp+1
......@@ -152,5 +152,6 @@ ConstParent *get_const_val_parent(CodeGen *g, ConstExprValue *value);
152152FnTableEntry *get_extern_panic_fn(CodeGen *g);
153153TypeTableEntry *create_enum_tag_type(CodeGen *g, TypeTableEntry *enum_type, TypeTableEntry *int_type);
154154void expand_undef_array(CodeGen *g, ConstExprValue *const_val);
155void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value);
155156
156157#endif
src/codegen.cpp+167-228
......@@ -47,7 +47,7 @@ static void init_darwin_native(CodeGen *g) {
4747 }
4848}
4949
50PackageTableEntry *new_package(const char *root_src_dir, const char *root_src_path) {
50static PackageTableEntry *new_package(const char *root_src_dir, const char *root_src_path) {
5151 PackageTableEntry *entry = allocate<PackageTableEntry>(1);
5252 entry->package_table.init(4);
5353 buf_init_from_str(&entry->root_src_dir, root_src_dir);
......@@ -70,7 +70,6 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
7070 g->generic_table.init(16);
7171 g->llvm_fn_table.init(16);
7272 g->memoized_fn_eval_table.init(16);
73 g->compile_vars.init(16);
7473 g->exported_symbol_names.init(8);
7574 g->external_prototypes.init(8);
7675 g->is_release_build = false;
......@@ -2883,7 +2882,6 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
28832882 case IrInstructionIdSetDebugSafety:
28842883 case IrInstructionIdArrayType:
28852884 case IrInstructionIdSliceType:
2886 case IrInstructionIdCompileVar:
28872885 case IrInstructionIdSizeOf:
28882886 case IrInstructionIdSwitchTarget:
28892887 case IrInstructionIdContainerInitFields:
......@@ -3627,6 +3625,13 @@ static LLVMValueRef build_alloca(CodeGen *g, TypeTableEntry *type_entry, const c
36273625 return result;
36283626}
36293627
3628static void ensure_cache_dir(CodeGen *g) {
3629 int err;
3630 if ((err = os_make_path(g->cache_dir))) {
3631 zig_panic("unable to make cache dir: %s", err_str(err));
3632 }
3633}
3634
36303635static void do_code_gen(CodeGen *g) {
36313636 if (g->verbose) {
36323637 fprintf(stderr, "\nCode Generation:\n");
......@@ -3919,10 +3924,7 @@ static void do_code_gen(CodeGen *g) {
39193924 buf_append_str(o_basename, o_ext);
39203925 Buf *output_path = buf_alloc();
39213926 os_path_join(g->cache_dir, o_basename, output_path);
3922 int err;
3923 if ((err = os_make_path(g->cache_dir))) {
3924 zig_panic("unable to make cache dir: %s", err_str(err));
3925 }
3927 ensure_cache_dir(g);
39263928 if (ZigLLVMTargetMachineEmitToFile(g->target_machine, g->module, buf_ptr(output_path),
39273929 LLVMObjectFile, &err_msg, !g->is_release_build))
39283930 {
......@@ -3970,36 +3972,6 @@ static const GlobalLinkageValue global_linkage_values[] = {
39703972 {GlobalLinkageIdLinkOnce, "LinkOnce"},
39713973};
39723974
3973static void init_enum_debug_info(CodeGen *g, TypeTableEntry *enum_type) {
3974 uint32_t field_count = enum_type->data.enumeration.src_field_count;
3975
3976 TypeTableEntry *tag_int_type = get_smallest_unsigned_int_type(g, field_count);
3977 TypeTableEntry *tag_type_entry = create_enum_tag_type(g, enum_type, tag_int_type);
3978 enum_type->data.enumeration.tag_type = tag_type_entry;
3979
3980 ZigLLVMDIEnumerator **di_enumerators = allocate<ZigLLVMDIEnumerator*>(field_count);
3981 for (uint32_t i = 0; i < field_count; i += 1) {
3982 TypeEnumField *field = &enum_type->data.enumeration.fields[i];
3983 di_enumerators[i] = ZigLLVMCreateDebugEnumerator(g->dbuilder, buf_ptr(field->name), i);
3984 }
3985
3986 // create debug type for tag
3987 uint64_t tag_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, tag_type_entry->type_ref);
3988 uint64_t tag_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, tag_type_entry->type_ref);
3989 enum_type->di_type = ZigLLVMCreateDebugEnumerationType(g->dbuilder,
3990 nullptr, buf_ptr(&enum_type->name),
3991 nullptr, 0,
3992 tag_debug_size_in_bits,
3993 tag_debug_align_in_bits,
3994 di_enumerators, field_count,
3995 tag_type_entry->di_type, "");
3996
3997 enum_type->type_ref = tag_type_entry->type_ref;
3998
3999 enum_type->data.enumeration.complete = true;
4000 enum_type->data.enumeration.zero_bits_known = true;
4001}
4002
40033975static void define_builtin_types(CodeGen *g) {
40043976 {
40053977 // if this type is anywhere in the AST, we should never hit codegen.
......@@ -4218,167 +4190,6 @@ static void define_builtin_types(CodeGen *g) {
42184190 g->primitive_type_table.put(&entry->name, entry);
42194191 }
42204192
4221 {
4222 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdEnum);
4223 buf_init_from_str(&entry->name, "Os");
4224 uint32_t field_count = (uint32_t)target_os_count();
4225 entry->data.enumeration.src_field_count = field_count;
4226 entry->data.enumeration.fields = allocate<TypeEnumField>(field_count);
4227 for (uint32_t i = 0; i < field_count; i += 1) {
4228 TypeEnumField *type_enum_field = &entry->data.enumeration.fields[i];
4229 ZigLLVM_OSType os_type = get_target_os(i);
4230 type_enum_field->name = buf_create_from_str(get_target_os_name(os_type));
4231 type_enum_field->value = i;
4232 type_enum_field->type_entry = g->builtin_types.entry_void;
4233
4234 if (os_type == g->zig_target.os) {
4235 g->target_os_index = i;
4236 }
4237 }
4238
4239 init_enum_debug_info(g, entry);
4240
4241 g->builtin_types.entry_os_enum = entry;
4242 g->primitive_type_table.put(&entry->name, entry);
4243 }
4244
4245 {
4246 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdEnum);
4247 buf_init_from_str(&entry->name, "Arch");
4248 uint32_t field_count = (uint32_t)target_arch_count();
4249 entry->data.enumeration.src_field_count = field_count;
4250 entry->data.enumeration.fields = allocate<TypeEnumField>(field_count);
4251 for (uint32_t i = 0; i < field_count; i += 1) {
4252 TypeEnumField *type_enum_field = &entry->data.enumeration.fields[i];
4253 const ArchType *arch_type = get_target_arch(i);
4254 type_enum_field->name = buf_alloc();
4255 buf_resize(type_enum_field->name, 50);
4256 get_arch_name(buf_ptr(type_enum_field->name), arch_type);
4257 buf_resize(type_enum_field->name, strlen(buf_ptr(type_enum_field->name)));
4258
4259 type_enum_field->value = i;
4260 type_enum_field->type_entry = g->builtin_types.entry_void;
4261
4262 if (arch_type->arch == g->zig_target.arch.arch &&
4263 arch_type->sub_arch == g->zig_target.arch.sub_arch)
4264 {
4265 g->target_arch_index = i;
4266 }
4267 }
4268
4269 init_enum_debug_info(g, entry);
4270
4271 g->builtin_types.entry_arch_enum = entry;
4272 g->primitive_type_table.put(&entry->name, entry);
4273 }
4274
4275 {
4276 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdEnum);
4277 buf_init_from_str(&entry->name, "Environ");
4278 uint32_t field_count = (uint32_t)target_environ_count();
4279 entry->data.enumeration.src_field_count = field_count;
4280 entry->data.enumeration.fields = allocate<TypeEnumField>(field_count);
4281 for (uint32_t i = 0; i < field_count; i += 1) {
4282 TypeEnumField *type_enum_field = &entry->data.enumeration.fields[i];
4283 ZigLLVM_EnvironmentType environ_type = get_target_environ(i);
4284 type_enum_field->name = buf_create_from_str(ZigLLVMGetEnvironmentTypeName(environ_type));
4285 type_enum_field->value = i;
4286 type_enum_field->type_entry = g->builtin_types.entry_void;
4287
4288 if (environ_type == g->zig_target.env_type) {
4289 g->target_environ_index = i;
4290 }
4291 }
4292
4293 init_enum_debug_info(g, entry);
4294
4295 g->builtin_types.entry_environ_enum = entry;
4296 g->primitive_type_table.put(&entry->name, entry);
4297 }
4298
4299 {
4300 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdEnum);
4301 buf_init_from_str(&entry->name, "ObjectFormat");
4302 uint32_t field_count = (uint32_t)target_oformat_count();
4303 entry->data.enumeration.src_field_count = field_count;
4304 entry->data.enumeration.fields = allocate<TypeEnumField>(field_count);
4305 for (uint32_t i = 0; i < field_count; i += 1) {
4306 TypeEnumField *type_enum_field = &entry->data.enumeration.fields[i];
4307 ZigLLVM_ObjectFormatType oformat = get_target_oformat(i);
4308 type_enum_field->name = buf_create_from_str(get_target_oformat_name(oformat));
4309 type_enum_field->value = i;
4310 type_enum_field->type_entry = g->builtin_types.entry_void;
4311
4312 if (oformat == g->zig_target.oformat) {
4313 g->target_oformat_index = i;
4314 }
4315 }
4316
4317 init_enum_debug_info(g, entry);
4318
4319 g->builtin_types.entry_oformat_enum = entry;
4320 g->primitive_type_table.put(&entry->name, entry);
4321 }
4322
4323 {
4324 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdEnum);
4325 entry->zero_bits = true; // only allowed at compile time
4326 buf_init_from_str(&entry->name, "GlobalLinkage");
4327 uint32_t field_count = array_length(global_linkage_values);
4328 entry->data.enumeration.src_field_count = field_count;
4329 entry->data.enumeration.fields = allocate<TypeEnumField>(field_count);
4330 for (uint32_t i = 0; i < field_count; i += 1) {
4331 TypeEnumField *type_enum_field = &entry->data.enumeration.fields[i];
4332 const GlobalLinkageValue *value = &global_linkage_values[i];
4333 type_enum_field->name = buf_create_from_str(value->name);
4334 type_enum_field->value = i;
4335 type_enum_field->type_entry = g->builtin_types.entry_void;
4336 }
4337 entry->data.enumeration.complete = true;
4338 entry->data.enumeration.zero_bits_known = true;
4339
4340 TypeTableEntry *tag_type_entry = get_smallest_unsigned_int_type(g, field_count);
4341 entry->data.enumeration.tag_type = tag_type_entry;
4342
4343 g->builtin_types.entry_global_linkage_enum = entry;
4344 g->primitive_type_table.put(&entry->name, entry);
4345 }
4346
4347 {
4348 TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdEnum);
4349 entry->zero_bits = true; // only allowed at compile time
4350 buf_init_from_str(&entry->name, "AtomicOrder");
4351 uint32_t field_count = 6;
4352 entry->data.enumeration.src_field_count = field_count;
4353 entry->data.enumeration.fields = allocate<TypeEnumField>(field_count);
4354 entry->data.enumeration.fields[0].name = buf_create_from_str("Unordered");
4355 entry->data.enumeration.fields[0].value = AtomicOrderUnordered;
4356 entry->data.enumeration.fields[0].type_entry = g->builtin_types.entry_void;
4357 entry->data.enumeration.fields[1].name = buf_create_from_str("Monotonic");
4358 entry->data.enumeration.fields[1].value = AtomicOrderMonotonic;
4359 entry->data.enumeration.fields[1].type_entry = g->builtin_types.entry_void;
4360 entry->data.enumeration.fields[2].name = buf_create_from_str("Acquire");
4361 entry->data.enumeration.fields[2].value = AtomicOrderAcquire;
4362 entry->data.enumeration.fields[2].type_entry = g->builtin_types.entry_void;
4363 entry->data.enumeration.fields[3].name = buf_create_from_str("Release");
4364 entry->data.enumeration.fields[3].value = AtomicOrderRelease;
4365 entry->data.enumeration.fields[3].type_entry = g->builtin_types.entry_void;
4366 entry->data.enumeration.fields[4].name = buf_create_from_str("AcqRel");
4367 entry->data.enumeration.fields[4].value = AtomicOrderAcqRel;
4368 entry->data.enumeration.fields[4].type_entry = g->builtin_types.entry_void;
4369 entry->data.enumeration.fields[5].name = buf_create_from_str("SeqCst");
4370 entry->data.enumeration.fields[5].value = AtomicOrderSeqCst;
4371 entry->data.enumeration.fields[5].type_entry = g->builtin_types.entry_void;
4372
4373 entry->data.enumeration.complete = true;
4374 entry->data.enumeration.zero_bits_known = true;
4375
4376 TypeTableEntry *tag_type_entry = get_smallest_unsigned_int_type(g, field_count);
4377 entry->data.enumeration.tag_type = tag_type_entry;
4378
4379 g->builtin_types.entry_atomic_order_enum = entry;
4380 g->primitive_type_table.put(&entry->name, entry);
4381 }
43824193}
43834194
43844195
......@@ -4475,7 +4286,6 @@ static void define_builtin_fns(CodeGen *g) {
44754286 create_builtin_fn(g, BuiltinFnIdCInclude, "cInclude", 1);
44764287 create_builtin_fn(g, BuiltinFnIdCDefine, "cDefine", 2);
44774288 create_builtin_fn(g, BuiltinFnIdCUndef, "cUndef", 1);
4478 create_builtin_fn(g, BuiltinFnIdCompileVar, "compileVar", 1);
44794289 create_builtin_fn(g, BuiltinFnIdGeneratedCode, "generatedCode", 1);
44804290 create_builtin_fn(g, BuiltinFnIdCtz, "ctz", 1);
44814291 create_builtin_fn(g, BuiltinFnIdClz, "clz", 1);
......@@ -4506,38 +4316,159 @@ static void define_builtin_fns(CodeGen *g) {
45064316 create_builtin_fn(g, BuiltinFnIdOffsetOf, "offsetOf", 2);
45074317}
45084318
4509static void add_compile_var(CodeGen *g, const char *name, ConstExprValue *value) {
4510 g->compile_vars.put_unique(buf_create_from_str(name), value);
4319static const char *bool_to_str(bool b) {
4320 return b ? "true" : "false";
45114321}
45124322
45134323static void define_builtin_compile_vars(CodeGen *g) {
4514 add_compile_var(g, "is_big_endian", create_const_bool(g, g->is_big_endian));
4515 add_compile_var(g, "is_release", create_const_bool(g, g->is_release_build));
4516 add_compile_var(g, "is_test", create_const_bool(g, g->is_test_build));
4517 add_compile_var(g, "os", create_const_enum_tag(g->builtin_types.entry_os_enum, g->target_os_index));
4518 add_compile_var(g, "arch", create_const_enum_tag(g->builtin_types.entry_arch_enum, g->target_arch_index));
4519 add_compile_var(g, "environ", create_const_enum_tag(g->builtin_types.entry_environ_enum, g->target_environ_index));
4520 add_compile_var(g, "object_format", create_const_enum_tag(
4521 g->builtin_types.entry_oformat_enum, g->target_oformat_index));
4324 if (g->std_package == nullptr)
4325 return;
4326
4327 const char *builtin_zig_basename = "builtin.zig";
4328 Buf *builtin_zig_path = buf_alloc();
4329 os_path_join(g->cache_dir, buf_create_from_str(builtin_zig_basename), builtin_zig_path);
4330 Buf *contents = buf_alloc();
4331
4332 const char *cur_os = nullptr;
4333 {
4334 buf_appendf(contents, "pub const Os = enum {\n");
4335 uint32_t field_count = (uint32_t)target_os_count();
4336 for (uint32_t i = 0; i < field_count; i += 1) {
4337 ZigLLVM_OSType os_type = get_target_os(i);
4338 const char *name = get_target_os_name(os_type);
4339 buf_appendf(contents, " %s,\n", name);
4340
4341 if (os_type == g->zig_target.os) {
4342 g->target_os_index = i;
4343 cur_os = name;
4344 }
4345 }
4346 buf_appendf(contents, "};\n\n");
4347 }
4348 assert(cur_os != nullptr);
4349
4350 const char *cur_arch = nullptr;
4351 {
4352 buf_appendf(contents, "pub const Arch = enum {\n");
4353 uint32_t field_count = (uint32_t)target_arch_count();
4354 for (uint32_t i = 0; i < field_count; i += 1) {
4355 const ArchType *arch_type = get_target_arch(i);
4356 Buf *arch_name = buf_alloc();
4357 buf_resize(arch_name, 50);
4358 get_arch_name(buf_ptr(arch_name), arch_type);
4359 buf_resize(arch_name, strlen(buf_ptr(arch_name)));
4360
4361 buf_appendf(contents, " %s,\n", buf_ptr(arch_name));
4362
4363 if (arch_type->arch == g->zig_target.arch.arch &&
4364 arch_type->sub_arch == g->zig_target.arch.sub_arch)
4365 {
4366 g->target_arch_index = i;
4367 cur_arch = buf_ptr(arch_name);
4368 }
4369 }
4370 buf_appendf(contents, "};\n\n");
4371 }
4372 assert(cur_arch != nullptr);
4373
4374 const char *cur_environ = nullptr;
4375 {
4376 buf_appendf(contents, "pub const Environ = enum {\n");
4377 uint32_t field_count = (uint32_t)target_environ_count();
4378 for (uint32_t i = 0; i < field_count; i += 1) {
4379 ZigLLVM_EnvironmentType environ_type = get_target_environ(i);
4380 const char *name = ZigLLVMGetEnvironmentTypeName(environ_type);
4381 buf_appendf(contents, " %s,\n", name);
4382
4383 if (environ_type == g->zig_target.env_type) {
4384 g->target_environ_index = i;
4385 cur_environ = name;
4386 }
4387 }
4388 buf_appendf(contents, "};\n\n");
4389 }
4390 assert(cur_environ != nullptr);
45224391
4392 const char *cur_obj_fmt = nullptr;
45234393 {
4524 TypeTableEntry *str_type = get_slice_type(g, g->builtin_types.entry_u8, true);
4525 ConstExprValue *const_val = allocate<ConstExprValue>(1);
4526 const_val->special = ConstValSpecialStatic;
4527 const_val->type = get_array_type(g, str_type, g->link_libs.length);
4528 const_val->data.x_array.s_none.elements = allocate<ConstExprValue>(g->link_libs.length);
4394 buf_appendf(contents, "pub const ObjectFormat = enum {\n");
4395 uint32_t field_count = (uint32_t)target_oformat_count();
4396 for (uint32_t i = 0; i < field_count; i += 1) {
4397 ZigLLVM_ObjectFormatType oformat = get_target_oformat(i);
4398 const char *name = get_target_oformat_name(oformat);
4399 buf_appendf(contents, " %s,\n", name);
4400
4401 if (oformat == g->zig_target.oformat) {
4402 g->target_oformat_index = i;
4403 cur_obj_fmt = name;
4404 }
4405 }
4406
4407 buf_appendf(contents, "};\n\n");
4408 }
4409 assert(cur_obj_fmt != nullptr);
4410
4411 {
4412 buf_appendf(contents, "pub const GlobalLinkage = enum {\n");
4413 uint32_t field_count = array_length(global_linkage_values);
4414 for (uint32_t i = 0; i < field_count; i += 1) {
4415 const GlobalLinkageValue *value = &global_linkage_values[i];
4416 buf_appendf(contents, " %s,\n", value->name);
4417 }
4418 buf_appendf(contents, "};\n\n");
4419 }
4420 {
4421 buf_appendf(contents,
4422 "pub const AtomicOrder = enum {\n"
4423 " Unordered,\n"
4424 " Monotonic,\n"
4425 " Acquire,\n"
4426 " Release,\n"
4427 " AcqRel,\n"
4428 " SeqCst,\n"
4429 "};\n\n");
4430 }
4431 buf_appendf(contents, "pub const is_big_endian = %s;\n", bool_to_str(g->is_big_endian));
4432 buf_appendf(contents, "pub const is_release = %s;\n", bool_to_str(g->is_release_build));
4433 buf_appendf(contents, "pub const is_test = %s;\n", bool_to_str(g->is_test_build));
4434 buf_appendf(contents, "pub const os = Os.%s;\n", cur_os);
4435 buf_appendf(contents, "pub const arch = Arch.%s;\n", cur_arch);
4436 buf_appendf(contents, "pub const environ = Environ.%s;\n", cur_environ);
4437 buf_appendf(contents, "pub const object_format = ObjectFormat.%s;\n", cur_obj_fmt);
4438
4439 {
4440 buf_appendf(contents, "pub const link_libs = [][]const u8 {\n");
45294441 for (size_t i = 0; i < g->link_libs.length; i += 1) {
45304442 Buf *link_lib_buf = g->link_libs.at(i);
4531 ConstExprValue *array_val = create_const_str_lit(g, link_lib_buf);
4532 init_const_slice(g, &const_val->data.x_array.s_none.elements[i], array_val, 0, buf_len(link_lib_buf), true);
4443 buf_appendf(contents, " \"%s\",\n", buf_ptr(link_lib_buf));
45334444 }
4445 buf_appendf(contents, "};\n");
4446 }
4447
4448 buf_appendf(contents, "pub const __zig_panic_implementation_provided = %s; // overwritten later\n",
4449 bool_to_str(false));
4450 buf_appendf(contents, "pub const __zig_test_fn_slice = {}; // overwritten later\n");
45344451
4535 add_compile_var(g, "link_libs", const_val);
4452 ensure_cache_dir(g);
4453 os_write_file(builtin_zig_path, contents);
4454
4455 int err;
4456 Buf *abs_full_path = buf_alloc();
4457 if ((err = os_path_real(builtin_zig_path, abs_full_path))) {
4458 zig_panic("unable to open '%s': %s", buf_ptr(builtin_zig_path), err_str(err));
45364459 }
4537 add_compile_var(g, "panic_implementation_provided", create_const_bool(g, false));
4460
4461 assert(g->root_package);
4462 assert(g->std_package);
4463 g->compile_var_package = new_package(buf_ptr(g->cache_dir), builtin_zig_basename);
4464 g->root_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package);
4465 g->std_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package);
4466 g->compile_var_import = add_source_file(g, g->compile_var_package, abs_full_path, contents);
45384467}
45394468
45404469static void init(CodeGen *g) {
4470 if (g->module)
4471 return;
45414472 assert(g->root_out_name);
45424473 g->module = LLVMModuleCreateWithName(buf_ptr(g->root_out_name));
45434474
......@@ -4600,14 +4531,15 @@ static void init(CodeGen *g) {
46004531 g->dummy_di_file = nullptr;
46014532
46024533 define_builtin_types(g);
4603 define_builtin_fns(g);
4604 define_builtin_compile_vars(g);
46054534
46064535 g->invalid_instruction = allocate<IrInstruction>(1);
46074536 g->invalid_instruction->value.type = g->builtin_types.entry_invalid;
46084537
46094538 g->const_void_val.special = ConstValSpecialStatic;
46104539 g->const_void_val.type = g->builtin_types.entry_void;
4540
4541 define_builtin_fns(g);
4542 define_builtin_compile_vars(g);
46114543}
46124544
46134545void codegen_parseh(CodeGen *g, Buf *full_path) {
......@@ -4661,21 +4593,17 @@ static ImportTableEntry *add_special_code(CodeGen *g, PackageTableEntry *package
46614593}
46624594
46634595static PackageTableEntry *create_bootstrap_pkg(CodeGen *g, PackageTableEntry *pkg_with_main) {
4664 PackageTableEntry *package = new_package(buf_ptr(g->zig_std_special_dir), "");
4665 package->package_table.put(buf_create_from_str("std"), g->std_package);
4596 PackageTableEntry *package = codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "bootstrap.zig");
46664597 package->package_table.put(buf_create_from_str("@root"), pkg_with_main);
46674598 return package;
46684599}
46694600
46704601static PackageTableEntry *create_test_runner_pkg(CodeGen *g) {
4671 PackageTableEntry *package = new_package(buf_ptr(g->zig_std_special_dir), "test_runner.zig");
4672 package->package_table.put(buf_create_from_str("std"), g->std_package);
4673 return package;
4602 return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "test_runner.zig");
46744603}
46754604
46764605static PackageTableEntry *create_zigrt_pkg(CodeGen *g) {
4677 PackageTableEntry *package = new_package(buf_ptr(g->zig_std_special_dir), "");
4678 package->package_table.put(buf_create_from_str("std"), g->std_package);
4606 PackageTableEntry *package = codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "zigrt.zig");
46794607 package->package_table.put(buf_create_from_str("@root"), g->root_package);
46804608 return package;
46814609}
......@@ -4723,7 +4651,7 @@ static void create_test_compile_var_and_add_test_runner(CodeGen *g) {
47234651
47244652 ConstExprValue *test_fn_slice = create_const_slice(g, test_fn_array, 0, g->test_fns.length, true);
47254653
4726 g->compile_vars.put(buf_create_from_str("zig_test_fn_slice"), test_fn_slice);
4654 update_compile_var(g, buf_create_from_str("__zig_test_fn_slice"), test_fn_slice);
47274655 g->test_runner_package = create_test_runner_pkg(g);
47284656 g->test_runner_import = add_special_code(g, g->test_runner_package, "test_runner.zig");
47294657}
......@@ -5066,3 +4994,14 @@ void codegen_build(CodeGen *g) {
50664994 do_code_gen(g);
50674995 gen_h_file(g);
50684996}
4997
4998PackageTableEntry *codegen_create_package(CodeGen *g, const char *root_src_dir, const char *root_src_path) {
4999 init(g);
5000 PackageTableEntry *pkg = new_package(root_src_dir, root_src_path);
5001 if (g->std_package != nullptr) {
5002 assert(g->compile_var_package != nullptr);
5003 pkg->package_table.put(buf_create_from_str("std"), g->std_package);
5004 pkg->package_table.put(buf_create_from_str("builtin"), g->compile_var_package);
5005 }
5006 return pkg;
5007}
src/codegen.hpp+1-1
......@@ -52,7 +52,7 @@ void codegen_add_time_event(CodeGen *g, const char *name);
5252void codegen_print_timing_report(CodeGen *g, FILE *f);
5353void codegen_build(CodeGen *g);
5454
55PackageTableEntry *new_package(const char *root_src_dir, const char *root_src_path);
55PackageTableEntry *codegen_create_package(CodeGen *g, const char *root_src_dir, const char *root_src_path);
5656void codegen_add_assembly(CodeGen *g, Buf *path);
5757void codegen_add_object(CodeGen *g, Buf *object_path);
5858
src/ir.cpp-55
......@@ -294,10 +294,6 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionAsm *) {
294294 return IrInstructionIdAsm;
295295}
296296
297static constexpr IrInstructionId ir_instruction_id(IrInstructionCompileVar *) {
298 return IrInstructionIdCompileVar;
299}
300
301297static constexpr IrInstructionId ir_instruction_id(IrInstructionSizeOf *) {
302298 return IrInstructionIdSizeOf;
303299}
......@@ -1249,15 +1245,6 @@ static IrInstruction *ir_build_asm_from(IrBuilder *irb, IrInstruction *old_instr
12491245 return new_instruction;
12501246}
12511247
1252static IrInstruction *ir_build_compile_var(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *name) {
1253 IrInstructionCompileVar *instruction = ir_build_instruction<IrInstructionCompileVar>(irb, scope, source_node);
1254 instruction->name = name;
1255
1256 ir_ref_instruction(name, irb->current_basic_block);
1257
1258 return &instruction->base;
1259}
1260
12611248static IrInstruction *ir_build_size_of(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *type_value) {
12621249 IrInstructionSizeOf *instruction = ir_build_instruction<IrInstructionSizeOf>(irb, scope, source_node);
12631250 instruction->type_value = type_value;
......@@ -2431,13 +2418,6 @@ static IrInstruction *ir_instruction_asm_get_dep(IrInstructionAsm *instruction,
24312418 return nullptr;
24322419}
24332420
2434static IrInstruction *ir_instruction_compilevar_get_dep(IrInstructionCompileVar *instruction, size_t index) {
2435 switch (index) {
2436 case 0: return instruction->name;
2437 default: return nullptr;
2438 }
2439}
2440
24412421static IrInstruction *ir_instruction_sizeof_get_dep(IrInstructionSizeOf *instruction, size_t index) {
24422422 switch (index) {
24432423 case 0: return instruction->type_value;
......@@ -2979,8 +2959,6 @@ static IrInstruction *ir_instruction_get_dep(IrInstruction *instruction, size_t
29792959 return ir_instruction_slicetype_get_dep((IrInstructionSliceType *) instruction, index);
29802960 case IrInstructionIdAsm:
29812961 return ir_instruction_asm_get_dep((IrInstructionAsm *) instruction, index);
2982 case IrInstructionIdCompileVar:
2983 return ir_instruction_compilevar_get_dep((IrInstructionCompileVar *) instruction, index);
29842962 case IrInstructionIdSizeOf:
29852963 return ir_instruction_sizeof_get_dep((IrInstructionSizeOf *) instruction, index);
29862964 case IrInstructionIdTestNonNull:
......@@ -3937,15 +3915,6 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
39373915
39383916 return ir_build_set_debug_safety(irb, scope, node, arg0_value, arg1_value);
39393917 }
3940 case BuiltinFnIdCompileVar:
3941 {
3942 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
3943 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, scope);
3944 if (arg0_value == irb->codegen->invalid_instruction)
3945 return arg0_value;
3946
3947 return ir_build_compile_var(irb, scope, node, arg0_value);
3948 }
39493918 case BuiltinFnIdSizeof:
39503919 {
39513920 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
......@@ -10409,27 +10378,6 @@ static TypeTableEntry *ir_analyze_instruction_array_type(IrAnalyze *ira,
1040910378 zig_unreachable();
1041010379}
1041110380
10412static TypeTableEntry *ir_analyze_instruction_compile_var(IrAnalyze *ira,
10413 IrInstructionCompileVar *compile_var_instruction)
10414{
10415 IrInstruction *name_value = compile_var_instruction->name->other;
10416 Buf *var_name = ir_resolve_str(ira, name_value);
10417 if (!var_name)
10418 return ira->codegen->builtin_types.entry_invalid;
10419
10420 ConstExprValue *out_val = ir_build_const_from(ira, &compile_var_instruction->base);
10421 auto entry = ira->codegen->compile_vars.maybe_get(var_name);
10422 if (entry) {
10423 *out_val = *entry->value;
10424 return out_val->type;
10425 } else {
10426 ir_add_error_node(ira, name_value->source_node,
10427 buf_sprintf("unrecognized compile variable: '%s'", buf_ptr(var_name)));
10428 return ira->codegen->builtin_types.entry_invalid;
10429 }
10430 zig_unreachable();
10431}
10432
1043310381static TypeTableEntry *ir_analyze_instruction_size_of(IrAnalyze *ira,
1043410382 IrInstructionSizeOf *size_of_instruction)
1043510383{
......@@ -13039,8 +12987,6 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
1303912987 return ir_analyze_instruction_asm(ira, (IrInstructionAsm *)instruction);
1304012988 case IrInstructionIdArrayType:
1304112989 return ir_analyze_instruction_array_type(ira, (IrInstructionArrayType *)instruction);
13042 case IrInstructionIdCompileVar:
13043 return ir_analyze_instruction_compile_var(ira, (IrInstructionCompileVar *)instruction);
1304412990 case IrInstructionIdSizeOf:
1304512991 return ir_analyze_instruction_size_of(ira, (IrInstructionSizeOf *)instruction);
1304612992 case IrInstructionIdTestNonNull:
......@@ -13292,7 +13238,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
1329213238 case IrInstructionIdEnumFieldPtr:
1329313239 case IrInstructionIdArrayType:
1329413240 case IrInstructionIdSliceType:
13295 case IrInstructionIdCompileVar:
1329613241 case IrInstructionIdSizeOf:
1329713242 case IrInstructionIdTestNonNull:
1329813243 case IrInstructionIdUnwrapMaybe:
src/ir_print.cpp-9
......@@ -403,12 +403,6 @@ static void ir_print_asm(IrPrint *irp, IrInstructionAsm *instruction) {
403403 fprintf(irp->f, ")");
404404}
405405
406static void ir_print_compile_var(IrPrint *irp, IrInstructionCompileVar *instruction) {
407 fprintf(irp->f, "@compileVar(");
408 ir_print_other_instruction(irp, instruction->name);
409 fprintf(irp->f, ")");
410}
411
412406static void ir_print_size_of(IrPrint *irp, IrInstructionSizeOf *instruction) {
413407 fprintf(irp->f, "@sizeOf(");
414408 ir_print_other_instruction(irp, instruction->type_value);
......@@ -987,9 +981,6 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
987981 case IrInstructionIdAsm:
988982 ir_print_asm(irp, (IrInstructionAsm *)instruction);
989983 break;
990 case IrInstructionIdCompileVar:
991 ir_print_compile_var(irp, (IrInstructionCompileVar *)instruction);
992 break;
993984 case IrInstructionIdSizeOf:
994985 ir_print_size_of(irp, (IrInstructionSizeOf *)instruction);
995986 break;
src/main.cpp+2-2
......@@ -263,8 +263,8 @@ int main(int argc, char **argv) {
263263 return 1;
264264 }
265265
266 PackageTableEntry *build_pkg = new_package(buf_ptr(&build_file_dirname), buf_ptr(&build_file_basename));
267 build_pkg->package_table.put(buf_create_from_str("std"), g->std_package);
266 PackageTableEntry *build_pkg = codegen_create_package(g, buf_ptr(&build_file_dirname),
267 buf_ptr(&build_file_basename));
268268 g->root_package->package_table.put(buf_create_from_str("@build"), build_pkg);
269269 codegen_build(g);
270270 codegen_link(g, buf_ptr(path_to_build_exe));
std/build.zig+14-9
......@@ -1,3 +1,4 @@
1const builtin = @import("builtin");
12const io = @import("io.zig");
23const mem = @import("mem.zig");
34const debug = @import("debug.zig");
......@@ -625,9 +626,9 @@ const Version = struct {
625626};
626627
627628const CrossTarget = struct {
628 arch: Arch,
629 os: Os,
630 environ: Environ,
629 arch: builtin.Arch,
630 os: builtin.Os,
631 environ: builtin.Environ,
631632};
632633
633634const Target = enum {
......@@ -636,22 +637,22 @@ const Target = enum {
636637
637638 pub fn oFileExt(self: &const Target) -> []const u8 {
638639 const environ = switch (*self) {
639 Target.Native => @compileVar("environ"),
640 Target.Native => builtin.environ,
640641 Target.Cross => |t| t.environ,
641642 };
642643 return switch (environ) {
643 Environ.msvc => ".obj",
644 builtin.Environ.msvc => ".obj",
644645 else => ".o",
645646 };
646647 }
647648
648649 pub fn exeFileExt(self: &const Target) -> []const u8 {
649650 const target_os = switch (*self) {
650 Target.Native => @compileVar("os"),
651 Target.Native => builtin.os,
651652 Target.Cross => |t| t.os,
652653 };
653654 return switch (target_os) {
654 Os.windows => ".exe",
655 builtin.Os.windows => ".exe",
655656 else => "",
656657 };
657658 }
......@@ -761,7 +762,9 @@ pub const LibExeObjStep = struct {
761762 }
762763 }
763764
764 pub fn setTarget(self: &LibExeObjStep, target_arch: Arch, target_os: Os, target_environ: Environ) {
765 pub fn setTarget(self: &LibExeObjStep, target_arch: builtin.Arch, target_os: builtin.Os,
766 target_environ: builtin.Environ)
767 {
765768 self.target = Target.Cross {
766769 CrossTarget {
767770 .arch = target_arch,
......@@ -1392,7 +1395,9 @@ pub const CLibExeObjStep = struct {
13921395 }
13931396 }
13941397
1395 pub fn setTarget(self: &CLibExeObjStep, target_arch: Arch, target_os: Os, target_environ: Environ) {
1398 pub fn setTarget(self: &CLibExeObjStep, target_arch: builtin.Arch, target_os: builtin.Os,
1399 target_environ: builtin.Environ)
1400 {
13961401 self.target = Target.Cross {
13971402 CrossTarget {
13981403 .arch = target_arch,
std/c/index.zig+3-1
......@@ -1,6 +1,8 @@
11pub use @import("../os/errno.zig");
2const builtin = @import("builtin");
3const Os = builtin.Os;
24
3pub use switch(@compileVar("os")) {
5pub use switch(builtin.os) {
46 Os.linux => @import("linux.zig"),
57 Os.windows => @import("windows.zig"),
68 Os.darwin, Os.macosx, Os.ios => @import("darwin.zig"),
std/debug.zig+6-5
......@@ -4,6 +4,7 @@ const os = @import("os/index.zig");
44const elf = @import("elf.zig");
55const DW = @import("dwarf.zig");
66const List = @import("list.zig").List;
7const builtin = @import("builtin");
78
89error MissingDebugInfo;
910error InvalidDebugInfo;
......@@ -50,8 +51,8 @@ pub var user_main_fn: ?fn() -> %void = null;
5051pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty_color: bool,
5152 ignore_frame_count: usize) -> %void
5253{
53 switch (@compileVar("object_format")) {
54 ObjectFormat.elf => {
54 switch (builtin.object_format) {
55 builtin.ObjectFormat.elf => {
5556 var stack_trace = ElfStackTrace {
5657 .self_exe_stream = undefined,
5758 .elf = undefined,
......@@ -125,13 +126,13 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty
125126 %return out_stream.flush();
126127 }
127128 },
128 ObjectFormat.coff => {
129 builtin.ObjectFormat.coff => {
129130 %return out_stream.write("(stack trace unavailable for COFF object format)\n");
130131 },
131 ObjectFormat.macho => {
132 builtin.ObjectFormat.macho => {
132133 %return out_stream.write("(stack trace unavailable for Mach-O object format)\n");
133134 },
134 ObjectFormat.unknown => {
135 builtin.ObjectFormat.unknown => {
135136 %return out_stream.write("(stack trace unavailable for unknown object format)\n");
136137 },
137138 }
std/elf.zig+11-13
......@@ -36,9 +36,7 @@ pub const FileType = enum {
3636 Core,
3737};
3838
39// TODO rename this to Arch when the builtin Arch enum is namespaced
40// or make debug info work for builtin enums
41pub const ElfArch = enum {
39pub const Arch = enum {
4240 Sparc,
4341 x86,
4442 Mips,
......@@ -69,7 +67,7 @@ pub const Elf = struct {
6967 is_64: bool,
7068 is_big_endian: bool,
7169 file_type: FileType,
72 arch: ElfArch,
70 arch: Arch,
7371 entry_addr: u64,
7472 program_header_offset: u64,
7573 section_header_offset: u64,
......@@ -123,15 +121,15 @@ pub const Elf = struct {
123121 };
124122
125123 elf.arch = switch (%return elf.in_stream.readInt(elf.is_big_endian, u16)) {
126 0x02 => ElfArch.Sparc,
127 0x03 => ElfArch.x86,
128 0x08 => ElfArch.Mips,
129 0x14 => ElfArch.PowerPc,
130 0x28 => ElfArch.Arm,
131 0x2A => ElfArch.SuperH,
132 0x32 => ElfArch.IA_64,
133 0x3E => ElfArch.x86_64,
134 0xb7 => ElfArch.AArch64,
124 0x02 => Arch.Sparc,
125 0x03 => Arch.x86,
126 0x08 => Arch.Mips,
127 0x14 => Arch.PowerPc,
128 0x28 => Arch.Arm,
129 0x2A => Arch.SuperH,
130 0x32 => Arch.IA_64,
131 0x3E => Arch.x86_64,
132 0xb7 => Arch.AArch64,
135133 else => return error.InvalidFormat,
136134 };
137135
std/endian.zig+2-1
......@@ -1,4 +1,5 @@
11const mem = @import("mem.zig");
2const builtin = @import("builtin");
23
34pub fn swapIfLe(comptime T: type, x: T) -> T {
45 swapIf(false, T, x)
......@@ -9,7 +10,7 @@ pub fn swapIfBe(comptime T: type, x: T) -> T {
910}
1011
1112pub fn swapIf(is_be: bool, comptime T: type, x: T) -> T {
12 if (@compileVar("is_big_endian") == is_be) swap(T, x) else x
13 if (builtin.is_big_endian == is_be) swap(T, x) else x
1314}
1415
1516pub fn swap(comptime T: type, x: T) -> T {
std/hash_map.zig+2-1
......@@ -3,8 +3,9 @@ const assert = debug.assert;
33const math = @import("math.zig");
44const mem = @import("mem.zig");
55const Allocator = mem.Allocator;
6const builtin = @import("builtin");
67
7const want_modification_safety = !@compileVar("is_release");
8const want_modification_safety = !builtin.is_release;
89const debug_u32 = if (want_modification_safety) u32 else void;
910
1011pub fn HashMap(comptime K: type, comptime V: type,
std/io.zig+11-9
......@@ -1,4 +1,6 @@
1const system = switch(@compileVar("os")) {
1const builtin = @import("builtin");
2const Os = builtin.Os;
3const system = switch(builtin.os) {
24 Os.linux => @import("os/linux.zig"),
35 Os.darwin => @import("os/darwin.zig"),
46 else => @compileError("Unsupported OS"),
......@@ -79,7 +81,7 @@ pub const OutStream = struct {
7981 /// otherwise if the fixed size buffer is too small, allocator is used to obtain the needed memory.
8082 /// Call close to clean up.
8183 pub fn openMode(path: []const u8, mode: usize, allocator: ?&mem.Allocator) -> %OutStream {
82 switch (@compileVar("os")) {
84 switch (builtin.os) {
8385 Os.linux, Os.darwin, Os.macosx, Os.ios => {
8486 const flags = system.O_LARGEFILE|system.O_WRONLY|system.O_CREAT|system.O_CLOEXEC|system.O_TRUNC;
8587 const fd = %return os.posixOpen(path, flags, mode, allocator);
......@@ -176,7 +178,7 @@ pub const InStream = struct {
176178 /// otherwise if the fixed size buffer is too small, allocator is used to obtain the needed memory.
177179 /// Call close to clean up.
178180 pub fn open(path: []const u8, allocator: ?&mem.Allocator) -> %InStream {
179 switch (@compileVar("os")) {
181 switch (builtin.os) {
180182 Os.linux, Os.darwin, Os.macosx, Os.ios => {
181183 const flags = system.O_LARGEFILE|system.O_RDONLY;
182184 const fd = %return os.posixOpen(path, flags, 0, allocator);
......@@ -191,7 +193,7 @@ pub const InStream = struct {
191193 /// Upon success, the stream is in an uninitialized state. To continue using it,
192194 /// you must use the open() function.
193195 pub fn close(self: &InStream) {
194 switch (@compileVar("os")) {
196 switch (builtin.os) {
195197 Os.linux, Os.darwin, Os.macosx, Os.ios => {
196198 os.posixClose(self.fd);
197199 },
......@@ -202,7 +204,7 @@ pub const InStream = struct {
202204 /// Returns the number of bytes read. If the number read is smaller than buf.len, then
203205 /// the stream reached End Of File.
204206 pub fn read(is: &InStream, buf: []u8) -> %usize {
205 switch (@compileVar("os")) {
207 switch (builtin.os) {
206208 Os.linux, Os.darwin => {
207209 var index: usize = 0;
208210 while (index < buf.len) {
......@@ -268,7 +270,7 @@ pub const InStream = struct {
268270 }
269271
270272 pub fn seekForward(is: &InStream, amount: usize) -> %void {
271 switch (@compileVar("os")) {
273 switch (builtin.os) {
272274 Os.linux, Os.darwin => {
273275 const result = system.lseek(is.fd, amount, system.SEEK_CUR);
274276 const err = system.getErrno(result);
......@@ -288,7 +290,7 @@ pub const InStream = struct {
288290 }
289291
290292 pub fn seekTo(is: &InStream, pos: usize) -> %void {
291 switch (@compileVar("os")) {
293 switch (builtin.os) {
292294 Os.linux, Os.darwin => {
293295 const result = system.lseek(is.fd, pos, system.SEEK_SET);
294296 const err = system.getErrno(result);
......@@ -308,7 +310,7 @@ pub const InStream = struct {
308310 }
309311
310312 pub fn getPos(is: &InStream) -> %usize {
311 switch (@compileVar("os")) {
313 switch (builtin.os) {
312314 Os.linux, Os.darwin => {
313315 const result = system.lseek(is.fd, 0, system.SEEK_CUR);
314316 const err = system.getErrno(result);
......@@ -365,7 +367,7 @@ pub const InStream = struct {
365367};
366368
367369pub fn openSelfExe() -> %InStream {
368 switch (@compileVar("os")) {
370 switch (builtin.os) {
369371 Os.linux => {
370372 return InStream.open("/proc/self/exe", null);
371373 },
std/mem.zig+3-1
......@@ -2,6 +2,8 @@ const assert = @import("debug.zig").assert;
22const math = @import("math.zig");
33const os = @import("os/index.zig");
44const io = @import("io.zig");
5const builtin = @import("builtin");
6const Os = builtin.Os;
57
68pub const Cmp = math.Cmp;
79
......@@ -53,7 +55,7 @@ pub const IncrementingAllocator = struct {
5355 end_index: usize,
5456
5557 fn init(capacity: usize) -> %IncrementingAllocator {
56 switch (@compileVar("os")) {
58 switch (builtin.os) {
5759 Os.linux, Os.darwin, Os.macosx, Os.ios => {
5860 const p = os.posix;
5961 const addr = p.mmap(null, capacity, p.PROT_READ|p.PROT_WRITE,
std/os/child_process.zig+3-1
......@@ -7,6 +7,8 @@ const errno = @import("errno.zig");
77const debug = @import("../debug.zig");
88const assert = debug.assert;
99const BufMap = @import("../buf_map.zig").BufMap;
10const builtin = @import("builtin");
11const Os = builtin.Os;
1012
1113pub const ChildProcess = struct {
1214 pid: i32,
......@@ -34,7 +36,7 @@ pub const ChildProcess = struct {
3436 cwd: ?[]const u8, env_map: &const BufMap,
3537 stdin: StdIo, stdout: StdIo, stderr: StdIo, allocator: &Allocator) -> %ChildProcess
3638 {
37 switch (@compileVar("os")) {
39 switch (builtin.os) {
3840 Os.linux, Os.macosx, Os.ios, Os.darwin => {
3941 return spawnPosix(exe_path, args, cwd, env_map, stdin, stdout, stderr, allocator);
4042 },
std/os/darwin.zig+3-2
......@@ -1,6 +1,7 @@
11
2const arch = switch (@compileVar("arch")) {
3 Arch.x86_64 => @import("darwin_x86_64.zig"),
2const builtin = @import("builtin");
3const arch = switch (builtin.arch) {
4 builtin.Arch.x86_64 => @import("darwin_x86_64.zig"),
45 else => @compileError("unsupported arch"),
56};
67
std/os/index.zig+6-4
......@@ -1,7 +1,9 @@
1const builtin = @import("builtin");
2const Os = builtin.Os;
13pub const windows = @import("windows.zig");
24pub const darwin = @import("darwin.zig");
35pub const linux = @import("linux.zig");
4pub const posix = switch(@compileVar("os")) {
6pub const posix = switch(builtin.os) {
57 Os.linux => linux,
68 Os.darwin, Os.macosx, Os.ios => darwin,
79 Os.windows => windows,
......@@ -12,7 +14,7 @@ pub const max_noalloc_path_len = 1024;
1214pub const ChildProcess = @import("child_process.zig").ChildProcess;
1315pub const path = @import("path.zig");
1416
15pub const line_sep = switch (@compileVar("os")) {
17pub const line_sep = switch (builtin.os) {
1618 Os.windows => "\r\n",
1719 else => "\n",
1820};
......@@ -56,7 +58,7 @@ error DirNotEmpty;
5658/// library implementation.
5759pub fn getRandomBytes(buf: []u8) -> %void {
5860 while (true) {
59 const err = switch (@compileVar("os")) {
61 const err = switch (builtin.os) {
6062 Os.linux => {
6163 if (linking_libc) {
6264 if (c.getrandom(buf.ptr, buf.len, 0) == -1) *c._errno() else 0
......@@ -104,7 +106,7 @@ pub coldcc fn abort() -> noreturn {
104106 if (linking_libc) {
105107 c.abort();
106108 }
107 switch (@compileVar("os")) {
109 switch (builtin.os) {
108110 Os.linux, Os.darwin, Os.macosx, Os.ios => {
109111 _ = posix.raise(posix.SIGABRT);
110112 _ = posix.raise(posix.SIGKILL);
std/os/linux.zig+4-3
......@@ -1,6 +1,7 @@
1const arch = switch (@compileVar("arch")) {
2 Arch.x86_64 => @import("linux_x86_64.zig"),
3 Arch.i386 => @import("linux_i386.zig"),
1const builtin = @import("builtin");
2const arch = switch (builtin.arch) {
3 builtin.Arch.x86_64 => @import("linux_x86_64.zig"),
4 builtin.Arch.i386 => @import("linux_i386.zig"),
45 else => @compileError("unsupported arch"),
56};
67const errno = @import("errno.zig");
std/os/path.zig+5-3
......@@ -1,3 +1,5 @@
1const builtin = @import("builtin");
2const Os = builtin.Os;
13const debug = @import("../debug.zig");
24const assert = debug.assert;
35const mem = @import("../mem.zig");
......@@ -7,11 +9,11 @@ const os = @import("index.zig");
79const math = @import("../math.zig");
810const posix = os.posix;
911
10pub const sep = switch (@compileVar("os")) {
12pub const sep = switch (builtin.os) {
1113 Os.windows => '\\',
1214 else => '/',
1315};
14pub const delimiter = switch (@compileVar("os")) {
16pub const delimiter = switch (builtin.os) {
1517 Os.windows => ';',
1618 else => ':',
1719};
......@@ -61,7 +63,7 @@ test "os.path.join" {
6163}
6264
6365pub fn isAbsolute(path: []const u8) -> bool {
64 switch (@compileVar("os")) {
66 switch (builtin.os) {
6567 Os.windows => @compileError("Unsupported OS"),
6668 else => return path[0] == sep,
6769 }
std/special/bootstrap.zig+6-5
......@@ -3,6 +3,7 @@
33
44const root = @import("@root");
55const std = @import("std");
6const builtin = @import("builtin");
67
78const want_main_symbol = std.target.linking_libc;
89const want_start_symbol = !want_main_symbol;
......@@ -13,15 +14,15 @@ var argc_ptr: &usize = undefined;
1314
1415export nakedcc fn _start() -> noreturn {
1516 if (!want_start_symbol) {
16 @setGlobalLinkage(_start, GlobalLinkage.Internal);
17 @setGlobalLinkage(_start, builtin.GlobalLinkage.Internal);
1718 unreachable;
1819 }
1920
20 switch (@compileVar("arch")) {
21 Arch.x86_64 => {
21 switch (builtin.arch) {
22 builtin.Arch.x86_64 => {
2223 argc_ptr = asm("lea (%%rsp), %[argc]": [argc] "=r" (-> &usize));
2324 },
24 Arch.i386 => {
25 builtin.Arch.i386 => {
2526 argc_ptr = asm("lea (%%esp), %[argc]": [argc] "=r" (-> &usize));
2627 },
2728 else => @compileError("unsupported arch"),
......@@ -51,7 +52,7 @@ fn callMain(argc: usize, argv: &&u8, envp: &?&u8) -> %void {
5152
5253export fn main(c_argc: i32, c_argv: &&u8, c_envp: &?&u8) -> i32 {
5354 if (!want_main_symbol) {
54 @setGlobalLinkage(main, GlobalLinkage.Internal);
55 @setGlobalLinkage(main, builtin.GlobalLinkage.Internal);
5556 unreachable;
5657 }
5758
std/special/compiler_rt.zig+23-21
......@@ -1,3 +1,5 @@
1const builtin = @import("builtin");
2
13const CHAR_BIT = 8;
24const du_int = u64;
35const di_int = i64;
......@@ -5,7 +7,7 @@ const si_int = c_int;
57const su_int = c_uint;
68
79const udwords = [2]su_int;
8const low = if (@compileVar("is_big_endian")) 1 else 0;
10const low = if (builtin.is_big_endian) 1 else 0;
911const high = 1 - low;
1012
1113export fn __udivdi3(a: du_int, b: du_int) -> du_int {
......@@ -213,25 +215,25 @@ export fn __umoddi3(a: du_int, b: du_int) -> du_int {
213215}
214216
215217fn isArmArch() -> bool {
216 return switch (@compileVar("arch")) {
217 Arch.armv8_2a,
218 Arch.armv8_1a,
219 Arch.armv8,
220 Arch.armv8m_baseline,
221 Arch.armv8m_mainline,
222 Arch.armv7,
223 Arch.armv7em,
224 Arch.armv7m,
225 Arch.armv7s,
226 Arch.armv7k,
227 Arch.armv6,
228 Arch.armv6m,
229 Arch.armv6k,
230 Arch.armv6t2,
231 Arch.armv5,
232 Arch.armv5te,
233 Arch.armv4t,
234 Arch.armeb => true,
218 return switch (builtin.arch) {
219 builtin.Arch.armv8_2a,
220 builtin.Arch.armv8_1a,
221 builtin.Arch.armv8,
222 builtin.Arch.armv8m_baseline,
223 builtin.Arch.armv8m_mainline,
224 builtin.Arch.armv7,
225 builtin.Arch.armv7em,
226 builtin.Arch.armv7m,
227 builtin.Arch.armv7s,
228 builtin.Arch.armv7k,
229 builtin.Arch.armv6,
230 builtin.Arch.armv6m,
231 builtin.Arch.armv6k,
232 builtin.Arch.armv6t2,
233 builtin.Arch.armv5,
234 builtin.Arch.armv5te,
235 builtin.Arch.armv4t,
236 builtin.Arch.armeb => true,
235237 else => false,
236238 };
237239}
......@@ -252,7 +254,7 @@ export nakedcc fn __aeabi_uidivmod() {
252254 unreachable;
253255 }
254256
255 @setGlobalLinkage(__aeabi_uidivmod, GlobalLinkage.Internal);
257 @setGlobalLinkage(__aeabi_uidivmod, builtin.GlobalLinkage.Internal);
256258}
257259
258260export fn __udivmodsi4(a: su_int, b: su_int, rem: &su_int) -> su_int {
std/special/test_runner.zig+2-1
......@@ -1,5 +1,6 @@
11const io = @import("std").io;
2const test_fn_list = @compileVar("zig_test_fn_slice");
2const builtin = @import("builtin");
3const test_fn_list = builtin.__zig_test_fn_slice;
34
45pub fn main() -> %void {
56 for (test_fn_list) |test_fn, i| {
std/special/zigrt.zig+5-3
......@@ -2,13 +2,15 @@
22// multiple .o files. The symbols are defined Weak so that multiple
33// instances of zig_rt.zig do not conflict with each other.
44
5const builtin = @import("builtin");
6
57export coldcc fn __zig_panic(message_ptr: &const u8, message_len: usize) -> noreturn {
6 @setGlobalLinkage(__zig_panic, GlobalLinkage.Weak);
8 @setGlobalLinkage(__zig_panic, builtin.GlobalLinkage.Weak);
79 @setDebugSafety(this, false);
810
9 if (@compileVar("panic_implementation_provided")) {
11 if (builtin.__zig_panic_implementation_provided) {
1012 @import("@root").panic(message_ptr[0...message_len]);
11 } else if (@compileVar("os") == Os.freestanding) {
13 } else if (builtin.os == builtin.Os.freestanding) {
1214 while (true) {}
1315 } else {
1416 @import("std").debug.panic("{}", message_ptr[0...message_len]);
std/target.zig+3-2
......@@ -1,11 +1,12 @@
11const mem = @import("mem.zig");
2const builtin = @import("builtin");
23
34pub const linking_libc = linkingLibrary("c");
45
56pub fn linkingLibrary(lib_name: []const u8) -> bool {
67 // TODO shouldn't need this if
7 if (@compileVar("link_libs").len != 0) {
8 for (@compileVar("link_libs")) |link_lib| {
8 if (builtin.link_libs.len != 0) {
9 for (builtin.link_libs) |link_lib| {
910 if (mem.eql(u8, link_lib, lib_name)) {
1011 return true;
1112 }
test/assemble_and_link.zig+2-1
......@@ -1,7 +1,8 @@
1const builtin = @import("builtin");
12const tests = @import("tests.zig");
23
34pub fn addCases(cases: &tests.CompareOutputContext) {
4 if (@compileVar("os") == Os.linux and @compileVar("arch") == Arch.x86_64) {
5 if (builtin.os == builtin.Os.linux and builtin.arch == builtin.Arch.x86_64) {
56 cases.addAsm("hello world linux x86_64",
67 \\.text
78 \\.globl _start
test/cases/asm.zig+3-2
......@@ -1,7 +1,8 @@
1const config = @import("builtin");
12const assert = @import("std").debug.assert;
23
34comptime {
4 if (@compileVar("arch") == Arch.x86_64) {
5 if (config.arch == config.Arch.x86_64) {
56 asm volatile (
67 \\.globl aoeu;
78 \\.type aoeu, @function;
......@@ -11,7 +12,7 @@ comptime {
1112}
1213
1314test "module level assembly" {
14 if (@compileVar("arch") == Arch.x86_64) {
15 if (config.arch == config.Arch.x86_64) {
1516 assert(aoeu() == 1234);
1617 }
1718}
test/cases/atomics.zig+1
......@@ -1,4 +1,5 @@
11const assert = @import("std").debug.assert;
2const AtomicOrder = @import("builtin").AtomicOrder;
23
34test "cmpxchg" {
45 var x: i32 = 1234;
test/cases/misc.zig+2-1
......@@ -1,6 +1,7 @@
11const assert = @import("std").debug.assert;
22const mem = @import("std").mem;
33const cstr = @import("std").cstr;
4const builtin = @import("builtin");
45
56// normal comment
67/// this is a documentation comment
......@@ -12,7 +13,7 @@ test "emptyFunctionWithComments" {
1213}
1314
1415export fn disabledExternFn() {
15 @setGlobalLinkage(disabledExternFn, GlobalLinkage.Internal);
16 @setGlobalLinkage(disabledExternFn, builtin.GlobalLinkage.Internal);
1617}
1718
1819test "callDisabledExternFn" {
test/cases/namespace_depends_on_compile_var/index.zig+3-2
......@@ -1,3 +1,4 @@
1const builtin = @import("builtin");
12const assert = @import("std").debug.assert;
23
34test "namespaceDependsOnCompileVar" {
......@@ -7,7 +8,7 @@ test "namespaceDependsOnCompileVar" {
78 assert(!some_namespace.a_bool);
89 }
910}
10const some_namespace = switch(@compileVar("os")) {
11 Os.linux => @import("a.zig"),
11const some_namespace = switch(builtin.os) {
12 builtin.Os.linux => @import("a.zig"),
1213 else => @import("b.zig"),
1314};
test/compile_errors.zig+9-6
......@@ -688,9 +688,9 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
688688
689689
690690 cases.add("bogus compile var",
691 \\const x = @compileVar("bogus");
691 \\const x = @import("builtin").bogus;
692692 \\export fn entry() -> usize { @sizeOf(@typeOf(x)) }
693 , ".tmp_source.zig:1:23: error: unrecognized compile variable: 'bogus'");
693 , ".tmp_source.zig:1:29: error: no member named 'bogus' in '");
694694
695695
696696 cases.add("non constant expression in array size outside function",
......@@ -910,18 +910,20 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
910910 , ".tmp_source.zig:2:15: error: unable to infer expression type");
911911
912912 cases.add("atomic orderings of cmpxchg - failure stricter than success",
913 \\const AtomicOrder = @import("builtin").AtomicOrder;
913914 \\export fn f() {
914915 \\ var x: i32 = 1234;
915916 \\ while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.Monotonic, AtomicOrder.SeqCst)) {}
916917 \\}
917 , ".tmp_source.zig:3:72: error: failure atomic ordering must be no stricter than success");
918 , ".tmp_source.zig:4:72: error: failure atomic ordering must be no stricter than success");
918919
919920 cases.add("atomic orderings of cmpxchg - success Monotonic or stricter",
921 \\const AtomicOrder = @import("builtin").AtomicOrder;
920922 \\export fn f() {
921923 \\ var x: i32 = 1234;
922924 \\ while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.Unordered, AtomicOrder.Unordered)) {}
923925 \\}
924 , ".tmp_source.zig:3:49: error: success atomic ordering must be Monotonic or stricter");
926 , ".tmp_source.zig:4:49: error: success atomic ordering must be Monotonic or stricter");
925927
926928 cases.add("negation overflow in function evaluation",
927929 \\const y = neg(-128);
......@@ -1487,10 +1489,11 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
14871489 , ".tmp_source.zig:6:5: error: unable to evaluate constant expression");
14881490
14891491 cases.add("invalid member of builtin enum",
1492 \\const builtin = @import("builtin");
14901493 \\export fn entry() {
1491 \\ const foo = Arch.x86;
1494 \\ const foo = builtin.Arch.x86;
14921495 \\}
1493 , ".tmp_source.zig:2:21: error: container 'Arch' has no member called 'x86'");
1496 , ".tmp_source.zig:3:29: error: container 'Arch' has no member called 'x86'");
14941497
14951498 cases.add("int to ptr of 0 bits",
14961499 \\export fn foo() {