sim: cgen: move cgen_cpu_max_extra_bytes logic into the common code
[binutils-gdb.git] / sim / h8300 / compile.c
index 394167a601fafe719d0855096fc3c596521fa827..fb0e51b2e58d381c26296fae805dff3fca1bed7a 100644 (file)
 
 #include "config.h"
 #include <signal.h>
-#ifdef HAVE_TIME_H
 #include <time.h>
-#endif
-#ifdef HAVE_STDLIB_H
 #include <stdlib.h>
-#endif
 #ifdef HAVE_SYS_PARAM_H
 #include <sys/param.h>
 #endif
 
 int debug;
 
-/* FIXME: Needs to live in header file.
-   This header should also include the things in remote-sim.h.
-   One could move this to remote-sim.h but this function isn't needed
-   by gdb.  */
-static void set_simcache_size (SIM_DESC, int);
+static int memory_size;
 
 #define X(op, size)  (op * 4 + size)
 
@@ -118,18 +110,6 @@ h8_set_vbr (SIM_DESC sd, int val)
   (STATE_CPU (sd, 0)) -> regs[VBR_REGNUM] = val;
 }
 
-static int
-h8_get_cache_top (SIM_DESC sd)
-{
-  return sd -> cache_top;
-}
-
-static void
-h8_set_cache_top (SIM_DESC sd, int val)
-{
-  sd -> cache_top = val;
-}
-
 static int
 h8_get_mask (SIM_DESC sd)
 {
@@ -226,18 +206,6 @@ h8_set_macl (SIM_DESC sd, unsigned int val)
   (STATE_CPU (sd, 0)) -> regs[MACL_REGNUM] = val;
 }
 
-static int
-h8_get_compiles (SIM_DESC sd)
-{
-  return sd -> compiles;
-}
-
-static void
-h8_increment_compiles (SIM_DESC sd)
-{
-  sd -> compiles ++;
-}
-
 static unsigned int *
 h8_get_reg_buf (SIM_DESC sd)
 {
@@ -270,32 +238,6 @@ h8_increment_stats (SIM_DESC sd, int idx)
 }
 #endif /* ADEBUG */
 
