Skip to content

Commit 8aecd27

Browse files
committed
Move source files for wasm
1 parent 739691e commit 8aecd27

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+434
-479
lines changed

wasm/.babelrc .babelrc

File renamed without changes.

wasm/.eslintrc.js .eslintrc.js

File renamed without changes.

.github/workflows/deploywcc.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ jobs:
1818
with:
1919
node-version: 16.x
2020
- name: Install NPM packages
21-
run: cd wasm && npm ci
21+
run: npm ci
2222
- name: Build release
23-
run: make -C wasm release
23+
run: make release-wcc
2424
- name: Upload artifact
2525
uses: actions/upload-pages-artifact@v1
2626
with:
27-
path: wasm/release
27+
path: release
2828

2929
deploy:
3030
runs-on: ubuntu-latest

.github/workflows/test.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ jobs:
2525
steps:
2626
- uses: actions/checkout@v3
2727
- name: setup
28-
run: cd wasm && npm install
28+
run: npm ci
2929
- name: test
30-
run: make -C wasm test test-gen2
30+
run: make test-wcc test-wcc-gen2

.gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,15 @@ core
3030
/dump_expr*
3131
/dump_ir*
3232
/dump_type*
33+
34+
### wasm
35+
36+
a.wasm
37+
/wcc
38+
/cc.wasm
39+
/gen3cc.wasm
40+
/tests/*.wasm
41+
42+
/public/
43+
/release/
44+
node_modules

Makefile

+88-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ test-all: test test-gen2 diff-gen23
140140
.PHONY: clean
141141
clean:
142142
rm -rf cc1 cpp as ld xcc $(OBJ_DIR) $(LIB_DIR) a.out gen2* gen3* tmp.s \
143-
dump_expr* dump_ir* dump_type*
143+
dump_expr* dump_ir* dump_type* \
144+
wcc cc.wasm a.wasm public release
144145
$(MAKE) -C tests clean
145146

146147
### Library
@@ -197,6 +198,92 @@ self-hosting: $(TARGET)cpp $(TARGET)cc1 $(TARGET)as $(TARGET)ld $(TARGET)xcc
197198
test-self-hosting: self-hosting
198199
$(MAKE) PREFIX=$(TARGET) -C tests clean cc-tests
199200

201+
202+
### Wasm version
203+
204+
WCC_DIR:=src/wcc
205+
206+
WCC_SRCS:=$(wildcard $(WCC_DIR)/*.c) \
207+
$(CC1_DIR)/lexer.c $(CC1_DIR)/type.c $(CC1_DIR)/var.c $(CC1_DIR)/ast.c $(CC1_DIR)/parser.c $(CC1_DIR)/parser_expr.c \
208+
$(CPP_DIR)/preprocessor.c $(CPP_DIR)/pp_parser.c $(CPP_DIR)/macro.c \
209+
$(UTIL_DIR)/util.c $(UTIL_DIR)/table.c
210+
WCC_OBJS:=$(addprefix $(OBJ_DIR)/,$(notdir $(WCC_SRCS:.c=.o)))
211+
212+
$(OBJ_DIR)/%.o: $(WCC_DIR)/%.c
213+
@mkdir -p $(OBJ_DIR)
214+
$(CC) $(CFLAGS) -I$(CPP_DIR) -c -o $@ $<
215+
216+
wcc: $(PARENT_DEPS) $(WCC_OBJS)
217+
$(CC) -o $@ $(WCC_OBJS) $(LDFLAGS)
218+
219+
.PHONY: test-wcc
220+
test-wcc: wcc
221+
$(MAKE) -C tests clean test-wcc
222+
223+
#### Self hosting
224+
225+
.PHONY: wcc-gen2
226+
wcc-gen2: wcc
227+
$(MAKE) HOST_TARGET=wcc HOST_CC="./wcc" WCC_TARGET= wcc-self-hosting
228+
.PHONY: test-wcc-gen2
229+
test-wcc-gen2: wcc-gen2
230+
$(MAKE) TARGET_CC="node ../tool/runwasm.js ../cc.wasm --" test-wcc-self-hosting
231+
232+
.PHONY: wcc-gen3
233+
wcc-gen3: wcc-gen2
234+
$(MAKE) HOST_TARGET=gen2 HOST_CC="node ./tool/runwasm.js ./cc.wasm --" WCC_TARGET=gen3 wcc-self-hosting
235+
236+
.PHONY: wcc-diff-gen23
237+
wcc-diff-gen23: wcc-gen2 wcc-gen3
238+
diff -b cc.wasm gen3cc.wasm
239+
240+
.PHONY: wcc-self-hosting
241+
wcc-self-hosting: $(WCC_TARGET)cc.wasm
242+
243+
.PHONY: test-wcc-self-hosting
244+
test-wcc-self-hosting:
245+
$(MAKE) WCC="$(TARGET_CC)" -C tests test-wcc
246+
247+
WCC_LIBS:=$(LIBSRC_DIR)/_wasm/crt0.c $(LIBSRC_DIR)/_wasm/libc.c
248+
249+
$(WCC_TARGET)cc.wasm: $(WCC_SRCS) $(WCC_LIBS)
250+
$(HOST_CC) -o $@ \
251+
-I$(CC1_DIR) -I$(CPP_DIR) -I$(UTIL_DIR) \
252+
$(WCC_SRCS)
253+
254+
#### www
255+
256+
ASSETS_DIR:=public
257+
258+
.PHONY: assets
259+
assets: wcc-gen2 $(ASSETS_DIR)/cc.wasm $(ASSETS_DIR)/libs.json
260+
261+
$(ASSETS_DIR)/cc.wasm: cc.wasm
262+
mkdir -p $(ASSETS_DIR)
263+
cp cc.wasm $@
264+
265+
$(ASSETS_DIR)/libs.json: $(WCC_DIR)/www/lib_list.json
266+
mkdir -p $(ASSETS_DIR)
267+
node tool/pack_libs.js $(WCC_DIR)/www/lib_list.json > $@
268+
269+
.PHONY: update-wcc-lib
270+
update-wcc-lib:
271+
find libsrc/* -type d \
272+
| egrep -v \(crt0\|math\|_wasm\) \
273+
| while read d; do \
274+
ls -1 $$d/*.c; \
275+
done \
276+
| sed -e 's/libsrc/../g' \
277+
| sort \
278+
| awk '{print "#include \"" $$0 "\""}' \
279+
> libsrc/_wasm/libc.c
280+
npx ts-node tool/update_lib_list.ts
281+
282+
.PHONY: release-wcc
283+
release-wcc: assets
284+
npm run release
285+
286+
200287
### Debug
201288

202289
DEBUG_CFLAGS:=$(subst -MMD,,$(CFLAGS))

README.md

+59-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,65 @@ C compiler:
7070

7171
### WebAssembly
7272

73-
[WCC](https://github.com/tyfkda/xcc/blob/main/wasm/README.md): Compile C to WebAssembly.
73+
Compile C to WebAssembly.
74+
75+
[Online demo](https://tyfkda.github.io/xcc/)
76+
77+
#### Requirements
78+
79+
#### Build
80+
81+
Requirements:
82+
83+
* node.js (>=16), npm
84+
85+
Setup:
86+
87+
```sh
88+
$ npm install
89+
```
90+
91+
Build:
92+
93+
```sh
94+
$ make wcc
95+
```
96+
97+
Generated files:
98+
99+
* `wcc`: Compiler entry
100+
101+
#### Usage
102+
103+
Compile:
104+
105+
```sh
106+
$ ./wcc -o hello.wasm examples/hello.c
107+
```
108+
109+
Command line options:
110+
111+
* `-o <filename>`: Set output filename (default: `a.wasm`)
112+
* `-I <path>`: Add include path
113+
* `-D <label>(=value)`: Define macro
114+
* `-entry-point=func_name`: Specify entry point (default: _start)
115+
* `-e func_name,...`: Export function names (comma separated)
116+
* `--stack-size=<size>`: Set stack size (default: 8192)
117+
* `-nodefaultlibs`: Ignore libc
118+
* `-nostdlib`: Ignore libc and crt0
119+
* `--verbose`: Output debug information
120+
121+
#### Run
122+
123+
```sh
124+
$ node tool/runwasm.js hello.wasm
125+
Hello, world!
126+
```
127+
128+
#### Missing features
129+
130+
* `goto` statement
131+
* `setjmp`
74132

75133

76134
### Reference

wasm/gulpfile.babel.js gulpfile.babel.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ import del from 'del'
2828
const ROOT_DIR = `${__dirname}`
2929
const DEST_DIR = `${ROOT_DIR}/public`
3030
const ASSETS_DIR = `${DEST_DIR}`
31-
const SRC_TS_DIR = `${ROOT_DIR}/src/www`
31+
const SRC_TS_DIR = `${ROOT_DIR}/src/wcc/www`
3232
const SRC_TS_FILES = `${SRC_TS_DIR}/**/*.ts`
33-
const SRC_HTML_DIR = `${ROOT_DIR}/src/www`
33+
const SRC_HTML_DIR = `${ROOT_DIR}/src/wcc/www`
3434
const SRC_HTML_FILES = `${SRC_HTML_DIR}/*.html`
35-
const SRC_SASS_FILES = `${ROOT_DIR}/src/www/**/*.scss`
35+
const SRC_SASS_FILES = `${ROOT_DIR}/src/wcc/www/**/*.scss`
3636
const SRC_TEST_DIR = `${ROOT_DIR}/test`
3737
const SRC_TEST_FILES = `${SRC_TEST_DIR}/**/*.spec.ts`
3838
const RELEASE_DIR = `${ROOT_DIR}/release`
File renamed without changes.

libsrc/_wasm/libc.c

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include "../misc/basename.c"
2+
#include "../misc/dirname.c"
3+
#include "../misc/errno.c"
4+
#include "../misc/isalnum.c"
5+
#include "../misc/isalpha.c"
6+
#include "../misc/isdigit.c"
7+
#include "../misc/isspace.c"
8+
#include "../misc/isxdigit.c"
9+
#include "../misc/longjmp.c"
10+
#include "../misc/rng_xor64.c"
11+
#include "../misc/setjmp.c"
12+
#include "../misc/tolower.c"
13+
#include "../misc/toupper.c"
14+
#include "../stdio/assert.c"
15+
#include "../stdio/fclose.c"
16+
#include "../stdio/fdopen.c"
17+
#include "../stdio/fflush.c"
18+
#include "../stdio/fgetc.c"
19+
#include "../stdio/fgets.c"
20+
#include "../stdio/fileno.c"
21+
#include "../stdio/fopen.c"
22+
#include "../stdio/fprintf.c"
23+
#include "../stdio/fputc.c"
24+
#include "../stdio/fputs.c"
25+
#include "../stdio/fread.c"
26+
#include "../stdio/fseek.c"
27+
#include "../stdio/ftell.c"
28+
#include "../stdio/fwrite.c"
29+
#include "../stdio/getc.c"
30+
#include "../stdio/getchar.c"
31+
#include "../stdio/getline.c"
32+
#include "../stdio/perror.c"
33+
#include "../stdio/printf.c"
34+
#include "../stdio/puts.c"
35+
#include "../stdio/remove.c"
36+
#include "../stdio/snprintf.c"
37+
#include "../stdio/sprintf.c"
38+
#include "../stdio/stdin.c"
39+
#include "../stdio/tmpfile.c"
40+
#include "../stdio/vfprintf.c"
41+
#include "../stdio/vprintf.c"
42+
#include "../stdio/vsnprintf.c"
43+
#include "../stdio/vsprintf.c"
44+
#include "../stdlib/atoi.c"
45+
#include "../stdlib/calloc.c"
46+
#include "../stdlib/drand48.c"
47+
#include "../stdlib/erand48.c"
48+
#include "../stdlib/exit.c"
49+
#include "../stdlib/malloc.c"
50+
#include "../stdlib/mkstemp.c"
51+
#include "../stdlib/mkstemps.c"
52+
#include "../stdlib/qsort.c"
53+
#include "../stdlib/realloc.c"
54+
#include "../stdlib/strtod.c"
55+
#include "../stdlib/strtol.c"
56+
#include "../stdlib/strtoul.c"
57+
#include "../string/memchr.c"
58+
#include "../string/memcmp.c"
59+
#include "../string/memcpy.c"
60+
#include "../string/memmove.c"
61+
#include "../string/memset.c"
62+
#include "../string/strcasecmp.c"
63+
#include "../string/strcat.c"
64+
#include "../string/strchr.c"
65+
#include "../string/strcmp.c"
66+
#include "../string/strcpy.c"
67+
#include "../string/strdup.c"
68+
#include "../string/strlen.c"
69+
#include "../string/strncasecmp.c"
70+
#include "../string/strncat.c"
71+
#include "../string/strncmp.c"
72+
#include "../string/strncpy.c"
73+
#include "../string/strndup.c"
74+
#include "../string/strrchr.c"
75+
#include "../string/strstr.c"
76+
#include "../unistd/brk.c"
77+
#include "../unistd/chmod.c"
78+
#include "../unistd/close.c"
79+
#include "../unistd/dup.c"
80+
#include "../unistd/execve.c"
81+
#include "../unistd/execvp.c"
82+
#include "../unistd/fork.c"
83+
#include "../unistd/getcwd.c"
84+
#include "../unistd/getrandom.c"
85+
#include "../unistd/ioctl.c"
86+
#include "../unistd/isatty.c"
87+
#include "../unistd/kill.c"
88+
#include "../unistd/lseek.c"
89+
#include "../unistd/open.c"
90+
#include "../unistd/pipe.c"
91+
#include "../unistd/read.c"
92+
#include "../unistd/unlink.c"
93+
#include "../unistd/wait4.c"
94+
#include "../unistd/waitpid.c"
95+
#include "../unistd/write.c"
File renamed without changes.

wasm/package.json package.json

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

wasm/src/wcc.c src/wcc/wcc.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,6 @@ int main(int argc, char *argv[]) {
902902
define_macro("__ILP32__");
903903
define_macro("__WASM");
904904
add_system_inc_path(cat_path(root, "include"));
905-
add_system_inc_path(cat_path(root, "../include"));
906905

907906
init_compiler();
908907

@@ -1074,7 +1073,7 @@ int main(int argc, char *argv[]) {
10741073

10751074
// if (out_type >= OutExecutable)
10761075
{
1077-
vec_push(lib_paths, cat_path(root, "./lib"));
1076+
vec_push(lib_paths, cat_path(root, "./libsrc/_wasm"));
10781077
if (!nostdlib)
10791078
add_lib(lib_paths, "crt0.c", sources);
10801079
if (!nodefaultlibs && !nostdlib)

wasm/src/wcc.h src/wcc/wcc.h

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

wasm/src/www/index.html src/wcc/www/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<meta name="twitter:site" content="@tyfkda">
88
<meta name="twitter:title" content="C-compiler on a browser">
99
<meta name="twitter:description" content="You can edit, compile and run C code on your browser!">
10-
<meta name="twitter:image" content="https://raw.githubusercontent.com/tyfkda/xcc/main/wasm/wcc.png">
10+
<meta name="twitter:image" content="https://raw.githubusercontent.com/tyfkda/xcc/main/wcc.png">
1111
<meta name="twitter:player:height" content="512">
1212
<meta name="twitter:player:width" content="400">
1313
<title>C-compiler on WASM</title>

0 commit comments

Comments
 (0)