Commit 345ac53836

joachimschmidt557 <joachim.schmidt557@outlook.com>
2021-03-01 22:43:42
stage2 ARM: Implement basic integer multiplication
1 parent 9550db3
src/codegen.zig
@@ -899,6 +899,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
                 .load => return self.genLoad(inst.castTag(.load).?),
                 .loop => return self.genLoop(inst.castTag(.loop).?),
                 .not => return self.genNot(inst.castTag(.not).?),
+                .mul => return self.genMul(inst.castTag(.mul).?),
                 .ptrtoint => return self.genPtrToInt(inst.castTag(.ptrtoint).?),
                 .ref => return self.genRef(inst.castTag(.ref).?),
                 .ret => return self.genRet(inst.castTag(.ret).?),
@@ -1128,6 +1129,16 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
             }
         }
 
+        fn genMul(self: *Self, inst: *ir.Inst.BinOp) !MCValue {
+            // No side effects, so if it's unreferenced, do nothing.
+            if (inst.base.isUnused())
+                return MCValue.dead;
+            switch (arch) {
+                .arm, .armeb => return try self.genArmMul(&inst.base, inst.lhs, inst.rhs),
+                else => return self.fail(inst.base.src, "TODO implement mul for {}", .{self.target.cpu.arch}),
+            }
+        }
+
         fn genBitAnd(self: *Self, inst: *ir.Inst.BinOp) !MCValue {
             // No side effects, so if it's unreferenced, do nothing.
             if (inst.base.isUnused())
@@ -1478,6 +1489,38 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
             }
         }
 
+        fn genArmMul(self: *Self, inst: *ir.Inst, op_lhs: *ir.Inst, op_rhs: *ir.Inst) !MCValue {
+            const lhs = try self.resolveInst(op_lhs);
+            const rhs = try self.resolveInst(op_rhs);
+
+            // Destination must be a register
+            // LHS must be a register
+            // RHS must be a register
+            var dst_mcv: MCValue = undefined;
+            var lhs_mcv: MCValue = undefined;
+            var rhs_mcv: MCValue = undefined;
+            if (self.reuseOperand(inst, 0, lhs)) {
+                // LHS is the destination
+                lhs_mcv = if (lhs != .register) try self.copyToNewRegister(inst, lhs) else lhs;
+                rhs_mcv = if (rhs != .register) try self.copyToNewRegister(inst, rhs) else rhs;
+                dst_mcv = lhs_mcv;
+            } else if (self.reuseOperand(inst, 1, rhs)) {
+                // RHS is the destination
+                lhs_mcv = if (lhs != .register) try self.copyToNewRegister(inst, lhs) else lhs;
+                rhs_mcv = if (rhs != .register) try self.copyToNewRegister(inst, rhs) else rhs;
+                dst_mcv = rhs_mcv;
+            } else {
+                // TODO save 1 copy instruction by directly allocating the destination register
+                // LHS is the destination
+                lhs_mcv = try self.copyToNewRegister(inst, lhs);
+                rhs_mcv = if (rhs != .register) try self.copyToNewRegister(inst, rhs) else rhs;
+                dst_mcv = lhs_mcv;
+            }
+
+            writeInt(u32, try self.code.addManyAsArray(4), Instruction.mul(.al, dst_mcv.register, lhs_mcv.register, rhs_mcv.register).toU32());
+            return dst_mcv;
+        }
+
         /// ADD, SUB, XOR, OR, AND
         fn genX8664BinMath(self: *Self, inst: *ir.Inst, op_lhs: *ir.Inst, op_rhs: *ir.Inst, opx: u8, mr: u8) !MCValue {
             try self.code.ensureCapacity(self.code.items.len + 8);
src/ir.zig
@@ -106,6 +106,7 @@ pub const Inst = struct {
         store,
         sub,
         unreach,
+        mul,
         not,
         floatcast,
         intcast,
@@ -165,6 +166,7 @@ pub const Inst = struct {
 
                 .add,
                 .sub,
+                .mul,
                 .cmp_lt,
                 .cmp_lte,
                 .cmp_eq,
src/zir.zig
@@ -1649,6 +1649,7 @@ const DumpTzir = struct {
 
                 .add,
                 .sub,
+                .mul,
                 .cmp_lt,
                 .cmp_lte,
                 .cmp_eq,
@@ -1771,6 +1772,7 @@ const DumpTzir = struct {
 
                 .add,
                 .sub,
+                .mul,
                 .cmp_lt,
                 .cmp_lte,
                 .cmp_eq,
src/zir_sema.zig
@@ -2075,6 +2075,7 @@ fn zirArithmetic(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) InnerError!
     const ir_tag = switch (inst.base.tag) {
         .add => Inst.Tag.add,
         .sub => Inst.Tag.sub,
+        .mul => Inst.Tag.mul,
         else => return mod.fail(scope, inst.base.src, "TODO implement arithmetic for operand '{s}''", .{@tagName(inst.base.tag)}),
     };