Merged changes between gbdk-293 and main
[fw/sdcc] / src / SDCCmain.c
index 12d340dad22a6450ebb45e738899dcf584493f96..28eebe722aebd716e3f03678a71186803001c996 100644 (file)
 
 #include "common.h"
 #include <ctype.h>
+
+#if NATIVE_WIN32
+#include <process.h>
+#else
 #include "spawn.h"
+#endif
 
 /* This is a bit messy.  We cant include unistd.h as it defines
    'link' which we also use.
@@ -71,7 +76,9 @@ char    *preOutName;
 
 #define OPTION_LARGE_MODEL "-model-large"
 #define OPTION_SMALL_MODEL "-model-small"
+#define OPTION_FLAT24_MODEL "-model-flat24"
 #define OPTION_STACK_AUTO  "-stack-auto"
+#define OPTION_STACK_10BIT "-stack-10bit"
 #define OPTION_XSTACK      "-xstack"
 #define OPTION_GENERIC     "-generic"
 #define OPTION_NO_GCSE     "-nogcse"
@@ -111,6 +118,7 @@ char    *preOutName;
 #define OPTION_VERSION     "-version"
 #define OPTION_STKAFTRDATA "-stack-after-data"
 #define OPTION_PREPROC_ONLY "-preprocessonly"
+#define OPTION_C1_MODE   "-c1mode"
 #define OPTION_HELP         "-help"
 #define OPTION_CALLEE_SAVES "-callee-saves"
 #define OPTION_NOREGPARMS   "-noregparms"
@@ -120,14 +128,34 @@ static const char *_preCmd[] = {
     "-I" SDCC_INCLUDE_DIR, "$l", "$1", "$2", NULL
 };
 
+#if !OPT_DISABLE_MCS51
 extern PORT mcs51_port;
+#endif
+#if !OPT_DISABLE_GBZ80
+extern PORT gbz80_port;
+#endif
+#if !OPT_DISABLE_Z80
 extern PORT z80_port;
+#endif
+#if !OPT_DISABLE_AVR
+extern PORT avr_port;
+#endif
 
 PORT *port;
 
 static PORT *_ports[] = {
-    &mcs51_port,
-    &z80_port
+#if !OPT_DISABLE_MCS51
+   &mcs51_port,
+#endif
+#if !OPT_DISABLE_GBZ80
+    &gbz80_port,
+#endif
+#if !OPT_DISABLE_Z80
+    &z80_port,
+#endif
+#if !OPT_DISABLE_AVR
+    &avr_port,
+#endif
 };
 
 #define NUM_PORTS (sizeof(_ports)/sizeof(_ports[0]))
@@ -146,7 +174,8 @@ static int _setPort(const char *name)
        }
     }
     /* Error - didnt find */
-    return 1;
+    werror(E_UNKNOWN_TARGET,name);
+    exit(1);
 }
 
 static void _buildCmdLine(char *into, char **args, const char **cmds, 
@@ -296,8 +325,8 @@ static void setDefaultOptions()
     int i ;
 
     for ( i = 0 ; i < 128 ; i++)
-       preArgv[i] = linkOptions [i] =
-           asmOptions[i] = relFiles[i] = libFiles[i] =
+       preArgv[i] = asmOptions [i] =
+           linkOptions[i] = relFiles[i] = libFiles[i] =
            libPaths[i] = NULL ;
 
     /* first the options part */
@@ -342,7 +371,7 @@ static void processFile (char *s)
     }
 
     /* otherwise depending on the file type */
