示例#1
0
        private AssemblyGenAttributes MakeGenAttrs(bool debug)
        {
            // TODO:
            ScriptDomainOptions options = ScriptDomainManager.Options;

            AssemblyGenAttributes result = options.AssemblyGenAttributes;

            result &= ~AssemblyGenAttributes.SaveAndReloadAssemblies;

            if (debug)
            {
                result |= AssemblyGenAttributes.EmitDebugInfo;


#if !SILVERLIGHT
                if (options.DebugCodeGeneration)
#endif
                result |= AssemblyGenAttributes.DisableOptimizations;
            }
            else
            {
                result &= ~AssemblyGenAttributes.EmitDebugInfo;
                result &= ~AssemblyGenAttributes.DisableOptimizations;
                result &= ~AssemblyGenAttributes.GenerateDebugAssemblies;
            }

            return(result);
        }
示例#2
0
        private readonly string _outDir; // null means the current directory

        #endif

        #region Constructors

        public AssemblyGen(string moduleName,
            string outDir,
            string outFile,
            AssemblyGenAttributes generationAttributes)
            : this(moduleName, outDir, outFile, generationAttributes, 
            PortableExecutableKinds.ILOnly, ImageFileMachine.I386)
        {
        }
示例#3
0
        public static AssemblyGen CreateModuleAssembly(string suid)
        {
            AssemblyGenAttributes genAttrs = ScriptDomainManager.Options.AssemblyGenAttributes;

            if (ScriptDomainManager.Options.DebugMode)
            {
                genAttrs |= AssemblyGenAttributes.EmitDebugInfo;
            }

            if (ScriptDomainManager.Options.DebugCodeGeneration)
            {
                genAttrs |= AssemblyGenAttributes.DisableOptimizations;
            }

            string outDir, fileName;

            GetCompiledSourceUnitAssemblyLocation(suid, out outDir, out fileName);


            AssemblyGen ag;
            string      ext = ".dll";

            //if (scriptCode.CodeBlock.Name == "ironscheme.boot.new")
            //{
            //  fileName = "ironscheme.boot.new";
            //  ext = ".dll";
            //}


            // Try to create a file called "filename.<cnt>.exe", ensuring that the filename does not clash with an existing file
            int cnt = 0;

            for (; ;)
            {
                try
                {
                    ag = new AssemblyGen(fileName, outDir, fileName + ext, genAttrs);
                    if (ScriptDomainManager.Options.DebugMode)
                    {
                        ag.CreateSymWriter();
                    }
                    break;
                }
                catch (FileNotFoundException)
                {
                    throw;
                }
                catch (IOException)
                {
                    // If a file already exits with the same name, try the next name in the sequence.
                    ext = "_" + cnt.ToString() + ext;
                }
            }

            //ag.SetSourceUnit(su);

            return(ag);
        }
示例#4
0
 public AssemblyGen(string moduleName,
                    string outDir,
                    string outFile,
                    AssemblyGenAttributes generationAttributes)
     :
     this(moduleName, outDir, outFile, generationAttributes,
          PortableExecutableKinds.ILOnly, ImageFileMachine.I386)
 {
 }
