]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - sim/sh/interp.c
sim: parse_args: display getopt error ourselves
[thirdparty/binutils-gdb.git] / sim / sh / interp.c
index adf05ee883c66a95ae22070ef8d1a441f6d4cfeb..2f59c5c92c9f29ee5841bd012fbcaac6023f9db0 100644 (file)
 #include "gdb/callback.h"
 #include "gdb/remote-sim.h"
 #include "gdb/sim-sh.h"
-#include "run-sim.h"
+
+#include "sim-main.h"
+#include "sim-base.h"
+#include "sim-options.h"
 
 /* This file is local - if newlib changes, then so should this.  */
 #include "syscall.h"
 #define SIGTRAP 5
 #endif
 
-extern unsigned short sh_jump_table[], sh_dsp_table[0x1000], ppi_table[];
+/* TODO: Stop using these names.  */
+#undef SEXT
+#undef SEXT32
 
-int sim_write (SIM_DESC sd, SIM_ADDR addr, const unsigned char *buffer, int size);
+extern unsigned short sh_jump_table[], sh_dsp_table[0x1000], ppi_table[];
 
 #define O_RECOMPILE 85
 #define DEFINE_TABLE
@@ -103,108 +108,7 @@ int sim_write (SIM_DESC sd, SIM_ADDR addr, const unsigned char *buffer, int size
    for a quit. */
 #define POLL_QUIT_INTERVAL 0x60000
 
-typedef struct
-{
-  int regs[20];
-} regstacktype;
-
-typedef union
-{
-
-  struct
-  {
-    int regs[16];
-    int pc;
-
-    /* System registers.  For sh-dsp this also includes A0 / X0 / X1 / Y0 / Y1
-       which are located in fregs, i.e. strictly speaking, these are
-       out-of-bounds accesses of sregs.i .  This wart of the code could be
-       fixed by making fregs part of sregs, and including pc too - to avoid
-       alignment repercussions - but this would cause very onerous union /
-       structure nesting, which would only be managable with anonymous
-       unions and structs.  */
-    union
-      {
-       struct
-         {
-           int mach;
-           int macl;
-           int pr;
-           int dummy3, dummy4;
-           int fpul; /* A1 for sh-dsp -  but only for movs etc.  */
-           int fpscr; /* dsr for sh-dsp */
-         } named;
-       int i[7];
-      } sregs;
-
-    /* sh3e / sh-dsp */
-    union fregs_u
-      {
-       float f[16];
-       double d[8];
-       int i[16];
-      }
-    fregs[2];
-
-    /* Control registers; on the SH4, ldc / stc is privileged, except when
-       accessing gbr.  */
-    union
-      {
-       struct
-         {
-           int sr;
-           int gbr;
-           int vbr;
-           int ssr;
-           int spc;
-           int mod;
-           /* sh-dsp */
-           int rs;
-           int re;
-           /* sh3 */
-           int bank[8];
-           int dbr;            /* debug base register */
-           int sgr;            /* saved gr15 */
-           int ldst;           /* load/store flag (boolean) */
-           int tbr;
-           int ibcr;           /* sh2a bank control register */
-           int ibnr;           /* sh2a bank number register */
-         } named;
-       int i[16];
-      } cregs;
-
-    unsigned char *insn_end;
-
-    int ticks;
-    int stalls;
-    int memstalls;
-    int cycles;
-    int insts;
-
-    int prevlock;
-    int thislock;
-    int exception;
-
-    int end_of_registers;
-
-    int msize;
-#define PROFILE_FREQ 1
-#define PROFILE_SHIFT 2
-    int profile;
-    unsigned short *profile_hist;
-    unsigned char *memory;
-    int xyram_select, xram_start, yram_start;
-    unsigned char *xmem;
-    unsigned char *ymem;
-    unsigned char *xmem_offset;
-    unsigned char *ymem_offset;
-    unsigned long bfd_mach;
-    regstacktype *regstack;
-  }
-  asregs;
-  int asints[40];
-} saved_state_type;
-
+/* TODO: Move into sim_cpu.  */
 saved_state_type saved_state;
 
 struct loop_bounds { unsigned char *start, *end; };
@@ -212,20 +116,14 @@ struct loop_bounds { unsigned char *start, *end; };
 /* These variables are at file scope so that functions other than
    sim_resume can use the fetch/store macros */
 
-static int target_little_endian;
+#define target_little_endian (CURRENT_TARGET_BYTE_ORDER == BFD_ENDIAN_LITTLE)
 static int global_endianw, endianb;
 static int target_dsp;
-static int host_little_endian;
-static char **prog_argv;
+#define host_little_endian (HOST_BYTE_ORDER == BFD_ENDIAN_LITTLE)
 
 static int maskw = 0;
 static int maskl = 0;
 
-static SIM_OPEN_KIND sim_kind;
-static char *myname;
-static int   tracing = 0;
-
-
 /* Short hand definitions of the registers */
 
 #define SBIT(x) ((x)&sbit)
@@ -342,20 +240,6 @@ do { \
 #define FPSCR_SZ  ((GET_FPSCR () & FPSCR_MASK_SZ) != 0)
 #define FPSCR_PR  ((GET_FPSCR () & FPSCR_MASK_PR) != 0)
 
-/* Count the number of arguments in an argv.  */
-static int
-count_argc (char **argv)
-{
-  int i;
-
-  if (! argv)
-    return -1;
-
-  for (i = 0; argv[i] != NULL; ++i)
-    continue;
-  return i;
-}
-
 static void
 set_fpscr1 (int x)
 {
@@ -477,7 +361,7 @@ int valid[16];
 #define UNDEF(x)
 #endif
 
-static void parse_and_set_memory_size (const char *str);
+static void parse_and_set_memory_size (SIM_DESC sd, const char *str);
 static int IOMEM (int addr, int write, int value);
 static struct loop_bounds get_loop_bounds (int, int, unsigned char *,
                                           unsigned char *, int, int);
@@ -488,10 +372,6 @@ static int process_rlat_addr (int);
 static int process_rwat_addr (int);
 static int process_rbat_addr (int);
 
-static host_callback *callback;
-
-
-
 /* Floating point registers */
 
 #define DR(n) (get_dr (n))
@@ -987,9 +867,12 @@ strnswap (int str, int len)
    return offset by which to adjust pc.  */
 
 static int
-trap (int i, int *regs, unsigned char *insn_ptr, unsigned char *memory,
-      int maskl, int maskw, int endianw)
+trap (SIM_DESC sd, int i, int *regs, unsigned char *insn_ptr,
+      unsigned char *memory, int maskl, int maskw, int endianw)
 {
+  host_callback *callback = STATE_CALLBACK (sd);
+  char **prog_argv = STATE_PROG_ARGV (sd);
+
   switch (i)
     {
     case 1:
@@ -1159,16 +1042,16 @@ trap (int i, int *regs, unsigned char *insn_ptr, unsigned char *memory,
              break;
            }
          case SYS_argc:
-           regs[0] = count_argc (prog_argv);
+           regs[0] = countargv (prog_argv);
            break;
          case SYS_argnlen:
-           if (regs[5] < count_argc (prog_argv))
+           if (regs[5] < countargv (prog_argv))
              regs[0] = strlen (prog_argv[regs[5]]);
            else
              regs[0] = -1;
            break;
          case SYS_argn:
-           if (regs[5] < count_argc (prog_argv))
+           if (regs[5] < countargv (prog_argv))
              {
                /* Include the termination byte.  */
                int i = strlen (prog_argv[regs[5]]) + 1;
@@ -1687,7 +1570,7 @@ mcalloc (size_t nmemb, size_t size)
 
 /* Set the memory size to the power of two provided. */
 
-void
+static void
 sim_size (int power)
 {
   sim_memory_size = power;
@@ -1809,10 +1692,6 @@ init_dsp (struct bfd *abfd)
 static void
 init_pointers (void)
 {
-  host_little_endian = 0;
-  * (char*) &host_little_endian = 1;
-  host_little_endian &= 1;
-
   if (saved_state.asregs.msize != 1 << sim_memory_size)
     {
       sim_size (sim_memory_size);
@@ -1869,13 +1748,6 @@ gotcall (int from, int to)
 
 #define MMASKB ((saved_state.asregs.msize -1) & ~0)
 
-int
-sim_stop (SIM_DESC sd)
-{
-  raise_exception (SIGINT);
-  return 1;
-}
-
 void
 sim_resume (SIM_DESC sd, int step, int siggnal)
 {
@@ -1968,9 +1840,6 @@ sim_resume (SIM_DESC sd, int step, int siggnal)
       insts++;
 #endif
     top:
-      if (tracing)
-       fprintf (stderr, "PC: %08lx, insn: %04x\n",
-                (unsigned long) PH2T (insn_ptr), iword);
 
 #include "code.c"
 
@@ -1980,6 +1849,8 @@ sim_resume (SIM_DESC sd, int step, int siggnal)
 
       if (--pollcount < 0)
        {
+         host_callback *callback = STATE_CALLBACK (sd);
+
          pollcount = POLL_QUIT_INTERVAL;
          if ((*callback->poll_quit) != NULL
              && (*callback->poll_quit) (callback))
@@ -2092,8 +1963,8 @@ enum {
   REGBANK_MACL = 19
 };
 
-int
-sim_store_register (SIM_DESC sd, int rn, unsigned char *memory, int length)
+static int
+sh_reg_store (SIM_CPU *cpu, int rn, unsigned char *memory, int length)
 {
   unsigned val;
 
@@ -2265,8 +2136,8 @@ sim_store_register (SIM_DESC sd, int rn, unsigned char *memory, int length)
   return length;
 }
 
-int
-sim_fetch_register (SIM_DESC sd, int rn, unsigned char *memory, int length)
+static int
+sh_reg_fetch (SIM_CPU *cpu, int rn, unsigned char *memory, int length)
 {
   int val;
 
@@ -2436,15 +2307,6 @@ sim_fetch_register (SIM_DESC sd, int rn, unsigned char *memory, int length)
   return length;
 }
 
-int
-sim_trace (SIM_DESC sd)
-{
-  tracing = 1;
-  sim_resume (sd, 0, 0);
-  tracing = 0;
-  return 1;
-}
-
 void
 sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc)
 {
@@ -2469,51 +2331,56 @@ sim_info (SIM_DESC sd, int verbose)
     (double) saved_state.asregs.ticks / (double) now_persec ();
   double virttime = saved_state.asregs.cycles / 36.0e6;
 
-  callback->printf_filtered (callback, "\n\n# instructions executed  %10d\n", 
-                            saved_state.asregs.insts);
-  callback->printf_filtered (callback, "# cycles                 %10d\n",
-                            saved_state.asregs.cycles);
-  callback->printf_filtered (callback, "# pipeline stalls        %10d\n",
-                            saved_state.asregs.stalls);
-  callback->printf_filtered (callback, "# misaligned load/store  %10d\n",
-                            saved_state.asregs.memstalls);
-  callback->printf_filtered (callback, "# real time taken        %10.4f\n",
-                            timetaken);
-  callback->printf_filtered (callback, "# virtual time taken     %10.4f\n",
-                            virttime);
-  callback->printf_filtered (callback, "# profiling size         %10d\n",
-                            sim_profile_size);
-  callback->printf_filtered (callback, "# profiling frequency    %10d\n",
-                            saved_state.asregs.profile);
-  callback->printf_filtered (callback, "# profile maxpc          %10x\n",
-                            (1 << sim_profile_size) << PROFILE_SHIFT);
+  sim_io_printf (sd, "\n\n# instructions executed  %10d\n",
+                saved_state.asregs.insts);
+  sim_io_printf (sd, "# cycles                 %10d\n",
+                saved_state.asregs.cycles);
+  sim_io_printf (sd, "# pipeline stalls        %10d\n",
+                saved_state.asregs.stalls);
+  sim_io_printf (sd, "# misaligned load/store  %10d\n",
+                saved_state.asregs.memstalls);
+  sim_io_printf (sd, "# real time taken        %10.4f\n", timetaken);
+  sim_io_printf (sd, "# virtual time taken     %10.4f\n", virttime);
+  sim_io_printf (sd, "# profiling size         %10d\n", sim_profile_size);
+  sim_io_printf (sd, "# profiling frequency    %10d\n",
+                saved_state.asregs.profile);
+  sim_io_printf (sd, "# profile maxpc          %10x\n",
+                (1 << sim_profile_size) << PROFILE_SHIFT);
 
   if (timetaken != 0)
     {
-      callback->printf_filtered (callback, "# cycles/second          %10d\n", 
-                                (int) (saved_state.asregs.cycles / timetaken));
-      callback->printf_filtered (callback, "# simulation ratio       %10.4f\n", 
-                                virttime / timetaken);
+      sim_io_printf (sd, "# cycles/second          %10d\n",
+                    (int) (saved_state.asregs.cycles / timetaken));
+      sim_io_printf (sd, "# simulation ratio       %10.4f\n",
+                    virttime / timetaken);
     }
 }
 
-void
-sim_set_profile (int n)
+static sim_cia
+sh_pc_get (sim_cpu *cpu)
 {
-  saved_state.asregs.profile = n;
+  return saved_state.asregs.pc;
 }
 
-void
-sim_set_profile_size (int n)
+static void
+sh_pc_set (sim_cpu *cpu, sim_cia pc)
+{
+  saved_state.asregs.pc = pc;
+}
+
+static void
+free_state (SIM_DESC sd)
 {
-  sim_profile_size = n;
+  if (STATE_MODULES (sd) != NULL)
+    sim_module_uninstall (sd);
+  sim_cpu_free_all (sd);
+  sim_state_free (sd);
 }
 
 SIM_DESC
 sim_open (SIM_OPEN_KIND kind, host_callback *cb, struct bfd *abfd, char **argv)
 {
   char **p;
-  int endian_set = 0;
   int i;
   union
     {
@@ -2523,31 +2390,71 @@ sim_open (SIM_OPEN_KIND kind, host_callback *cb, struct bfd *abfd, char **argv)
     }
   mem_word;
 
-  sim_kind = kind;
-  myname = argv[0];
-  callback = cb;
+  SIM_DESC sd = sim_state_alloc (kind, cb);
+  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
 
-  for (p = argv + 1; *p != NULL; ++p)
+  /* The cpu data is kept in a separately allocated chunk of memory.  */
+  if (sim_cpu_alloc_all (sd, 1, /*cgen_cpu_max_extra_bytes ()*/0) != SIM_RC_OK)
     {
-      if (strcmp (*p, "-E") == 0)
-       {
-         ++p;
-         if (*p == NULL)
-           {
-             /* FIXME: This doesn't use stderr, but then the rest of the
-                file doesn't either.  */
-             callback->printf_filtered (callback, "Missing argument to `-E'.\n");
-             return 0;
-           }
-         target_little_endian = strcmp (*p, "big") != 0;
-          endian_set = 1;
-       }
-      else if (isdigit (**p))
-       parse_and_set_memory_size (*p);
+      free_state (sd);
+      return 0;
+    }
+
+  if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
+    {
+      free_state (sd);
+      return 0;
+    }
+
+  /* The parser will print an error message for us, so we silently return.  */
+  if (sim_parse_args (sd, argv) != SIM_RC_OK)
+    {
+      free_state (sd);
+      return 0;
+    }
+
+  /* Check for/establish the a reference program image.  */
+  if (sim_analyze_program (sd,
+                          (STATE_PROG_ARGV (sd) != NULL
+                           ? *STATE_PROG_ARGV (sd)
+                           : NULL), abfd) != SIM_RC_OK)
+    {
+      free_state (sd);
+      return 0;
+    }
+
+  /* Configure/verify the target byte order and other runtime
+     configuration options.  */
+  if (sim_config (sd) != SIM_RC_OK)
+    {
+      sim_module_uninstall (sd);
+      return 0;
     }
 
-  if (abfd != NULL && ! endian_set)
-      target_little_endian = ! bfd_big_endian (abfd);
+  if (sim_post_argv_init (sd) != SIM_RC_OK)
+    {
+      /* Uninstall the modules to avoid memory leaks,
+        file descriptor leaks, etc.  */
+      sim_module_uninstall (sd);
+      return 0;
+    }
+
+  /* CPU specific initialization.  */
+  for (i = 0; i < MAX_NR_PROCESSORS; ++i)
+    {
+      SIM_CPU *cpu = STATE_CPU (sd, i);
+
+      CPU_REG_FETCH (cpu) = sh_reg_fetch;
+      CPU_REG_STORE (cpu) = sh_reg_store;
+      CPU_PC_FETCH (cpu) = sh_pc_get;
+      CPU_PC_STORE (cpu) = sh_pc_set;
+    }
+
+  for (p = argv + 1; *p != NULL; ++p)
+    {
+      if (isdigit (**p))
+       parse_and_set_memory_size (sd, *p);
+    }
 
   if (abfd)
     init_dsp (abfd);
@@ -2560,12 +2467,11 @@ sim_open (SIM_OPEN_KIND kind, host_callback *cb, struct bfd *abfd, char **argv)
     mem_word.c[i] = i;
   endianb = mem_word.i >> (target_little_endian ? 0 : 24) & 0xff;
 
-  /* fudge our descriptor for now */
-  return (SIM_DESC) 1;
+  return sd;
 }
 
 static void
-parse_and_set_memory_size (const char *str)
+parse_and_set_memory_size (SIM_DESC sd, const char *str)
 {
   int n;
 
@@ -2573,38 +2479,7 @@ parse_and_set_memory_size (const char *str)
   if (n > 0 && n <= 24)
     sim_memory_size = n;
   else
-    callback->printf_filtered (callback, "Bad memory size %d; must be 1 to 24, inclusive\n", n);
-}
-
-void
-sim_close (SIM_DESC sd, int quitting)
-{
-  /* nothing to do */
-}
-
-SIM_RC
-sim_load (SIM_DESC sd, const char *prog, bfd *abfd, int from_tty)
-{
-  extern bfd *sim_load_file (); /* ??? Don't know where this should live.  */
-  bfd *prog_bfd;
-
-  prog_bfd = sim_load_file (sd, myname, callback, prog, abfd,
-                           sim_kind == SIM_OPEN_DEBUG,
-                           0, sim_write);
-
-  /* Set the bfd machine type.  */
-  if (prog_bfd)
-    saved_state.asregs.bfd_mach = bfd_get_mach (prog_bfd);
-  else if (abfd)
-    saved_state.asregs.bfd_mach = bfd_get_mach (abfd);
-  else
-    saved_state.asregs.bfd_mach = 0;
-
-  if (prog_bfd == NULL)
-    return SIM_RC_FAIL;
-  if (abfd == NULL)
-    bfd_close (prog_bfd);
-  return SIM_RC_OK;
+    sim_io_printf (sd, "Bad memory size %d; must be 1 to 24, inclusive\n", n);
 }
 
 SIM_RC
@@ -2622,8 +2497,8 @@ sim_create_inferior (SIM_DESC sd, struct bfd *prog_bfd, char **argv, char **env)
   if (prog_bfd != NULL)
     saved_state.asregs.bfd_mach = bfd_get_mach (prog_bfd);
 
-  /* Record the program's arguments. */
-  prog_argv = argv;
+  if (prog_bfd != NULL)
+    init_dsp (prog_bfd);
 
   return SIM_RC_OK;
 }
@@ -2643,29 +2518,16 @@ sim_do_command (SIM_DESC sd, const char *cmd)
   if (strncmp (cmd, sms_cmd, cmdsize) == 0 
       && strchr (" \t", cmd[cmdsize]) != NULL)
     {
-      parse_and_set_memory_size (cmd + cmdsize + 1);
+      parse_and_set_memory_size (sd, cmd + cmdsize + 1);
     }
   else if (strcmp (cmd, "help") == 0)
     {
-      (callback->printf_filtered) (callback, 
-                                  "List of SH simulator commands:\n\n");
-      (callback->printf_filtered) (callback, "set-memory-size <n> -- Set the number of address bits to use\n");
-      (callback->printf_filtered) (callback, "\n");
+      sim_io_printf (sd, "List of SH simulator commands:\n\n");
+      sim_io_printf (sd, "set-memory-size <n> -- Set the number of address bits to use\n");
+      sim_io_printf (sd, "\n");
     }
   else
     {
-      (callback->printf_filtered) (callback, "Error: \"%s\" is not a valid SH simulator command.\n", cmd);
+      sim_io_printf (sd, "Error: \"%s\" is not a valid SH simulator command.\n", cmd);
     }
 }
-
-void
-sim_set_callbacks (host_callback *p)
-{
-  callback = p;
-}
-
-char **
-sim_complete_command (SIM_DESC sd, const char *text, const char *word)
-{
-  return NULL;
-}