]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - sim/sh/interp.c
sim: formally assume unistd.h always exists (via gnulib)
[thirdparty/binutils-gdb.git] / sim / sh / interp.c
index ce4d6cd760324134915e47ce32619532a899214a..5e0e8c47569fda61afb036a39cc728a48612a0d5 100644 (file)
@@ -25,9 +25,7 @@
 #include <stdio.h>
 #include <errno.h>
 #include <signal.h>
-#ifdef HAVE_UNISTD_H
 #include <unistd.h>
-#endif
 #ifdef HAVE_MMAP
 #include <sys/mman.h>
 # ifndef MAP_FAILED
 #endif
 #include <time.h>
 #include <sys/time.h>
-#ifndef _WIN32
+#ifdef HAVE_UTIME_H
 #include <utime.h>
+#endif
+#ifndef _WIN32
 #include <sys/wait.h>
 #endif
 
 #include "bfd.h"
 #include "sim/callback.h"
 #include "sim/sim.h"
-#include "gdb/sim-sh.h"
+#include "sim/sim-sh.h"
 
 #include "sim-main.h"
 #include "sim-base.h"
 #include "sim-options.h"
 
-#include "targ-vals.h"
+#include "target-newlib-syscall.h"
+
+#include "sh-sim.h"
 
 #include <math.h>
 
