Fix gcc warnings.
[oota-llvm.git] / tools / lli / RemoteTargetExternal.cpp
1 //===---- 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"
16
17 #include "RemoteTarget.h"
18 #include "RemoteTargetExternal.h"
19
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"
25 #include <string>
26
27 using namespace llvm;
28
29 bool RemoteTargetExternal::allocateSpace(size_t Size, unsigned Alignment,
30                                  uint64_t &Address) {
31   SendAllocateSpace(Alignment, Size);
32   Receive(LLI_AllocationResult, Address);
33   return false;
34 }
35
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);
39   return false;
40 }
41
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);
45   return false;
46 }
47
48 bool RemoteTargetExternal::executeCode(uint64_t Address, int &RetVal) {
49   SendExecute(Address);
50
51   Receive(LLI_ExecutionResult, RetVal);
52   return false;
53 }
54
55 void RemoteTargetExternal::stop() {
56   SendTerminate();
57   Wait();
58 }
59
60 void RemoteTargetExternal::SendAllocateSpace(uint32_t Alignment, uint32_t Size) {
61   int rc;
62   (void)rc;
63   uint32_t MsgType = (uint32_t)LLI_AllocateSpace;
64   rc = WriteBytes(&MsgType, 4);
65   assert(rc == 4 && "Error writing message type.");
66
67   uint32_t DataSize = 8;
68   rc = WriteBytes(&DataSize, 4);
69   assert(rc == 4 && "Error writing data size.");
70
71   rc = WriteBytes(&Alignment, 4);
72   assert(rc == 4 && "Error writing alignment data.");
73
74   rc = WriteBytes(&Size, 4);
75   assert(rc == 4 && "Error writing size data.");
76 }
77
78 void RemoteTargetExternal::SendLoadSection(uint64_t Addr,
79                                        const void *Data,
80                                        uint32_t Size,
81                                        bool IsCode) {
82   int rc;
83   (void)rc;
84   uint32_t MsgType = IsCode ? LLI_LoadCodeSection : LLI_LoadDataSection;
85   rc = WriteBytes(&MsgType, 4);
86   assert(rc == 4 && "Error writing message type.");
87
88   uint32_t DataSize = Size + 8;
89   rc = WriteBytes(&DataSize, 4);
90   assert(rc == 4 && "Error writing data size.");
91
92   rc = WriteBytes(&Addr, 8);
93   assert(rc == 8 && "Error writing data.");
94
95   rc = WriteBytes(Data, Size);
96   assert(rc == (int)Size && "Error writing data.");
97 }
98
99 void RemoteTargetExternal::SendExecute(uint64_t Addr) {
100   int rc;
101   (void)rc;
102   uint32_t MsgType = (uint32_t)LLI_Execute;
103   rc = WriteBytes(&MsgType, 4);
104   assert(rc == 4 && "Error writing message type.");
105
106   uint32_t DataSize = 8;
107   rc = WriteBytes(&DataSize, 4);
108   assert(rc == 4 && "Error writing data size.");
109
110   rc = WriteBytes(&Addr, 8);
111   assert(rc == 8 && "Error writing data.");
112 }
113
114 void RemoteTargetExternal::SendTerminate() {
115   int rc;
116   (void)rc;
117   uint32_t MsgType = (uint32_t)LLI_Terminate;
118   rc = WriteBytes(&MsgType, 4);
119   assert(rc == 4 && "Error writing message type.");
120
121   // No data or data size is sent with Terminate
122 }
123
124
125 void RemoteTargetExternal::Receive(LLIMessageType ExpectedMsgType) {
126   int rc;
127   (void)rc;
128   uint32_t MsgType;
129   rc = ReadBytes(&MsgType, 4);
130   assert(rc == 4 && "Error reading message type.");
131   assert(MsgType == (uint32_t)ExpectedMsgType && "Error: received unexpected message type.");
132
133   uint32_t DataSize;
134   rc = ReadBytes(&DataSize, 4);
135   assert(rc == 4 && "Error reading data size.");
136   assert(DataSize == 0 && "Error: unexpected data size.");
137 }
138
139 void RemoteTargetExternal::Receive(LLIMessageType ExpectedMsgType, int &Data) {
140   uint64_t Temp;
141   Receive(ExpectedMsgType, Temp);
142   Data = (int)(int64_t)Temp;
143 }
144
145 void RemoteTargetExternal::Receive(LLIMessageType ExpectedMsgType, uint64_t &Data) {
146   int rc;
147   (void)rc;
148   uint32_t MsgType;
149   rc = ReadBytes(&MsgType, 4);
150   assert(rc == 4 && "Error reading message type.");
151   assert(MsgType == (uint32_t)ExpectedMsgType && "Error: received unexpected message type.");
152
153   uint32_t DataSize;
154   rc = ReadBytes(&DataSize, 4);
155   assert(rc == 4 && "Error reading data size.");
156   assert(DataSize == 8 && "Error: unexpected data size.");
157
158   rc = ReadBytes(&Data, 8);
159   assert(DataSize == 8 && "Error: unexpected data.");
160 }
161
162 #ifdef LLVM_ON_UNIX
163 #include "Unix/RemoteTargetExternal.inc"
164 #endif
165
166 #ifdef LLVM_ON_WIN32
167 #include "Windows/RemoteTargetExternal.inc"
168 #endif