Ejemplo n.º 1
0
        private static void BuildRuntimeDataStringTable(HLStringTable pStringTable)
        {
            byte[]   bufferRuntimeDataStringTable   = pStringTable.ToASCIIBytes();
            string[] literalsRuntimeDataStringTable = Array.ConvertAll(bufferRuntimeDataStringTable, b => b.ToString());
            LLType   typeRuntimeDataStringTable     = LLModule.GetOrCreateArrayType(LLModule.GetOrCreateSignedType(8), literalsRuntimeDataStringTable.Length);
            LLGlobal globalRuntimeDataStringTable   = LLModule.CreateGlobal(typeRuntimeDataStringTable.PointerDepthPlusOne, "RuntimeDataStringTable");

            globalRuntimeDataStringTable.InitialValue = typeRuntimeDataStringTable.ToLiteral(literalsRuntimeDataStringTable);
        }
Ejemplo n.º 2
0
        private static void BuildRuntimeMethodData(HLStringTable pStringTable)
        {
            List <LLType>     fieldsRuntimeMethodData                 = sSystemRuntimeMethodData.Fields.Where(f => !f.IsStatic).ToList().ConvertAll(f => f.Type.LLType);
            LLType            typePointer                             = LLModule.GetOrCreatePointerType(LLModule.GetOrCreateUnsignedType(8), 1);
            LLType            typeRuntimeMethodData                   = LLModule.GetOrCreateStructureType("RuntimeMethodData", true, fieldsRuntimeMethodData);
            LLType            typeRuntimeMethodDataTable              = LLModule.GetOrCreateArrayType(typeRuntimeMethodData, sMethods.Values.Count);
            LLGlobal          globalRuntimeMethodDataTable            = LLModule.CreateGlobal(typeRuntimeMethodDataTable.PointerDepthPlusOne, "RuntimeMethodDataTable");
            List <LLLiteral>  literalsRuntimeMethodDataTable          = new List <LLLiteral>();
            List <LLFunction> functionsRuntimeMethodDataFunctionTable = new List <LLFunction>();
            List <LLLiteral>  literalsRuntimeMethodDataFunctionTable  = new List <LLLiteral>();

            foreach (HLMethod method in sMethods.Values.OrderBy(m => m.RuntimeMethodHandle))
            {
                int flags = 0;
                if (method.IsStatic)
                {
                    flags |= 1 << 0;
                }

                string definitionName = method.Definition.Name.Value;
                int    offsetName     = pStringTable.Include(definitionName);

                literalsRuntimeMethodDataTable.Add(typeRuntimeMethodData.ToLiteral(method.RuntimeMethodHandle.ToString(), flags.ToString(), offsetName.ToString()));

                LLFunction function = method.LLFunction;
                functionsRuntimeMethodDataFunctionTable.Add(function);
                string literalAddress = null;
                if (method.LLFunction.Abstract)
                {
                    literalAddress = "null";
                }
                else
                {
                    literalAddress = string.Format("bitcast({0} {1} to {2})", LLModule.GetOrCreatePointerType(function.FunctionType, 1), function, typePointer);
                }
                literalsRuntimeMethodDataFunctionTable.Add(LLLiteral.Create(typePointer, literalAddress));
            }
            globalRuntimeMethodDataTable.InitialValue = typeRuntimeMethodDataTable.ToLiteral(literalsRuntimeMethodDataTable.ConvertAll(l => l.Value).ToArray());
            LLGlobal globalRuntimeMethodDataTableCount = LLModule.CreateGlobal(LLModule.GetOrCreateSignedType(32), "RuntimeMethodDataTableCount");

            globalRuntimeMethodDataTableCount.InitialValue = LLLiteral.Create(LLModule.GetOrCreateSignedType(32), sMethods.Values.Count.ToString());

            LLType   typeRuntimeMethodDataFunctionTable   = LLModule.GetOrCreateArrayType(typePointer, functionsRuntimeMethodDataFunctionTable.Count);
            LLGlobal globalRuntimeMethodDataFunctionTable = LLModule.CreateGlobal(typeRuntimeMethodDataFunctionTable.PointerDepthPlusOne, "RuntimeMethodDataFunctionTable");

            globalRuntimeMethodDataFunctionTable.InitialValue = typeRuntimeMethodDataFunctionTable.ToLiteral(literalsRuntimeMethodDataFunctionTable.ConvertAll(l => l.Value).ToArray());
            LLGlobal globalRuntimeMethodDataFunctionTableCount = LLModule.CreateGlobal(LLModule.GetOrCreateSignedType(32), "RuntimeMethodDataFunctionTableCount");

            globalRuntimeMethodDataFunctionTableCount.InitialValue = LLLiteral.Create(LLModule.GetOrCreateSignedType(32), functionsRuntimeMethodDataFunctionTable.Count.ToString());

            sRuntimeMethodDataFunctionTable = globalRuntimeMethodDataFunctionTable;
        }
