I went down a deep rabbit hole (and I'm still falling). It started out with the idea of trying to compile #Uxntal into LLVM IR (and maybe WASM). Now I've jumped ships mid-course and changed to C as a target; I will probably, out of sheer perverseness, do Fortran as well. So this is decompilation of a stack-based assembly language into a non-stack-based higher-level language.
It's one of these "how hard can it be?" things.
The answer in short is, it's impossible; but there are degrees of approximation, and they are increasingly difficult. So it's a lot of fun.
That's me being lazy, because I use combined declarations + statements and that is not a valid target.
@wim_v12e I'm aware! :)
I've written some transpilers targeting C for fun; my solution was, more convolutedly, to track whether a label was needed :P
@wim_v12e It's a matter of axioms, really: do you intend for the output of your transpiler to be machine code that happens to be valid input to a C compiler, or to be C language code?
If the former, the "lazy" approach is actually the *correct* one. You shouldn't optimize for human readability for the C, because it's basically trash. Its only purpose is to be lowered to QBE/GIMPLE/LLVM/SDIR/etc and from there to machine code, which will be more readable than the C anyways.
@pixelherodev I prefer the former, that is why I'll probably go to LLVM IR directly (but after I've done Fortran ^_^).
Fosstodon is an English speaking Mastodon instance that is open to anyone who is interested in technology; particularly free & open source software.