]> git.sur5r.net Git - cc65/blobdiff - src/cc65/main.c
New option and #pragma --local-strings that causes string literals to be
[cc65] / src / cc65 / main.c
index 787dec0a49f6c4333c1350f53b37c1f46129a80c..ed3c987ab5309417a0bd0a750129406839825654 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 2000-2004 Ullrich von Bassewitz                                       */
-/*               Römerstraße 52                                              */
-/*               D-70794 Filderstadt                                         */
-/* EMail:        uz@cc65.org                                                 */
+/* (C) 2000-2009, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
@@ -48,6 +48,7 @@
 #include "mmodel.h"
 #include "print.h"
 #include "segnames.h"
+#include "strbuf.h"
 #include "target.h"
 #include "tgttrans.h"
 #include "version.h"
 #include "incpath.h"
 #include "input.h"
 #include "macrotab.h"
+#include "output.h"
 #include "scanner.h"
-#include "standard.h"
 #include "segments.h"
+#include "standard.h"
+#include "svnversion.h"
 
 
 
 static void Usage (void)
 /* Print usage information to stderr */
 {
-    fprintf (stderr,
-            "Usage: %s [options] file\n"
-            "Short options:\n"
-                    "  -Cl\t\t\tMake local variables static\n"
-                    "  -Dsym[=defn]\t\tDefine a symbol\n"
-             "  -E\t\t\tStop after the preprocessing stage\n"
-                    "  -I dir\t\tSet an include directory search path\n"
-                    "  -O\t\t\tOptimize code\n"
-                    "  -Oi\t\t\tOptimize code, inline more code\n"
-                    "  -Or\t\t\tEnable register variables\n"
-                    "  -Os\t\t\tInline some known functions\n"
-                    "  -T\t\t\tInclude source as comment\n"
-                    "  -V\t\t\tPrint the compiler version number\n"
-                    "  -W\t\t\tSuppress warnings\n"
-                    "  -d\t\t\tDebug mode\n"
-                    "  -g\t\t\tAdd debug info to object file\n"
-                    "  -h\t\t\tHelp (this text)\n"
-                    "  -j\t\t\tDefault characters are signed\n"
-             "  -mm model\t\tSet the memory model\n"
-                    "  -o name\t\tName the output file\n"
-             "  -r\t\t\tEnable register variables\n"
-                    "  -t sys\t\tSet the target system\n"
-                    "  -v\t\t\tIncrease verbosity\n"
-            "\n"
-            "Long options:\n"
-                    "  --add-source\t\tInclude source as comment\n"
-            "  --bss-name seg\tSet the name of the BSS segment\n"
-                    "  --check-stack\t\tGenerate stack overflow checks\n"
-                    "  --code-name seg\tSet the name of the CODE segment\n"
-            "  --codesize x\t\tAccept larger code by factor x\n"
-                    "  --cpu type\t\tSet cpu type (6502, 65c02)\n"
-            "  --create-dep\t\tCreate a make dependency file\n"
-                    "  --data-name seg\tSet the name of the DATA segment\n"
-                    "  --debug\t\tDebug mode\n"
-                    "  --debug-info\t\tAdd debug info to object file\n"
-            "  --debug-opt name\tDebug optimization steps\n"
-            "  --disable-opt name\tDisable an optimization step\n"
-                    "  --enable-opt name\tEnable an optimization step\n"
-                    "  --forget-inc-paths\tForget include search paths\n"
-            "  --help\t\tHelp (this text)\n"
-                    "  --include-dir dir\tSet an include directory search path\n"
-            "  --list-opt-steps\tList all optimizer steps and exit\n"
-             "  --memory-model model\tSet the memory model\n"
-             "  --register-space b\tSet space available for register variables\n"
-             "  --register-vars\tEnable register variables\n"
-                    "  --rodata-name seg\tSet the name of the RODATA segment\n"
-                    "  --signed-chars\tDefault characters are signed\n"
-             "  --standard std\tLanguage standard (c89, c99, cc65)\n"
-                    "  --static-locals\tMake local variables static\n"
-                    "  --target sys\t\tSet the target system\n"
-                    "  --verbose\t\tIncrease verbosity\n"
-                    "  --version\t\tPrint the compiler version number\n"
-             "  --writable-strings\tMake string literals writable\n",
-            ProgName);
+    printf ("Usage: %s [options] file\n"
+            "Short options:\n"
+            "  -Cl\t\t\tMake local variables static\n"
+            "  -Dsym[=defn]\t\tDefine a symbol\n"
+            "  -E\t\t\tStop after the preprocessing stage\n"
+            "  -I dir\t\tSet an include directory search path\n"
+            "  -O\t\t\tOptimize code\n"
+            "  -Oi\t\t\tOptimize code, inline more code\n"
+            "  -Or\t\t\tEnable register variables\n"
+            "  -Os\t\t\tInline some known functions\n"
+            "  -T\t\t\tInclude source as comment\n"
+            "  -V\t\t\tPrint the compiler version number\n"
+            "  -W\t\t\tSuppress warnings\n"
+            "  -d\t\t\tDebug mode\n"
+            "  -g\t\t\tAdd debug info to object file\n"
+            "  -h\t\t\tHelp (this text)\n"
+            "  -j\t\t\tDefault characters are signed\n"
+            "  -mm model\t\tSet the memory model\n"
+            "  -o name\t\tName the output file\n"
+            "  -r\t\t\tEnable register variables\n"
+            "  -t sys\t\tSet the target system\n"
+            "  -v\t\t\tIncrease verbosity\n"
+            "\n"
+            "Long options:\n"
+            "  --add-source\t\tInclude source as comment\n"
+            "  --bss-name seg\tSet the name of the BSS segment\n"
+            "  --check-stack\t\tGenerate stack overflow checks\n"
+            "  --code-name seg\tSet the name of the CODE segment\n"
+            "  --codesize x\t\tAccept larger code by factor x\n"
+            "  --cpu type\t\tSet cpu type (6502, 65c02)\n"
+            "  --create-dep\t\tCreate a make dependency file\n"
+            "  --data-name seg\tSet the name of the DATA segment\n"
+            "  --debug\t\tDebug mode\n"
+            "  --debug-info\t\tAdd debug info to object file\n"
+            "  --debug-opt name\tDebug optimization steps\n"
+            "  --disable-opt name\tDisable an optimization step\n"
+            "  --enable-opt name\tEnable an optimization step\n"
+            "  --forget-inc-paths\tForget include search paths\n"
+            "  --help\t\tHelp (this text)\n"
+            "  --include-dir dir\tSet an include directory search path\n"
+            "  --list-opt-steps\tList all optimizer steps and exit\n"
+            "  --local-strings\tEmit string literals immediately\n"
+            "  --memory-model model\tSet the memory model\n"       
+            "  --register-space b\tSet space available for register variables\n"
+            "  --register-vars\tEnable register variables\n"
+            "  --rodata-name seg\tSet the name of the RODATA segment\n"
+            "  --signed-chars\tDefault characters are signed\n"
+            "  --standard std\tLanguage standard (c89, c99, cc65)\n"
+            "  --static-locals\tMake local variables static\n"
+            "  --target sys\t\tSet the target system\n"
+            "  --verbose\t\tIncrease verbosity\n"
+            "  --version\t\tPrint the compiler version number\n"
+            "  --writable-strings\tMake string literals writable\n",
+            ProgName);
 }
 
 
