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