MIR Serialization: Change MIR syntax - use custom syntax for MBBs.
[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/CodeGen/MachineJumpTableInfo.h"
23 #include "llvm/Support/YAMLTraits.h"
24 #include <vector>
25
26 namespace llvm {
27 namespace yaml {
28
29 /// A wrapper around std::string which contains a source range that's being
30 /// set during parsing.
31 struct StringValue {
32   std::string Value;
33   SMRange SourceRange;
34
35   StringValue() {}
36   StringValue(std::string Value) : Value(std::move(Value)) {}
37
38   bool operator==(const StringValue &Other) const {
39     return Value == Other.Value;
40   }
41 };
42
43 template <> struct ScalarTraits<StringValue> {
44   static void output(const StringValue &S, void *, llvm::raw_ostream &OS) {
45     OS << S.Value;
46   }
47
48   static StringRef input(StringRef Scalar, void *Ctx, StringValue &S) {
49     S.Value = Scalar.str();
50     if (const auto *Node =
51             reinterpret_cast<yaml::Input *>(Ctx)->getCurrentNode())
52       S.SourceRange = Node->getSourceRange();
53     return "";
54   }
55
56   static bool mustQuote(StringRef Scalar) { return needsQuotes(Scalar); }
57 };
58
59 struct FlowStringValue : StringValue {
60   FlowStringValue() {}
61   FlowStringValue(std::string Value) : StringValue(Value) {}
62 };
63
64 template <> struct ScalarTraits<FlowStringValue> {
65   static void output(const FlowStringValue &S, void *, llvm::raw_ostream &OS) {
66     return ScalarTraits<StringValue>::output(S, nullptr, OS);
67   }
68
69   static StringRef input(StringRef Scalar, void *Ctx, FlowStringValue &S) {
70     return ScalarTraits<StringValue>::input(Scalar, Ctx, S);
71   }
72
73   static bool mustQuote(StringRef Scalar) { return needsQuotes(Scalar); }
74 };
75
76 struct BlockStringValue {
77   StringValue Value;
78 };
79
80 template <> struct BlockScalarTraits<BlockStringValue> {
81   static void output(const BlockStringValue &S, void *Ctx, raw_ostream &OS) {
82     return ScalarTraits<StringValue>::output(S.Value, Ctx, OS);
83   }
84
85   static StringRef input(StringRef Scalar, void *Ctx, BlockStringValue &S) {
86     return ScalarTraits<StringValue>::input(Scalar, Ctx, S.Value);
87   }
88 };
89
90 /// A wrapper around unsigned which contains a source range that's being set
91 /// during parsing.
92 struct UnsignedValue {
93   unsigned Value;
94   SMRange SourceRange;
95
96   UnsignedValue() : Value(0) {}
97   UnsignedValue(unsigned Value) : Value(Value) {}
98
99   bool operator==(const UnsignedValue &Other) const {
100     return Value == Other.Value;
101   }
102 };
103
104 template <> struct ScalarTraits<UnsignedValue> {
105   static void output(const UnsignedValue &Value, void *Ctx, raw_ostream &OS) {
106     return ScalarTraits<unsigned>::output(Value.Value, Ctx, OS);
107   }
108
109   static StringRef input(StringRef Scalar, void *Ctx, UnsignedValue &Value) {
110     if (const auto *Node =
111             reinterpret_cast<yaml::Input *>(Ctx)->getCurrentNode())
112       Value.SourceRange = Node->getSourceRange();
113     return ScalarTraits<unsigned>::input(Scalar, Ctx, Value.Value);
114   }
115
116   static bool mustQuote(StringRef Scalar) {
117     return ScalarTraits<unsigned>::mustQuote(Scalar);
118   }
119 };
120
121 template <> struct ScalarEnumerationTraits<MachineJumpTableInfo::JTEntryKind> {
122   static void enumeration(yaml::IO &IO,
123                           MachineJumpTableInfo::JTEntryKind &EntryKind) {
124     IO.enumCase(EntryKind, "block-address",
125                 MachineJumpTableInfo::EK_BlockAddress);
126     IO.enumCase(EntryKind, "gp-rel64-block-address",
127                 MachineJumpTableInfo::EK_GPRel64BlockAddress);
128     IO.enumCase(EntryKind, "gp-rel32-block-address",
129                 MachineJumpTableInfo::EK_GPRel32BlockAddress);
130     IO.enumCase(EntryKind, "label-difference32",
131                 MachineJumpTableInfo::EK_LabelDifference32);
132     IO.enumCase(EntryKind, "inline", MachineJumpTableInfo::EK_Inline);
133     IO.enumCase(EntryKind, "custom32", MachineJumpTableInfo::EK_Custom32);
134   }
135 };
136
137 } // end namespace yaml
138 } // end namespace llvm
139
140 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::StringValue)
141 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::FlowStringValue)
142 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::UnsignedValue)
143
144 namespace llvm {
145 namespace yaml {
146
147 struct VirtualRegisterDefinition {
148   UnsignedValue ID;
149   StringValue Class;
150   StringValue PreferredRegister;
151   // TODO: Serialize the target specific register hints.
152 };
153
154 template <> struct MappingTraits<VirtualRegisterDefinition> {
155   static void mapping(IO &YamlIO, VirtualRegisterDefinition &Reg) {
156     YamlIO.mapRequired("id", Reg.ID);
157     YamlIO.mapRequired("class", Reg.Class);
158     YamlIO.mapOptional("preferred-register", Reg.PreferredRegister,
159                        StringValue()); // Don't print out when it's empty.
160   }
161
162   static const bool flow = true;
163 };
164
165 struct MachineFunctionLiveIn {
166   StringValue Register;
167   StringValue VirtualRegister;
168 };
169
170 template <> struct MappingTraits<MachineFunctionLiveIn> {
171   static void mapping(IO &YamlIO, MachineFunctionLiveIn &LiveIn) {
172     YamlIO.mapRequired("reg", LiveIn.Register);
173     YamlIO.mapOptional(
174         "virtual-reg", LiveIn.VirtualRegister,
175         StringValue()); // Don't print the virtual register when it's empty.
176   }
177
178   static const bool flow = true;
179 };
180
181 /// Serializable representation of stack object from the MachineFrameInfo class.
182 ///
183 /// The flags 'isImmutable' and 'isAliased' aren't serialized, as they are
184 /// determined by the object's type and frame information flags.
185 /// Dead stack objects aren't serialized.
186 ///
187 /// TODO: Determine isPreallocated flag by mapping between objects and local
188 /// objects (Serialize local objects).
189 struct MachineStackObject {
190   enum ObjectType { DefaultType, SpillSlot, VariableSized };
191   UnsignedValue ID;
192   StringValue Name;
193   // TODO: Serialize unnamed LLVM alloca reference.
194   ObjectType Type = DefaultType;
195   int64_t Offset = 0;
196   uint64_t Size = 0;
197   unsigned Alignment = 0;
198   StringValue CalleeSavedRegister;
199 };
200
201 template <> struct ScalarEnumerationTraits<MachineStackObject::ObjectType> {
202   static void enumeration(yaml::IO &IO, MachineStackObject::ObjectType &Type) {
203     IO.enumCase(Type, "default", MachineStackObject::DefaultType);
204     IO.enumCase(Type, "spill-slot", MachineStackObject::SpillSlot);
205     IO.enumCase(Type, "variable-sized", MachineStackObject::VariableSized);
206   }
207 };
208
209 template <> struct MappingTraits<MachineStackObject> {
210   static void mapping(yaml::IO &YamlIO, MachineStackObject &Object) {
211     YamlIO.mapRequired("id", Object.ID);
212     YamlIO.mapOptional("name", Object.Name,
213                        StringValue()); // Don't print out an empty name.
214     YamlIO.mapOptional(
215         "type", Object.Type,
216         MachineStackObject::DefaultType); // Don't print the default type.
217     YamlIO.mapOptional("offset", Object.Offset);
218     if (Object.Type != MachineStackObject::VariableSized)
219       YamlIO.mapRequired("size", Object.Size);
220     YamlIO.mapOptional("alignment", Object.Alignment);
221     YamlIO.mapOptional("callee-saved-register", Object.CalleeSavedRegister,
222                        StringValue()); // Don't print it out when it's empty.
223   }
224
225   static const bool flow = true;
226 };
227
228 /// Serializable representation of the fixed stack object from the
229 /// MachineFrameInfo class.
230 struct FixedMachineStackObject {
231   enum ObjectType { DefaultType, SpillSlot };
232   UnsignedValue ID;
233   ObjectType Type = DefaultType;
234   int64_t Offset = 0;
235   uint64_t Size = 0;
236   unsigned Alignment = 0;
237   bool IsImmutable = false;
238   bool IsAliased = false;
239   StringValue CalleeSavedRegister;
240 };
241
242 template <>
243 struct ScalarEnumerationTraits<FixedMachineStackObject::ObjectType> {
244   static void enumeration(yaml::IO &IO,
245                           FixedMachineStackObject::ObjectType &Type) {
246     IO.enumCase(Type, "default", FixedMachineStackObject::DefaultType);
247     IO.enumCase(Type, "spill-slot", FixedMachineStackObject::SpillSlot);
248   }
249 };
250
251 template <> struct MappingTraits<FixedMachineStackObject> {
252   static void mapping(yaml::IO &YamlIO, FixedMachineStackObject &Object) {
253     YamlIO.mapRequired("id", Object.ID);
254     YamlIO.mapOptional(
255         "type", Object.Type,
256         FixedMachineStackObject::DefaultType); // Don't print the default type.
257     YamlIO.mapOptional("offset", Object.Offset);
258     YamlIO.mapOptional("size", Object.Size);
259     YamlIO.mapOptional("alignment", Object.Alignment);
260     if (Object.Type != FixedMachineStackObject::SpillSlot) {
261       YamlIO.mapOptional("isImmutable", Object.IsImmutable);
262       YamlIO.mapOptional("isAliased", Object.IsAliased);
263     }
264     YamlIO.mapOptional("callee-saved-register", Object.CalleeSavedRegister,
265                        StringValue()); // Don't print it out when it's empty.
266   }
267
268   static const bool flow = true;
269 };
270
271 struct MachineConstantPoolValue {
272   UnsignedValue ID;
273   StringValue Value;
274   unsigned Alignment = 0;
275 };
276
277 template <> struct MappingTraits<MachineConstantPoolValue> {
278   static void mapping(IO &YamlIO, MachineConstantPoolValue &Constant) {
279     YamlIO.mapRequired("id", Constant.ID);
280     YamlIO.mapOptional("value", Constant.Value);
281     YamlIO.mapOptional("alignment", Constant.Alignment);
282   }
283 };
284
285 struct MachineJumpTable {
286   struct Entry {
287     UnsignedValue ID;
288     std::vector<FlowStringValue> Blocks;
289   };
290
291   MachineJumpTableInfo::JTEntryKind Kind = MachineJumpTableInfo::EK_Custom32;
292   std::vector<Entry> Entries;
293 };
294
295 template <> struct MappingTraits<MachineJumpTable::Entry> {
296   static void mapping(IO &YamlIO, MachineJumpTable::Entry &Entry) {
297     YamlIO.mapRequired("id", Entry.ID);
298     YamlIO.mapOptional("blocks", Entry.Blocks);
299   }
300 };
301
302 } // end namespace yaml
303 } // end namespace llvm
304
305 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineFunctionLiveIn)
306 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::VirtualRegisterDefinition)
307 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineStackObject)
308 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::FixedMachineStackObject)
309 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineConstantPoolValue)
310 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineJumpTable::Entry)
311
312 namespace llvm {
313 namespace yaml {
314
315 template <> struct MappingTraits<MachineJumpTable> {
316   static void mapping(IO &YamlIO, MachineJumpTable &JT) {
317     YamlIO.mapRequired("kind", JT.Kind);
318     YamlIO.mapOptional("entries", JT.Entries);
319   }
320 };
321
322 /// Serializable representation of MachineFrameInfo.
323 ///
324 /// Doesn't serialize attributes like 'StackAlignment', 'IsStackRealignable' and
325 /// 'RealignOption' as they are determined by the target and LLVM function
326 /// attributes.
327 /// It also doesn't serialize attributes like 'NumFixedObject' and
328 /// 'HasVarSizedObjects' as they are determined by the frame objects themselves.
329 struct MachineFrameInfo {
330   bool IsFrameAddressTaken = false;
331   bool IsReturnAddressTaken = false;
332   bool HasStackMap = false;
333   bool HasPatchPoint = false;
334   uint64_t StackSize = 0;
335   int OffsetAdjustment = 0;
336   unsigned MaxAlignment = 0;
337   bool AdjustsStack = false;
338   bool HasCalls = false;
339   // TODO: Serialize StackProtectorIdx and FunctionContextIdx
340   unsigned MaxCallFrameSize = 0;
341   // TODO: Serialize local frame objects.
342   bool HasOpaqueSPAdjustment = false;
343   bool HasVAStart = false;
344   bool HasMustTailInVarArgFunc = false;
345   StringValue SavePoint;
346   StringValue RestorePoint;
347 };
348
349 template <> struct MappingTraits<MachineFrameInfo> {
350   static void mapping(IO &YamlIO, MachineFrameInfo &MFI) {
351     YamlIO.mapOptional("isFrameAddressTaken", MFI.IsFrameAddressTaken);
352     YamlIO.mapOptional("isReturnAddressTaken", MFI.IsReturnAddressTaken);
353     YamlIO.mapOptional("hasStackMap", MFI.HasStackMap);
354     YamlIO.mapOptional("hasPatchPoint", MFI.HasPatchPoint);
355     YamlIO.mapOptional("stackSize", MFI.StackSize);
356     YamlIO.mapOptional("offsetAdjustment", MFI.OffsetAdjustment);
357     YamlIO.mapOptional("maxAlignment", MFI.MaxAlignment);
358     YamlIO.mapOptional("adjustsStack", MFI.AdjustsStack);
359     YamlIO.mapOptional("hasCalls", MFI.HasCalls);
360     YamlIO.mapOptional("maxCallFrameSize", MFI.MaxCallFrameSize);
361     YamlIO.mapOptional("hasOpaqueSPAdjustment", MFI.HasOpaqueSPAdjustment);
362     YamlIO.mapOptional("hasVAStart", MFI.HasVAStart);
363     YamlIO.mapOptional("hasMustTailInVarArgFunc", MFI.HasMustTailInVarArgFunc);
364     YamlIO.mapOptional("savePoint", MFI.SavePoint,
365                        StringValue()); // Don't print it out when it's empty.
366     YamlIO.mapOptional("restorePoint", MFI.RestorePoint,
367                        StringValue()); // Don't print it out when it's empty.
368   }
369 };
370
371 struct MachineFunction {
372   StringRef Name;
373   unsigned Alignment = 0;
374   bool ExposesReturnsTwice = false;
375   bool HasInlineAsm = false;
376   // Register information
377   bool IsSSA = false;
378   bool TracksRegLiveness = false;
379   bool TracksSubRegLiveness = false;
380   std::vector<VirtualRegisterDefinition> VirtualRegisters;
381   std::vector<MachineFunctionLiveIn> LiveIns;
382   Optional<std::vector<FlowStringValue>> CalleeSavedRegisters;
383   // TODO: Serialize the various register masks.
384   // Frame information
385   MachineFrameInfo FrameInfo;
386   std::vector<FixedMachineStackObject> FixedStackObjects;
387   std::vector<MachineStackObject> StackObjects;
388   std::vector<MachineConstantPoolValue> Constants; /// Constant pool.
389   MachineJumpTable JumpTableInfo;
390   BlockStringValue Body;
391 };
392
393 template <> struct MappingTraits<MachineFunction> {
394   static void mapping(IO &YamlIO, MachineFunction &MF) {
395     YamlIO.mapRequired("name", MF.Name);
396     YamlIO.mapOptional("alignment", MF.Alignment);
397     YamlIO.mapOptional("exposesReturnsTwice", MF.ExposesReturnsTwice);
398     YamlIO.mapOptional("hasInlineAsm", MF.HasInlineAsm);
399     YamlIO.mapOptional("isSSA", MF.IsSSA);
400     YamlIO.mapOptional("tracksRegLiveness", MF.TracksRegLiveness);
401     YamlIO.mapOptional("tracksSubRegLiveness", MF.TracksSubRegLiveness);
402     YamlIO.mapOptional("registers", MF.VirtualRegisters);
403     YamlIO.mapOptional("liveins", MF.LiveIns);
404     YamlIO.mapOptional("calleeSavedRegisters", MF.CalleeSavedRegisters);
405     YamlIO.mapOptional("frameInfo", MF.FrameInfo);
406     YamlIO.mapOptional("fixedStack", MF.FixedStackObjects);
407     YamlIO.mapOptional("stack", MF.StackObjects);
408     YamlIO.mapOptional("constants", MF.Constants);
409     if (!YamlIO.outputting() || !MF.JumpTableInfo.Entries.empty())
410       YamlIO.mapOptional("jumpTable", MF.JumpTableInfo);
411     YamlIO.mapOptional("body", MF.Body);
412   }
413 };
414
415 } // end namespace yaml
416 } // end namespace llvm
417
418 #endif