blob: 3b10fc5eecaa3442e40da172429a5573a4e43be9 [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 Espindola1aabf982015-06-16 23:29:49 +000019#include "llvm/ADT/STLExtras.h"
Rafael Espindola6d354812013-07-16 19:44:17 +000020#include "llvm/Support/FileSystem.h"
Derek Schuff76b292f2012-02-07 00:28:46 +000021#include "llvm/Support/Program.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include <string>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000023#include <system_error>
Derek Schuff8b2dcad2012-02-06 22:30:29 +000024#if !defined(_MSC_VER) && !defined(__MINGW32__)
25#include <unistd.h>
26#else
27#include <io.h>
28#endif
Derek Schuff8b2dcad2012-02-06 22:30:29 +000029using namespace llvm;
30
Chandler Carruth64648262014-04-22 03:07:47 +000031#define DEBUG_TYPE "Data-stream"
32
Derek Schuff8b2dcad2012-02-06 22:30:29 +000033// Interface goals:
Rafael Espindola79e1f9f2014-11-12 03:55:46 +000034// * StreamingMemoryObject doesn't care about complexities like using
Derek Schuff8b2dcad2012-02-06 22:30:29 +000035// threads/async callbacks to actually overlap download+compile
36// * Don't want to duplicate Data in memory
37// * Don't need to know total Data len in advance
38// Non-goals:
Rafael Espindola79e1f9f2014-11-12 03:55:46 +000039// StreamingMemoryObject already has random access so this interface only does
Derek Schuff8b2dcad2012-02-06 22:30:29 +000040// in-order streaming (no arbitrary seeking, else we'd have to buffer all the
41// Data here in addition to MemoryObject). This also means that if we want
42// to be able to to free Data, BitstreamBytes/BitcodeReader will implement it
43
44STATISTIC(NumStreamFetches, "Number of calls to Data stream fetch");
45
46namespace llvm {
47DataStreamer::~DataStreamer() {}
48}
49
50namespace {
51
Derek Schuff8b2dcad2012-02-06 22:30:29 +000052// Very simple stream backed by a file. Mostly useful for stdin and debugging;
53// actual file access is probably still best done with mmap.
54class DataFileStreamer : public DataStreamer {
55 int Fd;
56public:
57 DataFileStreamer() : Fd(0) {}
Alexander Kornienkof817c1c2015-04-11 02:11:45 +000058 ~DataFileStreamer() override { close(Fd); }
Craig Topper6ff5aa72014-03-10 03:53:12 +000059 size_t GetBytes(unsigned char *buf, size_t len) override {
Derek Schuff8b2dcad2012-02-06 22:30:29 +000060 NumStreamFetches++;
61 return read(Fd, buf, len);
62 }
63
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000064 std::error_code OpenFile(const std::string &Filename) {
David Blaikiee7d7a5d2012-02-09 00:29:19 +000065 if (Filename == "-") {
66 Fd = 0;
Rafael Espindolacb2eca02013-06-12 20:58:35 +000067 sys::ChangeStdinToBinary();
Rafael Espindoladb4ed0b2014-06-13 02:24:39 +000068 return std::error_code();
David Blaikiee7d7a5d2012-02-09 00:29:19 +000069 }
Rafael Espindola6d354812013-07-16 19:44:17 +000070
71 return sys::fs::openFileForRead(Filename, Fd);
Derek Schuff8b2dcad2012-02-06 22:30:29 +000072 }
73};
74
Alexander Kornienkof00654e2015-06-23 09:49:53 +000075}
Derek Schuff8b2dcad2012-02-06 22:30:29 +000076
Rafael Espindola1aabf982015-06-16 23:29:49 +000077std::unique_ptr<DataStreamer>
78llvm::getDataFileStreamer(const std::string &Filename, std::string *StrError) {
79 std::unique_ptr<DataFileStreamer> s = make_unique<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 }
Rafael Espindola1aabf982015-06-16 23:29:49 +000085 return std::move(s);
Derek Schuff8b2dcad2012-02-06 22:30:29 +000086}