@@ -699,7 +701,7 @@ do { \
 #else
 
 #define MA(n) \
-  do { memstalls += ((((long) PC & 3) != 0) ? (n) : ((n) - 1)); } while (0)
+  do { memstalls += ((((uintptr_t) PC & 3) != 0) ? (n) : ((n) - 1)); } while (0)
 
 #define L(x)   thislock = x;
 #define TL(x)  if ((x) == prevlock) stalls++;
@@ -759,7 +761,7 @@ IOMEM (int addr, int write, int value)
 static int
 get_now (void)
 {
-  return time ((long *) 0);
+  return time (NULL);
 }
 
 static int
@@ -886,21 +888,21 @@ trap (SIM_DESC sd, int i, int *regs, unsigned char *insn_ptr,
          {
 
 #if !defined(__GO32__) && !defined(_WIN32)
-         case TARGET_SYS_fork:
+         case TARGET_NEWLIB_SH_SYS_fork:
            regs[0] = fork ();
            break;
 /* This would work only if endianness matched between host and target.
    Besides, it's quite dangerous.  */
 #if 0
-         case TARGET_SYS_execve:
+         case TARGET_NEWLIB_SH_SYS_execve:
            regs[0] = execve (ptr (regs[5]), (char **) ptr (regs[6]), 
                              (char **) ptr (regs[7]));
            break;
-         case TARGET_SYS_execv:
+         case TARGET_NEWLIB_SH_SYS_execv:
            regs[0] = execve (ptr (regs[5]), (char **) ptr (regs[6]), 0);
            break;
 #endif
-         case TARGET_SYS_pipe:
+         case TARGET_NEWLIB_SH_SYS_pipe:
            {
              regs[0] = (BUSERROR (regs[5], maskl)
                         ? -EINVAL
@@ -908,18 +910,18 @@ trap (SIM_DESC sd, int i, int *regs, unsigned char *insn_ptr,
            }
            break;
 
-         case TARGET_SYS_wait:
+         case TARGET_NEWLIB_SH_SYS_wait:
            regs[0] = wait ((int *) ptr (regs[5]));
            break;
 #endif /* !defined(__GO32__) && !defined(_WIN32) */
 
-         case TARGET_SYS_read:
+         case TARGET_NEWLIB_SH_SYS_read:
            strnswap (regs[6], regs[7]);
            regs[0]
              = callback->read (callback, regs[5], ptr (regs[6]), regs[7]);
            strnswap (regs[6], regs[7]);
            break;
-         case TARGET_SYS_write:
+         case TARGET_NEWLIB_SH_SYS_write:
            strnswap (regs[6], regs[7]);
            if (regs[5] == 1)
              regs[0] = (int) callback->write_stdout (callback, 
@@ -929,13 +931,13 @@ trap (SIM_DESC sd, int i, int *regs, unsigned char *insn_ptr,
                                               ptr (regs[6]), regs[7]);
            strnswap (regs[6], regs[7]);
            break;
-         case TARGET_SYS_lseek:
+         case TARGET_NEWLIB_SH_SYS_lseek:
            regs[0] = callback->lseek (callback,regs[5], regs[6], regs[7]);
            break;
-         case TARGET_SYS_close:
+         case TARGET_NEWLIB_SH_SYS_close:
            regs[0] = callback->close (callback,regs[5]);
            break;
-         case TARGET_SYS_open:
+         case TARGET_NEWLIB_SH_SYS_open:
            {
              int len = strswaplen (regs[5]);
              strnswap (regs[5], len);
@@ -943,13 +945,13 @@ trap (SIM_DESC sd, int i, int *regs, unsigned char *insn_ptr,
              strnswap (regs[5], len);
              break;
            }
-         case TARGET_SYS_exit:
+         case TARGET_NEWLIB_SH_SYS_exit:
            /* EXIT - caller can look in r5 to work out the reason */
            raise_exception (SIGQUIT);
            regs[0] = regs[5];
            break;
 
-         case TARGET_SYS_stat: /* added at hmsi */
+         case TARGET_NEWLIB_SH_SYS_stat:       /* added at hmsi */
            /* stat system call */
            {
              struct stat host_stat;
@@ -998,7 +1000,7 @@ trap (SIM_DESC sd, int i, int *regs, unsigned char *insn_ptr,
            break;
 
 #ifndef _WIN32
-         case TARGET_SYS_chown:
+         case TARGET_NEWLIB_SH_SYS_chown:
            {
              int len = strswaplen (regs[5]);
 
@@ -1008,7 +1010,7 @@ trap (SIM_DESC sd, int i, int *regs, unsigned char *insn_ptr,
              break;
            }
 #endif /* _WIN32 */
-         case TARGET_SYS_chmod:
+         case TARGET_NEWLIB_SH_SYS_chmod:
            {
              int len = strswaplen (regs[5]);
 
@@ -1017,43 +1019,48 @@ trap (SIM_DESC sd, int i, int *regs, unsigned char *insn_ptr,
              strnswap (regs[5], len);
              break;
            }
-         case TARGET_SYS_utime:
+         case TARGET_NEWLIB_SH_SYS_utime:
            {
              /* Cast the second argument to void *, to avoid type mismatch
                 if a prototype is present.  */
              int len = strswaplen (regs[5]);
 
              strnswap (regs[5], len);
+#ifdef HAVE_UTIME_H
              regs[0] = utime (ptr (regs[5]), (void *) ptr (regs[6]));
+#else
+             errno = ENOSYS;
+             regs[0] = -1;
+#endif
              strnswap (regs[5], len);
              break;
            }
-         case TARGET_SYS_argc:
+         case TARGET_NEWLIB_SH_SYS_argc:
            regs[0] = countargv (prog_argv);
            break;
-         case TARGET_SYS_argnlen:
+         case TARGET_NEWLIB_SH_SYS_argnlen:
            if (regs[5] < countargv (prog_argv))
              regs[0] = strlen (prog_argv[regs[5]]);
            else
              regs[0] = -1;
            break;
-         case TARGET_SYS_argn:
+         case TARGET_NEWLIB_SH_SYS_argn:
            if (regs[5] < countargv (prog_argv))
              {
                /* Include the termination byte.  */
                int i = strlen (prog_argv[regs[5]]) + 1;
-               regs[0] = sim_write (0, regs[6], (void *) prog_argv[regs[5]], i);
+               regs[0] = sim_write (0, regs[6], prog_argv[regs[5]], i);
              }
            else
              regs[0] = -1;
            break;
-         case TARGET_SYS_time:
+         case TARGET_NEWLIB_SH_SYS_time:
            regs[0] = get_now ();
            break;
-         case TARGET_SYS_ftruncate:
+         case TARGET_NEWLIB_SH_SYS_ftruncate:
            regs[0] = callback->ftruncate (callback, regs[5], regs[6]);
            break;
-         case TARGET_SYS_truncate:
+         case TARGET_NEWLIB_SH_SYS_truncate:
            {
              int len = strswaplen (regs[5]);
              strnswap (regs[5], len);
@@ -1097,74 +1104,51 @@ div1 (int *R, int iRn2, int iRn1/*, int T*/)
   R[iRn1] <<= 1;
   R[iRn1] |= (unsigned long) T;
 
-  switch (old_q)
+  if (!old_q)
     {
-    case 0:
-      switch (M)
+      if (!M)
        {
-       case 0:
          tmp0 = R[iRn1];
          R[iRn1] -= R[iRn2];
          tmp1 = (R[iRn1] > tmp0);
-         switch (Q)
-           {
-           case 0:
-             SET_SR_Q (tmp1);
-             break;
-           case 1:
-             SET_SR_Q ((unsigned char) (tmp1 == 0));
-             break;
-           }
-         break;
-       case 1:
+         if (!Q)
+           SET_SR_Q (tmp1);
+         else
+           SET_SR_Q ((unsigned char) (tmp1 == 0));
+       }
+      else
+       {
          tmp0 = R[iRn1];
          R[iRn1] += R[iRn2];
          tmp1 = (R[iRn1] < tmp0);
-         switch (Q)
-           {
-           case 0:
-             SET_SR_Q ((unsigned char) (tmp1 == 0));
-             break;
-           case 1:
-             SET_SR_Q (tmp1);
-             break;
-           }
-         break;
+         if (!Q)
+           SET_SR_Q ((unsigned char) (tmp1 == 0));
+         else
+           SET_SR_Q (tmp1);
        }
-      break;
-    case 1:
-      switch (M)
+    }
+  else
+    {
+      if (!M)
        {
-       case 0:
          tmp0 = R[iRn1];
          R[iRn1] += R[iRn2];
          tmp1 = (R[iRn1] < tmp0);
-         switch (Q)
-           {
-           case 0:
-             SET_SR_Q (tmp1);
-             break;
-           case 1:
-             SET_SR_Q ((unsigned char) (tmp1 == 0));
-             break;
-           }
-         break;
-       case 1:
+         if (!Q)
+           SET_SR_Q (tmp1);
+         else
+           SET_SR_Q ((unsigned char) (tmp1 == 0));
+       }
+      else
+       {
          tmp0 = R[iRn1];
          R[iRn1] -= R[iRn2];
          tmp1 = (R[iRn1] > tmp0);
-         switch (Q)
-           {
-           case 0:
-             SET_SR_Q ((unsigned char) (tmp1 == 0));
-             break;
-           case 1:
-             SET_SR_Q (tmp1);
-             break;
-           }
-         break;
+         if (!Q)
+           SET_SR_Q ((unsigned char) (tmp1 == 0));
+         else
+           SET_SR_Q (tmp1);
        }
-      break;
     }
   /*T = (Q == M);*/
   SET_SR_T (Q == M);
@@ -1417,7 +1401,7 @@ fsca_s (int in, double (*f) (double))
   lower = result - error;
   frac = frexp (lower, &exp);
   lower = ldexp (ceil (ldexp (frac, 24)), exp - 24);
-  return abs (upper - result) >= abs (lower - result) ? upper : lower;
+  return fabs (upper - result) >= fabs (lower - result) ? upper : lower;
 }
 
 static float
@@ -1508,8 +1492,6 @@ get_loop_bounds (int rs, int re, unsigned char *memory, unsigned char *mem_end,
   return loop;
 }
 
-static void ppi_insn ();
-
 #include "ppi.c"
 
 /* Provide calloc / free versions that use an anonymous mmap.  This can
@@ -1891,30 +1873,32 @@ sim_resume (SIM_DESC sd, int step, int siggnal)
   signal (SIGFPE, prev_fpe);
 }
 
-int
-sim_write (SIM_DESC sd, SIM_ADDR addr, const unsigned char *buffer, int size)
+uint64_t
+sim_write (SIM_DESC sd, uint64_t addr, const void *buffer, uint64_t size)
 {
   int i;
+  const unsigned char *data = buffer;
 
   init_pointers ();
 
   for (i = 0; i < size; i++)
     {
-      saved_state.asregs.memory[(MMASKB & (addr + i)) ^ endianb] = buffer[i];
+      saved_state.asregs.memory[(MMASKB & (addr + i)) ^ endianb] = data[i];
     }
   return size;
 }
 
-int
-sim_read (SIM_DESC sd, SIM_ADDR addr, unsigned char *buffer, int size)
+uint64_t
+sim_read (SIM_DESC sd, uint64_t addr, void *buffer, uint64_t size)
 {
   int i;
+  unsigned char *data = buffer;
 
   init_pointers ();
 
   for (i = 0; i < size; i++)
     {
-      buffer[i] = saved_state.asregs.memory[(MMASKB & (addr + i)) ^ endianb];
+      data[i] = saved_state.asregs.memory[(MMASKB & (addr + i)) ^ endianb];
     }
   return size;
 }
@@ -1929,7 +1913,7 @@ enum {
 };
 
 static int
-sh_reg_store (SIM_CPU *cpu, int rn, unsigned char *memory, int length)
+sh_reg_store (SIM_CPU *cpu, int rn, const void *memory, int length)
 {
   unsigned val;
 
@@ -2102,7 +2086,7 @@ sh_reg_store (SIM_CPU *cpu, int rn, unsigned char *memory, int length)
 }
 
 static int
-sh_reg_fetch (SIM_CPU *cpu, int rn, unsigned char *memory, int length)
+sh_reg_fetch (SIM_CPU *cpu, int rn, void *memory, int length)
 {
   int val;
 
@@ -2361,9 +2345,10 @@ sim_open (SIM_OPEN_KIND kind, host_callback *cb,
 
   /* Set default options before parsing user options.  */
   current_alignment = STRICT_ALIGNMENT;
+  cb->syscall_map = cb_sh_syscall_map;
 
   /* The cpu data is kept in a separately allocated chunk of memory.  */
-  if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
+  if (sim_cpu_alloc_all (sd, 0) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
@@ -2383,10 +2368,7 @@ sim_open (SIM_OPEN_KIND kind, host_callback *cb,
     }
 
   /* 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)
+  if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;