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