Fixed up support for the gbz80 such that it will at least run (and fail badly) the...
[fw/sdcc] / src / z80 / main.c
index 4675deada1d4d4a8fd7e7b8f6691ed33136509d9..777ab9c0cd65b177e306051d37ae80209d8588bd 100644 (file)
@@ -1,4 +1,6 @@
 #include "z80.h"
+#include "MySystem.h"
+#include "BuildCmd.h"
 
 static char _z80_defaultRules[] =
 {
@@ -26,6 +28,8 @@ ASM_TYPE;
 static struct
   {
     ASM_TYPE asmType;
+    /* determine if we can register a parameter */    
+    int regParams;
   }
 _G;
 
@@ -55,22 +59,31 @@ _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
 _reg_parm (sym_link * l)
 {
-  if (regParmFlg == 2)
-    return 0;
-
-  regParmFlg++;
-  return 1;
+  if (options.noRegParams) 
+    {
+      return FALSE;
+    }
+  else 
+    {
+      if (_G.regParams == 2)
+        {
+          return FALSE;
+        }
+      else
+        {
+          _G.regParams++;
+          return TRUE;
+        }
+    }
 }
 
 static bool
@@ -149,7 +162,6 @@ _gbz80_rgblink (void)
 {
   FILE *lnkfile;
   const char *sz;
-  char *argv[128];
 
   int i;
   sz = srcFileName;
@@ -157,10 +169,10 @@ _gbz80_rgblink (void)
     sz = "a";
 
   /* first we need to create the <filename>.lnk file */
-  sprintf (buffer, "%s.lnk", sz);
-  if (!(lnkfile = fopen (buffer, "w")))
+  sprintf (scratchFileName, "%s.lnk", sz);
+  if (!(lnkfile = fopen (scratchFileName, "w")))
     {
-      werror (E_FILE_OPEN_ERR, buffer);
+      werror (E_FILE_OPEN_ERR, scratchFileName);
       exit (1);
     }
 
@@ -182,9 +194,9 @@ _gbz80_rgblink (void)
 
   fclose (lnkfile);
 
-  buildCmdLine (buffer, argv, port->linker.cmd, sz, NULL, NULL, NULL);
+  buildCmdLine (buffer,port->linker.cmd, sz, NULL, NULL, NULL);
   /* call the linker */
-  if (my_system (argv[0], argv))
+  if (my_system (buffer))
     {
       perror ("Cannot exec linker");
       exit (1);
@@ -259,9 +271,20 @@ _setDefaultOptions (void)
   options.nopeep = 0;
   options.stackAuto = 1;
   options.mainreturn = 1;
-  options.nodebug = 1;
   /* first the options part */
   options.intlong_rent = 1;
+  options.noRegParams = 1;
+  /* Default code and data locations. */
+  options.code_loc = 0x200;
+
+  if (IS_GB) 
+    {
+      options.data_loc = 0xC000;
+    }
+  else
+    {
+      options.data_loc = 0x8000;
+    }
 
   optimize.global_cse = 1;
   optimize.label1 = 1;
@@ -269,14 +292,49 @@ _setDefaultOptions (void)
   optimize.label3 = 1;
   optimize.label4 = 1;
   optimize.loopInvariant = 1;
-  optimize.loopInduction = 0;
+  optimize.loopInduction = 1;
+}
+
+/* Mangaling format:
+    _fun_policy_params
+    where:
+      policy is the function policy
+      params is the parameter format
+
+   policy format:
+    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:
+    A combination of register short names and s to signify stack variables.
+    examples:
+      bds - first two args appear in BC and DE, the rest on the stack
+      s - all arguments are on the stack.
+*/
+static char *
+_mangleSupportFunctionName(char *original)
+{
+  char buffer[128];
+
+  sprintf(buffer, "%s_rr%s_%s", original,
+          options.profile ? "f" : "x",
+          options.noRegParams ? "s" : "bds"
+          );
+
+  return gc_strdup(buffer);
 }
 
 static const char *
 _getRegName (struct regs *reg)
 {
   if (reg)
-    return reg->name;
+    {
+      return reg->name;
+    }
   assert (0);
   return "err";
 }
@@ -287,31 +345,148 @@ _getRegName (struct regs *reg)
     $l is the list of extra options that should be there somewhere...
     MUST be terminated with a NULL.
 */
+static const char *_z80_asmCmd[] =
+{
+    "as-z80", 
+    "-plosgff", 
+    "$1.o", 
+    "$1.asm", 
+    NULL
+};
+
 static const char *_z80_linkCmd[] =
 {
-  "link-z80", "-nf", "$1", NULL
+    "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
 };
 
-static const char *_z80_asmCmd[] =
+static const char *_gbz80_asmCmd[] =
 {
-  "as-z80", "-plosgff", "$1.o", "$1.asm", NULL
+    "as-gbz80", 
+    "-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[] =
 {
-  "link-gbz80", "-nf", "$1", NULL
+    "link-z80", 
+    "-n",                       // Don't echo output
+    "-c",                       // Command line input
+    "--",                       // Again, command line input...
+    "-b_CODE=0x200",            // Code starts at 0x200
+    "-b_DATA=0xC000",           // RAM starts at 0xC000
+    "-j",                       // Output a symbol file as well
+    "-k" SDCC_LIB_DIR "/gbz80", // Library path
+    "-lgbz80.lib",              // Library to use
+    "-z",                       // Output Gameboy image
+    "$1.gb",                    // Output to
+    SDCC_LIB_DIR "/gbz80/crt0.o",// Link in crt0 first
+    "$1.o",                     // Actual code
+    NULL
 };
 
-static const char *_gbz80_asmCmd[] =
+/* sprintf that appends to the string. */
+static void
+_saprintf(char *pinto, const char *format, ...)
 {
-  "as-gbz80", "-plosgff", "$1.o", "$1.asm", NULL
-};
+    va_list ap;
+    va_start(ap, format);
+
+    vsprintf(pinto + strlen(pinto), format, ap);
+    va_end(ap);
+}
+
+static void
+_link(const char *portName, const char *portExt, const char *portOutputType)
+{
+    int i;
+    // PENDING
+    char buffer[2048];
+
+    sprintf(buffer, 
+            "link-%s "
+            "-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
+            portName,
+            options.code_loc,
+            options.data_loc
+            );
+
+    // Add the standard lib in.
+    if (options.nostdlib == FALSE) {
+        _saprintf(buffer,
+                  "-k" SDCC_LIB_DIR "/%s "    // Library path
+                  "-l%s.lib ",                // Library to use
+                  portName, portName
+                  );
+    }
+
+    // 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,
+              "-%s "                      // Output type
+              "%s.%s ",                   // Output to
+              portOutputType, srcFileName, portExt
+              );
+
+    if (options.nostdlib == FALSE) {
+        _saprintf(buffer, 
+                  SDCC_LIB_DIR "/%s/crt0.o ", // Link in crt0 first
+                  portName
+                  );
+    }
+
+    _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 void
+_z80_link(void)
+{
+  _link("z80", "ihx", "i");
+}
+
+static void
+_gbz80_link(void)
+{
+  _link("gbz80", "gb", "z");
+}
 
 /* Globals */
 PORT z80_port =
@@ -333,7 +508,7 @@ PORT z80_port =
   },
   {
     _z80_linkCmd,
-    NULL,
+    _z80_link,
     ".o"
   },
   {
@@ -341,7 +516,7 @@ PORT z80_port =
   },
   {
        /* Sizes: char, short, int, long, ptr, fptr, gptr, bit, float, max */
-    1, 1, 2, 4, 2, 2, 2, 1, 4, 4
+    1, 2, 2, 4, 2, 2, 2, 1, 4, 4
   },
   {
     "XSEG",
@@ -380,6 +555,7 @@ PORT z80_port =
   _reset_regparm,
   _reg_parm,
   _process_pragma,
+  _mangleSupportFunctionName,
   TRUE,
   0,                           /* leave lt */
   0,                           /* leave gt */
@@ -387,6 +563,7 @@ PORT z80_port =
   1,                           /* transform >= to ! < */
   1,                           /* transform != to !(a == b) */
   0,                           /* leave == */
+  TRUE,                         /* Array initializer support. */       
   PORT_MAGIC
 };
 
@@ -405,19 +582,20 @@ PORT gbz80_port =
     _gbz80_asmCmd,
     "-plosgff",                        /* Options with debug */
     "-plosgff",                        /* Options without debug */
-    1
+    0,
+    ".asm"
   },
   {
     _gbz80_linkCmd,
-    NULL,
+    _gbz80_link,
     ".o"
   },
   {
     _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",
@@ -456,6 +634,7 @@ PORT gbz80_port =
   _reset_regparm,
   _reg_parm,
   _process_pragma,
+  _mangleSupportFunctionName,
   TRUE,
   0,                           /* leave lt */
   0,                           /* leave gt */
@@ -463,5 +642,6 @@ PORT gbz80_port =
   1,                           /* transform >= to ! < */
   1,                           /* transform != to !(a == b) */
   0,                           /* leave == */
+  FALSE,                        /* No array initializer support. */
   PORT_MAGIC
 };