* as/mcs51/lkarea.c: removed old K&R style,
[fw/sdcc] / src / z80 / main.c
index a3d70ca92469abcc0c5268e59a772da4ed4b3bba..8fb6b0a2d18ef96d13784d134f62ddb09dbb1e97 100644 (file)
    what you give them.   Help stamp out software-hoarding!
 -------------------------------------------------------------------------*/
 
+#include <sys/stat.h>
 #include "z80.h"
 #include "MySystem.h"
 #include "BuildCmd.h"
 #include "SDCCutil.h"
+#include "dbuf.h"
 
 static char _z80_defaultRules[] =
 {
@@ -41,12 +43,26 @@ static char _gbz80_defaultRules[] =
 
 Z80_OPTS z80_opts;
 
+static OPTION _z80_options[] = 
+  {
+    {  0,   "--callee-saves-bc", &z80_opts.calleeSavesBC, "Force a called function to always save BC" },
+    {  0,   "--portmode=",       NULL,                    "Determine PORT I/O mode (z80/z180)" },
+    {  0, NULL }
+  };
+
+static OPTION _gbz80_options[] = 
+  {
+    {  0,   "--callee-saves-bc", &z80_opts.calleeSavesBC, "Force a called function to always save BC" },
+    {  0, NULL }
+  };
+
 typedef enum
   {
     /* Must be first */
     ASM_TYPE_ASXXXX,
     ASM_TYPE_RGBDS,
-    ASM_TYPE_ISAS
+    ASM_TYPE_ISAS,
+    ASM_TYPE_Z80ASM
   }
 ASM_TYPE;
 
@@ -63,6 +79,9 @@ static char *_keywords[] =
   "sfr",
   "nonbanked",
   "banked",
+  "at",       //.p.t.20030714 adding support for 'sfr at ADDR' construct
+  "_naked",   //.p.t.20030714 adding support for '_naked' functions
+  "critical",
   NULL
 };
 
@@ -71,6 +90,14 @@ extern PORT z80_port;
 
 #include "mappings.i"
 
+static builtins _z80_builtins[] = {
+  /* Disabled for now.
+    { "__builtin_strcpy", "v", 2, {"cg*", "cg*" } },
+    { "__builtin_memcpy", "cg*", 3, {"cg*", "cg*", "ui" } },
+  */
+    { NULL , NULL,0, {NULL}}
+};    
+
 static void
 _z80_init (void)
 {
@@ -85,7 +112,7 @@ _gbz80_init (void)
 }
 
 static void
-_reset_regparm ()
+_reset_regparm (void)
 {
   _G.regParams = 0;
 }
@@ -110,33 +137,38 @@ _reg_parm (sym_link * l)
         }
     }
 }
