fix segfault on casting int to pointer
[fw/sdcc] / src / mcs51 / ralloc.c
index b2dff194df679f4b3a300b0fe2c96972ea9c122c..09fd032dfcc3b2d7ef83d7ee2ab34bc5b3d5a684 100644 (file)
@@ -479,20 +479,19 @@ static symbol *createStackSpil (symbol *sym)
        temporarily turn it off ; we also
        turn off memory model to prevent
        the spil from going to the external storage
-       and turn off overlaying 
     */
     
     useXstack = options.useXstack;
     model = options.model;
-    noOverlay = options.noOverlay;
-    options.noOverlay = 1;
+/*     noOverlay = options.noOverlay; */
+/*     options.noOverlay = 1; */
     options.model = options.useXstack = 0;
 
     allocLocal(sloc);
 
     options.useXstack = useXstack;
     options.model     = model;
-    options.noOverlay = noOverlay;
+/*     options.noOverlay = noOverlay; */
     sloc->isref = 1; /* to prevent compiler warning */
     
     /* if it is on the stack then update the stack */
@@ -628,11 +627,13 @@ static symbol *selectSpil (iCode *ic, eBBlock *ebp, symbol *forSym)
           used in the remainder of the block */
        if (!_G.blockSpil && (selectS = liveRangesWith(lrcs,notUsedInRemaining,ebp,ic))) {
            sym = leastUsedLR (selectS);
-           if (!sym->remat) {
-               sym->remainSpil = 1;
-               _G.blockSpil++;
+           if (sym != forSym) {
+               if (!sym->remat) {
+                   sym->remainSpil = 1;
+                   _G.blockSpil++;
+               }
+               return sym;
            }
-           return sym;
        }
     }   
 
@@ -737,7 +738,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;
 
@@ -869,7 +870,7 @@ static void deassignLRs (iCode *ic, eBBlock *ebp)
                  sym->nRegs) >= result->nRegs)
                ) {
                
-               for (i = 0 ; i < max(sym->nRegs,result->nRegs) ; i++)
+               for (i = 0 ; i < result->nRegs ; i++)
                    if (i < sym->nRegs )
                        result->regs[i] = sym->regs[i] ;
                    else
@@ -1075,7 +1076,8 @@ static void serialRegAssign (eBBlock **ebbs, int count)
                
                /* 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) 
                    <= PTRSIZE) 
                {
                    mcs51_ptrRegReq++;
@@ -1103,7 +1105,7 @@ static void serialRegAssign (eBBlock **ebbs, int count)
                                     OP_SYMBOL(IC_LEFT(ic)),ic->lineno);
                /* do the same for the right operand */
                if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) &&
-                   OP_SYMBOL(IC_RIGHT(ic))->nRegs && ic->op != '=')
+                   OP_SYMBOL(IC_RIGHT(ic))->nRegs)
                        positionRegs(OP_SYMBOL(IC_RESULT(ic)),
                                     OP_SYMBOL(IC_RIGHT(ic)),ic->lineno);
                
@@ -1139,7 +1141,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);
     }
@@ -1459,11 +1461,13 @@ static iCode *farSpacePackable (iCode *ic)
 /*-----------------------------------------------------------------*/
 static int packRegsForAssign (iCode *ic,eBBlock *ebp)
 {
-    iCode *dic, *sic;
-    
+       iCode *dic, *sic;
+       link *etype = operandType(IC_RIGHT(ic));
+       
     if (!IS_ITEMP(IC_RIGHT(ic))       ||       
        OP_SYMBOL(IC_RIGHT(ic))->isind ||
-       OP_LIVETO(IC_RIGHT(ic)) > ic->seq) {
+       OP_LIVETO(IC_RIGHT(ic)) > ic->seq ||
+       IS_BITFIELD(etype)) {
        return 0;
     }
        
@@ -1530,7 +1534,12 @@ static int packRegsForAssign (iCode *ic,eBBlock *ebp)
     
     if (!dic)
        return 0 ; /* did not find */
-           
+       
+    /* if assignment then check that right is not a bit */
+    if (ASSIGNMENT(dic) && !POINTER_SET(dic)) {
+           link *etype = operandType(IC_RIGHT(dic));
+           if (IS_BITFIELD(etype)) return 0;
+    }
     /* if the result is on stack or iaccess then it must be
        the same atleast one of the operands */
     if (OP_SYMBOL(IC_RESULT(ic))->onStack  || 
@@ -1547,7 +1556,7 @@ static int packRegsForAssign (iCode *ic,eBBlock *ebp)
              (IC_RIGHT(dic) &&
               IC_RESULT(ic)->key == IC_RIGHT(dic)->key)))
            return 0;                
