* sim/ucsim/cmd.src/cmdutil.cc: NUL device is detected as CG_FILE type
[fw/sdcc] / src / pic16 / ralloc.c
index 83761a206c0a5b125c3e399f05f2376603a8ddd5..1319ff89e97ba01cb0ae31a4c2cc8463879e9117 100644 (file)
 #include "gen.h"
 #include "device.h"
 
-#if defined(__BORLANDC__) || defined(_MSC_VER)
-#define STRCASECMP stricmp
-#else
-#define STRCASECMP strcasecmp
-#endif
-
 #ifndef debugf
 #define debugf(frm, rest)       _debugf(__FILE__, __LINE__, frm, rest)
 #endif
@@ -153,6 +147,7 @@ debugLog (char *fmt,...)
   vsprintf (buffer, fmt, ap);
 
   fprintf (debugF, "%s", buffer);
+  //fprintf (stderr, "%s", buffer);
 /*
    while (isspace((unsigned char)*bufferP)) bufferP++;
 
@@ -275,7 +270,6 @@ pic16_decodeOp (unsigned int op)
                case STRUCT:            return "STRUCT";
                case UNION:             return "UNION";
                case ENUM:              return "ENUM";
-               case ELIPSIS:           return "ELIPSIS";
                case RANGE:             return "RANGE";
                case FAR:               return "FAR";
                case CASE:              return "CASE";
@@ -374,7 +368,7 @@ static int regname2key(char const *name)
 /*-----------------------------------------------------------------*/
 /* newReg - allocate and init memory for a new register            */
 /*-----------------------------------------------------------------*/
-regs* newReg(short type, short pc_type, int rIdx, char *name, int size, int alias, operand *refop)
+regs* newReg(int type, short pc_type, int rIdx, char *name, unsigned size, int alias, operand *refop)
 {
 
   regs *dReg;
@@ -424,7 +418,7 @@ regs* newReg(short type, short pc_type, int rIdx, char *name, int size, int alia
        dReg->regop = refop;
   
        if(!(type == REG_SFR && alias == 0x80))
-               hTabAddItem(&dynDirectRegNames, regname2key(name), dReg);
+               hTabAddItem(&dynDirectRegNames, regname2key(dReg->name), dReg);
 
   return dReg;
 }
@@ -578,17 +572,16 @@ allocReg (short type)
 //                                     (_inRegAllocator)?"ralloc":"", currFunc, reg->name, reg->rIdx);
 
 #if 1
-                if(_inRegAllocator && (dynrIdx > MAX_P16_NREGS)) {
-//                  debugf("allocating more registers than available\n", 0);
-//                  return (NULL);
-                }
+               if(_inRegAllocator && (dynrIdx > MAX_P16_NREGS)) {
+                 //                  debugf("allocating more registers than available\n", 0);
+                 //                  return (NULL);
+               }
+               
+               addSet(&pic16_dynAllocRegs, reg);
+               hTabAddItem(&dynAllocRegNames, regname2key(reg->name), reg);
+//             fprintf(stderr, "%s:%d added reg to pic16_dynAllocRegs = %p\n", __FUNCTION__, __LINE__, pic16_dynAllocRegs);
 #endif
        }
-
-       addSet(&pic16_dynAllocRegs, reg);
-       hTabAddItem(&dynAllocRegNames, regname2key(reg->name), reg);
-
-//     fprintf(stderr, "%s:%d added reg to pic16_dynAllocRegs = %p\n", __FUNCTION__, __LINE__, pic16_dynAllocRegs);
        
        debugLog ("%s of type %s for register rIdx: %d (0x%x)\n", __FUNCTION__, debugLogRegType (type), dynrIdx-1, dynrIdx-1);
 
@@ -663,7 +656,7 @@ pic16_allocregWithName (char *name)
 
   hkey = regname2key(name);
 
-       fprintf(stderr, "%s:%d: name = %s\thash = %d\n", __FUNCTION__, __LINE__, name, hkey);
+  //fprintf(stderr, "%s:%d: name = %s\thash = %d\n", __FUNCTION__, __LINE__, name, hkey);
 
   reg = hTabFirstItemWK(dynAllocRegNames, hkey);
 
