aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/wamr/Makefile16
-rw-r--r--lib/wamr/README.md6
-rw-r--r--lib/wamr/core/iwasm/aot/aot_runtime.h0
-rw-r--r--lib/wamr/core/iwasm/common/wasm_memory.c2
-rw-r--r--lib/wamr/custom/platform.c (renamed from lib/wamr/core/shared/platform/stub/stubs.c)55
-rw-r--r--lib/wamr/custom/platform_internal.h (renamed from lib/wamr/core/shared/platform/stub/platform_internal.h)0
-rw-r--r--lib/wamr/test/fibonacci.c3
-rw-r--r--lib/wamr/test/main.c7
-rw-r--r--lib/wamr/wamr.h6
9 files changed, 59 insertions, 36 deletions
diff --git a/lib/wamr/Makefile b/lib/wamr/Makefile
index e24dc9d..ef26bb6 100644
--- a/lib/wamr/Makefile
+++ b/lib/wamr/Makefile
@@ -15,7 +15,6 @@ SRC = core/iwasm/common/wasm_application.c \
core/shared/mem-alloc/ems/ems_hmu.c \
core/shared/mem-alloc/ems/ems_kfc.c \
core/shared/mem-alloc/mem_alloc.c \
- core/shared/platform/stub/stubs.c \
core/shared/utils/bh_assert.c \
core/shared/utils/bh_bitmap.c \
core/shared/utils/bh_common.c \
@@ -25,12 +24,11 @@ SRC = core/iwasm/common/wasm_application.c \
core/shared/utils/bh_log.c \
core/shared/utils/bh_queue.c \
core/shared/utils/bh_vector.c \
- core/shared/utils/runtime_timer.c
+ core/shared/utils/runtime_timer.c \
+ custom/platform.c
-CFLAGS = -DBH_DEBUG=1 \
- -DBH_FREE=wasm_runtime_free \
+CFLAGS = -DBH_FREE=wasm_runtime_free \
-DBH_MALLOC=wasm_runtime_malloc \
- -DWASM_ENABLE_BULK_MEMORY=1 \
-DWASM_ENABLE_FAST_INTERP=1 \
-DWASM_ENABLE_INTERP=1 \
-Icore/iwasm/common \
@@ -38,8 +36,8 @@ CFLAGS = -DBH_DEBUG=1 \
-Icore/iwasm/interpreter \
-Icore/shared/mem-alloc \
-Icore/shared/platform/include \
- -Icore/shared/platform/stub \
- -Icore/shared/utils
+ -Icore/shared/utils \
+ -Icustom
LIB = libwamr.a
OBJ = $(patsubst %.c,%.o,$(SRC))
@@ -51,7 +49,7 @@ $(LIB): $(OBJ)
ar rcs $@ $^
%.o: %.c
- $(CC) -c -o $@ $< $(CFLAGS)
+ $(CC) -c -o $@ $< $(CFLAGS) -DBH_DEBUG
.PHONY: clean
clean:
@@ -62,7 +60,7 @@ check: test/test.exe test/fibonacci.wasm
(cd $(dir $<); ./$(notdir $<))
test/test.exe: test/main.c $(LIB)
- $(CC) -o $@ $< -Icore/iwasm/include -L. -lwamr
+ $(CC) -o $@ $< -I. -L. -lwamr
test/fibonacci.wasm: test/fibonacci.c
clang -target wasm32 -nostdlib -Wl,--no-entry -Wl,--export=fibonacci -Wl,--allow-undefined -o $@ $<
diff --git a/lib/wamr/README.md b/lib/wamr/README.md
index a2e0cde..fc71542 100644
--- a/lib/wamr/README.md
+++ b/lib/wamr/README.md
@@ -1,3 +1,9 @@
# WebAssembly Micro Runtime
Code copied from [`bytecodealliance/wasm-micro-runtime#WAMR-2.2.0`](https://github.com/bytecodealliance/wasm-micro-runtime/tree/217ba3b10c41036f0e9727aa93c2a164dba35e5c).
+
+## Custom Platform
+
+Instead of using existing platform code, we implemented a minimal platform, stubbing out everything that isn't absolutely necessary. This allows us to tailor the WAMR interpreter such that it has _just_ enough features to run the PICO-8 runtime, while keeping it as easy as possible to port to other systems.
+
+As a special note, we explicitly do **not** implement the `invokeNative` as it is ABI and architecture dependent, and WAMR provides `*_raw` variants for native functions that do not rely on platform specific behaviour (at the cost of being less convenient to use).
diff --git a/lib/wamr/core/iwasm/aot/aot_runtime.h b/lib/wamr/core/iwasm/aot/aot_runtime.h
deleted file mode 100644
index e69de29..0000000
--- a/lib/wamr/core/iwasm/aot/aot_runtime.h
+++ /dev/null
diff --git a/lib/wamr/core/iwasm/common/wasm_memory.c b/lib/wamr/core/iwasm/common/wasm_memory.c
index 5f5a1be..be2c76c 100644
--- a/lib/wamr/core/iwasm/common/wasm_memory.c
+++ b/lib/wamr/core/iwasm/common/wasm_memory.c
@@ -5,7 +5,9 @@
#include "wasm_runtime_common.h"
#include "../interpreter/wasm_runtime.h"
+#if WASM_ENABLE_AOT != 0
#include "../aot/aot_runtime.h"
+#endif
#include "mem_alloc.h"
#include "wasm_memory.h"
diff --git a/lib/wamr/core/shared/platform/stub/stubs.c b/lib/wamr/custom/platform.c
index 14e9528..64250d0 100644
--- a/lib/wamr/core/shared/platform/stub/stubs.c
+++ b/lib/wamr/custom/platform.c
@@ -3,16 +3,12 @@
#include "platform_api_vmcore.h"
#include "platform_internal.h"
+#ifdef BH_DEBUG
#include <execinfo.h>
+#include <time.h>
+#endif
-int bh_platform_init(void) {
- return 0;
-}
-
-void bh_platform_destroy(void) {
- return;
-}
-
+#ifdef BH_DEBUG
#define NOT_IMPLEMENTED() \
do { \
fprintf(stderr, "'%s' is not implemented\n", __FUNCTION__); \
@@ -25,6 +21,17 @@ void bh_platform_destroy(void) {
free(strs); \
abort(); \
} while (0)
+#else
+#define NOT_IMPLEMENTED() abort()
+#endif
+
+int bh_platform_init(void) {
+ return 0;
+}
+
+void bh_platform_destroy(void) {
+ return;
+}
void *os_malloc(unsigned int size) {
NOT_IMPLEMENTED();
@@ -50,48 +57,51 @@ int os_printf(const char *format, ...) {
}
int os_vprintf(const char *format, va_list ap) {
+#ifdef BH_DEBUG
return vprintf(format, ap);
+#else
+ return 0
+#endif
}
uint64_t os_time_get_boot_us(void) {
+#ifdef BH_DEBUG
+ struct timespec ts;
+ if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
+ return 0;
+ }
+ return ((uint64)ts.tv_sec) * 1000 * 1000 + ((uint64)ts.tv_nsec) / 1000;
+#else
return 0;
- // TODO: NOT_IMPLEMENTED();
+#endif
}
korp_tid os_self_thread(void) {
return 0;
- // TODO: NOT_IMPLEMENTED();
}
uint8_t *os_thread_get_stack_boundary(void) {
return NULL;
- // TODO: NOT_IMPLEMENTED();
}
int os_mutex_init(korp_mutex *mutex) {
return 0;
- // TODO: NOT_IMPLEMENTED();
}
int os_mutex_destroy(korp_mutex *mutex) {
return 0;
- // TODO: NOT_IMPLEMENTED();
}
int os_mutex_lock(korp_mutex *mutex) {
return 0;
- // TODO: NOT_IMPLEMENTED();
}
int os_mutex_unlock(korp_mutex *mutex) {
return 0;
- // TODO: NOT_IMPLEMENTED();
}
void *os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file) {
- if (prot != 0 || flags != 0 || file != os_get_invalid_handle()) {
- NOT_IMPLEMENTED();
- }
+ bh_assert(file == os_get_invalid_handle());
if ((uint64_t)size > UINT32_MAX) {
return NULL;
@@ -105,18 +115,15 @@ void *os_mmap(void *hint, size_t size, int prot, int flags, os_file_handle file)
}
void os_munmap(void *addr, size_t size) {
- NOT_IMPLEMENTED();
+ wasm_runtime_free(addr);
}
int os_mprotect(void *addr, size_t size, int prot) {
- if (prot != (MMAP_PROT_READ | MMAP_PROT_WRITE)) {
- NOT_IMPLEMENTED();
- }
return 0;
}
void *os_mremap(void *old_addr, size_t old_size, size_t new_size) {
- NOT_IMPLEMENTED();
+ return os_mremap_slow(old_addr, old_size, new_size);
}
int os_cond_init(korp_cond *cond) {
@@ -133,12 +140,10 @@ int os_dumps_proc_mem_info(char *out, unsigned int size) {
os_file_handle os_get_invalid_handle(void) {
return -1;
- // TODO: NOT_IMPLEMENTED();
}
uint32_t os_getpagesize(void) {
return 4096;
- // TODO: NOT_IMPLEMENTED();
}
void invokeNative(void (*native_code)(), uint32_t argv[], uint32_t argc) {
diff --git a/lib/wamr/core/shared/platform/stub/platform_internal.h b/lib/wamr/custom/platform_internal.h
index 4677c25..4677c25 100644
--- a/lib/wamr/core/shared/platform/stub/platform_internal.h
+++ b/lib/wamr/custom/platform_internal.h
diff --git a/lib/wamr/test/fibonacci.c b/lib/wamr/test/fibonacci.c
index ce67a7e..c511883 100644
--- a/lib/wamr/test/fibonacci.c
+++ b/lib/wamr/test/fibonacci.c
@@ -1,6 +1,7 @@
#include <stdint.h>
extern void lognumber(int32_t n);
+extern int maxint;
extern int32_t fibonacci(int32_t i) {
if (i < 0) {
@@ -16,7 +17,7 @@ extern int32_t fibonacci(int32_t i) {
result += last;
last = current;
- if (result > INT32_MAX) {
+ if (result > maxint) {
// overflow
return -1;
}
diff --git a/lib/wamr/test/main.c b/lib/wamr/test/main.c
index b0bd004..0172b14 100644
--- a/lib/wamr/test/main.c
+++ b/lib/wamr/test/main.c
@@ -2,7 +2,7 @@
#include <stdio.h>
#include <stdlib.h>
-#include "wasm_export.h"
+#include "wamr.h"
#define P_CHECK(x, name) \
if (!(x)) { \
@@ -77,5 +77,10 @@ int main(int argc, char *argv[argc + 1]) {
}
printf("=> %d\n", result);
+ wasm_runtime_destroy_exec_env(exec);
+ wasm_runtime_deinstantiate(instance);
+ wasm_runtime_unload(module);
+ wasm_runtime_destroy();
+
return EXIT_SUCCESS;
}
diff --git a/lib/wamr/wamr.h b/lib/wamr/wamr.h
new file mode 100644
index 0000000..6c0236e
--- /dev/null
+++ b/lib/wamr/wamr.h
@@ -0,0 +1,6 @@
+#ifndef _WAMR_H_
+#define _WAMR_H_
+
+#include "./core/iwasm/include/wasm_export.h"
+
+#endif