From cbfb1fbaa6f739435e5c70db6ae13d935f098b1c Mon Sep 17 00:00:00 2001 From: Volodymyr Shymanskyy Date: Thu, 30 Jan 2020 23:26:08 +0200 Subject: [PATCH] Update library version --- src/m3_api_libc.h | 12 ++++---- src/m3_code.h | 7 +++++ src/m3_compile.c | 75 ++++++++++++++++++++++++++-------------------- src/m3_compile.h | 7 ++++- src/m3_core.c | 3 +- src/m3_core.h | 10 +++++-- src/m3_emit.h | 8 +++++ src/m3_env.c | 14 +++------ src/m3_env.h | 6 ++++ src/m3_exec.h | 15 +++++++++- src/m3_exec_defs.h | 8 +++++ src/m3_info.h | 8 +++++ src/m3_parse.c | 12 +++++--- src/wasm3.h | 5 ++-- 14 files changed, 131 insertions(+), 59 deletions(-) diff --git a/src/m3_api_libc.h b/src/m3_api_libc.h index b8a7b6d..1027afb 100644 --- a/src/m3_api_libc.h +++ b/src/m3_api_libc.h @@ -10,15 +10,15 @@ #include "m3_core.h" -# if defined(__cplusplus) +#if defined(__cplusplus) extern "C" { -# endif +#endif - M3Result m3_LinkLibC (IM3Module io_module); - M3Result m3_LinkSpecTest (IM3Module io_module); +M3Result m3_LinkLibC (IM3Module io_module); +M3Result m3_LinkSpecTest (IM3Module io_module); -# if defined(__cplusplus) +#if defined(__cplusplus) } -# endif +#endif #endif // m3_api_libc_h diff --git a/src/m3_code.h b/src/m3_code.h index 6e29bb1..e8f2b56 100644 --- a/src/m3_code.h +++ b/src/m3_code.h @@ -10,6 +10,9 @@ #include "m3_core.h" +#if defined(__cplusplus) +extern "C" { +#endif typedef struct M3CodePage { @@ -45,4 +48,8 @@ void dump_code_page (IM3CodePage i_codePage, pc_t # define EmitWord64(page, val) EmitWord_impl(page, (void*)(val)) #endif +#if defined(__cplusplus) +} +#endif + #endif // m3_code_h diff --git a/src/m3_compile.c b/src/m3_compile.c index cae5bc5..e5c71d2 100644 --- a/src/m3_compile.c +++ b/src/m3_compile.c @@ -42,6 +42,11 @@ bool IsFpRegisterLocation (i16 i_location) { return (i_location == d_m3 bool IsIntRegisterLocation (i16 i_location) { return (i_location == d_m3Reg0SlotAlias); } +u32 GetTypeNumSlots (u8 i_type) +{ + return Is64BitType (i_type) ? 1 : 1; +} + i16 GetStackTopIndex (IM3Compilation o) { return o->stackIndex - 1; @@ -77,7 +82,6 @@ u8 GetStackBottomType (IM3Compilation o, u16 i_offset) } - u8 GetBlockType (IM3Compilation o) { return o->block.type; @@ -154,10 +158,10 @@ void MarkSlotAllocated (IM3Compilation o, u16 i_slot) } -bool AllocateSlot (IM3Compilation o, u16 * o_execSlot) +bool AllocateSlots (IM3Compilation o, u16 * o_execSlot, u8 i_type) { bool found = false; - + // search for empty slot in the execution stack i16 i = o->firstSlotIndex; while (i < d_m3MaxFunctionStackHeight) @@ -194,11 +198,15 @@ M3Result IncrementSlotUsageCount (IM3Compilation o, u16 i_slot) } -void DeallocateSlot (IM3Compilation o, i16 i_slotIndex) +void DeallocateSlot (IM3Compilation o, i16 i_slotIndex, u8 i_type) { d_m3Assert (i_slotIndex >= o->firstSlotIndex); d_m3Assert (o->m3Slots [i_slotIndex]); - if (-- o->m3Slots [i_slotIndex] == 0) - o->numAllocatedExecSlots--; + for (u32 i = 0; i < GetTypeNumSlots (i_type); ++i, ++i_slotIndex) + { + if (-- o->m3Slots [i_slotIndex] == 0) + o->numAllocatedExecSlots--; + } + } @@ -268,15 +276,15 @@ M3Result PreserveRegisterIfOccupied (IM3Compilation o, u8 i_registerType) { u16 stackIndex = GetRegisterStackIndex (o, regSelect); DeallocateRegister (o, regSelect); + + u8 type = GetStackBottomType (o, stackIndex); // and point to a exec slot u16 slot; - if (AllocateSlot (o, & slot)) + if (AllocateSlots (o, & slot, type)) { o->wasmStack [stackIndex] = slot; - u8 type = o->typeStack [stackIndex]; - _ (EmitOp (o, c_setSetOps [type])); EmitSlotOffset (o, slot); } @@ -328,7 +336,7 @@ _ (PreserveRegisterIfOccupied (o, c_m3Type_f64)); //---------------------------------------------------------------------------------------------------------------------- -M3Result Push (IM3Compilation o, u8 i_m3Type, i16 i_location) +M3Result Push (IM3Compilation o, u8 i_type, i16 i_location) { M3Result result = m3Err_none; @@ -344,7 +352,7 @@ M3Result Push (IM3Compilation o, u8 i_m3Type, i16 i_location) } o->wasmStack [stackIndex] = i_location; - o->typeStack [stackIndex] = i_m3Type; + o->typeStack [stackIndex] = i_type; if (IsRegisterLocation (i_location)) { @@ -358,10 +366,10 @@ M3Result Push (IM3Compilation o, u8 i_m3Type, i16 i_location) } -M3Result PushRegister (IM3Compilation o, u8 i_m3Type) +M3Result PushRegister (IM3Compilation o, u8 i_type) { - i16 location = IsFpType (i_m3Type) ? d_m3Fp0SlotAlias : d_m3Reg0SlotAlias; d_m3Assert (i_m3Type or IsStackPolymorphic (o)); - return Push (o, i_m3Type, location); + i16 location = IsFpType (i_type) ? d_m3Fp0SlotAlias : d_m3Reg0SlotAlias; d_m3Assert (i_type or IsStackPolymorphic (o)); + return Push (o, i_type, location); } @@ -374,6 +382,7 @@ M3Result Pop (IM3Compilation o) o->stackIndex--; // printf ("pop: %d\n", (i32) o->stackIndex); i16 location = o->wasmStack [o->stackIndex]; + u8 type = o->typeStack [o->stackIndex]; if (IsRegisterLocation (location)) { @@ -382,7 +391,7 @@ M3Result Pop (IM3Compilation o) } else if (location >= o->firstSlotIndex) { - DeallocateSlot (o, location); + DeallocateSlot (o, location, type); } m3logif (stack, dump_type_stack (o)) @@ -414,15 +423,15 @@ _ (Pop (o)); } -M3Result _PushAllocatedSlotAndEmit (IM3Compilation o, u8 i_m3Type, bool i_doEmit) +M3Result _PushAllocatedSlotAndEmit (IM3Compilation o, u8 i_type, bool i_doEmit) { M3Result result = m3Err_none; u16 slot; - if (AllocateSlot (o, & slot)) + if (AllocateSlots (o, & slot, i_type)) { -_ (Push (o, i_m3Type, slot)); +_ (Push (o, i_type, slot)); if (i_doEmit) EmitSlotOffset (o, slot); @@ -433,19 +442,19 @@ _ (Push (o, i_m3Type, slot)); } -M3Result PushAllocatedSlotAndEmit (IM3Compilation o, u8 i_m3Type) +M3Result PushAllocatedSlotAndEmit (IM3Compilation o, u8 i_type) { - return _PushAllocatedSlotAndEmit (o, i_m3Type, true); + return _PushAllocatedSlotAndEmit (o, i_type, true); } -M3Result PushAllocatedSlot (IM3Compilation o, u8 i_m3Type) +M3Result PushAllocatedSlot (IM3Compilation o, u8 i_type) { - return _PushAllocatedSlotAndEmit (o, i_m3Type, false); + return _PushAllocatedSlotAndEmit (o, i_type, false); } -M3Result PushConst (IM3Compilation o, u64 i_word, u8 i_m3Type) +M3Result PushConst (IM3Compilation o, u64 i_word, u8 i_type) { M3Result result = m3Err_none; @@ -459,7 +468,7 @@ M3Result PushConst (IM3Compilation o, u64 i_word, u8 i_m3Type) if (o->constants [i] == i_word) { location = o->firstConstSlotIndex + i; -_ (Push (o, i_m3Type, location)); +_ (Push (o, i_type, location)); break; } } @@ -471,13 +480,13 @@ _ (Push (o, i_m3Type, location)); o->constants [numConstants] = i_word; location = o->constSlotIndex++; -_ (Push (o, i_m3Type, location)); +_ (Push (o, i_type, location)); } else { _ (EmitOp (o, op_Const)); EmitConstant64 (o, i_word); -_ (PushAllocatedSlotAndEmit (o, i_m3Type)); +_ (PushAllocatedSlotAndEmit (o, i_type)); } } @@ -686,7 +695,9 @@ M3Result FindReferencedLocalWithinCurrentBlock (IM3Compilation o, u16 * o_pres { if (* o_preservedSlotIndex == i_localIndex) { - if (not AllocateSlot (o, o_preservedSlotIndex)) + u8 localType = GetStackBottomType (o, i_localIndex); + + if (not AllocateSlots (o, o_preservedSlotIndex, localType)) _throw (m3Err_functionStackOverflow); } else @@ -1239,7 +1250,7 @@ _ (ReadLEB_i7 (& reserved, & o->wasm, o->wasmEnd)); _ (EmitOp (o, op_MemCurrent)); -_ (PushRegister (o, c_m3Type_i32)); // i32? +_ (PushRegister (o, c_m3Type_i32)); _catch: return result; } @@ -1257,7 +1268,7 @@ _ (Pop (o)); _ (EmitOp (o, op_MemGrow)); -_ (PushRegister (o, c_m3Type_i32)); // i32? +_ (PushRegister (o, c_m3Type_i32)); _catch: return result; } @@ -1760,9 +1771,9 @@ const M3OpInfo c_operations [] = M3OP( "i32.div_u", -1, i_32, d_binOpList (u32, Divide) ), // 0x6e M3OP( "i32.rem_s", -1, i_32, d_binOpList (i32, Remainder) ), // 0x6f M3OP( "i32.rem_u", -1, i_32, d_binOpList (u32, Remainder) ), // 0x70 - M3OP( "i32.and", -1, i_32, d_commutativeBinOpList (u64, And) ), // 0x71 - M3OP( "i32.or", -1, i_32, d_commutativeBinOpList (u64, Or) ), // 0x72 - M3OP( "i32.xor", -1, i_32, d_commutativeBinOpList (u64, Xor) ), // 0x73 + M3OP( "i32.and", -1, i_32, d_commutativeBinOpList (u32, And) ), // 0x71 + M3OP( "i32.or", -1, i_32, d_commutativeBinOpList (u32, Or) ), // 0x72 + M3OP( "i32.xor", -1, i_32, d_commutativeBinOpList (u32, Xor) ), // 0x73 M3OP( "i32.shl", -1, i_32, d_binOpList (u32, ShiftLeft) ), // 0x74 M3OP( "i32.shr_s", -1, i_32, d_binOpList (i32, ShiftRight) ), // 0x75 M3OP( "i32.shr_u", -1, i_32, d_binOpList (u32, ShiftRight) ), // 0x76 diff --git a/src/m3_compile.h b/src/m3_compile.h index 1f16684..0a2290b 100644 --- a/src/m3_compile.h +++ b/src/m3_compile.h @@ -11,6 +11,9 @@ #include "m3_code.h" #include "m3_exec_defs.h" +#if defined(__cplusplus) +extern "C" { +#endif enum { @@ -164,6 +167,8 @@ M3Result Compile_Function (IM3Function io_function); bool PeekNextOpcode (IM3Compilation o, u8 i_opcode); u16 GetMaxExecSlot (IM3Compilation o); -//M3Result Optimize_ConstOp (IM3Compilation o, u64 i_word, u8 i_waType); +#if defined(__cplusplus) +} +#endif #endif // m3_compile_h diff --git a/src/m3_core.c b/src/m3_core.c index 7c27d68..48f0911 100644 --- a/src/m3_core.c +++ b/src/m3_core.c @@ -22,8 +22,9 @@ void m3NotImplemented() { } M3_WEAK -void m3Yield () +M3Result m3_Yield () { + return m3Err_none; } #if d_m3FixedHeap diff --git a/src/m3_core.h b/src/m3_core.h index 98ab9ab..71a70c7 100644 --- a/src/m3_core.h +++ b/src/m3_core.h @@ -17,6 +17,10 @@ #include "wasm3.h" #include "m3_config.h" +#if defined(__cplusplus) +extern "C" { +#endif + #if !defined(d_m3ShortTypesDefined) typedef double f64; typedef float f32; @@ -193,8 +197,6 @@ size_t m3StackGetMax (); void m3Abort (const char* message); void m3NotImplemented (void); -void m3Yield (void); - M3Result m3Malloc (void ** o_ptr, size_t i_size); void * m3Realloc (void * i_ptr, size_t i_newSize, size_t i_oldSize); void m3Free_impl (void * o_ptr); @@ -225,4 +227,8 @@ size_t SPrintArg (char * o_string, size_t i_n, m3stack_t i_sp void ReportError (IM3Runtime io_runtime, IM3Module i_module, IM3Function i_function, ccstr_t i_errorMessage, ccstr_t i_file, u32 i_lineNum); +#if defined(__cplusplus) +} +#endif + #endif // m3_core_h diff --git a/src/m3_emit.h b/src/m3_emit.h index 40bb7f9..0251ab8 100644 --- a/src/m3_emit.h +++ b/src/m3_emit.h @@ -10,6 +10,10 @@ #include "m3_compile.h" +#if defined(__cplusplus) +extern "C" { +#endif + M3Result BridgeToNewPageIfNecessary (IM3Compilation o); M3Result EnsureCodePageNumLines (IM3Compilation o, u32 i_numLines); @@ -22,4 +26,8 @@ void * ReservePointer (IM3Compilation o); pc_t GetPC (IM3Compilation o); +#if defined(__cplusplus) +} +#endif + #endif // m3_emit_h diff --git a/src/m3_env.c b/src/m3_env.c index a038064..370fe94 100644 --- a/src/m3_env.c +++ b/src/m3_env.c @@ -455,21 +455,15 @@ M3Result InitStartFunc (IM3Module io_module) { M3Result result = m3Err_none; - if (io_module->startFunction >= 0) { - if ((u32)io_module->startFunction >= io_module->numFunctions) { - return "start function index out of bounds"; - } - IM3Function function = &io_module->functions [io_module->startFunction]; - if (not function) { - return "start function not found"; - } + if (io_module->startFunction >= 0) + { + IM3Function function = & io_module->functions [io_module->startFunction]; if (not function->compiled) { _ (Compile_Function (function)); - if (result) - function = NULL; } + _ (m3_Call(function)); io_module->startFunction = -1; diff --git a/src/m3_env.h b/src/m3_env.h index 2a9154e..b7c64e3 100644 --- a/src/m3_env.h +++ b/src/m3_env.h @@ -13,6 +13,9 @@ #include "m3_exec.h" #include "m3_compile.h" +#if defined(__cplusplus) +extern "C" { +#endif typedef struct M3FuncType { @@ -252,5 +255,8 @@ void ReleaseCodePage (IM3Runtime io_runtime, M3Result m3Error (M3Result i_result, IM3Runtime i_runtime, IM3Module i_module, IM3Function i_function, const char * const i_file, u32 i_lineNum, const char * const i_errorMessage, ...); +#if defined(__cplusplus) +} +#endif #endif // m3_env_h diff --git a/src/m3_exec.h b/src/m3_exec.h index e7e2f02..b5e43e2 100644 --- a/src/m3_exec.h +++ b/src/m3_exec.h @@ -29,6 +29,10 @@ #include #include +#if defined(__cplusplus) +extern "C" { +#endif + # define rewrite_op(OP) * ((void **) (_pc-1)) = (void*)(OP) # define d_m3RetSig static inline m3ret_t vectorcall @@ -67,7 +71,9 @@ d_m3RetSig profileOp (d_m3OpSig, cstr_t i_operationName); d_m3RetSig Call (d_m3OpSig) { - m3Yield (); + m3ret_t possible_trap = m3_Yield (); + if (UNLIKELY(possible_trap)) return possible_trap; + return nextOpDirect(); } @@ -161,6 +167,10 @@ d_m3OpFunc_i (u32, ShiftLeft, OP_SHL_32) d_m3OpFunc_i (u64, ShiftLeft d_m3OpFunc_i (i32, ShiftRight, OP_SHR_32) d_m3OpFunc_i (i64, ShiftRight, OP_SHR_64) d_m3OpFunc_i (u32, ShiftRight, OP_SHR_32) d_m3OpFunc_i (u64, ShiftRight, OP_SHR_64) +d_m3CommutativeOp_i (u32, And, &) +d_m3CommutativeOp_i (u32, Or, |) +d_m3CommutativeOp_i (u32, Xor, ^) + d_m3CommutativeOp_i (u64, And, &) d_m3CommutativeOp_i (u64, Or, |) d_m3CommutativeOp_i (u64, Xor, ^) @@ -916,5 +926,8 @@ d_m3RetSig profileOp (d_m3OpSig, cstr_t i_operationName) } # endif +#if defined(__cplusplus) +} +#endif #endif // m3_exec_h diff --git a/src/m3_exec_defs.h b/src/m3_exec_defs.h index 471d5e5..cfdb267 100644 --- a/src/m3_exec_defs.h +++ b/src/m3_exec_defs.h @@ -10,6 +10,10 @@ #include "m3_core.h" +#if defined(__cplusplus) +extern "C" { +#endif + # define d_m3OpSig pc_t _pc, u64 * _sp, M3MemoryHeader * _mem, m3reg_t _r0, f64 _fp0 # define d_m3OpArgs _sp, _mem, _r0, _fp0 # define d_m3OpAllArgs _pc, _sp, _mem, _r0, _fp0 @@ -19,4 +23,8 @@ typedef m3ret_t (vectorcall * IM3Operation) (d_m3OpSig); +#if defined(__cplusplus) +} +#endif + #endif // m3_exec_defs_h diff --git a/src/m3_info.h b/src/m3_info.h index 94e7346..5d9ce05 100644 --- a/src/m3_info.h +++ b/src/m3_info.h @@ -10,6 +10,10 @@ #include "m3_compile.h" +#if defined(__cplusplus) +extern "C" { +#endif + #if d_m3LogOutput void dump_type_stack (IM3Compilation o); @@ -28,4 +32,8 @@ void log_emit (IM3Compilation o, IM3Operation i_operat #endif // d_m3LogOutput +#if defined(__cplusplus) +} +#endif + #endif // m3_info_h diff --git a/src/m3_parse.c b/src/m3_parse.c index 9e7ec7c..52f0daf 100644 --- a/src/m3_parse.c +++ b/src/m3_parse.c @@ -234,10 +234,14 @@ M3Result ParseSection_Start (IM3Module io_module, bytes_t i_bytes, cbytes_t i_ { M3Result result = m3Err_none; - u32 startFunc; -_ (ReadLEB_u32 (& startFunc, & i_bytes, i_end)); m3log (parse, "** Start Function: %d", startFunc); - - io_module->startFunction = startFunc; + u32 startFuncIndex; +_ (ReadLEB_u32 (& startFuncIndex, & i_bytes, i_end)); m3log (parse, "** Start Function: %d", startFunc); + + if (startFuncIndex < io_module->numFunctions) + { + io_module->startFunction = startFuncIndex; + } + else result = "start function index out of bounds"; _catch: return result; } diff --git a/src/wasm3.h b/src/wasm3.h index d1b3195..ca1cb0d 100644 --- a/src/wasm3.h +++ b/src/wasm3.h @@ -10,8 +10,8 @@ #define M3_VERSION_MAJOR 0 #define M3_VERSION_MINOR 4 -#define M3_VERSION_REV 5 -#define M3_VERSION "0.4.5" +#define M3_VERSION_REV 6 +#define M3_VERSION "0.4.6" #include #include @@ -211,6 +211,7 @@ d_m3ErrorConst (trapStackOverflow, "[trap] stack overflow") //------------------------------------------------------------------------------------------------------------------------------- // functions //------------------------------------------------------------------------------------------------------------------------------- + M3Result m3_Yield (void); M3Result m3_FindFunction (IM3Function * o_function, IM3Runtime i_runtime,