gas: avoid spurious failures in non-ELF targets in the SPARC testsuite.
[deliverable/binutils-gdb.git] / sim / ppc / sim_calls.c
index d3462b3f6f65e6950c1c3eba442a39664b5a6fb7..470c95862a14ffdfff801340e39e3b3d42e56cc4 100644 (file)
@@ -1,10 +1,10 @@
 /*  This file is part of the program psim.
 
-    Copyright (C) 1994-1995, Andrew Cagney <cagney@highland.com.au>
+    Copyright 1994, 1995, 1996, 1998, 2003 Andrew Cagney
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
+    the Free Software Foundation; either version 3 of the License, or
     (at your option) any later version.
 
     This program is distributed in the hope that it will be useful,
     GNU General Public License for more details.
  
     You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+    along with this program; if not, see <http://www.gnu.org/licenses/>.
  
     */
 
 
 #include <signal.h> /* FIXME - should be machine dependant version */
 #include <stdarg.h>
-#include <stdlib.h>
-#include <string.h>
 #include <ctype.h>
 
-#include "basics.h"
 #include "psim.h"
+#include "options.h"
 
-#include "../../gdb/defs.h"
+#undef printf_filtered /* blow away the mapping */
 
-#include "devices.h"
-
-#include "../../gdb/remote-sim.h"
-#include "../../gdb/callback.h"
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
 
+#ifdef HAVE_STRING_H
+#include <string.h>
+#else
+#ifdef HAVE_STRINGS_H
+#include <strings.h>
+#endif
+#endif
+
+#include "libiberty.h"
+#include "bfd.h"
+#include "gdb/callback.h"
+#include "gdb/remote-sim.h"
+#include "gdb/signals.h"
+
+/* Define the rate at which the simulator should poll the host
+   for a quit. */
+#ifndef POLL_QUIT_INTERVAL
+#define POLL_QUIT_INTERVAL 0x20
+#endif
+
+static int poll_quit_count = POLL_QUIT_INTERVAL;
 
 /* Structures used by the simulator, for gdb just have static structures */
 
-static psim *simulator;
-static int nr_cpus;
-static char *register_names[] = REGISTER_NAMES;
-static int print_info = 0;
+psim *simulator;
+static device *root_device;
+static host_callback *callbacks;
 
-void
-sim_open (char *args)
+SIM_DESC
+sim_open (SIM_OPEN_KIND kind,
+         host_callback *callback,
+         struct bfd *abfd,
+         char * const *argv)
 {
-  int i;
+  callbacks = callback;
 
+  /* Note: The simulation is not created by sim_open() because
+     complete information is not yet available */
   /* trace the call */
-  TRACE(trace_gdb, ("sim_open(args=%s) called\n", args ? args : "(null)"));
-
-  if (args) {
-    char *buf = (char *)alloca (strlen (args) + 1);
-    char *p;
-    strcpy (buf, args);
-
-    p = strtok (args, " \t");
-    while (p != (char *)0) {
-      if (*p != '-')
-       error ("Argument is not an option '%s'", p);
-
-      else {
-       /* check arguments -- note, main.c also contains argument processing
-          code for the standalone emulator.  */
-       while (*++p != '\0') {
-         switch (*p) {
-         default:
-           error ("Usage: target sim [ -a -p -c -C -s -i -I -t ]\n");
-           break;
-         case 'a':
-           for (i = 0; i < nr_trace; i++)
-             trace[i] = 1;
-           break;
-         case 'p':
-           trace[trace_cpu] = trace[trace_semantics] = 1;
-           break;
-         case 'c':
-           trace[trace_core] = 1;
-           break;
-         case 'C':
-           trace[trace_console_device] = 1;
-           break;
-         case 's':
-           trace[trace_create_stack] = 1;
-           break;
-         case 'i':
-           trace[trace_icu_device] = 1;
-           break;
-         case 'I':
-           print_info = 1;
-           break;
-         case 't':
-           trace[trace_device_tree] = 1;
-           break;
-         }
-       }
-      }
+  TRACE(trace_gdb, ("sim_open called\n"));
 
-      p = strtok ((char *)0, " \t");
-    }
-  }
+  if (root_device != NULL)
+    sim_io_printf_filtered("Warning - re-open of simulator leaks memory\n");
+  root_device = psim_tree();
+  simulator = NULL;
+
+  if (psim_options (root_device, argv + 1, kind) == NULL)
+    return NULL;
 
-  /* do something */
-  TRACE(trace_tbd, ("sim_open() - TBD - should parse the arguments\n"));
-  TRACE(trace_tbd, ("sim_open() - TBD - can not create simulator here as do not have description of it\n"));
+  if (ppc_trace[trace_opts])
+    print_options ();
+
+  /* fudge our descriptor for now */
+  return (SIM_DESC) 1;
 }
 
 
 void
