X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;ds=sidebyside;f=lib%2FAsmParser%2FParser.cpp;h=4e55e62ecf5c49c76cb62a82995e35a9d2fde404;hb=7ecd92d75cda45668b6b5fdbcdd2142826514e66;hp=e8a7bdbbb9896010066edbd28052b88f6e446758;hpb=1913848e337c982f7997fc5fe2a722e514315308;p=oota-llvm.git diff --git a/lib/AsmParser/Parser.cpp b/lib/AsmParser/Parser.cpp index e8a7bdbbb98..4e55e62ecf5 100644 --- a/lib/AsmParser/Parser.cpp +++ b/lib/AsmParser/Parser.cpp @@ -1,84 +1,80 @@ -//===- Parser.cpp - Main dispatch module for the Parser library -------------=== -// +//===- Parser.cpp - Main dispatch module for the Parser library -----------===// +// // The LLVM Compiler Infrastructure // -// This file was developed by the LLVM research group and is distributed under -// the University of Illinois Open Source License. See LICENSE.TXT for details. -// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// //===----------------------------------------------------------------------===// // -// This library implements the functionality defined in llvm/assembly/parser.h +// This library implements the functionality defined in llvm/AsmParser/Parser.h // -//===------------------------------------------------------------------------=== +//===----------------------------------------------------------------------===// -#include "ParserInternals.h" -#include "llvm/Module.h" -#include "llvm/Analysis/Verifier.h" +#include "llvm/AsmParser/Parser.h" +#include "LLParser.h" +#include "llvm/ADT/STLExtras.h" +#include "llvm/IR/Module.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/raw_ostream.h" +#include +#include +using namespace llvm; -// The useful interface defined by this file... Parse an ASCII file, and return -// the internal representation in a nice slice'n'dice'able representation. -// -Module *ParseAssemblyFile(const std::string &Filename) { - FILE *F = stdin; +bool llvm::parseAssemblyInto(MemoryBufferRef F, Module &M, SMDiagnostic &Err, + SlotMapping *Slots) { + SourceMgr SM; + std::unique_ptr Buf = MemoryBuffer::getMemBuffer(F); + SM.AddNewSourceBuffer(std::move(Buf), SMLoc()); - if (Filename != "-") { - F = fopen(Filename.c_str(), "r"); - - if (F == 0) - throw ParseException(Filename, "Could not open file '" + Filename + "'"); - } + return LLParser(F.getBuffer(), SM, Err, &M, Slots).Run(); +} - Module *Result; - try { - Result = RunVMAsmParser(Filename, F); - } catch (...) { - if (F != stdin) fclose(F); // Make sure to close file descriptor if an - throw; // exception is thrown - } +std::unique_ptr llvm::parseAssembly(MemoryBufferRef F, + SMDiagnostic &Err, + LLVMContext &Context, + SlotMapping *Slots) { + std::unique_ptr M = + make_unique(F.getBufferIdentifier(), Context); - if (F != stdin) - fclose(F); + if (parseAssemblyInto(F, *M, Err, Slots)) + return nullptr; - return Result; + return M; } +std::unique_ptr llvm::parseAssemblyFile(StringRef Filename, + SMDiagnostic &Err, + LLVMContext &Context, + SlotMapping *Slots) { + ErrorOr> FileOrErr = + MemoryBuffer::getFileOrSTDIN(Filename); + if (std::error_code EC = FileOrErr.getError()) { + Err = SMDiagnostic(Filename, SourceMgr::DK_Error, + "Could not open input file: " + EC.message()); + return nullptr; + } -//===------------------------------------------------------------------------=== -// ParseException Class -//===------------------------------------------------------------------------=== - - -ParseException::ParseException(const std::string &filename, - const std::string &message, - int lineNo, int colNo) - : Filename(filename), Message(message) { - LineNo = lineNo; ColumnNo = colNo; + return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context, Slots); } -ParseException::ParseException(const ParseException &E) - : Filename(E.Filename), Message(E.Message) { - LineNo = E.LineNo; - ColumnNo = E.ColumnNo; +std::unique_ptr llvm::parseAssemblyString(StringRef AsmString, + SMDiagnostic &Err, + LLVMContext &Context, + SlotMapping *Slots) { + MemoryBufferRef F(AsmString, ""); + return parseAssembly(F, Err, Context, Slots); } -// Includes info from options -const std::string ParseException::getMessage() const { - std::string Result; - char Buffer[10]; - - if (Filename == "-") - Result += ""; - else - Result += Filename; - - if (LineNo != -1) { - sprintf(Buffer, "%d", LineNo); - Result += std::string(":") + Buffer; - if (ColumnNo != -1) { - sprintf(Buffer, "%d", ColumnNo); - Result += std::string(",") + Buffer; - } - } - - return Result + ": " + Message; +Constant *llvm::parseConstantValue(StringRef Asm, SMDiagnostic &Err, + const Module &M, const SlotMapping *Slots) { + SourceMgr SM; + std::unique_ptr Buf = MemoryBuffer::getMemBuffer(Asm); + SM.AddNewSourceBuffer(std::move(Buf), SMLoc()); + Constant *C; + if (LLParser(Asm, SM, Err, const_cast(&M)) + .parseStandaloneConstantValue(C, Slots)) + return nullptr; + return C; }