コード例 #1
0
        private void Given_ExpressionSimplifier()
        {
            SsaIdentifierCollection ssaIds = BuildSsaIdentifiers();
            var listener = new FakeDecompilerEventListener();

            simplifier = new ExpressionSimplifier(new SsaEvaluationContext(null, ssaIds), listener);
        }
コード例 #2
0
 public void Setup()
 {
     m        = new ProcedureBuilder();
     ssaState = new SsaState(m.Procedure, null);
     ssaIds   = ssaState.Identifiers;
     freg     = new RegisterStorage("flags", 32, 0, PrimitiveType.Word32);
 }
コード例 #3
0
        public void OutpReplacePhi()
        {
            var m    = new ProcedureBuilder();
            var foo  = new Identifier("foo", PrimitiveType.Word32, null);
            var foo1 = new Identifier("foo1", PrimitiveType.Word32, null);
            var foo2 = new Identifier("foo2", PrimitiveType.Word32, null);
            var foo3 = new Identifier("foo3", PrimitiveType.Word32, null);
            var pfoo = new Identifier("pfoo", PrimitiveType.Ptr32, null);

            m.Label("block1");
            m.Assign(foo1, Constant.Word32(1));
            Statement stmFoo1 = m.Block.Statements.Last;

            m.Label("block2");
            m.Assign(foo2, Constant.Word32(2));
            Statement stmFoo2 = m.Block.Statements.Last;

            m.Label("block3");
            Statement stmFoo3 = m.Phi(foo3, foo1, foo2);

            SsaIdentifierCollection ssaIds = new SsaIdentifierCollection();

            ssaIds.Add(foo1, new SsaIdentifier(foo1, foo, stmFoo1, null, false));
            ssaIds.Add(foo2, new SsaIdentifier(foo2, foo, stmFoo2, null, false));
            ssaIds.Add(foo3, new SsaIdentifier(foo3, foo, stmFoo3, null, false));

            OutParameterTransformer opt = new OutParameterTransformer(null, ssaIds);

            opt.ReplaceDefinitionsWithOutParameter(foo3, pfoo);

            Assert.AreEqual("*pfoo = 0x00000001", stmFoo1.Instruction.ToString());
            Assert.AreEqual("*pfoo = 0x00000002", stmFoo2.Instruction.ToString());
            Assert.AreEqual("foo3 = PHI(foo1, foo2)", stmFoo3.Instruction.ToString());
        }
コード例 #4
0
        public void OutpReplaceManyUses()
        {
            ProcedureBuilder m    = new ProcedureBuilder();
            Identifier       foo  = new Identifier("foo", PrimitiveType.Word32, null);
            Identifier       bar  = new Identifier("bar", PrimitiveType.Word32, null);
            Identifier       pfoo = new Identifier("pfoo", PrimitiveType.Ptr32, null);

            Block block = m.Label("block");

            m.Assign(foo, 1);
            Statement stmFoo = m.Block.Statements.Last;

            m.Assign(bar, foo);
            Statement stmBar = m.Block.Statements.Last;

            SsaIdentifier ssaFoo = new SsaIdentifier(foo, foo, stmFoo, ((Assignment)stmFoo.Instruction).Src, false);

            ssaFoo.Uses.Add(stmBar);
            SsaIdentifier ssaBar = new SsaIdentifier(bar, bar, stmBar, ((Assignment)stmBar.Instruction).Src, false);

            SsaIdentifierCollection ssaIds = new SsaIdentifierCollection();

            ssaIds.Add(foo, ssaFoo);
            ssaIds.Add(bar, ssaBar);

            OutParameterTransformer opt = new OutParameterTransformer(m.Procedure, ssaIds);

            opt.ReplaceDefinitionsWithOutParameter(foo, pfoo);
            Assert.AreEqual(3, block.Statements.Count);
            Assert.AreEqual("foo = 0x00000001", block.Statements[0].Instruction.ToString());
            Assert.AreEqual("*pfoo = foo", block.Statements[1].Instruction.ToString());
            Assert.AreEqual("bar = foo", block.Statements[2].Instruction.ToString());
        }
コード例 #5
0
        private void Build(Procedure proc, IProcessorArchitecture arch)
        {
            var platform = new DefaultPlatform(null, arch);

            this.proc = proc;
            Aliases alias = new Aliases(proc, arch);

            alias.Transform();
            var          gr  = proc.CreateBlockDominatorGraph();
            SsaTransform sst = new SsaTransform(new ProgramDataFlow(), proc, gr);
            SsaState     ssa = sst.SsaState;

            this.ssaIds = ssa.Identifiers;

            ConditionCodeEliminator cce = new ConditionCodeEliminator(ssa.Identifiers, platform);

            cce.Transform();
            DeadCode.Eliminate(proc, ssa);

            ValuePropagator vp = new ValuePropagator(arch, ssa.Identifiers, proc);

            vp.Transform();

            Coalescer coa = new Coalescer(proc, ssa);

            coa.Transform();

            DeadCode.Eliminate(proc, ssa);
        }
コード例 #6
0
		public LiveCopyInserter(Procedure proc, SsaIdentifierCollection ssaIds)
		{
			this.proc = proc;
			this.ssaIds = ssaIds;
			this.sla = new SsaLivenessAnalysis(proc, ssaIds);
			this.doms = proc.CreateBlockDominatorGraph();
		}
