Ejemplo n.º 1
0
 private static int GetBaseCommandId(string statement)
 {
     if (Namespace.IsMatch(statement))
     {
         return(99);
     }
     if (ClassStart.IsMatch(statement))
     {
         return(19);
     }
     if (IfStart.IsMatch(statement))
     {
         return(1);
     }
     if (ElseStart.IsMatch(statement.Trim()))
     {
         return(10);
     }
     if (LoopStart.IsMatch(statement))
     {
         return(3);
     }
     if (MethodStart.IsMatch(statement) && !ExcludeMethodDefRegex.IsMatch(statement))
     {
         return(8);
     }
     if (InterfaceStart.IsMatch(statement))
     {
         return(88);                                   // set end interface 89
     }
     if (EnumStart.IsMatch(statement))
     {
         return(78);                              // set end enum 79
     }
     if (SwitchStartRegex.IsMatch(statement))
     {
         return(58);                                     // set end switch 59
     }
     if (StructStart.IsMatch(statement))
     {
         return(48);                                // set end struct 49
     }
     return(0);
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 3
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;
        }