]> git.sur5r.net Git - cc65/blobdiff - src/cc65/compile.c
Fixed a bug
[cc65] / src / cc65 / compile.c
index 19a5446e3d445efa8a8d507f2fcd03382d452dd2..7edd3ae1786df2d5ca7212a4329af3af498adfab 100644 (file)
@@ -6,9 +6,9 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2000-2001 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 <stdlib.h>
+#include <time.h>
 
 /* common */
+#include "debugflag.h"
 #include "version.h"
+#include "xmalloc.h"
+#include "xsprintf.h"
 
 /* cc65 */
 #include "asmlabel.h"
+#include "asmstmt.h"
 #include "codegen.h"
 #include "declare.h"
 #include "error.h"
 #include "expr.h"
 #include "function.h"
 #include "global.h"
-#include "incpath.h"
 #include "input.h"
 #include "litpool.h"
 #include "macrotab.h"
@@ -75,9 +79,9 @@ static void Parse (void)
     /* Parse until end of input */
     while (CurTok.Tok != TOK_CEOF) {
 
-       DeclSpec        Spec;
+       DeclSpec        Spec;
        Declaration     Decl;
-       int             NeedStorage;
+       int             NeedStorage;
 
        /* Check for empty statements */
        if (CurTok.Tok == TOK_SEMI) {
@@ -87,7 +91,7 @@ static void Parse (void)
 
        /* Check for an ASM statement (which is allowed also on global level) */
        if (CurTok.Tok == TOK_ASM) {
-           doasm ();
+           AsmStatement ();
            ConsumeSemi ();
            continue;
        }
@@ -102,10 +106,13 @@ static void Parse (void)
        ParseDeclSpec (&Spec, SC_EXTERN | SC_STATIC, T_INT);
 
        /* Don't accept illegal storage classes */
-       if (Spec.StorageClass == SC_AUTO || Spec.StorageClass == SC_REGISTER) {
-           Error ("Illegal storage class");
-           Spec.StorageClass = SC_EXTERN | SC_STATIC;
-       }
+        if ((Spec.StorageClass & SC_TYPE) == 0) {
+            if ((Spec.StorageClass & SC_AUTO) != 0 ||
+                (Spec.StorageClass & SC_REGISTER) != 0) {
+                Error ("Illegal storage class");
+                Spec.StorageClass = SC_EXTERN | SC_STATIC;
+            }
+        }
 
        /* Check if this is only a type declaration */
        if (CurTok.Tok == TOK_SEMI) {
@@ -167,7 +174,7 @@ static void Parse (void)
                            if (Size == 0) {
                        if (!IsTypeVoid (Decl.Type)) {
                            if (!IsTypeArray (Decl.Type)) {
-                               /* Size is unknown and not an array */
+                               /* Size is unknown and not an array */
                                Error ("Variable `%s' has unknown size", Decl.Ident);
                            }
                        } else if (ANSI) {
@@ -196,19 +203,27 @@ static void Parse (void)
                    if (IsTypeVoid (Decl.Type)) {
                        /* We cannot declare variables of type void */
                        Error ("Illegal type for variable `%s'", Decl.Ident);
-                   } else if (Size == 0) {
-                       /* Size is unknown */
-                       Error ("Variable `%s' has unknown size", Decl.Ident);
+                        Entry->Flags &= ~(SC_STORAGE | SC_DEF);
+                    } else if (Size == 0) {
+                       /* Size is unknown. Is it an array? */
+                        if (!IsTypeArray (Decl.Type)) {
+                            Error ("Variable `%s' has unknown size", Decl.Ident);
+                        }
+                        Entry->Flags &= ~(SC_STORAGE | SC_DEF);
                    }
 
-                   /* Switch to the BSS segment */
-                   g_usebss ();
+                    /* Allocate storage if it is still needed */
+                    if (Entry->Flags & SC_STORAGE) {
 
-                   /* Define a label */
-                   g_defgloblabel (Entry->Name);
+                        /* Switch to the BSS segment */
+                        g_usebss ();
 
-                   /* Allocate space for uninitialized variable */
-                   g_res (SizeOf (Entry->Type));
+                        /* Define a label */
+                        g_defgloblabel (Entry->Name);
+
+                        /* Allocate space for uninitialized variable */
+                        g_res (Size);
+                    }
                }
 
            }
@@ -227,16 +242,16 @@ static void Parse (void)
 
            /* Function */
            if (!comma) {
-
                if (CurTok.Tok == TOK_SEMI) {
-
                    /* Prototype only */
                    NextToken ();
-
-               } else {
-                   if (Entry) {
-                       NewFunc (Entry);
-                   }
+               } else if (Entry) {
+                    /* Function body definition */
+                    if (SymIsDef (Entry)) {
+                        Error ("Body for function `%s' has already been defined",
+                               Entry->Name);
+                    }
+                    NewFunc (Entry);
                }
            }
 
@@ -254,44 +269,54 @@ static void Parse (void)
 void Compile (const char* FileName)
 /* Top level compile routine. Will setup things and call the parser. */
 {
-    char* Path;
-
-
-    /* Add some standard paths to the include search path */
-    AddIncludePath ("", INC_USER);             /* Current directory */
-    AddIncludePath ("include", INC_SYS);
-#ifdef CC65_INC
-    AddIncludePath (CC65_INC, INC_SYS);
-#else
-    AddIncludePath ("/usr/lib/cc65/include", INC_SYS);
-#endif
-    Path = getenv ("CC65_INC");
-    if (Path) {
-       AddIncludePath (Path, INC_SYS | INC_USER);
-    }
+    char        Buf[20];
+    char        DateStr[20];
+    char        TimeStr[20];
+    time_t      Time;
+    struct tm*  TM;
+
+    /* Since strftime is locale dependent, we need the abbreviated month names
+     * in english.
+     */
+    static const char MonthNames[12][4] = {
+        "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+        "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
+    };
 
     /* Add macros that are always defined */
-    DefineNumericMacro ("__CC65__", (VER_MAJOR * 0x100) + (VER_MINOR * 0x10) + VER_PATCH);
+    DefineNumericMacro ("__CC65__", VERSION);
 
     /* Strict ANSI macro */
     if (ANSI) {
        DefineNumericMacro ("__STRICT_ANSI__", 1);
     }
 
-    /* Optimization macros */
+    /* Optimization macros. Since no source code has been parsed for now, the
+     * IS_Get functions access the values in effect now, regardless of any
+     * changes using #pragma later.
+     */
     if (Optimize) {
        DefineNumericMacro ("__OPT__", 1);
        if (FavourSize == 0) {
            DefineNumericMacro ("__OPT_i__", 1);
        }
-       if (EnableRegVars) {
+       if (IS_Get (&EnableRegVars)) {
            DefineNumericMacro ("__OPT_r__", 1);
        }
-       if (InlineStdFuncs) {
+               if (IS_Get (&InlineStdFuncs)) {
            DefineNumericMacro ("__OPT_s__", 1);
        }
     }
 
+    /* __TIME__ and __DATE__ macros */
+    Time = time (0);
+    TM   = localtime (&Time);
+    strftime (Buf, sizeof (Buf), "%e %Y", TM);
+    xsprintf (DateStr, sizeof (DateStr), "\"%s %s\"", MonthNames[TM->tm_mon], Buf);
+    strftime (TimeStr, sizeof (TimeStr), "\"%H:%M:%S\"", TM);
+    DefineTextMacro ("__DATE__", DateStr);
+    DefineTextMacro ("__TIME__", TimeStr);
+
     /* Initialize the literal pool */
     InitLiteralPool ();
 
@@ -327,3 +352,4 @@ void Compile (const char* FileName)
 
 
 
+