ReleaseNotes: new Win EH instructions; by David Majnemer
[oota-llvm.git] / tools / lli / 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/RPCChannel.h"
18 #include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
19
20 #if !defined(_MSC_VER) && !defined(__MINGW32__)
21 #include <unistd.h>
22 #else
23 #include <io.h>
24 #endif
25
26 /// RPC channel that reads from and writes from file descriptors.
27 class FDRPCChannel : public llvm::orc::remote::RPCChannel {
28 public:
29   FDRPCChannel(int InFD, int OutFD) : InFD(InFD), OutFD(OutFD) {}
30
31   std::error_code readBytes(char *Dst, unsigned Size) override {
32     assert(Dst && "Attempt to read into null.");
33     ssize_t ReadResult = ::read(InFD, Dst, Size);
34     if (ReadResult != (ssize_t)Size)
35       return std::error_code(errno, std::generic_category());
36     return std::error_code();
37   }
38
39   std::error_code appendBytes(const char *Src, unsigned Size) override {
40     assert(Src && "Attempt to append from null.");
41     ssize_t WriteResult = ::write(OutFD, Src, Size);
42     if (WriteResult != (ssize_t)Size)
43       std::error_code(errno, std::generic_category());
44     return std::error_code();
45   }
46
47   std::error_code send() override { return std::error_code(); }
48
49 private:
50   int InFD, OutFD;
51 };
52
53 // launch the remote process (see lli.cpp) and return a channel to it.
54 std::unique_ptr<FDRPCChannel> launchRemote();
55
56 namespace llvm {
57
58 // ForwardingMM - Adapter to connect MCJIT to Orc's Remote memory manager.
59 class ForwardingMemoryManager : public llvm::RTDyldMemoryManager {
60 public:
61   void setMemMgr(std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr) {
62     this->MemMgr = std::move(MemMgr);
63   }
64
65   void setResolver(std::unique_ptr<RuntimeDyld::SymbolResolver> Resolver) {
66     this->Resolver = std::move(Resolver);
67   }
68
69   uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
70                                unsigned SectionID,
71                                StringRef SectionName) override {
72     return MemMgr->allocateCodeSection(Size, Alignment, SectionID, SectionName);
73   }
74
75   uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
76                                unsigned SectionID, StringRef SectionName,
77                                bool IsReadOnly) override {
78     return MemMgr->allocateDataSection(Size, Alignment, SectionID, SectionName,
79                                        IsReadOnly);
80   }
81
82   void reserveAllocationSpace(uintptr_t CodeSize, uint32_t CodeAlign,
83                               uintptr_t RODataSize, uint32_t RODataAlign,
84                               uintptr_t RWDataSize,
85                               uint32_t RWDataAlign) override {
86     MemMgr->reserveAllocationSpace(CodeSize, CodeAlign, RODataSize, RODataAlign,
87                                    RWDataSize, RWDataAlign);
88   }
89
90   bool needsToReserveAllocationSpace() override {
91     return MemMgr->needsToReserveAllocationSpace();
92   }
93
94   void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr,
95                         size_t Size) override {
96     MemMgr->registerEHFrames(Addr, LoadAddr, Size);
97   }
98
99   void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr,
100                           size_t Size) override {
101     MemMgr->deregisterEHFrames(Addr, LoadAddr, Size);
102   }
103
104   bool finalizeMemory(std::string *ErrMsg = nullptr) override {
105     return MemMgr->finalizeMemory(ErrMsg);
106   }
107
108   void notifyObjectLoaded(RuntimeDyld &RTDyld,
109                           const object::ObjectFile &Obj) override {
110     MemMgr->notifyObjectLoaded(RTDyld, Obj);
111   }
112
113   // Don't hide the sibling notifyObjectLoaded from RTDyldMemoryManager.
114   using RTDyldMemoryManager::notifyObjectLoaded;
115
116   RuntimeDyld::SymbolInfo findSymbol(const std::string &Name) override {
117     return Resolver->findSymbol(Name);
118   }
119
120   RuntimeDyld::SymbolInfo
121   findSymbolInLogicalDylib(const std::string &Name) override {
122     return Resolver->findSymbolInLogicalDylib(Name);
123   }
124
125 private:
126   std::unique_ptr<RuntimeDyld::MemoryManager> MemMgr;
127   std::unique_ptr<RuntimeDyld::SymbolResolver> Resolver;
128 };
129 }
130
131 #endif