from Hacker News

Ask HN: Can LLVM “compile” back to programming language?

by high_byte on 2/1/23, 6:07 PM with 5 comments

I just had a funny idea to create a "super language" that can be "compiled" to other high level languages from the same code; e.g. write a module once and create versions of it in different languages from the same "source of truth", pun intended.

then I thought that this actually already exists - LLVM! I wonder how easily would it be to create various frontends, but instead of x64, arm, etc. you would have targets like js, py, etc.

would LLVM be suitable for something like this? now imagine instead of writing code in one "super language", you could compile e.g. JS to LLVM and LLVM to Python. wouldn't it be awesome?

  • by HybridCurve on 2/1/23, 10:25 PM

    What you're talking about is called a Transpiler and there are are several instances of them. However, they typically perform a one-to-one translation are not general enough to translate to many languages. The overall issue with this is that certain higher level abstract operations in one language (perl hashes for example) might be easily unrolled and translated to C, but the reverse is not true. Compiling works best from higher to lower levels.
  • by tuckerpo on 2/1/23, 7:11 PM

    Haxe [1] does this.

    [1] https://haxe.org/

  • by ksherlock on 2/1/23, 8:22 PM

    There used to be a C back-end for LLVM ("cbe"). It was removed in 3.1 (circa 2012; "The C backend has been removed. It had numerous problems, to the point of not being able to compile any nontrivial program.").
  • by mikewarot on 2/1/23, 9:32 PM

    The big problem I've seen is that C and other languages that make heavy use of macros might be almost impossible to unwind to source code again.

    Pascal, Lisp, Forth might be more reasonable targets for something like this.