Skip to content

Commit

Permalink
Refactor: use more efficient code generation in MIPS rewriter.
Browse files Browse the repository at this point in the history
  • Loading branch information
uxmal committed Aug 22, 2024
1 parent ea8b066 commit 40c18eb
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 107 deletions.
3 changes: 0 additions & 3 deletions src/Arch/Mips/MipsPointerScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@

using Reko.Core;
using Reko.Core.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Reko.Arch.Mips
{
Expand Down
26 changes: 11 additions & 15 deletions src/Arch/Mips/MipsRewriter.Alu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,8 @@
using Reko.Core.Intrinsics;
using Reko.Core.Machine;
using Reko.Core.Operators;
using Reko.Core.Rtl;
using Reko.Core.Types;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Reko.Arch.Mips
{
Expand Down Expand Up @@ -136,31 +132,31 @@ private void RewriteCopy(MipsInstruction instr)

private void RewriteDiv(
MipsInstruction instr,
Func<Expression, Expression, Expression> div,
Func<Expression, Expression, Expression> mod)
BinaryOperator div,
BinaryOperator mod)
{
var op1 = RewriteOperand(instr.Operands[0]);
var op2 = RewriteOperand(instr.Operands[1]);
if (instr.Operands.Length > 3)
{
var op3 = RewriteOperand(instr.Operands[2]);
m.Assign(op1, div(op2, op3));
m.Assign(op1, m.Bin(div, op2, op3));
}
else
{
var hi = binder.EnsureRegister(arch.hi);
var lo = binder.EnsureRegister(arch.lo);
m.Assign(lo, div(op1, op2));
m.Assign(hi, mod(op1, op2));
m.Assign(lo, m.Bin(div, op1, op2));
m.Assign(hi, m.Bin(mod, op1, op2));
}
}

private void RewriteDshift32(MipsInstruction instr, Func<Expression, Expression, Expression> ctor)
private void RewriteDshift32(MipsInstruction instr, BinaryOperator shift)
{
var dst = RewriteOperand(instr.Operands[0]);
var src = RewriteOperand(instr.Operands[1]);
var sh = m.IAdd(RewriteOperand(instr.Operands[2]), 32);
m.Assign(dst, ctor(src, sh));
m.Assign(dst, m.Bin(shift, src, sh));
}

private void RewriteExt(MipsInstruction instr)
Expand Down Expand Up @@ -410,12 +406,12 @@ private void RewriteMt(MipsInstruction instr, RegisterStorage reg)
m.Assign(binder.EnsureRegister(reg), opSrc);
}

private void RewriteMod(MipsInstruction instr, Func<Expression, Expression, Expression> ctor)
private void RewriteMod(MipsInstruction instr, BinaryOperator ctor)
{
var dst = RewriteOperand(instr.Operands[0]);
var op1 = RewriteOperand(instr.Operands[1]);
var op2 = RewriteOperand(instr.Operands[2]);
m.Assign(dst, ctor(op1, op2));
m.Assign(dst, m.Bin(ctor, op1, op2));
}

private void RewriteMovCc(MipsInstruction instr, Func<Expression, Expression> cmp0)
Expand Down Expand Up @@ -488,11 +484,11 @@ private void RewriteNor(MipsInstruction instr)
m.Assign(opDst, m.Comp(opSrc));
}

private void RewriteMuh(MipsInstruction instr, PrimitiveType dtProduct, Func<Expression,Expression,Expression> fn)
private void RewriteMuh(MipsInstruction instr, PrimitiveType dtProduct, BinaryOperator fn)
{
var src1 = RewriteOperand(instr.Operands[1]);
var src2 = RewriteOperand(instr.Operands[2]);
var product = fn(src1, src2);
var product = m.Bin(fn, src1, src2);
product.DataType = dtProduct;
var dst = RewriteOperand(instr.Operands[0]);
m.Assign(dst, m.Slice(product, dst.DataType, product.DataType.BitSize - dst.DataType.BitSize));
Expand Down
24 changes: 12 additions & 12 deletions src/Arch/Mips/MipsRewriter.Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private void RewriteBgezal(MipsInstruction instr)
RewriteBal(instr, 1);
return;
}
RewriteBranch0(instr, m.Ge, false);
RewriteBranch0(instr, Operator.Ge, false);
}

