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