예제 #1
0
        private void EmitMethodBody(MethodBuilder methodBuilder, ConstructorBuilder constructorBuilder, Cci.IMethodBody body)
        {
            byte[] byteCode = body.IL.Copy(0, body.IL.Length);
            int[] tokenFixupOffsets = SubstituteTokens(byteCode);

            // prepare signature:
            var sigHelper = SignatureHelper.GetLocalVarSigHelper(_builder);
            int localCount = 0;
            foreach (var local in body.LocalVariables)
            {
                var type = ResolveType(local.Type);
                if (local.IsReference)
                {
                    type = type.MakeByRefType();
                }

                if (local.CustomModifiers.Any())
                {
                    if (local.IsPinned)
                    {
                        throw new NotSupportedException("Ref.Emit limitation");
                    }

                    Type[] reqMods, optMods;
                    ResolveCustomModifiers(local.CustomModifiers, out reqMods, out optMods);
                    sigHelper.AddArgument(type, reqMods, optMods);
                }
                else
                {
                    sigHelper.AddArgument(type, local.IsPinned);
                }

                localCount++;
            }

            byte[] signature = sigHelper.GetSignature();

            // prepare exception handlers:
            var regions = body.ExceptionRegions;
            var handlers = new ExceptionHandler[regions.Length];
            int i = 0;
            foreach (var info in regions)
            {
                handlers[i++] = new ExceptionHandler(
                    tryOffset: (int)info.TryStartOffset,
                    tryLength: (int)info.TryEndOffset - (int)info.TryStartOffset,
                    filterOffset: (int)info.FilterDecisionStartOffset,
                    handlerOffset: (int)info.HandlerStartOffset,
                    handlerLength: (int)info.HandlerEndOffset - (int)info.HandlerStartOffset,
                    kind: (ExceptionHandlingClauseOptions)info.HandlerKind,
                    exceptionTypeToken: (info.HandlerKind == ExceptionRegionKind.Catch) ? GetToken(ResolveType(info.ExceptionType)) : 0);
            }

            if (methodBuilder != null)
            {
                methodBuilder.InitLocals = body.LocalsAreZeroed;
                methodBuilder.SetMethodBody(byteCode, body.MaxStack, signature, handlers, tokenFixupOffsets);
            }
            else
            {
                constructorBuilder.InitLocals = body.LocalsAreZeroed;
                constructorBuilder.SetMethodBody(byteCode, body.MaxStack, signature, handlers, tokenFixupOffsets);
            }
        }