-sim_close (int quitting)
+sim_close (SIM_DESC sd, int quitting)
 {
   TRACE(trace_gdb, ("sim_close(quitting=%d) called\n", quitting));
-  if (print_info)
-    psim_print_info (simulator, 1);
-
-  /* nothing to do */
+  if (ppc_trace[trace_print_info] && simulator != NULL)
+    psim_print_info (simulator, ppc_trace[trace_print_info]);
 }
 
 
-int
-sim_load (char *prog, int from_tty)
+SIM_RC
+sim_load (SIM_DESC sd, const char *prog, bfd *abfd, int from_tty)
 {
   TRACE(trace_gdb, ("sim_load(prog=%s, from_tty=%d) called\n",
                    prog, from_tty));
-
-  /* sanity check */
-  if (prog == NULL) {
-    error ("sim_load() - TBD - read stan shebs e-mail about how to find the program name?\n");
-    return -1;
-  }
-  TRACE(trace_tbd, ("sim_load() - TBD - parse that prog stripping things like quotes\n"));
+  ASSERT(prog != NULL);
 
   /* create the simulator */
   TRACE(trace_gdb, ("sim_load() - first time, create the simulator\n"));
-  nr_cpus = (WITH_SMP ? WITH_SMP : 1);
-  simulator = psim_create(prog, nr_cpus);
+  simulator = psim_create(prog, root_device);
 
   /* bring in all the data section */
-  psim_load(simulator);
+  psim_init(simulator);
+
+  /* get the start address */
+  if (abfd == NULL)
+    {
+      abfd = bfd_openr (prog, 0);
+      if (abfd == NULL)
+       error ("psim: can't open \"%s\": %s\n", 
+              prog, bfd_errmsg (bfd_get_error ()));
+      if (!bfd_check_format (abfd, bfd_object)) 
+       {
+         const char *errmsg = bfd_errmsg (bfd_get_error ());
+         bfd_close (abfd);
+         error ("psim: \"%s\" is not an object file: %s\n",
+                prog, errmsg);
+       }
+      bfd_close (abfd);
+    }
 
-  return 0;
+  return SIM_RC_OK;
 }
 
 
-void
-sim_kill (void)
+int
+sim_read (SIM_DESC sd, SIM_ADDR mem, unsigned char *buf, int length)
 {
-  TRACE(trace_gdb, ("sim_kill(void) called\n"));
-  /* do nothing, nothing to do */
+  int result = psim_read_memory(simulator, MAX_NR_PROCESSORS,
+                               buf, mem, length);
+  TRACE(trace_gdb, ("sim_read(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
+                   (long)mem, (long)buf, length, result));
+  return result;
 }
 
 
 int
-sim_read (SIM_ADDR mem, unsigned char *buf, int length)
+sim_write (SIM_DESC sd, SIM_ADDR mem, const unsigned char *buf, int length)
 {
-  return psim_read_memory(simulator, nr_cpus, buf, mem, length,
-                         raw_transfer);
+  int result = psim_write_memory(simulator, MAX_NR_PROCESSORS,
+                                buf, mem, length,
+                                1/*violate_ro*/);
+  TRACE(trace_gdb, ("sim_write(mem=0x%lx, buf=0x%lx, length=%d) = %d\n",
+                   (long)mem, (long)buf, length, result));
+  return result;
 }
 
-
-int
-sim_write (SIM_ADDR mem, unsigned char *buf, int length)
+void
+sim_info (SIM_DESC sd, int verbose)
 {
-  return psim_write_memory(simulator, nr_cpus, buf, mem, length,
-                          raw_transfer, 1/*violate_ro*/);
+  TRACE(trace_gdb, ("sim_info(verbose=%d) called\n", verbose));
+  psim_print_info (simulator, verbose);
 }
 
 
