From a033184b5a2d8406d3316bfd4a0a928b7884fc7f Mon Sep 17 00:00:00 2001 From: Steven Massey Date: Sat, 10 Apr 2021 12:43:01 -0700 Subject: [PATCH] random stuff --- source/m3_function.c | 29 +++++++++++++++++++---------- source/m3_function.h | 19 ++++++++++--------- source/m3_info.c | 1 + source/m3_module.c | 2 +- source/wasm3.h | 6 +++++- 5 files changed, 36 insertions(+), 21 deletions(-) diff --git a/source/m3_function.c b/source/m3_function.c index 03a4f9a..6f128d3 100644 --- a/source/m3_function.c +++ b/source/m3_function.c @@ -26,12 +26,28 @@ bool AreFuncTypesEqual (const IM3FuncType i_typeA, const IM3FuncType i_typeB) return false; } -u32 GetFuncTypeNumReturns (const IM3FuncType i_funcType) +u16 GetFuncTypeNumReturns (const IM3FuncType i_funcType) { return i_funcType ? i_funcType->numRets : 0; } +u8 GetFuncTypeReturnType (const IM3FuncType i_funcType, u16 i_index) +{ + u8 type = c_m3Type_unknown; + + if (i_funcType) + { + if (i_index < i_funcType->numRets) + { + type = i_funcType->types [i_index]; + } + } + + return type; +} + + //--------------------------------------------------------------------------------------------------------------- @@ -178,16 +194,9 @@ u16 GetFunctionNumReturns (IM3Function i_function) } -u8 GetFunctionReturnType (IM3Function i_function, u32 i_index) +u8 GetFunctionReturnType (const IM3Function i_function, u16 i_index) { - u8 type = c_m3Type_none; - - if (i_index < GetFunctionNumReturns (i_function)) - { - type = i_function->funcType->types [i_index]; - } - - return type; + return i_function ? GetFuncTypeReturnType (i_function->funcType, i_index) : c_m3Type_unknown; } diff --git a/source/m3_function.h b/source/m3_function.h index 873f66c..a3d39d6 100644 --- a/source/m3_function.h +++ b/source/m3_function.h @@ -29,8 +29,8 @@ typedef M3FuncType * IM3FuncType; M3Result AllocFuncType (IM3FuncType * o_functionType, u32 i_numTypes); bool AreFuncTypesEqual (const IM3FuncType i_typeA, const IM3FuncType i_typeB); -u32 GetFuncTypeNumReturns (const IM3FuncType i_funcType); - +u16 GetFuncTypeNumReturns (const IM3FuncType i_funcType); +u8 GetFuncTypeReturnType (const IM3FuncType i_funcType, u16 i_index); //--------------------------------------------------------------------------------------------------------------------------------- @@ -59,9 +59,10 @@ typedef struct M3Function u32 hits; #endif -#if d_m3EnableStrace >= 2 - u16 index; -#endif +# if d_m3EnableStrace >= 2 || d_m3LogCompile + u32 index; +# endif + u16 maxStackSlots; u16 numRetSlots; @@ -70,10 +71,10 @@ typedef struct M3Function u16 numLocals; // not including args u16 numLocalBytes; - void * constants; - u16 numConstantBytes; + bool ownsWasmCode; - bool ownsWasmCode; + u16 numConstantBytes; + void * constants; } M3Function; @@ -86,7 +87,7 @@ u16 GetFunctionNumArgs (IM3Function i_function); u8 GetFunctionArgType (IM3Function i_function, u32 i_index); u16 GetFunctionNumReturns (IM3Function i_function); -u8 GetFunctionReturnType (IM3Function i_function, u32 i_index); +u8 GetFunctionReturnType (const IM3Function i_function, u16 i_index); u32 GetFunctionNumArgsAndLocals (IM3Function i_function); diff --git a/source/m3_info.c b/source/m3_info.c index edfedec..cc53124 100644 --- a/source/m3_info.c +++ b/source/m3_info.c @@ -26,6 +26,7 @@ void m3_PrintM3Info () // printf (" sizeof M3CodePage : %zu bytes (%d slots) \n", sizeof (M3CodePage), c_m3CodePageNumSlots); printf (" sizeof M3MemPage : %u bytes \n", d_m3MemPageSize); printf (" sizeof M3Compilation : %zu bytes \n", sizeof (M3Compilation)); + printf (" sizeof M3Function : %zu bytes \n", sizeof (M3Function)); printf ("----------------------------------------------------------------\n\n"); } diff --git a/source/m3_module.c b/source/m3_module.c index 450d947..6d59ee0 100644 --- a/source/m3_module.c +++ b/source/m3_module.c @@ -77,7 +77,7 @@ _try { IM3Function func = Module_GetFunction (io_module, index); func->funcType = ft; -#if d_m3EnableStrace >= 2 +#if d_m3EnableStrace >= 2 || d_m3LogCompile func->index = index; #endif diff --git a/source/wasm3.h b/source/wasm3.h index 39fd5ad..cb597a0 100644 --- a/source/wasm3.h +++ b/source/wasm3.h @@ -148,7 +148,8 @@ d_m3ErrorConst (functionStackOverflow, "compiling function overran its d_m3ErrorConst (functionStackUnderrun, "compiling function underran the stack") d_m3ErrorConst (mallocFailedCodePage, "memory allocation failed when acquiring a new M3 code page") d_m3ErrorConst (settingImmutableGlobal, "attempting to set an immutable global") -d_m3ErrorConst (typeMismatch, "malformed Wasm: incorrect type on stack") +d_m3ErrorConst (typeMismatch, "incorrect type on stack") +d_m3ErrorConst (typeCountMismatch, "incorrect value count on stack") // runtime errors d_m3ErrorConst (missingCompiledCode, "function is missing compiled m3 code") @@ -225,6 +226,9 @@ d_m3ErrorConst (trapStackOverflow, "[trap] stack overflow") // Calling m3_RunStart is optional M3Result m3_RunStart (IM3Module i_module); + // Arguments and return values are passed in and out through the stack pointer _sp. + // Placeholder return value slots are first and arguments after. So, the first argument is at _sp [numReturns] + // Return values should be written into _sp [0] to _sp [num_returns - 1] typedef const void * (* M3RawCall) (IM3Runtime runtime, IM3ImportContext _ctx, uint64_t * _sp, void * _mem); M3Result m3_LinkRawFunction (IM3Module io_module,