|
| 1 | +# Extended Constant Expressions |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +This page describes a proposal for extending constant expressions in |
| 6 | +WebAssembly. The current [spec][] for constant expressions is fairly limited, |
| 7 | +and was always intended to be extended. |
| 8 | + |
| 9 | +This proposal adds new instructions to the list of *constant instructions* that |
| 10 | +can be used in *constant expressions*. |
| 11 | + |
| 12 | +An overview of this proposal was presented at the 01-19-20 CG meeting along with |
| 13 | +a brief [presentation][]. This issue was originally discusses in |
| 14 | +https://github.com/WebAssembly/design/issues/1392. |
| 15 | + |
| 16 | +### Motivation |
| 17 | + |
| 18 | +The primay/initial motivation comes from LLVM where we could benefit from using |
| 19 | +integer addition in both global initializers and in segment initializers. Both |
| 20 | +of these use cases stem from dynamic linking, which is currently |
| 21 | +[experimental][abi]. |
| 22 | + |
| 23 | +1. With dynamic linking the data segments are relative to a global import called |
| 24 | + `__memory_base` which is supplied by the dynamic linker. We currently have |
| 25 | + to have [combine][] all our memory segments into one because there is no way |
| 26 | + to do `__memory_base + CONST_OFFSET` in a segment initilizer. |
| 27 | + |
| 28 | +2. The linker currently has to generate [dynamic relocations][reloc] for certain |
| 29 | + WebAssembly globals because its currently not possible to initialize a global |
| 30 | + with a value of `__memory_base + CONST_OFFSET`. Specifically, this happens |
| 31 | + when the static linker decides that a given symbol is local to the currently |
| 32 | + module. In this case, rather than importing a global it creates a new |
| 33 | + global which points insides the a data segment (i.e. it's value is an offset |
| 34 | + from `__memory_base` or `__table_base` which are themselves imported). |
| 35 | + |
| 36 | +## New Instructions |
| 37 | + |
| 38 | +This proposal adds the following new instructions to the list of valid constant |
| 39 | +instruction: |
| 40 | + |
| 41 | + - `i32.add` |
| 42 | + - `i32.sub` |
| 43 | + - `i64.add` |
| 44 | + - `i64.sub` |
| 45 | + - `i32.mul` |
| 46 | + - `i64.mul` |
| 47 | + |
| 48 | +[spec]: https://webassembly.github.io/spec/core/valid/instructions.html#constant-expressions |
| 49 | +[presentation]: https://docs.google.com/presentation/d/1sM9mJJ6iM7D8324ipYxot91hSKnWCtB8jX4Kh3bde5E |
| 50 | +[abi]: https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md |
| 51 | +[combine]: https://github.com/llvm/llvm-project/blob/5f9be2c3e37c0428ba56876dd84af04b8d9d8915/lld/wasm/Writer.cpp#L868 |
| 52 | +[reloc]: https://github.com/llvm/llvm-project/blob/5f9be2c3e37c0428ba56876dd84af04b8d9d8915/lld/wasm/SyntheticSections.cpp#L311 |
0 commit comments