* support/Util/NewAlloc.c (freeTrace): Changed free for the gc case to not free...
[fw/sdcc] / src / z80 / main.c
index aa8d569c15f16641d644652b6912a8138f2068db..8db371fdcfbfedabf41687dfeacf563f72bfdf40 100644 (file)
@@ -1,6 +1,31 @@
+/*-------------------------------------------------------------------------
+  main.c - Z80 specific definitions.
+
+  Michael Hope <michaelh@juju.net.nz> 2001
+
+   This program is free software; you can redistribute it and/or modify it
+   under the terms of the GNU General Public License as published by the
+   Free Software Foundation; either version 2, or (at your option) any
+   later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+   In other words, you are welcome to use, share and improve this program.
+   You are forbidden to forbid anyone else to use, share and improve
+   what you give them.   Help stamp out software-hoarding!
+-------------------------------------------------------------------------*/
+
 #include "z80.h"
 #include "MySystem.h"
 #include "BuildCmd.h"
+#include "SDCCutil.h"
 
 static char _z80_defaultRules[] =
 {
@@ -28,6 +53,8 @@ ASM_TYPE;
 static struct
   {
     ASM_TYPE asmType;
+    /* determine if we can register a parameter */    
+    int regParams;
   }
 _G;
 
@@ -57,12 +84,10 @@ _gbz80_init (void)
   z80_opts.sub = SUB_GBZ80;
 }
 
-static int regParmFlg = 0;     /* determine if we can register a parameter */
-
 static void
 _reset_regparm ()
 {
-  regParmFlg = 0;
+  _G.regParams = 0;
 }
 
 static int
@@ -74,40 +99,26 @@ _reg_parm (sym_link * l)
     }
   else 
     {
-      if (regParmFlg == 2)
+      if (_G.regParams == 2)
         {
           return FALSE;
         }
       else
         {
-          regParmFlg++;
+          _G.regParams++;
           return TRUE;
         }
     }
 }
 
-static bool
-_startsWith (const char *sz, const char *key)
-{
-  return !strncmp (sz, key, strlen (key));
-}
-
-static void
-_chomp (char *sz)
-{
-  char *nl;
-  while ((nl = strrchr (sz, '\n')))
-    *nl = '\0';
-}
-
 static int
 _process_pragma (const char *sz)
 {
-  if (_startsWith (sz, "bank="))
+  if (startsWith (sz, "bank="))
     {
       char buffer[128];
       strcpy (buffer, sz + 5);
-      _chomp (buffer);
+      chomp (buffer);
       if (isdigit (buffer[0]))
        {
 
@@ -122,7 +133,7 @@ _process_pragma (const char *sz)
             way. */
          char num[128];
          strcpy (num, sz + 5);
-         _chomp (num);
+         chomp (num);
 
          switch (_G.asmType)
            {
@@ -140,7 +151,7 @@ _process_pragma (const char *sz)
              wassert (0);
            }
        }
-      gbz80_port.mem.code_name = gc_strdup (buffer);
+      gbz80_port.mem.code_name = Safe_strdup (buffer);
       code->sname = gbz80_port.mem.code_name;
       return 0;
     }
@@ -217,12 +228,12 @@ _parseOptions (int *pargc, char **argv, int *i)
            case 'o':
              /* ROM bank */
              sprintf (buffer, "CODE_%u", bank);
-             gbz80_port.mem.code_name = gc_strdup (buffer);
+             gbz80_port.mem.code_name = Safe_strdup (buffer);
              return TRUE;
            case 'a':
              /* RAM bank */
              sprintf (buffer, "DATA_%u", bank);
-             gbz80_port.mem.data_name = gc_strdup (buffer);
+             gbz80_port.mem.data_name = Safe_strdup (buffer);
              return TRUE;
            }
        }
@@ -255,6 +266,40 @@ _parseOptions (int *pargc, char **argv, int *i)
   return FALSE;
 }
 
+static void
+_setValues(void)
+{
+  if (options.nostdlib == FALSE)
+    {
+      setMainValue ("z80libspec", "-k{libdir}{sep}{port} -l{port}.lib");
+      setMainValue ("z80crt0", "{libdir}{sep}{port}{sep}crt0{objext}");
+    }
+  else
+    {
+      setMainValue ("z80libspec", "");
+      setMainValue ("z80crt0", "");
+    }
+
+  setMainValue ("z80extralibfiles", joinn (libFiles, nlibFiles));
+  setMainValue ("z80extralibpaths", joinn (libPaths, nlibPaths));
+
+  if (IS_GB)
+    {
+      setMainValue ("z80outputtypeflag", "-z");
+      setMainValue ("z80outext", ".gb");
+    }
+  else
+    {
+      setMainValue ("z80outputtypeflag", "-i");
+      setMainValue ("z80outext", ".ihx");
+    }
+
+  setMainValue ("z80extraobj", joinn (relFiles, nrelFiles));
+  
+  sprintf (buffer, "-b_CODE=0x%04X -b_DATA=0x%04X", options.code_loc, options.data_loc);
+  setMainValue ("z80bases", buffer);
+}
+
 static void
 _finaliseOptions (void)
 {
@@ -262,6 +307,8 @@ _finaliseOptions (void)
   port->mem.default_globl_map = data;
   if (_G.asmType == ASM_TYPE_ASXXXX && IS_GB)
     asm_addTree (&_asxxxx_gb);
+
+  _setValues();
 }
 
 static void
