Martins fix for sdcdb
[fw/sdcc] / src / pic / ralloc.c
index 337615aab7537daa83a770aae101b80cf4e3b5cc..1614e6c94ce9dd24439f4640e7a99b464dfab0ec 100644 (file)
@@ -35,6 +35,9 @@
 #define STRCASECMP strcasecmp
 #endif
 
+/* this should go in SDCCicode.h, but it doesn't. */
+#define IS_REF(op)       (IS_SYMOP(op) && op->operand.symOperand->isref == 1)
+
 /*-----------------------------------------------------------------*/
 /* At this point we start getting processor specific although      */
 /* some routines are non-processor specific & can be reused when   */
@@ -73,6 +76,7 @@ set *dynDirectBitRegs=NULL;
 set *dynInternalRegs=NULL;
 
 static hTab  *dynDirectRegNames= NULL;
+// static hTab  *regHash = NULL;    /* a hash table containing ALL registers */
 
 static int dynrIdx=0x20;
 static int rDirectIdx=0;
@@ -101,14 +105,14 @@ debugLog (char *fmt,...)
   //char *bufferP=buffer;
   va_list ap;
 
-  if (!debug || !srcFileName)
+  if (!debug || !dstFileName)
     return;
 
 
   if (!debugF)
     {
       /* create the file name */
-      strcpy (buffer, srcFileName);
+      strcpy (buffer, dstFileName);
       strcat (buffer, ".d");
 
       if (!(debugF = fopen (buffer, (append ? "a+" : "w"))))
@@ -661,7 +665,7 @@ allocDirReg (operand *op )
              name, SPEC_ADDR ( OP_SYM_ETYPE(op)));
 */
   } else {
-    //fprintf(stderr,"ralloc %s \n", name);
+    //fprintf(stderr,"ralloc:%d %s \n", __LINE__,name);
     
     reg = dirregWithName(name);
   }
@@ -672,6 +676,7 @@ allocDirReg (operand *op )
     /* if this is at an absolute address, then get the address. */
     if (SPEC_ABSA ( OP_SYM_ETYPE(op)) ) {
       address = SPEC_ADDR ( OP_SYM_ETYPE(op));
+      //fprintf(stderr,"reg %s is at an absolute address: 0x%03x\n",name,address);
     }
 
     /* Register wasn't found in hash, so let's create
@@ -691,9 +696,10 @@ allocDirReg (operand *op )
        reg->type = REG_SFR;
       }
 
-      if (IS_BITVAR (OP_SYM_ETYPE(op)))
+      if (IS_BITVAR (OP_SYM_ETYPE(op))) {
        addSet(&dynDirectBitRegs, reg);
-      else
+       reg->isBitField = 1;
+      } else
        addSet(&dynDirectRegs, reg);
 
     } else {
@@ -943,8 +949,7 @@ void writeSetUsedRegs(FILE *of, set *dRegs)
 extern void assignFixedRegisters(set *regset);
 extern void assignRelocatableRegisters(set *regset,int used);
 extern void dump_map(void);
-extern void dump_cblock(FILE *of);
-
+extern void dump_sfr(FILE *of);
 
 void packBits(set *bregs)
 {
@@ -978,7 +983,8 @@ void packBits(set *bregs)
        bitfield->isBitField = 1;
        bitfield->isFixed = 1;
        bitfield->address = breg->address;
-       addSet(&dynDirectRegs,bitfield);
+       //addSet(&dynDirectRegs,bitfield);
+       addSet(&dynInternalRegs,bitfield);
        //hTabAddItem(&dynDirectRegNames, regname2key(buffer), bitfield);
       } else {
        //fprintf(stderr,"  which is occupied by %s (addr = %d)\n",bitfield->name,bitfield->address);
@@ -995,7 +1001,8 @@ void packBits(set *bregs)
        //fprintf(stderr,"new relocatable bit field\n");
        relocbitfield = newReg(REG_GPR, PO_GPR_BIT,rDirectIdx++,buffer,1,0);
        relocbitfield->isBitField = 1;
-       addSet(&dynDirectRegs,relocbitfield);
+       //addSet(&dynDirectRegs,relocbitfield);
+       addSet(&dynInternalRegs,relocbitfield);
        //hTabAddItem(&dynDirectRegNames, regname2key(buffer), relocbitfield);
 
       }
@@ -1029,7 +1036,7 @@ void bitEQUs(FILE *of, set *bregs)
               breg->rIdx & 0x0007);
 
     else {
-      fprintf(stderr, "bit field is not assigned to a register\n");
+      //fprintf(stderr, "bit field is not assigned to a register\n");
       fprintf (of, "%s\tEQU\t( (bitfield%d<<3)+%d)\n",
               breg->name,
               bit_no>>3,
@@ -1048,7 +1055,8 @@ void aliasEQUs(FILE *of, set *fregs, int use_rIdx)
   for (reg = setFirstItem(fregs) ; reg ;
        reg = setNextItem(fregs)) {
 
-    if(!reg->isEmitted && reg->wasUsed) {
+    //if(!reg->isEmitted && reg->wasUsed) {
+    if(reg->wasUsed) {
       if(use_rIdx)
        fprintf (of, "%s\tEQU\t0x%03x\n",
                 reg->name,
@@ -1066,7 +1074,6 @@ void writeUsedRegs(FILE *of)
 {
   packBits(dynDirectBitRegs);
 
-
   assignFixedRegisters(dynAllocRegs);
   assignFixedRegisters(dynStackRegs);
   assignFixedRegisters(dynDirectRegs);
@@ -1074,17 +1081,21 @@ void writeUsedRegs(FILE *of)
   assignRelocatableRegisters(dynInternalRegs,0);
   assignRelocatableRegisters(dynAllocRegs,0);
   assignRelocatableRegisters(dynStackRegs,0);
-  assignRelocatableRegisters(dynDirectRegs,0);
 
+/*
+  assignRelocatableRegisters(dynDirectRegs,0);
+  printf("assignRelocatableRegisters(dynDirectRegs,0);\n");
+*/
   //dump_map();
 
