blob: 612084c0b8d0701423e65ada03000dfe46663c81 [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 Lorenz60541c12015-07-09 19:55:27 +0000111 bool initializeFrameInfo(MachineFrameInfo &MFI,
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000112 const yaml::MachineFunction &YamlMF);
Alex Lorenz60541c12015-07-09 19:55:27 +0000113
Alex Lorenz09b832c2015-05-29 17:05:41 +0000114private:
Alex Lorenz51af1602015-06-23 22:39:23 +0000115 /// Return a MIR diagnostic converted from an MI string diagnostic.
116 SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error,
117 SMRange SourceRange);
118
Alex Lorenz09b832c2015-05-29 17:05:41 +0000119 /// Return a MIR diagnostic converted from an LLVM assembly diagnostic.
120 SMDiagnostic diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
121 SMRange SourceRange);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000122
123 /// Create an empty function with the given name.
124 void createDummyFunction(StringRef Name, Module &M);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000125
126 void initNames2RegClasses(const MachineFunction &MF);
127
128 /// Check if the given identifier is a name of a register class.
129 ///
130 /// Return null if the name isn't a register class.
131 const TargetRegisterClass *getRegClass(const MachineFunction &MF,
132 StringRef Name);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000133};
134
Alex Lorenz735c47e2015-06-15 20:30:22 +0000135} // end namespace llvm
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000136
137MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
138 StringRef Filename, LLVMContext &Context)
139 : SM(), Filename(Filename), Context(Context) {
140 SM.AddNewSourceBuffer(std::move(Contents), SMLoc());
141}
142
Alex Lorenz735c47e2015-06-15 20:30:22 +0000143bool MIRParserImpl::error(const Twine &Message) {
144 Context.diagnose(DiagnosticInfoMIRParser(
145 DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
146 return true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000147}
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000148
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000149bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) {
150 Context.diagnose(DiagnosticInfoMIRParser(
151 DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message)));
152 return true;
153}
154
Alex Lorenz0fd7c622015-06-30 17:55:00 +0000155bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) {
156 assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error");
157 reportDiagnostic(diagFromMIStringDiag(Error, SourceRange));
158 return true;
159}
160
Alex Lorenz735c47e2015-06-15 20:30:22 +0000161void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
162 DiagnosticSeverity Kind;
163 switch (Diag.getKind()) {
164 case SourceMgr::DK_Error:
165 Kind = DS_Error;
166 break;
167 case SourceMgr::DK_Warning:
168 Kind = DS_Warning;
169 break;
170 case SourceMgr::DK_Note:
171 Kind = DS_Note;
172 break;
173 }
174 Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
175}
176
177static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
178 reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
179}
180
181std::unique_ptr<Module> MIRParserImpl::parse() {
Alex Lorenz78d78312015-05-28 22:41:12 +0000182 yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(),
Alex Lorenz735c47e2015-06-15 20:30:22 +0000183 /*Ctxt=*/nullptr, handleYAMLDiag, this);
Alex Lorenz51af1602015-06-23 22:39:23 +0000184 In.setContext(&In);
Alex Lorenz78d78312015-05-28 22:41:12 +0000185
186 if (!In.setCurrentDocument()) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000187 if (In.error())
Alex Lorenz78d78312015-05-28 22:41:12 +0000188 return nullptr;
189 // Create an empty module when the MIR file is empty.
190 return llvm::make_unique<Module>(Filename, Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000191 }
192
Alex Lorenz78d78312015-05-28 22:41:12 +0000193 std::unique_ptr<Module> M;
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000194 bool NoLLVMIR = false;
Alex Lorenz78d78312015-05-28 22:41:12 +0000195 // Parse the block scalar manually so that we can return unique pointer
196 // without having to go trough YAML traits.
197 if (const auto *BSN =
198 dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000199 SMDiagnostic Error;
Alex Lorenz78d78312015-05-28 22:41:12 +0000200 M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000201 Context, &IRSlots);
Alex Lorenz09b832c2015-05-29 17:05:41 +0000202 if (!M) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000203 reportDiagnostic(diagFromLLVMAssemblyDiag(Error, BSN->getSourceRange()));
Alex Lorenz78d78312015-05-28 22:41:12 +0000204 return M;
Alex Lorenz09b832c2015-05-29 17:05:41 +0000205 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000206 In.nextDocument();
207 if (!In.setCurrentDocument())
208 return M;
209 } else {
210 // Create an new, empty module.
211 M = llvm::make_unique<Module>(Filename, Context);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000212 NoLLVMIR = true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000213 }
214
215 // Parse the machine functions.
216 do {
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000217 if (parseMachineFunction(In, *M, NoLLVMIR))
Alex Lorenz78d78312015-05-28 22:41:12 +0000218 return nullptr;
219 In.nextDocument();
220 } while (In.setCurrentDocument());
221
222 return M;
223}
224
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000225bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M,
226 bool NoLLVMIR) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000227 auto MF = llvm::make_unique<yaml::MachineFunction>();
228 yaml::yamlize(In, *MF, false);
Alex Lorenz78d78312015-05-28 22:41:12 +0000229 if (In.error())
230 return true;
Alex Lorenz735c47e2015-06-15 20:30:22 +0000231 auto FunctionName = MF->Name;
Alex Lorenzfe2aa972015-06-15 22:23:23 +0000232 if (Functions.find(FunctionName) != Functions.end())
233 return error(Twine("redefinition of machine function '") + FunctionName +
234 "'");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000235 Functions.insert(std::make_pair(FunctionName, std::move(MF)));
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000236 if (NoLLVMIR)
237 createDummyFunction(FunctionName, M);
Alex Lorenz5ef16b82015-06-16 17:06:29 +0000238 else if (!M.getFunction(FunctionName))
239 return error(Twine("function '") + FunctionName +
240 "' isn't defined in the provided LLVM IR");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000241 return false;
242}
243
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000244void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
245 auto &Context = M.getContext();
246 Function *F = cast<Function>(M.getOrInsertFunction(
247 Name, FunctionType::get(Type::getVoidTy(Context), false)));
248 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
249 new UnreachableInst(Context, BB);
250}
251
Alex Lorenz735c47e2015-06-15 20:30:22 +0000252bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
253 auto It = Functions.find(MF.getName());
254 if (It == Functions.end())
255 return error(Twine("no machine function information for function '") +
256 MF.getName() + "' in the MIR file");
257 // TODO: Recreate the machine function.
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000258 const yaml::MachineFunction &YamlMF = *It->getValue();
259 if (YamlMF.Alignment)
260 MF.setAlignment(YamlMF.Alignment);
261 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
262 MF.setHasInlineAsm(YamlMF.HasInlineAsm);
Alex Lorenz53464512015-07-10 22:51:20 +0000263 PerFunctionMIParsingState PFS;
264 if (initializeRegisterInfo(MF, MF.getRegInfo(), YamlMF,
265 PFS.VirtualRegisterSlots))
Alex Lorenz54565cf2015-06-24 19:56:10 +0000266 return true;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000267 if (initializeFrameInfo(*MF.getFrameInfo(), YamlMF))
Alex Lorenz60541c12015-07-09 19:55:27 +0000268 return true;
Alex Lorenz54565cf2015-06-24 19:56:10 +0000269
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000270 const auto &F = *MF.getFunction();
271 for (const auto &YamlMBB : YamlMF.BasicBlocks) {
272 const BasicBlock *BB = nullptr;
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000273 const yaml::StringValue &Name = YamlMBB.Name;
274 if (!Name.Value.empty()) {
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000275 BB = dyn_cast_or_null<BasicBlock>(
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000276 F.getValueSymbolTable().lookup(Name.Value));
Alex Lorenz00302df2015-06-19 20:12:03 +0000277 if (!BB)
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000278 return error(Name.SourceRange.Start,
279 Twine("basic block '") + Name.Value +
280 "' is not defined in the function '" + MF.getName() +
281 "'");
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000282 }
283 auto *MBB = MF.CreateMachineBasicBlock(BB);
284 MF.insert(MF.end(), MBB);
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000285 bool WasInserted =
286 PFS.MBBSlots.insert(std::make_pair(YamlMBB.ID, MBB)).second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000287 if (!WasInserted)
288 return error(Twine("redefinition of machine basic block with id #") +
289 Twine(YamlMBB.ID));
290 }
291
Alex Lorenzc8704b02015-07-09 21:21:33 +0000292 if (YamlMF.BasicBlocks.empty())
293 return error(Twine("machine function '") + Twine(MF.getName()) +
294 "' requires at least one machine basic block in its body");
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000295 // Initialize the machine basic blocks after creating them all so that the
296 // machine instructions parser can resolve the MBB references.
297 unsigned I = 0;
298 for (const auto &YamlMBB : YamlMF.BasicBlocks) {
299 if (initializeMachineBasicBlock(MF, *MF.getBlockNumbered(I++), YamlMBB,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000300 PFS))
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000301 return true;
302 }
303 return false;
304}
305
306bool MIRParserImpl::initializeMachineBasicBlock(
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000307 MachineFunction &MF, MachineBasicBlock &MBB,
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000308 const yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000309 const PerFunctionMIParsingState &PFS) {
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000310 MBB.setAlignment(YamlMBB.Alignment);
311 if (YamlMBB.AddressTaken)
312 MBB.setHasAddressTaken();
313 MBB.setIsLandingPad(YamlMBB.IsLandingPad);
Alex Lorenzf09df002015-06-30 18:16:42 +0000314 SMDiagnostic Error;
315 // Parse the successors.
316 for (const auto &MBBSource : YamlMBB.Successors) {
317 MachineBasicBlock *SuccMBB = nullptr;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000318 if (parseMBBReference(SuccMBB, SM, MF, MBBSource.Value, PFS, IRSlots,
Alex Lorenzf09df002015-06-30 18:16:42 +0000319 Error))
320 return error(Error, MBBSource.SourceRange);
321 // TODO: Report an error when adding the same successor more than once.
322 MBB.addSuccessor(SuccMBB);
323 }
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000324 // Parse the instructions.
325 for (const auto &MISource : YamlMBB.Instructions) {
Alex Lorenz3708a642015-06-30 17:47:50 +0000326 MachineInstr *MI = nullptr;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000327 if (parseMachineInstr(MI, SM, MF, MISource.Value, PFS, IRSlots, Error))
Alex Lorenz0fd7c622015-06-30 17:55:00 +0000328 return error(Error, MISource.SourceRange);
Alex Lorenz3708a642015-06-30 17:47:50 +0000329 MBB.insert(MBB.end(), MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000330 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000331 return false;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000332}
333
Alex Lorenz54565cf2015-06-24 19:56:10 +0000334bool MIRParserImpl::initializeRegisterInfo(
Alex Lorenz28148ba2015-07-09 22:23:13 +0000335 const MachineFunction &MF, MachineRegisterInfo &RegInfo,
Alex Lorenz53464512015-07-10 22:51:20 +0000336 const yaml::MachineFunction &YamlMF,
337 DenseMap<unsigned, unsigned> &VirtualRegisterSlots) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000338 assert(RegInfo.isSSA());
339 if (!YamlMF.IsSSA)
340 RegInfo.leaveSSA();
341 assert(RegInfo.tracksLiveness());
342 if (!YamlMF.TracksRegLiveness)
343 RegInfo.invalidateLiveness();
344 RegInfo.enableSubRegLiveness(YamlMF.TracksSubRegLiveness);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000345
346 // Parse the virtual register information.
347 for (const auto &VReg : YamlMF.VirtualRegisters) {
348 const auto *RC = getRegClass(MF, VReg.Class.Value);
349 if (!RC)
350 return error(VReg.Class.SourceRange.Start,
351 Twine("use of undefined register class '") +
352 VReg.Class.Value + "'");
Alex Lorenz53464512015-07-10 22:51:20 +0000353 unsigned Reg = RegInfo.createVirtualRegister(RC);
354 // TODO: Report an error when the same virtual register with the same ID is
355 // redefined.
356 VirtualRegisterSlots.insert(std::make_pair(VReg.ID, Reg));
Alex Lorenz28148ba2015-07-09 22:23:13 +0000357 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000358 return false;
359}
360
Alex Lorenz60541c12015-07-09 19:55:27 +0000361bool MIRParserImpl::initializeFrameInfo(MachineFrameInfo &MFI,
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000362 const yaml::MachineFunction &YamlMF) {
363 const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
Alex Lorenz60541c12015-07-09 19:55:27 +0000364 MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
365 MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
366 MFI.setHasStackMap(YamlMFI.HasStackMap);
367 MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
368 MFI.setStackSize(YamlMFI.StackSize);
369 MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
370 if (YamlMFI.MaxAlignment)
371 MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
372 MFI.setAdjustsStack(YamlMFI.AdjustsStack);
373 MFI.setHasCalls(YamlMFI.HasCalls);
374 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
375 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
376 MFI.setHasVAStart(YamlMFI.HasVAStart);
377 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000378
379 // Initialize the frame objects.
380 for (const auto &Object : YamlMF.StackObjects) {
381 int ObjectIdx = MFI.CreateStackObject(
382 Object.Size, Object.Alignment,
383 Object.Type == yaml::MachineStackObject::SpillSlot);
384 MFI.setObjectOffset(ObjectIdx, Object.Offset);
385 // TODO: Store the mapping between object IDs and object indices to parse
386 // stack object references correctly.
387 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000388 return false;
389}
390
Alex Lorenz51af1602015-06-23 22:39:23 +0000391SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
392 SMRange SourceRange) {
393 assert(SourceRange.isValid() && "Invalid source range");
394 SMLoc Loc = SourceRange.Start;
395 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
396 *Loc.getPointer() == '\'';
397 // Translate the location of the error from the location in the MI string to
398 // the corresponding location in the MIR file.
399 Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
400 (HasQuote ? 1 : 0));
401
402 // TODO: Translate any source ranges as well.
403 return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
404 Error.getFixIts());
405}
406
Alex Lorenz09b832c2015-05-29 17:05:41 +0000407SMDiagnostic MIRParserImpl::diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
408 SMRange SourceRange) {
409 assert(SourceRange.isValid());
410
411 // Translate the location of the error from the location in the llvm IR string
412 // to the corresponding location in the MIR file.
413 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
414 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
415 unsigned Column = Error.getColumnNo();
416 StringRef LineStr = Error.getLineContents();
417 SMLoc Loc = Error.getLoc();
418
419 // Get the full line and adjust the column number by taking the indentation of
420 // LLVM IR into account.
421 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
422 L != E; ++L) {
423 if (L.line_number() == Line) {
424 LineStr = *L;
425 Loc = SMLoc::getFromPointer(LineStr.data());
426 auto Indent = LineStr.find(Error.getLineContents());
427 if (Indent != StringRef::npos)
428 Column += Indent;
429 break;
430 }
431 }
432
433 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
434 Error.getMessage(), LineStr, Error.getRanges(),
435 Error.getFixIts());
436}
437
Alex Lorenz28148ba2015-07-09 22:23:13 +0000438void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) {
439 if (!Names2RegClasses.empty())
440 return;
441 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
442 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
443 const auto *RC = TRI->getRegClass(I);
444 Names2RegClasses.insert(
445 std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
446 }
447}
448
449const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
450 StringRef Name) {
451 initNames2RegClasses(MF);
452 auto RegClassInfo = Names2RegClasses.find(Name);
453 if (RegClassInfo == Names2RegClasses.end())
454 return nullptr;
455 return RegClassInfo->getValue();
456}
457
Alex Lorenz735c47e2015-06-15 20:30:22 +0000458MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
459 : Impl(std::move(Impl)) {}
460
461MIRParser::~MIRParser() {}
462
463std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); }
464
465bool MIRParser::initializeMachineFunction(MachineFunction &MF) {
466 return Impl->initializeMachineFunction(MF);
467}
468
469std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
470 SMDiagnostic &Error,
471 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000472 auto FileOrErr = MemoryBuffer::getFile(Filename);
473 if (std::error_code EC = FileOrErr.getError()) {
474 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
475 "Could not open input file: " + EC.message());
Alex Lorenz735c47e2015-06-15 20:30:22 +0000476 return nullptr;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000477 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000478 return createMIRParser(std::move(FileOrErr.get()), Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000479}
480
Alex Lorenz735c47e2015-06-15 20:30:22 +0000481std::unique_ptr<MIRParser>
482llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
483 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000484 auto Filename = Contents->getBufferIdentifier();
Alex Lorenz735c47e2015-06-15 20:30:22 +0000485 return llvm::make_unique<MIRParser>(
486 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000487}