1 //===- MIRPrinter.cpp - MIR serialization format printer ------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the class that prints out the LLVM IR and machine
11 // functions using the MIR serialization format.
13 //===----------------------------------------------------------------------===//
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"
29 /// This class prints out the machine functions using the MIR serialization
35 MIRPrinter(raw_ostream &OS) : OS(OS) {}
37 void print(const MachineFunction &MF);
39 void convert(yaml::MachineBasicBlock &YamlMBB, const MachineBasicBlock &MBB);
42 } // end anonymous namespace
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);
52 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
53 llvm_unreachable("LLVM Module is supposed to be parsed separately");
58 } // end namespace yaml
59 } // end namespace llvm
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);
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>";
83 YamlMBB.Alignment = MBB.getAlignment();
84 YamlMBB.AddressTaken = MBB.hasAddressTaken();
85 YamlMBB.IsLandingPad = MBB.isLandingPad();
88 void llvm::printMIR(raw_ostream &OS, const Module &M) {
90 Out << const_cast<Module &>(M);
93 void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
94 MIRPrinter Printer(OS);