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