da83eb2b2be84082b160866e69b69c86d09a1c3a
[platform/upstream/binutils.git] / gdb / mn10200-tdep.c
1 /* Target-dependent code for the Matsushita MN10200 for GDB, the GNU debugger.
2    Copyright 1997, 1998, 1999, 2000, 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 "frame.h"
23 #include "inferior.h"
24 #include "target.h"
25 #include "value.h"
26 #include "bfd.h"
27 #include "gdb_string.h"
28 #include "gdbcore.h"
29 #include "symfile.h"
30 #include "regcache.h"
31
32
33 /* Should call_function allocate stack space for a struct return?  */
34 int
35 mn10200_use_struct_convention (int gcc_p, struct type *type)
36 {
37   return (TYPE_NFIELDS (type) > 1 || TYPE_LENGTH (type) > 8);
38 }
39 /* *INDENT-OFF* */
40 /* The main purpose of this file is dealing with prologues to extract
41    information about stack frames and saved registers.
42
43    For reference here's how prologues look on the mn10200:
44
45      With frame pointer:
46         mov fp,a0
47         mov sp,fp
48         add <size>,sp
49         Register saves for d2, d3, a1, a2 as needed.  Saves start
50         at fp - <size> + <outgoing_args_size> and work towards higher
51         addresses.  Note that the saves are actually done off the stack
52         pointer in the prologue!  This makes for smaller code and easier
53         prologue scanning as the displacement fields will unlikely
54         be more than 8 bits!
55
56      Without frame pointer:
57         add <size>,sp
58         Register saves for d2, d3, a1, a2 as needed.  Saves start
59         at sp + <outgoing_args_size> and work towards higher addresses.
60
61      Out of line prologue:
62         add <local size>,sp  -- optional
63         jsr __prologue
64         add <outgoing_size>,sp -- optional
65
66    The stack pointer remains constant throughout the life of most
67    functions.  As a result the compiler will usually omit the
68    frame pointer, so we must handle frame pointerless functions.  */
69
70 /* Analyze the prologue to determine where registers are saved,
71    the end of the prologue, etc etc.  Return the end of the prologue
72    scanned.
73
74    We store into FI (if non-null) several tidbits of information:
75
76     * stack_size -- size of this stack frame.  Note that if we stop in
77     certain parts of the prologue/epilogue we may claim the size of the
78     current frame is zero.  This happens when the current frame has
79     not been allocated yet or has already been deallocated.
80
81     * fsr -- Addresses of registers saved in the stack by this frame.
82
83     * status -- A (relatively) generic status indicator.  It's a bitmask
84     with the following bits: 
85
86       MY_FRAME_IN_SP: The base of the current frame is actually in
87       the stack pointer.  This can happen for frame pointerless
88       functions, or cases where we're stopped in the prologue/epilogue
89       itself.  For these cases mn10200_analyze_prologue will need up
90       update fi->frame before returning or analyzing the register
91       save instructions.
92
93       MY_FRAME_IN_FP: The base of the current frame is in the
94       frame pointer register ($a2).
95
96       CALLER_A2_IN_A0: $a2 from the caller's frame is temporarily
97       in $a0.  This can happen if we're stopped in the prologue.
98
99       NO_MORE_FRAMES: Set this if the current frame is "start" or
100       if the first instruction looks like mov <imm>,sp.  This tells
101       frame chain to not bother trying to unwind past this frame.  */
102 /* *INDENT-ON* */
103
104
105
106
107 #define MY_FRAME_IN_SP 0x1
108 #define MY_FRAME_IN_FP 0x2
109 #define CALLER_A2_IN_A0 0x4
110 #define NO_MORE_FRAMES 0x8
111
112 static CORE_ADDR
113 mn10200_analyze_prologue (struct frame_info *fi, CORE_ADDR pc)
114 {
115   CORE_ADDR func_addr, func_end, addr, stop;
116   CORE_ADDR stack_size = 0;
117   unsigned char buf[4];
118   int status;
119   char *name;
120   int out_of_line_prologue = 0;
121
122   /* Use the PC in the frame if it's provided to look up the
123      start of this function.  */
124   pc = (fi ? get_frame_pc (fi) : pc);
125
126   /* Find the start of this function.  */
127   status = find_pc_partial_function (pc, &name, &func_addr, &func_end);
128
129   /* Do nothing if we couldn't find the start of this function or if we're
130      stopped at the first instruction in the prologue.  */
131   if (status == 0)
132     return pc;
133
134   /* If we're in start, then give up.  */
135   if (strcmp (name, "start") == 0)
136     {
137       if (fi)
138         fi->status = NO_MORE_FRAMES;
139       return pc;
140     }
141
142   /* At the start of a function our frame is in the stack pointer.  */
143   if (fi)
144     fi->status = MY_FRAME_IN_SP;
145
146   /* If we're physically on an RTS instruction, then our frame has already
147      been deallocated.
148
149      fi->frame is bogus, we need to fix it.  */
150   if (fi && get_frame_pc (fi) + 1 == func_end)
151     {
152       status = target_read_memory (get_frame_pc (fi), buf, 1);
153       if (status != 0)
154         {
155           if (get_next_frame (fi) == NULL)
156             deprecated_update_frame_base_hack (fi, read_sp ());
157           return get_frame_pc (fi);
158         }
159
160       if (buf[0] == 0xfe)
161         {
162           if (get_next_frame (fi) == NULL)
163             deprecated_update_frame_base_hack (fi, read_sp ());
164           return get_frame_pc (fi);
165         }
166     }
167
168   /* Similarly if we're stopped on the first insn of a prologue as our
169      frame hasn't been allocated yet.  */
170   if (fi && get_frame_pc (fi) == func_addr)
171     {
172       if (get_next_frame (fi) == NULL)
173         deprecated_update_frame_base_hack (fi, read_sp ());
174       return get_frame_pc (fi);
175     }
176
177   /* Figure out where to stop scanning.  */
178   stop = fi ? get_frame_pc (fi) : func_end;
179
180   /* Don't walk off the end of the function.  */
181   stop = stop > func_end ? func_end : stop;
182
183   /* Start scanning on the first instruction of this function.  */
184   addr = func_addr;
185
186   status = target_read_memory (addr, buf, 2);
187   if (status != 0)
188     {
189       if (fi && get_next_frame (fi) == NULL && fi->status & MY_FRAME_IN_SP)
190         deprecated_update_frame_base_hack (fi, read_sp ());
191       return addr;
192     }
193
194   /* First see if this insn sets the stack pointer; if so, it's something
195      we won't understand, so quit now.   */
196   if (buf[0] == 0xdf
197       || (buf[0] == 0xf4 && buf[1] == 0x77))
198     {
199       if (fi)
200         fi->status = NO_MORE_FRAMES;
201       return addr;
202     }
203
204   /* Now see if we have a frame pointer.
205
206      Search for mov a2,a0 (0xf278)
207      then       mov a3,a2 (0xf27e).  */
208
209   if (buf[0] == 0xf2 && buf[1] == 0x78)
210     {
211       /* Our caller's $a2 will be found in $a0 now.  Note it for
212          our callers.  */
213       if (fi)
214         fi->status |= CALLER_A2_IN_A0;
215       addr += 2;
216       if (addr >= stop)
217         {
218           /* We still haven't allocated our local stack.  Handle this
219              as if we stopped on the first or last insn of a function.   */
220           if (fi && get_next_frame (fi) == NULL)
221             deprecated_update_frame_base_hack (fi, read_sp ());
222           return addr;
223         }
224
225       status = target_read_memory (addr, buf, 2);
226       if (status != 0)
227         {
228           if (fi && get_next_frame (fi) == NULL)
229             deprecated_update_frame_base_hack (fi, read_sp ());
230           return addr;
231         }
232       if (buf[0] == 0xf2 && buf[1] == 0x7e)
233         {
234           addr += 2;
235
236           /* Our frame pointer is valid now.  */
237           if (fi)
238             {
239               fi->status |= MY_FRAME_IN_FP;
240               fi->status &= ~MY_FRAME_IN_SP;
241             }
242           if (addr >= stop)
243             return addr;
244         }
245       else
246         {
247           if (fi && get_next_frame (fi) == NULL)
248             deprecated_update_frame_base_hack (fi, read_sp ());
249           return addr;
250         }
251     }
252
253   /* Next we should allocate the local frame.
254
255      Search for add imm8,a3 (0xd3XX)
256      or add imm16,a3 (0xf70bXXXX)
257      or add imm24,a3 (0xf467XXXXXX).
258
259      If none of the above was found, then this prologue has
260      no stack, and therefore can't have any register saves,
261      so quit now.  */
262   status = target_read_memory (addr, buf, 2);
263   if (status != 0)
264     {
265       if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP))
266         deprecated_update_frame_base_hack (fi, read_sp ());
267       return addr;
268     }
269   if (buf[0] == 0xd3)
270     {
271       stack_size = extract_signed_integer (&buf[1], 1);
272       if (fi)
273         fi->stack_size = stack_size;
274       addr += 2;
275       if (addr >= stop)
276         {
277           if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP))
278             deprecated_update_frame_base_hack (fi, read_sp () - stack_size);
279           return addr;
280         }
281     }
282   else if (buf[0] == 0xf7 && buf[1] == 0x0b)
283     {
284       status = target_read_memory (addr + 2, buf, 2);
285       if (status != 0)
286         {
287           if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP))
288             deprecated_update_frame_base_hack (fi, read_sp ());
289           return addr;
290         }
291       stack_size = extract_signed_integer (buf, 2);
292       if (fi)
293         fi->stack_size = stack_size;
294       addr += 4;
295       if (addr >= stop)
296         {
297           if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP))
298             deprecated_update_frame_base_hack (fi, read_sp () - stack_size);
299           return addr;
300         }
301     }
302   else if (buf[0] == 0xf4 && buf[1] == 0x67)
303     {
304       status = target_read_memory (addr + 2, buf, 3);
305       if (status != 0)
306         {
307           if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP))
308             deprecated_update_frame_base_hack (fi, read_sp ());
309           return addr;
310         }
311       stack_size = extract_signed_integer (buf, 3);
312       if (fi)
313         fi->stack_size = stack_size;
314       addr += 5;
315       if (addr >= stop)
316         {
317           if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP))
318             deprecated_update_frame_base_hack (fi, read_sp () - stack_size);
319           return addr;
320         }
321     }
322
323   /* Now see if we have a call to __prologue for an out of line
324      prologue.  */
325   status = target_read_memory (addr, buf, 2);
326   if (status != 0)
327     return addr;
328
329   /* First check for 16bit pc-relative call to __prologue.  */
330   if (buf[0] == 0xfd)
331     {
332       CORE_ADDR temp;
333       status = target_read_memory (addr + 1, buf, 2);
334       if (status != 0)
335         {
336           if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP))
337             deprecated_update_frame_base_hack (fi, read_sp ());
338           return addr;
339         }
340
341       /* Get the PC this instruction will branch to.  */
342       temp = (extract_signed_integer (buf, 2) + addr + 3) & 0xffffff;
343
344       /* Get the name of the function at the target address.  */
345       status = find_pc_partial_function (temp, &name, NULL, NULL);
346       if (status == 0)
347         {
348           if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP))
349             deprecated_update_frame_base_hack (fi, read_sp ());
350           return addr;
351         }
352
353       /* Note if it is an out of line prologue.  */
354       out_of_line_prologue = (strcmp (name, "__prologue") == 0);
355
356       /* This sucks up 3 bytes of instruction space.  */
357       if (out_of_line_prologue)
358         addr += 3;
359
360       if (addr >= stop)
361         {
362           if (fi && get_next_frame (fi) == NULL)
363             {
364               fi->stack_size -= 16;
365               deprecated_update_frame_base_hack (fi, read_sp () - fi->stack_size);
366             }
367           return addr;
368         }
369     }
370   /* Now check for the 24bit pc-relative call to __prologue.  */
371   else if (buf[0] == 0xf4 && buf[1] == 0xe1)
372     {
373       CORE_ADDR temp;
374       status = target_read_memory (addr + 2, buf, 3);
375       if (status != 0)
376         {
377           if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP))
378             deprecated_update_frame_base_hack (fi, read_sp ());
379           return addr;
380         }
381
382       /* Get the PC this instruction will branch to.  */
383       temp = (extract_signed_integer (buf, 3) + addr + 5) & 0xffffff;
384
385       /* Get the name of the function at the target address.  */
386       status = find_pc_partial_function (temp, &name, NULL, NULL);
387       if (status == 0)
388         {
389           if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP))
390             deprecated_update_frame_base_hack (fi, read_sp ());
391           return addr;
392         }
393
394       /* Note if it is an out of line prologue.  */
395       out_of_line_prologue = (strcmp (name, "__prologue") == 0);
396
397       /* This sucks up 5 bytes of instruction space.  */
398       if (out_of_line_prologue)
399         addr += 5;
400
401       if (addr >= stop)
402         {
403           if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP))
404             {
405               fi->stack_size -= 16;
406               deprecated_update_frame_base_hack (fi, read_sp () - fi->stack_size);
407             }
408           return addr;
409         }
410     }
411
412   /* Now actually handle the out of line prologue.  */
413   if (out_of_line_prologue)
414     {
415       int outgoing_args_size = 0;
416
417       /* First adjust the stack size for this function.  The out of
418          line prologue saves 4 registers (16bytes of data).  */
419       if (fi)
420         fi->stack_size -= 16;
421
422       /* Update fi->frame if necessary.  */
423       if (fi && get_next_frame (fi) == NULL)
424         deprecated_update_frame_base_hack (fi, read_sp () - fi->stack_size);
425
426       /* After the out of line prologue, there may be another
427          stack adjustment for the outgoing arguments.
428
429          Search for add imm8,a3 (0xd3XX)
430          or     add imm16,a3 (0xf70bXXXX)
431          or     add imm24,a3 (0xf467XXXXXX).  */
432
433       status = target_read_memory (addr, buf, 2);
434       if (status != 0)
435         {
436           if (fi)
437             {
438               fi->fsr.regs[2] = get_frame_base (fi) + fi->stack_size + 4;
439               fi->fsr.regs[3] = get_frame_base (fi) + fi->stack_size + 8;
440               fi->fsr.regs[5] = get_frame_base (fi) + fi->stack_size + 12;
441               fi->fsr.regs[6] = get_frame_base (fi) + fi->stack_size + 16;
442             }
443           return addr;
444         }
445
446       if (buf[0] == 0xd3)
447         {
448           outgoing_args_size = extract_signed_integer (&buf[1], 1);
449           addr += 2;
450         }
451       else if (buf[0] == 0xf7 && buf[1] == 0x0b)
452         {
453           status = target_read_memory (addr + 2, buf, 2);
454           if (status != 0)
455             {
456               if (fi)
457                 {
458                   fi->fsr.regs[2] = get_frame_base (fi) + fi->stack_size + 4;
459                   fi->fsr.regs[3] = get_frame_base (fi) + fi->stack_size + 8;
460                   fi->fsr.regs[5] = get_frame_base (fi) + fi->stack_size + 12;
461                   fi->fsr.regs[6] = get_frame_base (fi) + fi->stack_size + 16;
462                 }
463               return addr;
464             }
465           outgoing_args_size = extract_signed_integer (buf, 2);
466           addr += 4;
467         }
468       else if (buf[0] == 0xf4 && buf[1] == 0x67)
469         {
470           status = target_read_memory (addr + 2, buf, 3);
471           if (status != 0)
472             {
473               if (fi && get_next_frame (fi) == NULL)
474                 {
475                   fi->fsr.regs[2] = get_frame_base (fi) + fi->stack_size + 4;
476                   fi->fsr.regs[3] = get_frame_base (fi) + fi->stack_size + 8;
477                   fi->fsr.regs[5] = get_frame_base (fi) + fi->stack_size + 12;
478                   fi->fsr.regs[6] = get_frame_base (fi) + fi->stack_size + 16;
479                 }
480               return addr;
481             }
482           outgoing_args_size = extract_signed_integer (buf, 3);
483           addr += 5;
484         }
485       else
486         outgoing_args_size = 0;
487
488       /* Now that we know the size of the outgoing arguments, fix
489          fi->frame again if this is the innermost frame.  */
490       if (fi && get_next_frame (fi) == NULL)
491         deprecated_update_frame_base_hack (fi, get_frame_base (fi) - outgoing_args_size);
492
493       /* Note the register save information and update the stack
494          size for this frame too.  */
495       if (fi)
496         {
497           fi->fsr.regs[2] = get_frame_base (fi) + fi->stack_size + 4;
498           fi->fsr.regs[3] = get_frame_base (fi) + fi->stack_size + 8;
499           fi->fsr.regs[5] = get_frame_base (fi) + fi->stack_size + 12;
500           fi->fsr.regs[6] = get_frame_base (fi) + fi->stack_size + 16;
501           fi->stack_size += outgoing_args_size;
502         }
503       /* There can be no more prologue insns, so return now.  */
504       return addr;
505     }
506
507   /* At this point fi->frame needs to be correct.
508
509      If MY_FRAME_IN_SP is set and we're the innermost frame, then we
510      need to fix fi->frame so that backtracing, find_frame_saved_regs,
511      etc work correctly.  */
512   if (fi && get_next_frame (fi) == NULL && (fi->status & MY_FRAME_IN_SP) != 0)
513     deprecated_update_frame_base_hack (fi, read_sp () - fi->stack_size);
514
515   /* And last we have the register saves.  These are relatively
516      simple because they're physically done off the stack pointer,
517      and thus the number of different instructions we need to
518      check is greatly reduced because we know the displacements
519      will be small.
520
521      Search for movx d2,(X,a3) (0xf55eXX)
522      then       movx d3,(X,a3) (0xf55fXX)
523      then       mov  a1,(X,a3) (0x5dXX)    No frame pointer case
524      then       mov  a2,(X,a3) (0x5eXX)    No frame pointer case
525      or  mov  a0,(X,a3) (0x5cXX)           Frame pointer case.  */
526
527   status = target_read_memory (addr, buf, 2);
528   if (status != 0)
529     return addr;
530   if (buf[0] == 0xf5 && buf[1] == 0x5e)
531     {
532       if (fi)
533         {
534           status = target_read_memory (addr + 2, buf, 1);
535           if (status != 0)
536             return addr;
537           fi->fsr.regs[2] = (get_frame_base (fi) + stack_size
538                              + extract_signed_integer (buf, 1));
539         }
540       addr += 3;
541       if (addr >= stop)
542         return addr;
543       status = target_read_memory (addr, buf, 2);
544       if (status != 0)
545         return addr;
546     }
547   if (buf[0] == 0xf5 && buf[1] == 0x5f)
548     {
549       if (fi)
550         {
551           status = target_read_memory (addr + 2, buf, 1);
552           if (status != 0)
553             return addr;
554           fi->fsr.regs[3] = (get_frame_base (fi) + stack_size
555                              + extract_signed_integer (buf, 1));
556         }
557       addr += 3;
558       if (addr >= stop)
559         return addr;
560       status = target_read_memory (addr, buf, 2);
561       if (status != 0)
562         return addr;
563     }
564   if (buf[0] == 0x5d)
565     {
566       if (fi)
567         {
568           status = target_read_memory (addr + 1, buf, 1);
569           if (status != 0)
570             return addr;
571           fi->fsr.regs[5] = (get_frame_base (fi) + stack_size
572                              + extract_signed_integer (buf, 1));
573         }
574       addr += 2;
575       if (addr >= stop)
576         return addr;
577       status = target_read_memory (addr, buf, 2);
578       if (status != 0)
579         return addr;
580     }
581   if (buf[0] == 0x5e || buf[0] == 0x5c)
582     {
583       if (fi)
584         {
585           status = target_read_memory (addr + 1, buf, 1);
586           if (status != 0)
587             return addr;
588           fi->fsr.regs[6] = (get_frame_base (fi) + stack_size
589                              + extract_signed_integer (buf, 1));
590           fi->status &= ~CALLER_A2_IN_A0;
591         }
592       addr += 2;
593       if (addr >= stop)
594         return addr;
595       return addr;
596     }
597   return addr;
598 }
599
600 /* Function: frame_chain
601    Figure out and return the caller's frame pointer given current
602    frame_info struct.
603
604    We don't handle dummy frames yet but we would probably just return the
605    stack pointer that was in use at the time the function call was made?  */
606
607 CORE_ADDR
608 mn10200_frame_chain (struct frame_info *fi)
609 {
610   struct frame_info *dummy_frame = deprecated_frame_xmalloc ();
611   struct cleanup *old_chain = make_cleanup (xfree, dummy_frame);
612   CORE_ADDR ret;
613
614   /* Walk through the prologue to determine the stack size,
615      location of saved registers, end of the prologue, etc.  */
616   if (fi->status == 0)
617     mn10200_analyze_prologue (fi, (CORE_ADDR) 0);
618
619   /* Quit now if mn10200_analyze_prologue set NO_MORE_FRAMES.  */
620   if (fi->status & NO_MORE_FRAMES)
621     return 0;
622
623   /* Now that we've analyzed our prologue, determine the frame
624      pointer for our caller.
625
626      If our caller has a frame pointer, then we need to
627      find the entry value of $a2 to our function.
628
629      If CALLER_A2_IN_A0, then the chain is in $a0.
630
631      If fsr.regs[6] is nonzero, then it's at the memory
632      location pointed to by fsr.regs[6].
633
634      Else it's still in $a2.
635
636      If our caller does not have a frame pointer, then his
637      frame base is fi->frame + -caller's stack size + 4.  */
638
639   /* The easiest way to get that info is to analyze our caller's frame.
640
641      So we set up a dummy frame and call mn10200_analyze_prologue to
642      find stuff for us.  */
643   deprecated_update_frame_pc_hack (dummy_frame, FRAME_SAVED_PC (fi));
644   deprecated_update_frame_base_hack (dummy_frame, get_frame_base (fi));
645   memset (dummy_frame->fsr.regs, '\000', sizeof dummy_frame->fsr.regs);
646   dummy_frame->status = 0;
647   dummy_frame->stack_size = 0;
648   mn10200_analyze_prologue (dummy_frame, 0);
649
650   if (dummy_frame->status & MY_FRAME_IN_FP)
651     {
652       /* Our caller has a frame pointer.  So find the frame in $a2, $a0,
653          or in the stack.  */
654       if (fi->fsr.regs[6])
655         ret = (read_memory_integer (fi->fsr.regs[FP_REGNUM], REGISTER_SIZE)
656                & 0xffffff);
657       else if (fi->status & CALLER_A2_IN_A0)
658         ret = read_register (4);
659       else
660         ret = read_register (FP_REGNUM);
661     }
662   else
663     {
664       /* Our caller does not have a frame pointer.  So his frame starts
665          at the base of our frame (fi->frame) + <his size> + 4 (saved pc).  */
666       ret = get_frame_base (fi) + -dummy_frame->stack_size + 4;
667     }
668   do_cleanups (old_chain);
669   return ret;
670 }
671
672 /* Function: skip_prologue
673    Return the address of the first inst past the prologue of the function.  */
674
675 CORE_ADDR
676 mn10200_skip_prologue (CORE_ADDR pc)
677 {
678   /* We used to check the debug symbols, but that can lose if
679      we have a null prologue.  */
680   return mn10200_analyze_prologue (NULL, pc);
681 }
682
683 /* Function: pop_frame
684    This routine gets called when either the user uses the `return'
685    command, or the call dummy breakpoint gets hit.  */
686
687 void
688 mn10200_pop_frame (struct frame_info *frame)
689 {
690   int regnum;
691
692   if (DEPRECATED_PC_IN_CALL_DUMMY (get_frame_pc (frame),
693                                    get_frame_base (frame),
694                                    get_frame_base (frame)))
695     generic_pop_dummy_frame ();
696   else
697     {
698       write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
699
700       /* Restore any saved registers.  */
701       for (regnum = 0; regnum < NUM_REGS; regnum++)
702         if (frame->fsr.regs[regnum] != 0)
703           {
704             ULONGEST value;
705
706             value = read_memory_unsigned_integer (frame->fsr.regs[regnum],
707                                                 REGISTER_RAW_SIZE (regnum));
708             write_register (regnum, value);
709           }
710
711       /* Actually cut back the stack.  */
712       write_register (SP_REGNUM, get_frame_base (frame));
713
714       /* Don't we need to set the PC?!?  XXX FIXME.  */
715     }
716
717   /* Throw away any cached frame information.  */
718   flush_cached_frames ();
719 }
720
721 /* Function: push_arguments
722    Setup arguments for a call to the target.  Arguments go in
723    order on the stack.  */
724
725 CORE_ADDR
726 mn10200_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
727                         unsigned char struct_return, CORE_ADDR struct_addr)
728 {
729   int argnum = 0;
730   int len = 0;
731   int stack_offset = 0;
732   int regsused = struct_return ? 1 : 0;
733
734   /* This should be a nop, but align the stack just in case something
735      went wrong.  Stacks are two byte aligned on the mn10200.  */
736   sp &= ~1;
737
738   /* Now make space on the stack for the args.
739
740      XXX This doesn't appear to handle pass-by-invisible reference
741      arguments.  */
742   for (argnum = 0; argnum < nargs; argnum++)
743     {
744       int arg_length = (TYPE_LENGTH (VALUE_TYPE (args[argnum])) + 1) & ~1;
745
746       /* If we've used all argument registers, then this argument is
747          pushed.  */
748       if (regsused >= 2 || arg_length > 4)
749         {
750           regsused = 2;
751           len += arg_length;
752         }
753       /* We know we've got some arg register space left.  If this argument
754          will fit entirely in regs, then put it there.  */
755       else if (arg_length <= 2
756                || TYPE_CODE (VALUE_TYPE (args[argnum])) == TYPE_CODE_PTR)
757         {
758           regsused++;
759         }
760       else if (regsused == 0)
761         {
762           regsused = 2;
763         }
764       else
765         {
766           regsused = 2;
767           len += arg_length;
768         }
769     }
770
771   /* Allocate stack space.  */
772   sp -= len;
773
774   regsused = struct_return ? 1 : 0;
775   /* Push all arguments onto the stack. */
776   for (argnum = 0; argnum < nargs; argnum++)
777     {
778       int len;
779       char *val;
780
781       /* XXX Check this.  What about UNIONS?  */
782       if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
783           && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
784         {
785           /* XXX Wrong, we want a pointer to this argument.  */
786           len = TYPE_LENGTH (VALUE_TYPE (*args));
787           val = (char *) VALUE_CONTENTS (*args);
788         }
789       else
790         {
791           len = TYPE_LENGTH (VALUE_TYPE (*args));
792           val = (char *) VALUE_CONTENTS (*args);
793         }
794
795       if (regsused < 2
796           && (len <= 2
797               || TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_PTR))
798         {
799           write_register (regsused, extract_unsigned_integer (val, 4));
800           regsused++;
801         }
802       else if (regsused == 0 && len == 4)
803         {
804           write_register (regsused, extract_unsigned_integer (val, 2));
805           write_register (regsused + 1, extract_unsigned_integer (val + 2, 2));
806           regsused = 2;
807         }
808       else
809         {
810           regsused = 2;
811           while (len > 0)
812             {
813               write_memory (sp + stack_offset, val, 2);
814
815               len -= 2;
816               val += 2;
817               stack_offset += 2;
818             }
819         }
820       args++;
821     }
822
823   return sp;
824 }
825
826 /* Function: push_return_address (pc)
827    Set up the return address for the inferior function call.
828    Needed for targets where we don't actually execute a JSR/BSR instruction */
829
830 CORE_ADDR
831 mn10200_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
832 {
833   unsigned char buf[4];
834
835   store_unsigned_integer (buf, 4, CALL_DUMMY_ADDRESS ());
836   write_memory (sp - 4, buf, 4);
837   return sp - 4;
838 }
839
840 /* Function: store_struct_return (addr,sp)
841    Store the structure value return address for an inferior function
842    call.  */
843
844 CORE_ADDR
845 mn10200_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
846 {
847   /* The structure return address is passed as the first argument.  */
848   write_register (0, addr);
849   return sp;
850 }
851
852 /* Function: frame_saved_pc 
853    Find the caller of this frame.  We do this by seeing if RP_REGNUM
854    is saved in the stack anywhere, otherwise we get it from the
855    registers.  If the inner frame is a dummy frame, return its PC
856    instead of RP, because that's where "caller" of the dummy-frame
857    will be found.  */
858
859 CORE_ADDR
860 mn10200_frame_saved_pc (struct frame_info *fi)
861 {
862   /* The saved PC will always be at the base of the current frame.  */
863   return (read_memory_integer (get_frame_base (fi), REGISTER_SIZE) & 0xffffff);
864 }
865
866 /* Function: init_extra_frame_info
867    Setup the frame's frame pointer, pc, and frame addresses for saved
868    registers.  Most of the work is done in mn10200_analyze_prologue().
869
870    Note that when we are called for the last frame (currently active frame),
871    that get_frame_pc (fi) and fi->frame will already be setup.  However, fi->frame will
872    be valid only if this routine uses FP.  For previous frames, fi-frame will
873    always be correct.  mn10200_analyze_prologue will fix fi->frame if
874    it's not valid.
875
876    We can be called with the PC in the call dummy under two circumstances.
877    First, during normal backtracing, second, while figuring out the frame
878    pointer just prior to calling the target function (see run_stack_dummy).  */
879
880 void
881 mn10200_init_extra_frame_info (struct frame_info *fi)
882 {
883   if (get_next_frame (fi))
884     deprecated_update_frame_pc_hack (fi, FRAME_SAVED_PC (get_next_frame (fi)));
885
886   memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
887   fi->status = 0;
888   fi->stack_size = 0;
889
890   mn10200_analyze_prologue (fi, 0);
891 }
892
893 void
894 _initialize_mn10200_tdep (void)
895 {
896   tm_print_insn = print_insn_mn10200;
897 }