[ Implementing Stack Layout in LLVM Backend ]
I'm trying to implement a Backend for LLVM. Right now I'm running into problems with stack frame lowering.
I need to implement the following stack layout: When a function gets called I need to put a "Return Symbol"(as the target system can only jump to absolute adresses) and "Offset" onto the stack followed by all the function arguments. The stack alignement is 1 Byte and the stack has to grow up.
Stack Layout before Call:
RetSymb <- SP Offset Arguments Local Data
Stack Layout before entry to function:
RetSymb Offset Arguments Local Data RetSymb <- SP Offset = SP - Old SP Arguments Local Data
On return the SP gets automatically decremented by the value stored in "Offset". Variable argument handling is not an importance right now.
I currently have no idea at which places I have to look and what I need to do at those places. I have found the emitPrologue and emitEpilogue functions in the XXXFrameLowering.cpp but I don't really know what they are supposed to do (I guess insert code at start and end of a function). I also found a couple of functions in the XXXISelLowering.cpp file. Is there a List that explains what the different functions are supposed to do? For example:
- LowerFormalArguments(I guess insert load from stack for arguments)
Thanks in advance for any information that point me in the right direction.
To the best of my knowledge, there's no single place that explains this. You'll have to pick one of the existing backends and follow its code to see where the magic is done.
emitEpilogue are good candidates to start from, because these specifically deal with the code that sets up and tears down the frame in a function. A function is lowered to (rough approximation, there are more details...):
func_label: prologue .. function code epilogue
So to handle the custom stack layout you will definitely have to write custom code for the prologue and epilogue. The same goes for calls to functions, if the caller is responsible for some of the stack layout.
I suggest you begin by reading about the stack frame layout of some of the existing backends and then study the relevant code in LLVM. I described some of the x86 (32-bit) frame information here, for example.