blob: bbf163a759efdd1569d758b592b0d8b263fae0b4 [file] [log] [blame]
Alex Lorenz345c1442015-06-15 23:52:35 +00001//===- 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 Lorenz4f093bf2015-06-19 17:43:07 +000019#include "llvm/IR/BasicBlock.h"
Alex Lorenz345c1442015-06-15 23:52:35 +000020#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
25using namespace llvm;
26
27namespace {
28
29/// This class prints out the machine functions using the MIR serialization
30/// format.
31class MIRPrinter {
32 raw_ostream &OS;
33
34public:
35 MIRPrinter(raw_ostream &OS) : OS(OS) {}
36
37 void print(const MachineFunction &MF);
Alex Lorenz4f093bf2015-06-19 17:43:07 +000038
39 void convert(yaml::MachineBasicBlock &YamlMBB, const MachineBasicBlock &MBB);
Alex Lorenz345c1442015-06-15 23:52:35 +000040};
41
42} // end anonymous namespace
43
44namespace llvm {
45namespace yaml {
46
47/// This struct serializes the LLVM IR module.
48template <> 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
61void MIRPrinter::print(const MachineFunction &MF) {
62 yaml::MachineFunction YamlMF;
63 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +000064 YamlMF.Alignment = MF.getAlignment();
65 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
66 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz4f093bf2015-06-19 17:43:07 +000067 for (const auto &MBB : MF) {
68 yaml::MachineBasicBlock YamlMBB;
69 convert(YamlMBB, MBB);
70 YamlMF.BasicBlocks.push_back(YamlMBB);
71 }
Alex Lorenz345c1442015-06-15 23:52:35 +000072 yaml::Output Out(OS);
73 Out << YamlMF;
74}
75
Alex Lorenz4f093bf2015-06-19 17:43:07 +000076void 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
Alex Lorenz345c1442015-06-15 23:52:35 +000088void llvm::printMIR(raw_ostream &OS, const Module &M) {
89 yaml::Output Out(OS);
90 Out << const_cast<Module &>(M);
91}
92
93void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
94 MIRPrinter Printer(OS);
95 Printer.print(MF);
96}