<div dir="ltr"><div class="gmail_default" style="font-family:tahoma,sans-serif">Hello!</div><div class="gmail_default" style="font-family:tahoma,sans-serif">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 <a href="http://lists.cs.uiuc.edu/pipermail/llvmdev/2012-September/053206.html" style="font-family:arial">http://lists.cs.uiuc.edu/pipermail/llvmdev/2012-September/053206.html</a> does): </div>
<div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default"><span style="font-family:tahoma,sans-serif"> </span><font face="tahoma, sans-serif">StringRef s = ins_temp->getOpcodeName(); //ins_temp is an Instruction* type</font></div>
<div class="gmail_default"><font face="tahoma, sans-serif"><span style="white-space:pre"> </span>Constant *to_be_print = ConstantDataArray::getString(ctx, s);</font></div><div class="gmail_default"><font face="tahoma, sans-serif"><span class="" style="white-space:pre"> </span>GlobalVariable *g_print = new GlobalVariable(M,</font></div>
<div class="gmail_default"><font face="tahoma, sans-serif"><span class="" style="white-space:pre"> </span>to_be_print->getType(),</font></div><div class="gmail_default"><font face="tahoma, sans-serif"><span class="" style="white-space:pre"> </span>true,</font></div>
<div class="gmail_default"><font face="tahoma, sans-serif"><span class="" style="white-space:pre"> </span>GlobalValue::InternalLinkage,</font></div><div class="gmail_default"><font face="tahoma, sans-serif"><span class="" style="white-space:pre"> </span>to_be_print,</font></div>
<div class="gmail_default"><font face="tahoma, sans-serif"><span class="" style="white-space:pre"> </span>"print_str");</font></div><div class="gmail_default"><font face="tahoma, sans-serif"><div class="gmail_default">
Constant *zero = Constant::getNullValue(llvm::IntegerType::getInt32Ty(ctx));</div><div class="gmail_default"><br></div><div class="gmail_default">// what's the function of the three line code below?</div><div class="gmail_default">
std::vector<llvm::Constant*> indices;</div><div class="gmail_default"> indices.push_back(zero);</div><div class="gmail_default"> indices.push_back(zero);</div></font></div><div class="gmail_default"><font face="tahoma, sans-serif"> Constant *test_var = ConstantExpr::getGetElementPtr(g_print,indices);</font></div>
<div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">and then I use </div><div class="gmail_default"><font face="tahoma, sans-serif"> CallInst *call = builder.CreateCall2(call_print_ptr,var_ref,test_var);</font><br>
</div><div class="gmail_default"><font face="tahoma, sans-serif">to create the instruction and insert it.</font></div><div class="gmail_default"><font face="tahoma, sans-serif"><br></font></div><div class="gmail_default">
<font face="tahoma, sans-serif">The method works well .But I didn't know the function of the </font></div><div class="gmail_default"><font face="tahoma, sans-serif"><br></font></div><div class="gmail_default"><div class="gmail_default" style="font-family:tahoma,sans-serif">
std::vector<llvm::Constant*> indices;</div><div class="gmail_default" style="font-family:tahoma,sans-serif"> indices.push_back(zero);</div><div class="gmail_default" style="font-family:tahoma,sans-serif"> indices.push_back(zero);</div>
<div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">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 ?</div>
<div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">Thank you !</div></div></div>