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