[LLVMdev] inserting a print statement into IR

John Criswell criswell at illinois.edu
Mon Apr 4 20:33:05 PDT 2011


On 4/4/2011 6:26 PM, George Baah wrote:
> Hi Everyone,
>   I am trying to construct the print statement : printf("value:%d\n", 
> value);
> This is my llvm code. It is seg faulting at 
> builder.CreateGlobalStringPtr(str,"").

This might be easier to debug with a stack trace.  Use a debugger to see 
the call stack when the segfault occurs.  Also try to isolate which 
pointer value is bad.

As a guess, did you give the builder object all the information it needs 
to insert IR (e.g., which LLVM Module * it is modifying, which 
BasicBlock or Instruction it is to use when inserting instructions, etc)?

-- John T.

> Thanks.
>
> George
>
> vector<const Type *> params;
> params.push_back(Type::getInt8PtrTy(M.getContext()));
> FunctionType *fType = 
> FunctionType::get(Type::getInt32Ty(M.getContext()), params, true);
> Constant *temp = M.getOrInsertFunction("printf",fType);
> if(!temp){
>   errs() << "printf function not in symbol table\n";
>   exit(1);
> }
> Function *f = cast<Function>(temp);
> f->setCallingConv(CallingConv::C);
>
> const char *str = "value: %d\n";
> Value *intparam = ...
> Value *strPtr = builder.CreateGlobalStringPtr(str,"");
> builder.CreateCall2(PrintF, strPtr, intparam,"tmp6");

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20110404/4e4c363d/attachment.html>


More information about the llvm-dev mailing list