Skip to content

Commit b8e5f91

Browse files
committed
[ORC] Flesh out ExecutorAddress, rename CommonOrcRuntimeTypes header.
Renames CommonOrcRuntimeTypes.h to ExecutorAddress.h and moves ExecutorAddress into the 'orc' namespace (rather than orc::shared). Also makes ExecutorAddress a class, adds an ExecutorAddrDiff type and some arithmetic operations on the pair (subtracting two addresses yields an addrdiff, adding an addrdiff and an address yields an address).
1 parent 3822e3d commit b8e5f91

File tree

4 files changed

+245
-109
lines changed

4 files changed

+245
-109
lines changed

llvm/include/llvm/ExecutionEngine/Orc/MachOPlatform.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "llvm/ExecutionEngine/Orc/Core.h"
1818
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
1919
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h"
20-
#include "llvm/ExecutionEngine/Orc/Shared/CommonOrcRuntimeTypes.h"
20+
#include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
2121

2222
#include <future>
2323
#include <thread>
@@ -32,29 +32,29 @@ bool objCRegistrationEnabled();
3232

3333
class MachOJITDylibInitializers {
3434
public:
35-
using RawPointerSectionList = std::vector<shared::ExecutorAddressRange>;
35+
using RawPointerSectionList = std::vector<ExecutorAddressRange>;
3636

3737
void setObjCImageInfoAddr(JITTargetAddress ObjCImageInfoAddr) {
3838
this->ObjCImageInfoAddr = ObjCImageInfoAddr;
3939
}
4040

41-
void addModInitsSection(shared::ExecutorAddressRange ModInit) {
41+
void addModInitsSection(ExecutorAddressRange ModInit) {
4242
ModInitSections.push_back(std::move(ModInit));
4343
}
4444

4545
const RawPointerSectionList &getModInitsSections() const {
4646
return ModInitSections;
4747
}
4848

49-
void addObjCSelRefsSection(shared::ExecutorAddressRange ObjCSelRefs) {
49+
void addObjCSelRefsSection(ExecutorAddressRange ObjCSelRefs) {
5050
ObjCSelRefsSections.push_back(std::move(ObjCSelRefs));
5151
}
5252

5353
const RawPointerSectionList &getObjCSelRefsSections() const {
5454
return ObjCSelRefsSections;
5555
}
5656

57-
void addObjCClassListSection(shared::ExecutorAddressRange ObjCClassList) {
57+
void addObjCClassListSection(ExecutorAddressRange ObjCClassList) {
5858
ObjCClassListSections.push_back(std::move(ObjCClassList));
5959
}
6060

@@ -145,9 +145,9 @@ class MachOPlatform : public Platform {
145145
};
146146

147147
void registerInitInfo(JITDylib &JD, JITTargetAddress ObjCImageInfoAddr,
148-
shared::ExecutorAddressRange ModInits,
149-
shared::ExecutorAddressRange ObjCSelRefs,
150-
shared::ExecutorAddressRange ObjCClassList);
148+
ExecutorAddressRange ModInits,
149+
ExecutorAddressRange ObjCSelRefs,
150+
ExecutorAddressRange ObjCClassList);
151151

152152
ExecutionSession &ES;
153153
ObjectLinkingLayer &ObjLinkingLayer;

llvm/include/llvm/ExecutionEngine/Orc/Shared/CommonOrcRuntimeTypes.h

Lines changed: 0 additions & 68 deletions
This file was deleted.
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
//===------ ExecutorAddress.h - Executing process address -------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Represents an address in the executing program.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_EXECUTIONENGINE_ORC_SHARED_EXECUTORADDRESS_H
14+
#define LLVM_EXECUTIONENGINE_ORC_SHARED_EXECUTORADDRESS_H
15+
16+
#include "llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h"
17+
18+
#include <cassert>
19+
#include <type_traits>
20+
21+
namespace llvm {
22+
namespace orc {
23+
24+
/// Represents the difference between two addresses in the executor process.
25+
class ExecutorAddrDiff {
26+
public:
27+
ExecutorAddrDiff() = default;
28+
explicit ExecutorAddrDiff(uint64_t Value) : Value(Value) {}
29+
30+
uint64_t getValue() const { return Value; }
31+
32+
private:
33+
int64_t Value = 0;
34+
};
35+
36+
/// Represents an address in the executor process.
37+
class ExecutorAddress {
38+
public:
39+
ExecutorAddress() = default;
40+
explicit ExecutorAddress(uint64_t Addr) : Addr(Addr) {}
41+
42+
/// Create an ExecutorAddress from the given pointer.
43+
/// Warning: This should only be used when JITing in-process.
44+
template <typename T> static ExecutorAddress fromPtr(T *Value) {
45+
return ExecutorAddress(
46+
static_cast<uint64_t>(reinterpret_cast<uintptr_t>(Value)));
47+
}
48+
49+
/// Cast this ExecutorAddress to a pointer of the given type.
50+
/// Warning: This should only be esude when JITing in-process.
51+
template <typename T> T toPtr() const {
52+
static_assert(std::is_pointer<T>::value, "T must be a pointer type");
53+
uintptr_t IntPtr = static_cast<uintptr_t>(Addr);
54+
assert(IntPtr == Addr &&
55+
"JITTargetAddress value out of range for uintptr_t");
56+
return reinterpret_cast<T>(IntPtr);
57+
}
58+
59+
uint64_t getValue() const { return Addr; }
60+
void setValue(uint64_t Addr) { this->Addr = Addr; }
61+
bool isNull() const { return Addr == 0; }
62+
63+
explicit operator bool() const { return Addr != 0; }
64+
65+
friend bool operator==(const ExecutorAddress &LHS,
66+
const ExecutorAddress &RHS) {
67+
return LHS.Addr == RHS.Addr;
68+
}
69+
70+
friend bool operator!=(const ExecutorAddress &LHS,
71+
const ExecutorAddress &RHS) {
72+
return LHS.Addr != RHS.Addr;
73+
}
74+
75+
friend bool operator<(const ExecutorAddress &LHS,
76+
const ExecutorAddress &RHS) {
77+
return LHS.Addr < RHS.Addr;
78+
}
79+
80+
friend bool operator<=(const ExecutorAddress &LHS,
81+
const ExecutorAddress &RHS) {
82+
return LHS.Addr <= RHS.Addr;
83+
}
84+
85+
friend bool operator>(const ExecutorAddress &LHS,
86+
const ExecutorAddress &RHS) {
87+
return LHS.Addr > RHS.Addr;
88+
}
89+
90+
friend bool operator>=(const ExecutorAddress &LHS,
91+
const ExecutorAddress &RHS) {
92+
return LHS.Addr >= RHS.Addr;
93+
}
94+
95+
ExecutorAddress &operator++() {
96+
++Addr;
97+
return *this;
98+
}
99+
ExecutorAddress &operator--() {
100+
--Addr;
101+
return *this;
102+
}
103+
ExecutorAddress operator++(int) { return ExecutorAddress(Addr++); }
104+
ExecutorAddress operator--(int) { return ExecutorAddress(Addr++); }
105+
106+
ExecutorAddress &operator+=(const ExecutorAddrDiff Delta) {
107+
Addr += Delta.getValue();
108+
return *this;
109+
}
110+
111+
ExecutorAddress &operator-=(const ExecutorAddrDiff Delta) {
112+
Addr -= Delta.getValue();
113+
return *this;
114+
}
115+
116+
private:
117+
uint64_t Addr = 0;
118+
};
119+
120+
/// Subtracting two addresses yields an offset.
121+
inline ExecutorAddrDiff operator-(const ExecutorAddress &LHS,
122+
const ExecutorAddress &RHS) {
123+
return ExecutorAddrDiff(LHS.getValue() - RHS.getValue());
124+
}
125+
126+
/// Adding an offset and an address yields an address.
127+
inline ExecutorAddress operator+(const ExecutorAddress &LHS,
128+
const ExecutorAddrDiff &RHS) {
129+
return ExecutorAddress(LHS.getValue() + RHS.getValue());
130+
}
131+
132+
/// Adding an address and an offset yields an address.
133+
inline ExecutorAddress operator+(const ExecutorAddrDiff &LHS,
134+
const ExecutorAddress &RHS) {
135+
return ExecutorAddress(LHS.getValue() + RHS.getValue());
136+
}
137+
138+
/// Represents an address range in the exceutor process.
139+
struct ExecutorAddressRange {
140+
ExecutorAddressRange() = default;
141+
ExecutorAddressRange(ExecutorAddress StartAddress, ExecutorAddress EndAddress)
142+
: StartAddress(StartAddress), EndAddress(EndAddress) {}
143+
144+
bool empty() const { return StartAddress == EndAddress; }
145+
ExecutorAddrDiff size() const { return EndAddress - StartAddress; }
146+
147+
ExecutorAddress StartAddress;
148+
ExecutorAddress EndAddress;
149+
};
150+
151+
namespace shared {
152+
153+
/// SPS serializatior for ExecutorAddress.
154+
template <> class SPSSerializationTraits<SPSExecutorAddress, ExecutorAddress> {
155+
public:
156+
static size_t size(const ExecutorAddress &EA) {
157+
return SPSArgList<uint64_t>::size(EA.getValue());
158+
}
159+
160+
static bool serialize(SPSOutputBuffer &BOB, const ExecutorAddress &EA) {
161+
return SPSArgList<uint64_t>::serialize(BOB, EA.getValue());
162+
}
163+
164+
static bool deserialize(SPSInputBuffer &BIB, ExecutorAddress &EA) {
165+
uint64_t Tmp;
166+
if (!SPSArgList<uint64_t>::deserialize(BIB, Tmp))
167+
return false;
168+
EA = ExecutorAddress(Tmp);
169+
return true;
170+
}
171+
};
172+
173+
using SPSExecutorAddressRange =
174+
SPSTuple<SPSExecutorAddress, SPSExecutorAddress>;
175+
176+
/// Serialization traits for address ranges.
177+
template <>
178+
class SPSSerializationTraits<SPSExecutorAddressRange, ExecutorAddressRange> {
179+
public:
180+
static size_t size(const ExecutorAddressRange &Value) {
181+
return SPSArgList<SPSExecutorAddress, SPSExecutorAddress>::size(
182+
Value.StartAddress, Value.EndAddress);
183+
}
184+
185+
static bool serialize(SPSOutputBuffer &BOB,
186+
const ExecutorAddressRange &Value) {
187+
return SPSArgList<SPSExecutorAddress, SPSExecutorAddress>::serialize(
188+
BOB, Value.StartAddress, Value.EndAddress);
189+
}
190+
191+
static bool deserialize(SPSInputBuffer &BIB, ExecutorAddressRange &Value) {
192+
return SPSArgList<SPSExecutorAddress, SPSExecutorAddress>::deserialize(
193+
BIB, Value.StartAddress, Value.EndAddress);
194+
}
195+
};
196+
197+
using SPSExecutorAddressRangeSequence = SPSSequence<SPSExecutorAddressRange>;
198+
199+
} // End namespace shared.
200+
} // End namespace orc.
201+
} // End namespace llvm.
202+
203+
#endif // LLVM_EXECUTIONENGINE_ORC_SHARED_EXECUTORADDRESS_H

0 commit comments

Comments
 (0)