@@ -276,7 +323,15 @@ _setDefaultOptions (void)
   options.noRegParams = 1;
   /* Default code and data locations. */
   options.code_loc = 0x200;
-  options.data_loc = 0x8000;
+
+  if (IS_GB) 
+    {
+      options.data_loc = 0xC000;
+    }
+  else
+    {
+      options.data_loc = 0x8000;
+    }
 
   optimize.global_cse = 1;
   optimize.label1 = 1;
@@ -284,7 +339,7 @@ _setDefaultOptions (void)
   optimize.label3 = 1;
   optimize.label4 = 1;
   optimize.loopInvariant = 1;
-  optimize.loopInduction = 0;
+  optimize.loopInduction = 1;
 }
 
 /* Mangaling format:
@@ -294,10 +349,11 @@ _setDefaultOptions (void)
       params is the parameter format
 
    policy format:
-    rs
+    rsp
     where:
       r is 'r' for reentrant, 's' for static functions
       s is 'c' for callee saves, 'r' for caller saves
+      p is 'p' for profiling on, 'x' for profiling off
     examples:
       rr - reentrant, caller saves
    params format:
@@ -311,153 +367,36 @@ _mangleSupportFunctionName(char *original)
 {
   char buffer[128];
 
-  if (TARGET_IS_Z80) 
-    {
-      if (options.noRegParams) 
-        {
-          sprintf(buffer, "%s_rr_s", original);
-        }
-      else 
-        {
-          sprintf(buffer, "%s_rr_bds", original);
-        }
-    }
-  else 
-    {
-      strcpy(buffer, original);
-    }
+  sprintf(buffer, "%s_rr%s_%s", original,
+          options.profile ? "f" : "x",
+          options.noRegParams ? "s" : "bds"
+          );
 
-  return gc_strdup(buffer);
+  return Safe_strdup(buffer);
 }
 
 static const char *
 _getRegName (struct regs *reg)
 {
   if (reg)
-    return reg->name;
+    {
+      return reg->name;
+    }
   assert (0);
   return "err";
 }
 
-/** $1 is always the basename.
-    $2 is always the output file.
-    $3 varies
-    $l is the list of extra options that should be there somewhere...
-    MUST be terminated with a NULL.
-*/
-static const char *_z80_linkCmd[] =
-{
-    "link-z80", 
-    "-n",                       // Don't echo output
-    "-c",                       // Command line input
-    "--",                       // Again, command line input...
-    "-b_CODE=0x200",            // Code starts at 0x200
-    "-b_DATA=0x8000",           // RAM starts at 0x8000
-    "-j",                       // Output a symbol file as well
-    "-k" SDCC_LIB_DIR "/z80",   // Library path
-    "-lz80.lib",                // Library to use
-    "-i",                       // Output Intel IHX
-    "$1.ihx",                   // Output to
-    SDCC_LIB_DIR "/z80/crt0.o", // Link in crt0 first
-    "$1.o",                     // Actual code
-    NULL
-};
-
-/* sprintf that appends to the string. */
-static void
-_saprintf(char *pinto, const char *format, ...)
-{
-    va_list ap;
-    va_start(ap, format);
+#define LINKCMD \
+    "{bindir}{sep}link-{port} -n -c -- {z80bases} -m -j" \
+    " {z80libspec}" \
+    " {z80extralibfiles} {z80extralibpaths}" \
+    " {z80outputtypeflag} {srcfilename}{z80outext}" \
+    " {z80crt0}" \
+    " {srcfilename}{objext}" \
+    " {z80extraobj}" 
 
-    vsprintf(pinto + strlen(pinto), format, ap);
-    va_end(ap);
-}
-
-static void
-_z80_link(void)
-{
-    int i;
-    // PENDING
-    char buffer[2048];
-
-    sprintf(buffer, 
-            "link-z80 "
-            "-n "                       // Don't echo output
-            "-c "                       // Command line input
-            "-- "                       // Again, command line input...
-            "-b_CODE=0x%04X "           // Code starts at 0x200
-            "-b_DATA=0x%04X "           // RAM starts at 0x8000
-            "-j ",                      // Output a symbol file as well
-            options.code_loc,
-            options.data_loc
-            );
-
-    // Add the standard lib in.
-    if (options.nostdlib == FALSE) {
-        _saprintf(buffer,
-                  "-k" SDCC_LIB_DIR "/z80 "   // Library path
-                  "-lz80.lib "                // Library to use
-                  );
-    }
-
-    // Add in the library paths and libraries
-    for (i = 0; i < nlibFiles; i++) {
-        _saprintf(buffer, "-k%s ", libFiles[i]);
-    }
-    for (i = 0; i < nlibPaths; i++) {
-        _saprintf(buffer, "-l%s ", libPaths[i]);
-    }
-
-    _saprintf(buffer,
-              "-i "                       // Output Intel IHX
-              "%s.ihx ",                  // Output to
-              srcFileName
-              );
-
-    if (options.nostdlib == FALSE) {
-        _saprintf(buffer, 
-                  SDCC_LIB_DIR "/z80/crt0.o " // Link in crt0 first
-                  );
-    }
-
-    _saprintf(buffer,
-              "%s.o ",                    // Actual code
-              srcFileName
-              );
-
-    // Append all the other targets
-    for (i = 0; i < nrelFiles; i++) {
-        _saprintf(buffer, "%s ", relFiles[i]);
-    }
-
-    // Do it.
-    if (my_system (buffer)) {
-        exit(1);
-    }
-}
-
-static const char *_z80_asmCmd[] =
-{
-    "as-z80", "-plosgff", "$1.o", "$1.asm", NULL
-};
-
-/** $1 is always the basename.
-    $2 is always the output file.
-    $3 varies
-    $l is the list of extra options that should be there somewhere...
-    MUST be terminated with a NULL.
-*/
-static const char *_gbz80_linkCmd[] =
-{
-    // PENDING
-    "link-gbz80", "-nf", "$1", NULL
-};
-
-static const char *_gbz80_asmCmd[] =
-{
-    "as-gbz80", "-plosgff", "$1.o", "$1.asm", NULL
-};
+#define ASMCMD \
+    "{bindir}{sep}as-{port} -plosgff {srcfilename}{objext} {srcfilename}{asmext}"
 
 /* Globals */
 PORT z80_port =
