LLVM  15.0.0git
BitReader.cpp
Go to the documentation of this file.
1 //===-- BitReader.cpp -----------------------------------------------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm-c/BitReader.h"
10 #include "llvm-c/Core.h"
12 #include "llvm/IR/LLVMContext.h"
13 #include "llvm/IR/Module.h"
15 #include <cstring>
16 #include <string>
17 
18 using namespace llvm;
19 
20 /* Builds a module from the bitcode in the specified memory buffer, returning a
21  reference to the module via the OutModule parameter. Returns 0 on success.
22  Optionally returns a human-readable error message via OutMessage. */
24  char **OutMessage) {
25  return LLVMParseBitcodeInContext(LLVMGetGlobalContext(), MemBuf, OutModule,
26  OutMessage);
27 }
28 
30  LLVMModuleRef *OutModule) {
31  return LLVMParseBitcodeInContext2(LLVMGetGlobalContext(), MemBuf, OutModule);
32 }
33 
35  LLVMMemoryBufferRef MemBuf,
36  LLVMModuleRef *OutModule,
37  char **OutMessage) {
38  MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
39  LLVMContext &Ctx = *unwrap(ContextRef);
40 
41  Expected<std::unique_ptr<Module>> ModuleOrErr = parseBitcodeFile(Buf, Ctx);
42  if (Error Err = ModuleOrErr.takeError()) {
43  std::string Message;
44  handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
45  Message = EIB.message();
46  });
47  if (OutMessage)
48  *OutMessage = strdup(Message.c_str());
49  *OutModule = wrap((Module *)nullptr);
50  return 1;
51  }
52 
53  *OutModule = wrap(ModuleOrErr.get().release());
54  return 0;
55 }
56 
58  LLVMMemoryBufferRef MemBuf,
59  LLVMModuleRef *OutModule) {
60  MemoryBufferRef Buf = unwrap(MemBuf)->getMemBufferRef();
61  LLVMContext &Ctx = *unwrap(ContextRef);
62 
63  ErrorOr<std::unique_ptr<Module>> ModuleOrErr =
65  if (ModuleOrErr.getError()) {
66  *OutModule = wrap((Module *)nullptr);
67  return 1;
68  }
69 
70  *OutModule = wrap(ModuleOrErr.get().release());
71  return 0;
72 }
73 
74 /* Reads a module from the specified path, returning via the OutModule parameter
75  a module provider which performs lazy deserialization. Returns 0 on success.
76  Optionally returns a human-readable error message via OutMessage. */
78  LLVMMemoryBufferRef MemBuf,
79  LLVMModuleRef *OutM, char **OutMessage) {
80  LLVMContext &Ctx = *unwrap(ContextRef);
81  std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
84  // Release the buffer if we didn't take ownership of it since we never owned
85  // it anyway.
86  (void)Owner.release();
87 
88  if (Error Err = ModuleOrErr.takeError()) {
89  std::string Message;
90  handleAllErrors(std::move(Err), [&](ErrorInfoBase &EIB) {
91  Message = EIB.message();
92  });
93  if (OutMessage)
94  *OutMessage = strdup(Message.c_str());
95  *OutM = wrap((Module *)nullptr);
96  return 1;
97  }
98 
99  *OutM = wrap(ModuleOrErr.get().release());
100 
101  return 0;
102 }
103 
105  LLVMMemoryBufferRef MemBuf,
106  LLVMModuleRef *OutM) {
107  LLVMContext &Ctx = *unwrap(ContextRef);
108  std::unique_ptr<MemoryBuffer> Owner(unwrap(MemBuf));
109 
111  Ctx, getOwningLazyBitcodeModule(std::move(Owner), Ctx));
112  Owner.release();
113 
114  if (ModuleOrErr.getError()) {
115  *OutM = wrap((Module *)nullptr);
116  return 1;
117  }
118 
119  *OutM = wrap(ModuleOrErr.get().release());
120  return 0;
121 }
122 
124  char **OutMessage) {
126  OutMessage);
127 }
128 
130  LLVMModuleRef *OutM) {
132 }
llvm::getOwningLazyBitcodeModule
Expected< std::unique_ptr< Module > > getOwningLazyBitcodeModule(std::unique_ptr< MemoryBuffer > &&Buffer, LLVMContext &Context, bool ShouldLazyLoadMetadata=false, bool IsImporting=false)
Like getLazyBitcodeModule, except that the module takes ownership of the memory buffer if successful.
Definition: BitcodeReader.cpp:7503
MemoryBuffer.h
llvm::expectedToErrorOrAndEmitErrors
ErrorOr< T > expectedToErrorOrAndEmitErrors(LLVMContext &Ctx, Expected< T > Val)
Definition: BitcodeReader.h:45
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
LLVMContextRef
struct LLVMOpaqueContext * LLVMContextRef
The top-level container for all LLVM global data.
Definition: Types.h:53
LLVMGetGlobalContext
LLVMContextRef LLVMGetGlobalContext(void)
Obtain the global context instance.
Definition: Core.cpp:83
LLVMParseBitcodeInContext2
LLVMBool LLVMParseBitcodeInContext2(LLVMContextRef ContextRef, LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule)
Definition: BitReader.cpp:57
Module.h
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
LLVMParseBitcode
LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule, char **OutMessage)
Definition: BitReader.cpp:23
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::ErrorOr::getError
std::error_code getError() const
Definition: ErrorOr.h:153
llvm::unwrap
Attribute unwrap(LLVMAttributeRef Attr)
Definition: Attributes.h:264
LLVMParseBitcodeInContext
LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef, LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule, char **OutMessage)
Definition: BitReader.cpp:34
LLVMGetBitcodeModuleInContext2
LLVMBool LLVMGetBitcodeModuleInContext2(LLVMContextRef ContextRef, LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM)
Reads a module from the specified path, returning via the OutMP parameter a module provider which per...
Definition: BitReader.cpp:104
llvm::ErrorInfoBase
Base class for error info classes.
Definition: Error.h:46
llvm::ErrorInfoBase::message
virtual std::string message() const
Return the error message as a string.
Definition: Error.h:54
LLVMGetBitcodeModule
LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM, char **OutMessage)
Definition: BitReader.cpp:123
llvm::parseBitcodeFile
Expected< std::unique_ptr< Module > > parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context, DataLayoutCallbackTy DataLayoutCallback=[](StringRef) { return None;})
Read the specified bitcode file, returning the module.
Definition: BitcodeReader.cpp:7522
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::wrap
LLVMAttributeRef wrap(Attribute Attr)
Definition: Attributes.h:259
LLVMParseBitcode2
LLVMBool LLVMParseBitcode2(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutModule)
Definition: BitReader.cpp:29
llvm::Expected::get
reference get()
Returns a reference to the stored T value.
Definition: Error.h:567
LLVMGetBitcodeModuleInContext
LLVMBool LLVMGetBitcodeModuleInContext(LLVMContextRef ContextRef, LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM, char **OutMessage)
Reads a module from the specified path, returning via the OutMP parameter a module provider which per...
Definition: BitReader.cpp:77
LLVMGetBitcodeModule2
LLVMBool LLVMGetBitcodeModule2(LLVMMemoryBufferRef MemBuf, LLVMModuleRef *OutM)
Definition: BitReader.cpp:129
LLVMBool
int LLVMBool
Definition: Types.h:28
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
Core.h
llvm::Expected::takeError
Error takeError()
Take ownership of the stored error.
Definition: Error.h:597
llvm::ErrorOr::get
reference get()
Definition: ErrorOr.h:150
llvm::ErrorOr
Represents either an error or a value T.
Definition: ErrorOr.h:56
BitReader.h
LLVMMemoryBufferRef
struct LLVMOpaqueMemoryBuffer * LLVMMemoryBufferRef
LLVM uses a polymorphic type hierarchy which C cannot represent, therefore parameters must be passed ...
Definition: Types.h:48
LLVMContext.h
BitcodeReader.h
llvm::handleAllErrors
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition: Error.h:966
LLVMModuleRef
struct LLVMOpaqueModule * LLVMModuleRef
The top-level container for all other LLVM Intermediate Representation (IR) objects.
Definition: Types.h:61