RFE #2484693 and peephole improvements
[fw/sdcc] / src / ds390 / main.c
index ae17bdc26aeeca2fc7400b837ffd5147b7e31054..8b82663f35f62346fab4869f82932e867aa0d1b4 100644 (file)
@@ -26,13 +26,13 @@ static OPTION _ds390_options[] =
   {
     { 0, OPTION_FLAT24_MODEL,   NULL, "use the flat24 model for the ds390 (default)" },
     { 0, OPTION_STACK_8BIT,     NULL, "use the 8bit stack for the ds390 (not supported yet)" },
-    { 0, OPTION_STACK_SIZE,     NULL, "Tells the linker to allocate this space for stack"},
+    { 0, OPTION_STACK_SIZE,     &options.stack_size, "Tells the linker to allocate this space for stack", CLAT_INTEGER },
     { 0, "--pack-iram",         NULL, "Tells the linker to pack variables in internal ram (default)"},
-    { 0, "--no-pack-iram",      &options.no_pack_iram,"Tells the linker not to pack variables in internal ram"},
+    { 0, "--no-pack-iram",      &options.no_pack_iram, "Tells the linker not to pack variables in internal ram"},
     { 0, "--stack-10bit",       &options.stack10bit, "use the 10bit stack for ds390 (default)" },
-    { 0, "--use-accelerator",   &options.useAccelerator,"generate code for ds390 arithmetic accelerator"},
-    { 0, "--protect-sp-update", &options.protect_sp_update,"will disable interrupts during ESP:SP updates"},
-    { 0, "--parms-in-bank1",    &options.parms_in_bank1,"use Bank1 for parameter passing"},
+    { 0, "--use-accelerator",   &options.useAccelerator, "generate code for ds390 arithmetic accelerator"},
+    { 0, "--protect-sp-update", &options.protect_sp_update, "will disable interrupts during ESP:SP updates"},
+    { 0, "--parms-in-bank1",    &options.parms_in_bank1, "use Bank1 for parameter passing"},
     { 0, NULL }
   };
 
@@ -145,11 +145,6 @@ _ds390_parseOptions (int *pargc, char **argv, int *i)
       options.model = MODEL_FLAT24;
       return TRUE;
     }
-  else if (!strcmp (argv[*i], OPTION_STACK_SIZE))
-    {
-      options.stack_size = getIntArg (OPTION_STACK_SIZE, argv, i, *pargc);
-      return TRUE;
-    }
   return FALSE;
 }
 
@@ -315,6 +310,44 @@ _ds390_genIVT (struct dbuf_s * oBuf, symbol ** interrupts, int maxInterrupts)
   return TRUE;
 }
 
