private CORINFO_MODULE_STRUCT_* _methodScope; // Needed to resolve CORINFO_EH_CLAUSE tokens public void CompileMethod(MethodCodeNode methodCodeNodeNeedingCode, MethodIL methodIL = null) { try { _methodCodeNode = methodCodeNodeNeedingCode; CORINFO_METHOD_INFO methodInfo; methodIL = Get_CORINFO_METHOD_INFO(MethodBeingCompiled, methodIL, out methodInfo); // This is e.g. an "extern" method in C# without a DllImport or InternalCall. if (methodIL == null) { throw new TypeSystemException.InvalidProgramException(ExceptionStringID.InvalidProgramSpecific, MethodBeingCompiled); } _methodScope = methodInfo.scope; try { MethodDebugInformation debugInfo = _compilation.GetDebugInfo(methodIL); if (!_compilation.Options.NoLineNumbers) { IEnumerable<ILSequencePoint> ilSequencePoints = debugInfo.GetSequencePoints(); if (ilSequencePoints != null) { SetSequencePoints(ilSequencePoints); } } IEnumerable<ILLocalVariable> localVariables = debugInfo.GetLocalVariables(); if (localVariables != null) { SetLocalVariables(localVariables); } IEnumerable<string> parameters = debugInfo.GetParameterNames(); if (parameters != null) { SetParameterNames(parameters); } } catch (Exception e) { // Debug info not successfully loaded. _compilation.Log.WriteLine(e.Message + " (" + methodCodeNodeNeedingCode.ToString() + ")"); } CorInfoImpl _this = this; IntPtr exception; IntPtr nativeEntry; uint codeSize; var result = JitCompileMethod(out exception, _jit, (IntPtr)Unsafe.AsPointer(ref _this), _unmanagedCallbacks, ref methodInfo, (uint)CorJitFlag.CORJIT_FLG_CALL_GETJITFLAGS, out nativeEntry, out codeSize); if (exception != IntPtr.Zero) { if (_lastException != null) { // If we captured a managed exception, rethrow that. // TODO: might not actually be the real reason. It could be e.g. a JIT failure/bad IL that followed // an inlining attempt with a type system problem in it... throw _lastException; } // This is a failure we don't know much about. char* szMessage = GetExceptionMessage(exception); string message = szMessage != null ? new string(szMessage) : "JIT Exception"; throw new Exception(message); } if (result == CorJitResult.CORJIT_BADCODE) { throw new TypeSystemException.InvalidProgramException(ExceptionStringID.InvalidProgramSpecific, MethodBeingCompiled); } if (result != CorJitResult.CORJIT_OK) { throw new Exception("JIT Failed"); } PublishCode(); } finally { CompileMethodCleanup(); } }
private void SetDebugInformation(MethodCodeNode methodCodeNodeNeedingCode, MethodIL methodIL) { try { MethodDebugInformation debugInfo = _compilation.GetDebugInfo(methodIL); // TODO: NoLineNumbers //if (!_compilation.Options.NoLineNumbers) { IEnumerable<ILSequencePoint> ilSequencePoints = debugInfo.GetSequencePoints(); if (ilSequencePoints != null) { SetSequencePoints(ilSequencePoints); } } IEnumerable<ILLocalVariable> localVariables = debugInfo.GetLocalVariables(); if (localVariables != null) { SetLocalVariables(localVariables); } IEnumerable<string> parameters = debugInfo.GetParameterNames(); if (parameters != null) { SetParameterNames(parameters); } ArrayBuilder<uint> variableToTypeIndex = new ArrayBuilder<uint>(); var signature = MethodBeingCompiled.Signature; if (!signature.IsStatic) { TypeDesc type = MethodBeingCompiled.OwningType; variableToTypeIndex.Add(GetVariableTypeIndex(type)); } for (int i = 0; i < signature.Length; ++i) { TypeDesc type = signature[i]; variableToTypeIndex.Add(GetVariableTypeIndex(type)); } var locals = methodIL.GetLocals(); for (int i = 0; i < locals.Length; ++i) { TypeDesc type = locals[i].Type; variableToTypeIndex.Add(GetVariableTypeIndex(type)); } _variableToTypeIndex = variableToTypeIndex.ToArray(); } catch (Exception e) { // Debug info not successfully loaded. Log.WriteLine(e.Message + " (" + methodCodeNodeNeedingCode.ToString() + ")"); } }