blob: 19e845fc01fe2f582999d1ec36be664a312f19db [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"
23#include "llvm/IR/LLVMContext.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000024#include "llvm/IR/Module.h"
Alex Lorenz09b832c2015-05-29 17:05:41 +000025#include "llvm/Support/LineIterator.h"
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000026#include "llvm/Support/SMLoc.h"
27#include "llvm/Support/SourceMgr.h"
28#include "llvm/Support/MemoryBuffer.h"
29#include "llvm/Support/YAMLTraits.h"
30#include <memory>
31
32using namespace llvm;
33
Alex Lorenz735c47e2015-06-15 20:30:22 +000034namespace llvm {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000035
36/// This class implements the parsing of LLVM IR that's embedded inside a MIR
37/// file.
38class MIRParserImpl {
39 SourceMgr SM;
40 StringRef Filename;
41 LLVMContext &Context;
Alex Lorenz735c47e2015-06-15 20:30:22 +000042 StringMap<std::unique_ptr<yaml::MachineFunction>> Functions;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000043
44public:
45 MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename,
46 LLVMContext &Context);
47
Alex Lorenz735c47e2015-06-15 20:30:22 +000048 void reportDiagnostic(const SMDiagnostic &Diag);
49
50 /// Report an error with the given message at unknown location.
51 ///
52 /// Always returns true.
53 bool error(const Twine &Message);
54
Alex Lorenz78d78312015-05-28 22:41:12 +000055 /// Try to parse the optional LLVM module and the machine functions in the MIR
56 /// file.
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000057 ///
Alex Lorenz78d78312015-05-28 22:41:12 +000058 /// Return null if an error occurred.
Alex Lorenz735c47e2015-06-15 20:30:22 +000059 std::unique_ptr<Module> parse();
Alex Lorenz78d78312015-05-28 22:41:12 +000060
61 /// Parse the machine function in the current YAML document.
62 ///
63 /// Return true if an error occurred.
64 bool parseMachineFunction(yaml::Input &In);
Alex Lorenz09b832c2015-05-29 17:05:41 +000065
Alex Lorenz735c47e2015-06-15 20:30:22 +000066 /// Initialize the machine function to the state that's described in the MIR
67 /// file.
68 ///
69 /// Return true if error occurred.
70 bool initializeMachineFunction(MachineFunction &MF);
71
Alex Lorenz09b832c2015-05-29 17:05:41 +000072private:
73 /// Return a MIR diagnostic converted from an LLVM assembly diagnostic.
74 SMDiagnostic diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
75 SMRange SourceRange);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000076};
77
Alex Lorenz735c47e2015-06-15 20:30:22 +000078} // end namespace llvm
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000079
80MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
81 StringRef Filename, LLVMContext &Context)
82 : SM(), Filename(Filename), Context(Context) {
83 SM.AddNewSourceBuffer(std::move(Contents), SMLoc());
84}
85
Alex Lorenz735c47e2015-06-15 20:30:22 +000086bool MIRParserImpl::error(const Twine &Message) {
87 Context.diagnose(DiagnosticInfoMIRParser(
88 DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
89 return true;
Alex Lorenz78d78312015-05-28 22:41:12 +000090}
Alex Lorenz2bdb4e12015-05-27 18:02:19 +000091
Alex Lorenz735c47e2015-06-15 20:30:22 +000092void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
93 DiagnosticSeverity Kind;
94 switch (Diag.getKind()) {
95 case SourceMgr::DK_Error:
96 Kind = DS_Error;
97 break;
98 case SourceMgr::DK_Warning:
99 Kind = DS_Warning;
100 break;
101 case SourceMgr::DK_Note:
102 Kind = DS_Note;
103 break;
104 }
105 Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
106}
107
108static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
109 reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
110}
111
112std::unique_ptr<Module> MIRParserImpl::parse() {
Alex Lorenz78d78312015-05-28 22:41:12 +0000113 yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(),
Alex Lorenz735c47e2015-06-15 20:30:22 +0000114 /*Ctxt=*/nullptr, handleYAMLDiag, this);
Alex Lorenz78d78312015-05-28 22:41:12 +0000115
116 if (!In.setCurrentDocument()) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000117 if (In.error())
Alex Lorenz78d78312015-05-28 22:41:12 +0000118 return nullptr;
119 // Create an empty module when the MIR file is empty.
120 return llvm::make_unique<Module>(Filename, Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000121 }
122
Alex Lorenz78d78312015-05-28 22:41:12 +0000123 std::unique_ptr<Module> M;
124 // Parse the block scalar manually so that we can return unique pointer
125 // without having to go trough YAML traits.
126 if (const auto *BSN =
127 dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000128 SMDiagnostic Error;
Alex Lorenz78d78312015-05-28 22:41:12 +0000129 M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
130 Context);
Alex Lorenz09b832c2015-05-29 17:05:41 +0000131 if (!M) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000132 reportDiagnostic(diagFromLLVMAssemblyDiag(Error, BSN->getSourceRange()));
Alex Lorenz78d78312015-05-28 22:41:12 +0000133 return M;
Alex Lorenz09b832c2015-05-29 17:05:41 +0000134 }
Alex Lorenz78d78312015-05-28 22:41:12 +0000135 In.nextDocument();
136 if (!In.setCurrentDocument())
137 return M;
138 } else {
139 // Create an new, empty module.
140 M = llvm::make_unique<Module>(Filename, Context);
141 }
142
143 // Parse the machine functions.
144 do {
145 if (parseMachineFunction(In))
146 return nullptr;
147 In.nextDocument();
148 } while (In.setCurrentDocument());
149
150 return M;
151}
152
153bool MIRParserImpl::parseMachineFunction(yaml::Input &In) {
Alex Lorenz735c47e2015-06-15 20:30:22 +0000154 auto MF = llvm::make_unique<yaml::MachineFunction>();
155 yaml::yamlize(In, *MF, false);
Alex Lorenz78d78312015-05-28 22:41:12 +0000156 if (In.error())
157 return true;
Alex Lorenz735c47e2015-06-15 20:30:22 +0000158 auto FunctionName = MF->Name;
Alex Lorenzfe2aa972015-06-15 22:23:23 +0000159 if (Functions.find(FunctionName) != Functions.end())
160 return error(Twine("redefinition of machine function '") + FunctionName +
161 "'");
Alex Lorenz735c47e2015-06-15 20:30:22 +0000162 Functions.insert(std::make_pair(FunctionName, std::move(MF)));
163 return false;
164}
165
166bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
167 auto It = Functions.find(MF.getName());
168 if (It == Functions.end())
169 return error(Twine("no machine function information for function '") +
170 MF.getName() + "' in the MIR file");
171 // TODO: Recreate the machine function.
Alex Lorenz78d78312015-05-28 22:41:12 +0000172 return false;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000173}
174
Alex Lorenz09b832c2015-05-29 17:05:41 +0000175SMDiagnostic MIRParserImpl::diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
176 SMRange SourceRange) {
177 assert(SourceRange.isValid());
178
179 // Translate the location of the error from the location in the llvm IR string
180 // to the corresponding location in the MIR file.
181 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
182 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
183 unsigned Column = Error.getColumnNo();
184 StringRef LineStr = Error.getLineContents();
185 SMLoc Loc = Error.getLoc();
186
187 // Get the full line and adjust the column number by taking the indentation of
188 // LLVM IR into account.
189 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
190 L != E; ++L) {
191 if (L.line_number() == Line) {
192 LineStr = *L;
193 Loc = SMLoc::getFromPointer(LineStr.data());
194 auto Indent = LineStr.find(Error.getLineContents());
195 if (Indent != StringRef::npos)
196 Column += Indent;
197 break;
198 }
199 }
200
201 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
202 Error.getMessage(), LineStr, Error.getRanges(),
203 Error.getFixIts());
204}
205
Alex Lorenz735c47e2015-06-15 20:30:22 +0000206MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
207 : Impl(std::move(Impl)) {}
208
209MIRParser::~MIRParser() {}
210
211std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); }
212
213bool MIRParser::initializeMachineFunction(MachineFunction &MF) {
214 return Impl->initializeMachineFunction(MF);
215}
216
217std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
218 SMDiagnostic &Error,
219 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000220 auto FileOrErr = MemoryBuffer::getFile(Filename);
221 if (std::error_code EC = FileOrErr.getError()) {
222 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
223 "Could not open input file: " + EC.message());
Alex Lorenz735c47e2015-06-15 20:30:22 +0000224 return nullptr;
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000225 }
Alex Lorenz735c47e2015-06-15 20:30:22 +0000226 return createMIRParser(std::move(FileOrErr.get()), Context);
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000227}
228
Alex Lorenz735c47e2015-06-15 20:30:22 +0000229std::unique_ptr<MIRParser>
230llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
231 LLVMContext &Context) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000232 auto Filename = Contents->getBufferIdentifier();
Alex Lorenz735c47e2015-06-15 20:30:22 +0000233 return llvm::make_unique<MIRParser>(
234 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000235}