Added design, updated url
[fw/sdcc] / src / SDCCast.c
index cfa68e040219774a5e2e6f8444d669321787e816..847bc6f5bd256ebcb5d4ab822c2bc7aca9dc94ed 100644 (file)
@@ -27,7 +27,6 @@
 int currLineno  = 0;
 set *astList = NULL ;
 set *operKeyReset = NULL;
-extern char *currFname ;
 ast *staticAutos = NULL;
 int labelKey = 1 ;
 
@@ -50,12 +49,8 @@ char  buffer[1024];
 int noLineno = 0;
 int noAlloc = 0 ;
 symbol *currFunc ;
-extern int fatalError ;
-extern int lineno;
-extern char *filename ;
-extern set *publics;
-extern ast  *createIval  (ast *, link *, initList *, ast *);
-extern ast *createIvalCharPtr (ast *, link *, ast *);
+ast  *createIval  (ast *, sym_link *, initList *, ast *);
+ast *createIvalCharPtr (ast *, sym_link *, ast *);
 ast *optimizeRRCRLC ( ast * );
 ast *optimizeGetHbit(ast *);
 ast *backPatchLabels (ast *,symbol *,symbol *);
@@ -70,6 +65,7 @@ int ptt(ast *tree) {
 /*-----------------------------------------------------------------*/
 /* newAst - creates a fresh node for an expression tree           */
 /*-----------------------------------------------------------------*/
+#if 0
 ast  *newAst (int  type, void *op )
 {
     ast  *ex ;
@@ -90,10 +86,10 @@ ast  *newAst (int  type, void *op )
        ex->opval.val = (value *) op;
        break ;
     case EX_OP     :
-       ex->opval.op   = (int) op ;
+       ex->opval.op   = (long) op ;
        break ;
     case EX_LINK   :
-       ex->opval.lnk  = (link *) op;
+       ex->opval.lnk  = (sym_link *) op;
        break ;
     case EX_STMNT  :
        ex->opval.stmnt= (unsigned) op;
@@ -101,15 +97,60 @@ ast  *newAst (int  type, void *op )
     
     return ex;
 }
+#endif
+
+static ast* newAst_(unsigned type)
+{
+    ast  *ex ;
+    static int oldLineno = 0 ;
+
+    ALLOC(ex,sizeof(ast));    
+    
+    ex->type = type ;          
+    ex->lineno = (noLineno ? oldLineno : yylineno);
+    ex->filename = currFname ;
+    ex->level = NestLevel ;
+    ex->block = currBlockno ;
+    ex->initMode = inInitMode;
+    return ex;
+}
+
+ast* newAst_VALUE(value*val)
+{
+      ast* ex = newAst_(EX_VALUE);
+      ex->opval.val = val;
+      return ex;
+}
+
+ast* newAst_OP(unsigned op)
+{
+      ast*ex = newAst_(EX_OP);
+      ex->opval.op = op;
+      return ex;
+}
+
+ast* newAst_LINK(sym_link*val)
+{
+      ast* ex = newAst_(EX_LINK);
+      ex->opval.lnk = val;
+      return ex;
+}
+
+ast* newAst_STMNT(unsigned val)
+{
+      ast* ex = newAst_(EX_STMNT);
+      ex->opval.stmnt = val;
+      return ex;
+}
 
 /*-----------------------------------------------------------------*/
 /* newNode - creates a new node                                    */
 /*-----------------------------------------------------------------*/
-ast  *newNode ( int op,   ast  *left, ast *right   )
+ast  *newNode ( long op,   ast  *left, ast *right   )
 {
     ast  *ex ;
     
-    ex = newAst (EX_OP,(void *) op) ;
+    ex = newAst_OP(op) ;
     ex->left    = left ;
     ex->right   = right;
         
@@ -128,11 +169,13 @@ ast *newIfxNode (ast *condAst, symbol *trueLabel, symbol *falseLabel)
        
        /* then depending on the expression value */
        if ( floatFromVal(condAst->opval.val) )
-           ifxNode = newNode(GOTO, newAst(EX_VALUE,
-                                          symbolVal (trueLabel )),NULL);
+             ifxNode = newNode(GOTO,
+                               newAst_VALUE(symbolVal(trueLabel)),
+                               NULL);
        else
-           ifxNode = newNode(GOTO, newAst(EX_VALUE,
-                                          symbolVal (falseLabel )),NULL);
+             ifxNode = newNode(GOTO,
+                               newAst_VALUE(symbolVal(falseLabel)),
+                               NULL);
     }
     else {
        ifxNode = newNode(IFX,condAst,NULL);
@@ -447,7 +490,7 @@ value *resolveFromTable (value *val)
 /*-----------------------------------------------------------------*/
 /* funcOfType :- function of type with name                        */
 /*-----------------------------------------------------------------*/
-symbol *funcOfType (char *name, link *type, link *argType, 
+symbol *funcOfType (char *name, sym_link *type, sym_link *argType, 
                    int nArgs , int rent)
 {
     symbol *sym;    
@@ -516,7 +559,7 @@ int processParms (ast *func, value *defParm,
                  ast *actParm, 
                  int *parmNumber)
 {
-    link *fetype = func->etype;
+    sym_link *fetype = func->etype;
     
     /* if none of them exist */
     if ( !defParm && !actParm)
@@ -575,7 +618,7 @@ int processParms (ast *func, value *defParm,
        /* now change the current one to a cast */      
        actParm->type = EX_OP ;
        actParm->opval.op = CAST ;
-       actParm->left = newAst(EX_LINK,defParm->type);
+       actParm->left = newAst_LINK(defParm->type);
        actParm->right= pTree ;
        actParm->etype= defParm->etype;
        actParm->ftype= defParm->type;
@@ -592,7 +635,7 @@ int processParms (ast *func, value *defParm,
 /*-----------------------------------------------------------------*/
 /* createIvalType - generates ival for basic types                 */
 /*-----------------------------------------------------------------*/
-ast *createIvalType ( ast *sym,link  *type, initList *ilist)
+ast *createIvalType ( ast *sym,sym_link  *type, initList *ilist)
 {
     ast *iExpr;
 
@@ -607,7 +650,7 @@ ast *createIvalType ( ast *sym,link  *type, initList *ilist)
 /*-----------------------------------------------------------------*/
 /* createIvalStruct - generates initial value for structures       */
 /*-----------------------------------------------------------------*/
-ast *createIvalStruct (ast *sym,link *type,initList *ilist)
+ast *createIvalStruct (ast *sym,sym_link *type,initList *ilist)
 {
     ast *rast = NULL ;
     symbol   *sflds  ;
@@ -628,7 +671,7 @@ ast *createIvalStruct (ast *sym,link *type,initList *ilist)
        if (!iloop)
            break;
        sflds->implicit = 1;
-       lAst = newNode(PTR_OP,newNode('&',sym,NULL),newAst(EX_VALUE,symbolVal(sflds)));
+       lAst = newNode(PTR_OP,newNode('&',sym,NULL),newAst_VALUE(symbolVal(sflds)));
        lAst = decorateType(resolveSymbols(lAst));
        rast = decorateType(resolveSymbols(createIval (lAst, sflds->type, iloop,rast)));
     }
@@ -639,7 +682,7 @@ ast *createIvalStruct (ast *sym,link *type,initList *ilist)
 /*-----------------------------------------------------------------*/
 /* createIvalArray - generates code for array initialization       */
 /*-----------------------------------------------------------------*/
-ast *createIvalArray (ast  *sym, link *type, initList *ilist)
+ast *createIvalArray (ast  *sym, sym_link *type, initList *ilist)
 {
     ast *rast = NULL;
     initList *iloop ;
@@ -668,7 +711,7 @@ ast *createIvalArray (ast  *sym, link *type, initList *ilist)
        ast *aSym ;
        size++ ;
 
-       aSym = newNode('[',sym,newAst(EX_VALUE,valueFromLit(size-1)));
+       aSym = newNode('[',sym,newAst_VALUE(valueFromLit(size-1)));
        aSym = decorateType(resolveSymbols(aSym));
        rast = createIval (aSym,type->next,iloop,rast)   ;
        iloop = (iloop ? iloop->next : NULL) ;
@@ -696,7 +739,7 @@ ast *createIvalArray (ast  *sym, link *type, initList *ilist)
 /*-----------------------------------------------------------------*/
 /* createIvalCharPtr - generates initial values for char pointers  */
 /*-----------------------------------------------------------------*/
-ast *createIvalCharPtr (ast *sym, link *type, ast *iexpr)
+ast *createIvalCharPtr (ast *sym, sym_link *type, ast *iexpr)
 {      
     ast *rast = NULL ;
 
@@ -723,8 +766,8 @@ ast *createIvalCharPtr (ast *sym, link *type, ast *iexpr)
                           rast,
                           newNode('=',
                                   newNode('[', sym,
-                                          newAst(EX_VALUE,valueFromLit(i))),
-                                  newAst(EX_VALUE,valueFromLit(*s))));
+                                          newAst_VALUE(valueFromLit(i))),
+                                  newAst_VALUE(valueFromLit(*s))));
            i++;
            s++;
        }
@@ -732,8 +775,8 @@ ast *createIvalCharPtr (ast *sym, link *type, ast *iexpr)
                           rast,
                           newNode('=',
                                   newNode('[', sym,
-                                          newAst(EX_VALUE,valueFromLit(i))),
-                                  newAst(EX_VALUE,valueFromLit(*s))));
+                                          newAst_VALUE(valueFromLit(i))),
+                                  newAst_VALUE(valueFromLit(*s))));
        return decorateType(resolveSymbols(rast));
     }
 
