blob: 9d90324adf9b68b979a000d876cd1f325024218d [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"
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000021#include <system_error>
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);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000039 if (std::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());
Alp Toker5ebb7b32014-06-27 04:33:58 +000042 // getLazyBitcodeModule does not take ownership of the Buffer in the
Chandler Carruthe60e57b2013-03-26 02:25:37 +000043 // 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 Espindoladb4ed0b2014-06-13 02:24:39 +000056 if (std::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
Alp Toker5ebb7b32014-06-27 04:33:58 +000065Module *llvm::ParseIR(const MemoryBuffer *Buffer, SMDiagnostic &Err,
Chandler Carruthe60e57b2013-03-26 02:25:37 +000066 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())) {
Alp Tokerf6ae8442014-06-27 04:48:32 +000071 ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(Buffer, Context);
Craig Topper2617dcc2014-04-15 06:32:26 +000072 Module *M = nullptr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000073 if (std::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 return M;
80 }
81
Alp Toker5ebb7b32014-06-27 04:33:58 +000082 return ParseAssembly(MemoryBuffer::getMemBuffer(
83 Buffer->getBuffer(), Buffer->getBufferIdentifier()),
84 nullptr, Err, Context);
Chandler Carruthe60e57b2013-03-26 02:25:37 +000085}
86
87Module *llvm::ParseIRFile(const std::string &Filename, SMDiagnostic &Err,
88 LLVMContext &Context) {
Ahmed Charles56440fd2014-03-06 05:51:42 +000089 std::unique_ptr<MemoryBuffer> File;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000090 if (std::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());
Craig Topper2617dcc2014-04-15 06:32:26 +000093 return nullptr;
Chandler Carruthe60e57b2013-03-26 02:25:37 +000094 }
95
Alp Toker5ebb7b32014-06-27 04:33:58 +000096 return ParseIR(File.get(), Err, Context);
Chandler Carruthe60e57b2013-03-26 02:25:37 +000097}
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
Alp Toker5ebb7b32014-06-27 04:33:58 +0000108 std::unique_ptr<MemoryBuffer> MB(unwrap(MemBuf));
109 *OutM = wrap(ParseIR(MB.get(), Diag, *unwrap(ContextRef)));
Peter Zotov285eed62013-11-06 09:21:15 +0000110
111 if(!*OutM) {
112 if (OutMessage) {
Alp Tokere69170a2014-06-26 22:52:05 +0000113 std::string buf;
114 raw_string_ostream os(buf);
115
Craig Topper2617dcc2014-04-15 06:32:26 +0000116 Diag.print(nullptr, os, false);
Alp Tokere69170a2014-06-26 22:52:05 +0000117 os.flush();
118
119 *OutMessage = strdup(buf.c_str());
Peter Zotov285eed62013-11-06 09:21:15 +0000120 }
121 return 1;
122 }
123
124 return 0;
125}