]> git.sur5r.net Git - cc65/blobdiff - src/cc65/locals.c
In a function call for all parameters not covered by a prototype, convert
[cc65] / src / cc65 / locals.c
index a4411e410fd8b0e982a176f96ff6a5d464619199..80f27cd71f29ad47a54a578078fd42b470adb9b5 100644 (file)
@@ -6,9 +6,9 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2000-2002 Ullrich von Bassewitz                                       */
-/*               Wacholderweg 14                                             */
-/*               D-70597 Stuttgart                                           */
+/* (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 "symtab.h"
+#include "loadexpr.h"
 #include "locals.h"
+#include "stackptr.h"
+#include "symtab.h"
+#include "typeconv.h"
 
 
 
@@ -62,7 +65,6 @@ static unsigned ParseRegisterDecl (Declaration* Decl, unsigned* SC, int Reg)
  * symbol data, which is the offset of the variable in the register bank.
  */
 {
-    unsigned Flags;
     unsigned InitLabel;
 
     /* Determine if this is a compound variable */
@@ -78,7 +80,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 ();
@@ -94,36 +96,33 @@ static unsigned ParseRegisterDecl (Declaration* Decl, unsigned* SC, int Reg)
             g_defdatalabel (InitLabel);
 
             /* Parse the initialization generating a memory image of the
-             * data in the RODATA segment.
+             * data in the RODATA segment. The function does return the size
+             * of the initialization data, which may be greater than the
+             * actual size of the type, if the type is a structure with a
+             * flexible array member that has been initialized. Since we must
+             * know the size of the data in advance for register variables,
+             * we cannot allow that here.
              */
-            ParseInit (Decl->Type);
+            if (ParseInit (Decl->Type) != Size) {
+                Error ("Cannot initialize flexible array members of storage class `register'");
+            }
 
             /* Generate code to copy this data into the variable space */
             g_initregister (InitLabel, Reg, Size);
 
         } else {
 
-            /* Setup the type flags for the assignment */
-            Flags = CF_NONE;
-            if (Size == SIZEOF_CHAR) {
-                Flags |= CF_FORCECHAR;
-            }
+            /* Parse the expression */
+            hie1 (&Expr);
 
-            /* Get the expression into the primary */
-            if (evalexpr (Flags, hie1, &lval) == 0) {
-                /* Constant expression. Adjust the types */
-                assignadjust (Decl->Type, &lval);
-                Flags |= CF_CONST;
-                /* Load it into the primary */
-                exprhs (Flags, 0, &lval);
-            } else {
-                /* Expression is not constant and in the primary */
-                assignadjust (Decl->Type, &lval);
-            }
+            /* Convert it to the target type */
+            TypeConversion (&Expr, Decl->Type);
+
+            /* Load the value into the primary */
+            LoadExpr (CF_NONE, &Expr);
 
             /* Store the value into the variable */
-            Flags |= CF_REGVAR;
-            g_putstatic (Flags | TypeOf (Decl->Type), Reg, 0);
+            g_putstatic (CF_REGVAR | TypeOf (Decl->Type), Reg, 0);
 
         }
 
