2005-04-18 Michael Snyder <msnyder@redhat.com>
[external/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, 2003, 2004 Free
4    Software 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 "arch-utils.h"
25 #include "dis-asm.h"
26 #include "gdbtypes.h"
27 #include "regcache.h"
28 #include "gdb_string.h"
29 #include "gdb_assert.h"
30 #include "gdbcore.h"    /* for write_memory_unsigned_integer */
31 #include "value.h"
32 #include "gdbtypes.h"
33 #include "frame.h"
34 #include "frame-unwind.h"
35 #include "frame-base.h"
36 #include "trad-frame.h"
37 #include "symtab.h"
38 #include "dwarf2-frame.h"
39 #include "regcache.h"
40
41 #include "mn10300-tdep.h"
42
43 /* Forward decl.  */
44 extern struct trad_frame_cache *mn10300_frame_unwind_cache (struct frame_info*,
45                                                             void **);
46
47 /* Compute the alignment required by a type.  */
48
49 static int
50 mn10300_type_align (struct type *type)
51 {
52   int i, align = 1;
53
54   switch (TYPE_CODE (type))
55     {
56     case TYPE_CODE_INT:
57     case TYPE_CODE_ENUM:
58     case TYPE_CODE_SET:
59     case TYPE_CODE_RANGE:
60     case TYPE_CODE_CHAR:
61     case TYPE_CODE_BOOL:
62     case TYPE_CODE_FLT:
63     case TYPE_CODE_PTR:
64     case TYPE_CODE_REF:
65       return TYPE_LENGTH (type);
66
67     case TYPE_CODE_COMPLEX:
68       return TYPE_LENGTH (type) / 2;
69
70     case TYPE_CODE_STRUCT:
71     case TYPE_CODE_UNION:
72       for (i = 0; i < TYPE_NFIELDS (type); i++)
73         {
74           int falign = mn10300_type_align (TYPE_FIELD_TYPE (type, i));
75           while (align < falign)
76             align <<= 1;
77         }
78       return align;
79
80     case TYPE_CODE_ARRAY:
81       /* HACK!  Structures containing arrays, even small ones, are not
82          elligible for returning in registers.  */
83       return 256;
84
85     case TYPE_CODE_TYPEDEF:
86       return mn10300_type_align (check_typedef (type));
87
88     default:
89       internal_error (__FILE__, __LINE__, _("bad switch"));
90     }
91 }
92
93 /* MVS note this is deprecated.  */
94 /* Should call_function allocate stack space for a struct return?  */
95 /* gcc_p unused */
96 static int
97 mn10300_use_struct_convention (int gcc_p, struct type *type)
98 {
99   /* Structures bigger than a pair of words can't be returned in
100      registers.  */
101   if (TYPE_LENGTH (type) > 8)
102     return 1;
103
104   switch (TYPE_CODE (type))
105     {
106     case TYPE_CODE_STRUCT:
107     case TYPE_CODE_UNION:
108       /* Structures with a single field are handled as the field
109          itself.  */
110       if (TYPE_NFIELDS (type) == 1)
111         return mn10300_use_struct_convention (gcc_p, 
112                                               TYPE_FIELD_TYPE (type, 0));
113
114       /* Structures with word or double-word size are passed in memory, as
115          long as they require at least word alignment.  */
116       if (mn10300_type_align (type) >= 4)
117         return 0;
118
119       return 1;
120
121       /* Arrays are addressable, so they're never returned in
122          registers.  This condition can only hold when the array is
123          the only field of a struct or union.  */
124     case TYPE_CODE_ARRAY:
125       return 1;
126
127     case TYPE_CODE_TYPEDEF:
128       return mn10300_use_struct_convention (gcc_p, check_typedef (type));
129
130     default:
131       return 0;
132     }
133 }
134
135 /* MVS note this is deprecated.  */
136 static void
137 mn10300_store_return_value (struct type *type,
138                             struct regcache *regcache, const void *valbuf)
139 {
140   struct gdbarch *gdbarch = get_regcache_arch (regcache);
141   int len = TYPE_LENGTH (type);
142   int reg, regsz;
143   
144   if (TYPE_CODE (type) == TYPE_CODE_PTR)
145     reg = 4;
146   else
147     reg = 0;
148
149   regsz = register_size (gdbarch, reg);
150
151   if (len <= regsz)
152     regcache_raw_write_part (regcache, reg, 0, len, valbuf);
153   else if (len <= 2 * regsz)
154     {
155       regcache_raw_write (regcache, reg, valbuf);
156       gdb_assert (regsz == register_size (gdbarch, reg + 1));
157       regcache_raw_write_part (regcache, reg+1, 0,
158                                len - regsz, (char *) valbuf + regsz);
159     }
160   else
161     internal_error (__FILE__, __LINE__,
162                     _("Cannot store return value %d bytes long."), len);
163 }
164
165 /* MVS note deprecated.  */
166 static void
167 mn10300_extract_return_value (struct type *type,
168                               struct regcache *regcache, void *valbuf)
169 {
170   struct gdbarch *gdbarch = get_regcache_arch (regcache);
171   char buf[MAX_REGISTER_SIZE];
172   int len = TYPE_LENGTH (type);
173   int reg, regsz;
174
175   if (TYPE_CODE (type) == TYPE_CODE_PTR)
176     reg = 4;
177   else
178     reg = 0;
179
180   regsz = register_size (gdbarch, reg);
181   if (len <= regsz)
182     {
183       regcache_raw_read (regcache, reg, buf);
184       memcpy (valbuf, buf, len);
185     }
186   else if (len <= 2 * regsz)
187     {
188       regcache_raw_read (regcache, reg, buf);
189       memcpy (valbuf, buf, regsz);
190       gdb_assert (regsz == register_size (gdbarch, reg + 1));
191       regcache_raw_read (regcache, reg + 1, buf);
192       memcpy ((char *) valbuf + regsz, buf, len - regsz);
193     }
194   else
195     internal_error (__FILE__, __LINE__,
196                     _("Cannot extract return value %d bytes long."), len);
197 }
198
199 static char *
200 register_name (int reg, char **regs, long sizeof_regs)
201 {
202   if (reg < 0 || reg >= sizeof_regs / sizeof (regs[0]))
203     return NULL;
204   else
205     return regs[reg];
206 }
207
208 static const char *
209 mn10300_generic_register_name (int reg)
210 {
211   static char *regs[] =
212   { "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",
213     "sp", "pc", "mdr", "psw", "lir", "lar", "", "",
214     "", "", "", "", "", "", "", "",
215     "", "", "", "", "", "", "", "fp"
216   };
217   return register_name (reg, regs, sizeof regs);
218 }
219
220
221 static const char *
222 am33_register_name (int reg)
223 {
224   static char *regs[] =
225   { "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",
226     "sp", "pc", "mdr", "psw", "lir", "lar", "",
227     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
228     "ssp", "msp", "usp", "mcrh", "mcrl", "mcvf", "", "", ""
229   };
230   return register_name (reg, regs, sizeof regs);
231 }
232
233
234 static struct type *
235 mn10300_register_type (struct gdbarch *gdbarch, int reg)
236 {
237   return builtin_type_int;
238 }
239
240 static CORE_ADDR
241 mn10300_read_pc (ptid_t ptid)
242 {
243   return read_register_pid (E_PC_REGNUM, ptid);
244 }
245
246 static void
247 mn10300_write_pc (CORE_ADDR val, ptid_t ptid)
248 {
249   return write_register_pid (E_PC_REGNUM, val, ptid);
250 }
251
252 /* The breakpoint instruction must be the same size as the smallest
253    instruction in the instruction set.
254
255    The Matsushita mn10x00 processors have single byte instructions
256    so we need a single byte breakpoint.  Matsushita hasn't defined
257    one, so we defined it ourselves.  */
258
259 const static unsigned char *
260 mn10300_breakpoint_from_pc (CORE_ADDR *bp_addr, int *bp_size)
261 {
262   static char breakpoint[] = {0xff};
263   *bp_size = 1;
264   return breakpoint;
265 }
266
267 /* 
268  * Frame Extra Info:
269  *
270  *   status -- actually frame type (SP, FP, or last frame)
271  *   stack size -- offset to the next frame
272  * 
273  * The former might ultimately be stored in the frame_base.
274  * Seems like there'd be a way to store the later too.
275  *
276  * Temporarily supply empty stub functions as place holders.
277  */
278
279 static void
280 my_frame_is_in_sp (struct frame_info *fi, void **this_cache)
281 {
282   struct trad_frame_cache *cache = mn10300_frame_unwind_cache (fi, this_cache);
283   trad_frame_set_this_base (cache, 
284                             frame_unwind_register_unsigned (fi, 
285                                                             E_SP_REGNUM));
286 }
287
288 static void
289 my_frame_is_in_fp (struct frame_info *fi, void **this_cache)
290 {
291   struct trad_frame_cache *cache = mn10300_frame_unwind_cache (fi, this_cache);
292   trad_frame_set_this_base (cache, 
293                             frame_unwind_register_unsigned (fi, 
294                                                             E_A3_REGNUM));
295 }
296
297 static void
298 my_frame_is_last (struct frame_info *fi)
299 {
300 }
301
302 static int
303 is_my_frame_in_sp (struct frame_info *fi)
304 {
305   return 0;
306 }
307
308 static int
309 is_my_frame_in_fp (struct frame_info *fi)
310 {
311   return 0;
312 }
313
314 static int
315 is_my_frame_last (struct frame_info *fi)
316 {
317   return 0;
318 }
319
320 static void
321 set_my_stack_size (struct frame_info *fi, CORE_ADDR size)
322 {
323 }
324
325
326 /* Set offsets of registers saved by movm instruction.
327    This is a helper function for mn10300_analyze_prologue.  */
328
329 static void
330 set_movm_offsets (struct frame_info *fi, 
331                   void **this_cache, 
332                   int movm_args)
333 {
334   struct trad_frame_cache *cache;
335   int offset = 0;
336   CORE_ADDR base;
337
338   if (fi == NULL || this_cache == NULL)
339     return;
340
341   cache = mn10300_frame_unwind_cache (fi, this_cache);
342   if (cache == NULL)
343     return;
344
345   base = trad_frame_get_this_base (cache);
346   if (movm_args & movm_other_bit)
347     {
348       /* The `other' bit leaves a blank area of four bytes at the
349          beginning of its block of saved registers, making it 32 bytes
350          long in total.  */
351       trad_frame_set_reg_addr (cache, E_LAR_REGNUM,    base + offset + 4);
352       trad_frame_set_reg_addr (cache, E_LIR_REGNUM,    base + offset + 8);
353       trad_frame_set_reg_addr (cache, E_MDR_REGNUM,    base + offset + 12);
354       trad_frame_set_reg_addr (cache, E_A0_REGNUM + 1, base + offset + 16);
355       trad_frame_set_reg_addr (cache, E_A0_REGNUM,     base + offset + 20);
356       trad_frame_set_reg_addr (cache, E_D0_REGNUM + 1, base + offset + 24);
357       trad_frame_set_reg_addr (cache, E_D0_REGNUM,     base + offset + 28);
358       offset += 32;
359     }
360
361   if (movm_args & movm_a3_bit)
362     {
363       trad_frame_set_reg_addr (cache, E_A3_REGNUM, base + offset);
364       offset += 4;
365     }
366   if (movm_args & movm_a2_bit)
367     {
368       trad_frame_set_reg_addr (cache, E_A2_REGNUM, base + offset);
369       offset += 4;
370     }
371   if (movm_args & movm_d3_bit)
372     {
373       trad_frame_set_reg_addr (cache, E_D3_REGNUM, base + offset);
374       offset += 4;
375     }
376   if (movm_args & movm_d2_bit)
377     {
378       trad_frame_set_reg_addr (cache, E_D2_REGNUM, base + offset);
379       offset += 4;
380     }
381   if (AM33_MODE)
382     {
383       if (movm_args & movm_exother_bit)
384         {
385           trad_frame_set_reg_addr (cache, E_MCVF_REGNUM, base + offset);
386           trad_frame_set_reg_addr (cache, E_MCRL_REGNUM, base + offset + 4);
387           trad_frame_set_reg_addr (cache, E_MCRH_REGNUM, base + offset + 8);
388           trad_frame_set_reg_addr (cache, E_MDRQ_REGNUM, base + offset + 12);
389           trad_frame_set_reg_addr (cache, E_E1_REGNUM,   base + offset + 16);
390           trad_frame_set_reg_addr (cache, E_E0_REGNUM,   base + offset + 20);
391           offset += 24;
392         }
393       if (movm_args & movm_exreg1_bit)
394         {
395           trad_frame_set_reg_addr (cache, E_E7_REGNUM, base + offset);
396           trad_frame_set_reg_addr (cache, E_E6_REGNUM, base + offset + 4);
397           trad_frame_set_reg_addr (cache, E_E5_REGNUM, base + offset + 8);
398           trad_frame_set_reg_addr (cache, E_E4_REGNUM, base + offset + 12);
399           offset += 16;
400         }
401       if (movm_args & movm_exreg0_bit)
402         {
403           trad_frame_set_reg_addr (cache, E_E3_REGNUM, base + offset);
404           trad_frame_set_reg_addr (cache, E_E2_REGNUM, base + offset + 4);
405           offset += 8;
406         }
407     }
408   /* The last (or first) thing on the stack will be the PC.  */
409   trad_frame_set_reg_addr (cache, E_PC_REGNUM, base + offset);
410   /* Save the SP in the 'traditional' way.  
411      This will be the same location where the PC is saved.  */
412   trad_frame_set_reg_value (cache, E_SP_REGNUM, base + offset);
413 }
414
415 /* The main purpose of this file is dealing with prologues to extract
416    information about stack frames and saved registers.
417
418    In gcc/config/mn13000/mn10300.c, the expand_prologue prologue
419    function is pretty readable, and has a nice explanation of how the
420    prologue is generated.  The prologues generated by that code will
421    have the following form (NOTE: the current code doesn't handle all
422    this!):
423
424    + If this is an old-style varargs function, then its arguments
425      need to be flushed back to the stack:
426      
427         mov d0,(4,sp)
428         mov d1,(4,sp)
429
430    + If we use any of the callee-saved registers, save them now.
431      
432         movm [some callee-saved registers],(sp)
433
434    + If we have any floating-point registers to save:
435
436      - Decrement the stack pointer to reserve space for the registers.
437        If the function doesn't need a frame pointer, we may combine
438        this with the adjustment that reserves space for the frame.
439
440         add -SIZE, sp
441
442      - Save the floating-point registers.  We have two possible
443        strategies:
444
445        . Save them at fixed offset from the SP:
446
447         fmov fsN,(OFFSETN,sp)
448         fmov fsM,(OFFSETM,sp)
449         ...
450
451        Note that, if OFFSETN happens to be zero, you'll get the
452        different opcode: fmov fsN,(sp)
453
454        . Or, set a0 to the start of the save area, and then use
455        post-increment addressing to save the FP registers.
456
457         mov sp, a0
458         add SIZE, a0
459         fmov fsN,(a0+)
460         fmov fsM,(a0+)
461         ...
462
463    + If the function needs a frame pointer, we set it here.
464
465         mov sp, a3
466
467    + Now we reserve space for the stack frame proper.  This could be
468      merged into the `add -SIZE, sp' instruction for FP saves up
469      above, unless we needed to set the frame pointer in the previous
470      step, or the frame is so large that allocating the whole thing at
471      once would put the FP register save slots out of reach of the
472      addressing mode (128 bytes).
473       
474         add -SIZE, sp        
475
476    One day we might keep the stack pointer constant, that won't
477    change the code for prologues, but it will make the frame
478    pointerless case much more common.  */
479
480 /* Analyze the prologue to determine where registers are saved,
481    the end of the prologue, etc etc.  Return the end of the prologue
482    scanned.
483
484    We store into FI (if non-null) several tidbits of information:
485
486    * stack_size -- size of this stack frame.  Note that if we stop in
487    certain parts of the prologue/epilogue we may claim the size of the
488    current frame is zero.  This happens when the current frame has
489    not been allocated yet or has already been deallocated.
490
491    * fsr -- Addresses of registers saved in the stack by this frame.
492
493    * status -- A (relatively) generic status indicator.  It's a bitmask
494    with the following bits: 
495
496    MY_FRAME_IN_SP: The base of the current frame is actually in
497    the stack pointer.  This can happen for frame pointerless
498    functions, or cases where we're stopped in the prologue/epilogue
499    itself.  For these cases mn10300_analyze_prologue will need up
500    update fi->frame before returning or analyzing the register
501    save instructions.
502
503    MY_FRAME_IN_FP: The base of the current frame is in the
504    frame pointer register ($a3).
505
506    NO_MORE_FRAMES: Set this if the current frame is "start" or
507    if the first instruction looks like mov <imm>,sp.  This tells
508    frame chain to not bother trying to unwind past this frame.  */
509
510 static CORE_ADDR
511 mn10300_analyze_prologue (struct frame_info *fi, 
512                           void **this_cache, 
513                           CORE_ADDR pc)
514 {
515   CORE_ADDR func_addr, func_end, addr, stop;
516   long      stack_size;
517   int imm_size;
518   unsigned char buf[4];
519   int status, movm_args = 0;
520   char *name;
521
522   /* Use the PC in the frame if it's provided to look up the
523      start of this function.
524
525      Note: kevinb/2003-07-16: We used to do the following here:
526         pc = (fi ? get_frame_pc (fi) : pc);
527      But this is (now) badly broken when called from analyze_dummy_frame().
528   */
529   if (fi)
530     {
531       pc = (pc ? pc : get_frame_pc (fi));
532       /* At the start of a function our frame is in the stack pointer.  */
533       my_frame_is_in_sp (fi, this_cache);
534     }
535
536   /* Find the start of this function.  */
537   status = find_pc_partial_function (pc, &name, &func_addr, &func_end);
538
539   /* Do nothing if we couldn't find the start of this function 
540
541      MVS: comment went on to say "or if we're stopped at the first
542      instruction in the prologue" -- but code doesn't reflect that, 
543      and I don't want to do that anyway.  */
544   if (status == 0)
545     {
546       return pc;
547     }
548
549   /* If we're in start, then give up.  */
550   if (strcmp (name, "start") == 0)
551     {
552       if (fi != NULL)
553         my_frame_is_last (fi);
554       return pc;
555     }
556
557 #if 0
558   /* Get the next two bytes into buf, we need two because rets is a two
559      byte insn and the first isn't enough to uniquely identify it.  */
560   status = deprecated_read_memory_nobpt (pc, buf, 2);
561   if (status != 0)
562     return pc;
563
564   /* Note: kevinb/2003-07-16: We shouldn't be making these sorts of
565      changes to the frame in prologue examination code.  */
566   /* If we're physically on an "rets" instruction, then our frame has
567      already been deallocated.  Note this can also be true for retf
568      and ret if they specify a size of zero.
569
570      In this case fi->frame is bogus, we need to fix it.  */
571   if (fi && buf[0] == 0xf0 && buf[1] == 0xfc)
572     {
573       if (get_next_frame (fi) == NULL)
574         deprecated_update_frame_base_hack (fi, read_sp ());
575       return get_frame_pc (fi);
576     }
577
578   /* Similarly if we're stopped on the first insn of a prologue as our
579      frame hasn't been allocated yet.  */
580   if (fi && get_frame_pc (fi) == func_addr)
581     {
582       if (get_next_frame (fi) == NULL)
583         deprecated_update_frame_base_hack (fi, read_sp ());
584       return get_frame_pc (fi);
585     }
586 #endif
587
588   /* NOTE: from here on, we don't want to return without jumping to
589      finish_prologue.  */
590
591
592   /* Figure out where to stop scanning.  */
593   stop = fi ? pc : func_end;
594
595   /* Don't walk off the end of the function.  */
596   stop = stop > func_end ? func_end : stop;
597
598   /* Start scanning on the first instruction of this function.  */
599   addr = func_addr;
600
601   /* Suck in two bytes.  */
602   if (addr + 2 >= stop
603       || (status = deprecated_read_memory_nobpt (addr, buf, 2)) != 0)
604     goto finish_prologue;
605
606   /* First see if this insn sets the stack pointer from a register; if
607      so, it's probably the initialization of the stack pointer in _start,
608      so mark this as the bottom-most frame.  */
609   if (buf[0] == 0xf2 && (buf[1] & 0xf3) == 0xf0)
610     {
611       if (fi)
612         my_frame_is_last (fi);
613       goto finish_prologue;
614     }
615
616   /* Now look for movm [regs],sp, which saves the callee saved registers.
617
618      At this time we don't know if fi->frame is valid, so we only note
619      that we encountered a movm instruction.  Later, we'll set the entries
620      in fsr.regs as needed.  */
621   if (buf[0] == 0xcf)
622     {
623       /* Extract the register list for the movm instruction.  */
624       movm_args = buf[1];
625
626       addr += 2;
627
628       /* Quit now if we're beyond the stop point.  */
629       if (addr >= stop)
630         goto finish_prologue;
631
632       /* Get the next two bytes so the prologue scan can continue.  */
633       status = deprecated_read_memory_nobpt (addr, buf, 2);
634       if (status != 0)
635         goto finish_prologue;
636     }
637
638   /* Now see if we set up a frame pointer via "mov sp,a3" */
639   if (buf[0] == 0x3f)
640     {
641       addr += 1;
642
643       /* The frame pointer is now valid.  */
644       if (fi)
645         {
646           my_frame_is_in_fp (fi, this_cache);
647         }
648
649       /* Quit now if we're beyond the stop point.  */
650       if (addr >= stop)
651         goto finish_prologue;
652
653       /* Get two more bytes so scanning can continue.  */
654       status = deprecated_read_memory_nobpt (addr, buf, 2);
655       if (status != 0)
656         goto finish_prologue;
657     }
658
659   /* Next we should allocate the local frame.  No more prologue insns
660      are found after allocating the local frame.
661
662      Search for add imm8,sp (0xf8feXX)
663      or add imm16,sp (0xfafeXXXX)
664      or add imm32,sp (0xfcfeXXXXXXXX).
665
666      If none of the above was found, then this prologue has no 
667      additional stack.  */
668
669   imm_size = 0;
670   if (buf[0] == 0xf8 && buf[1] == 0xfe)
671     imm_size = 1;
672   else if (buf[0] == 0xfa && buf[1] == 0xfe)
673     imm_size = 2;
674   else if (buf[0] == 0xfc && buf[1] == 0xfe)
675     imm_size = 4;
676
677   if (imm_size != 0)
678     {
679       /* Suck in imm_size more bytes, they'll hold the size of the
680          current frame.  */
681       status = deprecated_read_memory_nobpt (addr + 2, buf, imm_size);
682       if (status != 0)
683         goto finish_prologue;
684
685       /* Note the size of the stack in the frame info structure.  */
686       stack_size = extract_signed_integer (buf, imm_size);
687       if (fi)
688         set_my_stack_size (fi, stack_size);
689
690       /* We just consumed 2 + imm_size bytes.  */
691       addr += 2 + imm_size;
692
693       /* No more prologue insns follow, so begin preparation to return.  */
694       goto finish_prologue;
695     }
696   /* Do the essentials and get out of here.  */
697  finish_prologue:
698   /* Note if/where callee saved registers were saved.  */
699   if (fi)
700     set_movm_offsets (fi, this_cache, movm_args);
701   return addr;
702 }
703
704 /* Function: skip_prologue
705    Return the address of the first inst past the prologue of the function.  */
706
707 static CORE_ADDR
708 mn10300_skip_prologue (CORE_ADDR pc)
709 {
710   return mn10300_analyze_prologue (NULL, NULL, pc);
711 }
712
713 /* Simple frame_unwind_cache.  
714    This finds the "extra info" for the frame.  */
715 struct trad_frame_cache *
716 mn10300_frame_unwind_cache (struct frame_info *next_frame,
717                             void **this_prologue_cache)
718 {
719   struct trad_frame_cache *cache;
720   CORE_ADDR pc, start, end;
721
722   if (*this_prologue_cache)
723     return (*this_prologue_cache);
724
725   cache = trad_frame_cache_zalloc (next_frame);
726   pc = gdbarch_unwind_pc (current_gdbarch, next_frame);
727   mn10300_analyze_prologue (next_frame, (void **) &cache, pc);
728   if (find_pc_partial_function (pc, NULL, &start, &end))
729     trad_frame_set_id (cache, 
730                        frame_id_build (trad_frame_get_this_base (cache), 
731                                        start));
732   else
733     trad_frame_set_id (cache, 
734                        frame_id_build (trad_frame_get_this_base (cache), 
735                                        frame_func_unwind (next_frame)));
736
737   (*this_prologue_cache) = cache;
738   return cache;
739 }
740
741 /* Here is a dummy implementation.  */
742 static struct frame_id
743 mn10300_unwind_dummy_id (struct gdbarch *gdbarch,
744                          struct frame_info *next_frame)
745 {
746   return frame_id_build (frame_sp_unwind (next_frame), 
747                          frame_pc_unwind (next_frame));
748 }
749
750 /* Trad frame implementation.  */
751 static void
752 mn10300_frame_this_id (struct frame_info *next_frame,
753                        void **this_prologue_cache,
754                        struct frame_id *this_id)
755 {
756   struct trad_frame_cache *cache = 
757     mn10300_frame_unwind_cache (next_frame, this_prologue_cache);
758
759   trad_frame_get_id (cache, this_id);
760 }
761
762 static void
763 mn10300_frame_prev_register (struct frame_info *next_frame,
764                              void **this_prologue_cache,
765                              int regnum, int *optimizedp,
766                              enum lval_type *lvalp, CORE_ADDR *addrp,
767                              int *realnump, void *bufferp)
768 {
769   struct trad_frame_cache *cache =
770     mn10300_frame_unwind_cache (next_frame, this_prologue_cache);
771
772   trad_frame_get_register (cache, next_frame, regnum, optimizedp, 
773                            lvalp, addrp, realnump, bufferp);
774   /* Or...
775   trad_frame_get_prev_register (next_frame, cache->prev_regs, regnum, 
776                            optimizedp, lvalp, addrp, realnump, bufferp);
777   */
778 }
779
780 static const struct frame_unwind mn10300_frame_unwind = {
781   NORMAL_FRAME,
782   mn10300_frame_this_id, 
783   mn10300_frame_prev_register
784 };
785
786 static CORE_ADDR
787 mn10300_frame_base_address (struct frame_info *next_frame,
788                             void **this_prologue_cache)
789 {
790   struct trad_frame_cache *cache = 
791     mn10300_frame_unwind_cache (next_frame, this_prologue_cache);
792
793   return trad_frame_get_this_base (cache);
794 }
795
796 static const struct frame_unwind *
797 mn10300_frame_sniffer (struct frame_info *next_frame)
798 {
799   return &mn10300_frame_unwind;
800 }
801
802 static const struct frame_base mn10300_frame_base = {
803   &mn10300_frame_unwind, 
804   mn10300_frame_base_address, 
805   mn10300_frame_base_address,
806   mn10300_frame_base_address
807 };
808
809 static CORE_ADDR
810 mn10300_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
811 {
812   ULONGEST pc;
813
814   frame_unwind_unsigned_register (next_frame, E_PC_REGNUM, &pc);
815   return pc;
816 }
817
818 static CORE_ADDR
819 mn10300_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
820 {
821   ULONGEST sp;
822
823   frame_unwind_unsigned_register (next_frame, E_SP_REGNUM, &sp);
824   return sp;
825 }
826
827 static void
828 mn10300_frame_unwind_init (struct gdbarch *gdbarch)
829 {
830   frame_unwind_append_sniffer (gdbarch, dwarf2_frame_sniffer);
831   frame_unwind_append_sniffer (gdbarch, mn10300_frame_sniffer);
832   frame_base_set_default (gdbarch, &mn10300_frame_base);
833   set_gdbarch_unwind_dummy_id (gdbarch, mn10300_unwind_dummy_id);
834   set_gdbarch_unwind_pc (gdbarch, mn10300_unwind_pc);
835   set_gdbarch_unwind_sp (gdbarch, mn10300_unwind_sp);
836 }
837
838 /* Function: push_dummy_call
839  *
840  * Set up machine state for a target call, including
841  * function arguments, stack, return address, etc.
842  *
843  */
844
845 static CORE_ADDR
846 mn10300_push_dummy_call (struct gdbarch *gdbarch, 
847                          struct value *target_func,
848                          struct regcache *regcache,
849                          CORE_ADDR bp_addr, 
850                          int nargs, struct value **args,
851                          CORE_ADDR sp, 
852                          int struct_return,
853                          CORE_ADDR struct_addr)
854 {
855   const int push_size = register_size (gdbarch, E_PC_REGNUM);
856   int regs_used;
857   int len, arg_len; 
858   int stack_offset = 0;
859   int argnum;
860   char *val, valbuf[MAX_REGISTER_SIZE];
861
862   /* This should be a nop, but align the stack just in case something
863      went wrong.  Stacks are four byte aligned on the mn10300.  */
864   sp &= ~3;
865
866   /* Now make space on the stack for the args.
867
868      XXX This doesn't appear to handle pass-by-invisible reference
869      arguments.  */
870   regs_used = struct_return ? 1 : 0;
871   for (len = 0, argnum = 0; argnum < nargs; argnum++)
872     {
873       arg_len = (TYPE_LENGTH (value_type (args[argnum])) + 3) & ~3;
874       while (regs_used < 2 && arg_len > 0)
875         {
876           regs_used++;
877           arg_len -= push_size;
878         }
879       len += arg_len;
880     }
881
882   /* Allocate stack space.  */
883   sp -= len;
884
885   if (struct_return)
886     {
887       regs_used = 1;
888       write_register (E_D0_REGNUM, struct_addr);
889     }
890   else
891     regs_used = 0;
892
893   /* Push all arguments onto the stack. */
894   for (argnum = 0; argnum < nargs; argnum++)
895     {
896       /* FIXME what about structs?  Unions?  */
897       if (TYPE_CODE (value_type (*args)) == TYPE_CODE_STRUCT
898           && TYPE_LENGTH (value_type (*args)) > 8)
899         {
900           /* Change to pointer-to-type.  */
901           arg_len = push_size;
902           store_unsigned_integer (valbuf, push_size, 
903                                   VALUE_ADDRESS (*args));
904           val = &valbuf[0];
905         }
906       else
907         {
908           arg_len = TYPE_LENGTH (value_type (*args));
909           val = (char *) value_contents (*args);
910         }
911
912       while (regs_used < 2 && arg_len > 0)
913         {
914           write_register (regs_used, 
915                           extract_unsigned_integer (val, push_size));
916           val += push_size;
917           arg_len -= push_size;
918           regs_used++;
919         }
920
921       while (arg_len > 0)
922         {
923           write_memory (sp + stack_offset, val, push_size);
924           arg_len -= push_size;
925           val += push_size;
926           stack_offset += push_size;
927         }
928
929       args++;
930     }
931
932   /* Make space for the flushback area.  */
933   sp -= 8;
934
935   /* Push the return address that contains the magic breakpoint.  */
936   sp -= 4;
937   write_memory_unsigned_integer (sp, push_size, bp_addr);
938   /* Update $sp.  */
939   regcache_cooked_write_unsigned (regcache, E_SP_REGNUM, sp);
940   return sp;
941 }
942
943
944 static struct gdbarch *
945 mn10300_gdbarch_init (struct gdbarch_info info,
946                       struct gdbarch_list *arches)
947 {
948   struct gdbarch *gdbarch;
949   struct gdbarch_tdep *tdep;
950
951   arches = gdbarch_list_lookup_by_info (arches, &info);
952   if (arches != NULL)
953     return arches->gdbarch;
954
955   tdep = xmalloc (sizeof (struct gdbarch_tdep));
956   gdbarch = gdbarch_alloc (&info, tdep);
957
958   switch (info.bfd_arch_info->mach)
959     {
960     case 0:
961     case bfd_mach_mn10300:
962       set_gdbarch_register_name (gdbarch, mn10300_generic_register_name);
963       tdep->am33_mode = 0;
964       break;
965     case bfd_mach_am33:
966       set_gdbarch_register_name (gdbarch, am33_register_name);
967       tdep->am33_mode = 1;
968       break;
969     default:
970       internal_error (__FILE__, __LINE__,
971                       _("mn10300_gdbarch_init: Unknown mn10300 variant"));
972       break;
973     }
974
975   /* Registers.  */
976   set_gdbarch_num_regs (gdbarch, E_NUM_REGS);
977   set_gdbarch_register_type (gdbarch, mn10300_register_type);
978   set_gdbarch_skip_prologue (gdbarch, mn10300_skip_prologue);
979   set_gdbarch_read_pc (gdbarch, mn10300_read_pc);
980   set_gdbarch_write_pc (gdbarch, mn10300_write_pc);
981   set_gdbarch_pc_regnum (gdbarch, E_PC_REGNUM);
982   set_gdbarch_sp_regnum (gdbarch, E_SP_REGNUM);
983
984   /* Stack unwinding.  */
985   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
986   /* Breakpoints.  */
987   set_gdbarch_breakpoint_from_pc (gdbarch, mn10300_breakpoint_from_pc);
988   /* decr_pc_after_break? */
989   /* Disassembly.  */
990   set_gdbarch_print_insn (gdbarch, print_insn_mn10300);
991
992   /* Stage 2 */
993   /* MVS Note: at least the first one is deprecated!  */
994   set_gdbarch_deprecated_use_struct_convention (gdbarch, 
995                                                 mn10300_use_struct_convention);
996   set_gdbarch_store_return_value (gdbarch, mn10300_store_return_value);
997   set_gdbarch_extract_return_value (gdbarch, mn10300_extract_return_value);
998   
999   /* Stage 3 -- get target calls working.  */
1000   set_gdbarch_push_dummy_call (gdbarch, mn10300_push_dummy_call);
1001   /* set_gdbarch_return_value (store, extract) */
1002
1003
1004   mn10300_frame_unwind_init (gdbarch);
1005
1006   return gdbarch;
1007 }
1008  
1009 /* Dump out the mn10300 specific architecture information. */
1010
1011 static void
1012 mn10300_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file)
1013 {
1014   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
1015   fprintf_unfiltered (file, "mn10300_dump_tdep: am33_mode = %d\n",
1016                       tdep->am33_mode);
1017 }
1018
1019 void
1020 _initialize_mn10300_tdep (void)
1021 {
1022   gdbarch_register (bfd_arch_mn10300, mn10300_gdbarch_init, mn10300_dump_tdep);
1023 }
1024