Ejemplo n.º 3
0
        private static void BuildRuntimeTypeData(HLStringTable pStringTable)
        {
            List <LLType>    fieldsRuntimeTypeData                   = sSystemRuntimeTypeData.Fields.Where(f => !f.IsStatic).ToList().ConvertAll(f => f.Type.LLType);
            LLType           typePointer                             = LLModule.GetOrCreatePointerType(LLModule.GetOrCreateUnsignedType(8), 1);
            LLType           typeRuntimeTypeData                     = LLModule.GetOrCreateStructureType("RuntimeTypeData", true, fieldsRuntimeTypeData);
            LLType           typeRuntimeTypeDataTable                = LLModule.GetOrCreateArrayType(typeRuntimeTypeData, sTypes.Values.Count);
            LLGlobal         globalRuntimeTypeDataTable              = LLModule.CreateGlobal(typeRuntimeTypeDataTable.PointerDepthPlusOne, "RuntimeTypeDataTable");
            List <LLLiteral> literalsRuntimeTypeDataTable            = new List <LLLiteral>();
            List <LLLiteral> literalsRuntimeTypeDataEnum8ValueTable  = new List <LLLiteral>();
            List <LLLiteral> literalsRuntimeTypeDataEnum16ValueTable = new List <LLLiteral>();
            List <LLLiteral> literalsRuntimeTypeDataEnum32ValueTable = new List <LLLiteral>();
            List <LLLiteral> literalsRuntimeTypeDataEnum64ValueTable = new List <LLLiteral>();
            List <LLLiteral> literalsRuntimeTypeDataEnum8NameTable   = new List <LLLiteral>();
            List <LLLiteral> literalsRuntimeTypeDataEnum16NameTable  = new List <LLLiteral>();
            List <LLLiteral> literalsRuntimeTypeDataEnum32NameTable  = new List <LLLiteral>();
            List <LLLiteral> literalsRuntimeTypeDataEnum64NameTable  = new List <LLLiteral>();
            List <int>       handlesRuntimeTypeDataVirtualTable      = new List <int>();
            List <LLLiteral> literalsRuntimeTypeDataVirtualTable     = new List <LLLiteral>();

            foreach (HLType type in sTypes.Values.OrderBy(t => t.RuntimeTypeHandle))
            {
                int flags = 0;
                if (type.Definition.IsValueType)
                {
                    flags |= 1 << 0;
                }
                if (type.Definition.IsEnum)
                {
                    flags |= 1 << 1;
                }

                string definitionName  = TypeHelper.GetTypeName(type.Definition, NameFormattingOptions.OmitContainingNamespace);
                int    offsetName      = pStringTable.Include(definitionName);
                int    offsetNamespace = 0;
                if (type.Definition is INamespaceTypeDefinition)
                {
                    string definitionNamespace = TypeHelper.GetNamespaceName(((INamespaceTypeDefinition)type.Definition).ContainingUnitNamespace, NameFormattingOptions.None);
                    offsetNamespace = pStringTable.Include(definitionNamespace);
                }

                int offsetEnum = 0;
                int countEnum  = 0;
                if (type.Definition.IsEnum)
                {
                    countEnum = type.StaticFields.Count;
                    switch (type.MemberFields[0].Type.VariableSize)
                    {
                    case 1:
                        offsetEnum = literalsRuntimeTypeDataEnum8ValueTable.Count;
                        for (int index = 0; index < type.StaticFields.Count; ++index)
                        {
                            int offsetEnumName = pStringTable.Include(type.StaticFields[index].Name);
                            literalsRuntimeTypeDataEnum8ValueTable.Add(LLLiteral.Create(LLModule.GetOrCreateUnsignedType(8), type.StaticFields[index].CompileTimeConstant.ToString()));
                            literalsRuntimeTypeDataEnum8NameTable.Add(LLLiteral.Create(LLModule.GetOrCreateSignedType(32), offsetEnumName.ToString()));
                        }
                        break;

                    case 2:
                        offsetEnum = literalsRuntimeTypeDataEnum16ValueTable.Count;
                        for (int index = 0; index < type.StaticFields.Count; ++index)
                        {
                            int offsetEnumName = pStringTable.Include(type.StaticFields[index].Name);
                            literalsRuntimeTypeDataEnum16ValueTable.Add(LLLiteral.Create(LLModule.GetOrCreateUnsignedType(16), type.StaticFields[index].CompileTimeConstant.ToString()));
                            literalsRuntimeTypeDataEnum16NameTable.Add(LLLiteral.Create(LLModule.GetOrCreateSignedType(32), offsetEnumName.ToString()));
                        }
                        break;

                    case 4:
                        offsetEnum = literalsRuntimeTypeDataEnum32ValueTable.Count;
                        for (int index = 0; index < type.StaticFields.Count; ++index)
                        {
                            int offsetEnumName = pStringTable.Include(type.StaticFields[index].Name);
                            literalsRuntimeTypeDataEnum32ValueTable.Add(LLLiteral.Create(LLModule.GetOrCreateUnsignedType(32), type.StaticFields[index].CompileTimeConstant.ToString()));
                            literalsRuntimeTypeDataEnum32NameTable.Add(LLLiteral.Create(LLModule.GetOrCreateSignedType(32), offsetEnumName.ToString()));
                        }
                        break;

                    case 8:
                        offsetEnum = literalsRuntimeTypeDataEnum64ValueTable.Count;
                        for (int index = 0; index < type.StaticFields.Count; ++index)
                        {
                            int offsetEnumName = pStringTable.Include(type.StaticFields[index].Name);
                            literalsRuntimeTypeDataEnum64ValueTable.Add(LLLiteral.Create(LLModule.GetOrCreateUnsignedType(64), type.StaticFields[index].CompileTimeConstant.ToString()));
                            literalsRuntimeTypeDataEnum64NameTable.Add(LLLiteral.Create(LLModule.GetOrCreateSignedType(32), offsetEnumName.ToString()));
                        }
                        break;

                    default: throw new NotSupportedException();
                    }
                }

                literalsRuntimeTypeDataTable.Add(typeRuntimeTypeData.ToLiteral(type.RuntimeTypeHandle.ToString(),
                                                                               flags.ToString(),
                                                                               type.CalculatedSize.ToString(),
                                                                               offsetName.ToString(),
                                                                               offsetNamespace.ToString(),
                                                                               handlesRuntimeTypeDataVirtualTable.Count.ToString(),
                                                                               offsetEnum.ToString(),
                                                                               countEnum.ToString()));

                //List<LLFunction> functions = type.VirtualTable.ConvertAll(m => m.LLFunction);
                foreach (HLMethod method in type.VirtualTable)
                {
                    handlesRuntimeTypeDataVirtualTable.Add(method.RuntimeMethodHandle);
                    literalsRuntimeTypeDataVirtualTable.Add(LLLiteral.Create(LLModule.GetOrCreateSignedType(32), method.RuntimeMethodHandle.ToString()));
                }
                //foreach (LLFunction function in functions)
                //{
                //    handlesRuntimeTypeDataVirtualTable.Add(function);
                //    string literalAddress = null;
                //    if (function.Abstract) literalAddress = "null";
                //    else literalAddress = string.Format("bitcast({0} {1} to {2})", LLModule.GetOrCreatePointerType(function.FunctionType, 1), function, typePointer);
                //    literalsRuntimeTypeDataVirtualTable.Add(LLLiteral.Create(typePointer, literalAddress));
                //}
            }
            globalRuntimeTypeDataTable.InitialValue = typeRuntimeTypeDataTable.ToLiteral(literalsRuntimeTypeDataTable.ConvertAll(l => l.Value).ToArray());
            LLGlobal globalRuntimeTypeDataTableCount = LLModule.CreateGlobal(LLModule.GetOrCreateSignedType(32), "RuntimeTypeDataTableCount");

            globalRuntimeTypeDataTableCount.InitialValue = LLLiteral.Create(LLModule.GetOrCreateSignedType(32), sTypes.Values.Count.ToString());

            LLType   typeRuntimeTypeDataEnum8ValueTable   = LLModule.GetOrCreateArrayType(LLModule.GetOrCreateUnsignedType(8), literalsRuntimeTypeDataEnum8ValueTable.Count);
            LLGlobal globalRuntimeTypeDataEnum8ValueTable = LLModule.CreateGlobal(typeRuntimeTypeDataEnum8ValueTable.PointerDepthPlusOne, "RuntimeTypeDataEnum8ValueTable");

            globalRuntimeTypeDataEnum8ValueTable.InitialValue = typeRuntimeTypeDataEnum8ValueTable.ToLiteral(literalsRuntimeTypeDataEnum8ValueTable.ConvertAll(l => l.Value).ToArray());

            LLType   typeRuntimeTypeDataEnum8NameTable   = LLModule.GetOrCreateArrayType(LLModule.GetOrCreateSignedType(32), literalsRuntimeTypeDataEnum8NameTable.Count);
            LLGlobal globalRuntimeTypeDataEnum8NameTable = LLModule.CreateGlobal(typeRuntimeTypeDataEnum8NameTable.PointerDepthPlusOne, "RuntimeTypeDataEnum8NameTable");

            globalRuntimeTypeDataEnum8NameTable.InitialValue = typeRuntimeTypeDataEnum8NameTable.ToLiteral(literalsRuntimeTypeDataEnum8NameTable.ConvertAll(l => l.Value).ToArray());

            LLType   typeRuntimeTypeDataEnum16ValueTable   = LLModule.GetOrCreateArrayType(LLModule.GetOrCreateUnsignedType(16), literalsRuntimeTypeDataEnum16ValueTable.Count);
            LLGlobal globalRuntimeTypeDataEnum16ValueTable = LLModule.CreateGlobal(typeRuntimeTypeDataEnum16ValueTable.PointerDepthPlusOne, "RuntimeTypeDataEnum16ValueTable");

            globalRuntimeTypeDataEnum16ValueTable.InitialValue = typeRuntimeTypeDataEnum16ValueTable.ToLiteral(literalsRuntimeTypeDataEnum16ValueTable.ConvertAll(l => l.Value).ToArray());

            LLType   typeRuntimeTypeDataEnum16NameTable   = LLModule.GetOrCreateArrayType(LLModule.GetOrCreateSignedType(32), literalsRuntimeTypeDataEnum16NameTable.Count);
            LLGlobal globalRuntimeTypeDataEnum16NameTable = LLModule.CreateGlobal(typeRuntimeTypeDataEnum16NameTable.PointerDepthPlusOne, "RuntimeTypeDataEnum16NameTable");

            globalRuntimeTypeDataEnum16NameTable.InitialValue = typeRuntimeTypeDataEnum16NameTable.ToLiteral(literalsRuntimeTypeDataEnum16NameTable.ConvertAll(l => l.Value).ToArray());

            LLType   typeRuntimeTypeDataEnum32ValueTable   = LLModule.GetOrCreateArrayType(LLModule.GetOrCreateUnsignedType(32), literalsRuntimeTypeDataEnum32ValueTable.Count);
            LLGlobal globalRuntimeTypeDataEnum32ValueTable = LLModule.CreateGlobal(typeRuntimeTypeDataEnum32ValueTable.PointerDepthPlusOne, "RuntimeTypeDataEnum32ValueTable");

            globalRuntimeTypeDataEnum32ValueTable.InitialValue = typeRuntimeTypeDataEnum32ValueTable.ToLiteral(literalsRuntimeTypeDataEnum32ValueTable.ConvertAll(l => l.Value).ToArray());

            LLType   typeRuntimeTypeDataEnum32NameTable   = LLModule.GetOrCreateArrayType(LLModule.GetOrCreateSignedType(32), literalsRuntimeTypeDataEnum32NameTable.Count);
            LLGlobal globalRuntimeTypeDataEnum32NameTable = LLModule.CreateGlobal(typeRuntimeTypeDataEnum32NameTable.PointerDepthPlusOne, "RuntimeTypeDataEnum32NameTable");

            globalRuntimeTypeDataEnum32NameTable.InitialValue = typeRuntimeTypeDataEnum32NameTable.ToLiteral(literalsRuntimeTypeDataEnum32NameTable.ConvertAll(l => l.Value).ToArray());

            LLType   typeRuntimeTypeDataEnum64ValueTable   = LLModule.GetOrCreateArrayType(LLModule.GetOrCreateUnsignedType(64), literalsRuntimeTypeDataEnum64ValueTable.Count);
            LLGlobal globalRuntimeTypeDataEnum64ValueTable = LLModule.CreateGlobal(typeRuntimeTypeDataEnum64ValueTable.PointerDepthPlusOne, "RuntimeTypeDataEnum64ValueTable");

            globalRuntimeTypeDataEnum64ValueTable.InitialValue = typeRuntimeTypeDataEnum64ValueTable.ToLiteral(literalsRuntimeTypeDataEnum64ValueTable.ConvertAll(l => l.Value).ToArray());

            LLType   typeRuntimeTypeDataEnum64NameTable   = LLModule.GetOrCreateArrayType(LLModule.GetOrCreateSignedType(32), literalsRuntimeTypeDataEnum64NameTable.Count);
            LLGlobal globalRuntimeTypeDataEnum64NameTable = LLModule.CreateGlobal(typeRuntimeTypeDataEnum64NameTable.PointerDepthPlusOne, "RuntimeTypeDataEnum64NameTable");

            globalRuntimeTypeDataEnum64NameTable.InitialValue = typeRuntimeTypeDataEnum64NameTable.ToLiteral(literalsRuntimeTypeDataEnum64NameTable.ConvertAll(l => l.Value).ToArray());

            LLType   typeRuntimeTypeDataVirtualTable   = LLModule.GetOrCreateArrayType(LLModule.GetOrCreateSignedType(32), handlesRuntimeTypeDataVirtualTable.Count);
            LLGlobal globalRuntimeTypeDataVirtualTable = LLModule.CreateGlobal(typeRuntimeTypeDataVirtualTable.PointerDepthPlusOne, "RuntimeTypeDataVirtualTable");

            globalRuntimeTypeDataVirtualTable.InitialValue = typeRuntimeTypeDataVirtualTable.ToLiteral(literalsRuntimeTypeDataVirtualTable.ConvertAll(l => l.Value).ToArray());
            LLGlobal globalRuntimeTypeDataVirtualTableCount = LLModule.CreateGlobal(LLModule.GetOrCreateSignedType(32), "RuntimeTypeDataVirtualTableCount");

            globalRuntimeTypeDataVirtualTableCount.InitialValue = LLLiteral.Create(LLModule.GetOrCreateSignedType(32), handlesRuntimeTypeDataVirtualTable.Count.ToString());
        }
