[LLVMdev] A problem about convert a string to a *i8 pointer

Jin Huang 54jin.huang at gmail.com
Fri Dec 20 00:35:51 PST 2013


Hello!
Nearly, I write a pass to add an call "printf" instructions before every
instruction in a .bc file,which prints the opcode of every instruction
before the instruction executed. While passing the opcode to the printf
fucntion ,I used such way to convert the string to i8* (the same as
http://lists.cs.uiuc.edu/pipermail/llvmdev/2012-September/053206.html
 does):

 StringRef s = ins_temp->getOpcodeName();  //ins_temp is an Instruction*
type
 Constant *to_be_print = ConstantDataArray::getString(ctx, s);
GlobalVariable *g_print = new GlobalVariable(M,
to_be_print->getType(),
true,
GlobalValue::InternalLinkage,
to_be_print,
"print_str");
Constant *zero = Constant::getNullValue(llvm::IntegerType::getInt32Ty(ctx));

// what's the function of the three line code below?
 std::vector<llvm::Constant*> indices;
 indices.push_back(zero);
 indices.push_back(zero);
 Constant *test_var = ConstantExpr::getGetElementPtr(g_print,indices);

and then I use
   CallInst *call = builder.CreateCall2(call_print_ptr,var_ref,test_var);
to create the instruction and insert it.

The method works well .But I didn't know the function of the

std::vector<llvm::Constant*> indices;
 indices.push_back(zero);
 indices.push_back(zero);

is that just create a arrayRef that can be the parameter of the
getGetElementPtr() function ? Does it has anything to do with getting the
pointer to the string ?

Thank you !
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20131220/cded26bc/attachment.html>


More information about the llvm-dev mailing list