diff --git a/platforms/app/main.c b/platforms/app/main.c index 172ede8..2ee2eec 100644 --- a/platforms/app/main.c +++ b/platforms/app/main.c @@ -138,7 +138,7 @@ M3Result repl_load (const char* fn) fclose (f); f = NULL; - result = m3_ParseModule (env, &module, wasm, fsize); + result = m3_ParseModule (env, &module, wasm, fsize, false); if (result) goto on_error; result = m3_LoadModule (runtime, module); @@ -197,7 +197,7 @@ M3Result repl_load_hex (u32 fsize) } IM3Module module; - result = m3_ParseModule (env, &module, wasm, fsize); + result = m3_ParseModule (env, &module, wasm, fsize, false); if (result) return result; result = m3_LoadModule (runtime, module); diff --git a/source/extensions/m3_extensions.c b/source/extensions/m3_extensions.c index 7d8bf9d..19ada7f 100644 --- a/source/extensions/m3_extensions.c +++ b/source/extensions/m3_extensions.c @@ -92,8 +92,8 @@ _ (Module_AddFunction (i_module, funcTypeIndex, NULL)); function->wasmEnd = function->wasm + numBytes; function->ownsWasmCode = true; - function->module = i_module; - + function->module = i_module; + if (i_doCompilation and not i_module->runtime) _throw ("module must be loaded into runtime to compile function"); diff --git a/source/m3_compile.c b/source/m3_compile.c index 4ac321a..8b68d6b 100644 --- a/source/m3_compile.c +++ b/source/m3_compile.c @@ -1463,9 +1463,9 @@ _ (ReadLEB_u32 (& target, & o->wasm, o->wasmEnd)); IM3CompilationScope scope; _ (GetBlockScope (o, & scope, target)); - // TODO: don't need codepage rigmarole for - // no-param forward-branch targets - + // TODO: don't need codepage rigmarole for + // no-param forward-branch targets + _ (AcquireCompilationCodePage (o, & continueOpPage)); pc_t startPC = GetPagePC (continueOpPage); @@ -2431,7 +2431,9 @@ _ (Read_opcode (& opcode, & o->wasm, o->wasmEnd)); log_opco } IM3OpInfo opinfo = GetOpInfo(opcode); - _throwif (m3Err_unknownOpcode, opinfo == NULL); + + if (opinfo == NULL) + _throw (ErrorCompile (m3Err_unknownOpcode, o, "opcode '%x' not available", opcode)); if (opinfo->compiler) { _ ((* opinfo->compiler) (o, opcode)) diff --git a/source/m3_env.h b/source/m3_env.h index 33d0e00..d7649da 100644 --- a/source/m3_env.h +++ b/source/m3_env.h @@ -112,7 +112,7 @@ typedef struct M3Module M3MemoryInfo memoryInfo; bool memoryImported; - //bool hasWasmCodeCopy; + bool hasWasmCodeCopy; struct M3Module * next; } diff --git a/source/m3_exec.h b/source/m3_exec.h index 54b5a91..60d006c 100644 --- a/source/m3_exec.h +++ b/source/m3_exec.h @@ -1406,29 +1406,6 @@ d_m3Store_i (i64, i64) #undef m3MemCheck -//--------------------------------------------------------------------------------------------------------------------- -# if 0 //d_m3EnableOptimizations -//--------------------------------------------------------------------------------------------------------------------- - - #define d_m3BinaryOpWith1_i(TYPE, NAME, OPERATION) \ - d_m3Op(TYPE##_##NAME) \ - { \ - _r0 = _r0 OPERATION 1; \ - nextOp (); \ - } - - d_m3BinaryOpWith1_i (u64, Increment, +) - d_m3BinaryOpWith1_i (u32, Decrement, -) - - d_m3BinaryOpWith1_i (u32, ShiftLeft1, <<) - d_m3BinaryOpWith1_i (u64, ShiftLeft1, <<) - - d_m3BinaryOpWith1_i (u32, ShiftRight1, >>) - d_m3BinaryOpWith1_i (u64, ShiftRight1, >>) - -//--------------------------------------------------------------------------------------------------------------------- -# endif - //--------------------------------------------------------------------------------------------------------------------- // debug/profiling diff --git a/source/m3_module.c b/source/m3_module.c index d21b09e..26a31ba 100644 --- a/source/m3_module.c +++ b/source/m3_module.c @@ -41,6 +41,11 @@ void m3_FreeModule (IM3Module i_module) } m3_Free (i_module->globals); + if (i_module->hasWasmCodeCopy) + { + m3_Free (i_module->wasmStart); + } + m3_Free (i_module); } } diff --git a/source/m3_parse.c b/source/m3_parse.c index 4ea7bb1..e507699 100644 --- a/source/m3_parse.c +++ b/source/m3_parse.c @@ -572,7 +572,7 @@ M3Result ParseModuleSection (M3Module * o_module, u8 i_sectionType, bytes_t i_ } -M3Result m3_ParseModule (IM3Environment i_environment, IM3Module * o_module, cbytes_t i_bytes, u32 i_numBytes) +M3Result m3_ParseModule (IM3Environment i_environment, IM3Module * o_module, cbytes_t i_bytes, u32 i_numBytes, bool i_copyBytes) { M3Result result; @@ -582,10 +582,17 @@ _try { _throwifnull(module); module->name = ".unnamed"; m3log (parse, "load module: %d bytes", i_numBytes); module->startFunction = -1; - //module->hasWasmCodeCopy = false; - module->environment = i_environment; - - const u8 * pos = i_bytes; + module->environment = i_environment; + module->hasWasmCodeCopy = i_copyBytes; + + const u8 * pos = i_bytes; + + if (i_copyBytes) + { + pos = m3_CopyMem (i_bytes, i_numBytes); + _throwifnull (pos); + } + const u8 * end = pos + i_numBytes; module->wasmStart = pos; diff --git a/source/wasm3.h b/source/wasm3.h index 32fb3e1..4d4e816 100644 --- a/source/wasm3.h +++ b/source/wasm3.h @@ -15,6 +15,7 @@ #include #include +#include #include #include @@ -210,11 +211,12 @@ d_m3ErrorConst (trapStackOverflow, "[trap] stack overflow") // modules //------------------------------------------------------------------------------------------------------------------------------- - // i_wasmBytes data must be persistent during the lifetime of the module + // i_wasmBytes data must be persistent during the lifetime of the module unless i_copyWasmBytes is true M3Result m3_ParseModule (IM3Environment i_environment, IM3Module * o_module, const uint8_t * const i_wasmBytes, - uint32_t i_numWasmBytes); + uint32_t i_numWasmBytes, + bool i_copyWasmBytes); // Only modules not loaded into a M3Runtime need to be freed. A module is considered unloaded if // a. m3_LoadModule has not yet been called on that module. Or, diff --git a/test/internal/m3_test.c b/test/internal/m3_test.c index 9332d2d..0709d88 100644 --- a/test/internal/m3_test.c +++ b/test/internal/m3_test.c @@ -205,7 +205,7 @@ int main (int argc, const char * argv []) }; IM3Module module; - result = m3_ParseModule (env, & module, wasm, 44); expect (result == m3Err_none) + result = m3_ParseModule (env, & module, wasm, 44, false); expect (result == m3Err_none) result = m3_LoadModule (runtime, module); expect (result == m3Err_none)