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); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame^] | 53 | void print(const MachineOperand &Op, const TargetRegisterInfo *TRI); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 56 | } // end anonymous namespace |
| 57 | |
| 58 | namespace llvm { |
| 59 | namespace yaml { |
| 60 | |
| 61 | /// This struct serializes the LLVM IR module. |
| 62 | template <> struct BlockScalarTraits<Module> { |
| 63 | static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) { |
| 64 | Mod.print(OS, nullptr); |
| 65 | } |
| 66 | static StringRef input(StringRef Str, void *Ctxt, Module &Mod) { |
| 67 | llvm_unreachable("LLVM Module is supposed to be parsed separately"); |
| 68 | return ""; |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | } // end namespace yaml |
| 73 | } // end namespace llvm |
| 74 | |
| 75 | void MIRPrinter::print(const MachineFunction &MF) { |
| 76 | yaml::MachineFunction YamlMF; |
| 77 | YamlMF.Name = MF.getName(); |
Alex Lorenz | 5b5f975 | 2015-06-16 00:10:47 +0000 | [diff] [blame] | 78 | YamlMF.Alignment = MF.getAlignment(); |
| 79 | YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice(); |
| 80 | YamlMF.HasInlineAsm = MF.hasInlineAsm(); |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 81 | for (const auto &MBB : MF) { |
| 82 | yaml::MachineBasicBlock YamlMBB; |
| 83 | convert(YamlMBB, MBB); |
| 84 | YamlMF.BasicBlocks.push_back(YamlMBB); |
| 85 | } |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 86 | yaml::Output Out(OS); |
| 87 | Out << YamlMF; |
| 88 | } |
| 89 | |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 90 | void MIRPrinter::convert(yaml::MachineBasicBlock &YamlMBB, |
| 91 | const MachineBasicBlock &MBB) { |
| 92 | // TODO: Serialize unnamed BB references. |
| 93 | if (const auto *BB = MBB.getBasicBlock()) |
| 94 | YamlMBB.Name = BB->hasName() ? BB->getName() : "<unnamed bb>"; |
| 95 | else |
| 96 | YamlMBB.Name = ""; |
| 97 | YamlMBB.Alignment = MBB.getAlignment(); |
| 98 | YamlMBB.AddressTaken = MBB.hasAddressTaken(); |
| 99 | YamlMBB.IsLandingPad = MBB.isLandingPad(); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 100 | |
| 101 | // Print the machine instructions. |
| 102 | YamlMBB.Instructions.reserve(MBB.size()); |
| 103 | std::string Str; |
| 104 | for (const auto &MI : MBB) { |
| 105 | raw_string_ostream StrOS(Str); |
| 106 | MIPrinter(StrOS).print(MI); |
| 107 | YamlMBB.Instructions.push_back(StrOS.str()); |
| 108 | Str.clear(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void MIPrinter::print(const MachineInstr &MI) { |
| 113 | const auto &SubTarget = MI.getParent()->getParent()->getSubtarget(); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame^] | 114 | const auto *TRI = SubTarget.getRegisterInfo(); |
| 115 | assert(TRI && "Expected target register info"); |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 116 | const auto *TII = SubTarget.getInstrInfo(); |
| 117 | assert(TII && "Expected target instruction info"); |
| 118 | |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame^] | 119 | unsigned I = 0, E = MI.getNumOperands(); |
| 120 | for (; I < E && MI.getOperand(I).isReg() && MI.getOperand(I).isDef() && |
| 121 | !MI.getOperand(I).isImplicit(); |
| 122 | ++I) { |
| 123 | if (I) |
| 124 | OS << ", "; |
| 125 | print(MI.getOperand(I), TRI); |
| 126 | } |
| 127 | |
| 128 | if (I) |
| 129 | OS << " = "; |
Alex Lorenz | 8e0a1b4 | 2015-06-22 17:02:30 +0000 | [diff] [blame] | 130 | OS << TII->getName(MI.getOpcode()); |
Alex Lorenz | f3db51de | 2015-06-23 16:35:26 +0000 | [diff] [blame^] | 131 | // TODO: Print the instruction flags, machine mem operands. |
| 132 | if (I < E) |
| 133 | OS << ' '; |
| 134 | |
| 135 | bool NeedComma = false; |
| 136 | for (; I < E; ++I) { |
| 137 | if (NeedComma) |
| 138 | OS << ", "; |
| 139 | print(MI.getOperand(I), TRI); |
| 140 | NeedComma = true; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | static void printReg(unsigned Reg, raw_ostream &OS, |
| 145 | const TargetRegisterInfo *TRI) { |
| 146 | // TODO: Print Stack Slots. |
| 147 | // TODO: Print no register. |
| 148 | // TODO: Print virtual registers. |
| 149 | if (Reg < TRI->getNumRegs()) |
| 150 | OS << '%' << StringRef(TRI->getName(Reg)).lower(); |
| 151 | else |
| 152 | llvm_unreachable("Can't print this kind of register yet"); |
| 153 | } |
| 154 | |
| 155 | void MIPrinter::print(const MachineOperand &Op, const TargetRegisterInfo *TRI) { |
| 156 | switch (Op.getType()) { |
| 157 | case MachineOperand::MO_Register: |
| 158 | // TODO: Print register flags. |
| 159 | printReg(Op.getReg(), OS, TRI); |
| 160 | // TODO: Print sub register. |
| 161 | break; |
| 162 | default: |
| 163 | // TODO: Print the other machine operands. |
| 164 | llvm_unreachable("Can't print this machine operand at the moment"); |
| 165 | } |
Alex Lorenz | 4f093bf | 2015-06-19 17:43:07 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Alex Lorenz | 345c144 | 2015-06-15 23:52:35 +0000 | [diff] [blame] | 168 | void llvm::printMIR(raw_ostream &OS, const Module &M) { |
| 169 | yaml::Output Out(OS); |
| 170 | Out << const_cast<Module &>(M); |
| 171 | } |
| 172 | |
| 173 | void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) { |
| 174 | MIRPrinter Printer(OS); |
| 175 | Printer.print(MF); |
| 176 | } |