diff --git a/source/m3_bind.c b/source/m3_bind.c index 550ac8b..bb50823 100644 --- a/source/m3_bind.c +++ b/source/m3_bind.c @@ -23,9 +23,12 @@ M3State; // TODO: This binding code only work'n for System V AMD64 ABI calling convention (macOS & Linux) // Needs work for MS cdecl -#define d_m3BindingArgList i64 _i0, i64 _i1, i64 _i2, i64 _i3, i64 _i4, i64 _i5, f64 _f0, f64 _f1, f64 _f2, f64 _f3, f64 _f4 -#define d_m3BindingArgs _i0, _i1, _i2, _i3, _i4, _i5, _f0, _f1, _f2, _f3, _f4 -#define d_m3BindingDefaultArgs 0,0,0,0,0,0, 0.,0.,0.,0.,0. +#define d_m3BindingArgList i64 _i0, i64 _i1, i64 _i2, i64 _i3, i64 _i4, i64 _i5, \ + f64 _f0, f64 _f1, f64 _f2, f64 _f3, f64 _f4 +#define d_m3BindingArgs _i0, _i1, _i2, _i3, _i4, _i5, \ + _f0, _f1, _f2, _f3, _f4 +#define d_m3BindingDefaultArgs 0,0,0,0,0,0, \ + 0.,0.,0.,0.,0. typedef m3ret_t (* M3ArgPusher) (d_m3BindingArgList, M3State * i_state); typedef f64 (* M3ArgPusherFpReturn) (d_m3BindingArgList, M3State * i_state); @@ -400,7 +403,8 @@ M3Result m3_LinkCFunction (IM3Module io_module, // -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -M3Result LinkRawFunction (IM3Module io_module, IM3Function io_function, ccstr_t /* unused: */ signature, const void * i_function) +// TODO: validate signature as well? +M3Result LinkRawFunction (IM3Module io_module, IM3Function io_function, ccstr_t signature, const void * i_function) { M3Result result = c_m3Err_none; d_m3Assert (io_module->runtime); diff --git a/source/m3_compile.c b/source/m3_compile.c index 70a7654..d9d5a7c 100644 --- a/source/m3_compile.c +++ b/source/m3_compile.c @@ -2099,7 +2099,9 @@ _ (Compile_ReserveConstants (o)); o->block.initStackIndex = o->stackIndex; pc_t pc2 = GetPagePC (o->page); - d_m3AssertFatal (pc2 == pc); + if (pc2 != pc) { + m3Abort("pc2 != pc"); + } _ (EmitOp (o, op_Entry)); EmitPointer (o, io_function); diff --git a/source/m3_compile.h b/source/m3_compile.h index 3e66b30..37e40d4 100644 --- a/source/m3_compile.h +++ b/source/m3_compile.h @@ -164,6 +164,7 @@ M3Result Compile_BlockStatements (IM3Compilation io); 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); diff --git a/source/m3_config_platforms.h b/source/m3_config_platforms.h index e14bfdd..14e358e 100644 --- a/source/m3_config_platforms.h +++ b/source/m3_config_platforms.h @@ -142,6 +142,26 @@ * Apply settings */ +# if defined (M3_COMPILER_MSVC) +# define vectorcall +# elif defined(WIN32) +# define vectorcall __vectorcall +# elif defined (ESP8266) +# include +# define vectorcall //ICACHE_FLASH_ATTR +# elif defined (ESP32) +# include "esp_system.h" +# define vectorcall IRAM_ATTR +# elif defined (FOMU) +# define vectorcall __attribute__((section(".ramtext"))) +# elif defined(HIFIVE1) +# include +# define vectorcall +# else +# define vectorcall +# endif + + # if defined(PARTICLE) # define d_m3LogOutput false # define d_m3MaxFunctionStackHeight 256 diff --git a/source/m3_core.c b/source/m3_core.c index e9f7d39..4cf5de0 100644 --- a/source/m3_core.c +++ b/source/m3_core.c @@ -9,16 +9,13 @@ #include "m3_core.h" -void m3NotImplemented() { - puts("Error: Not implemented"); +void m3Abort(const char* message) { + fprintf(stderr, "Error: %s\n", message); abort(); } -void m3AbortIfNot(bool condition) { - if (!condition) { - puts("Error: Fatal"); - abort(); - } +void m3NotImplemented() { + m3Abort("Not implemented"); } M3_WEAK diff --git a/source/m3_core.h b/source/m3_core.h index 7347ad0..f3a1406 100644 --- a/source/m3_core.h +++ b/source/m3_core.h @@ -108,10 +108,8 @@ const void * const cvptr_t; # ifdef DEBUG # define d_m3Assert(ASS) assert (ASS) -# define d_m3AssertFatal(ASS) assert (ASS) # else # define d_m3Assert(ASS) -# define d_m3AssertFatal(ASS) m3AbortIfNot (ASS) # endif typedef void /*const*/ * code_t; @@ -173,11 +171,13 @@ static const char * const c_waCompactTypes [] = { "0", "i", "I", "f", "F", "v" # define _m3Error(RESULT, RT, MOD, FUN, FILE, LINE, FORMAT, ...) (RESULT) #endif -#define ErrorRuntime(RESULT, RUNTIME, FORMAT, ...) _m3Error (RESULT, RUNTIME, NULL, NULL, __FILE__, __LINE__, FORMAT, ##__VA_ARGS__) -#define ErrorModule(RESULT, MOD, FORMAT, ...) _m3Error (RESULT, MOD->runtime, MOD, NULL, __FILE__, __LINE__, FORMAT, ##__VA_ARGS__) -#define ErrorCompile(RESULT, COMP, FORMAT, ...) _m3Error (RESULT, COMP->runtime, COMP->module, NULL, __FILE__, __LINE__, FORMAT, ##__VA_ARGS__) -//#define ErrorExec(RESULT, MODULE, FORMAT, ...) _m3Error (RESULT, COMP->runtime, COMP->module, NULL, __FILE__, __LINE__, FORMAT, ##__VA_ARGS__) +#define ErrorRuntime(RESULT, RUNTIME, FORMAT, ...) _m3Error (RESULT, RUNTIME, NULL, NULL, __FILE__, __LINE__, FORMAT, ##__VA_ARGS__) +#define ErrorModule(RESULT, MOD, FORMAT, ...) _m3Error (RESULT, MOD->runtime, MOD, NULL, __FILE__, __LINE__, FORMAT, ##__VA_ARGS__) +#define ErrorCompile(RESULT, COMP, FORMAT, ...) _m3Error (RESULT, COMP->runtime, COMP->module, NULL, __FILE__, __LINE__, FORMAT, ##__VA_ARGS__) +#if d_m3VerboseErrorMessages +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, ...); +#endif #if d_m3LogNativeStack void m3StackCheckInit (); @@ -189,8 +189,8 @@ size_t m3StackGetMax (); #define m3StackGetMax() 0 #endif +void m3Abort (const char* message); void m3NotImplemented (void); -void m3AbortIfNot (bool condition); void m3Yield (void); diff --git a/source/m3_env.c b/source/m3_env.c index d2828c8..522a69f 100644 --- a/source/m3_env.c +++ b/source/m3_env.c @@ -266,7 +266,7 @@ M3Result InitMemory (IM3Runtime io_runtime, IM3Module i_module) result = ResizeMemory (io_runtime, i_module->memoryInfo.initPages); } - _catch: return result; + return result; } @@ -319,7 +319,7 @@ M3Result ResizeMemory (IM3Runtime io_runtime, u32 i_numPages) } else result = c_m3Err_wasmMemoryOverflow; - _catch: return result; + return result; } @@ -376,9 +376,7 @@ M3Result InitDataSegments (M3Memory * io_memory, IM3Module io_module) bytes_t start = segment->initExpr; _ (EvaluateExpression (io_module, & segmentOffset, c_m3Type_i32, & start, segment->initExpr + segment->initExprSize)); - u32 minMemorySize = segment->size + segmentOffset + 1; m3log (runtime, "loading data segment: %d; size: %d; offset: %d", i, segment->size, segmentOffset); - -//_ (Module_EnsureMemorySize (io_module, io_memory, minMemorySize)); + m3log (runtime, "loading data segment: %d; size: %d; offset: %d", i, segment->size, segmentOffset); if (io_memory->wasmPages) { @@ -454,7 +452,7 @@ M3Result InitStartFunc (IM3Module io_module) M3Result result = c_m3Err_none; if (io_module->startFunction >= 0) { - if (io_module->startFunction >= io_module->numFunctions) { + if ((u32)io_module->startFunction >= io_module->numFunctions) { return "start function index out of bounds"; } IM3Function function = &io_module->functions [io_module->startFunction]; diff --git a/source/m3_env.h b/source/m3_env.h index 9214270..c5ac401 100644 --- a/source/m3_env.h +++ b/source/m3_env.h @@ -248,7 +248,7 @@ void ReleaseCodePage (IM3Runtime io_runtime, //size_t m3_GetMemorySize (void * i_memory); -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, ...); +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, ...); #endif /* m3_env_h */ diff --git a/source/m3_exec.c b/source/m3_exec.c index 7fb3bf8..6ca2d95 100644 --- a/source/m3_exec.c +++ b/source/m3_exec.c @@ -421,8 +421,7 @@ void ProfileHit (cstr_t i_operationName) { if (slot->opName != i_operationName) { - printf ("**** profiler slot collision; increase mask width: c_m3ProfilerSlotMask\n"); - abort (); + m3Abort ("profiler slot collision; increase c_m3ProfilerSlotMask"); } } diff --git a/source/m3_exec_defs.h b/source/m3_exec_defs.h index 4b5a7a7..54b0c65 100644 --- a/source/m3_exec_defs.h +++ b/source/m3_exec_defs.h @@ -11,40 +11,11 @@ #include "m3_core.h" -// default Windows x64 calling convention doesn't have enough registers for M3. It only supports -// 4 args passed through registers but its enhanced __vectorcall calling convention does. - -# if defined (M3_COMPILER_MSVC) -# define vectorcall -# elif defined(WIN32) -# define vectorcall __vectorcall -# elif defined (ESP8266) -# include -# define vectorcall //ICACHE_FLASH_ATTR -# elif defined (ESP32) -# include "esp_system.h" -# define vectorcall IRAM_ATTR -# elif defined (FOMU) -# define vectorcall __attribute__((section(".ramtext"))) -# elif defined(HIFIVE1) -# include -# define vectorcall -# define hotcall METAL_PLACE_IN_ITIM -# else -# define vectorcall -# endif - - -//--------------------------------------------------------------------------------------------------------------- - # define d_m3OpSig pc_t _pc, u64 * _sp, u8 * _mem, m3reg_t _r0, f64 _fp0 # define d_m3OpArgs _sp, _mem, _r0, _fp0 # define d_m3OpAllArgs _pc, _sp, _mem, _r0, _fp0 # define d_m3OpDefaultArgs 666, NAN - typedef m3ret_t (vectorcall * IM3Operation) (d_m3OpSig); - - #endif /* m3_exec_defs_h */ diff --git a/source/m3_module.c b/source/m3_module.c index a4f5f57..a66070e 100644 --- a/source/m3_module.c +++ b/source/m3_module.c @@ -146,10 +146,12 @@ i32 AllocatePrivateHeap (M3Memory * io_memory, i32 i_size) size_t size = (u8 *) io_memory->mallocated->end - io_memory->wasmPages; - d_m3AssertFatal (ptrOffset < size); + if (ptrOffset >= size) { + m3Abort("ptrOffset >= size"); + } return (i32) ptrOffset;*/ - + return 0; }