示例#5
0
        public static object CompileBootfile(object libs)
        {
            AssemblyGenAttributes aga = ScriptDomainManager.Options.AssemblyGenAttributes;

            ScriptDomainManager.Options.AssemblyGenAttributes &= ~AssemblyGenAttributes.EmitDebugInfo;
            ScriptDomainManager.Options.AssemblyGenAttributes &= ~AssemblyGenAttributes.GenerateDebugAssemblies;
            ScriptDomainManager.Options.AssemblyGenAttributes &= ~AssemblyGenAttributes.DisableOptimizations;

            if (ScriptDomainManager.Options.DebugMode)
            {
                ScriptDomainManager.Options.AssemblyGenAttributes |= AssemblyGenAttributes.EmitDebugInfo;
                ScriptDomainManager.Options.AssemblyGenAttributes |= AssemblyGenAttributes.GenerateDebugAssemblies;
                //ScriptDomainManager.Options.AssemblyGenAttributes |= AssemblyGenAttributes.DisableOptimizations;

                ScriptDomainManager.Options.DebugCodeGeneration = false;
            }
            else
            {
                ScriptDomainManager.Options.DebugCodeGeneration = false;
            }

            ScriptDomainManager.Options.AssemblyGenAttributes |= AssemblyGenAttributes.SaveAndReloadAssemblies;


            ScriptDomainManager.CurrentManager.Snippets.CurrentAssembly =
                Compiler.Generator.CurrentAssemblyGen = AssemblyGen.CreateModuleAssembly("ironscheme.boot.new.dll");

            //Console.WriteLine(new Cons(libs).PrettyPrint);

            CodeBlock cb = IronSchemeLanguageContext.CompileExpr(libs as Cons);

            cb.ExplicitCodeContextExpression = null;
            cb.Name = "ironscheme.boot.new";

            ScriptCode sc = cc.LanguageContext.CompileSourceCode(cb);

            sc.LibraryGlobals  = Compiler.SimpleGenerator.libraryglobals;
            sc.LibraryGlobalsN = Compiler.SimpleGenerator.libraryglobalsN;
            sc.LibraryGlobalsX = Compiler.SimpleGenerator.libraryglobalsX;

            sc.SourceUnit.IsVisibleToDebugger = true;

            ScriptModule sm = ScriptDomainManager.CurrentManager.CreateModule("ironscheme.boot.new", sc);

            ScriptDomainManager.Options.AssemblyGenAttributes = aga;

            sc.ClearCache();
            Compiler.SimpleGenerator.ClearGlobals();

            return(TRUE);
        }
