slocs are not statics
[fw/sdcc] / src / pic / ralloc.c
index 8fcff68bbdd20e44f17e8a02ff3ca7148cd12aa9..95c75778a00e2adf6b699b24f5b52c12c2ab9c14 100644 (file)
@@ -58,6 +58,9 @@ _G;
 int pic14_ptrRegReq;           /* one byte pointer register required */
 
 /* pic14 registers */
+/* A nasty, awful, disgusting hack for register declarations */
+#ifdef p16c84
+
 regs regspic14[] =
 {
 
@@ -84,6 +87,58 @@ regs regspic14[] =
   {REG_PTR, PO_FSR, 4, "FSR", "FSR", 4, 1, 0},
 
 };
+#else
+
+int Gstack_base_addr=0x38; /* The starting address of registers that
+                           * are used to pass and return parameters */
+regs regspic14[] =
+{
+  {REG_GPR, PO_GPR_TEMP, 0x20, "r0x20", "r0x20", 0x20, 1, 0},
+  {REG_GPR, PO_GPR_TEMP, 0x21, "r0x21", "r0x21", 0x21, 1, 0},
+  {REG_GPR, PO_GPR_TEMP, 0x22, "r0x22", "r0x22", 0x22, 1, 0},
+  {REG_GPR, PO_GPR_TEMP, 0x23, "r0x23", "r0x23", 0x23, 1, 0},
+  {REG_GPR, PO_GPR_TEMP, 0x24, "r0x24", "r0x24", 0x24, 1, 0},
+  {REG_GPR, PO_GPR_TEMP, 0x25, "r0x25", "r0x25", 0x25, 1, 0},
+  {REG_GPR, PO_GPR_TEMP, 0x26, "r0x26", "r0x26", 0x26, 1, 0},
+  {REG_GPR, PO_GPR_TEMP, 0x27, "r0x27", "r0x27", 0x27, 1, 0},
+  {REG_GPR, PO_GPR_TEMP, 0x28, "r0x28", "r0x28", 0x28, 1, 0},
+  {REG_GPR, PO_GPR_TEMP, 0x29, "r0x29", "r0x29", 0x29, 1, 0},
+  {REG_GPR, PO_GPR_TEMP, 0x2A, "r0x2A", "r0x2A", 0x2A, 1, 0},
+  {REG_GPR, PO_GPR_TEMP, 0x2B, "r0x2B", "r0x2B", 0x2B, 1, 0},
+  {REG_GPR, PO_GPR_TEMP, 0x2C, "r0x2C", "r0x2C", 0x2C, 1, 0},
+  {REG_GPR, PO_GPR_TEMP, 0x2D, "r0x2D", "r0x2D", 0x2D, 1, 0},
+  {REG_GPR, PO_GPR_TEMP, 0x2E, "r0x2E", "r0x2E", 0x2E, 1, 0},
+  {REG_GPR, PO_GPR_TEMP, 0x2F, "r0x2F", "r0x2F", 0x2F, 1, 0},
+  {REG_GPR, PO_GPR_TEMP, 0x30, "r0x30", "r0x30", 0x30, 1, 0},
+  {REG_GPR, PO_GPR_TEMP, 0x31, "r0x31", "r0x31", 0x31, 1, 0},
+  {REG_GPR, PO_GPR_TEMP, 0x32, "r0x32", "r0x32", 0x32, 1, 0},
+  {REG_GPR, PO_GPR_TEMP, 0x33, "r0x33", "r0x33", 0x33, 1, 0},
+  {REG_GPR, PO_GPR_TEMP, 0x34, "r0x34", "r0x34", 0x34, 1, 0},
+  {REG_GPR, PO_GPR_TEMP, 0x35, "r0x35", "r0x35", 0x35, 1, 0},
+  {REG_GPR, PO_GPR_TEMP, 0x36, "r0x36", "r0x36", 0x36, 1, 0},
+  {REG_GPR, PO_GPR_TEMP, 0x37, "r0x37", "r0x37", 0x37, 1, 0},
+  {REG_STK, PO_GPR_TEMP, 0x38, "r0x38", "r0x38", 0x38, 1, 0},
+  {REG_STK, PO_GPR_TEMP, 0x39, "r0x39", "r0x39", 0x39, 1, 0},
+  {REG_STK, PO_GPR_TEMP, 0x3A, "r0x3A", "r0x3A", 0x3A, 1, 0},
+  {REG_STK, PO_GPR_TEMP, 0x3B, "r0x3B", "r0x3B", 0x3B, 1, 0},
+  {REG_STK, PO_GPR_TEMP, 0x3C, "r0x3C", "r0x3C", 0x3C, 1, 0},
+  {REG_STK, PO_GPR_TEMP, 0x3D, "r0x3D", "r0x3D", 0x3D, 1, 0},
+  {REG_STK, PO_GPR_TEMP, 0x3E, "r0x3E", "r0x3E", 0x3E, 1, 0},
+  {REG_STK, PO_GPR_TEMP, 0x3F, "r0x3F", "r0x3F", 0x3F, 1, 0},
+  {REG_STK, PO_GPR_TEMP, 0x40, "r0x40", "r0x40", 0x40, 1, 0},
+  {REG_STK, PO_GPR_TEMP, 0x41, "r0x41", "r0x41", 0x41, 1, 0},
+  {REG_STK, PO_GPR_TEMP, 0x42, "r0x42", "r0x42", 0x42, 1, 0},
+  {REG_STK, PO_GPR_TEMP, 0x43, "r0x43", "r0x43", 0x43, 1, 0},
+  {REG_STK, PO_GPR_TEMP, 0x44, "r0x44", "r0x44", 0x44, 1, 0},
+  {REG_STK, PO_GPR_TEMP, 0x45, "r0x45", "r0x45", 0x45, 1, 0},
+  {REG_STK, PO_GPR_TEMP, 0x46, "r0x46", "r0x46", 0x46, 1, 0},
+  {REG_STK, PO_GPR_TEMP, 0x47, "r0x47", "r0x47", 0x47, 1, 0},
+
+  {REG_PTR, PO_FSR, 4, "FSR", "FSR", 4, 1, 0},
+
+};
+
+#endif
 
 int pic14_nRegs = sizeof (regspic14) / sizeof (regs);
 static void spillThis (symbol *);
