webassemblyjs icon indicating copy to clipboard operation
webassemblyjs copied to clipboard

Nested instruction representation in AST

Open maurobringolf opened this issue 6 years ago • 5 comments

Our AST representation has different ways of representing nested instruction

  • node.args: https://github.com/xtuc/webassemblyjs/blob/master/packages/wast-parser/test/fixtures/return/expected.json#L18

  • node.instr: https://github.com/xtuc/webassemblyjs/blob/master/packages/wast-parser/test/fixtures/instruction/nested-block/expected.json#L20

  • node.instrArgs: https://github.com/xtuc/webassemblyjs/blob/d73d782786a02d97b21dd9285e20ada9911c219c/packages/wast-parser/test/fixtures/wast-call-instruction/expected.json#L11

This makes code working with them (in my case the type checker) unnecessarily complicated. Can we unify the representation and always use the same field? Which one?

maurobringolf avatar Apr 04 '18 07:04 maurobringolf

Following our discussion on Slack. I would suggest adding a transformation that flatten the AST before the Module compilation (like the identifier ⟶ index transformation).

That would allow different representation for printing and interpreting, which means that we don't have the original source during interpretation and error message could become confusing. We can fix it later using sourcemaps or something.

xtuc avatar Apr 04 '18 07:04 xtuc

I'd vote for args

ColinEberhardt avatar Apr 04 '18 07:04 ColinEberhardt

args makes sense to me, although there is some overlap with arguments that are not instructions but literals (indices, numberliterals). These are currently also stored under args, in i32.const 0 for example. Maybe worth separating the two

maurobringolf avatar Apr 04 '18 07:04 maurobringolf

Good point - I like how it is documented here:

https://github.com/sunfishcode/wasm-reference-manual/blob/master/WebAssembly.md#constant

In this case, i32.const 0, the zero is an immediate, rather than an argument. It is not part of the signature of the const instruction.

ColinEberhardt avatar Apr 04 '18 08:04 ColinEberhardt

Basically, I think args should be used for anything that can be written in a nested form, i.e.

(f64.const 0)
(f64.abs)

can be nested as ...

(f64.abs (f64.const 0))

which reflects the fact that the signature for abs https://github.com/sunfishcode/wasm-reference-manual/blob/master/WebAssembly.md#floating-point-absolute-value is (f64) : (f64)

ColinEberhardt avatar Apr 04 '18 08:04 ColinEberhardt