Hacked const and volatile modifiers a bit
[fw/sdcc] / src / SDCCpeeph.c
index ada11ff9e4a431c3adda955b18b16f6d431af2cf..37e349c6853d1ae6ec8b841d96818e1617507cc3 100644 (file)
@@ -66,7 +66,7 @@ pcDistance (lineNode * cpos, char *lbl, bool back)
   char buff[MAX_PATTERN_LEN];
   int dist = 0;
 
-  sprintf (buff, "%s:", lbl);
+  SNPRINTF (buff, sizeof(buff), "%s:", lbl);
   while (pl)
     {
 
@@ -89,6 +89,23 @@ pcDistance (lineNode * cpos, char *lbl, bool back)
   return 0;
 }
 
+/*-----------------------------------------------------------------*/
+/* flat24bitModeAndPortDS390 -                                            */
+/*-----------------------------------------------------------------*/
+FBYNAME (flat24bitModeAndPortDS390)
+{
+    return ((strcmp(port->target,"ds390") == 0) && 
+           (options.model == MODEL_FLAT24));
+}
+
+/*-----------------------------------------------------------------*/
+/* portIsDS390 - return true if port is DS390                             */
+/*-----------------------------------------------------------------*/
+FBYNAME (portIsDS390)
+{
+    return (strcmp(port->target,"ds390") == 0);
+}
+
 /*-----------------------------------------------------------------*/
 /* flat24bitMode - will check to see if we are in flat24 mode      */
 /*-----------------------------------------------------------------*/
@@ -97,6 +114,14 @@ FBYNAME (flat24bitMode)
   return (options.model == MODEL_FLAT24);
 }
 
+/*-----------------------------------------------------------------*/
+/* xramMovcOption - check if using movc to read xram               */
+/*-----------------------------------------------------------------*/
+FBYNAME (xramMovcOption)
+{
+  return (options.xram_movc && (strcmp(port->target,"mcs51") == 0));
+}
+
 /*-----------------------------------------------------------------*/
 /* labelInRange - will check to see if label %5 is within range    */
 /*-----------------------------------------------------------------*/
@@ -150,6 +175,190 @@ FBYNAME (operandsNotSame)
     return TRUE;
 }
 