Ejemplo n.º 4
0
        public static void Process(string pInputPath)
        {
            sModule          = Decompiler.GetCodeModelFromMetadataModel(Host, Host.LoadUnitFrom(pInputPath) as IModule, null);
            sPlatformType    = Module.PlatformType;
            sRuntimeAssembly = (IAssembly)PlatformType.SystemRuntimeTypeHandle.ContainingUnitNamespace.Unit.ResolvedUnit;

            EnlistRuntimeTypes();

            EnlistRuntimeMethods();

            sEntryMethod = GetOrCreateMethod(Module.EntryPoint);

            int checkedTypes = 0;

            while (sTypes.Count != checkedTypes || sPendingMethods.Count > 0)
            {
                // STARTED: Process IStatements/IExpressions into HLInstructionBlocks/HLInstructions
                while (sPendingMethods.Count > 0)
                {
                    sPendingMethods.Dequeue().Process();
                }

                // STARTED: Build virtual map for each type and include any missing override methods
                // starting at the top the chain (Object) and working down to the given type, then process
                // pending methods and repeat until no methods or types are added, as each new method may
                // add new types and methods
                checkedTypes = sTypes.Count;
                foreach (HLType type in sTypes.Values.ToList())
                {
                    type.MapVirtualMethods();
                }
            }
            // NOTE: After this point, do not GetOrCreate types and methods

            // DONE: Build virtual table and virtual index lookup from the complete virtual maps
            foreach (HLType type in sTypes.Values)
            {
                type.BuildVirtualTable();
            }

            // DONE: Determine HLType Calculated and Variable sizes
            // DONE: Layout Non-Static HLField offsets
            foreach (HLType type in sTypes.Values)
            {
                type.LayoutFields();
            }

            // NOTE: After this point, conversion to LL begins

            // DONE: Convert HLTypes to LLTypes

            BuildGCAllocate();
            BuildGCRoot();
            BuildVTableHandleLookup();
            BuildVTableFunctionLookup();
            BuildDelegateLookup();
            BuildDelegateInstance();

            BuildGlobals();

            foreach (HLMethod method in sMethods.Values.ToList())
            {
                method.BuildFunction();
            }

            HLStringTable stringTable = new HLStringTable();

            // STARTED: Build constant global runtime type handles
            BuildRuntimeTypeHandles();

            // STARTED: Build constant global runtime type data
            BuildRuntimeTypeData(stringTable);

            // STARTED: Build constant global runtime method data
            BuildRuntimeMethodData(stringTable);

            // STARTED: Build constant global runtime data string table
            BuildRuntimeDataStringTable(stringTable);

            // STARTED: Convert HLInstructions to LLInstructions
            foreach (HLMethod method in sMethods.Values)
            {
                method.Transform();
            }

            using (StreamWriter writer = new StreamWriter(Path.ChangeExtension(pInputPath, ".ll")))
            {
                LLModule.Dump(writer);
            }
        }
