12b0eed27SGordon Henriksen //===-- BitReader.cpp -----------------------------------------------------===//
22b0eed27SGordon Henriksen //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62b0eed27SGordon Henriksen //
72b0eed27SGordon Henriksen //===----------------------------------------------------------------------===//
82b0eed27SGordon Henriksen
92b0eed27SGordon Henriksen #include "llvm-c/BitReader.h"
10a6b96004SEric Christopher #include "llvm-c/Core.h"
11ad17679aSTeresa Johnson #include "llvm/Bitcode/BitcodeReader.h"
129fb823bbSChandler Carruth #include "llvm/IR/LLVMContext.h"
13dec20e43SFilip Pizlo #include "llvm/IR/Module.h"
142b0eed27SGordon Henriksen #include "llvm/Support/MemoryBuffer.h"
15579f0713SAnton Korobeynikov #include <cstring>
16ed0881b2SChandler Carruth #include <string>
172b0eed27SGordon Henriksen
182b0eed27SGordon Henriksen using namespace llvm;
192b0eed27SGordon Henriksen
2034eb6d87SGordon Henriksen /* Builds a module from the bitcode in the specified memory buffer, returning a
2134eb6d87SGordon Henriksen reference to the module via the OutModule parameter. Returns 0 on success.
2234eb6d87SGordon Henriksen Optionally returns a human-readable error message via OutMessage. */
LLVMParseBitcode(LLVMMemoryBufferRef MemBuf,LLVMModuleRef * OutModule,char ** OutMessage)23d7f9c250SRafael Espindola LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule,
24d7f9c250SRafael Espindola char **OutMessage) {
2503b42e41SMehdi Amini return LLVMParseBitcodeInContext(LLVMGetGlobalContext(), MemBuf, OutModule,
26754946c1SDaniel Dunbar OutMessage);
2731d44e49SOwen Anderson }
2831d44e49SOwen Anderson
LLVMParseBitcode2(LLVMMemoryBufferRef MemBuf,LLVMModuleRef * OutModule)292339ffedSRafael Espindola LLVMBool LLVMParseBitcode2(LLVMMemoryBufferRef MemBuf,
302339ffedSRafael Espindola LLVMModuleRef *OutModule) {
3103b42e41SMehdi Amini return LLVMParseBitcodeInContext2(LLVMGetGlobalContext(), MemBuf, OutModule);
322339ffedSRafael Espindola }
332339ffedSRafael Espindola
LLVMParseBitcodeInContext(LLVMContextRef ContextRef,LLVMMemoryBufferRef MemBuf,LLVMModuleRef * OutModule,char ** OutMessage)3425963c61SChris Lattner LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
3570ede484SErick Tryzelaar LLVMMemoryBufferRef MemBuf,
3625963c61SChris Lattner LLVMModuleRef *OutModule,
3725963c61SChris Lattner char **OutMessage) {
383ee23a9eSRafael Espindola MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
393ee23a9eSRafael Espindola LLVMContext &Ctx = *unwrap(ContextRef);
403ee23a9eSRafael Espindola
41d9445c49SPeter Collingbourne Expected<std::unique_ptr<Module>> ModuleOrErr = parseBitcodeFile(Buf, Ctx);
42d9445c49SPeter Collingbourne if (Error Err = ModuleOrErr.takeError()) {
433ee23a9eSRafael Espindola std::string Message;
44d9445c49SPeter Collingbourne handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
45d9445c49SPeter Collingbourne Message = EIB.message();
46d9445c49SPeter Collingbourne });
479d2bfc48SRafael Espindola if (OutMessage)
48dcfd6ed1SRafael Espindola *OutMessage = strdup(Message.c_str());
492617dcceSCraig Topper *OutModule = wrap((Module *)nullptr);
502b0eed27SGordon Henriksen return 1;
512b0eed27SGordon Henriksen }
522b0eed27SGordon Henriksen
53dcd1dca2SRafael Espindola *OutModule = wrap(ModuleOrErr.get().release());
542b0eed27SGordon Henriksen return 0;
552b0eed27SGordon Henriksen }
562b0eed27SGordon Henriksen
LLVMParseBitcodeInContext2(LLVMContextRef ContextRef,LLVMMemoryBufferRef MemBuf,LLVMModuleRef * OutModule)572339ffedSRafael Espindola LLVMBool LLVMParseBitcodeInContext2(LLVMContextRef ContextRef,
582339ffedSRafael Espindola LLVMMemoryBufferRef MemBuf,
592339ffedSRafael Espindola LLVMModuleRef *OutModule) {
602339ffedSRafael Espindola MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
612339ffedSRafael Espindola LLVMContext &Ctx = *unwrap(ContextRef);
622339ffedSRafael Espindola
63d9445c49SPeter Collingbourne ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
64d9445c49SPeter Collingbourne expectedToErrorOrAndEmitErrors(Ctx, parseBitcodeFile(Buf, Ctx));
652339ffedSRafael Espindola if (ModuleOrErr.getError()) {
662339ffedSRafael Espindola *OutModule = wrap((Module *)nullptr);
672339ffedSRafael Espindola return 1;
682339ffedSRafael Espindola }
692339ffedSRafael Espindola
702339ffedSRafael Espindola *OutModule = wrap(ModuleOrErr.get().release());
712339ffedSRafael Espindola return 0;
722339ffedSRafael Espindola }
732339ffedSRafael Espindola
7434eb6d87SGordon Henriksen /* Reads a module from the specified path, returning via the OutModule parameter
7534eb6d87SGordon Henriksen a module provider which performs lazy deserialization. Returns 0 on success.
7634eb6d87SGordon Henriksen Optionally returns a human-readable error message via OutMessage. */
LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,LLVMMemoryBufferRef MemBuf,LLVMModuleRef * OutM,char ** OutMessage)77ad0e0cb0SErick Tryzelaar LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef,
7870ede484SErick Tryzelaar LLVMMemoryBufferRef MemBuf,
79d7f9c250SRafael Espindola LLVMModuleRef *OutM, char **OutMessage) {
80f382b883SRafael Espindola LLVMContext &Ctx = *unwrap(ContextRef);
81e2c1d77fSRafael Espindola std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
82d9445c49SPeter Collingbourne Expected<std::unique_ptr<Module>> ModuleOrErr =
83e2dcf7c3SPeter Collingbourne getOwningLazyBitcodeModule(std::move(Owner), Ctx);
84c79c9d85SEric Fiselier // Release the buffer if we didn't take ownership of it since we never owned
85c79c9d85SEric Fiselier // it anyway.
86c79c9d85SEric Fiselier (void)Owner.release();
8731d44e49SOwen Anderson
88d9445c49SPeter Collingbourne if (Error Err = ModuleOrErr.takeError()) {
89d9445c49SPeter Collingbourne std::string Message;
90d9445c49SPeter Collingbourne handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
91d9445c49SPeter Collingbourne Message = EIB.message();
92d9445c49SPeter Collingbourne });
9334eb6d87SGordon Henriksen if (OutMessage)
94f382b883SRafael Espindola *OutMessage = strdup(Message.c_str());
95d9445c49SPeter Collingbourne *OutM = wrap((Module *)nullptr);
9634eb6d87SGordon Henriksen return 1;
9734eb6d87SGordon Henriksen }
9834eb6d87SGordon Henriksen
99dcd1dca2SRafael Espindola *OutM = wrap(ModuleOrErr.get().release());
1005b6c1e8eSRafael Espindola
10134eb6d87SGordon Henriksen return 0;
102ad0e0cb0SErick Tryzelaar }
103ad0e0cb0SErick Tryzelaar
LLVMGetBitcodeModuleInContext2(LLVMContextRef ContextRef,LLVMMemoryBufferRef MemBuf,LLVMModuleRef * OutM)1042339ffedSRafael Espindola LLVMBool LLVMGetBitcodeModuleInContext2(LLVMContextRef ContextRef,
1052339ffedSRafael Espindola LLVMMemoryBufferRef MemBuf,
1062339ffedSRafael Espindola LLVMModuleRef *OutM) {
1072339ffedSRafael Espindola LLVMContext &Ctx = *unwrap(ContextRef);
1082339ffedSRafael Espindola std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
1092339ffedSRafael Espindola
110d9445c49SPeter Collingbourne ErrorOr<std::unique_ptr<Module>> ModuleOrErr = expectedToErrorOrAndEmitErrors(
111d9445c49SPeter Collingbourne Ctx, getOwningLazyBitcodeModule(std::move(Owner), Ctx));
1122339ffedSRafael Espindola Owner.release();
1132339ffedSRafael Espindola
1142339ffedSRafael Espindola if (ModuleOrErr.getError()) {
1152339ffedSRafael Espindola *OutM = wrap((Module *)nullptr);
1162339ffedSRafael Espindola return 1;
1172339ffedSRafael Espindola }
1182339ffedSRafael Espindola
1192339ffedSRafael Espindola *OutM = wrap(ModuleOrErr.get().release());
1202339ffedSRafael Espindola return 0;
1212339ffedSRafael Espindola }
1222339ffedSRafael Espindola
LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf,LLVMModuleRef * OutM,char ** OutMessage)123ad0e0cb0SErick Tryzelaar LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM,
124ad0e0cb0SErick Tryzelaar char **OutMessage) {
125ad0e0cb0SErick Tryzelaar return LLVMGetBitcodeModuleInContext(LLVMGetGlobalContext(), MemBuf, OutM,
126ad0e0cb0SErick Tryzelaar OutMessage);
127ad0e0cb0SErick Tryzelaar }
1282339ffedSRafael Espindola
LLVMGetBitcodeModule2(LLVMMemoryBufferRef MemBuf,LLVMModuleRef * OutM)1292339ffedSRafael Espindola LLVMBool LLVMGetBitcodeModule2(LLVMMemoryBufferRef MemBuf,
1302339ffedSRafael Espindola LLVMModuleRef *OutM) {
1312339ffedSRafael Espindola return LLVMGetBitcodeModuleInContext2(LLVMGetGlobalContext(), MemBuf, OutM);
1322339ffedSRafael Espindola }
133