Ejemplo n.º 1
0
        public virtual bool ApplyTransformation(RecompilerContext context, CilCompilationUnit unit)
        {
            bool changed = false;

            while (unit.AcceptVisitor(this))
            {
                changed = true;
                // Repeat until no more changes.
            }

            return(changed);
        }
Ejemplo n.º 2
0
        public CilMethodBody Compile(MethodDefinition method, CilCompilationUnit unit)
        {
            var methodBody = new CilMethodBody(method);

            var context = new CodeGenerationContext(methodBody, _constants, unit.FlagVariable, _flagHelperType);

            var cilGenerator = new CilCodeGenerator(context);

            context.CodeGenerator = cilGenerator;

            // Traverse and recompile the AST.
            methodBody.Instructions.AddRange(unit.AcceptVisitor(cilGenerator));

            // Add variables to the method body.
            if (context.Variables.Count > 0)
            {
                foreach (var variable in context.Variables.Values)
                {
                    methodBody.LocalVariables.Add(variable);
                }
                methodBody.InitializeLocals = true;
            }

            methodBody.Instructions.OptimizeMacros();

            // Add all generated exception handlers to the method body.
            var handlers = context.ExceptionHandlers.Values.ToList();

            handlers.Sort(new EHComparer());
            foreach (var handler in handlers)
            {
                if (EnableExceptionHandlerValidation)
                {
                    AssertValidityExceptionHandler(method, handler);
                }
                methodBody.ExceptionHandlers.Add(handler);
            }

            if (!EnableStackVerification)
            {
                methodBody.ComputeMaxStackOnBuild = false;
                methodBody.MaxStack = ushort.MaxValue;
            }

            return(methodBody);
        }