|
| 1 | +# WebAssembly support in Swift |
| 2 | + |
| 3 | +WebAssembly is a platform that significantly differs from hardware platforms that Swift already supports. |
| 4 | +While it's a virtual machine, there are considerations to be taken into account when targeting it: |
| 5 | + |
| 6 | +* WebAssembly is still at an early stage, so many features you'd be expect from other platforms are not |
| 7 | +available yet, specifically: |
| 8 | + 1. `wasm64` variant is not specified yet, only the 32-bit `wasm32` variant is supported in WebAssembly |
| 9 | + hosts such as browsers. |
| 10 | + 2. While a preview of multi-threading and atomics is available in some browsers and stand-alone |
| 11 | + WebAssembly hosts, [the corresponding proposal](https://github.com/WebAssembly/threads/) haven't |
| 12 | + formally reached the implementation phase yet. |
| 13 | + 3. Dynamic linking is not formally specified and tooling for it is not available yet. |
| 14 | +* Binary size is a high priority requirement. Since WebAssembly payloads are usually served in browsers, |
| 15 | +one wouldn't want end users to download multi-megabyte binaries. |
| 16 | + |
| 17 | +Nevertheless, an early implementation of the WebAssembly target is available [in a separate |
| 18 | +fork](https://github.com/SwiftWasm). Here we're describing some decisions that were made while developing |
| 19 | +the implementation. |
| 20 | + |
| 21 | +## Relative Pointers |
| 22 | + |
| 23 | +Relative pointers are used in Swift runtime, but currently it's not feasible to use them for the WebAssembly |
| 24 | +target due to the design of WebAssembly and lack of LLVM support. If LLVM supported subtraction relocation |
| 25 | +type on WebAssembly like `R_X86_64_PC32` or `X86_64_RELOC_SUBTRACTOR`, this issue can be solved easily. |
| 26 | + |
| 27 | +Since `R_X86_64_PC32` and `X86_64_RELOC_SUBTRACTOR` are mainly used to generate PIC but WebAssembly doesn't |
| 28 | +require PIC because it doesn't support dynamic linking. In addition, the memory space also begins at 0, so |
| 29 | +it's unnecessary to relocate at load time. All absolute addresses can be embedded in wasm binary file directly. |
0 commit comments