|
| 1 | +using CSharpLLVM.Compilation; |
| 2 | +using Mono.Cecil; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Reflection; |
| 6 | + |
| 7 | +namespace CSharpLLVM.Runtime.CIL |
| 8 | +{ |
| 9 | + class CILRuntimeMethodCompiler |
| 10 | + { |
| 11 | + private Compiler mCompiler; |
| 12 | + private Dictionary<string, Tuple<IRuntimeHandler, MethodInfo>> mLookup = new Dictionary<string, Tuple<IRuntimeHandler, MethodInfo>>(); |
| 13 | + |
| 14 | + /// <summary> |
| 15 | + /// Creates a new CILRuntimeMethodCompiler. |
| 16 | + /// </summary> |
| 17 | + /// <param name="compiler">The compiler.</param> |
| 18 | + public CILRuntimeMethodCompiler(Compiler compiler) |
| 19 | + { |
| 20 | + mCompiler = compiler; |
| 21 | + initHandlers(); |
| 22 | + } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Initializes the handlers. |
| 26 | + /// </summary> |
| 27 | + private void initHandlers() |
| 28 | + { |
| 29 | + // Load code emitters. |
| 30 | + Type[] types = Assembly.GetExecutingAssembly().GetTypes(); |
| 31 | + foreach (Type type in types) |
| 32 | + { |
| 33 | + // Check and get handler. |
| 34 | + RuntimeHandlerAttribute classAttrib = (RuntimeHandlerAttribute)type.GetCustomAttribute(typeof(RuntimeHandlerAttribute)); |
| 35 | + if (classAttrib == null) |
| 36 | + continue; |
| 37 | + |
| 38 | + IRuntimeHandler handler = (IRuntimeHandler)Activator.CreateInstance(type); |
| 39 | + MethodInfo[] methods = type.GetMethods(); |
| 40 | + foreach (MethodInfo method in methods) |
| 41 | + { |
| 42 | + // Check and get method handler. |
| 43 | + MethodHandlerAttribute methodAttrib = (MethodHandlerAttribute)method.GetCustomAttribute(typeof(MethodHandlerAttribute)); |
| 44 | + if (methodAttrib == null) |
| 45 | + continue; |
| 46 | + |
| 47 | + string fullName = string.Format("{0}.{1}", classAttrib.TypeName, methodAttrib.MethodName); |
| 48 | + mLookup.Add(fullName, new Tuple<IRuntimeHandler, MethodInfo>(handler, method)); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// Returns true if we must handle the method, false if we can ignore it. |
| 55 | + /// </summary> |
| 56 | + /// <param name="method">The runtime method.</param> |
| 57 | + /// <returns>True if we must handle the method, false if we can ignore it.</returns> |
| 58 | + public bool MustHandleMethod(MethodDefinition method) |
| 59 | + { |
| 60 | + string fullName = string.Format("{0}.{1}", method.DeclaringType.BaseType, method.Name); |
| 61 | + return mLookup.ContainsKey(fullName); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments