[LLVMdev] printf decleration

Chris Lattner sabre at nondot.org
Mon May 1 10:47:42 PDT 2006


On Mon, 1 May 2006, Ryan M. Lefever wrote:
> Ok, I think I figured it out.  I talked to someone, and we figured out that 
> when I make a call to printf with a constant string, I need to make a global 
> variable and use getElementPtr to reference it.  The modified call for 
> accessing the printf is:

Yup, that sounds right!

-Chris

>  /* m is Module*, f is Function*, i is Instruction* */
>  Constant* constStr = ConstantArray::get("test\n");
>  GlobalVariable* gv = new GlobalVariable
>    (constStr->getType(), true, GlobalValue::InternalLinkage, constStr,
>     "", m);
>  std::vector<Constant*> geplist;
>  geplist.push_back(ConstantUInt::get(Type::UIntTy,0));
>  geplist.push_back(ConstantUInt::get(Type::UIntTy,0));
>  Constant* gep = ConstantExpr::getGetElementPtr(gv,geplist);
>
>  std::vector<Value*> args;
>  args.push_back(gep);
>
>  new CallInst(f, args, "", i);
>
> Ryan M. Lefever wrote:
>> I am writing a pass where I need to make a function deceleration for 
>> printf.  Below is the code I'm trying to use.
>> 
>> -----
>> bool MyPass::runOnModule(Module &m) {
>>   vector<const Type*> args;
>>   args.push_back(PointerType::get(Type::SByteTy));
>>   Function* f = m.getOrInsertFunction("printf",
>>        FunctionType::get(Type::IntTy, args, true));
>> -----
>> 
>> When I insert a call instruction to printf, I am using the following code:
>> 
>> -----
>>   /* i an Instruction*, and f is the Function* from above */
>>   std::vector<Value*> args;
>>   args.push_back(ConstantArray::get("test"));
>> 
>>   new CallInst(f, args, "", i);
>> -----
>> 
>> However, when my pass is run with opt it complains:
>> 
>> -----
>> Call parameter type does not match function signature!
>> [5 x sbyte] c"test\00"
>>  sbyte* call int (sbyte*, ...)* %printf( [5 x sbyte] c"test\00" )  ;
>> <int>:0 [#uses=0]
>> -----
>> 
>> Does anyone know what the problem is?
>> 
>> Thanks,
>> Ryan
>> 
>
>

-Chris

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




More information about the llvm-dev mailing list