add a thp-based huge page alloc fallback
[pcsx_rearmed.git] / deps / lightning / lib / jit_print.c
1 /*
2  * Copyright (C) 2012-2023  Free Software Foundation, Inc.
3  *
4  * This file is part of GNU lightning.
5  *
6  * GNU lightning is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published
8  * by the Free Software Foundation; either version 3, or (at your option)
9  * any later version.
10  *
11  * GNU lightning is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14  * License for more details.
15  *
16  * Authors:
17  *      Paulo Cesar Pereira de Andrade
18  */
19
20 #include <lightning.h>
21 #include <lightning/jit_private.h>
22
23 #if __WORDSIZE == 32
24 #  define MININT                0x80000000
25 #  define INT_FMT               "%d"
26 #  define DEC_FMT               "%d"
27 #  define HEX_FMT               "0x%x"
28 #else
29 #  define MININT                0x8000000000000000
30 #  define INT_FMT               "%d"
31 #  define DEC_FMT               "%ld"
32 #  define HEX_FMT               "0x%lx"
33 #endif
34
35
36 #define print_chr(value)                fputc(value, print_stream)
37 #define print_hex(value)                                                \
38     do {                                                                \
39         if (value < 0 && value != MININT)                               \
40             fprintf(print_stream, "-" HEX_FMT, (jit_uword_t)-value);    \
41         else                                                            \
42             fprintf(print_stream, HEX_FMT, (jit_uword_t)value);         \
43     } while (0)
44 #define print_dec(value)                fprintf(print_stream, DEC_FMT, value)
45 #define print_int(value)                fprintf(print_stream, INT_FMT, value)
46 #define print_flt(value)                fprintf(print_stream, "%g", value)
47 #define print_str(value)                fprintf(print_stream, "%s", value)
48 #define print_ptr(value)                fprintf(print_stream, "%p", value)
49 #define print_reg(value)                                                \
50     do {                                                                \
51         if ((value) & jit_regno_patch)                                  \
52             print_chr('?');                                             \
53         print_str(_rvs[jit_regno(value)].name);                         \
54     } while (0)
55 #define print_arg(value)                                                \
56     do {                                                                \
57         print_chr('#');                                                 \
58         if (value)                                                      \
59             print_dec((value)->v.w);                                    \
60         else                                                            \
61             print_chr('?');                                             \
62     } while (0)
63
64 /*
65  * Initialization
66  */
67 #include "jit_names.c"
68 /*
69  * Initialization
70  */
71 static FILE     *print_stream;
72
73
74 /*
75  * Implementation
76  */
77 void
78 jit_init_print(void)
79 {
80     if (!print_stream)
81         print_stream = stdout;
82 }
83
84 void
85 _jit_print(jit_state_t *_jit)
86 {
87     jit_node_t          *node;
88
89     if ((node = _jitc->head)) {
90         jit_print_node(node);
91         for (node = node->next; node; node = node->next) {
92             print_chr('\n');
93             jit_print_node(node);
94         }
95         print_chr('\n');
96     }
97 }
98
99 void
100 _jit_print_node(jit_state_t *_jit, jit_node_t *node)
101 {
102     jit_block_t         *block;
103     jit_int32_t          value;
104     jit_int32_t          offset;
105
106     if (node->code == jit_code_label ||
107         node->code == jit_code_prolog || node->code == jit_code_epilog) {
108         print_chr('L');
109         print_dec(node->v.w);
110         print_chr(':');
111         block = _jitc->blocks.ptr + node->v.w;
112         for (offset = 0; offset < _jitc->reglen; offset++) {
113             if (jit_regset_tstbit(&block->reglive, offset)) {
114                 print_chr(' ');
115                 print_reg(offset);
116             }
117         }
118         if (node->code == jit_code_prolog ||
119             node->code == jit_code_epilog) {
120             print_str(" /* ");
121             print_str(code_name[node->code]);
122             print_str(" */");
123         }
124         return;
125     }
126     value = jit_classify(node->code) &
127         (jit_cc_a0_int|jit_cc_a0_flt|jit_cc_a0_dbl|jit_cc_a0_jmp|
128          jit_cc_a0_reg|jit_cc_a0_rlh|jit_cc_a0_arg|
129          jit_cc_a1_reg|jit_cc_a1_rlh|jit_cc_a1_int|
130          jit_cc_a1_flt|jit_cc_a1_dbl|jit_cc_a1_arg|
131          jit_cc_a2_reg|jit_cc_a2_int|jit_cc_a2_flt|jit_cc_a2_dbl|jit_cc_a2_rlh);
132     if (!(node->flag & jit_flag_synth) && ((value & jit_cc_a0_jmp) ||
133                                            node->code == jit_code_finishr ||
134                                            node->code == jit_code_finishi))
135         print_str("    ");
136     else
137         print_chr('\t');
138     if (node->flag & jit_flag_synth)
139         print_str(" \\__ ");
140     print_str(code_name[node->code]);
141     switch (node->code) {
142         r:
143             print_chr(' ');     print_reg(node->u.w);   return;
144         w:
145             print_chr(' ');     print_hex(node->u.w);   return;
146         f:
147             print_chr(' ');
148             if (node->flag & jit_flag_data)
149                 print_flt(*(jit_float32_t *)node->u.n->u.w);
150             else
151                 print_flt(node->u.f);
152             return;
153         d:
154             print_chr(' ');
155             if (node->flag & jit_flag_data)
156                 print_flt(*(jit_float64_t *)node->u.n->u.w);
157             else
158                 print_flt(node->u.d);
159             return;
160         n:
161             print_chr(' ');
162             if (!(node->flag & jit_flag_node))
163                 print_ptr(node->u.p);
164             else {
165                 print_chr('L');
166                 print_dec(node->u.n->v.w);
167             }
168             return;
169         a:
170             print_chr(' ');     print_arg(node);        return;
171         r_r:
172             print_chr(' ');     print_reg(node->u.w);
173             print_chr(' ');     print_reg(node->v.w);   return;
174         r_w:
175             print_chr(' ');     print_reg(node->u.w);
176             print_chr(' ');     print_hex(node->v.w);   return;
177         r_f:
178             print_chr(' ');     print_reg(node->u.w);
179             print_chr(' ');
180             if (node->flag & jit_flag_data)
181                 print_flt(*(jit_float32_t *)node->v.n->u.w);
182             else
183                 print_flt(node->v.f);
184             return;
185         r_d:
186             print_chr(' ');     print_reg(node->u.w);
187             print_chr(' ');
188             if (node->flag & jit_flag_data)
189                 print_flt(*(jit_float64_t *)node->v.n->u.w);
190             else
191                 print_flt(node->v.d);
192             return;
193         r_a:
194             print_chr(' ');     print_reg(node->u.w);
195             print_chr(' ');     print_arg(node->v.n);
196             return;
197         w_r:
198             print_chr(' ');     print_hex(node->u.w);
199             print_chr(' ');     print_reg(node->v.w);   return;
200         w_w:
201             print_chr(' ');     print_hex(node->u.w);
202             print_chr(' ');     print_hex(node->v.w);   return;
203         w_a:
204             print_chr(' ');     print_hex(node->u.w);
205             print_chr(' ');     print_arg(node->v.n);
206             return;
207         f_a:
208             print_chr(' ');
209             if (node->flag & jit_flag_data)
210                 print_flt(*(jit_float32_t *)node->u.n->u.w);
211             else
212                 print_flt(node->u.f);
213             print_chr(' ');     print_arg(node->v.n);
214             return;
215         d_a:
216             print_chr(' ');
217             if (node->flag & jit_flag_data)
218                 print_flt(*(jit_float64_t *)node->u.n->u.w);
219             else
220                 print_flt(node->u.d);
221             print_chr(' ');     print_arg(node->v.n);
222             return;
223         r_r_r:
224             print_chr(' ');     print_reg(node->u.w);
225             print_chr(' ');     print_reg(node->v.w);
226             print_chr(' ');     print_reg(node->w.w);   return;
227         r_r_w:
228             print_chr(' ');     print_reg(node->u.w);
229             print_chr(' ');     print_reg(node->v.w);
230             print_chr(' ');     print_hex(node->w.w);   return;
231         r_w_w:
232             print_chr(' ');     print_reg(node->u.w);
233             print_chr(' ');     print_hex(node->v.w);
234             print_chr(' ');
235             if (node->code == jit_code_movi_ww_d)
236                 print_hex(node->w.w);
237             else
238                 print_dec(node->w.w);                   return;
239         w_r_w:
240             print_chr(' ');     print_hex(node->u.w);
241             print_chr(' ');     print_reg(node->v.w);
242             print_chr(' ');     print_dec(node->w.w);   return;
243         q_r_r:
244             print_str(" (");    print_reg(node->u.q.l);
245             print_chr(' ');     print_reg(node->u.q.h);
246             print_str(") ");    print_reg(node->v.w);
247             print_chr(' ');     print_reg(node->w.w);   return;
248         q_r_w:
249             print_str(" (");    print_reg(node->u.q.l);
250             print_chr(' ');     print_reg(node->u.q.h);
251             print_str(") ");    print_reg(node->v.w);
252             print_chr(' ');     print_hex(node->w.w);   return;
253         r_r_q:
254             print_chr(' ');     print_reg(node->u.w);
255             print_chr(' ');     print_reg(node->v.w);
256             print_str(" (");    print_reg(node->w.q.l);
257             print_chr(' ');     print_reg(node->w.q.h);
258             print_str(") ");    return;
259         r_w_q:
260             print_chr(' ');     print_reg(node->u.w);
261             print_chr(' ');     print_hex(node->v.w);
262             print_str(" (");    print_reg(node->w.q.l);
263             print_chr(' ');     print_reg(node->w.q.h);
264             print_str(") ");    return;
265         r_r_iq:
266             print_chr(' ');     print_reg(node->u.w);
267             print_chr(' ');     print_reg(node->v.w);
268             print_str(" (");    print_int(node->w.q.l);
269             print_chr(' ');     print_int(node->w.q.h);
270             print_str(") ");    return;
271         r_w_iq:
272             print_chr(' ');     print_reg(node->u.w);
273             print_chr(' ');     print_hex(node->v.w);
274             print_str(" (");    print_int(node->w.q.l);
275             print_chr(' ');     print_int(node->w.q.h);
276             print_str(") ");    return;
277         r_q_r:
278             print_chr(' ');     print_reg(node->u.w);
279             print_str(" (");    print_reg(node->v.q.l);
280             print_chr(' ');     print_reg(node->v.q.h);
281             print_str(") ");    print_reg(node->w.w);
282             return;
283         r_r_f:
284             print_chr(' ');     print_reg(node->u.w);
285             print_chr(' ');     print_reg(node->v.w);
286             print_chr(' ');
287             if (node->flag & jit_flag_data)
288                 print_flt(*(jit_float32_t *)node->w.n->u.w);
289             else
290                 print_flt(node->w.f);
291             return;
292         r_q_f:
293             print_chr(' ');     print_reg(node->u.w);
294             print_str(" (");    print_reg(node->v.q.l);
295             print_chr(' ');     print_reg(node->v.q.h);
296             print_str(") ");
297             if (node->flag & jit_flag_data)
298                 print_flt(*(jit_float32_t *)node->w.n->u.w);
299             else
300                 print_flt(node->w.f);
301             return;
302         r_r_d:
303             print_chr(' ');     print_reg(node->u.w);
304             print_chr(' ');     print_reg(node->v.w);
305             print_chr(' ');
306             if (node->flag & jit_flag_data)
307                 print_flt(*(jit_float64_t *)node->w.n->u.w);
308             else
309                 print_flt(node->w.d);
310             return;
311         r_q_d:
312             print_chr(' ');     print_reg(node->u.w);
313             print_str(" (");    print_reg(node->v.q.l);
314             print_chr(' ');     print_reg(node->v.q.h);
315             print_str(") ");
316             if (node->flag & jit_flag_data)
317                 print_flt(*(jit_float64_t *)node->w.n->u.w);
318             else
319                 print_flt(node->w.d);
320             return;
321         w_r_r:
322             print_chr(' ');     print_hex(node->u.w);
323             print_chr(' ');     print_reg(node->v.w);
324             print_chr(' ');     print_reg(node->w.w);   return;
325         n_r_r:
326             print_chr(' ');
327             if (!(node->flag & jit_flag_node))
328                 print_ptr(node->u.p);
329             else {
330                 print_chr('L');
331                 print_dec(node->u.n->v.w);
332             }
333             print_chr(' ');     print_reg(node->v.w);
334             print_chr(' ');     print_reg(node->w.w);   return;
335         n_r_w:
336             print_chr(' ');
337             if (!(node->flag & jit_flag_node))
338                 print_ptr(node->u.p);
339             else {
340                 print_chr('L');
341                 print_dec(node->u.n->v.w);
342             }
343             print_chr(' ');     print_reg(node->v.w);
344             print_chr(' ');     print_hex(node->w.w);   return;
345         n_r_f:
346             print_chr(' ');
347             if (!(node->flag & jit_flag_node))
348                 print_ptr(node->u.p);
349             else{
350                 print_chr('L');
351                 print_dec(node->u.n->v.w);
352             }
353             print_chr(' ');     print_reg(node->v.w);
354             print_chr(' ');
355             if (node->flag & jit_flag_data)
356                 print_flt(*(jit_float32_t *)node->w.n->u.w);
357             else
358                 print_flt(node->w.f);
359             return;
360         n_r_d:
361             print_chr(' ');
362             if (!(node->flag & jit_flag_node))
363                 print_ptr(node->u.p);
364             else {
365                 print_chr('L');
366                 print_dec(node->u.n->v.w);
367             }
368             print_chr(' ');     print_reg(node->v.w);
369             print_chr(' ');
370             if (node->flag & jit_flag_data)
371                 print_flt(*(jit_float64_t *)node->w.n->u.w);
372             else
373                 print_flt(node->w.d);
374             return;
375         case jit_code_name:
376             print_chr(' ');
377             if (node->v.p && _jitc->emit)
378                 print_str((char *)node->v.n->u.p);
379             break;
380         case jit_code_note:
381             print_chr(' ');
382             if (node->v.p && _jitc->emit)
383                 print_str((char *)node->v.n->u.p);
384             if (node->v.p && _jitc->emit && node->w.w)
385                 print_chr(':');
386             if (node->w.w)
387                 print_dec(node->w.w);
388             break;
389         case jit_code_data:
390         case jit_code_label:
391         case jit_code_ellipsis:
392         case jit_code_prolog:   case jit_code_epilog:
393         case jit_code_ret:      case jit_code_prepare:
394             break;
395         case jit_code_save:     case jit_code_load:
396             goto r;
397         default:
398             switch (value) {
399                 case jit_cc_a0_reg:
400                 case jit_cc_a0_reg|jit_cc_a0_chg:
401                 case jit_cc_a0_reg|jit_cc_a0_jmp:
402                     goto r;
403                 case jit_cc_a0_int:
404                     goto w;
405                 case jit_cc_a0_flt:
406                     goto f;
407                 case jit_cc_a0_dbl:
408                     goto d;
409                 case jit_cc_a0_jmp:
410                     goto n;
411                 case jit_cc_a0_int|jit_cc_a0_arg:
412                     goto a;
413                 case jit_cc_a0_reg|jit_cc_a1_reg:
414                     goto r_r;
415                 case jit_cc_a0_reg|jit_cc_a1_int:
416                     goto r_w;
417                 case jit_cc_a0_reg|jit_cc_a1_flt:
418                     goto r_f;
419                 case jit_cc_a0_reg|jit_cc_a1_dbl:
420                     goto r_d;
421                 case jit_cc_a0_reg|jit_cc_a1_arg:
422                     goto r_a;
423                 case jit_cc_a0_int|jit_cc_a1_reg:
424                     goto w_r;
425                 case jit_cc_a0_int|jit_cc_a1_int:
426                     goto w_w;
427                 case jit_cc_a0_int|jit_cc_a1_arg:
428                     goto w_a;
429                 case jit_cc_a0_flt|jit_cc_a1_arg:
430                     goto f_a;
431                 case jit_cc_a0_dbl|jit_cc_a1_arg:
432                     goto d_a;
433                 case jit_cc_a0_reg|jit_cc_a1_reg|jit_cc_a2_reg:
434                     goto r_r_r;
435                 case jit_cc_a0_reg|jit_cc_a1_reg|jit_cc_a2_int:
436                     goto r_r_w;
437                 case jit_cc_a0_reg|jit_cc_a1_int|jit_cc_a2_int:
438                     goto r_w_w;
439                 case jit_cc_a0_int|jit_cc_a1_reg|jit_cc_a2_int:
440                     goto w_r_w;
441                 case jit_cc_a0_reg|jit_cc_a0_rlh|
442                      jit_cc_a1_reg|jit_cc_a2_reg:
443                     goto q_r_r;
444                 case jit_cc_a0_reg|jit_cc_a0_rlh|
445                      jit_cc_a1_reg|jit_cc_a2_int:
446                     goto q_r_w;
447                 case jit_cc_a0_reg|jit_cc_a1_reg|
448                     jit_cc_a2_reg|jit_cc_a2_rlh:
449                     goto r_r_q;
450                 case jit_cc_a0_reg|jit_cc_a1_int|
451                     jit_cc_a2_reg|jit_cc_a2_rlh:
452                     goto r_w_q;
453                 case jit_cc_a0_reg|jit_cc_a1_reg|jit_cc_a2_rlh:
454                     goto r_r_iq;
455                 case jit_cc_a0_reg|jit_cc_a1_int|jit_cc_a2_rlh:
456                     goto r_w_iq;
457                 case jit_cc_a0_reg|jit_cc_a1_reg|jit_cc_a2_flt:
458                     goto r_r_f;
459                 case jit_cc_a0_reg|jit_cc_a1_reg|jit_cc_a2_dbl:
460                     goto r_r_d;
461                 case jit_cc_a0_int|jit_cc_a1_reg|jit_cc_a2_reg:
462                     goto w_r_r;
463                 case jit_cc_a0_jmp|jit_cc_a1_reg|jit_cc_a2_reg:
464                     goto n_r_r;
465                 case jit_cc_a0_jmp|jit_cc_a1_reg|jit_cc_a2_int:
466                     goto n_r_w;
467                 case jit_cc_a0_jmp|jit_cc_a1_reg|jit_cc_a2_flt:
468                     goto n_r_f;
469                 case jit_cc_a0_jmp|jit_cc_a1_reg|jit_cc_a2_dbl:
470                     goto n_r_d;
471                 case jit_cc_a0_reg|jit_cc_a1_reg|jit_cc_a1_rlh|jit_cc_a2_reg:
472                     goto r_q_r;
473                 case jit_cc_a0_reg|jit_cc_a1_reg|jit_cc_a1_rlh|jit_cc_a2_flt:
474                     goto r_q_f;
475                 case jit_cc_a0_reg|jit_cc_a1_reg|jit_cc_a1_rlh|jit_cc_a2_dbl:
476                     goto r_q_d;
477                 default:
478                     abort();
479             }
480             break;
481     }
482 }