-    }
+    }    
 pack:
     /* found the definition */
     /* replace the result with the result of */
@@ -1729,7 +1738,9 @@ static iCode *packRegsForOneuse (iCode *ic, operand *op , eBBlock *ebp)
        the usage of b, & acc */
     if (getSize(operandType(op)) >  (fReturnSize - 2) &&
        ic->op != RETURN             &&
-       ic->op != SEND)
+       ic->op != SEND               &&
+       !POINTER_SET(ic)             &&
+       !POINTER_GET(ic))
        return NULL;
 
     /* this routine will mark the a symbol as used in one 
@@ -1792,7 +1803,7 @@ static iCode *packRegsForOneuse (iCode *ic, operand *op , eBBlock *ebp)
 
     /* also make sure the intervenening instructions
        don't have any thing in far space */
-    for (dic = dic->next ; dic && dic != ic ; dic = dic->next) {
+    for (dic = dic->next ; dic && dic != ic && sic != ic; dic = dic->next) {
                
        /* if there is an intervening function call then no */
        if (dic->op == CALL || dic->op == PCALL)
@@ -1820,7 +1831,7 @@ static iCode *packRegsForOneuse (iCode *ic, operand *op , eBBlock *ebp)
                return NULL;
 
        /* if left or right or result is in far space */
-       if (isOperandInFarSpace(IC_LEFT(dic))   ||
+       if (isOperandInFarSpace(IC_LEFT(dic))   ||
            isOperandInFarSpace(IC_RIGHT(dic))  ||
            isOperandInFarSpace(IC_RESULT(dic)) ||
            IS_OP_RUONLY(IC_LEFT(dic))          ||
@@ -2086,11 +2097,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 &&
@@ -2098,11 +2106,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 */
@@ -2159,7 +2165,7 @@ static void packRegisters (eBBlock *ebp)
           can be eliminated for return statements */
        if ((ic->op == RETURN || ic->op == SEND) &&
            !isOperandInFarSpace(IC_LEFT(ic))    &&
-           !options.model)
+           options.model == MODEL_SMALL)
            packRegsForOneuse (ic,IC_LEFT(ic),ebp);     
 
        /* if pointer set & left has a size more than
@@ -2192,7 +2198,8 @@ static void packRegisters (eBBlock *ebp)
            link *toType = operandType(IC_LEFT(ic));
 
            if (IS_INTEGRAL(fromType) && IS_INTEGRAL(toType) &&
-               getSize(fromType) != getSize(toType) ) {
+               getSize(fromType) != getSize(toType)  &&
+               SPEC_USIGN(fromType) == SPEC_USIGN(toType)) {
 
                iCode *dic = packRegsForOneuse(ic,IC_RIGHT(ic),ebp);
                if (dic) {
@@ -2242,12 +2249,10 @@ static void packRegisters (eBBlock *ebp)
           the result of that operation is not on stack then
           we can leave the result of this operation in acc:b
           combination */
-       if ((IS_ARITHMETIC_OP(ic) 
-            
-            || IS_BITWISE_OP(ic) 
-            
+       if ((IS_ARITHMETIC_OP(ic)            
+            || IS_BITWISE_OP(ic)            
             || ic->op == LEFT_OP || ic->op == RIGHT_OP
-            
+            || (ic->op == ADDRESS_OF && isOperandOnStack(IC_LEFT(ic)))
             ) &&
            IS_ITEMP(IC_RESULT(ic)) &&
            getSize(operandType(IC_RESULT(ic))) <= 2)
@@ -2309,8 +2314,13 @@ void mcs51_assignRegisters (eBBlock **ebbs, int count)
     /* redo that offsets for stacked automatic variables */
     redoStackOffsets ();
 
-    if (options.dump_rassgn)
+    if (options.dump_rassgn) {
        dumpEbbsToFileExt(".dumprassgn",ebbs,count);
+       dumpLiveRanges(".lrange",liveRanges);
+    }
+
+    /* do the overlaysegment stuff SDCCmem.c */
+    doOverlays(ebbs,count);
 
     /* now get back the chain */
     ic = iCodeLabelOptimize(iCodeFromeBBlock (ebbs,count));