Stream: general

Topic: wast Instruction span


view this post on Zulip William (Apr 26 2024 at 16:45):

Hi all, I'm doing a university project which is to create a WebAssembly IDE.

I have made a WAST interpreter and am starting the part of the project where I make the debugger (breakpoints, step in, out, over, etc.)

I noticed that unfortunately span information is not present for instructions or expressions, so I'm finding it difficult to devise a way to map lines of WAST source code to parsed instructions that my interpreter consumes.

Does anyone have any advice that might point me in the right direction? I don't mind editing the wast crate internally just for this project, if that is how it must be done.

Many thanks all

view this post on Zulip fitzgen (he/him) (Apr 26 2024 at 17:01):

neat project!

I think adding span information to the wast crate's AST should be fine in principle, the details might be a bit fiddly

@Alex Crichton is the original author of that crate, knows it best, and would have opinions/ideas here, but he is also on a flight right now, so probably won't get back to you until later today or maybe monday

view this post on Zulip fitzgen (he/him) (Apr 26 2024 at 17:03):

it might make sense (from a minimal-churn for downstream consumers, ease of landing perspective) to keep these spans out of the Instruction enum and in a side table inside Expression, similar to the branch hints

view this post on Zulip fitzgen (he/him) (Apr 26 2024 at 17:04):

we could put a span in each variant of the Instruction enum but that is going to be a lot of ocean boiling

view this post on Zulip William (Apr 26 2024 at 17:15):

thanks for your response :)

Yes you would be right in saying the details are a bit fiddly -- from my layman's perspective, it is not trivial to implement without better knowledge of the specific logic that goes into parsing the instructions unfortunately.

And yes adding a span directly to the Instruction would be difficult and of course a breaking change. It would definitely be some busywork to sort out both on the implementation side and the user's side :(

Time is not on my side here; my deadline is in 2 weeks (stupid I know!) so even a jank or incomplete solution would probably suit me OK for now. Hopefully Alex can shine some light on those fiddly details so I can whip something up for internal use :grinning_face_with_smiling_eyes:

view this post on Zulip fitzgen (he/him) (Apr 26 2024 at 17:22):

if you need something sooner rather than later, I'd just fork wast and add the spans directly to the Instruction enum, since you won't have to worry about fixing up all the downstream users, just your own code

view this post on Zulip fitzgen (he/him) (Apr 26 2024 at 17:24):

https://github.com/bytecodealliance/wasm-tools/blob/7589b29dd9dc973ae4380c54039c1d380fc1e566/crates/wast/src/core/expr.rs#L134 and https://github.com/bytecodealliance/wasm-tools/blob/7589b29dd9dc973ae4380c54039c1d380fc1e566/crates/wast/src/core/expr.rs#L343 are the general area of the code you'll need to tweak

CLI and Rust libraries for low-level manipulation of WebAssembly modules - bytecodealliance/wasm-tools
CLI and Rust libraries for low-level manipulation of WebAssembly modules - bytecodealliance/wasm-tools

view this post on Zulip William (Apr 26 2024 at 17:26):

yeah, that is probably the wise thing to do at this stage. I will have to figure out how the logic behind parenthesis parsing works in relation to parsing instructions and therefore figuring out the relevant spans. thank you for your prompt advice :)

view this post on Zulip fitzgen (he/him) (Apr 26 2024 at 17:28):

I suspect the parser has a method to just grab the current span at any given moment

view this post on Zulip fitzgen (he/him) (Apr 26 2024 at 17:28):

so you hopefully shouldn't need to do any relative offset kind of thing to compute the span for a particular instruction in the middle of parsing


Last updated: Oct 23 2024 at 20:03 UTC