918a19e78f40af694c6f89c1be309221670b3e79
[platform/upstream/binutils.git] / gdb / xstormy16-tdep.c
1 /* Target-dependent code for the Sanyo Xstormy16a (LC590000) processor.
2    Copyright 2001, 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 2 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, write to the Free Software
18    Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include "defs.h"
22 #include "value.h"
23 #include "inferior.h"
24 #include "symfile.h"
25 #include "arch-utils.h"
26 #include "regcache.h"
27 #include "gdbcore.h"
28 #include "objfiles.h"
29
30 struct gdbarch_tdep
31 {
32   /* gdbarch target dependent data here. Currently unused for Xstormy16. */
33 };
34
35 /* Extra info which is saved in each frame_info. */
36 struct frame_extra_info
37 {
38   int framesize;
39   int frameless_p;
40 };
41
42 enum gdb_regnum
43 {
44   /* Xstormy16 has 16 general purpose registers (R0-R15) plus PC.
45      Functions will return their values in register R2-R7 as they fit.
46      Otherwise a hidden pointer to an big enough area is given as argument
47      to the function in r2. Further arguments are beginning in r3 then.
48      R13 is used as frame pointer when GCC compiles w/o optimization
49      R14 is used as "PSW", displaying the CPU status.
50      R15 is used implicitely as stack pointer. */
51   E_R0_REGNUM,
52   E_R1_REGNUM,
53   E_R2_REGNUM, E_1ST_ARG_REGNUM = E_R2_REGNUM, E_PTR_RET_REGNUM = E_R2_REGNUM,
54   E_R3_REGNUM,
55   E_R4_REGNUM,
56   E_R5_REGNUM,
57   E_R6_REGNUM,
58   E_R7_REGNUM, E_LST_ARG_REGNUM = E_R7_REGNUM,
59   E_R8_REGNUM,
60   E_R9_REGNUM,
61   E_R10_REGNUM,
62   E_R11_REGNUM,
63   E_R12_REGNUM,
64   E_R13_REGNUM, E_FP_REGNUM = E_R13_REGNUM,
65   E_R14_REGNUM, E_PSW_REGNUM = E_R14_REGNUM,
66   E_R15_REGNUM, E_SP_REGNUM = E_R15_REGNUM,
67   E_PC_REGNUM,
68   E_NUM_REGS
69 };
70
71 /* Size of instructions, registers, etc. */
72 enum
73 {
74   xstormy16_inst_size = 2,
75   xstormy16_reg_size = 2,
76   xstormy16_pc_size = 4
77 };
78
79 /* Size of return datatype which fits into the remaining return registers. */
80 #define E_MAX_RETTYPE_SIZE(regnum)      ((E_LST_ARG_REGNUM - (regnum) + 1) \
81                                         * xstormy16_reg_size)
82
83 /* Size of return datatype which fits into all return registers. */
84 enum
85 {
86   E_MAX_RETTYPE_SIZE_IN_REGS = E_MAX_RETTYPE_SIZE (E_R2_REGNUM)
87 };
88
89
90 /* Size of all registers as a whole. */
91 enum
92 {
93   E_ALL_REGS_SIZE = (E_NUM_REGS - 1) * xstormy16_reg_size + xstormy16_pc_size
94 };
95
96 /* Function: xstormy16_register_name
97    Returns the name of the standard Xstormy16 register N. */
98
99 static char *
100 xstormy16_register_name (int regnum)
101 {
102   static char *register_names[] = {
103     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
104     "r8", "r9", "r10", "r11", "r12", "r13",
105     "psw", "sp", "pc"
106   };
107
108   if (regnum < 0 ||
109       regnum >= sizeof (register_names) / sizeof (register_names[0]))
110     internal_error (__FILE__, __LINE__,
111                     "xstormy16_register_name: illegal register number %d",
112                     regnum);
113   else
114     return register_names[regnum];
115
116 }
117
118 /* Function: xstormy16_register_byte 
119    Returns the byte position in the register cache for register N. */
120
121 static int
122 xstormy16_register_byte (int regnum)
123 {
124   if (regnum < 0 || regnum >= E_NUM_REGS)
125     internal_error (__FILE__, __LINE__,
126                     "xstormy16_register_byte: illegal register number %d",
127                     regnum);
128   else
129     /* All registers occupy 2 bytes in the regcache except for PC
130        which is the last one. Therefore the byte position is still
131        simply a multiple of 2. */
132     return regnum * xstormy16_reg_size;
133 }
134
135 /* Function: xstormy16_register_raw_size
136    Returns the number of bytes occupied by the register on the target. */
137
138 static int
139 xstormy16_register_raw_size (int regnum)
140 {
141   if (regnum < 0 || regnum >= E_NUM_REGS)
142     internal_error (__FILE__, __LINE__,
143                     "xstormy16_register_raw_size: illegal register number %d",
144                     regnum);
145   /* Only the PC has 4 Byte, all other registers 2 Byte. */
146   else if (regnum == E_PC_REGNUM)
147     return xstormy16_pc_size;
148   else
149     return xstormy16_reg_size;
150 }
151
152 /* Function: xstormy16_register_virtual_size
153    Returns the number of bytes occupied by the register as represented
154    internally by gdb. */
155
156 static int
157 xstormy16_register_virtual_size (int regnum)
158 {
159   return xstormy16_register_raw_size (regnum);
160 }
161
162 /* Function: xstormy16_reg_virtual_type 
163    Returns the default type for register N. */
164
165 static struct type *
166 xstormy16_reg_virtual_type (int regnum)
167 {
168   if (regnum < 0 || regnum >= E_NUM_REGS)
169     internal_error (__FILE__, __LINE__,
170                     "xstormy16_register_virtual_type: illegal register number %d",
171                     regnum);
172   else if (regnum == E_PC_REGNUM)
173     return builtin_type_uint32;
174   else
175     return builtin_type_uint16;
176 }
177
178 /* Function: xstormy16_get_saved_register
179    Find a register's saved value on the call stack. */
180
181 static void
182 xstormy16_get_saved_register (char *raw_buffer,
183                               int *optimized,
184                               CORE_ADDR *addrp,
185                               struct frame_info *fi,
186                               int regnum, enum lval_type *lval)
187 {
188   generic_get_saved_register (raw_buffer, optimized, addrp, fi, regnum, lval);
189 }
190
191 /* Function: xstormy16_type_is_scalar
192    Makes the decision if a given type is a scalar types.  Scalar
193    types are returned in the registers r2-r7 as they fit. */
194
195 static int
196 xstormy16_type_is_scalar (struct type *t)
197 {
198   return (TYPE_CODE(t) != TYPE_CODE_STRUCT
199           && TYPE_CODE(t) != TYPE_CODE_UNION
200           && TYPE_CODE(t) != TYPE_CODE_ARRAY);
201 }
202
203 /* Function: xstormy16_extract_return_value
204    Copy the function's return value into VALBUF. 
205    This function is called only in the context of "target function calls",
206    ie. when the debugger forces a function to be called in the child, and
207    when the debugger forces a function to return prematurely via the
208    "return" command. */
209
210 static void
211 xstormy16_extract_return_value (struct type *type, char *regbuf, char *valbuf)
212 {
213   CORE_ADDR return_buffer;
214   int offset = 0;
215
216   if (xstormy16_type_is_scalar (type)
217       && TYPE_LENGTH (type) <= E_MAX_RETTYPE_SIZE_IN_REGS)
218     {
219       /* Scalar return values of <= 12 bytes are returned in 
220          E_1ST_ARG_REGNUM to E_LST_ARG_REGNUM. */
221       memcpy (valbuf,
222               &regbuf[REGISTER_BYTE (E_1ST_ARG_REGNUM)] + offset,
223               TYPE_LENGTH (type));
224     }
225   else
226     {
227       /* Aggregates and return values > 12 bytes are returned in memory,
228          pointed to by R2. */
229       return_buffer =
230         extract_address (regbuf + REGISTER_BYTE (E_PTR_RET_REGNUM),
231                          REGISTER_RAW_SIZE (E_PTR_RET_REGNUM));
232
233       read_memory (return_buffer, valbuf, TYPE_LENGTH (type));
234     }
235 }
236
237 /* Function: xstormy16_push_arguments
238    Setup the function arguments for GDB to call a function in the inferior.
239    Called only in the context of a target function call from the debugger.
240    Returns the value of the SP register after the args are pushed.
241 */
242
243 static CORE_ADDR
244 xstormy16_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
245                           int struct_return, CORE_ADDR struct_addr)
246 {
247   CORE_ADDR stack_dest = sp;
248   int argreg = E_1ST_ARG_REGNUM;
249   int i, j;
250   int typelen, slacklen;
251   char *val;
252
253   /* If struct_return is true, then the struct return address will
254      consume one argument-passing register.  */
255   if (struct_return)
256     argreg++;
257
258   /* Arguments are passed in R2-R7 as they fit. If an argument doesn't
259      fit in the remaining registers we're switching over to the stack.
260      No argument is put on stack partially and as soon as we switched
261      over to stack no further argument is put in a register even if it
262      would fit in the remaining unused registers. */
263   for (i = 0; i < nargs && argreg <= E_LST_ARG_REGNUM; i++)
264     {
265       typelen = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (args[i]));
266       if (typelen > E_MAX_RETTYPE_SIZE (argreg))
267         break;
268
269       /* Put argument into registers wordwise. */
270       val = VALUE_CONTENTS (args[i]);
271       for (j = 0; j < typelen; j += xstormy16_reg_size)
272         write_register (argreg++,
273                         extract_unsigned_integer (val + j,
274                                                   typelen - j ==
275                                                   1 ? 1 :
276                                                   xstormy16_reg_size));
277     }
278
279   /* Align SP */
280   if (stack_dest & 1)
281     ++stack_dest;
282
283   /* Loop backwards through remaining arguments and push them on the stack,
284      wordaligned. */
285   for (j = nargs - 1; j >= i; j--)
286     {
287       typelen = TYPE_LENGTH (VALUE_ENCLOSING_TYPE (args[j]));
288       slacklen = typelen & 1;
289       val = alloca (typelen + slacklen);
290       memcpy (val, VALUE_CONTENTS (args[j]), typelen);
291       memset (val + typelen, 0, slacklen);
292
293       /* Now write this data to the stack. The stack grows upwards. */
294       write_memory (stack_dest, val, typelen + slacklen);
295       stack_dest += typelen + slacklen;
296     }
297
298   /* And that should do it.  Return the new stack pointer. */
299   return stack_dest;
300 }
301
302 /* Function: xstormy16_push_return_address (pc)
303    Setup the return address for GDB to call a function in the inferior.
304    Called only in the context of a target function call from the debugger.
305    Returns the value of the SP register when the operation is finished
306    (which may or may not be the same as before).
307 */
308
309 CORE_ADDR
310 xstormy16_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
311 {
312   unsigned char buf[xstormy16_pc_size];
313
314   store_unsigned_integer (buf, xstormy16_pc_size, CALL_DUMMY_ADDRESS ());
315   write_memory (sp, buf, xstormy16_pc_size);
316   return sp + xstormy16_pc_size;
317 }
318
319 /* Function: xstormy16_pop_frame
320    Destroy the innermost (Top-Of-Stack) stack frame, restoring the 
321    machine state that was in effect before the frame was created. 
322    Used in the contexts of the "return" command, and of 
323    target function calls from the debugger.
324 */
325
326 static void
327 xstormy16_pop_frame (void)
328 {
329   struct frame_info *fi = get_current_frame ();
330   int i;
331
332   if (fi == NULL)
333     return;                     /* paranoia */
334
335   if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
336     {
337       generic_pop_dummy_frame ();
338     }
339   else
340     {
341       /* Restore the saved regs. */
342       for (i = 0; i < NUM_REGS; i++)
343         if (fi->saved_regs[i])
344           {
345             if (i == SP_REGNUM)
346               write_register (i, fi->saved_regs[i]);
347             else if (i == E_PC_REGNUM)
348               write_register (i, read_memory_integer (fi->saved_regs[i],
349                                                       xstormy16_pc_size));
350             else
351               write_register (i, read_memory_integer (fi->saved_regs[i],
352                                                       xstormy16_reg_size));
353           }
354       /* Restore the PC */
355       write_register (PC_REGNUM, FRAME_SAVED_PC (fi));
356       flush_cached_frames ();
357     }
358   return;
359 }
360
361 /* Function: xstormy16_store_struct_return
362    Copy the (struct) function return value to its destined location. 
363    Called only in the context of a target function call from the debugger.
364 */
365
366 static void
367 xstormy16_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
368 {
369   write_register (E_PTR_RET_REGNUM, addr);
370 }
371
372 /* Function: xstormy16_store_return_value
373    Copy the function return value from VALBUF into the 
374    proper location for a function return. 
375    Called only in the context of the "return" command.
376 */
377
378 static void
379 xstormy16_store_return_value (struct type *type, char *valbuf)
380 {
381   CORE_ADDR return_buffer;
382   char buf[xstormy16_reg_size];
383
384   if (xstormy16_type_is_scalar (type) && TYPE_LENGTH (type) == 1)
385     {
386       /* Add leading zeros to the value. */
387       memset (buf, 0, xstormy16_reg_size);
388       memcpy (buf, valbuf, 1);
389       write_register_gen (E_1ST_ARG_REGNUM, buf);
390     }
391   else if (xstormy16_type_is_scalar (type) &&
392            TYPE_LENGTH (type) <= E_MAX_RETTYPE_SIZE_IN_REGS)
393     write_register_bytes (REGISTER_BYTE (E_1ST_ARG_REGNUM),
394                           valbuf, TYPE_LENGTH (type));
395   else
396     {
397       return_buffer = read_register (E_PTR_RET_REGNUM);
398       write_memory (return_buffer, valbuf, TYPE_LENGTH (type));
399     }
400 }
401
402 /* Function: xstormy16_extract_struct_value_address
403    Returns the address in which a function should return a struct value. 
404    Used in the contexts of the "return" command, and of 
405    target function calls from the debugger.
406 */
407
408 static CORE_ADDR
409 xstormy16_extract_struct_value_address (char *regbuf)
410 {
411   return extract_address (regbuf +
412                           xstormy16_register_byte (E_PTR_RET_REGNUM),
413                           xstormy16_reg_size);
414 }
415
416 /* Function: xstormy16_use_struct_convention 
417    Returns non-zero if the given struct type will be returned using
418    a special convention, rather than the normal function return method. 
419    7sed in the contexts of the "return" command, and of 
420    target function calls from the debugger.
421 */
422
423 static int
424 xstormy16_use_struct_convention (int gcc_p, struct type *type)
425 {
426   return !xstormy16_type_is_scalar (type)
427     || TYPE_LENGTH (type) > E_MAX_RETTYPE_SIZE_IN_REGS;
428 }
429
430 /* Function: frame_saved_register
431    Returns the value that regnum had in frame fi
432    (saved in fi or in one of its children).  
433 */
434
435 static CORE_ADDR
436 xstormy16_frame_saved_register (struct frame_info *fi, int regnum)
437 {
438   int size = xstormy16_register_raw_size (regnum);
439   char *buf = (char *) alloca (size);
440
441   generic_get_saved_register (buf, NULL, NULL, fi, regnum, NULL);
442   return (CORE_ADDR) extract_unsigned_integer (buf, size);
443 }
444
445 /* Function: xstormy16_scan_prologue
446    Decode the instructions within the given address range.
447    Decide when we must have reached the end of the function prologue.
448    If a frame_info pointer is provided, fill in its saved_regs etc.
449
450    Returns the address of the first instruction after the prologue. 
451 */
452
453 static CORE_ADDR
454 xstormy16_scan_prologue (CORE_ADDR start_addr, CORE_ADDR end_addr,
455                          struct frame_info *fi, int *frameless)
456 {
457   CORE_ADDR sp = 0, fp = 0;
458   CORE_ADDR next_addr;
459   ULONGEST inst, inst2;
460   LONGEST offset;
461   int regnum;
462
463   if (frameless)
464     *frameless = 1;
465   if (fi)
466     {
467       /* In a call dummy, don't touch the frame. */
468       if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
469         return start_addr;
470
471       /* Grab the frame-relative values of SP and FP, needed below. 
472          The frame_saved_register function will find them on the
473          stack or in the registers as appropriate. */
474       sp = xstormy16_frame_saved_register (fi, E_SP_REGNUM);
475       fp = xstormy16_frame_saved_register (fi, E_FP_REGNUM);
476
477       /* Initialize framesize with size of PC put on stack by CALLF inst. */
478       fi->extra_info->framesize = xstormy16_pc_size;
479     }
480   for (next_addr = start_addr;
481        next_addr < end_addr; next_addr += xstormy16_inst_size)
482     {
483       inst = read_memory_unsigned_integer (next_addr, xstormy16_inst_size);
484       inst2 = read_memory_unsigned_integer (next_addr + xstormy16_inst_size,
485                                             xstormy16_inst_size);
486
487       if (inst >= 0x0082 && inst <= 0x008d)     /* push r2 .. push r13 */
488         {
489           if (fi)
490             {
491               regnum = inst & 0x000f;
492               fi->saved_regs[regnum] = fi->extra_info->framesize;
493               fi->extra_info->framesize += xstormy16_reg_size;
494             }
495         }
496
497       /* optional stack allocation for args and local vars <= 4 byte */
498       else if (inst == 0x301f || inst == 0x303f)        /* inc r15, #0x1/#0x3 */
499         {
500           if (fi)               /* Record the frame size. */
501             fi->extra_info->framesize += ((inst & 0x0030) >> 4) + 1;
502         }
503
504       /* optional stack allocation for args and local vars > 4 && < 16 byte */
505       else if ((inst & 0xff0f) == 0x510f)       /* 51Hf   add r15, #0xH */
506         {
507           if (fi)               /* Record the frame size. */
508             fi->extra_info->framesize += (inst & 0x00f0) >> 4;
509         }
510
511       /* optional stack allocation for args and local vars >= 16 byte */
512       else if (inst == 0x314f && inst2 >= 0x0010)       /* 314f HHHH  add r15, #0xH */
513         {
514           if (fi)               /* Record the frame size. */
515             fi->extra_info->framesize += inst2;
516           next_addr += xstormy16_inst_size;
517         }
518
519       else if (inst == 0x46fd)  /* mov r13, r15 */
520         {
521           if (fi)               /* Record that the frame pointer is in use. */
522             fi->extra_info->frameless_p = 0;
523           if (frameless)
524             *frameless = 0;
525         }
526
527       /* optional copying of args in r2-r7 to r10-r13 */
528       /* Probably only in optimized case but legal action for prologue */
529       else if ((inst & 0xff00) == 0x4600        /* 46SD   mov rD, rS */
530                && (inst & 0x00f0) >= 0x0020 && (inst & 0x00f0) <= 0x0070
531                && (inst & 0x000f) >= 0x00a0 && (inst & 0x000f) <= 0x000d)
532         ;
533
534       /* optional copying of args in r2-r7 to stack */
535       /* 72DS HHHH   mov.b (rD, 0xHHHH), r(S-8) (bit3 always 1, bit2-0 = reg) */
536       /* 73DS HHHH   mov.w (rD, 0xHHHH), r(S-8) */
537       else if ((inst & 0xfed8) == 0x72d8 && (inst & 0x0007) >= 2)
538         {
539           if (fi)
540             {
541               regnum = inst & 0x0007;
542               /* Only 12 of 16 bits of the argument are used for the
543                  signed offset. */
544               offset = (LONGEST) (inst2 & 0x0fff);
545               if (offset & 0x0800)
546                 offset -= 0x1000;
547
548               fi->saved_regs[regnum] = fi->extra_info->framesize + offset;
549             }
550           next_addr += xstormy16_inst_size;
551         }
552
553 #if 0
554       /* 2001-08-10: Not part of the prologue anymore due to change in
555          ABI. r8 and r9 are not used for argument passing anymore. */
556
557       /* optional copying of r8, r9 to stack */
558       /* 46S7; 73Df HHHH   mov.w r7,rS; mov.w (rD, 0xHHHH), r7 D=8,9; S=13,15 */
559       /* 46S7; 72df HHHH   mov.w r7,rS; mov.b (rD, 0xHHHH), r7 D=8,9; S=13,15 */
560       else if ((inst & 0xffef) == 0x4687 && (inst2 & 0xfedf) == 0x72df)
561         {
562           next_addr += xstormy16_inst_size;
563           if (fi)
564             {
565               regnum = (inst & 0x00f0) >> 4;
566               inst = inst2;
567               inst2 = read_memory_unsigned_integer (next_addr
568                                                     + xstormy16_inst_size,
569                                                     xstormy16_inst_size);
570               /* Only 12 of 16 bits of the argument are used for the
571                  signed offset. */
572               offset = (LONGEST) (inst2 & 0x0fff);
573               if (offset & 0x0800)
574                 offset -= 0x1000;
575
576               fi->saved_regs[regnum] = fi->extra_info->framesize + offset;
577             }
578           next_addr += xstormy16_inst_size;
579         }
580 #endif
581
582       else                      /* Not a prologue instruction. */
583         break;
584     }
585
586   if (fi)
587     {
588       /* Special handling for the "saved" address of the SP:
589          The SP is of course never saved on the stack at all, so
590          by convention what we put here is simply the previous 
591          _value_ of the SP (as opposed to an address where the
592          previous value would have been pushed).  */
593       if (fi->extra_info->frameless_p)
594         {
595           fi->saved_regs[E_SP_REGNUM] = sp - fi->extra_info->framesize;
596           fi->frame = sp;
597         }
598       else
599         {
600           fi->saved_regs[E_SP_REGNUM] = fp - fi->extra_info->framesize;
601           fi->frame = fp;
602         }
603
604       /* So far only offsets to the beginning of the frame are
605          saved in the saved_regs. Now we now the relation between
606          sp, fp and framesize. We know the beginning of the frame
607          so we can translate the register offsets to real addresses. */
608       for (regnum = 0; regnum < E_SP_REGNUM; ++regnum)
609         if (fi->saved_regs[regnum])
610           fi->saved_regs[regnum] += fi->saved_regs[E_SP_REGNUM];
611
612       /* Save address of PC on stack. */
613       fi->saved_regs[E_PC_REGNUM] = fi->saved_regs[E_SP_REGNUM];
614     }
615
616   return next_addr;
617 }
618
619 /* Function: xstormy16_skip_prologue
620    If the input address is in a function prologue, 
621    returns the address of the end of the prologue;
622    else returns the input address.
623
624    Note: the input address is likely to be the function start, 
625    since this function is mainly used for advancing a breakpoint
626    to the first line, or stepping to the first line when we have
627    stepped into a function call.  */
628
629 static CORE_ADDR
630 xstormy16_skip_prologue (CORE_ADDR pc)
631 {
632   CORE_ADDR func_addr = 0, func_end = 0;
633   char *func_name;
634
635   if (find_pc_partial_function (pc, &func_name, &func_addr, &func_end))
636     {
637       struct symtab_and_line sal;
638       struct symbol *sym;
639
640       /* Don't trust line number debug info in frameless functions. */
641       int frameless = 1;
642       CORE_ADDR plg_end = xstormy16_scan_prologue (func_addr, func_end,
643                                                    NULL, &frameless);
644       if (frameless)
645         return plg_end;
646
647       /* Found a function.  */
648       sym = lookup_symbol (func_name, NULL, VAR_NAMESPACE, NULL, NULL);
649       /* Don't use line number debug info for assembly source files. */
650       if (sym && SYMBOL_LANGUAGE (sym) != language_asm)
651         {
652           sal = find_pc_line (func_addr, 0);
653           if (sal.end && sal.end < func_end)
654             {
655               /* Found a line number, use it as end of prologue.  */
656               return sal.end;
657             }
658         }
659       /* No useable line symbol.  Use result of prologue parsing method. */
660       return plg_end;
661     }
662
663   /* No function symbol -- just return the PC. */
664
665   return (CORE_ADDR) pc;
666 }
667
668 /* The epilogue is defined here as the area at the end of a function,
669    either on the `ret' instruction itself or after an instruction which
670    destroys the function's stack frame. */
671 static int
672 xstormy16_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR pc)
673 {
674   CORE_ADDR addr, func_addr = 0, func_end = 0;
675
676   if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
677     {
678       ULONGEST inst, inst2;
679       CORE_ADDR addr = func_end - xstormy16_inst_size;
680
681       /* The Xstormy16 epilogue is max. 14 bytes long. */
682       if (pc < func_end - 7 * xstormy16_inst_size)
683         return 0;
684
685       /* Check if we're on a `ret' instruction.  Otherwise it's
686          too dangerous to proceed. */
687       inst = read_memory_unsigned_integer (addr, xstormy16_inst_size);
688       if (inst != 0x0003)
689         return 0;
690
691       while ((addr -= xstormy16_inst_size) >= func_addr)
692         {
693           inst = read_memory_unsigned_integer (addr, xstormy16_inst_size);
694           if (inst >= 0x009a && inst <= 0x009d) /* pop r10...r13 */
695             continue;
696           if (inst == 0x305f || inst == 0x307f) /* dec r15, #0x1/#0x3 */
697             break;
698           inst2 = read_memory_unsigned_integer (addr - xstormy16_inst_size,
699                                                 xstormy16_inst_size);
700           if (inst2 == 0x314f && inst >= 0x8000)        /* add r15, neg. value */
701             {
702               addr -= xstormy16_inst_size;
703               break;
704             }
705           return 0;
706         }
707       if (pc > addr)
708         return 1;
709     }
710   return 0;
711 }
712
713 /* Function: xstormy16_frame_init_saved_regs
714    Set up the 'saved_regs' array.
715    This is a data structure containing the addresses on the stack 
716    where each register has been saved, for each stack frame.  
717    Registers that have not been saved will have zero here.
718    The stack register is special: rather than the address where the 
719    stack register has been saved, saved_regs[SP_REGNUM] will have the
720    actual value of the previous frame's stack register. 
721
722    This function may be called in any context where the saved register
723    values may be needed (backtrace, frame_info, get_saved_register).
724    On many targets, it is called directly by init_extra_frame_info, 
725    in part because the information may be needed immediately by 
726    frame_chain.
727 */
728
729 static void
730 xstormy16_frame_init_saved_regs (struct frame_info *fi)
731 {
732   CORE_ADDR func_addr, func_end;
733
734   if (!fi->saved_regs)
735     {
736       frame_saved_regs_zalloc (fi);
737
738       /* Find the beginning of this function, so we can analyze its
739          prologue. */
740       if (find_pc_partial_function (fi->pc, NULL, &func_addr, &func_end))
741         xstormy16_scan_prologue (func_addr, fi->pc, fi, NULL);
742       /* Else we're out of luck (can't debug completely stripped code). 
743          FIXME. */
744     }
745 }
746
747 /* Function: xstormy16_frame_saved_pc
748    Returns the return address for the selected frame. 
749    Called by frame_info, frame_chain_valid, and sometimes by
750    get_prev_frame.
751 */
752
753 static CORE_ADDR
754 xstormy16_frame_saved_pc (struct frame_info *fi)
755 {
756   CORE_ADDR saved_pc;
757
758   if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
759     {
760       saved_pc = generic_read_register_dummy (fi->pc, fi->frame, E_PC_REGNUM);
761     }
762   else
763     {
764       saved_pc = read_memory_unsigned_integer (fi->saved_regs[E_PC_REGNUM],
765                                                xstormy16_pc_size);
766     }
767
768   return saved_pc;
769 }
770
771 /* Function: xstormy16_init_extra_frame_info
772    This is the constructor function for the frame_info struct, 
773    called whenever a new frame_info is created (from create_new_frame,
774    and from get_prev_frame).
775 */
776
777 static void
778 xstormy16_init_extra_frame_info (int fromleaf, struct frame_info *fi)
779 {
780   if (!fi->extra_info)
781     {
782       fi->extra_info = (struct frame_extra_info *)
783         frame_obstack_alloc (sizeof (struct frame_extra_info));
784       fi->extra_info->framesize = 0;
785       fi->extra_info->frameless_p = 1;  /* Default frameless, detect framed */
786
787       /* By default, the fi->frame is set to the value of the FP reg by gdb.
788          This may not always be right; we may be in a frameless function,
789          or we may be in the prologue, before the FP has been set up.
790          Unfortunately, we can't make this determination without first
791          calling scan_prologue, and we can't do that unles we know the
792          fi->pc.  */
793
794       if (!fi->pc)
795         {
796           /* Sometimes we are called from get_prev_frame without
797              the PC being set up first.  Long history, don't ask.
798              Fortunately this will never happen from the outermost
799              frame, so we should be able to get the saved pc from
800              the next frame. */
801           if (fi->next)
802             fi->pc = xstormy16_frame_saved_pc (fi->next);
803         }
804
805       /* Take care of the saved_regs right here (non-lazy). */
806       xstormy16_frame_init_saved_regs (fi);
807     }
808 }
809
810 /* Function: xstormy16_frame_chain
811    Returns a pointer to the stack frame of the calling function.
812    Called only from get_prev_frame.  Needed for backtrace, "up", etc.
813 */
814
815 static CORE_ADDR
816 xstormy16_frame_chain (struct frame_info *fi)
817 {
818   if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
819     {
820       /* Call dummy's frame is the same as caller's.  */
821       return fi->frame;
822     }
823   else
824     {
825       /* Return computed offset from this frame's fp. */
826       return fi->frame - fi->extra_info->framesize;
827     }
828 }
829
830 static int
831 xstormy16_frame_chain_valid (CORE_ADDR chain, struct frame_info *thisframe)
832 {
833   return chain < 0x8000 && FRAME_SAVED_PC (thisframe) >= 0x8000 &&
834     (thisframe->extra_info->frameless_p ||
835      thisframe->frame - thisframe->extra_info->framesize == chain);
836 }
837
838 /* Function: xstormy16_saved_pc_after_call
839    Returns the previous PC immediately after a function call.
840    This function is meant to bypass the regular get_saved_register
841    mechanism, ie. it is meant to work even if the frame isn't complete. 
842    Called by step_over_function, and sometimes by get_prev_frame.
843 */
844
845 static CORE_ADDR
846 xstormy16_saved_pc_after_call (struct frame_info *ignore)
847 {
848   CORE_ADDR sp, pc, tmp;
849
850   sp = read_register (E_SP_REGNUM) - xstormy16_pc_size;
851   pc = read_memory_integer (sp, xstormy16_pc_size);
852
853   /* Skip over jump table entry if necessary.  */
854   if ((tmp = SKIP_TRAMPOLINE_CODE (pc)))
855     pc = tmp;
856
857   return pc;
858 }
859
860 static unsigned char *
861 xstormy16_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
862 {
863   static unsigned char breakpoint[] = { 0x06, 0x0 };
864   *lenptr = sizeof (breakpoint);
865   return breakpoint;
866 }
867
868 /* Given a pointer to a jump table entry, return the address
869    of the function it jumps to.  Return 0 if not found. */
870 static CORE_ADDR
871 xstormy16_resolve_jmp_table_entry (CORE_ADDR faddr)
872 {
873   struct obj_section *faddr_sect = find_pc_section (faddr);
874
875   if (faddr_sect)
876     {
877       LONGEST inst, inst2, addr;
878       char buf[2 * xstormy16_inst_size];
879
880       /* Return faddr if it's not pointing into the jump table. */
881       if (strcmp (faddr_sect->the_bfd_section->name, ".plt"))
882         return faddr;
883
884       if (!target_read_memory (faddr, buf, sizeof buf))
885         {
886           inst = extract_unsigned_integer (buf, xstormy16_inst_size);
887           inst2 = extract_unsigned_integer (buf + xstormy16_inst_size,
888                                             xstormy16_inst_size);
889           addr = inst2 << 8 | (inst & 0xff);
890           return addr;
891         }
892     }
893   return 0;
894 }
895
896 /* Given a function's address, attempt to find (and return) the
897    address of the corresponding jump table entry.  Return 0 if
898    not found. */
899 static CORE_ADDR
900 xstormy16_find_jmp_table_entry (CORE_ADDR faddr)
901 {
902   struct obj_section *faddr_sect = find_pc_section (faddr);
903
904   if (faddr_sect)
905     {
906       struct obj_section *osect;
907
908       /* Return faddr if it's already a pointer to a jump table entry. */
909       if (!strcmp (faddr_sect->the_bfd_section->name, ".plt"))
910         return faddr;
911
912       ALL_OBJFILE_OSECTIONS (faddr_sect->objfile, osect)
913       {
914         if (!strcmp (osect->the_bfd_section->name, ".plt"))
915           break;
916       }
917
918       if (osect < faddr_sect->objfile->sections_end)
919         {
920           CORE_ADDR addr;
921           for (addr = osect->addr;
922                addr < osect->endaddr; addr += 2 * xstormy16_inst_size)
923             {
924               int status;
925               LONGEST inst, inst2, faddr2;
926               char buf[2 * xstormy16_inst_size];
927
928               if (target_read_memory (addr, buf, sizeof buf))
929                 return 0;
930               inst = extract_unsigned_integer (buf, xstormy16_inst_size);
931               inst2 = extract_unsigned_integer (buf + xstormy16_inst_size,
932                                                 xstormy16_inst_size);
933               faddr2 = inst2 << 8 | (inst & 0xff);
934               if (faddr == faddr2)
935                 return addr;
936             }
937         }
938     }
939   return 0;
940 }
941
942 static CORE_ADDR
943 xstormy16_skip_trampoline_code (CORE_ADDR pc)
944 {
945   int tmp = xstormy16_resolve_jmp_table_entry (pc);
946
947   if (tmp && tmp != pc)
948     return tmp;
949   return 0;
950 }
951
952 static int
953 xstormy16_in_solib_call_trampoline (CORE_ADDR pc, char *name)
954 {
955   return xstormy16_skip_trampoline_code (pc) != 0;
956 }
957
958 static CORE_ADDR
959 xstormy16_pointer_to_address (struct type *type, void *buf)
960 {
961   enum type_code target = TYPE_CODE (TYPE_TARGET_TYPE (type));
962   CORE_ADDR addr = extract_address (buf, TYPE_LENGTH (type));
963
964   if (target == TYPE_CODE_FUNC || target == TYPE_CODE_METHOD)
965     {
966       CORE_ADDR addr2 = xstormy16_resolve_jmp_table_entry (addr);
967       if (addr2)
968         addr = addr2;
969     }
970
971   return addr;
972 }
973
974 static void
975 xstormy16_address_to_pointer (struct type *type, void *buf, CORE_ADDR addr)
976 {
977   enum type_code target = TYPE_CODE (TYPE_TARGET_TYPE (type));
978
979   if (target == TYPE_CODE_FUNC || target == TYPE_CODE_METHOD)
980     {
981       CORE_ADDR addr2 = xstormy16_find_jmp_table_entry (addr);
982       if (addr2)
983         addr = addr2;
984     }
985   store_address (buf, TYPE_LENGTH (type), addr);
986 }
987
988 static CORE_ADDR
989 xstormy16_stack_align (CORE_ADDR addr)
990 {
991   if (addr & 1)
992     ++addr;
993   return addr;
994 }
995
996 void
997 xstormy16_save_dummy_frame_tos (CORE_ADDR sp)
998 {
999   generic_save_dummy_frame_tos (sp - xstormy16_pc_size);
1000 }
1001
1002 /* Function: xstormy16_gdbarch_init
1003    Initializer function for the xstormy16 gdbarch vector.
1004    Called by gdbarch.  Sets up the gdbarch vector(s) for this target. */
1005
1006 static struct gdbarch *
1007 xstormy16_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1008 {
1009   static LONGEST call_dummy_words[1] = { 0 };
1010   struct gdbarch_tdep *tdep = NULL;
1011   struct gdbarch *gdbarch;
1012
1013   /* find a candidate among the list of pre-declared architectures. */
1014   arches = gdbarch_list_lookup_by_info (arches, &info);
1015   if (arches != NULL)
1016     return (arches->gdbarch);
1017
1018 #if 0
1019   tdep = (struct gdbarch_tdep *) xmalloc (sizeof (struct gdbarch_tdep));
1020 #endif
1021
1022   gdbarch = gdbarch_alloc (&info, 0);
1023
1024   /*
1025    * Basic register fields and methods.
1026    */
1027
1028   set_gdbarch_num_regs (gdbarch, E_NUM_REGS);
1029   set_gdbarch_num_pseudo_regs (gdbarch, 0);
1030   set_gdbarch_sp_regnum (gdbarch, E_SP_REGNUM);
1031   set_gdbarch_fp_regnum (gdbarch, E_FP_REGNUM);
1032   set_gdbarch_pc_regnum (gdbarch, E_PC_REGNUM);
1033   set_gdbarch_register_name (gdbarch, xstormy16_register_name);
1034   set_gdbarch_register_size (gdbarch, xstormy16_reg_size);
1035   set_gdbarch_register_bytes (gdbarch, E_ALL_REGS_SIZE);
1036   set_gdbarch_register_byte (gdbarch, xstormy16_register_byte);
1037   set_gdbarch_register_raw_size (gdbarch, xstormy16_register_raw_size);
1038   set_gdbarch_max_register_raw_size (gdbarch, xstormy16_pc_size);
1039   set_gdbarch_register_virtual_size (gdbarch, xstormy16_register_raw_size);
1040   set_gdbarch_max_register_virtual_size (gdbarch, 4);
1041   set_gdbarch_register_virtual_type (gdbarch, xstormy16_reg_virtual_type);
1042
1043   /*
1044    * Frame Info
1045    */
1046   set_gdbarch_init_extra_frame_info (gdbarch,
1047                                      xstormy16_init_extra_frame_info);
1048   set_gdbarch_frame_init_saved_regs (gdbarch,
1049                                      xstormy16_frame_init_saved_regs);
1050   set_gdbarch_frame_chain (gdbarch, xstormy16_frame_chain);
1051   set_gdbarch_get_saved_register (gdbarch, xstormy16_get_saved_register);
1052   set_gdbarch_saved_pc_after_call (gdbarch, xstormy16_saved_pc_after_call);
1053   set_gdbarch_frame_saved_pc (gdbarch, xstormy16_frame_saved_pc);
1054   set_gdbarch_skip_prologue (gdbarch, xstormy16_skip_prologue);
1055   set_gdbarch_frame_chain_valid (gdbarch, xstormy16_frame_chain_valid);
1056   set_gdbarch_frame_args_address (gdbarch, default_frame_address);
1057   set_gdbarch_frame_locals_address (gdbarch, default_frame_address);
1058
1059   set_gdbarch_in_function_epilogue_p (gdbarch,
1060                                       xstormy16_in_function_epilogue_p);
1061
1062   /* 
1063    * Miscelany
1064    */
1065   /* Stack grows up. */
1066   set_gdbarch_inner_than (gdbarch, core_addr_greaterthan);
1067   /* PC stops zero byte after a trap instruction
1068      (which means: exactly on trap instruction). */
1069   set_gdbarch_decr_pc_after_break (gdbarch, 0);
1070   /* This value is almost never non-zero... */
1071   set_gdbarch_function_start_offset (gdbarch, 0);
1072   /* This value is almost never non-zero... */
1073   set_gdbarch_frame_args_skip (gdbarch, 0);
1074   /* OK to default this value to 'unknown'. */
1075   set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown);
1076
1077   /* W/o prototype, coerce float args to double. */
1078   set_gdbarch_coerce_float_to_double (gdbarch,
1079                                       standard_coerce_float_to_double);
1080
1081   /*
1082    * Call Dummies
1083    * 
1084    * These values and methods are used when gdb calls a target function.  */
1085   set_gdbarch_use_generic_dummy_frames (gdbarch, 1);
1086   set_gdbarch_push_dummy_frame (gdbarch, generic_push_dummy_frame);
1087   set_gdbarch_push_return_address (gdbarch, xstormy16_push_return_address);
1088   set_gdbarch_extract_return_value (gdbarch, xstormy16_extract_return_value);
1089   set_gdbarch_push_arguments (gdbarch, xstormy16_push_arguments);
1090   set_gdbarch_pop_frame (gdbarch, xstormy16_pop_frame);
1091   set_gdbarch_store_struct_return (gdbarch, xstormy16_store_struct_return);
1092   set_gdbarch_store_return_value (gdbarch, xstormy16_store_return_value);
1093   set_gdbarch_extract_struct_value_address (gdbarch,
1094                                             xstormy16_extract_struct_value_address);
1095   set_gdbarch_use_struct_convention (gdbarch,
1096                                      xstormy16_use_struct_convention);
1097   set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
1098   set_gdbarch_call_dummy_address (gdbarch, entry_point_address);
1099   set_gdbarch_call_dummy_start_offset (gdbarch, 0);
1100   set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 0);
1101   set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1);
1102   set_gdbarch_call_dummy_length (gdbarch, 0);
1103   set_gdbarch_pc_in_call_dummy (gdbarch, generic_pc_in_call_dummy);
1104   set_gdbarch_call_dummy_p (gdbarch, 1);
1105   set_gdbarch_call_dummy_words (gdbarch, call_dummy_words);
1106   set_gdbarch_sizeof_call_dummy_words (gdbarch, 0);
1107   set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
1108   /* set_gdbarch_call_dummy_stack_adjust */
1109   set_gdbarch_fix_call_dummy (gdbarch, generic_fix_call_dummy);
1110   set_gdbarch_breakpoint_from_pc (gdbarch, xstormy16_breakpoint_from_pc);
1111
1112   set_gdbarch_int_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1113   set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
1114   set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
1115   set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
1116
1117   set_gdbarch_address_to_pointer (gdbarch, xstormy16_address_to_pointer);
1118   set_gdbarch_pointer_to_address (gdbarch, xstormy16_pointer_to_address);
1119
1120   set_gdbarch_stack_align (gdbarch, xstormy16_stack_align);
1121   set_gdbarch_extra_stack_alignment_needed (gdbarch, 0);
1122
1123   set_gdbarch_save_dummy_frame_tos (gdbarch, xstormy16_save_dummy_frame_tos);
1124
1125   set_gdbarch_skip_trampoline_code (gdbarch, xstormy16_skip_trampoline_code);
1126
1127   set_gdbarch_in_solib_call_trampoline (gdbarch,
1128                                         xstormy16_in_solib_call_trampoline);
1129
1130   return gdbarch;
1131 }
1132
1133 /* Function: _initialize_xstormy16_tdep
1134    Initializer function for the Sanyo Xstormy16a module.
1135    Called by gdb at start-up. */
1136
1137 void
1138 _initialize_xstormy16_tdep (void)
1139 {
1140   extern int print_insn_xstormy16 ();
1141
1142   register_gdbarch_init (bfd_arch_xstormy16, xstormy16_gdbarch_init);
1143   tm_print_insn = print_insn_xstormy16;
1144 }