-static unsigned short *
-h8_get_cache_idx_buf (SIM_DESC sd)
-{
-  return sd -> cache_idx;
-}
-
-static void
-h8_set_cache_idx_buf (SIM_DESC sd, unsigned short *ptr)
-{
-  sd -> cache_idx = ptr;
-}
-
-static unsigned short
-h8_get_cache_idx (SIM_DESC sd, unsigned int idx)
-{
-  if (idx > sd->memory_size)
-    return (unsigned short) -1;
-  return sd -> cache_idx[idx];
-}
-
-static void
-h8_set_cache_idx (SIM_DESC sd, int idx, unsigned int val)
-{
-  sd -> cache_idx[idx] = (unsigned short) val;
-}
-
 static unsigned char *
 h8_get_memory_buf (SIM_DESC sd)
 {
@@ -311,39 +253,17 @@ h8_set_memory_buf (SIM_DESC sd, unsigned char *ptr)
 static unsigned char
 h8_get_memory (SIM_DESC sd, int idx)
 {
+  ASSERT (idx < memory_size);
   return (STATE_CPU (sd, 0)) -> memory[idx];
 }
 
 static void
 h8_set_memory (SIM_DESC sd, int idx, unsigned int val)
 {
+  ASSERT (idx < memory_size);
   (STATE_CPU (sd, 0)) -> memory[idx] = (unsigned char) val;
 }
 
-static unsigned char *
-h8_get_eightbit_buf (SIM_DESC sd)
-{
-  return (STATE_CPU (sd, 0)) -> eightbit;
-}
-
-static void
-h8_set_eightbit_buf (SIM_DESC sd, unsigned char *ptr)
-{
-  (STATE_CPU (sd, 0)) -> eightbit = ptr;
-}
-
-static unsigned char
-h8_get_eightbit (SIM_DESC sd, int idx)
-{
-  return (STATE_CPU (sd, 0)) -> eightbit[idx];
-}
-
-static void
-h8_set_eightbit (SIM_DESC sd, int idx, unsigned int val)
-{
-  (STATE_CPU (sd, 0)) -> eightbit[idx] = (unsigned char) val;
-}
-
 static unsigned int
 h8_get_delayed_branch (SIM_DESC sd)
 {
@@ -484,8 +404,6 @@ int h8300smode  = 0;
 int h8300_normal_mode  = 0;
 int h8300sxmode = 0;
 
-static int memory_size;
-
 static int
 get_now (void)
 {
@@ -547,7 +465,7 @@ lvalue (SIM_DESC sd, int x, int rn, unsigned int *val)
 }
 
 static int
-cmdline_location()
+cmdline_location(void)
 {
   if (h8300smode && !h8300_normal_mode)
     return 0xffff00L;
@@ -1197,34 +1115,6 @@ decode (SIM_DESC sd, int addr, unsigned char *data, decoded_inst *dst)
   dst->opcode = O (O_ILL, SB);
 }
 
-static void
-compile (SIM_DESC sd, int pc)
-{
-  int idx;
-
-  /* Find the next cache entry to use.  */
-  idx = h8_get_cache_top (sd) + 1;
-  h8_increment_compiles (sd);
-  if (idx >= sd->sim_cache_size)
-    {
-      idx = 1;
-    }
-  h8_set_cache_top (sd, idx);
-
-  /* Throw away its old meaning.  */
-  h8_set_cache_idx (sd, sd->sim_cache[idx].oldpc, 0);
-
-  /* Set to new address.  */
-  sd->sim_cache[idx].oldpc = pc;
-
-  /* Fill in instruction info.  */
-  decode (sd, pc, h8_get_memory_buf (sd) + pc, sd->sim_cache + idx);
-
-  /* Point to new cache entry.  */
-  h8_set_cache_idx (sd, pc, idx);
-}
-
-
 static unsigned char  *breg[32];
 static unsigned short *wreg[16];
 
@@ -1239,41 +1129,31 @@ static unsigned short *wreg[16];
   ((X) < memory_size \
    ? ((h8_get_memory (sd, (X)+0) << 24) | (h8_get_memory (sd, (X)+1) << 16)  \
     | (h8_get_memory (sd, (X)+2) <<  8) | (h8_get_memory (sd, (X)+3) <<  0)) \
-   : ((h8_get_eightbit (sd, ((X)+0) & 0xff) << 24) \
-    | (h8_get_eightbit (sd, ((X)+1) & 0xff) << 16) \
-    | (h8_get_eightbit (sd, ((X)+2) & 0xff) <<  8) \
-    | (h8_get_eightbit (sd, ((X)+3) & 0xff) <<  0)))
+   : 0)
 
 #define GET_MEMORY_W(X) \
   ((X) < memory_size \
-   ? ((h8_get_memory   (sd, (X)+0) << 8) \
-    | (h8_get_memory   (sd, (X)+1) << 0)) \
-   : ((h8_get_eightbit (sd, ((X)+0) & 0xff) << 8) \
-    | (h8_get_eightbit (sd, ((X)+1) & 0xff) << 0)))
-
+   ? ((h8_get_memory (sd, (X)+0) << 8) | (h8_get_memory (sd, (X)+1) << 0)) \
+   : 0)
 
 #define GET_MEMORY_B(X) \
-  ((X) < memory_size ? (h8_get_memory   (sd, (X))) \
-                     : (h8_get_eightbit (sd, (X) & 0xff)))
+  ((X) < memory_size ? h8_get_memory (sd, (X)) : 0)
 
 #define SET_MEMORY_L(X, Y)  \
 {  register unsigned char *_p; register int __y = (Y); \
-   _p = ((X) < memory_size ? h8_get_memory_buf   (sd) +  (X) : \
-                             h8_get_eightbit_buf (sd) + ((X) & 0xff)); \
+   _p = ((X) < memory_size ? h8_get_memory_buf (sd) + (X) : 0); \
    _p[0] = __y >> 24; _p[1] = __y >> 16; \
    _p[2] = __y >>  8; _p[3] = __y >>  0; \
 }
 
 #define SET_MEMORY_W(X, Y) \
 {  register unsigned char *_p; register int __y = (Y); \
-   _p = ((X) < memory_size ? h8_get_memory_buf   (sd) +  (X) : \
-                             h8_get_eightbit_buf (sd) + ((X) & 0xff)); \
+   _p = ((X) < memory_size ? h8_get_memory_buf (sd) + (X) : 0); \
    _p[0] = __y >> 8; _p[1] = __y; \
 }
 
 #define SET_MEMORY_B(X, Y) \
