blob: 00c6c0dbf437d9f3de27d5c67984d85cb3e03493 [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
Rafael Espindola8286fbf2014-07-05 00:39:08 +000032static Module *getLazyIRModule(MemoryBuffer *Buffer, SMDiagnostic &Err,
33 LLVMContext &Context) {
Chandler Carruthe60e57b2013-03-26 02:25:37 +000034 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
35 (const unsigned char *)Buffer->getBufferEnd())) {
36 std::string ErrMsg;
Rafael Espindola5b6c1e82014-01-13 18:31:04 +000037 ErrorOr<Module *> ModuleOrErr = getLazyBitcodeModule(Buffer, Context);
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000038 if (std::error_code EC = ModuleOrErr.getError()) {
Chandler Carruthe60e57b2013-03-26 02:25:37 +000039 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
Rafael Espindola5b6c1e82014-01-13 18:31:04 +000040 EC.message());
Alp Toker5ebb7b32014-06-27 04:33:58 +000041 // getLazyBitcodeModule does not take ownership of the Buffer in the
Chandler Carruthe60e57b2013-03-26 02:25:37 +000042 // case of an error.
43 delete Buffer;
Craig Topper2617dcc2014-04-15 06:32:26 +000044 return nullptr;
Chandler Carruthe60e57b2013-03-26 02:25:37 +000045 }
Rafael Espindola5b6c1e82014-01-13 18:31:04 +000046 return ModuleOrErr.get();
Chandler Carruthe60e57b2013-03-26 02:25:37 +000047 }
48
Craig Topper2617dcc2014-04-15 06:32:26 +000049 return ParseAssembly(Buffer, nullptr, Err, Context);
Chandler Carruthe60e57b2013-03-26 02:25:37 +000050}
51
52Module *llvm::getLazyIRFileModule(const std::string &Filename, SMDiagnostic &Err,
Eli Benderskye60fc2f2013-04-01 19:47:56 +000053 LLVMContext &Context) {
Ahmed Charles56440fd2014-03-06 05:51:42 +000054 std::unique_ptr<MemoryBuffer> File;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000055 if (std::error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
Chandler Carruthe60e57b2013-03-26 02:25:37 +000056 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
57 "Could not open input file: " + ec.message());
Craig Topper2617dcc2014-04-15 06:32:26 +000058 return nullptr;
Chandler Carruthe60e57b2013-03-26 02:25:37 +000059 }
60
Ahmed Charles96c9d952014-03-05 10:19:29 +000061 return getLazyIRModule(File.release(), Err, Context);
Chandler Carruthe60e57b2013-03-26 02:25:37 +000062}
63
Alp Tokerde4c0092014-06-27 09:19:14 +000064Module *llvm::ParseIR(MemoryBuffer *Buffer, SMDiagnostic &Err,
Chandler Carruthe60e57b2013-03-26 02:25:37 +000065 LLVMContext &Context) {
Eli Benderskyb35a2112013-04-03 15:33:45 +000066 NamedRegionTimer T(TimeIRParsingName, TimeIRParsingGroupName,
67 TimePassesIsEnabled);
Chandler Carruthe60e57b2013-03-26 02:25:37 +000068 if (isBitcode((const unsigned char *)Buffer->getBufferStart(),
69 (const unsigned char *)Buffer->getBufferEnd())) {
Alp Tokerf6ae8442014-06-27 04:48:32 +000070 ErrorOr<Module *> ModuleOrErr = parseBitcodeFile(Buffer, Context);
Craig Topper2617dcc2014-04-15 06:32:26 +000071 Module *M = nullptr;
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000072 if (std::error_code EC = ModuleOrErr.getError())
Chandler Carruthe60e57b2013-03-26 02:25:37 +000073 Err = SMDiagnostic(Buffer->getBufferIdentifier(), SourceMgr::DK_Error,
Rafael Espindola8f31e212014-01-15 01:08:23 +000074 EC.message());
75 else
76 M = ModuleOrErr.get();
77 // parseBitcodeFile does not take ownership of the Buffer.
Chandler Carruthe60e57b2013-03-26 02:25:37 +000078 return M;
79 }
80
Alp Toker5ebb7b32014-06-27 04:33:58 +000081 return ParseAssembly(MemoryBuffer::getMemBuffer(
82 Buffer->getBuffer(), Buffer->getBufferIdentifier()),
83 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 Espindoladb4ed0b2014-06-13 02:24:39 +000089 if (std::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
Alp Toker5ebb7b32014-06-27 04:33:58 +000095 return ParseIR(File.get(), 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
Alp Toker5ebb7b32014-06-27 04:33:58 +0000107 std::unique_ptr<MemoryBuffer> MB(unwrap(MemBuf));
108 *OutM = wrap(ParseIR(MB.get(), Diag, *unwrap(ContextRef)));
Peter Zotov285eed62013-11-06 09:21:15 +0000109
110 if(!*OutM) {
111 if (OutMessage) {
Alp Tokere69170a2014-06-26 22:52:05 +0000112 std::string buf;
113 raw_string_ostream os(buf);
114
Craig Topper2617dcc2014-04-15 06:32:26 +0000115 Diag.print(nullptr, os, false);
Alp Tokere69170a2014-06-26 22:52:05 +0000116 os.flush();
117
118 *OutMessage = strdup(buf.c_str());
Peter Zotov285eed62013-11-06 09:21:15 +0000119 }
120 return 1;
121 }
122
123 return 0;
124}