1 //===---- RemoteTargetExternal.cpp - LLVM out-of-process JIT execution ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Implementation of the RemoteTargetExternal class which executes JITed code
11 // in a separate process from where it was built.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Config/config.h"
17 #include "RemoteTarget.h"
18 #include "RemoteTargetExternal.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/Support/DataTypes.h"
22 #include "llvm/Support/Memory.h"
23 #include "llvm/Support/Program.h"
24 #include "llvm/Support/raw_ostream.h"
29 bool RemoteTargetExternal::allocateSpace(size_t Size, unsigned Alignment,
31 SendAllocateSpace(Alignment, Size);
32 Receive(LLI_AllocationResult, Address);
36 bool RemoteTargetExternal::loadData(uint64_t Address, const void *Data, size_t Size) {
37 SendLoadSection(Address, Data, (uint32_t)Size, false);
38 Receive(LLI_LoadComplete);
42 bool RemoteTargetExternal::loadCode(uint64_t Address, const void *Data, size_t Size) {
43 SendLoadSection(Address, Data, (uint32_t)Size, true);
44 Receive(LLI_LoadComplete);
48 bool RemoteTargetExternal::executeCode(uint64_t Address, int &RetVal) {
51 Receive(LLI_ExecutionResult, RetVal);
55 void RemoteTargetExternal::stop() {
60 void RemoteTargetExternal::SendAllocateSpace(uint32_t Alignment, uint32_t Size) {
63 uint32_t MsgType = (uint32_t)LLI_AllocateSpace;
64 rc = WriteBytes(&MsgType, 4);
65 assert(rc == 4 && "Error writing message type.");
67 uint32_t DataSize = 8;
68 rc = WriteBytes(&DataSize, 4);
69 assert(rc == 4 && "Error writing data size.");
71 rc = WriteBytes(&Alignment, 4);
72 assert(rc == 4 && "Error writing alignment data.");
74 rc = WriteBytes(&Size, 4);
75 assert(rc == 4 && "Error writing size data.");
78 void RemoteTargetExternal::SendLoadSection(uint64_t Addr,
84 uint32_t MsgType = IsCode ? LLI_LoadCodeSection : LLI_LoadDataSection;
85 rc = WriteBytes(&MsgType, 4);
86 assert(rc == 4 && "Error writing message type.");
88 uint32_t DataSize = Size + 8;
89 rc = WriteBytes(&DataSize, 4);
90 assert(rc == 4 && "Error writing data size.");
92 rc = WriteBytes(&Addr, 8);
93 assert(rc == 8 && "Error writing data.");
95 rc = WriteBytes(Data, Size);
96 assert(rc == (int)Size && "Error writing data.");
99 void RemoteTargetExternal::SendExecute(uint64_t Addr) {
102 uint32_t MsgType = (uint32_t)LLI_Execute;
103 rc = WriteBytes(&MsgType, 4);
104 assert(rc == 4 && "Error writing message type.");
106 uint32_t DataSize = 8;
107 rc = WriteBytes(&DataSize, 4);
108 assert(rc == 4 && "Error writing data size.");
110 rc = WriteBytes(&Addr, 8);
111 assert(rc == 8 && "Error writing data.");
114 void RemoteTargetExternal::SendTerminate() {
117 uint32_t MsgType = (uint32_t)LLI_Terminate;
118 rc = WriteBytes(&MsgType, 4);
119 assert(rc == 4 && "Error writing message type.");
121 // No data or data size is sent with Terminate
125 void RemoteTargetExternal::Receive(LLIMessageType ExpectedMsgType) {
129 rc = ReadBytes(&MsgType, 4);
130 assert(rc == 4 && "Error reading message type.");
131 assert(MsgType == (uint32_t)ExpectedMsgType && "Error: received unexpected message type.");
134 rc = ReadBytes(&DataSize, 4);
135 assert(rc == 4 && "Error reading data size.");
136 assert(DataSize == 0 && "Error: unexpected data size.");
139 void RemoteTargetExternal::Receive(LLIMessageType ExpectedMsgType, int &Data) {
141 Receive(ExpectedMsgType, Temp);
142 Data = (int)(int64_t)Temp;
145 void RemoteTargetExternal::Receive(LLIMessageType ExpectedMsgType, uint64_t &Data) {
149 rc = ReadBytes(&MsgType, 4);
150 assert(rc == 4 && "Error reading message type.");
151 assert(MsgType == (uint32_t)ExpectedMsgType && "Error: received unexpected message type.");
154 rc = ReadBytes(&DataSize, 4);
155 assert(rc == 4 && "Error reading data size.");
156 assert(DataSize == 8 && "Error: unexpected data size.");
158 rc = ReadBytes(&Data, 8);
159 assert(DataSize == 8 && "Error: unexpected data.");
163 #include "Unix/RemoteTargetExternal.inc"
167 #include "Windows/RemoteTargetExternal.inc"