Cleaned up some bits and peaces
[fw/sdcc] / src / SDCCmain.c
index f575a84e1da3f8c76c1770a2c0499f32f6d325f1..c2aee60697ef8c9e94a3ec71f9044a43040fd8a2 100644 (file)
@@ -22,8 +22,6 @@
    what you give them.   Help stamp out software-hoarding!
 -------------------------------------------------------------------------*/
 
-#define USE_SYSTEM_SYSTEM_CALLS
-
 #include "common.h"
 #include <ctype.h>
 #include "newalloc.h"
@@ -84,6 +82,7 @@ char DefaultExePath[128];
 #define OPTION_SMALL_MODEL "-model-small"
 #define OPTION_FLAT24_MODEL "-model-flat24"
 #define OPTION_STACK_AUTO  "-stack-auto"
+#define OPTION_STACK_8BIT "-stack-8bit"
 #define OPTION_STACK_10BIT "-stack-10bit"
 #define OPTION_XSTACK      "-xstack"
 #define OPTION_GENERIC     "-generic"
@@ -180,7 +179,7 @@ extern void pic14glue ();
     @param    The name minus the option (eg 'mcs51')
     @return     0 on success.
 */
-static int 
+static int
 _setPort (const char *name)
 {
   int i;
@@ -197,7 +196,7 @@ _setPort (const char *name)
   exit (1);
 }
 
-static void 
+static void
 _validatePorts (void)
 {
   int i;
@@ -211,8 +210,7 @@ _validatePorts (void)
     }
 }
 
-#ifdef USE_SYSTEM_SYSTEM_CALLS
-void 
+void
 buildCmdLine (char *into, const char **cmds,
              const char *p1, const char *p2,
              const char *p3, const char **list)
@@ -272,78 +270,11 @@ buildCmdLine (char *into, const char **cmds,
       strcat (into, " ");
     }
 }
-#else
-void 
-buildCmdLine (char *into, char **args, const char **cmds,
-             const char *p1, const char *p2,
-             const char *p3, const char **list)
-{
-  const char *p, *from;
-
-  while (*cmds)
-    {
-      *args = into;
-      args++;
-
-      from = *cmds;
-      cmds++;
-      *into = '\0';
-
-      /* See if it has a '$' anywhere - if not, just copy */
-      if ((p = strchr (from, '$')))
-       {
-         strncpy (into, from, p - from);
-         /* NULL terminate it */
-         into[p - from] = '\0';
-         from = p + 2;
-         p++;
-         switch (*p)
-           {
-           case '1':
-             if (p1)
-               strcat (into, p1);
-             break;
-           case '2':
-             if (p2)
-               strcat (into, p2);
-             break;
-           case '3':
-             if (p3)
-               strcat (into, p3);
-             break;
-           case 'l':
-             {
-               const char **tmp = list;
-               if (tmp)
-                 {
-                   while (*tmp)
-                     {
-                       strcpy (into, *tmp);
-                       into += strlen (into) + 1;
-                       *args = into;
-                       args++;
-                       tmp++;
-                     }
-                 }
-               break;
-             }
-           default:
-             assert (0);
-           }
-       }
-      strcat (into, from);
-      if (strlen (into) == 0)
-       args--;
-      into += strlen (into) + 1;
-    }
-  *args = NULL;
-}
-#endif
 
 /*-----------------------------------------------------------------*/
 /* printVersionInfo - prints the version info        */
 /*-----------------------------------------------------------------*/
