diff --git a/.clang-format b/.clang-format index 9b3aa8b..5aa5600 100644 --- a/.clang-format +++ b/.clang-format @@ -1 +1,2 @@ BasedOnStyle: LLVM +SortIncludes: Never diff --git a/src/buffer.c b/src/buffer.c index b9c449f..21b2253 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -1,16 +1,9 @@ /* buffer.c — fixed-capacity byte buffer */ #include "buffer.h" +#include "mem.h" #include "utf8.h" -typedef __SIZE_TYPE__ size_t; - -void *memcpy(void *dst, const void *src, size_t n) { - return __builtin_memcpy(dst, src, n); -} - -void *memset(void *dst, int c, size_t n) { return __builtin_memset(dst, c, n); } - static size_t strlen(const char *s) { size_t n = 0; while (s[n]) diff --git a/src/clayterm.c b/src/clayterm.c index 1047062..52481a5 100644 --- a/src/clayterm.c +++ b/src/clayterm.c @@ -13,6 +13,7 @@ #include "../clay/clay.h" #include "buffer.h" #include "cell.h" +#include "mem.h" #include "utf8.h" #include "wcwidth.h" @@ -290,7 +291,7 @@ static uint32_t rd(uint32_t *buf, int len, int *i) { static float rdf(uint32_t *buf, int len, int *i) { uint32_t v = rd(buf, len, i); float f; - __builtin_memcpy(&f, &v, 4); + memcpy(&f, &v, 4); return f; } diff --git a/src/mem.c b/src/mem.c new file mode 100644 index 0000000..77818fd --- /dev/null +++ b/src/mem.c @@ -0,0 +1,18 @@ +/* mem.c — memcpy/memset shims for freestanding wasm32 */ + +#include "mem.h" + +void *memcpy(void *dst, const void *src, size_t n) { + unsigned char *d = (unsigned char *)dst; + const unsigned char *s = (const unsigned char *)src; + while (n--) + *d++ = *s++; + return dst; +} + +void *memset(void *dst, int c, size_t n) { + unsigned char *d = (unsigned char *)dst; + while (n--) + *d++ = (unsigned char)c; + return dst; +} diff --git a/src/mem.h b/src/mem.h new file mode 100644 index 0000000..747b921 --- /dev/null +++ b/src/mem.h @@ -0,0 +1,11 @@ +/* mem.h — memcpy/memset shims for freestanding wasm32 */ + +#ifndef MEM_H +#define MEM_H + +typedef __SIZE_TYPE__ size_t; + +void *memcpy(void *dst, const void *src, size_t n); +void *memset(void *dst, int c, size_t n); + +#endif diff --git a/src/module.c b/src/module.c index 398535b..75276c0 100644 --- a/src/module.c +++ b/src/module.c @@ -2,8 +2,9 @@ #include "../clay/clay.h" +#include "mem.c" #include "buffer.c" #include "cell.c" -#include "clayterm.c" #include "utf8.c" #include "wcwidth.c" +#include "clayterm.c"