[LLVMdev] need to store the address of a variable

Paul Arndt arndt at rbg.informatik.tu-darmstadt.de
Sat Jun 28 09:00:28 PDT 2008

Hello everybody,

my problem is, that  I want to get an array of pointers to all local variables 
in a function. This array will be used for transfering these Variables to 
another execution engine. 

I've code which generates this array, and a pointer to the target field of the 

name = variables.fname + "_pointerArray";
Instruction* pointerArray = new 		
		ConstantInt::get(Type::Int32Ty, variables.inst.size()),
		// fill array with the addresses of the functions
		for(unsigned int i = 0; i < variables.inst.size(); i++) {
			name = "store_" + variables.inst[i]->getName();
			Value* GEP = new GetElementPtrInst(pointerArray, 
							   ConstantInt::get(Type::Int32Ty, i),

the result of this is:

%minPos_pointerArray = alloca i32*, i32 26              ; <i32**> [#uses=26]
%store_tmp1 = getelementptr i32** %minPos_pointerArray, i32 1    ; <i32**> ... 

But I could not find a way to get a pointer to a given variable.

When I translate the following C Code with llvm-gcc 

int a;
int* d;
d = &a;

to LLVM I get the following LLVM Code

%a_addr = alloca i32    ; <i32*> [#uses=2]
%d = alloca i32*    ; <i32**> [#uses=2]
store i32 %a, i32* %a_addr
store i32* %a_addr, i32** %d, align 4							

how can I generate such an Instruction?

When I use

name = variables.inst[i]->getName() + "address_";
Instruction *AD = new AllocaInst(Type::Int32Ty, 
ConstantInt::get(Type::Int32Ty, 1), name, alloca_point);
StoreInst *SI = new StoreInst(variables.inst[i], AD, alloca_point);
StoreInst(SI, GEP, alloca_point);

the compiler is happy, but I get the following error at run time:

Instructions.cpp:904: void llvm::StoreInst::AssertOK(): Assertion 
`getOperand(0)->getType() == 
cast<PointerType>(getOperand(1)->getType())->getElementType() && "Ptr must be 
a pointer to Val type!"' failed.

I hope you understand what I want, and someone knows the right way to do that.

Many thanks for your time.


More information about the llvm-dev mailing list