1 /* Target-dependent code for the S12Z, for the GDB.
2 Copyright (C) 2018 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. */
23 #include "arch-utils.h"
24 #include "dwarf2-frame.h"
26 #include "frame-unwind.h"
30 #include "opcode/s12z.h"
31 #include "trad-frame.h"
34 /* Two of the registers included in S12Z_N_REGISTERS are
35 the CCH and CCL "registers" which are just views into
37 #define N_PHYSICAL_REGISTERS (S12Z_N_REGISTERS - 2)
40 /* A permutation of all the physical registers. Indexing this array
41 with an integer from gdb's internal representation will return the
43 static const int reg_perm[N_PHYSICAL_REGISTERS] =
60 /* The inverse of the above permutation. Indexing this
61 array with a register enum (e.g. REG_D2) will return the register
62 number in gdb's internal representation. */
63 static const int inv_reg_perm[N_PHYSICAL_REGISTERS] =
65 2, 3, 4, 5, /* d2, d3, d4, d5 */
68 8, 9, 10, 11, 12 /* x, y, s, p, ccw */
71 /* Return the name of the register REGNUM. */
73 s12z_register_name (struct gdbarch *gdbarch, int regnum)
75 /* Registers is declared in opcodes/s12z.h. */
76 return registers[reg_perm[regnum]].name;
80 s12z_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
82 CORE_ADDR start_pc = 0;
84 if (find_pc_partial_function (pc, NULL, &start_pc, NULL))
86 CORE_ADDR prologue_end = skip_prologue_using_sal (gdbarch, pc);
88 if (prologue_end != 0)
92 warning (_("%s Failed to find end of prologue PC = %08x\n"),
93 __FUNCTION__, (unsigned int) pc);
98 /* Implement the unwind_pc gdbarch method. */
100 s12z_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
102 return frame_unwind_register_unsigned (next_frame, REG_P);
105 /* Implement the unwind_sp gdbarch method. */
107 s12z_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
109 return frame_unwind_register_unsigned (next_frame, REG_S);
113 s12z_register_type (struct gdbarch *gdbarch, int reg_nr)
115 switch (registers[reg_perm[reg_nr]].bytes)
118 return builtin_type (gdbarch)->builtin_uint8;
120 return builtin_type (gdbarch)->builtin_uint16;
122 return builtin_type (gdbarch)->builtin_uint24;
124 return builtin_type (gdbarch)->builtin_uint32;
126 return builtin_type (gdbarch)->builtin_uint32;
128 return builtin_type (gdbarch)->builtin_int0;
133 s12z_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int num)
137 case 15: return REG_S;
138 case 7: return REG_X;
139 case 8: return REG_Y;
140 case 42: return REG_D0;
141 case 43: return REG_D1;
142 case 44: return REG_D2;
143 case 45: return REG_D3;
144 case 46: return REG_D4;
145 case 47: return REG_D5;
146 case 48: return REG_D6;
147 case 49: return REG_D7;
153 /* Support functions for frame handling. */
155 /* Copy of gdb_buffered_insn_length_fprintf from disasm.c. */
157 static int ATTRIBUTE_PRINTF (2, 3)
158 s12z_fprintf_disasm (void *stream, const char *format, ...)
163 struct disassemble_info
164 s12z_disassemble_info (struct gdbarch *gdbarch)
166 struct disassemble_info di;
167 init_disassemble_info (&di, &null_stream, s12z_fprintf_disasm);
168 di.arch = gdbarch_bfd_arch_info (gdbarch)->arch;
169 di.mach = gdbarch_bfd_arch_info (gdbarch)->mach;
170 di.endian = gdbarch_byte_order (gdbarch);
171 di.read_memory_func = [](bfd_vma memaddr, gdb_byte *myaddr,
172 unsigned int len, struct disassemble_info *info)
174 return target_read_code (memaddr, myaddr, len);
179 /* Initialize a prologue cache. */
181 static struct trad_frame_cache *
182 s12z_frame_cache (struct frame_info *this_frame, void **prologue_cache)
184 struct trad_frame_cache *info;
187 CORE_ADDR this_sp_for_id;
189 CORE_ADDR start_addr;
192 /* Nothing to do if we already have this info. */
193 if (NULL != *prologue_cache)
194 return (struct trad_frame_cache *) *prologue_cache;
196 /* Get a new prologue cache and populate it with default values. */
197 info = trad_frame_cache_zalloc (this_frame);
198 *prologue_cache = info;
200 /* Find the start address of this function (which is a normal frame, even
201 if the next frame is the sentinel frame) and the end of its prologue. */
202 CORE_ADDR this_pc = get_frame_pc (this_frame);
203 struct gdbarch *gdbarch = get_frame_arch (this_frame);
204 find_pc_partial_function (this_pc, NULL, &start_addr, NULL);
206 /* Get the stack pointer if we have one (if there's no process executing
207 yet we won't have a frame. */
208 this_sp = (NULL == this_frame) ? 0 :
209 get_frame_register_unsigned (this_frame, REG_S);
211 /* Return early if GDB couldn't find the function. */
214 warning (_("Couldn't find function including address %s SP is %s\n"),
215 paddress (gdbarch, this_pc),
216 paddress (gdbarch, this_sp));
218 /* JPB: 28-Apr-11. This is a temporary patch, to get round GDB
219 crashing right at the beginning. Build the frame ID as best we
221 trad_frame_set_id (info, frame_id_build (this_sp, this_pc));
226 /* The default frame base of this frame (for ID purposes only - frame
227 base is an overloaded term) is its stack pointer. For now we use the
228 value of the SP register in this frame. However if the PC is in the
229 prologue of this frame, before the SP has been set up, then the value
230 will actually be that of the prev frame, and we'll need to adjust it
232 trad_frame_set_this_base (info, this_sp);
233 this_sp_for_id = this_sp;
235 /* We should only examine code that is in the prologue. This is all code
236 up to (but not including) end_addr. We should only populate the cache
237 while the address is up to (but not including) the PC or end_addr,
238 whichever is first. */
239 end_addr = s12z_skip_prologue (gdbarch, start_addr);
241 /* All the following analysis only occurs if we are in the prologue and
242 have executed the code. Check we have a sane prologue size, and if
243 zero we are frameless and can give up here. */
244 if (end_addr < start_addr)
245 error (_("end addr %s is less than start addr %s"),
246 paddress (gdbarch, end_addr), paddress (gdbarch, start_addr));
248 CORE_ADDR addr = start_addr; /* Where we have got to? */
250 int saved_frame_size = 0;
251 while (this_pc > addr)
253 struct disassemble_info di = s12z_disassemble_info (gdbarch);
255 /* No instruction can be more than 11 bytes long, I think. */
258 int nb = print_insn_s12z (addr, &di);
259 gdb_assert (nb <= 11);
261 if (0 != target_read_code (addr, buf, nb))
262 memory_error (TARGET_XFER_E_IO, addr);
264 if (buf[0] == 0x05) /* RTS */
266 frame_size = saved_frame_size;
268 /* Conditional Branches. If any of these are encountered, then
269 it is likely that a RTS will terminate it. So we need to save
270 the frame size so it can be restored. */
271 else if ( (buf[0] == 0x02) /* BRSET */
272 || (buf[0] == 0x0B) /* DBcc / TBcc */
273 || (buf[0] == 0x03)) /* BRCLR */
275 saved_frame_size = frame_size;
277 else if (buf[0] == 0x04) /* PUL/ PSH .. */
279 bool pull = buf[1] & 0x80;
280 int stack_adjustment = 0;
283 if (buf[1] & 0x01) stack_adjustment += 3; /* Y */
284 if (buf[1] & 0x02) stack_adjustment += 3; /* X */
285 if (buf[1] & 0x04) stack_adjustment += 4; /* D7 */
286 if (buf[1] & 0x08) stack_adjustment += 4; /* D6 */
287 if (buf[1] & 0x10) stack_adjustment += 2; /* D5 */
288 if (buf[1] & 0x20) stack_adjustment += 2; /* D4 */
292 if (buf[1] & 0x01) stack_adjustment += 2; /* D3 */
293 if (buf[1] & 0x02) stack_adjustment += 2; /* D2 */
294 if (buf[1] & 0x04) stack_adjustment += 1; /* D1 */
295 if (buf[1] & 0x08) stack_adjustment += 1; /* D0 */
296 if (buf[1] & 0x10) stack_adjustment += 1; /* CCL */
297 if (buf[1] & 0x20) stack_adjustment += 1; /* CCH */
301 stack_adjustment = -stack_adjustment;
302 frame_size -= stack_adjustment;
304 else if (buf[0] == 0x0a) /* LEA S, (xxx, S) */
306 if (0x06 == (buf[1] >> 4))
308 int simm = (signed char) (buf[1] & 0x0F);
312 else if (buf[0] == 0x1a) /* LEA S, (S, xxxx) */
314 int simm = (signed char) buf[1];
320 /* If the PC has not actually got to this point, then the frame
321 base will be wrong, and we adjust it. */
324 /* Only do if executing. */
327 this_sp_for_id = this_sp - frame_size;
328 trad_frame_set_this_base (info, this_sp_for_id);
330 trad_frame_set_reg_value (info, REG_S, this_sp + 3);
331 trad_frame_set_reg_addr (info, REG_P, this_sp);
335 gdb_assert (this_sp == this_sp_for_id);
336 /* The stack pointer of the prev frame is frame_size greater
337 than the stack pointer of this frame plus one address
338 size (caused by the JSR or BSR). */
339 trad_frame_set_reg_value (info, REG_S,
340 this_sp + frame_size + 3);
341 trad_frame_set_reg_addr (info, REG_P, this_sp + frame_size);
345 /* Build the frame ID. */
346 trad_frame_set_id (info, frame_id_build (this_sp_for_id, start_addr));
351 /* Implement the this_id function for the stub unwinder. */
353 s12z_frame_this_id (struct frame_info *this_frame,
354 void **prologue_cache, struct frame_id *this_id)
356 struct trad_frame_cache *info = s12z_frame_cache (this_frame,
359 trad_frame_get_id (info, this_id);
363 /* Implement the prev_register function for the stub unwinder. */
364 static struct value *
365 s12z_frame_prev_register (struct frame_info *this_frame,
366 void **prologue_cache, int regnum)
368 struct trad_frame_cache *info = s12z_frame_cache (this_frame,
371 return trad_frame_get_register (info, this_frame, regnum);
374 /* Data structures for the normal prologue-analysis-based unwinder. */
375 static const struct frame_unwind s12z_frame_unwind = {
377 default_frame_unwind_stop_reason,
379 s12z_frame_prev_register,
381 default_frame_sniffer,
386 constexpr gdb_byte s12z_break_insn[] = {0x00};
388 typedef BP_MANIPULATION (s12z_break_insn) s12z_breakpoint;
394 /* A vector of human readable characters representing the
395 bits in the CCW register. Unused bits are represented as '-'.
396 Lowest significant bit comes first. */
397 static const char ccw_bits[] =
400 'V', /* Two's Complement Overflow */
405 'X', /* Non-Maskable Interrupt */
406 'S', /* STOP Disable */
407 '0', /* Interrupt priority level */
414 'U' /* User/Supervisor State. */
417 /* Print a human readable representation of the CCW register.
418 For example: "u----000SX-Inzvc" corresponds to the value
421 s12z_print_ccw_info (struct gdbarch *gdbarch,
422 struct ui_file *file,
423 struct frame_info *frame,
426 struct value *v = value_of_register (reg, frame);
427 const char *name = gdbarch_register_name (gdbarch, reg);
428 uint32_t ccw = value_as_long (v);
429 fputs_filtered (name, file);
430 size_t len = strlen (name);
431 const int stop_1 = 15;
432 const int stop_2 = 17;
433 for (int i = 0; i < stop_1 - len; ++i)
434 fputc_filtered (' ', file);
435 fprintf_filtered (file, "0x%04x", ccw);
436 for (int i = 0; i < stop_2 - len; ++i)
437 fputc_filtered (' ', file);
438 for (int b = 15; b >= 0; --b)
440 if (ccw & (0x1u << b))
442 if (ccw_bits[b] == 0)
443 fputc_filtered ('1', file);
445 fputc_filtered (ccw_bits[b], file);
448 fputc_filtered (tolower (ccw_bits[b]), file);
450 fputc_filtered ('\n', file);
454 s12z_print_registers_info (struct gdbarch *gdbarch,
455 struct ui_file *file,
456 struct frame_info *frame,
457 int regnum, int print_all)
459 const int numregs = (gdbarch_num_regs (gdbarch)
460 + gdbarch_num_pseudo_regs (gdbarch));
464 for (int reg = 0; reg < numregs; reg++)
466 if (REG_CCW == reg_perm[reg])
468 s12z_print_ccw_info (gdbarch, file, frame, reg);
471 default_print_registers_info (gdbarch, file, frame, reg, print_all);
474 else if (REG_CCW == reg_perm[regnum])
475 s12z_print_ccw_info (gdbarch, file, frame, regnum);
477 default_print_registers_info (gdbarch, file, frame, regnum, print_all);
484 s12z_extract_return_value (struct type *type, struct regcache *regcache,
489 switch (TYPE_LENGTH (type))
491 case 0: /* Nothing to do */
511 error (_("bad size for return value"));
515 regcache->cooked_read (inv_reg_perm[reg], (gdb_byte *) valbuf);
518 static enum return_value_convention
519 s12z_return_value (struct gdbarch *gdbarch, struct value *function,
520 struct type *type, struct regcache *regcache,
521 gdb_byte *readbuf, const gdb_byte *writebuf)
523 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
524 || TYPE_CODE (type) == TYPE_CODE_UNION
525 || TYPE_CODE (type) == TYPE_CODE_ARRAY
526 || TYPE_LENGTH (type) > 4)
527 return RETURN_VALUE_STRUCT_CONVENTION;
530 s12z_extract_return_value (type, regcache, readbuf);
532 return RETURN_VALUE_REGISTER_CONVENTION;
537 show_bdccsr_command (const char *args, int from_tty)
539 struct string_file output;
540 target_rcmd ("bdccsr", &output);
542 printf_unfiltered ("The current BDCCSR value is %s\n", output.string().c_str());
545 static struct gdbarch *
546 s12z_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
548 struct gdbarch_tdep *tdep = XNEW (struct gdbarch_tdep);
549 struct gdbarch *gdbarch = gdbarch_alloc (&info, tdep);
551 add_cmd ("bdccsr", class_support, show_bdccsr_command,
552 _("Show the current value of the microcontroller's BDCCSR."),
553 &maintenanceinfolist);
555 /* Target data types. */
556 set_gdbarch_short_bit (gdbarch, 16);
557 set_gdbarch_int_bit (gdbarch, 16);
558 set_gdbarch_long_bit (gdbarch, 32);
559 set_gdbarch_long_long_bit (gdbarch, 32);
560 set_gdbarch_ptr_bit (gdbarch, 24);
561 set_gdbarch_addr_bit (gdbarch, 24);
562 set_gdbarch_char_signed (gdbarch, 0);
564 set_gdbarch_ps_regnum (gdbarch, REG_CCW);
565 set_gdbarch_pc_regnum (gdbarch, REG_P);
566 set_gdbarch_sp_regnum (gdbarch, REG_S);
569 set_gdbarch_print_registers_info (gdbarch, s12z_print_registers_info);
571 set_gdbarch_breakpoint_kind_from_pc (gdbarch,
572 s12z_breakpoint::kind_from_pc);
573 set_gdbarch_sw_breakpoint_from_kind (gdbarch,
574 s12z_breakpoint::bp_from_kind);
576 set_gdbarch_num_regs (gdbarch, N_PHYSICAL_REGISTERS);
577 set_gdbarch_register_name (gdbarch, s12z_register_name);
578 set_gdbarch_skip_prologue (gdbarch, s12z_skip_prologue);
579 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
580 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, s12z_dwarf_reg_to_regnum);
582 set_gdbarch_register_type (gdbarch, s12z_register_type);
584 /* Functions to access frame data. */
585 set_gdbarch_unwind_pc (gdbarch, s12z_unwind_pc);
586 set_gdbarch_unwind_sp (gdbarch, s12z_unwind_sp);
588 frame_unwind_append_unwinder (gdbarch, &s12z_frame_unwind);
589 /* Currently, the only known producer for this archtecture, produces buggy
590 dwarf CFI. So don't append a dwarf unwinder until the situation is
591 better understood. */
593 set_gdbarch_return_value (gdbarch, s12z_return_value);
599 _initialize_s12z_tdep (void)
601 gdbarch_register (bfd_arch_s12z, s12z_gdbarch_init, NULL);