@@ -463,14 +518,14 @@ pic14_regWithIdx (int idx)
 {
   int i;
 
-  debugLog ("%s\n", __FUNCTION__);
+  debugLog ("%s - requesting index = 0x%x\n", __FUNCTION__,idx);
 
   for (i = 0; i < pic14_nRegs; i++)
     if (regspic14[i].rIdx == idx)
       return &regspic14[i];
 
-  return &regspic14[0];
-
+  //return &regspic14[0];
+  fprintf(stderr,"%s %d - requested register: 0x%x\n",__FUNCTION__,__LINE__,idx);
   werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
          "regWithIdx not found");
   exit (1);
@@ -479,14 +534,17 @@ pic14_regWithIdx (int idx)
 /*-----------------------------------------------------------------*/
 /*-----------------------------------------------------------------*/
 regs *
-pic14_findFreeReg(void)
+pic14_findFreeReg(short type)
 {
   int i;
 
-  for (i = 0; i < pic14_nRegs; i++)
-    if (regspic14[i].isFree)
+  for (i = 0; i < pic14_nRegs; i++) {
+    if (!type && regspic14[i].isFree)
       return &regspic14[i];
-
+    if (regspic14[i].isFree &&
+       regspic14[i].type == type)
+      return &regspic14[i];
+  }
   return NULL;
 }
 /*-----------------------------------------------------------------*/
@@ -886,6 +944,7 @@ createStackSpil (symbol * sym)
   sloc->etype = getSpec (sloc->type);
   SPEC_SCLS (sloc->etype) = S_DATA;
   SPEC_EXTR (sloc->etype) = 0;
