some -xstack related stuff
[fw/sdcc] / src / SDCCicode.c
index 279d7b232c3e74515814723fc82d2b36c910f701..36410ad3328e43e1ecbfaffd1e6f4283e8316393 100644 (file)
@@ -38,8 +38,6 @@ int block;
 int scopeLevel;
 int lvaluereq;
 
-extern int labelKey ;
-
 symbol *returnLabel ; /* function return label */
 symbol *entryLabel  ; /* function entry  label */
 /*-----------------------------------------------------------------*/
@@ -648,13 +646,13 @@ link *operandType (operand *op)
        
     case TYPE :
        return op->operand.typeOperand ;
+    default:
+       werror (E_INTERNAL_ERROR,__FILE__,__LINE__,
+               " operand type not known ");
+       assert (0) ; /* should never come here */
+       /*  Just to keep the compiler happy */
+       return (link *)0;
     }
-    
-    werror (E_INTERNAL_ERROR,__FILE__,__LINE__,
-           " operand type not known ");
-    assert (0) ; /* should never come here */
-    /*  Just to keep the compiler happy */
-    return (link *)0;
 }
 
 /*-----------------------------------------------------------------*/
@@ -751,9 +749,29 @@ bool isOperandInFarSpace (operand *op)
        else            
            return FALSE;
     }
+    else
+    {
+       etype = getSpec(operandType(op));
+    }
+    return (IN_FARSPACE(SPEC_OCLS(etype)) ? TRUE : FALSE);
+}
+
+/*-----------------------------------------------------------------*/
+/* isOperandOnStack - will return true if operand is on stack      */
+/*-----------------------------------------------------------------*/
+bool isOperandOnStack(operand *op)
+{
+    link *etype;
+
+    if (!op)
+       return FALSE;
+
+    if (!IS_SYMOP(op))
+       return FALSE ;
 
     etype = getSpec(operandType(op));
-    return (IN_FARSPACE(SPEC_OCLS(etype)) ? TRUE : FALSE);
+
+    return ((IN_STACK(etype)) ? TRUE : FALSE);
 }
 
 /*-----------------------------------------------------------------*/
