blob: 70801c43f1a3ac475bb866d70048dad0c3dfb42e [file] [log] [blame]
Chandler Carruthe60e57b2013-03-26 02:25:37 +00001//===---- IRReader.cpp - Reader for LLVM IR files -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/IRReader/IRReader.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000011#include "llvm-c/Core.h"
12#include "llvm-c/IRReader.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000013#include "llvm/ADT/OwningPtr.h"
Chandler Carruth9aca9182014-01-07 12:34:26 +000014#include "llvm/AsmParser/Parser.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000015#include "llvm/Bitcode/ReaderWriter.h"
Peter Zotov285eed62013-11-06 09:21:15 +000016#include "llvm/IR/LLVMContext.h"
17#include "llvm/IR/Module.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000018#include "llvm/Support/MemoryBuffer.h"
19#include "llvm/Support/SourceMgr.h"
Eli Benderskyb35a2112013-04-03 15:33:45 +000020#include "llvm/Support/Timer.h"
Peter Zotov285eed62013-11-06 09:21:15 +000021#include "llvm/Support/raw_ostream.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000022#include "llvm/Support/system_error.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000023
24using namespace llvm;
25
Eli Benderskyb35a2112013-04-03 15:33:45 +000026namespace llvm {
27 extern bool TimePassesIsEnabled;
28}
29
Craig Topperd3a34f82013-07-16 01:17:10 +000030static const char *const TimeIRParsingGroupName = "LLVM IR Parsing";
31static const char *const TimeIRParsingName = "Parse IR";
Eli Benderskyb35a2112013-04-03 15:33:45 +000032
33
Chandler Carruthe60e57b2013-03-26 02:25:37 +000034Module *llvm::getLazyIRModule(MemoryBuffer *Buffer, SMDiagnostic &Err,
Eli Benderskye60fc2f2013-04-01 19:47:56 +000035 LLVMContext &Context) {
Chandler Carruthe60e57b2013-03-26 02:25:37 +000036 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
37 (const unsigned char *)Buffer->getBufferEnd())) {
38 std::string ErrMsg;
Rafael Espindola5b6c1e82014-01-13 18:31:04 +000039 ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
40 if (error_code EC = ModuleOrErr.getError()) {
Chandler Carruthe60e57b2013-03-26 02:25:37 +000041 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
Rafael Espindola5b6c1e82014-01-13 18:31:04 +000042 EC.message());
Chandler Carruthe60e57b2013-03-26 02:25:37 +000043 // ParseBitcodeFile does not take ownership of the Buffer in the
44 // case of an error.
45 delete Buffer;
Rafael Espindola5b6c1e82014-01-13 18:31:04 +000046 return NULL;
Chandler Carruthe60e57b2013-03-26 02:25:37 +000047 }
Rafael Espindola5b6c1e82014-01-13 18:31:04 +000048 return ModuleOrErr.get();
Chandler Carruthe60e57b2013-03-26 02:25:37 +000049 }
50
51 return ParseAssembly(Buffer, 0, Err, Context);
52}
53
54Module *llvm::getLazyIRFileModule(const std::string &Filename, SMDiagnostic &Err,
Eli Benderskye60fc2f2013-04-01 19:47:56 +000055 LLVMContext &Context) {
Chandler Carruthe60e57b2013-03-26 02:25:37 +000056 OwningPtr<MemoryBuffer> File;
Rafael Espindola8c811722013-06-25 05:28:34 +000057 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
Chandler Carruthe60e57b2013-03-26 02:25:37 +000058 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
59 "Could not open input file: " + ec.message());
60 return 0;
61 }
62
63 return getLazyIRModule(File.take(), Err, Context);
64}
65
66Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err,
67 LLVMContext &Context) {
Eli Benderskyb35a2112013-04-03 15:33:45 +000068 NamedRegionTimer T(TimeIRParsingName, TimeIRParsingGroupName,
69 TimePassesIsEnabled);
Chandler Carruthe60e57b2013-03-26 02:25:37 +000070 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
71 (const unsigned char *)Buffer->getBufferEnd())) {
Rafael Espindola8f31e212014-01-15 01:08:23 +000072 ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(Buffer, Context);
73 Module *M = 0;
74 if (error_code EC = ModuleOrErr.getError())
Chandler Carruthe60e57b2013-03-26 02:25:37 +000075 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
Rafael Espindola8f31e212014-01-15 01:08:23 +000076 EC.message());
77 else
78 M = ModuleOrErr.get();
79 // parseBitcodeFile does not take ownership of the Buffer.
Chandler Carruthe60e57b2013-03-26 02:25:37 +000080 delete Buffer;
81 return M;
82 }
83
84 return ParseAssembly(Buffer, 0, Err, Context);
85}
86
87Module *llvm::ParseIRFile(const std::string &Filename, SMDiagnostic &Err,
88 LLVMContext &Context) {
89 OwningPtr<MemoryBuffer> File;
Rafael Espindola8c811722013-06-25 05:28:34 +000090 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
Chandler Carruthe60e57b2013-03-26 02:25:37 +000091 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
92 "Could not open input file: " + ec.message());
93 return 0;
94 }
95
96 return ParseIR(File.take(), Err, Context);
97}
Peter Zotov285eed62013-11-06 09:21:15 +000098
99//===----------------------------------------------------------------------===//
100// C API.
101//===----------------------------------------------------------------------===//
102
103LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef,
104 LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
105 char **OutMessage) {
106 SMDiagnostic Diag;
107
108 *OutM = wrap(ParseIR(unwrap(MemBuf), Diag, *unwrap(ContextRef)));
109
110 if(!*OutM) {
111 if (OutMessage) {
112 std::string buf;
113 raw_string_ostream os(buf);
114
115 Diag.print(NULL, os, false);
116 os.flush();
117
118 *OutMessage = strdup(buf.c_str());
119 }
120 return 1;
121 }
122
123 return 0;
124}