summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeith Winstein <keithw@cs.stanford.edu>2023-12-15 14:50:17 -0800
committerGitHub <noreply@github.com>2023-12-15 22:50:17 +0000
commit9fdd024249b6b181d98a4164700ca6ee09f970d9 (patch)
treeb8557b07e4f0a774c31dd387251c86f94cccfd2b
parent80b9752d478f1a710fda7ca51379aff87c407941 (diff)
downloadwabt-9fdd024249b6b181d98a4164700ca6ee09f970d9.tar.gz
wabt-9fdd024249b6b181d98a4164700ca6ee09f970d9.tar.bz2
wabt-9fdd024249b6b181d98a4164700ca6ee09f970d9.zip
Update wasm2c examples (#2358)
-rw-r--r--wasm2c/examples/fac/fac.c97
-rw-r--r--wasm2c/examples/rot13/rot13.wat8
2 files changed, 49 insertions, 56 deletions
diff --git a/wasm2c/examples/fac/fac.c b/wasm2c/examples/fac/fac.c
index bd4ccb58..1365def8 100644
--- a/wasm2c/examples/fac/fac.c
+++ b/wasm2c/examples/fac/fac.c
@@ -18,6 +18,27 @@
#include "fac.h"
+// Computes a pointer to an object of the given size in a little-endian memory.
+//
+// On a little-endian host, this is just &mem->data[addr] - the object's size is
+// unused. On a big-endian host, it's &mem->data[mem->size - addr - n], where n
+// is the object's size.
+//
+// Note that mem may be evaluated multiple times.
+//
+// Parameters:
+// mem - The memory.
+// addr - The address.
+// n - The size of the object.
+//
+// Result:
+// A pointer for an object of size n.
+#if WABT_BIG_ENDIAN
+#define MEM_ADDR(mem, addr, n) &(mem)->data[(mem)->size - (addr) - (n)]
+#else
+#define MEM_ADDR(mem, addr, n) &(mem)->data[addr]
+#endif
+
#define TRAP(x) (wasm_rt_trap(WASM_RT_TRAP_##x), 0)
#if WASM_RT_STACK_DEPTH_COUNT
@@ -86,70 +107,42 @@ static inline bool func_types_eq(const wasm_rt_func_type_t a,
#define FORCE_READ_FLOAT(var)
#endif
-#if WABT_BIG_ENDIAN
static inline void load_data(void* dest, const void* src, size_t n) {
if (!n) {
return;
}
- size_t i = 0;
- u8* dest_chars = dest;
wasm_rt_memcpy(dest, src, n);
- for (i = 0; i < (n >> 1); i++) {
+#if WABT_BIG_ENDIAN
+ u8* dest_chars = dest;
+ for (size_t 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;
}
+#endif
}
-#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, force_read) \
- 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)); \
- force_read(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) {
- if (!n) {
- return;
- }
- wasm_rt_memcpy(dest, src, n);
-}
-#define LOAD_DATA(m, o, i, s) \
- do { \
- RANGE_CHECK((&m), o, s); \
- load_data(&(m.data[o]), i, s); \
+
+#define LOAD_DATA(m, o, i, s) \
+ do { \
+ RANGE_CHECK((&m), o, s); \
+ load_data(MEM_ADDR(&m, o, s), i, s); \
} while (0)
-#define DEFINE_LOAD(name, t1, t2, t3, force_read) \
- 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)); \
- force_read(result); \
- return (t3)(t2)result; \
+
+#define DEFINE_LOAD(name, t1, t2, t3, force_read) \
+ static inline t3 name(wasm_rt_memory_t* mem, u64 addr) { \
+ MEMCHECK(mem, addr, t1); \
+ t1 result; \
+ wasm_rt_memcpy(&result, MEM_ADDR(mem, addr, sizeof(t1)), sizeof(t1)); \
+ force_read(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)); \
+#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_ADDR(mem, addr, sizeof(t1)), &wrapped, sizeof(t1)); \
}
-#endif
DEFINE_LOAD(i32_load, u32, u32, u32, FORCE_READ_INT)
DEFINE_LOAD(i64_load, u64, u64, u64, FORCE_READ_INT)
@@ -499,7 +492,7 @@ static float wasm_sqrtf(float 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);
+ memset(MEM_ADDR(mem, d, n), val, n);
}
static inline void memory_copy(wasm_rt_memory_t* dest,
@@ -509,7 +502,7 @@ static inline void memory_copy(wasm_rt_memory_t* dest,
u32 n) {
RANGE_CHECK(dest, dest_addr, n);
RANGE_CHECK(src, src_addr, n);
- memmove(dest->data + dest_addr, src->data + src_addr, n);
+ memmove(MEM_ADDR(dest, dest_addr, n), MEM_ADDR(src, src_addr, n), n);
}
static inline void memory_init(wasm_rt_memory_t* dest,
diff --git a/wasm2c/examples/rot13/rot13.wat b/wasm2c/examples/rot13/rot13.wat
index 4f1bb4be..605c89f9 100644
--- a/wasm2c/examples/rot13/rot13.wat
+++ b/wasm2c/examples/rot13/rot13.wat
@@ -7,18 +7,18 @@
;; No change if < 'A'.
(if (i32.lt_u (local.get $c) (i32.const 65))
- (return (local.get $c)))
+ (then (return (local.get $c))))
;; Clear 5th bit of c, to force uppercase. 0xdf = 0b11011111
(local.set $uc (i32.and (local.get $c) (i32.const 0xdf)))
;; In range ['A', 'M'] return |c| + 13.
(if (i32.le_u (local.get $uc) (i32.const 77))
- (return (i32.add (local.get $c) (i32.const 13))))
+ (then (return (i32.add (local.get $c) (i32.const 13)))))
;; In range ['N', 'Z'] return |c| - 13.
(if (i32.le_u (local.get $uc) (i32.const 90))
- (return (i32.sub (local.get $c) (i32.const 13))))
+ (then (return (i32.sub (local.get $c) (i32.const 13)))))
;; No change for everything else.
(return (local.get $c))
@@ -38,7 +38,7 @@
(block $exit
(loop $top
;; if (i >= size) break
- (if (i32.ge_u (local.get $i) (local.get $size)) (br $exit))
+ (if (i32.ge_u (local.get $i) (local.get $size)) (then (br $exit)))
;; mem[i] = rot13c(mem[i])
(i32.store8