Instruction for print

Hi,

I am trying to insert a instruction for printf or cout. My aim is to print a integer and a string value using that instruction, which are already there in the code.
but I am not able to understand how to do it. Any help would be great help.

Thanks and Regards,
Tarun Agrawal

Hi,

I am trying to insert a instruction for printf or cout. My aim is to print a integer and a string value using that instruction, which are already there in the code.
but I am not able to understand how to do it. Any help would be great help.

You want to insert a call instruction that calls the printf function. You’ll need to insert a Function for printf (with no body) if no printf function exists within the LLVM module you’re transforming.

So, the code will look something like:

Function * F = M.getOrInsertFunction (“printf”, …);
CallInst::Create (F, …);

Be sure to look at the doxygen docs for exact method names and which parameters go where; I’ve skipped over such details above.

– John T.

Just to be clear, I have to add a function name printf in the module with no body. And I can call the function. This will automatically call the printf function which is defined in the library.

Tarun Agrawal

Just to be clear, I have to add a function name printf in the module with no body. And I can call the function. This will automatically call the printf function which is defined in the library.

Yes. That is correct.

You also need to link in the native code library that defines printf after code generation, but since printf is in libc, you are probably doing that step already.

– John T.