1 /* Target-dependent code for the Sanyo Xstormy16a (LC590000) processor.
2 
3    Copyright (C) 2001-2021 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19 
20 #include "defs.h"
21 #include "frame.h"
22 #include "frame-base.h"
23 #include "frame-unwind.h"
24 #include "dwarf2/frame.h"
25 #include "symtab.h"
26 #include "gdbtypes.h"
27 #include "gdbcmd.h"
28 #include "gdbcore.h"
29 #include "value.h"
30 #include "dis-asm.h"
31 #include "inferior.h"
32 #include "arch-utils.h"
33 #include "regcache.h"
34 #include "osabi.h"
35 #include "objfiles.h"
36 #include "gdbsupport/byte-vector.h"
37 
38 enum gdb_regnum
39 {
40   /* Xstormy16 has 16 general purpose registers (R0-R15) plus PC.
41      Functions will return their values in register R2-R7 as they fit.
42      Otherwise a hidden pointer to an big enough area is given as argument
43      to the function in r2.  Further arguments are beginning in r3 then.
44      R13 is used as frame pointer when GCC compiles w/o optimization
45      R14 is used as "PSW", displaying the CPU status.
46      R15 is used implicitly as stack pointer.  */
47   E_R0_REGNUM,
48   E_R1_REGNUM,
49   E_R2_REGNUM, E_1ST_ARG_REGNUM = E_R2_REGNUM, E_PTR_RET_REGNUM = E_R2_REGNUM,
50   E_R3_REGNUM,
51   E_R4_REGNUM,
52   E_R5_REGNUM,
53   E_R6_REGNUM,
54   E_R7_REGNUM, E_LST_ARG_REGNUM = E_R7_REGNUM,
55   E_R8_REGNUM,
56   E_R9_REGNUM,
57   E_R10_REGNUM,
58   E_R11_REGNUM,
59   E_R12_REGNUM,
60   E_R13_REGNUM, E_FP_REGNUM = E_R13_REGNUM,
61   E_R14_REGNUM, E_PSW_REGNUM = E_R14_REGNUM,
62   E_R15_REGNUM, E_SP_REGNUM = E_R15_REGNUM,
63   E_PC_REGNUM,
64   E_NUM_REGS
65 };
66 
67 /* Use an invalid address value as 'not available' marker.  */
68 enum { REG_UNAVAIL = (CORE_ADDR) -1 };
69 
70 struct xstormy16_frame_cache
71 {
72   /* Base address.  */
73   CORE_ADDR base;
74   CORE_ADDR pc;
75   LONGEST framesize;
76   int uses_fp;
77   CORE_ADDR saved_regs[E_NUM_REGS];
78   CORE_ADDR saved_sp;
79 };
80 
81 /* Size of instructions, registers, etc.  */
82 enum
83 {
84   xstormy16_inst_size = 2,
85   xstormy16_reg_size = 2,
86   xstormy16_pc_size = 4
87 };
88 
89 /* Size of return datatype which fits into the remaining return registers.  */
90 #define E_MAX_RETTYPE_SIZE(regnum)	((E_LST_ARG_REGNUM - (regnum) + 1) \
91 					* xstormy16_reg_size)
92 
93 /* Size of return datatype which fits into all return registers.  */
94 enum
95 {
96   E_MAX_RETTYPE_SIZE_IN_REGS = E_MAX_RETTYPE_SIZE (E_R2_REGNUM)
97 };
98 
99 /* Function: xstormy16_register_name
100    Returns the name of the standard Xstormy16 register N.  */
101 
102 static const char *
xstormy16_register_name(struct gdbarch * gdbarch,int regnum)103 xstormy16_register_name (struct gdbarch *gdbarch, int regnum)
104 {
105   static const char *register_names[] = {
106     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
107     "r8", "r9", "r10", "r11", "r12", "r13",
108     "psw", "sp", "pc"
109   };
110 
111   if (regnum < 0 || regnum >= E_NUM_REGS)
112     internal_error (__FILE__, __LINE__,
113 		    _("xstormy16_register_name: illegal register number %d"),
114 		    regnum);
115   else
116     return register_names[regnum];
117 
118 }
119 
120 static struct type *
xstormy16_register_type(struct gdbarch * gdbarch,int regnum)121 xstormy16_register_type (struct gdbarch *gdbarch, int regnum)
122 {
123   if (regnum == E_PC_REGNUM)
124     return builtin_type (gdbarch)->builtin_uint32;
125   else
126     return builtin_type (gdbarch)->builtin_uint16;
127 }
128 
129 /* Function: xstormy16_type_is_scalar
130    Makes the decision if a given type is a scalar types.  Scalar
131    types are returned in the registers r2-r7 as they fit.  */
132 
133 static int
xstormy16_type_is_scalar(struct type * t)134 xstormy16_type_is_scalar (struct type *t)
135 {
136   return (t->code () != TYPE_CODE_STRUCT
137 	  && t->code () != TYPE_CODE_UNION
138 	  && t->code () != TYPE_CODE_ARRAY);
139 }
140 
141 /* Function: xstormy16_use_struct_convention
142    Returns non-zero if the given struct type will be returned using
143    a special convention, rather than the normal function return method.
144    7sed in the contexts of the "return" command, and of
145    target function calls from the debugger.  */
146 
147 static int
xstormy16_use_struct_convention(struct type * type)148 xstormy16_use_struct_convention (struct type *type)
149 {
150   return !xstormy16_type_is_scalar (type)
151 	 || TYPE_LENGTH (type) > E_MAX_RETTYPE_SIZE_IN_REGS;
152 }
153 
154 /* Function: xstormy16_extract_return_value
155    Find a function's return value in the appropriate registers (in
156    regbuf), and copy it into valbuf.  */
157 
158 static void
xstormy16_extract_return_value(struct type * type,struct regcache * regcache,gdb_byte * valbuf)159 xstormy16_extract_return_value (struct type *type, struct regcache *regcache,
160 				gdb_byte *valbuf)
161 {
162   int len = TYPE_LENGTH (type);
163   int i, regnum = E_1ST_ARG_REGNUM;
164 
165   for (i = 0; i < len; i += xstormy16_reg_size)
166     regcache->raw_read (regnum++, valbuf + i);
167 }
168 
169 /* Function: xstormy16_store_return_value
170    Copy the function return value from VALBUF into the
171    proper location for a function return.
172    Called only in the context of the "return" command.  */
173 
174 static void
xstormy16_store_return_value(struct type * type,struct regcache * regcache,const gdb_byte * valbuf)175 xstormy16_store_return_value (struct type *type, struct regcache *regcache,
176 			      const gdb_byte *valbuf)
177 {
178   if (TYPE_LENGTH (type) == 1)
179     {
180       /* Add leading zeros to the value.  */
181       gdb_byte buf[xstormy16_reg_size];
182       memset (buf, 0, xstormy16_reg_size);
183       memcpy (buf, valbuf, 1);
184       regcache->raw_write (E_1ST_ARG_REGNUM, buf);
185     }
186   else
187     {
188       int len = TYPE_LENGTH (type);
189       int i, regnum = E_1ST_ARG_REGNUM;
190 
191       for (i = 0; i < len; i += xstormy16_reg_size)
192 	regcache->raw_write (regnum++, valbuf + i);
193     }
194 }
195 
196 static enum return_value_convention
xstormy16_return_value(struct gdbarch * gdbarch,struct value * function,struct type * type,struct regcache * regcache,gdb_byte * readbuf,const gdb_byte * writebuf)197 xstormy16_return_value (struct gdbarch *gdbarch, struct value *function,
198 			struct type *type, struct regcache *regcache,
199 			gdb_byte *readbuf, const gdb_byte *writebuf)
200 {
201   if (xstormy16_use_struct_convention (type))
202     return RETURN_VALUE_STRUCT_CONVENTION;
203   if (writebuf)
204     xstormy16_store_return_value (type, regcache, writebuf);
205   else if (readbuf)
206     xstormy16_extract_return_value (type, regcache, readbuf);
207   return RETURN_VALUE_REGISTER_CONVENTION;
208 }
209 
210 static CORE_ADDR
xstormy16_frame_align(struct gdbarch * gdbarch,CORE_ADDR addr)211 xstormy16_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
212 {
213   if (addr & 1)
214     ++addr;
215   return addr;
216 }
217 
218 /* Function: xstormy16_push_dummy_call
219    Setup the function arguments for GDB to call a function in the inferior.
220    Called only in the context of a target function call from the debugger.
221    Returns the value of the SP register after the args are pushed.  */
222 
223 static CORE_ADDR
xstormy16_push_dummy_call(struct gdbarch * gdbarch,struct value * function,struct regcache * regcache,CORE_ADDR bp_addr,int nargs,struct value ** args,CORE_ADDR sp,function_call_return_method return_method,CORE_ADDR struct_addr)224 xstormy16_push_dummy_call (struct gdbarch *gdbarch,
225 			   struct value *function,
226 			   struct regcache *regcache,
227 			   CORE_ADDR bp_addr, int nargs,
228 			   struct value **args,
229 			   CORE_ADDR sp,
230 			   function_call_return_method return_method,
231 			   CORE_ADDR struct_addr)
232 {
233   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
234   CORE_ADDR stack_dest = sp;
235   int argreg = E_1ST_ARG_REGNUM;
236   int i, j;
237   int typelen, slacklen;
238   gdb_byte buf[xstormy16_pc_size];
239 
240   /* If returning a struct using target ABI method, then the struct return
241      address will consume one argument-passing register.  */
242   if (return_method == return_method_struct)
243     {
244       regcache_cooked_write_unsigned (regcache, E_PTR_RET_REGNUM, struct_addr);
245       argreg++;
246     }
247 
248   /* Arguments are passed in R2-R7 as they fit.  If an argument doesn't
249      fit in the remaining registers we're switching over to the stack.
250      No argument is put on stack partially and as soon as we switched
251      over to stack no further argument is put in a register even if it
252      would fit in the remaining unused registers.  */
253   for (i = 0; i < nargs && argreg <= E_LST_ARG_REGNUM; i++)
254     {
255       typelen = TYPE_LENGTH (value_enclosing_type (args[i]));
256       if (typelen > E_MAX_RETTYPE_SIZE (argreg))
257 	break;
258 
259       /* Put argument into registers wordwise.  */
260       const gdb_byte *val = value_contents (args[i]);
261       for (j = 0; j < typelen; j += xstormy16_reg_size)
262 	{
263 	  ULONGEST regval;
264 	  int size = (typelen - j == 1) ? 1 : xstormy16_reg_size;
265 
266 	  regval = extract_unsigned_integer (val + j, size, byte_order);
267 	  regcache_cooked_write_unsigned (regcache, argreg++, regval);
268 	}
269     }
270 
271   /* Align SP */
272   stack_dest = xstormy16_frame_align (gdbarch, stack_dest);
273 
274   /* Loop backwards through remaining arguments and push them on the stack,
275      wordaligned.  */
276   for (j = nargs - 1; j >= i; j--)
277     {
278       const gdb_byte *bytes = value_contents (args[j]);
279 
280       typelen = TYPE_LENGTH (value_enclosing_type (args[j]));
281       slacklen = typelen & 1;
282       gdb::byte_vector val (typelen + slacklen);
283       memcpy (val.data (), bytes, typelen);
284       memset (val.data () + typelen, 0, slacklen);
285 
286       /* Now write this data to the stack.  The stack grows upwards.  */
287       write_memory (stack_dest, val.data (), typelen + slacklen);
288       stack_dest += typelen + slacklen;
289     }
290 
291   store_unsigned_integer (buf, xstormy16_pc_size, byte_order, bp_addr);
292   write_memory (stack_dest, buf, xstormy16_pc_size);
293   stack_dest += xstormy16_pc_size;
294 
295   /* Update stack pointer.  */
296   regcache_cooked_write_unsigned (regcache, E_SP_REGNUM, stack_dest);
297 
298   /* Return the new stack pointer minus the return address slot since
299      that's what DWARF2/GCC uses as the frame's CFA.  */
300   return stack_dest - xstormy16_pc_size;
301 }
302 
303 /* Function: xstormy16_scan_prologue
304    Decode the instructions within the given address range.
305    Decide when we must have reached the end of the function prologue.
306    If a frame_info pointer is provided, fill in its saved_regs etc.
307 
308    Returns the address of the first instruction after the prologue.  */
309 
310 static CORE_ADDR
xstormy16_analyze_prologue(struct gdbarch * gdbarch,CORE_ADDR start_addr,CORE_ADDR end_addr,struct xstormy16_frame_cache * cache,struct frame_info * this_frame)311 xstormy16_analyze_prologue (struct gdbarch *gdbarch,
312 			    CORE_ADDR start_addr, CORE_ADDR end_addr,
313 			    struct xstormy16_frame_cache *cache,
314 			    struct frame_info *this_frame)
315 {
316   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
317   CORE_ADDR next_addr;
318   ULONGEST inst, inst2;
319   LONGEST offset;
320   int regnum;
321 
322   /* Initialize framesize with size of PC put on stack by CALLF inst.  */
323   cache->saved_regs[E_PC_REGNUM] = 0;
324   cache->framesize = xstormy16_pc_size;
325 
326   if (start_addr >= end_addr)
327     return end_addr;
328 
329   for (next_addr = start_addr;
330        next_addr < end_addr; next_addr += xstormy16_inst_size)
331     {
332       inst = read_memory_unsigned_integer (next_addr,
333 					   xstormy16_inst_size, byte_order);
334       inst2 = read_memory_unsigned_integer (next_addr + xstormy16_inst_size,
335 					    xstormy16_inst_size, byte_order);
336 
337       if (inst >= 0x0082 && inst <= 0x008d)	/* push r2 .. push r13 */
338 	{
339 	  regnum = inst & 0x000f;
340 	  cache->saved_regs[regnum] = cache->framesize;
341 	  cache->framesize += xstormy16_reg_size;
342 	}
343 
344       /* Optional stack allocation for args and local vars <= 4 byte.  */
345       else if (inst == 0x301f || inst == 0x303f)       /* inc r15, #0x1/#0x3 */
346 	{
347 	  cache->framesize += ((inst & 0x0030) >> 4) + 1;
348 	}
349 
350       /* optional stack allocation for args and local vars > 4 && < 16 byte */
351       else if ((inst & 0xff0f) == 0x510f)	/* 51Hf   add r15, #0xH */
352 	{
353 	  cache->framesize += (inst & 0x00f0) >> 4;
354 	}
355 
356       /* Optional stack allocation for args and local vars >= 16 byte.  */
357       else if (inst == 0x314f && inst2 >= 0x0010) /* 314f HHHH add r15, #0xH */
358 	{
359 	  cache->framesize += inst2;
360 	  next_addr += xstormy16_inst_size;
361 	}
362 
363       else if (inst == 0x46fd)	/* mov r13, r15 */
364 	{
365 	  cache->uses_fp = 1;
366 	}
367 
368       /* optional copying of args in r2-r7 to r10-r13.  */
369       /* Probably only in optimized case but legal action for prologue.  */
370       else if ((inst & 0xff00) == 0x4600	/* 46SD   mov rD, rS */
371 	       && (inst & 0x00f0) >= 0x0020 && (inst & 0x00f0) <= 0x0070
372 	       && (inst & 0x000f) >= 0x000a && (inst & 0x000f) <= 0x000d)
373 	;
374 
375       /* Optional copying of args in r2-r7 to stack.  */
376       /* 72DS HHHH   mov.b (rD, 0xHHHH), r(S-8)
377 	 (bit3 always 1, bit2-0 = reg) */
378       /* 73DS HHHH   mov.w (rD, 0xHHHH), r(S-8) */
379       else if ((inst & 0xfed8) == 0x72d8 && (inst & 0x0007) >= 2)
380 	{
381 	  regnum = inst & 0x0007;
382 	  /* Only 12 of 16 bits of the argument are used for the
383 	     signed offset.  */
384 	  offset = (LONGEST) (inst2 & 0x0fff);
385 	  if (offset & 0x0800)
386 	    offset -= 0x1000;
387 
388 	  cache->saved_regs[regnum] = cache->framesize + offset;
389 	  next_addr += xstormy16_inst_size;
390 	}
391 
392       else			/* Not a prologue instruction.  */
393 	break;
394     }
395 
396   return next_addr;
397 }
398 
399 /* Function: xstormy16_skip_prologue
400    If the input address is in a function prologue,
401    returns the address of the end of the prologue;
402    else returns the input address.
403 
404    Note: the input address is likely to be the function start,
405    since this function is mainly used for advancing a breakpoint
406    to the first line, or stepping to the first line when we have
407    stepped into a function call.  */
408 
409 static CORE_ADDR
xstormy16_skip_prologue(struct gdbarch * gdbarch,CORE_ADDR pc)410 xstormy16_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
411 {
412   CORE_ADDR func_addr = 0, func_end = 0;
413   const char *func_name;
414 
415   if (find_pc_partial_function (pc, &func_name, &func_addr, &func_end))
416     {
417       struct symtab_and_line sal;
418       struct symbol *sym;
419       struct xstormy16_frame_cache cache;
420       CORE_ADDR plg_end;
421 
422       memset (&cache, 0, sizeof cache);
423 
424       /* Don't trust line number debug info in frameless functions.  */
425       plg_end = xstormy16_analyze_prologue (gdbarch, func_addr, func_end,
426 					    &cache, NULL);
427       if (!cache.uses_fp)
428 	return plg_end;
429 
430       /* Found a function.  */
431       sym = lookup_symbol (func_name, NULL, VAR_DOMAIN, NULL).symbol;
432       /* Don't use line number debug info for assembly source files.  */
433       if (sym && sym->language () != language_asm)
434 	{
435 	  sal = find_pc_line (func_addr, 0);
436 	  if (sal.end && sal.end < func_end)
437 	    {
438 	      /* Found a line number, use it as end of prologue.  */
439 	      return sal.end;
440 	    }
441 	}
442       /* No useable line symbol.  Use result of prologue parsing method.  */
443       return plg_end;
444     }
445 
446   /* No function symbol -- just return the PC.  */
447 
448   return (CORE_ADDR) pc;
449 }
450 
451 /* Implement the stack_frame_destroyed_p gdbarch method.
452 
453    The epilogue is defined here as the area at the end of a function,
454    either on the `ret' instruction itself or after an instruction which
455    destroys the function's stack frame.  */
456 
457 static int
xstormy16_stack_frame_destroyed_p(struct gdbarch * gdbarch,CORE_ADDR pc)458 xstormy16_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
459 {
460   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
461   CORE_ADDR func_addr = 0, func_end = 0;
462 
463   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
464     {
465       ULONGEST inst, inst2;
466       CORE_ADDR addr = func_end - xstormy16_inst_size;
467 
468       /* The Xstormy16 epilogue is max. 14 bytes long.  */
469       if (pc < func_end - 7 * xstormy16_inst_size)
470 	return 0;
471 
472       /* Check if we're on a `ret' instruction.  Otherwise it's
473 	 too dangerous to proceed.  */
474       inst = read_memory_unsigned_integer (addr,
475 					   xstormy16_inst_size, byte_order);
476       if (inst != 0x0003)
477 	return 0;
478 
479       while ((addr -= xstormy16_inst_size) >= func_addr)
480 	{
481 	  inst = read_memory_unsigned_integer (addr,
482 					       xstormy16_inst_size,
483 					       byte_order);
484 	  if (inst >= 0x009a && inst <= 0x009d)	/* pop r10...r13 */
485 	    continue;
486 	  if (inst == 0x305f || inst == 0x307f)	/* dec r15, #0x1/#0x3 */
487 	    break;
488 	  inst2 = read_memory_unsigned_integer (addr - xstormy16_inst_size,
489 						xstormy16_inst_size,
490 						byte_order);
491 	  if (inst2 == 0x314f && inst >= 0x8000)      /* add r15, neg. value */
492 	    {
493 	      addr -= xstormy16_inst_size;
494 	      break;
495 	    }
496 	  return 0;
497 	}
498       if (pc > addr)
499 	return 1;
500     }
501   return 0;
502 }
503 
504 constexpr gdb_byte xstormy16_break_insn[] = { 0x06, 0x0 };
505 
506 typedef BP_MANIPULATION (xstormy16_break_insn) xstormy16_breakpoint;
507 
508 /* Given a pointer to a jump table entry, return the address
509    of the function it jumps to.  Return 0 if not found.  */
510 static CORE_ADDR
xstormy16_resolve_jmp_table_entry(struct gdbarch * gdbarch,CORE_ADDR faddr)511 xstormy16_resolve_jmp_table_entry (struct gdbarch *gdbarch, CORE_ADDR faddr)
512 {
513   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
514   struct obj_section *faddr_sect = find_pc_section (faddr);
515 
516   if (faddr_sect)
517     {
518       LONGEST inst, inst2, addr;
519       gdb_byte buf[2 * xstormy16_inst_size];
520 
521       /* Return faddr if it's not pointing into the jump table.  */
522       if (strcmp (faddr_sect->the_bfd_section->name, ".plt"))
523 	return faddr;
524 
525       if (!target_read_memory (faddr, buf, sizeof buf))
526 	{
527 	  inst = extract_unsigned_integer (buf,
528 					   xstormy16_inst_size, byte_order);
529 	  inst2 = extract_unsigned_integer (buf + xstormy16_inst_size,
530 					    xstormy16_inst_size, byte_order);
531 	  addr = inst2 << 8 | (inst & 0xff);
532 	  return addr;
533 	}
534     }
535   return 0;
536 }
537 
538 /* Given a function's address, attempt to find (and return) the
539    address of the corresponding jump table entry.  Return 0 if
540    not found.  */
541 static CORE_ADDR
xstormy16_find_jmp_table_entry(struct gdbarch * gdbarch,CORE_ADDR faddr)542 xstormy16_find_jmp_table_entry (struct gdbarch *gdbarch, CORE_ADDR faddr)
543 {
544   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
545   struct obj_section *faddr_sect = find_pc_section (faddr);
546 
547   if (faddr_sect)
548     {
549       struct obj_section *osect;
550 
551       /* Return faddr if it's already a pointer to a jump table entry.  */
552       if (!strcmp (faddr_sect->the_bfd_section->name, ".plt"))
553 	return faddr;
554 
555       ALL_OBJFILE_OSECTIONS (faddr_sect->objfile, osect)
556       {
557 	if (!strcmp (osect->the_bfd_section->name, ".plt"))
558 	  break;
559       }
560 
561       if (osect < faddr_sect->objfile->sections_end)
562 	{
563 	  CORE_ADDR addr, endaddr;
564 
565 	  addr = osect->addr ();
566 	  endaddr = osect->endaddr ();
567 
568 	  for (; addr < endaddr; addr += 2 * xstormy16_inst_size)
569 	    {
570 	      LONGEST inst, inst2, faddr2;
571 	      gdb_byte buf[2 * xstormy16_inst_size];
572 
573 	      if (target_read_memory (addr, buf, sizeof buf))
574 		return 0;
575 	      inst = extract_unsigned_integer (buf,
576 					       xstormy16_inst_size,
577 					       byte_order);
578 	      inst2 = extract_unsigned_integer (buf + xstormy16_inst_size,
579 						xstormy16_inst_size,
580 						byte_order);
581 	      faddr2 = inst2 << 8 | (inst & 0xff);
582 	      if (faddr == faddr2)
583 		return addr;
584 	    }
585 	}
586     }
587   return 0;
588 }
589 
590 static CORE_ADDR
xstormy16_skip_trampoline_code(struct frame_info * frame,CORE_ADDR pc)591 xstormy16_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
592 {
593   struct gdbarch *gdbarch = get_frame_arch (frame);
594   CORE_ADDR tmp = xstormy16_resolve_jmp_table_entry (gdbarch, pc);
595 
596   if (tmp && tmp != pc)
597     return tmp;
598   return 0;
599 }
600 
601 /* Function pointers are 16 bit.  The address space is 24 bit, using
602    32 bit addresses.  Pointers to functions on the XStormy16 are implemented
603    by using 16 bit pointers, which are either direct pointers in case the
604    function begins below 0x10000, or indirect pointers into a jump table.
605    The next two functions convert 16 bit pointers into 24 (32) bit addresses
606    and vice versa.  */
607 
608 static CORE_ADDR
xstormy16_pointer_to_address(struct gdbarch * gdbarch,struct type * type,const gdb_byte * buf)609 xstormy16_pointer_to_address (struct gdbarch *gdbarch,
610 			      struct type *type, const gdb_byte *buf)
611 {
612   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
613   enum type_code target = TYPE_TARGET_TYPE (type)->code ();
614   CORE_ADDR addr
615     = extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);
616 
617   if (target == TYPE_CODE_FUNC || target == TYPE_CODE_METHOD)
618     {
619       CORE_ADDR addr2 = xstormy16_resolve_jmp_table_entry (gdbarch, addr);
620       if (addr2)
621 	addr = addr2;
622     }
623 
624   return addr;
625 }
626 
627 static void
xstormy16_address_to_pointer(struct gdbarch * gdbarch,struct type * type,gdb_byte * buf,CORE_ADDR addr)628 xstormy16_address_to_pointer (struct gdbarch *gdbarch,
629 			      struct type *type, gdb_byte *buf, CORE_ADDR addr)
630 {
631   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
632   enum type_code target = TYPE_TARGET_TYPE (type)->code ();
633 
634   if (target == TYPE_CODE_FUNC || target == TYPE_CODE_METHOD)
635     {
636       CORE_ADDR addr2 = xstormy16_find_jmp_table_entry (gdbarch, addr);
637       if (addr2)
638 	addr = addr2;
639     }
640   store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order, addr);
641 }
642 
643 static struct xstormy16_frame_cache *
xstormy16_alloc_frame_cache(void)644 xstormy16_alloc_frame_cache (void)
645 {
646   struct xstormy16_frame_cache *cache;
647   int i;
648 
649   cache = FRAME_OBSTACK_ZALLOC (struct xstormy16_frame_cache);
650 
651   cache->base = 0;
652   cache->saved_sp = 0;
653   cache->pc = 0;
654   cache->uses_fp = 0;
655   cache->framesize = 0;
656   for (i = 0; i < E_NUM_REGS; ++i)
657     cache->saved_regs[i] = REG_UNAVAIL;
658 
659   return cache;
660 }
661 
662 static struct xstormy16_frame_cache *
xstormy16_frame_cache(struct frame_info * this_frame,void ** this_cache)663 xstormy16_frame_cache (struct frame_info *this_frame, void **this_cache)
664 {
665   struct gdbarch *gdbarch = get_frame_arch (this_frame);
666   struct xstormy16_frame_cache *cache;
667   CORE_ADDR current_pc;
668   int i;
669 
670   if (*this_cache)
671     return (struct xstormy16_frame_cache *) *this_cache;
672 
673   cache = xstormy16_alloc_frame_cache ();
674   *this_cache = cache;
675 
676   cache->base = get_frame_register_unsigned (this_frame, E_FP_REGNUM);
677   if (cache->base == 0)
678     return cache;
679 
680   cache->pc = get_frame_func (this_frame);
681   current_pc = get_frame_pc (this_frame);
682   if (cache->pc)
683     xstormy16_analyze_prologue (gdbarch, cache->pc, current_pc,
684 				cache, this_frame);
685 
686   if (!cache->uses_fp)
687     cache->base = get_frame_register_unsigned (this_frame, E_SP_REGNUM);
688 
689   cache->saved_sp = cache->base - cache->framesize;
690 
691   for (i = 0; i < E_NUM_REGS; ++i)
692     if (cache->saved_regs[i] != REG_UNAVAIL)
693       cache->saved_regs[i] += cache->saved_sp;
694 
695   return cache;
696 }
697 
698 static struct value *
xstormy16_frame_prev_register(struct frame_info * this_frame,void ** this_cache,int regnum)699 xstormy16_frame_prev_register (struct frame_info *this_frame,
700 			       void **this_cache, int regnum)
701 {
702   struct xstormy16_frame_cache *cache = xstormy16_frame_cache (this_frame,
703 							       this_cache);
704   gdb_assert (regnum >= 0);
705 
706   if (regnum == E_SP_REGNUM && cache->saved_sp)
707     return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
708 
709   if (regnum < E_NUM_REGS && cache->saved_regs[regnum] != REG_UNAVAIL)
710     return frame_unwind_got_memory (this_frame, regnum,
711 				    cache->saved_regs[regnum]);
712 
713   return frame_unwind_got_register (this_frame, regnum, regnum);
714 }
715 
716 static void
xstormy16_frame_this_id(struct frame_info * this_frame,void ** this_cache,struct frame_id * this_id)717 xstormy16_frame_this_id (struct frame_info *this_frame, void **this_cache,
718 			 struct frame_id *this_id)
719 {
720   struct xstormy16_frame_cache *cache = xstormy16_frame_cache (this_frame,
721 							       this_cache);
722 
723   /* This marks the outermost frame.  */
724   if (cache->base == 0)
725     return;
726 
727   *this_id = frame_id_build (cache->saved_sp, cache->pc);
728 }
729 
730 static CORE_ADDR
xstormy16_frame_base_address(struct frame_info * this_frame,void ** this_cache)731 xstormy16_frame_base_address (struct frame_info *this_frame, void **this_cache)
732 {
733   struct xstormy16_frame_cache *cache = xstormy16_frame_cache (this_frame,
734 							       this_cache);
735   return cache->base;
736 }
737 
738 static const struct frame_unwind xstormy16_frame_unwind = {
739   "xstormy16 prologue",
740   NORMAL_FRAME,
741   default_frame_unwind_stop_reason,
742   xstormy16_frame_this_id,
743   xstormy16_frame_prev_register,
744   NULL,
745   default_frame_sniffer
746 };
747 
748 static const struct frame_base xstormy16_frame_base = {
749   &xstormy16_frame_unwind,
750   xstormy16_frame_base_address,
751   xstormy16_frame_base_address,
752   xstormy16_frame_base_address
753 };
754 
755 /* Function: xstormy16_gdbarch_init
756    Initializer function for the xstormy16 gdbarch vector.
757    Called by gdbarch.  Sets up the gdbarch vector(s) for this target.  */
758 
759 static struct gdbarch *
xstormy16_gdbarch_init(struct gdbarch_info info,struct gdbarch_list * arches)760 xstormy16_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
761 {
762   struct gdbarch *gdbarch;
763 
764   /* find a candidate among the list of pre-declared architectures.  */
765   arches = gdbarch_list_lookup_by_info (arches, &info);
766   if (arches != NULL)
767     return (arches->gdbarch);
768 
769   gdbarch = gdbarch_alloc (&info, NULL);
770 
771   /*
772    * Basic register fields and methods, datatype sizes and stuff.
773    */
774 
775   set_gdbarch_num_regs (gdbarch, E_NUM_REGS);
776   set_gdbarch_num_pseudo_regs (gdbarch, 0);
777   set_gdbarch_sp_regnum (gdbarch, E_SP_REGNUM);
778   set_gdbarch_pc_regnum (gdbarch, E_PC_REGNUM);
779   set_gdbarch_register_name (gdbarch, xstormy16_register_name);
780   set_gdbarch_register_type (gdbarch, xstormy16_register_type);
781 
782   set_gdbarch_char_signed (gdbarch, 0);
783   set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
784   set_gdbarch_int_bit (gdbarch, 2 * TARGET_CHAR_BIT);
785   set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
786   set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
787 
788   set_gdbarch_wchar_bit (gdbarch, 2 * TARGET_CHAR_BIT);
789   set_gdbarch_wchar_signed (gdbarch, 1);
790 
791   set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
792   set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
793   set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
794 
795   set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
796   set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
797   set_gdbarch_dwarf2_addr_size (gdbarch, 4);
798 
799   set_gdbarch_address_to_pointer (gdbarch, xstormy16_address_to_pointer);
800   set_gdbarch_pointer_to_address (gdbarch, xstormy16_pointer_to_address);
801 
802   /* Stack grows up.  */
803   set_gdbarch_inner_than (gdbarch, core_addr_greaterthan);
804 
805   /*
806    * Frame Info
807    */
808   set_gdbarch_frame_align (gdbarch, xstormy16_frame_align);
809   frame_base_set_default (gdbarch, &xstormy16_frame_base);
810 
811   set_gdbarch_skip_prologue (gdbarch, xstormy16_skip_prologue);
812   set_gdbarch_stack_frame_destroyed_p (gdbarch,
813 				       xstormy16_stack_frame_destroyed_p);
814 
815   /* These values and methods are used when gdb calls a target function.  */
816   set_gdbarch_push_dummy_call (gdbarch, xstormy16_push_dummy_call);
817   set_gdbarch_breakpoint_kind_from_pc (gdbarch,
818 				       xstormy16_breakpoint::kind_from_pc);
819   set_gdbarch_sw_breakpoint_from_kind (gdbarch,
820 				       xstormy16_breakpoint::bp_from_kind);
821   set_gdbarch_return_value (gdbarch, xstormy16_return_value);
822 
823   set_gdbarch_skip_trampoline_code (gdbarch, xstormy16_skip_trampoline_code);
824 
825   gdbarch_init_osabi (info, gdbarch);
826 
827   dwarf2_append_unwinders (gdbarch);
828   frame_unwind_append_unwinder (gdbarch, &xstormy16_frame_unwind);
829 
830   return gdbarch;
831 }
832 
833 /* Function: _initialize_xstormy16_tdep
834    Initializer function for the Sanyo Xstormy16a module.
835    Called by gdb at start-up.  */
836 
837 void _initialize_xstormy16_tdep ();
838 void
_initialize_xstormy16_tdep()839 _initialize_xstormy16_tdep ()
840 {
841   register_gdbarch_init (bfd_arch_xstormy16, xstormy16_gdbarch_init);
842 }
843