Set GDB version number to 8.3.1.
[external/binutils.git] / gdb / s12z-tdep.c
1 /* Target-dependent code for the S12Z, for the GDB.
2    Copyright (C) 2018-2019 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 "common/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
34 /* Two of the registers included in S12Z_N_REGISTERS are
35    the CCH and CCL "registers" which are just views into
36    the CCW register.  */
37 #define N_PHYSICAL_REGISTERS (S12Z_N_REGISTERS - 2)
38
39
40 /*  A permutation of all the physical registers.   Indexing this array
41     with an integer from gdb's internal representation will return the
42     register enum.  */
43 static const int reg_perm[N_PHYSICAL_REGISTERS] =
44   {
45    REG_D0,
46    REG_D1,
47    REG_D2,
48    REG_D3,
49    REG_D4,
50    REG_D5,
51    REG_D6,
52    REG_D7,
53    REG_X,
54    REG_Y,
55    REG_S,
56    REG_P,
57    REG_CCW
58   };
59
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] =
64   {
65    2, 3, 4, 5,      /* d2, d3, d4, d5 */
66    0, 1,            /* d0, d1 */
67    6, 7,            /* d6, d7 */
68    8, 9, 10, 11, 12 /* x, y, s, p, ccw */
69   };
70
71 /*  Return the name of the register REGNUM.  */
72 static const char *
73 s12z_register_name (struct gdbarch *gdbarch, int regnum)
74 {
75   /*  Registers is declared in opcodes/s12z.h.   */
76   return registers[reg_perm[regnum]].name;
77 }
78
79 static CORE_ADDR
80 s12z_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
81 {
82   CORE_ADDR start_pc = 0;
83
84   if (find_pc_partial_function (pc, NULL, &start_pc, NULL))
85     {
86       CORE_ADDR prologue_end = skip_prologue_using_sal (gdbarch, pc);
87
88       if (prologue_end != 0)
89         return prologue_end;
90     }
91
92   warning (_("%s Failed to find end of prologue PC = %08x\n"),
93                       __FUNCTION__, (unsigned int) pc);
94
95   return pc;
96 }
97
98 /* Implement the unwind_pc gdbarch method.  */
99 static CORE_ADDR
100 s12z_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
101 {
102   return frame_unwind_register_unsigned (next_frame, REG_P);
103 }
104
105 /* Implement the unwind_sp gdbarch method.  */
106 static CORE_ADDR
107 s12z_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
108 {
109   return frame_unwind_register_unsigned (next_frame, REG_S);
110 }
111
112 static struct type *
113 s12z_register_type (struct gdbarch *gdbarch, int reg_nr)
114 {
115   switch (registers[reg_perm[reg_nr]].bytes)
116     {
117     case 1:
118       return builtin_type (gdbarch)->builtin_uint8;
119     case 2:
120       return builtin_type (gdbarch)->builtin_uint16;
121     case 3:
122       return builtin_type (gdbarch)->builtin_uint24;
123     case 4:
124       return builtin_type (gdbarch)->builtin_uint32;
125     default:
126       return builtin_type (gdbarch)->builtin_uint32;
127     }
128   return builtin_type (gdbarch)->builtin_int0;
129 }
130
131
132 static int
133 s12z_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int num)
134 {
135   switch (num)
136     {
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;
148     }
149   return -1;
150 }
151
152
153 /* Support functions for frame handling.  */
154
155 /* Copy of gdb_buffered_insn_length_fprintf from disasm.c.  */
156
157 static int ATTRIBUTE_PRINTF (2, 3)
158 s12z_fprintf_disasm (void *stream, const char *format, ...)
159 {
160   return 0;
161 }
162
163 struct disassemble_info
164 s12z_disassemble_info (struct gdbarch *gdbarch)
165 {
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)
173     {
174       return target_read_code (memaddr, myaddr, len);
175     };
176   return di;
177 }
178
179 /* Initialize a prologue cache.  */
180
181 static struct trad_frame_cache *
182 s12z_frame_cache (struct frame_info *this_frame, void **prologue_cache)
183 {
184   struct trad_frame_cache *info;
185
186   CORE_ADDR this_sp;
187   CORE_ADDR this_sp_for_id;
188
189   CORE_ADDR start_addr;
190   CORE_ADDR end_addr;
191
192   /* Nothing to do if we already have this info.  */
193   if (NULL != *prologue_cache)
194     return (struct trad_frame_cache *) *prologue_cache;
195
196   /* Get a new prologue cache and populate it with default values.  */
197   info = trad_frame_cache_zalloc (this_frame);
198   *prologue_cache = info;
199
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);
205
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);
210
211   /* Return early if GDB couldn't find the function.  */
212   if (start_addr == 0)
213     {
214       warning (_("Couldn't find function including address %s SP is %s\n"),
215                paddress (gdbarch, this_pc),
216                paddress (gdbarch, this_sp));
217
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
220          can.  */
221       trad_frame_set_id (info, frame_id_build (this_sp, this_pc));
222
223       return info;
224     }
225
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
231      later.  */
232   trad_frame_set_this_base (info, this_sp);
233   this_sp_for_id = this_sp;
234
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);
240
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));
247
248   CORE_ADDR addr = start_addr; /* Where we have got to?  */
249   int frame_size = 0;
250   int saved_frame_size = 0;
251   while (this_pc > addr)
252     {
253       struct disassemble_info di = s12z_disassemble_info (gdbarch);
254
255       /* No instruction can be more than 11 bytes long, I think.  */
256       gdb_byte buf[11];
257
258       int nb = print_insn_s12z (addr, &di);
259       gdb_assert (nb <= 11);
260
261       if (0 != target_read_code (addr, buf, nb))
262         memory_error (TARGET_XFER_E_IO, addr);
263
264       if (buf[0] == 0x05)        /* RTS */
265         {
266           frame_size = saved_frame_size;
267         }
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 */
274         {
275           saved_frame_size = frame_size;
276         }
277       else if (buf[0] == 0x04)        /* PUL/ PSH .. */
278         {
279           bool pull = buf[1] & 0x80;
280           int stack_adjustment = 0;
281           if (buf[1] & 0x40)
282             {
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 */
289             }
290           else
291             {
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 */
298             }
299
300           if (!pull)
301             stack_adjustment = -stack_adjustment;
302           frame_size -= stack_adjustment;
303         }
304       else if (buf[0] == 0x0a)   /* LEA S, (xxx, S) */
305         {
306           if (0x06 == (buf[1] >> 4))
307             {
308               int simm = (signed char) (buf[1] & 0x0F);
309               frame_size -= simm;
310             }
311         }
312       else if (buf[0] == 0x1a)   /* LEA S, (S, xxxx) */
313         {
314           int simm = (signed char) buf[1];
315           frame_size -= simm;
316         }
317       addr += nb;
318     }
319
320   /* If the PC has not actually got to this point, then the frame
321      base will be wrong, and we adjust it. */
322   if (this_pc < addr)
323     {
324       /* Only do if executing.  */
325       if (0 != this_sp)
326         {
327           this_sp_for_id = this_sp - frame_size;
328           trad_frame_set_this_base (info, this_sp_for_id);
329         }
330       trad_frame_set_reg_value (info, REG_S, this_sp + 3);
331       trad_frame_set_reg_addr (info, REG_P, this_sp);
332     }
333   else
334     {
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);
342     }
343
344
345   /* Build the frame ID.  */
346   trad_frame_set_id (info, frame_id_build (this_sp_for_id, start_addr));
347
348   return info;
349 }
350
351 /* Implement the this_id function for the stub unwinder.  */
352 static void
353 s12z_frame_this_id (struct frame_info *this_frame,
354                     void **prologue_cache, struct frame_id *this_id)
355 {
356   struct trad_frame_cache *info = s12z_frame_cache (this_frame,
357                                                     prologue_cache);
358
359   trad_frame_get_id (info, this_id);
360 }
361
362
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)
367 {
368   struct trad_frame_cache *info = s12z_frame_cache (this_frame,
369                                                     prologue_cache);
370
371   return trad_frame_get_register (info, this_frame, regnum);
372 }
373
374 /* Data structures for the normal prologue-analysis-based unwinder.  */
375 static const struct frame_unwind s12z_frame_unwind = {
376   NORMAL_FRAME,
377   default_frame_unwind_stop_reason,
378   s12z_frame_this_id,
379   s12z_frame_prev_register,
380   NULL,
381   default_frame_sniffer,
382   NULL,
383 };
384
385
386 constexpr gdb_byte s12z_break_insn[] = {0x00};
387
388 typedef BP_MANIPULATION (s12z_break_insn) s12z_breakpoint;
389
390 struct gdbarch_tdep
391 {
392 };
393
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[] =
398   {
399    'C',  /* Carry  */
400    'V',  /* Two's Complement Overflow  */
401    'Z',  /* Zero  */
402    'N',  /* Negative  */
403    'I',  /* Interrupt  */
404    '-',
405    'X',  /* Non-Maskable Interrupt  */
406    'S',  /* STOP Disable  */
407    '0',  /*  Interrupt priority level */
408    '0',  /*  ditto  */
409    '0',  /*  ditto  */
410    '-',
411    '-',
412    '-',
413    '-',
414    'U'   /* User/Supervisor State.  */
415   };
416
417 /* Print a human readable representation of the CCW register.
418    For example: "u----000SX-Inzvc" corresponds to the value
419    0xD0.  */
420 static void
421 s12z_print_ccw_info (struct gdbarch *gdbarch,
422                      struct ui_file *file,
423                      struct frame_info *frame,
424                      int reg)
425 {
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)
439     {
440       if (ccw & (0x1u << b))
441         {
442           if (ccw_bits[b] == 0)
443             fputc_filtered ('1', file);
444           else
445             fputc_filtered (ccw_bits[b], file);
446         }
447       else
448         fputc_filtered (tolower (ccw_bits[b]), file);
449     }
450   fputc_filtered ('\n', file);
451 }
452
453 static void
454 s12z_print_registers_info (struct gdbarch *gdbarch,
455                             struct ui_file *file,
456                             struct frame_info *frame,
457                             int regnum, int print_all)
458 {
459   const int numregs = (gdbarch_num_regs (gdbarch)
460                        + gdbarch_num_pseudo_regs (gdbarch));
461
462   if (regnum == -1)
463     {
464       for (int reg = 0; reg < numregs; reg++)
465         {
466           if (REG_CCW == reg_perm[reg])
467             {
468               s12z_print_ccw_info (gdbarch, file, frame, reg);
469               continue;
470             }
471           default_print_registers_info (gdbarch, file, frame, reg, print_all);
472         }
473     }
474   else if (REG_CCW == reg_perm[regnum])
475     s12z_print_ccw_info (gdbarch, file, frame, regnum);
476   else
477     default_print_registers_info (gdbarch, file, frame, regnum, print_all);
478 }
479
480 \f
481
482
483 static void
484 s12z_extract_return_value (struct type *type, struct regcache *regcache,
485                               void *valbuf)
486 {
487   int reg = -1;
488
489   switch (TYPE_LENGTH (type))
490     {
491     case 0:   /* Nothing to do */
492       return;
493
494     case 1:
495       reg = REG_D0;
496       break;
497
498     case 2:
499       reg = REG_D2;
500       break;
501
502     case 3:
503       reg = REG_X;
504       break;
505
506     case 4:
507       reg = REG_D6;
508       break;
509
510     default:
511       error (_("bad size for return value"));
512       return;
513     }
514
515   regcache->cooked_read (inv_reg_perm[reg], (gdb_byte *) valbuf);
516 }
517
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)
522 {
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;
528
529   if (readbuf)
530     s12z_extract_return_value (type, regcache, readbuf);
531
532   return RETURN_VALUE_REGISTER_CONVENTION;
533 }
534
535
536 static void
537 show_bdccsr_command (const char *args, int from_tty)
538 {
539   struct string_file output;
540   target_rcmd ("bdccsr", &output);
541
542   printf_unfiltered ("The current BDCCSR value is %s\n", output.string().c_str());
543 }
544
545 static struct gdbarch *
546 s12z_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
547 {
548   struct gdbarch_tdep *tdep = XNEW (struct gdbarch_tdep);
549   struct gdbarch *gdbarch = gdbarch_alloc (&info, tdep);
550
551   add_cmd ("bdccsr", class_support, show_bdccsr_command,
552            _("Show the current value of the microcontroller's BDCCSR."),
553            &maintenanceinfolist);
554
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);
563
564   set_gdbarch_ps_regnum (gdbarch, REG_CCW);
565   set_gdbarch_pc_regnum (gdbarch, REG_P);
566   set_gdbarch_sp_regnum (gdbarch, REG_S);
567
568
569   set_gdbarch_print_registers_info (gdbarch, s12z_print_registers_info);
570
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);
575
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);
581
582   set_gdbarch_register_type (gdbarch, s12z_register_type);
583
584   /* Functions to access frame data.  */
585   set_gdbarch_unwind_pc (gdbarch, s12z_unwind_pc);
586   set_gdbarch_unwind_sp (gdbarch, s12z_unwind_sp);
587
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.  */
592
593   set_gdbarch_return_value (gdbarch, s12z_return_value);
594
595   return gdbarch;
596 }
597
598 void
599 _initialize_s12z_tdep (void)
600 {
601   gdbarch_register (bfd_arch_s12z, s12z_gdbarch_init, NULL);
602 }