1259698Sdim//===- RemoteMemoryManager.h - LLI MCJIT recording memory manager ------===//
2259698Sdim//
3259698Sdim//                     The LLVM Compiler Infrastructure
4259698Sdim//
5259698Sdim// This file is distributed under the University of Illinois Open Source
6259698Sdim// License. See LICENSE.TXT for details.
7259698Sdim//
8259698Sdim//===----------------------------------------------------------------------===//
9259698Sdim//
10259698Sdim// This memory manager allocates local storage and keeps a record of each
11259698Sdim// allocation. Iterators are provided for all data and code allocations.
12259698Sdim//
13259698Sdim//===----------------------------------------------------------------------===//
14259698Sdim
15259698Sdim#ifndef REMOTEMEMORYMANAGER_H
16259698Sdim#define REMOTEMEMORYMANAGER_H
17259698Sdim
18259698Sdim#include "llvm/ADT/DenseMap.h"
19259698Sdim#include "llvm/ADT/SmallVector.h"
20259698Sdim#include "llvm/ExecutionEngine/JITMemoryManager.h"
21259698Sdim#include "llvm/Support/ErrorHandling.h"
22259698Sdim#include "llvm/Support/Memory.h"
23259698Sdim#include <utility>
24259698Sdim
25259698Sdim#include "RemoteTarget.h"
26259698Sdim
27259698Sdimnamespace llvm {
28259698Sdim
29259698Sdimclass RemoteMemoryManager : public JITMemoryManager {
30259698Sdimpublic:
31259698Sdim  // Notice that this structure takes ownership of the memory allocated.
32259698Sdim  struct Allocation {
33259698Sdim    Allocation() {}
34259698Sdim    Allocation(sys::MemoryBlock mb, unsigned a, bool code)
35259698Sdim      : MB(mb), Alignment(a), IsCode(code) {}
36259698Sdim
37259698Sdim    sys::MemoryBlock  MB;
38259698Sdim    unsigned          Alignment;
39259698Sdim    bool              IsCode;
40259698Sdim  };
41259698Sdim
42259698Sdimprivate:
43259698Sdim  // This vector contains Allocation objects for all sections which we have
44259698Sdim  // allocated.  This vector effectively owns the memory associated with the
45259698Sdim  // allocations.
46259698Sdim  SmallVector<Allocation, 2>  AllocatedSections;
47259698Sdim
48259698Sdim  // This vector contains pointers to Allocation objects for any sections we
49259698Sdim  // have allocated locally but have not yet remapped for the remote target.
50259698Sdim  // When we receive notification of a completed module load, we will map
51259698Sdim  // these sections into the remote target.
52259698Sdim  SmallVector<Allocation, 2>  UnmappedSections;
53259698Sdim
54259698Sdim  // This map tracks the sections we have remapped for the remote target
55259698Sdim  // but have not yet copied to the target.
56259698Sdim  DenseMap<uint64_t, Allocation>  MappedSections;
57259698Sdim
58259698Sdim  // FIXME: This is part of a work around to keep sections near one another
59259698Sdim  // when MCJIT performs relocations after code emission but before
60259698Sdim  // the generated code is moved to the remote target.
61259698Sdim  sys::MemoryBlock Near;
62259698Sdim  sys::MemoryBlock allocateSection(uintptr_t Size);
63259698Sdim
64259698Sdim  RemoteTarget *Target;
65259698Sdim
66259698Sdimpublic:
67259698Sdim  RemoteMemoryManager() : Target(NULL) {}
68259698Sdim  virtual ~RemoteMemoryManager();
69259698Sdim
70259698Sdim  uint8_t *allocateCodeSection(uintptr_t Size, unsigned Alignment,
71259698Sdim                               unsigned SectionID, StringRef SectionName);
72259698Sdim
73259698Sdim  uint8_t *allocateDataSection(uintptr_t Size, unsigned Alignment,
74259698Sdim                               unsigned SectionID, StringRef SectionName,
75259698Sdim                               bool IsReadOnly);
76259698Sdim
77259698Sdim  // For now, remote symbol resolution is not support in lli.  The MCJIT
78259698Sdim  // interface does support this, but clients must provide their own
79259698Sdim  // mechanism for finding remote symbol addresses.  MCJIT will resolve
80259698Sdim  // symbols from Modules it contains.
81259698Sdim  uint64_t getSymbolAddress(const std::string &Name) { return 0; }
82259698Sdim
83259698Sdim  void notifyObjectLoaded(ExecutionEngine *EE, const ObjectImage *Obj);
84259698Sdim
85259698Sdim  bool finalizeMemory(std::string *ErrMsg);
86259698Sdim
87259698Sdim  // For now, remote EH frame registration isn't supported.  Remote symbol
88259698Sdim  // resolution is a prerequisite to supporting remote EH frame registration.
89259698Sdim  void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {}
90259698Sdim  void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) {}
91259698Sdim
92259698Sdim  // This is a non-interface function used by lli
93259698Sdim  void setRemoteTarget(RemoteTarget *T) { Target = T; }
94259698Sdim
95259698Sdim  // The following obsolete JITMemoryManager calls are stubbed out for
96259698Sdim  // this model.
97259698Sdim  void setMemoryWritable();
98259698Sdim  void setMemoryExecutable();
99259698Sdim  void setPoisonMemory(bool poison);
100259698Sdim  void AllocateGOT();
101259698Sdim  uint8_t *getGOTBase() const;
102259698Sdim  uint8_t *startFunctionBody(const Function *F, uintptr_t &ActualSize);
103259698Sdim  uint8_t *allocateStub(const GlobalValue* F, unsigned StubSize,
104259698Sdim                        unsigned Alignment);
105259698Sdim  void endFunctionBody(const Function *F, uint8_t *FunctionStart,
106259698Sdim                       uint8_t *FunctionEnd);
107259698Sdim  uint8_t *allocateSpace(intptr_t Size, unsigned Alignment);
108259698Sdim  uint8_t *allocateGlobal(uintptr_t Size, unsigned Alignment);
109259698Sdim  void deallocateFunctionBody(void *Body);
110259698Sdim};
111259698Sdim
112259698Sdim} // end namespace llvm
113259698Sdim
114259698Sdim#endif
115