-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathNativeCodeData.h
294 lines (256 loc) · 9.43 KB
/
NativeCodeData.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#pragma once
#define NativeCodeDataNew(alloc, T, ...) AllocatorNew(NativeCodeData::AllocatorT<T>, alloc, T, __VA_ARGS__)
#define NativeCodeDataNewZ(alloc, T, ...) AllocatorNewZ(NativeCodeData::AllocatorT<T>, alloc, T, __VA_ARGS__)
#define NativeCodeDataNewArray(alloc, T, count) AllocatorNewArray(NativeCodeData::AllocatorT<NativeCodeData::Array<T>>, alloc, T, count)
#define NativeCodeDataNewArrayZ(alloc, T, count) AllocatorNewArrayZ(NativeCodeData::AllocatorT<NativeCodeData::Array<T>>, alloc, T, count)
#define NativeCodeDataNewPlusZ(size, alloc, T, ...) AllocatorNewPlusZ(NativeCodeData::AllocatorT<T>, alloc, size, T, __VA_ARGS__)
#define NativeCodeDataNewNoFixup(alloc, T, ...) AllocatorNew(NativeCodeData::AllocatorNoFixup<T>, alloc, T, __VA_ARGS__)
#define NativeCodeDataNewZNoFixup(alloc, T, ...) AllocatorNewZ(NativeCodeData::AllocatorNoFixup<T>, alloc, T, __VA_ARGS__)
#define NativeCodeDataNewArrayNoFixup(alloc, T, count) AllocatorNewArray(NativeCodeData::AllocatorNoFixup<NativeCodeData::Array<T>>, alloc, T, count)
#define NativeCodeDataNewArrayZNoFixup(alloc, T, count) AllocatorNewArrayZ(NativeCodeData::AllocatorNoFixup<NativeCodeData::Array<T>>, alloc, T, count)
#define NativeCodeDataNewPlusZNoFixup(size, alloc, T, ...) AllocatorNewPlusZ(NativeCodeData::AllocatorNoFixup<T>, alloc, size, T, __VA_ARGS__)
#define FixupNativeDataPointer(field, chunkList) NativeCodeData::AddFixupEntry(this->field, &this->field, this, chunkList)
class NativeCodeData
{
public:
struct DataChunk
{
unsigned int len;
unsigned int allocIndex;
unsigned int offset; // offset to the aggregated buffer
#if DBG
const char* dataType;
#endif
// todo: use union?
void(*fixupFunc)(void* _this, NativeCodeData::DataChunk*);
NativeDataFixupEntry *fixupList;
DataChunk * next;
char data[0];
};
struct DataChunkNoFixup
{
unsigned int len; // only used for memory management purposes.
DataChunkNoFixup * next;
char data[0];
};
static DataChunk* GetDataChunk(void* data)
{
Assert(JITManager::GetJITManager()->IsJITServer());
return (NativeCodeData::DataChunk*)((char*)data - offsetof(NativeCodeData::DataChunk, data));
}
static char16* GetDataDescription(void* data, JitArenaAllocator * alloc);
static unsigned int GetDataTotalOffset(void* data)
{
Assert(JITManager::GetJITManager()->IsJITServer());
return GetDataChunk(data)->offset;
}
NativeCodeData(DataChunk * chunkList);
union
{
DataChunk * chunkList;
DataChunkNoFixup * noFixupChunkList;
};
#ifdef PERF_COUNTERS
size_t size;
#endif
public:
static void VerifyExistFixupEntry(void* targetAddr, void* addrToFixup, void* startAddress);
static void AddFixupEntry(void* targetAddr, void* addrToFixup, void* startAddress, DataChunk * chunkList);
static void AddFixupEntry(void* targetAddr, void* targetStartAddr, void* addrToFixup, void* startAddress, DataChunk * chunkList);
static void AddFixupEntryForPointerArray(void* startAddress, DataChunk * chunkList);
template<class DataChunkT>
static void DeleteChunkList(DataChunkT * chunkList);
public:
class Allocator
{
public:
static const bool FakeZeroLengthArray = false;
Allocator();
~Allocator();
char * Alloc(DECLSPEC_GUARD_OVERFLOW size_t requestedBytes);
char * AllocZero(DECLSPEC_GUARD_OVERFLOW size_t requestedBytes);
char * AllocLeaf(DECLSPEC_GUARD_OVERFLOW size_t requestedBytes);
NativeCodeData * Finalize();
void Free(void * buffer, size_t byteSize);
union
{
DataChunk * chunkList;
DataChunkNoFixup* noFixupChunkList;
};
DataChunk * lastChunkList; // used to maintain the allocation order in the list
unsigned int totalSize;
unsigned int allocCount;
#ifdef TRACK_ALLOC
// Doesn't support tracking information, dummy implementation
Allocator * TrackAllocInfo(TrackAllocData const& data) { return this; }
void ClearTrackAllocInfo(TrackAllocData* data = NULL) {}
#endif
protected:
bool isOOPJIT;
private:
#if DBG
bool finalized;
#endif
#ifdef PERF_COUNTERS
size_t size;
#endif
};
template<typename T>
class Array
{
public:
void Fixup(NativeCodeData::DataChunk* chunkList)
{
int count = NativeCodeData::GetDataChunk(this)->len / sizeof(T);
while (count-- > 0)
{
(((T*)this) + count)->Fixup(chunkList);
}
}
};
template<typename T>
class AllocatorNoFixup : public Allocator
{
public:
char * Alloc(size_t requestedBytes)
{
char* dataBlock = __super::Alloc(requestedBytes);
#if DBG
if (JITManager::GetJITManager()->IsJITServer())
{
DataChunk* chunk = NativeCodeData::GetDataChunk(dataBlock);
chunk->dataType = typeid(T).name();
if (PHASE_TRACE1(Js::NativeCodeDataPhase))
{
Output::Print(_u("NativeCodeData AllocNoFix: chunk: %p, data: %p, index: %d, len: %x, totalOffset: %x, type: %S\n"),
chunk, (void*)dataBlock, chunk->allocIndex, chunk->len, chunk->offset, chunk->dataType);
}
}
#endif
return dataBlock;
}
char * AllocZero(size_t requestedBytes)
{
char* dataBlock = __super::AllocZero(requestedBytes);
#if DBG
if (JITManager::GetJITManager()->IsJITServer())
{
DataChunk* chunk = NativeCodeData::GetDataChunk(dataBlock);
chunk->dataType = typeid(T).name();
if (PHASE_TRACE1(Js::NativeCodeDataPhase))
{
Output::Print(_u("NativeCodeData AllocNoFix: chunk: %p, data: %p, index: %d, len: %x, totalOffset: %x, type: %S\n"),
chunk, (void*)dataBlock, chunk->allocIndex, chunk->len, chunk->offset, chunk->dataType);
}
}
#endif
return dataBlock;
}
char * AllocLeaf(size_t requestedBytes)
{
return Alloc(requestedBytes);
}
};
template<typename T>
class AllocatorT : public Allocator
{
char* AddFixup(char* dataBlock)
{
if (isOOPJIT)
{
DataChunk* chunk = NativeCodeData::GetDataChunk(dataBlock);
chunk->fixupFunc = &Fixup;
#if DBG
chunk->dataType = typeid(T).name();
if (PHASE_TRACE1(Js::NativeCodeDataPhase))
{
Output::Print(_u("NativeCodeData Alloc: chunk: %p, data: %p, index: %d, len: %x, totalOffset: %x, type: %S\n"),
chunk, (void*)dataBlock, chunk->allocIndex, chunk->len, chunk->offset, chunk->dataType);
}
#endif
}
return dataBlock;
}
public:
char * Alloc(size_t requestedBytes)
{
return AddFixup(__super::Alloc(requestedBytes));
}
char * AllocZero(size_t requestedBytes)
{
return AddFixup(__super::AllocZero(requestedBytes));
}
static void Fixup(void* pThis, NativeCodeData::DataChunk* chunkList)
{
((T*)pThis)->Fixup(chunkList);
}
};
~NativeCodeData();
};
enum DataDesc
{
DataDesc_None,
DataDesc_InlineeFrameRecord_ArgOffsets,
DataDesc_InlineeFrameRecord_Constants,
DataDesc_BailoutInfo_CotalOutParamCount,
DataDesc_ArgOutOffsetInfo_StartCallOutParamCounts,
DataDesc_ArgOutOffsetInfo_StartCallArgRestoreAdjustCounts,
DataDesc_LowererMD_LoadFloatValue_Float,
DataDesc_LowererMD_LoadFloatValue_Double,
DataDesc_LowererMD_EmitLoadFloatCommon_Double,
DataDesc_LowererMD_Simd128LoadConst,
};
template<DataDesc desc = DataDesc_None>
struct IntType
{
int data;
};
template<DataDesc desc = DataDesc_None>
struct UIntType
{
uint data;
};
template<DataDesc desc = DataDesc_None>
struct FloatType
{
FloatType(float val) :data(val) {}
float data;
};
template<DataDesc desc = DataDesc_None>
struct DoubleType
{
DoubleType() {}
DoubleType(double val) :data(val) {}
double data;
};
template<DataDesc desc = DataDesc_None>
struct SIMDType
{
SIMDType() {}
SIMDType(AsmJsSIMDValue val) :data(val) {}
AsmJsSIMDValue data;
};
template<DataDesc desc = DataDesc_None>
struct VarType
{
Js::Var data;
void Fixup(NativeCodeData::DataChunk* chunkList)
{
AssertMsg(false, "Please specialize Fixup method for this Var type or use no-fixup allocator");
}
};
template<>
inline void VarType<DataDesc_InlineeFrameRecord_Constants>::Fixup(NativeCodeData::DataChunk* chunkList)
{
AssertMsg(false, "InlineeFrameRecord::constants contains Var from main process, should not fixup");
}
struct GlobalBailOutRecordDataTable;
template<>
inline void NativeCodeData::Array<GlobalBailOutRecordDataTable *>::Fixup(NativeCodeData::DataChunk* chunkList)
{
NativeCodeData::AddFixupEntryForPointerArray(this, chunkList);
}