コード例 #7
0
 public SegmentedAccessClassifier(Procedure proc, SsaIdentifierCollection ssaIds)
 {
     this.proc = proc;
     this.ssaIds = ssaIds;
     assocs = new Dictionary<Identifier,Identifier>();
     consts = new Dictionary<Identifier, Constant>();
 }
コード例 #8
0
        private void RunValuePropagator(ProcedureBuilder m)
        {
            var ssa = new SsaState(m.Procedure, null);

            foreach (var ssaId in ssaIds)
            {
                ssa.Identifiers.Add(ssaId.Identifier, ssaId);
            }
            ssaIds = ssa.Identifiers;
            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;
                    ssaIds[ass.Dst].DefExpression = ass.Src;
                }
                var phiAss = stm.Instruction as PhiAssignment;
                if (phiAss != null)
                {
                    ssaIds[phiAss.Dst].DefStatement  = stm;
                    ssaIds[phiAss.Dst].DefExpression = phiAss.Src;
                }
                ssa.AddUses(stm);
            });
            var vp = new ValuePropagator(arch, ssa, listener);

            vp.Transform();
            ssa.CheckUses(s => Assert.Fail(s));
        }
コード例 #9
0
        private void Prepare(ProcedureBuilder mock)
        {
            proc = mock.Procedure;
            SsaTransform tr = new SsaTransform(new ProgramDataFlow(), proc, proc.CreateBlockDominatorGraph());

            ssaIds = tr.SsaState.Identifiers;
        }
コード例 #10
0
        private void Prepare(Procedure proc)
        {
            this.proc = proc;
            doms      = proc.CreateBlockDominatorGraph();
            SsaTransform sst = new SsaTransform(
                new ProgramDataFlow(),
                proc,
                null,
                doms,
                new HashSet <RegisterStorage>());
            SsaState ssa = sst.SsaState;

            ssaIds = ssa.Identifiers;

            var arch = new FakeArchitecture();
            var cce  = new ConditionCodeEliminator(ssa, new DefaultPlatform(null, arch));

            cce.Transform();

            DeadCode.Eliminate(proc, ssa);

            var vp = new ValuePropagator(arch, ssa.Identifiers, proc);

            vp.Transform();

            DeadCode.Eliminate(proc, ssa);
        }
コード例 #11
0
        public ConditionCodeEliminator(SsaState ssa, IPlatform arch)
		{
            this.ssa=ssa;
			this.ssaIds = ssa.Identifiers;
            this.platform = arch;
            this.m = new ExpressionEmitter();
		}
コード例 #12
0
        private void Prepare(Procedure proc)
        {
            var listener       = new FakeDecompilerEventListener();
            var importResolver = new Mock <IImportResolver>().Object;

            this.proc = proc;
            doms      = proc.CreateBlockDominatorGraph();
            SsaTransform sst = new SsaTransform(
                new ProgramDataFlow(),
                proc,
                importResolver,
                doms,
                new HashSet <RegisterStorage>());
            SsaState ssa = sst.SsaState;

            ssaIds = ssa.Identifiers;

            var arch = new FakeArchitecture();
            var cce  = new ConditionCodeEliminator(ssa, new DefaultPlatform(null, arch));

            cce.Transform();

            DeadCode.Eliminate(proc, ssa);

            var segmentMap = new SegmentMap(Address.Ptr32(0x00123400));
            var vp         = new ValuePropagator(segmentMap, ssa, new CallGraph(), importResolver, listener);

            vp.Transform();

            DeadCode.Eliminate(proc, ssa);
        }
コード例 #13
0
        private void BuildExpressionSimplifier()
        {
            SsaIdentifierCollection ssaIds = BuildSsaIdentifiers();

            table      = new Dictionary <Expression, Expression>();
            simplifier = new ExpressionSimplifier(new SsaEvaluationContext(null, ssaIds));
        }
コード例 #14
0
        public void VpEquality2()
        {
            // Makes sure that
            // y = x - 2
            // if (y == 0) ...
            // doesn't get munged into
            // y = x - 2
            // if (x == 2)

            ProcedureBuilder m = new ProcedureBuilder();
            var ssa            = new SsaState(m.Procedure, null);

            this.ssaIds = ssa.Identifiers;
            Identifier x    = Reg32("x");
            Identifier y    = Reg32("y");
            var        stmX = m.Assign(x, m.LoadDw(Constant.Word32(0x1000300)));

            ssaIds[x].DefStatement = m.Block.Statements.Last;
            var stmY = m.Assign(y, m.ISub(x, 2));

            ssaIds[y].DefStatement = m.Block.Statements.Last;
            var stm = m.BranchIf(m.Eq(y, 0), "test");

            Assert.AreEqual("x = Mem0[0x01000300:word32]", stmX.ToString());
            Assert.AreEqual("y = x - 0x00000002", stmY.ToString());
            Assert.AreEqual("branch y == 0x00000000 test", stm.ToString());

            var vp = new ValuePropagator(arch, ssa, listener);

            vp.Transform(stm);
            Assert.AreEqual("branch x == 0x00000002 test", stm.Instruction.ToString());
        }
コード例 #15
0
		public void Setup()
		{
            m = new ProcedureBuilder();
            ssaState = new SsaState(m.Procedure, null);
            ssaIds = ssaState.Identifiers;
            freg = new FlagRegister("flags", 32, PrimitiveType.Word32);
		}
