public void Tmer_PointerToSingleItem()
		{
			var ptr = new Identifier("ptr", PrimitiveType.Word32, null);
			var tv = store.EnsureExpressionTypeVariable(factory, ptr);
			tv.OriginalDataType = new Pointer(point, 4);
			var eq = new EquivalenceClass(tv);
			eq.DataType = point;
			tv.DataType = new Pointer(eq, 4);

			TypedExpressionRewriter tmer = new TypedExpressionRewriter(program);
            var access = Wrap(new MemoryAccess(ptr, PrimitiveType.Word32));
            TypeVariable tvAccess = access.TypeVariable;
            tvAccess.DataType = PrimitiveType.Word32;
            Expression e = access.Accept(tmer);
			Assert.AreEqual("ptr->dw0000", e.ToString());
		}
        protected override void RunTest(Program program, string outputFile)
        {
            using (FileUnitTester fut = new FileUnitTester(outputFile))
            {
                fut.TextWriter.WriteLine("// Before ///////");
                DumpProgram(program, fut);

                SetupPreStages(program);
                aen.Transform(program);
                eqb.Build(program);
#if OLD
                coll = new TraitCollector(factory, store, dtb, program);
                coll.CollectProgramTraits(program);
#else
                var coll = new TypeCollector(program.TypeFactory, program.TypeStore, program);
                coll.CollectTypes();
#endif
                program.TypeStore.BuildEquivalenceClassDataTypes(program.TypeFactory);
                program.TypeStore.Dump();
                tvr.ReplaceTypeVariables();
                trans.Transform();
                ctn.RenameAllTypes(program.TypeStore);

                ter = new TypedExpressionRewriter(program);
                try
                {
                    ter.RewriteProgram(program);
                }
                catch (Exception ex)
                {
                    fut.TextWriter.WriteLine("** Exception **");
                    fut.TextWriter.WriteLine(ex);
                }
                finally
                {
                    fut.TextWriter.WriteLine("// After ///////");
                    DumpProgAndStore(program, fut);
                }
            }
        }
Exemplo n.º 3
0
		/// <summary>
		/// Performs type analysis and rewrites program based on the inferred information.
		/// </summary>
		/// <remarks>
		/// For instance, all MemoryAccesses will be converted to structure field
		/// accesses or array accesses as appropriate.
		/// </remarks>
		/// <param name="prog"></param>
		public void RewriteProgram(Program prog)
		{
            factory = prog.TypeFactory;
            store = prog.TypeStore;

            aen = new ExpressionNormalizer(prog.Platform.PointerType);
            eqb = new EquivalenceClassBuilder(factory, store);
            dtb = new DataTypeBuilder(factory, store, prog.Platform);
            trco = new TraitCollector(factory, store, dtb, prog);
            //dpa = new DerivedPointerAnalysis(factory, store, prog.Architecture);
            tvr = new TypeVariableReplacer(store);
            trans = new TypeTransformer(factory, store,prog, eventListener);
            ctn = new ComplexTypeNamer();
            ter = new TypedExpressionRewriter(prog);

            // RestrictProcedures(prog, 0, 1, true); //$DEBUG
            eventListener.ShowStatus("Gathering primitive datatypes from instructions.");
			aen.Transform(prog);
			eqb.Build(prog);
            eventListener.ShowStatus("Collecting datatype usage traits.");
			trco.CollectProgramTraits(prog);
            eventListener.ShowStatus("Building equivalence classes.");
			dtb.BuildEquivalenceClassDataTypes();
            //dpa.FollowConstantPointers(prog);
			tvr.ReplaceTypeVariables();

            eventListener.ShowStatus("Transforming datatypes.");
			var ppr = new PtrPrimitiveReplacer(factory, store, prog);
			ppr.ReplaceAll();

			trans.Transform();
			ctn.RenameAllTypes(store);
			store.Dump();
            eventListener.ShowStatus("Rewriting expressions.");
			ter.RewriteProgram(prog);
		}
 public void TerConstants()
 {
     Program prog = new Program();
     prog.Architecture = new FakeArchitecture();
     prog.Platform = new DefaultPlatform(null, prog.Architecture);
     SetupPreStages(prog);
     Constant r = Constant.Real32(3.0F);
     Constant i = Constant.Int32(1);
     Identifier x = new Identifier("x", PrimitiveType.Word32, null);
     Assignment ass = new Assignment(x, r);
     TypeVariable tvR = r.TypeVariable = prog.TypeFactory.CreateTypeVariable();
     TypeVariable tvI = i.TypeVariable = prog.TypeFactory.CreateTypeVariable();
     TypeVariable tvX = x.TypeVariable = prog.TypeFactory.CreateTypeVariable();
     prog.TypeStore.TypeVariables.AddRange(new TypeVariable[] { tvR, tvI, tvX });
     UnionType u = prog.TypeFactory.CreateUnionType(null, null, new DataType[] { r.DataType, i.DataType });
     tvR.OriginalDataType = r.DataType;
     tvI.OriginalDataType = i.DataType;
     tvX.OriginalDataType = x.DataType;
     tvR.DataType = u;
     tvI.DataType = u;
     tvX.DataType = u;
     ctn.RenameAllTypes(prog.TypeStore);
     TypedExpressionRewriter ter = new TypedExpressionRewriter(prog);
     Instruction instr = ter.TransformAssignment(ass);
     Assert.AreEqual("x.u1 = 3F;", instr.ToString());
 }
        public void TerComplex()
        {
            Program program = new Program();
            program.Architecture = new FakeArchitecture();
            program.Platform = new DefaultPlatform(null, program.Architecture);
            SetupPreStages(program);
            Identifier id = new Identifier("v0", PrimitiveType.Word32, null);
            Expression cmp = MemLoad(id, 4, PrimitiveType.Word32);

            program.Globals.Accept(eqb);
            cmp.Accept(aen);
            cmp.Accept(eqb);
            coll = new TraitCollector(program.TypeFactory, program.TypeStore, dtb, program);
            cmp.Accept(coll);
            dtb.BuildEquivalenceClassDataTypes();

            tvr.ReplaceTypeVariables();
            trans.Transform();
            ctn.RenameAllTypes(program.TypeStore);

            ter = new TypedExpressionRewriter(program);
            cmp = cmp.Accept(ter);
            Assert.AreEqual("v0->dw0004", cmp.ToString());
        }
		public void Tmer_PointerToSecondItemOfPoint()
		{
			Identifier ptr = new Identifier("ptr", PrimitiveType.Word32, null);
			store.EnsureExpressionTypeVariable(factory, ptr);
			EquivalenceClass eqPtr = new EquivalenceClass(ptr.TypeVariable);
			eqPtr.DataType = point;
			ptr.TypeVariable.OriginalDataType = new Pointer(point, 4);
			ptr.TypeVariable.DataType = new Pointer(eqPtr, 4);

			var c = Wrap(Constant.Word32(4));
			var bin = Wrap(new BinaryExpression(BinaryOperator.IAdd, PrimitiveType.Word32, ptr, c));
            var mem = Wrap(new MemoryAccess(bin, PrimitiveType.Word32));
			var tmer = new TypedExpressionRewriter(program);
			Expression e = mem.Accept(tmer);
			Assert.AreEqual("ptr->dw0004", e.ToString());
		}