* config/tc-sparc.c (md_show_usage): Add missing backslash at end
[deliverable/binutils-gdb.git] / gdb / sh-tdep.c
index 7a405d29057f5cbc4f5e263ba901a0bd88ef713f..4a3eb2e0d46c6482534d0bb4ff63f476822d0d8b 100644 (file)
@@ -31,6 +31,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 #include "gdbcore.h"
 #include "value.h"
 #include "dis-asm.h"
+#include "inferior.h"          /* for BEFORE_TEXT_END etc. */
 
 extern int remote_write_size;  /* in remote.c */
 
@@ -50,39 +51,39 @@ char *tmp_sh_processor_type;
 char *sh_generic_reg_names[] = REGISTER_NAMES;
 
 char *sh_reg_names[] = {
-  "r0", "r1", "r2",  "r3",  "r4",  "r5",   "r6",  "r7",
-  "r8", "r9", "r10", "r11", "r12", "r13",  "r14", "r15",
-  "pc", "pr", "gbr", "vbr", "mach","macl", "sr",
-  "", "",
-  "", "", "", "", "", "", "", "",
-  "", "", "", "", "", "", "", "",
-  "","",
-  "", "", "", "", "", "", "", "",
-  "", "", "", "", "", "", "", ""
+  "r0",   "r1",   "r2",   "r3",   "r4",   "r5",   "r6",   "r7",
+  "r8",   "r9",   "r10",  "r11",  "r12",  "r13",  "r14",  "r15",
+  "pc",   "pr",   "gbr",  "vbr",  "mach", "macl", "sr",
+  "",     "",
+  "",     "",     "",     "",     "",     "",     "",     "",
+  "",     "",     "",     "",     "",     "",     "",     "",
+  "",     "",
+  "",     "",     "",     "",     "",     "",     "",     "",
+  "",     "",     "",     "",     "",     "",     "",     "",
 };
 
 char *sh3_reg_names[] = {
-  "r0",  "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
-  "r8",  "r9",  "r10", "r11", "r12", "r13", "r14", "r15",
-  "pc",  "pr",  "gbr", "vbr", "mach","macl","sr",
-  "ssr", "spc",
+  "r0",   "r1",   "r2",   "r3",   "r4",   "r5",   "r6",   "r7",
+  "r8",   "r9",   "r10",  "r11",  "r12",  "r13",  "r14",  "r15",
+  "pc",   "pr",   "gbr",  "vbr",  "mach", "macl", "sr",
+  "",     "",
+  "",     "",     "",     "",     "",     "",     "",     "",
+  "",     "",     "",     "",     "",     "",     "",     "",
+  "ssr",  "spc",
   "r0b0", "r1b0", "r2b0", "r3b0", "r4b0", "r5b0", "r6b0", "r7b0",
-  "r0b1", "r1b1", "r2b1", "r3b1", "r4b1", "r5b1", "r6b1", "r7b1",
-  "", "",
-  "", "", "", "", "", "", "", "",
-  "", "", "", "", "", "", "", ""
+  "r0b1", "r1b1", "r2b1", "r3b1", "r4b1", "r5b1", "r6b1", "r7b1"
 };
 
 char *sh3e_reg_names[] = {
-  "r0",  "r1",  "r2",  "r3",  "r4",  "r5",  "r6",  "r7",
-  "r8",  "r9",  "r10", "r11", "r12", "r13", "r14", "r15",
-  "pc",  "pr",  "gbr", "vbr", "mach","macl","sr",
-  "ssr", "spc",
+  "r0",   "r1",   "r2",   "r3",   "r4",   "r5",   "r6",   "r7",
+  "r8",   "r9",   "r10",  "r11",  "r12",  "r13",  "r14",  "r15",
+  "pc",   "pr",   "gbr",  "vbr",  "mach", "macl", "sr",
+  "fpul", "fpscr",
+  "fr0",  "fr1",  "fr2",  "fr3",  "fr4",  "fr5",  "fr6",  "fr7",
+  "fr8",  "fr9",  "fr10", "fr11", "fr12", "fr13", "fr14", "fr15",
+  "ssr",  "spc",
   "r0b0", "r1b0", "r2b0", "r3b0", "r4b0", "r5b0", "r6b0", "r7b0",
   "r0b1", "r1b1", "r2b1", "r3b1", "r4b1", "r5b1", "r6b1", "r7b1",
-  "fpul", "fpscr",
-  "fr0", "fr1", "fr2", "fr3", "fr4", "fr5", "fr6", "fr7",
-  "fr8", "fr9", "fr10","fr11","fr12","fr13","fr14","fr15"
 };
 
 struct {
@@ -159,12 +160,42 @@ CORE_ADDR
 sh_frame_chain (frame)
      struct frame_info *frame;
 {
+  if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))
+    return frame->frame;       /* dummy frame same as caller's frame */
   if (!inside_entry_file (frame->pc))
     return read_memory_integer (FRAME_FP (frame) + frame->f_offset, 4);
   else
     return 0;
 }
 
