diff --git a/Makefile b/Makefile index c2fac77..1bd805a 100644 --- a/Makefile +++ b/Makefile @@ -33,6 +33,16 @@ LDFLAGS := $(if $(DEBUG),-O0 -g,-O3) CFLAGS += -std=c11 -Wall -Wextra -Wdouble-promotion -Wvla -pedantic +TARGET = $(lastword $(shell $(CC) -v 2>&1 | grep "Target: ")) + +LIB_SUFFIX := so + +ifeq ($(TARGET),wasm32) + LIB_SUFFIX := wasm + CFLAGS += -mbulk-memory + LDFLAGS += -nostdlib -Wl,--no-entry -Wl,--export-dynamic +endif + ifneq ($(LC3_PLUS),) DEFINE += LC3_PLUS=$(LC3_PLUS) endif @@ -50,7 +60,7 @@ lib_list := bin_list := define add-lib - $(eval $(1)_bin ?= $(1).so) + $(eval $(1)_bin ?= $(1).$(LIB_SUFFIX)) $(eval $(1)_bin := $(addprefix $(BIN_DIR)/,$($(1)_bin))) lib_list += $(1) diff --git a/src/common.h b/src/common.h index 0304811..2f6cd1f 100644 --- a/src/common.h +++ b/src/common.h @@ -24,7 +24,15 @@ #include #include + +#ifdef __wasm32__ +#define memmove __builtin_memmove +#define memset __builtin_memset +#define memcpy __builtin_memcpy +#define NULL ((void*)0) +#else #include +#endif #ifdef __ARM_ARCH #include diff --git a/src/fastmath.h b/src/fastmath.h index fe68f14..a44e2e8 100644 --- a/src/fastmath.h +++ b/src/fastmath.h @@ -20,7 +20,26 @@ #define __LC3_FASTMATH_H #include + +#ifdef __wasm32__ +#define log10f __builtin_log10f +#define sqrtf __builtin_sqrtf +#define fabsf __builtin_fabsf +#define floorf __builtin_floorf +#define fminf __builtin_fminf +#define fmaxf __builtin_fmaxf +#define truncf __builtin_truncf +// This is not exactly roundf, as this return the even value for two equally near +// values. e.g +// - roundf(0.5) = 1, nearbyint(0.5) = 0, +// - roundf(1.5) = 2, nearbyint(1.5) = 2, +// - roundf(2.5) = 3, nearbyint(2.5) = 2 +// but this builtin maps to https://webassembly.github.io/spec/core/exec/numerics.html#op-fnearest +#define roundf __builtin_nearbyint +#define INFINITY __builtin_inff() +#else #include +#endif /**