コード例 #16
0
ファイル: ValuePropagationTests.cs プロジェクト: uxmal/reko
		public void Setup()
		{
			ssaIds = new SsaIdentifierCollection();
            mr = new MockRepository();
            arch = mr.Stub<IProcessorArchitecture>();
            importResolver = mr.Stub<IImportResolver>();
		}
コード例 #17
0
 public void Setup()
 {
     ssaIds         = new SsaIdentifierCollection();
     mr             = new MockRepository();
     arch           = mr.Stub <IProcessorArchitecture>();
     importResolver = mr.Stub <IImportResolver>();
     listener       = new FakeDecompilerEventListener();
 }
コード例 #18
0
        private void Given_ExpressionSimplifier()
        {
            SsaIdentifierCollection ssaIds = BuildSsaIdentifiers();
            var listener   = new FakeDecompilerEventListener();
            var segmentMap = new SegmentMap(Address.Ptr32(0));

            simplifier = new ExpressionSimplifier(segmentMap, new SsaEvaluationContext(null, ssaIds), listener);
        }
コード例 #19
0
 public void Setup()
 {
     m          = new ProcedureBuilder();
     ssaState   = new SsaState(m.Procedure);
     ssaIds     = ssaState.Identifiers;
     freg       = new RegisterStorage("flags", 32, 0, PrimitiveType.Word32);
     segmentMap = new SegmentMap(Address.Ptr32(0));
 }
コード例 #20
0
        private void Build(Procedure proc)
        {
            this.proc = proc;
            this.doms = proc.CreateBlockDominatorGraph();
            SsaTransform sst = new SsaTransform(new ProgramDataFlow(), proc, doms);

            this.ssaIds = sst.SsaState.Identifiers;
        }
コード例 #21
0
        private void Build(Procedure proc)
        {
            this.proc = proc;
            this.doms = proc.CreateBlockDominatorGraph();
            SsaTransform sst = new SsaTransform(new ProgramDataFlow(), proc, doms);

            this.ssaIds = sst.SsaState.Identifiers;
        }
コード例 #22
0
        /// <summary>
        /// Builds a strongly connected component corresponding to:
        /// a1 = 0
        /// a2 = phi(a1, a3)
        /// while (a2 != 10)
        /// {
        ///    a3 = a2 + 4
        /// }
        /// </summary>
        private List <SsaIdentifier> BuildScc()
        {
            var        m = new ProcedureBuilder("test");
            Identifier a = new Identifier("a", PrimitiveType.Word32, null);

            m.Label("b1");
            m.Assign(a, Constant.Word32(0));
            m.Label("b2");
            m.Assign(a, m.IAdd(a, 4));
            m.BranchIf(m.Ne(a, 10), "b2");
            m.Label("b3");
            m.Return();
            this.dom = m.Procedure.CreateBlockDominatorGraph();
            var ssa = new SsaTransform(
                new ProgramDataFlow(),
                m.Procedure,
                null,
                dom,
                new HashSet <RegisterStorage>());

            /*
             *
             * proc = new Procedure("test", new Frame(PrimitiveType.Word32));
             *          Block b1 = proc.AddBlock("b1");
             *          Block b2 = proc.AddBlock("b2");
             *
             *          Identifier a2 = new Identifier("a2", PrimitiveType.Word32, null);
             *          Identifier a3 = new Identifier("a3", PrimitiveType.Word32, null);
             *          PhiFunction phi = new PhiFunction(a1.DataType, new Expression [] { a1, a3 });
             *
             *          Statement stm_a1 = new Statement(0, new Assignment(a1, Constant.Word32(0)), null);
             *          Statement stm_a2 = new Statement(0, new PhiAssignment(a2, new PhiFunction(a1.DataType,  a1, a3 )), null);
             *          Statement stm_ex = new Statement(0, new Branch(new BinaryExpression(Operator.Ne, PrimitiveType.Bool, a2, Constant.Word32(10)), b2), null);
             *          Statement stm_a3 = new Statement(0, new Assignment(a3, new BinaryExpression(Operator.IAdd, a3.DataType, a2, Constant.Word32(4))), null);
             *          b1.Statements.Add(stm_a1);
             *
             *          b2.Statements.Add(stm_a2);
             *          b2.Statements.Add(stm_a3);
             *
             *          SsaIdentifier sid_a1 = new SsaIdentifier(a1, a1, stm_a1, ((Assignment)stm_a1.Instruction).Src, false);
             * SsaIdentifier sid_a2 = new SsaIdentifier(a2, a2, stm_a2, ((PhiAssignment) stm_a2.Instruction).Src, false);
             * SsaIdentifier sid_a3 = new SsaIdentifier(a3, a3, stm_a3, ((Assignment) stm_a3.Instruction).Src, false);
             *          sid_a1.Uses.Add(stm_a2);
             *          ssaIds = new SsaIdentifierCollection();
             *          ssaIds.Add(a1, sid_a1);
             *          ssaIds.Add(a2, sid_a2);
             *          ssaIds.Add(a3, sid_a3);
             */
            ssaIds = ssa.SsaState.Identifiers;
            List <SsaIdentifier> list = new List <SsaIdentifier> {
                ssaIds.Where(i => i.Identifier.Name == "a_0").Single(),
                ssaIds.Where(i => i.Identifier.Name == "a_1").Single(),
                ssaIds.Where(i => i.Identifier.Name == "a_2").Single(),
            };

            return(list);
        }
