* doc/sdccman.lyx, src/SDCCmain.c:
[fw/sdcc] / src / SDCCmain.c
index e6c867f804fde3906a10072f31901d5854ed04b5..f4c21bf1b66312eeceab11718d8c49d3b235b99f 100644 (file)
@@ -135,6 +135,7 @@ char buffer[PATH_MAX * 2];
 #define OPTION_PACK_IRAM        "--pack-iram"
 #define OPTION_NO_PACK_IRAM     "--no-pack-iram"
 #define OPTION_NO_PEEP_COMMENTS "--no-peep-comments"
+#define OPTION_VERBOSE_ASM      "--fverbose-asm"
 #define OPTION_OPT_CODE_SPEED   "--opt-code-speed"
 #define OPTION_OPT_CODE_SIZE    "--opt-code-size"
 #define OPTION_STD_C89          "--std-c89"
@@ -144,6 +145,7 @@ char buffer[PATH_MAX * 2];
 #define OPTION_CODE_SEG         "--codeseg"
 #define OPTION_CONST_SEG        "--constseg"
 #define OPTION_DOLLARS_IN_IDENT "--fdollars-in-identifiers"
+#define OPTION_UNSIGNED_CHAR    "--funsigned-char"
 
 static const OPTION
 optionsTable[] = {
@@ -179,6 +181,7 @@ optionsTable[] = {
     { 0,    OPTION_STD_C99,         NULL, "Use C99 standard only (incomplete)" },
     { 0,    OPTION_STD_SDCC99,      NULL, "Use C99 standard with SDCC extensions (incomplete)" },
     { 0,    OPTION_DOLLARS_IN_IDENT, &options.dollars_in_ident, "Permit '$' as an identifier character" },
+    { 0,    OPTION_UNSIGNED_CHAR,   &options.unsigned_char, "Make \"char\" unsigned by default" },
 
     { 0,    NULL,                   NULL, "Code generation options"},
     { 'm',  NULL,                   NULL, "Set the port to use e.g. -mz80." },
@@ -217,6 +220,7 @@ optionsTable[] = {
     { 0,    OPTION_NO_XINIT_OPT,    &options.noXinitOpt, "don't memcpy initialized xram from code"},
     { 0,    OPTION_NO_CCODE_IN_ASM, &options.noCcodeInAsm, "don't include c-code as comments in the asm file"},
     { 0,    OPTION_NO_PEEP_COMMENTS, &options.noPeepComments, "don't include peephole optimizer comments"},
+    { 0,    OPTION_VERBOSE_ASM,     &options.verboseAsm, "include code generator comments"},
 #if !OPT_DISABLE_Z80 || !OPT_DISABLE_GBZ80
     { 0,    "--no-std-crt0", &options.no_std_crt0, "For the z80/gbz80 do not link default crt0.o"},
 #endif
@@ -581,8 +585,8 @@ setDefaultOptions (void)
   options.shortis8bits = 0;
   options.std_sdcc = 1;           /* enable SDCC language extensions */
   options.std_c99 = 0;            /* default to C89 until more C99 support */
-  options.code_seg = CODE_NAME;   /* default to CSEG for generated code */
-  options.const_seg = CONST_NAME; /* default to CONST for generated code */
+  options.code_seg = CODE_NAME ? Safe_strdup(CODE_NAME) : NULL; /* default to CSEG for generated code */
+  options.const_seg = CONST_NAME ? Safe_strdup(CONST_NAME) : NULL; /* default to CONST for generated code */
 
   options.stack10bit=0;
 
@@ -628,9 +632,9 @@ processFile (char *s)
 
   /* otherwise depending on the file type */
   extp = dbuf_c_str (&ext);
-  if (extp[1] == '\0' && (extp[0] == 'c' || extp[0] == 'C'))
+  if (STRCASECMP (extp, ".c") == 0)
     {
-      unsigned char *p;
+      char *p;
 
       dbuf_destroy (&ext);
 
@@ -667,15 +671,14 @@ processFile (char *s)
       moduleName = dbuf_detach (&ext);
 
       for (p = moduleName; *p; ++p)
-        if (!isalnum(*p))
+        if (!isalnum ((unsigned char)*p))
           *p = '_';
       return;
     }
 
   /* if the extention is type .rel or .r or .REL or .R
      additional object file will be passed to the linker */
-  if ((extp[1] == '\0' && (extp[0] == 'r' || extp[0] == 'R')) ||
-      strcmp (extp, "rel") == 0 || strcmp (extp, "REL") == 0 ||
+  if (STRCASECMP (extp, ".r") == 0 || STRCASECMP (extp, ".rel") == 0 ||
       strcmp (extp, port->linker.rel_ext) == 0)
     {
       dbuf_destroy (&ext);
@@ -686,7 +689,7 @@ processFile (char *s)
     }
 
   /* if .lib or .LIB */
-  if (strcmp (extp, "lib") == 0 || strcmp (extp, ".LIB") == 0)
+  if (STRCASECMP (extp, ".lib") == 0)
     {
       dbuf_destroy (&ext);
       dbuf_destroy (&path);
@@ -885,6 +888,12 @@ parseCmdLine (int argc, char **argv)
       if (i >= argc)
         break;
 
+      /* check port specific options before general ones */
+      if (port->parseOption (&argc, argv, &i) == TRUE)
+        {
+          continue;
+        }
+
       if (tryHandleUnsupportedOpt(argv, &i) == TRUE)
         {
           continue;
@@ -1154,6 +1163,7 @@ parseCmdLine (int argc, char **argv)
 
               dbuf_init (&segname, 16);
               dbuf_printf (&segname, "%-8s(CODE)", getStringArg (OPTION_CODE_SEG, argv, &i, argc));
+              if (options.code_seg) Safe_free(options.code_seg);
               options.code_seg = dbuf_detach (&segname);
               continue;
             }
@@ -1164,19 +1174,13 @@ parseCmdLine (int argc, char **argv)
 
               dbuf_init (&segname, 16);
               dbuf_printf (&segname, "%-8s(CODE)", getStringArg (OPTION_CONST_SEG, argv, &i, argc));
+              if (options.const_seg) Safe_free(options.const_seg);
               options.const_seg = dbuf_detach (&segname);
               continue;
             }
 
-          if (!port->parseOption (&argc, argv, &i))
-            {
-              werror (W_UNKNOWN_OPTION, argv[i]);
-              continue;
-            }
-          else
-            {
-              continue;
-            }
+          werror (W_UNKNOWN_OPTION, argv[i]);
+          continue;
         }
 
       /* if preceded by  '-' then option */
@@ -1339,23 +1343,19 @@ parseCmdLine (int argc, char **argv)
               break;
 
             default:
-              if (!port->parseOption (&argc, argv, &i))
-                werror (W_UNKNOWN_OPTION, argv[i]);
+              werror (W_UNKNOWN_OPTION, argv[i]);
             }
           continue;
         }
 
-      if (!port->parseOption (&argc, argv, &i))
+      /* no option must be a filename */
+      if (options.c1mode)
         {
-          /* no option must be a filename */
-          if (options.c1mode)
-            {
-              werror (W_NO_FILE_ARG_IN_C1, argv[i]);
-            }
-          else
-            {
-              processFile (argv[i]);
-            }
+          werror (W_NO_FILE_ARG_IN_C1, argv[i]);
+        }
+      else
+        {
+          processFile (argv[i]);
         }
     }
 
