Skip to content

Commit 96bf6bd

Browse files
committed
wip
1 parent a9cefc9 commit 96bf6bd

24 files changed

+334
-159
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,6 @@ all_spec
1919

2020
# Test suite object files
2121
*.o
22+
*.wat
23+
*.wast
2224
*.wasm

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ $(LIB_CRYSTAL_TARGET): $(LIB_CRYSTAL_OBJS)
131131
clean: clean_crystal ## Clean up built directories and files
132132
rm -rf $(LLVM_EXT_OBJ)
133133
rm -rf $(LIB_CRYSTAL_OBJS) $(LIB_CRYSTAL_TARGET)
134+
rm -rf *.o *.wasm *.wat
134135

135136
.PHONY: clean_crystal
136137
clean_crystal: ## Clean up crystal built files

spec/generate_wasm_spec.sh

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ set +x
3434
SPEC_SUITE=${1:-std}
3535
RUNNER=${RUNNER:-wavm run --abi=wasi --enable all-proposed}
3636
WASI_SDK_PATH=${WASI_SDK_PATH:-"$HOME/toolchains/wasi-sdk-11.0"}
37+
WASM_DIS=${WASM_DIS:-"$HOME/toolchains/bin/wasm-dis"}
38+
WASM_OPT=${WASM_OPT:-"$HOME/toolchains/bin/wasm-opt -O --asyncify"}
3739

3840
INITIAL_MEMORY=${INITIAL_MEMORY:-16777216}
3941
MAX_MEMORY=${MAX_MEMORY:-2147483648}
@@ -42,7 +44,9 @@ STACK_SIZE=${STACK_SIZE:-5242880}
4244
export CC="$WASI_SDK_PATH/bin/clang --sysroot=$WASI_SDK_PATH/share/wasi-sysroot"
4345
export LIBRARY_PATH="$WASI_SDK_PATH/share/wasi-sysroot/lib/wasm32-wasi"
4446
export CRYSTAL_LIBRARY_PATH="$HOME/toolchains/crystal-wasm-libs/targets/wasm32-wasi"
45-
export WASM_LDFLAGS="-Wl,--allow-undefined \
47+
# -Wl,--export-all \
48+
export WASM_LDFLAGS="-lwasi-emulated-mman -lwasi-emulated-signal \
49+
-Wl,--allow-undefined \
4650
-Wl,--initial-memory=${INITIAL_MEMORY} \
4751
-Wl,--max-memory=${MAX_MEMORY} \
4852
-Wl,-z -Wl,stack-size=${STACK_SIZE} \
@@ -75,7 +79,20 @@ for spec in $(find "spec/$SPEC_SUITE" -type f -iname "*_spec.cr" | sort); do
7579
continue
7680
fi
7781

78-
binary_path="./$(echo "$linker_command" | grep -oP '(?<=-o\s)(.*)(?=\.wasm)').wasm"
82+
program_name=$(echo "$linker_command" | grep -oP '(?<=-o\s)(.*)(?=\.wasm)')
83+
preopt_wasm_path="./$program_name.wasm"
84+
preopt_wat_path="./$program_name.wat"
85+
86+
if ! $WASM_DIS "$preopt_wasm_path" > "$preopt_wat_path"; then
87+
echo "# $require (failed to run wasm-dis)"
88+
continue
89+
fi
90+
91+
binary_path=$preopt_wasm_path
92+
if ! $WASM_OPT -o "$binary_path" "$preopt_wat_path"; then
93+
echo "# $require (failed to run wasm-opt)"
94+
continue
95+
fi
7996

8097
$RUNNER "$binary_path" > /dev/null; exit=$?
8198

spec/run_wasm_spec.sh

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,28 @@
33
set -euo pipefail
44

55
WASI_SDK_PATH=${WASI_SDK_PATH:-"$HOME/toolchains/wasi-sdk-11.0"}
6+
WASM_DIS=${WASM_DIS:-"$HOME/toolchains/bin/wasm-dis"}
7+
WASM_OPT=${WASM_OPT:-"$HOME/toolchains/bin/wasm-opt -g --asyncify"}
68
INITIAL_MEMORY=${INITIAL_MEMORY:-16777216}
79
MAX_MEMORY=${MAX_MEMORY:-2147483648}
810
STACK_SIZE=${STACK_SIZE:-5242880}
911

1012
export CC="$WASI_SDK_PATH/bin/clang --sysroot=$WASI_SDK_PATH/share/wasi-sysroot"
1113
export LIBRARY_PATH="$WASI_SDK_PATH/share/wasi-sysroot/lib/wasm32-wasi"
1214
export CRYSTAL_LIBRARY_PATH="$HOME/toolchains/crystal-wasm-libs/targets/wasm32-wasi"
13-
export WASM_LDFLAGS="-Wl,--allow-undefined \
15+
export WASM_LDFLAGS="-lwasi-emulated-mman -lwasi-emulated-signal \
16+
-Wl,--allow-undefined \
1417
-Wl,--initial-memory=${INITIAL_MEMORY} \
1518
-Wl,--max-memory=${MAX_MEMORY} \
1619
-Wl,-z -Wl,stack-size=${STACK_SIZE} \
1720
-Wl,--stack-first \
1821
-Wl,--no-threads"
1922

