cfallin / waffle

Apache License 2.0
56 stars 5 forks source link

Support creating function declarations in arbitrary order #3

Closed bjorn3 closed 6 months ago

bjorn3 commented 7 months ago

Currently all imports have to come first. This significantly complicates using waffle as compiler backend as for that the natural declaration order is to first declare the function itself, then while translating the function body declare the imports and finally define the function. This mixes imports and definitions. Having waffle reorder all function declarations to put imports first either while emitting the wasm module or as a pass before emitting the wasm module would make this a lot easier.

cfallin commented 6 months ago

While I agree this would be a nice dimension of flexibility to have from the producer side, one of the design goals of waffle was to keep the IR as close to Wasm as possible; having the function index space match exactly between the Module and the Wasm (as ingested, and as produced) is important for some applications where we interpret function pointers (e.g. weval). While it's not technically impossible to introduce a level of indirection, I'd prefer to keep the current design, I think. The pass you mention should be possible to build in a layer above waffle as well; or, the producer could accumulate required imports in a separate imports table as it generates its internal IR (or parses its input language, or ...), and dump that imports table to the waffle Module before the functions.