Beispiel #1
0
        protected override void RunTest(Program program, TextWriter fut)
        {
            IDynamicLinker   dynamicLinker = null;
            var              listener      = sc.RequireService <DecompilerEventListener>();
            DataFlowAnalysis dfa           = new DataFlowAnalysis(program, dynamicLinker, sc);
            var              ssts          = dfa.UntangleProcedures();

            foreach (Procedure proc in program.Procedures.Values)
            {
                var      sst = ssts.Single(s => s.SsaState.Procedure == proc);
                SsaState ssa = sst.SsaState;

                ConditionCodeEliminator cce = new ConditionCodeEliminator(program, ssa, listener);
                cce.Transform();
                DeadCode.Eliminate(ssa);

                ValuePropagator vp = new ValuePropagator(program.SegmentMap, ssa, program.CallGraph, dynamicLinker, listener);
                vp.Transform();
                DeadCode.Eliminate(ssa);
                Coalescer co = new Coalescer(ssa);
                co.Transform();

                ssa.Write(fut);
                proc.Write(false, fut);
                fut.WriteLine();

                ssa.Validate(s => { ssa.Dump(true); Assert.Fail(s); });
            }
        }
        protected override void RunTest(Program program, TextWriter writer)
        {
            var importResolver = new Mock <IImportResolver>().Object;
            var listener       = new FakeDecompilerEventListener();
            var dfa            = new DataFlowAnalysis(program, importResolver, listener);

            dfa.UntangleProcedures();
            foreach (Procedure proc in program.Procedures.Values)
            {
                var larw = new LongAddRewriter(proc);
                larw.Transform();

                Aliases alias = new Aliases(proc, dfa.ProgramDataFlow);
                alias.Transform();
                var      sst = new SsaTransform(dfa.ProgramDataFlow, proc, importResolver, proc.CreateBlockDominatorGraph(), new HashSet <RegisterStorage>());
                SsaState ssa = sst.SsaState;

                var cce = new ConditionCodeEliminator(ssa, program.Platform);
                cce.Transform();

                var vp = new ValuePropagator(program.SegmentMap, ssa, importResolver, listener);
                vp.Transform();

                DeadCode.Eliminate(proc, ssa);

                ssa.Write(writer);
                proc.Write(false, writer);
                writer.WriteLine();

                ssa.Validate(s => Assert.Fail(s));
            }
        }
Beispiel #3
0
        protected override void RunTest(Program program, TextWriter writer)
        {
            var flow           = new ProgramDataFlow(program);
            var eventListener  = new FakeDecompilerEventListener();
            var importResolver = new Mock <IImportResolver>().Object;
            var trf            = new TrashedRegisterFinder(program, program.Procedures.Values, flow, eventListener);

            trf.Compute();
            trf.RewriteBasicBlocks();
            Dump(program.CallGraph);
            RegisterLiveness.Compute(program, flow, eventListener);
            GlobalCallRewriter.Rewrite(program, flow, eventListener);

            foreach (Procedure proc in program.Procedures.Values)
            {
                Aliases alias = new Aliases(proc);
                alias.Transform();
                var          gr  = proc.CreateBlockDominatorGraph();
                SsaTransform sst = new SsaTransform(
                    flow,
                    proc,
                    importResolver,
                    gr,
                    new HashSet <RegisterStorage>());
                ssa = sst.SsaState;
                ssa.Write(writer);
                proc.Write(false, true, writer);
                writer.WriteLine();
                ssa.Validate(s => Assert.Fail(s));
            }
        }
Beispiel #4
0
        private void When_RewriteReturns(SsaState ssa)
        {
            var crw = When_CallRewriterCreated();

            crw.RewriteReturns(ssa);
            ssa.Validate(s => Assert.Fail(s));
        }
Beispiel #5
0
        private void RunTest(SsaState ssa)
        {
            var spbp = new StackPointerBackpropagator(ssa);

            spbp.BackpropagateStackPointer();
            ssa.Validate(s => Assert.Fail(s));
        }
