Hi Everyone, I want to generate IR for the following C code printf("Hello World!"); To insert a printf call in the IR, I use Function* myPrint = M.getFunction("printf"); to get the function from my symbol table. Once I have the function to make a call to this function I use CallInst::Create(myPrint, args.begin(), args.end(),"", B); But I am not being able to pass a string into the argument vector. should I define args as std::vector<const Type*> args; or std::vector<const PointerType*> args; or std::vector<Value*> args; and how do I convert a constant string to either of these types, so that I can push it on the args list. Thanks! Mrunal
Shah, Mrunal J wrote:> Once I have the function > to make a call to this function I use > > CallInst::Create(myPrint, args.begin(), args.end(),"", B); > > But I am not being able to pass a string into the argument vector. > should I define args as > std::vector<const Type*> args; > or > std::vector<const PointerType*> args; > or > std::vector<Value*> args; > > and how do I convert a constant string to either of these types, so that I can push it on the args list.You need to put the string constant into a GlobalVariable (which is a Value). This is a bit tricky if you're using the direct API. I suggest using "llc -march=cpp" on a program that does printf("Hello world!"); Using IRBuilder is easier. Starting with LLVM 2.4, there's a CreateGlobalString function that takes a const char *String and returns a Value*. Nick> Thanks! > Mrunal > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >
Thanks a lot Nick -march=cpp was very helpful. But I still have a small problem, I am trying to insert a printf in a transformation pass. So when the original program already has a printf, on executing the transformation pass, it tries to create function "printf1" instead of "printf" Am I missing something here? Thanks again! Mrunal ----- Original Message ----- From: "Nick Lewycky" <nicholas at mxc.ca> To: "LLVM Developers Mailing List" <llvmdev at cs.uiuc.edu> Sent: Saturday, November 15, 2008 7:49:09 PM GMT -05:00 US/Canada Eastern Subject: Re: [LLVMdev] How do I insert a printf call in the IR? Shah, Mrunal J wrote:> Once I have the function > to make a call to this function I use > > CallInst::Create(myPrint, args.begin(), args.end(),"", B); > > But I am not being able to pass a string into the argument vector. > should I define args as > std::vector<const Type*> args; > or > std::vector<const PointerType*> args; > or > std::vector<Value*> args; > > and how do I convert a constant string to either of these types, so that I can push it on the args list.You need to put the string constant into a GlobalVariable (which is a Value). This is a bit tricky if you're using the direct API. I suggest using "llc -march=cpp" on a program that does printf("Hello world!"); Using IRBuilder is easier. Starting with LLVM 2.4, there's a CreateGlobalString function that takes a const char *String and returns a Value*. Nick> Thanks! > Mrunal > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >_______________________________________________ LLVM Developers mailing list LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Maybe Matching Threads
- [LLVMdev] How do I insert a printf call in the IR?
- [LLVMdev] How do I insert a printf call in the IR?
- [LLVMdev] Assertion `castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"' failed.
- [LLVMdev] Assertion `InReg && "Value not in map!"' failed
- [LLVMdev] Assertion `InReg && "Value not in map!"' failed