-
 static int
 _process_pragma (const char *sz)
 {
-  if (startsWith (sz, "bank="))
+  if( startsWith( sz, "bank=" ) || startsWith( sz, "bank " ))
+  {
+    char buffer[128];
+    
+    if (sz[4]=='=')
+      werror(W_DEPRECATED_PRAGMA, "bank=");
+    
+    strncpy (buffer, sz + 5, sizeof (buffer));
+    buffer[sizeof (buffer) - 1 ] = '\0';
+    chomp (buffer);
+    if (isdigit (buffer[0]))
     {
-      char buffer[128];
-      strcpy (buffer, sz + 5);
-      chomp (buffer);
-      if (isdigit (buffer[0]))
-       {
 
-       }
-      else if (!strcmp (buffer, "BASE"))
-       {
-         strcpy (buffer, "HOME");
-       }
-      if (isdigit (buffer[0]))
-       {
+    }
+    else if (!strcmp (buffer, "BASE"))
+    {
+      strcpy (buffer, "HOME");
+    }
+    if (isdigit (buffer[0]))
+    {
          /* Arg was a bank number.  Handle in an ASM independent
             way. */
-         char num[128];
-         strcpy (num, sz + 5);
-         chomp (num);
+      char num[128];
+      strncpy (num, sz + 5, sizeof (num));
+      num[sizeof (num) -1] = '\0';
+      chomp (num);
 
-         switch (_G.asmType)
-           {
+      switch (_G.asmType)
+      {
            case ASM_TYPE_ASXXXX:
              sprintf (buffer, "CODE_%s", num);
              break;
@@ -149,38 +181,52 @@ _process_pragma (const char *sz)
              break;
            default:
              wassert (0);
-           }
-       }
-      gbz80_port.mem.code_name = Safe_strdup (buffer);
-      code->sname = gbz80_port.mem.code_name;
-      return 0;
+      }
     }
+    gbz80_port.mem.code_name = Safe_strdup (buffer);
+    code->sname = gbz80_port.mem.code_name;
+    return 0;
+  }
+  else if( startsWith( sz, "portmode=" ) || startsWith( sz, "portmode " ))
+  { /*.p.t.20030716 - adding pragma to manipulate z80 i/o port addressing modes */
+    char bfr[128];
+
+    if (sz[8]=='=')
+      werror(W_DEPRECATED_PRAGMA, "portmode=");
+
+    strncpy( bfr, sz + 9, sizeof (bfr));
+    bfr[sizeof (bfr) - 1] = '\0';
+    chomp( bfr );
+
+    if     ( !strcmp( bfr, "z80"     )){ z80_opts.port_mode =  80; }
+    else if( !strcmp( bfr, "z180"    )){ z80_opts.port_mode = 180; }
+    else if( !strcmp( bfr, "save"    )){ z80_opts.port_back = z80_opts.port_mode; }
+    else if( !strcmp( bfr, "restore" )){ z80_opts.port_mode = z80_opts.port_back; }
+    else                                 return( 1 );
+
+    return( 0 );
+  }
+
   return 1;
 }
 
 static const char *_gbz80_rgbasmCmd[] =
 {
-  "rgbasm", "-o$1.o", "$1.asm", NULL
+  "rgbasm", "-o\"$1.o\"", "\"$1.asm\"", NULL
 };
 
 static const char *_gbz80_rgblinkCmd[] =
 {
-  "xlink", "-tg", "-n$1.sym", "-m$1.map", "-zFF", "$1.lnk", NULL
+  "xlink", "-tg", "-n\"$1.sym\"", "-m\"$1.map\"", "-zFF", "\"$1.lnk\"", NULL
 };
 
 static void
 _gbz80_rgblink (void)
 {
   FILE *lnkfile;
-  const char *sz;
-
-  int i;
-  sz = srcFileName;
-  if (!sz)
-    sz = "a";
 
   /* first we need to create the <filename>.lnk file */
-  sprintf (scratchFileName, "%s.lnk", sz);
+  sprintf (scratchFileName, "%s.lnk", dstFileName);
   if (!(lnkfile = fopen (scratchFileName, "w")))
     {
       werror (E_FILE_OPEN_ERR, scratchFileName);
@@ -189,23 +235,19 @@ _gbz80_rgblink (void)
 
   fprintf (lnkfile, "[Objects]\n");
 
-  if (srcFileName)
-    fprintf (lnkfile, "%s.o\n", sz);
+  fprintf (lnkfile, "%s.o\n", dstFileName);
 
-  for (i = 0; i < nrelFiles; i++)
-    fprintf (lnkfile, "%s\n", relFiles[i]);
+  fputStrSet(lnkfile, relFilesSet);
 
   fprintf (lnkfile, "\n[Libraries]\n");
   /* additional libraries if any */
-  for (i = 0; i < nlibFiles; i++)
-    fprintf (lnkfile, "%s\n", libFiles[i]);
+  fputStrSet(lnkfile, libFilesSet);
 
-
-  fprintf (lnkfile, "\n[Output]\n" "%s.gb", sz);
+  fprintf (lnkfile, "\n[Output]\n" "%s.gb", dstFileName);
 
   fclose (lnkfile);
 
-  buildCmdLine (buffer,port->linker.cmd, sz, NULL, NULL, NULL);
+  buildCmdLine (buffer,port->linker.cmd, dstFileName, NULL, NULL, NULL);
   /* call the linker */
   if (my_system (buffer))
     {
@@ -261,7 +303,27 @@ _parseOptions (int *pargc, char **argv, int *i)
              _G.asmType = ASM_TYPE_ISAS;
              return TRUE;
            }
+         else if (!strcmp (argv[*i], "--asm=z80asm"))
+           {
+              port->assembler.externGlobal = TRUE;
+             asm_addTree (&_z80asm_z80);
+             _G.asmType = ASM_TYPE_ISAS;
+             return TRUE;
+           }
        }
+      else if (!strncmp (argv[*i], "--portmode=", 11))
+       {
+         if (!strcmp (argv[*i], "--portmode=z80"))
+           {
+             z80_opts.port_mode =  80;
+             return TRUE;
+           }
+         else if (!strcmp (argv[*i], "--portmode=z180"))
+           {
+             z80_opts.port_mode =  180;
+             return TRUE;
+           }
+        }
     }
   return FALSE;
 }