-void
-sim_fetch_register (int regno, unsigned char *buf)
+SIM_RC
+sim_create_inferior (SIM_DESC sd,
+                    struct bfd *abfd,
+                    char * const *argv,
+                    char * const *envp)
 {
-  if (simulator == NULL) {
-    return;
-  }
+  unsigned_word entry_point;
+  TRACE(trace_gdb, ("sim_create_inferior(start_address=0x%x, ...)\n",
+                   entry_point));
 
-  psim_read_register(simulator, nr_cpus, buf, register_names[regno],
-                    raw_transfer);
+  if (simulator == NULL)
+    error ("No program loaded");
+
+  if (abfd != NULL)
+    entry_point = bfd_get_start_address (abfd);
+  else
+    entry_point = 0xfff00000; /* ??? */
+
+  psim_init(simulator);
+  psim_stack(simulator, argv, envp);
+
+  ASSERT (psim_write_register(simulator, -1 /* all start at same PC */,
+                             &entry_point, "pc", cooked_transfer) > 0);
+  return SIM_RC_OK;
 }
 
 
 void
-sim_store_register (int regno, unsigned char *buf)
+sim_stop_reason (SIM_DESC sd, enum sim_stop *reason, int *sigrc)
 {
-  if (simulator == NULL)
-    return;
+  psim_status status = psim_get_status(simulator);
 
-  psim_write_register(simulator, nr_cpus, buf, register_names[regno],
-                     raw_transfer);
+  switch (status.reason) {
+  case was_continuing:
+    *reason = sim_stopped;
+    if (status.signal == 0)
+      *sigrc = GDB_SIGNAL_TRAP;
+    else
+      *sigrc = status.signal;
+    break;
+  case was_trap:
+    *reason = sim_stopped;
+    *sigrc = GDB_SIGNAL_TRAP;
+    break;
+  case was_exited:
+    *reason = sim_exited;
+    *sigrc = status.signal;
+    break;
+  case was_signalled:
+    *reason = sim_signalled;
+    *sigrc = status.signal;
+    break;
+  }
+
+  TRACE(trace_gdb, ("sim_stop_reason(reason=0x%lx(%ld), sigrc=0x%lx(%ld))\n",
+                   (long)reason, (long)*reason, (long)sigrc, (long)*sigrc));
 }
 
 
-void
-sim_info (int verbose)
+
+/* Run (or resume) the program.  */
+
+int
+sim_stop (SIM_DESC sd)
 {
-  TRACE(trace_gdb, ("sim_info(verbose=%d) called\n", verbose));
-  psim_print_info (simulator, verbose);
+  psim_stop (simulator);
+  return 1;
 }
 
-
 void
-sim_create_inferior (SIM_ADDR start_address, char **argv, char **envp)
+sim_resume (SIM_DESC sd, int step, int siggnal)
 {
-  unsigned_word entry_point = start_address;
+  TRACE(trace_gdb, ("sim_resume(step=%d, siggnal=%d)\n",
+                   step, siggnal));
 
-  TRACE(trace_gdb, ("sim_create_inferior(start_address=0x%x, ...)\n",
-                   start_address));
-
-  psim_load(simulator);
-  psim_stack(simulator, argv, envp);
+  if (step)
+    {
+      psim_step (simulator);
+    }
+  else
+    {
+      psim_run (simulator);
+    }
+}
 
-  psim_write_register(simulator, -1 /* all start at same PC */,
-                     &entry_point, "pc", cooked_transfer);
+void
+sim_do_command (SIM_DESC sd, const char *cmd)
+{
+  TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n",
+                   cmd ? cmd : "(null)"));
+  if (cmd != NULL) {
+    char **argv = buildargv(cmd);
+    psim_command(root_device, argv);
+    freeargv(argv);
+  }
 }
 
+char **
+sim_complete_command (SIM_DESC sd, const char *text, const char *word)
+{
+  return NULL;
+}
 
-static volatile int sim_should_run;
+/* Polling, if required */
 
 void
