Merge pull request #718 from pcercuei/update-lightrec-20230224
[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 DEC_FMT               "%d"
26 #  define HEX_FMT               "0x%x"
27 #else
28 #  define MININT                0x8000000000000000
29 #  define DEC_FMT               "%ld"
30 #  define HEX_FMT               "0x%lx"
31 #endif
32
33
34 #define print_chr(value)                fputc(value, print_stream)
35 #define print_hex(value)                                                \
36     do {                                                                \
37         if (value < 0 && value != MININT)                               \
38             fprintf(print_stream, "-" HEX_FMT, (jit_uword_t)-value);    \
39         else                                                            \
40             fprintf(print_stream, HEX_FMT, (jit_uword_t)value);         \
41     } while (0)
42 #define print_dec(value)                fprintf(print_stream, DEC_FMT, value)
43 #define print_flt(value)                fprintf(print_stream, "%g", value)
44 #define print_str(value)                fprintf(print_stream, "%s", value)
45 #define print_ptr(value)                fprintf(print_stream, "%p", value)
46 #define print_reg(value)                                                \
47     do {                                                                \
48         if ((value) & jit_regno_patch)                                  \
49             print_chr('?');                                             \
50         print_str(_rvs[jit_regno(value)].name);                         \
51     } while (0)
52 #define print_arg(value)                                                \
53     do {                                                                \
54         print_chr('#');                                                 \
55         if (value)                                                      \
56             print_dec((value)->v.w);                                    \
57         else                                                            \
58             print_chr('?');                                             \
59     } while (0)
60
61 /*
62  * Initialization
63  */
64 #include "jit_names.c"
65 /*
66  * Initialization
67  */
68 static FILE     *print_stream;
69
70
71 /*
72  * Implementation
73  */
74 void
75 jit_init_print(void)
76 {
77     if (!print_stream)
78         print_stream = stdout;
79 }
80
81 void
82 _jit_print(jit_state_t *_jit)
83 {
84     jit_node_t          *node;
85
86     if ((node = _jitc->head)) {
87         jit_print_node(node);
88         for (node = node->next; node; node = node->next) {
89             print_chr('\n');
90             jit_print_node(node);
91         }
92         print_chr('\n');
93     }
94 }
95
96 void
97 _jit_print_node(jit_state_t *_jit, jit_node_t *node)
98 {
99     jit_block_t         *block;
100     jit_int32_t          value;
101     jit_int32_t          offset;
102
103     if (node->code == jit_code_label ||
104         node->code == jit_code_prolog || node->code == jit_code_epilog) {
105         print_chr('L');
106         print_dec(node->v.w);
107         print_chr(':');
108         block = _jitc->blocks.ptr + node->v.w;
109         for (offset = 0; offset < _jitc->reglen; offset++) {
110             if (jit_regset_tstbit(&block->reglive, offset)) {
111                 print_chr(' ');
112                 print_reg(offset);
113             }
114         }
115         if (node->code == jit_code_prolog ||
116             node->code == jit_code_epilog) {
117             print_str(" /* ");
118             print_str(code_name[node->code]);
119             print_str(" */");
120         }
121         return;
122     }
123     value = jit_classify(node->code) &
124         (jit_cc_a0_int|jit_cc_a0_flt|jit_cc_a0_dbl|jit_cc_a0_jmp|
125          jit_cc_a0_reg|jit_cc_a0_rlh|jit_cc_a0_arg|
126          jit_cc_a1_reg|jit_cc_a1_int|jit_cc_a1_flt|jit_cc_a1_dbl|jit_cc_a1_arg|
127          jit_cc_a2_reg|jit_cc_a2_int|jit_cc_a2_flt|jit_cc_a2_dbl|jit_cc_a2_rlh);
128     if (!(node->flag & jit_flag_synth) && ((value & jit_cc_a0_jmp) ||
129                                            node->code == jit_code_finishr ||
130                                            node->code == jit_code_finishi))
131         print_str("    ");
132     else
133         print_chr('\t');
134     if (node->flag & jit_flag_synth)
135         print_str(" \\__ ");
136     print_str(code_name[node->code]);
137     switch (node->code) {
138         r:
139             print_chr(' ');     print_reg(node->u.w);   return;
140         w:
141             print_chr(' ');     print_hex(node->u.w);   return;
142         f:
143             print_chr(' ');
144             if (node->flag & jit_flag_data)
145                 print_flt(*(jit_float32_t *)node->u.n->u.w);
146             else
147                 print_flt(node->u.f);
148             return;
149         d:
150             print_chr(' ');
151             if (node->flag & jit_flag_data)
152                 print_flt(*(jit_float64_t *)node->u.n->u.w);
153             else
154                 print_flt(node->u.d);
155             return;
156         n:
157             print_chr(' ');
158             if (!(node->flag & jit_flag_node))
159                 print_ptr(node->u.p);
160             else {
161                 print_chr('L');
162                 print_dec(node->u.n->v.w);
163             }
164             return;
165         a:
166             print_chr(' ');     print_arg(node);        return;
167         r_r:
168             print_chr(' ');     print_reg(node->u.w);
169             print_chr(' ');     print_reg(node->v.w);   return;
170         r_w:
171             print_chr(' ');     print_reg(node->u.w);
172             print_chr(' ');     print_hex(node->v.w);   return;
173         r_f:
174             print_chr(' ');     print_reg(node->u.w);
175             print_chr(' ');
176             if (node->flag & jit_flag_data)
177                 print_flt(*(jit_float32_t *)node->v.n->u.w);
178             else
179                 print_flt(node->v.f);
180             return;
181         r_d:
182             print_chr(' ');     print_reg(node->u.w);
183             print_chr(' ');
184             if (node->flag & jit_flag_data)
185                 print_flt(*(jit_float64_t *)node->v.n->u.w);
186             else
187                 print_flt(node->v.d);
188             return;
189         r_a:
190             print_chr(' ');     print_reg(node->u.w);
191             print_chr(' ');     print_arg(node->v.n);
192             return;
193         w_r:
194             print_chr(' ');     print_hex(node->u.w);
195             print_chr(' ');     print_reg(node->v.w);   return;
196         w_w:
197             print_chr(' ');     print_hex(node->u.w);
198             print_chr(' ');     print_hex(node->v.w);   return;
199         w_a:
200             print_chr(' ');     print_hex(node->u.w);
201             print_chr(' ');     print_arg(node->v.n);
202             return;
203         f_a:
204             print_chr(' ');
205             if (node->flag & jit_flag_data)
206                 print_flt(*(jit_float32_t *)node->u.n->u.w);
207             else
208                 print_flt(node->u.f);
209             print_chr(' ');     print_arg(node->v.n);
210             return;
211         d_a:
212             print_chr(' ');
213             if (node->flag & jit_flag_data)
214                 print_flt(*(jit_float64_t *)node->u.n->u.w);
215             else
216                 print_flt(node->u.d);
217             print_chr(' ');     print_arg(node->v.n);
218             return;
219         r_r_r:
220             print_chr(' ');     print_reg(node->u.w);
221             print_chr(' ');     print_reg(node->v.w);
222             print_chr(' ');     print_reg(node->w.w);   return;
223         r_r_w:
224             print_chr(' ');     print_reg(node->u.w);
225             print_chr(' ');     print_reg(node->v.w);
226             print_chr(' ');     print_hex(node->w.w);   return;
227         q_r_r:
228             print_str(" (");    print_reg(node->u.q.l);
229             print_chr(' ');     print_reg(node->u.q.h);
230             print_str(") ");    print_reg(node->v.w);
231             print_chr(' ');     print_reg(node->w.w);   return;
232         q_r_w:
233             print_str(" (");    print_reg(node->u.q.l);
234             print_chr(' ');     print_reg(node->u.q.h);
235             print_str(") ");    print_reg(node->v.w);
236             print_chr(' ');     print_hex(node->w.w);   return;
237         r_r_q:
238             print_chr(' ');     print_reg(node->u.w);
239             print_chr(' ');     print_reg(node->v.w);
240             print_str(" (");    print_reg(node->w.q.l);
241             print_chr(' ');     print_reg(node->w.q.h);
242             print_str(") ");    return;
243         r_w_q:
244             print_chr(' ');     print_reg(node->u.w);
245             print_chr(' ');     print_hex(node->v.w);
246             print_str(" (");    print_reg(node->w.q.l);
247             print_chr(' ');     print_reg(node->w.q.h);
248             print_str(") ");    return;
249         r_r_f:
250             print_chr(' ');     print_reg(node->u.w);
251             print_chr(' ');     print_reg(node->v.w);
252             print_chr(' ');
253             if (node->flag & jit_flag_data)
254                 print_flt(*(jit_float32_t *)node->w.n->u.w);
255             else
256                 print_flt(node->w.f);
257             return;
258         r_r_d:
259             print_chr(' ');     print_reg(node->u.w);
260             print_chr(' ');     print_reg(node->v.w);
261             print_chr(' ');
262             if (node->flag & jit_flag_data)
263                 print_flt(*(jit_float64_t *)node->w.n->u.w);
264             else
265                 print_flt(node->w.d);
266             return;
267         w_r_r:
268             print_chr(' ');     print_hex(node->u.w);
269             print_chr(' ');     print_reg(node->v.w);
270             print_chr(' ');     print_reg(node->w.w);   return;
271         n_r_r:
272             print_chr(' ');
273             if (!(node->flag & jit_flag_node))
274                 print_ptr(node->u.p);
275             else {
276                 print_chr('L');
277                 print_dec(node->u.n->v.w);
278             }
279             print_chr(' ');     print_reg(node->v.w);
280             print_chr(' ');     print_reg(node->w.w);   return;
281         n_r_w:
282             print_chr(' ');
283             if (!(node->flag & jit_flag_node))
284                 print_ptr(node->u.p);
285             else {
286                 print_chr('L');
287                 print_dec(node->u.n->v.w);
288             }
289             print_chr(' ');     print_reg(node->v.w);
290             print_chr(' ');     print_hex(node->w.w);   return;
291         n_r_f:
292             print_chr(' ');
293             if (!(node->flag & jit_flag_node))
294                 print_ptr(node->u.p);
295             else{
296                 print_chr('L');
297                 print_dec(node->u.n->v.w);
298             }
299             print_chr(' ');     print_reg(node->v.w);
300             print_chr(' ');
301             if (node->flag & jit_flag_data)
302                 print_flt(*(jit_float32_t *)node->w.n->u.w);
303             else
304                 print_flt(node->w.f);
305             return;
306         n_r_d:
307             print_chr(' ');
308             if (!(node->flag & jit_flag_node))
309                 print_ptr(node->u.p);
310             else {
311                 print_chr('L');
312                 print_dec(node->u.n->v.w);
313             }
314             print_chr(' ');     print_reg(node->v.w);
315             print_chr(' ');
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         case jit_code_name:
322             print_chr(' ');
323             if (node->v.p && _jitc->emit)
324                 print_str((char *)node->v.n->u.p);
325             break;
326         case jit_code_note:
327             print_chr(' ');
328             if (node->v.p && _jitc->emit)
329                 print_str((char *)node->v.n->u.p);
330             if (node->v.p && _jitc->emit && node->w.w)
331                 print_chr(':');
332             if (node->w.w)
333                 print_dec(node->w.w);
334             break;
335         case jit_code_data:
336         case jit_code_label:
337         case jit_code_ellipsis:
338         case jit_code_prolog:   case jit_code_epilog:
339         case jit_code_ret:      case jit_code_prepare:
340             break;
341         case jit_code_save:     case jit_code_load:
342             goto r;
343         default:
344             switch (value) {
345                 case jit_cc_a0_reg:
346                 case jit_cc_a0_reg|jit_cc_a0_chg:
347                 case jit_cc_a0_reg|jit_cc_a0_jmp:
348                     goto r;
349                 case jit_cc_a0_int:
350                     goto w;
351                 case jit_cc_a0_flt:
352                     goto f;
353                 case jit_cc_a0_dbl:
354                     goto d;
355                 case jit_cc_a0_jmp:
356                     goto n;
357                 case jit_cc_a0_int|jit_cc_a0_arg:
358                     goto a;
359                 case jit_cc_a0_reg|jit_cc_a1_reg:
360                     goto r_r;
361                 case jit_cc_a0_reg|jit_cc_a1_int:
362                     goto r_w;
363                 case jit_cc_a0_reg|jit_cc_a1_flt:
364                     goto r_f;
365                 case jit_cc_a0_reg|jit_cc_a1_dbl:
366                     goto r_d;
367                 case jit_cc_a0_reg|jit_cc_a1_arg:
368                     goto r_a;
369                 case jit_cc_a0_int|jit_cc_a1_reg:
370                     goto w_r;
371                 case jit_cc_a0_int|jit_cc_a1_int:
372                     goto w_w;
373                 case jit_cc_a0_int|jit_cc_a1_arg:
374                     goto w_a;
375                 case jit_cc_a0_flt|jit_cc_a1_arg:
376                     goto f_a;
377                 case jit_cc_a0_dbl|jit_cc_a1_arg:
378                     goto d_a;
379                 case jit_cc_a0_reg|jit_cc_a1_reg|jit_cc_a2_reg:
380                     goto r_r_r;
381                 case jit_cc_a0_reg|jit_cc_a1_reg|jit_cc_a2_int:
382                     goto r_r_w;
383                 case jit_cc_a0_reg|jit_cc_a0_rlh|
384                      jit_cc_a1_reg|jit_cc_a2_reg:
385                     goto q_r_r;
386                 case jit_cc_a0_reg|jit_cc_a0_rlh|
387                      jit_cc_a1_reg|jit_cc_a2_int:
388                     goto q_r_w;
389                 case jit_cc_a0_reg|jit_cc_a1_reg|
390                     jit_cc_a2_reg|jit_cc_a2_rlh:
391                     goto r_r_q;
392                 case jit_cc_a0_reg|jit_cc_a1_int|
393                     jit_cc_a2_reg|jit_cc_a2_rlh:
394                     goto r_w_q;
395                 case jit_cc_a0_reg|jit_cc_a1_reg|jit_cc_a2_flt:
396                     goto r_r_f;
397                 case jit_cc_a0_reg|jit_cc_a1_reg|jit_cc_a2_dbl:
398                     goto r_r_d;
399                 case jit_cc_a0_int|jit_cc_a1_reg|jit_cc_a2_reg:
400                     goto w_r_r;
401                 case jit_cc_a0_jmp|jit_cc_a1_reg|jit_cc_a2_reg:
402                     goto n_r_r;
403                 case jit_cc_a0_jmp|jit_cc_a1_reg|jit_cc_a2_int:
404                     goto n_r_w;
405                 case jit_cc_a0_jmp|jit_cc_a1_reg|jit_cc_a2_flt:
406                     goto n_r_f;
407                 case jit_cc_a0_jmp|jit_cc_a1_reg|jit_cc_a2_dbl:
408                     goto n_r_d;
409                 default:
410                     abort();
411             }
412             break;
413     }
414 }