+static void
+_ds390_genInitStartup (FILE *of)
+{
+  fprintf (of, "__sdcc_gsinit_startup:\n");
+  /* if external stack is specified then the
+     higher order byte of the xdatalocation is
+     going into P2 and the lower order going into
+     spx */
+  if (options.useXstack)
+    {
+      fprintf (of, "\tmov\tP2,#0x%02x\n",
+               (((unsigned int) options.xdata_loc) >> 8) & 0xff);
+      fprintf (of, "\tmov\t_spx,#0x%02x\n",
+               (unsigned int) options.xdata_loc & 0xff);
+    }
+
+  // This should probably be a port option, but I'm being lazy.
+  // on the 400, the firmware boot loader gives us a valid stack
+  // (see '400 data sheet pg. 85 (TINI400 ROM Initialization code)
+  if (!TARGET_IS_DS400)
+    {
+      /* initialise the stack pointer.  JCF: aslink takes care of the location */
+      fprintf (of, "\tmov\tsp,#__start__stack - 1\n");     /* MOF */
+    }
+
+  fprintf (of, "\tlcall\t__sdcc_external_startup\n");
+  fprintf (of, "\tmov\ta,dpl\n");
+  fprintf (of, "\tjz\t__sdcc_init_data\n");
+  fprintf (of, "\tljmp\t__sdcc_program_startup\n");
+  fprintf (of, "__sdcc_init_data:\n");
+
+  // if the port can copy the XINIT segment to XISEG
+  if (port->genXINIT)
+    {
+      port->genXINIT(of);
+    }
+}
+
 /* Generate code to copy XINIT to XISEG */
 static void _ds390_genXINIT (FILE * of) {
   fprintf (of, ";      _ds390_genXINIT() start\n");
@@ -957,7 +990,7 @@ PORT ds390_port =
   NULL,                         /* no genAssemblerEnd */
   _ds390_genIVT,
   _ds390_genXINIT,
-  NULL,                         /* genInitStartup */
+  _ds390_genInitStartup,
   _ds390_reset_regparm,
   _ds390_regparm,
   NULL,
@@ -992,31 +1025,17 @@ static OPTION _tininative_options[] =
   {
     { 0, OPTION_FLAT24_MODEL,   NULL, "use the flat24 model for the ds390 (default)" },
     { 0, OPTION_STACK_8BIT,     NULL, "use the 8bit stack for the ds390 (not supported yet)" },
-    { 0, OPTION_STACK_SIZE,     NULL, "Tells the linker to allocate this space for stack"},
+    { 0, OPTION_STACK_SIZE,     &options.stack_size, "Tells the linker to allocate this space for stack", CLAT_INTEGER },
     { 0, "--pack-iram",         NULL, "Tells the linker to pack variables in internal ram (default)"},
-    { 0, "--no-pack-iram",      &options.no_pack_iram,"Tells the linker not to pack variables in internal ram"},
+    { 0, "--no-pack-iram",      &options.no_pack_iram, "Tells the linker not to pack variables in internal ram"},
     { 0, "--stack-10bit",       &options.stack10bit, "use the 10bit stack for ds390 (default)" },
-    { 0, "--use-accelerator",   &options.useAccelerator,"generate code for ds390 arithmetic accelerator"},
-    { 0, "--protect-sp-update", &options.protect_sp_update,"will disable interrupts during ESP:SP updates"},
-    { 0, "--parms-in-bank1",    &options.parms_in_bank1,"use Bank1 for parameter passing"},
-    { 0, OPTION_TINI_LIBID,     NULL, "<nnnn> LibraryID used in -mTININative"},
+    { 0, "--use-accelerator",   &options.useAccelerator, "generate code for ds390 arithmetic accelerator"},
+    { 0, "--protect-sp-update", &options.protect_sp_update, "will disable interrupts during ESP:SP updates"},
+    { 0, "--parms-in-bank1",    &options.parms_in_bank1, "use Bank1 for parameter passing"},
+    { 0, OPTION_TINI_LIBID,     &options.tini_libid, "<nnnn> LibraryID used in -mTININative", CLAT_INTEGER },
     { 0, NULL }
   };
 
-static bool
-_tininative_parseOptions (int *pargc, char **argv, int *i)
-{
-  if (_ds390_parseOptions (pargc, argv, i))
-    return TRUE;
-
-  if (!strcmp (argv[*i], OPTION_TINI_LIBID))
-    {
-      options.tini_libid = getIntArg (OPTION_TINI_LIBID, argv, i, *pargc);
-      return TRUE;
-    }
-  return FALSE;
-}
-
 static void _tininative_init (void)
 {
     asm_addTree (&asm_a390_mapping);
@@ -1293,7 +1312,7 @@ PORT tininative_port =
   },
   "",
   _tininative_init,
-  _tininative_parseOptions,
+  _ds390_parseOptions,
   _tininative_options,
   NULL,
   _tininative_finaliseOptions,
@@ -1305,7 +1324,7 @@ PORT tininative_port =
   _tininative_genAssemblerEnd,
   _tininative_genIVT,
   NULL,
-  NULL,                         /* genInitStartup */
+  _ds390_genInitStartup,
   _ds390_reset_regparm,
   _ds390_regparm,
   NULL,
@@ -1356,13 +1375,13 @@ static OPTION _ds400_options[] =
   {
     { 0, OPTION_FLAT24_MODEL,   NULL, "use the flat24 model for the ds400 (default)" },
     { 0, OPTION_STACK_8BIT,     NULL, "use the 8bit stack for the ds400 (not supported yet)" },
-    { 0, OPTION_STACK_SIZE,     NULL, "Tells the linker to allocate this space for stack"},
+    { 0, OPTION_STACK_SIZE,     &options.stack_size, "Tells the linker to allocate this space for stack", CLAT_INTEGER },
     { 0, "--pack-iram",         NULL, "Tells the linker to pack variables in internal ram (default)"},
-    { 0, "--no-pack-iram",      &options.no_pack_iram,"Tells the linker not to pack variables in internal ram"},
+    { 0, "--no-pack-iram",      &options.no_pack_iram, "Tells the linker not to pack variables in internal ram"},
     { 0, "--stack-10bit",       &options.stack10bit, "use the 10bit stack for ds400 (default)" },
-    { 0, "--use-accelerator",   &options.useAccelerator,"generate code for ds400 arithmetic accelerator"},
-    { 0, "--protect-sp-update", &options.protect_sp_update,"will disable interrupts during ESP:SP updates"},
-    { 0, "--parms-in-bank1",    &options.parms_in_bank1,"use Bank1 for parameter passing"},
+    { 0, "--use-accelerator",   &options.useAccelerator, "generate code for ds400 arithmetic accelerator"},
+    { 0, "--protect-sp-update", &options.protect_sp_update, "will disable interrupts during ESP:SP updates"},
+    { 0, "--parms-in-bank1",    &options.parms_in_bank1, "use Bank1 for parameter passing"},
     { 0, NULL }
   };
 
@@ -1558,7 +1577,7 @@ PORT ds400_port =
   NULL,                         /* no genAssemblerEnd */
   _ds400_genIVT,
   _ds390_genXINIT,
-  NULL,                         /* genInitStartup */
+  _ds390_genInitStartup,
   _ds390_reset_regparm,
   _ds390_regparm,
   NULL,