-sim_stop_reason (enum sim_stop *reason, int *sigrc)
+sim_io_poll_quit (void)
 {
-  psim_status status = psim_get_status(simulator);
-
-  switch (CURRENT_ENVIRONMENT) {
-
-  case VIRTUAL_ENVIRONMENT:
-    switch (status.reason) {
-    case was_continuing:
-      *reason = sim_stopped;
-      *sigrc = SIGTRAP;
-      if (sim_should_run) {
-       error("sim_stop_reason() unknown reason for halt\n");
-      }
-      break;
-    case was_trap:
-      *reason = sim_stopped;
-      *sigrc = SIGTRAP;
-      break;
-    case was_exited:
-      *reason = sim_exited;
-      *sigrc = 0;
-      break;
-    case was_signalled:
-      *reason = sim_signalled;
-      *sigrc = status.signal;
-      break;
+  if (callbacks->poll_quit != NULL && poll_quit_count-- < 0)
+    {
+      poll_quit_count = POLL_QUIT_INTERVAL;
+      if (callbacks->poll_quit (callbacks))
+       psim_stop (simulator);
     }
-    break;
+}
+
 
-  case OPERATING_ENVIRONMENT:
-    *reason = sim_stopped;
-    *sigrc = SIGTRAP;
-    break;
 
+/* Map simulator IO operations onto the corresponding GDB I/O
+   functions.
+   
+   NB: Only a limited subset of operations are mapped across.  More
+   advanced operations (such as dup or write) must either be mapped to
+   one of the below calls or handled internally */
+
+int
+sim_io_read_stdin(char *buf,
+                 int sizeof_buf)
+{
+  switch (CURRENT_STDIO) {
+  case DO_USE_STDIO:
+    return callbacks->read_stdin(callbacks, buf, sizeof_buf);
+    break;
+  case DONT_USE_STDIO:
+    return callbacks->read(callbacks, 0, buf, sizeof_buf);
+    break;
   default:
-    error("sim_stop_reason() - unknown environment\n");
-  
+    error("sim_io_read_stdin: unaccounted switch\n");
+    break;
   }
+  return 0;
 }
 
-
-
-/* Run (or resume) the program.  */
-static void
-sim_ctrl_c()
+int
+sim_io_write_stdout(const char *buf,
+                   int sizeof_buf)
 {
-  sim_should_run = 0;
+  switch (CURRENT_STDIO) {
+  case DO_USE_STDIO:
+    return callbacks->write_stdout(callbacks, buf, sizeof_buf);
+    break;
+  case DONT_USE_STDIO:
+    return callbacks->write(callbacks, 1, buf, sizeof_buf);
+    break;
+  default:
+    error("sim_io_write_stdout: unaccounted switch\n");
+    break;
+  }
+  return 0;
 }
 
-void
-sim_resume (int step, int siggnal)
+int
+sim_io_write_stderr(const char *buf,
+                   int sizeof_buf)
 {
-  void (*prev) ();
-  unsigned_word program_counter;
-
-  prev = signal(SIGINT, sim_ctrl_c);
-  sim_should_run = 1;
+  switch (CURRENT_STDIO) {
+  case DO_USE_STDIO:
+    /* NB: I think there should be an explicit write_stderr callback */
+    return callbacks->write(callbacks, 3, buf, sizeof_buf);
+    break;
+  case DONT_USE_STDIO:
+    return callbacks->write(callbacks, 3, buf, sizeof_buf);
+    break;
+  default:
+    error("sim_io_write_stderr: unaccounted switch\n");
+    break;
+  }
+  return 0;
+}
 
-  if (step)
-    psim_step(simulator);
-  else
-    psim_run_until_stop(simulator, &sim_should_run);
 
-  signal(SIGINT, prev);
+void
+sim_io_printf_filtered(const char *fmt,
+                      ...)
+{
+  char message[1024];
+  va_list ap;
+  /* format the message */
+  va_start(ap, fmt);
+  vsprintf(message, fmt, ap);
+  va_end(ap);
+  /* sanity check */
+  if (strlen(message) >= sizeof(message))
+    error("sim_io_printf_filtered: buffer overflow\n");
+  callbacks->printf_filtered(callbacks, "%s", message);
 }
 
 void
-sim_do_command(char *cmd)
+sim_io_flush_stdoutput(void)
 {
-  TRACE(trace_gdb, ("sim_do_commands(cmd=%s) called\n", cmd));
+  switch (CURRENT_STDIO) {
+  case DO_USE_STDIO:
+    callbacks->flush_stdout (callbacks);
+    break;
+  case DONT_USE_STDIO:
+    break;
+  default:
+    error("sim_io_read_stdin: unaccounted switch\n");
+    break;
+  }
 }
 
 void
-sim_set_callbacks (host_callback *callback)
+sim_io_error (SIM_DESC sd, const char *fmt, ...)
 {
-  TRACE(trace_gdb, ("sim_set_callbacks called\n"));
+  va_list ap;
+  va_start(ap, fmt);
+  callbacks->evprintf_filtered (callbacks, fmt, ap);
+  va_end(ap);
+  callbacks->error (callbacks, "");
 }
 
 /****/
@@ -305,11 +392,6 @@ zalloc(long size)
   void *memory = (void*)xmalloc(size);
   if (memory == NULL)
     error("xmalloc failed\n");
-  bzero(memory, size);
+  memset(memory, 0, size);
   return memory;
 }
-
-void zfree(void *data)
-{
-  mfree(NULL, data);
-}
This page took 0.032312 seconds and 4 git commands to generate.