]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - examples/Kaleidoscope/BuildingAJIT/Chapter5/RemoteJITUtils.h
Vendor import of llvm trunk r290819:
[FreeBSD/FreeBSD.git] / examples / Kaleidoscope / BuildingAJIT / Chapter5 / RemoteJITUtils.h
1 //===-- RemoteJITUtils.h - Utilities for remote-JITing with LLI -*- C++ -*-===//
2 //
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 // Utilities for remote-JITing with LLI.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_TOOLS_LLI_REMOTEJITUTILS_H
15 #define LLVM_TOOLS_LLI_REMOTEJITUTILS_H
16
17 #include "llvm/ExecutionEngine/Orc/RawByteChannel.h"
18 #include "llvm/Support/Error.h"
19 #include <cassert>
20 #include <cerrno>
21 #include <system_error>
22
23 #if !defined(_MSC_VER) && !defined(__MINGW32__)
24 #include <unistd.h>
25 #else
26 #include <io.h>
27 #endif
28
29 /// RPC channel that reads from and writes from file descriptors.
30 class FDRPCChannel final : public llvm::orc::rpc::RawByteChannel {
31 public:
32   FDRPCChannel(int InFD, int OutFD) : InFD(InFD), OutFD(OutFD) {}
33
34   llvm::Error readBytes(char *Dst, unsigned Size) override {
35     assert(Dst && "Attempt to read into null.");
36     ssize_t Completed = 0;
37     while (Completed < static_cast<ssize_t>(Size)) {
38       ssize_t Read = ::read(InFD, Dst + Completed, Size - Completed);
39       if (Read <= 0) {
40         auto ErrNo = errno;
41         if (ErrNo == EAGAIN || ErrNo == EINTR)
42           continue;
43         else
44           return llvm::errorCodeToError(
45                    std::error_code(errno, std::generic_category()));
46       }
47       Completed += Read;
48     }
49     return llvm::Error::success();
50   }
51
52   llvm::Error appendBytes(const char *Src, unsigned Size) override {
53     assert(Src && "Attempt to append from null.");
54     ssize_t Completed = 0;
55     while (Completed < static_cast<ssize_t>(Size)) {
56       ssize_t Written = ::write(OutFD, Src + Completed, Size - Completed);
57       if (Written < 0) {
58         auto ErrNo = errno;
59         if (ErrNo == EAGAIN || ErrNo == EINTR)
60           continue;
61         else
62           return llvm::errorCodeToError(
63                    std::error_code(errno, std::generic_category()));
64       }
65       Completed += Written;
66     }
67     return llvm::Error::success();
68   }
69
70   llvm::Error send() override { return llvm::Error::success(); }
71
72 private:
73   int InFD, OutFD;
74 };
75
76 #endif // LLVM_TOOLS_LLI_REMOTEJITUTILS_H