Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 1 | //===- MIRPrinter.cpp - MIR serialization format printer ------------------===// |
| 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 the class that prints out the LLVM IR and machine |
| 11 | // functions using the MIR serialization format. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "MIRPrinter.h" |
| 16 | #include "llvm/ADT/STLExtras.h" |
| 17 | #include "llvm/CodeGen/MachineFunction.h" |
| 18 | #include "llvm/CodeGen/MIRYamlMapping.h" |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 19 | #include "llvm/IR/BasicBlock.h" |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Module.h" |
| 21 | #include "llvm/Support/MemoryBuffer.h" |
| 22 | #include "llvm/Support/raw_ostream.h" |
| 23 | #include "llvm/Support/YAMLTraits.h" |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame^] | 24 | #include "llvm/Target/TargetInstrInfo.h" |
| 25 | #include "llvm/Target/TargetSubtargetInfo.h" |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | |
| 29 | namespace { |
| 30 | |
| 31 | /// This class prints out the machine functions using the MIR serialization |
| 32 | /// format. |
| 33 | class MIRPrinter { |
| 34 | raw_ostream &OS; |
| 35 | |
| 36 | public: |
| 37 | MIRPrinter(raw_ostream &OS) : OS(OS) {} |
| 38 | |
| 39 | void print(const MachineFunction &MF); |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 40 | |
| 41 | void convert(yaml::MachineBasicBlock &YamlMBB, const MachineBasicBlock &MBB); |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame^] | 44 | /// This class prints out the machine instructions using the MIR serialization |
| 45 | /// format. |
| 46 | class MIPrinter { |
| 47 | raw_ostream &OS; |
| 48 | |
| 49 | public: |
| 50 | MIPrinter(raw_ostream &OS) : OS(OS) {} |
| 51 | |
| 52 | void print(const MachineInstr &MI); |
| 53 | }; |
| 54 | |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 55 | } // end anonymous namespace |
| 56 | |
| 57 | namespace llvm { |
| 58 | namespace yaml { |
| 59 | |
| 60 | /// This struct serializes the LLVM IR module. |
| 61 | template <> struct BlockScalarTraits<Module> { |
| 62 | static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) { |
| 63 | Mod.print(OS, nullptr); |
| 64 | } |
| 65 | static StringRef input(StringRef Str, void *Ctxt, Module &Mod) { |
| 66 | llvm_unreachable("LLVM Module is supposed to be parsed separately"); |
| 67 | return ""; |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | } // end namespace yaml |
| 72 | } // end namespace llvm |
| 73 | |
| 74 | void MIRPrinter::print(const MachineFunction &MF) { |
| 75 | yaml::MachineFunction YamlMF; |
| 76 | YamlMF.Name = MF.getName(); |
Alex Lorenz | 5b5f975 | 2015-06-16 00:10:47 +0000 | [diff] [blame] | 77 | YamlMF.Alignment = MF.getAlignment(); |
| 78 | YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice(); |
| 79 | YamlMF.HasInlineAsm = MF.hasInlineAsm(); |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 80 | for (const auto &MBB : MF) { |
| 81 | yaml::MachineBasicBlock YamlMBB; |
| 82 | convert(YamlMBB, MBB); |
| 83 | YamlMF.BasicBlocks.push_back(YamlMBB); |
| 84 | } |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 85 | yaml::Output Out(OS); |
| 86 | Out << YamlMF; |
| 87 | } |
| 88 | |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 89 | void MIRPrinter::convert(yaml::MachineBasicBlock &YamlMBB, |
| 90 | const MachineBasicBlock &MBB) { |
| 91 | // TODO: Serialize unnamed BB references. |
| 92 | if (const auto *BB = MBB.getBasicBlock()) |
| 93 | YamlMBB.Name = BB->hasName() ? BB->getName() : "<unnamed bb>"; |
| 94 | else |
| 95 | YamlMBB.Name = ""; |
| 96 | YamlMBB.Alignment = MBB.getAlignment(); |
| 97 | YamlMBB.AddressTaken = MBB.hasAddressTaken(); |
| 98 | YamlMBB.IsLandingPad = MBB.isLandingPad(); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame^] | 99 | |
| 100 | // Print the machine instructions. |
| 101 | YamlMBB.Instructions.reserve(MBB.size()); |
| 102 | std::string Str; |
| 103 | for (const auto &MI : MBB) { |
| 104 | raw_string_ostream StrOS(Str); |
| 105 | MIPrinter(StrOS).print(MI); |
| 106 | YamlMBB.Instructions.push_back(StrOS.str()); |
| 107 | Str.clear(); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void MIPrinter::print(const MachineInstr &MI) { |
| 112 | const auto &SubTarget = MI.getParent()->getParent()->getSubtarget(); |
| 113 | const auto *TII = SubTarget.getInstrInfo(); |
| 114 | assert(TII && "Expected target instruction info"); |
| 115 | |
| 116 | OS << TII->getName(MI.getOpcode()); |
| 117 | // TODO: Print the instruction flags, machine operands, machine mem operands. |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 120 | void llvm::printMIR(raw_ostream &OS, const Module &M) { |
| 121 | yaml::Output Out(OS); |
| 122 | Out << const_cast<Module &>(M); |
| 123 | } |
| 124 | |
| 125 | void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) { |
| 126 | MIRPrinter Printer(OS); |
| 127 | Printer.print(MF); |
| 128 | } |