blob: 1803116b417a18600f376c2084ca9ff8ac966bca [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 Lorenz2bdb4e12015-05-27 18:02:19 +000017#include "llvm/ADT/StringRef.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000018#include "llvm/ADT/StringMap.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000019#include "llvm/ADT/STLExtras.h"
20#include "llvm/AsmParser/Parser.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000021#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz54565cf2015-06-24 19:56:10 +000022#include "llvm/CodeGen/MachineRegisterInfo.h"
Alex Lorenz78d78312015-05-28 22:41:12 +000023#include "llvm/CodeGen/MIRYamlMapping.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000024#include "llvm/IR/BasicBlock.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000025#include "llvm/IR/DiagnosticInfo.h"
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000026#include "llvm/IR/Instructions.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000027#include "llvm/IR/LLVMContext.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000028#include "llvm/IR/Module.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000029#include "llvm/IR/ValueSymbolTable.h"
Alex Lorenz09b832c2015-05-29 17:05:41 +000030#include "llvm/Support/LineIterator.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000031#include "llvm/Support/SMLoc.h"
32#include "llvm/Support/SourceMgr.h"
33#include "llvm/Support/MemoryBuffer.h"
34#include "llvm/Support/YAMLTraits.h"
35#include <memory>
36
37using namespace llvm;
38
Alex Lorenz735c47e2015-06-15 20:30:22 +000039namespace llvm {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000040
41/// This class implements the parsing of LLVM IR that's embedded inside a MIR
42/// file.
43class MIRParserImpl {
44 SourceMgr SM;
45 StringRef Filename;
46 LLVMContext &Context;
Alex Lorenz735c47e2015-06-15 20:30:22 +000047 StringMap<std::unique_ptr<yaml::MachineFunction>> Functions;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000048
49public:
50 MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename,
51 LLVMContext &Context);
52
Alex Lorenz735c47e2015-06-15 20:30:22 +000053 void reportDiagnostic(const SMDiagnostic &Diag);
54
55 /// Report an error with the given message at unknown location.
56 ///
57 /// Always returns true.
58 bool error(const Twine &Message);
59
Alex Lorenz78d78312015-05-28 22:41:12 +000060 /// Try to parse the optional LLVM module and the machine functions in the MIR
61 /// file.
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000062 ///
Alex Lorenz78d78312015-05-28 22:41:12 +000063 /// Return null if an error occurred.
Alex Lorenz735c47e2015-06-15 20:30:22 +000064 std::unique_ptr<Module> parse();
Alex Lorenz78d78312015-05-28 22:41:12 +000065
66 /// Parse the machine function in the current YAML document.
67 ///
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000068 /// \param NoLLVMIR - set to true when the MIR file doesn't have LLVM IR.
69 /// A dummy IR function is created and inserted into the given module when
70 /// this parameter is true.
71 ///
Alex Lorenz78d78312015-05-28 22:41:12 +000072 /// Return true if an error occurred.
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000073 bool parseMachineFunction(yaml::Input &In, Module &M, bool NoLLVMIR);
Alex Lorenz09b832c2015-05-29 17:05:41 +000074
Alex Lorenz735c47e2015-06-15 20:30:22 +000075 /// Initialize the machine function to the state that's described in the MIR
76 /// file.
77 ///
78 /// Return true if error occurred.
79 bool initializeMachineFunction(MachineFunction &MF);
80
Alex Lorenz4f093bf2015-06-19 17:43:07 +000081 /// Initialize the machine basic block using it's YAML representation.
82 ///
83 /// Return true if an error occurred.
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000084 bool initializeMachineBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB,
Alex Lorenz4f093bf2015-06-19 17:43:07 +000085 const yaml::MachineBasicBlock &YamlMBB);
86
Alex Lorenz54565cf2015-06-24 19:56:10 +000087 bool initializeRegisterInfo(MachineRegisterInfo &RegInfo,
88 const yaml::MachineFunction &YamlMF);
89
Alex Lorenz09b832c2015-05-29 17:05:41 +000090private:
Alex Lorenz51af1602015-06-23 22:39:23 +000091 /// Return a MIR diagnostic converted from an MI string diagnostic.
92 SMDiagnostic diagFromMIStringDiag(const SMDiagnostic &Error,
93 SMRange SourceRange);
94
Alex Lorenz09b832c2015-05-29 17:05:41 +000095 /// Return a MIR diagnostic converted from an LLVM assembly diagnostic.
96 SMDiagnostic diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
97 SMRange SourceRange);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000098
99 /// Create an empty function with the given name.
100 void createDummyFunction(StringRef Name, Module &M);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000101};
102
Alex Lorenz735c47e2015-06-15 20:30:22 +0000103} // end namespace llvm
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000104
105MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
106 StringRef Filename, LLVMContext &Context)
107 : SM(), Filename(Filename), Context(Context) {
108 SM.AddNewSourceBuffer(std::move(Contents), SMLoc());
109}
110
Alex Lorenz735c47e2015-06-15 20:30:22 +0000111bool MIRParserImpl::error(const Twine &Message) {
112 Context.diagnose(DiagnosticInfoMIRParser(
113 DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
114 return true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000115}
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000116
Alex Lorenz735c47e2015-06-15 20:30:22 +0000117void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
118 DiagnosticSeverity Kind;
119 switch (Diag.getKind()) {
120 case SourceMgr::DK_Error:
121 Kind = DS_Error;
122 break;
123 case SourceMgr::DK_Warning:
124 Kind = DS_Warning;
125 break;
126 case SourceMgr::DK_Note:
127 Kind = DS_Note;
128 break;
129 }
130 Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
131}
132
133static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
134 reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
135}
136
137std::unique_ptr<Module> MIRParserImpl::parse() {
Alex Lorenz78d78312015-05-28 22:41:12 +0000138 yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(),
Alex Lorenz735c47e2015-06-15 20:30:22 +0000139 /*Ctxt=*/nullptr, handleYAMLDiag, this);
Alex Lorenz51af1602015-06-23 22:39:23 +0000140 In.setContext(&In);
Alex Lorenz78d78312015-05-28 22:41:12 +0000141
142 if (!In.setCurrentDocument()) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000143 if (In.error())
Alex Lorenz78d78312015-05-28 22:41:12 +0000144 return nullptr;
145 // Create an empty module when the MIR file is empty.
146 return llvm::make_unique<Module>(Filename, Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000147 }
148
Alex Lorenz78d78312015-05-28 22:41:12 +0000149 std::unique_ptr<Module> M;
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000150 bool NoLLVMIR = false;
Alex Lorenz78d78312015-05-28 22:41:12 +0000151 // Parse the block scalar manually so that we can return unique pointer
152 // without having to go trough YAML traits.
153 if (const auto *BSN =
154 dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000155 SMDiagnostic Error;
Alex Lorenz78d78312015-05-28 22:41:12 +0000156 M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
157 Context);
Alex Lorenz09b832c2015-05-29 17:05:41 +0000158 if (!M) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000159 reportDiagnostic(diagFromLLVMAssemblyDiag(Error, BSN->getSourceRange()));
Alex Lorenz78d78312015-05-28 22:41:12 +0000160 return M;
Alex Lorenz09b832c2015-05-29 17:05:41 +0000161 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000162 In.nextDocument();
163 if (!In.setCurrentDocument())
164 return M;
165 } else {
166 // Create an new, empty module.
167 M = llvm::make_unique<Module>(Filename, Context);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000168 NoLLVMIR = true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000169 }
170
171 // Parse the machine functions.
172 do {
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000173 if (parseMachineFunction(In, *M, NoLLVMIR))
Alex Lorenz78d78312015-05-28 22:41:12 +0000174 return nullptr;
175 In.nextDocument();
176 } while (In.setCurrentDocument());
177
178 return M;
179}
180
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000181bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M,
182 bool NoLLVMIR) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000183 auto MF = llvm::make_unique<yaml::MachineFunction>();
184 yaml::yamlize(In, *MF, false);
Alex Lorenz78d78312015-05-28 22:41:12 +0000185 if (In.error())
186 return true;
Alex Lorenz735c47e2015-06-15 20:30:22 +0000187 auto FunctionName = MF->Name;
Alex Lorenzfe2aa972015-06-15 22:23:23 +0000188 if (Functions.find(FunctionName) != Functions.end())
189 return error(Twine("redefinition of machine function '") + FunctionName +
190 "'");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000191 Functions.insert(std::make_pair(FunctionName, std::move(MF)));
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000192 if (NoLLVMIR)
193 createDummyFunction(FunctionName, M);
Alex Lorenz5ef16b82015-06-16 17:06:29 +0000194 else if (!M.getFunction(FunctionName))
195 return error(Twine("function '") + FunctionName +
196 "' isn't defined in the provided LLVM IR");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000197 return false;
198}
199
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000200void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
201 auto &Context = M.getContext();
202 Function *F = cast<Function>(M.getOrInsertFunction(
203 Name, FunctionType::get(Type::getVoidTy(Context), false)));
204 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
205 new UnreachableInst(Context, BB);
206}
207
Alex Lorenz735c47e2015-06-15 20:30:22 +0000208bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
209 auto It = Functions.find(MF.getName());
210 if (It == Functions.end())
211 return error(Twine("no machine function information for function '") +
212 MF.getName() + "' in the MIR file");
213 // TODO: Recreate the machine function.
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000214 const yaml::MachineFunction &YamlMF = *It->getValue();
215 if (YamlMF.Alignment)
216 MF.setAlignment(YamlMF.Alignment);
217 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
218 MF.setHasInlineAsm(YamlMF.HasInlineAsm);
Alex Lorenz54565cf2015-06-24 19:56:10 +0000219 if (initializeRegisterInfo(MF.getRegInfo(), YamlMF))
220 return true;
221
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000222 const auto &F = *MF.getFunction();
223 for (const auto &YamlMBB : YamlMF.BasicBlocks) {
224 const BasicBlock *BB = nullptr;
225 if (!YamlMBB.Name.empty()) {
226 BB = dyn_cast_or_null<BasicBlock>(
227 F.getValueSymbolTable().lookup(YamlMBB.Name));
Alex Lorenz00302df2015-06-19 20:12:03 +0000228 if (!BB)
229 return error(Twine("basic block '") + YamlMBB.Name +
230 "' is not defined in the function '" + MF.getName() + "'");
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000231 }
232 auto *MBB = MF.CreateMachineBasicBlock(BB);
233 MF.insert(MF.end(), MBB);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000234 if (initializeMachineBasicBlock(MF, *MBB, YamlMBB))
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000235 return true;
236 }
237 return false;
238}
239
240bool MIRParserImpl::initializeMachineBasicBlock(
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000241 MachineFunction &MF, MachineBasicBlock &MBB,
242 const yaml::MachineBasicBlock &YamlMBB) {
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000243 MBB.setAlignment(YamlMBB.Alignment);
244 if (YamlMBB.AddressTaken)
245 MBB.setHasAddressTaken();
246 MBB.setIsLandingPad(YamlMBB.IsLandingPad);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000247 // Parse the instructions.
248 for (const auto &MISource : YamlMBB.Instructions) {
249 SMDiagnostic Error;
Alex Lorenz51af1602015-06-23 22:39:23 +0000250 if (auto *MI = parseMachineInstr(SM, MF, MISource.Value, Error)) {
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000251 MBB.insert(MBB.end(), MI);
252 continue;
253 }
Alex Lorenz51af1602015-06-23 22:39:23 +0000254 reportDiagnostic(diagFromMIStringDiag(Error, MISource.SourceRange));
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000255 return true;
256 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000257 return false;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000258}
259
Alex Lorenz54565cf2015-06-24 19:56:10 +0000260bool MIRParserImpl::initializeRegisterInfo(
261 MachineRegisterInfo &RegInfo, const yaml::MachineFunction &YamlMF) {
262 assert(RegInfo.isSSA());
263 if (!YamlMF.IsSSA)
264 RegInfo.leaveSSA();
265 assert(RegInfo.tracksLiveness());
266 if (!YamlMF.TracksRegLiveness)
267 RegInfo.invalidateLiveness();
268 RegInfo.enableSubRegLiveness(YamlMF.TracksSubRegLiveness);
269 return false;
270}
271
Alex Lorenz51af1602015-06-23 22:39:23 +0000272SMDiagnostic MIRParserImpl::diagFromMIStringDiag(const SMDiagnostic &Error,
273 SMRange SourceRange) {
274 assert(SourceRange.isValid() && "Invalid source range");
275 SMLoc Loc = SourceRange.Start;
276 bool HasQuote = Loc.getPointer() < SourceRange.End.getPointer() &&
277 *Loc.getPointer() == '\'';
278 // Translate the location of the error from the location in the MI string to
279 // the corresponding location in the MIR file.
280 Loc = Loc.getFromPointer(Loc.getPointer() + Error.getColumnNo() +
281 (HasQuote ? 1 : 0));
282
283 // TODO: Translate any source ranges as well.
284 return SM.GetMessage(Loc, Error.getKind(), Error.getMessage(), None,
285 Error.getFixIts());
286}
287
Alex Lorenz09b832c2015-05-29 17:05:41 +0000288SMDiagnostic MIRParserImpl::diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
289 SMRange SourceRange) {
290 assert(SourceRange.isValid());
291
292 // Translate the location of the error from the location in the llvm IR string
293 // to the corresponding location in the MIR file.
294 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
295 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
296 unsigned Column = Error.getColumnNo();
297 StringRef LineStr = Error.getLineContents();
298 SMLoc Loc = Error.getLoc();
299
300 // Get the full line and adjust the column number by taking the indentation of
301 // LLVM IR into account.
302 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
303 L != E; ++L) {
304 if (L.line_number() == Line) {
305 LineStr = *L;
306 Loc = SMLoc::getFromPointer(LineStr.data());
307 auto Indent = LineStr.find(Error.getLineContents());
308 if (Indent != StringRef::npos)
309 Column += Indent;
310 break;
311 }
312 }
313
314 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
315 Error.getMessage(), LineStr, Error.getRanges(),
316 Error.getFixIts());
317}
318
Alex Lorenz735c47e2015-06-15 20:30:22 +0000319MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
320 : Impl(std::move(Impl)) {}
321
322MIRParser::~MIRParser() {}
323
324std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); }
325
326bool MIRParser::initializeMachineFunction(MachineFunction &MF) {
327 return Impl->initializeMachineFunction(MF);
328}
329
330std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
331 SMDiagnostic &Error,
332 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000333 auto FileOrErr = MemoryBuffer::getFile(Filename);
334 if (std::error_code EC = FileOrErr.getError()) {
335 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
336 "Could not open input file: " + EC.message());
Alex Lorenz735c47e2015-06-15 20:30:22 +0000337 return nullptr;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000338 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000339 return createMIRParser(std::move(FileOrErr.get()), Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000340}
341
Alex Lorenz735c47e2015-06-15 20:30:22 +0000342std::unique_ptr<MIRParser>
343llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
344 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000345 auto Filename = Contents->getBufferIdentifier();
Alex Lorenz735c47e2015-06-15 20:30:22 +0000346 return llvm::make_unique<MIRParser>(
347 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000348}