blob: 32653de5194383549de9bb6b88b427383cca9f1f [file] [log] [blame]
Nick Lewycky998bce02012-02-06 22:41:47 +00001//===--- llvm/Support/DataStream.cpp - Lazy streamed data -----------------===//
Derek Schuff8b2dcad2012-02-06 22:30:29 +00002//
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 DataStreamer, which fetches bytes of Data 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
Derek Schuff8b2dcad2012-02-06 22:30:29 +000017#include "llvm/Support/DataStream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/Statistic.h"
Rafael Espindola6d354812013-07-16 19:44:17 +000019#include "llvm/Support/FileSystem.h"
Derek Schuff76b292f2012-02-07 00:28:46 +000020#include "llvm/Support/Program.h"
Derek Schuff8b2dcad2012-02-06 22:30:29 +000021#include <cerrno>
22#include <cstdio>
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include <string>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000024#include <system_error>
Derek Schuff8b2dcad2012-02-06 22:30:29 +000025#if !defined(_MSC_VER) && !defined(__MINGW32__)
26#include <unistd.h>
27#else
28#include <io.h>
29#endif
Derek Schuff8b2dcad2012-02-06 22:30:29 +000030using namespace llvm;
31
Chandler Carruth64648262014-04-22 03:07:47 +000032#define DEBUG_TYPE "Data-stream"
33
Derek Schuff8b2dcad2012-02-06 22:30:29 +000034// Interface goals:
35// * StreamableMemoryObject doesn't care about complexities like using
36// threads/async callbacks to actually overlap download+compile
37// * Don't want to duplicate Data in memory
38// * Don't need to know total Data len in advance
39// Non-goals:
40// StreamableMemoryObject already has random access so this interface only does
41// in-order streaming (no arbitrary seeking, else we'd have to buffer all the
42// Data here in addition to MemoryObject). This also means that if we want
43// to be able to to free Data, BitstreamBytes/BitcodeReader will implement it
44
45STATISTIC(NumStreamFetches, "Number of calls to Data stream fetch");
46
47namespace llvm {
48DataStreamer::~DataStreamer() {}
49}
50
51namespace {
52
Derek Schuff8b2dcad2012-02-06 22:30:29 +000053// Very simple stream backed by a file. Mostly useful for stdin and debugging;
54// actual file access is probably still best done with mmap.
55class DataFileStreamer : public DataStreamer {
56 int Fd;
57public:
58 DataFileStreamer() : Fd(0) {}
59 virtual ~DataFileStreamer() {
60 close(Fd);
61 }
Craig Topper6ff5aa72014-03-10 03:53:12 +000062 size_t GetBytes(unsigned char *buf, size_t len) override {
Derek Schuff8b2dcad2012-02-06 22:30:29 +000063 NumStreamFetches++;
64 return read(Fd, buf, len);
65 }
66
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000067 std::error_code OpenFile(const std::string &Filename) {
David Blaikiee7d7a5d2012-02-09 00:29:19 +000068 if (Filename == "-") {
69 Fd = 0;
Rafael Espindolacb2eca02013-06-12 20:58:35 +000070 sys::ChangeStdinToBinary();
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000071 return std::error_code();
David Blaikiee7d7a5d2012-02-09 00:29:19 +000072 }
Rafael Espindola6d354812013-07-16 19:44:17 +000073
74 return sys::fs::openFileForRead(Filename, Fd);
Derek Schuff8b2dcad2012-02-06 22:30:29 +000075 }
76};
77
78}
79
80namespace llvm {
81DataStreamer *getDataFileStreamer(const std::string &Filename,
82 std::string *StrError) {
83 DataFileStreamer *s = new DataFileStreamer();
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000084 if (std::error_code e = s->OpenFile(Filename)) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +000085 *StrError = std::string("Could not open ") + Filename + ": " +
86 e.message() + "\n";
Craig Topperc10719f2014-04-07 04:17:22 +000087 return nullptr;
Derek Schuff8b2dcad2012-02-06 22:30:29 +000088 }
89 return s;
90}
91
92}