コード例 #23
0
ファイル: SsaLiveness.cs プロジェクト: gitter-badger/reko
		public SsaLivenessAnalysis(Procedure proc, SsaIdentifierCollection ssaIds)
		{
			this.proc = proc;
			this.ssaIds = ssaIds;
            this.visited = new HashSet<Block>();
            BuildRecords(proc.ControlGraph.Blocks);
			BuildDefinedMap(ssaIds);
			BuildInterferenceGraph(ssaIds);
		}
コード例 #24
0
ファイル: WebBuilder.cs プロジェクト: gitter-badger/reko
 public WebBuilder(Procedure proc, SsaIdentifierCollection ssaIds, Dictionary<Identifier,LinearInductionVariable> ivs)
 {
     this.proc = proc;
     this.ssaIds = ssaIds;
     this.ivs = ivs;
     this.sla = new SsaLivenessAnalysis(proc, ssaIds);
     this.doms = proc.CreateBlockDominatorGraph();
     this.webs = new List<Web>();
 }
コード例 #25
0
        private void Given_ExpressionSimplifier()
        {
            SsaIdentifierCollection ssaIds = BuildSsaIdentifiers();
            var listener       = new FakeDecompilerEventListener();
            var segmentMap     = new SegmentMap(Address.Ptr32(0));
            var importResolver = new Mock <IImportResolver>();
            var ssaCtx         = new SsaEvaluationContext(null, ssaIds, importResolver.Object);

            simplifier = new ExpressionSimplifier(segmentMap, ssaCtx, listener);
        }
コード例 #26
0
        private void Given_ExpressionSimplifier()
        {
            this.ssaIds = BuildSsaIdentifiers();
            var listener      = new FakeDecompilerEventListener();
            var segmentMap    = new SegmentMap(Address.Ptr32(0));
            var dynamicLinker = new Mock <IDynamicLinker>();
            var ssaCtx        = new SsaEvaluationContext(arch?.Object, ssaIds, dynamicLinker.Object);

            simplifier = new ExpressionSimplifier(segmentMap, ssaCtx, listener);
        }
コード例 #27
0
ファイル: Shl_mul_e_RuleTest.cs プロジェクト: feelworld/reko
 public void SetUp()
 {
     m      = new ProcedureBuilder();
     id     = m.Local32("id");
     x      = m.Local32("x");
     ssaIds = new SsaIdentifierCollection();
     foreach (Identifier i in m.Procedure.Frame.Identifiers)
     {
         ssaIds.Add(i, null, null, false);
     }
 }
コード例 #28
0
        private SsaIdentifierCollection BuildSsaIdentifiers()
        {
            var mrFoo = new RegisterStorage("foo", 1, 0, PrimitiveType.Word32);
            var mrBar = new RegisterStorage("bar", 2, 1, PrimitiveType.Word32);
            foo = new Identifier(mrFoo.Name, mrFoo.DataType, mrFoo);

            var coll = new SsaIdentifierCollection();
            var src = Constant.Word32(1);
            foo = coll.Add(foo, new Statement(0, new Assignment(foo, src), null), src, false).Identifier;
            return coll;
        }
コード例 #29
0
		public void SetUp()
		{
			m = new ProcedureBuilder();
			id = m.Local32("id");
			x = m.Local32("x");
			ssaIds = new SsaIdentifierCollection();
			foreach (Identifier i in m.Procedure.Frame.Identifiers)
			{
				ssaIds.Add(i, null, null, false);
			}
		}
コード例 #30
0
        private SsaIdentifierCollection BuildSsaIdentifiers()
        {
            var mrFoo = new RegisterStorage("foo", 1, 0, PrimitiveType.Word32);
            var mrBar = new RegisterStorage("bar", 2, 1, PrimitiveType.Word32);

            foo = new Identifier(mrFoo.Name, mrFoo.DataType, mrFoo);

            var coll = new SsaIdentifierCollection();
            var src  = Constant.Word32(1);

            foo = coll.Add(foo, new Statement(0, new Assignment(foo, src), null), src, false).Identifier;
            return(coll);
        }
コード例 #31
0
 public TrashedRegisterFinder2(
     IProcessorArchitecture arch,
     ProgramDataFlow flow,
     Procedure proc,
     SsaIdentifierCollection ssa,
     DecompilerEventListener listener)
 {
     this.arch = arch;
     this.progFlow = flow;
     this.proc = proc;
     this.ssa = ssa;
     this.decompilerEventListener = listener;
     this.flow = new ProcedureFlow2();
 }
コード例 #32
0
		private void Build(Procedure proc)
		{
			this.proc = proc;
			this.doms = proc.CreateBlockDominatorGraph();

			SsaTransform sst = new SsaTransform(
                new ProgramDataFlow(), 
                proc, 
                null, 
                doms,
                new HashSet<RegisterStorage>());
			
			this.ssaIds = sst.SsaState.Identifiers;
		}
コード例 #33
0
        private void Build(Procedure proc)
        {
            this.proc = proc;
            this.doms = proc.CreateBlockDominatorGraph();

            SsaTransform sst = new SsaTransform(
                new ProgramDataFlow(),
                proc,
                null,
                doms,
                new HashSet <RegisterStorage>());

            this.ssaIds = sst.SsaState.Identifiers;
        }
