Bump GDB's version number to 15.0.91.DATE-git.
[binutils-gdb.git] / gdb / rx-tdep.c
blob6b12fe0a3148533974cef99ff581be5c7ac71d6a
1 /* Target-dependent code for the Renesas RX for GDB, the GNU debugger.
3 Copyright (C) 2008-2024 Free Software Foundation, Inc.
5 Contributed by Red Hat, Inc.
7 This file is part of GDB.
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.
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.
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/>. */
22 #include "arch-utils.h"
23 #include "extract-store-integer.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 #include "remote.h"
37 #include "target-descriptions.h"
38 #include "gdbarch.h"
39 #include "inferior.h"
41 #include "elf/rx.h"
42 #include "elf-bfd.h"
43 #include <algorithm>
45 #include "features/rx.c"
47 /* Certain important register numbers. */
48 enum
50 RX_SP_REGNUM = 0,
51 RX_R1_REGNUM = 1,
52 RX_R4_REGNUM = 4,
53 RX_FP_REGNUM = 6,
54 RX_R15_REGNUM = 15,
55 RX_USP_REGNUM = 16,
56 RX_PSW_REGNUM = 18,
57 RX_PC_REGNUM = 19,
58 RX_BPSW_REGNUM = 21,
59 RX_BPC_REGNUM = 22,
60 RX_FPSW_REGNUM = 24,
61 RX_ACC_REGNUM = 25,
62 RX_NUM_REGS = 26
65 /* RX frame types. */
66 enum rx_frame_type {
67 RX_FRAME_TYPE_NORMAL,
68 RX_FRAME_TYPE_EXCEPTION,
69 RX_FRAME_TYPE_FAST_INTERRUPT
72 /* Architecture specific data. */
73 struct rx_gdbarch_tdep : gdbarch_tdep_base
75 /* The ELF header flags specify the multilib used. */
76 int elf_flags = 0;
78 /* Type of PSW and BPSW. */
79 struct type *rx_psw_type = nullptr;
81 /* Type of FPSW. */
82 struct type *rx_fpsw_type = nullptr;
85 /* This structure holds the results of a prologue analysis. */
86 struct rx_prologue
88 /* Frame type, either a normal frame or one of two types of exception
89 frames. */
90 enum rx_frame_type frame_type;
92 /* The offset from the frame base to the stack pointer --- always
93 zero or negative.
95 Calling this a "size" is a bit misleading, but given that the
96 stack grows downwards, using offsets for everything keeps one
97 from going completely sign-crazy: you never change anything's
98 sign for an ADD instruction; always change the second operand's
99 sign for a SUB instruction; and everything takes care of
100 itself. */
101 int frame_size;
103 /* Non-zero if this function has initialized the frame pointer from
104 the stack pointer, zero otherwise. */
105 int has_frame_ptr;
107 /* If has_frame_ptr is non-zero, this is the offset from the frame
108 base to where the frame pointer points. This is always zero or
109 negative. */
110 int frame_ptr_offset;
112 /* The address of the first instruction at which the frame has been
113 set up and the arguments are where the debug info says they are
114 --- as best as we can tell. */
115 CORE_ADDR prologue_end;
117 /* reg_offset[R] is the offset from the CFA at which register R is
118 saved, or 1 if register R has not been saved. (Real values are
119 always zero or negative.) */
120 int reg_offset[RX_NUM_REGS];
123 /* RX register names */
124 static const char *const rx_register_names[] = {
125 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
126 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
127 "usp", "isp", "psw", "pc", "intb", "bpsw","bpc","fintv",
128 "fpsw", "acc",
132 /* Function for finding saved registers in a 'struct pv_area'; this
133 function is passed to pv_area::scan.
135 If VALUE is a saved register, ADDR says it was saved at a constant
136 offset from the frame base, and SIZE indicates that the whole
137 register was saved, record its offset. */
138 static void
139 check_for_saved (void *result_untyped, pv_t addr, CORE_ADDR size, pv_t value)
141 struct rx_prologue *result = (struct rx_prologue *) result_untyped;
143 if (value.kind == pvk_register
144 && value.k == 0
145 && pv_is_register (addr, RX_SP_REGNUM)
146 && size == register_size (current_inferior ()->arch (), value.reg))
147 result->reg_offset[value.reg] = addr.k;
150 /* Define a "handle" struct for fetching the next opcode. */
151 struct rx_get_opcode_byte_handle
153 CORE_ADDR pc;
156 /* Fetch a byte on behalf of the opcode decoder. HANDLE contains
157 the memory address of the next byte to fetch. If successful,
158 the address in the handle is updated and the byte fetched is
159 returned as the value of the function. If not successful, -1
160 is returned. */
161 static int
162 rx_get_opcode_byte (void *handle)
164 struct rx_get_opcode_byte_handle *opcdata
165 = (struct rx_get_opcode_byte_handle *) handle;
166 int status;
167 gdb_byte byte;
169 status = target_read_code (opcdata->pc, &byte, 1);
170 if (status == 0)
172 opcdata->pc += 1;
173 return byte;
175 else
176 return -1;
179 /* Analyze a prologue starting at START_PC, going no further than
180 LIMIT_PC. Fill in RESULT as appropriate. */
182 static void
183 rx_analyze_prologue (CORE_ADDR start_pc, CORE_ADDR limit_pc,
184 enum rx_frame_type frame_type,
185 struct rx_prologue *result)
187 CORE_ADDR pc, next_pc;
188 int rn;
189 pv_t reg[RX_NUM_REGS];
190 CORE_ADDR after_last_frame_setup_insn = start_pc;
192 memset (result, 0, sizeof (*result));
194 result->frame_type = frame_type;
196 for (rn = 0; rn < RX_NUM_REGS; rn++)
198 reg[rn] = pv_register (rn, 0);
199 result->reg_offset[rn] = 1;
202 pv_area stack (RX_SP_REGNUM, gdbarch_addr_bit (current_inferior ()->arch ()));
204 if (frame_type == RX_FRAME_TYPE_FAST_INTERRUPT)
206 /* This code won't do anything useful at present, but this is
207 what happens for fast interrupts. */
208 reg[RX_BPSW_REGNUM] = reg[RX_PSW_REGNUM];
209 reg[RX_BPC_REGNUM] = reg[RX_PC_REGNUM];
211 else
213 /* When an exception occurs, the PSW is saved to the interrupt stack
214 first. */
215 if (frame_type == RX_FRAME_TYPE_EXCEPTION)
217 reg[RX_SP_REGNUM] = pv_add_constant (reg[RX_SP_REGNUM], -4);
218 stack.store (reg[RX_SP_REGNUM], 4, reg[RX_PSW_REGNUM]);
221 /* The call instruction (or an exception/interrupt) has saved the return
222 address on the stack. */
223 reg[RX_SP_REGNUM] = pv_add_constant (reg[RX_SP_REGNUM], -4);
224 stack.store (reg[RX_SP_REGNUM], 4, reg[RX_PC_REGNUM]);
229 pc = start_pc;
230 while (pc < limit_pc)
232 int bytes_read;
233 struct rx_get_opcode_byte_handle opcode_handle;
234 RX_Opcode_Decoded opc;
236 opcode_handle.pc = pc;
237 bytes_read = rx_decode_opcode (pc, &opc, rx_get_opcode_byte,
238 &opcode_handle);
239 next_pc = pc + bytes_read;
241 if (opc.id == RXO_pushm /* pushm r1, r2 */
242 && opc.op[1].type == RX_Operand_Register
243 && opc.op[2].type == RX_Operand_Register)
245 int r1, r2;
246 int r;
248 r1 = opc.op[1].reg;
249 r2 = opc.op[2].reg;
250 for (r = r2; r >= r1; r--)
252 reg[RX_SP_REGNUM] = pv_add_constant (reg[RX_SP_REGNUM], -4);
253 stack.store (reg[RX_SP_REGNUM], 4, reg[r]);
255 after_last_frame_setup_insn = next_pc;
257 else if (opc.id == RXO_mov /* mov.l rdst, rsrc */
258 && opc.op[0].type == RX_Operand_Register
259 && opc.op[1].type == RX_Operand_Register
260 && opc.size == RX_Long)
262 int rdst, rsrc;
264 rdst = opc.op[0].reg;
265 rsrc = opc.op[1].reg;
266 reg[rdst] = reg[rsrc];
267 if (rdst == RX_FP_REGNUM && rsrc == RX_SP_REGNUM)
268 after_last_frame_setup_insn = next_pc;
270 else if (opc.id == RXO_mov /* mov.l rsrc, [-SP] */
271 && opc.op[0].type == RX_Operand_Predec
272 && opc.op[0].reg == RX_SP_REGNUM
273 && opc.op[1].type == RX_Operand_Register
274 && opc.size == RX_Long)
276 int rsrc;
278 rsrc = opc.op[1].reg;
279 reg[RX_SP_REGNUM] = pv_add_constant (reg[RX_SP_REGNUM], -4);
280 stack.store (reg[RX_SP_REGNUM], 4, reg[rsrc]);
281 after_last_frame_setup_insn = next_pc;
283 else if (opc.id == RXO_add /* add #const, rsrc, rdst */
284 && opc.op[0].type == RX_Operand_Register
285 && opc.op[1].type == RX_Operand_Immediate
286 && opc.op[2].type == RX_Operand_Register)
288 int rdst = opc.op[0].reg;
289 int addend = opc.op[1].addend;
290 int rsrc = opc.op[2].reg;
291 reg[rdst] = pv_add_constant (reg[rsrc], addend);
292 /* Negative adjustments to the stack pointer or frame pointer
293 are (most likely) part of the prologue. */
294 if ((rdst == RX_SP_REGNUM || rdst == RX_FP_REGNUM) && addend < 0)
295 after_last_frame_setup_insn = next_pc;
297 else if (opc.id == RXO_mov
298 && opc.op[0].type == RX_Operand_Indirect
299 && opc.op[1].type == RX_Operand_Register
300 && opc.size == RX_Long
301 && (opc.op[0].reg == RX_SP_REGNUM
302 || opc.op[0].reg == RX_FP_REGNUM)
303 && (RX_R1_REGNUM <= opc.op[1].reg
304 && opc.op[1].reg <= RX_R4_REGNUM))
306 /* This moves an argument register to the stack. Don't
307 record it, but allow it to be a part of the prologue. */
309 else if (opc.id == RXO_branch
310 && opc.op[0].type == RX_Operand_Immediate
311 && next_pc < opc.op[0].addend)
313 /* When a loop appears as the first statement of a function
314 body, gcc 4.x will use a BRA instruction to branch to the
315 loop condition checking code. This BRA instruction is
316 marked as part of the prologue. We therefore set next_pc
317 to this branch target and also stop the prologue scan.
318 The instructions at and beyond the branch target should
319 no longer be associated with the prologue.
321 Note that we only consider forward branches here. We
322 presume that a forward branch is being used to skip over
323 a loop body.
325 A backwards branch is covered by the default case below.
326 If we were to encounter a backwards branch, that would
327 most likely mean that we've scanned through a loop body.
328 We definitely want to stop the prologue scan when this
329 happens and that is precisely what is done by the default
330 case below. */
332 after_last_frame_setup_insn = opc.op[0].addend;
333 break; /* Scan no further if we hit this case. */
335 else
337 /* Terminate the prologue scan. */
338 break;
341 pc = next_pc;
344 /* Is the frame size (offset, really) a known constant? */
345 if (pv_is_register (reg[RX_SP_REGNUM], RX_SP_REGNUM))
346 result->frame_size = reg[RX_SP_REGNUM].k;
348 /* Was the frame pointer initialized? */
349 if (pv_is_register (reg[RX_FP_REGNUM], RX_SP_REGNUM))
351 result->has_frame_ptr = 1;
352 result->frame_ptr_offset = reg[RX_FP_REGNUM].k;
355 /* Record where all the registers were saved. */
356 stack.scan (check_for_saved, (void *) result);
358 result->prologue_end = after_last_frame_setup_insn;
362 /* Implement the "skip_prologue" gdbarch method. */
363 static CORE_ADDR
364 rx_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
366 const char *name;
367 CORE_ADDR func_addr, func_end;
368 struct rx_prologue p;
370 /* Try to find the extent of the function that contains PC. */
371 if (!find_pc_partial_function (pc, &name, &func_addr, &func_end))
372 return pc;
374 /* The frame type doesn't matter here, since we only care about
375 where the prologue ends. We'll use RX_FRAME_TYPE_NORMAL. */
376 rx_analyze_prologue (pc, func_end, RX_FRAME_TYPE_NORMAL, &p);
377 return p.prologue_end;
380 /* Given a frame described by THIS_FRAME, decode the prologue of its
381 associated function if there is not cache entry as specified by
382 THIS_PROLOGUE_CACHE. Save the decoded prologue in the cache and
383 return that struct as the value of this function. */
385 static struct rx_prologue *
386 rx_analyze_frame_prologue (const frame_info_ptr &this_frame,
387 enum rx_frame_type frame_type,
388 void **this_prologue_cache)
390 if (!*this_prologue_cache)
392 CORE_ADDR func_start, stop_addr;
394 *this_prologue_cache = FRAME_OBSTACK_ZALLOC (struct rx_prologue);
396 func_start = get_frame_func (this_frame);
397 stop_addr = get_frame_pc (this_frame);
399 /* If we couldn't find any function containing the PC, then
400 just initialize the prologue cache, but don't do anything. */
401 if (!func_start)
402 stop_addr = func_start;
404 rx_analyze_prologue (func_start, stop_addr, frame_type,
405 (struct rx_prologue *) *this_prologue_cache);
408 return (struct rx_prologue *) *this_prologue_cache;
411 /* Determine type of frame by scanning the function for a return
412 instruction. */
414 static enum rx_frame_type
415 rx_frame_type (const frame_info_ptr &this_frame, void **this_cache)
417 const char *name;
418 CORE_ADDR pc, start_pc, lim_pc;
419 int bytes_read;
420 struct rx_get_opcode_byte_handle opcode_handle;
421 RX_Opcode_Decoded opc;
423 gdb_assert (this_cache != NULL);
425 /* If we have a cached value, return it. */
427 if (*this_cache != NULL)
429 struct rx_prologue *p = (struct rx_prologue *) *this_cache;
431 return p->frame_type;
434 /* No cached value; scan the function. The frame type is cached in
435 rx_analyze_prologue / rx_analyze_frame_prologue. */
437 pc = get_frame_pc (this_frame);
439 /* Attempt to find the last address in the function. If it cannot
440 be determined, set the limit to be a short ways past the frame's
441 pc. */
442 if (!find_pc_partial_function (pc, &name, &start_pc, &lim_pc))
443 lim_pc = pc + 20;
445 while (pc < lim_pc)
447 opcode_handle.pc = pc;
448 bytes_read = rx_decode_opcode (pc, &opc, rx_get_opcode_byte,
449 &opcode_handle);
451 if (bytes_read <= 0 || opc.id == RXO_rts)
452 return RX_FRAME_TYPE_NORMAL;
453 else if (opc.id == RXO_rtfi)
454 return RX_FRAME_TYPE_FAST_INTERRUPT;
455 else if (opc.id == RXO_rte)
456 return RX_FRAME_TYPE_EXCEPTION;
458 pc += bytes_read;
461 return RX_FRAME_TYPE_NORMAL;
465 /* Given the next frame and a prologue cache, return this frame's
466 base. */
468 static CORE_ADDR
469 rx_frame_base (const frame_info_ptr &this_frame, void **this_cache)
471 enum rx_frame_type frame_type = rx_frame_type (this_frame, this_cache);
472 struct rx_prologue *p
473 = rx_analyze_frame_prologue (this_frame, frame_type, this_cache);
475 /* In functions that use alloca, the distance between the stack
476 pointer and the frame base varies dynamically, so we can't use
477 the SP plus static information like prologue analysis to find the
478 frame base. However, such functions must have a frame pointer,
479 to be able to restore the SP on exit. So whenever we do have a
480 frame pointer, use that to find the base. */
481 if (p->has_frame_ptr)
483 CORE_ADDR fp = get_frame_register_unsigned (this_frame, RX_FP_REGNUM);
484 return fp - p->frame_ptr_offset;
486 else
488 CORE_ADDR sp = get_frame_register_unsigned (this_frame, RX_SP_REGNUM);
489 return sp - p->frame_size;
493 /* Implement the "frame_this_id" method for unwinding frames. */
495 static void
496 rx_frame_this_id (const frame_info_ptr &this_frame, void **this_cache,
497 struct frame_id *this_id)
499 *this_id = frame_id_build (rx_frame_base (this_frame, this_cache),
500 get_frame_func (this_frame));
503 /* Implement the "frame_prev_register" method for unwinding frames. */
505 static struct value *
506 rx_frame_prev_register (const frame_info_ptr &this_frame, void **this_cache,
507 int regnum)
509 enum rx_frame_type frame_type = rx_frame_type (this_frame, this_cache);
510 struct rx_prologue *p
511 = rx_analyze_frame_prologue (this_frame, frame_type, this_cache);
512 CORE_ADDR frame_base = rx_frame_base (this_frame, this_cache);
514 if (regnum == RX_SP_REGNUM)
516 if (frame_type == RX_FRAME_TYPE_EXCEPTION)
518 struct value *psw_val;
519 CORE_ADDR psw;
521 psw_val = rx_frame_prev_register (this_frame, this_cache,
522 RX_PSW_REGNUM);
523 psw = extract_unsigned_integer
524 (psw_val->contents_all ().data (), 4,
525 gdbarch_byte_order (get_frame_arch (this_frame)));
527 if ((psw & 0x20000 /* U bit */) != 0)
528 return rx_frame_prev_register (this_frame, this_cache,
529 RX_USP_REGNUM);
531 /* Fall through for the case where U bit is zero. */
534 return frame_unwind_got_constant (this_frame, regnum, frame_base);
537 if (frame_type == RX_FRAME_TYPE_FAST_INTERRUPT)
539 if (regnum == RX_PC_REGNUM)
540 return rx_frame_prev_register (this_frame, this_cache,
541 RX_BPC_REGNUM);
542 if (regnum == RX_PSW_REGNUM)
543 return rx_frame_prev_register (this_frame, this_cache,
544 RX_BPSW_REGNUM);
547 /* If prologue analysis says we saved this register somewhere,
548 return a description of the stack slot holding it. */
549 if (p->reg_offset[regnum] != 1)
550 return frame_unwind_got_memory (this_frame, regnum,
551 frame_base + p->reg_offset[regnum]);
553 /* Otherwise, presume we haven't changed the value of this
554 register, and get it from the next frame. */
555 return frame_unwind_got_register (this_frame, regnum, regnum);
558 /* Return TRUE if the frame indicated by FRAME_TYPE is a normal frame. */
560 static int
561 normal_frame_p (enum rx_frame_type frame_type)
563 return (frame_type == RX_FRAME_TYPE_NORMAL);
566 /* Return TRUE if the frame indicated by FRAME_TYPE is an exception
567 frame. */
569 static int
570 exception_frame_p (enum rx_frame_type frame_type)
572 return (frame_type == RX_FRAME_TYPE_EXCEPTION
573 || frame_type == RX_FRAME_TYPE_FAST_INTERRUPT);
576 /* Common code used by both normal and exception frame sniffers. */
578 static int
579 rx_frame_sniffer_common (const struct frame_unwind *self,
580 const frame_info_ptr &this_frame,
581 void **this_cache,
582 int (*sniff_p)(enum rx_frame_type) )
584 gdb_assert (this_cache != NULL);
586 if (*this_cache == NULL)
588 enum rx_frame_type frame_type = rx_frame_type (this_frame, this_cache);
590 if (sniff_p (frame_type))
592 /* The call below will fill in the cache, including the frame
593 type. */
594 (void) rx_analyze_frame_prologue (this_frame, frame_type, this_cache);
596 return 1;
598 else
599 return 0;
601 else
603 struct rx_prologue *p = (struct rx_prologue *) *this_cache;
605 return sniff_p (p->frame_type);
609 /* Frame sniffer for normal (non-exception) frames. */
611 static int
612 rx_frame_sniffer (const struct frame_unwind *self,
613 const frame_info_ptr &this_frame,
614 void **this_cache)
616 return rx_frame_sniffer_common (self, this_frame, this_cache,
617 normal_frame_p);
620 /* Frame sniffer for exception frames. */
622 static int
623 rx_exception_sniffer (const struct frame_unwind *self,
624 const frame_info_ptr &this_frame,
625 void **this_cache)
627 return rx_frame_sniffer_common (self, this_frame, this_cache,
628 exception_frame_p);
631 /* Data structure for normal code using instruction-based prologue
632 analyzer. */
634 static const struct frame_unwind rx_frame_unwind = {
635 "rx prologue",
636 NORMAL_FRAME,
637 default_frame_unwind_stop_reason,
638 rx_frame_this_id,
639 rx_frame_prev_register,
640 NULL,
641 rx_frame_sniffer
644 /* Data structure for exception code using instruction-based prologue
645 analyzer. */
647 static const struct frame_unwind rx_exception_unwind = {
648 "rx exception",
649 /* SIGTRAMP_FRAME could be used here, but backtraces are less informative. */
650 NORMAL_FRAME,
651 default_frame_unwind_stop_reason,
652 rx_frame_this_id,
653 rx_frame_prev_register,
654 NULL,
655 rx_exception_sniffer
658 /* Implement the "push_dummy_call" gdbarch method. */
659 static CORE_ADDR
660 rx_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
661 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
662 struct value **args, CORE_ADDR sp,
663 function_call_return_method return_method,
664 CORE_ADDR struct_addr)
666 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
667 int write_pass;
668 int sp_off = 0;
669 CORE_ADDR cfa;
670 int num_register_candidate_args;
672 struct type *func_type = function->type ();
674 /* Dereference function pointer types. */
675 while (func_type->code () == TYPE_CODE_PTR)
676 func_type = func_type->target_type ();
678 /* The end result had better be a function or a method. */
679 gdb_assert (func_type->code () == TYPE_CODE_FUNC
680 || func_type->code () == TYPE_CODE_METHOD);
682 /* Functions with a variable number of arguments have all of their
683 variable arguments and the last non-variable argument passed
684 on the stack.
686 Otherwise, we can pass up to four arguments on the stack.
688 Once computed, we leave this value alone. I.e. we don't update
689 it in case of a struct return going in a register or an argument
690 requiring multiple registers, etc. We rely instead on the value
691 of the ``arg_reg'' variable to get these other details correct. */
693 if (func_type->has_varargs ())
694 num_register_candidate_args = func_type->num_fields () - 1;
695 else
696 num_register_candidate_args = 4;
698 /* We make two passes; the first does the stack allocation,
699 the second actually stores the arguments. */
700 for (write_pass = 0; write_pass <= 1; write_pass++)
702 int i;
703 int arg_reg = RX_R1_REGNUM;
705 if (write_pass)
706 sp = align_down (sp - sp_off, 4);
707 sp_off = 0;
709 if (return_method == return_method_struct)
711 struct type *return_type = func_type->target_type ();
713 gdb_assert (return_type->code () == TYPE_CODE_STRUCT
714 || func_type->code () == TYPE_CODE_UNION);
716 if (return_type->length () > 16
717 || return_type->length () % 4 != 0)
719 if (write_pass)
720 regcache_cooked_write_unsigned (regcache, RX_R15_REGNUM,
721 struct_addr);
725 /* Push the arguments. */
726 for (i = 0; i < nargs; i++)
728 struct value *arg = args[i];
729 const gdb_byte *arg_bits = arg->contents_all ().data ();
730 struct type *arg_type = check_typedef (arg->type ());
731 ULONGEST arg_size = arg_type->length ();
733 if (i == 0 && struct_addr != 0
734 && return_method != return_method_struct
735 && arg_type->code () == TYPE_CODE_PTR
736 && extract_unsigned_integer (arg_bits, 4,
737 byte_order) == struct_addr)
739 /* This argument represents the address at which C++ (and
740 possibly other languages) store their return value.
741 Put this value in R15. */
742 if (write_pass)
743 regcache_cooked_write_unsigned (regcache, RX_R15_REGNUM,
744 struct_addr);
746 else if (arg_type->code () != TYPE_CODE_STRUCT
747 && arg_type->code () != TYPE_CODE_UNION
748 && arg_size <= 8)
750 /* Argument is a scalar. */
751 if (arg_size == 8)
753 if (i < num_register_candidate_args
754 && arg_reg <= RX_R4_REGNUM - 1)
756 /* If argument registers are going to be used to pass
757 an 8 byte scalar, the ABI specifies that two registers
758 must be available. */
759 if (write_pass)
761 regcache_cooked_write_unsigned (regcache, arg_reg,
762 extract_unsigned_integer
763 (arg_bits, 4,
764 byte_order));
765 regcache_cooked_write_unsigned (regcache,
766 arg_reg + 1,
767 extract_unsigned_integer
768 (arg_bits + 4, 4,
769 byte_order));
771 arg_reg += 2;
773 else
775 sp_off = align_up (sp_off, 4);
776 /* Otherwise, pass the 8 byte scalar on the stack. */
777 if (write_pass)
778 write_memory (sp + sp_off, arg_bits, 8);
779 sp_off += 8;
782 else
784 ULONGEST u;
786 gdb_assert (arg_size <= 4);
789 extract_unsigned_integer (arg_bits, arg_size, byte_order);
791 if (i < num_register_candidate_args
792 && arg_reg <= RX_R4_REGNUM)
794 if (write_pass)
795 regcache_cooked_write_unsigned (regcache, arg_reg, u);
796 arg_reg += 1;
798 else
800 int p_arg_size = 4;
802 if (func_type->is_prototyped ()
803 && i < func_type->num_fields ())
805 struct type *p_arg_type =
806 func_type->field (i).type ();
807 p_arg_size = p_arg_type->length ();
810 sp_off = align_up (sp_off, p_arg_size);
812 if (write_pass)
813 write_memory_unsigned_integer (sp + sp_off,
814 p_arg_size, byte_order,
816 sp_off += p_arg_size;
820 else
822 /* Argument is a struct or union. Pass as much of the struct
823 in registers, if possible. Pass the rest on the stack. */
824 while (arg_size > 0)
826 if (i < num_register_candidate_args
827 && arg_reg <= RX_R4_REGNUM
828 && arg_size <= 4 * (RX_R4_REGNUM - arg_reg + 1)
829 && arg_size % 4 == 0)
831 int len = std::min (arg_size, (ULONGEST) 4);
833 if (write_pass)
834 regcache_cooked_write_unsigned (regcache, arg_reg,
835 extract_unsigned_integer
836 (arg_bits, len,
837 byte_order));
838 arg_bits += len;
839 arg_size -= len;
840 arg_reg++;
842 else
844 sp_off = align_up (sp_off, 4);
845 if (write_pass)
846 write_memory (sp + sp_off, arg_bits, arg_size);
847 sp_off += align_up (arg_size, 4);
848 arg_size = 0;
855 /* Keep track of the stack address prior to pushing the return address.
856 This is the value that we'll return. */
857 cfa = sp;
859 /* Push the return address. */
860 sp = sp - 4;
861 write_memory_unsigned_integer (sp, 4, byte_order, bp_addr);
863 /* Update the stack pointer. */
864 regcache_cooked_write_unsigned (regcache, RX_SP_REGNUM, sp);
866 return cfa;
869 /* Implement the "return_value" gdbarch method. */
870 static enum return_value_convention
871 rx_return_value (struct gdbarch *gdbarch,
872 struct value *function,
873 struct type *valtype,
874 struct regcache *regcache,
875 gdb_byte *readbuf, const gdb_byte *writebuf)
877 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
878 ULONGEST valtype_len = valtype->length ();
880 if (valtype->length () > 16
881 || ((valtype->code () == TYPE_CODE_STRUCT
882 || valtype->code () == TYPE_CODE_UNION)
883 && valtype->length () % 4 != 0))
884 return RETURN_VALUE_STRUCT_CONVENTION;
886 if (readbuf)
888 ULONGEST u;
889 int argreg = RX_R1_REGNUM;
890 int offset = 0;
892 while (valtype_len > 0)
894 int len = std::min (valtype_len, (ULONGEST) 4);
896 regcache_cooked_read_unsigned (regcache, argreg, &u);
897 store_unsigned_integer (readbuf + offset, len, byte_order, u);
898 valtype_len -= len;
899 offset += len;
900 argreg++;
904 if (writebuf)
906 ULONGEST u;
907 int argreg = RX_R1_REGNUM;
908 int offset = 0;
910 while (valtype_len > 0)
912 int len = std::min (valtype_len, (ULONGEST) 4);
914 u = extract_unsigned_integer (writebuf + offset, len, byte_order);
915 regcache_cooked_write_unsigned (regcache, argreg, u);
916 valtype_len -= len;
917 offset += len;
918 argreg++;
922 return RETURN_VALUE_REGISTER_CONVENTION;
925 constexpr gdb_byte rx_break_insn[] = { 0x00 };
927 typedef BP_MANIPULATION (rx_break_insn) rx_breakpoint;
929 /* Implement the dwarf_reg_to_regnum" gdbarch method. */
931 static int
932 rx_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
934 if (0 <= reg && reg <= 15)
935 return reg;
936 else if (reg == 16)
937 return RX_PSW_REGNUM;
938 else if (reg == 17)
939 return RX_PC_REGNUM;
940 else
941 return -1;
944 /* Allocate and initialize a gdbarch object. */
945 static struct gdbarch *
946 rx_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
948 int elf_flags;
949 tdesc_arch_data_up tdesc_data;
950 const struct target_desc *tdesc = info.target_desc;
952 /* Extract the elf_flags if available. */
953 if (info.abfd != NULL
954 && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
955 elf_flags = elf_elfheader (info.abfd)->e_flags;
956 else
957 elf_flags = 0;
960 /* Try to find the architecture in the list of already defined
961 architectures. */
962 for (arches = gdbarch_list_lookup_by_info (arches, &info);
963 arches != NULL;
964 arches = gdbarch_list_lookup_by_info (arches->next, &info))
966 rx_gdbarch_tdep *tdep
967 = gdbarch_tdep<rx_gdbarch_tdep> (arches->gdbarch);
969 if (tdep->elf_flags != elf_flags)
970 continue;
972 return arches->gdbarch;
975 if (tdesc == NULL)
976 tdesc = tdesc_rx;
978 /* Check any target description for validity. */
979 if (tdesc_has_registers (tdesc))
981 const struct tdesc_feature *feature;
982 bool valid_p = true;
984 feature = tdesc_find_feature (tdesc, "org.gnu.gdb.rx.core");
986 if (feature != NULL)
988 tdesc_data = tdesc_data_alloc ();
989 for (int i = 0; i < RX_NUM_REGS; i++)
990 valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
991 rx_register_names[i]);
994 if (!valid_p)
995 return NULL;
998 gdb_assert(tdesc_data != NULL);
1000 gdbarch *gdbarch
1001 = gdbarch_alloc (&info, gdbarch_tdep_up (new rx_gdbarch_tdep));
1002 rx_gdbarch_tdep *tdep = gdbarch_tdep<rx_gdbarch_tdep> (gdbarch);
1004 tdep->elf_flags = elf_flags;
1006 set_gdbarch_num_regs (gdbarch, RX_NUM_REGS);
1007 tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
1009 set_gdbarch_num_pseudo_regs (gdbarch, 0);
1010 set_gdbarch_pc_regnum (gdbarch, RX_PC_REGNUM);
1011 set_gdbarch_sp_regnum (gdbarch, RX_SP_REGNUM);
1012 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1013 set_gdbarch_decr_pc_after_break (gdbarch, 1);
1014 set_gdbarch_breakpoint_kind_from_pc (gdbarch, rx_breakpoint::kind_from_pc);
1015 set_gdbarch_sw_breakpoint_from_kind (gdbarch, rx_breakpoint::bp_from_kind);
1016 set_gdbarch_skip_prologue (gdbarch, rx_skip_prologue);
1018 /* Target builtin data types. */
1019 set_gdbarch_char_signed (gdbarch, 0);
1020 set_gdbarch_short_bit (gdbarch, 16);
1021 set_gdbarch_int_bit (gdbarch, 32);
1022 set_gdbarch_long_bit (gdbarch, 32);
1023 set_gdbarch_long_long_bit (gdbarch, 64);
1024 set_gdbarch_ptr_bit (gdbarch, 32);
1025 set_gdbarch_float_bit (gdbarch, 32);
1026 set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
1028 if (elf_flags & E_FLAG_RX_64BIT_DOUBLES)
1030 set_gdbarch_double_bit (gdbarch, 64);
1031 set_gdbarch_long_double_bit (gdbarch, 64);
1032 set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
1033 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
1035 else
1037 set_gdbarch_double_bit (gdbarch, 32);
1038 set_gdbarch_long_double_bit (gdbarch, 32);
1039 set_gdbarch_double_format (gdbarch, floatformats_ieee_single);
1040 set_gdbarch_long_double_format (gdbarch, floatformats_ieee_single);
1043 /* DWARF register mapping. */
1044 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, rx_dwarf_reg_to_regnum);
1046 /* Frame unwinding. */
1047 frame_unwind_append_unwinder (gdbarch, &rx_exception_unwind);
1048 dwarf2_append_unwinders (gdbarch);
1049 frame_unwind_append_unwinder (gdbarch, &rx_frame_unwind);
1051 /* Methods setting up a dummy call, and extracting the return value from
1052 a call. */
1053 set_gdbarch_push_dummy_call (gdbarch, rx_push_dummy_call);
1054 set_gdbarch_return_value (gdbarch, rx_return_value);
1056 /* Virtual tables. */
1057 set_gdbarch_vbit_in_delta (gdbarch, 1);
1059 return gdbarch;
1062 /* Register the above initialization routine. */
1064 void _initialize_rx_tdep ();
1065 void
1066 _initialize_rx_tdep ()
1068 gdbarch_register (bfd_arch_rx, rx_gdbarch_init);
1069 initialize_tdesc_rx ();