示例#6
0
        public static object CompileCore(object expr)
        {
            // fast path for really simple stuff
            if (expr is SymbolId)
            {
                CallTarget0 n = delegate
                {
                    return(SymbolValue(expr));
                };
                return(Closure.Create(n));
            }

            AssemblyGenAttributes aga = ScriptDomainManager.Options.AssemblyGenAttributes;

            ScriptDomainManager.Options.AssemblyGenAttributes &= ~AssemblyGenAttributes.EmitDebugInfo;
            ScriptDomainManager.Options.AssemblyGenAttributes &= ~AssemblyGenAttributes.GenerateDebugAssemblies;
            ScriptDomainManager.Options.AssemblyGenAttributes &= ~AssemblyGenAttributes.DisableOptimizations;

            if (ScriptDomainManager.Options.DebugMode)
            {
                ScriptDomainManager.Options.AssemblyGenAttributes |= AssemblyGenAttributes.EmitDebugInfo;
                ScriptDomainManager.Options.AssemblyGenAttributes |= AssemblyGenAttributes.GenerateDebugAssemblies;
                ScriptDomainManager.Options.AssemblyGenAttributes |= AssemblyGenAttributes.DisableOptimizations;
                ScriptDomainManager.Options.DebugCodeGeneration    = true;
            }
            else
            {
                ScriptDomainManager.Options.DebugCodeGeneration = false;
            }

            ScriptDomainManager.Options.AssemblyGenAttributes |= AssemblyGenAttributes.SaveAndReloadAssemblies;
            // if you ever want to inspect the emitted dll's comment the following out (or skip in the debugger), use with care
            ScriptDomainManager.Options.AssemblyGenAttributes &= ~AssemblyGenAttributes.SaveAndReloadAssemblies;

            var prevt  = IronScheme.Compiler.Generator.AllowTransientBinding;
            var prevag = Compiler.Generator.CurrentAssemblyGen;

            if ((ScriptDomainManager.Options.AssemblyGenAttributes & AssemblyGenAttributes.SaveAndReloadAssemblies) != 0)
            {
                IronScheme.Compiler.Generator.AllowTransientBinding = false;

                ScriptDomainManager.CurrentManager.Snippets.CurrentAssembly =
                    Compiler.Generator.CurrentAssemblyGen = AssemblyGen.CreateModuleAssembly(null);
            }
            else
            {
                Compiler.Generator.CurrentAssemblyGen = ScriptDomainManager.Options.DebugMode ?
                                                        ScriptDomainManager.CurrentManager.Snippets.DebugAssembly :
                                                        ScriptDomainManager.CurrentManager.Snippets.Assembly;
            }

            int c = Interlocked.Increment(ref evalcounter);

#if DEBUG
            Stopwatch sw = Stopwatch.StartNew();
#endif
            //Console.WriteLine(new Cons(expr).PrettyPrint);
            try
            {
                CodeBlock cb = IronSchemeLanguageContext.CompileExpr(new Cons(expr));
                cb.ExplicitCodeContextExpression = null;

                ScriptCode sc = Context.LanguageContext.CompileSourceCode(cb); //wrap

#if DEBUG
                sw.Stop();

                Trace.WriteLine(sw.Elapsed.TotalMilliseconds, string.Format("compile - eval-core({0:D3})", c));
                sw = Stopwatch.StartNew();
#endif
                try
                {
                    sc.LibraryGlobals  = Compiler.SimpleGenerator.libraryglobals;
                    sc.LibraryGlobalsN = Compiler.SimpleGenerator.libraryglobalsN;
                    sc.LibraryGlobalsX = Compiler.SimpleGenerator.libraryglobalsX;

                    ScriptModule sm = ScriptDomainManager.CurrentManager.CreateModule(string.Format("eval-core({0:D3})", c), sc);
                    sc = sm.GetScripts()[0];

#if DEBUG
                    sw.Stop();

                    Trace.WriteLine(sw.Elapsed.TotalMilliseconds, string.Format("compile*- eval-core({0:D3})", c));
#endif
                    CallTarget0 compiled = delegate
                    {
#if DEBUG
                        try
                        {
                            sw = Stopwatch.StartNew();
#endif
                        return(sc.Run(Context.ModuleContext.Module));

#if DEBUG
                    }

                    finally
                    {
                        sw.Stop();
                        Trace.WriteLine(sw.Elapsed.TotalMilliseconds, string.Format("run     - eval-core({0:D3})", c));
                    }
#endif
                    };
                    return(Closure.Create(compiled));
                }
                catch (Variable.UnInitializedUsageException ex)
                {
                    CallTarget0 err = delegate
                    {
                        return(AssertionViolation(ex.Variable.Block.Name, ex.Message, UnGenSym(ex.Variable.Name)));
                    };

                    return(Closure.Create(err));
                }
                finally
                {
                    BoundExpression.Fixups.Clear();
                    BoundExpression.FixupTypes.Clear();
                    Compiler.Generator.CurrentAssemblyGen               = prevag;
                    ScriptDomainManager.Options.AssemblyGenAttributes   = aga;
                    IronScheme.Compiler.Generator.AllowTransientBinding = prevt;
                    sc.ClearCache();
                    Compiler.SimpleGenerator.ClearGlobals();
                    Compiler.ClrGenerator.compiletimetypes.Clear();
                }
            }
            catch (Continuation)
            {
                throw;
            }
            catch (Exception ex)
            {
                var who = ex.Data["Who"];
                return(SyntaxError(who ?? FALSE, ex.Message, FALSE, FALSE));
            }
        }
