So, I recently run into this bug in the compiler for my programming language that outputs WebAssembly Text Format. Here is what the output looked like:
I fixed it by changing in my compiler:
: context.structureSizes.at(getType(context))) +
")\n" +
std::string(
convertToInteger32(children.at(0), context).indentBy(3)) +
"\n\t)\n)",
AssemblyCode::AssemblyType::i32);
to this:
: context.structureSizes.at(getType(context))) +
")\n" +
std::string(
convertToInteger32(children.at(0), context)
.indentBy(
2)) // Indenting by 2 instead of by 3 because of this:
// https://github.com/FlatAssembler/AECforWebAssembly/issues/24
+ "\n\t)\n)",
AssemblyCode::AssemblyType::i32);
But there are almost certainly more such bugs.
So, how do other compilers that output WebAssembly Text Format make sure the s-expressions are properly indented? Using wat2wasm and then wasm2wat from the WebAssembly Binary Toolkit won't do the trick for two reasons:
- We will lose the comments.
- We will lose the data about which instructions are put into an S-expression and which ones are put linearly, one after another.
So, is there a better way?
