Exemple #1
0
        static public void EmitCode()
        {
            if (type_container_resolve_order != null)
            {
                foreach (TypeContainer tc in type_container_resolve_order)
                {
                    tc.EmitType();
                }

                if (RootContext.ToplevelTypes.Compiler.Report.Errors > 0)
                {
                    return;
                }

                foreach (TypeContainer tc in type_container_resolve_order)
                {
                    tc.VerifyMembers();
                }
            }

            if (root.Delegates != null)
            {
                foreach (Delegate d in root.Delegates)
                {
                    d.Emit();
                }
            }

            CodeGen.Assembly.Emit(root);
            root.Emit();
        }
Exemple #2
0
        CompiledMethod CompileBlock(Class host, Undo undo, Report Report)
        {
            string current_debug_name = "eval-" + count + ".dll";

            ++count;
#if STATIC
            throw new NotSupportedException();
#else
            AssemblyDefinitionDynamic assembly;
            AssemblyBuilderAccess     access;

            if (Environment.GetEnvironmentVariable("SAVE") != null)
            {
                access            = AssemblyBuilderAccess.RunAndSave;
                assembly          = new AssemblyDefinitionDynamic(module, current_debug_name, current_debug_name);
                assembly.Importer = importer;
            }
            else
            {
#if NET_4_0
                access = AssemblyBuilderAccess.RunAndCollect;
#else
                access = AssemblyBuilderAccess.Run;
#endif
                assembly = new AssemblyDefinitionDynamic(module, current_debug_name);
            }

            assembly.Create(AppDomain.CurrentDomain, access);

            Method expression_method;
            if (host != null)
            {
                var base_class_imported = importer.ImportType(base_class);
                var baseclass_list      = new List <FullNamedExpression> (1)
                {
                    new TypeExpression(base_class_imported, host.Location)
                };

                host.AddBasesForPart(host, baseclass_list);

                host.CreateType();
                host.DefineType();
                host.Define();

                expression_method = (Method)host.Methods[0];
            }
            else
            {
                expression_method = null;
            }

            module.CreateType();
            module.Define();

            if (Report.Errors != 0)
            {
                if (undo != null)
                {
                    undo.ExecuteUndo();
                }

                return(null);
            }

            if (host != null)
            {
                host.EmitType();
            }

            module.Emit();
            if (Report.Errors != 0)
            {
                if (undo != null)
                {
                    undo.ExecuteUndo();
                }
                return(null);
            }

            module.CloseType();
            if (host != null)
            {
                host.CloseType();
            }

            if (access == AssemblyBuilderAccess.RunAndSave)
            {
                assembly.Save();
            }

            if (host == null)
            {
                return(null);
            }

            //
            // Unlike Mono, .NET requires that the MethodInfo is fetched, it cant
            // work from MethodBuilders.   Retarded, I know.
            //
            var tt = assembly.Builder.GetType(host.TypeBuilder.Name);
            var mi = tt.GetMethod(expression_method.Name);

            if (host.Fields != null)
            {
                //
                // We need to then go from FieldBuilder to FieldInfo
                // or reflection gets confused (it basically gets confused, and variables override each
                // other).
                //
                foreach (Field field in host.Fields)
                {
                    var fi = tt.GetField(field.Name);

                    Tuple <FieldSpec, FieldInfo> old;

                    // If a previous value was set, nullify it, so that we do
                    // not leak memory
                    if (fields.TryGetValue(field.Name, out old))
                    {
                        if (old.Item1.MemberType.IsStruct)
                        {
                            //
                            // TODO: Clear fields for structs
                            //
                        }
                        else
                        {
                            try {
                                old.Item2.SetValue(null, null);
                            } catch {
                            }
                        }
                    }

                    fields[field.Name] = Tuple.Create(field.Spec, fi);
                }
            }

            return((CompiledMethod)System.Delegate.CreateDelegate(typeof(CompiledMethod), mi));
#endif
        }
Exemple #3
0
        public virtual void Emit()
        {
            if (Compiler.Settings.Target == Target.Module)
            {
                module_target_attrs = new AssemblyAttributesPlaceholder(module, name);
                module_target_attrs.CreateType();
                module_target_attrs.DefineType();
                module_target_attrs.Define();
                module.AddCompilerGeneratedClass(module_target_attrs);
            }
            else if (added_modules != null)
            {
                ReadModulesAssemblyAttributes();
            }

            if (Compiler.Settings.GenerateDebugInfo)
            {
                symbol_writer = new MonoSymbolWriter(file_name);

                // Register all source files with symbol writer
                foreach (var source in Compiler.SourceFiles)
                {
                    source.DefineSymbolInfo(symbol_writer);
                }

                // TODO: global variables
                SymbolWriter.symwriter = symbol_writer;
            }

            module.Emit();

            if (module.HasExtensionMethod)
            {
                var pa = module.PredefinedAttributes.Extension;
                if (pa.IsDefined)
                {
                    SetCustomAttribute(pa.Constructor, AttributeEncoder.Empty);
                }
            }

            if (!wrap_non_exception_throws_custom)
            {
                PredefinedAttribute pa = module.PredefinedAttributes.RuntimeCompatibility;
                if (pa.IsDefined && pa.ResolveBuilder())
                {
                    var prop = module.PredefinedMembers.RuntimeCompatibilityWrapNonExceptionThrows.Get();
                    if (prop != null)
                    {
                        AttributeEncoder encoder = new AttributeEncoder();
                        encoder.EncodeNamedPropertyArgument(prop, new BoolLiteral(Compiler.BuiltinTypes, true, Location.Null));
                        SetCustomAttribute(pa.Constructor, encoder.ToArray());
                    }
                }
            }

            if (declarative_security != null)
            {
#if STATIC
                foreach (var entry in declarative_security)
                {
                    Builder.__AddDeclarativeSecurity(entry);
                }
#else
                var args = new PermissionSet[3];
                declarative_security.TryGetValue(SecurityAction.RequestMinimum, out args[0]);
                declarative_security.TryGetValue(SecurityAction.RequestOptional, out args[1]);
                declarative_security.TryGetValue(SecurityAction.RequestRefuse, out args[2]);
                builder_extra.AddPermissionRequests(args);
#endif
            }

            CheckReferencesPublicToken();

            SetEntryPoint();
        }