[LLVMdev] GetElementPtrConstantExpr

Duncan Sands baldrick at free.fr
Wed Dec 19 02:02:56 PST 2012


On 19/12/12 10:25, Alessio Giovanni Baroni wrote:
> Ok, now it's arising another problem. IR code that I obtain is the following:
>
> i8* bitcast ([5 x i8] c"hello\00" to i8*)

You need to put your "hello" in a (constant) global variable.  You can then
bitcast the address of the global to i8*.  I suggest you compile some C code
that makes use of the address of a string using "clang -emit-llvm" to see how
this can be done.

Ciao, Duncan.

PS: Unlike in C, in LLVM IR an array is not the same thing as a pointer to the
first element.

>
> generated from instructions:
>
> ConstantExpr::getBitCast (ConstantDataArray::getString (getGlobalContext (),
> "hello"), PointerType::get (Type::getInt8Ty (getGlobalContext ()), 0))
>
> but the LLC tool says: invalid cast opcode for cast from '[5 x i8]' to 'i8*'.
>
> Any help?
>
>
> 2012/12/18 Óscar Fuentes <ofv at wanadoo.es <mailto:ofv at wanadoo.es>>
>
>     Alessio Giovanni Baroni <alessiogiovanni.baroni at gmail.com
>     <mailto:alessiogiovanni.baroni at gmail.com>> writes:
>
>      > Because I need to convert an [4 x i8] type to i8* in the instantiation of a
>      > GlobalVariable.
>      > I have the follow declaration: %xxx = type { i8* } and I must emit the
>      > following variable:
>      > @yyy = linkonce_odr constant %xxx { [4 x i8] c"hello\00" }, but it's wrong.
>      >
>      > How do I do?
>
>     You can use the ConstantExpr::getBitCast static member function:
>
>     http://llvm.org/doxygen/classllvm_1_1ConstantExpr.html#aba93ec4079f8d709a1d5b4745310782e
>
>     This is a common idiom on LLVM: a base class has static methods for
>     creating instances of derived classes.
>
>     _______________________________________________
>     LLVM Developers mailing list
>     LLVMdev at cs.uiuc.edu <mailto:LLVMdev at cs.uiuc.edu> http://llvm.cs.uiuc.edu
>     http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>
>
>
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>




More information about the llvm-dev mailing list