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