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/CodeGen/MachineJumpTableInfo.h"
23 #include "llvm/Support/YAMLTraits.h"
29 /// A wrapper around std::string which contains a source range that's being
30 /// set during parsing.
36 StringValue(std::string Value) : Value(std::move(Value)) {}
38 bool operator==(const StringValue &Other) const {
39 return Value == Other.Value;
43 template <> struct ScalarTraits<StringValue> {
44 static void output(const StringValue &S, void *, llvm::raw_ostream &OS) {
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();
56 static bool mustQuote(StringRef Scalar) { return needsQuotes(Scalar); }
59 struct FlowStringValue : StringValue {
61 FlowStringValue(std::string Value) : StringValue(Value) {}
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);
69 static StringRef input(StringRef Scalar, void *Ctx, FlowStringValue &S) {
70 return ScalarTraits<StringValue>::input(Scalar, Ctx, S);
73 static bool mustQuote(StringRef Scalar) { return needsQuotes(Scalar); }
76 template <> struct ScalarEnumerationTraits<MachineJumpTableInfo::JTEntryKind> {
77 static void enumeration(yaml::IO &IO,
78 MachineJumpTableInfo::JTEntryKind &EntryKind) {
79 IO.enumCase(EntryKind, "block-address",
80 MachineJumpTableInfo::EK_BlockAddress);
81 IO.enumCase(EntryKind, "gp-rel64-block-address",
82 MachineJumpTableInfo::EK_GPRel64BlockAddress);
83 IO.enumCase(EntryKind, "gp-rel32-block-address",
84 MachineJumpTableInfo::EK_GPRel32BlockAddress);
85 IO.enumCase(EntryKind, "label-difference32",
86 MachineJumpTableInfo::EK_LabelDifference32);
87 IO.enumCase(EntryKind, "inline", MachineJumpTableInfo::EK_Inline);
88 IO.enumCase(EntryKind, "custom32", MachineJumpTableInfo::EK_Custom32);
92 } // end namespace yaml
93 } // end namespace llvm
95 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::StringValue)
96 LLVM_YAML_IS_FLOW_SEQUENCE_VECTOR(llvm::yaml::FlowStringValue)
101 struct VirtualRegisterDefinition {
104 StringValue PreferredRegister;
105 // TODO: Serialize the target specific register hints.
108 template <> struct MappingTraits<VirtualRegisterDefinition> {
109 static void mapping(IO &YamlIO, VirtualRegisterDefinition &Reg) {
110 YamlIO.mapRequired("id", Reg.ID);
111 YamlIO.mapRequired("class", Reg.Class);
112 YamlIO.mapOptional("preferred-register", Reg.PreferredRegister,
113 StringValue()); // Don't print out when it's empty.
116 static const bool flow = true;
119 struct MachineBasicBlock {
122 unsigned Alignment = 0;
123 bool IsLandingPad = false;
124 bool AddressTaken = false;
125 // TODO: Serialize the successor weights.
126 std::vector<FlowStringValue> Successors;
127 std::vector<FlowStringValue> LiveIns;
128 std::vector<StringValue> Instructions;
131 template <> struct MappingTraits<MachineBasicBlock> {
132 static void mapping(IO &YamlIO, MachineBasicBlock &MBB) {
133 YamlIO.mapRequired("id", MBB.ID);
134 YamlIO.mapOptional("name", MBB.Name,
135 StringValue()); // Don't print out an empty name.
136 YamlIO.mapOptional("alignment", MBB.Alignment);
137 YamlIO.mapOptional("isLandingPad", MBB.IsLandingPad);
138 YamlIO.mapOptional("addressTaken", MBB.AddressTaken);
139 YamlIO.mapOptional("successors", MBB.Successors);
140 YamlIO.mapOptional("liveins", MBB.LiveIns);
141 YamlIO.mapOptional("instructions", MBB.Instructions);
145 /// Serializable representation of stack object from the MachineFrameInfo class.
147 /// The flags 'isImmutable' and 'isAliased' aren't serialized, as they are
148 /// determined by the object's type and frame information flags.
149 /// Dead stack objects aren't serialized.
151 /// TODO: Determine isPreallocated flag by mapping between objects and local
152 /// objects (Serialize local objects).
153 struct MachineStackObject {
154 enum ObjectType { DefaultType, SpillSlot, VariableSized };
157 // TODO: Serialize unnamed LLVM alloca reference.
158 ObjectType Type = DefaultType;
161 unsigned Alignment = 0;
162 StringValue CalleeSavedRegister;
165 template <> struct ScalarEnumerationTraits<MachineStackObject::ObjectType> {
166 static void enumeration(yaml::IO &IO, MachineStackObject::ObjectType &Type) {
167 IO.enumCase(Type, "default", MachineStackObject::DefaultType);
168 IO.enumCase(Type, "spill-slot", MachineStackObject::SpillSlot);
169 IO.enumCase(Type, "variable-sized", MachineStackObject::VariableSized);
173 template <> struct MappingTraits<MachineStackObject> {
174 static void mapping(yaml::IO &YamlIO, MachineStackObject &Object) {
175 YamlIO.mapRequired("id", Object.ID);
176 YamlIO.mapOptional("name", Object.Name,
177 StringValue()); // Don't print out an empty name.
180 MachineStackObject::DefaultType); // Don't print the default type.
181 YamlIO.mapOptional("offset", Object.Offset);
182 if (Object.Type != MachineStackObject::VariableSized)
183 YamlIO.mapRequired("size", Object.Size);
184 YamlIO.mapOptional("alignment", Object.Alignment);
185 YamlIO.mapOptional("callee-saved-register", Object.CalleeSavedRegister,
186 StringValue()); // Don't print it out when it's empty.
189 static const bool flow = true;
192 /// Serializable representation of the fixed stack object from the
193 /// MachineFrameInfo class.
194 struct FixedMachineStackObject {
195 enum ObjectType { DefaultType, SpillSlot };
197 ObjectType Type = DefaultType;
200 unsigned Alignment = 0;
201 bool IsImmutable = false;
202 bool IsAliased = false;
203 StringValue CalleeSavedRegister;
207 struct ScalarEnumerationTraits<FixedMachineStackObject::ObjectType> {
208 static void enumeration(yaml::IO &IO,
209 FixedMachineStackObject::ObjectType &Type) {
210 IO.enumCase(Type, "default", FixedMachineStackObject::DefaultType);
211 IO.enumCase(Type, "spill-slot", FixedMachineStackObject::SpillSlot);
215 template <> struct MappingTraits<FixedMachineStackObject> {
216 static void mapping(yaml::IO &YamlIO, FixedMachineStackObject &Object) {
217 YamlIO.mapRequired("id", Object.ID);
220 FixedMachineStackObject::DefaultType); // Don't print the default type.
221 YamlIO.mapOptional("offset", Object.Offset);
222 YamlIO.mapOptional("size", Object.Size);
223 YamlIO.mapOptional("alignment", Object.Alignment);
224 if (Object.Type != FixedMachineStackObject::SpillSlot) {
225 YamlIO.mapOptional("isImmutable", Object.IsImmutable);
226 YamlIO.mapOptional("isAliased", Object.IsAliased);
228 YamlIO.mapOptional("callee-saved-register", Object.CalleeSavedRegister,
229 StringValue()); // Don't print it out when it's empty.
232 static const bool flow = true;
235 struct MachineConstantPoolValue {
238 unsigned Alignment = 0;
241 template <> struct MappingTraits<MachineConstantPoolValue> {
242 static void mapping(IO &YamlIO, MachineConstantPoolValue &Constant) {
243 YamlIO.mapRequired("id", Constant.ID);
244 YamlIO.mapOptional("value", Constant.Value);
245 YamlIO.mapOptional("alignment", Constant.Alignment);
249 struct MachineJumpTable {
252 std::vector<FlowStringValue> Blocks;
255 MachineJumpTableInfo::JTEntryKind Kind = MachineJumpTableInfo::EK_Custom32;
256 std::vector<Entry> Entries;
259 template <> struct MappingTraits<MachineJumpTable::Entry> {
260 static void mapping(IO &YamlIO, MachineJumpTable::Entry &Entry) {
261 YamlIO.mapRequired("id", Entry.ID);
262 YamlIO.mapOptional("blocks", Entry.Blocks);
266 } // end namespace yaml
267 } // end namespace llvm
269 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::VirtualRegisterDefinition)
270 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineBasicBlock)
271 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineStackObject)
272 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::FixedMachineStackObject)
273 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineConstantPoolValue)
274 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineJumpTable::Entry)
279 template <> struct MappingTraits<MachineJumpTable> {
280 static void mapping(IO &YamlIO, MachineJumpTable &JT) {
281 YamlIO.mapRequired("kind", JT.Kind);
282 YamlIO.mapOptional("entries", JT.Entries);
286 /// Serializable representation of MachineFrameInfo.
288 /// Doesn't serialize attributes like 'StackAlignment', 'IsStackRealignable' and
289 /// 'RealignOption' as they are determined by the target and LLVM function
291 /// It also doesn't serialize attributes like 'NumFixedObject' and
292 /// 'HasVarSizedObjects' as they are determined by the frame objects themselves.
293 struct MachineFrameInfo {
294 bool IsFrameAddressTaken = false;
295 bool IsReturnAddressTaken = false;
296 bool HasStackMap = false;
297 bool HasPatchPoint = false;
298 uint64_t StackSize = 0;
299 int OffsetAdjustment = 0;
300 unsigned MaxAlignment = 0;
301 bool AdjustsStack = false;
302 bool HasCalls = false;
303 // TODO: Serialize StackProtectorIdx and FunctionContextIdx
304 unsigned MaxCallFrameSize = 0;
305 // TODO: Serialize local frame objects.
306 bool HasOpaqueSPAdjustment = false;
307 bool HasVAStart = false;
308 bool HasMustTailInVarArgFunc = false;
309 // TODO: Serialize save and restore MBB references.
312 template <> struct MappingTraits<MachineFrameInfo> {
313 static void mapping(IO &YamlIO, MachineFrameInfo &MFI) {
314 YamlIO.mapOptional("isFrameAddressTaken", MFI.IsFrameAddressTaken);
315 YamlIO.mapOptional("isReturnAddressTaken", MFI.IsReturnAddressTaken);
316 YamlIO.mapOptional("hasStackMap", MFI.HasStackMap);
317 YamlIO.mapOptional("hasPatchPoint", MFI.HasPatchPoint);
318 YamlIO.mapOptional("stackSize", MFI.StackSize);
319 YamlIO.mapOptional("offsetAdjustment", MFI.OffsetAdjustment);
320 YamlIO.mapOptional("maxAlignment", MFI.MaxAlignment);
321 YamlIO.mapOptional("adjustsStack", MFI.AdjustsStack);
322 YamlIO.mapOptional("hasCalls", MFI.HasCalls);
323 YamlIO.mapOptional("maxCallFrameSize", MFI.MaxCallFrameSize);
324 YamlIO.mapOptional("hasOpaqueSPAdjustment", MFI.HasOpaqueSPAdjustment);
325 YamlIO.mapOptional("hasVAStart", MFI.HasVAStart);
326 YamlIO.mapOptional("hasMustTailInVarArgFunc", MFI.HasMustTailInVarArgFunc);
330 struct MachineFunction {
332 unsigned Alignment = 0;
333 bool ExposesReturnsTwice = false;
334 bool HasInlineAsm = false;
335 // Register information
337 bool TracksRegLiveness = false;
338 bool TracksSubRegLiveness = false;
339 std::vector<VirtualRegisterDefinition> VirtualRegisters;
340 // TODO: Serialize the various register masks.
341 // TODO: Serialize live in registers.
343 MachineFrameInfo FrameInfo;
344 std::vector<FixedMachineStackObject> FixedStackObjects;
345 std::vector<MachineStackObject> StackObjects;
346 std::vector<MachineConstantPoolValue> Constants; /// Constant pool.
347 MachineJumpTable JumpTableInfo;
349 std::vector<MachineBasicBlock> BasicBlocks;
352 template <> struct MappingTraits<MachineFunction> {
353 static void mapping(IO &YamlIO, MachineFunction &MF) {
354 YamlIO.mapRequired("name", MF.Name);
355 YamlIO.mapOptional("alignment", MF.Alignment);
356 YamlIO.mapOptional("exposesReturnsTwice", MF.ExposesReturnsTwice);
357 YamlIO.mapOptional("hasInlineAsm", MF.HasInlineAsm);
358 YamlIO.mapOptional("isSSA", MF.IsSSA);
359 YamlIO.mapOptional("tracksRegLiveness", MF.TracksRegLiveness);
360 YamlIO.mapOptional("tracksSubRegLiveness", MF.TracksSubRegLiveness);
361 YamlIO.mapOptional("registers", MF.VirtualRegisters);
362 YamlIO.mapOptional("frameInfo", MF.FrameInfo);
363 YamlIO.mapOptional("fixedStack", MF.FixedStackObjects);
364 YamlIO.mapOptional("stack", MF.StackObjects);
365 YamlIO.mapOptional("constants", MF.Constants);
366 if (!YamlIO.outputting() || !MF.JumpTableInfo.Entries.empty())
367 YamlIO.mapOptional("jumpTable", MF.JumpTableInfo);
368 YamlIO.mapOptional("body", MF.BasicBlocks);
372 } // end namespace yaml
373 } // end namespace llvm