MIR Serialization: Serialize the list of machine basic blocks with simple attributes.
[oota-llvm.git] / lib / CodeGen / MIRPrinter.cpp
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"
19 #include "llvm/IR/BasicBlock.h"
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"
24
25 using namespace llvm;
26
27 namespace {
28
29 /// This class prints out the machine functions using the MIR serialization
30 /// format.
31 class MIRPrinter {
32   raw_ostream &OS;
33
34 public:
35   MIRPrinter(raw_ostream &OS) : OS(OS) {}
36
37   void print(const MachineFunction &MF);
38
39   void convert(yaml::MachineBasicBlock &YamlMBB, const MachineBasicBlock &MBB);
40 };
41
42 } // end anonymous namespace
43
44 namespace llvm {
45 namespace yaml {
46
47 /// This struct serializes the LLVM IR module.
48 template <> struct BlockScalarTraits<Module> {
49   static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
50     Mod.print(OS, nullptr);
51   }
52   static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
53     llvm_unreachable("LLVM Module is supposed to be parsed separately");
54     return "";
55   }
56 };
57
58 } // end namespace yaml
59 } // end namespace llvm
60
61 void MIRPrinter::print(const MachineFunction &MF) {
62   yaml::MachineFunction YamlMF;
63   YamlMF.Name = MF.getName();
64   YamlMF.Alignment = MF.getAlignment();
65   YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
66   YamlMF.HasInlineAsm = MF.hasInlineAsm();
67   for (const auto &MBB : MF) {
68     yaml::MachineBasicBlock YamlMBB;
69     convert(YamlMBB, MBB);
70     YamlMF.BasicBlocks.push_back(YamlMBB);
71   }
72   yaml::Output Out(OS);
73   Out << YamlMF;
74 }
75
76 void MIRPrinter::convert(yaml::MachineBasicBlock &YamlMBB,
77                          const MachineBasicBlock &MBB) {
78   // TODO: Serialize unnamed BB references.
79   if (const auto *BB = MBB.getBasicBlock())
80     YamlMBB.Name = BB->hasName() ? BB->getName() : "<unnamed bb>";
81   else
82     YamlMBB.Name = "";
83   YamlMBB.Alignment = MBB.getAlignment();
84   YamlMBB.AddressTaken = MBB.hasAddressTaken();
85   YamlMBB.IsLandingPad = MBB.isLandingPad();
86 }
87
88 void llvm::printMIR(raw_ostream &OS, const Module &M) {
89   yaml::Output Out(OS);
90   Out << const_cast<Module &>(M);
91 }
92
93 void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
94   MIRPrinter Printer(OS);
95   Printer.print(MF);
96 }