Commit 9981b3fd2f

Jakub Konka <kubkon@jakubkonka.com>
2022-02-08 18:05:54
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
1 parent f50203c
Changed files (8)
src/arch/wasm/CodeGen.zig
@@ -970,6 +970,8 @@ pub const DeclGen = struct {
 
     /// Generates the wasm bytecode for the declaration belonging to `Context`
     fn genTypedValue(self: *DeclGen, ty: Type, val: Value) InnerError!Result {
+        log.debug("genTypedValue: ty = {}, val = {}", .{ ty, val });
+
         const writer = self.code.writer();
         if (val.isUndef()) {
             try writer.writeByteNTimes(0xaa, @intCast(usize, ty.abiSize(self.target())));
src/arch/x86_64/CodeGen.zig
@@ -1973,6 +1973,7 @@ fn structFieldPtr(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, inde
     if (self.liveness.isUnused(inst)) {
         return MCValue.dead;
     }
+
     const mcv = try self.resolveInst(operand);
     const ptr_ty = self.air.typeOf(operand);
     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:
 }
 
 fn genBinMathOpMir(self: *Self, mir_tag: Mir.Inst.Tag, dst_ty: Type, dst_mcv: MCValue, src_mcv: MCValue) !void {
+    const abi_size = dst_ty.abiSize(self.target.*);
     switch (dst_mcv) {
         .none => unreachable,
         .undef => unreachable,
@@ -2216,7 +2218,6 @@ fn genBinMathOpMir(self: *Self, mir_tag: Mir.Inst.Tag, dst_ty: Type, dst_mcv: MC
                     });
                 },
                 .immediate => |imm| {
-                    const abi_size = dst_ty.abiSize(self.target.*);
                     _ = try self.addInst(.{
                         .tag = mir_tag,
                         .ops = (Mir.Ops{
@@ -2226,7 +2227,11 @@ fn genBinMathOpMir(self: *Self, mir_tag: Mir.Inst.Tag, dst_ty: Type, dst_mcv: MC
                     });
                 },
                 .embedded_in_code, .memory => {
-                    return self.fail("TODO implement x86 ADD/SUB/CMP source memory", .{});
+                    assert(abi_size <= 8);
+                    self.register_manager.freezeRegs(&.{dst_reg});
+                    defer self.register_manager.unfreezeRegs(&.{dst_reg});
+                    const reg = try self.copyToTmpRegister(dst_ty, src_mcv);
+                    return self.genBinMathOpMir(mir_tag, dst_ty, dst_mcv, .{ .register = reg });
                 },
                 .got_load, .direct_load => {
                     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
                     if (off > math.maxInt(i32)) {
                         return self.fail("stack offset too large", .{});
                     }
-                    const abi_size = dst_ty.abiSize(self.target.*);
                     const adj_off = off + @intCast(i32, abi_size);
                     _ = try self.addInst(.{
                         .tag = mir_tag,
@@ -2259,7 +2263,6 @@ fn genBinMathOpMir(self: *Self, mir_tag: Mir.Inst.Tag, dst_ty: Type, dst_mcv: MC
             if (off > math.maxInt(i32)) {
                 return self.fail("stack offset too large", .{});
             }
-            const abi_size = dst_ty.abiSize(self.target.*);
             if (abi_size > 8) {
                 return self.fail("TODO implement ADD/SUB/CMP for stack dst with large ABI", .{});
             }
src/codegen.zig
@@ -150,6 +150,8 @@ pub fn generateSymbol(
     const tracy = trace(@src());
     defer tracy.end();
 
+    log.debug("generateSymbol: ty = {}, val = {}", .{ typed_value.ty, typed_value.val });
+
     if (typed_value.val.isUndefDeep()) {
         const target = bin_file.options.target;
         const abi_size = try math.cast(usize, typed_value.ty.abiSize(target));
@@ -485,6 +487,18 @@ fn lowerDeclRef(
         return Result{ .appended = {} };
     }
 
+    const is_fn_body = decl.ty.zigTypeTag() == .Fn;
+    if (!is_fn_body and !decl.ty.hasRuntimeBits()) {
+        return Result{
+            .fail = try ErrorMsg.create(
+                bin_file.allocator,
+                src_loc,
+                "TODO handle void types when lowering decl ref",
+                .{},
+            ),
+        };
+    }
+
     if (decl.analysis != .complete) return error.AnalysisFail;
     decl.markAlive();
     const vaddr = vaddr: {
src/value.zig
@@ -711,7 +711,10 @@ pub const Value = extern union {
                 const decl = val.castTag(.decl_ref_mut).?.data.decl;
                 return out_stream.print("(decl_ref_mut '{s}')", .{decl.name});
             },
-            .decl_ref => return out_stream.writeAll("(decl ref)"),
+            .decl_ref => {
+                const decl = val.castTag(.decl_ref).?.data;
+                return out_stream.print("(decl ref '{s}')", .{decl.name});
+            },
             .elem_ptr => {
                 const elem_ptr = val.castTag(.elem_ptr).?.data;
                 try out_stream.print("&[{}] ", .{elem_ptr.index});
test/behavior/bugs/1025.zig
@@ -9,7 +9,6 @@ fn getA() A {
 }
 
 test "bug 1025" {
-    if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
     const a = getA();
     try @import("std").testing.expect(a.B == u8);
 }
test/behavior/bugs/1277.zig
@@ -13,6 +13,5 @@ fn f() i32 {
 
 test "don't emit an LLVM global for a const function when it's in an optional in a struct" {
     if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
-    if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
     try std.testing.expect(s.f.?() == 1234);
 }
test/behavior/bugs/1310.zig
@@ -24,6 +24,5 @@ fn agent_callback(_vm: [*]VM, options: [*]u8) callconv(.C) i32 {
 
 test "fixed" {
     if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
-    if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
     try expect(agent_callback(undefined, undefined) == 11);
 }
test/behavior/bugs/1500.zig
@@ -7,7 +7,6 @@ const B = *const fn (A) void;
 
 test "allow these dependencies" {
     if (builtin.zig_backend == .stage2_c) return error.SkipZigTest;
-    if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
     var a: A = undefined;
     var b: B = undefined;
     if (false) {