OLD | NEW |
(Empty) | |
| 1 //===---- llvm/Bitcode/Reader/BitcodeStream.cpp - Lazy bitcode -*- C++ -*-===// |
| 2 // |
| 3 // The LLVM Compiler Infrastructure |
| 4 // |
| 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. |
| 7 // |
| 8 //===----------------------------------------------------------------------===// |
| 9 // |
| 10 // This file implements BitcodeStreamer, which fetches bytes of bitcode from |
| 11 // a stream source. It provides support for streaming (lazy reading) of |
| 12 // bitcode. An example implementation of streaming from a file or stdin |
| 13 // is included. |
| 14 // |
| 15 //===----------------------------------------------------------------------===// |
| 16 |
| 17 #define DEBUG_TYPE "bitcode-stream" |
| 18 #include "llvm/ADT/Statistic.h" |
| 19 #include "llvm/Support/BitcodeStream.h" |
| 20 #include "llvm/Support/MemoryBuffer.h" |
| 21 #include "llvm/Support/system_error.h" |
| 22 #include <string> |
| 23 #include <cerrno> |
| 24 #include <cstdio> |
| 25 #if !defined(_MSC_VER) && !defined(__MINGW32__) |
| 26 #include <unistd.h> |
| 27 #else |
| 28 #include <io.h> |
| 29 #endif |
| 30 #include <fcntl.h> |
| 31 using namespace llvm; |
| 32 |
| 33 // Interface goals: |
| 34 // * BitstreamBytes doesn't care about complexities like using |
| 35 // threads/async callbacks to actually overlap download+compile |
| 36 // * Don't want to duplicate bitcode in memory |
| 37 // * Don't need to know total bitcode len in advance |
| 38 // Non-goals: |
| 39 // BitstreamBytes already has random access so this interface only does in-order |
| 40 // streaming (no arbitrary seeking, else we'd have to buffer all the bitcode |
| 41 // here in addition to BitstreamBytes). This also means that if we want to |
| 42 // be able to to free bitcode, BitstreamBytes/BitcodeReader will implement it. |
| 43 |
| 44 STATISTIC(NumStreamFetches, "Number of calls to bitcode stream fetch"); |
| 45 |
| 46 namespace llvm { |
| 47 BitcodeStreamer::~BitcodeStreamer() {} |
| 48 } |
| 49 |
| 50 namespace { |
| 51 |
| 52 const static error_code success; |
| 53 |
| 54 // Very simple stream backed by a file. Mostly useful for stdin and debugging; |
| 55 // actual file access is probably still best done with mmap |
| 56 class BitcodeFileStreamer : public BitcodeStreamer { |
| 57 int Fd; |
| 58 public: |
| 59 BitcodeFileStreamer() : Fd(0) {} |
| 60 virtual ~BitcodeFileStreamer() { |
| 61 close(Fd); |
| 62 } |
| 63 virtual size_t GetBytes(unsigned char *buf, size_t len) { |
| 64 NumStreamFetches++; |
| 65 return read(Fd, buf, len); |
| 66 } |
| 67 |
| 68 error_code OpenFile(const std::string &Filename) { |
| 69 int OpenFlags = O_RDONLY; |
| 70 #ifdef O_BINARY |
| 71 OpenFlags |= O_BINARY; // Open input file in binary mode on win32. |
| 72 #endif |
| 73 if (Filename == "-") |
| 74 Fd = 0; |
| 75 else |
| 76 Fd = ::open(Filename.c_str(), OpenFlags); |
| 77 if (Fd == -1) return error_code(errno, posix_category()); |
| 78 return success; |
| 79 } |
| 80 }; |
| 81 |
| 82 } |
| 83 |
| 84 namespace llvm { |
| 85 BitcodeStreamer *getBitcodeFileStreamer(const std::string &Filename, |
| 86 std::string *StrError) { |
| 87 BitcodeFileStreamer *s = new BitcodeFileStreamer(); |
| 88 error_code e = s->OpenFile(Filename); |
| 89 if (e != success) { |
| 90 *StrError = std::string() + "Could not open " + Filename + ": " |
| 91 + e.message() + "\n"; |
| 92 return NULL; |
| 93 } |
| 94 return s; |
| 95 } |
| 96 |
| 97 } |
OLD | NEW |