-  ((X) < memory_size ? (h8_set_memory   (sd, (X), (Y))) \
-                     : (h8_set_eightbit (sd, (X) & 0xff, (Y))))
+  ((X) < memory_size ? h8_set_memory (sd, (X), (Y)) : 0)
 
 /* Simulate a memory fetch.
    Return 0 for success, -1 for failure.
@@ -1744,22 +1624,16 @@ init_pointers (SIM_DESC sd)
          sim_io_printf
            (sd,
             "init_pointers: bad memory size %d, defaulting to %d.\n", 
-            memory_size, memory_size = H8300S_MSIZE);
+            memory_size, H8300S_MSIZE);
+         memory_size = H8300S_MSIZE;
        }
 
       if (h8_get_memory_buf (sd))
        free (h8_get_memory_buf (sd));
-      if (h8_get_cache_idx_buf (sd))
-       free (h8_get_cache_idx_buf (sd));
-      if (h8_get_eightbit_buf (sd))
-       free (h8_get_eightbit_buf (sd));
 
       h8_set_memory_buf (sd, (unsigned char *) 
                         calloc (sizeof (char), memory_size));
-      h8_set_cache_idx_buf (sd, (unsigned short *) 
-                           calloc (sizeof (short), memory_size));
       sd->memory_size = memory_size;
-      h8_set_eightbit_buf (sd, (unsigned char *) calloc (sizeof (char), 256));
 
       h8_set_mask (sd, memory_size - 1);
 
@@ -1808,10 +1682,6 @@ init_pointers (SIM_DESC sd)
        }
 
       init_pointers_needed = 0;
-
-      /* Initialize the seg registers.  */
-      if (!sd->sim_cache)
-       set_simcache_size (sd, CSIZE);
     }
 }
 
@@ -1875,16 +1745,10 @@ step_once (SIM_DESC sd, SIM_CPU *cpu)
     h8_set_mask (sd, 0xffff);
   do
     {
-      unsigned short cidx;
-      decoded_inst *code;
-
-    top:
-      cidx = h8_get_cache_idx (sd, pc);
-      if (cidx == (unsigned short) -1 ||
-         cidx >= sd->sim_cache_size)
-       goto illegal;
-         
-      code = sd->sim_cache + cidx;
+      decoded_inst _code, *code = &_code;
+      memset (code, 0, sizeof (*code));
+      decode (sd, pc, h8_get_memory_buf (sd) + pc, code);
+      code->oldpc = pc;
 
 #if ADEBUG
       if (debug)
@@ -1903,15 +1767,6 @@ step_once (SIM_DESC sd, SIM_CPU *cpu)
 
       switch (code->opcode)
        {
-       case 0:
-         /*
-          * This opcode is a fake for when we get to an
-          * instruction which hasnt been compiled
-          */
-         compile (sd, pc);
-         goto top;
-         break;
-
        case O (O_MOVAB, SL):
        case O (O_MOVAW, SL):
        case O (O_MOVAL, SL):
@@ -2275,25 +2130,12 @@ step_once (SIM_DESC sd, SIM_CPU *cpu)
                                    ? h8_get_reg (sd, R4_REGNUM) & 0xffff
                                    : h8_get_reg (sd, R4_REGNUM) & 0xff);
 
-             _src = (h8_get_reg (sd, R5_REGNUM) < memory_size
-                     ? h8_get_memory_buf   (sd) + h8_get_reg (sd, R5_REGNUM)
-                     : h8_get_eightbit_buf (sd) + 
-                      (h8_get_reg (sd, R5_REGNUM) & 0xff));
+             _src = h8_get_memory_buf (sd) + h8_get_reg (sd, R5_REGNUM);
              if ((_src + count) >= (h8_get_memory_buf (sd) + memory_size))
-               {
-                 if ((_src + count) >= (h8_get_eightbit_buf (sd) + 0x100))
-                   goto illegal;
-               }
-             _dst = (h8_get_reg (sd, R6_REGNUM) < memory_size
-                     ? h8_get_memory_buf   (sd) + h8_get_reg (sd, R6_REGNUM)
-                     : h8_get_eightbit_buf (sd) + 
-                      (h8_get_reg (sd, R6_REGNUM) & 0xff));
-
+               goto illegal;
+             _dst = h8_get_memory_buf (sd) + h8_get_reg (sd, R6_REGNUM);
              if ((_dst + count) >= (h8_get_memory_buf (sd) + memory_size))
-               {
-                 if ((_dst + count) >= (h8_get_eightbit_buf (sd) + 0x100))
-                   goto illegal;
-               }
+               goto illegal;
              memcpy (_dst, _src, count);
 
              h8_set_reg (sd, R5_REGNUM, h8_get_reg (sd, R5_REGNUM) + count);
@@ -4553,14 +4395,11 @@ sim_write (SIM_DESC sd, SIM_ADDR addr, const unsigned char *buffer, int size)
       if (addr < memory_size)
        {
          h8_set_memory    (sd, addr + i, buffer[i]);
-         h8_set_cache_idx (sd, addr + i,  0);
        }
       else
-       {
-         h8_set_eightbit (sd, (addr + i) & 0xff, buffer[i]);
-       }
+       break;
     }
