[LLVMdev] generating instructions with embedded ConstantExprs from within LLVM

Patrick Meredith pmeredit at uiuc.edu
Thu Jun 17 12:48:02 PDT 2004


----- Original Message ----- 
From: "Chris Lattner" <sabre at nondot.org>
To: <llvmdev at cs.uiuc.edu>
Sent: Thursday, June 17, 2004 12:38 PM
Subject: Re: [LLVMdev] generating instructions with embedded ConstantExprs
from within LLVM


> 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.

Ohh thanks, I noticed that it didn't derive from Constant but I didn't see
any way around it.  I should have realized that that was the problem.


> 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/
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://mail.cs.uiuc.edu/mailman/listinfo/llvmdev




More information about the llvm-dev mailing list