private void AssertSmallConst(string sExp, int shift, uint mult) { var m = new ProcedureBuilder(); var c = Constant.Int32((int)mult); var r1 = m.Reg32("r1", 1); var r2 = m.Reg32("r2", 2); var r2_r1 = m.Frame.EnsureSequence(r2.Storage, r1.Storage, PrimitiveType.Word64); var ass = m.Assign(r2_r1, m.SMul(r1, c)); m.Alias(r2, m.Slice(PrimitiveType.Word32, r2_r1, 32)); if (shift != 0) { m.Assign(r2, m.Sar(r2, shift)); } var proc = m.Procedure; var ssa = new SsaTransform( null, proc, null, proc.CreateBlockDominatorGraph(), new HashSet <RegisterStorage>()).Transform(); var ctx = new SsaEvaluationContext(null, ssa.Identifiers, null); var rule = new ConstDivisionImplementedByMultiplication(ssa); ctx.Statement = proc.EntryBlock.Succ[0].Statements[0]; Assert.IsTrue(rule.Match(ass)); var instr = rule.TransformInstruction(); Assert.AreEqual(sExp, instr.Src.ToString()); }
private void AssertSmallConst(string sExp, int shift, uint mult) { var m = new ProcedureBuilder(); var c = Constant.Int32((int)mult); var r1 = m.Reg32("r1", 1); var r2 = m.Reg32("r2", 2); var r2_r1 = m.Frame.EnsureSequence(PrimitiveType.Word64, r2.Storage, r1.Storage); var ass = m.Assign(r2_r1, m.SMul(r1, c)); m.Alias(r2, m.Slice(PrimitiveType.Word32, r2_r1, 32)); if (shift != 0) { m.Assign(r2, m.Sar(r2, shift)); } m.MStore(m.Word32(0x0402000), r2); // Force use of r2 m.Return(); var proc = m.Procedure; var flow = new ProgramDataFlow(); var program = new Program() { Architecture = m.Architecture, }; var sst = new SsaTransform( program, proc, new HashSet <Procedure>(), null, flow); sst.Transform(); proc.Dump(true); var ctx = new SsaEvaluationContext(m.Architecture, sst.SsaState.Identifiers, null); var rule = new ConstDivisionImplementedByMultiplication(sst.SsaState); ctx.Statement = proc.EntryBlock.Succ[0].Statements[0]; Assert.IsTrue(rule.Match(ctx.Statement.Instruction)); var instr = rule.TransformInstruction(); Assert.AreEqual(sExp, instr.Src.ToString()); }