blob: 8fe4c8380003b5c8bc6b9429c8a26bcdf1e75c2e [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"
16#include "llvm/ADT/StringRef.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000017#include "llvm/ADT/StringMap.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000018#include "llvm/ADT/STLExtras.h"
19#include "llvm/AsmParser/Parser.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000020#include "llvm/CodeGen/MachineFunction.h"
Alex Lorenz78d78312015-05-28 22:41:12 +000021#include "llvm/CodeGen/MIRYamlMapping.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000022#include "llvm/IR/DiagnosticInfo.h"
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000023#include "llvm/IR/Instructions.h"
Alex Lorenz735c47e2015-06-15 20:30:22 +000024#include "llvm/IR/LLVMContext.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000025#include "llvm/IR/Module.h"
Alex Lorenz09b832c2015-05-29 17:05:41 +000026#include "llvm/Support/LineIterator.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000027#include "llvm/Support/SMLoc.h"
28#include "llvm/Support/SourceMgr.h"
29#include "llvm/Support/MemoryBuffer.h"
30#include "llvm/Support/YAMLTraits.h"
31#include <memory>
32
33using namespace llvm;
34
Alex Lorenz735c47e2015-06-15 20:30:22 +000035namespace llvm {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000036
37/// This class implements the parsing of LLVM IR that's embedded inside a MIR
38/// file.
39class MIRParserImpl {
40 SourceMgr SM;
41 StringRef Filename;
42 LLVMContext &Context;
Alex Lorenz735c47e2015-06-15 20:30:22 +000043 StringMap<std::unique_ptr<yaml::MachineFunction>> Functions;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000044
45public:
46 MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename,
47 LLVMContext &Context);
48
Alex Lorenz735c47e2015-06-15 20:30:22 +000049 void reportDiagnostic(const SMDiagnostic &Diag);
50
51 /// Report an error with the given message at unknown location.
52 ///
53 /// Always returns true.
54 bool error(const Twine &Message);
55
Alex Lorenz78d78312015-05-28 22:41:12 +000056 /// Try to parse the optional LLVM module and the machine functions in the MIR
57 /// file.
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000058 ///
Alex Lorenz78d78312015-05-28 22:41:12 +000059 /// Return null if an error occurred.
Alex Lorenz735c47e2015-06-15 20:30:22 +000060 std::unique_ptr<Module> parse();
Alex Lorenz78d78312015-05-28 22:41:12 +000061
62 /// Parse the machine function in the current YAML document.
63 ///
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000064 /// \param NoLLVMIR - set to true when the MIR file doesn't have LLVM IR.
65 /// A dummy IR function is created and inserted into the given module when
66 /// this parameter is true.
67 ///
Alex Lorenz78d78312015-05-28 22:41:12 +000068 /// Return true if an error occurred.
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000069 bool parseMachineFunction(yaml::Input &In, Module &M, bool NoLLVMIR);
Alex Lorenz09b832c2015-05-29 17:05:41 +000070
Alex Lorenz735c47e2015-06-15 20:30:22 +000071 /// Initialize the machine function to the state that's described in the MIR
72 /// file.
73 ///
74 /// Return true if error occurred.
75 bool initializeMachineFunction(MachineFunction &MF);
76
Alex Lorenz09b832c2015-05-29 17:05:41 +000077private:
78 /// Return a MIR diagnostic converted from an LLVM assembly diagnostic.
79 SMDiagnostic diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
80 SMRange SourceRange);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +000081
82 /// Create an empty function with the given name.
83 void createDummyFunction(StringRef Name, Module &M);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000084};
85
Alex Lorenz735c47e2015-06-15 20:30:22 +000086} // end namespace llvm
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000087
88MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
89 StringRef Filename, LLVMContext &Context)
90 : SM(), Filename(Filename), Context(Context) {
91 SM.AddNewSourceBuffer(std::move(Contents), SMLoc());
92}
93
Alex Lorenz735c47e2015-06-15 20:30:22 +000094bool MIRParserImpl::error(const Twine &Message) {
95 Context.diagnose(DiagnosticInfoMIRParser(
96 DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
97 return true;
Alex Lorenz78d78312015-05-28 22:41:12 +000098}
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000099
Alex Lorenz735c47e2015-06-15 20:30:22 +0000100void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
101 DiagnosticSeverity Kind;
102 switch (Diag.getKind()) {
103 case SourceMgr::DK_Error:
104 Kind = DS_Error;
105 break;
106 case SourceMgr::DK_Warning:
107 Kind = DS_Warning;
108 break;
109 case SourceMgr::DK_Note:
110 Kind = DS_Note;
111 break;
112 }
113 Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
114}
115
116static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
117 reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
118}
119
120std::unique_ptr<Module> MIRParserImpl::parse() {
Alex Lorenz78d78312015-05-28 22:41:12 +0000121 yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(),
Alex Lorenz735c47e2015-06-15 20:30:22 +0000122 /*Ctxt=*/nullptr, handleYAMLDiag, this);
Alex Lorenz78d78312015-05-28 22:41:12 +0000123
124 if (!In.setCurrentDocument()) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000125 if (In.error())
Alex Lorenz78d78312015-05-28 22:41:12 +0000126 return nullptr;
127 // Create an empty module when the MIR file is empty.
128 return llvm::make_unique<Module>(Filename, Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000129 }
130
Alex Lorenz78d78312015-05-28 22:41:12 +0000131 std::unique_ptr<Module> M;
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000132 bool NoLLVMIR = false;
Alex Lorenz78d78312015-05-28 22:41:12 +0000133 // Parse the block scalar manually so that we can return unique pointer
134 // without having to go trough YAML traits.
135 if (const auto *BSN =
136 dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000137 SMDiagnostic Error;
Alex Lorenz78d78312015-05-28 22:41:12 +0000138 M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
139 Context);
Alex Lorenz09b832c2015-05-29 17:05:41 +0000140 if (!M) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000141 reportDiagnostic(diagFromLLVMAssemblyDiag(Error, BSN->getSourceRange()));
Alex Lorenz78d78312015-05-28 22:41:12 +0000142 return M;
Alex Lorenz09b832c2015-05-29 17:05:41 +0000143 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000144 In.nextDocument();
145 if (!In.setCurrentDocument())
146 return M;
147 } else {
148 // Create an new, empty module.
149 M = llvm::make_unique<Module>(Filename, Context);
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000150 NoLLVMIR = true;
Alex Lorenz78d78312015-05-28 22:41:12 +0000151 }
152
153 // Parse the machine functions.
154 do {
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000155 if (parseMachineFunction(In, *M, NoLLVMIR))
Alex Lorenz78d78312015-05-28 22:41:12 +0000156 return nullptr;
157 In.nextDocument();
158 } while (In.setCurrentDocument());
159
160 return M;
161}
162
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000163bool MIRParserImpl::parseMachineFunction(yaml::Input &In, Module &M,
164 bool NoLLVMIR) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000165 auto MF = llvm::make_unique<yaml::MachineFunction>();
166 yaml::yamlize(In, *MF, false);
Alex Lorenz78d78312015-05-28 22:41:12 +0000167 if (In.error())
168 return true;
Alex Lorenz735c47e2015-06-15 20:30:22 +0000169 auto FunctionName = MF->Name;
Alex Lorenzfe2aa972015-06-15 22:23:23 +0000170 if (Functions.find(FunctionName) != Functions.end())
171 return error(Twine("redefinition of machine function '") + FunctionName +
172 "'");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000173 Functions.insert(std::make_pair(FunctionName, std::move(MF)));
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000174 if (NoLLVMIR)
175 createDummyFunction(FunctionName, M);
Alex Lorenz5ef16b82015-06-16 17:06:29 +0000176 else if (!M.getFunction(FunctionName))
177 return error(Twine("function '") + FunctionName +
178 "' isn't defined in the provided LLVM IR");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000179 return false;
180}
181
Alex Lorenz8e7a58d72015-06-15 23:07:38 +0000182void MIRParserImpl::createDummyFunction(StringRef Name, Module &M) {
183 auto &Context = M.getContext();
184 Function *F = cast<Function>(M.getOrInsertFunction(
185 Name, FunctionType::get(Type::getVoidTy(Context), false)));
186 BasicBlock *BB = BasicBlock::Create(Context, "entry", F);
187 new UnreachableInst(Context, BB);
188}
189
Alex Lorenz735c47e2015-06-15 20:30:22 +0000190bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
191 auto It = Functions.find(MF.getName());
192 if (It == Functions.end())
193 return error(Twine("no machine function information for function '") +
194 MF.getName() + "' in the MIR file");
195 // TODO: Recreate the machine function.
Alex Lorenz5b5f9752015-06-16 00:10:47 +0000196 const yaml::MachineFunction &YamlMF = *It->getValue();
197 if (YamlMF.Alignment)
198 MF.setAlignment(YamlMF.Alignment);
199 MF.setExposesReturnsTwice(YamlMF.ExposesReturnsTwice);
200 MF.setHasInlineAsm(YamlMF.HasInlineAsm);
Alex Lorenz78d78312015-05-28 22:41:12 +0000201 return false;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000202}
203
Alex Lorenz09b832c2015-05-29 17:05:41 +0000204SMDiagnostic MIRParserImpl::diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
205 SMRange SourceRange) {
206 assert(SourceRange.isValid());
207
208 // Translate the location of the error from the location in the llvm IR string
209 // to the corresponding location in the MIR file.
210 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
211 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
212 unsigned Column = Error.getColumnNo();
213 StringRef LineStr = Error.getLineContents();
214 SMLoc Loc = Error.getLoc();
215
216 // Get the full line and adjust the column number by taking the indentation of
217 // LLVM IR into account.
218 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
219 L != E; ++L) {
220 if (L.line_number() == Line) {
221 LineStr = *L;
222 Loc = SMLoc::getFromPointer(LineStr.data());
223 auto Indent = LineStr.find(Error.getLineContents());
224 if (Indent != StringRef::npos)
225 Column += Indent;
226 break;
227 }
228 }
229
230 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
231 Error.getMessage(), LineStr, Error.getRanges(),
232 Error.getFixIts());
233}
234
Alex Lorenz735c47e2015-06-15 20:30:22 +0000235MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
236 : Impl(std::move(Impl)) {}
237
238MIRParser::~MIRParser() {}
239
240std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); }
241
242bool MIRParser::initializeMachineFunction(MachineFunction &MF) {
243 return Impl->initializeMachineFunction(MF);
244}
245
246std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
247 SMDiagnostic &Error,
248 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000249 auto FileOrErr = MemoryBuffer::getFile(Filename);
250 if (std::error_code EC = FileOrErr.getError()) {
251 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
252 "Could not open input file: " + EC.message());
Alex Lorenz735c47e2015-06-15 20:30:22 +0000253 return nullptr;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000254 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000255 return createMIRParser(std::move(FileOrErr.get()), Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000256}
257
Alex Lorenz735c47e2015-06-15 20:30:22 +0000258std::unique_ptr<MIRParser>
259llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
260 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000261 auto Filename = Contents->getBufferIdentifier();
Alex Lorenz735c47e2015-06-15 20:30:22 +0000262 return llvm::make_unique<MIRParser>(
263 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000264}