MIR Serialization: Serialize the fixed 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/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 struct FlowStringValue : StringValue {
59   FlowStringValue() {}
60   FlowStringValue(std::string Value) : StringValue(Value) {}
61 };
62
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);
66   }
67
68   static StringRef input(StringRef Scalar, void *Ctx, FlowStringValue &S) {
69     return ScalarTraits<StringValue>::input(Scalar, Ctx, S);
70   }
71
72   static bool mustQuote(StringRef Scalar) { return needsQuotes(Scalar); }
73 };
74
75 } // end namespace yaml
76 } // end namespace llvm
77
78 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::StringValue)
79 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::FlowStringValue)
80
81 namespace llvm {
82 namespace yaml {
83
84 struct VirtualRegisterDefinition {
85   unsigned ID;
86   StringValue Class;
87   // TODO: Serialize the virtual register hints.
88 };
89
90 template <> struct MappingTraits<VirtualRegisterDefinition> {
91   static void mapping(IO &YamlIO, VirtualRegisterDefinition &Reg) {
92     YamlIO.mapRequired("id", Reg.ID);
93     YamlIO.mapRequired("class", Reg.Class);
94   }
95
96   static const bool flow = true;
97 };
98
99 struct MachineBasicBlock {
100   unsigned ID;
101   StringValue Name;
102   unsigned Alignment = 0;
103   bool IsLandingPad = false;
104   bool AddressTaken = false;
105   // TODO: Serialize the successor weights and liveins.
106   std::vector<FlowStringValue> Successors;
107
108   std::vector<StringValue> Instructions;
109 };
110
111 template <> struct MappingTraits<MachineBasicBlock> {
112   static void mapping(IO &YamlIO, MachineBasicBlock &MBB) {
113     YamlIO.mapRequired("id", MBB.ID);
114     YamlIO.mapOptional("name", MBB.Name,
115                        StringValue()); // Don't print out an empty name.
116     YamlIO.mapOptional("alignment", MBB.Alignment);
117     YamlIO.mapOptional("isLandingPad", MBB.IsLandingPad);
118     YamlIO.mapOptional("addressTaken", MBB.AddressTaken);
119     YamlIO.mapOptional("successors", MBB.Successors);
120     YamlIO.mapOptional("instructions", MBB.Instructions);
121   }
122 };
123
124 /// Serializable representation of stack object from the MachineFrameInfo class.
125 ///
126 /// The flags 'isImmutable' and 'isAliased' aren't serialized, as they are
127 /// determined by the object's type and frame information flags.
128 /// Dead stack objects aren't serialized.
129 ///
130 /// TODO: Determine isPreallocated flag by mapping between objects and local
131 /// objects (Serialize local objects).
132 /// TODO: Serialize variable sized objects.
133 struct MachineStackObject {
134   enum ObjectType { DefaultType, SpillSlot };
135   // TODO: Serialize LLVM alloca reference.
136   unsigned ID;
137   ObjectType Type = DefaultType;
138   int64_t Offset = 0;
139   uint64_t Size = 0;
140   unsigned Alignment = 0;
141 };
142
143 template <> struct ScalarEnumerationTraits<MachineStackObject::ObjectType> {
144   static void enumeration(yaml::IO &IO, MachineStackObject::ObjectType &Type) {
145     IO.enumCase(Type, "default", MachineStackObject::DefaultType);
146     IO.enumCase(Type, "spill-slot", MachineStackObject::SpillSlot);
147   }
148 };
149
150 template <> struct MappingTraits<MachineStackObject> {
151   static void mapping(yaml::IO &YamlIO, MachineStackObject &Object) {
152     YamlIO.mapRequired("id", Object.ID);
153     YamlIO.mapOptional(
154         "type", Object.Type,
155         MachineStackObject::DefaultType); // Don't print the default type.
156     YamlIO.mapOptional("offset", Object.Offset);
157     YamlIO.mapRequired("size", Object.Size);
158     YamlIO.mapOptional("alignment", Object.Alignment);
159   }
160
161   static const bool flow = true;
162 };
163
164 /// Serializable representation of the fixed stack object from the
165 /// MachineFrameInfo class.
166 struct FixedMachineStackObject {
167   enum ObjectType { DefaultType, SpillSlot };
168   unsigned ID;
169   ObjectType Type = DefaultType;
170   int64_t Offset = 0;
171   uint64_t Size = 0;
172   unsigned Alignment = 0;
173   bool IsImmutable = false;
174   bool IsAliased = false;
175 };
176
177 template <>
178 struct ScalarEnumerationTraits<FixedMachineStackObject::ObjectType> {
179   static void enumeration(yaml::IO &IO,
180                           FixedMachineStackObject::ObjectType &Type) {
181     IO.enumCase(Type, "default", FixedMachineStackObject::DefaultType);
182     IO.enumCase(Type, "spill-slot", FixedMachineStackObject::SpillSlot);
183   }
184 };
185
186 template <> struct MappingTraits<FixedMachineStackObject> {
187   static void mapping(yaml::IO &YamlIO, FixedMachineStackObject &Object) {
188     YamlIO.mapRequired("id", Object.ID);
189     YamlIO.mapOptional(
190         "type", Object.Type,
191         FixedMachineStackObject::DefaultType); // Don't print the default type.
192     YamlIO.mapOptional("offset", Object.Offset);
193     YamlIO.mapOptional("size", Object.Size);
194     YamlIO.mapOptional("alignment", Object.Alignment);
195     if (Object.Type != FixedMachineStackObject::SpillSlot) {
196       YamlIO.mapOptional("isImmutable", Object.IsImmutable);
197       YamlIO.mapOptional("isAliased", Object.IsAliased);
198     }
199   }
200
201   static const bool flow = true;
202 };
203
204 } // end namespace yaml
205 } // end namespace llvm
206
207 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::VirtualRegisterDefinition)
208 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineBasicBlock)
209 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineStackObject)
210 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::FixedMachineStackObject)
211
212 namespace llvm {
213 namespace yaml {
214
215 /// Serializable representation of MachineFrameInfo.
216 ///
217 /// Doesn't serialize attributes like 'StackAlignment', 'IsStackRealignable' and
218 /// 'RealignOption' as they are determined by the target and LLVM function
219 /// attributes.
220 /// It also doesn't serialize attributes like 'NumFixedObject' and
221 /// 'HasVarSizedObjects' as they are determined by the frame objects themselves.
222 struct MachineFrameInfo {
223   bool IsFrameAddressTaken = false;
224   bool IsReturnAddressTaken = false;
225   bool HasStackMap = false;
226   bool HasPatchPoint = false;
227   uint64_t StackSize = 0;
228   int OffsetAdjustment = 0;
229   unsigned MaxAlignment = 0;
230   bool AdjustsStack = false;
231   bool HasCalls = false;
232   // TODO: Serialize StackProtectorIdx and FunctionContextIdx
233   unsigned MaxCallFrameSize = 0;
234   // TODO: Serialize callee saved info.
235   // TODO: Serialize local frame objects.
236   bool HasOpaqueSPAdjustment = false;
237   bool HasVAStart = false;
238   bool HasMustTailInVarArgFunc = false;
239   // TODO: Serialize save and restore MBB references.
240 };
241
242 template <> struct MappingTraits<MachineFrameInfo> {
243   static void mapping(IO &YamlIO, MachineFrameInfo &MFI) {
244     YamlIO.mapOptional("isFrameAddressTaken", MFI.IsFrameAddressTaken);
245     YamlIO.mapOptional("isReturnAddressTaken", MFI.IsReturnAddressTaken);
246     YamlIO.mapOptional("hasStackMap", MFI.HasStackMap);
247     YamlIO.mapOptional("hasPatchPoint", MFI.HasPatchPoint);
248     YamlIO.mapOptional("stackSize", MFI.StackSize);
249     YamlIO.mapOptional("offsetAdjustment", MFI.OffsetAdjustment);
250     YamlIO.mapOptional("maxAlignment", MFI.MaxAlignment);
251     YamlIO.mapOptional("adjustsStack", MFI.AdjustsStack);
252     YamlIO.mapOptional("hasCalls", MFI.HasCalls);
253     YamlIO.mapOptional("maxCallFrameSize", MFI.MaxCallFrameSize);
254     YamlIO.mapOptional("hasOpaqueSPAdjustment", MFI.HasOpaqueSPAdjustment);
255     YamlIO.mapOptional("hasVAStart", MFI.HasVAStart);
256     YamlIO.mapOptional("hasMustTailInVarArgFunc", MFI.HasMustTailInVarArgFunc);
257   }
258 };
259
260 struct MachineFunction {
261   StringRef Name;
262   unsigned Alignment = 0;
263   bool ExposesReturnsTwice = false;
264   bool HasInlineAsm = false;
265   // Register information
266   bool IsSSA = false;
267   bool TracksRegLiveness = false;
268   bool TracksSubRegLiveness = false;
269   std::vector<VirtualRegisterDefinition> VirtualRegisters;
270   // TODO: Serialize the various register masks.
271   // TODO: Serialize live in registers.
272   // Frame information
273   MachineFrameInfo FrameInfo;
274   std::vector<FixedMachineStackObject> FixedStackObjects;
275   std::vector<MachineStackObject> StackObjects;
276
277   std::vector<MachineBasicBlock> BasicBlocks;
278 };
279
280 template <> struct MappingTraits<MachineFunction> {
281   static void mapping(IO &YamlIO, MachineFunction &MF) {
282     YamlIO.mapRequired("name", MF.Name);
283     YamlIO.mapOptional("alignment", MF.Alignment);
284     YamlIO.mapOptional("exposesReturnsTwice", MF.ExposesReturnsTwice);
285     YamlIO.mapOptional("hasInlineAsm", MF.HasInlineAsm);
286     YamlIO.mapOptional("isSSA", MF.IsSSA);
287     YamlIO.mapOptional("tracksRegLiveness", MF.TracksRegLiveness);
288     YamlIO.mapOptional("tracksSubRegLiveness", MF.TracksSubRegLiveness);
289     YamlIO.mapOptional("registers", MF.VirtualRegisters);
290     YamlIO.mapOptional("frameInfo", MF.FrameInfo);
291     YamlIO.mapOptional("fixedStack", MF.FixedStackObjects);
292     YamlIO.mapOptional("stack", MF.StackObjects);
293     YamlIO.mapOptional("body", MF.BasicBlocks);
294   }
295 };
296
297 } // end namespace yaml
298 } // end namespace llvm
299
300 #endif