1 //===- ReaderWrappers.cpp - Parse bitcode from file or buffer -------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements loading and parsing a bitcode file and parsing a
11 // module from a memory buffer.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Bitcode/ReaderWriter.h"
16 #include "BitcodeReader.h"
17 #include "llvm/System/MappedFile.h"
20 //===----------------------------------------------------------------------===//
21 // BitcodeFileReader - Read from an mmap'able file descriptor.
24 /// BitcodeFileReader - parses bitcode from a file.
26 class BitcodeFileReader : public BitcodeReader {
31 BitcodeFileReader(const BitcodeFileReader&); // DO NOT IMPLEMENT
32 void operator=(const BitcodeFileReader&); // DO NOT IMPLEMENT
34 BitcodeFileReader(const std::string &FN) : Filename(FN) {}
35 bool Read(std::string *ErrMsg);
38 BitcodeReader::FreeState();
44 bool BitcodeFileReader::Read(std::string *ErrMsg) {
45 if (File.open(sys::Path(Filename), sys::MappedFile::READ_ACCESS, ErrMsg))
47 if (!File.map(ErrMsg)) {
51 unsigned char *Buffer = reinterpret_cast<unsigned char*>(File.base());
52 if (!ParseBitcode(Buffer, File.size(), Filename))
54 assert(getErrorString() && "Didn't set an error string?");
55 if (ErrMsg) *ErrMsg = getErrorString();
61 //===----------------------------------------------------------------------===//
63 //===----------------------------------------------------------------------===//
65 /// getBitcodeModuleProvider - lazy function-at-a-time loading from a file.
67 ModuleProvider *llvm::getBitcodeModuleProvider(const std::string &Filename,
68 std::string *ErrMsg) {
69 if (Filename != std::string("-")) {
70 BitcodeFileReader *R = new BitcodeFileReader(Filename);
71 if (R->Read(ErrMsg)) {
78 assert(0 && "FIXME: stdin reading unimp!");
81 BytecodeStdinReader *R = new BytecodeStdinReader();
82 if (R->Read(ErrMsg)) {
90 /// ParseBitcodeFile - Read the specified bitcode file, returning the module.
91 /// If an error occurs, return null and fill in *ErrMsg if non-null.
92 Module *llvm::ParseBitcodeFile(const std::string &Filename,std::string *ErrMsg){
93 ModuleProvider *MP = getBitcodeModuleProvider(Filename, ErrMsg);
95 Module *M = MP->releaseModule(ErrMsg);