Skip to content

Commit

Permalink
Add wasm32-unknown-wasi target to mlton
Browse files Browse the repository at this point in the history
This requires:
- WASI SDK: https://github.com/WebAssembly/wasi-sdk
- wasmtime: https://wasmtime.dev/
  • Loading branch information
agoode committed May 16, 2024
1 parent 94e4af4 commit 9a7e207
Show file tree
Hide file tree
Showing 19 changed files with 608 additions and 7 deletions.
6 changes: 6 additions & 0 deletions bin/mlton-script
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ for arg in "$@"; do
-target-cc-opt ia64-hpux "-mlp64" \
-target-cc-opt ia64 "-mtune=itanium2" \
-target-cc-opt sparc '-m32 -mcpu=v8 -Wa,-xarch=v8plusa' \
-target-cc-opt wasi '-D_WASI_EMULATED_SIGNAL' \
-target-cc-opt wasi '-D_WASI_EMULATED_PROCESS_CLOCKS' \
-target-cc-opt wasi '-D_WASI_EMULATED_GETPID' \
-target-cc-opt x86 '-m32' \
-target-link-opt aix '-maix64' \
-target-link-opt alpha \
Expand All @@ -134,6 +137,9 @@ for arg in "$@"; do
-target-link-opt mingw '-Wl,--enable-stdcall-fixup' \
-target-link-opt openbsd '-Wl,--no-execute-only' \
-target-link-opt solaris '-lnsl -lsocket -lrt' \
-target-link-opt wasi '-lwasi-emulated-signal' \
-target-link-opt wasi '-lwasi-emulated-process-clocks' \
-target-link-opt wasi '-lwasi-emulated-getpid' \
-target-link-opt x86 '-m32' \
-profile-exclude '\$\(SML_LIB\)'
fi
Expand Down
26 changes: 19 additions & 7 deletions runtime/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ endif

EXE :=

WASMTIME :=

MDVARIANTS := OPT DBG
PIVARIANTS := DPI NPI PIC PIE

Expand Down Expand Up @@ -110,6 +112,16 @@ ifeq ($(TARGET_OS), cygwin)
EXE := .exe
endif

ifeq ($(TARGET_OS), wasi)
WASMTIME := wasmtime
XCPPFLAGS += -D_WASI_EMULATED_SIGNAL \
-D_WASI_EMULATED_PROCESS_CLOCKS \
-D_WASI_EMULATED_GETPID
XLDFLAGS += -lwasi-emulated-signal \
-lwasi-emulated-process-clocks \
-lwasi-emulated-getpid
endif

ifeq ($(TARGET_OS), mingw)
EXE := .exe
# GCC doesn't recognize the %I64 format specifier which means %ll on windows
Expand Down Expand Up @@ -239,11 +251,11 @@ ml-types.h: gen/ml-types.h
$(CP) $< $@

gen/ml-types.h: gen/gen-types$(EXE)
./gen/gen-types$(EXE) ml-types.h > gen/ml-types.h
$(WASMTIME) ./gen/gen-types$(EXE) ml-types.h > gen/ml-types.h
gen/c-types.h: gen/gen-types$(EXE)
./gen/gen-types$(EXE) c-types.h > gen/c-types.h
$(WASMTIME) ./gen/gen-types$(EXE) c-types.h > gen/c-types.h
gen/c-types.sml: gen/gen-types$(EXE)
./gen/gen-types$(EXE) c-types.sml > gen/c-types.sml
$(WASMTIME) ./gen/gen-types$(EXE) c-types.sml > gen/c-types.sml

ifneq ($(MAKECMDGOALS),clean)
-include gen/gen-types.d
Expand All @@ -262,7 +274,7 @@ gen/$(1): gen/gen-basis-ffi.sml gen/basis-ffi.def
touch gen/$(1)
else
gen/$(1): gen/gen-basis-ffi$(EXE) gen/basis-ffi.def
./gen/gen-basis-ffi$(EXE) $(1) < gen/basis-ffi.def > gen/$(1)
$(WASMTIME) ./gen/gen-basis-ffi$(EXE) $(1) < gen/basis-ffi.def > gen/$(1)
$(CAT) gen/gen-basis-ffi.sml gen/basis-ffi.def gen/$(1) | $(SHA1DGST) > gen/$(1).chk
endif
endef
Expand Down Expand Up @@ -307,7 +319,7 @@ gdtoa/README: gdtoa.tgz gdtoa.may_alias-unions.patch gdtoa.rename-public-fns.pat
@touch $@

gdtoa/arith.h: gdtoa/arithchk$(EXE)
./gdtoa/arithchk$(EXE) > gdtoa/arith.h
$(WASMTIME) ./gdtoa/arithchk$(EXE) > gdtoa/arith.h

