Skip to content

Commit

Permalink
document wasm features
Browse files Browse the repository at this point in the history
  • Loading branch information
arnetheduck committed Oct 30, 2023
1 parent 55b6c0c commit 5d04d52
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,11 @@ included in the compilation with `{.compile.}` - this works both with `.S` and

### wasm32 support

`wasm32` support is still very bare-bones, so you will need to do a bit of
tinkering to get it to work.
Use `--cpu:wasm32 --os:standalone --gc:none` to compile Nim to (barebones) WASM.

Presently, the `wasm32-unknown-unknown` target is mapped to `--os:standalone`
and `--cpu:wasm32` - this choice represents a very raw `wasm` engine with 32-bit
little-endian integers and pointers - in the future, the `nim` standard library
and `system.nim` will need to be updated to support WASM system interfaces like
emscripten or WASI.
You will need to provide a runtime (ie WASI) and use manual memory allocation as
the garbage collector hasn't yet been ported to WASM and the Nim standard
library lacks WASM / WASI support.

To compile wasm files, you will thus need a `panicoverride.nim` - a minimal
example looks like this and discards any errors:
Expand All @@ -291,6 +288,17 @@ nlvm c --cpu:wasm32 --os:standalone --gc:none --passl:--no-entry myfile.nim
wasm2wat -l myfile.wasm
```

Most WASM-compile code ends up needing WASM [extensions](https://webassembly.org/roadmap/) -
in particular, the bulk memory extension is needed to process data.

Extensions are enabled by passing `--passc:-mattr=+feature,+feature2`, for example:

```sh
nlvm c --cpu:wasm32 --os:standalone --gc:none --passl:--no-entry --passc:-mattr=+bulk-memory
```

Passing `--passc:-mattr=help` will print available features (only works while compiling, for now!)

# Random notes

* Upstream is pinned using a submodule - nlvm relies heavily on internals
Expand Down
2 changes: 1 addition & 1 deletion nlvm-lib/nlvmbase-wasm32-Standalone.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; base presently used for wasm32!

target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
target datalayout = "e-m:e-p:32:32-p10:8:8-p20:8:8-i64:64-n32:64-S128-ni:1:10:20"
target triple = "wasm32-unknown-unknown"

0 comments on commit 5d04d52

Please sign in to comment.