diff --git a/Makefile b/Makefile index ea70ff3..5f9e692 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,11 @@ WASM3_C := $(wildcard wasm3/source/*.c) WASM3_O := $(WASM3_C:.c=.o) -uw8-wasm3: main.o $(WASM3_O) platform.o wasm-rt-impl.o +uw8-wasm3: main.o $(WASM3_O) platform.o loader.o wasm-rt-impl.o gcc -g -lm -lSDL2 -o uw8-wasm3 $^ run: uw8-wasm3 .PHONY - ./uw8-wasm3 never_sleeps.uw8 + ./uw8-wasm3 skipahead.uw8 run-ts: deno run --allow-read main.ts diff --git a/loader.c b/loader.c new file mode 100644 index 0000000..86cb58a --- /dev/null +++ b/loader.c @@ -0,0 +1,1080 @@ +/* Automatically generated by wasm2c */ +#include +#include +#include +#include +#if defined(_MSC_VER) +#include +#include +#define alloca _alloca +#else +#include +#endif + +#include "loader.h" + +#define TRAP(x) (wasm_rt_trap(WASM_RT_TRAP_##x), 0) + +#if WASM_RT_USE_STACK_DEPTH_COUNT +#define FUNC_PROLOGUE \ + if (++wasm_rt_call_stack_depth > WASM_RT_MAX_CALL_STACK_DEPTH) \ + TRAP(EXHAUSTION); + +#define FUNC_EPILOGUE --wasm_rt_call_stack_depth +#else +#define FUNC_PROLOGUE + +#define FUNC_EPILOGUE +#endif + +#define UNREACHABLE TRAP(UNREACHABLE) + +#define CALL_INDIRECT(table, t, ft, x, ...) \ + (LIKELY((x) < table.size && table.data[x].func && \ + table.data[x].func_type == func_types[ft]) || \ + TRAP(CALL_INDIRECT), \ + ((t)table.data[x].func)(__VA_ARGS__)) + +#define RANGE_CHECK(mem, offset, len) \ + if (UNLIKELY(offset + (uint64_t)len > mem->size)) \ + TRAP(OOB); + +#if WASM_RT_MEMCHECK_SIGNAL_HANDLER +#define MEMCHECK(mem, a, t) +#else +#define MEMCHECK(mem, a, t) RANGE_CHECK(mem, a, sizeof(t)) +#endif + +#ifdef __GNUC__ +#define wasm_asm __asm__ +#else +#define wasm_asm(X) +#endif + +#if WABT_BIG_ENDIAN +static inline void load_data(void* dest, const void* src, size_t n) { + size_t i = 0; + u8* dest_chars = dest; + memcpy(dest, src, n); + for (i = 0; i < (n >> 1); i++) { + u8 cursor = dest_chars[i]; + dest_chars[i] = dest_chars[n - i - 1]; + dest_chars[n - i - 1] = cursor; + } +} +#define LOAD_DATA(m, o, i, s) \ + do { \ + RANGE_CHECK((&m), m.size - o - s, s); \ + load_data(&(m.data[m.size - o - s]), i, s); \ + } while (0) +#define DEFINE_LOAD(name, t1, t2, t3) \ + static inline t3 name(wasm_rt_memory_t* mem, u64 addr) { \ + MEMCHECK(mem, addr, t1); \ + t1 result; \ + wasm_rt_memcpy(&result, &mem->data[mem->size - addr - sizeof(t1)], \ + sizeof(t1)); \ + wasm_asm("" ::"r"(result)); \ + return (t3)(t2)result; \ + } + +#define DEFINE_STORE(name, t1, t2) \ + static inline void name(wasm_rt_memory_t* mem, u64 addr, t2 value) { \ + MEMCHECK(mem, addr, t1); \ + t1 wrapped = (t1)value; \ + wasm_rt_memcpy(&mem->data[mem->size - addr - sizeof(t1)], &wrapped, \ + sizeof(t1)); \ + } +#else +static inline void load_data(void* dest, const void* src, size_t n) { + memcpy(dest, src, n); +} +#define LOAD_DATA(m, o, i, s) \ + do { \ + RANGE_CHECK((&m), o, s); \ + load_data(&(m.data[o]), i, s); \ + } while (0) +#define DEFINE_LOAD(name, t1, t2, t3) \ + static inline t3 name(wasm_rt_memory_t* mem, u64 addr) { \ + MEMCHECK(mem, addr, t1); \ + t1 result; \ + wasm_rt_memcpy(&result, &mem->data[addr], sizeof(t1)); \ + wasm_asm("" ::"r"(result)); \ + return (t3)(t2)result; \ + } + +#define DEFINE_STORE(name, t1, t2) \ + static inline void name(wasm_rt_memory_t* mem, u64 addr, t2 value) { \ + MEMCHECK(mem, addr, t1); \ + t1 wrapped = (t1)value; \ + wasm_rt_memcpy(&mem->data[addr], &wrapped, sizeof(t1)); \ + } +#endif + +DEFINE_LOAD(i32_load, u32, u32, u32) +DEFINE_LOAD(i64_load, u64, u64, u64) +DEFINE_LOAD(f32_load, f32, f32, f32) +DEFINE_LOAD(f64_load, f64, f64, f64) +DEFINE_LOAD(i32_load8_s, s8, s32, u32) +DEFINE_LOAD(i64_load8_s, s8, s64, u64) +DEFINE_LOAD(i32_load8_u, u8, u32, u32) +DEFINE_LOAD(i64_load8_u, u8, u64, u64) +DEFINE_LOAD(i32_load16_s, s16, s32, u32) +DEFINE_LOAD(i64_load16_s, s16, s64, u64) +DEFINE_LOAD(i32_load16_u, u16, u32, u32) +DEFINE_LOAD(i64_load16_u, u16, u64, u64) +DEFINE_LOAD(i64_load32_s, s32, s64, u64) +DEFINE_LOAD(i64_load32_u, u32, u64, u64) +DEFINE_STORE(i32_store, u32, u32) +DEFINE_STORE(i64_store, u64, u64) +DEFINE_STORE(f32_store, f32, f32) +DEFINE_STORE(f64_store, f64, f64) +DEFINE_STORE(i32_store8, u8, u32) +DEFINE_STORE(i32_store16, u16, u32) +DEFINE_STORE(i64_store8, u8, u64) +DEFINE_STORE(i64_store16, u16, u64) +DEFINE_STORE(i64_store32, u32, u64) + +#if defined(_MSC_VER) + +// Adapted from +// https://github.com/nemequ/portable-snippets/blob/master/builtin/builtin.h + +static inline int I64_CLZ(unsigned long long v) { + unsigned long r = 0; +#if defined(_M_AMD64) || defined(_M_ARM) + if (_BitScanReverse64(&r, v)) { + return 63 - r; + } +#else + if (_BitScanReverse(&r, (unsigned long)(v >> 32))) { + return 31 - r; + } else if (_BitScanReverse(&r, (unsigned long)v)) { + return 63 - r; + } +#endif + return 64; +} + +static inline int I32_CLZ(unsigned long v) { + unsigned long r = 0; + if (_BitScanReverse(&r, v)) { + return 31 - r; + } + return 32; +} + +static inline int I64_CTZ(unsigned long long v) { + if (!v) { + return 64; + } + unsigned long r = 0; +#if defined(_M_AMD64) || defined(_M_ARM) + _BitScanForward64(&r, v); + return (int)r; +#else + if (_BitScanForward(&r, (unsigned int)(v))) { + return (int)(r); + } + + _BitScanForward(&r, (unsigned int)(v >> 32)); + return (int)(r + 32); +#endif +} + +static inline int I32_CTZ(unsigned long v) { + if (!v) { + return 32; + } + unsigned long r = 0; + _BitScanForward(&r, v); + return (int)r; +} + +#define POPCOUNT_DEFINE_PORTABLE(f_n, T) \ + static inline u32 f_n(T x) { \ + x = x - ((x >> 1) & (T) ~(T)0 / 3); \ + x = (x & (T) ~(T)0 / 15 * 3) + ((x >> 2) & (T) ~(T)0 / 15 * 3); \ + x = (x + (x >> 4)) & (T) ~(T)0 / 255 * 15; \ + return (T)(x * ((T) ~(T)0 / 255)) >> (sizeof(T) - 1) * 8; \ + } + +POPCOUNT_DEFINE_PORTABLE(I32_POPCNT, u32) +POPCOUNT_DEFINE_PORTABLE(I64_POPCNT, u64) + +#undef POPCOUNT_DEFINE_PORTABLE + +#else + +#define I32_CLZ(x) ((x) ? __builtin_clz(x) : 32) +#define I64_CLZ(x) ((x) ? __builtin_clzll(x) : 64) +#define I32_CTZ(x) ((x) ? __builtin_ctz(x) : 32) +#define I64_CTZ(x) ((x) ? __builtin_ctzll(x) : 64) +#define I32_POPCNT(x) (__builtin_popcount(x)) +#define I64_POPCNT(x) (__builtin_popcountll(x)) + +#endif + +#define DIV_S(ut, min, x, y) \ + ((UNLIKELY((y) == 0)) \ + ? TRAP(DIV_BY_ZERO) \ + : (UNLIKELY((x) == min && (y) == -1)) ? TRAP(INT_OVERFLOW) \ + : (ut)((x) / (y))) + +#define REM_S(ut, min, x, y) \ + ((UNLIKELY((y) == 0)) \ + ? TRAP(DIV_BY_ZERO) \ + : (UNLIKELY((x) == min && (y) == -1)) ? 0 : (ut)((x) % (y))) + +#define I32_DIV_S(x, y) DIV_S(u32, INT32_MIN, (s32)x, (s32)y) +#define I64_DIV_S(x, y) DIV_S(u64, INT64_MIN, (s64)x, (s64)y) +#define I32_REM_S(x, y) REM_S(u32, INT32_MIN, (s32)x, (s32)y) +#define I64_REM_S(x, y) REM_S(u64, INT64_MIN, (s64)x, (s64)y) + +#define DIVREM_U(op, x, y) \ + ((UNLIKELY((y) == 0)) ? TRAP(DIV_BY_ZERO) : ((x)op(y))) + +#define DIV_U(x, y) DIVREM_U(/, x, y) +#define REM_U(x, y) DIVREM_U(%, x, y) + +#define ROTL(x, y, mask) \ + (((x) << ((y) & (mask))) | ((x) >> (((mask) - (y) + 1) & (mask)))) +#define ROTR(x, y, mask) \ + (((x) >> ((y) & (mask))) | ((x) << (((mask) - (y) + 1) & (mask)))) + +#define I32_ROTL(x, y) ROTL(x, y, 31) +#define I64_ROTL(x, y) ROTL(x, y, 63) +#define I32_ROTR(x, y) ROTR(x, y, 31) +#define I64_ROTR(x, y) ROTR(x, y, 63) + +#define FMIN(x, y) \ + ((UNLIKELY((x) != (x))) \ + ? NAN \ + : (UNLIKELY((y) != (y))) \ + ? NAN \ + : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? x : y) \ + : (x < y) ? x : y) + +#define FMAX(x, y) \ + ((UNLIKELY((x) != (x))) \ + ? NAN \ + : (UNLIKELY((y) != (y))) \ + ? NAN \ + : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? y : x) \ + : (x > y) ? x : y) + +#define TRUNC_S(ut, st, ft, min, minop, max, x) \ + ((UNLIKELY((x) != (x))) \ + ? TRAP(INVALID_CONVERSION) \ + : (UNLIKELY(!((x)minop(min) && (x) < (max)))) ? TRAP(INT_OVERFLOW) \ + : (ut)(st)(x)) + +#define I32_TRUNC_S_F32(x) \ + TRUNC_S(u32, s32, f32, (f32)INT32_MIN, >=, 2147483648.f, x) +#define I64_TRUNC_S_F32(x) \ + TRUNC_S(u64, s64, f32, (f32)INT64_MIN, >=, (f32)INT64_MAX, x) +#define I32_TRUNC_S_F64(x) \ + TRUNC_S(u32, s32, f64, -2147483649., >, 2147483648., x) +#define I64_TRUNC_S_F64(x) \ + TRUNC_S(u64, s64, f64, (f64)INT64_MIN, >=, (f64)INT64_MAX, x) + +#define TRUNC_U(ut, ft, max, x) \ + ((UNLIKELY((x) != (x))) \ + ? TRAP(INVALID_CONVERSION) \ + : (UNLIKELY(!((x) > (ft)-1 && (x) < (max)))) ? TRAP(INT_OVERFLOW) \ + : (ut)(x)) + +#define I32_TRUNC_U_F32(x) TRUNC_U(u32, f32, 4294967296.f, x) +#define I64_TRUNC_U_F32(x) TRUNC_U(u64, f32, (f32)UINT64_MAX, x) +#define I32_TRUNC_U_F64(x) TRUNC_U(u32, f64, 4294967296., x) +#define I64_TRUNC_U_F64(x) TRUNC_U(u64, f64, (f64)UINT64_MAX, x) + +#define TRUNC_SAT_S(ut, st, ft, min, smin, minop, max, smax, x) \ + ((UNLIKELY((x) != (x))) \ + ? 0 \ + : (UNLIKELY(!((x)minop(min)))) \ + ? smin \ + : (UNLIKELY(!((x) < (max)))) ? smax : (ut)(st)(x)) + +#define I32_TRUNC_SAT_S_F32(x) \ + TRUNC_SAT_S(u32, s32, f32, (f32)INT32_MIN, INT32_MIN, >=, 2147483648.f, \ + INT32_MAX, x) +#define I64_TRUNC_SAT_S_F32(x) \ + TRUNC_SAT_S(u64, s64, f32, (f32)INT64_MIN, INT64_MIN, >=, (f32)INT64_MAX, \ + INT64_MAX, x) +#define I32_TRUNC_SAT_S_F64(x) \ + TRUNC_SAT_S(u32, s32, f64, -2147483649., INT32_MIN, >, 2147483648., \ + INT32_MAX, x) +#define I64_TRUNC_SAT_S_F64(x) \ + TRUNC_SAT_S(u64, s64, f64, (f64)INT64_MIN, INT64_MIN, >=, (f64)INT64_MAX, \ + INT64_MAX, x) + +#define TRUNC_SAT_U(ut, ft, max, smax, x) \ + ((UNLIKELY((x) != (x))) ? 0 \ + : (UNLIKELY(!((x) > (ft)-1))) \ + ? 0 \ + : (UNLIKELY(!((x) < (max)))) ? smax : (ut)(x)) + +#define I32_TRUNC_SAT_U_F32(x) \ + TRUNC_SAT_U(u32, f32, 4294967296.f, UINT32_MAX, x) +#define I64_TRUNC_SAT_U_F32(x) \ + TRUNC_SAT_U(u64, f32, (f32)UINT64_MAX, UINT64_MAX, x) +#define I32_TRUNC_SAT_U_F64(x) TRUNC_SAT_U(u32, f64, 4294967296., UINT32_MAX, x) +#define I64_TRUNC_SAT_U_F64(x) \ + TRUNC_SAT_U(u64, f64, (f64)UINT64_MAX, UINT64_MAX, x) + +#define DEFINE_REINTERPRET(name, t1, t2) \ + static inline t2 name(t1 x) { \ + t2 result; \ + memcpy(&result, &x, sizeof(result)); \ + return result; \ + } + +DEFINE_REINTERPRET(f32_reinterpret_i32, u32, f32) +DEFINE_REINTERPRET(i32_reinterpret_f32, f32, u32) +DEFINE_REINTERPRET(f64_reinterpret_i64, u64, f64) +DEFINE_REINTERPRET(i64_reinterpret_f64, f64, u64) + +static float quiet_nanf(float x) { + uint32_t tmp; + memcpy(&tmp, &x, 4); + tmp |= 0x7fc00000lu; + memcpy(&x, &tmp, 4); + return x; +} + +static double quiet_nan(double x) { + uint64_t tmp; + memcpy(&tmp, &x, 8); + tmp |= 0x7ff8000000000000llu; + memcpy(&x, &tmp, 8); + return x; +} + +static double wasm_quiet(double x) { + if (UNLIKELY(isnan(x))) { + return quiet_nan(x); + } + return x; +} + +static float wasm_quietf(float x) { + if (UNLIKELY(isnan(x))) { + return quiet_nanf(x); + } + return x; +} + +static double wasm_floor(double x) { + if (UNLIKELY(isnan(x))) { + return quiet_nan(x); + } + return floor(x); +} + +static float wasm_floorf(float x) { + if (UNLIKELY(isnan(x))) { + return quiet_nanf(x); + } + return floorf(x); +} + +static double wasm_ceil(double x) { + if (UNLIKELY(isnan(x))) { + return quiet_nan(x); + } + return ceil(x); +} + +static float wasm_ceilf(float x) { + if (UNLIKELY(isnan(x))) { + return quiet_nanf(x); + } + return ceilf(x); +} + +static double wasm_trunc(double x) { + if (UNLIKELY(isnan(x))) { + return quiet_nan(x); + } + return trunc(x); +} + +static float wasm_truncf(float x) { + if (UNLIKELY(isnan(x))) { + return quiet_nanf(x); + } + return truncf(x); +} + +static float wasm_nearbyintf(float x) { + if (UNLIKELY(isnan(x))) { + return quiet_nanf(x); + } + return nearbyintf(x); +} + +static double wasm_nearbyint(double x) { + if (UNLIKELY(isnan(x))) { + return quiet_nan(x); + } + return nearbyint(x); +} + +static float wasm_fabsf(float x) { + if (UNLIKELY(isnan(x))) { + uint32_t tmp; + memcpy(&tmp, &x, 4); + tmp = tmp & ~(1 << 31); + memcpy(&x, &tmp, 4); + return x; + } + return fabsf(x); +} + +static double wasm_fabs(double x) { + if (UNLIKELY(isnan(x))) { + uint64_t tmp; + memcpy(&tmp, &x, 8); + tmp = tmp & ~(1ll << 63); + memcpy(&x, &tmp, 8); + return x; + } + return fabs(x); +} + +static double wasm_sqrt(double x) { + if (UNLIKELY(isnan(x))) { + return quiet_nan(x); + } + return sqrt(x); +} + +static float wasm_sqrtf(float x) { + if (UNLIKELY(isnan(x))) { + return quiet_nanf(x); + } + return sqrtf(x); +} + +static inline void memory_fill(wasm_rt_memory_t* mem, u32 d, u32 val, u32 n) { + RANGE_CHECK(mem, d, n); + memset(mem->data + d, val, n); +} + +static inline void memory_copy(wasm_rt_memory_t* dest, + const wasm_rt_memory_t* src, + u32 dest_addr, + u32 src_addr, + u32 n) { + RANGE_CHECK(dest, dest_addr, n); + RANGE_CHECK(src, src_addr, n); + memmove(dest->data + dest_addr, src->data + src_addr, n); +} + +static inline void memory_init(wasm_rt_memory_t* dest, + const u8* src, + u32 src_size, + u32 dest_addr, + u32 src_addr, + u32 n) { + if (UNLIKELY(src_addr + (uint64_t)n > src_size)) + TRAP(OOB); + LOAD_DATA((*dest), dest_addr, src + src_addr, n); +} + +typedef struct { + uint32_t func_type_index; + wasm_rt_function_ptr_t func; + size_t module_offset; +} wasm_elem_segment_expr_t; + +static inline void funcref_table_init(wasm_rt_funcref_table_t* dest, + const wasm_elem_segment_expr_t* src, + u32 src_size, + u32 dest_addr, + u32 src_addr, + u32 n, + void* module_instance, + const u32* func_types) { + if (UNLIKELY(src_addr + (uint64_t)n > src_size)) + TRAP(OOB); + if (UNLIKELY(dest_addr + (uint64_t)n > dest->size)) + TRAP(OOB); + for (u32 i = 0; i < n; i++) { + const wasm_elem_segment_expr_t* src_expr = &src[src_addr + i]; + dest->data[dest_addr + i] = (wasm_rt_funcref_t){ + func_types[src_expr->func_type_index], src_expr->func, + (char*)module_instance + src_expr->module_offset}; + } +} + +// Currently we only support initializing externref tables with ref.null. +static inline void externref_table_init(wasm_rt_externref_table_t* dest, + const wasm_rt_externref_t* src, + u32 src_size, + u32 dest_addr, + u32 src_addr, + u32 n) { + if (UNLIKELY(src_addr + (uint64_t)n > src_size)) + TRAP(OOB); + if (UNLIKELY(dest_addr + (uint64_t)n > dest->size)) + TRAP(OOB); + for (u32 i = 0; i < n; i++) { + const wasm_rt_externref_t* src_expr = &src[src_addr + i]; + dest->data[dest_addr + i] = *src_expr; + } +} + +#define DEFINE_TABLE_COPY(type) \ + static inline void type##_table_copy(wasm_rt_##type##_table_t* dest, \ + const wasm_rt_##type##_table_t* src, \ + u32 dest_addr, u32 src_addr, u32 n) { \ + if (UNLIKELY(dest_addr + (uint64_t)n > dest->size)) \ + TRAP(OOB); \ + if (UNLIKELY(src_addr + (uint64_t)n > src->size)) \ + TRAP(OOB); \ + \ + memmove(dest->data + dest_addr, src->data + src_addr, \ + n * sizeof(wasm_rt_##type##_t)); \ + } + +DEFINE_TABLE_COPY(funcref) +DEFINE_TABLE_COPY(externref) + +#define DEFINE_TABLE_GET(type) \ + static inline wasm_rt_##type##_t type##_table_get( \ + const wasm_rt_##type##_table_t* table, u32 i) { \ + if (UNLIKELY(i >= table->size)) \ + TRAP(OOB); \ + return table->data[i]; \ + } + +DEFINE_TABLE_GET(funcref) +DEFINE_TABLE_GET(externref) + +#define DEFINE_TABLE_SET(type) \ + static inline void type##_table_set(const wasm_rt_##type##_table_t* table, \ + u32 i, const wasm_rt_##type##_t val) { \ + if (UNLIKELY(i >= table->size)) \ + TRAP(OOB); \ + table->data[i] = val; \ + } + +DEFINE_TABLE_SET(funcref) +DEFINE_TABLE_SET(externref) + +#define DEFINE_TABLE_FILL(type) \ + static inline void type##_table_fill(const wasm_rt_##type##_table_t* table, \ + u32 d, const wasm_rt_##type##_t val, \ + u32 n) { \ + if (UNLIKELY((uint64_t)d + n > table->size)) \ + TRAP(OOB); \ + for (uint32_t i = d; i < d + n; i++) { \ + table->data[i] = val; \ + } \ + } + +DEFINE_TABLE_FILL(funcref) +DEFINE_TABLE_FILL(externref) + +static bool s_module_initialized = false; + +static u32 func_types[4]; + +static void init_func_types(void) { + func_types[0] = wasm_rt_register_func_type(1, 1, WASM_RT_I32, WASM_RT_I32); + func_types[1] = wasm_rt_register_func_type(2, 1, WASM_RT_I32, WASM_RT_I32, WASM_RT_I32); + func_types[2] = wasm_rt_register_func_type(3, 0, WASM_RT_I32, WASM_RT_I32, WASM_RT_I32); + func_types[3] = wasm_rt_register_func_type(0, 0); +} + +static u32 w2c_load_uw8(Z_loader_instance_t*, u32); +static u32 w2c_f1(Z_loader_instance_t*, u32); +static u32 w2c_f2(Z_loader_instance_t*, u32, u32); +static void w2c_f3(Z_loader_instance_t*, u32, u32, u32); +static u32 w2c_uncompress(Z_loader_instance_t*, u32, u32); +static u32 w2c_f5(Z_loader_instance_t*, u32); +static u32 w2c_f6(Z_loader_instance_t*, u32); +static void w2c_f7(Z_loader_instance_t*); + +static void init_globals(Z_loader_instance_t* instance) { + instance->w2c_g0 = 0u; + instance->w2c_g1 = 0u; + instance->w2c_g2 = 0u; +} + + +static const u8 data_segment_data_w2c_d0[] = { + 0xff, 0x9f, 0x97, 0xbc, 0x33, 0x66, 0xca, 0x12, 0x2c, 0xaa, 0xa6, 0xb1, + 0xe8, 0x64, 0x7c, 0x9f, 0xa8, 0x5e, 0x2c, 0xd7, 0x27, 0xc9, 0x2b, 0x8c, + 0x1a, 0xfa, 0x33, 0x1e, 0x5d, 0x5a, 0x44, 0x7d, 0xe9, 0xa6, 0x67, 0x43, + 0x25, 0x96, 0x19, 0xa5, 0xf3, 0x41, 0xd5, 0x99, 0x7e, 0x97, 0x1d, 0xf5, + 0x71, 0x69, 0x74, 0xdf, 0x5b, 0xac, 0x4f, 0x19, 0x05, 0x0a, 0x34, 0x9f, + 0x75, 0xab, 0xfe, 0xcf, 0xb0, 0x71, 0xd1, 0x54, 0x5b, 0xad, 0x3b, 0xf4, + 0x31, 0x1c, 0x0f, 0x08, 0xd8, 0x9a, 0xbc, 0x60, 0xe6, 0x9b, 0x45, 0xe2, + 0x8e, 0xc8, 0x94, 0x89, 0xb0, 0xc4, 0x48, 0x49, 0x49, 0x6a, 0xc1, 0x6e, + 0xd2, 0x5e, 0xa6, 0x36, 0x9e, 0x0b, 0x83, 0xe8, 0x8d, 0x5e, 0xc9, 0x82, + 0x1a, 0xf5, 0x54, 0xfc, 0x03, 0xb1, 0xcf, 0x37, 0x46, 0xcb, 0xae, 0x0b, + 0x22, 0xf4, 0xdf, 0xbf, 0x75, 0xbb, 0x09, 0x70, 0x8b, 0x44, 0xfc, 0x98, + 0x94, 0x7e, 0xb2, 0x51, 0xfc, 0xd9, 0xc9, 0x0a, 0x94, 0x2e, 0x53, 0x2b, + 0xac, 0x27, 0x3f, 0x2a, 0x01, 0xf7, 0x4f, 0x24, 0x49, 0xe2, 0xd8, 0xf8, + 0x48, 0x6e, 0x9c, 0x38, 0x6d, 0x58, 0x70, 0x3b, 0x11, 0x6c, 0x30, 0x75, + 0x0b, 0xe2, 0x81, 0x83, 0x21, 0x4d, 0x0e, 0x82, 0xca, 0x2f, 0x97, 0x7a, + 0x59, 0x12, 0xab, 0xa0, 0xa7, 0x09, 0x09, 0xd9, 0xac, 0x0a, 0x97, 0xae, + 0xf1, 0x2c, 0x49, 0xee, 0xfe, 0x60, 0xdb, 0x77, 0x1e, 0x0d, 0x0c, 0x17, + 0x27, 0x17, 0x79, 0xd0, 0xc8, 0xa0, 0x50, 0x0d, 0x04, 0x3c, 0x9f, 0x3d, + 0xac, 0xa7, 0xd1, 0x5a, 0x72, 0x24, 0x1f, 0xe2, 0x81, 0xc2, 0xd2, 0x51, + 0x59, 0xe0, 0x0a, 0x56, 0x21, 0xea, 0xa8, 0x9b, 0x8f, 0xfa, 0x8c, 0x50, + 0x9a, 0x77, 0x50, 0xca, 0x28, 0x3c, 0xaf, 0x46, 0x1e, 0x07, 0x81, 0x9f, + 0x92, 0xf8, 0x10, 0xc0, 0xca, 0xb6, 0x8d, 0xbd, 0x9c, 0xa3, 0x33, 0xb8, + 0x6b, 0xa5, 0xfd, 0x4f, 0xdc, 0xb2, 0xb4, 0xe9, 0x1a, 0xf3, 0x2c, 0x36, + 0x1b, 0xcf, 0xf6, 0x88, 0x91, 0x6c, 0x62, 0xd3, 0xfa, 0xd2, 0x8d, 0xf5, + 0x8d, 0x3c, 0x93, 0x7b, 0xa6, 0x02, 0xe2, 0xa0, 0x66, 0xe3, 0xde, 0x8e, + 0x14, 0x9a, 0x46, 0xee, 0xfd, 0x89, 0xba, 0xd3, 0x62, 0xc1, 0x66, 0xa7, + 0xe8, 0xcc, 0xfa, 0xba, 0xf8, 0xaa, 0xc4, 0xed, 0x6e, 0x5d, 0xef, 0x8d, + 0xad, 0x03, 0xb7, 0xb5, 0x49, 0x45, 0x01, 0xaa, 0xc6, 0xfe, 0xd0, 0x25, + 0x48, 0x58, 0x91, 0xdb, 0x6c, 0x09, 0x3a, 0x80, 0xcd, 0xe7, 0xe5, 0x6d, + 0x07, 0x9d, 0x67, 0xf2, 0xe4, 0xf0, 0x6a, 0x81, 0x1a, 0x6c, 0x7f, 0xd7, + 0x11, 0xfb, 0xfa, 0x6b, 0x6b, 0x60, 0xb8, 0xbc, 0x57, 0xf7, 0x29, 0x59, + 0xdf, 0x1a, 0xf3, 0xdd, 0x0d, 0x4c, 0x05, 0xc9, 0xf3, 0x42, 0x8f, 0x58, + 0x97, 0x22, 0xb9, 0x11, 0x29, 0x96, 0x64, 0xc5, 0xb4, 0x66, 0xba, 0xe2, + 0x0a, 0x82, 0x0d, 0x1c, 0x30, 0x6c, 0xec, 0xec, 0x7f, 0xc7, 0x26, 0x7f, + 0xa9, 0x17, 0x8d, 0xc0, 0x72, 0x22, 0xd4, 0x7d, 0xab, 0x4e, 0x7b, 0x65, + 0xa5, 0xe2, 0xd0, 0x5b, 0x85, 0x5e, 0x6d, 0xf9, 0x9a, 0x88, 0x64, 0x7a, + 0x8b, 0xc5, 0x61, 0xe4, 0x97, 0x2b, 0xf7, 0x15, 0xc3, 0x33, 0xa6, 0xc6, + 0x54, 0x72, 0x32, 0x25, 0x13, 0x9e, 0xc6, 0x90, 0xdb, 0xdf, 0x72, 0xb3, + 0xec, 0xf8, 0x44, 0x79, 0xa7, 0x1c, 0xf2, 0xac, 0x3e, 0x15, 0x93, 0xe3, + 0x68, 0xff, 0x9f, 0x5b, 0xa2, 0x70, 0x39, 0xb9, 0x81, 0x5c, 0x70, 0xe1, + 0xb4, 0x8f, 0x39, 0x16, 0x4a, 0xf4, 0xdc, 0xef, 0x93, 0xdc, 0xb7, 0xfa, + +}; + +static void init_memories(Z_loader_instance_t* instance) { + LOAD_DATA((*instance->Z_envZ_memory), 0u, data_segment_data_w2c_d0, 492); +} + +static void init_data_instances(Z_loader_instance_t *instance) { +} + +static u32 w2c_load_uw8(Z_loader_instance_t* instance, u32 w2c_p0) { + u32 w2c_l1 = 0, w2c_l2 = 0, w2c_l3 = 0, w2c_l4 = 0, w2c_l5 = 0, w2c_l6 = 0, w2c_l7 = 0; + FUNC_PROLOGUE; + u32 w2c_i0, w2c_i1, w2c_i2, w2c_i3; + w2c_i0 = 0u; + w2c_i0 = i32_load8_u(instance->Z_envZ_memory, (u64)(w2c_i0)); + w2c_i1 = 1u; + w2c_i0 -= w2c_i1; + w2c_l1 = w2c_i0; + w2c_i1 = 0u; + w2c_i0 = (u32)((s32)w2c_i0 < (s32)w2c_i1); + if (w2c_i0) { + w2c_i0 = w2c_p0; + goto w2c_Bfunc; + } + w2c_i0 = 122880u; + w2c_i1 = w2c_p0; + w2c_i0 += w2c_i1; + w2c_l2 = w2c_i0; + w2c_i0 = w2c_l1; + w2c_i1 = 1u; + w2c_i0 &= w2c_i1; + if (w2c_i0) { + w2c_i0 = 1u; + w2c_i1 = 122881u; + w2c_i0 = w2c_uncompress(instance, w2c_i0, w2c_i1); + w2c_l2 = w2c_i0; + } else { + w2c_i0 = 122880u; + w2c_i1 = 0u; + w2c_i2 = w2c_p0; + w2c_f3(instance, w2c_i0, w2c_i1, w2c_i2); + } + w2c_i0 = 0u; + w2c_i1 = 246272u; + w2c_i2 = 8u; + w2c_f3(instance, w2c_i0, w2c_i1, w2c_i2); + w2c_i0 = 246280u; + w2c_l3 = w2c_i0; + w2c_i0 = 8u; + w2c_l4 = w2c_i0; + w2c_i0 = 122881u; + w2c_l5 = w2c_i0; + w2c_L2: + w2c_i0 = w2c_l5; + w2c_i1 = w2c_l2; + w2c_i0 = (u32)((s32)w2c_i0 < (s32)w2c_i1); + w2c_i1 = w2c_l3; + w2c_i2 = instance->w2c_g0; + w2c_i1 = (u32)((s32)w2c_i1 >= (s32)w2c_i2); + w2c_i2 = w2c_l5; + w2c_i2 = i32_load8_u(instance->Z_envZ_memory, (u64)(w2c_i2)); + w2c_i3 = w2c_l3; + w2c_i3 = i32_load8_u(instance->Z_envZ_memory, (u64)(w2c_i3)); + w2c_i2 = (u32)((s32)w2c_i2 <= (s32)w2c_i3); + w2c_i1 |= w2c_i2; + w2c_i0 &= w2c_i1; + if (w2c_i0) { + w2c_i0 = w2c_l4; + w2c_i1 = w2c_l4; + w2c_i2 = w2c_l5; + w2c_i1 = w2c_f2(instance, w2c_i1, w2c_i2); + w2c_l6 = w2c_i1; + w2c_i0 += w2c_i1; + w2c_l4 = w2c_i0; + w2c_i0 = w2c_l3; + w2c_i1 = instance->w2c_g0; + w2c_i0 = (u32)((s32)w2c_i0 < (s32)w2c_i1); + w2c_i1 = w2c_l5; + w2c_i1 = i32_load8_u(instance->Z_envZ_memory, (u64)(w2c_i1)); + w2c_i2 = w2c_l3; + w2c_i2 = i32_load8_u(instance->Z_envZ_memory, (u64)(w2c_i2)); + w2c_i1 = w2c_i1 == w2c_i2; + w2c_i0 &= w2c_i1; + if (w2c_i0) { + w2c_i0 = w2c_l3; + w2c_i1 = w2c_l3; + w2c_i1 = w2c_f1(instance, w2c_i1); + w2c_i0 += w2c_i1; + w2c_l3 = w2c_i0; + } + w2c_i0 = w2c_l5; + w2c_i1 = w2c_l6; + w2c_i0 += w2c_i1; + w2c_l5 = w2c_i0; + goto w2c_L2; + } + w2c_i0 = w2c_l3; + w2c_i1 = instance->w2c_g0; + w2c_i0 = (u32)((s32)w2c_i0 < (s32)w2c_i1); + if (w2c_i0) { + w2c_i0 = w2c_l4; + w2c_i1 = w2c_l4; + w2c_i2 = w2c_l3; + w2c_i1 = w2c_f2(instance, w2c_i1, w2c_i2); + w2c_l7 = w2c_i1; + w2c_i0 += w2c_i1; + w2c_l4 = w2c_i0; + w2c_i0 = w2c_l3; + w2c_i1 = w2c_l7; + w2c_i0 += w2c_i1; + w2c_l3 = w2c_i0; + goto w2c_L2; + } + w2c_i0 = w2c_l4; + w2c_Bfunc:; + FUNC_EPILOGUE; + return w2c_i0; +} + +static u32 w2c_f1(Z_loader_instance_t* instance, u32 w2c_p0) { + u32 w2c_l1 = 0, w2c_l2 = 0, w2c_l3 = 0, w2c_l4 = 0; + FUNC_PROLOGUE; + u32 w2c_i0, w2c_i1, w2c_i2; + w2c_i0 = w2c_p0; + w2c_l1 = w2c_i0; + w2c_L0: + w2c_i0 = w2c_l2; + w2c_i1 = w2c_l1; + w2c_i2 = 1u; + w2c_i1 += w2c_i2; + w2c_l1 = w2c_i1; + w2c_i1 = i32_load8_u(instance->Z_envZ_memory, (u64)(w2c_i1)); + w2c_l4 = w2c_i1; + w2c_i2 = 127u; + w2c_i1 &= w2c_i2; + w2c_i2 = w2c_l3; + w2c_i1 <<= (w2c_i2 & 31); + w2c_i0 |= w2c_i1; + w2c_l2 = w2c_i0; + w2c_i0 = w2c_l3; + w2c_i1 = 7u; + w2c_i0 += w2c_i1; + w2c_l3 = w2c_i0; + w2c_i0 = w2c_l4; + w2c_i1 = 7u; + w2c_i0 = (u32)((s32)w2c_i0 >> (w2c_i1 & 31)); + if (w2c_i0) {goto w2c_L0;} + w2c_i0 = w2c_l1; + w2c_i1 = 1u; + w2c_i0 += w2c_i1; + w2c_i1 = w2c_p0; + w2c_i0 -= w2c_i1; + w2c_i1 = w2c_l2; + w2c_i0 += w2c_i1; + FUNC_EPILOGUE; + return w2c_i0; +} + +static u32 w2c_f2(Z_loader_instance_t* instance, u32 w2c_p0, u32 w2c_p1) { + u32 w2c_l2 = 0; + FUNC_PROLOGUE; + u32 w2c_i0, w2c_i1, w2c_i2; + w2c_i0 = w2c_p0; + w2c_i1 = w2c_p1; + w2c_i2 = w2c_p1; + w2c_i2 = w2c_f1(instance, w2c_i2); + w2c_l2 = w2c_i2; + w2c_f3(instance, w2c_i0, w2c_i1, w2c_i2); + w2c_i0 = w2c_l2; + FUNC_EPILOGUE; + return w2c_i0; +} + +static void w2c_f3(Z_loader_instance_t* instance, u32 w2c_p0, u32 w2c_p1, u32 w2c_p2) { + FUNC_PROLOGUE; + u32 w2c_i0, w2c_i1, w2c_i2; + w2c_L0: + w2c_i0 = w2c_p2; + w2c_i1 = 0u; + w2c_i0 = (u32)((s32)w2c_i0 > (s32)w2c_i1); + if (w2c_i0) { + w2c_i0 = w2c_p0; + w2c_i1 = w2c_p2; + w2c_i2 = 1u; + w2c_i1 -= w2c_i2; + w2c_p2 = w2c_i1; + w2c_i0 += w2c_i1; + w2c_i1 = w2c_p1; + w2c_i2 = w2c_p2; + w2c_i1 += w2c_i2; + w2c_i1 = i32_load8_u(instance->Z_envZ_memory, (u64)(w2c_i1)); + i32_store8(instance->Z_envZ_memory, (u64)(w2c_i0), w2c_i1); + goto w2c_L0; + } + FUNC_EPILOGUE; +} + +static u32 w2c_uncompress(Z_loader_instance_t* instance, u32 w2c_p0, u32 w2c_p1) { + u32 w2c_l2 = 0, w2c_l3 = 0, w2c_l4 = 0, w2c_l5 = 0, w2c_l6 = 0, w2c_l7 = 0; + FUNC_PROLOGUE; + u32 w2c_i0, w2c_i1, w2c_i2; + w2c_i0 = w2c_p0; + instance->w2c_g1 = w2c_i0; + w2c_i0 = 0u; + instance->w2c_g2 = w2c_i0; + w2c_L0: + w2c_i0 = w2c_l3; + w2c_i1 = 128u; + i32_store8(instance->Z_envZ_memory, (u64)(w2c_i0) + 245760, w2c_i1); + w2c_i0 = w2c_l3; + w2c_i1 = 1u; + w2c_i0 += w2c_i1; + w2c_l3 = w2c_i0; + w2c_i1 = 385u; + w2c_i0 = (u32)((s32)w2c_i0 < (s32)w2c_i1); + if (w2c_i0) {goto w2c_L0;} + w2c_L2: + w2c_i0 = 0u; + w2c_i0 = w2c_f6(instance, w2c_i0); + w2c_l5 = w2c_i0; + if (w2c_i0) { + w2c_i0 = w2c_l4; + if (w2c_i0) { + w2c_i0 = 1u; + } else { + w2c_i0 = 256u; + w2c_i0 = w2c_f6(instance, w2c_i0); + } + if (w2c_i0) { + w2c_i0 = 257u; + w2c_i0 = w2c_f5(instance, w2c_i0); + w2c_i1 = 1u; + w2c_i0 -= w2c_i1; + w2c_l2 = w2c_i0; + w2c_i0 = !(w2c_i0); + if (w2c_i0) {goto w2c_B1;} + } + w2c_i0 = 321u; + w2c_i0 = w2c_f5(instance, w2c_i0); + w2c_l6 = w2c_i0; + w2c_L6: + w2c_i0 = w2c_p1; + w2c_i1 = w2c_p1; + w2c_i2 = w2c_l2; + w2c_i1 -= w2c_i2; + w2c_i1 = i32_load8_u(instance->Z_envZ_memory, (u64)(w2c_i1)); + i32_store8(instance->Z_envZ_memory, (u64)(w2c_i0), w2c_i1); + w2c_i0 = w2c_p1; + w2c_i1 = 1u; + w2c_i0 += w2c_i1; + w2c_p1 = w2c_i0; + w2c_i0 = w2c_l6; + w2c_i1 = 1u; + w2c_i0 -= w2c_i1; + w2c_l6 = w2c_i0; + if (w2c_i0) {goto w2c_L6;} + } else { + w2c_i0 = 1u; + w2c_l7 = w2c_i0; + w2c_L7: + w2c_i0 = w2c_l7; + w2c_i1 = 1u; + w2c_i0 <<= (w2c_i1 & 31); + w2c_i1 = w2c_l7; + w2c_i1 = w2c_f6(instance, w2c_i1); + w2c_i0 |= w2c_i1; + w2c_l7 = w2c_i0; + w2c_i1 = 256u; + w2c_i0 = (u32)((s32)w2c_i0 < (s32)w2c_i1); + if (w2c_i0) {goto w2c_L7;} + w2c_i0 = w2c_p1; + w2c_i1 = w2c_l7; + i32_store8(instance->Z_envZ_memory, (u64)(w2c_i0), w2c_i1); + w2c_i0 = w2c_p1; + w2c_i1 = 1u; + w2c_i0 += w2c_i1; + w2c_p1 = w2c_i0; + } + w2c_i0 = w2c_l5; + w2c_l4 = w2c_i0; + goto w2c_L2; + w2c_B1:; + w2c_i0 = w2c_p1; + FUNC_EPILOGUE; + return w2c_i0; +} + +static u32 w2c_f5(Z_loader_instance_t* instance, u32 w2c_p0) { + u32 w2c_l1 = 0, w2c_l2 = 0; + FUNC_PROLOGUE; + u32 w2c_i0, w2c_i1, w2c_i2; + w2c_L0: + w2c_i0 = w2c_p0; + w2c_i1 = w2c_l2; + w2c_i0 += w2c_i1; + w2c_i0 = w2c_f6(instance, w2c_i0); + if (w2c_i0) { + w2c_i0 = w2c_l1; + w2c_i1 = w2c_p0; + w2c_i2 = w2c_l2; + w2c_i1 += w2c_i2; + w2c_i2 = 32u; + w2c_i1 += w2c_i2; + w2c_i1 = w2c_f6(instance, w2c_i1); + w2c_i2 = w2c_l2; + w2c_i1 <<= (w2c_i2 & 31); + w2c_i0 |= w2c_i1; + w2c_l1 = w2c_i0; + w2c_i0 = w2c_l2; + w2c_i1 = 1u; + w2c_i0 += w2c_i1; + w2c_l2 = w2c_i0; + goto w2c_L0; + } + w2c_i0 = w2c_l1; + w2c_i1 = 1u; + w2c_i2 = w2c_l2; + w2c_i1 <<= (w2c_i2 & 31); + w2c_i0 |= w2c_i1; + FUNC_EPILOGUE; + return w2c_i0; +} + +static u32 w2c_f6(Z_loader_instance_t* instance, u32 w2c_p0) { + u32 w2c_l1 = 0, w2c_l2 = 0, w2c_l3 = 0, w2c_l4 = 0; + FUNC_PROLOGUE; + u32 w2c_i0, w2c_i1, w2c_i2, w2c_i3, w2c_i4; + w2c_L0: + w2c_i0 = instance->w2c_g2; + w2c_i1 = 4096u; + w2c_i0 = (u32)((s32)w2c_i0 < (s32)w2c_i1); + if (w2c_i0) { + w2c_i0 = instance->w2c_g2; + w2c_i1 = 8u; + w2c_i0 <<= (w2c_i1 & 31); + w2c_i1 = instance->w2c_g1; + w2c_i1 = i32_load8_u(instance->Z_envZ_memory, (u64)(w2c_i1)); + w2c_i0 |= w2c_i1; + instance->w2c_g2 = w2c_i0; + w2c_i0 = instance->w2c_g1; + w2c_i1 = 1u; + w2c_i0 += w2c_i1; + instance->w2c_g1 = w2c_i0; + goto w2c_L0; + } + w2c_i0 = instance->w2c_g2; + w2c_i1 = 255u; + w2c_i0 &= w2c_i1; + w2c_l2 = w2c_i0; + w2c_i1 = w2c_p0; + w2c_i1 = i32_load8_u(instance->Z_envZ_memory, (u64)(w2c_i1) + 245760u); + w2c_l1 = w2c_i1; + w2c_i2 = instance->w2c_g2; + w2c_i3 = 8u; + w2c_i2 = (u32)((s32)w2c_i2 >> (w2c_i3 & 31)); + w2c_l3 = w2c_i2; + w2c_i1 *= w2c_i2; + w2c_i2 = 256u; + w2c_i3 = w2c_l1; + w2c_i2 -= w2c_i3; + w2c_i3 = w2c_l3; + w2c_i2 *= w2c_i3; + w2c_i3 = w2c_l1; + w2c_i2 -= w2c_i3; + w2c_i3 = w2c_l2; + w2c_i4 = w2c_l1; + w2c_i3 = (u32)((s32)w2c_i3 < (s32)w2c_i4); + w2c_l4 = w2c_i3; + w2c_i1 = w2c_i3 ? w2c_i1 : w2c_i2; + w2c_i0 += w2c_i1; + instance->w2c_g2 = w2c_i0; + w2c_i0 = w2c_p0; + w2c_i1 = w2c_l1; + w2c_i2 = 7u; + w2c_i3 = w2c_l4; + w2c_i4 = 257u; + w2c_i3 *= w2c_i4; + w2c_i2 += w2c_i3; + w2c_i3 = w2c_l1; + w2c_i2 -= w2c_i3; + w2c_i3 = 4u; + w2c_i2 = (u32)((s32)w2c_i2 >> (w2c_i3 & 31)); + w2c_i1 += w2c_i2; + i32_store8(instance->Z_envZ_memory, (u64)(w2c_i0) + 245760, w2c_i1); + w2c_i0 = w2c_l4; + FUNC_EPILOGUE; + return w2c_i0; +} + +static void w2c_f7(Z_loader_instance_t* instance) { + FUNC_PROLOGUE; + u32 w2c_i0, w2c_i1; + w2c_i0 = 0u; + w2c_i1 = 246272u; + w2c_i0 = w2c_uncompress(instance, w2c_i0, w2c_i1); + instance->w2c_g0 = w2c_i0; + FUNC_EPILOGUE; +} + +/* export: 'load_uw8' */ +u32 Z_loaderZ_load_uw8(Z_loader_instance_t* instance, u32 w2c_p0) { + return w2c_load_uw8(instance, w2c_p0); +} + +/* export: 'uncompress' */ +u32 Z_loaderZ_uncompress(Z_loader_instance_t* instance, u32 w2c_p0, u32 w2c_p1) { + return w2c_uncompress(instance, w2c_p0, w2c_p1); +} + +static void init_instance_import(Z_loader_instance_t* instance, struct Z_env_instance_t* Z_env_instance){ + instance->Z_envZ_memory = Z_envZ_memory(Z_env_instance); +} + +void Z_loader_init_module(void) { + assert(wasm_rt_is_initialized()); + s_module_initialized = true; + init_func_types(); +} + +void Z_loader_instantiate(Z_loader_instance_t* instance, struct Z_env_instance_t* Z_env_instance) { + assert(wasm_rt_is_initialized()); + assert(s_module_initialized); + init_instance_import(instance, Z_env_instance); + init_globals(instance); + init_memories(instance); + init_data_instances(instance); + w2c_f7(instance); +} + +void Z_loader_free(Z_loader_instance_t* instance) { +} diff --git a/loader.h b/loader.h new file mode 100644 index 0000000..5a57959 --- /dev/null +++ b/loader.h @@ -0,0 +1,54 @@ +/* Automatically generated by wasm2c */ +#ifndef LOADER_H_GENERATED_ +#define LOADER_H_GENERATED_ + +#include + +#include "wasm-rt.h" + +/* TODO(binji): only use stdint.h types in header */ +#ifndef WASM_RT_CORE_TYPES_DEFINED +#define WASM_RT_CORE_TYPES_DEFINED +typedef uint8_t u8; +typedef int8_t s8; +typedef uint16_t u16; +typedef int16_t s16; +typedef uint32_t u32; +typedef int32_t s32; +typedef uint64_t u64; +typedef int64_t s64; +typedef float f32; +typedef double f64; +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +struct Z_env_instance_t; +extern wasm_rt_memory_t* Z_envZ_memory(struct Z_env_instance_t*); + +typedef struct Z_loader_instance_t { + /* import: 'env' 'memory' */ + wasm_rt_memory_t *Z_envZ_memory; + u32 w2c_g0; + u32 w2c_g1; + u32 w2c_g2; +} Z_loader_instance_t; + +void Z_loader_init_module(void); +void Z_loader_instantiate(Z_loader_instance_t*, struct Z_env_instance_t*); +void Z_loader_free(Z_loader_instance_t*); + + +/* export: 'load_uw8' */ +u32 Z_loaderZ_load_uw8(Z_loader_instance_t*, u32); + +/* export: 'uncompress' */ +u32 Z_loaderZ_uncompress(Z_loader_instance_t*, u32, u32); + +#ifdef __cplusplus +} +#endif + +#endif /* LOADER_H_GENERATED_ */ diff --git a/loader.wasm b/loader.wasm deleted file mode 100644 index ac3ef2a..0000000 Binary files a/loader.wasm and /dev/null differ diff --git a/main.c b/main.c index 2cb0b6d..dd4f243 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,6 @@ #include "wasm3/source/wasm3.h" #include "wasm3/source/m3_env.h" +#include "loader.h" #include "platform.h" #include "SDL2/SDL.h" #include "SDL2/SDL_video.h" @@ -261,15 +262,21 @@ void linkPlatformFunctions(IM3Runtime runtime, IM3Module cartMod, Z_platform_ins } } -void* loadUw8(uint32_t* sizeOut, IM3Runtime runtime, IM3Function loadFunc, const char* filename) { +void* loadUw8(uint32_t* sizeOut, IM3Runtime runtime, const char* filename) { size_t uw8Size; void* uw8 = loadFile(&uw8Size, filename); - uint8_t* memory = m3_GetMemory(runtime, NULL, 0); - memcpy(memory, uw8, uw8Size); - verifyM3(runtime, m3_CallV(loadFunc, (uint32_t)uw8Size)); - verifyM3(runtime, m3_GetResultsV(loadFunc, sizeOut)); + + wasm_rt_memory_t memory; + memory.data = m3_GetMemory(runtime, NULL, 0); + memory.max_pages = memory.pages = 4; + memory.size = 4 * 65536; + Z_loader_instance_t loader; + Z_loader_instantiate(&loader, (struct Z_env_instance_t*)&memory); + + memcpy(memory.data, uw8, uw8Size); + *sizeOut = Z_loaderZ_load_uw8(&loader, (uint32_t)uw8Size); void* wasm = malloc(*sizeOut); - memcpy(wasm, memory, *sizeOut); + memcpy(wasm, memory.data, *sizeOut); return wasm; } @@ -345,32 +352,20 @@ int main(int argc, const char** argv) { uint32_t* pixels32 = malloc(320*240*4); + wasm_rt_init(); + Z_loader_init_module(); + Z_platform_init_module(); + IM3Environment env = m3_NewEnvironment(); IM3Runtime loaderRuntime = m3_NewRuntime(env, 65536, NULL); loaderRuntime->memory.maxPages = 4; verifyM3(loaderRuntime, ResizeMemory(loaderRuntime, 4)); - size_t loaderSize; - void* loaderWasm = loadFile(&loaderSize, "loader.wasm"); - - IM3Module loaderMod; - verifyM3(loaderRuntime, m3_ParseModule(env, &loaderMod, loaderWasm, loaderSize)); - loaderMod->memoryImported = true; - verifyM3(loaderRuntime, m3_LoadModule(loaderRuntime, loaderMod)); - verifyM3(loaderRuntime, m3_CompileModule(loaderMod)); - verifyM3(loaderRuntime, m3_RunStart(loaderMod)); - - IM3Function loadFunc; - verifyM3(loaderRuntime, m3_FindFunction(&loadFunc, loaderRuntime, "load_uw8")); - uint32_t cartSize; - void* cartWasm = loadUw8(&cartSize, loaderRuntime, loadFunc, argv[1]); + void* cartWasm = loadUw8(&cartSize, loaderRuntime, argv[1]); m3_FreeRuntime(loaderRuntime); - wasm_rt_init(); - Z_platform_init_module(); - bool quit = false; while(!quit) { Uw8Runtime runtime; diff --git a/main.ts b/main.ts deleted file mode 100644 index 2011a38..0000000 --- a/main.ts +++ /dev/null @@ -1,24 +0,0 @@ -let U8 = (...a) => new Uint8Array(...a); - -let memory = new WebAssembly.Memory({ initial: 4 }); -let memU8 = U8(memory.buffer); - -let importObject = { - env: { - memory - } -}; - -let loaderWasm = await Deno.readFile("loader.wasm"); -let loader = (await WebAssembly.instantiate(loaderWasm, importObject)).instance; - -let platformUw8 = await Deno.readFile("platform.uw8"); -console.log("platform.uw8 size: " + platformUw8.byteLength); -memU8.set(U8(platformUw8)); -let platformSize = loader.exports.load_uw8(platformUw8.byteLength); -let platformWasm = new ArrayBuffer(platformSize); -U8(platformWasm).set(memU8.slice(0, platformSize)); - -console.log("Unpacked platform size: " + platformSize); - -console.log("First byte: " + U8(platformWasm)[0]); \ No newline at end of file diff --git a/never_sleeps.uw8 b/never_sleeps.uw8 deleted file mode 100644 index 38919a1..0000000 Binary files a/never_sleeps.uw8 and /dev/null differ diff --git a/skipahead.uw8 b/skipahead.uw8 new file mode 100644 index 0000000..ed69fdb Binary files /dev/null and b/skipahead.uw8 differ