blob: e170a3c85ac87c948eef0832f5752f5bc2b18ada [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;
Rafael Espindola3acea392014-06-12 21:46:39 +000024using std::error_code;
Chandler Carruthe60e57b2013-03-26 02:25:37 +000025
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;
Craig Topper2617dcc2014-04-15 06:32:26 +000046 return nullptr;
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
Craig Topper2617dcc2014-04-15 06:32:26 +000051 return ParseAssembly(Buffer, nullptr, Err, Context);
Chandler Carruthe60e57b2013-03-26 02:25:37 +000052}
53
54Module *llvm::getLazyIRFileModule(const std::string &Filename, SMDiagnostic &Err,
Eli Benderskye60fc2f2013-04-01 19:47:56 +000055 LLVMContext &Context) {
Ahmed Charles56440fd2014-03-06 05:51:42 +000056 std::unique_ptr<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());
Craig Topper2617dcc2014-04-15 06:32:26 +000060 return nullptr;
Chandler Carruthe60e57b2013-03-26 02:25:37 +000061 }
62
Ahmed Charles96c9d952014-03-05 10:19:29 +000063 return getLazyIRModule(File.release(), Err, Context);
Chandler Carruthe60e57b2013-03-26 02:25:37 +000064}
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);
Craig Topper2617dcc2014-04-15 06:32:26 +000073 Module *M = nullptr;
Rafael Espindola8f31e212014-01-15 01:08:23 +000074 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
Craig Topper2617dcc2014-04-15 06:32:26 +000084 return ParseAssembly(Buffer, 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 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());
Craig Topper2617dcc2014-04-15 06:32:26 +000093 return nullptr;
Chandler Carruthe60e57b2013-03-26 02:25:37 +000094 }
95
Ahmed Charles96c9d952014-03-05 10:19:29 +000096 return ParseIR(File.release(), 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
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
Craig Topper2617dcc2014-04-15 06:32:26 +0000115 Diag.print(nullptr, os, false);
Peter Zotov285eed62013-11-06 09:21:15 +0000116 os.flush();
117
118 *OutMessage = strdup(buf.c_str());
119 }
120 return 1;
121 }
122
123 return 0;
124}