コード例 #34
0
		public void OutpReplaceSimple()
		{
            var m = new ProcedureBuilder();
            m.Label("block");
			var foo = new Identifier("foo", PrimitiveType.Word32, null);
			var pfoo = new Identifier("pfoo", PrimitiveType.Pointer32, null);
            m.Assign(foo, 3);
			var sid = new SsaIdentifier(foo, foo, m.Block.Statements.Last, null, false);

            var ssaIds = new SsaIdentifierCollection { { foo, sid } };

			var opt = new OutParameterTransformer(null, ssaIds);
			opt.ReplaceDefinitionsWithOutParameter(foo, pfoo);

			Assert.AreEqual("*pfoo = 0x00000003", m.Block.Statements[0].ToString());
		}
コード例 #35
0
ファイル: SsaLiveness.cs プロジェクト: gitter-badger/reko
		public void BuildDefinedMap(SsaIdentifierCollection ssaIds)
		{
			defined = new Dictionary<Statement,List<Identifier>>();
			foreach (SsaIdentifier ssa in ssaIds)
			{
				if (ssa.Uses.Count > 0 && ssa.DefStatement != null)
				{
					List<Identifier> al;
                    if (!defined.TryGetValue(ssa.DefStatement, out al))
					{
						al = new List<Identifier>();
						defined.Add(ssa.DefStatement, al);
					}
					al.Add(ssa.Identifier);
				}
			}
		}
コード例 #36
0
        private void Build(Procedure proc, IProcessorArchitecture arch)
        {
            var platform = new DefaultPlatform(null, arch);
            var program  = new Program()
            {
                Architecture = arch,
                Platform     = platform,
            };

            this.proc = proc;
            var          dynamicLinker = new Mock <IDynamicLinker>().Object;
            var          gr            = proc.CreateBlockDominatorGraph();
            SsaTransform sst           = new SsaTransform(
                program,
                proc,
                new HashSet <Procedure>(),
                null,
                new ProgramDataFlow());

            sst.Transform();
            this.ssa    = sst.SsaState;
            this.ssaIds = ssa.Identifiers;

            var listener = new FakeDecompilerEventListener();
            ConditionCodeEliminator cce = new ConditionCodeEliminator(ssa, platform, listener);

            cce.Transform();
            DeadCode.Eliminate(ssa);

            var             segmentMap = new SegmentMap(Address.Ptr32(0x00400000));
            ValuePropagator vp         = new ValuePropagator(
                segmentMap,
                ssa,
                program.CallGraph,
                null,
                listener);

            vp.Transform();

            Coalescer coa = new Coalescer(ssa);

            coa.Transform();

            DeadCode.Eliminate(ssa);
        }
コード例 #37
0
        public void Liv_CreateIncInitialValue()
        {
            ssaIds = new SsaIdentifierCollection();
            LinearInductionVariableFinder liv = new LinearInductionVariableFinder(null, ssaIds, null);

            liv.Context.InitialValue  = Constant.Word32(0);
            liv.Context.PhiStatement  = new Statement(0, null, null);
            liv.Context.PhiIdentifier = new Identifier("foo_0", PrimitiveType.Word32, null);
            ssaIds.Add(liv.Context.PhiIdentifier, new SsaIdentifier(liv.Context.PhiIdentifier, liv.Context.PhiIdentifier, liv.Context.PhiStatement, null, false));
            liv.Context.DeltaValue     = Constant.Word32(1);
            liv.Context.DeltaStatement = new Statement(0, new Assignment(new Identifier("foo_1", PrimitiveType.Word32, null),
                                                                         new BinaryExpression(Operator.IAdd, PrimitiveType.Word32, liv.Context.PhiIdentifier, liv.Context.DeltaValue)), null);
            ssaIds[liv.Context.PhiIdentifier].Uses.Add(liv.Context.DeltaStatement);

            LinearInductionVariable iv = liv.CreateInductionVariable();

            Assert.AreEqual("(0x00000001 0x00000001 ?)", iv.ToString());
        }
コード例 #38
0
        public void Liv_CreateBareMinimum()
        {
            ssaIds = new SsaIdentifierCollection();
            Identifier id0 = new Identifier("foo", PrimitiveType.Word32, new TemporaryStorage("foo", 1, PrimitiveType.Word32));
            Identifier id1 = new Identifier("bar", PrimitiveType.Word32, new TemporaryStorage("bar", 1, PrimitiveType.Word32));
            Identifier phi = new Identifier("i_3", PrimitiveType.Word32, null);

            ssaIds.Add(id0, new SsaIdentifier(id0, id0, null, null, false));
            ssaIds.Add(id1, new SsaIdentifier(id1, id1, null, null, false));
            ssaIds.Add(phi, new SsaIdentifier(phi, phi, null, null, false));
            LinearInductionVariableFinder liv = new LinearInductionVariableFinder(null, ssaIds, null);

            liv.Context.PhiStatement  = new Statement(0, null, null);
            liv.Context.PhiIdentifier = phi;
            liv.Context.DeltaValue    = Constant.Word32(1);
            LinearInductionVariable iv = liv.CreateInductionVariable();

            Assert.AreEqual("(? 0x00000001 ?)", iv.ToString());
        }
コード例 #39
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());
        }
コード例 #40
0
        public void OutpReplaceSimple()
        {
            var m     = new ProcedureBuilder();
            var block = m.Label("block");
            var foo   = new Identifier("foo", PrimitiveType.Word32, null);
            var pfoo  = new Identifier("pfoo", PrimitiveType.Pointer32, null);

            m.Assign(foo, 3);
            var sid = new SsaIdentifier(foo, foo, m.Block.Statements.Last, null, false);

            var ssaIds = new SsaIdentifierCollection {
                { foo, sid }
            };

            var opt = new OutParameterTransformer(null, ssaIds);

            opt.ReplaceDefinitionsWithOutParameter(foo, pfoo);

            Assert.AreEqual("*pfoo = 0x00000003", m.Block.Statements[0].ToString());
        }
