blob: 5ac893aef14e4686c2e79887fe74a80ac84527cd [file] [log] [blame]
Gordon Henriksen2b0eed22007-12-11 00:20:48 +00001//===-- BitReader.cpp -----------------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Gordon Henriksen2b0eed22007-12-11 00:20:48 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm-c/BitReader.h"
Eric Christophera6b96002015-12-18 01:46:52 +000010#include "llvm-c/Core.h"
Teresa Johnsonad176792016-11-11 05:34:58 +000011#include "llvm/Bitcode/BitcodeReader.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000012#include "llvm/IR/LLVMContext.h"
Filip Pizlodec20e42013-05-01 20:59:00 +000013#include "llvm/IR/Module.h"
Gordon Henriksen2b0eed22007-12-11 00:20:48 +000014#include "llvm/Support/MemoryBuffer.h"
Rafael Espindola3ee23a92015-02-03 00:49:57 +000015#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov579f0712008-02-20 11:08:44 +000016#include <cstring>
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include <string>
Gordon Henriksen2b0eed22007-12-11 00:20:48 +000018
19using namespace llvm;
20
Gordon Henriksen34eb6d82007-12-19 22:30:40 +000021/* Builds a module from the bitcode in the specified memory buffer, returning a
22 reference to the module via the OutModule parameter. Returns 0 on success.
Chris Lattner25963c62010-01-09 22:27:07 +000023 Optionally returns a human-readable error message via OutMessage. */
Rafael Espindolad7f9c252015-12-18 14:06:34 +000024LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule,
25 char **OutMessage) {
Mehdi Amini03b42e42016-04-14 21:59:01 +000026 return LLVMParseBitcodeInContext(LLVMGetGlobalContext(), MemBuf, OutModule,
Daniel Dunbar754946c2010-02-15 21:08:22 +000027 OutMessage);
Owen Anderson31d44e42009-07-02 07:17:57 +000028}
29
Rafael Espindola2339ffe2015-12-18 23:46:42 +000030LLVMBool LLVMParseBitcode2(LLVMMemoryBufferRef MemBuf,
31 LLVMModuleRef *OutModule) {
Mehdi Amini03b42e42016-04-14 21:59:01 +000032 return LLVMParseBitcodeInContext2(LLVMGetGlobalContext(), MemBuf, OutModule);
Rafael Espindola2339ffe2015-12-18 23:46:42 +000033}
34
Chris Lattner25963c62010-01-09 22:27:07 +000035LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
36 LLVMMemoryBufferRef MemBuf,
37 LLVMModuleRef *OutModule,
38 char **OutMessage) {
Rafael Espindola3ee23a92015-02-03 00:49:57 +000039 MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
40 LLVMContext &Ctx = *unwrap(ContextRef);
41
Peter Collingbourned9445c42016-11-13 07:00:17 +000042 Expected<std::unique_ptr<Module>> ModuleOrErr = parseBitcodeFile(Buf, Ctx);
43 if (Error Err = ModuleOrErr.takeError()) {
44 std::string Message;
45 handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
46 Message = EIB.message();
47 });
Rafael Espindola9d2bfc42015-12-14 23:17:03 +000048 if (OutMessage)
Rafael Espindoladcfd6ed2015-02-03 01:53:03 +000049 *OutMessage = strdup(Message.c_str());
Rafael Espindolad7f9c252015-12-18 14:06:34 +000050 *OutModule = wrap((Module *)nullptr);
Gordon Henriksen2b0eed22007-12-11 00:20:48 +000051 return 1;
52 }
Joe Abbey2ad8df22012-11-25 15:23:39 +000053
Rafael Espindoladcd1dca2015-06-16 22:27:55 +000054 *OutModule = wrap(ModuleOrErr.get().release());
Gordon Henriksen2b0eed22007-12-11 00:20:48 +000055 return 0;
56}
57
Rafael Espindola2339ffe2015-12-18 23:46:42 +000058LLVMBool LLVMParseBitcodeInContext2(LLVMContextRef ContextRef,
59 LLVMMemoryBufferRef MemBuf,
60 LLVMModuleRef *OutModule) {
61 MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
62 LLVMContext &Ctx = *unwrap(ContextRef);
63
Peter Collingbourned9445c42016-11-13 07:00:17 +000064 ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
65 expectedToErrorOrAndEmitErrors(Ctx, parseBitcodeFile(Buf, Ctx));
Rafael Espindola2339ffe2015-12-18 23:46:42 +000066 if (ModuleOrErr.getError()) {
67 *OutModule = wrap((Module *)nullptr);
68 return 1;
69 }
70
71 *OutModule = wrap(ModuleOrErr.get().release());
72 return 0;
73}
74
Gordon Henriksen34eb6d82007-12-19 22:30:40 +000075/* Reads a module from the specified path, returning via the OutModule parameter
76 a module provider which performs lazy deserialization. Returns 0 on success.
Joe Abbey2ad8df22012-11-25 15:23:39 +000077 Optionally returns a human-readable error message via OutMessage. */
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +000078LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
79 LLVMMemoryBufferRef MemBuf,
Rafael Espindolad7f9c252015-12-18 14:06:34 +000080 LLVMModuleRef *OutM, char **OutMessage) {
Rafael Espindolaf382b882015-12-18 13:58:05 +000081 LLVMContext &Ctx = *unwrap(ContextRef);
Rafael Espindolae2c1d772014-08-26 22:00:09 +000082 std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
Peter Collingbourned9445c42016-11-13 07:00:17 +000083 Expected<std::unique_ptr<Module>> ModuleOrErr =
Peter Collingbournee2dcf7c2016-11-08 06:03:43 +000084 getOwningLazyBitcodeModule(std::move(Owner), Ctx);
Eric Fiselierc79c9d82016-11-14 07:26:17 +000085 // Release the buffer if we didn't take ownership of it since we never owned
86 // it anyway.
87 (void)Owner.release();
Joe Abbey2ad8df22012-11-25 15:23:39 +000088
Peter Collingbourned9445c42016-11-13 07:00:17 +000089 if (Error Err = ModuleOrErr.takeError()) {
90 std::string Message;
91 handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
92 Message = EIB.message();
93 });
Gordon Henriksen34eb6d82007-12-19 22:30:40 +000094 if (OutMessage)
Rafael Espindolaf382b882015-12-18 13:58:05 +000095 *OutMessage = strdup(Message.c_str());
Peter Collingbourned9445c42016-11-13 07:00:17 +000096 *OutM = wrap((Module *)nullptr);
Gordon Henriksen34eb6d82007-12-19 22:30:40 +000097 return 1;
98 }
Joe Abbey2ad8df22012-11-25 15:23:39 +000099
Rafael Espindoladcd1dca2015-06-16 22:27:55 +0000100 *OutM = wrap(ModuleOrErr.get().release());
Rafael Espindola5b6c1e82014-01-13 18:31:04 +0000101
Gordon Henriksen34eb6d82007-12-19 22:30:40 +0000102 return 0;
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +0000103}
104
Rafael Espindola2339ffe2015-12-18 23:46:42 +0000105LLVMBool LLVMGetBitcodeModuleInContext2(LLVMContextRef ContextRef,
106 LLVMMemoryBufferRef MemBuf,
107 LLVMModuleRef *OutM) {
108 LLVMContext &Ctx = *unwrap(ContextRef);
109 std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
110
Peter Collingbourned9445c42016-11-13 07:00:17 +0000111 ErrorOr<std::unique_ptr<Module>> ModuleOrErr = expectedToErrorOrAndEmitErrors(
112 Ctx, getOwningLazyBitcodeModule(std::move(Owner), Ctx));
Rafael Espindola2339ffe2015-12-18 23:46:42 +0000113 Owner.release();
114
115 if (ModuleOrErr.getError()) {
116 *OutM = wrap((Module *)nullptr);
117 return 1;
118 }
119
120 *OutM = wrap(ModuleOrErr.get().release());
121 return 0;
122}
123
Erick Tryzelaarad0e0cb2010-03-02 23:58:54 +0000124LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
125 char **OutMessage) {
126 return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM,
127 OutMessage);
128}
Rafael Espindola2339ffe2015-12-18 23:46:42 +0000129
130LLVMBool LLVMGetBitcodeModule2(LLVMMemoryBufferRef MemBuf,
131 LLVMModuleRef *OutM) {
132 return LLVMGetBitcodeModuleInContext2(LLVMGetGlobalContext(), MemBuf, OutM);
133}