diff options
| author | Jacob Young <jacobly0@users.noreply.github.com> | 2023-10-17 22:15:46 -0400 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2023-10-18 13:57:28 -0400 |
| commit | d8f7c792986d6b9367f49d914689fc2744fdb73a (patch) | |
| tree | 83a40ec29dc1d72242aa40fd341978b605f88b01 | |
| parent | 8804d726842037e923f532ea477e559779c24587 (diff) | |
| download | zig-d8f7c792986d6b9367f49d914689fc2744fdb73a.tar.gz zig-d8f7c792986d6b9367f49d914689fc2744fdb73a.zip | |
x86_64: improve inline assembly support
* C++-style comments
* indirect call operands
* fix misleading immediate debug formatting
| -rw-r--r-- | src/arch/x86_64/CodeGen.zig | 10 | ||||
| -rw-r--r-- | src/arch/x86_64/Encoding.zig | 1 | ||||
| -rw-r--r-- | src/arch/x86_64/bits.zig | 18 | ||||
| -rw-r--r-- | src/arch/x86_64/encoder.zig | 6 | ||||
| -rw-r--r-- | test/tests.zig | 5 |
5 files changed, 34 insertions, 6 deletions
diff --git a/src/arch/x86_64/CodeGen.zig b/src/arch/x86_64/CodeGen.zig index 59d54bd04f..55e872d2fc 100644 --- a/src/arch/x86_64/CodeGen.zig +++ b/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, "%%")) { diff --git a/src/arch/x86_64/Encoding.zig b/src/arch/x86_64/Encoding.zig index 0a0e3f3b7d..7b621e9551 100644 --- a/src/arch/x86_64/Encoding.zig +++ b/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, }; } diff --git a/src/arch/x86_64/bits.zig b/src/arch/x86_64/bits.zig index a640853d5d..5cffaf4fe0 100644 --- a/src/arch/x86_64/bits.zig +++ b/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) { diff --git a/src/arch/x86_64/encoder.zig b/src/arch/x86_64/encoder.zig index 0923a43a77..4966d9864f 100644 --- a/src/arch/x86_64/encoder.zig +++ b/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())}), } } diff --git a/test/tests.zig b/test/tests.zig index 0185f665c9..88ab31aabd 100644 --- a/test/tests.zig +++ b/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. |
