blob: 809488c9e390305eceee52af5aea96c9cade7029 [file] [log] [blame]
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +00001//===---- RemoteTargetExternal.cpp - 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// Implementation of the RemoteTargetExternal class which executes JITed code
11// in a separate process from where it was built.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Config/config.h"
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +000016#include "RemoteTarget.h"
17#include "RemoteTargetExternal.h"
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +000018#include "llvm/ADT/StringRef.h"
19#include "llvm/Support/DataTypes.h"
20#include "llvm/Support/Memory.h"
21#include "llvm/Support/Program.h"
22#include "llvm/Support/raw_ostream.h"
23#include <string>
24
25using namespace llvm;
26
27bool RemoteTargetExternal::allocateSpace(size_t Size, unsigned Alignment,
28 uint64_t &Address) {
29 SendAllocateSpace(Alignment, Size);
30 Receive(LLI_AllocationResult, Address);
31 return false;
32}
33
34bool RemoteTargetExternal::loadData(uint64_t Address, const void *Data, size_t Size) {
35 SendLoadSection(Address, Data, (uint32_t)Size, false);
36 Receive(LLI_LoadComplete);
37 return false;
38}
39
40bool RemoteTargetExternal::loadCode(uint64_t Address, const void *Data, size_t Size) {
41 SendLoadSection(Address, Data, (uint32_t)Size, true);
42 Receive(LLI_LoadComplete);
43 return false;
44}
45
46bool RemoteTargetExternal::executeCode(uint64_t Address, int &RetVal) {
47 SendExecute(Address);
48
49 Receive(LLI_ExecutionResult, RetVal);
50 return false;
51}
52
53void RemoteTargetExternal::stop() {
54 SendTerminate();
55 Wait();
56}
57
58void RemoteTargetExternal::SendAllocateSpace(uint32_t Alignment, uint32_t Size) {
59 int rc;
Matt Arsenaultc44a3ff2013-12-05 19:37:36 +000060 (void)rc;
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +000061 uint32_t MsgType = (uint32_t)LLI_AllocateSpace;
62 rc = WriteBytes(&MsgType, 4);
63 assert(rc == 4 && "Error writing message type.");
64
65 uint32_t DataSize = 8;
66 rc = WriteBytes(&DataSize, 4);
67 assert(rc == 4 && "Error writing data size.");
68
69 rc = WriteBytes(&Alignment, 4);
70 assert(rc == 4 && "Error writing alignment data.");
71
72 rc = WriteBytes(&Size, 4);
73 assert(rc == 4 && "Error writing size data.");
74}
75
76void RemoteTargetExternal::SendLoadSection(uint64_t Addr,
77 const void *Data,
78 uint32_t Size,
79 bool IsCode) {
80 int rc;
Matt Arsenault0f5f0152013-12-10 18:55:37 +000081 (void)rc;
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +000082 uint32_t MsgType = IsCode ? LLI_LoadCodeSection : LLI_LoadDataSection;
83 rc = WriteBytes(&MsgType, 4);
84 assert(rc == 4 && "Error writing message type.");
85
86 uint32_t DataSize = Size + 8;
87 rc = WriteBytes(&DataSize, 4);
88 assert(rc == 4 && "Error writing data size.");
89
90 rc = WriteBytes(&Addr, 8);
91 assert(rc == 8 && "Error writing data.");
92
93 rc = WriteBytes(Data, Size);
94 assert(rc == (int)Size && "Error writing data.");
95}
96
97void RemoteTargetExternal::SendExecute(uint64_t Addr) {
98 int rc;
Matt Arsenault0f5f0152013-12-10 18:55:37 +000099 (void)rc;
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +0000100 uint32_t MsgType = (uint32_t)LLI_Execute;
101 rc = WriteBytes(&MsgType, 4);
102 assert(rc == 4 && "Error writing message type.");
103
104 uint32_t DataSize = 8;
105 rc = WriteBytes(&DataSize, 4);
106 assert(rc == 4 && "Error writing data size.");
107
108 rc = WriteBytes(&Addr, 8);
109 assert(rc == 8 && "Error writing data.");
110}
111
112void RemoteTargetExternal::SendTerminate() {
113 int rc;
Matt Arsenault0f5f0152013-12-10 18:55:37 +0000114 (void)rc;
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +0000115 uint32_t MsgType = (uint32_t)LLI_Terminate;
116 rc = WriteBytes(&MsgType, 4);
117 assert(rc == 4 && "Error writing message type.");
118
119 // No data or data size is sent with Terminate
120}
121
122
123void RemoteTargetExternal::Receive(LLIMessageType ExpectedMsgType) {
124 int rc;
Matt Arsenault0f5f0152013-12-10 18:55:37 +0000125 (void)rc;
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +0000126 uint32_t MsgType;
127 rc = ReadBytes(&MsgType, 4);
128 assert(rc == 4 && "Error reading message type.");
Andrew Kaylor004e12f2013-10-02 18:00:34 +0000129 assert(MsgType == (uint32_t)ExpectedMsgType && "Error: received unexpected message type.");
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +0000130
131 uint32_t DataSize;
132 rc = ReadBytes(&DataSize, 4);
133 assert(rc == 4 && "Error reading data size.");
134 assert(DataSize == 0 && "Error: unexpected data size.");
135}
136
137void RemoteTargetExternal::Receive(LLIMessageType ExpectedMsgType, int &Data) {
138 uint64_t Temp;
139 Receive(ExpectedMsgType, Temp);
140 Data = (int)(int64_t)Temp;
141}
142
143void RemoteTargetExternal::Receive(LLIMessageType ExpectedMsgType, uint64_t &Data) {
144 int rc;
Matt Arsenault0f5f0152013-12-10 18:55:37 +0000145 (void)rc;
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +0000146 uint32_t MsgType;
147 rc = ReadBytes(&MsgType, 4);
148 assert(rc == 4 && "Error reading message type.");
Andrew Kaylor004e12f2013-10-02 18:00:34 +0000149 assert(MsgType == (uint32_t)ExpectedMsgType && "Error: received unexpected message type.");
Andrew Kaylorc2ebf3f2013-10-02 17:12:36 +0000150
151 uint32_t DataSize;
152 rc = ReadBytes(&DataSize, 4);
153 assert(rc == 4 && "Error reading data size.");
154 assert(DataSize == 8 && "Error: unexpected data size.");
155
156 rc = ReadBytes(&Data, 8);
157 assert(DataSize == 8 && "Error: unexpected data.");
158}
159
160#ifdef LLVM_ON_UNIX
161#include "Unix/RemoteTargetExternal.inc"
162#endif
163
164#ifdef LLVM_ON_WIN32
165#include "Windows/RemoteTargetExternal.inc"
166#endif