blob: 383fde1101a9724e6d7e55353122d1660df57046 [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 Lorenz53464512015-07-10 22:51:20 +0000106 bool
107 initializeRegisterInfo(const MachineFunction &MF,
108 MachineRegisterInfo &RegInfo,
109 const yaml::MachineFunction &YamlMF,
110 DenseMap<unsigned, unsigned> &VirtualRegisterSlots);
Alex Lorenz54565cf2015-06-24 19:56:10 +0000111
Alex Lorenz37643a02015-07-15 22:14:49 +0000112 bool initializeFrameInfo(const Function &F, MachineFrameInfo &MFI,
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000113 const yaml::MachineFunction &YamlMF,
114 DenseMap<unsigned, int> &StackObjectSlots,
115 DenseMap<unsigned, int> &FixedStackObjectSlots);
Alex Lorenz60541c12015-07-09 19:55:27 +0000116
Alex Lorenzab980492015-07-20 20:51:18 +0000117 bool initializeConstantPool(MachineConstantPool &ConstantPool,
118 const yaml::MachineFunction &YamlMF,
119 const MachineFunction &MF,
120 DenseMap<unsigned, unsigned> &ConstantPoolSlots);
121
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000122 bool initializeJumpTableInfo(MachineFunction &MF,
123 const yaml::MachineJumpTable &YamlJTI,
Alex Lorenz31d70682015-07-15 23:38:35 +0000124 PerFunctionMIParsingState &PFS);
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000125
Alex Lorenz09b832c2015-05-29 17:05:41 +0000126private:
Alex Lorenz51af1602015-06-23 22:39:23 +0000127 /// Return a MIR diagnostic converted from an MI string diagnostic.
128 SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error,
129 SMRange SourceRange);
130
Alex Lorenz09b832c2015-05-29 17:05:41 +0000131 /// Return a MIR diagnostic converted from an LLVM assembly diagnostic.
132 SMDiagnostic diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
133 SMRange SourceRange);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000134
135 /// Create an empty function with the given name.
136 void createDummyFunction(StringRef Name, Module &M);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000137
138 void initNames2RegClasses(const MachineFunction &MF);
139
140 /// Check if the given identifier is a name of a register class.
141 ///
142 /// Return null if the name isn't a register class.
143 const TargetRegisterClass *getRegClass(const MachineFunction &MF,
144 StringRef Name);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000145};
146
Alex Lorenz735c47e2015-06-15 20:30:22 +0000147} // end namespace llvm
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000148
149MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
150 StringRef Filename, LLVMContext &Context)
151 : SM(), Filename(Filename), Context(Context) {
152 SM.AddNewSourceBuffer(std::move(Contents), SMLoc());
153}
154
Alex Lorenz735c47e2015-06-15 20:30:22 +0000155bool MIRParserImpl::error(const Twine &Message) {
156 Context.diagnose(DiagnosticInfoMIRParser(
157 DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
158 return true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000159}
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000160
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000161bool MIRParserImpl::error(SMLoc Loc, const Twine &Message) {
162 Context.diagnose(DiagnosticInfoMIRParser(
163 DS_Error, SM.GetMessage(Loc, SourceMgr::DK_Error, Message)));
164 return true;
165}
166
Alex Lorenz0fd7c622015-06-30 17:55:00 +0000167bool MIRParserImpl::error(const SMDiagnostic &Error, SMRange SourceRange) {
168 assert(Error.getKind() == SourceMgr::DK_Error && "Expected an error");
169 reportDiagnostic(diagFromMIStringDiag(Error, SourceRange));
170 return true;
171}
172
Alex Lorenz735c47e2015-06-15 20:30:22 +0000173void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
174 DiagnosticSeverity Kind;
175 switch (Diag.getKind()) {
176 case SourceMgr::DK_Error:
177 Kind = DS_Error;
178 break;
179 case SourceMgr::DK_Warning:
180 Kind = DS_Warning;
181 break;
182 case SourceMgr::DK_Note:
183 Kind = DS_Note;
184 break;
185 }
186 Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
187}
188
189static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
190 reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
191}
192
193std::unique_ptr<Module> MIRParserImpl::parse() {
Alex Lorenz78d78312015-05-28 22:41:12 +0000194 yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(),
Alex Lorenz735c47e2015-06-15 20:30:22 +0000195 /*Ctxt=*/nullptr, handleYAMLDiag, this);
Alex Lorenz51af1602015-06-23 22:39:23 +0000196 In.setContext(&In);
Alex Lorenz78d78312015-05-28 22:41:12 +0000197
198 if (!In.setCurrentDocument()) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000199 if (In.error())
Alex Lorenz78d78312015-05-28 22:41:12 +0000200 return nullptr;
201 // Create an empty module when the MIR file is empty.
202 return llvm::make_unique<Module>(Filename, Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000203 }
204
Alex Lorenz78d78312015-05-28 22:41:12 +0000205 std::unique_ptr<Module> M;
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000206 bool NoLLVMIR = false;
Alex Lorenz78d78312015-05-28 22:41:12 +0000207 // Parse the block scalar manually so that we can return unique pointer
208 // without having to go trough YAML traits.
209 if (const auto *BSN =
210 dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000211 SMDiagnostic Error;
Alex Lorenz78d78312015-05-28 22:41:12 +0000212 M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
Alex Lorenz5d6108e2015-06-26 22:56:48 +0000213 Context, &IRSlots);
Alex Lorenz09b832c2015-05-29 17:05:41 +0000214 if (!M) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000215 reportDiagnostic(diagFromLLVMAssemblyDiag(Error, BSN->getSourceRange()));
Alex Lorenz78d78312015-05-28 22:41:12 +0000216 return M;
Alex Lorenz09b832c2015-05-29 17:05:41 +0000217 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000218 In.nextDocument();
219 if (!In.setCurrentDocument())
220 return M;
221 } else {
222 // Create an new, empty module.
223 M = llvm::make_unique<Module>(Filename, Context);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000224 NoLLVMIR = true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000225 }
226
227 // Parse the machine functions.
228 do {
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000229 if (parseMachineFunction(In, *M, NoLLVMIR))
Alex Lorenz78d78312015-05-28 22:41:12 +0000230 return nullptr;
231 In.nextDocument();
232 } while (In.setCurrentDocument());
233
234 return M;
235}
236
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000237bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M,
238 bool NoLLVMIR) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000239 auto MF = llvm::make_unique<yaml::MachineFunction>();
240 yaml::yamlize(In, *MF, false);
Alex Lorenz78d78312015-05-28 22:41:12 +0000241 if (In.error())
242 return true;
Alex Lorenz735c47e2015-06-15 20:30:22 +0000243 auto FunctionName = MF->Name;
Alex Lorenzfe2aa972015-06-15 22:23:23 +0000244 if (Functions.find(FunctionName) != Functions.end())
245 return error(Twine("redefinition of machine function '") + FunctionName +
246 "'");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000247 Functions.insert(std::make_pair(FunctionName, std::move(MF)));
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000248 if (NoLLVMIR)
249 createDummyFunction(FunctionName, M);
Alex Lorenz5ef16b82015-06-16 17:06:29 +0000250 else if (!M.getFunction(FunctionName))
251 return error(Twine("function '") + FunctionName +
252 "' isn't defined in the provided LLVM IR");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000253 return false;
254}
255
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000256void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
257 auto &Context = M.getContext();
258 Function *F = cast<Function>(M.getOrInsertFunction(
259 Name, FunctionType::get(Type::getVoidTy(Context), false)));
260 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
261 new UnreachableInst(Context, BB);
262}
263
Alex Lorenz735c47e2015-06-15 20:30:22 +0000264bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
265 auto It = Functions.find(MF.getName());
266 if (It == Functions.end())
267 return error(Twine("no machine function information for function '") +
268 MF.getName() + "' in the MIR file");
269 // TODO: Recreate the machine function.
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000270 const yaml::MachineFunction &YamlMF = *It->getValue();
271 if (YamlMF.Alignment)
272 MF.setAlignment(YamlMF.Alignment);
273 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
274 MF.setHasInlineAsm(YamlMF.HasInlineAsm);
Alex Lorenz53464512015-07-10 22:51:20 +0000275 PerFunctionMIParsingState PFS;
276 if (initializeRegisterInfo(MF, MF.getRegInfo(), YamlMF,
277 PFS.VirtualRegisterSlots))
Alex Lorenz54565cf2015-06-24 19:56:10 +0000278 return true;
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000279 if (initializeFrameInfo(*MF.getFunction(), *MF.getFrameInfo(), YamlMF,
280 PFS.StackObjectSlots, PFS.FixedStackObjectSlots))
Alex Lorenz60541c12015-07-09 19:55:27 +0000281 return true;
Alex Lorenzab980492015-07-20 20:51:18 +0000282 if (!YamlMF.Constants.empty()) {
283 auto *ConstantPool = MF.getConstantPool();
284 assert(ConstantPool && "Constant pool must be created");
285 if (initializeConstantPool(*ConstantPool, YamlMF, MF,
286 PFS.ConstantPoolSlots))
287 return true;
288 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000289
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000290 const auto &F = *MF.getFunction();
291 for (const auto &YamlMBB : YamlMF.BasicBlocks) {
292 const BasicBlock *BB = nullptr;
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000293 const yaml::StringValue &Name = YamlMBB.Name;
294 if (!Name.Value.empty()) {
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000295 BB = dyn_cast_or_null<BasicBlock>(
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000296 F.getValueSymbolTable().lookup(Name.Value));
Alex Lorenz00302df2015-06-19 20:12:03 +0000297 if (!BB)
Alex Lorenzb1f9ce82015-07-08 20:22:20 +0000298 return error(Name.SourceRange.Start,
299 Twine("basic block '") + Name.Value +
300 "' is not defined in the function '" + MF.getName() +
301 "'");
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000302 }
303 auto *MBB = MF.CreateMachineBasicBlock(BB);
304 MF.insert(MF.end(), MBB);
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000305 bool WasInserted =
306 PFS.MBBSlots.insert(std::make_pair(YamlMBB.ID, MBB)).second;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000307 if (!WasInserted)
308 return error(Twine("redefinition of machine basic block with id #") +
309 Twine(YamlMBB.ID));
310 }
311
Alex Lorenzc8704b02015-07-09 21:21:33 +0000312 if (YamlMF.BasicBlocks.empty())
313 return error(Twine("machine function '") + Twine(MF.getName()) +
314 "' requires at least one machine basic block in its body");
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000315 // Initialize the jump table after creating all the MBBs so that the MBB
316 // references can be resolved.
317 if (!YamlMF.JumpTableInfo.Entries.empty() &&
318 initializeJumpTableInfo(MF, YamlMF.JumpTableInfo, PFS))
319 return true;
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000320 // Initialize the machine basic blocks after creating them all so that the
321 // machine instructions parser can resolve the MBB references.
322 unsigned I = 0;
323 for (const auto &YamlMBB : YamlMF.BasicBlocks) {
324 if (initializeMachineBasicBlock(MF, *MF.getBlockNumbered(I++), YamlMBB,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000325 PFS))
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000326 return true;
327 }
Alex Lorenzc7bf2042015-07-24 17:44:49 +0000328 // FIXME: This is a temporary workaround until the reserved registers can be
329 // serialized.
330 MF.getRegInfo().freezeReservedRegs(MF);
331 MF.verify();
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000332 return false;
333}
334
335bool MIRParserImpl::initializeMachineBasicBlock(
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000336 MachineFunction &MF, MachineBasicBlock &MBB,
Alex Lorenz33f0aef2015-06-26 16:46:11 +0000337 const yaml::MachineBasicBlock &YamlMBB,
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000338 const PerFunctionMIParsingState &PFS) {
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000339 MBB.setAlignment(YamlMBB.Alignment);
340 if (YamlMBB.AddressTaken)
341 MBB.setHasAddressTaken();
342 MBB.setIsLandingPad(YamlMBB.IsLandingPad);
Alex Lorenzf09df002015-06-30 18:16:42 +0000343 SMDiagnostic Error;
344 // Parse the successors.
345 for (const auto &MBBSource : YamlMBB.Successors) {
346 MachineBasicBlock *SuccMBB = nullptr;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000347 if (parseMBBReference(SuccMBB, SM, MF, MBBSource.Value, PFS, IRSlots,
Alex Lorenzf09df002015-06-30 18:16:42 +0000348 Error))
349 return error(Error, MBBSource.SourceRange);
350 // TODO: Report an error when adding the same successor more than once.
351 MBB.addSuccessor(SuccMBB);
352 }
Alex Lorenz9fab3702015-07-14 21:24:41 +0000353 // Parse the liveins.
354 for (const auto &LiveInSource : YamlMBB.LiveIns) {
355 unsigned Reg = 0;
356 if (parseNamedRegisterReference(Reg, SM, MF, LiveInSource.Value, PFS,
357 IRSlots, Error))
358 return error(Error, LiveInSource.SourceRange);
359 MBB.addLiveIn(Reg);
360 }
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000361 // Parse the instructions.
362 for (const auto &MISource : YamlMBB.Instructions) {
Alex Lorenz3708a642015-06-30 17:47:50 +0000363 MachineInstr *MI = nullptr;
Alex Lorenz7a503fa2015-07-07 17:46:43 +0000364 if (parseMachineInstr(MI, SM, MF, MISource.Value, PFS, IRSlots, Error))
Alex Lorenz0fd7c622015-06-30 17:55:00 +0000365 return error(Error, MISource.SourceRange);
Alex Lorenz3708a642015-06-30 17:47:50 +0000366 MBB.insert(MBB.end(), MI);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000367 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000368 return false;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000369}
370
Alex Lorenz54565cf2015-06-24 19:56:10 +0000371bool MIRParserImpl::initializeRegisterInfo(
Alex Lorenz28148ba2015-07-09 22:23:13 +0000372 const MachineFunction &MF, MachineRegisterInfo &RegInfo,
Alex Lorenz53464512015-07-10 22:51:20 +0000373 const yaml::MachineFunction &YamlMF,
374 DenseMap<unsigned, unsigned> &VirtualRegisterSlots) {
Alex Lorenz54565cf2015-06-24 19:56:10 +0000375 assert(RegInfo.isSSA());
376 if (!YamlMF.IsSSA)
377 RegInfo.leaveSSA();
378 assert(RegInfo.tracksLiveness());
379 if (!YamlMF.TracksRegLiveness)
380 RegInfo.invalidateLiveness();
381 RegInfo.enableSubRegLiveness(YamlMF.TracksSubRegLiveness);
Alex Lorenz28148ba2015-07-09 22:23:13 +0000382
383 // Parse the virtual register information.
384 for (const auto &VReg : YamlMF.VirtualRegisters) {
385 const auto *RC = getRegClass(MF, VReg.Class.Value);
386 if (!RC)
387 return error(VReg.Class.SourceRange.Start,
388 Twine("use of undefined register class '") +
389 VReg.Class.Value + "'");
Alex Lorenz53464512015-07-10 22:51:20 +0000390 unsigned Reg = RegInfo.createVirtualRegister(RC);
391 // TODO: Report an error when the same virtual register with the same ID is
392 // redefined.
393 VirtualRegisterSlots.insert(std::make_pair(VReg.ID, Reg));
Alex Lorenz28148ba2015-07-09 22:23:13 +0000394 }
Alex Lorenz54565cf2015-06-24 19:56:10 +0000395 return false;
396}
397
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000398bool MIRParserImpl::initializeFrameInfo(
399 const Function &F, MachineFrameInfo &MFI,
400 const yaml::MachineFunction &YamlMF,
401 DenseMap<unsigned, int> &StackObjectSlots,
402 DenseMap<unsigned, int> &FixedStackObjectSlots) {
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000403 const yaml::MachineFrameInfo &YamlMFI = YamlMF.FrameInfo;
Alex Lorenz60541c12015-07-09 19:55:27 +0000404 MFI.setFrameAddressIsTaken(YamlMFI.IsFrameAddressTaken);
405 MFI.setReturnAddressIsTaken(YamlMFI.IsReturnAddressTaken);
406 MFI.setHasStackMap(YamlMFI.HasStackMap);
407 MFI.setHasPatchPoint(YamlMFI.HasPatchPoint);
408 MFI.setStackSize(YamlMFI.StackSize);
409 MFI.setOffsetAdjustment(YamlMFI.OffsetAdjustment);
410 if (YamlMFI.MaxAlignment)
411 MFI.ensureMaxAlignment(YamlMFI.MaxAlignment);
412 MFI.setAdjustsStack(YamlMFI.AdjustsStack);
413 MFI.setHasCalls(YamlMFI.HasCalls);
414 MFI.setMaxCallFrameSize(YamlMFI.MaxCallFrameSize);
415 MFI.setHasOpaqueSPAdjustment(YamlMFI.HasOpaqueSPAdjustment);
416 MFI.setHasVAStart(YamlMFI.HasVAStart);
417 MFI.setHasMustTailInVarArgFunc(YamlMFI.HasMustTailInVarArgFunc);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000418
Alex Lorenzde491f02015-07-13 18:07:26 +0000419 // Initialize the fixed frame objects.
420 for (const auto &Object : YamlMF.FixedStackObjects) {
421 int ObjectIdx;
422 if (Object.Type != yaml::FixedMachineStackObject::SpillSlot)
423 ObjectIdx = MFI.CreateFixedObject(Object.Size, Object.Offset,
424 Object.IsImmutable, Object.IsAliased);
425 else
426 ObjectIdx = MFI.CreateFixedSpillStackObject(Object.Size, Object.Offset);
427 MFI.setObjectAlignment(ObjectIdx, Object.Alignment);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000428 // TODO: Report an error when objects are redefined.
429 FixedStackObjectSlots.insert(std::make_pair(Object.ID, ObjectIdx));
Alex Lorenzde491f02015-07-13 18:07:26 +0000430 }
431
432 // Initialize the ordinary frame objects.
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000433 for (const auto &Object : YamlMF.StackObjects) {
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000434 int ObjectIdx;
Alex Lorenz37643a02015-07-15 22:14:49 +0000435 const AllocaInst *Alloca = nullptr;
436 const yaml::StringValue &Name = Object.Name;
437 if (!Name.Value.empty()) {
438 Alloca = dyn_cast_or_null<AllocaInst>(
439 F.getValueSymbolTable().lookup(Name.Value));
440 if (!Alloca)
441 return error(Name.SourceRange.Start,
442 "alloca instruction named '" + Name.Value +
443 "' isn't defined in the function '" + F.getName() +
444 "'");
445 }
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000446 if (Object.Type == yaml::MachineStackObject::VariableSized)
Alex Lorenz37643a02015-07-15 22:14:49 +0000447 ObjectIdx = MFI.CreateVariableSizedObject(Object.Alignment, Alloca);
Alex Lorenz418f3ec2015-07-14 00:26:26 +0000448 else
449 ObjectIdx = MFI.CreateStackObject(
450 Object.Size, Object.Alignment,
Alex Lorenz37643a02015-07-15 22:14:49 +0000451 Object.Type == yaml::MachineStackObject::SpillSlot, Alloca);
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000452 MFI.setObjectOffset(ObjectIdx, Object.Offset);
Alex Lorenz7feaf7c2015-07-16 23:37:45 +0000453 // TODO: Report an error when objects are redefined.
454 StackObjectSlots.insert(std::make_pair(Object.ID, ObjectIdx));
Alex Lorenzf6bc8662015-07-10 18:13:57 +0000455 }
Alex Lorenz60541c12015-07-09 19:55:27 +0000456 return false;
457}
458
Alex Lorenzab980492015-07-20 20:51:18 +0000459bool MIRParserImpl::initializeConstantPool(
460 MachineConstantPool &ConstantPool, const yaml::MachineFunction &YamlMF,
461 const MachineFunction &MF,
462 DenseMap<unsigned, unsigned> &ConstantPoolSlots) {
463 const auto &M = *MF.getFunction()->getParent();
464 SMDiagnostic Error;
465 for (const auto &YamlConstant : YamlMF.Constants) {
466 const Constant *Value = dyn_cast_or_null<Constant>(
467 parseConstantValue(YamlConstant.Value.Value, Error, M));
468 if (!Value)
469 return error(Error, YamlConstant.Value.SourceRange);
470 unsigned Alignment =
471 YamlConstant.Alignment
472 ? YamlConstant.Alignment
473 : M.getDataLayout().getPrefTypeAlignment(Value->getType());
474 // TODO: Report an error when the same constant pool value ID is redefined.
475 ConstantPoolSlots.insert(std::make_pair(
476 YamlConstant.ID, ConstantPool.getConstantPoolIndex(Value, Alignment)));
477 }
478 return false;
479}
480
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000481bool MIRParserImpl::initializeJumpTableInfo(
482 MachineFunction &MF, const yaml::MachineJumpTable &YamlJTI,
Alex Lorenz31d70682015-07-15 23:38:35 +0000483 PerFunctionMIParsingState &PFS) {
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000484 MachineJumpTableInfo *JTI = MF.getOrCreateJumpTableInfo(YamlJTI.Kind);
485 SMDiagnostic Error;
486 for (const auto &Entry : YamlJTI.Entries) {
487 std::vector<MachineBasicBlock *> Blocks;
488 for (const auto &MBBSource : Entry.Blocks) {
489 MachineBasicBlock *MBB = nullptr;
490 if (parseMBBReference(MBB, SM, MF, MBBSource.Value, PFS, IRSlots, Error))
491 return error(Error, MBBSource.SourceRange);
492 Blocks.push_back(MBB);
493 }
Alex Lorenz31d70682015-07-15 23:38:35 +0000494 unsigned Index = JTI->createJumpTableIndex(Blocks);
495 // TODO: Report an error when the same jump table slot ID is redefined.
496 PFS.JumpTableSlots.insert(std::make_pair(Entry.ID, Index));
Alex Lorenz6799e9b2015-07-15 23:31:07 +0000497 }
498 return false;
499}
500
Alex Lorenz51af1602015-06-23 22:39:23 +0000501SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
502 SMRange SourceRange) {
503 assert(SourceRange.isValid() && "Invalid source range");
504 SMLoc Loc = SourceRange.Start;
505 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
506 *Loc.getPointer() == '\'';
507 // Translate the location of the error from the location in the MI string to
508 // the corresponding location in the MIR file.
509 Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
510 (HasQuote ? 1 : 0));
511
512 // TODO: Translate any source ranges as well.
513 return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
514 Error.getFixIts());
515}
516
Alex Lorenz09b832c2015-05-29 17:05:41 +0000517SMDiagnostic MIRParserImpl::diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
518 SMRange SourceRange) {
519 assert(SourceRange.isValid());
520
521 // Translate the location of the error from the location in the llvm IR string
522 // to the corresponding location in the MIR file.
523 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
524 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
525 unsigned Column = Error.getColumnNo();
526 StringRef LineStr = Error.getLineContents();
527 SMLoc Loc = Error.getLoc();
528
529 // Get the full line and adjust the column number by taking the indentation of
530 // LLVM IR into account.
531 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
532 L != E; ++L) {
533 if (L.line_number() == Line) {
534 LineStr = *L;
535 Loc = SMLoc::getFromPointer(LineStr.data());
536 auto Indent = LineStr.find(Error.getLineContents());
537 if (Indent != StringRef::npos)
538 Column += Indent;
539 break;
540 }
541 }
542
543 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
544 Error.getMessage(), LineStr, Error.getRanges(),
545 Error.getFixIts());
546}
547
Alex Lorenz28148ba2015-07-09 22:23:13 +0000548void MIRParserImpl::initNames2RegClasses(const MachineFunction &MF) {
549 if (!Names2RegClasses.empty())
550 return;
551 const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
552 for (unsigned I = 0, E = TRI->getNumRegClasses(); I < E; ++I) {
553 const auto *RC = TRI->getRegClass(I);
554 Names2RegClasses.insert(
555 std::make_pair(StringRef(TRI->getRegClassName(RC)).lower(), RC));
556 }
557}
558
559const TargetRegisterClass *MIRParserImpl::getRegClass(const MachineFunction &MF,
560 StringRef Name) {
561 initNames2RegClasses(MF);
562 auto RegClassInfo = Names2RegClasses.find(Name);
563 if (RegClassInfo == Names2RegClasses.end())
564 return nullptr;
565 return RegClassInfo->getValue();
566}
567
Alex Lorenz735c47e2015-06-15 20:30:22 +0000568MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
569 : Impl(std::move(Impl)) {}
570
571MIRParser::~MIRParser() {}
572
573std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); }
574
575bool MIRParser::initializeMachineFunction(MachineFunction &MF) {
576 return Impl->initializeMachineFunction(MF);
577}
578
579std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
580 SMDiagnostic &Error,
581 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000582 auto FileOrErr = MemoryBuffer::getFile(Filename);
583 if (std::error_code EC = FileOrErr.getError()) {
584 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
585 "Could not open input file: " + EC.message());
Alex Lorenz735c47e2015-06-15 20:30:22 +0000586 return nullptr;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000587 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000588 return createMIRParser(std::move(FileOrErr.get()), Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000589}
590
Alex Lorenz735c47e2015-06-15 20:30:22 +0000591std::unique_ptr<MIRParser>
592llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
593 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000594 auto Filename = Contents->getBufferIdentifier();
Alex Lorenz735c47e2015-06-15 20:30:22 +0000595 return llvm::make_unique<MIRParser>(
596 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000597}