authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-09-30 18:58:41-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-09-30 20:21:57-04:00
logc6295fe9abf5508d522ab4000b577a6f30a98a88
treede5ff247f235fbf8f9cfe6f8ad2267c897c4d022
parent6db589fff5be05e3e782a34443a1133ebc6ef17d

remove zigrt

adds test case for #394 partially reverts a32b5929ccf8cbf79396d8924097a1a911985dac

13 files changed, 109 insertions(+), 122 deletions(-)

CMakeLists.txt+1-1
...@@ -585,8 +585,8 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/udivmod.zig" DESTINAT...@@ -585,8 +585,8 @@ install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/udivmod.zig" DESTINAT
585install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/udivmoddi4.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")585install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/udivmoddi4.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")
586install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/udivmodti4.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")586install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/udivmodti4.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")
587install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/umodti3.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")587install(FILES "${CMAKE_SOURCE_DIR}/std/special/compiler_rt/umodti3.zig" DESTINATION "${ZIG_STD_DEST}/special/compiler_rt")
588install(FILES "${CMAKE_SOURCE_DIR}/std/special/panic.zig" DESTINATION "${ZIG_STD_DEST}/special")
588install(FILES "${CMAKE_SOURCE_DIR}/std/special/test_runner.zig" DESTINATION "${ZIG_STD_DEST}/special")589install(FILES "${CMAKE_SOURCE_DIR}/std/special/test_runner.zig" DESTINATION "${ZIG_STD_DEST}/special")
589install(FILES "${CMAKE_SOURCE_DIR}/std/special/zigrt.zig" DESTINATION "${ZIG_STD_DEST}/special")
590590
591if (ZIG_TEST_COVERAGE)591if (ZIG_TEST_COVERAGE)
592 add_custom_target(coverage592 add_custom_target(coverage
src/all_types.hpp+2-4
...@@ -1472,7 +1472,7 @@ struct CodeGen {...@@ -1472,7 +1472,7 @@ struct CodeGen {
1472 bool is_native_target;1472 bool is_native_target;
1473 PackageTableEntry *root_package;1473 PackageTableEntry *root_package;
1474 PackageTableEntry *std_package;1474 PackageTableEntry *std_package;
1475 PackageTableEntry *zigrt_package;1475 PackageTableEntry *panic_package;
1476 PackageTableEntry *test_runner_package;1476 PackageTableEntry *test_runner_package;
1477 PackageTableEntry *compile_var_package;1477 PackageTableEntry *compile_var_package;
1478 ImportTableEntry *compile_var_import;1478 ImportTableEntry *compile_var_import;
...@@ -1484,7 +1484,6 @@ struct CodeGen {...@@ -1484,7 +1484,6 @@ struct CodeGen {
1484 Buf *mios_version_min;1484 Buf *mios_version_min;
1485 bool linker_rdynamic;1485 bool linker_rdynamic;
1486 const char *linker_script;1486 const char *linker_script;
1487 bool omit_zigrt;
14881487
1489 // The function definitions this module includes. There must be a corresponding1488 // The function definitions this module includes. There must be a corresponding
1490 // fn_protos entry.1489 // fn_protos entry.
...@@ -1498,8 +1497,7 @@ struct CodeGen {...@@ -1498,8 +1497,7 @@ struct CodeGen {
1498 OutType out_type;1497 OutType out_type;
1499 FnTableEntry *cur_fn;1498 FnTableEntry *cur_fn;
1500 FnTableEntry *main_fn;1499 FnTableEntry *main_fn;
1501 FnTableEntry *user_panic_fn;1500 FnTableEntry *panic_fn;
1502 FnTableEntry *extern_panic_fn;
1503 LLVMValueRef cur_ret_ptr;1501 LLVMValueRef cur_ret_ptr;
1504 LLVMValueRef cur_fn_val;1502 LLVMValueRef cur_fn_val;
1505 bool c_want_stdint;1503 bool c_want_stdint;
src/analyze.cpp+7-48
...@@ -2072,11 +2072,10 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {...@@ -2072,11 +2072,10 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
2072 if (fn_def_node)2072 if (fn_def_node)
2073 g->fn_defs.append(fn_table_entry);2073 g->fn_defs.append(fn_table_entry);
20742074
2075 if (g->have_pub_main && scope_is_root_decls(tld_fn->base.parent_scope) &&2075 if (scope_is_root_decls(tld_fn->base.parent_scope) &&
2076 ((!g->is_test_build && import == g->root_import) ||2076 (import == g->root_import || import->package == g->panic_package))
2077 (g->is_test_build && import == g->test_runner_import)))
2078 {2077 {
2079 if (buf_eql_str(&fn_table_entry->symbol_name, "main")) {2078 if (g->have_pub_main && buf_eql_str(&fn_table_entry->symbol_name, "main")) {
2080 g->main_fn = fn_table_entry;2079 g->main_fn = fn_table_entry;
20812080
2082 if (tld_fn->base.visib_mod != VisibModExport) {2081 if (tld_fn->base.visib_mod != VisibModExport) {
...@@ -2088,13 +2087,12 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {...@@ -2088,13 +2087,12 @@ static void resolve_decl_fn(CodeGen *g, TldFn *tld_fn) {
2088 buf_ptr(&actual_return_type->name)));2087 buf_ptr(&actual_return_type->name)));
2089 }2088 }
2090 }2089 }
2091 } else if (buf_eql_str(&fn_table_entry->symbol_name, "panic")) {2090 } else if ((import->package == g->panic_package || g->have_pub_panic) &&
2091 buf_eql_str(&fn_table_entry->symbol_name, "panic"))
2092 {
2093 g->panic_fn = fn_table_entry;
2092 typecheck_panic_fn(g, fn_table_entry);2094 typecheck_panic_fn(g, fn_table_entry);
2093 }2095 }
2094 } else if (import->package == g->zigrt_package && scope_is_root_decls(tld_fn->base.parent_scope)) {
2095 if (buf_eql_str(&fn_table_entry->symbol_name, "__zig_panic")) {
2096 g->extern_panic_fn = fn_table_entry;
2097 }
2098 }2096 }
2099 }2097 }
2100 } else if (source_node->type == NodeTypeTestDecl) {2098 } else if (source_node->type == NodeTypeTestDecl) {
...@@ -2272,14 +2270,6 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {...@@ -2272,14 +2270,6 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
2272 tld_fn->extern_lib_name = node->data.fn_proto.lib_name;2270 tld_fn->extern_lib_name = node->data.fn_proto.lib_name;
2273 add_top_level_decl(g, decls_scope, &tld_fn->base);2271 add_top_level_decl(g, decls_scope, &tld_fn->base);
22742272
2275 ImportTableEntry *import = get_scope_import(&decls_scope->base);
2276 if (import == g->root_import && scope_is_root_decls(&decls_scope->base) &&
2277 buf_eql_str(fn_name, "panic"))
2278 {
2279 update_compile_var(g, buf_create_from_str("__zig_panic_implementation_provided"),
2280 create_const_bool(g, true));
2281 }
2282
2283 break;2273 break;
2284 }2274 }
2285 case NodeTypeUse:2275 case NodeTypeUse:
...@@ -4606,37 +4596,6 @@ ConstParent *get_const_val_parent(CodeGen *g, ConstExprValue *value) {...@@ -4606,37 +4596,6 @@ ConstParent *get_const_val_parent(CodeGen *g, ConstExprValue *value) {
4606 return nullptr;4596 return nullptr;
4607}4597}
46084598
4609FnTableEntry *get_extern_panic_fn(CodeGen *g) {
4610 if (g->extern_panic_fn)
4611 return g->extern_panic_fn;
4612
4613 FnTypeId fn_type_id = {0};
4614 fn_type_id.cc = CallingConventionCold;
4615 fn_type_id.param_count = 2;
4616 fn_type_id.param_info = allocate<FnTypeParamInfo>(2);
4617 fn_type_id.next_param_index = 0;
4618 fn_type_id.param_info[0].type = get_pointer_to_type(g, g->builtin_types.entry_u8, true);
4619 fn_type_id.param_info[1].type = g->builtin_types.entry_usize;
4620 fn_type_id.return_type = g->builtin_types.entry_unreachable;
4621
4622 TypeTableEntry *fn_type = get_fn_type(g, &fn_type_id);
4623 assert(!type_is_invalid(fn_type));
4624
4625 FnTableEntry *fn_entry = create_fn_raw(FnInlineAuto, GlobalLinkageIdStrong);
4626 buf_init_from_str(&fn_entry->symbol_name, "__zig_panic");
4627
4628 TldFn *tld_fn = allocate<TldFn>(1);
4629 init_tld(&tld_fn->base, TldIdFn, &fn_entry->symbol_name, VisibModPrivate, nullptr, nullptr);
4630 tld_fn->fn_entry = fn_entry;
4631
4632 g->external_prototypes.put_unique(tld_fn->base.name, &tld_fn->base);
4633
4634 fn_entry->type_entry = fn_type;
4635
4636 g->extern_panic_fn = fn_entry;
4637 return g->extern_panic_fn;
4638}
4639
4640static const TypeTableEntryId all_type_ids[] = {4599static const TypeTableEntryId all_type_ids[] = {
4641 TypeTableEntryIdMetaType,4600 TypeTableEntryIdMetaType,
4642 TypeTableEntryIdVoid,4601 TypeTableEntryIdVoid,
src/analyze.hpp-1
...@@ -157,7 +157,6 @@ ConstExprValue *create_const_vals(size_t count);...@@ -157,7 +157,6 @@ ConstExprValue *create_const_vals(size_t count);
157157
158TypeTableEntry *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits);158TypeTableEntry *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits);
159ConstParent *get_const_val_parent(CodeGen *g, ConstExprValue *value);159ConstParent *get_const_val_parent(CodeGen *g, ConstExprValue *value);
160FnTableEntry *get_extern_panic_fn(CodeGen *g);
161TypeTableEntry *create_enum_tag_type(CodeGen *g, TypeTableEntry *enum_type, TypeTableEntry *int_type);160TypeTableEntry *create_enum_tag_type(CodeGen *g, TypeTableEntry *enum_type, TypeTableEntry *int_type);
162void expand_undef_array(CodeGen *g, ConstExprValue *const_val);161void expand_undef_array(CodeGen *g, ConstExprValue *const_val);
163void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value);162void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value);
src/codegen.cpp+49-45
...@@ -157,10 +157,6 @@ void codegen_set_llvm_argv(CodeGen *g, const char **args, size_t len) {...@@ -157,10 +157,6 @@ void codegen_set_llvm_argv(CodeGen *g, const char **args, size_t len) {
157 g->llvm_argv_len = len;157 g->llvm_argv_len = len;
158}158}
159159
160void codegen_set_omit_zigrt(CodeGen *g, bool omit_zigrt) {
161 g->omit_zigrt = omit_zigrt;
162}
163
164void codegen_set_test_filter(CodeGen *g, Buf *filter) {160void codegen_set_test_filter(CodeGen *g, Buf *filter) {
165 g->test_filter = filter;161 g->test_filter = filter;
166}162}
...@@ -751,26 +747,12 @@ static LLVMValueRef get_panic_msg_ptr_val(CodeGen *g, PanicMsgId msg_id) {...@@ -751,26 +747,12 @@ static LLVMValueRef get_panic_msg_ptr_val(CodeGen *g, PanicMsgId msg_id) {
751 return val->global_refs->llvm_global;747 return val->global_refs->llvm_global;
752}748}
753749
754static void gen_panic_raw(CodeGen *g, LLVMValueRef msg_ptr, LLVMValueRef msg_len) {
755 FnTableEntry *panic_fn = get_extern_panic_fn(g);
756 LLVMValueRef fn_val = fn_llvm_value(g, panic_fn);
757 LLVMValueRef args[] = { msg_ptr, msg_len };
758 LLVMCallConv llvm_cc = get_llvm_cc(g, panic_fn->type_entry->data.fn.fn_type_id.cc);
759 ZigLLVMBuildCall(g->builder, fn_val, args, 2, llvm_cc, false, "");
760 LLVMBuildUnreachable(g->builder);
761}
762
763static void gen_panic(CodeGen *g, LLVMValueRef msg_arg) {750static void gen_panic(CodeGen *g, LLVMValueRef msg_arg) {
764 TypeTableEntry *ptr_type = get_pointer_to_type(g, g->builtin_types.entry_u8, true);751 assert(g->panic_fn != nullptr);
765 TypeTableEntry *str_type = get_slice_type(g, ptr_type);752 LLVMValueRef fn_val = fn_llvm_value(g, g->panic_fn);
766 size_t ptr_index = str_type->data.structure.fields[slice_ptr_index].gen_index;753 LLVMCallConv llvm_cc = get_llvm_cc(g, g->panic_fn->type_entry->data.fn.fn_type_id.cc);
767 size_t len_index = str_type->data.structure.fields[slice_len_index].gen_index;754 ZigLLVMBuildCall(g->builder, fn_val, &msg_arg, 1, llvm_cc, false, "");
768 LLVMValueRef ptr_ptr = LLVMBuildStructGEP(g->builder, msg_arg, (unsigned)ptr_index, "");755 LLVMBuildUnreachable(g->builder);
769 LLVMValueRef len_ptr = LLVMBuildStructGEP(g->builder, msg_arg, (unsigned)len_index, "");
770
771 LLVMValueRef msg_ptr = gen_load_untyped(g, ptr_ptr, 0, false, "");
772 LLVMValueRef msg_len = gen_load_untyped(g, len_ptr, 0, false, "");
773 gen_panic_raw(g, msg_ptr, msg_len);
774}756}
775757
776static void gen_debug_safety_crash(CodeGen *g, PanicMsgId msg_id) {758static void gen_debug_safety_crash(CodeGen *g, PanicMsgId msg_id) {
...@@ -818,26 +800,40 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {...@@ -818,26 +800,40 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
818 }800 }
819 uint32_t u8_align_bytes = get_abi_alignment(g, g->builtin_types.entry_u8);801 uint32_t u8_align_bytes = get_abi_alignment(g, g->builtin_types.entry_u8);
820 LLVMValueRef init_value = LLVMConstArray(LLVMInt8Type(), err_buf_vals, err_buf_len);802 LLVMValueRef init_value = LLVMConstArray(LLVMInt8Type(), err_buf_vals, err_buf_len);
821 Buf *global_name = get_mangled_name(g, buf_create_from_str("__zig_panic_buf"), false);803 LLVMValueRef global_array = LLVMAddGlobal(g->module, LLVMTypeOf(init_value), "");
822 LLVMValueRef global_value = LLVMAddGlobal(g->module, LLVMTypeOf(init_value), buf_ptr(global_name));804 LLVMSetInitializer(global_array, init_value);
823 LLVMSetInitializer(global_value, init_value);805 LLVMSetLinkage(global_array, LLVMInternalLinkage);
824 LLVMSetLinkage(global_value, LLVMInternalLinkage);806 LLVMSetGlobalConstant(global_array, false);
825 LLVMSetGlobalConstant(global_value, false);807 LLVMSetUnnamedAddr(global_array, true);
826 LLVMSetUnnamedAddr(global_value, true);808 LLVMSetAlignment(global_array, u8_align_bytes);
827 LLVMSetAlignment(global_value, u8_align_bytes);
828809
829 TypeTableEntry *usize = g->builtin_types.entry_usize;810 TypeTableEntry *usize = g->builtin_types.entry_usize;
830 LLVMValueRef full_buf_ptr_indices[] = {811 LLVMValueRef full_buf_ptr_indices[] = {
831 LLVMConstNull(usize->type_ref),812 LLVMConstNull(usize->type_ref),
832 LLVMConstNull(usize->type_ref),813 LLVMConstNull(usize->type_ref),
833 };814 };
834 LLVMValueRef full_buf_ptr = LLVMConstInBoundsGEP(global_value, full_buf_ptr_indices, 2);815 LLVMValueRef full_buf_ptr = LLVMConstInBoundsGEP(global_array, full_buf_ptr_indices, 2);
816
817
818 TypeTableEntry *u8_ptr_type = get_pointer_to_type(g, g->builtin_types.entry_u8, true);
819 TypeTableEntry *str_type = get_slice_type(g, u8_ptr_type);
820 LLVMValueRef global_slice_fields[] = {
821 full_buf_ptr,
822 LLVMConstNull(usize->type_ref),
823 };
824 LLVMValueRef slice_init_value = LLVMConstNamedStruct(str_type->type_ref, global_slice_fields, 2);
825 LLVMValueRef global_slice = LLVMAddGlobal(g->module, LLVMTypeOf(slice_init_value), "");
826 LLVMSetInitializer(global_slice, slice_init_value);
827 LLVMSetLinkage(global_slice, LLVMInternalLinkage);
828 LLVMSetGlobalConstant(global_slice, false);
829 LLVMSetUnnamedAddr(global_slice, true);
830 LLVMSetAlignment(global_slice, get_abi_alignment(g, str_type));
835831
836 LLVMValueRef offset_ptr_indices[] = {832 LLVMValueRef offset_ptr_indices[] = {
837 LLVMConstNull(usize->type_ref),833 LLVMConstNull(usize->type_ref),
838 LLVMConstInt(usize->type_ref, unwrap_err_msg_text_len, false),834 LLVMConstInt(usize->type_ref, unwrap_err_msg_text_len, false),
839 };835 };
840 LLVMValueRef offset_buf_ptr = LLVMConstInBoundsGEP(global_value, offset_ptr_indices, 2);836 LLVMValueRef offset_buf_ptr = LLVMConstInBoundsGEP(global_array, offset_ptr_indices, 2);
841837
842 Buf *fn_name = get_mangled_name(g, buf_create_from_str("__zig_fail_unwrap"), false);838 Buf *fn_name = get_mangled_name(g, buf_create_from_str("__zig_fail_unwrap"), false);
843 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), &g->err_tag_type->type_ref, 1, false);839 LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), &g->err_tag_type->type_ref, 1, false);
...@@ -888,7 +884,10 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {...@@ -888,7 +884,10 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
888 LLVMValueRef const_prefix_len = LLVMConstInt(LLVMTypeOf(err_name_len), strlen(unwrap_err_msg_text), false);884 LLVMValueRef const_prefix_len = LLVMConstInt(LLVMTypeOf(err_name_len), strlen(unwrap_err_msg_text), false);
889 LLVMValueRef full_buf_len = LLVMBuildNUWAdd(g->builder, const_prefix_len, err_name_len, "");885 LLVMValueRef full_buf_len = LLVMBuildNUWAdd(g->builder, const_prefix_len, err_name_len, "");
890886
891 gen_panic_raw(g, full_buf_ptr, full_buf_len);887 LLVMValueRef global_slice_len_field_ptr = LLVMBuildStructGEP(g->builder, global_slice, slice_len_index, "");
888 gen_store(g, full_buf_len, global_slice_len_field_ptr, u8_ptr_type);
889
890 gen_panic(g, global_slice);
892891
893 LLVMPositionBuilderAtEnd(g->builder, prev_block);892 LLVMPositionBuilderAtEnd(g->builder, prev_block);
894 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);893 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
...@@ -3744,8 +3743,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {...@@ -3744,8 +3743,7 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) {
3744 fields[type_struct_field->gen_index] = gen_const_val(g, &const_val->data.x_struct.fields[i]);3743 fields[type_struct_field->gen_index] = gen_const_val(g, &const_val->data.x_struct.fields[i]);
3745 }3744 }
3746 }3745 }
3747 return LLVMConstStruct(fields, type_entry->data.structure.gen_field_count,3746 return LLVMConstNamedStruct(type_entry->type_ref, fields, type_entry->data.structure.gen_field_count);
3748 type_entry->data.structure.layout == ContainerLayoutPacked);
3749 }3747 }
3750 case TypeTableEntryIdUnion:3748 case TypeTableEntryIdUnion:
3751 {3749 {
...@@ -4103,7 +4101,7 @@ static void validate_inline_fns(CodeGen *g) {...@@ -4103,7 +4101,7 @@ static void validate_inline_fns(CodeGen *g) {
4103}4101}
41044102
4105static void do_code_gen(CodeGen *g) {4103static void do_code_gen(CodeGen *g) {
4106 if (g->verbose || g->verbose_ir) {4104 if (g->verbose) {
4107 fprintf(stderr, "\nCode Generation:\n");4105 fprintf(stderr, "\nCode Generation:\n");
4108 fprintf(stderr, "------------------\n");4106 fprintf(stderr, "------------------\n");
4109 }4107 }
...@@ -4918,8 +4916,6 @@ static void define_builtin_compile_vars(CodeGen *g) {...@@ -4918,8 +4916,6 @@ static void define_builtin_compile_vars(CodeGen *g) {
4918 buf_appendf(contents, "pub const mode = %s;\n", build_mode_to_str(g->build_mode));4916 buf_appendf(contents, "pub const mode = %s;\n", build_mode_to_str(g->build_mode));
4919 buf_appendf(contents, "pub const link_libc = %s;\n", bool_to_str(g->libc_link_lib != nullptr));4917 buf_appendf(contents, "pub const link_libc = %s;\n", bool_to_str(g->libc_link_lib != nullptr));
49204918
4921 buf_appendf(contents, "pub const __zig_panic_implementation_provided = %s; // overwritten later\n",
4922 bool_to_str(false));
4923 buf_appendf(contents, "pub const __zig_test_fn_slice = {}; // overwritten later\n");4919 buf_appendf(contents, "pub const __zig_test_fn_slice = {}; // overwritten later\n");
49244920
4925 ensure_cache_dir(g);4921 ensure_cache_dir(g);
...@@ -5105,10 +5101,8 @@ static PackageTableEntry *create_test_runner_pkg(CodeGen *g) {...@@ -5105,10 +5101,8 @@ static PackageTableEntry *create_test_runner_pkg(CodeGen *g) {
5105 return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "test_runner.zig");5101 return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "test_runner.zig");
5106}5102}
51075103
5108static PackageTableEntry *create_zigrt_pkg(CodeGen *g) {5104static PackageTableEntry *create_panic_pkg(CodeGen *g) {
5109 PackageTableEntry *package = codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "zigrt.zig");5105 return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "panic.zig");
5110 package->package_table.put(buf_create_from_str("@root"), g->root_package);
5111 return package;
5112}5106}
51135107
5114static void create_test_compile_var_and_add_test_runner(CodeGen *g) {5108static void create_test_compile_var_and_add_test_runner(CodeGen *g) {
...@@ -5190,9 +5184,19 @@ static void gen_root_source(CodeGen *g) {...@@ -5190,9 +5184,19 @@ static void gen_root_source(CodeGen *g) {
5190 {5184 {
5191 g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g, g->root_package), "bootstrap.zig");5185 g->bootstrap_import = add_special_code(g, create_bootstrap_pkg(g, g->root_package), "bootstrap.zig");
5192 }5186 }
5193 if (!g->omit_zigrt) {5187 ImportTableEntry *import_with_panic;
5194 g->zigrt_package = create_zigrt_pkg(g);5188 if (g->have_pub_panic) {
5195 add_special_code(g, g->zigrt_package, "zigrt.zig");5189 import_with_panic = g->root_import;
5190 } else {
5191 g->panic_package = create_panic_pkg(g);
5192 import_with_panic = add_special_code(g, g->panic_package, "panic.zig");
5193 }
5194 // Zig has lazy top level definitions. Here we semantically analyze the panic function.
5195 {
5196 scan_import(g, import_with_panic);
5197 Tld *panic_tld = find_decl(g, &import_with_panic->decls_scope->base, buf_create_from_str("panic"));
5198 assert(panic_tld != nullptr);
5199 resolve_top_level_decl(g, panic_tld, false, nullptr);
5196 }5200 }
51975201
5198 if (g->verbose) {5202 if (g->verbose) {
src/codegen.hpp-1
...@@ -41,7 +41,6 @@ void codegen_set_rdynamic(CodeGen *g, bool rdynamic);...@@ -41,7 +41,6 @@ void codegen_set_rdynamic(CodeGen *g, bool rdynamic);
41void codegen_set_mmacosx_version_min(CodeGen *g, Buf *mmacosx_version_min);41void codegen_set_mmacosx_version_min(CodeGen *g, Buf *mmacosx_version_min);
42void codegen_set_mios_version_min(CodeGen *g, Buf *mios_version_min);42void codegen_set_mios_version_min(CodeGen *g, Buf *mios_version_min);
43void codegen_set_linker_script(CodeGen *g, const char *linker_script);43void codegen_set_linker_script(CodeGen *g, const char *linker_script);
44void codegen_set_omit_zigrt(CodeGen *g, bool omit_zigrt);
45void codegen_set_test_filter(CodeGen *g, Buf *filter);44void codegen_set_test_filter(CodeGen *g, Buf *filter);
46void codegen_set_test_name_prefix(CodeGen *g, Buf *prefix);45void codegen_set_test_name_prefix(CodeGen *g, Buf *prefix);
47void codegen_set_lib_version(CodeGen *g, size_t major, size_t minor, size_t patch);46void codegen_set_lib_version(CodeGen *g, size_t major, size_t minor, size_t patch);
src/link.cpp-1
...@@ -35,7 +35,6 @@ static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path)...@@ -35,7 +35,6 @@ static Buf *build_o_raw(CodeGen *parent_gen, const char *oname, Buf *full_path)
35 ZigTarget *child_target = parent_gen->is_native_target ? nullptr : &parent_gen->zig_target;35 ZigTarget *child_target = parent_gen->is_native_target ? nullptr : &parent_gen->zig_target;
36 CodeGen *child_gen = codegen_create(full_path, child_target, OutTypeObj, parent_gen->build_mode);36 CodeGen *child_gen = codegen_create(full_path, child_target, OutTypeObj, parent_gen->build_mode);
3737
38 codegen_set_omit_zigrt(child_gen, true);
39 child_gen->want_h_file = false;38 child_gen->want_h_file = false;
40 child_gen->verbose_link = parent_gen->verbose_link;39 child_gen->verbose_link = parent_gen->verbose_link;
4140
std/special/builtin.zig+16-3
...@@ -1,11 +1,21 @@...@@ -1,11 +1,21 @@
1// These functions are provided when not linking against libc because LLVM1// These functions are provided when not linking against libc because LLVM
2// sometimes generates code that calls them.2// sometimes generates code that calls them.
33
4// Note that these functions do not return `dest`, like the libc API.
5// The semantics of these functions is dictated by the corresponding
6// LLVM intrinsics, not by the libc API.
7const builtin = @import("builtin");4const builtin = @import("builtin");
85
6// Avoid dragging in the debug safety mechanisms into this .o file,
7// unless we're trying to test this file.
8pub coldcc fn panic(msg: []const u8) -> noreturn {
9 if (builtin.is_test) {
10 @import("std").debug.panic("{}", msg);
11 } else {
12 unreachable;
13 }
14}
15
16// Note that memset does not return `dest`, like the libc API.
17// The semantics of memset is dictated by the corresponding
18// LLVM intrinsics, not by the libc API.
9export fn memset(dest: ?&u8, c: u8, n: usize) {19export fn memset(dest: ?&u8, c: u8, n: usize) {
10 @setDebugSafety(this, false);20 @setDebugSafety(this, false);
1121
...@@ -14,6 +24,9 @@ export fn memset(dest: ?&u8, c: u8, n: usize) {...@@ -14,6 +24,9 @@ export fn memset(dest: ?&u8, c: u8, n: usize) {
14 (??dest)[index] = c;24 (??dest)[index] = c;
15}25}
1626
27// Note that memcpy does not return `dest`, like the libc API.
28// The semantics of memcpy is dictated by the corresponding
29// LLVM intrinsics, not by the libc API.
17export fn memcpy(noalias dest: ?&u8, noalias src: ?&const u8, n: usize) {30export fn memcpy(noalias dest: ?&u8, noalias src: ?&const u8, n: usize) {
18 @setDebugSafety(this, false);31 @setDebugSafety(this, false);
1932
std/special/compiler_rt/index.zig+10
...@@ -23,6 +23,16 @@ const assert = @import("../../debug.zig").assert;...@@ -23,6 +23,16 @@ const assert = @import("../../debug.zig").assert;
2323
24const __udivmoddi4 = @import("udivmoddi4.zig").__udivmoddi4;24const __udivmoddi4 = @import("udivmoddi4.zig").__udivmoddi4;
2525
26// Avoid dragging in the debug safety mechanisms into this .o file,
27// unless we're trying to test this file.
28pub coldcc fn panic(msg: []const u8) -> noreturn {
29 if (is_test) {
30 @import("std").debug.panic("{}", msg);
31 } else {
32 unreachable;
33 }
34}
35
26export fn __udivdi3(a: u64, b: u64) -> u64 {36export fn __udivdi3(a: u64, b: u64) -> u64 {
27 @setDebugSafety(this, is_test);37 @setDebugSafety(this, is_test);
28 @setGlobalLinkage(__udivdi3, builtin.GlobalLinkage.LinkOnce);38 @setGlobalLinkage(__udivdi3, builtin.GlobalLinkage.LinkOnce);
std/special/panic.zig created+14
...@@ -0,0 +1,14 @@
1// This file is the default panic handler if the root source file does not
2// have a `pub fn panic`.
3// If this file wants to import other files *by name*, support for that would
4// have to be added in the compiler.
5
6const builtin = @import("builtin");
7
8pub coldcc fn panic(msg: []const u8) -> noreturn {
9 if (builtin.os == builtin.Os.freestanding) {
10 while (true) {}
11 } else {
12 @import("std").debug.panic("{}", msg);
13 }
14}
std/special/zigrt.zig deleted-18
...@@ -1,18 +0,0 @@
1// This file contains functions that zig depends on to coordinate between
2// multiple .o files. The symbols are defined LinkOnce so that multiple
3// instances of zig_rt.zig do not conflict with each other.
4
5const builtin = @import("builtin");
6
7export coldcc fn __zig_panic(message_ptr: &const u8, message_len: usize) -> noreturn {
8 @setGlobalLinkage(__zig_panic, builtin.GlobalLinkage.LinkOnce);
9 @setDebugSafety(this, false);
10
11 if (builtin.__zig_panic_implementation_provided) {
12 @import("@root").panic(message_ptr[0..message_len]);
13 } else if (builtin.os == builtin.Os.freestanding) {
14 while (true) {}
15 } else {
16 @import("std").debug.panic("{}", message_ptr[0..message_len]);
17 }
18}
test/behavior.zig+1
...@@ -6,6 +6,7 @@ comptime {...@@ -6,6 +6,7 @@ comptime {
6 _ = @import("cases/atomics.zig");6 _ = @import("cases/atomics.zig");
7 _ = @import("cases/bitcast.zig");7 _ = @import("cases/bitcast.zig");
8 _ = @import("cases/bool.zig");8 _ = @import("cases/bool.zig");
9 _ = @import("cases/bugs/394.zig");
9 _ = @import("cases/cast.zig");10 _ = @import("cases/cast.zig");
10 _ = @import("cases/const_slice_child.zig");11 _ = @import("cases/const_slice_child.zig");
11 _ = @import("cases/defer.zig");12 _ = @import("cases/defer.zig");
test/cases/bugs/394.zig created+9
...@@ -0,0 +1,9 @@
1const E = enum { A: [9]u8, B: u64, };
2const S = struct { x: u8, y: E, };
3
4const assert = @import("std").debug.assert;
5
6test "bug 394 fixed" {
7 const x = S { .x = 3, .y = E.B {1} };
8 assert(x.x == 3);
9}