+  SPEC_STAT (sloc->etype) = 0;
 
   /* we don't allow it to be allocated`
      onto the external stack since : so we
@@ -1573,6 +1632,8 @@ serialRegAssign (eBBlock ** ebbs, int count)
              _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
 
              debugLog ("  %d - \n", __LINE__);
+             if(debugF) 
+               bitVectDebugOn(_G.regAssigned, debugF);
 
              for (j = 0; j < sym->nRegs; j++)
                {
@@ -1820,102 +1881,97 @@ regTypeNum ()
   debugLog ("%s\n", __FUNCTION__);
   /* for each live range do */
   for (sym = hTabFirstItem (liveRanges, &k); sym;
-       sym = hTabNextItem (liveRanges, &k))
-    {
-
-      debugLog ("  %d - %s\n", __LINE__, sym->rname);
-
-      /* if used zero times then no registers needed */
-      if ((sym->liveTo - sym->liveFrom) == 0)
-       continue;
+       sym = hTabNextItem (liveRanges, &k)) {
 
+    debugLog ("  %d - %s\n", __LINE__, sym->rname);
 
-      /* if the live range is a temporary */
-      if (sym->isitmp)
-       {
+    /* if used zero times then no registers needed */
+    if ((sym->liveTo - sym->liveFrom) == 0)
+      continue;
 
-         debugLog ("  %d - \n", __LINE__);
 
-         /* if the type is marked as a conditional */
-         if (sym->regType == REG_CND)
-           continue;
+    /* if the live range is a temporary */
+    if (sym->isitmp) {
 
-         /* if used in return only then we don't 
-            need registers */
-         if (sym->ruonly || sym->accuse)
-           {
-             if (IS_AGGREGATE (sym->type) || sym->isptr)
-               sym->type = aggrToPtr (sym->type, FALSE);
-             debugLog ("  %d - \n", __LINE__);
+      debugLog ("  %d - itemp register\n", __LINE__);
 
-             continue;
-           }
+      /* if the type is marked as a conditional */
+      if (sym->regType == REG_CND)
+       continue;
 
-         /* if the symbol has only one definition &
-            that definition is a get_pointer and the
-            pointer we are getting is rematerializable and
-            in "data" space */
-
-         if (bitVectnBitsOn (sym->defs) == 1 &&
-             (ic = hTabItemWithKey (iCodehTab,
-                                    bitVectFirstBit (sym->defs))) &&
-             POINTER_GET (ic) &&
-             !sym->noSpilLoc &&
-             !IS_BITVAR (sym->etype))
-           {
+      /* if used in return only then we don't 
+        need registers */
+      if (sym->ruonly || sym->accuse) {
+       if (IS_AGGREGATE (sym->type) || sym->isptr)
+         sym->type = aggrToPtr (sym->type, FALSE);
+       debugLog ("  %d - no reg needed - used as a return\n", __LINE__);
 
-             debugLog ("  %d - \n", __LINE__);
+       continue;
+      }
 
-             /* if remat in data space */
-             if (OP_SYMBOL (IC_LEFT (ic))->remat &&
-                 DCL_TYPE (aggrToPtr (sym->type, FALSE)) == POINTER)
-               {
+      /* if the symbol has only one definition &
+        that definition is a get_pointer and the
+        pointer we are getting is rematerializable and
+        in "data" space */
+
+      if (bitVectnBitsOn (sym->defs) == 1 &&
+         (ic = hTabItemWithKey (iCodehTab,
+                                bitVectFirstBit (sym->defs))) &&
+         POINTER_GET (ic) &&
+         !sym->noSpilLoc &&
+         !IS_BITVAR (sym->etype)) {
+       
+
+       debugLog ("  %d - \n", __LINE__);
+
+       /* if remat in data space */
+       if (OP_SYMBOL (IC_LEFT (ic))->remat &&
+           DCL_TYPE (aggrToPtr (sym->type, FALSE)) == POINTER) {
+
+         /* create a psuedo symbol & force a spil */
+         symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
+         psym->type = sym->type;
+         psym->etype = sym->etype;
+         strcpy (psym->rname, psym->name);
+         sym->isspilt = 1;
+         sym->usl.spillLoc = psym;
+         continue;
+       }
 
-                 /* create a psuedo symbol & force a spil */
-                 symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
-                 psym->type = sym->type;
-                 psym->etype = sym->etype;
-                 strcpy (psym->rname, psym->name);
-                 sym->isspilt = 1;
-                 sym->usl.spillLoc = psym;
-                 continue;
-               }
+       /* if in data space or idata space then try to
+          allocate pointer register */
 
-             /* if in data space or idata space then try to
-                allocate pointer register */
+      }
 
-           }
+      /* if not then we require registers */
+      sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
+                   getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
+                   getSize (sym->type));
 
-         /* if not then we require registers */
-         sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
-                       getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
-                       getSize (sym->type));
+      if (sym->nRegs > 4) {
+       fprintf (stderr, "allocated more than 4 or 0 registers for type ");
+       printTypeChain (sym->type, stderr);
+       fprintf (stderr, "\n");
+      }
 
-         if (sym->nRegs > 4)
-           {
-             fprintf (stderr, "allocated more than 4 or 0 registers for type ");
-             printTypeChain (sym->type, stderr);
-             fprintf (stderr, "\n");
-           }
+      /* determine the type of register required */
+      if (sym->nRegs == 1 &&
+         IS_PTR (sym->type) &&
+         sym->uptr)
+       sym->regType = REG_PTR;
+      else
+       sym->regType = REG_GPR;
 
-         debugLog ("  %d - \n", __LINE__);
 
-         /* determine the type of register required */
-         if (sym->nRegs == 1 &&
-             IS_PTR (sym->type) &&
-             sym->uptr)
-           sym->regType = REG_PTR;
-         else
-           sym->regType = REG_GPR;
-         debugLog ("  reg type %s\n", debugLogRegType (sym->regType));
+      debugLog ("  reg name %s,  reg type %s\n", sym->rname, debugLogRegType (sym->regType));
 
-       }
-      else
-       /* for the first run we don't provide */
-       /* registers for true symbols we will */
-       /* see how things go                  */
-       sym->nRegs = 0;
     }
