[LLVMdev] generating instructions with embedded ConstantExprs from within LLVM
Chris Lattner
sabre at nondot.org
Thu Jun 17 12:28:00 PDT 2004
On Thu, 17 Jun 2004, Patrick Meredith wrote:
> How is this done? Everything logical I have tried has failed, here was
> one attempt:
>
> Constant *C = (Constant*) ConstantArray::get(inst2string(I)); //fucnction defined elsewhere
>
> //generates a correct Global string
> GlobalVariable *str = new GlobalVariable(C->getType(), true,
> GlobalValue::InternalLinkage,
> C, mkStrName( strNumber++ ), &M);
...
> Constant *gep = ConstantExpr::getGetElementPtr( (Constant*) str, indices);
Sorry for responding twice. As soon as I sent that I realized the
problem. The problem is that the cast on the bottom line here is masking
the bug. In particular, you can't use the address of global variables as
constants (yet: see PR122). This is definitely a bug in LLVM that you
can't do this, but we do have a gross work-around. In particular, to get
the address of a global value as a constant, use the ConstantPointerRef
class:
Constant *strconst = ConstantPointerRef::get(str);
Constant *gep = ConstantExpr::getGetElementPtr(strconst, indices);
In the future, the GlobalValue class will derive from the Constant class,
making this unnecessary. Since the "value" of a GlobalValue is the
*address* of the global, and since this is a link-time constant, it makes
sense for it to derive from the Constant class.
Sorry for the confusion, you have every right to expect things to work
this way. In the future they will, as part of PR122. :)
-Chris
--
http://llvm.cs.uiuc.edu/
http://www.nondot.org/~sabre/Projects/
More information about the llvm-dev
mailing list