示例#7
0
        public static object CompileLibrary(object filename, object content)
        {
            IronScheme.Compiler.Generator.LocationHint = null;
            AssemblyGenAttributes aga = ScriptDomainManager.Options.AssemblyGenAttributes;

            ScriptDomainManager.Options.AssemblyGenAttributes &= ~AssemblyGenAttributes.EmitDebugInfo;
            ScriptDomainManager.Options.AssemblyGenAttributes &= ~AssemblyGenAttributes.GenerateDebugAssemblies;
            ScriptDomainManager.Options.AssemblyGenAttributes &= ~AssemblyGenAttributes.DisableOptimizations;

            if (ScriptDomainManager.Options.DebugMode)
            {
                ScriptDomainManager.Options.AssemblyGenAttributes |= AssemblyGenAttributes.EmitDebugInfo;
                ScriptDomainManager.Options.AssemblyGenAttributes |= AssemblyGenAttributes.GenerateDebugAssemblies;
                ScriptDomainManager.Options.AssemblyGenAttributes |= AssemblyGenAttributes.DisableOptimizations;

                ScriptDomainManager.Options.DebugCodeGeneration = true;
            }
            else
            {
                ScriptDomainManager.Options.DebugCodeGeneration = false;
            }

            ScriptDomainManager.Options.AssemblyGenAttributes |= AssemblyGenAttributes.SaveAndReloadAssemblies;

            //object[] arr = ListToVector(content as Cons);
            //0: id
            //1: name
            //2: version
            //3: imp*
            //4: vis*
            //5: inv*
            //6: subst
            //7: env
            //8: visit code
            //9: invoke code
            //10: guard code
            //11: guard req?
            //12: visible?


            Compiler.Generator.AllowTransientBinding = false;

            ScriptDomainManager.CurrentManager.Snippets.CurrentAssembly =
                Compiler.Generator.CurrentAssemblyGen = AssemblyGen.CreateModuleAssembly(filename as string);

            try
            {
                CodeBlock cb = IronSchemeLanguageContext.CompileExpr(new Cons(content));
                cb.ExplicitCodeContextExpression = null;
                cb.Name = "";

                ScriptCode sc = cc.LanguageContext.CompileSourceCode(cb, filename as string);

                sc.LibraryGlobals  = Compiler.SimpleGenerator.libraryglobals;
                sc.LibraryGlobalsN = Compiler.SimpleGenerator.libraryglobalsN;
                sc.LibraryGlobalsX = Compiler.SimpleGenerator.libraryglobalsX;

                sc.SourceUnit.IsVisibleToDebugger = true;

                ScriptModule sm = ScriptDomainManager.CurrentManager.CreateModule(Path.GetFileNameWithoutExtension(filename as string), sc);

                // clean up transient non-generative types, could maybe also have used AllowTransientBinding to check?
                IronScheme.Runtime.R6RS.Records.ClearTypesFrom(Compiler.Generator.CurrentAssemblyGen);

                ScriptDomainManager.Options.AssemblyGenAttributes = aga;

                Compiler.SimpleGenerator.ClearGlobals();



                return(TRUE);
            }
            finally
            {
                Compiler.Generator.AllowTransientBinding = true;
            }
        }
示例#8
0
        public AssemblyGen(string moduleName, 
            string outDir, 
            string outFile, 
            AssemblyGenAttributes generationAttributes,
            PortableExecutableKinds peKind, 
            ImageFileMachine machine)
        {
            Contract.Requires(!String.IsNullOrEmpty(moduleName), "moduleName", "Module name cannot be a null reference or an empty string.");
            Contract.Requires(outFile != null || !SaveAndReloadAssemblies, "outFile", "SaveAssemblies mode requires non-null output file name.");

            _genAttrs = generationAttributes;

            AssemblyName asmname = new AssemblyName();

            AppDomain domain = AppDomain.CurrentDomain; //System.Threading.Thread.GetDomain();

            _machine = machine;
            _peKind = peKind;
            _outFileName = outFile;

            #if SILVERLIGHT  // AssemblyBuilderAccess.RunAndSave, Environment.CurrentDirectory
            asmname.Name = moduleName;
            _myAssembly = domain.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run);
            _myModule = _myAssembly.DefineDynamicModule(moduleName, EmitDebugInfo);
            #else
            try {
                outDir = Path.GetFullPath(String.IsNullOrEmpty(outDir) ? Environment.CurrentDirectory : outDir);
            } catch (Exception e) {
                throw new ArgumentException("Invalid output directory", e);
            }

            if (SaveAndReloadAssemblies
            #if PEVERIFY
              || VerifyAssemblies
            #endif
              ) {
                _outDir = outDir;
            }

            if (moduleName == "ironscheme.boot.new")
            {
              _outDir = outDir = Path.Combine(outDir, "build");
              _outFileName = "ironscheme.boot.dll";
            }

              // SymbolWriter fails on Mono for some reason

            if (SaveAndReloadAssemblies) {
                asmname.Name = moduleName == "ironscheme.boot.new" ? "ironscheme.boot" : moduleName;
                if (File.Exists("DEVELOPMENT.snk"))
                {
                  asmname.KeyPair = new StrongNameKeyPair(File.ReadAllBytes("DEVELOPMENT.snk"));
                }
                asmname.Version = new Version("1.0.0.0");
            #pragma warning disable 0618
                _myAssembly = domain.DefineDynamicAssembly(asmname, moduleName == "ironscheme.boot.new" ? AssemblyBuilderAccess.Save : AssemblyBuilderAccess.Save, outDir, null);
            #pragma warning restore 0618

                _myModule = _myAssembly.DefineDynamicModule( moduleName == "ironscheme.boot.new" ? "ironscheme.boot.dll" : _outFileName,
                                                           _outFileName, EmitDebugInfo);
            } else {
                asmname.Name = moduleName;
                _myAssembly = domain.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run);
                _myModule = _myAssembly.DefineDynamicModule(moduleName, EmitDebugInfo);
            }
            _myAssembly.DefineVersionInfoResource();

            #endif
            if (EmitDebugInfo) SetDebuggableAttributes();
        }