コード例 #41
0
        private void Build(Procedure proc, IProcessorArchitecture arch)
        {
            var platform = new DefaultPlatform(null, arch);

            this.proc = proc;
            var     importResolver = new Mock <IImportResolver>().Object;
            Aliases alias          = new Aliases(proc);

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

            this.ssaIds = ssa.Identifiers;

            ConditionCodeEliminator cce = new ConditionCodeEliminator(ssa, platform);

            cce.Transform();
            DeadCode.Eliminate(proc, ssa);

            var             segmentMap = new SegmentMap(Address.Ptr32(0x00400000));
            ValuePropagator vp         = new ValuePropagator(
                segmentMap,
                ssa,
                new CallGraph(),
                null,
                new FakeDecompilerEventListener());

            vp.Transform();

            Coalescer coa = new Coalescer(proc, ssa);

            coa.Transform();

            DeadCode.Eliminate(proc, ssa);
        }
コード例 #42
0
ファイル: ValueNumbering.cs プロジェクト: nemerle/reko
        public ValueNumbering(SsaIdentifierCollection ssaIds)
        {
            this.ssaIds = ssaIds;
            optimistic = new Dictionary<Expression, Expression>();
            valid = new Dictionary<Expression,Expression>();
            stack = new Stack<Node>();

            // Set initial value numbers for all nodes (SSA identifiers).
            // Value numbers for the original values at procedure entry are just the
            // SSA identifiers, while any other values are undefined.

            AssignInitialValueNumbers();

            // Walk the SCC's of the node graph using Tarjan's algorithm.

            iDFS = 0;
            foreach (var id in nodes.Keys)
            {
                DFS(id);
            }
        }
コード例 #43
0
        public void VpCopyPropagate()
        {
            var ssa = new SsaState(new Procedure("foo", new Frame(PrimitiveType.Pointer32)), null);

            ssaIds = ssa.Identifiers;
            Identifier x    = Reg32("x");
            Identifier y    = Reg32("y");
            Identifier z    = Reg32("z");
            Identifier w    = Reg32("w");
            Statement  stmX = new Statement(0, new Assignment(x, new MemoryAccess(MemoryIdentifier.GlobalMemory, Constant.Word32(0x10004000), PrimitiveType.Word32)), null);
            Statement  stmY = new Statement(1, new Assignment(y, x), null);
            Statement  stmZ = new Statement(2, new Assignment(z, new BinaryExpression(Operator.IAdd, PrimitiveType.Word32, y, Constant.Word32(2))), null);
            Statement  stmW = new Statement(3, new Assignment(w, y), null);

            ssaIds[x].DefStatement = stmX;
            ssaIds[y].DefStatement = stmY;
            ssaIds[z].DefStatement = stmZ;
            ssaIds[w].DefStatement = stmW;
            ssaIds[x].Uses.Add(stmY);
            ssaIds[y].Uses.Add(stmZ);
            ssaIds[y].Uses.Add(stmW);
            Assert.AreEqual("x = Mem0[0x10004000:word32]", stmX.Instruction.ToString());
            Assert.AreEqual("y = x", stmY.Instruction.ToString());
            Assert.AreEqual("z = y + 0x00000002", stmZ.Instruction.ToString());
            Assert.AreEqual("w = y", stmW.Instruction.ToString());

            ValuePropagator vp = new ValuePropagator(arch, ssa, listener);

            vp.Transform(stmX);
            vp.Transform(stmY);
            vp.Transform(stmZ);
            vp.Transform(stmW);

            Assert.AreEqual("x = Mem0[0x10004000:word32]", stmX.Instruction.ToString());
            Assert.AreEqual("y = x", stmY.Instruction.ToString());
            Assert.AreEqual("z = x + 0x00000002", stmZ.Instruction.ToString());
            Assert.AreEqual("w = x", stmW.Instruction.ToString());
            Assert.AreEqual(3, ssaIds[x].Uses.Count);
            Assert.AreEqual(0, ssaIds[y].Uses.Count);
        }
コード例 #44
0
        private void Prepare(Procedure proc)
        {
            this.proc = proc;
            doms      = proc.CreateBlockDominatorGraph();
            SsaTransform sst = new SsaTransform(new ProgramDataFlow(), proc, doms);
            SsaState     ssa = sst.SsaState;

            ssaIds = ssa.Identifiers;

            var cce = new ConditionCodeEliminator(ssaIds, new DefaultPlatform(null, new FakeArchitecture()));

            cce.Transform();

            DeadCode.Eliminate(proc, ssa);

            var vp = new ValuePropagator(ssa.Identifiers, proc);

            vp.Transform();

            DeadCode.Eliminate(proc, ssa);
            proc.Dump(true); //$DEBUG
        }