@@ -743,7 +786,7 @@ ast *createIvalCharPtr (ast *sym, link *type, ast *iexpr)
 /*-----------------------------------------------------------------*/
 /* createIvalPtr - generates initial value for pointers            */
 /*-----------------------------------------------------------------*/
-ast *createIvalPtr (ast *sym,link *type,initList *ilist)
+ast *createIvalPtr (ast *sym,sym_link *type,initList *ilist)
 {    
     ast *rast;
     ast *iexpr ;
@@ -765,7 +808,7 @@ ast *createIvalPtr (ast *sym,link *type,initList *ilist)
 /*-----------------------------------------------------------------*/
 /* createIval - generates code for initial value                   */
 /*-----------------------------------------------------------------*/
-ast  *createIval  (ast *sym, link *type, initList *ilist, ast *wid)
+ast  *createIval  (ast *sym, sym_link *type, initList *ilist, ast *wid)
 {
     ast *rast = NULL;  
 
@@ -798,7 +841,7 @@ ast  *createIval  (ast *sym, link *type, initList *ilist, ast *wid)
 /*-----------------------------------------------------------------*/
 ast *initAggregates ( symbol *sym, initList *ival, ast *wid)
 {
-    return createIval (newAst(EX_VALUE,symbolVal(sym)),sym->type,ival,wid);
+    return createIval (newAst_VALUE(symbolVal(sym)),sym->type,ival,wid);
 }
 
 /*-----------------------------------------------------------------*/
@@ -835,7 +878,7 @@ ast *gatherAutoInit ( symbol *autoChain )
            if (IS_AGGREGATE(sym->type))
                work = initAggregates (sym, sym->ival,NULL);
            else
-               work = newNode('=' ,newAst(EX_VALUE,symbolVal(newSym)),
+               work = newNode('=' ,newAst_VALUE(symbolVal(newSym)),
                               list2expr(sym->ival));
            
            setAstLineno(work,sym->lineDef);
@@ -854,7 +897,7 @@ ast *gatherAutoInit ( symbol *autoChain )
            if (IS_AGGREGATE(sym->type)) 
                work = initAggregates (sym,sym->ival,NULL);
            else
-               work = newNode('=' ,newAst(EX_VALUE,symbolVal(sym)),
+               work = newNode('=' ,newAst_VALUE(symbolVal(sym)),
                               list2expr(sym->ival));
            
            setAstLineno (work,sym->lineDef);
@@ -893,7 +936,7 @@ static value *stringToSymbol (value *val)
     sym->block = sym->level = 0;
     sym->isstrlit = 1;
     /* create an ival */
-    sym->ival = newiList(INIT_NODE,newAst(EX_VALUE,val));
+    sym->ival = newiList(INIT_NODE,newAst_VALUE(val));
     if (noAlloc == 0) {
        /* allocate it */
        addSymChain(sym);
@@ -1049,7 +1092,7 @@ bool isLoopCountable (ast *initExpr, ast *condExpr, ast *loopExpr,
                isSymbolEqual (*sym,AST_SYMBOL(condExpr->left->left))) {
                
                *end = newNode('+', condExpr->left->right,
-                              newAst(EX_VALUE,constVal("1")));
+                              newAst_VALUE(constVal("1")));
                break;
            }
            return FALSE ;
@@ -1405,8 +1448,8 @@ static void replLoopSym ( ast *body, symbol *sym)
            
            body->type = EX_OP;
            body->opval.op = '-';
-           body->left = newAst(EX_VALUE,symbolVal(sym));
-           body->right= newAst(EX_VALUE,constVal("1"));
+           body->left = newAst_VALUE(symbolVal(sym));
+           body->right= newAst_VALUE(constVal("1"));
 
        }
            
@@ -1432,34 +1475,31 @@ ast *reverseLoop (ast *loop, symbol *sym, ast *init, ast *end)
                forbody
                <sym> -= 1;
                if (sym) goto for_continue ; 
-               <sym> = end - 1; */
+               <sym> = end */
     
     /* put it together piece by piece */
     rloop = newNode (NULLOP,
-                    createIf(newAst(EX_VALUE,symbolVal(sym)),
+                    createIf(newAst_VALUE(symbolVal(sym)),
                              newNode(GOTO,
-                                     newAst(EX_VALUE,
-                                            symbolVal(AST_FOR(loop,continueLabel))),
+                                     newAst_VALUE(symbolVal(AST_FOR(loop,continueLabel))),
                                      NULL),NULL),
                     newNode('=',
-                            newAst(EX_VALUE,symbolVal(sym)),
-                            newNode('-', end,
-                                    newAst(EX_VALUE,
-                                           constVal("1")))));
+                            newAst_VALUE(symbolVal(sym)),
+                            end));
 
     replLoopSym(loop->left, sym);
 
     rloop = newNode(NULLOP,
                    newNode('=',
-                           newAst(EX_VALUE,symbolVal(sym)),
+                           newAst_VALUE(symbolVal(sym)),
                            newNode('-',end,init)),
                    createLabel(AST_FOR(loop,continueLabel),
                                newNode(NULLOP,
                                        loop->left,
                                        newNode(NULLOP,
                                                newNode(SUB_ASSIGN,
-                                                       newAst(EX_VALUE,symbolVal(sym)),
-                                                       newAst(EX_VALUE,constVal("1"))),
+                                                       newAst_VALUE(symbolVal(sym)),
+                                                       newAst_VALUE(constVal("1"))),
                                                rloop ))));
     
     return decorateType(rloop);
@@ -1474,7 +1514,7 @@ ast *reverseLoop (ast *loop, symbol *sym, ast *init, ast *end)
 ast *decorateType (ast *tree)
 {         
     int parmNumber ;
-    link *p;
+    sym_link *p;
     
     if ( ! tree )
        return tree ;
@@ -1517,6 +1557,10 @@ ast *decorateType (ast *tree)
            
            /* otherwise just copy the type information */
            COPYTYPE(TTYPE(tree),TETYPE(tree),tree->opval.val->type);
+           if (funcInChain(tree->opval.val->type)) {
+                   tree->hasVargs = tree->opval.val->sym->hasVargs;
+                   tree->args = copyValueChain(tree->opval.val->sym->args) ;
+           }
            return tree ;
        }
        
@@ -1659,7 +1703,7 @@ ast *decorateType (ast *tree)
     case  INC_OP:  /* incerement operator unary so left only */
     case  DEC_OP:
        {
-           link *ltc = (tree->right ? RTYPE(tree) : LTYPE(tree) );
+           sym_link *ltc = (tree->right ? RTYPE(tree) : LTYPE(tree) );
            COPYTYPE(TTYPE(tree),TETYPE(tree),ltc);
            if (!tree->initMode && IS_CONSTANT(TETYPE(tree)))
                werror(E_CODE_WRITE,"++/--");
@@ -2235,7 +2279,7 @@ ast *decorateType (ast *tree)
        /*----------------------------*/
        /*         casting            */
        /*----------------------------*/
-    case CAST   :  /* change the type   */
+    case CAST:  /* change the type   */
        /* cannot cast to an aggregate type */
        if (IS_AGGREGATE(LTYPE(tree))) {
            werror(E_CAST_ILLEGAL);
@@ -2266,7 +2310,7 @@ ast *decorateType (ast *tree)
        /*       logical &&, ||       */
        /*----------------------------*/
     case AND_OP:
-    case OR_OP :
+    case OR_OP:
        /* each must me arithmetic type or be a pointer */
        if (!IS_PTR(LTYPE(tree)) && 
            !IS_ARRAY(LTYPE(tree)) && 
@@ -2620,8 +2664,7 @@ ast *decorateType (ast *tree)
        if (checkType(currFunc->type->next,RTYPE(tree)) < 0 ) {
            tree->right = 
                decorateType(newNode(CAST,
-                                    newAst(EX_LINK,
-                                           copyLinkChain(currFunc->type->next)),
+                                    newAst_LINK(copyLinkChain(currFunc->type->next)),
                                     tree->right));
        }
        
@@ -2709,7 +2752,7 @@ ast *decorateType (ast *tree)
 /*-----------------------------------------------------------------*/
 /* sizeofOp - processes size of operation                          */
 /*-----------------------------------------------------------------*/
-value  *sizeofOp( link  *type)
+value  *sizeofOp( sym_link  *type)
 {
        char buff[10];
 
@@ -2868,7 +2911,7 @@ ast  *createLabel  ( symbol  *label,  ast  *stmnt  )
     
     label->islbl = 1;
     label->key = labelKey++ ;
-    rValue =  newNode (LABEL,newAst(EX_VALUE,symbolVal(label)),stmnt);  
+    rValue =  newNode (LABEL,newAst_VALUE(symbolVal(label)),stmnt);  
     rValue->lineno = 0;
     
     return rValue ;
@@ -3001,7 +3044,7 @@ ast *createIf ( ast *condAst, ast *ifBody, ast *elseBody )
     if ( elseBody ) {
        ifBody = newNode(NULLOP,ifBody,
                         newNode(GOTO,
-                                newAst(EX_VALUE,symbolVal(ifEnd)),             
+                                newAst_VALUE(symbolVal(ifEnd)),
                                 NULL));
        /* put the elseLabel on the else body */
        elseBody = createLabel (ifFalse,elseBody);
@@ -3118,7 +3161,7 @@ ast *createFor ( symbol *trueLabel, symbol *continueLabel ,
                           newNode(NULLOP,
                                   loopExpr,
                                   newNode(GOTO,
-                                          newAst(EX_VALUE,symbolVal(condLabel)),
+                                          newAst_VALUE(symbolVal(condLabel)),
                                           NULL)));
     /* now start putting them together */
     forTree = newNode(NULLOP,initExpr,condExpr);
@@ -3161,8 +3204,7 @@ ast *createWhile (symbol *trueLabel, symbol *continueLabel,
     whileBody = newNode(NULLOP,
                        whileBody,
                        newNode(GOTO,
-                               newAst(EX_VALUE,
-                                      symbolVal(continueLabel)),
+                               newAst_VALUE(symbolVal(continueLabel)),
                                createLabel(falseLabel,NULL)));
     
     /* put it all together */
@@ -3380,12 +3422,12 @@ ast  *optimizeCompare ( ast *root )
        case '>' :
        case '<' :
        case NE_OP :
-           optExpr = newAst(EX_VALUE,constVal("0"));
+           optExpr = newAst_VALUE(constVal("0"));
            break;
        case GE_OP :
        case LE_OP :
        case EQ_OP :
-           optExpr = newAst(EX_VALUE,constVal("1"));
+           optExpr = newAst_VALUE(constVal("1"));
            break;
        }
 
@@ -3421,16 +3463,16 @@ ast  *optimizeCompare ( ast *root )
                
            case '<' : /* bit value < 1 means 0 */
            case NE_OP :
-               optExpr = newNode('!',newAst(EX_VALUE,vleft),NULL);
+               optExpr = newNode('!',newAst_VALUE(vleft),NULL);
                break;
                
            case LE_OP : /* bit value <= 1 means no check */
-               optExpr = newAst(EX_VALUE,vright);              
+               optExpr = newAst_VALUE(vright);         
                break;
                
            case GE_OP : /* bit value >= 1 means only check for = */
            case EQ_OP :
-               optExpr = newAst(EX_VALUE,vleft);               
+               optExpr = newAst_VALUE(vleft);          
                break;
            }
        } else { /* literal is zero */
@@ -3442,7 +3484,7 @@ ast  *optimizeCompare ( ast *root )
                
            case '>' : /* bit value > 0 means 1 */
            case NE_OP :
-               optExpr = newAst(EX_VALUE,vleft);            
+               optExpr = newAst_VALUE(vleft);       
                break;
                
            case LE_OP : /* bit value <= 0 means no check */
@@ -3452,7 +3494,7 @@ ast  *optimizeCompare ( ast *root )
                break;
                
            case EQ_OP : /* bit == 0 means ! of bit */
-               optExpr = newNode('!',newAst(EX_VALUE,vleft),NULL);           
+               optExpr = newNode('!',newAst_VALUE(vleft),NULL);              
                break;
            }
        }                      
@@ -3523,7 +3565,7 @@ ast  *createFunction   (symbol  *name,   ast  *body )
     ast  *ex ;
     symbol *csym;
     int stack = 0 ;
-    link *fetype;       
+    sym_link *fetype;       
     iCode *piCode = NULL;
     
     /* if check function return 0 then some problem */
@@ -3594,7 +3636,7 @@ ast  *createFunction   (symbol  *name,   ast  *body )
     body = resolveSymbols(body); /* resolve the symbols */
     body = decorateType (body);  /* propagateType & do semantic checks */
     
-    ex = newAst (EX_VALUE, symbolVal(name));    /* create name       */
+    ex = newAst_VALUE(symbolVal(name));    /* create name       */
     ex = newNode (FUNCTION,ex,body);
     ex->values.args = name->args ;