Exemplo n.º 1
0
 public ILDisassember(MethodIL methodIL)
 {
     _methodIL = methodIL;
     _ilBytes = methodIL.GetILBytes();
     _currentOffset = 0;
     _typeNameFormatter = null;
 }
Exemplo n.º 2
0
        public InstantiatedMethodIL(MethodIL methodIL, Instantiation typeInstantiation, Instantiation methodInstantiation)
        {
            _methodIL = methodIL;

            _typeInstantiation = typeInstantiation;
            _methodInstantiation = methodInstantiation;
        }
Exemplo n.º 3
0
        public InstantiatedMethodIL(MethodDesc owningMethod, MethodIL methodIL)
        {
            Debug.Assert(methodIL.GetMethodILDefinition() == methodIL);
            Debug.Assert(owningMethod.HasInstantiation || owningMethod.OwningType.HasInstantiation);
            Debug.Assert(owningMethod.GetTypicalMethodDefinition() == methodIL.OwningMethod);
            
            _methodIL = methodIL;
            _method = owningMethod;

            _typeInstantiation = owningMethod.OwningType.Instantiation;
            _methodInstantiation = owningMethod.Instantiation;
        }
Exemplo n.º 4
0
        public override MethodIL GetMethodIL(MethodDesc method)
        {
            if (method is EcmaMethod ecmaMethod)
            {
                if (method.IsIntrinsic)
                {
                    MethodIL result = TryGetIntrinsicMethodIL(method);
                    if (result != null)
                    {
                        return(result);
                    }
                }

                MethodIL methodIL = EcmaMethodIL.Create(ecmaMethod);
                if (methodIL != null)
                {
                    return(methodIL);
                }

                return(null);
            }
            else if (method is MethodForInstantiatedType || method is InstantiatedMethod)
            {
                // Intrinsics specialized per instantiation
                if (method.IsIntrinsic)
                {
                    MethodIL methodIL = TryGetPerInstantiationIntrinsicMethodIL(method);
                    if (methodIL != null)
                    {
                        return(methodIL);
                    }
                }

                var methodDefinitionIL = GetMethodIL(method.GetTypicalMethodDefinition());
                if (methodDefinitionIL == null)
                {
                    return(null);
                }
                return(new InstantiatedMethodIL(method, methodDefinitionIL));
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 5
0
        public ILImporter(ILInterpreter interpreter, MethodDesc method, MethodIL methodIL)
        {
            _ilBytes     = methodIL.GetILBytes();
            _method      = method;
            _methodIL    = methodIL;
            _interpreter = interpreter;

            var ilExceptionRegions = methodIL.GetExceptionRegions();

            _exceptionRegions = new ExceptionRegion[methodIL.GetExceptionRegions().Length];
            for (int i = 0; i < ilExceptionRegions.Length; i++)
            {
                _exceptionRegions[i] = new ExceptionRegion()
                {
                    ILRegion = ilExceptionRegions[i]
                };
            }
        }
Exemplo n.º 6
0
        public ILImporter(WebAssemblyCodegenCompilation compilation, MethodDesc method, MethodIL methodIL, string mangledName)
        {
            Module       = compilation.Module;
            _compilation = compilation;
            _method      = method;
            _methodIL    = methodIL;
            _ilBytes     = methodIL.GetILBytes();
            _locals      = methodIL.GetLocals();

            var ilExceptionRegions = methodIL.GetExceptionRegions();

            _exceptionRegions = new ExceptionRegion[ilExceptionRegions.Length];
            for (int i = 0; i < ilExceptionRegions.Length; i++)
            {
                _exceptionRegions[i] = new ExceptionRegion()
                {
                    ILRegion = ilExceptionRegions[i]
                };
            }
            _llvmFunction = GetOrCreateLLVMFunction(mangledName);
            _builder      = LLVM.CreateBuilder();
        }
Exemplo n.º 7
0
 public static void CheckStackBalance(this MethodIL methodIL)
 {
     methodIL.ComputeMaxStack();
 }
Exemplo n.º 8
0
 public MethodDebugInformation GetDebugInfo(MethodIL methodIL)
 {
     // This method looks odd right now, but it's an extensibility point that lets us generate
     // fake debugging information for things that don't have physical symbols.
     return methodIL.GetDebugInfo();
 }
Exemplo n.º 9
0
        public ILImporter(Compilation compilation, CppWriter writer, MethodDesc method, MethodIL methodIL)
        {
            _compilation = compilation;
            _nodeFactory = _compilation.NodeFactory;

            _writer = writer;

            _method = method;
            _methodSignature = method.Signature;

            _typeSystemContext = method.Context;

            if (!_methodSignature.IsStatic)
                _thisType = method.OwningType;

            _methodIL = methodIL;

            _ilBytes = _methodIL.GetILBytes();
            _locals = _methodIL.GetLocals();

            var ilExceptionRegions = _methodIL.GetExceptionRegions();
            _exceptionRegions = new ExceptionRegion[ilExceptionRegions.Length];
            for (int i = 0; i < ilExceptionRegions.Length; i++)
            {
                _exceptionRegions[i] = new ExceptionRegion() { ILRegion = ilExceptionRegions[i] };
            }
        }
Exemplo n.º 10
0
        private MethodIL CreateMethodIL(MethodDesc method)
        {
            if (method is EcmaMethod)
            {
                // TODO: Workaround: we should special case methods with Intrinsic attribute, but since
                //       CoreLib source is still not in the repo, we have to work with what we have, which is
                //       an MCG attribute on the type itself...
                if (((MetadataType)method.OwningType).HasCustomAttribute("System.Runtime.InteropServices", "McgIntrinsicsAttribute"))
                {
                    var name = method.Name;
                    if (name == "Call")
                    {
                        return(CalliIntrinsic.EmitIL(method));
                    }
                    else
                    if (name == "AddrOf")
                    {
                        return(AddrOfIntrinsic.EmitIL(method));
                    }
                }

                if (method.IsIntrinsic)
                {
                    MethodIL result = TryGetIntrinsicMethodIL(method);
                    if (result != null)
                    {
                        return(result);
                    }
                }

                if (method.IsPInvoke)
                {
                    var pregenerated = McgInteropSupport.TryGetPregeneratedPInvoke(method);
                    if (pregenerated == null)
                    {
                        return(PInvokeILEmitter.EmitIL(method));
                    }
                    method = pregenerated;
                }

                if (method.IsRuntimeImplemented)
                {
                    MethodIL result = TryGetRuntimeImplementedMethodIL(method);
                    if (result != null)
                    {
                        return(result);
                    }
                }

                MethodIL methodIL = EcmaMethodIL.Create((EcmaMethod)method);
                if (methodIL != null)
                {
                    return(methodIL);
                }

                if (!method.IsInternalCall && !method.IsRuntimeImplemented)
                {
                    return(MissingMethodBodyILEmitter.EmitIL(method));
                }

                return(null);
            }
            else
            if (method is MethodForInstantiatedType || method is InstantiatedMethod)
            {
                if (method.IsIntrinsic && method.Name == "CreateInstanceIntrinsic")
                {
                    // CreateInstanceIntrinsic is specialized per instantiation
                    return(CreateInstanceIntrinsic.EmitIL(method));
                }

                var methodDefinitionIL = GetMethodIL(method.GetTypicalMethodDefinition());
                if (methodDefinitionIL == null)
                {
                    return(null);
                }
                return(new InstantiatedMethodIL(method, methodDefinitionIL, method.OwningType.Instantiation, method.Instantiation));
            }
            else
            if (method is ILStubMethod)
            {
                return(((ILStubMethod)method).EmitIL());
            }
            else
            if (method is ArrayMethod)
            {
                return(ArrayMethodILEmitter.EmitIL((ArrayMethod)method));
            }
            else
            {
                Debug.Assert(!(method is PInvokeTargetNativeMethod), "Who is asking for IL of PInvokeTargetNativeMethod?");
                return(null);
            }
        }
Exemplo n.º 11
0
 public MethodILDebugView(MethodIL methodIL)
 {
     _methodIL = methodIL;
 }
Exemplo n.º 12
0
        public override MethodIL GetMethodIL(MethodDesc method)
        {
            if (method is EcmaMethod ecmaMethod)
            {
                // TODO: Workaround: we should special case methods with Intrinsic attribute, but since
                //       CoreLib source is still not in the repo, we have to work with what we have, which is
                //       an MCG attribute on the type itself...
                if (((MetadataType)ecmaMethod.OwningType).HasCustomAttribute("System.Runtime.InteropServices", "McgIntrinsicsAttribute"))
                {
                    var name = ecmaMethod.Name;
                    if (name == "Call" || name.StartsWith("StdCall"))
                    {
                        return(CalliIntrinsic.EmitIL(ecmaMethod));
                    }
                    else
                    if (name == "AddrOf")
                    {
                        return(AddrOfIntrinsic.EmitIL(ecmaMethod));
                    }
                }

                if (ecmaMethod.IsIntrinsic)
                {
                    MethodIL result = TryGetIntrinsicMethodIL(ecmaMethod);
                    if (result != null)
                    {
                        return(result);
                    }
                }

                if (ecmaMethod.IsRuntimeImplemented)
                {
                    MethodIL result = TryGetRuntimeImplementedMethodIL(ecmaMethod);
                    if (result != null)
                    {
                        return(result);
                    }
                }

                // TODO: Remove: Workaround for missing ClearInitLocals transforms in CoreRT CoreLib
                bool clearInitLocals = ecmaMethod.Module == ecmaMethod.Context.SystemModule;

                MethodIL methodIL = EcmaMethodIL.Create(ecmaMethod, clearInitLocals);
                if (methodIL != null)
                {
                    return(methodIL);
                }

                return(null);
            }
            else
            if (method is MethodForInstantiatedType || method is InstantiatedMethod)
            {
                // Intrinsics specialized per instantiation
                if (method.IsIntrinsic)
                {
                    MethodIL methodIL = TryGetPerInstantiationIntrinsicMethodIL(method);
                    if (methodIL != null)
                    {
                        return(methodIL);
                    }
                }

                var methodDefinitionIL = GetMethodIL(method.GetTypicalMethodDefinition());
                if (methodDefinitionIL == null)
                {
                    return(null);
                }
                return(new InstantiatedMethodIL(method, methodDefinitionIL));
            }
            else
            if (method is ILStubMethod)
            {
                return(((ILStubMethod)method).EmitIL());
            }
            else
            if (method is ArrayMethod)
            {
                return(ArrayMethodILEmitter.EmitIL((ArrayMethod)method));
            }
            else
            {
                Debug.Assert(!(method is PInvokeTargetNativeMethod), "Who is asking for IL of PInvokeTargetNativeMethod?");
                return(null);
            }
        }
Exemplo n.º 13
0
        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();
            }
        }
