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