blob: ca46de447402b857baab997e489658bfe537b020 [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 Lorenz735c47e2015-06-15 20:30:22 +000023#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz60541c12015-07-09 19:55:27 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Alex Lorenz54565cf2015-06-24 19:56:10 +000025#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenz78d78312015-05-28 22:41:12 +000026#include "llvm/CodeGen/MIRYamlMapping.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000027#include "llvm/IR/BasicBlock.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000028#include "llvm/IR/DiagnosticInfo.h"
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000029#include "llvm/IR/Instructions.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000030#include "llvm/IR/LLVMContext.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000031#include "llvm/IR/Module.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000032#include "llvm/IR/ValueSymbolTable.h"
Alex Lorenz09b832c2015-05-29 17:05:41 +000033#include "llvm/Support/LineIterator.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000034#include "llvm/Support/SMLoc.h"
35#include "llvm/Support/SourceMgr.h"
36#include "llvm/Support/MemoryBuffer.h"
37#include "llvm/Support/YAMLTraits.h"
38#include <memory>
39
40using namespace llvm;
41
Alex Lorenz735c47e2015-06-15 20:30:22 +000042namespace llvm {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000043
44/// This class implements the parsing of LLVM IR that's embedded inside a MIR
45/// file.
46class MIRParserImpl {
47 SourceMgr SM;
48 StringRef Filename;
49 LLVMContext &Context;
Alex Lorenz735c47e2015-06-15 20:30:22 +000050 StringMap<std::unique_ptr<yaml::MachineFunction>> Functions;
Alex Lorenz5d6108e2015-06-26 22:56:48 +000051 SlotMapping IRSlots;
Alex Lorenz28148ba2015-07-09 22:23:13 +000052 /// Maps from register class names to register classes.
53 StringMap<const TargetRegisterClass *> Names2RegClasses;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000054
55public:
56 MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename,
57 LLVMContext &Context);
58
Alex Lorenz735c47e2015-06-15 20:30:22 +000059 void reportDiagnostic(const SMDiagnostic &Diag);
60
61 /// Report an error with the given message at unknown location.
62 ///
63 /// Always returns true.
64 bool error(const Twine &Message);
65
Alex Lorenzb1f9ce82015-07-08 20:22:20 +000066 /// Report an error with the given message at the given location.
67 ///
68 /// Always returns true.
69 bool error(SMLoc Loc, const Twine &Message);
70
Alex Lorenz0fd7c622015-06-30 17:55:00 +000071 /// Report a given error with the location translated from the location in an
72 /// embedded string literal to a location in the MIR file.
73 ///
74 /// Always returns true.
75 bool error(const SMDiagnostic &Error, SMRange SourceRange);
76
Alex Lorenz78d78312015-05-28 22:41:12 +000077 /// Try to parse the optional LLVM module and the machine functions in the MIR
78 /// file.
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000079 ///
Alex Lorenz78d78312015-05-28 22:41:12 +000080 /// Return null if an error occurred.
Alex Lorenz735c47e2015-06-15 20:30:22 +000081 std::unique_ptr<Module> parse();
Alex Lorenz78d78312015-05-28 22:41:12 +000082
83 /// Parse the machine function in the current YAML document.
84 ///
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000085 /// \param NoLLVMIR - set to true when the MIR file doesn't have LLVM IR.
86 /// A dummy IR function is created and inserted into the given module when
87 /// this parameter is true.
88 ///
Alex Lorenz78d78312015-05-28 22:41:12 +000089 /// Return true if an error occurred.
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000090 bool parseMachineFunction(yaml::Input &In, Module &M, bool NoLLVMIR);
Alex Lorenz09b832c2015-05-29 17:05:41 +000091
Alex Lorenz735c47e2015-06-15 20:30:22 +000092 /// Initialize the machine function to the state that's described in the MIR
93 /// file.
94 ///
95 /// Return true if error occurred.
96 bool initializeMachineFunction(MachineFunction &MF);
97
Alex Lorenz4f093bf2015-06-19 17:43:07 +000098 /// Initialize the machine basic block using it's YAML representation.
99 ///
100 /// Return true if an error occurred.
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000101 bool initializeMachineBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB,
102 const yaml::MachineBasicBlock &YamlMBB,
103 const PerFunctionMIParsingState &PFS);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000104
Alex Lorenz53464512015-07-10 22:51:20 +0000105 bool
106 initializeRegisterInfo(const MachineFunction &MF,
107 MachineRegisterInfo &RegInfo,
108 const yaml::MachineFunction &YamlMF,
109 DenseMap<unsigned, unsigned> &VirtualRegisterSlots);
Alex Lorenz54565cf2015-06-24 19:56:10 +0000110
Alex Lorenz37643a02015-07-15 22:14:49 +0000111 bool initializeFrameInfo(const Function &F, MachineFrameInfo &MFI,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000112 const yaml::MachineFunction &YamlMF,
113 DenseMap<unsigned, int> &StackObjectSlots,
114 DenseMap<unsigned, int> &FixedStackObjectSlots);
Alex Lorenz60541c12015-07-09 19:55:27 +0000115
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000116 bool initializeJumpTableInfo(MachineFunction &MF,
117 const yaml::MachineJumpTable &YamlJTI,
Alex Lorenz31d70682015-07-15 23:38:35 +0000118 PerFunctionMIParsingState &PFS);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000119
Alex Lorenz09b832c2015-05-29 17:05:41 +0000120private:
Alex Lorenz51af1602015-06-23 22:39:23 +0000121 /// Return a MIR diagnostic converted from an MI string diagnostic.
122 SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error,
123 SMRange SourceRange);
124
Alex Lorenz09b832c2015-05-29 17:05:41 +0000125 /// Return a MIR diagnostic converted from an LLVM assembly diagnostic.
126 SMDiagnostic diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
127 SMRange SourceRange);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000128
129 /// Create an empty function with the given name.
130 void createDummyFunction(StringRef Name, Module &M);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000131
132 void initNames2RegClasses(const MachineFunction &MF);
133
134 /// Check if the given identifier is a name of a register class.
135 ///
136 /// Return null if the name isn't a register class.
137 const TargetRegisterClass *getRegClass(const MachineFunction &MF,
138 StringRef Name);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000139};
140
Alex Lorenz735c47e2015-06-15 20:30:22 +0000141} // end namespace llvm
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000142
143MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
144 StringRef Filename, LLVMContext &Context)
145 : SM(), Filename(Filename), Context(Context) {
146 SM.AddNewSourceBuffer(std::move(Contents), SMLoc());
147}
148
Alex Lorenz735c47e2015-06-15 20:30:22 +0000149bool MIRParserImpl::error(const Twine &Message) {
150 Context.diagnose(DiagnosticInfoMIRParser(
151 DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
152 return true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000153}
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000154
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000155bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) {
156 Context.diagnose(DiagnosticInfoMIRParser(
157 DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message)));
158 return true;
159}
160
Alex Lorenz0fd7c622015-06-30 17:55:00 +0000161bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) {
162 assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error");
163 reportDiagnostic(diagFromMIStringDiag(Error, SourceRange));
164 return true;
165}
166
Alex Lorenz735c47e2015-06-15 20:30:22 +0000167void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
168 DiagnosticSeverity Kind;
169 switch (Diag.getKind()) {
170 case SourceMgr::DK_Error:
171 Kind = DS_Error;
172 break;
173 case SourceMgr::DK_Warning:
174 Kind = DS_Warning;
175 break;
176 case SourceMgr::DK_Note:
177 Kind = DS_Note;
178 break;
179 }
180 Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
181}
182
183static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
184 reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
185}
186
187std::unique_ptr<Module> MIRParserImpl::parse() {
Alex Lorenz78d78312015-05-28 22:41:12 +0000188 yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(),
Alex Lorenz735c47e2015-06-15 20:30:22 +0000189 /*Ctxt=*/nullptr, handleYAMLDiag, this);
Alex Lorenz51af1602015-06-23 22:39:23 +0000190 In.setContext(&In);
Alex Lorenz78d78312015-05-28 22:41:12 +0000191
192 if (!In.setCurrentDocument()) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000193 if (In.error())
Alex Lorenz78d78312015-05-28 22:41:12 +0000194 return nullptr;
195 // Create an empty module when the MIR file is empty.
196 return llvm::make_unique<Module>(Filename, Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000197 }
198
Alex Lorenz78d78312015-05-28 22:41:12 +0000199 std::unique_ptr<Module> M;
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000200 bool NoLLVMIR = false;
Alex Lorenz78d78312015-05-28 22:41:12 +0000201 // Parse the block scalar manually so that we can return unique pointer
202 // without having to go trough YAML traits.
203 if (const auto *BSN =
204 dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000205 SMDiagnostic Error;
Alex Lorenz78d78312015-05-28 22:41:12 +0000206 M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000207 Context, &IRSlots);
Alex Lorenz09b832c2015-05-29 17:05:41 +0000208 if (!M) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000209 reportDiagnostic(diagFromLLVMAssemblyDiag(Error, BSN->getSourceRange()));
Alex Lorenz78d78312015-05-28 22:41:12 +0000210 return M;
Alex Lorenz09b832c2015-05-29 17:05:41 +0000211 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000212 In.nextDocument();
213 if (!In.setCurrentDocument())
214 return M;
215 } else {
216 // Create an new, empty module.
217 M = llvm::make_unique<Module>(Filename, Context);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000218 NoLLVMIR = true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000219 }
220
221 // Parse the machine functions.
222 do {
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000223 if (parseMachineFunction(In, *M, NoLLVMIR))
Alex Lorenz78d78312015-05-28 22:41:12 +0000224 return nullptr;
225 In.nextDocument();
226 } while (In.setCurrentDocument());
227
228 return M;
229}
230
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000231bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M,
232 bool NoLLVMIR) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000233 auto MF = llvm::make_unique<yaml::MachineFunction>();
234 yaml::yamlize(In, *MF, false);
Alex Lorenz78d78312015-05-28 22:41:12 +0000235 if (In.error())
236 return true;
Alex Lorenz735c47e2015-06-15 20:30:22 +0000237 auto FunctionName = MF->Name;
Alex Lorenzfe2aa972015-06-15 22:23:23 +0000238 if (Functions.find(FunctionName) != Functions.end())
239 return error(Twine("redefinition of machine function '") + FunctionName +
240 "'");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000241 Functions.insert(std::make_pair(FunctionName, std::move(MF)));
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000242 if (NoLLVMIR)
243 createDummyFunction(FunctionName, M);
Alex Lorenz5ef16b82015-06-16 17:06:29 +0000244 else if (!M.getFunction(FunctionName))
245 return error(Twine("function '") + FunctionName +
246 "' isn't defined in the provided LLVM IR");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000247 return false;
248}
249
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000250void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
251 auto &Context = M.getContext();
252 Function *F = cast<Function>(M.getOrInsertFunction(
253 Name, FunctionType::get(Type::getVoidTy(Context), false)));
254 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
255 new UnreachableInst(Context, BB);
256}
257
Alex Lorenz735c47e2015-06-15 20:30:22 +0000258bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
259 auto It = Functions.find(MF.getName());
260 if (It == Functions.end())
261 return error(Twine("no machine function information for function '") +
262 MF.getName() + "' in the MIR file");
263 // TODO: Recreate the machine function.
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000264 const yaml::MachineFunction &YamlMF = *It->getValue();
265 if (YamlMF.Alignment)
266 MF.setAlignment(YamlMF.Alignment);
267 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
268 MF.setHasInlineAsm(YamlMF.HasInlineAsm);
Alex Lorenz53464512015-07-10 22:51:20 +0000269 PerFunctionMIParsingState PFS;
270 if (initializeRegisterInfo(MF, MF.getRegInfo(), YamlMF,
271 PFS.VirtualRegisterSlots))
Alex Lorenz54565cf2015-06-24 19:56:10 +0000272 return true;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000273 if (initializeFrameInfo(*MF.getFunction(), *MF.getFrameInfo(), YamlMF,
274 PFS.StackObjectSlots, PFS.FixedStackObjectSlots))
Alex Lorenz60541c12015-07-09 19:55:27 +0000275 return true;
Alex Lorenz54565cf2015-06-24 19:56:10 +0000276
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000277 const auto &F = *MF.getFunction();
278 for (const auto &YamlMBB : YamlMF.BasicBlocks) {
279 const BasicBlock *BB = nullptr;
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000280 const yaml::StringValue &Name = YamlMBB.Name;
281 if (!Name.Value.empty()) {
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000282 BB = dyn_cast_or_null<BasicBlock>(
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000283 F.getValueSymbolTable().lookup(Name.Value));
Alex Lorenz00302df2015-06-19 20:12:03 +0000284 if (!BB)
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000285 return error(Name.SourceRange.Start,
286 Twine("basic block '") + Name.Value +
287 "' is not defined in the function '" + MF.getName() +
288 "'");
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000289 }
290 auto *MBB = MF.CreateMachineBasicBlock(BB);
291 MF.insert(MF.end(), MBB);
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000292 bool WasInserted =
293 PFS.MBBSlots.insert(std::make_pair(YamlMBB.ID, MBB)).second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000294 if (!WasInserted)
295 return error(Twine("redefinition of machine basic block with id #") +
296 Twine(YamlMBB.ID));
297 }
298
Alex Lorenzc8704b02015-07-09 21:21:33 +0000299 if (YamlMF.BasicBlocks.empty())
300 return error(Twine("machine function '") + Twine(MF.getName()) +
301 "' requires at least one machine basic block in its body");
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000302 // Initialize the jump table after creating all the MBBs so that the MBB
303 // references can be resolved.
304 if (!YamlMF.JumpTableInfo.Entries.empty() &&
305 initializeJumpTableInfo(MF, YamlMF.JumpTableInfo, PFS))
306 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000307 // Initialize the machine basic blocks after creating them all so that the
308 // machine instructions parser can resolve the MBB references.
309 unsigned I = 0;
310 for (const auto &YamlMBB : YamlMF.BasicBlocks) {
311 if (initializeMachineBasicBlock(MF, *MF.getBlockNumbered(I++), YamlMBB,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000312 PFS))
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000313 return true;
314 }
315 return false;
316}
317
318bool MIRParserImpl::initializeMachineBasicBlock(
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000319 MachineFunction &MF, MachineBasicBlock &MBB,
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000320 const yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000321 const PerFunctionMIParsingState &PFS) {
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000322 MBB.setAlignment(YamlMBB.Alignment);
323 if (YamlMBB.AddressTaken)
324 MBB.setHasAddressTaken();
325 MBB.setIsLandingPad(YamlMBB.IsLandingPad);
Alex Lorenzf09df002015-06-30 18:16:42 +0000326 SMDiagnostic Error;
327 // Parse the successors.
328 for (const auto &MBBSource : YamlMBB.Successors) {
329 MachineBasicBlock *SuccMBB = nullptr;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000330 if (parseMBBReference(SuccMBB, SM, MF, MBBSource.Value, PFS, IRSlots,
Alex Lorenzf09df002015-06-30 18:16:42 +0000331 Error))
332 return error(Error, MBBSource.SourceRange);
333 // TODO: Report an error when adding the same successor more than once.
334 MBB.addSuccessor(SuccMBB);
335 }
Alex Lorenz9fab3702015-07-14 21:24:41 +0000336 // Parse the liveins.
337 for (const auto &LiveInSource : YamlMBB.LiveIns) {
338 unsigned Reg = 0;
339 if (parseNamedRegisterReference(Reg, SM, MF, LiveInSource.Value, PFS,
340 IRSlots, Error))
341 return error(Error, LiveInSource.SourceRange);
342 MBB.addLiveIn(Reg);
343 }
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000344 // Parse the instructions.
345 for (const auto &MISource : YamlMBB.Instructions) {
Alex Lorenz3708a642015-06-30 17:47:50 +0000346 MachineInstr *MI = nullptr;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000347 if (parseMachineInstr(MI, SM, MF, MISource.Value, PFS, IRSlots, Error))
Alex Lorenz0fd7c622015-06-30 17:55:00 +0000348 return error(Error, MISource.SourceRange);
Alex Lorenz3708a642015-06-30 17:47:50 +0000349 MBB.insert(MBB.end(), MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000350 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000351 return false;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000352}
353
Alex Lorenz54565cf2015-06-24 19:56:10 +0000354bool MIRParserImpl::initializeRegisterInfo(
Alex Lorenz28148ba2015-07-09 22:23:13 +0000355 const MachineFunction &MF, MachineRegisterInfo &RegInfo,
Alex Lorenz53464512015-07-10 22:51:20 +0000356 const yaml::MachineFunction &YamlMF,
357 DenseMap<unsigned, unsigned> &VirtualRegisterSlots) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000358 assert(RegInfo.isSSA());
359 if (!YamlMF.IsSSA)
360 RegInfo.leaveSSA();
361 assert(RegInfo.tracksLiveness());
362 if (!YamlMF.TracksRegLiveness)
363 RegInfo.invalidateLiveness();
364 RegInfo.enableSubRegLiveness(YamlMF.TracksSubRegLiveness);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000365
366 // Parse the virtual register information.
367 for (const auto &VReg : YamlMF.VirtualRegisters) {
368 const auto *RC = getRegClass(MF, VReg.Class.Value);
369 if (!RC)
370 return error(VReg.Class.SourceRange.Start,
371 Twine("use of undefined register class '") +
372 VReg.Class.Value + "'");
Alex Lorenz53464512015-07-10 22:51:20 +0000373 unsigned Reg = RegInfo.createVirtualRegister(RC);
374 // TODO: Report an error when the same virtual register with the same ID is
375 // redefined.
376 VirtualRegisterSlots.insert(std::make_pair(VReg.ID, Reg));
Alex Lorenz28148ba2015-07-09 22:23:13 +0000377 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000378 return false;
379}
380
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000381bool MIRParserImpl::initializeFrameInfo(
382 const Function &F, MachineFrameInfo &MFI,
383 const yaml::MachineFunction &YamlMF,
384 DenseMap<unsigned, int> &StackObjectSlots,
385 DenseMap<unsigned, int> &FixedStackObjectSlots) {
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000386 const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
Alex Lorenz60541c12015-07-09 19:55:27 +0000387 MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
388 MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
389 MFI.setHasStackMap(YamlMFI.HasStackMap);
390 MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
391 MFI.setStackSize(YamlMFI.StackSize);
392 MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
393 if (YamlMFI.MaxAlignment)
394 MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
395 MFI.setAdjustsStack(YamlMFI.AdjustsStack);
396 MFI.setHasCalls(YamlMFI.HasCalls);
397 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
398 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
399 MFI.setHasVAStart(YamlMFI.HasVAStart);
400 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000401
Alex Lorenzde491f02015-07-13 18:07:26 +0000402 // Initialize the fixed frame objects.
403 for (const auto &Object : YamlMF.FixedStackObjects) {
404 int ObjectIdx;
405 if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
406 ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset,
407 Object.IsImmutable, Object.IsAliased);
408 else
409 ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset);
410 MFI.setObjectAlignment(ObjectIdx, Object.Alignment);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000411 // TODO: Report an error when objects are redefined.
412 FixedStackObjectSlots.insert(std::make_pair(Object.ID, ObjectIdx));
Alex Lorenzde491f02015-07-13 18:07:26 +0000413 }
414
415 // Initialize the ordinary frame objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000416 for (const auto &Object : YamlMF.StackObjects) {
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000417 int ObjectIdx;
Alex Lorenz37643a02015-07-15 22:14:49 +0000418 const AllocaInst *Alloca = nullptr;
419 const yaml::StringValue &Name = Object.Name;
420 if (!Name.Value.empty()) {
421 Alloca = dyn_cast_or_null<AllocaInst>(
422 F.getValueSymbolTable().lookup(Name.Value));
423 if (!Alloca)
424 return error(Name.SourceRange.Start,
425 "alloca instruction named '" + Name.Value +
426 "' isn't defined in the function '" + F.getName() +
427 "'");
428 }
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000429 if (Object.Type == yaml::MachineStackObject::VariableSized)
Alex Lorenz37643a02015-07-15 22:14:49 +0000430 ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca);
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000431 else
432 ObjectIdx = MFI.CreateStackObject(
433 Object.Size, Object.Alignment,
Alex Lorenz37643a02015-07-15 22:14:49 +0000434 Object.Type == yaml::MachineStackObject::SpillSlot, Alloca);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000435 MFI.setObjectOffset(ObjectIdx, Object.Offset);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000436 // TODO: Report an error when objects are redefined.
437 StackObjectSlots.insert(std::make_pair(Object.ID, ObjectIdx));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000438 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000439 return false;
440}
441
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000442bool MIRParserImpl::initializeJumpTableInfo(
443 MachineFunction &MF, const yaml::MachineJumpTable &YamlJTI,
Alex Lorenz31d70682015-07-15 23:38:35 +0000444 PerFunctionMIParsingState &PFS) {
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000445 MachineJumpTableInfo *JTI = MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
446 SMDiagnostic Error;
447 for (const auto &Entry : YamlJTI.Entries) {
448 std::vector<MachineBasicBlock *> Blocks;
449 for (const auto &MBBSource : Entry.Blocks) {
450 MachineBasicBlock *MBB = nullptr;
451 if (parseMBBReference(MBB, SM, MF, MBBSource.Value, PFS, IRSlots, Error))
452 return error(Error, MBBSource.SourceRange);
453 Blocks.push_back(MBB);
454 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000455 unsigned Index = JTI->createJumpTableIndex(Blocks);
456 // TODO: Report an error when the same jump table slot ID is redefined.
457 PFS.JumpTableSlots.insert(std::make_pair(Entry.ID, Index));
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000458 }
459 return false;
460}
461
Alex Lorenz51af1602015-06-23 22:39:23 +0000462SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
463 SMRange SourceRange) {
464 assert(SourceRange.isValid() && "Invalid source range");
465 SMLoc Loc = SourceRange.Start;
466 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
467 *Loc.getPointer() == '\'';
468 // Translate the location of the error from the location in the MI string to
469 // the corresponding location in the MIR file.
470 Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
471 (HasQuote ? 1 : 0));
472
473 // TODO: Translate any source ranges as well.
474 return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
475 Error.getFixIts());
476}
477
Alex Lorenz09b832c2015-05-29 17:05:41 +0000478SMDiagnostic MIRParserImpl::diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
479 SMRange SourceRange) {
480 assert(SourceRange.isValid());
481
482 // Translate the location of the error from the location in the llvm IR string
483 // to the corresponding location in the MIR file.
484 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
485 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
486 unsigned Column = Error.getColumnNo();
487 StringRef LineStr = Error.getLineContents();
488 SMLoc Loc = Error.getLoc();
489
490 // Get the full line and adjust the column number by taking the indentation of
491 // LLVM IR into account.
492 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
493 L != E; ++L) {
494 if (L.line_number() == Line) {
495 LineStr = *L;
496 Loc = SMLoc::getFromPointer(LineStr.data());
497 auto Indent = LineStr.find(Error.getLineContents());
498 if (Indent != StringRef::npos)
499 Column += Indent;
500 break;
501 }
502 }
503
504 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
505 Error.getMessage(), LineStr, Error.getRanges(),
506 Error.getFixIts());
507}
508
Alex Lorenz28148ba2015-07-09 22:23:13 +0000509void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) {
510 if (!Names2RegClasses.empty())
511 return;
512 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
513 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
514 const auto *RC = TRI->getRegClass(I);
515 Names2RegClasses.insert(
516 std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
517 }
518}
519
520const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
521 StringRef Name) {
522 initNames2RegClasses(MF);
523 auto RegClassInfo = Names2RegClasses.find(Name);
524 if (RegClassInfo == Names2RegClasses.end())
525 return nullptr;
526 return RegClassInfo->getValue();
527}
528
Alex Lorenz735c47e2015-06-15 20:30:22 +0000529MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
530 : Impl(std::move(Impl)) {}
531
532MIRParser::~MIRParser() {}
533
534std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); }
535
536bool MIRParser::initializeMachineFunction(MachineFunction &MF) {
537 return Impl->initializeMachineFunction(MF);
538}
539
540std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
541 SMDiagnostic &Error,
542 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000543 auto FileOrErr = MemoryBuffer::getFile(Filename);
544 if (std::error_code EC = FileOrErr.getError()) {
545 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
546 "Could not open input file: " + EC.message());
Alex Lorenz735c47e2015-06-15 20:30:22 +0000547 return nullptr;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000548 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000549 return createMIRParser(std::move(FileOrErr.get()), Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000550}
551
Alex Lorenz735c47e2015-06-15 20:30:22 +0000552std::unique_ptr<MIRParser>
553llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
554 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000555 auto Filename = Contents->getBufferIdentifier();
Alex Lorenz735c47e2015-06-15 20:30:22 +0000556 return llvm::make_unique<MIRParser>(
557 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000558}