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 VirtualRegisterDefinition {
87 // TODO: Serialize the virtual register hints.
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);
96 static const bool flow = true;
99 struct MachineBasicBlock {
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;
108 std::vector<StringValue> Instructions;
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);
124 /// Serializable representation of stack object from the MachineFrameInfo class.
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.
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.
137 ObjectType Type = DefaultType;
140 unsigned Alignment = 0;
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);
150 template <> struct MappingTraits<MachineStackObject> {
151 static void mapping(yaml::IO &YamlIO, MachineStackObject &Object) {
152 YamlIO.mapRequired("id", Object.ID);
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);
161 static const bool flow = true;
164 /// Serializable representation of the fixed stack object from the
165 /// MachineFrameInfo class.
166 struct FixedMachineStackObject {
167 enum ObjectType { DefaultType, SpillSlot };
169 ObjectType Type = DefaultType;
172 unsigned Alignment = 0;
173 bool IsImmutable = false;
174 bool IsAliased = false;
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);
186 template <> struct MappingTraits<FixedMachineStackObject> {
187 static void mapping(yaml::IO &YamlIO, FixedMachineStackObject &Object) {
188 YamlIO.mapRequired("id", Object.ID);
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);
201 static const bool flow = true;
204 } // end namespace yaml
205 } // end namespace llvm
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)
215 /// Serializable representation of MachineFrameInfo.
217 /// Doesn't serialize attributes like 'StackAlignment', 'IsStackRealignable' and
218 /// 'RealignOption' as they are determined by the target and LLVM function
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.
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);
260 struct MachineFunction {
262 unsigned Alignment = 0;
263 bool ExposesReturnsTwice = false;
264 bool HasInlineAsm = false;
265 // Register information
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.
273 MachineFrameInfo FrameInfo;
274 std::vector<FixedMachineStackObject> FixedStackObjects;
275 std::vector<MachineStackObject> StackObjects;
277 std::vector<MachineBasicBlock> BasicBlocks;
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);
297 } // end namespace yaml
298 } // end namespace llvm