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