Ejemplo n.º 5
0
        private static void BuildRuntimeTypeData(HLStringTable pStringTable)
        {
            List<LLType> fieldsRuntimeTypeData = sSystemRuntimeTypeData.Fields.Where(f => !f.IsStatic).ToList().ConvertAll(f => f.Type.LLType);
            LLType typePointer = LLModule.GetOrCreatePointerType(LLModule.GetOrCreateUnsignedType(8), 1);
            LLType typeRuntimeTypeData = LLModule.GetOrCreateStructureType("RuntimeTypeData", true, fieldsRuntimeTypeData);
            LLType typeRuntimeTypeDataTable = LLModule.GetOrCreateArrayType(typeRuntimeTypeData, sTypes.Values.Count);
            LLGlobal globalRuntimeTypeDataTable = LLModule.CreateGlobal(typeRuntimeTypeDataTable.PointerDepthPlusOne, "RuntimeTypeDataTable");
            List<LLLiteral> literalsRuntimeTypeDataTable = new List<LLLiteral>();
            List<LLLiteral> literalsRuntimeTypeDataEnum8ValueTable = new List<LLLiteral>();
            List<LLLiteral> literalsRuntimeTypeDataEnum16ValueTable = new List<LLLiteral>();
            List<LLLiteral> literalsRuntimeTypeDataEnum32ValueTable = new List<LLLiteral>();
            List<LLLiteral> literalsRuntimeTypeDataEnum64ValueTable = new List<LLLiteral>();
            List<LLLiteral> literalsRuntimeTypeDataEnum8NameTable = new List<LLLiteral>();
            List<LLLiteral> literalsRuntimeTypeDataEnum16NameTable = new List<LLLiteral>();
            List<LLLiteral> literalsRuntimeTypeDataEnum32NameTable = new List<LLLiteral>();
            List<LLLiteral> literalsRuntimeTypeDataEnum64NameTable = new List<LLLiteral>();
            List<int> handlesRuntimeTypeDataVirtualTable = new List<int>();
            List<LLLiteral> literalsRuntimeTypeDataVirtualTable = new List<LLLiteral>();

            foreach (HLType type in sTypes.Values.OrderBy(t => t.RuntimeTypeHandle))
            {
                int flags = 0;
                if (type.Definition.IsValueType) flags |= 1 << 0;
                if (type.Definition.IsEnum) flags |= 1 << 1;

                string definitionName = TypeHelper.GetTypeName(type.Definition, NameFormattingOptions.OmitContainingNamespace);
                int offsetName = pStringTable.Include(definitionName);
                int offsetNamespace = 0;
                if (type.Definition is INamespaceTypeDefinition)
                {
                    string definitionNamespace = TypeHelper.GetNamespaceName(((INamespaceTypeDefinition)type.Definition).ContainingUnitNamespace, NameFormattingOptions.None);
                    offsetNamespace = pStringTable.Include(definitionNamespace);
                }

                int offsetEnum = 0;
                int countEnum = 0;
                if (type.Definition.IsEnum)
                {
                    countEnum = type.StaticFields.Count;
                    switch (type.MemberFields[0].Type.VariableSize)
                    {
                        case 1:
                            offsetEnum = literalsRuntimeTypeDataEnum8ValueTable.Count;
                            for (int index = 0; index < type.StaticFields.Count; ++index)
                            {
                                int offsetEnumName = pStringTable.Include(type.StaticFields[index].Name);
                                literalsRuntimeTypeDataEnum8ValueTable.Add(LLLiteral.Create(LLModule.GetOrCreateUnsignedType(8), type.StaticFields[index].CompileTimeConstant.ToString()));
                                literalsRuntimeTypeDataEnum8NameTable.Add(LLLiteral.Create(LLModule.GetOrCreateSignedType(32), offsetEnumName.ToString()));
                            }
                            break;
                        case 2:
                            offsetEnum = literalsRuntimeTypeDataEnum16ValueTable.Count;
                            for (int index = 0; index < type.StaticFields.Count; ++index)
                            {
                                int offsetEnumName = pStringTable.Include(type.StaticFields[index].Name);
                                literalsRuntimeTypeDataEnum16ValueTable.Add(LLLiteral.Create(LLModule.GetOrCreateUnsignedType(16), type.StaticFields[index].CompileTimeConstant.ToString()));
                                literalsRuntimeTypeDataEnum16NameTable.Add(LLLiteral.Create(LLModule.GetOrCreateSignedType(32), offsetEnumName.ToString()));
                            }
                            break;
                        case 4:
                            offsetEnum = literalsRuntimeTypeDataEnum32ValueTable.Count;
                            for (int index = 0; index < type.StaticFields.Count; ++index)
                            {
                                int offsetEnumName = pStringTable.Include(type.StaticFields[index].Name);
                                literalsRuntimeTypeDataEnum32ValueTable.Add(LLLiteral.Create(LLModule.GetOrCreateUnsignedType(32), type.StaticFields[index].CompileTimeConstant.ToString()));
                                literalsRuntimeTypeDataEnum32NameTable.Add(LLLiteral.Create(LLModule.GetOrCreateSignedType(32), offsetEnumName.ToString()));
                            }
                            break;
                        case 8:
                            offsetEnum = literalsRuntimeTypeDataEnum64ValueTable.Count;
                            for (int index = 0; index < type.StaticFields.Count; ++index)
                            {
                                int offsetEnumName = pStringTable.Include(type.StaticFields[index].Name);
                                literalsRuntimeTypeDataEnum64ValueTable.Add(LLLiteral.Create(LLModule.GetOrCreateUnsignedType(64), type.StaticFields[index].CompileTimeConstant.ToString()));
                                literalsRuntimeTypeDataEnum64NameTable.Add(LLLiteral.Create(LLModule.GetOrCreateSignedType(32), offsetEnumName.ToString()));
                            }
                            break;
                        default: throw new NotSupportedException();
                    }
                }

                literalsRuntimeTypeDataTable.Add(typeRuntimeTypeData.ToLiteral(type.RuntimeTypeHandle.ToString(),
                                                                               flags.ToString(),
                                                                               type.CalculatedSize.ToString(),
                                                                               offsetName.ToString(),
                                                                               offsetNamespace.ToString(),
                                                                               handlesRuntimeTypeDataVirtualTable.Count.ToString(),
                                                                               offsetEnum.ToString(),
                                                                               countEnum.ToString()));

                //List<LLFunction> functions = type.VirtualTable.ConvertAll(m => m.LLFunction);
                foreach (HLMethod method in type.VirtualTable)
                {
                    handlesRuntimeTypeDataVirtualTable.Add(method.RuntimeMethodHandle);
                    literalsRuntimeTypeDataVirtualTable.Add(LLLiteral.Create(LLModule.GetOrCreateSignedType(32), method.RuntimeMethodHandle.ToString()));
                }
                //foreach (LLFunction function in functions)
                //{
                //    handlesRuntimeTypeDataVirtualTable.Add(function);
                //    string literalAddress = null;
                //    if (function.Abstract) literalAddress = "null";
                //    else literalAddress = string.Format("bitcast({0} {1} to {2})", LLModule.GetOrCreatePointerType(function.FunctionType, 1), function, typePointer);
                //    literalsRuntimeTypeDataVirtualTable.Add(LLLiteral.Create(typePointer, literalAddress));
                //}
            }
            globalRuntimeTypeDataTable.InitialValue = typeRuntimeTypeDataTable.ToLiteral(literalsRuntimeTypeDataTable.ConvertAll(l => l.Value).ToArray());
            LLGlobal globalRuntimeTypeDataTableCount = LLModule.CreateGlobal(LLModule.GetOrCreateSignedType(32), "RuntimeTypeDataTableCount");
            globalRuntimeTypeDataTableCount.InitialValue = LLLiteral.Create(LLModule.GetOrCreateSignedType(32), sTypes.Values.Count.ToString());

            LLType typeRuntimeTypeDataEnum8ValueTable = LLModule.GetOrCreateArrayType(LLModule.GetOrCreateUnsignedType(8), literalsRuntimeTypeDataEnum8ValueTable.Count);
            LLGlobal globalRuntimeTypeDataEnum8ValueTable = LLModule.CreateGlobal(typeRuntimeTypeDataEnum8ValueTable.PointerDepthPlusOne, "RuntimeTypeDataEnum8ValueTable");
            globalRuntimeTypeDataEnum8ValueTable.InitialValue = typeRuntimeTypeDataEnum8ValueTable.ToLiteral(literalsRuntimeTypeDataEnum8ValueTable.ConvertAll(l => l.Value).ToArray());

            LLType typeRuntimeTypeDataEnum8NameTable = LLModule.GetOrCreateArrayType(LLModule.GetOrCreateSignedType(32), literalsRuntimeTypeDataEnum8NameTable.Count);
            LLGlobal globalRuntimeTypeDataEnum8NameTable = LLModule.CreateGlobal(typeRuntimeTypeDataEnum8NameTable.PointerDepthPlusOne, "RuntimeTypeDataEnum8NameTable");
            globalRuntimeTypeDataEnum8NameTable.InitialValue = typeRuntimeTypeDataEnum8NameTable.ToLiteral(literalsRuntimeTypeDataEnum8NameTable.ConvertAll(l => l.Value).ToArray());

            LLType typeRuntimeTypeDataEnum16ValueTable = LLModule.GetOrCreateArrayType(LLModule.GetOrCreateUnsignedType(16), literalsRuntimeTypeDataEnum16ValueTable.Count);
            LLGlobal globalRuntimeTypeDataEnum16ValueTable = LLModule.CreateGlobal(typeRuntimeTypeDataEnum16ValueTable.PointerDepthPlusOne, "RuntimeTypeDataEnum16ValueTable");
            globalRuntimeTypeDataEnum16ValueTable.InitialValue = typeRuntimeTypeDataEnum16ValueTable.ToLiteral(literalsRuntimeTypeDataEnum16ValueTable.ConvertAll(l => l.Value).ToArray());

            LLType typeRuntimeTypeDataEnum16NameTable = LLModule.GetOrCreateArrayType(LLModule.GetOrCreateSignedType(32), literalsRuntimeTypeDataEnum16NameTable.Count);
            LLGlobal globalRuntimeTypeDataEnum16NameTable = LLModule.CreateGlobal(typeRuntimeTypeDataEnum16NameTable.PointerDepthPlusOne, "RuntimeTypeDataEnum16NameTable");
            globalRuntimeTypeDataEnum16NameTable.InitialValue = typeRuntimeTypeDataEnum16NameTable.ToLiteral(literalsRuntimeTypeDataEnum16NameTable.ConvertAll(l => l.Value).ToArray());

            LLType typeRuntimeTypeDataEnum32ValueTable = LLModule.GetOrCreateArrayType(LLModule.GetOrCreateUnsignedType(32), literalsRuntimeTypeDataEnum32ValueTable.Count);
            LLGlobal globalRuntimeTypeDataEnum32ValueTable = LLModule.CreateGlobal(typeRuntimeTypeDataEnum32ValueTable.PointerDepthPlusOne, "RuntimeTypeDataEnum32ValueTable");
            globalRuntimeTypeDataEnum32ValueTable.InitialValue = typeRuntimeTypeDataEnum32ValueTable.ToLiteral(literalsRuntimeTypeDataEnum32ValueTable.ConvertAll(l => l.Value).ToArray());

            LLType typeRuntimeTypeDataEnum32NameTable = LLModule.GetOrCreateArrayType(LLModule.GetOrCreateSignedType(32), literalsRuntimeTypeDataEnum32NameTable.Count);
            LLGlobal globalRuntimeTypeDataEnum32NameTable = LLModule.CreateGlobal(typeRuntimeTypeDataEnum32NameTable.PointerDepthPlusOne, "RuntimeTypeDataEnum32NameTable");
            globalRuntimeTypeDataEnum32NameTable.InitialValue = typeRuntimeTypeDataEnum32NameTable.ToLiteral(literalsRuntimeTypeDataEnum32NameTable.ConvertAll(l => l.Value).ToArray());

            LLType typeRuntimeTypeDataEnum64ValueTable = LLModule.GetOrCreateArrayType(LLModule.GetOrCreateUnsignedType(64), literalsRuntimeTypeDataEnum64ValueTable.Count);
            LLGlobal globalRuntimeTypeDataEnum64ValueTable = LLModule.CreateGlobal(typeRuntimeTypeDataEnum64ValueTable.PointerDepthPlusOne, "RuntimeTypeDataEnum64ValueTable");
            globalRuntimeTypeDataEnum64ValueTable.InitialValue = typeRuntimeTypeDataEnum64ValueTable.ToLiteral(literalsRuntimeTypeDataEnum64ValueTable.ConvertAll(l => l.Value).ToArray());

            LLType typeRuntimeTypeDataEnum64NameTable = LLModule.GetOrCreateArrayType(LLModule.GetOrCreateSignedType(32), literalsRuntimeTypeDataEnum64NameTable.Count);
            LLGlobal globalRuntimeTypeDataEnum64NameTable = LLModule.CreateGlobal(typeRuntimeTypeDataEnum64NameTable.PointerDepthPlusOne, "RuntimeTypeDataEnum64NameTable");
            globalRuntimeTypeDataEnum64NameTable.InitialValue = typeRuntimeTypeDataEnum64NameTable.ToLiteral(literalsRuntimeTypeDataEnum64NameTable.ConvertAll(l => l.Value).ToArray());

            LLType typeRuntimeTypeDataVirtualTable = LLModule.GetOrCreateArrayType(LLModule.GetOrCreateSignedType(32), handlesRuntimeTypeDataVirtualTable.Count);
            LLGlobal globalRuntimeTypeDataVirtualTable = LLModule.CreateGlobal(typeRuntimeTypeDataVirtualTable.PointerDepthPlusOne, "RuntimeTypeDataVirtualTable");
            globalRuntimeTypeDataVirtualTable.InitialValue = typeRuntimeTypeDataVirtualTable.ToLiteral(literalsRuntimeTypeDataVirtualTable.ConvertAll(l => l.Value).ToArray());
            LLGlobal globalRuntimeTypeDataVirtualTableCount = LLModule.CreateGlobal(LLModule.GetOrCreateSignedType(32), "RuntimeTypeDataVirtualTableCount");
            globalRuntimeTypeDataVirtualTableCount.InitialValue = LLLiteral.Create(LLModule.GetOrCreateSignedType(32), handlesRuntimeTypeDataVirtualTable.Count.ToString());
        }
