From 9540e82ae1de924aa6a7dc7de2e5c009f1a9267b Mon Sep 17 00:00:00 2001 From: Steven Massey Date: Sun, 1 Dec 2019 00:34:28 -0700 Subject: [PATCH] 32-bit slot progress --- source/m3_code.c | 8 ++++++- source/m3_code.h | 3 +-- source/m3_compile.c | 54 +++++++++++++++++++++++++++------------------ source/m3_core.h | 2 +- source/m3_env.c | 5 +++-- source/m3_exec.c | 2 +- source/m3_info.c | 26 +++++++++++----------- 7 files changed, 59 insertions(+), 41 deletions(-) diff --git a/source/m3_code.c b/source/m3_code.c index ae63b69..e13de71 100644 --- a/source/m3_code.c +++ b/source/m3_code.c @@ -68,12 +68,18 @@ void EmitWordImpl (IM3CodePage i_page, const void * i_word) } +pc_t GetPageStartPC (IM3CodePage i_page) +{ + return & i_page->code [0]; +} + + pc_t GetPagePC (IM3CodePage i_page) { if (i_page) return & i_page->code [i_page->info.lineIndex]; else - return 0; + return NULL; } diff --git a/source/m3_code.h b/source/m3_code.h index f30759b..cec3854 100644 --- a/source/m3_code.h +++ b/source/m3_code.h @@ -15,8 +15,6 @@ typedef struct M3CodePage { M3CodePageHeader info; -// code_t code [c_m3CodePageNumSlots]; - code_t code [1]; } M3CodePage; @@ -29,6 +27,7 @@ IM3CodePage NewCodePage (u32 i_minNumLines); void FreeCodePages (IM3CodePage i_page); //void CloseCodePage (IM3CodePage i_page); u32 NumFreeLines (IM3CodePage i_page); +pc_t GetPageStartPC (IM3CodePage i_page); pc_t GetPagePC (IM3CodePage i_page); void EmitWordImpl (IM3CodePage i_page, const void * i_word); diff --git a/source/m3_compile.c b/source/m3_compile.c index 923fb58..91581a2 100644 --- a/source/m3_compile.c +++ b/source/m3_compile.c @@ -78,6 +78,8 @@ void log_opcode (IM3Compilation o, u8 i_opcode) //------------------------------------------------------------------------------------------------------------------------- +static const IM3Operation c_setSetOps [] = { NULL, op_SetSlot_i32, op_SetSlot_i64, op_SetSlot_f32, op_SetSlot_f64 }; + // just want less letter and numbers to stare at down the way in the compiler table #define i_32 c_m3Type_i32 #define i_64 c_m3Type_i64 @@ -486,8 +488,8 @@ M3Result CopyTopSlot (IM3Compilation o, u16 i_destSlot) if (IsStackTopInRegister (o)) { - bool isFp = IsStackTopTypeFp (o); - op = isFp ? op_SetSlot_f64 : op_SetSlot_i64; + u8 type = GetStackTopType (o); + op = c_setSetOps [type]; } else op = op_CopySlot; @@ -539,7 +541,9 @@ M3Result MoveStackTopToRegister (IM3Compilation o) { u8 type = GetStackTopType (o); - IM3Operation op = IsFpType (type) ? op_SetRegister_f64 : op_SetRegister_i64; + static const IM3Operation setRegisterOps [] = { NULL, op_SetRegister_i32, op_SetRegister_i64, op_SetRegister_f32, op_SetRegister_f64 }; + + IM3Operation op = setRegisterOps [type]; _ (EmitOp (o, op)); _ (EmitTopSlotAndPop (o)); @@ -659,7 +663,7 @@ M3Result Compile_Const_f32 (IM3Compilation o, u8 i_opcode) M3Result result; f32 value; - union { u64 u; f64 f; } union64; + union { u64 u; f32 f; } union64; _ (Read_f32 (& value, & o->wasm, o->wasmEnd)); m3log (compile, d_indent "%s (const f32 = %f)", GetIndentionString (o), value); @@ -1424,9 +1428,9 @@ const M3OpInfo c_operations [] = M3OP( "i32.rotl", -1, i_32, d_binOpList (u32, Rotl) ), // 0x77 M3OP( "i32.rotr", -1, i_32, d_binOpList (u32, Rotr) ), // 0x78 - M3OP( "i64.clz", 0, i_32, d_unaryOpList (u64, Clz) ), // 0x79 - M3OP( "i64.ctz", 0, i_32, d_unaryOpList (u64, Ctz) ), // 0x7a - M3OP( "i64.popcnt", 0, i_32, d_unaryOpList (u64, Popcnt) ), // 0x7b + M3OP( "i64.clz", 0, i_64, d_unaryOpList (u64, Clz) ), // 0x79 + M3OP( "i64.ctz", 0, i_64, d_unaryOpList (u64, Ctz) ), // 0x7a + M3OP( "i64.popcnt", 0, i_64, d_unaryOpList (u64, Popcnt) ), // 0x7b M3OP( "i64.add", -1, i_64, d_commutativeBinOpList (i64, Add) ), // 0x7c M3OP( "i64.sub", -1, i_64, d_binOpList (i64, Subtract) ), // 0x7d @@ -1506,9 +1510,17 @@ const M3OpInfo c_operations [] = M3OP( "f32.reinterpret/i32", 0, f_32, d_unaryOpList(f32, Reinterpret_i32) ), // 0xbe M3OP( "f64.reinterpret/i64", 0, f_64, d_unaryOpList(f64, Reinterpret_i64) ), // 0xbf - // for code logging - M3OP( "Const", 1, any, op_Const ), - M3OP( "termination", 0, c_m3Type_void ) // termination for FindOperationInfo () +# ifdef DEBUG // for code logging: + M3OP( "m3.const", 1, any, op_Const ), + M3OP( "m3.entry", 0, none, op_Entry ), + M3OP( "i32.m3.setslot", 0, none, op_SetSlot_i32), + M3OP( "i64.m3.setslot", 0, none, op_SetSlot_i64), + M3OP( "f32.m3.setslot", 0, none, op_SetSlot_f32), + M3OP( "f64.m3.setslot", 0, none, op_SetSlot_f64), + M3OP( "m3.end", 0, none, op_End ), +# endif + + M3OP( "termination", 0, c_m3Type_void ) // termination for FindOperationInfo () }; @@ -1629,11 +1641,14 @@ M3Result CompileBlock (IM3Compilation o, u8 i_blockType, u8 i_blockOpcode) // save and clear the locals modification slots #if defined(M3_COMPILER_MSVC) - assert (numArgsAndLocals <= 128); - u16 locals [128]; + u16 locals [128]; // hmm, heap allocate?... + + if (numArgsAndLocals > 128) + _throw ("argument/local count overflow"); #else u16 locals [numArgsAndLocals]; #endif + memcpy (locals, o->wasmStack, numArgsAndLocals * sizeof (u16)); for (u32 i = 0; i < numArgsAndLocals; ++i) { @@ -1664,7 +1679,6 @@ _ (IsLocalReferencedWithCurrentBlock (o, & preserveToSlot, i)); // printf ("local usage: [%d] = %d\n", i, o->wasmStack [i]); } - _catch: return result; } @@ -1759,16 +1773,16 @@ M3Result Compile_Function (IM3Function io_function) { M3Result result = c_m3Err_none; m3log (compile, "compiling: '%s'; wasm-size: %d; numArgs: %d; return: %s", io_function->name, (u32) (io_function->wasmEnd - io_function->wasm), GetFunctionNumArgs (io_function), c_waTypes [GetFunctionReturnType (io_function)]); - IM3Runtime rt = io_function->module->runtime; + IM3Runtime runtime = io_function->module->runtime; IM3Compilation o = NULL; -_ (m3Malloc (& o, sizeof(M3Compilation))); +_ (m3Alloc (& o, M3Compilation, 1)); // TODO: i think an M3Compilation object could just live in M3Runtime. only one function is compiled at a time. - o->runtime = rt; + o->runtime = runtime; o->module = io_function->module; o->wasm = io_function->wasm; o->wasmEnd = io_function->wasmEnd; - o->page = AcquireCodePage (rt); + o->page = AcquireCodePage (runtime); if (o->page) { @@ -1821,10 +1835,8 @@ _ (m3Alloc (& io_function->constants, u64, numConstants)); else _throw (c_m3Err_mallocFailedCodePage); _catch: - { - ReleaseCodePage (rt, o->page); - } - + + ReleaseCodePage (runtime, o->page); m3Free (o); return result; diff --git a/source/m3_core.h b/source/m3_core.h index 505b523..ae8e547 100644 --- a/source/m3_core.h +++ b/source/m3_core.h @@ -155,7 +155,7 @@ typedef struct M3MemoryHeader M3MemoryHeader; -typedef struct M3CodePageInfo +typedef struct M3CodePageHeader { struct M3CodePage * next; diff --git a/source/m3_env.c b/source/m3_env.c index edfc881..8ff1f7a 100644 --- a/source/m3_env.c +++ b/source/m3_env.c @@ -455,6 +455,7 @@ M3Result m3_CallWithArgs (IM3Function i_function, uint32_t i_argc, const char { m3stack_t s = &stack[i]; ccstr_t str = i_argv[i]; + switch (ftype->argTypes[i]) { #ifdef USE_HUMAN_FRIENDLY_ARGS case c_m3Type_i32: *(i32*)(s) = atol(str); break; @@ -486,7 +487,7 @@ _ ((M3Result)Call (i_function->compiled, stack, linearMemory, d_m3OpDefaul case c_m3Type_i32: printf("Result: %u\n", *(u32*)(stack)); break; case c_m3Type_f32: { union { u32 u; f32 f; } union32; - union32.f = *(f64*)(stack); + union32.f = * (f32 *)(stack); printf("Result: %u\n", union32.u ); break; } @@ -601,7 +602,7 @@ IM3CodePage AcquireCodePageWithCapacity (IM3Runtime i_runtime, u32 i_lineCount void ReleaseCodePage (IM3Runtime i_runtime, IM3CodePage i_codePage) { -// DumpCodePage (i_codePage, /* startPC: */ NULL); + DumpCodePage (i_codePage, /* startPC: */ NULL); if (i_codePage) { diff --git a/source/m3_exec.c b/source/m3_exec.c index 53c6417..f9df267 100644 --- a/source/m3_exec.c +++ b/source/m3_exec.c @@ -186,7 +186,7 @@ d_m3OpDef (Entry) if (not r) SPrintArg (str, 99, _sp, function->funcType->returnType); - m3log (exec, " exit < %s %s %s %s\n", function->name, returnType ? "->" : "", str, r ? r : ""); + m3log (exec, " exit < %s %s %s %s\n", function->name, returnType ? "->" : "", str, r ? r : ""); #endif return r; diff --git a/source/m3_info.c b/source/m3_info.c index 9ba556b..db6b184 100644 --- a/source/m3_info.c +++ b/source/m3_info.c @@ -80,7 +80,7 @@ size_t SPrintArg (char * o_string, size_t i_n, m3stack_t i_sp, u8 i_type) else if (i_type == c_m3Type_i64) len = snprintf (o_string, i_n, "%ld", * i_sp); else if (i_type == c_m3Type_f32) - len = snprintf (o_string, i_n, "%f", * (f64 *) i_sp); // f32 value in 64-bit register + len = snprintf (o_string, i_n, "%f", * (f32 *) i_sp); else if (i_type == c_m3Type_f64) len = snprintf (o_string, i_n, "%lf", * (f64 *) i_sp); @@ -159,6 +159,7 @@ OpInfo FindOperationInfo (IM3Operation i_operation) return opInfo; } + void DecodeOperation (char * o_string, u8 i_opcode, IM3OpInfo i_opInfo, pc_t * o_pc) { pc_t pc = * o_pc; @@ -188,16 +189,19 @@ void DecodeOperation (char * o_string, u8 i_opcode, IM3OpInfo i_opInfo, pc_t * void DumpCodePage (IM3CodePage i_codePage, pc_t i_startPC) { - if (d_m3LogCodePages) - { m3log (code, "code page seq: %d", i_codePage->info.sequence); - pc_t pc = i_startPC ? i_startPC : (pc_t) i_codePage; +# if defined (DEBUG) && d_m3LogCodePages + + m3log (code, "code page seq: %d", i_codePage->info.sequence); + + pc_t pc = i_startPC ? i_startPC : GetPageStartPC (i_codePage); pc_t end = GetPagePC (i_codePage); m3log (code, "---------------------------------------------------"); + // TODO: this isn't fully implemented. blindly assumes each word is a Operation pointer while (pc < end) { - IM3Operation op = (IM3Operation)(* pc++); + IM3Operation op = (IM3Operation) (* pc++); if (op) { @@ -208,19 +212,15 @@ void DumpCodePage (IM3CodePage i_codePage, pc_t i_startPC) char infoString [1000] = { 0 }; DecodeOperation (infoString, i.opcode, i.info, & pc); -#ifdef DEBUG - m3log (code, "%p: %15s %-20s", pc - 1, i.info->name, infoString); -#else - m3log (code, "%p: %15s %-20s", pc - 1, "---", infoString); -#endif + m3log (code, "%p | %20s %-20s", pc - 1, i.info->name, infoString); } - else break; +// else break; } - else break; +// else break; } m3log (code, "---------------------------------------------------"); - } +# endif }