+/* Find REGNUM on the stack.  Otherwise, it's in an active register.  One thing
+   we might want to do here is to check REGNUM against the clobber mask, and
+   somehow flag it as invalid if it isn't saved on the stack somewhere.  This
+   would provide a graceful failure mode when trying to get the value of
+   caller-saves registers for an inner frame.  */
+
+CORE_ADDR
+sh_find_callers_reg (fi, regnum)
+     struct frame_info *fi;
+     int regnum;
+{
+  struct frame_saved_regs fsr;
+
+  for (; fi; fi = fi->next)
+    if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
+      /* When the caller requests PR from the dummy frame, we return PC because
+        that's where the previous routine appears to have done a call from. */
+      return generic_read_register_dummy (fi->pc, fi->frame, regnum);
+    else 
+      {
+       FRAME_FIND_SAVED_REGS(fi, fsr);
+       if (fsr.regs[regnum] != 0)
+         return read_memory_integer (fsr.regs[regnum], 
+                                     REGISTER_RAW_SIZE(regnum));
+      }
+  return read_register (regnum);
+}
+
 /* Put here the code to store, into a struct frame_saved_regs, the
    addresses of the saved registers of frame described by FRAME_INFO.
    This includes special registers such as pc and fp saved in special
@@ -172,7 +203,7 @@ sh_frame_chain (frame)
    return for it IS the sp for the next frame. */
 
 void
-frame_find_saved_regs (fi, fsr)
+sh_frame_find_saved_regs (fi, fsr)
      struct frame_info *fi;
      struct frame_saved_regs *fsr;
 {
@@ -184,6 +215,16 @@ frame_find_saved_regs (fi, fsr)
   int opc;
   int insn;
   int r3_val = 0;
+  char * dummy_regs = generic_find_dummy_frame (fi->pc, fi->frame);
+
+  if (dummy_regs)
+    {
+      /* DANGER!  This is ONLY going to work if the char buffer format of
+        the saved registers is byte-for-byte identical to the 
+        CORE_ADDR regs[NUM_REGS] format used by struct frame_saved_regs! */
+      memcpy (&fsr->regs, dummy_regs, sizeof(fsr));
+      return;
+    }
 
   opc = pc = get_pc_function_start (fi->pc);
 
@@ -276,57 +317,250 @@ frame_find_saved_regs (fi, fsr)
   fi->f_offset = depth - where[FP_REGNUM] - 4;
   /* Work out the return pc - either from the saved pr or the pr
      value */
-
-  if (fsr->regs[PR_REGNUM])
-    fi->return_pc = read_memory_integer (fsr->regs[PR_REGNUM], 4);
-  else
-    fi->return_pc = read_register (PR_REGNUM);
 }
 
 /* initialize the extra info saved in a FRAME */
 
 void
-init_extra_frame_info (fromleaf, fi)
+sh_init_extra_frame_info (fromleaf, fi)
      int fromleaf;
      struct frame_info *fi;
 {
-  struct frame_saved_regs dummy;
+  struct frame_saved_regs fsr;
 
   if (fi->next)
-    fi->pc = fi->next->return_pc;
+    fi->pc = FRAME_SAVED_PC (fi->next);
 
-  frame_find_saved_regs (fi, &dummy);
+  if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
+    {
+      /* We need to setup fi->frame here because run_stack_dummy gets it wrong
+        by assuming it's always FP.  */
+      fi->frame     = generic_read_register_dummy (fi->pc, fi->frame, 
+                                                  SP_REGNUM);
+      fi->return_pc = generic_read_register_dummy (fi->pc, fi->frame, 
+                                                  PC_REGNUM);
+      fi->f_offset = -(CALL_DUMMY_LENGTH + 4);
+      fi->leaf_function = 0;
+      return;
+    }
+  else
+    {
+      FRAME_FIND_SAVED_REGS (fi, fsr);
+      fi->return_pc = sh_find_callers_reg (fi, PR_REGNUM);
+    }
 }
 
-
 /* Discard from the stack the innermost frame,
    restoring all saved registers.  */
 
 void
