Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 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" |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/MIRYamlMapping.h" |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Module.h" |
| 20 | #include "llvm/Support/Debug.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | #include "llvm/Support/YAMLTraits.h" |
| 23 | |
| 24 | using namespace llvm; |
| 25 | |
| 26 | namespace llvm { |
| 27 | namespace yaml { |
| 28 | |
| 29 | /// This struct serializes the LLVM IR module. |
| 30 | template <> struct BlockScalarTraits<Module> { |
| 31 | static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) { |
| 32 | Mod.print(OS, nullptr); |
| 33 | } |
| 34 | static StringRef input(StringRef Str, void *Ctxt, Module &Mod) { |
| 35 | llvm_unreachable("LLVM Module is supposed to be parsed separately"); |
| 36 | return ""; |
| 37 | } |
| 38 | }; |
| 39 | |
| 40 | } // end namespace yaml |
| 41 | } // end namespace llvm |
| 42 | |
| 43 | namespace { |
| 44 | |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 45 | /// This class prints out the machine functions using the MIR serialization |
| 46 | /// format. |
| 47 | class MIRPrinter { |
| 48 | raw_ostream &OS; |
| 49 | |
| 50 | public: |
| 51 | MIRPrinter(raw_ostream &OS) : OS(OS) {} |
| 52 | |
| 53 | void print(const MachineFunction &MF); |
| 54 | }; |
| 55 | |
| 56 | void MIRPrinter::print(const MachineFunction &MF) { |
| 57 | yaml::MachineFunction YamlMF; |
| 58 | YamlMF.Name = MF.getName(); |
| 59 | yaml::Output Out(OS); |
| 60 | Out << YamlMF; |
| 61 | } |
| 62 | |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 63 | /// This pass prints out the LLVM IR to an output stream using the MIR |
| 64 | /// serialization format. |
| 65 | struct MIRPrintingPass : public MachineFunctionPass { |
| 66 | static char ID; |
| 67 | raw_ostream &OS; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 68 | std::string MachineFunctions; |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 69 | |
| 70 | MIRPrintingPass() : MachineFunctionPass(ID), OS(dbgs()) {} |
| 71 | MIRPrintingPass(raw_ostream &OS) : MachineFunctionPass(ID), OS(OS) {} |
| 72 | |
| 73 | const char *getPassName() const override { return "MIR Printing Pass"; } |
| 74 | |
| 75 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 76 | AU.setPreservesAll(); |
| 77 | MachineFunctionPass::getAnalysisUsage(AU); |
| 78 | } |
| 79 | |
| 80 | virtual bool runOnMachineFunction(MachineFunction &MF) override { |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 81 | std::string Str; |
| 82 | raw_string_ostream StrOS(Str); |
| 83 | MIRPrinter(StrOS).print(MF); |
| 84 | MachineFunctions.append(StrOS.str()); |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 85 | return false; |
| 86 | } |
| 87 | |
| 88 | virtual bool doFinalization(Module &M) override { |
| 89 | yaml::Output Out(OS); |
| 90 | Out << M; |
Alex Lorenz | 78d7831 | 2015-05-28 22:41:12 +0000 | [diff] [blame] | 91 | OS << MachineFunctions; |
Alex Lorenz | 2bdb4e1 | 2015-05-27 18:02:19 +0000 | [diff] [blame] | 92 | return false; |
| 93 | } |
| 94 | }; |
| 95 | |
| 96 | char MIRPrintingPass::ID = 0; |
| 97 | |
| 98 | } // end anonymous namespace |
| 99 | |
| 100 | char &llvm::MIRPrintingPassID = MIRPrintingPass::ID; |
| 101 | INITIALIZE_PASS(MIRPrintingPass, "mir-printer", "MIR Printer", false, false) |
| 102 | |
| 103 | namespace llvm { |
| 104 | |
| 105 | MachineFunctionPass *createPrintMIRPass(raw_ostream &OS) { |
| 106 | return new MIRPrintingPass(OS); |
| 107 | } |
| 108 | |
| 109 | } // end namespace llvm |