authorgravatar for joachim.schmidt557@outlook.comJoachim Schmidt <joachim.schmidt557@outlook.com> 2021-03-27 21:39:55+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-03-31 23:26:49+02:00
loge088a17f56a154ecc43c36e8308833d657e6f43e
tree776c36bb528ac145d96dda5ede95ec3d3e8ef3d0
parent501b4aff9968e691283dd6ce637b503a12b03d38

stage2 AArch64: implement strb and strh


2 files changed, 63 insertions(+), 33 deletions(-)

src/codegen.zig+8-2
......@@ -3111,7 +3111,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
31113111 const adj_off = stack_offset + abi_size;
31123112
31133113 switch (abi_size) {
3114 4, 8 => {
3114 1, 2, 4, 8 => {
31153115 const offset = if (math.cast(i9, adj_off)) |imm|
31163116 Instruction.LoadStoreOffset.imm_post_index(-imm)
31173117 else |_|
......@@ -3121,8 +3121,14 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
31213121 .aarch64_32 => .w29,
31223122 else => unreachable,
31233123 };
3124 const str = switch (abi_size) {
3125 1 => Instruction.strb,
3126 2 => Instruction.strh,
3127 4, 8 => Instruction.str,
3128 else => unreachable, // unexpected abi size
3129 };
31243130
3125 writeInt(u32, try self.code.addManyAsArray(4), Instruction.str(reg, rn, .{
3131 writeInt(u32, try self.code.addManyAsArray(4), str(reg, rn, .{
31263132 .offset = offset,
31273133 }).toU32());
31283134 },
src/codegen/aarch64.zig+55-31
......@@ -484,7 +484,23 @@ pub const Instruction = union(enum) {
484484 }
485485 };
486486
487 fn loadStoreRegister(rt: Register, rn: Register, offset: LoadStoreOffset, load: bool) Instruction {
487 /// Which kind of load/store to perform
488 const LoadStoreVariant = enum {
489 /// 32-bit or 64-bit
490 normal,
491 /// 16-bit
492 half,
493 /// 8-bit
494 byte,
495 };
496
497 fn loadStoreRegister(
498 rt: Register,
499 rn: Register,
500 offset: LoadStoreOffset,
501 variant: LoadStoreVariant,
502 load: bool,
503 ) Instruction {
488504 const off = offset.toU12();
489505 const op1: u2 = blk: {
490506 switch (offset) {
......@@ -497,35 +513,27 @@ pub const Instruction = union(enum) {
497513 break :blk 0b00;
498514 };
499515 const opc: u2 = if (load) 0b01 else 0b00;
500 switch (rt.size()) {
501 32 => {
502 return Instruction{
503 .LoadStoreRegister = .{
504 .rt = rt.id(),
505 .rn = rn.id(),
506 .offset = offset.toU12(),
507 .opc = opc,
508 .op1 = op1,
509 .v = 0,
510 .size = 0b10,
511 },
512 };
513 },
514 64 => {
515 return Instruction{
516 .LoadStoreRegister = .{
517 .rt = rt.id(),
518 .rn = rn.id(),
519 .offset = offset.toU12(),
520 .opc = opc,
521 .op1 = op1,
522 .v = 0,
523 .size = 0b11,
524 },
525 };
516 return Instruction{
517 .LoadStoreRegister = .{
518 .rt = rt.id(),
519 .rn = rn.id(),
520 .offset = off,
521 .opc = opc,
522 .op1 = op1,
523 .v = 0,
524 .size = blk: {
525 switch (variant) {
526 .normal => switch (rt.size()) {
527 32 => break :blk 0b10,
528 64 => break :blk 0b11,
529 else => unreachable, // unexpected register size
530 },
531 .half => break :blk 0b01,
532 .byte => break :blk 0b00,
533 }
534 },
526535 },
527 else => unreachable, // unexpected register size
528 }
536 };
529537 }
530538
531539 fn loadStorePairOfRegisters(
......@@ -748,7 +756,7 @@ pub const Instruction = union(enum) {
748756
749757 pub fn ldr(rt: Register, args: LdrArgs) Instruction {
750758 switch (args) {
751 .register => |info| return loadStoreRegister(rt, info.rn, info.offset, true),
759 .register => |info| return loadStoreRegister(rt, info.rn, info.offset, .normal, true),
752760 .literal => |literal| return loadLiteral(rt, literal),
753761 }
754762 }
......@@ -758,7 +766,15 @@ pub const Instruction = union(enum) {
758766 };
759767
760768 pub fn str(rt: Register, rn: Register, args: StrArgs) Instruction {
761 return loadStoreRegister(rt, rn, args.offset, false);
769 return loadStoreRegister(rt, rn, args.offset, .normal, false);
770 }
771
772 pub fn strh(rt: Register, rn: Register, args: StrArgs) Instruction {
773 return loadStoreRegister(rt, rn, args.offset, .half, false);
774 }
775
776 pub fn strb(rt: Register, rn: Register, args: StrArgs) Instruction {
777 return loadStoreRegister(rt, rn, args.offset, .byte, false);
762778 }
763779
764780 // Load or store pair of registers
......@@ -996,6 +1012,14 @@ test "serialize instructions" {
9961012 .inst = Instruction.str(.x2, .x1, .{ .offset = Instruction.LoadStoreOffset.reg(.x3) }),
9971013 .expected = 0b11_111_0_00_00_1_00011_011_0_10_00001_00010,
9981014 },
1015 .{ // strh w0, [x1]
1016 .inst = Instruction.strh(.w0, .x1, .{}),
1017 .expected = 0b01_111_0_01_00_000000000000_00001_00000,
1018 },
1019 .{ // strb w8, [x9]
1020 .inst = Instruction.strb(.w8, .x9, .{}),
1021 .expected = 0b00_111_0_01_00_000000000000_01001_01000,
1022 },
9991023 .{ // adr x2, #0x8
10001024 .inst = Instruction.adr(.x2, 0x8),
10011025 .expected = 0b0_00_10000_0000000000000000010_00010,