authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-19 14:32:09-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-03-19 14:32:09-04:00
log69e6d455ce8e21835ec3ce268a0533e0e7666e8b
tree8bdc050b60371970ddc8c1d1263a3688e79456aa
parentc6cf40a0c03822cac3112be58c61ca55d436b5d0
parent12f3c461a4429d9c7a0ddbaa6465bf0499a99b8c
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #11228 from Vexu/panic

enable default panic handler for stage2 LLVM

12 files changed, 183 insertions(+), 77 deletions(-)

lib/std/builtin.zig+1-1
......@@ -753,7 +753,7 @@ pub fn default_panic(msg: []const u8, error_return_trace: ?*StackTrace) noreturn
753753 @setCold(true);
754754 // Until self-hosted catches up with stage1 language features, we have a simpler
755755 // default panic function:
756 if (builtin.zig_backend != .stage1) {
756 if (builtin.zig_backend != .stage1 and builtin.zig_backend != .stage2_llvm) {
757757 while (true) {
758758 @breakpoint();
759759 }
lib/std/c/darwin.zig+10-6
......@@ -624,8 +624,7 @@ pub const pthread_attr_t = extern struct {
624624 __opaque: [56]u8,
625625};
626626
627const pthread_t = std.c.pthread_t;
628pub extern "c" fn pthread_threadid_np(thread: ?pthread_t, thread_id: *u64) c_int;
627pub extern "c" fn pthread_threadid_np(thread: ?std.c.pthread_t, thread_id: *u64) c_int;
629628pub extern "c" fn pthread_setname_np(name: [*:0]const u8) E;
630629pub extern "c" fn pthread_getname_np(thread: std.c.pthread_t, name: [*:0]u8, len: usize) E;
631630
......@@ -921,12 +920,17 @@ pub const siginfo_t = extern struct {
921920
922921/// Renamed from `sigaction` to `Sigaction` to avoid conflict with function name.
923922pub const Sigaction = extern struct {
924 pub const handler_fn = fn (c_int) callconv(.C) void;
925 pub const sigaction_fn = fn (c_int, *const siginfo_t, ?*const anyopaque) callconv(.C) void;
923 pub usingnamespace if (builtin.zig_backend == .stage1) struct {
924 pub const handler_fn = fn (c_int) callconv(.C) void;
925 pub const sigaction_fn = fn (c_int, *const siginfo_t, ?*const anyopaque) callconv(.C) void;
926 } else struct {
927 pub const handler_fn = *const fn (c_int) callconv(.C) void;
928 pub const sigaction_fn = *const fn (c_int, *const siginfo_t, ?*const anyopaque) callconv(.C) void;
929 };
926930
927931 handler: extern union {
928 handler: ?handler_fn,
929 sigaction: ?sigaction_fn,
932 handler: ?Sigaction.handler_fn,
933 sigaction: ?Sigaction.sigaction_fn,
930934 },
931935 mask: sigset_t,
932936 flags: c_uint,
lib/std/debug.zig-1
......@@ -1541,7 +1541,6 @@ pub const ModuleDebugInfo = switch (native_os) {
15411541 .symbol_name = o_file_di.getSymbolName(relocated_address_o) orelse "???",
15421542 .compile_unit_name = compile_unit.die.getAttrString(o_file_di, DW.AT.name) catch |err| switch (err) {
15431543 error.MissingDebugInfo, error.InvalidDebugInfo => "???",
1544 else => return err,
15451544 },
15461545 .line_info = o_file_di.getLineNumberInfo(compile_unit.*, relocated_address_o + addr_off) catch |err| switch (err) {
15471546 error.MissingDebugInfo, error.InvalidDebugInfo => null,
lib/std/heap/general_purpose_allocator.zig+6-18
......@@ -341,15 +341,9 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
341341 const slot_index = @intCast(SlotIndex, used_bits_byte * 8 + bit_index);
342342 const stack_trace = bucketStackTrace(bucket, size_class, slot_index, .alloc);
343343 const addr = bucket.page + slot_index * size_class;
344 if (builtin.zig_backend == .stage1) {
345 log.err("memory address 0x{x} leaked: {s}", .{
346 @ptrToInt(addr), stack_trace,
347 });
348 } else { // TODO
349 log.err("memory address 0x{x} leaked", .{
350 @ptrToInt(addr),
351 });
352 }
344 log.err("memory address 0x{x} leaked: {s}", .{
345 @ptrToInt(addr), stack_trace,
346 });
353347 leaks = true;
354348 }
355349 if (bit_index == math.maxInt(u3))
......@@ -379,15 +373,9 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
379373 while (it.next()) |large_alloc| {
380374 if (config.retain_metadata and large_alloc.freed) continue;
381375 const stack_trace = large_alloc.getStackTrace(.alloc);
382 if (builtin.zig_backend == .stage1) {
383 log.err("memory address 0x{x} leaked: {s}", .{
384 @ptrToInt(large_alloc.bytes.ptr), stack_trace,
385 });
386 } else { // TODO
387 log.err("memory address 0x{x} leaked", .{
388 @ptrToInt(large_alloc.bytes.ptr),
389 });
390 }
376 log.err("memory address 0x{x} leaked: {s}", .{
377 @ptrToInt(large_alloc.bytes.ptr), stack_trace,
378 });
391379 leaks = true;
392380 }
393381 return leaks;
lib/std/macho.zig+3-1
......@@ -624,7 +624,9 @@ pub const segment_command_64 = extern struct {
624624 cmd: LC = .SEGMENT_64,
625625
626626 /// includes sizeof section_64 structs
627 cmdsize: u32 = @sizeOf(segment_command_64),
627 cmdsize: u32,
628 // TODO lazy values in stage2
629 // cmdsize: u32 = @sizeOf(segment_command_64),
628630
629631 /// segment name
630632 segname: [16]u8,
lib/std/os/linux.zig+36-13
......@@ -1080,12 +1080,19 @@ pub fn sigaction(sig: u6, noalias act: ?*const Sigaction, noalias oact: ?*Sigact
10801080 const mask_size = @sizeOf(@TypeOf(ksa.mask));
10811081
10821082 if (act) |new| {
1083 const restorer_fn = if ((new.flags & SA.SIGINFO) != 0) restore_rt else restore;
1083 const restore_rt_ptr = if (builtin.zig_backend == .stage1) restore_rt else &syscall_bits.restore_rt;
1084 // TODO https://github.com/ziglang/zig/issues/11227
1085 const restore_ptr = if (builtin.zig_backend == .stage1) restore else switch (native_arch) {
1086 .arm, .thumb, .mips, .mipsel, .i386 => &syscall_bits.restore,
1087 .x86_64, .aarch64, .riscv64, .sparcv9, .powerpc, .powerpc64, .powerpc64le => &syscall_bits.restore_rt,
1088 else => unreachable,
1089 };
1090 const restorer_fn = if ((new.flags & SA.SIGINFO) != 0) restore_rt_ptr else restore_ptr;
10841091 ksa = k_sigaction{
10851092 .handler = new.handler.handler,
10861093 .flags = new.flags | SA.RESTORER,
10871094 .mask = undefined,
1088 .restorer = @ptrCast(fn () callconv(.C) void, restorer_fn),
1095 .restorer = @ptrCast(k_sigaction_funcs.restorer, restorer_fn),
10891096 };
10901097 @memcpy(@ptrCast([*]u8, &ksa.mask), @ptrCast([*]const u8, &new.mask), mask_size);
10911098 }
......@@ -3047,39 +3054,55 @@ pub const sigset_t = [1024 / 32]u32;
30473054pub const all_mask: sigset_t = [_]u32{0xffffffff} ** sigset_t.len;
30483055pub const app_mask: sigset_t = [2]u32{ 0xfffffffc, 0x7fffffff } ++ [_]u32{0xffffffff} ** 30;
30493056
3057const k_sigaction_funcs = if (builtin.zig_backend == .stage1) struct {
3058 const handler = ?fn (c_int) callconv(.C) void;
3059 const restorer = fn () callconv(.C) void;
3060} else struct {
3061 const handler = ?*const fn (c_int) callconv(.C) void;
3062 const restorer = *const fn () callconv(.C) void;
3063};
3064
30503065pub const k_sigaction = switch (native_arch) {
30513066 .mips, .mipsel => extern struct {
30523067 flags: c_uint,
3053 handler: ?fn (c_int) callconv(.C) void,
3068 handler: k_sigaction_funcs.handler,
30543069 mask: [4]c_ulong,
3055 restorer: fn () callconv(.C) void,
3070 restorer: k_sigaction_funcs.restorer,
30563071 },
30573072 .mips64, .mips64el => extern struct {
30583073 flags: c_uint,
3059 handler: ?fn (c_int) callconv(.C) void,
3074 handler: k_sigaction_funcs.handler,
30603075 mask: [2]c_ulong,
3061 restorer: fn () callconv(.C) void,
3076 restorer: k_sigaction_funcs.restorer,
30623077 },
30633078 else => extern struct {
3064 handler: ?fn (c_int) callconv(.C) void,
3079 handler: k_sigaction_funcs.handler,
30653080 flags: c_ulong,
3066 restorer: fn () callconv(.C) void,
3081 restorer: k_sigaction_funcs.restorer,
30673082 mask: [2]c_uint,
30683083 },
30693084};
30703085
30713086/// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.
30723087pub const Sigaction = extern struct {
3073 pub const handler_fn = fn (c_int) callconv(.C) void;
3074 pub const sigaction_fn = fn (c_int, *const siginfo_t, ?*const anyopaque) callconv(.C) void;
3088 pub usingnamespace if (builtin.zig_backend == .stage1) struct {
3089 pub const handler_fn = fn (c_int) callconv(.C) void;
3090 pub const sigaction_fn = fn (c_int, *const siginfo_t, ?*const anyopaque) callconv(.C) void;
3091 } else struct {
3092 pub const handler_fn = *const fn (c_int) callconv(.C) void;
3093 pub const sigaction_fn = *const fn (c_int, *const siginfo_t, ?*const anyopaque) callconv(.C) void;
3094 };
30753095
30763096 handler: extern union {
3077 handler: ?handler_fn,
3078 sigaction: ?sigaction_fn,
3097 handler: ?Sigaction.handler_fn,
3098 sigaction: ?Sigaction.sigaction_fn,
30793099 },
30803100 mask: sigset_t,
30813101 flags: c_uint,
3082 restorer: ?fn () callconv(.C) void = null,
3102 restorer: ?if (builtin.zig_backend == .stage1)
3103 fn () callconv(.C) void
3104 else
3105 *const fn () callconv(.C) void = null,
30833106};
30843107
30853108pub const empty_sigset = [_]u32{0} ** @typeInfo(sigset_t).Array.len;
src/Sema.zig+88-29
......@@ -131,6 +131,9 @@ pub const Block = struct {
131131
132132 c_import_buf: ?*std.ArrayList(u8) = null,
133133
134 /// type of `err` in `else => |err|`
135 switch_else_err_ty: ?Type = null,
136
134137 const Param = struct {
135138 /// `noreturn` means `anytype`.
136139 ty: Type,
......@@ -189,6 +192,7 @@ pub const Block = struct {
189192 .runtime_index = parent.runtime_index,
190193 .want_safety = parent.want_safety,
191194 .c_import_buf = parent.c_import_buf,
195 .switch_else_err_ty = parent.switch_else_err_ty,
192196 };
193197 }
194198
......@@ -3930,6 +3934,23 @@ fn analyzeBlockBody(
39303934 // to emit a jump instruction to after the block when it encounters the break.
39313935 try parent_block.instructions.append(gpa, merges.block_inst);
39323936 const resolved_ty = try sema.resolvePeerTypes(parent_block, src, merges.results.items, .none);
3937
3938 const type_src = src; // TODO: better source location
3939 const valid_rt = try sema.validateRunTimeType(child_block, type_src, resolved_ty, false);
3940 if (!valid_rt) {
3941 const msg = msg: {
3942 const msg = try sema.errMsg(child_block, type_src, "value with comptime only type '{}' depends on runtime control flow", .{resolved_ty});
3943 errdefer msg.destroy(sema.gpa);
3944
3945 const runtime_src = child_block.runtime_cond orelse child_block.runtime_loop.?;
3946 try sema.errNote(child_block, runtime_src, msg, "runtime control flow here", .{});
3947
3948 try sema.explainWhyTypeIsComptime(child_block, type_src, msg, type_src.toSrcLoc(child_block.src_decl), resolved_ty);
3949
3950 break :msg msg;
3951 };
3952 return sema.failWithOwnedErrorMsg(child_block, msg);
3953 }
39333954 const ty_inst = try sema.addType(resolved_ty);
39343955 try sema.air_extra.ensureUnusedCapacity(gpa, @typeInfo(Air.Block).Struct.fields.len +
39353956 child_block.instructions.items.len);
......@@ -4191,6 +4212,11 @@ fn zirBreak(sema: *Sema, start_block: *Block, inst: Zir.Inst.Index) CompileError
41914212 const br_ref = try start_block.addBr(label.merges.block_inst, operand);
41924213 try label.merges.results.append(sema.gpa, operand);
41934214 try label.merges.br_list.append(sema.gpa, Air.refToIndex(br_ref).?);
4215 block.runtime_index += 1;
4216 if (block.runtime_cond == null and block.runtime_loop == null) {
4217 block.runtime_cond = start_block.runtime_cond orelse start_block.runtime_loop;
4218 block.runtime_loop = start_block.runtime_loop;
4219 }
41944220 return inst;
41954221 }
41964222 }
......@@ -6692,12 +6718,6 @@ fn zirSwitchCapture(
66926718
66936719 if (capture_info.prong_index == std.math.maxInt(@TypeOf(capture_info.prong_index))) {
66946720 // It is the else/`_` prong.
6695 switch (operand_ty.zigTypeTag()) {
6696 .ErrorSet => {
6697 return sema.fail(block, operand_src, "TODO implement Sema for zirSwitchCaptureElse for error sets", .{});
6698 },
6699 else => {},
6700 }
67016721 if (is_ref) {
67026722 assert(operand_is_ref);
67036723 return operand_ptr;
......@@ -6708,7 +6728,10 @@ fn zirSwitchCapture(
67086728 else
67096729 operand_ptr;
67106730
6711 return operand;
6731 switch (operand_ty.zigTypeTag()) {
6732 .ErrorSet => return sema.bitCast(block, block.switch_else_err_ty.?, operand, operand_src),
6733 else => return operand,
6734 }
67126735 }
67136736
67146737 if (is_multi) {
......@@ -6885,6 +6908,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
68856908
68866909 const operand_ty = sema.typeOf(operand);
68876910
6911 var else_error_ty: ?Type = null;
6912
68886913 // Validate usage of '_' prongs.
68896914 if (special_prong == .under and !operand_ty.isNonexhaustiveEnum()) {
68906915 const msg = msg: {
......@@ -7077,6 +7102,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
70777102 .{},
70787103 );
70797104 }
7105 else_error_ty = Type.@"anyerror";
70807106 } else {
70817107 var maybe_msg: ?*Module.ErrorMsg = null;
70827108 errdefer if (maybe_msg) |msg| msg.destroy(sema.gpa);
......@@ -7121,6 +7147,17 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
71217147 .{},
71227148 );
71237149 }
7150
7151 const error_names = operand_ty.errorSetNames();
7152 var names: Module.ErrorSet.NameMap = .{};
7153 try names.ensureUnusedCapacity(sema.arena, error_names.len);
7154 for (error_names) |error_name| {
7155 if (seen_errors.contains(error_name)) continue;
7156
7157 names.putAssumeCapacityNoClobber(error_name, {});
7158 }
7159
7160 else_error_ty = try Type.Tag.error_set_merged.create(sema.arena, names);
71247161 }
71257162 },
71267163 .Union => return sema.fail(block, src, "TODO validate switch .Union", .{}),
......@@ -7398,6 +7435,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
73987435 .label = &label,
73997436 .inlining = block.inlining,
74007437 .is_comptime = block.is_comptime,
7438 .switch_else_err_ty = else_error_ty,
74017439 };
74027440 const merges = &child_block.label.?.merges;
74037441 defer child_block.instructions.deinit(gpa);
......@@ -15447,6 +15485,26 @@ fn validateVarType(
1544715485 var_ty: Type,
1544815486 is_extern: bool,
1544915487) CompileError!void {
15488 if (try sema.validateRunTimeType(block, src, var_ty, is_extern)) return;
15489
15490 const msg = msg: {
15491 const msg = try sema.errMsg(block, src, "variable of type '{}' must be const or comptime", .{var_ty});
15492 errdefer msg.destroy(sema.gpa);
15493
15494 try sema.explainWhyTypeIsComptime(block, src, msg, src.toSrcLoc(block.src_decl), var_ty);
15495
15496 break :msg msg;
15497 };
15498 return sema.failWithOwnedErrorMsg(block, msg);
15499}
15500
15501fn validateRunTimeType(
15502 sema: *Sema,
15503 block: *Block,
15504 src: LazySrcLoc,
15505 var_ty: Type,
15506 is_extern: bool,
15507) CompileError!bool {
1545015508 var ty = var_ty;
1545115509 while (true) switch (ty.zigTypeTag()) {
1545215510 .Bool,
......@@ -15457,7 +15515,7 @@ fn validateVarType(
1545715515 .Frame,
1545815516 .AnyFrame,
1545915517 .Void,
15460 => return,
15518 => return true,
1546115519
1546215520 .BoundFn,
1546315521 .ComptimeFloat,
......@@ -15468,21 +15526,21 @@ fn validateVarType(
1546815526 .Undefined,
1546915527 .Null,
1547015528 .Fn,
15471 => break,
15529 => return false,
1547215530
1547315531 .Pointer => {
1547415532 const elem_ty = ty.childType();
1547515533 switch (elem_ty.zigTypeTag()) {
15476 .Opaque, .Fn => return,
15534 .Opaque, .Fn => return true,
1547715535 else => ty = elem_ty,
1547815536 }
1547915537 },
15480 .Opaque => if (is_extern) return else break,
15538 .Opaque => return is_extern,
1548115539
1548215540 .Optional => {
1548315541 var buf: Type.Payload.ElemType = undefined;
1548415542 const child_ty = ty.optionalChild(&buf);
15485 return validateVarType(sema, block, src, child_ty, is_extern);
15543 return validateRunTimeType(sema, block, src, child_ty, is_extern);
1548615544 },
1548715545 .Array, .Vector => ty = ty.elemType(),
1548815546
......@@ -15490,23 +15548,10 @@ fn validateVarType(
1549015548
1549115549 .Struct, .Union => {
1549215550 const resolved_ty = try sema.resolveTypeFields(block, src, ty);
15493 if (try sema.typeRequiresComptime(block, src, resolved_ty)) {
15494 break;
15495 } else {
15496 return;
15497 }
15551 const needs_comptime = try sema.typeRequiresComptime(block, src, resolved_ty);
15552 return !needs_comptime;
1549815553 },
15499 } else unreachable; // TODO should not need else unreachable
15500
15501 const msg = msg: {
15502 const msg = try sema.errMsg(block, src, "variable of type '{}' must be const or comptime", .{var_ty});
15503 errdefer msg.destroy(sema.gpa);
15504
15505 try sema.explainWhyTypeIsComptime(block, src, msg, src.toSrcLoc(block.src_decl), var_ty);
15506
15507 break :msg msg;
1550815554 };
15509 return sema.failWithOwnedErrorMsg(block, msg);
1551015555}
1551115556
1551215557fn explainWhyTypeIsComptime(
......@@ -18494,8 +18539,8 @@ pub fn bitCastVal(
1849418539 const abi_size = try sema.usizeCast(block, src, old_ty.abiSize(target));
1849518540 const buffer = try sema.gpa.alloc(u8, abi_size);
1849618541 defer sema.gpa.free(buffer);
18497 val.writeToMemory(old_ty, target, buffer);
18498 return Value.readFromMemory(new_ty, target, buffer[buffer_offset..], sema.arena);
18542 val.writeToMemory(old_ty, sema.mod, buffer);
18543 return Value.readFromMemory(new_ty, sema.mod, buffer[buffer_offset..], sema.arena);
1849918544}
1850018545
1850118546fn coerceArrayPtrToSlice(
......@@ -20351,6 +20396,20 @@ pub fn resolveTypeFully(
2035120396 return resolveTypeFully(sema, block, src, ty.optionalChild(&buf));
2035220397 },
2035320398 .ErrorUnion => return resolveTypeFully(sema, block, src, ty.errorUnionPayload()),
20399 .Fn => {
20400 const info = ty.fnInfo();
20401 if (info.is_generic) {
20402 // Resolving of generic function types is defeerred to when
20403 // the function is instantiated.
20404 return;
20405 }
20406 for (info.param_types) |param_ty| {
20407 const param_ty_src = src; // TODO better source location
20408 try sema.resolveTypeFully(block, param_ty_src, param_ty);
20409 }
20410 const return_ty_src = src; // TODO better source location
20411 try sema.resolveTypeFully(block, return_ty_src, info.return_type);
20412 },
2035420413 else => {},
2035520414 }
2035620415}
src/link/MachO.zig+5
......@@ -4301,6 +4301,7 @@ fn populateMissingMetadata(self: *MachO) !void {
43014301 .inner = .{
43024302 .segname = makeStaticString("__PAGEZERO"),
43034303 .vmsize = pagezero_vmsize,
4304 .cmdsize = @sizeOf(macho.segment_command_64),
43044305 },
43054306 },
43064307 });
......@@ -4326,6 +4327,7 @@ fn populateMissingMetadata(self: *MachO) !void {
43264327 .filesize = needed_size,
43274328 .maxprot = macho.PROT.READ | macho.PROT.EXEC,
43284329 .initprot = macho.PROT.READ | macho.PROT.EXEC,
4330 .cmdsize = @sizeOf(macho.segment_command_64),
43294331 },
43304332 },
43314333 });
......@@ -4431,6 +4433,7 @@ fn populateMissingMetadata(self: *MachO) !void {
44314433 .filesize = needed_size,
44324434 .maxprot = macho.PROT.READ | macho.PROT.WRITE,
44334435 .initprot = macho.PROT.READ | macho.PROT.WRITE,
4436 .cmdsize = @sizeOf(macho.segment_command_64),
44344437 },
44354438 },
44364439 });
......@@ -4480,6 +4483,7 @@ fn populateMissingMetadata(self: *MachO) !void {
44804483 .filesize = needed_size,
44814484 .maxprot = macho.PROT.READ | macho.PROT.WRITE,
44824485 .initprot = macho.PROT.READ | macho.PROT.WRITE,
4486 .cmdsize = @sizeOf(macho.segment_command_64),
44834487 },
44844488 },
44854489 });
......@@ -4589,6 +4593,7 @@ fn populateMissingMetadata(self: *MachO) !void {
45894593 .fileoff = fileoff,
45904594 .maxprot = macho.PROT.READ,
45914595 .initprot = macho.PROT.READ,
4596 .cmdsize = @sizeOf(macho.segment_command_64),
45924597 },
45934598 },
45944599 });
src/link/MachO/DebugSymbols.zig+1
......@@ -148,6 +148,7 @@ pub fn populateMissingMetadata(self: *DebugSymbols, allocator: Allocator) !void
148148 .vmsize = needed_size,
149149 .fileoff = fileoff,
150150 .filesize = needed_size,
151 .cmdsize = @sizeOf(macho.segment_command_64),
151152 },
152153 },
153154 });
src/value.zig+26-6
......@@ -1042,7 +1042,8 @@ pub const Value = extern union {
10421042 };
10431043 }
10441044
1045 pub fn writeToMemory(val: Value, ty: Type, target: Target, buffer: []u8) void {
1045 pub fn writeToMemory(val: Value, ty: Type, mod: *Module, buffer: []u8) void {
1046 const target = mod.getTarget();
10461047 if (val.isUndef()) {
10471048 const size = @intCast(usize, ty.abiSize(target));
10481049 std.mem.set(u8, buffer[0..size], 0xaa);
......@@ -1081,7 +1082,7 @@ pub const Value = extern union {
10811082 var buf_off: usize = 0;
10821083 while (elem_i < len) : (elem_i += 1) {
10831084 const elem_val = val.elemValueBuffer(elem_i, &elem_value_buf);
1084 writeToMemory(elem_val, elem_ty, target, buffer[buf_off..]);
1085 writeToMemory(elem_val, elem_ty, mod, buffer[buf_off..]);
10851086 buf_off += elem_size;
10861087 }
10871088 },
......@@ -1092,7 +1093,7 @@ pub const Value = extern union {
10921093 const field_vals = val.castTag(.aggregate).?.data;
10931094 for (fields) |field, i| {
10941095 const off = @intCast(usize, ty.structFieldOffset(i, target));
1095 writeToMemory(field_vals[i], field.ty, target, buffer[off..]);
1096 writeToMemory(field_vals[i], field.ty, mod, buffer[off..]);
10961097 }
10971098 },
10981099 .Packed => {
......@@ -1105,6 +1106,12 @@ pub const Value = extern union {
11051106 host_int.writeTwosComplement(buffer, bit_size, abi_size, target.cpu.arch.endian());
11061107 },
11071108 },
1109 .ErrorSet => {
1110 // TODO revisit this when we have the concept of the error tag type
1111 const Int = u16;
1112 const int = mod.global_error_set.get(val.castTag(.@"error").?.data.name).?;
1113 std.mem.writeInt(Int, buffer[0..@sizeOf(Int)], @intCast(Int, int), target.cpu.arch.endian());
1114 },
11081115 else => @panic("TODO implement writeToMemory for more types"),
11091116 }
11101117 }
......@@ -1153,10 +1160,11 @@ pub const Value = extern union {
11531160
11541161 pub fn readFromMemory(
11551162 ty: Type,
1156 target: Target,
1163 mod: *Module,
11571164 buffer: []const u8,
11581165 arena: Allocator,
11591166 ) Allocator.Error!Value {
1167 const target = mod.getTarget();
11601168 switch (ty.zigTypeTag()) {
11611169 .Int => {
11621170 if (buffer.len == 0) return Value.zero;
......@@ -1184,7 +1192,7 @@ pub const Value = extern union {
11841192 const elems = try arena.alloc(Value, @intCast(usize, ty.arrayLen()));
11851193 var offset: usize = 0;
11861194 for (elems) |*elem| {
1187 elem.* = try readFromMemory(elem_ty, target, buffer[offset..], arena);
1195 elem.* = try readFromMemory(elem_ty, mod, buffer[offset..], arena);
11881196 offset += @intCast(usize, elem_size);
11891197 }
11901198 return Tag.aggregate.create(arena, elems);
......@@ -1196,7 +1204,7 @@ pub const Value = extern union {
11961204 const field_vals = try arena.alloc(Value, fields.len);
11971205 for (fields) |field, i| {
11981206 const off = @intCast(usize, ty.structFieldOffset(i, target));
1199 field_vals[i] = try readFromMemory(field.ty, target, buffer[off..], arena);
1207 field_vals[i] = try readFromMemory(field.ty, mod, buffer[off..], arena);
12001208 }
12011209 return Tag.aggregate.create(arena, field_vals);
12021210 },
......@@ -1212,6 +1220,18 @@ pub const Value = extern union {
12121220 return intToPackedStruct(ty, target, bigint.toConst(), arena);
12131221 },
12141222 },
1223 .ErrorSet => {
1224 // TODO revisit this when we have the concept of the error tag type
1225 const Int = u16;
1226 const int = std.mem.readInt(Int, buffer[0..@sizeOf(Int)], target.cpu.arch.endian());
1227
1228 const payload = try arena.create(Value.Payload.Error);
1229 payload.* = .{
1230 .base = .{ .tag = .@"error" },
1231 .data = .{ .name = mod.error_name_list.items[@intCast(usize, int)] },
1232 };
1233 return Value.initPayload(&payload.base);
1234 },
12151235 else => @panic("TODO implement readFromMemory for more types"),
12161236 }
12171237 }
test/behavior/basic.zig+2-1
......@@ -331,6 +331,7 @@ fn copy(src: *const u64, dst: *u64) void {
331331}
332332
333333test "call result of if else expression" {
334 if (builtin.zig_backend == .stage1) return error.SkipZigTest; // stage1 has different function pointers
334335 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
335336 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
336337 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
......@@ -341,7 +342,7 @@ test "call result of if else expression" {
341342 try expect(mem.eql(u8, f2(false), "b"));
342343}
343344fn f2(x: bool) []const u8 {
344 return (if (x) fA else fB)();
345 return (if (x) &fA else &fB)();
345346}
346347
347348test "memcpy and memset intrinsics" {
test/behavior/switch.zig+5-1
......@@ -430,7 +430,11 @@ test "switch on integer with else capturing expr" {
430430}
431431
432432test "else prong of switch on error set excludes other cases" {
433 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
433 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
434 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
435 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
436 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
437 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
434438
435439 const S = struct {
436440 fn doTheTest() !void {