Commit d8f7c79298

Jacob Young <jacobly0@users.noreply.github.com>
2023-10-18 04:15:46
x86_64: improve inline assembly support
* C++-style comments * indirect call operands * fix misleading immediate debug formatting
1 parent 8804d72
Changed files (5)
src/arch/x86_64/bits.zig
@@ -607,6 +607,24 @@ pub const Immediate = union(enum) {
         return .{ .signed = x };
     }
 
+    pub fn asSigned(imm: Immediate, bit_size: u64) i64 {
+        return switch (imm) {
+            .signed => |x| switch (bit_size) {
+                1, 8 => @as(i8, @intCast(x)),
+                16 => @as(i16, @intCast(x)),
+                32, 64 => x,
+                else => unreachable,
+            },
+            .unsigned => |x| switch (bit_size) {
+                1, 8 => @as(i8, @bitCast(@as(u8, @intCast(x)))),
+                16 => @as(i16, @bitCast(@as(u16, @intCast(x)))),
+                32 => @as(i32, @bitCast(@as(u32, @intCast(x)))),
+                64 => @as(i64, @bitCast(x)),
+                else => unreachable,
+            },
+        };
+    }
+
     pub fn asUnsigned(imm: Immediate, bit_size: u64) u64 {
         return switch (imm) {
             .signed => |x| switch (bit_size) {
src/arch/x86_64/CodeGen.zig
@@ -10631,6 +10631,7 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void {
         var prefix: Instruction.Prefix = .none;
         const mnem_str = while (mnem_it.next()) |mnem_str| {
             if (mem.startsWith(u8, mnem_str, "#")) continue :next_line;
+            if (mem.startsWith(u8, mnem_str, "//")) continue :next_line;
             if (std.meta.stringToEnum(Instruction.Prefix, mnem_str)) |pre| {
                 if (prefix != .none) return self.fail("extra prefix: '{s}'", .{mnem_str});
                 prefix = pre;
@@ -10714,10 +10715,13 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void {
         next_op: for (&ops) |*op| {
             const op_str = while (!last_op) {
                 const full_str = op_it.next() orelse break :next_op;
-                const trim_str = mem.trim(u8, if (mem.indexOfScalar(u8, full_str, '#')) |hash| hash: {
+                const code_str = if (mem.indexOfScalar(u8, full_str, '#') orelse
+                    mem.indexOf(u8, full_str, "//")) |comment|
+                code: {
                     last_op = true;
-                    break :hash full_str[0..hash];
-                } else full_str, " \t");
+                    break :code full_str[0..comment];
+                } else full_str;
+                const trim_str = mem.trim(u8, code_str, " \t*");
                 if (trim_str.len > 0) break trim_str;
             } else break;
             if (mem.startsWith(u8, op_str, "%%")) {
src/arch/x86_64/encoder.zig
@@ -151,7 +151,11 @@ pub const Instruction = struct {
                         moffs.offset,
                     }),
                 },
-                .imm => |imm| try writer.print("0x{x}", .{imm.asUnsigned(enc_op.immBitSize())}),
+                .imm => |imm| if (enc_op.isSigned()) {
+                    var imms = imm.asSigned(enc_op.immBitSize());
+                    if (imms < 0) try writer.writeByte('-');
+                    try writer.print("0x{x}", .{@abs(imms)});
+                } else try writer.print("0x{x}", .{imm.asUnsigned(enc_op.immBitSize())}),
             }
         }
 
src/arch/x86_64/Encoding.zig
@@ -554,6 +554,7 @@ pub const Op = enum {
         return switch (op) {
             .unity, .imm8, .imm16, .imm32, .imm64 => false,
             .imm8s, .imm16s, .imm32s => true,
+            .rel8, .rel16, .rel32 => true,
             else => unreachable,
         };
     }
test/tests.zig
@@ -997,8 +997,9 @@ pub fn addModuleTests(b: *std.Build, options: ModuleTestOptions) *Step {
             continue;
         }
 
-        // TODO get universal-libc tests passing for self-hosted backends.
-        if (test_target.use_llvm == false and mem.eql(u8, options.name, "universal-libc"))
+        // TODO get universal-libc tests passing for other self-hosted backends.
+        if (test_target.target.getCpuArch() != .x86_64 and
+            test_target.use_llvm == false and mem.eql(u8, options.name, "universal-libc"))
             continue;
 
         // TODO get std lib tests passing for self-hosted backends.