* gdbarch.sh (BREAKPOINT_FROM_PC): Return a const buffer.
[platform/upstream/binutils.git] / gdb / mn10300-tdep.c
1 /* Target-dependent code for the Matsushita MN10300 for GDB, the GNU debugger.
2
3    Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 Free Software
4    Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22
23 #include "defs.h"
24 #include "frame.h"
25 #include "inferior.h"
26 #include "obstack.h"
27 #include "target.h"
28 #include "value.h"
29 #include "bfd.h"
30 #include "gdb_string.h"
31 #include "gdbcore.h"
32 #include "symfile.h"
33 #include "regcache.h"
34 #include "arch-utils.h"
35
36 #define D0_REGNUM 0
37 #define D2_REGNUM 2
38 #define D3_REGNUM 3
39 #define A0_REGNUM 4
40 #define A2_REGNUM 6
41 #define A3_REGNUM 7
42 #define MDR_REGNUM 10
43 #define PSW_REGNUM 11
44 #define LIR_REGNUM 12
45 #define LAR_REGNUM 13
46 #define MDRQ_REGNUM 14
47 #define E0_REGNUM 15
48 #define MCRH_REGNUM 26
49 #define MCRL_REGNUM 27
50 #define MCVF_REGNUM 28
51
52 enum movm_register_bits {
53   movm_exother_bit = 0x01,
54   movm_exreg1_bit  = 0x02,
55   movm_exreg0_bit  = 0x04,
56   movm_other_bit   = 0x08,
57   movm_a3_bit      = 0x10,
58   movm_a2_bit      = 0x20,
59   movm_d3_bit      = 0x40,
60   movm_d2_bit      = 0x80
61 };
62
63 extern void _initialize_mn10300_tdep (void);
64 static CORE_ADDR mn10300_analyze_prologue (struct frame_info *fi,
65                                            CORE_ADDR pc);
66
67 /* mn10300 private data */
68 struct gdbarch_tdep
69 {
70   int am33_mode;
71 #define AM33_MODE (gdbarch_tdep (current_gdbarch)->am33_mode)
72 };
73
74 /* Additional info used by the frame */
75
76 struct frame_extra_info
77   {
78     int status;
79     int stack_size;
80   };
81
82
83 static char *
84 register_name (int reg, char **regs, long sizeof_regs)
85 {
86   if (reg < 0 || reg >= sizeof_regs / sizeof (regs[0]))
87     return NULL;
88   else
89     return regs[reg];
90 }
91
92 static char *
93 mn10300_generic_register_name (int reg)
94 {
95   static char *regs[] =
96   { "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",
97     "sp", "pc", "mdr", "psw", "lir", "lar", "", "",
98     "", "", "", "", "", "", "", "",
99     "", "", "", "", "", "", "", "fp"
100   };
101   return register_name (reg, regs, sizeof regs);
102 }
103
104
105 static char *
106 am33_register_name (int reg)
107 {
108   static char *regs[] =
109   { "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",
110     "sp", "pc", "mdr", "psw", "lir", "lar", "",
111     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
112     "ssp", "msp", "usp", "mcrh", "mcrl", "mcvf", "", "", ""
113   };
114   return register_name (reg, regs, sizeof regs);
115 }
116   
117 static CORE_ADDR
118 mn10300_saved_pc_after_call (struct frame_info *fi)
119 {
120   return read_memory_integer (read_register (SP_REGNUM), 4);
121 }
122
123 static void
124 mn10300_extract_return_value (struct type *type, char *regbuf, char *valbuf)
125 {
126   if (TYPE_CODE (type) == TYPE_CODE_PTR)
127     memcpy (valbuf, regbuf + REGISTER_BYTE (4), TYPE_LENGTH (type));
128   else
129     memcpy (valbuf, regbuf + REGISTER_BYTE (0), TYPE_LENGTH (type));
130 }
131
132 static CORE_ADDR
133 mn10300_extract_struct_value_address (char *regbuf)
134 {
135   return extract_address (regbuf + REGISTER_BYTE (4),
136                           REGISTER_RAW_SIZE (4));
137 }
138
139 static void
140 mn10300_store_return_value (struct type *type, char *valbuf)
141 {
142   if (TYPE_CODE (type) == TYPE_CODE_PTR)
143     write_register_bytes (REGISTER_BYTE (4), valbuf, TYPE_LENGTH (type));
144   else
145     write_register_bytes (REGISTER_BYTE (0), valbuf, TYPE_LENGTH (type));
146 }
147
148 static struct frame_info *analyze_dummy_frame (CORE_ADDR, CORE_ADDR);
149 static struct frame_info *
150 analyze_dummy_frame (CORE_ADDR pc, CORE_ADDR frame)
151 {
152   static struct frame_info *dummy = NULL;
153   if (dummy == NULL)
154     {
155       dummy = xmalloc (sizeof (struct frame_info));
156       dummy->saved_regs = xmalloc (SIZEOF_FRAME_SAVED_REGS);
157       dummy->extra_info = xmalloc (sizeof (struct frame_extra_info));
158     }
159   dummy->next = NULL;
160   dummy->prev = NULL;
161   dummy->pc = pc;
162   dummy->frame = frame;
163   dummy->extra_info->status = 0;
164   dummy->extra_info->stack_size = 0;
165   memset (dummy->saved_regs, '\000', SIZEOF_FRAME_SAVED_REGS);
166   mn10300_analyze_prologue (dummy, 0);
167   return dummy;
168 }
169
170 /* Values for frame_info.status */
171
172 #define MY_FRAME_IN_SP 0x1
173 #define MY_FRAME_IN_FP 0x2
174 #define NO_MORE_FRAMES 0x4
175
176
177 /* Should call_function allocate stack space for a struct return?  */
178 static int
179 mn10300_use_struct_convention (int gcc_p, struct type *type)
180 {
181   return (TYPE_NFIELDS (type) > 1 || TYPE_LENGTH (type) > 8);
182 }
183
184 /* The breakpoint instruction must be the same size as the smallest
185    instruction in the instruction set.
186
187    The Matsushita mn10x00 processors have single byte instructions
188    so we need a single byte breakpoint.  Matsushita hasn't defined
189    one, so we defined it ourselves.  */
190
191 const static unsigned char *
192 mn10300_breakpoint_from_pc (CORE_ADDR *bp_addr, int *bp_size)
193 {
194   static char breakpoint[] =
195   {0xff};
196   *bp_size = 1;
197   return breakpoint;
198 }
199
200
201 /* Fix fi->frame if it's bogus at this point.  This is a helper
202    function for mn10300_analyze_prologue. */
203
204 static void
205 fix_frame_pointer (struct frame_info *fi, int stack_size)
206 {
207   if (fi && fi->next == NULL)
208     {
209       if (fi->extra_info->status & MY_FRAME_IN_SP)
210         fi->frame = read_sp () - stack_size;
211       else if (fi->extra_info->status & MY_FRAME_IN_FP)
212         fi->frame = read_register (A3_REGNUM);
213     }
214 }
215
216
217 /* Set offsets of registers saved by movm instruction.
218    This is a helper function for mn10300_analyze_prologue.  */
219
220 static void
221 set_movm_offsets (struct frame_info *fi, int movm_args)
222 {
223   int offset = 0;
224
225   if (fi == NULL || movm_args == 0)
226     return;
227
228   if (movm_args & movm_other_bit)
229     {
230       /* The `other' bit leaves a blank area of four bytes at the
231          beginning of its block of saved registers, making it 32 bytes
232          long in total.  */
233       fi->saved_regs[LAR_REGNUM]    = fi->frame + offset + 4;
234       fi->saved_regs[LIR_REGNUM]    = fi->frame + offset + 8;
235       fi->saved_regs[MDR_REGNUM]    = fi->frame + offset + 12;
236       fi->saved_regs[A0_REGNUM + 1] = fi->frame + offset + 16;
237       fi->saved_regs[A0_REGNUM]     = fi->frame + offset + 20;
238       fi->saved_regs[D0_REGNUM + 1] = fi->frame + offset + 24;
239       fi->saved_regs[D0_REGNUM]     = fi->frame + offset + 28;
240       offset += 32;
241     }
242   if (movm_args & movm_a3_bit)
243     {
244       fi->saved_regs[A3_REGNUM] = fi->frame + offset;
245       offset += 4;
246     }
247   if (movm_args & movm_a2_bit)
248     {
249       fi->saved_regs[A2_REGNUM] = fi->frame + offset;
250       offset += 4;
251     }
252   if (movm_args & movm_d3_bit)
253     {
254       fi->saved_regs[D3_REGNUM] = fi->frame + offset;
255       offset += 4;
256     }
257   if (movm_args & movm_d2_bit)
258     {
259       fi->saved_regs[D2_REGNUM] = fi->frame + offset;
260       offset += 4;
261     }
262   if (AM33_MODE)
263     {
264       if (movm_args & movm_exother_bit)
265         {
266           fi->saved_regs[MCVF_REGNUM]   = fi->frame + offset;
267           fi->saved_regs[MCRL_REGNUM]   = fi->frame + offset + 4;
268           fi->saved_regs[MCRH_REGNUM]   = fi->frame + offset + 8;
269           fi->saved_regs[MDRQ_REGNUM]   = fi->frame + offset + 12;
270           fi->saved_regs[E0_REGNUM + 1] = fi->frame + offset + 16;
271           fi->saved_regs[E0_REGNUM + 0] = fi->frame + offset + 20;
272           offset += 24;
273         }
274       if (movm_args & movm_exreg1_bit)
275         {
276           fi->saved_regs[E0_REGNUM + 7] = fi->frame + offset;
277           fi->saved_regs[E0_REGNUM + 6] = fi->frame + offset + 4;
278           fi->saved_regs[E0_REGNUM + 5] = fi->frame + offset + 8;
279           fi->saved_regs[E0_REGNUM + 4] = fi->frame + offset + 12;
280           offset += 16;
281         }
282       if (movm_args & movm_exreg0_bit)
283         {
284           fi->saved_regs[E0_REGNUM + 3] = fi->frame + offset;
285           fi->saved_regs[E0_REGNUM + 2] = fi->frame + offset + 4;
286           offset += 8;
287         }
288     }
289 }
290
291
292 /* The main purpose of this file is dealing with prologues to extract
293    information about stack frames and saved registers.
294
295    In gcc/config/mn13000/mn10300.c, the expand_prologue prologue
296    function is pretty readable, and has a nice explanation of how the
297    prologue is generated.  The prologues generated by that code will
298    have the following form (NOTE: the current code doesn't handle all
299    this!):
300
301    + If this is an old-style varargs function, then its arguments
302      need to be flushed back to the stack:
303      
304         mov d0,(4,sp)
305         mov d1,(4,sp)
306
307    + If we use any of the callee-saved registers, save them now.
308      
309         movm [some callee-saved registers],(sp)
310
311    + If we have any floating-point registers to save:
312
313      - Decrement the stack pointer to reserve space for the registers.
314        If the function doesn't need a frame pointer, we may combine
315        this with the adjustment that reserves space for the frame.
316
317         add -SIZE, sp
318
319      - Save the floating-point registers.  We have two possible
320        strategies:
321
322        . Save them at fixed offset from the SP:
323
324         fmov fsN,(OFFSETN,sp)
325         fmov fsM,(OFFSETM,sp)
326         ...
327
328        Note that, if OFFSETN happens to be zero, you'll get the
329        different opcode: fmov fsN,(sp)
330
331        . Or, set a0 to the start of the save area, and then use
332        post-increment addressing to save the FP registers.
333
334         mov sp, a0
335         add SIZE, a0
336         fmov fsN,(a0+)
337         fmov fsM,(a0+)
338         ...
339
340    + If the function needs a frame pointer, we set it here.
341
342         mov sp, a3
343
344    + Now we reserve space for the stack frame proper.  This could be
345      merged into the `add -SIZE, sp' instruction for FP saves up
346      above, unless we needed to set the frame pointer in the previous
347      step, or the frame is so large that allocating the whole thing at
348      once would put the FP register save slots out of reach of the
349      addressing mode (128 bytes).
350       
351         add -SIZE, sp        
352
353    One day we might keep the stack pointer constant, that won't
354    change the code for prologues, but it will make the frame
355    pointerless case much more common.  */
356
357 /* Analyze the prologue to determine where registers are saved,
358    the end of the prologue, etc etc.  Return the end of the prologue
359    scanned.
360
361    We store into FI (if non-null) several tidbits of information:
362
363    * stack_size -- size of this stack frame.  Note that if we stop in
364    certain parts of the prologue/epilogue we may claim the size of the
365    current frame is zero.  This happens when the current frame has
366    not been allocated yet or has already been deallocated.
367
368    * fsr -- Addresses of registers saved in the stack by this frame.
369
370    * status -- A (relatively) generic status indicator.  It's a bitmask
371    with the following bits: 
372
373    MY_FRAME_IN_SP: The base of the current frame is actually in
374    the stack pointer.  This can happen for frame pointerless
375    functions, or cases where we're stopped in the prologue/epilogue
376    itself.  For these cases mn10300_analyze_prologue will need up
377    update fi->frame before returning or analyzing the register
378    save instructions.
379
380    MY_FRAME_IN_FP: The base of the current frame is in the
381    frame pointer register ($a3).
382
383    NO_MORE_FRAMES: Set this if the current frame is "start" or
384    if the first instruction looks like mov <imm>,sp.  This tells
385    frame chain to not bother trying to unwind past this frame.  */
386
387 static CORE_ADDR
388 mn10300_analyze_prologue (struct frame_info *fi, CORE_ADDR pc)
389 {
390   CORE_ADDR func_addr, func_end, addr, stop;
391   CORE_ADDR stack_size;
392   int imm_size;
393   unsigned char buf[4];
394   int status, movm_args = 0;
395   char *name;
396
397   /* Use the PC in the frame if it's provided to look up the
398      start of this function.  */
399   pc = (fi ? fi->pc : pc);
400
401   /* Find the start of this function.  */
402   status = find_pc_partial_function (pc, &name, &func_addr, &func_end);
403
404   /* Do nothing if we couldn't find the start of this function or if we're
405      stopped at the first instruction in the prologue.  */
406   if (status == 0)
407     {
408       return pc;
409     }
410
411   /* If we're in start, then give up.  */
412   if (strcmp (name, "start") == 0)
413     {
414       if (fi != NULL)
415         fi->extra_info->status = NO_MORE_FRAMES;
416       return pc;
417     }
418
419   /* At the start of a function our frame is in the stack pointer.  */
420   if (fi)
421     fi->extra_info->status = MY_FRAME_IN_SP;
422
423   /* Get the next two bytes into buf, we need two because rets is a two
424      byte insn and the first isn't enough to uniquely identify it.  */
425   status = read_memory_nobpt (pc, buf, 2);
426   if (status != 0)
427     return pc;
428
429   /* If we're physically on an "rets" instruction, then our frame has
430      already been deallocated.  Note this can also be true for retf
431      and ret if they specify a size of zero.
432
433      In this case fi->frame is bogus, we need to fix it.  */
434   if (fi && buf[0] == 0xf0 && buf[1] == 0xfc)
435     {
436       if (fi->next == NULL)
437         fi->frame = read_sp ();
438       return fi->pc;
439     }
440
441   /* Similarly if we're stopped on the first insn of a prologue as our
442      frame hasn't been allocated yet.  */
443   if (fi && fi->pc == func_addr)
444     {
445       if (fi->next == NULL)
446         fi->frame = read_sp ();
447       return fi->pc;
448     }
449
450   /* Figure out where to stop scanning.  */
451   stop = fi ? fi->pc : func_end;
452
453   /* Don't walk off the end of the function.  */
454   stop = stop > func_end ? func_end : stop;
455
456   /* Start scanning on the first instruction of this function.  */
457   addr = func_addr;
458
459   /* Suck in two bytes.  */
460   status = read_memory_nobpt (addr, buf, 2);
461   if (status != 0)
462     {
463       fix_frame_pointer (fi, 0);
464       return addr;
465     }
466
467   /* First see if this insn sets the stack pointer from a register; if
468      so, it's probably the initialization of the stack pointer in _start,
469      so mark this as the bottom-most frame.  */
470   if (buf[0] == 0xf2 && (buf[1] & 0xf3) == 0xf0)
471     {
472       if (fi)
473         fi->extra_info->status = NO_MORE_FRAMES;
474       return addr;
475     }
476
477   /* Now look for movm [regs],sp, which saves the callee saved registers.
478
479      At this time we don't know if fi->frame is valid, so we only note
480      that we encountered a movm instruction.  Later, we'll set the entries
481      in fsr.regs as needed.  */
482   if (buf[0] == 0xcf)
483     {
484       /* Extract the register list for the movm instruction.  */
485       status = read_memory_nobpt (addr + 1, buf, 1);
486       movm_args = *buf;
487
488       addr += 2;
489
490       /* Quit now if we're beyond the stop point.  */
491       if (addr >= stop)
492         {
493           /* Fix fi->frame since it's bogus at this point.  */
494           if (fi && fi->next == NULL)
495             fi->frame = read_sp ();
496
497           /* Note if/where callee saved registers were saved.  */
498           set_movm_offsets (fi, movm_args);
499           return addr;
500         }
501
502       /* Get the next two bytes so the prologue scan can continue.  */
503       status = read_memory_nobpt (addr, buf, 2);
504       if (status != 0)
505         {
506           /* Fix fi->frame since it's bogus at this point.  */
507           if (fi && fi->next == NULL)
508             fi->frame = read_sp ();
509
510           /* Note if/where callee saved registers were saved.  */
511           set_movm_offsets (fi, movm_args);
512           return addr;
513         }
514     }
515
516   /* Now see if we set up a frame pointer via "mov sp,a3" */
517   if (buf[0] == 0x3f)
518     {
519       addr += 1;
520
521       /* The frame pointer is now valid.  */
522       if (fi)
523         {
524           fi->extra_info->status |= MY_FRAME_IN_FP;
525           fi->extra_info->status &= ~MY_FRAME_IN_SP;
526         }
527
528       /* Quit now if we're beyond the stop point.  */
529       if (addr >= stop)
530         {
531           /* Fix fi->frame if it's bogus at this point.  */
532           fix_frame_pointer (fi, 0);
533
534           /* Note if/where callee saved registers were saved.  */
535           set_movm_offsets (fi, movm_args);
536           return addr;
537         }
538
539       /* Get two more bytes so scanning can continue.  */
540       status = read_memory_nobpt (addr, buf, 2);
541       if (status != 0)
542         {
543           /* Fix fi->frame if it's bogus at this point.  */
544           fix_frame_pointer (fi, 0);
545
546           /* Note if/where callee saved registers were saved.  */
547           set_movm_offsets (fi, movm_args);
548           return addr;
549         }
550     }
551
552   /* Next we should allocate the local frame.  No more prologue insns
553      are found after allocating the local frame.
554
555      Search for add imm8,sp (0xf8feXX)
556      or add imm16,sp (0xfafeXXXX)
557      or add imm32,sp (0xfcfeXXXXXXXX).
558
559      If none of the above was found, then this prologue has no 
560      additional stack.  */
561
562   status = read_memory_nobpt (addr, buf, 2);
563   if (status != 0)
564     {
565       /* Fix fi->frame if it's bogus at this point.  */
566       fix_frame_pointer (fi, 0);
567
568       /* Note if/where callee saved registers were saved.  */
569       set_movm_offsets (fi, movm_args);
570       return addr;
571     }
572
573   imm_size = 0;
574   if (buf[0] == 0xf8 && buf[1] == 0xfe)
575     imm_size = 1;
576   else if (buf[0] == 0xfa && buf[1] == 0xfe)
577     imm_size = 2;
578   else if (buf[0] == 0xfc && buf[1] == 0xfe)
579     imm_size = 4;
580
581   if (imm_size != 0)
582     {
583       /* Suck in imm_size more bytes, they'll hold the size of the
584          current frame.  */
585       status = read_memory_nobpt (addr + 2, buf, imm_size);
586       if (status != 0)
587         {
588           /* Fix fi->frame if it's bogus at this point.  */
589           fix_frame_pointer (fi, 0);
590
591           /* Note if/where callee saved registers were saved.  */
592           set_movm_offsets (fi, movm_args);
593           return addr;
594         }
595
596       /* Note the size of the stack in the frame info structure.  */
597       stack_size = extract_signed_integer (buf, imm_size);
598       if (fi)
599         fi->extra_info->stack_size = stack_size;
600
601       /* We just consumed 2 + imm_size bytes.  */
602       addr += 2 + imm_size;
603
604       /* No more prologue insns follow, so begin preparation to return.  */
605       /* Fix fi->frame if it's bogus at this point.  */
606       fix_frame_pointer (fi, stack_size);
607
608       /* Note if/where callee saved registers were saved.  */
609       set_movm_offsets (fi, movm_args);
610       return addr;
611     }
612
613   /* We never found an insn which allocates local stack space, regardless
614      this is the end of the prologue.  */
615   /* Fix fi->frame if it's bogus at this point.  */
616   fix_frame_pointer (fi, 0);
617
618   /* Note if/where callee saved registers were saved.  */
619   set_movm_offsets (fi, movm_args);
620   return addr;
621 }
622
623
624 /* Function: saved_regs_size
625    Return the size in bytes of the register save area, based on the
626    saved_regs array in FI.  */
627 static int
628 saved_regs_size (struct frame_info *fi)
629 {
630   int adjust = 0;
631   int i;
632
633   /* Reserve four bytes for every register saved.  */
634   for (i = 0; i < NUM_REGS; i++)
635     if (fi->saved_regs[i])
636       adjust += 4;
637
638   /* If we saved LIR, then it's most likely we used a `movm'
639      instruction with the `other' bit set, in which case the SP is
640      decremented by an extra four bytes, "to simplify calculation
641      of the transfer area", according to the processor manual.  */
642   if (fi->saved_regs[LIR_REGNUM])
643     adjust += 4;
644
645   return adjust;
646 }
647
648
649 /* Function: frame_chain
650    Figure out and return the caller's frame pointer given current
651    frame_info struct.
652
653    We don't handle dummy frames yet but we would probably just return the
654    stack pointer that was in use at the time the function call was made?  */
655
656 static CORE_ADDR
657 mn10300_frame_chain (struct frame_info *fi)
658 {
659   struct frame_info *dummy;
660   /* Walk through the prologue to determine the stack size,
661      location of saved registers, end of the prologue, etc.  */
662   if (fi->extra_info->status == 0)
663     mn10300_analyze_prologue (fi, (CORE_ADDR) 0);
664
665   /* Quit now if mn10300_analyze_prologue set NO_MORE_FRAMES.  */
666   if (fi->extra_info->status & NO_MORE_FRAMES)
667     return 0;
668
669   /* Now that we've analyzed our prologue, determine the frame
670      pointer for our caller.
671
672      If our caller has a frame pointer, then we need to
673      find the entry value of $a3 to our function.
674
675      If fsr.regs[A3_REGNUM] is nonzero, then it's at the memory
676      location pointed to by fsr.regs[A3_REGNUM].
677
678      Else it's still in $a3.
679
680      If our caller does not have a frame pointer, then his
681      frame base is fi->frame + -caller's stack size.  */
682
683   /* The easiest way to get that info is to analyze our caller's frame.
684      So we set up a dummy frame and call mn10300_analyze_prologue to
685      find stuff for us.  */
686   dummy = analyze_dummy_frame (FRAME_SAVED_PC (fi), fi->frame);
687
688   if (dummy->extra_info->status & MY_FRAME_IN_FP)
689     {
690       /* Our caller has a frame pointer.  So find the frame in $a3 or
691          in the stack.  */
692       if (fi->saved_regs[A3_REGNUM])
693         return (read_memory_integer (fi->saved_regs[A3_REGNUM], REGISTER_SIZE));
694       else
695         return read_register (A3_REGNUM);
696     }
697   else
698     {
699       int adjust = saved_regs_size (fi);
700
701       /* Our caller does not have a frame pointer.  So his frame starts
702          at the base of our frame (fi->frame) + register save space
703          + <his size>.  */
704       return fi->frame + adjust + -dummy->extra_info->stack_size;
705     }
706 }
707
708 /* Function: skip_prologue
709    Return the address of the first inst past the prologue of the function.  */
710
711 static CORE_ADDR
712 mn10300_skip_prologue (CORE_ADDR pc)
713 {
714   /* We used to check the debug symbols, but that can lose if
715      we have a null prologue.  */
716   return mn10300_analyze_prologue (NULL, pc);
717 }
718
719 /* generic_pop_current_frame calls this function if the current
720    frame isn't a dummy frame.  */
721 static void
722 mn10300_pop_frame_regular (struct frame_info *frame)
723 {
724   int regnum;
725
726   write_register (PC_REGNUM, FRAME_SAVED_PC (frame));
727
728   /* Restore any saved registers.  */
729   for (regnum = 0; regnum < NUM_REGS; regnum++)
730     if (frame->saved_regs[regnum] != 0)
731       {
732         ULONGEST value;
733
734         value = read_memory_unsigned_integer (frame->saved_regs[regnum],
735                                               REGISTER_RAW_SIZE (regnum));
736         write_register (regnum, value);
737       }
738
739   /* Actually cut back the stack.  */
740   write_register (SP_REGNUM, FRAME_FP (frame));
741
742   /* Don't we need to set the PC?!?  XXX FIXME.  */
743 }
744
745 /* Function: pop_frame
746    This routine gets called when either the user uses the `return'
747    command, or the call dummy breakpoint gets hit.  */
748 static void
749 mn10300_pop_frame (void)
750 {
751   /* This function checks for and handles generic dummy frames, and
752      calls back to our function for ordinary frames.  */
753   generic_pop_current_frame (mn10300_pop_frame_regular);
754
755   /* Throw away any cached frame information.  */
756   flush_cached_frames ();
757 }
758
759 /* Function: push_arguments
760    Setup arguments for a call to the target.  Arguments go in
761    order on the stack.  */
762
763 static CORE_ADDR
764 mn10300_push_arguments (int nargs, struct value **args, CORE_ADDR sp,
765                         int struct_return, CORE_ADDR struct_addr)
766 {
767   int argnum = 0;
768   int len = 0;
769   int stack_offset = 0;
770   int regsused = struct_return ? 1 : 0;
771
772   /* This should be a nop, but align the stack just in case something
773      went wrong.  Stacks are four byte aligned on the mn10300.  */
774   sp &= ~3;
775
776   /* Now make space on the stack for the args.
777
778      XXX This doesn't appear to handle pass-by-invisible reference
779      arguments.  */
780   for (argnum = 0; argnum < nargs; argnum++)
781     {
782       int arg_length = (TYPE_LENGTH (VALUE_TYPE (args[argnum])) + 3) & ~3;
783
784       while (regsused < 2 && arg_length > 0)
785         {
786           regsused++;
787           arg_length -= 4;
788         }
789       len += arg_length;
790     }
791
792   /* Allocate stack space.  */
793   sp -= len;
794
795   regsused = struct_return ? 1 : 0;
796   /* Push all arguments onto the stack. */
797   for (argnum = 0; argnum < nargs; argnum++)
798     {
799       int len;
800       char *val;
801
802       /* XXX Check this.  What about UNIONS?  */
803       if (TYPE_CODE (VALUE_TYPE (*args)) == TYPE_CODE_STRUCT
804           && TYPE_LENGTH (VALUE_TYPE (*args)) > 8)
805         {
806           /* XXX Wrong, we want a pointer to this argument.  */
807           len = TYPE_LENGTH (VALUE_TYPE (*args));
808           val = (char *) VALUE_CONTENTS (*args);
809         }
810       else
811         {
812           len = TYPE_LENGTH (VALUE_TYPE (*args));
813           val = (char *) VALUE_CONTENTS (*args);
814         }
815
816       while (regsused < 2 && len > 0)
817         {
818           write_register (regsused, extract_unsigned_integer (val, 4));
819           val += 4;
820           len -= 4;
821           regsused++;
822         }
823
824       while (len > 0)
825         {
826           write_memory (sp + stack_offset, val, 4);
827           len -= 4;
828           val += 4;
829           stack_offset += 4;
830         }
831
832       args++;
833     }
834
835   /* Make space for the flushback area.  */
836   sp -= 8;
837   return sp;
838 }
839
840 /* Function: push_return_address (pc)
841    Set up the return address for the inferior function call.
842    Needed for targets where we don't actually execute a JSR/BSR instruction */
843
844 static CORE_ADDR
845 mn10300_push_return_address (CORE_ADDR pc, CORE_ADDR sp)
846 {
847   unsigned char buf[4];
848
849   store_unsigned_integer (buf, 4, CALL_DUMMY_ADDRESS ());
850   write_memory (sp - 4, buf, 4);
851   return sp - 4;
852 }
853
854 /* Function: store_struct_return (addr,sp)
855    Store the structure value return address for an inferior function
856    call.  */
857
858 static void
859 mn10300_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
860 {
861   /* The structure return address is passed as the first argument.  */
862   write_register (0, addr);
863 }
864
865 /* Function: frame_saved_pc 
866    Find the caller of this frame.  We do this by seeing if RP_REGNUM
867    is saved in the stack anywhere, otherwise we get it from the
868    registers.  If the inner frame is a dummy frame, return its PC
869    instead of RP, because that's where "caller" of the dummy-frame
870    will be found.  */
871
872 static CORE_ADDR
873 mn10300_frame_saved_pc (struct frame_info *fi)
874 {
875   int adjust = saved_regs_size (fi);
876
877   return (read_memory_integer (fi->frame + adjust, REGISTER_SIZE));
878 }
879
880 /* Function: mn10300_init_extra_frame_info
881    Setup the frame's frame pointer, pc, and frame addresses for saved
882    registers.  Most of the work is done in mn10300_analyze_prologue().
883
884    Note that when we are called for the last frame (currently active frame),
885    that fi->pc and fi->frame will already be setup.  However, fi->frame will
886    be valid only if this routine uses FP.  For previous frames, fi-frame will
887    always be correct.  mn10300_analyze_prologue will fix fi->frame if
888    it's not valid.
889
890    We can be called with the PC in the call dummy under two circumstances.
891    First, during normal backtracing, second, while figuring out the frame
892    pointer just prior to calling the target function (see run_stack_dummy).  */
893
894 static void
895 mn10300_init_extra_frame_info (int fromleaf, struct frame_info *fi)
896 {
897   if (fi->next)
898     fi->pc = FRAME_SAVED_PC (fi->next);
899
900   frame_saved_regs_zalloc (fi);
901   fi->extra_info = (struct frame_extra_info *)
902     frame_obstack_alloc (sizeof (struct frame_extra_info));
903
904   fi->extra_info->status = 0;
905   fi->extra_info->stack_size = 0;
906
907   mn10300_analyze_prologue (fi, 0);
908 }
909
910
911 /* This function's job is handled by init_extra_frame_info.  */
912 static void
913 mn10300_frame_init_saved_regs (struct frame_info *frame)
914 {
915 }
916
917
918 /* Function: mn10300_virtual_frame_pointer
919    Return the register that the function uses for a frame pointer, 
920    plus any necessary offset to be applied to the register before
921    any frame pointer offsets.  */
922
923 static void
924 mn10300_virtual_frame_pointer (CORE_ADDR pc,
925                                int *reg,
926                                LONGEST *offset)
927 {
928   struct frame_info *dummy = analyze_dummy_frame (pc, 0);
929   /* Set up a dummy frame_info, Analyze the prolog and fill in the
930      extra info.  */
931   /* Results will tell us which type of frame it uses.  */
932   if (dummy->extra_info->status & MY_FRAME_IN_SP)
933     {
934       *reg = SP_REGNUM;
935       *offset = -(dummy->extra_info->stack_size);
936     }
937   else
938     {
939       *reg = A3_REGNUM;
940       *offset = 0;
941     }
942 }
943
944 static int
945 mn10300_reg_struct_has_addr (int gcc_p, struct type *type)
946 {
947   return (TYPE_LENGTH (type) > 8);
948 }
949
950 static struct type *
951 mn10300_register_virtual_type (int reg)
952 {
953   return builtin_type_int;
954 }
955
956 static int
957 mn10300_register_byte (int reg)
958 {
959   return (reg * 4);
960 }
961
962 static int
963 mn10300_register_virtual_size (int reg)
964 {
965   return 4;
966 }
967
968 static int
969 mn10300_register_raw_size (int reg)
970 {
971   return 4;
972 }
973
974 /* If DWARF2 is a register number appearing in Dwarf2 debug info, then
975    mn10300_dwarf2_reg_to_regnum (DWARF2) is the corresponding GDB
976    register number.  Why don't Dwarf2 and GDB use the same numbering?
977    Who knows?  But since people have object files lying around with
978    the existing Dwarf2 numbering, and other people have written stubs
979    to work with the existing GDB, neither of them can change.  So we
980    just have to cope.  */
981 static int
982 mn10300_dwarf2_reg_to_regnum (int dwarf2)
983 {
984   /* This table is supposed to be shaped like the REGISTER_NAMES
985      initializer in gcc/config/mn10300/mn10300.h.  Registers which
986      appear in GCC's numbering, but have no counterpart in GDB's
987      world, are marked with a -1.  */
988   static int dwarf2_to_gdb[] = {
989     0,  1,  2,  3,  4,  5,  6,  7, -1, 8,
990     15, 16, 17, 18, 19, 20, 21, 22
991   };
992   int gdb;
993
994   if (dwarf2 < 0
995       || dwarf2 >= (sizeof (dwarf2_to_gdb) / sizeof (dwarf2_to_gdb[0]))
996       || dwarf2_to_gdb[dwarf2] == -1)
997     internal_error (__FILE__, __LINE__,
998                     "bogus register number in debug info: %d", dwarf2);
999
1000   return dwarf2_to_gdb[dwarf2];
1001 }
1002
1003 static void
1004 mn10300_print_register (const char *name, int regnum, int reg_width)
1005 {
1006   char *raw_buffer = alloca (MAX_REGISTER_RAW_SIZE);
1007
1008   if (reg_width)
1009     printf_filtered ("%*s: ", reg_width, name);
1010   else
1011     printf_filtered ("%s: ", name);
1012
1013   /* Get the data */
1014   if (!frame_register_read (selected_frame, regnum, raw_buffer))
1015     {
1016       printf_filtered ("[invalid]");
1017       return;
1018     }
1019   else
1020     {
1021       int byte;
1022       if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
1023         {
1024           for (byte = REGISTER_RAW_SIZE (regnum) - REGISTER_VIRTUAL_SIZE (regnum);
1025                byte < REGISTER_RAW_SIZE (regnum);
1026                byte++)
1027             printf_filtered ("%02x", (unsigned char) raw_buffer[byte]);
1028         }
1029       else
1030         {
1031           for (byte = REGISTER_VIRTUAL_SIZE (regnum) - 1;
1032                byte >= 0;
1033                byte--)
1034             printf_filtered ("%02x", (unsigned char) raw_buffer[byte]);
1035         }
1036     }
1037 }
1038
1039 static void
1040 mn10300_do_registers_info (int regnum, int fpregs)
1041 {
1042   if (regnum >= 0)
1043     {
1044       const char *name = REGISTER_NAME (regnum);
1045       if (name == NULL || name[0] == '\0')
1046         error ("Not a valid register for the current processor type");
1047       mn10300_print_register (name, regnum, 0);
1048       printf_filtered ("\n");
1049     }
1050   else
1051     {
1052       /* print registers in an array 4x8 */
1053       int r;
1054       int reg;
1055       const int nr_in_row = 4;
1056       const int reg_width = 4;
1057       for (r = 0; r < NUM_REGS; r += nr_in_row)
1058         {
1059           int c;
1060           int printing = 0;
1061           int padding = 0;
1062           for (c = r; c < r + nr_in_row; c++)
1063             {
1064               const char *name = REGISTER_NAME (c);
1065               if (name != NULL && *name != '\0')
1066                 {
1067                   printing = 1;
1068                   while (padding > 0)
1069                     {
1070                       printf_filtered (" ");
1071                       padding--;
1072                     }
1073                   mn10300_print_register (name, c, reg_width);
1074                   printf_filtered (" ");
1075                 }
1076               else
1077                 {
1078                   padding += (reg_width + 2 + 8 + 1);
1079                 }
1080             }
1081           if (printing)
1082             printf_filtered ("\n");
1083         }
1084     }
1085 }
1086
1087 /* Dump out the mn10300 speciic architecture information. */
1088
1089 static void
1090 mn10300_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file)
1091 {
1092   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
1093   fprintf_unfiltered (file, "mn10300_dump_tdep: am33_mode = %d\n",
1094                       tdep->am33_mode);
1095 }
1096
1097 static struct gdbarch *
1098 mn10300_gdbarch_init (struct gdbarch_info info,
1099                       struct gdbarch_list *arches)
1100 {
1101   static LONGEST mn10300_call_dummy_words[] = { 0 };
1102   struct gdbarch *gdbarch;
1103   struct gdbarch_tdep *tdep = NULL;
1104   int am33_mode;
1105   gdbarch_register_name_ftype *register_name;
1106   int mach;
1107   int num_regs;
1108
1109   arches = gdbarch_list_lookup_by_info (arches, &info);
1110   if (arches != NULL)
1111     return arches->gdbarch;
1112   tdep = xmalloc (sizeof (struct gdbarch_tdep));
1113   gdbarch = gdbarch_alloc (&info, tdep);
1114
1115   if (info.bfd_arch_info != NULL
1116       && info.bfd_arch_info->arch == bfd_arch_mn10300)
1117     mach = info.bfd_arch_info->mach;
1118   else
1119     mach = 0;
1120   switch (mach)
1121     {
1122     case 0:
1123     case bfd_mach_mn10300:
1124       am33_mode = 0;
1125       register_name = mn10300_generic_register_name;
1126       num_regs = 32;
1127       break;
1128     case bfd_mach_am33:
1129       am33_mode = 1;
1130       register_name = am33_register_name;
1131       num_regs = 32;
1132       break;
1133     default:
1134       internal_error (__FILE__, __LINE__,
1135                       "mn10300_gdbarch_init: Unknown mn10300 variant");
1136       return NULL; /* keep GCC happy. */
1137     }
1138
1139   /* Registers.  */
1140   set_gdbarch_num_regs (gdbarch, num_regs);
1141   set_gdbarch_register_name (gdbarch, register_name);
1142   set_gdbarch_register_size (gdbarch, 4);
1143   set_gdbarch_register_bytes (gdbarch, 
1144                               num_regs * gdbarch_register_size (gdbarch));
1145   set_gdbarch_max_register_raw_size (gdbarch, 4);
1146   set_gdbarch_register_raw_size (gdbarch, mn10300_register_raw_size);
1147   set_gdbarch_register_byte (gdbarch, mn10300_register_byte);
1148   set_gdbarch_max_register_virtual_size (gdbarch, 4);
1149   set_gdbarch_register_virtual_size (gdbarch, mn10300_register_virtual_size);
1150   set_gdbarch_register_virtual_type (gdbarch, mn10300_register_virtual_type);
1151   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, mn10300_dwarf2_reg_to_regnum);
1152   set_gdbarch_do_registers_info (gdbarch, mn10300_do_registers_info);
1153   set_gdbarch_sp_regnum (gdbarch, 8);
1154   set_gdbarch_pc_regnum (gdbarch, 9);
1155   set_gdbarch_fp_regnum (gdbarch, 31);
1156   set_gdbarch_virtual_frame_pointer (gdbarch, mn10300_virtual_frame_pointer);
1157
1158   /* Breakpoints.  */
1159   set_gdbarch_breakpoint_from_pc (gdbarch, mn10300_breakpoint_from_pc);
1160   set_gdbarch_function_start_offset (gdbarch, 0);
1161   set_gdbarch_decr_pc_after_break (gdbarch, 0);
1162
1163   /* Stack unwinding.  */
1164   set_gdbarch_get_saved_register (gdbarch, generic_get_saved_register);
1165   set_gdbarch_frame_chain_valid (gdbarch, generic_file_frame_chain_valid);
1166   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1167   set_gdbarch_frame_chain_valid (gdbarch, generic_file_frame_chain_valid);
1168   set_gdbarch_saved_pc_after_call (gdbarch, mn10300_saved_pc_after_call);
1169   set_gdbarch_init_extra_frame_info (gdbarch, mn10300_init_extra_frame_info);
1170   set_gdbarch_init_frame_pc (gdbarch, init_frame_pc_noop);
1171   set_gdbarch_frame_init_saved_regs (gdbarch, mn10300_frame_init_saved_regs);
1172   set_gdbarch_frame_chain (gdbarch, mn10300_frame_chain);
1173   set_gdbarch_frame_saved_pc (gdbarch, mn10300_frame_saved_pc);
1174   set_gdbarch_extract_return_value (gdbarch, mn10300_extract_return_value);
1175   set_gdbarch_extract_struct_value_address
1176     (gdbarch, mn10300_extract_struct_value_address);
1177   set_gdbarch_store_return_value (gdbarch, mn10300_store_return_value);
1178   set_gdbarch_store_struct_return (gdbarch, mn10300_store_struct_return);
1179   set_gdbarch_pop_frame (gdbarch, mn10300_pop_frame);
1180   set_gdbarch_skip_prologue (gdbarch, mn10300_skip_prologue);
1181   set_gdbarch_frame_args_skip (gdbarch, 0);
1182   set_gdbarch_frame_args_address (gdbarch, default_frame_address);
1183   set_gdbarch_frame_locals_address (gdbarch, default_frame_address);
1184   set_gdbarch_frame_num_args (gdbarch, frame_num_args_unknown);
1185   /* That's right, we're using the stack pointer as our frame pointer.  */
1186   set_gdbarch_read_fp (gdbarch, generic_target_read_sp);
1187
1188   /* Calling functions in the inferior from GDB.  */
1189   set_gdbarch_call_dummy_p (gdbarch, 1);
1190   set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 1);
1191   set_gdbarch_call_dummy_breakpoint_offset (gdbarch, 0);
1192   set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
1193   set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
1194   set_gdbarch_call_dummy_address (gdbarch, entry_point_address);
1195   set_gdbarch_call_dummy_words (gdbarch, mn10300_call_dummy_words);
1196   set_gdbarch_sizeof_call_dummy_words (gdbarch, 
1197                                        sizeof (mn10300_call_dummy_words));
1198   set_gdbarch_call_dummy_length (gdbarch, 0);
1199   set_gdbarch_fix_call_dummy (gdbarch, generic_fix_call_dummy);
1200   set_gdbarch_call_dummy_start_offset (gdbarch, 0);
1201   set_gdbarch_pc_in_call_dummy (gdbarch, pc_in_call_dummy_at_entry_point);
1202   set_gdbarch_use_generic_dummy_frames (gdbarch, 1);
1203   set_gdbarch_push_dummy_frame (gdbarch, generic_push_dummy_frame);
1204   set_gdbarch_push_arguments (gdbarch, mn10300_push_arguments);
1205   set_gdbarch_reg_struct_has_addr (gdbarch, mn10300_reg_struct_has_addr);
1206   set_gdbarch_push_return_address (gdbarch, mn10300_push_return_address);
1207   set_gdbarch_save_dummy_frame_tos (gdbarch, generic_save_dummy_frame_tos);
1208   set_gdbarch_use_struct_convention (gdbarch, mn10300_use_struct_convention);
1209
1210   tdep->am33_mode = am33_mode;
1211
1212   return gdbarch;
1213 }
1214  
1215 void
1216 _initialize_mn10300_tdep (void)
1217 {
1218 /*  printf("_initialize_mn10300_tdep\n"); */
1219
1220   tm_print_insn = print_insn_mn10300;
1221
1222   register_gdbarch_init (bfd_arch_mn10300, mn10300_gdbarch_init);
1223 }