blob: d66c13d39c09f656efc511510ccdb6d2d43dce73 [file] [log] [blame]
Chris Lattnerdf986172009-01-02 07:01:27 +00001//===- Parser.cpp - Main dispatch module for the Parser library -----------===//
Misha Brukman019b6392005-04-21 21:10:11 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman019b6392005-04-21 21:10:11 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Dan Gohman5d136652009-01-08 22:17:05 +000010// This library implements the functionality defined in llvm/Assembly/Parser.h
Chris Lattner00950542001-06-06 20:29:01 +000011//
Chris Lattnerdf986172009-01-02 07:01:27 +000012//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +000013
Chris Lattnerdf986172009-01-02 07:01:27 +000014#include "llvm/Assembly/Parser.h"
15#include "LLParser.h"
Misha Brukman19138482003-10-23 18:00:34 +000016#include "llvm/Module.h"
Chris Lattnerad7d1e22009-01-04 20:44:11 +000017#include "llvm/ADT/OwningPtr.h"
Chris Lattner92bcb422009-07-02 22:46:18 +000018#include "llvm/Support/SourceMgr.h"
Chris Lattner8e3a8e02007-11-18 08:46:26 +000019#include "llvm/Support/MemoryBuffer.h"
Chris Lattnerdf986172009-01-02 07:01:27 +000020#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovae9f3a32008-02-20 11:08:44 +000021#include <cstring>
Chris Lattner65cd4b02004-07-13 08:42:12 +000022using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000023
Chris Lattner92bcb422009-07-02 22:46:18 +000024Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err,
25 LLVMContext &Context) {
Chris Lattner8e3a8e02007-11-18 08:46:26 +000026 std::string ErrorStr;
Chris Lattnereeb4a842009-07-02 23:08:13 +000027 MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
Chris Lattner8e3a8e02007-11-18 08:46:26 +000028 if (F == 0) {
Chris Lattner92bcb422009-07-02 22:46:18 +000029 Err = SMDiagnostic("", -1, -1,
30 "Could not open input file '" + Filename + "'", "");
Chris Lattner8e3a8e02007-11-18 08:46:26 +000031 return 0;
Chris Lattner00950542001-06-06 20:29:01 +000032 }
Misha Brukman9ea40342009-01-02 22:46:48 +000033
Chris Lattnereeb4a842009-07-02 23:08:13 +000034 SourceMgr SM;
35 SM.AddNewSourceBuffer(F, SMLoc());
36
Owen Anderson8b477ed2009-07-01 16:58:40 +000037 OwningPtr<Module> M(new Module(Filename, Context));
Chris Lattnereeb4a842009-07-02 23:08:13 +000038 if (LLParser(F, SM, Err, M.get()).Run())
Chris Lattnerad7d1e22009-01-04 20:44:11 +000039 return 0;
40 return M.take();
Chris Lattner00950542001-06-06 20:29:01 +000041}
42
Misha Brukman9ea40342009-01-02 22:46:48 +000043Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
Chris Lattner92bcb422009-07-02 22:46:18 +000044 SMDiagnostic &Err, LLVMContext &Context) {
Chris Lattnereeb4a842009-07-02 23:08:13 +000045 MemoryBuffer *F =
46 MemoryBuffer::getMemBuffer(AsmString, AsmString+strlen(AsmString),
47 "<string>");
Chris Lattnerad7d1e22009-01-04 20:44:11 +000048
Chris Lattnereeb4a842009-07-02 23:08:13 +000049 SourceMgr SM;
50 SM.AddNewSourceBuffer(F, SMLoc());
51
Chris Lattnerad7d1e22009-01-04 20:44:11 +000052 // If we are parsing into an existing module, do it.
53 if (M)
Chris Lattnereeb4a842009-07-02 23:08:13 +000054 return LLParser(F, SM, Err, M).Run() ? 0 : M;
Chris Lattnerad7d1e22009-01-04 20:44:11 +000055
56 // Otherwise create a new module.
Owen Anderson8b477ed2009-07-01 16:58:40 +000057 OwningPtr<Module> M2(new Module("<string>", Context));
Chris Lattnereeb4a842009-07-02 23:08:13 +000058 if (LLParser(F, SM, Err, M2.get()).Run())
Chris Lattnerad7d1e22009-01-04 20:44:11 +000059 return 0;
60 return M2.take();
Chris Lattner6184feb2005-05-20 03:25:47 +000061}