How to implement push and pop in LLVM assembly?
I want to implement push and pop operations in LLVM assembly.
The alloca instruction does not follow the concept of stack, push and pop.
Examples:
PUSH
x86subl $4, %esp
movl %eax, 0(%esp)
or
pushl %eax
mips
addi $sp, $sp, -4
sw $t2, 0($sp)
POP
x86movl 0(%esp), %eax
addl $4, %esp
or
popl %eax
mips
lw $t2, 0($sp)
addi $sp, $sp, 4
EDIT 1:
I need a platform-independent solution.
First
I want to use the top of the stack to store temporary objects.The expression "a*b + c*d + e*f" will need to store the result of three multiplications, the operands are big objects of a class and the operators are overloaded. The operation "a*b" will take many instructions, "a" and "b" cannot be modified during the multiplication, it means the object that will result from "a*b" cannot use the same memory place of "a" or "b".
In the code in a hypothetical language,
call_function( &Object(), &(a + b) );
"&Object()" will create an object on the stack and get its pointer, the pointer is a parameter to the function, and the object will be deleted after the function return.
Second
I want to optimize recursive functions. I want to transform a recursive function into an iter开发者_JAVA百科ative function and use the stack to push data that will be used later. I cannot use array because I do not know the largest number of recursive calls that will be on the stack.If you want "just" push and pop - then you should definitely use inline assembler.
If you want to extend LLVM IR this way, you should define the clean semantics first. E.g. how this should interact with the stack movements inserted by backend, what will happen if you will have push in the middle of the basic block w/o any pop, etc. However, this does not seem to be a good idea, why do you need this at all?
精彩评论