コード例 #45
0
ファイル: LiveCopyInserterTests.cs プロジェクト: xor2003/reko
        private void Build(Procedure proc, IProcessorArchitecture arch)
        {
            var platform = new DefaultPlatform(null, arch);

            this.proc = proc;
            var importResolver = MockRepository.GenerateStub <IImportResolver>();

            importResolver.Replay();
            Aliases alias = new Aliases(proc, arch);

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

            this.ssaIds = ssa.Identifiers;

            ConditionCodeEliminator cce = new ConditionCodeEliminator(ssa, platform);

            cce.Transform();
            DeadCode.Eliminate(proc, ssa);

            ValuePropagator vp = new ValuePropagator(arch, ssa.Identifiers, proc);

            vp.Transform();

            Coalescer coa = new Coalescer(proc, ssa);

            coa.Transform();

            DeadCode.Eliminate(proc, ssa);
        }
コード例 #46
0
        public void OutpReplaceManyUses()
        {
            ProcedureBuilder m = new ProcedureBuilder();
            Identifier foo = new Identifier("foo", PrimitiveType.Word32, null);
            Identifier bar = new Identifier("bar", PrimitiveType.Word32, null);
            Identifier pfoo = new Identifier("pfoo", PrimitiveType.Pointer32, null);

            Block block = m.Label("block");
            m.Assign(foo, 1);
            Statement stmFoo = m.Block.Statements.Last;
            m.Assign(bar, foo);
            Statement stmBar = m.Block.Statements.Last;

            SsaIdentifier ssaFoo = new SsaIdentifier(foo, foo, stmFoo, ((Assignment) stmFoo.Instruction).Src, false);
            ssaFoo.Uses.Add(stmBar);
            SsaIdentifier ssaBar = new SsaIdentifier(bar, bar, stmBar, ((Assignment) stmBar.Instruction).Src, false);

            SsaIdentifierCollection ssaIds = new SsaIdentifierCollection();
            ssaIds.Add(foo, ssaFoo);
            ssaIds.Add(bar, ssaBar);

            OutParameterTransformer opt = new OutParameterTransformer(m.Procedure, ssaIds);
            opt.ReplaceDefinitionsWithOutParameter(foo, pfoo);
            Assert.AreEqual(3, block.Statements.Count);
            Assert.AreEqual("foo = 0x00000001", block.Statements[0].Instruction.ToString());
            Assert.AreEqual("*pfoo = foo", block.Statements[1].Instruction.ToString());
            Assert.AreEqual("bar = foo", block.Statements[2].Instruction.ToString());
        }
コード例 #47
0
ファイル: IdConstantTests.cs プロジェクト: gh0std4ncer/reko
		public void Setup()
		{
			m = new ProcedureBuilder();
            ssa = new SsaIdentifierCollection();
		}
コード例 #48
0
        public void OutpReplacePhi()
        {
            var m = new ProcedureBuilder();
            var foo = new Identifier("foo", PrimitiveType.Word32, null);
            var foo1 = new Identifier("foo1", PrimitiveType.Word32, null);
            var foo2 = new Identifier("foo2", PrimitiveType.Word32, null);
            var foo3 = new Identifier("foo3", PrimitiveType.Word32, null);
            var pfoo = new Identifier("pfoo", PrimitiveType.Pointer32, null);

            Block block1 = m.Label("block1");
             m.Assign(foo1, Constant.Word32(1));
             Statement stmFoo1 = m.Block.Statements.Last;
            Block block2 = m.Label("block2");
            m.Assign(foo2, Constant.Word32(2));
            Statement stmFoo2 = m.Block.Statements.Last;
            Block block3 = m.Label("block3");
            Statement stmFoo3 = m.Phi(foo3, foo1, foo2);

            SsaIdentifierCollection ssaIds = new SsaIdentifierCollection();
            ssaIds.Add(foo1, new SsaIdentifier(foo1, foo, stmFoo1, null, false));
            ssaIds.Add(foo2, new SsaIdentifier(foo2, foo, stmFoo2, null, false));
            ssaIds.Add(foo3, new SsaIdentifier(foo3, foo, stmFoo3, null, false));

            OutParameterTransformer opt = new OutParameterTransformer(null, ssaIds);
            opt.ReplaceDefinitionsWithOutParameter(foo3, pfoo);

            Assert.AreEqual("*pfoo = 0x00000001", stmFoo1.Instruction.ToString());
            Assert.AreEqual("*pfoo = 0x00000002", stmFoo2.Instruction.ToString());
            Assert.AreEqual("foo3 = PHI(foo1, foo2)", stmFoo3.Instruction.ToString());
        }
コード例 #49
0
ファイル: SsaLiveness.cs プロジェクト: gitter-badger/reko
		public void BuildInterferenceGraph(SsaIdentifierCollection ssaIds)
		{
			interference = new InterferenceGraph();
			foreach (SsaIdentifier v in ssaIds)
			{
				visited = new HashSet<Block>();
				foreach (Statement s in v.Uses)
				{
					PhiFunction phi = GetPhiFunction(s);
					if (phi != null)
					{
						int i = Array.IndexOf(phi.Arguments, v.Identifier);
						Block p = s.Block.Pred[i];
						LiveOutAtBlock(p, v);
					}
					else
					{
						int i = s.Block.Statements.IndexOf(s);
						LiveInAtStatement(s.Block, i, v);
					}
				}
			}
		}
コード例 #50
0
ファイル: ValuePropagationTests.cs プロジェクト: uxmal/reko
 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);
     vp.Transform();
     Assert.AreEqual("x = fp - 0x000C", phiStm.Instruction.ToString());
 }
