]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/s12z-tdep.c
Unify gdb printf functions
[thirdparty/binutils-gdb.git] / gdb / s12z-tdep.c
1 /* Target-dependent code for the S12Z, for the GDB.
2 Copyright (C) 2018-2022 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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.
10
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.
15
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/>. */
18
19 /* Much of this file is shamelessly copied from or1k-tdep.c and others. */
20
21 #include "defs.h"
22
23 #include "arch-utils.h"
24 #include "dwarf2/frame.h"
25 #include "gdbsupport/errors.h"
26 #include "frame-unwind.h"
27 #include "gdbcore.h"
28 #include "gdbcmd.h"
29 #include "inferior.h"
30 #include "opcode/s12z.h"
31 #include "trad-frame.h"
32 #include "remote.h"
33 #include "opcodes/s12z-opc.h"
34 #include "gdbarch.h"
35
36 /* Two of the registers included in S12Z_N_REGISTERS are
37 the CCH and CCL "registers" which are just views into
38 the CCW register. */
39 #define N_PHYSICAL_REGISTERS (S12Z_N_REGISTERS - 2)
40
41
42 /* A permutation of all the physical registers. Indexing this array
43 with an integer from gdb's internal representation will return the
44 register enum. */
45 static const int reg_perm[N_PHYSICAL_REGISTERS] =
46 {
47 REG_D0,
48 REG_D1,
49 REG_D2,
50 REG_D3,
51 REG_D4,
52 REG_D5,
53 REG_D6,
54 REG_D7,
55 REG_X,
56 REG_Y,
57 REG_S,
58 REG_P,
59 REG_CCW
60 };
61
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] =
66 {
67 2, 3, 4, 5, /* d2, d3, d4, d5 */
68 0, 1, /* d0, d1 */
69 6, 7, /* d6, d7 */
70 8, 9, 10, 11, 12 /* x, y, s, p, ccw */
71 };
72
73 /* Return the name of the register REGNUM. */
74 static const char *
75 s12z_register_name (struct gdbarch *gdbarch, int regnum)
76 {
77 /* Registers is declared in opcodes/s12z.h. */
78 return registers[reg_perm[regnum]].name;
79 }
80
81 static CORE_ADDR
82 s12z_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
83 {
84 CORE_ADDR start_pc = 0;
85
86 if (find_pc_partial_function (pc, NULL, &start_pc, NULL))
87 {
88 CORE_ADDR prologue_end = skip_prologue_using_sal (gdbarch, pc);
89
90 if (prologue_end != 0)
91 return prologue_end;
92 }
93
94 warning (_("%s Failed to find end of prologue PC = %08x"),
95 __FUNCTION__, (unsigned int) pc);
96
97 return pc;
98 }
99
100 static struct type *
101 s12z_register_type (struct gdbarch *gdbarch, int reg_nr)
102 {
103 switch (registers[reg_perm[reg_nr]].bytes)
104 {
105 case 1:
106 return builtin_type (gdbarch)->builtin_uint8;
107 case 2:
108 return builtin_type (gdbarch)->builtin_uint16;
109 case 3:
110 return builtin_type (gdbarch)->builtin_uint24;
111 case 4:
112 return builtin_type (gdbarch)->builtin_uint32;
113 default:
114 return builtin_type (gdbarch)->builtin_uint32;
115 }
116 return builtin_type (gdbarch)->builtin_int0;
117 }
118
119
120 static int
121 s12z_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int num)
122 {
123 switch (num)
124 {
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;
136 }
137 return -1;
138 }
139
140
141 /* Support functions for frame handling. */
142
143
144 /* Return a disassemble_info initialized for s12z disassembly, however,
145 the disassembler will not actually print anything. */
146
147 static struct disassemble_info
148 s12z_disassemble_info (struct gdbarch *gdbarch)
149 {
150 struct disassemble_info di;
151 init_disassemble_info_for_no_printing (&di);
152 di.arch = gdbarch_bfd_arch_info (gdbarch)->arch;
153 di.mach = gdbarch_bfd_arch_info (gdbarch)->mach;
154 di.endian = gdbarch_byte_order (gdbarch);
155 di.read_memory_func = [](bfd_vma memaddr, gdb_byte *myaddr,
156 unsigned int len, struct disassemble_info *info)
157 {
158 return target_read_code (memaddr, myaddr, len);
159 };
160 return di;
161 }
162
163
164 /* A struct (based on mem_read_abstraction_base) to read memory
165 through the disassemble_info API. */
166 struct mem_read_abstraction
167 {
168 struct mem_read_abstraction_base base; /* The parent struct. */
169 bfd_vma memaddr; /* Where to read from. */
170 struct disassemble_info* info; /* The disassembler to use for reading. */
171 };
172
173 /* Advance the reader by one byte. */
174 static void
175 advance (struct mem_read_abstraction_base *b)
176 {
177 struct mem_read_abstraction *mra = (struct mem_read_abstraction *) b;
178 mra->memaddr++;
179 }
180
181 /* Return the current position of the reader. */
182 static bfd_vma
183 posn (struct mem_read_abstraction_base *b)
184 {
185 struct mem_read_abstraction *mra = (struct mem_read_abstraction *) b;
186 return mra->memaddr;
187 }
188
189 /* Read the N bytes at OFFSET using B. The bytes read are stored in BYTES.
190 It is the caller's responsibility to ensure that this is of at least N
191 in size. */
192 static int
193 abstract_read_memory (struct mem_read_abstraction_base *b,
194 int offset,
195 size_t n, bfd_byte *bytes)
196 {
197 struct mem_read_abstraction *mra = (struct mem_read_abstraction *) b;
198
199 int status =
200 (*mra->info->read_memory_func) (mra->memaddr + offset,
201 bytes, n, mra->info);
202
203 if (status != 0)
204 {
205 (*mra->info->memory_error_func) (status, mra->memaddr, mra->info);
206 return -1;
207 }
208
209 return 0;
210 }
211
212
213 /* Return the stack adjustment caused by a push or pull instruction. */
214 static int
215 push_pull_get_stack_adjustment (int n_operands,
216 struct operand *const *operands)
217 {
218 int stack_adjustment = 0;
219 gdb_assert (n_operands > 0);
220 if (operands[0]->cl == OPND_CL_REGISTER_ALL)
221 stack_adjustment = 26; /* All the regs are involved. */
222 else if (operands[0]->cl == OPND_CL_REGISTER_ALL16)
223 stack_adjustment = 4 * 2; /* All four 16 bit regs are involved. */
224 else
225 for (int i = 0; i < n_operands; ++i)
226 {
227 if (operands[i]->cl != OPND_CL_REGISTER)
228 continue; /* I don't think this can ever happen. */
229 const struct register_operand *op
230 = (const struct register_operand *) operands[i];
231 switch (op->reg)
232 {
233 case REG_X:
234 case REG_Y:
235 stack_adjustment += 3;
236 break;
237 case REG_D7:
238 case REG_D6:
239 stack_adjustment += 4;
240 break;
241 case REG_D2:
242 case REG_D3:
243 case REG_D4:
244 case REG_D5:
245 stack_adjustment += 2;
246 break;
247 case REG_D0:
248 case REG_D1:
249 case REG_CCL:
250 case REG_CCH:
251 stack_adjustment += 1;
252 break;
253 default:
254 gdb_assert_not_reached ("Invalid register in push/pull operation.");
255 break;
256 }
257 }
258 return stack_adjustment;
259 }
260
261 /* Initialize a prologue cache. */
262
263 static struct trad_frame_cache *
264 s12z_frame_cache (struct frame_info *this_frame, void **prologue_cache)
265 {
266 struct trad_frame_cache *info;
267
268 CORE_ADDR this_sp;
269 CORE_ADDR this_sp_for_id;
270
271 CORE_ADDR start_addr;
272 CORE_ADDR end_addr;
273
274 /* Nothing to do if we already have this info. */
275 if (NULL != *prologue_cache)
276 return (struct trad_frame_cache *) *prologue_cache;
277
278 /* Get a new prologue cache and populate it with default values. */
279 info = trad_frame_cache_zalloc (this_frame);
280 *prologue_cache = info;
281
282 /* Find the start address of this function (which is a normal frame, even
283 if the next frame is the sentinel frame) and the end of its prologue. */
284 CORE_ADDR this_pc = get_frame_pc (this_frame);
285 struct gdbarch *gdbarch = get_frame_arch (this_frame);
286 find_pc_partial_function (this_pc, NULL, &start_addr, NULL);
287
288 /* Get the stack pointer if we have one (if there's no process executing
289 yet we won't have a frame. */
290 this_sp = (NULL == this_frame) ? 0 :
291 get_frame_register_unsigned (this_frame, REG_S);
292
293 /* Return early if GDB couldn't find the function. */
294 if (start_addr == 0)
295 {
296 warning (_("Couldn't find function including address %s SP is %s"),
297 paddress (gdbarch, this_pc),
298 paddress (gdbarch, this_sp));
299
300 /* JPB: 28-Apr-11. This is a temporary patch, to get round GDB
301 crashing right at the beginning. Build the frame ID as best we
302 can. */
303 trad_frame_set_id (info, frame_id_build (this_sp, this_pc));
304
305 return info;
306 }
307
308 /* The default frame base of this frame (for ID purposes only - frame
309 base is an overloaded term) is its stack pointer. For now we use the
310 value of the SP register in this frame. However if the PC is in the
311 prologue of this frame, before the SP has been set up, then the value
312 will actually be that of the prev frame, and we'll need to adjust it
313 later. */
314 trad_frame_set_this_base (info, this_sp);
315 this_sp_for_id = this_sp;
316
317 /* We should only examine code that is in the prologue. This is all code
318 up to (but not including) end_addr. We should only populate the cache
319 while the address is up to (but not including) the PC or end_addr,
320 whichever is first. */
321 end_addr = s12z_skip_prologue (gdbarch, start_addr);
322
323 /* All the following analysis only occurs if we are in the prologue and
324 have executed the code. Check we have a sane prologue size, and if
325 zero we are frameless and can give up here. */
326 if (end_addr < start_addr)
327 error (_("end addr %s is less than start addr %s"),
328 paddress (gdbarch, end_addr), paddress (gdbarch, start_addr));
329
330 CORE_ADDR addr = start_addr; /* Where we have got to? */
331 int frame_size = 0;
332 int saved_frame_size = 0;
333
334 struct disassemble_info di = s12z_disassemble_info (gdbarch);
335
336
337 struct mem_read_abstraction mra;
338 mra.base.read = (int (*)(mem_read_abstraction_base*,
339 int, size_t, bfd_byte*)) abstract_read_memory;
340 mra.base.advance = advance ;
341 mra.base.posn = posn;
342 mra.info = &di;
343
344 while (this_pc > addr)
345 {
346 enum optr optr = OP_INVALID;
347 short osize;
348 int n_operands = 0;
349 struct operand *operands[6];
350 mra.memaddr = addr;
351 int n_bytes =
352 decode_s12z (&optr, &osize, &n_operands, operands,
353 (mem_read_abstraction_base *) &mra);
354
355 switch (optr)
356 {
357 case OP_tbNE:
358 case OP_tbPL:
359 case OP_tbMI:
360 case OP_tbGT:
361 case OP_tbLE:
362 case OP_dbNE:
363 case OP_dbEQ:
364 case OP_dbPL:
365 case OP_dbMI:
366 case OP_dbGT:
367 case OP_dbLE:
368 /* Conditional Branches. If any of these are encountered, then
369 it is likely that a RTS will terminate it. So we need to save
370 the frame size so it can be restored. */
371 saved_frame_size = frame_size;
372 break;
373 case OP_rts:
374 /* Restore the frame size from a previously saved value. */
375 frame_size = saved_frame_size;
376 break;
377 case OP_push:
378 frame_size += push_pull_get_stack_adjustment (n_operands, operands);
379 break;
380 case OP_pull:
381 frame_size -= push_pull_get_stack_adjustment (n_operands, operands);
382 break;
383 case OP_lea:
384 if (operands[0]->cl == OPND_CL_REGISTER)
385 {
386 int reg = ((struct register_operand *) (operands[0]))->reg;
387 if ((reg == REG_S) && (operands[1]->cl == OPND_CL_MEMORY))
388 {
389 const struct memory_operand *mo
390 = (const struct memory_operand * ) operands[1];
391 if (mo->n_regs == 1 && !mo->indirect
392 && mo->regs[0] == REG_S
393 && mo->mutation == OPND_RM_NONE)
394 {
395 /* LEA S, (xxx, S) -- Decrement the stack. This is
396 almost certainly the start of a frame. */
397 int simm = (signed char) mo->base_offset;
398 frame_size -= simm;
399 }
400 }
401 }
402 break;
403 default:
404 break;
405 }
406 addr += n_bytes;
407 for (int o = 0; o < n_operands; ++o)
408 free (operands[o]);
409 }
410
411 /* If the PC has not actually got to this point, then the frame
412 base will be wrong, and we adjust it. */
413 if (this_pc < addr)
414 {
415 /* Only do if executing. */
416 if (0 != this_sp)
417 {
418 this_sp_for_id = this_sp - frame_size;
419 trad_frame_set_this_base (info, this_sp_for_id);
420 }
421 trad_frame_set_reg_value (info, REG_S, this_sp + 3);
422 trad_frame_set_reg_addr (info, REG_P, this_sp);
423 }
424 else
425 {
426 gdb_assert (this_sp == this_sp_for_id);
427 /* The stack pointer of the prev frame is frame_size greater
428 than the stack pointer of this frame plus one address
429 size (caused by the JSR or BSR). */
430 trad_frame_set_reg_value (info, REG_S,
431 this_sp + frame_size + 3);
432 trad_frame_set_reg_addr (info, REG_P, this_sp + frame_size);
433 }
434
435
436 /* Build the frame ID. */
437 trad_frame_set_id (info, frame_id_build (this_sp_for_id, start_addr));
438
439 return info;
440 }
441
442 /* Implement the this_id function for the stub unwinder. */
443 static void
444 s12z_frame_this_id (struct frame_info *this_frame,
445 void **prologue_cache, struct frame_id *this_id)
446 {
447 struct trad_frame_cache *info = s12z_frame_cache (this_frame,
448 prologue_cache);
449
450 trad_frame_get_id (info, this_id);
451 }
452
453
454 /* Implement the prev_register function for the stub unwinder. */
455 static struct value *
456 s12z_frame_prev_register (struct frame_info *this_frame,
457 void **prologue_cache, int regnum)
458 {
459 struct trad_frame_cache *info = s12z_frame_cache (this_frame,
460 prologue_cache);
461
462 return trad_frame_get_register (info, this_frame, regnum);
463 }
464
465 /* Data structures for the normal prologue-analysis-based unwinder. */
466 static const struct frame_unwind s12z_frame_unwind = {
467 "s12z prologue",
468 NORMAL_FRAME,
469 default_frame_unwind_stop_reason,
470 s12z_frame_this_id,
471 s12z_frame_prev_register,
472 NULL,
473 default_frame_sniffer,
474 NULL,
475 };
476
477
478 constexpr gdb_byte s12z_break_insn[] = {0x00};
479
480 typedef BP_MANIPULATION (s12z_break_insn) s12z_breakpoint;
481
482 struct s12z_gdbarch_tdep : gdbarch_tdep
483 {
484 };
485
486 /* A vector of human readable characters representing the
487 bits in the CCW register. Unused bits are represented as '-'.
488 Lowest significant bit comes first. */
489 static const char ccw_bits[] =
490 {
491 'C', /* Carry */
492 'V', /* Two's Complement Overflow */
493 'Z', /* Zero */
494 'N', /* Negative */
495 'I', /* Interrupt */
496 '-',
497 'X', /* Non-Maskable Interrupt */
498 'S', /* STOP Disable */
499 '0', /* Interrupt priority level */
500 '0', /* ditto */
501 '0', /* ditto */
502 '-',
503 '-',
504 '-',
505 '-',
506 'U' /* User/Supervisor State. */
507 };
508
509 /* Print a human readable representation of the CCW register.
510 For example: "u----000SX-Inzvc" corresponds to the value
511 0xD0. */
512 static void
513 s12z_print_ccw_info (struct gdbarch *gdbarch,
514 struct ui_file *file,
515 struct frame_info *frame,
516 int reg)
517 {
518 struct value *v = value_of_register (reg, frame);
519 const char *name = gdbarch_register_name (gdbarch, reg);
520 uint32_t ccw = value_as_long (v);
521 gdb_puts (name, file);
522 size_t len = strlen (name);
523 const int stop_1 = 15;
524 const int stop_2 = 17;
525 for (int i = 0; i < stop_1 - len; ++i)
526 gdb_putc (' ', file);
527 gdb_printf (file, "0x%04x", ccw);
528 for (int i = 0; i < stop_2 - len; ++i)
529 gdb_putc (' ', file);
530 for (int b = 15; b >= 0; --b)
531 {
532 if (ccw & (0x1u << b))
533 {
534 if (ccw_bits[b] == 0)
535 gdb_putc ('1', file);
536 else
537 gdb_putc (ccw_bits[b], file);
538 }
539 else
540 gdb_putc (tolower (ccw_bits[b]), file);
541 }
542 gdb_putc ('\n', file);
543 }
544
545 static void
546 s12z_print_registers_info (struct gdbarch *gdbarch,
547 struct ui_file *file,
548 struct frame_info *frame,
549 int regnum, int print_all)
550 {
551 const int numregs = (gdbarch_num_regs (gdbarch)
552 + gdbarch_num_pseudo_regs (gdbarch));
553
554 if (regnum == -1)
555 {
556 for (int reg = 0; reg < numregs; reg++)
557 {
558 if (REG_CCW == reg_perm[reg])
559 {
560 s12z_print_ccw_info (gdbarch, file, frame, reg);
561 continue;
562 }
563 default_print_registers_info (gdbarch, file, frame, reg, print_all);
564 }
565 }
566 else if (REG_CCW == reg_perm[regnum])
567 s12z_print_ccw_info (gdbarch, file, frame, regnum);
568 else
569 default_print_registers_info (gdbarch, file, frame, regnum, print_all);
570 }
571
572 \f
573
574
575 static void
576 s12z_extract_return_value (struct type *type, struct regcache *regcache,
577 void *valbuf)
578 {
579 int reg = -1;
580
581 switch (TYPE_LENGTH (type))
582 {
583 case 0: /* Nothing to do */
584 return;
585
586 case 1:
587 reg = REG_D0;
588 break;
589
590 case 2:
591 reg = REG_D2;
592 break;
593
594 case 3:
595 reg = REG_X;
596 break;
597
598 case 4:
599 reg = REG_D6;
600 break;
601
602 default:
603 error (_("bad size for return value"));
604 return;
605 }
606
607 regcache->cooked_read (inv_reg_perm[reg], (gdb_byte *) valbuf);
608 }
609
610 static enum return_value_convention
611 s12z_return_value (struct gdbarch *gdbarch, struct value *function,
612 struct type *type, struct regcache *regcache,
613 gdb_byte *readbuf, const gdb_byte *writebuf)
614 {
615 if (type->code () == TYPE_CODE_STRUCT
616 || type->code () == TYPE_CODE_UNION
617 || type->code () == TYPE_CODE_ARRAY
618 || TYPE_LENGTH (type) > 4)
619 return RETURN_VALUE_STRUCT_CONVENTION;
620
621 if (readbuf)
622 s12z_extract_return_value (type, regcache, readbuf);
623
624 return RETURN_VALUE_REGISTER_CONVENTION;
625 }
626
627
628 static void
629 show_bdccsr_command (const char *args, int from_tty)
630 {
631 struct string_file output;
632 target_rcmd ("bdccsr", &output);
633
634 gdb_printf ("The current BDCCSR value is %s\n", output.string().c_str());
635 }
636
637 static struct gdbarch *
638 s12z_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
639 {
640 s12z_gdbarch_tdep *tdep = new s12z_gdbarch_tdep;
641 struct gdbarch *gdbarch = gdbarch_alloc (&info, tdep);
642
643 add_cmd ("bdccsr", class_support, show_bdccsr_command,
644 _("Show the current value of the microcontroller's BDCCSR."),
645 &maintenanceinfolist);
646
647 /* Target data types. */
648 set_gdbarch_short_bit (gdbarch, 16);
649 set_gdbarch_int_bit (gdbarch, 16);
650 set_gdbarch_long_bit (gdbarch, 32);
651 set_gdbarch_long_long_bit (gdbarch, 32);
652 set_gdbarch_ptr_bit (gdbarch, 24);
653 set_gdbarch_addr_bit (gdbarch, 24);
654 set_gdbarch_char_signed (gdbarch, 0);
655
656 set_gdbarch_ps_regnum (gdbarch, REG_CCW);
657 set_gdbarch_pc_regnum (gdbarch, REG_P);
658 set_gdbarch_sp_regnum (gdbarch, REG_S);
659
660
661 set_gdbarch_print_registers_info (gdbarch, s12z_print_registers_info);
662
663 set_gdbarch_breakpoint_kind_from_pc (gdbarch,
664 s12z_breakpoint::kind_from_pc);
665 set_gdbarch_sw_breakpoint_from_kind (gdbarch,
666 s12z_breakpoint::bp_from_kind);
667
668 set_gdbarch_num_regs (gdbarch, N_PHYSICAL_REGISTERS);
669 set_gdbarch_register_name (gdbarch, s12z_register_name);
670 set_gdbarch_skip_prologue (gdbarch, s12z_skip_prologue);
671 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
672 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, s12z_dwarf_reg_to_regnum);
673
674 set_gdbarch_register_type (gdbarch, s12z_register_type);
675
676 frame_unwind_append_unwinder (gdbarch, &s12z_frame_unwind);
677 /* Currently, the only known producer for this architecture, produces buggy
678 dwarf CFI. So don't append a dwarf unwinder until the situation is
679 better understood. */
680
681 set_gdbarch_return_value (gdbarch, s12z_return_value);
682
683 return gdbarch;
684 }
685
686 void _initialize_s12z_tdep ();
687 void
688 _initialize_s12z_tdep ()
689 {
690 gdbarch_register (bfd_arch_s12z, s12z_gdbarch_init, NULL);
691 }