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