[LLVMdev] Question about Global Variable
Chris Lattner
sabre at nondot.org
Mon Jan 31 09:13:27 PST 2005
On Sun, 30 Jan 2005, Qiuyu Zhang wrote:
> 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";
Yup.
> 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,
Ok.
> Type *PointerAryType = ArrayType::get(PointerType::get(Type::SByteTy) , 20);
> AllocaInst *PointerAry = new AllocaInst(PointerAryType , 0 , "AddrOfGstr", BB);
Okay, this should be fine.
> // 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?
Okay, you do not want to use the initializer here. Instead, you want the
*address* of the global. The address of the global is represented by the
global variable object itself. In your code above, the 'gI' variable
should be a pointer to a GlobalVariable. This Value* will have a
pointer-to-array-of-byte type.
> 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
Very close. Try using gI instead of Cstr.
> Is the question clear? How could I fix it? Thanks so much.
Try the above, and please let me know if it doesn't help!
-Chris
--
http://nondot.org/sabre/
http://llvm.cs.uiuc.edu/
More information about the llvm-dev
mailing list