+/*-----------------------------------------------------------------*/
+/* operandsNotSame3- check if any pair of %1,%2,%3 are the same    */
+/*-----------------------------------------------------------------*/
+FBYNAME (operandsNotSame3)
+{
+  char *op1 = hTabItemWithKey (vars, 1);
+  char *op2 = hTabItemWithKey (vars, 2);
+  char *op3 = hTabItemWithKey (vars, 3);
+
+  if ( (strcmp (op1, op2) == 0) ||
+       (strcmp (op1, op3) == 0) ||
+       (strcmp (op2, op3) == 0) )
+    return FALSE;
+  else
+    return TRUE;
+}
+
+/*-----------------------------------------------------------------*/
+/* operandsNotSame4- check if any pair of %1,%2,%3,.. are the same */
+/*-----------------------------------------------------------------*/
+FBYNAME (operandsNotSame4)
+{
+  char *op1 = hTabItemWithKey (vars, 1);
+  char *op2 = hTabItemWithKey (vars, 2);
+  char *op3 = hTabItemWithKey (vars, 3);
+  char *op4 = hTabItemWithKey (vars, 4);
+
+  if ( (strcmp (op1, op2) == 0) ||
+       (strcmp (op1, op3) == 0) ||
+       (strcmp (op1, op4) == 0) ||
+       (strcmp (op2, op3) == 0) ||
+       (strcmp (op2, op4) == 0) ||
+       (strcmp (op3, op4) == 0) )
+    return FALSE;
+  else
+    return TRUE;
+}
+
+/*-----------------------------------------------------------------*/
+/* operandsNotSame5- check if any pair of %1,%2,%3,.. are the same */
+/*-----------------------------------------------------------------*/
+FBYNAME (operandsNotSame5)
+{
+  char *op1 = hTabItemWithKey (vars, 1);
+  char *op2 = hTabItemWithKey (vars, 2);
+  char *op3 = hTabItemWithKey (vars, 3);
+  char *op4 = hTabItemWithKey (vars, 4);
+  char *op5 = hTabItemWithKey (vars, 5);
+
+  if ( (strcmp (op1, op2) == 0) ||
+       (strcmp (op1, op3) == 0) ||
+       (strcmp (op1, op4) == 0) ||
+       (strcmp (op1, op5) == 0) ||
+       (strcmp (op2, op3) == 0) ||
+       (strcmp (op2, op4) == 0) ||
+       (strcmp (op2, op5) == 0) ||
+       (strcmp (op3, op4) == 0) ||
+       (strcmp (op3, op5) == 0) ||
+       (strcmp (op4, op5) == 0) )
+    return FALSE;
+  else
+    return TRUE;
+}
+
+/*-----------------------------------------------------------------*/
+/* operandsNotSame6- check if any pair of %1,%2,%3,.. are the same */
+/*-----------------------------------------------------------------*/
+FBYNAME (operandsNotSame6)
+{
+  char *op1 = hTabItemWithKey (vars, 1);
+  char *op2 = hTabItemWithKey (vars, 2);
+  char *op3 = hTabItemWithKey (vars, 3);
+  char *op4 = hTabItemWithKey (vars, 4);
+  char *op5 = hTabItemWithKey (vars, 5);
+  char *op6 = hTabItemWithKey (vars, 6);
+
+  if ( (strcmp (op1, op2) == 0) ||
+       (strcmp (op1, op3) == 0) ||
+       (strcmp (op1, op4) == 0) ||
+       (strcmp (op1, op5) == 0) ||
+       (strcmp (op1, op6) == 0) ||
+       (strcmp (op2, op3) == 0) ||
+       (strcmp (op2, op4) == 0) ||
+       (strcmp (op2, op5) == 0) ||
+       (strcmp (op2, op6) == 0) ||
+       (strcmp (op3, op4) == 0) ||
+       (strcmp (op3, op5) == 0) ||
+       (strcmp (op3, op6) == 0) ||
+       (strcmp (op4, op5) == 0) ||
+       (strcmp (op4, op6) == 0) ||
+       (strcmp (op5, op6) == 0) )
+    return FALSE;
+  else
+    return TRUE;
+}
+
+
+/*-----------------------------------------------------------------*/
+/* operandsNotSame7- check if any pair of %1,%2,%3,.. are the same */
+/*-----------------------------------------------------------------*/
+FBYNAME (operandsNotSame7)
+{
+  char *op1 = hTabItemWithKey (vars, 1);
+  char *op2 = hTabItemWithKey (vars, 2);
+  char *op3 = hTabItemWithKey (vars, 3);
+  char *op4 = hTabItemWithKey (vars, 4);
+  char *op5 = hTabItemWithKey (vars, 5);
+  char *op6 = hTabItemWithKey (vars, 6);
+  char *op7 = hTabItemWithKey (vars, 7);
+
+  if ( (strcmp (op1, op2) == 0) ||
+       (strcmp (op1, op3) == 0) ||
+       (strcmp (op1, op4) == 0) ||
+       (strcmp (op1, op5) == 0) ||
+       (strcmp (op1, op6) == 0) ||
+       (strcmp (op1, op7) == 0) ||
+       (strcmp (op2, op3) == 0) ||
+       (strcmp (op2, op4) == 0) ||
+       (strcmp (op2, op5) == 0) ||
+       (strcmp (op2, op6) == 0) ||
+       (strcmp (op2, op7) == 0) ||
+       (strcmp (op3, op4) == 0) ||
+       (strcmp (op3, op5) == 0) ||
+       (strcmp (op3, op6) == 0) ||
+       (strcmp (op3, op7) == 0) ||
+       (strcmp (op4, op5) == 0) ||
+       (strcmp (op4, op6) == 0) ||
+       (strcmp (op4, op7) == 0) ||
+       (strcmp (op5, op6) == 0) ||
+       (strcmp (op5, op7) == 0) ||
+       (strcmp (op6, op7) == 0) )
+    return FALSE;
+  else
+    return TRUE;
+}
+
+/*-----------------------------------------------------------------*/
+/* operandsNotSame8- check if any pair of %1,%2,%3,.. are the same */
+/*-----------------------------------------------------------------*/
+FBYNAME (operandsNotSame8)
+{
+  char *op1 = hTabItemWithKey (vars, 1);
+  char *op2 = hTabItemWithKey (vars, 2);
+  char *op3 = hTabItemWithKey (vars, 3);
+  char *op4 = hTabItemWithKey (vars, 4);
+  char *op5 = hTabItemWithKey (vars, 5);
+  char *op6 = hTabItemWithKey (vars, 6);
+  char *op7 = hTabItemWithKey (vars, 7);
+  char *op8 = hTabItemWithKey (vars, 8);
+
+  if ( (strcmp (op1, op2) == 0) ||
+       (strcmp (op1, op3) == 0) ||
+       (strcmp (op1, op4) == 0) ||
+       (strcmp (op1, op5) == 0) ||
+       (strcmp (op1, op6) == 0) ||
+       (strcmp (op1, op7) == 0) ||
+       (strcmp (op1, op8) == 0) ||
+       (strcmp (op2, op3) == 0) ||
+       (strcmp (op2, op4) == 0) ||
+       (strcmp (op2, op5) == 0) ||
+       (strcmp (op2, op6) == 0) ||
+       (strcmp (op2, op7) == 0) ||
+       (strcmp (op2, op8) == 0) ||
+       (strcmp (op3, op4) == 0) ||
+       (strcmp (op3, op5) == 0) ||
+       (strcmp (op3, op6) == 0) ||
+       (strcmp (op3, op7) == 0) ||
+       (strcmp (op3, op8) == 0) ||
+       (strcmp (op4, op5) == 0) ||
+       (strcmp (op4, op6) == 0) ||
+       (strcmp (op4, op7) == 0) ||
+       (strcmp (op4, op8) == 0) ||
+       (strcmp (op5, op6) == 0) ||
+       (strcmp (op5, op7) == 0) ||
+       (strcmp (op5, op8) == 0) ||
+       (strcmp (op6, op7) == 0) ||
+       (strcmp (op6, op8) == 0) ||
+       (strcmp (op7, op8) == 0) )
+    return FALSE;
+  else
+    return TRUE;
+}
+
+
 /* labelRefCount:
 
  * takes two parameters: a variable (bound to a label name)
@@ -245,25 +454,83 @@ callFuncByName (char *fname,
       "operandsNotSame", operandsNotSame
     }
     ,
+    {
+      "operandsNotSame3", operandsNotSame3
+    }
+    ,
+    {
+      "operandsNotSame4", operandsNotSame4
+    }
+    ,
+    {
+      "operandsNotSame5", operandsNotSame5
+    }
+    ,
+    {
+      "operandsNotSame6", operandsNotSame6
+    }
+    ,
+    {
+      "operandsNotSame7", operandsNotSame7
+    }
+    ,
+    {
+      "operandsNotSame8", operandsNotSame8
+    }
+    ,    
     {
       "24bitMode", flat24bitMode
     }
     ,
+    {
+      "xramMovcOption", xramMovcOption
+    }
+    ,
     {
       "labelRefCount", labelRefCount
     }
     ,
+    {
+      "portIsDS390", portIsDS390
+    },
+    {
+      "24bitModeAndPortDS390", flat24bitModeAndPortDS390
+    }
   };
-  int i;
+  int  i;
+  char  *cmdCopy, *funcName, *funcArgs;
+  int  rc = -1;
+    
+  /* Isolate the function name part (we are passed the full condition 
+   * string including arguments) 
+   */
+  cmdCopy = Safe_strdup(fname);
+  funcName = strtok(cmdCopy, " \t");
+  funcArgs = strtok(NULL, "");
 