@@ -1060,7 +1078,7 @@ operand *operandFromSymbol (symbol *sym)
 {
     operand *op ;
     iCode *ic ;
-    
+    int ok =1 ;
     /* if the symbol's type is a literal */
     /* then it is an enumerator type     */
     if (IS_LITERAL(sym->etype) && SPEC_ENUM(sym->etype)) 
@@ -1084,6 +1102,11 @@ operand *operandFromSymbol (symbol *sym)
     
     /* under the following conditions create a
        register equivalent for a local symbol */
+    if (sym->level && sym->etype && SPEC_OCLS(sym->etype) &&
+       (IN_FARSPACE(SPEC_OCLS(sym->etype)) && (!IS_DS390_PORT)) &&
+       options.stackAuto == 0)
+       ok =0;
+
     if (!IS_AGGREGATE(sym->type) &&     /* not an aggregate */
        !IS_FUNC(sym->type)      &&     /* not a function   */
        !sym->_isparm            &&     /* not a parameter  */
@@ -1093,7 +1116,7 @@ operand *operandFromSymbol (symbol *sym)
        !IS_VOLATILE(sym->etype) &&     /* not declared as volatile */
        !IS_STATIC(sym->etype)   &&     /* and not declared static  */
        !sym->islbl              &&     /* not a label */
-       !IN_FARSPACE(SPEC_OCLS(sym->etype)) && /* not in far space */
+       ok                       &&     /* farspace check */
        !IS_BITVAR(sym->etype)          /* not a bit variable */
        ) {
        
@@ -1106,7 +1129,7 @@ operand *operandFromSymbol (symbol *sym)
        OP_SYMBOL(sym->reqv)->islocal = 1;
        SPIL_LOC(sym->reqv) = sym;
     }
-
+   
     if (!IS_AGGREGATE(sym->type)) {
        op = newOperand();
        op->type = SYMBOL;
@@ -1328,7 +1351,7 @@ operand *geniCodeCast (link *type, operand *op, bool implicit)
        return op;
     
     /* if this is a literal then just change the type & return */
-    if (IS_LITERAL(opetype) && !IS_PTR(type) && !IS_PTR(optype))
+    if (IS_LITERAL(opetype) && op->type == VALUE && !IS_PTR(type) && !IS_PTR(optype))
        return operandFromValue(valCastLiteral(type,
                                               operandLitValue(op)));
           
@@ -1540,7 +1563,8 @@ operand *geniCodeSubtract (operand *left, operand *right)
        return geniCodePtrPtrSubtract (left,right);
     
     /* if they are both literal then we know the result */
-    if (IS_LITERAL(letype) && IS_LITERAL(retype)) 
+    if (IS_LITERAL(letype) && IS_LITERAL(retype)
+       && left->isLiteral && right->isLiteral) 
        return operandFromValue (valMinus(left->operand.valOperand,
                                          right->operand.valOperand));
     
@@ -1580,18 +1604,18 @@ operand *geniCodeAdd (operand *left, operand *right )
     operand *size ;
     int isarray = 0;
     LRTYPE ;
-    
+
     /* if left is an array then array access */
     if (IS_ARRAY(ltype)) 
        return geniCodeArray (left,right);           
     
     /* if the right side is LITERAL zero */
     /* return the left side              */
-    if (IS_LITERAL(retype) && !floatFromVal(valFromType(retype)))
+    if (IS_LITERAL(retype) && right->isLiteral && !floatFromVal(valFromType(retype)))
        return left;
     
     /* if left is literal zero return right */
-    if (IS_LITERAL(letype) && !floatFromVal(valFromType(letype)))
+    if (IS_LITERAL(letype) && left->isLiteral && !floatFromVal(valFromType(letype)))
        return right ;
     
     /* if left is an array or pointer then size */
@@ -1601,6 +1625,8 @@ operand *geniCodeAdd (operand *left, operand *right )
        size = 
            operandFromLit(getSize(ltype->next));
        right = geniCodeMultiply (right ,size);
+       if (getSize(ltype) > 1) 
+               right = geniCodeCast(INTTYPE,right,TRUE);
        resType = copyLinkChain(ltype);
     }
     else { /* make them the same size */
@@ -1610,7 +1636,8 @@ operand *geniCodeAdd (operand *left, operand *right )
     }
     
     /* if they are both literals then we know */
-    if (IS_LITERAL(letype) && IS_LITERAL(retype))
+    if (IS_LITERAL(letype) && IS_LITERAL(retype)
+       && left->isLiteral && right->isLiteral)
        return operandFromValue (valPlus(valFromType(letype),
                                         valFromType(retype)));
     
@@ -1648,27 +1675,8 @@ link *aggrToPtr ( link *type, bool force)
     ptype->next = type;
     /* if the output class is generic */
     if ((DCL_TYPE(ptype) = PTR_TYPE(SPEC_OCLS(etype))) == CPOINTER)
-       DCL_PTR_CONST(ptype) = 1;
+       DCL_PTR_CONST(ptype) = port->mem.code_ro;
 
-/*     if (SPEC_OCLS(etype) == generic) */
-/*     DCL_TYPE(ptype) = GPOINTER; */
-/*     else */
-/*     if (SPEC_OCLS(etype)->codesp ) { */
-/*         DCL_TYPE(ptype) = CPOINTER ; */
-/*         DCL_PTR_CONST(ptype) = 1; */
-/*     } */
-/*     else */
-/*         if (SPEC_OCLS(etype)->fmap && !SPEC_OCLS(etype)->paged) */
-/*             DCL_TYPE(ptype) = FPOINTER ; */
-/*         else */
-/*             if (SPEC_OCLS(etype)->fmap && SPEC_OCLS(etype)->paged) */
-/*                 DCL_TYPE(ptype) = PPOINTER ; */
-/*             else */
-/*                 if (SPEC_OCLS(etype) == idata) */
-/*                     DCL_TYPE(ptype) = IPOINTER; */
-/*                 else */
-/*                     DCL_TYPE(ptype) = POINTER ; */
-    
     /* if the variable was declared a constant */
     /* then the pointer points to a constant */
     if (IS_CONSTANT(etype) )
@@ -1687,26 +1695,11 @@ operand *geniCodeArray2Ptr (operand *op)
 {
     link *optype = operandType(op);
     link *opetype = getSpec(optype);
-    
+
+    /* set the pointer depending on the storage class */    
     if ((DCL_TYPE(optype) = PTR_TYPE(SPEC_OCLS(opetype))) == CPOINTER)
-       DCL_PTR_CONST(optype) = 1;
+       DCL_PTR_CONST(optype) = port->mem.code_ro;
 
-    /* set the pointer depending on the storage class */
-/*     if (SPEC_OCLS(opetype)->codesp ) { */
-/*     DCL_TYPE(optype) = CPOINTER ; */
-/*     DCL_PTR_CONST(optype) = 1; */
-/*     } */
-/*     else */
-/*     if (SPEC_OCLS(opetype)->fmap && !SPEC_OCLS(opetype)->paged) */
-/*         DCL_TYPE(optype) = FPOINTER ; */
-/*     else */
-/*         if (SPEC_OCLS(opetype)->fmap && SPEC_OCLS(opetype)->paged) */
-/*             DCL_TYPE(optype) = PPOINTER ; */
-/*         else */
-/*             if (SPEC_OCLS(opetype) == idata) */
-/*                 DCL_TYPE(optype) = IPOINTER; */
-/*             else */
-/*                 DCL_TYPE(optype) = POINTER ; */
     
     /* if the variable was declared a constant */
     /* then the pointer points to a constant */
@@ -1729,10 +1722,12 @@ operand *geniCodeArray (operand *left,operand *right)
     link *ltype = operandType(left);
     
     if (IS_PTR(ltype)) {
+       if (IS_PTR(ltype->next) && left->isaddr)
+           left = geniCodeRValue(left,FALSE);
        return geniCodeDerefPtr(geniCodeAdd(left,right));
     }
 
-   /* array access */
+    /* array access */
     right = geniCodeMultiply(right,
                             operandFromLit(getSize(ltype->next)));
 
@@ -1958,39 +1953,20 @@ operand *geniCodeAddressOf (operand *op)
     link *optype = operandType(op);
     link *opetype= getSpec(optype);
     
+    /* lvalue check already done in decorateType */
     /* this must be a lvalue */
-    if (!op->isaddr && !IS_AGGREGATE(optype)) {
-       werror (E_LVALUE_REQUIRED,"&");
-       return op;
-    }
+/*     if (!op->isaddr && !IS_AGGREGATE(optype)) { */
+/*     werror (E_LVALUE_REQUIRED,"&"); */
+/*     return op; */
+/*     } */
     
     p = newLink();
     p->class = DECLARATOR ;
     
+    /* set the pointer depending on the storage class */
     if ((DCL_TYPE(p) = PTR_TYPE(SPEC_OCLS(opetype))) == CPOINTER)
-       DCL_PTR_CONST(p) = 1;
+       DCL_PTR_CONST(p) = port->mem.code_ro;
 
-    /* set the pointer depending on the storage class */
-/*     if (SPEC_OCLS(opetype)->codesp ) { */
-/*     DCL_TYPE(p) = CPOINTER ; */
-/*     DCL_PTR_CONST(p) = 1; */
-/*     } */
-/*     else */
-/*     if (SPEC_OCLS(opetype)->fmap && !SPEC_OCLS(opetype)->paged) */
-/*         DCL_TYPE(p) = FPOINTER ; */
-/*     else */
-/*         if (SPEC_OCLS(opetype)->fmap && SPEC_OCLS(opetype)->paged) */
-/*             DCL_TYPE(p) = PPOINTER ; */
-/*         else */
-/*             if (SPEC_OCLS(opetype) == idata) */
-/*                 DCL_TYPE(p) = IPOINTER; */
-/*             else */
-/*                 if (SPEC_OCLS(opetype) == data || */
-/*                     SPEC_OCLS(opetype) == overlay) */
-/*                     DCL_TYPE(p) = POINTER ; */
-/*                 else */
-/*                     DCL_TYPE(p) = GPOINTER; */
-    
     /* make sure we preserve the const & volatile */
     if (IS_CONSTANT(opetype)) 
        DCL_PTR_CONST(p) = 1;
@@ -2043,6 +2019,14 @@ void setOClass (link *ptr, link *spec)
     case PPOINTER:
        SPEC_OCLS(spec) = xstack;
        break;
+
+    case EEPPOINTER:
+       SPEC_OCLS(spec) = eeprom;
+       break;
+
+    default:
+       break;
+
     }
 }
 