Exemplo n.º 14
0
        public override MethodIL GetMethodIL(MethodDesc method)
        {
            if (method is EcmaMethod ecmaMethod)
            {
                if (ecmaMethod.IsIntrinsic)
                {
                    MethodIL result = TryGetIntrinsicMethodIL(ecmaMethod);
                    if (result != null)
                    {
                        return(result);
                    }
                }

                if (ecmaMethod.IsRuntimeImplemented)
                {
                    MethodIL result = TryGetRuntimeImplementedMethodIL(ecmaMethod);
                    if (result != null)
                    {
                        return(result);
                    }
                }

                MethodIL methodIL = EcmaMethodIL.Create(ecmaMethod);
                if (methodIL != null)
                {
                    return(methodIL);
                }

                return(null);
            }
            else
            if (method is MethodForInstantiatedType || method is InstantiatedMethod)
            {
                // Intrinsics specialized per instantiation
                if (method.IsIntrinsic)
                {
                    MethodIL methodIL = TryGetPerInstantiationIntrinsicMethodIL(method);
                    if (methodIL != null)
                    {
                        return(methodIL);
                    }
                }

                var methodDefinitionIL = GetMethodIL(method.GetTypicalMethodDefinition());
                if (methodDefinitionIL == null)
                {
                    return(null);
                }
                return(new InstantiatedMethodIL(method, methodDefinitionIL));
            }
            else
            if (method is ILStubMethod)
            {
                return(((ILStubMethod)method).EmitIL());
            }
            else
            if (method is ArrayMethod)
            {
                return(ArrayMethodILEmitter.EmitIL((ArrayMethod)method));
            }
            else
            {
                Debug.Assert(!(method is PInvokeTargetNativeMethod), "Who is asking for IL of PInvokeTargetNativeMethod?");
                return(null);
            }
        }