private void RewriteBb(MipsInstruction instr, Func<Expression, Expression> condOp)
Expand All @@ -92,15 +92,15 @@ private void RewriteBb(MipsInstruction instr, Func<Expression, Expression> condO
m.Branch(test, addr, instr.InstructionClass);
}

private void RewriteBranch(MipsInstruction instr, Func<Expression, Expression, Expression> condOp, bool link)
private void RewriteBranch(MipsInstruction instr, BinaryOperator condOp, bool link)
{
if (!link)
{
var reg1 = RewriteOperand0(instr.Operands[0]);
var reg2 = RewriteOperand0(instr.Operands[1]);
var addr = (Address)RewriteOperand0(instr.Operands[2]);
var cond = condOp(reg1, reg2);
if (condOp == m.Eq &&
var cond = m.Bin(condOp, PrimitiveType.Bool, reg1, reg2);
if (condOp.Type == OperatorType.Eq &&
(RegisterStorage)instr.Operands[0] ==
(RegisterStorage)instr.Operands[1])
{
Expand All @@ -117,7 +117,7 @@ private void RewriteBranch(MipsInstruction instr, Func<Expression, Expression, E
}
}

private void RewriteBranch0(MipsInstruction instr, Func<Expression, Expression, Expression> condOp, bool link)
private void RewriteBranch0(MipsInstruction instr, BinaryOperator condOp, bool link)
{
if (link)
{
Expand All @@ -130,17 +130,17 @@ private void RewriteBranch0(MipsInstruction instr, Func<Expression, Expression,
if (reg is Constant)
{
// r0 has been replaced with '0'.
if (condOp == m.Lt)
if (condOp.Type == OperatorType.Lt)
{
iclass = InstrClass.Linear;
return; // Branch will never be taken
}
}
var cond = condOp(reg, Constant.Zero(reg.DataType));
var cond = m.Bin(condOp, PrimitiveType.Bool, reg, Constant.Zero(reg.DataType));
m.Branch(cond, addr, instr.InstructionClass);
}

private void RewriteBranchImm(MipsInstruction instr, Func<Expression, Expression, Expression> condOp, bool link)
private void RewriteBranchImm(MipsInstruction instr, BinaryOperator condOp, bool link)
{
if (link)
{
Expand All @@ -151,17 +151,17 @@ private void RewriteBranchImm(MipsInstruction instr, Func<Expression, Expression
var reg = RewriteOperand0(instr.Operands[0]);
var imm = RewriteOperand(instr.Operands[1]);
var addr = (Address) RewriteOperand0(instr.Operands[2]);
var cond = condOp(reg, imm);
var cond = m.Bin(condOp, PrimitiveType.Bool, reg, imm);
m.Branch(cond, addr, instr.InstructionClass);
}

private void RewriteBranchLikely(MipsInstruction instr, Func<Expression, Expression, Expression> condOp)
private void RewriteBranchLikely(MipsInstruction instr, BinaryOperator cmp)
{
var reg1 = RewriteOperand0(instr.Operands[0]);
var reg2 = RewriteOperand0(instr.Operands[1]);
var addr = (Address)RewriteOperand0(instr.Operands[2]);
var cond = condOp(reg1, reg2);
if (condOp == m.Eq &&
var cond = m.Bin(cmp, PrimitiveType.Bool, reg1, reg2);
if (cmp.Type == OperatorType.Eq &&
((RegisterStorage)instr.Operands[0] ==
(RegisterStorage)instr.Operands[1]))
{
Expand Down
29 changes: 12 additions & 17 deletions src/Arch/Mips/MipsRewriter.Fpu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,8 @@
using Reko.Core.Expressions;
using Reko.Core.Machine;
using Reko.Core.Operators;
using Reko.Core.Rtl;
using Reko.Core.Types;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace Reko.Arch.Mips
{
Expand All @@ -45,70 +40,70 @@ private Identifier GetFpuRegPair(MachineOperand op)
return seq;
}

private void RewriteFpuBinopS(MipsInstruction instr, Func<Expression, Expression, Expression> ctor)
private void RewriteFpuBinopS(MipsInstruction instr, BinaryOperator op)
{
var dst = RewriteOperand(instr.Operands[0]);
var src1 = RewriteOperand(instr.Operands[1]);
var src2 = RewriteOperand(instr.Operands[2]);
m.Assign(dst, ctor(src1, src2));
m.Assign(dst, m.Bin(op, src1, src2));
}

private void RewriteFpuBinopD(MipsInstruction instr, Func<Expression,Expression,Expression> ctor)
private void RewriteFpuBinopD(MipsInstruction instr, BinaryOperator op)
{
var dst = GetFpuRegPair(instr.Operands[0]);
var src1 = GetFpuRegPair(instr.Operands[1]);
var src2 = GetFpuRegPair(instr.Operands[2]);
m.Assign(dst, ctor(src1, src2));
m.Assign(dst, m.Bin(op, src1, src2));
}

private void RewriteMac_real(MipsInstruction instr, PrimitiveType dt, Func<Expression, Expression, Expression> ctor)
private void RewriteMac_real(MipsInstruction instr, PrimitiveType dt, BinaryOperator accumFn)
{
var dst = RewriteOperand(instr.Operands[0]);
var acc = RewriteOperand(instr.Operands[1]);
var src1 = RewriteOperand(instr.Operands[2]);
var src2 = RewriteOperand(instr.Operands[3]);
var product = m.FMul(src1, src2);
product.DataType = dt;
var sum = ctor(acc, product);
var sum = m.Bin(accumFn, acc, product);
sum.DataType = dt;
m.Assign(dst, sum);
}

private void RewriteMac_vec(MipsInstruction instr, PrimitiveType dt, Func<Expression, Expression, Expression> ctor)
private void RewriteMac_vec(MipsInstruction instr, PrimitiveType dt, BinaryOperator accFn)
{
var dst = RewriteOperand(instr.Operands[0]);
var acc = RewriteOperand(instr.Operands[1]);
var src1 = RewriteOperand(instr.Operands[2]);
var src2 = RewriteOperand(instr.Operands[3]);
var product = m.FMul(src1, src2);
product.DataType = new ArrayType(dt, src1.DataType.BitSize / dt.BitSize);
var sum = ctor(acc, product);
var sum = m.Bin(accFn, acc, product);
sum.DataType = product.DataType;
m.Assign(dst, sum);
}

private void RewriteNmac_real(MipsInstruction instr, PrimitiveType dt, Func<Expression, Expression, Expression> ctor)
private void RewriteNmac_real(MipsInstruction instr, PrimitiveType dt, BinaryOperator accFn)
{
var dst = RewriteOperand(instr.Operands[0]);
var acc = RewriteOperand(instr.Operands[1]);
var src1 = RewriteOperand(instr.Operands[2]);
var src2 = RewriteOperand(instr.Operands[3]);
var product = m.FMul(src1, src2);
product.DataType = dt;
var sum = ctor(acc, product);
var sum = m.Bin(accFn, acc, product);
sum.DataType = dt;
m.Assign(dst, m.FNeg(sum));
}

private void RewriteNmac_vec(MipsInstruction instr, PrimitiveType dt, Func<Expression, Expression, Expression> ctor)
private void RewriteNmac_vec(MipsInstruction instr, PrimitiveType dt, BinaryOperator accFn)
{
var dst = RewriteOperand(instr.Operands[0]);
var acc = RewriteOperand(instr.Operands[1]);
var src1 = RewriteOperand(instr.Operands[2]);
var src2 = RewriteOperand(instr.Operands[3]);
var product = m.FMul(src1, src2);
product.DataType = new ArrayType(dt, src1.DataType.BitSize / dt.BitSize);
var sum = ctor(acc, product);
var sum = m.Bin(accFn, acc, product);
sum.DataType = product.DataType;
m.Assign(dst, m.FNeg(sum));
}
Expand Down
Loading

0 comments on commit 40c18eb

Please sign in to comment.