Пример #1
0
        public void VpPhiWithConstants()
        {
            var c1 = Constant.Word16(0x4711);
            var c2 = Constant.Word16(0x4711);
            var r1 = Reg16("r1");
            var r2 = Reg16("r2");
            var r3 = Reg16("r3");

            m.Emit(m.Assign(r1, c1));
            m.Emit(m.Assign(r2, c2));
            var phiStm = m.Phi(r3, r1, r2);

            RunValuePropagator(m);
            Assert.AreEqual("r3 = 0x4711", phiStm.Instruction.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(r2.Storage, r1.Storage, PrimitiveType.Word64);

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

            m.Emit(new AliasAssignment(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);
            var rule = new ConstDivisionImplementedByMultiplication(ssa);

            ctx.Statement = proc.EntryBlock.Succ[0].Statements[0];
            Assert.IsTrue(rule.Match(ass));
            ass = rule.TransformInstruction();
            Assert.AreEqual(sExp, ass.Src.ToString());
        }
Пример #3
0
        public void VpPhiLoops()
        {
            var m   = new ProcedureBuilder();
            var ssa = new SsaState(m.Procedure, null);

            ssaIds = ssa.Identifiers;
            var fp = Reg16("fp");
            var a  = Reg16("a");
            var b  = Reg16("b");
            var c  = Reg16("c");
            var d  = Reg16("d");
            var x  = Reg16("x");
            var y  = Reg16("y");
            var z  = Reg16("z");

            m.Emit(m.Assign(y, m.IAdd(x, 4)));
            m.Emit(m.Assign(z, m.ISub(x, 8)));
            m.Emit(m.Assign(a, m.ISub(fp, 12)));
            m.Emit(m.Assign(b, m.ISub(fp, 12)));
            m.Emit(m.Assign(c, m.ISub(y, 4)));
            m.Emit(m.Assign(d, m.IAdd(z, 8)));
            var phiStm = m.Phi(x, a, b, c, d);
            var stms   = m.Procedure.EntryBlock.Succ[0].Statements;

            stms.ForEach(stm =>
            {
                var ass = stm.Instruction as Assignment;
                if (ass != null)
                {
                    ssaIds[ass.Dst].DefStatement = stm;
                }
                var phiAss = stm.Instruction as PhiAssignment;
                if (phiAss != null)
                {
                    ssaIds[phiAss.Dst].DefStatement = stm;
                }
            });
            var vp = new ValuePropagator(arch, ssa, listener);

            vp.Transform();
            Assert.AreEqual("x = fp - 0x000C", phiStm.Instruction.ToString());
        }