[C++11] Replace OwningPtr::take() with OwningPtr::release().
[oota-llvm.git] / tools / lli / RemoteTargetExternal.h
1 //===----- RemoteTargetExternal.h - LLVM out-of-process JIT execution -----===//
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 // Definition of the RemoteTargetExternal class which executes JITed code in a
11 // separate process from where it was built.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLI_REMOTETARGETEXTERNAL_H
16 #define LLI_REMOTETARGETEXTERNAL_H
17
18 #include "RPCChannel.h"
19 #include "RemoteTarget.h"
20 #include "RemoteTargetMessage.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/Config/config.h"
24 #include "llvm/Support/DataTypes.h"
25 #include "llvm/Support/Memory.h"
26 #include <stdlib.h>
27 #include <string>
28
29 namespace llvm {
30
31 class RemoteTargetExternal : public RemoteTarget {
32   RPCChannel RPC;
33
34   bool WriteBytes(const void *Data, size_t Size) {
35     return RPC.WriteBytes(Data, Size);
36   }
37
38   bool ReadBytes(void *Data, size_t Size) { return RPC.ReadBytes(Data, Size); }
39
40 public:
41   /// Allocate space in the remote target address space.
42   ///
43   /// @param      Size      Amount of space, in bytes, to allocate.
44   /// @param      Alignment Required minimum alignment for allocated space.
45   /// @param[out] Address   Remote address of the allocated memory.
46   ///
47   /// @returns True on success. On failure, ErrorMsg is updated with
48   ///          descriptive text of the encountered error.
49   virtual bool allocateSpace(size_t Size,
50                              unsigned Alignment,
51                              uint64_t &Address);
52
53   /// Load data into the target address space.
54   ///
55   /// @param      Address   Destination address in the target process.
56   /// @param      Data      Source address in the host process.
57   /// @param      Size      Number of bytes to copy.
58   ///
59   /// @returns True on success. On failure, ErrorMsg is updated with
60   ///          descriptive text of the encountered error.
61   virtual bool loadData(uint64_t Address, const void *Data, size_t Size);
62
63   /// Load code into the target address space and prepare it for execution.
64   ///
65   /// @param      Address   Destination address in the target process.
66   /// @param      Data      Source address in the host process.
67   /// @param      Size      Number of bytes to copy.
68   ///
69   /// @returns True on success. On failure, ErrorMsg is updated with
70   ///          descriptive text of the encountered error.
71   virtual bool loadCode(uint64_t Address, const void *Data, size_t Size);
72
73   /// Execute code in the target process. The called function is required
74   /// to be of signature int "(*)(void)".
75   ///
76   /// @param      Address   Address of the loaded function in the target
77   ///                       process.
78   /// @param[out] RetVal    The integer return value of the called function.
79   ///
80   /// @returns True on success. On failure, ErrorMsg is updated with
81   ///          descriptive text of the encountered error.
82   virtual bool executeCode(uint64_t Address, int &RetVal);
83
84   /// Minimum alignment for memory permissions. Used to separate code and
85   /// data regions to make sure data doesn't get marked as code or vice
86   /// versa.
87   ///
88   /// @returns Page alignment return value. Default of 4k.
89   virtual unsigned getPageAlignment() { return 4096; }
90
91   virtual bool create() {
92     RPC.ChildName = ChildName;
93     if (!RPC.createServer())
94       return true;
95
96     // We must get Ack from the client (blocking read)
97     if (!Receive(LLI_ChildActive)) {
98       ErrorMsg += ", (RPCChannel::create) - Stopping process!";
99       stop();
100       return false;
101     }
102
103     return true;
104   }
105
106   /// Terminate the remote process.
107   virtual void stop();
108
109   RemoteTargetExternal(std::string &Name) : RemoteTarget(), ChildName(Name) {}
110   virtual ~RemoteTargetExternal() {}
111
112 private:
113   std::string ChildName;
114
115   bool SendAllocateSpace(uint32_t Alignment, uint32_t Size);
116   bool SendLoadSection(uint64_t Addr,
117                        const void *Data,
118                        uint32_t Size,
119                        bool IsCode);
120   bool SendExecute(uint64_t Addr);
121   bool SendTerminate();
122
123   // High-level wrappers for receiving data
124   bool Receive(LLIMessageType Msg);
125   bool Receive(LLIMessageType Msg, int32_t &Data);
126   bool Receive(LLIMessageType Msg, uint64_t &Data);
127
128   // Lower level target-independent read/write to deal with errors
129   bool ReceiveHeader(LLIMessageType Msg);
130   bool ReceivePayload();
131   bool SendHeader(LLIMessageType Msg);
132   bool SendPayload();
133
134   // Functions to append/retrieve data from the payload
135   SmallVector<const void *, 2> SendData;
136   SmallVector<void *, 1> ReceiveData; // Future proof
137   SmallVector<int, 2> Sizes;
138   void AppendWrite(const void *Data, uint32_t Size);
139   void AppendRead(void *Data, uint32_t Size);
140 };
141
142 } // end namespace llvm
143
144 #endif // LLI_REMOTETARGETEXTERNAL_H