diff options
author | Sam Clegg <sbc@chromium.org> | 2022-03-07 18:43:46 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-03-07 10:43:46 -0800 |
commit | ccbe21aac55a088069f15064e210684803e6a169 (patch) | |
tree | 34b1b0c711cd7afc56caa7af04a3e018e8c4176b | |
parent | 7b40e2fbf69446f203dfb3e667dc8355741c7d69 (diff) | |
download | wabt-ccbe21aac55a088069f15064e210684803e6a169.tar.gz wabt-ccbe21aac55a088069f15064e210684803e6a169.tar.bz2 wabt-ccbe21aac55a088069f15064e210684803e6a169.zip |
Update wasm2c example output. NFC (#1851)
Do we really want to have this output checked in like this?
If so, we should probably automate its update ot at least add
a check to avoid it getting out-of-date.
-rw-r--r-- | wasm2c/.gitignore | 5 | ||||
-rw-r--r-- | wasm2c/examples/fac/fac.c | 210 | ||||
-rw-r--r-- | wasm2c/examples/fac/fac.h | 3 |
3 files changed, 141 insertions, 77 deletions
diff --git a/wasm2c/.gitignore b/wasm2c/.gitignore index 47ea610d..5d665f06 100644 --- a/wasm2c/.gitignore +++ b/wasm2c/.gitignore @@ -1,10 +1,7 @@ wasm-rt-impl.o -examples/fac/main.o +examples/**/*.o examples/fac/fac -examples/fac/fac.o -examples/rot13/main.o examples/rot13/rot13 examples/rot13/rot13.c examples/rot13/rot13.h -examples/rot13/rot13.o examples/rot13/rot13.wasm diff --git a/wasm2c/examples/fac/fac.c b/wasm2c/examples/fac/fac.c index acd8ed91..eeb28d9f 100644 --- a/wasm2c/examples/fac/fac.c +++ b/wasm2c/examples/fac/fac.c @@ -19,50 +19,94 @@ #define CALL_INDIRECT(table, t, ft, x, ...) \ (LIKELY((x) < table.size && table.data[x].func && \ table.data[x].func_type == func_types[ft]) \ - ? ((t)table.data[x].func)(__VA_ARGS__) \ - : TRAP(CALL_INDIRECT)) + || TRAP(CALL_INDIRECT) \ + , ((t)table.data[x].func)(__VA_ARGS__)) -#define MEMCHECK(mem, a, t) \ +#define RANGE_CHECK(mem, a, t) \ if (UNLIKELY((a) + sizeof(t) > mem->size)) TRAP(OOB) -#define DEFINE_LOAD(name, t1, t2, t3) \ - static inline t3 name(wasm_rt_memory_t* mem, u64 addr) { \ - MEMCHECK(mem, addr, t1); \ - t1 result; \ - memcpy(&result, &mem->data[addr], sizeof(t1)); \ - return (t3)(t2)result; \ +#if WASM_RT_MEMCHECK_SIGNAL_HANDLER +#define MEMCHECK(mem, a, t) +#else +#define MEMCHECK(mem, a, t) RANGE_CHECK(mem, a, t) +#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, char[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; \ + __builtin_memcpy(&result, &mem->data[mem->size - addr - sizeof(t1)], sizeof(t1)); \ + return (t3)(t2)result; \ } -#define DEFINE_STORE(name, t1, t2) \ - static inline void name(wasm_rt_memory_t* mem, u64 addr, t2 value) { \ +#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; \ + __builtin_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, char[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 wrapped = (t1)value; \ - memcpy(&mem->data[addr], &wrapped, sizeof(t1)); \ + t1 result; \ + __builtin_memcpy(&result, &mem->data[addr], sizeof(t1)); \ + return (t3)(t2)result; \ } -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); +#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; \ + __builtin_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) #define I32_CLZ(x) ((x) ? __builtin_clz(x) : 32) #define I64_CLZ(x) ((x) ? __builtin_clzll(x) : 64) @@ -114,25 +158,47 @@ DEFINE_STORE(i64_store32, u32, u64); : (UNLIKELY((x) == 0 && (y) == 0)) ? (signbit(x) ? y : x) \ : (x > y) ? x : y) -#define TRUNC_S(ut, st, ft, min, max, maxop, x) \ - ((UNLIKELY((x) != (x))) ? TRAP(INVALID_CONVERSION) \ - : (UNLIKELY((x) < (ft)(min) || (x) maxop (ft)(max))) ? TRAP(INT_OVERFLOW) \ - : (ut)(st)(x)) - -#define I32_TRUNC_S_F32(x) TRUNC_S(u32, s32, f32, INT32_MIN, INT32_MAX, >=, x) -#define I64_TRUNC_S_F32(x) TRUNC_S(u64, s64, f32, INT64_MIN, INT64_MAX, >=, x) -#define I32_TRUNC_S_F64(x) TRUNC_S(u32, s32, f64, INT32_MIN, INT32_MAX, >, x) -#define I64_TRUNC_S_F64(x) TRUNC_S(u64, s64, f64, INT64_MIN, INT64_MAX, >=, x) - -#define TRUNC_U(ut, ft, max, maxop, x) \ - ((UNLIKELY((x) != (x))) ? TRAP(INVALID_CONVERSION) \ - : (UNLIKELY((x) <= (ft)-1 || (x) maxop (ft)(max))) ? TRAP(INT_OVERFLOW) \ - : (ut)(x)) - -#define I32_TRUNC_U_F32(x) TRUNC_U(u32, f32, UINT32_MAX, >=, x) -#define I64_TRUNC_U_F32(x) TRUNC_U(u64, f32, UINT64_MAX, >=, x) -#define I32_TRUNC_U_F64(x) TRUNC_U(u32, f64, UINT32_MAX, >, x) -#define I64_TRUNC_U_F64(x) TRUNC_U(u64, f64, UINT64_MAX, >=, x) +#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) { \ @@ -146,36 +212,34 @@ DEFINE_REINTERPRET(i32_reinterpret_f32, f32, u32) DEFINE_REINTERPRET(f64_reinterpret_i64, u64, f64) DEFINE_REINTERPRET(i64_reinterpret_f64, f64, u64) - static u32 func_types[1]; - static void init_func_types(void) { func_types[0] = wasm_rt_register_func_type(1, 1, WASM_RT_I32, WASM_RT_I32); } -static u32 fac(u32); +static u32 w2c_fac(u32); static void init_globals(void) { } -static u32 fac(u32 p0) { +static u32 w2c_fac(u32 w2c_p0) { FUNC_PROLOGUE; - u32 i0, i1, i2; - i0 = p0; - i1 = 0u; - i0 = i0 == i1; - if (i0) { - i0 = 1u; + u32 w2c_i0, w2c_i1, w2c_i2; + w2c_i0 = w2c_p0; + w2c_i1 = 0u; + w2c_i0 = w2c_i0 == w2c_i1; + if (w2c_i0) { + w2c_i0 = 1u; } else { - i0 = p0; - i1 = p0; - i2 = 1u; - i1 -= i2; - i1 = fac(i1); - i0 *= i1; + w2c_i0 = w2c_p0; + w2c_i1 = w2c_p0; + w2c_i2 = 1u; + w2c_i1 -= w2c_i2; + w2c_i1 = w2c_fac(w2c_i1); + w2c_i0 *= w2c_i1; } FUNC_EPILOGUE; - return i0; + return w2c_i0; } static void init_memory(void) { @@ -190,7 +254,7 @@ u32 (*WASM_RT_ADD_PREFIX(Z_facZ_ii))(u32); static void init_exports(void) { /* export: 'fac' */ - WASM_RT_ADD_PREFIX(Z_facZ_ii) = (&fac); + WASM_RT_ADD_PREFIX(Z_facZ_ii) = (&w2c_fac); } void WASM_RT_ADD_PREFIX(init)(void) { diff --git a/wasm2c/examples/fac/fac.h b/wasm2c/examples/fac/fac.h index 5e78f33b..41c50d4d 100644 --- a/wasm2c/examples/fac/fac.h +++ b/wasm2c/examples/fac/fac.h @@ -18,6 +18,8 @@ extern "C" { #define WASM_RT_ADD_PREFIX(x) WASM_RT_PASTE(WASM_RT_MODULE_PREFIX, x) /* 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; @@ -28,6 +30,7 @@ typedef uint64_t u64; typedef int64_t s64; typedef float f32; typedef double f64; +#endif extern void WASM_RT_ADD_PREFIX(init)(void); |