[LLVMdev] Instantiating modules from .bc files

Danny llvm at pixelperplexity.net
Wed Dec 26 18:01:31 PST 2007


That worked quite well. Thank you. One question as a follow up: is  
there a nice/standard way of including the pre-made bitcode chunks in  
with the binaries that are being created which read them?

Bascially, I'd like to have the same functionality, but rather than  
having one or more .bc files running around which need to be read at  
runtime by an executable, moving that into the code itself so it's  
all contained in one.

Thanks,
danny

On Dec 26, 2007, at 3:33 PM, Gordon Henriksen wrote:

> Hi Danny,
>
> On 2007-12-26, at 15:39, Danny wrote:
>
>> I've noticed that the BitcodeReader appears to be an internal  
>> module, but the BitstreamReader is public. Should I be using the  
>> BitstreamReader? If so how.
>
> The generic BitstreamReader class is public because it's used in  
> other projects, including clang, to serialize data structures other  
> than LLVM IR. The coding of the LLVM IR within the bitstream are  
> private to the reader and writer modules. The functions exposed in  
> ReaderWriter.h are the public interface to that functionality.
>
>> I tried to get a module out of a file that I'd read in to it using  
>> this code:
>>
>> MemoryBuffer* memBuf = MemoryBuffer::getFile(filename, sizeof
>> filename, &errStr);
>> printf("errStr: %s\n", errStr.c_str());
>> BitcodeReader::BitcodeReader bcReader(memBuf);
>> Module* Mod = bcReader.materializeModule(&errInfo);
>> printf("errInfo: %s\n", errInfo.c_str());
>> 	
>> verifyModule(*Mod, PrintMessageAction);
>>
>> The errStr and the errInfo strings were empty, but I got a crash  
>> on the verifyModule call.
>
>
> Try this:
>
> #include "llvm/Bitcode/ReaderWriter.h"
>
> ...
>
> std::string &Message;
> llvm::Module* Mod = llvm::ParseBitcodeFile(MemBuf, &Message);
> if (!Mod) {
>   // Message contains an error message.
>   std::cerr << "error reading bitcode: " << Message << "\n";
>   exit(1);  // Don't segfault!
> }
>
> // Okay, Mod is valid.
> ...
>
> delete Mod;
>
> Also note that the primary error indicator is not the string, but  
> the return pointer, so you should always check that (not the error  
> string) and stop if it's null in order to avoid segfaulting.
>
> — Gordon
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20071226/c1a33f3e/attachment.html>


More information about the llvm-dev mailing list