1 //===- MIRParser.cpp - MIR serialization format parser implementation -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the class that parses the optional LLVM IR and machine
11 // functions that are stored in MIR files.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/CodeGen/MIRParser/MIRParser.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/AsmParser/Parser.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MIRYamlMapping.h"
22 #include "llvm/IR/DiagnosticInfo.h"
23 #include "llvm/IR/LLVMContext.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/Support/LineIterator.h"
26 #include "llvm/Support/SMLoc.h"
27 #include "llvm/Support/SourceMgr.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/YAMLTraits.h"
36 /// This class implements the parsing of LLVM IR that's embedded inside a MIR
42 StringMap<std::unique_ptr<yaml::MachineFunction>> Functions;
45 MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename,
46 LLVMContext &Context);
48 void reportDiagnostic(const SMDiagnostic &Diag);
50 /// Report an error with the given message at unknown location.
52 /// Always returns true.
53 bool error(const Twine &Message);
55 /// Try to parse the optional LLVM module and the machine functions in the MIR
58 /// Return null if an error occurred.
59 std::unique_ptr<Module> parse();
61 /// Parse the machine function in the current YAML document.
63 /// Return true if an error occurred.
64 bool parseMachineFunction(yaml::Input &In);
66 /// Initialize the machine function to the state that's described in the MIR
69 /// Return true if error occurred.
70 bool initializeMachineFunction(MachineFunction &MF);
73 /// Return a MIR diagnostic converted from an LLVM assembly diagnostic.
74 SMDiagnostic diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
78 } // end namespace llvm
80 MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
81 StringRef Filename, LLVMContext &Context)
82 : SM(), Filename(Filename), Context(Context) {
83 SM.AddNewSourceBuffer(std::move(Contents), SMLoc());
86 bool MIRParserImpl::error(const Twine &Message) {
87 Context.diagnose(DiagnosticInfoMIRParser(
88 DS_Error, SMDiagnostic(Filename, SourceMgr::DK_Error, Message.str())));
92 void MIRParserImpl::reportDiagnostic(const SMDiagnostic &Diag) {
93 DiagnosticSeverity Kind;
94 switch (Diag.getKind()) {
95 case SourceMgr::DK_Error:
98 case SourceMgr::DK_Warning:
101 case SourceMgr::DK_Note:
105 Context.diagnose(DiagnosticInfoMIRParser(Kind, Diag));
108 static void handleYAMLDiag(const SMDiagnostic &Diag, void *Context) {
109 reinterpret_cast<MIRParserImpl *>(Context)->reportDiagnostic(Diag);
112 std::unique_ptr<Module> MIRParserImpl::parse() {
113 yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer(),
114 /*Ctxt=*/nullptr, handleYAMLDiag, this);
116 if (!In.setCurrentDocument()) {
119 // Create an empty module when the MIR file is empty.
120 return llvm::make_unique<Module>(Filename, Context);
123 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())) {
129 M = parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
132 reportDiagnostic(diagFromLLVMAssemblyDiag(Error, BSN->getSourceRange()));
136 if (!In.setCurrentDocument())
139 // Create an new, empty module.
140 M = llvm::make_unique<Module>(Filename, Context);
143 // Parse the machine functions.
145 if (parseMachineFunction(In))
148 } while (In.setCurrentDocument());
153 bool MIRParserImpl::parseMachineFunction(yaml::Input &In) {
154 auto MF = llvm::make_unique<yaml::MachineFunction>();
155 yaml::yamlize(In, *MF, false);
158 auto FunctionName = MF->Name;
159 Functions.insert(std::make_pair(FunctionName, std::move(MF)));
163 bool MIRParserImpl::initializeMachineFunction(MachineFunction &MF) {
164 auto It = Functions.find(MF.getName());
165 if (It == Functions.end())
166 return error(Twine("no machine function information for function '") +
167 MF.getName() + "' in the MIR file");
168 // TODO: Recreate the machine function.
172 SMDiagnostic MIRParserImpl::diagFromLLVMAssemblyDiag(const SMDiagnostic &Error,
173 SMRange SourceRange) {
174 assert(SourceRange.isValid());
176 // Translate the location of the error from the location in the llvm IR string
177 // to the corresponding location in the MIR file.
178 auto LineAndColumn = SM.getLineAndColumn(SourceRange.Start);
179 unsigned Line = LineAndColumn.first + Error.getLineNo() - 1;
180 unsigned Column = Error.getColumnNo();
181 StringRef LineStr = Error.getLineContents();
182 SMLoc Loc = Error.getLoc();
184 // Get the full line and adjust the column number by taking the indentation of
185 // LLVM IR into account.
186 for (line_iterator L(*SM.getMemoryBuffer(SM.getMainFileID()), false), E;
188 if (L.line_number() == Line) {
190 Loc = SMLoc::getFromPointer(LineStr.data());
191 auto Indent = LineStr.find(Error.getLineContents());
192 if (Indent != StringRef::npos)
198 return SMDiagnostic(SM, Loc, Filename, Line, Column, Error.getKind(),
199 Error.getMessage(), LineStr, Error.getRanges(),
203 MIRParser::MIRParser(std::unique_ptr<MIRParserImpl> Impl)
204 : Impl(std::move(Impl)) {}
206 MIRParser::~MIRParser() {}
208 std::unique_ptr<Module> MIRParser::parseLLVMModule() { return Impl->parse(); }
210 bool MIRParser::initializeMachineFunction(MachineFunction &MF) {
211 return Impl->initializeMachineFunction(MF);
214 std::unique_ptr<MIRParser> llvm::createMIRParserFromFile(StringRef Filename,
216 LLVMContext &Context) {
217 auto FileOrErr = MemoryBuffer::getFile(Filename);
218 if (std::error_code EC = FileOrErr.getError()) {
219 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
220 "Could not open input file: " + EC.message());
223 return createMIRParser(std::move(FileOrErr.get()), Context);
226 std::unique_ptr<MIRParser>
227 llvm::createMIRParser(std::unique_ptr<MemoryBuffer> Contents,
228 LLVMContext &Context) {
229 auto Filename = Contents->getBufferIdentifier();
230 return llvm::make_unique<MIRParser>(
231 llvm::make_unique<MIRParserImpl>(std::move(Contents), Filename, Context));