[LLVMdev] Injecting code before function prolog

Arlen Cox arlencox at gmail.com
Wed Apr 7 10:43:26 PDT 2010

I'm trying to implement something similar to this:
http://gcc.gnu.org/wiki/SplitStacks in LLVM.  The reason I want this
is so that I can have dynamically growing and shrinking stacks in my
programming language.  In order to do this, I need to be able to check
for overflow of a stack frame.  The methods of doing this are outlined
in the link above, but my intention is to pass the current stack limit
as the first argument to the function.

What I'm hoping to do is to be able to inject the following code (in
x86 asm, c calling convention) on entry to each function:
  lea -frame_size(%esp), %eax
  cmpl %eax, 4(%esp)
  jb function_entry
  // handle overflow
  function prolog

The problem I'm encountering is how to force this before the prolog.
I'm attempting to add a machine function pass after the emit
prolog/epilog pass that injects this code, but directly injecting x86
code seems to be very messy as I have to figure out how LLVM encodes
the addressing modes and instructions specific to x86.  Additionally,
directly inject x86 code produces an LLVM that is not target
independent anymore.

Is there a better way to do this?  Can I maintain target independence?
 All I really need is to be able to access the stack pointer.

Thanks for your help,
Arlen Cox

More information about the llvm-dev mailing list