-    if (strcmp(fext,".c") == 0 || strcmp(fext,".C") == 0) {
+    if (strcmp(fext,".c") == 0 || strcmp(fext,".C") == 0 || options.c1mode) {
        /* source file name : not if we already have a 
           source file */
        if (srcFileName) {
@@ -399,6 +428,20 @@ static void processFile (char *s)
   
 }
 
+static void _processC1Arg(char *s)
+{
+    if (srcFileName) {
+       if (options.out_name) {
+           werror(W_TOO_MANY_SRC,s);
+           return;
+       }
+       options.out_name = strdup(s);
+    }
+    else {
+       processFile(s);
+    }
+}
+
 static void _addToList(const char **list, const char *str)
 {
     /* This is the bad way to do things :) */
@@ -439,14 +482,24 @@ int   parseCmdLine ( int argc, char **argv )
            }
 
            if (strcmp(&argv[i][1],OPTION_LARGE_MODEL) == 0) {
-               options.model = 1;
+               options.model = MODEL_LARGE;
                 continue;
            }
            
            if (strcmp(&argv[i][1],OPTION_SMALL_MODEL) == 0) {
-               options.model = 0;
+               options.model = MODEL_SMALL;
+                continue;
+           }
+           
+           if (strcmp(&argv[i][1],OPTION_FLAT24_MODEL) == 0) {
+               options.model = MODEL_FLAT24;
                 continue;
            }
+           
+           if (strcmp(&argv[i][1],OPTION_STACK_10BIT) == 0) {
+               options.stack10bit = 1;
+               continue;
+           }
 
            if (strcmp(&argv[i][1],OPTION_STACK_AUTO) == 0) {
                options.stackAuto = 1;
@@ -528,6 +581,11 @@ int   parseCmdLine ( int argc, char **argv )
                 continue;
            }
 
+           if (strcmp(&argv[i][1],OPTION_C1_MODE) == 0) {
+               options.c1mode = 1;
+                continue;
+           }
+
            
            if (strcmp(&argv[i][1],OPTION_DUMP_ALL) == 0) {
                options.dump_rassgn = 
@@ -716,15 +774,23 @@ int   parseCmdLine ( int argc, char **argv )
                 continue;
            }
 
-           if (!port->parseOption(&argc, argv))
+           if (!port->parseOption(&argc, argv, &i))
+           {
                werror(W_UNKNOWN_OPTION,argv[i]);
+           }
+           else
+           {
+               continue;
+           }
        }      
 
        /* these are undocumented options */
        /* if preceded by '/' then turn off certain optmizations, used
           for debugging only these are also the legacy options from
-          version 1.xx will be removed gradually */
-       if ( *argv[i] == '/') {
+          version 1.xx will be removed gradually.
+          It may be an absolute filename.
+       */
+       if ( *argv[i] == '/' && strlen(argv[i]) < 3) {
            switch (argv[i][1]) {
                
            case 'p':
@@ -904,15 +970,18 @@ int   parseCmdLine ( int argc, char **argv )
                break ;
 
            default:
-               if (!port->parseOption(&argc, argv))
+               if (!port->parseOption(&argc, argv, &i))
                    werror(W_UNKNOWN_OPTION,argv[i]);
            }
            continue ;
        }
 
-       if (!port->parseOption(&argc, argv)) {
+       if (!port->parseOption(&argc, argv, &i)) {
            /* no option must be a filename */
-           processFile(argv[i]);
+           if (options.c1mode)
+               _processC1Arg(argv[i]);
+           else
+               processFile(argv[i]);
        }
     }  
 
@@ -930,7 +999,6 @@ int   parseCmdLine ( int argc, char **argv )
            fprintf(cdbFile,"M:%s\n",moduleName);
        }
     }
-    port->finaliseOptions();
     return 0;
 }
 
