2009-01-02 08:01:27 +01:00
|
|
|
//===- Parser.cpp - Main dispatch module for the Parser library -----------===//
|
2005-04-21 23:10:11 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 21:36:04 +01:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:10:11 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
2014-01-07 13:34:26 +01:00
|
|
|
// This library implements the functionality defined in llvm/AsmParser/Parser.h
|
2001-06-06 22:29:01 +02:00
|
|
|
//
|
2009-01-02 08:01:27 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
2001-06-06 22:29:01 +02:00
|
|
|
|
2014-01-07 13:34:26 +01:00
|
|
|
#include "llvm/AsmParser/Parser.h"
|
2009-01-02 08:01:27 +01:00
|
|
|
#include "LLParser.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Module.h"
|
2007-11-18 09:46:26 +01:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2009-01-02 08:01:27 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2008-02-20 12:08:44 +01:00
|
|
|
#include <cstring>
|
2014-06-12 19:38:55 +02:00
|
|
|
#include <system_error>
|
2004-07-13 10:42:12 +02:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2014-08-17 23:36:47 +02:00
|
|
|
Module *llvm::ParseAssembly(std::unique_ptr<MemoryBuffer> F, Module *M,
|
|
|
|
SMDiagnostic &Err, LLVMContext &Context) {
|
2009-09-02 19:18:19 +02:00
|
|
|
SourceMgr SM;
|
2014-08-17 23:36:47 +02:00
|
|
|
MemoryBuffer *Buf = F.get();
|
|
|
|
SM.AddNewSourceBuffer(F.release(), SMLoc());
|
2009-09-02 19:18:19 +02:00
|
|
|
|
|
|
|
// If we are parsing into an existing module, do it.
|
|
|
|
if (M)
|
2014-08-19 00:28:28 +02:00
|
|
|
return LLParser(Buf->getBuffer(), SM, Err, M).Run() ? nullptr : M;
|
2009-09-02 19:18:19 +02:00
|
|
|
|
|
|
|
// Otherwise create a new module.
|
2014-08-17 23:36:47 +02:00
|
|
|
std::unique_ptr<Module> M2(new Module(Buf->getBufferIdentifier(), Context));
|
2014-08-19 00:28:28 +02:00
|
|
|
if (LLParser(Buf->getBuffer(), SM, Err, M2.get()).Run())
|
2014-04-15 08:32:26 +02:00
|
|
|
return nullptr;
|
2014-03-05 11:19:29 +01:00
|
|
|
return M2.release();
|
2009-09-02 19:18:19 +02:00
|
|
|
}
|
|
|
|
|
2009-07-03 00:46:18 +02:00
|
|
|
Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err,
|
|
|
|
LLVMContext &Context) {
|
2014-07-06 19:43:13 +02:00
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(Filename);
|
|
|
|
if (std::error_code EC = FileOrErr.getError()) {
|
2011-10-16 07:43:57 +02:00
|
|
|
Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
|
2014-07-06 19:43:13 +02:00
|
|
|
"Could not open input file: " + EC.message());
|
2014-04-15 08:32:26 +02:00
|
|
|
return nullptr;
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
2009-01-02 23:46:48 +01:00
|
|
|
|
2014-08-17 23:36:47 +02:00
|
|
|
return ParseAssembly(std::move(FileOrErr.get()), nullptr, Err, Context);
|
2001-06-06 22:29:01 +02:00
|
|
|
}
|
|
|
|
|
2009-01-02 23:46:48 +01:00
|
|
|
Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
|
2009-07-03 00:46:18 +02:00
|
|
|
SMDiagnostic &Err, LLVMContext &Context) {
|
2009-07-03 01:08:13 +02:00
|
|
|
MemoryBuffer *F =
|
2014-06-27 06:33:58 +02:00
|
|
|
MemoryBuffer::getMemBuffer(StringRef(AsmString), "<string>");
|
2009-07-03 01:08:13 +02:00
|
|
|
|
2014-08-17 23:36:47 +02:00
|
|
|
return ParseAssembly(std::unique_ptr<MemoryBuffer>(F), M, Err, Context);
|
2005-05-20 05:25:47 +02:00
|
|
|
}
|