Delegate opcodes to select disassembler in GDB
[external/binutils.git] / gdb / sh-tdep.c
1 /* Target-dependent code for Renesas Super-H, for GDB.
2
3    Copyright (C) 1993-2017 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 /* Contributed by Steve Chamberlain
21    sac@cygnus.com.  */
22
23 #include "defs.h"
24 #include "frame.h"
25 #include "frame-base.h"
26 #include "frame-unwind.h"
27 #include "dwarf2-frame.h"
28 #include "symtab.h"
29 #include "gdbtypes.h"
30 #include "gdbcmd.h"
31 #include "gdbcore.h"
32 #include "value.h"
33 #include "dis-asm.h"
34 #include "inferior.h"
35 #include "arch-utils.h"
36 #include "floatformat.h"
37 #include "regcache.h"
38 #include "doublest.h"
39 #include "osabi.h"
40 #include "reggroups.h"
41 #include "regset.h"
42 #include "objfiles.h"
43
44 #include "sh-tdep.h"
45 #include "sh64-tdep.h"
46
47 #include "elf-bfd.h"
48 #include "solib-svr4.h"
49
50 /* sh flags */
51 #include "elf/sh.h"
52 #include "dwarf2.h"
53 /* registers numbers shared with the simulator.  */
54 #include "gdb/sim-sh.h"
55 #include <algorithm>
56
57 /* List of "set sh ..." and "show sh ..." commands.  */
58 static struct cmd_list_element *setshcmdlist = NULL;
59 static struct cmd_list_element *showshcmdlist = NULL;
60
61 static const char sh_cc_gcc[] = "gcc";
62 static const char sh_cc_renesas[] = "renesas";
63 static const char *const sh_cc_enum[] = {
64   sh_cc_gcc,
65   sh_cc_renesas, 
66   NULL
67 };
68
69 static const char *sh_active_calling_convention = sh_cc_gcc;
70
71 #define SH_NUM_REGS 67
72
73 struct sh_frame_cache
74 {
75   /* Base address.  */
76   CORE_ADDR base;
77   LONGEST sp_offset;
78   CORE_ADDR pc;
79
80   /* Flag showing that a frame has been created in the prologue code.  */
81   int uses_fp;
82
83   /* Saved registers.  */
84   CORE_ADDR saved_regs[SH_NUM_REGS];
85   CORE_ADDR saved_sp;
86 };
87
88 static int
89 sh_is_renesas_calling_convention (struct type *func_type)
90 {
91   int val = 0;
92
93   if (func_type)
94     {
95       func_type = check_typedef (func_type);
96
97       if (TYPE_CODE (func_type) == TYPE_CODE_PTR)
98         func_type = check_typedef (TYPE_TARGET_TYPE (func_type));
99
100       if (TYPE_CODE (func_type) == TYPE_CODE_FUNC
101           && TYPE_CALLING_CONVENTION (func_type) == DW_CC_GNU_renesas_sh)
102         val = 1;
103     }
104
105   if (sh_active_calling_convention == sh_cc_renesas)
106     val = 1;
107
108   return val;
109 }
110
111 static const char *
112 sh_sh_register_name (struct gdbarch *gdbarch, int reg_nr)
113 {
114   static const char *register_names[] = {
115     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
116     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
117     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
118     "", "",
119     "", "", "", "", "", "", "", "",
120     "", "", "", "", "", "", "", "",
121     "", "",
122     "", "", "", "", "", "", "", "",
123     "", "", "", "", "", "", "", "",
124     "", "", "", "", "", "", "", "",
125   };
126   if (reg_nr < 0)
127     return NULL;
128   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
129     return NULL;
130   return register_names[reg_nr];
131 }
132
133 static const char *
134 sh_sh3_register_name (struct gdbarch *gdbarch, int reg_nr)
135 {
136   static const char *register_names[] = {
137     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
138     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
139     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
140     "", "",
141     "", "", "", "", "", "", "", "",
142     "", "", "", "", "", "", "", "",
143     "ssr", "spc",
144     "r0b0", "r1b0", "r2b0", "r3b0", "r4b0", "r5b0", "r6b0", "r7b0",
145     "r0b1", "r1b1", "r2b1", "r3b1", "r4b1", "r5b1", "r6b1", "r7b1"
146     "", "", "", "", "", "", "", "",
147   };
148   if (reg_nr < 0)
149     return NULL;
150   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
151     return NULL;
152   return register_names[reg_nr];
153 }
154
155 static const char *
156 sh_sh3e_register_name (struct gdbarch *gdbarch, int reg_nr)
157 {
158   static const char *register_names[] = {
159     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
160     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
161     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
162     "fpul", "fpscr",
163     "fr0", "fr1", "fr2", "fr3", "fr4", "fr5", "fr6", "fr7",
164     "fr8", "fr9", "fr10", "fr11", "fr12", "fr13", "fr14", "fr15",
165     "ssr", "spc",
166     "r0b0", "r1b0", "r2b0", "r3b0", "r4b0", "r5b0", "r6b0", "r7b0",
167     "r0b1", "r1b1", "r2b1", "r3b1", "r4b1", "r5b1", "r6b1", "r7b1",
168     "", "", "", "", "", "", "", "",
169   };
170   if (reg_nr < 0)
171     return NULL;
172   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
173     return NULL;
174   return register_names[reg_nr];
175 }
176
177 static const char *
178 sh_sh2e_register_name (struct gdbarch *gdbarch, int reg_nr)
179 {
180   static const char *register_names[] = {
181     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
182     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
183     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
184     "fpul", "fpscr",
185     "fr0", "fr1", "fr2", "fr3", "fr4", "fr5", "fr6", "fr7",
186     "fr8", "fr9", "fr10", "fr11", "fr12", "fr13", "fr14", "fr15",
187     "", "",
188     "", "", "", "", "", "", "", "",
189     "", "", "", "", "", "", "", "",
190     "", "", "", "", "", "", "", "",
191   };
192   if (reg_nr < 0)
193     return NULL;
194   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
195     return NULL;
196   return register_names[reg_nr];
197 }
198
199 static const char *
200 sh_sh2a_register_name (struct gdbarch *gdbarch, int reg_nr)
201 {
202   static const char *register_names[] = {
203     /* general registers 0-15 */
204     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
205     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
206     /* 16 - 22 */
207     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
208     /* 23, 24 */
209     "fpul", "fpscr",
210     /* floating point registers 25 - 40 */
211     "fr0", "fr1", "fr2", "fr3", "fr4", "fr5", "fr6", "fr7",
212     "fr8", "fr9", "fr10", "fr11", "fr12", "fr13", "fr14", "fr15",
213     /* 41, 42 */
214     "", "",
215     /* 43 - 62.  Banked registers.  The bank number used is determined by
216        the bank register (63).  */
217     "r0b", "r1b", "r2b", "r3b", "r4b", "r5b", "r6b", "r7b",
218     "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b",
219     "machb", "ivnb", "prb", "gbrb", "maclb",
220     /* 63: register bank number, not a real register but used to
221        communicate the register bank currently get/set.  This register
222        is hidden to the user, who manipulates it using the pseudo
223        register called "bank" (67).  See below.  */
224     "",
225     /* 64 - 66 */
226     "ibcr", "ibnr", "tbr",
227     /* 67: register bank number, the user visible pseudo register.  */
228     "bank",
229     /* double precision (pseudo) 68 - 75 */
230     "dr0", "dr2", "dr4", "dr6", "dr8", "dr10", "dr12", "dr14",
231   };
232   if (reg_nr < 0)
233     return NULL;
234   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
235     return NULL;
236   return register_names[reg_nr];
237 }
238
239 static const char *
240 sh_sh2a_nofpu_register_name (struct gdbarch *gdbarch, int reg_nr)
241 {
242   static const char *register_names[] = {
243     /* general registers 0-15 */
244     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
245     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
246     /* 16 - 22 */
247     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
248     /* 23, 24 */
249     "", "",
250     /* floating point registers 25 - 40 */
251     "", "", "", "", "", "", "", "",
252     "", "", "", "", "", "", "", "",
253     /* 41, 42 */
254     "", "",
255     /* 43 - 62.  Banked registers.  The bank number used is determined by
256        the bank register (63).  */
257     "r0b", "r1b", "r2b", "r3b", "r4b", "r5b", "r6b", "r7b",
258     "r8b", "r9b", "r10b", "r11b", "r12b", "r13b", "r14b",
259     "machb", "ivnb", "prb", "gbrb", "maclb",
260     /* 63: register bank number, not a real register but used to
261        communicate the register bank currently get/set.  This register
262        is hidden to the user, who manipulates it using the pseudo
263        register called "bank" (67).  See below.  */
264     "",
265     /* 64 - 66 */
266     "ibcr", "ibnr", "tbr",
267     /* 67: register bank number, the user visible pseudo register.  */
268     "bank",
269     /* double precision (pseudo) 68 - 75 */
270     "", "", "", "", "", "", "", "",
271   };
272   if (reg_nr < 0)
273     return NULL;
274   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
275     return NULL;
276   return register_names[reg_nr];
277 }
278
279 static const char *
280 sh_sh_dsp_register_name (struct gdbarch *gdbarch, int reg_nr)
281 {
282   static const char *register_names[] = {
283     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
284     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
285     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
286     "", "dsr",
287     "a0g", "a0", "a1g", "a1", "m0", "m1", "x0", "x1",
288     "y0", "y1", "", "", "", "", "", "mod",
289     "", "",
290     "rs", "re", "", "", "", "", "", "",
291     "", "", "", "", "", "", "", "",
292     "", "", "", "", "", "", "", "",
293   };
294   if (reg_nr < 0)
295     return NULL;
296   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
297     return NULL;
298   return register_names[reg_nr];
299 }
300
301 static const char *
302 sh_sh3_dsp_register_name (struct gdbarch *gdbarch, int reg_nr)
303 {
304   static const char *register_names[] = {
305     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
306     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
307     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
308     "", "dsr",
309     "a0g", "a0", "a1g", "a1", "m0", "m1", "x0", "x1",
310     "y0", "y1", "", "", "", "", "", "mod",
311     "ssr", "spc",
312     "rs", "re", "", "", "", "", "", "",
313     "r0b", "r1b", "r2b", "r3b", "r4b", "r5b", "r6b", "r7b",
314     "", "", "", "", "", "", "", "",
315     "", "", "", "", "", "", "", "",
316   };
317   if (reg_nr < 0)
318     return NULL;
319   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
320     return NULL;
321   return register_names[reg_nr];
322 }
323
324 static const char *
325 sh_sh4_register_name (struct gdbarch *gdbarch, int reg_nr)
326 {
327   static const char *register_names[] = {
328     /* general registers 0-15 */
329     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
330     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
331     /* 16 - 22 */
332     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
333     /* 23, 24 */
334     "fpul", "fpscr",
335     /* floating point registers 25 - 40 */
336     "fr0", "fr1", "fr2", "fr3", "fr4", "fr5", "fr6", "fr7",
337     "fr8", "fr9", "fr10", "fr11", "fr12", "fr13", "fr14", "fr15",
338     /* 41, 42 */
339     "ssr", "spc",
340     /* bank 0 43 - 50 */
341     "r0b0", "r1b0", "r2b0", "r3b0", "r4b0", "r5b0", "r6b0", "r7b0",
342     /* bank 1 51 - 58 */
343     "r0b1", "r1b1", "r2b1", "r3b1", "r4b1", "r5b1", "r6b1", "r7b1",
344     /* 59 - 66 */
345     "", "", "", "", "", "", "", "",
346     /* pseudo bank register.  */
347     "",
348     /* double precision (pseudo) 68 - 75 */
349     "dr0", "dr2", "dr4", "dr6", "dr8", "dr10", "dr12", "dr14",
350     /* vectors (pseudo) 76 - 79 */
351     "fv0", "fv4", "fv8", "fv12",
352     /* FIXME: missing XF */
353     /* FIXME: missing XD */
354   };
355   if (reg_nr < 0)
356     return NULL;
357   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
358     return NULL;
359   return register_names[reg_nr];
360 }
361
362 static const char *
363 sh_sh4_nofpu_register_name (struct gdbarch *gdbarch, int reg_nr)
364 {
365   static const char *register_names[] = {
366     /* general registers 0-15 */
367     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
368     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
369     /* 16 - 22 */
370     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
371     /* 23, 24 */
372     "", "",
373     /* floating point registers 25 - 40 -- not for nofpu target */
374     "", "", "", "", "", "", "", "",
375     "", "", "", "", "", "", "", "",
376     /* 41, 42 */
377     "ssr", "spc",
378     /* bank 0 43 - 50 */
379     "r0b0", "r1b0", "r2b0", "r3b0", "r4b0", "r5b0", "r6b0", "r7b0",
380     /* bank 1 51 - 58 */
381     "r0b1", "r1b1", "r2b1", "r3b1", "r4b1", "r5b1", "r6b1", "r7b1",
382     /* 59 - 66 */
383     "", "", "", "", "", "", "", "",
384     /* pseudo bank register.  */
385     "",
386     /* double precision (pseudo) 68 - 75 -- not for nofpu target */
387     "", "", "", "", "", "", "", "",
388     /* vectors (pseudo) 76 - 79 -- not for nofpu target */
389     "", "", "", "",
390   };
391   if (reg_nr < 0)
392     return NULL;
393   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
394     return NULL;
395   return register_names[reg_nr];
396 }
397
398 static const char *
399 sh_sh4al_dsp_register_name (struct gdbarch *gdbarch, int reg_nr)
400 {
401   static const char *register_names[] = {
402     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
403     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
404     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
405     "", "dsr",
406     "a0g", "a0", "a1g", "a1", "m0", "m1", "x0", "x1",
407     "y0", "y1", "", "", "", "", "", "mod",
408     "ssr", "spc",
409     "rs", "re", "", "", "", "", "", "",
410     "r0b", "r1b", "r2b", "r3b", "r4b", "r5b", "r6b", "r7b",
411     "", "", "", "", "", "", "", "",
412     "", "", "", "", "", "", "", "",
413   };
414   if (reg_nr < 0)
415     return NULL;
416   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
417     return NULL;
418   return register_names[reg_nr];
419 }
420
421 /* Implement the breakpoint_kind_from_pc gdbarch method.  */
422
423 static int
424 sh_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
425 {
426   return 2;
427 }
428
429 /* Implement the sw_breakpoint_from_kind gdbarch method.  */
430
431 static const gdb_byte *
432 sh_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
433 {
434   *size = kind;
435
436   /* For remote stub targets, trapa #20 is used.  */
437   if (strcmp (target_shortname, "remote") == 0)
438     {
439       static unsigned char big_remote_breakpoint[] = { 0xc3, 0x20 };
440       static unsigned char little_remote_breakpoint[] = { 0x20, 0xc3 };
441
442       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
443         return big_remote_breakpoint;
444       else
445         return little_remote_breakpoint;
446     }
447   else
448     {
449       /* 0xc3c3 is trapa #c3, and it works in big and little endian
450          modes.  */
451       static unsigned char breakpoint[] = { 0xc3, 0xc3 };
452
453       return breakpoint;
454     }
455 }
456
457 /* Prologue looks like
458    mov.l        r14,@-r15
459    sts.l        pr,@-r15
460    mov.l        <regs>,@-r15
461    sub          <room_for_loca_vars>,r15
462    mov          r15,r14
463
464    Actually it can be more complicated than this but that's it, basically.  */
465
466 #define GET_SOURCE_REG(x)       (((x) >> 4) & 0xf)
467 #define GET_TARGET_REG(x)       (((x) >> 8) & 0xf)
468
469 /* JSR @Rm         0100mmmm00001011 */
470 #define IS_JSR(x)               (((x) & 0xf0ff) == 0x400b)
471
472 /* STS.L PR,@-r15  0100111100100010
473    r15-4-->r15, PR-->(r15) */
474 #define IS_STS(x)               ((x) == 0x4f22)
475
476 /* STS.L MACL,@-r15  0100111100010010
477    r15-4-->r15, MACL-->(r15) */
478 #define IS_MACL_STS(x)          ((x) == 0x4f12)
479
480 /* MOV.L Rm,@-r15  00101111mmmm0110
481    r15-4-->r15, Rm-->(R15) */
482 #define IS_PUSH(x)              (((x) & 0xff0f) == 0x2f06)
483
484 /* MOV r15,r14     0110111011110011
485    r15-->r14  */
486 #define IS_MOV_SP_FP(x)         ((x) == 0x6ef3)
487
488 /* ADD #imm,r15    01111111iiiiiiii
489    r15+imm-->r15 */
490 #define IS_ADD_IMM_SP(x)        (((x) & 0xff00) == 0x7f00)
491
492 #define IS_MOV_R3(x)            (((x) & 0xff00) == 0x1a00)
493 #define IS_SHLL_R3(x)           ((x) == 0x4300)
494
495 /* ADD r3,r15      0011111100111100
496    r15+r3-->r15 */
497 #define IS_ADD_R3SP(x)          ((x) == 0x3f3c)
498
499 /* FMOV.S FRm,@-Rn  Rn-4-->Rn, FRm-->(Rn)     1111nnnnmmmm1011
500    FMOV DRm,@-Rn    Rn-8-->Rn, DRm-->(Rn)     1111nnnnmmm01011
501    FMOV XDm,@-Rn    Rn-8-->Rn, XDm-->(Rn)     1111nnnnmmm11011 */
502 /* CV, 2003-08-28: Only suitable with Rn == SP, therefore name changed to
503                    make this entirely clear.  */
504 /* #define IS_FMOV(x)           (((x) & 0xf00f) == 0xf00b) */
505 #define IS_FPUSH(x)             (((x) & 0xff0f) == 0xff0b)
506
507 /* MOV Rm,Rn          Rm-->Rn        0110nnnnmmmm0011  4 <= m <= 7 */
508 #define IS_MOV_ARG_TO_REG(x) \
509         (((x) & 0xf00f) == 0x6003 && \
510          ((x) & 0x00f0) >= 0x0040 && \
511          ((x) & 0x00f0) <= 0x0070)
512 /* MOV.L Rm,@Rn               0010nnnnmmmm0010  n = 14, 4 <= m <= 7 */
513 #define IS_MOV_ARG_TO_IND_R14(x) \
514         (((x) & 0xff0f) == 0x2e02 && \
515          ((x) & 0x00f0) >= 0x0040 && \
516          ((x) & 0x00f0) <= 0x0070)
517 /* MOV.L Rm,@(disp*4,Rn)      00011110mmmmdddd  n = 14, 4 <= m <= 7 */
518 #define IS_MOV_ARG_TO_IND_R14_WITH_DISP(x) \
519         (((x) & 0xff00) == 0x1e00 && \
520          ((x) & 0x00f0) >= 0x0040 && \
521          ((x) & 0x00f0) <= 0x0070)
522
523 /* MOV.W @(disp*2,PC),Rn      1001nnnndddddddd */
524 #define IS_MOVW_PCREL_TO_REG(x) (((x) & 0xf000) == 0x9000)
525 /* MOV.L @(disp*4,PC),Rn      1101nnnndddddddd */
526 #define IS_MOVL_PCREL_TO_REG(x) (((x) & 0xf000) == 0xd000)
527 /* MOVI20 #imm20,Rn           0000nnnniiii0000 */
528 #define IS_MOVI20(x)            (((x) & 0xf00f) == 0x0000)
529 /* SUB Rn,R15                 00111111nnnn1000 */
530 #define IS_SUB_REG_FROM_SP(x)   (((x) & 0xff0f) == 0x3f08)
531
532 #define FPSCR_SZ                (1 << 20)
533
534 /* The following instructions are used for epilogue testing.  */
535 #define IS_RESTORE_FP(x)        ((x) == 0x6ef6)
536 #define IS_RTS(x)               ((x) == 0x000b)
537 #define IS_LDS(x)               ((x) == 0x4f26)
538 #define IS_MACL_LDS(x)          ((x) == 0x4f16)
539 #define IS_MOV_FP_SP(x)         ((x) == 0x6fe3)
540 #define IS_ADD_REG_TO_FP(x)     (((x) & 0xff0f) == 0x3e0c)
541 #define IS_ADD_IMM_FP(x)        (((x) & 0xff00) == 0x7e00)
542
543 static CORE_ADDR
544 sh_analyze_prologue (struct gdbarch *gdbarch,
545                      CORE_ADDR pc, CORE_ADDR limit_pc,
546                      struct sh_frame_cache *cache, ULONGEST fpscr)
547 {
548   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
549   ULONGEST inst;
550   int offset;
551   int sav_offset = 0;
552   int r3_val = 0;
553   int reg, sav_reg = -1;
554
555   cache->uses_fp = 0;
556   for (; pc < limit_pc; pc += 2)
557     {
558       inst = read_memory_unsigned_integer (pc, 2, byte_order);
559       /* See where the registers will be saved to.  */
560       if (IS_PUSH (inst))
561         {
562           cache->saved_regs[GET_SOURCE_REG (inst)] = cache->sp_offset;
563           cache->sp_offset += 4;
564         }
565       else if (IS_STS (inst))
566         {
567           cache->saved_regs[PR_REGNUM] = cache->sp_offset;
568           cache->sp_offset += 4;
569         }
570       else if (IS_MACL_STS (inst))
571         {
572           cache->saved_regs[MACL_REGNUM] = cache->sp_offset;
573           cache->sp_offset += 4;
574         }
575       else if (IS_MOV_R3 (inst))
576         {
577           r3_val = ((inst & 0xff) ^ 0x80) - 0x80;
578         }
579       else if (IS_SHLL_R3 (inst))
580         {
581           r3_val <<= 1;
582         }
583       else if (IS_ADD_R3SP (inst))
584         {
585           cache->sp_offset += -r3_val;
586         }
587       else if (IS_ADD_IMM_SP (inst))
588         {
589           offset = ((inst & 0xff) ^ 0x80) - 0x80;
590           cache->sp_offset -= offset;
591         }
592       else if (IS_MOVW_PCREL_TO_REG (inst))
593         {
594           if (sav_reg < 0)
595             {
596               reg = GET_TARGET_REG (inst);
597               if (reg < 14)
598                 {
599                   sav_reg = reg;
600                   offset = (inst & 0xff) << 1;
601                   sav_offset =
602                     read_memory_integer ((pc + 4) + offset, 2, byte_order);
603                 }
604             }
605         }
606       else if (IS_MOVL_PCREL_TO_REG (inst))
607         {
608           if (sav_reg < 0)
609             {
610               reg = GET_TARGET_REG (inst);
611               if (reg < 14)
612                 {
613                   sav_reg = reg;
614                   offset = (inst & 0xff) << 2;
615                   sav_offset =
616                     read_memory_integer (((pc & 0xfffffffc) + 4) + offset,
617                                          4, byte_order);
618                 }
619             }
620         }
621       else if (IS_MOVI20 (inst)
622                && (pc + 2 < limit_pc))
623         {
624           if (sav_reg < 0)
625             {
626               reg = GET_TARGET_REG (inst);
627               if (reg < 14)
628                 {
629                   sav_reg = reg;
630                   sav_offset = GET_SOURCE_REG (inst) << 16;
631                   /* MOVI20 is a 32 bit instruction!  */
632                   pc += 2;
633                   sav_offset
634                     |= read_memory_unsigned_integer (pc, 2, byte_order);
635                   /* Now sav_offset contains an unsigned 20 bit value.
636                      It must still get sign extended.  */
637                   if (sav_offset & 0x00080000)
638                     sav_offset |= 0xfff00000;
639                 }
640             }
641         }
642       else if (IS_SUB_REG_FROM_SP (inst))
643         {
644           reg = GET_SOURCE_REG (inst);
645           if (sav_reg > 0 && reg == sav_reg)
646             {
647               sav_reg = -1;
648             }
649           cache->sp_offset += sav_offset;
650         }
651       else if (IS_FPUSH (inst))
652         {
653           if (fpscr & FPSCR_SZ)
654             {
655               cache->sp_offset += 8;
656             }
657           else
658             {
659               cache->sp_offset += 4;
660             }
661         }
662       else if (IS_MOV_SP_FP (inst))
663         {
664           pc += 2;
665           /* Don't go any further than six more instructions.  */
666           limit_pc = std::min (limit_pc, pc + (2 * 6));
667
668           cache->uses_fp = 1;
669           /* At this point, only allow argument register moves to other
670              registers or argument register moves to @(X,fp) which are
671              moving the register arguments onto the stack area allocated
672              by a former add somenumber to SP call.  Don't allow moving
673              to an fp indirect address above fp + cache->sp_offset.  */
674           for (; pc < limit_pc; pc += 2)
675             {
676               inst = read_memory_integer (pc, 2, byte_order);
677               if (IS_MOV_ARG_TO_IND_R14 (inst))
678                 {
679                   reg = GET_SOURCE_REG (inst);
680                   if (cache->sp_offset > 0)
681                     cache->saved_regs[reg] = cache->sp_offset;
682                 }
683               else if (IS_MOV_ARG_TO_IND_R14_WITH_DISP (inst))
684                 {
685                   reg = GET_SOURCE_REG (inst);
686                   offset = (inst & 0xf) * 4;
687                   if (cache->sp_offset > offset)
688                     cache->saved_regs[reg] = cache->sp_offset - offset;
689                 }
690               else if (IS_MOV_ARG_TO_REG (inst))
691                 continue;
692               else
693                 break;
694             }
695           break;
696         }
697       else if (IS_JSR (inst))
698         {
699           /* We have found a jsr that has been scheduled into the prologue.
700              If we continue the scan and return a pc someplace after this,
701              then setting a breakpoint on this function will cause it to
702              appear to be called after the function it is calling via the
703              jsr, which will be very confusing.  Most likely the next
704              instruction is going to be IS_MOV_SP_FP in the delay slot.  If
705              so, note that before returning the current pc.  */
706           if (pc + 2 < limit_pc)
707             {
708               inst = read_memory_integer (pc + 2, 2, byte_order);
709               if (IS_MOV_SP_FP (inst))
710                 cache->uses_fp = 1;
711             }
712           break;
713         }
714 #if 0           /* This used to just stop when it found an instruction
715                    that was not considered part of the prologue.  Now,
716                    we just keep going looking for likely
717                    instructions.  */
718       else
719         break;
720 #endif
721     }
722
723   return pc;
724 }
725
726 /* Skip any prologue before the guts of a function.  */
727 static CORE_ADDR
728 sh_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
729 {
730   CORE_ADDR post_prologue_pc, func_addr, func_end_addr, limit_pc;
731   struct sh_frame_cache cache;
732
733   /* See if we can determine the end of the prologue via the symbol table.
734      If so, then return either PC, or the PC after the prologue, whichever
735      is greater.  */
736   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end_addr))
737     {
738       post_prologue_pc = skip_prologue_using_sal (gdbarch, func_addr);
739       if (post_prologue_pc != 0)
740         return std::max (pc, post_prologue_pc);
741     }
742
743   /* Can't determine prologue from the symbol table, need to examine
744      instructions.  */
745
746   /* Find an upper limit on the function prologue using the debug
747      information.  If the debug information could not be used to provide
748      that bound, then use an arbitrary large number as the upper bound.  */
749   limit_pc = skip_prologue_using_sal (gdbarch, pc);
750   if (limit_pc == 0)
751     /* Don't go any further than 28 instructions.  */
752     limit_pc = pc + (2 * 28);
753
754   /* Do not allow limit_pc to be past the function end, if we know
755      where that end is...  */
756   if (func_end_addr != 0)
757     limit_pc = std::min (limit_pc, func_end_addr);
758
759   cache.sp_offset = -4;
760   post_prologue_pc = sh_analyze_prologue (gdbarch, pc, limit_pc, &cache, 0);
761   if (cache.uses_fp)
762     pc = post_prologue_pc;
763
764   return pc;
765 }
766
767 /* The ABI says:
768
769    Aggregate types not bigger than 8 bytes that have the same size and
770    alignment as one of the integer scalar types are returned in the
771    same registers as the integer type they match.
772
773    For example, a 2-byte aligned structure with size 2 bytes has the
774    same size and alignment as a short int, and will be returned in R0.
775    A 4-byte aligned structure with size 8 bytes has the same size and
776    alignment as a long long int, and will be returned in R0 and R1.
777
778    When an aggregate type is returned in R0 and R1, R0 contains the
779    first four bytes of the aggregate, and R1 contains the
780    remainder.  If the size of the aggregate type is not a multiple of 4
781    bytes, the aggregate is tail-padded up to a multiple of 4
782    bytes.  The value of the padding is undefined.  For little-endian
783    targets the padding will appear at the most significant end of the
784    last element, for big-endian targets the padding appears at the
785    least significant end of the last element.
786
787    All other aggregate types are returned by address.  The caller
788    function passes the address of an area large enough to hold the
789    aggregate value in R2.  The called function stores the result in
790    this location.
791
792    To reiterate, structs smaller than 8 bytes could also be returned
793    in memory, if they don't pass the "same size and alignment as an
794    integer type" rule.
795
796    For example, in
797
798    struct s { char c[3]; } wibble;
799    struct s foo(void) {  return wibble; }
800
801    the return value from foo() will be in memory, not
802    in R0, because there is no 3-byte integer type.
803
804    Similarly, in 
805
806    struct s { char c[2]; } wibble;
807    struct s foo(void) {  return wibble; }
808
809    because a struct containing two chars has alignment 1, that matches
810    type char, but size 2, that matches type short.  There's no integer
811    type that has alignment 1 and size 2, so the struct is returned in
812    memory.  */
813
814 static int
815 sh_use_struct_convention (int renesas_abi, struct type *type)
816 {
817   int len = TYPE_LENGTH (type);
818   int nelem = TYPE_NFIELDS (type);
819
820   /* The Renesas ABI returns aggregate types always on stack.  */
821   if (renesas_abi && (TYPE_CODE (type) == TYPE_CODE_STRUCT
822                       || TYPE_CODE (type) == TYPE_CODE_UNION))
823     return 1;
824
825   /* Non-power of 2 length types and types bigger than 8 bytes (which don't
826      fit in two registers anyway) use struct convention.  */
827   if (len != 1 && len != 2 && len != 4 && len != 8)
828     return 1;
829
830   /* Scalar types and aggregate types with exactly one field are aligned
831      by definition.  They are returned in registers.  */
832   if (nelem <= 1)
833     return 0;
834
835   /* If the first field in the aggregate has the same length as the entire
836      aggregate type, the type is returned in registers.  */
837   if (TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0)) == len)
838     return 0;
839
840   /* If the size of the aggregate is 8 bytes and the first field is
841      of size 4 bytes its alignment is equal to long long's alignment,
842      so it's returned in registers.  */
843   if (len == 8 && TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0)) == 4)
844     return 0;
845
846   /* Otherwise use struct convention.  */
847   return 1;
848 }
849
850 static int
851 sh_use_struct_convention_nofpu (int renesas_abi, struct type *type)
852 {
853   /* The Renesas ABI returns long longs/doubles etc. always on stack.  */
854   if (renesas_abi && TYPE_NFIELDS (type) == 0 && TYPE_LENGTH (type) >= 8)
855     return 1;
856   return sh_use_struct_convention (renesas_abi, type);
857 }
858
859 static CORE_ADDR
860 sh_frame_align (struct gdbarch *ignore, CORE_ADDR sp)
861 {
862   return sp & ~3;
863 }
864
865 /* Function: push_dummy_call (formerly push_arguments)
866    Setup the function arguments for calling a function in the inferior.
867
868    On the Renesas SH architecture, there are four registers (R4 to R7)
869    which are dedicated for passing function arguments.  Up to the first
870    four arguments (depending on size) may go into these registers.
871    The rest go on the stack.
872
873    MVS: Except on SH variants that have floating point registers.
874    In that case, float and double arguments are passed in the same
875    manner, but using FP registers instead of GP registers.
876
877    Arguments that are smaller than 4 bytes will still take up a whole
878    register or a whole 32-bit word on the stack, and will be 
879    right-justified in the register or the stack word.  This includes
880    chars, shorts, and small aggregate types.
881
882    Arguments that are larger than 4 bytes may be split between two or 
883    more registers.  If there are not enough registers free, an argument
884    may be passed partly in a register (or registers), and partly on the
885    stack.  This includes doubles, long longs, and larger aggregates.
886    As far as I know, there is no upper limit to the size of aggregates 
887    that will be passed in this way; in other words, the convention of 
888    passing a pointer to a large aggregate instead of a copy is not used.
889
890    MVS: The above appears to be true for the SH variants that do not
891    have an FPU, however those that have an FPU appear to copy the
892    aggregate argument onto the stack (and not place it in registers)
893    if it is larger than 16 bytes (four GP registers).
894
895    An exceptional case exists for struct arguments (and possibly other
896    aggregates such as arrays) if the size is larger than 4 bytes but 
897    not a multiple of 4 bytes.  In this case the argument is never split 
898    between the registers and the stack, but instead is copied in its
899    entirety onto the stack, AND also copied into as many registers as 
900    there is room for.  In other words, space in registers permitting, 
901    two copies of the same argument are passed in.  As far as I can tell,
902    only the one on the stack is used, although that may be a function 
903    of the level of compiler optimization.  I suspect this is a compiler
904    bug.  Arguments of these odd sizes are left-justified within the 
905    word (as opposed to arguments smaller than 4 bytes, which are 
906    right-justified).
907
908    If the function is to return an aggregate type such as a struct, it 
909    is either returned in the normal return value register R0 (if its 
910    size is no greater than one byte), or else the caller must allocate
911    space into which the callee will copy the return value (if the size
912    is greater than one byte).  In this case, a pointer to the return 
913    value location is passed into the callee in register R2, which does 
914    not displace any of the other arguments passed in via registers R4
915    to R7.  */
916
917 /* Helper function to justify value in register according to endianess.  */
918 static const gdb_byte *
919 sh_justify_value_in_reg (struct gdbarch *gdbarch, struct value *val, int len)
920 {
921   static gdb_byte valbuf[4];
922
923   memset (valbuf, 0, sizeof (valbuf));
924   if (len < 4)
925     {
926       /* value gets right-justified in the register or stack word.  */
927       if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
928         memcpy (valbuf + (4 - len), value_contents (val), len);
929       else
930         memcpy (valbuf, value_contents (val), len);
931       return valbuf;
932     }
933   return value_contents (val);
934 }
935
936 /* Helper function to eval number of bytes to allocate on stack.  */
937 static CORE_ADDR
938 sh_stack_allocsize (int nargs, struct value **args)
939 {
940   int stack_alloc = 0;
941   while (nargs-- > 0)
942     stack_alloc += ((TYPE_LENGTH (value_type (args[nargs])) + 3) & ~3);
943   return stack_alloc;
944 }
945
946 /* Helper functions for getting the float arguments right.  Registers usage
947    depends on the ABI and the endianess.  The comments should enlighten how
948    it's intended to work.  */
949
950 /* This array stores which of the float arg registers are already in use.  */
951 static int flt_argreg_array[FLOAT_ARGLAST_REGNUM - FLOAT_ARG0_REGNUM + 1];
952
953 /* This function just resets the above array to "no reg used so far".  */
954 static void
955 sh_init_flt_argreg (void)
956 {
957   memset (flt_argreg_array, 0, sizeof flt_argreg_array);
958 }
959
960 /* This function returns the next register to use for float arg passing.
961    It returns either a valid value between FLOAT_ARG0_REGNUM and
962    FLOAT_ARGLAST_REGNUM if a register is available, otherwise it returns 
963    FLOAT_ARGLAST_REGNUM + 1 to indicate that no register is available.
964
965    Note that register number 0 in flt_argreg_array corresponds with the
966    real float register fr4.  In contrast to FLOAT_ARG0_REGNUM (value is
967    29) the parity of the register number is preserved, which is important
968    for the double register passing test (see the "argreg & 1" test below).  */
969 static int
970 sh_next_flt_argreg (struct gdbarch *gdbarch, int len, struct type *func_type)
971 {
972   int argreg;
973
974   /* First search for the next free register.  */
975   for (argreg = 0; argreg <= FLOAT_ARGLAST_REGNUM - FLOAT_ARG0_REGNUM;
976        ++argreg)
977     if (!flt_argreg_array[argreg])
978       break;
979
980   /* No register left?  */
981   if (argreg > FLOAT_ARGLAST_REGNUM - FLOAT_ARG0_REGNUM)
982     return FLOAT_ARGLAST_REGNUM + 1;
983
984   if (len == 8)
985     {
986       /* Doubles are always starting in a even register number.  */
987       if (argreg & 1)
988         {
989           /* In gcc ABI, the skipped register is lost for further argument
990              passing now.  Not so in Renesas ABI.  */
991           if (!sh_is_renesas_calling_convention (func_type))
992             flt_argreg_array[argreg] = 1;
993
994           ++argreg;
995
996           /* No register left?  */
997           if (argreg > FLOAT_ARGLAST_REGNUM - FLOAT_ARG0_REGNUM)
998             return FLOAT_ARGLAST_REGNUM + 1;
999         }
1000       /* Also mark the next register as used.  */
1001       flt_argreg_array[argreg + 1] = 1;
1002     }
1003   else if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE
1004            && !sh_is_renesas_calling_convention (func_type))
1005     {
1006       /* In little endian, gcc passes floats like this: f5, f4, f7, f6, ...  */
1007       if (!flt_argreg_array[argreg + 1])
1008         ++argreg;
1009     }
1010   flt_argreg_array[argreg] = 1;
1011   return FLOAT_ARG0_REGNUM + argreg;
1012 }
1013
1014 /* Helper function which figures out, if a type is treated like a float type.
1015
1016    The FPU ABIs have a special way how to treat types as float types.
1017    Structures with exactly one member, which is of type float or double, are
1018    treated exactly as the base types float or double:
1019
1020      struct sf {
1021        float f;
1022      };
1023
1024      struct sd {
1025        double d;
1026      };
1027
1028    are handled the same way as just
1029
1030      float f;
1031
1032      double d;
1033
1034    As a result, arguments of these struct types are pushed into floating point
1035    registers exactly as floats or doubles, using the same decision algorithm.
1036
1037    The same is valid if these types are used as function return types.  The
1038    above structs are returned in fr0 resp. fr0,fr1 instead of in r0, r0,r1
1039    or even using struct convention as it is for other structs.  */
1040
1041 static int
1042 sh_treat_as_flt_p (struct type *type)
1043 {
1044   /* Ordinary float types are obviously treated as float.  */
1045   if (TYPE_CODE (type) == TYPE_CODE_FLT)
1046     return 1;
1047   /* Otherwise non-struct types are not treated as float.  */
1048   if (TYPE_CODE (type) != TYPE_CODE_STRUCT)
1049     return 0;
1050   /* Otherwise structs with more than one memeber are not treated as float.  */
1051   if (TYPE_NFIELDS (type) != 1)
1052     return 0;
1053   /* Otherwise if the type of that member is float, the whole type is
1054      treated as float.  */
1055   if (TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_FLT)
1056     return 1;
1057   /* Otherwise it's not treated as float.  */
1058   return 0;
1059 }
1060
1061 static CORE_ADDR
1062 sh_push_dummy_call_fpu (struct gdbarch *gdbarch,
1063                         struct value *function,
1064                         struct regcache *regcache,
1065                         CORE_ADDR bp_addr, int nargs,
1066                         struct value **args,
1067                         CORE_ADDR sp, int struct_return,
1068                         CORE_ADDR struct_addr)
1069 {
1070   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1071   int stack_offset = 0;
1072   int argreg = ARG0_REGNUM;
1073   int flt_argreg = 0;
1074   int argnum;
1075   struct type *func_type = value_type (function);
1076   struct type *type;
1077   CORE_ADDR regval;
1078   const gdb_byte *val;
1079   int len, reg_size = 0;
1080   int pass_on_stack = 0;
1081   int treat_as_flt;
1082   int last_reg_arg = INT_MAX;
1083
1084   /* The Renesas ABI expects all varargs arguments, plus the last
1085      non-vararg argument to be on the stack, no matter how many
1086      registers have been used so far.  */
1087   if (sh_is_renesas_calling_convention (func_type)
1088       && TYPE_VARARGS (func_type))
1089     last_reg_arg = TYPE_NFIELDS (func_type) - 2;
1090
1091   /* First force sp to a 4-byte alignment.  */
1092   sp = sh_frame_align (gdbarch, sp);
1093
1094   /* Make room on stack for args.  */
1095   sp -= sh_stack_allocsize (nargs, args);
1096
1097   /* Initialize float argument mechanism.  */
1098   sh_init_flt_argreg ();
1099
1100   /* Now load as many as possible of the first arguments into
1101      registers, and push the rest onto the stack.  There are 16 bytes
1102      in four registers available.  Loop thru args from first to last.  */
1103   for (argnum = 0; argnum < nargs; argnum++)
1104     {
1105       type = value_type (args[argnum]);
1106       len = TYPE_LENGTH (type);
1107       val = sh_justify_value_in_reg (gdbarch, args[argnum], len);
1108
1109       /* Some decisions have to be made how various types are handled.
1110          This also differs in different ABIs.  */
1111       pass_on_stack = 0;
1112
1113       /* Find out the next register to use for a floating point value.  */
1114       treat_as_flt = sh_treat_as_flt_p (type);
1115       if (treat_as_flt)
1116         flt_argreg = sh_next_flt_argreg (gdbarch, len, func_type);
1117       /* In Renesas ABI, long longs and aggregate types are always passed
1118          on stack.  */
1119       else if (sh_is_renesas_calling_convention (func_type)
1120                && ((TYPE_CODE (type) == TYPE_CODE_INT && len == 8)
1121                    || TYPE_CODE (type) == TYPE_CODE_STRUCT
1122                    || TYPE_CODE (type) == TYPE_CODE_UNION))
1123         pass_on_stack = 1;
1124       /* In contrast to non-FPU CPUs, arguments are never split between
1125          registers and stack.  If an argument doesn't fit in the remaining
1126          registers it's always pushed entirely on the stack.  */
1127       else if (len > ((ARGLAST_REGNUM - argreg + 1) * 4))
1128         pass_on_stack = 1;
1129
1130       while (len > 0)
1131         {
1132           if ((treat_as_flt && flt_argreg > FLOAT_ARGLAST_REGNUM)
1133               || (!treat_as_flt && (argreg > ARGLAST_REGNUM
1134                                     || pass_on_stack))
1135               || argnum > last_reg_arg)
1136             {
1137               /* The data goes entirely on the stack, 4-byte aligned.  */
1138               reg_size = (len + 3) & ~3;
1139               write_memory (sp + stack_offset, val, reg_size);
1140               stack_offset += reg_size;
1141             }
1142           else if (treat_as_flt && flt_argreg <= FLOAT_ARGLAST_REGNUM)
1143             {
1144               /* Argument goes in a float argument register.  */
1145               reg_size = register_size (gdbarch, flt_argreg);
1146               regval = extract_unsigned_integer (val, reg_size, byte_order);
1147               /* In little endian mode, float types taking two registers
1148                  (doubles on sh4, long doubles on sh2e, sh3e and sh4) must
1149                  be stored swapped in the argument registers.  The below
1150                  code first writes the first 32 bits in the next but one
1151                  register, increments the val and len values accordingly
1152                  and then proceeds as normal by writing the second 32 bits
1153                  into the next register.  */
1154               if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE
1155                   && TYPE_LENGTH (type) == 2 * reg_size)
1156                 {
1157                   regcache_cooked_write_unsigned (regcache, flt_argreg + 1,
1158                                                   regval);
1159                   val += reg_size;
1160                   len -= reg_size;
1161                   regval = extract_unsigned_integer (val, reg_size,
1162                                                      byte_order);
1163                 }
1164               regcache_cooked_write_unsigned (regcache, flt_argreg++, regval);
1165             }
1166           else if (!treat_as_flt && argreg <= ARGLAST_REGNUM)
1167             {
1168               /* there's room in a register */
1169               reg_size = register_size (gdbarch, argreg);
1170               regval = extract_unsigned_integer (val, reg_size, byte_order);
1171               regcache_cooked_write_unsigned (regcache, argreg++, regval);
1172             }
1173           /* Store the value one register at a time or in one step on
1174              stack.  */
1175           len -= reg_size;
1176           val += reg_size;
1177         }
1178     }
1179
1180   if (struct_return)
1181     {
1182       if (sh_is_renesas_calling_convention (func_type))
1183         /* If the function uses the Renesas ABI, subtract another 4 bytes from
1184            the stack and store the struct return address there.  */
1185         write_memory_unsigned_integer (sp -= 4, 4, byte_order, struct_addr);
1186       else
1187         /* Using the gcc ABI, the "struct return pointer" pseudo-argument has
1188            its own dedicated register.  */
1189         regcache_cooked_write_unsigned (regcache,
1190                                         STRUCT_RETURN_REGNUM, struct_addr);
1191     }
1192
1193   /* Store return address.  */
1194   regcache_cooked_write_unsigned (regcache, PR_REGNUM, bp_addr);
1195
1196   /* Update stack pointer.  */
1197   regcache_cooked_write_unsigned (regcache,
1198                                   gdbarch_sp_regnum (gdbarch), sp);
1199
1200   return sp;
1201 }
1202
1203 static CORE_ADDR
1204 sh_push_dummy_call_nofpu (struct gdbarch *gdbarch,
1205                           struct value *function,
1206                           struct regcache *regcache,
1207                           CORE_ADDR bp_addr,
1208                           int nargs, struct value **args,
1209                           CORE_ADDR sp, int struct_return,
1210                           CORE_ADDR struct_addr)
1211 {
1212   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1213   int stack_offset = 0;
1214   int argreg = ARG0_REGNUM;
1215   int argnum;
1216   struct type *func_type = value_type (function);
1217   struct type *type;
1218   CORE_ADDR regval;
1219   const gdb_byte *val;
1220   int len, reg_size = 0;
1221   int pass_on_stack = 0;
1222   int last_reg_arg = INT_MAX;
1223
1224   /* The Renesas ABI expects all varargs arguments, plus the last
1225      non-vararg argument to be on the stack, no matter how many
1226      registers have been used so far.  */
1227   if (sh_is_renesas_calling_convention (func_type)
1228       && TYPE_VARARGS (func_type))
1229     last_reg_arg = TYPE_NFIELDS (func_type) - 2;
1230
1231   /* First force sp to a 4-byte alignment.  */
1232   sp = sh_frame_align (gdbarch, sp);
1233
1234   /* Make room on stack for args.  */
1235   sp -= sh_stack_allocsize (nargs, args);
1236
1237   /* Now load as many as possible of the first arguments into
1238      registers, and push the rest onto the stack.  There are 16 bytes
1239      in four registers available.  Loop thru args from first to last.  */
1240   for (argnum = 0; argnum < nargs; argnum++)
1241     {
1242       type = value_type (args[argnum]);
1243       len = TYPE_LENGTH (type);
1244       val = sh_justify_value_in_reg (gdbarch, args[argnum], len);
1245
1246       /* Some decisions have to be made how various types are handled.
1247          This also differs in different ABIs.  */
1248       pass_on_stack = 0;
1249       /* Renesas ABI pushes doubles and long longs entirely on stack.
1250          Same goes for aggregate types.  */
1251       if (sh_is_renesas_calling_convention (func_type)
1252           && ((TYPE_CODE (type) == TYPE_CODE_INT && len >= 8)
1253               || (TYPE_CODE (type) == TYPE_CODE_FLT && len >= 8)
1254               || TYPE_CODE (type) == TYPE_CODE_STRUCT
1255               || TYPE_CODE (type) == TYPE_CODE_UNION))
1256         pass_on_stack = 1;
1257       while (len > 0)
1258         {
1259           if (argreg > ARGLAST_REGNUM || pass_on_stack
1260               || argnum > last_reg_arg)
1261             {
1262               /* The remainder of the data goes entirely on the stack,
1263                  4-byte aligned.  */
1264               reg_size = (len + 3) & ~3;
1265               write_memory (sp + stack_offset, val, reg_size);
1266               stack_offset += reg_size;
1267             }
1268           else if (argreg <= ARGLAST_REGNUM)
1269             {
1270               /* There's room in a register.  */
1271               reg_size = register_size (gdbarch, argreg);
1272               regval = extract_unsigned_integer (val, reg_size, byte_order);
1273               regcache_cooked_write_unsigned (regcache, argreg++, regval);
1274             }
1275           /* Store the value reg_size bytes at a time.  This means that things
1276              larger than reg_size bytes may go partly in registers and partly
1277              on the stack.  */
1278           len -= reg_size;
1279           val += reg_size;
1280         }
1281     }
1282
1283   if (struct_return)
1284     {
1285       if (sh_is_renesas_calling_convention (func_type))
1286         /* If the function uses the Renesas ABI, subtract another 4 bytes from
1287            the stack and store the struct return address there.  */
1288         write_memory_unsigned_integer (sp -= 4, 4, byte_order, struct_addr);
1289       else
1290         /* Using the gcc ABI, the "struct return pointer" pseudo-argument has
1291            its own dedicated register.  */
1292         regcache_cooked_write_unsigned (regcache,
1293                                         STRUCT_RETURN_REGNUM, struct_addr);
1294     }
1295
1296   /* Store return address.  */
1297   regcache_cooked_write_unsigned (regcache, PR_REGNUM, bp_addr);
1298
1299   /* Update stack pointer.  */
1300   regcache_cooked_write_unsigned (regcache,
1301                                   gdbarch_sp_regnum (gdbarch), sp);
1302
1303   return sp;
1304 }
1305
1306 /* Find a function's return value in the appropriate registers (in
1307    regbuf), and copy it into valbuf.  Extract from an array REGBUF
1308    containing the (raw) register state a function return value of type
1309    TYPE, and copy that, in virtual format, into VALBUF.  */
1310 static void
1311 sh_extract_return_value_nofpu (struct type *type, struct regcache *regcache,
1312                                gdb_byte *valbuf)
1313 {
1314   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1315   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1316   int len = TYPE_LENGTH (type);
1317
1318   if (len <= 4)
1319     {
1320       ULONGEST c;
1321
1322       regcache_cooked_read_unsigned (regcache, R0_REGNUM, &c);
1323       store_unsigned_integer (valbuf, len, byte_order, c);
1324     }
1325   else if (len == 8)
1326     {
1327       int i, regnum = R0_REGNUM;
1328       for (i = 0; i < len; i += 4)
1329         regcache_raw_read (regcache, regnum++, valbuf + i);
1330     }
1331   else
1332     error (_("bad size for return value"));
1333 }
1334
1335 static void
1336 sh_extract_return_value_fpu (struct type *type, struct regcache *regcache,
1337                              gdb_byte *valbuf)
1338 {
1339   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1340   if (sh_treat_as_flt_p (type))
1341     {
1342       int len = TYPE_LENGTH (type);
1343       int i, regnum = gdbarch_fp0_regnum (gdbarch);
1344       for (i = 0; i < len; i += 4)
1345         if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
1346           regcache_raw_read (regcache, regnum++,
1347                              valbuf + len - 4 - i);
1348         else
1349           regcache_raw_read (regcache, regnum++, valbuf + i);
1350     }
1351   else
1352     sh_extract_return_value_nofpu (type, regcache, valbuf);
1353 }
1354
1355 /* Write into appropriate registers a function return value
1356    of type TYPE, given in virtual format.
1357    If the architecture is sh4 or sh3e, store a function's return value
1358    in the R0 general register or in the FP0 floating point register,
1359    depending on the type of the return value.  In all the other cases
1360    the result is stored in r0, left-justified.  */
1361 static void
1362 sh_store_return_value_nofpu (struct type *type, struct regcache *regcache,
1363                              const gdb_byte *valbuf)
1364 {
1365   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1366   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1367   ULONGEST val;
1368   int len = TYPE_LENGTH (type);
1369
1370   if (len <= 4)
1371     {
1372       val = extract_unsigned_integer (valbuf, len, byte_order);
1373       regcache_cooked_write_unsigned (regcache, R0_REGNUM, val);
1374     }
1375   else
1376     {
1377       int i, regnum = R0_REGNUM;
1378       for (i = 0; i < len; i += 4)
1379         regcache_raw_write (regcache, regnum++, valbuf + i);
1380     }
1381 }
1382
1383 static void
1384 sh_store_return_value_fpu (struct type *type, struct regcache *regcache,
1385                            const gdb_byte *valbuf)
1386 {
1387   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1388   if (sh_treat_as_flt_p (type))
1389     {
1390       int len = TYPE_LENGTH (type);
1391       int i, regnum = gdbarch_fp0_regnum (gdbarch);
1392       for (i = 0; i < len; i += 4)
1393         if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_LITTLE)
1394           regcache_raw_write (regcache, regnum++,
1395                               valbuf + len - 4 - i);
1396         else
1397           regcache_raw_write (regcache, regnum++, valbuf + i);
1398     }
1399   else
1400     sh_store_return_value_nofpu (type, regcache, valbuf);
1401 }
1402
1403 static enum return_value_convention
1404 sh_return_value_nofpu (struct gdbarch *gdbarch, struct value *function,
1405                        struct type *type, struct regcache *regcache,
1406                        gdb_byte *readbuf, const gdb_byte *writebuf)
1407 {
1408   struct type *func_type = function ? value_type (function) : NULL;
1409
1410   if (sh_use_struct_convention_nofpu (
1411         sh_is_renesas_calling_convention (func_type), type))
1412     return RETURN_VALUE_STRUCT_CONVENTION;
1413   if (writebuf)
1414     sh_store_return_value_nofpu (type, regcache, writebuf);
1415   else if (readbuf)
1416     sh_extract_return_value_nofpu (type, regcache, readbuf);
1417   return RETURN_VALUE_REGISTER_CONVENTION;
1418 }
1419
1420 static enum return_value_convention
1421 sh_return_value_fpu (struct gdbarch *gdbarch, struct value *function,
1422                      struct type *type, struct regcache *regcache,
1423                      gdb_byte *readbuf, const gdb_byte *writebuf)
1424 {
1425   struct type *func_type = function ? value_type (function) : NULL;
1426
1427   if (sh_use_struct_convention (
1428         sh_is_renesas_calling_convention (func_type), type))
1429     return RETURN_VALUE_STRUCT_CONVENTION;
1430   if (writebuf)
1431     sh_store_return_value_fpu (type, regcache, writebuf);
1432   else if (readbuf)
1433     sh_extract_return_value_fpu (type, regcache, readbuf);
1434   return RETURN_VALUE_REGISTER_CONVENTION;
1435 }
1436
1437 static struct type *
1438 sh_sh2a_register_type (struct gdbarch *gdbarch, int reg_nr)
1439 {
1440   if ((reg_nr >= gdbarch_fp0_regnum (gdbarch)
1441        && (reg_nr <= FP_LAST_REGNUM)) || (reg_nr == FPUL_REGNUM))
1442     return builtin_type (gdbarch)->builtin_float;
1443   else if (reg_nr >= DR0_REGNUM && reg_nr <= DR_LAST_REGNUM)
1444     return builtin_type (gdbarch)->builtin_double;
1445   else
1446     return builtin_type (gdbarch)->builtin_int;
1447 }
1448
1449 /* Return the GDB type object for the "standard" data type
1450    of data in register N.  */
1451 static struct type *
1452 sh_sh3e_register_type (struct gdbarch *gdbarch, int reg_nr)
1453 {
1454   if ((reg_nr >= gdbarch_fp0_regnum (gdbarch)
1455        && (reg_nr <= FP_LAST_REGNUM)) || (reg_nr == FPUL_REGNUM))
1456     return builtin_type (gdbarch)->builtin_float;
1457   else
1458     return builtin_type (gdbarch)->builtin_int;
1459 }
1460
1461 static struct type *
1462 sh_sh4_build_float_register_type (struct gdbarch *gdbarch, int high)
1463 {
1464   return lookup_array_range_type (builtin_type (gdbarch)->builtin_float,
1465                                   0, high);
1466 }
1467
1468 static struct type *
1469 sh_sh4_register_type (struct gdbarch *gdbarch, int reg_nr)
1470 {
1471   if ((reg_nr >= gdbarch_fp0_regnum (gdbarch)
1472        && (reg_nr <= FP_LAST_REGNUM)) || (reg_nr == FPUL_REGNUM))
1473     return builtin_type (gdbarch)->builtin_float;
1474   else if (reg_nr >= DR0_REGNUM && reg_nr <= DR_LAST_REGNUM)
1475     return builtin_type (gdbarch)->builtin_double;
1476   else if (reg_nr >= FV0_REGNUM && reg_nr <= FV_LAST_REGNUM)
1477     return sh_sh4_build_float_register_type (gdbarch, 3);
1478   else
1479     return builtin_type (gdbarch)->builtin_int;
1480 }
1481
1482 static struct type *
1483 sh_default_register_type (struct gdbarch *gdbarch, int reg_nr)
1484 {
1485   return builtin_type (gdbarch)->builtin_int;
1486 }
1487
1488 /* Is a register in a reggroup?
1489    The default code in reggroup.c doesn't identify system registers, some
1490    float registers or any of the vector registers.
1491    TODO: sh2a and dsp registers.  */
1492 static int
1493 sh_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
1494                         struct reggroup *reggroup)
1495 {
1496   if (gdbarch_register_name (gdbarch, regnum) == NULL
1497       || *gdbarch_register_name (gdbarch, regnum) == '\0')
1498     return 0;
1499
1500   if (reggroup == float_reggroup
1501       && (regnum == FPUL_REGNUM
1502           || regnum == FPSCR_REGNUM))
1503     return 1;
1504
1505   if (regnum >= FV0_REGNUM && regnum <= FV_LAST_REGNUM)
1506     {
1507       if (reggroup == vector_reggroup || reggroup == float_reggroup)
1508         return 1;
1509       if (reggroup == general_reggroup)
1510         return 0;
1511     }
1512
1513   if (regnum == VBR_REGNUM
1514       || regnum == SR_REGNUM
1515       || regnum == FPSCR_REGNUM
1516       || regnum == SSR_REGNUM
1517       || regnum == SPC_REGNUM)
1518     {
1519       if (reggroup == system_reggroup)
1520         return 1;
1521       if (reggroup == general_reggroup)
1522         return 0;
1523     }
1524
1525   /* The default code can cope with any other registers.  */
1526   return default_register_reggroup_p (gdbarch, regnum, reggroup);
1527 }
1528
1529 /* On the sh4, the DRi pseudo registers are problematic if the target
1530    is little endian.  When the user writes one of those registers, for
1531    instance with 'set var $dr0=1', we want the double to be stored
1532    like this: 
1533    fr0 = 0x00 0x00 0xf0 0x3f 
1534    fr1 = 0x00 0x00 0x00 0x00 
1535
1536    This corresponds to little endian byte order & big endian word
1537    order.  However if we let gdb write the register w/o conversion, it
1538    will write fr0 and fr1 this way:
1539    fr0 = 0x00 0x00 0x00 0x00
1540    fr1 = 0x00 0x00 0xf0 0x3f
1541    because it will consider fr0 and fr1 as a single LE stretch of memory.
1542    
1543    To achieve what we want we must force gdb to store things in
1544    floatformat_ieee_double_littlebyte_bigword (which is defined in
1545    include/floatformat.h and libiberty/floatformat.c.
1546
1547    In case the target is big endian, there is no problem, the
1548    raw bytes will look like:
1549    fr0 = 0x3f 0xf0 0x00 0x00
1550    fr1 = 0x00 0x00 0x00 0x00
1551
1552    The other pseudo registers (the FVs) also don't pose a problem
1553    because they are stored as 4 individual FP elements.  */
1554
1555 static void
1556 sh_register_convert_to_virtual (struct gdbarch *gdbarch, int regnum,
1557                                 struct type *type, gdb_byte *from, gdb_byte *to)
1558 {
1559   if (gdbarch_byte_order (gdbarch) != BFD_ENDIAN_LITTLE)
1560     {
1561       /* It is a no-op.  */
1562       memcpy (to, from, register_size (gdbarch, regnum));
1563       return;
1564     }
1565
1566   if (regnum >= DR0_REGNUM && regnum <= DR_LAST_REGNUM)
1567     {
1568       DOUBLEST val;
1569       floatformat_to_doublest (&floatformat_ieee_double_littlebyte_bigword,
1570                                from, &val);
1571       store_typed_floating (to, type, val);
1572     }
1573   else
1574     error
1575       ("sh_register_convert_to_virtual called with non DR register number");
1576 }
1577
1578 static void
1579 sh_register_convert_to_raw (struct gdbarch *gdbarch, struct type *type,
1580                             int regnum, const gdb_byte *from, gdb_byte *to)
1581 {
1582   if (gdbarch_byte_order (gdbarch) != BFD_ENDIAN_LITTLE)
1583     {
1584       /* It is a no-op.  */
1585       memcpy (to, from, register_size (gdbarch, regnum));
1586       return;
1587     }
1588
1589   if (regnum >= DR0_REGNUM && regnum <= DR_LAST_REGNUM)
1590     {
1591       DOUBLEST val = extract_typed_floating (from, type);
1592       floatformat_from_doublest (&floatformat_ieee_double_littlebyte_bigword,
1593                                  &val, to);
1594     }
1595   else
1596     error (_("sh_register_convert_to_raw called with non DR register number"));
1597 }
1598
1599 /* For vectors of 4 floating point registers.  */
1600 static int
1601 fv_reg_base_num (struct gdbarch *gdbarch, int fv_regnum)
1602 {
1603   int fp_regnum;
1604
1605   fp_regnum = gdbarch_fp0_regnum (gdbarch)
1606               + (fv_regnum - FV0_REGNUM) * 4;
1607   return fp_regnum;
1608 }
1609
1610 /* For double precision floating point registers, i.e 2 fp regs.  */
1611 static int
1612 dr_reg_base_num (struct gdbarch *gdbarch, int dr_regnum)
1613 {
1614   int fp_regnum;
1615
1616   fp_regnum = gdbarch_fp0_regnum (gdbarch)
1617               + (dr_regnum - DR0_REGNUM) * 2;
1618   return fp_regnum;
1619 }
1620
1621 /* Concatenate PORTIONS contiguous raw registers starting at
1622    BASE_REGNUM into BUFFER.  */
1623
1624 static enum register_status
1625 pseudo_register_read_portions (struct gdbarch *gdbarch,
1626                                struct regcache *regcache,
1627                                int portions,
1628                                int base_regnum, gdb_byte *buffer)
1629 {
1630   int portion;
1631
1632   for (portion = 0; portion < portions; portion++)
1633     {
1634       enum register_status status;
1635       gdb_byte *b;
1636
1637       b = buffer + register_size (gdbarch, base_regnum) * portion;
1638       status = regcache_raw_read (regcache, base_regnum + portion, b);
1639       if (status != REG_VALID)
1640         return status;
1641     }
1642
1643   return REG_VALID;
1644 }
1645
1646 static enum register_status
1647 sh_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
1648                          int reg_nr, gdb_byte *buffer)
1649 {
1650   int base_regnum;
1651   gdb_byte temp_buffer[MAX_REGISTER_SIZE];
1652   enum register_status status;
1653
1654   if (reg_nr == PSEUDO_BANK_REGNUM)
1655     return regcache_raw_read (regcache, BANK_REGNUM, buffer);
1656   else if (reg_nr >= DR0_REGNUM && reg_nr <= DR_LAST_REGNUM)
1657     {
1658       base_regnum = dr_reg_base_num (gdbarch, reg_nr);
1659
1660       /* Build the value in the provided buffer.  */
1661       /* Read the real regs for which this one is an alias.  */
1662       status = pseudo_register_read_portions (gdbarch, regcache,
1663                                               2, base_regnum, temp_buffer);
1664       if (status == REG_VALID)
1665         {
1666           /* We must pay attention to the endiannes. */
1667           sh_register_convert_to_virtual (gdbarch, reg_nr,
1668                                           register_type (gdbarch, reg_nr),
1669                                           temp_buffer, buffer);
1670         }
1671       return status;
1672     }
1673   else if (reg_nr >= FV0_REGNUM && reg_nr <= FV_LAST_REGNUM)
1674     {
1675       base_regnum = fv_reg_base_num (gdbarch, reg_nr);
1676
1677       /* Read the real regs for which this one is an alias.  */
1678       return pseudo_register_read_portions (gdbarch, regcache,
1679                                             4, base_regnum, buffer);
1680     }
1681   else
1682     gdb_assert_not_reached ("invalid pseudo register number");
1683 }
1684
1685 static void
1686 sh_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
1687                           int reg_nr, const gdb_byte *buffer)
1688 {
1689   int base_regnum, portion;
1690   gdb_byte temp_buffer[MAX_REGISTER_SIZE];
1691
1692   if (reg_nr == PSEUDO_BANK_REGNUM)
1693     {
1694       /* When the bank register is written to, the whole register bank
1695          is switched and all values in the bank registers must be read
1696          from the target/sim again.  We're just invalidating the regcache
1697          so that a re-read happens next time it's necessary.  */
1698       int bregnum;
1699
1700       regcache_raw_write (regcache, BANK_REGNUM, buffer);
1701       for (bregnum = R0_BANK0_REGNUM; bregnum < MACLB_REGNUM; ++bregnum)
1702         regcache_invalidate (regcache, bregnum);
1703     }
1704   else if (reg_nr >= DR0_REGNUM && reg_nr <= DR_LAST_REGNUM)
1705     {
1706       base_regnum = dr_reg_base_num (gdbarch, reg_nr);
1707
1708       /* We must pay attention to the endiannes.  */
1709       sh_register_convert_to_raw (gdbarch, register_type (gdbarch, reg_nr),
1710                                   reg_nr, buffer, temp_buffer);
1711
1712       /* Write the real regs for which this one is an alias.  */
1713       for (portion = 0; portion < 2; portion++)
1714         regcache_raw_write (regcache, base_regnum + portion,
1715                             (temp_buffer
1716                              + register_size (gdbarch,
1717                                               base_regnum) * portion));
1718     }
1719   else if (reg_nr >= FV0_REGNUM && reg_nr <= FV_LAST_REGNUM)
1720     {
1721       base_regnum = fv_reg_base_num (gdbarch, reg_nr);
1722
1723       /* Write the real regs for which this one is an alias.  */
1724       for (portion = 0; portion < 4; portion++)
1725         regcache_raw_write (regcache, base_regnum + portion,
1726                             (buffer
1727                              + register_size (gdbarch,
1728                                               base_regnum) * portion));
1729     }
1730 }
1731
1732 static int
1733 sh_dsp_register_sim_regno (struct gdbarch *gdbarch, int nr)
1734 {
1735   if (legacy_register_sim_regno (gdbarch, nr) < 0)
1736     return legacy_register_sim_regno (gdbarch, nr);
1737   if (nr >= DSR_REGNUM && nr <= Y1_REGNUM)
1738     return nr - DSR_REGNUM + SIM_SH_DSR_REGNUM;
1739   if (nr == MOD_REGNUM)
1740     return SIM_SH_MOD_REGNUM;
1741   if (nr == RS_REGNUM)
1742     return SIM_SH_RS_REGNUM;
1743   if (nr == RE_REGNUM)
1744     return SIM_SH_RE_REGNUM;
1745   if (nr >= DSP_R0_BANK_REGNUM && nr <= DSP_R7_BANK_REGNUM)
1746     return nr - DSP_R0_BANK_REGNUM + SIM_SH_R0_BANK_REGNUM;
1747   return nr;
1748 }
1749
1750 static int
1751 sh_sh2a_register_sim_regno (struct gdbarch *gdbarch, int nr)
1752 {
1753   switch (nr)
1754     {
1755       case TBR_REGNUM:
1756         return SIM_SH_TBR_REGNUM;
1757       case IBNR_REGNUM:
1758         return SIM_SH_IBNR_REGNUM;
1759       case IBCR_REGNUM:
1760         return SIM_SH_IBCR_REGNUM;
1761       case BANK_REGNUM:
1762         return SIM_SH_BANK_REGNUM;
1763       case MACLB_REGNUM:
1764         return SIM_SH_BANK_MACL_REGNUM;
1765       case GBRB_REGNUM:
1766         return SIM_SH_BANK_GBR_REGNUM;
1767       case PRB_REGNUM:
1768         return SIM_SH_BANK_PR_REGNUM;
1769       case IVNB_REGNUM:
1770         return SIM_SH_BANK_IVN_REGNUM;
1771       case MACHB_REGNUM:
1772         return SIM_SH_BANK_MACH_REGNUM;
1773       default:
1774         break;
1775     }
1776   return legacy_register_sim_regno (gdbarch, nr);
1777 }
1778
1779 /* Set up the register unwinding such that call-clobbered registers are
1780    not displayed in frames >0 because the true value is not certain.
1781    The 'undefined' registers will show up as 'not available' unless the
1782    CFI says otherwise.
1783
1784    This function is currently set up for SH4 and compatible only.  */
1785
1786 static void
1787 sh_dwarf2_frame_init_reg (struct gdbarch *gdbarch, int regnum,
1788                           struct dwarf2_frame_state_reg *reg,
1789                           struct frame_info *this_frame)
1790 {
1791   /* Mark the PC as the destination for the return address.  */
1792   if (regnum == gdbarch_pc_regnum (gdbarch))
1793     reg->how = DWARF2_FRAME_REG_RA;
1794
1795   /* Mark the stack pointer as the call frame address.  */
1796   else if (regnum == gdbarch_sp_regnum (gdbarch))
1797     reg->how = DWARF2_FRAME_REG_CFA;
1798
1799   /* The above was taken from the default init_reg in dwarf2-frame.c
1800      while the below is SH specific.  */
1801
1802   /* Caller save registers.  */
1803   else if ((regnum >= R0_REGNUM && regnum <= R0_REGNUM+7)
1804            || (regnum >= FR0_REGNUM && regnum <= FR0_REGNUM+11)
1805            || (regnum >= DR0_REGNUM && regnum <= DR0_REGNUM+5)
1806            || (regnum >= FV0_REGNUM && regnum <= FV0_REGNUM+2)
1807            || (regnum == MACH_REGNUM)
1808            || (regnum == MACL_REGNUM)
1809            || (regnum == FPUL_REGNUM)
1810            || (regnum == SR_REGNUM))
1811     reg->how = DWARF2_FRAME_REG_UNDEFINED;
1812
1813   /* Callee save registers.  */
1814   else if ((regnum >= R0_REGNUM+8 && regnum <= R0_REGNUM+15)
1815            || (regnum >= FR0_REGNUM+12 && regnum <= FR0_REGNUM+15)
1816            || (regnum >= DR0_REGNUM+6 && regnum <= DR0_REGNUM+8)
1817            || (regnum == FV0_REGNUM+3))
1818     reg->how = DWARF2_FRAME_REG_SAME_VALUE;
1819
1820   /* Other registers.  These are not in the ABI and may or may not
1821      mean anything in frames >0 so don't show them.  */
1822   else if ((regnum >= R0_BANK0_REGNUM && regnum <= R0_BANK0_REGNUM+15)
1823            || (regnum == GBR_REGNUM)
1824            || (regnum == VBR_REGNUM)
1825            || (regnum == FPSCR_REGNUM)
1826            || (regnum == SSR_REGNUM)
1827            || (regnum == SPC_REGNUM))
1828     reg->how = DWARF2_FRAME_REG_UNDEFINED;
1829 }
1830
1831 static struct sh_frame_cache *
1832 sh_alloc_frame_cache (void)
1833 {
1834   struct sh_frame_cache *cache;
1835   int i;
1836
1837   cache = FRAME_OBSTACK_ZALLOC (struct sh_frame_cache);
1838
1839   /* Base address.  */
1840   cache->base = 0;
1841   cache->saved_sp = 0;
1842   cache->sp_offset = 0;
1843   cache->pc = 0;
1844
1845   /* Frameless until proven otherwise.  */
1846   cache->uses_fp = 0;
1847
1848   /* Saved registers.  We initialize these to -1 since zero is a valid
1849      offset (that's where fp is supposed to be stored).  */
1850   for (i = 0; i < SH_NUM_REGS; i++)
1851     {
1852       cache->saved_regs[i] = -1;
1853     }
1854
1855   return cache;
1856 }
1857
1858 static struct sh_frame_cache *
1859 sh_frame_cache (struct frame_info *this_frame, void **this_cache)
1860 {
1861   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1862   struct sh_frame_cache *cache;
1863   CORE_ADDR current_pc;
1864   int i;
1865
1866   if (*this_cache)
1867     return (struct sh_frame_cache *) *this_cache;
1868
1869   cache = sh_alloc_frame_cache ();
1870   *this_cache = cache;
1871
1872   /* In principle, for normal frames, fp holds the frame pointer,
1873      which holds the base address for the current stack frame.
1874      However, for functions that don't need it, the frame pointer is
1875      optional.  For these "frameless" functions the frame pointer is
1876      actually the frame pointer of the calling frame.  */
1877   cache->base = get_frame_register_unsigned (this_frame, FP_REGNUM);
1878   if (cache->base == 0)
1879     return cache;
1880
1881   cache->pc = get_frame_func (this_frame);
1882   current_pc = get_frame_pc (this_frame);
1883   if (cache->pc != 0)
1884     {
1885       ULONGEST fpscr;
1886
1887       /* Check for the existence of the FPSCR register.  If it exists,
1888          fetch its value for use in prologue analysis.  Passing a zero
1889          value is the best choice for architecture variants upon which
1890          there's no FPSCR register.  */
1891       if (gdbarch_register_reggroup_p (gdbarch, FPSCR_REGNUM, all_reggroup))
1892         fpscr = get_frame_register_unsigned (this_frame, FPSCR_REGNUM);
1893       else
1894         fpscr = 0;
1895
1896       sh_analyze_prologue (gdbarch, cache->pc, current_pc, cache, fpscr);
1897     }
1898
1899   if (!cache->uses_fp)
1900     {
1901       /* We didn't find a valid frame, which means that CACHE->base
1902          currently holds the frame pointer for our calling frame.  If
1903          we're at the start of a function, or somewhere half-way its
1904          prologue, the function's frame probably hasn't been fully
1905          setup yet.  Try to reconstruct the base address for the stack
1906          frame by looking at the stack pointer.  For truly "frameless"
1907          functions this might work too.  */
1908       cache->base = get_frame_register_unsigned
1909                      (this_frame, gdbarch_sp_regnum (gdbarch));
1910     }
1911
1912   /* Now that we have the base address for the stack frame we can
1913      calculate the value of sp in the calling frame.  */
1914   cache->saved_sp = cache->base + cache->sp_offset;
1915
1916   /* Adjust all the saved registers such that they contain addresses
1917      instead of offsets.  */
1918   for (i = 0; i < SH_NUM_REGS; i++)
1919     if (cache->saved_regs[i] != -1)
1920       cache->saved_regs[i] = cache->saved_sp - cache->saved_regs[i] - 4;
1921
1922   return cache;
1923 }
1924
1925 static struct value *
1926 sh_frame_prev_register (struct frame_info *this_frame,
1927                         void **this_cache, int regnum)
1928 {
1929   struct gdbarch *gdbarch = get_frame_arch (this_frame);
1930   struct sh_frame_cache *cache = sh_frame_cache (this_frame, this_cache);
1931
1932   gdb_assert (regnum >= 0);
1933
1934   if (regnum == gdbarch_sp_regnum (gdbarch) && cache->saved_sp)
1935     return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
1936
1937   /* The PC of the previous frame is stored in the PR register of
1938      the current frame.  Frob regnum so that we pull the value from
1939      the correct place.  */
1940   if (regnum == gdbarch_pc_regnum (gdbarch))
1941     regnum = PR_REGNUM;
1942
1943   if (regnum < SH_NUM_REGS && cache->saved_regs[regnum] != -1)
1944     return frame_unwind_got_memory (this_frame, regnum,
1945                                     cache->saved_regs[regnum]);
1946
1947   return frame_unwind_got_register (this_frame, regnum, regnum);
1948 }
1949
1950 static void
1951 sh_frame_this_id (struct frame_info *this_frame, void **this_cache,
1952                   struct frame_id *this_id)
1953 {
1954   struct sh_frame_cache *cache = sh_frame_cache (this_frame, this_cache);
1955
1956   /* This marks the outermost frame.  */
1957   if (cache->base == 0)
1958     return;
1959
1960   *this_id = frame_id_build (cache->saved_sp, cache->pc);
1961 }
1962
1963 static const struct frame_unwind sh_frame_unwind = {
1964   NORMAL_FRAME,
1965   default_frame_unwind_stop_reason,
1966   sh_frame_this_id,
1967   sh_frame_prev_register,
1968   NULL,
1969   default_frame_sniffer
1970 };
1971
1972 static CORE_ADDR
1973 sh_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
1974 {
1975   return frame_unwind_register_unsigned (next_frame,
1976                                          gdbarch_sp_regnum (gdbarch));
1977 }
1978
1979 static CORE_ADDR
1980 sh_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1981 {
1982   return frame_unwind_register_unsigned (next_frame,
1983                                          gdbarch_pc_regnum (gdbarch));
1984 }
1985
1986 static struct frame_id
1987 sh_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
1988 {
1989   CORE_ADDR sp = get_frame_register_unsigned (this_frame,
1990                                               gdbarch_sp_regnum (gdbarch));
1991   return frame_id_build (sp, get_frame_pc (this_frame));
1992 }
1993
1994 static CORE_ADDR
1995 sh_frame_base_address (struct frame_info *this_frame, void **this_cache)
1996 {
1997   struct sh_frame_cache *cache = sh_frame_cache (this_frame, this_cache);
1998
1999   return cache->base;
2000 }
2001
2002 static const struct frame_base sh_frame_base = {
2003   &sh_frame_unwind,
2004   sh_frame_base_address,
2005   sh_frame_base_address,
2006   sh_frame_base_address
2007 };
2008
2009 static struct sh_frame_cache *
2010 sh_make_stub_cache (struct frame_info *this_frame)
2011 {
2012   struct gdbarch *gdbarch = get_frame_arch (this_frame);
2013   struct sh_frame_cache *cache;
2014
2015   cache = sh_alloc_frame_cache ();
2016
2017   cache->saved_sp
2018     = get_frame_register_unsigned (this_frame, gdbarch_sp_regnum (gdbarch));
2019
2020   return cache;
2021 }
2022
2023 static void
2024 sh_stub_this_id (struct frame_info *this_frame, void **this_cache,
2025                  struct frame_id *this_id)
2026 {
2027   struct sh_frame_cache *cache;
2028
2029   if (*this_cache == NULL)
2030     *this_cache = sh_make_stub_cache (this_frame);
2031   cache = (struct sh_frame_cache *) *this_cache;
2032
2033   *this_id = frame_id_build (cache->saved_sp, get_frame_pc (this_frame));
2034 }
2035
2036 static int
2037 sh_stub_unwind_sniffer (const struct frame_unwind *self,
2038                         struct frame_info *this_frame,
2039                         void **this_prologue_cache)
2040 {
2041   CORE_ADDR addr_in_block;
2042
2043   addr_in_block = get_frame_address_in_block (this_frame);
2044   if (in_plt_section (addr_in_block))
2045     return 1;
2046
2047   return 0;
2048 }
2049
2050 static const struct frame_unwind sh_stub_unwind =
2051 {
2052   NORMAL_FRAME,
2053   default_frame_unwind_stop_reason,
2054   sh_stub_this_id,
2055   sh_frame_prev_register,
2056   NULL,
2057   sh_stub_unwind_sniffer
2058 };
2059
2060 /* Implement the stack_frame_destroyed_p gdbarch method.
2061
2062    The epilogue is defined here as the area at the end of a function,
2063    either on the `ret' instruction itself or after an instruction which
2064    destroys the function's stack frame.  */
2065
2066 static int
2067 sh_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
2068 {
2069   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
2070   CORE_ADDR func_addr = 0, func_end = 0;
2071
2072   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
2073     {
2074       ULONGEST inst;
2075       /* The sh epilogue is max. 14 bytes long.  Give another 14 bytes
2076          for a nop and some fixed data (e.g. big offsets) which are
2077          unfortunately also treated as part of the function (which
2078          means, they are below func_end.  */
2079       CORE_ADDR addr = func_end - 28;
2080       if (addr < func_addr + 4)
2081         addr = func_addr + 4;
2082       if (pc < addr)
2083         return 0;
2084
2085       /* First search forward until hitting an rts.  */
2086       while (addr < func_end
2087              && !IS_RTS (read_memory_unsigned_integer (addr, 2, byte_order)))
2088         addr += 2;
2089       if (addr >= func_end)
2090         return 0;
2091
2092       /* At this point we should find a mov.l @r15+,r14 instruction,
2093          either before or after the rts.  If not, then the function has
2094          probably no "normal" epilogue and we bail out here.  */
2095       inst = read_memory_unsigned_integer (addr - 2, 2, byte_order);
2096       if (IS_RESTORE_FP (read_memory_unsigned_integer (addr - 2, 2,
2097                                                        byte_order)))
2098         addr -= 2;
2099       else if (!IS_RESTORE_FP (read_memory_unsigned_integer (addr + 2, 2,
2100                                                              byte_order)))
2101         return 0;
2102
2103       inst = read_memory_unsigned_integer (addr - 2, 2, byte_order);
2104
2105       /* Step over possible lds.l @r15+,macl.  */
2106       if (IS_MACL_LDS (inst))
2107         {
2108           addr -= 2;
2109           inst = read_memory_unsigned_integer (addr - 2, 2, byte_order);
2110         }
2111
2112       /* Step over possible lds.l @r15+,pr.  */
2113       if (IS_LDS (inst))
2114         {
2115           addr -= 2;
2116           inst = read_memory_unsigned_integer (addr - 2, 2, byte_order);
2117         }
2118
2119       /* Step over possible mov r14,r15.  */
2120       if (IS_MOV_FP_SP (inst))
2121         {
2122           addr -= 2;
2123           inst = read_memory_unsigned_integer (addr - 2, 2, byte_order);
2124         }
2125
2126       /* Now check for FP adjustments, using add #imm,r14 or add rX, r14
2127          instructions.  */
2128       while (addr > func_addr + 4
2129              && (IS_ADD_REG_TO_FP (inst) || IS_ADD_IMM_FP (inst)))
2130         {
2131           addr -= 2;
2132           inst = read_memory_unsigned_integer (addr - 2, 2, byte_order);
2133         }
2134
2135       /* On SH2a check if the previous instruction was perhaps a MOVI20.
2136          That's allowed for the epilogue.  */
2137       if ((gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_sh2a
2138            || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_sh2a_nofpu)
2139           && addr > func_addr + 6
2140           && IS_MOVI20 (read_memory_unsigned_integer (addr - 4, 2,
2141                                                       byte_order)))
2142         addr -= 4;
2143
2144       if (pc >= addr)
2145         return 1;
2146     }
2147   return 0;
2148 }
2149
2150
2151 /* Supply register REGNUM from the buffer specified by REGS and LEN
2152    in the register set REGSET to register cache REGCACHE.
2153    REGTABLE specifies where each register can be found in REGS.
2154    If REGNUM is -1, do this for all registers in REGSET.  */
2155
2156 void
2157 sh_corefile_supply_regset (const struct regset *regset,
2158                            struct regcache *regcache,
2159                            int regnum, const void *regs, size_t len)
2160 {
2161   struct gdbarch *gdbarch = get_regcache_arch (regcache);
2162   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2163   const struct sh_corefile_regmap *regmap = (regset == &sh_corefile_gregset
2164                                              ? tdep->core_gregmap
2165                                              : tdep->core_fpregmap);
2166   int i;
2167
2168   for (i = 0; regmap[i].regnum != -1; i++)
2169     {
2170       if ((regnum == -1 || regnum == regmap[i].regnum)
2171           && regmap[i].offset + 4 <= len)
2172         regcache_raw_supply (regcache, regmap[i].regnum,
2173                              (char *)regs + regmap[i].offset);
2174     }
2175 }
2176
2177 /* Collect register REGNUM in the register set REGSET from register cache
2178    REGCACHE into the buffer specified by REGS and LEN.
2179    REGTABLE specifies where each register can be found in REGS.
2180    If REGNUM is -1, do this for all registers in REGSET.  */
2181
2182 void
2183 sh_corefile_collect_regset (const struct regset *regset,
2184                             const struct regcache *regcache,
2185                             int regnum, void *regs, size_t len)
2186 {
2187   struct gdbarch *gdbarch = get_regcache_arch (regcache);
2188   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2189   const struct sh_corefile_regmap *regmap = (regset == &sh_corefile_gregset
2190                                              ? tdep->core_gregmap
2191                                              : tdep->core_fpregmap);
2192   int i;
2193
2194   for (i = 0; regmap[i].regnum != -1; i++)
2195     {
2196       if ((regnum == -1 || regnum == regmap[i].regnum)
2197           && regmap[i].offset + 4 <= len)
2198         regcache_raw_collect (regcache, regmap[i].regnum,
2199                               (char *)regs + regmap[i].offset);
2200     }
2201 }
2202
2203 /* The following two regsets have the same contents, so it is tempting to
2204    unify them, but they are distiguished by their address, so don't.  */
2205
2206 const struct regset sh_corefile_gregset =
2207 {
2208   NULL,
2209   sh_corefile_supply_regset,
2210   sh_corefile_collect_regset
2211 };
2212
2213 static const struct regset sh_corefile_fpregset =
2214 {
2215   NULL,
2216   sh_corefile_supply_regset,
2217   sh_corefile_collect_regset
2218 };
2219
2220 static void
2221 sh_iterate_over_regset_sections (struct gdbarch *gdbarch,
2222                                  iterate_over_regset_sections_cb *cb,
2223                                  void *cb_data,
2224                                  const struct regcache *regcache)
2225 {
2226   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
2227
2228   if (tdep->core_gregmap != NULL)
2229     cb (".reg", tdep->sizeof_gregset, &sh_corefile_gregset, NULL, cb_data);
2230
2231   if (tdep->core_fpregmap != NULL)
2232     cb (".reg2", tdep->sizeof_fpregset, &sh_corefile_fpregset, NULL, cb_data);
2233 }
2234
2235 /* This is the implementation of gdbarch method
2236    return_in_first_hidden_param_p.  */
2237
2238 static int
2239 sh_return_in_first_hidden_param_p (struct gdbarch *gdbarch,
2240                                      struct type *type)
2241 {
2242   return 0;
2243 }
2244
2245 \f
2246
2247 static struct gdbarch *
2248 sh_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
2249 {
2250   struct gdbarch *gdbarch;
2251   struct gdbarch_tdep *tdep;
2252
2253   /* SH5 is handled entirely in sh64-tdep.c.  */
2254   if (info.bfd_arch_info->mach == bfd_mach_sh5)
2255     return sh64_gdbarch_init (info, arches);
2256
2257   /* If there is already a candidate, use it.  */
2258   arches = gdbarch_list_lookup_by_info (arches, &info);
2259   if (arches != NULL)
2260     return arches->gdbarch;
2261
2262   /* None found, create a new architecture from the information
2263      provided.  */
2264   tdep = XCNEW (struct gdbarch_tdep);
2265   gdbarch = gdbarch_alloc (&info, tdep);
2266
2267   set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
2268   set_gdbarch_int_bit (gdbarch, 4 * TARGET_CHAR_BIT);
2269   set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
2270   set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
2271
2272   set_gdbarch_wchar_bit (gdbarch, 2 * TARGET_CHAR_BIT);
2273   set_gdbarch_wchar_signed (gdbarch, 0);
2274
2275   set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
2276   set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
2277   set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
2278   set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
2279
2280   set_gdbarch_num_regs (gdbarch, SH_NUM_REGS);
2281   set_gdbarch_sp_regnum (gdbarch, 15);
2282   set_gdbarch_pc_regnum (gdbarch, 16);
2283   set_gdbarch_fp0_regnum (gdbarch, -1);
2284   set_gdbarch_num_pseudo_regs (gdbarch, 0);
2285
2286   set_gdbarch_register_type (gdbarch, sh_default_register_type);
2287   set_gdbarch_register_reggroup_p (gdbarch, sh_register_reggroup_p);
2288
2289   set_gdbarch_breakpoint_kind_from_pc (gdbarch, sh_breakpoint_kind_from_pc);
2290   set_gdbarch_sw_breakpoint_from_kind (gdbarch, sh_sw_breakpoint_from_kind);
2291
2292   set_gdbarch_register_sim_regno (gdbarch, legacy_register_sim_regno);
2293
2294   set_gdbarch_return_value (gdbarch, sh_return_value_nofpu);
2295
2296   set_gdbarch_skip_prologue (gdbarch, sh_skip_prologue);
2297   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2298
2299   set_gdbarch_push_dummy_call (gdbarch, sh_push_dummy_call_nofpu);
2300   set_gdbarch_return_in_first_hidden_param_p (gdbarch,
2301                                               sh_return_in_first_hidden_param_p);
2302
2303   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
2304
2305   set_gdbarch_frame_align (gdbarch, sh_frame_align);
2306   set_gdbarch_unwind_sp (gdbarch, sh_unwind_sp);
2307   set_gdbarch_unwind_pc (gdbarch, sh_unwind_pc);
2308   set_gdbarch_dummy_id (gdbarch, sh_dummy_id);
2309   frame_base_set_default (gdbarch, &sh_frame_base);
2310
2311   set_gdbarch_stack_frame_destroyed_p (gdbarch, sh_stack_frame_destroyed_p);
2312
2313   dwarf2_frame_set_init_reg (gdbarch, sh_dwarf2_frame_init_reg);
2314
2315   set_gdbarch_iterate_over_regset_sections
2316     (gdbarch, sh_iterate_over_regset_sections);
2317
2318   switch (info.bfd_arch_info->mach)
2319     {
2320     case bfd_mach_sh:
2321       set_gdbarch_register_name (gdbarch, sh_sh_register_name);
2322       break;
2323
2324     case bfd_mach_sh2:
2325       set_gdbarch_register_name (gdbarch, sh_sh_register_name);
2326       break;
2327
2328     case bfd_mach_sh2e:
2329       /* doubles on sh2e and sh3e are actually 4 byte.  */
2330       set_gdbarch_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
2331       set_gdbarch_double_format (gdbarch, floatformats_ieee_single);
2332
2333       set_gdbarch_register_name (gdbarch, sh_sh2e_register_name);
2334       set_gdbarch_register_type (gdbarch, sh_sh3e_register_type);
2335       set_gdbarch_fp0_regnum (gdbarch, 25);
2336       set_gdbarch_return_value (gdbarch, sh_return_value_fpu);
2337       set_gdbarch_push_dummy_call (gdbarch, sh_push_dummy_call_fpu);
2338       break;
2339
2340     case bfd_mach_sh2a:
2341       set_gdbarch_register_name (gdbarch, sh_sh2a_register_name);
2342       set_gdbarch_register_type (gdbarch, sh_sh2a_register_type);
2343       set_gdbarch_register_sim_regno (gdbarch, sh_sh2a_register_sim_regno);
2344
2345       set_gdbarch_fp0_regnum (gdbarch, 25);
2346       set_gdbarch_num_pseudo_regs (gdbarch, 9);
2347       set_gdbarch_pseudo_register_read (gdbarch, sh_pseudo_register_read);
2348       set_gdbarch_pseudo_register_write (gdbarch, sh_pseudo_register_write);
2349       set_gdbarch_return_value (gdbarch, sh_return_value_fpu);
2350       set_gdbarch_push_dummy_call (gdbarch, sh_push_dummy_call_fpu);
2351       break;
2352
2353     case bfd_mach_sh2a_nofpu:
2354       set_gdbarch_register_name (gdbarch, sh_sh2a_nofpu_register_name);
2355       set_gdbarch_register_sim_regno (gdbarch, sh_sh2a_register_sim_regno);
2356
2357       set_gdbarch_num_pseudo_regs (gdbarch, 1);
2358       set_gdbarch_pseudo_register_read (gdbarch, sh_pseudo_register_read);
2359       set_gdbarch_pseudo_register_write (gdbarch, sh_pseudo_register_write);
2360       break;
2361
2362     case bfd_mach_sh_dsp:
2363       set_gdbarch_register_name (gdbarch, sh_sh_dsp_register_name);
2364       set_gdbarch_register_sim_regno (gdbarch, sh_dsp_register_sim_regno);
2365       break;
2366
2367     case bfd_mach_sh3:
2368     case bfd_mach_sh3_nommu:
2369     case bfd_mach_sh2a_nofpu_or_sh3_nommu:
2370       set_gdbarch_register_name (gdbarch, sh_sh3_register_name);
2371       break;
2372
2373     case bfd_mach_sh3e:
2374     case bfd_mach_sh2a_or_sh3e:
2375       /* doubles on sh2e and sh3e are actually 4 byte.  */
2376       set_gdbarch_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
2377       set_gdbarch_double_format (gdbarch, floatformats_ieee_single);
2378
2379       set_gdbarch_register_name (gdbarch, sh_sh3e_register_name);
2380       set_gdbarch_register_type (gdbarch, sh_sh3e_register_type);
2381       set_gdbarch_fp0_regnum (gdbarch, 25);
2382       set_gdbarch_return_value (gdbarch, sh_return_value_fpu);
2383       set_gdbarch_push_dummy_call (gdbarch, sh_push_dummy_call_fpu);
2384       break;
2385
2386     case bfd_mach_sh3_dsp:
2387       set_gdbarch_register_name (gdbarch, sh_sh3_dsp_register_name);
2388       set_gdbarch_register_sim_regno (gdbarch, sh_dsp_register_sim_regno);
2389       break;
2390
2391     case bfd_mach_sh4:
2392     case bfd_mach_sh4a:
2393     case bfd_mach_sh2a_or_sh4:
2394       set_gdbarch_register_name (gdbarch, sh_sh4_register_name);
2395       set_gdbarch_register_type (gdbarch, sh_sh4_register_type);
2396       set_gdbarch_fp0_regnum (gdbarch, 25);
2397       set_gdbarch_num_pseudo_regs (gdbarch, 13);
2398       set_gdbarch_pseudo_register_read (gdbarch, sh_pseudo_register_read);
2399       set_gdbarch_pseudo_register_write (gdbarch, sh_pseudo_register_write);
2400       set_gdbarch_return_value (gdbarch, sh_return_value_fpu);
2401       set_gdbarch_push_dummy_call (gdbarch, sh_push_dummy_call_fpu);
2402       break;
2403
2404     case bfd_mach_sh4_nofpu:
2405     case bfd_mach_sh4a_nofpu:
2406     case bfd_mach_sh4_nommu_nofpu:
2407     case bfd_mach_sh2a_nofpu_or_sh4_nommu_nofpu:
2408       set_gdbarch_register_name (gdbarch, sh_sh4_nofpu_register_name);
2409       break;
2410
2411     case bfd_mach_sh4al_dsp:
2412       set_gdbarch_register_name (gdbarch, sh_sh4al_dsp_register_name);
2413       set_gdbarch_register_sim_regno (gdbarch, sh_dsp_register_sim_regno);
2414       break;
2415
2416     default:
2417       set_gdbarch_register_name (gdbarch, sh_sh_register_name);
2418       break;
2419     }
2420
2421   /* Hook in ABI-specific overrides, if they have been registered.  */
2422   gdbarch_init_osabi (info, gdbarch);
2423
2424   dwarf2_append_unwinders (gdbarch);
2425   frame_unwind_append_unwinder (gdbarch, &sh_stub_unwind);
2426   frame_unwind_append_unwinder (gdbarch, &sh_frame_unwind);
2427
2428   return gdbarch;
2429 }
2430
2431 static void
2432 show_sh_command (char *args, int from_tty)
2433 {
2434   help_list (showshcmdlist, "show sh ", all_commands, gdb_stdout);
2435 }
2436
2437 static void
2438 set_sh_command (char *args, int from_tty)
2439 {
2440   printf_unfiltered
2441     ("\"set sh\" must be followed by an appropriate subcommand.\n");
2442   help_list (setshcmdlist, "set sh ", all_commands, gdb_stdout);
2443 }
2444
2445 extern initialize_file_ftype _initialize_sh_tdep;  /* -Wmissing-prototypes */
2446
2447 void
2448 _initialize_sh_tdep (void)
2449 {
2450   gdbarch_register (bfd_arch_sh, sh_gdbarch_init, NULL);
2451
2452   add_prefix_cmd ("sh", no_class, set_sh_command, "SH specific commands.",
2453                   &setshcmdlist, "set sh ", 0, &setlist);
2454   add_prefix_cmd ("sh", no_class, show_sh_command, "SH specific commands.",
2455                   &showshcmdlist, "show sh ", 0, &showlist);
2456   
2457   add_setshow_enum_cmd ("calling-convention", class_vars, sh_cc_enum,
2458                         &sh_active_calling_convention,
2459                         _("Set calling convention used when calling target "
2460                           "functions from GDB."),
2461                         _("Show calling convention used when calling target "
2462                           "functions from GDB."),
2463                         _("gcc       - Use GCC calling convention (default).\n"
2464                           "renesas   - Enforce Renesas calling convention."),
2465                         NULL, NULL,
2466                         &setshcmdlist, &showshcmdlist);
2467 }