Skip to content

Commit

Permalink
Merge branch 'feature/wasm-ar' into production
Browse files Browse the repository at this point in the history
  • Loading branch information
tyfkda committed Feb 28, 2024
2 parents ac55b9d + b3e969d commit 4a3fb57
Show file tree
Hide file tree
Showing 19 changed files with 808 additions and 859 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/deploywcc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: 20.x
- name: Setup llvm-ar
run: sudo apt install llvm-dev
- name: Install NPM packages
run: npm ci
- name: Build release
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ jobs:
run: make test-gen2

- name: setup
run: npm ci
run: |
npm ci
sudo apt update && sudo apt install llvm-dev
- name: make wcc
run: make -j8 wcc
- name: test wcc
Expand Down
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ core

### wasm

libsrc/_wasm/crt0.c
libsrc/_wasm/libc.c

*.wasm
/wcc
/wcc-ld
Expand Down
35 changes: 13 additions & 22 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ test-libs: all
clean:
rm -rf $(EXES) $(OBJ_DIR) a.out gen2* gen3* tmp.s \
dump_expr* dump_ir* dump_type* \
wcc wcc-ld cc.wasm a.wasm public release $(WCC_LIBS)
wcc wcc-ld cc.wasm a.wasm public release $(WCC_LIBS) $(WCC_OBJ_DIR) $(WCC_LIB_DIR)
@$(MAKE) -C libsrc clean
@$(MAKE) -C tests clean

Expand Down Expand Up @@ -187,24 +187,30 @@ test-self-hosting: self-hosting
### Wasm version

WCC_OBJ_DIR:=obj/wcc
WCC_LIB_DIR:=lib

WCC_DIR:=src/wcc
WCC_CFLAGS:=$(CFLAGS) -I$(CPP_DIR)

