]> git.sur5r.net Git - cc65/blobdiff - src/cc65/locals.c
Fixed handling of function definitions with an empty parameter list. According
[cc65] / src / cc65 / locals.c
index 7b7af04792318f5eb99547dac35b4dfa0884e880..af2c5a28be654efc4979fba55442d24466049eeb 100644 (file)
@@ -6,7 +6,7 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2000-2003 Ullrich von Bassewitz                                       */
+/* (C) 2000-2004 Ullrich von Bassewitz                                       */
 /*               Römerstrasse 52                                             */
 /*               D-70794 Filderstadt                                         */
 /* EMail:        uz@cc65.org                                                 */
 #include "expr.h"
 #include "function.h"
 #include "global.h"
+#include "loadexpr.h"
 #include "locals.h"
+#include "stackptr.h"
+#include "standard.h"
 #include "symtab.h"
 #include "typeconv.h"
 
@@ -78,7 +81,7 @@ static unsigned ParseRegisterDecl (Declaration* Decl, unsigned* SC, int Reg)
     /* Check for an optional initialization */
     if (CurTok.Tok == TOK_ASSIGN) {
 
-        ExprDesc lval;
+        ExprDesc Expr;
 
         /* Skip the '=' */
         NextToken ();
@@ -111,13 +114,13 @@ static unsigned ParseRegisterDecl (Declaration* Decl, unsigned* SC, int Reg)
         } else {
 
             /* Parse the expression */
-            int k = hie1 (InitExprDesc (&lval));
+            hie1 (&Expr);
 
             /* Convert it to the target type */
-            k = TypeConversion (&lval, k, Decl->Type);
+            TypeConversion (&Expr, Decl->Type);
 
             /* Load the value into the primary */
-            ExprLoad (CF_NONE, k, &lval);
+            LoadExpr (CF_NONE, &Expr);
 
             /* Store the value into the variable */
             g_putstatic (CF_REGVAR | TypeOf (Decl->Type), Reg, 0);
@@ -156,12 +159,12 @@ static unsigned ParseAutoDecl (Declaration* Decl, unsigned* SC)
     unsigned Size = SizeOf (Decl->Type);
 
     /* Check if this is a variable on the stack or in static memory */
-    if (StaticLocals == 0) {
+    if (IS_Get (&StaticLocals) == 0) {
 
         /* Check for an optional initialization */
         if (CurTok.Tok == TOK_ASSIGN) {
 
-            ExprDesc lval;
+            ExprDesc Expr;
 
             /* Skip the '=' */
             NextToken ();
@@ -200,8 +203,6 @@ static unsigned ParseAutoDecl (Declaration* Decl, unsigned* SC)
 
             } else {
 
-                int k;
-
                 /* Allocate previously reserved local space */
                 F_AllocLocalSpace (CurrentFunc);
 
@@ -209,23 +210,23 @@ static unsigned ParseAutoDecl (Declaration* Decl, unsigned* SC)
                 Flags = (Size == SIZEOF_CHAR)? CF_FORCECHAR : CF_NONE;
 
                 /* Parse the expression */
-                k = hie1 (InitExprDesc (&lval));
+                hie1 (&Expr);
 
                 /* Convert it to the target type */
-                k = TypeConversion (&lval, k, Decl->Type);
+                TypeConversion (&Expr, Decl->Type);
 
                 /* If the value is not const, load it into the primary.
                  * Otherwise pass the information to the code generator.
                  */
-                if (k != 0 || lval.Flags != E_MCONST) {
-                    ExprLoad (CF_NONE, k, &lval);
-                    k = 0;
-                } else {
+                if (ED_IsConstAbsInt (&Expr)) {
                     Flags |= CF_CONST;
+                } else {
+                    LoadExpr (CF_NONE, &Expr);
+                    ED_MakeRVal (&Expr);
                 }
 
                 /* Push the value */
-                g_push (Flags | TypeOf (Decl->Type), lval.ConstVal);
+                g_push (Flags | TypeOf (Decl->Type), Expr.IVal);
 
             }
 
@@ -233,7 +234,7 @@ static unsigned ParseAutoDecl (Declaration* Decl, unsigned* SC)
             *SC |= SC_REF;
 
             /* Variable is located at the current SP */
-            SymData = oursp;
+            SymData = StackPtr;
 
         } else {
             /* Non-initialized local variable. Just keep track of
@@ -260,7 +261,7 @@ static unsigned ParseAutoDecl (Declaration* Decl, unsigned* SC)
         /* Allow assignments */
         if (CurTok.Tok == TOK_ASSIGN) {
 
-            ExprDesc lval;
+            ExprDesc Expr;
 
             /* Skip the '=' */
             NextToken ();
@@ -285,13 +286,13 @@ static unsigned ParseAutoDecl (Declaration* Decl, unsigned* SC)
             } else {
 
                 /* Parse the expression */
-                int k = hie1 (InitExprDesc (&lval));
+                hie1 (&Expr);
 
                 /* Convert it to the target type */
-                k = TypeConversion (&lval, k, Decl->Type);
+                TypeConversion (&Expr, Decl->Type);
 
                 /* Load the value into the primary */
-                ExprLoad (CF_NONE, k, &lval);
+                LoadExpr (CF_NONE, &Expr);
 
                 /* Store the value into the variable */
                 g_putstatic (TypeOf (Decl->Type), SymData, 0);
@@ -379,7 +380,7 @@ static unsigned ParseStaticDecl (Declaration* Decl, unsigned* SC)
 static void ParseOneDecl (const DeclSpec* Spec)
 /* Parse one variable declaration */
 {
-    unsigned    SC;            /* Storage class for symbol */
+    unsigned    SC;            /* Storage class for symbol */
     unsigned    SymData = 0;    /* Symbol data (offset, label name, ...) */
     Declaration Decl;          /* Declaration data structure */
 
@@ -426,12 +427,25 @@ static void ParseOneDecl (const DeclSpec* Spec)
                } else if ((SC & SC_AUTO) == SC_AUTO) {
             /* Auto variable */
             SymData = ParseAutoDecl (&Decl, &SC);
+        } else if ((SC & SC_EXTERN) == SC_EXTERN) {
+            /* External identifier - may not get initialized */
+            if (CurTok.Tok == TOK_ASSIGN) {
+                Error ("Cannot initialize externals");
+            }
+            SymData = 0;
                } else if ((SC & SC_STATIC) == SC_STATIC) {
             /* Static variable */
             SymData = ParseStaticDecl (&Decl, &SC);
                } else {
             Internal ("Invalid storage class in ParseOneDecl: %04X", SC);
         }
+
+        /* If the standard was not set explicitly to C89, print a warning
+         * for variables with implicit int type.
+         */
+        if ((Spec->Flags & DS_DEF_TYPE) != 0 && IS_Get (&Standard) >= STD_C99) {
+            Warning ("Implicit `int' is an obsolete feature");
+        }
     }
 
     /* If the symbol is not marked as external, it will be defined now */
@@ -443,49 +457,49 @@ static void ParseOneDecl (const DeclSpec* Spec)
     AddLocalSym (Decl.Ident, Decl.Type, SC, SymData);
 }
 
-
+                        
 
 void DeclareLocals (void)
 /* Declare local variables and types. */
 {
     /* Remember the current stack pointer */
-    int InitialStack = oursp;
+    int InitialStack = StackPtr;
 
     /* Loop until we don't find any more variables */
     while (1) {
 
-       /* Check variable declarations. We need to distinguish between a
-        * default int type and the end of variable declarations. So we
-        * will do the following: If there is no explicit storage class
-        * specifier *and* no explicit type given, *and* no type qualifiers
+       /* Check variable declarations. We need to distinguish between a
+        * default int type and the end of variable declarations. So we
+        * will do the following: If there is no explicit storage class
+        * specifier *and* no explicit type given, *and* no type qualifiers
          * have been read, it is assumed that we have reached the end of
          * declarations.
-        */
-       DeclSpec Spec;
-       ParseDeclSpec (&Spec, SC_AUTO, T_INT);
+        */
+       DeclSpec Spec;
+       ParseDeclSpec (&Spec, SC_AUTO, T_INT);
                if ((Spec.Flags & DS_DEF_STORAGE) != 0 &&       /* No storage spec */
             (Spec.Flags & DS_DEF_TYPE) != 0    &&       /* No type given */
             GetQualifier (Spec.Type) == T_QUAL_NONE) {  /* No type qualifier */
-           break;
-       }
+           break;
+       }
 
-       /* Accept type only declarations */
-       if (CurTok.Tok == TOK_SEMI) {
-           /* Type declaration only */
-           CheckEmptyDecl (&Spec);
-           NextToken ();
-           continue;
-       }
+       /* Accept type only declarations */
+       if (CurTok.Tok == TOK_SEMI) {
+           /* Type declaration only */
+           CheckEmptyDecl (&Spec);
+           NextToken ();
+           continue;
+       }
 
        /* Parse a comma separated variable list */
        while (1) {
 
-           /* Parse one declaration */
-           ParseOneDecl (&Spec);
+           /* Parse one declaration */
+           ParseOneDecl (&Spec);
 
-           /* Check if there is more */
+           /* Check if there is more */
            if (CurTok.Tok == TOK_COMMA) {
-               /* More to come */
+               /* More to come */
                NextToken ();
            } else {
                /* Done */
@@ -503,7 +517,7 @@ void DeclareLocals (void)
     /* In case we've allocated local variables in this block, emit a call to
      * the stack checking routine if stack checks are enabled.
      */
-    if (CheckStack && InitialStack != oursp) {
+    if (IS_Get (&CheckStack) && InitialStack != StackPtr) {
                g_cstackcheck ();
     }
 }