Can LLVM optimize for a stack machine backend?

Hi all,

I am developing a high-level language that compiles to bytecode for a stack-based virtual machine. There are only two stacks similar to Forth.

LLVM IR seems register-machine oriented and most backends target register machines. I have seen several attempts to use it for a stack machine backend, such as this, this, and this. What is the state of the art of using LLVM to optimize for a stack machine? Or LLVM is not a good choice for this job?


Hi Xiaohui,

I think the situation didn’t really change with respect to what you found.
In particular I believe the advice from Chris that you already found ( is the best thing you can do right now.


This is a bit out of date, LLVM now has a stack-based target included by default - WebAssembly. It has a “stackifier” implementation (look for files with Stackify in the name inside llvm/lib/Target/WebAssembly). However, stack transformations are in fact somewhat of a pain point, that’s one of the reasons Emscripten (C/C++ to Wasm compiler) uses an additional tool after emitting code using Clang/LLVM, though another popular Wasm toolchain does not.