wasm: Add a wasm liblc3 library

The wasm library is automatically built when the compiler target
is set to wasm32 in bin/liblc3.wasm.

This can be done using `make CC="clang --target=wasm32"`.

This wasm library doesn't have any import and expose all liblc3
functions.
This commit is contained in:
David Duarte
2024-02-21 00:50:17 +00:00
committed by Antoine SOULIER
parent d7a739849a
commit e67bb2d07d
3 changed files with 38 additions and 1 deletions

View File

@@ -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)

View File

@@ -24,7 +24,15 @@
#include <stdalign.h>
#include <limits.h>
#ifdef __wasm32__
#define memmove __builtin_memmove
#define memset __builtin_memset
#define memcpy __builtin_memcpy
#define NULL ((void*)0)
#else
#include <string.h>
#endif
#ifdef __ARM_ARCH
#include <arm_acle.h>

View File

@@ -20,7 +20,26 @@
#define __LC3_FASTMATH_H
#include <stdint.h>
#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 <math.h>
#endif
/**