aboutsummaryrefslogtreecommitdiff
path: root/deps/lightning/lib/jit_print.c
blob: 3d7c0ac6b5021faeffeb35cc8b7e12c99030a3f9 (plain)
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
/*
 * Copyright (C) 2012-2019  Free Software Foundation, Inc.
 *
 * This file is part of GNU lightning.
 *
 * GNU lightning is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * GNU lightning is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 *
 * Authors:
 *	Paulo Cesar Pereira de Andrade
 */

#include <lightning.h>
#include <lightning/jit_private.h>

#define print_chr(value)		fputc(value, stdout)
#define print_hex(value)		fprintf(stdout, "0x%lx", value)
#define print_dec(value)		fprintf(stdout, "%ld", value)
#define print_flt(value)		fprintf(stdout, "%g", value)
#define print_str(value)		fprintf(stdout, "%s", value)
#define print_ptr(value)		fprintf(stdout, "%p", value)
#define print_reg(value)						\
    do {								\
	if ((value) & jit_regno_patch)					\
	    print_chr('?');						\
	print_str(_rvs[jit_regno(value)].name);				\
    } while (0)
#define print_arg(value)						\
    do {								\
	print_chr('#');							\
	if (value)							\
	    print_dec((value)->v.w);					\
	else								\
	    print_chr('?');						\
    } while (0)

/*
 * Initialization
 */
#include "jit_names.c"

/*
 * Implementation
 */
void
_jit_print(jit_state_t *_jit)
{
    jit_node_t		*node;

    if ((node = _jitc->head)) {
	jit_print_node(node);
	for (node = node->next; node; node = node->next) {
	    print_chr('\n');
	    jit_print_node(node);
	}
	print_chr('\n');
    }
}

