authorgravatar for joachim.schmidt557@outlook.comJoachim Schmidt <joachim.schmidt557@outlook.com> 2021-03-01 22:43:42+01:00
committergravatar for joachim.schmidt557@outlook.comJoachim Schmidt <joachim.schmidt557@outlook.com> 2021-03-02 00:14:56+01:00
log345ac53836bce5eb39a04723b30ac82dd7d5098f
tree5f55cdb9a5031f3a9f5ae570cbad0b2e24be1402
parent9550db33cbb7dadd555842ef6d7214660e2b00d6
signature Commit is signed but in an unrecognized format.

stage2 ARM: Implement basic integer multiplication


4 files changed, 48 insertions(+), 0 deletions(-)

src/codegen.zig+43
......@@ -899,6 +899,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
899899 .load => return self.genLoad(inst.castTag(.load).?),
900900 .loop => return self.genLoop(inst.castTag(.loop).?),
901901 .not => return self.genNot(inst.castTag(.not).?),
902 .mul => return self.genMul(inst.castTag(.mul).?),
902903 .ptrtoint => return self.genPtrToInt(inst.castTag(.ptrtoint).?),
903904 .ref => return self.genRef(inst.castTag(.ref).?),
904905 .ret => return self.genRet(inst.castTag(.ret).?),
......@@ -1128,6 +1129,16 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
11281129 }
11291130 }
11301131
1132 fn genMul(self: *Self, inst: *ir.Inst.BinOp) !MCValue {
1133 // No side effects, so if it's unreferenced, do nothing.
1134 if (inst.base.isUnused())
1135 return MCValue.dead;
1136 switch (arch) {
1137 .arm, .armeb => return try self.genArmMul(&inst.base, inst.lhs, inst.rhs),
1138 else => return self.fail(inst.base.src, "TODO implement mul for {}", .{self.target.cpu.arch}),
1139 }
1140 }
1141
11311142 fn genBitAnd(self: *Self, inst: *ir.Inst.BinOp) !MCValue {
11321143 // No side effects, so if it's unreferenced, do nothing.
11331144 if (inst.base.isUnused())
......@@ -1478,6 +1489,38 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
14781489 }
14791490 }
14801491
1492 fn genArmMul(self: *Self, inst: *ir.Inst, op_lhs: *ir.Inst, op_rhs: *ir.Inst) !MCValue {
1493 const lhs = try self.resolveInst(op_lhs);
1494 const rhs = try self.resolveInst(op_rhs);
1495
1496 // Destination must be a register
1497 // LHS must be a register
1498 // RHS must be a register
1499 var dst_mcv: MCValue = undefined;
1500 var lhs_mcv: MCValue = undefined;
1501 var rhs_mcv: MCValue = undefined;
1502 if (self.reuseOperand(inst, 0, lhs)) {
1503 // LHS is the destination
1504 lhs_mcv = if (lhs != .register) try self.copyToNewRegister(inst, lhs) else lhs;
1505 rhs_mcv = if (rhs != .register) try self.copyToNewRegister(inst, rhs) else rhs;
1506 dst_mcv = lhs_mcv;
1507 } else if (self.reuseOperand(inst, 1, rhs)) {
1508 // RHS is the destination
1509 lhs_mcv = if (lhs != .register) try self.copyToNewRegister(inst, lhs) else lhs;
1510 rhs_mcv = if (rhs != .register) try self.copyToNewRegister(inst, rhs) else rhs;
1511 dst_mcv = rhs_mcv;
1512 } else {
1513 // TODO save 1 copy instruction by directly allocating the destination register
1514 // LHS is the destination
1515 lhs_mcv = try self.copyToNewRegister(inst, lhs);
1516 rhs_mcv = if (rhs != .register) try self.copyToNewRegister(inst, rhs) else rhs;
1517 dst_mcv = lhs_mcv;
1518 }
1519
1520 writeInt(u32, try self.code.addManyAsArray(4), Instruction.mul(.al, dst_mcv.register, lhs_mcv.register, rhs_mcv.register).toU32());
1521 return dst_mcv;
1522 }
1523
14811524 /// ADD, SUB, XOR, OR, AND
14821525 fn genX8664BinMath(self: *Self, inst: *ir.Inst, op_lhs: *ir.Inst, op_rhs: *ir.Inst, opx: u8, mr: u8) !MCValue {
14831526 try self.code.ensureCapacity(self.code.items.len + 8);
src/ir.zig+2
......@@ -106,6 +106,7 @@ pub const Inst = struct {
106106 store,
107107 sub,
108108 unreach,
109 mul,
109110 not,
110111 floatcast,
111112 intcast,
......@@ -165,6 +166,7 @@ pub const Inst = struct {
165166
166167 .add,
167168 .sub,
169 .mul,
168170 .cmp_lt,
169171 .cmp_lte,
170172 .cmp_eq,
src/zir.zig+2
......@@ -1649,6 +1649,7 @@ const DumpTzir = struct {
16491649
16501650 .add,
16511651 .sub,
1652 .mul,
16521653 .cmp_lt,
16531654 .cmp_lte,
16541655 .cmp_eq,
......@@ -1771,6 +1772,7 @@ const DumpTzir = struct {
17711772
17721773 .add,
17731774 .sub,
1775 .mul,
17741776 .cmp_lt,
17751777 .cmp_lte,
17761778 .cmp_eq,
src/zir_sema.zig+1
......@@ -2075,6 +2075,7 @@ fn zirArithmetic(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!
20752075 const ir_tag = switch (inst.base.tag) {
20762076 .add => Inst.Tag.add,
20772077 .sub => Inst.Tag.sub,
2078 .mul => Inst.Tag.mul,
20782079 else => return mod.fail(scope, inst.base.src, "TODO implement arithmetic for operand '{s}''", .{@tagName(inst.base.tag)}),
20792080 };
20802081