I've made a compiler for my programming language that outputs WebAssembly Text Format, and it uses the wat2wasm assembler from WebAssembly Binary Toolkit to convert it to WebAssembly. I am interested, can I somehow convert WebAssembly to LLVM in order to make native executables, or do I need to write a new compiler in order to target LLVM?

As far as I know, WebAssembly corresponds very closely to LLVM, so it should be possible to convert WebAssembly to LLVM. LLVM supports everything WebAssembly does plus a lot more (goto statements...). Or am I wrong about that?

  • 4
    $\begingroup$ Sure it's possible! Might not be easy though. (I'm VTCing this question due to lacking objective design criterion. Of course it's possible to do this, but we can't help you unless you're having a specific problem doing it.) $\endgroup$
    – Ginger
    Commented Oct 6, 2023 at 14:30
  • 3
    $\begingroup$ If the question is reopened, it should be rephrased to LLVM IR, because that's what it is about. $\endgroup$
    – feldentm
    Commented Oct 6, 2023 at 16:28
  • 2
    $\begingroup$ A more objective question could be something like, "does WebAssembly correspond to a subset of LLVM IR?" or "does WebAssembly have any instructions which don't have analogues in LLVM IR?" $\endgroup$
    – kaya3
    Commented Oct 6, 2023 at 16:46
  • $\begingroup$ "[...] or do I need to write a new compiler in order to target LLVM?" Usually you wouldn't write an entirely new compiler. You would just have one compiler that, internally, has two backends: One that produces WebAssembly and one that produces LLVM IR. This is the approach I would suggest, rather than trying to convert between the two. The multiple backend approach solves the problem directly. And you also wouldn't need to worry if your WebAsm -> LLVM IR translation is correct (which actually could be a pretty major concern, otherwise)! $\endgroup$ Commented Oct 7, 2023 at 16:03


Browse other questions tagged .