MIR Serialization: Print and parse simple machine function 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/Module.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Support/YAMLTraits.h"
23
24 using namespace llvm;
25
26 namespace {
27
28 /// This class prints out the machine functions using the MIR serialization
29 /// format.
30 class MIRPrinter {
31   raw_ostream &OS;
32
33 public:
34   MIRPrinter(raw_ostream &OS) : OS(OS) {}
35
36   void print(const MachineFunction &MF);
37 };
38
39 } // end anonymous namespace
40
41 namespace llvm {
42 namespace yaml {
43
44 /// This struct serializes the LLVM IR module.
45 template <> 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
58 void MIRPrinter::print(const MachineFunction &MF) {
59   yaml::MachineFunction YamlMF;
60   YamlMF.Name = MF.getName();
61   YamlMF.Alignment = MF.getAlignment();
62   YamlMF.ExposesReturnsTwice = MF.exposesReturnsTwice();
63   YamlMF.HasInlineAsm = MF.hasInlineAsm();
64   yaml::Output Out(OS);
65   Out << YamlMF;
66 }
67
68 void llvm::printMIR(raw_ostream &OS, const Module &M) {
69   yaml::Output Out(OS);
70   Out << const_cast<Module &>(M);
71 }
72
73 void llvm::printMIR(raw_ostream &OS, const MachineFunction &MF) {
74   MIRPrinter Printer(OS);
75   Printer.print(MF);
76 }