561055f43bd0db4b3b8ab4a872df89aeb16881f3
[oota-llvm.git] / tools / lli / RemoteTarget.cpp
1 //===- RemoteTarget.cpp - LLVM Remote 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 RemoteTarget class which executes JITed code in a
11 // separate address range from where it was built.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "RemoteTarget.h"
16 #include "RemoteTargetExternal.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/DataTypes.h"
19 #include "llvm/Support/Memory.h"
20 #include <stdlib.h>
21 #include <string>
22
23 using namespace llvm;
24
25 ////////////////////////////////////////////////////////////////////////////////
26 // Simulated remote execution
27 //
28 // This implementation will simply move generated code and data to a new memory
29 // location in the current executable and let it run from there.
30 ////////////////////////////////////////////////////////////////////////////////
31
32 bool RemoteTarget::allocateSpace(size_t Size, unsigned Alignment,
33                                  uint64_t &Address) {
34   sys::MemoryBlock *Prev = Allocations.size() ? &Allocations.back() : NULL;
35   sys::MemoryBlock Mem = sys::Memory::AllocateRWX(Size, Prev, &ErrorMsg);
36   if (Mem.base() == NULL)
37     return false;
38   if ((uintptr_t)Mem.base() % Alignment) {
39     ErrorMsg = "unable to allocate sufficiently aligned memory";
40     return false;
41   }
42   Address = reinterpret_cast<uint64_t>(Mem.base());
43   Allocations.push_back(Mem);
44   return true;
45 }
46
47 bool RemoteTarget::loadData(uint64_t Address, const void *Data, size_t Size) {
48   memcpy ((void*)Address, Data, Size);
49   return true;
50 }
51
52 bool RemoteTarget::loadCode(uint64_t Address, const void *Data, size_t Size) {
53   memcpy ((void*)Address, Data, Size);
54   sys::MemoryBlock Mem((void*)Address, Size);
55   sys::Memory::setExecutable(Mem, &ErrorMsg);
56   return true;
57 }
58
59 bool RemoteTarget::executeCode(uint64_t Address, int &RetVal) {
60   int (*fn)(void) = (int(*)(void))Address;
61   RetVal = fn();
62   return true;
63 }
64
65 bool RemoteTarget::create() {
66   return true;
67 }
68
69 void RemoteTarget::stop() {
70   for (unsigned i = 0, e = Allocations.size(); i != e; ++i)
71     sys::Memory::ReleaseRWX(Allocations[i]);
72 }