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