gmux 0.1.0
Linux on Cloudflare Workers
Loading...
Searching...
No Matches
aot.h
1#ifndef AOT_H
2#define AOT_H
3
4#ifndef KB_AOT
5 #error "lifted regions need -DKB_AOT"
6#endif
7
8#include <stdint.h>
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12
13#include "kb.h"
14#include "wide.h"
15
16/* the interpreter's flags, conditions and x86 mul/div (src/gmux/katybug/run.c)
17 over locals instead of the cpu, so lifted code keeps them in registers; keep
18 in step with run.c. Always inlined: the kind is a constant only after
19 inlining, and a call spills the flags to memory (measured: 13-17% of the
20 lifted runs when clang declined) */
21
22struct aot_fl {
23 int n, z, c, v, p;
24};
25
26static inline uint64_t aot_mask(int w) {
27 return w >= 8 ? ~0ull : (1ull << (8 * w)) - 1;
28}
29
30static inline int64_t aot_sext(uint64_t v, int w) {
31 int s = 64 - 8 * w;
32 return w >= 8 ? (int64_t) v : (int64_t) (v << s) >> s;
33}
34
35static inline int aot_parity(uint64_t v) {
36 v &= 0xff;
37 v ^= v >> 4;
38 v ^= v >> 2;
39 v ^= v >> 1;
40 return !(v & 1);
41}
42
43static inline __attribute__((always_inline)) void aot_flags(
44 struct aot_fl* f, int kind, uint64_t a, uint64_t b, uint64_t r, int w
45) {
46 uint64_t m = aot_mask(w), sign = 1ull << (8 * w - 1);
47 if ((kind == KB_F_SHL || kind == KB_F_SHR || kind == KB_F_SAR ||
48 kind == KB_F_ROL || kind == KB_F_ROR) &&
49 b == 0)
50 return;
51 switch (kind) {
52 case KB_F_MULOV: f->c = f->v = b != 0; return;
53 case KB_F_SETC: f->c = (int) (b & 1); return;
54 case KB_F_ZERO: f->z = (b & m) == 0; return;
55 case KB_F_CNT:
56 f->c = (b & m) == 0;
57 f->z = (r & m) == 0;
58 return;
59 case KB_F_ROL:
60 f->c = (int) (r & 1);
61 f->v = !!(r & sign) ^ f->c;
62 return;
63 case KB_F_ROR:
64 f->c = !!(r & sign);
65 f->v = !!(r & sign) ^ !!(r & (sign >> 1));
66 return;
67 }
68 a &= m;
69 b &= m;
70 r &= m;
71 f->z = r == 0;
72 f->n = !!(r & sign);
73 f->p = aot_parity(r);
74 switch (kind) {
75 case KB_F_ADD:
76 case KB_F_A_ADD:
77 f->c = r < a;
78 f->v = !!(~(a ^ b) & (a ^ r) & sign);
79 break;
80 case KB_F_ADC: {
81 uint64_t cin = (uint64_t) f->c;
82 f->c = w < 8 ? a + b + cin > m : (cin ? r <= a : r < a);
83 f->v = !!(~(a ^ b) & (a ^ r) & sign);
84 break;
85 }
86 case KB_F_SUB:
87 f->c = a < b;
88 f->v = !!((a ^ b) & (a ^ r) & sign);
89 break;
90 case KB_F_SBB: {
91 uint64_t cin = (uint64_t) f->c;
92 f->c = w < 8 ? a < b + cin : (a < b || (cin && a == b));
93 f->v = !!((a ^ b) & (a ^ r) & sign);
94 break;
95 }
96 case KB_F_A_SUB:
97 f->c = a >= b;
98 f->v = !!((a ^ b) & (a ^ r) & sign);
99 break;
100 case KB_F_LOGIC:
101 case KB_F_A_LOGIC:
102 f->c = 0;
103 f->v = 0;
104 break;
105 case KB_F_INC: f->v = r == sign; break;
106 case KB_F_DEC: f->v = r == sign - 1; break;
107 case KB_F_NEG:
108 f->c = b != 0;
109 f->v = b == sign;
110 break;
111 case KB_F_SHL:
112 f->c = b <= 8u * (unsigned) w ? !!((a << (b - 1)) & sign) : 0;
113 f->v = f->n ^ f->c;
114 break;
115 case KB_F_SHR:
116 f->c = !!((a >> (b - 1)) & 1);
117 f->v = !!(a & sign);
118 break;
119 case KB_F_SAR:
120 f->c = !!(((uint64_t) aot_sext(a, w) >> (b - 1)) & 1);
121 f->v = 0;
122 break;
123 case KB_F_NZCV:
124 f->n = !!(b & (1u << 31));
125 f->z = !!(b & (1u << 30));
126 f->c = !!(b & (1u << 29));
127 f->v = !!(b & (1u << 28));
128 break;
129 }
130}
131
132static inline __attribute__((always_inline)) int aot_cond(
133 const struct aot_fl* f, int cond
134) {
135 int n = f->n, z = f->z, c = f->c, v = f->v;
136 if (cond < KB_C_A64) {
137 int r;
138 switch (cond >> 1) {
139 case 0: r = v; break;
140 case 1: r = c; break;
141 case 2: r = z; break;
142 case 3: r = c || z; break;
143 case 4: r = n; break;
144 case 5: r = f->p; break;
145 case 6: r = n != v; break;
146 default: r = z || n != v; break;
147 }
148 return (cond & 1) ? !r : r;
149 }
150 int k = cond - KB_C_A64, r;
151 if (k >= 14) return 1;
152 switch (k >> 1) {
153 case 0: r = z; break;
154 case 1: r = c; break;
155 case 2: r = n; break;
156 case 3: r = v; break;
157 case 4: r = c && !z; break;
158 case 5: r = n == v; break;
159 default: r = !z && n == v; break;
160 }
161 return (k & 1) ? !r : r;
162}
163
164/* x86 mul, imul, div and idiv of rdx:rax by v; 1 on a divide error, leaving
165 * both untouched */
166static inline __attribute__((always_inline)) int aot_muldiv(
167 uint64_t* rax, uint64_t* rdx, struct aot_fl* f, int kind, int w, uint64_t v
168) {
169 if (kind == 4 || kind == 5) {
170 f->c = f->v = kb_mul(kind == 5, w, rax, rdx, v);
171 return 0;
172 }
173 return kb_divide(kind == 7, w, rax, rdx, v);
174}
175
176/* lift.ts --identity: the host bytes of a guest address. The default is the
177 address itself, for a runtime that maps guests at their own addresses
178 (-DKB_IDENTITY in mem.c) */
179#ifndef AOT_HOST
180 #ifdef KB_WINDOW
181/* the guest lives in one block of the program's own memory at its own offsets
182 (mem.c under -DKB_IDENTITY -DKB_WINDOW=<bytes>); an access is the block's
183 address plus the low 32 bits of the guest address */
184extern uint8_t kb_window[] __attribute__((visibility("hidden")));
185 #define AOT_HOST(va) (kb_window + (uint32_t) (va))
186 #else
187 #define AOT_HOST(va) ((uint8_t*) (uintptr_t) (va))
188 #endif
189#endif
190
191/* attribution arms, unsafe outside a measurement:-DAOT_NO_SIGNAL_CHECK moves
192 between lifted blocks without looking for a pending signal (the verification
193 check stays: an unmatched block has no IR to run), -DAOT_NO_RANGE_CHECK
194 trusts a load or store's cached mapping whenever its generation holds */
195/* back: the transition goes to the same or a lower address; AOT_POLL (kb.h's
196 KB_POLL unless set) polls only there, or every KB_POLL_FUEL transitions */
197#ifndef AOT_POLL
198 #define AOT_POLL KB_POLL
199#endif
200/* -DKB_COUNT: moves between lifted blocks and the reads of the pending flag
201 among them, and the accesses by kind (printed beside the interpreter's
202 counts at exit; not counted under AOT_POLL 3, which polls by fuel) */
203#ifdef KB_COUNT
204struct aot_stat {
205 uint64_t win_enter, win_acc, chk_acc, grp_acc, cont, polls;
206 uint64_t ep_in, ep_slow, ep_back;
207 uint64_t win_idx, win_slow, win_fail, reval;
208 uint64_t flat_hit, flat_sync, flat_miss;
209};
210static struct aot_stat aot_stat;
211 #define AOT_STAT(f, n) (aot_stat.f += (n))
212 #define AOT_NOTE(polled) (aot_stat.cont++, aot_stat.polls += (polled))
213 #define AOT_NOTE_EP(back) (aot_stat.cont++, aot_stat.ep_back += (back))
214#else
215 #define AOT_STAT(f, n) ((void) 0)
216 #define AOT_NOTE(polled) ((void) 0)
217 #define AOT_NOTE_EP(back) ((void) 0)
218#endif
219/* lift.ts --flatexit: a table site that misses leaves the region at its
220 instruction with this set, and run.c runs that instruction in the
221 interpreter before it takes a lifted entry again (defined in run.c) */
222extern int kb_aot_miss;
223/* -DAOT_CLASS (lifted with --classes): each access reports its kind (0 window,
224 1 group, 2 checked, 3 slot held in a local, 4 frame window, 5 table site
225 that leaves on a miss), load or store
226 and the mapping it lands in; the tally is appended to $KATYBUG_COUNT at exit
227 */
228#ifdef AOT_CLASS
229struct aot_cls_row {
230 uint64_t start, end, n[6][2], straddle[6][2], cross[6][2];
231 int prot, bits;
232};
233static struct aot_cls_row aot_cls_rows[256];
234static int aot_cls_nrows;
235static uint64_t aot_cls_none[6][2];
236/* one entry per load or store the lifter emitted (its site id, counted in
237 emission order, so a second lift with the same flags numbers them the same):
238 accesses, those on the page of the site's previous access, those at the
239 previous address, the mapping start */
240static struct aot_site {
241 uint64_t n, page, same, last, map;
242 int kind, st, w, multi;
243} aot_sites[1 << 16];
244
245static __attribute__((noinline)) void aot_cls(
246 struct kb_cpu* cpu, int kind, int st, uint64_t va, int w, int site
247) {
248 static int last = -1;
249 const struct kb_mapping* m = NULL;
250 if (last >= 0 && last < cpu->nmaps && va >= cpu->maps[last].start &&
251 va < cpu->maps[last].end)
252 m = &cpu->maps[last];
253 for (int i = 0; !m && i < cpu->nmaps; i++)
254 if (va >= cpu->maps[i].start && va < cpu->maps[i].end)
255 m = &cpu->maps[last = i];
256 struct aot_site* s = &aot_sites[site & 0xffff];
257 if (!m) {
258 aot_cls_none[kind][st]++;
259 s->multi |= s->n != 0;
260 s->n++;
261 return;
262 }
263 if (s->n && s->map != m->start) s->multi = 1;
264 s->page += s->n && (s->last >> 12) == (va >> 12);
265 s->same += s->n && s->last == va;
266 s->n++, s->last = va, s->map = m->start, s->kind = kind, s->st = st,
267 s->w = w;
268 struct aot_cls_row* r = NULL;
269 for (int i = 0; i < aot_cls_nrows && !r; i++)
270 if (aot_cls_rows[i].start == m->start &&
271 aot_cls_rows[i].end == m->end && aot_cls_rows[i].prot == m->prot &&
272 aot_cls_rows[i].bits == m->bits)
273 r = &aot_cls_rows[i];
274 if (!r) {
275 if (aot_cls_nrows == 256) return;
276 r = &aot_cls_rows[aot_cls_nrows++];
277 r->start = m->start, r->end = m->end, r->prot = m->prot,
278 r->bits = m->bits;
279 }
280 r->n[kind][st]++;
281 r->straddle[kind][st] +=
282 (va >> m->bits) != ((va + (uint64_t) w - 1) >> m->bits);
283 r->cross[kind][st] += va + (uint64_t) w > m->end;
284}
285
286__attribute__((destructor)) static void aot_cls_report(void) {
287 const char* path = getenv("KATYBUG_COUNT");
288 FILE* f = path ? fopen(path, "a") : NULL;
289 if (!f) return;
290 const char* min = getenv("KATYBUG_SITE_MIN");
291 uint64_t at_least = min ? strtoull(min, NULL, 10) : 1000;
292 for (int k = 0; k < 6; k++)
293 for (int s = 0; s < 2; s++) {
294 if (aot_cls_none[k][s])
295 fprintf(
296 f,
297 "katybug cls: kind %d st %d start 0 end 0 prot -1 bits 0 n "
298 "%llu straddle 0 cross 0\n",
299 k, s, (unsigned long long) aot_cls_none[k][s]
300 );
301 for (int i = 0; i < aot_cls_nrows; i++) {
302 const struct aot_cls_row* r = &aot_cls_rows[i];
303 if (!r->n[k][s]) continue;
304 fprintf(
305 f,
306 "katybug cls: kind %d st %d start %llx end %llx prot %d "
307 "bits %d n %llu straddle %llu cross %llu\n",
308 k, s, (unsigned long long) r->start,
309 (unsigned long long) r->end, r->prot, r->bits,
310 (unsigned long long) r->n[k][s],
311 (unsigned long long) r->straddle[k][s],
312 (unsigned long long) r->cross[k][s]
313 );
314 }
315 }
316 for (int i = 0; i < 1 << 16; i++)
317 if (aot_sites[i].n >= at_least)
318 fprintf(
319 f,
320 "katybug site: id %d kind %d st %d w %d n %llu page %llu "
321 "same %llu multi %d map %llx\n",
322 i, aot_sites[i].kind, aot_sites[i].st, aot_sites[i].w,
323 (unsigned long long) aot_sites[i].n,
324 (unsigned long long) aot_sites[i].page,
325 (unsigned long long) aot_sites[i].same, aot_sites[i].multi,
326 (unsigned long long) aot_sites[i].map
327 );
328 fclose(f);
329}
330 #define AOT_CLS(site, kind, st, va, w) aot_cls(cpu, kind, st, va, w, site)
331#else
332 #define AOT_CLS(site, kind, st, va, w) ((void) 0)
333#endif
334/* -DAOT_WIN_CHECK: an access with an index bound stops the run when its address
335 is outside its window (a verification build of the lifter's bounds) */
336#ifdef AOT_WIN_CHECK
337 #define AOT_WINCHK(dist, room) \
338 do { \
339 if ((dist) > (room)) { \
340 fprintf( \
341 stderr, "katybug: indexed access outside its window\n" \
342 ); \
343 abort(); \
344 } \
345 } while (0)
346#else
347 #define AOT_WINCHK(dist, room) ((void) 0)
348#endif
349/* lift.ts --epochs: a region keeps the kb_epoch it was entered at (ep) and its
350 caches are valid until kb_epoch moves; a back edge tests ep, and a cache is
351 cold or current (run.c's KB_IC_COLD) */
352#ifdef AOT_EPOCHS
353 #if !KB_EPOCH
354 #error "lifted with --epochs: build with -DKB_EPOCH=1"
355 #endif
356 #define AOT_COLD 7
357#endif
358#ifdef AOT_NO_SIGNAL_CHECK
359 #define AOT_CONTINUE(ok, back) (AOT_NOTE(0), (ok))
360#elif defined(AOT_EPOCHS)
361 #define AOT_CONTINUE(ok, back) \
362 (AOT_NOTE_EP(!!(back)), (!(back) || kb_epoch == ep) && (ok))
363#elif AOT_POLL == 1 || AOT_POLL == 2
364 #define AOT_CONTINUE(ok, back) \
365 (AOT_NOTE(!!(back)), (!(back) || !*kb_pending_flag) && (ok))
366#elif AOT_POLL == 3
367 #define AOT_CONTINUE(ok, back) \
368 (AOT_NOTE(0), \
369 (--fuel || (fuel = KB_POLL_FUEL, !*kb_pending_flag)) && (ok))
370#else
371 #define AOT_CONTINUE(ok, back) (AOT_NOTE(1), !*kb_pending_flag && (ok))
372#endif
373#if defined(AOT_EPOCHS) && defined(AOT_NO_RANGE_CHECK)
374 #define AOT_SLOW(q, gen, va, w) ((q)->host == NULL)
375#elif defined(AOT_EPOCHS)
376 #define AOT_SLOW(q, gen, va, w) ((va) - (q)->lo >= (q)->span - AOT_COLD)
377#elif defined(AOT_NO_RANGE_CHECK)
378 #define AOT_SLOW(q, gen, va, w) ((q)->gen != (gen))
379#else
380 #define AOT_SLOW(q, gen, va, w) \
381 ((q)->gen != (gen) || (va) - (q)->lo > (q)->span - (w))
382#endif
383
384/* a resolve that fails (the span is not in one accessible piece) is counted
385 where the lifted code makes it */
386#ifdef KB_COUNT
387static inline uint8_t* aot_host_ic_at(
388 struct kb_cpu* cpu, struct kb_ic* ic, uint64_t va, uint64_t len, int acc
389) {
390 uint8_t* p = kb_host_ic_at(cpu, ic, va, len, acc);
391 aot_stat.win_fail += !p;
392 return p;
393}
394 #define kb_host_ic_at aot_host_ic_at
395#endif
396
397/* lift.ts --bounds=two: a window of len bytes from va that may cross one piece
398 boundary. Returns the host bytes of va's piece; *h1 is the next piece's and
399 *bd the guest address where it starts, or (uint64_t) -1 when the window ends
400 in the first. NULL when either piece does not resolve for acc (KB_R for a
401 window that only loads, KB_W for one that stores) */
402static inline __attribute__((always_inline)) uint8_t* aot_open2(
403 struct kb_cpu* cpu, struct kb_ic* q0, struct kb_ic* q1, uint32_t gen,
404 uint64_t va, uint64_t len, int acc, uint8_t** h1, uint64_t* bd
405) {
406 /* under epochs a cache is cold (no span) or current, so no generation */
407#ifdef AOT_EPOCHS
408 #define AOT_GEN(q) ((void) gen, 1)
409#else
410 #define AOT_GEN(q) ((q)->gen == gen)
411#endif
412 uint8_t* p = AOT_GEN(q0) && va - q0->lo < q0->span
413 ? q0->host + (va - q0->lo)
414 : kb_host_ic_at(cpu, q0, va, 1, acc);
415 if (!p) return NULL;
416 uint64_t end = q0->lo + q0->span;
417 if (va + len <= end) {
418 *bd = ~0ull;
419 return p;
420 }
421 uint64_t need = va + len - end;
422 uint8_t* r =
423 AOT_GEN(q1) && need <= q1->span && end - q1->lo <= q1->span - need
424 ? q1->host + (end - q1->lo)
425 : kb_host_ic_at(cpu, q1, end, need, acc);
426 if (!r) return NULL;
427 *h1 = r;
428 *bd = end;
429 return p;
430}
431
432/* the host address of an access at a, w bytes wide, in a two-piece window that
433 starts at va (host h0) with its second piece at bd (host h1); NULL when the
434 window did not resolve or the access straddles the boundary */
435#define AOT_PICK2(a, w, va, h0, h1, bd) \
436 ((h0) ? ((a) >= (bd) ? (h1) + ((a) - (bd)) \
437 : (a) + (w) <= (bd) ? (h0) + ((a) - (va)) \
438 : NULL) \
439 : NULL)
440
441/* -DAOT_LOOP_CHECK: a loop window used after the mapping generation moved past
442 the one it was resolved at stops the run (a verification build of the loop
443 guards' revalidation) */
444#ifdef AOT_LOOP_CHECK
445 #define AOT_LCHK_FAIL() \
446 do { \
447 fprintf(stderr, "katybug: loop window used past a remap\n"); \
448 abort(); \
449 } while (0)
450 #ifdef AOT_EPOCHS
451 #define AOT_LGEN cpu->mapgen
452 #else
453 #define AOT_LGEN gen
454 #endif
455 #define AOT_LCHK(q) \
456 do { \
457 if ((q)->gen != AOT_LGEN) AOT_LCHK_FAIL(); \
458 } while (0)
459 #define AOT_LCHK1(q, bd) \
460 do { \
461 if ((bd) != ~0ull && (q)->gen != AOT_LGEN) AOT_LCHK_FAIL(); \
462 } while (0)
463#else
464 #define AOT_LCHK(q) ((void) 0)
465 #define AOT_LCHK1(q, bd) ((void) 0)
466#endif
467
468/* -DKB_COUNT: a region's own cpu traffic (entry loads, exit stores, spills
469 around helpers) and its block runs, beside the interpreter's (run.c) */
470enum
471{
472 AOT_C_REGION, /* entry loads and exit stores */
473 AOT_C_SSE,
474 AOT_C_X87,
475 AOT_C_STR,
476 AOT_C_SYS,
477 AOT_C_FS,
478 AOT_C_SLOT, /* slots form: loads served by a local [0], stores that updated
479 one [1] */
480 AOT_C_FIX, /* slots form: loads at a stub or edge [0], re-reads after an
481 unknown store [1] */
482 AOT_C_MEM, /* slots form: guest loads [0] and stores [1] that went to memory
483 */
484 AOT_C_CALL /* direct calls into another region [0] */
485};
486
487/* A direct call (lift.ts --calls) from region A to a block of region B, a call
488 * to B's block that has no other way in than a return address pushed by A's
489 * block:
490 * - B's blocks run as `rB_go(cpu, pc, io)`, always inlined into A at the call
491 * site, with A's registers, flags, df, mapping generation and fs base in
492 * io, a struct of locals; nothing goes through the cpu. A holds every
493 * register B holds (B's region registers are a subset of A's), so a
494 * register is never in the cpu on one side and in a local on the other;
495 * B's helper registers (string, SSE, x87) are part of that set
496 * - B runs from the target block and leaves through its own `out` with the
497 * pc it stopped at, its state in io. A takes every field back; a pc that is
498 * A's return site (the pushed address) continues at that block, any other
499 * pc (a fault, a pending signal, a jump elsewhere) leaves through A's out,
500 * which stores every register and the flags in the cpu with the pc exact
501 * - a fault in B sets the cpu's fault and ipc before it leaves, so A's exit
502 * hands the interpreter the faulting pc with all registers and flags as
503 * they stood; a signal handler that returns resumes the interpreter there
504 * - a callee has no syscall (it may fork or switch threads and needs every
505 * register in the cpu), a call that would close a cycle between regions
506 * leaves instead, and a block that did not attach (kb_aot_attach) is never
507 * entered, so the call leaves for the interpreter at that pc
508 * - the slots form re-enters a promoted return site through its stub: the
509 * callee may have written a frame slot through a pointer */
510
511/* -DKB_COUNT: where regions are left, by the guest pc the interpreter resumes
512 at; KATYBUG_EXITS=<n> prints the n most common at exit */
513#ifdef KB_COUNT
514struct aot_exit {
515 uint64_t pc, n;
516};
517static struct aot_exit aot_exits[1024];
518
519static void aot_exit_at(uint64_t pc) {
520 for (unsigned h = (unsigned) (pc ^ (pc >> 10)) & 1023, k = 0; k < 1024;
521 k++, h = (h + 1) & 1023)
522 if (aot_exits[h].n == 0 || aot_exits[h].pc == pc) {
523 aot_exits[h].pc = pc;
524 aot_exits[h].n++;
525 return;
526 }
527}
528
529static int aot_exit_order(const void* a, const void* b) {
530 const struct aot_exit *x = a, *y = b;
531 return x->n < y->n ? 1 : x->n > y->n ? -1 : 0;
532}
533
534__attribute__((destructor)) static void aot_exit_report(void) {
535 const char* e = getenv("KATYBUG_EXITS");
536 if (!e) return;
537 qsort(aot_exits, 1024, sizeof aot_exits[0], aot_exit_order);
538 for (int i = 0; i < atoi(e) && i < 1024 && aot_exits[i].n; i++)
539 fprintf(
540 stderr, "katybug exit: %llx %llu\n",
541 (unsigned long long) aot_exits[i].pc,
542 (unsigned long long) aot_exits[i].n
543 );
544}
545 #define AOT_COUNT_EXIT(pc) aot_exit_at(pc)
546
547__attribute__((destructor)) static void aot_stat_report(void) {
548 const char* path = getenv("KATYBUG_COUNT");
549 FILE* f = path ? fopen(path, "a") : NULL;
550 if (!f) return;
551 fprintf(
552 f,
553 "katybug aot: win_enter %llu win_acc %llu chk_acc %llu grp_acc %llu "
554 "cont %llu polls %llu ep_in %llu ep_slow %llu ep_back %llu "
555 "win_idx %llu win_slow %llu win_fail %llu reval %llu\n",
556 (unsigned long long) aot_stat.win_enter,
557 (unsigned long long) aot_stat.win_acc,
558 (unsigned long long) aot_stat.chk_acc,
559 (unsigned long long) aot_stat.grp_acc,
560 (unsigned long long) aot_stat.cont, (unsigned long long) aot_stat.polls,
561 (unsigned long long) aot_stat.ep_in,
562 (unsigned long long) aot_stat.ep_slow,
563 (unsigned long long) aot_stat.ep_back,
564 (unsigned long long) aot_stat.win_idx,
565 (unsigned long long) aot_stat.win_slow,
566 (unsigned long long) aot_stat.win_fail,
567 (unsigned long long) aot_stat.reval
568 );
569 fprintf(
570 f, "katybug flat: hit %llu sync %llu miss %llu\n",
571 (unsigned long long) aot_stat.flat_hit,
572 (unsigned long long) aot_stat.flat_sync,
573 (unsigned long long) aot_stat.flat_miss
574 );
575 fclose(f);
576}
577#else
578 #define AOT_COUNT_EXIT(pc) ((void) 0)
579#endif
580
581#ifdef KB_COUNT
582 #define AOT_COUNT_BLOCK(b) ((b)->runs++)
583 #define AOT_COUNT_RD(n) (kb_count.rd += (n))
584 #define AOT_COUNT_WR(n) (kb_count.wr += (n))
585 #define AOT_COUNT_RDC(c, n) (kb_count.rd += (n), kb_count.cat[c][0] += (n))
586 #define AOT_COUNT_WRC(c, n) (kb_count.wr += (n), kb_count.cat[c][1] += (n))
587 #define AOT_COUNT_CAT(c, i, n) (kb_count.cat[c][i] += (n))
588#else
589 #define AOT_COUNT_BLOCK(b) ((void) 0)
590 #define AOT_COUNT_RD(n) ((void) 0)
591 #define AOT_COUNT_WR(n) ((void) 0)
592 #define AOT_COUNT_RDC(c, n) ((void) 0)
593 #define AOT_COUNT_WRC(c, n) ((void) 0)
594 #define AOT_COUNT_CAT(c, i, n) ((void) 0)
595#endif
596
597/* KATYBUG_REGS=1 runs the regions built with lift.ts --regs in their precise
598 * form (aot_runp), 2 the one built with --slots (aot_runs); anything else, the
599 * form every earlier arm used */
600static inline int aot_regs(void) {
601 static int on = -1;
602 if (on < 0) {
603 const char* e = getenv("KATYBUG_REGS");
604 on = e && e[0] >= '1' && e[0] <= '2' ? e[0] - '0' : 0;
605 }
606 return on;
607}
608
609struct aot_entry {
610 uint64_t pc, next, target;
611 int n, region, idx;
612 const struct kb_ins* ins;
613 const uint32_t* pcs; /* each op's instruction offset, or NULL */
614};
615
616#endif
Definition aot.h:609
Definition aot.h:22
Definition kb.h:349
Definition kb.h:241
Definition kb.h:223
Definition kb.h:325