예제 #1
0
        private void RunTest(string sExp, Action <ProcedureBuilder> bld)
        {
            var m = new ProcedureBuilder();

            bld(m);
            var proc  = m.Procedure;
            var alias = new Aliases(proc);

            alias.Transform();
            var ssa = new SsaTransform(
                null,
                proc,
                null,
                proc.CreateBlockDominatorGraph(),
                new HashSet <RegisterStorage>()).Transform();
            var segmentMap = new SegmentMap(Address.Ptr32(0));
            var vp         = new ValuePropagator(segmentMap, ssa, null, null);

            vp.Transform();
            var rule = new ConstDivisionImplementedByMultiplication(ssa);

            rule.Transform();
            var sw = new StringWriter();

            proc.Write(false, sw);
            if (sExp != sw.ToString())
            {
                Debug.Print("{0}", sw);
                Assert.AreEqual(sExp, sw.ToString());
            }
        }
        private void RunTest(string sExp, Action <ProcedureBuilder> bld)
        {
            var m = new ProcedureBuilder();

            bld(m);
            var proc = m.Procedure;
            var ssa  = new SsaTransform(
                new Program {
                Architecture = m.Architecture
            },
                proc,
                null,
                null,
                null).Transform();
            var segmentMap = new SegmentMap(Address.Ptr32(0));
            var vp         = new ValuePropagator(segmentMap, ssa, new CallGraph(), null, null);

            vp.Transform();
            var rule = new ConstDivisionImplementedByMultiplication(ssa);

            rule.Transform();
            var sw = new StringWriter();

            proc.Write(false, sw);
            if (sExp != sw.ToString())
            {
                Debug.Print("{0}", sw);
                Assert.AreEqual(sExp, sw.ToString());
            }
        }
예제 #3
0
        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 Frac(int num, int denom)
        {
            var q = (double)num / denom;
            var n = (uint)Math.Round(q * Math.Pow(2.0, 32));

            var rat = ConstDivisionImplementedByMultiplication.FindBestRational(n);

            Debug.Print("{0}/{1} - {2}", num, denom, rat);
            if (num != rat.Numerator ||
                denom != rat.Denominator)
            {
                Debug.Print("***** inexact *****");
            }
        }
        private void Frac(int num, int denom)
        {
            var q = (double)num / denom;
            var n = (uint)Math.Round(q * Math.Pow(2.0, 32));
            var f = n * Math.Pow(2.0, -32);

            var rat = ConstDivisionImplementedByMultiplication.ContinuedFraction(f);

            if (num != rat.Numerator ||
                denom != rat.Denominator)
            {
                Debug.Print("***** inexact *****");
            }
        }
        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());
        }
        public void Cdiv_3()
        {
            /*
             *   eax = ~0x33333332
             * edx_eax = esi *u eax
             * edx = edx >>u 0x03
             */
            var c     = Constant.UInt32(0x55555555);
            var r1    = m.Reg32("r1");
            var r2    = m.Reg32("r2");
            var r2_r1 = m.Frame.EnsureSequence(r2, r1, PrimitiveType.Word64);

            var ass = m.Assign(r2_r1, m.UMul(r1, c));

            var proc = m.Procedure;
            var ssa  = new SsaTransform(null, proc, proc.CreateBlockDominatorGraph()).Transform();
            var ctx  = new SsaEvaluationContext(null, ssa.Identifiers);
            var rule = new ConstDivisionImplementedByMultiplication(ctx);

            //ctx.Statement = proc.EntryBlock.Succ[0].Statements[0];
            //Assert.IsTrue(rule.Match((BinaryExpression) ass.Src));
            //ass.Src = rule.Transform();
            Assert.AreEqual("x = id /u 3", ass.ToString());
        }