@@ -1086,7 +1079,7 @@ regs *
 pic16_allocWithIdx (int idx)
 {
 
-  regs *dReg;
+  regs *dReg=NULL;
 
   debugLog ("%s - allocating with index = 0x%x\n", __FUNCTION__,idx);
 //  fprintf(stderr, "%s - allocating with index = 0x%x\n", __FUNCTION__,idx);
@@ -1105,12 +1098,19 @@ pic16_allocWithIdx (int idx)
     
     debugLog ("Dynamic Register not found\n");
 
-//     return (NULL);
-    //fprintf(stderr,"%s %d - requested register: 0x%x\n",__FUNCTION__,__LINE__,idx);
-    werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
-           "allocWithIdx not found");
-    exit (1);
+#if 1
+       dReg = newReg(REG_GPR, PO_GPR_TEMP, idx, NULL, 1, 0, NULL);
+       addSet(&pic16_dynAllocRegs, dReg);
+       hTabAddItem(&dynAllocRegNames, regname2key(dReg->name), dReg);
+#endif
 
+       if(!dReg) {
+//     return (NULL);
+    //fprintf(stderr,"%s %d - requested register: 0x%x\n",__FUNCTION__,__LINE__,idx);
+           werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
+                   "allocWithIdx not found");
+           exit (1);
+       }
   }
 
   dReg->wasUsed = 1;
@@ -3174,12 +3174,12 @@ packRegsForAssign (iCode * ic, eBBlock * ebp)
         * if it fits for pic16, but I leave it here just in case */
 
        /* if assignment then check that right is not a bit */
-       if (ASSIGNMENT (dic) && !POINTER_SET (dic)) {
-         sym_link *etype = operandType (IC_RIGHT (dic));
+       if (ASSIGNMENT (ic) && !POINTER_SET (ic)) {
+         sym_link *etype = operandType (IC_RESULT (dic));
 
                if (IS_BITFIELD (etype)) {
                        /* if result is a bit too then it's ok */
-                       etype = operandType (IC_RESULT (dic));
+                       etype = operandType (IC_RESULT (ic));
                        if (!IS_BITFIELD (etype)) {
                                debugLog(" %d bitfields\n");
                          return 0;
@@ -3796,6 +3796,7 @@ static void
 packForPush (iCode * ic, eBBlock * ebp)
 {
   iCode *dic;
+  char *iLine;
   
   debugLog ("%s\n", __FUNCTION__);
   if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
@@ -3804,10 +3805,13 @@ packForPush (iCode * ic, eBBlock * ebp)
 #if 0
   {
     int n1, n2;
+    char *iLine;
 
       n1 = bitVectnBitsOn( OP_DEFS(IC_LEFT(ic)));
       n2 = bitVectnBitsOn( OP_USES(IC_LEFT(ic)));
+      iLine = printILine(ic);
       debugf3("defs: %d\tuses: %d\t%s\n", n1, n2, printILine(ic));
+      dbuf_free(iLine);
       debugf2("IC_LEFT(ic): from %d to %d\n", OP_LIVEFROM(IC_LEFT(ic)), OP_LIVETO(IC_LEFT(ic)));
   }
 #endif
@@ -3841,7 +3845,9 @@ packForPush (iCode * ic, eBBlock * ebp)
      and the that the definition is an assignment */
   IC_LEFT (ic) = IC_RIGHT (dic);
   
-  debugf("remiCodeFromeBBlock: %s\n", printILine(dic));
+  iLine = printILine(dic);
+  debugf("remiCodeFromeBBlock: %s\n", iLine);
+  dbuf_free(iLine);
 
   remiCodeFromeBBlock (ebp, dic);
   bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
@@ -3853,7 +3859,7 @@ static void printSymType(char * str, sym_link *sl)
        if(!pic16_ralloc_debug)return;
        
        debugLog ("    %s Symbol type: ",str);
-       printTypeChainsl, debugF);
+       printTypeChain (sl, debugF);
        debugLog ("\n");
 }
 
@@ -4457,7 +4463,9 @@ pic16_assignRegisters (ebbIndex * ebbi)
   regTypeNum ();
 
   /* start counting function temporary registers from zero */
-  dynrIdx = 0;
+  /* XXX: Resetting dynrIdx breaks register allocation,
+   *      see #1489055, #1483693 (?), and #1445850! */
+  //dynrIdx = 0;
 
   /* and serially allocate registers */
   serialRegAssign (ebbs, count);