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