fixed Z80 port - crt0.o: cannot open.
[fw/sdcc] / src / z80 / main.c
index 2c172e5c585fdf375c0f8f2da488166fb827946c..6a715333d0665f76223869146be526e3f0dc4fbf 100644 (file)
@@ -22,6 +22,7 @@
    what you give them.   Help stamp out software-hoarding!
 -------------------------------------------------------------------------*/
 
+#include <sys/stat.h>
 #include "z80.h"
 #include "MySystem.h"
 #include "BuildCmd.h"
@@ -41,12 +42,19 @@ 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, NULL }
+  };
+
 typedef enum
   {
     /* Must be first */
     ASM_TYPE_ASXXXX,
     ASM_TYPE_RGBDS,
-    ASM_TYPE_ISAS
+    ASM_TYPE_ISAS,
+    ASM_TYPE_Z80ASM
   }
 ASM_TYPE;
 
@@ -168,27 +176,21 @@ _process_pragma (const char *sz)
 
 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);
@@ -197,23 +199,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))
     {
@@ -269,6 +267,13 @@ _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;
+           }
        }
     }
   return FALSE;
@@ -277,10 +282,36 @@ _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];
+
+      setMainValue ("z80libspec", "-l\"{port}.lib\"");
+
+      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
     {
@@ -288,8 +319,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)
     {
@@ -302,8 +335,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);
 }
@@ -322,7 +359,6 @@ _finaliseOptions (void)
 static void
 _setDefaultOptions (void)
 {
-  options.genericPtr = 1;      /* default on */
   options.nopeep = 0;
   options.stackAuto = 1;
   options.mainreturn = 1;
@@ -391,7 +427,7 @@ _getRegName (struct regs *reg)
     {
       return reg->name;
     }
-  //  assert (0);
+  /*  assert (0); */
   return "err";
 }
 
@@ -430,16 +466,16 @@ _hasNativeMulFor (iCode *ic, sym_link *left, sym_link *right)
 }
 
 #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 =
@@ -447,6 +483,7 @@ PORT z80_port =
   TARGET_ID_Z80,
   "z80",
   "Zilog Z80",                 /* Target name */
+  NULL,                                /* Processor name */
   {
     FALSE,
     MODEL_MEDIUM | MODEL_SMALL,
@@ -486,8 +523,8 @@ PORT z80_port =
     "OVERLAY",
     "GSFINAL",
     "HOME",
-    NULL, // xidata
-    NULL, // xinit
+    NULL, /* xidata */
+    NULL, /* xinit */
     NULL,
     NULL,
     1
@@ -502,12 +539,14 @@ PORT z80_port =
   "_",
   _z80_init,
   _parseOptions,
+  _z80_options,
   _finaliseOptions,
   _setDefaultOptions,
   z80_assignRegisters,
   _getRegName,
   _keywords,
   0,                           /* no assembler preamble */
+  NULL,                                /* no genAssemblerEnd */
   0,                           /* no local IVT generation code */
   0,                            /* no genXINIT code */
   _reset_regparm,
@@ -537,6 +576,7 @@ PORT gbz80_port =
   TARGET_ID_GBZ80,
   "gbz80",
   "Gameboy Z80-like",          /* Target name */
+  NULL,
   {
     FALSE,
     MODEL_MEDIUM | MODEL_SMALL,
@@ -548,7 +588,8 @@ PORT gbz80_port =
     "-plosgff",                        /* Options with debug */
     "-plosgff",                        /* Options without debug */
     0,
-    ".asm"
+    ".asm",
+    NULL                       /* no do_assemble function */
   },
   {
     NULL,
@@ -576,8 +617,8 @@ PORT gbz80_port =
     "OVERLAY",
     "GSFINAL",
     "HOME",
-    NULL, // xidata
-    NULL, // xinit
+    NULL, /* xidata */
+    NULL, /* xinit */
     NULL,
     NULL,
     1
@@ -592,12 +633,14 @@ PORT gbz80_port =
   "_",
   _gbz80_init,
   _parseOptions,
+  _z80_options,
   _finaliseOptions,
   _setDefaultOptions,
   z80_assignRegisters,
   _getRegName,
   _keywords,
   0,                           /* no assembler preamble */
+  NULL,                                /* no genAssemblerEnd */
   0,                           /* no local IVT generation code */
   0,                            /* no genXINIT code */
   _reset_regparm,