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 StringValue()); // 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 /// Serializable representation of MachineFrameInfo.
119 /// Doesn't serialize attributes like 'StackAlignment', 'IsStackRealignable' and
120 /// 'RealignOption' as they are determined by the target and LLVM function
122 /// It also doesn't serialize attributes like 'NumFixedObject' and
123 /// 'HasVarSizedObjects' as they are determined by the frame objects themselves.
124 struct MachineFrameInfo {
125 // TODO: Serialize stack objects.
126 bool IsFrameAddressTaken = false;
127 bool IsReturnAddressTaken = false;
128 bool HasStackMap = false;
129 bool HasPatchPoint = false;
130 uint64_t StackSize = 0;
131 int OffsetAdjustment = 0;
132 unsigned MaxAlignment = 0;
133 bool AdjustsStack = false;
134 bool HasCalls = false;
135 // TODO: Serialize StackProtectorIdx and FunctionContextIdx
136 unsigned MaxCallFrameSize = 0;
137 // TODO: Serialize callee saved info.
138 // TODO: Serialize local frame objects.
139 bool HasOpaqueSPAdjustment = false;
140 bool HasVAStart = false;
141 bool HasMustTailInVarArgFunc = false;
142 // TODO: Serialize save and restore MBB references.
145 template <> struct MappingTraits<MachineFrameInfo> {
146 static void mapping(IO &YamlIO, MachineFrameInfo &MFI) {
147 YamlIO.mapOptional("isFrameAddressTaken", MFI.IsFrameAddressTaken);
148 YamlIO.mapOptional("isReturnAddressTaken", MFI.IsReturnAddressTaken);
149 YamlIO.mapOptional("hasStackMap", MFI.HasStackMap);
150 YamlIO.mapOptional("hasPatchPoint", MFI.HasPatchPoint);
151 YamlIO.mapOptional("stackSize", MFI.StackSize);
152 YamlIO.mapOptional("offsetAdjustment", MFI.OffsetAdjustment);
153 YamlIO.mapOptional("maxAlignment", MFI.MaxAlignment);
154 YamlIO.mapOptional("adjustsStack", MFI.AdjustsStack);
155 YamlIO.mapOptional("hasCalls", MFI.HasCalls);
156 YamlIO.mapOptional("maxCallFrameSize", MFI.MaxCallFrameSize);
157 YamlIO.mapOptional("hasOpaqueSPAdjustment", MFI.HasOpaqueSPAdjustment);
158 YamlIO.mapOptional("hasVAStart", MFI.HasVAStart);
159 YamlIO.mapOptional("hasMustTailInVarArgFunc", MFI.HasMustTailInVarArgFunc);
163 struct MachineFunction {
165 unsigned Alignment = 0;
166 bool ExposesReturnsTwice = false;
167 bool HasInlineAsm = false;
168 // Register information
170 bool TracksRegLiveness = false;
171 bool TracksSubRegLiveness = false;
172 // TODO: Serialize virtual register definitions.
173 // TODO: Serialize the various register masks.
174 // TODO: Serialize live in registers.
176 MachineFrameInfo FrameInfo;
178 std::vector<MachineBasicBlock> BasicBlocks;
181 template <> struct MappingTraits<MachineFunction> {
182 static void mapping(IO &YamlIO, MachineFunction &MF) {
183 YamlIO.mapRequired("name", MF.Name);
184 YamlIO.mapOptional("alignment", MF.Alignment);
185 YamlIO.mapOptional("exposesReturnsTwice", MF.ExposesReturnsTwice);
186 YamlIO.mapOptional("hasInlineAsm", MF.HasInlineAsm);
187 YamlIO.mapOptional("isSSA", MF.IsSSA);
188 YamlIO.mapOptional("tracksRegLiveness", MF.TracksRegLiveness);
189 YamlIO.mapOptional("tracksSubRegLiveness", MF.TracksSubRegLiveness);
190 YamlIO.mapOptional("frameInfo", MF.FrameInfo);
191 YamlIO.mapOptional("body", MF.BasicBlocks);
195 } // end namespace yaml
196 } // end namespace llvm