/// <summary> /// Constructs a new method. /// </summary> /// <param name="unit">The target unit.</param> /// <param name="methodBase">The target method.</param> internal Method(CompileUnit unit, MethodBase methodBase) { if ((methodBase.MethodImplementationFlags & MethodImplAttributes.InternalCall) == MethodImplAttributes.InternalCall) { throw unit.CompilationContext.GetNotSupportedException( ErrorMessages.RuntimeInternalMethodNotSupported, methodBase); } if ((methodBase.MethodImplementationFlags & MethodImplAttributes.Native) == MethodImplAttributes.Native) { throw unit.CompilationContext.GetNotSupportedException( ErrorMessages.NativeMethodNotSupported, methodBase); } CompileUnit = unit; Name = unit.GetLLVMName(methodBase); MethodBase = methodBase; if (methodBase is MethodInfo) { var genericArgs = methodBase.GetGenericArguments(); for (int i = 0, e = genericArgs.Length; i < e; ++i) { if (genericArgs[i].IsGenericParameter) { throw unit.CompilationContext.GetNotSupportedException( ErrorMessages.NotSupportedGenericMethod, methodBase); } } } var functionType = unit.GetType(methodBase); LLVMFunction = AddFunction(unit.LLVMModule, Name, functionType); AddFunctionAttr(LLVMFunction, LLVMAttribute.LLVMAlwaysInlineAttribute); SetLinkage(LLVMFunction, LLVMLinkage.LLVMInternalLinkage); }
internal void Decompile(CompileUnit unit) { Debug.Assert(unit != null, "Invalid unit"); Disassemble(unit.CompilationContext); using (var decompiler = new CodeGenerator(unit, this, DisassembledMethod)) { decompiler.GenerateCode(); } // Perform a simple pass over the method RunFunctionPassManager(unit.CodeGenFunctionPassManager, LLVMFunction); }
/// <summary> /// Visits all call instructions and invokes the call handler for each /// called method. /// </summary> /// <param name="callHandler">The handler to handle called methods.</param> /// <param name="recursive">True, iff this method should be invoked on each called method recursively.</param> public void VisitCalls( Action <ILInstruction, Method> callHandler, bool recursive) { foreach (var call in DisassembledMethod.DirectCallInstructions) { var m = CompileUnit.GetMethod(call.GetArgumentAs <MethodBase>(), false); if (m == null) { continue; } callHandler(call, m); if (recursive) { m.VisitCalls(callHandler); } } }
/// <summary> /// Constructs a new compilation context. /// </summary> /// <param name="compileUnit">The associated compile unit.</param> public CompilationContext(CompileUnit compileUnit) { Debug.Assert(compileUnit != null, "Invalid compile unit"); CompileUnit = compileUnit; NotSupportedILInstructionHandler = (sender, opCode) => { switch (opCode) { case ILOpCode.Ldftn: throw GetNotSupportedException( ErrorMessages.NotSupportedILInstructionPossibleLambda, opCode.ToString()); default: throw GetNotSupportedException( ErrorMessages.NotSupportedILInstruction, opCode.ToString()); } }; }