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 | //===----------------------------------------------------------------------===// |
| 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 Voung | 2f7f2b7 | 2015-06-03 17:50:20 -0700 | [diff] [blame] | 17 | #include <atomic> |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 18 | #include <thread> |
| 19 | |
| 20 | #include "IceClFlags.h" |
| 21 | #include "IceClFlagsExtra.h" |
| 22 | #include "IceCompileServer.h" |
| 23 | #include "IceDefs.h" |
| 24 | #include "IceELFStreamer.h" |
| 25 | |
| 26 | namespace llvm { |
| 27 | class QueueStreamer; |
| 28 | class raw_fd_ostream; |
Mircea Trofin | 59c6f5a | 2015-04-06 16:06:47 -0700 | [diff] [blame] | 29 | } |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 30 | |
| 31 | namespace 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. |
| 39 | class BrowserCompileServer : public CompileServer { |
| 40 | BrowserCompileServer() = delete; |
| 41 | BrowserCompileServer(const BrowserCompileServer &) = delete; |
| 42 | BrowserCompileServer &operator=(const BrowserCompileServer &) = delete; |
Karl Schimpf | 2f67b92 | 2015-04-22 15:20:16 -0700 | [diff] [blame] | 43 | class StringStream; |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 44 | |
| 45 | public: |
| 46 | explicit BrowserCompileServer(Compiler &Comp) |
Jan Voung | 2f7f2b7 | 2015-06-03 17:50:20 -0700 | [diff] [blame] | 47 | : CompileServer(Comp), InputStream(nullptr), HadError(false) {} |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 48 | |
| 49 | ~BrowserCompileServer() final; |
| 50 | |
| 51 | void run() final; |
| 52 | |
Jan Voung | 2f7f2b7 | 2015-06-03 17:50:20 -0700 | [diff] [blame] | 53 | ErrorCode &getErrorCode() final; |
| 54 | |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 55 | // 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 Voung | 2f7f2b7 | 2015-06-03 17:50:20 -0700 | [diff] [blame] | 72 | if (Ctx->getErrorStatus()->value()) |
| 73 | LastError.assign(Ctx->getErrorStatus()->value()); |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 74 | // 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 Stichnoth | 620ad73 | 2015-04-28 14:12:20 -0700 | [diff] [blame] | 82 | StringStream &getErrorStream() { return *ErrorStream; } |
Karl Schimpf | 2f67b92 | 2015-04-22 15:20:16 -0700 | [diff] [blame] | 83 | |
Jan Voung | 2f7f2b7 | 2015-06-03 17:50:20 -0700 | [diff] [blame] | 84 | void setFatalError(const IceString &Reason); |
| 85 | |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 86 | private: |
Karl Schimpf | 2f67b92 | 2015-04-22 15:20:16 -0700 | [diff] [blame] | 87 | class StringStream { |
| 88 | public: |
| 89 | StringStream() : StrBuf(Buffer) {} |
| 90 | const IceString &getContents() { return StrBuf.str(); } |
| 91 | Ostream &getStream() { return StrBuf; } |
Jim Stichnoth | 620ad73 | 2015-04-28 14:12:20 -0700 | [diff] [blame] | 92 | |
Karl Schimpf | 2f67b92 | 2015-04-22 15:20:16 -0700 | [diff] [blame] | 93 | private: |
| 94 | std::string Buffer; |
| 95 | llvm::raw_string_ostream StrBuf; |
| 96 | }; |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 97 | // 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 Schimpf | 2f67b92 | 2015-04-22 15:20:16 -0700 | [diff] [blame] | 106 | std::unique_ptr<StringStream> ErrorStream; |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 107 | std::unique_ptr<ELFStreamer> ELFStream; |
| 108 | ClFlags Flags; |
| 109 | ClFlagsExtra ExtraFlags; |
| 110 | std::thread CompileThread; |
Jan Voung | 2f7f2b7 | 2015-06-03 17:50:20 -0700 | [diff] [blame] | 111 | std::atomic<bool> HadError; |
Jan Voung | 44c3a80 | 2015-03-27 16:29:08 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | } // end of namespace Ice |
| 115 | |
| 116 | #endif // SUBZERO_SRC_ICEBROWSERCOMPILESERVER_H |