| Index: lib/Support/BitcodeStream.cpp
|
| diff --git a/lib/Support/BitcodeStream.cpp b/lib/Support/BitcodeStream.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..96168f6dcf1ad7602e918d251a710c04a9c5209b
|
| --- /dev/null
|
| +++ b/lib/Support/BitcodeStream.cpp
|
| @@ -0,0 +1,97 @@
|
| +//===---- llvm/Bitcode/Reader/BitcodeStream.cpp - Lazy bitcode -*- C++ -*-===//
|
| +//
|
| +// The LLVM Compiler Infrastructure
|
| +//
|
| +// This file is distributed under the University of Illinois Open Source
|
| +// License. See LICENSE.TXT for details.
|
| +//
|
| +//===----------------------------------------------------------------------===//
|
| +//
|
| +// This file implements BitcodeStreamer, which fetches bytes of bitcode from
|
| +// a stream source. It provides support for streaming (lazy reading) of
|
| +// bitcode. An example implementation of streaming from a file or stdin
|
| +// is included.
|
| +//
|
| +//===----------------------------------------------------------------------===//
|
| +
|
| +#define DEBUG_TYPE "bitcode-stream"
|
| +#include "llvm/ADT/Statistic.h"
|
| +#include "llvm/Support/BitcodeStream.h"
|
| +#include "llvm/Support/MemoryBuffer.h"
|
| +#include "llvm/Support/system_error.h"
|
| +#include <string>
|
| +#include <cerrno>
|
| +#include <cstdio>
|
| +#if !defined(_MSC_VER) && !defined(__MINGW32__)
|
| +#include <unistd.h>
|
| +#else
|
| +#include <io.h>
|
| +#endif
|
| +#include <fcntl.h>
|
| +using namespace llvm;
|
| +
|
| +// Interface goals:
|
| +// * BitstreamBytes doesn't care about complexities like using
|
| +// threads/async callbacks to actually overlap download+compile
|
| +// * Don't want to duplicate bitcode in memory
|
| +// * Don't need to know total bitcode len in advance
|
| +// Non-goals:
|
| +// BitstreamBytes already has random access so this interface only does in-order
|
| +// streaming (no arbitrary seeking, else we'd have to buffer all the bitcode
|
| +// here in addition to BitstreamBytes). This also means that if we want to
|
| +// be able to to free bitcode, BitstreamBytes/BitcodeReader will implement it.
|
| +
|
| +STATISTIC(NumStreamFetches, "Number of calls to bitcode stream fetch");
|
| +
|
| +namespace llvm {
|
| +BitcodeStreamer::~BitcodeStreamer() {}
|
| +}
|
| +
|
| +namespace {
|
| +
|
| +const static error_code success;
|
| +
|
| +// Very simple stream backed by a file. Mostly useful for stdin and debugging;
|
| +// actual file access is probably still best done with mmap
|
| +class BitcodeFileStreamer : public BitcodeStreamer {
|
| + int Fd;
|
| +public:
|
| + BitcodeFileStreamer() : Fd(0) {}
|
| + virtual ~BitcodeFileStreamer() {
|
| + close(Fd);
|
| + }
|
| + virtual size_t GetBytes(unsigned char *buf, size_t len) {
|
| + NumStreamFetches++;
|
| + return read(Fd, buf, len);
|
| + }
|
| +
|
| + error_code OpenFile(const std::string &Filename) {
|
| + int OpenFlags = O_RDONLY;
|
| +#ifdef O_BINARY
|
| + OpenFlags |= O_BINARY; // Open input file in binary mode on win32.
|
| +#endif
|
| + if (Filename == "-")
|
| + Fd = 0;
|
| + else
|
| + Fd = ::open(Filename.c_str(), OpenFlags);
|
| + if (Fd == -1) return error_code(errno, posix_category());
|
| + return success;
|
| + }
|
| +};
|
| +
|
| +}
|
| +
|
| +namespace llvm {
|
| +BitcodeStreamer *getBitcodeFileStreamer(const std::string &Filename,
|
| + std::string *StrError) {
|
| + BitcodeFileStreamer *s = new BitcodeFileStreamer();
|
| + error_code e = s->OpenFile(Filename);
|
| + if (e != success) {
|
| + *StrError = std::string() + "Could not open " + Filename + ": "
|
| + + e.message() + "\n";
|
| + return NULL;
|
| + }
|
| + return s;
|
| +}
|
| +
|
| +}
|
|
|