1259698Sdim//===----- RemoteTargetExternal.h - LLVM out-of-process JIT execution -----===//
2259698Sdim//
3259698Sdim//                     The LLVM Compiler Infrastructure
4259698Sdim//
5259698Sdim// This file is distributed under the University of Illinois Open Source
6259698Sdim// License. See LICENSE.TXT for details.
7259698Sdim//
8259698Sdim//===----------------------------------------------------------------------===//
9259698Sdim//
10259698Sdim// Definition of the RemoteTargetExternal class which executes JITed code in a
11259698Sdim// separate process from where it was built.
12259698Sdim//
13259698Sdim//===----------------------------------------------------------------------===//
14259698Sdim
15259698Sdim#ifndef LLI_REMOTETARGETEXTERNAL_H
16259698Sdim#define LLI_REMOTETARGETEXTERNAL_H
17259698Sdim
18259698Sdim#include "llvm/Config/config.h"
19259698Sdim
20259698Sdim#include "llvm/ADT/SmallVector.h"
21259698Sdim#include "llvm/ADT/StringRef.h"
22259698Sdim#include "llvm/Support/DataTypes.h"
23259698Sdim#include "llvm/Support/Memory.h"
24259698Sdim#include <stdlib.h>
25259698Sdim#include <string>
26259698Sdim
27259698Sdim#include "RemoteTarget.h"
28259698Sdim#include "RemoteTargetMessage.h"
29259698Sdim
30259698Sdimnamespace llvm {
31259698Sdim
32259698Sdimclass RemoteTargetExternal : public RemoteTarget {
33259698Sdimpublic:
34259698Sdim  /// Allocate space in the remote target address space.
35259698Sdim  ///
36259698Sdim  /// @param      Size      Amount of space, in bytes, to allocate.
37259698Sdim  /// @param      Alignment Required minimum alignment for allocated space.
38259698Sdim  /// @param[out] Address   Remote address of the allocated memory.
39259698Sdim  ///
40259698Sdim  /// @returns False on success. On failure, ErrorMsg is updated with
41259698Sdim  ///          descriptive text of the encountered error.
42259698Sdim  virtual bool allocateSpace(size_t Size,
43259698Sdim                             unsigned Alignment,
44259698Sdim                             uint64_t &Address);
45259698Sdim
46259698Sdim  /// Load data into the target address space.
47259698Sdim  ///
48259698Sdim  /// @param      Address   Destination address in the target process.
49259698Sdim  /// @param      Data      Source address in the host process.
50259698Sdim  /// @param      Size      Number of bytes to copy.
51259698Sdim  ///
52259698Sdim  /// @returns False on success. On failure, ErrorMsg is updated with
53259698Sdim  ///          descriptive text of the encountered error.
54259698Sdim  virtual bool loadData(uint64_t Address, const void *Data, size_t Size);
55259698Sdim
56259698Sdim  /// Load code into the target address space and prepare it for execution.
57259698Sdim  ///
58259698Sdim  /// @param      Address   Destination address in the target process.
59259698Sdim  /// @param      Data      Source address in the host process.
60259698Sdim  /// @param      Size      Number of bytes to copy.
61259698Sdim  ///
62259698Sdim  /// @returns False on success. On failure, ErrorMsg is updated with
63259698Sdim  ///          descriptive text of the encountered error.
64259698Sdim  virtual bool loadCode(uint64_t Address, const void *Data, size_t Size);
65259698Sdim
66259698Sdim  /// Execute code in the target process. The called function is required
67259698Sdim  /// to be of signature int "(*)(void)".
68259698Sdim  ///
69259698Sdim  /// @param      Address   Address of the loaded function in the target
70259698Sdim  ///                       process.
71259698Sdim  /// @param[out] RetVal    The integer return value of the called function.
72259698Sdim  ///
73259698Sdim  /// @returns False on success. On failure, ErrorMsg is updated with
74259698Sdim  ///          descriptive text of the encountered error.
75259698Sdim  virtual bool executeCode(uint64_t Address, int &RetVal);
76259698Sdim
77259698Sdim  /// Minimum alignment for memory permissions. Used to seperate code and
78259698Sdim  /// data regions to make sure data doesn't get marked as code or vice
79259698Sdim  /// versa.
80259698Sdim  ///
81259698Sdim  /// @returns Page alignment return value. Default of 4k.
82259698Sdim  virtual unsigned getPageAlignment() { return 4096; }
83259698Sdim
84259698Sdim  /// Start the remote process.
85259698Sdim  virtual void create();
86259698Sdim
87259698Sdim  /// Terminate the remote process.
88259698Sdim  virtual void stop();
89259698Sdim
90259698Sdim  RemoteTargetExternal(std::string &Name) : RemoteTarget(), ChildName(Name) {}
91259698Sdim  virtual ~RemoteTargetExternal();
92259698Sdim
93259698Sdimprivate:
94259698Sdim  std::string ChildName;
95259698Sdim
96259698Sdim  // This will get filled in as a point to an OS-specific structure.
97259698Sdim  void *ConnectionData;
98259698Sdim
99259698Sdim  void SendAllocateSpace(uint32_t Alignment, uint32_t Size);
100259698Sdim  void SendLoadSection(uint64_t Addr,
101259698Sdim                       const void *Data,
102259698Sdim                       uint32_t Size,
103259698Sdim                       bool IsCode);
104259698Sdim  void SendExecute(uint64_t Addr);
105259698Sdim  void SendTerminate();
106259698Sdim
107259698Sdim  void Receive(LLIMessageType Msg);
108259698Sdim  void Receive(LLIMessageType Msg, int &Data);
109259698Sdim  void Receive(LLIMessageType Msg, uint64_t &Data);
110259698Sdim
111259698Sdim  int WriteBytes(const void *Data, size_t Size);
112259698Sdim  int ReadBytes(void *Data, size_t Size);
113259698Sdim  void Wait();
114259698Sdim};
115259698Sdim
116259698Sdim} // end namespace llvm
117259698Sdim
118259698Sdim#endif // LLI_REMOTETARGETEXTERNAL_H
119