gdb/sh-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for Renesas Super-H, for GDB.

  2.    Copyright (C) 1993-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. /* Contributed by Steve Chamberlain
  15.    sac@cygnus.com.  */

  16. #include "defs.h"
  17. #include "frame.h"
  18. #include "frame-base.h"
  19. #include "frame-unwind.h"
  20. #include "dwarf2-frame.h"
  21. #include "symtab.h"
  22. #include "gdbtypes.h"
  23. #include "gdbcmd.h"
  24. #include "gdbcore.h"
  25. #include "value.h"
  26. #include "dis-asm.h"
  27. #include "inferior.h"
  28. #include "arch-utils.h"
  29. #include "floatformat.h"
  30. #include "regcache.h"
  31. #include "doublest.h"
  32. #include "osabi.h"
  33. #include "reggroups.h"
  34. #include "regset.h"
  35. #include "objfiles.h"

  36. #include "sh-tdep.h"
  37. #include "sh64-tdep.h"

  38. #include "elf-bfd.h"
  39. #include "solib-svr4.h"

  40. /* sh flags */
  41. #include "elf/sh.h"
  42. #include "dwarf2.h"
  43. /* registers numbers shared with the simulator.  */
  44. #include "gdb/sim-sh.h"

  45. /* List of "set sh ..." and "show sh ..." commands.  */
  46. static struct cmd_list_element *setshcmdlist = NULL;
  47. static struct cmd_list_element *showshcmdlist = NULL;

  48. static const char sh_cc_gcc[] = "gcc";
  49. static const char sh_cc_renesas[] = "renesas";
  50. static const char *const sh_cc_enum[] = {
  51.   sh_cc_gcc,
  52.   sh_cc_renesas,
  53.   NULL
  54. };

  55. static const char *sh_active_calling_convention = sh_cc_gcc;

  56. #define SH_NUM_REGS 67

  57. struct sh_frame_cache
  58. {
  59.   /* Base address.  */
  60.   CORE_ADDR base;
  61.   LONGEST sp_offset;
  62.   CORE_ADDR pc;

  63.   /* Flag showing that a frame has been created in the prologue code.  */
  64.   int uses_fp;

  65.   /* Saved registers.  */
  66.   CORE_ADDR saved_regs[SH_NUM_REGS];
  67.   CORE_ADDR saved_sp;
  68. };

  69. static int
  70. sh_is_renesas_calling_convention (struct type *func_type)
  71. {
  72.   int val = 0;

  73.   if (func_type)
  74.     {
  75.       func_type = check_typedef (func_type);

  76.       if (TYPE_CODE (func_type) == TYPE_CODE_PTR)
  77.         func_type = check_typedef (TYPE_TARGET_TYPE (func_type));

  78.       if (TYPE_CODE (func_type) == TYPE_CODE_FUNC
  79.           && TYPE_CALLING_CONVENTION (func_type) == DW_CC_GNU_renesas_sh)
  80.         val = 1;
  81.     }

  82.   if (sh_active_calling_convention == sh_cc_renesas)
  83.     val = 1;

  84.   return val;
  85. }

  86. static const char *
  87. sh_sh_register_name (struct gdbarch *gdbarch, int reg_nr)
  88. {
  89.   static char *register_names[] = {
  90.     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  91.     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  92.     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
  93.     "", "",
  94.     "", "", "", "", "", "", "", "",
  95.     "", "", "", "", "", "", "", "",
  96.     "", "",
  97.     "", "", "", "", "", "", "", "",
  98.     "", "", "", "", "", "", "", "",
  99.     "", "", "", "", "", "", "", "",
  100.   };
  101.   if (reg_nr < 0)
  102.     return NULL;
  103.   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
  104.     return NULL;
  105.   return register_names[reg_nr];
  106. }

  107. static const char *
  108. sh_sh3_register_name (struct gdbarch *gdbarch, int reg_nr)
  109. {
  110.   static char *register_names[] = {
  111.     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  112.     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  113.     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
  114.     "", "",
  115.     "", "", "", "", "", "", "", "",
  116.     "", "", "", "", "", "", "", "",
  117.     "ssr", "spc",
  118.     "r0b0", "r1b0", "r2b0", "r3b0", "r4b0", "r5b0", "r6b0", "r7b0",
  119.     "r0b1", "r1b1", "r2b1", "r3b1", "r4b1", "r5b1", "r6b1", "r7b1"
  120.     "", "", "", "", "", "", "", "",
  121.   };
  122.   if (reg_nr < 0)
  123.     return NULL;
  124.   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
  125.     return NULL;
  126.   return register_names[reg_nr];
  127. }

  128. static const char *
  129. sh_sh3e_register_name (struct gdbarch *gdbarch, int reg_nr)
  130. {
  131.   static char *register_names[] = {
  132.     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  133.     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  134.     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
  135.     "fpul", "fpscr",
  136.     "fr0", "fr1", "fr2", "fr3", "fr4", "fr5", "fr6", "fr7",
  137.     "fr8", "fr9", "fr10", "fr11", "fr12", "fr13", "fr14", "fr15",
  138.     "ssr", "spc",
  139.     "r0b0", "r1b0", "r2b0", "r3b0", "r4b0", "r5b0", "r6b0", "r7b0",
  140.     "r0b1", "r1b1", "r2b1", "r3b1", "r4b1", "r5b1", "r6b1", "r7b1",
  141.     "", "", "", "", "", "", "", "",
  142.   };
  143.   if (reg_nr < 0)
  144.     return NULL;
  145.   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
  146.     return NULL;
  147.   return register_names[reg_nr];
  148. }

  149. static const char *
  150. sh_sh2e_register_name (struct gdbarch *gdbarch, int reg_nr)
  151. {
  152.   static char *register_names[] = {
  153.     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  154.     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  155.     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
  156.     "fpul", "fpscr",
  157.     "fr0", "fr1", "fr2", "fr3", "fr4", "fr5", "fr6", "fr7",
  158.     "fr8", "fr9", "fr10", "fr11", "fr12", "fr13", "fr14", "fr15",
  159.     "", "",
  160.     "", "", "", "", "", "", "", "",
  161.     "", "", "", "", "", "", "", "",
  162.     "", "", "", "", "", "", "", "",
  163.   };
  164.   if (reg_nr < 0)
  165.     return NULL;
  166.   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
  167.     return NULL;
  168.   return register_names[reg_nr];
  169. }

  170. static const char *
  171. sh_sh2a_register_name (struct gdbarch *gdbarch, int reg_nr)
  172. {
  173.   static char *register_names[] = {
  174.     /* general registers 0-15 */
  175.     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  176.     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  177.     /* 16 - 22 */
  178.     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
  179.     /* 23, 24 */
  180.     "fpul", "fpscr",
  181.     /* floating point registers 25 - 40 */
  182.     "fr0", "fr1", "fr2", "fr3", "fr4", "fr5", "fr6", "fr7",
  183.     "fr8", "fr9", "fr10", "fr11", "fr12", "fr13", "fr14", "fr15",
  184.     /* 41, 42 */
  185.     "", "",
  186.     /* 43 - 62.  Banked registers.  The bank number used is determined by
  187.        the bank register (63).  */
  188.     "r0b", "r1b", "r2b", "r3b", "r4b", "r5b", "r6b", "r7b",
  189.     "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b",
  190.     "machb", "ivnb", "prb", "gbrb", "maclb",
  191.     /* 63: register bank number, not a real register but used to
  192.        communicate the register bank currently get/set.  This register
  193.        is hidden to the user, who manipulates it using the pseudo
  194.        register called "bank" (67).  See below.  */
  195.     "",
  196.     /* 64 - 66 */
  197.     "ibcr", "ibnr", "tbr",
  198.     /* 67: register bank number, the user visible pseudo register.  */
  199.     "bank",
  200.     /* double precision (pseudo) 68 - 75 */
  201.     "dr0", "dr2", "dr4", "dr6", "dr8", "dr10", "dr12", "dr14",
  202.   };
  203.   if (reg_nr < 0)
  204.     return NULL;
  205.   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
  206.     return NULL;
  207.   return register_names[reg_nr];
  208. }

  209. static const char *
  210. sh_sh2a_nofpu_register_name (struct gdbarch *gdbarch, int reg_nr)
  211. {
  212.   static char *register_names[] = {
  213.     /* general registers 0-15 */
  214.     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  215.     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  216.     /* 16 - 22 */
  217.     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
  218.     /* 23, 24 */
  219.     "", "",
  220.     /* floating point registers 25 - 40 */
  221.     "", "", "", "", "", "", "", "",
  222.     "", "", "", "", "", "", "", "",
  223.     /* 41, 42 */
  224.     "", "",
  225.     /* 43 - 62.  Banked registers.  The bank number used is determined by
  226.        the bank register (63).  */
  227.     "r0b", "r1b", "r2b", "r3b", "r4b", "r5b", "r6b", "r7b",
  228.     "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b",
  229.     "machb", "ivnb", "prb", "gbrb", "maclb",
  230.     /* 63: register bank number, not a real register but used to
  231.        communicate the register bank currently get/set.  This register
  232.        is hidden to the user, who manipulates it using the pseudo
  233.        register called "bank" (67).  See below.  */
  234.     "",
  235.     /* 64 - 66 */
  236.     "ibcr", "ibnr", "tbr",
  237.     /* 67: register bank number, the user visible pseudo register.  */
  238.     "bank",
  239.     /* double precision (pseudo) 68 - 75 */
  240.     "", "", "", "", "", "", "", "",
  241.   };
  242.   if (reg_nr < 0)
  243.     return NULL;
  244.   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
  245.     return NULL;
  246.   return register_names[reg_nr];
  247. }

  248. static const char *
  249. sh_sh_dsp_register_name (struct gdbarch *gdbarch, int reg_nr)
  250. {
  251.   static char *register_names[] = {
  252.     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  253.     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  254.     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
  255.     "", "dsr",
  256.     "a0g", "a0", "a1g", "a1", "m0", "m1", "x0", "x1",
  257.     "y0", "y1", "", "", "", "", "", "mod",
  258.     "", "",
  259.     "rs", "re", "", "", "", "", "", "",
  260.     "", "", "", "", "", "", "", "",
  261.     "", "", "", "", "", "", "", "",
  262.   };
  263.   if (reg_nr < 0)
  264.     return NULL;
  265.   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
  266.     return NULL;
  267.   return register_names[reg_nr];
  268. }

  269. static const char *
  270. sh_sh3_dsp_register_name (struct gdbarch *gdbarch, int reg_nr)
  271. {
  272.   static char *register_names[] = {
  273.     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  274.     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  275.     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
  276.     "", "dsr",
  277.     "a0g", "a0", "a1g", "a1", "m0", "m1", "x0", "x1",
  278.     "y0", "y1", "", "", "", "", "", "mod",
  279.     "ssr", "spc",
  280.     "rs", "re", "", "", "", "", "", "",
  281.     "r0b", "r1b", "r2b", "r3b", "r4b", "r5b", "r6b", "r7b",
  282.     "", "", "", "", "", "", "", "",
  283.     "", "", "", "", "", "", "", "",
  284.   };
  285.   if (reg_nr < 0)
  286.     return NULL;
  287.   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
  288.     return NULL;
  289.   return register_names[reg_nr];
  290. }

  291. static const char *
  292. sh_sh4_register_name (struct gdbarch *gdbarch, int reg_nr)
  293. {
  294.   static char *register_names[] = {
  295.     /* general registers 0-15 */
  296.     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  297.     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  298.     /* 16 - 22 */
  299.     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
  300.     /* 23, 24 */
  301.     "fpul", "fpscr",
  302.     /* floating point registers 25 - 40 */
  303.     "fr0", "fr1", "fr2", "fr3", "fr4", "fr5", "fr6", "fr7",
  304.     "fr8", "fr9", "fr10", "fr11", "fr12", "fr13", "fr14", "fr15",
  305.     /* 41, 42 */
  306.     "ssr", "spc",
  307.     /* bank 0 43 - 50 */
  308.     "r0b0", "r1b0", "r2b0", "r3b0", "r4b0", "r5b0", "r6b0", "r7b0",
  309.     /* bank 1 51 - 58 */
  310.     "r0b1", "r1b1", "r2b1", "r3b1", "r4b1", "r5b1", "r6b1", "r7b1",
  311.     /* 59 - 66 */
  312.     "", "", "", "", "", "", "", "",
  313.     /* pseudo bank register.  */
  314.     "",
  315.     /* double precision (pseudo) 68 - 75 */
  316.     "dr0", "dr2", "dr4", "dr6", "dr8", "dr10", "dr12", "dr14",
  317.     /* vectors (pseudo) 76 - 79 */
  318.     "fv0", "fv4", "fv8", "fv12",
  319.     /* FIXME: missing XF */
  320.     /* FIXME: missing XD */
  321.   };
  322.   if (reg_nr < 0)
  323.     return NULL;
  324.   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
  325.     return NULL;
  326.   return register_names[reg_nr];
  327. }

  328. static const char *
  329. sh_sh4_nofpu_register_name (struct gdbarch *gdbarch, int reg_nr)
  330. {
  331.   static char *register_names[] = {
  332.     /* general registers 0-15 */
  333.     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  334.     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  335.     /* 16 - 22 */
  336.     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
  337.     /* 23, 24 */
  338.     "", "",
  339.     /* floating point registers 25 - 40 -- not for nofpu target */
  340.     "", "", "", "", "", "", "", "",
  341.     "", "", "", "", "", "", "", "",
  342.     /* 41, 42 */
  343.     "ssr", "spc",
  344.     /* bank 0 43 - 50 */
  345.     "r0b0", "r1b0", "r2b0", "r3b0", "r4b0", "r5b0", "r6b0", "r7b0",
  346.     /* bank 1 51 - 58 */
  347.     "r0b1", "r1b1", "r2b1", "r3b1", "r4b1", "r5b1", "r6b1", "r7b1",
  348.     /* 59 - 66 */
  349.     "", "", "", "", "", "", "", "",
  350.     /* pseudo bank register.  */
  351.     "",
  352.     /* double precision (pseudo) 68 - 75 -- not for nofpu target */
  353.     "", "", "", "", "", "", "", "",
  354.     /* vectors (pseudo) 76 - 79 -- not for nofpu target */
  355.     "", "", "", "",
  356.   };
  357.   if (reg_nr < 0)
  358.     return NULL;
  359.   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
  360.     return NULL;
  361.   return register_names[reg_nr];
  362. }

  363. static const char *
  364. sh_sh4al_dsp_register_name (struct gdbarch *gdbarch, int reg_nr)
  365. {
  366.   static char *register_names[] = {
  367.     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  368.     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  369.     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
  370.     "", "dsr",
  371.     "a0g", "a0", "a1g", "a1", "m0", "m1", "x0", "x1",
  372.     "y0", "y1", "", "", "", "", "", "mod",
  373.     "ssr", "spc",
  374.     "rs", "re", "", "", "", "", "", "",
  375.     "r0b", "r1b", "r2b", "r3b", "r4b", "r5b", "r6b", "r7b",
  376.     "", "", "", "", "", "", "", "",
  377.     "", "", "", "", "", "", "", "",
  378.   };
  379.   if (reg_nr < 0)
  380.     return NULL;
  381.   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
  382.     return NULL;
  383.   return register_names[reg_nr];
  384. }

  385. static const unsigned char *
  386. sh_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenptr)
  387. {
  388.   /* 0xc3c3 is trapa #c3, and it works in big and little endian modes.  */
  389.   static unsigned char breakpoint[] = { 0xc3, 0xc3 };

  390.   /* For remote stub targets, trapa #20 is used.  */
  391.   if (strcmp (target_shortname, "remote") == 0)
  392.     {
  393.       static unsigned char big_remote_breakpoint[] = { 0xc3, 0x20 };
  394.       static unsigned char little_remote_breakpoint[] = { 0x20, 0xc3 };

  395.       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  396.         {
  397.           *lenptr = sizeof (big_remote_breakpoint);
  398.           return big_remote_breakpoint;
  399.         }
  400.       else
  401.         {
  402.           *lenptr = sizeof (little_remote_breakpoint);
  403.           return little_remote_breakpoint;
  404.         }
  405.     }

  406.   *lenptr = sizeof (breakpoint);
  407.   return breakpoint;
  408. }

  409. /* Prologue looks like
  410.    mov.l        r14,@-r15
  411.    sts.l        pr,@-r15
  412.    mov.l        <regs>,@-r15
  413.    sub                <room_for_loca_vars>,r15
  414.    mov                r15,r14

  415.    Actually it can be more complicated than this but that's it, basically.  */

  416. #define GET_SOURCE_REG(x)          (((x) >> 4) & 0xf)
  417. #define GET_TARGET_REG(x)          (((x) >> 8) & 0xf)

  418. /* JSR @Rm         0100mmmm00001011 */
  419. #define IS_JSR(x)                (((x) & 0xf0ff) == 0x400b)

  420. /* STS.L PR,@-r15  0100111100100010
  421.    r15-4-->r15, PR-->(r15) */
  422. #define IS_STS(x)                  ((x) == 0x4f22)

  423. /* STS.L MACL,@-r15  0100111100010010
  424.    r15-4-->r15, MACL-->(r15) */
  425. #define IS_MACL_STS(x)          ((x) == 0x4f12)

  426. /* MOV.L Rm,@-r15  00101111mmmm0110
  427.    r15-4-->r15, Rm-->(R15) */
  428. #define IS_PUSH(x)                 (((x) & 0xff0f) == 0x2f06)

  429. /* MOV r15,r14     0110111011110011
  430.    r15-->r14  */
  431. #define IS_MOV_SP_FP(x)          ((x) == 0x6ef3)

  432. /* ADD #imm,r15    01111111iiiiiiii
  433.    r15+imm-->r15 */
  434. #define IS_ADD_IMM_SP(x)         (((x) & 0xff00) == 0x7f00)

  435. #define IS_MOV_R3(x)                 (((x) & 0xff00) == 0x1a00)
  436. #define IS_SHLL_R3(x)                ((x) == 0x4300)

  437. /* ADD r3,r15      0011111100111100
  438.    r15+r3-->r15 */
  439. #define IS_ADD_R3SP(x)                ((x) == 0x3f3c)

  440. /* FMOV.S FRm,@-Rn  Rn-4-->Rn, FRm-->(Rn)     1111nnnnmmmm1011
  441.    FMOV DRm,@-Rn    Rn-8-->Rn, DRm-->(Rn)     1111nnnnmmm01011
  442.    FMOV XDm,@-Rn    Rn-8-->Rn, XDm-->(Rn)     1111nnnnmmm11011 */
  443. /* CV, 2003-08-28: Only suitable with Rn == SP, therefore name changed to
  444.                    make this entirely clear.  */
  445. /* #define IS_FMOV(x)                (((x) & 0xf00f) == 0xf00b) */
  446. #define IS_FPUSH(x)                (((x) & 0xff0f) == 0xff0b)

  447. /* MOV Rm,Rn          Rm-->Rn        0110nnnnmmmm0011  4 <= m <= 7 */
  448. #define IS_MOV_ARG_TO_REG(x) \
  449.         (((x) & 0xf00f) == 0x6003 && \
  450.          ((x) & 0x00f0) >= 0x0040 && \
  451.          ((x) & 0x00f0) <= 0x0070)
  452. /* MOV.L Rm,@Rn               0010nnnnmmmm0010  n = 14, 4 <= m <= 7 */
  453. #define IS_MOV_ARG_TO_IND_R14(x) \
  454.         (((x) & 0xff0f) == 0x2e02 && \
  455.          ((x) & 0x00f0) >= 0x0040 && \
  456.          ((x) & 0x00f0) <= 0x0070)
  457. /* MOV.L Rm,@(disp*4,Rn)      00011110mmmmdddd  n = 14, 4 <= m <= 7 */
  458. #define IS_MOV_ARG_TO_IND_R14_WITH_DISP(x) \
  459.         (((x) & 0xff00) == 0x1e00 && \
  460.          ((x) & 0x00f0) >= 0x0040 && \
  461.          ((x) & 0x00f0) <= 0x0070)

  462. /* MOV.W @(disp*2,PC),Rn      1001nnnndddddddd */
  463. #define IS_MOVW_PCREL_TO_REG(x)        (((x) & 0xf000) == 0x9000)
  464. /* MOV.L @(disp*4,PC),Rn      1101nnnndddddddd */
  465. #define IS_MOVL_PCREL_TO_REG(x)        (((x) & 0xf000) == 0xd000)
  466. /* MOVI20 #imm20,Rn           0000nnnniiii0000 */
  467. #define IS_MOVI20(x)                (((x) & 0xf00f) == 0x0000)
  468. /* SUB Rn,R15                 00111111nnnn1000 */
  469. #define IS_SUB_REG_FROM_SP(x)        (((x) & 0xff0f) == 0x3f08)

  470. #define FPSCR_SZ                (1 << 20)

  471. /* The following instructions are used for epilogue testing.  */
  472. #define IS_RESTORE_FP(x)        ((x) == 0x6ef6)
  473. #define IS_RTS(x)                ((x) == 0x000b)
  474. #define IS_LDS(x)                  ((x) == 0x4f26)
  475. #define IS_MACL_LDS(x)          ((x) == 0x4f16)
  476. #define IS_MOV_FP_SP(x)          ((x) == 0x6fe3)
  477. #define IS_ADD_REG_TO_FP(x)        (((x) & 0xff0f) == 0x3e0c)
  478. #define IS_ADD_IMM_FP(x)         (((x) & 0xff00) == 0x7e00)

  479. static CORE_ADDR
  480. sh_analyze_prologue (struct gdbarch *gdbarch,
  481.                      CORE_ADDR pc, CORE_ADDR limit_pc,
  482.                      struct sh_frame_cache *cache, ULONGEST fpscr)
  483. {
  484.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  485.   ULONGEST inst;
  486.   int offset;
  487.   int sav_offset = 0;
  488.   int r3_val = 0;
  489.   int reg, sav_reg = -1;

  490.   cache->uses_fp = 0;
  491.   for (; pc < limit_pc; pc += 2)
  492.     {
  493.       inst = read_memory_unsigned_integer (pc, 2, byte_order);
  494.       /* See where the registers will be saved to.  */
  495.       if (IS_PUSH (inst))
  496.         {
  497.           cache->saved_regs[GET_SOURCE_REG (inst)] = cache->sp_offset;
  498.           cache->sp_offset += 4;
  499.         }
  500.       else if (IS_STS (inst))
  501.         {
  502.           cache->saved_regs[PR_REGNUM] = cache->sp_offset;
  503.           cache->sp_offset += 4;
  504.         }
  505.       else if (IS_MACL_STS (inst))
  506.         {
  507.           cache->saved_regs[MACL_REGNUM] = cache->sp_offset;
  508.           cache->sp_offset += 4;
  509.         }
  510.       else if (IS_MOV_R3 (inst))
  511.         {
  512.           r3_val = ((inst & 0xff) ^ 0x80) - 0x80;
  513.         }
  514.       else if (IS_SHLL_R3 (inst))
  515.         {
  516.           r3_val <<= 1;
  517.         }
  518.       else if (IS_ADD_R3SP (inst))
  519.         {
  520.           cache->sp_offset += -r3_val;
  521.         }
  522.       else if (IS_ADD_IMM_SP (inst))
  523.         {
  524.           offset = ((inst & 0xff) ^ 0x80) - 0x80;
  525.           cache->sp_offset -= offset;
  526.         }
  527.       else if (IS_MOVW_PCREL_TO_REG (inst))
  528.         {
  529.           if (sav_reg < 0)
  530.             {
  531.               reg = GET_TARGET_REG (inst);
  532.               if (reg < 14)
  533.                 {
  534.                   sav_reg = reg;
  535.                   offset = (inst & 0xff) << 1;
  536.                   sav_offset =
  537.                     read_memory_integer ((pc + 4) + offset, 2, byte_order);
  538.                 }
  539.             }
  540.         }
  541.       else if (IS_MOVL_PCREL_TO_REG (inst))
  542.         {
  543.           if (sav_reg < 0)
  544.             {
  545.               reg = GET_TARGET_REG (inst);
  546.               if (reg < 14)
  547.                 {
  548.                   sav_reg = reg;
  549.                   offset = (inst & 0xff) << 2;
  550.                   sav_offset =
  551.                     read_memory_integer (((pc & 0xfffffffc) + 4) + offset,
  552.                                          4, byte_order);
  553.                 }
  554.             }
  555.         }
  556.       else if (IS_MOVI20 (inst)
  557.                && (pc + 2 < limit_pc))
  558.         {
  559.           if (sav_reg < 0)
  560.             {
  561.               reg = GET_TARGET_REG (inst);
  562.               if (reg < 14)
  563.                 {
  564.                   sav_reg = reg;
  565.                   sav_offset = GET_SOURCE_REG (inst) << 16;
  566.                   /* MOVI20 is a 32 bit instruction!  */
  567.                   pc += 2;
  568.                   sav_offset
  569.                     |= read_memory_unsigned_integer (pc, 2, byte_order);
  570.                   /* Now sav_offset contains an unsigned 20 bit value.
  571.                      It must still get sign extended.  */
  572.                   if (sav_offset & 0x00080000)
  573.                     sav_offset |= 0xfff00000;
  574.                 }
  575.             }
  576.         }
  577.       else if (IS_SUB_REG_FROM_SP (inst))
  578.         {
  579.           reg = GET_SOURCE_REG (inst);
  580.           if (sav_reg > 0 && reg == sav_reg)
  581.             {
  582.               sav_reg = -1;
  583.             }
  584.           cache->sp_offset += sav_offset;
  585.         }
  586.       else if (IS_FPUSH (inst))
  587.         {
  588.           if (fpscr & FPSCR_SZ)
  589.             {
  590.               cache->sp_offset += 8;
  591.             }
  592.           else
  593.             {
  594.               cache->sp_offset += 4;
  595.             }
  596.         }
  597.       else if (IS_MOV_SP_FP (inst))
  598.         {
  599.           pc += 2;
  600.           /* Don't go any further than six more instructions.  */
  601.           limit_pc = min (limit_pc, pc + (2 * 6));

  602.           cache->uses_fp = 1;
  603.           /* At this point, only allow argument register moves to other
  604.              registers or argument register moves to @(X,fp) which are
  605.              moving the register arguments onto the stack area allocated
  606.              by a former add somenumber to SP call.  Don't allow moving
  607.              to an fp indirect address above fp + cache->sp_offset.  */
  608.           for (; pc < limit_pc; pc += 2)
  609.             {
  610.               inst = read_memory_integer (pc, 2, byte_order);
  611.               if (IS_MOV_ARG_TO_IND_R14 (inst))
  612.                 {
  613.                   reg = GET_SOURCE_REG (inst);
  614.                   if (cache->sp_offset > 0)
  615.                     cache->saved_regs[reg] = cache->sp_offset;
  616.                 }
  617.               else if (IS_MOV_ARG_TO_IND_R14_WITH_DISP (inst))
  618.                 {
  619.                   reg = GET_SOURCE_REG (inst);
  620.                   offset = (inst & 0xf) * 4;
  621.                   if (cache->sp_offset > offset)
  622.                     cache->saved_regs[reg] = cache->sp_offset - offset;
  623.                 }
  624.               else if (IS_MOV_ARG_TO_REG (inst))
  625.                 continue;
  626.               else
  627.                 break;
  628.             }
  629.           break;
  630.         }
  631.       else if (IS_JSR (inst))
  632.         {
  633.           /* We have found a jsr that has been scheduled into the prologue.
  634.              If we continue the scan and return a pc someplace after this,
  635.              then setting a breakpoint on this function will cause it to
  636.              appear to be called after the function it is calling via the
  637.              jsr, which will be very confusing.  Most likely the next
  638.              instruction is going to be IS_MOV_SP_FP in the delay slot.  If
  639.              so, note that before returning the current pc.  */
  640.           if (pc + 2 < limit_pc)
  641.             {
  642.               inst = read_memory_integer (pc + 2, 2, byte_order);
  643.               if (IS_MOV_SP_FP (inst))
  644.                 cache->uses_fp = 1;
  645.             }
  646.           break;
  647.         }
  648. #if 0                /* This used to just stop when it found an instruction
  649.                    that was not considered part of the prologue.  Now,
  650.                    we just keep going looking for likely
  651.                    instructions.  */
  652.       else
  653.         break;
  654. #endif
  655.     }

  656.   return pc;
  657. }

  658. /* Skip any prologue before the guts of a function.  */
  659. static CORE_ADDR
  660. sh_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  661. {
  662.   CORE_ADDR post_prologue_pc, func_addr, func_end_addr, limit_pc;
  663.   struct sh_frame_cache cache;

  664.   /* See if we can determine the end of the prologue via the symbol table.
  665.      If so, then return either PC, or the PC after the prologue, whichever
  666.      is greater.  */
  667.   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end_addr))
  668.     {
  669.       post_prologue_pc = skip_prologue_using_sal (gdbarch, func_addr);
  670.       if (post_prologue_pc != 0)
  671.         return max (pc, post_prologue_pc);
  672.     }

  673.   /* Can't determine prologue from the symbol table, need to examine
  674.      instructions.  */

  675.   /* Find an upper limit on the function prologue using the debug
  676.      information.  If the debug information could not be used to provide
  677.      that bound, then use an arbitrary large number as the upper bound.  */
  678.   limit_pc = skip_prologue_using_sal (gdbarch, pc);
  679.   if (limit_pc == 0)
  680.     /* Don't go any further than 28 instructions.  */
  681.     limit_pc = pc + (2 * 28);

  682.   /* Do not allow limit_pc to be past the function end, if we know
  683.      where that end is...  */
  684.   if (func_end_addr != 0)
  685.     limit_pc = min (limit_pc, func_end_addr);

  686.   cache.sp_offset = -4;
  687.   post_prologue_pc = sh_analyze_prologue (gdbarch, pc, limit_pc, &cache, 0);
  688.   if (cache.uses_fp)
  689.     pc = post_prologue_pc;

  690.   return pc;
  691. }

  692. /* The ABI says:

  693.    Aggregate types not bigger than 8 bytes that have the same size and
  694.    alignment as one of the integer scalar types are returned in the
  695.    same registers as the integer type they match.

  696.    For example, a 2-byte aligned structure with size 2 bytes has the
  697.    same size and alignment as a short int, and will be returned in R0.
  698.    A 4-byte aligned structure with size 8 bytes has the same size and
  699.    alignment as a long long int, and will be returned in R0 and R1.

  700.    When an aggregate type is returned in R0 and R1, R0 contains the
  701.    first four bytes of the aggregate, and R1 contains the
  702.    remainder.  If the size of the aggregate type is not a multiple of 4
  703.    bytes, the aggregate is tail-padded up to a multiple of 4
  704.    bytes.  The value of the padding is undefined.  For little-endian
  705.    targets the padding will appear at the most significant end of the
  706.    last element, for big-endian targets the padding appears at the
  707.    least significant end of the last element.

  708.    All other aggregate types are returned by address.  The caller
  709.    function passes the address of an area large enough to hold the
  710.    aggregate value in R2.  The called function stores the result in
  711.    this location.

  712.    To reiterate, structs smaller than 8 bytes could also be returned
  713.    in memory, if they don't pass the "same size and alignment as an
  714.    integer type" rule.

  715.    For example, in

  716.    struct s { char c[3]; } wibble;
  717.    struct s foo(void) {  return wibble; }

  718.    the return value from foo() will be in memory, not
  719.    in R0, because there is no 3-byte integer type.

  720.    Similarly, in

  721.    struct s { char c[2]; } wibble;
  722.    struct s foo(void) {  return wibble; }

  723.    because a struct containing two chars has alignment 1, that matches
  724.    type char, but size 2, that matches type short.  There's no integer
  725.    type that has alignment 1 and size 2, so the struct is returned in
  726.    memory.  */

  727. static int
  728. sh_use_struct_convention (int renesas_abi, struct type *type)
  729. {
  730.   int len = TYPE_LENGTH (type);
  731.   int nelem = TYPE_NFIELDS (type);

  732.   /* The Renesas ABI returns aggregate types always on stack.  */
  733.   if (renesas_abi && (TYPE_CODE (type) == TYPE_CODE_STRUCT
  734.                       || TYPE_CODE (type) == TYPE_CODE_UNION))
  735.     return 1;

  736.   /* Non-power of 2 length types and types bigger than 8 bytes (which don't
  737.      fit in two registers anyway) use struct convention.  */
  738.   if (len != 1 && len != 2 && len != 4 && len != 8)
  739.     return 1;

  740.   /* Scalar types and aggregate types with exactly one field are aligned
  741.      by definition.  They are returned in registers.  */
  742.   if (nelem <= 1)
  743.     return 0;

  744.   /* If the first field in the aggregate has the same length as the entire
  745.      aggregate type, the type is returned in registers.  */
  746.   if (TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0)) == len)
  747.     return 0;

  748.   /* If the size of the aggregate is 8 bytes and the first field is
  749.      of size 4 bytes its alignment is equal to long long's alignment,
  750.      so it's returned in registers.  */
  751.   if (len == 8 && TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0)) == 4)
  752.     return 0;

  753.   /* Otherwise use struct convention.  */
  754.   return 1;
  755. }

  756. static int
  757. sh_use_struct_convention_nofpu (int renesas_abi, struct type *type)
  758. {
  759.   /* The Renesas ABI returns long longs/doubles etc. always on stack.  */
  760.   if (renesas_abi && TYPE_NFIELDS (type) == 0 && TYPE_LENGTH (type) >= 8)
  761.     return 1;
  762.   return sh_use_struct_convention (renesas_abi, type);
  763. }

  764. static CORE_ADDR
  765. sh_frame_align (struct gdbarch *ignore, CORE_ADDR sp)
  766. {
  767.   return sp & ~3;
  768. }

  769. /* Function: push_dummy_call (formerly push_arguments)
  770.    Setup the function arguments for calling a function in the inferior.

  771.    On the Renesas SH architecture, there are four registers (R4 to R7)
  772.    which are dedicated for passing function arguments.  Up to the first
  773.    four arguments (depending on size) may go into these registers.
  774.    The rest go on the stack.

  775.    MVS: Except on SH variants that have floating point registers.
  776.    In that case, float and double arguments are passed in the same
  777.    manner, but using FP registers instead of GP registers.

  778.    Arguments that are smaller than 4 bytes will still take up a whole
  779.    register or a whole 32-bit word on the stack, and will be
  780.    right-justified in the register or the stack word.  This includes
  781.    chars, shorts, and small aggregate types.

  782.    Arguments that are larger than 4 bytes may be split between two or
  783.    more registers.  If there are not enough registers free, an argument
  784.    may be passed partly in a register (or registers), and partly on the
  785.    stack.  This includes doubles, long longs, and larger aggregates.
  786.    As far as I know, there is no upper limit to the size of aggregates
  787.    that will be passed in this way; in other words, the convention of
  788.    passing a pointer to a large aggregate instead of a copy is not used.

  789.    MVS: The above appears to be true for the SH variants that do not
  790.    have an FPU, however those that have an FPU appear to copy the
  791.    aggregate argument onto the stack (and not place it in registers)
  792.    if it is larger than 16 bytes (four GP registers).

  793.    An exceptional case exists for struct arguments (and possibly other
  794.    aggregates such as arrays) if the size is larger than 4 bytes but
  795.    not a multiple of 4 bytes.  In this case the argument is never split
  796.    between the registers and the stack, but instead is copied in its
  797.    entirety onto the stack, AND also copied into as many registers as
  798.    there is room for.  In other words, space in registers permitting,
  799.    two copies of the same argument are passed in.  As far as I can tell,
  800.    only the one on the stack is used, although that may be a function
  801.    of the level of compiler optimization.  I suspect this is a compiler
  802.    bug.  Arguments of these odd sizes are left-justified within the
  803.    word (as opposed to arguments smaller than 4 bytes, which are
  804.    right-justified).

  805.    If the function is to return an aggregate type such as a struct, it
  806.    is either returned in the normal return value register R0 (if its
  807.    size is no greater than one byte), or else the caller must allocate
  808.    space into which the callee will copy the return value (if the size
  809.    is greater than one byte).  In this case, a pointer to the return
  810.    value location is passed into the callee in register R2, which does
  811.    not displace any of the other arguments passed in via registers R4
  812.    to R7.  */

  813. /* Helper function to justify value in register according to endianess.  */
  814. static const gdb_byte *
  815. sh_justify_value_in_reg (struct gdbarch *gdbarch, struct value *val, int len)
  816. {
  817.   static gdb_byte valbuf[4];

  818.   memset (valbuf, 0, sizeof (valbuf));
  819.   if (len < 4)
  820.     {
  821.       /* value gets right-justified in the register or stack word.  */
  822.       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  823.         memcpy (valbuf + (4 - len), value_contents (val), len);
  824.       else
  825.         memcpy (valbuf, value_contents (val), len);
  826.       return valbuf;
  827.     }
  828.   return value_contents (val);
  829. }

  830. /* Helper function to eval number of bytes to allocate on stack.  */
  831. static CORE_ADDR
  832. sh_stack_allocsize (int nargs, struct value **args)
  833. {
  834.   int stack_alloc = 0;
  835.   while (nargs-- > 0)
  836.     stack_alloc += ((TYPE_LENGTH (value_type (args[nargs])) + 3) & ~3);
  837.   return stack_alloc;
  838. }

  839. /* Helper functions for getting the float arguments right.  Registers usage
  840.    depends on the ABI and the endianess.  The comments should enlighten how
  841.    it's intended to work.  */

  842. /* This array stores which of the float arg registers are already in use.  */
  843. static int flt_argreg_array[FLOAT_ARGLAST_REGNUM - FLOAT_ARG0_REGNUM + 1];

  844. /* This function just resets the above array to "no reg used so far".  */
  845. static void
  846. sh_init_flt_argreg (void)
  847. {
  848.   memset (flt_argreg_array, 0, sizeof flt_argreg_array);
  849. }

  850. /* This function returns the next register to use for float arg passing.
  851.    It returns either a valid value between FLOAT_ARG0_REGNUM and
  852.    FLOAT_ARGLAST_REGNUM if a register is available, otherwise it returns
  853.    FLOAT_ARGLAST_REGNUM + 1 to indicate that no register is available.

  854.    Note that register number 0 in flt_argreg_array corresponds with the
  855.    real float register fr4.  In contrast to FLOAT_ARG0_REGNUM (value is
  856.    29) the parity of the register number is preserved, which is important
  857.    for the double register passing test (see the "argreg & 1" test below).  */
  858. static int
  859. sh_next_flt_argreg (struct gdbarch *gdbarch, int len, struct type *func_type)
  860. {
  861.   int argreg;

  862.   /* First search for the next free register.  */
  863.   for (argreg = 0; argreg <= FLOAT_ARGLAST_REGNUM - FLOAT_ARG0_REGNUM;
  864.        ++argreg)
  865.     if (!flt_argreg_array[argreg])
  866.       break;

  867.   /* No register left?  */
  868.   if (argreg > FLOAT_ARGLAST_REGNUM - FLOAT_ARG0_REGNUM)
  869.     return FLOAT_ARGLAST_REGNUM + 1;

  870.   if (len == 8)
  871.     {
  872.       /* Doubles are always starting in a even register number.  */
  873.       if (argreg & 1)
  874.         {
  875.           /* In gcc ABI, the skipped register is lost for further argument
  876.              passing now.  Not so in Renesas ABI.  */
  877.           if (!sh_is_renesas_calling_convention (func_type))
  878.             flt_argreg_array[argreg] = 1;

  879.           ++argreg;

  880.           /* No register left?  */
  881.           if (argreg > FLOAT_ARGLAST_REGNUM - FLOAT_ARG0_REGNUM)
  882.             return FLOAT_ARGLAST_REGNUM + 1;
  883.         }
  884.       /* Also mark the next register as used.  */
  885.       flt_argreg_array[argreg + 1] = 1;
  886.     }
  887.   else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE
  888.            && !sh_is_renesas_calling_convention (func_type))
  889.     {
  890.       /* In little endian, gcc passes floats like this: f5, f4, f7, f6, ...  */
  891.       if (!flt_argreg_array[argreg + 1])
  892.         ++argreg;
  893.     }
  894.   flt_argreg_array[argreg] = 1;
  895.   return FLOAT_ARG0_REGNUM + argreg;
  896. }

  897. /* Helper function which figures out, if a type is treated like a float type.

  898.    The FPU ABIs have a special way how to treat types as float types.
  899.    Structures with exactly one member, which is of type float or double, are
  900.    treated exactly as the base types float or double:

  901.      struct sf {
  902.        float f;
  903.      };

  904.      struct sd {
  905.        double d;
  906.      };

  907.    are handled the same way as just

  908.      float f;

  909.      double d;

  910.    As a result, arguments of these struct types are pushed into floating point
  911.    registers exactly as floats or doubles, using the same decision algorithm.

  912.    The same is valid if these types are used as function return types.  The
  913.    above structs are returned in fr0 resp. fr0,fr1 instead of in r0, r0,r1
  914.    or even using struct convention as it is for other structs.  */

  915. static int
  916. sh_treat_as_flt_p (struct type *type)
  917. {
  918.   /* Ordinary float types are obviously treated as float.  */
  919.   if (TYPE_CODE (type) == TYPE_CODE_FLT)
  920.     return 1;
  921.   /* Otherwise non-struct types are not treated as float.  */
  922.   if (TYPE_CODE (type) != TYPE_CODE_STRUCT)
  923.     return 0;
  924.   /* Otherwise structs with more than one memeber are not treated as float.  */
  925.   if (TYPE_NFIELDS (type) != 1)
  926.     return 0;
  927.   /* Otherwise if the type of that member is float, the whole type is
  928.      treated as float.  */
  929.   if (TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_FLT)
  930.     return 1;
  931.   /* Otherwise it's not treated as float.  */
  932.   return 0;
  933. }

  934. static CORE_ADDR
  935. sh_push_dummy_call_fpu (struct gdbarch *gdbarch,
  936.                         struct value *function,
  937.                         struct regcache *regcache,
  938.                         CORE_ADDR bp_addr, int nargs,
  939.                         struct value **args,
  940.                         CORE_ADDR sp, int struct_return,
  941.                         CORE_ADDR struct_addr)
  942. {
  943.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  944.   int stack_offset = 0;
  945.   int argreg = ARG0_REGNUM;
  946.   int flt_argreg = 0;
  947.   int argnum;
  948.   struct type *func_type = value_type (function);
  949.   struct type *type;
  950.   CORE_ADDR regval;
  951.   const gdb_byte *val;
  952.   int len, reg_size = 0;
  953.   int pass_on_stack = 0;
  954.   int treat_as_flt;
  955.   int last_reg_arg = INT_MAX;

  956.   /* The Renesas ABI expects all varargs arguments, plus the last
  957.      non-vararg argument to be on the stack, no matter how many
  958.      registers have been used so far.  */
  959.   if (sh_is_renesas_calling_convention (func_type)
  960.       && TYPE_VARARGS (func_type))
  961.     last_reg_arg = TYPE_NFIELDS (func_type) - 2;

  962.   /* First force sp to a 4-byte alignment.  */
  963.   sp = sh_frame_align (gdbarch, sp);

  964.   /* Make room on stack for args.  */
  965.   sp -= sh_stack_allocsize (nargs, args);

  966.   /* Initialize float argument mechanism.  */
  967.   sh_init_flt_argreg ();

  968.   /* Now load as many as possible of the first arguments into
  969.      registers, and push the rest onto the stack.  There are 16 bytes
  970.      in four registers available.  Loop thru args from first to last.  */
  971.   for (argnum = 0; argnum < nargs; argnum++)
  972.     {
  973.       type = value_type (args[argnum]);
  974.       len = TYPE_LENGTH (type);
  975.       val = sh_justify_value_in_reg (gdbarch, args[argnum], len);

  976.       /* Some decisions have to be made how various types are handled.
  977.          This also differs in different ABIs.  */
  978.       pass_on_stack = 0;

  979.       /* Find out the next register to use for a floating point value.  */
  980.       treat_as_flt = sh_treat_as_flt_p (type);
  981.       if (treat_as_flt)
  982.         flt_argreg = sh_next_flt_argreg (gdbarch, len, func_type);
  983.       /* In Renesas ABI, long longs and aggregate types are always passed
  984.          on stack.  */
  985.       else if (sh_is_renesas_calling_convention (func_type)
  986.                && ((TYPE_CODE (type) == TYPE_CODE_INT && len == 8)
  987.                    || TYPE_CODE (type) == TYPE_CODE_STRUCT
  988.                    || TYPE_CODE (type) == TYPE_CODE_UNION))
  989.         pass_on_stack = 1;
  990.       /* In contrast to non-FPU CPUs, arguments are never split between
  991.          registers and stack.  If an argument doesn't fit in the remaining
  992.          registers it's always pushed entirely on the stack.  */
  993.       else if (len > ((ARGLAST_REGNUM - argreg + 1) * 4))
  994.         pass_on_stack = 1;

  995.       while (len > 0)
  996.         {
  997.           if ((treat_as_flt && flt_argreg > FLOAT_ARGLAST_REGNUM)
  998.               || (!treat_as_flt && (argreg > ARGLAST_REGNUM
  999.                                     || pass_on_stack))
  1000.               || argnum > last_reg_arg)
  1001.             {
  1002.               /* The data goes entirely on the stack, 4-byte aligned.  */
  1003.               reg_size = (len + 3) & ~3;
  1004.               write_memory (sp + stack_offset, val, reg_size);
  1005.               stack_offset += reg_size;
  1006.             }
  1007.           else if (treat_as_flt && flt_argreg <= FLOAT_ARGLAST_REGNUM)
  1008.             {
  1009.               /* Argument goes in a float argument register.  */
  1010.               reg_size = register_size (gdbarch, flt_argreg);
  1011.               regval = extract_unsigned_integer (val, reg_size, byte_order);
  1012.               /* In little endian mode, float types taking two registers
  1013.                  (doubles on sh4, long doubles on sh2e, sh3e and sh4) must
  1014.                  be stored swapped in the argument registers.  The below
  1015.                  code first writes the first 32 bits in the next but one
  1016.                  register, increments the val and len values accordingly
  1017.                  and then proceeds as normal by writing the second 32 bits
  1018.                  into the next register.  */
  1019.               if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE
  1020.                   && TYPE_LENGTH (type) == 2 * reg_size)
  1021.                 {
  1022.                   regcache_cooked_write_unsigned (regcache, flt_argreg + 1,
  1023.                                                   regval);
  1024.                   val += reg_size;
  1025.                   len -= reg_size;
  1026.                   regval = extract_unsigned_integer (val, reg_size,
  1027.                                                      byte_order);
  1028.                 }
  1029.               regcache_cooked_write_unsigned (regcache, flt_argreg++, regval);
  1030.             }
  1031.           else if (!treat_as_flt && argreg <= ARGLAST_REGNUM)
  1032.             {
  1033.               /* there's room in a register */
  1034.               reg_size = register_size (gdbarch, argreg);
  1035.               regval = extract_unsigned_integer (val, reg_size, byte_order);
  1036.               regcache_cooked_write_unsigned (regcache, argreg++, regval);
  1037.             }
  1038.           /* Store the value one register at a time or in one step on
  1039.              stack.  */
  1040.           len -= reg_size;
  1041.           val += reg_size;
  1042.         }
  1043.     }

  1044.   if (struct_return)
  1045.     {
  1046.       if (sh_is_renesas_calling_convention (func_type))
  1047.         /* If the function uses the Renesas ABI, subtract another 4 bytes from
  1048.            the stack and store the struct return address there.  */
  1049.         write_memory_unsigned_integer (sp -= 4, 4, byte_order, struct_addr);
  1050.       else
  1051.         /* Using the gcc ABI, the "struct return pointer" pseudo-argument has
  1052.            its own dedicated register.  */
  1053.         regcache_cooked_write_unsigned (regcache,
  1054.                                         STRUCT_RETURN_REGNUM, struct_addr);
  1055.     }

  1056.   /* Store return address.  */
  1057.   regcache_cooked_write_unsigned (regcache, PR_REGNUM, bp_addr);

  1058.   /* Update stack pointer.  */
  1059.   regcache_cooked_write_unsigned (regcache,
  1060.                                   gdbarch_sp_regnum (gdbarch), sp);

  1061.   return sp;
  1062. }

  1063. static CORE_ADDR
  1064. sh_push_dummy_call_nofpu (struct gdbarch *gdbarch,
  1065.                           struct value *function,
  1066.                           struct regcache *regcache,
  1067.                           CORE_ADDR bp_addr,
  1068.                           int nargs, struct value **args,
  1069.                           CORE_ADDR sp, int struct_return,
  1070.                           CORE_ADDR struct_addr)
  1071. {
  1072.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1073.   int stack_offset = 0;
  1074.   int argreg = ARG0_REGNUM;
  1075.   int argnum;
  1076.   struct type *func_type = value_type (function);
  1077.   struct type *type;
  1078.   CORE_ADDR regval;
  1079.   const gdb_byte *val;
  1080.   int len, reg_size = 0;
  1081.   int pass_on_stack = 0;
  1082.   int last_reg_arg = INT_MAX;

  1083.   /* The Renesas ABI expects all varargs arguments, plus the last
  1084.      non-vararg argument to be on the stack, no matter how many
  1085.      registers have been used so far.  */
  1086.   if (sh_is_renesas_calling_convention (func_type)
  1087.       && TYPE_VARARGS (func_type))
  1088.     last_reg_arg = TYPE_NFIELDS (func_type) - 2;

  1089.   /* First force sp to a 4-byte alignment.  */
  1090.   sp = sh_frame_align (gdbarch, sp);

  1091.   /* Make room on stack for args.  */
  1092.   sp -= sh_stack_allocsize (nargs, args);

  1093.   /* Now load as many as possible of the first arguments into
  1094.      registers, and push the rest onto the stack.  There are 16 bytes
  1095.      in four registers available.  Loop thru args from first to last.  */
  1096.   for (argnum = 0; argnum < nargs; argnum++)
  1097.     {
  1098.       type = value_type (args[argnum]);
  1099.       len = TYPE_LENGTH (type);
  1100.       val = sh_justify_value_in_reg (gdbarch, args[argnum], len);

  1101.       /* Some decisions have to be made how various types are handled.
  1102.          This also differs in different ABIs.  */
  1103.       pass_on_stack = 0;
  1104.       /* Renesas ABI pushes doubles and long longs entirely on stack.
  1105.          Same goes for aggregate types.  */
  1106.       if (sh_is_renesas_calling_convention (func_type)
  1107.           && ((TYPE_CODE (type) == TYPE_CODE_INT && len >= 8)
  1108.               || (TYPE_CODE (type) == TYPE_CODE_FLT && len >= 8)
  1109.               || TYPE_CODE (type) == TYPE_CODE_STRUCT
  1110.               || TYPE_CODE (type) == TYPE_CODE_UNION))
  1111.         pass_on_stack = 1;
  1112.       while (len > 0)
  1113.         {
  1114.           if (argreg > ARGLAST_REGNUM || pass_on_stack
  1115.               || argnum > last_reg_arg)
  1116.             {
  1117.               /* The remainder of the data goes entirely on the stack,
  1118.                  4-byte aligned.  */
  1119.               reg_size = (len + 3) & ~3;
  1120.               write_memory (sp + stack_offset, val, reg_size);
  1121.               stack_offset += reg_size;
  1122.             }
  1123.           else if (argreg <= ARGLAST_REGNUM)
  1124.             {
  1125.               /* There's room in a register.  */
  1126.               reg_size = register_size (gdbarch, argreg);
  1127.               regval = extract_unsigned_integer (val, reg_size, byte_order);
  1128.               regcache_cooked_write_unsigned (regcache, argreg++, regval);
  1129.             }
  1130.           /* Store the value reg_size bytes at a time.  This means that things
  1131.              larger than reg_size bytes may go partly in registers and partly
  1132.              on the stack.  */
  1133.           len -= reg_size;
  1134.           val += reg_size;
  1135.         }
  1136.     }

  1137.   if (struct_return)
  1138.     {
  1139.       if (sh_is_renesas_calling_convention (func_type))
  1140.         /* If the function uses the Renesas ABI, subtract another 4 bytes from
  1141.            the stack and store the struct return address there.  */
  1142.         write_memory_unsigned_integer (sp -= 4, 4, byte_order, struct_addr);
  1143.       else
  1144.         /* Using the gcc ABI, the "struct return pointer" pseudo-argument has
  1145.            its own dedicated register.  */
  1146.         regcache_cooked_write_unsigned (regcache,
  1147.                                         STRUCT_RETURN_REGNUM, struct_addr);
  1148.     }

  1149.   /* Store return address.  */
  1150.   regcache_cooked_write_unsigned (regcache, PR_REGNUM, bp_addr);

  1151.   /* Update stack pointer.  */
  1152.   regcache_cooked_write_unsigned (regcache,
  1153.                                   gdbarch_sp_regnum (gdbarch), sp);

  1154.   return sp;
  1155. }

  1156. /* Find a function's return value in the appropriate registers (in
  1157.    regbuf), and copy it into valbuf.  Extract from an array REGBUF
  1158.    containing the (raw) register state a function return value of type
  1159.    TYPE, and copy that, in virtual format, into VALBUF.  */
  1160. static void
  1161. sh_extract_return_value_nofpu (struct type *type, struct regcache *regcache,
  1162.                                gdb_byte *valbuf)
  1163. {
  1164.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1165.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1166.   int len = TYPE_LENGTH (type);
  1167.   int return_register = R0_REGNUM;
  1168.   int offset;

  1169.   if (len <= 4)
  1170.     {
  1171.       ULONGEST c;

  1172.       regcache_cooked_read_unsigned (regcache, R0_REGNUM, &c);
  1173.       store_unsigned_integer (valbuf, len, byte_order, c);
  1174.     }
  1175.   else if (len == 8)
  1176.     {
  1177.       int i, regnum = R0_REGNUM;
  1178.       for (i = 0; i < len; i += 4)
  1179.         regcache_raw_read (regcache, regnum++, valbuf + i);
  1180.     }
  1181.   else
  1182.     error (_("bad size for return value"));
  1183. }

  1184. static void
  1185. sh_extract_return_value_fpu (struct type *type, struct regcache *regcache,
  1186.                              gdb_byte *valbuf)
  1187. {
  1188.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1189.   if (sh_treat_as_flt_p (type))
  1190.     {
  1191.       int len = TYPE_LENGTH (type);
  1192.       int i, regnum = gdbarch_fp0_regnum (gdbarch);
  1193.       for (i = 0; i < len; i += 4)
  1194.         if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
  1195.           regcache_raw_read (regcache, regnum++,
  1196.                              valbuf + len - 4 - i);
  1197.         else
  1198.           regcache_raw_read (regcache, regnum++, valbuf + i);
  1199.     }
  1200.   else
  1201.     sh_extract_return_value_nofpu (type, regcache, valbuf);
  1202. }

  1203. /* Write into appropriate registers a function return value
  1204.    of type TYPE, given in virtual format.
  1205.    If the architecture is sh4 or sh3e, store a function's return value
  1206.    in the R0 general register or in the FP0 floating point register,
  1207.    depending on the type of the return value.  In all the other cases
  1208.    the result is stored in r0, left-justified.  */
  1209. static void
  1210. sh_store_return_value_nofpu (struct type *type, struct regcache *regcache,
  1211.                              const gdb_byte *valbuf)
  1212. {
  1213.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1214.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1215.   ULONGEST val;
  1216.   int len = TYPE_LENGTH (type);

  1217.   if (len <= 4)
  1218.     {
  1219.       val = extract_unsigned_integer (valbuf, len, byte_order);
  1220.       regcache_cooked_write_unsigned (regcache, R0_REGNUM, val);
  1221.     }
  1222.   else
  1223.     {
  1224.       int i, regnum = R0_REGNUM;
  1225.       for (i = 0; i < len; i += 4)
  1226.         regcache_raw_write (regcache, regnum++, valbuf + i);
  1227.     }
  1228. }

  1229. static void
  1230. sh_store_return_value_fpu (struct type *type, struct regcache *regcache,
  1231.                            const gdb_byte *valbuf)
  1232. {
  1233.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1234.   if (sh_treat_as_flt_p (type))
  1235.     {
  1236.       int len = TYPE_LENGTH (type);
  1237.       int i, regnum = gdbarch_fp0_regnum (gdbarch);
  1238.       for (i = 0; i < len; i += 4)
  1239.         if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
  1240.           regcache_raw_write (regcache, regnum++,
  1241.                               valbuf + len - 4 - i);
  1242.         else
  1243.           regcache_raw_write (regcache, regnum++, valbuf + i);
  1244.     }
  1245.   else
  1246.     sh_store_return_value_nofpu (type, regcache, valbuf);
  1247. }

  1248. static enum return_value_convention
  1249. sh_return_value_nofpu (struct gdbarch *gdbarch, struct value *function,
  1250.                        struct type *type, struct regcache *regcache,
  1251.                        gdb_byte *readbuf, const gdb_byte *writebuf)
  1252. {
  1253.   struct type *func_type = function ? value_type (function) : NULL;

  1254.   if (sh_use_struct_convention_nofpu (
  1255.           sh_is_renesas_calling_convention (func_type), type))
  1256.     return RETURN_VALUE_STRUCT_CONVENTION;
  1257.   if (writebuf)
  1258.     sh_store_return_value_nofpu (type, regcache, writebuf);
  1259.   else if (readbuf)
  1260.     sh_extract_return_value_nofpu (type, regcache, readbuf);
  1261.   return RETURN_VALUE_REGISTER_CONVENTION;
  1262. }

  1263. static enum return_value_convention
  1264. sh_return_value_fpu (struct gdbarch *gdbarch, struct value *function,
  1265.                      struct type *type, struct regcache *regcache,
  1266.                      gdb_byte *readbuf, const gdb_byte *writebuf)
  1267. {
  1268.   struct type *func_type = function ? value_type (function) : NULL;

  1269.   if (sh_use_struct_convention (
  1270.         sh_is_renesas_calling_convention (func_type), type))
  1271.     return RETURN_VALUE_STRUCT_CONVENTION;
  1272.   if (writebuf)
  1273.     sh_store_return_value_fpu (type, regcache, writebuf);
  1274.   else if (readbuf)
  1275.     sh_extract_return_value_fpu (type, regcache, readbuf);
  1276.   return RETURN_VALUE_REGISTER_CONVENTION;
  1277. }

  1278. static struct type *
  1279. sh_sh2a_register_type (struct gdbarch *gdbarch, int reg_nr)
  1280. {
  1281.   if ((reg_nr >= gdbarch_fp0_regnum (gdbarch)
  1282.        && (reg_nr <= FP_LAST_REGNUM)) || (reg_nr == FPUL_REGNUM))
  1283.     return builtin_type (gdbarch)->builtin_float;
  1284.   else if (reg_nr >= DR0_REGNUM && reg_nr <= DR_LAST_REGNUM)
  1285.     return builtin_type (gdbarch)->builtin_double;
  1286.   else
  1287.     return builtin_type (gdbarch)->builtin_int;
  1288. }

  1289. /* Return the GDB type object for the "standard" data type
  1290.    of data in register N.  */
  1291. static struct type *
  1292. sh_sh3e_register_type (struct gdbarch *gdbarch, int reg_nr)
  1293. {
  1294.   if ((reg_nr >= gdbarch_fp0_regnum (gdbarch)
  1295.        && (reg_nr <= FP_LAST_REGNUM)) || (reg_nr == FPUL_REGNUM))
  1296.     return builtin_type (gdbarch)->builtin_float;
  1297.   else
  1298.     return builtin_type (gdbarch)->builtin_int;
  1299. }

  1300. static struct type *
  1301. sh_sh4_build_float_register_type (struct gdbarch *gdbarch, int high)
  1302. {
  1303.   return lookup_array_range_type (builtin_type (gdbarch)->builtin_float,
  1304.                                   0, high);
  1305. }

  1306. static struct type *
  1307. sh_sh4_register_type (struct gdbarch *gdbarch, int reg_nr)
  1308. {
  1309.   if ((reg_nr >= gdbarch_fp0_regnum (gdbarch)
  1310.        && (reg_nr <= FP_LAST_REGNUM)) || (reg_nr == FPUL_REGNUM))
  1311.     return builtin_type (gdbarch)->builtin_float;
  1312.   else if (reg_nr >= DR0_REGNUM && reg_nr <= DR_LAST_REGNUM)
  1313.     return builtin_type (gdbarch)->builtin_double;
  1314.   else if (reg_nr >= FV0_REGNUM && reg_nr <= FV_LAST_REGNUM)
  1315.     return sh_sh4_build_float_register_type (gdbarch, 3);
  1316.   else
  1317.     return builtin_type (gdbarch)->builtin_int;
  1318. }

  1319. static struct type *
  1320. sh_default_register_type (struct gdbarch *gdbarch, int reg_nr)
  1321. {
  1322.   return builtin_type (gdbarch)->builtin_int;
  1323. }

  1324. /* Is a register in a reggroup?
  1325.    The default code in reggroup.c doesn't identify system registers, some
  1326.    float registers or any of the vector registers.
  1327.    TODO: sh2a and dsp registers.  */
  1328. static int
  1329. sh_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
  1330.                         struct reggroup *reggroup)
  1331. {
  1332.   if (gdbarch_register_name (gdbarch, regnum) == NULL
  1333.       || *gdbarch_register_name (gdbarch, regnum) == '\0')
  1334.     return 0;

  1335.   if (reggroup == float_reggroup
  1336.       && (regnum == FPUL_REGNUM
  1337.           || regnum == FPSCR_REGNUM))
  1338.     return 1;

  1339.   if (regnum >= FV0_REGNUM && regnum <= FV_LAST_REGNUM)
  1340.     {
  1341.       if (reggroup == vector_reggroup || reggroup == float_reggroup)
  1342.         return 1;
  1343.       if (reggroup == general_reggroup)
  1344.         return 0;
  1345.     }

  1346.   if (regnum == VBR_REGNUM
  1347.       || regnum == SR_REGNUM
  1348.       || regnum == FPSCR_REGNUM
  1349.       || regnum == SSR_REGNUM
  1350.       || regnum == SPC_REGNUM)
  1351.     {
  1352.       if (reggroup == system_reggroup)
  1353.         return 1;
  1354.       if (reggroup == general_reggroup)
  1355.         return 0;
  1356.     }

  1357.   /* The default code can cope with any other registers.  */
  1358.   return default_register_reggroup_p (gdbarch, regnum, reggroup);
  1359. }

  1360. /* On the sh4, the DRi pseudo registers are problematic if the target
  1361.    is little endian.  When the user writes one of those registers, for
  1362.    instance with 'set var $dr0=1', we want the double to be stored
  1363.    like this:
  1364.    fr0 = 0x00 0x00 0xf0 0x3f
  1365.    fr1 = 0x00 0x00 0x00 0x00

  1366.    This corresponds to little endian byte order & big endian word
  1367.    order.  However if we let gdb write the register w/o conversion, it
  1368.    will write fr0 and fr1 this way:
  1369.    fr0 = 0x00 0x00 0x00 0x00
  1370.    fr1 = 0x00 0x00 0xf0 0x3f
  1371.    because it will consider fr0 and fr1 as a single LE stretch of memory.

  1372.    To achieve what we want we must force gdb to store things in
  1373.    floatformat_ieee_double_littlebyte_bigword (which is defined in
  1374.    include/floatformat.h and libiberty/floatformat.c.

  1375.    In case the target is big endian, there is no problem, the
  1376.    raw bytes will look like:
  1377.    fr0 = 0x3f 0xf0 0x00 0x00
  1378.    fr1 = 0x00 0x00 0x00 0x00

  1379.    The other pseudo registers (the FVs) also don't pose a problem
  1380.    because they are stored as 4 individual FP elements.  */

  1381. static void
  1382. sh_register_convert_to_virtual (struct gdbarch *gdbarch, int regnum,
  1383.                                 struct type *type, gdb_byte *from, gdb_byte *to)
  1384. {
  1385.   if (gdbarch_byte_order (gdbarch) != BFD_ENDIAN_LITTLE)
  1386.     {
  1387.       /* It is a no-op.  */
  1388.       memcpy (to, from, register_size (gdbarch, regnum));
  1389.       return;
  1390.     }

  1391.   if (regnum >= DR0_REGNUM && regnum <= DR_LAST_REGNUM)
  1392.     {
  1393.       DOUBLEST val;
  1394.       floatformat_to_doublest (&floatformat_ieee_double_littlebyte_bigword,
  1395.                                from, &val);
  1396.       store_typed_floating (to, type, val);
  1397.     }
  1398.   else
  1399.     error
  1400.       ("sh_register_convert_to_virtual called with non DR register number");
  1401. }

  1402. static void
  1403. sh_register_convert_to_raw (struct gdbarch *gdbarch, struct type *type,
  1404.                             int regnum, const gdb_byte *from, gdb_byte *to)
  1405. {
  1406.   if (gdbarch_byte_order (gdbarch) != BFD_ENDIAN_LITTLE)
  1407.     {
  1408.       /* It is a no-op.  */
  1409.       memcpy (to, from, register_size (gdbarch, regnum));
  1410.       return;
  1411.     }

  1412.   if (regnum >= DR0_REGNUM && regnum <= DR_LAST_REGNUM)
  1413.     {
  1414.       DOUBLEST val = extract_typed_floating (from, type);
  1415.       floatformat_from_doublest (&floatformat_ieee_double_littlebyte_bigword,
  1416.                                  &val, to);
  1417.     }
  1418.   else
  1419.     error (_("sh_register_convert_to_raw called with non DR register number"));
  1420. }

  1421. /* For vectors of 4 floating point registers.  */
  1422. static int
  1423. fv_reg_base_num (struct gdbarch *gdbarch, int fv_regnum)
  1424. {
  1425.   int fp_regnum;

  1426.   fp_regnum = gdbarch_fp0_regnum (gdbarch)
  1427.               + (fv_regnum - FV0_REGNUM) * 4;
  1428.   return fp_regnum;
  1429. }

  1430. /* For double precision floating point registers, i.e 2 fp regs.  */
  1431. static int
  1432. dr_reg_base_num (struct gdbarch *gdbarch, int dr_regnum)
  1433. {
  1434.   int fp_regnum;

  1435.   fp_regnum = gdbarch_fp0_regnum (gdbarch)
  1436.               + (dr_regnum - DR0_REGNUM) * 2;
  1437.   return fp_regnum;
  1438. }

  1439. /* Concatenate PORTIONS contiguous raw registers starting at
  1440.    BASE_REGNUM into BUFFER.  */

  1441. static enum register_status
  1442. pseudo_register_read_portions (struct gdbarch *gdbarch,
  1443.                                struct regcache *regcache,
  1444.                                int portions,
  1445.                                int base_regnum, gdb_byte *buffer)
  1446. {
  1447.   int portion;

  1448.   for (portion = 0; portion < portions; portion++)
  1449.     {
  1450.       enum register_status status;
  1451.       gdb_byte *b;

  1452.       b = buffer + register_size (gdbarch, base_regnum) * portion;
  1453.       status = regcache_raw_read (regcache, base_regnum + portion, b);
  1454.       if (status != REG_VALID)
  1455.         return status;
  1456.     }

  1457.   return REG_VALID;
  1458. }

  1459. static enum register_status
  1460. sh_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
  1461.                          int reg_nr, gdb_byte *buffer)
  1462. {
  1463.   int base_regnum;
  1464.   gdb_byte temp_buffer[MAX_REGISTER_SIZE];
  1465.   enum register_status status;

  1466.   if (reg_nr == PSEUDO_BANK_REGNUM)
  1467.     return regcache_raw_read (regcache, BANK_REGNUM, buffer);
  1468.   else if (reg_nr >= DR0_REGNUM && reg_nr <= DR_LAST_REGNUM)
  1469.     {
  1470.       base_regnum = dr_reg_base_num (gdbarch, reg_nr);

  1471.       /* Build the value in the provided buffer.  */
  1472.       /* Read the real regs for which this one is an alias.  */
  1473.       status = pseudo_register_read_portions (gdbarch, regcache,
  1474.                                               2, base_regnum, temp_buffer);
  1475.       if (status == REG_VALID)
  1476.         {
  1477.           /* We must pay attention to the endiannes. */
  1478.           sh_register_convert_to_virtual (gdbarch, reg_nr,
  1479.                                           register_type (gdbarch, reg_nr),
  1480.                                           temp_buffer, buffer);
  1481.         }
  1482.       return status;
  1483.     }
  1484.   else if (reg_nr >= FV0_REGNUM && reg_nr <= FV_LAST_REGNUM)
  1485.     {
  1486.       base_regnum = fv_reg_base_num (gdbarch, reg_nr);

  1487.       /* Read the real regs for which this one is an alias.  */
  1488.       return pseudo_register_read_portions (gdbarch, regcache,
  1489.                                             4, base_regnum, buffer);
  1490.     }
  1491.   else
  1492.     gdb_assert_not_reached ("invalid pseudo register number");
  1493. }

  1494. static void
  1495. sh_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
  1496.                           int reg_nr, const gdb_byte *buffer)
  1497. {
  1498.   int base_regnum, portion;
  1499.   gdb_byte temp_buffer[MAX_REGISTER_SIZE];

  1500.   if (reg_nr == PSEUDO_BANK_REGNUM)
  1501.     {
  1502.       /* When the bank register is written to, the whole register bank
  1503.          is switched and all values in the bank registers must be read
  1504.          from the target/sim again.  We're just invalidating the regcache
  1505.          so that a re-read happens next time it's necessary.  */
  1506.       int bregnum;

  1507.       regcache_raw_write (regcache, BANK_REGNUM, buffer);
  1508.       for (bregnum = R0_BANK0_REGNUM; bregnum < MACLB_REGNUM; ++bregnum)
  1509.         regcache_invalidate (regcache, bregnum);
  1510.     }
  1511.   else if (reg_nr >= DR0_REGNUM && reg_nr <= DR_LAST_REGNUM)
  1512.     {
  1513.       base_regnum = dr_reg_base_num (gdbarch, reg_nr);

  1514.       /* We must pay attention to the endiannes.  */
  1515.       sh_register_convert_to_raw (gdbarch, register_type (gdbarch, reg_nr),
  1516.                                   reg_nr, buffer, temp_buffer);

  1517.       /* Write the real regs for which this one is an alias.  */
  1518.       for (portion = 0; portion < 2; portion++)
  1519.         regcache_raw_write (regcache, base_regnum + portion,
  1520.                             (temp_buffer
  1521.                              + register_size (gdbarch,
  1522.                                               base_regnum) * portion));
  1523.     }
  1524.   else if (reg_nr >= FV0_REGNUM && reg_nr <= FV_LAST_REGNUM)
  1525.     {
  1526.       base_regnum = fv_reg_base_num (gdbarch, reg_nr);

  1527.       /* Write the real regs for which this one is an alias.  */
  1528.       for (portion = 0; portion < 4; portion++)
  1529.         regcache_raw_write (regcache, base_regnum + portion,
  1530.                             (buffer
  1531.                              + register_size (gdbarch,
  1532.                                               base_regnum) * portion));
  1533.     }
  1534. }

  1535. static int
  1536. sh_dsp_register_sim_regno (struct gdbarch *gdbarch, int nr)
  1537. {
  1538.   if (legacy_register_sim_regno (gdbarch, nr) < 0)
  1539.     return legacy_register_sim_regno (gdbarch, nr);
  1540.   if (nr >= DSR_REGNUM && nr <= Y1_REGNUM)
  1541.     return nr - DSR_REGNUM + SIM_SH_DSR_REGNUM;
  1542.   if (nr == MOD_REGNUM)
  1543.     return SIM_SH_MOD_REGNUM;
  1544.   if (nr == RS_REGNUM)
  1545.     return SIM_SH_RS_REGNUM;
  1546.   if (nr == RE_REGNUM)
  1547.     return SIM_SH_RE_REGNUM;
  1548.   if (nr >= DSP_R0_BANK_REGNUM && nr <= DSP_R7_BANK_REGNUM)
  1549.     return nr - DSP_R0_BANK_REGNUM + SIM_SH_R0_BANK_REGNUM;
  1550.   return nr;
  1551. }

  1552. static int
  1553. sh_sh2a_register_sim_regno (struct gdbarch *gdbarch, int nr)
  1554. {
  1555.   switch (nr)
  1556.     {
  1557.       case TBR_REGNUM:
  1558.         return SIM_SH_TBR_REGNUM;
  1559.       case IBNR_REGNUM:
  1560.         return SIM_SH_IBNR_REGNUM;
  1561.       case IBCR_REGNUM:
  1562.         return SIM_SH_IBCR_REGNUM;
  1563.       case BANK_REGNUM:
  1564.         return SIM_SH_BANK_REGNUM;
  1565.       case MACLB_REGNUM:
  1566.         return SIM_SH_BANK_MACL_REGNUM;
  1567.       case GBRB_REGNUM:
  1568.         return SIM_SH_BANK_GBR_REGNUM;
  1569.       case PRB_REGNUM:
  1570.         return SIM_SH_BANK_PR_REGNUM;
  1571.       case IVNB_REGNUM:
  1572.         return SIM_SH_BANK_IVN_REGNUM;
  1573.       case MACHB_REGNUM:
  1574.         return SIM_SH_BANK_MACH_REGNUM;
  1575.       default:
  1576.         break;
  1577.     }
  1578.   return legacy_register_sim_regno (gdbarch, nr);
  1579. }

  1580. /* Set up the register unwinding such that call-clobbered registers are
  1581.    not displayed in frames >0 because the true value is not certain.
  1582.    The 'undefined' registers will show up as 'not available' unless the
  1583.    CFI says otherwise.

  1584.    This function is currently set up for SH4 and compatible only.  */

  1585. static void
  1586. sh_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
  1587.                           struct dwarf2_frame_state_reg *reg,
  1588.                           struct frame_info *this_frame)
  1589. {
  1590.   /* Mark the PC as the destination for the return address.  */
  1591.   if (regnum == gdbarch_pc_regnum (gdbarch))
  1592.     reg->how = DWARF2_FRAME_REG_RA;

  1593.   /* Mark the stack pointer as the call frame address.  */
  1594.   else if (regnum == gdbarch_sp_regnum (gdbarch))
  1595.     reg->how = DWARF2_FRAME_REG_CFA;

  1596.   /* The above was taken from the default init_reg in dwarf2-frame.c
  1597.      while the below is SH specific.  */

  1598.   /* Caller save registers.  */
  1599.   else if ((regnum >= R0_REGNUM && regnum <= R0_REGNUM+7)
  1600.            || (regnum >= FR0_REGNUM && regnum <= FR0_REGNUM+11)
  1601.            || (regnum >= DR0_REGNUM && regnum <= DR0_REGNUM+5)
  1602.            || (regnum >= FV0_REGNUM && regnum <= FV0_REGNUM+2)
  1603.            || (regnum == MACH_REGNUM)
  1604.            || (regnum == MACL_REGNUM)
  1605.            || (regnum == FPUL_REGNUM)
  1606.            || (regnum == SR_REGNUM))
  1607.     reg->how = DWARF2_FRAME_REG_UNDEFINED;

  1608.   /* Callee save registers.  */
  1609.   else if ((regnum >= R0_REGNUM+8 && regnum <= R0_REGNUM+15)
  1610.            || (regnum >= FR0_REGNUM+12 && regnum <= FR0_REGNUM+15)
  1611.            || (regnum >= DR0_REGNUM+6 && regnum <= DR0_REGNUM+8)
  1612.            || (regnum == FV0_REGNUM+3))
  1613.     reg->how = DWARF2_FRAME_REG_SAME_VALUE;

  1614.   /* Other registers.  These are not in the ABI and may or may not
  1615.      mean anything in frames >0 so don't show them.  */
  1616.   else if ((regnum >= R0_BANK0_REGNUM && regnum <= R0_BANK0_REGNUM+15)
  1617.            || (regnum == GBR_REGNUM)
  1618.            || (regnum == VBR_REGNUM)
  1619.            || (regnum == FPSCR_REGNUM)
  1620.            || (regnum == SSR_REGNUM)
  1621.            || (regnum == SPC_REGNUM))
  1622.     reg->how = DWARF2_FRAME_REG_UNDEFINED;
  1623. }

  1624. static struct sh_frame_cache *
  1625. sh_alloc_frame_cache (void)
  1626. {
  1627.   struct sh_frame_cache *cache;
  1628.   int i;

  1629.   cache = FRAME_OBSTACK_ZALLOC (struct sh_frame_cache);

  1630.   /* Base address.  */
  1631.   cache->base = 0;
  1632.   cache->saved_sp = 0;
  1633.   cache->sp_offset = 0;
  1634.   cache->pc = 0;

  1635.   /* Frameless until proven otherwise.  */
  1636.   cache->uses_fp = 0;

  1637.   /* Saved registers.  We initialize these to -1 since zero is a valid
  1638.      offset (that's where fp is supposed to be stored).  */
  1639.   for (i = 0; i < SH_NUM_REGS; i++)
  1640.     {
  1641.       cache->saved_regs[i] = -1;
  1642.     }

  1643.   return cache;
  1644. }

  1645. static struct sh_frame_cache *
  1646. sh_frame_cache (struct frame_info *this_frame, void **this_cache)
  1647. {
  1648.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1649.   struct sh_frame_cache *cache;
  1650.   CORE_ADDR current_pc;
  1651.   int i;

  1652.   if (*this_cache)
  1653.     return *this_cache;

  1654.   cache = sh_alloc_frame_cache ();
  1655.   *this_cache = cache;

  1656.   /* In principle, for normal frames, fp holds the frame pointer,
  1657.      which holds the base address for the current stack frame.
  1658.      However, for functions that don't need it, the frame pointer is
  1659.      optional.  For these "frameless" functions the frame pointer is
  1660.      actually the frame pointer of the calling frame.  */
  1661.   cache->base = get_frame_register_unsigned (this_frame, FP_REGNUM);
  1662.   if (cache->base == 0)
  1663.     return cache;

  1664.   cache->pc = get_frame_func (this_frame);
  1665.   current_pc = get_frame_pc (this_frame);
  1666.   if (cache->pc != 0)
  1667.     {
  1668.       ULONGEST fpscr;

  1669.       /* Check for the existence of the FPSCR register.         If it exists,
  1670.          fetch its value for use in prologue analysis.        Passing a zero
  1671.          value is the best choice for architecture variants upon which
  1672.          there's no FPSCR register.  */
  1673.       if (gdbarch_register_reggroup_p (gdbarch, FPSCR_REGNUM, all_reggroup))
  1674.         fpscr = get_frame_register_unsigned (this_frame, FPSCR_REGNUM);
  1675.       else
  1676.         fpscr = 0;

  1677.       sh_analyze_prologue (gdbarch, cache->pc, current_pc, cache, fpscr);
  1678.     }

  1679.   if (!cache->uses_fp)
  1680.     {
  1681.       /* We didn't find a valid frame, which means that CACHE->base
  1682.          currently holds the frame pointer for our calling frame.  If
  1683.          we're at the start of a function, or somewhere half-way its
  1684.          prologue, the function's frame probably hasn't been fully
  1685.          setup yet.  Try to reconstruct the base address for the stack
  1686.          frame by looking at the stack pointer.  For truly "frameless"
  1687.          functions this might work too.  */
  1688.       cache->base = get_frame_register_unsigned
  1689.                      (this_frame, gdbarch_sp_regnum (gdbarch));
  1690.     }

  1691.   /* Now that we have the base address for the stack frame we can
  1692.      calculate the value of sp in the calling frame.  */
  1693.   cache->saved_sp = cache->base + cache->sp_offset;

  1694.   /* Adjust all the saved registers such that they contain addresses
  1695.      instead of offsets.  */
  1696.   for (i = 0; i < SH_NUM_REGS; i++)
  1697.     if (cache->saved_regs[i] != -1)
  1698.       cache->saved_regs[i] = cache->saved_sp - cache->saved_regs[i] - 4;

  1699.   return cache;
  1700. }

  1701. static struct value *
  1702. sh_frame_prev_register (struct frame_info *this_frame,
  1703.                         void **this_cache, int regnum)
  1704. {
  1705.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1706.   struct sh_frame_cache *cache = sh_frame_cache (this_frame, this_cache);

  1707.   gdb_assert (regnum >= 0);

  1708.   if (regnum == gdbarch_sp_regnum (gdbarch) && cache->saved_sp)
  1709.     return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);

  1710.   /* The PC of the previous frame is stored in the PR register of
  1711.      the current frame.  Frob regnum so that we pull the value from
  1712.      the correct place.  */
  1713.   if (regnum == gdbarch_pc_regnum (gdbarch))
  1714.     regnum = PR_REGNUM;

  1715.   if (regnum < SH_NUM_REGS && cache->saved_regs[regnum] != -1)
  1716.     return frame_unwind_got_memory (this_frame, regnum,
  1717.                                     cache->saved_regs[regnum]);

  1718.   return frame_unwind_got_register (this_frame, regnum, regnum);
  1719. }

  1720. static void
  1721. sh_frame_this_id (struct frame_info *this_frame, void **this_cache,
  1722.                   struct frame_id *this_id)
  1723. {
  1724.   struct sh_frame_cache *cache = sh_frame_cache (this_frame, this_cache);

  1725.   /* This marks the outermost frame.  */
  1726.   if (cache->base == 0)
  1727.     return;

  1728.   *this_id = frame_id_build (cache->saved_sp, cache->pc);
  1729. }

  1730. static const struct frame_unwind sh_frame_unwind = {
  1731.   NORMAL_FRAME,
  1732.   default_frame_unwind_stop_reason,
  1733.   sh_frame_this_id,
  1734.   sh_frame_prev_register,
  1735.   NULL,
  1736.   default_frame_sniffer
  1737. };

  1738. static CORE_ADDR
  1739. sh_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
  1740. {
  1741.   return frame_unwind_register_unsigned (next_frame,
  1742.                                          gdbarch_sp_regnum (gdbarch));
  1743. }

  1744. static CORE_ADDR
  1745. sh_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  1746. {
  1747.   return frame_unwind_register_unsigned (next_frame,
  1748.                                          gdbarch_pc_regnum (gdbarch));
  1749. }

  1750. static struct frame_id
  1751. sh_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  1752. {
  1753.   CORE_ADDR sp = get_frame_register_unsigned (this_frame,
  1754.                                               gdbarch_sp_regnum (gdbarch));
  1755.   return frame_id_build (sp, get_frame_pc (this_frame));
  1756. }

  1757. static CORE_ADDR
  1758. sh_frame_base_address (struct frame_info *this_frame, void **this_cache)
  1759. {
  1760.   struct sh_frame_cache *cache = sh_frame_cache (this_frame, this_cache);

  1761.   return cache->base;
  1762. }

  1763. static const struct frame_base sh_frame_base = {
  1764.   &sh_frame_unwind,
  1765.   sh_frame_base_address,
  1766.   sh_frame_base_address,
  1767.   sh_frame_base_address
  1768. };

  1769. static struct sh_frame_cache *
  1770. sh_make_stub_cache (struct frame_info *this_frame)
  1771. {
  1772.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1773.   struct sh_frame_cache *cache;

  1774.   cache = sh_alloc_frame_cache ();

  1775.   cache->saved_sp
  1776.     = get_frame_register_unsigned (this_frame, gdbarch_sp_regnum (gdbarch));

  1777.   return cache;
  1778. }

  1779. static void
  1780. sh_stub_this_id (struct frame_info *this_frame, void **this_cache,
  1781.                  struct frame_id *this_id)
  1782. {
  1783.   struct sh_frame_cache *cache;

  1784.   if (*this_cache == NULL)
  1785.     *this_cache = sh_make_stub_cache (this_frame);
  1786.   cache = *this_cache;

  1787.   *this_id = frame_id_build (cache->saved_sp, get_frame_pc (this_frame));
  1788. }

  1789. static int
  1790. sh_stub_unwind_sniffer (const struct frame_unwind *self,
  1791.                         struct frame_info *this_frame,
  1792.                         void **this_prologue_cache)
  1793. {
  1794.   CORE_ADDR addr_in_block;

  1795.   addr_in_block = get_frame_address_in_block (this_frame);
  1796.   if (in_plt_section (addr_in_block))
  1797.     return 1;

  1798.   return 0;
  1799. }

  1800. static const struct frame_unwind sh_stub_unwind =
  1801. {
  1802.   NORMAL_FRAME,
  1803.   default_frame_unwind_stop_reason,
  1804.   sh_stub_this_id,
  1805.   sh_frame_prev_register,
  1806.   NULL,
  1807.   sh_stub_unwind_sniffer
  1808. };

  1809. /* The epilogue is defined here as the area at the end of a function,
  1810.    either on the `ret' instruction itself or after an instruction which
  1811.    destroys the function's stack frame.  */
  1812. static int
  1813. sh_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  1814. {
  1815.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1816.   CORE_ADDR func_addr = 0, func_end = 0;

  1817.   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
  1818.     {
  1819.       ULONGEST inst;
  1820.       /* The sh epilogue is max. 14 bytes long.  Give another 14 bytes
  1821.          for a nop and some fixed data (e.g. big offsets) which are
  1822.          unfortunately also treated as part of the function (which
  1823.          means, they are below func_end.  */
  1824.       CORE_ADDR addr = func_end - 28;
  1825.       if (addr < func_addr + 4)
  1826.         addr = func_addr + 4;
  1827.       if (pc < addr)
  1828.         return 0;

  1829.       /* First search forward until hitting an rts.  */
  1830.       while (addr < func_end
  1831.              && !IS_RTS (read_memory_unsigned_integer (addr, 2, byte_order)))
  1832.         addr += 2;
  1833.       if (addr >= func_end)
  1834.         return 0;

  1835.       /* At this point we should find a mov.l @r15+,r14 instruction,
  1836.          either before or after the rts.  If not, then the function has
  1837.          probably no "normal" epilogue and we bail out here.  */
  1838.       inst = read_memory_unsigned_integer (addr - 2, 2, byte_order);
  1839.       if (IS_RESTORE_FP (read_memory_unsigned_integer (addr - 2, 2,
  1840.                                                        byte_order)))
  1841.         addr -= 2;
  1842.       else if (!IS_RESTORE_FP (read_memory_unsigned_integer (addr + 2, 2,
  1843.                                                              byte_order)))
  1844.         return 0;

  1845.       inst = read_memory_unsigned_integer (addr - 2, 2, byte_order);

  1846.       /* Step over possible lds.l @r15+,macl.  */
  1847.       if (IS_MACL_LDS (inst))
  1848.         {
  1849.           addr -= 2;
  1850.           inst = read_memory_unsigned_integer (addr - 2, 2, byte_order);
  1851.         }

  1852.       /* Step over possible lds.l @r15+,pr.  */
  1853.       if (IS_LDS (inst))
  1854.         {
  1855.           addr -= 2;
  1856.           inst = read_memory_unsigned_integer (addr - 2, 2, byte_order);
  1857.         }

  1858.       /* Step over possible mov r14,r15.  */
  1859.       if (IS_MOV_FP_SP (inst))
  1860.         {
  1861.           addr -= 2;
  1862.           inst = read_memory_unsigned_integer (addr - 2, 2, byte_order);
  1863.         }

  1864.       /* Now check for FP adjustments, using add #imm,r14 or add rX, r14
  1865.          instructions.  */
  1866.       while (addr > func_addr + 4
  1867.              && (IS_ADD_REG_TO_FP (inst) || IS_ADD_IMM_FP (inst)))
  1868.         {
  1869.           addr -= 2;
  1870.           inst = read_memory_unsigned_integer (addr - 2, 2, byte_order);
  1871.         }

  1872.       /* On SH2a check if the previous instruction was perhaps a MOVI20.
  1873.          That's allowed for the epilogue.  */
  1874.       if ((gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_sh2a
  1875.            || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_sh2a_nofpu)
  1876.           && addr > func_addr + 6
  1877.           && IS_MOVI20 (read_memory_unsigned_integer (addr - 4, 2,
  1878.                                                       byte_order)))
  1879.         addr -= 4;

  1880.       if (pc >= addr)
  1881.         return 1;
  1882.     }
  1883.   return 0;
  1884. }


  1885. /* Supply register REGNUM from the buffer specified by REGS and LEN
  1886.    in the register set REGSET to register cache REGCACHE.
  1887.    REGTABLE specifies where each register can be found in REGS.
  1888.    If REGNUM is -1, do this for all registers in REGSET.  */

  1889. void
  1890. sh_corefile_supply_regset (const struct regset *regset,
  1891.                            struct regcache *regcache,
  1892.                            int regnum, const void *regs, size_t len)
  1893. {
  1894.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1895.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1896.   const struct sh_corefile_regmap *regmap = (regset == &sh_corefile_gregset
  1897.                                              ? tdep->core_gregmap
  1898.                                              : tdep->core_fpregmap);
  1899.   int i;

  1900.   for (i = 0; regmap[i].regnum != -1; i++)
  1901.     {
  1902.       if ((regnum == -1 || regnum == regmap[i].regnum)
  1903.           && regmap[i].offset + 4 <= len)
  1904.         regcache_raw_supply (regcache, regmap[i].regnum,
  1905.                              (char *)regs + regmap[i].offset);
  1906.     }
  1907. }

  1908. /* Collect register REGNUM in the register set REGSET from register cache
  1909.    REGCACHE into the buffer specified by REGS and LEN.
  1910.    REGTABLE specifies where each register can be found in REGS.
  1911.    If REGNUM is -1, do this for all registers in REGSET.  */

  1912. void
  1913. sh_corefile_collect_regset (const struct regset *regset,
  1914.                             const struct regcache *regcache,
  1915.                             int regnum, void *regs, size_t len)
  1916. {
  1917.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1918.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  1919.   const struct sh_corefile_regmap *regmap = (regset == &sh_corefile_gregset
  1920.                                              ? tdep->core_gregmap
  1921.                                              : tdep->core_fpregmap);
  1922.   int i;

  1923.   for (i = 0; regmap[i].regnum != -1; i++)
  1924.     {
  1925.       if ((regnum == -1 || regnum == regmap[i].regnum)
  1926.           && regmap[i].offset + 4 <= len)
  1927.         regcache_raw_collect (regcache, regmap[i].regnum,
  1928.                               (char *)regs + regmap[i].offset);
  1929.     }
  1930. }

  1931. /* The following two regsets have the same contents, so it is tempting to
  1932.    unify them, but they are distiguished by their address, so don't.  */

  1933. const struct regset sh_corefile_gregset =
  1934. {
  1935.   NULL,
  1936.   sh_corefile_supply_regset,
  1937.   sh_corefile_collect_regset
  1938. };

  1939. static const struct regset sh_corefile_fpregset =
  1940. {
  1941.   NULL,
  1942.   sh_corefile_supply_regset,
  1943.   sh_corefile_collect_regset
  1944. };

  1945. static void
  1946. sh_iterate_over_regset_sections (struct gdbarch *gdbarch,
  1947.                                  iterate_over_regset_sections_cb *cb,
  1948.                                  void *cb_data,
  1949.                                  const struct regcache *regcache)
  1950. {
  1951.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);

  1952.   if (tdep->core_gregmap != NULL)
  1953.     cb (".reg", tdep->sizeof_gregset, &sh_corefile_gregset, NULL, cb_data);

  1954.   if (tdep->core_fpregmap != NULL)
  1955.     cb (".reg2", tdep->sizeof_fpregset, &sh_corefile_fpregset, NULL, cb_data);
  1956. }

  1957. /* This is the implementation of gdbarch method
  1958.    return_in_first_hidden_param_p.  */

  1959. static int
  1960. sh_return_in_first_hidden_param_p (struct gdbarch *gdbarch,
  1961.                                      struct type *type)
  1962. {
  1963.   return 0;
  1964. }



  1965. static struct gdbarch *
  1966. sh_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  1967. {
  1968.   struct gdbarch *gdbarch;
  1969.   struct gdbarch_tdep *tdep;

  1970.   /* SH5 is handled entirely in sh64-tdep.c.  */
  1971.   if (info.bfd_arch_info->mach == bfd_mach_sh5)
  1972.     return sh64_gdbarch_init (info, arches);

  1973.   /* If there is already a candidate, use it.  */
  1974.   arches = gdbarch_list_lookup_by_info (arches, &info);
  1975.   if (arches != NULL)
  1976.     return arches->gdbarch;

  1977.   /* None found, create a new architecture from the information
  1978.      provided.  */
  1979.   tdep = XCNEW (struct gdbarch_tdep);
  1980.   gdbarch = gdbarch_alloc (&info, tdep);

  1981.   set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1982.   set_gdbarch_int_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1983.   set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1984.   set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
  1985.   set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1986.   set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
  1987.   set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
  1988.   set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);

  1989.   set_gdbarch_num_regs (gdbarch, SH_NUM_REGS);
  1990.   set_gdbarch_sp_regnum (gdbarch, 15);
  1991.   set_gdbarch_pc_regnum (gdbarch, 16);
  1992.   set_gdbarch_fp0_regnum (gdbarch, -1);
  1993.   set_gdbarch_num_pseudo_regs (gdbarch, 0);

  1994.   set_gdbarch_register_type (gdbarch, sh_default_register_type);
  1995.   set_gdbarch_register_reggroup_p (gdbarch, sh_register_reggroup_p);

  1996.   set_gdbarch_breakpoint_from_pc (gdbarch, sh_breakpoint_from_pc);

  1997.   set_gdbarch_print_insn (gdbarch, print_insn_sh);
  1998.   set_gdbarch_register_sim_regno (gdbarch, legacy_register_sim_regno);

  1999.   set_gdbarch_return_value (gdbarch, sh_return_value_nofpu);

  2000.   set_gdbarch_skip_prologue (gdbarch, sh_skip_prologue);
  2001.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);

  2002.   set_gdbarch_push_dummy_call (gdbarch, sh_push_dummy_call_nofpu);
  2003.   set_gdbarch_return_in_first_hidden_param_p (gdbarch,
  2004.                                               sh_return_in_first_hidden_param_p);

  2005.   set_gdbarch_believe_pcc_promotion (gdbarch, 1);

  2006.   set_gdbarch_frame_align (gdbarch, sh_frame_align);
  2007.   set_gdbarch_unwind_sp (gdbarch, sh_unwind_sp);
  2008.   set_gdbarch_unwind_pc (gdbarch, sh_unwind_pc);
  2009.   set_gdbarch_dummy_id (gdbarch, sh_dummy_id);
  2010.   frame_base_set_default (gdbarch, &sh_frame_base);

  2011.   set_gdbarch_in_function_epilogue_p (gdbarch, sh_in_function_epilogue_p);

  2012.   dwarf2_frame_set_init_reg (gdbarch, sh_dwarf2_frame_init_reg);

  2013.   set_gdbarch_iterate_over_regset_sections
  2014.     (gdbarch, sh_iterate_over_regset_sections);

  2015.   switch (info.bfd_arch_info->mach)
  2016.     {
  2017.     case bfd_mach_sh:
  2018.       set_gdbarch_register_name (gdbarch, sh_sh_register_name);
  2019.       break;

  2020.     case bfd_mach_sh2:
  2021.       set_gdbarch_register_name (gdbarch, sh_sh_register_name);
  2022.       break;

  2023.     case bfd_mach_sh2e:
  2024.       /* doubles on sh2e and sh3e are actually 4 byte.  */
  2025.       set_gdbarch_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  2026.       set_gdbarch_double_format (gdbarch, floatformats_ieee_single);

  2027.       set_gdbarch_register_name (gdbarch, sh_sh2e_register_name);
  2028.       set_gdbarch_register_type (gdbarch, sh_sh3e_register_type);
  2029.       set_gdbarch_fp0_regnum (gdbarch, 25);
  2030.       set_gdbarch_return_value (gdbarch, sh_return_value_fpu);
  2031.       set_gdbarch_push_dummy_call (gdbarch, sh_push_dummy_call_fpu);
  2032.       break;

  2033.     case bfd_mach_sh2a:
  2034.       set_gdbarch_register_name (gdbarch, sh_sh2a_register_name);
  2035.       set_gdbarch_register_type (gdbarch, sh_sh2a_register_type);
  2036.       set_gdbarch_register_sim_regno (gdbarch, sh_sh2a_register_sim_regno);

  2037.       set_gdbarch_fp0_regnum (gdbarch, 25);
  2038.       set_gdbarch_num_pseudo_regs (gdbarch, 9);
  2039.       set_gdbarch_pseudo_register_read (gdbarch, sh_pseudo_register_read);
  2040.       set_gdbarch_pseudo_register_write (gdbarch, sh_pseudo_register_write);
  2041.       set_gdbarch_return_value (gdbarch, sh_return_value_fpu);
  2042.       set_gdbarch_push_dummy_call (gdbarch, sh_push_dummy_call_fpu);
  2043.       break;

  2044.     case bfd_mach_sh2a_nofpu:
  2045.       set_gdbarch_register_name (gdbarch, sh_sh2a_nofpu_register_name);
  2046.       set_gdbarch_register_sim_regno (gdbarch, sh_sh2a_register_sim_regno);

  2047.       set_gdbarch_num_pseudo_regs (gdbarch, 1);
  2048.       set_gdbarch_pseudo_register_read (gdbarch, sh_pseudo_register_read);
  2049.       set_gdbarch_pseudo_register_write (gdbarch, sh_pseudo_register_write);
  2050.       break;

  2051.     case bfd_mach_sh_dsp:
  2052.       set_gdbarch_register_name (gdbarch, sh_sh_dsp_register_name);
  2053.       set_gdbarch_register_sim_regno (gdbarch, sh_dsp_register_sim_regno);
  2054.       break;

  2055.     case bfd_mach_sh3:
  2056.     case bfd_mach_sh3_nommu:
  2057.     case bfd_mach_sh2a_nofpu_or_sh3_nommu:
  2058.       set_gdbarch_register_name (gdbarch, sh_sh3_register_name);
  2059.       break;

  2060.     case bfd_mach_sh3e:
  2061.     case bfd_mach_sh2a_or_sh3e:
  2062.       /* doubles on sh2e and sh3e are actually 4 byte.  */
  2063.       set_gdbarch_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  2064.       set_gdbarch_double_format (gdbarch, floatformats_ieee_single);

  2065.       set_gdbarch_register_name (gdbarch, sh_sh3e_register_name);
  2066.       set_gdbarch_register_type (gdbarch, sh_sh3e_register_type);
  2067.       set_gdbarch_fp0_regnum (gdbarch, 25);
  2068.       set_gdbarch_return_value (gdbarch, sh_return_value_fpu);
  2069.       set_gdbarch_push_dummy_call (gdbarch, sh_push_dummy_call_fpu);
  2070.       break;

  2071.     case bfd_mach_sh3_dsp:
  2072.       set_gdbarch_register_name (gdbarch, sh_sh3_dsp_register_name);
  2073.       set_gdbarch_register_sim_regno (gdbarch, sh_dsp_register_sim_regno);
  2074.       break;

  2075.     case bfd_mach_sh4:
  2076.     case bfd_mach_sh4a:
  2077.     case bfd_mach_sh2a_or_sh4:
  2078.       set_gdbarch_register_name (gdbarch, sh_sh4_register_name);
  2079.       set_gdbarch_register_type (gdbarch, sh_sh4_register_type);
  2080.       set_gdbarch_fp0_regnum (gdbarch, 25);
  2081.       set_gdbarch_num_pseudo_regs (gdbarch, 13);
  2082.       set_gdbarch_pseudo_register_read (gdbarch, sh_pseudo_register_read);
  2083.       set_gdbarch_pseudo_register_write (gdbarch, sh_pseudo_register_write);
  2084.       set_gdbarch_return_value (gdbarch, sh_return_value_fpu);
  2085.       set_gdbarch_push_dummy_call (gdbarch, sh_push_dummy_call_fpu);
  2086.       break;

  2087.     case bfd_mach_sh4_nofpu:
  2088.     case bfd_mach_sh4a_nofpu:
  2089.     case bfd_mach_sh4_nommu_nofpu:
  2090.     case bfd_mach_sh2a_nofpu_or_sh4_nommu_nofpu:
  2091.       set_gdbarch_register_name (gdbarch, sh_sh4_nofpu_register_name);
  2092.       break;

  2093.     case bfd_mach_sh4al_dsp:
  2094.       set_gdbarch_register_name (gdbarch, sh_sh4al_dsp_register_name);
  2095.       set_gdbarch_register_sim_regno (gdbarch, sh_dsp_register_sim_regno);
  2096.       break;

  2097.     default:
  2098.       set_gdbarch_register_name (gdbarch, sh_sh_register_name);
  2099.       break;
  2100.     }

  2101.   /* Hook in ABI-specific overrides, if they have been registered.  */
  2102.   gdbarch_init_osabi (info, gdbarch);

  2103.   dwarf2_append_unwinders (gdbarch);
  2104.   frame_unwind_append_unwinder (gdbarch, &sh_stub_unwind);
  2105.   frame_unwind_append_unwinder (gdbarch, &sh_frame_unwind);

  2106.   return gdbarch;
  2107. }

  2108. static void
  2109. show_sh_command (char *args, int from_tty)
  2110. {
  2111.   help_list (showshcmdlist, "show sh ", all_commands, gdb_stdout);
  2112. }

  2113. static void
  2114. set_sh_command (char *args, int from_tty)
  2115. {
  2116.   printf_unfiltered
  2117.     ("\"set sh\" must be followed by an appropriate subcommand.\n");
  2118.   help_list (setshcmdlist, "set sh ", all_commands, gdb_stdout);
  2119. }

  2120. extern initialize_file_ftype _initialize_sh_tdep/* -Wmissing-prototypes */

  2121. void
  2122. _initialize_sh_tdep (void)
  2123. {
  2124.   gdbarch_register (bfd_arch_sh, sh_gdbarch_init, NULL);

  2125.   add_prefix_cmd ("sh", no_class, set_sh_command, "SH specific commands.",
  2126.                   &setshcmdlist, "set sh ", 0, &setlist);
  2127.   add_prefix_cmd ("sh", no_class, show_sh_command, "SH specific commands.",
  2128.                   &showshcmdlist, "show sh ", 0, &showlist);

  2129.   add_setshow_enum_cmd ("calling-convention", class_vars, sh_cc_enum,
  2130.                         &sh_active_calling_convention,
  2131.                         _("Set calling convention used when calling target "
  2132.                           "functions from GDB."),
  2133.                         _("Show calling convention used when calling target "
  2134.                           "functions from GDB."),
  2135.                         _("gcc       - Use GCC calling convention (default).\n"
  2136.                           "renesas   - Enforce Renesas calling convention."),
  2137.                         NULL, NULL,
  2138.                         &setshcmdlist, &showshcmdlist);
  2139. }