Tue Nov 12 12:16:40 1996 Michael Snyder <msnyder@cleaver.cygnus.com>
[external/binutils.git] / gdb / sh-tdep.c
1 /* Target-dependent code for Hitachi Super-H, for GDB.
2    Copyright (C) 1993, 1994, 1995, 1996 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, Boston, MA 02111-1307, USA.  */
19
20 /*
21  Contributed by Steve Chamberlain
22                 sac@cygnus.com
23  */
24
25 #include "defs.h"
26 #include "frame.h"
27 #include "obstack.h"
28 #include "symtab.h"
29 #include "gdbtypes.h"
30 #include "gdbcmd.h"
31 #include "gdbcore.h"
32 #include "value.h"
33 #include "dis-asm.h"
34 #include "inferior.h"           /* for BEFORE_TEXT_END etc. */
35
36 extern int remote_write_size;   /* in remote.c */
37
38 /* Default to the original SH.  */
39
40 #define DEFAULT_SH_TYPE "sh"
41
42 /* This value is the model of SH in use.  */
43
44 char *sh_processor_type;
45
46 char *tmp_sh_processor_type;
47
48 /* A set of original names, to be used when restoring back to generic
49    registers from a specific set.  */
50
51 char *sh_generic_reg_names[] = REGISTER_NAMES;
52
53 char *sh_reg_names[] = {
54   "r0",   "r1",   "r2",   "r3",   "r4",   "r5",   "r6",   "r7",
55   "r8",   "r9",   "r10",  "r11",  "r12",  "r13",  "r14",  "r15",
56   "pc",   "pr",   "gbr",  "vbr",  "mach", "macl", "sr",
57   "",     "",
58   "",     "",     "",     "",     "",     "",     "",     "",
59   "",     "",     "",     "",     "",     "",     "",     "",
60   "",     "",
61   "",     "",     "",     "",     "",     "",     "",     "",
62   "",     "",     "",     "",     "",     "",     "",     "",
63 };
64
65 char *sh3_reg_names[] = {
66   "r0",   "r1",   "r2",   "r3",   "r4",   "r5",   "r6",   "r7",
67   "r8",   "r9",   "r10",  "r11",  "r12",  "r13",  "r14",  "r15",
68   "pc",   "pr",   "gbr",  "vbr",  "mach", "macl", "sr",
69   "",     "",
70   "",     "",     "",     "",     "",     "",     "",     "",
71   "",     "",     "",     "",     "",     "",     "",     "",
72   "ssr",  "spc",
73   "r0b0", "r1b0", "r2b0", "r3b0", "r4b0", "r5b0", "r6b0", "r7b0",
74   "r0b1", "r1b1", "r2b1", "r3b1", "r4b1", "r5b1", "r6b1", "r7b1"
75 };
76
77 char *sh3e_reg_names[] = {
78   "r0",   "r1",   "r2",   "r3",   "r4",   "r5",   "r6",   "r7",
79   "r8",   "r9",   "r10",  "r11",  "r12",  "r13",  "r14",  "r15",
80   "pc",   "pr",   "gbr",  "vbr",  "mach", "macl", "sr",
81   "fpul", "fpscr",
82   "fr0",  "fr1",  "fr2",  "fr3",  "fr4",  "fr5",  "fr6",  "fr7",
83   "fr8",  "fr9",  "fr10", "fr11", "fr12", "fr13", "fr14", "fr15",
84   "ssr",  "spc",
85   "r0b0", "r1b0", "r2b0", "r3b0", "r4b0", "r5b0", "r6b0", "r7b0",
86   "r0b1", "r1b1", "r2b1", "r3b1", "r4b1", "r5b1", "r6b1", "r7b1",
87 };
88
89 struct {
90   char *name;
91   char **regnames;
92 } sh_processor_type_table[] = {
93   { "sh", sh_reg_names },
94   { "sh3", sh3_reg_names },
95   { "sh3e", sh3e_reg_names },
96   { NULL, NULL }
97 };
98
99 /* Prologue looks like
100    [mov.l       <regs>,@-r15]...
101    [sts.l       pr,@-r15]
102    [mov.l       r14,@-r15]
103    [mov         r15,r14]
104 */
105
106 #define IS_STS(x)               ((x) == 0x4f22)
107 #define IS_PUSH(x)              (((x) & 0xff0f) == 0x2f06)
108 #define GET_PUSHED_REG(x)       (((x) >> 4) & 0xf)
109 #define IS_MOV_SP_FP(x)         ((x) == 0x6ef3)
110 #define IS_ADD_SP(x)            (((x) & 0xff00) == 0x7f00)
111 #define IS_MOV_R3(x)            (((x) & 0xff00) == 0x1a00)
112 #define IS_SHLL_R3(x)           ((x) == 0x4300)
113 #define IS_ADD_R3SP(x)          ((x) == 0x3f3c)
114
115 /* Skip any prologue before the guts of a function */
116
117 CORE_ADDR
118 sh_skip_prologue (start_pc)
119      CORE_ADDR start_pc;
120 {
121   int w;
122
123   w = read_memory_integer (start_pc, 2);
124   while (IS_STS (w)
125          || IS_PUSH (w)
126          || IS_MOV_SP_FP (w)
127          || IS_MOV_R3 (w)
128          || IS_ADD_R3SP (w)
129          || IS_ADD_SP (w)
130          || IS_SHLL_R3 (w))
131     {
132       start_pc += 2;
133       w = read_memory_integer (start_pc, 2);
134     }
135
136   return start_pc;
137 }
138
139 /* Disassemble an instruction.  */
140
141 int
142 gdb_print_insn_sh (memaddr, info)
143      bfd_vma memaddr;
144      disassemble_info *info;
145 {
146   if (TARGET_BYTE_ORDER == BIG_ENDIAN)
147     return print_insn_sh (memaddr, info);
148   else
149     return print_insn_shl (memaddr, info);
150 }
151
152 /* Given a GDB frame, determine the address of the calling function's frame.
153    This will be used to create a new GDB frame struct, and then
154    INIT_EXTRA_FRAME_INFO and INIT_FRAME_PC will be called for the new frame.
155
156    For us, the frame address is its stack pointer value, so we look up
157    the function prologue to determine the caller's sp value, and return it.  */
158
159 CORE_ADDR
160 sh_frame_chain (frame)
161      struct frame_info *frame;
162 {
163   if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))
164     return frame->frame;        /* dummy frame same as caller's frame */
165   if (!inside_entry_file (frame->pc))
166     return read_memory_integer (FRAME_FP (frame) + frame->f_offset, 4);
167   else
168     return 0;
169 }
170
171 /* Find REGNUM on the stack.  Otherwise, it's in an active register.  One thing
172    we might want to do here is to check REGNUM against the clobber mask, and
173    somehow flag it as invalid if it isn't saved on the stack somewhere.  This
174    would provide a graceful failure mode when trying to get the value of
175    caller-saves registers for an inner frame.  */
176
177 CORE_ADDR
178 sh_find_callers_reg (fi, regnum)
179      struct frame_info *fi;
180      int regnum;
181 {
182   struct frame_saved_regs fsr;
183
184   for (; fi; fi = fi->next)
185     if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
186       /* When the caller requests PR from the dummy frame, we return PC because
187          that's where the previous routine appears to have done a call from. */
188       return generic_read_register_dummy (fi, regnum);
189     else 
190       {
191         FRAME_FIND_SAVED_REGS(fi, fsr);
192         if (fsr.regs[regnum] != 0)
193           return read_memory_integer (fsr.regs[regnum], 
194                                       REGISTER_RAW_SIZE(regnum));
195       }
196   return read_register (regnum);
197 }
198
199 /* Put here the code to store, into a struct frame_saved_regs, the
200    addresses of the saved registers of frame described by FRAME_INFO.
201    This includes special registers such as pc and fp saved in special
202    ways in the stack frame.  sp is even more special: the address we
203    return for it IS the sp for the next frame. */
204
205 /* FIXME!  A lot of this should be abstracted out into a sh_scan_prologue 
206    function, and the struct frame_info should have a frame_saved_regs
207    embedded in it, so we would only have to do this once. */
208
209 void
210 sh_frame_find_saved_regs (fi, fsr)
211      struct frame_info *fi;
212      struct frame_saved_regs *fsr;
213 {
214   int where[NUM_REGS];
215   int rn;
216   int have_fp = 0;
217   int depth;
218   int pc;
219   int opc;
220   int insn;
221   int r3_val = 0;
222   char * dummy_regs = generic_find_dummy_frame (fi->pc, fi->frame, fi->frame);
223
224   if (dummy_regs)
225     {
226       /* DANGER!  This is ONLY going to work if the char buffer format of
227          the saved registers is byte-for-byte identical to the 
228          CORE_ADDR regs[NUM_REGS] format used by struct frame_saved_regs! */
229       memcpy (&fsr->regs, dummy_regs, sizeof(fsr));
230       return;
231     }
232
233   opc = pc = get_pc_function_start (fi->pc);
234
235   insn = read_memory_integer (pc, 2);
236
237   fi->leaf_function = 1;
238   fi->f_offset = 0;
239
240   for (rn = 0; rn < NUM_REGS; rn++)
241     where[rn] = -1;
242
243   depth = 0;
244
245   /* Loop around examining the prologue insns, but give up
246      after 15 of them, since we're getting silly then */
247   while (pc < opc + 15 * 2)
248     {
249       /* See where the registers will be saved to */
250       if (IS_PUSH (insn))
251         {
252           pc += 2;
253           rn = GET_PUSHED_REG (insn);
254           where[rn] = depth;
255           insn = read_memory_integer (pc, 2);
256           depth += 4;
257         }
258       else if (IS_STS (insn))
259         {
260           pc += 2;
261           where[PR_REGNUM] = depth;
262           insn = read_memory_integer (pc, 2);
263           /* If we're storing the pr then this isn't a leaf */
264           fi->leaf_function = 0;
265           depth += 4;
266         }
267       else if (IS_MOV_R3 (insn))
268         {
269           r3_val = (char) (insn & 0xff);
270           pc += 2;
271           insn = read_memory_integer (pc, 2);
272         }
273       else if (IS_SHLL_R3 (insn))
274         {
275           r3_val <<= 1;
276           pc += 2;
277           insn = read_memory_integer (pc, 2);
278         }
279       else if (IS_ADD_R3SP (insn))
280         {
281           depth += -r3_val;
282           pc += 2;
283           insn = read_memory_integer (pc, 2);
284         }
285       else if (IS_ADD_SP (insn))
286         {
287           pc += 2;
288           depth += -((char) (insn & 0xff));
289           insn = read_memory_integer (pc, 2);
290         }
291       else
292         break;
293     }
294
295   /* Now we know how deep things are, we can work out their addresses */
296
297   for (rn = 0; rn < NUM_REGS; rn++)
298     {
299       if (where[rn] >= 0)
300         {
301           if (rn == FP_REGNUM)
302             have_fp = 1;
303
304           fsr->regs[rn] = fi->frame - where[rn] + depth - 4;
305         }
306       else
307         {
308           fsr->regs[rn] = 0;
309         }
310     }
311
312   if (have_fp)
313     {
314       fsr->regs[SP_REGNUM] = read_memory_integer (fsr->regs[FP_REGNUM], 4);
315     }
316   else
317     {
318       fsr->regs[SP_REGNUM] = fi->frame - 4;
319     }
320
321   fi->f_offset = depth - where[FP_REGNUM] - 4;
322   /* Work out the return pc - either from the saved pr or the pr
323      value */
324 }
325
326 /* initialize the extra info saved in a FRAME */
327
328 void
329 sh_init_extra_frame_info (fromleaf, fi)
330      int fromleaf;
331      struct frame_info *fi;
332 {
333   struct frame_saved_regs fsr;
334
335   if (fi->next)
336     fi->pc = FRAME_SAVED_PC (fi->next);
337
338   if (PC_IN_CALL_DUMMY (fi->pc, fi->frame, fi->frame))
339     {
340       /* We need to setup fi->frame here because run_stack_dummy gets it wrong
341          by assuming it's always FP.  */
342       fi->frame     = generic_read_register_dummy (fi, SP_REGNUM);
343       fi->return_pc = generic_read_register_dummy (fi, PC_REGNUM);
344       fi->f_offset = -(CALL_DUMMY_LENGTH + 4);
345       fi->leaf_function = 0;
346       return;
347     }
348   else
349     {
350       FRAME_FIND_SAVED_REGS (fi, fsr);
351       fi->return_pc = sh_find_callers_reg (fi, PR_REGNUM);
352     }
353 }
354
355 /* Discard from the stack the innermost frame,
356    restoring all saved registers.  */
357
358 void
359 sh_pop_frame ()
360 {
361   register struct frame_info *frame = get_current_frame ();
362   register CORE_ADDR fp;
363   register int regnum;
364   struct frame_saved_regs fsr;
365
366   if (PC_IN_CALL_DUMMY (frame->pc, frame->frame, frame->frame))
367     generic_pop_dummy_frame ();
368   else
369   {
370     fp = FRAME_FP (frame);
371     get_frame_saved_regs (frame, &fsr);
372
373     /* Copy regs from where they were saved in the frame */
374     for (regnum = 0; regnum < NUM_REGS; regnum++)
375       if (fsr.regs[regnum])
376         write_register (regnum, read_memory_integer (fsr.regs[regnum], 4));
377
378     write_register (PC_REGNUM, frame->return_pc);
379     write_register (SP_REGNUM, fp + 4);
380   }
381   flush_cached_frames ();
382 }
383
384 /* Function: push_arguments
385    Setup the function arguments for calling a function in the inferior.
386
387    On the Hitachi SH architecture, there are four registers (R4 to R7)
388    which are dedicated for passing function arguments.  Up to the first
389    four arguments (depending on size) may go into these registers.
390    The rest go on the stack.
391
392    Arguments that are smaller than 4 bytes will still take up a whole
393    register or a whole 32-bit word on the stack, and will be 
394    right-justified in the register or the stack word.  This includes
395    chars, shorts, and small aggregate types.
396
397    Arguments that are larger than 4 bytes may be split between two or 
398    more registers.  If there are not enough registers free, an argument
399    may be passed partly in a register (or registers), and partly on the
400    stack.  This includes doubles, long longs, and larger aggregates. 
401    As far as I know, there is no upper limit to the size of aggregates 
402    that will be passed in this way; in other words, the convention of 
403    passing a pointer to a large aggregate instead of a copy is not used.
404
405    An exceptional case exists for struct arguments (and possibly other
406    aggregates such as arrays) if the size is larger than 4 bytes but 
407    not a multiple of 4 bytes.  In this case the argument is never split 
408    between the registers and the stack, but instead is copied in its
409    entirety onto the stack, AND also copied into as many registers as 
410    there is room for.  In other words, space in registers permitting, 
411    two copies of the same argument are passed in.  As far as I can tell,
412    only the one on the stack is used, although that may be a function 
413    of the level of compiler optimization.  I suspect this is a compiler
414    bug.  Arguments of these odd sizes are left-justified within the 
415    word (as opposed to arguments smaller than 4 bytes, which are 
416    right-justified).
417  
418
419    If the function is to return an aggregate type such as a struct, it 
420    is either returned in the normal return value register R0 (if its 
421    size is no greater than one byte), or else the caller must allocate
422    space into which the callee will copy the return value (if the size
423    is greater than one byte).  In this case, a pointer to the return 
424    value location is passed into the callee in register R2, which does 
425    not displace any of the other arguments passed in via registers R4
426    to R7.   */
427
428 CORE_ADDR
429 sh_push_arguments (nargs, args, sp, struct_return, struct_addr)
430      int nargs;
431      value_ptr *args;
432      CORE_ADDR sp;
433      unsigned char struct_return;
434      CORE_ADDR struct_addr;
435 {
436   int argreg;
437   int argnum;
438   CORE_ADDR regval;
439   char *val;
440   char valbuf[4];
441   int len;
442   int push[4];          /* some of the first 4 args may not need to be pushed
443                            onto the stack, because they can go in registers */
444
445   /* first force sp to a 4-byte alignment */
446   sp = sp & ~3;
447
448   /* The "struct return pointer" pseudo-argument has its own dedicated 
449      register */
450   if (struct_return)
451       write_register (STRUCT_RETURN_REGNUM, struct_addr);
452
453   /* Now load as many as possible of the first arguments into registers.
454      There are 16 bytes in four registers available. 
455      Loop thru args from first to last.  */
456   push[0] = push[1] = push[2] = push[3] = 0;
457   for (argnum = 0, argreg = ARG0_REGNUM; 
458        argnum < nargs && argreg <= ARGLAST_REGNUM; 
459        argnum++)
460     {
461       struct type *type = VALUE_TYPE (args[argnum]);
462       
463       len = TYPE_LENGTH (type);
464
465       switch (TYPE_CODE(type)) {
466       case TYPE_CODE_STRUCT:
467       case TYPE_CODE_UNION:
468       /* case TYPE_CODE_ARRAY:  case TYPE_CODE_STRING: */
469         if (len <= 4   ||   (len & ~3) == 0)
470           push[argnum] = 0;             /* doesn't get pushed onto stack */
471         else
472           push[argnum] = len;           /* does    get pushed onto stack */
473         break;
474       default:
475         push[argnum] = 0;               /* doesn't get pushed onto stack */
476       }
477       if (len < 4)
478         { /* value gets right-justified in the register */
479           memcpy(valbuf + (4 - len), 
480                  (char *) VALUE_CONTENTS (args[argnum]), len);
481           val = valbuf;
482         }
483       else
484         val = (char *) VALUE_CONTENTS (args[argnum]);
485
486       while (len > 0)
487         {
488           regval = extract_address (val, REGISTER_RAW_SIZE (argreg));
489           write_register (argreg, regval);
490
491           len -= REGISTER_RAW_SIZE (argreg);
492           val += REGISTER_RAW_SIZE (argreg);
493           argreg++;
494           if (argreg > ARGLAST_REGNUM)
495             {
496               push[argnum] = len;       /* ran out of arg passing registers! */
497               break;                    /* len bytes remain to go onto stack */
498             }
499         }
500     }
501
502   /* Now push as many as necessary of the remaining arguments onto the stack.
503      For args 0 to 3, the arg may have been passed in a register. 
504      Loop thru args from last to first.  */
505   for (argnum = nargs-1; argnum >= 0; --argnum)
506     {
507       if (argnum < 4 && push[argnum] == 0)
508         continue;       /* no need to push this arg */
509
510       len = TYPE_LENGTH (VALUE_TYPE (args[argnum]));
511       if (len < 4)
512         {
513           memcpy(valbuf + (4 - len), 
514                  (char *) VALUE_CONTENTS (args[argnum]), len);
515           val = valbuf;
516         }
517       else
518         val = (char *) VALUE_CONTENTS (args[argnum]);
519
520       if (argnum < 4)
521         if (len > push[argnum])         /* some part may already be in a reg */
522           {
523             val += (len - push[argnum]);
524             len = push[argnum];
525           }
526
527       sp -= (len + 3) & ~3;
528       write_memory (sp, val, len);
529     }
530   return sp;
531 }
532
533 /* Function: push_return_address (pc)
534    Set up the return address for the inferior function call.
535    Necessary for targets where we don't actually execute a JSR/BSR instruction */
536
537 void
538 sh_push_return_address (pc)
539      CORE_ADDR pc;
540 {
541   write_register (PR_REGNUM, entry_point_address ());
542 }
543
544 /* Command to set the processor type.  */
545
546 void
547 sh_set_processor_type_command (args, from_tty)
548      char *args;
549      int from_tty;
550 {
551   int i;
552   char *temp;
553
554   /* The `set' commands work by setting the value, then calling the hook,
555      so we let the general command modify a scratch location, then decide
556      here if we really want to modify the processor type.  */
557   if (tmp_sh_processor_type == NULL || *tmp_sh_processor_type == '\0')
558     {
559       printf_unfiltered ("The known SH processor types are as follows:\n\n");
560       for (i = 0; sh_processor_type_table[i].name != NULL; ++i)
561         printf_unfiltered ("%s\n", sh_processor_type_table[i].name);
562
563       /* Restore the value.  */
564       tmp_sh_processor_type = strsave (sh_processor_type);
565
566       return;
567     }
568   
569   if (!sh_set_processor_type (tmp_sh_processor_type))
570     {
571       /* Restore to a valid value before erroring out.  */
572       temp = tmp_sh_processor_type;
573       tmp_sh_processor_type = strsave (sh_processor_type);
574       error ("Unknown processor type `%s'.", temp);
575     }
576 }
577
578 /* This is a dummy not actually run.  */
579
580 static void
581 sh_show_processor_type_command (args, from_tty)
582      char *args;
583      int from_tty;
584 {
585 }
586
587 /* Modify the actual processor type. */
588
589 int
590 sh_set_processor_type (str)
591      char *str;
592 {
593   int i, j;
594
595   if (str == NULL)
596     return 0;
597
598   for (i = 0; sh_processor_type_table[i].name != NULL; ++i)
599     {
600       if (strcasecmp (str, sh_processor_type_table[i].name) == 0)
601         {
602           sh_processor_type = str;
603
604           for (j = 0; j < NUM_REGS; ++j)
605             reg_names[j] = sh_processor_type_table[i].regnames[j];
606
607           return 1;
608         }
609     }
610
611   return 0;
612 }
613
614 /* Print the registers in a form similar to the E7000 */
615
616 static void
617 sh_show_regs (args, from_tty)
618      char *args;
619      int from_tty;
620 {
621   printf_filtered ("PC=%08x SR=%08x PR=%08x MACH=%08x MACHL=%08x\n",
622                    read_register (PC_REGNUM),
623                    read_register (SR_REGNUM),
624                    read_register (PR_REGNUM),
625                    read_register (MACH_REGNUM),
626                    read_register (MACL_REGNUM));
627
628   printf_filtered ("R0-R7  %08x %08x %08x %08x %08x %08x %08x %08x\n",
629                    read_register (0),
630                    read_register (1),
631                    read_register (2),
632                    read_register (3),
633                    read_register (4),
634                    read_register (5),
635                    read_register (6),
636                    read_register (7));
637   printf_filtered ("R8-R15 %08x %08x %08x %08x %08x %08x %08x %08x\n",
638                    read_register (8),
639                    read_register (9),
640                    read_register (10),
641                    read_register (11),
642                    read_register (12),
643                    read_register (13),
644                    read_register (14),
645                    read_register (15));
646 }
647
648 void
649 sh_extract_return_value (type, regbuf, valbuf)
650      struct type *type;
651      void *regbuf;
652      void *valbuf;
653 {
654   int len = TYPE_LENGTH(type);
655
656   if (len <= 4)
657     memcpy (valbuf, ((char *) regbuf) + 4 - len, len);
658   else if (len <= 8)
659     memcpy (valbuf, ((char *) regbuf) + 8 - len, len);
660   else
661     error ("bad size for return value");
662 }
663
664 void
665 _initialize_sh_tdep ()
666 {
667   struct cmd_list_element *c;
668
669   tm_print_insn = gdb_print_insn_sh;
670
671   c = add_set_cmd ("processor", class_support, var_string_noescape,
672                    (char *) &tmp_sh_processor_type,
673                    "Set the type of SH processor in use.\n\
674 Set this to be able to access processor-type-specific registers.\n\
675 ",
676                    &setlist);
677   c->function.cfunc = sh_set_processor_type_command;
678   c = add_show_from_set (c, &showlist);
679   c->function.cfunc = sh_show_processor_type_command;
680
681   tmp_sh_processor_type = strsave (DEFAULT_SH_TYPE);
682   sh_set_processor_type_command (strsave (DEFAULT_SH_TYPE), 0);
683
684   add_com ("regs", class_vars, sh_show_regs, "Print all registers");
685
686   /* Reduce the remote write size because some CMONs can't take
687     more than 400 bytes in a packet.  300 seems like a safe bet.  */
688   remote_write_size = 300;
689 }
690
691 /*
692  * DUMMY FRAMES
693  * 
694  * The following code serves to maintain the dummy stack frames for
695  * inferior function calls (ie. when gdb calls into the inferior via
696  * call_function_by_hand).  This code saves the machine state before 
697  * the call in host memory, so it must maintain an independant stack 
698  * and keep it consistant etc.  I am attempting to make this code 
699  * generic enough to be used by many targets.
700  *
701  * The cheapest and most generic way to do CALL_DUMMY on a new target
702  * is probably to define CALL_DUMMY to be empty, CALL_DUMMY_LENGTH to zero,
703  * and CALL_DUMMY_LOCATION to AT_ENTRY.  Then you must remember to define
704  * PUSH_RETURN_ADDRESS, because there won't be a call instruction to do it.
705  */
706
707 /* Dummy frame.  This saves the processor state just prior to setting up the
708    inferior function call.  On most targets, the registers are saved on the
709    target stack, but that really slows down function calls.  */
710
711 struct dummy_frame
712 {
713   struct dummy_frame *next;
714
715   CORE_ADDR pc;
716   CORE_ADDR fp;
717   CORE_ADDR sp;
718   char regs[REGISTER_BYTES];
719 };
720
721 static struct dummy_frame *dummy_frame_stack = NULL;
722
723 /* Function: find_dummy_frame(pc, fp, sp)
724    Search the stack of dummy frames for one matching the given PC, FP and SP.
725    This is the work-horse for pc_in_call_dummy and read_register_dummy     */
726
727 char * 
728 generic_find_dummy_frame (pc, fp, sp)
729      CORE_ADDR pc;
730      CORE_ADDR fp;
731      CORE_ADDR sp;
732 {
733   struct dummy_frame * dummyframe;
734   CORE_ADDR bkpt_address;
735   extern CORE_ADDR text_end;
736
737 #if CALL_DUMMY_LOCATION == AT_ENTRY_POINT
738   bkpt_address = entry_point_address () + CALL_DUMMY_BREAKPOINT_OFFSET;
739   if (pc != bkpt_address &&
740       pc != bkpt_address + DECR_PC_AFTER_BREAK)
741     return 0;
742 #endif  /* AT_ENTRY_POINT */
743
744 #if CALL_DUMMY_LOCATION == BEFORE_TEXT_END
745   bkpt_address = text_end - CALL_DUMMY_LENGTH + CALL_DUMMY_BREAKPOINT_OFFSET;
746   if (pc != bkpt_address &&
747       pc != bkpt_address + DECR_PC_AFTER_BREAK)
748     return 0;
749 #endif  /* BEFORE_TEXT_END */
750
751 #if CALL_DUMMY_LOCATION == AFTER_TEXT_END
752   bkpt_address = text_end + CALL_DUMMY_BREAKPOINT_OFFSET;
753   if (pc != bkpt_address &&
754       pc != bkpt_address + DECR_PC_AFTER_BREAK)
755     return 0;
756 #endif  /* AFTER_TEXT_END */
757
758   for (dummyframe = dummy_frame_stack;
759        dummyframe;
760        dummyframe = dummyframe->next)
761     if (fp == dummyframe->fp || 
762         sp == dummyframe->sp)
763       {
764 #if CALL_DUMMY_LOCATION == ON_STACK
765         CORE_ADDR bkpt_offset;  /* distance from original frame ptr to bkpt */
766
767         if (1 INNER_THAN 2)
768           bkpt_offset = CALL_DUMMY_BREAK_OFFSET;
769         else
770           bkpt_offset = CALL_DUMMY_LENGTH - CALL_DUMMY_BREAK_OFFSET;
771
772         if (pc + bkpt_offset == dummyframe->fp ||
773             pc + bkpt_offset == dummyframe->sp ||
774             pc + bkpt_offset + DECR_PC_AFTER_BREAK == dummyframe->fp ||
775             pc + bkpt_offset + DECR_PC_AFTER_BREAK == dummyframe->sp)
776 #endif /* ON_STACK */
777           return dummyframe->regs;
778       }
779   return 0;
780 }
781
782 /* Function: pc_in_call_dummy (pc, fp, sp)
783    Return true if this is a dummy frame created by gdb for an inferior call */
784
785 int
786 generic_pc_in_call_dummy (pc, fp, sp)
787      CORE_ADDR pc;
788      CORE_ADDR fp;
789      CORE_ADDR sp;
790 {
791   /* if find_dummy_frame succeeds, then PC is in a call dummy */
792   return (generic_find_dummy_frame (pc, fp, sp) != 0);
793 }
794
795 /* Function: read_register_dummy (pc, fp, sp, regno)
796    Find a saved register from before GDB calls a function in the inferior */
797
798 CORE_ADDR
799 generic_read_register_dummy (fi, regno)
800      struct frame_info *fi;
801      int regno;
802 {
803   char *dummy_regs = generic_find_dummy_frame (fi->pc, fi->frame, NULL);
804
805   if (dummy_regs)
806     return extract_address (&dummy_regs[REGISTER_BYTE (regno)],
807                             REGISTER_RAW_SIZE(regno));
808   else
809     return 0;
810 }
811
812 /* Save all the registers on the dummy frame stack.  Most ports save the
813    registers on the target stack.  This results in lots of unnecessary memory
814    references, which are slow when debugging via a serial line.  Instead, we
815    save all the registers internally, and never write them to the stack.  The
816    registers get restored when the called function returns to the entry point,
817    where a breakpoint is laying in wait.  */
818
819 void
820 generic_push_dummy_frame ()
821 {
822   struct dummy_frame *dummy_frame;
823   CORE_ADDR fp = read_register(FP_REGNUM);
824
825   /* check to see if there are stale dummy frames, 
826      perhaps left over from when a longjump took us out of a 
827      function that was called by the debugger */
828
829   dummy_frame = dummy_frame_stack;
830   while (dummy_frame)
831     if (dummy_frame->fp INNER_THAN fp)  /* stale -- destroy! */
832       {
833         dummy_frame_stack = dummy_frame->next;
834         free (dummy_frame);
835         dummy_frame = dummy_frame_stack;
836       }
837     else
838       dummy_frame = dummy_frame->next;
839
840   dummy_frame = xmalloc (sizeof (struct dummy_frame));
841
842   read_register_bytes (0, dummy_frame->regs, REGISTER_BYTES);
843   dummy_frame->pc   = read_register (PC_REGNUM);
844   dummy_frame->fp   = read_register (FP_REGNUM);
845   dummy_frame->sp   = read_register (SP_REGNUM);
846   dummy_frame->next = dummy_frame_stack;
847   dummy_frame_stack = dummy_frame;
848 }
849
850 /* Function: pop_dummy_frame
851    Restore the machine state from a saved dummy stack frame. */
852
853 void
854 generic_pop_dummy_frame ()
855 {
856   struct dummy_frame *dummy_frame = dummy_frame_stack;
857
858   if (!dummy_frame)
859     error ("Can't pop dummy frame!");
860   dummy_frame_stack = dummy_frame->next;
861   write_register_bytes (0, dummy_frame->regs, REGISTER_BYTES);
862   free (dummy_frame);
863 }
864
865 /* Function: frame_chain_valid 
866    Returns true for a user frame or a call_function_by_hand dummy frame,
867    and false for the CRT0 start-up frame.  Purpose is to terminate backtrace */
868
869 int
870 generic_frame_chain_valid (fp, fi)
871      CORE_ADDR fp;
872      struct frame_info *fi;
873 {
874   if (PC_IN_CALL_DUMMY(FRAME_SAVED_PC(fi), fp, fp))
875     return 1;   /* don't prune CALL_DUMMY frames */
876   else          /* fall back to default algorithm (see frame.h) */
877     return (fp != 0 && !inside_entry_file (FRAME_SAVED_PC(fi)));
878 }
879