@@ -269,10 +331,48 @@ _parseOptions (int *pargc, char **argv, int *i)
 static void
 _setValues(void)
 {
+  const char *s;
+
   if (options.nostdlib == FALSE)
     {
-      setMainValue ("z80libspec", "-k{libdir}{sep}{port} -l{port}.lib");
-      setMainValue ("z80crt0", "{libdir}{sep}{port}{sep}crt0{objext}");
+      const char *s;
+      char path[PATH_MAX];
+      struct dbuf_s dbuf;
+
+      dbuf_init(&dbuf, PATH_MAX);
+
+      for (s = setFirstItem(libDirsSet); s != NULL; s = setNextItem(libDirsSet))
+        {
+          buildCmdLine2(path, sizeof path, "-k\"%s" DIR_SEPARATOR_STRING "{port}\" ", s);
+          dbuf_append(&dbuf, path, strlen(path));
+        }
+      buildCmdLine2(path, sizeof path, "-l\"{port}.lib\"", s);
+      dbuf_append(&dbuf, path, strlen(path));
+
+      setMainValue ("z80libspec", dbuf_c_str(&dbuf));
+      dbuf_destroy(&dbuf);
+
+      for (s = setFirstItem(libDirsSet); s != NULL; s = setNextItem(libDirsSet))
+        {
+          struct stat stat_buf;
+
+          buildCmdLine2(path, sizeof path, "%s" DIR_SEPARATOR_STRING "{port}" DIR_SEPARATOR_STRING "crt0{objext}", s);
+          if (stat(path, &stat_buf) == 0)
+            break;
+        }
+
+      if (s == NULL)
+        setMainValue ("z80crt0", "\"crt0{objext}\"");
+      else
+        {
+          char *buf;
+          size_t len = strlen(path) + 3;
+
+          buf = Safe_alloc(len);
+          SNPRINTF(buf, len, "\"%s\"", path);
+          setMainValue("z80crt0", buf);
+          Safe_free(buf);
+        } 
     }
   else
     {
@@ -280,8 +380,10 @@ _setValues(void)
       setMainValue ("z80crt0", "");
     }
 
-  setMainValue ("z80extralibfiles", joinn (libFiles, nlibFiles));
-  setMainValue ("z80extralibpaths", joinn (libPaths, nlibPaths));
+  setMainValue ("z80extralibfiles", (s = joinStrSet(libFilesSet)));
+  Safe_free((void *)s);
+  setMainValue ("z80extralibpaths", (s = joinStrSet(libPathsSet)));
+  Safe_free((void *)s);
 
   if (IS_GB)
     {
@@ -294,8 +396,12 @@ _setValues(void)
       setMainValue ("z80outext", ".ihx");
     }
 
-  setMainValue ("z80extraobj", joinn (relFiles, nrelFiles));
-  
+  setMainValue ("stdobjdstfilename" , "{dstfilename}{objext}");
+  setMainValue ("stdlinkdstfilename", "{dstfilename}{z80outext}");
+
+  setMainValue ("z80extraobj", (s = joinStrSet(relFilesSet)));
+  Safe_free((void *)s);
+
   sprintf (buffer, "-b_CODE=0x%04X -b_DATA=0x%04X", options.code_loc, options.data_loc);
   setMainValue ("z80bases", buffer);
 }
