* src/SDCCmain.c (linkEdit): Added runtime path detection to the mcs51 port.
[fw/sdcc] / src / SDCCast.c
index 4572ac760c72eb72dc2db377f491334cc8e78b99..0277d7ff20e9b64e0546af0a0dacefff012123f8 100644 (file)
@@ -53,6 +53,7 @@ ast *createIvalCharPtr (ast *, sym_link *, ast *);
 ast *optimizeRRCRLC (ast *);
 ast *optimizeGetHbit (ast *);
 ast *backPatchLabels (ast *, symbol *, symbol *);
+void PA(ast *t);
 int inInitMode = 0;
 memmap *GcurMemmap=NULL;  /* points to the memmap that's currently active */
 FILE *codeOutFile;
@@ -258,6 +259,9 @@ copyAst (ast * src)
   dest->level = src->level;
   dest->funcName = src->funcName;
 
+  if (src->ftype)
+    dest->etype = getSpec (dest->ftype = copyLinkChain (src->ftype));
+
   /* if this is a leaf */
   /* if value */
   if (src->type == EX_VALUE)
@@ -278,9 +282,6 @@ copyAst (ast * src)
   /* if this is a node that has special values */
   copyAstValues (dest, src);
 
-  if (src->ftype)
-    dest->etype = getSpec (dest->ftype = copyLinkChain (src->ftype));
-
   dest->trueLabel = copySymbol (src->trueLabel);
   dest->falseLabel = copySymbol (src->falseLabel);
   dest->left = copyAst (src->left);
@@ -642,6 +643,11 @@ processParms (ast * func,
       return 1;
     }
 
