[LLVMdev] could you give me some advice ?

Chris Lattner sabre at nondot.org
Sun Mar 26 22:51:32 PST 2006


On Mon, 27 Mar 2006, lizhuo wrote:
> but it is not  applicable for c
> string s= cast<ConstantArray>(c)->getAsString(); // Assert Fail
>
> because c is ConstantExpr::GetElementPtr actually , not ConstantArray

Exactly.

> so I have to get string object like this :
> GlobalVariable* gv = cast<GlobalVariable>(c->getOperand(0));
> ConstantArray* ca = cast<ConstantArray>(gv->getOperand(0));
> std::string str = ca->getAsString();
> I feel this code a little awkward .
> I wonder whether it should to be improved ?

You have the right approach, there is nothing better that you can do, 
other than writing helper functions for yourself.

The issue is that LLVM has no notion of a 'string'.  It only has global 
variables that are constant arrays, which happen to be initialized with 
byte values that can be interpreted in a string-like way.

The ConstantArray::getAsString method is a nice convenience method, but it 
doesn't make sense for GlobalVariable's to have this.

-Chris

>> On Sat, 25 Mar 2006, lizhuo wrote:
>>> ; define ConstantArray
>>> "E1$str" = internal constant [3 x sbyte] c"E1\00"
>>>
>>> ; use getPtrPtrFromArrayPtr to define SByte* from ConstantArray
>>> "E1$entry" = internal constant sbyte* getelementptr ([3 x sbyte]* "E1$str", uint 0, uint 0)
>>> ...
>>>
>>> when i want to get std::string object from this declaration in another program
>>>
>>> 	Module* m = loadModule("XXX.bc");
>>> 	Constant* c =  findValue(m,"E1$entry");
>>> 	GlobalVariable* gv = cast<GlobalVariable>(c->getOperand(0));
>>> 	ConstantArray* ca = cast<ConstantArray>(gv->getOperand(0));
>>> 	std::string str = ca->getAsString();
>>>
>>> now str = "E1"
>>> these code looks a little ugly , could you tell me better solution ?
>>> In another word , I want to know the reverse operation of ConstantExpr::getPtrPtrFromArrayPtr .
>>
>> I'm not sure I understand.  You have a pointer to "E1$entry" and you want
>> the string it points to?  I assume this is what your findValue
>> function does?
>>
>> -Chris
>>
>
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>

-Chris

-- 
http://nondot.org/sabre/
http://llvm.org/




More information about the llvm-dev mailing list