[LLVMdev] llvm cannot iterate a [3 x i8]

Duncan Sands baldrick at free.fr
Mon Mar 4 04:48:35 PST 2013


Hi,

On 04/03/13 11:23, Alexandru Ionut Diaconescu wrote:
> Hello everyone,
>
> I am trying to "parse" a part of LLVM IR. More exactly, from
>
>      @.str = private unnamed_addr constant [3 x i8] c"DS\00", section
> "llvm.metadata"
>
> I want to get "DS". It is the single place in the whole bytecode from where I
> can get it. I have :
>
>      ...
>      Value *VV = cast<Value>(LD100->getOperand(1)->getOperand(0));
>      errs()<<"\n VV  "<<*(VV)<<"\n";
>      RESULT : VV  @.str = private unnamed_addr constant [3 x i8] c"DS\00",
> section "llvm.metadata"
>
>      if(VV->getValueID() == Value::GlobalVariableVal){
>          GlobalVariable* FD = cast<GlobalVariable>(VV);
>          Value *VVV = cast<Value>(FD->getOperand(0));
>          errs()<<"\n VVV  "<<*(VVV)<<"\n";
>          RESULT :  VVV  [3 x i8] c"DS\00"
>
>          if(VVV->getValueID() == Value::ConstantDataArrayVal){
>              ConstantArray *caa = (ConstantArray *)VVV;
>              errs()<<"\n "<<(caa->getNumOperands())<<"\n";
>              errs()<<"\n "<<*(caa->getType())<<"\n";
>              RESULT :  0
>                        [3 x i8]
>          }
>
>
>  From this point, I tried to cast to every `enum llvm::Value::ValueTy` in order
> to try to iterate through [3 X i8], in order to get "DS" (as a StringRef or
> std::string would be nice), but I cannot. How I can parse this structure?
>
> Thank you for any help !

I think you are looking for ConstantDataSequential::getAsCString
Note that ConstantDataArray derives from ConstantDataSequential.

Ciao, Duncan.

PS: rather than doing "VVV->getValueID() == Value::ConstantDataArrayVal" you can
do: isa<ConstantDataArray>(VVV)

Ciao, Duncan.



More information about the llvm-dev mailing list