@@ -2065,16 +2049,19 @@ operand *geniCodeDerefPtr (operand *op)
     }
     
     /* now get rid of the pointer part */
-    if (lvaluereq && IS_ITEMP(op))
+    if (lvaluereq && IS_ITEMP(op) )
+    {
        retype = getSpec(rtype = copyLinkChain(optype)) ;
+    }
     else
+    {
        retype = getSpec(rtype = copyLinkChain(optype->next)) ;
+    }
     
     /* if this is a pointer then outputclass needs 2b updated */
     if (IS_PTR(optype)) 
        setOClass(optype,retype);    
         
-
     op->isGptr = IS_GENPTR(optype);
 
     /* if the pointer was declared as a constant */
@@ -2090,6 +2077,7 @@ operand *geniCodeDerefPtr (operand *op)
 
     if (!lvaluereq)
        op = geniCodeRValue(op,TRUE);
+
     setOperandType(op,rtype);
     
     return op;    
@@ -2140,6 +2128,12 @@ operand *geniCodeRightShift (operand *left, operand *right)
     return IC_RESULT(ic) ;  
 }
 
+#ifdef __BORLANDC__
+#define LONG_LONG __int64
+#else
+#define LONG_LONG long long
+#endif
+
 /*-----------------------------------------------------------------*/
 /* geniCodeLogic- logic code                                       */
 /*-----------------------------------------------------------------*/