-pop_frame ()
+sh_pop_frame ()
 {
   register struct frame_info *frame = get_current_frame ();
   register CORE_ADDR fp;
   register int regnum;
   struct frame_saved_regs fsr;
 
-  fp = FRAME_FP (frame);
-  get_frame_saved_regs (frame, &fsr);
+  if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))
+    generic_pop_dummy_frame ();
+  else
+  {
+    fp = FRAME_FP (frame);
+    get_frame_saved_regs (frame, &fsr);
 
-  /* Copy regs from where they were saved in the frame */
-  for (regnum = 0; regnum < NUM_REGS; regnum++)
-    {
+    /* Copy regs from where they were saved in the frame */
+    for (regnum = 0; regnum < NUM_REGS; regnum++)
       if (fsr.regs[regnum])
+       write_register (regnum, read_memory_integer (fsr.regs[regnum], 4));
+
+    write_register (PC_REGNUM, frame->return_pc);
+    write_register (SP_REGNUM, fp + 4);
+  }
+  flush_cached_frames ();
+}
+
+/* Function: push_arguments
+   Setup the function arguments for calling a function in the inferior.
+
+   On the Hitachi SH architecture, there are four registers (R4 to R7)
+   which are dedicated for passing function arguments.  Up to the first
+   four arguments (depending on size) may go into these registers.
+   The rest go on the stack.
+
+   Arguments that are smaller than 4 bytes will still take up a whole
+   register or a whole 32-bit word on the stack, and will be 
+   right-justified in the register or the stack word.  This includes
+   chars, shorts, and small aggregate types.
+
+   Arguments that are larger than 4 bytes may be split between two or 
+   more registers.  If there are not enough registers free, an argument
+   may be passed partly in a register (or registers), and partly on the
+   stack.  This includes doubles, long longs, and larger aggregates. 
+   As far as I know, there is no upper limit to the size of aggregates 
+   that will be passed in this way; in other words, the convention of 
+   passing a pointer to a large aggregate instead of a copy is not used.
+
+   An exceptional case exists for struct arguments (and possibly other
+   aggregates such as arrays) if the size is larger than 4 bytes but 
+   not a multiple of 4 bytes.  In this case the argument is never split 
+   between the registers and the stack, but instead is copied in its
+   entirety onto the stack, AND also copied into as many registers as 
+   there is room for.  In other words, space in registers permitting, 
+   two copies of the same argument are passed in.  As far as I can tell,
+   only the one on the stack is used, although that may be a function 
+   of the level of compiler optimization.  I suspect this is a compiler
+   bug.  Arguments of these odd sizes are left-justified within the 
+   word (as opposed to arguments smaller than 4 bytes, which are 
+   right-justified).
+
+   If the function is to return an aggregate type such as a struct, it 
+   is either returned in the normal return value register R0 (if its 
+   size is no greater than one byte), or else the caller must allocate
+   space into which the callee will copy the return value (if the size
+   is greater than one byte).  In this case, a pointer to the return 
+   value location is passed into the callee in register R2, which does 
+   not displace any of the other arguments passed in via registers R4
+   to R7.   */
+
+CORE_ADDR
+sh_push_arguments (nargs, args, sp, struct_return, struct_addr)
+     int nargs;
+     value_ptr *args;
+     CORE_ADDR sp;
+     unsigned char struct_return;
+     CORE_ADDR struct_addr;
+{
+  int stack_offset, stack_alloc;
+  int argreg;
+  int argnum;
+  struct type *type;
+  CORE_ADDR regval;
+  char *val;
+  char valbuf[4];
+  int len;
+  int odd_sized_struct;
+
+  /* first force sp to a 4-byte alignment */
+  sp = sp & ~3;
+
+  /* The "struct return pointer" pseudo-argument has its own dedicated 
+     register */
+  if (struct_return)
+      write_register (STRUCT_RETURN_REGNUM, struct_addr);
+
+  /* Now make sure there's space on the stack */
+  for (argnum = 0, stack_alloc = 0;
+       argnum < nargs; argnum++)
+    stack_alloc += ((TYPE_LENGTH(VALUE_TYPE(args[argnum])) + 3) & ~3);
+  sp -= stack_alloc;    /* make room on stack for args */
+
+
+  /* Now load as many as possible of the first arguments into
+     registers, and push the rest onto the stack.  There are 16 bytes
+     in four registers available.  Loop thru args from first to last.  */
+
+  argreg = ARG0_REGNUM;
+  for (argnum = 0, stack_offset = 0; argnum < nargs; argnum++)
+    {
+      type = VALUE_TYPE (args[argnum]);
+      len  = TYPE_LENGTH (type);
+      memset(valbuf, 0, sizeof(valbuf));
+      if (len < 4)
+        { /* value gets right-justified in the register or stack word */
+          memcpy(valbuf + (4 - len), 
+                (char *) VALUE_CONTENTS (args[argnum]), len);
+          val = valbuf;
+        }
+      else
+        val = (char *) VALUE_CONTENTS (args[argnum]);
+
+      if (len > 4 && (len & 3) != 0)
+       odd_sized_struct = 1;           /* such structs go entirely on stack */
+      else 
+       odd_sized_struct = 0;
+      while (len > 0)
        {
-         write_register (regnum, read_memory_integer (fsr.regs[regnum], 4));
+         if (argreg > ARGLAST_REGNUM || odd_sized_struct)
+           {                           /* must go on the stack */
+             write_memory (sp + stack_offset, val, 4);
+             stack_offset += 4;
+           }
+         /* NOTE WELL!!!!!  This is not an "else if" clause!!!
+            That's because some *&^%$ things get passed on the stack
+            AND in the registers!   */
+         if (argreg <= ARGLAST_REGNUM)
+           {                           /* there's room in a register */
+             regval = extract_address (val, REGISTER_RAW_SIZE(argreg));
+             write_register (argreg++, regval);
+           }
+         /* Store the value 4 bytes at a time.  This means that things
+            larger than 4 bytes may go partly in registers and partly
+            on the stack.  */
+         len -= REGISTER_RAW_SIZE(argreg);
+         val += REGISTER_RAW_SIZE(argreg);
        }
     }
+  return sp;
+}
 
