Exemple #1
0
        private List <Scope> GenerateScriptScopes()
        {
            List <Scope> scopes = new List <Scope>(_scriptCodes.Length);
            ScriptModule sm     = ScriptDomainManager.CurrentManager.Host.DefaultModule as ScriptModule;

            for (int i = 0; i < _scriptCodes.Length; i++)
            {
                ScriptCode scriptCode = _scriptCodes[i];

                // Force creation of names used in other script codes into all optimized dictionaries
                ScopeAllocator        allocator = _allocators[scriptCode.LanguageContext];
                IAttributesCollection iac       = CreateLanguageDictionary(scriptCode.LanguageContext, allocator);
                Scope scope = new Scope(sm.Scope, iac);

                // module context is filled later:
                CodeContext codeContext = new CodeContext(scope, scriptCode.LanguageContext);

                IModuleDictionaryInitialization ici = iac as IModuleDictionaryInitialization;
                if (ici != null)
                {
                    ici.InitializeModuleDictionary(codeContext);
                }

                scopes.Add(scope);
                _codeContexts.Add(codeContext);
            }
            return(scopes);
        }
        /// <summary>
        /// allocates slots out of a FunctionEnvironment.
        /// </summary>
        internal static ScopeAllocator CreateFrameAllocator()
        {
            // Globals
            ScopeAllocator global = new ScopeAllocator(
                null,
                new GlobalNamedAllocator()
                );

            // Locals
            ScopeAllocator ns = new ScopeAllocator(
                global,
                new FrameStorageAllocator()
                );

            return(ns);
        }
Exemple #3
0
        private List <CodeGen> GenerateScriptMethods()
        {
            List <CodeGen> cgs = new List <CodeGen>(_scriptCodes.Length);

            foreach (ScriptCode sc in _scriptCodes)
            {
                ScopeAllocator sa = CreateStorageAllocator(sc);
                CodeGen        cg = CreateCodeGen(sc);
                cg.Allocator = sa;

                // every module can hand it's environment to anything embedded in it.
                cg.EnvironmentSlot = new EnvironmentSlot(new PropertySlot(
                                                             new PropertySlot(cg.ContextSlot,
                                                                              typeof(CodeContext).GetProperty("Scope")),
                                                             typeof(Scope).GetProperty("Dict"))
                                                         );

                cg.Context = sc.CompilerContext;

                GlobalFieldAllocator gfa = sa.LocalAllocator as GlobalFieldAllocator;
                if (gfa != null)
                {
                    Dictionary <SymbolId, Slot> fields = gfa.SlotFactory.Fields;

                    Label ok = cg.DefineLabel();
                    cg.ContextSlot.EmitGet(cg);
                    //cg.EmitNull();
                    //cg.Emit(OpCodes.Ceq);
                    cg.Emit(OpCodes.Brtrue_S, ok);

                    // MyModuleDictType.ContextSlot = arg0

                    cg.EmitNew(cg.TypeGen.DefaultConstructor);
                    cg.EmitArgGet(0);
                    cg.EmitCall(typeof(IModuleDictionaryInitialization), "InitializeModuleDictionary");

                    cg.MarkLabel(ok);
                }

                sc.CodeBlock.EmitFunctionImplementation(cg);

                cg.Finish();

                cgs.Add(cg);
            }
            return(cgs);
        }
Exemple #4
0
        private ScopeAllocator CreateStorageAllocator(ScriptCode scriptCode)
        {
            ScopeAllocator allocator;

            if (!_allocators.TryGetValue(scriptCode.LanguageContext, out allocator))
            {
                var sf  = CreateSlotFactory(scriptCode) as StaticFieldSlotFactory;
                var mgf = new ModuleGlobalFactory(sf);
                var sf2 = new StaticFieldSlotFactory(sf.TypeGen);
                GlobalFieldAllocator gfa = new GlobalFieldAllocator(mgf);
                var gfa2 = new GlobalFieldAllocator(sf2);

                // Locals and globals are allocated from the same namespace for optimized modules
                ScopeAllocator global = new ScopeAllocator(null, gfa);
                allocator = new ScopeAllocator(global, gfa2);

                _allocators[scriptCode.LanguageContext] = allocator;
            }

            return(allocator);
        }
Exemple #5
0
 public ScopeAllocator(ScopeAllocator parent, StorageAllocator allocator)
 {
     _parent    = parent;
     _allocator = allocator;
 }