@@ -2156,7 +2150,7 @@ operand *geniCodeLogic (operand *left, operand *right, int op )
        int nbits = bitsForType(ltype);
        long v = operandLitValue(right);
 
-       if (v > ((long long) 1 << nbits) && v > 0)
+       if (v > ((LONG_LONG) 1 << nbits) && v > 0)
            werror(W_CONST_RANGE," compare operation ");
     }
 
@@ -2245,13 +2239,14 @@ operand *geniCodeAssign (operand *left, operand *right, int nosupdate)
        
     /* left is integral type and right is literal then
        check if the literal value is within bounds */
-    if (IS_INTEGRAL(ltype) && IS_LITERAL(rtype)) {
+    if (IS_INTEGRAL(ltype) && right->type == VALUE && IS_LITERAL(rtype)) {
        int nbits = bitsForType(ltype);
        long v = operandLitValue(right);
 
-       if (v > ((long long)1 << nbits) && v > 0)
+       if (v > ((LONG_LONG)1 << nbits) && v > 0)
            werror(W_CONST_RANGE," = operation");
     }
+
     /* if the left & right type don't exactly match */
     /* if pointer set then make sure the check is
        done with the type & not the pointer */
@@ -2330,7 +2325,7 @@ static void geniCodeSEParms (ast *parms)
 /*-----------------------------------------------------------------*/
 /* geniCodeParms - generates parameters                            */
 /*-----------------------------------------------------------------*/
-static void geniCodeParms ( ast *parms , int *stack, link *fetype)
+static void geniCodeParms ( ast *parms , int *stack, link *fetype, symbol *func)
 {
     iCode *ic ;
     operand *pval ; 
@@ -2340,8 +2335,8 @@ static void geniCodeParms ( ast *parms , int *stack, link *fetype)
     
     /* if this is a param node then do the left & right */
     if (parms->type == EX_OP && parms->opval.op == PARAM) {
-       geniCodeParms (parms->left, stack,fetype) ;
-       geniCodeParms (parms->right, stack,fetype);
+       geniCodeParms (parms->left, stack,fetype,func) ;
+       geniCodeParms (parms->right, stack,fetype,func);
        return ;
     }
     
@@ -2364,8 +2359,8 @@ static void geniCodeParms ( ast *parms , int *stack, link *fetype)
     }
 
     /* if register parm then make it a send */
-    if ((parms->argSym && IS_REGPARM(parms->argSym->etype)) ||
-       IS_REGPARM(parms->etype)) {
+    if (((parms->argSym && IS_REGPARM(parms->argSym->etype)) ||
+       IS_REGPARM(parms->etype)) && !func->hasVargs ) {
        ic = newiCode(SEND,pval,NULL);
        ADDTOCHAIN(ic);
     } else {
@@ -2405,7 +2400,7 @@ operand *geniCodeCall (operand *left, ast *parms)
     geniCodeSEParms ( parms );
 
     /* first the parameters */
-    geniCodeParms ( parms , &stack , getSpec(operandType(left)));
+    geniCodeParms ( parms , &stack , getSpec(operandType(left)), OP_SYMBOL(left));
     
     /* now call : if symbol then pcall */
     if (IS_ITEMP(left)) 
@@ -2443,16 +2438,20 @@ static void geniCodeReceive (value *args)
 
            /* we will use it after all optimizations
               and before liveRange calculation */          
-           if (!sym->addrtaken && 
-               !IS_VOLATILE(sym->etype) &&
-               !IN_FARSPACE(SPEC_OCLS(sym->etype))) {
-               opl = newiTempOperand(args->type,0);
-               sym->reqv = opl ;           
-               sym->reqv->key = sym->key ;
-               OP_SYMBOL(sym->reqv)->key = sym->key;
-               OP_SYMBOL(sym->reqv)->isreqv = 1;
-               OP_SYMBOL(sym->reqv)->islocal= 0;
-               SPIL_LOC(sym->reqv) =  sym;
+           if (!sym->addrtaken && !IS_VOLATILE(sym->etype)) {
+
+               if (IN_FARSPACE(SPEC_OCLS(sym->etype)) &&
+                  options.stackAuto == 0 &&
+                  !IS_DS390_PORT) {
+               } else {
+                   opl = newiTempOperand(args->type,0);
+                   sym->reqv = opl ;       
+                   sym->reqv->key = sym->key ;
+                   OP_SYMBOL(sym->reqv)->key = sym->key;
+                   OP_SYMBOL(sym->reqv)->isreqv = 1;
+                   OP_SYMBOL(sym->reqv)->islocal= 0;
+                   SPIL_LOC(sym->reqv) =  sym;
+               }
            }
 
            ic = newiCode(RECEIVE,NULL,NULL);
@@ -2773,14 +2772,30 @@ operand *ast2iCode (ast *tree)
         tree->opval.op != SWITCH &&
         tree->opval.op != FUNCTION &&
         tree->opval.op != INLINEASM ) {
-       if (IS_ASSIGN_OP(tree->opval.op) || IS_DEREF_OP(tree)) {
-           lvaluereq++;
-           left = operandFromAst(tree->left);
-           lvaluereq--;
+
+       if (IS_ASSIGN_OP(tree->opval.op) || 
+           IS_DEREF_OP(tree)            || 
+           (tree->opval.op == '&' && !tree->right) ||
+           tree->opval.op == PTR_OP) {
+           lvaluereq++;
+           if ((IS_ARRAY_OP(tree->left) && IS_ARRAY_OP(tree->left->left)) ||
+               (IS_DEREF_OP(tree) && IS_ARRAY_OP(tree->left)))
+           {
+               int olvr = lvaluereq ;
+               lvaluereq = 0;
+               left = operandFromAst(tree->left);
+               lvaluereq = olvr - 1;
+           } else {
+               left = operandFromAst(tree->left);
+               lvaluereq--;
+           }
+           if (IS_DEREF_OP(tree) && IS_DEREF_OP(tree->left))
+                   left = geniCodeRValue(left,TRUE);
        } else {
            left =  operandFromAst(tree->left);
        }
-       if (tree->opval.op == INC_OP || tree->opval.op == DEC_OP) {
+       if (tree->opval.op == INC_OP || 
+           tree->opval.op == DEC_OP) {
            lvaluereq++;
            right= operandFromAst(tree->right);
            lvaluereq--;
@@ -2794,8 +2809,11 @@ operand *ast2iCode (ast *tree)
     switch (tree->opval.op) {
        
     case '[' :    /* array operation */
-       left= geniCodeRValue (left,FALSE);
-       right=geniCodeRValue (right,TRUE);                 
+       {
+           link *ltype = operandType(left);
+           left= geniCodeRValue (left,IS_PTR(ltype->next) ? TRUE : FALSE);
+           right=geniCodeRValue (right,TRUE);             
+       }
        
        return geniCodeArray (left,right);