@@ -314,12 +420,12 @@ _finaliseOptions (void)
 static void
 _setDefaultOptions (void)
 {
-  options.genericPtr = 1;      /* default on */
   options.nopeep = 0;
   options.stackAuto = 1;
   options.mainreturn = 1;
   /* first the options part */
   options.intlong_rent = 1;
+  options.float_rent = 1;
   options.noRegParams = 1;
   /* Default code and data locations. */
   options.code_loc = 0x200;
@@ -382,7 +488,7 @@ _getRegName (struct regs *reg)
     {
       return reg->name;
     }
-  assert (0);
+  /*  assert (0); */
   return "err";
 }
 
@@ -420,17 +526,41 @@ _hasNativeMulFor (iCode *ic, sym_link *left, sym_link *right)
   return FALSE;
 }
 
+/* Indicate which extended bit operations this port supports */
+static bool
+hasExtBitOp (int op, int size)
+{
+  if (op == GETHBIT)
+    return TRUE;
+  else
+    return FALSE;
+}
+
+/* Indicate the expense of an access to an output storage class */
+static int
+oclsExpense (struct memmap *oclass)
+{
+  if (IN_FARSPACE(oclass))
+    return 1;
+    
+  return 0;
+}
+
+
+#define LINKCMD "link-{port} -nf {dstfilename}"
+/*
 #define LINKCMD \
-    "{bindir}{sep}link-{port} -n -c -- {z80bases} -m -j" \
+    "link-{port} -n -c -- {z80bases} -m -j" \
     " {z80libspec}" \
     " {z80extralibfiles} {z80extralibpaths}" \
-    " {z80outputtypeflag} {srcfilename}{z80outext}" \
+    " {z80outputtypeflag} \"{linkdstfilename}\"" \
     " {z80crt0}" \
-    " {srcfilename}{objext}" \
-    " {z80extraobj}" 
+    " \"{dstfilename}{objext}\"" \
+    " {z80extraobj}"
+*/
 
 #define ASMCMD \
-    "{bindir}{sep}as-{port} -plosgff {srcfilename}{objext} {srcfilename}{asmext}"
+    "as-{port} -plosgff \"{objdstfilename}\" \"{dstfilename}{asmext}\""
 
 /* Globals */
 PORT z80_port =
@@ -438,7 +568,9 @@ PORT z80_port =
   TARGET_ID_Z80,
   "z80",
   "Zilog Z80",                 /* Target name */
+  NULL,                                /* Processor name */
   {
+    glue,
     FALSE,
     MODEL_MEDIUM | MODEL_SMALL,
     MODEL_SMALL
@@ -455,7 +587,8 @@ PORT z80_port =
     NULL,
     LINKCMD,
     NULL,
-    ".o"
+    ".o",
+    1
   },
   {
     _z80_defaultRules
@@ -470,6 +603,7 @@ PORT z80_port =
     "CODE",
     "DATA",
     "ISEG",
+    NULL, /* pdata */
     "XSEG",
     "BSEG",
     "RSEG",
@@ -477,10 +611,13 @@ PORT z80_port =
     "OVERLAY",
     "GSFINAL",
     "HOME",
+    NULL, /* xidata */
+    NULL, /* xinit */
     NULL,
     NULL,
     1
   },
+  { NULL, NULL },
   {
     -1, 0, 0, 4, 0, 2
   },
@@ -488,22 +625,43 @@ PORT z80_port =
   {
     0, 2
   },
+  {
+    z80_emitDebuggerSymbol
+  },
+  {
+    255,        /* maxCount */
+    3,          /* sizeofElement */
+    /* The rest of these costs are bogus. They approximate */
+    /* the behavior of src/SDCCicode.c 1.207 and earlier.  */
+    {4,4,4},    /* sizeofMatchJump[] */
+    {0,0,0},    /* sizeofRangeCompare[] */
+    0,          /* sizeofSubtract */
+    3,          /* sizeofDispatch */
+  },
   "_",
   _z80_init,
   _parseOptions,
+  _z80_options,
+  NULL,
   _finaliseOptions,
   _setDefaultOptions,
   z80_assignRegisters,
   _getRegName,
   _keywords,
   0,                           /* no assembler preamble */
+  NULL,                                /* no genAssemblerEnd */
   0,                           /* no local IVT generation code */
+  0,                            /* no genXINIT code */
+  NULL,                        /* genInitStartup */
   _reset_regparm,
   _reg_parm,
   _process_pragma,
   _mangleSupportFunctionName,
   _hasNativeMulFor,
