예제 #1
0
        /// <summary>
        /// Loads the target architecture library and fills in the TargetILOps, MethodStartOp, MethodEndOp and StackSwitchOp
        /// fields.
        /// </summary>
        /// <returns>True if fully loaded without error. Otherwise, false.</returns>
        private static bool LoadTargetArchitecture()
        {
            bool OK = false;

            try
            {
                string CurrentAssemblyDir = System.IO.Path.GetDirectoryName(typeof(TargetArchitecture).Assembly.Location);
                string fileName           = null;
                switch (Options.TargetArchitecture)
                {
                case "x86":
                {
                    fileName = System.IO.Path.Combine(CurrentAssemblyDir, @"Drivers.Compiler.Architectures.x86.dll");
                    OK       = true;
                }
                break;

                case "mips":
                {
                    fileName = System.IO.Path.Combine(CurrentAssemblyDir, @"Drivers.Compiler.Architectures.MIPS32.dll");
                    OK       = true;
                }
                break;

                default:
                    OK = false;
                    throw new ArgumentException("Unrecognised target architecture!");
                }

                if (OK)
                {
                    fileName = System.IO.Path.GetFullPath(fileName);
                    TargetArchitectureAssembly = System.Reflection.Assembly.LoadFrom(fileName);
                }

                if (OK)
                {
                    Type[] AllTypes = TargetArchitectureAssembly.GetTypes();
                    foreach (Type aType in AllTypes)
                    {
                        if (aType.IsSubclassOf(typeof(IL.ILOp)))
                        {
                            if (aType.IsSubclassOf(typeof(MethodStart)))
                            {
                                MethodStartOp = (MethodStart)Activator.CreateInstance(aType);
                            }
                            else if (aType.IsSubclassOf(typeof(MethodEnd)))
                            {
                                MethodEndOp = (MethodEnd)Activator.CreateInstance(aType);
                            }
                            else if (aType.IsSubclassOf(typeof(StackSwitch)))
                            {
                                StackSwitchOp = (StackSwitch)Activator.CreateInstance(aType);
                            }
                            else
                            {
                                IL.ILOps.ILOpTargetAttribute[] targetAttrs = (IL.ILOps.ILOpTargetAttribute[])aType.GetCustomAttributes(typeof(IL.ILOps.ILOpTargetAttribute), true);
                                if (targetAttrs == null || targetAttrs.Length == 0)
                                {
                                    throw new Exception("ILScanner could not load target architecture ILOp because target attribute was not specified!");
                                }
                                else
                                {
                                    foreach (IL.ILOps.ILOpTargetAttribute targetAttr in targetAttrs)
                                    {
                                        TargetILOps.Add(targetAttr.Target, (IL.ILOp)Activator.CreateInstance(aType));
                                    }
                                }
                            }
                        }
                        else if (aType.IsSubclassOf(typeof(ASM.ASMOp)))
                        {
                            ASMOpTargetAttribute[] targetAttrs = (ASMOpTargetAttribute[])aType.GetCustomAttributes(typeof(ASMOpTargetAttribute), true);
                            foreach (ASMOpTargetAttribute targetAttr in targetAttrs)
                            {
                                TargetASMOps.Add(targetAttr.Target, aType);
                            }
                        }
                        else if (aType.IsSubclassOf(typeof(TargetArchitectureFunctions)))
                        {
                            TargetFunctions = (TargetArchitectureFunctions)Activator.CreateInstance(aType);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                OK = false;
                Logger.LogError(Errors.ILCompiler_LoadTargetArchError_ErrorCode, "", 0,
                                string.Format(Errors.ErrorMessages[Errors.ILCompiler_LoadTargetArchError_ErrorCode],
                                              ex.Message));
            }

            return(OK);
        }
예제 #2
0
        /// <summary>
        /// Loads the target architecture library and fills in the TargetILOps, MethodStartOp, MethodEndOp and StackSwitchOp
        /// fields.
        /// </summary>
        /// <returns>True if fully loaded without error. Otherwise, false.</returns>
        private static bool LoadTargetArchitecture()
        {
            bool OK = false;

            try
            {
                string CurrentAssemblyDir = System.IO.Path.GetDirectoryName(typeof(TargetArchitecture).Assembly.Location);
                string fileName = null;
                switch (Options.TargetArchitecture)
                {
                    case "x86":
                        {
                            fileName = System.IO.Path.Combine(CurrentAssemblyDir, @"Drivers.Compiler.Architectures.x86.dll");
                            OK = true;
                        }
                        break;
                    case "mips":
                        {
                            fileName = System.IO.Path.Combine(CurrentAssemblyDir, @"Drivers.Compiler.Architectures.MIPS32.dll");
                            OK = true;
                        }
                        break;
                    default:
                        OK = false;
                        throw new ArgumentException("Unrecognised target architecture!");
                }

                if (OK)
                {
                    fileName = System.IO.Path.GetFullPath(fileName);
                    TargetArchitectureAssembly = System.Reflection.Assembly.LoadFrom(fileName);
                }
                
                if (OK)
                {
                    Type[] AllTypes = TargetArchitectureAssembly.GetTypes();
                    foreach (Type aType in AllTypes)
                    {
                        if (aType.IsSubclassOf(typeof(IL.ILOp)))
                        {
                            if (aType.IsSubclassOf(typeof(MethodStart)))
                            {
                                MethodStartOp = (MethodStart)Activator.CreateInstance(aType);
                            }
                            else if (aType.IsSubclassOf(typeof(MethodEnd)))
                            {
                                MethodEndOp = (MethodEnd)Activator.CreateInstance(aType);
                            }
                            else if (aType.IsSubclassOf(typeof(StackSwitch)))
                            {
                                StackSwitchOp = (StackSwitch)Activator.CreateInstance(aType);
                            }
                            else
                            {
                                IL.ILOps.ILOpTargetAttribute[] targetAttrs = (IL.ILOps.ILOpTargetAttribute[])aType.GetCustomAttributes(typeof(IL.ILOps.ILOpTargetAttribute), true);
                                if (targetAttrs == null || targetAttrs.Length == 0)
                                {
                                    throw new Exception("ILScanner could not load target architecture ILOp because target attribute was not specified!");
                                }
                                else
                                {
                                    foreach (IL.ILOps.ILOpTargetAttribute targetAttr in targetAttrs)
                                    {
                                        TargetILOps.Add(targetAttr.Target, (IL.ILOp)Activator.CreateInstance(aType));
                                    }
                                }
                            }
                        }
                        else if (aType.IsSubclassOf(typeof(ASM.ASMOp)))
                        {
                            ASMOpTargetAttribute[] targetAttrs = (ASMOpTargetAttribute[])aType.GetCustomAttributes(typeof(ASMOpTargetAttribute), true);
                            foreach (ASMOpTargetAttribute targetAttr in targetAttrs)
                            {
                                TargetASMOps.Add(targetAttr.Target, aType);
                            }
                        }
                        else if (aType.IsSubclassOf(typeof(TargetArchitectureFunctions)))
                        {
                            TargetFunctions = (TargetArchitectureFunctions)Activator.CreateInstance(aType);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                OK = false;
                Logger.LogError(Errors.ILCompiler_LoadTargetArchError_ErrorCode, "", 0,
                    string.Format(Errors.ErrorMessages[Errors.ILCompiler_LoadTargetArchError_ErrorCode],
                                    ex.Message));
            }

            return OK;
        }