aboutsummaryrefslogtreecommitdiff
path: root/src/codegen.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/codegen.zig')
-rw-r--r--src/codegen.zig43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/codegen.zig b/src/codegen.zig
index 57fd732b42..c3cd64cf73 100644
--- a/src/codegen.zig
+++ b/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);