* sh-tdep.c (sh_push_dummy_call_fpu): Initialize flt_argreg and
[platform/upstream/binutils.git] / gdb / sh-tdep.c
1 /* Target-dependent code for Hitachi Super-H, for GDB.
2    Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3    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 2 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, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 /*
23    Contributed by Steve Chamberlain
24    sac@cygnus.com
25  */
26
27 #include "defs.h"
28 #include "frame.h"
29 #include "frame-base.h"
30 #include "frame-unwind.h"
31 #include "dwarf2-frame.h"
32 #include "symtab.h"
33 #include "symfile.h"
34 #include "gdbtypes.h"
35 #include "gdbcmd.h"
36 #include "gdbcore.h"
37 #include "value.h"
38 #include "dis-asm.h"
39 #include "inferior.h"
40 #include "gdb_string.h"
41 #include "gdb_assert.h"
42 #include "arch-utils.h"
43 #include "floatformat.h"
44 #include "regcache.h"
45 #include "doublest.h"
46 #include "osabi.h"
47
48 #include "sh-tdep.h"
49
50 #include "elf-bfd.h"
51 #include "solib-svr4.h"
52
53 /* sh flags */
54 #include "elf/sh.h"
55 /* registers numbers shared with the simulator */
56 #include "gdb/sim-sh.h"
57
58 static void (*sh_show_regs) (void);
59
60 #define SH_NUM_REGS 59
61
62 struct sh_frame_cache
63 {
64   /* Base address.  */
65   CORE_ADDR base;
66   LONGEST sp_offset;
67   CORE_ADDR pc;
68
69   /* Flag showing that a frame has been created in the prologue code. */
70   int uses_fp;
71
72   /* Saved registers.  */
73   CORE_ADDR saved_regs[SH_NUM_REGS];
74   CORE_ADDR saved_sp;
75 };
76
77 static const char *
78 sh_generic_register_name (int reg_nr)
79 {
80   static char *register_names[] = {
81     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
82     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
83     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
84     "fpul", "fpscr",
85     "fr0", "fr1", "fr2", "fr3", "fr4", "fr5", "fr6", "fr7",
86     "fr8", "fr9", "fr10", "fr11", "fr12", "fr13", "fr14", "fr15",
87     "ssr", "spc",
88     "r0b0", "r1b0", "r2b0", "r3b0", "r4b0", "r5b0", "r6b0", "r7b0",
89     "r0b1", "r1b1", "r2b1", "r3b1", "r4b1", "r5b1", "r6b1", "r7b1",
90   };
91   if (reg_nr < 0)
92     return NULL;
93   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
94     return NULL;
95   return register_names[reg_nr];
96 }
97
98 static const char *
99 sh_sh_register_name (int reg_nr)
100 {
101   static char *register_names[] = {
102     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
103     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
104     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
105     "", "",
106     "", "", "", "", "", "", "", "",
107     "", "", "", "", "", "", "", "",
108     "", "",
109     "", "", "", "", "", "", "", "",
110     "", "", "", "", "", "", "", "",
111   };
112   if (reg_nr < 0)
113     return NULL;
114   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
115     return NULL;
116   return register_names[reg_nr];
117 }
118
119 static const char *
120 sh_sh3_register_name (int reg_nr)
121 {
122   static char *register_names[] = {
123     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
124     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
125     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
126     "", "",
127     "", "", "", "", "", "", "", "",
128     "", "", "", "", "", "", "", "",
129     "ssr", "spc",
130     "r0b0", "r1b0", "r2b0", "r3b0", "r4b0", "r5b0", "r6b0", "r7b0",
131     "r0b1", "r1b1", "r2b1", "r3b1", "r4b1", "r5b1", "r6b1", "r7b1"
132   };
133   if (reg_nr < 0)
134     return NULL;
135   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
136     return NULL;
137   return register_names[reg_nr];
138 }
139
140 static const char *
141 sh_sh3e_register_name (int reg_nr)
142 {
143   static char *register_names[] = {
144     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
145     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
146     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
147     "fpul", "fpscr",
148     "fr0", "fr1", "fr2", "fr3", "fr4", "fr5", "fr6", "fr7",
149     "fr8", "fr9", "fr10", "fr11", "fr12", "fr13", "fr14", "fr15",
150     "ssr", "spc",
151     "r0b0", "r1b0", "r2b0", "r3b0", "r4b0", "r5b0", "r6b0", "r7b0",
152     "r0b1", "r1b1", "r2b1", "r3b1", "r4b1", "r5b1", "r6b1", "r7b1",
153   };
154   if (reg_nr < 0)
155     return NULL;
156   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
157     return NULL;
158   return register_names[reg_nr];
159 }
160
161 static const char *
162 sh_sh2e_register_name (int reg_nr)
163 {
164   static char *register_names[] = {
165     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
166     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
167     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
168     "fpul", "fpscr",
169     "fr0", "fr1", "fr2", "fr3", "fr4", "fr5", "fr6", "fr7",
170     "fr8", "fr9", "fr10", "fr11", "fr12", "fr13", "fr14", "fr15",
171     "", "",
172     "", "", "", "", "", "", "", "",
173     "", "", "", "", "", "", "", "",
174   };
175   if (reg_nr < 0)
176     return NULL;
177   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
178     return NULL;
179   return register_names[reg_nr];
180 }
181
182 static const char *
183 sh_sh_dsp_register_name (int reg_nr)
184 {
185   static char *register_names[] = {
186     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
187     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
188     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
189     "", "dsr",
190     "a0g", "a0", "a1g", "a1", "m0", "m1", "x0", "x1",
191     "y0", "y1", "", "", "", "", "", "mod",
192     "", "",
193     "rs", "re", "", "", "", "", "", "",
194     "", "", "", "", "", "", "", "",
195   };
196   if (reg_nr < 0)
197     return NULL;
198   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
199     return NULL;
200   return register_names[reg_nr];
201 }
202
203 static const char *
204 sh_sh3_dsp_register_name (int reg_nr)
205 {
206   static char *register_names[] = {
207     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
208     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
209     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
210     "", "dsr",
211     "a0g", "a0", "a1g", "a1", "m0", "m1", "x0", "x1",
212     "y0", "y1", "", "", "", "", "", "mod",
213     "ssr", "spc",
214     "rs", "re", "", "", "", "", "", "",
215     "r0b", "r1b", "r2b", "r3b", "r4b", "r5b", "r6b", "r7b"
216       "", "", "", "", "", "", "", "",
217   };
218   if (reg_nr < 0)
219     return NULL;
220   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
221     return NULL;
222   return register_names[reg_nr];
223 }
224
225 static const char *
226 sh_sh4_register_name (int reg_nr)
227 {
228   static char *register_names[] = {
229     /* general registers 0-15 */
230     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
231     "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
232     /* 16 - 22 */
233     "pc", "pr", "gbr", "vbr", "mach", "macl", "sr",
234     /* 23, 24 */
235     "fpul", "fpscr",
236     /* floating point registers 25 - 40 */
237     "fr0", "fr1", "fr2", "fr3", "fr4", "fr5", "fr6", "fr7",
238     "fr8", "fr9", "fr10", "fr11", "fr12", "fr13", "fr14", "fr15",
239     /* 41, 42 */
240     "ssr", "spc",
241     /* bank 0 43 - 50 */
242     "r0b0", "r1b0", "r2b0", "r3b0", "r4b0", "r5b0", "r6b0", "r7b0",
243     /* bank 1 51 - 58 */
244     "r0b1", "r1b1", "r2b1", "r3b1", "r4b1", "r5b1", "r6b1", "r7b1",
245     /* double precision (pseudo) 59 - 66 */
246     "dr0", "dr2", "dr4", "dr6", "dr8", "dr10", "dr12", "dr14",
247     /* vectors (pseudo) 67 - 70 */
248     "fv0", "fv4", "fv8", "fv12",
249     /* FIXME: missing XF 71 - 86 */
250     /* FIXME: missing XD 87 - 94 */
251   };
252   if (reg_nr < 0)
253     return NULL;
254   if (reg_nr >= (sizeof (register_names) / sizeof (*register_names)))
255     return NULL;
256   return register_names[reg_nr];
257 }
258
259 static const unsigned char *
260 sh_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
261 {
262   /* 0xc3c3 is trapa #c3, and it works in big and little endian modes */
263   static unsigned char breakpoint[] = { 0xc3, 0xc3 };
264
265   *lenptr = sizeof (breakpoint);
266   return breakpoint;
267 }
268
269 static CORE_ADDR
270 sh_push_dummy_code (struct gdbarch *gdbarch,
271                     CORE_ADDR sp, CORE_ADDR funaddr, int using_gcc,
272                     struct value **args, int nargs,
273                     struct type *value_type,
274                     CORE_ADDR *real_pc, CORE_ADDR *bp_addr)
275 {
276   /* Allocate space sufficient for a breakpoint.  */
277   sp = (sp - 2) & ~1;
278   /* Store the address of that breakpoint */
279   *bp_addr = sp;
280   /* sh always starts the call at the callee's entry point.  */
281   *real_pc = funaddr;
282   return sp;
283 }
284
285 /* Prologue looks like
286    mov.l        r14,@-r15
287    sts.l        pr,@-r15
288    mov.l        <regs>,@-r15
289    sub          <room_for_loca_vars>,r15
290    mov          r15,r14
291
292    Actually it can be more complicated than this but that's it, basically.
293  */
294
295 #define GET_SOURCE_REG(x)       (((x) >> 4) & 0xf)
296 #define GET_TARGET_REG(x)       (((x) >> 8) & 0xf)
297
298 /* STS.L PR,@-r15  0100111100100010
299    r15-4-->r15, PR-->(r15) */
300 #define IS_STS(x)               ((x) == 0x4f22)
301
302 /* MOV.L Rm,@-r15  00101111mmmm0110
303    r15-4-->r15, Rm-->(R15) */
304 #define IS_PUSH(x)              (((x) & 0xff0f) == 0x2f06)
305
306 /* MOV r15,r14     0110111011110011
307    r15-->r14  */
308 #define IS_MOV_SP_FP(x)         ((x) == 0x6ef3)
309
310 /* ADD #imm,r15    01111111iiiiiiii
311    r15+imm-->r15 */
312 #define IS_ADD_IMM_SP(x)        (((x) & 0xff00) == 0x7f00)
313
314 #define IS_MOV_R3(x)            (((x) & 0xff00) == 0x1a00)
315 #define IS_SHLL_R3(x)           ((x) == 0x4300)
316
317 /* ADD r3,r15      0011111100111100
318    r15+r3-->r15 */
319 #define IS_ADD_R3SP(x)          ((x) == 0x3f3c)
320
321 /* FMOV.S FRm,@-Rn  Rn-4-->Rn, FRm-->(Rn)     1111nnnnmmmm1011
322    FMOV DRm,@-Rn    Rn-8-->Rn, DRm-->(Rn)     1111nnnnmmm01011
323    FMOV XDm,@-Rn    Rn-8-->Rn, XDm-->(Rn)     1111nnnnmmm11011 */
324 /* CV, 2003-08-28: Only suitable with Rn == SP, therefore name changed to
325                    make this entirely clear. */
326 /* #define IS_FMOV(x)           (((x) & 0xf00f) == 0xf00b) */
327 #define IS_FPUSH(x)             (((x) & 0xff0f) == 0xff0b)
328
329 /* MOV Rm,Rn          Rm-->Rn        0110nnnnmmmm0011  4 <= m <= 7 */
330 #define IS_MOV_ARG_TO_REG(x) \
331         (((x) & 0xf00f) == 0x6003 && \
332          ((x) & 0x00f0) >= 0x0040 && \
333          ((x) & 0x00f0) <= 0x0070)
334 /* MOV.L Rm,@Rn               0010nnnnmmmm0010  n = 14, 4 <= m <= 7 */
335 #define IS_MOV_ARG_TO_IND_R14(x) \
336         (((x) & 0xff0f) == 0x2e02 && \
337          ((x) & 0x00f0) >= 0x0040 && \
338          ((x) & 0x00f0) <= 0x0070)
339 /* MOV.L Rm,@(disp*4,Rn)      00011110mmmmdddd  n = 14, 4 <= m <= 7 */
340 #define IS_MOV_ARG_TO_IND_R14_WITH_DISP(x) \
341         (((x) & 0xff00) == 0x1e00 && \
342          ((x) & 0x00f0) >= 0x0040 && \
343          ((x) & 0x00f0) <= 0x0070)
344
345 /* MOV.W @(disp*2,PC),Rn      1001nnnndddddddd */
346 #define IS_MOVW_PCREL_TO_REG(x) (((x) & 0xf000) == 0x9000)
347 /* MOV.L @(disp*4,PC),Rn      1101nnnndddddddd */
348 #define IS_MOVL_PCREL_TO_REG(x) (((x) & 0xf000) == 0xd000)
349 /* SUB Rn,R15                 00111111nnnn1000 */
350 #define IS_SUB_REG_FROM_SP(x)   (((x) & 0xff0f) == 0x3f08)
351
352 #define FPSCR_SZ                (1 << 20)
353
354 /* The following instructions are used for epilogue testing. */
355 #define IS_RESTORE_FP(x)        ((x) == 0x6ef6)
356 #define IS_RTS(x)               ((x) == 0x000b)
357 #define IS_LDS(x)               ((x) == 0x4f26)
358 #define IS_MOV_FP_SP(x)         ((x) == 0x6fe3)
359 #define IS_ADD_REG_TO_FP(x)     (((x) & 0xff0f) == 0x3e0c)
360 #define IS_ADD_IMM_FP(x)        (((x) & 0xff00) == 0x7e00)
361
362 /* Disassemble an instruction.  */
363 static int
364 gdb_print_insn_sh (bfd_vma memaddr, disassemble_info * info)
365 {
366   info->endian = TARGET_BYTE_ORDER;
367   return print_insn_sh (memaddr, info);
368 }
369
370 static CORE_ADDR
371 sh_analyze_prologue (CORE_ADDR pc, CORE_ADDR current_pc,
372                      struct sh_frame_cache *cache)
373 {
374   ULONGEST inst;
375   CORE_ADDR opc;
376   int offset;
377   int sav_offset = 0;
378   int r3_val = 0;
379   int reg, sav_reg = -1;
380
381   if (pc >= current_pc)
382     return current_pc;
383
384   cache->uses_fp = 0;
385   for (opc = pc + (2 * 28); pc < opc; pc += 2)
386     {
387       inst = read_memory_unsigned_integer (pc, 2);
388       /* See where the registers will be saved to */
389       if (IS_PUSH (inst))
390         {
391           cache->saved_regs[GET_SOURCE_REG (inst)] = cache->sp_offset;
392           cache->sp_offset += 4;
393         }
394       else if (IS_STS (inst))
395         {
396           cache->saved_regs[PR_REGNUM] = cache->sp_offset;
397           cache->sp_offset += 4;
398         }
399       else if (IS_MOV_R3 (inst))
400         {
401           r3_val = ((inst & 0xff) ^ 0x80) - 0x80;
402         }
403       else if (IS_SHLL_R3 (inst))
404         {
405           r3_val <<= 1;
406         }
407       else if (IS_ADD_R3SP (inst))
408         {
409           cache->sp_offset += -r3_val;
410         }
411       else if (IS_ADD_IMM_SP (inst))
412         {
413           offset = ((inst & 0xff) ^ 0x80) - 0x80;
414           cache->sp_offset -= offset;
415         }
416       else if (IS_MOVW_PCREL_TO_REG (inst))
417         {
418           if (sav_reg < 0)
419             {
420               reg = GET_TARGET_REG (inst);
421               if (reg < 14)
422                 {
423                   sav_reg = reg;
424                   offset = (((inst & 0xff) ^ 0x80) - 0x80) << 1;
425                   sav_offset =
426                     read_memory_integer (((pc + 4) & ~3) + offset, 2);
427                 }
428             }
429         }
430       else if (IS_MOVL_PCREL_TO_REG (inst))
431         {
432           if (sav_reg < 0)
433             {
434               reg = (inst & 0x0f00) >> 8;
435               if (reg < 14)
436                 {
437                   sav_reg = reg;
438                   offset = (((inst & 0xff) ^ 0x80) - 0x80) << 1;
439                   sav_offset =
440                     read_memory_integer (((pc + 4) & ~3) + offset, 4);
441                 }
442             }
443         }
444       else if (IS_SUB_REG_FROM_SP (inst))
445         {
446           reg = GET_SOURCE_REG (inst);
447           if (sav_reg > 0 && reg == sav_reg)
448             {
449               sav_reg = -1;
450             }
451           cache->sp_offset += sav_offset;
452         }
453       else if (IS_FPUSH (inst))
454         {
455           if (read_register (FPSCR_REGNUM) & FPSCR_SZ)
456             {
457               cache->sp_offset += 8;
458             }
459           else
460             {
461               cache->sp_offset += 4;
462             }
463         }
464       else if (IS_MOV_SP_FP (inst))
465         {
466           if (!cache->uses_fp)
467             cache->uses_fp = 1;
468           /* At this point, only allow argument register moves to other
469              registers or argument register moves to @(X,fp) which are
470              moving the register arguments onto the stack area allocated
471              by a former add somenumber to SP call.  Don't allow moving
472              to an fp indirect address above fp + cache->sp_offset. */
473           pc += 2;
474           for (opc = pc + 12; pc < opc; pc += 2)
475             {
476               inst = read_memory_integer (pc, 2);
477               if (IS_MOV_ARG_TO_IND_R14 (inst))
478                 {
479                   reg = GET_SOURCE_REG (inst);
480                   if (cache->sp_offset > 0)
481                     cache->saved_regs[reg] = cache->sp_offset;
482                 }
483               else if (IS_MOV_ARG_TO_IND_R14_WITH_DISP (inst))
484                 {
485                   reg = GET_SOURCE_REG (inst);
486                   offset = (inst & 0xf) * 4;
487                   if (cache->sp_offset > offset)
488                     cache->saved_regs[reg] = cache->sp_offset - offset;
489                 }
490               else if (IS_MOV_ARG_TO_REG (inst))
491                 continue;
492               else
493                 break;
494             }
495           break;
496         }
497 #if 0                           /* This used to just stop when it found an instruction that
498                                    was not considered part of the prologue.  Now, we just
499                                    keep going looking for likely instructions. */
500       else
501         break;
502 #endif
503     }
504
505   return pc;
506 }
507
508 /* Skip any prologue before the guts of a function */
509
510 /* Skip the prologue using the debug information. If this fails we'll
511    fall back on the 'guess' method below. */
512 static CORE_ADDR
513 after_prologue (CORE_ADDR pc)
514 {
515   struct symtab_and_line sal;
516   CORE_ADDR func_addr, func_end;
517
518   /* If we can not find the symbol in the partial symbol table, then
519      there is no hope we can determine the function's start address
520      with this code.  */
521   if (!find_pc_partial_function (pc, NULL, &func_addr, &func_end))
522     return 0;
523
524   /* Get the line associated with FUNC_ADDR.  */
525   sal = find_pc_line (func_addr, 0);
526
527   /* There are only two cases to consider.  First, the end of the source line
528      is within the function bounds.  In that case we return the end of the
529      source line.  Second is the end of the source line extends beyond the
530      bounds of the current function.  We need to use the slow code to
531      examine instructions in that case.  */
532   if (sal.end < func_end)
533     return sal.end;
534   else
535     return 0;
536 }
537
538 static CORE_ADDR
539 sh_skip_prologue (CORE_ADDR start_pc)
540 {
541   CORE_ADDR pc;
542   struct sh_frame_cache cache;
543
544   /* See if we can determine the end of the prologue via the symbol table.
545      If so, then return either PC, or the PC after the prologue, whichever
546      is greater.  */
547   pc = after_prologue (start_pc);
548
549   /* If after_prologue returned a useful address, then use it.  Else
550      fall back on the instruction skipping code. */
551   if (pc)
552     return max (pc, start_pc);
553
554   cache.sp_offset = -4;
555   pc = sh_analyze_prologue (start_pc, (CORE_ADDR) -1, &cache);
556   if (!cache.uses_fp)
557     return start_pc;
558
559   return pc;
560 }
561
562 /* Should call_function allocate stack space for a struct return?  */
563 static int
564 sh_use_struct_convention (int gcc_p, struct type *type)
565 {
566   int len = TYPE_LENGTH (type);
567   int nelem = TYPE_NFIELDS (type);
568   return ((len != 1 && len != 2 && len != 4 && len != 8) || nelem != 1) &&
569     (len != 8 || TYPE_LENGTH (TYPE_FIELD_TYPE (type, 0)) != 4);
570 }
571
572 /* Extract from an array REGBUF containing the (raw) register state
573    the address in which a function should return its structure value,
574    as a CORE_ADDR (or an expression that can be used as one).  */
575 static CORE_ADDR
576 sh_extract_struct_value_address (struct regcache *regcache)
577 {
578   ULONGEST addr;
579
580   regcache_cooked_read_unsigned (regcache, STRUCT_RETURN_REGNUM, &addr);
581   return addr;
582 }
583
584 static CORE_ADDR
585 sh_frame_align (struct gdbarch *ignore, CORE_ADDR sp)
586 {
587   return sp & ~3;
588 }
589
590 /* Function: push_dummy_call (formerly push_arguments)
591    Setup the function arguments for calling a function in the inferior.
592
593    On the Hitachi SH architecture, there are four registers (R4 to R7)
594    which are dedicated for passing function arguments.  Up to the first
595    four arguments (depending on size) may go into these registers.
596    The rest go on the stack.
597
598    MVS: Except on SH variants that have floating point registers.
599    In that case, float and double arguments are passed in the same
600    manner, but using FP registers instead of GP registers.
601
602    Arguments that are smaller than 4 bytes will still take up a whole
603    register or a whole 32-bit word on the stack, and will be 
604    right-justified in the register or the stack word.  This includes
605    chars, shorts, and small aggregate types.
606
607    Arguments that are larger than 4 bytes may be split between two or 
608    more registers.  If there are not enough registers free, an argument
609    may be passed partly in a register (or registers), and partly on the
610    stack.  This includes doubles, long longs, and larger aggregates. 
611    As far as I know, there is no upper limit to the size of aggregates 
612    that will be passed in this way; in other words, the convention of 
613    passing a pointer to a large aggregate instead of a copy is not used.
614
615    MVS: The above appears to be true for the SH variants that do not
616    have an FPU, however those that have an FPU appear to copy the
617    aggregate argument onto the stack (and not place it in registers)
618    if it is larger than 16 bytes (four GP registers).
619
620    An exceptional case exists for struct arguments (and possibly other
621    aggregates such as arrays) if the size is larger than 4 bytes but 
622    not a multiple of 4 bytes.  In this case the argument is never split 
623    between the registers and the stack, but instead is copied in its
624    entirety onto the stack, AND also copied into as many registers as 
625    there is room for.  In other words, space in registers permitting, 
626    two copies of the same argument are passed in.  As far as I can tell,
627    only the one on the stack is used, although that may be a function 
628    of the level of compiler optimization.  I suspect this is a compiler
629    bug.  Arguments of these odd sizes are left-justified within the 
630    word (as opposed to arguments smaller than 4 bytes, which are 
631    right-justified).
632
633    If the function is to return an aggregate type such as a struct, it 
634    is either returned in the normal return value register R0 (if its 
635    size is no greater than one byte), or else the caller must allocate
636    space into which the callee will copy the return value (if the size
637    is greater than one byte).  In this case, a pointer to the return 
638    value location is passed into the callee in register R2, which does 
639    not displace any of the other arguments passed in via registers R4
640    to R7.   */
641
642 /* Helper function to justify value in register according to endianess. */
643 static char *
644 sh_justify_value_in_reg (struct value *val, int len)
645 {
646   static char valbuf[4];
647
648   memset (valbuf, 0, sizeof (valbuf));
649   if (len < 4)
650     {
651       /* value gets right-justified in the register or stack word */
652       if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
653         memcpy (valbuf + (4 - len), (char *) VALUE_CONTENTS (val), len);
654       else
655         memcpy (valbuf, (char *) VALUE_CONTENTS (val), len);
656       return valbuf;
657     }
658   return (char *) VALUE_CONTENTS (val);
659 }
660
661 /* Helper function to eval number of bytes to allocate on stack. */
662 static CORE_ADDR
663 sh_stack_allocsize (int nargs, struct value **args)
664 {
665   int stack_alloc = 0;
666   while (nargs-- > 0)
667     stack_alloc += ((TYPE_LENGTH (VALUE_TYPE (args[nargs])) + 3) & ~3);
668   return stack_alloc;
669 }
670
671 /* Helper functions for getting the float arguments right.  Registers usage
672    depends on the ABI and the endianess.  The comments should enlighten how
673    it's intended to work. */
674
675 /* This array stores which of the float arg registers are already in use. */
676 static int flt_argreg_array[FLOAT_ARGLAST_REGNUM - FLOAT_ARG0_REGNUM + 1];
677
678 /* This function just resets the above array to "no reg used so far". */
679 static void
680 sh_init_flt_argreg (void)
681 {
682   memset (flt_argreg_array, 0, sizeof flt_argreg_array);
683 }
684
685 /* This function returns the next register to use for float arg passing.
686    It returns either a valid value between FLOAT_ARG0_REGNUM and
687    FLOAT_ARGLAST_REGNUM if a register is available, otherwise it returns 
688    FLOAT_ARGLAST_REGNUM + 1 to indicate that no register is available.
689
690    Note that register number 0 in flt_argreg_array corresponds with the
691    real float register fr4.  In contrast to FLOAT_ARG0_REGNUM (value is
692    29) the parity of the register number is preserved, which is important
693    for the double register passing test (see the "argreg & 1" test below). */
694 static int
695 sh_next_flt_argreg (int len)
696 {
697   int argreg;
698
699   /* First search for the next free register. */
700   for (argreg = 0; argreg <= FLOAT_ARGLAST_REGNUM - FLOAT_ARG0_REGNUM;
701        ++argreg)
702     if (!flt_argreg_array[argreg])
703       break;
704
705   /* No register left? */
706   if (argreg > FLOAT_ARGLAST_REGNUM - FLOAT_ARG0_REGNUM)
707     return FLOAT_ARGLAST_REGNUM + 1;
708
709   if (len == 8)
710     {
711       /* Doubles are always starting in a even register number. */
712       if (argreg & 1)
713         {
714           flt_argreg_array[argreg] = 1;
715
716           ++argreg;
717
718           /* No register left? */
719           if (argreg > FLOAT_ARGLAST_REGNUM - FLOAT_ARG0_REGNUM)
720             return FLOAT_ARGLAST_REGNUM + 1;
721         }
722       /* Also mark the next register as used. */
723       flt_argreg_array[argreg + 1] = 1;
724     }
725   else if (TARGET_BYTE_ORDER == BFD_ENDIAN_LITTLE)
726     {
727       /* In little endian, gcc passes floats like this: f5, f4, f7, f6, ... */
728       if (!flt_argreg_array[argreg + 1])
729         ++argreg;
730     }
731   flt_argreg_array[argreg] = 1;
732   return FLOAT_ARG0_REGNUM + argreg;
733 }
734
735 static CORE_ADDR
736 sh_push_dummy_call_fpu (struct gdbarch *gdbarch,
737                         CORE_ADDR func_addr,
738                         struct regcache *regcache,
739                         CORE_ADDR bp_addr, int nargs,
740                         struct value **args,
741                         CORE_ADDR sp, int struct_return,
742                         CORE_ADDR struct_addr)
743 {
744   int stack_offset = 0;
745   int argreg = ARG0_REGNUM;
746   int flt_argreg = 0;
747   int argnum;
748   struct type *type;
749   CORE_ADDR regval;
750   char *val;
751   int len, reg_size = 0;
752   int pass_on_stack;
753
754   /* first force sp to a 4-byte alignment */
755   sp = sh_frame_align (gdbarch, sp);
756
757   if (struct_return)
758     regcache_cooked_write_unsigned (regcache,
759                                     STRUCT_RETURN_REGNUM, struct_addr);
760
761   /* make room on stack for args */
762   sp -= sh_stack_allocsize (nargs, args);
763
764   /* Initialize float argument mechanism. */
765   sh_init_flt_argreg ();
766
767   /* Now load as many as possible of the first arguments into
768      registers, and push the rest onto the stack.  There are 16 bytes
769      in four registers available.  Loop thru args from first to last.  */
770   for (argnum = 0; argnum < nargs; argnum++)
771     {
772       type = VALUE_TYPE (args[argnum]);
773       len = TYPE_LENGTH (type);
774       val = sh_justify_value_in_reg (args[argnum], len);
775
776       /* Some decisions have to be made how various types are handled.
777          This also differs in different ABIs. */
778       pass_on_stack = 0;
779       if (len > 16)
780         pass_on_stack = 1;      /* Types bigger than 16 bytes are passed on stack. */
781
782       /* Find out the next register to use for a floating point value. */
783       if (TYPE_CODE (type) == TYPE_CODE_FLT)
784         flt_argreg = sh_next_flt_argreg (len);
785
786       while (len > 0)
787         {
788           if ((TYPE_CODE (type) == TYPE_CODE_FLT
789                && flt_argreg > FLOAT_ARGLAST_REGNUM)
790               || argreg > ARGLAST_REGNUM || pass_on_stack)
791             {
792               /* The remainder of the data goes entirely on the stack,
793                  4-byte aligned. */
794               reg_size = (len + 3) & ~3;
795               write_memory (sp + stack_offset, val, reg_size);
796               stack_offset += reg_size;
797             }
798           else if (TYPE_CODE (type) == TYPE_CODE_FLT
799                    && flt_argreg <= FLOAT_ARGLAST_REGNUM)
800             {
801               /* Argument goes in a float argument register.  */
802               reg_size = register_size (gdbarch, flt_argreg);
803               regval = extract_unsigned_integer (val, reg_size);
804               regcache_cooked_write_unsigned (regcache, flt_argreg++, regval);
805             }
806           else if (argreg <= ARGLAST_REGNUM)
807             {
808               /* there's room in a register */
809               reg_size = register_size (gdbarch, argreg);
810               regval = extract_unsigned_integer (val, reg_size);
811               regcache_cooked_write_unsigned (regcache, argreg++, regval);
812             }
813           /* Store the value reg_size bytes at a time.  This means that things
814              larger than reg_size bytes may go partly in registers and partly
815              on the stack.  */
816           len -= reg_size;
817           val += reg_size;
818         }
819     }
820
821   /* Store return address. */
822   regcache_cooked_write_unsigned (regcache, PR_REGNUM, bp_addr);
823
824   /* Update stack pointer.  */
825   regcache_cooked_write_unsigned (regcache, SP_REGNUM, sp);
826
827   return sp;
828 }
829
830 static CORE_ADDR
831 sh_push_dummy_call_nofpu (struct gdbarch *gdbarch,
832                           CORE_ADDR func_addr,
833                           struct regcache *regcache,
834                           CORE_ADDR bp_addr,
835                           int nargs, struct value **args,
836                           CORE_ADDR sp, int struct_return,
837                           CORE_ADDR struct_addr)
838 {
839   int stack_offset = 0;
840   int argreg = ARG0_REGNUM;
841   int argnum;
842   struct type *type;
843   CORE_ADDR regval;
844   char *val;
845   int len, reg_size;
846
847   /* first force sp to a 4-byte alignment */
848   sp = sh_frame_align (gdbarch, sp);
849
850   if (struct_return)
851     regcache_cooked_write_unsigned (regcache,
852                                     STRUCT_RETURN_REGNUM, struct_addr);
853
854   /* make room on stack for args */
855   sp -= sh_stack_allocsize (nargs, args);
856
857   /* Now load as many as possible of the first arguments into
858      registers, and push the rest onto the stack.  There are 16 bytes
859      in four registers available.  Loop thru args from first to last.  */
860   for (argnum = 0; argnum < nargs; argnum++)
861     {
862       type = VALUE_TYPE (args[argnum]);
863       len = TYPE_LENGTH (type);
864       val = sh_justify_value_in_reg (args[argnum], len);
865
866       while (len > 0)
867         {
868           if (argreg > ARGLAST_REGNUM)
869             {
870               /* The remainder of the data goes entirely on the stack,
871                  4-byte aligned. */
872               reg_size = (len + 3) & ~3;
873               write_memory (sp + stack_offset, val, reg_size);
874               stack_offset += reg_size;
875             }
876           else if (argreg <= ARGLAST_REGNUM)
877             {
878               /* there's room in a register */
879               reg_size = register_size (gdbarch, argreg);
880               regval = extract_unsigned_integer (val, reg_size);
881               regcache_cooked_write_unsigned (regcache, argreg++, regval);
882             }
883           /* Store the value reg_size bytes at a time.  This means that things
884              larger than reg_size bytes may go partly in registers and partly
885              on the stack.  */
886           len -= reg_size;
887           val += reg_size;
888         }
889     }
890
891   /* Store return address. */
892   regcache_cooked_write_unsigned (regcache, PR_REGNUM, bp_addr);
893
894   /* Update stack pointer.  */
895   regcache_cooked_write_unsigned (regcache, SP_REGNUM, sp);
896
897   return sp;
898 }
899
900 /* Find a function's return value in the appropriate registers (in
901    regbuf), and copy it into valbuf.  Extract from an array REGBUF
902    containing the (raw) register state a function return value of type
903    TYPE, and copy that, in virtual format, into VALBUF.  */
904 static void
905 sh_default_extract_return_value (struct type *type, struct regcache *regcache,
906                                  void *valbuf)
907 {
908   int len = TYPE_LENGTH (type);
909   int return_register = R0_REGNUM;
910   int offset;
911
912   if (len <= 4)
913     {
914       ULONGEST c;
915
916       regcache_cooked_read_unsigned (regcache, R0_REGNUM, &c);
917       store_unsigned_integer (valbuf, len, c);
918     }
919   else if (len == 8)
920     {
921       int i, regnum = R0_REGNUM;
922       for (i = 0; i < len; i += 4)
923         regcache_raw_read (regcache, regnum++, (char *) valbuf + i);
924     }
925   else
926     error ("bad size for return value");
927 }
928
929 static void
930 sh3e_sh4_extract_return_value (struct type *type, struct regcache *regcache,
931                                void *valbuf)
932 {
933   if (TYPE_CODE (type) == TYPE_CODE_FLT)
934     {
935       int len = TYPE_LENGTH (type);
936       int i, regnum = FP0_REGNUM;
937       for (i = 0; i < len; i += 4)
938         regcache_raw_read (regcache, regnum++, (char *) valbuf + i);
939     }
940   else
941     sh_default_extract_return_value (type, regcache, valbuf);
942 }
943
944 /* Write into appropriate registers a function return value
945    of type TYPE, given in virtual format.
946    If the architecture is sh4 or sh3e, store a function's return value
947    in the R0 general register or in the FP0 floating point register,
948    depending on the type of the return value. In all the other cases
949    the result is stored in r0, left-justified. */
950 static void
951 sh_default_store_return_value (struct type *type, struct regcache *regcache,
952                                const void *valbuf)
953 {
954   ULONGEST val;
955   int len = TYPE_LENGTH (type);
956
957   if (len <= 4)
958     {
959       val = extract_unsigned_integer (valbuf, len);
960       regcache_cooked_write_unsigned (regcache, R0_REGNUM, val);
961     }
962   else
963     {
964       int i, regnum = R0_REGNUM;
965       for (i = 0; i < len; i += 4)
966         regcache_raw_write (regcache, regnum++, (char *) valbuf + i);
967     }
968 }
969
970 static void
971 sh3e_sh4_store_return_value (struct type *type, struct regcache *regcache,
972                              const void *valbuf)
973 {
974   if (TYPE_CODE (type) == TYPE_CODE_FLT)
975     {
976       int len = TYPE_LENGTH (type);
977       int i, regnum = FP0_REGNUM;
978       for (i = 0; i < len; i += 4)
979         regcache_raw_write (regcache, regnum++, (char *) valbuf + i);
980     }
981   else
982     sh_default_store_return_value (type, regcache, valbuf);
983 }
984
985 /* Print the registers in a form similar to the E7000 */
986
987 static void
988 sh_generic_show_regs (void)
989 {
990   printf_filtered ("PC=%s SR=%08lx PR=%08lx MACH=%08lx MACHL=%08lx\n",
991                    paddr (read_register (PC_REGNUM)),
992                    (long) read_register (SR_REGNUM),
993                    (long) read_register (PR_REGNUM),
994                    (long) read_register (MACH_REGNUM),
995                    (long) read_register (MACL_REGNUM));
996
997   printf_filtered ("GBR=%08lx VBR=%08lx",
998                    (long) read_register (GBR_REGNUM),
999                    (long) read_register (VBR_REGNUM));
1000
1001   printf_filtered
1002     ("\nR0-R7  %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
1003      (long) read_register (0), (long) read_register (1),
1004      (long) read_register (2), (long) read_register (3),
1005      (long) read_register (4), (long) read_register (5),
1006      (long) read_register (6), (long) read_register (7));
1007   printf_filtered ("R8-R15 %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
1008                    (long) read_register (8), (long) read_register (9),
1009                    (long) read_register (10), (long) read_register (11),
1010                    (long) read_register (12), (long) read_register (13),
1011                    (long) read_register (14), (long) read_register (15));
1012 }
1013
1014 static void
1015 sh3_show_regs (void)
1016 {
1017   printf_filtered ("PC=%s SR=%08lx PR=%08lx MACH=%08lx MACHL=%08lx\n",
1018                    paddr (read_register (PC_REGNUM)),
1019                    (long) read_register (SR_REGNUM),
1020                    (long) read_register (PR_REGNUM),
1021                    (long) read_register (MACH_REGNUM),
1022                    (long) read_register (MACL_REGNUM));
1023
1024   printf_filtered ("GBR=%08lx VBR=%08lx",
1025                    (long) read_register (GBR_REGNUM),
1026                    (long) read_register (VBR_REGNUM));
1027   printf_filtered (" SSR=%08lx SPC=%08lx",
1028                    (long) read_register (SSR_REGNUM),
1029                    (long) read_register (SPC_REGNUM));
1030
1031   printf_filtered
1032     ("\nR0-R7  %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
1033      (long) read_register (0), (long) read_register (1),
1034      (long) read_register (2), (long) read_register (3),
1035      (long) read_register (4), (long) read_register (5),
1036      (long) read_register (6), (long) read_register (7));
1037   printf_filtered ("R8-R15 %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
1038                    (long) read_register (8), (long) read_register (9),
1039                    (long) read_register (10), (long) read_register (11),
1040                    (long) read_register (12), (long) read_register (13),
1041                    (long) read_register (14), (long) read_register (15));
1042 }
1043
1044
1045 static void
1046 sh2e_show_regs (void)
1047 {
1048   printf_filtered ("PC=%s SR=%08lx PR=%08lx MACH=%08lx MACHL=%08lx\n",
1049                    paddr (read_register (PC_REGNUM)),
1050                    (long) read_register (SR_REGNUM),
1051                    (long) read_register (PR_REGNUM),
1052                    (long) read_register (MACH_REGNUM),
1053                    (long) read_register (MACL_REGNUM));
1054
1055   printf_filtered ("GBR=%08lx VBR=%08lx",
1056                    (long) read_register (GBR_REGNUM),
1057                    (long) read_register (VBR_REGNUM));
1058   printf_filtered (" FPUL=%08lx FPSCR=%08lx",
1059                    (long) read_register (FPUL_REGNUM),
1060                    (long) read_register (FPSCR_REGNUM));
1061
1062   printf_filtered
1063     ("\nR0-R7  %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
1064      (long) read_register (0), (long) read_register (1),
1065      (long) read_register (2), (long) read_register (3),
1066      (long) read_register (4), (long) read_register (5),
1067      (long) read_register (6), (long) read_register (7));
1068   printf_filtered ("R8-R15 %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
1069                    (long) read_register (8), (long) read_register (9),
1070                    (long) read_register (10), (long) read_register (11),
1071                    (long) read_register (12), (long) read_register (13),
1072                    (long) read_register (14), (long) read_register (15));
1073
1074   printf_filtered (("FP0-FP7  %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n"), (long) read_register (FP0_REGNUM + 0), (long) read_register (FP0_REGNUM + 1), (long) read_register (FP0_REGNUM + 2), (long) read_register (FP0_REGNUM + 3), (long) read_register (FP0_REGNUM + 4), (long) read_register (FP0_REGNUM + 5), (long) read_register (FP0_REGNUM + 6), (long) read_register (FP0_REGNUM + 7));
1075   printf_filtered (("FP8-FP15 %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n"), (long) read_register (FP0_REGNUM + 8), (long) read_register (FP0_REGNUM + 9), (long) read_register (FP0_REGNUM + 10), (long) read_register (FP0_REGNUM + 11), (long) read_register (FP0_REGNUM + 12), (long) read_register (FP0_REGNUM + 13), (long) read_register (FP0_REGNUM + 14), (long) read_register (FP0_REGNUM + 15));
1076 }
1077
1078 static void
1079 sh3e_show_regs (void)
1080 {
1081   printf_filtered ("PC=%s SR=%08lx PR=%08lx MACH=%08lx MACHL=%08lx\n",
1082                    paddr (read_register (PC_REGNUM)),
1083                    (long) read_register (SR_REGNUM),
1084                    (long) read_register (PR_REGNUM),
1085                    (long) read_register (MACH_REGNUM),
1086                    (long) read_register (MACL_REGNUM));
1087
1088   printf_filtered ("GBR=%08lx VBR=%08lx",
1089                    (long) read_register (GBR_REGNUM),
1090                    (long) read_register (VBR_REGNUM));
1091   printf_filtered (" SSR=%08lx SPC=%08lx",
1092                    (long) read_register (SSR_REGNUM),
1093                    (long) read_register (SPC_REGNUM));
1094   printf_filtered (" FPUL=%08lx FPSCR=%08lx",
1095                    (long) read_register (FPUL_REGNUM),
1096                    (long) read_register (FPSCR_REGNUM));
1097
1098   printf_filtered
1099     ("\nR0-R7  %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
1100      (long) read_register (0), (long) read_register (1),
1101      (long) read_register (2), (long) read_register (3),
1102      (long) read_register (4), (long) read_register (5),
1103      (long) read_register (6), (long) read_register (7));
1104   printf_filtered ("R8-R15 %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
1105                    (long) read_register (8), (long) read_register (9),
1106                    (long) read_register (10), (long) read_register (11),
1107                    (long) read_register (12), (long) read_register (13),
1108                    (long) read_register (14), (long) read_register (15));
1109
1110   printf_filtered (("FP0-FP7  %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n"), (long) read_register (FP0_REGNUM + 0), (long) read_register (FP0_REGNUM + 1), (long) read_register (FP0_REGNUM + 2), (long) read_register (FP0_REGNUM + 3), (long) read_register (FP0_REGNUM + 4), (long) read_register (FP0_REGNUM + 5), (long) read_register (FP0_REGNUM + 6), (long) read_register (FP0_REGNUM + 7));
1111   printf_filtered (("FP8-FP15 %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n"), (long) read_register (FP0_REGNUM + 8), (long) read_register (FP0_REGNUM + 9), (long) read_register (FP0_REGNUM + 10), (long) read_register (FP0_REGNUM + 11), (long) read_register (FP0_REGNUM + 12), (long) read_register (FP0_REGNUM + 13), (long) read_register (FP0_REGNUM + 14), (long) read_register (FP0_REGNUM + 15));
1112 }
1113
1114 static void
1115 sh3_dsp_show_regs (void)
1116 {
1117   printf_filtered ("PC=%s SR=%08lx PR=%08lx MACH=%08lx MACHL=%08lx\n",
1118                    paddr (read_register (PC_REGNUM)),
1119                    (long) read_register (SR_REGNUM),
1120                    (long) read_register (PR_REGNUM),
1121                    (long) read_register (MACH_REGNUM),
1122                    (long) read_register (MACL_REGNUM));
1123
1124   printf_filtered ("GBR=%08lx VBR=%08lx",
1125                    (long) read_register (GBR_REGNUM),
1126                    (long) read_register (VBR_REGNUM));
1127
1128   printf_filtered (" SSR=%08lx SPC=%08lx",
1129                    (long) read_register (SSR_REGNUM),
1130                    (long) read_register (SPC_REGNUM));
1131
1132   printf_filtered (" DSR=%08lx", (long) read_register (DSR_REGNUM));
1133
1134   printf_filtered
1135     ("\nR0-R7  %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
1136      (long) read_register (0), (long) read_register (1),
1137      (long) read_register (2), (long) read_register (3),
1138      (long) read_register (4), (long) read_register (5),
1139      (long) read_register (6), (long) read_register (7));
1140   printf_filtered ("R8-R15 %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
1141                    (long) read_register (8), (long) read_register (9),
1142                    (long) read_register (10), (long) read_register (11),
1143                    (long) read_register (12), (long) read_register (13),
1144                    (long) read_register (14), (long) read_register (15));
1145
1146   printf_filtered
1147     ("A0G=%02lx A0=%08lx M0=%08lx X0=%08lx Y0=%08lx RS=%08lx MOD=%08lx\n",
1148      (long) read_register (A0G_REGNUM) & 0xff,
1149      (long) read_register (A0_REGNUM), (long) read_register (M0_REGNUM),
1150      (long) read_register (X0_REGNUM), (long) read_register (Y0_REGNUM),
1151      (long) read_register (RS_REGNUM), (long) read_register (MOD_REGNUM));
1152   printf_filtered ("A1G=%02lx A1=%08lx M1=%08lx X1=%08lx Y1=%08lx RE=%08lx\n",
1153                    (long) read_register (A1G_REGNUM) & 0xff,
1154                    (long) read_register (A1_REGNUM),
1155                    (long) read_register (M1_REGNUM),
1156                    (long) read_register (X1_REGNUM),
1157                    (long) read_register (Y1_REGNUM),
1158                    (long) read_register (RE_REGNUM));
1159 }
1160
1161 static void
1162 sh4_show_regs (void)
1163 {
1164   int pr = read_register (FPSCR_REGNUM) & 0x80000;
1165   printf_filtered ("PC=%s SR=%08lx PR=%08lx MACH=%08lx MACHL=%08lx\n",
1166                    paddr (read_register (PC_REGNUM)),
1167                    (long) read_register (SR_REGNUM),
1168                    (long) read_register (PR_REGNUM),
1169                    (long) read_register (MACH_REGNUM),
1170                    (long) read_register (MACL_REGNUM));
1171
1172   printf_filtered ("GBR=%08lx VBR=%08lx",
1173                    (long) read_register (GBR_REGNUM),
1174                    (long) read_register (VBR_REGNUM));
1175   printf_filtered (" SSR=%08lx SPC=%08lx",
1176                    (long) read_register (SSR_REGNUM),
1177                    (long) read_register (SPC_REGNUM));
1178   printf_filtered (" FPUL=%08lx FPSCR=%08lx",
1179                    (long) read_register (FPUL_REGNUM),
1180                    (long) read_register (FPSCR_REGNUM));
1181
1182   printf_filtered
1183     ("\nR0-R7  %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
1184      (long) read_register (0), (long) read_register (1),
1185      (long) read_register (2), (long) read_register (3),
1186      (long) read_register (4), (long) read_register (5),
1187      (long) read_register (6), (long) read_register (7));
1188   printf_filtered ("R8-R15 %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
1189                    (long) read_register (8), (long) read_register (9),
1190                    (long) read_register (10), (long) read_register (11),
1191                    (long) read_register (12), (long) read_register (13),
1192                    (long) read_register (14), (long) read_register (15));
1193
1194   printf_filtered ((pr
1195                     ? "DR0-DR6  %08lx%08lx %08lx%08lx %08lx%08lx %08lx%08lx\n"
1196                     :
1197                     "FP0-FP7  %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n"),
1198                    (long) read_register (FP0_REGNUM + 0),
1199                    (long) read_register (FP0_REGNUM + 1),
1200                    (long) read_register (FP0_REGNUM + 2),
1201                    (long) read_register (FP0_REGNUM + 3),
1202                    (long) read_register (FP0_REGNUM + 4),
1203                    (long) read_register (FP0_REGNUM + 5),
1204                    (long) read_register (FP0_REGNUM + 6),
1205                    (long) read_register (FP0_REGNUM + 7));
1206   printf_filtered ((pr ?
1207                     "DR8-DR14 %08lx%08lx %08lx%08lx %08lx%08lx %08lx%08lx\n" :
1208                     "FP8-FP15 %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n"),
1209                    (long) read_register (FP0_REGNUM + 8),
1210                    (long) read_register (FP0_REGNUM + 9),
1211                    (long) read_register (FP0_REGNUM + 10),
1212                    (long) read_register (FP0_REGNUM + 11),
1213                    (long) read_register (FP0_REGNUM + 12),
1214                    (long) read_register (FP0_REGNUM + 13),
1215                    (long) read_register (FP0_REGNUM + 14),
1216                    (long) read_register (FP0_REGNUM + 15));
1217 }
1218
1219 static void
1220 sh_dsp_show_regs (void)
1221 {
1222   printf_filtered ("PC=%s SR=%08lx PR=%08lx MACH=%08lx MACHL=%08lx\n",
1223                    paddr (read_register (PC_REGNUM)),
1224                    (long) read_register (SR_REGNUM),
1225                    (long) read_register (PR_REGNUM),
1226                    (long) read_register (MACH_REGNUM),
1227                    (long) read_register (MACL_REGNUM));
1228
1229   printf_filtered ("GBR=%08lx VBR=%08lx",
1230                    (long) read_register (GBR_REGNUM),
1231                    (long) read_register (VBR_REGNUM));
1232
1233   printf_filtered (" DSR=%08lx", (long) read_register (DSR_REGNUM));
1234
1235   printf_filtered
1236     ("\nR0-R7  %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
1237      (long) read_register (0), (long) read_register (1),
1238      (long) read_register (2), (long) read_register (3),
1239      (long) read_register (4), (long) read_register (5),
1240      (long) read_register (6), (long) read_register (7));
1241   printf_filtered ("R8-R15 %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx\n",
1242                    (long) read_register (8), (long) read_register (9),
1243                    (long) read_register (10), (long) read_register (11),
1244                    (long) read_register (12), (long) read_register (13),
1245                    (long) read_register (14), (long) read_register (15));
1246
1247   printf_filtered
1248     ("A0G=%02lx A0=%08lx M0=%08lx X0=%08lx Y0=%08lx RS=%08lx MOD=%08lx\n",
1249      (long) read_register (A0G_REGNUM) & 0xff,
1250      (long) read_register (A0_REGNUM), (long) read_register (M0_REGNUM),
1251      (long) read_register (X0_REGNUM), (long) read_register (Y0_REGNUM),
1252      (long) read_register (RS_REGNUM), (long) read_register (MOD_REGNUM));
1253   printf_filtered ("A1G=%02lx A1=%08lx M1=%08lx X1=%08lx Y1=%08lx RE=%08lx\n",
1254                    (long) read_register (A1G_REGNUM) & 0xff,
1255                    (long) read_register (A1_REGNUM),
1256                    (long) read_register (M1_REGNUM),
1257                    (long) read_register (X1_REGNUM),
1258                    (long) read_register (Y1_REGNUM),
1259                    (long) read_register (RE_REGNUM));
1260 }
1261
1262 static void
1263 sh_show_regs_command (char *args, int from_tty)
1264 {
1265   if (sh_show_regs)
1266     (*sh_show_regs) ();
1267 }
1268
1269 /* Return the GDB type object for the "standard" data type
1270    of data in register N.  */
1271 static struct type *
1272 sh_sh3e_register_type (struct gdbarch *gdbarch, int reg_nr)
1273 {
1274   if ((reg_nr >= FP0_REGNUM
1275        && (reg_nr <= FP_LAST_REGNUM)) || (reg_nr == FPUL_REGNUM))
1276     return builtin_type_float;
1277   else
1278     return builtin_type_int;
1279 }
1280
1281 static struct type *
1282 sh_sh4_build_float_register_type (int high)
1283 {
1284   struct type *temp;
1285
1286   temp = create_range_type (NULL, builtin_type_int, 0, high);
1287   return create_array_type (NULL, builtin_type_float, temp);
1288 }
1289
1290 static struct type *
1291 sh_sh4_register_type (struct gdbarch *gdbarch, int reg_nr)
1292 {
1293   if ((reg_nr >= FP0_REGNUM
1294        && (reg_nr <= FP_LAST_REGNUM)) || (reg_nr == FPUL_REGNUM))
1295     return builtin_type_float;
1296   else if (reg_nr >= DR0_REGNUM && reg_nr <= DR_LAST_REGNUM)
1297     return builtin_type_double;
1298   else if (reg_nr >= FV0_REGNUM && reg_nr <= FV_LAST_REGNUM)
1299     return sh_sh4_build_float_register_type (3);
1300   else
1301     return builtin_type_int;
1302 }
1303
1304 static struct type *
1305 sh_default_register_type (struct gdbarch *gdbarch, int reg_nr)
1306 {
1307   return builtin_type_int;
1308 }
1309
1310 /* On the sh4, the DRi pseudo registers are problematic if the target
1311    is little endian. When the user writes one of those registers, for
1312    instance with 'ser var $dr0=1', we want the double to be stored
1313    like this: 
1314    fr0 = 0x00 0x00 0x00 0x00 0x00 0xf0 0x3f 
1315    fr1 = 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
1316
1317    This corresponds to little endian byte order & big endian word
1318    order.  However if we let gdb write the register w/o conversion, it
1319    will write fr0 and fr1 this way:
1320    fr0 = 0x00 0x00 0x00 0x00 0x00 0x00 0x00
1321    fr1 = 0x00 0x00 0x00 0x00 0x00 0xf0 0x3f
1322    because it will consider fr0 and fr1 as a single LE stretch of memory.
1323    
1324    To achieve what we want we must force gdb to store things in
1325    floatformat_ieee_double_littlebyte_bigword (which is defined in
1326    include/floatformat.h and libiberty/floatformat.c.
1327
1328    In case the target is big endian, there is no problem, the
1329    raw bytes will look like:
1330    fr0 = 0x3f 0xf0 0x00 0x00 0x00 0x00 0x00
1331    fr1 = 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
1332
1333    The other pseudo registers (the FVs) also don't pose a problem
1334    because they are stored as 4 individual FP elements. */
1335
1336 static void
1337 sh_sh4_register_convert_to_virtual (int regnum, struct type *type,
1338                                     char *from, char *to)
1339 {
1340   if (regnum >= DR0_REGNUM && regnum <= DR_LAST_REGNUM)
1341     {
1342       DOUBLEST val;
1343       floatformat_to_doublest (&floatformat_ieee_double_littlebyte_bigword,
1344                                from, &val);
1345       store_typed_floating (to, type, val);
1346     }
1347   else
1348     error
1349       ("sh_register_convert_to_virtual called with non DR register number");
1350 }
1351
1352 static void
1353 sh_sh4_register_convert_to_raw (struct type *type, int regnum,
1354                                 const void *from, void *to)
1355 {
1356   if (regnum >= DR0_REGNUM && regnum <= DR_LAST_REGNUM)
1357     {
1358       DOUBLEST val = extract_typed_floating (from, type);
1359       floatformat_from_doublest (&floatformat_ieee_double_littlebyte_bigword,
1360                                  &val, to);
1361     }
1362   else
1363     error ("sh_register_convert_to_raw called with non DR register number");
1364 }
1365
1366 /* For vectors of 4 floating point registers. */
1367 static int
1368 fv_reg_base_num (int fv_regnum)
1369 {
1370   int fp_regnum;
1371
1372   fp_regnum = FP0_REGNUM + (fv_regnum - FV0_REGNUM) * 4;
1373   return fp_regnum;
1374 }
1375
1376 /* For double precision floating point registers, i.e 2 fp regs.*/
1377 static int
1378 dr_reg_base_num (int dr_regnum)
1379 {
1380   int fp_regnum;
1381
1382   fp_regnum = FP0_REGNUM + (dr_regnum - DR0_REGNUM) * 2;
1383   return fp_regnum;
1384 }
1385
1386 static void
1387 sh_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
1388                          int reg_nr, void *buffer)
1389 {
1390   int base_regnum, portion;
1391   char temp_buffer[MAX_REGISTER_SIZE];
1392
1393   if (reg_nr >= DR0_REGNUM && reg_nr <= DR_LAST_REGNUM)
1394     {
1395       base_regnum = dr_reg_base_num (reg_nr);
1396
1397       /* Build the value in the provided buffer. */
1398       /* Read the real regs for which this one is an alias.  */
1399       for (portion = 0; portion < 2; portion++)
1400         regcache_raw_read (regcache, base_regnum + portion,
1401                            (temp_buffer
1402                             + register_size (gdbarch,
1403                                              base_regnum) * portion));
1404       /* We must pay attention to the endiannes. */
1405       sh_sh4_register_convert_to_virtual (reg_nr,
1406                                           gdbarch_register_type (gdbarch,
1407                                                                  reg_nr),
1408                                           temp_buffer, buffer);
1409     }
1410   else if (reg_nr >= FV0_REGNUM && reg_nr <= FV_LAST_REGNUM)
1411     {
1412       base_regnum = fv_reg_base_num (reg_nr);
1413
1414       /* Read the real regs for which this one is an alias.  */
1415       for (portion = 0; portion < 4; portion++)
1416         regcache_raw_read (regcache, base_regnum + portion,
1417                            ((char *) buffer
1418                             + register_size (gdbarch,
1419                                              base_regnum) * portion));
1420     }
1421 }
1422
1423 static void
1424 sh_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
1425                           int reg_nr, const void *buffer)
1426 {
1427   int base_regnum, portion;
1428   char temp_buffer[MAX_REGISTER_SIZE];
1429
1430   if (reg_nr >= DR0_REGNUM && reg_nr <= DR_LAST_REGNUM)
1431     {
1432       base_regnum = dr_reg_base_num (reg_nr);
1433
1434       /* We must pay attention to the endiannes. */
1435       sh_sh4_register_convert_to_raw (gdbarch_register_type (gdbarch, reg_nr),
1436                                       reg_nr, buffer, temp_buffer);
1437
1438       /* Write the real regs for which this one is an alias.  */
1439       for (portion = 0; portion < 2; portion++)
1440         regcache_raw_write (regcache, base_regnum + portion,
1441                             (temp_buffer
1442                              + register_size (gdbarch,
1443                                               base_regnum) * portion));
1444     }
1445   else if (reg_nr >= FV0_REGNUM && reg_nr <= FV_LAST_REGNUM)
1446     {
1447       base_regnum = fv_reg_base_num (reg_nr);
1448
1449       /* Write the real regs for which this one is an alias.  */
1450       for (portion = 0; portion < 4; portion++)
1451         regcache_raw_write (regcache, base_regnum + portion,
1452                             ((char *) buffer
1453                              + register_size (gdbarch,
1454                                               base_regnum) * portion));
1455     }
1456 }
1457
1458 /* Floating point vector of 4 float registers. */
1459 static void
1460 do_fv_register_info (struct gdbarch *gdbarch, struct ui_file *file,
1461                      int fv_regnum)
1462 {
1463   int first_fp_reg_num = fv_reg_base_num (fv_regnum);
1464   fprintf_filtered (file, "fv%d\t0x%08x\t0x%08x\t0x%08x\t0x%08x\n",
1465                     fv_regnum - FV0_REGNUM,
1466                     (int) read_register (first_fp_reg_num),
1467                     (int) read_register (first_fp_reg_num + 1),
1468                     (int) read_register (first_fp_reg_num + 2),
1469                     (int) read_register (first_fp_reg_num + 3));
1470 }
1471
1472 /* Double precision registers. */
1473 static void
1474 do_dr_register_info (struct gdbarch *gdbarch, struct ui_file *file,
1475                      int dr_regnum)
1476 {
1477   int first_fp_reg_num = dr_reg_base_num (dr_regnum);
1478
1479   fprintf_filtered (file, "dr%d\t0x%08x%08x\n",
1480                     dr_regnum - DR0_REGNUM,
1481                     (int) read_register (first_fp_reg_num),
1482                     (int) read_register (first_fp_reg_num + 1));
1483 }
1484
1485 static void
1486 sh_print_pseudo_register (struct gdbarch *gdbarch, struct ui_file *file,
1487                           int regnum)
1488 {
1489   if (regnum < NUM_REGS || regnum >= NUM_REGS + NUM_PSEUDO_REGS)
1490     internal_error (__FILE__, __LINE__,
1491                     "Invalid pseudo register number %d\n", regnum);
1492   else if (regnum >= DR0_REGNUM && regnum <= DR_LAST_REGNUM)
1493     do_dr_register_info (gdbarch, file, regnum);
1494   else if (regnum >= FV0_REGNUM && regnum <= FV_LAST_REGNUM)
1495     do_fv_register_info (gdbarch, file, regnum);
1496 }
1497
1498 static void
1499 sh_do_fp_register (struct gdbarch *gdbarch, struct ui_file *file, int regnum)
1500 {                               /* do values for FP (float) regs */
1501   char *raw_buffer;
1502   double flt;                   /* double extracted from raw hex data */
1503   int inv;
1504   int j;
1505
1506   /* Allocate space for the float. */
1507   raw_buffer = (char *) alloca (register_size (gdbarch, FP0_REGNUM));
1508
1509   /* Get the data in raw format.  */
1510   if (!frame_register_read (get_selected_frame (), regnum, raw_buffer))
1511     error ("can't read register %d (%s)", regnum, REGISTER_NAME (regnum));
1512
1513   /* Get the register as a number */
1514   flt = unpack_double (builtin_type_float, raw_buffer, &inv);
1515
1516   /* Print the name and some spaces. */
1517   fputs_filtered (REGISTER_NAME (regnum), file);
1518   print_spaces_filtered (15 - strlen (REGISTER_NAME (regnum)), file);
1519
1520   /* Print the value. */
1521   if (inv)
1522     fprintf_filtered (file, "<invalid float>");
1523   else
1524     fprintf_filtered (file, "%-10.9g", flt);
1525
1526   /* Print the fp register as hex. */
1527   fprintf_filtered (file, "\t(raw 0x");
1528   for (j = 0; j < register_size (gdbarch, regnum); j++)
1529     {
1530       register int idx = TARGET_BYTE_ORDER == BFD_ENDIAN_BIG ? j
1531         : register_size (gdbarch, regnum) - 1 - j;
1532       fprintf_filtered (file, "%02x", (unsigned char) raw_buffer[idx]);
1533     }
1534   fprintf_filtered (file, ")");
1535   fprintf_filtered (file, "\n");
1536 }
1537
1538 static void
1539 sh_do_register (struct gdbarch *gdbarch, struct ui_file *file, int regnum)
1540 {
1541   char raw_buffer[MAX_REGISTER_SIZE];
1542
1543   fputs_filtered (REGISTER_NAME (regnum), file);
1544   print_spaces_filtered (15 - strlen (REGISTER_NAME (regnum)), file);
1545
1546   /* Get the data in raw format.  */
1547   if (!frame_register_read (get_selected_frame (), regnum, raw_buffer))
1548     fprintf_filtered (file, "*value not available*\n");
1549
1550   val_print (gdbarch_register_type (gdbarch, regnum), raw_buffer, 0, 0,
1551              file, 'x', 1, 0, Val_pretty_default);
1552   fprintf_filtered (file, "\t");
1553   val_print (gdbarch_register_type (gdbarch, regnum), raw_buffer, 0, 0,
1554              file, 0, 1, 0, Val_pretty_default);
1555   fprintf_filtered (file, "\n");
1556 }
1557
1558 static void
1559 sh_print_register (struct gdbarch *gdbarch, struct ui_file *file, int regnum)
1560 {
1561   if (regnum < 0 || regnum >= NUM_REGS + NUM_PSEUDO_REGS)
1562     internal_error (__FILE__, __LINE__,
1563                     "Invalid register number %d\n", regnum);
1564
1565   else if (regnum >= 0 && regnum < NUM_REGS)
1566     {
1567       if (TYPE_CODE (gdbarch_register_type (gdbarch, regnum)) ==
1568           TYPE_CODE_FLT)
1569         sh_do_fp_register (gdbarch, file, regnum);      /* FP regs */
1570       else
1571         sh_do_register (gdbarch, file, regnum); /* All other regs */
1572     }
1573
1574   else if (regnum < NUM_REGS + NUM_PSEUDO_REGS)
1575     {
1576       sh_print_pseudo_register (gdbarch, file, regnum);
1577     }
1578 }
1579
1580 static void
1581 sh_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
1582                          struct frame_info *frame, int regnum, int fpregs)
1583 {
1584   if (regnum != -1)             /* do one specified register */
1585     {
1586       if (*(REGISTER_NAME (regnum)) == '\0')
1587         error ("Not a valid register for the current processor type");
1588
1589       sh_print_register (gdbarch, file, regnum);
1590     }
1591   else
1592     /* do all (or most) registers */
1593     {
1594       regnum = 0;
1595       while (regnum < NUM_REGS)
1596         {
1597           /* If the register name is empty, it is undefined for this
1598              processor, so don't display anything.  */
1599           if (REGISTER_NAME (regnum) == NULL
1600               || *(REGISTER_NAME (regnum)) == '\0')
1601             {
1602               regnum++;
1603               continue;
1604             }
1605
1606           if (TYPE_CODE (gdbarch_register_type (gdbarch, regnum)) ==
1607               TYPE_CODE_FLT)
1608             {
1609               if (fpregs)
1610                 {
1611                   /* true for "INFO ALL-REGISTERS" command */
1612                   sh_do_fp_register (gdbarch, file, regnum);    /* FP regs */
1613                   regnum++;
1614                 }
1615               else
1616                 regnum += (FP_LAST_REGNUM - FP0_REGNUM);        /* skip FP regs */
1617             }
1618           else
1619             {
1620               sh_do_register (gdbarch, file, regnum);   /* All other regs */
1621               regnum++;
1622             }
1623         }
1624
1625       if (fpregs)
1626         while (regnum < NUM_REGS + NUM_PSEUDO_REGS)
1627           {
1628             sh_print_pseudo_register (gdbarch, file, regnum);
1629             regnum++;
1630           }
1631     }
1632 }
1633
1634 #ifdef SVR4_SHARED_LIBS
1635
1636 /* Fetch (and possibly build) an appropriate link_map_offsets structure
1637    for native i386 linux targets using the struct offsets defined in
1638    link.h (but without actual reference to that file).
1639
1640    This makes it possible to access i386-linux shared libraries from
1641    a gdb that was not built on an i386-linux host (for cross debugging).
1642    */
1643
1644 struct link_map_offsets *
1645 sh_linux_svr4_fetch_link_map_offsets (void)
1646 {
1647   static struct link_map_offsets lmo;
1648   static struct link_map_offsets *lmp = 0;
1649
1650   if (lmp == 0)
1651     {
1652       lmp = &lmo;
1653
1654       lmo.r_debug_size = 8;     /* 20 not actual size but all we need */
1655
1656       lmo.r_map_offset = 4;
1657       lmo.r_map_size = 4;
1658
1659       lmo.link_map_size = 20;   /* 552 not actual size but all we need */
1660
1661       lmo.l_addr_offset = 0;
1662       lmo.l_addr_size = 4;
1663
1664       lmo.l_name_offset = 4;
1665       lmo.l_name_size = 4;
1666
1667       lmo.l_next_offset = 12;
1668       lmo.l_next_size = 4;
1669
1670       lmo.l_prev_offset = 16;
1671       lmo.l_prev_size = 4;
1672     }
1673
1674   return lmp;
1675 }
1676 #endif /* SVR4_SHARED_LIBS */
1677
1678 static int
1679 sh_dsp_register_sim_regno (int nr)
1680 {
1681   if (legacy_register_sim_regno (nr) < 0)
1682     return legacy_register_sim_regno (nr);
1683   if (nr >= DSR_REGNUM && nr <= Y1_REGNUM)
1684     return nr - DSR_REGNUM + SIM_SH_DSR_REGNUM;
1685   if (nr == MOD_REGNUM)
1686     return SIM_SH_MOD_REGNUM;
1687   if (nr == RS_REGNUM)
1688     return SIM_SH_RS_REGNUM;
1689   if (nr == RE_REGNUM)
1690     return SIM_SH_RE_REGNUM;
1691   if (nr >= R0_BANK_REGNUM && nr <= R7_BANK_REGNUM)
1692     return nr - R0_BANK_REGNUM + SIM_SH_R0_BANK_REGNUM;
1693   return nr;
1694 }
1695
1696 static struct sh_frame_cache *
1697 sh_alloc_frame_cache (void)
1698 {
1699   struct sh_frame_cache *cache;
1700   int i;
1701
1702   cache = FRAME_OBSTACK_ZALLOC (struct sh_frame_cache);
1703
1704   /* Base address.  */
1705   cache->base = 0;
1706   cache->saved_sp = 0;
1707   cache->sp_offset = 0;
1708   cache->pc = 0;
1709
1710   /* Frameless until proven otherwise.  */
1711   cache->uses_fp = 0;
1712
1713   /* Saved registers.  We initialize these to -1 since zero is a valid
1714      offset (that's where fp is supposed to be stored).  */
1715   for (i = 0; i < SH_NUM_REGS; i++)
1716     {
1717       cache->saved_regs[i] = -1;
1718     }
1719
1720   return cache;
1721 }
1722
1723 static struct sh_frame_cache *
1724 sh_frame_cache (struct frame_info *next_frame, void **this_cache)
1725 {
1726   struct sh_frame_cache *cache;
1727   CORE_ADDR current_pc;
1728   int i;
1729
1730   if (*this_cache)
1731     return *this_cache;
1732
1733   cache = sh_alloc_frame_cache ();
1734   *this_cache = cache;
1735
1736   /* In principle, for normal frames, fp holds the frame pointer,
1737      which holds the base address for the current stack frame.
1738      However, for functions that don't need it, the frame pointer is
1739      optional.  For these "frameless" functions the frame pointer is
1740      actually the frame pointer of the calling frame. */
1741   cache->base = frame_unwind_register_unsigned (next_frame, FP_REGNUM);
1742   if (cache->base == 0)
1743     return cache;
1744
1745   cache->pc = frame_func_unwind (next_frame);
1746   current_pc = frame_pc_unwind (next_frame);
1747   if (cache->pc != 0)
1748     sh_analyze_prologue (cache->pc, current_pc, cache);
1749
1750   if (!cache->uses_fp)
1751     {
1752       /* We didn't find a valid frame, which means that CACHE->base
1753          currently holds the frame pointer for our calling frame.  If
1754          we're at the start of a function, or somewhere half-way its
1755          prologue, the function's frame probably hasn't been fully
1756          setup yet.  Try to reconstruct the base address for the stack
1757          frame by looking at the stack pointer.  For truly "frameless"
1758          functions this might work too.  */
1759       cache->base = frame_unwind_register_unsigned (next_frame, SP_REGNUM);
1760     }
1761
1762   /* Now that we have the base address for the stack frame we can
1763      calculate the value of sp in the calling frame.  */
1764   cache->saved_sp = cache->base + cache->sp_offset;
1765
1766   /* Adjust all the saved registers such that they contain addresses
1767      instead of offsets.  */
1768   for (i = 0; i < SH_NUM_REGS; i++)
1769     if (cache->saved_regs[i] != -1)
1770       cache->saved_regs[i] = cache->saved_sp - cache->saved_regs[i] - 4;
1771
1772   return cache;
1773 }
1774
1775 static void
1776 sh_frame_prev_register (struct frame_info *next_frame, void **this_cache,
1777                         int regnum, int *optimizedp,
1778                         enum lval_type *lvalp, CORE_ADDR *addrp,
1779                         int *realnump, void *valuep)
1780 {
1781   struct sh_frame_cache *cache = sh_frame_cache (next_frame, this_cache);
1782
1783   gdb_assert (regnum >= 0);
1784
1785   if (regnum == SP_REGNUM && cache->saved_sp)
1786     {
1787       *optimizedp = 0;
1788       *lvalp = not_lval;
1789       *addrp = 0;
1790       *realnump = -1;
1791       if (valuep)
1792         {
1793           /* Store the value.  */
1794           store_unsigned_integer (valuep, 4, cache->saved_sp);
1795         }
1796       return;
1797     }
1798
1799   /* The PC of the previous frame is stored in the PR register of
1800      the current frame.  Frob regnum so that we pull the value from
1801      the correct place.  */
1802   if (regnum == PC_REGNUM)
1803     regnum = PR_REGNUM;
1804
1805   if (regnum < SH_NUM_REGS && cache->saved_regs[regnum] != -1)
1806     {
1807       *optimizedp = 0;
1808       *lvalp = lval_memory;
1809       *addrp = cache->saved_regs[regnum];
1810       *realnump = -1;
1811       if (valuep)
1812         {
1813           /* Read the value in from memory.  */
1814           read_memory (*addrp, valuep,
1815                        register_size (current_gdbarch, regnum));
1816         }
1817       return;
1818     }
1819
1820   frame_register_unwind (next_frame, regnum,
1821                          optimizedp, lvalp, addrp, realnump, valuep);
1822 }
1823
1824 static void
1825 sh_frame_this_id (struct frame_info *next_frame, void **this_cache,
1826                   struct frame_id *this_id)
1827 {
1828   struct sh_frame_cache *cache = sh_frame_cache (next_frame, this_cache);
1829
1830   /* This marks the outermost frame.  */
1831   if (cache->base == 0)
1832     return;
1833
1834   *this_id = frame_id_build (cache->saved_sp, cache->pc);
1835 }
1836
1837 static const struct frame_unwind sh_frame_unwind = {
1838   NORMAL_FRAME,
1839   sh_frame_this_id,
1840   sh_frame_prev_register
1841 };
1842
1843 static const struct frame_unwind *
1844 sh_frame_sniffer (struct frame_info *next_frame)
1845 {
1846   return &sh_frame_unwind;
1847 }
1848
1849 static CORE_ADDR
1850 sh_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
1851 {
1852   return frame_unwind_register_unsigned (next_frame, SP_REGNUM);
1853 }
1854
1855 static CORE_ADDR
1856 sh_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
1857 {
1858   return frame_unwind_register_unsigned (next_frame, PC_REGNUM);
1859 }
1860
1861 static struct frame_id
1862 sh_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
1863 {
1864   return frame_id_build (sh_unwind_sp (gdbarch, next_frame),
1865                          frame_pc_unwind (next_frame));
1866 }
1867
1868 static CORE_ADDR
1869 sh_frame_base_address (struct frame_info *next_frame, void **this_cache)
1870 {
1871   struct sh_frame_cache *cache = sh_frame_cache (next_frame, this_cache);
1872
1873   return cache->base;
1874 }
1875
1876 static const struct frame_base sh_frame_base = {
1877   &sh_frame_unwind,
1878   sh_frame_base_address,
1879   sh_frame_base_address,
1880   sh_frame_base_address
1881 };
1882
1883 /* The epilogue is defined here as the area at the end of a function,
1884    either on the `ret' instruction itself or after an instruction which
1885    destroys the function's stack frame. */
1886 static int
1887 sh_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
1888 {
1889   CORE_ADDR func_addr = 0, func_end = 0;
1890
1891   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
1892     {
1893       ULONGEST inst;
1894       /* The sh epilogue is max. 14 bytes long.  Give another 14 bytes
1895          for a nop and some fixed data (e.g. big offsets) which are
1896          unfortunately also treated as part of the function (which
1897          means, they are below func_end. */
1898       CORE_ADDR addr = func_end - 28;
1899       if (addr < func_addr + 4)
1900         addr = func_addr + 4;
1901       if (pc < addr)
1902         return 0;
1903
1904       /* First search forward until hitting an rts. */
1905       while (addr < func_end
1906              && !IS_RTS (read_memory_unsigned_integer (addr, 2)))
1907         addr += 2;
1908       if (addr >= func_end)
1909         return 0;
1910
1911       /* At this point we should find a mov.l @r15+,r14 instruction,
1912          either before or after the rts.  If not, then the function has
1913          probably no "normal" epilogue and we bail out here. */
1914       inst = read_memory_unsigned_integer (addr - 2, 2);
1915       if (IS_RESTORE_FP (read_memory_unsigned_integer (addr - 2, 2)))
1916         addr -= 2;
1917       else if (!IS_RESTORE_FP (read_memory_unsigned_integer (addr + 2, 2)))
1918         return 0;
1919
1920       /* Step over possible lds.l @r15+,pr. */
1921       inst = read_memory_unsigned_integer (addr - 2, 2);
1922       if (IS_LDS (inst))
1923         {
1924           addr -= 2;
1925           inst = read_memory_unsigned_integer (addr - 2, 2);
1926         }
1927
1928       /* Step over possible mov r14,r15. */
1929       if (IS_MOV_FP_SP (inst))
1930         {
1931           addr -= 2;
1932           inst = read_memory_unsigned_integer (addr - 2, 2);
1933         }
1934
1935       /* Now check for FP adjustments, using add #imm,r14 or add rX, r14
1936          instructions. */
1937       while (addr > func_addr + 4
1938              && (IS_ADD_REG_TO_FP (inst) || IS_ADD_IMM_FP (inst)))
1939         {
1940           addr -= 2;
1941           inst = read_memory_unsigned_integer (addr - 2, 2);
1942         }
1943
1944       if (pc >= addr)
1945         return 1;
1946     }
1947   return 0;
1948 }
1949
1950 static gdbarch_init_ftype sh_gdbarch_init;
1951
1952 static struct gdbarch *
1953 sh_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1954 {
1955   struct gdbarch *gdbarch;
1956
1957   sh_show_regs = sh_generic_show_regs;
1958   switch (info.bfd_arch_info->mach)
1959     {
1960     case bfd_mach_sh2e:
1961       sh_show_regs = sh2e_show_regs;
1962       break;
1963     case bfd_mach_sh_dsp:
1964       sh_show_regs = sh_dsp_show_regs;
1965       break;
1966
1967     case bfd_mach_sh3:
1968       sh_show_regs = sh3_show_regs;
1969       break;
1970
1971     case bfd_mach_sh3e:
1972       sh_show_regs = sh3e_show_regs;
1973       break;
1974
1975     case bfd_mach_sh3_dsp:
1976       sh_show_regs = sh3_dsp_show_regs;
1977       break;
1978
1979     case bfd_mach_sh4:
1980       sh_show_regs = sh4_show_regs;
1981       break;
1982
1983     case bfd_mach_sh5:
1984       sh_show_regs = sh64_show_regs;
1985       /* SH5 is handled entirely in sh64-tdep.c */
1986       return sh64_gdbarch_init (info, arches);
1987     }
1988
1989   /* If there is already a candidate, use it.  */
1990   arches = gdbarch_list_lookup_by_info (arches, &info);
1991   if (arches != NULL)
1992     return arches->gdbarch;
1993
1994   /* None found, create a new architecture from the information
1995      provided. */
1996   gdbarch = gdbarch_alloc (&info, NULL);
1997
1998   set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1999   set_gdbarch_int_bit (gdbarch, 4 * TARGET_CHAR_BIT);
2000   set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
2001   set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
2002   set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
2003   set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
2004   set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
2005   set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
2006
2007   set_gdbarch_num_regs (gdbarch, SH_NUM_REGS);
2008   set_gdbarch_sp_regnum (gdbarch, 15);
2009   set_gdbarch_pc_regnum (gdbarch, 16);
2010   set_gdbarch_fp0_regnum (gdbarch, -1);
2011   set_gdbarch_num_pseudo_regs (gdbarch, 0);
2012
2013   set_gdbarch_register_type (gdbarch, sh_default_register_type);
2014
2015   set_gdbarch_print_registers_info (gdbarch, sh_print_registers_info);
2016
2017   set_gdbarch_breakpoint_from_pc (gdbarch, sh_breakpoint_from_pc);
2018   set_gdbarch_use_struct_convention (gdbarch, sh_use_struct_convention);
2019
2020   set_gdbarch_print_insn (gdbarch, gdb_print_insn_sh);
2021   set_gdbarch_register_sim_regno (gdbarch, legacy_register_sim_regno);
2022
2023   set_gdbarch_write_pc (gdbarch, generic_target_write_pc);
2024
2025   set_gdbarch_store_return_value (gdbarch, sh_default_store_return_value);
2026   set_gdbarch_extract_return_value (gdbarch, sh_default_extract_return_value);
2027   set_gdbarch_extract_struct_value_address (gdbarch,
2028                                             sh_extract_struct_value_address);
2029
2030   set_gdbarch_skip_prologue (gdbarch, sh_skip_prologue);
2031   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
2032   set_gdbarch_decr_pc_after_break (gdbarch, 0);
2033   set_gdbarch_function_start_offset (gdbarch, 0);
2034
2035   set_gdbarch_push_dummy_code (gdbarch, sh_push_dummy_code);
2036   set_gdbarch_push_dummy_call (gdbarch, sh_push_dummy_call_nofpu);
2037
2038   set_gdbarch_frame_args_skip (gdbarch, 0);
2039   set_gdbarch_frameless_function_invocation (gdbarch,
2040                                              frameless_look_for_prologue);
2041   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
2042
2043   set_gdbarch_frame_align (gdbarch, sh_frame_align);
2044   set_gdbarch_unwind_sp (gdbarch, sh_unwind_sp);
2045   set_gdbarch_unwind_pc (gdbarch, sh_unwind_pc);
2046   set_gdbarch_unwind_dummy_id (gdbarch, sh_unwind_dummy_id);
2047   frame_base_set_default (gdbarch, &sh_frame_base);
2048
2049   set_gdbarch_in_function_epilogue_p (gdbarch, sh_in_function_epilogue_p);
2050
2051   switch (info.bfd_arch_info->mach)
2052     {
2053     case bfd_mach_sh:
2054       set_gdbarch_register_name (gdbarch, sh_sh_register_name);
2055       break;
2056
2057     case bfd_mach_sh2:
2058       set_gdbarch_register_name (gdbarch, sh_sh_register_name);
2059       break;
2060
2061     case bfd_mach_sh2e:
2062       /* doubles on sh2e and sh3e are actually 4 byte. */
2063       set_gdbarch_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
2064
2065       set_gdbarch_register_name (gdbarch, sh_sh2e_register_name);
2066       set_gdbarch_register_type (gdbarch, sh_sh3e_register_type);
2067       set_gdbarch_fp0_regnum (gdbarch, 25);
2068       set_gdbarch_store_return_value (gdbarch, sh3e_sh4_store_return_value);
2069       set_gdbarch_extract_return_value (gdbarch,
2070                                         sh3e_sh4_extract_return_value);
2071       set_gdbarch_push_dummy_call (gdbarch, sh_push_dummy_call_fpu);
2072       break;
2073
2074     case bfd_mach_sh_dsp:
2075       set_gdbarch_register_name (gdbarch, sh_sh_dsp_register_name);
2076       set_gdbarch_register_sim_regno (gdbarch, sh_dsp_register_sim_regno);
2077       break;
2078
2079     case bfd_mach_sh3:
2080       set_gdbarch_register_name (gdbarch, sh_sh3_register_name);
2081       break;
2082
2083     case bfd_mach_sh3e:
2084       /* doubles on sh2e and sh3e are actually 4 byte. */
2085       set_gdbarch_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
2086
2087       set_gdbarch_register_name (gdbarch, sh_sh3e_register_name);
2088       set_gdbarch_register_type (gdbarch, sh_sh3e_register_type);
2089       set_gdbarch_fp0_regnum (gdbarch, 25);
2090       set_gdbarch_store_return_value (gdbarch, sh3e_sh4_store_return_value);
2091       set_gdbarch_extract_return_value (gdbarch,
2092                                         sh3e_sh4_extract_return_value);
2093       set_gdbarch_push_dummy_call (gdbarch, sh_push_dummy_call_fpu);
2094       break;
2095
2096     case bfd_mach_sh3_dsp:
2097       set_gdbarch_register_name (gdbarch, sh_sh3_dsp_register_name);
2098       set_gdbarch_register_sim_regno (gdbarch, sh_dsp_register_sim_regno);
2099       break;
2100
2101     case bfd_mach_sh4:
2102       set_gdbarch_register_name (gdbarch, sh_sh4_register_name);
2103       set_gdbarch_register_type (gdbarch, sh_sh4_register_type);
2104       set_gdbarch_fp0_regnum (gdbarch, 25);
2105       set_gdbarch_num_pseudo_regs (gdbarch, 12);
2106       set_gdbarch_pseudo_register_read (gdbarch, sh_pseudo_register_read);
2107       set_gdbarch_pseudo_register_write (gdbarch, sh_pseudo_register_write);
2108       set_gdbarch_store_return_value (gdbarch, sh3e_sh4_store_return_value);
2109       set_gdbarch_extract_return_value (gdbarch,
2110                                         sh3e_sh4_extract_return_value);
2111       set_gdbarch_push_dummy_call (gdbarch, sh_push_dummy_call_fpu);
2112       break;
2113
2114     default:
2115       set_gdbarch_register_name (gdbarch, sh_generic_register_name);
2116       break;
2117     }
2118
2119   /* Hook in ABI-specific overrides, if they have been registered.  */
2120   gdbarch_init_osabi (info, gdbarch);
2121
2122   frame_unwind_append_sniffer (gdbarch, dwarf2_frame_sniffer);
2123   frame_unwind_append_sniffer (gdbarch, sh_frame_sniffer);
2124
2125   return gdbarch;
2126 }
2127
2128 extern initialize_file_ftype _initialize_sh_tdep;       /* -Wmissing-prototypes */
2129
2130 void
2131 _initialize_sh_tdep (void)
2132 {
2133   struct cmd_list_element *c;
2134
2135   gdbarch_register (bfd_arch_sh, sh_gdbarch_init, NULL);
2136
2137   add_com ("regs", class_vars, sh_show_regs_command, "Print all registers");
2138 }