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`....@@ -446,23 +446,6 @@ This function can only occur inside `@c_import`.
446446
447This appends `#undef $name` to the `c_import` temporary buffer.447This 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
466### @generatedCode(expression) -> @typeOf(expression)449### @generatedCode(expression) -> @typeOf(expression)
467450
468This function wraps an expression and returns the result of the expression451This function wraps an expression and returns the result of the expression
src/all_types.hpp+3-9
...@@ -1173,7 +1173,6 @@ enum BuiltinFnId {...@@ -1173,7 +1173,6 @@ enum BuiltinFnId {
1173 BuiltinFnIdCInclude,1173 BuiltinFnIdCInclude,
1174 BuiltinFnIdCDefine,1174 BuiltinFnIdCDefine,
1175 BuiltinFnIdCUndef,1175 BuiltinFnIdCUndef,
1176 BuiltinFnIdCompileVar,
1177 BuiltinFnIdCompileErr,1176 BuiltinFnIdCompileErr,
1178 BuiltinFnIdCompileLog,1177 BuiltinFnIdCompileLog,
1179 BuiltinFnIdGeneratedCode,1178 BuiltinFnIdGeneratedCode,
...@@ -1323,7 +1322,6 @@ struct CodeGen {...@@ -1323,7 +1322,6 @@ struct CodeGen {
1323 HashMap<GenericFnTypeId *, FnTableEntry *, generic_fn_type_id_hash, generic_fn_type_id_eql> generic_table;1322 HashMap<GenericFnTypeId *, FnTableEntry *, generic_fn_type_id_hash, generic_fn_type_id_eql> generic_table;
1324 HashMap<Scope *, IrInstruction *, fn_eval_hash, fn_eval_eql> memoized_fn_eval_table;1323 HashMap<Scope *, IrInstruction *, fn_eval_hash, fn_eval_eql> memoized_fn_eval_table;
1325 HashMap<ZigLLVMFnKey, LLVMValueRef, zig_llvm_fn_key_hash, zig_llvm_fn_key_eql> llvm_fn_table;1324 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;
1327 HashMap<Buf *, Tld *, buf_hash, buf_eql_buf> exported_symbol_names;1325 HashMap<Buf *, Tld *, buf_hash, buf_eql_buf> exported_symbol_names;
1328 HashMap<Buf *, Tld *, buf_hash, buf_eql_buf> external_prototypes;1326 HashMap<Buf *, Tld *, buf_hash, buf_eql_buf> external_prototypes;
13291327
...@@ -1407,6 +1405,8 @@ struct CodeGen {...@@ -1407,6 +1405,8 @@ struct CodeGen {
1407 PackageTableEntry *std_package;1405 PackageTableEntry *std_package;
1408 PackageTableEntry *zigrt_package;1406 PackageTableEntry *zigrt_package;
1409 PackageTableEntry *test_runner_package;1407 PackageTableEntry *test_runner_package;
1408 PackageTableEntry *compile_var_package;
1409 ImportTableEntry *compile_var_import;
1410 Buf *root_out_name;1410 Buf *root_out_name;
1411 bool windows_subsystem_windows;1411 bool windows_subsystem_windows;
1412 bool windows_subsystem_console;1412 bool windows_subsystem_console;
...@@ -1635,6 +1635,7 @@ struct ScopeFnDef {...@@ -1635,6 +1635,7 @@ struct ScopeFnDef {
1635 FnTableEntry *fn_entry;1635 FnTableEntry *fn_entry;
1636};1636};
16371637
1638// synchronized with code in define_builtin_compile_vars
1638enum AtomicOrder {1639enum AtomicOrder {
1639 AtomicOrderUnordered,1640 AtomicOrderUnordered,
1640 AtomicOrderMonotonic,1641 AtomicOrderMonotonic,
...@@ -1706,7 +1707,6 @@ enum IrInstructionId {...@@ -1706,7 +1707,6 @@ enum IrInstructionId {
1706 IrInstructionIdArrayType,1707 IrInstructionIdArrayType,
1707 IrInstructionIdSliceType,1708 IrInstructionIdSliceType,
1708 IrInstructionIdAsm,1709 IrInstructionIdAsm,
1709 IrInstructionIdCompileVar,
1710 IrInstructionIdSizeOf,1710 IrInstructionIdSizeOf,
1711 IrInstructionIdTestNonNull,1711 IrInstructionIdTestNonNull,
1712 IrInstructionIdUnwrapMaybe,1712 IrInstructionIdUnwrapMaybe,
...@@ -2085,12 +2085,6 @@ struct IrInstructionAsm {...@@ -2085,12 +2085,6 @@ struct IrInstructionAsm {
2085 bool has_side_effects;2085 bool has_side_effects;
2086};2086};
20872087
2088struct IrInstructionCompileVar {
2089 IrInstruction base;
2090
2091 IrInstruction *name;
2092};
2093
2094struct IrInstructionSizeOf {2088struct IrInstructionSizeOf {
2095 IrInstruction base;2089 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...@@ -2084,6 +2084,14 @@ void init_tld(Tld *tld, TldId id, Buf *name, VisibMod visib_mod, AstNode *source
2084 tld->parent_scope = parent_scope;2084 tld->parent_scope = parent_scope;
2085}2085}
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
2087void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {2095void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
2088 switch (node->type) {2096 switch (node->type) {
2089 case NodeTypeRoot:2097 case NodeTypeRoot:
...@@ -2122,7 +2130,7 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {...@@ -2122,7 +2130,7 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
2122 if (import == g->root_import && scope_is_root_decls(&decls_scope->base) &&2130 if (import == g->root_import && scope_is_root_decls(&decls_scope->base) &&
2123 buf_eql_str(fn_name, "panic"))2131 buf_eql_str(fn_name, "panic"))
2124 {2132 {
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"),
2126 create_const_bool(g, true));2134 create_const_bool(g, true));
2127 }2135 }
21282136
src/analyze.hpp+1
...@@ -152,5 +152,6 @@ ConstParent *get_const_val_parent(CodeGen *g, ConstExprValue *value);...@@ -152,5 +152,6 @@ ConstParent *get_const_val_parent(CodeGen *g, ConstExprValue *value);
152FnTableEntry *get_extern_panic_fn(CodeGen *g);152FnTableEntry *get_extern_panic_fn(CodeGen *g);
153TypeTableEntry *create_enum_tag_type(CodeGen *g, TypeTableEntry *enum_type, TypeTableEntry *int_type);153TypeTableEntry *create_enum_tag_type(CodeGen *g, TypeTableEntry *enum_type, TypeTableEntry *int_type);
154void expand_undef_array(CodeGen *g, ConstExprValue *const_val);154void expand_undef_array(CodeGen *g, ConstExprValue *const_val);
155void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value);
155156
156#endif157#endif
src/codegen.cpp+167-228
...@@ -47,7 +47,7 @@ static void init_darwin_native(CodeGen *g) {...@@ -47,7 +47,7 @@ static void init_darwin_native(CodeGen *g) {
47 }47 }
48}48}
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) {
51 PackageTableEntry *entry = allocate<PackageTableEntry>(1);51 PackageTableEntry *entry = allocate<PackageTableEntry>(1);
52 entry->package_table.init(4);52 entry->package_table.init(4);
53 buf_init_from_str(&entry->root_src_dir, root_src_dir);53 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...@@ -70,7 +70,6 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
70 g->generic_table.init(16);70 g->generic_table.init(16);
71 g->llvm_fn_table.init(16);71 g->llvm_fn_table.init(16);
72 g->memoized_fn_eval_table.init(16);72 g->memoized_fn_eval_table.init(16);
73 g->compile_vars.init(16);
74 g->exported_symbol_names.init(8);73 g->exported_symbol_names.init(8);
75 g->external_prototypes.init(8);74 g->external_prototypes.init(8);
76 g->is_release_build = false;75 g->is_release_build = false;
...@@ -2883,7 +2882,6 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,...@@ -2883,7 +2882,6 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
2883 case IrInstructionIdSetDebugSafety:2882 case IrInstructionIdSetDebugSafety:
2884 case IrInstructionIdArrayType:2883 case IrInstructionIdArrayType:
2885 case IrInstructionIdSliceType:2884 case IrInstructionIdSliceType:
2886 case IrInstructionIdCompileVar:
2887 case IrInstructionIdSizeOf:2885 case IrInstructionIdSizeOf:
2888 case IrInstructionIdSwitchTarget:2886 case IrInstructionIdSwitchTarget:
2889 case IrInstructionIdContainerInitFields:2887 case IrInstructionIdContainerInitFields:
...@@ -3627,6 +3625,13 @@ static LLVMValueRef build_alloca(CodeGen *g, TypeTableEntry *type_entry, const c...@@ -3627,6 +3625,13 @@ static LLVMValueRef build_alloca(CodeGen *g, TypeTableEntry *type_entry, const c
3627 return result;3625 return result;
3628}3626}
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
3630static void do_code_gen(CodeGen *g) {3635static void do_code_gen(CodeGen *g) {
3631 if (g->verbose) {3636 if (g->verbose) {
3632 fprintf(stderr, "\nCode Generation:\n");3637 fprintf(stderr, "\nCode Generation:\n");
...@@ -3919,10 +3924,7 @@ static void do_code_gen(CodeGen *g) {...@@ -3919,10 +3924,7 @@ static void do_code_gen(CodeGen *g) {
3919 buf_append_str(o_basename, o_ext);3924 buf_append_str(o_basename, o_ext);
3920 Buf *output_path = buf_alloc();3925 Buf *output_path = buf_alloc();
3921 os_path_join(g->cache_dir, o_basename, output_path);3926 os_path_join(g->cache_dir, o_basename, output_path);
3922 int err;3927 ensure_cache_dir(g);
3923 if ((err = os_make_path(g->cache_dir))) {
3924 zig_panic("unable to make cache dir: %s", err_str(err));
3925 }
3926 if (ZigLLVMTargetMachineEmitToFile(g->target_machine, g->module, buf_ptr(output_path),3928 if (ZigLLVMTargetMachineEmitToFile(g->target_machine, g->module, buf_ptr(output_path),
3927 LLVMObjectFile, &err_msg, !g->is_release_build))3929 LLVMObjectFile, &err_msg, !g->is_release_build))
3928 {3930 {
...@@ -3970,36 +3972,6 @@ static const GlobalLinkageValue global_linkage_values[] = {...@@ -3970,36 +3972,6 @@ static const GlobalLinkageValue global_linkage_values[] = {
3970 {GlobalLinkageIdLinkOnce, "LinkOnce"},3972 {GlobalLinkageIdLinkOnce, "LinkOnce"},
3971};3973};
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
4003static void define_builtin_types(CodeGen *g) {3975static void define_builtin_types(CodeGen *g) {
4004 {3976 {
4005 // if this type is anywhere in the AST, we should never hit codegen.3977 // if this type is anywhere in the AST, we should never hit codegen.
...@@ -4218,167 +4190,6 @@ static void define_builtin_types(CodeGen *g) {...@@ -4218,167 +4190,6 @@ static void define_builtin_types(CodeGen *g) {
4218 g->primitive_type_table.put(&entry->name, entry);4190 g->primitive_type_table.put(&entry->name, entry);
4219 }4191 }
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 }
4382}4193}
43834194
43844195
...@@ -4475,7 +4286,6 @@ static void define_builtin_fns(CodeGen *g) {...@@ -4475,7 +4286,6 @@ static void define_builtin_fns(CodeGen *g) {
4475 create_builtin_fn(g, BuiltinFnIdCInclude, "cInclude", 1);4286 create_builtin_fn(g, BuiltinFnIdCInclude, "cInclude", 1);
4476 create_builtin_fn(g, BuiltinFnIdCDefine, "cDefine", 2);4287 create_builtin_fn(g, BuiltinFnIdCDefine, "cDefine", 2);
4477 create_builtin_fn(g, BuiltinFnIdCUndef, "cUndef", 1);4288 create_builtin_fn(g, BuiltinFnIdCUndef, "cUndef", 1);
4478 create_builtin_fn(g, BuiltinFnIdCompileVar, "compileVar", 1);
4479 create_builtin_fn(g, BuiltinFnIdGeneratedCode, "generatedCode", 1);4289 create_builtin_fn(g, BuiltinFnIdGeneratedCode, "generatedCode", 1);
4480 create_builtin_fn(g, BuiltinFnIdCtz, "ctz", 1);4290 create_builtin_fn(g, BuiltinFnIdCtz, "ctz", 1);
4481 create_builtin_fn(g, BuiltinFnIdClz, "clz", 1);4291 create_builtin_fn(g, BuiltinFnIdClz, "clz", 1);
...@@ -4506,38 +4316,159 @@ static void define_builtin_fns(CodeGen *g) {...@@ -4506,38 +4316,159 @@ static void define_builtin_fns(CodeGen *g) {
4506 create_builtin_fn(g, BuiltinFnIdOffsetOf, "offsetOf", 2);4316 create_builtin_fn(g, BuiltinFnIdOffsetOf, "offsetOf", 2);
4507}4317}
45084318
4509static void add_compile_var(CodeGen *g, const char *name, ConstExprValue *value) {4319static const char *bool_to_str(bool b) {
4510 g->compile_vars.put_unique(buf_create_from_str(name), value);4320 return b ? "true" : "false";
4511}4321}
45124322
4513static void define_builtin_compile_vars(CodeGen *g) {4323static void define_builtin_compile_vars(CodeGen *g) {
4514 add_compile_var(g, "is_big_endian", create_const_bool(g, g->is_big_endian));4324 if (g->std_package == nullptr)
4515 add_compile_var(g, "is_release", create_const_bool(g, g->is_release_build));4325 return;
4516 add_compile_var(g, "is_test", create_const_bool(g, g->is_test_build));4326
4517 add_compile_var(g, "os", create_const_enum_tag(g->builtin_types.entry_os_enum, g->target_os_index));4327 const char *builtin_zig_basename = "builtin.zig";
4518 add_compile_var(g, "arch", create_const_enum_tag(g->builtin_types.entry_arch_enum, g->target_arch_index));4328 Buf *builtin_zig_path = buf_alloc();
4519 add_compile_var(g, "environ", create_const_enum_tag(g->builtin_types.entry_environ_enum, g->target_environ_index));4329 os_path_join(g->cache_dir, buf_create_from_str(builtin_zig_basename), builtin_zig_path);
4520 add_compile_var(g, "object_format", create_const_enum_tag(4330 Buf *contents = buf_alloc();
4521 g->builtin_types.entry_oformat_enum, g->target_oformat_index));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;
4523 {4393 {
4524 TypeTableEntry *str_type = get_slice_type(g, g->builtin_types.entry_u8, true);4394 buf_appendf(contents, "pub const ObjectFormat = enum {\n");
4525 ConstExprValue *const_val = allocate<ConstExprValue>(1);4395 uint32_t field_count = (uint32_t)target_oformat_count();
4526 const_val->special = ConstValSpecialStatic;4396 for (uint32_t i = 0; i < field_count; i += 1) {
4527 const_val->type = get_array_type(g, str_type, g->link_libs.length);4397 ZigLLVM_ObjectFormatType oformat = get_target_oformat(i);
4528 const_val->data.x_array.s_none.elements = allocate<ConstExprValue>(g->link_libs.length);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");
4529 for (size_t i = 0; i < g->link_libs.length; i += 1) {4441 for (size_t i = 0; i < g->link_libs.length; i += 1) {
4530 Buf *link_lib_buf = g->link_libs.at(i);4442 Buf *link_lib_buf = g->link_libs.at(i);
4531 ConstExprValue *array_val = create_const_str_lit(g, link_lib_buf);4443 buf_appendf(contents, " \"%s\",\n", buf_ptr(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);
4533 }4444 }
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));
4536 }4459 }
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);
4538}4467}
45394468
4540static void init(CodeGen *g) {4469static void init(CodeGen *g) {
4470 if (g->module)
4471 return;
4541 assert(g->root_out_name);4472 assert(g->root_out_name);
4542 g->module = LLVMModuleCreateWithName(buf_ptr(g->root_out_name));4473 g->module = LLVMModuleCreateWithName(buf_ptr(g->root_out_name));
45434474
...@@ -4600,14 +4531,15 @@ static void init(CodeGen *g) {...@@ -4600,14 +4531,15 @@ static void init(CodeGen *g) {
4600 g->dummy_di_file = nullptr;4531 g->dummy_di_file = nullptr;
46014532
4602 define_builtin_types(g);4533 define_builtin_types(g);
4603 define_builtin_fns(g);
4604 define_builtin_compile_vars(g);
46054534
4606 g->invalid_instruction = allocate<IrInstruction>(1);4535 g->invalid_instruction = allocate<IrInstruction>(1);
4607 g->invalid_instruction->value.type = g->builtin_types.entry_invalid;4536 g->invalid_instruction->value.type = g->builtin_types.entry_invalid;
46084537
4609 g->const_void_val.special = ConstValSpecialStatic;4538 g->const_void_val.special = ConstValSpecialStatic;
4610 g->const_void_val.type = g->builtin_types.entry_void;4539 g->const_void_val.type = g->builtin_types.entry_void;
4540
4541 define_builtin_fns(g);
4542 define_builtin_compile_vars(g);
4611}4543}
46124544
4613void codegen_parseh(CodeGen *g, Buf *full_path) {4545void codegen_parseh(CodeGen *g, Buf *full_path) {
...@@ -4661,21 +4593,17 @@ static ImportTableEntry *add_special_code(CodeGen *g, PackageTableEntry *package...@@ -4661,21 +4593,17 @@ static ImportTableEntry *add_special_code(CodeGen *g, PackageTableEntry *package
4661}4593}
46624594
4663static PackageTableEntry *create_bootstrap_pkg(CodeGen *g, PackageTableEntry *pkg_with_main) {4595static PackageTableEntry *create_bootstrap_pkg(CodeGen *g, PackageTableEntry *pkg_with_main) {
4664 PackageTableEntry *package = new_package(buf_ptr(g->zig_std_special_dir), "");4596 PackageTableEntry *package = codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "bootstrap.zig");
4665 package->package_table.put(buf_create_from_str("std"), g->std_package);
4666 package->package_table.put(buf_create_from_str("@root"), pkg_with_main);4597 package->package_table.put(buf_create_from_str("@root"), pkg_with_main);
4667 return package;4598 return package;
4668}4599}
46694600
4670static PackageTableEntry *create_test_runner_pkg(CodeGen *g) {4601static PackageTableEntry *create_test_runner_pkg(CodeGen *g) {
4671 PackageTableEntry *package = new_package(buf_ptr(g->zig_std_special_dir), "test_runner.zig");4602 return codegen_create_package(g, 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;
4674}4603}
46754604
4676static PackageTableEntry *create_zigrt_pkg(CodeGen *g) {4605static PackageTableEntry *create_zigrt_pkg(CodeGen *g) {
4677 PackageTableEntry *package = new_package(buf_ptr(g->zig_std_special_dir), "");4606 PackageTableEntry *package = codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "zigrt.zig");
4678 package->package_table.put(buf_create_from_str("std"), g->std_package);
4679 package->package_table.put(buf_create_from_str("@root"), g->root_package);4607 package->package_table.put(buf_create_from_str("@root"), g->root_package);
4680 return package;4608 return package;
4681}4609}
...@@ -4723,7 +4651,7 @@ static void create_test_compile_var_and_add_test_runner(CodeGen *g) {...@@ -4723,7 +4651,7 @@ static void create_test_compile_var_and_add_test_runner(CodeGen *g) {
47234651
4724 ConstExprValue *test_fn_slice = create_const_slice(g, test_fn_array, 0, g->test_fns.length, true);4652 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);
4727 g->test_runner_package = create_test_runner_pkg(g);4655 g->test_runner_package = create_test_runner_pkg(g);
4728 g->test_runner_import = add_special_code(g, g->test_runner_package, "test_runner.zig");4656 g->test_runner_import = add_special_code(g, g->test_runner_package, "test_runner.zig");
4729}4657}
...@@ -5066,3 +4994,14 @@ void codegen_build(CodeGen *g) {...@@ -5066,3 +4994,14 @@ void codegen_build(CodeGen *g) {
5066 do_code_gen(g);4994 do_code_gen(g);
5067 gen_h_file(g);4995 gen_h_file(g);
5068}4996}
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);...@@ -52,7 +52,7 @@ void codegen_add_time_event(CodeGen *g, const char *name);
52void codegen_print_timing_report(CodeGen *g, FILE *f);52void codegen_print_timing_report(CodeGen *g, FILE *f);
53void codegen_build(CodeGen *g);53void 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);
56void codegen_add_assembly(CodeGen *g, Buf *path);56void codegen_add_assembly(CodeGen *g, Buf *path);
57void codegen_add_object(CodeGen *g, Buf *object_path);57void codegen_add_object(CodeGen *g, Buf *object_path);
5858
src/ir.cpp-55
...@@ -294,10 +294,6 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionAsm *) {...@@ -294,10 +294,6 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionAsm *) {
294 return IrInstructionIdAsm;294 return IrInstructionIdAsm;
295}295}
296296
297static constexpr IrInstructionId ir_instruction_id(IrInstructionCompileVar *) {
298 return IrInstructionIdCompileVar;
299}
300
301static constexpr IrInstructionId ir_instruction_id(IrInstructionSizeOf *) {297static constexpr IrInstructionId ir_instruction_id(IrInstructionSizeOf *) {
302 return IrInstructionIdSizeOf;298 return IrInstructionIdSizeOf;
303}299}
...@@ -1249,15 +1245,6 @@ static IrInstruction *ir_build_asm_from(IrBuilder *irb, IrInstruction *old_instr...@@ -1249,15 +1245,6 @@ static IrInstruction *ir_build_asm_from(IrBuilder *irb, IrInstruction *old_instr
1249 return new_instruction;1245 return new_instruction;
1250}1246}
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
1261static IrInstruction *ir_build_size_of(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *type_value) {1248static IrInstruction *ir_build_size_of(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *type_value) {
1262 IrInstructionSizeOf *instruction = ir_build_instruction<IrInstructionSizeOf>(irb, scope, source_node);1249 IrInstructionSizeOf *instruction = ir_build_instruction<IrInstructionSizeOf>(irb, scope, source_node);
1263 instruction->type_value = type_value;1250 instruction->type_value = type_value;
...@@ -2431,13 +2418,6 @@ static IrInstruction *ir_instruction_asm_get_dep(IrInstructionAsm *instruction,...@@ -2431,13 +2418,6 @@ static IrInstruction *ir_instruction_asm_get_dep(IrInstructionAsm *instruction,
2431 return nullptr;2418 return nullptr;
2432}2419}
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
2441static IrInstruction *ir_instruction_sizeof_get_dep(IrInstructionSizeOf *instruction, size_t index) {2421static IrInstruction *ir_instruction_sizeof_get_dep(IrInstructionSizeOf *instruction, size_t index) {
2442 switch (index) {2422 switch (index) {
2443 case 0: return instruction->type_value;2423 case 0: return instruction->type_value;
...@@ -2979,8 +2959,6 @@ static IrInstruction *ir_instruction_get_dep(IrInstruction *instruction, size_t...@@ -2979,8 +2959,6 @@ static IrInstruction *ir_instruction_get_dep(IrInstruction *instruction, size_t
2979 return ir_instruction_slicetype_get_dep((IrInstructionSliceType *) instruction, index);2959 return ir_instruction_slicetype_get_dep((IrInstructionSliceType *) instruction, index);
2980 case IrInstructionIdAsm:2960 case IrInstructionIdAsm:
2981 return ir_instruction_asm_get_dep((IrInstructionAsm *) instruction, index);2961 return ir_instruction_asm_get_dep((IrInstructionAsm *) instruction, index);
2982 case IrInstructionIdCompileVar:
2983 return ir_instruction_compilevar_get_dep((IrInstructionCompileVar *) instruction, index);
2984 case IrInstructionIdSizeOf:2962 case IrInstructionIdSizeOf:
2985 return ir_instruction_sizeof_get_dep((IrInstructionSizeOf *) instruction, index);2963 return ir_instruction_sizeof_get_dep((IrInstructionSizeOf *) instruction, index);
2986 case IrInstructionIdTestNonNull:2964 case IrInstructionIdTestNonNull:
...@@ -3937,15 +3915,6 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo...@@ -3937,15 +3915,6 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
39373915
3938 return ir_build_set_debug_safety(irb, scope, node, arg0_value, arg1_value);3916 return ir_build_set_debug_safety(irb, scope, node, arg0_value, arg1_value);
3939 }3917 }
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 }
3949 case BuiltinFnIdSizeof:3918 case BuiltinFnIdSizeof:
3950 {3919 {
3951 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);3920 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
...@@ -10409,27 +10378,6 @@ static TypeTableEntry *ir_analyze_instruction_array_type(IrAnalyze *ira,...@@ -10409,27 +10378,6 @@ static TypeTableEntry *ir_analyze_instruction_array_type(IrAnalyze *ira,
10409 zig_unreachable();10378 zig_unreachable();
10410}10379}
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
10433static TypeTableEntry *ir_analyze_instruction_size_of(IrAnalyze *ira,10381static TypeTableEntry *ir_analyze_instruction_size_of(IrAnalyze *ira,
10434 IrInstructionSizeOf *size_of_instruction)10382 IrInstructionSizeOf *size_of_instruction)
10435{10383{
...@@ -13039,8 +12987,6 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi...@@ -13039,8 +12987,6 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
13039 return ir_analyze_instruction_asm(ira, (IrInstructionAsm *)instruction);12987 return ir_analyze_instruction_asm(ira, (IrInstructionAsm *)instruction);
13040 case IrInstructionIdArrayType:12988 case IrInstructionIdArrayType:
13041 return ir_analyze_instruction_array_type(ira, (IrInstructionArrayType *)instruction);12989 return ir_analyze_instruction_array_type(ira, (IrInstructionArrayType *)instruction);
13042 case IrInstructionIdCompileVar:
13043 return ir_analyze_instruction_compile_var(ira, (IrInstructionCompileVar *)instruction);
13044 case IrInstructionIdSizeOf:12990 case IrInstructionIdSizeOf:
13045 return ir_analyze_instruction_size_of(ira, (IrInstructionSizeOf *)instruction);12991 return ir_analyze_instruction_size_of(ira, (IrInstructionSizeOf *)instruction);
13046 case IrInstructionIdTestNonNull:12992 case IrInstructionIdTestNonNull:
...@@ -13292,7 +13238,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {...@@ -13292,7 +13238,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
13292 case IrInstructionIdEnumFieldPtr:13238 case IrInstructionIdEnumFieldPtr:
13293 case IrInstructionIdArrayType:13239 case IrInstructionIdArrayType:
13294 case IrInstructionIdSliceType:13240 case IrInstructionIdSliceType:
13295 case IrInstructionIdCompileVar:
13296 case IrInstructionIdSizeOf:13241 case IrInstructionIdSizeOf:
13297 case IrInstructionIdTestNonNull:13242 case IrInstructionIdTestNonNull:
13298 case IrInstructionIdUnwrapMaybe:13243 case IrInstructionIdUnwrapMaybe:
src/ir_print.cpp-9
...@@ -403,12 +403,6 @@ static void ir_print_asm(IrPrint *irp, IrInstructionAsm *instruction) {...@@ -403,12 +403,6 @@ static void ir_print_asm(IrPrint *irp, IrInstructionAsm *instruction) {
403 fprintf(irp->f, ")");403 fprintf(irp->f, ")");
404}404}
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
412static void ir_print_size_of(IrPrint *irp, IrInstructionSizeOf *instruction) {406static void ir_print_size_of(IrPrint *irp, IrInstructionSizeOf *instruction) {
413 fprintf(irp->f, "@sizeOf(");407 fprintf(irp->f, "@sizeOf(");
414 ir_print_other_instruction(irp, instruction->type_value);408 ir_print_other_instruction(irp, instruction->type_value);
...@@ -987,9 +981,6 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {...@@ -987,9 +981,6 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
987 case IrInstructionIdAsm:981 case IrInstructionIdAsm:
988 ir_print_asm(irp, (IrInstructionAsm *)instruction);982 ir_print_asm(irp, (IrInstructionAsm *)instruction);
989 break;983 break;
990 case IrInstructionIdCompileVar:
991 ir_print_compile_var(irp, (IrInstructionCompileVar *)instruction);
992 break;
993 case IrInstructionIdSizeOf:984 case IrInstructionIdSizeOf:
994 ir_print_size_of(irp, (IrInstructionSizeOf *)instruction);985 ir_print_size_of(irp, (IrInstructionSizeOf *)instruction);
995 break;986 break;
src/main.cpp+2-2
...@@ -263,8 +263,8 @@ int main(int argc, char **argv) {...@@ -263,8 +263,8 @@ int main(int argc, char **argv) {
263 return 1;263 return 1;
264 }264 }
265265
266 PackageTableEntry *build_pkg = new_package(buf_ptr(&build_file_dirname), buf_ptr(&build_file_basename));266 PackageTableEntry *build_pkg = codegen_create_package(g, buf_ptr(&build_file_dirname),
267 build_pkg->package_table.put(buf_create_from_str("std"), g->std_package);267 buf_ptr(&build_file_basename));
268 g->root_package->package_table.put(buf_create_from_str("@build"), build_pkg);268 g->root_package->package_table.put(buf_create_from_str("@build"), build_pkg);
269 codegen_build(g);269 codegen_build(g);
270 codegen_link(g, buf_ptr(path_to_build_exe));270 codegen_link(g, buf_ptr(path_to_build_exe));
std/build.zig+14-9
...@@ -1,3 +1,4 @@...@@ -1,3 +1,4 @@
1const builtin = @import("builtin");
1const io = @import("io.zig");2const io = @import("io.zig");
2const mem = @import("mem.zig");3const mem = @import("mem.zig");
3const debug = @import("debug.zig");4const debug = @import("debug.zig");
...@@ -625,9 +626,9 @@ const Version = struct {...@@ -625,9 +626,9 @@ const Version = struct {
625};626};
626627
627const CrossTarget = struct {628const CrossTarget = struct {
628 arch: Arch,629 arch: builtin.Arch,
629 os: Os,630 os: builtin.Os,
630 environ: Environ,631 environ: builtin.Environ,
631};632};
632633
633const Target = enum {634const Target = enum {
...@@ -636,22 +637,22 @@ const Target = enum {...@@ -636,22 +637,22 @@ const Target = enum {
636637
637 pub fn oFileExt(self: &const Target) -> []const u8 {638 pub fn oFileExt(self: &const Target) -> []const u8 {
638 const environ = switch (*self) {639 const environ = switch (*self) {
639 Target.Native => @compileVar("environ"),640 Target.Native => builtin.environ,
640 Target.Cross => |t| t.environ,641 Target.Cross => |t| t.environ,
641 };642 };
642 return switch (environ) {643 return switch (environ) {
643 Environ.msvc => ".obj",644 builtin.Environ.msvc => ".obj",
644 else => ".o",645 else => ".o",
645 };646 };
646 }647 }
647648
648 pub fn exeFileExt(self: &const Target) -> []const u8 {649 pub fn exeFileExt(self: &const Target) -> []const u8 {
649 const target_os = switch (*self) {650 const target_os = switch (*self) {
650 Target.Native => @compileVar("os"),651 Target.Native => builtin.os,
651 Target.Cross => |t| t.os,652 Target.Cross => |t| t.os,
652 };653 };
653 return switch (target_os) {654 return switch (target_os) {
654 Os.windows => ".exe",655 builtin.Os.windows => ".exe",
655 else => "",656 else => "",
656 };657 };
657 }658 }
...@@ -761,7 +762,9 @@ pub const LibExeObjStep = struct {...@@ -761,7 +762,9 @@ pub const LibExeObjStep = struct {
761 }762 }
762 }763 }
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 {
765 self.target = Target.Cross {768 self.target = Target.Cross {
766 CrossTarget {769 CrossTarget {
767 .arch = target_arch,770 .arch = target_arch,
...@@ -1392,7 +1395,9 @@ pub const CLibExeObjStep = struct {...@@ -1392,7 +1395,9 @@ pub const CLibExeObjStep = struct {
1392 }1395 }
1393 }1396 }
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 {
1396 self.target = Target.Cross {1401 self.target = Target.Cross {
1397 CrossTarget {1402 CrossTarget {
1398 .arch = target_arch,1403 .arch = target_arch,
std/c/index.zig+3-1
...@@ -1,6 +1,8 @@...@@ -1,6 +1,8 @@
1pub use @import("../os/errno.zig");1pub use @import("../os/errno.zig");
2const builtin = @import("builtin");
3const Os = builtin.Os;
24
3pub use switch(@compileVar("os")) {5pub use switch(builtin.os) {
4 Os.linux => @import("linux.zig"),6 Os.linux => @import("linux.zig"),
5 Os.windows => @import("windows.zig"),7 Os.windows => @import("windows.zig"),
6 Os.darwin, Os.macosx, Os.ios => @import("darwin.zig"),8 Os.darwin, Os.macosx, Os.ios => @import("darwin.zig"),
std/debug.zig+6-5
...@@ -4,6 +4,7 @@ const os = @import("os/index.zig");...@@ -4,6 +4,7 @@ const os = @import("os/index.zig");
4const elf = @import("elf.zig");4const elf = @import("elf.zig");
5const DW = @import("dwarf.zig");5const DW = @import("dwarf.zig");
6const List = @import("list.zig").List;6const List = @import("list.zig").List;
7const builtin = @import("builtin");
78
8error MissingDebugInfo;9error MissingDebugInfo;
9error InvalidDebugInfo;10error InvalidDebugInfo;
...@@ -50,8 +51,8 @@ pub var user_main_fn: ?fn() -> %void = null;...@@ -50,8 +51,8 @@ pub var user_main_fn: ?fn() -> %void = null;
50pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty_color: bool,51pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty_color: bool,
51 ignore_frame_count: usize) -> %void52 ignore_frame_count: usize) -> %void
52{53{
53 switch (@compileVar("object_format")) {54 switch (builtin.object_format) {
54 ObjectFormat.elf => {55 builtin.ObjectFormat.elf => {
55 var stack_trace = ElfStackTrace {56 var stack_trace = ElfStackTrace {
56 .self_exe_stream = undefined,57 .self_exe_stream = undefined,
57 .elf = undefined,58 .elf = undefined,
...@@ -125,13 +126,13 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty...@@ -125,13 +126,13 @@ pub fn writeStackTrace(out_stream: &io.OutStream, allocator: &mem.Allocator, tty
125 %return out_stream.flush();126 %return out_stream.flush();
126 }127 }
127 },128 },
128 ObjectFormat.coff => {129 builtin.ObjectFormat.coff => {
129 %return out_stream.write("(stack trace unavailable for COFF object format)\n");130 %return out_stream.write("(stack trace unavailable for COFF object format)\n");
130 },131 },
131 ObjectFormat.macho => {132 builtin.ObjectFormat.macho => {
132 %return out_stream.write("(stack trace unavailable for Mach-O object format)\n");133 %return out_stream.write("(stack trace unavailable for Mach-O object format)\n");
133 },134 },
134 ObjectFormat.unknown => {135 builtin.ObjectFormat.unknown => {
135 %return out_stream.write("(stack trace unavailable for unknown object format)\n");136 %return out_stream.write("(stack trace unavailable for unknown object format)\n");
136 },137 },
137 }138 }
std/elf.zig+11-13
...@@ -36,9 +36,7 @@ pub const FileType = enum {...@@ -36,9 +36,7 @@ pub const FileType = enum {
36 Core,36 Core,
37};37};
3838
39// TODO rename this to Arch when the builtin Arch enum is namespaced39pub const Arch = enum {
40// or make debug info work for builtin enums
41pub const ElfArch = enum {
42 Sparc,40 Sparc,
43 x86,41 x86,
44 Mips,42 Mips,
...@@ -69,7 +67,7 @@ pub const Elf = struct {...@@ -69,7 +67,7 @@ pub const Elf = struct {
69 is_64: bool,67 is_64: bool,
70 is_big_endian: bool,68 is_big_endian: bool,
71 file_type: FileType,69 file_type: FileType,
72 arch: ElfArch,70 arch: Arch,
73 entry_addr: u64,71 entry_addr: u64,
74 program_header_offset: u64,72 program_header_offset: u64,
75 section_header_offset: u64,73 section_header_offset: u64,
...@@ -123,15 +121,15 @@ pub const Elf = struct {...@@ -123,15 +121,15 @@ pub const Elf = struct {
123 };121 };
124122
125 elf.arch = switch (%return elf.in_stream.readInt(elf.is_big_endian, u16)) {123 elf.arch = switch (%return elf.in_stream.readInt(elf.is_big_endian, u16)) {
126 0x02 => ElfArch.Sparc,124 0x02 => Arch.Sparc,
127 0x03 => ElfArch.x86,125 0x03 => Arch.x86,
128 0x08 => ElfArch.Mips,126 0x08 => Arch.Mips,
129 0x14 => ElfArch.PowerPc,127 0x14 => Arch.PowerPc,
130 0x28 => ElfArch.Arm,128 0x28 => Arch.Arm,
131 0x2A => ElfArch.SuperH,129 0x2A => Arch.SuperH,
132 0x32 => ElfArch.IA_64,130 0x32 => Arch.IA_64,
133 0x3E => ElfArch.x86_64,131 0x3E => Arch.x86_64,
134 0xb7 => ElfArch.AArch64,132 0xb7 => Arch.AArch64,
135 else => return error.InvalidFormat,133 else => return error.InvalidFormat,
136 };134 };
137135
std/endian.zig+2-1
...@@ -1,4 +1,5 @@...@@ -1,4 +1,5 @@
1const mem = @import("mem.zig");1const mem = @import("mem.zig");
2const builtin = @import("builtin");
23
3pub fn swapIfLe(comptime T: type, x: T) -> T {4pub fn swapIfLe(comptime T: type, x: T) -> T {
4 swapIf(false, T, x)5 swapIf(false, T, x)
...@@ -9,7 +10,7 @@ pub fn swapIfBe(comptime T: type, x: T) -> T {...@@ -9,7 +10,7 @@ pub fn swapIfBe(comptime T: type, x: T) -> T {
9}10}
1011
11pub fn swapIf(is_be: bool, comptime T: type, x: T) -> T {12pub fn swapIf(is_be: bool, comptime T: type, x: T) -> T {
12 if (@compileVar("is_big_endian") == is_be) swap(T, x) else x13 if (builtin.is_big_endian == is_be) swap(T, x) else x
13}14}
1415
15pub fn swap(comptime T: type, x: T) -> T {16pub fn swap(comptime T: type, x: T) -> T {
std/hash_map.zig+2-1
...@@ -3,8 +3,9 @@ const assert = debug.assert;...@@ -3,8 +3,9 @@ const assert = debug.assert;
3const math = @import("math.zig");3const math = @import("math.zig");
4const mem = @import("mem.zig");4const mem = @import("mem.zig");
5const Allocator = mem.Allocator;5const Allocator = mem.Allocator;
6const builtin = @import("builtin");
67
7const want_modification_safety = !@compileVar("is_release");8const want_modification_safety = !builtin.is_release;
8const debug_u32 = if (want_modification_safety) u32 else void;9const debug_u32 = if (want_modification_safety) u32 else void;
910
10pub fn HashMap(comptime K: type, comptime V: type,11pub fn HashMap(comptime K: type, comptime V: type,
std/io.zig+11-9
...@@ -1,4 +1,6 @@...@@ -1,4 +1,6 @@
1const system = switch(@compileVar("os")) {1const builtin = @import("builtin");
2const Os = builtin.Os;
3const system = switch(builtin.os) {
2 Os.linux => @import("os/linux.zig"),4 Os.linux => @import("os/linux.zig"),
3 Os.darwin => @import("os/darwin.zig"),5 Os.darwin => @import("os/darwin.zig"),
4 else => @compileError("Unsupported OS"),6 else => @compileError("Unsupported OS"),
...@@ -79,7 +81,7 @@ pub const OutStream = struct {...@@ -79,7 +81,7 @@ pub const OutStream = struct {
79 /// otherwise if the fixed size buffer is too small, allocator is used to obtain the needed memory.81 /// otherwise if the fixed size buffer is too small, allocator is used to obtain the needed memory.
80 /// Call close to clean up.82 /// Call close to clean up.
81 pub fn openMode(path: []const u8, mode: usize, allocator: ?&mem.Allocator) -> %OutStream {83 pub fn openMode(path: []const u8, mode: usize, allocator: ?&mem.Allocator) -> %OutStream {
82 switch (@compileVar("os")) {84 switch (builtin.os) {
83 Os.linux, Os.darwin, Os.macosx, Os.ios => {85 Os.linux, Os.darwin, Os.macosx, Os.ios => {
84 const flags = system.O_LARGEFILE|system.O_WRONLY|system.O_CREAT|system.O_CLOEXEC|system.O_TRUNC;86 const flags = system.O_LARGEFILE|system.O_WRONLY|system.O_CREAT|system.O_CLOEXEC|system.O_TRUNC;
85 const fd = %return os.posixOpen(path, flags, mode, allocator);87 const fd = %return os.posixOpen(path, flags, mode, allocator);
...@@ -176,7 +178,7 @@ pub const InStream = struct {...@@ -176,7 +178,7 @@ pub const InStream = struct {
176 /// otherwise if the fixed size buffer is too small, allocator is used to obtain the needed memory.178 /// otherwise if the fixed size buffer is too small, allocator is used to obtain the needed memory.
177 /// Call close to clean up.179 /// Call close to clean up.
178 pub fn open(path: []const u8, allocator: ?&mem.Allocator) -> %InStream {180 pub fn open(path: []const u8, allocator: ?&mem.Allocator) -> %InStream {
179 switch (@compileVar("os")) {181 switch (builtin.os) {
180 Os.linux, Os.darwin, Os.macosx, Os.ios => {182 Os.linux, Os.darwin, Os.macosx, Os.ios => {
181 const flags = system.O_LARGEFILE|system.O_RDONLY;183 const flags = system.O_LARGEFILE|system.O_RDONLY;
182 const fd = %return os.posixOpen(path, flags, 0, allocator);184 const fd = %return os.posixOpen(path, flags, 0, allocator);
...@@ -191,7 +193,7 @@ pub const InStream = struct {...@@ -191,7 +193,7 @@ pub const InStream = struct {
191 /// Upon success, the stream is in an uninitialized state. To continue using it,193 /// Upon success, the stream is in an uninitialized state. To continue using it,
192 /// you must use the open() function.194 /// you must use the open() function.
193 pub fn close(self: &InStream) {195 pub fn close(self: &InStream) {
194 switch (@compileVar("os")) {196 switch (builtin.os) {
195 Os.linux, Os.darwin, Os.macosx, Os.ios => {197 Os.linux, Os.darwin, Os.macosx, Os.ios => {
196 os.posixClose(self.fd);198 os.posixClose(self.fd);
197 },199 },
...@@ -202,7 +204,7 @@ pub const InStream = struct {...@@ -202,7 +204,7 @@ pub const InStream = struct {
202 /// Returns the number of bytes read. If the number read is smaller than buf.len, then204 /// Returns the number of bytes read. If the number read is smaller than buf.len, then
203 /// the stream reached End Of File.205 /// the stream reached End Of File.
204 pub fn read(is: &InStream, buf: []u8) -> %usize {206 pub fn read(is: &InStream, buf: []u8) -> %usize {
205 switch (@compileVar("os")) {207 switch (builtin.os) {
206 Os.linux, Os.darwin => {208 Os.linux, Os.darwin => {
207 var index: usize = 0;209 var index: usize = 0;
208 while (index < buf.len) {210 while (index < buf.len) {
...@@ -268,7 +270,7 @@ pub const InStream = struct {...@@ -268,7 +270,7 @@ pub const InStream = struct {
268 }270 }
269271
270 pub fn seekForward(is: &InStream, amount: usize) -> %void {272 pub fn seekForward(is: &InStream, amount: usize) -> %void {
271 switch (@compileVar("os")) {273 switch (builtin.os) {
272 Os.linux, Os.darwin => {274 Os.linux, Os.darwin => {
273 const result = system.lseek(is.fd, amount, system.SEEK_CUR);275 const result = system.lseek(is.fd, amount, system.SEEK_CUR);
274 const err = system.getErrno(result);276 const err = system.getErrno(result);
...@@ -288,7 +290,7 @@ pub const InStream = struct {...@@ -288,7 +290,7 @@ pub const InStream = struct {
288 }290 }
289291
290 pub fn seekTo(is: &InStream, pos: usize) -> %void {292 pub fn seekTo(is: &InStream, pos: usize) -> %void {
291 switch (@compileVar("os")) {293 switch (builtin.os) {
292 Os.linux, Os.darwin => {294 Os.linux, Os.darwin => {
293 const result = system.lseek(is.fd, pos, system.SEEK_SET);295 const result = system.lseek(is.fd, pos, system.SEEK_SET);
294 const err = system.getErrno(result);296 const err = system.getErrno(result);
...@@ -308,7 +310,7 @@ pub const InStream = struct {...@@ -308,7 +310,7 @@ pub const InStream = struct {
308 }310 }
309311
310 pub fn getPos(is: &InStream) -> %usize {312 pub fn getPos(is: &InStream) -> %usize {
311 switch (@compileVar("os")) {313 switch (builtin.os) {
312 Os.linux, Os.darwin => {314 Os.linux, Os.darwin => {
313 const result = system.lseek(is.fd, 0, system.SEEK_CUR);315 const result = system.lseek(is.fd, 0, system.SEEK_CUR);
314 const err = system.getErrno(result);316 const err = system.getErrno(result);
...@@ -365,7 +367,7 @@ pub const InStream = struct {...@@ -365,7 +367,7 @@ pub const InStream = struct {
365};367};
366368
367pub fn openSelfExe() -> %InStream {369pub fn openSelfExe() -> %InStream {
368 switch (@compileVar("os")) {370 switch (builtin.os) {
369 Os.linux => {371 Os.linux => {
370 return InStream.open("/proc/self/exe", null);372 return InStream.open("/proc/self/exe", null);
371 },373 },
std/mem.zig+3-1
...@@ -2,6 +2,8 @@ const assert = @import("debug.zig").assert;...@@ -2,6 +2,8 @@ const assert = @import("debug.zig").assert;
2const math = @import("math.zig");2const math = @import("math.zig");
3const os = @import("os/index.zig");3const os = @import("os/index.zig");
4const io = @import("io.zig");4const io = @import("io.zig");
5const builtin = @import("builtin");
6const Os = builtin.Os;
57
6pub const Cmp = math.Cmp;8pub const Cmp = math.Cmp;
79
...@@ -53,7 +55,7 @@ pub const IncrementingAllocator = struct {...@@ -53,7 +55,7 @@ pub const IncrementingAllocator = struct {
53 end_index: usize,55 end_index: usize,
5456
55 fn init(capacity: usize) -> %IncrementingAllocator {57 fn init(capacity: usize) -> %IncrementingAllocator {
56 switch (@compileVar("os")) {58 switch (builtin.os) {
57 Os.linux, Os.darwin, Os.macosx, Os.ios => {59 Os.linux, Os.darwin, Os.macosx, Os.ios => {
58 const p = os.posix;60 const p = os.posix;
59 const addr = p.mmap(null, capacity, p.PROT_READ|p.PROT_WRITE,61 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");...@@ -7,6 +7,8 @@ const errno = @import("errno.zig");
7const debug = @import("../debug.zig");7const debug = @import("../debug.zig");
8const assert = debug.assert;8const assert = debug.assert;
9const BufMap = @import("../buf_map.zig").BufMap;9const BufMap = @import("../buf_map.zig").BufMap;
10const builtin = @import("builtin");
11const Os = builtin.Os;
1012
11pub const ChildProcess = struct {13pub const ChildProcess = struct {
12 pid: i32,14 pid: i32,
...@@ -34,7 +36,7 @@ pub const ChildProcess = struct {...@@ -34,7 +36,7 @@ pub const ChildProcess = struct {
34 cwd: ?[]const u8, env_map: &const BufMap,36 cwd: ?[]const u8, env_map: &const BufMap,
35 stdin: StdIo, stdout: StdIo, stderr: StdIo, allocator: &Allocator) -> %ChildProcess37 stdin: StdIo, stdout: StdIo, stderr: StdIo, allocator: &Allocator) -> %ChildProcess
36 {38 {
37 switch (@compileVar("os")) {39 switch (builtin.os) {
38 Os.linux, Os.macosx, Os.ios, Os.darwin => {40 Os.linux, Os.macosx, Os.ios, Os.darwin => {
39 return spawnPosix(exe_path, args, cwd, env_map, stdin, stdout, stderr, allocator);41 return spawnPosix(exe_path, args, cwd, env_map, stdin, stdout, stderr, allocator);
40 },42 },
std/os/darwin.zig+3-2
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
11
2const arch = switch (@compileVar("arch")) {2const builtin = @import("builtin");
3 Arch.x86_64 => @import("darwin_x86_64.zig"),3const arch = switch (builtin.arch) {
4 builtin.Arch.x86_64 => @import("darwin_x86_64.zig"),
4 else => @compileError("unsupported arch"),5 else => @compileError("unsupported arch"),
5};6};
67
std/os/index.zig+6-4
...@@ -1,7 +1,9 @@...@@ -1,7 +1,9 @@
1const builtin = @import("builtin");
2const Os = builtin.Os;
1pub const windows = @import("windows.zig");3pub const windows = @import("windows.zig");
2pub const darwin = @import("darwin.zig");4pub const darwin = @import("darwin.zig");
3pub const linux = @import("linux.zig");5pub const linux = @import("linux.zig");
4pub const posix = switch(@compileVar("os")) {6pub const posix = switch(builtin.os) {
5 Os.linux => linux,7 Os.linux => linux,
6 Os.darwin, Os.macosx, Os.ios => darwin,8 Os.darwin, Os.macosx, Os.ios => darwin,
7 Os.windows => windows,9 Os.windows => windows,
...@@ -12,7 +14,7 @@ pub const max_noalloc_path_len = 1024;...@@ -12,7 +14,7 @@ pub const max_noalloc_path_len = 1024;
12pub const ChildProcess = @import("child_process.zig").ChildProcess;14pub const ChildProcess = @import("child_process.zig").ChildProcess;
13pub const path = @import("path.zig");15pub const path = @import("path.zig");
1416
15pub const line_sep = switch (@compileVar("os")) {17pub const line_sep = switch (builtin.os) {
16 Os.windows => "\r\n",18 Os.windows => "\r\n",
17 else => "\n",19 else => "\n",
18};20};
...@@ -56,7 +58,7 @@ error DirNotEmpty;...@@ -56,7 +58,7 @@ error DirNotEmpty;
56/// library implementation.58/// library implementation.
57pub fn getRandomBytes(buf: []u8) -> %void {59pub fn getRandomBytes(buf: []u8) -> %void {
58 while (true) {60 while (true) {
59 const err = switch (@compileVar("os")) {61 const err = switch (builtin.os) {
60 Os.linux => {62 Os.linux => {
61 if (linking_libc) {63 if (linking_libc) {
62 if (c.getrandom(buf.ptr, buf.len, 0) == -1) *c._errno() else 064 if (c.getrandom(buf.ptr, buf.len, 0) == -1) *c._errno() else 0
...@@ -104,7 +106,7 @@ pub coldcc fn abort() -> noreturn {...@@ -104,7 +106,7 @@ pub coldcc fn abort() -> noreturn {
104 if (linking_libc) {106 if (linking_libc) {
105 c.abort();107 c.abort();
106 }108 }
107 switch (@compileVar("os")) {109 switch (builtin.os) {
108 Os.linux, Os.darwin, Os.macosx, Os.ios => {110 Os.linux, Os.darwin, Os.macosx, Os.ios => {
109 _ = posix.raise(posix.SIGABRT);111 _ = posix.raise(posix.SIGABRT);
110 _ = posix.raise(posix.SIGKILL);112 _ = posix.raise(posix.SIGKILL);
std/os/linux.zig+4-3
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1const arch = switch (@compileVar("arch")) {1const builtin = @import("builtin");
2 Arch.x86_64 => @import("linux_x86_64.zig"),2const arch = switch (builtin.arch) {
3 Arch.i386 => @import("linux_i386.zig"),3 builtin.Arch.x86_64 => @import("linux_x86_64.zig"),
4 builtin.Arch.i386 => @import("linux_i386.zig"),
4 else => @compileError("unsupported arch"),5 else => @compileError("unsupported arch"),
5};6};
6const errno = @import("errno.zig");7const errno = @import("errno.zig");
std/os/path.zig+5-3
...@@ -1,3 +1,5 @@...@@ -1,3 +1,5 @@
1const builtin = @import("builtin");
2const Os = builtin.Os;
1const debug = @import("../debug.zig");3const debug = @import("../debug.zig");
2const assert = debug.assert;4const assert = debug.assert;
3const mem = @import("../mem.zig");5const mem = @import("../mem.zig");
...@@ -7,11 +9,11 @@ const os = @import("index.zig");...@@ -7,11 +9,11 @@ const os = @import("index.zig");
7const math = @import("../math.zig");9const math = @import("../math.zig");
8const posix = os.posix;10const posix = os.posix;
911
10pub const sep = switch (@compileVar("os")) {12pub const sep = switch (builtin.os) {
11 Os.windows => '\\',13 Os.windows => '\\',
12 else => '/',14 else => '/',
13};15};
14pub const delimiter = switch (@compileVar("os")) {16pub const delimiter = switch (builtin.os) {
15 Os.windows => ';',17 Os.windows => ';',
16 else => ':',18 else => ':',
17};19};
...@@ -61,7 +63,7 @@ test "os.path.join" {...@@ -61,7 +63,7 @@ test "os.path.join" {
61}63}
6264
63pub fn isAbsolute(path: []const u8) -> bool {65pub fn isAbsolute(path: []const u8) -> bool {
64 switch (@compileVar("os")) {66 switch (builtin.os) {
65 Os.windows => @compileError("Unsupported OS"),67 Os.windows => @compileError("Unsupported OS"),
66 else => return path[0] == sep,68 else => return path[0] == sep,
67 }69 }
std/special/bootstrap.zig+6-5
...@@ -3,6 +3,7 @@...@@ -3,6 +3,7 @@
33
4const root = @import("@root");4const root = @import("@root");
5const std = @import("std");5const std = @import("std");
6const builtin = @import("builtin");
67
7const want_main_symbol = std.target.linking_libc;8const want_main_symbol = std.target.linking_libc;
8const want_start_symbol = !want_main_symbol;9const want_start_symbol = !want_main_symbol;
...@@ -13,15 +14,15 @@ var argc_ptr: &usize = undefined;...@@ -13,15 +14,15 @@ var argc_ptr: &usize = undefined;
1314
14export nakedcc fn _start() -> noreturn {15export nakedcc fn _start() -> noreturn {
15 if (!want_start_symbol) {16 if (!want_start_symbol) {
16 @setGlobalLinkage(_start, GlobalLinkage.Internal);17 @setGlobalLinkage(_start, builtin.GlobalLinkage.Internal);
17 unreachable;18 unreachable;
18 }19 }
1920
20 switch (@compileVar("arch")) {21 switch (builtin.arch) {
21 Arch.x86_64 => {22 builtin.Arch.x86_64 => {
22 argc_ptr = asm("lea (%%rsp), %[argc]": [argc] "=r" (-> &usize));23 argc_ptr = asm("lea (%%rsp), %[argc]": [argc] "=r" (-> &usize));
23 },24 },
24 Arch.i386 => {25 builtin.Arch.i386 => {
25 argc_ptr = asm("lea (%%esp), %[argc]": [argc] "=r" (-> &usize));26 argc_ptr = asm("lea (%%esp), %[argc]": [argc] "=r" (-> &usize));
26 },27 },
27 else => @compileError("unsupported arch"),28 else => @compileError("unsupported arch"),
...@@ -51,7 +52,7 @@ fn callMain(argc: usize, argv: &&u8, envp: &?&u8) -> %void {...@@ -51,7 +52,7 @@ fn callMain(argc: usize, argv: &&u8, envp: &?&u8) -> %void {
5152
52export fn main(c_argc: i32, c_argv: &&u8, c_envp: &?&u8) -> i32 {53export fn main(c_argc: i32, c_argv: &&u8, c_envp: &?&u8) -> i32 {
53 if (!want_main_symbol) {54 if (!want_main_symbol) {
54 @setGlobalLinkage(main, GlobalLinkage.Internal);55 @setGlobalLinkage(main, builtin.GlobalLinkage.Internal);
55 unreachable;56 unreachable;
56 }57 }
5758
std/special/compiler_rt.zig+23-21
...@@ -1,3 +1,5 @@...@@ -1,3 +1,5 @@
1const builtin = @import("builtin");
2
1const CHAR_BIT = 8;3const CHAR_BIT = 8;
2const du_int = u64;4const du_int = u64;
3const di_int = i64;5const di_int = i64;
...@@ -5,7 +7,7 @@ const si_int = c_int;...@@ -5,7 +7,7 @@ const si_int = c_int;
5const su_int = c_uint;7const su_int = c_uint;
68
7const udwords = [2]su_int;9const udwords = [2]su_int;
8const low = if (@compileVar("is_big_endian")) 1 else 0;10const low = if (builtin.is_big_endian) 1 else 0;
9const high = 1 - low;11const high = 1 - low;
1012
11export fn __udivdi3(a: du_int, b: du_int) -> du_int {13export 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 {...@@ -213,25 +215,25 @@ export fn __umoddi3(a: du_int, b: du_int) -> du_int {
213}215}
214216
215fn isArmArch() -> bool {217fn isArmArch() -> bool {
216 return switch (@compileVar("arch")) {218 return switch (builtin.arch) {
217 Arch.armv8_2a,219 builtin.Arch.armv8_2a,
218 Arch.armv8_1a,220 builtin.Arch.armv8_1a,
219 Arch.armv8,221 builtin.Arch.armv8,
220 Arch.armv8m_baseline,222 builtin.Arch.armv8m_baseline,
221 Arch.armv8m_mainline,223 builtin.Arch.armv8m_mainline,
222 Arch.armv7,224 builtin.Arch.armv7,
223 Arch.armv7em,225 builtin.Arch.armv7em,
224 Arch.armv7m,226 builtin.Arch.armv7m,
225 Arch.armv7s,227 builtin.Arch.armv7s,
226 Arch.armv7k,228 builtin.Arch.armv7k,
227 Arch.armv6,229 builtin.Arch.armv6,
228 Arch.armv6m,230 builtin.Arch.armv6m,
229 Arch.armv6k,231 builtin.Arch.armv6k,
230 Arch.armv6t2,232 builtin.Arch.armv6t2,
231 Arch.armv5,233 builtin.Arch.armv5,
232 Arch.armv5te,234 builtin.Arch.armv5te,
233 Arch.armv4t,235 builtin.Arch.armv4t,
234 Arch.armeb => true,236 builtin.Arch.armeb => true,
235 else => false,237 else => false,
236 };238 };
237}239}
...@@ -252,7 +254,7 @@ export nakedcc fn __aeabi_uidivmod() {...@@ -252,7 +254,7 @@ export nakedcc fn __aeabi_uidivmod() {
252 unreachable;254 unreachable;
253 }255 }
254256
255 @setGlobalLinkage(__aeabi_uidivmod, GlobalLinkage.Internal);257 @setGlobalLinkage(__aeabi_uidivmod, builtin.GlobalLinkage.Internal);
256}258}
257259
258export fn __udivmodsi4(a: su_int, b: su_int, rem: &su_int) -> su_int {260export fn __udivmodsi4(a: su_int, b: su_int, rem: &su_int) -> su_int {
std/special/test_runner.zig+2-1
...@@ -1,5 +1,6 @@...@@ -1,5 +1,6 @@
1const io = @import("std").io;1const 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
4pub fn main() -> %void {5pub fn main() -> %void {
5 for (test_fn_list) |test_fn, i| {6 for (test_fn_list) |test_fn, i| {
std/special/zigrt.zig+5-3
...@@ -2,13 +2,15 @@...@@ -2,13 +2,15 @@
2// multiple .o files. The symbols are defined Weak so that multiple2// multiple .o files. The symbols are defined Weak so that multiple
3// instances of zig_rt.zig do not conflict with each other.3// instances of zig_rt.zig do not conflict with each other.
44
5const builtin = @import("builtin");
6
5export coldcc fn __zig_panic(message_ptr: &const u8, message_len: usize) -> noreturn {7export 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);
7 @setDebugSafety(this, false);9 @setDebugSafety(this, false);
810
9 if (@compileVar("panic_implementation_provided")) {11 if (builtin.__zig_panic_implementation_provided) {
10 @import("@root").panic(message_ptr[0...message_len]);12 @import("@root").panic(message_ptr[0...message_len]);
11 } else if (@compileVar("os") == Os.freestanding) {13 } else if (builtin.os == builtin.Os.freestanding) {
12 while (true) {}14 while (true) {}
13 } else {15 } else {
14 @import("std").debug.panic("{}", message_ptr[0...message_len]);16 @import("std").debug.panic("{}", message_ptr[0...message_len]);
std/target.zig+3-2
...@@ -1,11 +1,12 @@...@@ -1,11 +1,12 @@
1const mem = @import("mem.zig");1const mem = @import("mem.zig");
2const builtin = @import("builtin");
23
3pub const linking_libc = linkingLibrary("c");4pub const linking_libc = linkingLibrary("c");
45
5pub fn linkingLibrary(lib_name: []const u8) -> bool {6pub fn linkingLibrary(lib_name: []const u8) -> bool {
6 // TODO shouldn't need this if7 // TODO shouldn't need this if
7 if (@compileVar("link_libs").len != 0) {8 if (builtin.link_libs.len != 0) {
8 for (@compileVar("link_libs")) |link_lib| {9 for (builtin.link_libs) |link_lib| {
9 if (mem.eql(u8, link_lib, lib_name)) {10 if (mem.eql(u8, link_lib, lib_name)) {
10 return true;11 return true;
11 }12 }
test/assemble_and_link.zig+2-1
...@@ -1,7 +1,8 @@...@@ -1,7 +1,8 @@
1const builtin = @import("builtin");
1const tests = @import("tests.zig");2const tests = @import("tests.zig");
23
3pub fn addCases(cases: &tests.CompareOutputContext) {4pub 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) {
5 cases.addAsm("hello world linux x86_64",6 cases.addAsm("hello world linux x86_64",
6 \\.text7 \\.text
7 \\.globl _start8 \\.globl _start
test/cases/asm.zig+3-2
...@@ -1,7 +1,8 @@...@@ -1,7 +1,8 @@
1const config = @import("builtin");
1const assert = @import("std").debug.assert;2const assert = @import("std").debug.assert;
23
3comptime {4comptime {
4 if (@compileVar("arch") == Arch.x86_64) {5 if (config.arch == config.Arch.x86_64) {
5 asm volatile (6 asm volatile (
6 \\.globl aoeu;7 \\.globl aoeu;
7 \\.type aoeu, @function;8 \\.type aoeu, @function;
...@@ -11,7 +12,7 @@ comptime {...@@ -11,7 +12,7 @@ comptime {
11}12}
1213
13test "module level assembly" {14test "module level assembly" {
14 if (@compileVar("arch") == Arch.x86_64) {15 if (config.arch == config.Arch.x86_64) {
15 assert(aoeu() == 1234);16 assert(aoeu() == 1234);
16 }17 }
17}18}
test/cases/atomics.zig+1
...@@ -1,4 +1,5 @@...@@ -1,4 +1,5 @@
1const assert = @import("std").debug.assert;1const assert = @import("std").debug.assert;
2const AtomicOrder = @import("builtin").AtomicOrder;
23
3test "cmpxchg" {4test "cmpxchg" {
4 var x: i32 = 1234;5 var x: i32 = 1234;
test/cases/misc.zig+2-1
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1const assert = @import("std").debug.assert;1const assert = @import("std").debug.assert;
2const mem = @import("std").mem;2const mem = @import("std").mem;
3const cstr = @import("std").cstr;3const cstr = @import("std").cstr;
4const builtin = @import("builtin");
45
5// normal comment6// normal comment
6/// this is a documentation comment7/// this is a documentation comment
...@@ -12,7 +13,7 @@ test "emptyFunctionWithComments" {...@@ -12,7 +13,7 @@ test "emptyFunctionWithComments" {
12}13}
1314
14export fn disabledExternFn() {15export fn disabledExternFn() {
15 @setGlobalLinkage(disabledExternFn, GlobalLinkage.Internal);16 @setGlobalLinkage(disabledExternFn, builtin.GlobalLinkage.Internal);
16}17}
1718
18test "callDisabledExternFn" {19test "callDisabledExternFn" {
test/cases/namespace_depends_on_compile_var/index.zig+3-2
...@@ -1,3 +1,4 @@...@@ -1,3 +1,4 @@
1const builtin = @import("builtin");
1const assert = @import("std").debug.assert;2const assert = @import("std").debug.assert;
23
3test "namespaceDependsOnCompileVar" {4test "namespaceDependsOnCompileVar" {
...@@ -7,7 +8,7 @@ test "namespaceDependsOnCompileVar" {...@@ -7,7 +8,7 @@ test "namespaceDependsOnCompileVar" {
7 assert(!some_namespace.a_bool);8 assert(!some_namespace.a_bool);
8 }9 }
9}10}
10const some_namespace = switch(@compileVar("os")) {11const some_namespace = switch(builtin.os) {
11 Os.linux => @import("a.zig"),12 builtin.Os.linux => @import("a.zig"),
12 else => @import("b.zig"),13 else => @import("b.zig"),
13};14};
test/compile_errors.zig+9-6
...@@ -688,9 +688,9 @@ pub fn addCases(cases: &tests.CompileErrorContext) {...@@ -688,9 +688,9 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
688688
689689
690 cases.add("bogus compile var",690 cases.add("bogus compile var",
691 \\const x = @compileVar("bogus");691 \\const x = @import("builtin").bogus;
692 \\export fn entry() -> usize { @sizeOf(@typeOf(x)) }692 \\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
696 cases.add("non constant expression in array size outside function",696 cases.add("non constant expression in array size outside function",
...@@ -910,18 +910,20 @@ pub fn addCases(cases: &tests.CompileErrorContext) {...@@ -910,18 +910,20 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
910 , ".tmp_source.zig:2:15: error: unable to infer expression type");910 , ".tmp_source.zig:2:15: error: unable to infer expression type");
911911
912 cases.add("atomic orderings of cmpxchg - failure stricter than success",912 cases.add("atomic orderings of cmpxchg - failure stricter than success",
913 \\const AtomicOrder = @import("builtin").AtomicOrder;
913 \\export fn f() {914 \\export fn f() {
914 \\ var x: i32 = 1234;915 \\ var x: i32 = 1234;
915 \\ while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.Monotonic, AtomicOrder.SeqCst)) {}916 \\ while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.Monotonic, AtomicOrder.SeqCst)) {}
916 \\}917 \\}
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
919 cases.add("atomic orderings of cmpxchg - success Monotonic or stricter",920 cases.add("atomic orderings of cmpxchg - success Monotonic or stricter",
921 \\const AtomicOrder = @import("builtin").AtomicOrder;
920 \\export fn f() {922 \\export fn f() {
921 \\ var x: i32 = 1234;923 \\ var x: i32 = 1234;
922 \\ while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.Unordered, AtomicOrder.Unordered)) {}924 \\ while (!@cmpxchg(&x, 1234, 5678, AtomicOrder.Unordered, AtomicOrder.Unordered)) {}
923 \\}925 \\}
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
926 cases.add("negation overflow in function evaluation",928 cases.add("negation overflow in function evaluation",
927 \\const y = neg(-128);929 \\const y = neg(-128);
...@@ -1487,10 +1489,11 @@ pub fn addCases(cases: &tests.CompileErrorContext) {...@@ -1487,10 +1489,11 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
1487 , ".tmp_source.zig:6:5: error: unable to evaluate constant expression");1489 , ".tmp_source.zig:6:5: error: unable to evaluate constant expression");
14881490
1489 cases.add("invalid member of builtin enum",1491 cases.add("invalid member of builtin enum",
1492 \\const builtin = @import("builtin");
1490 \\export fn entry() {1493 \\export fn entry() {
1491 \\ const foo = Arch.x86;1494 \\ const foo = builtin.Arch.x86;
1492 \\}1495 \\}
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
1495 cases.add("int to ptr of 0 bits",1498 cases.add("int to ptr of 0 bits",
1496 \\export fn foo() {1499 \\export fn foo() {