Beispiel #6
0
        protected override void RunTest(Program program, TextWriter fut)
        {
            IImportResolver  importResolver = null;
            var              listener       = new FakeDecompilerEventListener();
            DataFlowAnalysis dfa            = new DataFlowAnalysis(program, importResolver, listener);

            dfa.UntangleProcedures();

            foreach (Procedure proc in program.Procedures.Values)
            {
                Aliases alias = new Aliases(proc);
                alias.Transform();
                SsaTransform sst = new SsaTransform(dfa.ProgramDataFlow, proc, importResolver, proc.CreateBlockDominatorGraph(), new HashSet <RegisterStorage>());
                SsaState     ssa = sst.SsaState;

                ConditionCodeEliminator cce = new ConditionCodeEliminator(ssa, program.Platform);
                cce.Transform();
                DeadCode.Eliminate(proc, ssa);

                ValuePropagator vp = new ValuePropagator(program.SegmentMap, ssa, program.CallGraph, importResolver, listener);
                vp.Transform();
                DeadCode.Eliminate(proc, ssa);
                Coalescer co = new Coalescer(proc, ssa);
                co.Transform();

                ssa.Write(fut);
                proc.Write(false, fut);
                fut.WriteLine();

                ssa.Validate(s => Assert.Fail(s));
            }
        }
Beispiel #7
0
        protected override void RunTest(Program program, TextWriter writer)
        {
            DataFlowAnalysis dfa = new DataFlowAnalysis(program, null, new FakeDecompilerEventListener());

            dfa.UntangleProcedures();
            foreach (Procedure proc in program.Procedures.Values)
            {
                Aliases alias = new Aliases(proc);
                alias.Transform();
                SsaTransform sst = new SsaTransform(
                    dfa.ProgramDataFlow,
                    proc,
                    null,
                    proc.CreateBlockDominatorGraph(),
                    program.Platform.CreateImplicitArgumentRegisters());
                SsaState ssa = sst.SsaState;
                ConditionCodeEliminator cce = new ConditionCodeEliminator(ssa, program.Platform);
                cce.Transform();

                DeadCode.Eliminate(proc, ssa);
                ssa.Write(writer);
                proc.Write(false, writer);

                ssa.Validate(s => Assert.Fail(s));
            }
        }
Beispiel #8
0
        private void RunTest(SsaState ssa)
        {
            var spbp = new StackPointerBackpropagator(ssa, new FakeDecompilerEventListener());

            spbp.BackpropagateStackPointer();
            ssa.Validate(s => Assert.Fail(s));
        }
Beispiel #9
0
        private void RunUnitTest(ProcedureBuilder m, string outfile)
        {
            var proc = m.Procedure;
            var sst  = new SsaTransform(
                new ProgramDataFlow(),
                proc,
                null,
                proc.CreateBlockDominatorGraph(),
                new HashSet <RegisterStorage>());

            ssa = sst.SsaState;
            using (var fut = new FileUnitTester(outfile))
            {
                ssa.Write(fut.TextWriter);
                proc.Write(false, fut.TextWriter);
                fut.AssertFilesEqual();
                ssa.Validate(s => Assert.Fail(s));
            }
        }
