OSDN Git Service

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