This page describes a proposal for extending constant expressions in WebAssembly. The current spec for constant expressions is fairly limited, and was always intended to be extended.
This proposal adds new instructions to the list of constant instructions that can be used in constant expressions.
An overview of this proposal was presented at the 01-19-20 CG meeting along with a brief presentation. This issue was originally discusses in WebAssembly/design#1392.
The primay/initial motivation comes from LLVM where we could benefit from using integer addition in both global initializers and in segment initializers. Both of these use cases stem from dynamic linking, which is currently experimental.
-
With dynamic linking the data segments are relative to a global import called
__memory_basewhich is supplied by the dynamic linker. We currently have to have combine all our memory segments into one because there is no way to do__memory_base + CONST_OFFSETin a segment initilizer. -
The linker currently has to generate dynamic relocations for certain WebAssembly globals because its currently not possible to initialize a global with a value of
__memory_base + CONST_OFFSET. Specifically, this happens when the static linker decides that a given symbol is local to the currently module. In this case, rather than importing a global it creates a new global which points insides the a data segment (i.e. it's value is an offset from__memory_baseor__table_basewhich are themselves imported).
This proposal adds the following new instructions to the list of valid constant instruction:
i32.addi32.subi32.muli64.addi64.subi64.mul