public static HLInstructionBlock Create(HLMethod pMethod, HLLabel pStartLabel)
 {
     HLInstructionBlock block = new HLInstructionBlock();
     block.mMethod = pMethod;
     block.mStartLabel = pStartLabel;
     block.EmitLabel(pStartLabel);
     return block;
 }
Esempio n. 2
0
        public static HLInstructionBlock Create(HLMethod pMethod, HLLabel pStartLabel)
        {
            HLInstructionBlock block = new HLInstructionBlock();

            block.mMethod     = pMethod;
            block.mStartLabel = pStartLabel;
            block.EmitLabel(pStartLabel);
            return(block);
        }
Esempio n. 3
0
        public static HLMethod GetOrCreateMethod(IMethodDefinition pDefinition)
        {
            HLMethod method = null;

            if (!sMethods.TryGetValue(GetMethodSignature(pDefinition), out method))
            {
                HLType container = GetOrCreateType(pDefinition.Container);
                method = CreateMethod(container, pDefinition);
            }
            return(method);
        }
Esempio n. 4
0
        private HLLocation ProcessCreateDelegateInstanceExpression(ICreateDelegateInstance pExpression)
        {
            HLLocation locationInstance = null;

            if (pExpression.Instance != null)
            {
                locationInstance = ProcessExpression(pExpression.Instance);
            }
            HLMethod   methodCalled     = HLDomain.GetOrCreateMethod(pExpression.MethodToCallViaDelegate);
            HLLocation locationDelegate = HLTemporaryLocation.Create(CreateTemporary(HLDomain.GetOrCreateType(pExpression.Type)));

            mCurrentBlock.EmitNewDelegate(locationDelegate.Type, locationDelegate.AddressOf(), locationInstance, methodCalled, pExpression.IsVirtualDelegate);
            return(locationDelegate);
        }
Esempio n. 5
0
        private static HLMethod CreateMethod(HLType pContainer, IMethodDefinition pDefinition)
        {
            HLMethod method = new HLMethod();

            method.Definition          = pDefinition;
            method.Name                = pDefinition.Name.Value;
            method.Signature           = GetMethodSignature(pDefinition);
            sMethods[method.Signature] = method;

            method.Container = pContainer;
            pContainer.Methods.Add(method);

            method.IsStatic             = pDefinition.IsStatic;
            method.IsExternal           = pDefinition.IsExternal;
            method.IsRuntimeImplemented = pDefinition.IsRuntimeImplemented;
            method.IsAbstract           = pDefinition.IsAbstract;
            method.IsVirtual            = pDefinition.IsVirtual;
            method.IsConstructor        = pDefinition.IsConstructor;
            method.ReturnType           = GetOrCreateType(pDefinition.Type);
            if (pDefinition != Module.EntryPoint.ResolvedMethod && !pDefinition.HasExplicitThisParameter)
            {
                HLType typeImplicitThis = method.Container;
                if (typeImplicitThis.Definition.IsValueType)
                {
                    typeImplicitThis = GetOrCreateType(MutableModelHelper.GetManagedPointerTypeReference(pDefinition.Container, Host.InternFactory, pDefinition.Container));
                }
                method.Parameters.Add(new HLParameter()
                {
                    Name = "this", Type = typeImplicitThis
                });
            }
            foreach (IParameterDefinition definition in pDefinition.Parameters.OrderBy(d => d.Index))
            {
                method.Parameters.Add(CreateParameter(definition));
            }
            foreach (ILocalDefinition definition in pDefinition.Body.LocalVariables.Where(d => d.Name.Value != string.Empty))
            {
                method.Locals.Add(CreateLocal(definition));
            }
            // TODO: Finish loading from Definition

            sPendingMethods.Enqueue(method);
            return(method);
        }
Esempio n. 6
0
 private static void EnlistRuntimeMethods()
 {
     foreach (IMethodDefinition method in PlatformType.SystemString.ResolvedType.Methods.Where(d => d.IsConstructor && d.ParameterCount == 1))
     {
         IParameterDefinition parameter = method.Parameters.First();
         if (!(parameter.Type is IPointerTypeReference))
         {
             continue;
         }
         if (((IPointerTypeReference)parameter.Type).TargetType.TypeCode != PrimitiveTypeCode.Char)
         {
             continue;
         }
         sSystemStringCtorWithCharPointer = GetOrCreateMethod(method);
         break;
     }
     if (sSystemStringCtorWithCharPointer == null)
     {
         throw new MissingMethodException();
     }
 }
Esempio n. 7
0
        private HLLocation ProcessMethodCallExpression(IMethodCall pExpression)
        {
            List <HLLocation> locationsParameters = new List <HLLocation>();

            if (pExpression.ThisArgument.Type.TypeCode != PrimitiveTypeCode.Invalid)
            {
                locationsParameters.Add(ProcessExpression(pExpression.ThisArgument));
            }
            else if (pExpression.MethodToCall.IsStatic)
            {
                HLType typeContainer = null;
                if (pExpression.MethodToCall.ContainingType.ResolvedType.IsValueType)
                {
                    typeContainer = HLDomain.GetOrCreateType(MutableModelHelper.GetManagedPointerTypeReference(pExpression.MethodToCall.ContainingType, HLDomain.Host.InternFactory, pExpression.MethodToCall.ContainingType));
                }
                else
                {
                    typeContainer = HLDomain.GetOrCreateType(pExpression.MethodToCall.ContainingType);
                }
                locationsParameters.Add(HLNullLocation.Create(typeContainer));
            }
            foreach (IExpression argument in pExpression.Arguments)
            {
                locationsParameters.Add(ProcessExpression(argument));
            }

            HLLocation locationReturn = null;
            HLMethod   methodCalled   = HLDomain.GetOrCreateMethod(pExpression.MethodToCall);

            if (methodCalled.ReturnType.Definition.TypeCode != PrimitiveTypeCode.Void)
            {
                locationReturn = HLTemporaryLocation.Create(CreateTemporary(methodCalled.ReturnType));
            }
            mCurrentBlock.EmitCall(methodCalled, pExpression.IsVirtualCall, locationReturn, locationsParameters);
            return(locationReturn);
        }
