Fix TARGET_IS_PIC error
[fw/sdcc] / src / SDCCmain.c
index f575a84e1da3f8c76c1770a2c0499f32f6d325f1..62fb47392b3baf01a8e9f653e72e9d2ddcc92e83 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"
@@ -136,7 +135,7 @@ char DefaultExePath[128];
 static const char *_preCmd[] =
 {
   "sdcpp", "-Wall", "-lang-c++", "-DSDCC=1",
-  "$l", "-I" SDCC_INCLUDE_DIR, "$1", "$2", NULL
+  "$l", "$1", "$2", NULL
 };
 
 PORT *port;
@@ -159,7 +158,7 @@ static PORT *_ports[] =
   &ds390_port,
 #endif
 #if !OPT_DISABLE_PIC
-  &pic14_port,
+  &pic_port,
 #endif
 #if !OPT_DISABLE_I186
   &i186_port,
@@ -174,13 +173,13 @@ static PORT *_ports[] =
 /**
    remove me - TSD a hack to force sdcc to generate gpasm format .asm files.
  */
-extern void pic14glue ();
+extern void picglue ();
 
 /** Sets the port to the one given by the command line option.
     @param    The name minus the option (eg 'mcs51')
     @return     0 on success.
 */
-static int 
+static void
 _setPort (const char *name)
 {
   int i;
@@ -189,7 +188,7 @@ _setPort (const char *name)
       if (!strcmp (_ports[i]->target, name))
        {
          port = _ports[i];
-         return 0;
+         return;
        }
     }
   /* Error - didnt find */
@@ -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;
@@ -352,19 +283,23 @@ printVersionInfo ()
           "SDCC : ");
   for (i = 0; i < NUM_PORTS; i++)
     fprintf (stderr, "%s%s", i == 0 ? "" : "/", _ports[i]->target);
+
   fprintf (stderr, " %s"
 #ifdef SDCC_SUB_VERSION_STR
           "/" SDCC_SUB_VERSION_STR
 #endif
-          " ` "
 #ifdef __CYGWIN32__
           " (CYGWIN32)\n"
 #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;
@@ -1090,10 +1034,7 @@ parseCmdLine (int argc, char **argv)
 
            case 'm':
              /* Used to select the port */
-             if (_setPort (argv[i] + 2))
-               {
-                 werror (W_UNSUPP_OPTION, "-m", "Unrecognised processor");
-               }
+             _setPort (argv[i] + 2);
              break;
 
            case 'a':
@@ -1260,21 +1201,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 +1220,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 +1260,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;
@@ -1472,7 +1324,7 @@ linkEdit (char **envp)
   /* standard library path */
   if (!options.nostdlib)
     {
-      if (IS_DS390_PORT)
+      if (TARGET_IS_DS390)
        {
          c = "ds390";
        }
@@ -1525,24 +1377,11 @@ 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)
     {
@@ -1557,29 +1396,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 +1414,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;
@@ -1634,10 +1458,16 @@ preProcess (char **envp)
          break;
        }
 
+      /* standard include path */
+      if (!options.nostdinc) {
+       _addToList (preArgv, "-I" SDCC_INCLUDE_DIR);
+      }
 
       /* 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 +1475,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 +1500,7 @@ preProcess (char **envp)
   return 0;
 }
 
-static void 
+static void
 _findPort (int argc, char **argv)
 {
   _validatePorts ();
@@ -1710,7 +1525,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 */
@@ -1718,40 +1533,27 @@ main (int argc, char **argv, char **envp)
 
   /*printVersionInfo (); */
 
+  if (NUM_PORTS==0) {
+    fprintf (stderr, "Build error: no ports are enabled.\n");
+    exit (1);
+  }
+
   _findPort (argc, argv);
   /* Initalise the port. */
   if (port->init)
     port->init ();
 
-#if defined(_MSC_VER)
-
-  {
-    int i;
-
-    // Create a default exe search path from the path to the sdcc command
-
-    strcpy (DefaultExePath, argv[0]);
+  // Create a default exe search path from the path to the sdcc command
 
-    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 +1585,15 @@ 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)
-           pic14glue ();
+         if (TARGET_IS_PIC) {
+           /* 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
+           */
+           picglue ();
+         }
          else
-#endif
            glue ();
          if (fatalError)
            {
@@ -1802,7 +1603,6 @@ main (int argc, char **argv, char **envp)
            {
              if (options.verbose)
                printf ("sdcc: Calling assembler...\n");
-
              assemble (envp);
            }
        }