+    else
+      /* for the first run we don't provide */
+      /* registers for true symbols we will */
+      /* see how things go                  */
+      sym->nRegs = 0;
+  }
 
 }
 
@@ -2047,6 +2103,7 @@ packRegsForAssign (iCode * ic, eBBlock * ebp)
       OP_SYMBOL (IC_RIGHT (ic))->isind ||
       OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
     {
+      debugLog ("  %d - not packing - right side fails \n", __LINE__);
       return 0;
     }
 
@@ -2750,6 +2807,8 @@ packRegisters (eBBlock * ebp)
          !OP_SYMBOL (IC_LEFT (ic))->onStack)
        {
 
+         debugLog ("  %d - %s. result is rematerializable\n", __LINE__,__FUNCTION__);
+
          OP_SYMBOL (IC_RESULT (ic))->remat = 1;
          OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
          OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
@@ -2764,6 +2823,7 @@ packRegisters (eBBlock * ebp)
          OP_SYMBOL (IC_RIGHT (ic))->remat &&
          bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
        {
+         debugLog ("  %d - %s. straight rematerializable\n", __LINE__,__FUNCTION__);
 
          OP_SYMBOL (IC_RESULT (ic))->remat =
            OP_SYMBOL (IC_RIGHT (ic))->remat;
@@ -2780,7 +2840,7 @@ packRegisters (eBBlock * ebp)
           bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
           IS_OP_LITERAL (IC_RIGHT (ic))))
        {
-
+         debugLog ("  %d - %s. rematerializable because op is +/-\n", __LINE__,__FUNCTION__);
          //int i = 
          operandLitValue (IC_RIGHT (ic));
          OP_SYMBOL (IC_RESULT (ic))->remat = 1;
@@ -2792,12 +2852,14 @@ packRegisters (eBBlock * ebp)
       if (POINTER_SET (ic))
        {
          OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
-         debugLog ("  marking as a pointer (set)\n");
+         debugLog ("  marking as a pointer (set) =>");
+         debugAopGet ("  result:", IC_RESULT (ic));
        }
       if (POINTER_GET (ic))
        {
          OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
-         debugLog ("  marking as a pointer (get)\n");
+         debugLog ("  marking as a pointer (get) =>");
+         debugAopGet ("  left:", IC_LEFT (ic));
        }
 
       if (!SKIP_IC2 (ic))