Beispiel #10
0
        public void Larw_Add16to32()
        {
            var sExp =
                #region Expected
                @"l1:
	dx_ax_17 = SEQ(dx, ax)
	dx_ax_18 = dx_ax_17 + SEQ(0<16>, Mem0[bx + 2<16>:word16])
	ax_4 = SLICE(dx_ax_18, word16, 0) (alias)
	dx_8 = SLICE(dx_ax_18, word16, 16) (alias)
	SCZ_5 = cond(ax_4)
	C_7 = SLICE(SCZ_5, bool, 2) (alias)
	dx_ax_19 = SEQ(dx_8, ax_4)
	dx_ax_20 = dx_ax_19 + Mem0[bx + 6<16>:ui32]
	ax_9 = SLICE(dx_ax_20, word16, 0) (alias)
	dx_12 = SLICE(dx_ax_20, word16, 16) (alias)
	SCZ_10 = cond(ax_9)
	C_11 = SLICE(SCZ_10, bool, 2) (alias)
	SCZ_13 = cond(dx_12)
	C_14 = SLICE(SCZ_13, bool, 2) (alias)
	S_15 = SLICE(SCZ_13, bool, 0) (alias)
	Z_16 = SLICE(SCZ_13, bool, 1) (alias)
	return
";

            #endregion

            RunTest(sExp, m =>
            {
                m.Assign(ax, m.IAdd(ax, m.Mem16(m.IAdd(bx, 2))));
                m.Assign(SCZ, m.Cond(ax));
                //m.Alias(CF, m.Slice(PrimitiveType.Bool, SCZ, 1));
                m.Assign(dx, m.IAdd(dx, CF));

                m.Assign(ax, m.IAdd(ax, m.Mem16(m.IAdd(bx, 6))));
                m.Assign(SCZ, m.Cond(ax));
                //m.Alias(CF, m.Slice(PrimitiveType.Bool, SCZO, 1));
                m.Assign(dx, m.IAdd(m.IAdd(dx, m.Mem16(m.IAdd(bx, 8))), CF));
                m.Assign(SCZ, m.Cond(dx));
                this.block = m.Block;
                m.Return();
            });
            ssa.Validate(s => Assert.Fail(s));
        }
Beispiel #11
0
        protected override void RunTest(Program program, TextWriter writer)
        {
            var listener = new FakeDecompilerEventListener();
            var sc       = new ServiceContainer();

            sc.AddService <DecompilerEventListener>(listener);
            DataFlowAnalysis dfa = new DataFlowAnalysis(program, dynamicLinker.Object, sc);
            var ssts             = dfa.UntangleProcedures();

            foreach (var sst in ssts)
            {
                SsaState ssa = sst.SsaState;
                ConditionCodeEliminator cce = new ConditionCodeEliminator(program, ssa, listener);
                cce.Transform();

                DeadCode.Eliminate(ssa);
                ssa.Write(writer);
                ssa.Validate(s => writer.WriteLine("*** SSA state invalid: {0}", s));
                ssa.Procedure.Write(false, writer);
            }
        }
Beispiel #12
0
        private void RunUnitTest(ProcedureBuilder m, string outfile)
        {
            var flow          = new ProgramDataFlow();
            var dynamicLinker = new Mock <IDynamicLinker>();

            var proc     = m.Procedure;
            var platform = new FakePlatform(null, m.Architecture)
            {
                Test_CreateTrashedRegisters = () =>
                                              new HashSet <RegisterStorage>()
                {
                    (RegisterStorage)r1.Storage,
                    (RegisterStorage)r2.Storage,
                }
            };
            var program = new Program()
            {
                Architecture = m.Architecture,
                Platform     = platform,
            };
            var sst = new SsaTransform(
                program,
                proc,
                new HashSet <Procedure>(),
                dynamicLinker.Object,
                flow);

            sst.Transform();
            ssa = sst.SsaState;
            using (var fut = new FileUnitTester(outfile))
            {
                ssa.Write(fut.TextWriter);
                proc.Write(false, fut.TextWriter);
                fut.AssertFilesEqual();
                ssa.Validate(s => Assert.Fail(s));
            }
        }
Beispiel #13
0
        protected override void RunTest(Program program, TextWriter writer)
        {
            var flow          = new ProgramDataFlow();
            var dynamicLinker = new Mock <IDynamicLinker>();

            foreach (Procedure proc in program.Procedures.Values)
            {
                var sst = new SsaTransform(
                    program,
                    proc,
                    new HashSet <Procedure>(),
                    dynamicLinker.Object,
                    flow);
                sst.Transform();
                sst.AddUsesToExitBlock();
                sst.RemoveDeadSsaIdentifiers();
                Debug.Print("SsaTest: {0}", new StackFrame(3).GetMethod().Name);
                ssa = sst.SsaState;
                ssa.Write(writer);
                proc.Write(false, true, writer);
                writer.WriteLine();
                ssa.Validate(s => Assert.Fail(s));
            }
        }