From: cuz Date: Fri, 7 Mar 2003 12:41:47 +0000 (+0000) Subject: If main() takes arguments, generate a forced import to a special module X-Git-Tag: V2.12.0~1681 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=065b8f25965f6d9175a5e4c8fbd28c2fd9638c16;p=cc65 If main() takes arguments, generate a forced import to a special module constructor which will setup command line arguments. git-svn-id: svn://svn.cc65.org/cc65/trunk@2011 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- diff --git a/src/cc65/codegen.c b/src/cc65/codegen.c index f3a556fb2..d3ec179c3 100644 --- a/src/cc65/codegen.c +++ b/src/cc65/codegen.c @@ -157,16 +157,16 @@ void g_preamble (void) /* Identify the compiler version */ AddTextLine (";"); AddTextLine ("; File generated by cc65 v %u.%u.%u", - VER_MAJOR, VER_MINOR, VER_PATCH); + VER_MAJOR, VER_MINOR, VER_PATCH); AddTextLine (";"); /* Insert some object file options */ AddTextLine ("\t.fopt\t\tcompiler,\"cc65 v %u.%u.%u\"", - VER_MAJOR, VER_MINOR, VER_PATCH); + VER_MAJOR, VER_MINOR, VER_PATCH); /* If we're producing code for some other CPU, switch the command set */ if (CPU == CPU_65C02) { - AddTextLine ("\t.pc02"); + AddTextLine ("\t.pc02"); } /* Allow auto import for runtime library routines */ @@ -189,7 +189,7 @@ void g_preamble (void) void g_fileinfo (const char* Name, unsigned long Size, unsigned long MTime) /* If debug info is enabled, place a file info into the source */ -{ +{ if (DebugInfo) { /* We have to place this into the global text segment, so it will * appear before all .dbg line statements. @@ -387,6 +387,14 @@ void g_defimport (const char* Name, int ZP) +void g_importmainargs (void) +/* Forced import of a special symbol that handles arguments to main */ +{ + AddTextLine ("\t.forceimport\tinitmainargs"); +} + + + /*****************************************************************************/ /* Load functions for various registers */ /*****************************************************************************/ diff --git a/src/cc65/codegen.h b/src/cc65/codegen.h index ff9b18306..15a6872bc 100644 --- a/src/cc65/codegen.h +++ b/src/cc65/codegen.h @@ -155,6 +155,9 @@ void g_defexport (const char* Name, int ZP); void g_defimport (const char* Name, int ZP); /* Import the given label */ +void g_importmainargs (void); +/* Forced import of a special symbol that handles arguments to main */ + /*****************************************************************************/ diff --git a/src/cc65/function.c b/src/cc65/function.c index e4dbe83d8..c4f30ca49 100644 --- a/src/cc65/function.c +++ b/src/cc65/function.c @@ -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) {