Merge pull request #724 from StormedBubbles/guncon_framing
[pcsx_rearmed.git] / deps / lightning / lib / jit_arm.c
index 783fa90..64a70f9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012-2019  Free Software Foundation, Inc.
+ * Copyright (C) 2012-2023  Free Software Foundation, Inc.
  *
  * This file is part of GNU lightning.
  *
@@ -21,6 +21,8 @@
 #  include <stdio.h>
 #endif
 
+#define stack_framesize                        48
+
 #define jit_arg_reg_p(i)               ((i) >= 0 && (i) < 4)
 #define jit_arg_f_reg_p(i)             ((i) >= 0 && (i) < 16)
 #define jit_arg_d_reg_p(i)             ((i) >= 0 && (i) < 15)
 #define arm_patch_node                 0x80000000
 #define arm_patch_word                 0x40000000
 #define arm_patch_jump                 0x20000000
-#define arm_patch_load                 0x00000000
+#define arm_patch_load                 0x10000000
+#define arm_patch_call                 0x08000000
 
 #define jit_fpr_p(rn)                  ((rn) > 15)
 
-#define arg_base()                                                     \
-    (stack_framesize - 16 + (jit_cpu.abi ? 64 : 0))
+#define arg_base()                     (stack_framesize - 16)
 #define arg_offset(n)                                                  \
     ((n) < 4 ? arg_base() + ((n) << 2) : (n))
 
  * arm mode, what may cause a crash upon return of that function
  * if generating jit for a relative jump.
  */
-#define jit_exchange_p()               1
+#define jit_exchange_p()               jit_cpu.exchange
 
 /* FIXME is it really required to not touch _R10? */
 
+#define CHECK_REG_ARGS()                                               \
+    do {                                                               \
+       if (!_jitc->function->save_reg_args)                            \
+           _jitc->again = _jitc->function->save_reg_args = 1;          \
+    } while (0)
+
+#define CHECK_SWF_OFFSET()                                             \
+    do {                                                               \
+       if (!_jitc->function->swf_offset) {                             \
+           _jitc->again = _jitc->function->save_reg_args =             \
+               _jitc->function->swf_offset = 1;                        \
+           _jitc->function->self.aoff = -64;                           \
+       }                                                               \
+    } while (0)
+
+#define CHECK_RETURN()                                                 \
+    do {                                                               \
+       if (!_jitc->function->need_frame &&                             \
+           !_jitc->function->need_return)                              \
+           _jitc->again = _jitc->function->need_return = 1;            \
+    } while (0)
+
 /*
  * Types
  */
@@ -59,8 +83,8 @@ typedef jit_pointer_t jit_va_list;
 /*
  * Prototypes
  */
-#define jit_make_arg(node)             _jit_make_arg(_jit,node)
-static jit_node_t *_jit_make_arg(jit_state_t*,jit_node_t*);
+#define jit_make_arg(node,code)                _jit_make_arg(_jit,node,code)
+static jit_node_t *_jit_make_arg(jit_state_t*,jit_node_t*,jit_code_t);
 #define jit_make_arg_f(node)           _jit_make_arg_f(_jit,node)
 static jit_node_t *_jit_make_arg_f(jit_state_t*,jit_node_t*);
 #define jit_make_arg_d(node)           _jit_make_arg_d(_jit,node)
@@ -77,8 +101,10 @@ static void _load_const(jit_state_t*,jit_bool_t,jit_int32_t,jit_word_t);
 static void _flush_consts(jit_state_t*);
 #define invalidate_consts()            _invalidate_consts(_jit)
 static void _invalidate_consts(jit_state_t*);
-#define patch(instr, node)             _patch(_jit, instr, node)
-static void _patch(jit_state_t*,jit_word_t,jit_node_t*);
+#define compute_framesize()            _compute_framesize(_jit)
+static void _compute_framesize(jit_state_t*);
+#define patch(instr, node, kind)       _patch(_jit, instr, node, kind)
+static void _patch(jit_state_t*,jit_word_t,jit_node_t*,jit_int32_t);
 
 #if defined(__GNUC__)
 /* libgcc */
@@ -90,6 +116,7 @@ extern void __clear_cache(void *, void *);
 #  include "jit_arm-cpu.c"
 #  include "jit_arm-swf.c"
 #  include "jit_arm-vfp.c"
+#  include "jit_fallback.c"
 #undef PROTO
 
 /*
@@ -148,6 +175,10 @@ jit_register_t             _rvs[] = {
     { _NOREG,                          "<none>" },
 };
 
+static jit_int32_t iregs[] = {
+    _R4, _R5, _R6, _R7, _R8, _R9,
+};
+
 /*
  * Implementation
  */
@@ -201,6 +232,14 @@ jit_get_cpu(void)
     /* armv6t2 todo (software float and thumb2) */
     if (!jit_cpu.vfp && jit_cpu.thumb)
        jit_cpu.thumb = 0;
+    /* FIXME need test environments for the below. For the moment just
+     * be very conservative */
+    /* force generation of code assuming jit and function libraries called
+     * instruction set do not match */
+    jit_cpu.exchange = 1;
+    /* do not generate hardware integer division by default */
+    if (jit_cpu.version == 7)
+       jit_cpu.extend = 0;
 }
 
 void