@@ -176,10 +179,6 @@ static void SetSys (const char* Sys)
            cbmsys ("__C128__");
            break;
 
-       case TGT_ACE:
-           cbmsys ("__ACE__");
-           break;
-
        case TGT_PLUS4:
            cbmsys ("__PLUS4__");
            break;
@@ -229,7 +228,7 @@ static void SetSys (const char* Sys)
             DefineNumericMacro ("__SUPERVISION__", 1);
             break;
 
-        case TGT_SUPERVISION:
+        case TGT_LYNX:
             DefineNumericMacro ("__LYNX__", 1);
             break;
 
@@ -398,7 +397,7 @@ static void OptCPU (const char* Opt, const char* Arg)
     /* Find the CPU from the given name */
     CPU = FindCPU (Arg);
     if (CPU != CPU_6502 && CPU != CPU_6502X && CPU != CPU_65SC02 &&
-        CPU != CPU_65C02 && CPU != CPU_65816) {
+        CPU != CPU_65C02 && CPU != CPU_65816 && CPU != CPU_HUC6280) {
                AbEnd ("Invalid argument for %s: `%s'", Opt, Arg);
     }
 }
@@ -558,6 +557,15 @@ static void OptListOptSteps (const char* Opt attribute ((unused)),
 
 
 
+static void OptLocalStrings (const char* Opt attribute ((unused)),
+                            const char* Arg attribute ((unused)))
+/* Emit string literals immediately */
+{
+    IS_Set (&LocalStrings, 1);
+}
+
+
+
 static void OptMemoryModel (const char* Opt, const char* Arg)
 /* Set the memory model */
 {
@@ -666,12 +674,59 @@ static void OptVerbose (const char* Opt attribute ((unused)),
 
 
 static void OptVersion (const char* Opt attribute ((unused)),
-                       const char* Arg attribute ((unused)))
-/* Print the assembler version */
+                       const char* Arg attribute ((unused)))
+/* Print the compiler version */
 {
     fprintf (stderr,
-                    "cc65 V%u.%u.%u\n",
-                    VER_MAJOR, VER_MINOR, VER_PATCH);
+                    "cc65 V%s\nSVN version: %s\n",
+                    GetVersionAsString (), SVNVersion);
+    exit (EXIT_SUCCESS);
+}
+
+
+
+static void OptWarning (const char* Opt attribute ((unused)), const char* Arg)
+/* Handle the -W option */
+{
+    StrBuf W = AUTO_STRBUF_INITIALIZER;
+
+    /* Arg is a list of suboptions, separated by commas */
+    while (Arg) {
+
+        const char* Pos;
+        int         Enabled = 1;
+        IntStack*   S;
+
+        /* The suboption may be prefixed with '-' or '+' */
+        if (*Arg == '-') {
+            Enabled = 0;
+            ++Arg;
+        } else if (*Arg == '+') {
+            /* This is the default */
+            ++Arg;
+        }
+
+        /* Get the next suboption */
+        Pos = strchr (Arg, ',');
+        if (Pos) {
+            SB_CopyBuf (&W, Arg, Pos - Arg);
+            Arg = Pos + 1;
+        } else {
+            SB_CopyStr (&W, Arg);
+            Arg = 0;
+        }
+        SB_Terminate (&W);
+
+        /* Search for the warning */
+        S = FindWarning (SB_GetConstBuf (&W));
+        if (S == 0) {
+            InvArg (Opt, SB_GetConstBuf (&W));
+        }
+        IS_Set (S, Enabled);
+    }
+
+    /* Free allocated memory */
+    SB_Done (&W);
 }
 
 
@@ -706,6 +761,7 @@ int main (int argc, char* argv[])
        { "--help",             0,      OptHelp                 },
        { "--include-dir",      1,      OptIncludeDir           },
        { "--list-opt-steps",   0,      OptListOptSteps         },
+        { "--local-strings",    0,      OptLocalStrings         },
         { "--memory-model",     1,      OptMemoryModel          },
         { "--register-space",   1,      OptRegisterSpace        },
         { "--register-vars",    0,      OptRegisterVars         },
@@ -721,8 +777,7 @@ int main (int argc, char* argv[])
 
     unsigned I;
 
-    /* Initialize the output file name */
-    const char* OutputFile = 0;
+    /* Initialize the input file name */
     const char* InputFile  = 0;
 
     /* Initialize the cmdline module */
@@ -730,7 +785,7 @@ int main (int argc, char* argv[])
 
     /* Initialize the default segment names */
     InitSegNames ();
-                                       
+
     /* Initialize the include search paths */
     InitIncludePaths ();
 
@@ -744,9 +799,9 @@ int main (int argc, char* argv[])
                const char* Arg = ArgVec[I];
 
                /* Check for an option */
-               if (Arg [0] == '-') {
+               if (Arg[0] == '-') {
 
-                   switch (Arg [1]) {
+                   switch (Arg[1]) {
 
                case '-':
                    LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
@@ -770,7 +825,7 @@ int main (int argc, char* argv[])
                    break;
 
                case 'o':
-                   OutputFile = GetArg (&I, 2);
+                   SetOutputName (GetArg (&I, 2));
                    break;
 
                 case 'r':
@@ -799,7 +854,7 @@ int main (int argc, char* argv[])
                            default:
                                UnknownOption (Arg);
                                break;
-                       }
+                       }
                    }
                    break;
 
@@ -842,7 +897,7 @@ int main (int argc, char* argv[])
                            break;
 
                        case 'W':
-                           IS_Set (&WarnDisable, 1);
+                    OptWarning (Arg, GetArg (&I, 2));
                            break;
 
                        default:
@@ -867,9 +922,7 @@ int main (int argc, char* argv[])
     }
 
     /* Create the output file name if it was not explicitly given */
-    if (OutputFile == 0) {
-       OutputFile = MakeFilename (InputFile, ".s");
-    }
+    MakeDefaultOutputName (InputFile);
 
     /* If no CPU given, use the default CPU for the target */
     if (CPU == CPU_UNKNOWN) {
@@ -894,29 +947,24 @@ int main (int argc, char* argv[])
     Compile (InputFile);
 
     /* Create the output file if we didn't had any errors */
-    if (ErrorCount == 0 || Debug) {
+    if (PreprocessOnly == 0 && (ErrorCount == 0 || Debug)) {
 
-       /* Open the file */
-       FILE* F = fopen (OutputFile, "w");
-       if (F == 0) {
-           Fatal ("Cannot open output file `%s': %s", OutputFile, strerror (errno));
-       }
-       Print (stdout, 1, "Opened output file `%s'\n", OutputFile);
+        /* Emit literals, externals, do cleanup and optimizations */
+        FinishCompile ();
 
-       /* Write the output to the file */
-       WriteOutput (F);
-               Print (stdout, 1, "Wrote output to `%s'\n", OutputFile);
+       /* Open the file */
+        OpenOutputFile ();
+
+       /* Write the output to the file */
+               WriteAsmOutput ();
+               Print (stdout, 1, "Wrote output to `%s'\n", OutputFilename);
 
        /* Close the file, check for errors */
-       if (fclose (F) != 0) {
-           remove (OutputFile);
-           Fatal ("Cannot write to output file (disk full?)");
-       }
-               Print (stdout, 1, "Closed output file `%s'\n", OutputFile);
+        CloseOutputFile ();
 
        /* Create dependencies if requested */
        if (CreateDep) {
-           DoCreateDep (OutputFile);
+           DoCreateDep (OutputFilename);
            Print (stdout, 1, "Creating dependeny file\n");
        }