-  for (i = 0; i < ((sizeof (ftab)) / (sizeof (struct ftab))); i++)
-    if (strncmp (ftab[i].fname, fname, strlen (ftab[i].fname)) == 0)
-      {
-       return (*ftab[i].func) (vars, currPl, head,
-                               fname + strlen (ftab[i].fname));
-      }
-  fprintf (stderr, "could not find named function in function table\n");
-  return TRUE;
+    for (i = 0; i < ((sizeof (ftab)) / (sizeof (struct ftab))); i++)
+    {
+       if (strcmp (ftab[i].fname, funcName) == 0)
+       {
+           rc = (*ftab[i].func) (vars, currPl, head,
+                                 funcArgs);
+       }
+    }
+    
+    Safe_free(cmdCopy);
+    
+    if (rc == -1)
+    {
+       fprintf (stderr, 
+                "could not find named function \"%s\" in "
+                "peephole function table\n",
+                funcName);
+        // If the function couldn't be found, let's assume it's
+       // a bad rule and refuse it.
+       rc = FALSE;
+    }
+    
+  return rc;
 }
 
 /*-----------------------------------------------------------------*/
@@ -311,8 +578,7 @@ newPeepRule (lineNode * match,
 
   if (cond && *cond)
     {
-      pr->cond = Safe_alloc ( strlen (cond) + 1);
-      strcpy (pr->cond, cond);
+      pr->cond = Safe_strdup (cond);
     }
   else
     pr->cond = NULL;
@@ -337,8 +603,7 @@ newLineNode (char *line)
   lineNode *pl;
 
   pl = Safe_alloc ( sizeof (lineNode));
-  pl->line = Safe_alloc ( strlen (line) + 1);
-  strcpy (pl->line, line);
+  pl->line = Safe_strdup (line);
   return pl;
 }
 
