4325fb98078c1c624c3fdf9e880ea247ffdb0627
[oota-llvm.git] / include / llvm / CodeGen / MIRYamlMapping.h
1 //===- MIRYAMLMapping.h - Describes the mapping between MIR and YAML ------===//
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 // The MIR serialization library is currently a work in progress. It can't
11 // serialize machine functions at this time.
12 //
13 // This file implements the mapping between various MIR data structures and
14 // their corresponding YAML representation.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #ifndef LLVM_LIB_CODEGEN_MIRYAMLMAPPING_H
19 #define LLVM_LIB_CODEGEN_MIRYAMLMAPPING_H
20
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/Support/YAMLTraits.h"
23 #include <vector>
24
25 namespace llvm {
26 namespace yaml {
27
28 /// A wrapper around std::string which contains a source range that's being
29 /// set during parsing.
30 struct StringValue {
31   std::string Value;
32   SMRange SourceRange;
33
34   StringValue() {}
35   StringValue(std::string Value) : Value(std::move(Value)) {}
36
37   bool operator==(const StringValue &Other) const {
38     return Value == Other.Value;
39   }
40 };
41
42 template <> struct ScalarTraits<StringValue> {
43   static void output(const StringValue &S, void *, llvm::raw_ostream &OS) {
44     OS << S.Value;
45   }
46
47   static StringRef input(StringRef Scalar, void *Ctx, StringValue &S) {
48     S.Value = Scalar.str();
49     if (const auto *Node =
50             reinterpret_cast<yaml::Input *>(Ctx)->getCurrentNode())
51       S.SourceRange = Node->getSourceRange();
52     return "";
53   }
54
55   static bool mustQuote(StringRef Scalar) { return needsQuotes(Scalar); }
56 };
57
58 } // end namespace yaml
59 } // end namespace llvm
60
61 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::StringValue)
62
63 namespace llvm {
64 namespace yaml {
65
66 struct MachineBasicBlock {
67   std::string Name;
68   unsigned Alignment = 0;
69   bool IsLandingPad = false;
70   bool AddressTaken = false;
71   // TODO: Serialize the successors and liveins.
72
73   std::vector<StringValue> Instructions;
74 };
75
76 template <> struct MappingTraits<MachineBasicBlock> {
77   static void mapping(IO &YamlIO, MachineBasicBlock &MBB) {
78     YamlIO.mapOptional("name", MBB.Name,
79                        std::string()); // Don't print out an empty name.
80     YamlIO.mapOptional("alignment", MBB.Alignment);
81     YamlIO.mapOptional("isLandingPad", MBB.IsLandingPad);
82     YamlIO.mapOptional("addressTaken", MBB.AddressTaken);
83     YamlIO.mapOptional("instructions", MBB.Instructions);
84   }
85 };
86
87 } // end namespace yaml
88 } // end namespace llvm
89
90 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineBasicBlock)
91
92 namespace llvm {
93 namespace yaml {
94
95 struct MachineFunction {
96   StringRef Name;
97   unsigned Alignment = 0;
98   bool ExposesReturnsTwice = false;
99   bool HasInlineAsm = false;
100
101   std::vector<MachineBasicBlock> BasicBlocks;
102 };
103
104 template <> struct MappingTraits<MachineFunction> {
105   static void mapping(IO &YamlIO, MachineFunction &MF) {
106     YamlIO.mapRequired("name", MF.Name);
107     YamlIO.mapOptional("alignment", MF.Alignment);
108     YamlIO.mapOptional("exposesReturnsTwice", MF.ExposesReturnsTwice);
109     YamlIO.mapOptional("hasInlineAsm", MF.HasInlineAsm);
110     YamlIO.mapOptional("body", MF.BasicBlocks);
111   }
112 };
113
114 } // end namespace yaml
115 } // end namespace llvm
116
117 #endif