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