void
_jit_print_node(jit_state_t *_jit, jit_node_t *node)
{
    jit_block_t		*block;
    jit_int32_t		 value;
    jit_int32_t		 offset;

    if (node->code == jit_code_label ||
	node->code == jit_code_prolog || node->code == jit_code_epilog) {
	print_chr('L');
	print_dec(node->v.w);
	print_chr(':');
	block = _jitc->blocks.ptr + node->v.w;
	for (offset = 0; offset < _jitc->reglen; offset++) {
	    if (jit_regset_tstbit(&block->reglive, offset)) {
		print_chr(' ');
		print_reg(offset);
	    }
	}
	if (node->code == jit_code_prolog ||
	    node->code == jit_code_epilog) {
	    print_str(" /* ");
	    print_str(code_name[node->code]);
	    print_str(" */");
	}
	return;
    }
    value = jit_classify(node->code) &
	(jit_cc_a0_int|jit_cc_a0_flt|jit_cc_a0_dbl|jit_cc_a0_jmp|
	 jit_cc_a0_reg|jit_cc_a0_rlh|jit_cc_a0_arg|
	 jit_cc_a1_reg|jit_cc_a1_int|jit_cc_a1_flt|jit_cc_a1_dbl|jit_cc_a1_arg|
	 jit_cc_a2_reg|jit_cc_a2_int|jit_cc_a2_flt|jit_cc_a2_dbl);
    if (!(node->flag & jit_flag_synth) && ((value & jit_cc_a0_jmp) ||
					   node->code == jit_code_finishr ||
					   node->code == jit_code_finishi))
	print_str("    ");
    else
	print_chr('\t');
    if (node->flag & jit_flag_synth)
	print_str(" \\__ ");
    print_str(code_name[node->code]);
    switch (node->code) {
	r:
	    print_chr(' ');	print_reg(node->u.w);	return;
	w:
	    print_chr(' ');	print_hex(node->u.w);	return;
	f:
	    print_chr(' ');
	    if (node->flag & jit_flag_data)
		print_flt(*(jit_float32_t *)node->u.n->u.w);
	    else
		print_flt(node->u.f);
	    return;
	d:
	    print_chr(' ');
	    if (node->flag & jit_flag_data)
		print_flt(*(jit_float64_t *)node->u.n->u.w);
	    else
		print_flt(node->u.d);
	    return;
	n:
	    print_chr(' ');
	    if (!(node->flag & jit_flag_node))
		print_ptr(node->u.p);
	    else {
		print_chr('L');
		print_dec(node->u.n->v.w);
	    }
	    return;
	a:
	    print_chr(' ');	print_arg(node);	return;
	r_r:
	    print_chr(' ');	print_reg(node->u.w);
	    print_chr(' ');	print_reg(node->v.w);	return;
	r_w:
	    print_chr(' ');	print_reg(node->u.w);
	    print_chr(' ');	print_hex(node->v.w);	return;
	r_f:
	    print_chr(' ');	print_reg(node->u.w);
	    print_chr(' ');
	    if (node->flag & jit_flag_data)
		print_flt(*(jit_float32_t *)node->v.n->u.w);
	    else
		print_flt(node->v.f);
	    return;
	r_d:
	    print_chr(' ');	print_reg(node->u.w);
	    print_chr(' ');
	    if (node->flag & jit_flag_data)
		print_flt(*(jit_float64_t *)node->v.n->u.w);
	    else
		print_flt(node->v.d);
	    return;
	r_a:
	    print_chr(' ');	print_reg(node->u.w);
	    print_chr(' ');	print_arg(node->v.n);
	    return;
	w_r:
	    print_chr(' ');	print_hex(node->u.w);
	    print_chr(' ');	print_reg(node->v.w);   return;
	w_w:
	    print_chr(' ');	print_hex(node->u.w);
	    print_chr(' ');	print_hex(node->v.w);   return;
	w_a:
	    print_chr(' ');	print_hex(node->u.w);
	    print_chr(' ');	print_arg(node->v.n);
	    return;
	f_a:
	    print_chr(' ');
	    if (node->flag & jit_flag_data)
		print_flt(*(jit_float32_t *)node->u.n->u.w);
	    else
		print_flt(node->u.f);
	    print_chr(' ');	print_arg(node->v.n);
	    return;
	d_a:
	    print_chr(' ');
	    if (node->flag & jit_flag_data)
		print_flt(*(jit_float64_t *)node->u.n->u.w);
	    else
		print_flt(node->u.d);
	    print_chr(' ');	print_arg(node->v.n);
	    return;
	r_r_r:
	    print_chr(' ');	print_reg(node->u.w);
	    print_chr(' ');	print_reg(node->v.w);
	    print_chr(' ');	print_reg(node->w.w);   return;
	r_r_w:
	    print_chr(' ');	print_reg(node->u.w);
	    print_chr(' ');	print_reg(node->v.w);
	    print_chr(' ');	print_hex(node->w.w);   return;
	q_r_r:
	    print_str(" (");	print_reg(node->u.q.l);
	    print_chr(' ');	print_reg(node->u.q.h);
	    print_str(") ");	print_reg(node->v.w);
	    print_chr(' ');	print_reg(node->w.w);   return;
	q_r_w:
	    print_str(" (");	print_reg(node->u.q.l);
	    print_chr(' ');	print_reg(node->u.q.h);
	    print_str(") ");	print_reg(node->v.w);
	    print_chr(' ');	print_hex(node->w.w);   return;
	r_r_f:
	    print_chr(' ');	print_reg(node->u.w);
	    print_chr(' ');	print_reg(node->v.w);
	    print_chr(' ');
	    if (node->flag & jit_flag_data)
		print_flt(*(jit_float32_t *)node->w.n->u.w);
	    else
		print_flt(node->w.f);
	    return;
	r_r_d:
	    print_chr(' ');	print_reg(node->u.w);
	    print_chr(' ');	print_reg(node->v.w);
	    print_chr(' ');
	    if (node->flag & jit_flag_data)
		print_flt(*(jit_float64_t *)node->w.n->u.w);
	    else
		print_flt(node->w.d);
	    return;
	w_r_r:
	    print_chr(' ');	print_hex(node->u.w);
	    print_chr(' ');	print_reg(node->v.w);
	    print_chr(' ');	print_reg(node->w.w);   return;
	n_r_r:
	    print_chr(' ');
	    if (!(node->flag & jit_flag_node))
		print_ptr(node->u.p);
	    else {
		print_chr('L');
		print_dec(node->u.n->v.w);
	    }
	    print_chr(' ');	print_reg(node->v.w);
	    print_chr(' ');	print_reg(node->w.w);   return;
	n_r_w:
	    print_chr(' ');
	    if (!(node->flag & jit_flag_node))
		print_ptr(node->u.p);
	    else {
		print_chr('L');
		print_dec(node->u.n->v.w);
	    }
	    print_chr(' ');	print_reg(node->v.w);
	    print_chr(' ');	print_hex(node->w.w);   return;
	n_r_f:
	    print_chr(' ');
	    if (!(node->flag & jit_flag_node))
		print_ptr(node->u.p);
	    else{
		print_chr('L');
		print_dec(node->u.n->v.w);
	    }
	    print_chr(' ');	print_reg(node->v.w);
	    print_chr(' ');
	    if (node->flag & jit_flag_data)
		print_flt(*(jit_float32_t *)node->w.n->u.w);
	    else
		print_flt(node->w.f);
	    return;
	n_r_d:
	    print_chr(' ');
	    if (!(node->flag & jit_flag_node))
		print_ptr(node->u.p);
	    else {
		print_chr('L');
		print_dec(node->u.n->v.w);
	    }
	    print_chr(' ');	print_reg(node->v.w);
	    print_chr(' ');
	    if (node->flag & jit_flag_data)
		print_flt(*(jit_float64_t *)node->w.n->u.w);
	    else
		print_flt(node->w.d);
	    return;
	case jit_code_name:
	    print_chr(' ');
	    if (node->v.p && _jitc->emit)
		print_str(node->v.n->u.p);
	    break;
	case jit_code_note:
	    print_chr(' ');
	    if (node->v.p && _jitc->emit)
		print_str(node->v.n->u.p);
	    if (node->v.p && _jitc->emit && node->w.w)
		print_chr(':');
	    if (node->w.w)
		print_dec(node->w.w);
	    break;
	case jit_code_data:
	case jit_code_label:
	case jit_code_ellipsis:
	case jit_code_prolog:	case jit_code_epilog:
	case jit_code_ret:	case jit_code_prepare:
	    break;
	case jit_code_save:	case jit_code_load:
	    goto r;
	default:
	    switch (value) {
		case jit_cc_a0_reg:
		case jit_cc_a0_reg|jit_cc_a0_chg:
		case jit_cc_a0_reg|jit_cc_a0_jmp:
		    goto r;
		case jit_cc_a0_int:
		    goto w;
		case jit_cc_a0_flt:
		    goto f;
		case jit_cc_a0_dbl:
		    goto d;
		case jit_cc_a0_jmp:
		    goto n;
		case jit_cc_a0_int|jit_cc_a0_arg:
		    goto a;
		case jit_cc_a0_reg|jit_cc_a1_reg:
		    goto r_r;
		case jit_cc_a0_reg|jit_cc_a1_int:
		    goto r_w;
		case jit_cc_a0_reg|jit_cc_a1_flt:
		    goto r_f;
		case jit_cc_a0_reg|jit_cc_a1_dbl:
		    goto r_d;
		case jit_cc_a0_reg|jit_cc_a1_arg:
		    goto r_a;
		case jit_cc_a0_int|jit_cc_a1_reg:
		    goto w_r;
		case jit_cc_a0_int|jit_cc_a1_int:
		    goto w_w;
		case jit_cc_a0_int|jit_cc_a1_arg:
		    goto w_a;
		case jit_cc_a0_flt|jit_cc_a1_arg:
		    goto f_a;
		case jit_cc_a0_dbl|jit_cc_a1_arg:
		    goto d_a;
		case jit_cc_a0_reg|jit_cc_a1_reg|jit_cc_a2_reg:
		    goto r_r_r;
		case jit_cc_a0_reg|jit_cc_a1_reg|jit_cc_a2_int:
		    goto r_r_w;
		case jit_cc_a0_reg|jit_cc_a0_rlh|
		     jit_cc_a1_reg|jit_cc_a2_reg:
		    goto q_r_r;
		case jit_cc_a0_reg|jit_cc_a0_rlh|
		     jit_cc_a1_reg|jit_cc_a2_int:
		    goto q_r_w;
		case jit_cc_a0_reg|jit_cc_a1_reg|jit_cc_a2_flt:
		    goto r_r_f;
		case jit_cc_a0_reg|jit_cc_a1_reg|jit_cc_a2_dbl:
		    goto r_r_d;
		case jit_cc_a0_int|jit_cc_a1_reg|jit_cc_a2_reg:
		    goto w_r_r;
		case jit_cc_a0_jmp|jit_cc_a1_reg|jit_cc_a2_reg:
		    goto n_r_r;
		case jit_cc_a0_jmp|jit_cc_a1_reg|jit_cc_a2_int:
		    goto n_r_w;
		case jit_cc_a0_jmp|jit_cc_a1_reg|jit_cc_a2_flt:
		    goto n_r_f;
		case jit_cc_a0_jmp|jit_cc_a1_reg|jit_cc_a2_dbl:
		    goto n_r_d;
		default:
		    abort();
	    }
	    break;
    }
}