gdtoa/arithchk.c: gdtoa/README
@touch $@
Expand All @@ -319,7 +331,7 @@ ifneq ($(MAKECMDGOALS),clean)
endif

gdtoa/gd_qnan.h: gdtoa/qnan$(EXE)
./gdtoa/qnan$(EXE) > gdtoa/gd_qnan.h
$(WASMTIME) ./gdtoa/qnan$(EXE) > gdtoa/gd_qnan.h

gdtoa/qnan.c: gdtoa/README
@touch $@
Expand Down Expand Up @@ -348,7 +360,7 @@ $(eval $(call A_TEMPLATE,mlton,MLTON))
### gen/constants ###

gen/constants: gen/gen-constants$(EXE)
./gen/gen-constants > gen/constants
$(WASMTIME) ./gen/gen-constants > gen/constants

ifneq ($(MAKECMDGOALS),clean)
-include gen/gen-constants.d
Expand Down
4 changes: 4 additions & 0 deletions runtime/cenv.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ COMPILE_TIME_ASSERT(sizeof_double__is_eight, sizeof(double) == 8);
#include "platform/openbsd.h"
#elif (defined (__sun__))
#include "platform/solaris.h"
#elif (defined (__wasi__))
#include "platform/wasi.h"
#else
#error unknown platform os
#endif
Expand Down Expand Up @@ -117,6 +119,8 @@ COMPILE_TIME_ASSERT(sizeof_double__is_eight, sizeof(double) == 8);
#include "platform/x86.h"
#elif (defined (__loongarch64))
#include "platform/loongarch64.h"
#elif (defined (__wasm32))
#include "platform/wasm32.h"
#else
#error unknown platform arch
#endif
Expand Down
2 changes: 2 additions & 0 deletions runtime/gc/heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ void shrinkHeap (GC_state s, GC_heap h, size_t keepSize) {
}
assert (isAligned (keepWithMapsSize, s->sysvals.pageSize));
assert (keepWithMapsSize <= h->withMapsSize);
#if HAS_SHRINK_HEAP
GC_release (h->start + keepWithMapsSize, h->withMapsSize - keepWithMapsSize);
#endif
h->size = keepSize;
h->withMapsSize = keepWithMapsSize;
}
Expand Down
4 changes: 4 additions & 0 deletions runtime/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
#error HAS_REMAP not defined
#endif

#ifndef HAS_SHRINK_HEAP
#error HAS_SHRINK_HEAP not defined
#endif

#ifndef HAS_SIGALTSTACK
#error HAS_SIGALTSTACK not defined
#else
Expand Down
1 change: 1 addition & 0 deletions runtime/platform/aix.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#define HAS_MSG_DONTWAIT FALSE
#define HAS_PTRACE FALSE
#define HAS_REMAP FALSE
#define HAS_SHRINK_HEAP TRUE
#define HAS_SIGALTSTACK TRUE
#define NEEDS_SIGALTSTACK_EXEC FALSE
#define HAS_SPAWN FALSE
Expand Down
1 change: 1 addition & 0 deletions runtime/platform/cygwin.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

