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