1 //===- MIRPrintingPass.cpp - Pass that prints out using the MIR format ----===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements a pass that prints out the LLVM module using the MIR 11 // serialization format. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/CodeGen/Passes.h" 16 #include "llvm/CodeGen/MachineFunction.h" 17 #include "llvm/CodeGen/MachineFunctionPass.h" 18 #include "llvm/IR/Module.h" 19 #include "llvm/Support/Debug.h" 20 #include "llvm/Support/raw_ostream.h" 21 #include "llvm/Support/YAMLTraits.h" 22 23 using namespace llvm; 24 25 namespace llvm { 26 namespace yaml { 27 28 /// This struct serializes the LLVM IR module. 29 template <> struct BlockScalarTraits<Module> { 30 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) { 31 Mod.print(OS, nullptr); 32 } 33 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) { 34 llvm_unreachable("LLVM Module is supposed to be parsed separately"); 35 return ""; 36 } 37 }; 38 39 } // end namespace yaml 40 } // end namespace llvm 41 42 namespace { 43 44 /// This pass prints out the LLVM IR to an output stream using the MIR 45 /// serialization format. 46 struct MIRPrintingPass : public MachineFunctionPass { 47 static char ID; 48 raw_ostream &OS; 49 50 MIRPrintingPass() : MachineFunctionPass(ID), OS(dbgs()) {} 51 MIRPrintingPass(raw_ostream &OS) : MachineFunctionPass(ID), OS(OS) {} 52 53 const char *getPassName() const override { return "MIR Printing Pass"; } 54 55 void getAnalysisUsage(AnalysisUsage &AU) const override { 56 AU.setPreservesAll(); 57 MachineFunctionPass::getAnalysisUsage(AU); 58 } 59 60 virtual bool runOnMachineFunction(MachineFunction &MF) override { 61 // TODO: Print out the machine function. 62 return false; 63 } 64 65 virtual bool doFinalization(Module &M) override { 66 yaml::Output Out(OS); 67 Out << M; 68 return false; 69 } 70 }; 71 72 char MIRPrintingPass::ID = 0; 73 74 } // end anonymous namespace 75 76 char &llvm::MIRPrintingPassID = MIRPrintingPass::ID; 77 INITIALIZE_PASS(MIRPrintingPass, "mir-printer", "MIR Printer", false, false) 78 79 namespace llvm { 80 81 MachineFunctionPass *createPrintMIRPass(raw_ostream &OS) { 82 return new MIRPrintingPass(OS); 83 } 84 85 } // end namespace llvm 86