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/MIR/MIRParser.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/AsmParser/Parser.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Support/SMLoc.h"
21 #include "llvm/Support/SourceMgr.h"
22 #include "llvm/Support/MemoryBuffer.h"
23 #include "llvm/Support/YAMLTraits.h"
30 /// This class implements the parsing of LLVM IR that's embedded inside a MIR
38 MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents, StringRef Filename,
39 LLVMContext &Context);
41 /// Try to parse the optional LLVM module in the MIR file.
43 /// Return null if an error occurred while parsing the LLVM module.
44 std::unique_ptr<Module> parseLLVMModule(SMDiagnostic &Error);
47 } // end anonymous namespace
49 MIRParserImpl::MIRParserImpl(std::unique_ptr<MemoryBuffer> Contents,
50 StringRef Filename, LLVMContext &Context)
51 : SM(), Filename(Filename), Context(Context) {
52 SM.AddNewSourceBuffer(std::move(Contents), SMLoc());
55 std::unique_ptr<Module> MIRParserImpl::parseLLVMModule(SMDiagnostic &Error) {
56 yaml::Input In(SM.getMemoryBuffer(SM.getMainFileID())->getBuffer());
58 // Parse the block scalar manually so that we can return unique pointer
59 // without having to go trough YAML traits.
60 if (In.setCurrentDocument()) {
62 dyn_cast_or_null<yaml::BlockScalarNode>(In.getCurrentNode())) {
63 return parseAssembly(MemoryBufferRef(BSN->getValue(), Filename), Error,
68 // Create an new, empty module.
69 return llvm::make_unique<Module>(Filename, Context);
72 std::unique_ptr<Module> llvm::parseMIRFile(StringRef Filename,
74 LLVMContext &Context) {
75 auto FileOrErr = MemoryBuffer::getFile(Filename);
76 if (std::error_code EC = FileOrErr.getError()) {
77 Error = SMDiagnostic(Filename, SourceMgr::DK_Error,
78 "Could not open input file: " + EC.message());
79 return std::unique_ptr<Module>();
81 return parseMIR(std::move(FileOrErr.get()), Error, Context);
84 std::unique_ptr<Module> llvm::parseMIR(std::unique_ptr<MemoryBuffer> Contents,
86 LLVMContext &Context) {
87 auto Filename = Contents->getBufferIdentifier();
88 MIRParserImpl Parser(std::move(Contents), Filename, Context);
89 return Parser.parseLLVMModule(Error);