From 1983a2cf9e4e3c1dd9b571dd00cb64984eb1fcbf Mon Sep 17 00:00:00 2001 From: Steven Massey Date: Sat, 28 Dec 2019 16:35:32 -0800 Subject: [PATCH] pooled M3BranchPatch; cleanup --- platforms/app/main.c | 1 + source/m3.h | 7 ---- source/m3_bind.c | 3 +- source/m3_compile.c | 93 ++++++++++++++++++++++++++++++------------- source/m3_compile.h | 4 +- source/m3_config.h | 7 ---- source/m3_core.h | 2 - source/m3_env.c | 16 ++++++++ source/m3_exec.c | 4 +- source/m3_exec_defs.h | 7 ---- source/m3_parse.c | 5 +-- 11 files changed, 90 insertions(+), 59 deletions(-) diff --git a/platforms/app/main.c b/platforms/app/main.c index 9ddde2f..671799b 100644 --- a/platforms/app/main.c +++ b/platforms/app/main.c @@ -273,6 +273,7 @@ _onfatal: fprintf (stderr, "\n"); } + m3_FreeRuntime (runtime); m3_FreeEnvironment (env); return 0; diff --git a/source/m3.h b/source/m3.h index 42b6d76..83d4ba0 100644 --- a/source/m3.h +++ b/source/m3.h @@ -175,13 +175,6 @@ typedef int64_t (* M3Callback) (IM3Function i_currentFunction, void * i_ref); //------------------------------------------------------------------------------------------------------------------------------- // configuration (found in m3_core.h) //------------------------------------------------------------------------------------------------------------------------------- -/* - define default - ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - d_m3AlignWasmMemoryToPages false The WebAssembly spec defines a 64kB page size and memory size is always - quantized to pages. In a non-Javascript centric WA host this seems - unnecessary. - */ //-------------------------------------------------------------------------------------------------------------------------------------------- diff --git a/source/m3_bind.c b/source/m3_bind.c index 970d058..550ac8b 100644 --- a/source/m3_bind.c +++ b/source/m3_bind.c @@ -400,8 +400,7 @@ M3Result m3_LinkCFunction (IM3Module io_module, // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -// TODO: validate signature as well? -M3Result LinkRawFunction (IM3Module io_module, IM3Function io_function, ccstr_t i_signature, const void * i_function) +M3Result LinkRawFunction (IM3Module io_module, IM3Function io_function, ccstr_t /* unused: */ signature, const void * i_function) { M3Result result = c_m3Err_none; d_m3Assert (io_module->runtime); diff --git a/source/m3_compile.c b/source/m3_compile.c index 92b3f44..70a7654 100644 --- a/source/m3_compile.c +++ b/source/m3_compile.c @@ -493,27 +493,55 @@ M3Result AddTrapRecord (IM3Compilation o) } +M3Result AcquirePatch (IM3Compilation o, IM3BranchPatch * o_patch) +{ + M3Result result = c_m3Err_none; + + IM3BranchPatch patch = o->releasedPatches; + + if (patch) + { + o->releasedPatches = patch->next; + patch->next = NULL; + } + else +_ (m3Alloc (& patch, M3BranchPatch, 1)); + + * o_patch = patch; + + _catch: return result; +} + + bool PatchBranches (IM3Compilation o) { - bool patched = false; + bool didPatch = false; M3CompilationScope * block = & o->block; pc_t pc = GetPC (o); - while (block->patches) - { m3log (compile, "patching location: %p to pc: %p", block->patches->location, pc); - IM3BranchPatch next = block->patches->next; - - pc_t * location = block->patches->location; - * location = pc; - - m3Free (block->patches); - block->patches = next; - - patched = true; + IM3BranchPatch patches = block->patches; + IM3BranchPatch endPatch = patches; + + while (patches) + { m3log (compile, "patching location: %p to pc: %p", patches->location, pc); + * (patches->location) = pc; + + endPatch = patches; + patches = patches->next; } - - return patched; + + if (block->patches) + { d_m3Assert (endPatch->next == NULL); + // return patches to pool + endPatch->next = o->releasedPatches; + o->releasedPatches = block->patches; + block->patches = NULL; + + didPatch = true; + } + + return didPatch; } //------------------------------------------------------------------------------------------------------------------------- @@ -990,18 +1018,20 @@ _ (EmitOp (o, op)); if (IsValidSlot (valueSlot)) EmitConstant (o, valueSlot); - IM3BranchPatch patch = scope->patches; - -_ (m3Alloc (& scope->patches, M3BranchPatch, 1)); - - scope->patches->location = (pc_t *) ReservePointer (o); - scope->patches->next = patch; + IM3BranchPatch patch; +_ (AcquirePatch (o, & patch)); + + patch->location = (pc_t *) ReservePointer (o); + patch->next = scope->patches; + scope->patches = patch; } _catch: return result; } + + M3Result Compile_BranchTable (IM3Compilation o, u8 i_opcode) { M3Result result; @@ -1058,11 +1088,12 @@ _ (EmitOp (o, op_ContinueLoop)); } else { - IM3BranchPatch prev_patch = scope->patches; -_ (m3Alloc (& scope->patches, M3BranchPatch, 1)); - - scope->patches->location = (pc_t *) ReservePointer (o); - scope->patches->next = prev_patch; + IM3BranchPatch patch; +_ (AcquirePatch (o, & patch)); + + patch->location = (pc_t *) ReservePointer (o); + patch->next = scope->patches; + scope->patches = patch; } } @@ -2017,6 +2048,14 @@ M3Result Compile_ReserveConstants (IM3Compilation o) } +void SetupCompilation (IM3Compilation o) +{ + IM3BranchPatch patches = o->releasedPatches; + memset (o, 0x0, sizeof (M3Compilation)); + o->releasedPatches = patches; +} + + M3Result Compile_Function (IM3Function io_function) { M3Result result = c_m3Err_none; m3log (compile, "compiling: '%s'; wasm-size: %d; numArgs: %d; return: %s", @@ -2024,7 +2063,7 @@ M3Result Compile_Function (IM3Function io_function) IM3Runtime runtime = io_function->module->runtime; IM3Compilation o = & runtime->compilation; - memset (o, 0x0, sizeof (M3Compilation)); + SetupCompilation (o); o->runtime = runtime; o->module = io_function->module; @@ -2062,7 +2101,7 @@ _ (Compile_ReserveConstants (o)); pc_t pc2 = GetPagePC (o->page); d_m3AssertFatal (pc2 == pc); -_ (EmitOp (o, op_Entry));//, comp.stackIndex); +_ (EmitOp (o, op_Entry)); EmitPointer (o, io_function); _ (Compile_BlockStatements (o)); diff --git a/source/m3_compile.h b/source/m3_compile.h index 3fadfdc..3e66b30 100644 --- a/source/m3_compile.h +++ b/source/m3_compile.h @@ -77,6 +77,8 @@ typedef struct IM3Function function; IM3CodePage page; + + IM3BranchPatch releasedPatches; u32 numEmits; u32 numOpcodes; @@ -100,8 +102,6 @@ typedef struct u16 regStackIndexPlusOne [2]; - //bool enableOptimizations; // no longer used. currently implementation is highly pre-optimized. - u8 previousOpcode; } M3Compilation; diff --git a/source/m3_config.h b/source/m3_config.h index 9c39477..cf4407b 100644 --- a/source/m3_config.h +++ b/source/m3_config.h @@ -22,13 +22,6 @@ # define d_m3MaxFunctionStackHeight 2000 # endif -//# ifndef d_m3EnableFp32Maths -//# define d_m3EnableFp32Maths false -//# endif -//# ifndef d_m3EnableFp64Maths -//# define d_m3EnableFp64Maths true -//# endif - # ifndef d_m3LogOutput # define d_m3LogOutput 1 # endif diff --git a/source/m3_core.h b/source/m3_core.h index 0b97712..7347ad0 100644 --- a/source/m3_core.h +++ b/source/m3_core.h @@ -151,8 +151,6 @@ M3CodePageHeader; #define c_m3MaxNumFunctionConstants 60 -#define c_m3AlignWasmMemoryToPages d_m3AlignWasmMemoryToPages - #define c_m3MaxSaneUtf8Length 2000 #define c_m3MaxNumFunctionArgs d_m3MaxNumFunctionArgs diff --git a/source/m3_env.c b/source/m3_env.c index 988c990..d2828c8 100644 --- a/source/m3_env.c +++ b/source/m3_env.c @@ -151,12 +151,28 @@ void * _FreeModule (IM3Module i_module, void * i_info) } + +void FreeCompilationPatches (IM3Compilation o) +{ + IM3BranchPatch patches = o->releasedPatches; + + while (patches) + { + IM3BranchPatch next = patches->next; + m3Free (patches); + patches = next; + } +} + + void ReleaseRuntime (IM3Runtime i_runtime) { ForEachModule (i_runtime, _FreeModule, NULL); FreeCodePages (i_runtime->pagesOpen); FreeCodePages (i_runtime->pagesFull); + + FreeCompilationPatches (& i_runtime->compilation); m3Free (i_runtime->stack); } diff --git a/source/m3_exec.c b/source/m3_exec.c index e068b14..7fb3bf8 100644 --- a/source/m3_exec.c +++ b/source/m3_exec.c @@ -421,8 +421,8 @@ void ProfileHit (cstr_t i_operationName) { if (slot->opName != i_operationName) { - printf ("**** profiler slot collision; increase mask width\n"); - m3NotImplemented (); + printf ("**** profiler slot collision; increase mask width: c_m3ProfilerSlotMask\n"); + abort (); } } diff --git a/source/m3_exec_defs.h b/source/m3_exec_defs.h index c1fb70b..4b5a7a7 100644 --- a/source/m3_exec_defs.h +++ b/source/m3_exec_defs.h @@ -35,14 +35,7 @@ # endif -typedef f64 arch_f; -typedef i64 arch_i; - - //--------------------------------------------------------------------------------------------------------------- -#define c_m3NumIntRegisters 1 -#define c_m3NumFpRegisters 1 -#define c_m3NumRegisters (c_m3NumIntRegisters + c_m3NumFpRegisters) # define d_m3OpSig pc_t _pc, u64 * _sp, u8 * _mem, m3reg_t _r0, f64 _fp0 # define d_m3OpArgs _sp, _mem, _r0, _fp0 diff --git a/source/m3_parse.c b/source/m3_parse.c index c9ae8ef..16b08ac 100644 --- a/source/m3_parse.c +++ b/source/m3_parse.c @@ -191,9 +191,8 @@ _ (Module_AddGlobal (io_module, & global, type, isMutable, true /* } _catch: - { - FreeImportInfo (& import); - } + + FreeImportInfo (& import); return result; }