* .version: bumped version number to 2.4.8
[fw/sdcc] / src / pic16 / ralloc.c
index 0fec45041ddb85bb4eeb038038336b8081022444..bfeeac608e7f3381765908abf2b47a3b9dfe31c4 100644 (file)
 #define STRCASECMP strcasecmp
 #endif
 
+#ifndef debugf
+#define debugf(frm, rest)       _debugf(__FILE__, __LINE__, frm, rest)
+#endif
+void _debugf(char *f, int l, char *frm, ...);
+
+#define NEWREG_DEBUG   0
 //#define USE_ONSTACK
 
 
@@ -103,7 +109,9 @@ int pic16_Gstack_base_addr=0; /* The starting address of registers that
                         * are used to pass and return parameters */
 
 
-
+int _inRegAllocator=0; /* flag that marks whther allocReg happens while
+                         * inside the register allocator function */
+                         
 
 static void spillThis (symbol *);
 int pic16_ralloc_debug = 0;
@@ -195,8 +203,8 @@ debugAopGet (char *str, operand * op)
   return NULL;
 }
 
-static char *
-decodeOp (unsigned int op)
+char *
+pic16_decodeOp (unsigned int op)
 {
        if (op < 128 && op > ' ') {
                buffer[0] = (op & 0xff);
@@ -382,8 +390,9 @@ regs* newReg(short type, short pc_type, int rIdx, char *name, int size, int alia
                dReg->accessBank = 0;
        }
 
-//     fprintf(stderr,"newReg: %s, rIdx = 0x%02x\taccess= %d\tregop= %p\n",dReg->name,rIdx, dReg->accessBank, refop);
-
+#if NEWREG_DEBUG
+       fprintf(stderr,"newReg: %s, rIdx = 0x%02x\taccess= %d\tregop= %p\n",dReg->name,rIdx, dReg->accessBank, refop);
+#endif
        dReg->size = size;
        dReg->alias = alias;
        dReg->reg_alias = NULL;
@@ -486,6 +495,8 @@ pic16_allocInternalRegister(int rIdx, char * name, short po_type, int alias)
 
   return NULL;
 }
+
+
 /*-----------------------------------------------------------------*/
 /* allocReg - allocates register of given type                     */
 /*-----------------------------------------------------------------*/
@@ -494,6 +505,9 @@ allocReg (short type)
 {
   regs * reg=NULL;
   
+#define MAX_P16_NREGS  6
+
+
 #if 0
   if(dynrIdx > pic16_nRegs)
        return NULL;
@@ -503,11 +517,20 @@ allocReg (short type)
        reg = regFindFree( pic16_dynAllocRegs );
 
        if(reg) {
-//             fprintf(stderr, "%s: found FREE register %s\n", __FILE__, reg->name);
+//             fprintf(stderr, "%s: [%s] found FREE register %s, rIdx: %d\n", __FILE__, (_inRegAllocator)?"ralloc":"", reg->name, reg->rIdx);
        }
 
        if(!reg) {
                reg = newReg(REG_GPR, PO_GPR_TEMP, dynrIdx++, NULL, 1, 0, NULL);
+//             fprintf(stderr, "%s: [%s] allocating NEW register %s, rIdx: %d\n", __FILE__, (_inRegAllocator)?"ralloc":"", reg->name, reg->rIdx);
+
+#if 1
+                if(_inRegAllocator && (dynrIdx > MAX_P16_NREGS)) {
+//                  debugf("allocating more registers than available\n", 0);
+//                  return (NULL);
+                }
+#endif
+
 //             addSet(&pic16_dynAllocRegs, reg);
        }
 
@@ -518,9 +541,10 @@ allocReg (short type)
 
        debugLog ("%s of type %s for register rIdx: %d (0x%x)\n", __FUNCTION__, debugLogRegType (type), dynrIdx-1, dynrIdx-1);
 
-//     fprintf(stderr,"%s:%d: %s\t%s addr= 0x%x\trIdx= 0x%02x isFree: %d\n",
-//             __FILE__, __LINE__, __FUNCTION__, reg->name, reg->address, reg->rIdx, reg->isFree);
-
+#if 0
+       fprintf(stderr,"%s:%d: %s\t%s addr= 0x%x\trIdx= 0x%02x isFree: %d\n",
+               __FILE__, __LINE__, __FUNCTION__, reg->name, reg->address, reg->rIdx, reg->isFree);
+#endif
        if(reg) {
                reg->accessBank = 1;    /* this is a temporary register alloc in accessBank */
                reg->isLocal = 1;       /* this is a local frame register */
@@ -754,8 +778,12 @@ pic16_allocDirReg (operand *op )
 
        if(IS_ITEMP(op))return NULL;
        
+//     if(IS_STATIC(OP_SYM_ETYPE(op)))return NULL;
+       
+       if(IN_STACK(OP_SYM_ETYPE(op)))return NULL;
+       
        debugLog ("%s:%d symbol name %s\n", __FUNCTION__, __LINE__, name);
-//     fprintf(stderr, "%s symbol name %s\n", __FUNCTION__,name);
+//     fprintf(stderr, "%s symbol name %s\tSTATIC:%d\n", __FUNCTION__,name, IS_STATIC(OP_SYM_ETYPE(op)));
 
        {
                if(SPEC_CONST ( OP_SYM_ETYPE(op)) && (IS_CHAR ( OP_SYM_ETYPE(op)) )) {
@@ -859,7 +887,12 @@ pic16_allocDirReg (operand *op )
 //                     fprintf(stderr, "%s:%d adding %s in direct registers\n", __FILE__, __LINE__, reg->name);
 //                     addSet(&pic16_dynDirectRegs, reg);
 
-                       checkAddReg(&pic16_dynDirectRegs, reg);
+#if 1
+                  if(!(IS_STATIC(OP_SYM_ETYPE(op))
+                      && OP_SYMBOL(op)->ival
+                  ))
+#endif
+                    checkAddReg(&pic16_dynDirectRegs, reg);
                }
        
        } else {
@@ -2183,14 +2216,12 @@ static void
 serialRegAssign (eBBlock ** ebbs, int count)
 {
   int i;
-
+  iCode *ic;
+  
   debugLog ("%s\n", __FUNCTION__);
   /* for all blocks */
   for (i = 0; i < count; i++)
     {
-
-      iCode *ic;
-
       if (ebbs[i]->noPath &&
          (ebbs[i]->entryLabel != entryLabel &&
           ebbs[i]->entryLabel != returnLabel))
@@ -2200,7 +2231,7 @@ serialRegAssign (eBBlock ** ebbs, int count)
       for (ic = ebbs[i]->sch; ic; ic = ic->next)
        {
 
-         debugLog ("  op: %s\n", decodeOp (ic->op));
+         debugLog ("  op: %s\n", pic16_decodeOp (ic->op));
 
                if(IC_RESULT(ic) && !IS_ITEMP( IC_RESULT(ic)))
                        pic16_allocDirReg(IC_RESULT(ic));
@@ -2313,9 +2344,22 @@ serialRegAssign (eBBlock ** ebbs, int count)
              if (ic->op == RECEIVE)
                debugLog ("When I get clever, I'll optimize the receive logic\n");
 
+              if(POINTER_GET(ic) && IS_BITFIELD(getSpec(operandType(IC_RESULT(ic))))
+                && (SPEC_BLEN(getSpec(operandType(IC_RESULT(ic))))==1)
+                && (ic->next->op == IFX)
+                && (OP_LIVETO(IC_RESULT(ic)) == ic->next->seq)) {
+
+                /* skip register allocation since none will be used */
+                for(j=0;j<sym->nRegs;j++)
+                  sym->regs[j] = newReg(REG_TMP, PO_GPR_TEMP, 0, "bad", 1, 0, NULL);
+//                OP_SYMBOL(IC_RESULT(ic))->nRegs = 0;
+                
+                continue;
+              }
+
              /* if we need ptr regs for the right side
                 then mark it */
-             if (POINTER_GET (ic) && getSize (OP_SYMBOL (IC_LEFT (ic))->type)
+             if (POINTER_GET (ic) && IS_SYMOP( IC_LEFT(ic) ) && getSize (OP_SYMBOL (IC_LEFT (ic))->type)
                  <= (unsigned) PTRSIZE)
                {
                  pic16_ptrRegReq++;
@@ -2324,7 +2368,6 @@ serialRegAssign (eBBlock ** ebbs, int count)
              /* else we assign registers to it */
              _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
 
-             debugLog ("  %d - nRegs: %d\n", __LINE__, sym->nRegs);
              if(debugF) 
                bitVectDebugOn(_G.regAssigned, debugF);
 
@@ -2677,7 +2720,6 @@ regTypeNum ()
        if (IS_AGGREGATE (sym->type) || sym->isptr)
          sym->type = aggrToPtr (sym->type, FALSE);
        debugLog ("  %d - no reg needed - used as a return\n", __LINE__);
-
        continue;
       }
 
@@ -2755,6 +2797,7 @@ regTypeNum ()
       /* registers for true symbols we will */
       /* see how things go                  */
       sym->nRegs = 0;
+      
   }
 
 }
@@ -2887,7 +2930,7 @@ packRegsForAssign (iCode * ic, eBBlock * ebp)
   iCode *dic, *sic;
 
   debugLog ("%d\t%s\n", __LINE__, __FUNCTION__);
-  debugLog ("ic->op = %s\n", decodeOp( ic->op ) );
+  debugLog ("ic->op = %s\n", pic16_decodeOp( ic->op ) );
   debugAopGet ("  result:", IC_RESULT (ic));
   debugAopGet ("  left:", IC_LEFT (ic));
   debugAopGet ("  right:", IC_RIGHT (ic));
@@ -2901,10 +2944,11 @@ packRegsForAssign (iCode * ic, eBBlock * ebp)
     debugLog ("  %d - result is not temp\n", __LINE__);
   }
 
+//  if(IS_VALOP(IC_RIGHT(ic)))return 0;
 
 /* See BUGLOG0001 - VR */
 #if 1
-  if (!IS_ITEMP (IC_RIGHT (ic))) {
+  if (!IS_ITEMP (IC_RIGHT (ic)) /*&& (!IS_PARM(IC_RESULT(ic)))*/) {
     debugLog ("  %d - not packing - right is not temp\n", __LINE__);
     pic16_allocDirReg(IC_RIGHT (ic));
     return 0;
@@ -3599,20 +3643,16 @@ packForReceive (iCode * ic, eBBlock * ebp)
 
   for (dic = ic->next; dic; dic = dic->next)
     {
-
-
-
       if (IC_LEFT (dic) && (IC_RESULT (ic)->key == IC_LEFT (dic)->key))
-       debugLog ("    used on left\n");
+        debugLog ("    used on left\n");
       if (IC_RIGHT (dic) && IC_RESULT (ic)->key == IC_RIGHT (dic)->key)
-       debugLog ("    used on right\n");
+        debugLog ("    used on right\n");
       if (IC_RESULT (dic) && IC_RESULT (ic)->key == IC_RESULT (dic)->key)
-       debugLog ("    used on result\n");
+        debugLog ("    used on result\n");
 
       if ((IC_LEFT (dic) && (IC_RESULT (ic)->key == IC_LEFT (dic)->key)) ||
-         (IC_RESULT (dic) && IC_RESULT (ic)->key == IC_RESULT (dic)->key))
+        (IC_RESULT (dic) && IC_RESULT (ic)->key == IC_RESULT (dic)->key))
        return;
-
     }
 
   debugLog ("  hey we can remove this unnecessary assign\n");
@@ -3624,11 +3664,22 @@ static void
 packForPush (iCode * ic, eBBlock * ebp)
 {
   iCode *dic;
-
+  
   debugLog ("%s\n", __FUNCTION__);
   if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
     return;
 
+#if 0
+  {
+    int n1, n2;
+
+      n1 = bitVectnBitsOn( OP_DEFS(IC_LEFT(ic)));
+      n2 = bitVectnBitsOn( OP_USES(IC_LEFT(ic)));
+      debugf3("defs: %d\tuses: %d\t%s\n", n1, n2, printILine(ic));
+      debugf2("IC_LEFT(ic): from %d to %d\n", OP_LIVEFROM(IC_LEFT(ic)), OP_LIVETO(IC_LEFT(ic)));
+  }
+#endif
+
   /* must have only definition & one usage */
   if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
       bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
@@ -3639,12 +3690,26 @@ packForPush (iCode * ic, eBBlock * ebp)
                               bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
     return;
 
+  /* if definition is not assignment,
+   * or is not pointer (because pointer might have changed) */
   if (dic->op != '=' || POINTER_SET (dic))
     return;
 
+  /* we must ensure that we can use the delete the assignment,
+   * because the source might have been modified in between.
+   * Until I know how to fix this, I'll use the adhoc fix
+   * to check the liveranges */
+  if((OP_LIVEFROM(IC_RIGHT(dic))==0) || (OP_LIVETO(IC_RIGHT(dic))==0))
+    return;
+//  debugf2("IC_RIGHT(dic): from %d to %d\n", OP_LIVEFROM(IC_RIGHT(dic)), OP_LIVETO(IC_RIGHT(dic)));
+  
+
+
   /* we now we know that it has one & only one def & use
      and the that the definition is an assignment */
   IC_LEFT (ic) = IC_RIGHT (dic);
+  
+  debugf("remiCodeFromeBBlock: %s\n", printILine(dic));
 
   remiCodeFromeBBlock (ebp, dic);
   bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
@@ -3669,33 +3734,32 @@ static void isData(sym_link *sl)
 {
   FILE *of = stderr;
 
-       if(!pic16_ralloc_debug)return;
-       
-       if(!sl)return;
-
-       if(debugF)
-               of = debugF;
-
-       for ( ; sl; sl=sl->next) {
-               if(!IS_DECL(sl) ) {
-                       switch (SPEC_SCLS(sl)) {
-                               case S_DATA: fprintf (of, "data "); break;
-                               case S_XDATA: fprintf (of, "xdata "); break;
-                               case S_SFR: fprintf (of, "sfr "); break;
-                               case S_SBIT: fprintf (of, "sbit "); break;
-                               case S_CODE: fprintf (of, "code "); break;
-                               case S_IDATA: fprintf (of, "idata "); break;
-                               case S_PDATA: fprintf (of, "pdata "); break;
-                               case S_LITERAL: fprintf (of, "literal "); break;
-                               case S_STACK: fprintf (of, "stack "); break;
-                               case S_XSTACK: fprintf (of, "xstack "); break;
-                               case S_BIT: fprintf (of, "bit "); break;
-                               case S_EEPROM: fprintf (of, "eeprom "); break;
-                               default: break;
-                       }
-
-               }
-       }
+    if(!pic16_ralloc_debug)return;
+    
+    if(!sl)return;
+    
+    if(debugF)
+      of = debugF;
+
+    for ( ; sl; sl=sl->next) {
+      if(!IS_DECL(sl) ) {
+        switch (SPEC_SCLS(sl)) {
+          case S_DATA: fprintf (of, "data "); break;
+          case S_XDATA: fprintf (of, "xdata "); break;
+          case S_SFR: fprintf (of, "sfr "); break;
+          case S_SBIT: fprintf (of, "sbit "); break;
+          case S_CODE: fprintf (of, "code "); break;
+          case S_IDATA: fprintf (of, "idata "); break;
+          case S_PDATA: fprintf (of, "pdata "); break;
+          case S_LITERAL: fprintf (of, "literal "); break;
+          case S_STACK: fprintf (of, "stack "); break;
+          case S_XSTACK: fprintf (of, "xstack "); break;
+          case S_BIT: fprintf (of, "bit "); break;
+          case S_EEPROM: fprintf (of, "eeprom "); break;
+          default: break;
+        }
+      }
+    }
 }
 
 
@@ -3724,8 +3788,6 @@ pic16_packRegisters (eBBlock * ebp)
       {
 //             debugLog("%d\n", __LINE__);
        /* find assignment of the form TrueSym := iTempNN:1 */
-       /* see BUGLOG0001 for workaround with the CAST - VR */
-//     if ( (ic->op == '=' || ic->op == CAST) && !POINTER_SET (ic) ) // patch 11
        if ( (ic->op == '=') && !POINTER_SET (ic) ) // patch 11
          change += packRegsForAssign (ic, ebp);
        /* debug stuff */
@@ -3797,9 +3859,45 @@ pic16_packRegisters (eBBlock * ebp)
 
 
     if (POINTER_SET (ic))
-       debugLog ("  %d - Pointer set\n", __LINE__);
-
+      debugLog ("  %d - Pointer set\n", __LINE__);
+      
+      /* Look for two subsequent iCodes with */
+      /*   iTemp := _c;         */
+      /*   _c = iTemp & op;     */
+      /* and replace them by    */
+      /*   iTemp := _c;         */
+      /*   _c = _c & op;        */
+      if ((ic->op == BITWISEAND || ic->op == '|' || ic->op == '^')
+        && ic->prev
+        && ic->prev->op == '='
+        && IS_ITEMP (IC_LEFT (ic))
+        && IC_LEFT (ic) == IC_RESULT (ic->prev)
+        && isOperandEqual (IC_RESULT(ic), IC_RIGHT(ic->prev)))
+        {
+          iCode* ic_prev = ic->prev;
+          symbol* prev_result_sym = OP_SYMBOL (IC_RESULT (ic_prev));
+                       
+          ReplaceOpWithCheaperOp (&IC_LEFT (ic), IC_RESULT (ic));
+          if (IC_RESULT (ic_prev) != IC_RIGHT (ic)) {
+            bitVectUnSetBit (OP_USES (IC_RESULT (ic_prev)), ic->key);
+            if (/*IS_ITEMP (IC_RESULT (ic_prev)) && */
+              prev_result_sym->liveTo == ic->seq)
+            {
+              prev_result_sym->liveTo = ic_prev->seq;
+            }
+          }
+          bitVectSetBit (OP_USES (IC_RESULT (ic)), ic->key);
+          
+          bitVectSetBit (ic->rlive, IC_RESULT (ic)->key);
 
+          if (bitVectIsZero (OP_USES (IC_RESULT (ic_prev)))) {
+            bitVectUnSetBit (ic->rlive, IC_RESULT (ic)->key);
+            bitVectUnSetBit (OP_DEFS (IC_RESULT (ic_prev)), ic_prev->key);
+            remiCodeFromeBBlock (ebp, ic_prev);
+            hTabDeleteItem (&iCodehTab, ic_prev->key, ic_prev, DELETE_ITEM, NULL);
+          }
+        }
+               
     /* if this is an itemp & result of a address of a true sym 
        then mark this as rematerialisable   */
     if (ic->op == ADDRESS_OF &&
@@ -3859,9 +3957,11 @@ pic16_packRegisters (eBBlock * ebp)
       }
     if (POINTER_GET (ic))
       {
-       OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
-       debugLog ("  marking as a pointer (get) =>");
-       debugAopGet ("  left:", IC_LEFT (ic));
+        if(IS_SYMOP(IC_LEFT(ic))) {
+          OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
+          debugLog ("  marking as a pointer (get) =>");
+          debugAopGet ("  left:", IC_LEFT (ic));
+        }
       }
 
        //debugLog("  %d   %s\n", __LINE__, __FUNCTION__);
@@ -4018,6 +4118,12 @@ pic16_packRegisters (eBBlock * ebp)
       }
     }
 #endif
+
+#if 1
+    /* there are some problems with packing variables
+     * it seems that the live range estimator doesn't
+     * estimate correctly the liveranges of some symbols */
+     
     /* pack for PUSH 
        iTempNN := (some variable in farspace) V1
        push iTempNN ;
@@ -4028,7 +4134,7 @@ pic16_packRegisters (eBBlock * ebp)
       {
        packForPush (ic, ebp);
       }
-
+#endif
 
 #ifndef NO_packRegsForAccUse
     /* pack registers for accumulator use, when the
@@ -4111,6 +4217,8 @@ pic16_assignRegisters (eBBlock ** ebbs, int count)
   debugLog ("\nebbs before optimizing:\n");
   dumpEbbsToDebug (ebbs, count);
 
+  _inRegAllocator = 1;
+
   setToNull ((void *) &_G.funcrUsed);
   pic16_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
 
@@ -4188,6 +4296,7 @@ pic16_assignRegisters (eBBlock ** ebbs, int count)
   debugLog ("ebbs after optimizing:\n");
   dumpEbbsToDebug (ebbs, count);
 
+  _inRegAllocator = 0;
 
   genpic16Code (ic);
 
@@ -4199,6 +4308,7 @@ pic16_assignRegisters (eBBlock ** ebbs, int count)
   /* mark all registers as free */
   pic16_freeAllRegs ();
 
+
   debugLog ("leaving\n<><><><><><><><><><><><><><><><><>\n");
   debugLogClose ();
   return;