| ... | @@ -43,17 +43,11 @@ pub const Instruction = struct { | ... | @@ -43,17 +43,11 @@ pub const Instruction = struct { |
| 43 | | 43 | |
| 44 | pub fn asSigned(imm: Immediate, bit_size: u64) i64 { | 44 | pub fn asSigned(imm: Immediate, bit_size: u64) i64 { |
| 45 | return switch (imm) { | 45 | return switch (imm) { |
| 46 | .signed => |x| switch (bit_size) { | 46 | inline .signed, .unsigned => |x| switch (bit_size) { |
| 47 | 1, 8 => @as(i8, @intCast(x)), | 47 | 1, 8 => @as(i8, if (x < 0) @intCast(x) else @bitCast(@as(u8, @intCast(x)))), |
| 48 | 16 => @as(i16, @intCast(x)), | 48 | 16 => @as(i16, if (x < 0) @intCast(x) else @bitCast(@as(u16, @intCast(x)))), |
| 49 | 32, 64 => x, | 49 | 32 => @as(i32, if (x < 0) @intCast(x) else @bitCast(@as(u32, @intCast(x)))), |
| 50 | else => unreachable, | 50 | 64 => @as(i64, if (x < 0) @intCast(x) else @bitCast(@as(u64, @intCast(x)))), |
| 51 | }, | | |
| 52 | .unsigned => |x| switch (bit_size) { | | |
| 53 | 1, 8 => @as(i8, @bitCast(@as(u8, @intCast(x)))), | | |
| 54 | 16 => @as(i16, @bitCast(@as(u16, @intCast(x)))), | | |
| 55 | 32 => @as(i32, @bitCast(@as(u32, @intCast(x)))), | | |
| 56 | 64 => @bitCast(x), | | |
| 57 | else => unreachable, | 51 | else => unreachable, |
| 58 | }, | 52 | }, |
| 59 | }; | 53 | }; |
| ... | @@ -61,17 +55,11 @@ pub const Instruction = struct { | ... | @@ -61,17 +55,11 @@ pub const Instruction = struct { |
| 61 | | 55 | |
| 62 | pub fn asUnsigned(imm: Immediate, bit_size: u64) u64 { | 56 | pub fn asUnsigned(imm: Immediate, bit_size: u64) u64 { |
| 63 | return switch (imm) { | 57 | return switch (imm) { |
| 64 | .signed => |x| switch (bit_size) { | 58 | inline .signed, .unsigned => |x| switch (bit_size) { |
| 65 | 1, 8 => @as(u8, @bitCast(@as(i8, @intCast(x)))), | 59 | 1, 8 => @as(u8, if (x < 0) @bitCast(@as(i8, @intCast(x))) else @intCast(x)), |
| 66 | 16 => @as(u16, @bitCast(@as(i16, @intCast(x)))), | 60 | 16 => @as(u16, if (x < 0) @bitCast(@as(i16, @intCast(x))) else @intCast(x)), |
| 67 | 32, 64 => @as(u32, @bitCast(x)), | 61 | 32 => @as(u32, if (x < 0) @bitCast(@as(i32, @intCast(x))) else @intCast(x)), |
| 68 | else => unreachable, | 62 | 64 => @as(u64, if (x < 0) @bitCast(@as(i64, @intCast(x))) else @intCast(x)), |
| 69 | }, | | |
| 70 | .unsigned => |x| switch (bit_size) { | | |
| 71 | 1, 8 => @as(u8, @intCast(x)), | | |
| 72 | 16 => @as(u16, @intCast(x)), | | |
| 73 | 32 => @as(u32, @intCast(x)), | | |
| 74 | 64 => x, | | |
| 75 | else => unreachable, | 63 | else => unreachable, |
| 76 | }, | 64 | }, |
| 77 | }; | 65 | }; |
| ... | @@ -712,9 +700,10 @@ pub const Instruction = struct { | ... | @@ -712,9 +700,10 @@ pub const Instruction = struct { |
| 712 | } | 700 | } |
| 713 | } | 701 | } |
| 714 | | 702 | |
| 715 | fn encodeImm(imm: Immediate, kind: Encoding.Op, encoder: anytype) !void { | 703 | fn encodeImm(imm: Immediate, enc_op: Encoding.Op, encoder: anytype) !void { |
| 716 | const raw = imm.asUnsigned(kind.immBitSize()); | 704 | const bit_size = enc_op.immBitSize(); |
| 717 | switch (kind.immBitSize()) { | 705 | const raw = imm.asUnsigned(bit_size); |
| | 706 | switch (bit_size) { |
| 718 | 8 => try encoder.imm8(@as(u8, @intCast(raw))), | 707 | 8 => try encoder.imm8(@as(u8, @intCast(raw))), |
| 719 | 16 => try encoder.imm16(@as(u16, @intCast(raw))), | 708 | 16 => try encoder.imm16(@as(u16, @intCast(raw))), |
| 720 | 32 => try encoder.imm32(@as(u32, @intCast(raw))), | 709 | 32 => try encoder.imm32(@as(u32, @intCast(raw))), |