]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/rx-tdep.c
update copyright year range in GDB files
[thirdparty/binutils-gdb.git] / gdb / rx-tdep.c
CommitLineData
baa835b4
KB
1/* Target-dependent code for the Renesas RX for GDB, the GNU debugger.
2
61baf725 3 Copyright (C) 2008-2017 Free Software Foundation, Inc.
baa835b4
KB
4
5 Contributed by Red Hat, Inc.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22#include "defs.h"
23#include "arch-utils.h"
24#include "prologue-value.h"
25#include "target.h"
26#include "regcache.h"
27#include "opcode/rx.h"
28#include "dis-asm.h"
29#include "gdbtypes.h"
30#include "frame.h"
31#include "frame-unwind.h"
32#include "frame-base.h"
33#include "value.h"
34#include "gdbcore.h"
35#include "dwarf2-frame.h"
36
37#include "elf/rx.h"
38#include "elf-bfd.h"
325fac50 39#include <algorithm>
baa835b4
KB
40
41/* Certain important register numbers. */
42enum
43{
44 RX_SP_REGNUM = 0,
45 RX_R1_REGNUM = 1,
46 RX_R4_REGNUM = 4,
47 RX_FP_REGNUM = 6,
48 RX_R15_REGNUM = 15,
1b485e67 49 RX_USP_REGNUM = 16,
fd6e021d 50 RX_PSW_REGNUM = 18,
baa835b4 51 RX_PC_REGNUM = 19,
0561fea4 52 RX_BPSW_REGNUM = 21,
1b485e67 53 RX_BPC_REGNUM = 22,
0561fea4 54 RX_FPSW_REGNUM = 24,
fd60dc69
KB
55 RX_ACC_REGNUM = 25,
56 RX_NUM_REGS = 26
baa835b4
KB
57};
58
1b485e67
KB
59/* RX frame types. */
60enum rx_frame_type {
61 RX_FRAME_TYPE_NORMAL,
62 RX_FRAME_TYPE_EXCEPTION,
63 RX_FRAME_TYPE_FAST_INTERRUPT
64};
65
baa835b4
KB
66/* Architecture specific data. */
67struct gdbarch_tdep
68{
69 /* The ELF header flags specify the multilib used. */
70 int elf_flags;
0561fea4
KB
71
72 /* Type of PSW and BPSW. */
73 struct type *rx_psw_type;
74
75 /* Type of FPSW. */
76 struct type *rx_fpsw_type;
baa835b4
KB
77};
78
79/* This structure holds the results of a prologue analysis. */
80struct rx_prologue
81{
1b485e67
KB
82 /* Frame type, either a normal frame or one of two types of exception
83 frames. */
84 enum rx_frame_type frame_type;
85
baa835b4
KB
86 /* The offset from the frame base to the stack pointer --- always
87 zero or negative.
88
89 Calling this a "size" is a bit misleading, but given that the
90 stack grows downwards, using offsets for everything keeps one
91 from going completely sign-crazy: you never change anything's
92 sign for an ADD instruction; always change the second operand's
93 sign for a SUB instruction; and everything takes care of
94 itself. */
95 int frame_size;
96
97 /* Non-zero if this function has initialized the frame pointer from
98 the stack pointer, zero otherwise. */
99 int has_frame_ptr;
100
101 /* If has_frame_ptr is non-zero, this is the offset from the frame
102 base to where the frame pointer points. This is always zero or
103 negative. */
104 int frame_ptr_offset;
105
106 /* The address of the first instruction at which the frame has been
107 set up and the arguments are where the debug info says they are
108 --- as best as we can tell. */
109 CORE_ADDR prologue_end;
110
111 /* reg_offset[R] is the offset from the CFA at which register R is
112 saved, or 1 if register R has not been saved. (Real values are
113 always zero or negative.) */
114 int reg_offset[RX_NUM_REGS];
115};
116
117/* Implement the "register_name" gdbarch method. */
118static const char *
119rx_register_name (struct gdbarch *gdbarch, int regnr)
120{
121 static const char *const reg_names[] = {
122 "r0",
123 "r1",
124 "r2",
125 "r3",
126 "r4",
127 "r5",
128 "r6",
129 "r7",
130 "r8",
131 "r9",
132 "r10",
133 "r11",
134 "r12",
135 "r13",
136 "r14",
137 "r15",
baa835b4 138 "usp",
fd60dc69 139 "isp",
baa835b4 140 "psw",
fd60dc69
KB
141 "pc",
142 "intb",
baa835b4 143 "bpsw",
fd60dc69
KB
144 "bpc",
145 "fintv",
146 "fpsw",
147 "acc"
baa835b4
KB
148 };
149
150 return reg_names[regnr];
151}
152
a28aa6ed
YQ
153/* Construct the flags type for PSW and BPSW. */
154
155static struct type *
156rx_psw_type (struct gdbarch *gdbarch)
157{
158 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
159
160 if (tdep->rx_psw_type == NULL)
161 {
162 tdep->rx_psw_type = arch_flags_type (gdbarch, "rx_psw_type", 4);
163 append_flags_type_flag (tdep->rx_psw_type, 0, "C");
164 append_flags_type_flag (tdep->rx_psw_type, 1, "Z");
165 append_flags_type_flag (tdep->rx_psw_type, 2, "S");
166 append_flags_type_flag (tdep->rx_psw_type, 3, "O");
167 append_flags_type_flag (tdep->rx_psw_type, 16, "I");
168 append_flags_type_flag (tdep->rx_psw_type, 17, "U");
169 append_flags_type_flag (tdep->rx_psw_type, 20, "PM");
170 append_flags_type_flag (tdep->rx_psw_type, 24, "IPL0");
171 append_flags_type_flag (tdep->rx_psw_type, 25, "IPL1");
172 append_flags_type_flag (tdep->rx_psw_type, 26, "IPL2");
173 append_flags_type_flag (tdep->rx_psw_type, 27, "IPL3");
174 }
175 return tdep->rx_psw_type;
176}
177
178/* Construct flags type for FPSW. */
179
180static struct type *
181rx_fpsw_type (struct gdbarch *gdbarch)
182{
183 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
184
185 if (tdep->rx_psw_type == NULL)
186 {
187 tdep->rx_fpsw_type = arch_flags_type (gdbarch, "rx_fpsw_type", 4);
188 append_flags_type_flag (tdep->rx_fpsw_type, 0, "RM0");
189 append_flags_type_flag (tdep->rx_fpsw_type, 1, "RM1");
190 append_flags_type_flag (tdep->rx_fpsw_type, 2, "CV");
191 append_flags_type_flag (tdep->rx_fpsw_type, 3, "CO");
192 append_flags_type_flag (tdep->rx_fpsw_type, 4, "CZ");
193 append_flags_type_flag (tdep->rx_fpsw_type, 5, "CU");
194 append_flags_type_flag (tdep->rx_fpsw_type, 6, "CX");
195 append_flags_type_flag (tdep->rx_fpsw_type, 7, "CE");
196 append_flags_type_flag (tdep->rx_fpsw_type, 8, "DN");
197 append_flags_type_flag (tdep->rx_fpsw_type, 10, "EV");
198 append_flags_type_flag (tdep->rx_fpsw_type, 11, "EO");
199 append_flags_type_flag (tdep->rx_fpsw_type, 12, "EZ");
200 append_flags_type_flag (tdep->rx_fpsw_type, 13, "EU");
201 append_flags_type_flag (tdep->rx_fpsw_type, 14, "EX");
202 append_flags_type_flag (tdep->rx_fpsw_type, 26, "FV");
203 append_flags_type_flag (tdep->rx_fpsw_type, 27, "FO");
204 append_flags_type_flag (tdep->rx_fpsw_type, 28, "FZ");
205 append_flags_type_flag (tdep->rx_fpsw_type, 29, "FU");
206 append_flags_type_flag (tdep->rx_fpsw_type, 30, "FX");
207 append_flags_type_flag (tdep->rx_fpsw_type, 31, "FS");
208 }
209
210 return tdep->rx_fpsw_type;
211}
212
baa835b4
KB
213/* Implement the "register_type" gdbarch method. */
214static struct type *
215rx_register_type (struct gdbarch *gdbarch, int reg_nr)
216{
0561fea4
KB
217 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
218
baa835b4
KB
219 if (reg_nr == RX_PC_REGNUM)
220 return builtin_type (gdbarch)->builtin_func_ptr;
0561fea4 221 else if (reg_nr == RX_PSW_REGNUM || reg_nr == RX_BPSW_REGNUM)
a28aa6ed 222 return rx_psw_type (gdbarch);
0561fea4 223 else if (reg_nr == RX_FPSW_REGNUM)
a28aa6ed 224 return rx_fpsw_type (gdbarch);
fd60dc69
KB
225 else if (reg_nr == RX_ACC_REGNUM)
226 return builtin_type (gdbarch)->builtin_unsigned_long_long;
baa835b4
KB
227 else
228 return builtin_type (gdbarch)->builtin_unsigned_long;
229}
230
231
232/* Function for finding saved registers in a 'struct pv_area'; this
233 function is passed to pv_area_scan.
234
235 If VALUE is a saved register, ADDR says it was saved at a constant
236 offset from the frame base, and SIZE indicates that the whole
237 register was saved, record its offset. */
238static void
239check_for_saved (void *result_untyped, pv_t addr, CORE_ADDR size, pv_t value)
240{
241 struct rx_prologue *result = (struct rx_prologue *) result_untyped;
242
243 if (value.kind == pvk_register
244 && value.k == 0
245 && pv_is_register (addr, RX_SP_REGNUM)
f5656ead 246 && size == register_size (target_gdbarch (), value.reg))
baa835b4
KB
247 result->reg_offset[value.reg] = addr.k;
248}
249
250/* Define a "handle" struct for fetching the next opcode. */
251struct rx_get_opcode_byte_handle
252{
253 CORE_ADDR pc;
254};
255
256/* Fetch a byte on behalf of the opcode decoder. HANDLE contains
257 the memory address of the next byte to fetch. If successful,
258 the address in the handle is updated and the byte fetched is
259 returned as the value of the function. If not successful, -1
260 is returned. */
261static int
262rx_get_opcode_byte (void *handle)
263{
19ba03f4
SM
264 struct rx_get_opcode_byte_handle *opcdata
265 = (struct rx_get_opcode_byte_handle *) handle;
baa835b4
KB
266 int status;
267 gdb_byte byte;
268
a0e28e54 269 status = target_read_code (opcdata->pc, &byte, 1);
baa835b4
KB
270 if (status == 0)
271 {
272 opcdata->pc += 1;
273 return byte;
274 }
275 else
276 return -1;
277}
278
279/* Analyze a prologue starting at START_PC, going no further than
280 LIMIT_PC. Fill in RESULT as appropriate. */
1b485e67 281
baa835b4 282static void
1b485e67
KB
283rx_analyze_prologue (CORE_ADDR start_pc, CORE_ADDR limit_pc,
284 enum rx_frame_type frame_type,
285 struct rx_prologue *result)
baa835b4
KB
286{
287 CORE_ADDR pc, next_pc;
288 int rn;
289 pv_t reg[RX_NUM_REGS];
290 struct pv_area *stack;
291 struct cleanup *back_to;
292 CORE_ADDR after_last_frame_setup_insn = start_pc;
293
294 memset (result, 0, sizeof (*result));
295
1b485e67
KB
296 result->frame_type = frame_type;
297
baa835b4
KB
298 for (rn = 0; rn < RX_NUM_REGS; rn++)
299 {
300 reg[rn] = pv_register (rn, 0);
301 result->reg_offset[rn] = 1;
302 }
303
f5656ead 304 stack = make_pv_area (RX_SP_REGNUM, gdbarch_addr_bit (target_gdbarch ()));
baa835b4
KB
305 back_to = make_cleanup_free_pv_area (stack);
306
1b485e67
KB
307 if (frame_type == RX_FRAME_TYPE_FAST_INTERRUPT)
308 {
309 /* This code won't do anything useful at present, but this is
310 what happens for fast interrupts. */
311 reg[RX_BPSW_REGNUM] = reg[RX_PSW_REGNUM];
312 reg[RX_BPC_REGNUM] = reg[RX_PC_REGNUM];
313 }
314 else
315 {
316 /* When an exception occurs, the PSW is saved to the interrupt stack
317 first. */
318 if (frame_type == RX_FRAME_TYPE_EXCEPTION)
319 {
320 reg[RX_SP_REGNUM] = pv_add_constant (reg[RX_SP_REGNUM], -4);
321 pv_area_store (stack, reg[RX_SP_REGNUM], 4, reg[RX_PSW_REGNUM]);
322 }
323
324 /* The call instruction (or an exception/interrupt) has saved the return
325 address on the stack. */
326 reg[RX_SP_REGNUM] = pv_add_constant (reg[RX_SP_REGNUM], -4);
327 pv_area_store (stack, reg[RX_SP_REGNUM], 4, reg[RX_PC_REGNUM]);
328
329 }
330
baa835b4
KB
331
332 pc = start_pc;
333 while (pc < limit_pc)
334 {
335 int bytes_read;
336 struct rx_get_opcode_byte_handle opcode_handle;
337 RX_Opcode_Decoded opc;
338
339 opcode_handle.pc = pc;
340 bytes_read = rx_decode_opcode (pc, &opc, rx_get_opcode_byte,
341 &opcode_handle);
342 next_pc = pc + bytes_read;
343
344 if (opc.id == RXO_pushm /* pushm r1, r2 */
345 && opc.op[1].type == RX_Operand_Register
346 && opc.op[2].type == RX_Operand_Register)
347 {
348 int r1, r2;
349 int r;
350
351 r1 = opc.op[1].reg;
352 r2 = opc.op[2].reg;
353 for (r = r2; r >= r1; r--)
354 {
355 reg[RX_SP_REGNUM] = pv_add_constant (reg[RX_SP_REGNUM], -4);
356 pv_area_store (stack, reg[RX_SP_REGNUM], 4, reg[r]);
357 }
358 after_last_frame_setup_insn = next_pc;
359 }
360 else if (opc.id == RXO_mov /* mov.l rdst, rsrc */
361 && opc.op[0].type == RX_Operand_Register
362 && opc.op[1].type == RX_Operand_Register
363 && opc.size == RX_Long)
364 {
365 int rdst, rsrc;
366
367 rdst = opc.op[0].reg;
368 rsrc = opc.op[1].reg;
369 reg[rdst] = reg[rsrc];
370 if (rdst == RX_FP_REGNUM && rsrc == RX_SP_REGNUM)
371 after_last_frame_setup_insn = next_pc;
372 }
373 else if (opc.id == RXO_mov /* mov.l rsrc, [-SP] */
374 && opc.op[0].type == RX_Operand_Predec
375 && opc.op[0].reg == RX_SP_REGNUM
376 && opc.op[1].type == RX_Operand_Register
377 && opc.size == RX_Long)
378 {
379 int rsrc;
380
381 rsrc = opc.op[1].reg;
382 reg[RX_SP_REGNUM] = pv_add_constant (reg[RX_SP_REGNUM], -4);
383 pv_area_store (stack, reg[RX_SP_REGNUM], 4, reg[rsrc]);
384 after_last_frame_setup_insn = next_pc;
385 }
386 else if (opc.id == RXO_add /* add #const, rsrc, rdst */
387 && opc.op[0].type == RX_Operand_Register
388 && opc.op[1].type == RX_Operand_Immediate
389 && opc.op[2].type == RX_Operand_Register)
390 {
391 int rdst = opc.op[0].reg;
392 int addend = opc.op[1].addend;
393 int rsrc = opc.op[2].reg;
394 reg[rdst] = pv_add_constant (reg[rsrc], addend);
395 /* Negative adjustments to the stack pointer or frame pointer
396 are (most likely) part of the prologue. */
397 if ((rdst == RX_SP_REGNUM || rdst == RX_FP_REGNUM) && addend < 0)
398 after_last_frame_setup_insn = next_pc;
399 }
400 else if (opc.id == RXO_mov
401 && opc.op[0].type == RX_Operand_Indirect
402 && opc.op[1].type == RX_Operand_Register
403 && opc.size == RX_Long
404 && (opc.op[0].reg == RX_SP_REGNUM
405 || opc.op[0].reg == RX_FP_REGNUM)
406 && (RX_R1_REGNUM <= opc.op[1].reg
407 && opc.op[1].reg <= RX_R4_REGNUM))
408 {
409 /* This moves an argument register to the stack. Don't
410 record it, but allow it to be a part of the prologue. */
411 }
412 else if (opc.id == RXO_branch
413 && opc.op[0].type == RX_Operand_Immediate
baa835b4
KB
414 && next_pc < opc.op[0].addend)
415 {
416 /* When a loop appears as the first statement of a function
417 body, gcc 4.x will use a BRA instruction to branch to the
418 loop condition checking code. This BRA instruction is
419 marked as part of the prologue. We therefore set next_pc
420 to this branch target and also stop the prologue scan.
421 The instructions at and beyond the branch target should
422 no longer be associated with the prologue.
423
424 Note that we only consider forward branches here. We
425 presume that a forward branch is being used to skip over
426 a loop body.
427
428 A backwards branch is covered by the default case below.
429 If we were to encounter a backwards branch, that would
430 most likely mean that we've scanned through a loop body.
431 We definitely want to stop the prologue scan when this
432 happens and that is precisely what is done by the default
433 case below. */
434
435 after_last_frame_setup_insn = opc.op[0].addend;
436 break; /* Scan no further if we hit this case. */
437 }
438 else
439 {
440 /* Terminate the prologue scan. */
441 break;
442 }
443
444 pc = next_pc;
445 }
446
447 /* Is the frame size (offset, really) a known constant? */
448 if (pv_is_register (reg[RX_SP_REGNUM], RX_SP_REGNUM))
449 result->frame_size = reg[RX_SP_REGNUM].k;
450
451 /* Was the frame pointer initialized? */
452 if (pv_is_register (reg[RX_FP_REGNUM], RX_SP_REGNUM))
453 {
454 result->has_frame_ptr = 1;
455 result->frame_ptr_offset = reg[RX_FP_REGNUM].k;
456 }
457
458 /* Record where all the registers were saved. */
459 pv_area_scan (stack, check_for_saved, (void *) result);
460
461 result->prologue_end = after_last_frame_setup_insn;
462
463 do_cleanups (back_to);
464}
465
466
467/* Implement the "skip_prologue" gdbarch method. */
468static CORE_ADDR
469rx_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
470{
2c02bd72 471 const char *name;
baa835b4
KB
472 CORE_ADDR func_addr, func_end;
473 struct rx_prologue p;
474
475 /* Try to find the extent of the function that contains PC. */
476 if (!find_pc_partial_function (pc, &name, &func_addr, &func_end))
477 return pc;
478
1b485e67
KB
479 /* The frame type doesn't matter here, since we only care about
480 where the prologue ends. We'll use RX_FRAME_TYPE_NORMAL. */
481 rx_analyze_prologue (pc, func_end, RX_FRAME_TYPE_NORMAL, &p);
baa835b4
KB
482 return p.prologue_end;
483}
484
485/* Given a frame described by THIS_FRAME, decode the prologue of its
486 associated function if there is not cache entry as specified by
487 THIS_PROLOGUE_CACHE. Save the decoded prologue in the cache and
488 return that struct as the value of this function. */
1b485e67 489
baa835b4
KB
490static struct rx_prologue *
491rx_analyze_frame_prologue (struct frame_info *this_frame,
1b485e67 492 enum rx_frame_type frame_type,
baa835b4
KB
493 void **this_prologue_cache)
494{
495 if (!*this_prologue_cache)
496 {
497 CORE_ADDR func_start, stop_addr;
498
499 *this_prologue_cache = FRAME_OBSTACK_ZALLOC (struct rx_prologue);
500
501 func_start = get_frame_func (this_frame);
502 stop_addr = get_frame_pc (this_frame);
503
504 /* If we couldn't find any function containing the PC, then
505 just initialize the prologue cache, but don't do anything. */
506 if (!func_start)
507 stop_addr = func_start;
508
1b485e67 509 rx_analyze_prologue (func_start, stop_addr, frame_type,
19ba03f4 510 (struct rx_prologue *) *this_prologue_cache);
baa835b4
KB
511 }
512
19ba03f4 513 return (struct rx_prologue *) *this_prologue_cache;
baa835b4
KB
514}
515
1b485e67
KB
516/* Determine type of frame by scanning the function for a return
517 instruction. */
518
519static enum rx_frame_type
520rx_frame_type (struct frame_info *this_frame, void **this_cache)
521{
522 const char *name;
523 CORE_ADDR pc, start_pc, lim_pc;
524 int bytes_read;
525 struct rx_get_opcode_byte_handle opcode_handle;
526 RX_Opcode_Decoded opc;
527
528 gdb_assert (this_cache != NULL);
529
530 /* If we have a cached value, return it. */
531
532 if (*this_cache != NULL)
533 {
19ba03f4 534 struct rx_prologue *p = (struct rx_prologue *) *this_cache;
1b485e67
KB
535
536 return p->frame_type;
537 }
538
539 /* No cached value; scan the function. The frame type is cached in
540 rx_analyze_prologue / rx_analyze_frame_prologue. */
541
542 pc = get_frame_pc (this_frame);
543
544 /* Attempt to find the last address in the function. If it cannot
545 be determined, set the limit to be a short ways past the frame's
546 pc. */
547 if (!find_pc_partial_function (pc, &name, &start_pc, &lim_pc))
548 lim_pc = pc + 20;
549
550 while (pc < lim_pc)
551 {
552 opcode_handle.pc = pc;
553 bytes_read = rx_decode_opcode (pc, &opc, rx_get_opcode_byte,
554 &opcode_handle);
555
556 if (bytes_read <= 0 || opc.id == RXO_rts)
557 return RX_FRAME_TYPE_NORMAL;
558 else if (opc.id == RXO_rtfi)
559 return RX_FRAME_TYPE_FAST_INTERRUPT;
560 else if (opc.id == RXO_rte)
561 return RX_FRAME_TYPE_EXCEPTION;
562
563 pc += bytes_read;
564 }
565
566 return RX_FRAME_TYPE_NORMAL;
567}
568
569
baa835b4
KB
570/* Given the next frame and a prologue cache, return this frame's
571 base. */
1b485e67 572
baa835b4 573static CORE_ADDR
1b485e67 574rx_frame_base (struct frame_info *this_frame, void **this_cache)
baa835b4 575{
1b485e67 576 enum rx_frame_type frame_type = rx_frame_type (this_frame, this_cache);
baa835b4 577 struct rx_prologue *p
1b485e67 578 = rx_analyze_frame_prologue (this_frame, frame_type, this_cache);
baa835b4
KB
579
580 /* In functions that use alloca, the distance between the stack
581 pointer and the frame base varies dynamically, so we can't use
582 the SP plus static information like prologue analysis to find the
583 frame base. However, such functions must have a frame pointer,
584 to be able to restore the SP on exit. So whenever we do have a
585 frame pointer, use that to find the base. */
586 if (p->has_frame_ptr)
587 {
588 CORE_ADDR fp = get_frame_register_unsigned (this_frame, RX_FP_REGNUM);
589 return fp - p->frame_ptr_offset;
590 }
591 else
592 {
593 CORE_ADDR sp = get_frame_register_unsigned (this_frame, RX_SP_REGNUM);
594 return sp - p->frame_size;
595 }
596}
597
598/* Implement the "frame_this_id" method for unwinding frames. */
1b485e67 599
baa835b4 600static void
1b485e67
KB
601rx_frame_this_id (struct frame_info *this_frame, void **this_cache,
602 struct frame_id *this_id)
baa835b4 603{
1b485e67 604 *this_id = frame_id_build (rx_frame_base (this_frame, this_cache),
baa835b4
KB
605 get_frame_func (this_frame));
606}
607
608/* Implement the "frame_prev_register" method for unwinding frames. */
1b485e67 609
baa835b4 610static struct value *
1b485e67
KB
611rx_frame_prev_register (struct frame_info *this_frame, void **this_cache,
612 int regnum)
baa835b4 613{
1b485e67 614 enum rx_frame_type frame_type = rx_frame_type (this_frame, this_cache);
baa835b4 615 struct rx_prologue *p
1b485e67
KB
616 = rx_analyze_frame_prologue (this_frame, frame_type, this_cache);
617 CORE_ADDR frame_base = rx_frame_base (this_frame, this_cache);
baa835b4
KB
618
619 if (regnum == RX_SP_REGNUM)
1b485e67
KB
620 {
621 if (frame_type == RX_FRAME_TYPE_EXCEPTION)
622 {
623 struct value *psw_val;
624 CORE_ADDR psw;
625
626 psw_val = rx_frame_prev_register (this_frame, this_cache,
627 RX_PSW_REGNUM);
628 psw = extract_unsigned_integer (value_contents_all (psw_val), 4,
629 gdbarch_byte_order (
630 get_frame_arch (this_frame)));
631
632 if ((psw & 0x20000 /* U bit */) != 0)
633 return rx_frame_prev_register (this_frame, this_cache,
634 RX_USP_REGNUM);
635
636 /* Fall through for the case where U bit is zero. */
637 }
638
639 return frame_unwind_got_constant (this_frame, regnum, frame_base);
640 }
641
642 if (frame_type == RX_FRAME_TYPE_FAST_INTERRUPT)
643 {
644 if (regnum == RX_PC_REGNUM)
645 return rx_frame_prev_register (this_frame, this_cache,
646 RX_BPC_REGNUM);
647 if (regnum == RX_PSW_REGNUM)
648 return rx_frame_prev_register (this_frame, this_cache,
649 RX_BPSW_REGNUM);
650 }
baa835b4
KB
651
652 /* If prologue analysis says we saved this register somewhere,
653 return a description of the stack slot holding it. */
1b485e67 654 if (p->reg_offset[regnum] != 1)
baa835b4
KB
655 return frame_unwind_got_memory (this_frame, regnum,
656 frame_base + p->reg_offset[regnum]);
657
658 /* Otherwise, presume we haven't changed the value of this
659 register, and get it from the next frame. */
1b485e67
KB
660 return frame_unwind_got_register (this_frame, regnum, regnum);
661}
662
663/* Return TRUE if the frame indicated by FRAME_TYPE is a normal frame. */
664
665static int
666normal_frame_p (enum rx_frame_type frame_type)
667{
668 return (frame_type == RX_FRAME_TYPE_NORMAL);
669}
670
671/* Return TRUE if the frame indicated by FRAME_TYPE is an exception
672 frame. */
673
674static int
675exception_frame_p (enum rx_frame_type frame_type)
676{
677 return (frame_type == RX_FRAME_TYPE_EXCEPTION
678 || frame_type == RX_FRAME_TYPE_FAST_INTERRUPT);
679}
680
681/* Common code used by both normal and exception frame sniffers. */
682
683static int
684rx_frame_sniffer_common (const struct frame_unwind *self,
685 struct frame_info *this_frame,
686 void **this_cache,
687 int (*sniff_p)(enum rx_frame_type) )
688{
689 gdb_assert (this_cache != NULL);
690
691 if (*this_cache == NULL)
692 {
693 enum rx_frame_type frame_type = rx_frame_type (this_frame, this_cache);
694
695 if (sniff_p (frame_type))
696 {
697 /* The call below will fill in the cache, including the frame
698 type. */
699 (void) rx_analyze_frame_prologue (this_frame, frame_type, this_cache);
700
701 return 1;
702 }
703 else
704 return 0;
705 }
baa835b4 706 else
1b485e67 707 {
19ba03f4 708 struct rx_prologue *p = (struct rx_prologue *) *this_cache;
1b485e67
KB
709
710 return sniff_p (p->frame_type);
711 }
712}
713
714/* Frame sniffer for normal (non-exception) frames. */
715
716static int
717rx_frame_sniffer (const struct frame_unwind *self,
718 struct frame_info *this_frame,
719 void **this_cache)
720{
721 return rx_frame_sniffer_common (self, this_frame, this_cache,
722 normal_frame_p);
723}
724
725/* Frame sniffer for exception frames. */
726
727static int
728rx_exception_sniffer (const struct frame_unwind *self,
729 struct frame_info *this_frame,
730 void **this_cache)
731{
732 return rx_frame_sniffer_common (self, this_frame, this_cache,
733 exception_frame_p);
baa835b4
KB
734}
735
1b485e67
KB
736/* Data structure for normal code using instruction-based prologue
737 analyzer. */
738
baa835b4
KB
739static const struct frame_unwind rx_frame_unwind = {
740 NORMAL_FRAME,
e0f68161 741 default_frame_unwind_stop_reason,
baa835b4
KB
742 rx_frame_this_id,
743 rx_frame_prev_register,
744 NULL,
1b485e67
KB
745 rx_frame_sniffer
746};
747
748/* Data structure for exception code using instruction-based prologue
749 analyzer. */
750
751static const struct frame_unwind rx_exception_unwind = {
752 /* SIGTRAMP_FRAME could be used here, but backtraces are less informative. */
753 NORMAL_FRAME,
754 default_frame_unwind_stop_reason,
755 rx_frame_this_id,
756 rx_frame_prev_register,
757 NULL,
758 rx_exception_sniffer
baa835b4
KB
759};
760
761/* Implement the "unwind_pc" gdbarch method. */
762static CORE_ADDR
763rx_unwind_pc (struct gdbarch *gdbarch, struct frame_info *this_frame)
764{
765 ULONGEST pc;
766
767 pc = frame_unwind_register_unsigned (this_frame, RX_PC_REGNUM);
768 return pc;
769}
770
771/* Implement the "unwind_sp" gdbarch method. */
772static CORE_ADDR
773rx_unwind_sp (struct gdbarch *gdbarch, struct frame_info *this_frame)
774{
775 ULONGEST sp;
776
777 sp = frame_unwind_register_unsigned (this_frame, RX_SP_REGNUM);
778 return sp;
779}
780
781/* Implement the "dummy_id" gdbarch method. */
782static struct frame_id
783rx_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
784{
785 return
786 frame_id_build (get_frame_register_unsigned (this_frame, RX_SP_REGNUM),
787 get_frame_pc (this_frame));
788}
789
790/* Implement the "push_dummy_call" gdbarch method. */
791static CORE_ADDR
792rx_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
793 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
794 struct value **args, CORE_ADDR sp, int struct_return,
795 CORE_ADDR struct_addr)
796{
797 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
798 int write_pass;
799 int sp_off = 0;
800 CORE_ADDR cfa;
801 int num_register_candidate_args;
802
803 struct type *func_type = value_type (function);
804
805 /* Dereference function pointer types. */
806 while (TYPE_CODE (func_type) == TYPE_CODE_PTR)
807 func_type = TYPE_TARGET_TYPE (func_type);
808
809 /* The end result had better be a function or a method. */
810 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC
811 || TYPE_CODE (func_type) == TYPE_CODE_METHOD);
812
813 /* Functions with a variable number of arguments have all of their
814 variable arguments and the last non-variable argument passed
815 on the stack.
816
817 Otherwise, we can pass up to four arguments on the stack.
818
819 Once computed, we leave this value alone. I.e. we don't update
820 it in case of a struct return going in a register or an argument
821 requiring multiple registers, etc. We rely instead on the value
822 of the ``arg_reg'' variable to get these other details correct. */
823
824 if (TYPE_VARARGS (func_type))
825 num_register_candidate_args = TYPE_NFIELDS (func_type) - 1;
826 else
827 num_register_candidate_args = 4;
828
829 /* We make two passes; the first does the stack allocation,
830 the second actually stores the arguments. */
831 for (write_pass = 0; write_pass <= 1; write_pass++)
832 {
833 int i;
834 int arg_reg = RX_R1_REGNUM;
835
836 if (write_pass)
837 sp = align_down (sp - sp_off, 4);
838 sp_off = 0;
839
840 if (struct_return)
841 {
842 struct type *return_type = TYPE_TARGET_TYPE (func_type);
843
844 gdb_assert (TYPE_CODE (return_type) == TYPE_CODE_STRUCT
845 || TYPE_CODE (func_type) == TYPE_CODE_UNION);
846
847 if (TYPE_LENGTH (return_type) > 16
848 || TYPE_LENGTH (return_type) % 4 != 0)
849 {
850 if (write_pass)
851 regcache_cooked_write_unsigned (regcache, RX_R15_REGNUM,
852 struct_addr);
853 }
854 }
855
856 /* Push the arguments. */
857 for (i = 0; i < nargs; i++)
858 {
859 struct value *arg = args[i];
860 const gdb_byte *arg_bits = value_contents_all (arg);
861 struct type *arg_type = check_typedef (value_type (arg));
862 ULONGEST arg_size = TYPE_LENGTH (arg_type);
863
864 if (i == 0 && struct_addr != 0 && !struct_return
865 && TYPE_CODE (arg_type) == TYPE_CODE_PTR
866 && extract_unsigned_integer (arg_bits, 4,
867 byte_order) == struct_addr)
868 {
869 /* This argument represents the address at which C++ (and
870 possibly other languages) store their return value.
871 Put this value in R15. */
872 if (write_pass)
873 regcache_cooked_write_unsigned (regcache, RX_R15_REGNUM,
874 struct_addr);
875 }
876 else if (TYPE_CODE (arg_type) != TYPE_CODE_STRUCT
94715c17
KB
877 && TYPE_CODE (arg_type) != TYPE_CODE_UNION
878 && arg_size <= 8)
baa835b4
KB
879 {
880 /* Argument is a scalar. */
881 if (arg_size == 8)
882 {
883 if (i < num_register_candidate_args
884 && arg_reg <= RX_R4_REGNUM - 1)
885 {
886 /* If argument registers are going to be used to pass
887 an 8 byte scalar, the ABI specifies that two registers
888 must be available. */
889 if (write_pass)
890 {
891 regcache_cooked_write_unsigned (regcache, arg_reg,
892 extract_unsigned_integer
893 (arg_bits, 4,
894 byte_order));
895 regcache_cooked_write_unsigned (regcache,
896 arg_reg + 1,
897 extract_unsigned_integer
898 (arg_bits + 4, 4,
899 byte_order));
900 }
901 arg_reg += 2;
902 }
903 else
904 {
905 sp_off = align_up (sp_off, 4);
906 /* Otherwise, pass the 8 byte scalar on the stack. */
907 if (write_pass)
908 write_memory (sp + sp_off, arg_bits, 8);
909 sp_off += 8;
910 }
911 }
912 else
913 {
914 ULONGEST u;
915
916 gdb_assert (arg_size <= 4);
917
918 u =
919 extract_unsigned_integer (arg_bits, arg_size, byte_order);
920
921 if (i < num_register_candidate_args
922 && arg_reg <= RX_R4_REGNUM)
923 {
924 if (write_pass)
925 regcache_cooked_write_unsigned (regcache, arg_reg, u);
926 arg_reg += 1;
927 }
928 else
929 {
930 int p_arg_size = 4;
931
932 if (TYPE_PROTOTYPED (func_type)
933 && i < TYPE_NFIELDS (func_type))
934 {
935 struct type *p_arg_type =
936 TYPE_FIELD_TYPE (func_type, i);
937 p_arg_size = TYPE_LENGTH (p_arg_type);
938 }
939
940 sp_off = align_up (sp_off, p_arg_size);
941
942 if (write_pass)
943 write_memory_unsigned_integer (sp + sp_off,
944 p_arg_size, byte_order,
945 u);
946 sp_off += p_arg_size;
947 }
948 }
949 }
950 else
951 {
952 /* Argument is a struct or union. Pass as much of the struct
953 in registers, if possible. Pass the rest on the stack. */
954 while (arg_size > 0)
955 {
956 if (i < num_register_candidate_args
957 && arg_reg <= RX_R4_REGNUM
958 && arg_size <= 4 * (RX_R4_REGNUM - arg_reg + 1)
959 && arg_size % 4 == 0)
960 {
325fac50 961 int len = std::min (arg_size, (ULONGEST) 4);
baa835b4
KB
962
963 if (write_pass)
964 regcache_cooked_write_unsigned (regcache, arg_reg,
965 extract_unsigned_integer
966 (arg_bits, len,
967 byte_order));
968 arg_bits += len;
969 arg_size -= len;
970 arg_reg++;
971 }
972 else
973 {
974 sp_off = align_up (sp_off, 4);
975 if (write_pass)
976 write_memory (sp + sp_off, arg_bits, arg_size);
977 sp_off += align_up (arg_size, 4);
978 arg_size = 0;
979 }
980 }
981 }
982 }
983 }
984
985 /* Keep track of the stack address prior to pushing the return address.
986 This is the value that we'll return. */
987 cfa = sp;
988
989 /* Push the return address. */
990 sp = sp - 4;
991 write_memory_unsigned_integer (sp, 4, byte_order, bp_addr);
992
993 /* Update the stack pointer. */
994 regcache_cooked_write_unsigned (regcache, RX_SP_REGNUM, sp);
995
996 return cfa;
997}
998
999/* Implement the "return_value" gdbarch method. */
1000static enum return_value_convention
1001rx_return_value (struct gdbarch *gdbarch,
6a3a010b 1002 struct value *function,
baa835b4
KB
1003 struct type *valtype,
1004 struct regcache *regcache,
1005 gdb_byte *readbuf, const gdb_byte *writebuf)
1006{
1007 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1008 ULONGEST valtype_len = TYPE_LENGTH (valtype);
1009
1010 if (TYPE_LENGTH (valtype) > 16
1011 || ((TYPE_CODE (valtype) == TYPE_CODE_STRUCT
1012 || TYPE_CODE (valtype) == TYPE_CODE_UNION)
1013 && TYPE_LENGTH (valtype) % 4 != 0))
1014 return RETURN_VALUE_STRUCT_CONVENTION;
1015
1016 if (readbuf)
1017 {
1018 ULONGEST u;
1019 int argreg = RX_R1_REGNUM;
1020 int offset = 0;
1021
1022 while (valtype_len > 0)
1023 {
325fac50 1024 int len = std::min (valtype_len, (ULONGEST) 4);
baa835b4
KB
1025
1026 regcache_cooked_read_unsigned (regcache, argreg, &u);
1027 store_unsigned_integer (readbuf + offset, len, byte_order, u);
1028 valtype_len -= len;
1029 offset += len;
1030 argreg++;
1031 }
1032 }
1033
1034 if (writebuf)
1035 {
1036 ULONGEST u;
1037 int argreg = RX_R1_REGNUM;
1038 int offset = 0;
1039
1040 while (valtype_len > 0)
1041 {
325fac50 1042 int len = std::min (valtype_len, (ULONGEST) 4);
baa835b4
KB
1043
1044 u = extract_unsigned_integer (writebuf + offset, len, byte_order);
1045 regcache_cooked_write_unsigned (regcache, argreg, u);
1046 valtype_len -= len;
1047 offset += len;
1048 argreg++;
1049 }
1050 }
1051
1052 return RETURN_VALUE_REGISTER_CONVENTION;
1053}
1054
04180708 1055constexpr gdb_byte rx_break_insn[] = { 0x00 };
598cc9dc 1056
04180708 1057typedef BP_MANIPULATION (rx_break_insn) rx_breakpoint;
baa835b4 1058
fd6e021d
KB
1059/* Implement the dwarf_reg_to_regnum" gdbarch method. */
1060
1061static int
1062rx_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
1063{
1064 if (0 <= reg && reg <= 15)
1065 return reg;
1066 else if (reg == 16)
1067 return RX_PSW_REGNUM;
1068 else if (reg == 17)
1069 return RX_PC_REGNUM;
1070 else
0fde2c53 1071 return -1;
fd6e021d
KB
1072}
1073
baa835b4
KB
1074/* Allocate and initialize a gdbarch object. */
1075static struct gdbarch *
1076rx_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1077{
1078 struct gdbarch *gdbarch;
1079 struct gdbarch_tdep *tdep;
1080 int elf_flags;
1081
1082 /* Extract the elf_flags if available. */
1083 if (info.abfd != NULL
1084 && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
1085 elf_flags = elf_elfheader (info.abfd)->e_flags;
1086 else
1087 elf_flags = 0;
1088
1089
1090 /* Try to find the architecture in the list of already defined
1091 architectures. */
1092 for (arches = gdbarch_list_lookup_by_info (arches, &info);
1093 arches != NULL;
1094 arches = gdbarch_list_lookup_by_info (arches->next, &info))
1095 {
1096 if (gdbarch_tdep (arches->gdbarch)->elf_flags != elf_flags)
1097 continue;
1098
1099 return arches->gdbarch;
1100 }
1101
1102 /* None found, create a new architecture from the information
1103 provided. */
8d749320 1104 tdep = XNEW (struct gdbarch_tdep);
baa835b4
KB
1105 gdbarch = gdbarch_alloc (&info, tdep);
1106 tdep->elf_flags = elf_flags;
1107
1108 set_gdbarch_num_regs (gdbarch, RX_NUM_REGS);
1109 set_gdbarch_num_pseudo_regs (gdbarch, 0);
1110 set_gdbarch_register_name (gdbarch, rx_register_name);
1111 set_gdbarch_register_type (gdbarch, rx_register_type);
1112 set_gdbarch_pc_regnum (gdbarch, RX_PC_REGNUM);
1113 set_gdbarch_sp_regnum (gdbarch, RX_SP_REGNUM);
1114 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1115 set_gdbarch_decr_pc_after_break (gdbarch, 1);
04180708
YQ
1116 set_gdbarch_breakpoint_kind_from_pc (gdbarch, rx_breakpoint::kind_from_pc);
1117 set_gdbarch_sw_breakpoint_from_kind (gdbarch, rx_breakpoint::bp_from_kind);
baa835b4
KB
1118 set_gdbarch_skip_prologue (gdbarch, rx_skip_prologue);
1119
1120 set_gdbarch_print_insn (gdbarch, print_insn_rx);
1121
1122 set_gdbarch_unwind_pc (gdbarch, rx_unwind_pc);
1123 set_gdbarch_unwind_sp (gdbarch, rx_unwind_sp);
1124
1125 /* Target builtin data types. */
1126 set_gdbarch_char_signed (gdbarch, 0);
1127 set_gdbarch_short_bit (gdbarch, 16);
1128 set_gdbarch_int_bit (gdbarch, 32);
1129 set_gdbarch_long_bit (gdbarch, 32);
1130 set_gdbarch_long_long_bit (gdbarch, 64);
1131 set_gdbarch_ptr_bit (gdbarch, 32);
1132 set_gdbarch_float_bit (gdbarch, 32);
1133 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
1134 if (elf_flags & E_FLAG_RX_64BIT_DOUBLES)
1135 {
1136 set_gdbarch_double_bit (gdbarch, 64);
1137 set_gdbarch_long_double_bit (gdbarch, 64);
1138 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
1139 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
1140 }
1141 else
1142 {
1143 set_gdbarch_double_bit (gdbarch, 32);
1144 set_gdbarch_long_double_bit (gdbarch, 32);
1145 set_gdbarch_double_format (gdbarch, floatformats_ieee_single);
1146 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_single);
1147 }
1148
fd6e021d
KB
1149 /* DWARF register mapping. */
1150 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, rx_dwarf_reg_to_regnum);
1151
baa835b4 1152 /* Frame unwinding. */
1b485e67 1153 frame_unwind_append_unwinder (gdbarch, &rx_exception_unwind);
baa835b4 1154 dwarf2_append_unwinders (gdbarch);
baa835b4
KB
1155 frame_unwind_append_unwinder (gdbarch, &rx_frame_unwind);
1156
1157 /* Methods for saving / extracting a dummy frame's ID.
1158 The ID's stack address must match the SP value returned by
1159 PUSH_DUMMY_CALL, and saved by generic_save_dummy_frame_tos. */
1160 set_gdbarch_dummy_id (gdbarch, rx_dummy_id);
1161 set_gdbarch_push_dummy_call (gdbarch, rx_push_dummy_call);
1162 set_gdbarch_return_value (gdbarch, rx_return_value);
1163
1164 /* Virtual tables. */
1165 set_gdbarch_vbit_in_delta (gdbarch, 1);
1166
1167 return gdbarch;
1168}
1169
693be288
JK
1170/* -Wmissing-prototypes */
1171extern initialize_file_ftype _initialize_rx_tdep;
1172
baa835b4 1173/* Register the above initialization routine. */
693be288 1174
baa835b4
KB
1175void
1176_initialize_rx_tdep (void)
1177{
1178 register_gdbarch_init (bfd_arch_rx, rx_gdbarch_init);
1179}