Esempio n. 8
0
        private HLLocation ProcessCreateObjectInstanceExpression(ICreateObjectInstance pExpression)
        {
            HLMethod          methodCalled        = HLDomain.GetOrCreateMethod(pExpression.MethodToCall);
            HLLocation        locationThis        = HLTemporaryLocation.Create(CreateTemporary(methodCalled.Container));
            List <HLLocation> locationsParameters = new List <HLLocation>();

            if (methodCalled.Container == HLDomain.SystemString)
            {
                locationsParameters.Add(locationThis.AddressOf());
            }
            else
            {
                mCurrentBlock.EmitNewObject(methodCalled.Container, locationThis.AddressOf());
                locationsParameters.Add(locationThis);
            }

            foreach (IExpression argument in pExpression.Arguments)
            {
                locationsParameters.Add(ProcessExpression(argument));
            }

            mCurrentBlock.EmitCall(methodCalled, false, null, locationsParameters);
            return(locationThis);
        }
Esempio n. 9
0
 protected HLInstruction(HLMethod pMethod)
 {
     mMethod = pMethod;
 }
Esempio n. 10
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);
            }
        }
Esempio n. 11
0
 public void EmitNewDelegate(HLType pNewDelegateType, HLLocation pDestinationSource, HLLocation pInstanceSource, HLMethod pMethodCalled, bool pVirtual)
 {
     Emit(HLNewDelegateInstruction.Create(mMethod, pNewDelegateType, pDestinationSource, pInstanceSource, pMethodCalled, pVirtual));
 }
Esempio n. 12
0
 public void EmitCall(HLMethod pCalledMethod, bool pVirtual, HLLocation pReturnDestination, List <HLLocation> pParameterSources)
 {
     Emit(HLCallInstruction.Create(mMethod, pCalledMethod, pVirtual, pReturnDestination, pParameterSources));
 }
Esempio n. 13
0
 protected HLInstruction(HLMethod pMethod)
 {
     mMethod = pMethod;
 }
Esempio n. 14
0
 public void EmitNewDelegate(HLType pNewDelegateType, HLLocation pDestinationSource, HLLocation pInstanceSource, HLMethod pMethodCalled, bool pVirtual)
 {
     Emit(HLNewDelegateInstruction.Create(mMethod, pNewDelegateType, pDestinationSource, pInstanceSource, pMethodCalled, pVirtual));
 }
Esempio n. 15
0
 public void EmitCall(HLMethod pCalledMethod, bool pVirtual, HLLocation pReturnDestination, List<HLLocation> pParameterSources)
 {
     Emit(HLCallInstruction.Create(mMethod, pCalledMethod, pVirtual, pReturnDestination, pParameterSources));
 }
Esempio n. 16
0
 private static void EnlistRuntimeMethods()
 {
     foreach (IMethodDefinition method in PlatformType.SystemString.ResolvedType.Methods.Where(d => d.IsConstructor && d.ParameterCount == 1))
     {
         IParameterDefinition parameter = method.Parameters.First();
         if (!(parameter.Type is IPointerTypeReference)) continue;
         if (((IPointerTypeReference)parameter.Type).TargetType.TypeCode != PrimitiveTypeCode.Char) continue;
         sSystemStringCtorWithCharPointer = GetOrCreateMethod(method);
         break;
     }
     if (sSystemStringCtorWithCharPointer == null) throw new MissingMethodException();
 }
Esempio n. 17
0
        private static HLMethod CreateMethod(HLType pContainer, IMethodDefinition pDefinition)
        {
            HLMethod method = new HLMethod();
            method.Definition = pDefinition;
            method.Name = pDefinition.Name.Value;
            method.Signature = GetMethodSignature(pDefinition);
            sMethods[method.Signature] = method;

            method.Container = pContainer;
            pContainer.Methods.Add(method);

            method.IsStatic = pDefinition.IsStatic;
            method.IsExternal = pDefinition.IsExternal;
            method.IsRuntimeImplemented = pDefinition.IsRuntimeImplemented;
            method.IsAbstract = pDefinition.IsAbstract;
            method.IsVirtual = pDefinition.IsVirtual;
            method.IsConstructor = pDefinition.IsConstructor;
            method.ReturnType = GetOrCreateType(pDefinition.Type);
            if (pDefinition != Module.EntryPoint.ResolvedMethod && !pDefinition.HasExplicitThisParameter)
            {
                HLType typeImplicitThis = method.Container;
                if (typeImplicitThis.Definition.IsValueType)
                    typeImplicitThis = GetOrCreateType(MutableModelHelper.GetManagedPointerTypeReference(pDefinition.Container, Host.InternFactory, pDefinition.Container));
                method.Parameters.Add(new HLParameter() { Name = "this", Type = typeImplicitThis });
            }
            foreach (IParameterDefinition definition in pDefinition.Parameters.OrderBy(d => d.Index))
                method.Parameters.Add(CreateParameter(definition));
            foreach (ILocalDefinition definition in pDefinition.Body.LocalVariables.Where(d => d.Name.Value != string.Empty))
                method.Locals.Add(CreateLocal(definition));
            // TODO: Finish loading from Definition

            sPendingMethods.Enqueue(method);
            return method;
        }
Esempio n. 18
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);
            }
        }