* src/mcs51/gen.c (genCpl): quick fix for bug #974835
[fw/sdcc] / src / SDCCmain.c
index 3eeddd1a99274182cc0fec407ce52b460bf1e1bc..b37a9bf08403fabe6275e1a261f70abb7d05af9e 100644 (file)
@@ -58,8 +58,10 @@ char *fullDstFileName;               /* full name for the output file; */
 char *dstFileName;             /* destination file name without extension */
 char *dstPath = "";            /* path for the output files; */
                                /* "" is equivalent with cwd */
-char *moduleName;              /* module name is source file without path and extension */
+char *moduleNameBase;          /* module name base is source file without path and extension */
                                 /* can be NULL while linking without compiling */
+char *moduleName;              /* module name is same as module name base, but with all */
+                               /* non-alphanumeric characters replaced with underscore */
 int currRegBank = 0;
 int RegBankUsed[4] = {1, 0, 0, 0}; /*JCF: Reg Bank 0 used by default*/
 struct optimize optimize;
@@ -124,6 +126,9 @@ char buffer[PATH_MAX * 2];
 #define OPTION_PRINT_SEARCH_DIRS "--print-search-dirs"
 #define OPTION_MSVC_ERROR_STYLE "--vc"
 #define OPTION_USE_STDOUT       "--use-stdout"
+#define OPTION_STACK_SIZE      "--stack-size"
+#define OPTION_PACK_IRAM       "--pack-iram"
+#define OPTION_NO_PEEP_COMMENTS        "--no-peep-comments"
 
 static const OPTION
 optionsTable[] = {
@@ -218,10 +223,13 @@ optionsTable[] = {
 #endif
 #if !OPT_DISABLE_DS390 || !OPT_DISABLE_MCS51
     { 0,    "--parms-in-bank1",            &options.parms_in_bank1,"MCS51/DS390 - use Bank1 for parameter passing"},
+    { 0,    OPTION_STACK_SIZE,     NULL,"MCS51/DS390 - Tells the linker to allocate this space for stack"},
+    { 0,    OPTION_PACK_IRAM,      &options.pack_iram,"MCS51/DS390 - Tells the linker to pack variables in internal ram"},
 #endif
     { 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_ICODE_IN_ASM,    &options.iCodeInAsm, "include i-code as comments in the asm file"},
+    { 0,    OPTION_NO_PEEP_COMMENTS, &options.noPeepComments, "don't include peephole optimizer comments"},
     { 0,    OPTION_PRINT_SEARCH_DIRS, &options.printSearchDirs, "display the directories in the compiler's search path"},
     { 0,    OPTION_MSVC_ERROR_STYLE, &options.vc_err_style, "messages are compatible with Micro$oft visual studio"},
     { 0,    OPTION_USE_STDOUT, &options.use_stdout, "send errors to stdout instead of stderr"},
@@ -600,7 +608,12 @@ processFile (char *s)
           fext--;
         }
 #endif
+      moduleNameBase = Safe_strdup ( fext );
       moduleName = Safe_strdup ( fext );
+      
+      for (fext = moduleName; *fext; fext++)
+        if (!isalnum (*fext))
+         *fext = '_';
       return;
     }
 
@@ -908,6 +921,12 @@ parseCmdLine (int argc, char **argv)
                 options.stack_loc = getIntArg(OPTION_STACK_LOC, argv, &i, argc);
                 continue;
            }
