Ejemplo n.º 1
0
        /*public static List<ICompiledMethod> JIT(IJITCompiler compiler, string assemblyfile, string[] functionnames, FunctionFilterDelegate functionfilter)
         * {
         *  if (functionfilter == null)
         *      functionfilter = SameAssemblyFunctionFilter;
         *
         *  if (!System.IO.File.Exists(assemblyfile))
         *      throw new Exception("Could not find program file: " + assemblyfile);
         *
         *  AssemblyDefinition asm = AssemblyFactory.GetAssembly(assemblyfile);
         *
         *  //TODO: Should support function overloading
         *  Dictionary<string, MethodDefinition> functionLookup = new Dictionary<string, MethodDefinition>();
         *  foreach (ModuleDefinition mod in asm.Modules)
         *      foreach (TypeDefinition tref in mod.Types)
         *          foreach (MethodDefinition mdef in tref.Methods)
         *              functionLookup.Add(mdef.DeclaringType.FullName + "::" + mdef.Name, mdef);
         *
         *  Dictionary<MethodReference, string> visitedMethods = new Dictionary<MethodReference, string>();
         *  List<ICompiledMethod> methods = new List<ICompiledMethod>();
         *
         *  Dictionary<Mono.Cecil.AssemblyDefinition, List<Mono.Cecil.MethodReference>> pendingWork = new Dictionary<AssemblyDefinition,List<MethodReference>>();
         *
         *  foreach (string name in functionnames)
         *  {
         *      if (!functionLookup.ContainsKey(name))
         *          throw new Exception("Unable to find function {0} in assembly {1}");
         *
         *      MethodDefinition mainfunction = functionLookup[name];
         *      IR.MethodEntry m = BuildIRTree(mainfunction);
         *      m.ResetVirtualRegisters();
         *      methods.Add(compiler.JIT(m));
         *      visitedMethods.Add(mainfunction, null);
         *
         *      var work = from x in m.FlatInstructionList
         *                 let code = x.Instruction.OpCode.Code
         *                 where code == Mono.Cecil.Cil.Code.Call || code == Mono.Cecil.Cil.Code.Calli || code == Mono.Cecil.Cil.Code.Callvirt
         *                 select ((Mono.Cecil.MethodReference)x.Instruction.Operand);
         *
         *      foreach (MethodReference mdef in work)
         *      {
         *          if (visitedMethods.ContainsKey(mdef))
         *              continue;
         *
         *          visitedMethods.Add(mdef, null);
         *
         *          if (!functionfilter(null, mainfunction, mdef))
         *              continue;
         *
         *
         *          List<Mono.Cecil.MethodReference> mr;
         *          pendingWork.TryGetValue(mdef.DeclaringType.Module.Assembly, out mr);
         *          if (mr == null)
         *              pendingWork.Add(mdef.DeclaringType.Module.Assembly, mr = new List<MethodReference>());
         *          mr.Add(mdef);
         *      }
         *
         *  }
         *
         *  return methods;
         *
         * }*/

        /// <summary>
        /// Function that compiles all functions in an assembly
        /// </summary>
        /// <param name="compiler">The compiler that will do the work</param>
        /// <param name="assemblyfile">The assembly to compile all methods for</param>
        /// <returns>A list of compiled methods</returns>
        public static List <ICompiledMethod> JIT(IJITCompiler compiler, string assemblyfile)
        {
            if (System.IO.File.Exists(assemblyfile))
            {
                AssemblyDefinition asm = LoadAssemblyFile(assemblyfile);

                List <ICompiledMethod> methods = new List <ICompiledMethod>();
                foreach (ModuleDefinition mod in asm.Modules)
                {
                    foreach (TypeDefinition tref in mod.Types)
                    {
                        foreach (MethodDefinition mdef in tref.Methods)
                        {
                            IR.MethodEntry root = BuildIRTree(mdef);
                            methods.Add(compiler.JIT(root));
                        }
                    }
                }

                return(methods);
            }
            else
            {
                throw new Exception("Could not find program file: " + assemblyfile);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Compiles a single method
 /// </summary>
 /// <param name="compiler">The compiler that will do the work</param>
 /// <param name="mdef">The method to compile</param>
 /// <returns>The compiled method</returns>
 public static ICompiledMethod JIT(IJITCompiler compiler, Mono.Cecil.MethodDefinition mdef)
 {
     return compiler.JIT(BuildIRTree(mdef));
 }
Ejemplo n.º 3
0
        /*public static List<ICompiledMethod> JIT(IJITCompiler compiler, string assemblyfile, string[] functionnames, FunctionFilterDelegate functionfilter)
        {
            if (functionfilter == null)
                functionfilter = SameAssemblyFunctionFilter;

            if (!System.IO.File.Exists(assemblyfile))
                throw new Exception("Could not find program file: " + assemblyfile);

            AssemblyDefinition asm = AssemblyFactory.GetAssembly(assemblyfile);

            //TODO: Should support function overloading
            Dictionary<string, MethodDefinition> functionLookup = new Dictionary<string, MethodDefinition>();
            foreach (ModuleDefinition mod in asm.Modules)
                foreach (TypeDefinition tref in mod.Types)
                    foreach (MethodDefinition mdef in tref.Methods)
                        functionLookup.Add(mdef.DeclaringType.FullName + "::" + mdef.Name, mdef);

            Dictionary<MethodReference, string> visitedMethods = new Dictionary<MethodReference, string>();
            List<ICompiledMethod> methods = new List<ICompiledMethod>();

            Dictionary<Mono.Cecil.AssemblyDefinition, List<Mono.Cecil.MethodReference>> pendingWork = new Dictionary<AssemblyDefinition,List<MethodReference>>();

            foreach (string name in functionnames)
            {
                if (!functionLookup.ContainsKey(name))
                    throw new Exception("Unable to find function {0} in assembly {1}");

                MethodDefinition mainfunction = functionLookup[name];
                IR.MethodEntry m = BuildIRTree(mainfunction);
                m.ResetVirtualRegisters();
                methods.Add(compiler.JIT(m));
                visitedMethods.Add(mainfunction, null);

                var work = from x in m.FlatInstructionList
                           let code = x.Instruction.OpCode.Code
                           where code == Mono.Cecil.Cil.Code.Call || code == Mono.Cecil.Cil.Code.Calli || code == Mono.Cecil.Cil.Code.Callvirt
                           select ((Mono.Cecil.MethodReference)x.Instruction.Operand);

                foreach (MethodReference mdef in work)
                {
                    if (visitedMethods.ContainsKey(mdef))
                        continue;

                    visitedMethods.Add(mdef, null);

                    if (!functionfilter(null, mainfunction, mdef))
                        continue;

                    List<Mono.Cecil.MethodReference> mr;
                    pendingWork.TryGetValue(mdef.DeclaringType.Module.Assembly, out mr);
                    if (mr == null)
                        pendingWork.Add(mdef.DeclaringType.Module.Assembly, mr = new List<MethodReference>());
                    mr.Add(mdef);
                }

            }

            return methods;

        }*/
        /// <summary>
        /// Function that compiles all functions in an assembly
        /// </summary>
        /// <param name="compiler">The compiler that will do the work</param>
        /// <param name="assemblyfile">The assembly to compile all methods for</param>
        /// <returns>A list of compiled methods</returns>
        public static List<ICompiledMethod> JIT(IJITCompiler compiler, string assemblyfile)
        {
            if (System.IO.File.Exists(assemblyfile))
            {
                AssemblyDefinition asm = LoadAssemblyFile(assemblyfile);

                List<ICompiledMethod> methods = new List<ICompiledMethod>();
                foreach(ModuleDefinition mod in asm.Modules)
                    foreach(TypeDefinition tref in mod.Types)
                        foreach (MethodDefinition mdef in tref.Methods)
                        {
                            IR.MethodEntry root = BuildIRTree(mdef);
                            methods.Add(compiler.JIT(root));
                        }

                return methods;
            }
            else
                throw new Exception("Could not find program file: " + assemblyfile);
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Compiles a single method
 /// </summary>
 /// <param name="compiler">The compiler that will do the work</param>
 /// <param name="mdef">The method to compile</param>
 /// <returns>The compiled method</returns>
 public static ICompiledMethod JIT(IJITCompiler compiler, Mono.Cecil.MethodDefinition mdef)
 {
     return(compiler.JIT(BuildIRTree(mdef)));
 }