blob: 45b401917a3ee443fb1d5f97291962af45304e5d [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 Lorenzab4cbcf2015-07-24 20:35:40 +0000106 bool initializeRegisterInfo(MachineFunction &MF, MachineRegisterInfo &RegInfo,
107 const yaml::MachineFunction &YamlMF,
108 PerFunctionMIParsingState &PFS);
Alex Lorenz54565cf2015-06-24 19:56:10 +0000109
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000110 bool initializeFrameInfo(MachineFunction &MF, MachineFrameInfo &MFI,
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 Lorenz51af1602015-06-23 22:39:23 +0000130 /// Return a MIR diagnostic converted from an MI string diagnostic.
131 SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error,
132 SMRange SourceRange);
133
Alex Lorenz09b832c2015-05-29 17:05:41 +0000134 /// Return a MIR diagnostic converted from an LLVM assembly diagnostic.
135 SMDiagnostic diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
136 SMRange SourceRange);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000137
138 /// Create an empty function with the given name.
139 void createDummyFunction(StringRef Name, Module &M);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000140
141 void initNames2RegClasses(const MachineFunction &MF);
142
143 /// Check if the given identifier is a name of a register class.
144 ///
145 /// Return null if the name isn't a register class.
146 const TargetRegisterClass *getRegClass(const MachineFunction &MF,
147 StringRef Name);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000148};
149
Alex Lorenz735c47e2015-06-15 20:30:22 +0000150} // end namespace llvm
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000151
152MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
153 StringRef Filename, LLVMContext &Context)
154 : SM(), Filename(Filename), Context(Context) {
155 SM.AddNewSourceBuffer(std::move(Contents), SMLoc());
156}
157
Alex Lorenz735c47e2015-06-15 20:30:22 +0000158bool MIRParserImpl::error(const Twine &Message) {
159 Context.diagnose(DiagnosticInfoMIRParser(
160 DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
161 return true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000162}
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000163
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000164bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) {
165 Context.diagnose(DiagnosticInfoMIRParser(
166 DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message)));
167 return true;
168}
169
Alex Lorenz0fd7c622015-06-30 17:55:00 +0000170bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) {
171 assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error");
172 reportDiagnostic(diagFromMIStringDiag(Error, SourceRange));
173 return true;
174}
175
Alex Lorenz735c47e2015-06-15 20:30:22 +0000176void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
177 DiagnosticSeverity Kind;
178 switch (Diag.getKind()) {
179 case SourceMgr::DK_Error:
180 Kind = DS_Error;
181 break;
182 case SourceMgr::DK_Warning:
183 Kind = DS_Warning;
184 break;
185 case SourceMgr::DK_Note:
186 Kind = DS_Note;
187 break;
188 }
189 Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
190}
191
192static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
193 reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
194}
195
196std::unique_ptr<Module> MIRParserImpl::parse() {
Alex Lorenz78d78312015-05-28 22:41:12 +0000197 yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(),
Alex Lorenz735c47e2015-06-15 20:30:22 +0000198 /*Ctxt=*/nullptr, handleYAMLDiag, this);
Alex Lorenz51af1602015-06-23 22:39:23 +0000199 In.setContext(&In);
Alex Lorenz78d78312015-05-28 22:41:12 +0000200
201 if (!In.setCurrentDocument()) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000202 if (In.error())
Alex Lorenz78d78312015-05-28 22:41:12 +0000203 return nullptr;
204 // Create an empty module when the MIR file is empty.
205 return llvm::make_unique<Module>(Filename, Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000206 }
207
Alex Lorenz78d78312015-05-28 22:41:12 +0000208 std::unique_ptr<Module> M;
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000209 bool NoLLVMIR = false;
Alex Lorenz78d78312015-05-28 22:41:12 +0000210 // Parse the block scalar manually so that we can return unique pointer
211 // without having to go trough YAML traits.
212 if (const auto *BSN =
213 dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000214 SMDiagnostic Error;
Alex Lorenz78d78312015-05-28 22:41:12 +0000215 M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000216 Context, &IRSlots);
Alex Lorenz09b832c2015-05-29 17:05:41 +0000217 if (!M) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000218 reportDiagnostic(diagFromLLVMAssemblyDiag(Error, BSN->getSourceRange()));
Alex Lorenz78d78312015-05-28 22:41:12 +0000219 return M;
Alex Lorenz09b832c2015-05-29 17:05:41 +0000220 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000221 In.nextDocument();
222 if (!In.setCurrentDocument())
223 return M;
224 } else {
225 // Create an new, empty module.
226 M = llvm::make_unique<Module>(Filename, Context);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000227 NoLLVMIR = true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000228 }
229
230 // Parse the machine functions.
231 do {
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000232 if (parseMachineFunction(In, *M, NoLLVMIR))
Alex Lorenz78d78312015-05-28 22:41:12 +0000233 return nullptr;
234 In.nextDocument();
235 } while (In.setCurrentDocument());
236
237 return M;
238}
239
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000240bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M,
241 bool NoLLVMIR) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000242 auto MF = llvm::make_unique<yaml::MachineFunction>();
243 yaml::yamlize(In, *MF, false);
Alex Lorenz78d78312015-05-28 22:41:12 +0000244 if (In.error())
245 return true;
Alex Lorenz735c47e2015-06-15 20:30:22 +0000246 auto FunctionName = MF->Name;
Alex Lorenzfe2aa972015-06-15 22:23:23 +0000247 if (Functions.find(FunctionName) != Functions.end())
248 return error(Twine("redefinition of machine function '") + FunctionName +
249 "'");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000250 Functions.insert(std::make_pair(FunctionName, std::move(MF)));
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000251 if (NoLLVMIR)
252 createDummyFunction(FunctionName, M);
Alex Lorenz5ef16b82015-06-16 17:06:29 +0000253 else if (!M.getFunction(FunctionName))
254 return error(Twine("function '") + FunctionName +
255 "' isn't defined in the provided LLVM IR");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000256 return false;
257}
258
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000259void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
260 auto &Context = M.getContext();
261 Function *F = cast<Function>(M.getOrInsertFunction(
262 Name, FunctionType::get(Type::getVoidTy(Context), false)));
263 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
264 new UnreachableInst(Context, BB);
265}
266
Alex Lorenz735c47e2015-06-15 20:30:22 +0000267bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
268 auto It = Functions.find(MF.getName());
269 if (It == Functions.end())
270 return error(Twine("no machine function information for function '") +
271 MF.getName() + "' in the MIR file");
272 // TODO: Recreate the machine function.
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000273 const yaml::MachineFunction &YamlMF = *It->getValue();
274 if (YamlMF.Alignment)
275 MF.setAlignment(YamlMF.Alignment);
276 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
277 MF.setHasInlineAsm(YamlMF.HasInlineAsm);
Alex Lorenz53464512015-07-10 22:51:20 +0000278 PerFunctionMIParsingState PFS;
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000279 if (initializeRegisterInfo(MF, MF.getRegInfo(), YamlMF, PFS))
Alex Lorenz54565cf2015-06-24 19:56:10 +0000280 return true;
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000281 if (initializeFrameInfo(MF, *MF.getFrameInfo(), YamlMF, PFS))
Alex Lorenz60541c12015-07-09 19:55:27 +0000282 return true;
Alex Lorenzab980492015-07-20 20:51:18 +0000283 if (!YamlMF.Constants.empty()) {
284 auto *ConstantPool = MF.getConstantPool();
285 assert(ConstantPool && "Constant pool must be created");
286 if (initializeConstantPool(*ConstantPool, YamlMF, MF,
287 PFS.ConstantPoolSlots))
288 return true;
289 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000290
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000291 const auto &F = *MF.getFunction();
292 for (const auto &YamlMBB : YamlMF.BasicBlocks) {
293 const BasicBlock *BB = nullptr;
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000294 const yaml::StringValue &Name = YamlMBB.Name;
295 if (!Name.Value.empty()) {
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000296 BB = dyn_cast_or_null<BasicBlock>(
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000297 F.getValueSymbolTable().lookup(Name.Value));
Alex Lorenz00302df2015-06-19 20:12:03 +0000298 if (!BB)
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000299 return error(Name.SourceRange.Start,
300 Twine("basic block '") + Name.Value +
301 "' is not defined in the function '" + MF.getName() +
302 "'");
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000303 }
304 auto *MBB = MF.CreateMachineBasicBlock(BB);
305 MF.insert(MF.end(), MBB);
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000306 bool WasInserted =
307 PFS.MBBSlots.insert(std::make_pair(YamlMBB.ID, MBB)).second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000308 if (!WasInserted)
309 return error(Twine("redefinition of machine basic block with id #") +
310 Twine(YamlMBB.ID));
311 }
312
Alex Lorenzc8704b02015-07-09 21:21:33 +0000313 if (YamlMF.BasicBlocks.empty())
314 return error(Twine("machine function '") + Twine(MF.getName()) +
315 "' requires at least one machine basic block in its body");
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000316 // Initialize the jump table after creating all the MBBs so that the MBB
317 // references can be resolved.
318 if (!YamlMF.JumpTableInfo.Entries.empty() &&
319 initializeJumpTableInfo(MF, YamlMF.JumpTableInfo, PFS))
320 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000321 // Initialize the machine basic blocks after creating them all so that the
322 // machine instructions parser can resolve the MBB references.
323 unsigned I = 0;
324 for (const auto &YamlMBB : YamlMF.BasicBlocks) {
325 if (initializeMachineBasicBlock(MF, *MF.getBlockNumbered(I++), YamlMBB,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000326 PFS))
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000327 return true;
328 }
Alex Lorenzc7bf2042015-07-24 17:44:49 +0000329 // FIXME: This is a temporary workaround until the reserved registers can be
330 // serialized.
331 MF.getRegInfo().freezeReservedRegs(MF);
332 MF.verify();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000333 return false;
334}
335
336bool MIRParserImpl::initializeMachineBasicBlock(
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000337 MachineFunction &MF, MachineBasicBlock &MBB,
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000338 const yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000339 const PerFunctionMIParsingState &PFS) {
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000340 MBB.setAlignment(YamlMBB.Alignment);
341 if (YamlMBB.AddressTaken)
342 MBB.setHasAddressTaken();
343 MBB.setIsLandingPad(YamlMBB.IsLandingPad);
Alex Lorenzf09df002015-06-30 18:16:42 +0000344 SMDiagnostic Error;
345 // Parse the successors.
346 for (const auto &MBBSource : YamlMBB.Successors) {
347 MachineBasicBlock *SuccMBB = nullptr;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000348 if (parseMBBReference(SuccMBB, SM, MF, MBBSource.Value, PFS, IRSlots,
Alex Lorenzf09df002015-06-30 18:16:42 +0000349 Error))
350 return error(Error, MBBSource.SourceRange);
351 // TODO: Report an error when adding the same successor more than once.
352 MBB.addSuccessor(SuccMBB);
353 }
Alex Lorenz9fab3702015-07-14 21:24:41 +0000354 // Parse the liveins.
355 for (const auto &LiveInSource : YamlMBB.LiveIns) {
356 unsigned Reg = 0;
357 if (parseNamedRegisterReference(Reg, SM, MF, LiveInSource.Value, PFS,
358 IRSlots, Error))
359 return error(Error, LiveInSource.SourceRange);
360 MBB.addLiveIn(Reg);
361 }
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000362 // Parse the instructions.
363 for (const auto &MISource : YamlMBB.Instructions) {
Alex Lorenz3708a642015-06-30 17:47:50 +0000364 MachineInstr *MI = nullptr;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000365 if (parseMachineInstr(MI, SM, MF, MISource.Value, PFS, IRSlots, Error))
Alex Lorenz0fd7c622015-06-30 17:55:00 +0000366 return error(Error, MISource.SourceRange);
Alex Lorenz3708a642015-06-30 17:47:50 +0000367 MBB.insert(MBB.end(), MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000368 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000369 return false;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000370}
371
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000372bool MIRParserImpl::initializeRegisterInfo(MachineFunction &MF,
373 MachineRegisterInfo &RegInfo,
374 const yaml::MachineFunction &YamlMF,
375 PerFunctionMIParsingState &PFS) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000376 assert(RegInfo.isSSA());
377 if (!YamlMF.IsSSA)
378 RegInfo.leaveSSA();
379 assert(RegInfo.tracksLiveness());
380 if (!YamlMF.TracksRegLiveness)
381 RegInfo.invalidateLiveness();
382 RegInfo.enableSubRegLiveness(YamlMF.TracksSubRegLiveness);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000383
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000384 SMDiagnostic Error;
Alex Lorenz28148ba2015-07-09 22:23:13 +0000385 // Parse the virtual register information.
386 for (const auto &VReg : YamlMF.VirtualRegisters) {
387 const auto *RC = getRegClass(MF, VReg.Class.Value);
388 if (!RC)
389 return error(VReg.Class.SourceRange.Start,
390 Twine("use of undefined register class '") +
391 VReg.Class.Value + "'");
Alex Lorenz53464512015-07-10 22:51:20 +0000392 unsigned Reg = RegInfo.createVirtualRegister(RC);
393 // TODO: Report an error when the same virtual register with the same ID is
394 // redefined.
Alex Lorenzab4cbcf2015-07-24 20:35:40 +0000395 PFS.VirtualRegisterSlots.insert(std::make_pair(VReg.ID, Reg));
396 if (!VReg.PreferredRegister.Value.empty()) {
397 unsigned PreferredReg = 0;
398 if (parseNamedRegisterReference(PreferredReg, SM, MF,
399 VReg.PreferredRegister.Value, PFS,
400 IRSlots, Error))
401 return error(Error, VReg.PreferredRegister.SourceRange);
402 RegInfo.setSimpleHint(Reg, PreferredReg);
403 }
Alex Lorenz28148ba2015-07-09 22:23:13 +0000404 }
Alex Lorenz12045a42015-07-27 17:42:45 +0000405
406 // Parse the liveins.
407 for (const auto &LiveIn : YamlMF.LiveIns) {
408 unsigned Reg = 0;
409 if (parseNamedRegisterReference(Reg, SM, MF, LiveIn.Register.Value, PFS,
410 IRSlots, Error))
411 return error(Error, LiveIn.Register.SourceRange);
412 unsigned VReg = 0;
413 if (!LiveIn.VirtualRegister.Value.empty()) {
414 if (parseVirtualRegisterReference(
415 VReg, SM, MF, LiveIn.VirtualRegister.Value, PFS, IRSlots, Error))
416 return error(Error, LiveIn.VirtualRegister.SourceRange);
417 }
418 RegInfo.addLiveIn(Reg, VReg);
419 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000420 return false;
421}
422
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000423bool MIRParserImpl::initializeFrameInfo(MachineFunction &MF,
424 MachineFrameInfo &MFI,
425 const yaml::MachineFunction &YamlMF,
426 PerFunctionMIParsingState &PFS) {
427 const Function &F = *MF.getFunction();
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000428 const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
Alex Lorenz60541c12015-07-09 19:55:27 +0000429 MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
430 MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
431 MFI.setHasStackMap(YamlMFI.HasStackMap);
432 MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
433 MFI.setStackSize(YamlMFI.StackSize);
434 MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
435 if (YamlMFI.MaxAlignment)
436 MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
437 MFI.setAdjustsStack(YamlMFI.AdjustsStack);
438 MFI.setHasCalls(YamlMFI.HasCalls);
439 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
440 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
441 MFI.setHasVAStart(YamlMFI.HasVAStart);
442 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000443
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000444 std::vector<CalleeSavedInfo> CSIInfo;
Alex Lorenzde491f02015-07-13 18:07:26 +0000445 // Initialize the fixed frame objects.
446 for (const auto &Object : YamlMF.FixedStackObjects) {
447 int ObjectIdx;
448 if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
449 ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset,
450 Object.IsImmutable, Object.IsAliased);
451 else
452 ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset);
453 MFI.setObjectAlignment(ObjectIdx, Object.Alignment);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000454 // TODO: Report an error when objects are redefined.
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000455 PFS.FixedStackObjectSlots.insert(std::make_pair(Object.ID, ObjectIdx));
456 if (parseCalleeSavedRegister(MF, PFS, CSIInfo, Object.CalleeSavedRegister,
457 ObjectIdx))
458 return true;
Alex Lorenzde491f02015-07-13 18:07:26 +0000459 }
460
461 // Initialize the ordinary frame objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000462 for (const auto &Object : YamlMF.StackObjects) {
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000463 int ObjectIdx;
Alex Lorenz37643a02015-07-15 22:14:49 +0000464 const AllocaInst *Alloca = nullptr;
465 const yaml::StringValue &Name = Object.Name;
466 if (!Name.Value.empty()) {
467 Alloca = dyn_cast_or_null<AllocaInst>(
468 F.getValueSymbolTable().lookup(Name.Value));
469 if (!Alloca)
470 return error(Name.SourceRange.Start,
471 "alloca instruction named '" + Name.Value +
472 "' isn't defined in the function '" + F.getName() +
473 "'");
474 }
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000475 if (Object.Type == yaml::MachineStackObject::VariableSized)
Alex Lorenz37643a02015-07-15 22:14:49 +0000476 ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca);
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000477 else
478 ObjectIdx = MFI.CreateStackObject(
479 Object.Size, Object.Alignment,
Alex Lorenz37643a02015-07-15 22:14:49 +0000480 Object.Type == yaml::MachineStackObject::SpillSlot, Alloca);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000481 MFI.setObjectOffset(ObjectIdx, Object.Offset);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000482 // TODO: Report an error when objects are redefined.
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000483 PFS.StackObjectSlots.insert(std::make_pair(Object.ID, ObjectIdx));
484 if (parseCalleeSavedRegister(MF, PFS, CSIInfo, Object.CalleeSavedRegister,
485 ObjectIdx))
486 return true;
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000487 }
Alex Lorenz1bb48de2015-07-24 22:22:50 +0000488 MFI.setCalleeSavedInfo(CSIInfo);
489 if (!CSIInfo.empty())
490 MFI.setCalleeSavedInfoValid(true);
491 return false;
492}
493
494bool MIRParserImpl::parseCalleeSavedRegister(
495 MachineFunction &MF, PerFunctionMIParsingState &PFS,
496 std::vector<CalleeSavedInfo> &CSIInfo,
497 const yaml::StringValue &RegisterSource, int FrameIdx) {
498 if (RegisterSource.Value.empty())
499 return false;
500 unsigned Reg = 0;
501 SMDiagnostic Error;
502 if (parseNamedRegisterReference(Reg, SM, MF, RegisterSource.Value, PFS,
503 IRSlots, Error))
504 return error(Error, RegisterSource.SourceRange);
505 CSIInfo.push_back(CalleeSavedInfo(Reg, FrameIdx));
Alex Lorenz60541c12015-07-09 19:55:27 +0000506 return false;
507}
508
Alex Lorenzab980492015-07-20 20:51:18 +0000509bool MIRParserImpl::initializeConstantPool(
510 MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF,
511 const MachineFunction &MF,
512 DenseMap<unsigned, unsigned> &ConstantPoolSlots) {
513 const auto &M = *MF.getFunction()->getParent();
514 SMDiagnostic Error;
515 for (const auto &YamlConstant : YamlMF.Constants) {
516 const Constant *Value = dyn_cast_or_null<Constant>(
517 parseConstantValue(YamlConstant.Value.Value, Error, M));
518 if (!Value)
519 return error(Error, YamlConstant.Value.SourceRange);
520 unsigned Alignment =
521 YamlConstant.Alignment
522 ? YamlConstant.Alignment
523 : M.getDataLayout().getPrefTypeAlignment(Value->getType());
524 // TODO: Report an error when the same constant pool value ID is redefined.
525 ConstantPoolSlots.insert(std::make_pair(
526 YamlConstant.ID, ConstantPool.getConstantPoolIndex(Value, Alignment)));
527 }
528 return false;
529}
530
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000531bool MIRParserImpl::initializeJumpTableInfo(
532 MachineFunction &MF, const yaml::MachineJumpTable &YamlJTI,
Alex Lorenz31d70682015-07-15 23:38:35 +0000533 PerFunctionMIParsingState &PFS) {
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000534 MachineJumpTableInfo *JTI = MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
535 SMDiagnostic Error;
536 for (const auto &Entry : YamlJTI.Entries) {
537 std::vector<MachineBasicBlock *> Blocks;
538 for (const auto &MBBSource : Entry.Blocks) {
539 MachineBasicBlock *MBB = nullptr;
540 if (parseMBBReference(MBB, SM, MF, MBBSource.Value, PFS, IRSlots, Error))
541 return error(Error, MBBSource.SourceRange);
542 Blocks.push_back(MBB);
543 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000544 unsigned Index = JTI->createJumpTableIndex(Blocks);
545 // TODO: Report an error when the same jump table slot ID is redefined.
546 PFS.JumpTableSlots.insert(std::make_pair(Entry.ID, Index));
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000547 }
548 return false;
549}
550
Alex Lorenz51af1602015-06-23 22:39:23 +0000551SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
552 SMRange SourceRange) {
553 assert(SourceRange.isValid() && "Invalid source range");
554 SMLoc Loc = SourceRange.Start;
555 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
556 *Loc.getPointer() == '\'';
557 // Translate the location of the error from the location in the MI string to
558 // the corresponding location in the MIR file.
559 Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
560 (HasQuote ? 1 : 0));
561
562 // TODO: Translate any source ranges as well.
563 return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
564 Error.getFixIts());
565}
566
Alex Lorenz09b832c2015-05-29 17:05:41 +0000567SMDiagnostic MIRParserImpl::diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
568 SMRange SourceRange) {
569 assert(SourceRange.isValid());
570
571 // Translate the location of the error from the location in the llvm IR string
572 // to the corresponding location in the MIR file.
573 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
574 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
575 unsigned Column = Error.getColumnNo();
576 StringRef LineStr = Error.getLineContents();
577 SMLoc Loc = Error.getLoc();
578
579 // Get the full line and adjust the column number by taking the indentation of
580 // LLVM IR into account.
581 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
582 L != E; ++L) {
583 if (L.line_number() == Line) {
584 LineStr = *L;
585 Loc = SMLoc::getFromPointer(LineStr.data());
586 auto Indent = LineStr.find(Error.getLineContents());
587 if (Indent != StringRef::npos)
588 Column += Indent;
589 break;
590 }
591 }
592
593 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
594 Error.getMessage(), LineStr, Error.getRanges(),
595 Error.getFixIts());
596}
597
Alex Lorenz28148ba2015-07-09 22:23:13 +0000598void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) {
599 if (!Names2RegClasses.empty())
600 return;
601 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
602 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
603 const auto *RC = TRI->getRegClass(I);
604 Names2RegClasses.insert(
605 std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
606 }
607}
608
609const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
610 StringRef Name) {
611 initNames2RegClasses(MF);
612 auto RegClassInfo = Names2RegClasses.find(Name);
613 if (RegClassInfo == Names2RegClasses.end())
614 return nullptr;
615 return RegClassInfo->getValue();
616}
617
Alex Lorenz735c47e2015-06-15 20:30:22 +0000618MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
619 : Impl(std::move(Impl)) {}
620
621MIRParser::~MIRParser() {}
622
623std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); }
624
625bool MIRParser::initializeMachineFunction(MachineFunction &MF) {
626 return Impl->initializeMachineFunction(MF);
627}
628
629std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
630 SMDiagnostic &Error,
631 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000632 auto FileOrErr = MemoryBuffer::getFile(Filename);
633 if (std::error_code EC = FileOrErr.getError()) {
634 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
635 "Could not open input file: " + EC.message());
Alex Lorenz735c47e2015-06-15 20:30:22 +0000636 return nullptr;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000637 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000638 return createMIRParser(std::move(FileOrErr.get()), Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000639}
640
Alex Lorenz735c47e2015-06-15 20:30:22 +0000641std::unique_ptr<MIRParser>
642llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
643 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000644 auto Filename = Contents->getBufferIdentifier();
Alex Lorenz735c47e2015-06-15 20:30:22 +0000645 return llvm::make_unique<MIRParser>(
646 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000647}