2 * Copyright (C) 2012-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 #if defined(__linux__)
24 #define stack_framesize 48
26 #define jit_arg_reg_p(i) ((i) >= 0 && (i) < 4)
27 #define jit_arg_f_reg_p(i) ((i) >= 0 && (i) < 16)
28 #define jit_arg_d_reg_p(i) ((i) >= 0 && (i) < 15)
30 #define arm_patch_node 0x80000000
31 #define arm_patch_word 0x40000000
32 #define arm_patch_jump 0x20000000
33 #define arm_patch_load 0x10000000
34 #define arm_patch_call 0x08000000
36 #define jit_fpr_p(rn) ((rn) > 15)
38 #define arg_base() (stack_framesize - 16)
39 #define arg_offset(n) \
40 ((n) < 4 ? arg_base() + ((n) << 2) : (n))
42 /* Assume functions called never match jit instruction set, that is
43 * libc, gmp, mpfr, etc functions are in thumb mode and jit is in
44 * arm mode, what may cause a crash upon return of that function
45 * if generating jit for a relative jump.
47 #define jit_exchange_p() jit_cpu.exchange
49 /* FIXME is it really required to not touch _R10? */
51 #define CHECK_REG_ARGS() \
53 if (!_jitc->function->save_reg_args) \
54 _jitc->again = _jitc->function->save_reg_args = 1; \
57 #define CHECK_SWF_OFFSET() \
59 if (!_jitc->function->swf_offset) { \
60 _jitc->again = _jitc->function->save_reg_args = \
61 _jitc->function->swf_offset = 1; \
62 _jitc->function->self.aoff = -64; \
66 #define CHECK_RETURN() \
68 if (!_jitc->function->need_frame && \
69 !_jitc->function->need_return) \
70 _jitc->again = _jitc->function->need_return = 1; \
76 typedef union _jit_thumb_t {
81 typedef jit_pointer_t jit_va_list;
86 #define jit_make_arg(node,code) _jit_make_arg(_jit,node,code)
87 static jit_node_t *_jit_make_arg(jit_state_t*,jit_node_t*,jit_code_t);
88 #define jit_make_arg_f(node) _jit_make_arg_f(_jit,node)
89 static jit_node_t *_jit_make_arg_f(jit_state_t*,jit_node_t*);
90 #define jit_make_arg_d(node) _jit_make_arg_d(_jit,node)
91 static jit_node_t *_jit_make_arg_d(jit_state_t*,jit_node_t*);
92 #define jit_get_reg_pair() _jit_get_reg_pair(_jit)
93 static jit_int32_t _jit_get_reg_pair(jit_state_t*);
94 #define jit_unget_reg_pair(rn) _jit_unget_reg_pair(_jit,rn)
95 static void _jit_unget_reg_pair(jit_state_t*,jit_int32_t);
96 # define must_align_p(node) _must_align_p(_jit, node)
97 static jit_bool_t _must_align_p(jit_state_t*,jit_node_t*);
98 #define load_const(uniq,r0,i0) _load_const(_jit,uniq,r0,i0)
99 static void _load_const(jit_state_t*,jit_bool_t,jit_int32_t,jit_word_t);
100 #define flush_consts() _flush_consts(_jit)
101 static void _flush_consts(jit_state_t*);
102 #define invalidate_consts() _invalidate_consts(_jit)
103 static void _invalidate_consts(jit_state_t*);
104 #define compute_framesize() _compute_framesize(_jit)
105 static void _compute_framesize(jit_state_t*);
106 #define patch(instr, node, kind) _patch(_jit, instr, node, kind)
107 static void _patch(jit_state_t*,jit_word_t,jit_node_t*,jit_int32_t);
109 #if defined(__GNUC__)
111 extern void __clear_cache(void *, void *);
115 # include "jit_rewind.c"
116 # include "jit_arm-cpu.c"
117 # include "jit_arm-swf.c"
118 # include "jit_arm-vfp.c"
119 # include "jit_fallback.c"
126 jit_register_t _rvs[] = {
127 { rc(gpr) | 0x0c, "ip" },
128 { rc(sav) | rc(gpr) | 0x04, "r4" },
129 { rc(sav) | rc(gpr) | 0x05, "r5" },
130 { rc(sav) | rc(gpr) | 0x06, "r6" },
131 { rc(sav) | rc(gpr) | 0x07, "r7" },
132 { rc(sav) | rc(gpr) | 0x08, "r8" },
133 { rc(sav) | rc(gpr) | 0x09, "r9" },
134 { rc(sav) | 0x0a, "sl" },
135 { rc(sav) | 0x0b, "fp" },
136 { rc(sav) | 0x0d, "sp" },
137 { rc(sav) | 0x0e, "lr" },
139 { rc(arg) | rc(gpr) | 0x03, "r3" },
140 { rc(arg) | rc(gpr) | 0x02, "r2" },
141 { rc(arg) | rc(gpr) | 0x01, "r1" },
142 { rc(arg) | rc(gpr) | 0x00, "r0" },
143 { rc(fpr) | 0x20, "d8" },
145 { rc(fpr) | 0x22, "d9" },
147 { rc(fpr) | 0x24, "d10" },
149 { rc(fpr) | 0x26, "d11" },
151 { rc(fpr) | 0x28, "d12" },
153 { rc(fpr) | 0x2a, "d13" },
155 { rc(fpr) | 0x2c, "d14" },
157 { rc(fpr) | 0x2e, "d15" },
159 { rc(arg) | 0x1f, "s15" },
160 { rc(arg)|rc(sft)|rc(fpr)|0x1e, "d7" },
161 { rc(arg) | 0x1d, "s13" },
162 { rc(arg)|rc(sft)|rc(fpr)|0x1c, "d6" },
163 { rc(arg) | 0x1b, "s11" },
164 { rc(arg)|rc(sft)|rc(fpr)|0x1a, "d5" },
165 { rc(arg) | 0x19, "s9" },
166 { rc(arg)|rc(sft)|rc(fpr)|0x18, "d4" },
167 { rc(arg) | 0x17, "s7" },
168 { rc(arg)|rc(sft)|rc(fpr)|0x16, "d3" },
169 { rc(arg) | 0x15, "s5" },
170 { rc(arg)|rc(sft)|rc(fpr)|0x14, "d2" },
171 { rc(arg) | 0x13, "s3" },
172 { rc(arg)|rc(sft)|rc(fpr)|0x12, "d1" },
173 { rc(arg) | 0x11, "s1" },
174 { rc(arg)|rc(sft)|rc(fpr)|0x10, "d0" },
175 { _NOREG, "<none>" },
178 static jit_int32_t iregs[] = {
179 _R4, _R5, _R6, _R7, _R8, _R9,
188 #if defined(__linux__)
193 if ((fp = fopen("/proc/cpuinfo", "r")) != NULL) {
194 while (fgets(buf, sizeof(buf), fp)) {
195 if (strncmp(buf, "CPU architecture:", 17) == 0) {
196 jit_cpu.version = strtol(buf + 17, &ptr, 10);
198 if (*ptr == 'T' || *ptr == 't') {
202 else if (*ptr == 'E' || *ptr == 'e') {
210 else if (strncmp(buf, "Features\t:", 10) == 0) {
211 if ((ptr = strstr(buf + 10, "vfpv")))
212 jit_cpu.vfp = strtol(ptr + 4, NULL, 0);
213 if ((ptr = strstr(buf + 10, "neon")))
215 if ((ptr = strstr(buf + 10, "thumb")))
222 #if defined(__ARM_PCS_VFP)
225 if (!jit_cpu.version)
229 #if defined(__thumb2__)
232 /* armv6t2 todo (software float and thumb2) */
233 if (!jit_cpu.vfp && jit_cpu.thumb)
235 /* FIXME need test environments for the below. For the moment just
236 * be very conservative */
237 /* force generation of code assuming jit and function libraries called
238 * instruction set do not match */
239 jit_cpu.exchange = 1;
240 /* do not generate hardware integer division by default */
241 if (jit_cpu.version == 7)
246 _jit_init(jit_state_t *_jit)
249 static jit_bool_t first = 1;
251 _jitc->reglen = jit_size(_rvs) - 1;
253 /* jit_get_cpu() should have been already called, and only once */
255 /* cause register to never be allocated, because simple
256 * software float only allocates stack space for 8 slots */
257 for (regno = _D8; regno < _D7; regno++)
258 _rvs[regno].spec = 0;
261 for (regno = _S15; regno <= _D0; regno++)
262 _rvs[regno].spec &= ~rc(arg);
269 _jit_prolog(jit_state_t *_jit)
275 assert(jit_regset_cmp_ui(&_jitc->regarg, 0) == 0);
276 jit_regset_set_ui(&_jitc->regsav, 0);
277 offset = _jitc->functions.offset;
278 if (offset >= _jitc->functions.length) {
279 jit_realloc((jit_pointer_t *)&_jitc->functions.ptr,
280 _jitc->functions.length * sizeof(jit_function_t),
281 (_jitc->functions.length + 16) * sizeof(jit_function_t));
282 _jitc->functions.length += 16;
284 _jitc->function = _jitc->functions.ptr + _jitc->functions.offset++;
285 _jitc->function->self.size = stack_framesize;
286 _jitc->function->self.argi = _jitc->function->self.argf =
287 _jitc->function->self.alen = _jitc->function->self.aoff = 0;
288 _jitc->function->swf_offset = _jitc->function->save_reg_args =
289 _jitc->function->need_return = 0;
290 _jitc->function->self.call = jit_call_default;
291 jit_alloc((jit_pointer_t *)&_jitc->function->regoff,
292 _jitc->reglen * sizeof(jit_int32_t));
294 /* _no_link here does not mean the jit_link() call can be removed
296 * _jitc->function->prolog = jit_new_node(jit_code_prolog);
298 _jitc->function->prolog = jit_new_node_no_link(jit_code_prolog);
299 jit_link(_jitc->function->prolog);
300 _jitc->function->prolog->w.w = offset;
301 _jitc->function->epilog = jit_new_node_no_link(jit_code_epilog);
303 * v: offset in blocks vector
304 * w: offset in functions vector
306 _jitc->function->epilog->w.w = offset;
308 jit_regset_new(&_jitc->function->regset);
312 _jit_allocai(jit_state_t *_jit, jit_int32_t length)
314 assert(_jitc->function);
319 case 0: case 1: break;
320 case 2: _jitc->function->self.aoff &= -2; break;
321 case 3: case 4: _jitc->function->self.aoff &= -4; break;
322 default: _jitc->function->self.aoff &= -8; break;
324 _jitc->function->self.aoff -= length;
325 if (!_jitc->realize) {
326 jit_inc_synth_ww(allocai, _jitc->function->self.aoff, length);
329 return (_jitc->function->self.aoff);
333 _jit_allocar(jit_state_t *_jit, jit_int32_t u, jit_int32_t v)
336 assert(_jitc->function);
337 jit_inc_synth_ww(allocar, u, v);
338 if (!_jitc->function->allocar) {
339 _jitc->function->aoffoff = jit_allocai(sizeof(jit_int32_t));
340 _jitc->function->allocar = 1;
342 reg = jit_get_reg(jit_class_gpr);
344 jit_andi(reg, reg, -8);
345 jit_ldxi_i(u, JIT_FP, _jitc->function->aoffoff);
347 jit_addr(JIT_SP, JIT_SP, reg);
348 jit_stxi_i(_jitc->function->aoffoff, JIT_FP, u);
354 _jit_ret(jit_state_t *_jit)
357 assert(_jitc->function);
361 jit_patch_at(instr, _jitc->function->epilog);
366 _jit_retr(jit_state_t *_jit, jit_int32_t u, jit_code_t code)
368 jit_code_inc_synth_w(code, u);
369 jit_movr(JIT_RET, u);
375 _jit_reti(jit_state_t *_jit, jit_word_t u, jit_code_t code)
377 jit_code_inc_synth_w(code, u);
378 jit_movi(JIT_RET, u);
384 _jit_retr_f(jit_state_t *_jit, jit_int32_t u)
386 jit_inc_synth_w(retr_f, u);
389 jit_movr_f(JIT_FRET, u);
395 jit_movr_f_w(JIT_RET, u);
404 _jit_reti_f(jit_state_t *_jit, jit_float32_t u)
406 jit_inc_synth_f(reti_f, u);
408 jit_movi_f(JIT_FRET, u);
410 jit_movi_f_w(JIT_RET, u);
416 _jit_retr_d(jit_state_t *_jit, jit_int32_t u)
418 jit_inc_synth_w(retr_d, u);
421 jit_movr_d(JIT_FRET, u);
427 jit_movr_d_ww(JIT_RET, _R1, u);
436 _jit_reti_d(jit_state_t *_jit, jit_float64_t u)
438 jit_inc_synth_d(reti_d, u);
440 jit_movi_d(JIT_FRET, u);
442 jit_movi_d_ww(JIT_RET, _R1, u);
448 _jit_epilog(jit_state_t *_jit)
450 assert(_jitc->function);
451 assert(_jitc->function->epilog->next == NULL);
452 jit_link(_jitc->function->epilog);
453 _jitc->function = NULL;
457 _jit_arg_register_p(jit_state_t *_jit, jit_node_t *u)
459 if (!(u->code >= jit_code_arg_c && u->code <= jit_code_arg)) {
460 if (u->code == jit_code_arg_f) {
462 return (jit_arg_f_reg_p(u->u.w));
465 assert(u->code == jit_code_arg_d);
467 return (jit_arg_d_reg_p(u->u.w));
470 return (jit_arg_reg_p(u->u.w));
474 _jit_make_arg(jit_state_t *_jit, jit_node_t *node, jit_code_t code)
477 if (jit_arg_reg_p(_jitc->function->self.argi))
478 offset = _jitc->function->self.argi++;
480 offset = _jitc->function->self.size;
481 _jitc->function->self.size += sizeof(jit_word_t);
483 if (node == (jit_node_t *)0)
484 node = jit_new_node(code);
488 node->v.w = ++_jitc->function->self.argn;
494 _jit_make_arg_f(jit_state_t *_jit, jit_node_t *node)
497 if (jit_cpu.abi && !(_jitc->function->self.call & jit_call_varargs)) {
498 if (jit_arg_f_reg_p(_jitc->function->self.argf)) {
499 offset = _jitc->function->self.argf++;
504 if (jit_arg_reg_p(_jitc->function->self.argi)) {
505 offset = _jitc->function->self.argi++;
509 offset = _jitc->function->self.size;
510 _jitc->function->self.size += sizeof(jit_float32_t);
512 if (node == (jit_node_t *)0)
513 node = jit_new_node(jit_code_arg_f);
517 node->v.w = ++_jitc->function->self.argn;
523 _jit_make_arg_d(jit_state_t *_jit, jit_node_t *node)
526 if (jit_cpu.abi && !(_jitc->function->self.call & jit_call_varargs)) {
527 if (jit_arg_d_reg_p(_jitc->function->self.argf)) {
528 if (_jitc->function->self.argf & 1)
529 ++_jitc->function->self.argf;
530 offset = _jitc->function->self.argf;
531 _jitc->function->self.argf += 2;
536 if (_jitc->function->self.argi & 1)
537 ++_jitc->function->self.argi;
538 if (jit_arg_reg_p(_jitc->function->self.argi)) {
539 offset = _jitc->function->self.argi;
540 _jitc->function->self.argi += 2;
544 if (_jitc->function->self.size & 7)
545 _jitc->function->self.size += 4;
546 offset = _jitc->function->self.size;
547 _jitc->function->self.size += sizeof(jit_float64_t);
549 if (node == (jit_node_t *)0)
550 node = jit_new_node(jit_code_arg_d);
554 node->v.w = ++_jitc->function->self.argn;
560 _jit_ellipsis(jit_state_t *_jit)
562 if (_jitc->prepare) {
563 assert(!(_jitc->function->call.call & jit_call_varargs));
564 _jitc->function->call.call |= jit_call_varargs;
565 if (jit_cpu.abi && _jitc->function->call.argf)
569 assert(!(_jitc->function->self.call & jit_call_varargs));
570 _jitc->function->self.call |= jit_call_varargs;
572 if (jit_cpu.abi && _jitc->function->self.argf)
574 /* First 4 stack addresses need to be spilled r0-r3 */
575 if (jit_arg_reg_p(_jitc->function->self.argi))
576 _jitc->function->vagp = _jitc->function->self.argi * 4;
578 _jitc->function->vagp = 16;
580 jit_inc_synth(ellipsis);
589 _jit_va_push(jit_state_t *_jit, jit_int32_t u)
591 jit_inc_synth_w(va_push, u);
597 _jit_arg(jit_state_t *_jit, jit_code_t code)
599 assert(_jitc->function);
600 assert(!(_jitc->function->self.call & jit_call_varargs));
601 #if STRONG_TYPE_CHECKING
602 assert(code >= jit_code_arg_c && code <= jit_code_arg);
604 return (jit_make_arg((jit_node_t*)0, code));
608 _jit_arg_f(jit_state_t *_jit)
610 assert(_jitc->function);
611 assert(!(_jitc->function->self.call & jit_call_varargs));
612 return (jit_make_arg_f((jit_node_t*)0));
616 _jit_arg_d(jit_state_t *_jit)
618 assert(_jitc->function);
619 assert(!(_jitc->function->self.call & jit_call_varargs));
620 return (jit_make_arg_d((jit_node_t*)0));
624 _jit_getarg_c(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
626 jit_node_t *node = NULL;
627 assert_arg_type(v->code, jit_code_arg_c);
628 jit_inc_synth_wp(getarg_c, u, v);
630 node = jit_ldxi_c(u, JIT_FP, arg_offset(v->u.w));
631 else if (jit_arg_reg_p(v->u.w))
632 jit_extr_c(u, JIT_RA0 - v->u.w);
634 node = jit_ldxi_c(u, JIT_FP, v->u.w);
637 jit_link_alist(node);
644 _jit_getarg_uc(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
646 jit_node_t *node = NULL;
647 assert_arg_type(v->code, jit_code_arg_c);
648 jit_inc_synth_wp(getarg_uc, u, v);
650 node = jit_ldxi_uc(u, JIT_FP, arg_offset(v->u.w));
651 else if (jit_arg_reg_p(v->u.w))
652 jit_extr_uc(u, JIT_RA0 - v->u.w);
654 node = jit_ldxi_uc(u, JIT_FP, v->u.w);
657 jit_link_alist(node);
664 _jit_getarg_s(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
666 jit_node_t *node = NULL;
667 assert_arg_type(v->code, jit_code_arg_s);
668 jit_inc_synth_wp(getarg_s, u, v);
670 node = jit_ldxi_s(u, JIT_FP, arg_offset(v->u.w));
671 else if (jit_arg_reg_p(v->u.w))
672 jit_extr_s(u, JIT_RA0 - v->u.w);
674 node = jit_ldxi_s(u, JIT_FP, v->u.w);
677 jit_link_alist(node);
684 _jit_getarg_us(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
686 jit_node_t *node = NULL;
687 assert_arg_type(v->code, jit_code_arg_s);
688 jit_inc_synth_wp(getarg_us, u, v);
690 node = jit_ldxi_us(u, JIT_FP, arg_offset(v->u.w));
691 else if (jit_arg_reg_p(v->u.w))
692 jit_extr_us(u, JIT_RA0 - v->u.w);
694 node = jit_ldxi_us(u, JIT_FP, v->u.w);
697 jit_link_alist(node);
704 _jit_getarg_i(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
706 jit_node_t *node = NULL;
707 assert_arg_type(v->code, jit_code_arg_i);
708 jit_inc_synth_wp(getarg_i, u, v);
710 node = jit_ldxi_i(u, JIT_FP, arg_offset(v->u.w));
711 else if (jit_arg_reg_p(v->u.w))
712 jit_movr(u, JIT_RA0 - v->u.w);
714 node = jit_ldxi_i(u, JIT_FP, v->u.w);
717 jit_link_alist(node);
724 _jit_putargr(jit_state_t *_jit, jit_int32_t u, jit_node_t *v, jit_code_t code)
726 jit_node_t *node = NULL;
727 assert_putarg_type(code, v->code);
728 jit_code_inc_synth_wp(code, u, v);
730 node = jit_stxi(arg_offset(v->u.w), JIT_FP, u);
731 else if (jit_arg_reg_p(v->u.w))
732 jit_movr(JIT_RA0 - v->u.w, u);
734 node = jit_stxi(v->u.w, JIT_FP, u);
737 jit_link_alist(node);
744 _jit_putargi(jit_state_t *_jit, jit_word_t u, jit_node_t *v, jit_code_t code)
747 jit_node_t *node = NULL;
748 assert_putarg_type(code, v->code);
749 jit_code_inc_synth_wp(code, u, v);
751 regno = jit_get_reg(jit_class_gpr);
753 node = jit_stxi(arg_offset(v->u.w), JIT_FP, regno);
754 jit_unget_reg(regno);
756 else if (jit_arg_reg_p(v->u.w))
757 jit_movi(JIT_RA0 - v->u.w, u);
759 regno = jit_get_reg(jit_class_gpr);
761 node = jit_stxi(v->u.w, JIT_FP, regno);
762 jit_unget_reg(regno);
766 jit_link_alist(node);
773 _jit_getarg_f(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
775 jit_node_t *node = NULL;
776 assert(v->code == jit_code_arg_f);
777 jit_inc_synth_wp(getarg_f, u, v);
778 if (jit_cpu.abi && !(_jitc->function->self.call & jit_call_varargs)) {
779 if (jit_arg_f_reg_p(v->u.w))
780 jit_movr_f(u, JIT_FA0 - v->u.w);
782 node = jit_ldxi_f(u, JIT_FP, v->u.w);
784 else if (jit_swf_p())
785 node = jit_ldxi_f(u, JIT_FP, arg_offset(v->u.w));
787 if (jit_arg_reg_p(v->u.w))
788 jit_movr_w_f(u, JIT_RA0 - v->u.w);
790 node = jit_ldxi_f(u, JIT_FP, v->u.w);
794 jit_link_alist(node);
801 _jit_putargr_f(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
803 jit_node_t *node = NULL;
804 assert(v->code == jit_code_arg_f);
805 jit_inc_synth_wp(putargr_f, u, v);
807 if (jit_arg_f_reg_p(v->u.w))
808 jit_movr_f(JIT_FA0 - v->u.w, u);
810 node = jit_stxi_f(v->u.w, JIT_FP, u);
812 else if (jit_swf_p())
813 node = jit_stxi_f(arg_offset(v->u.w), JIT_FP, u);
815 if (jit_arg_reg_p(v->u.w))
816 jit_movr_f_w(JIT_RA0 - v->u.w, u);
818 node = jit_stxi_f(v->u.w, JIT_FP, u);
822 jit_link_alist(node);
829 _jit_putargi_f(jit_state_t *_jit, jit_float32_t u, jit_node_t *v)
832 jit_node_t *node = NULL;
833 assert(v->code == jit_code_arg_f);
834 jit_inc_synth_fp(putargi_f, u, v);
836 if (jit_arg_f_reg_p(v->u.w))
837 jit_movi_f(JIT_FA0 - v->u.w, u);
839 regno = jit_get_reg(jit_class_fpr);
840 jit_movi_f(regno, u);
841 node = jit_stxi_f(v->u.w, JIT_FP, regno);
842 jit_unget_reg(regno);
845 else if (jit_swf_p()) {
846 regno = jit_get_reg(jit_class_fpr);
847 jit_movi_f(regno, u);
848 node = jit_stxi_f(arg_offset(v->u.w), JIT_FP, regno);
849 jit_unget_reg(regno);
852 regno = jit_get_reg(jit_class_fpr);
853 jit_movi_f(regno, u);
854 if (jit_arg_reg_p(v->u.w))
855 jit_movr_f_w(JIT_RA0 - v->u.w, regno);
857 node = jit_stxi_f(v->u.w, JIT_FP, regno);
858 jit_unget_reg(regno);
862 jit_link_alist(node);
869 _jit_getarg_d(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
871 jit_node_t *node = NULL;
872 assert(v->code == jit_code_arg_d);
873 jit_inc_synth_wp(getarg_d, u, v);
874 if (jit_cpu.abi && !(_jitc->function->self.call & jit_call_varargs)) {
875 if (jit_arg_f_reg_p(v->u.w))
876 jit_movr_d(u, JIT_FA0 - v->u.w);
878 node = jit_ldxi_d(u, JIT_FP, v->u.w);
880 else if (jit_swf_p())
881 node = jit_ldxi_d(u, JIT_FP, arg_offset(v->u.w));
883 if (jit_arg_reg_p(v->u.w))
884 jit_movr_ww_d(u, JIT_RA0 - v->u.w, JIT_RA0 - (v->u.w + 1));
886 node = jit_ldxi_d(u, JIT_FP, v->u.w);
890 jit_link_alist(node);
897 _jit_putargr_d(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
899 jit_node_t *node = NULL;
900 assert(v->code == jit_code_arg_d);
901 jit_inc_synth_wp(putargr_d, u, v);
903 if (jit_arg_f_reg_p(v->u.w))
904 jit_movr_d(JIT_FA0 - v->u.w, u);
906 node = jit_stxi_d(v->u.w, JIT_FP, u);
908 else if (jit_swf_p())
909 node = jit_stxi_d(arg_offset(v->u.w), JIT_FP, u);
911 if (jit_arg_reg_p(v->u.w))
912 jit_movr_d_ww(JIT_RA0 - v->u.w, JIT_RA0 - (v->u.w + 1), u);
914 node = jit_stxi_d(v->u.w, JIT_FP, u);
918 jit_link_alist(node);
925 _jit_putargi_d(jit_state_t *_jit, jit_float64_t u, jit_node_t *v)
928 jit_node_t *node = NULL;
929 assert(v->code == jit_code_arg_d);
930 jit_inc_synth_dp(putargi_d, u, v);
932 if (jit_arg_f_reg_p(v->u.w))
933 jit_movi_d(JIT_FA0 - v->u.w, u);
935 regno = jit_get_reg(jit_class_fpr);
936 jit_movi_d(regno, u);
937 node = jit_stxi_d(v->u.w, JIT_FP, regno);
938 jit_unget_reg(regno);
941 else if (jit_swf_p()) {
942 regno = jit_get_reg(jit_class_fpr);
943 jit_movi_d(regno, u);
944 node = jit_stxi_d(arg_offset(v->u.w), JIT_FP, regno);
945 jit_unget_reg(regno);
948 regno = jit_get_reg(jit_class_fpr);
949 jit_movi_d(regno, u);
950 if (jit_arg_reg_p(v->u.w))
951 jit_movr_d_ww(JIT_RA0 - v->u.w, JIT_RA0 - (v->u.w + 1), regno);
953 node = jit_stxi_d(v->u.w, JIT_FP, regno);
954 jit_unget_reg(regno);
958 jit_link_alist(node);
965 _jit_pushargr(jit_state_t *_jit, jit_int32_t u, jit_code_t code)
967 assert(_jitc->function);
968 jit_code_inc_synth_w(code, u);
970 if (jit_arg_reg_p(_jitc->function->call.argi)) {
971 jit_movr(JIT_RA0 - _jitc->function->call.argi, u);
972 ++_jitc->function->call.argi;
975 jit_stxi(_jitc->function->call.size, JIT_SP, u);
976 _jitc->function->call.size += sizeof(jit_word_t);
982 _jit_pushargi(jit_state_t *_jit, jit_word_t u, jit_code_t code)
985 assert(_jitc->function);
986 jit_code_inc_synth_w(code, u);
988 if (jit_arg_reg_p(_jitc->function->call.argi)) {
989 jit_movi(JIT_RA0 - _jitc->function->call.argi, u);
990 ++_jitc->function->call.argi;
993 regno = jit_get_reg(jit_class_gpr);
995 jit_stxi(_jitc->function->call.size, JIT_SP, regno);
996 jit_unget_reg(regno);
997 _jitc->function->call.size += sizeof(jit_word_t);
1003 _jit_pushargr_f(jit_state_t *_jit, jit_int32_t u)
1005 assert(_jitc->function);
1006 jit_inc_synth_w(pushargr_f, u);
1008 if (jit_cpu.abi && !(_jitc->function->call.call & jit_call_varargs)) {
1009 if (jit_arg_f_reg_p(_jitc->function->call.argf)) {
1010 jit_movr_f(JIT_FA0 - _jitc->function->call.argf, u);
1011 ++_jitc->function->call.argf;
1016 if (jit_arg_reg_p(_jitc->function->call.argi)) {
1017 jit_movr_f_w(JIT_RA0 - _jitc->function->call.argi, u);
1018 ++_jitc->function->call.argi;
1022 jit_stxi_f(_jitc->function->call.size, JIT_SP, u);
1023 _jitc->function->call.size += sizeof(jit_word_t);
1029 _jit_pushargi_f(jit_state_t *_jit, jit_float32_t u)
1032 assert(_jitc->function);
1033 jit_inc_synth_f(pushargi_f, u);
1035 if (jit_cpu.abi && !(_jitc->function->call.call & jit_call_varargs)) {
1036 if (jit_arg_f_reg_p(_jitc->function->call.argf)) {
1037 /* cannot jit_movi_f in the argument register because
1038 * float arguments are packed, and that would cause
1039 * either an assertion in debug mode, or overwritting
1041 regno = jit_get_reg(jit_class_fpr);
1042 jit_movi_f(regno, u);
1043 jit_movr_f(JIT_FA0 - _jitc->function->call.argf, regno);
1044 jit_unget_reg(regno);
1045 ++_jitc->function->call.argf;
1050 if (jit_arg_reg_p(_jitc->function->call.argi)) {
1051 jit_movi_f_w(JIT_RA0 - _jitc->function->call.argi, u);
1052 ++_jitc->function->call.argi;
1056 regno = jit_get_reg(jit_class_fpr);
1057 jit_movi_f(regno, u);
1058 jit_stxi_f(_jitc->function->call.size, JIT_SP, regno);
1059 jit_unget_reg(regno);
1060 _jitc->function->call.size += sizeof(jit_word_t);
1066 _jit_pushargr_d(jit_state_t *_jit, jit_int32_t u)
1068 assert(_jitc->function);
1069 jit_inc_synth_w(pushargr_d, u);
1071 if (jit_cpu.abi && !(_jitc->function->call.call & jit_call_varargs)) {
1072 if (jit_arg_d_reg_p(_jitc->function->call.argf)) {
1073 if (_jitc->function->call.argf & 1)
1074 ++_jitc->function->call.argf;
1075 jit_movr_d(JIT_FA0 - _jitc->function->call.argf, u);
1076 _jitc->function->call.argf += 2;
1081 if (_jitc->function->call.argi & 1)
1082 ++_jitc->function->call.argi;
1083 if (jit_arg_reg_p(_jitc->function->call.argi)) {
1084 jit_movr_d_ww(JIT_RA0 - _jitc->function->call.argi,
1085 JIT_RA0 - (_jitc->function->call.argi + 1),
1087 _jitc->function->call.argi += 2;
1091 if (_jitc->function->call.size & 7)
1092 _jitc->function->call.size += 4;
1093 jit_stxi_d(_jitc->function->call.size, JIT_SP, u);
1094 _jitc->function->call.size += sizeof(jit_float64_t);
1100 _jit_pushargi_d(jit_state_t *_jit, jit_float64_t u)
1103 assert(_jitc->function);
1104 jit_inc_synth_d(pushargi_d, u);
1106 if (jit_cpu.abi && !(_jitc->function->call.call & jit_call_varargs)) {
1107 if (jit_arg_d_reg_p(_jitc->function->call.argf)) {
1108 if (_jitc->function->call.argf & 1)
1109 ++_jitc->function->call.argf;
1110 jit_movi_d(JIT_FA0 - _jitc->function->call.argf, u);
1111 _jitc->function->call.argf += 2;
1116 if (_jitc->function->call.argi & 1)
1117 ++_jitc->function->call.argi;
1118 if (jit_arg_reg_p(_jitc->function->call.argi)) {
1119 jit_movi_d_ww(JIT_RA0 - _jitc->function->call.argi,
1120 JIT_RA0 - (_jitc->function->call.argi + 1),
1122 _jitc->function->call.argi += 2;
1126 if (_jitc->function->call.size & 7)
1127 _jitc->function->call.size += 4;
1128 regno = jit_get_reg(jit_class_fpr);
1129 jit_movi_d(regno, u);
1130 jit_stxi_d(_jitc->function->call.size, JIT_SP, regno);
1131 jit_unget_reg(regno);
1132 _jitc->function->call.size += sizeof(jit_float64_t);
1138 _jit_regarg_p(jit_state_t *_jit, jit_node_t *node, jit_int32_t regno)
1141 spec = jit_class(_rvs[regno].spec);
1142 if (spec & jit_class_arg) {
1143 regno = JIT_RA0 - regno;
1144 if (regno >= 0 && regno < node->v.w)
1146 if (jit_cpu.abi && spec & jit_class_fpr) {
1147 regno = JIT_FA0 - regno;
1148 if (regno >= 0 && regno < node->w.w)
1157 _jit_finishr(jit_state_t *_jit, jit_int32_t r0)
1160 assert(_jitc->function);
1161 jit_inc_synth_w(finishr, r0);
1162 if (_jitc->function->self.alen < _jitc->function->call.size)
1163 _jitc->function->self.alen = _jitc->function->call.size;
1164 node = jit_callr(r0);
1165 node->v.w = _jitc->function->self.argi;
1166 node->w.w = _jitc->function->call.argf;
1167 _jitc->function->call.argi = _jitc->function->call.argf =
1168 _jitc->function->call.size = 0;
1174 _jit_finishi(jit_state_t *_jit, jit_pointer_t i0)
1177 assert(_jitc->function);
1178 jit_inc_synth_w(finishi, (jit_word_t)i0);
1179 if (_jitc->function->self.alen < _jitc->function->call.size)
1180 _jitc->function->self.alen = _jitc->function->call.size;
1181 node = jit_calli(i0);
1182 node->v.w = _jitc->function->call.argi;
1183 node->w.w = _jitc->function->call.argf;
1184 _jitc->function->call.argi = _jitc->function->call.argf =
1185 _jitc->function->call.size = 0;
1192 _jit_retval_c(jit_state_t *_jit, jit_int32_t r0)
1194 jit_inc_synth_w(retval_c, r0);
1195 jit_extr_c(r0, JIT_RET);
1200 _jit_retval_uc(jit_state_t *_jit, jit_int32_t r0)
1202 jit_inc_synth_w(retval_uc, r0);
1203 jit_extr_uc(r0, JIT_RET);
1208 _jit_retval_s(jit_state_t *_jit, jit_int32_t r0)
1210 jit_inc_synth_w(retval_s, r0);
1211 jit_extr_s(r0, JIT_RET);
1216 _jit_retval_us(jit_state_t *_jit, jit_int32_t r0)
1218 jit_inc_synth_w(retval_us, r0);
1219 jit_extr_us(r0, JIT_RET);
1224 _jit_retval_i(jit_state_t *_jit, jit_int32_t r0)
1226 jit_inc_synth_w(retval_i, r0);
1228 jit_movr(r0, JIT_RET);
1233 _jit_retval_f(jit_state_t *_jit, jit_int32_t r0)
1235 jit_inc_synth_w(retval_f, r0);
1238 jit_movr_f(r0, JIT_FRET);
1240 else if (r0 != JIT_RET)
1241 jit_movr_w_f(r0, JIT_RET);
1246 _jit_retval_d(jit_state_t *_jit, jit_int32_t r0)
1248 jit_inc_synth_w(retval_d, r0);
1251 jit_movr_d(r0, JIT_FRET);
1253 else if (r0 != JIT_RET)
1254 jit_movr_ww_d(r0, JIT_RET, _R1);
1259 _emit_code(jit_state_t *_jit)
1270 jit_function_t func;
1271 #if DEVEL_DISASSEMBLER
1276 jit_int32_t info_offset;
1278 jit_int32_t const_offset;
1279 jit_int32_t patch_offset;
1281 #if DEVEL_DISASSEMBLER
1285 _jitc->function = NULL;
1288 jit_reglive_setup();
1290 _jitc->consts.data = NULL;
1291 _jitc->consts.offset = _jitc->consts.length = 0;
1300 undo.const_offset = undo.patch_offset = 0;
1301 # define assert_data(node) /**/
1302 #define case_rr(name, type) \
1303 case jit_code_##name##r##type: \
1304 name##r##type(rn(node->u.w), rn(node->v.w)); \
1306 #define case_rw(name, type) \
1307 case jit_code_##name##i##type: \
1308 name##i##type(rn(node->u.w), node->v.w); \
1310 #define case_vv(name, type) \
1311 case jit_code_##name##r##type: \
1313 swf_##name##r##type(rn(node->u.w), rn(node->v.w)); \
1315 vfp_##name##r##type(rn(node->u.w), rn(node->v.w)); \
1317 #define case_vw(name, type) \
1318 case jit_code_##name##i##type: \
1320 swf_##name##i##type(rn(node->u.w), node->v.w); \
1322 vfp_##name##i##type(rn(node->u.w), node->v.w); \
1324 #define case_wr(name, type) \
1325 case jit_code_##name##i##type: \
1326 name##i##type(node->u.w, rn(node->v.w)); \
1328 #define case_wv(name, type) \
1329 case jit_code_##name##i##type: \
1331 swf_##name##i##type(node->u.w, rn(node->v.w)); \
1333 vfp_##name##i##type(node->u.w, rn(node->v.w)); \
1335 #define case_rrr(name, type) \
1336 case jit_code_##name##r##type: \
1337 name##r##type(rn(node->u.w), \
1338 rn(node->v.w), rn(node->w.w)); \
1340 #define case_rrrr(name, type) \
1341 case jit_code_##name##r##type: \
1342 name##r##type(rn(node->u.q.l), rn(node->u.q.h), \
1343 rn(node->v.w), rn(node->w.w)); \
1345 #define case_vvv(name, type) \
1346 case jit_code_##name##r##type: \
1348 swf_##name##r##type(rn(node->u.w), \
1349 rn(node->v.w), rn(node->w.w)); \
1351 vfp_##name##r##type(rn(node->u.w), \
1352 rn(node->v.w), rn(node->w.w)); \
1354 #define case_rrw(name, type) \
1355 case jit_code_##name##i##type: \
1356 name##i##type(rn(node->u.w), rn(node->v.w), node->w.w); \
1358 #define case_rrrw(name, type) \
1359 case jit_code_##name##i##type: \
1360 name##i##type(rn(node->u.q.l), rn(node->u.q.h), \
1361 rn(node->v.w), node->w.w); \
1363 #define case_vvw(name, type) \
1364 case jit_code_##name##i##type: \
1366 swf_##name##i##type(rn(node->u.w), \
1367 rn(node->v.w), node->w.w); \
1369 vfp_##name##i##type(rn(node->u.w), \
1370 rn(node->v.w), node->w.w); \
1372 #define case_vvf(name) \
1373 case jit_code_##name##i_f: \
1374 assert_data(node); \
1376 swf_##name##i_f(rn(node->u.w), rn(node->v.w), \
1379 vfp_##name##i_f(rn(node->u.w), rn(node->v.w), \
1382 #define case_vvd(name) \
1383 case jit_code_##name##i_d: \
1384 assert_data(node); \
1386 swf_##name##i_d(rn(node->u.w), rn(node->v.w), \
1389 vfp_##name##i_d(rn(node->u.w), rn(node->v.w), \
1392 #define case_wrr(name, type) \
1393 case jit_code_##name##i##type: \
1394 name##i##type(node->u.w, rn(node->v.w), rn(node->w.w)); \
1396 #define case_wvv(name, type) \
1397 case jit_code_##name##i##type: \
1399 swf_##name##i##type(node->u.w, \
1400 rn(node->v.w), rn(node->w.w)); \
1402 vfp_##name##i##type(node->u.w, \
1403 rn(node->v.w), rn(node->w.w)); \
1405 #define case_brr(name, type) \
1406 case jit_code_##name##r##type: \
1408 assert(temp->code == jit_code_label || \
1409 temp->code == jit_code_epilog); \
1410 if (temp->flag & jit_flag_patch) \
1411 name##r##type(temp->u.w, rn(node->v.w), \
1414 word = name##r##type(_jit->pc.w, \
1415 rn(node->v.w), rn(node->w.w)); \
1416 patch(word, node, arm_patch_jump); \
1419 #define case_bvv(name, type) \
1420 case jit_code_##name##r##type: \
1422 assert(temp->code == jit_code_label || \
1423 temp->code == jit_code_epilog); \
1424 if (temp->flag & jit_flag_patch) { \
1426 swf_##name##r##type(temp->u.w, rn(node->v.w), \
1429 vfp_##name##r##type(temp->u.w, rn(node->v.w), \
1434 word = swf_##name##r##type(_jit->pc.w, \
1438 word = vfp_##name##r##type(_jit->pc.w, \
1441 patch(word, node, arm_patch_jump); \
1444 #define case_brw(name, type) \
1445 case jit_code_##name##i##type: \
1447 assert(temp->code == jit_code_label || \
1448 temp->code == jit_code_epilog); \
1449 if (temp->flag & jit_flag_patch) \
1450 name##i##type(temp->u.w, \
1451 rn(node->v.w), node->w.w); \
1453 word = name##i##type(_jit->pc.w, \
1454 rn(node->v.w), node->w.w); \
1455 patch(word, node, arm_patch_jump); \
1458 #define case_bvf(name) \
1459 case jit_code_##name##i_f: \
1461 assert(temp->code == jit_code_label || \
1462 temp->code == jit_code_epilog); \
1463 if (temp->flag & jit_flag_patch) { \
1465 swf_##name##i_f(temp->u.w, rn(node->v.w), \
1468 vfp_##name##i_f(temp->u.w, rn(node->v.w), \
1473 word = swf_##name##i_f(_jit->pc.w, \
1477 word = vfp_##name##i_f(_jit->pc.w, \
1480 patch(word, node, arm_patch_jump); \
1483 #define case_bvd(name) \
1484 case jit_code_##name##i_d: \
1486 assert(temp->code == jit_code_label || \
1487 temp->code == jit_code_epilog); \
1488 if (temp->flag & jit_flag_patch) { \
1490 swf_##name##i_d(temp->u.w, rn(node->v.w), \
1493 vfp_##name##i_d(temp->u.w, rn(node->v.w), \
1498 word = swf_##name##i_d(_jit->pc.w, \
1502 word = vfp_##name##i_d(_jit->pc.w, \
1505 patch(word, node, arm_patch_jump); \
1508 #if DEVEL_DISASSEMBLER
1511 for (node = _jitc->head; node; node = node->next) {
1512 if (_jit->pc.uc >= _jitc->code.end)
1515 #if DEVEL_DISASSEMBLER
1516 node->offset = (jit_uword_t)_jit->pc.w - (jit_uword_t)prevw;
1519 value = jit_classify(node->code);
1520 jit_regarg_set(node, value);
1521 switch (node->code) {
1522 case jit_code_align:
1523 /* Must align to a power of two */
1524 assert(!(node->u.w & (node->u.w - 1)));
1525 if ((word = _jit->pc.w & (node->u.w - 1)))
1526 nop(node->u.w - word);
1530 nop((node->u.w + 1) & ~1);
1532 nop((node->u.w + 3) & ~3);
1534 case jit_code_note: case jit_code_name:
1535 if (must_align_p(node->next))
1537 node->u.w = _jit->pc.w;
1539 case jit_code_label:
1540 if (must_align_p(node->next))
1542 /* remember label is defined */
1543 node->flag |= jit_flag_patch;
1544 node->u.w = _jit->pc.w;
1563 case_rrrr(qmul, _u);
1564 case_rrrw(qmul, _u);
1571 case_rrrr(qdiv, _u);
1572 case_rrrw(qdiv, _u);
1595 case_vv(trunc, _f_i);
1596 case_vv(trunc, _d_i);
1631 case_rr(bswap, _us);
1632 case_rr(bswap, _ui);
1638 casr(rn(node->u.w), rn(node->v.w),
1639 rn(node->w.q.l), rn(node->w.q.h));
1642 casi(rn(node->u.w), node->v.w,
1643 rn(node->w.q.l), rn(node->w.q.h));
1649 if (node->flag & jit_flag_node) {
1651 if (temp->code == jit_code_data ||
1652 (temp->code == jit_code_label &&
1653 (temp->flag & jit_flag_patch)))
1654 movi(rn(node->u.w), temp->u.w);
1656 assert(temp->code == jit_code_label ||
1657 temp->code == jit_code_epilog);
1658 word = movi_p(rn(node->u.w), temp->u.w);
1659 patch(word, node, arm_patch_word);
1663 movi(rn(node->u.w), node->v.w);
1707 case_brr(boadd, _u);
1708 case_brw(boadd, _u);
1711 case_brr(bxadd, _u);
1712 case_brw(bxadd, _u);
1715 case_brr(bosub, _u);
1716 case_brw(bosub, _u);
1719 case_brr(bxsub, _u);
1720 case_brw(bxsub, _u);
1747 case jit_code_movi_f:
1750 swf_movi_f(rn(node->u.w), node->v.f);
1752 vfp_movi_f(rn(node->u.w), node->v.f);
1781 case_vvv(unord, _f);
1795 case_bvv(bunlt, _f);
1797 case_bvv(bunle, _f);
1799 case_bvv(buneq, _f);
1801 case_bvv(bunge, _f);
1803 case_bvv(bungt, _f);
1805 case_bvv(bltgt, _f);
1809 case_bvv(bunord, _f);
1833 case jit_code_movi_d:
1836 swf_movi_d(rn(node->u.w), node->v.d);
1838 vfp_movi_d(rn(node->u.w), node->v.d);
1867 case_vvv(unord, _d);
1881 case_bvv(bunlt, _d);
1883 case_bvv(bunle, _d);
1885 case_bvv(buneq, _d);
1887 case_bvv(bunge, _d);
1889 case_bvv(bungt, _d);
1891 case_bvv(bltgt, _d);
1895 case_bvv(bunord, _d);
1899 jmpr(rn(node->u.w));
1903 if (node->flag & jit_flag_node) {
1905 assert(temp->code == jit_code_label ||
1906 temp->code == jit_code_epilog);
1907 if (temp->flag & jit_flag_patch)
1910 word = _jit->code.length -
1911 (_jit->pc.uc - _jit->code.ptr);
1912 if (jit_thumb_p()) word >>= 1;
1915 value = _s24P(word);
1916 word = jmpi_p(_jit->pc.w, value);
1917 patch(word, node, value ?
1918 arm_patch_jump : arm_patch_word);
1927 case jit_code_callr:
1929 callr(rn(node->u.w));
1931 case jit_code_calli:
1932 if (node->flag & jit_flag_node) {
1935 assert(temp->code == jit_code_label ||
1936 temp->code == jit_code_epilog);
1937 if (temp->flag & jit_flag_patch)
1938 calli(temp->u.w, 0);
1940 word = _jit->code.length -
1941 (_jit->pc.uc - _jit->code.ptr);
1942 if (jit_exchange_p())
1944 if (jit_thumb_p()) word >>= 1;
1947 value = _s24P(word);
1948 word = calli_p(_jit->pc.w, value);
1949 patch(word, node, value ?
1950 arm_patch_call : arm_patch_word);
1955 calli(node->u.w, jit_exchange_p());
1958 case jit_code_prolog:
1959 _jitc->function = _jitc->functions.ptr + node->w.w;
1961 undo.word = _jit->pc.w;
1962 memcpy(&undo.func, _jitc->function, sizeof(undo.func));
1963 #if DEVEL_DISASSEMBLER
1966 undo.data = _jitc->consts.data;
1967 undo.thumb = _jitc->thumb;
1968 undo.const_offset = _jitc->consts.offset;
1969 undo.patch_offset = _jitc->patches.offset;
1971 if (_jitc->data_info.ptr)
1972 undo.info_offset = _jitc->data_info.offset;
1976 compute_framesize();
1980 case jit_code_epilog:
1981 assert(_jitc->function == _jitc->functions.ptr + node->w.w);
1983 for (temp = undo.node->next;
1984 temp != node; temp = temp->next) {
1985 if (temp->code == jit_code_label ||
1986 temp->code == jit_code_epilog)
1987 temp->flag &= ~jit_flag_patch;
1989 temp->flag &= ~jit_flag_patch;
1991 _jit->pc.w = undo.word;
1992 /* undo.func.self.aoff and undo.func.regset should not
1993 * be undone, as they will be further updated, and are
1994 * the reason of the undo. */
1995 undo.func.self.aoff = _jitc->function->frame +
1996 _jitc->function->self.aoff;
1997 undo.func.need_frame = _jitc->function->need_frame;
1998 undo.func.need_return = _jitc->function->need_return;
1999 jit_regset_set(&undo.func.regset, &_jitc->function->regset);
2000 /* allocar information also does not need to be undone */
2001 undo.func.aoffoff = _jitc->function->aoffoff;
2002 undo.func.allocar = _jitc->function->allocar;
2003 /* swf_offset and check_reg_args must also not be undone */
2004 undo.func.swf_offset = _jitc->function->swf_offset;
2005 undo.func.save_reg_args = _jitc->function->save_reg_args;
2006 memcpy(_jitc->function, &undo.func, sizeof(undo.func));
2007 #if DEVEL_DISASSEMBLER
2010 invalidate_consts();
2011 _jitc->consts.data = undo.data;
2012 _jitc->thumb = undo.thumb;
2013 _jitc->consts.offset = undo.const_offset;
2014 _jitc->patches.offset = undo.patch_offset;
2016 if (_jitc->data_info.ptr)
2017 _jitc->data_info.offset = undo.info_offset;
2020 goto restart_function;
2022 /* remember label is defined */
2023 node->flag |= jit_flag_patch;
2024 node->u.w = _jit->pc.w;
2026 _jitc->function = NULL;
2029 case jit_code_movr_w_f:
2031 swf_movr_f(rn(node->u.w), rn(node->v.w));
2033 vfp_movr_f(rn(node->u.w), rn(node->v.w));
2035 case jit_code_movr_f_w:
2037 swf_movr_f(rn(node->u.w), rn(node->v.w));
2039 vfp_movr_f(rn(node->u.w), rn(node->v.w));
2041 case jit_code_movi_f_w:
2044 swf_movi_f(rn(node->u.w), node->v.f);
2046 vfp_movi_f(rn(node->u.w), node->v.f);
2048 case jit_code_movr_ww_d:
2050 swf_movr_d(rn(node->u.w), rn(node->v.w));
2052 vfp_movr_d(rn(node->u.w), rn(node->v.w));
2054 case jit_code_movr_d_ww:
2056 swf_movr_d(rn(node->u.w), rn(node->w.w));
2058 vfp_movr_d(rn(node->u.w), rn(node->w.w));
2060 case jit_code_movi_d_ww:
2063 swf_movi_d(rn(node->u.w), node->w.d);
2065 vfp_movi_d(rn(node->u.w), node->w.d);
2067 case jit_code_va_start:
2068 vastart(rn(node->u.w));
2070 case jit_code_va_arg:
2071 vaarg(rn(node->u.w), rn(node->v.w));
2073 case jit_code_va_arg_d:
2075 swf_vaarg_d(rn(node->u.w), rn(node->v.w));
2077 vfp_vaarg_d(rn(node->u.w), rn(node->v.w));
2079 case jit_code_live: case jit_code_ellipsis:
2080 case jit_code_va_push:
2081 case jit_code_allocai: case jit_code_allocar:
2082 case jit_code_arg_c: case jit_code_arg_s:
2083 case jit_code_arg_i:
2084 case jit_code_arg_f: case jit_code_arg_d:
2085 case jit_code_va_end:
2087 case jit_code_retr_c: case jit_code_reti_c:
2088 case jit_code_retr_uc: case jit_code_reti_uc:
2089 case jit_code_retr_s: case jit_code_reti_s:
2090 case jit_code_retr_us: case jit_code_reti_us:
2091 case jit_code_retr_i: case jit_code_reti_i:
2092 case jit_code_retr_f: case jit_code_reti_f:
2093 case jit_code_retr_d: case jit_code_reti_d:
2094 case jit_code_getarg_c: case jit_code_getarg_uc:
2095 case jit_code_getarg_s: case jit_code_getarg_us:
2096 case jit_code_getarg_i:
2097 case jit_code_getarg_f: case jit_code_getarg_d:
2098 case jit_code_putargr_c: case jit_code_putargi_c:
2099 case jit_code_putargr_uc: case jit_code_putargi_uc:
2100 case jit_code_putargr_s: case jit_code_putargi_s:
2101 case jit_code_putargr_us: case jit_code_putargi_us:
2102 case jit_code_putargr_i: case jit_code_putargi_i:
2103 case jit_code_putargr_f: case jit_code_putargi_f:
2104 case jit_code_putargr_d: case jit_code_putargi_d:
2105 case jit_code_pushargr_c: case jit_code_pushargi_c:
2106 case jit_code_pushargr_uc: case jit_code_pushargi_uc:
2107 case jit_code_pushargr_s: case jit_code_pushargi_s:
2108 case jit_code_pushargr_us: case jit_code_pushargi_us:
2109 case jit_code_pushargr_i: case jit_code_pushargi_i:
2110 case jit_code_pushargr_f: case jit_code_pushargi_f:
2111 case jit_code_pushargr_d: case jit_code_pushargi_d:
2112 case jit_code_retval_c: case jit_code_retval_uc:
2113 case jit_code_retval_s: case jit_code_retval_us:
2114 case jit_code_retval_i:
2115 case jit_code_retval_f: case jit_code_retval_d:
2116 case jit_code_prepare:
2117 case jit_code_finishr: case jit_code_finishi:
2122 jit_regarg_clr(node, value);
2123 assert(_jitc->regarg == 0 && _jitc->synth == 0);
2124 /* update register live state */
2127 if (_jitc->consts.length &&
2128 (_jit->pc.uc - _jitc->consts.data >= 3968 ||
2129 (jit_uword_t)_jit->pc.uc -
2130 (jit_uword_t)_jitc->consts.patches[0] >= 3968)) {
2131 /* longest sequence should be 64 bytes, but preventively
2132 * do not let it go past 128 remaining bytes before a flush */
2134 node->next->code != jit_code_jmpi &&
2135 node->next->code != jit_code_jmpr &&
2136 node->next->code != jit_code_epilog) {
2137 /* insert a jump, flush constants and continue */
2139 assert(!jit_thumb_p());
2142 patch_at(arm_patch_jump, word, _jit->pc.w);
2167 for (offset = 0; offset < _jitc->patches.offset; offset++) {
2168 assert(_jitc->patches.ptr[offset].kind & arm_patch_node);
2169 node = _jitc->patches.ptr[offset].node;
2170 word = _jitc->patches.ptr[offset].inst;
2171 if (!jit_thumb_p() &&
2172 (node->code == jit_code_movi ||
2173 (node->code == jit_code_calli &&
2174 (_jitc->patches.ptr[offset].kind & ~arm_patch_node) ==
2176 /* calculate where to patch word */
2177 value = *(jit_int32_t *)word;
2178 assert((value & 0x0f700000) == ARM_LDRI);
2179 /* offset may become negative (-4) if last instruction
2180 * before unconditional branch and data following
2181 * FIXME can this cause issues in the preprocessor prefetch
2182 * or something else? should not, as the constants are after
2183 * an unconditional jump */
2184 if (value & ARM_P) value = value & 0x00000fff;
2185 else value = -(value & 0x00000fff);
2186 word = word + 8 + value;
2188 value = node->code == jit_code_movi ? node->v.n->u.w : node->u.n->u.w;
2189 patch_at(_jitc->patches.ptr[offset].kind & ~arm_patch_node, word, value);
2192 jit_flush(_jit->code.ptr, _jit->pc.uc);
2194 return (_jit->code.ptr);
2198 # include "jit_rewind.c"
2199 # include "jit_arm-cpu.c"
2200 # include "jit_arm-swf.c"
2201 # include "jit_arm-vfp.c"
2202 # include "jit_fallback.c"
2206 jit_flush(void *fptr, void *tptr)
2208 #if defined(__GNUC__)
2209 jit_uword_t i, f, t, s;
2211 s = sysconf(_SC_PAGE_SIZE);
2212 f = (jit_uword_t)fptr & -s;
2213 t = (((jit_uword_t)tptr) + s - 1) & -s;
2214 for (i = f; i < t; i += s)
2215 __clear_cache((void *)i, (void *)(i + s));
2220 _emit_ldxi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
2222 ldxi_i(rn(r0), rn(r1), i0);
2226 _emit_stxi(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_int32_t r1)
2228 stxi_i(i0, rn(r0), rn(r1));
2232 _emit_ldxi_d(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
2235 swf_ldxi_d(rn(r0), rn(r1), i0);
2237 vfp_ldxi_d(rn(r0), rn(r1), i0);
2241 _emit_stxi_d(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_int32_t r1)
2244 swf_stxi_d(i0, rn(r0), rn(r1));
2246 vfp_stxi_d(i0, rn(r0), rn(r1));
2250 _jit_get_reg_pair(jit_state_t *_jit)
2252 /* bypass jit_get_reg() with argument or'ed with jit_class_chk
2253 * and try to find an consecutive, even free register pair, or
2254 * return JIT_NOREG if fail, as the cost of spills is greater
2255 * than splitting a double load/store in two operations. */
2256 if (jit_reg_free_p(_R0) && jit_reg_free_p(_R1)) {
2257 jit_regset_setbit(&_jitc->regarg, _R0);
2258 jit_regset_setbit(&_jitc->regarg, _R1);
2261 if (jit_reg_free_p(_R2) && jit_reg_free_p(_R3)) {
2262 jit_regset_setbit(&_jitc->regarg, _R2);
2263 jit_regset_setbit(&_jitc->regarg, _R3);
2266 if (jit_reg_free_p(_R4) && jit_reg_free_p(_R5)) {
2267 jit_regset_setbit(&_jitc->regarg, _R4);
2268 jit_regset_setbit(&_jitc->regarg, _R5);
2271 if (jit_reg_free_p(_R6) && jit_reg_free_p(_R7)) {
2272 jit_regset_setbit(&_jitc->regarg, _R6);
2273 jit_regset_setbit(&_jitc->regarg, _R7);
2276 if (jit_reg_free_p(_R8) && jit_reg_free_p(_R9)) {
2277 jit_regset_setbit(&_jitc->regarg, _R8);
2278 jit_regset_setbit(&_jitc->regarg, _R9);
2285 _jit_unget_reg_pair(jit_state_t *_jit, jit_int32_t reg)
2289 case _R0: jit_unget_reg(_R1); break;
2290 case _R2: jit_unget_reg(_R3); break;
2291 case _R4: jit_unget_reg(_R5); break;
2292 case _R6: jit_unget_reg(_R7); break;
2293 case _R8: jit_unget_reg(_R9); break;
2298 /* A prolog must be aligned at mod 4 bytes boundary.
2299 * This condition was not being required to be tested by
2300 * accident previously, but with the jit_frame and jit_tramp
2301 * code it is required */
2303 _must_align_p(jit_state_t *_jit, jit_node_t *node)
2305 if (jit_thumb_p() && (_jit->pc.w & 3)) {
2306 for (; node; node = node->next) {
2307 switch (node->code) {
2310 case jit_code_label:
2312 case jit_code_prolog:
2323 _load_const(jit_state_t *_jit, jit_bool_t uniq, jit_int32_t r0, jit_word_t i0)
2332 assert(!jit_thumb_p());
2334 /* use zero, a valid directly encoded immediate, to avoid the
2335 * need of a bitmask to know what offsets will be patched, so
2336 * that comparison will always fail for constants that cannot
2340 /* Actually, code is (currently at least) not self modifying,
2341 * so, any value reachable backwards is valid as a constant. */
2343 /* FIXME a quickly updateable/mutable hash table could be
2344 * better here, but most times only a few comparisons
2348 /* search in previous constant pool */
2349 if ((data = (jit_int32_t *)_jitc->consts.data)) {
2350 w = (jit_word_t)data;
2351 /* maximum backwards offset */
2352 base = (_jit->pc.w + 8) - 4092;
2354 /* can scan all possible available backward constants */
2357 base = (base - w) >> 2;
2358 size = _jitc->consts.size >> 2;
2359 for (offset = size - 1; offset >= base; offset--) {
2360 if (data[offset] == i0) {
2361 w = (jit_word_t)(data + offset);
2362 d = (_jit->pc.w + 8) - w;
2363 LDRIN(r0, _R15_REGNO, d);
2372 _jitc->consts.patches[_jitc->consts.offset++] = _jit->pc.w;
2373 /* (probably) positive forward offset */
2374 LDRI(r0, _R15_REGNO, 0);
2377 /* search already requested values */
2378 for (offset = 0; offset < _jitc->consts.length; offset++) {
2379 if (_jitc->consts.values[offset] == i0) {
2380 _jitc->consts.patches[_jitc->consts.offset++] = offset;
2387 /* cannot run out of space because of limited range
2388 * but assert anyway to catch logic errors */
2389 assert(_jitc->consts.length < 1024);
2390 assert(_jitc->consts.offset < 2048);
2392 _jitc->consts.patches[_jitc->consts.offset++] = _jitc->consts.length;
2393 _jitc->consts.values[_jitc->consts.length++] = i0;
2397 _flush_consts(jit_state_t *_jit)
2402 /* if no forward constants */
2403 if (!_jitc->consts.length)
2405 assert(!jit_thumb_p());
2407 _jitc->consts.data = _jit->pc.uc;
2408 _jitc->consts.size = _jitc->consts.length << 2;
2409 /* FIXME check will not overrun, otherwise, need to reallocate
2410 * code buffer and start over */
2411 jit_memcpy(_jitc->consts.data, _jitc->consts.values, _jitc->consts.size);
2412 _jit->pc.w += _jitc->consts.size;
2415 if (_jitc->data_info.ptr) {
2416 if (_jitc->data_info.offset >= _jitc->data_info.length) {
2417 jit_realloc((jit_pointer_t *)&_jitc->data_info.ptr,
2418 _jitc->data_info.length * sizeof(jit_data_info_t),
2419 (_jitc->data_info.length + 1024) *
2420 sizeof(jit_data_info_t));
2421 _jitc->data_info.length += 1024;
2423 _jitc->data_info.ptr[_jitc->data_info.offset].code = word;
2424 _jitc->data_info.ptr[_jitc->data_info.offset].length = _jitc->consts.size;
2425 ++_jitc->data_info.offset;
2429 for (offset = 0; offset < _jitc->consts.offset; offset += 2)
2430 patch_at(arm_patch_load, _jitc->consts.patches[offset],
2431 word + (_jitc->consts.patches[offset + 1] << 2));
2432 _jitc->consts.length = _jitc->consts.offset = 0;
2435 /* to be called if needing to start over a function */
2437 _invalidate_consts(jit_state_t *_jit)
2439 /* if no forward constants */
2440 if (_jitc->consts.length)
2441 _jitc->consts.length = _jitc->consts.offset = 0;
2445 _compute_framesize(jit_state_t *_jit)
2448 _jitc->framesize = sizeof(jit_word_t) * 2; /* lr+fp */
2449 for (reg = 0; reg < jit_size(iregs); reg++)
2450 if (jit_regset_tstbit(&_jitc->function->regset, iregs[reg]))
2451 _jitc->framesize += sizeof(jit_word_t);
2453 if (_jitc->function->save_reg_args)
2454 _jitc->framesize += 16;
2456 /* Make sure functions called have a 8 byte aligned stack */
2457 _jitc->framesize = (_jitc->framesize + 7) & -8;
2461 _patch(jit_state_t *_jit, jit_word_t instr, jit_node_t *node, jit_int32_t kind)
2465 assert(node->flag & jit_flag_node);
2466 if (node->code == jit_code_movi)
2467 flag = node->v.n->flag;
2469 flag = node->u.n->flag;
2470 assert(!(flag & jit_flag_patch));
2471 kind |= arm_patch_node;
2472 if (_jitc->patches.offset >= _jitc->patches.length) {
2473 jit_realloc((jit_pointer_t *)&_jitc->patches.ptr,
2474 _jitc->patches.length * sizeof(jit_patch_t),
2475 (_jitc->patches.length + 1024) * sizeof(jit_patch_t));
2476 _jitc->patches.length += 1024;
2478 _jitc->patches.ptr[_jitc->patches.offset].kind = kind;
2479 _jitc->patches.ptr[_jitc->patches.offset].inst = instr;
2480 _jitc->patches.ptr[_jitc->patches.offset].node = node;
2481 ++_jitc->patches.offset;