+  hasExtBitOp,                 /* hasExtBitOp */
+  oclsExpense,                 /* oclsExpense */
   TRUE,
+  TRUE,                                /* little endian */
   0,                           /* leave lt */
   0,                           /* leave gt */
   1,                           /* transform <= to ! > */
@@ -511,6 +669,11 @@ PORT z80_port =
   1,                           /* transform != to !(a == b) */
   0,                           /* leave == */
   TRUE,                         /* Array initializer support. */       
+  0,                            /* no CSE cost estimation yet */
+  _z80_builtins,               /* no builtin functions */
+  GPOINTER,                    /* treat unqualified pointers as "generic" pointers */
+  1,                           /* reset labelKey to 1 */
+  1,                           /* globals & local static allowed */
   PORT_MAGIC
 };
 
@@ -520,7 +683,9 @@ PORT gbz80_port =
   TARGET_ID_GBZ80,
   "gbz80",
   "Gameboy Z80-like",          /* Target name */
+  NULL,
   {
+    glue,
     FALSE,
     MODEL_MEDIUM | MODEL_SMALL,
     MODEL_SMALL
@@ -531,13 +696,15 @@ PORT gbz80_port =
     "-plosgff",                        /* Options with debug */
     "-plosgff",                        /* Options without debug */
     0,
-    ".asm"
+    ".asm",
+    NULL                       /* no do_assemble function */
   },
   {
     NULL,
     LINKCMD,
     NULL,
-    ".o"
+    ".o",
+    1
   },
   {
     _gbz80_defaultRules
@@ -552,6 +719,7 @@ PORT gbz80_port =
     "CODE",
     "DATA",
     "ISEG",
+    NULL, /* pdata */
     "XSEG",
     "BSEG",
     "RSEG",
@@ -559,10 +727,13 @@ PORT gbz80_port =
     "OVERLAY",
     "GSFINAL",
     "HOME",
+    NULL, /* xidata */
+    NULL, /* xinit */
     NULL,
     NULL,
     1
   },
+  { NULL, NULL },
   {
     -1, 0, 0, 2, 0, 4
   },
@@ -570,28 +741,54 @@ PORT gbz80_port =
   {
     0, 2
   },
+  {
+    z80_emitDebuggerSymbol
+  },
+  {
+    255,        /* maxCount */
+    3,          /* sizeofElement */
+    /* The rest of these costs are bogus. They approximate */
+    /* the behavior of src/SDCCicode.c 1.207 and earlier.  */
+    {4,4,4},    /* sizeofMatchJump[] */
+    {0,0,0},    /* sizeofRangeCompare[] */
+    0,          /* sizeofSubtract */
+    3,          /* sizeofDispatch */
+  },
   "_",
   _gbz80_init,
   _parseOptions,
+  _gbz80_options,
+  NULL,
   _finaliseOptions,
   _setDefaultOptions,
   z80_assignRegisters,
   _getRegName,
   _keywords,
   0,                           /* no assembler preamble */
+  NULL,                                /* no genAssemblerEnd */
   0,                           /* no local IVT generation code */
+  0,                            /* no genXINIT code */
+  NULL,                        /* genInitStartup */
   _reset_regparm,
   _reg_parm,
   _process_pragma,
   _mangleSupportFunctionName,
   _hasNativeMulFor,
+  hasExtBitOp,                 /* hasExtBitOp */
+  oclsExpense,                 /* oclsExpense */
   TRUE,
+  TRUE,                                /* little endian */
   0,                           /* leave lt */
   0,                           /* leave gt */
   1,                           /* transform <= to ! > */
   1,                           /* transform >= to ! < */
   1,                           /* transform != to !(a == b) */
   0,                           /* leave == */
-  FALSE,                        /* No array initializer support. */
+  TRUE,                         /* Array initializer support. */
+  0,                            /* no CSE cost estimation yet */
+  NULL,                        /* no builtin functions */
+  GPOINTER,                    /* treat unqualified pointers as "generic" pointers */
+  1,                           /* reset labelKey to 1 */
+  1,                           /* globals & local static allowed */
   PORT_MAGIC
 };