[LLVMdev] Question about Global Variable

Qiuyu Zhang qiuyu at ucla.edu
Sun Jan 30 20:51:52 PST 2005


Hi,

Sorry for bothering you guys again.

I got problem when I am trying to recover the Global Variable Initial value. What I did is like the following

ConstantArray *Cstr = dyn_cast<ConstantArray>(gI->getInitializer());

// the above instruction enable me to get the content of initial string of global variable, like char a[10] ="test global";

And then I make some change for the Cstr and write it back to the global variable by gI->setInitializer(Constant *); Meanwhile I am trying to put a routine (constructing IR routine) into entry of main function, which is for recover initial vaule of global variable to the original string. In this routine,

Type *PointerAryType  =  ArrayType::get(PointerType::get(Type::SByteTy) , 20);
AllocaInst *PointerAry = new AllocaInst(PointerAryType , 0 , "AddrOfGstr", BB);

// Here, I tried to insert IR like alloca [20 x sbyte *], for C code, it is char *AddrOfGstr[20]; which pointer array is for storing the pointer of initial value of each 
// global variable.

So next step in my Pass, I should get the pointer of initial value of each global variable and assign them the AddrOfGstr[i]. Here, I got problem, I can get initial value by gI->getInitializer(), but this pointer is not the memory address of the initial value, it is a pointer of constant, like constant *. I cannot assign it to the AddrOfGstr[i]. How could I get memory address the initial vaule, not the pointer of constant? 

I tried to use the following way,

std::vector <Value *> idxVec;
Value *Zero1 = ConstantInt::get(Type::IntTy , 0);
Value *Zero2 = ConstantInt::get(Type::IntTy , 0);

idxVec.push_back(Zero1);
idxVec.push_back(Zero2);

Constant *pStr = ConstantExpr::getGetElementPtr(Cstr, idxVec); // trying to get pointer of initial value

GetElementPtrInst *GEP = new GetElementPtrInst( PointerAry , idxVec, "GetAryElement" , BB);  //  insert getelementptr and get the pionter of AddrOfGstr[0] 
StoreInst *storeAddr = new StoreInst(pStr , GEP , BB); // trying to assign the pStr to GEP

The above code is for getting the pionter of initial value of Cstr and assign it to AddrOfGstr[0], which should be like

char a[10]="test str";

main(){

char *AddrOfGstr[20];
AddrOfGstr[0] = a;

}



the code is compiled successfully, but when I run the pass, it failed since the instruction

Constant *pStr = ConstantExpr::getGetElementPtr(Cstr, idxVec);

I got the error as the following, 

pt: Constants.cpp:1398: static llvm::Constant* llvm::ConstantExpr::getGetElementPtr(llvm::Constant*, const std::vector<llvm::Value*, std::allocator<llvm::Value*> >&): Assertion `Ty && "GEP indices invalid!"' failed.


Is the question clear? How could I fix it? Thanks so much.

Qiuyu


  














-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20050130/e73a382b/attachment.html>


More information about the llvm-dev mailing list