New target "hc08" for the Motorola 68hc08 family of micros
[fw/sdcc] / src / pic / ralloc.c
index d4ab0afc798b72c3012899f27cd08e41bd03a72c..3fa1870eb889d97873f083831c9f68be08947c57 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   */
@@ -484,7 +487,7 @@ regWithIdx (set *dRegs, int idx, int fixed)
   for (dReg = setFirstItem(dRegs) ; dReg ; 
        dReg = setNextItem(dRegs)) {
 
-    if(idx == dReg->rIdx && (fixed == dReg->isFixed)) {
+    if(idx == dReg->rIdx && (fixed == (int)dReg->isFixed)) {
       return dReg;
     }
   }
@@ -835,6 +838,8 @@ pic14_allocWithIdx (int idx)
     debugLog ("Found a Processor Register!\n");
   } else if( (dReg = regWithIdx ( dynInternalRegs, idx,0)) != NULL ) {
     debugLog ("Found an Internal Register!\n");
+  } else if( (dReg = regWithIdx ( dynInternalRegs, idx,1)) != NULL ) {
+    debugLog ("Found an Internal Register!\n");
   } else {
     
     debugLog ("Dynamic Register not found\n");
@@ -946,8 +951,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)
 {
@@ -975,13 +979,15 @@ void packBits(set *bregs)
       breg->address >>= 3;
 
       if(!bitfield) {
-       sprintf (buffer, "fbitfield%02x", breg->address);
+       //sprintf (buffer, "fbitfield%02x", breg->address);
+       sprintf (buffer, "0x%02x", breg->address);
        //fprintf(stderr,"new bit field\n");
        bitfield = newReg(REG_SFR, PO_GPR_BIT,breg->address,buffer,1,0);
        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);
@@ -998,7 +1004,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);
 
       }
@@ -1032,7 +1039,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,
@@ -1051,7 +1058,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,
@@ -1069,7 +1077,7 @@ void writeUsedRegs(FILE *of)
 {
   packBits(dynDirectBitRegs);
 
-
+  assignFixedRegisters(dynInternalRegs);
   assignFixedRegisters(dynAllocRegs);
   assignFixedRegisters(dynStackRegs);
   assignFixedRegisters(dynDirectRegs);
@@ -1077,17 +1085,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
@@ -1288,7 +1300,7 @@ leastUsedLR (set * sset)
 
     }
 
-  setToNull ((void **) &sset);
+  setToNull ((void *) &sset);
   sym->blockSpil = 0;
   return sym;
 }
@@ -2125,7 +2137,6 @@ serialRegAssign (eBBlock ** ebbs, int count)
              debugLog ("  %d - \n", __LINE__);
              if(debugF) 
                bitVectDebugOn(_G.regAssigned, debugF);
-
              for (j = 0; j < sym->nRegs; j++)
                {
                  if (sym->regType == REG_PTR)
@@ -2133,7 +2144,7 @@ serialRegAssign (eBBlock ** ebbs, int count)
                  else
                    sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
 
-                 /* if the allocation falied which means
+                 /* if the allocation failed which means
                     this was spilt then break */
                  if (!sym->regs[j])
                    break;
@@ -2436,14 +2447,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
@@ -2485,7 +2504,11 @@ regTypeNum ()
                    getSize (sym->type));
 
 
+#if 0
     if(IS_PTR_CONST (sym->type)) {
+#else
+    if(IS_CODEPTR (sym->type)) {
+#endif
       debugLog ("  %d const pointer type requires %d registers, changing to 2\n",__LINE__,sym->nRegs);
       sym->nRegs = 2;
     }
@@ -2703,7 +2726,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;
   }
 
@@ -2839,7 +2865,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;
 
 
@@ -3479,7 +3505,11 @@ packRegisters (eBBlock * ebp)
       sym_link *etype = getSpec (operandType (IC_LEFT (ic)));
 
       debugAopGet ("  left:", IC_LEFT (ic));
+#if 0
       if(IS_PTR_CONST(OP_SYMBOL(IC_LEFT(ic))->type))
+#else
+      if(IS_CODEPTR(OP_SYMBOL(IC_LEFT(ic))->type))
+#endif
        debugLog ("    is a pointer\n");
 
       if(IS_OP_VOLATILE(IC_LEFT(ic)))
@@ -3675,7 +3705,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
                
@@ -3696,7 +3726,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;
          }
        }
@@ -3865,8 +3895,8 @@ pic14_assignRegisters (eBBlock ** ebbs, int count)
   /* free up any _G.stackSpil locations allocated */
   applyToSet (_G.stackSpil, deallocStackSpil);
   _G.slocNum = 0;
-  setToNull ((void **) &_G.stackSpil);
-  setToNull ((void **) &_G.spiltSet);
+  setToNull ((void *) &_G.stackSpil);
+  setToNull ((void *) &_G.spiltSet);
   /* mark all registers as free */
   //pic14_freeAllRegs ();