blob: f64785b3ad923b04fc4de5a11cec52af4b72dccc [file] [log] [blame]
Gordon Henriksen2b0eed22007-12-11 00:20:48 +00001//===-- BitReader.cpp -----------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Gordon Henriksen2b0eed22007-12-11 00:20:48 +00007//
8//===----------------------------------------------------------------------===//
9
10#include "llvm-c/BitReader.h"
Eric Christophera6b96002015-12-18 01:46:52 +000011#include "llvm-c/Core.h"
Teresa Johnsonad176792016-11-11 05:34:58 +000012#include "llvm/Bitcode/BitcodeReader.h"
Rafael Espindola3ee23a92015-02-03 00:49:57 +000013#include "llvm/IR/DiagnosticPrinter.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000014#include "llvm/IR/LLVMContext.h"
Filip Pizlodec20e42013-05-01 20:59:00 +000015#include "llvm/IR/Module.h"
Gordon Henriksen2b0eed22007-12-11 00:20:48 +000016#include "llvm/Support/MemoryBuffer.h"
Rafael Espindola3ee23a92015-02-03 00:49:57 +000017#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000018#include <cstring>
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include <string>
Gordon Henriksen2b0eed22007-12-11 00:20:48 +000020
21using namespace llvm;
22
Gordon Henriksen34eb6d82007-12-19 22:30:40 +000023/* Builds a module from the bitcode in the specified memory buffer, returning a
24 reference to the module via the OutModule parameter. Returns 0 on success.
Chris Lattner25963c62010-01-09 22:27:07 +000025 Optionally returns a human-readable error message via OutMessage. */
Rafael Espindolad7f9c252015-12-18 14:06:34 +000026LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule,
27 char **OutMessage) {
Mehdi Amini03b42e42016-04-14 21:59:01 +000028 return LLVMParseBitcodeInContext(LLVMGetGlobalContext(), MemBuf, OutModule,
Daniel Dunbar754946c2010-02-15 21:08:22 +000029 OutMessage);
Owen Anderson31d44e42009-07-02 07:17:57 +000030}
31
Rafael Espindola2339ffe2015-12-18 23:46:42 +000032LLVMBool LLVMParseBitcode2(LLVMMemoryBufferRef MemBuf,
33 LLVMModuleRef *OutModule) {
Mehdi Amini03b42e42016-04-14 21:59:01 +000034 return LLVMParseBitcodeInContext2(LLVMGetGlobalContext(), MemBuf, OutModule);
Rafael Espindola2339ffe2015-12-18 23:46:42 +000035}
36
Chris Lattner25963c62010-01-09 22:27:07 +000037LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
38 LLVMMemoryBufferRef MemBuf,
39 LLVMModuleRef *OutModule,
40 char **OutMessage) {
Rafael Espindola3ee23a92015-02-03 00:49:57 +000041 MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
42 LLVMContext &Ctx = *unwrap(ContextRef);
43
Peter Collingbourned9445c42016-11-13 07:00:17 +000044 Expected<std::unique_ptr<Module>> ModuleOrErr = parseBitcodeFile(Buf, Ctx);
45 if (Error Err = ModuleOrErr.takeError()) {
46 std::string Message;
47 handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
48 Message = EIB.message();
49 });
Rafael Espindola9d2bfc42015-12-14 23:17:03 +000050 if (OutMessage)
Rafael Espindoladcfd6ed2015-02-03 01:53:03 +000051 *OutMessage = strdup(Message.c_str());
Rafael Espindolad7f9c252015-12-18 14:06:34 +000052 *OutModule = wrap((Module *)nullptr);
Gordon Henriksen2b0eed22007-12-11 00:20:48 +000053 return 1;
54 }
Joe Abbey2ad8df22012-11-25 15:23:39 +000055
Rafael Espindoladcd1dca2015-06-16 22:27:55 +000056 *OutModule = wrap(ModuleOrErr.get().release());
Gordon Henriksen2b0eed22007-12-11 00:20:48 +000057 return 0;
58}
59
Rafael Espindola2339ffe2015-12-18 23:46:42 +000060LLVMBool LLVMParseBitcodeInContext2(LLVMContextRef ContextRef,
61 LLVMMemoryBufferRef MemBuf,
62 LLVMModuleRef *OutModule) {
63 MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
64 LLVMContext &Ctx = *unwrap(ContextRef);
65
Peter Collingbourned9445c42016-11-13 07:00:17 +000066 ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
67 expectedToErrorOrAndEmitErrors(Ctx, parseBitcodeFile(Buf, Ctx));
Rafael Espindola2339ffe2015-12-18 23:46:42 +000068 if (ModuleOrErr.getError()) {
69 *OutModule = wrap((Module *)nullptr);
70 return 1;
71 }
72
73 *OutModule = wrap(ModuleOrErr.get().release());
74 return 0;
75}
76
Gordon Henriksen34eb6d82007-12-19 22:30:40 +000077/* Reads a module from the specified path, returning via the OutModule parameter
78 a module provider which performs lazy deserialization. Returns 0 on success.
Joe Abbey2ad8df22012-11-25 15:23:39 +000079 Optionally returns a human-readable error message via OutMessage. */
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +000080LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
81 LLVMMemoryBufferRef MemBuf,
Rafael Espindolad7f9c252015-12-18 14:06:34 +000082 LLVMModuleRef *OutM, char **OutMessage) {
Rafael Espindolaf382b882015-12-18 13:58:05 +000083 LLVMContext &Ctx = *unwrap(ContextRef);
Rafael Espindolae2c1d772014-08-26 22:00:09 +000084 std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
Peter Collingbourned9445c42016-11-13 07:00:17 +000085 Expected<std::unique_ptr<Module>> ModuleOrErr =
Peter Collingbournee2dcf7c2016-11-08 06:03:43 +000086 getOwningLazyBitcodeModule(std::move(Owner), Ctx);
Eric Fiselierc79c9d82016-11-14 07:26:17 +000087 // Release the buffer if we didn't take ownership of it since we never owned
88 // it anyway.
89 (void)Owner.release();
Joe Abbey2ad8df22012-11-25 15:23:39 +000090
Peter Collingbourned9445c42016-11-13 07:00:17 +000091 if (Error Err = ModuleOrErr.takeError()) {
92 std::string Message;
93 handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
94 Message = EIB.message();
95 });
Gordon Henriksen34eb6d82007-12-19 22:30:40 +000096 if (OutMessage)
Rafael Espindolaf382b882015-12-18 13:58:05 +000097 *OutMessage = strdup(Message.c_str());
Peter Collingbourned9445c42016-11-13 07:00:17 +000098 *OutM = wrap((Module *)nullptr);
Gordon Henriksen34eb6d82007-12-19 22:30:40 +000099 return 1;
100 }
Joe Abbey2ad8df22012-11-25 15:23:39 +0000101
Rafael Espindoladcd1dca2015-06-16 22:27:55 +0000102 *OutM = wrap(ModuleOrErr.get().release());
Rafael Espindola5b6c1e82014-01-13 18:31:04 +0000103
Gordon Henriksen34eb6d82007-12-19 22:30:40 +0000104 return 0;
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +0000105}
106
Rafael Espindola2339ffe2015-12-18 23:46:42 +0000107LLVMBool LLVMGetBitcodeModuleInContext2(LLVMContextRef ContextRef,
108 LLVMMemoryBufferRef MemBuf,
109 LLVMModuleRef *OutM) {
110 LLVMContext &Ctx = *unwrap(ContextRef);
111 std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
112
Peter Collingbourned9445c42016-11-13 07:00:17 +0000113 ErrorOr<std::unique_ptr<Module>> ModuleOrErr = expectedToErrorOrAndEmitErrors(
114 Ctx, getOwningLazyBitcodeModule(std::move(Owner), Ctx));
Rafael Espindola2339ffe2015-12-18 23:46:42 +0000115 Owner.release();
116
117 if (ModuleOrErr.getError()) {
118 *OutM = wrap((Module *)nullptr);
119 return 1;
120 }
121
122 *OutM = wrap(ModuleOrErr.get().release());
123 return 0;
124}
125
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +0000126LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
127 char **OutMessage) {
128 return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM,
129 OutMessage);
130}
Rafael Espindola2339ffe2015-12-18 23:46:42 +0000131
132LLVMBool LLVMGetBitcodeModule2(LLVMMemoryBufferRef MemBuf,
133 LLVMModuleRef *OutM) {
134 return LLVMGetBitcodeModuleInContext2(LLVMGetGlobalContext(), MemBuf, OutM);
135}