forked from racket/racket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodetab.inc
More file actions
555 lines (476 loc) · 13.6 KB
/
codetab.inc
File metadata and controls
555 lines (476 loc) · 13.6 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/* Implementation of the "symbol table" for mapping code
pointers to function names. */
#ifndef MZ_PRECISE_GC
# ifdef USE_SENORA_GC
extern void *GC_base(void *d);
# define GC_is_marked(p) GC_base(p)
# else
extern MZ_DLLIMPORT int GC_is_marked(void *);
# endif
#endif
#define LOG_KEY_SIZE 4
#define KEY_MASK ((1 << LOG_KEY_SIZE) - 1)
#define KEY_COUNT (1 << LOG_KEY_SIZE)
/* In words: */
#define NODE_HEADER_SIZE 3
#define NODE_STARTS_OFFSET 1
#define NODE_GCABLE_OFFSET 2
/* Represent a node in the tree as an array of
NODE_HEADER_SIZE + KEY_COUNT pointers.
The first pointer is always NULL, so that the node can be
recognized as a node (as opposed to a Racket value) when referenced
by other nodes.
The second "pointer" is used as an array of bits, where each bit
indicates whether the corresponding pointer is the start of a
mapped range. The bits are shfted up by 1 an dthe low bit is always
set to 1, so that the GC doesn't think it's a pointer.
The third "pointer" is similar to the first, but it represents the
GC status of the reference, andthe bit is only used at a ramge
start.
Then there are KEY_COUNT "key" pointers for the content of the
node. Note that KEY_COUNT is smaller than the number of bits
available in the header's second and third "pointer"s.
In a leaf node, every "key" corresponds to an address. In a node
that references leaf nodes, every "key" corresponds to KEY_SIZE
addresses. So, at the root, the first key corresonds to address 0,
the second key corresponds to address (1 << (32-LOG_KEY_SIZE)) on a
32-bit platform, and so on.
To make the tree more compact, when all of the pointers in a node
would be the same non-node value, then the node itself can be
replaced with the non-node value. (That's why we need to be able to
distinguish node references from Scheme-object references.) Of
course, "leaf node" above means nodes that would be leaves without
this compaction.
When PLT_DUMP_JIT_RANGES is defined for compilation and as an
environment variable at run time, then an exit hook prints the
ranges of JIT-generated code. Furthermore, for 3m, defining
PLT_DUMP_JIT_RANGES at compilation time disables GC of
JIT-generated code. */
THREAD_LOCAL_DECL(static void **codetab_tree);
THREAD_LOCAL_DECL(static int during_set);
#ifdef MZ_USE_PLACES
static void **shared_codetab_tree;
/* The table is shared but not locked for read; We rely on x86-TSO
and the fact that kentries are only added (never removed)
to skip the lock. */
static mzrt_mutex *shared_codetab_lock;
#endif
#ifdef PLT_DUMP_JIT_RANGES
static int dump_registered = 0;
static void dump_symbol_ranges();
#endif
static int do_clear_symbols(void **t, uintptr_t start, int offset, uintptr_t addr, int clearing);
static void *do_find_symbol(void **the_tree, uintptr_t v)
{
uintptr_t k;
void **t = the_tree, *val;
int offset = (JIT_WORD_SIZE * 8);
while (offset) {
if (!t)
return NULL;
offset -= LOG_KEY_SIZE;
k = ((v >> offset) & KEY_MASK) + NODE_HEADER_SIZE;
val = t[k];
if (!val)
return NULL;
if (*(Scheme_Type *)val)
return val;
t = (void **)val;
}
printf("Error: walked off end of tree\n");
return NULL;
}
static void *find_symbol(uintptr_t v)
{
void *r;
r = do_find_symbol(codetab_tree, v);
#ifdef MZ_USE_PLACES
if (!r && shared_codetab_tree) {
r = do_find_symbol(shared_codetab_tree, v);
}
#endif
return r;
}
static void **malloc_node(int for_gc_able)
{
void **v;
long sz = (KEY_COUNT + NODE_HEADER_SIZE) * sizeof(void *);
#ifdef MZ_USE_PLACES
if (!for_gc_able) {
v = (void **)malloc(sz);
memset(v, 0, sz);
} else
#endif
v = (void **)scheme_malloc(sz);
/* Set low bit in each of STARTS and GCABLE so that they're not confused
for pointers: */
((uintptr_t *)v)[NODE_STARTS_OFFSET] = 0x1;
((uintptr_t *)v)[NODE_GCABLE_OFFSET] = 0x1;
return v;
}
void scheme_jit_add_symbol(uintptr_t start, uintptr_t end, void *value, int gc_able)
{
uintptr_t k1, k2, split_t_start = 0, split_t_end = 0, i;
int m;
int offset = (JIT_WORD_SIZE * 8), split_offset = 0;
void **the_tree, **t1, **t2, **split_t, *val1, *val2;
#ifdef PLT_DUMP_JIT_RANGES
if (!dump_registered) {
dump_registered = 1;
if (getenv("PLT_DUMP_JIT_RANGES"))
atexit(dump_symbol_ranges);
}
#endif
#ifdef MZ_USE_PLACES
if (!gc_able) {
if (!shared_codetab_lock) {
/* this function will be called in the main place
before others are started, so a lazy lock creation
is ok */
mzrt_mutex_create(&shared_codetab_lock);
}
mzrt_mutex_lock(shared_codetab_lock);
if (!shared_codetab_tree)
shared_codetab_tree = malloc_node(0);
the_tree = shared_codetab_tree;
} else
#endif
{
if (!codetab_tree) {
REGISTER_SO(codetab_tree);
codetab_tree = malloc_node(gc_able);
}
the_tree = codetab_tree;
}
during_set++;
t1 = t2 = the_tree;
split_t = NULL;
while (offset) {
offset -= LOG_KEY_SIZE;
k1 = ((start >> offset) & KEY_MASK) + NODE_HEADER_SIZE;
if (offset) {
val1 = t1[k1];
if (!val1) {
val1 = malloc_node(gc_able);
t1[k1] = val1;
}
} else
val1 = t1;
k2 = ((end >> offset) & KEY_MASK) + NODE_HEADER_SIZE;
if (offset) {
/* Need to go deeper... */
val2 = t2[k2];
if (!val2) {
val2 = malloc_node(gc_able);
t2[k2] = val2;
}
} else
val2 = t2;
if (!split_t && (val1 != val2)) {
split_t = t1;
split_t_start = k1;
split_t_end = k2;
split_offset = offset;
}
t1 = val1;
t2 = val2;
}
if (!split_t) {
/* assert: t1 == t2 */
split_t = t1;
split_t_start = k1;
split_t_end = k2;
}
/* Mark start bit: */
m = (1 << (k1 - NODE_HEADER_SIZE + 1));
((uintptr_t *)t1)[NODE_STARTS_OFFSET] |= m;
#ifndef MZ_PRECISE_GC
/* GCABLE flag indicates whether to check for GC later */
if (gc_able)
((uintptr_t *)t1)[NODE_GCABLE_OFFSET] |= m;
#else
/* GCABLE flag indicates whether it's been GCed: */
if (!value)
((uintptr_t *)t1)[NODE_GCABLE_OFFSET] |= m;
#endif
/* Fill in start and end: */
t1[k1] = value;
t2[k2] = value;
/* Fill in range between branches: */
for (i = split_t_start + 1; i < split_t_end; i++) {
split_t[i] = value;
}
/* Fill in places to right of start branches: */
if (t1 != split_t) {
k1 = ((start >> split_offset) & KEY_MASK) + NODE_HEADER_SIZE;
t1 = split_t[k1];
offset = split_offset;
while (offset) {
offset -= LOG_KEY_SIZE;
k1 = ((start >> offset) & KEY_MASK) + NODE_HEADER_SIZE;
for (i = k1 + 1; i < KEY_COUNT + NODE_HEADER_SIZE; i++) {
t1[i] = value;
}
t1 = t1[k1];
}
}
/* Fill in places to left of end branch: */
if (t2 != split_t) {
k2 = ((end >> split_offset) & KEY_MASK) + NODE_HEADER_SIZE;
t2 = split_t[k2];
offset = split_offset;
while (offset) {
offset -= LOG_KEY_SIZE;
k2 = ((end >> offset) & KEY_MASK) + NODE_HEADER_SIZE;
for (i = NODE_HEADER_SIZE; i < k2; i++) {
t2[i] = value;
}
t2 = t2[k2];
}
}
--during_set;
#ifdef MZ_PRECISE_GC
if (!value) {
/* Prune empty branches in the tree. Only do this if this
object is mapped deeply enough in the tree, otherwise
we end up scanning the whole tree. */
do_clear_symbols(the_tree, start, 0, 0, 0);
}
#endif
#ifdef MZ_USE_PLACES
if (!gc_able) {
mzrt_mutex_unlock(shared_codetab_lock);
}
#endif
}
static int do_clear_symbols(void **t, uintptr_t start, int offset, uintptr_t addr, int clearing)
/* If MZ_PRECISE_GC, then offset and addr are not used. */
{
int i, m, j;
void *val, **subt;
/* Note: this function might be called (via a GC callback)
while add_symbol is running. */
for (i = ((start >> offset) & KEY_MASK); i < KEY_COUNT; i++) {
m = (1 << (i + 1));
if (((uintptr_t *)t)[NODE_STARTS_OFFSET] & m) {
clearing = 0;
if (((uintptr_t *)t)[NODE_GCABLE_OFFSET] & m) {
/* GCable pointer starts here */
#ifndef MZ_PRECISE_GC
/* Conservative GC: GCable flag means use GC_is_marked */
void *p = (void *)(addr + ((uintptr_t)i << offset));
if (!GC_is_marked(p))
clearing = 1;
#else
/* Precise GC: GCable flag means it's gone */
clearing = 1;
#endif
if (clearing) {
/* Collected... */
((uintptr_t *)t)[NODE_STARTS_OFFSET] -= m;
((uintptr_t *)t)[NODE_GCABLE_OFFSET] -= m;
}
} else {
#ifdef MZ_PRECISE_GC
return 0;
#endif
}
}
#ifdef MZ_PRECISE_GC
if (!clearing)
val = NULL;
else
#endif
val = t[i + NODE_HEADER_SIZE];
if (val) {
if (!*(Scheme_Type *)val) {
subt = (void **)val;
clearing = do_clear_symbols(subt, start,
offset - LOG_KEY_SIZE,
(addr + ((uintptr_t)i << offset)),
clearing);
if (!during_set) {
/* If the branch is empty, then drop it. */
for (j = 0; j < KEY_COUNT; j++) {
if (subt[j + NODE_HEADER_SIZE])
break;
}
if (j == KEY_COUNT) {
t[i + NODE_HEADER_SIZE] = NULL;
}
}
#ifdef MZ_PRECISE_GC
if (!clearing) {
/* Finished clearing the one item, so return. */
return 0;
}
#endif
} else if (clearing)
t[i + NODE_HEADER_SIZE] = NULL;
}
}
return clearing;
}
#ifndef MZ_PRECISE_GC
static void clear_symbols_for_collected()
{
if (codetab_tree) {
do_clear_symbols(codetab_tree, 0, (JIT_WORD_SIZE * 8) - LOG_KEY_SIZE, 0, 0);
}
}
#endif
#ifdef PLT_DUMP_JIT_RANGES
static void *dump_symbol_range(void **t, int offset, uintptr_t base, void *val)
{
int i, m;
void *p;
offset -= LOG_KEY_SIZE;
for (i = 0; i < KEY_COUNT; i++) {
p = t[i + NODE_HEADER_SIZE];
if (p) {
if (*(Scheme_Type *)p) {
int is_start;
m = (1 << (i + 1));
is_start = (((uintptr_t *)t)[NODE_STARTS_OFFSET] & m);
if (val && ((p != val) || is_start)) {
printf("%" PRIxPTR "\n", base + (((uintptr_t)i - 1) << offset)); /* end range */
val = NULL;
}
if (is_start) {
/* start new range */
val = p;
printf("%" PRIxPTR "-", base + ((uintptr_t)i << offset));
if (find_symbol(base + ((uintptr_t)i << offset)) != val)
printf("[oops]");
}
} else {
val = dump_symbol_range((void **)p, offset, base + ((uintptr_t)i << offset), val);
}
} else if (val) {
printf("%" PRIxPTR "\n", base + (((uintptr_t)i - 1) << offset)); /* end range */
val = NULL;
}
}
return val;
}
static void dump_symbol_ranges()
{
(void)dump_symbol_range(codetab_tree, JIT_WORD_SIZE * 8, 0, NULL);
#ifdef MZ_USE_PLACES
if (shared_codetab_tree)
(void)dump_symbol_range(shared_codetab_tree, JIT_WORD_SIZE * 8, 0, NULL);
#endif
}
#endif
/*============================================================*/
/* testing */
/*============================================================*/
#if 0
Scheme_Type a[] = {1};
Scheme_Type b[] = {2};
Scheme_Type c[] = {3};
static char *nameof(void *p)
{
if (p == a) return "a";
if (p == b) return "b";
if (p == c) return "c";
if (!p) return "NULL";
return "?";
}
void *alt_gc;
void *gcs[3];
int GC_is_marked(void *p)
{
if (alt_gc) {
if (p == alt_gc)
return 1;
else
return 0;
} else {
if ((p == gcs[0])
|| (p == gcs[1])
|| (p == gcs[2]))
return 0;
else
return 1;
}
}
void check(int j, int delta, int i, void *expect, uintptr_t addr)
{
void *got;
got = find_symbol(addr);
if (i == 2)
expect = NULL;
if (expect != got)
printf("(%d,%d,%d) Expected %s, found %s at %p\n",
j, delta, i,
nameof(expect), nameof(got),
addr);
}
int main()
{
int i, d, delta, j;
for (j = 0; j < 2; j++) {
for (d = 0; d < 16; d++) {
delta = d;
for (i = 0; i < 3; i++) {
if (i != 1)
check(j, delta, 1, NULL, (delta + 0x12341234));
if (!i)
add_symbol(delta + 0x12341200, delta + 0x12341234, a, 1);
check(j, delta, i, a, ((delta + 0x12341234)));
check(j, delta, i, a, ((delta + 0x12341200)));
check(j, delta, i, a, ((delta + 0x12341201)));
check(j, delta, i, a, ((delta + 0x12341210)));
check(j, delta, i, a, ((delta + 0x12341231)));
check(j, delta, i, a, ((delta + 0x12341200)));
if (i != 1)
check(j, delta, i, NULL, ((delta + 0x12341236)));
if (!i)
add_symbol(delta + 0x12341236, delta + 0x12370000, b, 1);
check(j, delta, i, a, ((delta + 0x12341234)));
if (!i)
check(j, delta, i, NULL, ((delta + 0x12341235)));
check(j, delta, i, b, ((delta + 0x12341236)));
check(j, delta, i, b, ((delta + 0x12370000)));
check(j, delta, i, NULL, ((delta + 0x12370001)));
check(j, delta, i, b, ((delta + 0x12351236)));
check(j, delta, i, b, ((delta + 0x12350000)));
check(j, delta, i, b, ((delta + 0x12360000)));
if (!i) {
check(j, delta, i, NULL, ((delta + 0x12341235)));
add_symbol(delta + 0x12341235, delta + 0x12341235, c, 1);
}
check(j, delta, i, a, ((delta + 0x12341234)));
check(j, delta, i == 2 ? 0 : i, c, ((delta + 0x12341235)));
check(j, delta, i, b, ((delta + 0x12341236)));
if (!delta) {
if (!i && !j) {
check(j, delta, i, NULL, ((0x55556666)));
add_symbol(0x55556663, 0x55556669, a, 0); /* Not GCable */
}
}
check(j, delta, 0, a, ((0x55556666)));
check(j, delta, 0, a, ((0x55556663)));
check(j, delta, 0, a, ((0x55556669)));
if (i == 0) {
alt_gc = NULL;
gcs[0] = NULL;
gcs[1] = NULL;
gcs[2] = NULL;
} else {
if (0)
alt_gc = (void *)0x55556663;
gcs[0] = (void *)(delta + 0x12341200);
gcs[1] = (void *)(delta + 0x12341236);
if (i == 2)
gcs[2] = (void *)(delta + 0x12341235);
else
gcs[2] = NULL;
}
clear_symbols_for_collected();
}
}
}
}
#endif