blob: 1ba5db841affeb368c0f11adf4fef149a477850f [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 Lorenzf6bc8662015-07-10 18:13:57 +0000112 const yaml::MachineFunction &YamlMF);
Alex Lorenz60541c12015-07-09 19:55:27 +0000113
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000114 bool initializeJumpTableInfo(MachineFunction &MF,
115 const yaml::MachineJumpTable &YamlJTI,
116 const PerFunctionMIParsingState &PFS);
117
Alex Lorenz09b832c2015-05-29 17:05:41 +0000118private:
Alex Lorenz51af1602015-06-23 22:39:23 +0000119 /// Return a MIR diagnostic converted from an MI string diagnostic.
120 SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error,
121 SMRange SourceRange);
122
Alex Lorenz09b832c2015-05-29 17:05:41 +0000123 /// Return a MIR diagnostic converted from an LLVM assembly diagnostic.
124 SMDiagnostic diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
125 SMRange SourceRange);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000126
127 /// Create an empty function with the given name.
128 void createDummyFunction(StringRef Name, Module &M);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000129
130 void initNames2RegClasses(const MachineFunction &MF);
131
132 /// Check if the given identifier is a name of a register class.
133 ///
134 /// Return null if the name isn't a register class.
135 const TargetRegisterClass *getRegClass(const MachineFunction &MF,
136 StringRef Name);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000137};
138
Alex Lorenz735c47e2015-06-15 20:30:22 +0000139} // end namespace llvm
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000140
141MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
142 StringRef Filename, LLVMContext &Context)
143 : SM(), Filename(Filename), Context(Context) {
144 SM.AddNewSourceBuffer(std::move(Contents), SMLoc());
145}
146
Alex Lorenz735c47e2015-06-15 20:30:22 +0000147bool MIRParserImpl::error(const Twine &Message) {
148 Context.diagnose(DiagnosticInfoMIRParser(
149 DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
150 return true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000151}
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000152
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000153bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) {
154 Context.diagnose(DiagnosticInfoMIRParser(
155 DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message)));
156 return true;
157}
158
Alex Lorenz0fd7c622015-06-30 17:55:00 +0000159bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) {
160 assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error");
161 reportDiagnostic(diagFromMIStringDiag(Error, SourceRange));
162 return true;
163}
164
Alex Lorenz735c47e2015-06-15 20:30:22 +0000165void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
166 DiagnosticSeverity Kind;
167 switch (Diag.getKind()) {
168 case SourceMgr::DK_Error:
169 Kind = DS_Error;
170 break;
171 case SourceMgr::DK_Warning:
172 Kind = DS_Warning;
173 break;
174 case SourceMgr::DK_Note:
175 Kind = DS_Note;
176 break;
177 }
178 Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
179}
180
181static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
182 reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
183}
184
185std::unique_ptr<Module> MIRParserImpl::parse() {
Alex Lorenz78d78312015-05-28 22:41:12 +0000186 yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(),
Alex Lorenz735c47e2015-06-15 20:30:22 +0000187 /*Ctxt=*/nullptr, handleYAMLDiag, this);
Alex Lorenz51af1602015-06-23 22:39:23 +0000188 In.setContext(&In);
Alex Lorenz78d78312015-05-28 22:41:12 +0000189
190 if (!In.setCurrentDocument()) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000191 if (In.error())
Alex Lorenz78d78312015-05-28 22:41:12 +0000192 return nullptr;
193 // Create an empty module when the MIR file is empty.
194 return llvm::make_unique<Module>(Filename, Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000195 }
196
Alex Lorenz78d78312015-05-28 22:41:12 +0000197 std::unique_ptr<Module> M;
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000198 bool NoLLVMIR = false;
Alex Lorenz78d78312015-05-28 22:41:12 +0000199 // Parse the block scalar manually so that we can return unique pointer
200 // without having to go trough YAML traits.
201 if (const auto *BSN =
202 dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000203 SMDiagnostic Error;
Alex Lorenz78d78312015-05-28 22:41:12 +0000204 M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000205 Context, &IRSlots);
Alex Lorenz09b832c2015-05-29 17:05:41 +0000206 if (!M) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000207 reportDiagnostic(diagFromLLVMAssemblyDiag(Error, BSN->getSourceRange()));
Alex Lorenz78d78312015-05-28 22:41:12 +0000208 return M;
Alex Lorenz09b832c2015-05-29 17:05:41 +0000209 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000210 In.nextDocument();
211 if (!In.setCurrentDocument())
212 return M;
213 } else {
214 // Create an new, empty module.
215 M = llvm::make_unique<Module>(Filename, Context);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000216 NoLLVMIR = true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000217 }
218
219 // Parse the machine functions.
220 do {
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000221 if (parseMachineFunction(In, *M, NoLLVMIR))
Alex Lorenz78d78312015-05-28 22:41:12 +0000222 return nullptr;
223 In.nextDocument();
224 } while (In.setCurrentDocument());
225
226 return M;
227}
228
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000229bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M,
230 bool NoLLVMIR) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000231 auto MF = llvm::make_unique<yaml::MachineFunction>();
232 yaml::yamlize(In, *MF, false);
Alex Lorenz78d78312015-05-28 22:41:12 +0000233 if (In.error())
234 return true;
Alex Lorenz735c47e2015-06-15 20:30:22 +0000235 auto FunctionName = MF->Name;
Alex Lorenzfe2aa972015-06-15 22:23:23 +0000236 if (Functions.find(FunctionName) != Functions.end())
237 return error(Twine("redefinition of machine function '") + FunctionName +
238 "'");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000239 Functions.insert(std::make_pair(FunctionName, std::move(MF)));
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000240 if (NoLLVMIR)
241 createDummyFunction(FunctionName, M);
Alex Lorenz5ef16b82015-06-16 17:06:29 +0000242 else if (!M.getFunction(FunctionName))
243 return error(Twine("function '") + FunctionName +
244 "' isn't defined in the provided LLVM IR");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000245 return false;
246}
247
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000248void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
249 auto &Context = M.getContext();
250 Function *F = cast<Function>(M.getOrInsertFunction(
251 Name, FunctionType::get(Type::getVoidTy(Context), false)));
252 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
253 new UnreachableInst(Context, BB);
254}
255
Alex Lorenz735c47e2015-06-15 20:30:22 +0000256bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
257 auto It = Functions.find(MF.getName());
258 if (It == Functions.end())
259 return error(Twine("no machine function information for function '") +
260 MF.getName() + "' in the MIR file");
261 // TODO: Recreate the machine function.
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000262 const yaml::MachineFunction &YamlMF = *It->getValue();
263 if (YamlMF.Alignment)
264 MF.setAlignment(YamlMF.Alignment);
265 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
266 MF.setHasInlineAsm(YamlMF.HasInlineAsm);
Alex Lorenz53464512015-07-10 22:51:20 +0000267 PerFunctionMIParsingState PFS;
268 if (initializeRegisterInfo(MF, MF.getRegInfo(), YamlMF,
269 PFS.VirtualRegisterSlots))
Alex Lorenz54565cf2015-06-24 19:56:10 +0000270 return true;
Alex Lorenz37643a02015-07-15 22:14:49 +0000271 if (initializeFrameInfo(*MF.getFunction(), *MF.getFrameInfo(), YamlMF))
Alex Lorenz60541c12015-07-09 19:55:27 +0000272 return true;
Alex Lorenz54565cf2015-06-24 19:56:10 +0000273
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000274 const auto &F = *MF.getFunction();
275 for (const auto &YamlMBB : YamlMF.BasicBlocks) {
276 const BasicBlock *BB = nullptr;
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000277 const yaml::StringValue &Name = YamlMBB.Name;
278 if (!Name.Value.empty()) {
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000279 BB = dyn_cast_or_null<BasicBlock>(
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000280 F.getValueSymbolTable().lookup(Name.Value));
Alex Lorenz00302df2015-06-19 20:12:03 +0000281 if (!BB)
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000282 return error(Name.SourceRange.Start,
283 Twine("basic block '") + Name.Value +
284 "' is not defined in the function '" + MF.getName() +
285 "'");
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000286 }
287 auto *MBB = MF.CreateMachineBasicBlock(BB);
288 MF.insert(MF.end(), MBB);
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000289 bool WasInserted =
290 PFS.MBBSlots.insert(std::make_pair(YamlMBB.ID, MBB)).second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000291 if (!WasInserted)
292 return error(Twine("redefinition of machine basic block with id #") +
293 Twine(YamlMBB.ID));
294 }
295
Alex Lorenzc8704b02015-07-09 21:21:33 +0000296 if (YamlMF.BasicBlocks.empty())
297 return error(Twine("machine function '") + Twine(MF.getName()) +
298 "' requires at least one machine basic block in its body");
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000299 // Initialize the jump table after creating all the MBBs so that the MBB
300 // references can be resolved.
301 if (!YamlMF.JumpTableInfo.Entries.empty() &&
302 initializeJumpTableInfo(MF, YamlMF.JumpTableInfo, PFS))
303 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000304 // Initialize the machine basic blocks after creating them all so that the
305 // machine instructions parser can resolve the MBB references.
306 unsigned I = 0;
307 for (const auto &YamlMBB : YamlMF.BasicBlocks) {
308 if (initializeMachineBasicBlock(MF, *MF.getBlockNumbered(I++), YamlMBB,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000309 PFS))
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000310 return true;
311 }
312 return false;
313}
314
315bool MIRParserImpl::initializeMachineBasicBlock(
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000316 MachineFunction &MF, MachineBasicBlock &MBB,
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000317 const yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000318 const PerFunctionMIParsingState &PFS) {
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000319 MBB.setAlignment(YamlMBB.Alignment);
320 if (YamlMBB.AddressTaken)
321 MBB.setHasAddressTaken();
322 MBB.setIsLandingPad(YamlMBB.IsLandingPad);
Alex Lorenzf09df002015-06-30 18:16:42 +0000323 SMDiagnostic Error;
324 // Parse the successors.
325 for (const auto &MBBSource : YamlMBB.Successors) {
326 MachineBasicBlock *SuccMBB = nullptr;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000327 if (parseMBBReference(SuccMBB, SM, MF, MBBSource.Value, PFS, IRSlots,
Alex Lorenzf09df002015-06-30 18:16:42 +0000328 Error))
329 return error(Error, MBBSource.SourceRange);
330 // TODO: Report an error when adding the same successor more than once.
331 MBB.addSuccessor(SuccMBB);
332 }
Alex Lorenz9fab3702015-07-14 21:24:41 +0000333 // Parse the liveins.
334 for (const auto &LiveInSource : YamlMBB.LiveIns) {
335 unsigned Reg = 0;
336 if (parseNamedRegisterReference(Reg, SM, MF, LiveInSource.Value, PFS,
337 IRSlots, Error))
338 return error(Error, LiveInSource.SourceRange);
339 MBB.addLiveIn(Reg);
340 }
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000341 // Parse the instructions.
342 for (const auto &MISource : YamlMBB.Instructions) {
Alex Lorenz3708a642015-06-30 17:47:50 +0000343 MachineInstr *MI = nullptr;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000344 if (parseMachineInstr(MI, SM, MF, MISource.Value, PFS, IRSlots, Error))
Alex Lorenz0fd7c622015-06-30 17:55:00 +0000345 return error(Error, MISource.SourceRange);
Alex Lorenz3708a642015-06-30 17:47:50 +0000346 MBB.insert(MBB.end(), MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000347 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000348 return false;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000349}
350
Alex Lorenz54565cf2015-06-24 19:56:10 +0000351bool MIRParserImpl::initializeRegisterInfo(
Alex Lorenz28148ba2015-07-09 22:23:13 +0000352 const MachineFunction &MF, MachineRegisterInfo &RegInfo,
Alex Lorenz53464512015-07-10 22:51:20 +0000353 const yaml::MachineFunction &YamlMF,
354 DenseMap<unsigned, unsigned> &VirtualRegisterSlots) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000355 assert(RegInfo.isSSA());
356 if (!YamlMF.IsSSA)
357 RegInfo.leaveSSA();
358 assert(RegInfo.tracksLiveness());
359 if (!YamlMF.TracksRegLiveness)
360 RegInfo.invalidateLiveness();
361 RegInfo.enableSubRegLiveness(YamlMF.TracksSubRegLiveness);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000362
363 // Parse the virtual register information.
364 for (const auto &VReg : YamlMF.VirtualRegisters) {
365 const auto *RC = getRegClass(MF, VReg.Class.Value);
366 if (!RC)
367 return error(VReg.Class.SourceRange.Start,
368 Twine("use of undefined register class '") +
369 VReg.Class.Value + "'");
Alex Lorenz53464512015-07-10 22:51:20 +0000370 unsigned Reg = RegInfo.createVirtualRegister(RC);
371 // TODO: Report an error when the same virtual register with the same ID is
372 // redefined.
373 VirtualRegisterSlots.insert(std::make_pair(VReg.ID, Reg));
Alex Lorenz28148ba2015-07-09 22:23:13 +0000374 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000375 return false;
376}
377
Alex Lorenz37643a02015-07-15 22:14:49 +0000378bool MIRParserImpl::initializeFrameInfo(const Function &F,
379 MachineFrameInfo &MFI,
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000380 const yaml::MachineFunction &YamlMF) {
381 const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
Alex Lorenz60541c12015-07-09 19:55:27 +0000382 MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
383 MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
384 MFI.setHasStackMap(YamlMFI.HasStackMap);
385 MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
386 MFI.setStackSize(YamlMFI.StackSize);
387 MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
388 if (YamlMFI.MaxAlignment)
389 MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
390 MFI.setAdjustsStack(YamlMFI.AdjustsStack);
391 MFI.setHasCalls(YamlMFI.HasCalls);
392 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
393 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
394 MFI.setHasVAStart(YamlMFI.HasVAStart);
395 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000396
Alex Lorenzde491f02015-07-13 18:07:26 +0000397 // Initialize the fixed frame objects.
398 for (const auto &Object : YamlMF.FixedStackObjects) {
399 int ObjectIdx;
400 if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
401 ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset,
402 Object.IsImmutable, Object.IsAliased);
403 else
404 ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset);
405 MFI.setObjectAlignment(ObjectIdx, Object.Alignment);
406 // TODO: Store the mapping between fixed object IDs and object indices to
407 // parse fixed stack object references correctly.
408 }
409
410 // Initialize the ordinary frame objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000411 for (const auto &Object : YamlMF.StackObjects) {
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000412 int ObjectIdx;
Alex Lorenz37643a02015-07-15 22:14:49 +0000413 const AllocaInst *Alloca = nullptr;
414 const yaml::StringValue &Name = Object.Name;
415 if (!Name.Value.empty()) {
416 Alloca = dyn_cast_or_null<AllocaInst>(
417 F.getValueSymbolTable().lookup(Name.Value));
418 if (!Alloca)
419 return error(Name.SourceRange.Start,
420 "alloca instruction named '" + Name.Value +
421 "' isn't defined in the function '" + F.getName() +
422 "'");
423 }
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000424 if (Object.Type == yaml::MachineStackObject::VariableSized)
Alex Lorenz37643a02015-07-15 22:14:49 +0000425 ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca);
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000426 else
427 ObjectIdx = MFI.CreateStackObject(
428 Object.Size, Object.Alignment,
Alex Lorenz37643a02015-07-15 22:14:49 +0000429 Object.Type == yaml::MachineStackObject::SpillSlot, Alloca);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000430 MFI.setObjectOffset(ObjectIdx, Object.Offset);
431 // TODO: Store the mapping between object IDs and object indices to parse
432 // stack object references correctly.
433 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000434 return false;
435}
436
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000437bool MIRParserImpl::initializeJumpTableInfo(
438 MachineFunction &MF, const yaml::MachineJumpTable &YamlJTI,
439 const PerFunctionMIParsingState &PFS) {
440 MachineJumpTableInfo *JTI = MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
441 SMDiagnostic Error;
442 for (const auto &Entry : YamlJTI.Entries) {
443 std::vector<MachineBasicBlock *> Blocks;
444 for (const auto &MBBSource : Entry.Blocks) {
445 MachineBasicBlock *MBB = nullptr;
446 if (parseMBBReference(MBB, SM, MF, MBBSource.Value, PFS, IRSlots, Error))
447 return error(Error, MBBSource.SourceRange);
448 Blocks.push_back(MBB);
449 }
450 JTI->createJumpTableIndex(Blocks);
451 }
452 return false;
453}
454
Alex Lorenz51af1602015-06-23 22:39:23 +0000455SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
456 SMRange SourceRange) {
457 assert(SourceRange.isValid() && "Invalid source range");
458 SMLoc Loc = SourceRange.Start;
459 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
460 *Loc.getPointer() == '\'';
461 // Translate the location of the error from the location in the MI string to
462 // the corresponding location in the MIR file.
463 Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
464 (HasQuote ? 1 : 0));
465
466 // TODO: Translate any source ranges as well.
467 return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
468 Error.getFixIts());
469}
470
Alex Lorenz09b832c2015-05-29 17:05:41 +0000471SMDiagnostic MIRParserImpl::diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
472 SMRange SourceRange) {
473 assert(SourceRange.isValid());
474
475 // Translate the location of the error from the location in the llvm IR string
476 // to the corresponding location in the MIR file.
477 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
478 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
479 unsigned Column = Error.getColumnNo();
480 StringRef LineStr = Error.getLineContents();
481 SMLoc Loc = Error.getLoc();
482
483 // Get the full line and adjust the column number by taking the indentation of
484 // LLVM IR into account.
485 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
486 L != E; ++L) {
487 if (L.line_number() == Line) {
488 LineStr = *L;
489 Loc = SMLoc::getFromPointer(LineStr.data());
490 auto Indent = LineStr.find(Error.getLineContents());
491 if (Indent != StringRef::npos)
492 Column += Indent;
493 break;
494 }
495 }
496
497 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
498 Error.getMessage(), LineStr, Error.getRanges(),
499 Error.getFixIts());
500}
501
Alex Lorenz28148ba2015-07-09 22:23:13 +0000502void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) {
503 if (!Names2RegClasses.empty())
504 return;
505 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
506 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
507 const auto *RC = TRI->getRegClass(I);
508 Names2RegClasses.insert(
509 std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
510 }
511}
512
513const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
514 StringRef Name) {
515 initNames2RegClasses(MF);
516 auto RegClassInfo = Names2RegClasses.find(Name);
517 if (RegClassInfo == Names2RegClasses.end())
518 return nullptr;
519 return RegClassInfo->getValue();
520}
521
Alex Lorenz735c47e2015-06-15 20:30:22 +0000522MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
523 : Impl(std::move(Impl)) {}
524
525MIRParser::~MIRParser() {}
526
527std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); }
528
529bool MIRParser::initializeMachineFunction(MachineFunction &MF) {
530 return Impl->initializeMachineFunction(MF);
531}
532
533std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
534 SMDiagnostic &Error,
535 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000536 auto FileOrErr = MemoryBuffer::getFile(Filename);
537 if (std::error_code EC = FileOrErr.getError()) {
538 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
539 "Could not open input file: " + EC.message());
Alex Lorenz735c47e2015-06-15 20:30:22 +0000540 return nullptr;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000541 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000542 return createMIRParser(std::move(FileOrErr.get()), Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000543}
544
Alex Lorenz735c47e2015-06-15 20:30:22 +0000545std::unique_ptr<MIRParser>
546llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
547 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000548 auto Filename = Contents->getBufferIdentifier();
Alex Lorenz735c47e2015-06-15 20:30:22 +0000549 return llvm::make_unique<MIRParser>(
550 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000551}