@@ -940,15 +1008,40 @@ int   parseCmdLine ( int argc, char **argv )
 char *try_dir[]= {SRCDIR "/bin",PREFIX "/bin", NULL};
 int my_system (const char *cmd, char **cmd_argv)
 {    
-
     char *dir, *got= NULL; int i= 0;
-    while (!got && try_dir[i]) {
-       dir= (char*)malloc(strlen(try_dir[i])+strlen(cmd)+10);
-       strcpy(dir, try_dir[i]); strcat(dir, "/"); strcat(dir, cmd);
-       if (access(dir, X_OK) == 0)
-           got= strdup(dir);
-       free(dir);
-       i++;
+
+    while (!got && try_dir[i])
+    {
+        dir= (char*)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 FEATURE_VERBOSE_EXEC
     if (verboseExec) {
@@ -979,6 +1072,7 @@ static void linkEdit (char **envp)
 {
     FILE *lnkfile ;
     char *argv[128];
+    char *segName, *c;
 
     int i;
     if (!srcFileName)
@@ -1000,24 +1094,50 @@ static void linkEdit (char **envp)
     
     /*if (options.debug) */
     fprintf(lnkfile,"-z\n");
+
+#define WRITE_SEG_LOC(N, L) \
+    segName = strdup(N); \
+    c = strtok(segName, " \t"); \
+    fprintf (lnkfile,"-b %s = 0x%04x\n", c, L); \
+    if (segName) { free(segName); } 
+    
     /* code segment start */
-    fprintf (lnkfile,"-b CODE = 0x%04x\n",options.code_loc);
-    /* data segment start */
-    fprintf (lnkfile,"-b DSEG = 0x%04x\n",options.data_loc);
+    WRITE_SEG_LOC(CODE_NAME, options.code_loc);
+    
+     /* data segment start */
+     WRITE_SEG_LOC(DATA_NAME, options.data_loc);
+                 
     /* xdata start */
-    fprintf (lnkfile,"-b XSEG = 0x%04x\n",options.xdata_loc);
+    WRITE_SEG_LOC(XDATA_NAME, options. xdata_loc);
+
     /* indirect data */
-    fprintf (lnkfile,"-b ISEG = 0x%04x\n",options.idata_loc);
+    WRITE_SEG_LOC(IDATA_NAME, options.idata_loc);
+
     /* bit segment start */
-    fprintf (lnkfile,"-b BSEG = 0x%04x\n",0);
+    WRITE_SEG_LOC(BIT_NAME, 0);
     
     /* add the extra linker options */
     for (i=0; linkOptions[i] ; i++)
        fprintf(lnkfile,"%s\n",linkOptions[i]);
 
     /* standard library path */
-    fprintf (lnkfile,"-k %s/%s\n",SDCC_LIB_DIR/*STD_LIB_PATH*/,
-            ( (options.model==0) ? "small": "large"));
+    switch(options.model)
+    {
+        case MODEL_SMALL:
+                   c = "small";
+                   break;
+               case MODEL_LARGE:
+                   c = "large";
+                   break;
+               case MODEL_FLAT24:
+                   c = "flat24";
+                   break;
+        default:
+            werror(W_UNKNOWN_MODEL, __FILE__, __LINE__);
+            c = "unknown";
+            break;
+    }
+    fprintf (lnkfile,"-k %s/%s\n",SDCC_LIB_DIR/*STD_LIB_PATH*/,c);
            
     /* other library paths if specified */
     for (i = 0 ; i < nlibPaths ; i++ )
@@ -1087,40 +1207,61 @@ static int preProcess (char **envp)
 
     preOutName = NULL;
 
-    /* if using external stack define the macro */
-    if ( options.useXstack )
-       _addToList(preArgv, "-DSDCC_USE_XSTACK");
-    
-    /* set the macro for stack autos   */
-    if ( options.stackAuto )
-       _addToList(preArgv, "-DSDCC_STACK_AUTO");
+    if (!options.c1mode) {
+       /* if using external stack define the macro */
+       if ( options.useXstack )
+           _addToList(preArgv, "-DSDCC_USE_XSTACK");
+       
+       /* set the macro for stack autos        */
+       if ( options.stackAuto )
+           _addToList(preArgv, "-DSDCC_STACK_AUTO");
+           
+       /* set the macro for stack autos        */
+       if ( options.stack10bit )
+           _addToList(preArgv, "-DSDCC_STACK_TENBIT"); 
     
-    /* set the macro for large model   */
-    if ( options.model )
-       _addToList(preArgv, "-DSDCC_MODEL_LARGE");
-    else
-       _addToList(preArgv, "-DSDCC_MODEL_SMALL");
+       /* set the macro for large model        */
+       switch(options.model)
+           {
+           case MODEL_LARGE:
+               _addToList(preArgv, "-DSDCC_MODEL_LARGE");
+               break;
+           case MODEL_SMALL:
+               _addToList(preArgv, "-DSDCC_MODEL_SMALL");
+               break;
+           case MODEL_FLAT24:
+               _addToList(preArgv, "-DSDCC_MODEL_FLAT24");
+               break;
+           default:
+               werror(W_UNKNOWN_MODEL, __FILE__, __LINE__);
+               break;
+           }       
+           
     
-    /* add port (processor information to processor */
-    sprintf(procDef,"-DSDCC_%s",port->target);
-    _addToList(preArgv,procDef);
+       /* add port (processor information to processor */
+       sprintf(procDef,"-DSDCC_%s",port->target);
+       _addToList(preArgv,procDef);
 
-    if (!preProcOnly)
-       preOutName = strdup(tmpnam(NULL));
+       if (!preProcOnly)
+           preOutName = strdup(tmpnam(NULL));
 
-    _buildCmdLine(buffer, argv, _preCmd, fullSrcFileName, 
-                 preOutName, srcFileName, preArgv);
+       _buildCmdLine(buffer, argv, _preCmd, fullSrcFileName, 
+                     preOutName, srcFileName, preArgv);
 
-    if (my_system(argv[0], argv)) {
-       unlink (preOutName);
-       perror("Cannot exec Preprocessor");
-       exit(1);
-    }
+       if (my_system(argv[0], argv)) {
+           unlink (preOutName);
+           perror("Cannot exec Preprocessor");
+           exit(1);
+       }
 
-    if (preProcOnly)
-       exit(0);
+       if (preProcOnly)
+           exit(0);
+    }
+    else {
+       preOutName = fullSrcFileName;
+    }
 
-    yyin = fopen(preOutName,"r");
+    yyin = fopen(preOutName, "r");
     if (yyin == NULL) {
        perror("Preproc file not found\n");
        exit(1);
@@ -1157,14 +1298,23 @@ int main ( int argc, char **argv , char **envp)
     /*printVersionInfo ();*/
 
     _findPort(argc, argv);
+    /* Initalise the port. */
+    if (port->init)
+       port->init();
+
     setDefaultOptions();
     parseCmdLine(argc,argv);
 
+    initMem();
+
+    port->finaliseOptions();
+
     /* if no input then printUsage & exit */
-    if (!srcFileName && !nrelFiles) {
+    if ((!options.c1mode && !srcFileName && !nrelFiles) || (options.c1mode && !srcFileName && !options.out_name)) {
        printUsage();
        exit(0);
     }
+
        
     if (srcFileName)
        preProcess(envp) ;
@@ -1172,7 +1322,6 @@ int main ( int argc, char **argv , char **envp)
     if (srcFileName) {
 
        initSymt();
-       initMem();                  
        initiCode();
        initCSupport ();
        initPeepHole();
@@ -1180,8 +1329,11 @@ int main ( int argc, char **argv , char **envp)
 
        if (!fatalError) {
            glue();
-           assemble(envp);
-       }
+           if (!options.c1mode)
+               assemble(envp);
+       } else {
+           exit(-1);
+        }
        
     }
     
@@ -1191,15 +1343,16 @@ int main ( int argc, char **argv , char **envp)
     if (!options.cc_only && 
        !fatalError      &&
        !noAssemble      &&
+       !options.c1mode  &&
        (srcFileName || nrelFiles))
        linkEdit (envp);
 
     if (yyin && yyin != stdin)
        fclose(yyin);
 
-    if (preOutName) {
-       unlink(preOutName);
-       free(preOutName);
+    if (preOutName && !options.c1mode) {
+        unlink(preOutName);
+        free(preOutName);
     }
     return 0;