Ejemplo n.º 6
0
        private static void BuildRuntimeMethodData(HLStringTable pStringTable)
        {
            List<LLType> fieldsRuntimeMethodData = sSystemRuntimeMethodData.Fields.Where(f => !f.IsStatic).ToList().ConvertAll(f => f.Type.LLType);
            LLType typePointer = LLModule.GetOrCreatePointerType(LLModule.GetOrCreateUnsignedType(8), 1);
            LLType typeRuntimeMethodData = LLModule.GetOrCreateStructureType("RuntimeMethodData", true, fieldsRuntimeMethodData);
            LLType typeRuntimeMethodDataTable = LLModule.GetOrCreateArrayType(typeRuntimeMethodData, sMethods.Values.Count);
            LLGlobal globalRuntimeMethodDataTable = LLModule.CreateGlobal(typeRuntimeMethodDataTable.PointerDepthPlusOne, "RuntimeMethodDataTable");
            List<LLLiteral> literalsRuntimeMethodDataTable = new List<LLLiteral>();
            List<LLFunction> functionsRuntimeMethodDataFunctionTable = new List<LLFunction>();
            List<LLLiteral> literalsRuntimeMethodDataFunctionTable = new List<LLLiteral>();

            foreach (HLMethod method in sMethods.Values.OrderBy(m => m.RuntimeMethodHandle))
            {
                int flags = 0;
                if (method.IsStatic) flags |= 1 << 0;

                string definitionName = method.Definition.Name.Value;
                int offsetName = pStringTable.Include(definitionName);

                literalsRuntimeMethodDataTable.Add(typeRuntimeMethodData.ToLiteral(method.RuntimeMethodHandle.ToString(), flags.ToString(), offsetName.ToString()));

                LLFunction function = method.LLFunction;
                functionsRuntimeMethodDataFunctionTable.Add(function);
                string literalAddress = null;
                if (method.LLFunction.Abstract) literalAddress = "null";
                else literalAddress = string.Format("bitcast({0} {1} to {2})", LLModule.GetOrCreatePointerType(function.FunctionType, 1), function, typePointer);
                literalsRuntimeMethodDataFunctionTable.Add(LLLiteral.Create(typePointer, literalAddress));
            }
            globalRuntimeMethodDataTable.InitialValue = typeRuntimeMethodDataTable.ToLiteral(literalsRuntimeMethodDataTable.ConvertAll(l => l.Value).ToArray());
            LLGlobal globalRuntimeMethodDataTableCount = LLModule.CreateGlobal(LLModule.GetOrCreateSignedType(32), "RuntimeMethodDataTableCount");
            globalRuntimeMethodDataTableCount.InitialValue = LLLiteral.Create(LLModule.GetOrCreateSignedType(32), sMethods.Values.Count.ToString());

            LLType typeRuntimeMethodDataFunctionTable = LLModule.GetOrCreateArrayType(typePointer, functionsRuntimeMethodDataFunctionTable.Count);
            LLGlobal globalRuntimeMethodDataFunctionTable = LLModule.CreateGlobal(typeRuntimeMethodDataFunctionTable.PointerDepthPlusOne, "RuntimeMethodDataFunctionTable");
            globalRuntimeMethodDataFunctionTable.InitialValue = typeRuntimeMethodDataFunctionTable.ToLiteral(literalsRuntimeMethodDataFunctionTable.ConvertAll(l => l.Value).ToArray());
            LLGlobal globalRuntimeMethodDataFunctionTableCount = LLModule.CreateGlobal(LLModule.GetOrCreateSignedType(32), "RuntimeMethodDataFunctionTableCount");
            globalRuntimeMethodDataFunctionTableCount.InitialValue = LLLiteral.Create(LLModule.GetOrCreateSignedType(32), functionsRuntimeMethodDataFunctionTable.Count.ToString());

            sRuntimeMethodDataFunctionTable = globalRuntimeMethodDataFunctionTable;
        }
