FT32: Remove recursion in ft32_opcode
authorJames Bowman (FTDI-UK) <james.bowman@ftdichip.com>
Tue, 24 Aug 2021 02:16:56 +0000 (02:16 +0000)
committerAlan Modra <amodra@gmail.com>
Tue, 24 Aug 2021 11:09:29 +0000 (20:39 +0930)
The function ft32_opcode used recursion.  This could cause a stack
overflow.  Replaced with a pair of non-recursive functions.

PR 28169
        * ft32-dis.c: Formatting.
(ft32_opcode1): Split out from..
(ft32_opcode): ..here.

opcodes/ft32-dis.c

index cefc99d1eb34a0d1c5a33315b8c8ff4d57997584..6c68dea9bc8b357e6e35abc8c1393f5e7780ea72 100644 (file)
@@ -33,16 +33,15 @@ static fprintf_ftype fpr;
 static void *stream;
 
 static int
-sign_extend(int bit, int value)
+sign_extend (int bit, int value)
 {
   int onebit = (1 << bit);
   return (value & (onebit - 1)) - (value & onebit);
 }
 
 static void
-ft32_opcode(bfd_vma addr ATTRIBUTE_UNUSED,
-            unsigned int iword,
-            struct disassemble_info *info)
+ft32_opcode1 (unsigned int iword,
+             struct disassemble_info *info)
 {
   const ft32_opc_info_t *oo;
 
@@ -50,14 +49,6 @@ ft32_opcode(bfd_vma addr ATTRIBUTE_UNUSED,
     if ((iword & oo->mask) == oo->bits)
       break;
 
-  unsigned int sc[2];
-  if (ft32_decode_shortcode((unsigned int)addr, iword, sc))
-    {
-      ft32_opcode(addr, sc[0], info);
-      fpr (stream, " ; ");
-      ft32_opcode(addr, sc[1], info);
-    }
-
   if (oo->name)
     {
       int f = oo->fields;
@@ -65,113 +56,123 @@ ft32_opcode(bfd_vma addr ATTRIBUTE_UNUSED,
 
       fpr (stream, "%s", oo->name);
       if (oo->dw)
-        {
-          fpr (stream, ".%c ", "bsl"[(iword >> FT32_FLD_DW_BIT) & 3]);
-        }
+       fpr (stream, ".%c ", "bsl"[(iword >> FT32_FLD_DW_BIT) & 3]);
       else
-        {
-          fpr (stream, " ");
-        }
+       fpr (stream, " ");
 
       while (f)
-        {
-          int lobit = f & -f;
-          if (f & lobit)
-            {
-              switch (lobit)
-              {
-              case  FT32_FLD_CBCRCV:
-                /* imm is {CB, CV}  */
-                imm = ((iword >> FT32_FLD_CB_BIT) & ((1 << FT32_FLD_CB_SIZ) - 1)) << 4;
-                imm |= ((iword >> FT32_FLD_CV_BIT) & ((1 << FT32_FLD_CV_SIZ) - 1));
-                switch (imm)
-                {
-                case 0x00: fpr(stream, "nz");  break;
-                case 0x01: fpr(stream, "z");   break;
-                case 0x10: fpr(stream, "ae");  break;
-                case 0x11: fpr(stream, "b");   break;
-                case 0x20: fpr(stream, "no");  break;
-                case 0x21: fpr(stream, "o");   break;
-                case 0x30: fpr(stream, "ns");  break;
-                case 0x31: fpr(stream, "s");   break;
-                case 0x40: fpr(stream, "lt");  break;
-                case 0x41: fpr(stream, "gte"); break;
-                case 0x50: fpr(stream, "lte"); break;
-                case 0x51: fpr(stream, "gt");  break;
-                case 0x60: fpr(stream, "be");  break;
-                case 0x61: fpr(stream, "a");   break;
-                default:   fpr(stream, "%d,$r30,%d", (imm >> 4), (imm & 1)); break;
-                }
-                break;
-              case  FT32_FLD_CB:
-                imm = (iword >> FT32_FLD_CB_BIT) & ((1 << FT32_FLD_CB_SIZ) - 1);
-                fpr(stream, "%d", imm);
-                break;
-              case  FT32_FLD_R_D:
-                fpr(stream, "$r%d", (iword >> FT32_FLD_R_D_BIT) & 0x1f);
-                break;
-              case  FT32_FLD_CR:
-                imm = (iword >> FT32_FLD_CR_BIT) & ((1 << FT32_FLD_CR_SIZ) - 1);
-                fpr(stream, "$r%d", 28 + imm);
-                break;
-              case  FT32_FLD_CV:
-                imm = (iword >> FT32_FLD_CV_BIT) & ((1 << FT32_FLD_CV_SIZ) - 1);
-                fpr(stream, "%d", imm);
-                break;
-              case  FT32_FLD_R_1:
-                fpr(stream, "$r%d", (iword >> FT32_FLD_R_1_BIT) & 0x1f);
-                break;
-              case  FT32_FLD_RIMM:
-                imm = (iword >> FT32_FLD_RIMM_BIT) & ((1 << FT32_FLD_RIMM_SIZ) - 1);
-                if (imm & 0x400)
-                  fpr(stream, "%d", sign_extend(9, imm));
-                else
-                  fpr(stream, "$r%d", imm & 0x1f);
-                break;
-              case  FT32_FLD_R_2:
-                fpr(stream, "$r%d", (iword >> FT32_FLD_R_2_BIT) & 0x1f);
-                break;
-              case  FT32_FLD_K20:
-                imm = iword & ((1 << FT32_FLD_K20_SIZ) - 1);
-                fpr(stream, "%d", sign_extend(19, imm));
-                break;
-              case  FT32_FLD_PA:
-                imm = (iword & ((1 << FT32_FLD_PA_SIZ) - 1)) << 2;
-                info->print_address_func ((bfd_vma) imm, info);
-                break;
-              case  FT32_FLD_AA:
-                imm = iword & ((1 << FT32_FLD_AA_SIZ) - 1);
-                info->print_address_func ((1 << 23) | (bfd_vma) imm, info);
-                break;
-              case  FT32_FLD_K16:
-                imm = iword & ((1 << FT32_FLD_K16_SIZ) - 1);
-                fpr(stream, "%d", imm);
-                break;
-              case  FT32_FLD_K15:
-                imm = iword & ((1 << FT32_FLD_K15_SIZ) - 1);
-                fpr(stream, "%d", sign_extend(14, imm));
-                break;
-              case  FT32_FLD_R_D_POST:
-                fpr(stream, "$r%d", (iword >> FT32_FLD_R_D_BIT) & 0x1f);
-                break;
-              case  FT32_FLD_R_1_POST:
-                fpr(stream, "$r%d", (iword >> FT32_FLD_R_1_BIT) & 0x1f);
-                break;
-              default:
-                break;
-              }
-              f &= ~lobit;
-              if (f)
-                {
-                  fpr(stream, ",");
-                }
-            }
-        }
+       {
+         int lobit = f & -f;
+         if (f & lobit)
+           {
+             switch (lobit)
+               {
+               case  FT32_FLD_CBCRCV:
+                 /* imm is {CB, CV}  */
+                 imm = ((iword >> FT32_FLD_CB_BIT) & ((1 << FT32_FLD_CB_SIZ) - 1)) << 4;
+                 imm |= ((iword >> FT32_FLD_CV_BIT) & ((1 << FT32_FLD_CV_SIZ) - 1));
+                 switch (imm)
+                   {
+                   case 0x00: fpr (stream, "nz");  break;
+                   case 0x01: fpr (stream, "z");   break;
+                   case 0x10: fpr (stream, "ae");  break;
+                   case 0x11: fpr (stream, "b");   break;
+                   case 0x20: fpr (stream, "no");  break;
+                   case 0x21: fpr (stream, "o");   break;
+                   case 0x30: fpr (stream, "ns");  break;
+                   case 0x31: fpr (stream, "s");   break;
+                   case 0x40: fpr (stream, "lt");  break;
+                   case 0x41: fpr (stream, "gte"); break;
+                   case 0x50: fpr (stream, "lte"); break;
+                   case 0x51: fpr (stream, "gt");  break;
+                   case 0x60: fpr (stream, "be");  break;
+                   case 0x61: fpr (stream, "a");   break;
+                   default:
+                     fpr (stream, "%d,$r30,%d", (imm >> 4), (imm & 1));
+                     break;
+                   }
+                 break;
+               case  FT32_FLD_CB:
+                 imm = (iword >> FT32_FLD_CB_BIT) & ((1 << FT32_FLD_CB_SIZ) - 1);
+                 fpr (stream, "%d", imm);
+                 break;
+               case  FT32_FLD_R_D:
+                 fpr (stream, "$r%d", (iword >> FT32_FLD_R_D_BIT) & 0x1f);
+                 break;
+               case  FT32_FLD_CR:
+                 imm = (iword >> FT32_FLD_CR_BIT) & ((1 << FT32_FLD_CR_SIZ) - 1);
+                 fpr (stream, "$r%d", 28 + imm);
+                 break;
+               case  FT32_FLD_CV:
+                 imm = (iword >> FT32_FLD_CV_BIT) & ((1 << FT32_FLD_CV_SIZ) - 1);
+                 fpr (stream, "%d", imm);
+                 break;
+               case  FT32_FLD_R_1:
+                 fpr (stream, "$r%d", (iword >> FT32_FLD_R_1_BIT) & 0x1f);
+                 break;
+               case  FT32_FLD_RIMM:
+                 imm = (iword >> FT32_FLD_RIMM_BIT) & ((1 << FT32_FLD_RIMM_SIZ) - 1);
+                 if (imm & 0x400)
+                   fpr (stream, "%d", sign_extend (9, imm));
+                 else
+                   fpr (stream, "$r%d", imm & 0x1f);
+                 break;
+               case  FT32_FLD_R_2:
+                 fpr (stream, "$r%d", (iword >> FT32_FLD_R_2_BIT) & 0x1f);
+                 break;
+               case  FT32_FLD_K20:
+                 imm = iword & ((1 << FT32_FLD_K20_SIZ) - 1);
+                 fpr (stream, "%d", sign_extend (19, imm));
+                 break;
+               case  FT32_FLD_PA:
+                 imm = (iword & ((1 << FT32_FLD_PA_SIZ) - 1)) << 2;
+                 info->print_address_func ((bfd_vma) imm, info);
+                 break;
+               case  FT32_FLD_AA:
+                 imm = iword & ((1 << FT32_FLD_AA_SIZ) - 1);
+                 info->print_address_func ((1 << 23) | (bfd_vma) imm, info);
+                 break;
+               case  FT32_FLD_K16:
+                 imm = iword & ((1 << FT32_FLD_K16_SIZ) - 1);
+                 fpr (stream, "%d", imm);
+                 break;
+               case  FT32_FLD_K15:
+                 imm = iword & ((1 << FT32_FLD_K15_SIZ) - 1);
+                 fpr (stream, "%d", sign_extend (14, imm));
+                 break;
+               case  FT32_FLD_R_D_POST:
+                 fpr (stream, "$r%d", (iword >> FT32_FLD_R_D_BIT) & 0x1f);
+                 break;
+               case  FT32_FLD_R_1_POST:
+                 fpr (stream, "$r%d", (iword >> FT32_FLD_R_1_BIT) & 0x1f);
+                 break;
+               default:
+                 break;
+               }
+             f &= ~lobit;
+             if (f)
+               fpr (stream, ",");
+           }
+       }
     }
-    else
+  else
+    fpr (stream, "!");
+}
+
+static void
+ft32_opcode (bfd_vma addr ATTRIBUTE_UNUSED,
+            unsigned int iword,
+            struct disassemble_info *info)
+{
+  unsigned int sc[2];
+  if (ft32_decode_shortcode ((unsigned int) addr, iword, sc))
     {
-      fpr (stream, "!");
+      ft32_opcode1 (sc[0], info);
+      fpr (stream, " ; ");
+      ft32_opcode1 (sc[1], info);
     }
+  else
+    ft32_opcode1 (iword, info);
 }
 
 int
@@ -191,7 +192,7 @@ print_insn_ft32 (bfd_vma addr, struct disassemble_info *info)
 
   fpr (stream, "%08x ", iword);
 
-  ft32_opcode(addr, iword, info);
+  ft32_opcode (addr, iword, info);
 
   return 4;