c6e5042664ab613c5e88df0f58a16599ae993245
[platform/upstream/binutils.git] / gdb / sh64-tdep.c
1 /* Target-dependent code for Renesas Super-H, for GDB.
2
3    Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
4    2003, 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 /*
22    Contributed by Steve Chamberlain
23    sac@cygnus.com
24  */
25
26 #include "defs.h"
27 #include "frame.h"
28 #include "frame-base.h"
29 #include "frame-unwind.h"
30 #include "dwarf2-frame.h"
31 #include "symtab.h"
32 #include "gdbtypes.h"
33 #include "gdbcmd.h"
34 #include "gdbcore.h"
35 #include "value.h"
36 #include "dis-asm.h"
37 #include "inferior.h"
38 #include "gdb_string.h"
39 #include "gdb_assert.h"
40 #include "arch-utils.h"
41 #include "regcache.h"
42 #include "osabi.h"
43 #include "valprint.h"
44
45 #include "elf-bfd.h"
46
47 /* sh flags */
48 #include "elf/sh.h"
49 /* registers numbers shared with the simulator */
50 #include "gdb/sim-sh.h"
51 #include "language.h"
52
53 /* Information that is dependent on the processor variant.  */
54 enum sh_abi
55   {
56     SH_ABI_UNKNOWN,
57     SH_ABI_32,
58     SH_ABI_64
59   };
60
61 struct gdbarch_tdep
62   {
63     enum sh_abi sh_abi;
64   };
65
66 struct sh64_frame_cache
67 {
68   /* Base address.  */
69   CORE_ADDR base;
70   LONGEST sp_offset;
71   CORE_ADDR pc;
72
73   /* Flag showing that a frame has been created in the prologue code. */
74   int uses_fp;
75
76   int media_mode;
77
78   /* Saved registers.  */
79   CORE_ADDR saved_regs[SIM_SH64_NR_REGS];
80   CORE_ADDR saved_sp;
81 };
82
83 /* Registers of SH5 */
84 enum
85   {
86     R0_REGNUM = 0,
87     DEFAULT_RETURN_REGNUM = 2,
88     STRUCT_RETURN_REGNUM = 2,
89     ARG0_REGNUM = 2,
90     ARGLAST_REGNUM = 9,
91     FLOAT_ARGLAST_REGNUM = 11,
92     MEDIA_FP_REGNUM = 14,
93     PR_REGNUM = 18,
94     SR_REGNUM = 65,
95     DR0_REGNUM = 141,
96     DR_LAST_REGNUM = 172,
97     /* FPP stands for Floating Point Pair, to avoid confusion with
98        GDB's gdbarch_fp0_regnum, which is the number of the first Floating
99        point register. Unfortunately on the sh5, the floating point
100        registers are called FR, and the floating point pairs are called FP.  */
101     FPP0_REGNUM = 173,
102     FPP_LAST_REGNUM = 204,
103     FV0_REGNUM = 205,
104     FV_LAST_REGNUM = 220,
105     R0_C_REGNUM = 221,
106     R_LAST_C_REGNUM = 236,
107     PC_C_REGNUM = 237,
108     GBR_C_REGNUM = 238,
109     MACH_C_REGNUM = 239,
110     MACL_C_REGNUM = 240,
111     PR_C_REGNUM = 241,
112     T_C_REGNUM = 242,
113     FPSCR_C_REGNUM = 243,
114     FPUL_C_REGNUM = 244,
115     FP0_C_REGNUM = 245,
116     FP_LAST_C_REGNUM = 260,
117     DR0_C_REGNUM = 261,
118     DR_LAST_C_REGNUM = 268,
119     FV0_C_REGNUM = 269,
120     FV_LAST_C_REGNUM = 272,
121     FPSCR_REGNUM = SIM_SH64_FPCSR_REGNUM,
122     SSR_REGNUM = SIM_SH64_SSR_REGNUM,
123     SPC_REGNUM = SIM_SH64_SPC_REGNUM,
124     TR7_REGNUM = SIM_SH64_TR0_REGNUM + 7,
125     FP_LAST_REGNUM = SIM_SH64_FR0_REGNUM + SIM_SH64_NR_FP_REGS - 1
126   };
127
128 static const char *
129 sh64_register_name (struct gdbarch *gdbarch, int reg_nr)
130 {
131   static char *register_names[] =
132   {
133     /* SH MEDIA MODE (ISA 32) */
134     /* general registers (64-bit) 0-63 */
135     "r0",   "r1",   "r2",   "r3",   "r4",   "r5",   "r6",   "r7",
136     "r8",   "r9",   "r10",  "r11",  "r12",  "r13",  "r14",  "r15",
137     "r16",  "r17",  "r18",  "r19",  "r20",  "r21",  "r22",  "r23",
138     "r24",  "r25",  "r26",  "r27",  "r28",  "r29",  "r30",  "r31",
139     "r32",  "r33",  "r34",  "r35",  "r36",  "r37",  "r38",  "r39",
140     "r40",  "r41",  "r42",  "r43",  "r44",  "r45",  "r46",  "r47",
141     "r48",  "r49",  "r50",  "r51",  "r52",  "r53",  "r54",  "r55",
142     "r56",  "r57",  "r58",  "r59",  "r60",  "r61",  "r62",  "r63",
143
144     /* pc (64-bit) 64 */
145     "pc",   
146
147     /* status reg., saved status reg., saved pc reg. (64-bit) 65-67 */
148     "sr",  "ssr",  "spc", 
149
150     /* target registers (64-bit) 68-75*/
151     "tr0",  "tr1",  "tr2",  "tr3",  "tr4",  "tr5",  "tr6",  "tr7",
152
153     /* floating point state control register (32-bit) 76 */
154     "fpscr",
155
156     /* single precision floating point registers (32-bit) 77-140*/
157     "fr0",  "fr1",  "fr2",  "fr3",  "fr4",  "fr5",  "fr6",  "fr7",
158     "fr8",  "fr9",  "fr10", "fr11", "fr12", "fr13", "fr14", "fr15",
159     "fr16", "fr17", "fr18", "fr19", "fr20", "fr21", "fr22", "fr23",
160     "fr24", "fr25", "fr26", "fr27", "fr28", "fr29", "fr30", "fr31",
161     "fr32", "fr33", "fr34", "fr35", "fr36", "fr37", "fr38", "fr39",
162     "fr40", "fr41", "fr42", "fr43", "fr44", "fr45", "fr46", "fr47",
163     "fr48", "fr49", "fr50", "fr51", "fr52", "fr53", "fr54", "fr55",
164     "fr56", "fr57", "fr58", "fr59", "fr60", "fr61", "fr62", "fr63",
165
166     /* double precision registers (pseudo) 141-172 */
167     "dr0",  "dr2",  "dr4",  "dr6",  "dr8",  "dr10", "dr12", "dr14",
168     "dr16", "dr18", "dr20", "dr22", "dr24", "dr26", "dr28", "dr30",
169     "dr32", "dr34", "dr36", "dr38", "dr40", "dr42", "dr44", "dr46",
170     "dr48", "dr50", "dr52", "dr54", "dr56", "dr58", "dr60", "dr62",
171
172     /* floating point pairs (pseudo) 173-204*/
173     "fp0",  "fp2",  "fp4",  "fp6",  "fp8",  "fp10", "fp12", "fp14",
174     "fp16", "fp18", "fp20", "fp22", "fp24", "fp26", "fp28", "fp30",
175     "fp32", "fp34", "fp36", "fp38", "fp40", "fp42", "fp44", "fp46",
176     "fp48", "fp50", "fp52", "fp54", "fp56", "fp58", "fp60", "fp62",
177
178     /* floating point vectors (4 floating point regs) (pseudo) 205-220*/
179     "fv0",  "fv4",  "fv8",  "fv12", "fv16", "fv20", "fv24", "fv28",
180     "fv32", "fv36", "fv40", "fv44", "fv48", "fv52", "fv56", "fv60",
181
182     /* SH COMPACT MODE (ISA 16) (all pseudo) 221-272*/
183     "r0_c", "r1_c", "r2_c",  "r3_c",  "r4_c",  "r5_c",  "r6_c",  "r7_c",
184     "r8_c", "r9_c", "r10_c", "r11_c", "r12_c", "r13_c", "r14_c", "r15_c",
185     "pc_c",
186     "gbr_c", "mach_c", "macl_c", "pr_c", "t_c",
187     "fpscr_c", "fpul_c",
188     "fr0_c", "fr1_c", "fr2_c",  "fr3_c",  "fr4_c",  "fr5_c",  "fr6_c",  "fr7_c",
189     "fr8_c", "fr9_c", "fr10_c", "fr11_c", "fr12_c", "fr13_c", "fr14_c", "fr15_c",
190     "dr0_c", "dr2_c", "dr4_c",  "dr6_c",  "dr8_c",  "dr10_c", "dr12_c", "dr14_c",
191     "fv0_c", "fv4_c", "fv8_c",  "fv12_c",
192     /* FIXME!!!! XF0 XF15, XD0 XD14 ?????*/
193   };
194
195   if (reg_nr < 0)
196     return NULL;
197   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
198     return NULL;
199   return register_names[reg_nr];
200 }
201
202 #define NUM_PSEUDO_REGS_SH_MEDIA 80
203 #define NUM_PSEUDO_REGS_SH_COMPACT 51
204
205 /* Macros and functions for setting and testing a bit in a minimal
206    symbol that marks it as 32-bit function.  The MSB of the minimal
207    symbol's "info" field is used for this purpose.
208
209    gdbarch_elf_make_msymbol_special tests whether an ELF symbol is "special",
210    i.e. refers to a 32-bit function, and sets a "special" bit in a
211    minimal symbol to mark it as a 32-bit function
212    MSYMBOL_IS_SPECIAL   tests the "special" bit in a minimal symbol  */
213
214 #define MSYMBOL_IS_SPECIAL(msym) \
215   MSYMBOL_TARGET_FLAG_1 (msym)
216
217 static void
218 sh64_elf_make_msymbol_special (asymbol *sym, struct minimal_symbol *msym)
219 {
220   if (msym == NULL)
221     return;
222
223   if (((elf_symbol_type *)(sym))->internal_elf_sym.st_other == STO_SH5_ISA32)
224     {
225       MSYMBOL_TARGET_FLAG_1 (msym) = 1;
226       SYMBOL_VALUE_ADDRESS (msym) |= 1;
227     }
228 }
229
230 /* ISA32 (shmedia) function addresses are odd (bit 0 is set).  Here
231    are some macros to test, set, or clear bit 0 of addresses.  */
232 #define IS_ISA32_ADDR(addr)      ((addr) & 1)
233 #define MAKE_ISA32_ADDR(addr)    ((addr) | 1)
234 #define UNMAKE_ISA32_ADDR(addr)  ((addr) & ~1)
235
236 static int
237 pc_is_isa32 (bfd_vma memaddr)
238 {
239   struct minimal_symbol *sym;
240
241   /* If bit 0 of the address is set, assume this is a
242      ISA32 (shmedia) address.  */
243   if (IS_ISA32_ADDR (memaddr))
244     return 1;
245
246   /* A flag indicating that this is a ISA32 function is stored by elfread.c in
247      the high bit of the info field.  Use this to decide if the function is
248      ISA16 or ISA32.  */
249   sym = lookup_minimal_symbol_by_pc (memaddr);
250   if (sym)
251     return MSYMBOL_IS_SPECIAL (sym);
252   else
253     return 0;
254 }
255
256 static const unsigned char *
257 sh64_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr, int *lenptr)
258 {
259   /* The BRK instruction for shmedia is 
260      01101111 11110101 11111111 11110000
261      which translates in big endian mode to 0x6f, 0xf5, 0xff, 0xf0
262      and in little endian mode to 0xf0, 0xff, 0xf5, 0x6f */
263
264   /* The BRK instruction for shcompact is
265      00000000 00111011
266      which translates in big endian mode to 0x0, 0x3b
267      and in little endian mode to 0x3b, 0x0*/
268
269   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
270     {
271       if (pc_is_isa32 (*pcptr))
272         {
273           static unsigned char big_breakpoint_media[] = {0x6f, 0xf5, 0xff, 0xf0};
274           *pcptr = UNMAKE_ISA32_ADDR (*pcptr);
275           *lenptr = sizeof (big_breakpoint_media);
276           return big_breakpoint_media;
277         }
278       else
279         {
280           static unsigned char big_breakpoint_compact[] = {0x0, 0x3b};
281           *lenptr = sizeof (big_breakpoint_compact);
282           return big_breakpoint_compact;
283         }
284     }
285   else
286     {
287       if (pc_is_isa32 (*pcptr))
288         {
289           static unsigned char little_breakpoint_media[] = {0xf0, 0xff, 0xf5, 0x6f};
290           *pcptr = UNMAKE_ISA32_ADDR (*pcptr);
291           *lenptr = sizeof (little_breakpoint_media);
292           return little_breakpoint_media;
293         }
294       else
295         {
296           static unsigned char little_breakpoint_compact[] = {0x3b, 0x0};
297           *lenptr = sizeof (little_breakpoint_compact);
298           return little_breakpoint_compact;
299         }
300     }
301 }
302
303 /* Prologue looks like
304    [mov.l       <regs>,@-r15]...
305    [sts.l       pr,@-r15]
306    [mov.l       r14,@-r15]
307    [mov         r15,r14]
308
309    Actually it can be more complicated than this.  For instance, with
310    newer gcc's:
311
312    mov.l   r14,@-r15
313    add     #-12,r15
314    mov     r15,r14
315    mov     r4,r1
316    mov     r5,r2
317    mov.l   r6,@(4,r14)
318    mov.l   r7,@(8,r14)
319    mov.b   r1,@r14
320    mov     r14,r1
321    mov     r14,r1
322    add     #2,r1
323    mov.w   r2,@r1
324
325  */
326
327 /* PTABS/L Rn, TRa       0110101111110001nnnnnnl00aaa0000 
328    with l=1 and n = 18   0110101111110001010010100aaa0000 */
329 #define IS_PTABSL_R18(x)  (((x) & 0xffffff8f) == 0x6bf14a00)
330
331 /* STS.L PR,@-r0   0100000000100010
332    r0-4-->r0, PR-->(r0) */
333 #define IS_STS_R0(x)            ((x) == 0x4022)
334
335 /* STS PR, Rm      0000mmmm00101010
336    PR-->Rm */
337 #define IS_STS_PR(x)            (((x) & 0xf0ff) == 0x2a)
338
339 /* MOV.L Rm,@(disp,r15)  00011111mmmmdddd
340    Rm-->(dispx4+r15) */
341 #define IS_MOV_TO_R15(x)              (((x) & 0xff00) == 0x1f00)
342
343 /* MOV.L R14,@(disp,r15)  000111111110dddd
344    R14-->(dispx4+r15) */
345 #define IS_MOV_R14(x)              (((x) & 0xfff0) == 0x1fe0)
346
347 /* ST.Q R14, disp, R18    101011001110dddddddddd0100100000
348    R18-->(dispx8+R14) */
349 #define IS_STQ_R18_R14(x)          (((x) & 0xfff003ff) == 0xace00120)
350
351 /* ST.Q R15, disp, R18    101011001111dddddddddd0100100000
352    R18-->(dispx8+R15) */
353 #define IS_STQ_R18_R15(x)          (((x) & 0xfff003ff) == 0xacf00120)
354
355 /* ST.L R15, disp, R18    101010001111dddddddddd0100100000
356    R18-->(dispx4+R15) */
357 #define IS_STL_R18_R15(x)          (((x) & 0xfff003ff) == 0xa8f00120)
358
359 /* ST.Q R15, disp, R14    1010 1100 1111 dddd dddd dd00 1110 0000
360    R14-->(dispx8+R15) */
361 #define IS_STQ_R14_R15(x)          (((x) & 0xfff003ff) == 0xacf000e0)
362
363 /* ST.L R15, disp, R14    1010 1000 1111 dddd dddd dd00 1110 0000
364    R14-->(dispx4+R15) */
365 #define IS_STL_R14_R15(x)          (((x) & 0xfff003ff) == 0xa8f000e0)
366
367 /* ADDI.L R15,imm,R15     1101 0100 1111 ssss ssss ss00 1111 0000
368    R15 + imm --> R15 */
369 #define IS_ADDIL_SP_MEDIA(x)         (((x) & 0xfff003ff) == 0xd4f000f0)
370
371 /* ADDI R15,imm,R15     1101 0000 1111 ssss ssss ss00 1111 0000
372    R15 + imm --> R15 */
373 #define IS_ADDI_SP_MEDIA(x)         (((x) & 0xfff003ff) == 0xd0f000f0)
374
375 /* ADD.L R15,R63,R14    0000 0000 1111 1000 1111 1100 1110 0000 
376    R15 + R63 --> R14 */
377 #define IS_ADDL_SP_FP_MEDIA(x)          ((x) == 0x00f8fce0)
378
379 /* ADD R15,R63,R14    0000 0000 1111 1001 1111 1100 1110 0000 
380    R15 + R63 --> R14 */
381 #define IS_ADD_SP_FP_MEDIA(x)   ((x) == 0x00f9fce0)
382
383 #define IS_MOV_SP_FP_MEDIA(x)   (IS_ADDL_SP_FP_MEDIA(x) || IS_ADD_SP_FP_MEDIA(x))
384
385 /* MOV #imm, R0    1110 0000 ssss ssss 
386    #imm-->R0 */
387 #define IS_MOV_R0(x)            (((x) & 0xff00) == 0xe000)
388
389 /* MOV.L @(disp,PC), R0    1101 0000 iiii iiii  */
390 #define IS_MOVL_R0(x)           (((x) & 0xff00) == 0xd000)
391
392 /* ADD r15,r0      0011 0000 1111 1100
393    r15+r0-->r0 */
394 #define IS_ADD_SP_R0(x)         ((x) == 0x30fc)
395
396 /* MOV.L R14 @-R0  0010 0000 1110 0110
397    R14-->(R0-4), R0-4-->R0 */
398 #define IS_MOV_R14_R0(x)        ((x) == 0x20e6)
399
400 /* ADD Rm,R63,Rn  Rm+R63-->Rn  0000 00mm mmmm 1001 1111 11nn nnnn 0000
401    where Rm is one of r2-r9 which are the argument registers.  */
402 /* FIXME: Recognize the float and double register moves too! */
403 #define IS_MEDIA_IND_ARG_MOV(x) \
404 ((((x) & 0xfc0ffc0f) == 0x0009fc00) && (((x) & 0x03f00000) >= 0x00200000 && ((x) & 0x03f00000) <= 0x00900000))
405
406 /* ST.Q Rn,0,Rm  Rm-->Rn+0  1010 11nn nnnn 0000 0000 00mm mmmm 0000
407    or ST.L Rn,0,Rm  Rm-->Rn+0  1010 10nn nnnn 0000 0000 00mm mmmm 0000
408    where Rm is one of r2-r9 which are the argument registers.  */
409 #define IS_MEDIA_ARG_MOV(x) \
410 (((((x) & 0xfc0ffc0f) == 0xac000000) || (((x) & 0xfc0ffc0f) == 0xa8000000)) \
411    && (((x) & 0x000003f0) >= 0x00000020 && ((x) & 0x000003f0) <= 0x00000090))
412
413 /* ST.B R14,0,Rn     Rn-->(R14+0) 1010 0000 1110 0000 0000 00nn nnnn 0000*/
414 /* ST.W R14,0,Rn     Rn-->(R14+0) 1010 0100 1110 0000 0000 00nn nnnn 0000*/
415 /* ST.L R14,0,Rn     Rn-->(R14+0) 1010 1000 1110 0000 0000 00nn nnnn 0000*/
416 /* FST.S R14,0,FRn   Rn-->(R14+0) 1011 0100 1110 0000 0000 00nn nnnn 0000*/
417 /* FST.D R14,0,DRn   Rn-->(R14+0) 1011 1100 1110 0000 0000 00nn nnnn 0000*/
418 #define IS_MEDIA_MOV_TO_R14(x)  \
419 ((((x) & 0xfffffc0f) == 0xa0e00000) \
420 || (((x) & 0xfffffc0f) == 0xa4e00000) \
421 || (((x) & 0xfffffc0f) == 0xa8e00000) \
422 || (((x) & 0xfffffc0f) == 0xb4e00000) \
423 || (((x) & 0xfffffc0f) == 0xbce00000))
424
425 /* MOV Rm, Rn  Rm-->Rn 0110 nnnn mmmm 0011
426    where Rm is r2-r9 */
427 #define IS_COMPACT_IND_ARG_MOV(x) \
428 ((((x) & 0xf00f) == 0x6003) && (((x) & 0x00f0) >= 0x0020) && (((x) & 0x00f0) <= 0x0090))
429
430 /* compact direct arg move! 
431    MOV.L Rn, @r14     0010 1110 mmmm 0010 */
432 #define IS_COMPACT_ARG_MOV(x) \
433 (((((x) & 0xff0f) == 0x2e02) && (((x) & 0x00f0) >= 0x0020) && ((x) & 0x00f0) <= 0x0090))
434
435 /* MOV.B Rm, @R14     0010 1110 mmmm 0000 
436    MOV.W Rm, @R14     0010 1110 mmmm 0001 */
437 #define IS_COMPACT_MOV_TO_R14(x) \
438 ((((x) & 0xff0f) == 0x2e00) || (((x) & 0xff0f) == 0x2e01))
439
440 #define IS_JSR_R0(x)           ((x) == 0x400b)
441 #define IS_NOP(x)              ((x) == 0x0009)
442
443
444 /* MOV r15,r14     0110111011110011
445    r15-->r14  */
446 #define IS_MOV_SP_FP(x)         ((x) == 0x6ef3)
447
448 /* ADD #imm,r15    01111111iiiiiiii
449    r15+imm-->r15 */
450 #define IS_ADD_SP(x)            (((x) & 0xff00) == 0x7f00)
451
452 /* Skip any prologue before the guts of a function */
453
454 /* Skip the prologue using the debug information.  If this fails we'll
455    fall back on the 'guess' method below.  */
456 static CORE_ADDR
457 after_prologue (CORE_ADDR pc)
458 {
459   struct symtab_and_line sal;
460   CORE_ADDR func_addr, func_end;
461
462   /* If we can not find the symbol in the partial symbol table, then
463      there is no hope we can determine the function's start address
464      with this code.  */
465   if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
466     return 0;
467
468
469   /* Get the line associated with FUNC_ADDR.  */
470   sal = find_pc_line (func_addr, 0);
471
472   /* There are only two cases to consider.  First, the end of the source line
473      is within the function bounds.  In that case we return the end of the
474      source line.  Second is the end of the source line extends beyond the
475      bounds of the current function.  We need to use the slow code to
476      examine instructions in that case.  */
477   if (sal.end < func_end)
478     return sal.end;
479   else
480     return 0;
481 }
482
483 static CORE_ADDR 
484 look_for_args_moves (CORE_ADDR start_pc, int media_mode)
485 {
486   CORE_ADDR here, end;
487   int w;
488   int insn_size = (media_mode ? 4 : 2);
489
490   for (here = start_pc, end = start_pc + (insn_size * 28); here < end;)
491     {
492       if (media_mode)
493         {
494           w = read_memory_integer (UNMAKE_ISA32_ADDR (here), insn_size);
495           here += insn_size;
496           if (IS_MEDIA_IND_ARG_MOV (w))
497             {
498               /* This must be followed by a store to r14, so the argument
499                  is where the debug info says it is. This can happen after
500                  the SP has been saved, unfortunately.  */
501          
502               int next_insn = read_memory_integer (UNMAKE_ISA32_ADDR (here),
503                                                    insn_size);
504               here += insn_size;
505               if (IS_MEDIA_MOV_TO_R14 (next_insn))
506                 start_pc = here;          
507             }
508           else if (IS_MEDIA_ARG_MOV (w))
509             {
510               /* These instructions store directly the argument in r14.  */
511               start_pc = here;
512             }
513           else
514             break;
515         }
516       else
517         {
518           w = read_memory_integer (here, insn_size);
519           w = w & 0xffff;
520           here += insn_size;
521           if (IS_COMPACT_IND_ARG_MOV (w))
522             {
523               /* This must be followed by a store to r14, so the argument
524                  is where the debug info says it is. This can happen after
525                  the SP has been saved, unfortunately.  */
526          
527               int next_insn = 0xffff & read_memory_integer (here, insn_size);
528               here += insn_size;
529               if (IS_COMPACT_MOV_TO_R14 (next_insn))
530                 start_pc = here;
531             }
532           else if (IS_COMPACT_ARG_MOV (w))
533             {
534               /* These instructions store directly the argument in r14.  */
535               start_pc = here;
536             }
537           else if (IS_MOVL_R0 (w))
538             {
539               /* There is a function that gcc calls to get the arguments
540                  passed correctly to the function. Only after this
541                  function call the arguments will be found at the place
542                  where they are supposed to be. This happens in case the
543                  argument has to be stored into a 64-bit register (for
544                  instance doubles, long longs).  SHcompact doesn't have
545                  access to the full 64-bits, so we store the register in
546                  stack slot and store the address of the stack slot in
547                  the register, then do a call through a wrapper that
548                  loads the memory value into the register.  A SHcompact
549                  callee calls an argument decoder
550                  (GCC_shcompact_incoming_args) that stores the 64-bit
551                  value in a stack slot and stores the address of the
552                  stack slot in the register.  GCC thinks the argument is
553                  just passed by transparent reference, but this is only
554                  true after the argument decoder is called. Such a call
555                  needs to be considered part of the prologue.  */
556
557               /* This must be followed by a JSR @r0 instruction and by
558                  a NOP instruction. After these, the prologue is over!  */
559          
560               int next_insn = 0xffff & read_memory_integer (here, insn_size);
561               here += insn_size;
562               if (IS_JSR_R0 (next_insn))
563                 {
564                   next_insn = 0xffff & read_memory_integer (here, insn_size);
565                   here += insn_size;
566
567                   if (IS_NOP (next_insn))
568                     start_pc = here;
569                 }
570             }
571           else
572             break;
573         }
574     }
575
576   return start_pc;
577 }
578
579 static CORE_ADDR
580 sh64_skip_prologue_hard_way (CORE_ADDR start_pc)
581 {
582   CORE_ADDR here, end;
583   int updated_fp = 0;
584   int insn_size = 4;
585   int media_mode = 1;
586
587   if (!start_pc)
588     return 0;
589
590   if (pc_is_isa32 (start_pc) == 0)
591     {
592       insn_size = 2;
593       media_mode = 0;
594     }
595
596   for (here = start_pc, end = start_pc + (insn_size * 28); here < end;)
597     {
598
599       if (media_mode)
600         {
601           int w = read_memory_integer (UNMAKE_ISA32_ADDR (here), insn_size);
602           here += insn_size;
603           if (IS_STQ_R18_R14 (w) || IS_STQ_R18_R15 (w) || IS_STQ_R14_R15 (w)
604               || IS_STL_R14_R15 (w) || IS_STL_R18_R15 (w)
605               || IS_ADDIL_SP_MEDIA (w) || IS_ADDI_SP_MEDIA (w) || IS_PTABSL_R18 (w))
606             {
607               start_pc = here;
608             }
609           else if (IS_MOV_SP_FP (w) || IS_MOV_SP_FP_MEDIA(w))
610             {
611               start_pc = here;
612               updated_fp = 1;
613             }
614           else
615             if (updated_fp)
616               {
617                 /* Don't bail out yet, we may have arguments stored in
618                    registers here, according to the debug info, so that
619                    gdb can print the frames correctly.  */
620                 start_pc = look_for_args_moves (here - insn_size, media_mode);
621                 break;
622               }
623         }
624       else
625         {
626           int w = 0xffff & read_memory_integer (here, insn_size);
627           here += insn_size;
628
629           if (IS_STS_R0 (w) || IS_STS_PR (w)
630               || IS_MOV_TO_R15 (w) || IS_MOV_R14 (w) 
631               || IS_MOV_R0 (w) || IS_ADD_SP_R0 (w) || IS_MOV_R14_R0 (w))
632             {
633               start_pc = here;
634             }
635           else if (IS_MOV_SP_FP (w))
636             {
637               start_pc = here;
638               updated_fp = 1;
639             }
640           else
641             if (updated_fp)
642               {
643                 /* Don't bail out yet, we may have arguments stored in
644                    registers here, according to the debug info, so that
645                    gdb can print the frames correctly.  */
646                 start_pc = look_for_args_moves (here - insn_size, media_mode);
647                 break;
648               }
649         }
650     }
651
652   return start_pc;
653 }
654
655 static CORE_ADDR
656 sh64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
657 {
658   CORE_ADDR post_prologue_pc;
659
660   /* See if we can determine the end of the prologue via the symbol table.
661      If so, then return either PC, or the PC after the prologue, whichever
662      is greater.  */
663   post_prologue_pc = after_prologue (pc);
664
665   /* If after_prologue returned a useful address, then use it.  Else
666      fall back on the instruction skipping code.  */
667   if (post_prologue_pc != 0)
668     return max (pc, post_prologue_pc);
669   else
670     return sh64_skip_prologue_hard_way (pc);
671 }
672
673 /* Should call_function allocate stack space for a struct return?  */
674 static int
675 sh64_use_struct_convention (struct type *type)
676 {
677   return (TYPE_LENGTH (type) > 8);
678 }
679
680 /* For vectors of 4 floating point registers.  */
681 static int
682 sh64_fv_reg_base_num (struct gdbarch *gdbarch, int fv_regnum)
683 {
684   int fp_regnum;
685
686   fp_regnum = gdbarch_fp0_regnum (gdbarch) + (fv_regnum - FV0_REGNUM) * 4;
687   return fp_regnum;
688 }
689
690 /* For double precision floating point registers, i.e 2 fp regs.*/
691 static int
692 sh64_dr_reg_base_num (struct gdbarch *gdbarch, int dr_regnum)
693 {
694   int fp_regnum;
695
696   fp_regnum = gdbarch_fp0_regnum (gdbarch) + (dr_regnum - DR0_REGNUM) * 2;
697   return fp_regnum;
698 }
699
700 /* For pairs of floating point registers */
701 static int
702 sh64_fpp_reg_base_num (struct gdbarch *gdbarch, int fpp_regnum)
703 {
704   int fp_regnum;
705
706   fp_regnum = gdbarch_fp0_regnum (gdbarch) + (fpp_regnum - FPP0_REGNUM) * 2;
707   return fp_regnum;
708 }
709
710 /* *INDENT-OFF* */
711 /*
712     SH COMPACT MODE (ISA 16) (all pseudo) 221-272
713        GDB_REGNUM  BASE_REGNUM
714  r0_c       221      0
715  r1_c       222      1
716  r2_c       223      2
717  r3_c       224      3
718  r4_c       225      4
719  r5_c       226      5
720  r6_c       227      6
721  r7_c       228      7
722  r8_c       229      8
723  r9_c       230      9
724  r10_c      231      10
725  r11_c      232      11
726  r12_c      233      12
727  r13_c      234      13
728  r14_c      235      14
729  r15_c      236      15
730
731  pc_c       237      64
732  gbr_c      238      16
733  mach_c     239      17
734  macl_c     240      17
735  pr_c       241      18
736  t_c        242      19
737  fpscr_c    243      76
738  fpul_c     244      109
739
740  fr0_c      245      77
741  fr1_c      246      78
742  fr2_c      247      79
743  fr3_c      248      80
744  fr4_c      249      81
745  fr5_c      250      82
746  fr6_c      251      83
747  fr7_c      252      84
748  fr8_c      253      85
749  fr9_c      254      86
750  fr10_c     255      87
751  fr11_c     256      88
752  fr12_c     257      89
753  fr13_c     258      90
754  fr14_c     259      91
755  fr15_c     260      92
756
757  dr0_c      261      77
758  dr2_c      262      79
759  dr4_c      263      81
760  dr6_c      264      83
761  dr8_c      265      85
762  dr10_c     266      87
763  dr12_c     267      89
764  dr14_c     268      91
765
766  fv0_c      269      77
767  fv4_c      270      81
768  fv8_c      271      85
769  fv12_c     272      91
770 */
771 /* *INDENT-ON* */
772 static int
773 sh64_compact_reg_base_num (struct gdbarch *gdbarch, int reg_nr)
774 {
775   int base_regnum = reg_nr;
776
777   /* general register N maps to general register N */
778   if (reg_nr >= R0_C_REGNUM 
779       && reg_nr <= R_LAST_C_REGNUM)
780     base_regnum = reg_nr - R0_C_REGNUM;
781
782   /* floating point register N maps to floating point register N */
783   else if (reg_nr >= FP0_C_REGNUM 
784             && reg_nr <= FP_LAST_C_REGNUM)
785     base_regnum = reg_nr - FP0_C_REGNUM + gdbarch_fp0_regnum (gdbarch);
786
787   /* double prec register N maps to base regnum for double prec register N */
788   else if (reg_nr >= DR0_C_REGNUM 
789             && reg_nr <= DR_LAST_C_REGNUM)
790     base_regnum = sh64_dr_reg_base_num (gdbarch,
791                                         DR0_REGNUM + reg_nr - DR0_C_REGNUM);
792
793   /* vector N maps to base regnum for vector register N */
794   else if (reg_nr >= FV0_C_REGNUM 
795             && reg_nr <= FV_LAST_C_REGNUM)
796     base_regnum = sh64_fv_reg_base_num (gdbarch,
797                                         FV0_REGNUM + reg_nr - FV0_C_REGNUM);
798
799   else if (reg_nr == PC_C_REGNUM)
800     base_regnum = gdbarch_pc_regnum (gdbarch);
801
802   else if (reg_nr == GBR_C_REGNUM) 
803     base_regnum = 16;
804
805   else if (reg_nr == MACH_C_REGNUM
806            || reg_nr == MACL_C_REGNUM)
807     base_regnum = 17;
808
809   else if (reg_nr == PR_C_REGNUM) 
810     base_regnum = PR_REGNUM;
811
812   else if (reg_nr == T_C_REGNUM) 
813     base_regnum = 19;
814
815   else if (reg_nr == FPSCR_C_REGNUM) 
816     base_regnum = FPSCR_REGNUM; /*???? this register is a mess.  */
817
818   else if (reg_nr == FPUL_C_REGNUM) 
819     base_regnum = gdbarch_fp0_regnum (gdbarch) + 32;
820   
821   return base_regnum;
822 }
823
824 static int
825 sign_extend (int value, int bits)
826 {
827   value = value & ((1 << bits) - 1);
828   return (value & (1 << (bits - 1))
829           ? value | (~((1 << bits) - 1))
830           : value);
831 }
832
833 static void
834 sh64_analyze_prologue (struct gdbarch *gdbarch,
835                        struct sh64_frame_cache *cache,
836                        CORE_ADDR func_pc,
837                        CORE_ADDR current_pc)
838 {
839   int reg_nr;
840   int pc;
841   int opc;
842   int insn;
843   int r0_val = 0;
844   int insn_size;
845   int gdb_register_number;
846   int register_number;
847   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
848   
849   cache->sp_offset = 0;
850
851   /* Loop around examining the prologue insns until we find something
852      that does not appear to be part of the prologue.  But give up
853      after 20 of them, since we're getting silly then.  */
854
855   pc = func_pc;
856
857   if (cache->media_mode)
858     insn_size = 4;
859   else
860     insn_size = 2;
861
862   opc = pc + (insn_size * 28);
863   if (opc > current_pc)
864     opc = current_pc;
865   for ( ; pc <= opc; pc += insn_size)
866     {
867       insn = read_memory_integer (cache->media_mode ? UNMAKE_ISA32_ADDR (pc)
868                                                     : pc,
869                                   insn_size);
870
871       if (!cache->media_mode)
872         {
873           if (IS_STS_PR (insn))
874             {
875               int next_insn = read_memory_integer (pc + insn_size, insn_size);
876               if (IS_MOV_TO_R15 (next_insn))
877                 {
878                   cache->saved_regs[PR_REGNUM] =
879                     cache->sp_offset - ((((next_insn & 0xf) ^ 0x8) - 0x8) << 2);
880                   pc += insn_size;
881                 }
882             }
883
884           else if (IS_MOV_R14 (insn))
885             cache->saved_regs[MEDIA_FP_REGNUM] =
886               cache->sp_offset - ((((insn & 0xf) ^ 0x8) - 0x8) << 2);
887
888           else if (IS_MOV_R0 (insn))
889             {
890               /* Put in R0 the offset from SP at which to store some
891                  registers. We are interested in this value, because it
892                  will tell us where the given registers are stored within
893                  the frame.  */
894               r0_val = ((insn & 0xff) ^ 0x80) - 0x80;
895             }
896
897           else if (IS_ADD_SP_R0 (insn))
898             {
899               /* This instruction still prepares r0, but we don't care.
900                  We already have the offset in r0_val.  */
901             }
902
903           else if (IS_STS_R0 (insn))
904             {
905               /* Store PR at r0_val-4 from SP. Decrement r0 by 4*/
906               cache->saved_regs[PR_REGNUM] = cache->sp_offset - (r0_val - 4);
907               r0_val -= 4;
908             }
909
910           else if (IS_MOV_R14_R0 (insn))
911             {
912               /* Store R14 at r0_val-4 from SP. Decrement r0 by 4 */
913               cache->saved_regs[MEDIA_FP_REGNUM] = cache->sp_offset
914                                                    - (r0_val - 4);
915               r0_val -= 4;
916             }
917
918           else if (IS_ADD_SP (insn))
919             cache->sp_offset -= ((insn & 0xff) ^ 0x80) - 0x80;
920
921           else if (IS_MOV_SP_FP (insn))
922             break;
923         }
924       else
925         {
926           if (IS_ADDIL_SP_MEDIA (insn) || IS_ADDI_SP_MEDIA (insn))
927             cache->sp_offset -=
928               sign_extend ((((insn & 0xffc00) ^ 0x80000) - 0x80000) >> 10, 9);
929
930           else if (IS_STQ_R18_R15 (insn))
931             cache->saved_regs[PR_REGNUM] = 
932               cache->sp_offset - (sign_extend ((insn & 0xffc00) >> 10, 9) << 3);
933
934           else if (IS_STL_R18_R15 (insn))
935             cache->saved_regs[PR_REGNUM] = 
936               cache->sp_offset - (sign_extend ((insn & 0xffc00) >> 10, 9) << 2);
937
938           else if (IS_STQ_R14_R15 (insn))
939             cache->saved_regs[MEDIA_FP_REGNUM] =
940               cache->sp_offset - (sign_extend ((insn & 0xffc00) >> 10, 9) << 3);
941
942           else if (IS_STL_R14_R15 (insn))
943             cache->saved_regs[MEDIA_FP_REGNUM] =
944               cache->sp_offset - (sign_extend ((insn & 0xffc00) >> 10, 9) << 2);
945
946           else if (IS_MOV_SP_FP_MEDIA (insn))
947             break;
948         }
949     }
950
951   if (cache->saved_regs[MEDIA_FP_REGNUM] >= 0)
952     cache->uses_fp = 1;
953 }
954
955 static CORE_ADDR
956 sh64_frame_align (struct gdbarch *ignore, CORE_ADDR sp)
957 {
958   return sp & ~7;
959 }
960
961 /* Function: push_dummy_call
962    Setup the function arguments for calling a function in the inferior.
963
964    On the Renesas SH architecture, there are four registers (R4 to R7)
965    which are dedicated for passing function arguments.  Up to the first
966    four arguments (depending on size) may go into these registers.
967    The rest go on the stack.
968
969    Arguments that are smaller than 4 bytes will still take up a whole
970    register or a whole 32-bit word on the stack, and will be 
971    right-justified in the register or the stack word.  This includes
972    chars, shorts, and small aggregate types.
973
974    Arguments that are larger than 4 bytes may be split between two or 
975    more registers.  If there are not enough registers free, an argument
976    may be passed partly in a register (or registers), and partly on the
977    stack.  This includes doubles, long longs, and larger aggregates. 
978    As far as I know, there is no upper limit to the size of aggregates 
979    that will be passed in this way; in other words, the convention of 
980    passing a pointer to a large aggregate instead of a copy is not used.
981
982    An exceptional case exists for struct arguments (and possibly other
983    aggregates such as arrays) if the size is larger than 4 bytes but 
984    not a multiple of 4 bytes.  In this case the argument is never split 
985    between the registers and the stack, but instead is copied in its
986    entirety onto the stack, AND also copied into as many registers as 
987    there is room for.  In other words, space in registers permitting, 
988    two copies of the same argument are passed in.  As far as I can tell,
989    only the one on the stack is used, although that may be a function 
990    of the level of compiler optimization.  I suspect this is a compiler
991    bug.  Arguments of these odd sizes are left-justified within the 
992    word (as opposed to arguments smaller than 4 bytes, which are 
993    right-justified).
994
995    If the function is to return an aggregate type such as a struct, it 
996    is either returned in the normal return value register R0 (if its 
997    size is no greater than one byte), or else the caller must allocate
998    space into which the callee will copy the return value (if the size
999    is greater than one byte).  In this case, a pointer to the return 
1000    value location is passed into the callee in register R2, which does 
1001    not displace any of the other arguments passed in via registers R4
1002    to R7.   */
1003
1004 /* R2-R9 for integer types and integer equivalent (char, pointers) and
1005    non-scalar (struct, union) elements (even if the elements are
1006    floats).  
1007    FR0-FR11 for single precision floating point (float)
1008    DR0-DR10 for double precision floating point (double) 
1009    
1010    If a float is argument number 3 (for instance) and arguments number
1011    1,2, and 4 are integer, the mapping will be:
1012    arg1 -->R2, arg2 --> R3, arg3 -->FR0, arg4 --> R5. I.e. R4 is not used.
1013    
1014    If a float is argument number 10 (for instance) and arguments number
1015    1 through 10 are integer, the mapping will be:
1016    arg1->R2, arg2->R3, arg3->R4, arg4->R5, arg5->R6, arg6->R7, arg7->R8,
1017    arg8->R9, arg9->(0,SP)stack(8-byte aligned), arg10->FR0, arg11->stack(16,SP).
1018    I.e. there is hole in the stack.
1019
1020    Different rules apply for variable arguments functions, and for functions
1021    for which the prototype is not known.  */
1022
1023 static CORE_ADDR
1024 sh64_push_dummy_call (struct gdbarch *gdbarch,
1025                       struct value *function,
1026                       struct regcache *regcache,
1027                       CORE_ADDR bp_addr,
1028                       int nargs, struct value **args,
1029                       CORE_ADDR sp, int struct_return,
1030                       CORE_ADDR struct_addr)
1031 {
1032   int stack_offset, stack_alloc;
1033   int int_argreg;
1034   int float_argreg;
1035   int double_argreg;
1036   int float_arg_index = 0;
1037   int double_arg_index = 0;
1038   int argnum;
1039   struct type *type;
1040   CORE_ADDR regval;
1041   char *val;
1042   char valbuf[8];
1043   char valbuf_tmp[8];
1044   int len;
1045   int argreg_size;
1046   int fp_args[12];
1047
1048   memset (fp_args, 0, sizeof (fp_args));
1049
1050   /* first force sp to a 8-byte alignment */
1051   sp = sh64_frame_align (gdbarch, sp);
1052
1053   /* The "struct return pointer" pseudo-argument has its own dedicated 
1054      register */
1055
1056   if (struct_return)
1057     regcache_cooked_write_unsigned (regcache, 
1058                                     STRUCT_RETURN_REGNUM, struct_addr);
1059
1060   /* Now make sure there's space on the stack */
1061   for (argnum = 0, stack_alloc = 0; argnum < nargs; argnum++)
1062     stack_alloc += ((TYPE_LENGTH (value_type (args[argnum])) + 7) & ~7);
1063   sp -= stack_alloc;            /* make room on stack for args */
1064
1065   /* Now load as many as possible of the first arguments into
1066      registers, and push the rest onto the stack.  There are 64 bytes
1067      in eight registers available.  Loop thru args from first to last.  */
1068
1069   int_argreg = ARG0_REGNUM;
1070   float_argreg = gdbarch_fp0_regnum (gdbarch);
1071   double_argreg = DR0_REGNUM;
1072
1073   for (argnum = 0, stack_offset = 0; argnum < nargs; argnum++)
1074     {
1075       type = value_type (args[argnum]);
1076       len = TYPE_LENGTH (type);
1077       memset (valbuf, 0, sizeof (valbuf));
1078       
1079       if (TYPE_CODE (type) != TYPE_CODE_FLT)
1080         {
1081           argreg_size = register_size (gdbarch, int_argreg);
1082
1083           if (len < argreg_size)
1084             {
1085               /* value gets right-justified in the register or stack word */
1086               if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1087                 memcpy (valbuf + argreg_size - len,
1088                         (char *) value_contents (args[argnum]), len);
1089               else
1090                 memcpy (valbuf, (char *) value_contents (args[argnum]), len);
1091
1092               val = valbuf;
1093             }
1094           else
1095             val = (char *) value_contents (args[argnum]);
1096
1097           while (len > 0)
1098             {
1099               if (int_argreg > ARGLAST_REGNUM)
1100                 {                       
1101                   /* must go on the stack */
1102                   write_memory (sp + stack_offset, (const bfd_byte *) val,
1103                                 argreg_size);
1104                   stack_offset += 8;/*argreg_size;*/
1105                 }
1106               /* NOTE WELL!!!!!  This is not an "else if" clause!!!
1107                  That's because some *&^%$ things get passed on the stack
1108                  AND in the registers!   */
1109               if (int_argreg <= ARGLAST_REGNUM)
1110                 {                       
1111                   /* there's room in a register */
1112                   regval = extract_unsigned_integer (val, argreg_size);
1113                   regcache_cooked_write_unsigned (regcache, int_argreg, regval);
1114                 }
1115               /* Store the value 8 bytes at a time.  This means that
1116                  things larger than 8 bytes may go partly in registers
1117                  and partly on the stack. FIXME: argreg is incremented
1118                  before we use its size.  */
1119               len -= argreg_size;
1120               val += argreg_size;
1121               int_argreg++;
1122             }
1123         }
1124       else
1125         {
1126           val = (char *) value_contents (args[argnum]);
1127           if (len == 4)
1128             {
1129               /* Where is it going to be stored? */
1130               while (fp_args[float_arg_index])
1131                 float_arg_index ++;
1132
1133               /* Now float_argreg points to the register where it
1134                  should be stored.  Are we still within the allowed
1135                  register set? */
1136               if (float_arg_index <= FLOAT_ARGLAST_REGNUM)
1137                 {
1138                   /* Goes in FR0...FR11 */
1139                   regcache_cooked_write (regcache,
1140                                          gdbarch_fp0_regnum (gdbarch)
1141                                          + float_arg_index,
1142                                          val);
1143                   fp_args[float_arg_index] = 1;
1144                   /* Skip the corresponding general argument register.  */
1145                   int_argreg ++;
1146                 }
1147               else 
1148                 ;
1149                 /* Store it as the integers, 8 bytes at the time, if
1150                    necessary spilling on the stack.  */
1151               
1152             }
1153             else if (len == 8)
1154               {
1155                 /* Where is it going to be stored? */
1156                 while (fp_args[double_arg_index])
1157                   double_arg_index += 2;
1158                 /* Now double_argreg points to the register
1159                    where it should be stored.
1160                    Are we still within the allowed register set? */
1161                 if (double_arg_index < FLOAT_ARGLAST_REGNUM)
1162                   {
1163                     /* Goes in DR0...DR10 */
1164                     /* The numbering of the DRi registers is consecutive,
1165                        i.e. includes odd numbers.  */
1166                     int double_register_offset = double_arg_index / 2;
1167                     int regnum = DR0_REGNUM + double_register_offset;
1168                     regcache_cooked_write (regcache, regnum, val);
1169                     fp_args[double_arg_index] = 1;
1170                     fp_args[double_arg_index + 1] = 1;
1171                     /* Skip the corresponding general argument register.  */
1172                     int_argreg ++;
1173                   }
1174                 else
1175                   ;
1176                   /* Store it as the integers, 8 bytes at the time, if
1177                      necessary spilling on the stack.  */
1178               }
1179         }
1180     }
1181   /* Store return address. */
1182   regcache_cooked_write_unsigned (regcache, PR_REGNUM, bp_addr);
1183
1184   /* Update stack pointer.  */
1185   regcache_cooked_write_unsigned (regcache,
1186                                   gdbarch_sp_regnum (gdbarch), sp);
1187
1188   return sp;
1189 }
1190
1191 /* Find a function's return value in the appropriate registers (in
1192    regbuf), and copy it into valbuf.  Extract from an array REGBUF
1193    containing the (raw) register state a function return value of type
1194    TYPE, and copy that, in virtual format, into VALBUF.  */
1195 static void
1196 sh64_extract_return_value (struct type *type, struct regcache *regcache,
1197                            void *valbuf)
1198 {
1199   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1200   int len = TYPE_LENGTH (type);
1201
1202   if (TYPE_CODE (type) == TYPE_CODE_FLT)
1203     {
1204       if (len == 4)
1205         {
1206           /* Return value stored in gdbarch_fp0_regnum */
1207           regcache_raw_read (regcache,
1208                              gdbarch_fp0_regnum (gdbarch), valbuf);
1209         }
1210       else if (len == 8)
1211         {
1212           /* return value stored in DR0_REGNUM */
1213           DOUBLEST val;
1214           gdb_byte buf[8];
1215
1216           regcache_cooked_read (regcache, DR0_REGNUM, buf);
1217           
1218           if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
1219             floatformat_to_doublest (&floatformat_ieee_double_littlebyte_bigword,
1220                                      buf, &val);
1221           else
1222             floatformat_to_doublest (&floatformat_ieee_double_big,
1223                                      buf, &val);
1224           store_typed_floating (valbuf, type, val);
1225         }
1226     }
1227   else
1228     { 
1229       if (len <= 8)
1230         {
1231           int offset;
1232           char buf[8];
1233           /* Result is in register 2. If smaller than 8 bytes, it is padded 
1234              at the most significant end.  */
1235           regcache_raw_read (regcache, DEFAULT_RETURN_REGNUM, buf);
1236
1237           if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1238             offset = register_size (gdbarch, DEFAULT_RETURN_REGNUM)
1239                      - len;
1240           else
1241             offset = 0;
1242           memcpy (valbuf, buf + offset, len);
1243         }
1244       else
1245         error ("bad size for return value");
1246     }
1247 }
1248
1249 /* Write into appropriate registers a function return value
1250    of type TYPE, given in virtual format.
1251    If the architecture is sh4 or sh3e, store a function's return value
1252    in the R0 general register or in the FP0 floating point register,
1253    depending on the type of the return value. In all the other cases
1254    the result is stored in r0, left-justified.  */
1255
1256 static void
1257 sh64_store_return_value (struct type *type, struct regcache *regcache,
1258                          const void *valbuf)
1259 {
1260   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1261   char buf[64]; /* more than enough...  */
1262   int len = TYPE_LENGTH (type);
1263
1264   if (TYPE_CODE (type) == TYPE_CODE_FLT)
1265     {
1266       int i, regnum = gdbarch_fp0_regnum (gdbarch);
1267       for (i = 0; i < len; i += 4)
1268         if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
1269           regcache_raw_write (regcache, regnum++,
1270                               (char *) valbuf + len - 4 - i);
1271         else
1272           regcache_raw_write (regcache, regnum++, (char *) valbuf + i);
1273     }
1274   else
1275     {
1276       int return_register = DEFAULT_RETURN_REGNUM;
1277       int offset = 0;
1278
1279       if (len <= register_size (gdbarch, return_register))
1280         {
1281           /* Pad with zeros.  */
1282           memset (buf, 0, register_size (gdbarch, return_register));
1283           if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
1284             offset = 0; /*register_size (gdbarch, 
1285                           return_register) - len;*/
1286           else
1287             offset = register_size (gdbarch, return_register) - len;
1288
1289           memcpy (buf + offset, valbuf, len);
1290           regcache_raw_write (regcache, return_register, buf);
1291         }
1292       else
1293         regcache_raw_write (regcache, return_register, valbuf);
1294     }
1295 }
1296
1297 static enum return_value_convention
1298 sh64_return_value (struct gdbarch *gdbarch, struct type *func_type,
1299                    struct type *type, struct regcache *regcache,
1300                    gdb_byte *readbuf, const gdb_byte *writebuf)
1301 {
1302   if (sh64_use_struct_convention (type))
1303     return RETURN_VALUE_STRUCT_CONVENTION;
1304   if (writebuf)
1305     sh64_store_return_value (type, regcache, writebuf);
1306   else if (readbuf)
1307     sh64_extract_return_value (type, regcache, readbuf);
1308   return RETURN_VALUE_REGISTER_CONVENTION;
1309 }
1310
1311 static void
1312 sh64_show_media_regs (struct frame_info *frame)
1313 {
1314   struct gdbarch *gdbarch = get_frame_arch (frame);
1315   int i;
1316
1317   printf_filtered
1318     ("PC=%s SR=%016llx \n",
1319      phex (get_frame_register_unsigned (frame,
1320                                         gdbarch_pc_regnum (gdbarch)), 8),
1321      (long long) get_frame_register_unsigned (frame, SR_REGNUM));
1322
1323   printf_filtered
1324     ("SSR=%016llx SPC=%016llx \n",
1325      (long long) get_frame_register_unsigned (frame, SSR_REGNUM),
1326      (long long) get_frame_register_unsigned (frame, SPC_REGNUM));
1327   printf_filtered
1328     ("FPSCR=%016lx\n ",
1329      (long) get_frame_register_unsigned (frame, FPSCR_REGNUM));
1330
1331   for (i = 0; i < 64; i = i + 4)
1332     printf_filtered
1333       ("\nR%d-R%d  %016llx %016llx %016llx %016llx\n",
1334        i, i + 3,
1335       (long long) get_frame_register_unsigned (frame, i + 0),
1336       (long long) get_frame_register_unsigned (frame, i + 1),
1337       (long long) get_frame_register_unsigned (frame, i + 2),
1338       (long long) get_frame_register_unsigned (frame, i + 3));
1339
1340   printf_filtered ("\n");
1341   
1342   for (i = 0; i < 64; i = i + 8)
1343     printf_filtered
1344       ("FR%d-FR%d  %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
1345        i, i + 7,
1346        (long) get_frame_register_unsigned
1347                 (frame, gdbarch_fp0_regnum (gdbarch) + i + 0),
1348        (long) get_frame_register_unsigned
1349                 (frame, gdbarch_fp0_regnum (gdbarch) + i + 1),
1350        (long) get_frame_register_unsigned
1351                 (frame, gdbarch_fp0_regnum (gdbarch) + i + 2),
1352        (long) get_frame_register_unsigned
1353                 (frame, gdbarch_fp0_regnum (gdbarch) + i + 3),
1354        (long) get_frame_register_unsigned
1355                 (frame, gdbarch_fp0_regnum (gdbarch) + i + 4),
1356        (long) get_frame_register_unsigned
1357                 (frame, gdbarch_fp0_regnum (gdbarch) + i + 5),
1358        (long) get_frame_register_unsigned
1359                 (frame, gdbarch_fp0_regnum (gdbarch) + i + 6),
1360        (long) get_frame_register_unsigned
1361                 (frame, gdbarch_fp0_regnum (gdbarch) + i + 7));
1362 }
1363
1364 static void
1365 sh64_show_compact_regs (struct frame_info *frame)
1366 {
1367   struct gdbarch *gdbarch = get_frame_arch (frame);
1368   int i;
1369
1370   printf_filtered
1371     ("PC=%s \n",
1372      phex (get_frame_register_unsigned (frame, PC_C_REGNUM), 8));
1373
1374   printf_filtered
1375     ("GBR=%08lx MACH=%08lx MACL=%08lx PR=%08lx T=%08lx\n",
1376      (long) get_frame_register_unsigned (frame, GBR_C_REGNUM),
1377      (long) get_frame_register_unsigned (frame, MACH_C_REGNUM),
1378      (long) get_frame_register_unsigned (frame, MACL_C_REGNUM),
1379      (long) get_frame_register_unsigned (frame, PR_C_REGNUM),
1380      (long) get_frame_register_unsigned (frame, T_C_REGNUM));
1381   printf_filtered
1382     ("FPSCR=%08lx FPUL=%08lx\n",
1383      (long) get_frame_register_unsigned (frame, FPSCR_C_REGNUM),
1384      (long) get_frame_register_unsigned (frame, FPUL_C_REGNUM));
1385
1386   for (i = 0; i < 16; i = i + 4)
1387     printf_filtered
1388       ("\nR%d-R%d  %08lx %08lx %08lx %08lx\n",
1389        i, i + 3,
1390        (long) get_frame_register_unsigned (frame, i + 0),
1391        (long) get_frame_register_unsigned (frame, i + 1),
1392        (long) get_frame_register_unsigned (frame, i + 2),
1393        (long) get_frame_register_unsigned (frame, i + 3));
1394
1395   printf_filtered ("\n");
1396   
1397   for (i = 0; i < 16; i = i + 8)
1398     printf_filtered
1399       ("FR%d-FR%d  %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
1400        i, i + 7,
1401        (long) get_frame_register_unsigned
1402                 (frame, gdbarch_fp0_regnum (gdbarch) + i + 0),
1403        (long) get_frame_register_unsigned
1404                 (frame, gdbarch_fp0_regnum (gdbarch) + i + 1),
1405        (long) get_frame_register_unsigned
1406                 (frame, gdbarch_fp0_regnum (gdbarch) + i + 2),
1407        (long) get_frame_register_unsigned
1408                 (frame, gdbarch_fp0_regnum (gdbarch) + i + 3),
1409        (long) get_frame_register_unsigned
1410                 (frame, gdbarch_fp0_regnum (gdbarch) + i + 4),
1411        (long) get_frame_register_unsigned
1412                 (frame, gdbarch_fp0_regnum (gdbarch) + i + 5),
1413        (long) get_frame_register_unsigned
1414                 (frame, gdbarch_fp0_regnum (gdbarch) + i + 6),
1415        (long) get_frame_register_unsigned
1416                 (frame, gdbarch_fp0_regnum (gdbarch) + i + 7));
1417 }
1418
1419 /* FIXME!!! This only shows the registers for shmedia, excluding the
1420    pseudo registers.  */
1421 void
1422 sh64_show_regs (struct frame_info *frame)
1423 {
1424   if (pc_is_isa32 (get_frame_pc (frame)))
1425     sh64_show_media_regs (frame);
1426   else
1427     sh64_show_compact_regs (frame);
1428 }
1429
1430 /* *INDENT-OFF* */
1431 /*
1432     SH MEDIA MODE (ISA 32)
1433     general registers (64-bit) 0-63
1434 0    r0,   r1,   r2,   r3,   r4,   r5,   r6,   r7,
1435 64   r8,   r9,   r10,  r11,  r12,  r13,  r14,  r15,
1436 128  r16,  r17,  r18,  r19,  r20,  r21,  r22,  r23,
1437 192  r24,  r25,  r26,  r27,  r28,  r29,  r30,  r31,
1438 256  r32,  r33,  r34,  r35,  r36,  r37,  r38,  r39,
1439 320  r40,  r41,  r42,  r43,  r44,  r45,  r46,  r47,
1440 384  r48,  r49,  r50,  r51,  r52,  r53,  r54,  r55,
1441 448  r56,  r57,  r58,  r59,  r60,  r61,  r62,  r63,
1442
1443     pc (64-bit) 64
1444 512  pc,
1445
1446     status reg., saved status reg., saved pc reg. (64-bit) 65-67
1447 520  sr,  ssr,  spc,
1448
1449     target registers (64-bit) 68-75
1450 544  tr0,  tr1,  tr2,  tr3,  tr4,  tr5,  tr6,  tr7,
1451
1452     floating point state control register (32-bit) 76
1453 608  fpscr,
1454
1455     single precision floating point registers (32-bit) 77-140
1456 612  fr0,  fr1,  fr2,  fr3,  fr4,  fr5,  fr6,  fr7,
1457 644  fr8,  fr9,  fr10, fr11, fr12, fr13, fr14, fr15,
1458 676  fr16, fr17, fr18, fr19, fr20, fr21, fr22, fr23,
1459 708  fr24, fr25, fr26, fr27, fr28, fr29, fr30, fr31,
1460 740  fr32, fr33, fr34, fr35, fr36, fr37, fr38, fr39,
1461 772  fr40, fr41, fr42, fr43, fr44, fr45, fr46, fr47,
1462 804  fr48, fr49, fr50, fr51, fr52, fr53, fr54, fr55,
1463 836  fr56, fr57, fr58, fr59, fr60, fr61, fr62, fr63,
1464
1465 TOTAL SPACE FOR REGISTERS: 868 bytes
1466
1467 From here on they are all pseudo registers: no memory allocated.
1468 REGISTER_BYTE returns the register byte for the base register.
1469
1470     double precision registers (pseudo) 141-172
1471      dr0,  dr2,  dr4,  dr6,  dr8,  dr10, dr12, dr14,
1472      dr16, dr18, dr20, dr22, dr24, dr26, dr28, dr30,
1473      dr32, dr34, dr36, dr38, dr40, dr42, dr44, dr46,
1474      dr48, dr50, dr52, dr54, dr56, dr58, dr60, dr62,
1475  
1476     floating point pairs (pseudo) 173-204
1477      fp0,  fp2,  fp4,  fp6,  fp8,  fp10, fp12, fp14,
1478      fp16, fp18, fp20, fp22, fp24, fp26, fp28, fp30,
1479      fp32, fp34, fp36, fp38, fp40, fp42, fp44, fp46,
1480      fp48, fp50, fp52, fp54, fp56, fp58, fp60, fp62,
1481  
1482     floating point vectors (4 floating point regs) (pseudo) 205-220
1483      fv0,  fv4,  fv8,  fv12, fv16, fv20, fv24, fv28,
1484      fv32, fv36, fv40, fv44, fv48, fv52, fv56, fv60,
1485  
1486     SH COMPACT MODE (ISA 16) (all pseudo) 221-272
1487      r0_c, r1_c, r2_c,  r3_c,  r4_c,  r5_c,  r6_c,  r7_c,
1488      r8_c, r9_c, r10_c, r11_c, r12_c, r13_c, r14_c, r15_c,
1489      pc_c,
1490      gbr_c, mach_c, macl_c, pr_c, t_c,
1491      fpscr_c, fpul_c,
1492      fr0_c, fr1_c, fr2_c,  fr3_c,  fr4_c,  fr5_c,  fr6_c,  fr7_c,
1493      fr8_c, fr9_c, fr10_c, fr11_c, fr12_c, fr13_c, fr14_c, fr15_c
1494      dr0_c, dr2_c, dr4_c,  dr6_c,  dr8_c,  dr10_c, dr12_c, dr14_c
1495      fv0_c, fv4_c, fv8_c,  fv12_c
1496 */
1497
1498 static struct type *
1499 sh64_build_float_register_type (struct gdbarch *gdbarch, int high)
1500 {
1501   return lookup_array_range_type (builtin_type (gdbarch)->builtin_float,
1502                                   0, high);
1503 }
1504
1505 /* Return the GDB type object for the "standard" data type
1506    of data in register REG_NR.  */
1507 static struct type *
1508 sh64_register_type (struct gdbarch *gdbarch, int reg_nr)
1509 {
1510   if ((reg_nr >= gdbarch_fp0_regnum (gdbarch)
1511        && reg_nr <= FP_LAST_REGNUM)
1512       || (reg_nr >= FP0_C_REGNUM
1513           && reg_nr <= FP_LAST_C_REGNUM))
1514     return builtin_type (gdbarch)->builtin_float;
1515   else if ((reg_nr >= DR0_REGNUM 
1516             && reg_nr <= DR_LAST_REGNUM)
1517            || (reg_nr >= DR0_C_REGNUM 
1518                && reg_nr <= DR_LAST_C_REGNUM))
1519     return builtin_type (gdbarch)->builtin_double;
1520   else if  (reg_nr >= FPP0_REGNUM 
1521             && reg_nr <= FPP_LAST_REGNUM)
1522     return sh64_build_float_register_type (gdbarch, 1);
1523   else if ((reg_nr >= FV0_REGNUM
1524             && reg_nr <= FV_LAST_REGNUM)
1525            ||(reg_nr >= FV0_C_REGNUM 
1526               && reg_nr <= FV_LAST_C_REGNUM))
1527     return sh64_build_float_register_type (gdbarch, 3);
1528   else if (reg_nr == FPSCR_REGNUM)
1529     return builtin_type (gdbarch)->builtin_int;
1530   else if (reg_nr >= R0_C_REGNUM
1531            && reg_nr < FP0_C_REGNUM)
1532     return builtin_type (gdbarch)->builtin_int;
1533   else
1534     return builtin_type (gdbarch)->builtin_long_long;
1535 }
1536
1537 static void
1538 sh64_register_convert_to_virtual (struct gdbarch *gdbarch, int regnum,
1539                                   struct type *type, char *from, char *to)
1540 {
1541   if (gdbarch_byte_order (gdbarch) != BFD_ENDIAN_LITTLE)
1542     {
1543       /* It is a no-op.  */
1544       memcpy (to, from, register_size (gdbarch, regnum));
1545       return;
1546     }
1547
1548   if ((regnum >= DR0_REGNUM 
1549        && regnum <= DR_LAST_REGNUM)
1550       || (regnum >= DR0_C_REGNUM 
1551           && regnum <= DR_LAST_C_REGNUM))
1552     {
1553       DOUBLEST val;
1554       floatformat_to_doublest (&floatformat_ieee_double_littlebyte_bigword, 
1555                                from, &val);
1556       store_typed_floating (to, type, val);
1557     }
1558   else
1559     error ("sh64_register_convert_to_virtual called with non DR register number");
1560 }
1561
1562 static void
1563 sh64_register_convert_to_raw (struct gdbarch *gdbarch, struct type *type,
1564                               int regnum, const void *from, void *to)
1565 {
1566   if (gdbarch_byte_order (gdbarch) != BFD_ENDIAN_LITTLE)
1567     {
1568       /* It is a no-op.  */
1569       memcpy (to, from, register_size (gdbarch, regnum));
1570       return;
1571     }
1572
1573   if ((regnum >= DR0_REGNUM 
1574        && regnum <= DR_LAST_REGNUM)
1575       || (regnum >= DR0_C_REGNUM 
1576           && regnum <= DR_LAST_C_REGNUM))
1577     {
1578       DOUBLEST val = extract_typed_floating (from, type);
1579       floatformat_from_doublest (&floatformat_ieee_double_littlebyte_bigword, 
1580                                  &val, to);
1581     }
1582   else
1583     error ("sh64_register_convert_to_raw called with non DR register number");
1584 }
1585
1586 static void
1587 sh64_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
1588                            int reg_nr, gdb_byte *buffer)
1589 {
1590   int base_regnum;
1591   int portion;
1592   int offset = 0;
1593   char temp_buffer[MAX_REGISTER_SIZE];
1594
1595   if (reg_nr >= DR0_REGNUM 
1596       && reg_nr <= DR_LAST_REGNUM)
1597     {
1598       base_regnum = sh64_dr_reg_base_num (gdbarch, reg_nr);
1599
1600       /* Build the value in the provided buffer.  */ 
1601       /* DR regs are double precision registers obtained by
1602          concatenating 2 single precision floating point registers.  */
1603       for (portion = 0; portion < 2; portion++)
1604         regcache_raw_read (regcache, base_regnum + portion, 
1605                            (temp_buffer
1606                             + register_size (gdbarch, base_regnum) * portion));
1607
1608       /* We must pay attention to the endianness.  */
1609       sh64_register_convert_to_virtual (gdbarch, reg_nr,
1610                                         register_type (gdbarch, reg_nr),
1611                                         temp_buffer, buffer);
1612
1613     }
1614
1615   else if (reg_nr >= FPP0_REGNUM 
1616            && reg_nr <= FPP_LAST_REGNUM)
1617     {
1618       base_regnum = sh64_fpp_reg_base_num (gdbarch, reg_nr);
1619
1620       /* Build the value in the provided buffer.  */ 
1621       /* FPP regs are pairs of single precision registers obtained by
1622          concatenating 2 single precision floating point registers.  */
1623       for (portion = 0; portion < 2; portion++)
1624         regcache_raw_read (regcache, base_regnum + portion, 
1625                            ((char *) buffer
1626                             + register_size (gdbarch, base_regnum) * portion));
1627     }
1628
1629   else if (reg_nr >= FV0_REGNUM 
1630            && reg_nr <= FV_LAST_REGNUM)
1631     {
1632       base_regnum = sh64_fv_reg_base_num (gdbarch, reg_nr);
1633
1634       /* Build the value in the provided buffer.  */ 
1635       /* FV regs are vectors of single precision registers obtained by
1636          concatenating 4 single precision floating point registers.  */
1637       for (portion = 0; portion < 4; portion++)
1638         regcache_raw_read (regcache, base_regnum + portion, 
1639                            ((char *) buffer
1640                             + register_size (gdbarch, base_regnum) * portion));
1641     }
1642
1643   /* sh compact pseudo registers. 1-to-1 with a shmedia register */
1644   else if (reg_nr >= R0_C_REGNUM 
1645            && reg_nr <= T_C_REGNUM)
1646     {
1647       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
1648
1649       /* Build the value in the provided buffer.  */ 
1650       regcache_raw_read (regcache, base_regnum, temp_buffer);
1651       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1652         offset = 4;
1653       memcpy (buffer, temp_buffer + offset, 4); /* get LOWER 32 bits only????*/
1654     }
1655
1656   else if (reg_nr >= FP0_C_REGNUM
1657            && reg_nr <= FP_LAST_C_REGNUM)
1658     {
1659       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
1660
1661       /* Build the value in the provided buffer.  */ 
1662       /* Floating point registers map 1-1 to the media fp regs,
1663          they have the same size and endianness.  */
1664       regcache_raw_read (regcache, base_regnum, buffer);
1665     }
1666
1667   else if (reg_nr >= DR0_C_REGNUM 
1668            && reg_nr <= DR_LAST_C_REGNUM)
1669     {
1670       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
1671
1672       /* DR_C regs are double precision registers obtained by
1673          concatenating 2 single precision floating point registers.  */
1674       for (portion = 0; portion < 2; portion++)
1675         regcache_raw_read (regcache, base_regnum + portion, 
1676                            (temp_buffer
1677                             + register_size (gdbarch, base_regnum) * portion));
1678
1679       /* We must pay attention to the endianness.  */
1680       sh64_register_convert_to_virtual (gdbarch, reg_nr, 
1681                                         register_type (gdbarch, reg_nr),
1682                                         temp_buffer, buffer);
1683     }
1684
1685   else if (reg_nr >= FV0_C_REGNUM 
1686            && reg_nr <= FV_LAST_C_REGNUM)
1687     {
1688       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
1689
1690       /* Build the value in the provided buffer.  */ 
1691       /* FV_C regs are vectors of single precision registers obtained by
1692          concatenating 4 single precision floating point registers.  */
1693       for (portion = 0; portion < 4; portion++)
1694         regcache_raw_read (regcache, base_regnum + portion, 
1695                            ((char *) buffer
1696                             + register_size (gdbarch, base_regnum) * portion));
1697     }
1698
1699   else if (reg_nr == FPSCR_C_REGNUM)
1700     {
1701       int fpscr_base_regnum;
1702       int sr_base_regnum;
1703       unsigned int fpscr_value;
1704       unsigned int sr_value;
1705       unsigned int fpscr_c_value;
1706       unsigned int fpscr_c_part1_value;
1707       unsigned int fpscr_c_part2_value;
1708
1709       fpscr_base_regnum = FPSCR_REGNUM;
1710       sr_base_regnum = SR_REGNUM;
1711
1712       /* Build the value in the provided buffer.  */ 
1713       /* FPSCR_C is a very weird register that contains sparse bits
1714          from the FPSCR and the SR architectural registers.
1715          Specifically: */
1716       /* *INDENT-OFF* */
1717       /*
1718          FPSRC_C bit
1719             0         Bit 0 of FPSCR
1720             1         reserved
1721             2-17      Bit 2-18 of FPSCR
1722             18-20     Bits 12,13,14 of SR
1723             21-31     reserved
1724        */
1725       /* *INDENT-ON* */
1726       /* Get FPSCR into a local buffer */
1727       regcache_raw_read (regcache, fpscr_base_regnum, temp_buffer);
1728       /* Get value as an int.  */
1729       fpscr_value = extract_unsigned_integer (temp_buffer, 4);
1730       /* Get SR into a local buffer */
1731       regcache_raw_read (regcache, sr_base_regnum, temp_buffer);
1732       /* Get value as an int.  */
1733       sr_value = extract_unsigned_integer (temp_buffer, 4);
1734       /* Build the new value.  */
1735       fpscr_c_part1_value = fpscr_value & 0x3fffd;
1736       fpscr_c_part2_value = (sr_value & 0x7000) << 6;
1737       fpscr_c_value = fpscr_c_part1_value | fpscr_c_part2_value;
1738       /* Store that in out buffer!!! */
1739       store_unsigned_integer (buffer, 4, fpscr_c_value);
1740       /* FIXME There is surely an endianness gotcha here.  */
1741     }
1742
1743   else if (reg_nr == FPUL_C_REGNUM)
1744     {
1745       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
1746
1747       /* FPUL_C register is floating point register 32,
1748          same size, same endianness.  */
1749       regcache_raw_read (regcache, base_regnum, buffer);
1750     }
1751 }
1752
1753 static void
1754 sh64_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
1755                             int reg_nr, const gdb_byte *buffer)
1756 {
1757   int base_regnum, portion;
1758   int offset;
1759   char temp_buffer[MAX_REGISTER_SIZE];
1760
1761   if (reg_nr >= DR0_REGNUM
1762       && reg_nr <= DR_LAST_REGNUM)
1763     {
1764       base_regnum = sh64_dr_reg_base_num (gdbarch, reg_nr);
1765       /* We must pay attention to the endianness.  */
1766       sh64_register_convert_to_raw (gdbarch, register_type (gdbarch, reg_nr),
1767                                     reg_nr,
1768                                     buffer, temp_buffer);
1769
1770       /* Write the real regs for which this one is an alias.  */
1771       for (portion = 0; portion < 2; portion++)
1772         regcache_raw_write (regcache, base_regnum + portion, 
1773                             (temp_buffer
1774                              + register_size (gdbarch, 
1775                                               base_regnum) * portion));
1776     }
1777
1778   else if (reg_nr >= FPP0_REGNUM 
1779            && reg_nr <= FPP_LAST_REGNUM)
1780     {
1781       base_regnum = sh64_fpp_reg_base_num (gdbarch, reg_nr);
1782
1783       /* Write the real regs for which this one is an alias.  */
1784       for (portion = 0; portion < 2; portion++)
1785         regcache_raw_write (regcache, base_regnum + portion,
1786                             ((char *) buffer
1787                              + register_size (gdbarch, 
1788                                               base_regnum) * portion));
1789     }
1790
1791   else if (reg_nr >= FV0_REGNUM
1792            && reg_nr <= FV_LAST_REGNUM)
1793     {
1794       base_regnum = sh64_fv_reg_base_num (gdbarch, reg_nr);
1795
1796       /* Write the real regs for which this one is an alias.  */
1797       for (portion = 0; portion < 4; portion++)
1798         regcache_raw_write (regcache, base_regnum + portion,
1799                             ((char *) buffer
1800                              + register_size (gdbarch, 
1801                                               base_regnum) * portion));
1802     }
1803
1804   /* sh compact general pseudo registers. 1-to-1 with a shmedia
1805      register but only 4 bytes of it.  */
1806   else if (reg_nr >= R0_C_REGNUM 
1807            && reg_nr <= T_C_REGNUM)
1808     {
1809       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
1810       /* reg_nr is 32 bit here, and base_regnum is 64 bits.  */
1811       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1812         offset = 4;
1813       else 
1814         offset = 0;
1815       /* Let's read the value of the base register into a temporary
1816          buffer, so that overwriting the last four bytes with the new
1817          value of the pseudo will leave the upper 4 bytes unchanged.  */
1818       regcache_raw_read (regcache, base_regnum, temp_buffer);
1819       /* Write as an 8 byte quantity */
1820       memcpy (temp_buffer + offset, buffer, 4);
1821       regcache_raw_write (regcache, base_regnum, temp_buffer);
1822     }
1823
1824   /* sh floating point compact pseudo registers. 1-to-1 with a shmedia
1825      registers. Both are 4 bytes.  */
1826   else if (reg_nr >= FP0_C_REGNUM
1827                && reg_nr <= FP_LAST_C_REGNUM)
1828     {
1829       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
1830       regcache_raw_write (regcache, base_regnum, buffer);
1831     }
1832
1833   else if (reg_nr >= DR0_C_REGNUM 
1834            && reg_nr <= DR_LAST_C_REGNUM)
1835     {
1836       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
1837       for (portion = 0; portion < 2; portion++)
1838         {
1839           /* We must pay attention to the endianness.  */
1840           sh64_register_convert_to_raw (gdbarch,
1841                                         register_type (gdbarch, reg_nr),
1842                                         reg_nr,
1843                                         buffer, temp_buffer);
1844
1845           regcache_raw_write (regcache, base_regnum + portion,
1846                               (temp_buffer
1847                                + register_size (gdbarch, 
1848                                                 base_regnum) * portion));
1849         }
1850     }
1851
1852   else if (reg_nr >= FV0_C_REGNUM 
1853            && reg_nr <= FV_LAST_C_REGNUM)
1854     {
1855       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
1856      
1857       for (portion = 0; portion < 4; portion++)
1858         {
1859           regcache_raw_write (regcache, base_regnum + portion,
1860                               ((char *) buffer
1861                                + register_size (gdbarch, 
1862                                                 base_regnum) * portion));
1863         }
1864     }
1865
1866   else if (reg_nr == FPSCR_C_REGNUM)
1867     {      
1868       int fpscr_base_regnum;
1869       int sr_base_regnum;
1870       unsigned int fpscr_value;
1871       unsigned int sr_value;
1872       unsigned int old_fpscr_value;
1873       unsigned int old_sr_value;
1874       unsigned int fpscr_c_value;
1875       unsigned int fpscr_mask;
1876       unsigned int sr_mask;
1877
1878       fpscr_base_regnum = FPSCR_REGNUM;
1879       sr_base_regnum = SR_REGNUM;
1880
1881       /* FPSCR_C is a very weird register that contains sparse bits
1882          from the FPSCR and the SR architectural registers.
1883          Specifically: */
1884       /* *INDENT-OFF* */
1885       /*
1886          FPSRC_C bit
1887             0         Bit 0 of FPSCR
1888             1         reserved
1889             2-17      Bit 2-18 of FPSCR
1890             18-20     Bits 12,13,14 of SR
1891             21-31     reserved
1892        */
1893       /* *INDENT-ON* */
1894       /* Get value as an int.  */
1895       fpscr_c_value = extract_unsigned_integer (buffer, 4);
1896
1897       /* Build the new values.  */
1898       fpscr_mask = 0x0003fffd;
1899       sr_mask = 0x001c0000;
1900        
1901       fpscr_value = fpscr_c_value & fpscr_mask;
1902       sr_value = (fpscr_value & sr_mask) >> 6;
1903       
1904       regcache_raw_read (regcache, fpscr_base_regnum, temp_buffer);
1905       old_fpscr_value = extract_unsigned_integer (temp_buffer, 4);
1906       old_fpscr_value &= 0xfffc0002;
1907       fpscr_value |= old_fpscr_value;
1908       store_unsigned_integer (temp_buffer, 4, fpscr_value);
1909       regcache_raw_write (regcache, fpscr_base_regnum, temp_buffer);
1910       
1911       regcache_raw_read (regcache, sr_base_regnum, temp_buffer);
1912       old_sr_value = extract_unsigned_integer (temp_buffer, 4);
1913       old_sr_value &= 0xffff8fff;
1914       sr_value |= old_sr_value;
1915       store_unsigned_integer (temp_buffer, 4, sr_value);
1916       regcache_raw_write (regcache, sr_base_regnum, temp_buffer);
1917     }
1918
1919   else if (reg_nr == FPUL_C_REGNUM)
1920     {
1921       base_regnum = sh64_compact_reg_base_num (gdbarch, reg_nr);
1922       regcache_raw_write (regcache, base_regnum, buffer);
1923     }
1924 }
1925
1926 /* FIXME:!! THIS SHOULD TAKE CARE OF GETTING THE RIGHT PORTION OF THE
1927    shmedia REGISTERS.  */
1928 /* Control registers, compact mode.  */
1929 static void
1930 sh64_do_cr_c_register_info (struct ui_file *file, struct frame_info *frame,
1931                             int cr_c_regnum)
1932 {
1933   switch (cr_c_regnum)
1934     {
1935     case PC_C_REGNUM:
1936       fprintf_filtered (file, "pc_c\t0x%08x\n",
1937           (int) get_frame_register_unsigned (frame, cr_c_regnum));
1938       break;
1939     case GBR_C_REGNUM: 
1940       fprintf_filtered (file, "gbr_c\t0x%08x\n",
1941           (int) get_frame_register_unsigned (frame, cr_c_regnum));
1942       break;
1943     case MACH_C_REGNUM: 
1944       fprintf_filtered (file, "mach_c\t0x%08x\n",
1945           (int) get_frame_register_unsigned (frame, cr_c_regnum));
1946       break;
1947     case MACL_C_REGNUM: 
1948       fprintf_filtered (file, "macl_c\t0x%08x\n",
1949           (int) get_frame_register_unsigned (frame, cr_c_regnum));
1950       break;
1951     case PR_C_REGNUM: 
1952       fprintf_filtered (file, "pr_c\t0x%08x\n",
1953           (int) get_frame_register_unsigned (frame, cr_c_regnum));
1954       break;
1955     case T_C_REGNUM: 
1956       fprintf_filtered (file, "t_c\t0x%08x\n",
1957           (int) get_frame_register_unsigned (frame, cr_c_regnum));
1958       break;
1959     case FPSCR_C_REGNUM: 
1960       fprintf_filtered (file, "fpscr_c\t0x%08x\n",
1961           (int) get_frame_register_unsigned (frame, cr_c_regnum));
1962       break;
1963     case FPUL_C_REGNUM:
1964       fprintf_filtered (file, "fpul_c\t0x%08x\n",
1965           (int) get_frame_register_unsigned (frame, cr_c_regnum));
1966       break;
1967     }
1968 }
1969
1970 static void
1971 sh64_do_fp_register (struct gdbarch *gdbarch, struct ui_file *file,
1972                      struct frame_info *frame, int regnum)
1973 {                               /* do values for FP (float) regs */
1974   unsigned char *raw_buffer;
1975   double flt;   /* double extracted from raw hex data */
1976   int inv;
1977   int j;
1978
1979   /* Allocate space for the float.  */
1980   raw_buffer = (unsigned char *) alloca
1981                                  (register_size (gdbarch,
1982                                                  gdbarch_fp0_regnum
1983                                                    (gdbarch)));
1984
1985   /* Get the data in raw format.  */
1986   if (!frame_register_read (frame, regnum, raw_buffer))
1987     error ("can't read register %d (%s)",
1988            regnum, gdbarch_register_name (gdbarch, regnum));
1989
1990   /* Get the register as a number */ 
1991   flt = unpack_double (builtin_type (gdbarch)->builtin_float, raw_buffer, &inv);
1992
1993   /* Print the name and some spaces.  */
1994   fputs_filtered (gdbarch_register_name (gdbarch, regnum), file);
1995   print_spaces_filtered (15 - strlen (gdbarch_register_name
1996                                         (gdbarch, regnum)), file);
1997
1998   /* Print the value.  */
1999   if (inv)
2000     fprintf_filtered (file, "<invalid float>");
2001   else
2002     fprintf_filtered (file, "%-10.9g", flt);
2003
2004   /* Print the fp register as hex.  */
2005   fprintf_filtered (file, "\t(raw 0x");
2006   for (j = 0; j < register_size (gdbarch, regnum); j++)
2007     {
2008       int idx = gdbarch_byte_order (gdbarch)
2009                 == BFD_ENDIAN_BIG ? j : register_size
2010                 (gdbarch, regnum) - 1 - j;
2011       fprintf_filtered (file, "%02x", raw_buffer[idx]);
2012     }
2013   fprintf_filtered (file, ")");
2014   fprintf_filtered (file, "\n");
2015 }
2016
2017 static void
2018 sh64_do_pseudo_register (struct gdbarch *gdbarch, struct ui_file *file,
2019                          struct frame_info *frame, int regnum)
2020 {
2021   /* All the sh64-compact mode registers are pseudo registers.  */
2022
2023   if (regnum < gdbarch_num_regs (gdbarch)
2024       || regnum >= gdbarch_num_regs (gdbarch)
2025                    + NUM_PSEUDO_REGS_SH_MEDIA
2026                    + NUM_PSEUDO_REGS_SH_COMPACT)
2027     internal_error (__FILE__, __LINE__,
2028                     _("Invalid pseudo register number %d\n"), regnum);
2029
2030   else if ((regnum >= DR0_REGNUM && regnum <= DR_LAST_REGNUM))
2031     {
2032       int fp_regnum = sh64_dr_reg_base_num (gdbarch, regnum);
2033       fprintf_filtered (file, "dr%d\t0x%08x%08x\n", regnum - DR0_REGNUM, 
2034           (unsigned) get_frame_register_unsigned (frame, fp_regnum),
2035           (unsigned) get_frame_register_unsigned (frame, fp_regnum + 1));
2036     }
2037
2038   else if ((regnum >= DR0_C_REGNUM && regnum <= DR_LAST_C_REGNUM))
2039     {
2040       int fp_regnum = sh64_compact_reg_base_num (gdbarch, regnum);
2041       fprintf_filtered (file, "dr%d_c\t0x%08x%08x\n", regnum - DR0_C_REGNUM,
2042           (unsigned) get_frame_register_unsigned (frame, fp_regnum),
2043           (unsigned) get_frame_register_unsigned (frame, fp_regnum + 1));
2044     }
2045
2046   else if ((regnum >= FV0_REGNUM && regnum <= FV_LAST_REGNUM))
2047     {
2048       int fp_regnum = sh64_fv_reg_base_num (gdbarch, regnum);
2049       fprintf_filtered (file, "fv%d\t0x%08x\t0x%08x\t0x%08x\t0x%08x\n", 
2050            regnum - FV0_REGNUM, 
2051            (unsigned) get_frame_register_unsigned (frame, fp_regnum),
2052            (unsigned) get_frame_register_unsigned (frame, fp_regnum + 1),
2053            (unsigned) get_frame_register_unsigned (frame, fp_regnum + 2),
2054            (unsigned) get_frame_register_unsigned (frame, fp_regnum + 3));
2055     }
2056            
2057   else if ((regnum >= FV0_C_REGNUM && regnum <= FV_LAST_C_REGNUM))
2058     {
2059       int fp_regnum = sh64_compact_reg_base_num (gdbarch, regnum);
2060       fprintf_filtered (file, "fv%d_c\t0x%08x\t0x%08x\t0x%08x\t0x%08x\n", 
2061            regnum - FV0_C_REGNUM, 
2062            (unsigned) get_frame_register_unsigned (frame, fp_regnum),
2063            (unsigned) get_frame_register_unsigned (frame, fp_regnum + 1),
2064            (unsigned) get_frame_register_unsigned (frame, fp_regnum + 2),
2065            (unsigned) get_frame_register_unsigned (frame, fp_regnum + 3));
2066     }
2067
2068   else if (regnum >= FPP0_REGNUM && regnum <= FPP_LAST_REGNUM)
2069     {
2070       int fp_regnum = sh64_fpp_reg_base_num (gdbarch, regnum);
2071       fprintf_filtered (file, "fpp%d\t0x%08x\t0x%08x\n", regnum - FPP0_REGNUM, 
2072           (unsigned) get_frame_register_unsigned (frame, fp_regnum),
2073           (unsigned) get_frame_register_unsigned (frame, fp_regnum + 1));
2074     }
2075
2076   else if (regnum >= R0_C_REGNUM && regnum <= R_LAST_C_REGNUM)
2077     {
2078       int c_regnum = sh64_compact_reg_base_num (gdbarch, regnum);
2079       fprintf_filtered (file, "r%d_c\t0x%08x\n", regnum - R0_C_REGNUM, 
2080            (unsigned) get_frame_register_unsigned (frame, c_regnum));
2081     }
2082   else if (regnum >= FP0_C_REGNUM && regnum <= FP_LAST_C_REGNUM)
2083     /* This should work also for pseudoregs.  */
2084     sh64_do_fp_register (gdbarch, file, frame, regnum);
2085   else if (regnum >= PC_C_REGNUM && regnum <= FPUL_C_REGNUM)
2086     sh64_do_cr_c_register_info (file, frame, regnum);
2087 }
2088
2089 static void
2090 sh64_do_register (struct gdbarch *gdbarch, struct ui_file *file,
2091                   struct frame_info *frame, int regnum)
2092 {
2093   unsigned char raw_buffer[MAX_REGISTER_SIZE];
2094   struct value_print_options opts;
2095
2096   fputs_filtered (gdbarch_register_name (gdbarch, regnum), file);
2097   print_spaces_filtered (15 - strlen (gdbarch_register_name
2098                                       (gdbarch, regnum)), file);
2099
2100   /* Get the data in raw format.  */
2101   if (!frame_register_read (frame, regnum, raw_buffer))
2102     fprintf_filtered (file, "*value not available*\n");
2103
2104   get_formatted_print_options (&opts, 'x');
2105   opts.deref_ref = 1;
2106   val_print (register_type (gdbarch, regnum), raw_buffer, 0, 0,
2107              file, 0, &opts, current_language);
2108   fprintf_filtered (file, "\t");
2109   get_formatted_print_options (&opts, 0);
2110   opts.deref_ref = 1;
2111   val_print (register_type (gdbarch, regnum), raw_buffer, 0, 0,
2112              file, 0, &opts, current_language);
2113   fprintf_filtered (file, "\n");
2114 }
2115
2116 static void
2117 sh64_print_register (struct gdbarch *gdbarch, struct ui_file *file,
2118                      struct frame_info *frame, int regnum)
2119 {
2120   if (regnum < 0 || regnum >= gdbarch_num_regs (gdbarch)
2121                               + gdbarch_num_pseudo_regs (gdbarch))
2122     internal_error (__FILE__, __LINE__,
2123                     _("Invalid register number %d\n"), regnum);
2124
2125   else if (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch))
2126     {
2127       if (TYPE_CODE (register_type (gdbarch, regnum)) == TYPE_CODE_FLT)
2128         sh64_do_fp_register (gdbarch, file, frame, regnum);     /* FP regs */
2129       else
2130         sh64_do_register (gdbarch, file, frame, regnum);
2131     }
2132
2133   else if (regnum < gdbarch_num_regs (gdbarch)
2134                     + gdbarch_num_pseudo_regs (gdbarch))
2135     sh64_do_pseudo_register (gdbarch, file, frame, regnum);
2136 }
2137
2138 static void
2139 sh64_media_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
2140                                  struct frame_info *frame, int regnum,
2141                                  int fpregs)
2142 {
2143   if (regnum != -1)             /* do one specified register */
2144     {
2145       if (*(gdbarch_register_name (gdbarch, regnum)) == '\0')
2146         error ("Not a valid register for the current processor type");
2147
2148       sh64_print_register (gdbarch, file, frame, regnum);
2149     }
2150   else
2151     /* do all (or most) registers */
2152     {
2153       regnum = 0;
2154       while (regnum < gdbarch_num_regs (gdbarch))
2155         {
2156           /* If the register name is empty, it is undefined for this
2157              processor, so don't display anything.  */
2158           if (gdbarch_register_name (gdbarch, regnum) == NULL
2159               || *(gdbarch_register_name (gdbarch, regnum)) == '\0')
2160             { 
2161               regnum++;
2162               continue;
2163             }
2164
2165           if (TYPE_CODE (register_type (gdbarch, regnum))
2166               == TYPE_CODE_FLT)
2167             {
2168               if (fpregs)
2169                 {
2170                   /* true for "INFO ALL-REGISTERS" command */
2171                   sh64_do_fp_register (gdbarch, file, frame, regnum);
2172                   regnum ++;
2173                 }
2174               else
2175                 regnum += FP_LAST_REGNUM - gdbarch_fp0_regnum (gdbarch);
2176                 /* skip FP regs */
2177             }
2178           else
2179             {
2180               sh64_do_register (gdbarch, file, frame, regnum);
2181               regnum++;
2182             }
2183         }
2184
2185       if (fpregs)
2186         while (regnum < gdbarch_num_regs (gdbarch)
2187                         + gdbarch_num_pseudo_regs (gdbarch))
2188           {
2189             sh64_do_pseudo_register (gdbarch, file, frame, regnum);
2190             regnum++;
2191           }
2192     }
2193 }
2194
2195 static void
2196 sh64_compact_print_registers_info (struct gdbarch *gdbarch,
2197                                    struct ui_file *file,
2198                                    struct frame_info *frame, int regnum,
2199                                    int fpregs)
2200 {
2201   if (regnum != -1)             /* do one specified register */
2202     {
2203       if (*(gdbarch_register_name (gdbarch, regnum)) == '\0')
2204         error ("Not a valid register for the current processor type");
2205
2206       if (regnum >= 0 && regnum < R0_C_REGNUM)
2207         error ("Not a valid register for the current processor mode.");
2208
2209       sh64_print_register (gdbarch, file, frame, regnum);
2210     }
2211   else
2212     /* do all compact registers */
2213     {
2214       regnum = R0_C_REGNUM;
2215       while (regnum < gdbarch_num_regs (gdbarch)
2216                       + gdbarch_num_pseudo_regs (gdbarch))
2217         {
2218           sh64_do_pseudo_register (gdbarch, file, frame, regnum);
2219           regnum++;
2220         }
2221     }
2222 }
2223
2224 static void
2225 sh64_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
2226                            struct frame_info *frame, int regnum, int fpregs)
2227 {
2228   if (pc_is_isa32 (get_frame_pc (frame)))
2229     sh64_media_print_registers_info (gdbarch, file, frame, regnum, fpregs);
2230   else
2231     sh64_compact_print_registers_info (gdbarch, file, frame, regnum, fpregs);
2232 }
2233
2234 static struct sh64_frame_cache *
2235 sh64_alloc_frame_cache (void)
2236 {
2237   struct sh64_frame_cache *cache;
2238   int i;
2239
2240   cache = FRAME_OBSTACK_ZALLOC (struct sh64_frame_cache);
2241
2242   /* Base address.  */
2243   cache->base = 0;
2244   cache->saved_sp = 0;
2245   cache->sp_offset = 0;
2246   cache->pc = 0;
2247
2248   /* Frameless until proven otherwise.  */
2249   cache->uses_fp = 0;
2250
2251   /* Saved registers.  We initialize these to -1 since zero is a valid
2252      offset (that's where fp is supposed to be stored).  */
2253   for (i = 0; i < SIM_SH64_NR_REGS; i++)
2254     {
2255       cache->saved_regs[i] = -1;
2256     }
2257
2258   return cache;
2259 }
2260
2261 static struct sh64_frame_cache *
2262 sh64_frame_cache (struct frame_info *this_frame, void **this_cache)
2263 {
2264   struct gdbarch *gdbarch;
2265   struct sh64_frame_cache *cache;
2266   CORE_ADDR current_pc;
2267   int i;
2268
2269   if (*this_cache)
2270     return *this_cache;
2271
2272   gdbarch = get_frame_arch (this_frame);
2273   cache = sh64_alloc_frame_cache ();
2274   *this_cache = cache;
2275
2276   current_pc = get_frame_pc (this_frame);
2277   cache->media_mode = pc_is_isa32 (current_pc);
2278
2279   /* In principle, for normal frames, fp holds the frame pointer,
2280      which holds the base address for the current stack frame.
2281      However, for functions that don't need it, the frame pointer is
2282      optional.  For these "frameless" functions the frame pointer is
2283      actually the frame pointer of the calling frame. */
2284   cache->base = get_frame_register_unsigned (this_frame, MEDIA_FP_REGNUM);
2285   if (cache->base == 0)
2286     return cache;
2287
2288   cache->pc = get_frame_func (this_frame);
2289   if (cache->pc != 0)
2290     sh64_analyze_prologue (gdbarch, cache, cache->pc, current_pc);
2291
2292   if (!cache->uses_fp)
2293     {
2294       /* We didn't find a valid frame, which means that CACHE->base
2295          currently holds the frame pointer for our calling frame.  If
2296          we're at the start of a function, or somewhere half-way its
2297          prologue, the function's frame probably hasn't been fully
2298          setup yet.  Try to reconstruct the base address for the stack
2299          frame by looking at the stack pointer.  For truly "frameless"
2300          functions this might work too.  */
2301       cache->base = get_frame_register_unsigned
2302                     (this_frame, gdbarch_sp_regnum (gdbarch));
2303     }
2304
2305   /* Now that we have the base address for the stack frame we can
2306      calculate the value of sp in the calling frame.  */
2307   cache->saved_sp = cache->base + cache->sp_offset;
2308
2309   /* Adjust all the saved registers such that they contain addresses
2310      instead of offsets.  */
2311   for (i = 0; i < SIM_SH64_NR_REGS; i++)
2312     if (cache->saved_regs[i] != -1)
2313       cache->saved_regs[i] = cache->saved_sp - cache->saved_regs[i];
2314
2315   return cache;
2316 }
2317
2318 static struct value *
2319 sh64_frame_prev_register (struct frame_info *this_frame,
2320                           void **this_cache, int regnum)
2321 {
2322   struct sh64_frame_cache *cache = sh64_frame_cache (this_frame, this_cache);
2323   struct gdbarch *gdbarch = get_frame_arch (this_frame);
2324
2325   gdb_assert (regnum >= 0);
2326
2327   if (regnum == gdbarch_sp_regnum (gdbarch) && cache->saved_sp)
2328     frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
2329
2330   /* The PC of the previous frame is stored in the PR register of
2331      the current frame.  Frob regnum so that we pull the value from
2332      the correct place.  */
2333   if (regnum == gdbarch_pc_regnum (gdbarch))
2334     regnum = PR_REGNUM;
2335
2336   if (regnum < SIM_SH64_NR_REGS && cache->saved_regs[regnum] != -1)
2337     {
2338       if (gdbarch_tdep (gdbarch)->sh_abi == SH_ABI_32
2339           && (regnum == MEDIA_FP_REGNUM || regnum == PR_REGNUM))
2340         {
2341           CORE_ADDR val;
2342           val = read_memory_unsigned_integer (cache->saved_regs[regnum], 4);
2343           return frame_unwind_got_constant (this_frame, regnum, val);
2344         }
2345
2346       return frame_unwind_got_memory (this_frame, regnum,
2347                                       cache->saved_regs[regnum]);
2348     }
2349
2350   return frame_unwind_got_register (this_frame, regnum, regnum);
2351 }
2352
2353 static void
2354 sh64_frame_this_id (struct frame_info *this_frame, void **this_cache,
2355                     struct frame_id *this_id)
2356 {
2357   struct sh64_frame_cache *cache = sh64_frame_cache (this_frame, this_cache);
2358
2359   /* This marks the outermost frame.  */
2360   if (cache->base == 0)
2361     return;
2362
2363   *this_id = frame_id_build (cache->saved_sp, cache->pc);
2364 }
2365
2366 static const struct frame_unwind sh64_frame_unwind = {
2367   NORMAL_FRAME,
2368   sh64_frame_this_id,
2369   sh64_frame_prev_register,
2370   NULL,
2371   default_frame_sniffer
2372 };
2373
2374 static CORE_ADDR
2375 sh64_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
2376 {
2377   return frame_unwind_register_unsigned (next_frame,
2378                                          gdbarch_sp_regnum (gdbarch));
2379 }
2380
2381 static CORE_ADDR
2382 sh64_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
2383 {
2384   return frame_unwind_register_unsigned (next_frame,
2385                                          gdbarch_pc_regnum (gdbarch));
2386 }
2387
2388 static struct frame_id
2389 sh64_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
2390 {
2391   CORE_ADDR sp = get_frame_register_unsigned (this_frame,
2392                                               gdbarch_sp_regnum (gdbarch));
2393   return frame_id_build (sp, get_frame_pc (this_frame));
2394 }
2395
2396 static CORE_ADDR
2397 sh64_frame_base_address (struct frame_info *this_frame, void **this_cache)
2398 {
2399   struct sh64_frame_cache *cache = sh64_frame_cache (this_frame, this_cache);
2400
2401   return cache->base;
2402 }
2403
2404 static const struct frame_base sh64_frame_base = {
2405   &sh64_frame_unwind,
2406   sh64_frame_base_address,
2407   sh64_frame_base_address,
2408   sh64_frame_base_address
2409 };
2410
2411
2412 struct gdbarch *
2413 sh64_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2414 {
2415   struct gdbarch *gdbarch;
2416   struct gdbarch_tdep *tdep;
2417
2418   /* If there is already a candidate, use it.  */
2419   arches = gdbarch_list_lookup_by_info (arches, &info);
2420   if (arches != NULL)
2421     return arches->gdbarch;
2422
2423   /* None found, create a new architecture from the information
2424      provided.  */
2425   tdep = XMALLOC (struct gdbarch_tdep);
2426   gdbarch = gdbarch_alloc (&info, tdep);
2427
2428   /* Determine the ABI */
2429   if (info.abfd && bfd_get_arch_size (info.abfd) == 64)
2430     {
2431       /* If the ABI is the 64-bit one, it can only be sh-media.  */
2432       tdep->sh_abi = SH_ABI_64;
2433       set_gdbarch_ptr_bit (gdbarch, 8 * TARGET_CHAR_BIT);
2434       set_gdbarch_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
2435     }
2436   else
2437     {
2438       /* If the ABI is the 32-bit one it could be either media or
2439          compact.  */
2440       tdep->sh_abi = SH_ABI_32;
2441       set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
2442       set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
2443     }
2444
2445   set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
2446   set_gdbarch_int_bit (gdbarch, 4 * TARGET_CHAR_BIT);
2447   set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
2448   set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
2449   set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
2450   set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
2451   set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
2452
2453   /* The number of real registers is the same whether we are in 
2454      ISA16(compact) or ISA32(media).  */
2455   set_gdbarch_num_regs (gdbarch, SIM_SH64_NR_REGS);
2456   set_gdbarch_sp_regnum (gdbarch, 15);
2457   set_gdbarch_pc_regnum (gdbarch, 64);
2458   set_gdbarch_fp0_regnum (gdbarch, SIM_SH64_FR0_REGNUM);
2459   set_gdbarch_num_pseudo_regs (gdbarch, NUM_PSEUDO_REGS_SH_MEDIA
2460                                         + NUM_PSEUDO_REGS_SH_COMPACT);
2461
2462   set_gdbarch_register_name (gdbarch, sh64_register_name);
2463   set_gdbarch_register_type (gdbarch, sh64_register_type);
2464
2465   set_gdbarch_pseudo_register_read (gdbarch, sh64_pseudo_register_read);
2466   set_gdbarch_pseudo_register_write (gdbarch, sh64_pseudo_register_write);
2467
2468   set_gdbarch_breakpoint_from_pc (gdbarch, sh64_breakpoint_from_pc);
2469
2470   set_gdbarch_print_insn (gdbarch, print_insn_sh);
2471   set_gdbarch_register_sim_regno (gdbarch, legacy_register_sim_regno);
2472
2473   set_gdbarch_return_value (gdbarch, sh64_return_value);
2474
2475   set_gdbarch_skip_prologue (gdbarch, sh64_skip_prologue);
2476   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2477
2478   set_gdbarch_push_dummy_call (gdbarch, sh64_push_dummy_call);
2479
2480   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
2481
2482   set_gdbarch_frame_align (gdbarch, sh64_frame_align);
2483   set_gdbarch_unwind_sp (gdbarch, sh64_unwind_sp);
2484   set_gdbarch_unwind_pc (gdbarch, sh64_unwind_pc);
2485   set_gdbarch_dummy_id (gdbarch, sh64_dummy_id);
2486   frame_base_set_default (gdbarch, &sh64_frame_base);
2487
2488   set_gdbarch_print_registers_info (gdbarch, sh64_print_registers_info);
2489
2490   set_gdbarch_elf_make_msymbol_special (gdbarch,
2491                                         sh64_elf_make_msymbol_special);
2492
2493   /* Hook in ABI-specific overrides, if they have been registered.  */
2494   gdbarch_init_osabi (info, gdbarch);
2495
2496   dwarf2_append_unwinders (gdbarch);
2497   frame_unwind_append_unwinder (gdbarch, &sh64_frame_unwind);
2498
2499   return gdbarch;
2500 }