MIR Serialization: Serialize the local offsets for the stack objects.
[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 /// The 'isPreallocated' flag is determined by the local offset.
188 struct MachineStackObject {
189   enum ObjectType { DefaultType, SpillSlot, VariableSized };
190   UnsignedValue ID;
191   StringValue Name;
192   // TODO: Serialize unnamed LLVM alloca reference.
193   ObjectType Type = DefaultType;
194   int64_t Offset = 0;
195   uint64_t Size = 0;
196   unsigned Alignment = 0;
197   StringValue CalleeSavedRegister;
198   Optional<int64_t> LocalOffset;
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     YamlIO.mapOptional("local-offset", Object.LocalOffset);
224   }
225
226   static const bool flow = true;
227 };
228
229 /// Serializable representation of the fixed stack object from the
230 /// MachineFrameInfo class.
231 struct FixedMachineStackObject {
232   enum ObjectType { DefaultType, SpillSlot };
233   UnsignedValue ID;
234   ObjectType Type = DefaultType;
235   int64_t Offset = 0;
236   uint64_t Size = 0;
237   unsigned Alignment = 0;
238   bool IsImmutable = false;
239   bool IsAliased = false;
240   StringValue CalleeSavedRegister;
241 };
242
243 template <>
244 struct ScalarEnumerationTraits<FixedMachineStackObject::ObjectType> {
245   static void enumeration(yaml::IO &IO,
246                           FixedMachineStackObject::ObjectType &Type) {
247     IO.enumCase(Type, "default", FixedMachineStackObject::DefaultType);
248     IO.enumCase(Type, "spill-slot", FixedMachineStackObject::SpillSlot);
249   }
250 };
251
252 template <> struct MappingTraits<FixedMachineStackObject> {
253   static void mapping(yaml::IO &YamlIO, FixedMachineStackObject &Object) {
254     YamlIO.mapRequired("id", Object.ID);
255     YamlIO.mapOptional(
256         "type", Object.Type,
257         FixedMachineStackObject::DefaultType); // Don't print the default type.
258     YamlIO.mapOptional("offset", Object.Offset);
259     YamlIO.mapOptional("size", Object.Size);
260     YamlIO.mapOptional("alignment", Object.Alignment);
261     if (Object.Type != FixedMachineStackObject::SpillSlot) {
262       YamlIO.mapOptional("isImmutable", Object.IsImmutable);
263       YamlIO.mapOptional("isAliased", Object.IsAliased);
264     }
265     YamlIO.mapOptional("callee-saved-register", Object.CalleeSavedRegister,
266                        StringValue()); // Don't print it out when it's empty.
267   }
268
269   static const bool flow = true;
270 };
271
272 struct MachineConstantPoolValue {
273   UnsignedValue ID;
274   StringValue Value;
275   unsigned Alignment = 0;
276 };
277
278 template <> struct MappingTraits<MachineConstantPoolValue> {
279   static void mapping(IO &YamlIO, MachineConstantPoolValue &Constant) {
280     YamlIO.mapRequired("id", Constant.ID);
281     YamlIO.mapOptional("value", Constant.Value);
282     YamlIO.mapOptional("alignment", Constant.Alignment);
283   }
284 };
285
286 struct MachineJumpTable {
287   struct Entry {
288     UnsignedValue ID;
289     std::vector<FlowStringValue> Blocks;
290   };
291
292   MachineJumpTableInfo::JTEntryKind Kind = MachineJumpTableInfo::EK_Custom32;
293   std::vector<Entry> Entries;
294 };
295
296 template <> struct MappingTraits<MachineJumpTable::Entry> {
297   static void mapping(IO &YamlIO, MachineJumpTable::Entry &Entry) {
298     YamlIO.mapRequired("id", Entry.ID);
299     YamlIO.mapOptional("blocks", Entry.Blocks);
300   }
301 };
302
303 } // end namespace yaml
304 } // end namespace llvm
305
306 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineFunctionLiveIn)
307 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::VirtualRegisterDefinition)
308 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineStackObject)
309 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::FixedMachineStackObject)
310 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineConstantPoolValue)
311 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineJumpTable::Entry)
312
313 namespace llvm {
314 namespace yaml {
315
316 template <> struct MappingTraits<MachineJumpTable> {
317   static void mapping(IO &YamlIO, MachineJumpTable &JT) {
318     YamlIO.mapRequired("kind", JT.Kind);
319     YamlIO.mapOptional("entries", JT.Entries);
320   }
321 };
322
323 /// Serializable representation of MachineFrameInfo.
324 ///
325 /// Doesn't serialize attributes like 'StackAlignment', 'IsStackRealignable' and
326 /// 'RealignOption' as they are determined by the target and LLVM function
327 /// attributes.
328 /// It also doesn't serialize attributes like 'NumFixedObject' and
329 /// 'HasVarSizedObjects' as they are determined by the frame objects themselves.
330 struct MachineFrameInfo {
331   bool IsFrameAddressTaken = false;
332   bool IsReturnAddressTaken = false;
333   bool HasStackMap = false;
334   bool HasPatchPoint = false;
335   uint64_t StackSize = 0;
336   int OffsetAdjustment = 0;
337   unsigned MaxAlignment = 0;
338   bool AdjustsStack = false;
339   bool HasCalls = false;
340   // TODO: Serialize StackProtectorIdx and FunctionContextIdx
341   unsigned MaxCallFrameSize = 0;
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