public TranslationUnitItemStore() { _cursorStore = new CursorStore(this); _fileStore = new FileStore(); _locationStore = new SourceLocationStore(this); _sourceRangeStore = new SourceRangeStore(this); _typeStore = new TypeStore(this); }
public void Setup() { this.m = new ExpressionEmitter(); this.store = new TypeStore(); this.factory = new TypeFactory(); var arch = new FakeArchitecture(); var platform = new DefaultPlatform(null, arch); program = new Program { Architecture = arch, Platform = platform }; this.exa = new ExpressionTypeAscender(program, store, factory); }
public void Setup() { program = new Program(); program.Architecture = new FakeArchitecture(); program.Platform = new DefaultPlatform(null, program.Architecture); factory = program.TypeFactory; store = program.TypeStore; handler = new TestTraitHandler(store); eqb = new EquivalenceClassBuilder(factory, store); store.EnsureExpressionTypeVariable(factory, program.Globals); atrco = new AddressTraitCollector(factory, store, handler, program); m = new ProcedureBuilder(); }
public void NameStructure() { var store = new TypeStore(); var factory = new TypeFactory(); var tv1 = store.CreateTypeVariable(factory); tv1.Class.DataType = new StructureType(null, 0); tv1.DataType = tv1.Class.DataType; var ctn = new ComplexTypeNamer(); ctn.RenameAllTypes(store); Assert.AreEqual("Eq_1", tv1.Class.DataType.Name); }
public void Setup() { this.m = new ExpressionEmitter(); this.store = new TypeStore(); this.factory = new TypeFactory(); this.arch = new FakeArchitecture(); var prog = new Program { Architecture = arch, Platform = new DefaultPlatform(null, arch) }; this.exa = new ExpressionTypeAscender(prog, store, factory); this.exd = new ExpressionTypeDescender(prog, store, factory); store.EnsureExpressionTypeVariable(factory, prog.Globals, "globals_t"); }
public void EqbSimpleEquivalence() { TypeFactory factory = new TypeFactory(); TypeStore store = new TypeStore(); EquivalenceClassBuilder eqb = new EquivalenceClassBuilder(factory, store, listener); Identifier id1 = new Identifier("id2", PrimitiveType.Word32, null); Identifier id2 = new Identifier("id2", PrimitiveType.Word32, null); Assignment ass = new Assignment(id1, id2); ass.Accept(eqb); Assert.IsNotNull(id1); Assert.IsNotNull(id2); Assert.AreEqual(2, id1.TypeVariable.Number, "id1 type number"); Assert.AreEqual(2, id1.TypeVariable.Number, "id2 type number"); Assert.AreEqual(id1.TypeVariable.Class, id2.TypeVariable.Class); }
public void RenameAllTypes(TypeStore store) { foreach (EquivalenceClass e in store.UsedEquivalenceClasses) { eq = e; if (eq.DataType != null) { eq.DataType.Accept(this); } } eq = null; foreach (TypeVariable tv in store.TypeVariables) { tv.DataType.Accept(this); } }
public static bool ReplaceAll(TypeFactory factory, TypeStore store) { EquivalenceClass [] eqs = new EquivalenceClass[store.UsedEquivalenceClasses.Count]; store.UsedEquivalenceClasses.CopyTo(eqs, 0); bool changed = false; for (int i = 0; i < eqs.Length; ++i) { if (eqs[i].DataType != null) { NestedComplexTypeExtractor nctr = new NestedComplexTypeExtractor(factory, store); eqs[i].DataType = eqs[i].DataType.Accept(nctr); changed |= nctr.Changed; } } return(changed); }
public void Setup() { program = new Program(); program.Architecture = new FakeArchitecture(new ServiceContainer()); program.Platform = new DefaultPlatform(program.Architecture.Services, program.Architecture); store = program.TypeStore; factory = program.TypeFactory; globals = program.Globals; StructureType point = new StructureType("Point", 0) { Fields = { { 0, PrimitiveType.Word32, null }, { 4, PrimitiveType.Word32, null } } }; TypeVariable tvPoint = store.CreateTypeVariable(factory); EquivalenceClass eq = new EquivalenceClass(tvPoint) { DataType = point }; tvPoint.DataType = eq; ptrPoint = new Pointer(eq, 32); UnionType u = new UnionType("RealInt", null) { Alternatives = { new UnionAlternative("w", PrimitiveType.Word32, 0), new UnionAlternative("r", PrimitiveType.Real32, 1), } }; TypeVariable tvUnion = store.CreateTypeVariable(factory); eq = new EquivalenceClass(tvUnion) { DataType = u }; tvUnion.DataType = eq; ptrUnion = new Pointer(eq, 32); ptrInt = new Pointer(PrimitiveType.Int32, 32); ptrWord = new Pointer(PrimitiveType.Word32, 32); m = new ExpressionEmitter(); }
public TypeCollector( TypeFactory factory, TypeStore store, Program program, DecompilerEventListener eventListener) { this.factory = factory; this.store = store; this.program = program; if (eventListener == null) { throw new ArgumentNullException("eventListener"); } this.eventListener = eventListener; this.asc = new ExpressionTypeAscender(program, store, factory); this.desc = new ExpressionTypeDescender(program, store, factory); }
/// <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="program"></param> public void RewriteProgram(Program program) { factory = program.TypeFactory; store = program.TypeStore; aen = new ExpressionNormalizer(program.Platform.PointerType); eqb = new EquivalenceClassBuilder(factory, store); #if OLD dtb = new DataTypeBuilder(factory, store, program.Platform); trco = new TraitCollector(factory, store, dtb, program); #else tyco = new TypeCollector(program.TypeFactory, program.TypeStore, program); #endif //dpa = new DerivedPointerAnalysis(factory, store, program.Architecture); tvr = new TypeVariableReplacer(store); trans = new TypeTransformer(factory, store, program, eventListener); ctn = new ComplexTypeNamer(); ter = new TypedExpressionRewriter(program); // RestrictProcedures(program, 0, 1, true); // Re-enable this for debugging eventListener.ShowStatus("Gathering primitive datatypes from instructions."); aen.Transform(program); eqb.Build(program); #if OLD eventListener.ShowStatus("Collecting datatype usage traits."); trco.CollectProgramTraits(program); eventListener.ShowStatus("Building equivalence classes."); dtb.BuildEquivalenceClassDataTypes(); #else eventListener.ShowStatus("Collecting data types"); tyco.CollectTypes(); store.BuildEquivalenceClassDataTypes(factory); #endif //dpa.FollowConstantPointers(prog); tvr.ReplaceTypeVariables(); eventListener.ShowStatus("Transforming datatypes."); var ppr = new PtrPrimitiveReplacer(factory, store, program); ppr.ReplaceAll(); trans.Transform(); ctn.RenameAllTypes(store); store.Dump(); eventListener.ShowStatus("Rewriting expressions."); ter.RewriteProgram(program); }
public void Setup() { var image = new LoadedImage(Address.Ptr32(0x00400000), new byte[1024]); var arch = new FakeArchitecture(); program = new Program { Architecture = arch, Image = image, ImageMap = image.CreateImageMap(), Platform = new DefaultPlatform(null, arch) }; store = program.TypeStore; factory = program.TypeFactory; point = new StructureType(null, 0); point.Fields.Add(0, PrimitiveType.Word32, null); point.Fields.Add(4, PrimitiveType.Word32, null); }
public TypedConstantRewriter(Program program, DecompilerEventListener eventListener) { this.eventListener = eventListener; this.program = program; this.platform = program.Platform; this.store = program.TypeStore; this.globals = program.Globals; if (program.SegmentMap != null) { this.mpSelectorToSegId = program.SegmentMap.Segments.Values .Where(s => s.Identifier != null && s.Address.Selector.HasValue) .ToDictionary(s => s.Address.Selector.Value, s => s.Identifier); } else { this.mpSelectorToSegId = new Dictionary <ushort, Identifier>(); } }
public void PprRecursiveStructs() { StructureType s1 = new StructureType(null, 0, true); StructureType s2 = new StructureType(null, 0, true); s1.Fields.Add(0, new Pointer(s2, 4)); s2.Fields.Add(0, new Pointer(s1, 4)); var program = new Program(); var factory = new TypeFactory(); var store = new TypeStore(); var ppr = new PtrPrimitiveReplacer(factory, store, program); var sExp = "(struct (0 (ptr (struct (0 (ptr (struct)) ptr0000))) ptr0000))"; Assert.AreEqual(sExp, ppr.Replace(s1).ToString()); Assert.AreEqual(sExp, ppr.Replace(s2).ToString()); }
public void Setup() { store = new TypeStore(); factory = new TypeFactory(); globals = new Identifier("globals", PrimitiveType.Word32, null); StructureType point = new StructureType("Point", 0) { Fields = { { 0, PrimitiveType.Word32, null }, { 4, PrimitiveType.Word32, null } } }; TypeVariable tvPoint = store.CreateTypeVariable(factory); EquivalenceClass eq = new EquivalenceClass(tvPoint) { DataType = point }; tvPoint.DataType = eq; ptrPoint = new Pointer(eq, 32); UnionType u = new UnionType("RealInt", null) { Alternatives = { new UnionAlternative("w", PrimitiveType.Word32, 0), new UnionAlternative("r", PrimitiveType.Real32, 1), } }; TypeVariable tvUnion = store.CreateTypeVariable(factory); eq = new EquivalenceClass(tvUnion) { DataType = u }; tvUnion.DataType = eq; ptrUnion = new Pointer(eq, 32); ptrInt = new Pointer(PrimitiveType.Int32, 32); ptrWord = new Pointer(PrimitiveType.Word32, 32); m = new ExpressionEmitter(); }
public void Setup() { var mem = new MemoryArea(Address.Ptr32(0x00400000), new byte[1024]); var arch = new FakeArchitecture(); program = new Program { Architecture = arch, SegmentMap = new SegmentMap( mem.BaseAddress, new ImageSegment(".text", mem, AccessMode.ReadWriteExecute)), Platform = new DefaultPlatform(null, arch) }; store = program.TypeStore; factory = program.TypeFactory; point = new StructureType(null, 0); point.Fields.Add(0, PrimitiveType.Word32, null); point.Fields.Add(4, PrimitiveType.Word32, null); m = new ProcedureBuilder(); }
public void TystCopyClassToTypes() { TypeStore store = new TypeStore(); TypeFactory factory = new TypeFactory(); TypeVariable tv1 = store.CreateTypeVariable(factory); TypeVariable tv2 = store.CreateTypeVariable(factory); Assert.IsNotNull(tv1.Class, "Expected store.EnsureTypeVariable to create an equivalence class"); Assert.IsNotNull(tv2.Class, "Expected store.EnsureTypeVariable to create an equivalence class"); EquivalenceClass e = store.MergeClasses(tv1, tv2); e.DataType = PrimitiveType.Word16; store.CopyClassDataTypesToTypeVariables(); foreach (TypeVariable tv in store.TypeVariables) { Assert.IsNotNull(tv.DataType); } }
public void PprReplaceInts() { var arch = new FakeArchitecture(); var program = new Program { Architecture = arch, Platform = new DefaultPlatform(null, arch) }; TypeFactory factory = new TypeFactory(); store = new TypeStore(); TypeVariable tv1 = store.CreateTypeVariable(factory); TypeVariable tv2 = store.CreateTypeVariable(factory); Assert.IsNotNull(tv1.Class, "Expected store.EnsureTypeVariable to create equivalence class"); Assert.IsNotNull(tv2.Class, "Expected store.EnsureTypeVariable to create equivalence class"); tv1.Class.DataType = PrimitiveType.Word32; tv2.Class.DataType = PrimitiveType.Word16; program.Globals.TypeVariable = store.CreateTypeVariable(factory); program.Globals.DataType = factory.CreateStructureType(); TypeVariable tv3 = store.CreateTypeVariable(factory); Assert.IsNotNull(tv3.Class, "Expected store.EnsureTypeVariable to create equivalence class"); StructureType mem = factory.CreateStructureType(null, 0); mem.Fields.Add(0, tv1); mem.Fields.Add(4, tv2); tv3.Class.DataType = factory.CreatePointer(mem, 4); store.CopyClassDataTypesToTypeVariables(); TypeVariableReplacer tvr = new TypeVariableReplacer(store); tvr.ReplaceTypeVariables(); var ppr = new PtrPrimitiveReplacer(factory, store, program); ppr.ReplaceAll(); Verify(null, "Typing/PprReplaceInts.txt"); }
private void baseload() { #region 初始化级别 var lavel = from c in dc.CsBaseinfoset where c.Fid == int.Parse(PublicMethod.ReadXmlReturnNode("YHJB", this)) select new { YHLevelID = c.Infoid, YHLevel = c.Infoname }; LevelStore.DataSource = lavel; LevelStore.DataBind(); #endregion #region 初始化类型 var type = from t in dc.CsBaseinfoset where t.Fid == int.Parse(PublicMethod.ReadXmlReturnNode("ZY", this)) orderby t.Infoid ascending select new { YHTypeID = t.Infoid, YHType = t.Infoname }; TypeStore.DataSource = type; TypeStore.DataBind(); #endregion #region 初始化事故类型 var sg = from t in dc.CsBaseinfoset where t.Fid == int.Parse(PublicMethod.ReadXmlReturnNode("SGLX", this)) orderby t.Infoid ascending select new { t.Infoid, t.Infoname }; SGStore.DataSource = sg; SGStore.DataBind(); #endregion }
/// <summary> /// User has requested the generation of code given an assembly and specification file. /// </summary> /// <param name="specification"></param> /// <param name="assembly"></param> /// <param name="outputPath"></param> public static void GenerateCode(FileInfo specification, FileInfo assembly, string outputPath = default) { if (specification == null) { throw new ArgumentException(Resources.NoSpecificationFileProvided, nameof(specification)); } // register the appropriate style class for its related type key. Each type in the specification needs to have a 'style' specified // for it to be able to generate the appropriate type of code for it. TypeGeneratorStore.Instance.Register(global::Birch.Generator.Styles.Extension.Style.Type, () => new global::Birch.Generator.Styles.Extension.Style()); TypeGeneratorStore.Instance.Register(global::Birch.Generator.Styles.Class.Style.Type, () => new global::Birch.Generator.Styles.Class.Style()); var job = JobReader.FromFile(Path.Join(specification.DirectoryName, specification.Name)); var outputTypeStore = OutputTypesStore.Create(job.Configuration.OutputTypes); var typeStore = new TypeStore(); typeStore.Load(Path.Join(assembly.DirectoryName, assembly.Name)); var modelBuilder = new ModelBuilder(typeStore, outputTypeStore); var models = modelBuilder.Create(job); var codeBuilder = new CodeBuilder(job); var output = codeBuilder.Create(models); if (outputPath != null) { File.WriteAllText(outputPath, output); Console.WriteLine(Resources.CodeWrittenTo, outputPath); } else { Console.WriteLine(output); } }
public void SaveGeneric(string filename) { var ts = new TypeStore(); foreach (SettingsBrokerInstance settingsBrokerInstance in SettingsBrokerInstances) { ts.Typen.Add(settingsBrokerInstance.Value.GetType()); } using (var fs = new FileStream(filename + ".typestore", FileMode.Create)) { var formatter = new BinaryFormatter(); formatter.Serialize(fs, ts); } var serializer = new XmlSerializer(typeof(List <SettingsBrokerInstance>), ts.Typen.ToArray()); using (var textWriter = new StreamWriter(filename)) { serializer.Serialize(textWriter, SettingsBrokerInstances); textWriter.Close(); } }
public void Initalise(CargoStorageDB storageDB, Guid storageTypeID) { _storageDB = storageDB; TypeID = storageTypeID; CargoTypeSD cargoType = _dataStore.CargoTypes[TypeID]; TypeName = cargoType.Name; foreach (var itemKVP in StorageSpaceProcessor.GetResourcesOfCargoType(storageDB, TypeID)) { CargoItemVM cargoItem = new CargoItemVM(_gameVM, _storageDB, itemKVP.Key); TypeStore.Add(cargoItem); } if (_storageDB.StoredEntities.ContainsKey(TypeID)) { InitEntities(); } HeaderText = cargoType.Name + ": " + NetWeight.ToString() + " of " + MaxWeight.ToString() + " used, " + RemainingWeight.ToString() + " remaining"; _storageDB.OwningEntity.Manager.ManagerSubpulses.SystemDateChangedEvent += ManagerSubpulses_SystemDateChangedEvent; _storageDB.MinsAndMatsByCargoType[TypeID].CollectionChanged += _storageDB_CollectionChanged; _storageDB.StoredEntities.CollectionChanged += StoredEntities_CollectionChanged; }
/// <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="program"></param> public void RewriteProgram(Program program) { factory = program.TypeFactory; store = program.TypeStore; var timer = new Stopwatch(); aen = new ExpressionNormalizer(program.Platform.PointerType); eqb = new EquivalenceClassBuilder(factory, store, eventListener); tyco = new TypeCollector( program.TypeFactory, program.TypeStore, program, eventListener); //dpa = new DerivedPointerAnalysis(factory, store, program.Architecture); tvr = new TypeVariableReplacer(store); trans = new TypeTransformer(factory, store, program, eventListener); ctn = new ComplexTypeNamer(); ter = new TypedExpressionRewriter(program, eventListener); // RestrictProcedures(program, 0, 60, true); // Re-enable this for debugging Time("Normalizing expressions", () => aen.Transform(program)); Time("Building equivalence classes", () => eqb.Build(program)); Time("Collecting data types", tyco.CollectTypes); Time("Build eq. class data types", () => store.BuildEquivalenceClassDataTypes(factory)); //dpa.FollowConstantPointers(program); Time("Replacing type variables", tvr.ReplaceTypeVariables); eventListener.ShowStatus("Transforming datatypes."); Time("Replace primitive types", () => { var ppr = new PtrPrimitiveReplacer(factory, store, program); ppr.ReplaceAll(eventListener); }); Time("Transforming data types", trans.Transform); Time("Renaming data types", () => ctn.RenameAllTypes(store)); Time("Rewriting program with type information", () => ter.RewriteProgram(program)); }
public Job Generate(TypeStore typeStore, Func <ITypeDefinition, bool> includeType = default, Func <IMethod, bool> includeMethod = default, string templateStyles = default, TemplateExportStyle embedTemplatesExport = TemplateExportStyle.File) { // const string voidTypeName = "System.Void"; const string enumerableTypeName = "System.Collections.IEnumerable"; const string genericListTypeName = "System.Collections.Generic.IList"; const string equatableTypeName = "System.IEquatable"; includeType ??= _ => true; includeMethod ??= _ => true; var typesToProcess = new List <TypeToProcess>(); // Iterate through the public types foreach (var type in typeStore.GetTypes().Where(t => t.Accessibility == Accessibility.Public && // only pull in public items t.Kind == TypeKind.Class && // which are classes !t.IsStatic && // and aren't static t.TypeParameters.Count == 0 && // and aren't generics includeType(t))) // and matches with custom additional properties { // now pull in those public properties for which there is a getter AND a setter and it isn't static var properties = type.Properties.Where(property => property.Accessibility == Accessibility.Public && property.CanGet && property.CanSet && !property.IsStatic && property.DeclaringType.Equals(type)). Select(property => { var propertyType = property.ReturnType; var baseTypes = propertyType.GetAllBaseTypeDefinitions(); var isCollection = baseTypes.Any(bt => bt.FullName == genericListTypeName) || propertyType.ReflectionName == enumerableTypeName; var notes = new StringBuilder(); if (isCollection) { notes.AppendLine($"Collection."); } // if this is a reference type, lets check to see if we have an IEquatable implementation if ((property.ReturnType.IsReferenceType ?? false) && !baseTypes.Any(bt => bt.FullName == equatableTypeName)) { notes.Append($"{property.ReturnType.FullName} doesn't have an implementation of IEquatable, use a field or a static to hold its value to reduce updates."); } var prop = new Property() { Source = property.Name }; if (notes.Length > 0) { prop.Notes = notes.ToString(); } return(prop); } ).ToList(); // get those public void methods which aren't constructors, destructors, aren't static, aren't operations and aren't generic and have less than 5 parameters var methods = type.Methods .Where(method => !method.IsConstructor && !method.IsDestructor && method.HasBody && !method.IsStatic && !method.IsOperator && method.TypeParameters.Count == 0 && method.Accessibility == Accessibility.Public && method.ReturnType.FullName == voidTypeName && method.DeclaringType.Equals(type) && includeMethod(method)). Select(method => method.Name). Distinct(). Select(methodName => new Method() { Source = methodName }). OrderBy(m => m.Source). ToList(); // and finally, those public events which aren't static. var events = type.Events.Where(e => e.Accessibility == Accessibility.Public && !e.IsStatic && e.DeclaringType.Equals(type)).Select(e => new Event() { Source = e.Name }).ToList(); var typeToProcess = new TypeToProcess(type.FullName, new Style() { MethodName = type.Name }, properties, methods, events); typesToProcess.Add(typeToProcess); } // if template style specified, load up the templates var templates = string.IsNullOrEmpty(templateStyles) ? new List <Template> (): ResourceTemplateLoader.ReadFor(templateStyles); var generation = new Configuration("Enter your namespace here", RootTemplateName, templates , Style.Type, new List <CodeOutputType>() { CodeOutputType.Create <Styles.Extension.ModelFactory>(Styles.Extension.Style.Type) }); var generatedRoot = new Job(typesToProcess, generation); return(generatedRoot); }
public void SetUp() { factory = new TypeFactory(); store = new TypeStore(); }
public TypeTransformer(TypeFactory factory, TypeStore store, Program program) : this(factory, store, program, new NullDecompilerEventListener()) { }
public TraitMapping(TypeStore store) { items = new SortedList <TypeVariable, List <Trait> >(this); this.store = store; }
public void Setup() { factory = new TypeFactory(); store = new TypeStore(); eqb = new EquivalenceClassBuilder(factory, store); }
private ITraitHandler CreateHandler(TypeStore store) { return(new TestTraitHandler(store)); }
public TestTraitHandler(TypeStore store) { this.Traits = new TraitMapping(store); }
public void Setup() { store = new TypeStore(); factory = new TypeFactory(); }