-  dump_cblock(of);
+  dump_sfr(of);
   bitEQUs(of,dynDirectBitRegs);
+/*
   aliasEQUs(of,dynAllocRegs,0);
   aliasEQUs(of,dynDirectRegs,0);
   aliasEQUs(of,dynStackRegs,0);
   aliasEQUs(of,dynProcessorRegs,1);
-
+*/
 }
 
 #if 0
@@ -2433,14 +2444,22 @@ regTypeNum ()
 
       /* if used in return only then we don't 
         need registers */
-      if (sym->ruonly || sym->accuse) {
+      if (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 - no reg needed - accumulator used\n", __LINE__);
 
        continue;
       }
 
+      if (sym->ruonly) {
+       //if (IS_AGGREGATE (sym->type) || sym->isptr)
+       //  sym->type = aggrToPtr (sym->type, FALSE);
+       debugLog ("  %d - used as a return\n", __LINE__);
+
+       //continue;
+      }
+
       /* if the symbol has only one definition &
         that definition is a get_pointer and the
         pointer we are getting is rematerializable and
@@ -2700,7 +2719,10 @@ packRegsForAssign (iCode * ic, eBBlock * ebp)
 
   if (!IS_ITEMP (IC_RIGHT (ic))) {
     debugLog ("  %d - not packing - right is not temp\n", __LINE__);
-    allocDirReg(IC_RIGHT (ic));
+
+    /* only pack if this is not a function pointer */
+    if (!IS_REF (IC_RIGHT (ic)))
+      allocDirReg(IC_RIGHT (ic));
     return 0;
   }
 
@@ -2836,7 +2858,7 @@ pack:
   remiCodeFromeBBlock (ebp, ic);
   bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
   hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
-  OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
+  OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
   return 1;
 
 
@@ -3389,6 +3411,45 @@ void printSymType(char * str, sym_link *sl)
 
 }
 
+/*-----------------------------------------------------------------*/
+/* some debug code to print the symbol S_TYPE. Note that
+ * the function checkSClass in src/SDCCsymt.c dinks with
+ * the S_TYPE in ways the PIC port doesn't fully like...*/
+/*-----------------------------------------------------------------*/
+void isData(sym_link *sl)
+{
+  FILE *of = stderr;
+
+  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;
+      }
+
+    }
+
+  }
+    
+}
 /*-----------------------------------------------------------------*/
 /* packRegisters - does some transformations to reduce register    */
 /*                   pressure                                      */
@@ -3434,11 +3495,16 @@ packRegisters (eBBlock * ebp)
   for (ic = ebp->sch; ic; ic = ic->next) {
 
     if(IS_SYMOP ( IC_LEFT(ic))) {
-      //sym_link *etype = getSpec (operandType (IC_LEFT (ic)));
+      sym_link *etype = getSpec (operandType (IC_LEFT (ic)));
 
       debugAopGet ("  left:", IC_LEFT (ic));
       if(IS_PTR_CONST(OP_SYMBOL(IC_LEFT(ic))->type))
-       debugLog ("    is a pointer");
+       debugLog ("    is a pointer\n");
+
+      if(IS_OP_VOLATILE(IC_LEFT(ic)))
+       debugLog ("    is volatile\n");
+
+      isData(etype);
 
       printSymType("   ", OP_SYMBOL(IC_LEFT(ic))->type);
     }
@@ -3628,7 +3694,7 @@ packRegisters (eBBlock * ebp)
            remiCodeFromeBBlock (ebp, ic);
            bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
            hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
-           OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
+           OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
            ic = ic->prev;
          }  else
                
@@ -3649,7 +3715,7 @@ packRegisters (eBBlock * ebp)
            bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
            remiCodeFromeBBlock (ebp, ic);
            hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
-           OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
+           OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
            ic = ic->prev;
          }
        }