23+
entry=${1:-"spec/std_spec.cr --exclude-warnings spec/std --exclude-warnings spec/compiler"}
24+
2025
linker_command=$(bin/crystal build \
21-
spec/std_spec.cr \
22-
--exclude-warnings spec/std \
23-
--exclude-warnings spec/compiler \
26+
$entry \
27+
-Ddebug_raise \
2428
-Dgc_none \
2529
-Dwithout_openssl \
2630
-Dwithout_zlib \
@@ -30,10 +34,18 @@ linker_command=$(bin/crystal build \
3034
--static \
3135
--error-trace \
3236
--link-flags="$WASM_LDFLAGS")
33-
binary_path="./$(echo "$linker_command" | grep -oP '(?<=-o\s)(.*)(?=\.wasm)').wasm"
37+
38+
program_name=$(echo "$linker_command" | grep -oP '(?<=-o\s)(.*)(?=\.wasm)')
39+
preopt_wasm_path="./$program_name.wasm"
40+
preopt_wat_path="./$program_name.wat"
41+
binary_path=$preopt_wasm_path
3442

3543
eval $linker_command
3644

45+
$WASM_DIS "$preopt_wasm_path" > "$preopt_wat_path"
46+
$WASM_OPT -o "$binary_path" "$preopt_wat_path"
47+
48+
# --wasi-trace=syscalls-with-callstacks \
3749
wavm run \
3850
--abi=wasi \
3951
--enable all-proposed \

src/crystal/fiber_channel.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{% skip_file if flag?(:wasi) %}
12
# :nodoc:
23
#
34
# This struct wraps around a IO pipe to send and receive fibers between

src/crystal/scheduler.cr

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@ require "crystal/system/event_loop"
22
require "crystal/system/print_error"
33
require "./fiber_channel"
44
require "fiber"
5+
require "fiber/context"
6+
require "fiber/context/wasm32"
57
require "crystal/system/thread"
68

9+
{% if flag?(:wasi) %}
10+
lib Asyncify
11+
fun stop_rewind()
12+
end
13+
{% end %}
14+
715
# :nodoc:
816
#
917
# Schedulers are tied to a thread, and must only ever be accessed from within
@@ -120,8 +128,8 @@ class Crystal::Scheduler
120128

121129
private def fatal_resume_error(fiber, message)
122130
Crystal::System.print_error "\nFATAL: #{message}: #{fiber}\n"
123-
{% unless flag?(:win32) %}
124-
# FIXME: Enable when caller is supported on win32
131+
{% unless flag?(:win32) || flag?(:wasi) %}
132+
# FIXME: Enable when caller is supported on win32 and/or wasi
125133
caller.each { |line| Crystal::System.print_error " from #{line}\n" }
126134
{% end %}
127135

@@ -141,6 +149,10 @@ class Crystal::Scheduler
141149
{% end %}
142150

143151
protected def reschedule : Nil
152+
{% if flag?(:wasi) %}
153+
Asyncify.stop_rewind
154+
{% end %}
155+
144156
loop do
145157
if runnable = @lock.sync { @runnables.shift? }
146158
unless runnable == Fiber.current

src/crystal/system/event_loop.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct Crystal::Event
2828
# def add(time_span : Time::Span?) : Nil
2929
end
3030

31-
{% if flag?(:unix) %}
31+
{% if flag?(:unix) || flag?(:wasi) %}
3232
require "./unix/event_loop_libevent"
3333
{% elsif flag?(:win32) %}
3434
require "./win32/event_loop_iocp"

src/crystal/system/fiber.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module Crystal::System::Fiber
99
# def self.main_fiber_stack(stack_bottom : Void*) : Void*
1010
end
1111

12-
{% if flag?(:unix) %}
12+
{% if flag?(:unix) || flag?(:wasi) %}
1313
require "./unix/fiber"
1414
{% elsif flag?(:win32) %}
1515
require "./win32/fiber"

src/crystal/system/thread.cr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ require "./thread_linked_list"
3131
{% if flag?(:unix) %}
3232
require "./unix/pthread"
3333
require "./unix/pthread_condition_variable"
34+
{% elsif flag?(:wasi) %}
35+
require "./wasi/thread"
3436
{% elsif flag?(:win32) %}
3537
require "./win32/thread"
3638
{% else %}

src/crystal/system/thread_mutex.cr

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ end
1919

2020
{% if flag?(:unix) %}
2121
require "./unix/pthread_mutex"
22+
{% elsif flag?(:wasi) %}
23+
require "./wasi/thread_mutex"
2224
{% elsif flag?(:win32) %}
2325
require "./win32/thread_mutex"
24-
{% else %}
26+
{% else%}
2527
{% raise "thread not supported" %}
2628
{% end %}

0 commit comments

Comments
 (0)