1 /* Target-dependent code for the S12Z, for the GDB.
2 Copyright (C) 2018-2024 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 /* Much of this file is shamelessly copied from or1k-tdep.c and others. */
22 #include "arch-utils.h"
23 #include "dwarf2/frame.h"
24 #include "gdbsupport/errors.h"
25 #include "frame-unwind.h"
27 #include "cli/cli-cmds.h"
29 #include "opcode/s12z.h"
30 #include "trad-frame.h"
32 #include "opcodes/s12z-opc.h"
36 /* Two of the registers included in S12Z_N_REGISTERS are
37 the CCH and CCL "registers" which are just views into
39 #define N_PHYSICAL_REGISTERS (S12Z_N_REGISTERS - 2)
42 /* A permutation of all the physical registers. Indexing this array
43 with an integer from gdb's internal representation will return the
45 static const int reg_perm
[N_PHYSICAL_REGISTERS
] =
62 /* The inverse of the above permutation. Indexing this
63 array with a register enum (e.g. REG_D2) will return the register
64 number in gdb's internal representation. */
65 static const int inv_reg_perm
[N_PHYSICAL_REGISTERS
] =
67 2, 3, 4, 5, /* d2, d3, d4, d5 */
70 8, 9, 10, 11, 12 /* x, y, s, p, ccw */
73 /* Return the name of the register REGNUM. */
75 s12z_register_name (struct gdbarch
*gdbarch
, int regnum
)
77 /* Registers is declared in opcodes/s12z.h. */
78 return registers
[reg_perm
[regnum
]].name
;
82 s12z_skip_prologue (struct gdbarch
*gdbarch
, CORE_ADDR pc
)
84 CORE_ADDR start_pc
= 0;
86 if (find_pc_partial_function (pc
, NULL
, &start_pc
, NULL
))
88 CORE_ADDR prologue_end
= skip_prologue_using_sal (gdbarch
, pc
);
90 if (prologue_end
!= 0)
94 warning (_("%s Failed to find end of prologue PC = %08x"),
95 __FUNCTION__
, (unsigned int) pc
);
101 s12z_register_type (struct gdbarch
*gdbarch
, int reg_nr
)
103 switch (registers
[reg_perm
[reg_nr
]].bytes
)
106 return builtin_type (gdbarch
)->builtin_uint8
;
108 return builtin_type (gdbarch
)->builtin_uint16
;
110 return builtin_type (gdbarch
)->builtin_uint24
;
112 return builtin_type (gdbarch
)->builtin_uint32
;
114 return builtin_type (gdbarch
)->builtin_uint32
;
116 return builtin_type (gdbarch
)->builtin_int0
;
121 s12z_dwarf_reg_to_regnum (struct gdbarch
*gdbarch
, int num
)
125 case 15: return REG_S
;
126 case 7: return REG_X
;
127 case 8: return REG_Y
;
128 case 42: return REG_D0
;
129 case 43: return REG_D1
;
130 case 44: return REG_D2
;
131 case 45: return REG_D3
;
132 case 46: return REG_D4
;
133 case 47: return REG_D5
;
134 case 48: return REG_D6
;
135 case 49: return REG_D7
;
141 /* Support functions for frame handling. */
143 /* A struct (based on mem_read_abstraction_base) to read memory
144 through the disassemble_info API. */
145 struct mem_read_abstraction
147 struct mem_read_abstraction_base base
; /* The parent struct. */
148 bfd_vma memaddr
; /* Where to read from. */
149 struct disassemble_info
* info
; /* The disassembler to use for reading. */
152 /* Advance the reader by one byte. */
154 advance (struct mem_read_abstraction_base
*b
)
156 struct mem_read_abstraction
*mra
= (struct mem_read_abstraction
*) b
;
160 /* Return the current position of the reader. */
162 posn (struct mem_read_abstraction_base
*b
)
164 struct mem_read_abstraction
*mra
= (struct mem_read_abstraction
*) b
;
168 /* Read the N bytes at OFFSET using B. The bytes read are stored in BYTES.
169 It is the caller's responsibility to ensure that this is of at least N
172 abstract_read_memory (struct mem_read_abstraction_base
*b
,
174 size_t n
, bfd_byte
*bytes
)
176 struct mem_read_abstraction
*mra
= (struct mem_read_abstraction
*) b
;
179 (*mra
->info
->read_memory_func
) (mra
->memaddr
+ offset
,
180 bytes
, n
, mra
->info
);
184 (*mra
->info
->memory_error_func
) (status
, mra
->memaddr
, mra
->info
);
192 /* Return the stack adjustment caused by a push or pull instruction. */
194 push_pull_get_stack_adjustment (int n_operands
,
195 struct operand
*const *operands
)
197 int stack_adjustment
= 0;
198 gdb_assert (n_operands
> 0);
199 if (operands
[0]->cl
== OPND_CL_REGISTER_ALL
)
200 stack_adjustment
= 26; /* All the regs are involved. */
201 else if (operands
[0]->cl
== OPND_CL_REGISTER_ALL16
)
202 stack_adjustment
= 4 * 2; /* All four 16 bit regs are involved. */
204 for (int i
= 0; i
< n_operands
; ++i
)
206 if (operands
[i
]->cl
!= OPND_CL_REGISTER
)
207 continue; /* I don't think this can ever happen. */
208 const struct register_operand
*op
209 = (const struct register_operand
*) operands
[i
];
214 stack_adjustment
+= 3;
218 stack_adjustment
+= 4;
224 stack_adjustment
+= 2;
230 stack_adjustment
+= 1;
233 gdb_assert_not_reached ("Invalid register in push/pull operation.");
237 return stack_adjustment
;
240 /* Initialize a prologue cache. */
242 static struct trad_frame_cache
*
243 s12z_frame_cache (const frame_info_ptr
&this_frame
, void **prologue_cache
)
245 struct trad_frame_cache
*info
;
248 CORE_ADDR this_sp_for_id
;
250 CORE_ADDR start_addr
;
253 /* Nothing to do if we already have this info. */
254 if (NULL
!= *prologue_cache
)
255 return (struct trad_frame_cache
*) *prologue_cache
;
257 /* Get a new prologue cache and populate it with default values. */
258 info
= trad_frame_cache_zalloc (this_frame
);
259 *prologue_cache
= info
;
261 /* Find the start address of this function (which is a normal frame, even
262 if the next frame is the sentinel frame) and the end of its prologue. */
263 CORE_ADDR this_pc
= get_frame_pc (this_frame
);
264 struct gdbarch
*gdbarch
= get_frame_arch (this_frame
);
265 find_pc_partial_function (this_pc
, NULL
, &start_addr
, NULL
);
267 /* Get the stack pointer if we have one (if there's no process executing
268 yet we won't have a frame. */
269 this_sp
= (NULL
== this_frame
) ? 0 :
270 get_frame_register_unsigned (this_frame
, REG_S
);
272 /* Return early if GDB couldn't find the function. */
275 warning (_("Couldn't find function including address %s SP is %s"),
276 paddress (gdbarch
, this_pc
),
277 paddress (gdbarch
, this_sp
));
279 /* JPB: 28-Apr-11. This is a temporary patch, to get round GDB
280 crashing right at the beginning. Build the frame ID as best we
282 trad_frame_set_id (info
, frame_id_build (this_sp
, this_pc
));
287 /* The default frame base of this frame (for ID purposes only - frame
288 base is an overloaded term) is its stack pointer. For now we use the
289 value of the SP register in this frame. However if the PC is in the
290 prologue of this frame, before the SP has been set up, then the value
291 will actually be that of the prev frame, and we'll need to adjust it
293 trad_frame_set_this_base (info
, this_sp
);
294 this_sp_for_id
= this_sp
;
296 /* We should only examine code that is in the prologue. This is all code
297 up to (but not including) end_addr. We should only populate the cache
298 while the address is up to (but not including) the PC or end_addr,
299 whichever is first. */
300 end_addr
= s12z_skip_prologue (gdbarch
, start_addr
);
302 /* All the following analysis only occurs if we are in the prologue and
303 have executed the code. Check we have a sane prologue size, and if
304 zero we are frameless and can give up here. */
305 if (end_addr
< start_addr
)
306 error (_("end addr %s is less than start addr %s"),
307 paddress (gdbarch
, end_addr
), paddress (gdbarch
, start_addr
));
309 CORE_ADDR addr
= start_addr
; /* Where we have got to? */
311 int saved_frame_size
= 0;
313 struct gdb_non_printing_memory_disassembler
dis (gdbarch
);
315 struct mem_read_abstraction mra
;
316 mra
.base
.read
= (int (*)(mem_read_abstraction_base
*,
317 int, size_t, bfd_byte
*)) abstract_read_memory
;
318 mra
.base
.advance
= advance
;
319 mra
.base
.posn
= posn
;
320 mra
.info
= dis
.disasm_info ();
322 while (this_pc
> addr
)
324 enum optr optr
= OP_INVALID
;
327 struct operand
*operands
[6];
330 decode_s12z (&optr
, &osize
, &n_operands
, operands
,
331 (mem_read_abstraction_base
*) &mra
);
346 /* Conditional Branches. If any of these are encountered, then
347 it is likely that a RTS will terminate it. So we need to save
348 the frame size so it can be restored. */
349 saved_frame_size
= frame_size
;
352 /* Restore the frame size from a previously saved value. */
353 frame_size
= saved_frame_size
;
356 frame_size
+= push_pull_get_stack_adjustment (n_operands
, operands
);
359 frame_size
-= push_pull_get_stack_adjustment (n_operands
, operands
);
362 if (operands
[0]->cl
== OPND_CL_REGISTER
)
364 int reg
= ((struct register_operand
*) (operands
[0]))->reg
;
365 if ((reg
== REG_S
) && (operands
[1]->cl
== OPND_CL_MEMORY
))
367 const struct memory_operand
*mo
368 = (const struct memory_operand
* ) operands
[1];
369 if (mo
->n_regs
== 1 && !mo
->indirect
370 && mo
->regs
[0] == REG_S
371 && mo
->mutation
== OPND_RM_NONE
)
373 /* LEA S, (xxx, S) -- Decrement the stack. This is
374 almost certainly the start of a frame. */
375 int simm
= (signed char) mo
->base_offset
;
385 for (int o
= 0; o
< n_operands
; ++o
)
389 /* If the PC has not actually got to this point, then the frame
390 base will be wrong, and we adjust it. */
393 /* Only do if executing. */
396 this_sp_for_id
= this_sp
- frame_size
;
397 trad_frame_set_this_base (info
, this_sp_for_id
);
399 trad_frame_set_reg_value (info
, REG_S
, this_sp
+ 3);
400 trad_frame_set_reg_addr (info
, REG_P
, this_sp
);
404 gdb_assert (this_sp
== this_sp_for_id
);
405 /* The stack pointer of the prev frame is frame_size greater
406 than the stack pointer of this frame plus one address
407 size (caused by the JSR or BSR). */
408 trad_frame_set_reg_value (info
, REG_S
,
409 this_sp
+ frame_size
+ 3);
410 trad_frame_set_reg_addr (info
, REG_P
, this_sp
+ frame_size
);
414 /* Build the frame ID. */
415 trad_frame_set_id (info
, frame_id_build (this_sp_for_id
, start_addr
));
420 /* Implement the this_id function for the stub unwinder. */
422 s12z_frame_this_id (const frame_info_ptr
&this_frame
,
423 void **prologue_cache
, struct frame_id
*this_id
)
425 struct trad_frame_cache
*info
= s12z_frame_cache (this_frame
,
428 trad_frame_get_id (info
, this_id
);
432 /* Implement the prev_register function for the stub unwinder. */
433 static struct value
*
434 s12z_frame_prev_register (const frame_info_ptr
&this_frame
,
435 void **prologue_cache
, int regnum
)
437 struct trad_frame_cache
*info
= s12z_frame_cache (this_frame
,
440 return trad_frame_get_register (info
, this_frame
, regnum
);
443 /* Data structures for the normal prologue-analysis-based unwinder. */
444 static const struct frame_unwind s12z_frame_unwind
= {
447 default_frame_unwind_stop_reason
,
449 s12z_frame_prev_register
,
451 default_frame_sniffer
,
456 constexpr gdb_byte s12z_break_insn
[] = {0x00};
458 typedef BP_MANIPULATION (s12z_break_insn
) s12z_breakpoint
;
460 struct s12z_gdbarch_tdep
: gdbarch_tdep_base
464 /* A vector of human readable characters representing the
465 bits in the CCW register. Unused bits are represented as '-'.
466 Lowest significant bit comes first. */
467 static const char ccw_bits
[] =
470 'V', /* Two's Complement Overflow */
475 'X', /* Non-Maskable Interrupt */
476 'S', /* STOP Disable */
477 '0', /* Interrupt priority level */
484 'U' /* User/Supervisor State. */
487 /* Print a human readable representation of the CCW register.
488 For example: "u----000SX-Inzvc" corresponds to the value
491 s12z_print_ccw_info (struct gdbarch
*gdbarch
,
492 struct ui_file
*file
,
493 const frame_info_ptr
&frame
,
496 value
*v
= value_of_register (reg
, get_next_frame_sentinel_okay (frame
));
497 const char *name
= gdbarch_register_name (gdbarch
, reg
);
498 uint32_t ccw
= value_as_long (v
);
499 gdb_puts (name
, file
);
500 size_t len
= strlen (name
);
501 const int stop_1
= 15;
502 const int stop_2
= 17;
503 for (int i
= 0; i
< stop_1
- len
; ++i
)
504 gdb_putc (' ', file
);
505 gdb_printf (file
, "0x%04x", ccw
);
506 for (int i
= 0; i
< stop_2
- len
; ++i
)
507 gdb_putc (' ', file
);
508 for (int b
= 15; b
>= 0; --b
)
510 if (ccw
& (0x1u
<< b
))
512 if (ccw_bits
[b
] == 0)
513 gdb_putc ('1', file
);
515 gdb_putc (ccw_bits
[b
], file
);
518 gdb_putc (tolower (ccw_bits
[b
]), file
);
520 gdb_putc ('\n', file
);
524 s12z_print_registers_info (struct gdbarch
*gdbarch
,
525 struct ui_file
*file
,
526 const frame_info_ptr
&frame
,
527 int regnum
, int print_all
)
529 const int numregs
= (gdbarch_num_regs (gdbarch
)
530 + gdbarch_num_pseudo_regs (gdbarch
));
534 for (int reg
= 0; reg
< numregs
; reg
++)
536 if (REG_CCW
== reg_perm
[reg
])
538 s12z_print_ccw_info (gdbarch
, file
, frame
, reg
);
541 default_print_registers_info (gdbarch
, file
, frame
, reg
, print_all
);
544 else if (REG_CCW
== reg_perm
[regnum
])
545 s12z_print_ccw_info (gdbarch
, file
, frame
, regnum
);
547 default_print_registers_info (gdbarch
, file
, frame
, regnum
, print_all
);
554 s12z_extract_return_value (struct type
*type
, struct regcache
*regcache
,
559 switch (type
->length ())
561 case 0: /* Nothing to do */
581 error (_("bad size for return value"));
585 regcache
->cooked_read (inv_reg_perm
[reg
], (gdb_byte
*) valbuf
);
588 static enum return_value_convention
589 s12z_return_value (struct gdbarch
*gdbarch
, struct value
*function
,
590 struct type
*type
, struct regcache
*regcache
,
591 gdb_byte
*readbuf
, const gdb_byte
*writebuf
)
593 if (type
->code () == TYPE_CODE_STRUCT
594 || type
->code () == TYPE_CODE_UNION
595 || type
->code () == TYPE_CODE_ARRAY
596 || type
->length () > 4)
597 return RETURN_VALUE_STRUCT_CONVENTION
;
600 s12z_extract_return_value (type
, regcache
, readbuf
);
602 return RETURN_VALUE_REGISTER_CONVENTION
;
607 show_bdccsr_command (const char *args
, int from_tty
)
609 struct string_file output
;
610 target_rcmd ("bdccsr", &output
);
612 gdb_printf ("The current BDCCSR value is %s\n", output
.string().c_str());
615 static struct gdbarch
*
616 s12z_gdbarch_init (struct gdbarch_info info
, struct gdbarch_list
*arches
)
619 = gdbarch_alloc (&info
, gdbarch_tdep_up (new s12z_gdbarch_tdep
));
621 add_cmd ("bdccsr", class_support
, show_bdccsr_command
,
622 _("Show the current value of the microcontroller's BDCCSR."),
623 &maintenanceinfolist
);
625 /* Target data types. */
626 set_gdbarch_short_bit (gdbarch
, 16);
627 set_gdbarch_int_bit (gdbarch
, 16);
628 set_gdbarch_long_bit (gdbarch
, 32);
629 set_gdbarch_long_long_bit (gdbarch
, 32);
630 set_gdbarch_ptr_bit (gdbarch
, 24);
631 set_gdbarch_addr_bit (gdbarch
, 24);
632 set_gdbarch_char_signed (gdbarch
, 0);
634 set_gdbarch_ps_regnum (gdbarch
, REG_CCW
);
635 set_gdbarch_pc_regnum (gdbarch
, REG_P
);
636 set_gdbarch_sp_regnum (gdbarch
, REG_S
);
639 set_gdbarch_print_registers_info (gdbarch
, s12z_print_registers_info
);
641 set_gdbarch_breakpoint_kind_from_pc (gdbarch
,
642 s12z_breakpoint::kind_from_pc
);
643 set_gdbarch_sw_breakpoint_from_kind (gdbarch
,
644 s12z_breakpoint::bp_from_kind
);
646 set_gdbarch_num_regs (gdbarch
, N_PHYSICAL_REGISTERS
);
647 set_gdbarch_register_name (gdbarch
, s12z_register_name
);
648 set_gdbarch_skip_prologue (gdbarch
, s12z_skip_prologue
);
649 set_gdbarch_inner_than (gdbarch
, core_addr_lessthan
);
650 set_gdbarch_dwarf2_reg_to_regnum (gdbarch
, s12z_dwarf_reg_to_regnum
);
652 set_gdbarch_register_type (gdbarch
, s12z_register_type
);
654 frame_unwind_append_unwinder (gdbarch
, &s12z_frame_unwind
);
655 /* Currently, the only known producer for this architecture, produces buggy
656 dwarf CFI. So don't append a dwarf unwinder until the situation is
657 better understood. */
659 set_gdbarch_return_value (gdbarch
, s12z_return_value
);
664 void _initialize_s12z_tdep ();
666 _initialize_s12z_tdep ()
668 gdbarch_register (bfd_arch_s12z
, s12z_gdbarch_init
, NULL
);