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 #include <lightning.h>
21 #include <lightning/jit_private.h>
23 # include <sys/mman.h>
30 # define MAP_ANON MAP_ANONYMOUS
31 # ifndef MAP_ANONYMOUS
32 # define MAP_ANONYMOUS 0
36 #define jit_regload_reload 0 /* convert to reload */
37 #define jit_regload_delete 1 /* just remove node */
38 #define jit_regload_isdead 2 /* delete and unset live bit */
43 static jit_word_t hash_data(const void*, jit_word_t);
45 #define new_pool() _new_pool(_jit)
46 static void _new_pool(jit_state_t*);
48 #define new_node(u) _new_node(_jit, u)
49 static jit_node_t *_new_node(jit_state_t*, jit_code_t);
51 #define link_node(u) _link_node(_jit, u)
52 static inline jit_node_t *_link_node(jit_state_t*, jit_node_t*);
54 #define del_node(u, v) _del_node(_jit, u, v)
55 static inline void _del_node(jit_state_t*, jit_node_t*, jit_node_t*);
57 #define free_node(u) _free_node(_jit, u)
58 static inline void _free_node(jit_state_t*, jit_node_t*);
60 #define del_label(u, v) _del_label(_jit, u, v)
61 static void _del_label(jit_state_t*, jit_node_t*, jit_node_t*);
63 #define jit_dataset() _jit_dataset(_jit)
65 _jit_dataset(jit_state_t *_jit);
67 #define block_update_set(block, target) _block_update_set(_jit, block, target)
68 static jit_bool_t _block_update_set(jit_state_t*, jit_block_t*, jit_block_t*);
70 #define propagate_backward(block) _propagate_backward(_jit, block)
71 static void _propagate_backward(jit_state_t*, jit_block_t*);
73 #define check_block_again() _check_block_again(_jit)
74 static jit_bool_t _check_block_again(jit_state_t*);
76 #define do_setup() _do_setup(_jit)
77 static void _do_setup(jit_state_t*);
79 #define jit_setup(block) _jit_setup(_jit, block)
81 _jit_setup(jit_state_t *_jit, jit_block_t *block);
83 #define do_follow(always) _do_follow(_jit, always)
84 static void _do_follow(jit_state_t*, jit_bool_t);
86 #define jit_follow(block) _jit_follow(_jit, block)
88 _jit_follow(jit_state_t *_jit, jit_block_t *block);
90 #define jit_update(node, live, mask) _jit_update(_jit, node, live, mask)
92 _jit_update(jit_state_t *_jit, jit_node_t *node,
93 jit_regset_t *live, jit_regset_t *mask);
95 #define thread_jumps() _thread_jumps(_jit)
97 _thread_jumps(jit_state_t *_jit);
99 #define sequential_labels() _sequential_labels(_jit)
101 _sequential_labels(jit_state_t *_jit);
103 #define split_branches() _split_branches(_jit)
105 _split_branches(jit_state_t *_jit);
107 #define shortcut_jump(prev, node) _shortcut_jump(_jit, prev, node)
109 _shortcut_jump(jit_state_t *_jit, jit_node_t *prev, jit_node_t *node);
111 #define redundant_jump(prev, node) _redundant_jump(_jit, prev, node)
113 _redundant_jump(jit_state_t *_jit, jit_node_t *prev, jit_node_t *node);
116 reverse_jump_code(jit_code_t code);
118 #define reverse_jump(prev, node) _reverse_jump(_jit, prev, node)
120 _reverse_jump(jit_state_t *_jit, jit_node_t *prev, jit_node_t *node);
122 #define redundant_store(node, jump) _redundant_store(_jit, node, jump)
124 _redundant_store(jit_state_t *_jit, jit_node_t *node, jit_bool_t jump);
126 #define simplify_movr(p, n, k, s) _simplify_movr(_jit, p, n, k, s)
128 _simplify_movr(jit_state_t *_jit, jit_node_t *prev, jit_node_t *node,
129 jit_int32_t kind, jit_int32_t size);
131 #define simplify_movi(p, n, k, s) _simplify_movi(_jit, p, n, k, s)
133 _simplify_movi(jit_state_t *_jit, jit_node_t *prev, jit_node_t *node,
134 jit_int32_t kind, jit_int32_t size);
136 #define simplify_ldxi(prev, node) _simplify_ldxi(_jit, prev, node)
138 _simplify_ldxi(jit_state_t *_jit, jit_node_t *prev, jit_node_t *node);
140 #define simplify_stxi(prev, node) _simplify_stxi(_jit, prev, node)
142 _simplify_stxi(jit_state_t *_jit, jit_node_t *prev, jit_node_t *node);
144 #define simplify_spill(node, regno) _simplify_spill(_jit, node, regno)
146 _simplify_spill(jit_state_t *_jit, jit_node_t *node, jit_int32_t regno);
148 #define simplify() _simplify(_jit)
150 _simplify(jit_state_t *_jit);
152 #define jit_reg_undef -1
153 #define jit_reg_static 0
154 #define jit_reg_change 1
155 #define register_change_p(n, l, r) _register_change_p(_jit, n, l, r)
157 _register_change_p(jit_state_t *_jit, jit_node_t *node, jit_node_t *link,
160 #define spill_reglive_p(node, regno) _spill_reglive_p(_jit, node, regno)
162 _spill_reglive_p(jit_state_t *_jit, jit_node_t *node, jit_int32_t regno);
164 #define patch_registers() _patch_registers(_jit)
166 _patch_registers(jit_state_t *_jit);
168 #define patch_register(n,l,r,p) _patch_register(_jit,n,l,r,p)
170 _patch_register(jit_state_t *jit, jit_node_t *node, jit_node_t *link,
171 jit_int32_t regno, jit_int32_t patch);
184 init_jit(const char *progname)
187 jit_init_debug(progname);
199 _jit_get_reg(jit_state_t *_jit, jit_int32_t regspec)
204 spec = regspec & ~(jit_class_chk|jit_class_nospill);
205 if (spec & jit_class_named) {
206 regno = jit_regno(spec);
207 if (jit_regset_tstbit(&_jitc->regsav, regno))
208 /* fail if register is spilled */
210 if (jit_regset_tstbit(&_jitc->regarg, regno))
211 /* fail if register is an argument to current instruction */
213 if (jit_regset_tstbit(&_jitc->reglive, regno)) {
214 if (regspec & jit_class_nospill)
215 /* fail if register is live and should not spill/reload */
219 jit_regset_setbit(&_jitc->regarg, regno);
223 assert(jit_class(spec) != 0);
226 /* search for a free register matching spec */
227 for (regno = 0; regno < _jitc->reglen; regno++) {
228 if ((jit_class(_rvs[regno].spec) & spec) == spec &&
229 !jit_regset_tstbit(&_jitc->regarg, regno) &&
230 !jit_regset_tstbit(&_jitc->reglive, regno)) {
231 if (jit_regset_tstbit(&_jitc->regmask, regno)) {
232 /* search further, attempting to find a truly known
233 * free register, not just one in unknown state. */
236 for (regfree = regno + 1;
237 regfree < _jitc->reglen; regfree++) {
238 if ((jit_class(_rvs[regfree].spec) & spec) == spec &&
239 !jit_regset_tstbit(&_jitc->regarg, regfree) &&
240 !jit_regset_tstbit(&_jitc->reglive, regfree) &&
241 !jit_regset_tstbit(&_jitc->regmask, regfree)) {
251 /* search for a register matching spec that is not an argument
252 * for the current instruction */
253 for (regno = 0; regno < _jitc->reglen; regno++) {
254 if ((jit_class(_rvs[regno].spec) & spec) == spec &&
255 !jit_regset_tstbit(&_jitc->regsav, regno) &&
256 !jit_regset_tstbit(&_jitc->regarg, regno) &&
257 !(regspec & jit_class_nospill)) {
259 assert(_jitc->function != NULL);
260 if (spec & jit_class_gpr) {
261 if (!_jitc->function->regoff[regno]) {
262 _jitc->function->regoff[regno] =
263 jit_allocai(sizeof(jit_word_t));
267 /* emit_stxi must not need temporary registers */
268 assert(!_jitc->getreg);
271 emit_stxi(_jitc->function->regoff[regno], JIT_FP, regno);
277 if (!_jitc->function->regoff[regno]) {
278 _jitc->function->regoff[regno] =
279 jit_allocai(sizeof(jit_float64_t));
283 /* emit_stxi must not need temporary registers */
284 assert(!_jitc->getreg);
287 emit_stxi_d(_jitc->function->regoff[regno], JIT_FP, regno);
292 jit_regset_setbit(&_jitc->regsav, regno);
294 jit_regset_setbit(&_jitc->regarg, regno);
295 if (jit_class(_rvs[regno].spec) & jit_class_sav) {
296 /* if will modify callee save registers without a
297 * function prolog, better patch this assertion */
298 assert(_jitc->function != NULL);
299 if (!jit_regset_tstbit(&_jitc->function->regset, regno)) {
300 jit_regset_setbit(&_jitc->function->regset, regno);
309 /* nospill hint only valid during emit" */
310 assert(!(regspec & jit_class_nospill));
311 for (regno = 0; regno < _jitc->reglen; regno++) {
312 if ((jit_class(_rvs[regno].spec) & spec) == spec &&
313 !jit_regset_tstbit(&_jitc->regsav, regno) &&
314 !jit_regset_tstbit(&_jitc->regarg, regno)) {
315 jit_regset_setbit(&_jitc->regarg, regno);
316 jit_regset_setbit(&_jitc->regsav, regno);
318 return (jit_regno_patch|regno);
323 /* Out of hardware registers */
325 assert(regspec & jit_class_chk);
330 _jit_unget_reg(jit_state_t *_jit, jit_int32_t regno)
332 regno = jit_regno(regno);
333 if (jit_regset_tstbit(&_jitc->regsav, regno)) {
336 /* emit_ldxi must not need a temporary register */
337 assert(!_jitc->getreg);
340 if (jit_class(_rvs[regno].spec) & jit_class_gpr)
341 emit_ldxi(regno, JIT_FP, _jitc->function->regoff[regno]);
343 emit_ldxi_d(regno, JIT_FP, _jitc->function->regoff[regno]);
345 /* emit_ldxi must not need a temporary register */
351 jit_regset_clrbit(&_jitc->regsav, regno);
353 #if defined(jit_carry)
354 assert((regno == jit_carry /*&& _NOREG != jit_carry*/) ||
355 jit_regset_tstbit(&_jitc->regarg, regno) != 0);
357 assert(jit_regset_tstbit(&_jitc->regarg, regno) != 0);
359 jit_regset_clrbit(&_jitc->regarg, regno);
363 _jit_callee_save_p(jit_state_t *_jit, jit_int32_t regno)
365 assert(regno >= 0 && regno < JIT_NOREG);
366 return (!!(_rvs[regno].spec & jit_class_sav));
370 _jit_pointer_p(jit_state_t *_jit, jit_pointer_t address)
372 return ((jit_uint8_t *)address >= _jit->code.ptr &&
373 (jit_word_t)address < _jit->pc.w);
378 jit_regset_com(jit_regset_t *u, jit_regset_t *v)
380 u->rl = ~v->rl; u->rh = ~v->rh;
381 u->fl = ~v->fl; u->fh = ~v->fh;
385 jit_regset_and(jit_regset_t *u, jit_regset_t *v, jit_regset_t *w)
387 u->rl = v->rl & w->rl; u->rh = v->rh & w->rh;
388 u->fl = v->fl & w->fl; u->fh = v->fh & w->fh;
392 jit_regset_ior(jit_regset_t *u, jit_regset_t *v, jit_regset_t *w)
394 u->rl = v->rl | w->rl; u->rh = v->rh | w->rh;
395 u->fl = v->fl | w->fl; u->fh = v->fh | w->fh;
399 jit_regset_xor(jit_regset_t *u, jit_regset_t *v, jit_regset_t *w)
401 u->rl = v->rl ^ w->rl; u->rh = v->rh ^ w->rh;
402 u->fl = v->fl ^ w->fl; u->fh = v->fh ^ w->fh;
406 jit_regset_set(jit_regset_t *u, jit_regset_t *v)
408 u->rl = v->rl; u->rh = v->rh;
409 u->fl = v->fl; u->fh = v->fh;
413 jit_regset_set_mask(jit_regset_t *u, jit_int32_t v)
415 jit_bool_t w = !!(v & (v - 1));
417 assert(v >= 0 && v <= 256);
419 u->rl = u->rh = u->fl = u->fh = -1LL;
421 u->rl = w ? (1LL << v) - 1 : -1LL;
422 u->rh = u->fl = u->fh = 0;
426 u->rh = w ? (1LL << (v - 64)) - 1 : -1LL;
430 u->rl = u->rh = -1LL;
431 u->fl = w ? (1LL << (v - 128)) - 1 : -1LL;
435 u->rl = u->rh = u->fl = -1LL;
436 u->fh = w ? (1LL << (v - 128)) - 1 : -1LL;
441 jit_regset_cmp_ui(jit_regset_t *u, jit_word_t v)
443 return !((u->rl == v && u->rh == 0 && u->fl == 0 && u->fh == 0));
447 jit_regset_set_ui(jit_regset_t *u, jit_word_t v)
450 u->rh = u->fl = u->fh = 0;
454 jit_regset_set_p(jit_regset_t *u)
456 return (u->rl || u->rh || u->fl || u->fh);
460 jit_regset_clrbit(jit_regset_t *set, jit_int32_t bit)
462 assert(bit >= 0 && bit <= 255);
464 set->rl &= ~(1LL << bit);
466 set->rh &= ~(1LL << (bit - 64));
468 set->fl &= ~(1LL << (bit - 128));
470 set->fh &= ~(1LL << (bit - 192));
474 jit_regset_setbit(jit_regset_t *set, jit_int32_t bit)
476 assert(bit >= 0 && bit <= 255);
478 set->rl |= 1LL << bit;
480 set->rh |= 1LL << (bit - 64);
482 set->fl |= 1LL << (bit - 128);
484 set->fh |= 1LL << (bit - 192);
488 jit_regset_tstbit(jit_regset_t *set, jit_int32_t bit)
490 assert(bit >= 0 && bit <= 255);
492 return (!!(set->rl & (1LL << bit)));
494 return (!!(set->rh & (1LL << (bit - 64))));
496 return (!!(set->fl & (1LL << (bit - 128))));
497 return (!!(set->fh & (1LL << (bit - 192))));
501 jit_regset_scan1(jit_regset_t *set, jit_int32_t offset)
503 assert(offset >= 0 && offset <= 255);
504 for (; offset < 64; offset++) {
505 if (set->rl & (1LL << offset))
508 for (; offset < 128; offset++) {
509 if (set->rh & (1LL << (offset - 64)))
512 for (; offset < 192; offset++) {
513 if (set->fl & (1LL << (offset - 128)))
516 for (; offset < 256; offset++) {
517 if (set->fh & (1LL << (offset - 192)))
523 #elif __sparc__ && __WORDSIZE == 64
525 jit_regset_com(jit_regset_t *u, jit_regset_t *v)
527 u->rl = ~v->rl; u->rh = ~v->rh;
531 jit_regset_and(jit_regset_t *u, jit_regset_t *v, jit_regset_t *w)
533 u->rl = v->rl & w->rl; u->rh = v->rh & w->rh;
537 jit_regset_ior(jit_regset_t *u, jit_regset_t *v, jit_regset_t *w)
539 u->rl = v->rl | w->rl; u->rh = v->rh | w->rh;
543 jit_regset_xor(jit_regset_t *u, jit_regset_t *v, jit_regset_t *w)
545 u->rl = v->rl ^ w->rl; u->rh = v->rh ^ w->rh;
549 jit_regset_set(jit_regset_t *u, jit_regset_t *v)
551 u->rl = v->rl; u->rh = v->rh;
555 jit_regset_set_mask(jit_regset_t *u, jit_int32_t v)
557 jit_bool_t w = !!(v & (v - 1));
559 assert(v >= 0 && v <= 128);
561 u->rl = u->rh = -1LL;
563 u->rl = w ? (1LL << v) - 1 : -1LL;
568 u->rh = w ? (1LL << (v - 64)) - 1 : -1LL;
573 jit_regset_cmp_ui(jit_regset_t *u, jit_word_t v)
575 return !((u->rl == v && u->rh == 0));
579 jit_regset_set_ui(jit_regset_t *u, jit_word_t v)
586 jit_regset_set_p(jit_regset_t *u)
588 return (u->rl || u->rh);
592 jit_regset_clrbit(jit_regset_t *set, jit_int32_t bit)
594 assert(bit >= 0 && bit <= 128);
596 set->rl &= ~(1LL << bit);
598 set->rh &= ~(1LL << (bit - 64));
602 jit_regset_setbit(jit_regset_t *set, jit_int32_t bit)
604 assert(bit >= 0 && bit <= 127);
606 set->rl |= 1LL << bit;
608 set->rh |= 1LL << (bit - 64);
612 jit_regset_tstbit(jit_regset_t *set, jit_int32_t bit)
614 assert(bit >= 0 && bit <= 127);
616 return (!!(set->rl & (1LL << bit)));
618 return (!!(set->rh & (1LL << (bit - 64))));
622 jit_regset_scan1(jit_regset_t *set, jit_int32_t offset)
624 assert(offset >= 0 && offset <= 127);
625 for (; offset < 64; offset++) {
626 if (set->rl & (1LL << offset))
629 for (; offset < 128; offset++) {
630 if (set->rh & (1LL << (offset - 64)))
638 jit_regset_scan1(jit_regset_t *set, jit_int32_t offset)
641 assert(offset >= 0 && offset <= 63);
642 if ((mask = *set >> offset)) {
655 _jit_save(jit_state_t *_jit, jit_int32_t reg)
657 reg = jit_regno(reg);
658 assert(!_jitc->realize);
659 _jitc->spill[reg] = jit_new_node_w(jit_code_save, reg);
663 _jit_load(jit_state_t *_jit, jit_int32_t reg)
667 reg = jit_regno(reg);
668 assert(!_jitc->realize);
669 assert(_jitc->spill[reg] != NULL);
670 node = jit_new_node_w(jit_code_load, reg);
671 /* create a path to flag the save/load is not required */
672 node->link = _jitc->spill[reg];
673 node->link->link = node;
674 _jitc->spill[reg] = NULL;
678 hash_data(const void *data, jit_word_t length)
680 const jit_uint8_t *ptr;
682 for (i = key = 0, ptr = data; i < length; i++)
683 key = (key << (key & 1)) ^ ptr[i];
688 _jit_address(jit_state_t *_jit, jit_node_t *node)
691 assert(node != NULL &&
692 /* If a node type that is documented to be a fixed marker */
693 (node->code == jit_code_note || node->code == jit_code_name ||
694 /* If another special fixed marker, returned by jit_indirect() */
695 (node->code == jit_code_label && (node->flag & jit_flag_use) != 0)));
696 return ((jit_pointer_t)node->u.w);
700 _jit_data(jit_state_t *_jit, const void *data,
701 jit_word_t length, jit_int32_t align)
706 assert(!_jitc->realize);
708 /* Ensure there is space even if asking for a duplicate */
709 if (((_jitc->data.offset + 7) & -8) + length > _jit->data.length) {
712 size = (_jit->data.length + length + 4096) & - 4095;
713 assert(size >= _jit->data.length);
714 if (_jitc->data.ptr == NULL)
715 jit_alloc((jit_pointer_t *)&_jitc->data.ptr, size);
717 jit_realloc((jit_pointer_t *)&_jitc->data.ptr,
718 _jit->data.length, size);
719 _jit->data.length = size;
721 if (_jitc->data.table == NULL)
722 jit_alloc((jit_pointer_t *)&_jitc->data.table,
723 (_jitc->data.size = 16) * sizeof(jit_node_t*));
725 key = hash_data(data, length) & (_jitc->data.size - 1);
726 node = _jitc->data.table[key];
727 for (; node; node = node->next) {
728 if (node->v.w == length &&
729 memcmp(_jitc->data.ptr + node->u.w, data, length) == 0)
734 node = jit_new_node_no_link(jit_code_data);
741 _jitc->data.offset = (_jitc->data.offset + 1) & -2;
744 _jitc->data.offset = (_jitc->data.offset + 3) & -4;
747 _jitc->data.offset = (_jitc->data.offset + 7) & -8;
750 node->u.w = _jitc->data.offset;
752 jit_memcpy(_jitc->data.ptr + _jitc->data.offset, data, length);
753 _jitc->data.offset += length;
755 node->next = _jitc->data.table[key];
756 _jitc->data.table[key] = node;
759 /* Rehash if more than 75% used table */
760 if (_jitc->data.count >
761 (_jitc->data.size >> 1) + (_jitc->data.size >> 2) &&
762 (_jitc->data.size << 1) > _jitc->data.size) {
768 jit_alloc((jit_pointer_t *)&hash,
769 (_jitc->data.size << 1) * sizeof(jit_node_t*));
770 for (i = 0; i < _jitc->data.size; i++) {
771 temp = _jitc->data.table[i];
772 for (; temp; temp = next) {
774 key = hash_data(_jitc->data.ptr + temp->u.w, temp->v.w) &
775 ((_jitc->data.size << 1) - 1);
776 temp->next = hash[key];
780 jit_free((jit_pointer_t *)&_jitc->data.table);
781 _jitc->data.table = hash;
782 _jitc->data.size <<= 1;
790 _new_pool(jit_state_t *_jit)
795 if (_jitc->pool.offset >= _jitc->pool.length) {
798 length = _jitc->pool.length + 16;
799 jit_realloc((jit_pointer_t *)&_jitc->pool.ptr,
800 _jitc->pool.length * sizeof(jit_node_t *),
801 length * sizeof(jit_node_t *));
802 _jitc->pool.length = length;
804 jit_alloc((jit_pointer_t *)(_jitc->pool.ptr + _jitc->pool.offset),
805 sizeof(jit_node_t) * 1024);
806 list = _jitc->pool.ptr[_jitc->pool.offset];
807 for (offset = 1; offset < 1024; offset++, list++)
808 list->next = list + 1;
809 list->next = _jitc->list;
810 _jitc->list = _jitc->pool.ptr[_jitc->pool.offset];
811 ++_jitc->pool.offset;
815 _new_node(jit_state_t *_jit, jit_code_t code)
819 if (_jitc->list == NULL)
822 _jitc->list = node->next;
824 node->flag |= jit_flag_synth;
831 static inline jit_node_t *
832 _link_node(jit_state_t *_jit, jit_node_t *node)
835 _jitc->tail->next = node;
838 return (_jitc->tail = node);
842 _del_node(jit_state_t *_jit, jit_node_t *prev, jit_node_t *node)
845 assert(prev == _jitc->head);
846 _jitc->head = node->next;
849 prev->next = node->next;
850 memset(node, 0, sizeof(jit_node_t));
851 node->next = _jitc->list;
856 _free_node(jit_state_t *_jit, jit_node_t *node)
858 memset(node, 0, sizeof(jit_node_t));
859 node->next = _jitc->list;
864 _del_label(jit_state_t *_jit, jit_node_t *prev, jit_node_t *node)
868 /* only allow call to del_label on linked labels */
869 block = _jitc->blocks.ptr + node->v.w;
870 assert(block->label == node);
872 /* del_label() should only be called when optimizing.
873 * This will leave an empty block index */
874 jit_regset_del(&block->reglive);
875 jit_regset_del(&block->regmask);
878 /* redundant, should be already true */
879 assert(node->link == NULL);
880 del_node(prev, node);
888 jit_alloc((jit_pointer_t *)&_jit, sizeof(jit_state_t));
889 jit_alloc((jit_pointer_t *)&_jitc, sizeof(jit_compiler_t));
890 jit_regset_new(&_jitc->regarg);
891 jit_regset_new(&_jitc->regsav);
892 jit_regset_new(&_jitc->reglive);
893 jit_regset_new(&_jitc->regmask);
894 jit_regset_new(&_jitc->explive);
898 jit_alloc((jit_pointer_t *)&_jitc->spill,
899 _jitc->reglen * sizeof(jit_node_t*));
900 jit_alloc((jit_pointer_t *)&_jitc->gen,
901 _jitc->reglen * sizeof(jit_int32_t));
902 jit_alloc((jit_pointer_t *)&_jitc->values,
903 _jitc->reglen * sizeof(jit_value_t));
905 jit_alloc((jit_pointer_t *)&_jitc->patches.ptr,
906 (_jitc->patches.length = 1024) * sizeof(jit_patch_t));
907 jit_alloc((jit_pointer_t *)&_jitc->functions.ptr,
908 (_jitc->functions.length = 16) * sizeof(jit_function_t));
909 jit_alloc((jit_pointer_t *)&_jitc->pool.ptr,
910 (_jitc->pool.length = 16) * sizeof(jit_node_t*));
911 jit_alloc((jit_pointer_t *)&_jitc->blocks.ptr,
912 (_jitc->blocks.length = 16) * sizeof(jit_block_t));
913 #if __arm__ && DISASSEMBLER
914 jit_alloc((jit_pointer_t *)&_jitc->data_info.ptr,
915 (_jitc->data_info.length = 1024) * sizeof(jit_data_info_t));
918 /* allocate at most one extra note in case jit_name() is
919 * never called, or called after adding at least one note */
920 _jit->note.length = 1;
921 _jitc->note.size = sizeof(jit_note_t);
927 _jit_clear_state(jit_state_t *_jit)
929 #if DEVEL_DISASSEMBLER
930 # define jit_really_clear_state() _jit_really_clear_state(_jit)
933 void _jit_really_clear_state(jit_state_t *_jit)
937 jit_function_t *function;
939 /* release memory not required at jit execution time and set
940 * pointers to NULL to explicitly know they are released */
941 _jitc->head = _jitc->tail = NULL;
943 jit_free((jit_pointer_t *)&_jitc->data.table);
944 _jitc->data.size = _jitc->data.count = 0;
946 jit_free((jit_pointer_t *)&_jitc->spill);
947 jit_free((jit_pointer_t *)&_jitc->gen);
948 jit_free((jit_pointer_t *)&_jitc->values);
950 jit_free((jit_pointer_t *)&_jitc->blocks.ptr);
952 jit_free((jit_pointer_t *)&_jitc->patches.ptr);
953 _jitc->patches.offset = _jitc->patches.length = 0;
955 for (offset = 0; offset < _jitc->functions.offset; offset++) {
956 function = _jitc->functions.ptr + offset;
957 jit_free((jit_pointer_t *)&function->regoff);
959 jit_free((jit_pointer_t *)&_jitc->functions.ptr);
960 _jitc->functions.offset = _jitc->functions.length = 0;
961 _jitc->function = NULL;
963 for (offset = 0; offset < _jitc->pool.offset; offset++)
964 jit_free((jit_pointer_t *)(_jitc->pool.ptr + offset));
965 jit_free((jit_pointer_t *)&_jitc->pool.ptr);
966 _jitc->pool.offset = _jitc->pool.length = 0;
969 _jitc->note.head = _jitc->note.tail =
970 _jitc->note.name = _jitc->note.note = NULL;
971 _jitc->note.base = NULL;
973 #if __arm__ && DISASSEMBLER
974 jit_free((jit_pointer_t *)&_jitc->data_info.ptr);
977 #if (__powerpc__ && _CALL_AIXDESC) || __ia64__
978 jit_free((jit_pointer_t *)&_jitc->prolog.ptr);
982 jit_regset_del(&_jitc->regs);
985 jit_free((jit_pointer_t *)&_jitc);
989 _jit_destroy_state(jit_state_t *_jit)
991 #if DEVEL_DISASSEMBLER
992 jit_really_clear_state();
995 if (!_jit->user_code)
996 munmap(_jit->code.ptr, _jit->code.length);
997 if (!_jit->user_data)
998 munmap(_jit->data.ptr, _jit->data.length);
1000 jit_free((jit_pointer_t *)&_jit);
1004 _jit_synth_inc(jit_state_t *_jit)
1006 assert(_jitc->synth < 8);
1011 _jit_new_node(jit_state_t *_jit, jit_code_t code)
1013 assert(!_jitc->realize);
1014 return (link_node(new_node(code)));
1018 _jit_new_node_no_link(jit_state_t *_jit, jit_code_t code)
1020 assert(!_jitc->realize);
1021 return (new_node(code));
1025 _jit_link_node(jit_state_t *_jit, jit_node_t *node)
1027 assert(!_jitc->realize);
1032 _jit_synth_dec(jit_state_t *_jit)
1034 assert(_jitc->synth > 0);
1039 _jit_new_node_w(jit_state_t *_jit, jit_code_t code,
1042 jit_node_t *node = new_node(code);
1043 assert(!_jitc->realize);
1045 return (link_node(node));
1049 _jit_new_node_f(jit_state_t *_jit, jit_code_t code,
1052 jit_node_t *node = new_node(code);
1053 assert(!_jitc->realize);
1055 return (link_node(node));
1059 _jit_new_node_d(jit_state_t *_jit, jit_code_t code,
1062 jit_node_t *node = new_node(code);
1063 assert(!_jitc->realize);
1065 return (link_node(node));
1069 _jit_new_node_p(jit_state_t *_jit, jit_code_t code,
1072 jit_node_t *node = new_node(code);
1073 assert(!_jitc->realize);
1075 return (link_node(node));
1079 _jit_new_node_ww(jit_state_t *_jit, jit_code_t code,
1080 jit_word_t u, jit_word_t v)
1082 jit_node_t *node = new_node(code);
1083 assert(!_jitc->realize);
1086 return (link_node(node));
1090 _jit_new_node_wp(jit_state_t *_jit, jit_code_t code,
1091 jit_word_t u, jit_pointer_t v)
1093 return (jit_new_node_ww(code, u, (jit_word_t)v));
1097 _jit_new_node_fp(jit_state_t *_jit, jit_code_t code,
1098 jit_float32_t u, jit_pointer_t v)
1100 jit_node_t *node = new_node(code);
1101 assert(!_jitc->realize);
1103 node->v.w = (jit_word_t)v;
1104 return (link_node(node));
1108 _jit_new_node_dp(jit_state_t *_jit, jit_code_t code,
1109 jit_float64_t u, jit_pointer_t v)
1111 jit_node_t *node = new_node(code);
1112 assert(!_jitc->realize);
1114 node->v.w = (jit_word_t)v;
1115 return (link_node(node));
1119 _jit_new_node_pw(jit_state_t *_jit, jit_code_t code,
1120 jit_pointer_t u, jit_word_t v)
1122 return (jit_new_node_ww(code, (jit_word_t)u, v));
1126 _jit_new_node_wf(jit_state_t *_jit, jit_code_t code,
1127 jit_word_t u, jit_float32_t v)
1129 jit_node_t *node = new_node(code);
1130 assert(!_jitc->realize);
1133 return (link_node(node));
1137 _jit_new_node_wd(jit_state_t *_jit, jit_code_t code,
1138 jit_word_t u, jit_float64_t v)
1140 jit_node_t *node = new_node(code);
1141 assert(!_jitc->realize);
1144 return (link_node(node));
1148 _jit_new_node_www(jit_state_t *_jit, jit_code_t code,
1149 jit_word_t u, jit_word_t v, jit_word_t w)
1151 jit_node_t *node = new_node(code);
1152 assert(!_jitc->realize);
1156 return (link_node(node));
1160 _jit_new_node_qww(jit_state_t *_jit, jit_code_t code,
1161 jit_int32_t l, jit_int32_t h,
1162 jit_word_t v, jit_word_t w)
1164 jit_node_t *node = new_node(code);
1165 assert(!_jitc->realize);
1171 return (link_node(node));
1175 _jit_new_node_wqw(jit_state_t *_jit, jit_code_t code,
1176 jit_word_t u, jit_int32_t l,
1177 jit_int32_t h, jit_word_t w)
1179 jit_node_t *node = new_node(code);
1180 assert(!_jitc->realize);
1186 return (link_node(node));
1190 _jit_new_node_wwq(jit_state_t *_jit, jit_code_t code,
1191 jit_word_t u, jit_word_t v,
1192 jit_int32_t l, jit_int32_t h)
1194 jit_node_t *node = new_node(code);
1195 assert(!_jitc->realize);
1200 return (link_node(node));
1204 _jit_new_node_wwf(jit_state_t *_jit, jit_code_t code,
1205 jit_word_t u, jit_word_t v, jit_float32_t w)
1207 jit_node_t *node = new_node(code);
1208 assert(!_jitc->realize);
1212 return (link_node(node));
1216 _jit_new_node_wqf(jit_state_t *_jit, jit_code_t code,
1217 jit_word_t u, jit_int32_t l,
1218 jit_int32_t h, jit_float32_t w)
1220 jit_node_t *node = new_node(code);
1221 assert(!_jitc->realize);
1227 return (link_node(node));
1231 _jit_new_node_wwd(jit_state_t *_jit, jit_code_t code,
1232 jit_word_t u, jit_word_t v, jit_float64_t w)
1234 jit_node_t *node = new_node(code);
1235 assert(!_jitc->realize);
1239 return (link_node(node));
1243 _jit_new_node_wqd(jit_state_t *_jit, jit_code_t code,
1244 jit_word_t u, jit_int32_t l,
1245 jit_int32_t h, jit_float64_t w)
1247 jit_node_t *node = new_node(code);
1248 assert(!_jitc->realize);
1254 return (link_node(node));
1258 _jit_new_node_pww(jit_state_t *_jit, jit_code_t code,
1259 jit_pointer_t u, jit_word_t v, jit_word_t w)
1261 jit_node_t *node = new_node(code);
1262 assert(!_jitc->realize);
1266 return (link_node(node));
1270 _jit_new_node_pwf(jit_state_t *_jit, jit_code_t code,
1271 jit_pointer_t u, jit_word_t v, jit_float32_t w)
1273 jit_node_t *node = new_node(code);
1274 assert(!_jitc->realize);
1278 return (link_node(node));
1282 _jit_new_node_pwd(jit_state_t *_jit, jit_code_t code,
1283 jit_pointer_t u, jit_word_t v, jit_float64_t w)
1285 jit_node_t *node = new_node(code);
1286 assert(!_jitc->realize);
1290 return (link_node(node));
1294 _jit_label(jit_state_t *_jit)
1298 if (!(node = _jitc->tail) || node->code != jit_code_label) {
1299 node = jit_forward();
1307 _jit_forward(jit_state_t *_jit)
1309 return (jit_new_node_no_link(jit_code_label));
1313 _jit_indirect(jit_state_t *_jit)
1318 node->flag |= jit_flag_use;
1324 _jit_link(jit_state_t *_jit, jit_node_t *node)
1328 assert((node->code == jit_code_label ||
1329 node->code == jit_code_prolog ||
1330 node->code == jit_code_epilog) && !node->next);
1331 jit_link_node(node);
1332 if (_jitc->blocks.offset >= _jitc->blocks.length) {
1335 length = _jitc->blocks.length + 16;
1336 jit_realloc((jit_pointer_t *)&_jitc->blocks.ptr,
1337 _jitc->blocks.length * sizeof(jit_block_t),
1338 length * sizeof(jit_block_t));
1339 _jitc->blocks.length = length;
1341 block = _jitc->blocks.ptr + _jitc->blocks.offset;
1342 block->label = node;
1343 node->v.w = _jitc->blocks.offset;
1344 jit_regset_new(&block->reglive);
1345 jit_regset_new(&block->regmask);
1346 ++_jitc->blocks.offset;
1350 _jit_forward_p(jit_state_t *_jit, jit_node_t *node)
1352 return (node->code == jit_code_label && !node->next && node != _jitc->tail);
1356 _jit_indirect_p(jit_state_t *_jit, jit_node_t *node)
1358 return (node->code == jit_code_label && !!(node->flag & jit_flag_use));
1362 _jit_target_p(jit_state_t *_jit, jit_node_t *node)
1364 return (node->code == jit_code_label && !!node->link);
1368 _jit_prepare(jit_state_t *_jit)
1370 assert(_jitc->function != NULL);
1371 _jitc->function->call.call = jit_call_default;
1372 _jitc->function->call.argi =
1373 _jitc->function->call.argf =
1374 _jitc->function->call.size = 0;
1375 _jitc->prepare = jit_new_node(jit_code_prepare);
1379 _jit_patch(jit_state_t* _jit, jit_node_t *instr)
1383 if (!(label = _jitc->tail) || label->code != jit_code_label)
1384 label = jit_label();
1385 jit_patch_at(instr, label);
1389 _jit_classify(jit_state_t *_jit, jit_code_t code)
1394 case jit_code_data: case jit_code_save: case jit_code_load:
1395 case jit_code_name: case jit_code_label: case jit_code_note:
1396 case jit_code_prolog: case jit_code_ellipsis: case jit_code_va_push:
1397 case jit_code_epilog: case jit_code_ret: case jit_code_prepare:
1400 case jit_code_live: case jit_code_va_end:
1401 case jit_code_retr_c: case jit_code_retr_uc:
1402 case jit_code_retr_s: case jit_code_retr_us:
1403 case jit_code_retr_i: case jit_code_retr_ui:
1404 case jit_code_retr_l:
1405 case jit_code_retr_f: case jit_code_retr_d:
1406 case jit_code_pushargr_c:
1407 case jit_code_pushargr_uc:
1408 case jit_code_pushargr_s:
1409 case jit_code_pushargr_us:
1410 case jit_code_pushargr_i:
1411 case jit_code_pushargr_ui:
1412 case jit_code_pushargr_l:
1413 case jit_code_pushargr_f:
1414 case jit_code_pushargr_d:
1415 case jit_code_finishr: /* synthesized will set jit_cc_a0_jmp */
1416 mask = jit_cc_a0_reg;
1418 case jit_code_align: case jit_code_skip:
1419 case jit_code_reti_c: case jit_code_reti_uc:
1420 case jit_code_reti_s: case jit_code_reti_us:
1421 case jit_code_reti_i: case jit_code_reti_ui:
1422 case jit_code_reti_l:
1423 case jit_code_pushargi_c:
1424 case jit_code_pushargi_uc:
1425 case jit_code_pushargi_s:
1426 case jit_code_pushargi_us:
1427 case jit_code_pushargi_i:
1428 case jit_code_pushargi_ui:
1429 case jit_code_pushargi_l:
1430 case jit_code_finishi: /* synthesized will set jit_cc_a0_jmp */
1431 mask = jit_cc_a0_int;
1433 case jit_code_reti_f: case jit_code_pushargi_f:
1434 mask = jit_cc_a0_flt;
1436 case jit_code_reti_d: case jit_code_pushargi_d:
1437 mask = jit_cc_a0_dbl;
1439 case jit_code_allocai:
1440 mask = jit_cc_a0_int|jit_cc_a1_int;
1442 case jit_code_arg_c: case jit_code_arg_s:
1443 case jit_code_arg_i: case jit_code_arg_l:
1444 case jit_code_arg_f: case jit_code_arg_d:
1445 mask = jit_cc_a0_int|jit_cc_a0_arg;
1447 case jit_code_calli: case jit_code_jmpi:
1448 mask = jit_cc_a0_jmp;
1450 case jit_code_callr: case jit_code_jmpr:
1451 mask = jit_cc_a0_reg|jit_cc_a0_jmp;
1453 case jit_code_retval_c: case jit_code_retval_uc:
1454 case jit_code_retval_s: case jit_code_retval_us:
1455 case jit_code_retval_i: case jit_code_retval_ui:
1456 case jit_code_retval_l:
1457 case jit_code_retval_f: case jit_code_retval_d:
1458 case jit_code_va_start:
1459 mask = jit_cc_a0_reg|jit_cc_a0_chg;
1461 case jit_code_getarg_c: case jit_code_getarg_uc:
1462 case jit_code_getarg_s: case jit_code_getarg_us:
1463 case jit_code_getarg_i: case jit_code_getarg_ui:
1464 case jit_code_getarg_l:
1465 case jit_code_getarg_f: case jit_code_getarg_d:
1466 mask = jit_cc_a0_reg|jit_cc_a0_chg|jit_cc_a1_arg;
1468 case jit_code_putargr_c:case jit_code_putargr_uc:
1469 case jit_code_putargr_s:case jit_code_putargr_us:
1470 case jit_code_putargr_i:case jit_code_putargr_ui:
1471 case jit_code_putargr_l:
1472 case jit_code_putargr_f:case jit_code_putargr_d:
1473 mask = jit_cc_a0_reg|jit_cc_a1_arg;
1475 case jit_code_putargi_c:case jit_code_putargi_uc:
1476 case jit_code_putargi_s:case jit_code_putargi_us:
1477 case jit_code_putargi_i:case jit_code_putargi_ui:
1478 case jit_code_putargi_l:
1479 mask = jit_cc_a0_int|jit_cc_a1_arg;
1481 case jit_code_putargi_f:
1482 mask = jit_cc_a0_flt|jit_cc_a1_arg;
1484 case jit_code_putargi_d:
1485 mask = jit_cc_a0_dbl|jit_cc_a1_arg;
1487 case jit_code_movi: case jit_code_ldi_c: case jit_code_ldi_uc:
1488 case jit_code_ldi_s: case jit_code_ldi_us: case jit_code_ldi_i:
1489 case jit_code_ldi_ui: case jit_code_ldi_l: case jit_code_ldi_f:
1490 case jit_code_ldi_d:
1491 case jit_code_negi: case jit_code_comi:
1492 case jit_code_exti_c: case jit_code_exti_uc:
1493 case jit_code_exti_s: case jit_code_exti_us:
1494 case jit_code_exti_i: case jit_code_exti_ui:
1495 case jit_code_bswapi_us:case jit_code_bswapi_ui:
1496 case jit_code_bswapi_ul:
1497 case jit_code_htoni_us: case jit_code_htoni_ui:
1498 case jit_code_htoni_ul:
1499 case jit_code_cloi: case jit_code_clzi:
1500 case jit_code_ctoi: case jit_code_ctzi:
1501 case jit_code_rbiti: case jit_code_popcnti:
1502 case jit_code_movi_w_f: case jit_code_movi_w_d:
1503 mask = jit_cc_a0_reg|jit_cc_a0_chg|jit_cc_a1_int;
1505 case jit_code_unldi: case jit_code_unldi_u: case jit_code_unldi_x:
1506 mask = jit_cc_a0_reg|jit_cc_a0_chg|jit_cc_a1_int|jit_cc_a2_int;
1508 case jit_code_movi_ww_d:
1509 mask = jit_cc_a0_reg|jit_cc_a0_chg|jit_cc_a1_int|jit_cc_a2_int;
1511 case jit_code_movi_f: case jit_code_movi_f_w:
1512 case jit_code_negi_f: case jit_code_absi_f:
1513 case jit_code_sqrti_f:
1514 mask = jit_cc_a0_reg|jit_cc_a0_chg|jit_cc_a1_flt;
1516 case jit_code_movi_d: case jit_code_movi_d_w:
1517 case jit_code_negi_d: case jit_code_absi_d:
1518 case jit_code_sqrti_d:
1519 mask = jit_cc_a0_reg|jit_cc_a0_chg|jit_cc_a1_dbl;
1521 case jit_code_movi_d_ww:
1522 mask = jit_cc_a0_reg|jit_cc_a0_chg|jit_cc_a1_reg|jit_cc_a1_chg|
1525 case jit_code_negr: case jit_code_comr: case jit_code_movr:
1526 case jit_code_extr_c: case jit_code_extr_uc: case jit_code_extr_s:
1527 case jit_code_extr_us: case jit_code_extr_i: case jit_code_extr_ui:
1528 case jit_code_truncr_f_i: case jit_code_truncr_f_l:
1529 case jit_code_truncr_d_i: case jit_code_truncr_d_l:
1530 case jit_code_htonr_us: case jit_code_htonr_ui: case jit_code_htonr_ul:
1531 case jit_code_bswapr_us: case jit_code_bswapr_ui: case jit_code_bswapr_ul:
1532 case jit_code_ldr_c: case jit_code_ldr_uc:
1533 case jit_code_ldr_s: case jit_code_ldr_us: case jit_code_ldr_i:
1534 case jit_code_ldr_ui: case jit_code_ldr_l: case jit_code_negr_f:
1535 case jit_code_absr_f: case jit_code_sqrtr_f: case jit_code_movr_f:
1536 case jit_code_extr_f: case jit_code_extr_d_f: case jit_code_ldr_f:
1537 case jit_code_negr_d: case jit_code_absr_d: case jit_code_sqrtr_d:
1538 case jit_code_movr_d: case jit_code_extr_d: case jit_code_extr_f_d:
1539 case jit_code_ldr_d: case jit_code_rbitr: case jit_code_popcntr:
1540 case jit_code_clor: case jit_code_clzr:
1541 case jit_code_ctor: case jit_code_ctzr:
1542 case jit_code_movr_w_f: case jit_code_movr_f_w:
1543 case jit_code_movr_w_d: case jit_code_movr_d_w:
1544 case jit_code_va_arg: case jit_code_va_arg_d:
1545 mask = jit_cc_a0_reg|jit_cc_a0_chg|jit_cc_a1_reg;
1547 case jit_code_movr_d_ww:
1548 mask = jit_cc_a0_reg|jit_cc_a0_chg|jit_cc_a1_reg|jit_cc_a1_chg|
1551 case jit_code_addi: case jit_code_addxi: case jit_code_addci:
1552 case jit_code_subi: case jit_code_subxi: case jit_code_subci:
1554 case jit_code_muli: case jit_code_hmuli: case jit_code_hmuli_u:
1555 case jit_code_divi: case jit_code_divi_u:
1556 case jit_code_remi: case jit_code_remi_u: case jit_code_andi:
1557 case jit_code_ori: case jit_code_xori: case jit_code_lshi:
1558 case jit_code_rshi: case jit_code_rshi_u: case jit_code_lroti:
1559 case jit_code_rroti: case jit_code_lti:
1560 case jit_code_lti_u: case jit_code_lei: case jit_code_lei_u:
1561 case jit_code_eqi: case jit_code_gei: case jit_code_gei_u:
1562 case jit_code_gti: case jit_code_gti_u: case jit_code_nei:
1563 case jit_code_ldxi_c: case jit_code_ldxi_uc: case jit_code_ldxi_s:
1564 case jit_code_ldxi_us: case jit_code_ldxi_i: case jit_code_ldxi_ui:
1565 case jit_code_ldxi_l: case jit_code_ldxi_f: case jit_code_ldxi_d:
1566 case jit_code_unldr: case jit_code_unldr_u: case jit_code_unldr_x:
1567 mask = jit_cc_a0_reg|jit_cc_a0_chg|jit_cc_a1_reg|jit_cc_a2_int;
1569 case jit_code_qmuli: case jit_code_qmuli_u:
1570 case jit_code_qdivi: case jit_code_qdivi_u:
1571 case jit_code_qlshi: case jit_code_qlshi_u:
1572 case jit_code_qrshi: case jit_code_qrshi_u:
1573 mask = jit_cc_a0_reg|jit_cc_a0_rlh|jit_cc_a0_chg|
1574 jit_cc_a1_reg|jit_cc_a2_int;
1576 case jit_code_addi_f: case jit_code_subi_f: case jit_code_rsbi_f:
1577 case jit_code_muli_f: case jit_code_divi_f: case jit_code_lti_f:
1578 case jit_code_lei_f: case jit_code_eqi_f: case jit_code_gei_f:
1579 case jit_code_gti_f: case jit_code_nei_f: case jit_code_unlti_f:
1580 case jit_code_unlei_f: case jit_code_uneqi_f: case jit_code_ungei_f:
1581 case jit_code_ungti_f: case jit_code_ltgti_f: case jit_code_ordi_f:
1582 case jit_code_unordi_f:
1583 mask = jit_cc_a0_reg|jit_cc_a0_chg|jit_cc_a1_reg|jit_cc_a2_flt;
1585 case jit_code_addi_d: case jit_code_subi_d: case jit_code_rsbi_d:
1586 case jit_code_muli_d: case jit_code_divi_d: case jit_code_lti_d:
1587 case jit_code_lei_d: case jit_code_eqi_d: case jit_code_gei_d:
1588 case jit_code_gti_d: case jit_code_nei_d: case jit_code_unlti_d:
1589 case jit_code_unlei_d: case jit_code_uneqi_d: case jit_code_ungei_d:
1590 case jit_code_ungti_d: case jit_code_ltgti_d: case jit_code_ordi_d:
1591 case jit_code_unordi_d:
1592 mask = jit_cc_a0_reg|jit_cc_a0_chg|jit_cc_a1_reg|jit_cc_a2_dbl;
1594 case jit_code_addr: case jit_code_addxr: case jit_code_addcr:
1595 case jit_code_subr: case jit_code_subxr: case jit_code_subcr:
1596 case jit_code_mulr: case jit_code_hmulr: case jit_code_hmulr_u:
1597 case jit_code_divr: case jit_code_divr_u:
1598 case jit_code_remr: case jit_code_remr_u: case jit_code_andr:
1599 case jit_code_orr: case jit_code_xorr: case jit_code_lshr:
1600 case jit_code_rshr: case jit_code_rshr_u: case jit_code_lrotr:
1601 case jit_code_rrotr: case jit_code_ltr:
1602 case jit_code_ltr_u: case jit_code_ler: case jit_code_ler_u:
1603 case jit_code_eqr: case jit_code_ger: case jit_code_ger_u:
1604 case jit_code_gtr: case jit_code_gtr_u: case jit_code_ner:
1605 case jit_code_ldxr_c: case jit_code_ldxr_uc: case jit_code_ldxr_s:
1606 case jit_code_ldxr_us: case jit_code_ldxr_i: case jit_code_ldxr_ui:
1607 case jit_code_ldxr_l: case jit_code_addr_f: case jit_code_subr_f:
1608 case jit_code_mulr_f: case jit_code_divr_f: case jit_code_ltr_f:
1609 case jit_code_ler_f: case jit_code_eqr_f: case jit_code_ger_f:
1610 case jit_code_gtr_f: case jit_code_ner_f: case jit_code_unltr_f:
1611 case jit_code_unler_f: case jit_code_uneqr_f: case jit_code_unger_f:
1612 case jit_code_ungtr_f: case jit_code_ltgtr_f: case jit_code_ordr_f:
1613 case jit_code_unordr_f: case jit_code_ldxr_f: case jit_code_addr_d:
1614 case jit_code_subr_d: case jit_code_mulr_d: case jit_code_divr_d:
1615 case jit_code_ltr_d: case jit_code_ler_d: case jit_code_eqr_d:
1616 case jit_code_ger_d: case jit_code_gtr_d: case jit_code_ner_d:
1617 case jit_code_unltr_d: case jit_code_unler_d: case jit_code_uneqr_d:
1618 case jit_code_unger_d: case jit_code_ungtr_d: case jit_code_ltgtr_d:
1619 case jit_code_ordr_d: case jit_code_unordr_d: case jit_code_ldxr_d:
1620 case jit_code_movr_ww_d:
1621 mask = jit_cc_a0_reg|jit_cc_a0_chg|jit_cc_a1_reg|jit_cc_a2_reg;
1623 case jit_code_qmulr: case jit_code_qmulr_u:
1624 case jit_code_qdivr: case jit_code_qdivr_u:
1625 case jit_code_qlshr: case jit_code_qlshr_u:
1626 case jit_code_qrshr: case jit_code_qrshr_u:
1627 mask = jit_cc_a0_reg|jit_cc_a0_rlh|jit_cc_a0_chg|
1628 jit_cc_a1_reg|jit_cc_a2_reg;
1630 case jit_code_sti_c: case jit_code_sti_s: case jit_code_sti_i:
1631 case jit_code_sti_l: case jit_code_sti_f: case jit_code_sti_d:
1632 mask = jit_cc_a0_int|jit_cc_a1_reg;
1634 case jit_code_unsti: case jit_code_unsti_x:
1635 mask = jit_cc_a0_int|jit_cc_a1_reg|jit_cc_a2_int;
1637 case jit_code_blti: case jit_code_blti_u: case jit_code_blei:
1638 case jit_code_blei_u: case jit_code_beqi: case jit_code_bgei:
1639 case jit_code_bgei_u: case jit_code_bgti: case jit_code_bgti_u:
1640 case jit_code_bnei: case jit_code_bmsi: case jit_code_bmci:
1641 mask = jit_cc_a0_jmp|jit_cc_a1_reg|jit_cc_a2_int;
1643 case jit_code_blti_f: case jit_code_blei_f: case jit_code_beqi_f:
1644 case jit_code_bgei_f: case jit_code_bgti_f: case jit_code_bnei_f:
1645 case jit_code_bunlti_f: case jit_code_bunlei_f: case jit_code_buneqi_f:
1646 case jit_code_bungei_f: case jit_code_bungti_f: case jit_code_bltgti_f:
1647 case jit_code_bordi_f: case jit_code_bunordi_f:
1648 mask = jit_cc_a0_jmp|jit_cc_a1_reg|jit_cc_a2_flt;
1650 case jit_code_blti_d: case jit_code_blei_d: case jit_code_beqi_d:
1651 case jit_code_bgei_d: case jit_code_bgti_d: case jit_code_bnei_d:
1652 case jit_code_bunlti_d: case jit_code_bunlei_d: case jit_code_buneqi_d:
1653 case jit_code_bungei_d: case jit_code_bungti_d: case jit_code_bltgti_d:
1654 case jit_code_bordi_d: case jit_code_bunordi_d:
1655 mask = jit_cc_a0_jmp|jit_cc_a1_reg|jit_cc_a2_dbl;
1657 case jit_code_allocar: /* synthesized instructions make it
1658 * equivalent to jit_cc_a0_chg */
1659 case jit_code_str_c: case jit_code_str_s: case jit_code_str_i:
1660 case jit_code_str_l: case jit_code_str_f: case jit_code_str_d:
1661 mask = jit_cc_a0_reg|jit_cc_a1_reg;
1663 case jit_code_unstr: case jit_code_unstr_x:
1664 mask = jit_cc_a0_reg|jit_cc_a1_reg|jit_cc_a2_int;
1666 case jit_code_stxi_c: case jit_code_stxi_s: case jit_code_stxi_i:
1667 case jit_code_stxi_l: case jit_code_stxi_f: case jit_code_stxi_d:
1668 mask = jit_cc_a0_int|jit_cc_a1_reg|jit_cc_a2_reg;
1670 case jit_code_bltr: case jit_code_bltr_u: case jit_code_bler:
1671 case jit_code_bler_u: case jit_code_beqr: case jit_code_bger:
1672 case jit_code_bger_u: case jit_code_bgtr: case jit_code_bgtr_u:
1673 case jit_code_bner: case jit_code_bmsr: case jit_code_bmcr:
1674 case jit_code_bltr_f: case jit_code_bler_f: case jit_code_beqr_f:
1675 case jit_code_bger_f: case jit_code_bgtr_f: case jit_code_bner_f:
1676 case jit_code_bunltr_f: case jit_code_bunler_f: case jit_code_buneqr_f:
1677 case jit_code_bunger_f: case jit_code_bungtr_f: case jit_code_bltgtr_f:
1678 case jit_code_bordr_f: case jit_code_bunordr_f:case jit_code_bltr_d:
1679 case jit_code_bler_d: case jit_code_beqr_d: case jit_code_bger_d:
1680 case jit_code_bgtr_d: case jit_code_bner_d: case jit_code_bunltr_d:
1681 case jit_code_bunler_d: case jit_code_buneqr_d: case jit_code_bunger_d:
1682 case jit_code_bungtr_d: case jit_code_bltgtr_d: case jit_code_bordr_d:
1683 case jit_code_bunordr_d:
1684 mask = jit_cc_a0_jmp|jit_cc_a1_reg|jit_cc_a2_reg;
1686 case jit_code_boaddi: case jit_code_boaddi_u: case jit_code_bxaddi:
1687 case jit_code_bxaddi_u: case jit_code_bosubi: case jit_code_bosubi_u:
1688 case jit_code_bxsubi: case jit_code_bxsubi_u:
1689 mask = jit_cc_a0_jmp|jit_cc_a1_reg|jit_cc_a1_chg|jit_cc_a2_int;
1691 case jit_code_stxr_c: case jit_code_stxr_s: case jit_code_stxr_i:
1692 case jit_code_stxr_l: case jit_code_stxr_f: case jit_code_stxr_d:
1693 mask = jit_cc_a0_reg|jit_cc_a1_reg|jit_cc_a2_reg;
1695 case jit_code_boaddr: case jit_code_boaddr_u: case jit_code_bxaddr:
1696 case jit_code_bxaddr_u: case jit_code_bosubr: case jit_code_bosubr_u:
1697 case jit_code_bxsubr: case jit_code_bxsubr_u:
1698 mask = jit_cc_a0_jmp|jit_cc_a1_reg|jit_cc_a1_chg|jit_cc_a2_reg;
1700 case jit_code_movnr: case jit_code_movzr:
1701 mask = jit_cc_a0_reg|jit_cc_a0_cnd|jit_cc_a1_reg|jit_cc_a2_reg;
1704 mask = jit_cc_a0_reg|jit_cc_a0_chg|jit_cc_a1_reg|
1705 jit_cc_a2_reg|jit_cc_a2_rlh;
1708 mask = jit_cc_a0_reg|jit_cc_a0_chg|jit_cc_a1_int|
1709 jit_cc_a2_reg|jit_cc_a2_rlh;
1711 case jit_code_extr: case jit_code_extr_u: case jit_code_depr:
1712 mask = jit_cc_a0_reg|jit_cc_a0_chg|jit_cc_a1_reg|jit_cc_a2_rlh;
1714 case jit_code_exti: case jit_code_exti_u: case jit_code_depi:
1715 mask = jit_cc_a0_reg|jit_cc_a0_chg|jit_cc_a1_int|jit_cc_a2_rlh;
1717 case jit_code_fmar_f: case jit_code_fmar_d:
1718 case jit_code_fmsr_f: case jit_code_fmsr_d:
1719 case jit_code_fnmar_f: case jit_code_fnmar_d:
1720 case jit_code_fnmsr_f: case jit_code_fnmsr_d:
1721 mask = jit_cc_a0_reg|jit_cc_a0_chg|
1722 jit_cc_a1_reg|jit_cc_a1_rlh|jit_cc_a2_reg;
1724 case jit_code_fmai_f: case jit_code_fmsi_f:
1725 case jit_code_fnmai_f: case jit_code_fnmsi_f:
1726 mask = jit_cc_a0_reg|jit_cc_a0_chg|
1727 jit_cc_a1_reg|jit_cc_a1_rlh|jit_cc_a2_flt;
1729 case jit_code_fmai_d: case jit_code_fmsi_d:
1730 case jit_code_fnmai_d: case jit_code_fnmsi_d:
1731 mask = jit_cc_a0_reg|jit_cc_a0_chg|
1732 jit_cc_a1_reg|jit_cc_a1_rlh|jit_cc_a2_dbl;
1742 _jit_patch_abs(jit_state_t *_jit, jit_node_t *instr, jit_pointer_t address)
1746 switch (instr->code) {
1747 case jit_code_movi: case jit_code_ldi_c: case jit_code_ldi_uc:
1748 case jit_code_ldi_s: case jit_code_ldi_us: case jit_code_ldi_i:
1749 case jit_code_ldi_ui: case jit_code_ldi_l: case jit_code_ldi_f:
1750 case jit_code_ldi_d:
1751 instr->v.p = address;
1753 case jit_code_sti_c: case jit_code_sti_s: case jit_code_sti_i:
1754 case jit_code_sti_l: case jit_code_sti_f: case jit_code_sti_d:
1755 instr->u.p = address;
1758 mask = jit_classify(instr->code);
1759 assert((mask & (jit_cc_a0_reg|jit_cc_a0_jmp)) == jit_cc_a0_jmp);
1760 instr->u.p = address;
1765 _jit_patch_at(jit_state_t *_jit, jit_node_t *instr, jit_node_t *label)
1769 assert(!(instr->flag & jit_flag_node));
1770 instr->flag |= jit_flag_node;
1771 switch (instr->code) {
1773 assert(label->code == jit_code_label ||
1774 label->code == jit_code_data);
1776 if (label->code == jit_code_data)
1777 instr->flag |= jit_flag_data;
1780 assert(label->code == jit_code_label ||
1781 label->code == jit_code_epilog);
1785 mask = jit_classify(instr->code);
1786 assert((mask & (jit_cc_a0_reg|jit_cc_a0_jmp)) == jit_cc_a0_jmp);
1787 assert(label->code == jit_code_label);
1791 /* link field is used as list of nodes associated with a given label */
1792 instr->link = label->link;
1793 label->link = instr;
1797 _do_setup(jit_state_t *_jit)
1802 /* create initial mapping of live register values
1803 * at the start of a basic block */
1804 for (offset = 0; offset < _jitc->blocks.offset; offset++) {
1805 block = _jitc->blocks.ptr + offset;
1808 if (block->label->code == jit_code_epilog) {
1809 jit_regset_setbit(&block->reglive, JIT_RET);
1810 jit_regset_setbit(&block->reglive, JIT_FRET);
1811 jit_regset_com(&block->regmask, &block->reglive);
1819 _block_update_set(jit_state_t *_jit,
1820 jit_block_t *block, jit_block_t *target)
1822 jit_regset_t regmask;
1824 jit_regset_ior(®mask, &block->reglive, &target->reglive);
1825 jit_regset_and(®mask, ®mask, &block->regmask);
1826 if (jit_regset_set_p(®mask)) {
1827 jit_regset_ior(&block->reglive, &block->reglive, ®mask);
1828 jit_regset_and(®mask, &block->reglive, &block->regmask);
1829 jit_regset_com(®mask, ®mask);
1830 jit_regset_and(&block->regmask, &block->regmask, ®mask);
1838 _propagate_backward(jit_state_t *_jit, jit_block_t *block)
1843 for (offset = block->label->v.w - 1;
1844 offset >= 0; --offset) {
1845 prev = _jitc->blocks.ptr + offset;
1846 if (!block_update_set(prev, block) ||
1847 !(prev->label->flag & jit_flag_head))
1853 _check_block_again(jit_state_t *_jit)
1857 jit_node_t *node, *label;
1858 jit_block_t *block, *target;
1861 for (offset = 0; offset < _jitc->blocks.offset; offset++) {
1862 block = _jitc->blocks.ptr + offset;
1868 /* If no block changed state */
1875 for (node = _jitc->head; node; node = node->next) {
1876 /* Special jumps that match jit_cc_a0_jmp */
1877 if (node->code == jit_code_calli || node->code == jit_code_callr)
1880 /* Remember current label */
1881 if (node->code == jit_code_label ||
1882 node->code == jit_code_prolog ||
1883 node->code == jit_code_epilog) {
1885 /* If previous block does not pass through */
1886 if (!(node->flag & jit_flag_head))
1889 target = _jitc->blocks.ptr + node->v.w;
1890 if (block && target->again && block_update_set(block, target)) {
1891 propagate_backward(block);
1896 /* If not the first jmpi */
1898 /* If a jump to dynamic address or if a jump to raw address */
1899 if (!(jit_classify(node->code) & jit_cc_a0_jmp) ||
1900 !(node->flag & jit_flag_node))
1903 /* Mark predecessor needs updating due to target change */
1904 target = _jitc->blocks.ptr + label->v.w;
1905 if (target->again && block_update_set(block, target)) {
1906 propagate_backward(block);
1918 _do_follow(jit_state_t *_jit, jit_bool_t always)
1923 /* set live state of registers not referenced in a block, but
1924 * referenced in a jump target or normal flow */
1925 for (offset = 0; offset < _jitc->blocks.offset; offset++) {
1926 block = _jitc->blocks.ptr + offset;
1927 if (!block->label || block->label->code == jit_code_epilog)
1929 if (always || block->again) {
1937 _jit_optimize(jit_state_t *_jit)
1945 jit_regset_t regmask;
1948 _jitc->function = NULL;
1951 sequential_labels();
1956 jit_regset_set_ui(®mask, 0);
1957 for (offset = 0; offset < _jitc->reglen; offset++) {
1958 if ((jit_class(_rvs[offset].spec) & (jit_class_gpr|jit_class_fpr)) &&
1959 (jit_class(_rvs[offset].spec) & jit_class_sav) == jit_class_sav)
1960 jit_regset_setbit(®mask, offset);
1963 /* Figure out labels that are only reached with a jump */
1965 for (node = _jitc->head; node; node = node->next) {
1966 switch (node->code) {
1967 case jit_code_label:
1969 node->flag |= jit_flag_head;
1971 /* Block is dead code or only reachable with an
1972 * indirect jumps. In such condition, must assume
1973 * all callee save registers are live. */
1974 block = _jitc->blocks.ptr + node->v.w;
1975 jit_regset_ior(&block->reglive,
1976 &block->reglive, ®mask);
1977 /* Cleanup regmask */
1978 block_update_set(block, block);
1982 case jit_code_jmpi: case jit_code_jmpr:
1983 case jit_code_epilog:
1986 case jit_code_data: case jit_code_note:
1998 for (node = _jitc->head; node; node = node->next) {
1999 mask = jit_classify(node->code);
2000 if (mask & jit_cc_a0_reg)
2001 node->u.w &= ~jit_regno_patch;
2002 if (mask & jit_cc_a1_reg)
2003 node->v.w &= ~jit_regno_patch;
2004 if (mask & jit_cc_a2_reg)
2005 node->w.w &= ~jit_regno_patch;
2006 if (node->code == jit_code_beqi) {
2007 if (redundant_store(node, 1)) {
2008 block = _jitc->blocks.ptr + ((jit_node_t *)node->u.n)->v.w;
2013 else if (node->code == jit_code_bnei) {
2014 if (redundant_store(node, 0)) {
2015 block = _jitc->blocks.ptr + ((jit_node_t *)node->u.n)->v.w;
2023 todo = check_block_again();
2025 /* If instructions were removed or first pass did modify the entry
2026 * state of any block */
2032 /* If any block again has the entry state modified. */
2033 todo = check_block_again();
2037 for (node = _jitc->head; node; node = node->next) {
2038 mask = jit_classify(node->code);
2039 if (mask & jit_cc_a0_reg)
2040 node->u.w &= ~jit_regno_patch;
2041 if (mask & jit_cc_a1_reg)
2042 node->v.w &= ~jit_regno_patch;
2043 if (mask & jit_cc_a2_reg)
2044 node->w.w &= ~jit_regno_patch;
2045 if (node->code == jit_code_prolog)
2046 _jitc->function = _jitc->functions.ptr + node->w.w;
2047 else if(node->code == jit_code_epilog)
2048 _jitc->function = NULL;
2051 if (mask & jit_cc_a0_flt) {
2052 node->u.p = jit_data(&node->u.f, sizeof(jit_float32_t), 4);
2053 node->flag |= jit_flag_node | jit_flag_data;
2055 else if (mask & jit_cc_a0_dbl) {
2056 node->u.p = jit_data(&node->u.d, sizeof(jit_float64_t), 8);
2057 node->flag |= jit_flag_node | jit_flag_data;
2059 else if (mask & jit_cc_a1_flt) {
2060 node->v.p = jit_data(&node->v.f, sizeof(jit_float32_t), 4);
2061 node->flag |= jit_flag_node | jit_flag_data;
2063 else if (mask & jit_cc_a1_dbl) {
2064 node->v.p = jit_data(&node->v.d, sizeof(jit_float64_t), 8);
2065 node->flag |= jit_flag_node | jit_flag_data;
2067 else if (mask & jit_cc_a2_flt) {
2068 node->w.p = jit_data(&node->w.f, sizeof(jit_float32_t), 4);
2069 node->flag |= jit_flag_node | jit_flag_data;
2071 else if (mask & jit_cc_a2_dbl) {
2072 node->w.p = jit_data(&node->w.d, sizeof(jit_float64_t), 8);
2073 node->flag |= jit_flag_node | jit_flag_data;
2076 if (_jitc->function) {
2077 if ((mask & (jit_cc_a0_reg|jit_cc_a0_chg)) ==
2078 (jit_cc_a0_reg|jit_cc_a0_chg)) {
2079 if (mask & jit_cc_a0_rlh) {
2080 jit_regset_setbit(&_jitc->function->regset,
2081 jit_regno(node->u.q.l));
2082 jit_regset_setbit(&_jitc->function->regset,
2083 jit_regno(node->u.q.h));
2086 jit_regset_setbit(&_jitc->function->regset,
2087 jit_regno(node->u.w));
2089 if ((mask & (jit_cc_a1_reg|jit_cc_a1_chg)) ==
2090 (jit_cc_a1_reg|jit_cc_a1_chg))
2091 jit_regset_setbit(&_jitc->function->regset,
2092 jit_regno(node->v.w));
2093 if ((mask & (jit_cc_a2_reg|jit_cc_a2_chg)) ==
2094 (jit_cc_a2_reg|jit_cc_a2_chg))
2095 jit_regset_setbit(&_jitc->function->regset,
2096 jit_regno(node->w.w));
2103 _jit_reglive(jit_state_t *_jit, jit_node_t *node)
2109 switch (node->code) {
2110 case jit_code_label: case jit_code_prolog: case jit_code_epilog:
2111 block = _jitc->blocks.ptr + node->v.w;
2112 jit_regset_set(&_jitc->reglive, &block->reglive);
2113 jit_regset_set_ui(&_jitc->explive, 0);
2116 jit_regset_setbit(&_jitc->explive, node->u.w);
2118 case jit_code_callr:
2119 value = jit_regno(node->u.w);
2120 if (!(node->u.w & jit_regno_patch)) {
2121 jit_regset_setbit(&_jitc->reglive, value);
2123 case jit_code_calli:
2124 for (value = 0; value < _jitc->reglen; value++) {
2125 spec = jit_class(_rvs[value].spec);
2126 if ((spec & jit_class_arg) && jit_regarg_p(node, value))
2127 jit_regset_setbit(&_jitc->reglive, value);
2128 else if (!(spec & jit_class_sav))
2129 jit_regset_clrbit(&_jitc->reglive, value);
2133 value = jit_classify(node->code);
2134 if (value & jit_cc_a0_reg) {
2135 if (value & jit_cc_a0_rlh) {
2136 if (!(node->u.q.l & jit_regno_patch)) {
2137 if (value & jit_cc_a0_chg) {
2138 jit_regset_clrbit(&_jitc->reglive, node->u.q.l);
2139 jit_regset_setbit(&_jitc->regmask, node->u.q.l);
2142 jit_regset_setbit(&_jitc->reglive, node->u.q.l);
2144 if (!(node->u.q.h & jit_regno_patch)) {
2145 if (value & jit_cc_a0_chg) {
2146 jit_regset_clrbit(&_jitc->reglive, node->u.q.h);
2147 jit_regset_setbit(&_jitc->regmask, node->u.q.h);
2150 jit_regset_setbit(&_jitc->reglive, node->u.q.h);
2154 if (!(node->u.w & jit_regno_patch)) {
2155 if (value & jit_cc_a0_chg) {
2156 jit_regset_clrbit(&_jitc->reglive, node->u.w);
2157 jit_regset_setbit(&_jitc->regmask, node->u.w);
2160 jit_regset_setbit(&_jitc->reglive, node->u.w);
2164 if (value & jit_cc_a1_reg) {
2165 if (value & jit_cc_a1_rlh) {
2166 /* Assume registers are not changed */
2167 if (!(node->v.q.l & jit_regno_patch))
2168 jit_regset_setbit(&_jitc->reglive, node->v.q.l);
2169 if (!(node->v.q.h & jit_regno_patch))
2170 jit_regset_setbit(&_jitc->reglive, node->v.q.h);
2172 else if (!(node->v.w & jit_regno_patch)) {
2173 if (value & jit_cc_a1_chg) {
2174 jit_regset_clrbit(&_jitc->reglive, node->v.w);
2175 jit_regset_setbit(&_jitc->regmask, node->v.w);
2178 jit_regset_setbit(&_jitc->reglive, node->v.w);
2181 if (value & jit_cc_a2_reg) {
2182 if (value & jit_cc_a2_rlh) {
2183 /* Assume registers are not changed */
2184 if (!(node->w.q.l & jit_regno_patch))
2185 jit_regset_setbit(&_jitc->reglive, node->w.q.l);
2186 if (!(node->w.q.h & jit_regno_patch))
2187 jit_regset_setbit(&_jitc->reglive, node->w.q.h);
2190 if (!(node->w.w & jit_regno_patch)) {
2191 if (value & jit_cc_a2_chg) {
2192 jit_regset_clrbit(&_jitc->reglive, node->w.w);
2193 jit_regset_setbit(&_jitc->regmask, node->w.w);
2196 jit_regset_setbit(&_jitc->reglive, node->w.w);
2200 if (jit_regset_set_p(&_jitc->regmask)) {
2201 jit_update(node->next, &_jitc->reglive, &_jitc->regmask);
2202 if (jit_regset_set_p(&_jitc->regmask)) {
2203 /* any unresolved live state is considered as live */
2204 jit_regset_ior(&_jitc->reglive,
2205 &_jitc->reglive, &_jitc->regmask);
2206 jit_regset_set_ui(&_jitc->regmask, 0);
2214 _jit_regarg_set(jit_state_t *_jit, jit_node_t *node, jit_int32_t value)
2219 if (value & jit_cc_a0_reg) {
2220 if (value & jit_cc_a0_rlh) {
2221 jit_regset_setbit(&_jitc->regarg, jit_regno(node->u.q.l));
2222 jit_regset_setbit(&_jitc->regarg, jit_regno(node->u.q.h));
2225 jit_regset_setbit(&_jitc->regarg, jit_regno(node->u.w));
2227 if (value & jit_cc_a1_reg) {
2228 if (value & jit_cc_a1_rlh) {
2229 jit_regset_setbit(&_jitc->regarg, jit_regno(node->v.q.l));
2230 jit_regset_setbit(&_jitc->regarg, jit_regno(node->v.q.h));
2233 jit_regset_setbit(&_jitc->regarg, jit_regno(node->v.w));
2235 if (value & jit_cc_a2_reg) {
2236 if (value & jit_cc_a2_rlh) {
2237 jit_regset_setbit(&_jitc->regarg, jit_regno(node->w.q.l));
2238 jit_regset_setbit(&_jitc->regarg, jit_regno(node->w.q.h));
2241 jit_regset_setbit(&_jitc->regarg, jit_regno(node->w.w));
2243 /* Prevent incorrect detection of running out of registers
2244 * if will need to patch jump, and all registers have been
2245 * used in the current block. */
2246 if (node->code == jit_code_jmpi && (node->flag & jit_flag_node)) {
2247 jit_node_t *label = node->u.n;
2248 jit_block_t *block = _jitc->blocks.ptr + label->v.w;
2249 jit_regset_set(&_jitc->reglive, &block->reglive);
2250 jit_regset_set(&_jitc->regmask, &block->regmask);
2251 if (jit_regset_set_p(&_jitc->explive)) {
2252 jit_regset_ior(&_jitc->reglive, &block->reglive, &_jitc->explive);
2253 jit_regset_xor(&_jitc->regmask, &_jitc->regmask, &_jitc->explive);
2259 _jit_regarg_clr(jit_state_t *_jit, jit_node_t *node, jit_int32_t value)
2262 jit_size_collect(node);
2264 if (value & jit_cc_a0_reg) {
2265 if (value & jit_cc_a0_rlh) {
2266 jit_regset_clrbit(&_jitc->regarg, jit_regno(node->u.q.l));
2267 jit_regset_clrbit(&_jitc->regarg, jit_regno(node->u.q.h));
2270 jit_regset_clrbit(&_jitc->regarg, jit_regno(node->u.w));
2272 if (value & jit_cc_a1_reg) {
2273 if (value & jit_cc_a1_rlh) {
2274 jit_regset_clrbit(&_jitc->regarg, jit_regno(node->v.q.l));
2275 jit_regset_clrbit(&_jitc->regarg, jit_regno(node->v.q.h));
2278 jit_regset_clrbit(&_jitc->regarg, jit_regno(node->v.w));
2280 if (value & jit_cc_a2_reg) {
2281 if (value & jit_cc_a2_rlh) {
2282 jit_regset_clrbit(&_jitc->regarg, jit_regno(node->w.q.l));
2283 jit_regset_clrbit(&_jitc->regarg, jit_regno(node->w.q.h));
2286 jit_regset_clrbit(&_jitc->regarg, jit_regno(node->w.w));
2291 _jit_realize(jit_state_t *_jit)
2293 assert(!_jitc->realize);
2294 if (_jitc->function)
2299 /* ensure it is aligned */
2300 _jitc->data.offset = (_jitc->data.offset + 7) & -8;
2303 /* Heuristic to guess code buffer size */
2305 _jit->code.length = _jitc->pool.length * 1024 * _jitc->mult;
2307 _jit->code.length = jit_get_size();
2312 _jit_dataset(jit_state_t *_jit)
2321 assert(!_jitc->dataset);
2323 assert(_jit->user_data);
2325 if (!_jit->user_data) {
2327 /* create read only data buffer */
2328 _jit->data.length = (_jitc->data.offset +
2329 /* reserve space for annotations */
2330 _jitc->note.size + 4095) & -4096;
2332 mmap_fd = open("/dev/zero", O_RDWR);
2334 _jit->data.ptr = mmap(NULL, _jit->data.length,
2335 PROT_READ | PROT_WRITE,
2336 MAP_PRIVATE | MAP_ANON, mmap_fd, 0);
2337 assert(_jit->data.ptr != MAP_FAILED);
2342 #endif /* !HAVE_MMAP */
2344 if (!_jitc->no_data)
2345 jit_memcpy(_jit->data.ptr, _jitc->data.ptr, _jitc->data.offset);
2347 if (_jitc->no_note) {
2348 /* Space for one note is always allocated, so revert it here
2349 * if after jit_new_state was called, it is also requested to
2350 * not generate annotation information */
2351 _jit->note.length = 0;
2352 _jitc->note.size = 0;
2355 _jitc->note.base = _jit->data.ptr;
2356 if (!_jitc->no_data)
2357 _jitc->note.base += _jitc->data.offset;
2358 memset(_jitc->note.base, 0, _jitc->note.size);
2361 if (_jit->user_data)
2362 /* Need the temporary hashed data until jit_emit is finished */
2363 ptr = _jitc->no_data ? _jitc->data.ptr : _jit->data.ptr;
2365 ptr = _jit->data.ptr;
2366 /* Temporary hashed data no longer required */
2367 jit_free((jit_pointer_t *)&_jitc->data.ptr);
2370 for (offset = 0; offset < _jitc->data.size; offset++) {
2371 for (node = _jitc->data.table[offset]; node; node = node->next) {
2372 node->flag |= jit_flag_patch;
2373 node->u.w = (jit_word_t)(ptr + node->u.w);
2381 _jit_get_code(jit_state_t *_jit, jit_word_t *length)
2383 assert(_jitc->realize);
2386 /* If code already generated, return exact size of code */
2387 *length = _jit->pc.uc - _jit->code.ptr;
2389 /* Else return current size of the code buffer */
2390 *length = _jit->code.length;
2393 return (_jit->code.ptr);
2397 _jit_set_code(jit_state_t *_jit, jit_pointer_t ptr, jit_word_t length)
2399 assert(_jitc->realize);
2400 _jit->code.ptr = ptr;
2401 _jit->code.length = length;
2402 _jit->user_code = 1;
2406 _jit_get_data(jit_state_t *_jit, jit_word_t *data_size, jit_word_t *note_size)
2408 assert(_jitc->realize);
2410 *data_size = _jitc->data.offset;
2412 *note_size = _jitc->note.size;
2413 return (_jit->data.ptr);
2417 _jit_set_data(jit_state_t *_jit, jit_pointer_t ptr,
2418 jit_word_t length, jit_word_t flags)
2420 assert(_jitc->realize);
2421 if (flags & JIT_DISABLE_DATA)
2424 assert(length >= _jitc->data.offset);
2425 if (flags & JIT_DISABLE_NOTE)
2428 if (flags & JIT_DISABLE_DATA)
2429 assert(length >= _jitc->note.size);
2431 assert(length >= _jitc->data.offset + _jitc->note.size);
2433 _jit->data.ptr = ptr;
2434 _jit->data.length = length;
2435 _jit->user_data = 1;
2439 _jit_emit(jit_state_t *_jit)
2448 int mmap_prot, mmap_flags;
2450 if (!_jitc->realize)
2453 if (!_jitc->dataset)
2459 assert(_jit->user_code);
2461 if (!_jit->user_code) {
2462 mmap_prot = PROT_READ | PROT_WRITE;
2463 #if !(__OpenBSD__ || __APPLE__)
2464 mmap_prot |= PROT_EXEC;
2467 mmap_prot = PROT_MPROTECT(mmap_prot);
2470 mmap_flags = MAP_PRIVATE;
2472 mmap_flags |= MAP_ANON;
2474 mmap_fd = open("/dev/zero", O_RDWR);
2476 _jit->code.ptr = mmap(NULL, _jit->code.length,
2477 mmap_prot, mmap_flags, mmap_fd, 0);
2478 assert(_jit->code.ptr != MAP_FAILED);
2480 #endif /* !HAVE_MMAP */
2481 _jitc->code.end = _jit->code.ptr + _jit->code.length -
2482 jit_get_max_instr();
2483 _jit->pc.uc = _jit->code.ptr;
2487 result = mprotect(_jit->code.ptr, _jit->code.length,
2488 PROT_READ | PROT_WRITE);
2489 assert(result == 0);
2491 if ((code = emit_code()) == NULL) {
2492 _jitc->patches.offset = 0;
2493 for (node = _jitc->head; node; node = node->next) {
2495 (node->code == jit_code_label ||
2496 node->code == jit_code_epilog))
2497 node->flag &= ~jit_flag_patch;
2500 assert(_jit->user_code);
2502 if (_jit->user_code)
2506 length = _jitc->pool.length * 1024 * _jitc->mult;
2508 /* Should only happen on very special cases */
2509 length = _jit->code.length + 4096;
2513 munmap(_jit->code.ptr, _jit->code.length);
2518 _jit->code.ptr = mremap(_jit->code.ptr, _jit->code.length,
2519 _jit->code.ptr, length, 0);
2521 _jit->code.ptr = mremap(_jit->code.ptr, _jit->code.length,
2522 length, MREMAP_MAYMOVE, NULL);
2525 _jit->code.ptr = mmap(NULL, length,
2526 mmap_prot, mmap_flags, mmap_fd, 0);
2529 assert(_jit->code.ptr != MAP_FAILED);
2530 _jit->code.length = length;
2531 _jitc->code.end = _jit->code.ptr + _jit->code.length -
2532 jit_get_max_instr();
2533 _jit->pc.uc = _jit->code.ptr;
2534 #endif /* !HAVE_MMAP */
2541 if (!_jit->user_code)
2546 if (!_jitc->no_note)
2549 if (_jit->user_data)
2550 jit_free((jit_pointer_t *)&_jitc->data.ptr);
2553 result = mprotect(_jit->data.ptr,
2554 _jit->data.length, PROT_READ);
2555 assert(result == 0);
2557 if (!_jit->user_code) {
2558 _jit->code.protect = _jit->pc.uc - _jit->code.ptr;
2559 # if __riscv && __WORDSIZE == 64
2560 /* FIXME should start adding consts at a page boundary */
2561 _jit->code.protect -= _jitc->consts.hash.count * sizeof(jit_word_t);
2563 result = mprotect(_jit->code.ptr, _jit->code.protect, PROT_READ | PROT_EXEC);
2564 assert(result == 0);
2566 #endif /* HAVE_MMAP */
2568 return (_jit->code.ptr);
2574 _jit_protect(jit_state_t *_jit)
2577 assert (_jit->user_code);
2580 if (_jit->user_code) return;
2581 result = mprotect (_jit->code.ptr, _jit->code.protect, PROT_READ | PROT_EXEC);
2582 assert (result == 0);
2587 _jit_unprotect(jit_state_t *_jit)
2590 assert (_jit->user_code);
2593 if (_jit->user_code) return;
2594 result = mprotect (_jit->code.ptr, _jit->code.protect, PROT_READ | PROT_WRITE);
2595 assert (result == 0);
2600 _jit_frame(jit_state_t *_jit, jit_int32_t frame)
2602 jit_trampoline(frame, 1);
2606 _jit_tramp(jit_state_t *_jit, jit_int32_t frame)
2608 jit_trampoline(frame, 0);
2612 _jit_trampoline(jit_state_t *_jit, jit_int32_t frame, jit_bool_t prolog)
2616 /* Must be called after prolog, actually, just to simplify
2617 * tests and know there is a current function and that
2618 * _jitc->function->self.aoff is at the before any alloca value */
2619 assert(_jitc->tail && _jitc->tail->code == jit_code_prolog);
2621 /* + 24 for 3 possible spilled temporaries (that could be a double) */
2623 #if defined(__hppa__)
2624 frame += _jitc->function->self.aoff;
2626 frame -= _jitc->function->self.aoff;
2628 _jitc->function->frame = frame;
2630 _jitc->function->define_frame = 1;
2632 _jitc->function->assume_frame = 1;
2633 for (regno = 0; regno < _jitc->reglen; regno++)
2634 if (jit_class(_rvs[regno].spec) & jit_class_sav)
2635 jit_regset_setbit(&_jitc->function->regset, regno);
2638 /* Compute initial reglive and regmask set values of a basic block.
2639 * reglive is the set of known live registers
2640 * regmask is the set of registers not referenced in the block
2641 * Registers in regmask might be live.
2644 _jit_setup(jit_state_t *_jit, jit_block_t *block)
2648 unsigned long value;
2650 jit_regset_set_mask(&block->regmask, _jitc->reglen);
2651 for (value = 0; value < _jitc->reglen; ++value)
2652 if (!(jit_class(_rvs[value].spec) & (jit_class_gpr|jit_class_fpr)))
2653 jit_regset_clrbit(&block->regmask, value);
2655 for (node = block->label->next; node; node = node->next) {
2656 switch (node->code) {
2657 case jit_code_label: case jit_code_prolog:
2658 case jit_code_epilog:
2661 /* Check argument registers in reverse order to properly
2662 * handle registers that are both, argument and result */
2663 value = jit_classify(node->code);
2664 if (value & jit_cc_a2_reg) {
2665 live = !(value & jit_cc_a2_chg);
2666 if (value & jit_cc_a2_rlh) {
2667 /* Assume will not modify a pair in second argument */
2669 if (!(node->w.q.l & jit_regno_patch) &&
2670 jit_regset_tstbit(&block->regmask, node->w.q.l)) {
2671 jit_regset_clrbit(&block->regmask, node->w.q.l);
2673 if (!(node->w.q.h & jit_regno_patch) &&
2674 jit_regset_tstbit(&block->regmask, node->w.q.h)) {
2675 jit_regset_clrbit(&block->regmask, node->w.q.h);
2679 if (!(node->w.w & jit_regno_patch) &&
2680 jit_regset_tstbit(&block->regmask, node->w.w)) {
2681 jit_regset_clrbit(&block->regmask, node->w.w);
2683 jit_regset_setbit(&block->reglive, node->w.w);
2687 if (value & jit_cc_a1_reg) {
2688 live = !(value & jit_cc_a1_chg);
2689 if (value & jit_cc_a1_rlh) {
2690 /* Assume will not modify a pair in second argument */
2692 if (!(node->v.q.l & jit_regno_patch) &&
2693 jit_regset_tstbit(&block->regmask, node->v.q.l)) {
2694 jit_regset_clrbit(&block->regmask, node->v.q.l);
2696 if (!(node->v.q.h & jit_regno_patch) &&
2697 jit_regset_tstbit(&block->regmask, node->v.q.h)) {
2698 jit_regset_clrbit(&block->regmask, node->v.q.h);
2702 if (!(node->v.w & jit_regno_patch) &&
2703 jit_regset_tstbit(&block->regmask, node->v.w)) {
2704 jit_regset_clrbit(&block->regmask, node->v.w);
2706 jit_regset_setbit(&block->reglive, node->v.w);
2710 if (value & jit_cc_a0_reg) {
2711 live = !(value & jit_cc_a0_chg);
2712 if (value & jit_cc_a0_rlh) {
2713 if (!(node->u.q.l & jit_regno_patch) &&
2714 jit_regset_tstbit(&block->regmask, node->u.q.l)) {
2715 jit_regset_clrbit(&block->regmask, node->u.q.l);
2717 jit_regset_setbit(&block->reglive, node->u.q.l);
2719 if (!(node->u.q.h & jit_regno_patch) &&
2720 jit_regset_tstbit(&block->regmask, node->u.q.h)) {
2721 jit_regset_clrbit(&block->regmask, node->u.q.h);
2723 jit_regset_setbit(&block->reglive, node->u.q.h);
2727 if (!(node->u.w & jit_regno_patch) &&
2728 jit_regset_tstbit(&block->regmask, node->u.w)) {
2729 jit_regset_clrbit(&block->regmask, node->u.w);
2731 jit_regset_setbit(&block->reglive, node->u.w);
2740 /* Update regmask and reglive of blocks at entry point of branch targets
2741 * or normal flow that have a live register not used in this block.
2744 _jit_follow(jit_state_t *_jit, jit_block_t *block)
2750 unsigned long value;
2752 jit_regset_t reglive;
2753 jit_regset_t regmask;
2754 jit_regset_t regtemp;
2756 jit_regset_set(®live, &block->reglive);
2757 jit_regset_set(®mask, &block->regmask);
2758 for (node = block->label->next; node; node = node->next) {
2759 switch (node->code) {
2760 case jit_code_label:
2761 /* Do not consider jmpi and jmpr cannot jump to the
2762 * next instruction. */
2763 next = _jitc->blocks.ptr + node->v.w;
2764 /* Set of live registers in next block that are at unknown
2765 * state in this block. */
2766 jit_regset_and(®temp, ®mask, &next->reglive);
2767 if (jit_regset_set_p(®temp)) {
2768 /* Add live state of next block to current block. */
2769 jit_regset_ior(&block->reglive, &block->reglive, ®temp);
2770 /* Remove from unknown state bitmask. */
2771 jit_regset_com(®temp, ®temp);
2772 jit_regset_and(&block->regmask, &block->regmask, ®temp);
2775 case jit_code_prolog:
2776 case jit_code_epilog:
2778 case jit_code_callr:
2779 value = jit_regno(node->u.w);
2780 if (!(node->u.w & jit_regno_patch)) {
2781 if (jit_regset_tstbit(®mask, value)) {
2782 jit_regset_clrbit(®mask, value);
2783 jit_regset_setbit(®live, value);
2786 case jit_code_calli:
2787 for (value = 0; value < _jitc->reglen; ++value) {
2788 value = jit_regset_scan1(®mask, value);
2789 if (value >= _jitc->reglen)
2791 spec = jit_class(_rvs[value].spec);
2792 if (!(spec & jit_class_sav))
2793 jit_regset_clrbit(®mask, value);
2794 if ((spec & jit_class_arg) && jit_regarg_p(node, value))
2795 jit_regset_setbit(®live, value);
2799 value = jit_classify(node->code);
2800 if (value & jit_cc_a2_reg) {
2801 if (value & jit_cc_a2_rlh) {
2802 if (!(node->w.q.l & jit_regno_patch)) {
2803 /* Assume register is not changed */
2804 if (jit_regset_tstbit(®mask, node->w.q.l))
2805 jit_regset_clrbit(®mask, node->w.q.l);
2807 if (!(node->w.q.h & jit_regno_patch)) {
2808 if (jit_regset_tstbit(®mask, node->w.q.h))
2809 jit_regset_clrbit(®mask, node->w.q.h);
2813 if (!(node->w.w & jit_regno_patch)) {
2814 if (jit_regset_tstbit(®mask, node->w.w)) {
2815 jit_regset_clrbit(®mask, node->w.w);
2816 if (!(value & jit_cc_a2_chg))
2817 jit_regset_setbit(®live, node->w.w);
2822 if (value & jit_cc_a1_reg) {
2823 if (value & jit_cc_a1_rlh) {
2824 if (!(node->v.q.l & jit_regno_patch)) {
2825 /* Assume register is not changed */
2826 if (jit_regset_tstbit(®mask, node->v.q.l))
2827 jit_regset_clrbit(®mask, node->v.q.l);
2829 if (!(node->v.q.h & jit_regno_patch)) {
2830 if (jit_regset_tstbit(®mask, node->v.q.h))
2831 jit_regset_clrbit(®mask, node->v.q.h);
2835 if (!(node->v.w & jit_regno_patch)) {
2836 if (jit_regset_tstbit(®mask, node->v.w)) {
2837 jit_regset_clrbit(®mask, node->v.w);
2838 if (!(value & jit_cc_a1_chg))
2839 jit_regset_setbit(®live, node->v.w);
2844 if (value & jit_cc_a0_reg) {
2845 if (value & jit_cc_a0_rlh) {
2846 if (!(node->u.q.l & jit_regno_patch)) {
2847 if (jit_regset_tstbit(®mask, node->u.q.l)) {
2848 jit_regset_clrbit(®mask, node->u.q.l);
2849 if (!(value & jit_cc_a0_chg))
2850 jit_regset_setbit(®live, node->u.q.l);
2853 if (!(node->u.q.h & jit_regno_patch)) {
2854 if (jit_regset_tstbit(®mask, node->u.q.h)) {
2855 jit_regset_clrbit(®mask, node->u.q.h);
2856 if (!(value & jit_cc_a0_chg))
2857 jit_regset_setbit(®live, node->u.q.h);
2862 if (!(node->u.w & jit_regno_patch)) {
2863 if (jit_regset_tstbit(®mask, node->u.w)) {
2864 jit_regset_clrbit(®mask, node->u.w);
2865 if (!(value & jit_cc_a0_chg))
2866 jit_regset_setbit(®live, node->u.w);
2871 if (value & jit_cc_a0_jmp) {
2872 if (node->flag & jit_flag_node) {
2874 /* Do not consider jmpi and jmpr cannot jump to the
2875 * next instruction. */
2876 next = _jitc->blocks.ptr + label->v.w;
2877 jit_regset_and(®temp, ®mask, &next->reglive);
2878 if (jit_regset_set_p(®temp)) {
2879 /* Add live state. */
2880 jit_regset_ior(&block->reglive,
2881 &block->reglive, ®temp);
2882 /* Remove from unknown state bitmask. */
2883 jit_regset_com(®temp, ®temp);
2884 jit_regset_and(&block->regmask,
2885 &block->regmask, ®temp);
2890 /* Jump to unknown location.
2891 * This is a pitfall of the implementation.
2892 * Only jmpi to not a jit code should reach here,
2893 * or a jmpr of a computed address.
2894 * Because the implementation needs jit_class_nospill
2895 * registers, must treat jmpr as a function call. This
2896 * means that only JIT_Vn registers can be trusted on
2899 jit_regset_set_ui(®mask, 0);
2900 for (regno = 0; regno < _jitc->reglen; regno++) {
2901 spec = jit_class(_rvs[regno].spec);
2902 if ((spec & (jit_class_gpr|jit_class_fpr)) &&
2903 (spec & jit_class_sav))
2904 jit_regset_setbit(®mask, regno);
2906 /* Assume non callee save registers are live due
2907 * to jump to unknown location. */
2908 /* Treat all callee save as live. */
2909 jit_regset_ior(&block->reglive, ®live, ®mask);
2910 /* Treat anything else as dead. */
2919 /* Follow code generation up to finding a label or end of code.
2920 * When finding a label, update the set of live registers.
2921 * On branches, update based on taken branch or normal flow.
2924 _jit_update(jit_state_t *_jit, jit_node_t *node,
2925 jit_regset_t *live, jit_regset_t *mask)
2929 unsigned long value;
2932 jit_regset_t regtemp;
2934 for (; node; node = node->next) {
2935 if (jit_regset_set_p(mask) == 0)
2937 switch (node->code) {
2938 case jit_code_label:
2939 block = _jitc->blocks.ptr + node->v.w;
2940 jit_regset_and(®temp, mask, &block->reglive);
2941 if (jit_regset_set_p(®temp)) {
2942 /* Add live state. */
2943 jit_regset_ior(live, live, ®temp);
2944 /* Remove from unknown state bitmask. */
2945 jit_regset_com(®temp, ®temp);
2946 jit_regset_and(mask, mask, ®temp);
2949 case jit_code_prolog:
2950 jit_regset_set_ui(mask, 0);
2952 case jit_code_epilog:
2953 jit_regset_set_ui(mask, 0);
2955 case jit_code_callr:
2956 value = jit_regno(node->u.w);
2957 if (!(node->u.w & jit_regno_patch)) {
2958 if (jit_regset_tstbit(mask, value)) {
2959 jit_regset_clrbit(mask, value);
2960 jit_regset_setbit(live, value);
2963 case jit_code_calli:
2964 for (value = 0; value < _jitc->reglen; ++value) {
2965 value = jit_regset_scan1(mask, value);
2966 if (value >= _jitc->reglen)
2968 spec = jit_class(_rvs[value].spec);
2969 if (!(spec & jit_class_sav))
2970 jit_regset_clrbit(mask, value);
2971 if ((spec & jit_class_arg) && jit_regarg_p(node, value))
2972 jit_regset_setbit(live, value);
2976 value = jit_classify(node->code);
2977 if (value & jit_cc_a2_reg) {
2978 if (value & jit_cc_a2_rlh) {
2979 if (!(node->w.q.l & jit_regno_patch)) {
2980 /* Assume register is not changed */
2981 if (jit_regset_tstbit(mask, node->w.q.l))
2982 jit_regset_clrbit(mask, node->w.q.l);
2984 if (!(node->w.q.h & jit_regno_patch)) {
2985 if (jit_regset_tstbit(mask, node->w.q.h))
2986 jit_regset_clrbit(mask, node->w.q.h);
2990 if (!(node->w.w & jit_regno_patch)) {
2991 if (jit_regset_tstbit(mask, node->w.w)) {
2992 jit_regset_clrbit(mask, node->w.w);
2993 if (!(value & jit_cc_a2_chg))
2994 jit_regset_setbit(live, node->w.w);
2999 if (value & jit_cc_a1_reg) {
3000 if (value & jit_cc_a1_rlh) {
3001 if (!(node->v.q.l & jit_regno_patch)) {
3002 /* Assume register is not changed */
3003 if (jit_regset_tstbit(mask, node->v.q.l))
3004 jit_regset_clrbit(mask, node->v.q.l);
3006 if (!(node->v.q.h & jit_regno_patch)) {
3007 if (jit_regset_tstbit(mask, node->v.q.h))
3008 jit_regset_clrbit(mask, node->v.q.h);
3012 if (!(node->v.w & jit_regno_patch)) {
3013 if (jit_regset_tstbit(mask, node->v.w)) {
3014 jit_regset_clrbit(mask, node->v.w);
3015 if (!(value & jit_cc_a1_chg))
3016 jit_regset_setbit(live, node->v.w);
3021 if (value & jit_cc_a0_reg) {
3022 if (value & jit_cc_a0_rlh) {
3023 if (!(node->u.q.l & jit_regno_patch)) {
3024 if (jit_regset_tstbit(mask, node->u.q.l)) {
3025 jit_regset_clrbit(mask, node->u.q.l);
3026 if (!(value & jit_cc_a0_chg))
3027 jit_regset_setbit(live, node->u.q.l);
3030 if (!(node->u.q.h & jit_regno_patch)) {
3031 if (jit_regset_tstbit(mask, node->u.q.h)) {
3032 jit_regset_clrbit(mask, node->u.q.h);
3033 if (!(value & jit_cc_a0_chg))
3034 jit_regset_setbit(live, node->u.q.h);
3039 if (!(node->u.w & jit_regno_patch)) {
3040 if (jit_regset_tstbit(mask, node->u.w)) {
3041 jit_regset_clrbit(mask, node->u.w);
3042 if (!(value & jit_cc_a0_chg))
3043 jit_regset_setbit(live, node->u.w);
3048 if (value & jit_cc_a0_jmp) {
3049 if (node->flag & jit_flag_node) {
3051 /* Do not consider jmpi and jmpr cannot jump to the
3052 * next instruction. */
3053 block = _jitc->blocks.ptr + label->v.w;
3054 jit_regset_and(®temp, mask, &block->reglive);
3055 if (jit_regset_set_p(®temp)) {
3056 /* Add live state. */
3057 jit_regset_ior(live, live, ®temp);
3058 /* Remove from unknown state bitmask. */
3059 jit_regset_com(®temp, ®temp);
3060 jit_regset_and(mask, mask, ®temp);
3064 /* Jump to unknown location.
3065 * This is a pitfall of the implementation.
3066 * Only jmpi to not a jit code should reach here,
3067 * or a jmpr of a computed address.
3068 * Because the implementation needs jit_class_nospill
3069 * registers, must treat jmpr as a function call. This
3070 * means that only JIT_Vn registers can be trusted on
3073 jit_regset_set_ui(mask, 0);
3074 for (regno = 0; regno < _jitc->reglen; regno++) {
3075 spec = jit_class(_rvs[regno].spec);
3076 if ((spec & (jit_class_gpr|jit_class_fpr)) &&
3077 (spec & jit_class_sav))
3078 jit_regset_setbit(mask, regno);
3080 /* Assume non callee save registers are live due
3081 * to jump to unknown location. */
3082 /* Treat all callee save as live. */
3083 jit_regset_ior(live, live, mask);
3084 /* Prevent explicitly set as live registers to
3085 * be used as a temporary for the jmpi. */
3086 jit_regset_ior(live, live, &_jitc->explive);
3087 /* Treat anything else as dead. */
3097 _thread_jumps(jit_state_t *_jit)
3104 for (prev = node = _jitc->head; node;) {
3106 switch (node->code) {
3108 if (redundant_jump(prev, node)) {
3112 if (shortcut_jump(prev, node))
3116 case jit_code_callr: case jit_code_calli:
3117 /* non optimizable jump like code */
3120 mask = jit_classify(node->code);
3121 if (mask & jit_cc_a0_jmp) {
3122 if (reverse_jump(prev, node) ||
3123 shortcut_jump(prev, node))
3134 _sequential_labels(jit_state_t *_jit)
3142 for (prev = node = _jitc->head; node; node = next) {
3144 if (node->code == jit_code_label) {
3147 del_label(prev, node);
3150 if (prev != node && prev->code == jit_code_label) {
3151 if ((jump = node->link)) {
3152 for (; jump; jump = link) {
3154 if (jump->code == jit_code_movi)
3158 jump->link = prev->link;
3163 del_label(prev, node);
3167 if (next && next->code == jit_code_label && !next->flag) {
3168 if ((jump = next->link)) {
3169 for (; jump; jump = link) {
3171 if (jump->code == jit_code_movi)
3175 jump->link = node->link;
3180 del_label(node, next);
3190 _split_branches(jit_state_t *_jit)
3196 jit_block_t *blocks;
3200 length = _jitc->blocks.length;
3201 jit_alloc((jit_pointer_t *)&blocks, length * sizeof(jit_block_t));
3202 if ((node = _jitc->head) &&
3203 (node->code == jit_code_label || node->code == jit_code_prolog)) {
3204 block = _jitc->blocks.ptr + node->v.w;
3205 memcpy(blocks, block, sizeof(jit_block_t));
3211 for (node = _jitc->head; node; node = next) {
3212 if ((next = node->next)) {
3213 if (next->code == jit_code_label ||
3214 next->code == jit_code_prolog ||
3215 next->code == jit_code_epilog) {
3216 if (offset >= length) {
3217 jit_realloc((jit_pointer_t *)&blocks,
3218 length * sizeof(jit_block_t),
3219 (length + 16) * sizeof(jit_block_t));
3222 block = _jitc->blocks.ptr + next->v.w;
3223 memcpy(blocks + offset, block, sizeof(jit_block_t));
3224 next->v.w = offset++;
3226 /* split block on branches */
3227 else if (jit_classify(node->code) & jit_cc_a0_jmp) {
3228 label = new_node(jit_code_label);
3231 if (offset >= length) {
3232 jit_realloc((jit_pointer_t *)&blocks,
3233 length * sizeof(jit_block_t),
3234 (length + 16) * sizeof(jit_block_t));
3237 block = blocks + offset;
3238 block->label = label;
3239 label->v.w = offset++;
3240 jit_regset_new(&block->reglive);
3241 jit_regset_new(&block->regmask);
3245 jit_free((jit_pointer_t *)&_jitc->blocks.ptr);
3246 _jitc->blocks.ptr = blocks;
3247 _jitc->blocks.offset = offset;
3248 _jitc->blocks.length = length;
3252 _shortcut_jump(jit_state_t *_jit, jit_node_t *prev, jit_node_t *node)
3259 if (!(node->flag & jit_flag_node))
3261 assert(node->code != jit_code_jmpr);
3262 cond = node->code != jit_code_jmpi;
3264 for (next = jump->next; next; next = next->next) {
3265 switch (next->code) {
3267 if (!(next->flag & jit_flag_node))
3269 if (jump->link == node)
3270 jump->link = node->link;
3272 for (temp = jump->link;
3275 assert(temp != NULL);
3276 temp->link = node->link;
3280 node->link = jump->link;
3286 node->code = jit_code_jmpr;
3287 node->u.w = next->u.w;
3289 node->flag &= ~jit_flag_node;
3291 case jit_code_note: case jit_code_label:
3301 _redundant_jump(jit_state_t *_jit, jit_node_t *prev, jit_node_t *node)
3303 jit_node_t *local_prev;
3304 jit_node_t *local_next;
3306 if (!(node->flag & jit_flag_node))
3308 for (local_prev = node, local_next = node->next;
3310 local_prev = local_next, local_next = local_next->next) {
3312 switch (local_next->code) {
3313 case jit_code_label: case jit_code_epilog:
3314 if (node->u.n == local_next) {
3315 if (local_next->link == node)
3316 local_next->link = node->link;
3318 for (local_prev = local_next->link;
3319 local_prev->link != node;
3320 local_prev = local_prev->link)
3321 assert(local_prev != NULL);
3322 local_prev->link = node->link;
3324 del_node(prev, node);
3328 case jit_code_name: case jit_code_note:
3338 reverse_jump_code(jit_code_t code)
3341 case jit_code_bltr: return (jit_code_bger);
3342 case jit_code_blti: return (jit_code_bgei);
3343 case jit_code_bltr_u: return (jit_code_bger_u);
3344 case jit_code_blti_u: return (jit_code_bgei_u);
3345 case jit_code_bler: return (jit_code_bgtr);
3346 case jit_code_blei: return (jit_code_bgti);
3347 case jit_code_bler_u: return (jit_code_bgtr_u);
3348 case jit_code_blei_u: return (jit_code_bgti_u);
3349 case jit_code_beqr: return (jit_code_bner);
3350 case jit_code_beqi: return (jit_code_bnei);
3351 case jit_code_bger: return (jit_code_bltr);
3352 case jit_code_bgei: return (jit_code_blti);
3353 case jit_code_bger_u: return (jit_code_bltr_u);
3354 case jit_code_bgei_u: return (jit_code_blti_u);
3355 case jit_code_bgtr: return (jit_code_bler);
3356 case jit_code_bgti: return (jit_code_blei);
3357 case jit_code_bgtr_u: return (jit_code_bler_u);
3358 case jit_code_bgti_u: return (jit_code_blei_u);
3359 case jit_code_bner: return (jit_code_beqr);
3360 case jit_code_bnei: return (jit_code_beqi);
3361 case jit_code_bmsr: return (jit_code_bmcr);
3362 case jit_code_bmsi: return (jit_code_bmci);
3363 case jit_code_bmcr: return (jit_code_bmsr);
3364 case jit_code_bmci: return (jit_code_bmsi);
3365 case jit_code_bltr_f: return (jit_code_bunger_f);
3366 case jit_code_blti_f: return (jit_code_bungei_f);
3367 case jit_code_bler_f: return (jit_code_bungtr_f);
3368 case jit_code_blei_f: return (jit_code_bungti_f);
3370 case jit_code_beqr_f: return (jit_code_bner_f);
3371 case jit_code_beqi_f: return (jit_code_bnei_f);
3373 case jit_code_bger_f: return (jit_code_bunltr_f);
3374 case jit_code_bgei_f: return (jit_code_bunlti_f);
3375 case jit_code_bgtr_f: return (jit_code_bunler_f);
3376 case jit_code_bgti_f: return (jit_code_bunlei_f);
3378 case jit_code_bner_f: return (jit_code_beqr_f);
3379 case jit_code_bnei_f: return (jit_code_beqi_f);
3381 case jit_code_bunltr_f: return (jit_code_bger_f);
3382 case jit_code_bunlti_f: return (jit_code_bgei_f);
3383 case jit_code_bunler_f: return (jit_code_bgtr_f);
3384 case jit_code_bunlei_f: return (jit_code_bgti_f);
3386 case jit_code_buneqr_f: return (jit_code_bltgtr_f);
3387 case jit_code_buneqi_f: return (jit_code_bltgti_f);
3389 case jit_code_bunger_f: return (jit_code_bltr_f);
3390 case jit_code_bungei_f: return (jit_code_blti_f);
3391 case jit_code_bungtr_f: return (jit_code_bler_f);
3392 case jit_code_bungti_f: return (jit_code_blei_f);
3394 case jit_code_bltgtr_f: return (jit_code_buneqr_f);
3395 case jit_code_bltgti_f: return (jit_code_buneqi_f);
3397 case jit_code_bordr_f: return (jit_code_bunordr_f);
3398 case jit_code_bordi_f: return (jit_code_bunordi_f);
3399 case jit_code_bunordr_f:return (jit_code_bordr_f);
3400 case jit_code_bunordi_f:return (jit_code_bordi_f);
3401 case jit_code_bltr_d: return (jit_code_bunger_d);
3402 case jit_code_blti_d: return (jit_code_bungei_d);
3403 case jit_code_bler_d: return (jit_code_bungtr_d);
3404 case jit_code_blei_d: return (jit_code_bungti_d);
3406 case jit_code_beqr_d: return (jit_code_bner_d);
3407 case jit_code_beqi_d: return (jit_code_bnei_d);
3409 case jit_code_bger_d: return (jit_code_bunltr_d);
3410 case jit_code_bgei_d: return (jit_code_bunlti_d);
3411 case jit_code_bgtr_d: return (jit_code_bunler_d);
3412 case jit_code_bgti_d: return (jit_code_bunlei_d);
3414 case jit_code_bner_d: return (jit_code_beqr_d);
3415 case jit_code_bnei_d: return (jit_code_beqi_d);
3417 case jit_code_bunltr_d: return (jit_code_bger_d);
3418 case jit_code_bunlti_d: return (jit_code_bgei_d);
3419 case jit_code_bunler_d: return (jit_code_bgtr_d);
3420 case jit_code_bunlei_d: return (jit_code_bgti_d);
3422 case jit_code_buneqr_d: return (jit_code_bltgtr_d);
3423 case jit_code_buneqi_d: return (jit_code_bltgti_d);
3425 case jit_code_bunger_d: return (jit_code_bltr_d);
3426 case jit_code_bungei_d: return (jit_code_blti_d);
3427 case jit_code_bungtr_d: return (jit_code_bler_d);
3428 case jit_code_bungti_d: return (jit_code_blei_d);
3430 case jit_code_bltgtr_d: return (jit_code_buneqr_d);
3431 case jit_code_bltgti_d: return (jit_code_buneqi_d);
3433 case jit_code_bordr_d: return (jit_code_bunordr_d);
3434 case jit_code_bordi_d: return (jit_code_bunordi_d);
3435 case jit_code_bunordr_d:return (jit_code_bordr_d);
3436 case jit_code_bunordi_d:return (jit_code_bordi_d);
3437 case jit_code_boaddr: return (jit_code_bxaddr);
3438 case jit_code_boaddi: return (jit_code_bxaddi);
3439 case jit_code_boaddr_u: return (jit_code_bxaddr_u);
3440 case jit_code_boaddi_u: return (jit_code_bxaddi_u);
3441 case jit_code_bxaddr: return (jit_code_boaddr);
3442 case jit_code_bxaddi: return (jit_code_boaddi);
3443 case jit_code_bxaddr_u: return (jit_code_boaddr_u);
3444 case jit_code_bxaddi_u: return (jit_code_boaddi_u);
3445 case jit_code_bosubr: return (jit_code_bxsubr);
3446 case jit_code_bosubi: return (jit_code_bxsubi);
3447 case jit_code_bosubr_u: return (jit_code_bxsubr_u);
3448 case jit_code_bosubi_u: return (jit_code_bxsubi_u);
3449 case jit_code_bxsubr: return (jit_code_bosubr);
3450 case jit_code_bxsubi: return (jit_code_bosubi);
3451 case jit_code_bxsubr_u: return (jit_code_bosubr_u);
3452 case jit_code_bxsubi_u: return (jit_code_bosubi_u);
3453 default: abort(); /* invalid jump code */
3458 * change common pattern:
3459 * <cond_jump L0> <jump L1> <label L0>
3461 * <reverse_cond_jump L1>
3464 _reverse_jump(jit_state_t *_jit, jit_node_t *prev, jit_node_t *node)
3466 jit_node_t *local_prev;
3467 jit_node_t *local_next;
3468 jit_node_t *local_jump;
3470 if (!(node->flag & jit_flag_node))
3472 /* =><cond_jump L0> <jump L1> <label L0> */
3473 local_next = node->next;
3474 if (local_next->code != jit_code_jmpi ||
3475 !(local_next->flag & jit_flag_node))
3477 /* <cond_jump L0> =><jump L1> <label L0> */
3479 local_jump = local_next->u.n;
3480 for (local_prev = local_next, local_next = local_next->next;
3482 local_prev = local_next, local_next = local_next->next) {
3483 switch (local_next->code) {
3484 case jit_code_label: case jit_code_epilog:
3485 if (node->u.n == local_next) {
3486 if (local_next->link == node)
3487 local_next->link = node->link;
3489 for (local_prev = local_next->link;
3490 local_prev->link != node;
3491 local_prev = local_prev->link)
3492 assert(local_prev != NULL);
3493 local_prev->link = node->link;
3495 del_node(node, node->next);
3496 node->code = reverse_jump_code(node->code);
3497 node->u.n = local_jump;
3498 node->link = local_jump->link;
3499 local_jump->link = node;
3513 _redundant_store(jit_state_t *_jit, jit_node_t *node, jit_bool_t jump)
3524 if (prev->code == jit_code_epilog)
3526 assert(prev->code == jit_code_label);
3527 if ((prev->flag & jit_flag_head) || node->link || prev->link != node)
3528 /* multiple sources */
3530 /* if there are sequential labels it will return below */
3536 regno = jit_regno(node->v.w);
3537 for (iter = prev->next; iter; prev = iter, iter = iter->next) {
3538 switch (iter->code) {
3539 case jit_code_label: case jit_code_prolog:
3540 case jit_code_epilog:
3543 if (regno == jit_regno(iter->u.w)) {
3544 if (iter->flag || iter->v.w != word)
3547 del_node(prev, iter);
3552 spec = jit_classify(iter->code);
3553 if (spec & jit_cc_a0_jmp)
3555 if ((spec & (jit_cc_a0_reg|jit_cc_a0_chg)) ==
3556 (jit_cc_a0_reg|jit_cc_a0_chg)) {
3557 if (spec & jit_cc_a0_rlh) {
3558 if (regno == jit_regno(iter->u.q.l) ||
3559 regno == jit_regno(iter->u.q.h))
3563 if (regno == jit_regno(iter->u.w))
3567 if ((spec & (jit_cc_a1_reg|jit_cc_a1_chg)) ==
3568 (jit_cc_a1_reg|jit_cc_a1_chg)) {
3569 if (regno == jit_regno(iter->v.w))
3572 if ((spec & (jit_cc_a2_reg|jit_cc_a2_chg)) ==
3573 (jit_cc_a2_reg|jit_cc_a2_chg)) {
3574 if (regno == jit_regno(iter->w.w))
3585 _simplify_movr(jit_state_t *_jit, jit_node_t *prev, jit_node_t *node,
3586 jit_int32_t kind, jit_int32_t size)
3593 regno = jit_regno(node->u.w);
3594 right = jit_regno(node->v.w);
3595 value = _jitc->values + regno;
3597 spec = jit_class(_rvs[regno].spec);
3598 if (!(spec & (jit_class_gpr | jit_class_fpr))) {
3599 /* reserved register */
3601 ++_jitc->gen[regno];
3605 if ((value->kind == jit_kind_register &&
3606 jit_regno(value->base.q.l) == right &&
3607 value->base.q.h == _jitc->gen[right]) ||
3608 (value->kind == kind && _jitc->values[right].kind == kind &&
3609 memcmp(&value->base.w, &_jitc->values[right].base.w, size) == 0)) {
3610 del_node(prev, node);
3613 if (_jitc->values[right].kind == jit_kind_word)
3614 jit_memcpy(value, _jitc->values + right, sizeof(jit_value_t));
3616 value->kind = jit_kind_register;
3617 value->base.q.l = right;
3618 value->base.q.h = _jitc->gen[right];
3620 ++_jitc->gen[regno];
3626 _simplify_movi(jit_state_t *_jit, jit_node_t *prev, jit_node_t *node,
3627 jit_int32_t kind, jit_int32_t size)
3634 regno = jit_regno(node->u.w);
3635 value = _jitc->values + regno;
3636 if (node->flag & jit_flag_node) {
3637 /* set to undefined if value will be patched */
3639 ++_jitc->gen[regno];
3642 spec = jit_class(_rvs[regno].spec);
3643 if (!(spec & (jit_class_gpr | jit_class_fpr))) {
3644 /* reserved register */
3646 ++_jitc->gen[regno];
3649 if (value->kind == kind) {
3650 if (memcmp(&node->v.w, &value->base.w, size) == 0) {
3651 del_node(prev, node);
3654 if (kind == jit_kind_word)
3655 spec &= jit_class_gpr;
3657 spec &= (jit_class_xpr | jit_class_fpr);
3658 for (offset = 0; offset < _jitc->reglen; offset++) {
3659 if (_jitc->values[offset].kind == kind &&
3660 memcmp(&node->v.w, &_jitc->values[offset].base.w, size) == 0 &&
3661 (jit_class(_rvs[offset].spec) & spec) == spec) {
3662 if (kind == jit_kind_word)
3663 node->code = jit_code_movr;
3664 else if (kind == jit_kind_float32)
3665 node->code = jit_code_movr_f;
3667 node->code = jit_code_movr_d;
3669 jit_memcpy(value, _jitc->values + offset, sizeof(jit_value_t));
3670 ++_jitc->gen[regno];
3676 jit_memcpy(&value->base.w, &node->v.w, size);
3677 ++_jitc->gen[regno];
3682 /* simple/safe redundandy test not checking if another register
3683 * holds the same value
3686 _simplify_ldxi(jit_state_t *_jit, jit_node_t *prev, jit_node_t *node)
3692 regno = jit_regno(node->u.w);
3693 right = jit_regno(node->v.w);
3694 value = _jitc->values + regno;
3695 if (regno != right &&
3696 value->kind == jit_kind_code && value->code == node->code &&
3697 value->base.q.l == right && value->base.q.h == _jitc->gen[right] &&
3698 node->w.w == value->disp.w) {
3699 del_node(prev, node);
3702 value->kind = jit_kind_code;
3703 value->code = node->code;
3704 value->base.q.l = right;
3705 value->base.q.h = _jitc->gen[right];
3706 value->disp.w = node->w.w;
3707 ++_jitc->gen[regno];
3713 _simplify_stxi(jit_state_t *_jit, jit_node_t *prev, jit_node_t *node)
3720 regno = jit_regno(node->w.w);
3721 right = jit_regno(node->v.w);
3722 value = _jitc->values + regno;
3724 /* check for redundant store after load */
3725 if (regno != right &&
3726 value->kind == jit_kind_code && value->code == node->code &&
3727 value->base.q.l == right && value->base.q.h == _jitc->gen[right] &&
3728 node->u.w == value->disp.w) {
3729 del_node(prev, node);
3733 /* assume anything can alias, and invalidate tracked values */
3734 for (offset = 0; offset < _jitc->reglen; offset++) {
3735 if (_jitc->values[offset].kind == jit_kind_code) {
3736 _jitc->values[offset].kind = 0;
3737 ++_jitc->gen[offset];
3741 /* no multiple information, so, if set to a constant,
3742 * prefer to keep that information */
3743 if (value->kind == 0) {
3744 switch (node->code) {
3745 /* no information about signed/unsigned either */
3746 case jit_code_stxi_c: value->code = jit_code_ldxi_c; break;
3747 case jit_code_stxi_s: value->code = jit_code_ldxi_s; break;
3748 case jit_code_stxi_i: value->code = jit_code_ldxi_i; break;
3749 case jit_code_stxi_l: value->code = jit_code_ldxi_l; break;
3750 case jit_code_stxi_f: value->code = jit_code_ldxi_f; break;
3751 case jit_code_stxi_d: value->code = jit_code_ldxi_d; break;
3754 value->kind = jit_kind_code;
3755 value->base.q.l = right;
3756 value->base.q.h = _jitc->gen[right];
3757 value->disp.w = node->u.w;
3763 /* usually there should be only one store in the
3764 * jit_get_reg/jit_unget_reg, but properly handle
3765 * multiple ones by moving the save node */
3767 _simplify_spill(jit_state_t *_jit, jit_node_t *node, jit_int32_t regno)
3772 if ((temp = _jitc->spill[regno]) && (save = temp->next) != node) {
3773 temp->next = save->next;
3774 save->next = node->next;
3776 _jitc->spill[regno] = node;
3780 /* checks for simple cases where a register is set more than
3781 * once to the same value, and is a common pattern of calls
3782 * to jit_pushargi and jit_pushargr
3785 _simplify(jit_state_t *_jit)
3795 for (prev = NULL, node = _jitc->head; node; prev = node, node = next) {
3797 switch (node->code) {
3798 case jit_code_label: case jit_code_prolog:
3799 case jit_code_callr: case jit_code_calli:
3801 memset(_jitc->gen, 0, sizeof(jit_int32_t) * _jitc->reglen);
3802 memset(_jitc->values, 0, sizeof(jit_value_t) * _jitc->reglen);
3805 _jitc->spill[jit_regno(node->u.w)] = prev;
3808 regno = jit_regno(node->u.w);
3809 if (register_change_p(node->link->next, node, regno) !=
3811 /* spill not required due to optimizing common
3812 * redundancy case of calling jit_get_reg/jit_unget_reg
3813 * and then setting the register to the value it is
3814 * already holding */
3815 patch_register(node->link->next, node,
3816 jit_regno_patch|regno, regno);
3818 del_node(_jitc->spill[regno], node->link);
3819 del_node(prev, node);
3822 _jitc->spill[regno] = NULL;
3825 regno = jit_regno(node->u.w);
3826 if (simplify_movr(prev, node,
3827 jit_kind_word, sizeof(jit_word_t))) {
3829 simplify_spill(node = prev, regno);
3833 regno = jit_regno(node->u.w);
3834 if (simplify_movi(prev, node,
3835 jit_kind_word, sizeof(jit_word_t))) {
3837 simplify_spill(node = prev, regno);
3840 case jit_code_movr_f:
3841 regno = jit_regno(node->u.w);
3842 if (simplify_movr(prev, node,
3843 jit_kind_float32, sizeof(jit_float32_t))) {
3845 simplify_spill(node = prev, regno);
3848 case jit_code_movi_f:
3849 regno = jit_regno(node->u.w);
3850 if (simplify_movi(prev, node,
3851 jit_kind_float32, sizeof(jit_float32_t))) {
3853 simplify_spill(node = prev, regno);
3856 case jit_code_movr_d:
3857 regno = jit_regno(node->u.w);
3858 if (simplify_movr(prev, node,
3859 jit_kind_float64, sizeof(jit_float64_t))) {
3861 simplify_spill(node = prev, regno);
3864 case jit_code_movi_d:
3865 regno = jit_regno(node->u.w);
3866 if (simplify_movi(prev, node,
3867 jit_kind_float64, sizeof(jit_float64_t))) {
3869 simplify_spill(node = prev, regno);
3872 case jit_code_ldxi_c: case jit_code_ldxi_uc:
3873 case jit_code_ldxi_s: case jit_code_ldxi_us:
3874 case jit_code_ldxi_i: case jit_code_ldxi_ui:
3875 case jit_code_ldxi_l:
3876 case jit_code_ldxi_f: case jit_code_ldxi_d:
3877 regno = jit_regno(node->u.w);
3878 if (simplify_ldxi(prev, node)) {
3880 simplify_spill(node = prev, regno);
3883 case jit_code_stxi_c: case jit_code_stxi_s:
3884 case jit_code_stxi_i: case jit_code_stxi_l:
3885 case jit_code_stxi_f: case jit_code_stxi_d:
3886 regno = jit_regno(node->u.w);
3887 if (simplify_stxi(prev, node)) {
3889 simplify_spill(node = prev, regno);
3893 info = jit_classify(node->code);
3894 if (info & jit_cc_a0_jmp)
3895 /* labels are not implicitly added when not taking
3896 * a conditional branch */
3898 if (info & jit_cc_a0_chg) {
3899 if (info & jit_cc_a0_rlh) {
3900 regno = jit_regno(node->u.q.l);
3901 _jitc->values[regno].kind = 0;
3902 ++_jitc->gen[regno];
3903 regno = jit_regno(node->u.q.h);
3904 _jitc->values[regno].kind = 0;
3905 ++_jitc->gen[regno];
3908 regno = jit_regno(node->u.w);
3909 _jitc->values[regno].kind = 0;
3910 ++_jitc->gen[regno];
3913 if (info & jit_cc_a1_chg) {
3915 /* Assume registers are not changed */
3916 if (info & jit_cc_a1_rlh) {
3917 regno = jit_regno(node->v.q.l);
3918 _jitc->values[regno].kind = 0;
3919 ++_jitc->gen[regno];
3920 regno = jit_regno(node->v.q.h);
3921 _jitc->values[regno].kind = 0;
3922 ++_jitc->gen[regno];
3926 regno = jit_regno(node->v.w);
3927 _jitc->values[regno].kind = 0;
3928 ++_jitc->gen[regno];
3933 if (info & jit_cc_a2_chg) {
3935 /* Assume registers are not changed */
3936 if (info & jit_cc_a2_rlh) {
3937 regno = jit_regno(node->w.q.l);
3938 _jitc->values[regno].kind = 0;
3939 ++_jitc->gen[regno];
3940 regno = jit_regno(node->w.q.h);
3941 _jitc->values[regno].kind = 0;
3942 ++_jitc->gen[regno];
3946 regno = jit_regno(node->w.w);
3947 _jitc->values[regno].kind = 0;
3948 ++_jitc->gen[regno];
3960 _register_change_p(jit_state_t *_jit, jit_node_t *node, jit_node_t *link,
3964 jit_bool_t use, change;
3966 for (; node != link; node = node->next) {
3967 switch (node->code) {
3968 case jit_code_label: case jit_code_prolog:
3969 /* lack of extra information so assume it is live */
3970 return (jit_reg_static);
3971 case jit_code_callr: case jit_code_calli:
3972 if (!(jit_class(_rvs[regno].spec) & jit_class_sav))
3973 return (jit_reg_undef);
3976 value = jit_classify(node->code);
3978 if (value & jit_cc_a0_rlh) {
3979 if (node->u.q.l == regno || node->u.q.h == regno) {
3980 if (value & jit_cc_a0_chg)
3985 else if ((value & jit_cc_a0_reg) && node->u.w == regno) {
3986 if (value & jit_cc_a0_chg)
3991 (value & jit_cc_a1_reg) && node->v.w == regno) {
3992 if (value & jit_cc_a1_chg)
3997 (value & jit_cc_a2_reg) && node->w.w == regno) {
3998 if (value & jit_cc_a2_chg)
4002 /* lack of extra information */
4003 if (!use && (value & (jit_cc_a0_jmp|jit_cc_a0_cnd)) &&
4004 /* In case of indirect branches, always consider
4005 * callee save registers as live. */
4006 !(jit_class(_rvs[regno].spec) & jit_class_sav))
4009 return (jit_reg_change);
4013 return (jit_reg_static);
4016 /* most of this could be done at the same time as generating jit, but
4017 * avoid complications on different cpu backends and patch spill/loads
4018 * here, by simulating jit generation */
4020 _spill_reglive_p(jit_state_t *_jit, jit_node_t *node, jit_int32_t regno)
4022 if (!jit_regset_tstbit(&_jitc->reglive, regno)) {
4023 jit_regset_setbit(&_jitc->regmask, regno);
4024 jit_update(node->next, &_jitc->reglive, &_jitc->regmask);
4025 if (!jit_regset_tstbit(&_jitc->reglive, regno) &&
4026 register_change_p(node->next, node->link, regno) != jit_reg_change)
4034 _patch_registers(jit_state_t *_jit)
4044 _jitc->function = NULL;
4046 jit_reglive_setup();
4047 for (prev = NULL, node = _jitc->head; node; node = next) {
4050 info = jit_classify(node->code);
4051 jit_regarg_set(node, info);
4053 switch (node->code) {
4055 regno = jit_regno(node->u.w);
4056 if (!spill_reglive_p(node, regno)) {
4057 /* register is not live, just remove spill/reload */
4058 jit_regarg_clr(node, info);
4059 node->link->v.w = jit_regload_delete;
4060 del_node(prev, node);
4064 /* try to find a free register of the same class */
4065 spec = jit_class(_rvs[regno].spec) & ~jit_class_arg;
4066 for (value = 0; value < _jitc->reglen; value++) {
4067 if (value != regno &&
4068 ((jit_class(_rvs[value].spec) & spec) &
4069 ~jit_class_arg) == spec &&
4070 !jit_regset_tstbit(&_jitc->regarg, value) &&
4071 !spill_reglive_p(node, value))
4074 if (value < _jitc->reglen) {
4075 jit_regarg_clr(node, info);
4076 patch_register(node->next, node->link,
4077 jit_regno_patch|node->u.w,
4078 jit_regno_patch|value);
4079 /* mark as live just in case there are nested
4080 * register patches, so that next patch will
4081 * not want to use the same register */
4082 jit_regset_setbit(&_jitc->reglive, value);
4083 /* register is not live, just remove spill/reload */
4084 node->link->v.w = jit_regload_isdead;
4085 del_node(prev, node);
4089 /* failed to find a free register */
4090 if (spec & jit_class_gpr) {
4091 if (!_jitc->function->regoff[regno])
4092 _jitc->function->regoff[regno] =
4093 jit_allocai(sizeof(jit_word_t));
4094 #if __WORDSIZE == 32
4095 node->code = jit_code_stxi_i;
4097 node->code = jit_code_stxi_l;
4101 node->code = jit_code_stxi_d;
4102 if (!_jitc->function->regoff[regno])
4103 _jitc->function->regoff[regno] =
4104 jit_allocai(sizeof(jit_float64_t));
4106 node->u.w = _jitc->function->regoff[regno];
4114 regno = jit_regno(node->u.w);
4116 if (node->v.w == jit_regload_isdead)
4117 jit_regset_clrbit(&_jitc->reglive, regno);
4118 del_node(prev, node);
4121 spec = jit_class(_rvs[regno].spec);
4122 if (spec & jit_class_gpr) {
4123 #if __WORDSIZE == 32
4124 node->code = jit_code_ldxi_i;
4126 node->code = jit_code_ldxi_l;
4130 node->code = jit_code_ldxi_d;
4133 node->w.w = _jitc->function->regoff[regno];
4136 case jit_code_prolog:
4137 _jitc->function = _jitc->functions.ptr + node->w.w;
4139 case jit_code_epilog:
4140 _jitc->function = NULL;
4146 jit_regarg_clr(node, info);
4147 /* update register live state */
4154 _patch_register(jit_state_t *_jit, jit_node_t *node, jit_node_t *link,
4155 jit_int32_t regno, jit_int32_t patch)
4159 for (; node != link; node = node->next) {
4160 value = jit_classify(node->code);
4161 if (value & jit_cc_a0_reg) {
4162 if (value & jit_cc_a0_rlh) {
4163 if (node->u.q.l == regno)
4164 node->u.q.l = patch;
4165 if (node->u.q.h == regno)
4166 node->u.q.h = patch;
4169 if (node->u.w == regno)
4173 if (value & jit_cc_a1_reg) {
4174 if (value & jit_cc_a1_rlh) {
4175 if (node->v.q.l == regno)
4176 node->v.q.l = patch;
4177 if (node->v.q.h == regno)
4178 node->v.q.h = patch;
4181 if (node->v.w == regno)
4185 if (value & jit_cc_a2_reg) {
4186 if (value & jit_cc_a2_rlh) {
4187 if (node->w.q.l == regno)
4188 node->w.q.l = patch;
4189 if (node->w.q.h == regno)
4190 node->w.q.h = patch;
4193 if (node->w.w == regno)
4200 #if __BYTE_ORDER == __LITTLE_ENDIAN
4201 # define htonr_us(r0,r1) bswapr_us(r0,r1)
4202 # define htonr_ui(r0,r1) bswapr_ui(r0,r1)
4203 # if __WORDSIZE == 64
4204 # define htonr_ul(r0,r1) bswapr_ul(r0,r1)
4207 # define htonr_us(r0,r1) extr_us(r0,r1)
4208 # if __WORDSIZE == 32
4209 # define htonr_ui(r0,r1) movr(r0,r1)
4211 # define htonr_ui(r0,r1) extr_ui(r0,r1)
4212 # define htonr_ul(r0,r1) movr(r0,r1)
4216 static maybe_unused void
4217 generic_bswapr_us(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1);
4218 static maybe_unused void
4219 generic_bswapr_ui(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1);
4220 #if __WORDSIZE == 64
4221 static maybe_unused void
4222 generic_bswapr_ul(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1);
4225 #define depi(r0, i0, i1, i2) _depi(_jit, r0, i0, i1, i2)
4226 static void _depi(jit_state_t*,jit_int32_t, jit_word_t, jit_word_t, jit_word_t);
4227 #define negi(r0, i0) _negi(_jit, r0, i0)
4228 static void _negi(jit_state_t*, jit_int32_t, jit_word_t);
4229 #define comi(r0, i0) _comi(_jit, r0, i0)
4230 static void _comi(jit_state_t*, jit_int32_t, jit_word_t);
4231 #define exti_c(r0, i0) _exti_c(_jit, r0, i0)
4232 static void _exti_c(jit_state_t*, jit_int32_t, jit_word_t);
4233 #define exti_uc(r0, i0) _exti_uc(_jit, r0, i0)
4234 static void _exti_uc(jit_state_t*, jit_int32_t, jit_word_t);
4235 #define exti_s(r0, i0) _exti_s(_jit, r0, i0)
4236 static void _exti_s(jit_state_t*, jit_int32_t, jit_word_t);
4237 #define exti_us(r0, i0) _exti_us(_jit, r0, i0)
4238 static void _exti_us(jit_state_t*, jit_int32_t, jit_word_t);
4239 #if __WORDSIZE == 64
4240 #define exti_i(r0, i0) _exti_i(_jit, r0, i0)
4241 static void _exti_i(jit_state_t*, jit_int32_t, jit_word_t);
4242 #define exti_ui(r0, i0) _exti_ui(_jit, r0, i0)
4243 static void _exti_ui(jit_state_t*, jit_int32_t, jit_word_t);
4245 #define bswapi_us(r0, i0) _bswapi_us(_jit, r0, i0)
4246 static void _bswapi_us(jit_state_t*, jit_int32_t, jit_word_t);
4247 #define bswapi_ui(r0, i0) _bswapi_ui(_jit, r0, i0)
4248 static void _bswapi_ui(jit_state_t*, jit_int32_t, jit_word_t);
4249 #if __WORDSIZE == 64
4250 # define bswapi_ul(r0, i0) _bswapi_ul(_jit, r0, i0)
4251 static void _bswapi_ul(jit_state_t*, jit_int32_t, jit_word_t);
4253 #define htoni_us(r0, i0) _htoni_us(_jit, r0, i0)
4254 static void _htoni_us(jit_state_t*, jit_int32_t, jit_word_t);
4255 #define htoni_ui(r0, i0) _htoni_ui(_jit, r0, i0)
4256 static void _htoni_ui(jit_state_t*, jit_int32_t, jit_word_t);
4257 #if __WORDSIZE == 64
4258 # define htoni_ul(r0, i0) _htoni_ul(_jit, r0, i0)
4259 static void _htoni_ul(jit_state_t*, jit_int32_t, jit_word_t);
4261 # define movi_f_w(r0, i0) _movi_f_w(_jit, r0, i0)
4262 static void _movi_f_w(jit_state_t*, jit_int32_t, jit_float32_t);
4263 #if __WORDSIZE == 32
4264 # define movi_d_ww(r0, r1, i0) _movi_d_ww(_jit, r0, r1, i0)
4265 static void _movi_d_ww(jit_state_t*, jit_int32_t, jit_int32_t, jit_float64_t);
4267 # define movi_d_w(r0, i0) _movi_d_w(_jit, r0, i0)
4268 static void _movi_d_w(jit_state_t*, jit_int32_t, jit_float64_t);
4270 #define cloi(r0, i0) _cloi(_jit, r0, i0)
4271 static void _cloi(jit_state_t*, jit_int32_t, jit_word_t);
4272 #define clzi(r0, i0) _clzi(_jit, r0, i0)
4273 static void _clzi(jit_state_t*, jit_int32_t, jit_word_t);
4274 #define ctoi(r0, i0) _ctoi(_jit, r0, i0)
4275 static void _ctoi(jit_state_t*, jit_int32_t, jit_word_t);
4276 #define ctzi(r0, i0) _ctzi(_jit, r0, i0)
4277 static void _ctzi(jit_state_t*, jit_int32_t, jit_word_t);
4278 #define rbiti(r0, i0) _rbiti(_jit, r0, i0)
4279 static void _rbiti(jit_state_t*, jit_int32_t, jit_word_t);
4280 #define popcnti(r0, i0) _popcnti(_jit, r0, i0)
4281 static void _popcnti(jit_state_t*, jit_int32_t, jit_word_t);
4282 #define exti(r0, i0, i1, i2) _exti(_jit, r0, i0, i1, i2)
4283 static void _exti(jit_state_t*,jit_int32_t, jit_word_t, jit_word_t, jit_word_t);
4284 #define exti_u(r0, i0, i1, i2) _exti_u(_jit, r0, i0, i1, i2)
4285 static void _exti_u(jit_state_t*,
4286 jit_int32_t, jit_word_t, jit_word_t, jit_word_t);
4287 #define generic_unldr(r0, r1, i0) _generic_unldr(_jit, r0, r1, i0)
4288 static void _generic_unldr(jit_state_t*, jit_int32_t, jit_int32_t, jit_word_t);
4289 #define generic_unldi(r0, i0, i1) _generic_unldi(_jit, r0, i0, i1)
4290 static void _generic_unldi(jit_state_t*, jit_int32_t, jit_word_t, jit_word_t);
4291 #define generic_unldr_u(r0, r1, i0) _generic_unldr_u(_jit, r0, r1, i0)
4292 static void _generic_unldr_u(jit_state_t*,
4293 jit_int32_t, jit_int32_t, jit_word_t);
4294 #define generic_unldi_u(r0, i0, i1) _generic_unldi_u(_jit, r0, i0, i1)
4295 static void _generic_unldi_u(jit_state_t*, jit_int32_t, jit_word_t, jit_word_t);
4296 #define generic_unstr(r0, r1, i0) _generic_unstr(_jit, r0, r1, i0)
4297 static void _generic_unstr(jit_state_t*, jit_int32_t, jit_int32_t, jit_word_t);
4298 #define generic_unsti(i0, r0, i1) _generic_unsti(_jit, i0, r0, i1)
4299 static void _generic_unsti(jit_state_t*, jit_word_t, jit_int32_t, jit_word_t);
4300 #if !defined(__i386__) && !defined(__x86_64__)
4301 # define generic_unldr_x(r0, r1, i0) _generic_unldr_x(_jit, r0, r1, i0)
4302 static void _generic_unldr_x(jit_state_t*,
4303 jit_int32_t, jit_int32_t, jit_word_t);
4304 # define generic_unldi_x(r0, i0, i1) _generic_unldi_x(_jit, r0, i0, i1)
4305 static void _generic_unldi_x(jit_state_t*, jit_int32_t, jit_word_t, jit_word_t);
4306 # define generic_unstr_x(r0, r1, i0) _generic_unstr_x(_jit, r0, r1, i0)
4307 static void _generic_unstr_x(jit_state_t*,
4308 jit_int32_t, jit_int32_t, jit_word_t);
4309 # define generic_unsti_x(i0, r0, i1) _generic_unsti_x(_jit, i0, r0, i1)
4310 static void _generic_unsti_x(jit_state_t*, jit_word_t, jit_int32_t, jit_word_t);
4312 #define patch_alist(revert) _patch_alist(_jit, revert)
4313 static maybe_unused void _patch_alist(jit_state_t *_jit, jit_bool_t revert);
4315 #if defined(__i386__) || defined(__x86_64__)
4316 # include "jit_x86.c"
4317 #elif defined(__mips__)
4318 # include "jit_mips.c"
4319 #elif defined(__arm__)
4320 # include "jit_arm.c"
4321 #elif defined(__powerpc__)
4322 # include "jit_ppc.c"
4323 #elif defined(__sparc__)
4324 # include "jit_sparc.c"
4325 #elif defined(__ia64__)
4326 # include "jit_ia64.c"
4327 #elif defined(__hppa__)
4328 # include "jit_hppa.c"
4329 #elif defined(__aarch64__)
4330 # include "jit_aarch64.c"
4331 #elif defined(__s390__) || defined(__s390x__)
4332 # include "jit_s390.c"
4333 #elif defined(__alpha__)
4334 # include "jit_alpha.c"
4335 #elif defined(__riscv)
4336 # include "jit_riscv.c"
4337 #elif defined(__loongarch__)
4338 # include "jit_loongarch.c"
4341 static maybe_unused void
4342 generic_bswapr_us(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1)
4344 jit_int32_t reg = jit_get_reg(jit_class_gpr);
4346 rshi(rn(reg), r1, 8);
4348 andi(rn(reg), rn(reg), 0xff);
4350 orr(r0, r0, rn(reg));
4355 static maybe_unused void
4356 generic_bswapr_ui(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1)
4358 jit_int32_t reg = jit_get_reg(jit_class_gpr);
4360 rshi(rn(reg), r1, 16);
4362 bswapr_us(rn(reg), rn(reg));
4364 orr(r0, r0, rn(reg));
4369 #if __WORDSIZE == 64
4370 static maybe_unused void
4371 generic_bswapr_ul(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1)
4373 jit_int32_t reg = jit_get_reg(jit_class_gpr);
4375 rshi_u(rn(reg), r1, 32);
4377 bswapr_ui(rn(reg), rn(reg));
4379 orr(r0, r0, rn(reg));
4386 _depi(jit_state_t *_jit,
4387 jit_int32_t r0, jit_word_t i0, jit_word_t i1, jit_word_t i2)
4390 reg = jit_get_reg(jit_class_gpr);
4392 depr(r0, rn(reg), i1, i2);
4397 _negi(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
4403 _comi(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
4409 _exti_c(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
4411 movi(r0, (jit_int8_t)i0);
4415 _exti_uc(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
4417 movi(r0, (jit_uint8_t)i0);
4421 _exti_s(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
4423 movi(r0, (jit_int16_t)i0);
4427 _exti_us(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
4429 movi(r0, (jit_uint16_t)i0);
4432 #if __WORDSIZE == 64
4434 _exti_i(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
4436 movi(r0, (jit_int32_t)i0);
4440 _exti_ui(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
4442 movi(r0, (jit_uint32_t)i0);
4447 _bswapi_us(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
4460 _bswapi_ui(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
4474 #if __WORDSIZE == 64
4476 _bswapi_ul(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
4496 _htoni_us(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
4498 #if __BYTE_ORDER == __LITTLE_ENDIAN
4506 _htoni_ui(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
4508 #if __BYTE_ORDER == __LITTLE_ENDIAN
4511 # if __WORDSIZE == 32
4519 #if __WORDSIZE == 64
4521 _htoni_ul(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
4523 # if __BYTE_ORDER == __LITTLE_ENDIAN
4532 _movi_f_w(jit_state_t *_jit, jit_int32_t r0, jit_float32_t i0)
4539 #if defined(__ia64__)
4540 /* Should be used only in this case (with out0 == 120) */
4542 r0 = _jitc->rout + (r0 - 120);
4547 #if __WORDSIZE == 32
4549 _movi_d_ww(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_float64_t i0)
4556 /* Mips does not change byte order of double values */
4557 # if __BYTE_ORDER == __LITTLE_ENDIAN || defined(__mips__)
4558 movi(r0, data.i[0]);
4559 movi(r1, data.i[1]);
4561 movi(r1, data.i[0]);
4562 movi(r0, data.i[1]);
4568 _movi_d_w(jit_state_t *_jit, jit_int32_t r0, jit_float64_t i0)
4575 # if defined(__ia64__)
4576 /* Should be used only in this case (with out0 == 120) */
4578 r0 = _jitc->rout + (r0 - 120);
4585 _jit_negi_f(jit_state_t *_jit, jit_fpr_t u, jit_float32_t v)
4587 jit_inc_synth_wf(negi_f, u, v);
4594 _jit_absi_f(jit_state_t *_jit, jit_fpr_t u, jit_float32_t v)
4596 jit_inc_synth_wf(absi_f, u, v);
4603 _jit_sqrti_f(jit_state_t *_jit, jit_fpr_t u, jit_float32_t v)
4605 jit_inc_synth_wf(sqrti_f, u, v);
4612 _jit_fmai_f(jit_state_t *_jit,
4613 jit_fpr_t u, jit_fpr_t v, jit_fpr_t w, jit_float32_t x)
4616 jit_inc_synth_wqf(fmai_f, u, v, w, x);
4617 if (u != v && u != w) {
4619 jit_fmar_f(u, v, w, u);
4622 y = jit_get_reg(jit_class_fpr);
4624 jit_fmar_f(u, v, w, y);
4631 _jit_fmsi_f(jit_state_t *_jit,
4632 jit_fpr_t u, jit_fpr_t v, jit_fpr_t w, jit_float32_t x)
4635 jit_inc_synth_wqf(fmai_f, u, v, w, x);
4636 if (u != v && u != w) {
4638 jit_fmsr_f(u, v, w, u);
4641 y = jit_get_reg(jit_class_fpr);
4643 jit_fmsr_f(u, v, w, y);
4650 _jit_fnmai_f(jit_state_t *_jit,
4651 jit_fpr_t u, jit_fpr_t v, jit_fpr_t w, jit_float32_t x)
4654 jit_inc_synth_wqf(fmai_f, u, v, w, x);
4655 if (u != v && u != w) {
4657 jit_fnmar_f(u, v, w, u);
4660 y = jit_get_reg(jit_class_fpr);
4662 jit_fnmar_f(u, v, w, y);
4669 _jit_fnmsi_f(jit_state_t *_jit,
4670 jit_fpr_t u, jit_fpr_t v, jit_fpr_t w, jit_float32_t x)
4673 jit_inc_synth_wqf(fmai_f, u, v, w, x);
4674 if (u != v && u != w) {
4676 jit_fnmsr_f(u, v, w, u);
4679 y = jit_get_reg(jit_class_fpr);
4681 jit_fnmsr_f(u, v, w, y);
4688 _jit_negi_d(jit_state_t *_jit, jit_fpr_t u, jit_float64_t v)
4690 jit_inc_synth_wd(negi_d, u, v);
4697 _jit_absi_d(jit_state_t *_jit, jit_fpr_t u, jit_float64_t v)
4699 jit_inc_synth_wd(absi_d, u, v);
4706 _jit_sqrti_d(jit_state_t *_jit, jit_fpr_t u, jit_float64_t v)
4708 jit_inc_synth_wd(sqrti_d, u, v);
4715 _jit_fmai_d(jit_state_t *_jit,
4716 jit_fpr_t u, jit_fpr_t v, jit_fpr_t w, jit_float64_t x)
4719 jit_inc_synth_wqd(fmai_d, u, v, w, x);
4720 if (u != v && u != w) {
4722 jit_fmar_d(u, v, w, u);
4725 y = jit_get_reg(jit_class_fpr);
4727 jit_fmar_d(u, v, w, y);
4734 _jit_fmsi_d(jit_state_t *_jit,
4735 jit_fpr_t u, jit_fpr_t v, jit_fpr_t w, jit_float64_t x)
4738 jit_inc_synth_wqd(fmai_d, u, v, w, x);
4739 if (u != v && u != w) {
4741 jit_fmsr_d(u, v, w, u);
4744 y = jit_get_reg(jit_class_fpr);
4746 jit_fmsr_d(u, v, w, y);
4753 _jit_fnmai_d(jit_state_t *_jit,
4754 jit_fpr_t u, jit_fpr_t v, jit_fpr_t w, jit_float64_t x)
4757 jit_inc_synth_wqd(fmai_d, u, v, w, x);
4758 if (u != v && u != w) {
4760 jit_fnmar_d(u, v, w, u);
4763 y = jit_get_reg(jit_class_fpr);
4765 jit_fnmar_d(u, v, w, y);
4772 _jit_fnmsi_d(jit_state_t *_jit,
4773 jit_fpr_t u, jit_fpr_t v, jit_fpr_t w, jit_float64_t x)
4776 jit_inc_synth_wqd(fmai_d, u, v, w, x);
4777 if (u != v && u != w) {
4779 jit_fnmsr_d(u, v, w, u);
4782 y = jit_get_reg(jit_class_fpr);
4784 jit_fnmsr_d(u, v, w, y);
4791 _cloi(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
4797 _clzi(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
4799 #if __WORDSIZE == 64 && _WIN32
4800 movi(r0, (i0) ? __builtin_clzll(i0) : __WORDSIZE);
4802 movi(r0, (i0) ? __builtin_clzl(i0) : __WORDSIZE);
4807 _ctoi(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
4813 _ctzi(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
4815 #if __WORDSIZE == 64 && _WIN32
4816 movi(r0, (i0) ? __builtin_ctzll(i0) : __WORDSIZE);
4818 movi(r0, (i0) ? __builtin_ctzl(i0) : __WORDSIZE);
4823 _rbiti(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
4828 jit_uint8_t v[__WORDSIZE >> 3];
4830 static const unsigned char swap_tab[256] = {
4831 0, 128, 64, 192, 32, 160, 96, 224,
4832 16, 144, 80, 208, 48, 176, 112, 240,
4833 8, 136, 72, 200, 40, 168, 104, 232,
4834 24, 152, 88, 216 ,56, 184, 120, 248,
4835 4, 132, 68, 196, 36, 164, 100, 228,
4836 20, 148, 84, 212, 52, 180, 116, 244,
4837 12, 140, 76, 204, 44, 172, 108, 236,
4838 28, 156, 92, 220, 60, 188, 124, 252,
4839 2, 130, 66, 194, 34, 162, 98, 226,
4840 18, 146, 82, 210, 50, 178, 114, 242,
4841 10, 138, 74, 202, 42, 170, 106, 234,
4842 26, 154, 90, 218, 58, 186, 122, 250,
4843 6, 134, 70, 198, 38, 166, 102, 230,
4844 22, 150, 86, 214, 54, 182, 118, 246,
4845 14, 142, 78, 206, 46, 174, 110, 238,
4846 30, 158, 94, 222, 62, 190, 126, 254,
4847 1, 129, 65, 193, 33, 161, 97, 225,
4848 17, 145, 81, 209, 49, 177, 113, 241,
4849 9, 137, 73, 201, 41, 169, 105, 233,
4850 25, 153, 89, 217, 57, 185, 121, 249,
4851 5, 133, 69, 197, 37, 165, 101, 229,
4852 21, 149, 85, 213, 53, 181, 117, 245,
4853 13, 141, 77, 205, 45, 173, 109, 237,
4854 29, 157, 93, 221, 61, 189, 125, 253,
4855 3, 131, 67, 195, 35, 163, 99, 227,
4856 19, 147, 83, 211, 51, 179, 115, 243,
4857 11, 139, 75, 203, 43, 171, 107, 235,
4858 27, 155, 91, 219, 59, 187, 123, 251,
4859 7, 135, 71, 199, 39, 167, 103, 231,
4860 23, 151, 87, 215, 55, 183, 119, 247,
4861 15, 143, 79, 207, 47, 175, 111, 239,
4862 31, 159, 95, 223, 63, 191, 127, 255
4865 for (i = 0; i < sizeof(jit_word_t); ++i)
4866 v.v[i] = swap_tab[u.v[sizeof(jit_word_t) - i - 1]];
4871 _popcnti(jit_state_t *_jit, jit_int32_t r0, jit_word_t i0)
4873 #if __WORDSIZE == 64 && _WIN32
4874 movi(r0, (i0) ? __builtin_popcountll(i0) : __WORDSIZE);
4876 movi(r0, (i0) ? __builtin_popcountl(i0) : __WORDSIZE);
4880 static void _exti(jit_state_t *_jit,
4881 jit_int32_t r0, jit_word_t i0, jit_word_t i1, jit_word_t i2)
4883 #if __BYTE_ORDER == __BIG_ENDIAN
4884 i1 = __WORDSIZE - (i1 + i2);
4886 i0 <<= __WORDSIZE - (i1 + i2);
4887 i0 >>= __WORDSIZE - i2;
4891 static void _exti_u(jit_state_t *_jit,
4892 jit_int32_t r0, jit_word_t i0, jit_word_t i1, jit_word_t i2)
4895 #if __BYTE_ORDER == __BIG_ENDIAN
4896 i1 = __WORDSIZE - (i1 + i2);
4899 i0 >>= __WORDSIZE - i2;
4900 #if __WORDSIZE == 64 && !_WIN32
4901 i0 &= (1L << i2) - 1;
4903 i0 &= (1LL << i2) - 1;
4909 _generic_unldr(jit_state_t *_jit,
4910 jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
4913 assert(i0 >= 1 && i0 <= sizeof(jit_word_t));
4914 if (i0 & (i0 - 1)) {
4915 t0 = jit_get_reg(jit_class_gpr);
4927 #if __BYTE_ORDER == __LITTLE_ENDIAN
4937 #if __WORDSIZE == 32
4946 # if __BYTE_ORDER == __LITTLE_ENDIAN
4957 # if __BYTE_ORDER == __LITTLE_ENDIAN
4968 # if __BYTE_ORDER == __LITTLE_ENDIAN
4985 if (i0 & (i0 - 1)) {
4992 _generic_unldi(jit_state_t *_jit,
4993 jit_int32_t r0, jit_word_t i0, jit_word_t i1)
4996 assert(i1 >= 1 && i1 <= sizeof(jit_word_t));
4997 if (i1 & (i1 - 1)) {
4998 t0 = jit_get_reg(jit_class_gpr);
5009 # if __BYTE_ORDER == __LITTLE_ENDIAN
5019 # if __WORDSIZE == 32
5028 # if __BYTE_ORDER == __LITTLE_ENDIAN
5039 # if __BYTE_ORDER == __LITTLE_ENDIAN
5050 # if __BYTE_ORDER == __LITTLE_ENDIAN
5067 if (i1 & (i1 - 1)) {
5074 _generic_unldr_u(jit_state_t *_jit,
5075 jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
5078 assert(i0 >= 1 && i0 <= sizeof(jit_word_t));
5079 if (i0 & (i0 - 1)) {
5080 t0 = jit_get_reg(jit_class_gpr);
5092 # if __BYTE_ORDER == __LITTLE_ENDIAN
5102 # if __WORDSIZE == 32
5111 # if __BYTE_ORDER == __LITTLE_ENDIAN
5122 # if __BYTE_ORDER == __LITTLE_ENDIAN
5133 # if __BYTE_ORDER == __LITTLE_ENDIAN
5150 if (i0 & (i0 - 1)) {
5157 _generic_unldi_u(jit_state_t *_jit,
5158 jit_int32_t r0, jit_word_t i0, jit_word_t i1)
5161 assert(i1 >= 1 && i1 <= sizeof(jit_word_t));
5162 if (i1 & (i1 - 1)) {
5163 t0 = jit_get_reg(jit_class_gpr);
5174 # if __BYTE_ORDER == __LITTLE_ENDIAN
5184 # if __WORDSIZE == 32
5193 # if __BYTE_ORDER == __LITTLE_ENDIAN
5204 # if __BYTE_ORDER == __LITTLE_ENDIAN
5215 # if __BYTE_ORDER == __LITTLE_ENDIAN
5232 if (i1 & (i1 - 1)) {
5239 _generic_unstr(jit_state_t *_jit, jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
5242 assert(i0 > 0 && i0 <= sizeof(jit_word_t));
5243 if (i0 & (i0 - 1)) {
5244 t0 = jit_get_reg(jit_class_gpr);
5255 # if __BYTE_ORDER == __LITTLE_ENDIAN
5265 # if __WORDSIZE == 32
5274 # if __BYTE_ORDER == __LITTLE_ENDIAN
5285 # if __BYTE_ORDER == __LITTLE_ENDIAN
5296 # if __BYTE_ORDER == __LITTLE_ENDIAN
5320 _generic_unsti(jit_state_t *_jit, jit_word_t i0, jit_int32_t r0, jit_word_t i1)
5323 assert(i1 > 0 && i1 <= sizeof(jit_word_t));
5324 if (i1 & (i1 - 1)) {
5325 t0 = jit_get_reg(jit_class_gpr);
5336 # if __BYTE_ORDER == __LITTLE_ENDIAN
5346 # if __WORDSIZE == 32
5355 # if __BYTE_ORDER == __LITTLE_ENDIAN
5366 # if __BYTE_ORDER == __LITTLE_ENDIAN
5377 # if __BYTE_ORDER == __LITTLE_ENDIAN
5400 #if !defined(__i386__) && !defined(__x86_64__) && !defined(__arm__)
5402 _generic_unldr_x(jit_state_t *_jit,
5403 jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
5405 assert(i0 == 4 || i0 == 8);
5413 _generic_unldi_x(jit_state_t *_jit,
5414 jit_int32_t r0, jit_word_t i0, jit_word_t i1)
5416 assert(i1 == 4 || i1 == 8);
5424 _generic_unstr_x(jit_state_t *_jit,
5425 jit_int32_t r0, jit_int32_t r1, jit_word_t i0)
5427 assert(i0 == 4 || i0 == 8);
5435 _generic_unsti_x(jit_state_t *_jit,
5436 jit_word_t i0, jit_int32_t r0, jit_word_t i1)
5438 assert(i1 == 4 || i1 == 8);
5446 #if defined(stack_framesize)
5447 static maybe_unused void
5448 _patch_alist(jit_state_t *_jit, jit_bool_t revert)
5452 diff = jit_diffsize();
5456 for (node = _jitc->function->alist; node; node = node->link) {
5457 switch (node->code) {
5458 case jit_code_ldxi_c: case jit_code_ldxi_uc:
5459 case jit_code_ldxi_s: case jit_code_ldxi_us:
5460 case jit_code_ldxi_i:
5461 #if __WORDSIZE == 64
5462 case jit_code_ldxi_ui: case jit_code_ldxi_l:
5464 case jit_code_ldxi_f: case jit_code_ldxi_d:
5467 case jit_code_stxi_c: case jit_code_stxi_s:
5468 case jit_code_stxi_i:
5469 #if __WORDSIZE == 64
5470 case jit_code_stxi_l:
5472 case jit_code_stxi_f: case jit_code_stxi_d: