Merged changes between gbdk-293 and main
[fw/sdcc] / src / mcs51 / ralloc.c
index 11d2216b9f075b1dfb669329fdd0675c0dbd2467..4440eb05cbce34d6a55ca900eddbb08fef9c4f91 100644 (file)
@@ -25,6 +25,7 @@
 
 #include "common.h"
 #include "ralloc.h"
+#include "gen.h"
 
 /*-----------------------------------------------------------------*/
 /* At this point we start getting processor specific although      */
@@ -271,7 +272,7 @@ static int notUsedInBlock (symbol *sym, eBBlock *ebp, iCode *ic)
 static int notUsedInRemaining (symbol *sym, eBBlock *ebp, iCode *ic)
 {
     return ((usedInRemaining (operandFromSymbol(sym),ic) ? 0 : 1) &&
-           allDefsOutOfRange (sym->defs,ic->seq,ebp->lSeq));
+           allDefsOutOfRange (sym->defs,ebp->fSeq,ebp->lSeq));
 }
 
 /*-----------------------------------------------------------------*/
@@ -440,7 +441,8 @@ static symbol *createStackSpil (symbol *sym)
 {
     symbol *sloc= NULL;
     int useXstack, model, noOverlay;
-    int stackAuto;
+
+    char slocBuffer[30];
 
     /* first go try and find a free one that is already 
        existing on the stack */
@@ -457,13 +459,19 @@ static symbol *createStackSpil (symbol *sym)
        we need to allocate this on the stack : this is really a
        hack!! but cannot think of anything better at this time */
        
-    sprintf(buffer,"sloc%d",_G.slocNum++);
-    sloc = newiTemp(buffer);
+    if (sprintf(slocBuffer,"sloc%d",_G.slocNum++) >= sizeof(slocBuffer))
+    {
+       fprintf(stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
+               __FILE__, __LINE__);
+       exit(1);        
+    }
+
+    sloc = newiTemp(slocBuffer);
 
     /* set the type to the spilling symbol */
     sloc->type = copyLinkChain(sym->type);
     sloc->etype = getSpec(sloc->type);
-    SPEC_SCLS(sloc->etype) = S_AUTO ;    
+    SPEC_SCLS(sloc->etype) = S_DATA ;
     SPEC_EXTR(sloc->etype) = 0;
 
     /* we don't allow it to be allocated`
@@ -477,7 +485,6 @@ static symbol *createStackSpil (symbol *sym)
     useXstack = options.useXstack;
     model = options.model;
     noOverlay = options.noOverlay;
-    stackAuto = options.stackAuto;
     options.noOverlay = 1;
     options.model = options.useXstack = 0;
 
@@ -486,7 +493,6 @@ static symbol *createStackSpil (symbol *sym)
     options.useXstack = useXstack;
     options.model     = model;
     options.noOverlay = noOverlay;
-    options.stackAuto = stackAuto;
     sloc->isref = 1; /* to prevent compiler warning */
     
     /* if it is on the stack then update the stack */
@@ -603,8 +609,8 @@ static symbol *selectSpil (iCode *ic, eBBlock *ebp, symbol *forSym)
        return sym;
     }
 
-    /* if the symbol is local to the block then */        
-    if (forSym->liveTo < ebp->lSeq ) {       
+    /* if the symbol is local to the block then */
+    if (forSym->liveTo < ebp->lSeq) {       
 
        /* check if there are any live ranges allocated
           to registers that are not used in this block */
@@ -731,7 +737,7 @@ static bool spilSomething (iCode *ic, eBBlock *ebp, symbol *forSym)
 /*-----------------------------------------------------------------*/
 /* getRegPtr - will try for PTR if not a GPR type if not spil      */
 /*-----------------------------------------------------------------*/
-regs *getRegPtr (iCode *ic, eBBlock *ebp, symbol *sym)
+static regs *getRegPtr (iCode *ic, eBBlock *ebp, symbol *sym)
 {
     regs *reg;
 
@@ -1133,7 +1139,7 @@ static bitVect *rUmaskForOp (operand *op)
 
     rumask = newBitVect(mcs51_nRegs);
 
-    for (j = 0; j < sym->nRegs; j++) {
+    for (j = 0; j < sym->nRegs; j++) { 
        rumask = bitVectSetBit(rumask,
                               sym->regs[j]->rIdx);
     }
@@ -1455,13 +1461,12 @@ static int packRegsForAssign (iCode *ic,eBBlock *ebp)
 {
     iCode *dic, *sic;
     
-    if (
-/*     !IS_TRUE_SYMOP(IC_RESULT(ic)) ||            */
-       !IS_ITEMP(IC_RIGHT(ic))       ||        
-       OP_LIVETO(IC_RIGHT(ic)) > ic->seq ||
-       OP_SYMBOL(IC_RIGHT(ic))->isind)
+    if (!IS_ITEMP(IC_RIGHT(ic))       ||       
+       OP_SYMBOL(IC_RIGHT(ic))->isind ||
+       OP_LIVETO(IC_RIGHT(ic)) > ic->seq) {
        return 0;
-        
+    }
+       
     /* if the true symbol is defined in far space or on stack
        then we should not since this will increase register pressure */
     if (isOperandInFarSpace(IC_RESULT(ic))) {
@@ -1563,6 +1568,7 @@ pack:
         
     remiCodeFromeBBlock(ebp,ic);
     hTabDeleteItem (&iCodehTab,ic->key,ic,DELETE_ITEM,NULL);
+    OP_DEFS(IC_RESULT(dic)) = bitVectSetBit(OP_DEFS(IC_RESULT(dic)),dic->key);
     return 1;
     
 }
@@ -1721,7 +1727,7 @@ static iCode *packRegsForOneuse (iCode *ic, operand *op , eBBlock *ebp)
     
     /* only upto 2 bytes since we cannot predict
        the usage of b, & acc */
-    if (getSize(operandType(op)) > 2 && 
+    if (getSize(operandType(op)) >  (fReturnSize - 2) &&
        ic->op != RETURN             &&
        ic->op != SEND)
        return NULL;
@@ -1880,6 +1886,10 @@ static void packRegsForAccUse (iCode *ic)
          getSize(operandType(IC_RESULT(ic))) > 1))
        return ;
        
+    if (IS_BITWISE_OP(ic) &&
+       getSize(operandType(IC_RESULT(ic))) > 1)
+       return ;
+           
        
     /* has only one definition */
     if (bitVectnBitsOn(OP_DEFS(IC_RESULT(ic))) > 1)
@@ -2076,11 +2086,8 @@ static void packRegisters (eBBlock *ebp)
        }
 
        /* if this is a +/- operation with a rematerizable 
-          then mark this as rematerializable as well only
-          if the literal value is within the range -255 and + 255
-          the assembler cannot handle it other wise */
+          then mark this as rematerializable as well */
        if ((ic->op == '+' || ic->op == '-') &&
-
            (IS_SYMOP(IC_LEFT(ic)) && 
             IS_ITEMP(IC_RESULT(ic)) &&
             OP_SYMBOL(IC_LEFT(ic))->remat &&
@@ -2088,11 +2095,9 @@ static void packRegisters (eBBlock *ebp)
             IS_OP_LITERAL(IC_RIGHT(ic))) ) {
 
            int i = operandLitValue(IC_RIGHT(ic));
-           if ( i < 255 && i > -255) {
-               OP_SYMBOL(IC_RESULT(ic))->remat = 1;
-               OP_SYMBOL(IC_RESULT(ic))->rematiCode = ic;
-               OP_SYMBOL(IC_RESULT(ic))->usl.spillLoc = NULL;
-           }
+           OP_SYMBOL(IC_RESULT(ic))->remat = 1;
+           OP_SYMBOL(IC_RESULT(ic))->rematiCode = ic;
+           OP_SYMBOL(IC_RESULT(ic))->usl.spillLoc = NULL;
        }
 
        /* mark the pointer usages */
@@ -2190,6 +2195,7 @@ static void packRegisters (eBBlock *ebp)
                        IC_RESULT(dic) = IC_RESULT(ic);
                        remiCodeFromeBBlock(ebp,ic);
                        hTabDeleteItem (&iCodehTab,ic->key,ic,DELETE_ITEM,NULL);
+                       OP_DEFS(IC_RESULT(dic)) = bitVectSetBit(OP_DEFS(IC_RESULT(dic)),dic->key);
                        ic = ic->prev;
                    } else
                        OP_SYMBOL(IC_RIGHT(ic))->ruonly =  0;
@@ -2205,6 +2211,7 @@ static void packRegisters (eBBlock *ebp)
                        IC_RESULT(dic) = IC_RESULT(ic);
                        remiCodeFromeBBlock(ebp,ic);
                        hTabDeleteItem (&iCodehTab,ic->key,ic,DELETE_ITEM,NULL);
+                       OP_DEFS(IC_RESULT(dic)) = bitVectSetBit(OP_DEFS(IC_RESULT(dic)),dic->key);
                        ic = ic->prev;
                    }
                }