gdb/sh64-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 "regcache.h"
  30. #include "osabi.h"
  31. #include "valprint.h"

  32. #include "elf-bfd.h"

  33. /* sh flags */
  34. #include "elf/sh.h"
  35. /* Register numbers shared with the simulator.  */
  36. #include "gdb/sim-sh.h"
  37. #include "language.h"
  38. #include "sh64-tdep.h"

  39. /* Information that is dependent on the processor variant.  */
  40. enum sh_abi
  41.   {
  42.     SH_ABI_UNKNOWN,
  43.     SH_ABI_32,
  44.     SH_ABI_64
  45.   };

  46. struct gdbarch_tdep
  47.   {
  48.     enum sh_abi sh_abi;
  49.   };

  50. struct sh64_frame_cache
  51. {
  52.   /* Base address.  */
  53.   CORE_ADDR base;
  54.   LONGEST sp_offset;
  55.   CORE_ADDR pc;

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

  58.   int media_mode;

  59.   /* Saved registers.  */
  60.   CORE_ADDR saved_regs[SIM_SH64_NR_REGS];
  61.   CORE_ADDR saved_sp;
  62. };

  63. /* Registers of SH5 */
  64. enum
  65.   {
  66.     R0_REGNUM = 0,
  67.     DEFAULT_RETURN_REGNUM = 2,
  68.     STRUCT_RETURN_REGNUM = 2,
  69.     ARG0_REGNUM = 2,
  70.     ARGLAST_REGNUM = 9,
  71.     FLOAT_ARGLAST_REGNUM = 11,
  72.     MEDIA_FP_REGNUM = 14,
  73.     PR_REGNUM = 18,
  74.     SR_REGNUM = 65,
  75.     DR0_REGNUM = 141,
  76.     DR_LAST_REGNUM = 172,
  77.     /* FPP stands for Floating Point Pair, to avoid confusion with
  78.        GDB's gdbarch_fp0_regnum, which is the number of the first Floating
  79.        point register.  Unfortunately on the sh5, the floating point
  80.        registers are called FR, and the floating point pairs are called FP.  */
  81.     FPP0_REGNUM = 173,
  82.     FPP_LAST_REGNUM = 204,
  83.     FV0_REGNUM = 205,
  84.     FV_LAST_REGNUM = 220,
  85.     R0_C_REGNUM = 221,
  86.     R_LAST_C_REGNUM = 236,
  87.     PC_C_REGNUM = 237,
  88.     GBR_C_REGNUM = 238,
  89.     MACH_C_REGNUM = 239,
  90.     MACL_C_REGNUM = 240,
  91.     PR_C_REGNUM = 241,
  92.     T_C_REGNUM = 242,
  93.     FPSCR_C_REGNUM = 243,
  94.     FPUL_C_REGNUM = 244,
  95.     FP0_C_REGNUM = 245,
  96.     FP_LAST_C_REGNUM = 260,
  97.     DR0_C_REGNUM = 261,
  98.     DR_LAST_C_REGNUM = 268,
  99.     FV0_C_REGNUM = 269,
  100.     FV_LAST_C_REGNUM = 272,
  101.     FPSCR_REGNUM = SIM_SH64_FPCSR_REGNUM,
  102.     SSR_REGNUM = SIM_SH64_SSR_REGNUM,
  103.     SPC_REGNUM = SIM_SH64_SPC_REGNUM,
  104.     TR7_REGNUM = SIM_SH64_TR0_REGNUM + 7,
  105.     FP_LAST_REGNUM = SIM_SH64_FR0_REGNUM + SIM_SH64_NR_FP_REGS - 1
  106.   };

  107. static const char *
  108. sh64_register_name (struct gdbarch *gdbarch, int reg_nr)
  109. {
  110.   static char *register_names[] =
  111.   {
  112.     /* SH MEDIA MODE (ISA 32) */
  113.     /* general registers (64-bit) 0-63 */
  114.     "r0",   "r1",   "r2",   "r3",   "r4",   "r5",   "r6",   "r7",
  115.     "r8",   "r9",   "r10""r11""r12""r13""r14""r15",
  116.     "r16""r17""r18""r19""r20""r21""r22""r23",
  117.     "r24""r25""r26""r27""r28""r29""r30""r31",
  118.     "r32""r33""r34""r35""r36""r37""r38""r39",
  119.     "r40""r41""r42""r43""r44""r45""r46""r47",
  120.     "r48""r49""r50""r51""r52""r53""r54""r55",
  121.     "r56""r57""r58""r59""r60""r61""r62""r63",

  122.     /* pc (64-bit) 64 */
  123.     "pc",

  124.     /* status reg., saved status reg., saved pc reg. (64-bit) 65-67 */
  125.     "sr""ssr""spc",

  126.     /* target registers (64-bit) 68-75 */
  127.     "tr0""tr1""tr2""tr3""tr4""tr5""tr6""tr7",

  128.     /* floating point state control register (32-bit) 76 */
  129.     "fpscr",

  130.     /* single precision floating point registers (32-bit) 77-140 */
  131.     "fr0""fr1""fr2""fr3""fr4""fr5""fr6""fr7",
  132.     "fr8""fr9""fr10", "fr11", "fr12", "fr13", "fr14", "fr15",
  133.     "fr16", "fr17", "fr18", "fr19", "fr20", "fr21", "fr22", "fr23",
  134.     "fr24", "fr25", "fr26", "fr27", "fr28", "fr29", "fr30", "fr31",
  135.     "fr32", "fr33", "fr34", "fr35", "fr36", "fr37", "fr38", "fr39",
  136.     "fr40", "fr41", "fr42", "fr43", "fr44", "fr45", "fr46", "fr47",
  137.     "fr48", "fr49", "fr50", "fr51", "fr52", "fr53", "fr54", "fr55",
  138.     "fr56", "fr57", "fr58", "fr59", "fr60", "fr61", "fr62", "fr63",

  139.     /* double precision registers (pseudo) 141-172 */
  140.     "dr0""dr2""dr4""dr6""dr8""dr10", "dr12", "dr14",
  141.     "dr16", "dr18", "dr20", "dr22", "dr24", "dr26", "dr28", "dr30",
  142.     "dr32", "dr34", "dr36", "dr38", "dr40", "dr42", "dr44", "dr46",
  143.     "dr48", "dr50", "dr52", "dr54", "dr56", "dr58", "dr60", "dr62",

  144.     /* floating point pairs (pseudo) 173-204 */
  145.     "fp0""fp2""fp4""fp6""fp8""fp10", "fp12", "fp14",
  146.     "fp16", "fp18", "fp20", "fp22", "fp24", "fp26", "fp28", "fp30",
  147.     "fp32", "fp34", "fp36", "fp38", "fp40", "fp42", "fp44", "fp46",
  148.     "fp48", "fp50", "fp52", "fp54", "fp56", "fp58", "fp60", "fp62",

  149.     /* floating point vectors (4 floating point regs) (pseudo) 205-220 */
  150.     "fv0""fv4""fv8""fv12", "fv16", "fv20", "fv24", "fv28",
  151.     "fv32", "fv36", "fv40", "fv44", "fv48", "fv52", "fv56", "fv60",

  152.     /* SH COMPACT MODE (ISA 16) (all pseudo) 221-272 */
  153.     "r0_c", "r1_c", "r2_c""r3_c""r4_c""r5_c""r6_c""r7_c",
  154.     "r8_c", "r9_c", "r10_c", "r11_c", "r12_c", "r13_c", "r14_c", "r15_c",
  155.     "pc_c",
  156.     "gbr_c", "mach_c", "macl_c", "pr_c", "t_c",
  157.     "fpscr_c", "fpul_c",
  158.     "fr0_c""fr1_c""fr2_c""fr3_c",
  159.     "fr4_c""fr5_c""fr6_c""fr7_c",
  160.     "fr8_c""fr9_c""fr10_c", "fr11_c",
  161.     "fr12_c", "fr13_c", "fr14_c", "fr15_c",
  162.     "dr0_c""dr2_c""dr4_c""dr6_c",
  163.     "dr8_c""dr10_c", "dr12_c", "dr14_c",
  164.     "fv0_c", "fv4_c", "fv8_c""fv12_c",
  165.     /* FIXME!!!! XF0 XF15, XD0 XD14 ?????  */
  166.   };

  167.   if (reg_nr < 0)
  168.     return NULL;
  169.   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
  170.     return NULL;
  171.   return register_names[reg_nr];
  172. }

  173. #define NUM_PSEUDO_REGS_SH_MEDIA 80
  174. #define NUM_PSEUDO_REGS_SH_COMPACT 51

  175. /* Macros and functions for setting and testing a bit in a minimal
  176.    symbol that marks it as 32-bit function.  The MSB of the minimal
  177.    symbol's "info" field is used for this purpose.

  178.    gdbarch_elf_make_msymbol_special tests whether an ELF symbol is "special",
  179.    i.e. refers to a 32-bit function, and sets a "special" bit in a
  180.    minimal symbol to mark it as a 32-bit function
  181.    MSYMBOL_IS_SPECIAL   tests the "special" bit in a minimal symbol  */

  182. #define MSYMBOL_IS_SPECIAL(msym) \
  183.   MSYMBOL_TARGET_FLAG_1 (msym)

  184. static void
  185. sh64_elf_make_msymbol_special (asymbol *sym, struct minimal_symbol *msym)
  186. {
  187.   if (msym == NULL)
  188.     return;

  189.   if (((elf_symbol_type *)(sym))->internal_elf_sym.st_other == STO_SH5_ISA32)
  190.     {
  191.       MSYMBOL_TARGET_FLAG_1 (msym) = 1;
  192.       SET_MSYMBOL_VALUE_ADDRESS (msym, MSYMBOL_VALUE_RAW_ADDRESS (msym) | 1);
  193.     }
  194. }

  195. /* ISA32 (shmedia) function addresses are odd (bit 0 is set).  Here
  196.    are some macros to test, set, or clear bit 0 of addresses.  */
  197. #define IS_ISA32_ADDR(addr)         ((addr) & 1)
  198. #define MAKE_ISA32_ADDR(addr)         ((addr) | 1)
  199. #define UNMAKE_ISA32_ADDR(addr)  ((addr) & ~1)

  200. static int
  201. pc_is_isa32 (bfd_vma memaddr)
  202. {
  203.   struct bound_minimal_symbol sym;

  204.   /* If bit 0 of the address is set, assume this is a
  205.      ISA32 (shmedia) address.  */
  206.   if (IS_ISA32_ADDR (memaddr))
  207.     return 1;

  208.   /* A flag indicating that this is a ISA32 function is stored by elfread.c in
  209.      the high bit of the info field.  Use this to decide if the function is
  210.      ISA16 or ISA32.  */
  211.   sym = lookup_minimal_symbol_by_pc (memaddr);
  212.   if (sym.minsym)
  213.     return MSYMBOL_IS_SPECIAL (sym.minsym);
  214.   else
  215.     return 0;
  216. }

  217. static const unsigned char *
  218. sh64_breakpoint_from_pc (struct gdbarch *gdbarch,
  219.                          CORE_ADDR *pcptr, int *lenptr)
  220. {
  221.   /* The BRK instruction for shmedia is
  222.      01101111 11110101 11111111 11110000
  223.      which translates in big endian mode to 0x6f, 0xf5, 0xff, 0xf0
  224.      and in little endian mode to 0xf0, 0xff, 0xf5, 0x6f */

  225.   /* The BRK instruction for shcompact is
  226.      00000000 00111011
  227.      which translates in big endian mode to 0x0, 0x3b
  228.      and in little endian mode to 0x3b, 0x0 */

  229.   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  230.     {
  231.       if (pc_is_isa32 (*pcptr))
  232.         {
  233.           static unsigned char big_breakpoint_media[] = {
  234.             0x6f, 0xf5, 0xff, 0xf0
  235.           };
  236.           *pcptr = UNMAKE_ISA32_ADDR (*pcptr);
  237.           *lenptr = sizeof (big_breakpoint_media);
  238.           return big_breakpoint_media;
  239.         }
  240.       else
  241.         {
  242.           static unsigned char big_breakpoint_compact[] = {0x0, 0x3b};
  243.           *lenptr = sizeof (big_breakpoint_compact);
  244.           return big_breakpoint_compact;
  245.         }
  246.     }
  247.   else
  248.     {
  249.       if (pc_is_isa32 (*pcptr))
  250.         {
  251.           static unsigned char little_breakpoint_media[] = {
  252.             0xf0, 0xff, 0xf5, 0x6f
  253.           };
  254.           *pcptr = UNMAKE_ISA32_ADDR (*pcptr);
  255.           *lenptr = sizeof (little_breakpoint_media);
  256.           return little_breakpoint_media;
  257.         }
  258.       else
  259.         {
  260.           static unsigned char little_breakpoint_compact[] = {0x3b, 0x0};
  261.           *lenptr = sizeof (little_breakpoint_compact);
  262.           return little_breakpoint_compact;
  263.         }
  264.     }
  265. }

  266. /* Prologue looks like
  267.    [mov.l       <regs>,@-r15]...
  268.    [sts.l       pr,@-r15]
  269.    [mov.l       r14,@-r15]
  270.    [mov         r15,r14]

  271.    Actually it can be more complicated than this.  For instance, with
  272.    newer gcc's:

  273.    mov.l   r14,@-r15
  274.    add     #-12,r15
  275.    mov     r15,r14
  276.    mov     r4,r1
  277.    mov     r5,r2
  278.    mov.l   r6,@(4,r14)
  279.    mov.l   r7,@(8,r14)
  280.    mov.b   r1,@r14
  281.    mov     r14,r1
  282.    mov     r14,r1
  283.    add     #2,r1
  284.    mov.w   r2,@r1

  285. */

  286. /* PTABS/L Rn, TRa       0110101111110001nnnnnnl00aaa0000
  287.    with l=1 and n = 18   0110101111110001010010100aaa0000 */
  288. #define IS_PTABSL_R18(x)  (((x) & 0xffffff8f) == 0x6bf14a00)

  289. /* STS.L PR,@-r0   0100000000100010
  290.    r0-4-->r0, PR-->(r0) */
  291. #define IS_STS_R0(x)                  ((x) == 0x4022)

  292. /* STS PR, Rm      0000mmmm00101010
  293.    PR-->Rm */
  294. #define IS_STS_PR(x)            (((x) & 0xf0ff) == 0x2a)

  295. /* MOV.L Rm,@(disp,r15)  00011111mmmmdddd
  296.    Rm-->(dispx4+r15) */
  297. #define IS_MOV_TO_R15(x)              (((x) & 0xff00) == 0x1f00)

  298. /* MOV.L R14,@(disp,r15)  000111111110dddd
  299.    R14-->(dispx4+r15) */
  300. #define IS_MOV_R14(x)              (((x) & 0xfff0) == 0x1fe0)

  301. /* ST.Q R14, disp, R18    101011001110dddddddddd0100100000
  302.    R18-->(dispx8+R14) */
  303. #define IS_STQ_R18_R14(x)          (((x) & 0xfff003ff) == 0xace00120)

  304. /* ST.Q R15, disp, R18    101011001111dddddddddd0100100000
  305.    R18-->(dispx8+R15) */
  306. #define IS_STQ_R18_R15(x)          (((x) & 0xfff003ff) == 0xacf00120)

  307. /* ST.L R15, disp, R18    101010001111dddddddddd0100100000
  308.    R18-->(dispx4+R15) */
  309. #define IS_STL_R18_R15(x)          (((x) & 0xfff003ff) == 0xa8f00120)

  310. /* ST.Q R15, disp, R14    1010 1100 1111 dddd dddd dd00 1110 0000
  311.    R14-->(dispx8+R15) */
  312. #define IS_STQ_R14_R15(x)          (((x) & 0xfff003ff) == 0xacf000e0)

  313. /* ST.L R15, disp, R14    1010 1000 1111 dddd dddd dd00 1110 0000
  314.    R14-->(dispx4+R15) */
  315. #define IS_STL_R14_R15(x)          (((x) & 0xfff003ff) == 0xa8f000e0)

  316. /* ADDI.L R15,imm,R15     1101 0100 1111 ssss ssss ss00 1111 0000
  317.    R15 + imm --> R15 */
  318. #define IS_ADDIL_SP_MEDIA(x)         (((x) & 0xfff003ff) == 0xd4f000f0)

  319. /* ADDI R15,imm,R15     1101 0000 1111 ssss ssss ss00 1111 0000
  320.    R15 + imm --> R15 */
  321. #define IS_ADDI_SP_MEDIA(x)         (((x) & 0xfff003ff) == 0xd0f000f0)

  322. /* ADD.L R15,R63,R14    0000 0000 1111 1000 1111 1100 1110 0000
  323.    R15 + R63 --> R14 */
  324. #define IS_ADDL_SP_FP_MEDIA(x)          ((x) == 0x00f8fce0)

  325. /* ADD R15,R63,R14    0000 0000 1111 1001 1111 1100 1110 0000
  326.    R15 + R63 --> R14 */
  327. #define IS_ADD_SP_FP_MEDIA(x)          ((x) == 0x00f9fce0)

  328. #define IS_MOV_SP_FP_MEDIA(x)          \
  329.   (IS_ADDL_SP_FP_MEDIA(x) || IS_ADD_SP_FP_MEDIA(x))

  330. /* MOV #imm, R0    1110 0000 ssss ssss
  331.    #imm-->R0 */
  332. #define IS_MOV_R0(x)                 (((x) & 0xff00) == 0xe000)

  333. /* MOV.L @(disp,PC), R0    1101 0000 iiii iiii  */
  334. #define IS_MOVL_R0(x)                 (((x) & 0xff00) == 0xd000)

  335. /* ADD r15,r0      0011 0000 1111 1100
  336.    r15+r0-->r0 */
  337. #define IS_ADD_SP_R0(x)                ((x) == 0x30fc)

  338. /* MOV.L R14 @-R0  0010 0000 1110 0110
  339.    R14-->(R0-4), R0-4-->R0 */
  340. #define IS_MOV_R14_R0(x)        ((x) == 0x20e6)

  341. /* ADD Rm,R63,Rn  Rm+R63-->Rn  0000 00mm mmmm 1001 1111 11nn nnnn 0000
  342.    where Rm is one of r2-r9 which are the argument registers.  */
  343. /* FIXME: Recognize the float and double register moves too!  */
  344. #define IS_MEDIA_IND_ARG_MOV(x) \
  345.   ((((x) & 0xfc0ffc0f) == 0x0009fc00) \
  346.    && (((x) & 0x03f00000) >= 0x00200000 \
  347.        && ((x) & 0x03f00000) <= 0x00900000))

  348. /* ST.Q Rn,0,Rm  Rm-->Rn+0  1010 11nn nnnn 0000 0000 00mm mmmm 0000
  349.    or ST.L Rn,0,Rm  Rm-->Rn+0  1010 10nn nnnn 0000 0000 00mm mmmm 0000
  350.    where Rm is one of r2-r9 which are the argument registers.  */
  351. #define IS_MEDIA_ARG_MOV(x) \
  352. (((((x) & 0xfc0ffc0f) == 0xac000000) || (((x) & 0xfc0ffc0f) == 0xa8000000)) \
  353.    && (((x) & 0x000003f0) >= 0x00000020 && ((x) & 0x000003f0) <= 0x00000090))

  354. /* ST.B R14,0,Rn     Rn-->(R14+0) 1010 0000 1110 0000 0000 00nn nnnn 0000 */
  355. /* ST.W R14,0,Rn     Rn-->(R14+0) 1010 0100 1110 0000 0000 00nn nnnn 0000 */
  356. /* ST.L R14,0,Rn     Rn-->(R14+0) 1010 1000 1110 0000 0000 00nn nnnn 0000 */
  357. /* FST.S R14,0,FRn   Rn-->(R14+0) 1011 0100 1110 0000 0000 00nn nnnn 0000 */
  358. /* FST.D R14,0,DRn   Rn-->(R14+0) 1011 1100 1110 0000 0000 00nn nnnn 0000 */
  359. #define IS_MEDIA_MOV_TO_R14(x)  \
  360. ((((x) & 0xfffffc0f) == 0xa0e00000) \
  361. || (((x) & 0xfffffc0f) == 0xa4e00000) \
  362. || (((x) & 0xfffffc0f) == 0xa8e00000) \
  363. || (((x) & 0xfffffc0f) == 0xb4e00000) \
  364. || (((x) & 0xfffffc0f) == 0xbce00000))

  365. /* MOV Rm, Rn  Rm-->Rn 0110 nnnn mmmm 0011
  366.    where Rm is r2-r9 */
  367. #define IS_COMPACT_IND_ARG_MOV(x) \
  368.   ((((x) & 0xf00f) == 0x6003) && (((x) & 0x00f0) >= 0x0020) \
  369.    && (((x) & 0x00f0) <= 0x0090))

  370. /* compact direct arg move!
  371.    MOV.L Rn, @r14     0010 1110 mmmm 0010 */
  372. #define IS_COMPACT_ARG_MOV(x) \
  373.   (((((x) & 0xff0f) == 0x2e02) && (((x) & 0x00f0) >= 0x0020) \
  374.     && ((x) & 0x00f0) <= 0x0090))

  375. /* MOV.B Rm, @R14     0010 1110 mmmm 0000
  376.    MOV.W Rm, @R14     0010 1110 mmmm 0001 */
  377. #define IS_COMPACT_MOV_TO_R14(x) \
  378. ((((x) & 0xff0f) == 0x2e00) || (((x) & 0xff0f) == 0x2e01))

  379. #define IS_JSR_R0(x)           ((x) == 0x400b)
  380. #define IS_NOP(x)              ((x) == 0x0009)


  381. /* MOV r15,r14     0110111011110011
  382.    r15-->r14  */
  383. #define IS_MOV_SP_FP(x)          ((x) == 0x6ef3)

  384. /* ADD #imm,r15    01111111iiiiiiii
  385.    r15+imm-->r15 */
  386. #define IS_ADD_SP(x)                 (((x) & 0xff00) == 0x7f00)

  387. /* Skip any prologue before the guts of a function.  */

  388. /* Skip the prologue using the debug information.  If this fails we'll
  389.    fall back on the 'guess' method below.  */
  390. static CORE_ADDR
  391. after_prologue (CORE_ADDR pc)
  392. {
  393.   struct symtab_and_line sal;
  394.   CORE_ADDR func_addr, func_end;

  395.   /* If we can not find the symbol in the partial symbol table, then
  396.      there is no hope we can determine the function's start address
  397.      with this code.  */
  398.   if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
  399.     return 0;


  400.   /* Get the line associated with FUNC_ADDR.  */
  401.   sal = find_pc_line (func_addr, 0);

  402.   /* There are only two cases to consider.  First, the end of the source line
  403.      is within the function bounds.  In that case we return the end of the
  404.      source line.  Second is the end of the source line extends beyond the
  405.      bounds of the current function.  We need to use the slow code to
  406.      examine instructions in that case.  */
  407.   if (sal.end < func_end)
  408.     return sal.end;
  409.   else
  410.     return 0;
  411. }

  412. static CORE_ADDR
  413. look_for_args_moves (struct gdbarch *gdbarch,
  414.                      CORE_ADDR start_pc, int media_mode)
  415. {
  416.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  417.   CORE_ADDR here, end;
  418.   int w;
  419.   int insn_size = (media_mode ? 4 : 2);

  420.   for (here = start_pc, end = start_pc + (insn_size * 28); here < end;)
  421.     {
  422.       if (media_mode)
  423.         {
  424.           w = read_memory_integer (UNMAKE_ISA32_ADDR (here),
  425.                                    insn_size, byte_order);
  426.           here += insn_size;
  427.           if (IS_MEDIA_IND_ARG_MOV (w))
  428.             {
  429.               /* This must be followed by a store to r14, so the argument
  430.                  is where the debug info says it is.  This can happen after
  431.                  the SP has been saved, unfortunately.  */

  432.               int next_insn = read_memory_integer (UNMAKE_ISA32_ADDR (here),
  433.                                                    insn_size, byte_order);
  434.               here += insn_size;
  435.               if (IS_MEDIA_MOV_TO_R14 (next_insn))
  436.                 start_pc = here;
  437.             }
  438.           else if (IS_MEDIA_ARG_MOV (w))
  439.             {
  440.               /* These instructions store directly the argument in r14.  */
  441.               start_pc = here;
  442.             }
  443.           else
  444.             break;
  445.         }
  446.       else
  447.         {
  448.           w = read_memory_integer (here, insn_size, byte_order);
  449.           w = w & 0xffff;
  450.           here += insn_size;
  451.           if (IS_COMPACT_IND_ARG_MOV (w))
  452.             {
  453.               /* This must be followed by a store to r14, so the argument
  454.                  is where the debug info says it is.  This can happen after
  455.                  the SP has been saved, unfortunately.  */

  456.               int next_insn = 0xffff & read_memory_integer (here, insn_size,
  457.                                                             byte_order);
  458.               here += insn_size;
  459.               if (IS_COMPACT_MOV_TO_R14 (next_insn))
  460.                 start_pc = here;
  461.             }
  462.           else if (IS_COMPACT_ARG_MOV (w))
  463.             {
  464.               /* These instructions store directly the argument in r14.  */
  465.               start_pc = here;
  466.             }
  467.           else if (IS_MOVL_R0 (w))
  468.             {
  469.               /* There is a function that gcc calls to get the arguments
  470.                  passed correctly to the function.  Only after this
  471.                  function call the arguments will be found at the place
  472.                  where they are supposed to be.  This happens in case the
  473.                  argument has to be stored into a 64-bit register (for
  474.                  instance doubles, long longs).  SHcompact doesn't have
  475.                  access to the full 64-bits, so we store the register in
  476.                  stack slot and store the address of the stack slot in
  477.                  the register, then do a call through a wrapper that
  478.                  loads the memory value into the register.  A SHcompact
  479.                  callee calls an argument decoder
  480.                  (GCC_shcompact_incoming_args) that stores the 64-bit
  481.                  value in a stack slot and stores the address of the
  482.                  stack slot in the register.  GCC thinks the argument is
  483.                  just passed by transparent reference, but this is only
  484.                  true after the argument decoder is called.  Such a call
  485.                  needs to be considered part of the prologue.  */

  486.               /* This must be followed by a JSR @r0 instruction and by
  487.                  a NOP instruction.  After these, the prologue is over!  */

  488.               int next_insn = 0xffff & read_memory_integer (here, insn_size,
  489.                                                             byte_order);
  490.               here += insn_size;
  491.               if (IS_JSR_R0 (next_insn))
  492.                 {
  493.                   next_insn = 0xffff & read_memory_integer (here, insn_size,
  494.                                                             byte_order);
  495.                   here += insn_size;

  496.                   if (IS_NOP (next_insn))
  497.                     start_pc = here;
  498.                 }
  499.             }
  500.           else
  501.             break;
  502.         }
  503.     }

  504.   return start_pc;
  505. }

  506. static CORE_ADDR
  507. sh64_skip_prologue_hard_way (struct gdbarch *gdbarch, CORE_ADDR start_pc)
  508. {
  509.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  510.   CORE_ADDR here, end;
  511.   int updated_fp = 0;
  512.   int insn_size = 4;
  513.   int media_mode = 1;

  514.   if (!start_pc)
  515.     return 0;

  516.   if (pc_is_isa32 (start_pc) == 0)
  517.     {
  518.       insn_size = 2;
  519.       media_mode = 0;
  520.     }

  521.   for (here = start_pc, end = start_pc + (insn_size * 28); here < end;)
  522.     {

  523.       if (media_mode)
  524.         {
  525.           int w = read_memory_integer (UNMAKE_ISA32_ADDR (here),
  526.                                        insn_size, byte_order);
  527.           here += insn_size;
  528.           if (IS_STQ_R18_R14 (w) || IS_STQ_R18_R15 (w) || IS_STQ_R14_R15 (w)
  529.               || IS_STL_R14_R15 (w) || IS_STL_R18_R15 (w)
  530.               || IS_ADDIL_SP_MEDIA (w) || IS_ADDI_SP_MEDIA (w)
  531.               || IS_PTABSL_R18 (w))
  532.             {
  533.               start_pc = here;
  534.             }
  535.           else if (IS_MOV_SP_FP (w) || IS_MOV_SP_FP_MEDIA(w))
  536.             {
  537.               start_pc = here;
  538.               updated_fp = 1;
  539.             }
  540.           else
  541.             if (updated_fp)
  542.               {
  543.                 /* Don't bail out yet, we may have arguments stored in
  544.                    registers here, according to the debug info, so that
  545.                    gdb can print the frames correctly.  */
  546.                 start_pc = look_for_args_moves (gdbarch,
  547.                                                 here - insn_size, media_mode);
  548.                 break;
  549.               }
  550.         }
  551.       else
  552.         {
  553.           int w = 0xffff & read_memory_integer (here, insn_size, byte_order);
  554.           here += insn_size;

  555.           if (IS_STS_R0 (w) || IS_STS_PR (w)
  556.               || IS_MOV_TO_R15 (w) || IS_MOV_R14 (w)
  557.               || IS_MOV_R0 (w) || IS_ADD_SP_R0 (w) || IS_MOV_R14_R0 (w))
  558.             {
  559.               start_pc = here;
  560.             }
  561.           else if (IS_MOV_SP_FP (w))
  562.             {
  563.               start_pc = here;
  564.               updated_fp = 1;
  565.             }
  566.           else
  567.             if (updated_fp)
  568.               {
  569.                 /* Don't bail out yet, we may have arguments stored in
  570.                    registers here, according to the debug info, so that
  571.                    gdb can print the frames correctly.  */
  572.                 start_pc = look_for_args_moves (gdbarch,
  573.                                                 here - insn_size, media_mode);
  574.                 break;
  575.               }
  576.         }
  577.     }

  578.   return start_pc;
  579. }

  580. static CORE_ADDR
  581. sh64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  582. {
  583.   CORE_ADDR post_prologue_pc;

  584.   /* See if we can determine the end of the prologue via the symbol table.
  585.      If so, then return either PC, or the PC after the prologue, whichever
  586.      is greater.  */
  587.   post_prologue_pc = after_prologue (pc);

  588.   /* If after_prologue returned a useful address, then use it.  Else
  589.      fall back on the instruction skipping code.  */
  590.   if (post_prologue_pc != 0)
  591.     return max (pc, post_prologue_pc);
  592.   else
  593.     return sh64_skip_prologue_hard_way (gdbarch, pc);
  594. }

  595. /* Should call_function allocate stack space for a struct return?  */
  596. static int
  597. sh64_use_struct_convention (struct type *type)
  598. {
  599.   return (TYPE_LENGTH (type) > 8);
  600. }

  601. /* For vectors of 4 floating point registers.  */
  602. static int
  603. sh64_fv_reg_base_num (struct gdbarch *gdbarch, int fv_regnum)
  604. {
  605.   int fp_regnum;

  606.   fp_regnum = gdbarch_fp0_regnum (gdbarch) + (fv_regnum - FV0_REGNUM) * 4;
  607.   return fp_regnum;
  608. }

  609. /* For double precision floating point registers, i.e 2 fp regs.  */
  610. static int
  611. sh64_dr_reg_base_num (struct gdbarch *gdbarch, int dr_regnum)
  612. {
  613.   int fp_regnum;

  614.   fp_regnum = gdbarch_fp0_regnum (gdbarch) + (dr_regnum - DR0_REGNUM) * 2;
  615.   return fp_regnum;
  616. }

  617. /* For pairs of floating point registers.  */
  618. static int
  619. sh64_fpp_reg_base_num (struct gdbarch *gdbarch, int fpp_regnum)
  620. {
  621.   int fp_regnum;

  622.   fp_regnum = gdbarch_fp0_regnum (gdbarch) + (fpp_regnum - FPP0_REGNUM) * 2;
  623.   return fp_regnum;
  624. }

  625. /* *INDENT-OFF* */
  626. /*
  627.     SH COMPACT MODE (ISA 16) (all pseudo) 221-272
  628.        GDB_REGNUM  BASE_REGNUM
  629. r0_c       221      0
  630. r1_c       222      1
  631. r2_c       223      2
  632. r3_c       224      3
  633. r4_c       225      4
  634. r5_c       226      5
  635. r6_c       227      6
  636. r7_c       228      7
  637. r8_c       229      8
  638. r9_c       230      9
  639. r10_c      231      10
  640. r11_c      232      11
  641. r12_c      233      12
  642. r13_c      234      13
  643. r14_c      235      14
  644. r15_c      236      15

  645. pc_c       237      64
  646. gbr_c      238      16
  647. mach_c     239      17
  648. macl_c     240      17
  649. pr_c       241      18
  650. t_c        242      19
  651. fpscr_c    243      76
  652. fpul_c     244      109

  653. fr0_c      245      77
  654. fr1_c      246      78
  655. fr2_c      247      79
  656. fr3_c      248      80
  657. fr4_c      249      81
  658. fr5_c      250      82
  659. fr6_c      251      83
  660. fr7_c      252      84
  661. fr8_c      253      85
  662. fr9_c      254      86
  663. fr10_c     255      87
  664. fr11_c     256      88
  665. fr12_c     257      89
  666. fr13_c     258      90
  667. fr14_c     259      91
  668. fr15_c     260      92

  669. dr0_c      261      77
  670. dr2_c      262      79
  671. dr4_c      263      81
  672. dr6_c      264      83
  673. dr8_c      265      85
  674. dr10_c     266      87
  675. dr12_c     267      89
  676. dr14_c     268      91

  677. fv0_c      269      77
  678. fv4_c      270      81
  679. fv8_c      271      85
  680. fv12_c     272      91
  681. */
  682. /* *INDENT-ON* */
  683. static int
  684. sh64_compact_reg_base_num (struct gdbarch *gdbarch, int reg_nr)
  685. {
  686.   int base_regnum = reg_nr;

  687.   /* general register N maps to general register N */
  688.   if (reg_nr >= R0_C_REGNUM
  689.       && reg_nr <= R_LAST_C_REGNUM)
  690.     base_regnum = reg_nr - R0_C_REGNUM;

  691.   /* floating point register N maps to floating point register N */
  692.   else if (reg_nr >= FP0_C_REGNUM
  693.             && reg_nr <= FP_LAST_C_REGNUM)
  694.     base_regnum = reg_nr - FP0_C_REGNUM + gdbarch_fp0_regnum (gdbarch);

  695.   /* double prec register N maps to base regnum for double prec register N */
  696.   else if (reg_nr >= DR0_C_REGNUM
  697.             && reg_nr <= DR_LAST_C_REGNUM)
  698.     base_regnum = sh64_dr_reg_base_num (gdbarch,
  699.                                         DR0_REGNUM + reg_nr - DR0_C_REGNUM);

  700.   /* vector N maps to base regnum for vector register N */
  701.   else if (reg_nr >= FV0_C_REGNUM
  702.             && reg_nr <= FV_LAST_C_REGNUM)
  703.     base_regnum = sh64_fv_reg_base_num (gdbarch,
  704.                                         FV0_REGNUM + reg_nr - FV0_C_REGNUM);

  705.   else if (reg_nr == PC_C_REGNUM)
  706.     base_regnum = gdbarch_pc_regnum (gdbarch);

  707.   else if (reg_nr == GBR_C_REGNUM)
  708.     base_regnum = 16;

  709.   else if (reg_nr == MACH_C_REGNUM
  710.            || reg_nr == MACL_C_REGNUM)
  711.     base_regnum = 17;

  712.   else if (reg_nr == PR_C_REGNUM)
  713.     base_regnum = PR_REGNUM;

  714.   else if (reg_nr == T_C_REGNUM)
  715.     base_regnum = 19;

  716.   else if (reg_nr == FPSCR_C_REGNUM)
  717.     base_regnum = FPSCR_REGNUM; /*???? this register is a mess.  */

  718.   else if (reg_nr == FPUL_C_REGNUM)
  719.     base_regnum = gdbarch_fp0_regnum (gdbarch) + 32;

  720.   return base_regnum;
  721. }

  722. static int
  723. sign_extend (int value, int bits)
  724. {
  725.   value = value & ((1 << bits) - 1);
  726.   return (value & (1 << (bits - 1))
  727.           ? value | (~((1 << bits) - 1))
  728.           : value);
  729. }

  730. static void
  731. sh64_analyze_prologue (struct gdbarch *gdbarch,
  732.                        struct sh64_frame_cache *cache,
  733.                        CORE_ADDR func_pc,
  734.                        CORE_ADDR current_pc)
  735. {
  736.   int pc;
  737.   int opc;
  738.   int insn;
  739.   int r0_val = 0;
  740.   int insn_size;
  741.   int gdb_register_number;
  742.   int register_number;
  743.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  744.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  745.   cache->sp_offset = 0;

  746.   /* Loop around examining the prologue insns until we find something
  747.      that does not appear to be part of the prologue.  But give up
  748.      after 20 of them, since we're getting silly then.  */

  749.   pc = func_pc;

  750.   if (cache->media_mode)
  751.     insn_size = 4;
  752.   else
  753.     insn_size = 2;

  754.   opc = pc + (insn_size * 28);
  755.   if (opc > current_pc)
  756.     opc = current_pc;
  757.   for ( ; pc <= opc; pc += insn_size)
  758.     {
  759.       insn = read_memory_integer (cache->media_mode ? UNMAKE_ISA32_ADDR (pc)
  760.                                                     : pc,
  761.                                   insn_size, byte_order);

  762.       if (!cache->media_mode)
  763.         {
  764.           if (IS_STS_PR (insn))
  765.             {
  766.               int next_insn = read_memory_integer (pc + insn_size,
  767.                                                    insn_size, byte_order);
  768.               if (IS_MOV_TO_R15 (next_insn))
  769.                 {
  770.                   cache->saved_regs[PR_REGNUM]
  771.                     = cache->sp_offset - ((((next_insn & 0xf) ^ 0x8)
  772.                                            - 0x8) << 2);
  773.                   pc += insn_size;
  774.                 }
  775.             }

  776.           else if (IS_MOV_R14 (insn))
  777.             cache->saved_regs[MEDIA_FP_REGNUM] =
  778.               cache->sp_offset - ((((insn & 0xf) ^ 0x8) - 0x8) << 2);

  779.           else if (IS_MOV_R0 (insn))
  780.             {
  781.               /* Put in R0 the offset from SP at which to store some
  782.                  registers.  We are interested in this value, because it
  783.                  will tell us where the given registers are stored within
  784.                  the frame.  */
  785.               r0_val = ((insn & 0xff) ^ 0x80) - 0x80;
  786.             }

  787.           else if (IS_ADD_SP_R0 (insn))
  788.             {
  789.               /* This instruction still prepares r0, but we don't care.
  790.                  We already have the offset in r0_val.  */
  791.             }

  792.           else if (IS_STS_R0 (insn))
  793.             {
  794.               /* Store PR at r0_val-4 from SP.  Decrement r0 by 4.  */
  795.               cache->saved_regs[PR_REGNUM] = cache->sp_offset - (r0_val - 4);
  796.               r0_val -= 4;
  797.             }

  798.           else if (IS_MOV_R14_R0 (insn))
  799.             {
  800.               /* Store R14 at r0_val-4 from SP.  Decrement r0 by 4.  */
  801.               cache->saved_regs[MEDIA_FP_REGNUM] = cache->sp_offset
  802.                                                          - (r0_val - 4);
  803.               r0_val -= 4;
  804.             }

  805.           else if (IS_ADD_SP (insn))
  806.             cache->sp_offset -= ((insn & 0xff) ^ 0x80) - 0x80;

  807.           else if (IS_MOV_SP_FP (insn))
  808.             break;
  809.         }
  810.       else
  811.         {
  812.           if (IS_ADDIL_SP_MEDIA (insn) || IS_ADDI_SP_MEDIA (insn))
  813.             cache->sp_offset -=
  814.               sign_extend ((((insn & 0xffc00) ^ 0x80000) - 0x80000) >> 10, 9);

  815.           else if (IS_STQ_R18_R15 (insn))
  816.             cache->saved_regs[PR_REGNUM]
  817.               = cache->sp_offset - (sign_extend ((insn & 0xffc00) >> 10,
  818.                                                  9) << 3);

  819.           else if (IS_STL_R18_R15 (insn))
  820.             cache->saved_regs[PR_REGNUM]
  821.               = cache->sp_offset - (sign_extend ((insn & 0xffc00) >> 10,
  822.                                                  9) << 2);

  823.           else if (IS_STQ_R14_R15 (insn))
  824.             cache->saved_regs[MEDIA_FP_REGNUM]
  825.               = cache->sp_offset - (sign_extend ((insn & 0xffc00) >> 10,
  826.                                                  9) << 3);

  827.           else if (IS_STL_R14_R15 (insn))
  828.             cache->saved_regs[MEDIA_FP_REGNUM]
  829.               = cache->sp_offset - (sign_extend ((insn & 0xffc00) >> 10,
  830.                                                  9) << 2);

  831.           else if (IS_MOV_SP_FP_MEDIA (insn))
  832.             break;
  833.         }
  834.     }

  835.   if (cache->saved_regs[MEDIA_FP_REGNUM] >= 0)
  836.     cache->uses_fp = 1;
  837. }

  838. static CORE_ADDR
  839. sh64_frame_align (struct gdbarch *ignore, CORE_ADDR sp)
  840. {
  841.   return sp & ~7;
  842. }

  843. /* Function: push_dummy_call
  844.    Setup the function arguments for calling a function in the inferior.

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

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

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

  860.    An exceptional case exists for struct arguments (and possibly other
  861.    aggregates such as arrays) if the size is larger than 4 bytes but
  862.    not a multiple of 4 bytes.  In this case the argument is never split
  863.    between the registers and the stack, but instead is copied in its
  864.    entirety onto the stack, AND also copied into as many registers as
  865.    there is room for.  In other words, space in registers permitting,
  866.    two copies of the same argument are passed in.  As far as I can tell,
  867.    only the one on the stack is used, although that may be a function
  868.    of the level of compiler optimization.  I suspect this is a compiler
  869.    bug.  Arguments of these odd sizes are left-justified within the
  870.    word (as opposed to arguments smaller than 4 bytes, which are
  871.    right-justified).

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

  880. /* R2-R9 for integer types and integer equivalent (char, pointers) and
  881.    non-scalar (struct, union) elements (even if the elements are
  882.    floats).
  883.    FR0-FR11 for single precision floating point (float)
  884.    DR0-DR10 for double precision floating point (double)

  885.    If a float is argument number 3 (for instance) and arguments number
  886.    1,2, and 4 are integer, the mapping will be:
  887.    arg1 -->R2, arg2 --> R3, arg3 -->FR0, arg4 --> R5.  I.e. R4 is not used.

  888.    If a float is argument number 10 (for instance) and arguments number
  889.    1 through 10 are integer, the mapping will be:
  890.    arg1->R2, arg2->R3, arg3->R4, arg4->R5, arg5->R6, arg6->R7, arg7->R8,
  891.    arg8->R9, arg9->(0,SP)stack(8-byte aligned), arg10->FR0,
  892.    arg11->stack(16,SP).  I.e. there is hole in the stack.

  893.    Different rules apply for variable arguments functions, and for functions
  894.    for which the prototype is not known.  */

  895. static CORE_ADDR
  896. sh64_push_dummy_call (struct gdbarch *gdbarch,
  897.                       struct value *function,
  898.                       struct regcache *regcache,
  899.                       CORE_ADDR bp_addr,
  900.                       int nargs, struct value **args,
  901.                       CORE_ADDR sp, int struct_return,
  902.                       CORE_ADDR struct_addr)
  903. {
  904.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  905.   int stack_offset, stack_alloc;
  906.   int int_argreg;
  907.   int float_argreg;
  908.   int double_argreg;
  909.   int float_arg_index = 0;
  910.   int double_arg_index = 0;
  911.   int argnum;
  912.   struct type *type;
  913.   CORE_ADDR regval;
  914.   const gdb_byte *val;
  915.   gdb_byte valbuf[8];
  916.   int len;
  917.   int argreg_size;
  918.   int fp_args[12];

  919.   memset (fp_args, 0, sizeof (fp_args));

  920.   /* First force sp to a 8-byte alignment.  */
  921.   sp = sh64_frame_align (gdbarch, sp);

  922.   /* The "struct return pointer" pseudo-argument has its own dedicated
  923.      register.  */

  924.   if (struct_return)
  925.     regcache_cooked_write_unsigned (regcache,
  926.                                     STRUCT_RETURN_REGNUM, struct_addr);

  927.   /* Now make sure there's space on the stack.  */
  928.   for (argnum = 0, stack_alloc = 0; argnum < nargs; argnum++)
  929.     stack_alloc += ((TYPE_LENGTH (value_type (args[argnum])) + 7) & ~7);
  930.   sp -= stack_alloc;                /* Make room on stack for args.  */

  931.   /* Now load as many as possible of the first arguments into
  932.      registers, and push the rest onto the stack.  There are 64 bytes
  933.      in eight registers available.  Loop thru args from first to last.  */

  934.   int_argreg = ARG0_REGNUM;
  935.   float_argreg = gdbarch_fp0_regnum (gdbarch);
  936.   double_argreg = DR0_REGNUM;

  937.   for (argnum = 0, stack_offset = 0; argnum < nargs; argnum++)
  938.     {
  939.       type = value_type (args[argnum]);
  940.       len = TYPE_LENGTH (type);
  941.       memset (valbuf, 0, sizeof (valbuf));

  942.       if (TYPE_CODE (type) != TYPE_CODE_FLT)
  943.         {
  944.           argreg_size = register_size (gdbarch, int_argreg);

  945.           if (len < argreg_size)
  946.             {
  947.               /* value gets right-justified in the register or stack word.  */
  948.               if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  949.                 memcpy (valbuf + argreg_size - len,
  950.                         value_contents (args[argnum]), len);
  951.               else
  952.                 memcpy (valbuf, value_contents (args[argnum]), len);

  953.               val = valbuf;
  954.             }
  955.           else
  956.             val = value_contents (args[argnum]);

  957.           while (len > 0)
  958.             {
  959.               if (int_argreg > ARGLAST_REGNUM)
  960.                 {
  961.                   /* Must go on the stack.  */
  962.                   write_memory (sp + stack_offset, val, argreg_size);
  963.                   stack_offset += 8;/*argreg_size;*/
  964.                 }
  965.               /* NOTE WELL!!!!!  This is not an "else if" clause!!!
  966.                  That's because some *&^%$ things get passed on the stack
  967.                  AND in the registers!   */
  968.               if (int_argreg <= ARGLAST_REGNUM)
  969.                 {
  970.                   /* There's room in a register.  */
  971.                   regval = extract_unsigned_integer (val, argreg_size,
  972.                                                      byte_order);
  973.                   regcache_cooked_write_unsigned (regcache,
  974.                                                   int_argreg, regval);
  975.                 }
  976.               /* Store the value 8 bytes at a time.  This means that
  977.                  things larger than 8 bytes may go partly in registers
  978.                  and partly on the stack.  FIXME: argreg is incremented
  979.                  before we use its size.  */
  980.               len -= argreg_size;
  981.               val += argreg_size;
  982.               int_argreg++;
  983.             }
  984.         }
  985.       else
  986.         {
  987.           val = value_contents (args[argnum]);
  988.           if (len == 4)
  989.             {
  990.               /* Where is it going to be stored?  */
  991.               while (fp_args[float_arg_index])
  992.                 float_arg_index ++;

  993.               /* Now float_argreg points to the register where it
  994.                  should be stored.  Are we still within the allowed
  995.                  register set?  */
  996.               if (float_arg_index <= FLOAT_ARGLAST_REGNUM)
  997.                 {
  998.                   /* Goes in FR0...FR11 */
  999.                   regcache_cooked_write (regcache,
  1000.                                          gdbarch_fp0_regnum (gdbarch)
  1001.                                          + float_arg_index,
  1002.                                          val);
  1003.                   fp_args[float_arg_index] = 1;
  1004.                   /* Skip the corresponding general argument register.  */
  1005.                   int_argreg ++;
  1006.                 }
  1007.               else
  1008.                 {
  1009.                   /* Store it as the integers, 8 bytes at the time, if
  1010.                      necessary spilling on the stack.  */
  1011.                 }
  1012.             }
  1013.             else if (len == 8)
  1014.               {
  1015.                 /* Where is it going to be stored?  */
  1016.                 while (fp_args[double_arg_index])
  1017.                   double_arg_index += 2;
  1018.                 /* Now double_argreg points to the register
  1019.                    where it should be stored.
  1020.                    Are we still within the allowed register set?  */
  1021.                 if (double_arg_index < FLOAT_ARGLAST_REGNUM)
  1022.                   {
  1023.                     /* Goes in DR0...DR10 */
  1024.                     /* The numbering of the DRi registers is consecutive,
  1025.                        i.e. includes odd numbers.  */
  1026.                     int double_register_offset = double_arg_index / 2;
  1027.                     int regnum = DR0_REGNUM + double_register_offset;
  1028.                     regcache_cooked_write (regcache, regnum, val);
  1029.                     fp_args[double_arg_index] = 1;
  1030.                     fp_args[double_arg_index + 1] = 1;
  1031.                     /* Skip the corresponding general argument register.  */
  1032.                     int_argreg ++;
  1033.                   }
  1034.                 else
  1035.                   {
  1036.                     /* Store it as the integers, 8 bytes at the time, if
  1037.                        necessary spilling on the stack.  */
  1038.                   }
  1039.               }
  1040.         }
  1041.     }
  1042.   /* Store return address.  */
  1043.   regcache_cooked_write_unsigned (regcache, PR_REGNUM, bp_addr);

  1044.   /* Update stack pointer.  */
  1045.   regcache_cooked_write_unsigned (regcache,
  1046.                                   gdbarch_sp_regnum (gdbarch), sp);

  1047.   return sp;
  1048. }

  1049. /* Find a function's return value in the appropriate registers (in
  1050.    regbuf), and copy it into valbuf.  Extract from an array REGBUF
  1051.    containing the (raw) register state a function return value of type
  1052.    TYPE, and copy that, in virtual format, into VALBUF.  */
  1053. static void
  1054. sh64_extract_return_value (struct type *type, struct regcache *regcache,
  1055.                            void *valbuf)
  1056. {
  1057.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1058.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1059.   int len = TYPE_LENGTH (type);

  1060.   if (TYPE_CODE (type) == TYPE_CODE_FLT)
  1061.     {
  1062.       if (len == 4)
  1063.         {
  1064.           /* Return value stored in gdbarch_fp0_regnum.  */
  1065.           regcache_raw_read (regcache,
  1066.                              gdbarch_fp0_regnum (gdbarch), valbuf);
  1067.         }
  1068.       else if (len == 8)
  1069.         {
  1070.           /* return value stored in DR0_REGNUM.  */
  1071.           DOUBLEST val;
  1072.           gdb_byte buf[8];

  1073.           regcache_cooked_read (regcache, DR0_REGNUM, buf);

  1074.           if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
  1075.             floatformat_to_doublest (&floatformat_ieee_double_littlebyte_bigword,
  1076.                                      buf, &val);
  1077.           else
  1078.             floatformat_to_doublest (&floatformat_ieee_double_big,
  1079.                                      buf, &val);
  1080.           store_typed_floating (valbuf, type, val);
  1081.         }
  1082.     }
  1083.   else
  1084.     {
  1085.       if (len <= 8)
  1086.         {
  1087.           int offset;
  1088.           gdb_byte buf[8];
  1089.           /* Result is in register 2.  If smaller than 8 bytes, it is padded
  1090.              at the most significant end.  */
  1091.           regcache_raw_read (regcache, DEFAULT_RETURN_REGNUM, buf);

  1092.           if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  1093.             offset = register_size (gdbarch, DEFAULT_RETURN_REGNUM)
  1094.                      - len;
  1095.           else
  1096.             offset = 0;
  1097.           memcpy (valbuf, buf + offset, len);
  1098.         }
  1099.       else
  1100.         error (_("bad size for return value"));
  1101.     }
  1102. }

  1103. /* Write into appropriate registers a function return value
  1104.    of type TYPE, given in virtual format.
  1105.    If the architecture is sh4 or sh3e, store a function's return value
  1106.    in the R0 general register or in the FP0 floating point register,
  1107.    depending on the type of the return value.  In all the other cases
  1108.    the result is stored in r0, left-justified.  */

  1109. static void
  1110. sh64_store_return_value (struct type *type, struct regcache *regcache,
  1111.                          const gdb_byte *valbuf)
  1112. {
  1113.   struct gdbarch *gdbarch = get_regcache_arch (regcache);
  1114.   gdb_byte buf[64];        /* more than enough...  */
  1115.   int len = TYPE_LENGTH (type);

  1116.   if (TYPE_CODE (type) == TYPE_CODE_FLT)
  1117.     {
  1118.       int i, regnum = gdbarch_fp0_regnum (gdbarch);
  1119.       for (i = 0; i < len; i += 4)
  1120.         if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
  1121.           regcache_raw_write (regcache, regnum++,
  1122.                               valbuf + len - 4 - i);
  1123.         else
  1124.           regcache_raw_write (regcache, regnum++, valbuf + i);
  1125.     }
  1126.   else
  1127.     {
  1128.       int return_register = DEFAULT_RETURN_REGNUM;
  1129.       int offset = 0;

  1130.       if (len <= register_size (gdbarch, return_register))
  1131.         {
  1132.           /* Pad with zeros.  */
  1133.           memset (buf, 0, register_size (gdbarch, return_register));
  1134.           if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
  1135.             offset = 0; /*register_size (gdbarch,
  1136.                           return_register) - len;*/
  1137.           else
  1138.             offset = register_size (gdbarch, return_register) - len;

  1139.           memcpy (buf + offset, valbuf, len);
  1140.           regcache_raw_write (regcache, return_register, buf);
  1141.         }
  1142.       else
  1143.         regcache_raw_write (regcache, return_register, valbuf);
  1144.     }
  1145. }

  1146. static enum return_value_convention
  1147. sh64_return_value (struct gdbarch *gdbarch, struct value *function,
  1148.                    struct type *type, struct regcache *regcache,
  1149.                    gdb_byte *readbuf, const gdb_byte *writebuf)
  1150. {
  1151.   if (sh64_use_struct_convention (type))
  1152.     return RETURN_VALUE_STRUCT_CONVENTION;
  1153.   if (writebuf)
  1154.     sh64_store_return_value (type, regcache, writebuf);
  1155.   else if (readbuf)
  1156.     sh64_extract_return_value (type, regcache, readbuf);
  1157.   return RETURN_VALUE_REGISTER_CONVENTION;
  1158. }

  1159. /* *INDENT-OFF* */
  1160. /*
  1161.     SH MEDIA MODE (ISA 32)
  1162.     general registers (64-bit) 0-63
  1163. 0    r0,   r1,   r2,   r3,   r4,   r5,   r6,   r7,
  1164. 64   r8,   r9,   r10,  r11,  r12,  r13,  r14,  r15,
  1165. 128  r16,  r17,  r18,  r19,  r20,  r21,  r22,  r23,
  1166. 192  r24,  r25,  r26,  r27,  r28,  r29,  r30,  r31,
  1167. 256  r32,  r33,  r34,  r35,  r36,  r37,  r38,  r39,
  1168. 320  r40,  r41,  r42,  r43,  r44,  r45,  r46,  r47,
  1169. 384  r48,  r49,  r50,  r51,  r52,  r53,  r54,  r55,
  1170. 448  r56,  r57,  r58,  r59,  r60,  r61,  r62,  r63,

  1171.     pc (64-bit) 64
  1172. 512  pc,

  1173.     status reg., saved status reg., saved pc reg. (64-bit) 65-67
  1174. 520  sr,  ssr,  spc,

  1175.     target registers (64-bit) 68-75
  1176. 544  tr0,  tr1,  tr2,  tr3,  tr4,  tr5,  tr6,  tr7,

  1177.     floating point state control register (32-bit) 76
  1178. 608  fpscr,

  1179.     single precision floating point registers (32-bit) 77-140
  1180. 612  fr0,  fr1,  fr2,  fr3,  fr4,  fr5,  fr6,  fr7,
  1181. 644  fr8,  fr9,  fr10, fr11, fr12, fr13, fr14, fr15,
  1182. 676  fr16, fr17, fr18, fr19, fr20, fr21, fr22, fr23,
  1183. 708  fr24, fr25, fr26, fr27, fr28, fr29, fr30, fr31,
  1184. 740  fr32, fr33, fr34, fr35, fr36, fr37, fr38, fr39,
  1185. 772  fr40, fr41, fr42, fr43, fr44, fr45, fr46, fr47,
  1186. 804  fr48, fr49, fr50, fr51, fr52, fr53, fr54, fr55,
  1187. 836  fr56, fr57, fr58, fr59, fr60, fr61, fr62, fr63,

  1188. TOTAL SPACE FOR REGISTERS: 868 bytes

  1189. From here on they are all pseudo registers: no memory allocated.
  1190. REGISTER_BYTE returns the register byte for the base register.

  1191.     double precision registers (pseudo) 141-172
  1192.      dr0,  dr2,  dr4,  dr6,  dr8,  dr10, dr12, dr14,
  1193.      dr16, dr18, dr20, dr22, dr24, dr26, dr28, dr30,
  1194.      dr32, dr34, dr36, dr38, dr40, dr42, dr44, dr46,
  1195.      dr48, dr50, dr52, dr54, dr56, dr58, dr60, dr62,

  1196.     floating point pairs (pseudo) 173-204
  1197.      fp0,  fp2,  fp4,  fp6,  fp8,  fp10, fp12, fp14,
  1198.      fp16, fp18, fp20, fp22, fp24, fp26, fp28, fp30,
  1199.      fp32, fp34, fp36, fp38, fp40, fp42, fp44, fp46,
  1200.      fp48, fp50, fp52, fp54, fp56, fp58, fp60, fp62,

  1201.     floating point vectors (4 floating point regs) (pseudo) 205-220
  1202.      fv0,  fv4,  fv8,  fv12, fv16, fv20, fv24, fv28,
  1203.      fv32, fv36, fv40, fv44, fv48, fv52, fv56, fv60,

  1204.     SH COMPACT MODE (ISA 16) (all pseudo) 221-272
  1205.      r0_c, r1_c, r2_c,  r3_c,  r4_c,  r5_c,  r6_c,  r7_c,
  1206.      r8_c, r9_c, r10_c, r11_c, r12_c, r13_c, r14_c, r15_c,
  1207.      pc_c,
  1208.      gbr_c, mach_c, macl_c, pr_c, t_c,
  1209.      fpscr_c, fpul_c,
  1210.      fr0_c, fr1_c, fr2_c,  fr3_c,  fr4_c,  fr5_c,  fr6_c,  fr7_c,
  1211.      fr8_c, fr9_c, fr10_c, fr11_c, fr12_c, fr13_c, fr14_c, fr15_c
  1212.      dr0_c, dr2_c, dr4_c,  dr6_c,  dr8_c,  dr10_c, dr12_c, dr14_c
  1213.      fv0_c, fv4_c, fv8_c,  fv12_c
  1214. */

  1215. static struct type *
  1216. sh64_build_float_register_type (struct gdbarch *gdbarch, int high)
  1217. {
  1218.   return lookup_array_range_type (builtin_type (gdbarch)->builtin_float,
  1219.                                   0, high);
  1220. }

  1221. /* Return the GDB type object for the "standard" data type
  1222.    of data in register REG_NR.  */
  1223. static struct type *
  1224. sh64_register_type (struct gdbarch *gdbarch, int reg_nr)
  1225. {
  1226.   if ((reg_nr >= gdbarch_fp0_regnum (gdbarch)
  1227.        && reg_nr <= FP_LAST_REGNUM)
  1228.       || (reg_nr >= FP0_C_REGNUM
  1229.           && reg_nr <= FP_LAST_C_REGNUM))
  1230.     return builtin_type (gdbarch)->builtin_float;
  1231.   else if ((reg_nr >= DR0_REGNUM
  1232.             && reg_nr <= DR_LAST_REGNUM)
  1233.            || (reg_nr >= DR0_C_REGNUM
  1234.                && reg_nr <= DR_LAST_C_REGNUM))
  1235.     return builtin_type (gdbarch)->builtin_double;
  1236.   else if  (reg_nr >= FPP0_REGNUM
  1237.             && reg_nr <= FPP_LAST_REGNUM)
  1238.     return sh64_build_float_register_type (gdbarch, 1);
  1239.   else if ((reg_nr >= FV0_REGNUM
  1240.             && reg_nr <= FV_LAST_REGNUM)
  1241.            ||(reg_nr >= FV0_C_REGNUM
  1242.               && reg_nr <= FV_LAST_C_REGNUM))
  1243.     return sh64_build_float_register_type (gdbarch, 3);
  1244.   else if (reg_nr == FPSCR_REGNUM)
  1245.     return builtin_type (gdbarch)->builtin_int;
  1246.   else if (reg_nr >= R0_C_REGNUM
  1247.            && reg_nr < FP0_C_REGNUM)
  1248.     return builtin_type (gdbarch)->builtin_int;
  1249.   else
  1250.     return builtin_type (gdbarch)->builtin_long_long;
  1251. }

  1252. static void
  1253. sh64_register_convert_to_virtual (struct gdbarch *gdbarch, int regnum,
  1254.                                   struct type *type, gdb_byte *from, gdb_byte *to)
  1255. {
  1256.   if (gdbarch_byte_order (gdbarch) != BFD_ENDIAN_LITTLE)
  1257.     {
  1258.       /* It is a no-op.  */
  1259.       memcpy (to, from, register_size (gdbarch, regnum));
  1260.       return;
  1261.     }

  1262.   if ((regnum >= DR0_REGNUM
  1263.        && regnum <= DR_LAST_REGNUM)
  1264.       || (regnum >= DR0_C_REGNUM
  1265.           && regnum <= DR_LAST_C_REGNUM))
  1266.     {
  1267.       DOUBLEST val;
  1268.       floatformat_to_doublest (&floatformat_ieee_double_littlebyte_bigword,
  1269.                                from, &val);
  1270.       store_typed_floating (to, type, val);
  1271.     }
  1272.   else
  1273.     error (_("sh64_register_convert_to_virtual "
  1274.              "called with non DR register number"));
  1275. }

  1276. static void
  1277. sh64_register_convert_to_raw (struct gdbarch *gdbarch, struct type *type,
  1278.                               int regnum, const void *from, void *to)
  1279. {
  1280.   if (gdbarch_byte_order (gdbarch) != BFD_ENDIAN_LITTLE)
  1281.     {
  1282.       /* It is a no-op.  */
  1283.       memcpy (to, from, register_size (gdbarch, regnum));
  1284.       return;
  1285.     }

  1286.   if ((regnum >= DR0_REGNUM
  1287.        && regnum <= DR_LAST_REGNUM)
  1288.       || (regnum >= DR0_C_REGNUM
  1289.           && regnum <= DR_LAST_C_REGNUM))
  1290.     {
  1291.       DOUBLEST val = extract_typed_floating (from, type);
  1292.       floatformat_from_doublest (&floatformat_ieee_double_littlebyte_bigword,
  1293.                                  &val, to);
  1294.     }
  1295.   else
  1296.     error (_("sh64_register_convert_to_raw called "
  1297.              "with non DR register number"));
  1298. }

  1299. /* Concatenate PORTIONS contiguous raw registers starting at
  1300.    BASE_REGNUM into BUFFER.  */

  1301. static enum register_status
  1302. pseudo_register_read_portions (struct gdbarch *gdbarch,
  1303.                                struct regcache *regcache,
  1304.                                int portions,
  1305.                                int base_regnum, gdb_byte *buffer)
  1306. {
  1307.   int portion;

  1308.   for (portion = 0; portion < portions; portion++)
  1309.     {
  1310.       enum register_status status;
  1311.       gdb_byte *b;

  1312.       b = buffer + register_size (gdbarch, base_regnum) * portion;
  1313.       status = regcache_raw_read (regcache, base_regnum + portion, b);
  1314.       if (status != REG_VALID)
  1315.         return status;
  1316.     }

  1317.   return REG_VALID;
  1318. }

  1319. static enum register_status
  1320. sh64_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
  1321.                            int reg_nr, gdb_byte *buffer)
  1322. {
  1323.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1324.   int base_regnum;
  1325.   int offset = 0;
  1326.   gdb_byte temp_buffer[MAX_REGISTER_SIZE];
  1327.   enum register_status status;

  1328.   if (reg_nr >= DR0_REGNUM
  1329.       && reg_nr <= DR_LAST_REGNUM)
  1330.     {
  1331.       base_regnum = sh64_dr_reg_base_num (gdbarch, reg_nr);

  1332.       /* Build the value in the provided buffer.  */
  1333.       /* DR regs are double precision registers obtained by
  1334.          concatenating 2 single precision floating point registers.  */
  1335.       status = pseudo_register_read_portions (gdbarch, regcache,
  1336.                                               2, base_regnum, temp_buffer);
  1337.       if (status == REG_VALID)
  1338.         {
  1339.           /* We must pay attention to the endianness.  */
  1340.           sh64_register_convert_to_virtual (gdbarch, reg_nr,
  1341.                                             register_type (gdbarch, reg_nr),
  1342.                                             temp_buffer, buffer);
  1343.         }

  1344.       return status;
  1345.     }

  1346.   else if (reg_nr >= FPP0_REGNUM
  1347.            && reg_nr <= FPP_LAST_REGNUM)
  1348.     {
  1349.       base_regnum = sh64_fpp_reg_base_num (gdbarch, reg_nr);

  1350.       /* Build the value in the provided buffer.  */
  1351.       /* FPP regs are pairs of single precision registers obtained by
  1352.          concatenating 2 single precision floating point registers.  */
  1353.       return pseudo_register_read_portions (gdbarch, regcache,
  1354.                                             2, base_regnum, buffer);
  1355.     }

  1356.   else if (reg_nr >= FV0_REGNUM
  1357.            && reg_nr <= FV_LAST_REGNUM)
  1358.     {
  1359.       base_regnum = sh64_fv_reg_base_num (gdbarch, reg_nr);

  1360.       /* Build the value in the provided buffer.  */
  1361.       /* FV regs are vectors of single precision registers obtained by
  1362.          concatenating 4 single precision floating point registers.  */
  1363.       return pseudo_register_read_portions (gdbarch, regcache,
  1364.                                             4, base_regnum, buffer);
  1365.     }

  1366.   /* sh compact pseudo registers.  1-to-1 with a shmedia register.  */
  1367.   else if (reg_nr >= R0_C_REGNUM
  1368.            && reg_nr <= T_C_REGNUM)
  1369.     {
  1370.       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);

  1371.       /* Build the value in the provided buffer.  */
  1372.       status = regcache_raw_read (regcache, base_regnum, temp_buffer);
  1373.       if (status != REG_VALID)
  1374.         return status;
  1375.       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  1376.         offset = 4;
  1377.       memcpy (buffer,
  1378.               temp_buffer + offset, 4); /* get LOWER 32 bits only????  */
  1379.       return REG_VALID;
  1380.     }

  1381.   else if (reg_nr >= FP0_C_REGNUM
  1382.            && reg_nr <= FP_LAST_C_REGNUM)
  1383.     {
  1384.       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);

  1385.       /* Build the value in the provided buffer.  */
  1386.       /* Floating point registers map 1-1 to the media fp regs,
  1387.          they have the same size and endianness.  */
  1388.       return regcache_raw_read (regcache, base_regnum, buffer);
  1389.     }

  1390.   else if (reg_nr >= DR0_C_REGNUM
  1391.            && reg_nr <= DR_LAST_C_REGNUM)
  1392.     {
  1393.       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);

  1394.       /* DR_C regs are double precision registers obtained by
  1395.          concatenating 2 single precision floating point registers.  */
  1396.       status = pseudo_register_read_portions (gdbarch, regcache,
  1397.                                               2, base_regnum, temp_buffer);
  1398.       if (status == REG_VALID)
  1399.         {
  1400.           /* We must pay attention to the endianness.  */
  1401.           sh64_register_convert_to_virtual (gdbarch, reg_nr,
  1402.                                             register_type (gdbarch, reg_nr),
  1403.                                             temp_buffer, buffer);
  1404.         }
  1405.       return status;
  1406.     }

  1407.   else if (reg_nr >= FV0_C_REGNUM
  1408.            && reg_nr <= FV_LAST_C_REGNUM)
  1409.     {
  1410.       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);

  1411.       /* Build the value in the provided buffer.  */
  1412.       /* FV_C regs are vectors of single precision registers obtained by
  1413.          concatenating 4 single precision floating point registers.  */
  1414.       return pseudo_register_read_portions (gdbarch, regcache,
  1415.                                             4, base_regnum, buffer);
  1416.     }

  1417.   else if (reg_nr == FPSCR_C_REGNUM)
  1418.     {
  1419.       int fpscr_base_regnum;
  1420.       int sr_base_regnum;
  1421.       unsigned int fpscr_value;
  1422.       unsigned int sr_value;
  1423.       unsigned int fpscr_c_value;
  1424.       unsigned int fpscr_c_part1_value;
  1425.       unsigned int fpscr_c_part2_value;

  1426.       fpscr_base_regnum = FPSCR_REGNUM;
  1427.       sr_base_regnum = SR_REGNUM;

  1428.       /* Build the value in the provided buffer.  */
  1429.       /* FPSCR_C is a very weird register that contains sparse bits
  1430.          from the FPSCR and the SR architectural registers.
  1431.          Specifically: */
  1432.       /* *INDENT-OFF* */
  1433.       /*
  1434.          FPSRC_C bit
  1435.             0         Bit 0 of FPSCR
  1436.             1         reserved
  1437.             2-17      Bit 2-18 of FPSCR
  1438.             18-20     Bits 12,13,14 of SR
  1439.             21-31     reserved
  1440.        */
  1441.       /* *INDENT-ON* */
  1442.       /* Get FPSCR into a local buffer.  */
  1443.       status = regcache_raw_read (regcache, fpscr_base_regnum, temp_buffer);
  1444.       if (status != REG_VALID)
  1445.         return status;
  1446.       /* Get value as an int.  */
  1447.       fpscr_value = extract_unsigned_integer (temp_buffer, 4, byte_order);
  1448.       /* Get SR into a local buffer */
  1449.       status = regcache_raw_read (regcache, sr_base_regnum, temp_buffer);
  1450.       if (status != REG_VALID)
  1451.         return status;
  1452.       /* Get value as an int.  */
  1453.       sr_value = extract_unsigned_integer (temp_buffer, 4, byte_order);
  1454.       /* Build the new value.  */
  1455.       fpscr_c_part1_value = fpscr_value & 0x3fffd;
  1456.       fpscr_c_part2_value = (sr_value & 0x7000) << 6;
  1457.       fpscr_c_value = fpscr_c_part1_value | fpscr_c_part2_value;
  1458.       /* Store that in out buffer!!!  */
  1459.       store_unsigned_integer (buffer, 4, byte_order, fpscr_c_value);
  1460.       /* FIXME There is surely an endianness gotcha here.  */

  1461.       return REG_VALID;
  1462.     }

  1463.   else if (reg_nr == FPUL_C_REGNUM)
  1464.     {
  1465.       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);

  1466.       /* FPUL_C register is floating point register 32,
  1467.          same size, same endianness.  */
  1468.       return regcache_raw_read (regcache, base_regnum, buffer);
  1469.     }
  1470.   else
  1471.     gdb_assert_not_reached ("invalid pseudo register number");
  1472. }

  1473. static void
  1474. sh64_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
  1475.                             int reg_nr, const gdb_byte *buffer)
  1476. {
  1477.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1478.   int base_regnum, portion;
  1479.   int offset;
  1480.   gdb_byte temp_buffer[MAX_REGISTER_SIZE];

  1481.   if (reg_nr >= DR0_REGNUM
  1482.       && reg_nr <= DR_LAST_REGNUM)
  1483.     {
  1484.       base_regnum = sh64_dr_reg_base_num (gdbarch, reg_nr);
  1485.       /* We must pay attention to the endianness.  */
  1486.       sh64_register_convert_to_raw (gdbarch, register_type (gdbarch, reg_nr),
  1487.                                     reg_nr,
  1488.                                     buffer, temp_buffer);

  1489.       /* Write the real regs for which this one is an alias.  */
  1490.       for (portion = 0; portion < 2; portion++)
  1491.         regcache_raw_write (regcache, base_regnum + portion,
  1492.                             (temp_buffer
  1493.                              + register_size (gdbarch,
  1494.                                               base_regnum) * portion));
  1495.     }

  1496.   else if (reg_nr >= FPP0_REGNUM
  1497.            && reg_nr <= FPP_LAST_REGNUM)
  1498.     {
  1499.       base_regnum = sh64_fpp_reg_base_num (gdbarch, reg_nr);

  1500.       /* Write the real regs for which this one is an alias.  */
  1501.       for (portion = 0; portion < 2; portion++)
  1502.         regcache_raw_write (regcache, base_regnum + portion,
  1503.                             (buffer + register_size (gdbarch,
  1504.                                                      base_regnum) * portion));
  1505.     }

  1506.   else if (reg_nr >= FV0_REGNUM
  1507.            && reg_nr <= FV_LAST_REGNUM)
  1508.     {
  1509.       base_regnum = sh64_fv_reg_base_num (gdbarch, reg_nr);

  1510.       /* Write the real regs for which this one is an alias.  */
  1511.       for (portion = 0; portion < 4; portion++)
  1512.         regcache_raw_write (regcache, base_regnum + portion,
  1513.                             (buffer + register_size (gdbarch,
  1514.                                                      base_regnum) * portion));
  1515.     }

  1516.   /* sh compact general pseudo registers.  1-to-1 with a shmedia
  1517.      register but only 4 bytes of it.  */
  1518.   else if (reg_nr >= R0_C_REGNUM
  1519.            && reg_nr <= T_C_REGNUM)
  1520.     {
  1521.       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
  1522.       /* reg_nr is 32 bit here, and base_regnum is 64 bits.  */
  1523.       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  1524.         offset = 4;
  1525.       else
  1526.         offset = 0;
  1527.       /* Let's read the value of the base register into a temporary
  1528.          buffer, so that overwriting the last four bytes with the new
  1529.          value of the pseudo will leave the upper 4 bytes unchanged.  */
  1530.       regcache_raw_read (regcache, base_regnum, temp_buffer);
  1531.       /* Write as an 8 byte quantity.  */
  1532.       memcpy (temp_buffer + offset, buffer, 4);
  1533.       regcache_raw_write (regcache, base_regnum, temp_buffer);
  1534.     }

  1535.   /* sh floating point compact pseudo registers.  1-to-1 with a shmedia
  1536.      registers.  Both are 4 bytes.  */
  1537.   else if (reg_nr >= FP0_C_REGNUM
  1538.                && reg_nr <= FP_LAST_C_REGNUM)
  1539.     {
  1540.       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
  1541.       regcache_raw_write (regcache, base_regnum, buffer);
  1542.     }

  1543.   else if (reg_nr >= DR0_C_REGNUM
  1544.            && reg_nr <= DR_LAST_C_REGNUM)
  1545.     {
  1546.       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
  1547.       for (portion = 0; portion < 2; portion++)
  1548.         {
  1549.           /* We must pay attention to the endianness.  */
  1550.           sh64_register_convert_to_raw (gdbarch,
  1551.                                         register_type (gdbarch, reg_nr),
  1552.                                         reg_nr,
  1553.                                         buffer, temp_buffer);

  1554.           regcache_raw_write (regcache, base_regnum + portion,
  1555.                               (temp_buffer
  1556.                                + register_size (gdbarch,
  1557.                                                 base_regnum) * portion));
  1558.         }
  1559.     }

  1560.   else if (reg_nr >= FV0_C_REGNUM
  1561.            && reg_nr <= FV_LAST_C_REGNUM)
  1562.     {
  1563.       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);

  1564.       for (portion = 0; portion < 4; portion++)
  1565.         {
  1566.           regcache_raw_write (regcache, base_regnum + portion,
  1567.                               (buffer
  1568.                                + register_size (gdbarch,
  1569.                                                 base_regnum) * portion));
  1570.         }
  1571.     }

  1572.   else if (reg_nr == FPSCR_C_REGNUM)
  1573.     {
  1574.       int fpscr_base_regnum;
  1575.       int sr_base_regnum;
  1576.       unsigned int fpscr_value;
  1577.       unsigned int sr_value;
  1578.       unsigned int old_fpscr_value;
  1579.       unsigned int old_sr_value;
  1580.       unsigned int fpscr_c_value;
  1581.       unsigned int fpscr_mask;
  1582.       unsigned int sr_mask;

  1583.       fpscr_base_regnum = FPSCR_REGNUM;
  1584.       sr_base_regnum = SR_REGNUM;

  1585.       /* FPSCR_C is a very weird register that contains sparse bits
  1586.          from the FPSCR and the SR architectural registers.
  1587.          Specifically: */
  1588.       /* *INDENT-OFF* */
  1589.       /*
  1590.          FPSRC_C bit
  1591.             0         Bit 0 of FPSCR
  1592.             1         reserved
  1593.             2-17      Bit 2-18 of FPSCR
  1594.             18-20     Bits 12,13,14 of SR
  1595.             21-31     reserved
  1596.        */
  1597.       /* *INDENT-ON* */
  1598.       /* Get value as an int.  */
  1599.       fpscr_c_value = extract_unsigned_integer (buffer, 4, byte_order);

  1600.       /* Build the new values.  */
  1601.       fpscr_mask = 0x0003fffd;
  1602.       sr_mask = 0x001c0000;

  1603.       fpscr_value = fpscr_c_value & fpscr_mask;
  1604.       sr_value = (fpscr_value & sr_mask) >> 6;

  1605.       regcache_raw_read (regcache, fpscr_base_regnum, temp_buffer);
  1606.       old_fpscr_value = extract_unsigned_integer (temp_buffer, 4, byte_order);
  1607.       old_fpscr_value &= 0xfffc0002;
  1608.       fpscr_value |= old_fpscr_value;
  1609.       store_unsigned_integer (temp_buffer, 4, byte_order, fpscr_value);
  1610.       regcache_raw_write (regcache, fpscr_base_regnum, temp_buffer);

  1611.       regcache_raw_read (regcache, sr_base_regnum, temp_buffer);
  1612.       old_sr_value = extract_unsigned_integer (temp_buffer, 4, byte_order);
  1613.       old_sr_value &= 0xffff8fff;
  1614.       sr_value |= old_sr_value;
  1615.       store_unsigned_integer (temp_buffer, 4, byte_order, sr_value);
  1616.       regcache_raw_write (regcache, sr_base_regnum, temp_buffer);
  1617.     }

  1618.   else if (reg_nr == FPUL_C_REGNUM)
  1619.     {
  1620.       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
  1621.       regcache_raw_write (regcache, base_regnum, buffer);
  1622.     }
  1623. }

  1624. /* FIXME:!! THIS SHOULD TAKE CARE OF GETTING THE RIGHT PORTION OF THE
  1625.    shmedia REGISTERS.  */
  1626. /* Control registers, compact mode.  */
  1627. static void
  1628. sh64_do_cr_c_register_info (struct ui_file *file, struct frame_info *frame,
  1629.                             int cr_c_regnum)
  1630. {
  1631.   switch (cr_c_regnum)
  1632.     {
  1633.     case PC_C_REGNUM:
  1634.       fprintf_filtered (file, "pc_c\t0x%08x\n",
  1635.           (int) get_frame_register_unsigned (frame, cr_c_regnum));
  1636.       break;
  1637.     case GBR_C_REGNUM:
  1638.       fprintf_filtered (file, "gbr_c\t0x%08x\n",
  1639.                 (int) get_frame_register_unsigned (frame, cr_c_regnum));
  1640.       break;
  1641.     case MACH_C_REGNUM:
  1642.       fprintf_filtered (file, "mach_c\t0x%08x\n",
  1643.                 (int) get_frame_register_unsigned (frame, cr_c_regnum));
  1644.       break;
  1645.     case MACL_C_REGNUM:
  1646.       fprintf_filtered (file, "macl_c\t0x%08x\n",
  1647.                 (int) get_frame_register_unsigned (frame, cr_c_regnum));
  1648.       break;
  1649.     case PR_C_REGNUM:
  1650.       fprintf_filtered (file, "pr_c\t0x%08x\n",
  1651.                 (int) get_frame_register_unsigned (frame, cr_c_regnum));
  1652.       break;
  1653.     case T_C_REGNUM:
  1654.       fprintf_filtered (file, "t_c\t0x%08x\n",
  1655.                 (int) get_frame_register_unsigned (frame, cr_c_regnum));
  1656.       break;
  1657.     case FPSCR_C_REGNUM:
  1658.       fprintf_filtered (file, "fpscr_c\t0x%08x\n",
  1659.                 (int) get_frame_register_unsigned (frame, cr_c_regnum));
  1660.       break;
  1661.     case FPUL_C_REGNUM:
  1662.       fprintf_filtered (file, "fpul_c\t0x%08x\n",
  1663.           (int) get_frame_register_unsigned (frame, cr_c_regnum));
  1664.       break;
  1665.     }
  1666. }

  1667. static void
  1668. sh64_do_fp_register (struct gdbarch *gdbarch, struct ui_file *file,
  1669.                      struct frame_info *frame, int regnum)
  1670. {                                /* Do values for FP (float) regs.  */
  1671.   unsigned char *raw_buffer;
  1672.   double flt;        /* Double extracted from raw hex data.  */
  1673.   int inv;
  1674.   int j;

  1675.   /* Allocate space for the float.  */
  1676.   raw_buffer = (unsigned char *)
  1677.     alloca (register_size (gdbarch, gdbarch_fp0_regnum (gdbarch)));

  1678.   /* Get the data in raw format.  */
  1679.   if (!deprecated_frame_register_read (frame, regnum, raw_buffer))
  1680.     error (_("can't read register %d (%s)"),
  1681.            regnum, gdbarch_register_name (gdbarch, regnum));

  1682.   /* Get the register as a number.  */
  1683.   flt = unpack_double (builtin_type (gdbarch)->builtin_float,
  1684.                        raw_buffer, &inv);

  1685.   /* Print the name and some spaces.  */
  1686.   fputs_filtered (gdbarch_register_name (gdbarch, regnum), file);
  1687.   print_spaces_filtered (15 - strlen (gdbarch_register_name
  1688.                                         (gdbarch, regnum)), file);

  1689.   /* Print the value.  */
  1690.   if (inv)
  1691.     fprintf_filtered (file, "<invalid float>");
  1692.   else
  1693.     fprintf_filtered (file, "%-10.9g", flt);

  1694.   /* Print the fp register as hex.  */
  1695.   fprintf_filtered (file, "\t(raw ");
  1696.   print_hex_chars (file, raw_buffer,
  1697.                    register_size (gdbarch, regnum),
  1698.                    gdbarch_byte_order (gdbarch));
  1699.   fprintf_filtered (file, ")");
  1700.   fprintf_filtered (file, "\n");
  1701. }

  1702. static void
  1703. sh64_do_pseudo_register (struct gdbarch *gdbarch, struct ui_file *file,
  1704.                          struct frame_info *frame, int regnum)
  1705. {
  1706.   /* All the sh64-compact mode registers are pseudo registers.  */

  1707.   if (regnum < gdbarch_num_regs (gdbarch)
  1708.       || regnum >= gdbarch_num_regs (gdbarch)
  1709.                    + NUM_PSEUDO_REGS_SH_MEDIA
  1710.                    + NUM_PSEUDO_REGS_SH_COMPACT)
  1711.     internal_error (__FILE__, __LINE__,
  1712.                     _("Invalid pseudo register number %d\n"), regnum);

  1713.   else if ((regnum >= DR0_REGNUM && regnum <= DR_LAST_REGNUM))
  1714.     {
  1715.       int fp_regnum = sh64_dr_reg_base_num (gdbarch, regnum);
  1716.       fprintf_filtered (file, "dr%d\t0x%08x%08x\n", regnum - DR0_REGNUM,
  1717.           (unsigned) get_frame_register_unsigned (frame, fp_regnum),
  1718.           (unsigned) get_frame_register_unsigned (frame, fp_regnum + 1));
  1719.     }

  1720.   else if ((regnum >= DR0_C_REGNUM && regnum <= DR_LAST_C_REGNUM))
  1721.     {
  1722.       int fp_regnum = sh64_compact_reg_base_num (gdbarch, regnum);
  1723.       fprintf_filtered (file, "dr%d_c\t0x%08x%08x\n", regnum - DR0_C_REGNUM,
  1724.           (unsigned) get_frame_register_unsigned (frame, fp_regnum),
  1725.           (unsigned) get_frame_register_unsigned (frame, fp_regnum + 1));
  1726.     }

  1727.   else if ((regnum >= FV0_REGNUM && regnum <= FV_LAST_REGNUM))
  1728.     {
  1729.       int fp_regnum = sh64_fv_reg_base_num (gdbarch, regnum);
  1730.       fprintf_filtered (file, "fv%d\t0x%08x\t0x%08x\t0x%08x\t0x%08x\n",
  1731.            regnum - FV0_REGNUM,
  1732.            (unsigned) get_frame_register_unsigned (frame, fp_regnum),
  1733.            (unsigned) get_frame_register_unsigned (frame, fp_regnum + 1),
  1734.            (unsigned) get_frame_register_unsigned (frame, fp_regnum + 2),
  1735.            (unsigned) get_frame_register_unsigned (frame, fp_regnum + 3));
  1736.     }

  1737.   else if ((regnum >= FV0_C_REGNUM && regnum <= FV_LAST_C_REGNUM))
  1738.     {
  1739.       int fp_regnum = sh64_compact_reg_base_num (gdbarch, regnum);
  1740.       fprintf_filtered (file, "fv%d_c\t0x%08x\t0x%08x\t0x%08x\t0x%08x\n",
  1741.            regnum - FV0_C_REGNUM,
  1742.            (unsigned) get_frame_register_unsigned (frame, fp_regnum),
  1743.            (unsigned) get_frame_register_unsigned (frame, fp_regnum + 1),
  1744.            (unsigned) get_frame_register_unsigned (frame, fp_regnum + 2),
  1745.            (unsigned) get_frame_register_unsigned (frame, fp_regnum + 3));
  1746.     }

  1747.   else if (regnum >= FPP0_REGNUM && regnum <= FPP_LAST_REGNUM)
  1748.     {
  1749.       int fp_regnum = sh64_fpp_reg_base_num (gdbarch, regnum);
  1750.       fprintf_filtered (file, "fpp%d\t0x%08x\t0x%08x\n", regnum - FPP0_REGNUM,
  1751.           (unsigned) get_frame_register_unsigned (frame, fp_regnum),
  1752.           (unsigned) get_frame_register_unsigned (frame, fp_regnum + 1));
  1753.     }

  1754.   else if (regnum >= R0_C_REGNUM && regnum <= R_LAST_C_REGNUM)
  1755.     {
  1756.       int c_regnum = sh64_compact_reg_base_num (gdbarch, regnum);
  1757.       fprintf_filtered (file, "r%d_c\t0x%08x\n", regnum - R0_C_REGNUM,
  1758.            (unsigned) get_frame_register_unsigned (frame, c_regnum));
  1759.     }
  1760.   else if (regnum >= FP0_C_REGNUM && regnum <= FP_LAST_C_REGNUM)
  1761.     /* This should work also for pseudoregs.  */
  1762.     sh64_do_fp_register (gdbarch, file, frame, regnum);
  1763.   else if (regnum >= PC_C_REGNUM && regnum <= FPUL_C_REGNUM)
  1764.     sh64_do_cr_c_register_info (file, frame, regnum);
  1765. }

  1766. static void
  1767. sh64_do_register (struct gdbarch *gdbarch, struct ui_file *file,
  1768.                   struct frame_info *frame, int regnum)
  1769. {
  1770.   unsigned char raw_buffer[MAX_REGISTER_SIZE];
  1771.   struct value_print_options opts;

  1772.   fputs_filtered (gdbarch_register_name (gdbarch, regnum), file);
  1773.   print_spaces_filtered (15 - strlen (gdbarch_register_name
  1774.                                       (gdbarch, regnum)), file);

  1775.   /* Get the data in raw format.  */
  1776.   if (!deprecated_frame_register_read (frame, regnum, raw_buffer))
  1777.     {
  1778.       fprintf_filtered (file, "*value not available*\n");
  1779.       return;
  1780.     }

  1781.   get_formatted_print_options (&opts, 'x');
  1782.   opts.deref_ref = 1;
  1783.   val_print (register_type (gdbarch, regnum), raw_buffer, 0, 0,
  1784.              file, 0, NULL, &opts, current_language);
  1785.   fprintf_filtered (file, "\t");
  1786.   get_formatted_print_options (&opts, 0);
  1787.   opts.deref_ref = 1;
  1788.   val_print (register_type (gdbarch, regnum), raw_buffer, 0, 0,
  1789.              file, 0, NULL, &opts, current_language);
  1790.   fprintf_filtered (file, "\n");
  1791. }

  1792. static void
  1793. sh64_print_register (struct gdbarch *gdbarch, struct ui_file *file,
  1794.                      struct frame_info *frame, int regnum)
  1795. {
  1796.   if (regnum < 0 || regnum >= gdbarch_num_regs (gdbarch)
  1797.                               + gdbarch_num_pseudo_regs (gdbarch))
  1798.     internal_error (__FILE__, __LINE__,
  1799.                     _("Invalid register number %d\n"), regnum);

  1800.   else if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
  1801.     {
  1802.       if (TYPE_CODE (register_type (gdbarch, regnum)) == TYPE_CODE_FLT)
  1803.         sh64_do_fp_register (gdbarch, file, frame, regnum);        /* FP regs */
  1804.       else
  1805.         sh64_do_register (gdbarch, file, frame, regnum);
  1806.     }

  1807.   else if (regnum < gdbarch_num_regs (gdbarch)
  1808.                     + gdbarch_num_pseudo_regs (gdbarch))
  1809.     sh64_do_pseudo_register (gdbarch, file, frame, regnum);
  1810. }

  1811. static void
  1812. sh64_media_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
  1813.                                  struct frame_info *frame, int regnum,
  1814.                                  int fpregs)
  1815. {
  1816.   if (regnum != -1)                /* Do one specified register.  */
  1817.     {
  1818.       if (*(gdbarch_register_name (gdbarch, regnum)) == '\0')
  1819.         error (_("Not a valid register for the current processor type"));

  1820.       sh64_print_register (gdbarch, file, frame, regnum);
  1821.     }
  1822.   else
  1823.     /* Do all (or most) registers.  */
  1824.     {
  1825.       regnum = 0;
  1826.       while (regnum < gdbarch_num_regs (gdbarch))
  1827.         {
  1828.           /* If the register name is empty, it is undefined for this
  1829.              processor, so don't display anything.  */
  1830.           if (gdbarch_register_name (gdbarch, regnum) == NULL
  1831.               || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
  1832.             {
  1833.               regnum++;
  1834.               continue;
  1835.             }

  1836.           if (TYPE_CODE (register_type (gdbarch, regnum))
  1837.               == TYPE_CODE_FLT)
  1838.             {
  1839.               if (fpregs)
  1840.                 {
  1841.                   /* true for "INFO ALL-REGISTERS" command.  */
  1842.                   sh64_do_fp_register (gdbarch, file, frame, regnum);
  1843.                   regnum ++;
  1844.                 }
  1845.               else
  1846.                 regnum += FP_LAST_REGNUM - gdbarch_fp0_regnum (gdbarch);
  1847.                 /* skip FP regs */
  1848.             }
  1849.           else
  1850.             {
  1851.               sh64_do_register (gdbarch, file, frame, regnum);
  1852.               regnum++;
  1853.             }
  1854.         }

  1855.       if (fpregs)
  1856.         while (regnum < gdbarch_num_regs (gdbarch)
  1857.                         + gdbarch_num_pseudo_regs (gdbarch))
  1858.           {
  1859.             sh64_do_pseudo_register (gdbarch, file, frame, regnum);
  1860.             regnum++;
  1861.           }
  1862.     }
  1863. }

  1864. static void
  1865. sh64_compact_print_registers_info (struct gdbarch *gdbarch,
  1866.                                    struct ui_file *file,
  1867.                                    struct frame_info *frame, int regnum,
  1868.                                    int fpregs)
  1869. {
  1870.   if (regnum != -1)                /* Do one specified register.  */
  1871.     {
  1872.       if (*(gdbarch_register_name (gdbarch, regnum)) == '\0')
  1873.         error (_("Not a valid register for the current processor type"));

  1874.       if (regnum >= 0 && regnum < R0_C_REGNUM)
  1875.         error (_("Not a valid register for the current processor mode."));

  1876.       sh64_print_register (gdbarch, file, frame, regnum);
  1877.     }
  1878.   else
  1879.     /* Do all compact registers.  */
  1880.     {
  1881.       regnum = R0_C_REGNUM;
  1882.       while (regnum < gdbarch_num_regs (gdbarch)
  1883.                       + gdbarch_num_pseudo_regs (gdbarch))
  1884.         {
  1885.           sh64_do_pseudo_register (gdbarch, file, frame, regnum);
  1886.           regnum++;
  1887.         }
  1888.     }
  1889. }

  1890. static void
  1891. sh64_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
  1892.                            struct frame_info *frame, int regnum, int fpregs)
  1893. {
  1894.   if (pc_is_isa32 (get_frame_pc (frame)))
  1895.     sh64_media_print_registers_info (gdbarch, file, frame, regnum, fpregs);
  1896.   else
  1897.     sh64_compact_print_registers_info (gdbarch, file, frame, regnum, fpregs);
  1898. }

  1899. static struct sh64_frame_cache *
  1900. sh64_alloc_frame_cache (void)
  1901. {
  1902.   struct sh64_frame_cache *cache;
  1903.   int i;

  1904.   cache = FRAME_OBSTACK_ZALLOC (struct sh64_frame_cache);

  1905.   /* Base address.  */
  1906.   cache->base = 0;
  1907.   cache->saved_sp = 0;
  1908.   cache->sp_offset = 0;
  1909.   cache->pc = 0;

  1910.   /* Frameless until proven otherwise.  */
  1911.   cache->uses_fp = 0;

  1912.   /* Saved registers.  We initialize these to -1 since zero is a valid
  1913.      offset (that's where fp is supposed to be stored).  */
  1914.   for (i = 0; i < SIM_SH64_NR_REGS; i++)
  1915.     {
  1916.       cache->saved_regs[i] = -1;
  1917.     }

  1918.   return cache;
  1919. }

  1920. static struct sh64_frame_cache *
  1921. sh64_frame_cache (struct frame_info *this_frame, void **this_cache)
  1922. {
  1923.   struct gdbarch *gdbarch;
  1924.   struct sh64_frame_cache *cache;
  1925.   CORE_ADDR current_pc;
  1926.   int i;

  1927.   if (*this_cache)
  1928.     return *this_cache;

  1929.   gdbarch = get_frame_arch (this_frame);
  1930.   cache = sh64_alloc_frame_cache ();
  1931.   *this_cache = cache;

  1932.   current_pc = get_frame_pc (this_frame);
  1933.   cache->media_mode = pc_is_isa32 (current_pc);

  1934.   /* In principle, for normal frames, fp holds the frame pointer,
  1935.      which holds the base address for the current stack frame.
  1936.      However, for functions that don't need it, the frame pointer is
  1937.      optional.  For these "frameless" functions the frame pointer is
  1938.      actually the frame pointer of the calling frame.  */
  1939.   cache->base = get_frame_register_unsigned (this_frame, MEDIA_FP_REGNUM);
  1940.   if (cache->base == 0)
  1941.     return cache;

  1942.   cache->pc = get_frame_func (this_frame);
  1943.   if (cache->pc != 0)
  1944.     sh64_analyze_prologue (gdbarch, cache, cache->pc, current_pc);

  1945.   if (!cache->uses_fp)
  1946.     {
  1947.       /* We didn't find a valid frame, which means that CACHE->base
  1948.          currently holds the frame pointer for our calling frame.  If
  1949.          we're at the start of a function, or somewhere half-way its
  1950.          prologue, the function's frame probably hasn't been fully
  1951.          setup yet.  Try to reconstruct the base address for the stack
  1952.          frame by looking at the stack pointer.  For truly "frameless"
  1953.          functions this might work too.  */
  1954.       cache->base = get_frame_register_unsigned
  1955.                     (this_frame, gdbarch_sp_regnum (gdbarch));
  1956.     }

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

  1960.   /* Adjust all the saved registers such that they contain addresses
  1961.      instead of offsets.  */
  1962.   for (i = 0; i < SIM_SH64_NR_REGS; i++)
  1963.     if (cache->saved_regs[i] != -1)
  1964.       cache->saved_regs[i] = cache->saved_sp - cache->saved_regs[i];

  1965.   return cache;
  1966. }

  1967. static struct value *
  1968. sh64_frame_prev_register (struct frame_info *this_frame,
  1969.                           void **this_cache, int regnum)
  1970. {
  1971.   struct sh64_frame_cache *cache = sh64_frame_cache (this_frame, this_cache);
  1972.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1973.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  1974.   gdb_assert (regnum >= 0);

  1975.   if (regnum == gdbarch_sp_regnum (gdbarch) && cache->saved_sp)
  1976.     frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);

  1977.   /* The PC of the previous frame is stored in the PR register of
  1978.      the current frame.  Frob regnum so that we pull the value from
  1979.      the correct place.  */
  1980.   if (regnum == gdbarch_pc_regnum (gdbarch))
  1981.     regnum = PR_REGNUM;

  1982.   if (regnum < SIM_SH64_NR_REGS && cache->saved_regs[regnum] != -1)
  1983.     {
  1984.       if (gdbarch_tdep (gdbarch)->sh_abi == SH_ABI_32
  1985.           && (regnum == MEDIA_FP_REGNUM || regnum == PR_REGNUM))
  1986.         {
  1987.           CORE_ADDR val;
  1988.           val = read_memory_unsigned_integer (cache->saved_regs[regnum],
  1989.                                               4, byte_order);
  1990.           return frame_unwind_got_constant (this_frame, regnum, val);
  1991.         }

  1992.       return frame_unwind_got_memory (this_frame, regnum,
  1993.                                       cache->saved_regs[regnum]);
  1994.     }

  1995.   return frame_unwind_got_register (this_frame, regnum, regnum);
  1996. }

  1997. static void
  1998. sh64_frame_this_id (struct frame_info *this_frame, void **this_cache,
  1999.                     struct frame_id *this_id)
  2000. {
  2001.   struct sh64_frame_cache *cache = sh64_frame_cache (this_frame, this_cache);

  2002.   /* This marks the outermost frame.  */
  2003.   if (cache->base == 0)
  2004.     return;

  2005.   *this_id = frame_id_build (cache->saved_sp, cache->pc);
  2006. }

  2007. static const struct frame_unwind sh64_frame_unwind = {
  2008.   NORMAL_FRAME,
  2009.   default_frame_unwind_stop_reason,
  2010.   sh64_frame_this_id,
  2011.   sh64_frame_prev_register,
  2012.   NULL,
  2013.   default_frame_sniffer
  2014. };

  2015. static CORE_ADDR
  2016. sh64_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
  2017. {
  2018.   return frame_unwind_register_unsigned (next_frame,
  2019.                                          gdbarch_sp_regnum (gdbarch));
  2020. }

  2021. static CORE_ADDR
  2022. sh64_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  2023. {
  2024.   return frame_unwind_register_unsigned (next_frame,
  2025.                                          gdbarch_pc_regnum (gdbarch));
  2026. }

  2027. static struct frame_id
  2028. sh64_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  2029. {
  2030.   CORE_ADDR sp = get_frame_register_unsigned (this_frame,
  2031.                                               gdbarch_sp_regnum (gdbarch));
  2032.   return frame_id_build (sp, get_frame_pc (this_frame));
  2033. }

  2034. static CORE_ADDR
  2035. sh64_frame_base_address (struct frame_info *this_frame, void **this_cache)
  2036. {
  2037.   struct sh64_frame_cache *cache = sh64_frame_cache (this_frame, this_cache);

  2038.   return cache->base;
  2039. }

  2040. static const struct frame_base sh64_frame_base = {
  2041.   &sh64_frame_unwind,
  2042.   sh64_frame_base_address,
  2043.   sh64_frame_base_address,
  2044.   sh64_frame_base_address
  2045. };


  2046. struct gdbarch *
  2047. sh64_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  2048. {
  2049.   struct gdbarch *gdbarch;
  2050.   struct gdbarch_tdep *tdep;

  2051.   /* If there is already a candidate, use it.  */
  2052.   arches = gdbarch_list_lookup_by_info (arches, &info);
  2053.   if (arches != NULL)
  2054.     return arches->gdbarch;

  2055.   /* None found, create a new architecture from the information
  2056.      provided.  */
  2057.   tdep = XNEW (struct gdbarch_tdep);
  2058.   gdbarch = gdbarch_alloc (&info, tdep);

  2059.   /* Determine the ABI */
  2060.   if (info.abfd && bfd_get_arch_size (info.abfd) == 64)
  2061.     {
  2062.       /* If the ABI is the 64-bit one, it can only be sh-media.  */
  2063.       tdep->sh_abi = SH_ABI_64;
  2064.       set_gdbarch_ptr_bit (gdbarch, 8 * TARGET_CHAR_BIT);
  2065.       set_gdbarch_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
  2066.     }
  2067.   else
  2068.     {
  2069.       /* If the ABI is the 32-bit one it could be either media or
  2070.          compact.  */
  2071.       tdep->sh_abi = SH_ABI_32;
  2072.       set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  2073.       set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  2074.     }

  2075.   set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  2076.   set_gdbarch_int_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  2077.   set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  2078.   set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
  2079.   set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  2080.   set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
  2081.   set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);

  2082.   /* The number of real registers is the same whether we are in
  2083.      ISA16(compact) or ISA32(media).  */
  2084.   set_gdbarch_num_regs (gdbarch, SIM_SH64_NR_REGS);
  2085.   set_gdbarch_sp_regnum (gdbarch, 15);
  2086.   set_gdbarch_pc_regnum (gdbarch, 64);
  2087.   set_gdbarch_fp0_regnum (gdbarch, SIM_SH64_FR0_REGNUM);
  2088.   set_gdbarch_num_pseudo_regs (gdbarch, NUM_PSEUDO_REGS_SH_MEDIA
  2089.                                           + NUM_PSEUDO_REGS_SH_COMPACT);

  2090.   set_gdbarch_register_name (gdbarch, sh64_register_name);
  2091.   set_gdbarch_register_type (gdbarch, sh64_register_type);

  2092.   set_gdbarch_pseudo_register_read (gdbarch, sh64_pseudo_register_read);
  2093.   set_gdbarch_pseudo_register_write (gdbarch, sh64_pseudo_register_write);

  2094.   set_gdbarch_breakpoint_from_pc (gdbarch, sh64_breakpoint_from_pc);

  2095.   set_gdbarch_print_insn (gdbarch, print_insn_sh);
  2096.   set_gdbarch_register_sim_regno (gdbarch, legacy_register_sim_regno);

  2097.   set_gdbarch_return_value (gdbarch, sh64_return_value);

  2098.   set_gdbarch_skip_prologue (gdbarch, sh64_skip_prologue);
  2099.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);

  2100.   set_gdbarch_push_dummy_call (gdbarch, sh64_push_dummy_call);

  2101.   set_gdbarch_believe_pcc_promotion (gdbarch, 1);

  2102.   set_gdbarch_frame_align (gdbarch, sh64_frame_align);
  2103.   set_gdbarch_unwind_sp (gdbarch, sh64_unwind_sp);
  2104.   set_gdbarch_unwind_pc (gdbarch, sh64_unwind_pc);
  2105.   set_gdbarch_dummy_id (gdbarch, sh64_dummy_id);
  2106.   frame_base_set_default (gdbarch, &sh64_frame_base);

  2107.   set_gdbarch_print_registers_info (gdbarch, sh64_print_registers_info);

  2108.   set_gdbarch_elf_make_msymbol_special (gdbarch,
  2109.                                         sh64_elf_make_msymbol_special);

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

  2112.   dwarf2_append_unwinders (gdbarch);
  2113.   frame_unwind_append_unwinder (gdbarch, &sh64_frame_unwind);

  2114.   return gdbarch;
  2115. }