+         
+      if (strcmp (argv[i], OPTION_STACK_SIZE) == 0)
+           {
+                options.stack_size = getIntArg(OPTION_STACK_SIZE, argv, &i, argc);
+                continue;
+           }
 
          if (strcmp (argv[i], OPTION_XRAM_LOC) == 0)
            {
@@ -998,6 +1017,7 @@ parseCmdLine (int argc, char **argv)
          if (!port->parseOption (&argc, argv, &i))
            {
              werror (W_UNKNOWN_OPTION, argv[i]);
+             continue;
            }
          else
            {
@@ -1223,11 +1243,11 @@ parseCmdLine (int argc, char **argv)
       /* use the modulename from the C-source */
       if (fullSrcFileName)
         {
-         size_t bufSize = strlen (dstPath) + strlen (moduleName) + 1;
+         size_t bufSize = strlen (dstPath) + strlen (moduleNameBase) + 1;
 
          dstFileName = Safe_alloc (bufSize);
           strncpyz (dstFileName, dstPath, bufSize);
-          strncatz (dstFileName, moduleName, bufSize);
+          strncatz (dstFileName, moduleNameBase, bufSize);
         }
       /* use the modulename from the first object file */
       else if ((s = peekSet(relFilesSet)) != NULL)
@@ -1290,6 +1310,23 @@ linkEdit (char **envp)
 
   if(port->linker.needLinkerScript)
     {
+      char out_fmt;
+      
+      switch (options.out_fmt)
+        {
+       case 0:
+         out_fmt = 'i';        /* Intel hex */
+         break;
+       case 1:
+         out_fmt = 's';        /* Motorola S19 */
+         break;
+       case 2:
+         out_fmt = 't';        /* Elf */
+         break;
+       default:
+         out_fmt = 'i';
+       }
+      
       /* first we need to create the <filename>.lnk file */
       SNPRINTF (scratchFileName, sizeof(scratchFileName),
         "%s.lnk", dstFileName);
@@ -1302,11 +1339,13 @@ linkEdit (char **envp)
       if (TARGET_IS_Z80 || TARGET_IS_GBZ80)
         {
           fprintf (lnkfile, "--\n-m\n-j\n-x\n-%c %s\n",
-            (options.out_fmt ? 's' : 'i'), dstFileName);
+            out_fmt, dstFileName);
         }
       else /*For all the other ports.  Including pics???*/
         {
-          fprintf (lnkfile, "-myux%c\n", (options.out_fmt ? 's' : 'i'));
+          fprintf (lnkfile, "-myux%c\n", out_fmt);
+          if(options.pack_iram)
+              fprintf (lnkfile, "-Y\n");
         }
 
       if (!(TARGET_IS_Z80 || TARGET_IS_GBZ80)) /*Not for the z80, gbz80*/
@@ -1315,6 +1354,10 @@ linkEdit (char **envp)
           if (options.iram_size)
             fprintf (lnkfile, "-a 0x%04x\n", options.iram_size);
 
+          /* if stack size specified*/
+          if(options.stack_size)
+              fprintf (lnkfile, "-A 0x%02x\n", options.stack_size);
+
           /* if xram size specified */
           if (options.xram_size_set)
             fprintf (lnkfile, "-v 0x%04x\n", options.xram_size);
@@ -1472,7 +1515,11 @@ linkEdit (char **envp)
             }
 #endif
 #endif
-          if (!(TARGET_IS_Z80 || TARGET_IS_GBZ80
+          if (TARGET_IS_MCS51)
+           {
+              fprintf (lnkfile, "-l mcs51\n");
+           }
+         if (!(TARGET_IS_Z80 || TARGET_IS_GBZ80
             || TARGET_IS_HC08)) /*Not for the z80, gbz80*/
             { /*Why the z80 port is not using the standard libraries?*/
               fprintf (lnkfile, "-l %s\n", STD_LIB);
@@ -1573,9 +1620,18 @@ linkEdit (char **envp)
   if (port->linker.cmd)
     {
       char buffer2[PATH_MAX];
+      set *libSet=NULL;
 
-      /* VR 030517 - gplink needs linker options to set the linker script,*/
-      buildCmdLine (buffer2, port->linker.cmd, dstFileName, scratchFileName, NULL, linkOptionsSet);
+      if(TARGET_IS_PIC16) {
+         /* use $3 to set the linker include directories */
+         libSet = appendStrSet(libDirsSet, "-I\"", "\"");
+
+         /* now add the libraries from command line */
+         mergeSets(&libSet, libFilesSet);
+      }
+      
+      buildCmdLine (buffer2, port->linker.cmd, dstFileName, scratchFileName, (libSet?joinStrSet(libSet):NULL), linkOptionsSet);
+      if(libSet)deleteSet(&libSet);
 
       buildCmdLine2 (buffer, sizeof(buffer), buffer2);
     }
@@ -1845,10 +1901,12 @@ setBinPaths(const char *argv0)
     addSetHead(&binPathSet, Safe_strdup(buf));
   }
 
+#if 0
   if (options.printSearchDirs) {
     printf("programs:\n");
     fputStrSet(stdout, binPathSet);
   }
+#endif
 }
 
 /* Set system include path */
@@ -1871,10 +1929,12 @@ setIncludePath(void)
   if ((p = getenv(SDCC_INCLUDE_NAME)) != NULL)
     addSetHead(&includeDirsSet, p);
 
+#if 0
   if (options.printSearchDirs) {
     printf("includedir:\n");
     fputStrSet(stdout, includeDirsSet);
   }
+#endif
 }
 
 /* Set system lib path */
@@ -1897,10 +1957,12 @@ setLibPath(void)
   if ((p = getenv(SDCC_LIB_NAME)) != NULL)
     addSetHead(&libDirsSet, p);
 
+#if 0
   if (options.printSearchDirs) {
     printf("libdir:\n");
     fputStrSet(stdout, libDirsSet);
   }
+#endif
 }
 
 /* Set data path */
@@ -1937,10 +1999,12 @@ setDataPaths(const char *argv0)
   addSet(&dataDirsSet, Safe_strdup(DATADIR));
 #endif
 
+#if 0
   if (options.printSearchDirs) {
     printf("datadir:\n");
     fputStrSet(stdout, dataDirsSet);
   }
+#endif
 
   setIncludePath();
   setLibPath();
@@ -1978,6 +2042,22 @@ initValues (void)
 
 }
 
+static void doPrintSearchDirs(void)
+{
+    printf("programs:\n");
+    fputStrSet(stdout, binPathSet);
+
+    printf("datadir:\n");
+    fputStrSet(stdout, dataDirsSet);
+
+    printf("includedir:\n");
+    fputStrSet(stdout, includeDirsSet);
+
+    printf("libdir:\n");
+    fputStrSet(stdout, libDirsSet);
+}
+
+
 static void
 sig_handler (int signal)
 {
@@ -2071,6 +2151,12 @@ main (int argc, char **argv, char **envp)
   setBinPaths(argv[0]);
   setDataPaths(argv[0]);
 
+  if(port->initPaths)
+       port->initPaths();
+  
+  if(options.printSearchDirs)
+       doPrintSearchDirs();
+
   /* if no input then printUsage & exit */
   if (!options.c1mode && !fullSrcFileName && peekSet(relFilesSet) == NULL) {
     if (!options.printSearchDirs)
@@ -2101,7 +2187,8 @@ main (int argc, char **argv, char **envp)
 
       yyparse ();
 
-      pclose(yyin);
+      if (pclose(yyin))
+        fatalError = 1;
       deleteSetItem(&pipeSet, yyin);
 
       if (fatalError) {