Back End for a stack based architecture

Hi all,

I was asked to write a C compiler back end for a dated stack based
architecture, i.e. once whose instructions operate only on the top
elements of a stack and doesn't use arguments, something like the JVM.

I looked at some open source compilers (gcc, sdcc, tinyc and LLVM). To
me LLVM seems promising (and I likes C++).

Because I'm new here I need your help to understand if:

1) LLVM could be used to target stack based architecture

2) is possible but LLVM is not the best solution for that kind of
architecture (in this case any hint for the rigth compiler is very



There is nothing stopping you from doing so. Probably the common code
generation infrastructure won't work for you without extra work. I
suspect that you could either write a custom code generator (like the
C backend) which doesn't use the common code generation stuff, or you
could fake it. By that I mean, model your arch as a register based
arch for instruction selection, then do a post pass over it to
stackify it instead of running the normal register allocator.


If you want to get something up and running relatively quickly. You can model the backend after X86 X87 FP math. See, X86FloatingPoint.cpp

Basically add a fake register class and then "stackify" it with a post-allocation pass. Play around with it to see if something like this fits your need.