コード例 #51
0
ファイル: ValuePropagationTests.cs プロジェクト: uxmal/reko
		public void VpEquality2()
		{
            // Makes sure that 
            // y = x - 2
            // if (y == 0) ...
            // doesn't get munged into
            // y = x - 2
            // if (x == 2)

            ProcedureBuilder m = new ProcedureBuilder();
            var ssa = new SsaState(m.Procedure, null);
            this.ssaIds = ssa.Identifiers;
            Identifier x = Reg32("x");
			Identifier y = Reg32("y");
            var stmX = m.Assign(x, m.LoadDw(Constant.Word32(0x1000300)));
			ssaIds[x].DefStatement = m.Block.Statements.Last;
            var stmY = m.Assign(y, m.ISub(x, 2));
			ssaIds[y].DefStatement = m.Block.Statements.Last;
			var stm = m.BranchIf(m.Eq(y, 0), "test");
			Assert.AreEqual("x = Mem0[0x01000300:word32]", stmX.ToString());
			Assert.AreEqual("y = x - 0x00000002", stmY.ToString());
			Assert.AreEqual("branch y == 0x00000000 test", stm.ToString());

			var vp = new ValuePropagator(arch, ssa);
			vp.Transform(stm);
			Assert.AreEqual("branch x == 0x00000002 test", stm.Instruction.ToString());
		}
コード例 #52
0
ファイル: SsaLiveness.cs プロジェクト: gitter-badger/reko
		public SsaLivenessAnalysis2(Procedure proc, SsaIdentifierCollection ssa)
		{
			this.ssa = ssa;
            visitedBlocks = new Dictionary<Block, Block>();
			interference = new InterferenceGraph();
		}
コード例 #53
0
		public OutParameterTransformer(Procedure proc, SsaIdentifierCollection ssaIds)
		{
			this.proc = proc;
			this.ssaIds = ssaIds;
		}
コード例 #54
0
		public void Setup()
		{
			ssaIds = new SsaIdentifierCollection();
            freg = new FlagRegister("flags", PrimitiveType.Word32);
		}
コード例 #55
0
ファイル: SsaState.cs プロジェクト: gitter-badger/reko
		public SsaState(Procedure proc, DominatorGraph<Block> domGraph)
		{
			this.Procedure = proc;
            this.DomGraph = domGraph;
			this.ids = new SsaIdentifierCollection();
		}
コード例 #56
0
ファイル: DeclarationInserter.cs プロジェクト: relaxar/reko
		public DeclarationInserter(SsaIdentifierCollection ssaIds, BlockDominatorGraph doms)
		{
			this.doms = doms;
		}
コード例 #57
0
ファイル: Coalescer.cs プロジェクト: killbug2004/reko
 public UsedIdentifierAdjuster(Statement def, SsaIdentifierCollection ssaIds, Statement use)
 {
     this.def = def;
     this.use = use;
     this.ssaIds = ssaIds;
 }
コード例 #58
0
 public void Setup()
 {
     sids = new SsaIdentifierCollection();
 }
コード例 #59
0
		public ConditionCodeEliminator(SsaIdentifierCollection ssaIds, IPlatform arch)
		{
			this.ssaIds = ssaIds;
            this.platform = arch;
		}
コード例 #60
0
ファイル: ValuePropagationTests.cs プロジェクト: uxmal/reko
		public void VpCopyPropagate()
		{
            var ssa = new SsaState(new Procedure("foo", new Frame(PrimitiveType.Pointer32)), null);
            ssaIds = ssa.Identifiers;
			Identifier x = Reg32("x");
			Identifier y = Reg32("y");
			Identifier z = Reg32("z");
			Identifier w = Reg32("w");
			Statement stmX = new Statement(0, new Assignment(x, new MemoryAccess(MemoryIdentifier.GlobalMemory, Constant.Word32(0x10004000), PrimitiveType.Word32)), null);
			Statement stmY = new Statement(1, new Assignment(y, x), null);
			Statement stmZ = new Statement(2, new Assignment(z, new BinaryExpression(Operator.IAdd, PrimitiveType.Word32, y, Constant.Word32(2))), null);
			Statement stmW = new Statement(3, new Assignment(w, y), null);
			ssaIds[x].DefStatement = stmX;
			ssaIds[y].DefStatement = stmY;
			ssaIds[z].DefStatement = stmZ;
			ssaIds[w].DefStatement = stmW;
			ssaIds[x].Uses.Add(stmY);
			ssaIds[y].Uses.Add(stmZ);
			ssaIds[y].Uses.Add(stmW);
			Assert.AreEqual("x = Mem0[0x10004000:word32]", stmX.Instruction.ToString());
			Assert.AreEqual("y = x", stmY.Instruction.ToString());
			Assert.AreEqual("z = y + 0x00000002", stmZ.Instruction.ToString());
			Assert.AreEqual("w = y", stmW.Instruction.ToString());

			ValuePropagator vp = new ValuePropagator(arch, ssa);
			vp.Transform(stmX);
			vp.Transform(stmY);
			vp.Transform(stmZ);
			vp.Transform(stmW);

			Assert.AreEqual("x = Mem0[0x10004000:word32]", stmX.Instruction.ToString());
			Assert.AreEqual("y = x", stmY.Instruction.ToString());
			Assert.AreEqual("z = x + 0x00000002", stmZ.Instruction.ToString());
			Assert.AreEqual("w = x", stmW.Instruction.ToString());
			Assert.AreEqual(3, ssaIds[x].Uses.Count);
			Assert.AreEqual(0, ssaIds[y].Uses.Count);
		}