blob: b2bb2f9278d8b2b5f0cf4817dfbdf8d028ea5834 [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"
19#include "llvm/IR/Module.h"
20#include "llvm/Support/MemoryBuffer.h"
21#include "llvm/Support/raw_ostream.h"
22#include "llvm/Support/YAMLTraits.h"
23
24using namespace llvm;
25
26namespace {
27
28/// This class prints out the machine functions using the MIR serialization
29/// format.
30class MIRPrinter {
31 raw_ostream &OS;
32
33public:
34 MIRPrinter(raw_ostream &OS) : OS(OS) {}
35
36 void print(const MachineFunction &MF);
37};
38
39} // end anonymous namespace
40
41namespace llvm {
42namespace yaml {
43
44/// This struct serializes the LLVM IR module.
45template <> struct BlockScalarTraits<Module> {
46 static void output(const Module &Mod, void *Ctxt, raw_ostream &OS) {
47 Mod.print(OS, nullptr);
48 }
49 static StringRef input(StringRef Str, void *Ctxt, Module &Mod) {
50 llvm_unreachable("LLVM Module is supposed to be parsed separately");
51 return "";
52 }
53};
54
55} // end namespace yaml
56} // end namespace llvm
57
58void MIRPrinter::print(const MachineFunction &MF) {
59 yaml::MachineFunction YamlMF;
60 YamlMF.Name = MF.getName();
Alex Lorenz5b5f9752015-06-16 00:10:47 +000061 YamlMF.Alignment = MF.getAlignment();
62 YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
63 YamlMF.HasInlineAsm = MF.hasInlineAsm();
Alex Lorenz345c1442015-06-15 23:52:35 +000064 yaml::Output Out(OS);
65 Out << YamlMF;
66}
67
68void llvm::printMIR(raw_ostream &OS, const Module &M) {
69 yaml::Output Out(OS);
70 Out << const_cast<Module &>(M);
71}
72
73void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
74 MIRPrinter Printer(OS);
75 Printer.print(MF);
76}