@@ -471,15 +410,17 @@ PORT z80_port =
     MODEL_SMALL
   },
   {
-    _z80_asmCmd,
+    NULL,
+    ASMCMD,
     "-plosgff",                        /* Options with debug */
     "-plosgff",                        /* Options without debug */
     0,
     ".asm"
   },
   {
-    _z80_linkCmd,
-    _z80_link,
+    NULL,
+    LINKCMD,
+    NULL,
     ".o"
   },
   {
@@ -511,7 +452,7 @@ PORT z80_port =
   },
     /* Z80 has no native mul/div commands */
   {
-    0, 0
+    0, 2
   },
   "_",
   _z80_init,
@@ -534,6 +475,7 @@ PORT z80_port =
   1,                           /* transform >= to ! < */
   1,                           /* transform != to !(a == b) */
   0,                           /* leave == */
+  TRUE,                         /* Array initializer support. */       
   PORT_MAGIC
 };
 
@@ -549,14 +491,16 @@ PORT gbz80_port =
     MODEL_SMALL
   },
   {
-    _gbz80_asmCmd,
+    NULL,
+    ASMCMD,
     "-plosgff",                        /* Options with debug */
     "-plosgff",                        /* Options without debug */
-    1,
+    0,
     ".asm"
   },
   {
-    _gbz80_linkCmd,
+    NULL,
+    LINKCMD,
     NULL,
     ".o"
   },
@@ -564,8 +508,8 @@ PORT gbz80_port =
     _gbz80_defaultRules
   },
   {
-       /* Sizes: char, short, int, long, ptr, fptr, gptr, bit, float, max */
-    1, 1, 2, 4, 2, 2, 2, 1, 4, 4
+    /* Sizes: char, short, int, long, ptr, fptr, gptr, bit, float, max */
+    1, 2, 2, 4, 2, 2, 2, 1, 4, 4
   },
   {
     "XSEG",
@@ -604,7 +548,7 @@ PORT gbz80_port =
   _reset_regparm,
   _reg_parm,
   _process_pragma,
-  NULL,
+  _mangleSupportFunctionName,
   TRUE,
   0,                           /* leave lt */
   0,                           /* leave gt */
@@ -612,5 +556,6 @@ PORT gbz80_port =
   1,                           /* transform >= to ! < */
   1,                           /* transform != to !(a == b) */
   0,                           /* leave == */
+  FALSE,                        /* No array initializer support. */
   PORT_MAGIC
 };