Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 1 | //===- 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 | //===----------------------------------------------------------------------===// |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
Jim Stichnoth | 92a6e5b | 2015-12-02 16:52:44 -0800 | [diff] [blame] | 11 | /// \brief Declares the browser-based compile server. |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 12 | /// |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef SUBZERO_SRC_ICEBROWSERCOMPILESERVER_H |
| 16 | #define SUBZERO_SRC_ICEBROWSERCOMPILESERVER_H |
| 17 | |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 18 | #include "IceClFlags.h" |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 19 | #include "IceCompileServer.h" |
| 20 | #include "IceDefs.h" |
| 21 | #include "IceELFStreamer.h" |
| 22 | |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 23 | #include <atomic> |
| 24 | #include <thread> |
| 25 | |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 26 | namespace llvm { |
| 27 | class QueueStreamer; |
| 28 | class raw_fd_ostream; |
John Porto | 67f8de9 | 2015-06-25 10:14:17 -0700 | [diff] [blame] | 29 | } // end of namespace llvm |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 30 | |
| 31 | namespace Ice { |
| 32 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 33 | /// The browser variant of the compile server. Compared to the commandline |
| 34 | /// version, this version gets compile requests over IPC. Each compile request |
| 35 | /// will have a slimmed down version of argc, argv while other flags are set to |
| 36 | /// defaults that make sense in the browser case. The output file is specified |
| 37 | /// via a posix FD, and input bytes are pushed to the server. |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 38 | class BrowserCompileServer : public CompileServer { |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 39 | BrowserCompileServer(const BrowserCompileServer &) = delete; |
| 40 | BrowserCompileServer &operator=(const BrowserCompileServer &) = delete; |
Karl Schimpf | 2f67b92 | 2015-04-22 15:20:16 -0700 | [diff] [blame] | 41 | class StringStream; |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 42 | |
| 43 | public: |
Karl Schimpf | d469994 | 2016-04-02 09:55:31 -0700 | [diff] [blame] | 44 | BrowserCompileServer() : HadError(false) {} |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 45 | |
| 46 | ~BrowserCompileServer() final; |
| 47 | |
| 48 | void run() final; |
| 49 | |
Jan Voung | 2f7f2b7 | 2015-06-03 17:50:20 -0700 | [diff] [blame] | 50 | ErrorCode &getErrorCode() final; |
| 51 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 52 | /// Parse and set up the flags for compile jobs. |
Jim Stichnoth | fd07ad0 | 2016-04-20 10:12:46 -0700 | [diff] [blame] | 53 | void getParsedFlags(bool UseNumThreadsFromBrowser, uint32_t NumThreads, |
| 54 | int argc, const char *const *argv); |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 55 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 56 | /// Creates the streams + context and starts the compile thread, handing off |
| 57 | /// the streams + context. |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 58 | void startCompileThread(int OutFD); |
| 59 | |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 60 | /// Call to push more bytes to the current input stream. Returns false on |
| 61 | /// success and true on error. |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 62 | bool pushInputBytes(const void *Data, size_t NumBytes); |
| 63 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 64 | /// Notify the input stream of EOF. |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 65 | void endInputStream(); |
| 66 | |
Andrew Scull | 9612d32 | 2015-07-06 14:53:25 -0700 | [diff] [blame] | 67 | /// Wait for the compile thread to complete then reset the state. |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 68 | void waitForCompileThread() { |
| 69 | CompileThread.join(); |
Jan Voung | 2f7f2b7 | 2015-06-03 17:50:20 -0700 | [diff] [blame] | 70 | if (Ctx->getErrorStatus()->value()) |
| 71 | LastError.assign(Ctx->getErrorStatus()->value()); |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 72 | // Reset some state. The InputStream is deleted by the compiler so only |
| 73 | // reset this to nullptr. Free and flush the rest of the streams. |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 74 | InputStream = nullptr; |
| 75 | EmitStream.reset(nullptr); |
| 76 | ELFStream.reset(nullptr); |
| 77 | } |
| 78 | |
Jim Stichnoth | 620ad73 | 2015-04-28 14:12:20 -0700 | [diff] [blame] | 79 | StringStream &getErrorStream() { return *ErrorStream; } |
Karl Schimpf | 2f67b92 | 2015-04-22 15:20:16 -0700 | [diff] [blame] | 80 | |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 81 | void setFatalError(const std::string &Reason); |
Jan Voung | 2f7f2b7 | 2015-06-03 17:50:20 -0700 | [diff] [blame] | 82 | |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 83 | private: |
Karl Schimpf | 2f67b92 | 2015-04-22 15:20:16 -0700 | [diff] [blame] | 84 | class StringStream { |
| 85 | public: |
| 86 | StringStream() : StrBuf(Buffer) {} |
Jim Stichnoth | 467ffe5 | 2016-03-29 15:01:06 -0700 | [diff] [blame] | 87 | const std::string &getContents() { return StrBuf.str(); } |
Karl Schimpf | 2f67b92 | 2015-04-22 15:20:16 -0700 | [diff] [blame] | 88 | Ostream &getStream() { return StrBuf; } |
Jim Stichnoth | 620ad73 | 2015-04-28 14:12:20 -0700 | [diff] [blame] | 89 | |
Karl Schimpf | 2f67b92 | 2015-04-22 15:20:16 -0700 | [diff] [blame] | 90 | private: |
| 91 | std::string Buffer; |
| 92 | llvm::raw_string_ostream StrBuf; |
| 93 | }; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 94 | /// This currently only handles a single compile request, hence one copy of |
| 95 | /// the state. |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 96 | std::unique_ptr<GlobalContext> Ctx; |
Andrew Scull | 57e1268 | 2015-09-16 11:30:19 -0700 | [diff] [blame] | 97 | /// A borrowed reference to the current InputStream. The compiler owns the |
| 98 | /// actual reference so the server must be careful not to access after the |
| 99 | /// compiler is done. |
Jim Stichnoth | eafb56c | 2015-06-22 10:35:22 -0700 | [diff] [blame] | 100 | llvm::QueueStreamer *InputStream = nullptr; |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 101 | std::unique_ptr<Ostream> LogStream; |
| 102 | std::unique_ptr<llvm::raw_fd_ostream> EmitStream; |
Karl Schimpf | 2f67b92 | 2015-04-22 15:20:16 -0700 | [diff] [blame] | 103 | std::unique_ptr<StringStream> ErrorStream; |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 104 | std::unique_ptr<ELFStreamer> ELFStream; |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 105 | std::thread CompileThread; |
Jan Voung | 2f7f2b7 | 2015-06-03 17:50:20 -0700 | [diff] [blame] | 106 | std::atomic<bool> HadError; |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | } // end of namespace Ice |
| 110 | |
| 111 | #endif // SUBZERO_SRC_ICEBROWSERCOMPILESERVER_H |