update libchdr
[pcsx_rearmed.git] / deps / lightning / lib / jit_sparc.c
index 158c09d..9e837d8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013-2019  Free Software Foundation, Inc.
+ * Copyright (C) 2013-2023  Free Software Foundation, Inc.
  *
  * This file is part of GNU lightning.
  *
  *     Paulo Cesar Pereira de Andrade
  */
 
+/* Handling SIGILL should not be done by Lightning, but can either use
+ * sample, or use another approach to set jit_cpu.lzcnt
+ */
+#define CHECK_LZCNT    0
+
+#if CHECK_LZCNT
+#include <signal.h>
+#include <setjmp.h>
+#endif
+
 #define jit_arg_reg_p(i)               ((i) >= 0 && (i) < 6)
 #if __WORDSIZE == 32
 #  define jit_arg_d_reg_p(i)           ((i) >= 0 && (i) < 5)
@@ -40,11 +50,13 @@ static void _patch(jit_state_t*,jit_word_t,jit_node_t*);
 #define PROTO                          1
 #  include "jit_sparc-cpu.c"
 #  include "jit_sparc-fpu.c"
+#  include "jit_fallback.c"
 #undef PROTO
 
 /*
  * Initialization
  */
+jit_cpu_t              jit_cpu;
 jit_register_t         _rvs[] = {
     { 0x00,                            "%g0" },
     { 0x01,                            "%g1" },
@@ -147,13 +159,45 @@ jit_register_t            _rvs[] = {
 #  endif
     { _NOREG,                          "<none>" },
 };
+#if CHECK_LZCNT
+sigjmp_buf             jit_env;
+#endif
 
 /*
  * Implementation
  */
+#if CHECK_LZCNT
+static void
+sigill_handler(int signum)
+{
+    jit_cpu.lzcnt = 0;
+    siglongjmp(jit_env, 1);
+}
+#endif
+
 void
 jit_get_cpu(void)
 {
+#if CHECK_LZCNT
+    int                        g2;
+    struct             sigaction new_action, old_action;
+    new_action.sa_handler = sigill_handler;
+    sigemptyset(&new_action.sa_mask);
+    new_action.sa_flags = 0;
+    sigaction(SIGILL, NULL, &old_action);
+    if (old_action.sa_handler != SIG_IGN) {
+       sigaction(SIGILL, &new_action, NULL);
+       if (!sigsetjmp(jit_env, 1)) {
+           jit_cpu.lzcnt = 1;
+           /* lzcnt %g2, %g2 */
+           __asm__ volatile("mov %%g2, %0; .long 0xa3b0021; mov %0, %%g2"
+                            : "=r" (g2));
+           sigaction(SIGILL, &old_action, NULL);
+       }
+    }
+#else
+    jit_cpu.lzcnt = 0;
+#endif
 }
 
 void
@@ -184,7 +228,7 @@ _jit_prolog(jit_state_t *_jit)
     _jitc->function = _jitc->functions.ptr + _jitc->functions.offset++;
     _jitc->function->self.size = stack_framesize;
     _jitc->function->self.argi = _jitc->function->self.argf =
-       _jitc->function->self.aoff = _jitc->function->self.alen = 0;
+       _jitc->function->self.alen = 0;
     /* float conversion */
 #  if __WORDSIZE == 32
     _jitc->function->self.aoff = -8;
@@ -265,20 +309,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();
@@ -339,12 +381,13 @@ jit_bool_t
 _jit_arg_register_p(jit_state_t *_jit, jit_node_t *u)
 {
 #  if __WORDSIZE == 32
-    if (u->code == jit_code_arg || u->code == jit_code_arg_f)
+    if ((u->code >= jit_code_arg_c && u->code <= jit_code_arg) ||
+       u->code == jit_code_arg_f)
        return (jit_arg_reg_p(u->u.w));
     assert(u->code == jit_code_arg_d);
     return (jit_arg_d_reg_p(u->u.w));
 #  else
-    if (u->code == jit_code_arg)
+    if (u->code >= jit_code_arg_c && u->code <= jit_code_arg)
        return (jit_arg_reg_p(u->u.w));
     assert(u->code == jit_code_arg_d || u->code == jit_code_arg_f);
     return (jit_arg_d_reg_p(u->u.w));
@@ -379,11 +422,15 @@ _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)
 {
     jit_node_t         *node;
     jit_int32_t                 offset;
     assert(_jitc->function);
+    assert(!(_jitc->function->self.call & jit_call_varargs));
+#if STRONG_TYPE_CHECKING
+    assert(code >= jit_code_arg_c && code <= jit_code_arg);
+#endif
     if (jit_arg_reg_p(_jitc->function->self.argi))
        offset = _jitc->function->self.argi++;
     else {
@@ -394,7 +441,7 @@ _jit_arg(jit_state_t *_jit)
        offset = BIAS(_jitc->function->self.size);
        _jitc->function->self.size += sizeof(jit_word_t);
     }
-    node = jit_new_node_ww(jit_code_arg, offset,
+    node = jit_new_node_ww(code, offset,
                           ++_jitc->function->self.argn);
     jit_link_prolog();
     return (node);
@@ -471,7 +518,7 @@ _jit_arg_d(jit_state_t *_jit)
 void
 _jit_getarg_c(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 {
-    assert(v->code == jit_code_arg);
+    assert_arg_type(v->code, jit_code_arg_c);
     jit_inc_synth_wp(getarg_c, u, v);
     if (jit_arg_reg_p(v->u.w))
        jit_extr_c(u, _I0 + v->u.w);
@@ -484,7 +531,7 @@ _jit_getarg_c(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 void
 _jit_getarg_uc(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 {
-    assert(v->code == jit_code_arg);
+    assert_arg_type(v->code, jit_code_arg_c);
     jit_inc_synth_wp(getarg_uc, u, v);
     if (jit_arg_reg_p(v->u.w))
        jit_extr_uc(u, _I0 + v->u.w);
@@ -497,7 +544,7 @@ _jit_getarg_uc(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 void
 _jit_getarg_s(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 {
-    assert(v->code == jit_code_arg);
+    assert_arg_type(v->code, jit_code_arg_s);
     jit_inc_synth_wp(getarg_s, u, v);
     if (jit_arg_reg_p(v->u.w))
        jit_extr_s(u, _I0 + v->u.w);
@@ -510,7 +557,7 @@ _jit_getarg_s(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 void
 _jit_getarg_us(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 {
-    assert(v->code == jit_code_arg);
+    assert_arg_type(v->code, jit_code_arg_s);
     jit_inc_synth_wp(getarg_us, u, v);
     if (jit_arg_reg_p(v->u.w))
        jit_extr_us(u, _I0 + v->u.w);
@@ -523,7 +570,7 @@ _jit_getarg_us(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 void
 _jit_getarg_i(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 {
-    assert(v->code == jit_code_arg);
+    assert_arg_type(v->code, jit_code_arg_i);
     jit_inc_synth_wp(getarg_i, u, v);
     if (jit_arg_reg_p(v->u.w)) {
 #  if __WORDSIZE == 64
@@ -542,7 +589,7 @@ _jit_getarg_i(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 void
 _jit_getarg_ui(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 {
-    assert(v->code == jit_code_arg);
+    assert_arg_type(v->code, jit_code_arg_i);
     jit_inc_synth_wp(getarg_i, u, v);
     if (jit_arg_reg_p(v->u.w))
        jit_extr_ui(u, _I0 + v->u.w);
@@ -555,7 +602,7 @@ _jit_getarg_ui(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 void
 _jit_getarg_l(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 {
-    assert(v->code == jit_code_arg);
+    assert_arg_type(v->code, jit_code_arg_l);
     jit_inc_synth_wp(getarg_i, u, v);
     if (jit_arg_reg_p(v->u.w))
        jit_movr(u, _I0 + v->u.w);
@@ -566,10 +613,10 @@ _jit_getarg_l(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 #  endif
 
 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);
+    assert_putarg_type(code, v->code);
+    jit_code_inc_synth_wp(code, u, v);
     if (jit_arg_reg_p(v->u.w))
        jit_movr(_I0 + v->u.w, u);
     else
@@ -578,11 +625,11 @@ _jit_putargr(jit_state_t *_jit, jit_int32_t u, jit_node_t *v)
 }
 
 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);
+    assert_putarg_type(code, v->code);
+    jit_code_inc_synth_wp(code, u, v);
     if (jit_arg_reg_p(v->u.w))
        jit_movi(_I0 + v->u.w, u);
     else {
@@ -795,9 +842,9 @@ _jit_putargi_d(jit_state_t *_jit, jit_float64_t u, jit_node_t *v)
 }
 
 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)
 {
-    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(_O0 + _jitc->function->call.argi, u);
@@ -816,10 +863,10 @@ _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;
-    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(_O0 + _jitc->function->call.argi, u);
@@ -1193,6 +1240,7 @@ _emit_code(jit_state_t *_jit)
     struct {
        jit_node_t      *node;
        jit_word_t       word;
+       jit_function_t   func;
 #if DEVEL_DISASSEMBLER
        jit_word_t       prevw;
 #endif
@@ -1321,11 +1369,13 @@ _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:
+               nop((node->u.w + 3) & ~3);
                break;
            case jit_code_note:         case jit_code_name:
                node->u.w = _jit->pc.w;
@@ -1463,6 +1513,11 @@ _emit_code(jit_state_t *_jit)
                case_rr(hton, _ui);
 #if __WORDSIZE == 64
                case_rr(hton, _ul);
+#endif
+               case_rr(bswap, _us);
+               case_rr(bswap, _ui);
+#if __WORDSIZE == 64
+               case_rr(bswap, _ul);
 #endif
                case_rr(ext, _c);
                case_rr(ext, _uc);
@@ -1472,6 +1527,16 @@ _emit_code(jit_state_t *_jit)
                case_rr(ext, _i);
                case_rr(ext, _ui);
 #endif
+           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_rrr(movn,);
+               case_rrr(movz,);
                case_rr(mov,);
            case jit_code_movi:
                if (node->flag & jit_flag_node) {
@@ -1492,6 +1557,10 @@ _emit_code(jit_state_t *_jit)
                break;
                case_rr(neg,);
                case_rr(com,);
+               case_rr(clo,);
+               case_rr(clz,);
+               case_rr(cto,);
+               case_rr(ctz,);
                case_brr(blt,);
                case_brw(blt,);
                case_brr(blt, _u);
@@ -1709,7 +1778,12 @@ _emit_code(jit_state_t *_jit)
                    if (temp->flag & jit_flag_patch)
                        jmpi(temp->u.w);
                    else {
-                       word = jmpi_p(_jit->pc.w);
+                       word = _jit->code.length -
+                           (_jit->pc.uc - _jit->code.ptr);
+                       if (s22_p(word >> 2))
+                           word = jmpi(_jit->pc.w);
+                       else
+                           word = jmpi_p(_jit->pc.w);
                        patch(word, node);
                    }
                }
@@ -1724,9 +1798,17 @@ _emit_code(jit_state_t *_jit)
                    temp = node->u.n;
                    assert(temp->code == jit_code_label ||
                           temp->code == jit_code_epilog);
-                   word = calli_p(temp->u.w);
-                   if (!(temp->flag & jit_flag_patch))
+                   if (temp->flag & jit_flag_patch)
+                       calli(temp->u.w);
+                   else {
+                       word = _jit->code.length -
+                           (_jit->pc.uc - _jit->code.ptr);
+                       if (s30_p(word >> 2))
+                           word = calli(_jit->pc.w);
+                       else
+                           word = calli_p(_jit->pc.w);
                        patch(word, node);
+                   }
                }
                else
                    calli(node->u.w);
@@ -1735,6 +1817,7 @@ _emit_code(jit_state_t *_jit)
                _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
@@ -1755,6 +1838,16 @@ _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;
+                   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;
+                   memcpy(_jitc->function, &undo.func, sizeof(undo.func));
 #if DEVEL_DISASSEMBLER
                    prevw = undo.prevw;
 #endif
@@ -1779,11 +1872,23 @@ _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:
+#if __WORDSIZE == 64
+          case jit_code_arg_l:
+#endif
            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:
+#if __WORDSIZE == 64
+           case jit_code_retr_ui:              case jit_code_reti_ui:
+           case jit_code_retr_l:               case jit_code_reti_l:
+#endif
            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:
@@ -1793,10 +1898,26 @@ _emit_code(jit_state_t *_jit)
            case jit_code_getarg_ui:            case jit_code_getarg_l:
 #endif
            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:
+#if __WORDSIZE == 64
+           case jit_code_putargr_ui:           case jit_code_putargi_ui:
+           case jit_code_putargr_l:            case jit_code_putargi_l:
+#endif
            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:
+#if __WORDSIZE == 64
+           case jit_code_pushargr_ui:          case jit_code_pushargi_ui:
+           case jit_code_pushargr_l:           case jit_code_pushargi_l:
+#endif
            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:
@@ -1868,6 +1989,7 @@ _emit_code(jit_state_t *_jit)
 #define CODE                           1
 #  include "jit_sparc-cpu.c"
 #  include "jit_sparc-fpu.c"
+#  include "jit_fallback.c"
 #undef CODE
 
 void