blob: c24315526cffc721813af7acb3f65c507c8b9bf9 [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include <string>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000022#include <system_error>
Derek Schuff8b2dcad2012-02-06 22:30:29 +000023#if !defined(_MSC_VER) && !defined(__MINGW32__)
24#include <unistd.h>
25#else
26#include <io.h>
27#endif
Derek Schuff8b2dcad2012-02-06 22:30:29 +000028using namespace llvm;
29
Chandler Carruth64648262014-04-22 03:07:47 +000030#define DEBUG_TYPE "Data-stream"
31
Derek Schuff8b2dcad2012-02-06 22:30:29 +000032// Interface goals:
Rafael Espindola79e1f9f2014-11-12 03:55:46 +000033// * StreamingMemoryObject doesn't care about complexities like using
Derek Schuff8b2dcad2012-02-06 22:30:29 +000034// threads/async callbacks to actually overlap download+compile
35// * Don't want to duplicate Data in memory
36// * Don't need to know total Data len in advance
37// Non-goals:
Rafael Espindola79e1f9f2014-11-12 03:55:46 +000038// StreamingMemoryObject already has random access so this interface only does
Derek Schuff8b2dcad2012-02-06 22:30:29 +000039// in-order streaming (no arbitrary seeking, else we'd have to buffer all the
40// Data here in addition to MemoryObject). This also means that if we want
41// to be able to to free Data, BitstreamBytes/BitcodeReader will implement it
42
43STATISTIC(NumStreamFetches, "Number of calls to Data stream fetch");
44
45namespace llvm {
46DataStreamer::~DataStreamer() {}
47}
48
49namespace {
50
Derek Schuff8b2dcad2012-02-06 22:30:29 +000051// Very simple stream backed by a file. Mostly useful for stdin and debugging;
52// actual file access is probably still best done with mmap.
53class DataFileStreamer : public DataStreamer {
54 int Fd;
55public:
56 DataFileStreamer() : Fd(0) {}
Alexander Kornienkof817c1c2015-04-11 02:11:45 +000057 ~DataFileStreamer() override { close(Fd); }
Craig Topper6ff5aa72014-03-10 03:53:12 +000058 size_t GetBytes(unsigned char *buf, size_t len) override {
Derek Schuff8b2dcad2012-02-06 22:30:29 +000059 NumStreamFetches++;
60 return read(Fd, buf, len);
61 }
62
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000063 std::error_code OpenFile(const std::string &Filename) {
David Blaikiee7d7a5d2012-02-09 00:29:19 +000064 if (Filename == "-") {
65 Fd = 0;
Rafael Espindolacb2eca02013-06-12 20:58:35 +000066 sys::ChangeStdinToBinary();
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000067 return std::error_code();
David Blaikiee7d7a5d2012-02-09 00:29:19 +000068 }
Rafael Espindola6d354812013-07-16 19:44:17 +000069
70 return sys::fs::openFileForRead(Filename, Fd);
Derek Schuff8b2dcad2012-02-06 22:30:29 +000071 }
72};
73
74}
75
76namespace llvm {
77DataStreamer *getDataFileStreamer(const std::string &Filename,
78 std::string *StrError) {
79 DataFileStreamer *s = new DataFileStreamer();
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000080 if (std::error_code e = s->OpenFile(Filename)) {
Derek Schuff8b2dcad2012-02-06 22:30:29 +000081 *StrError = std::string("Could not open ") + Filename + ": " +
82 e.message() + "\n";
Craig Topperc10719f2014-04-07 04:17:22 +000083 return nullptr;
Derek Schuff8b2dcad2012-02-06 22:30:29 +000084 }
85 return s;
86}
87
88}