@@ -159,12 +158,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 ();
@@ -172,14 +171,6 @@ static unsigned ParseAutoDecl (Declaration* Decl, unsigned* SC)
             /* Special handling for compound types */
             if (IsCompound) {
 
-                /* First reserve space for the variable */
-                SymData = F_ReserveLocalSpace (CurrentFunc, Size);
-
-                /* Next, allocate the space on the stack. This means that the
-                 * variable is now located at offset 0 from the current sp.
-                 */
-                F_AllocLocalSpace (CurrentFunc);
-
                 /* Switch to read only data */
                 g_userodata ();
 
@@ -188,11 +179,25 @@ static unsigned ParseAutoDecl (Declaration* Decl, unsigned* SC)
                 g_defdatalabel (InitLabel);
 
                 /* Parse the initialization generating a memory image of the
-                 * data in the RODATA segment.
+                 * data in the RODATA segment. The function will return the
+                 * actual size of the initialization data, which may be
+                 * greater than the size of the variable if it is a struct
+                 * that contains a flexible array member and we're not in
+                 * ANSI mode.
                  */
-                ParseInit (Decl->Type);
+                Size = ParseInit (Decl->Type);
 
-                /* Generate code to copy this data into the variable space */
+                /* Now reserve space for the variable on the stack */
+                SymData = F_ReserveLocalSpace (CurrentFunc, Size);
+
+                /* Next, allocate the space on the stack. This means that the
+                 * variable is now located at offset 0 from the current sp.
+                 */
+                F_AllocLocalSpace (CurrentFunc);
+
+                /* Generate code to copy the initialization data into the
+                 * variable space
+                 */
                 g_initauto (InitLabel, Size);
 
             } else {
@@ -203,18 +208,24 @@ static unsigned ParseAutoDecl (Declaration* Decl, unsigned* SC)
                 /* Setup the type flags for the assignment */
                 Flags = (Size == SIZEOF_CHAR)? CF_FORCECHAR : CF_NONE;
 
-                /* Get the expression into the primary */
-                if (evalexpr (Flags, hie1, &lval) == 0) {
-                    /* Constant expression. Adjust the types */
-                    assignadjust (Decl->Type, &lval);
+                /* Parse the expression */
+                hie1 (&Expr);
+
+                /* Convert it to the target 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 (ED_IsConstAbsInt (&Expr)) {
                     Flags |= CF_CONST;
                 } else {
-                    /* Expression is not constant and in the primary */
-                    assignadjust (Decl->Type, &lval);
+                    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);
 
             }
 
@@ -222,7 +233,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
@@ -249,7 +260,7 @@ static unsigned ParseAutoDecl (Declaration* Decl, unsigned* SC)
         /* Allow assignments */
         if (CurTok.Tok == TOK_ASSIGN) {
 
-            ExprDesc lval;
+            ExprDesc Expr;
 
             /* Skip the '=' */
             NextToken ();
@@ -273,24 +284,17 @@ static unsigned ParseAutoDecl (Declaration* Decl, unsigned* SC)
 
             } else {
 
-                /* Setup the type flags for the assignment */
-                Flags = (Size == SIZEOF_CHAR)? CF_FORCECHAR : CF_NONE;
+                /* Parse the expression */
+                hie1 (&Expr);
 
-                /* Get the expression into the primary */
-                if (evalexpr (Flags, hie1, &lval) == 0) {
-                    /* Constant expression. Adjust the types */
-                    assignadjust (Decl->Type, &lval);
-                    Flags |= CF_CONST;
-                    /* Load it into the primary */
-                    exprhs (Flags, 0, &lval);
-                } else {
-                    /* Expression is not constant and in the primary */
-                    assignadjust (Decl->Type, &lval);
-                }
+                /* Convert it to the target type */
+                TypeConversion (&Expr, Decl->Type);
 
-                /* Store the value into the variable */
-                g_putstatic (Flags | TypeOf (Decl->Type), SymData, 0);
+                /* Load the value into the primary */
+                LoadExpr (CF_NONE, &Expr);
 
+                /* Store the value into the variable */
+                g_putstatic (TypeOf (Decl->Type), SymData, 0);
             }
 
             /* Mark the variable as referenced */
@@ -445,7 +449,7 @@ 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) {
@@ -453,12 +457,15 @@ void DeclareLocals (void)
        /* 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, it is assume that we
-                * have reached the end of declarations.
+        * 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);
-               if ((Spec.Flags & DS_DEF_STORAGE) != 0 && (Spec.Flags & DS_DEF_TYPE) != 0) {
+               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;
        }
 
@@ -496,7 +503,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 ();
     }
 }