@@ -1404,6 +1404,7 @@ parseCmdLine (int argc, char **argv)
 
           if (*dstPath != '\0')
             {
+              dbuf_init(&path, 128);
               dbuf_makePath (&path, dstPath, moduleNameBase);
               dbuf_c_str (&path);
               dstFileName = dbuf_detach (&path);
@@ -1418,6 +1419,14 @@ parseCmdLine (int argc, char **argv)
 
           dbuf_init(&file, 128);
 
+          /* get rid of the "."-extension */
+          dbuf_splitFile (s, &file, NULL);
+
+          dbuf_c_str (&file);
+          s = dbuf_detach (&file);
+
+          dbuf_init (&file, 128);
+
           dbuf_splitPath (s, NULL, &file);
 
           if (*dstPath != '\0')
@@ -1450,11 +1459,11 @@ parseCmdLine (int argc, char **argv)
   if (TARGET_IS_MCS51)
     {
       options.float_rent++;
-    }
 
-  /* set up external stack location if not explicitly specified */
-  if (!options.xstack_loc)
-    options.xstack_loc = options.xdata_loc;
+      /* set up external stack location if not explicitly specified */
+      if (!options.xstack_loc)
+        options.xstack_loc = options.xdata_loc;
+    }
 
   /* if debug option is set then open the cdbFile */
   if (options.debug && fullSrcFileName)
