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 } // end namespace yaml
125 } // end namespace llvm
127 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::VirtualRegisterDefinition)
128 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::yaml::MachineBasicBlock)
133 /// Serializable representation of MachineFrameInfo.
135 /// Doesn't serialize attributes like 'StackAlignment', 'IsStackRealignable' and
136 /// 'RealignOption' as they are determined by the target and LLVM function
138 /// It also doesn't serialize attributes like 'NumFixedObject' and
139 /// 'HasVarSizedObjects' as they are determined by the frame objects themselves.
140 struct MachineFrameInfo {
141 // TODO: Serialize stack objects.
142 bool IsFrameAddressTaken = false;
143 bool IsReturnAddressTaken = false;
144 bool HasStackMap = false;
145 bool HasPatchPoint = false;
146 uint64_t StackSize = 0;
147 int OffsetAdjustment = 0;
148 unsigned MaxAlignment = 0;
149 bool AdjustsStack = false;
150 bool HasCalls = false;
151 // TODO: Serialize StackProtectorIdx and FunctionContextIdx
152 unsigned MaxCallFrameSize = 0;
153 // TODO: Serialize callee saved info.
154 // TODO: Serialize local frame objects.
155 bool HasOpaqueSPAdjustment = false;
156 bool HasVAStart = false;
157 bool HasMustTailInVarArgFunc = false;
158 // TODO: Serialize save and restore MBB references.
161 template <> struct MappingTraits<MachineFrameInfo> {
162 static void mapping(IO &YamlIO, MachineFrameInfo &MFI) {
163 YamlIO.mapOptional("isFrameAddressTaken", MFI.IsFrameAddressTaken);
164 YamlIO.mapOptional("isReturnAddressTaken", MFI.IsReturnAddressTaken);
165 YamlIO.mapOptional("hasStackMap", MFI.HasStackMap);
166 YamlIO.mapOptional("hasPatchPoint", MFI.HasPatchPoint);
167 YamlIO.mapOptional("stackSize", MFI.StackSize);
168 YamlIO.mapOptional("offsetAdjustment", MFI.OffsetAdjustment);
169 YamlIO.mapOptional("maxAlignment", MFI.MaxAlignment);
170 YamlIO.mapOptional("adjustsStack", MFI.AdjustsStack);
171 YamlIO.mapOptional("hasCalls", MFI.HasCalls);
172 YamlIO.mapOptional("maxCallFrameSize", MFI.MaxCallFrameSize);
173 YamlIO.mapOptional("hasOpaqueSPAdjustment", MFI.HasOpaqueSPAdjustment);
174 YamlIO.mapOptional("hasVAStart", MFI.HasVAStart);
175 YamlIO.mapOptional("hasMustTailInVarArgFunc", MFI.HasMustTailInVarArgFunc);
179 struct MachineFunction {
181 unsigned Alignment = 0;
182 bool ExposesReturnsTwice = false;
183 bool HasInlineAsm = false;
184 // Register information
186 bool TracksRegLiveness = false;
187 bool TracksSubRegLiveness = false;
188 std::vector<VirtualRegisterDefinition> VirtualRegisters;
189 // TODO: Serialize the various register masks.
190 // TODO: Serialize live in registers.
192 MachineFrameInfo FrameInfo;
194 std::vector<MachineBasicBlock> BasicBlocks;
197 template <> struct MappingTraits<MachineFunction> {
198 static void mapping(IO &YamlIO, MachineFunction &MF) {
199 YamlIO.mapRequired("name", MF.Name);
200 YamlIO.mapOptional("alignment", MF.Alignment);
201 YamlIO.mapOptional("exposesReturnsTwice", MF.ExposesReturnsTwice);
202 YamlIO.mapOptional("hasInlineAsm", MF.HasInlineAsm);
203 YamlIO.mapOptional("isSSA", MF.IsSSA);
204 YamlIO.mapOptional("tracksRegLiveness", MF.TracksRegLiveness);
205 YamlIO.mapOptional("tracksSubRegLiveness", MF.TracksSubRegLiveness);
206 YamlIO.mapOptional("registers", MF.VirtualRegisters);
207 YamlIO.mapOptional("frameInfo", MF.FrameInfo);
208 YamlIO.mapOptional("body", MF.BasicBlocks);
212 } // end namespace yaml
213 } // end namespace llvm