OSDN Git Service

Initial revision
[pf3gnuchains/pf3gnuchains3x.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, Boston, MA 02111-1307, USA.  */
19
20 #include "defs.h"
21 #include "frame.h"
22 #include "inferior.h"
23 #include "obstack.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
31         
32 /* Should call_function allocate stack space for a struct return?  */
33 int
34 mn10200_use_struct_convention (gcc_p, type)
35      int gcc_p;
36      struct type *type;
37 {
38   return (TYPE_NFIELDS (type) > 1 || TYPE_LENGTH (type) > 8);
39 }
40
41
42
43 /* The main purpose of this file is dealing with prologues to extract
44    information about stack frames and saved registers.
45
46    For reference here's how prologues look on the mn10200:
47
48      With frame pointer:
49         mov fp,a0
50         mov sp,fp
51         add <size>,sp
52         Register saves for d2, d3, a1, a2 as needed.  Saves start
53         at fp - <size> + <outgoing_args_size> and work towards higher
54         addresses.  Note that the saves are actually done off the stack
55         pointer in the prologue!  This makes for smaller code and easier
56         prologue scanning as the displacement fields will unlikely
57         be more than 8 bits!
58
59      Without frame pointer:
60         add <size>,sp
61         Register saves for d2, d3, a1, a2 as needed.  Saves start
62         at sp + <outgoing_args_size> and work towards higher addresses.
63
64      Out of line prologue:
65         add <local size>,sp  -- optional
66         jsr __prologue
67         add <outgoing_size>,sp -- optional
68
69    The stack pointer remains constant throughout the life of most
70    functions.  As a result the compiler will usually omit the
71    frame pointer, so we must handle frame pointerless functions.  */
72
73 /* Analyze the prologue to determine where registers are saved,
74    the end of the prologue, etc etc.  Return the end of the prologue
75    scanned.
76
77    We store into FI (if non-null) several tidbits of information:
78
79     * stack_size -- size of this stack frame.  Note that if we stop in
80     certain parts of the prologue/epilogue we may claim the size of the
81     current frame is zero.  This happens when the current frame has
82     not been allocated yet or has already been deallocated.
83
84     * fsr -- Addresses of registers saved in the stack by this frame.
85
86     * status -- A (relatively) generic status indicator.  It's a bitmask
87     with the following bits: 
88
89       MY_FRAME_IN_SP: The base of the current frame is actually in
90       the stack pointer.  This can happen for frame pointerless
91       functions, or cases where we're stopped in the prologue/epilogue
92       itself.  For these cases mn10200_analyze_prologue will need up
93       update fi->frame before returning or analyzing the register
94       save instructions.
95
96       MY_FRAME_IN_FP: The base of the current frame is in the
97       frame pointer register ($a2).
98
99       CALLER_A2_IN_A0: $a2 from the caller's frame is temporarily
100       in $a0.  This can happen if we're stopped in the prologue.
101
102       NO_MORE_FRAMES: Set this if the current frame is "start" or
103       if the first instruction looks like mov <imm>,sp.  This tells
104       frame chain to not bother trying to unwind past this frame.  */
105
106 #define MY_FRAME_IN_SP 0x1
107 #define MY_FRAME_IN_FP 0x2
108 #define CALLER_A2_IN_A0 0x4
109 #define NO_MORE_FRAMES 0x8
110  
111 static CORE_ADDR
112 mn10200_analyze_prologue (fi, pc)
113     struct frame_info *fi;
114     CORE_ADDR pc;
115 {
116   CORE_ADDR func_addr, func_end, addr, stop;
117   CORE_ADDR stack_size;
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 (fi)
610      struct frame_info *fi;
611 {
612   struct frame_info dummy_frame;
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   dummy_frame.pc = FRAME_SAVED_PC (fi);
644   dummy_frame.frame = fi->frame;
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);
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         return (read_memory_integer (fi->fsr.regs[FP_REGNUM], REGISTER_SIZE)
656                 & 0xffffff);
657       else if (fi->status & CALLER_A2_IN_A0)
658         return read_register (4);
659       else
660         return 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       return fi->frame + -dummy_frame.stack_size + 4;
667     }
668 }
669
670 /* Function: skip_prologue
671    Return the address of the first inst past the prologue of the function.  */
672
673 CORE_ADDR
674 mn10200_skip_prologue (pc)
675      CORE_ADDR pc;
676 {
677   /* We used to check the debug symbols, but that can lose if
678      we have a null prologue.  */
679   return mn10200_analyze_prologue (NULL, pc);
680 }
681
682 /* Function: pop_frame
683    This routine gets called when either the user uses the `return'
684    command, or the call dummy breakpoint gets hit.  */
685
686 void
687 mn10200_pop_frame (frame)
688      struct frame_info *frame;
689 {
690   int regnum;
691
692   if (PC_IN_CALL_DUMMY(frame->pc, frame->frame, frame->frame))
693     generic_pop_dummy_frame ();
694   else
695     {
696       write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
697
698       /* Restore any saved registers.  */
699       for (regnum = 0; regnum < NUM_REGS; regnum++)
700         if (frame->fsr.regs[regnum] != 0)
701           {
702             ULONGEST value;
703
704             value = read_memory_unsigned_integer (frame->fsr.regs[regnum],
705                                                   REGISTER_RAW_SIZE (regnum));
706             write_register (regnum, value);
707           }
708
709       /* Actually cut back the stack.  */
710       write_register (SP_REGNUM, FRAME_FP (frame));
711
712       /* Don't we need to set the PC?!?  XXX FIXME.  */
713     }
714
715   /* Throw away any cached frame information.  */
716   flush_cached_frames ();
717 }
718
719 /* Function: push_arguments
720    Setup arguments for a call to the target.  Arguments go in
721    order on the stack.  */
722
723 CORE_ADDR
724 mn10200_push_arguments (nargs, args, sp, struct_return, struct_addr)
725      int nargs;
726      value_ptr *args;
727      CORE_ADDR sp;
728      unsigned char struct_return;
729      CORE_ADDR struct_addr;
730 {
731   int argnum = 0;
732   int len = 0;
733   int stack_offset = 0;
734   int regsused = struct_return ? 1 : 0;
735
736   /* This should be a nop, but align the stack just in case something
737      went wrong.  Stacks are two byte aligned on the mn10200.  */
738   sp &= ~1;
739
740   /* Now make space on the stack for the args.
741
742      XXX This doesn't appear to handle pass-by-invisible reference
743      arguments.  */
744   for (argnum = 0; argnum < nargs; argnum++)
745     {
746       int arg_length = (TYPE_LENGTH (VALUE_TYPE (args[argnum])) + 1) & ~1;
747
748       /* If we've used all argument registers, then this argument is
749          pushed.  */
750       if (regsused >= 2 || arg_length > 4)
751         {
752           regsused = 2;
753           len += arg_length;
754         }
755       /* We know we've got some arg register space left.  If this argument
756          will fit entirely in regs, then put it there.  */
757       else if (arg_length <= 2
758                || TYPE_CODE (VALUE_TYPE (args[argnum])) == TYPE_CODE_PTR) 
759         {
760           regsused++;
761         }
762       else if (regsused == 0)
763         {
764           regsused = 2;
765         }
766       else
767         {
768           regsused = 2;
769           len += arg_length;
770         }
771     }
772
773   /* Allocate stack space.  */
774   sp -= len;
775
776   regsused = struct_return ? 1 : 0;
777   /* Push all arguments onto the stack. */
778   for (argnum = 0; argnum < nargs; argnum++)
779     {
780       int len;
781       char *val;
782
783       /* XXX Check this.  What about UNIONS?  */
784       if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
785           && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
786         {
787           /* XXX Wrong, we want a pointer to this argument.  */
788           len = TYPE_LENGTH (VALUE_TYPE (*args));
789           val = (char *)VALUE_CONTENTS (*args);
790         }
791       else
792         {
793           len = TYPE_LENGTH (VALUE_TYPE (*args));
794           val = (char *)VALUE_CONTENTS (*args);
795         }
796
797       if (regsused < 2
798           && (len <= 2
799               || TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_PTR))
800         {
801           write_register (regsused, extract_unsigned_integer (val, 4));
802           regsused++;
803         }
804       else if (regsused == 0 && len == 4)
805         {
806           write_register (regsused, extract_unsigned_integer (val, 2));
807           write_register (regsused + 1, extract_unsigned_integer (val + 2, 2));
808           regsused = 2;
809         }
810       else
811         {
812           regsused = 2;
813           while (len > 0)
814             {
815               write_memory (sp + stack_offset, val, 2);
816
817               len -= 2;
818               val += 2;
819               stack_offset += 2;
820             }
821         }
822       args++;
823     }
824
825   return sp;
826 }
827
828 /* Function: push_return_address (pc)
829    Set up the return address for the inferior function call.
830    Needed for targets where we don't actually execute a JSR/BSR instruction */
831  
832 CORE_ADDR
833 mn10200_push_return_address (pc, sp)
834      CORE_ADDR pc;
835      CORE_ADDR sp;
836 {
837   unsigned char buf[4];
838
839   store_unsigned_integer (buf, 4, CALL_DUMMY_ADDRESS ());
840   write_memory (sp - 4, buf, 4);
841   return sp - 4;
842 }
843
844 /* Function: store_struct_return (addr,sp)
845    Store the structure value return address for an inferior function
846    call.  */
847  
848 CORE_ADDR
849 mn10200_store_struct_return (addr, sp)
850      CORE_ADDR addr;
851      CORE_ADDR sp;
852 {
853   /* The structure return address is passed as the first argument.  */
854   write_register (0, addr);
855   return sp;
856 }
857  
858 /* Function: frame_saved_pc 
859    Find the caller of this frame.  We do this by seeing if RP_REGNUM
860    is saved in the stack anywhere, otherwise we get it from the
861    registers.  If the inner frame is a dummy frame, return its PC
862    instead of RP, because that's where "caller" of the dummy-frame
863    will be found.  */
864
865 CORE_ADDR
866 mn10200_frame_saved_pc (fi)
867      struct frame_info *fi;
868 {
869   /* The saved PC will always be at the base of the current frame.  */
870   return (read_memory_integer (fi->frame, REGISTER_SIZE) & 0xffffff);
871 }
872
873 void
874 get_saved_register (raw_buffer, optimized, addrp, frame, regnum, lval)
875      char *raw_buffer;
876      int *optimized;
877      CORE_ADDR *addrp;
878      struct frame_info *frame;
879      int regnum;
880      enum lval_type *lval;
881 {
882   generic_get_saved_register (raw_buffer, optimized, addrp, 
883                               frame, regnum, lval);
884 }
885
886 /* Function: init_extra_frame_info
887    Setup the frame's frame pointer, pc, and frame addresses for saved
888    registers.  Most of the work is done in mn10200_analyze_prologue().
889
890    Note that when we are called for the last frame (currently active frame),
891    that fi->pc and fi->frame will already be setup.  However, fi->frame will
892    be valid only if this routine uses FP.  For previous frames, fi-frame will
893    always be correct.  mn10200_analyze_prologue will fix fi->frame if
894    it's not valid.
895
896    We can be called with the PC in the call dummy under two circumstances.
897    First, during normal backtracing, second, while figuring out the frame
898    pointer just prior to calling the target function (see run_stack_dummy).  */
899
900 void
901 mn10200_init_extra_frame_info (fi)
902      struct frame_info *fi;
903 {
904   if (fi->next)
905     fi->pc = FRAME_SAVED_PC (fi->next);
906
907   memset (fi->fsr.regs, '\000', sizeof fi->fsr.regs);
908   fi->status = 0;
909   fi->stack_size = 0;
910
911   mn10200_analyze_prologue (fi, 0);
912 }
913
914 void
915 _initialize_mn10200_tdep ()
916 {
917   tm_print_insn = print_insn_mn10200;
918 }
919