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 Feb 12, 2024
1 parent b55cff3 commit 1841618
Show file tree
Hide file tree
Showing 6 changed files with 591 additions and 7 deletions.
8 changes: 8 additions & 0 deletions bin/mlton-script
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ 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 wasi '-D_WASI_EMULATED_MMAN' \
-target-cc-opt x86 '-m32' \
-target-link-opt aix '-maix64' \
-target-link-opt alpha \
Expand All @@ -143,6 +147,10 @@ for arg in "$@"; do
'-lws2_32 -lkernel32 -lpsapi -lnetapi32 -lwinmm' \
-target-link-opt mingw '-Wl,--enable-stdcall-fixup' \
-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 wasi '-lwasi-emulated-mman' \
-target-link-opt x86 '-m32' \
-profile-exclude '\$\(SML_LIB\)'
fi
Expand Down
28 changes: 21 additions & 7 deletions runtime/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ endif

EXE :=

WASMTIME :=

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

Expand Down Expand Up @@ -115,6 +117,18 @@ 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 \
-D_WASI_EMULATED_MMAN
XLDFLAGS += -lwasi-emulated-signal \
-lwasi-emulated-process-clocks \
-lwasi-emulated-getpid \
-lwasi-emulated-mman
endif

ifeq ($(TARGET_OS), mingw)
EXE := .exe
# GCC doesn't recognize the %I64 format specifier which means %ll on windows
Expand Down Expand Up @@ -244,11 +258,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 @@ -267,7 +281,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 @@ -312,7 +326,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 @@ -324,7 +338,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 @@ -353,7 +367,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
67 changes: 67 additions & 0 deletions runtime/platform/wasi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "platform.h"

#include "platform/mremap.c"
#include "platform/use-mmap.c"

/* 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) {
long int pageSize;

pageSize = sysconf (_SC_PAGESIZE);
if (pageSize < 0)
diee ("GC_pageSize error: sysconf (_SC_PAGESIZE) failed");

return (size_t)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_extendHead (void *base, size_t length) {
return mmapAnon (base, length);
}

void* GC_extendTail (void *base, size_t length) {
return mmapAnon (base, length);
}

void GC_displayMem (void) {
printf ("GC_displayMem() not implemented on WASI\n");
}

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 1841618

Please sign in to comment.