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)
244 /* By default generate extra instructions for unaligned load/store. */
245 jit_cpu.unaligned = 1;
246 /* Linux should only not handle unaligned vfp load/store */
247 jit_cpu.vfp_unaligned = 1;
251 _jit_init(jit_state_t *_jit)
254 static jit_bool_t first = 1;
256 _jitc->reglen = jit_size(_rvs) - 1;
258 /* jit_get_cpu() should have been already called, and only once */
260 /* cause register to never be allocated, because simple
261 * software float only allocates stack space for 8 slots */
262 for (regno = _D8; regno < _D7; regno++)
263 _rvs[regno].spec = 0;
266 for (regno = _S15; regno <= _D0; regno++)
267 _rvs[regno].spec &= ~rc(arg);
274 _jit_prolog(jit_state_t *_jit)
280 assert(jit_regset_cmp_ui(&_jitc->regarg, 0) == 0);
281 jit_regset_set_ui(&_jitc->regsav, 0);
282 offset = _jitc->functions.offset;
283 if (offset >= _jitc->functions.length) {
284 jit_realloc((jit_pointer_t *)&_jitc->functions.ptr,
285 _jitc->functions.length * sizeof(jit_function_t),
286 (_jitc->functions.length + 16) * sizeof(jit_function_t));
287 _jitc->functions.length += 16;
289 _jitc->function = _jitc->functions.ptr + _jitc->functions.offset++;
290 _jitc->function->self.size = stack_framesize;
291 _jitc->function->self.argi = _jitc->function->self.argf =
292 _jitc->function->self.alen = _jitc->function->self.aoff = 0;
293 _jitc->function->swf_offset = _jitc->function->save_reg_args =
294 _jitc->function->need_return = 0;
295 _jitc->function->self.call = jit_call_default;
296 jit_alloc((jit_pointer_t *)&_jitc->function->regoff,
297 _jitc->reglen * sizeof(jit_int32_t));
299 /* _no_link here does not mean the jit_link() call can be removed
301 * _jitc->function->prolog = jit_new_node(jit_code_prolog);
303 _jitc->function->prolog = jit_new_node_no_link(jit_code_prolog);
304 jit_link(_jitc->function->prolog);
305 _jitc->function->prolog->w.w = offset;
306 _jitc->function->epilog = jit_new_node_no_link(jit_code_epilog);
308 * v: offset in blocks vector
309 * w: offset in functions vector
311 _jitc->function->epilog->w.w = offset;
313 jit_regset_new(&_jitc->function->regset);
317 _jit_allocai(jit_state_t *_jit, jit_int32_t length)
319 assert(_jitc->function);
324 case 0: case 1: break;
325 case 2: _jitc->function->self.aoff &= -2; break;
326 case 3: case 4: _jitc->function->self.aoff &= -4; break;
327 default: _jitc->function->self.aoff &= -8; break;
329 _jitc->function->self.aoff -= length;
330 if (!_jitc->realize) {
331 jit_inc_synth_ww(allocai, _jitc->function->self.aoff, length);
334 return (_jitc->function->self.aoff);
338 _jit_allocar(jit_state_t *_jit, jit_int32_t u, jit_int32_t v)
341 assert(_jitc->function);
342 jit_inc_synth_ww(allocar, u, v);
343 if (!_jitc->function->allocar) {
344 _jitc->function->aoffoff = jit_allocai(sizeof(jit_int32_t));
345 _jitc->function->allocar = 1;
347 reg = jit_get_reg(jit_class_gpr);
349 jit_andi(reg, reg, -8);
350 jit_ldxi_i(u, JIT_FP, _jitc->function->aoffoff);
352 jit_addr(JIT_SP, JIT_SP, reg);
353 jit_stxi_i(_jitc->function->aoffoff, JIT_FP, u);
359 _jit_ret(jit_state_t *_jit)
362 assert(_jitc->function);
366 jit_patch_at(instr, _jitc->function->epilog);
371 _jit_retr(jit_state_t *_jit, jit_int32_t u, jit_code_t code)
373 jit_code_inc_synth_w(code, u);
374 jit_movr(JIT_RET, u);
380 _jit_reti(jit_state_t *_jit, jit_word_t u, jit_code_t code)
382 jit_code_inc_synth_w(code, u);
383 jit_movi(JIT_RET, u);
389 _jit_retr_f(jit_state_t *_jit, jit_int32_t u)
391 jit_inc_synth_w(retr_f, u);
394 jit_movr_f(JIT_FRET, u);
400 jit_movr_f_w(JIT_RET, u);
409 _jit_reti_f(jit_state_t *_jit, jit_float32_t u)
411 jit_inc_synth_f(reti_f, u);
413 jit_movi_f(JIT_FRET, u);
415 jit_movi_f_w(JIT_RET, u);
421 _jit_retr_d(jit_state_t *_jit, jit_int32_t u)
423 jit_inc_synth_w(retr_d, u);
426 jit_movr_d(JIT_FRET, u);
432 jit_movr_d_ww(JIT_RET, _R1, u);
441 _jit_reti_d(jit_state_t *_jit, jit_float64_t u)
443 jit_inc_synth_d(reti_d, u);
445 jit_movi_d(JIT_FRET, u);
447 jit_movi_d_ww(JIT_RET, _R1, u);
453 _jit_epilog(jit_state_t *_jit)
455 assert(_jitc->function);
456 assert(_jitc->function->epilog->next == NULL);
457 jit_link(_jitc->function->epilog);
458 _jitc->function = NULL;
462 _jit_arg_register_p(jit_state_t *_jit, jit_node_t *u)
464 if (!(u->code >= jit_code_arg_c && u->code <= jit_code_arg)) {
465 if (u->code == jit_code_arg_f) {
467 return (jit_arg_f_reg_p(u->u.w));
470 assert(u->code == jit_code_arg_d);
472 return (jit_arg_d_reg_p(u->u.w));
475 return (jit_arg_reg_p(u->u.w));
479 _jit_make_arg(jit_state_t *_jit, jit_node_t *node, jit_code_t code)
482 if (jit_arg_reg_p(_jitc->function->self.argi))
483 offset = _jitc->function->self.argi++;
485 offset = _jitc->function->self.size;
486 _jitc->function->self.size += sizeof(jit_word_t);
488 if (node == (jit_node_t *)0)
489 node = jit_new_node(code);
493 node->v.w = ++_jitc->function->self.argn;
499 _jit_make_arg_f(jit_state_t *_jit, jit_node_t *node)
502 if (jit_cpu.abi && !(_jitc->function->self.call & jit_call_varargs)) {
503 if (jit_arg_f_reg_p(_jitc->function->self.argf)) {
504 offset = _jitc->function->self.argf++;
509 if (jit_arg_reg_p(_jitc->function->self.argi)) {
510 offset = _jitc->function->self.argi++;
514 offset = _jitc->function->self.size;
515 _jitc->function->self.size += sizeof(jit_float32_t);
517 if (node == (jit_node_t *)0)
518 node = jit_new_node(jit_code_arg_f);
522 node->v.w = ++_jitc->function->self.argn;
528 _jit_make_arg_d(jit_state_t *_jit, jit_node_t *node)
531 if (jit_cpu.abi && !(_jitc->function->self.call & jit_call_varargs)) {
532 if (jit_arg_d_reg_p(_jitc->function->self.argf)) {
533 if (_jitc->function->self.argf & 1)
534 ++_jitc->function->self.argf;
535 offset = _jitc->function->self.argf;
536 _jitc->function->self.argf += 2;
541 if (_jitc->function->self.argi & 1)
542 ++_jitc->function->self.argi;
543 if (jit_arg_reg_p(_jitc->function->self.argi)) {
544 offset = _jitc->function->self.argi;
545 _jitc->function->self.argi += 2;
549 if (_jitc->function->self.size & 7)
550 _jitc->function->self.size += 4;
551 offset = _jitc->function->self.size;
552 _jitc->function->self.size += sizeof(jit_float64_t);
554 if (node == (jit_node_t *)0)
555 node = jit_new_node(jit_code_arg_d);
559 node->v.w = ++_jitc->function->self.argn;
565 _jit_ellipsis(jit_state_t *_jit)
567 if (_jitc->prepare) {
568 assert(!(_jitc->function->call.call & jit_call_varargs));
569 _jitc->function->call.call |= jit_call_varargs;
570 if (jit_cpu.abi && _jitc->function->call.argf)
574 assert(!(_jitc->function->self.call & jit_call_varargs));
575 _jitc->function->self.call |= jit_call_varargs;
577 if (jit_cpu.abi && _jitc->function->self.argf)
579 /* First 4 stack addresses need to be spilled r0-r3 */
580 if (jit_arg_reg_p(_jitc->function->self.argi))
581 _jitc->function->vagp = _jitc->function->self.argi * 4;
583 _jitc->function->vagp = 16;
585 jit_inc_synth(ellipsis);
594 _jit_va_push(jit_state_t *_jit, jit_int32_t u)
596 jit_inc_synth_w(va_push, u);
602 _jit_arg(jit_state_t *_jit, jit_code_t code)
604 assert(_jitc->function);
605 assert(!(_jitc->function->self.call & jit_call_varargs));
606 #if STRONG_TYPE_CHECKING
607 assert(code >= jit_code_arg_c && code <= jit_code_arg);
609 return (jit_make_arg((jit_node_t*)0, code));
613 _jit_arg_f(jit_state_t *_jit)
615 assert(_jitc->function);
616 assert(!(_jitc->function->self.call & jit_call_varargs));
617 return (jit_make_arg_f((jit_node_t*)0));
621 _jit_arg_d(jit_state_t *_jit)
623 assert(_jitc->function);
624 assert(!(_jitc->function->self.call & jit_call_varargs));
625 return (jit_make_arg_d((jit_node_t*)0));
629 _jit_getarg_c(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
631 jit_node_t *node = NULL;
632 assert_arg_type(v->code, jit_code_arg_c);
633 jit_inc_synth_wp(getarg_c, u, v);
635 node = jit_ldxi_c(u, JIT_FP, arg_offset(v->u.w));
636 else if (jit_arg_reg_p(v->u.w))
637 jit_extr_c(u, JIT_RA0 - v->u.w);
639 node = jit_ldxi_c(u, JIT_FP, v->u.w);
642 jit_link_alist(node);
649 _jit_getarg_uc(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
651 jit_node_t *node = NULL;
652 assert_arg_type(v->code, jit_code_arg_c);
653 jit_inc_synth_wp(getarg_uc, u, v);
655 node = jit_ldxi_uc(u, JIT_FP, arg_offset(v->u.w));
656 else if (jit_arg_reg_p(v->u.w))
657 jit_extr_uc(u, JIT_RA0 - v->u.w);
659 node = jit_ldxi_uc(u, JIT_FP, v->u.w);
662 jit_link_alist(node);
669 _jit_getarg_s(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
671 jit_node_t *node = NULL;
672 assert_arg_type(v->code, jit_code_arg_s);
673 jit_inc_synth_wp(getarg_s, u, v);
675 node = jit_ldxi_s(u, JIT_FP, arg_offset(v->u.w));
676 else if (jit_arg_reg_p(v->u.w))
677 jit_extr_s(u, JIT_RA0 - v->u.w);
679 node = jit_ldxi_s(u, JIT_FP, v->u.w);
682 jit_link_alist(node);
689 _jit_getarg_us(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
691 jit_node_t *node = NULL;
692 assert_arg_type(v->code, jit_code_arg_s);
693 jit_inc_synth_wp(getarg_us, u, v);
695 node = jit_ldxi_us(u, JIT_FP, arg_offset(v->u.w));
696 else if (jit_arg_reg_p(v->u.w))
697 jit_extr_us(u, JIT_RA0 - v->u.w);
699 node = jit_ldxi_us(u, JIT_FP, v->u.w);
702 jit_link_alist(node);
709 _jit_getarg_i(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
711 jit_node_t *node = NULL;
712 assert_arg_type(v->code, jit_code_arg_i);
713 jit_inc_synth_wp(getarg_i, u, v);
715 node = jit_ldxi_i(u, JIT_FP, arg_offset(v->u.w));
716 else if (jit_arg_reg_p(v->u.w))
717 jit_movr(u, JIT_RA0 - v->u.w);
719 node = jit_ldxi_i(u, JIT_FP, v->u.w);
722 jit_link_alist(node);
729 _jit_putargr(jit_state_t *_jit, jit_int32_t u, jit_node_t *v, jit_code_t code)
731 jit_node_t *node = NULL;
732 assert_putarg_type(code, v->code);
733 jit_code_inc_synth_wp(code, u, v);
735 node = jit_stxi(arg_offset(v->u.w), JIT_FP, u);
736 else if (jit_arg_reg_p(v->u.w))
737 jit_movr(JIT_RA0 - v->u.w, u);
739 node = jit_stxi(v->u.w, JIT_FP, u);
742 jit_link_alist(node);
749 _jit_putargi(jit_state_t *_jit, jit_word_t u, jit_node_t *v, jit_code_t code)
752 jit_node_t *node = NULL;
753 assert_putarg_type(code, v->code);
754 jit_code_inc_synth_wp(code, u, v);
756 regno = jit_get_reg(jit_class_gpr);
758 node = jit_stxi(arg_offset(v->u.w), JIT_FP, regno);
759 jit_unget_reg(regno);
761 else if (jit_arg_reg_p(v->u.w))
762 jit_movi(JIT_RA0 - v->u.w, u);
764 regno = jit_get_reg(jit_class_gpr);
766 node = jit_stxi(v->u.w, JIT_FP, regno);
767 jit_unget_reg(regno);
771 jit_link_alist(node);
778 _jit_getarg_f(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
780 jit_node_t *node = NULL;
781 assert(v->code == jit_code_arg_f);
782 jit_inc_synth_wp(getarg_f, u, v);
783 if (jit_cpu.abi && !(_jitc->function->self.call & jit_call_varargs)) {
784 if (jit_arg_f_reg_p(v->u.w))
785 jit_movr_f(u, JIT_FA0 - v->u.w);
787 node = jit_ldxi_f(u, JIT_FP, v->u.w);
789 else if (jit_swf_p())
790 node = jit_ldxi_f(u, JIT_FP, arg_offset(v->u.w));
792 if (jit_arg_reg_p(v->u.w))
793 jit_movr_w_f(u, JIT_RA0 - v->u.w);
795 node = jit_ldxi_f(u, JIT_FP, v->u.w);
799 jit_link_alist(node);
806 _jit_putargr_f(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
808 jit_node_t *node = NULL;
809 assert(v->code == jit_code_arg_f);
810 jit_inc_synth_wp(putargr_f, u, v);
812 if (jit_arg_f_reg_p(v->u.w))
813 jit_movr_f(JIT_FA0 - v->u.w, u);
815 node = jit_stxi_f(v->u.w, JIT_FP, u);
817 else if (jit_swf_p())
818 node = jit_stxi_f(arg_offset(v->u.w), JIT_FP, u);
820 if (jit_arg_reg_p(v->u.w))
821 jit_movr_f_w(JIT_RA0 - v->u.w, u);
823 node = jit_stxi_f(v->u.w, JIT_FP, u);
827 jit_link_alist(node);
834 _jit_putargi_f(jit_state_t *_jit, jit_float32_t u, jit_node_t *v)
837 jit_node_t *node = NULL;
838 assert(v->code == jit_code_arg_f);
839 jit_inc_synth_fp(putargi_f, u, v);
841 if (jit_arg_f_reg_p(v->u.w))
842 jit_movi_f(JIT_FA0 - v->u.w, u);
844 regno = jit_get_reg(jit_class_fpr);
845 jit_movi_f(regno, u);
846 node = jit_stxi_f(v->u.w, JIT_FP, regno);
847 jit_unget_reg(regno);
850 else if (jit_swf_p()) {
851 regno = jit_get_reg(jit_class_fpr);
852 jit_movi_f(regno, u);
853 node = jit_stxi_f(arg_offset(v->u.w), JIT_FP, regno);
854 jit_unget_reg(regno);
857 regno = jit_get_reg(jit_class_fpr);
858 jit_movi_f(regno, u);
859 if (jit_arg_reg_p(v->u.w))
860 jit_movr_f_w(JIT_RA0 - v->u.w, regno);
862 node = jit_stxi_f(v->u.w, JIT_FP, regno);
863 jit_unget_reg(regno);
867 jit_link_alist(node);
874 _jit_getarg_d(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
876 jit_node_t *node = NULL;
877 assert(v->code == jit_code_arg_d);
878 jit_inc_synth_wp(getarg_d, u, v);
879 if (jit_cpu.abi && !(_jitc->function->self.call & jit_call_varargs)) {
880 if (jit_arg_f_reg_p(v->u.w))
881 jit_movr_d(u, JIT_FA0 - v->u.w);
883 node = jit_ldxi_d(u, JIT_FP, v->u.w);
885 else if (jit_swf_p())
886 node = jit_ldxi_d(u, JIT_FP, arg_offset(v->u.w));
888 if (jit_arg_reg_p(v->u.w))
889 jit_movr_ww_d(u, JIT_RA0 - v->u.w, JIT_RA0 - (v->u.w + 1));
891 node = jit_ldxi_d(u, JIT_FP, v->u.w);
895 jit_link_alist(node);
902 _jit_putargr_d(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
904 jit_node_t *node = NULL;
905 assert(v->code == jit_code_arg_d);
906 jit_inc_synth_wp(putargr_d, u, v);
908 if (jit_arg_f_reg_p(v->u.w))
909 jit_movr_d(JIT_FA0 - v->u.w, u);
911 node = jit_stxi_d(v->u.w, JIT_FP, u);
913 else if (jit_swf_p())
914 node = jit_stxi_d(arg_offset(v->u.w), JIT_FP, u);
916 if (jit_arg_reg_p(v->u.w))
917 jit_movr_d_ww(JIT_RA0 - v->u.w, JIT_RA0 - (v->u.w + 1), u);
919 node = jit_stxi_d(v->u.w, JIT_FP, u);
923 jit_link_alist(node);
930 _jit_putargi_d(jit_state_t *_jit, jit_float64_t u, jit_node_t *v)
933 jit_node_t *node = NULL;
934 assert(v->code == jit_code_arg_d);
935 jit_inc_synth_dp(putargi_d, u, v);
937 if (jit_arg_f_reg_p(v->u.w))
938 jit_movi_d(JIT_FA0 - v->u.w, u);
940 regno = jit_get_reg(jit_class_fpr);
941 jit_movi_d(regno, u);
942 node = jit_stxi_d(v->u.w, JIT_FP, regno);
943 jit_unget_reg(regno);
946 else if (jit_swf_p()) {
947 regno = jit_get_reg(jit_class_fpr);
948 jit_movi_d(regno, u);
949 node = jit_stxi_d(arg_offset(v->u.w), JIT_FP, regno);
950 jit_unget_reg(regno);
953 regno = jit_get_reg(jit_class_fpr);
954 jit_movi_d(regno, u);
955 if (jit_arg_reg_p(v->u.w))
956 jit_movr_d_ww(JIT_RA0 - v->u.w, JIT_RA0 - (v->u.w + 1), regno);
958 node = jit_stxi_d(v->u.w, JIT_FP, regno);
959 jit_unget_reg(regno);
963 jit_link_alist(node);
970 _jit_pushargr(jit_state_t *_jit, jit_int32_t u, jit_code_t code)
972 assert(_jitc->function);
973 jit_code_inc_synth_w(code, u);
975 if (jit_arg_reg_p(_jitc->function->call.argi)) {
976 jit_movr(JIT_RA0 - _jitc->function->call.argi, u);
977 ++_jitc->function->call.argi;
980 jit_stxi(_jitc->function->call.size, JIT_SP, u);
981 _jitc->function->call.size += sizeof(jit_word_t);
987 _jit_pushargi(jit_state_t *_jit, jit_word_t u, jit_code_t code)
990 assert(_jitc->function);
991 jit_code_inc_synth_w(code, u);
993 if (jit_arg_reg_p(_jitc->function->call.argi)) {
994 jit_movi(JIT_RA0 - _jitc->function->call.argi, u);
995 ++_jitc->function->call.argi;
998 regno = jit_get_reg(jit_class_gpr);
1000 jit_stxi(_jitc->function->call.size, JIT_SP, regno);
1001 jit_unget_reg(regno);
1002 _jitc->function->call.size += sizeof(jit_word_t);
1008 _jit_pushargr_f(jit_state_t *_jit, jit_int32_t u)
1010 assert(_jitc->function);
1011 jit_inc_synth_w(pushargr_f, u);
1013 if (jit_cpu.abi && !(_jitc->function->call.call & jit_call_varargs)) {
1014 if (jit_arg_f_reg_p(_jitc->function->call.argf)) {
1015 jit_movr_f(JIT_FA0 - _jitc->function->call.argf, u);
1016 ++_jitc->function->call.argf;
1021 if (jit_arg_reg_p(_jitc->function->call.argi)) {
1022 jit_movr_f_w(JIT_RA0 - _jitc->function->call.argi, u);
1023 ++_jitc->function->call.argi;
1027 jit_stxi_f(_jitc->function->call.size, JIT_SP, u);
1028 _jitc->function->call.size += sizeof(jit_word_t);
1034 _jit_pushargi_f(jit_state_t *_jit, jit_float32_t u)
1037 assert(_jitc->function);
1038 jit_inc_synth_f(pushargi_f, u);
1040 if (jit_cpu.abi && !(_jitc->function->call.call & jit_call_varargs)) {
1041 if (jit_arg_f_reg_p(_jitc->function->call.argf)) {
1042 /* cannot jit_movi_f in the argument register because
1043 * float arguments are packed, and that would cause
1044 * either an assertion in debug mode, or overwritting
1046 regno = jit_get_reg(jit_class_fpr);
1047 jit_movi_f(regno, u);
1048 jit_movr_f(JIT_FA0 - _jitc->function->call.argf, regno);
1049 jit_unget_reg(regno);
1050 ++_jitc->function->call.argf;
1055 if (jit_arg_reg_p(_jitc->function->call.argi)) {
1056 jit_movi_f_w(JIT_RA0 - _jitc->function->call.argi, u);
1057 ++_jitc->function->call.argi;
1061 regno = jit_get_reg(jit_class_fpr);
1062 jit_movi_f(regno, u);
1063 jit_stxi_f(_jitc->function->call.size, JIT_SP, regno);
1064 jit_unget_reg(regno);
1065 _jitc->function->call.size += sizeof(jit_word_t);
1071 _jit_pushargr_d(jit_state_t *_jit, jit_int32_t u)
1073 assert(_jitc->function);
1074 jit_inc_synth_w(pushargr_d, u);
1076 if (jit_cpu.abi && !(_jitc->function->call.call & jit_call_varargs)) {
1077 if (jit_arg_d_reg_p(_jitc->function->call.argf)) {
1078 if (_jitc->function->call.argf & 1)
1079 ++_jitc->function->call.argf;
1080 jit_movr_d(JIT_FA0 - _jitc->function->call.argf, u);
1081 _jitc->function->call.argf += 2;
1086 if (_jitc->function->call.argi & 1)
1087 ++_jitc->function->call.argi;
1088 if (jit_arg_reg_p(_jitc->function->call.argi)) {
1089 jit_movr_d_ww(JIT_RA0 - _jitc->function->call.argi,
1090 JIT_RA0 - (_jitc->function->call.argi + 1),
1092 _jitc->function->call.argi += 2;
1096 if (_jitc->function->call.size & 7)
1097 _jitc->function->call.size += 4;
1098 jit_stxi_d(_jitc->function->call.size, JIT_SP, u);
1099 _jitc->function->call.size += sizeof(jit_float64_t);
1105 _jit_pushargi_d(jit_state_t *_jit, jit_float64_t u)
1108 assert(_jitc->function);
1109 jit_inc_synth_d(pushargi_d, u);
1111 if (jit_cpu.abi && !(_jitc->function->call.call & jit_call_varargs)) {
1112 if (jit_arg_d_reg_p(_jitc->function->call.argf)) {
1113 if (_jitc->function->call.argf & 1)
1114 ++_jitc->function->call.argf;
1115 jit_movi_d(JIT_FA0 - _jitc->function->call.argf, u);
1116 _jitc->function->call.argf += 2;
1121 if (_jitc->function->call.argi & 1)
1122 ++_jitc->function->call.argi;
1123 if (jit_arg_reg_p(_jitc->function->call.argi)) {
1124 jit_movi_d_ww(JIT_RA0 - _jitc->function->call.argi,
1125 JIT_RA0 - (_jitc->function->call.argi + 1),
1127 _jitc->function->call.argi += 2;
1131 if (_jitc->function->call.size & 7)
1132 _jitc->function->call.size += 4;
1133 regno = jit_get_reg(jit_class_fpr);
1134 jit_movi_d(regno, u);
1135 jit_stxi_d(_jitc->function->call.size, JIT_SP, regno);
1136 jit_unget_reg(regno);
1137 _jitc->function->call.size += sizeof(jit_float64_t);
1143 _jit_regarg_p(jit_state_t *_jit, jit_node_t *node, jit_int32_t regno)
1146 spec = jit_class(_rvs[regno].spec);
1147 if (spec & jit_class_arg) {
1148 regno = JIT_RA0 - regno;
1149 if (regno >= 0 && regno < node->v.w)
1151 if (jit_cpu.abi && spec & jit_class_fpr) {
1152 regno = JIT_FA0 - regno;
1153 if (regno >= 0 && regno < node->w.w)
1162 _jit_finishr(jit_state_t *_jit, jit_int32_t r0)
1165 assert(_jitc->function);
1166 jit_inc_synth_w(finishr, r0);
1167 if (_jitc->function->self.alen < _jitc->function->call.size)
1168 _jitc->function->self.alen = _jitc->function->call.size;
1169 node = jit_callr(r0);
1170 node->v.w = _jitc->function->self.argi;
1171 node->w.w = _jitc->function->call.argf;
1172 _jitc->function->call.argi = _jitc->function->call.argf =
1173 _jitc->function->call.size = 0;
1179 _jit_finishi(jit_state_t *_jit, jit_pointer_t i0)
1182 assert(_jitc->function);
1183 jit_inc_synth_w(finishi, (jit_word_t)i0);
1184 if (_jitc->function->self.alen < _jitc->function->call.size)
1185 _jitc->function->self.alen = _jitc->function->call.size;
1186 node = jit_calli(i0);
1187 node->v.w = _jitc->function->call.argi;
1188 node->w.w = _jitc->function->call.argf;
1189 _jitc->function->call.argi = _jitc->function->call.argf =
1190 _jitc->function->call.size = 0;
1197 _jit_retval_c(jit_state_t *_jit, jit_int32_t r0)
1199 jit_inc_synth_w(retval_c, r0);
1200 jit_extr_c(r0, JIT_RET);
1205 _jit_retval_uc(jit_state_t *_jit, jit_int32_t r0)
1207 jit_inc_synth_w(retval_uc, r0);
1208 jit_extr_uc(r0, JIT_RET);
1213 _jit_retval_s(jit_state_t *_jit, jit_int32_t r0)
1215 jit_inc_synth_w(retval_s, r0);
1216 jit_extr_s(r0, JIT_RET);
1221 _jit_retval_us(jit_state_t *_jit, jit_int32_t r0)
1223 jit_inc_synth_w(retval_us, r0);
1224 jit_extr_us(r0, JIT_RET);
1229 _jit_retval_i(jit_state_t *_jit, jit_int32_t r0)
1231 jit_inc_synth_w(retval_i, r0);
1233 jit_movr(r0, JIT_RET);
1238 _jit_retval_f(jit_state_t *_jit, jit_int32_t r0)
1240 jit_inc_synth_w(retval_f, r0);
1243 jit_movr_f(r0, JIT_FRET);
1245 else if (r0 != JIT_RET)
1246 jit_movr_w_f(r0, JIT_RET);
1251 _jit_retval_d(jit_state_t *_jit, jit_int32_t r0)
1253 jit_inc_synth_w(retval_d, r0);
1256 jit_movr_d(r0, JIT_FRET);
1258 else if (r0 != JIT_RET)
1259 jit_movr_ww_d(r0, JIT_RET, _R1);
1264 _emit_code(jit_state_t *_jit)
1275 jit_function_t func;
1276 #if DEVEL_DISASSEMBLER
1281 jit_int32_t info_offset;
1283 jit_int32_t const_offset;
1284 jit_int32_t patch_offset;
1286 #if DEVEL_DISASSEMBLER
1290 _jitc->function = NULL;
1293 jit_reglive_setup();
1295 _jitc->consts.data = NULL;
1296 _jitc->consts.offset = _jitc->consts.length = 0;
1305 undo.const_offset = undo.patch_offset = 0;
1306 # define assert_data(node) /**/
1307 #define case_rr(name, type) \
1308 case jit_code_##name##r##type: \
1309 name##r##type(rn(node->u.w), rn(node->v.w)); \
1311 #define case_rw(name, type) \
1312 case jit_code_##name##i##type: \
1313 name##i##type(rn(node->u.w), node->v.w); \
1315 #define case_vv(name, type) \
1316 case jit_code_##name##r##type: \
1318 swf_##name##r##type(rn(node->u.w), rn(node->v.w)); \
1320 vfp_##name##r##type(rn(node->u.w), rn(node->v.w)); \
1322 #define case_vw(name, type) \
1323 case jit_code_##name##i##type: \
1325 swf_##name##i##type(rn(node->u.w), node->v.w); \
1327 vfp_##name##i##type(rn(node->u.w), node->v.w); \
1329 #define case_wr(name, type) \
1330 case jit_code_##name##i##type: \
1331 name##i##type(node->u.w, rn(node->v.w)); \
1333 #define case_wv(name, type) \
1334 case jit_code_##name##i##type: \
1336 swf_##name##i##type(node->u.w, rn(node->v.w)); \
1338 vfp_##name##i##type(node->u.w, rn(node->v.w)); \
1340 #define case_rrr(name, type) \
1341 case jit_code_##name##r##type: \
1342 name##r##type(rn(node->u.w), \
1343 rn(node->v.w), rn(node->w.w)); \
1345 #define case_rqr(name, type) \
1346 case jit_code_##name##r##type: \
1348 swf_##name##r##type(rn(node->u.w), rn(node->v.q.l), \
1349 rn(node->v.q.h), rn(node->w.w));\
1351 vfp_##name##r##type(rn(node->u.w), rn(node->v.q.l), \
1352 rn(node->v.q.h), rn(node->w.w));\
1353 case jit_code_##name##i##type: \
1355 #define case_rrrr(name, type) \
1356 case jit_code_##name##r##type: \
1357 name##r##type(rn(node->u.q.l), rn(node->u.q.h), \
1358 rn(node->v.w), rn(node->w.w)); \
1360 #define case_vvv(name, type) \
1361 case jit_code_##name##r##type: \
1363 swf_##name##r##type(rn(node->u.w), \
1364 rn(node->v.w), rn(node->w.w)); \
1366 vfp_##name##r##type(rn(node->u.w), \
1367 rn(node->v.w), rn(node->w.w)); \
1369 #define case_rrw(name, type) \
1370 case jit_code_##name##i##type: \
1371 name##i##type(rn(node->u.w), rn(node->v.w), node->w.w); \
1373 #define case_rrrw(name, type) \
1374 case jit_code_##name##i##type: \
1375 name##i##type(rn(node->u.q.l), rn(node->u.q.h), \
1376 rn(node->v.w), node->w.w); \
1378 #define case_vvw(name, type) \
1379 case jit_code_##name##i##type: \
1381 swf_##name##i##type(rn(node->u.w), \
1382 rn(node->v.w), node->w.w); \
1384 vfp_##name##i##type(rn(node->u.w), \
1385 rn(node->v.w), node->w.w); \
1387 #define case_vvf(name) \
1388 case jit_code_##name##i_f: \
1389 assert_data(node); \
1391 swf_##name##i_f(rn(node->u.w), rn(node->v.w), \
1394 vfp_##name##i_f(rn(node->u.w), rn(node->v.w), \
1397 #define case_vvd(name) \
1398 case jit_code_##name##i_d: \
1399 assert_data(node); \
1401 swf_##name##i_d(rn(node->u.w), rn(node->v.w), \
1404 vfp_##name##i_d(rn(node->u.w), rn(node->v.w), \
1407 #define case_wrr(name, type) \
1408 case jit_code_##name##i##type: \
1409 name##i##type(node->u.w, rn(node->v.w), rn(node->w.w)); \
1411 #define case_wvv(name, type) \
1412 case jit_code_##name##i##type: \
1414 swf_##name##i##type(node->u.w, \
1415 rn(node->v.w), rn(node->w.w)); \
1417 vfp_##name##i##type(node->u.w, \
1418 rn(node->v.w), rn(node->w.w)); \
1420 #define case_brr(name, type) \
1421 case jit_code_##name##r##type: \
1423 assert(temp->code == jit_code_label || \
1424 temp->code == jit_code_epilog); \
1425 if (temp->flag & jit_flag_patch) \
1426 name##r##type(temp->u.w, rn(node->v.w), \
1429 word = name##r##type(_jit->pc.w, \
1430 rn(node->v.w), rn(node->w.w)); \
1431 patch(word, node, arm_patch_jump); \
1434 #define case_bvv(name, type) \
1435 case jit_code_##name##r##type: \
1437 assert(temp->code == jit_code_label || \
1438 temp->code == jit_code_epilog); \
1439 if (temp->flag & jit_flag_patch) { \
1441 swf_##name##r##type(temp->u.w, rn(node->v.w), \
1444 vfp_##name##r##type(temp->u.w, rn(node->v.w), \
1449 word = swf_##name##r##type(_jit->pc.w, \
1453 word = vfp_##name##r##type(_jit->pc.w, \
1456 patch(word, node, arm_patch_jump); \
1459 #define case_brw(name, type) \
1460 case jit_code_##name##i##type: \
1462 assert(temp->code == jit_code_label || \
1463 temp->code == jit_code_epilog); \
1464 if (temp->flag & jit_flag_patch) \
1465 name##i##type(temp->u.w, \
1466 rn(node->v.w), node->w.w); \
1468 word = name##i##type(_jit->pc.w, \
1469 rn(node->v.w), node->w.w); \
1470 patch(word, node, arm_patch_jump); \
1473 #define case_bvf(name) \
1474 case jit_code_##name##i_f: \
1476 assert(temp->code == jit_code_label || \
1477 temp->code == jit_code_epilog); \
1478 if (temp->flag & jit_flag_patch) { \
1480 swf_##name##i_f(temp->u.w, rn(node->v.w), \
1483 vfp_##name##i_f(temp->u.w, rn(node->v.w), \
1488 word = swf_##name##i_f(_jit->pc.w, \
1492 word = vfp_##name##i_f(_jit->pc.w, \
1495 patch(word, node, arm_patch_jump); \
1498 #define case_bvd(name) \
1499 case jit_code_##name##i_d: \
1501 assert(temp->code == jit_code_label || \
1502 temp->code == jit_code_epilog); \
1503 if (temp->flag & jit_flag_patch) { \
1505 swf_##name##i_d(temp->u.w, rn(node->v.w), \
1508 vfp_##name##i_d(temp->u.w, rn(node->v.w), \
1513 word = swf_##name##i_d(_jit->pc.w, \
1517 word = vfp_##name##i_d(_jit->pc.w, \
1520 patch(word, node, arm_patch_jump); \
1523 #if DEVEL_DISASSEMBLER
1526 for (node = _jitc->head; node; node = node->next) {
1527 if (_jit->pc.uc >= _jitc->code.end)
1530 #if DEVEL_DISASSEMBLER
1531 node->offset = (jit_uword_t)_jit->pc.w - (jit_uword_t)prevw;
1534 value = jit_classify(node->code);
1535 jit_regarg_set(node, value);
1536 switch (node->code) {
1537 case jit_code_align:
1538 /* Must align to a power of two */
1539 assert(!(node->u.w & (node->u.w - 1)));
1540 if ((word = _jit->pc.w & (node->u.w - 1)))
1541 nop(node->u.w - word);
1545 nop((node->u.w + 1) & ~1);
1547 nop((node->u.w + 3) & ~3);
1549 case jit_code_note: case jit_code_name:
1550 if (must_align_p(node->next))
1552 node->u.w = _jit->pc.w;
1554 case jit_code_label:
1555 if (must_align_p(node->next))
1557 /* remember label is defined */
1558 node->flag |= jit_flag_patch;
1559 node->u.w = _jit->pc.w;
1582 case_rrrr(qmul, _u);
1583 case_rrrw(qmul, _u);
1590 case_rrrr(qdiv, _u);
1591 case_rrrw(qdiv, _u);
1598 #define qlshr(r0, r1, r2, r3) fallback_qlshr(r0, r1, r2, r3)
1599 #define qlshi(r0, r1, r2, i0) fallback_qlshi(r0, r1, r2, i0)
1600 #define qlshr_u(r0, r1, r2, r3) fallback_qlshr_u(r0, r1, r2, r3)
1601 #define qlshi_u(r0, r1, r2, i0) fallback_qlshi_u(r0, r1, r2, i0)
1604 case_rrrr(qlsh, _u);
1605 case_rrrw(qlsh, _u);
1610 #define qrshr(r0, r1, r2, r3) fallback_qrshr(r0, r1, r2, r3)
1611 #define qrshi(r0, r1, r2, i0) fallback_qrshi(r0, r1, r2, i0)
1612 #define qrshr_u(r0, r1, r2, r3) fallback_qrshr_u(r0, r1, r2, r3)
1613 #define qrshi_u(r0, r1, r2, i0) fallback_qrshi_u(r0, r1, r2, i0)
1616 case_rrrr(qrsh, _u);
1617 case_rrrw(qrsh, _u);
1636 case_vv(trunc, _f_i);
1637 case_vv(trunc, _d_i);
1658 case jit_code_unldr:
1659 unldr(rn(node->u.w), rn(node->v.w), node->w.w);
1661 case jit_code_unldi:
1662 unldi(rn(node->u.w), node->v.w, node->w.w);
1664 case jit_code_unldr_u:
1665 unldr_u(rn(node->u.w), rn(node->v.w), node->w.w);
1667 case jit_code_unldi_u:
1668 unldi_u(rn(node->u.w), node->v.w, node->w.w);
1682 case jit_code_unstr:
1683 unstr(rn(node->u.w), rn(node->v.w), node->w.w);
1685 case jit_code_unsti:
1686 unsti(node->u.w, rn(node->v.w), node->w.w);
1690 case_rr(bswap, _us);
1691 case_rr(bswap, _ui);
1693 extr(rn(node->u.w), rn(node->v.w), node->w.q.l, node->w.q.h);
1695 case jit_code_extr_u:
1696 extr_u(rn(node->u.w), rn(node->v.w), node->w.q.l, node->w.q.h);
1699 depr(rn(node->u.w), rn(node->v.w), node->w.q.l, node->w.q.h);
1702 depi(rn(node->u.w), node->v.w, node->w.q.l, node->w.q.h);
1709 casr(rn(node->u.w), rn(node->v.w),
1710 rn(node->w.q.l), rn(node->w.q.h));
1713 casi(rn(node->u.w), node->v.w,
1714 rn(node->w.q.l), rn(node->w.q.h));
1720 if (node->flag & jit_flag_node) {
1722 if (temp->code == jit_code_data ||
1723 (temp->code == jit_code_label &&
1724 (temp->flag & jit_flag_patch)))
1725 movi(rn(node->u.w), temp->u.w);
1727 assert(temp->code == jit_code_label ||
1728 temp->code == jit_code_epilog);
1729 word = movi_p(rn(node->u.w), temp->u.w);
1730 patch(word, node, arm_patch_word);
1734 movi(rn(node->u.w), node->v.w);
1778 case_brr(boadd, _u);
1779 case_brw(boadd, _u);
1782 case_brr(bxadd, _u);
1783 case_brw(bxadd, _u);
1786 case_brr(bosub, _u);
1787 case_brw(bosub, _u);
1790 case_brr(bxsub, _u);
1791 case_brw(bxsub, _u);
1817 case jit_code_unldr_x:
1819 swf_unldr_x(rn(node->u.w), rn(node->v.w), node->w.w);
1821 vfp_unldr_x(rn(node->u.w), rn(node->v.w), node->w.w);
1823 case jit_code_unldi_x:
1825 swf_unldi_x(rn(node->u.w), node->v.w, node->w.w);
1827 vfp_unldi_x(rn(node->u.w), node->v.w, node->w.w);
1833 case jit_code_unstr_x:
1835 swf_unstr_x(rn(node->u.w), rn(node->v.w), node->w.w);
1837 vfp_unstr_x(rn(node->u.w), rn(node->v.w), node->w.w);
1839 case jit_code_unsti_x:
1841 swf_unsti_x(node->u.w, rn(node->v.w), node->w.w);
1843 vfp_unsti_x(node->u.w, rn(node->v.w), node->w.w);
1846 case jit_code_movi_f:
1849 swf_movi_f(rn(node->u.w), node->v.f);
1851 vfp_movi_f(rn(node->u.w), node->v.f);
1880 case_vvv(unord, _f);
1894 case_bvv(bunlt, _f);
1896 case_bvv(bunle, _f);
1898 case_bvv(buneq, _f);
1900 case_bvv(bunge, _f);
1902 case_bvv(bungt, _f);
1904 case_bvv(bltgt, _f);
1908 case_bvv(bunord, _f);
1936 case jit_code_movi_d:
1939 swf_movi_d(rn(node->u.w), node->v.d);
1941 vfp_movi_d(rn(node->u.w), node->v.d);
1970 case_vvv(unord, _d);
1984 case_bvv(bunlt, _d);
1986 case_bvv(bunle, _d);
1988 case_bvv(buneq, _d);
1990 case_bvv(bunge, _d);
1992 case_bvv(bungt, _d);
1994 case_bvv(bltgt, _d);
1998 case_bvv(bunord, _d);
2002 jmpr(rn(node->u.w));
2006 if (node->flag & jit_flag_node) {
2008 assert(temp->code == jit_code_label ||
2009 temp->code == jit_code_epilog);
2010 if (temp->flag & jit_flag_patch)
2013 word = _jit->code.length -
2014 (_jit->pc.uc - _jit->code.ptr);
2015 if (jit_thumb_p()) word >>= 1;
2018 value = _s24P(word);
2019 word = jmpi_p(_jit->pc.w, value);
2020 patch(word, node, value ?
2021 arm_patch_jump : arm_patch_word);
2030 case jit_code_callr:
2032 callr(rn(node->u.w));
2034 case jit_code_calli:
2035 if (node->flag & jit_flag_node) {
2038 assert(temp->code == jit_code_label ||
2039 temp->code == jit_code_epilog);
2040 if (temp->flag & jit_flag_patch)
2041 calli(temp->u.w, 0);
2043 word = _jit->code.length -
2044 (_jit->pc.uc - _jit->code.ptr);
2045 if (jit_exchange_p())
2047 if (jit_thumb_p()) word >>= 1;
2050 value = _s24P(word);
2051 word = calli_p(_jit->pc.w, value);
2052 patch(word, node, value ?
2053 arm_patch_call : arm_patch_word);
2058 calli(node->u.w, jit_exchange_p());
2061 case jit_code_prolog:
2062 _jitc->function = _jitc->functions.ptr + node->w.w;
2064 undo.word = _jit->pc.w;
2065 memcpy(&undo.func, _jitc->function, sizeof(undo.func));
2066 #if DEVEL_DISASSEMBLER
2069 undo.data = _jitc->consts.data;
2070 undo.thumb = _jitc->thumb;
2071 undo.const_offset = _jitc->consts.offset;
2072 undo.patch_offset = _jitc->patches.offset;
2074 if (_jitc->data_info.ptr)
2075 undo.info_offset = _jitc->data_info.offset;
2079 compute_framesize();
2083 case jit_code_epilog:
2084 assert(_jitc->function == _jitc->functions.ptr + node->w.w);
2086 for (temp = undo.node->next;
2087 temp != node; temp = temp->next) {
2088 if (temp->code == jit_code_label ||
2089 temp->code == jit_code_epilog)
2090 temp->flag &= ~jit_flag_patch;
2092 temp->flag &= ~jit_flag_patch;
2094 _jit->pc.w = undo.word;
2095 /* undo.func.self.aoff and undo.func.regset should not
2096 * be undone, as they will be further updated, and are
2097 * the reason of the undo. */
2098 undo.func.self.aoff = _jitc->function->frame +
2099 _jitc->function->self.aoff;
2100 undo.func.need_frame = _jitc->function->need_frame;
2101 undo.func.need_return = _jitc->function->need_return;
2102 jit_regset_set(&undo.func.regset, &_jitc->function->regset);
2103 /* allocar information also does not need to be undone */
2104 undo.func.aoffoff = _jitc->function->aoffoff;
2105 undo.func.allocar = _jitc->function->allocar;
2106 /* swf_offset and check_reg_args must also not be undone */
2107 undo.func.swf_offset = _jitc->function->swf_offset;
2108 undo.func.save_reg_args = _jitc->function->save_reg_args;
2109 memcpy(_jitc->function, &undo.func, sizeof(undo.func));
2110 #if DEVEL_DISASSEMBLER
2113 invalidate_consts();
2114 _jitc->consts.data = undo.data;
2115 _jitc->thumb = undo.thumb;
2116 _jitc->consts.offset = undo.const_offset;
2117 _jitc->patches.offset = undo.patch_offset;
2119 if (_jitc->data_info.ptr)
2120 _jitc->data_info.offset = undo.info_offset;
2123 goto restart_function;
2125 /* remember label is defined */
2126 node->flag |= jit_flag_patch;
2127 node->u.w = _jit->pc.w;
2129 _jitc->function = NULL;
2132 case jit_code_movr_w_f:
2134 swf_movr_w_f(rn(node->u.w), rn(node->v.w));
2136 vfp_movr_w_f(rn(node->u.w), rn(node->v.w));
2138 case jit_code_movr_f_w:
2140 swf_movr_f_w(rn(node->u.w), rn(node->v.w));
2142 vfp_movr_f_w(rn(node->u.w), rn(node->v.w));
2144 case jit_code_movi_f_w:
2146 movi_f_w(rn(node->u.w), node->v.f);
2148 case jit_code_movi_w_f:
2150 swf_movi_w_f(rn(node->u.w), node->v.w);
2152 vfp_movi_w_f(rn(node->u.w), node->v.w);
2154 case jit_code_movr_ww_d:
2156 swf_movr_ww_d(rn(node->u.w), rn(node->v.w), rn(node->w.w));
2158 vfp_movr_ww_d(rn(node->u.w), rn(node->v.w), rn(node->w.w));
2160 case jit_code_movr_d_ww:
2162 swf_movr_d_ww(rn(node->u.w), rn(node->v.w), rn(node->w.w));
2164 vfp_movr_d_ww(rn(node->u.w), rn(node->v.w), rn(node->w.w));
2166 case jit_code_movi_d_ww:
2167 movi_d_ww(rn(node->u.w), rn(node->v.w), node->w.d);
2169 case jit_code_movi_ww_d:
2171 swf_movi_ww_d(rn(node->u.w), node->v.w, node->w.w);
2173 vfp_movi_ww_d(rn(node->u.w), node->v.w, node->w.w);
2175 case jit_code_va_start:
2176 vastart(rn(node->u.w));
2178 case jit_code_va_arg:
2179 vaarg(rn(node->u.w), rn(node->v.w));
2181 case jit_code_va_arg_d:
2183 swf_vaarg_d(rn(node->u.w), rn(node->v.w));
2185 vfp_vaarg_d(rn(node->u.w), rn(node->v.w));
2187 case jit_code_live: case jit_code_ellipsis:
2188 case jit_code_va_push:
2189 case jit_code_allocai: case jit_code_allocar:
2190 case jit_code_arg_c: case jit_code_arg_s:
2191 case jit_code_arg_i:
2192 case jit_code_arg_f: case jit_code_arg_d:
2193 case jit_code_va_end:
2195 case jit_code_retr_c: case jit_code_reti_c:
2196 case jit_code_retr_uc: case jit_code_reti_uc:
2197 case jit_code_retr_s: case jit_code_reti_s:
2198 case jit_code_retr_us: case jit_code_reti_us:
2199 case jit_code_retr_i: case jit_code_reti_i:
2200 case jit_code_retr_f: case jit_code_reti_f:
2201 case jit_code_retr_d: case jit_code_reti_d:
2202 case jit_code_getarg_c: case jit_code_getarg_uc:
2203 case jit_code_getarg_s: case jit_code_getarg_us:
2204 case jit_code_getarg_i:
2205 case jit_code_getarg_f: case jit_code_getarg_d:
2206 case jit_code_putargr_c: case jit_code_putargi_c:
2207 case jit_code_putargr_uc: case jit_code_putargi_uc:
2208 case jit_code_putargr_s: case jit_code_putargi_s:
2209 case jit_code_putargr_us: case jit_code_putargi_us:
2210 case jit_code_putargr_i: case jit_code_putargi_i:
2211 case jit_code_putargr_f: case jit_code_putargi_f:
2212 case jit_code_putargr_d: case jit_code_putargi_d:
2213 case jit_code_pushargr_c: case jit_code_pushargi_c:
2214 case jit_code_pushargr_uc: case jit_code_pushargi_uc:
2215 case jit_code_pushargr_s: case jit_code_pushargi_s:
2216 case jit_code_pushargr_us: case jit_code_pushargi_us:
2217 case jit_code_pushargr_i: case jit_code_pushargi_i:
2218 case jit_code_pushargr_f: case jit_code_pushargi_f:
2219 case jit_code_pushargr_d: case jit_code_pushargi_d:
2220 case jit_code_retval_c: case jit_code_retval_uc:
2221 case jit_code_retval_s: case jit_code_retval_us:
2222 case jit_code_retval_i:
2223 case jit_code_retval_f: case jit_code_retval_d:
2224 case jit_code_prepare:
2225 case jit_code_finishr: case jit_code_finishi:
2226 case jit_code_negi_f: case jit_code_absi_f:
2227 case jit_code_sqrti_f: case jit_code_negi_d:
2228 case jit_code_absi_d: case jit_code_sqrti_d:
2231 negi(rn(node->u.w), node->v.w);
2234 comi(rn(node->u.w), node->v.w);
2236 case jit_code_exti_c:
2237 exti_c(rn(node->u.w), node->v.w);
2239 case jit_code_exti_uc:
2240 exti_uc(rn(node->u.w), node->v.w);
2242 case jit_code_exti_s:
2243 exti_s(rn(node->u.w), node->v.w);
2245 case jit_code_exti_us:
2246 exti_us(rn(node->u.w), node->v.w);
2248 case jit_code_bswapi_us:
2249 bswapi_us(rn(node->u.w), node->v.w);
2251 case jit_code_bswapi_ui:
2252 bswapi_ui(rn(node->u.w), node->v.w);
2254 case jit_code_htoni_us:
2255 htoni_us(rn(node->u.w), node->v.w);
2257 case jit_code_htoni_ui:
2258 htoni_ui(rn(node->u.w), node->v.w);
2261 cloi(rn(node->u.w), node->v.w);
2264 clzi(rn(node->u.w), node->v.w);
2267 ctoi(rn(node->u.w), node->v.w);
2270 ctzi(rn(node->u.w), node->v.w);
2272 case jit_code_rbiti:
2273 rbiti(rn(node->u.w), node->v.w);
2275 case jit_code_popcnti:
2276 popcnti(rn(node->u.w), node->v.w);
2279 exti(rn(node->u.w), node->v.w, node->w.q.l, node->w.q.h);
2281 case jit_code_exti_u:
2282 exti_u(rn(node->u.w), node->v.w, node->w.q.l, node->w.q.h);
2287 jit_regarg_clr(node, value);
2288 assert(_jitc->regarg == 0 && _jitc->synth == 0);
2289 /* update register live state */
2292 #if defined JIT_INSTR_MAX
2293 word = 4096 - JIT_INSTR_MAX;
2297 /* longest sequence should be 64 bytes, but preventively
2298 * do not let it go past 256 remaining bytes before a flush */
2301 if (_jitc->consts.length &&
2302 (_jit->pc.uc - _jitc->consts.data >= word ||
2303 (jit_uword_t)_jit->pc.uc -
2304 (jit_uword_t)_jitc->consts.patches[0] >= word)) {
2306 node->next->code != jit_code_jmpi &&
2307 node->next->code != jit_code_jmpr &&
2308 node->next->code != jit_code_epilog) {
2309 /* insert a jump, flush constants and continue */
2311 assert(!jit_thumb_p());
2314 patch_at(arm_patch_jump, word, _jit->pc.w);
2339 for (offset = 0; offset < _jitc->patches.offset; offset++) {
2340 assert(_jitc->patches.ptr[offset].kind & arm_patch_node);
2341 node = _jitc->patches.ptr[offset].node;
2342 word = _jitc->patches.ptr[offset].inst;
2343 if (!jit_thumb_p() &&
2344 (node->code == jit_code_movi ||
2345 (node->code == jit_code_calli &&
2346 (_jitc->patches.ptr[offset].kind & ~arm_patch_node) ==
2348 /* calculate where to patch word */
2349 value = *(jit_int32_t *)word;
2350 assert((value & 0x0f700000) == ARM_LDRI);
2351 /* offset may become negative (-4) if last instruction
2352 * before unconditional branch and data following
2353 * FIXME can this cause issues in the preprocessor prefetch
2354 * or something else? should not, as the constants are after
2355 * an unconditional jump */
2356 if (value & ARM_P) value = value & 0x00000fff;
2357 else value = -(value & 0x00000fff);
2358 word = word + 8 + value;
2360 value = node->code == jit_code_movi ? node->v.n->u.w : node->u.n->u.w;
2361 patch_at(_jitc->patches.ptr[offset].kind & ~arm_patch_node, word, value);
2364 jit_flush(_jit->code.ptr, _jit->pc.uc);
2366 return (_jit->code.ptr);
2370 # include "jit_rewind.c"
2371 # include "jit_arm-cpu.c"
2372 # include "jit_arm-swf.c"
2373 # include "jit_arm-vfp.c"
2374 # include "jit_fallback.c"
2378 jit_flush(void *fptr, void *tptr)
2380 #if defined(__GNUC__)
2381 jit_uword_t i, f, t, s;
2383 s = sysconf(_SC_PAGE_SIZE);
2384 f = (jit_uword_t)fptr & -s;
2385 t = (((jit_uword_t)tptr) + s - 1) & -s;
2386 for (i = f; i < t; i += s)
2387 __clear_cache((void *)i, (void *)(i + s));
2392 _emit_ldxi(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
2394 ldxi_i(rn(r0), rn(r1), i0);
2398 _emit_stxi(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_int32_t r1)
2400 stxi_i(i0, rn(r0), rn(r1));
2404 _emit_ldxi_d(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
2407 swf_ldxi_d(rn(r0), rn(r1), i0);
2409 vfp_ldxi_d(rn(r0), rn(r1), i0);
2413 _emit_stxi_d(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_int32_t r1)
2416 swf_stxi_d(i0, rn(r0), rn(r1));
2418 vfp_stxi_d(i0, rn(r0), rn(r1));
2422 _jit_get_reg_pair(jit_state_t *_jit)
2424 /* bypass jit_get_reg() with argument or'ed with jit_class_chk
2425 * and try to find an consecutive, even free register pair, or
2426 * return JIT_NOREG if fail, as the cost of spills is greater
2427 * than splitting a double load/store in two operations. */
2428 if (jit_reg_free_p(_R0) && jit_reg_free_p(_R1)) {
2429 jit_regset_setbit(&_jitc->regarg, _R0);
2430 jit_regset_setbit(&_jitc->regarg, _R1);
2433 if (jit_reg_free_p(_R2) && jit_reg_free_p(_R3)) {
2434 jit_regset_setbit(&_jitc->regarg, _R2);
2435 jit_regset_setbit(&_jitc->regarg, _R3);
2438 if (jit_reg_free_p(_R4) && jit_reg_free_p(_R5)) {
2439 jit_regset_setbit(&_jitc->regarg, _R4);
2440 jit_regset_setbit(&_jitc->regarg, _R5);
2443 if (jit_reg_free_p(_R6) && jit_reg_free_p(_R7)) {
2444 jit_regset_setbit(&_jitc->regarg, _R6);
2445 jit_regset_setbit(&_jitc->regarg, _R7);
2448 if (jit_reg_free_p(_R8) && jit_reg_free_p(_R9)) {
2449 jit_regset_setbit(&_jitc->regarg, _R8);
2450 jit_regset_setbit(&_jitc->regarg, _R9);
2457 _jit_unget_reg_pair(jit_state_t *_jit, jit_int32_t reg)
2461 case _R0: jit_unget_reg(_R1); break;
2462 case _R2: jit_unget_reg(_R3); break;
2463 case _R4: jit_unget_reg(_R5); break;
2464 case _R6: jit_unget_reg(_R7); break;
2465 case _R8: jit_unget_reg(_R9); break;
2470 /* A prolog must be aligned at mod 4 bytes boundary.
2471 * This condition was not being required to be tested by
2472 * accident previously, but with the jit_frame and jit_tramp
2473 * code it is required */
2475 _must_align_p(jit_state_t *_jit, jit_node_t *node)
2477 if (jit_thumb_p() && (_jit->pc.w & 3)) {
2478 for (; node; node = node->next) {
2479 switch (node->code) {
2482 case jit_code_label:
2484 case jit_code_prolog:
2495 _load_const(jit_state_t *_jit, jit_bool_t uniq, jit_int32_t r0, jit_word_t i0)
2504 assert(!jit_thumb_p());
2506 /* use zero, a valid directly encoded immediate, to avoid the
2507 * need of a bitmask to know what offsets will be patched, so
2508 * that comparison will always fail for constants that cannot
2512 /* Actually, code is (currently at least) not self modifying,
2513 * so, any value reachable backwards is valid as a constant. */
2515 /* FIXME a quickly updateable/mutable hash table could be
2516 * better here, but most times only a few comparisons
2520 /* search in previous constant pool */
2521 if ((data = (jit_int32_t *)_jitc->consts.data)) {
2522 w = (jit_word_t)data;
2523 /* maximum backwards offset */
2524 base = (_jit->pc.w + 8) - 4092;
2526 /* can scan all possible available backward constants */
2529 base = (base - w) >> 2;
2530 size = _jitc->consts.size >> 2;
2531 for (offset = size - 1; offset >= base; offset--) {
2532 if (data[offset] == i0) {
2533 w = (jit_word_t)(data + offset);
2534 d = (_jit->pc.w + 8) - w;
2535 LDRIN(r0, _R15_REGNO, d);
2544 _jitc->consts.patches[_jitc->consts.offset++] = _jit->pc.w;
2545 /* (probably) positive forward offset */
2546 LDRI(r0, _R15_REGNO, 0);
2549 /* search already requested values */
2550 for (offset = 0; offset < _jitc->consts.length; offset++) {
2551 if (_jitc->consts.values[offset] == i0) {
2552 _jitc->consts.patches[_jitc->consts.offset++] = offset;
2559 /* cannot run out of space because of limited range
2560 * but assert anyway to catch logic errors */
2561 assert(_jitc->consts.length < 1024);
2562 assert(_jitc->consts.offset < 2048);
2564 _jitc->consts.patches[_jitc->consts.offset++] = _jitc->consts.length;
2565 _jitc->consts.values[_jitc->consts.length++] = i0;
2569 _flush_consts(jit_state_t *_jit)
2574 /* if no forward constants */
2575 if (!_jitc->consts.length)
2577 assert(!jit_thumb_p());
2579 _jitc->consts.data = _jit->pc.uc;
2580 _jitc->consts.size = _jitc->consts.length << 2;
2581 /* FIXME check will not overrun, otherwise, need to reallocate
2582 * code buffer and start over */
2583 jit_memcpy(_jitc->consts.data, _jitc->consts.values, _jitc->consts.size);
2584 _jit->pc.w += _jitc->consts.size;
2587 if (_jitc->data_info.ptr) {
2588 if (_jitc->data_info.offset >= _jitc->data_info.length) {
2589 jit_realloc((jit_pointer_t *)&_jitc->data_info.ptr,
2590 _jitc->data_info.length * sizeof(jit_data_info_t),
2591 (_jitc->data_info.length + 1024) *
2592 sizeof(jit_data_info_t));
2593 _jitc->data_info.length += 1024;
2595 _jitc->data_info.ptr[_jitc->data_info.offset].code = word;
2596 _jitc->data_info.ptr[_jitc->data_info.offset].length = _jitc->consts.size;
2597 ++_jitc->data_info.offset;
2601 for (offset = 0; offset < _jitc->consts.offset; offset += 2)
2602 patch_at(arm_patch_load, _jitc->consts.patches[offset],
2603 word + (_jitc->consts.patches[offset + 1] << 2));
2604 _jitc->consts.length = _jitc->consts.offset = 0;
2607 /* to be called if needing to start over a function */
2609 _invalidate_consts(jit_state_t *_jit)
2611 /* if no forward constants */
2612 if (_jitc->consts.length)
2613 _jitc->consts.length = _jitc->consts.offset = 0;
2617 _compute_framesize(jit_state_t *_jit)
2620 _jitc->framesize = sizeof(jit_word_t) * 2; /* lr+fp */
2621 for (reg = 0; reg < jit_size(iregs); reg++)
2622 if (jit_regset_tstbit(&_jitc->function->regset, iregs[reg]))
2623 _jitc->framesize += sizeof(jit_word_t);
2625 if (_jitc->function->save_reg_args)
2626 _jitc->framesize += 16;
2628 /* Make sure functions called have a 8 byte aligned stack */
2629 _jitc->framesize = (_jitc->framesize + 7) & -8;
2633 _patch(jit_state_t *_jit, jit_word_t instr, jit_node_t *node, jit_int32_t kind)
2637 assert(node->flag & jit_flag_node);
2638 if (node->code == jit_code_movi)
2639 flag = node->v.n->flag;
2641 flag = node->u.n->flag;
2642 assert(!(flag & jit_flag_patch));
2643 kind |= arm_patch_node;
2644 if (_jitc->patches.offset >= _jitc->patches.length) {
2645 jit_realloc((jit_pointer_t *)&_jitc->patches.ptr,
2646 _jitc->patches.length * sizeof(jit_patch_t),
2647 (_jitc->patches.length + 1024) * sizeof(jit_patch_t));
2648 _jitc->patches.length += 1024;
2650 _jitc->patches.ptr[_jitc->patches.offset].kind = kind;
2651 _jitc->patches.ptr[_jitc->patches.offset].inst = instr;
2652 _jitc->patches.ptr[_jitc->patches.offset].node = node;
2653 ++_jitc->patches.offset;