+  if (IS_VOID(actParm->ftype)) {
+    werror (E_VOID_VALUE_USED);
+    return 1;
+  }
+
   /* If this is a varargs function... */
   if (!defParm && actParm && IFFUNC_HASVARARGS(func->ftype))
     {
@@ -685,7 +691,6 @@ processParms (ast * func,
 
       if (IS_AGGREGATE (ftype))
        {
-         // jwk: don't we need aggregateToPointer here?
          newType = newAst_LINK (copyLinkChain (ftype));
          DCL_TYPE (newType->opval.lnk) = GPOINTER;
        }
@@ -709,7 +714,7 @@ processParms (ast * func,
     }
 
   /* if defined parameters ended but actual has not & */
-  /* stackAuto                */
+  /* reentrant */
   if (!defParm && actParm &&
       (options.stackAuto || IFFUNC_ISREENT (func->ftype)))
     return 0;
@@ -729,7 +734,7 @@ processParms (ast * func,
        * Therefore, if there are more defined parameters, the caller didn't
        * supply enough.
        */
-      if (0 && rightmost && defParm->next)
+      if (rightmost && defParm->next)
        {
          werror (E_TOO_FEW_PARMS);
          return 1;
@@ -739,12 +744,7 @@ processParms (ast * func,
   /* the parameter type must be at least castable */
   if (compareType (defParm->type, actParm->ftype) == 0) {
     werror (E_INCOMPAT_TYPES);
-    fprintf (stderr, "type --> '");
-    printTypeChain (actParm->ftype, stderr);
-    fprintf (stderr, "' ");
-    fprintf (stderr, "assigned to type --> '");
-    printTypeChain (defParm->type, stderr);
-    fprintf (stderr, "'\n");
+    printFromToType (actParm->ftype, defParm->type);
     return 1;
   }
 
@@ -791,6 +791,7 @@ ast *
 createIvalStruct (ast * sym, sym_link * type, initList * ilist)
 {
   ast *rast = NULL;
+  ast *lAst;
   symbol *sflds;
   initList *iloop;
 
@@ -805,8 +806,6 @@ createIvalStruct (ast * sym, sym_link * type, initList * ilist)
 
   for (; sflds; sflds = sflds->next, iloop = (iloop ? iloop->next : NULL))
     {
-      ast *lAst;
-
       /* if we have come to end */
       if (!iloop)
        break;
@@ -1033,7 +1032,7 @@ ast * initAggregates (symbol * sym, initList * ival, ast * wid) {
 
     if (!TARGET_IS_MCS51 || !(options.model==MODEL_LARGE)) {
       fprintf (stderr, "Can't \"TRY_THE_NEW_INITIALIZER\" unless "
-              "with -mmcs51 and --model-large");
+              "with -mmcs51 and --model-large\n");
       exit(404);
     }
 
@@ -1827,31 +1826,6 @@ pushTypeCastToLeaves (sym_link * type, ast * node, ast ** parentPtr)
 
 #endif
 
-/*-----------------------------------------------------------------*/
-/* Given an assignment operation in a tree, determine if the LHS   */
-/* (the result) has a different (integer) type than the RHS.     */
-/* If so, walk the RHS and add a typecast to the type of the LHS   */
-/* to all leaf nodes.              */
-/*-----------------------------------------------------------------*/
-void 
-propAsgType (ast * tree)
-{
-#ifdef DEMAND_INTEGER_PROMOTION
-  if (!IS_INTEGRAL (LTYPE (tree)) || !IS_INTEGRAL (RTYPE (tree)))
-    {
-      /* Nothing to do here... */
-      return;
-    }
-
-  if (getSize (LTYPE (tree)) > getSize (RTYPE (tree)))
-    {
-      pushTypeCastToLeaves (LTYPE (tree), tree->right, &(tree->right));
-    }
-#else
-  (void) tree;
-#endif
-}
-
 /*-----------------------------------------------------------------*/
 /* decorateType - compute type for this tree also does type cheking */
 /*          this is done bottom up, since type have to flow upwards */
@@ -2158,7 +2132,7 @@ decorateType (ast * tree)
       /* if bit field then error */
       if (IS_BITVAR (tree->left->etype))
        {
-         werror (E_ILLEGAL_ADDR, "addrress of bit variable");
+         werror (E_ILLEGAL_ADDR, "address of bit variable");
          goto errorTreeReturn;
        }
 
@@ -2174,7 +2148,13 @@ decorateType (ast * tree)
          goto errorTreeReturn;
        }
 
-      if (LRVAL (tree))
+      if (IS_LITERAL(LTYPE(tree)))
+       {
+         werror (E_ILLEGAL_ADDR, "address of literal");
+         goto errorTreeReturn;
+       }
+
+     if (LRVAL (tree))
        {
          werror (E_LVALUE_REQUIRED, "address of");
          goto errorTreeReturn;
@@ -2940,8 +2920,6 @@ decorateType (ast * tree)
        }
       LLVAL (tree) = 1;
 
-      propAsgType (tree);
-
       return tree;
 
     case AND_ASSIGN:
@@ -2969,8 +2947,6 @@ decorateType (ast * tree)
        }
       LLVAL (tree) = 1;
 
-      propAsgType (tree);
-
       return tree;
 
 /*------------------------------------------------------------------*/
@@ -3006,8 +2982,6 @@ decorateType (ast * tree)
        }
       LLVAL (tree) = 1;
 
-      propAsgType (tree);
-
       return tree;
 
 /*------------------------------------------------------------------*/
@@ -3051,8 +3025,6 @@ decorateType (ast * tree)
       tree->right = decorateType (newNode ('+', copyAst (tree->left), tree->right));
       tree->opval.op = '=';
 
-      propAsgType (tree);
-
       return tree;
 
 /*------------------------------------------------------------------*/
@@ -3085,12 +3057,7 @@ decorateType (ast * tree)
       if (IS_VOID (LTYPE (tree)))
        {
          werror (E_CAST_ZERO);
-         fprintf (stderr, "type --> '");
-         printTypeChain (RTYPE (tree), stderr);
-         fprintf (stderr, "' ");
-         fprintf (stderr, "assigned to type --> '");
-         printTypeChain (LTYPE (tree), stderr);
-         fprintf (stderr, "'\n");
+         printFromToType(RTYPE(tree), LTYPE(tree));
        }
 
       TETYPE (tree) = getSpec (TTYPE (tree) =
@@ -3107,8 +3074,6 @@ decorateType (ast * tree)
          goto errorTreeReturn;
        }
 
-      propAsgType (tree);
-
       return tree;
 
 /*------------------------------------------------------------------*/
@@ -3152,11 +3117,7 @@ decorateType (ast * tree)
       if (compareType (currFunc->type->next, RTYPE (tree)) == 0)
        {
          werror (W_RETURN_MISMATCH);
-         fprintf (stderr, "from type '");
-         printTypeChain (RTYPE(tree), stderr);
-         fprintf (stderr, "' to type '");
-         printTypeChain (currFunc->type->next, stderr);
-         fprintf (stderr, "'\n");
+         printFromToType (RTYPE(tree), currFunc->type->next);
          goto errorTreeReturn;
        }
 
@@ -3171,19 +3132,10 @@ decorateType (ast * tree)
       /* if there is going to be a casing required then add it */
       if (compareType (currFunc->type->next, RTYPE (tree)) < 0)
        {
-#if 0 && defined DEMAND_INTEGER_PROMOTION
-         if (IS_INTEGRAL (currFunc->type->next))
-           {
-             pushTypeCastToLeaves (currFunc->type->next, tree->right, &(tree->right));
-           }
-         else
-#endif
-           {
-             tree->right =
-               decorateType (newNode (CAST,
-                        newAst_LINK (copyLinkChain (currFunc->type->next)),
-                                      tree->right));
-           }
+         tree->right =
+           decorateType (newNode (CAST,
+                          newAst_LINK (copyLinkChain (currFunc->type->next)),
+                                  tree->right));
        }
 
       RRVAL (tree) = 1;
@@ -4222,7 +4174,8 @@ createFunction (symbol * name, ast * body)
   ex = newAst_VALUE (symbolVal (name));                /* create name       */
   ex = newNode (FUNCTION, ex, body);
   ex->values.args = FUNC_ARGS(name->type);
-
+  ex->decorated=1;
+  if (options.dump_tree) PA(ex);
   if (fatalError)
     {
       werror (E_FUNC_NO_CODE, name->name);
@@ -4312,25 +4265,40 @@ void ast_print (ast * tree, FILE *outfile, int indent)
        }
        
        if (tree->opval.op == FUNCTION) {
-               fprintf(outfile,"FUNCTION (%p) type (",tree);
+               int arg=0;
+               value *args=FUNC_ARGS(tree->left->opval.val->type);
+               fprintf(outfile,"FUNCTION (%s=%p) type (", 
+                       tree->left->opval.val->name, tree);
                printTypeChain (tree->ftype,outfile);
+               fprintf(outfile,") args (");
+               do {
+                 if (arg) {
+                   fprintf (outfile, ", ");
+                 }
+                 printTypeChain (args->type, outfile);
+                 arg++;
+                 args=args->next;
+               } while (args);
                fprintf(outfile,")\n");
-               ast_print(tree->left,outfile,indent+4);
-               ast_print(tree->right,outfile,indent+4);
+               ast_print(tree->left,outfile,indent);
+               ast_print(tree->right,outfile,indent);
                return ;
        }
        if (tree->opval.op == BLOCK) {
                symbol *decls = tree->values.sym;
+               INDENT(indent,outfile);
                fprintf(outfile,"{\n");
                while (decls) {
                        INDENT(indent+4,outfile);
-                       fprintf(outfile,"DECLARE SYMBOL %s, type(",decls->name);
+                       fprintf(outfile,"DECLARE SYMBOL (%s=%p) type (",
+                               decls->name, decls);
                        printTypeChain(decls->type,outfile);
                        fprintf(outfile,")\n");
                        
                        decls = decls->next;                    
                }
                ast_print(tree->right,outfile,indent+4);
+               INDENT(indent,outfile);
                fprintf(outfile,"}\n");
                return;
        }
@@ -4363,7 +4331,8 @@ void ast_print (ast * tree, FILE *outfile, int indent)
                        } else {
                                fprintf(outfile,"SYMBOL ");
                        }
-                       fprintf(outfile,"(%p) name= %s ",tree,tree->opval.val->sym->name);
+                       fprintf(outfile,"(%s=%p)",
+                               tree->opval.val->sym->name,tree);
                }
                if (tree->ftype) {
                        fprintf(outfile," type (");
@@ -4636,7 +4605,9 @@ void ast_print (ast * tree, FILE *outfile, int indent)
                /*         casting            */
                /*----------------------------*/
        case CAST:                      /* change the type   */
-               fprintf(outfile,"CAST (%p) type (",tree);
+               fprintf(outfile,"CAST (%p) from type (",tree);
+               printTypeChain(tree->right->ftype,outfile);
+               fprintf(outfile,") to type (");
                printTypeChain(tree->ftype,outfile);
                fprintf(outfile,")\n");
                ast_print(tree->right,outfile,indent+4);
@@ -4890,18 +4861,17 @@ void ast_print (ast * tree, FILE *outfile, int indent)
                /* ifx Statement              */
                /*----------------------------*/
        case IFX:
-               ast_print(tree->left,outfile,indent);
-               INDENT(indent,outfile);
                fprintf(outfile,"IF (%p) \n",tree);
+               ast_print(tree->left,outfile,indent+4);
                if (tree->trueLabel) {
                        INDENT(indent,outfile);
-                       fprintf(outfile,"NE(==) 0 goto %s\n",tree->trueLabel->name);
+                       fprintf(outfile,"NE(!=) 0 goto %s\n",tree->trueLabel->name);
                }
                if (tree->falseLabel) {
                        INDENT(indent,outfile);
                        fprintf(outfile,"EQ(==) 0 goto %s\n",tree->falseLabel->name);
                }
-               ast_print(tree->right,outfile,indent);
+               ast_print(tree->right,outfile,indent+4);
                return ;
                /*------------------------------------------------------------------*/
                /*----------------------------*/
@@ -4934,5 +4904,5 @@ void ast_print (ast * tree, FILE *outfile, int indent)
 
 void PA(ast *t)
 {
-       ast_print(t,stdout,1);
+       ast_print(t,stdout,0);
 }