blob: 1ba7f1f1df22390abe0e598d4e7c0b26cc878d63 [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 Lorenz78d78312015-05-28 22:41:12 +000022#include "llvm/CodeGen/MIRYamlMapping.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000023#include "llvm/IR/BasicBlock.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000024#include "llvm/IR/DiagnosticInfo.h"
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000025#include "llvm/IR/Instructions.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000026#include "llvm/IR/LLVMContext.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000027#include "llvm/IR/Module.h"
Alex Lorenz4f093bf2015-06-19 17:43:07 +000028#include "llvm/IR/ValueSymbolTable.h"
Alex Lorenz09b832c2015-05-29 17:05:41 +000029#include "llvm/Support/LineIterator.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000030#include "llvm/Support/SMLoc.h"
31#include "llvm/Support/SourceMgr.h"
32#include "llvm/Support/MemoryBuffer.h"
33#include "llvm/Support/YAMLTraits.h"
34#include <memory>
35
36using namespace llvm;
37
Alex Lorenz735c47e2015-06-15 20:30:22 +000038namespace llvm {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000039
40/// This class implements the parsing of LLVM IR that's embedded inside a MIR
41/// file.
42class MIRParserImpl {
43 SourceMgr SM;
44 StringRef Filename;
45 LLVMContext &Context;
Alex Lorenz735c47e2015-06-15 20:30:22 +000046 StringMap<std::unique_ptr<yaml::MachineFunction>> Functions;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000047
48public:
49 MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename,
50 LLVMContext &Context);
51
Alex Lorenz735c47e2015-06-15 20:30:22 +000052 void reportDiagnostic(const SMDiagnostic &Diag);
53
54 /// Report an error with the given message at unknown location.
55 ///
56 /// Always returns true.
57 bool error(const Twine &Message);
58
Alex Lorenz78d78312015-05-28 22:41:12 +000059 /// Try to parse the optional LLVM module and the machine functions in the MIR
60 /// file.
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000061 ///
Alex Lorenz78d78312015-05-28 22:41:12 +000062 /// Return null if an error occurred.
Alex Lorenz735c47e2015-06-15 20:30:22 +000063 std::unique_ptr<Module> parse();
Alex Lorenz78d78312015-05-28 22:41:12 +000064
65 /// Parse the machine function in the current YAML document.
66 ///
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000067 /// \param NoLLVMIR - set to true when the MIR file doesn't have LLVM IR.
68 /// A dummy IR function is created and inserted into the given module when
69 /// this parameter is true.
70 ///
Alex Lorenz78d78312015-05-28 22:41:12 +000071 /// Return true if an error occurred.
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000072 bool parseMachineFunction(yaml::Input &In, Module &M, bool NoLLVMIR);
Alex Lorenz09b832c2015-05-29 17:05:41 +000073
Alex Lorenz735c47e2015-06-15 20:30:22 +000074 /// Initialize the machine function to the state that's described in the MIR
75 /// file.
76 ///
77 /// Return true if error occurred.
78 bool initializeMachineFunction(MachineFunction &MF);
79
Alex Lorenz4f093bf2015-06-19 17:43:07 +000080 /// Initialize the machine basic block using it's YAML representation.
81 ///
82 /// Return true if an error occurred.
Alex Lorenz8e0a1b42015-06-22 17:02:30 +000083 bool initializeMachineBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB,
Alex Lorenz4f093bf2015-06-19 17:43:07 +000084 const yaml::MachineBasicBlock &YamlMBB);
85
Alex Lorenz09b832c2015-05-29 17:05:41 +000086private:
87 /// Return a MIR diagnostic converted from an LLVM assembly diagnostic.
88 SMDiagnostic diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
89 SMRange SourceRange);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000090
91 /// Create an empty function with the given name.
92 void createDummyFunction(StringRef Name, Module &M);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000093};
94
Alex Lorenz735c47e2015-06-15 20:30:22 +000095} // end namespace llvm
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000096
97MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
98 StringRef Filename, LLVMContext &Context)
99 : SM(), Filename(Filename), Context(Context) {
100 SM.AddNewSourceBuffer(std::move(Contents), SMLoc());
101}
102
Alex Lorenz735c47e2015-06-15 20:30:22 +0000103bool MIRParserImpl::error(const Twine &Message) {
104 Context.diagnose(DiagnosticInfoMIRParser(
105 DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
106 return true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000107}
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000108
Alex Lorenz735c47e2015-06-15 20:30:22 +0000109void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
110 DiagnosticSeverity Kind;
111 switch (Diag.getKind()) {
112 case SourceMgr::DK_Error:
113 Kind = DS_Error;
114 break;
115 case SourceMgr::DK_Warning:
116 Kind = DS_Warning;
117 break;
118 case SourceMgr::DK_Note:
119 Kind = DS_Note;
120 break;
121 }
122 Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
123}
124
125static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
126 reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
127}
128
129std::unique_ptr<Module> MIRParserImpl::parse() {
Alex Lorenz78d78312015-05-28 22:41:12 +0000130 yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(),
Alex Lorenz735c47e2015-06-15 20:30:22 +0000131 /*Ctxt=*/nullptr, handleYAMLDiag, this);
Alex Lorenz78d78312015-05-28 22:41:12 +0000132
133 if (!In.setCurrentDocument()) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000134 if (In.error())
Alex Lorenz78d78312015-05-28 22:41:12 +0000135 return nullptr;
136 // Create an empty module when the MIR file is empty.
137 return llvm::make_unique<Module>(Filename, Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000138 }
139
Alex Lorenz78d78312015-05-28 22:41:12 +0000140 std::unique_ptr<Module> M;
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000141 bool NoLLVMIR = false;
Alex Lorenz78d78312015-05-28 22:41:12 +0000142 // Parse the block scalar manually so that we can return unique pointer
143 // without having to go trough YAML traits.
144 if (const auto *BSN =
145 dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000146 SMDiagnostic Error;
Alex Lorenz78d78312015-05-28 22:41:12 +0000147 M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
148 Context);
Alex Lorenz09b832c2015-05-29 17:05:41 +0000149 if (!M) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000150 reportDiagnostic(diagFromLLVMAssemblyDiag(Error, BSN->getSourceRange()));
Alex Lorenz78d78312015-05-28 22:41:12 +0000151 return M;
Alex Lorenz09b832c2015-05-29 17:05:41 +0000152 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000153 In.nextDocument();
154 if (!In.setCurrentDocument())
155 return M;
156 } else {
157 // Create an new, empty module.
158 M = llvm::make_unique<Module>(Filename, Context);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000159 NoLLVMIR = true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000160 }
161
162 // Parse the machine functions.
163 do {
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000164 if (parseMachineFunction(In, *M, NoLLVMIR))
Alex Lorenz78d78312015-05-28 22:41:12 +0000165 return nullptr;
166 In.nextDocument();
167 } while (In.setCurrentDocument());
168
169 return M;
170}
171
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000172bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M,
173 bool NoLLVMIR) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000174 auto MF = llvm::make_unique<yaml::MachineFunction>();
175 yaml::yamlize(In, *MF, false);
Alex Lorenz78d78312015-05-28 22:41:12 +0000176 if (In.error())
177 return true;
Alex Lorenz735c47e2015-06-15 20:30:22 +0000178 auto FunctionName = MF->Name;
Alex Lorenzfe2aa972015-06-15 22:23:23 +0000179 if (Functions.find(FunctionName) != Functions.end())
180 return error(Twine("redefinition of machine function '") + FunctionName +
181 "'");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000182 Functions.insert(std::make_pair(FunctionName, std::move(MF)));
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000183 if (NoLLVMIR)
184 createDummyFunction(FunctionName, M);
Alex Lorenz5ef16b82015-06-16 17:06:29 +0000185 else if (!M.getFunction(FunctionName))
186 return error(Twine("function '") + FunctionName +
187 "' isn't defined in the provided LLVM IR");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000188 return false;
189}
190
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000191void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
192 auto &Context = M.getContext();
193 Function *F = cast<Function>(M.getOrInsertFunction(
194 Name, FunctionType::get(Type::getVoidTy(Context), false)));
195 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
196 new UnreachableInst(Context, BB);
197}
198
Alex Lorenz735c47e2015-06-15 20:30:22 +0000199bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
200 auto It = Functions.find(MF.getName());
201 if (It == Functions.end())
202 return error(Twine("no machine function information for function '") +
203 MF.getName() + "' in the MIR file");
204 // TODO: Recreate the machine function.
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000205 const yaml::MachineFunction &YamlMF = *It->getValue();
206 if (YamlMF.Alignment)
207 MF.setAlignment(YamlMF.Alignment);
208 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
209 MF.setHasInlineAsm(YamlMF.HasInlineAsm);
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000210 const auto &F = *MF.getFunction();
211 for (const auto &YamlMBB : YamlMF.BasicBlocks) {
212 const BasicBlock *BB = nullptr;
213 if (!YamlMBB.Name.empty()) {
214 BB = dyn_cast_or_null<BasicBlock>(
215 F.getValueSymbolTable().lookup(YamlMBB.Name));
Alex Lorenz00302df2015-06-19 20:12:03 +0000216 if (!BB)
217 return error(Twine("basic block '") + YamlMBB.Name +
218 "' is not defined in the function '" + MF.getName() + "'");
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000219 }
220 auto *MBB = MF.CreateMachineBasicBlock(BB);
221 MF.insert(MF.end(), MBB);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000222 if (initializeMachineBasicBlock(MF, *MBB, YamlMBB))
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000223 return true;
224 }
225 return false;
226}
227
228bool MIRParserImpl::initializeMachineBasicBlock(
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000229 MachineFunction &MF, MachineBasicBlock &MBB,
230 const yaml::MachineBasicBlock &YamlMBB) {
Alex Lorenz4f093bf2015-06-19 17:43:07 +0000231 MBB.setAlignment(YamlMBB.Alignment);
232 if (YamlMBB.AddressTaken)
233 MBB.setHasAddressTaken();
234 MBB.setIsLandingPad(YamlMBB.IsLandingPad);
Alex Lorenz8e0a1b42015-06-22 17:02:30 +0000235 // Parse the instructions.
236 for (const auto &MISource : YamlMBB.Instructions) {
237 SMDiagnostic Error;
238 if (auto *MI = parseMachineInstr(SM, MF, MISource, Error)) {
239 MBB.insert(MBB.end(), MI);
240 continue;
241 }
242 reportDiagnostic(Error);
243 return true;
244 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000245 return false;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000246}
247
Alex Lorenz09b832c2015-05-29 17:05:41 +0000248SMDiagnostic MIRParserImpl::diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
249 SMRange SourceRange) {
250 assert(SourceRange.isValid());
251
252 // Translate the location of the error from the location in the llvm IR string
253 // to the corresponding location in the MIR file.
254 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
255 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
256 unsigned Column = Error.getColumnNo();
257 StringRef LineStr = Error.getLineContents();
258 SMLoc Loc = Error.getLoc();
259
260 // Get the full line and adjust the column number by taking the indentation of
261 // LLVM IR into account.
262 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
263 L != E; ++L) {
264 if (L.line_number() == Line) {
265 LineStr = *L;
266 Loc = SMLoc::getFromPointer(LineStr.data());
267 auto Indent = LineStr.find(Error.getLineContents());
268 if (Indent != StringRef::npos)
269 Column += Indent;
270 break;
271 }
272 }
273
274 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
275 Error.getMessage(), LineStr, Error.getRanges(),
276 Error.getFixIts());
277}
278
Alex Lorenz735c47e2015-06-15 20:30:22 +0000279MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
280 : Impl(std::move(Impl)) {}
281
282MIRParser::~MIRParser() {}
283
284std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); }
285
286bool MIRParser::initializeMachineFunction(MachineFunction &MF) {
287 return Impl->initializeMachineFunction(MF);
288}
289
290std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
291 SMDiagnostic &Error,
292 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000293 auto FileOrErr = MemoryBuffer::getFile(Filename);
294 if (std::error_code EC = FileOrErr.getError()) {
295 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
296 "Could not open input file: " + EC.message());
Alex Lorenz735c47e2015-06-15 20:30:22 +0000297 return nullptr;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000298 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000299 return createMIRParser(std::move(FileOrErr.get()), Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000300}
301
Alex Lorenz735c47e2015-06-15 20:30:22 +0000302std::unique_ptr<MIRParser>
303llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
304 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000305 auto Filename = Contents->getBufferIdentifier();
Alex Lorenz735c47e2015-06-15 20:30:22 +0000306 return llvm::make_unique<MIRParser>(
307 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000308}