-  write_register (PC_REGNUM, frame->return_pc);
-  write_register (SP_REGNUM, fp + 4);
-  flush_cached_frames ();
+/* Function: push_return_address (pc)
+   Set up the return address for the inferior function call.
+   Needed for targets where we don't actually execute a JSR/BSR instruction */
+
+CORE_ADDR
+sh_push_return_address (pc, sp)
+     CORE_ADDR pc;
+     CORE_ADDR sp;
+{
+  write_register (PR_REGNUM, CALL_DUMMY_ADDRESS ());
+  return sp;
+}
+
+/* Function: fix_call_dummy
+   Poke the callee function's address into the destination part of 
+   the CALL_DUMMY.  The address is actually stored in a data word 
+   following the actualy CALL_DUMMY instructions, which will load
+   it into a register using PC-relative addressing.  This function
+   expects the CALL_DUMMY to look like this:
+
+        mov.w @(2,PC), R8
+        jsr   @R8
+        nop
+        trap
+        <destination>
+  */
+
+int
+sh_fix_call_dummy (dummy, pc, fun, nargs, args, type, gcc_p)
+     char *dummy;
+     CORE_ADDR pc;
+     CORE_ADDR fun;
+     int nargs;
+     value_ptr *args;
+     struct type *type;
+     int gcc_p;
+{
+  *(unsigned long *) (dummy + 8) = fun;
+}
+
+/* Function: get_saved_register
+   Just call the generic_get_saved_register function.  */
+
+void
+get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
+     char *raw_buffer;
+     int *optimized;
+     CORE_ADDR *addrp;
+     struct frame_info *frame;
+     int regnum;
+     enum lval_type *lval;
+{
+  generic_get_saved_register (raw_buffer, optimized, addrp, 
+                             frame, regnum, lval);
 }
 
+
 /* Command to set the processor type.  */
 
 void
@@ -361,6 +595,8 @@ sh_set_processor_type_command (args, from_tty)
     }
 }
 
+/* This is a dummy not actually run.  */
+
 static void
 sh_show_processor_type_command (args, from_tty)
      char *args;
@@ -398,7 +634,7 @@ sh_set_processor_type (str)
 /* Print the registers in a form similar to the E7000 */
 
 static void
-show_regs (args, from_tty)
+sh_show_regs (args, from_tty)
      char *args;
      int from_tty;
 {
@@ -428,7 +664,27 @@ show_regs (args, from_tty)
                   read_register (14),
                   read_register (15));
 }
-\f
+
+/* Function: extract_return_value
+   Find a function's return value in the appropriate registers (in regbuf),
+   and copy it into valbuf.  */
+
+void
+sh_extract_return_value (type, regbuf, valbuf)
+     struct type *type;
+     void *regbuf;
+     void *valbuf;
+{
+  int len = TYPE_LENGTH(type);
+
+  if (len <= 4)
+    memcpy (valbuf, ((char *) regbuf) + 4 - len, len);
+  else if (len <= 8)
+    memcpy (valbuf, ((char *) regbuf) + 8 - len, len);
+  else
+    error ("bad size for return value");
+}
+
 void
 _initialize_sh_tdep ()
 {
@@ -449,9 +705,10 @@ Set this to be able to access processor-type-specific registers.\n\
   tmp_sh_processor_type = strsave (DEFAULT_SH_TYPE);
   sh_set_processor_type_command (strsave (DEFAULT_SH_TYPE), 0);
 
-  add_com ("regs", class_vars, show_regs, "Print all registers");
+  add_com ("regs", class_vars, sh_show_regs, "Print all registers");
 
   /* Reduce the remote write size because some CMONs can't take
     more than 400 bytes in a packet.  300 seems like a safe bet.  */
   remote_write_size = 300;
 }
+
This page took 0.028927 seconds and 4 git commands to generate.