Exemple #6
0
        protected override IAttributesCollection CreateLanguageDictionary(LanguageContext context, ScopeAllocator allocator)
        {
            LanguageInfo li = _languages[context];

            // TODO: Force all dictionaries to share same object data (for multi-module)
            GlobalFieldAllocator gfa = allocator.GlobalAllocator as GlobalFieldAllocator;

            if (gfa != null)
            {
                Dictionary <SymbolId, Slot> fields = gfa.SlotFactory.Fields;

                BuildDictionary(li, fields);

                Type t   = li.TypeGen.FinishType();
                var  ass = li.TypeGen.AssemblyGen.DumpAndLoad();

                if (ModuleName == "ironscheme.boot.new")
                {
                    return(null);
                }

                try
                {
                    return((IAttributesCollection)Activator.CreateInstance(ass.GetType(t.FullName)));
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Failed to create language dictionary: {0}", ex.Message);
                    return(null);
                }
            }
            else
            {
                throw new InvalidOperationException("invalid allocator");
            }
        }
Exemple #7
0
        protected override IAttributesCollection CreateLanguageDictionary(LanguageContext context, ScopeAllocator allocator)
        {
            TupleSlotFactory tsf   = _languages[context];
            object           tuple = tsf.CreateTupleInstance();

            // TODO: Force all dictionaries to share same object data (for multi-module)

            IAttributesCollection res = (IAttributesCollection)Activator.CreateInstance(
                tsf.DictionaryType.MakeGenericType(tsf.TupleType),
                tuple,
                tsf.Names);

            return(res);
        }
Exemple #8
0
 protected abstract IAttributesCollection CreateLanguageDictionary(LanguageContext context, ScopeAllocator allocator);
        protected override IAttributesCollection CreateLanguageDictionary(LanguageContext context, ScopeAllocator allocator)
        {
            TupleSlotFactory tsf = _languages[context];
            object tuple = tsf.CreateTupleInstance();

            // TODO: Force all dictionaries to share same object data (for multi-module)

            IAttributesCollection res = (IAttributesCollection)Activator.CreateInstance(
                tsf.DictionaryType.MakeGenericType(tsf.TupleType),
                tuple,
                tsf.Names);

            return res;
        }
        protected override IAttributesCollection CreateLanguageDictionary(LanguageContext context, ScopeAllocator allocator)
        {
            LanguageInfo li = _languages[context];

              // TODO: Force all dictionaries to share same object data (for multi-module)
              GlobalFieldAllocator gfa = allocator.GlobalAllocator as GlobalFieldAllocator;
              if (gfa != null)
              {
            Dictionary<SymbolId, Slot> fields = gfa.SlotFactory.Fields;

            BuildDictionary(li, fields);

            Type t = li.TypeGen.FinishType();
            var ass = li.TypeGen.AssemblyGen.DumpAndLoad();

            if (ModuleName == "ironscheme.boot.new")
            {
              return null;
            }

            try
            {
              var rt = ass.GetType(t.FullName);
              if (rt == null)
              {
                Console.WriteLine("Type: '{0}' not found in '{1}' (mono bug)", t.FullName, ass.FullName);
                return null;
              }
              return (IAttributesCollection)Activator.CreateInstance(rt);
            }
            catch (Exception ex)
            {
              Console.WriteLine("Failed to create language dictionary: {0}", ex.Message);
              return null;
            }
              }
              else
              {
            throw new InvalidOperationException("invalid allocator");
              }
        }
        private ScopeAllocator CreateStorageAllocator(ScriptCode scriptCode)
        {
            ScopeAllocator allocator;
            if (!_allocators.TryGetValue(scriptCode.LanguageContext, out allocator)) {
                var sf = CreateSlotFactory(scriptCode) as StaticFieldSlotFactory;
                var mgf = new ModuleGlobalFactory(sf);
                var sf2 = new StaticFieldSlotFactory(sf.TypeGen);
                GlobalFieldAllocator gfa = new GlobalFieldAllocator(mgf);
                var gfa2 = new GlobalFieldAllocator(sf2);

                // Locals and globals are allocated from the same namespace for optimized modules
                ScopeAllocator global = new ScopeAllocator(null, gfa);
                allocator = new ScopeAllocator(global, gfa2);

                _allocators[scriptCode.LanguageContext] = allocator;
            }

            return allocator;
        }
 protected abstract IAttributesCollection CreateLanguageDictionary(LanguageContext context, ScopeAllocator allocator);
Exemple #13
0
 public ScopeAllocator(ScopeAllocator parent, StorageAllocator allocator)
 {
     _parent = parent;
     _allocator = allocator;
 }