示例#9
0
        public AssemblyGen(string moduleName,
                           string outDir,
                           string outFile,
                           AssemblyGenAttributes generationAttributes,
                           PortableExecutableKinds peKind,
                           ImageFileMachine machine)
        {
            Contract.Requires(!String.IsNullOrEmpty(moduleName), "moduleName", "Module name cannot be a null reference or an empty string.");
            Contract.Requires(outFile != null || !SaveAndReloadAssemblies, "outFile", "SaveAssemblies mode requires non-null output file name.");

            _genAttrs = generationAttributes;

            AssemblyName asmname = new AssemblyName();

            AppDomain domain = AppDomain.CurrentDomain; //System.Threading.Thread.GetDomain();

            _machine     = machine;
            _peKind      = peKind;
            _outFileName = outFile;

#if SILVERLIGHT  // AssemblyBuilderAccess.RunAndSave, Environment.CurrentDirectory
            asmname.Name = moduleName;
            _myAssembly  = domain.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run);
            _myModule    = _myAssembly.DefineDynamicModule(moduleName, EmitDebugInfo);
#else
            try {
                outDir = Path.GetFullPath(String.IsNullOrEmpty(outDir) ? Environment.CurrentDirectory : outDir);
            } catch (Exception e) {
                throw new ArgumentException("Invalid output directory", e);
            }

            if (SaveAndReloadAssemblies
#if PEVERIFY
                || VerifyAssemblies
#endif
                )
            {
                _outDir = outDir;
            }

            if (moduleName == "ironscheme.boot.new")
            {
                _outDir      = outDir = Path.Combine(outDir, "build");
                _outFileName = "ironscheme.boot.dll";
            }

            // SymbolWriter fails on Mono for some reason

            if (SaveAndReloadAssemblies)
            {
                asmname.Name = moduleName == "ironscheme.boot.new" ? "ironscheme.boot" : moduleName;
                if (File.Exists("DEVELOPMENT.snk"))
                {
                    asmname.KeyPair = new StrongNameKeyPair(File.ReadAllBytes("DEVELOPMENT.snk"));
                }
                asmname.Version = new Version("1.0.0.0");
#pragma warning disable 0618
                _myAssembly = domain.DefineDynamicAssembly(asmname, moduleName == "ironscheme.boot.new" ? AssemblyBuilderAccess.Save : AssemblyBuilderAccess.RunAndSave, outDir, null);
#pragma warning restore 0618

                _myModule = _myAssembly.DefineDynamicModule(moduleName == "ironscheme.boot.new" ? "ironscheme.boot.dll" : _outFileName,
                                                            _outFileName, EmitDebugInfo);
            }
            else
            {
                asmname.Name = moduleName;
                _myAssembly  = domain.DefineDynamicAssembly(asmname, AssemblyBuilderAccess.Run);
                _myModule    = _myAssembly.DefineDynamicModule(moduleName, EmitDebugInfo);
            }
            _myAssembly.DefineVersionInfoResource();
#endif
            if (EmitDebugInfo)
            {
                SetDebuggableAttributes();
            }
        }
示例#10
0
 private AssemblyGen CreateNewAssembly(string name, AssemblyGenAttributes attrs)
 {
     Debug.Assert(!String.IsNullOrEmpty(name));
     name = GenerateAssemblyName(name);
     return new AssemblyGen(name, null, name + ".dll", attrs);
 }
示例#11
0
 private AssemblyGen CreateNewAssembly(string name, AssemblyGenAttributes attrs)
 {
     Debug.Assert(!String.IsNullOrEmpty(name));
     name = GenerateAssemblyName(name);
     return(new AssemblyGen(name, null, name + ".dll", attrs));
 }