WebAssembly / wabt

The WebAssembly Binary Toolkit
Apache License 2.0
6.74k stars 688 forks source link

Sourcemap support for wat2wasm? #1210

Open binji opened 4 years ago

binji commented 4 years ago

A discussion about adding sourcemap support came up on twitter. There's an old PR (#432), that could be revived, but we may want to approach this differently now. @dschuff @sbc100 thoughts?

yurydelendik commented 4 years ago

There's an old PR (#432), that could be revived, but we may want to approach this differently now.

Just as a status:

Source maps assumes that only source code line information/mapping is present. But for efficient debugging, the original source types and locals metadata much be present.

battlelinegames commented 4 years ago

There's an old PR (#432), that could be revived, but we may want to approach this differently now.

Just as a status:

  • wasm source maps supported by Firefox and Chrome as they are documented in the Sourcemap WIP
  • binaryen is able to use source map data in wasm-dis, wasm-as and wasm-opt
  • there is no documented way on how to represent source map information in wat/wast file
  • it is possible binaryen can start using DWARF in addition to source maps -- related discussion at WebAssembly/binaryen#2400

Source maps assumes that only source code line information/mapping is present. But for efficient debugging, the original source types and locals metadata much be present.

I've only recently begun looking through the WABT code. There may be something I don't understand here. If that is the case, I apologize for my ignorance. It doesn't look to me like wat2wasm in WABT uses binaryen. My thought is that wat2wasm could generate the sourcemap at the same time as the binary. If a flag is passed into wat2wasm, a structure would need to track the opcode bytes and the line/column they correspond to within the WAT file. It could then generate the sourcemap file from this data when the binary is complete and add the //#sourcemap= tag at the end of the binary that ties the sourcemap file to the wasm binary. I'm working on a prototype modification to WABT right now. Am I way off base on this? Is this less simple a change than I am imagining?

Please let me know if there is some reason it can't be done like this.

Thanks

binji commented 4 years ago

Generally source maps would be used to map from some other source language: e.g. C++, rust, etc. There's no standardized way to specify this information in the WebAssembly text format, so the best you can do when disassembling is include this information as a comment in the .wat file.

Similarly, if you wanted to include arbitrary source map information in the text file, there's no standardized way to do that either. AIUI, binaryen uses a special comment format for this, but IMO it would be better to use an annotation, though those aren't standardized yet.

OTOH, what you're describing is creating a sourcemap for the wat file itself, which shouldn't be too hard. This isn't all that useful for most users, since they won't be writing wat. But I think it still would be a useful feature, as long as that sourcemap information can be overridden when (or if?) we eventually get support for that.

battlelinegames commented 4 years ago

I am talking about a WAT sourcemap because disassembling the WAT code loses variable names and things like that. I know that the majority of users will not be writing code directly in WAT, but I feel like understanding WAT is beneficial for understanding WebAssembly in general. I will work on adding sourcemaps to wat2wasm if no one has any objection to that.

carlsmith commented 4 years ago

@battlelinegames - I'd appreciate sourcemap support, and personally do write WAT code by hand.

There are good reasons for using WAT as a first-class language in educational and hobby contexts. Even though it'll be a small percentage of users that use WAT directly, it may still get a lot of use in absolute terms.

sabine commented 4 years ago

When you're writing a compiler, it could be convenient to use the browser to debug the generated code. Here, having the ability to see all the names that you deliberately placed in the generated .wat file seems convenient.

there is no documented way on how to represent source map information in wat/wast file

Does that mean there are undocumented ways? From my point of view, it would be nice if I could generate .wat (or .wast) files that contain source map information in such a way that a subsequent run of wat2wasm gives me both a .wasm file and a source map.