blob: 4075d9d3f27390c37ce5ff92912bee31b676faf4 [file] [log] [blame]
Jan Voung44c3a802015-03-27 16:29:08 -07001//===- subzero/src/IceBrowserCompileServer.h - Browser server ---*- C++ -*-===//
2//
3// The Subzero Code Generator
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 declares the browser-specific compile server.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef SUBZERO_SRC_ICEBROWSERCOMPILESERVER_H
15#define SUBZERO_SRC_ICEBROWSERCOMPILESERVER_H
16
Jan Voung2f7f2b72015-06-03 17:50:20 -070017#include <atomic>
Jan Voung44c3a802015-03-27 16:29:08 -070018#include <thread>
19
20#include "IceClFlags.h"
21#include "IceClFlagsExtra.h"
22#include "IceCompileServer.h"
23#include "IceDefs.h"
24#include "IceELFStreamer.h"
25
26namespace llvm {
27class QueueStreamer;
28class raw_fd_ostream;
Mircea Trofin59c6f5a2015-04-06 16:06:47 -070029}
Jan Voung44c3a802015-03-27 16:29:08 -070030
31namespace Ice {
32
33// The browser variant of the compile server.
34// Compared to the commandline version, this version gets compile
35// requests over IPC. Each compile request will have a slimmed down
36// version of argc, argv while other flags are set to defaults that
37// make sense in the browser case. The output file is specified via
38// a posix FD, and input bytes are pushed to the server.
39class BrowserCompileServer : public CompileServer {
40 BrowserCompileServer() = delete;
41 BrowserCompileServer(const BrowserCompileServer &) = delete;
42 BrowserCompileServer &operator=(const BrowserCompileServer &) = delete;
Karl Schimpf2f67b922015-04-22 15:20:16 -070043 class StringStream;
Jan Voung44c3a802015-03-27 16:29:08 -070044
45public:
46 explicit BrowserCompileServer(Compiler &Comp)
Jan Voung2f7f2b72015-06-03 17:50:20 -070047 : CompileServer(Comp), InputStream(nullptr), HadError(false) {}
Jan Voung44c3a802015-03-27 16:29:08 -070048
49 ~BrowserCompileServer() final;
50
51 void run() final;
52
Jan Voung2f7f2b72015-06-03 17:50:20 -070053 ErrorCode &getErrorCode() final;
54
Jan Voung44c3a802015-03-27 16:29:08 -070055 // Parse and set up the flags for compile jobs.
56 void getParsedFlags(uint32_t NumThreads, int argc, char **argv);
57
58 // Creates the streams + context and starts the compile thread,
59 // handing off the streams + context.
60 void startCompileThread(int OutFD);
61
62 // Call to push more bytes to the current input stream.
63 // Returns false on success and true on error.
64 bool pushInputBytes(const void *Data, size_t NumBytes);
65
66 // Notify the input stream of EOF.
67 void endInputStream();
68
69 // Wait for the compile thread to complete then reset the state.
70 void waitForCompileThread() {
71 CompileThread.join();
Jan Voung2f7f2b72015-06-03 17:50:20 -070072 if (Ctx->getErrorStatus()->value())
73 LastError.assign(Ctx->getErrorStatus()->value());
Jan Voung44c3a802015-03-27 16:29:08 -070074 // Reset some state. The InputStream is deleted by the compiler
75 // so only reset this to nullptr. Free and flush the rest
76 // of the streams.
77 InputStream = nullptr;
78 EmitStream.reset(nullptr);
79 ELFStream.reset(nullptr);
80 }
81
Jim Stichnoth620ad732015-04-28 14:12:20 -070082 StringStream &getErrorStream() { return *ErrorStream; }
Karl Schimpf2f67b922015-04-22 15:20:16 -070083
Jan Voung2f7f2b72015-06-03 17:50:20 -070084 void setFatalError(const IceString &Reason);
85
Jan Voung44c3a802015-03-27 16:29:08 -070086private:
Karl Schimpf2f67b922015-04-22 15:20:16 -070087 class StringStream {
88 public:
89 StringStream() : StrBuf(Buffer) {}
90 const IceString &getContents() { return StrBuf.str(); }
91 Ostream &getStream() { return StrBuf; }
Jim Stichnoth620ad732015-04-28 14:12:20 -070092
Karl Schimpf2f67b922015-04-22 15:20:16 -070093 private:
94 std::string Buffer;
95 llvm::raw_string_ostream StrBuf;
96 };
Jan Voung44c3a802015-03-27 16:29:08 -070097 // This currently only handles a single compile request, hence one copy
98 // of the state.
99 std::unique_ptr<GlobalContext> Ctx;
100 // A borrowed reference to the current InputStream. The compiler owns
101 // the actual reference so the server must be careful not to access
102 // after the compiler is done.
103 llvm::QueueStreamer *InputStream;
104 std::unique_ptr<Ostream> LogStream;
105 std::unique_ptr<llvm::raw_fd_ostream> EmitStream;
Karl Schimpf2f67b922015-04-22 15:20:16 -0700106 std::unique_ptr<StringStream> ErrorStream;
Jan Voung44c3a802015-03-27 16:29:08 -0700107 std::unique_ptr<ELFStreamer> ELFStream;
108 ClFlags Flags;
109 ClFlagsExtra ExtraFlags;
110 std::thread CompileThread;
Jan Voung2f7f2b72015-06-03 17:50:20 -0700111 std::atomic<bool> HadError;
Jan Voung44c3a802015-03-27 16:29:08 -0700112};
113
114} // end of namespace Ice
115
116#endif // SUBZERO_SRC_ICEBROWSERCOMPILESERVER_H