blob: f4ed43769388abbf57069eb560df9341e6038d57 [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 Carruth9aca9182014-01-07 12:34:26 +000013#include "llvm/AsmParser/Parser.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000014#include "llvm/Bitcode/ReaderWriter.h"
Peter Zotov285eed62013-11-06 09:21:15 +000015#include "llvm/IR/LLVMContext.h"
16#include "llvm/IR/Module.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000017#include "llvm/Support/MemoryBuffer.h"
18#include "llvm/Support/SourceMgr.h"
Eli Benderskyb35a2112013-04-03 15:33:45 +000019#include "llvm/Support/Timer.h"
Peter Zotov285eed62013-11-06 09:21:15 +000020#include "llvm/Support/raw_ostream.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000021#include "llvm/Support/system_error.h"
Chandler Carruthe60e57b2013-03-26 02:25:37 +000022
23using namespace llvm;
24
Eli Benderskyb35a2112013-04-03 15:33:45 +000025namespace llvm {
26 extern bool TimePassesIsEnabled;
27}
28
Craig Topperd3a34f82013-07-16 01:17:10 +000029static const char *const TimeIRParsingGroupName = "LLVM IR Parsing";
30static const char *const TimeIRParsingName = "Parse IR";
Eli Benderskyb35a2112013-04-03 15:33:45 +000031
32
Chandler Carruthe60e57b2013-03-26 02:25:37 +000033Module *llvm::getLazyIRModule(MemoryBuffer *Buffer, SMDiagnostic &Err,
Eli Benderskye60fc2f2013-04-01 19:47:56 +000034 LLVMContext &Context) {
Chandler Carruthe60e57b2013-03-26 02:25:37 +000035 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
36 (const unsigned char *)Buffer->getBufferEnd())) {
37 std::string ErrMsg;
Rafael Espindola5b6c1e82014-01-13 18:31:04 +000038 ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
39 if (error_code EC = ModuleOrErr.getError()) {
Chandler Carruthe60e57b2013-03-26 02:25:37 +000040 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
Rafael Espindola5b6c1e82014-01-13 18:31:04 +000041 EC.message());
Chandler Carruthe60e57b2013-03-26 02:25:37 +000042 // ParseBitcodeFile does not take ownership of the Buffer in the
43 // case of an error.
44 delete Buffer;
Craig Topper2617dcc2014-04-15 06:32:26 +000045 return nullptr;
Chandler Carruthe60e57b2013-03-26 02:25:37 +000046 }
Rafael Espindola5b6c1e82014-01-13 18:31:04 +000047 return ModuleOrErr.get();
Chandler Carruthe60e57b2013-03-26 02:25:37 +000048 }
49
Craig Topper2617dcc2014-04-15 06:32:26 +000050 return ParseAssembly(Buffer, nullptr, Err, Context);
Chandler Carruthe60e57b2013-03-26 02:25:37 +000051}
52
53Module *llvm::getLazyIRFileModule(const std::string &Filename, SMDiagnostic &Err,
Eli Benderskye60fc2f2013-04-01 19:47:56 +000054 LLVMContext &Context) {
Ahmed Charles56440fd2014-03-06 05:51:42 +000055 std::unique_ptr<MemoryBuffer> File;
Rafael Espindola8c811722013-06-25 05:28:34 +000056 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
Chandler Carruthe60e57b2013-03-26 02:25:37 +000057 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
58 "Could not open input file: " + ec.message());
Craig Topper2617dcc2014-04-15 06:32:26 +000059 return nullptr;
Chandler Carruthe60e57b2013-03-26 02:25:37 +000060 }
61
Ahmed Charles96c9d952014-03-05 10:19:29 +000062 return getLazyIRModule(File.release(), Err, Context);
Chandler Carruthe60e57b2013-03-26 02:25:37 +000063}
64
65Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err,
66 LLVMContext &Context) {
Eli Benderskyb35a2112013-04-03 15:33:45 +000067 NamedRegionTimer T(TimeIRParsingName, TimeIRParsingGroupName,
68 TimePassesIsEnabled);
Chandler Carruthe60e57b2013-03-26 02:25:37 +000069 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
70 (const unsigned char *)Buffer->getBufferEnd())) {
Rafael Espindola8f31e212014-01-15 01:08:23 +000071 ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(Buffer, Context);
Craig Topper2617dcc2014-04-15 06:32:26 +000072 Module *M = nullptr;
Rafael Espindola8f31e212014-01-15 01:08:23 +000073 if (error_code EC = ModuleOrErr.getError())
Chandler Carruthe60e57b2013-03-26 02:25:37 +000074 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
Rafael Espindola8f31e212014-01-15 01:08:23 +000075 EC.message());
76 else
77 M = ModuleOrErr.get();
78 // parseBitcodeFile does not take ownership of the Buffer.
Chandler Carruthe60e57b2013-03-26 02:25:37 +000079 delete Buffer;
80 return M;
81 }
82
Craig Topper2617dcc2014-04-15 06:32:26 +000083 return ParseAssembly(Buffer, nullptr, Err, Context);
Chandler Carruthe60e57b2013-03-26 02:25:37 +000084}
85
86Module *llvm::ParseIRFile(const std::string &Filename, SMDiagnostic &Err,
87 LLVMContext &Context) {
Ahmed Charles56440fd2014-03-06 05:51:42 +000088 std::unique_ptr<MemoryBuffer> File;
Rafael Espindola8c811722013-06-25 05:28:34 +000089 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
Chandler Carruthe60e57b2013-03-26 02:25:37 +000090 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
91 "Could not open input file: " + ec.message());
Craig Topper2617dcc2014-04-15 06:32:26 +000092 return nullptr;
Chandler Carruthe60e57b2013-03-26 02:25:37 +000093 }
94
Ahmed Charles96c9d952014-03-05 10:19:29 +000095 return ParseIR(File.release(), Err, Context);
Chandler Carruthe60e57b2013-03-26 02:25:37 +000096}
Peter Zotov285eed62013-11-06 09:21:15 +000097
98//===----------------------------------------------------------------------===//
99// C API.
100//===----------------------------------------------------------------------===//
101
102LLVMBool LLVMParseIRInContext(LLVMContextRef ContextRef,
103 LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
104 char **OutMessage) {
105 SMDiagnostic Diag;
106
107 *OutM = wrap(ParseIR(unwrap(MemBuf), Diag, *unwrap(ContextRef)));
108
109 if(!*OutM) {
110 if (OutMessage) {
111 std::string buf;
112 raw_string_ostream os(buf);
113
Craig Topper2617dcc2014-04-15 06:32:26 +0000114 Diag.print(nullptr, os, false);
Peter Zotov285eed62013-11-06 09:21:15 +0000115 os.flush();
116
117 *OutMessage = strdup(buf.c_str());
118 }
119 return 1;
120 }
121
122 return 0;
123}