@@ -244,15 +283,10 @@ _jit_prolog(jit_state_t *_jit)
     }
     _jitc->function = _jitc->functions.ptr + _jitc->functions.offset++;
     _jitc->function->self.size = stack_framesize;
-    if (jit_cpu.abi)
-       _jitc->function->self.size += 64;
     _jitc->function->self.argi = _jitc->function->self.argf =
-       _jitc->function->self.alen = 0;
-    if (jit_swf_p())
-       /* 8 soft float registers */
-       _jitc->function->self.aoff = -64;
-    else
-       _jitc->function->self.aoff = 0;
+       _jitc->function->self.alen = _jitc->function->self.aoff = 0;
+    _jitc->function->swf_offset = _jitc->function->save_reg_args =
+       _jitc->function->need_return = 0;
     _jitc->function->self.call = jit_call_default;
     jit_alloc((jit_pointer_t *)&_jitc->function->regoff,
              _jitc->reglen * sizeof(jit_int32_t));
@@ -278,6 +312,9 @@ jit_int32_t
 _jit_allocai(jit_state_t *_jit, jit_int32_t length)
 {
     assert(_jitc->function);
+    if (jit_swf_p())
+       CHECK_SWF_OFFSET();
+    jit_check_frame();
     switch (length) {
        case 0: case 1:                                         break;
        case 2:         _jitc->function->self.aoff &= -2;       break;
@@ -326,20 +363,18 @@ _jit_ret(jit_state_t *_jit)
 }
 
 void
-_jit_retr(jit_state_t *_jit, jit_int32_t u)
+_jit_retr(jit_state_t *_jit, jit_int32_t u, jit_code_t code)
 {
-    jit_inc_synth_w(retr, u);
-    if (JIT_RET != u)
-       jit_movr(JIT_RET, u);
-    jit_live(JIT_RET);
+    jit_code_inc_synth_w(code, u);
+    jit_movr(JIT_RET, u);
     jit_ret();
     jit_dec_synth();
 }
 
 void
-_jit_reti(jit_state_t *_jit, jit_word_t u)
+_jit_reti(jit_state_t *_jit, jit_word_t u, jit_code_t code)
 {
-    jit_inc_synth_w(reti, u);
+    jit_code_inc_synth_w(code, u);
     jit_movi(JIT_RET, u);
     jit_ret();
     jit_dec_synth();
@@ -421,7 +456,7 @@ _jit_epilog(jit_state_t *_jit)
 jit_bool_t
 _jit_arg_register_p(jit_state_t *_jit, jit_node_t *u)
 {
-    if (u->code != jit_code_arg) {
+    if (!(u->code >= jit_code_arg_c && u->code <= jit_code_arg)) {
        if (u->code == jit_code_arg_f) {
            if (jit_cpu.abi)
                return (jit_arg_f_reg_p(u->u.w));
@@ -436,7 +471,7 @@ _jit_arg_register_p(jit_state_t *_jit, jit_node_t *u)
 }
 
 static jit_node_t *
-_jit_make_arg(jit_state_t *_jit, jit_node_t *node)
+_jit_make_arg(jit_state_t *_jit, jit_node_t *node, jit_code_t code)
 {
     jit_int32_t                 offset;
     if (jit_arg_reg_p(_jitc->function->self.argi))
@@ -446,7 +481,7 @@ _jit_make_arg(jit_state_t *_jit, jit_node_t *node)
        _jitc->function->self.size += sizeof(jit_word_t);
     }
     if (node == (jit_node_t *)0)
-       node = jit_new_node(jit_code_arg);
+       node = jit_new_node(code);
     else
        link_node(node);
     node->u.w = offset;
@@ -533,9 +568,10 @@ _jit_ellipsis(jit_state_t *_jit)
     else {
        assert(!(_jitc->function->self.call & jit_call_varargs));
        _jitc->function->self.call |= jit_call_varargs;
+       CHECK_REG_ARGS();
        if (jit_cpu.abi &&  _jitc->function->self.argf)
            rewind_prolog();
-       /* First 4 stack addresses are always spilled r0-r3 */
+       /* First 4 stack addresses need to be spilled r0-r3 */
        if (jit_arg_reg_p(_jitc->function->self.argi))
            _jitc->function->vagp = _jitc->function->self.argi * 4;
        else
@@ -558,16 +594,21 @@ _jit_va_push(jit_state_t *_jit, jit_int32_t u)
 }
 
 jit_node_t *
-_jit_arg(jit_state_t *_jit)
+_jit_arg(jit_state_t *_jit, jit_code_t code)
 {
     assert(_jitc->function);
-    return (jit_make_arg((jit_node_t*)0));
+    assert(!(_jitc->function->self.call & jit_call_varargs));
+#if STRONG_TYPE_CHECKING
+    assert(code >= jit_code_arg_c && code <= jit_code_arg);
+#endif
+    return (jit_make_arg((jit_node_t*)0, code));
 }
 
 jit_node_t *
 _jit_arg_f(jit_state_t *_jit)
 {
     assert(_jitc->function);
+    assert(!(_jitc->function->self.call & jit_call_varargs));
     return (jit_make_arg_f((jit_node_t*)0));
 }
 
@@ -575,103 +616,141 @@ jit_node_t *
 _jit_arg_d(jit_state_t *_jit)
 {
     assert(_jitc->function);
+    assert(!(_jitc->function->self.call & jit_call_varargs));
     return (jit_make_arg_d((jit_node_t*)0));
 }
 
 void
 _jit_getarg_c(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 {
-    assert(v->code == jit_code_arg);
+    jit_node_t         *node = NULL;
+    assert_arg_type(v->code, jit_code_arg_c);
     jit_inc_synth_wp(getarg_c, u, v);
     if (jit_swf_p())
-       jit_ldxi_c(u, JIT_FP, arg_offset(v->u.w));
+       node = jit_ldxi_c(u, JIT_FP, arg_offset(v->u.w));
     else if (jit_arg_reg_p(v->u.w))
        jit_extr_c(u, JIT_RA0 - v->u.w);
     else
-       jit_ldxi_c(u, JIT_FP, v->u.w);
+       node = jit_ldxi_c(u, JIT_FP, v->u.w);
+    if (node) {
+       CHECK_REG_ARGS();
+       jit_link_alist(node);
+       jit_check_frame();
+    }
     jit_dec_synth();
 }
 
 void
 _jit_getarg_uc(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 {
-    assert(v->code == jit_code_arg);
+    jit_node_t         *node = NULL;
+    assert_arg_type(v->code, jit_code_arg_c);
     jit_inc_synth_wp(getarg_uc, u, v);
     if (jit_swf_p())
-       jit_ldxi_uc(u, JIT_FP, arg_offset(v->u.w));
+       node = jit_ldxi_uc(u, JIT_FP, arg_offset(v->u.w));
     else if (jit_arg_reg_p(v->u.w))
        jit_extr_uc(u, JIT_RA0 - v->u.w);
     else
-       jit_ldxi_uc(u, JIT_FP, v->u.w);
+       node = jit_ldxi_uc(u, JIT_FP, v->u.w);
+    if (node) {
+       CHECK_REG_ARGS();
+       jit_link_alist(node);
+       jit_check_frame();
+    }
     jit_dec_synth();
 }
 
 void
 _jit_getarg_s(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 {
-    assert(v->code == jit_code_arg);
+    jit_node_t         *node = NULL;
+    assert_arg_type(v->code, jit_code_arg_s);
     jit_inc_synth_wp(getarg_s, u, v);
     if (jit_swf_p())
-       jit_ldxi_s(u, JIT_FP, arg_offset(v->u.w));
+       node = jit_ldxi_s(u, JIT_FP, arg_offset(v->u.w));
     else if (jit_arg_reg_p(v->u.w))
        jit_extr_s(u, JIT_RA0 - v->u.w);
     else
-       jit_ldxi_s(u, JIT_FP, v->u.w);
+       node = jit_ldxi_s(u, JIT_FP, v->u.w);
+    if (node) {
+       CHECK_REG_ARGS();
+       jit_link_alist(node);
+       jit_check_frame();
+    }
     jit_dec_synth();
 }
 
 void
 _jit_getarg_us(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 {
-    assert(v->code == jit_code_arg);
+    jit_node_t         *node = NULL;
+    assert_arg_type(v->code, jit_code_arg_s);
     jit_inc_synth_wp(getarg_us, u, v);
     if (jit_swf_p())
-       jit_ldxi_us(u, JIT_FP, arg_offset(v->u.w));
+       node = jit_ldxi_us(u, JIT_FP, arg_offset(v->u.w));
     else if (jit_arg_reg_p(v->u.w))
        jit_extr_us(u, JIT_RA0 - v->u.w);
     else
-       jit_ldxi_us(u, JIT_FP, v->u.w);
+       node = jit_ldxi_us(u, JIT_FP, v->u.w);
+    if (node) {
+       CHECK_REG_ARGS();
+       jit_link_alist(node);
+       jit_check_frame();
+    }
     jit_dec_synth();
 }
 
 void
 _jit_getarg_i(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 {
-    assert(v->code == jit_code_arg);
+    jit_node_t         *node = NULL;
+    assert_arg_type(v->code, jit_code_arg_i);
     jit_inc_synth_wp(getarg_i, u, v);
     if (jit_swf_p())
-       jit_ldxi_i(u, JIT_FP, arg_offset(v->u.w));
+       node = jit_ldxi_i(u, JIT_FP, arg_offset(v->u.w));
     else if (jit_arg_reg_p(v->u.w))
        jit_movr(u, JIT_RA0 - v->u.w);
     else
-       jit_ldxi_i(u, JIT_FP, v->u.w);
+       node = jit_ldxi_i(u, JIT_FP, v->u.w);
+    if (node) {
+       CHECK_REG_ARGS();
+       jit_link_alist(node);
+       jit_check_frame();
+    }
     jit_dec_synth();
 }
 
 void
-_jit_putargr(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
+_jit_putargr(jit_state_t *_jit, jit_int32_t u, jit_node_t *v, jit_code_t code)
 {
-    assert(v->code == jit_code_arg);
-    jit_inc_synth_wp(putargr, u, v);
+    jit_node_t         *node = NULL;
+    assert_putarg_type(code, v->code);
+    jit_code_inc_synth_wp(code, u, v);
     if (jit_swf_p())
-       jit_stxi(arg_offset(v->u.w), JIT_FP, u);
+       node = jit_stxi(arg_offset(v->u.w), JIT_FP, u);
     else if (jit_arg_reg_p(v->u.w))
        jit_movr(JIT_RA0 - v->u.w, u);
     else
-       jit_stxi(v->u.w, JIT_FP, u);
+       node = jit_stxi(v->u.w, JIT_FP, u);
+    if (node) {
+       CHECK_REG_ARGS();
+       jit_link_alist(node);
+       jit_check_frame();
+    }
     jit_dec_synth();
 }
 
 void
-_jit_putargi(jit_state_t *_jit, jit_word_t u, jit_node_t *v)
+_jit_putargi(jit_state_t *_jit, jit_word_t u, jit_node_t *v, jit_code_t code)
 {
-    jit_int32_t                regno;
-    assert(v->code == jit_code_arg);
-    jit_inc_synth_wp(putargi, u, v);
+    jit_int32_t                 regno;
+    jit_node_t         *node = NULL;
+    assert_putarg_type(code, v->code);
+    jit_code_inc_synth_wp(code, u, v);
     if (jit_swf_p()) {
        regno = jit_get_reg(jit_class_gpr);
        jit_movi(regno, u);
-       jit_stxi(arg_offset(v->u.w), JIT_FP, regno);
+       node = jit_stxi(arg_offset(v->u.w), JIT_FP, regno);
        jit_unget_reg(regno);
     }
     else if (jit_arg_reg_p(v->u.w))
@@ -679,30 +758,41 @@ _jit_putargi(jit_state_t *_jit, jit_word_t u, jit_node_t *v)
     else {
        regno = jit_get_reg(jit_class_gpr);
        jit_movi(regno, u);
-       jit_stxi(v->u.w, JIT_FP, regno);
+       node = jit_stxi(v->u.w, JIT_FP, regno);
        jit_unget_reg(regno);
     }
+    if (node) {
+       CHECK_REG_ARGS();
+       jit_link_alist(node);
+       jit_check_frame();
+    }
     jit_dec_synth();
 }
 
 void
 _jit_getarg_f(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 {
+    jit_node_t         *node = NULL;
     assert(v->code == jit_code_arg_f);
     jit_inc_synth_wp(getarg_f, u, v);
     if (jit_cpu.abi && !(_jitc->function->self.call & jit_call_varargs)) {
        if (jit_arg_f_reg_p(v->u.w))
            jit_movr_f(u, JIT_FA0 - v->u.w);
        else
-           jit_ldxi_f(u, JIT_FP, v->u.w);
+           node = jit_ldxi_f(u, JIT_FP, v->u.w);
     }
     else if (jit_swf_p())
-       jit_ldxi_f(u, JIT_FP, arg_offset(v->u.w));
+       node = jit_ldxi_f(u, JIT_FP, arg_offset(v->u.w));
     else {
        if (jit_arg_reg_p(v->u.w))
            jit_movr_w_f(u, JIT_RA0 - v->u.w);
        else
-           jit_ldxi_f(u, JIT_FP, v->u.w);
+           node = jit_ldxi_f(u, JIT_FP, v->u.w);
+    }
+    if (node) {
+       CHECK_REG_ARGS();
+       jit_link_alist(node);
+       jit_check_frame();
     }
     jit_dec_synth();
 }
@@ -710,21 +800,27 @@ _jit_getarg_f(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 void
 _jit_putargr_f(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 {
+    jit_node_t         *node = NULL;
     assert(v->code == jit_code_arg_f);
     jit_inc_synth_wp(putargr_f, u, v);
     if (jit_cpu.abi) {
        if (jit_arg_f_reg_p(v->u.w))
            jit_movr_f(JIT_FA0 - v->u.w, u);
        else
-           jit_stxi_f(v->u.w, JIT_FP, u);
+           node = jit_stxi_f(v->u.w, JIT_FP, u);
     }
     else if (jit_swf_p())
-       jit_stxi_f(arg_offset(v->u.w), JIT_FP, u);
+       node = jit_stxi_f(arg_offset(v->u.w), JIT_FP, u);
     else {
        if (jit_arg_reg_p(v->u.w))
            jit_movr_f_w(JIT_RA0 - v->u.w, u);
        else
-           jit_stxi_f(v->u.w, JIT_FP, u);
+           node = jit_stxi_f(v->u.w, JIT_FP, u);
+    }
+    if (node) {
+       CHECK_REG_ARGS();
+       jit_link_alist(node);
+       jit_check_frame();
     }
     jit_dec_synth();
 }
@@ -732,7 +828,8 @@ _jit_putargr_f(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 void
 _jit_putargi_f(jit_state_t *_jit, jit_float32_t u, jit_node_t *v)
 {
-    jit_int32_t                regno;
+    jit_int32_t                 regno;
+    jit_node_t         *node = NULL;
     assert(v->code == jit_code_arg_f);
     jit_inc_synth_fp(putargi_f, u, v);
     if (jit_cpu.abi) {
@@ -741,14 +838,14 @@ _jit_putargi_f(jit_state_t *_jit, jit_float32_t u, jit_node_t *v)
        else {
            regno = jit_get_reg(jit_class_fpr);
            jit_movi_f(regno, u);
-           jit_stxi_f(v->u.w, JIT_FP, regno);
+           node = jit_stxi_f(v->u.w, JIT_FP, regno);
            jit_unget_reg(regno);
        }
     }
     else if (jit_swf_p()) {
        regno = jit_get_reg(jit_class_fpr);
        jit_movi_f(regno, u);
-       jit_stxi_f(arg_offset(v->u.w), JIT_FP, regno);
+       node = jit_stxi_f(arg_offset(v->u.w), JIT_FP, regno);
        jit_unget_reg(regno);
     }
     else {
@@ -757,30 +854,41 @@ _jit_putargi_f(jit_state_t *_jit, jit_float32_t u, jit_node_t *v)
        if (jit_arg_reg_p(v->u.w))
            jit_movr_f_w(JIT_RA0 - v->u.w, regno);
        else
-           jit_stxi_f(v->u.w, JIT_FP, regno);
+           node = jit_stxi_f(v->u.w, JIT_FP, regno);
        jit_unget_reg(regno);
     }
+    if (node) {
+       CHECK_REG_ARGS();
+       jit_link_alist(node);
+       jit_check_frame();
+    }
     jit_dec_synth();
 }
 
 void
 _jit_getarg_d(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 {
+    jit_node_t         *node = NULL;
     assert(v->code == jit_code_arg_d);
     jit_inc_synth_wp(getarg_d, u, v);
     if (jit_cpu.abi && !(_jitc->function->self.call & jit_call_varargs)) {
        if (jit_arg_f_reg_p(v->u.w))
            jit_movr_d(u, JIT_FA0 - v->u.w);
        else
-           jit_ldxi_d(u, JIT_FP, v->u.w);
+           node = jit_ldxi_d(u, JIT_FP, v->u.w);
     }
     else if (jit_swf_p())
-       jit_ldxi_d(u, JIT_FP, arg_offset(v->u.w));
+       node = jit_ldxi_d(u, JIT_FP, arg_offset(v->u.w));
     else {
        if (jit_arg_reg_p(v->u.w))
            jit_movr_ww_d(u, JIT_RA0 - v->u.w, JIT_RA0 - (v->u.w + 1));
        else
-           jit_ldxi_d(u, JIT_FP, v->u.w);
+           node = jit_ldxi_d(u, JIT_FP, v->u.w);
+    }
+    if (node) {
+       CHECK_REG_ARGS();
+       jit_link_alist(node);
+       jit_check_frame();
     }
     jit_dec_synth();
 }
@@ -788,21 +896,27 @@ _jit_getarg_d(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 void
 _jit_putargr_d(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 {
+    jit_node_t         *node = NULL;
     assert(v->code == jit_code_arg_d);
     jit_inc_synth_wp(putargr_d, u, v);
     if (jit_cpu.abi) {
        if (jit_arg_f_reg_p(v->u.w))
            jit_movr_d(JIT_FA0 - v->u.w, u);
        else
-           jit_stxi_d(v->u.w, JIT_FP, u);
+           node = jit_stxi_d(v->u.w, JIT_FP, u);
     }
     else if (jit_swf_p())
-       jit_stxi_d(arg_offset(v->u.w), JIT_FP, u);
+       node = jit_stxi_d(arg_offset(v->u.w), JIT_FP, u);
     else {
        if (jit_arg_reg_p(v->u.w))
            jit_movr_d_ww(JIT_RA0 - v->u.w, JIT_RA0 - (v->u.w + 1), u);
        else
-           jit_stxi_d(v->u.w, JIT_FP, u);
+           node = jit_stxi_d(v->u.w, JIT_FP, u);
+    }
+    if (node) {
+       CHECK_REG_ARGS();
+       jit_link_alist(node);
+       jit_check_frame();
     }
     jit_dec_synth();
 }
@@ -810,7 +924,8 @@ _jit_putargr_d(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 void
 _jit_putargi_d(jit_state_t *_jit, jit_float64_t u, jit_node_t *v)
 {
-    jit_int32_t                regno;
+    jit_int32_t                 regno;
+    jit_node_t         *node = NULL;
     assert(v->code == jit_code_arg_d);
     jit_inc_synth_dp(putargi_d, u, v);
     if (jit_cpu.abi) {
@@ -819,14 +934,14 @@ _jit_putargi_d(jit_state_t *_jit, jit_float64_t u, jit_node_t *v)
        else {
            regno = jit_get_reg(jit_class_fpr);
            jit_movi_d(regno, u);
-           jit_stxi_d(v->u.w, JIT_FP, regno);
+           node = jit_stxi_d(v->u.w, JIT_FP, regno);
            jit_unget_reg(regno);
        }
     }
     else if (jit_swf_p()) {
        regno = jit_get_reg(jit_class_fpr);
        jit_movi_d(regno, u);
-       jit_stxi_d(arg_offset(v->u.w), JIT_FP, regno);
+       node = jit_stxi_d(arg_offset(v->u.w), JIT_FP, regno);
        jit_unget_reg(regno);
     }
     else {
@@ -835,17 +950,22 @@ _jit_putargi_d(jit_state_t *_jit, jit_float64_t u, jit_node_t *v)
        if (jit_arg_reg_p(v->u.w))
            jit_movr_d_ww(JIT_RA0 - v->u.w, JIT_RA0 - (v->u.w + 1), regno);
        else
-           jit_stxi_d(v->u.w, JIT_FP, regno);
+           node = jit_stxi_d(v->u.w, JIT_FP, regno);
        jit_unget_reg(regno);
     }
+    if (node) {
+       CHECK_REG_ARGS();
+       jit_link_alist(node);
+       jit_check_frame();
+    }
     jit_dec_synth();
 }
 
 void
-_jit_pushargr(jit_state_t *_jit, jit_int32_t u)
+_jit_pushargr(jit_state_t *_jit, jit_int32_t u, jit_code_t code)
 {
     assert(_jitc->function);
-    jit_inc_synth_w(pushargr, u);
+    jit_code_inc_synth_w(code, u);
     jit_link_prepare();
     if (jit_arg_reg_p(_jitc->function->call.argi)) {
        jit_movr(JIT_RA0 - _jitc->function->call.argi, u);
@@ -859,11 +979,11 @@ _jit_pushargr(jit_state_t *_jit, jit_int32_t u)
 }
 
 void
-_jit_pushargi(jit_state_t *_jit, jit_word_t u)
+_jit_pushargi(jit_state_t *_jit, jit_word_t u, jit_code_t code)
 {
     jit_int32_t                 regno;
     assert(_jitc->function);
-    jit_inc_synth_w(pushargi, u);
+    jit_code_inc_synth_w(code, u);
     jit_link_prepare();
     if (jit_arg_reg_p(_jitc->function->call.argi)) {
        jit_movi(JIT_RA0 - _jitc->function->call.argi, u);
@@ -1147,6 +1267,7 @@ _emit_code(jit_state_t *_jit)
        jit_node_t      *node;
        jit_uint8_t     *data;
        jit_word_t       word;
+       jit_function_t   func;
 #if DEVEL_DISASSEMBLER
        jit_word_t       prevw;
 #endif
@@ -1292,7 +1413,7 @@ _emit_code(jit_state_t *_jit)
                else {                                                  \
                    word = name##r##type(_jit->pc.w,                    \
                                         rn(node->v.w), rn(node->w.w)); \
-                   patch(word, node);                                  \
+                   patch(word, node, arm_patch_jump);                  \
                }                                                       \
                break
 #define case_bvv(name, type)                                           \
@@ -1317,7 +1438,7 @@ _emit_code(jit_state_t *_jit)
                        word = vfp_##name##r##type(_jit->pc.w,          \
                                                   rn(node->v.w),       \
                                                   rn(node->w.w));      \
-                   patch(word, node);                                  \
+                   patch(word, node, arm_patch_jump);                  \
                }                                                       \
                break
 #define case_brw(name, type)                                           \
@@ -1331,7 +1452,7 @@ _emit_code(jit_state_t *_jit)
                else {                                                  \
                    word = name##i##type(_jit->pc.w,                    \
                                         rn(node->v.w), node->w.w);     \
-                   patch(word, node);                                  \
+                   patch(word, node, arm_patch_jump);                  \
                }                                                       \
                break;
 #define case_bvf(name)                                                 \
@@ -1356,7 +1477,7 @@ _emit_code(jit_state_t *_jit)
                        word = vfp_##name##i_f(_jit->pc.w,              \
                                               rn(node->v.w),           \
                                               node->w.f);              \
-                   patch(word, node);                                  \
+                   patch(word, node, arm_patch_jump);                  \
                }                                                       \
                break
 #define case_bvd(name)                                                 \
@@ -1381,7 +1502,7 @@ _emit_code(jit_state_t *_jit)
                        word = vfp_##name##i_d(_jit->pc.w,              \
                                               rn(node->v.w),           \
                                               node->w.d);              \
-                   patch(word, node);                                  \
+                   patch(word, node, arm_patch_jump);                  \
                }                                                       \
                break
 #if DEVEL_DISASSEMBLER
@@ -1399,11 +1520,16 @@ _emit_code(jit_state_t *_jit)
        jit_regarg_set(node, value);
        switch (node->code) {
            case jit_code_align:
-               assert(!(node->u.w & (node->u.w - 1)) &&
-                      node->u.w <= sizeof(jit_word_t));
-               if (node->u.w == sizeof(jit_word_t) &&
-                   (word = _jit->pc.w & (sizeof(jit_word_t) - 1)))
-                   nop(sizeof(jit_word_t) - word);
+               /* Must align to a power of two */
+               assert(!(node->u.w & (node->u.w - 1)));
+               if ((word = _jit->pc.w & (node->u.w - 1)))
+                   nop(node->u.w - word);
+               break;
+           case jit_code_skip:
+               if (jit_thumb_p())
+                   nop((node->u.w + 1) & ~1);
+               else
+                   nop((node->u.w + 3) & ~3);
                break;
            case jit_code_note:         case jit_code_name:
                if (must_align_p(node->next))
@@ -1456,6 +1582,10 @@ _emit_code(jit_state_t *_jit)
                case_rrw(rsh, _u);
                case_rr(neg,);
                case_rr(com,);
+               case_rr(clo,);
+               case_rr(clz,);
+               case_rr(cto,);
+               case_rr(ctz,);
                case_rrr(and,);
                case_rrw(and,);
                case_rrr(or,);
@@ -1498,11 +1628,23 @@ _emit_code(jit_state_t *_jit)
                case_wrr(stx, _i);
                case_rr(hton, _us);
                case_rr(hton, _ui);
+               case_rr(bswap, _us);
+               case_rr(bswap, _ui);
                case_rr(ext, _c);
                case_rr(ext, _uc);
                case_rr(ext, _s);
                case_rr(ext, _us);
+           case jit_code_casr:
+               casr(rn(node->u.w), rn(node->v.w),
+                    rn(node->w.q.l), rn(node->w.q.h));
+               break;
+           case jit_code_casi:
+               casi(rn(node->u.w), node->v.w,
+                    rn(node->w.q.l), rn(node->w.q.h));
+               break;
                case_rr(mov,);
+               case_rrr(movn,);
+               case_rrr(movz,);
            case jit_code_movi:
                if (node->flag & jit_flag_node) {
                    temp = node->v.n;
@@ -1514,7 +1656,7 @@ _emit_code(jit_state_t *_jit)
                        assert(temp->code == jit_code_label ||
                               temp->code == jit_code_epilog);
                        word = movi_p(rn(node->u.w), temp->u.w);
-                       patch(word, node);
+                       patch(word, node, arm_patch_word);
                    }
                }
                else
@@ -1753,6 +1895,7 @@ _emit_code(jit_state_t *_jit)
                case_bvv(bunord, _d);
                case_bvd(bunord);
            case jit_code_jmpr:
+               jit_check_frame();
                jmpr(rn(node->u.w));
                flush_consts();
                break;
@@ -1764,36 +1907,59 @@ _emit_code(jit_state_t *_jit)
                    if (temp->flag & jit_flag_patch)
                        jmpi(temp->u.w);
                    else {
-                       word = jmpi_p(_jit->pc.w, 1);
-                       patch(word, node);
+                       word = _jit->code.length -
+                           (_jit->pc.uc - _jit->code.ptr);
+                       if (jit_thumb_p())      word >>= 1;
+                       else                    word >>= 2;
+                       word -= 2;
+                       value = _s24P(word);
+                       word = jmpi_p(_jit->pc.w, value);
+                       patch(word, node, value ?
+                             arm_patch_jump : arm_patch_word);
                    }
                }
-               else
+               else {
+                   jit_check_frame();
                    jmpi(node->u.w);
+               }
                flush_consts();
                break;
            case jit_code_callr:
+               jit_check_frame();
                callr(rn(node->u.w));
                break;
            case jit_code_calli:
                if (node->flag & jit_flag_node) {
+                   CHECK_RETURN();
                    temp = node->u.n;
                    assert(temp->code == jit_code_label ||
                           temp->code == jit_code_epilog);
                    if (temp->flag & jit_flag_patch)
-                       calli(temp->u.w);
+                       calli(temp->u.w, 0);
                    else {
-                       word = calli_p(_jit->pc.w);
-                       patch(word, node);
+                       word = _jit->code.length -
+                           (_jit->pc.uc - _jit->code.ptr);
+                       if (jit_exchange_p())
+                           word -= 8;
+                       if (jit_thumb_p())      word >>= 1;
+                       else                    word >>= 2;
+                       word -= 2;
+                       value = _s24P(word);
+                       word = calli_p(_jit->pc.w, value);
+                       patch(word, node, value ?
+                             arm_patch_call : arm_patch_word);
                    }
                }
-               else
-                   calli(node->u.w);
+               else {
+                   jit_check_frame();
+                   calli(node->u.w, jit_exchange_p());
+               }
                break;
            case jit_code_prolog:
                _jitc->function = _jitc->functions.ptr + node->w.w;
                undo.node = node;
                undo.word = _jit->pc.w;
+               memcpy(&undo.func, _jitc->function, sizeof(undo.func));
 #if DEVEL_DISASSEMBLER
                undo.prevw = prevw;
 #endif
@@ -1807,6 +1973,8 @@ _emit_code(jit_state_t *_jit)
 #endif
            restart_function:
                _jitc->again = 0;
+               compute_framesize();
+               patch_alist(0);
                prolog(node);
                break;
            case jit_code_epilog:
@@ -1821,6 +1989,21 @@ _emit_code(jit_state_t *_jit)
                    temp->flag &= ~jit_flag_patch;
                    node = undo.node;
                    _jit->pc.w = undo.word;
+                   /* undo.func.self.aoff and undo.func.regset should not
+                    * be undone, as they will be further updated, and are
+                    * the reason of the undo. */
+                   undo.func.self.aoff = _jitc->function->frame +
+                       _jitc->function->self.aoff;
+                   undo.func.need_frame = _jitc->function->need_frame;
+                   undo.func.need_return = _jitc->function->need_return;
+                   jit_regset_set(&undo.func.regset, &_jitc->function->regset);
+                   /* allocar information also does not need to be undone */
+                   undo.func.aoffoff = _jitc->function->aoffoff;
+                   undo.func.allocar = _jitc->function->allocar;
+                   /* swf_offset and check_reg_args must also not be undone */
+                   undo.func.swf_offset = _jitc->function->swf_offset;
+                   undo.func.save_reg_args = _jitc->function->save_reg_args;
+                   memcpy(_jitc->function, &undo.func, sizeof(undo.func));
 #if DEVEL_DISASSEMBLER
                    prevw = undo.prevw;
 #endif
@@ -1833,6 +2016,7 @@ _emit_code(jit_state_t *_jit)
                    if (_jitc->data_info.ptr)
                        _jitc->data_info.offset = undo.info_offset;
 #endif
+                   patch_alist(1);
                    goto restart_function;
                }
                /* remember label is defined */
@@ -1895,21 +2079,34 @@ _emit_code(jit_state_t *_jit)
            case jit_code_live:                 case jit_code_ellipsis:
            case jit_code_va_push:
            case jit_code_allocai:              case jit_code_allocar:
-           case jit_code_arg:
+           case jit_code_arg_c:                case jit_code_arg_s:
+           case jit_code_arg_i:
            case jit_code_arg_f:                case jit_code_arg_d:
            case jit_code_va_end:
            case jit_code_ret:
-           case jit_code_retr:                 case jit_code_reti:
+           case jit_code_retr_c:               case jit_code_reti_c:
+           case jit_code_retr_uc:              case jit_code_reti_uc:
+           case jit_code_retr_s:               case jit_code_reti_s:
+           case jit_code_retr_us:              case jit_code_reti_us:
+           case jit_code_retr_i:               case jit_code_reti_i:
            case jit_code_retr_f:               case jit_code_reti_f:
            case jit_code_retr_d:               case jit_code_reti_d:
            case jit_code_getarg_c:             case jit_code_getarg_uc:
            case jit_code_getarg_s:             case jit_code_getarg_us:
            case jit_code_getarg_i:
            case jit_code_getarg_f:             case jit_code_getarg_d:
-           case jit_code_putargr:              case jit_code_putargi:
+           case jit_code_putargr_c:            case jit_code_putargi_c:
+           case jit_code_putargr_uc:           case  jit_code_putargi_uc:
+           case jit_code_putargr_s:            case jit_code_putargi_s:
+           case jit_code_putargr_us:           case jit_code_putargi_us:
+           case jit_code_putargr_i:            case jit_code_putargi_i:
            case jit_code_putargr_f:            case jit_code_putargi_f:
            case jit_code_putargr_d:            case jit_code_putargi_d:
-           case jit_code_pushargr:             case jit_code_pushargi:
+           case jit_code_pushargr_c:           case jit_code_pushargi_c:
+           case jit_code_pushargr_uc:          case jit_code_pushargi_uc:
+           case jit_code_pushargr_s:           case jit_code_pushargi_s:
+           case jit_code_pushargr_us:          case jit_code_pushargi_us:
+           case jit_code_pushargr_i:           case jit_code_pushargi_i:
            case jit_code_pushargr_f:           case jit_code_pushargi_f:
            case jit_code_pushargr_d:           case jit_code_pushargi_d:
            case jit_code_retval_c:             case jit_code_retval_uc:
@@ -1972,7 +2169,10 @@ _emit_code(jit_state_t *_jit)
        node = _jitc->patches.ptr[offset].node;
        word = _jitc->patches.ptr[offset].inst;
        if (!jit_thumb_p() &&
-           (node->code == jit_code_movi || node->code == jit_code_calli)) {
+           (node->code == jit_code_movi ||
+            (node->code == jit_code_calli &&
+             (_jitc->patches.ptr[offset].kind & ~arm_patch_node) ==
+             arm_patch_word))) {
            /* calculate where to patch word */
            value = *(jit_int32_t *)word;
            assert((value & 0x0f700000) == ARM_LDRI);
@@ -1999,6 +2199,7 @@ _emit_code(jit_state_t *_jit)
 #  include "jit_arm-cpu.c"
 #  include "jit_arm-swf.c"
 #  include "jit_arm-vfp.c"
+#  include "jit_fallback.c"
 #undef CODE
 
 void
@@ -2241,24 +2442,31 @@ _invalidate_consts(jit_state_t *_jit)
 }
 
 static void
-_patch(jit_state_t *_jit, jit_word_t instr, jit_node_t *node)
+_compute_framesize(jit_state_t *_jit)
+{
+    jit_int32_t                reg;
+    _jitc->framesize = sizeof(jit_word_t) * 2; /* lr+fp */
+    for (reg = 0; reg < jit_size(iregs); reg++)
+       if (jit_regset_tstbit(&_jitc->function->regset, iregs[reg]))
+           _jitc->framesize += sizeof(jit_word_t);
+
+    if (_jitc->function->save_reg_args)
+       _jitc->framesize += 16;
+
+    /* Make sure functions called have a 8 byte aligned stack */
+    _jitc->framesize = (_jitc->framesize + 7) & -8;
+}
+
+static void
+_patch(jit_state_t *_jit, jit_word_t instr, jit_node_t *node, jit_int32_t kind)
 {
     jit_int32_t                 flag;
-    jit_int32_t                 kind;
 
     assert(node->flag & jit_flag_node);
-    if (node->code == jit_code_movi) {
+    if (node->code == jit_code_movi)
        flag = node->v.n->flag;
-       kind = arm_patch_word;
-    }
-    else {
+    else
        flag = node->u.n->flag;
-       if (node->code == jit_code_calli ||
-           (node->code == jit_code_jmpi && !(node->flag & jit_flag_node)))
-           kind = arm_patch_word;
-       else
-           kind = arm_patch_jump;
-    }
     assert(!(flag & jit_flag_patch));
     kind |= arm_patch_node;
     if (_jitc->patches.offset >= _jitc->patches.length) {