pooled M3BranchPatch; cleanup

extensions
Steven Massey 5 years ago
parent e7c04280b8
commit 1983a2cf9e

@ -273,6 +273,7 @@ _onfatal:
fprintf (stderr, "\n"); fprintf (stderr, "\n");
} }
m3_FreeRuntime (runtime);
m3_FreeEnvironment (env); m3_FreeEnvironment (env);
return 0; return 0;

@ -175,13 +175,6 @@ typedef int64_t (* M3Callback) (IM3Function i_currentFunction, void * i_ref);
//------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------------------------------------------
// configuration (found in m3_core.h) // 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.
*/
//-------------------------------------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------------------------------

@ -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 /* unused: */ signature, const void * i_function)
M3Result LinkRawFunction (IM3Module io_module, IM3Function io_function, ccstr_t i_signature, const void * i_function)
{ {
M3Result result = c_m3Err_none; d_m3Assert (io_module->runtime); M3Result result = c_m3Err_none; d_m3Assert (io_module->runtime);

@ -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 PatchBranches (IM3Compilation o)
{ {
bool patched = false; bool didPatch = false;
M3CompilationScope * block = & o->block; M3CompilationScope * block = & o->block;
pc_t pc = GetPC (o); pc_t pc = GetPC (o);
while (block->patches) IM3BranchPatch patches = block->patches;
{ m3log (compile, "patching location: %p to pc: %p", block->patches->location, pc); IM3BranchPatch endPatch = patches;
IM3BranchPatch next = block->patches->next;
while (patches)
pc_t * location = block->patches->location; { m3log (compile, "patching location: %p to pc: %p", patches->location, pc);
* location = pc; * (patches->location) = pc;
m3Free (block->patches); endPatch = patches;
block->patches = next; patches = patches->next;
patched = true;
} }
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)) if (IsValidSlot (valueSlot))
EmitConstant (o, valueSlot); EmitConstant (o, valueSlot);
IM3BranchPatch patch = scope->patches; IM3BranchPatch patch;
_ (AcquirePatch (o, & patch));
_ (m3Alloc (& scope->patches, M3BranchPatch, 1));
patch->location = (pc_t *) ReservePointer (o);
scope->patches->location = (pc_t *) ReservePointer (o); patch->next = scope->patches;
scope->patches->next = patch; scope->patches = patch;
} }
_catch: return result; _catch: return result;
} }
M3Result Compile_BranchTable (IM3Compilation o, u8 i_opcode) M3Result Compile_BranchTable (IM3Compilation o, u8 i_opcode)
{ {
M3Result result; M3Result result;
@ -1058,11 +1088,12 @@ _ (EmitOp (o, op_ContinueLoop));
} }
else else
{ {
IM3BranchPatch prev_patch = scope->patches; IM3BranchPatch patch;
_ (m3Alloc (& scope->patches, M3BranchPatch, 1)); _ (AcquirePatch (o, & patch));
scope->patches->location = (pc_t *) ReservePointer (o); patch->location = (pc_t *) ReservePointer (o);
scope->patches->next = prev_patch; 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 Compile_Function (IM3Function io_function)
{ {
M3Result result = c_m3Err_none; m3log (compile, "compiling: '%s'; wasm-size: %d; numArgs: %d; return: %s", 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; IM3Runtime runtime = io_function->module->runtime;
IM3Compilation o = & runtime->compilation; IM3Compilation o = & runtime->compilation;
memset (o, 0x0, sizeof (M3Compilation)); SetupCompilation (o);
o->runtime = runtime; o->runtime = runtime;
o->module = io_function->module; o->module = io_function->module;
@ -2062,7 +2101,7 @@ _ (Compile_ReserveConstants (o));
pc_t pc2 = GetPagePC (o->page); pc_t pc2 = GetPagePC (o->page);
d_m3AssertFatal (pc2 == pc); d_m3AssertFatal (pc2 == pc);
_ (EmitOp (o, op_Entry));//, comp.stackIndex); _ (EmitOp (o, op_Entry));
EmitPointer (o, io_function); EmitPointer (o, io_function);
_ (Compile_BlockStatements (o)); _ (Compile_BlockStatements (o));

@ -77,6 +77,8 @@ typedef struct
IM3Function function; IM3Function function;
IM3CodePage page; IM3CodePage page;
IM3BranchPatch releasedPatches;
u32 numEmits; u32 numEmits;
u32 numOpcodes; u32 numOpcodes;
@ -100,8 +102,6 @@ typedef struct
u16 regStackIndexPlusOne [2]; u16 regStackIndexPlusOne [2];
//bool enableOptimizations; // no longer used. currently implementation is highly pre-optimized.
u8 previousOpcode; u8 previousOpcode;
} }
M3Compilation; M3Compilation;

@ -22,13 +22,6 @@
# define d_m3MaxFunctionStackHeight 2000 # define d_m3MaxFunctionStackHeight 2000
# endif # endif
//# ifndef d_m3EnableFp32Maths
//# define d_m3EnableFp32Maths false
//# endif
//# ifndef d_m3EnableFp64Maths
//# define d_m3EnableFp64Maths true
//# endif
# ifndef d_m3LogOutput # ifndef d_m3LogOutput
# define d_m3LogOutput 1 # define d_m3LogOutput 1
# endif # endif

@ -151,8 +151,6 @@ M3CodePageHeader;
#define c_m3MaxNumFunctionConstants 60 #define c_m3MaxNumFunctionConstants 60
#define c_m3AlignWasmMemoryToPages d_m3AlignWasmMemoryToPages
#define c_m3MaxSaneUtf8Length 2000 #define c_m3MaxSaneUtf8Length 2000
#define c_m3MaxNumFunctionArgs d_m3MaxNumFunctionArgs #define c_m3MaxNumFunctionArgs d_m3MaxNumFunctionArgs

@ -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) void ReleaseRuntime (IM3Runtime i_runtime)
{ {
ForEachModule (i_runtime, _FreeModule, NULL); ForEachModule (i_runtime, _FreeModule, NULL);
FreeCodePages (i_runtime->pagesOpen); FreeCodePages (i_runtime->pagesOpen);
FreeCodePages (i_runtime->pagesFull); FreeCodePages (i_runtime->pagesFull);
FreeCompilationPatches (& i_runtime->compilation);
m3Free (i_runtime->stack); m3Free (i_runtime->stack);
} }

@ -421,8 +421,8 @@ void ProfileHit (cstr_t i_operationName)
{ {
if (slot->opName != i_operationName) if (slot->opName != i_operationName)
{ {
printf ("**** profiler slot collision; increase mask width\n"); printf ("**** profiler slot collision; increase mask width: c_m3ProfilerSlotMask\n");
m3NotImplemented (); abort ();
} }
} }

@ -35,14 +35,7 @@
# endif # 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_m3OpSig pc_t _pc, u64 * _sp, u8 * _mem, m3reg_t _r0, f64 _fp0
# define d_m3OpArgs _sp, _mem, _r0, _fp0 # define d_m3OpArgs _sp, _mem, _r0, _fp0

@ -191,9 +191,8 @@ _ (Module_AddGlobal (io_module, & global, type, isMutable, true /*
} }
_catch: _catch:
{
FreeImportInfo (& import); FreeImportInfo (& import);
}
return result; return result;
} }

Loading…
Cancel
Save