From 640f241c516fef0ad547aa420030294a0613310f Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Wed, 20 Nov 2024 01:09:09 +0300 Subject: [PATCH] Further unifying module's loading and setup routines related to global vars --- src/vm/module.cs | 13 +++---------- src/vm/util/stack.cs | 2 ++ src/vm/vm.exec.cs | 2 +- src/vm/vm.module.cs | 31 +++++++++++++++++-------------- 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/vm/module.cs b/src/vm/module.cs index 86cbb624..f661e0fa 100644 --- a/src/vm/module.cs +++ b/src/vm/module.cs @@ -117,19 +117,12 @@ public void InitWithCompiled(CompiledModule compiled) gvar_vals.Resize(compiled.total_gvars_num); } - public void InitGlobalVars(VM vm) - { - //let's init all our own global variables - for(int g=0;g import2module, SetupFlags flags = SetupFl ); } - public void InitRuntimeGlobalVars() + public void ImportGlobalVars() { int gvars_offset = local_gvars_num; diff --git a/src/vm/util/stack.cs b/src/vm/util/stack.cs index 7c815e35..8a9e030e 100644 --- a/src/vm/util/stack.cs +++ b/src/vm/util/stack.cs @@ -11,6 +11,7 @@ public class FixedStack public int Count { + [MethodImpl(MethodImplOptions.AggressiveInlining)] get { return head; } } @@ -21,6 +22,7 @@ public FixedStack(int max_capacity) public T this[int index] { + [MethodImpl(MethodImplOptions.AggressiveInlining)] get { ValidateIndex(index); return storage[index]; diff --git a/src/vm/vm.exec.cs b/src/vm/vm.exec.cs index 555c5e47..08964c0c 100644 --- a/src/vm/vm.exec.cs +++ b/src/vm/vm.exec.cs @@ -757,7 +757,7 @@ BHS ExecuteOnce(ExecState exec) return BHS.SUCCESS; } - void ExecuteInitCode(Module module) + void ExecInitCode(Module module) { var bytecode = module.compiled.initcode; if(bytecode == null || bytecode.Length == 0) diff --git a/src/vm/vm.module.cs b/src/vm/vm.module.cs index d91f2ed3..f8fe9f80 100644 --- a/src/vm/vm.module.cs +++ b/src/vm/vm.module.cs @@ -35,7 +35,6 @@ public enum LoadModuleSymbolError public bool LoadModule(string module_name) { - //Console.WriteLine("==START LOAD " + module_name); if(loading_modules.Count > 0) throw new Exception("Already loading modules"); @@ -46,9 +45,12 @@ public bool LoadModule(string module_name) if(loading_modules.Count == 0) return false; - //NOTE: registering modules in reverse order + //NOTE: initing modules in reverse order for(int i=loading_modules.Count;i-- > 0;) - FinishRegistration(loading_modules[i].loaded); + Init_Phase2(loading_modules[i].loaded); + for(int i=loading_modules.Count;i-- > 0;) + Init_Phase3(loading_modules[i].loaded); + loading_modules.Clear(); return true; @@ -57,8 +59,9 @@ public bool LoadModule(string module_name) //NOTE: this method is public only for testing convenience public void LoadModule(Module module) { - BeginRegistration(module); - FinishRegistration(module); + Init_Phase1(module); + Init_Phase2(module); + Init_Phase3(module); } public Module FindModule(string module_name) @@ -89,7 +92,6 @@ bool TryAddToLoadingList(string module_name) lm.name = module_name; loading_modules.Add(lm); - //NOTE: passing self as a type proxies 'resolver' var loaded = loader.Load(module_name, this); //if no such a module let's remove it from the loading list @@ -99,33 +101,34 @@ bool TryAddToLoadingList(string module_name) } else { + //let's add all imported modules as well foreach(var imported in loaded.compiled.imports) TryAddToLoadingList(imported); lm.loaded = loaded; - BeginRegistration(loaded); + Init_Phase1(loaded); } return true; } - void BeginRegistration(Module module) + void Init_Phase1(Module module) { //NOTE: for simplicity we add it to the modules at once, // this is probably a bit 'smelly' but makes further // symbols setup logic easier registered_modules[module.name] = module; - - module.InitGlobalVars(this); } - void FinishRegistration(Module module) + void Init_Phase2(Module module) { module.Setup(name => FindModule(name)); + ExecInitCode(module); + } - module.InitRuntimeGlobalVars(); - - ExecuteInitCode(module); + void Init_Phase3(Module module) + { + module.ImportGlobalVars(); ExecModuleInitFunc(module); }