]> git.sur5r.net Git - cc65/blobdiff - src/cc65/function.c
Fixed a bug
[cc65] / src / cc65 / function.c
index 09ed488e7ac25d6736aa3e9056c96ead0a2d79d8..c4f30ca499bdac676ad0443ec17c5d802c7c108a 100644 (file)
@@ -6,9 +6,9 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2000-2002 Ullrich von Bassewitz                                       */
-/*               Wacholderweg 14                                             */
-/*               D-70597 Stuttgart                                           */
+/* (C) 2000-2003 Ullrich von Bassewitz                                       */
+/*               Römerstrasse 52                                             */
+/*               D-70794 Filderstadt                                         */
 /* EMail:        uz@cc65.org                                                 */
 /*                                                                           */
 /*                                                                           */
@@ -369,6 +369,22 @@ void NewFunc (SymEntry* Func)
     /* Allocate code and data segments for this function */
     Func->V.F.Seg = PushSegments (Func);
 
+    /* Special handling for main() */
+    if (strcmp (Func->Name, "main") == 0) {
+        /* Main cannot be a fastcall function */
+        if (IsFastCallFunc (Func->Type)) {
+            Error ("`main' cannot be declared as __fastcall__");
+        }
+
+        /* If main() takes parameters, generate a forced import to a function
+         * that will setup these parameters. This way, programs that do not
+         * need the additional code will not get it.
+         */
+        if (D->ParamCount > 0 || (D->Flags & FD_VARIADIC) != 0) {
+            g_importmainargs ();
+        }
+    }
+
     /* If this is a fastcall function, push the last parameter onto the stack */
     if (IsFastCallFunc (Func->Type) && D->ParamCount > 0) {
 
@@ -395,6 +411,9 @@ void NewFunc (SymEntry* Func)
        g_stackcheck ();
     }
 
+    /* Setup the stack */
+    oursp = 0;
+
     /* Walk through the parameter list and allocate register variable space
      * for parameters declared as register. Generate code to swap the contents
      * of the register bank with the save area on the stack.
@@ -426,9 +445,6 @@ void NewFunc (SymEntry* Func)
         Param = Param->NextSym;
     }
 
-    /* Setup the stack */
-    oursp = 0;
-
     /* Need a starting curly brace */
     ConsumeLCurly ();