Exemplo n.º 15
0
        public void LogWarning(MethodIL origin, int ilOffset, DiagnosticId id, params string[] args)
        {
            MessageOrigin messageOrigin = new MessageOrigin(origin, ilOffset);

            LogWarning(messageOrigin, id, args);
        }
Exemplo n.º 16
0
        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() + ")");
            }
        }
Exemplo n.º 17
0
        private bool _isFallbackBodyCompilation; // True if we're compiling a fallback method body after compiling the real body failed

        public void CompileMethod(MethodCodeNode methodCodeNodeNeedingCode, MethodIL methodIL = null)
        {
            try
            {
                _methodCodeNode = methodCodeNodeNeedingCode;

                _isFallbackBodyCompilation = methodIL != null;

                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;
                SetDebugInformation(methodCodeNodeNeedingCode, methodIL);

                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_FLAG_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...
                        _lastException.Throw();
                    }

                    // 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();
            }
        }
Exemplo n.º 18
0
        /// <summary>
        /// Computes the maximum number of items that can be pushed onto the CIL evaluation stack.
        /// </summary>
        public static int ComputeMaxStack(this MethodIL methodIL)
        {
            const int StackHeightNotSet = Int32.MinValue;

            byte[] ilbytes       = methodIL.GetILBytes();
            int    currentOffset = 0;
            int    stackHeight   = 0;
            int    maxStack      = 0;

            // TODO: Use Span<T> for this and stackalloc the array if reasonably sized
            int[] stackHeights = new int[ilbytes.Length];
            for (int i = 0; i < stackHeights.Length; i++)
            {
                stackHeights[i] = StackHeightNotSet;
            }

            // Catch and filter clauses have a known non-zero stack height.
            foreach (ILExceptionRegion region in methodIL.GetExceptionRegions())
            {
                if (region.Kind == ILExceptionRegionKind.Catch)
                {
                    stackHeights[region.HandlerOffset] = 1;
                }
                else if (region.Kind == ILExceptionRegionKind.Filter)
                {
                    stackHeights[region.FilterOffset]  = 1;
                    stackHeights[region.HandlerOffset] = 1;
                }
            }

            while (currentOffset < ilbytes.Length)
            {
                ILOpcode opcode = (ILOpcode)ilbytes[currentOffset];
                if (opcode == ILOpcode.prefix1)
                {
                    opcode = 0x100 + (ILOpcode)ilbytes[currentOffset + 1];
                }

                // The stack height could be unknown if the previous instruction
                // was an unconditional control transfer.
                // In that case we check if we have a known stack height due to
                // this instruction being a target of a previous branch or an EH block.
                if (stackHeight == StackHeightNotSet)
                {
                    stackHeight = stackHeights[currentOffset];
                }

                // If we still don't know the stack height, ECMA-335 III.1.7.5
                // "Backward branch constraint" demands the evaluation stack be empty.
                if (stackHeight == StackHeightNotSet)
                {
                    stackHeight = 0;
                }

                // Remeber the stack height at this offset.
                Debug.Assert(stackHeights[currentOffset] == StackHeightNotSet ||
                             stackHeights[currentOffset] == stackHeight);
                stackHeights[currentOffset] = stackHeight;

                bool isVariableSize = false;
                switch (opcode)
                {
                case ILOpcode.arglist:
                case ILOpcode.dup:
                case ILOpcode.ldc_i4:
                case ILOpcode.ldc_i4_0:
                case ILOpcode.ldc_i4_1:
                case ILOpcode.ldc_i4_2:
                case ILOpcode.ldc_i4_3:
                case ILOpcode.ldc_i4_4:
                case ILOpcode.ldc_i4_5:
                case ILOpcode.ldc_i4_6:
                case ILOpcode.ldc_i4_7:
                case ILOpcode.ldc_i4_8:
                case ILOpcode.ldc_i4_m1:
                case ILOpcode.ldc_i4_s:
                case ILOpcode.ldc_i8:
                case ILOpcode.ldc_r4:
                case ILOpcode.ldc_r8:
                case ILOpcode.ldftn:
                case ILOpcode.ldnull:
                case ILOpcode.ldsfld:
                case ILOpcode.ldsflda:
                case ILOpcode.ldstr:
                case ILOpcode.ldtoken:
                case ILOpcode.ldarg:
                case ILOpcode.ldarg_0:
                case ILOpcode.ldarg_1:
                case ILOpcode.ldarg_2:
                case ILOpcode.ldarg_3:
                case ILOpcode.ldarg_s:
                case ILOpcode.ldarga:
                case ILOpcode.ldarga_s:
                case ILOpcode.ldloc:
                case ILOpcode.ldloc_0:
                case ILOpcode.ldloc_1:
                case ILOpcode.ldloc_2:
                case ILOpcode.ldloc_3:
                case ILOpcode.ldloc_s:
                case ILOpcode.ldloca:
                case ILOpcode.ldloca_s:
                case ILOpcode.sizeof_:
                    stackHeight += 1;
                    break;

                case ILOpcode.add:
                case ILOpcode.add_ovf:
                case ILOpcode.add_ovf_un:
                case ILOpcode.and:
                case ILOpcode.ceq:
                case ILOpcode.cgt:
                case ILOpcode.cgt_un:
                case ILOpcode.clt:
                case ILOpcode.clt_un:
                case ILOpcode.div:
                case ILOpcode.div_un:
                case ILOpcode.initobj:
                case ILOpcode.ldelem:
                case ILOpcode.ldelem_i:
                case ILOpcode.ldelem_i1:
                case ILOpcode.ldelem_i2:
                case ILOpcode.ldelem_i4:
                case ILOpcode.ldelem_i8:
                case ILOpcode.ldelem_r4:
                case ILOpcode.ldelem_r8:
                case ILOpcode.ldelem_ref:
                case ILOpcode.ldelem_u1:
                case ILOpcode.ldelem_u2:
                case ILOpcode.ldelem_u4:
                case ILOpcode.ldelema:
                case ILOpcode.mkrefany:
                case ILOpcode.mul:
                case ILOpcode.mul_ovf:
                case ILOpcode.mul_ovf_un:
                case ILOpcode.or:
                case ILOpcode.pop:
                case ILOpcode.rem:
                case ILOpcode.rem_un:
                case ILOpcode.shl:
                case ILOpcode.shr:
                case ILOpcode.shr_un:
                case ILOpcode.stsfld:
                case ILOpcode.sub:
                case ILOpcode.sub_ovf:
                case ILOpcode.sub_ovf_un:
                case ILOpcode.xor:
                case ILOpcode.starg:
                case ILOpcode.starg_s:
                case ILOpcode.stloc:
                case ILOpcode.stloc_0:
                case ILOpcode.stloc_1:
                case ILOpcode.stloc_2:
                case ILOpcode.stloc_3:
                case ILOpcode.stloc_s:
                    Debug.Assert(stackHeight > 0);
                    stackHeight -= 1;
                    break;

                case ILOpcode.throw_:
                    Debug.Assert(stackHeight > 0);
                    stackHeight = StackHeightNotSet;
                    break;

                case ILOpcode.br:
                case ILOpcode.leave:
                case ILOpcode.brfalse:
                case ILOpcode.brtrue:
                case ILOpcode.beq:
                case ILOpcode.bge:
                case ILOpcode.bge_un:
                case ILOpcode.bgt:
                case ILOpcode.bgt_un:
                case ILOpcode.ble:
                case ILOpcode.ble_un:
                case ILOpcode.blt:
                case ILOpcode.blt_un:
                case ILOpcode.bne_un:
                {
                    int target = currentOffset + ReadInt32(ilbytes, currentOffset + 1) + 5;

                    int  adjustment;
                    bool isConditional;
                    if (opcode == ILOpcode.br || opcode == ILOpcode.leave)
                    {
                        isConditional = false;
                        adjustment    = 0;
                    }
                    else if (opcode == ILOpcode.brfalse || opcode == ILOpcode.brtrue)
                    {
                        isConditional = true;
                        adjustment    = 1;
                    }
                    else
                    {
                        isConditional = true;
                        adjustment    = 2;
                    }

                    Debug.Assert(stackHeight >= adjustment);
                    stackHeight -= adjustment;

                    Debug.Assert(stackHeights[target] == StackHeightNotSet ||
                                 stackHeights[target] == stackHeight);

                    // Forward branch carries information about stack height at a future
                    // offset. We need to remember it.
                    if (target > currentOffset)
                    {
                        stackHeights[target] = stackHeight;
                    }

                    if (!isConditional)
                    {
                        stackHeight = StackHeightNotSet;
                    }
                }
                break;

                case ILOpcode.br_s:
                case ILOpcode.leave_s:
                case ILOpcode.brfalse_s:
                case ILOpcode.brtrue_s:
                case ILOpcode.beq_s:
                case ILOpcode.bge_s:
                case ILOpcode.bge_un_s:
                case ILOpcode.bgt_s:
                case ILOpcode.bgt_un_s:
                case ILOpcode.ble_s:
                case ILOpcode.ble_un_s:
                case ILOpcode.blt_s:
                case ILOpcode.blt_un_s:
                case ILOpcode.bne_un_s:
                {
                    int target = currentOffset + (sbyte)ilbytes[currentOffset + 1] + 2;

                    int  adjustment;
                    bool isConditional;
                    if (opcode == ILOpcode.br_s || opcode == ILOpcode.leave_s)
                    {
                        isConditional = false;
                        adjustment    = 0;
                    }
                    else if (opcode == ILOpcode.brfalse_s || opcode == ILOpcode.brtrue_s)
                    {
                        isConditional = true;
                        adjustment    = 1;
                    }
                    else
                    {
                        isConditional = true;
                        adjustment    = 2;
                    }

                    Debug.Assert(stackHeight >= adjustment);
                    stackHeight -= adjustment;

                    Debug.Assert(stackHeights[target] == StackHeightNotSet ||
                                 stackHeights[target] == stackHeight);

                    // Forward branch carries information about stack height at a future
                    // offset. We need to remember it.
                    if (target > currentOffset)
                    {
                        stackHeights[target] = stackHeight;
                    }

                    if (!isConditional)
                    {
                        stackHeight = StackHeightNotSet;
                    }
                }
                break;

                case ILOpcode.call:
                case ILOpcode.calli:
                case ILOpcode.callvirt:
                case ILOpcode.newobj:
                {
                    int             token = ReadILToken(ilbytes, currentOffset + 1);
                    object          obj   = methodIL.GetObject(token);
                    MethodSignature sig   = obj is MethodSignature ?
                                            (MethodSignature)obj :
                                            ((MethodDesc)obj).Signature;
                    int adjustment = sig.Length;
                    if (opcode == ILOpcode.newobj)
                    {
                        adjustment--;
                    }
                    else
                    {
                        if (opcode == ILOpcode.calli)
                        {
                            adjustment++;
                        }
                        if (!sig.IsStatic)
                        {
                            adjustment++;
                        }
                        if (!sig.ReturnType.IsVoid)
                        {
                            adjustment--;
                        }
                    }

                    Debug.Assert(stackHeight >= adjustment);
                    stackHeight -= adjustment;
                }
                break;

                case ILOpcode.ret:
                {
                    bool hasReturnValue = !methodIL.OwningMethod.Signature.ReturnType.IsVoid;
                    if (hasReturnValue)
                    {
                        stackHeight -= 1;
                    }

                    Debug.Assert(stackHeight == 0);

                    stackHeight = StackHeightNotSet;
                }
                break;

                case ILOpcode.cpobj:
                case ILOpcode.stfld:
                case ILOpcode.stind_i:
                case ILOpcode.stind_i1:
                case ILOpcode.stind_i2:
                case ILOpcode.stind_i4:
                case ILOpcode.stind_i8:
                case ILOpcode.stind_r4:
                case ILOpcode.stind_r8:
                case ILOpcode.stind_ref:
                case ILOpcode.stobj:
                    Debug.Assert(stackHeight > 1);
                    stackHeight -= 2;
                    break;

                case ILOpcode.cpblk:
                case ILOpcode.initblk:
                case ILOpcode.stelem:
                case ILOpcode.stelem_i:
                case ILOpcode.stelem_i1:
                case ILOpcode.stelem_i2:
                case ILOpcode.stelem_i4:
                case ILOpcode.stelem_i8:
                case ILOpcode.stelem_r4:
                case ILOpcode.stelem_r8:
                case ILOpcode.stelem_ref:
                    Debug.Assert(stackHeight > 2);
                    stackHeight -= 3;
                    break;

                case ILOpcode.break_:
                case ILOpcode.constrained:
                case ILOpcode.no:
                case ILOpcode.nop:
                case ILOpcode.readonly_:
                case ILOpcode.tail:
                case ILOpcode.unaligned:
                case ILOpcode.volatile_:
                    break;

                case ILOpcode.endfilter:
                    Debug.Assert(stackHeight > 0);
                    stackHeight = StackHeightNotSet;
                    break;

                case ILOpcode.jmp:
                case ILOpcode.rethrow:
                case ILOpcode.endfinally:
                    stackHeight = StackHeightNotSet;
                    break;

                case ILOpcode.box:
                case ILOpcode.castclass:
                case ILOpcode.ckfinite:
                case ILOpcode.conv_i:
                case ILOpcode.conv_i1:
                case ILOpcode.conv_i2:
                case ILOpcode.conv_i4:
                case ILOpcode.conv_i8:
                case ILOpcode.conv_ovf_i:
                case ILOpcode.conv_ovf_i_un:
                case ILOpcode.conv_ovf_i1:
                case ILOpcode.conv_ovf_i1_un:
                case ILOpcode.conv_ovf_i2:
                case ILOpcode.conv_ovf_i2_un:
                case ILOpcode.conv_ovf_i4:
                case ILOpcode.conv_ovf_i4_un:
                case ILOpcode.conv_ovf_i8:
                case ILOpcode.conv_ovf_i8_un:
                case ILOpcode.conv_ovf_u:
                case ILOpcode.conv_ovf_u_un:
                case ILOpcode.conv_ovf_u1:
                case ILOpcode.conv_ovf_u1_un:
                case ILOpcode.conv_ovf_u2:
                case ILOpcode.conv_ovf_u2_un:
                case ILOpcode.conv_ovf_u4:
                case ILOpcode.conv_ovf_u4_un:
                case ILOpcode.conv_ovf_u8:
                case ILOpcode.conv_ovf_u8_un:
                case ILOpcode.conv_r_un:
                case ILOpcode.conv_r4:
                case ILOpcode.conv_r8:
                case ILOpcode.conv_u:
                case ILOpcode.conv_u1:
                case ILOpcode.conv_u2:
                case ILOpcode.conv_u4:
                case ILOpcode.conv_u8:
                case ILOpcode.isinst:
                case ILOpcode.ldfld:
                case ILOpcode.ldflda:
                case ILOpcode.ldind_i:
                case ILOpcode.ldind_i1:
                case ILOpcode.ldind_i2:
                case ILOpcode.ldind_i4:
                case ILOpcode.ldind_i8:
                case ILOpcode.ldind_r4:
                case ILOpcode.ldind_r8:
                case ILOpcode.ldind_ref:
                case ILOpcode.ldind_u1:
                case ILOpcode.ldind_u2:
                case ILOpcode.ldind_u4:
                case ILOpcode.ldlen:
                case ILOpcode.ldobj:
                case ILOpcode.ldvirtftn:
                case ILOpcode.localloc:
                case ILOpcode.neg:
                case ILOpcode.newarr:
                case ILOpcode.not:
                case ILOpcode.refanytype:
                case ILOpcode.refanyval:
                case ILOpcode.unbox:
                case ILOpcode.unbox_any:
                    Debug.Assert(stackHeight > 0);
                    break;

                case ILOpcode.switch_:
                    Debug.Assert(stackHeight > 0);
                    isVariableSize = true;
                    stackHeight   -= 1;
                    currentOffset += 1 + (ReadInt32(ilbytes, currentOffset + 1) * 4) + 4;
                    break;

                default:
                    Debug.Fail("Unknown instruction");
                    break;
                }

                if (!isVariableSize)
                {
                    currentOffset += opcode.GetSize();
                }

                maxStack = Math.Max(maxStack, stackHeight);
            }

            return(maxStack);
        }