-  return size;
+  return i;
 }
 
 int
@@ -4569,10 +4408,10 @@ sim_read (SIM_DESC sd, SIM_ADDR addr, unsigned char *buffer, int size)
   init_pointers (sd);
   if (addr < 0)
     return 0;
-  if (addr < memory_size)
+  if (addr + size < memory_size)
     memcpy (buffer, h8_get_memory_buf (sd) + addr, size);
   else
-    memcpy (buffer, h8_get_eightbit_buf (sd) + (addr & 0xff), size);
+    return 0;
   return size;
 }
 
@@ -4682,19 +4521,6 @@ h8300_reg_fetch (SIM_CPU *cpu, int rn, unsigned char *buf, int length)
     }
 }
 
-static void
-set_simcache_size (SIM_DESC sd, int n)
-{
-  if (sd->sim_cache)
-    free (sd->sim_cache);
-  if (n < 2)
-    n = 2;
-  sd->sim_cache = (decoded_inst *) malloc (sizeof (decoded_inst) * n);
-  memset (sd->sim_cache, 0, sizeof (decoded_inst) * n);
-  sd->sim_cache_size = n;
-}
-
-
 void
 sim_info (SIM_DESC sd, int verbose)
 {
@@ -4707,8 +4533,6 @@ sim_info (SIM_DESC sd, int verbose)
   sim_io_printf (sd, "#virtual time taken     %10.4f\n", virttime);
   if (timetaken != 0.0)
     sim_io_printf (sd, "#simulation ratio       %10.4f\n", virttime / timetaken);
-  sim_io_printf (sd, "#compiles               %10d\n", h8_get_compiles (sd));
-  sim_io_printf (sd, "#cache size             %10d\n", sd->sim_cache_size);
 
 #ifdef ADEBUG
   /* This to be conditional on `what' (aka `verbose'),
@@ -4826,7 +4650,7 @@ SIM_DESC
 sim_open (SIM_OPEN_KIND kind, 
          struct host_callback_struct *callback, 
          struct bfd *abfd, 
-         char **argv)
+         char * const *argv)
 {
   int i;
   SIM_DESC sd;
@@ -4835,7 +4659,7 @@ sim_open (SIM_OPEN_KIND kind,
   sd = sim_state_alloc (kind, callback);
 
   /* 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 (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
     {
       free_state (sd);
       return 0;
@@ -4962,17 +4786,10 @@ sim_load (SIM_DESC sd, const char *prog, bfd *abfd, int from_tty)
 
   if (h8_get_memory_buf (sd))
     free (h8_get_memory_buf (sd));
-  if (h8_get_cache_idx_buf (sd))
-    free (h8_get_cache_idx_buf (sd));
-  if (h8_get_eightbit_buf (sd))
-    free (h8_get_eightbit_buf (sd));
 
   h8_set_memory_buf (sd, (unsigned char *) 
                     calloc (sizeof (char), memory_size));
-  h8_set_cache_idx_buf (sd, (unsigned short *) 
-                       calloc (sizeof (short), memory_size));
   sd->memory_size = memory_size;
-  h8_set_eightbit_buf (sd, (unsigned char *) calloc (sizeof (char), 256));
 
   /* `msize' must be a power of two.  */
   if ((memory_size & (memory_size - 1)) != 0)
@@ -5000,7 +4817,8 @@ sim_load (SIM_DESC sd, const char *prog, bfd *abfd, int from_tty)
 }
 
 SIM_RC
-sim_create_inferior (SIM_DESC sd, struct bfd *abfd, char **argv, char **env)
+sim_create_inferior (SIM_DESC sd, struct bfd *abfd,
+                    char * const *argv, char * const *env)
 {
   int i = 0;
   int len_arg = 0;