From 3defa25b2d409fea31d70dabea3ba02ea720b136 Mon Sep 17 00:00:00 2001 From: Volodymyr Shymanskyy Date: Fri, 11 Oct 2019 04:46:41 +0300 Subject: [PATCH] Fix 32-bit mode (x86) --- CMakeLists.txt | 5 +++++ source/m3_core.h | 11 +---------- source/m3_env.c | 6 +++--- source/m3_env.h | 2 +- source/m3_exception.h | 1 + source/m3_exec.c | 2 +- source/m3_host.c | 4 ++++ source/m3_info.c | 2 +- source/m3_module.c | 6 +++--- source/m3_module.h | 2 +- 10 files changed, 21 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0f92383..d7f29b1 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,11 @@ if(CLANG_CL) set(CMAKE_LINKER "lld-link") endif() +if(M EQUAL 32) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -m32") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m32") +endif() + if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE "Release" CACHE STRING "set build type to Release") endif() diff --git a/source/m3_core.h b/source/m3_core.h index a548519..e5e0a2e 100644 --- a/source/m3_core.h +++ b/source/m3_core.h @@ -26,22 +26,13 @@ typedef uint8_t u8; typedef int8_t i8; -#if __LP64__ - typedef i64 m3reg_t; - - typedef u64 m3word_t; -#else - typedef i32 m3reg_t; - - typedef u32 m3word_t; -#endif - typedef const void * m3ret_t; typedef const char * cstr_t; typedef const char * const ccstr_t; typedef const u8 * bytes_t; typedef const u8 * const cbytes_t; +typedef i64 m3reg_t; typedef u64 * m3stack_t; typedef diff --git a/source/m3_env.c b/source/m3_env.c index 7f49ab4..cc4044e 100644 --- a/source/m3_env.c +++ b/source/m3_env.c @@ -73,7 +73,7 @@ void FreeImportInfo (M3ImportInfo * i_info) void InitRuntime (IM3Runtime io_runtime, u32 i_stackSizeInBytes) { m3Malloc (& io_runtime->stack, i_stackSizeInBytes); - io_runtime->numStackSlots = i_stackSizeInBytes / sizeof (m3word_t); + io_runtime->numStackSlots = i_stackSizeInBytes / sizeof (m3reg_t); } @@ -448,9 +448,9 @@ _ (Call (i_function->compiled, stack, linearMemory, d_m3OpDefaultArgs)); case c_m3Type_f64: printf("Result: %lf\n", *(f64*)(stack)); break; #else case c_m3Type_i32: printf("Result: %u\n", *(u32*)(stack)); break; - case c_m3Type_i64: printf("Result: %lu\n", *(u64*)(stack)); break; + case c_m3Type_i64: printf("Result: %llu\n", *(u64*)(stack)); break; case c_m3Type_f32: { f32 val = *(f64*)(stack); printf("Result: %u\n", *(u32*)&val ); } break; - case c_m3Type_f64: printf("Result: %lu\n", *(u64*)(stack)); break; + case c_m3Type_f64: printf("Result: %llu\n", *(u64*)(stack)); break; #endif default: _throw("unknown return type"); } diff --git a/source/m3_env.h b/source/m3_env.h index 6541813..f2eb92e 100644 --- a/source/m3_env.h +++ b/source/m3_env.h @@ -39,7 +39,7 @@ typedef struct M3Function cstr_t name; - m3word_t hits; + u32 hits; IM3FuncType funcType; diff --git a/source/m3_exception.h b/source/m3_exception.h index c9d2123..7b0fa87 100644 --- a/source/m3_exception.h +++ b/source/m3_exception.h @@ -12,6 +12,7 @@ // some macros to emulate try/catch #define EXC_PRINT //printf("Exc: %s:%d\n", __FILE__, __LINE__); +#define _try #define _(TRY) { result = TRY; if (result) { EXC_PRINT; goto _catch; } } #define _throw(ERROR) { result = ERROR; EXC_PRINT; goto _catch; } diff --git a/source/m3_exec.c b/source/m3_exec.c index d51620d..3d5c760 100644 --- a/source/m3_exec.c +++ b/source/m3_exec.c @@ -100,7 +100,7 @@ d_m3OpDef (Compile) if (not result) { // patch up compiled pc and call rewriten op_Call - *((m3word_t *) --_pc) = (m3word_t) (function->compiled); + *((size_t *) --_pc) = (size_t) (function->compiled); --_pc; result = nextOp (); } diff --git a/source/m3_host.c b/source/m3_host.c index 2d82a2e..b03371f 100644 --- a/source/m3_host.c +++ b/source/m3_host.c @@ -159,7 +159,11 @@ i32 m3_fwrite (void * i_ptr, i32 i_size, i32 i_count, FILE * i_file) i32 m3_write (i32 i_fd, const void * i_data, i32 i_count) { +#if defined(WIN32) + return 0; +#else return (i32) write (i_fd, i_data, i_count); +#endif } diff --git a/source/m3_info.c b/source/m3_info.c index 4bd375a..4c9b002 100644 --- a/source/m3_info.c +++ b/source/m3_info.c @@ -33,7 +33,7 @@ void m3_PrintRuntimeInfo (IM3Runtime i_runtime) { printf ("\n-- m3 runtime -------------------------------------------------\n"); - printf (" stack-size: %lu \n\n", i_runtime->numStackSlots * sizeof (m3word_t)); + printf (" stack-size: %lu \n\n", i_runtime->numStackSlots * sizeof (m3reg_t)); u32 moduleIndex = 0; ForEachModule (i_runtime, (ModuleVisitor) v_PrintEnvModuleInfo, & moduleIndex); diff --git a/source/m3_module.c b/source/m3_module.c index cbe4eb2..1b20947 100644 --- a/source/m3_module.c +++ b/source/m3_module.c @@ -96,7 +96,7 @@ IM3Function Module_GetFunction (IM3Module i_module, u32 i_functionIndex) } -M3Result Module_EnsureMemorySize (IM3Module i_module, M3Memory * io_memory, m3word_t i_memorySize) +M3Result Module_EnsureMemorySize (IM3Module i_module, M3Memory * io_memory, size_t i_memorySize) { M3Result result = c_m3Err_none; @@ -119,11 +119,11 @@ M3Result Module_EnsureMemorySize (IM3Module i_module, M3Memory * io_memory, m3 size_t extra = c_m3MemPageSize * pages + 900000 * 4 + sizeof (M3MemoryHeader); - m3word_t alignedSize = i_memorySize + extra; + size_t alignedSize = i_memorySize + extra; if (c_m3AlignWasmMemoryToPages) { - m3word_t aligner = c_m3MemPageSize - 1; + size_t aligner = c_m3MemPageSize - 1; alignedSize += aligner; alignedSize &= ~aligner; } diff --git a/source/m3_module.h b/source/m3_module.h index 0e39682..980190d 100644 --- a/source/m3_module.h +++ b/source/m3_module.h @@ -11,7 +11,7 @@ #include "m3_env.h" -M3Result Module_EnsureMemorySize (IM3Module i_module, IM3Memory io_memory, m3word_t i_memorySize); +M3Result Module_EnsureMemorySize (IM3Module i_module, IM3Memory io_memory, size_t i_memorySize); i32 AllocateHeap (M3Memory * io_memory, i32 i_size);