-void 
+void
 printVersionInfo ()
 {
   int i;
@@ -362,9 +293,13 @@ printVersionInfo ()
 #else
 #ifdef __DJGPP__
           " (DJGPP) \n"
+#else
+#if defined(_MSC_VER)
+          " (WIN32) \n"
 #else
           " (UNIX) \n"
 #endif
+#endif
 #endif
 
           ,VersionString
@@ -374,7 +309,7 @@ printVersionInfo ()
 /*-----------------------------------------------------------------*/
 /* printUsage - prints command line syntax         */
 /*-----------------------------------------------------------------*/
-void 
+void
 printUsage ()
 {
   printVersionInfo ();
@@ -408,7 +343,7 @@ printUsage ()
 /*-----------------------------------------------------------------*/
 /* parseWithComma - separates string with comma                    */
 /*-----------------------------------------------------------------*/
-void 
+void
 parseWithComma (char **dest, char *src)
 {
   int i = 0;
@@ -435,7 +370,7 @@ parseWithComma (char **dest, char *src)
 /*-----------------------------------------------------------------*/
 /* setDefaultOptions - sets the default options                    */
 /*-----------------------------------------------------------------*/
-static void 
+static void
 setDefaultOptions ()
 {
   int i;
@@ -459,6 +394,8 @@ setDefaultOptions ()
   options.nostdinc = 0;
   options.verbose = 0;
 
+  options.stack10bit=0;
+
   /* now for the optimizations */
   /* turn on the everything */
   optimize.global_cse = 1;
@@ -469,13 +406,14 @@ setDefaultOptions ()
   optimize.loopInvariant = 1;
   optimize.loopInduction = 1;
 
+  /* now for the ports */
   port->setDefaultOptions ();
 }
 
 /*-----------------------------------------------------------------*/
 /* processFile - determines the type of file from the extension    */
 /*-----------------------------------------------------------------*/
-static void 
+static void
 processFile (char *s)
 {
   char *fext = NULL;
@@ -556,7 +494,7 @@ processFile (char *s)
 
 }
 
-static void 
+static void
 _processC1Arg (char *s)
 {
   if (srcFileName)
@@ -574,7 +512,7 @@ _processC1Arg (char *s)
     }
 }
 
-static void 
+static void
 _addToList (const char **list, const char *str)
 {
   /* This is the bad way to do things :) */
@@ -589,7 +527,7 @@ _addToList (const char **list, const char *str)
   *(++list) = NULL;
 }
 
-static void 
+static void
 _setModel (int model, const char *sz)
 {
   if (port->general.supported_models & model)
@@ -601,7 +539,7 @@ _setModel (int model, const char *sz)
 /*-----------------------------------------------------------------*/
 /* parseCmdLine - parses the command line and sets the options     */
 /*-----------------------------------------------------------------*/
-int 
+int
 parseCmdLine (int argc, char **argv)
 {
   int i;
@@ -659,6 +597,12 @@ parseCmdLine (int argc, char **argv)
              continue;
            }
 
+         if (strcmp (&argv[i][1], OPTION_STACK_8BIT) == 0)
+           {
+             options.stack10bit = 0;
+             continue;
+           }
+
          if (strcmp (&argv[i][1], OPTION_STACK_AUTO) == 0)
            {
              options.stackAuto = 1;
@@ -1260,21 +1204,15 @@ parseCmdLine (int argc, char **argv)
 /* my_system - will call a program with arguments                  */
 /*-----------------------------------------------------------------*/
 
-#if defined(_MSC_VER)
 
-char *try_dir[] =
-{DefaultExePath, NULL};                // TODO : Fill in some default search list
 
-#else
 
 //char *try_dir[]= {SRCDIR "/bin",PREFIX "/bin", NULL};
 char *try_dir[] =
-{NULL, NULL}; /* First entry may be overwritten, so use two. */
+{NULL, NULL};                  /* First entry may be overwritten, so use two. */
 
-#endif
 
-#ifdef USE_SYSTEM_SYSTEM_CALLS
-int 
+int
 my_system (const char *cmd)
 {
   int argsStart, e, i = 0;
@@ -1285,26 +1223,16 @@ my_system (const char *cmd)
   // try to find the command in predefined path's
   while (try_dir[i])
     {
-      cmdLine = (char *) malloc (strlen (try_dir[i]) + strlen (cmd) + 10);
+      cmdLine = (char *) Safe_malloc (strlen (try_dir[i]) + strlen (cmd) + 10);
       strcpy (cmdLine, try_dir[i]);    // the path
 
-      strcat (cmdLine, "/");
+      strcat (cmdLine, DIR_SEPARATOR_STRING);
       strncat (cmdLine, cmd, argsStart);       // the command
+
 #if NATIVE_WIN32
       strcat (cmdLine, ".exe");
-      /* Mung slashes into backslashes to keep WIndoze happy. */
-      {
-       char *r = cmdLine;
-       while (*r)
-         {
-           if (*r == '/')
-             {
-               *r = '\\';
-             }
-           r++;
-         }
-      }
 #endif
+
       if (access (cmdLine, X_OK) == 0)
        {
          // the arguments
@@ -1335,87 +1263,14 @@ my_system (const char *cmd)
   return e;
 }
 
-#else
-
-int 
-my_system (const char *cmd, char **cmd_argv)
-{
-  char *dir, *got = NULL;
-  int i = 0;
-
-  while (!got && try_dir[i])
-    {
-      dir = (char *) Safe_malloc (strlen (try_dir[i]) + strlen (cmd) + 10);
-      strcpy (dir, try_dir[i]);
-      strcat (dir, "/");
-      strcat (dir, cmd);
-
-#if NATIVE_WIN32
-      strcat (dir, ".exe");
-
-      /* Mung slashes into backslashes to keep WIndoze happy. */
-      {
-       char *r;
-       r = dir;
-
-       while (*r)
-         {
-           if (*r == '/')
-             {
-               *r = '\\';
-             }
-           r++;
-         }
-      }
-#endif
-
-      if (access (dir, X_OK) == 0)
-       {
-         got = strdup (dir);
-       }
-      free (dir);
-      i++;
-    }
-
-  if (verboseExec)
-    {
-      char **pCmd = cmd_argv;
-      printf ("+ ");
-      while (*pCmd)
-       {
-         printf ("%s ", *pCmd);
-         pCmd++;
-       }
-      printf ("\n");
-    }
-
-  if (got)
-    {
-      i = spawnv (P_WAIT, got, cmd_argv) == -1;
-      free (got);
-    }
-  else
-    i = spawnvp (P_WAIT, cmd, cmd_argv) == -1;
-  if (i)
-    {
-      perror ("Cannot exec process ");
-      return -1;
-    }
-
-  return 0;
-}
-#endif
 
 /*-----------------------------------------------------------------*/
 /* linkEdit : - calls the linkage editor  with options             */
 /*-----------------------------------------------------------------*/
-static void 
+static void
 linkEdit (char **envp)
 {
   FILE *lnkfile;
-#ifndef USE_SYSTEM_SYSTEM_CALLS
-  char *argv[128];
-#endif
   char *segName, *c;
 
   int i;
@@ -1525,25 +1380,12 @@ linkEdit (char **envp)
   if (options.verbose)
     printf ("sdcc: Calling linker...\n");
 
-#ifdef USE_SYSTEM_SYSTEM_CALLS
   buildCmdLine (buffer, port->linker.cmd, srcFileName, NULL, NULL, NULL);
   if (my_system (buffer))
     {
-      /* either system() or the linker itself has reported an error
-        perror ("Cannot exec linker");
-      */
-      exit (1);
-    }
-#else
-  buildCmdLine (buffer, argv, port->linker.cmd, srcFileName, NULL, NULL, NULL);
-  if (my_system (argv[0], argv))
-    {
-      perror ("Cannot exec linker");
       exit (1);
     }
 
-#endif
-
   if (strcmp (srcFileName, "temp") == 0)
     {
       /* rename "temp.cdb" to "firstRelFile.cdb" */
@@ -1557,29 +1399,17 @@ linkEdit (char **envp)
 /*-----------------------------------------------------------------*/
 /* assemble - spawns the assembler with arguments                  */
 /*-----------------------------------------------------------------*/
-static void 
+static void
 assemble (char **envp)
 {
-#ifdef USE_SYSTEM_SYSTEM_CALLS
   buildCmdLine (buffer, port->assembler.cmd, srcFileName, NULL, NULL, asmOptions);
   if (my_system (buffer))
     {
       /* either system() or the assembler itself has reported an error
-        perror ("Cannot exec assembler");
-      */
-      exit (1);
-    }
-#else
-  char *argv[128];             /* assembler arguments */
-
-  buildCmdLine (buffer, argv, port->assembler.cmd, srcFileName, NULL, NULL, asmOptions);
-
-  if (my_system (argv[0], argv))
-    {
-      perror ("Cannot exec assembler");
+         perror ("Cannot exec assembler");
+       */
       exit (1);
     }
-#endif
 }
 
 
@@ -1587,12 +1417,9 @@ assemble (char **envp)
 /*-----------------------------------------------------------------*/
 /* preProcess - spawns the preprocessor with arguments       */
 /*-----------------------------------------------------------------*/
-static int 
+static int
 preProcess (char **envp)
 {
-#ifndef USE_SYSTEM_SYSTEM_CALLS
-  char *argv[128];
-#endif
   char procDef[128];
 
   preOutName = NULL;
@@ -1638,6 +1465,8 @@ preProcess (char **envp)
       /* add port (processor information to processor */
       sprintf (procDef, "-DSDCC_%s", port->target);
       _addToList (preArgv, procDef);
+      sprintf (procDef, "-D__%s", port->target);
+      _addToList (preArgv, procDef);
 
       if (!preProcOnly)
        preOutName = strdup (tmpnam (NULL));
@@ -1645,28 +1474,13 @@ preProcess (char **envp)
       if (options.verbose)
        printf ("sdcc: Calling preprocessor...\n");
 
-#ifdef USE_SYSTEM_SYSTEM_CALLS
       buildCmdLine (buffer, _preCmd, fullSrcFileName,
                    preOutName, srcFileName, preArgv);
       if (my_system (buffer))
        {
-         /* either system() or the preprocessor itself has reported an error
-            perror ("Cannot exec Preprocessor");
-         */
          exit (1);
        }
-#else
-      buildCmdLine (buffer, argv, _preCmd, fullSrcFileName,
-                   preOutName, srcFileName, preArgv);
 
-      if (my_system (argv[0], argv))
-       {
-         unlink (preOutName);
-         perror ("Cannot exec Preprocessor");
-         exit (1);
-       }
-
-#endif
       if (preProcOnly)
        exit (0);
     }
@@ -1685,7 +1499,7 @@ preProcess (char **envp)
   return 0;
 }
 
-static void 
+static void
 _findPort (int argc, char **argv)
 {
   _validatePorts ();
@@ -1710,7 +1524,7 @@ _findPort (int argc, char **argv)
  * initialises and calls the parser
  */
 
-int 
+int
 main (int argc, char **argv, char **envp)
 {
   /* turn all optimizations off by default */
@@ -1723,35 +1537,17 @@ main (int argc, char **argv, char **envp)
   if (port->init)
     port->init ();
 
-#if defined(_MSC_VER)
+  // Create a default exe search path from the path to the sdcc command
 
-  {
-    int i;
 
-    // Create a default exe search path from the path to the sdcc command
 
-    strcpy (DefaultExePath, argv[0]);
-
-    for (i = strlen (DefaultExePath); i > 0; i--)
-      if (DefaultExePath[i] == '\\')
-       {
-         DefaultExePath[i] = '\0';
-         break;
-       }
-
-    if (i == 0)
-      DefaultExePath[0] = '\0';
-  }
-#else
-
-  if (strchr(argv[0], '/'))
-  {
-      strcpy(DefaultExePath, argv[0]);
-      *(strrchr(DefaultExePath, '/')) = 0;
+  if (strchr (argv[0], DIR_SEPARATOR_CHAR))
+    {
+      strcpy (DefaultExePath, argv[0]);
+      *(strrchr (DefaultExePath, DIR_SEPARATOR_CHAR)) = 0;
       try_dir[0] = DefaultExePath;
-  }
+    }
 
-#endif
 
   setDefaultOptions ();
   parseCmdLine (argc, argv);
@@ -1783,16 +1579,14 @@ main (int argc, char **argv, char **envp)
 
       if (!fatalError)
        {
-/* TSD PIC port hack - if the PIC port option is enabled
-   and SDCC is used to generate PIC code, then we will
-   generate .asm files in gpasm's format instead of SDCC's
-   assembler's format
- */
-#if !OPT_DISABLE_PIC
-         if (IS_PIC_PORT)
+         if (IS_PIC14_PORT)
+           /* TSD PIC port hack - if the PIC port option is enabled
+              and SDCC is used to generate PIC code, then we will
+              generate .asm files in gpasm's format instead of SDCC's
+              assembler's format
+           */
            pic14glue ();
          else
-#endif
            glue ();
          if (fatalError)
            {
@@ -1802,7 +1596,6 @@ main (int argc, char **argv, char **envp)
            {
              if (options.verbose)
                printf ("sdcc: Calling assembler...\n");
-
              assemble (envp);
            }
        }