blob: d49085b6fe594b402d3fbacb397e2082ad62bf1d [file] [log] [blame]
Alex Lorenz2bdb4e12015-05-27 18:02:19 +00001//===- MIRParser.cpp - MIR serialization format parser implementation -----===//
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// This file implements the class that parses the optional LLVM IR and machine
11// functions that are stored in MIR files.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/CodeGen/MIRParser/MIRParser.h"
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000016#include "MIParser.h"
Alex Lorenz33f0aef2015-06-26 16:46:11 +000017#include "llvm/ADT/DenseMap.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000018#include "llvm/ADT/StringRef.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000019#include "llvm/ADT/StringMap.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000020#include "llvm/ADT/STLExtras.h"
21#include "llvm/AsmParser/Parser.h"
Alex Lorenz5d6108e2015-06-26 22:56:48 +000022#include "llvm/AsmParser/SlotMapping.h"
Alex Lorenzab980492015-07-20 20:51:18 +000023#include "llvm/CodeGen/MachineConstantPool.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000024#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz60541c12015-07-09 19:55:27 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
Alex Lorenz54565cf2015-06-24 19:56:10 +000026#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenz78d78312015-05-28 22:41:12 +000027#include "llvm/CodeGen/MIRYamlMapping.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000028#include "llvm/IR/BasicBlock.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000029#include "llvm/IR/DiagnosticInfo.h"
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000030#include "llvm/IR/Instructions.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000031#include "llvm/IR/LLVMContext.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000032#include "llvm/IR/Module.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000033#include "llvm/IR/ValueSymbolTable.h"
Alex Lorenz09b832c2015-05-29 17:05:41 +000034#include "llvm/Support/LineIterator.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000035#include "llvm/Support/SMLoc.h"
36#include "llvm/Support/SourceMgr.h"
37#include "llvm/Support/MemoryBuffer.h"
38#include "llvm/Support/YAMLTraits.h"
39#include <memory>
40
41using namespace llvm;
42
Alex Lorenz735c47e2015-06-15 20:30:22 +000043namespace llvm {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000044
45/// This class implements the parsing of LLVM IR that's embedded inside a MIR
46/// file.
47class MIRParserImpl {
48 SourceMgr SM;
49 StringRef Filename;
50 LLVMContext &Context;
Alex Lorenz735c47e2015-06-15 20:30:22 +000051 StringMap<std::unique_ptr<yaml::MachineFunction>> Functions;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000052 SlotMapping IRSlots;
Alex Lorenz28148ba2015-07-09 22:23:13 +000053 /// Maps from register class names to register classes.
54 StringMap<const TargetRegisterClass *> Names2RegClasses;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000055
56public:
57 MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename,
58 LLVMContext &Context);
59
Alex Lorenz735c47e2015-06-15 20:30:22 +000060 void reportDiagnostic(const SMDiagnostic &Diag);
61
62 /// Report an error with the given message at unknown location.
63 ///
64 /// Always returns true.
65 bool error(const Twine &Message);
66
Alex Lorenzb1f9ce82015-07-08 20:22:20 +000067 /// Report an error with the given message at the given location.
68 ///
69 /// Always returns true.
70 bool error(SMLoc Loc, const Twine &Message);
71
Alex Lorenz0fd7c622015-06-30 17:55:00 +000072 /// Report a given error with the location translated from the location in an
73 /// embedded string literal to a location in the MIR file.
74 ///
75 /// Always returns true.
76 bool error(const SMDiagnostic &Error, SMRange SourceRange);
77
Alex Lorenz78d78312015-05-28 22:41:12 +000078 /// Try to parse the optional LLVM module and the machine functions in the MIR
79 /// file.
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000080 ///
Alex Lorenz78d78312015-05-28 22:41:12 +000081 /// Return null if an error occurred.
Alex Lorenz735c47e2015-06-15 20:30:22 +000082 std::unique_ptr<Module> parse();
Alex Lorenz78d78312015-05-28 22:41:12 +000083
84 /// Parse the machine function in the current YAML document.
85 ///
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000086 /// \param NoLLVMIR - set to true when the MIR file doesn't have LLVM IR.
87 /// A dummy IR function is created and inserted into the given module when
88 /// this parameter is true.
89 ///
Alex Lorenz78d78312015-05-28 22:41:12 +000090 /// Return true if an error occurred.
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000091 bool parseMachineFunction(yaml::Input &In, Module &M, bool NoLLVMIR);
Alex Lorenz09b832c2015-05-29 17:05:41 +000092
Alex Lorenz735c47e2015-06-15 20:30:22 +000093 /// Initialize the machine function to the state that's described in the MIR
94 /// file.
95 ///
96 /// Return true if error occurred.
97 bool initializeMachineFunction(MachineFunction &MF);
98
Alex Lorenz4f093bf2015-06-19 17:43:07 +000099 /// Initialize the machine basic block using it's YAML representation.
100 ///
101 /// Return true if an error occurred.
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000102 bool initializeMachineBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB,
103 const yaml::MachineBasicBlock &YamlMBB,
104 const PerFunctionMIParsingState &PFS);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000105
Alex Lorenzdb07c402015-07-28 16:48:37 +0000106 bool initializeRegisterInfo(MachineFunction &MF,
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000107 const yaml::MachineFunction &YamlMF,
108 PerFunctionMIParsingState &PFS);
Alex Lorenz54565cf2015-06-24 19:56:10 +0000109
Alex Lorenzdb07c402015-07-28 16:48:37 +0000110 bool initializeFrameInfo(MachineFunction &MF,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000111 const yaml::MachineFunction &YamlMF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000112 PerFunctionMIParsingState &PFS);
113
114 bool parseCalleeSavedRegister(MachineFunction &MF,
115 PerFunctionMIParsingState &PFS,
116 std::vector<CalleeSavedInfo> &CSIInfo,
117 const yaml::StringValue &RegisterSource,
118 int FrameIdx);
Alex Lorenz60541c12015-07-09 19:55:27 +0000119
Alex Lorenzab980492015-07-20 20:51:18 +0000120 bool initializeConstantPool(MachineConstantPool &ConstantPool,
121 const yaml::MachineFunction &YamlMF,
122 const MachineFunction &MF,
123 DenseMap<unsigned, unsigned> &ConstantPoolSlots);
124
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000125 bool initializeJumpTableInfo(MachineFunction &MF,
126 const yaml::MachineJumpTable &YamlJTI,
Alex Lorenz31d70682015-07-15 23:38:35 +0000127 PerFunctionMIParsingState &PFS);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000128
Alex Lorenz09b832c2015-05-29 17:05:41 +0000129private:
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000130 bool parseMBBReference(MachineBasicBlock *&MBB,
131 const yaml::StringValue &Source, MachineFunction &MF,
132 const PerFunctionMIParsingState &PFS);
133
Alex Lorenz51af1602015-06-23 22:39:23 +0000134 /// Return a MIR diagnostic converted from an MI string diagnostic.
135 SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error,
136 SMRange SourceRange);
137
Alex Lorenz09b832c2015-05-29 17:05:41 +0000138 /// Return a MIR diagnostic converted from an LLVM assembly diagnostic.
139 SMDiagnostic diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
140 SMRange SourceRange);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000141
142 /// Create an empty function with the given name.
143 void createDummyFunction(StringRef Name, Module &M);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000144
145 void initNames2RegClasses(const MachineFunction &MF);
146
147 /// Check if the given identifier is a name of a register class.
148 ///
149 /// Return null if the name isn't a register class.
150 const TargetRegisterClass *getRegClass(const MachineFunction &MF,
151 StringRef Name);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000152};
153
Alex Lorenz735c47e2015-06-15 20:30:22 +0000154} // end namespace llvm
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000155
156MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
157 StringRef Filename, LLVMContext &Context)
158 : SM(), Filename(Filename), Context(Context) {
159 SM.AddNewSourceBuffer(std::move(Contents), SMLoc());
160}
161
Alex Lorenz735c47e2015-06-15 20:30:22 +0000162bool MIRParserImpl::error(const Twine &Message) {
163 Context.diagnose(DiagnosticInfoMIRParser(
164 DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
165 return true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000166}
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000167
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000168bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) {
169 Context.diagnose(DiagnosticInfoMIRParser(
170 DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message)));
171 return true;
172}
173
Alex Lorenz0fd7c622015-06-30 17:55:00 +0000174bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) {
175 assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error");
176 reportDiagnostic(diagFromMIStringDiag(Error, SourceRange));
177 return true;
178}
179
Alex Lorenz735c47e2015-06-15 20:30:22 +0000180void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
181 DiagnosticSeverity Kind;
182 switch (Diag.getKind()) {
183 case SourceMgr::DK_Error:
184 Kind = DS_Error;
185 break;
186 case SourceMgr::DK_Warning:
187 Kind = DS_Warning;
188 break;
189 case SourceMgr::DK_Note:
190 Kind = DS_Note;
191 break;
192 }
193 Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
194}
195
196static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
197 reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
198}
199
200std::unique_ptr<Module> MIRParserImpl::parse() {
Alex Lorenz78d78312015-05-28 22:41:12 +0000201 yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(),
Alex Lorenz735c47e2015-06-15 20:30:22 +0000202 /*Ctxt=*/nullptr, handleYAMLDiag, this);
Alex Lorenz51af1602015-06-23 22:39:23 +0000203 In.setContext(&In);
Alex Lorenz78d78312015-05-28 22:41:12 +0000204
205 if (!In.setCurrentDocument()) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000206 if (In.error())
Alex Lorenz78d78312015-05-28 22:41:12 +0000207 return nullptr;
208 // Create an empty module when the MIR file is empty.
209 return llvm::make_unique<Module>(Filename, Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000210 }
211
Alex Lorenz78d78312015-05-28 22:41:12 +0000212 std::unique_ptr<Module> M;
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000213 bool NoLLVMIR = false;
Alex Lorenz78d78312015-05-28 22:41:12 +0000214 // Parse the block scalar manually so that we can return unique pointer
215 // without having to go trough YAML traits.
216 if (const auto *BSN =
217 dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000218 SMDiagnostic Error;
Alex Lorenz78d78312015-05-28 22:41:12 +0000219 M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000220 Context, &IRSlots);
Alex Lorenz09b832c2015-05-29 17:05:41 +0000221 if (!M) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000222 reportDiagnostic(diagFromLLVMAssemblyDiag(Error, BSN->getSourceRange()));
Alex Lorenz78d78312015-05-28 22:41:12 +0000223 return M;
Alex Lorenz09b832c2015-05-29 17:05:41 +0000224 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000225 In.nextDocument();
226 if (!In.setCurrentDocument())
227 return M;
228 } else {
229 // Create an new, empty module.
230 M = llvm::make_unique<Module>(Filename, Context);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000231 NoLLVMIR = true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000232 }
233
234 // Parse the machine functions.
235 do {
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000236 if (parseMachineFunction(In, *M, NoLLVMIR))
Alex Lorenz78d78312015-05-28 22:41:12 +0000237 return nullptr;
238 In.nextDocument();
239 } while (In.setCurrentDocument());
240
241 return M;
242}
243
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000244bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M,
245 bool NoLLVMIR) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000246 auto MF = llvm::make_unique<yaml::MachineFunction>();
247 yaml::yamlize(In, *MF, false);
Alex Lorenz78d78312015-05-28 22:41:12 +0000248 if (In.error())
249 return true;
Alex Lorenz735c47e2015-06-15 20:30:22 +0000250 auto FunctionName = MF->Name;
Alex Lorenzfe2aa972015-06-15 22:23:23 +0000251 if (Functions.find(FunctionName) != Functions.end())
252 return error(Twine("redefinition of machine function '") + FunctionName +
253 "'");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000254 Functions.insert(std::make_pair(FunctionName, std::move(MF)));
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000255 if (NoLLVMIR)
256 createDummyFunction(FunctionName, M);
Alex Lorenz5ef16b82015-06-16 17:06:29 +0000257 else if (!M.getFunction(FunctionName))
258 return error(Twine("function '") + FunctionName +
259 "' isn't defined in the provided LLVM IR");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000260 return false;
261}
262
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000263void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
264 auto &Context = M.getContext();
265 Function *F = cast<Function>(M.getOrInsertFunction(
266 Name, FunctionType::get(Type::getVoidTy(Context), false)));
267 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
268 new UnreachableInst(Context, BB);
269}
270
Alex Lorenz735c47e2015-06-15 20:30:22 +0000271bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
272 auto It = Functions.find(MF.getName());
273 if (It == Functions.end())
274 return error(Twine("no machine function information for function '") +
275 MF.getName() + "' in the MIR file");
276 // TODO: Recreate the machine function.
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000277 const yaml::MachineFunction &YamlMF = *It->getValue();
278 if (YamlMF.Alignment)
279 MF.setAlignment(YamlMF.Alignment);
280 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
281 MF.setHasInlineAsm(YamlMF.HasInlineAsm);
Alex Lorenz53464512015-07-10 22:51:20 +0000282 PerFunctionMIParsingState PFS;
Alex Lorenzdb07c402015-07-28 16:48:37 +0000283 if (initializeRegisterInfo(MF, YamlMF, PFS))
Alex Lorenz54565cf2015-06-24 19:56:10 +0000284 return true;
Alex Lorenzab980492015-07-20 20:51:18 +0000285 if (!YamlMF.Constants.empty()) {
286 auto *ConstantPool = MF.getConstantPool();
287 assert(ConstantPool && "Constant pool must be created");
288 if (initializeConstantPool(*ConstantPool, YamlMF, MF,
289 PFS.ConstantPoolSlots))
290 return true;
291 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000292
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000293 const auto &F = *MF.getFunction();
294 for (const auto &YamlMBB : YamlMF.BasicBlocks) {
295 const BasicBlock *BB = nullptr;
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000296 const yaml::StringValue &Name = YamlMBB.Name;
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000297 const yaml::StringValue &IRBlock = YamlMBB.IRBlock;
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000298 if (!Name.Value.empty()) {
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000299 BB = dyn_cast_or_null<BasicBlock>(
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000300 F.getValueSymbolTable().lookup(Name.Value));
Alex Lorenz00302df2015-06-19 20:12:03 +0000301 if (!BB)
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000302 return error(Name.SourceRange.Start,
303 Twine("basic block '") + Name.Value +
304 "' is not defined in the function '" + MF.getName() +
305 "'");
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000306 }
Alex Lorenz8a1915b2015-07-27 22:42:41 +0000307 if (!IRBlock.Value.empty()) {
308 // TODO: Report an error when both name and ir block are specified.
309 SMDiagnostic Error;
310 if (parseIRBlockReference(BB, SM, MF, IRBlock.Value, PFS, IRSlots, Error))
311 return error(Error, IRBlock.SourceRange);
312 }
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000313 auto *MBB = MF.CreateMachineBasicBlock(BB);
314 MF.insert(MF.end(), MBB);
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000315 bool WasInserted =
316 PFS.MBBSlots.insert(std::make_pair(YamlMBB.ID, MBB)).second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000317 if (!WasInserted)
318 return error(Twine("redefinition of machine basic block with id #") +
319 Twine(YamlMBB.ID));
320 }
321
Alex Lorenzc8704b02015-07-09 21:21:33 +0000322 if (YamlMF.BasicBlocks.empty())
323 return error(Twine("machine function '") + Twine(MF.getName()) +
324 "' requires at least one machine basic block in its body");
Alex Lorenza6f9a372015-07-29 21:09:09 +0000325 // Initialize the frame information after creating all the MBBs so that the
326 // MBB references in the frame information can be resolved.
327 if (initializeFrameInfo(MF, YamlMF, PFS))
328 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000329 // Initialize the jump table after creating all the MBBs so that the MBB
330 // references can be resolved.
331 if (!YamlMF.JumpTableInfo.Entries.empty() &&
332 initializeJumpTableInfo(MF, YamlMF.JumpTableInfo, PFS))
333 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000334 // Initialize the machine basic blocks after creating them all so that the
335 // machine instructions parser can resolve the MBB references.
336 unsigned I = 0;
337 for (const auto &YamlMBB : YamlMF.BasicBlocks) {
338 if (initializeMachineBasicBlock(MF, *MF.getBlockNumbered(I++), YamlMBB,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000339 PFS))
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000340 return true;
341 }
Alex Lorenzc7bf2042015-07-24 17:44:49 +0000342 // FIXME: This is a temporary workaround until the reserved registers can be
343 // serialized.
344 MF.getRegInfo().freezeReservedRegs(MF);
345 MF.verify();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000346 return false;
347}
348
349bool MIRParserImpl::initializeMachineBasicBlock(
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000350 MachineFunction &MF, MachineBasicBlock &MBB,
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000351 const yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000352 const PerFunctionMIParsingState &PFS) {
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000353 MBB.setAlignment(YamlMBB.Alignment);
354 if (YamlMBB.AddressTaken)
355 MBB.setHasAddressTaken();
356 MBB.setIsLandingPad(YamlMBB.IsLandingPad);
Alex Lorenzf09df002015-06-30 18:16:42 +0000357 SMDiagnostic Error;
358 // Parse the successors.
359 for (const auto &MBBSource : YamlMBB.Successors) {
360 MachineBasicBlock *SuccMBB = nullptr;
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000361 if (parseMBBReference(SuccMBB, MBBSource, MF, PFS))
362 return true;
Alex Lorenzf09df002015-06-30 18:16:42 +0000363 // TODO: Report an error when adding the same successor more than once.
364 MBB.addSuccessor(SuccMBB);
365 }
Alex Lorenz9fab3702015-07-14 21:24:41 +0000366 // Parse the liveins.
367 for (const auto &LiveInSource : YamlMBB.LiveIns) {
368 unsigned Reg = 0;
369 if (parseNamedRegisterReference(Reg, SM, MF, LiveInSource.Value, PFS,
370 IRSlots, Error))
371 return error(Error, LiveInSource.SourceRange);
372 MBB.addLiveIn(Reg);
373 }
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000374 // Parse the instructions.
375 for (const auto &MISource : YamlMBB.Instructions) {
Alex Lorenz3708a642015-06-30 17:47:50 +0000376 MachineInstr *MI = nullptr;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000377 if (parseMachineInstr(MI, SM, MF, MISource.Value, PFS, IRSlots, Error))
Alex Lorenz0fd7c622015-06-30 17:55:00 +0000378 return error(Error, MISource.SourceRange);
Alex Lorenz3708a642015-06-30 17:47:50 +0000379 MBB.insert(MBB.end(), MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000380 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000381 return false;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000382}
383
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000384bool MIRParserImpl::initializeRegisterInfo(MachineFunction &MF,
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000385 const yaml::MachineFunction &YamlMF,
386 PerFunctionMIParsingState &PFS) {
Alex Lorenzdb07c402015-07-28 16:48:37 +0000387 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Alex Lorenz54565cf2015-06-24 19:56:10 +0000388 assert(RegInfo.isSSA());
389 if (!YamlMF.IsSSA)
390 RegInfo.leaveSSA();
391 assert(RegInfo.tracksLiveness());
392 if (!YamlMF.TracksRegLiveness)
393 RegInfo.invalidateLiveness();
394 RegInfo.enableSubRegLiveness(YamlMF.TracksSubRegLiveness);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000395
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000396 SMDiagnostic Error;
Alex Lorenz28148ba2015-07-09 22:23:13 +0000397 // Parse the virtual register information.
398 for (const auto &VReg : YamlMF.VirtualRegisters) {
399 const auto *RC = getRegClass(MF, VReg.Class.Value);
400 if (!RC)
401 return error(VReg.Class.SourceRange.Start,
402 Twine("use of undefined register class '") +
403 VReg.Class.Value + "'");
Alex Lorenz53464512015-07-10 22:51:20 +0000404 unsigned Reg = RegInfo.createVirtualRegister(RC);
405 // TODO: Report an error when the same virtual register with the same ID is
406 // redefined.
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000407 PFS.VirtualRegisterSlots.insert(std::make_pair(VReg.ID, Reg));
408 if (!VReg.PreferredRegister.Value.empty()) {
409 unsigned PreferredReg = 0;
410 if (parseNamedRegisterReference(PreferredReg, SM, MF,
411 VReg.PreferredRegister.Value, PFS,
412 IRSlots, Error))
413 return error(Error, VReg.PreferredRegister.SourceRange);
414 RegInfo.setSimpleHint(Reg, PreferredReg);
415 }
Alex Lorenz28148ba2015-07-09 22:23:13 +0000416 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000417
418 // Parse the liveins.
419 for (const auto &LiveIn : YamlMF.LiveIns) {
420 unsigned Reg = 0;
421 if (parseNamedRegisterReference(Reg, SM, MF, LiveIn.Register.Value, PFS,
422 IRSlots, Error))
423 return error(Error, LiveIn.Register.SourceRange);
424 unsigned VReg = 0;
425 if (!LiveIn.VirtualRegister.Value.empty()) {
426 if (parseVirtualRegisterReference(
427 VReg, SM, MF, LiveIn.VirtualRegister.Value, PFS, IRSlots, Error))
428 return error(Error, LiveIn.VirtualRegister.SourceRange);
429 }
430 RegInfo.addLiveIn(Reg, VReg);
431 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000432 return false;
433}
434
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000435bool MIRParserImpl::initializeFrameInfo(MachineFunction &MF,
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000436 const yaml::MachineFunction &YamlMF,
437 PerFunctionMIParsingState &PFS) {
Alex Lorenzdb07c402015-07-28 16:48:37 +0000438 MachineFrameInfo &MFI = *MF.getFrameInfo();
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000439 const Function &F = *MF.getFunction();
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000440 const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
Alex Lorenz60541c12015-07-09 19:55:27 +0000441 MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
442 MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
443 MFI.setHasStackMap(YamlMFI.HasStackMap);
444 MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
445 MFI.setStackSize(YamlMFI.StackSize);
446 MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
447 if (YamlMFI.MaxAlignment)
448 MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
449 MFI.setAdjustsStack(YamlMFI.AdjustsStack);
450 MFI.setHasCalls(YamlMFI.HasCalls);
451 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
452 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
453 MFI.setHasVAStart(YamlMFI.HasVAStart);
454 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
Alex Lorenza6f9a372015-07-29 21:09:09 +0000455 if (!YamlMFI.SavePoint.Value.empty()) {
456 MachineBasicBlock *MBB = nullptr;
457 if (parseMBBReference(MBB, YamlMFI.SavePoint, MF, PFS))
458 return true;
459 MFI.setSavePoint(MBB);
460 }
461 if (!YamlMFI.RestorePoint.Value.empty()) {
462 MachineBasicBlock *MBB = nullptr;
463 if (parseMBBReference(MBB, YamlMFI.RestorePoint, MF, PFS))
464 return true;
465 MFI.setRestorePoint(MBB);
466 }
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000467
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000468 std::vector<CalleeSavedInfo> CSIInfo;
Alex Lorenzde491f02015-07-13 18:07:26 +0000469 // Initialize the fixed frame objects.
470 for (const auto &Object : YamlMF.FixedStackObjects) {
471 int ObjectIdx;
472 if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
473 ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset,
474 Object.IsImmutable, Object.IsAliased);
475 else
476 ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset);
477 MFI.setObjectAlignment(ObjectIdx, Object.Alignment);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000478 // TODO: Report an error when objects are redefined.
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000479 PFS.FixedStackObjectSlots.insert(std::make_pair(Object.ID, ObjectIdx));
480 if (parseCalleeSavedRegister(MF, PFS, CSIInfo, Object.CalleeSavedRegister,
481 ObjectIdx))
482 return true;
Alex Lorenzde491f02015-07-13 18:07:26 +0000483 }
484
485 // Initialize the ordinary frame objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000486 for (const auto &Object : YamlMF.StackObjects) {
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000487 int ObjectIdx;
Alex Lorenz37643a02015-07-15 22:14:49 +0000488 const AllocaInst *Alloca = nullptr;
489 const yaml::StringValue &Name = Object.Name;
490 if (!Name.Value.empty()) {
491 Alloca = dyn_cast_or_null<AllocaInst>(
492 F.getValueSymbolTable().lookup(Name.Value));
493 if (!Alloca)
494 return error(Name.SourceRange.Start,
495 "alloca instruction named '" + Name.Value +
496 "' isn't defined in the function '" + F.getName() +
497 "'");
498 }
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000499 if (Object.Type == yaml::MachineStackObject::VariableSized)
Alex Lorenz37643a02015-07-15 22:14:49 +0000500 ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca);
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000501 else
502 ObjectIdx = MFI.CreateStackObject(
503 Object.Size, Object.Alignment,
Alex Lorenz37643a02015-07-15 22:14:49 +0000504 Object.Type == yaml::MachineStackObject::SpillSlot, Alloca);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000505 MFI.setObjectOffset(ObjectIdx, Object.Offset);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000506 // TODO: Report an error when objects are redefined.
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000507 PFS.StackObjectSlots.insert(std::make_pair(Object.ID, ObjectIdx));
508 if (parseCalleeSavedRegister(MF, PFS, CSIInfo, Object.CalleeSavedRegister,
509 ObjectIdx))
510 return true;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000511 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000512 MFI.setCalleeSavedInfo(CSIInfo);
513 if (!CSIInfo.empty())
514 MFI.setCalleeSavedInfoValid(true);
515 return false;
516}
517
518bool MIRParserImpl::parseCalleeSavedRegister(
519 MachineFunction &MF, PerFunctionMIParsingState &PFS,
520 std::vector<CalleeSavedInfo> &CSIInfo,
521 const yaml::StringValue &RegisterSource, int FrameIdx) {
522 if (RegisterSource.Value.empty())
523 return false;
524 unsigned Reg = 0;
525 SMDiagnostic Error;
526 if (parseNamedRegisterReference(Reg, SM, MF, RegisterSource.Value, PFS,
527 IRSlots, Error))
528 return error(Error, RegisterSource.SourceRange);
529 CSIInfo.push_back(CalleeSavedInfo(Reg, FrameIdx));
Alex Lorenz60541c12015-07-09 19:55:27 +0000530 return false;
531}
532
Alex Lorenzab980492015-07-20 20:51:18 +0000533bool MIRParserImpl::initializeConstantPool(
534 MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF,
535 const MachineFunction &MF,
536 DenseMap<unsigned, unsigned> &ConstantPoolSlots) {
537 const auto &M = *MF.getFunction()->getParent();
538 SMDiagnostic Error;
539 for (const auto &YamlConstant : YamlMF.Constants) {
540 const Constant *Value = dyn_cast_or_null<Constant>(
541 parseConstantValue(YamlConstant.Value.Value, Error, M));
542 if (!Value)
543 return error(Error, YamlConstant.Value.SourceRange);
544 unsigned Alignment =
545 YamlConstant.Alignment
546 ? YamlConstant.Alignment
547 : M.getDataLayout().getPrefTypeAlignment(Value->getType());
548 // TODO: Report an error when the same constant pool value ID is redefined.
549 ConstantPoolSlots.insert(std::make_pair(
550 YamlConstant.ID, ConstantPool.getConstantPoolIndex(Value, Alignment)));
551 }
552 return false;
553}
554
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000555bool MIRParserImpl::initializeJumpTableInfo(
556 MachineFunction &MF, const yaml::MachineJumpTable &YamlJTI,
Alex Lorenz31d70682015-07-15 23:38:35 +0000557 PerFunctionMIParsingState &PFS) {
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000558 MachineJumpTableInfo *JTI = MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
559 SMDiagnostic Error;
560 for (const auto &Entry : YamlJTI.Entries) {
561 std::vector<MachineBasicBlock *> Blocks;
562 for (const auto &MBBSource : Entry.Blocks) {
563 MachineBasicBlock *MBB = nullptr;
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000564 if (parseMBBReference(MBB, MBBSource.Value, MF, PFS))
565 return true;
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000566 Blocks.push_back(MBB);
567 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000568 unsigned Index = JTI->createJumpTableIndex(Blocks);
569 // TODO: Report an error when the same jump table slot ID is redefined.
570 PFS.JumpTableSlots.insert(std::make_pair(Entry.ID, Index));
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000571 }
572 return false;
573}
574
Alex Lorenz05fa73b2015-07-29 20:57:11 +0000575bool MIRParserImpl::parseMBBReference(MachineBasicBlock *&MBB,
576 const yaml::StringValue &Source,
577 MachineFunction &MF,
578 const PerFunctionMIParsingState &PFS) {
579 SMDiagnostic Error;
580 if (llvm::parseMBBReference(MBB, SM, MF, Source.Value, PFS, IRSlots, Error))
581 return error(Error, Source.SourceRange);
582 return false;
583}
584
Alex Lorenz51af1602015-06-23 22:39:23 +0000585SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
586 SMRange SourceRange) {
587 assert(SourceRange.isValid() && "Invalid source range");
588 SMLoc Loc = SourceRange.Start;
589 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
590 *Loc.getPointer() == '\'';
591 // Translate the location of the error from the location in the MI string to
592 // the corresponding location in the MIR file.
593 Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
594 (HasQuote ? 1 : 0));
595
596 // TODO: Translate any source ranges as well.
597 return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
598 Error.getFixIts());
599}
600
Alex Lorenz09b832c2015-05-29 17:05:41 +0000601SMDiagnostic MIRParserImpl::diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
602 SMRange SourceRange) {
603 assert(SourceRange.isValid());
604
605 // Translate the location of the error from the location in the llvm IR string
606 // to the corresponding location in the MIR file.
607 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
608 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
609 unsigned Column = Error.getColumnNo();
610 StringRef LineStr = Error.getLineContents();
611 SMLoc Loc = Error.getLoc();
612
613 // Get the full line and adjust the column number by taking the indentation of
614 // LLVM IR into account.
615 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
616 L != E; ++L) {
617 if (L.line_number() == Line) {
618 LineStr = *L;
619 Loc = SMLoc::getFromPointer(LineStr.data());
620 auto Indent = LineStr.find(Error.getLineContents());
621 if (Indent != StringRef::npos)
622 Column += Indent;
623 break;
624 }
625 }
626
627 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
628 Error.getMessage(), LineStr, Error.getRanges(),
629 Error.getFixIts());
630}
631
Alex Lorenz28148ba2015-07-09 22:23:13 +0000632void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) {
633 if (!Names2RegClasses.empty())
634 return;
635 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
636 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
637 const auto *RC = TRI->getRegClass(I);
638 Names2RegClasses.insert(
639 std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
640 }
641}
642
643const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
644 StringRef Name) {
645 initNames2RegClasses(MF);
646 auto RegClassInfo = Names2RegClasses.find(Name);
647 if (RegClassInfo == Names2RegClasses.end())
648 return nullptr;
649 return RegClassInfo->getValue();
650}
651
Alex Lorenz735c47e2015-06-15 20:30:22 +0000652MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
653 : Impl(std::move(Impl)) {}
654
655MIRParser::~MIRParser() {}
656
657std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); }
658
659bool MIRParser::initializeMachineFunction(MachineFunction &MF) {
660 return Impl->initializeMachineFunction(MF);
661}
662
663std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
664 SMDiagnostic &Error,
665 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000666 auto FileOrErr = MemoryBuffer::getFile(Filename);
667 if (std::error_code EC = FileOrErr.getError()) {
668 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
669 "Could not open input file: " + EC.message());
Alex Lorenz735c47e2015-06-15 20:30:22 +0000670 return nullptr;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000671 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000672 return createMIRParser(std::move(FileOrErr.get()), Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000673}
674
Alex Lorenz735c47e2015-06-15 20:30:22 +0000675std::unique_ptr<MIRParser>
676llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
677 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000678 auto Filename = Contents->getBufferIdentifier();
Alex Lorenz735c47e2015-06-15 20:30:22 +0000679 return llvm::make_unique<MIRParser>(
680 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000681}