@@ -1471,6 +1480,17 @@ parseCmdLine (int argc, char **argv)
   return 0;
 }
 
+/*-----------------------------------------------------------------*/
+/* finalizeOptions - finalize (post-process( options               */
+/*-----------------------------------------------------------------*/
+static void
+finalizeOptions (void)
+{
+  /* no peephole comments if not verbose asm */
+  if (!options.verboseAsm)
+    options.noPeepComments = 1;
+}
+
 /*-----------------------------------------------------------------*/
 /* linkEdit : - calls the linkage editor  with options             */
 /*-----------------------------------------------------------------*/
@@ -1484,8 +1504,9 @@ linkEdit (char **envp)
   char linkerScriptFileName[PATH_MAX];
 
   linkerScriptFileName[0] = 0;
+  c = NULL;
 
-  if(port->linker.needLinkerScript)
+  if (port->linker.needLinkerScript)
     {
       char out_fmt;
 
@@ -1505,7 +1526,7 @@ linkEdit (char **envp)
         }
 
       /* first we need to create the <filename>.lnk file */
-      SNPRINTF (linkerScriptFileName, sizeof(scratchFileName),
+      SNPRINTF (linkerScriptFileName, sizeof(linkerScriptFileName),
         "%s.lnk", dstFileName);
       if (!(lnkfile = fopen (linkerScriptFileName, "w")))
         {
@@ -1548,10 +1569,13 @@ linkEdit (char **envp)
         }
 
 #define WRITE_SEG_LOC(N, L) \
-  segName = Safe_strdup(N); \
-  c = strtok(segName, " \t"); \
-  fprintf (lnkfile,"-b %s = 0x%04x\n", c, L); \
-  if (segName) { Safe_free(segName); }
+  if (N) \
+  { \
+    segName = Safe_strdup(N); \
+    c = strtok(segName, " \t"); \
+    fprintf (lnkfile,"-b %s = 0x%04x\n", c, L); \
+    if (segName) { Safe_free(segName); } \
+  }
 
       if (!(TARGET_Z80_LIKE)) /*Not for the z80, gbz80*/
         {
@@ -2019,7 +2043,7 @@ preProcess (char **envp)
 
       /* if using dollar signs in identifiers */
       if (options.dollars_in_ident)
-        addSet(&preArgvSet, Safe_strdup("--fdollars-in-identifiers"));
+        addSet(&preArgvSet, Safe_strdup("-fdollars-in-identifiers"));
 
       /* if using external stack define the macro */
       if (options.useXstack)
@@ -2077,8 +2101,8 @@ preProcess (char **envp)
 
       if (port && port->processor && TARGET_IS_PIC) {
         char proc[512];
-       SNPRINTF(&proc[0], 512, "-DSDCC_PROCESSOR=\"%s\"", port->processor);
-       addSet(&preArgvSet, Safe_strdup(proc));
+        SNPRINTF(&proc[0], 512, "-DSDCC_PROCESSOR=\"%s\"", port->processor);
+        addSet(&preArgvSet, Safe_strdup(proc));
       }
 
       /* standard include path */
@@ -2420,8 +2444,13 @@ main (int argc, char **argv, char **envp)
      And the z80 port needs port->finaliseOptions(),
      even if we're only linking. */
   initMem ();
+
+  /* finalize target specific options */
   port->finaliseOptions ();
 
+  /* finalize common options */
+  finalizeOptions ();
+
   if (fullSrcFileName || options.c1mode)
     {
       preProcess (envp);