Exemplo n.º 19
0
        public void LogWarning(string text, int code, MethodIL origin, int ilOffset, string subcategory = MessageSubCategory.None)
        {
            MessageOrigin messageOrigin = new MessageOrigin(origin, ilOffset);

            LogWarning(text, code, messageOrigin, subcategory);
        }
Exemplo n.º 20
0
        private MethodIL CreateMethodIL(MethodDesc method)
        {
            if (method is EcmaMethod)
            {
                // TODO: Workaround: we should special case methods with Intrinsic attribute, but since
                //       CoreLib source is still not in the repo, we have to work with what we have, which is
                //       an MCG attribute on the type itself...
                if (((MetadataType)method.OwningType).HasCustomAttribute("System.Runtime.InteropServices", "McgIntrinsicsAttribute"))
                {
                    var name = method.Name;
                    if (name == "Call")
                    {
                        return(CalliIntrinsic.EmitIL(method));
                    }
                    else
                    if (name == "AddrOf")
                    {
                        return(AddrOfIntrinsic.EmitIL(method));
                    }
                }

                if (method.IsIntrinsic)
                {
                    MethodIL result = TryGetIntrinsicMethodIL(method);
                    if (result != null)
                    {
                        return(result);
                    }
                }

                if (method.IsPInvoke)
                {
                    var pregenerated = McgInteropSupport.TryGetPregeneratedPInvoke(method);
                    if (pregenerated == null)
                    {
                        return(PInvokeMarshallingILEmitter.EmitIL(method));
                    }
                    method = pregenerated;
                }

                return(EcmaMethodIL.Create((EcmaMethod)method));
            }
            else
            if (method is MethodForInstantiatedType)
            {
                var methodDefinitionIL = GetMethodIL(method.GetTypicalMethodDefinition());
                if (methodDefinitionIL == null)
                {
                    return(null);
                }
                return(new InstantiatedMethodIL(methodDefinitionIL, method.OwningType.Instantiation, new Instantiation()));
            }
            else
            if (method is InstantiatedMethod)
            {
                var methodDefinitionIL = GetMethodIL(method.GetMethodDefinition());
                if (methodDefinitionIL == null)
                {
                    return(null);
                }
                return(new InstantiatedMethodIL(methodDefinitionIL, new Instantiation(), method.Instantiation));
            }
            else
            if (method is ILStubMethod)
            {
                return(((ILStubMethod)method).EmitIL());
            }
            else
            if (method is ArrayMethod)
            {
                return(ArrayMethodILEmitter.EmitIL((ArrayMethod)method));
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 21
0
        private MethodIL Get_CORINFO_METHOD_INFO(MethodDesc method, MethodIL methodIL, out CORINFO_METHOD_INFO methodInfo)
        {
            // MethodIL can be provided externally for the case of a method whose IL was replaced because we couldn't compile it.
            if (methodIL == null)
                methodIL = _compilation.GetMethodIL(method);

            if (methodIL == null)
            {
                methodInfo = default(CORINFO_METHOD_INFO);
                return null;
            }

            methodInfo.ftn = ObjectToHandle(method);
            methodInfo.scope = (CORINFO_MODULE_STRUCT_*)ObjectToHandle(methodIL);
            var ilCode = methodIL.GetILBytes();
            methodInfo.ILCode = (byte*)GetPin(ilCode);
            methodInfo.ILCodeSize = (uint)ilCode.Length;
            methodInfo.maxStack = (uint)methodIL.MaxStack;
            methodInfo.EHcount = (uint)methodIL.GetExceptionRegions().Length;
            methodInfo.options = methodIL.IsInitLocals ? CorInfoOptions.CORINFO_OPT_INIT_LOCALS : (CorInfoOptions)0;
            methodInfo.regionKind = CorInfoRegionKind.CORINFO_REGION_NONE;

            Get_CORINFO_SIG_INFO(method, out methodInfo.args);
            Get_CORINFO_SIG_INFO(methodIL.GetLocals(), out methodInfo.locals);

            return methodIL;
        }
Exemplo n.º 22
0
 public MethodILDebugView(MethodIL methodIL)
 {
     _methodIL = methodIL;
 }