Ejemplo n.º 7
0
 private static void BuildRuntimeDataStringTable(HLStringTable pStringTable)
 {
     byte[] bufferRuntimeDataStringTable = pStringTable.ToASCIIBytes();
     string[] literalsRuntimeDataStringTable = Array.ConvertAll(bufferRuntimeDataStringTable, b => b.ToString());
     LLType typeRuntimeDataStringTable = LLModule.GetOrCreateArrayType(LLModule.GetOrCreateSignedType(8), literalsRuntimeDataStringTable.Length);
     LLGlobal globalRuntimeDataStringTable = LLModule.CreateGlobal(typeRuntimeDataStringTable.PointerDepthPlusOne, "RuntimeDataStringTable");
     globalRuntimeDataStringTable.InitialValue = typeRuntimeDataStringTable.ToLiteral(literalsRuntimeDataStringTable);
 }
Ejemplo n.º 8
0
        public static void Process(string pInputPath)
        {
            sModule = Decompiler.GetCodeModelFromMetadataModel(Host, Host.LoadUnitFrom(pInputPath) as IModule, null);
            sPlatformType = Module.PlatformType;
            sRuntimeAssembly = (IAssembly)PlatformType.SystemRuntimeTypeHandle.ContainingUnitNamespace.Unit.ResolvedUnit;

            EnlistRuntimeTypes();

            EnlistRuntimeMethods();

            sEntryMethod = GetOrCreateMethod(Module.EntryPoint);

            int checkedTypes = 0;
            while (sTypes.Count != checkedTypes || sPendingMethods.Count > 0)
            {
                // STARTED: Process IStatements/IExpressions into HLInstructionBlocks/HLInstructions
                while (sPendingMethods.Count > 0) sPendingMethods.Dequeue().Process();

                // STARTED: Build virtual map for each type and include any missing override methods
                // starting at the top the chain (Object) and working down to the given type, then process
                // pending methods and repeat until no methods or types are added, as each new method may
                // add new types and methods
                checkedTypes = sTypes.Count;
                foreach (HLType type in sTypes.Values.ToList()) type.MapVirtualMethods();
            }
            // NOTE: After this point, do not GetOrCreate types and methods

            // DONE: Build virtual table and virtual index lookup from the complete virtual maps
            foreach (HLType type in sTypes.Values) type.BuildVirtualTable();

            // DONE: Determine HLType Calculated and Variable sizes
            // DONE: Layout Non-Static HLField offsets
            foreach (HLType type in sTypes.Values) type.LayoutFields();

            // NOTE: After this point, conversion to LL begins

            // DONE: Convert HLTypes to LLTypes

            BuildGCAllocate();
            BuildGCRoot();
            BuildVTableHandleLookup();
            BuildVTableFunctionLookup();
            BuildDelegateLookup();
            BuildDelegateInstance();

            BuildGlobals();

            foreach (HLMethod method in sMethods.Values.ToList()) method.BuildFunction();

            HLStringTable stringTable = new HLStringTable();

            // STARTED: Build constant global runtime type handles
            BuildRuntimeTypeHandles();

            // STARTED: Build constant global runtime type data
            BuildRuntimeTypeData(stringTable);

            // STARTED: Build constant global runtime method data
            BuildRuntimeMethodData(stringTable);

            // STARTED: Build constant global runtime data string table
            BuildRuntimeDataStringTable(stringTable);

            // STARTED: Convert HLInstructions to LLInstructions
            foreach (HLMethod method in sMethods.Values) method.Transform();

            using (StreamWriter writer = new StreamWriter(Path.ChangeExtension(pInputPath, ".ll")))
            {
                LLModule.Dump(writer);
            }
        }