write IR on file

hi
how can i write IR instruction on a file.txt? when i use {for(inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I,count++) IRcodefile << std::basic_ostream(*I) <<“\n” } i get error.
is there any way that i can write it on file?

Hey Rahil,

try this:

#include "llvm/Support/raw_ostream.h"

Function* f = ...
std::string errorMessage = "";
raw_fd_ostream file("file.txt", errorMessage);
f->print(file, NULL);
file.close();

The class "Module" also has a print-function you can use to dump the entire module.
If you want to print additional, custom information (e.g. a comment behind each instruction), have a look at the AssemblyAnnotationWriter class.

Best,
Ralf

hi
how can i write IR instruction on a file.txt? when i use {for(inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I,count++) IRcodefile << std::basic_ostream(*I) <<“\n” } i get error.
is there any way that i can write it on file?