Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 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" |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 16 | #include "RemoteTarget.h" |
| 17 | #include "RemoteTargetExternal.h" |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 18 | #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 | |
| 25 | using namespace llvm; |
| 26 | |
| 27 | bool 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 | |
| 34 | bool 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 | |
| 40 | bool 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 | |
| 46 | bool RemoteTargetExternal::executeCode(uint64_t Address, int &RetVal) { |
| 47 | SendExecute(Address); |
| 48 | |
| 49 | Receive(LLI_ExecutionResult, RetVal); |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | void RemoteTargetExternal::stop() { |
| 54 | SendTerminate(); |
| 55 | Wait(); |
| 56 | } |
| 57 | |
| 58 | void RemoteTargetExternal::SendAllocateSpace(uint32_t Alignment, uint32_t Size) { |
| 59 | int rc; |
Matt Arsenault | c44a3ff | 2013-12-05 19:37:36 +0000 | [diff] [blame] | 60 | (void)rc; |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 61 | 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 | |
| 76 | void RemoteTargetExternal::SendLoadSection(uint64_t Addr, |
| 77 | const void *Data, |
| 78 | uint32_t Size, |
| 79 | bool IsCode) { |
| 80 | int rc; |
Matt Arsenault | 0f5f015 | 2013-12-10 18:55:37 +0000 | [diff] [blame] | 81 | (void)rc; |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 82 | 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 | |
| 97 | void RemoteTargetExternal::SendExecute(uint64_t Addr) { |
| 98 | int rc; |
Matt Arsenault | 0f5f015 | 2013-12-10 18:55:37 +0000 | [diff] [blame] | 99 | (void)rc; |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 100 | 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 | |
| 112 | void RemoteTargetExternal::SendTerminate() { |
| 113 | int rc; |
Matt Arsenault | 0f5f015 | 2013-12-10 18:55:37 +0000 | [diff] [blame] | 114 | (void)rc; |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 115 | 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 | |
| 123 | void RemoteTargetExternal::Receive(LLIMessageType ExpectedMsgType) { |
| 124 | int rc; |
Matt Arsenault | 0f5f015 | 2013-12-10 18:55:37 +0000 | [diff] [blame] | 125 | (void)rc; |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 126 | uint32_t MsgType; |
| 127 | rc = ReadBytes(&MsgType, 4); |
| 128 | assert(rc == 4 && "Error reading message type."); |
Andrew Kaylor | 004e12f | 2013-10-02 18:00:34 +0000 | [diff] [blame] | 129 | assert(MsgType == (uint32_t)ExpectedMsgType && "Error: received unexpected message type."); |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 130 | |
| 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 | |
| 137 | void RemoteTargetExternal::Receive(LLIMessageType ExpectedMsgType, int &Data) { |
| 138 | uint64_t Temp; |
| 139 | Receive(ExpectedMsgType, Temp); |
| 140 | Data = (int)(int64_t)Temp; |
| 141 | } |
| 142 | |
| 143 | void RemoteTargetExternal::Receive(LLIMessageType ExpectedMsgType, uint64_t &Data) { |
| 144 | int rc; |
Matt Arsenault | 0f5f015 | 2013-12-10 18:55:37 +0000 | [diff] [blame] | 145 | (void)rc; |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 146 | uint32_t MsgType; |
| 147 | rc = ReadBytes(&MsgType, 4); |
| 148 | assert(rc == 4 && "Error reading message type."); |
Andrew Kaylor | 004e12f | 2013-10-02 18:00:34 +0000 | [diff] [blame] | 149 | assert(MsgType == (uint32_t)ExpectedMsgType && "Error: received unexpected message type."); |
Andrew Kaylor | c2ebf3f | 2013-10-02 17:12:36 +0000 | [diff] [blame] | 150 | |
| 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 |