#define HAS_FEROUND FALSE
#define HAS_REMAP TRUE
#define HAS_SHRINK_HEAP TRUE
#define HAS_SIGALTSTACK FALSE
#define NEEDS_SIGALTSTACK_EXEC FALSE
#define HAS_SPAWN TRUE
Expand Down
1 change: 1 addition & 0 deletions runtime/platform/darwin.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#define HAS_FEROUND TRUE
#define HAS_MSG_DONTWAIT TRUE
#define HAS_REMAP FALSE
#define HAS_SHRINK_HEAP TRUE
#define HAS_SIGALTSTACK TRUE
#define NEEDS_SIGALTSTACK_EXEC FALSE
#define HAS_SPAWN FALSE
Expand Down
1 change: 1 addition & 0 deletions runtime/platform/freebsd.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#define HAS_FEROUND TRUE
#define HAS_MSG_DONTWAIT TRUE
#define HAS_REMAP FALSE
#define HAS_SHRINK_HEAP TRUE
#define HAS_SIGALTSTACK TRUE
#define NEEDS_SIGALTSTACK_EXEC FALSE
#define HAS_SPAWN FALSE
Expand Down
1 change: 1 addition & 0 deletions runtime/platform/hpux.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#define HAS_FEROUND TRUE
#define HAS_MSG_DONTWAIT FALSE
#define HAS_REMAP FALSE
#define HAS_SHRINK_HEAP TRUE
#define HAS_SIGALTSTACK TRUE
#define NEEDS_SIGALTSTACK_EXEC FALSE
#define HAS_SPAWN FALSE
Expand Down
1 change: 1 addition & 0 deletions runtime/platform/hurd.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#define HAS_FEROUND TRUE
#define HAS_MSG_DONTWAIT TRUE
#define HAS_REMAP TRUE
#define HAS_SHRINK_HEAP TRUE
#define HAS_SIGALTSTACK TRUE
#define NEEDS_SIGALTSTACK_EXEC FALSE
#define HAS_SPAWN FALSE
Expand Down
1 change: 1 addition & 0 deletions runtime/platform/linux.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#endif
#define HAS_MSG_DONTWAIT TRUE
#define HAS_REMAP TRUE
#define HAS_SHRINK_HEAP TRUE
#define HAS_SIGALTSTACK TRUE
#if (defined (__hppa__))
#define NEEDS_SIGALTSTACK_EXEC TRUE
Expand Down
1 change: 1 addition & 0 deletions runtime/platform/mingw.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#define HAS_FEROUND FALSE
#define HAS_MSG_DONTWAIT FALSE
#define HAS_REMAP TRUE
#define HAS_SHRINK_HEAP TRUE
#define HAS_SIGALTSTACK FALSE
#define NEEDS_SIGALTSTACK_EXEC FALSE
#define HAS_SPAWN TRUE
Expand Down
1 change: 1 addition & 0 deletions runtime/platform/netbsd.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#define HAS_FEROUND FALSE
#define HAS_MSG_DONTWAIT TRUE
#define HAS_REMAP FALSE
#define HAS_SHRINK_HEAP TRUE
#define HAS_SIGALTSTACK TRUE
#define NEEDS_SIGALTSTACK_EXEC FALSE
#define HAS_SPAWN FALSE
Expand Down
1 change: 1 addition & 0 deletions runtime/platform/openbsd.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#define HAS_FEROUND FALSE
#define HAS_MSG_DONTWAIT TRUE
#define HAS_REMAP FALSE
#define HAS_SHRINK_HEAP TRUE
#define HAS_SIGALTSTACK TRUE
#define NEEDS_SIGALTSTACK_EXEC FALSE
#define HAS_SPAWN FALSE
Expand Down
1 change: 1 addition & 0 deletions runtime/platform/solaris.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#define HAS_FEROUND TRUE
#define HAS_MSG_DONTWAIT TRUE
#define HAS_REMAP FALSE
#define HAS_SHRINK_HEAP TRUE
#define HAS_SIGALTSTACK TRUE
#define NEEDS_SIGALTSTACK_EXEC FALSE
#define HAS_SPAWN FALSE
Expand Down
71 changes: 71 additions & 0 deletions runtime/platform/wasi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "platform.h"

/* WASI only implements a subset of POSIX, and given how it works it doesn't
* make too much sense to try too hard to emulate missing functionality.
*
* Every function needed by the runtime library and not provided by WASI
* is declared in wasi.h, but only a tiny number are implemented here.
*
* This means that any use of missing runtime functions from SML code will
* result in linker errors at the end of compilation, rather than as a runtime
* error later.
*
* The functions in this file are the bare minimum to get the GC working,
* needed by every program compiled by MLton.
*/

size_t GC_pageSize (void) {
return PAGESIZE;
}

uintmax_t GC_physMem (void) {
/* WASI doesn't provide a way to query actual physical memory, so pick
* a reasonable amount (as of Feb 2024).
*/
return 1 << 30; /* 1 GiB */
}

void *GC_mmapAnon (__attribute__ ((unused)) void *start, size_t length) {
void *mem;
int err = posix_memalign (&mem, PAGESIZE, length);
if (err) {
return (void *) -1;
}
return memset (mem, 0, length);
}

void *GC_mmapAnonFlags (void *start, size_t length,
__attribute__ ((unused)) int flags) {
return GC_mmapAnon (start, length);
}

void GC_release (void *base, __attribute__ ((unused)) size_t length) {
free (base);
}

void GC_displayMem (void) {
size_t memory_size = (size_t) sbrk(0);
size_t pages = memory_size / PAGESIZE;
printf ("memory.size: pages=%zu, pagesize=%d, total=%zu\n",
pages, PAGESIZE, memory_size);
}

void GC_diskBack_close (__attribute__ ((unused)) void *data) {
die ("Disk-backed heap not supported on WASI");
}

void GC_diskBack_read (__attribute__ ((unused)) void *data,
__attribute__ ((unused)) pointer buf,
__attribute__ ((unused)) size_t size) {
die ("Disk-backed heap not supported on WASI");
}

void *GC_diskBack_write (__attribute__ ((unused)) pointer buf,
__attribute__ ((unused)) size_t size) {
die ("Disk-backed heap not supported on WASI");
}

int sigemptyset (sigset_t *set) {
*set = (sigset_t) 0;
return 0;
}
Loading

0 comments on commit 9a7e207

Please sign in to comment.