Stack Management in LLVM

Hi, going through the tutorial (
it linked to a post from 2004
( stating
that LLVM does not allow explicit stack management. It also mentioned that
this was a deficiency in LLVM, leading me to think that there is a
possibility that things have changed since them.

I'm not just looking to implement garbage collection that may have been done
already in a better way, I will be conducting research into stack handling
routines for multi-threaded programs. As such, I will need to have complete
control over the stack, including the ability to

    * Add memprotected regions before and after stack pages, with the
intention to catch the interrupt/signal and modify the stack
    * Hook before functions place data on stack, possibly moving the stack
pointer before data is placed
    * Hook before functions return with explicit control over where the new
stack pointer will be
    * Ability to resize stack (e.g. on a method call if local variables at the
end of the stack are dead, new stack will overwrite the dead space)
    * Ability to MOVE stack pages around (yes, I know this requires a language
that will not allow for pointers to a stack variable)
    * Ability to arbitrarily change the value of the stack pointer

Are there now explicit stack management hooks in LLVM? Or am I safer staying
away from LLVM if I need to manage the stack explicitly?

Thanks for your time, Joshua Moore-Oliva

To the list of modifications I need to be able to perform, add

the ability to specify the ordering of local variables in the stack
the ability to have one thread access the contents of another thread's stack.

There may be more I am forgetting, so simply put I need to be able to have
complete control over the stack. It's layout, its contents etc.

If you need specific locations for everything you're allocating on the
stack, you should probably just use a continuation-passing style like
the one mentioned in ;
LLVM won't preserve the values of variables allocated with alloca
whose address isn't taken.


If I could reduce my requirements to the ability to the following:
    * Ability to know how much stack space a function requires
    * Move the stack pointer arbitrarily (the ability to write my own function
prologue and epilogue, would need to be able to insert assembly/low level
code here) before and after a method call

could LLVM do that for me, using the system stack, (not continuations with
malloc)? Or am I barking up the wrong tree with this tool?

Thanks, Josh

Messing with the prologue and epilogue should be feasible, although
you'll have to modify the code, and it's very platform-specific; see
X86RegisterInfo::emitPrologue in lib/Target/X86/X86RegisterInfo.cpp in
the source tree.


First off, thanks for the help so far.

From what I have been able to tell, emitPrologue kicks in after the arguments

for the function have been copied. For example, consider the function

int testfunc( int foo, int bar );

Emitting assembly code from the llvm-gcc frontend in a small test program
gives the following for the call to testfunc

movl $1338, (%esp)
movl $1339, 4(%esp)
call testfunc

Correct me if I am wrong, but I believe that the hook emitPrologue gives is
before "call testfunc". The issue I have is that if I am going to move the
stack pointer, I need to do it before "movl $1338, (%esp)" so that the
called function can access its arguments where it expects them to be.

Is there anywhere in the compiler I could feasibly hook before arguments are
pushed to the stack for a function call?

Thanks, Josh

EmitPrologue is not even related to that location; it's at the
beginning and end of a function.

Basically the only place you can catch the manipulations for a call is