Interim build fix for Makefiles
[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 #ifndef LLI_BUILDING_CHILD
26
27 // Static methods
28 RemoteTarget *RemoteTarget::createRemoteTarget() {
29   return new RemoteTarget;
30 }
31
32 RemoteTarget *RemoteTarget::createExternalRemoteTarget(std::string &ChildName) {
33 #ifdef LLVM_ON_UNIX
34   return new RemoteTargetExternal(ChildName);
35 #else
36   return 0;
37 #endif
38 }
39
40 bool RemoteTarget::hostSupportsExternalRemoteTarget() {
41 #ifdef LLVM_ON_UNIX
42   return true;
43 #else
44   return false;
45 #endif
46 }
47
48 #endif
49
50 ////////////////////////////////////////////////////////////////////////////////
51 // Simulated remote execution
52 //
53 // This implementation will simply move generated code and data to a new memory
54 // location in the current executable and let it run from there.
55 ////////////////////////////////////////////////////////////////////////////////
56
57 bool RemoteTarget::allocateSpace(size_t Size, unsigned Alignment,
58                                  uint64_t &Address) {
59   sys::MemoryBlock *Prev = Allocations.size() ? &Allocations.back() : NULL;
60   sys::MemoryBlock Mem = sys::Memory::AllocateRWX(Size, Prev, &ErrorMsg);
61   if (Mem.base() == NULL)
62     return false;
63   if ((uintptr_t)Mem.base() % Alignment) {
64     ErrorMsg = "unable to allocate sufficiently aligned memory";
65     return false;
66   }
67   Address = reinterpret_cast<uint64_t>(Mem.base());
68   Allocations.push_back(Mem);
69   return true;
70 }
71
72 bool RemoteTarget::loadData(uint64_t Address, const void *Data, size_t Size) {
73   memcpy ((void*)Address, Data, Size);
74   return true;
75 }
76
77 bool RemoteTarget::loadCode(uint64_t Address, const void *Data, size_t Size) {
78   memcpy ((void*)Address, Data, Size);
79   sys::MemoryBlock Mem((void*)Address, Size);
80   sys::Memory::setExecutable(Mem, &ErrorMsg);
81   return true;
82 }
83
84 bool RemoteTarget::executeCode(uint64_t Address, int &RetVal) {
85   int (*fn)(void) = (int(*)(void))Address;
86   RetVal = fn();
87   return true;
88 }
89
90 bool RemoteTarget::create() {
91   return true;
92 }
93
94 void RemoteTarget::stop() {
95   for (unsigned i = 0, e = Allocations.size(); i != e; ++i)
96     sys::Memory::ReleaseRWX(Allocations[i]);
97 }