Exemplo n.º 1
0
 public ILStructure(PEFile module, MethodDefinitionHandle handle, MetadataGenericContext genericContext, ILStructureType type, int startOffset, int endOffset, ExceptionRegion handler = default)
 {
     Debug.Assert(startOffset < endOffset);
     this.Module           = module;
     this.MethodHandle     = handle;
     this.GenericContext   = genericContext;
     this.Type             = type;
     this.StartOffset      = startOffset;
     this.EndOffset        = endOffset;
     this.ExceptionHandler = handler;
 }
 public _ExceptionRegionInfo(Module aModule, ExceptionRegion aExceptionRegion)
 {
     Module          = aModule;
     ExceptionRegion = aExceptionRegion;
     HandlerOffset   = aExceptionRegion.HandlerOffset;
     HandlerLength   = aExceptionRegion.HandlerLength;
     TryOffset       = aExceptionRegion.TryOffset;
     TryLength       = aExceptionRegion.TryLength;
     FilterOffset    = aExceptionRegion.FilterOffset;
     Kind            = aExceptionRegion.Kind;
 }
Exemplo n.º 3
0
        public ILImporter(ILScanner compilation, MethodDesc method, MethodIL methodIL = null)
        {
            if (methodIL == null)
            {
                methodIL = compilation.GetMethodIL(method);
            }
            else
            {
                _isFallbackBodyCompilation = true;
            }

            // This is e.g. an "extern" method in C# without a DllImport or InternalCall.
            if (methodIL == null)
            {
                ThrowHelper.ThrowInvalidProgramException(ExceptionStringID.InvalidProgramSpecific, method);
            }

            _compilation = compilation;
            _factory     = (ILScanNodeFactory)compilation.NodeFactory;

            _ilBytes = methodIL.GetILBytes();

            _canonMethodIL = methodIL;

            // Get the runtime determined method IL so that this works right in shared code
            // and tokens in shared code resolve to runtime determined types.
            MethodIL uninstantiatiedMethodIL = methodIL.GetMethodILDefinition();

            if (methodIL != uninstantiatiedMethodIL)
            {
                MethodDesc sharedMethod = method.GetSharedRuntimeFormMethodTarget();
                _methodIL = new InstantiatedMethodIL(sharedMethod, uninstantiatiedMethodIL);
            }
            else
            {
                _methodIL = methodIL;
            }

            _canonMethod = method;

            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.º 4
0
        public ILImporter(LLVMBuilderRef builder, WebAssemblyCodegenCompilation compilation, LLVMModuleRef module, LLVMValueRef helperFunc, MethodDesc delegateCtor)
        {
            this._builder        = builder;
            this._compilation    = compilation;
            this.Module          = module;
            this._currentFunclet = helperFunc;
            _locals = new LocalVariableDefinition[0];
            if (delegateCtor == null)
            {
                _signature = new MethodSignature(MethodSignatureFlags.None, 0, GetWellKnownType(WellKnownType.Void),
                                                 new TypeDesc[0]);
            }
            else
            {
                _signature = delegateCtor.Signature;
                _argSlots  = new LLVMValueRef[_signature.Length];
                int signatureIndex = 2; // past hidden param
                int thisOffset     = 0;
                if (!_signature.IsStatic)
                {
                    thisOffset = 1;
                }
                for (int i = 0; i < _signature.Length; i++)
                {
                    if (CanStoreTypeOnStack(_signature[i]))
                    {
                        LLVMValueRef storageAddr;
                        LLVMValueRef argValue = helperFunc.GetParam((uint)signatureIndex);

                        // The caller will always pass the argument on the stack. If this function doesn't have
                        // EH, we can put it in an alloca for efficiency and better debugging. Otherwise,
                        // copy it to the shadow stack so funclets can find it
                        int    argOffset = i + thisOffset;
                        string argName   = $"arg{argOffset}_";
                        storageAddr  = _builder.BuildAlloca(GetLLVMTypeForTypeDesc(_signature[i]), argName);
                        _argSlots[i] = storageAddr;
                        _builder.BuildStore(argValue, storageAddr);
                        signatureIndex++;
                    }
                }
            }
            _thisType         = GetWellKnownType(WellKnownType.Void);
            _pointerSize      = compilation.NodeFactory.Target.PointerSize;
            _exceptionRegions = new ExceptionRegion[0];
        }
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 ILStructure(PEFile module, MethodDefinitionHandle handle, MetadataGenericContext genericContext, MethodBodyBlock body)
            : this(module, handle, genericContext, ILStructureType.Root, 0, body.GetILReader().Length)
        {
            // Build the tree of exception structures:
            for (int i = 0; i < body.ExceptionRegions.Length; i++)
            {
                ExceptionRegion eh = body.ExceptionRegions[i];
                if (!body.ExceptionRegions.Take(i).Any(oldEh => oldEh.TryOffset == eh.TryOffset && oldEh.TryLength == eh.TryLength))
                {
                    AddNestedStructure(new ILStructure(module, handle, genericContext, ILStructureType.Try, eh.TryOffset, eh.TryOffset + eh.TryLength, eh));
                }
                if (eh.Kind == ExceptionRegionKind.Filter)
                {
                    AddNestedStructure(new ILStructure(module, handle, genericContext, ILStructureType.Filter, eh.FilterOffset, eh.HandlerOffset, eh));
                }
                AddNestedStructure(new ILStructure(module, handle, genericContext, ILStructureType.Handler, eh.HandlerOffset, eh.HandlerOffset + eh.HandlerLength, eh));
            }
            // Very simple loop detection: look for backward branches
            (var allBranches, var isAfterUnconditionalBranch) = FindAllBranches(body.GetILReader());
            // We go through the branches in reverse so that we find the biggest possible loop boundary first (think loops with "continue;")
            for (int i = allBranches.Count - 1; i >= 0; i--)
            {
                int loopEnd   = allBranches[i].Source.End;
                int loopStart = allBranches[i].Target;
                if (loopStart < loopEnd)
                {
                    // We found a backward branch. This is a potential loop.
                    // Check that is has only one entry point:
                    int entryPoint = -1;

                    // entry point is first instruction in loop if prev inst isn't an unconditional branch
                    if (loopStart > 0 && !isAfterUnconditionalBranch[loopStart])
                    {
                        entryPoint = allBranches[i].Target;
                    }

                    bool multipleEntryPoints = false;
                    foreach (var branch in allBranches)
                    {
                        if (branch.Source.Start < loopStart || branch.Source.Start >= loopEnd)
                        {
                            if (loopStart <= branch.Target && branch.Target < loopEnd)
                            {
                                // jump from outside the loop into the loop
                                if (entryPoint < 0)
                                {
                                    entryPoint = branch.Target;
                                }
                                else if (branch.Target != entryPoint)
                                {
                                    multipleEntryPoints = true;
                                }
                            }
                        }
                    }
                    if (!multipleEntryPoints)
                    {
                        AddNestedStructure(new ILStructure(module, handle, genericContext, ILStructureType.Loop, loopStart, loopEnd, entryPoint));
                    }
                }
            }
            SortChildren();
        }
 /// <summary>
 /// </summary>
 /// <param name="exceptionRegion">
 /// </param>
 /// <param name="catchType">
 /// </param>
 /// <param name="genericContext">
 /// </param>
 internal MetadataExceptionHandlingClauseAdapter(ExceptionRegion exceptionRegion, TypeSymbol catchType, IGenericContext genericContext)
     : this(exceptionRegion, catchType)
 {
     this.GenericContext = genericContext;
 }
 /// <summary>
 /// </summary>
 /// <param name="exceptionRegion">
 /// </param>
 /// <param name="catchType">
 /// </param>
 internal MetadataExceptionHandlingClauseAdapter(ExceptionRegion exceptionRegion, TypeSymbol catchType)
 {
     this.exceptionRegion  = exceptionRegion;
     this.catchType        = catchType;
     this.lazyFinallyJumps = new Lazy <IList <string> >(() => new List <string>());
 }
Exemplo n.º 10
0
 private static string AddReturnLabel(ExceptionRegion r)
 {
     r.ReturnLabels++;
     return r.ReturnLabels.ToStringInvariant();
 }
 /// <summary>
 /// Closes the currently open exception clause.
 /// </summary>
 /// <param name="exceptionRegion"> The exception region to modify. </param>
 private void EndCurrentClause(ExceptionRegion exceptionRegion)
 {
     if (exceptionRegion.Clauses.Count == 0)
     {
         // End the try block.
         Leave(exceptionRegion.EndLabel);
         exceptionRegion.TryLength = this.offset - exceptionRegion.Start;
     }
     else
     {
         var latestClause = exceptionRegion.Clauses[exceptionRegion.Clauses.Count - 1];
         switch (latestClause.Type)
         {
             case ExceptionClauseType.Catch:
                 Leave(exceptionRegion.EndLabel);
                 break;
             case ExceptionClauseType.Finally:
                 EndFinally();
                 break;
             case ExceptionClauseType.Filter:
                 break;
             case ExceptionClauseType.Fault:
                 EndFault();
                 break;
         }
         latestClause.ILLength = this.offset - latestClause.ILStart;
     }
 }
        /// <summary>
        /// Begins a try-catch-finally block.
        /// </summary>
        public override void BeginExceptionBlock()
        {
            // Create a new active exception region stack, if necessary.
            if (this.activeExceptionRegions == null)
                this.activeExceptionRegions = new Stack<ExceptionRegion>();

            // Create a new exception region.
            var region = new ExceptionRegion();
            region.Start = this.offset;
            region.EndLabel = this.CreateLabel();
            region.Clauses = new List<ExceptionClause>(3);

            // Push the exception region to the stack.
            this.activeExceptionRegions.Push(region);
        }
Exemplo n.º 13
0
 internal static Tuple cil_handler_type(ExceptionRegion region, Type t) =>
 new Tuple("cil_handler_type", region, t);
Exemplo n.º 14
0
 internal static Tuple cil_handler_filter(ExceptionRegion region, Instruction filter_start) =>
 new Tuple("cil_handler_filter", region, filter_start);
Exemplo n.º 15
0
 internal static Tuple cil_handler(ExceptionRegion region, MethodImplementation method, int index, int kind,
                                   Instruction region_start,
                                   Instruction region_end,
                                   Instruction handler_start) =>
 new Tuple("cil_handler", region, method, index, kind, region_start, region_end, handler_start);
Exemplo n.º 16
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.º 17
0
 private static string AddReturnLabel(ExceptionRegion r)
 {
     r.ReturnLabels++;
     return IntToString(r.ReturnLabels);
 }
Exemplo n.º 18
0
        public static Type GetCatchType(Module aModule, ExceptionRegion aRegion)
        {
            string xLocation = aModule.Assembly.Location;
            var    xReader   = MetadataHelper.TryGetReader(xLocation);

            switch (aRegion.CatchType.Kind)
            {
            case HandleKind.TypeReference:
                return(MetadataHelper.GetTypeFromReference(xReader, aModule,
                                                           (TypeReferenceHandle)aRegion.CatchType, 0));

            case HandleKind.TypeDefinition:
                return(aModule.ResolveType(MetadataTokens.GetToken(aRegion.CatchType)));

            case HandleKind.FieldDefinition:
                break;

            case HandleKind.MethodDefinition:
                break;

            case HandleKind.Parameter:
                break;

            case HandleKind.InterfaceImplementation:
                break;

            case HandleKind.MemberReference:
                break;

            case HandleKind.Constant:
                break;

            case HandleKind.CustomAttribute:
                break;

            case HandleKind.DeclarativeSecurityAttribute:
                break;

            case HandleKind.StandaloneSignature:
                break;

            case HandleKind.EventDefinition:
                break;

            case HandleKind.PropertyDefinition:
                break;

            case HandleKind.MethodImplementation:
                break;

            case HandleKind.ModuleReference:
                break;

            case HandleKind.TypeSpecification:
                break;

            case HandleKind.AssemblyDefinition:
                break;

            case HandleKind.AssemblyFile:
                break;

            case HandleKind.AssemblyReference:
                break;

            case HandleKind.ExportedType:
                break;

            case HandleKind.GenericParameter:
                break;

            case HandleKind.MethodSpecification:
                break;

            case HandleKind.GenericParameterConstraint:
                break;

            case HandleKind.MethodDebugInformation:
                break;

            case HandleKind.CustomDebugInformation:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
            throw new NotImplementedException();
        }
Exemplo n.º 19
0
 static string AddReturnLabel(ExceptionRegion r)
 {
     r.ReturnLabels++;
     return r.ReturnLabels.ToString();
 }