@@ -565,6 +830,8 @@ bindVar (int key, char **s, hTab ** vtab)
              if (*vvx == ')')
                ubb--;
            }
+         // include the trailing ')'
+         *vv++ = *vvx++;
        }
       else
        *vv++ = *vvx++;
@@ -572,8 +839,7 @@ bindVar (int key, char **s, hTab ** vtab)
   *s = vvx;
   *vv = '\0';
   /* got value */
-  vvx = traceAlloc (&_G.values, Safe_alloc(strlen (vval) + 1));
-  strcpy (vvx, vval);
+  vvx = traceAlloc (&_G.values, Safe_strdup(vval));
 
   hTabAddItem (vtab, key, vvx);
 }
@@ -748,6 +1014,7 @@ replaceRule (lineNode ** shead, lineNode * stail, peepRule * pr)
       lbp = lb;
 
       l = pl->line;
+
       while (*l)
        {
          /* if the line contains a variable */
@@ -798,8 +1065,7 @@ replaceRule (lineNode ** shead, lineNode * stail, peepRule * pr)
       (*shead)->prev->next = lhead;
       lhead->prev = (*shead)->prev;
     }
-  else
-    *shead = lhead;
+  *shead = lhead;
   /* now for the tail */
   if (stail && stail->next)
     {
@@ -973,6 +1239,12 @@ peepHole (lineNode ** pls)
   lineNode *mtail = NULL;
   bool restart;
 
+#if !OPT_DISABLE_PIC
+  /* The PIC port uses a different peep hole optimizer based on "pCode" */
+  if (TARGET_IS_PIC)
+    return;
+#endif
+
   assert(labelHash == NULL);
 
   do
@@ -982,8 +1254,6 @@ peepHole (lineNode ** pls)
       /* for all rules */
       for (pr = rootRules; pr; pr = pr->next)
         {
-          fflush(stdout);
-          
           for (spl = *pls; spl; spl = spl->next)
             {
               /* if inline assembler then no peep hole */
@@ -991,7 +1261,7 @@ peepHole (lineNode ** pls)
                 continue;
               
               mtail = NULL;
-              
+
               /* Tidy up any data stored in the hTab */
               
               /* if it matches */
@@ -1063,12 +1333,11 @@ readFileIntoBuffer (char *fname)
          if (rs)
            {
              rs = Safe_realloc (rs, strlen (rs) + strlen (lb) + 1);
-             strcat (rs, lb);
+             strncatz (rs, lb,  strlen (rs) + strlen (lb) + 1);
            }
          else
            {
-             rs = Safe_alloc ( strlen (lb) + 1);
-             strcpy (rs, lb);
+             rs = Safe_strdup (lb);
            }
          nch = 0;
        }
@@ -1082,12 +1351,11 @@ readFileIntoBuffer (char *fname)
       if (rs)
        {
          rs = Safe_realloc (rs, strlen (rs) + strlen (lb) + 1);
-         strcat (rs, lb);
+         strncatz (rs, lb, strlen (rs) + strlen (lb) + 1);
        }
       else
        {
-         rs = Safe_alloc ( strlen (lb) + 1);
-         strcpy (rs, lb);
+         rs = Safe_strdup (lb);
        }
     }
   return rs;