1 //===- MIRYAMLMapping.h - Describes the mapping between MIR and YAML ------===//
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 // The MIR serialization library is currently a work in progress. It can't
11 // serialize machine functions at this time.
13 // This file implements the mapping between various MIR data structures and
14 // their corresponding YAML representation.
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_LIB_CODEGEN_MIRYAMLMAPPING_H
19 #define LLVM_LIB_CODEGEN_MIRYAMLMAPPING_H
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/Support/YAMLTraits.h"
28 /// A wrapper around std::string which contains a source range that's being
29 /// set during parsing.
35 StringValue(std::string Value) : Value(std::move(Value)) {}
37 bool operator==(const StringValue &Other) const {
38 return Value == Other.Value;
42 template <> struct ScalarTraits<StringValue> {
43 static void output(const StringValue &S, void *, llvm::raw_ostream &OS) {
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();
55 static bool mustQuote(StringRef Scalar) { return needsQuotes(Scalar); }
58 struct FlowStringValue : StringValue {
60 FlowStringValue(std::string Value) : StringValue(Value) {}
63 template <> struct ScalarTraits<FlowStringValue> {
64 static void output(const FlowStringValue &S, void *, llvm::raw_ostream &OS) {
65 return ScalarTraits<StringValue>::output(S, nullptr, OS);
68 static StringRef input(StringRef Scalar, void *Ctx, FlowStringValue &S) {
69 return ScalarTraits<StringValue>::input(Scalar, Ctx, S);
72 static bool mustQuote(StringRef Scalar) { return needsQuotes(Scalar); }
75 } // end namespace yaml
76 } // end namespace llvm
78 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::StringValue)
79 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::FlowStringValue)
84 struct MachineBasicBlock {
87 unsigned Alignment = 0;
88 bool IsLandingPad = false;
89 bool AddressTaken = false;
90 // TODO: Serialize the successor weights and liveins.
91 std::vector<FlowStringValue> Successors;
93 std::vector<StringValue> Instructions;
96 template <> struct MappingTraits<MachineBasicBlock> {
97 static void mapping(IO &YamlIO, MachineBasicBlock &MBB) {
98 YamlIO.mapRequired("id", MBB.ID);
99 YamlIO.mapOptional("name", MBB.Name,
100 std::string()); // Don't print out an empty name.
101 YamlIO.mapOptional("alignment", MBB.Alignment);
102 YamlIO.mapOptional("isLandingPad", MBB.IsLandingPad);
103 YamlIO.mapOptional("addressTaken", MBB.AddressTaken);
104 YamlIO.mapOptional("successors", MBB.Successors);
105 YamlIO.mapOptional("instructions", MBB.Instructions);
109 } // end namespace yaml
110 } // end namespace llvm
112 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineBasicBlock)
117 struct MachineFunction {
119 unsigned Alignment = 0;
120 bool ExposesReturnsTwice = false;
121 bool HasInlineAsm = false;
122 // Register information
124 bool TracksRegLiveness = false;
125 bool TracksSubRegLiveness = false;
126 // TODO: Serialize virtual register definitions.
127 // TODO: Serialize the various register masks.
128 // TODO: Serialize live in registers.
130 std::vector<MachineBasicBlock> BasicBlocks;
133 template <> struct MappingTraits<MachineFunction> {
134 static void mapping(IO &YamlIO, MachineFunction &MF) {
135 YamlIO.mapRequired("name", MF.Name);
136 YamlIO.mapOptional("alignment", MF.Alignment);
137 YamlIO.mapOptional("exposesReturnsTwice", MF.ExposesReturnsTwice);
138 YamlIO.mapOptional("hasInlineAsm", MF.HasInlineAsm);
139 YamlIO.mapOptional("isSSA", MF.IsSSA);
140 YamlIO.mapOptional("tracksRegLiveness", MF.TracksRegLiveness);
141 YamlIO.mapOptional("tracksSubRegLiveness", MF.TracksSubRegLiveness);
142 YamlIO.mapOptional("body", MF.BasicBlocks);
146 } // end namespace yaml
147 } // end namespace llvm