WCC_SRCS:=$(wildcard $(WCC_DIR)/*.c) \
$(wildcard $(CC1_FE_DIR)/*.c) \
$(wildcard $(CC1_FE_DIR)/*.c) $(UTIL_DIR)/archive.c \
$(CPP_DIR)/preprocessor.c $(CPP_DIR)/pp_parser.c $(CPP_DIR)/macro.c \
$(UTIL_DIR)/util.c $(UTIL_DIR)/table.c
WCC_OBJS:=$(addprefix $(WCC_OBJ_DIR)/,$(notdir $(WCC_SRCS:.c=.o)))
WCC_LIBS:=$(LIBSRC_DIR)/_wasm/crt0.c $(LIBSRC_DIR)/_wasm/libc.c
WCC_LIBS:=$(WCC_LIB_DIR)/wcrt0.a $(WCC_LIB_DIR)/wlibc.a

wcc: $(PARENT_DEPS) $(WCC_OBJS) $(WCC_LIBS)
wcc: $(PARENT_DEPS) $(WCC_OBJS)
$(CC) -o $@ $(WCC_OBJS) $(LDFLAGS)
$(MAKE) wcc-libs

.PHONY: wcc-libs
wcc-libs:
$(MAKE) CC=../wcc AR=llvm-ar -C libsrc wcc-libs

WCCLD_SRCS:=$(DEBUG_DIR)/wcc-ld.c $(WCC_DIR)/wasm_linker.c \
$(WCC_DIR)/wcc_util.c $(WCC_DIR)/emit_wasm.c $(WCC_DIR)/traverse.c $(WCC_DIR)/traverse_setjmp.c \
$(wildcard $(CC1_FE_DIR)/*.c) \
$(UTIL_DIR)/util.c $(UTIL_DIR)/table.c
$(UTIL_DIR)/util.c $(UTIL_DIR)/table.c $(UTIL_DIR)/archive.c
WCCLD_OBJS:=$(addprefix $(WCC_OBJ_DIR)/,$(notdir $(WCCLD_SRCS:.c=.o)))

wcc-ld: $(WCCLD_OBJS)
Expand All @@ -228,18 +234,6 @@ WCC_LIBC_SRCS:=$(wildcard $(LIBSRC_DIR)/math/*.c) \
$(wildcard $(LIBSRC_DIR)/string/*.c) \
$(wildcard $(LIBSRC_DIR)/_wasm/unistd/*.c)

define GENERATE_INCLUDE_SRCS
(cd $(LIBSRC_DIR); \
for dir in $1; do \
find $${dir} -name '*.c' -exec echo '#include <{}>' \; | sort; \
done)
endef

$(LIBSRC_DIR)/_wasm/crt0.c: $(WCC_CRT0_SRCS)
$(call GENERATE_INCLUDE_SRCS,_wasm/crt0) > $@
$(LIBSRC_DIR)/_wasm/libc.c: $(WCC_LIBC_SRCS)
$(call GENERATE_INCLUDE_SRCS,math misc stdio stdlib string _wasm/unistd) > $@

.PHONY: test-wcc
test-wcc: wcc
$(MAKE) -C tests clean && $(MAKE) -C tests test-wcc
Expand Down Expand Up @@ -281,7 +275,7 @@ wcc-self-hosting: $(WCC_TARGET)cc.wasm
test-wcc-self-hosting:
$(MAKE) -C tests clean && $(MAKE) WCC="$(TARGET_CC)" -C tests test-wcc

$(WCC_TARGET)cc.wasm: $(WCC_SRCS) $(WCC_LIBS) $(WCC_PARENT)
$(WCC_TARGET)cc.wasm: $(WCC_SRCS) wcc $(WCC_PARENT)
$(HOST_WCC) -o $@ $(WCC_CFLAGS) \
-I$(CC1_FE_DIR) -I$(CPP_DIR) -I$(UTIL_DIR) \
$(WCC_SRCS)
Expand All @@ -293,10 +287,7 @@ ASSETS_DIR:=public
.PHONY: assets
assets: $(ASSETS_DIR)/wccfiles.zip

$(WCC_DIR)/www/lib_list.json: $(LIBSRC_DIR)/_wasm/crt0.c $(LIBSRC_DIR)/_wasm/libc.c
npx ts-node tool/update_lib_list.ts --base=./libsrc $^

$(ASSETS_DIR)/wccfiles.zip: $(WCC_DIR)/www/lib_list.json cc.wasm
$(ASSETS_DIR)/wccfiles.zip: $(WCC_DIR)/www/lib_list.json cc.wasm wcc-libs
@mkdir -p $(ASSETS_DIR)
npx ts-node tool/pack_libs.js $(WCC_DIR)/www/lib_list.json $@

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ Compile C to WebAssembly/WASI binary.

[Online demo](https://tyfkda.github.io/xcc/)

#### Requirements

* `llvm-ar`

#### Build

```sh
Expand Down
74 changes: 64 additions & 10 deletions libsrc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@ endif

### Library

CRT0_SRCS:=$(wildcard $(SRC_DIR)/crt0/*.c)
CRT0_DIR:=$(SRC_DIR)/crt0
MATH_DIR:=$(SRC_DIR)/math
MISC_DIR:=$(SRC_DIR)/misc
STDIO_DIR:=$(SRC_DIR)/stdio
STDLIB_DIR:=$(SRC_DIR)/stdlib
STRING_DIR:=$(SRC_DIR)/string
UNISTD_DIR:=$(SRC_DIR)/unistd

CRT0_SRCS:=$(wildcard $(CRT0_DIR)/*.c)

LIBC_SRCS:=\
$(wildcard $(SRC_DIR)/math/*.c) \
$(wildcard $(SRC_DIR)/misc/*.c) \
$(wildcard $(SRC_DIR)/stdio/*.c) \
$(wildcard $(SRC_DIR)/stdlib/*.c) \
$(wildcard $(SRC_DIR)/string/*.c) \
$(wildcard $(SRC_DIR)/unistd/*.c) \
$(wildcard $(MATH_DIR)/*.c) \
$(wildcard $(MISC_DIR)/*.c) \
$(wildcard $(STDIO_DIR)/*.c) \
$(wildcard $(STDLIB_DIR)/*.c) \
$(wildcard $(STRING_DIR)/*.c) \
$(wildcard $(UNISTD_DIR)/*.c) \

CRT0_OBJS:=$(addprefix $(OBJ_DIR)/,$(notdir $(CRT0_SRCS:.c=.o)))
LIBC_OBJS:=$(addprefix $(OBJ_DIR)/,$(notdir $(LIBC_SRCS:.c=.o)))
Expand All @@ -33,7 +41,7 @@ libs: $(LIBS)

.PHONY: clean
clean: clean-test
rm -rf $(OBJ_DIR) $(LIB_DIR)
rm -rf $(OBJ_DIR) $(LIB_DIR) $(WCC_OBJ_DIR) $(WCC_LIB_DIR)

$(LIB_DIR)/crt0.a: $(CRT0_OBJS)
@mkdir -p $(LIB_DIR)
Expand All @@ -43,9 +51,13 @@ $(LIB_DIR)/libc.a: $(LIBC_OBJS)
@mkdir -p $(LIB_DIR)
$(AR) r $@ $^

$(OBJ_DIR)/%.o: $(SRC_DIR)/**/%.c
define DEFINE_OBJ_TARGET
$(OBJ_DIR)/%.o: $(1)/%.c $(PARENT_DEPS)
@mkdir -p $(OBJ_DIR)
$(CC) -c -o $@ -Werror -ffreestanding $(CFLAGS) $<
$(CC) -c -o $$@ -Werror -ffreestanding $(CFLAGS) $$<
endef
LIB_SRC_DIRS:=$(CRT0_DIR) $(MATH_DIR) $(MISC_DIR) $(STDIO_DIR) $(STDLIB_DIR) $(STRING_DIR) $(UNISTD_DIR)
$(foreach D, $(LIB_SRC_DIRS), $(eval $(call DEFINE_OBJ_TARGET,$(D))))

### Test

Expand Down Expand Up @@ -81,6 +93,48 @@ $(foreach D, $(TESTS), $(eval $(call DEFINE_TEST_TARGET,$(D))))

WCC:=../wcc
WCC_TESTS:=$(TESTS)
WCC_OBJ_DIR:=./obj/wcc
WCC_LIB_DIR:=../lib

WCC_LIBS:=$(WCC_LIB_DIR)/wcrt0.a $(WCC_LIB_DIR)/wlibc.a

WASM_UNISTD_DIR:=$(SRC_DIR)/_wasm/unistd
WASM_CRT0_DIR:=$(SRC_DIR)/_wasm/crt0

WCC_CRT0_SRCS:=$(wildcard $(WASM_CRT0_DIR)/*.c)

WCC_LIBC_SRCS:=\
$(wildcard $(MATH_DIR)/*.c) \
$(wildcard $(MISC_DIR)/*.c) \
$(wildcard $(STDIO_DIR)/*.c) \
$(wildcard $(STDLIB_DIR)/*.c) \
$(wildcard $(STRING_DIR)/*.c) \
$(wildcard $(WASM_UNISTD_DIR)/*.c) \

WCC_CRT0_OBJS:=$(addprefix $(WCC_OBJ_DIR)/,$(notdir $(WCC_CRT0_SRCS:.c=.o)))
WCC_LIBC_OBJS:=$(addprefix $(WCC_OBJ_DIR)/,$(notdir $(WCC_LIBC_SRCS:.c=.o)))

echo-wcc-libc-objs:
@echo $(WCC_LIBC_OBJS)

$(WCC_LIB_DIR)/wcrt0.a: $(WCC_CRT0_OBJS)
@mkdir -p $(WCC_LIB_DIR)
$(AR) r $@ $^

$(WCC_LIB_DIR)/wlibc.a: $(WCC_LIBC_OBJS)
@mkdir -p $(WCC_LIB_DIR)
$(AR) r $@ $^

define DEFINE_WCCOBJ_TARGET
$(WCC_OBJ_DIR)/%.o: $(1)/%.c
@mkdir -p $(WCC_OBJ_DIR)
$(CC) -c -o $$@ -Werror $(WCC_CFLAGS) $$<
endef
WCC_SRC_DIRS:=$(MATH_DIR) $(MISC_DIR) $(STDIO_DIR) $(STDLIB_DIR) $(STRING_DIR) $(WASM_UNISTD_DIR) $(WASM_CRT0_DIR)
$(foreach D, $(WCC_SRC_DIRS), $(eval $(call DEFINE_WCCOBJ_TARGET,$(D))))

.PHONY: wcc-libs
wcc-libs: $(WCC_LIBS)

.PHONY: test-wcc
test-wcc: $(foreach D, $(WCC_TESTS), $(addprefix test-wcc-,$(D)))
Expand Down
1 change: 0 additions & 1 deletion src/_debug/wcc-ld.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "util.h"

static void init(void) {
out_type = OutExecutable;
functypes = new_vector();
tags = new_vector();
table_init(&indirect_function_table);
Expand Down
4 changes: 1 addition & 3 deletions src/ld/ld.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ typedef struct {
} LinkEditor;

void ld_init(LinkEditor *ld, int nfiles) {
size_t size = sizeof(*ld->files) * nfiles;
ld->files = malloc_or_die(size);
memset(ld->files, 0x00, size);
ld->files = calloc_or_die(sizeof(*ld->files) * nfiles);
ld->nfiles = nfiles;

for (int secno = 0; secno < SEC_BSS + 1; ++secno) {
Expand Down
Loading

0 comments on commit 4a3fb57

Please sign in to comment.