blob: 5c74e6e7a044fd629cd7f83126eb54cc35c82c39 [file] [log] [blame]
Jim Grosbach706f03a2012-09-05 16:50:34 +00001//===- 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"
Andrew Kaylor0ab5c6c2013-10-02 17:12:36 +000016#include "RemoteTargetExternal.h"
Craig Topperdbf54572012-09-15 18:45:38 +000017#include "llvm/ADT/StringRef.h"
18#include "llvm/Support/DataTypes.h"
19#include "llvm/Support/Memory.h"
Jim Grosbach706f03a2012-09-05 16:50:34 +000020#include <stdlib.h>
21#include <string>
Andrew Kaylor0ab5c6c2013-10-02 17:12:36 +000022
Jim Grosbach706f03a2012-09-05 16:50:34 +000023using namespace llvm;
24
Andrew Kaylor0ab5c6c2013-10-02 17:12:36 +000025// Static methods
26RemoteTarget *RemoteTarget::createRemoteTarget() {
27 return new RemoteTarget;
28}
29
30RemoteTarget *RemoteTarget::createExternalRemoteTarget(std::string &ChildName) {
31#ifdef LLVM_ON_UNIX
32 return new RemoteTargetExternal(ChildName);
33#else
34 return 0;
35#endif
36}
37
38bool RemoteTarget::hostSupportsExternalRemoteTarget() {
39#ifdef LLVM_ON_UNIX
40 return true;
41#else
42 return false;
43#endif
44}
45
46
47////////////////////////////////////////////////////////////////////////////////
48// Simulated remote execution
49//
50// This implementation will simply move generated code and data to a new memory
51// location in the current executable and let it run from there.
52////////////////////////////////////////////////////////////////////////////////
53
Jim Grosbach706f03a2012-09-05 16:50:34 +000054bool RemoteTarget::allocateSpace(size_t Size, unsigned Alignment,
55 uint64_t &Address) {
56 sys::MemoryBlock *Prev = Allocations.size() ? &Allocations.back() : NULL;
57 sys::MemoryBlock Mem = sys::Memory::AllocateRWX(Size, Prev, &ErrorMsg);
58 if (Mem.base() == NULL)
59 return true;
60 if ((uintptr_t)Mem.base() % Alignment) {
61 ErrorMsg = "unable to allocate sufficiently aligned memory";
62 return true;
63 }
64 Address = reinterpret_cast<uint64_t>(Mem.base());
65 return false;
66}
67
68bool RemoteTarget::loadData(uint64_t Address, const void *Data, size_t Size) {
69 memcpy ((void*)Address, Data, Size);
Jim Grosbach706f03a2012-09-05 16:50:34 +000070 return false;
71}
72
73bool RemoteTarget::loadCode(uint64_t Address, const void *Data, size_t Size) {
74 memcpy ((void*)Address, Data, Size);
Andrew Kaylor29322842012-10-31 20:37:14 +000075 sys::MemoryBlock Mem((void*)Address, Size);
76 sys::Memory::setExecutable(Mem, &ErrorMsg);
Jim Grosbach706f03a2012-09-05 16:50:34 +000077 return false;
78}
79
80bool RemoteTarget::executeCode(uint64_t Address, int &RetVal) {
81 int (*fn)(void) = (int(*)(void))Address;
82 RetVal = fn();
83 return false;
84}
85
86void RemoteTarget::create() {
87}
88
89void RemoteTarget::stop() {
90 for (unsigned i = 0, e = Allocations.size(); i != e; ++i)
91 sys::Memory::ReleaseRWX(Allocations[i]);
92}