authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-08 18:05:54+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2022-02-08 21:00:07+01:00
log9981b3fd2f7ab85146efa9feebe08a795411d131
tree426608bca102609dd4e4926179b01b68af7b4081
parentf50203c83667ed3ad0c57fdc953322a5f9c221ac

stage2: tiny improvements all over the place

* pass more x64 behavior tests * return with a TODO error when lowering a decl with no runtime bits * insert some debug logs for tracing recursive descent down the type-value tree when lowering types * print `Decl`'s name when print debugging `decl_ref` value

8 files changed, 27 insertions(+), 9 deletions(-)

src/arch/wasm/CodeGen.zig+2
......@@ -970,6 +970,8 @@ pub const DeclGen = struct {
970970
971971 /// Generates the wasm bytecode for the declaration belonging to `Context`
972972 fn genTypedValue(self: *DeclGen, ty: Type, val: Value) InnerError!Result {
973 log.debug("genTypedValue: ty = {}, val = {}", .{ ty, val });
974
973975 const writer = self.code.writer();
974976 if (val.isUndef()) {
975977 try writer.writeByteNTimes(0xaa, @intCast(usize, ty.abiSize(self.target())));
src/arch/x86_64/CodeGen.zig+7-4
......@@ -1973,6 +1973,7 @@ fn structFieldPtr(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, inde
19731973 if (self.liveness.isUnused(inst)) {
19741974 return MCValue.dead;
19751975 }
1976
19761977 const mcv = try self.resolveInst(operand);
19771978 const ptr_ty = self.air.typeOf(operand);
19781979 const struct_ty = ptr_ty.childType();
......@@ -2190,6 +2191,7 @@ fn genBinMathOp(self: *Self, inst: Air.Inst.Index, op_lhs: Air.Inst.Ref, op_rhs:
21902191}
21912192
21922193fn genBinMathOpMir(self: *Self, mir_tag: Mir.Inst.Tag, dst_ty: Type, dst_mcv: MCValue, src_mcv: MCValue) !void {
2194 const abi_size = dst_ty.abiSize(self.target.*);
21932195 switch (dst_mcv) {
21942196 .none => unreachable,
21952197 .undef => unreachable,
......@@ -2216,7 +2218,6 @@ fn genBinMathOpMir(self: *Self, mir_tag: Mir.Inst.Tag, dst_ty: Type, dst_mcv: MC
22162218 });
22172219 },
22182220 .immediate => |imm| {
2219 const abi_size = dst_ty.abiSize(self.target.*);
22202221 _ = try self.addInst(.{
22212222 .tag = mir_tag,
22222223 .ops = (Mir.Ops{
......@@ -2226,7 +2227,11 @@ fn genBinMathOpMir(self: *Self, mir_tag: Mir.Inst.Tag, dst_ty: Type, dst_mcv: MC
22262227 });
22272228 },
22282229 .embedded_in_code, .memory => {
2229 return self.fail("TODO implement x86 ADD/SUB/CMP source memory", .{});
2230 assert(abi_size <= 8);
2231 self.register_manager.freezeRegs(&.{dst_reg});
2232 defer self.register_manager.unfreezeRegs(&.{dst_reg});
2233 const reg = try self.copyToTmpRegister(dst_ty, src_mcv);
2234 return self.genBinMathOpMir(mir_tag, dst_ty, dst_mcv, .{ .register = reg });
22302235 },
22312236 .got_load, .direct_load => {
22322237 return self.fail("TODO implement x86 ADD/SUB/CMP source symbol at index in linker", .{});
......@@ -2235,7 +2240,6 @@ fn genBinMathOpMir(self: *Self, mir_tag: Mir.Inst.Tag, dst_ty: Type, dst_mcv: MC
22352240 if (off > math.maxInt(i32)) {
22362241 return self.fail("stack offset too large", .{});
22372242 }
2238 const abi_size = dst_ty.abiSize(self.target.*);
22392243 const adj_off = off + @intCast(i32, abi_size);
22402244 _ = try self.addInst(.{
22412245 .tag = mir_tag,
......@@ -2259,7 +2263,6 @@ fn genBinMathOpMir(self: *Self, mir_tag: Mir.Inst.Tag, dst_ty: Type, dst_mcv: MC
22592263 if (off > math.maxInt(i32)) {
22602264 return self.fail("stack offset too large", .{});
22612265 }
2262 const abi_size = dst_ty.abiSize(self.target.*);
22632266 if (abi_size > 8) {
22642267 return self.fail("TODO implement ADD/SUB/CMP for stack dst with large ABI", .{});
22652268 }
src/codegen.zig+14
......@@ -150,6 +150,8 @@ pub fn generateSymbol(
150150 const tracy = trace(@src());
151151 defer tracy.end();
152152
153 log.debug("generateSymbol: ty = {}, val = {}", .{ typed_value.ty, typed_value.val });
154
153155 if (typed_value.val.isUndefDeep()) {
154156 const target = bin_file.options.target;
155157 const abi_size = try math.cast(usize, typed_value.ty.abiSize(target));
......@@ -485,6 +487,18 @@ fn lowerDeclRef(
485487 return Result{ .appended = {} };
486488 }
487489
490 const is_fn_body = decl.ty.zigTypeTag() == .Fn;
491 if (!is_fn_body and !decl.ty.hasRuntimeBits()) {
492 return Result{
493 .fail = try ErrorMsg.create(
494 bin_file.allocator,
495 src_loc,
496 "TODO handle void types when lowering decl ref",
497 .{},
498 ),
499 };
500 }
501
488502 if (decl.analysis != .complete) return error.AnalysisFail;
489503 decl.markAlive();
490504 const vaddr = vaddr: {
src/value.zig+4-1
......@@ -711,7 +711,10 @@ pub const Value = extern union {
711711 const decl = val.castTag(.decl_ref_mut).?.data.decl;
712712 return out_stream.print("(decl_ref_mut '{s}')", .{decl.name});
713713 },
714 .decl_ref => return out_stream.writeAll("(decl ref)"),
714 .decl_ref => {
715 const decl = val.castTag(.decl_ref).?.data;
716 return out_stream.print("(decl ref '{s}')", .{decl.name});
717 },
715718 .elem_ptr => {
716719 const elem_ptr = val.castTag(.elem_ptr).?.data;
717720 try out_stream.print("&[{}] ", .{elem_ptr.index});
test/behavior/bugs/1025.zig-1
......@@ -9,7 +9,6 @@ fn getA() A {
99}
1010
1111test "bug 1025" {
12 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
1312 const a = getA();
1413 try @import("std").testing.expect(a.B == u8);
1514}
test/behavior/bugs/1277.zig-1
......@@ -13,6 +13,5 @@ fn f() i32 {
1313
1414test "don't emit an LLVM global for a const function when it's in an optional in a struct" {
1515 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
16 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
1716 try std.testing.expect(s.f.?() == 1234);
1817}
test/behavior/bugs/1310.zig-1
......@@ -24,6 +24,5 @@ fn agent_callback(_vm: [*]VM, options: [*]u8) callconv(.C) i32 {
2424
2525test "fixed" {
2626 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
27 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
2827 try expect(agent_callback(undefined, undefined) == 11);
2928}
test/behavior/bugs/1500.zig-1
......@@ -7,7 +7,6 @@ const B = *const fn (A) void;
77
88test "allow these dependencies" {
99 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
10 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
1110 var a: A = undefined;
1211 var b: B = undefined;
1312 if (false) {