2 * Copyright (C) 2014-2023 Free Software Foundation, Inc.
4 * This file is part of GNU lightning.
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)
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.
17 * Paulo Cesar Pereira de Andrade
20 #define jit_arg_reg_p(i) ((i) >= 0 && (i) < 6)
21 #define jit_arg_f_reg_p(i) ((i) >= 0 && (i) < 6)
22 #if __BYTE_ORDER == __LITTLE_ENDIAN
28 # define C_DISP 8 - sizeof(jit_int8_t)
29 # define S_DISP 8 - sizeof(jit_int16_t)
30 # define I_DISP 8 - sizeof(jit_int32_t)
31 # define F_DISP 8 - sizeof(jit_float32_t)
38 * What I could understand from gcc/config/alpha/alpha.c:alpha_build_builtin_va_list()
39 * and other helpers, as well as objdump of simple test programs; could not
40 * get gdb working on the test system I had access...
42 * base-48 to base is where up to 6 float registers are saved.
43 * base to base+48 is where up to 6 integer registers are saved.
44 * base+48... is where varargs arguments are stored.
50 * load(reg, base, offset);
53 typedef struct jit_va_list {
61 #define patch(instr, node) _patch(_jit, instr, node)
62 static void _patch(jit_state_t*,jit_word_t,jit_node_t*);
65 # include "jit_alpha-cpu.c"
66 # include "jit_alpha-fpu.c"
67 # include "jit_fallback.c"
73 jit_register_t _rvs[] = {
74 { rc(gpr) | 0x1c, "at" },
75 { rc(gpr) | 0x00, "v0" },
76 { rc(gpr) | 0x01, "t0" },
77 { rc(gpr) | 0x02, "t1" },
78 { rc(gpr) | 0x03, "t2" },
79 { rc(gpr) | 0x04, "t3" },
80 { rc(gpr) | 0x05, "t4" },
81 { rc(gpr) | 0x06, "t5" },
82 { rc(gpr) | 0x07, "t6" },
83 { rc(gpr) | 0x08, "t7" },
84 { rc(gpr) | 0x16, "t8" },
85 { rc(gpr) | 0x17, "t9" },
86 { rc(gpr) | 0x18, "t10" },
87 { rc(gpr) | 0x19, "t11" },
88 { rc(sav) | rc(gpr) | 0x09, "s0" },
89 { rc(sav) | rc(gpr) | 0x0a, "s1" },
90 { rc(sav) | rc(gpr) | 0x0b, "s2" },
91 { rc(sav) | rc(gpr) | 0x0c, "s3" },
92 { rc(sav) | rc(gpr) | 0x0d, "s4" },
93 { rc(sav) | rc(gpr) | 0x0e, "s5" },
95 { rc(arg) | rc(gpr) | 0x15, "a5" },
96 { rc(arg) | rc(gpr) | 0x14, "a4" },
97 { rc(arg) | rc(gpr) | 0x13, "a3" },
98 { rc(arg) | rc(gpr) | 0x12, "a2" },
99 { rc(arg) | rc(gpr) | 0x11, "a1" },
100 { rc(arg) | rc(gpr) | 0x10, "a0" },
106 { rc(fpr) | 0x00, "$f0" },
107 { rc(fpr) | 0x01, "$f1" },
108 { rc(sav) | rc(fpr) | 0x02, "$f2" },
109 { rc(sav) | rc(fpr) | 0x03, "$f3" },
110 { rc(sav) | rc(fpr) | 0x04, "$f4" },
111 { rc(sav) | rc(fpr) | 0x05, "$f5" },
112 { rc(sav) | rc(fpr) | 0x06, "$f6" },
113 { rc(sav) | rc(fpr) | 0x07, "$f7" },
114 { rc(sav) | rc(fpr) | 0x08, "$f8" },
115 { rc(sav) | rc(fpr) | 0x09, "$f9" },
116 { rc(fpr) | 0x0a, "$f10" },
117 { rc(fpr) | 0x0b, "$f11" },
118 { rc(fpr) | 0x0c, "$f12" },
119 { rc(fpr) | 0x0d, "$f13" },
120 { rc(fpr) | 0x0e, "$f14" },
121 { rc(fpr) | 0x0f, "$f15" },
122 { rc(arg) | rc(fpr) | 0x15, "$f21" },
123 { rc(arg) | rc(fpr) | 0x14, "$f20" },
124 { rc(arg) | rc(fpr) | 0x13, "$f19" },
125 { rc(arg) | rc(fpr) | 0x12, "$f18" },
126 { rc(arg) | rc(fpr) | 0x11, "$f17" },
127 { rc(arg) | rc(fpr) | 0x10, "$f16" },
128 { rc(fpr) | 0x16, "$f22" },
129 { rc(fpr) | 0x17, "$f23" },
130 { rc(fpr) | 0x18, "$f24" },
131 { rc(fpr) | 0x19, "$f25" },
132 { rc(fpr) | 0x1a, "$f26" },
133 { rc(fpr) | 0x1b, "$f27" },
134 { rc(fpr) | 0x1c, "$f28" },
135 { rc(fpr) | 0x1d, "$f29" },
136 { rc(fpr) | 0x1e, "$f30" },
138 { _NOREG, "<none>" },
150 _jit_init(jit_state_t *_jit)
152 _jitc->reglen = jit_size(_rvs) - 1;
157 _jit_prolog(jit_state_t *_jit)
163 assert(jit_regset_cmp_ui(&_jitc->regarg, 0) == 0);
164 jit_regset_set_ui(&_jitc->regsav, 0);
165 offset = _jitc->functions.offset;
166 if (offset >= _jitc->functions.length) {
167 jit_realloc((jit_pointer_t *)&_jitc->functions.ptr,
168 _jitc->functions.length * sizeof(jit_function_t),
169 (_jitc->functions.length + 16) * sizeof(jit_function_t));
170 _jitc->functions.length += 16;
172 _jitc->function = _jitc->functions.ptr + _jitc->functions.offset++;
173 _jitc->function->self.size = stack_framesize;
174 _jitc->function->self.argi = _jitc->function->self.alen = 0;
175 /* float conversion */
176 _jitc->function->self.aoff = _jitc->function->cvt_offset = -8;
177 _jitc->function->self.call = jit_call_default;
178 jit_alloc((jit_pointer_t *)&_jitc->function->regoff,
179 _jitc->reglen * sizeof(jit_int32_t));
181 /* _no_link here does not mean the jit_link() call can be removed
183 * _jitc->function->prolog = jit_new_node(jit_code_prolog);
185 _jitc->function->prolog = jit_new_node_no_link(jit_code_prolog);
186 jit_link(_jitc->function->prolog);
187 _jitc->function->prolog->w.w = offset;
188 _jitc->function->epilog = jit_new_node_no_link(jit_code_epilog);
190 * v: offset in blocks vector
191 * w: offset in functions vector
193 _jitc->function->epilog->w.w = offset;
195 jit_regset_new(&_jitc->function->regset);
199 _jit_allocai(jit_state_t *_jit, jit_int32_t length)
201 assert(_jitc->function != NULL);
203 case 0: case 1: break;
204 case 2: _jitc->function->self.aoff &= -2; break;
205 case 3: case 4: _jitc->function->self.aoff &= -4; break;
206 default: _jitc->function->self.aoff &= -8; break;
208 _jitc->function->self.aoff -= length;
209 if (!_jitc->realize) {
210 jit_inc_synth_ww(allocai, _jitc->function->self.aoff, length);
213 return (_jitc->function->self.aoff);
217 _jit_allocar(jit_state_t *_jit, jit_int32_t u, jit_int32_t v)
220 assert(_jitc->function != NULL);
221 jit_inc_synth_ww(allocar, u, v);
222 if (!_jitc->function->allocar) {
223 _jitc->function->aoffoff = jit_allocai(sizeof(jit_int32_t));
224 _jitc->function->allocar = 1;
226 reg = jit_get_reg(jit_class_gpr);
228 jit_andi(reg, reg, -8);
229 jit_ldxi_i(u, JIT_FP, _jitc->function->aoffoff);
231 jit_addr(JIT_SP, JIT_SP, reg);
232 jit_stxi_i(_jitc->function->aoffoff, JIT_FP, u);
238 _jit_ret(jit_state_t *_jit)
241 assert(_jitc->function != NULL);
245 jit_patch_at(instr, _jitc->function->epilog);
250 _jit_retr(jit_state_t *_jit, jit_int32_t u, jit_code_t code)
252 jit_code_inc_synth_w(code, u);
253 jit_movr(JIT_RET, u);
259 _jit_reti(jit_state_t *_jit, jit_word_t u, jit_code_t code)
261 jit_code_inc_synth_w(code, u);
262 jit_movi(JIT_RET, u);
268 _jit_retr_f(jit_state_t *_jit, jit_int32_t u)
270 jit_inc_synth_w(retr_f, u);
272 jit_movr_f(JIT_FRET, u);
280 _jit_reti_f(jit_state_t *_jit, jit_float32_t u)
282 jit_inc_synth_f(reti_f, u);
283 jit_movi_f(JIT_FRET, u);
289 _jit_retr_d(jit_state_t *_jit, jit_int32_t u)
291 jit_inc_synth_w(retr_d, u);
293 jit_movr_d(JIT_FRET, u);
301 _jit_reti_d(jit_state_t *_jit, jit_float64_t u)
303 jit_inc_synth_d(reti_d, u);
304 jit_movi_d(JIT_FRET, u);
310 _jit_epilog(jit_state_t *_jit)
312 assert(_jitc->function != NULL);
313 assert(_jitc->function->epilog->next == NULL);
314 jit_link(_jitc->function->epilog);
315 _jitc->function = NULL;
319 _jit_arg_register_p(jit_state_t *_jit, jit_node_t *u)
321 if (u->code >= jit_code_arg_c && u->code <= jit_code_arg)
322 return (jit_arg_reg_p(u->u.w));
323 assert(u->code == jit_code_arg_f || u->code == jit_code_arg_d);
324 return (jit_arg_f_reg_p(u->u.w));
328 _jit_ellipsis(jit_state_t *_jit)
330 jit_inc_synth(ellipsis);
331 if (_jitc->prepare) {
333 assert(!(_jitc->function->call.call & jit_call_varargs));
334 _jitc->function->call.call |= jit_call_varargs;
338 assert(!(_jitc->function->self.call & jit_call_varargs));
339 _jitc->function->self.call |= jit_call_varargs;
341 /* Allocate va_list like object in the stack */
342 _jitc->function->vaoff = jit_allocai(sizeof(jit_va_list_t));
343 _jitc->function->vagp = _jitc->function->self.argi;
349 _jit_va_push(jit_state_t *_jit, jit_int32_t u)
352 jit_inc_synth_w(va_push, u);
353 reg = jit_get_reg(jit_class_gpr);
354 jit_ldxi(reg, u, offsetof(jit_va_list_t, base));
356 jit_ldxi(reg, u, offsetof(jit_va_list_t, offset));
363 _jit_arg(jit_state_t *_jit, jit_code_t code)
367 assert(_jitc->function != NULL);
368 assert(!(_jitc->function->self.call & jit_call_varargs));
369 #if STRONG_TYPE_CHECKING
370 assert(code >= jit_code_arg_c && code <= jit_code_arg);
372 if (jit_arg_reg_p(_jitc->function->self.argi))
373 offset = _jitc->function->self.argi++;
375 offset = _jitc->function->self.size;
376 _jitc->function->self.size += 8;
378 node = jit_new_node_ww(code, offset,
379 ++_jitc->function->self.argn);
385 _jit_arg_f(jit_state_t *_jit)
389 assert(_jitc->function != NULL);
390 if (jit_arg_f_reg_p(_jitc->function->self.argi))
391 offset = _jitc->function->self.argi++;
393 offset = _jitc->function->self.size;
394 _jitc->function->self.size += 8;
396 node = jit_new_node_ww(jit_code_arg_f, offset,
397 ++_jitc->function->self.argn);
403 _jit_arg_d(jit_state_t *_jit)
407 assert(_jitc->function != NULL);
408 if (jit_arg_f_reg_p(_jitc->function->self.argi))
409 offset = _jitc->function->self.argi++;
411 offset = _jitc->function->self.size;
412 _jitc->function->self.size += 8;
414 node = jit_new_node_ww(jit_code_arg_d, offset,
415 ++_jitc->function->self.argn);
421 _jit_getarg_c(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
423 assert_arg_type(v->code, jit_code_arg_c);
424 jit_inc_synth_wp(getarg_c, u, v);
425 if (jit_arg_reg_p(v->u.w))
426 jit_extr_c(u, _A0 - v->u.w);
428 jit_ldxi_c(u, _FP, v->u.w + C_DISP);
433 _jit_getarg_uc(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
435 assert_arg_type(v->code, jit_code_arg_c);
436 jit_inc_synth_wp(getarg_uc, u, v);
437 if (jit_arg_reg_p(v->u.w))
438 jit_extr_uc(u, _A0 - v->u.w);
440 jit_ldxi_uc(u, _FP, v->u.w + C_DISP);
445 _jit_getarg_s(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
447 assert_arg_type(v->code, jit_code_arg_s);
448 jit_inc_synth_wp(getarg_s, u, v);
449 if (jit_arg_reg_p(v->u.w))
450 jit_extr_s(u, _A0 - v->u.w);
452 jit_ldxi_s(u, _FP, v->u.w + S_DISP);
457 _jit_getarg_us(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
459 assert_arg_type(v->code, jit_code_arg_s);
460 jit_inc_synth_wp(getarg_us, u, v);
461 if (jit_arg_reg_p(v->u.w))
462 jit_extr_us(u, _A0 - v->u.w);
464 jit_ldxi_us(u, _FP, v->u.w + S_DISP);
469 _jit_getarg_i(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
471 assert_arg_type(v->code, jit_code_arg_i);
472 jit_inc_synth_wp(getarg_i, u, v);
473 if (jit_arg_reg_p(v->u.w))
474 jit_extr_i(u, _A0 - v->u.w);
476 jit_ldxi_i(u, _FP, v->u.w + I_DISP);
481 _jit_getarg_ui(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
483 assert_arg_type(v->code, jit_code_arg_i);
484 jit_inc_synth_wp(getarg_ui, u, v);
485 if (jit_arg_reg_p(v->u.w))
486 jit_extr_ui(u, _A0 - v->u.w);
488 jit_ldxi_ui(u, _FP, v->u.w + I_DISP);
493 _jit_getarg_l(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
495 assert_arg_type(v->code, jit_code_arg_l);
496 jit_inc_synth_wp(getarg_l, u, v);
497 if (jit_arg_reg_p(v->u.w))
498 jit_movr(u, _A0 - v->u.w);
500 jit_ldxi_l(u, _FP, v->u.w);
505 _jit_putargr(jit_state_t *_jit, jit_int32_t u, jit_node_t *v, jit_code_t code)
507 assert_putarg_type(code, v->code);
508 jit_code_inc_synth_wp(code, u, v);
509 if (jit_arg_reg_p(v->u.w))
510 jit_movr(_A0 - v->u.w, u);
512 jit_stxi(v->u.w, _FP, u);
517 _jit_putargi(jit_state_t *_jit, jit_word_t u, jit_node_t *v, jit_code_t code)
520 assert_putarg_type(code, v->code);
521 jit_code_inc_synth_wp(code, u, v);
522 if (jit_arg_reg_p(v->u.w))
523 jit_movi(_A0 - v->u.w, u);
525 regno = jit_get_reg(jit_class_gpr);
527 jit_stxi(v->u.w, _FP, regno);
528 jit_unget_reg(regno);
534 _jit_getarg_f(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
536 assert(v->code == jit_code_arg_f);
537 jit_inc_synth_wp(getarg_f, u, v);
538 if (jit_arg_f_reg_p(v->u.w))
539 jit_movr_f(u, _F16 - v->u.w);
541 jit_ldxi_f(u, _FP, v->u.w + F_DISP);
546 _jit_putargr_f(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
548 assert(v->code == jit_code_arg_f);
549 jit_inc_synth_wp(putargr_f, u, v);
550 if (jit_arg_f_reg_p(v->u.w))
551 jit_movr_f(_F16 - v->u.w, u);
553 jit_stxi_f(v->u.w, _FP, u + F_DISP);
558 _jit_putargi_f(jit_state_t *_jit, jit_float32_t u, jit_node_t *v)
561 assert(v->code == jit_code_arg_f);
562 jit_inc_synth_fp(putargi_f, u, v);
563 if (jit_arg_f_reg_p(v->u.w))
564 jit_movi_f(_F16 - v->u.w, u);
566 regno = jit_get_reg(jit_class_fpr);
567 jit_movi_f(regno, u);
568 jit_stxi_f(v->u.w, _FP, regno + F_DISP);
569 jit_unget_reg(regno);
575 _jit_getarg_d(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
577 assert(v->code == jit_code_arg_d);
578 jit_inc_synth_wp(getarg_d, u, v);
579 if (jit_arg_f_reg_p(v->u.w))
580 jit_movr_d(u, _F16 - v->u.w);
582 jit_ldxi_d(u, _FP, v->u.w);
587 _jit_putargr_d(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
589 assert(v->code == jit_code_arg_d);
590 jit_inc_synth_wp(putargr_d, u, v);
591 if (jit_arg_f_reg_p(v->u.w))
592 jit_movr_d(_F16 - v->u.w, u);
594 jit_stxi_d(v->u.w, _FP, u);
599 _jit_putargi_d(jit_state_t *_jit, jit_float64_t u, jit_node_t *v)
602 assert(v->code == jit_code_arg_d);
603 jit_inc_synth_dp(putargi_d, u, v);
604 if (jit_arg_f_reg_p(v->u.w))
605 jit_movi_d(_F16 - v->u.w, u);
607 regno = jit_get_reg(jit_class_fpr);
608 jit_movi_d(regno, u);
609 jit_stxi_d(v->u.w, _FP, regno);
610 jit_unget_reg(regno);
616 _jit_pushargr(jit_state_t *_jit, jit_int32_t u, jit_code_t code)
618 assert(_jitc->function != NULL);
619 jit_code_inc_synth_w(code, u);
621 if (jit_arg_reg_p(_jitc->function->call.argi)) {
622 jit_movr(_A0 - _jitc->function->call.argi, u);
623 ++_jitc->function->call.argi;
626 jit_stxi(_jitc->function->call.size, JIT_SP, u);
627 _jitc->function->call.size += 8;
633 _jit_pushargi(jit_state_t *_jit, jit_int64_t u, jit_code_t code)
636 assert(_jitc->function != NULL);
637 jit_code_inc_synth_w(code, u);
639 if (jit_arg_reg_p(_jitc->function->call.argi)) {
640 jit_movi(_A0 - _jitc->function->call.argi, u);
641 ++_jitc->function->call.argi;
644 regno = jit_get_reg(jit_class_gpr);
646 jit_stxi(_jitc->function->call.size, JIT_SP, regno);
647 _jitc->function->call.size += 8;
648 jit_unget_reg(regno);
654 _jit_pushargr_f(jit_state_t *_jit, jit_int32_t u)
656 assert(_jitc->function != NULL);
657 jit_inc_synth_w(pushargr_f, u);
659 if (jit_arg_f_reg_p(_jitc->function->call.argi)) {
660 jit_movr_f(_F16 - _jitc->function->call.argi, u);
661 ++_jitc->function->call.argi;
664 jit_stxi_f(_jitc->function->call.size + F_DISP, JIT_SP, u);
665 _jitc->function->call.size += 8;
671 _jit_pushargi_f(jit_state_t *_jit, jit_float32_t u)
674 assert(_jitc->function != NULL);
675 jit_inc_synth_f(pushargi_f, u);
677 if (jit_arg_f_reg_p(_jitc->function->call.argi)) {
678 jit_movi_f(_F16 - _jitc->function->call.argi, u);
679 ++_jitc->function->call.argi;
682 regno = jit_get_reg(jit_class_fpr);
683 jit_movi_f(regno, u);
684 jit_stxi_f(_jitc->function->call.size + F_DISP, JIT_SP, regno);
685 _jitc->function->call.size += 8;
686 jit_unget_reg(regno);
692 _jit_pushargr_d(jit_state_t *_jit, jit_int32_t u)
694 assert(_jitc->function != NULL);
695 jit_inc_synth_w(pushargr_d, u);
697 if (jit_arg_f_reg_p(_jitc->function->call.argi)) {
698 jit_movr_d(_F16 - _jitc->function->call.argi, u);
699 ++_jitc->function->call.argi;
702 jit_stxi_d(_jitc->function->call.size, JIT_SP, u);
703 _jitc->function->call.size += 8;
709 _jit_pushargi_d(jit_state_t *_jit, jit_float64_t u)
712 assert(_jitc->function != NULL);
713 jit_inc_synth_d(pushargi_d, u);
715 if (jit_arg_f_reg_p(_jitc->function->call.argi)) {
716 jit_movi_d(_F16 - _jitc->function->call.argi, u);
717 ++_jitc->function->call.argi;
720 regno = jit_get_reg(jit_class_fpr);
721 jit_movi_d(regno, u);
722 jit_stxi_d(_jitc->function->call.size, JIT_SP, regno);
723 _jitc->function->call.size += 8;
724 jit_unget_reg(regno);
730 _jit_regarg_p(jit_state_t *_jit, jit_node_t *node, jit_int32_t regno)
734 spec = jit_class(_rvs[regno].spec);
735 if (spec & jit_class_arg) {
736 if (spec & jit_class_gpr) {
738 if (regno >= 0 && regno < node->v.w)
741 else if (spec & jit_class_fpr) {
742 regno = _F16 - regno;
743 if (regno >= 0 && regno < node->w.w)
752 _jit_finishr(jit_state_t *_jit, jit_int32_t r0)
755 assert(_jitc->function != NULL);
756 jit_inc_synth_w(finishr, r0);
757 if (_jitc->function->self.alen < _jitc->function->call.size)
758 _jitc->function->self.alen = _jitc->function->call.size;
759 call = jit_callr(r0);
760 call->v.w = call->w.w = _jitc->function->self.argi;
761 _jitc->function->call.argi = _jitc->function->call.size = 0;
767 _jit_finishi(jit_state_t *_jit, jit_pointer_t i0)
770 assert(_jitc->function != NULL);
771 jit_inc_synth_w(finishi, (jit_word_t)i0);
772 if (_jitc->function->self.alen < _jitc->function->call.size)
773 _jitc->function->self.alen = _jitc->function->call.size;
774 call = jit_calli(i0);
775 call->v.w = call->w.w = _jitc->function->self.argf;
776 _jitc->function->call.argi = _jitc->function->call.size = 0;
783 _jit_retval_c(jit_state_t *_jit, jit_int32_t r0)
785 jit_inc_synth_w(retval_c, r0);
786 jit_extr_c(r0, JIT_RET);
791 _jit_retval_uc(jit_state_t *_jit, jit_int32_t r0)
793 jit_inc_synth_w(retval_uc, r0);
794 jit_extr_uc(r0, JIT_RET);
799 _jit_retval_s(jit_state_t *_jit, jit_int32_t r0)
801 jit_inc_synth_w(retval_s, r0);
802 jit_extr_s(r0, JIT_RET);
807 _jit_retval_us(jit_state_t *_jit, jit_int32_t r0)
809 jit_inc_synth_w(retval_us, r0);
810 jit_extr_us(r0, JIT_RET);
815 _jit_retval_i(jit_state_t *_jit, jit_int32_t r0)
817 jit_inc_synth_w(retval_i, r0);
818 jit_extr_i(r0, JIT_RET);
823 _jit_retval_ui(jit_state_t *_jit, jit_int32_t r0)
825 jit_inc_synth_w(retval_ui, r0);
826 jit_extr_ui(r0, JIT_RET);
831 _jit_retval_l(jit_state_t *_jit, jit_int32_t r0)
833 jit_inc_synth_w(retval_l, r0);
835 jit_movr(r0, JIT_RET);
840 _jit_retval_f(jit_state_t *_jit, jit_int32_t r0)
842 jit_inc_synth_w(retval_f, r0);
844 jit_movr_f(r0, JIT_FRET);
849 _jit_retval_d(jit_state_t *_jit, jit_int32_t r0)
851 jit_inc_synth_w(retval_d, r0);
853 jit_movr_d(r0, JIT_FRET);
858 _emit_code(jit_state_t *_jit)
870 #if DEVEL_DISASSEMBLER
873 jit_int32_t const_offset;
874 jit_int32_t patch_offset;
876 #if DEVEL_DISASSEMBLER
880 _jitc->function = NULL;
886 undo.const_offset = undo.patch_offset = 0;
887 #define case_rr(name, type) \
888 case jit_code_##name##r##type: \
889 name##r##type(rn(node->u.w), rn(node->v.w)); \
891 #define case_rw(name, type) \
892 case jit_code_##name##i##type: \
893 name##i##type(rn(node->u.w), node->v.w); \
895 #define case_wr(name, type) \
896 case jit_code_##name##i##type: \
897 name##i##type(node->u.w, rn(node->v.w)); \
899 #define case_rrr(name, type) \
900 case jit_code_##name##r##type: \
901 name##r##type(rn(node->u.w), \
902 rn(node->v.w), rn(node->w.w)); \
904 #define case_rrw(name, type) \
905 case jit_code_##name##i##type: \
906 name##i##type(rn(node->u.w), rn(node->v.w), node->w.w); \
908 #define case_rrf(name, type, size) \
909 case jit_code_##name##i##type: \
910 assert(node->flag & jit_flag_data); \
911 name##i##type(rn(node->u.w), rn(node->v.w), \
912 (jit_float##size##_t *)node->w.n->u.w); \
914 #define case_rqr(name, type) \
915 case jit_code_##name##r##type: \
916 name##r##type(rn(node->u.w), rn(node->v.q.l), \
917 rn(node->v.q.h), rn(node->w.w)); \
918 case jit_code_##name##i##type: \
920 #define case_rrrr(name, type) \
921 case jit_code_##name##r##type: \
922 name##r##type(rn(node->u.q.l), rn(node->u.q.h), \
923 rn(node->v.w), rn(node->w.w)); \
925 #define case_rrrw(name, type) \
926 case jit_code_##name##i##type: \
927 name##i##type(rn(node->u.q.l), rn(node->u.q.h), \
928 rn(node->v.w), node->w.w); \
930 #define case_wrr(name, type) \
931 case jit_code_##name##i##type: \
932 name##i##type(node->u.w, rn(node->v.w), rn(node->w.w)); \
934 #define case_brr(name, type) \
935 case jit_code_##name##r##type: \
937 assert(temp->code == jit_code_label || \
938 temp->code == jit_code_epilog); \
939 if (temp->flag & jit_flag_patch) \
940 name##r##type(temp->u.w, rn(node->v.w), \
943 word = name##r##type(_jit->pc.w, \
944 rn(node->v.w), rn(node->w.w)); \
948 #define case_brw(name, type) \
949 case jit_code_##name##i##type: \
951 assert(temp->code == jit_code_label || \
952 temp->code == jit_code_epilog); \
953 if (temp->flag & jit_flag_patch) \
954 name##i##type(temp->u.w, \
955 rn(node->v.w), node->w.w); \
957 word = name##i##type(_jit->pc.w, \
958 rn(node->v.w), node->w.w); \
962 #define case_brf(name, type, size) \
963 case jit_code_##name##i##type: \
965 assert(temp->code == jit_code_label || \
966 temp->code == jit_code_epilog); \
967 if (temp->flag & jit_flag_patch) \
968 name##i##type(temp->u.w, rn(node->v.w), \
969 (jit_float##size##_t *)node->w.n->u.w); \
971 word = name##i##type(_jit->pc.w, rn(node->v.w), \
972 (jit_float##size##_t *)node->w.n->u.w); \
976 #if DEVEL_DISASSEMBLER
979 for (node = _jitc->head; node; node = node->next) {
980 if (_jit->pc.uc >= _jitc->code.end)
983 #if DEVEL_DISASSEMBLER
984 node->offset = (jit_uword_t)_jit->pc.w - (jit_uword_t)prevw;
987 value = jit_classify(node->code);
988 jit_regarg_set(node, value);
989 switch (node->code) {
991 /* Must align to a power of two */
992 assert(!(node->u.w & (node->u.w - 1)));
993 if ((word = _jit->pc.w & (node->u.w - 1)))
994 nop(node->u.w - word);
997 nop((node->u.w + 3) & ~3);
999 case jit_code_note: case jit_code_name:
1000 node->u.w = _jit->pc.w;
1002 case jit_code_label:
1003 /* remember label is defined */
1004 node->flag |= jit_flag_patch;
1005 node->u.w = _jit->pc.w;
1028 case_rrrr(qmul, _u);
1029 case_rrrw(qmul, _u);
1036 case_rrrr(qdiv, _u);
1037 case_rrrw(qdiv, _u);
1046 case_rrrr(qlsh, _u);
1047 case_rrrw(qlsh, _u);
1054 case_rrrr(qrsh, _u);
1055 case_rrrw(qrsh, _u);
1056 #define lrotr(r0,r1,r2) fallback_lrotr(r0,r1,r2)
1057 #define lroti(r0,r1,i0) fallback_lroti(r0,r1,i0)
1058 #define rrotr(r0,r1,r2) fallback_rrotr(r0,r1,r2)
1059 #define rroti(r0,r1,i0) fallback_rroti(r0,r1,i0)
1070 case_rr(trunc, _f_i);
1071 case_rr(trunc, _d_i);
1072 case_rr(trunc, _f_l);
1073 case_rr(trunc, _d_l);
1102 case jit_code_unldr:
1103 unldr(rn(node->u.w), rn(node->v.w), node->w.w);
1105 case jit_code_unldi:
1106 unldi(rn(node->u.w), node->v.w, node->w.w);
1108 case jit_code_unldr_u:
1109 unldr_u(rn(node->u.w), rn(node->v.w), node->w.w);
1111 case jit_code_unldi_u:
1112 unldi_u(rn(node->u.w), node->v.w, node->w.w);
1130 case jit_code_unstr:
1131 unstr(rn(node->u.w), rn(node->v.w), node->w.w);
1133 case jit_code_unsti:
1134 unsti(node->u.w, rn(node->v.w), node->w.w);
1139 case_rr(bswap, _us);
1140 case_rr(bswap, _ui);
1141 case_rr(bswap, _ul);
1143 extr(rn(node->u.w), rn(node->v.w), node->w.q.l, node->w.q.h);
1145 case jit_code_extr_u:
1146 extr_u(rn(node->u.w), rn(node->v.w), node->w.q.l, node->w.q.h);
1149 depr(rn(node->u.w), rn(node->v.w), node->w.q.l, node->w.q.h);
1152 depi(rn(node->u.w), node->v.w, node->w.q.l, node->w.q.h);
1161 casr(rn(node->u.w), rn(node->v.w),
1162 rn(node->w.q.l), rn(node->w.q.h));
1165 casi(rn(node->u.w), node->v.w,
1166 rn(node->w.q.l), rn(node->w.q.h));
1172 if (node->flag & jit_flag_node) {
1174 if (temp->code == jit_code_data ||
1175 (temp->code == jit_code_label &&
1176 (temp->flag & jit_flag_patch)))
1177 movi(rn(node->u.w), temp->u.w);
1179 assert(temp->code == jit_code_label ||
1180 temp->code == jit_code_epilog);
1181 word = movi_p(rn(node->u.w), node->v.w);
1186 movi(rn(node->u.w), node->v.w);
1194 #define rbitr(r0, r1) fallback_rbit(r0, r1)
1239 case_brr(boadd, _u);
1240 case_brw(boadd, _u);
1243 case_brr(bxadd, _u);
1244 case_brw(bxadd, _u);
1247 case_brr(bosub, _u);
1248 case_brw(bosub, _u);
1251 case_brr(bxsub, _u);
1252 case_brw(bxsub, _u);
1258 case_rrf(add, _f, 32);
1260 case_rrf(sub, _f, 32);
1261 case_rrf(rsb, _f, 32);
1263 case_rrf(mul, _f, 32);
1265 case_rrf(div, _f, 32);
1278 #define unldr_x(r0, r1, i0) fallback_unldr_x(r0, r1, i0)
1279 case jit_code_unldr_x:
1280 unldr_x(rn(node->u.w), rn(node->v.w), node->w.w);
1282 #define unldi_x(r0, i0, i1) fallback_unldi_x(r0, i0, i1)
1283 case jit_code_unldi_x:
1284 unldi_x(rn(node->u.w), node->v.w, node->w.w);
1290 /* Cost of loading, masking, oring, etc to use STQ_U is
1291 * too high. Could be branchless for doubles, but would
1292 * generate larger code, and speed for unaligned double
1293 * store is not so important; just support it. */
1294 #define unstr_x(r0, r1, i0) fallback_unstr_x(r0, r1, i0)
1295 case jit_code_unstr_x:
1296 unstr_x(rn(node->u.w), rn(node->v.w), node->w.w);
1298 #define unsti_x(i0, r0, i1) fallback_unsti_x(i0, r0, i1)
1299 case jit_code_unsti_x:
1300 unsti_x(node->u.w, rn(node->v.w), node->w.w);
1303 case jit_code_movi_f:
1304 assert(node->flag & jit_flag_data);
1305 movi_f(rn(node->u.w), (jit_float32_t *)node->v.n->u.w);
1309 case_rrf(lt, _f, 32);
1311 case_rrf(le, _f, 32);
1313 case_rrf(eq, _f, 32);
1315 case_rrf(ge, _f, 32);
1317 case_rrf(gt, _f, 32);
1319 case_rrf(ne, _f, 32);
1321 case_rrf(unlt, _f, 32);
1323 case_rrf(unle, _f, 32);
1325 case_rrf(uneq, _f, 32);
1327 case_rrf(unge, _f, 32);
1329 case_rrf(ungt, _f, 32);
1331 case_rrf(ltgt, _f, 32);
1333 case_rrf(ord, _f, 32);
1334 case_rrr(unord, _f);
1335 case_rrf(unord, _f, 32);
1337 case_brf(blt, _f, 32);
1339 case_brf(ble, _f, 32);
1341 case_brf(beq, _f, 32);
1343 case_brf(bge, _f, 32);
1345 case_brf(bgt, _f, 32);
1347 case_brf(bne, _f, 32);
1348 case_brr(bunlt, _f);
1349 case_brf(bunlt, _f, 32);
1350 case_brr(bunle, _f);
1351 case_brf(bunle, _f, 32);
1352 case_brr(buneq, _f);
1353 case_brf(buneq, _f, 32);
1354 case_brr(bunge, _f);
1355 case_brf(bunge, _f, 32);
1356 case_brr(bungt, _f);
1357 case_brf(bungt, _f, 32);
1358 case_brr(bltgt, _f);
1359 case_brf(bltgt, _f, 32);
1361 case_brf(bord, _f, 32);
1362 case_brr(bunord, _f);
1363 case_brf(bunord, _f, 32);
1365 case_rrf(add, _d, 64);
1367 case_rrf(sub, _d, 64);
1368 case_rrf(rsb, _d, 64);
1370 case_rrf(mul, _d, 64);
1372 case_rrf(div, _d, 64);
1390 case jit_code_movi_d:
1391 assert(node->flag & jit_flag_data);
1392 movi_d(rn(node->u.w), (jit_float64_t *)node->v.n->u.w);
1396 case_rrf(lt, _d, 64);
1398 case_rrf(le, _d, 64);
1400 case_rrf(eq, _d, 64);
1402 case_rrf(ge, _d, 64);
1404 case_rrf(gt, _d, 64);
1406 case_rrf(ne, _d, 64);
1408 case_rrf(unlt, _d, 64);
1410 case_rrf(unle, _d, 64);
1412 case_rrf(uneq, _d, 64);
1414 case_rrf(unge, _d, 64);
1416 case_rrf(ungt, _d, 64);
1418 case_rrf(ltgt, _d, 64);
1420 case_rrf(ord, _d, 64);
1421 case_rrr(unord, _d);
1422 case_rrf(unord, _d, 64);
1424 case_brf(blt, _d, 64);
1426 case_brf(ble, _d, 64);
1428 case_brf(beq, _d, 64);
1430 case_brf(bge, _d, 64);
1432 case_brf(bgt, _d, 64);
1434 case_brf(bne, _d, 64);
1435 case_brr(bunlt, _d);
1436 case_brf(bunlt, _d, 64);
1437 case_brr(bunle, _d);
1438 case_brf(bunle, _d, 64);
1439 case_brr(buneq, _d);
1440 case_brf(buneq, _d, 64);
1441 case_brr(bunge, _d);
1442 case_brf(bunge, _d, 64);
1443 case_brr(bungt, _d);
1444 case_brf(bungt, _d, 64);
1445 case_brr(bltgt, _d);
1446 case_brf(bltgt, _d, 64);
1448 case_brf(bord, _d, 64);
1449 case_brr(bunord, _d);
1450 case_brf(bunord, _d, 64);
1452 jmpr(rn(node->u.w));
1455 if (node->flag & jit_flag_node) {
1457 assert(temp->code == jit_code_label ||
1458 temp->code == jit_code_epilog);
1459 if (temp->flag & jit_flag_patch)
1462 word = _jit->code.length -
1463 (_jit->pc.uc - _jit->code.ptr);
1465 word = jmpi(_jit->pc.w);
1467 word = jmpi_p(_jit->pc.w);
1474 case jit_code_callr:
1475 callr(rn(node->u.w));
1477 case jit_code_calli:
1478 if (node->flag & jit_flag_node) {
1480 assert(temp->code == jit_code_label ||
1481 temp->code == jit_code_epilog);
1482 if (!(temp->flag & jit_flag_patch)) {
1483 word = calli_p(temp->u.w);
1492 case jit_code_prolog:
1493 _jitc->function = _jitc->functions.ptr + node->w.w;
1495 undo.word = _jit->pc.w;
1496 memcpy(&undo.func, _jitc->function, sizeof(undo.func));
1497 #if DEVEL_DISASSEMBLER
1500 undo.patch_offset = _jitc->patches.offset;
1505 case jit_code_epilog:
1506 assert(_jitc->function == _jitc->functions.ptr + node->w.w);
1508 for (temp = undo.node->next;
1509 temp != node; temp = temp->next) {
1510 if (temp->code == jit_code_label ||
1511 temp->code == jit_code_epilog)
1512 temp->flag &= ~jit_flag_patch;
1514 temp->flag &= ~jit_flag_patch;
1516 _jit->pc.w = undo.word;
1517 /* undo.func.self.aoff and undo.func.regset should not
1518 * be undone, as they will be further updated, and are
1519 * the reason of the undo. */
1520 undo.func.self.aoff = _jitc->function->frame +
1521 _jitc->function->self.aoff;
1522 /* allocar information also does not need to be undone */
1523 undo.func.aoffoff = _jitc->function->aoffoff;
1524 undo.func.allocar = _jitc->function->allocar;
1525 jit_regset_set(&undo.func.regset, &_jitc->function->regset);
1526 memcpy(_jitc->function, &undo.func, sizeof(undo.func));
1527 #if DEVEL_DISASSEMBLER
1530 _jitc->patches.offset = undo.patch_offset;
1531 goto restart_function;
1533 /* remember label is defined */
1534 node->flag |= jit_flag_patch;
1535 node->u.w = _jit->pc.w;
1537 _jitc->function = NULL;
1539 case jit_code_movr_w_f:
1540 movr_w_f(rn(node->u.w), rn(node->v.w));
1542 case jit_code_movr_f_w:
1543 movr_f_w(rn(node->u.w), rn(node->v.w));
1545 case jit_code_movi_f_w:
1546 assert(node->flag & jit_flag_data);
1547 movi_f_w(rn(node->u.w), *(jit_float32_t *)node->v.n->u.w);
1549 case jit_code_movi_w_f:
1550 movi_w_f(rn(node->u.w), node->v.w);
1552 case jit_code_movr_d_w:
1553 movr_d_w(rn(node->u.w), rn(node->v.w));
1555 case jit_code_movi_d_w:
1556 assert(node->flag & jit_flag_data);
1557 movi_d_w(rn(node->u.w), *(jit_float64_t *)node->v.n->u.w);
1559 case jit_code_movr_w_d:
1560 movr_w_d(rn(node->u.w), rn(node->v.w));
1562 case jit_code_movi_w_d:
1563 movi_w_d(rn(node->u.w), node->v.w);
1565 case jit_code_va_start:
1566 vastart(rn(node->u.w));
1568 case jit_code_va_arg:
1569 vaarg(rn(node->u.w), rn(node->v.w));
1571 case jit_code_va_arg_d:
1572 vaarg_d(rn(node->u.w), rn(node->v.w));
1574 case jit_code_live: case jit_code_ellipsis:
1575 case jit_code_va_push:
1576 case jit_code_allocai: case jit_code_allocar:
1577 case jit_code_arg_c: case jit_code_arg_s:
1578 case jit_code_arg_i: case jit_code_arg_l:
1579 case jit_code_arg_f: case jit_code_arg_d:
1580 case jit_code_va_end:
1582 case jit_code_retr_c: case jit_code_reti_c:
1583 case jit_code_retr_uc: case jit_code_reti_uc:
1584 case jit_code_retr_s: case jit_code_reti_s:
1585 case jit_code_retr_us: case jit_code_reti_us:
1586 case jit_code_retr_i: case jit_code_reti_i:
1587 case jit_code_retr_ui: case jit_code_reti_ui:
1588 case jit_code_retr_l: case jit_code_reti_l:
1589 case jit_code_retr_f: case jit_code_reti_f:
1590 case jit_code_retr_d: case jit_code_reti_d:
1591 case jit_code_getarg_c: case jit_code_getarg_uc:
1592 case jit_code_getarg_s: case jit_code_getarg_us:
1593 case jit_code_getarg_i: case jit_code_getarg_ui:
1594 case jit_code_getarg_l:
1595 case jit_code_getarg_f: case jit_code_getarg_d:
1596 case jit_code_putargr_c: case jit_code_putargi_c:
1597 case jit_code_putargr_uc: case jit_code_putargi_uc:
1598 case jit_code_putargr_s: case jit_code_putargi_s:
1599 case jit_code_putargr_us: case jit_code_putargi_us:
1600 case jit_code_putargr_i: case jit_code_putargi_i:
1601 case jit_code_putargr_ui: case jit_code_putargi_ui:
1602 case jit_code_putargr_l: case jit_code_putargi_l:
1603 case jit_code_putargr_f: case jit_code_putargi_f:
1604 case jit_code_putargr_d: case jit_code_putargi_d:
1605 case jit_code_pushargr_c: case jit_code_pushargi_c:
1606 case jit_code_pushargr_uc: case jit_code_pushargi_uc:
1607 case jit_code_pushargr_s: case jit_code_pushargi_s:
1608 case jit_code_pushargr_us: case jit_code_pushargi_us:
1609 case jit_code_pushargr_i: case jit_code_pushargi_i:
1610 case jit_code_pushargr_ui: case jit_code_pushargi_ui:
1611 case jit_code_pushargr_l: case jit_code_pushargi_l:
1612 case jit_code_pushargr_f: case jit_code_pushargi_f:
1613 case jit_code_pushargr_d: case jit_code_pushargi_d:
1614 case jit_code_retval_c: case jit_code_retval_uc:
1615 case jit_code_retval_s: case jit_code_retval_us:
1616 case jit_code_retval_i:
1617 case jit_code_retval_ui: case jit_code_retval_l:
1618 case jit_code_retval_f: case jit_code_retval_d:
1619 case jit_code_prepare:
1620 case jit_code_finishr: case jit_code_finishi:
1621 case jit_code_negi_f: case jit_code_absi_f:
1622 case jit_code_sqrti_f: case jit_code_negi_d:
1623 case jit_code_absi_d: case jit_code_sqrti_d:
1626 negi(rn(node->u.w), node->v.w);
1629 comi(rn(node->u.w), node->v.w);
1631 case jit_code_exti_c:
1632 exti_c(rn(node->u.w), node->v.w);
1634 case jit_code_exti_uc:
1635 exti_uc(rn(node->u.w), node->v.w);
1637 case jit_code_exti_s:
1638 exti_s(rn(node->u.w), node->v.w);
1640 case jit_code_exti_us:
1641 exti_us(rn(node->u.w), node->v.w);
1643 case jit_code_bswapi_us:
1644 bswapi_us(rn(node->u.w), node->v.w);
1646 case jit_code_bswapi_ui:
1647 bswapi_ui(rn(node->u.w), node->v.w);
1649 case jit_code_htoni_us:
1650 htoni_us(rn(node->u.w), node->v.w);
1652 case jit_code_htoni_ui:
1653 htoni_ui(rn(node->u.w), node->v.w);
1655 case jit_code_exti_i:
1656 exti_i(rn(node->u.w), node->v.w);
1658 case jit_code_exti_ui:
1659 exti_ui(rn(node->u.w), node->v.w);
1661 case jit_code_bswapi_ul:
1662 bswapi_ul(rn(node->u.w), node->v.w);
1664 case jit_code_htoni_ul:
1665 htoni_ul(rn(node->u.w), node->v.w);
1668 cloi(rn(node->u.w), node->v.w);
1671 clzi(rn(node->u.w), node->v.w);
1674 ctoi(rn(node->u.w), node->v.w);
1677 ctzi(rn(node->u.w), node->v.w);
1679 case jit_code_rbiti:
1680 rbiti(rn(node->u.w), node->v.w);
1682 case jit_code_popcnti:
1683 popcnti(rn(node->u.w), node->v.w);
1686 exti(rn(node->u.w), node->v.w, node->w.q.l, node->w.q.h);
1688 case jit_code_exti_u:
1689 exti_u(rn(node->u.w), node->v.w, node->w.q.l, node->w.q.h);
1694 if (jit_carry != _NOREG) {
1695 switch (node->code) {
1697 case jit_code_addcr: case jit_code_addci:
1698 case jit_code_addxr: case jit_code_addxi:
1699 case jit_code_subcr: case jit_code_subci:
1700 case jit_code_subxr: case jit_code_subxi:
1703 jit_unget_reg(jit_carry);
1708 jit_regarg_clr(node, value);
1709 assert(_jitc->regarg == 0 ||
1710 (jit_carry != _NOREG && _jitc->regarg == (1 << jit_carry)));
1711 assert(_jitc->synth == 0);
1712 /* update register live state */
1727 for (offset = 0; offset < _jitc->patches.offset; offset++) {
1728 node = _jitc->patches.ptr[offset].node;
1729 word = node->code == jit_code_movi ? node->v.n->u.w : node->u.n->u.w;
1730 patch_at(_jitc->patches.ptr[offset].inst, word);
1733 jit_flush(_jit->code.ptr, _jit->pc.uc);
1735 return (_jit->code.ptr);
1739 # include "jit_alpha-cpu.c"
1740 # include "jit_alpha-fpu.c"
1741 # include "jit_fallback.c"
1745 jit_flush(void *fptr, void *tptr)
1750 _emit_ldxi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1752 ldxi(rn(r0), rn(r1), i0);
1756 _emit_stxi(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_int32_t r1)
1758 stxi(i0, rn(r0), rn(r1));
1762 _emit_ldxi_d(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
1764 ldxi_d(rn(r0), rn(r1), i0);
1768 _emit_stxi_d(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_int32_t r1)
1770 stxi_d(i0, rn(r0), rn(r1));
1774 _patch(jit_state_t *_jit, jit_word_t instr, jit_node_t *node)
1778 assert(node->flag & jit_flag_node);
1779 if (node->code == jit_code_movi)
1780 flag = node->v.n->flag;
1782 flag = node->u.n->flag;
1783 assert(!(flag & jit_flag_patch));
1784 if (_jitc->patches.offset >= _jitc->patches.length) {
1785 jit_realloc((jit_pointer_t *)&_jitc->patches.ptr,
1786 _jitc->patches.length * sizeof(jit_patch_t),
1787 (_jitc->patches.length + 1024) * sizeof(jit_patch_t));
1788 _jitc->patches.length += 1024;
1790 _jitc->patches.ptr[_jitc->patches.offset].inst = instr;
1791 _jitc->patches.ptr[_jitc->patches.offset].node = node;
1792 ++_jitc->patches.offset;