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