OSDN Git Service

* gdbarch.sh (get_longjmp_target): Add FRAME argument.
[pf3gnuchains/pf3gnuchains3x.git] / gdb / m68k-tdep.c
1 /* Target-dependent code for the Motorola 68000 series.
2
3    Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1999, 2000, 2001,
4    2002, 2003, 2004, 2005, 2006, 2007 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., 51 Franklin Street, Fifth Floor,
21    Boston, MA 02110-1301, USA.  */
22
23 #include "defs.h"
24 #include "dwarf2-frame.h"
25 #include "frame.h"
26 #include "frame-base.h"
27 #include "frame-unwind.h"
28 #include "gdbtypes.h"
29 #include "symtab.h"
30 #include "gdbcore.h"
31 #include "value.h"
32 #include "gdb_string.h"
33 #include "gdb_assert.h"
34 #include "inferior.h"
35 #include "regcache.h"
36 #include "arch-utils.h"
37 #include "osabi.h"
38 #include "dis-asm.h"
39 #include "target-descriptions.h"
40
41 #include "m68k-tdep.h"
42 \f
43
44 #define P_LINKL_FP      0x480e
45 #define P_LINKW_FP      0x4e56
46 #define P_PEA_FP        0x4856
47 #define P_MOVEAL_SP_FP  0x2c4f
48 #define P_ADDAW_SP      0xdefc
49 #define P_ADDAL_SP      0xdffc
50 #define P_SUBQW_SP      0x514f
51 #define P_SUBQL_SP      0x518f
52 #define P_LEA_SP_SP     0x4fef
53 #define P_LEA_PC_A5     0x4bfb0170
54 #define P_FMOVEMX_SP    0xf227
55 #define P_MOVEL_SP      0x2f00
56 #define P_MOVEML_SP     0x48e7
57
58 /* Offset from SP to first arg on stack at first instruction of a function */
59 #define SP_ARG0 (1 * 4)
60
61 #if !defined (BPT_VECTOR)
62 #define BPT_VECTOR 0xf
63 #endif
64
65 static const gdb_byte *
66 m68k_local_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
67 {
68   static gdb_byte break_insn[] = {0x4e, (0x40 | BPT_VECTOR)};
69   *lenptr = sizeof (break_insn);
70   return break_insn;
71 }
72
73 /* Return the GDB type object for the "standard" data type of data in
74    register N.  This should be int for D0-D7, SR, FPCONTROL and
75    FPSTATUS, long double for FP0-FP7, and void pointer for all others
76    (A0-A7, PC, FPIADDR).  Note, for registers which contain
77    addresses return pointer to void, not pointer to char, because we
78    don't want to attempt to print the string after printing the
79    address.  */
80
81 static struct type *
82 m68k_register_type (struct gdbarch *gdbarch, int regnum)
83 {
84   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
85
86   if (tdep->fpregs_present)
87     {
88       if (regnum >= FP0_REGNUM && regnum <= FP0_REGNUM + 7)
89         {
90           if (tdep->flavour == m68k_coldfire_flavour)
91             return builtin_type (gdbarch)->builtin_double;
92           else
93             return builtin_type_m68881_ext;
94         }
95
96       if (regnum == M68K_FPI_REGNUM)
97         return builtin_type_void_func_ptr;
98
99       if (regnum == M68K_FPC_REGNUM || regnum == M68K_FPS_REGNUM)
100         return builtin_type_int32;
101     }
102   else
103     {
104       if (regnum >= M68K_FP0_REGNUM && regnum <= M68K_FPI_REGNUM)
105         return builtin_type_int0;
106     }
107
108   if (regnum == PC_REGNUM)
109     return builtin_type_void_func_ptr;
110
111   if (regnum >= M68K_A0_REGNUM && regnum <= M68K_A0_REGNUM + 7)
112     return builtin_type_void_data_ptr;
113
114   return builtin_type_int32;
115 }
116
117 static const char *m68k_register_names[] = {
118     "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
119     "a0", "a1", "a2", "a3", "a4", "a5", "fp", "sp",
120     "ps", "pc",
121     "fp0", "fp1", "fp2", "fp3", "fp4", "fp5", "fp6", "fp7",
122     "fpcontrol", "fpstatus", "fpiaddr"
123   };
124
125 /* Function: m68k_register_name
126    Returns the name of the standard m68k register regnum. */
127
128 static const char *
129 m68k_register_name (int regnum)
130 {
131   if (regnum < 0 || regnum >= ARRAY_SIZE (m68k_register_names))
132     internal_error (__FILE__, __LINE__,
133                     _("m68k_register_name: illegal register number %d"), regnum);
134   else
135     return m68k_register_names[regnum];
136 }
137 \f
138 /* Return nonzero if a value of type TYPE stored in register REGNUM
139    needs any special handling.  */
140
141 static int
142 m68k_convert_register_p (int regnum, struct type *type)
143 {
144   if (!gdbarch_tdep (current_gdbarch)->fpregs_present)
145     return 0;
146   return (regnum >= M68K_FP0_REGNUM && regnum <= M68K_FP0_REGNUM + 7);
147 }
148
149 /* Read a value of type TYPE from register REGNUM in frame FRAME, and
150    return its contents in TO.  */
151
152 static void
153 m68k_register_to_value (struct frame_info *frame, int regnum,
154                         struct type *type, gdb_byte *to)
155 {
156   gdb_byte from[M68K_MAX_REGISTER_SIZE];
157   struct type *fpreg_type = register_type (current_gdbarch, M68K_FP0_REGNUM);
158
159   /* We only support floating-point values.  */
160   if (TYPE_CODE (type) != TYPE_CODE_FLT)
161     {
162       warning (_("Cannot convert floating-point register value "
163                "to non-floating-point type."));
164       return;
165     }
166
167   /* Convert to TYPE.  This should be a no-op if TYPE is equivalent to
168      the extended floating-point format used by the FPU.  */
169   get_frame_register (frame, regnum, from);
170   convert_typed_floating (from, fpreg_type, to, type);
171 }
172
173 /* Write the contents FROM of a value of type TYPE into register
174    REGNUM in frame FRAME.  */
175
176 static void
177 m68k_value_to_register (struct frame_info *frame, int regnum,
178                         struct type *type, const gdb_byte *from)
179 {
180   gdb_byte to[M68K_MAX_REGISTER_SIZE];
181   struct type *fpreg_type = register_type (current_gdbarch, M68K_FP0_REGNUM);
182
183   /* We only support floating-point values.  */
184   if (TYPE_CODE (type) != TYPE_CODE_FLT)
185     {
186       warning (_("Cannot convert non-floating-point type "
187                "to floating-point register value."));
188       return;
189     }
190
191   /* Convert from TYPE.  This should be a no-op if TYPE is equivalent
192      to the extended floating-point format used by the FPU.  */
193   convert_typed_floating (from, type, to, fpreg_type);
194   put_frame_register (frame, regnum, to);
195 }
196
197 \f
198 /* There is a fair number of calling conventions that are in somewhat
199    wide use.  The 68000/08/10 don't support an FPU, not even as a
200    coprocessor.  All function return values are stored in %d0/%d1.
201    Structures are returned in a static buffer, a pointer to which is
202    returned in %d0.  This means that functions returning a structure
203    are not re-entrant.  To avoid this problem some systems use a
204    convention where the caller passes a pointer to a buffer in %a1
205    where the return values is to be stored.  This convention is the
206    default, and is implemented in the function m68k_return_value.
207
208    The 68020/030/040/060 do support an FPU, either as a coprocessor
209    (68881/2) or built-in (68040/68060).  That's why System V release 4
210    (SVR4) instroduces a new calling convention specified by the SVR4
211    psABI.  Integer values are returned in %d0/%d1, pointer return
212    values in %a0 and floating values in %fp0.  When calling functions
213    returning a structure the caller should pass a pointer to a buffer
214    for the return value in %a0.  This convention is implemented in the
215    function m68k_svr4_return_value, and by appropriately setting the
216    struct_value_regnum member of `struct gdbarch_tdep'.
217
218    GNU/Linux returns values in the same way as SVR4 does, but uses %a1
219    for passing the structure return value buffer.
220
221    GCC can also generate code where small structures are returned in
222    %d0/%d1 instead of in memory by using -freg-struct-return.  This is
223    the default on NetBSD a.out, OpenBSD and GNU/Linux and several
224    embedded systems.  This convention is implemented by setting the
225    struct_return member of `struct gdbarch_tdep' to reg_struct_return.  */
226
227 /* Read a function return value of TYPE from REGCACHE, and copy that
228    into VALBUF.  */
229
230 static void
231 m68k_extract_return_value (struct type *type, struct regcache *regcache,
232                            gdb_byte *valbuf)
233 {
234   int len = TYPE_LENGTH (type);
235   gdb_byte buf[M68K_MAX_REGISTER_SIZE];
236
237   if (len <= 4)
238     {
239       regcache_raw_read (regcache, M68K_D0_REGNUM, buf);
240       memcpy (valbuf, buf + (4 - len), len);
241     }
242   else if (len <= 8)
243     {
244       regcache_raw_read (regcache, M68K_D0_REGNUM, buf);
245       memcpy (valbuf, buf + (8 - len), len - 4);
246       regcache_raw_read (regcache, M68K_D1_REGNUM, valbuf + (len - 4));
247     }
248   else
249     internal_error (__FILE__, __LINE__,
250                     _("Cannot extract return value of %d bytes long."), len);
251 }
252
253 static void
254 m68k_svr4_extract_return_value (struct type *type, struct regcache *regcache,
255                                 gdb_byte *valbuf)
256 {
257   int len = TYPE_LENGTH (type);
258   gdb_byte buf[M68K_MAX_REGISTER_SIZE];
259   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
260
261   if (tdep->float_return && TYPE_CODE (type) == TYPE_CODE_FLT)
262     {
263       struct type *fpreg_type = register_type 
264         (current_gdbarch, M68K_FP0_REGNUM);
265       regcache_raw_read (regcache, M68K_FP0_REGNUM, buf);
266       convert_typed_floating (buf, fpreg_type, valbuf, type);
267     }
268   else if (TYPE_CODE (type) == TYPE_CODE_PTR && len == 4)
269     regcache_raw_read (regcache, M68K_A0_REGNUM, valbuf);
270   else
271     m68k_extract_return_value (type, regcache, valbuf);
272 }
273
274 /* Write a function return value of TYPE from VALBUF into REGCACHE.  */
275
276 static void
277 m68k_store_return_value (struct type *type, struct regcache *regcache,
278                          const gdb_byte *valbuf)
279 {
280   int len = TYPE_LENGTH (type);
281
282   if (len <= 4)
283     regcache_raw_write_part (regcache, M68K_D0_REGNUM, 4 - len, len, valbuf);
284   else if (len <= 8)
285     {
286       regcache_raw_write_part (regcache, M68K_D0_REGNUM, 8 - len,
287                                len - 4, valbuf);
288       regcache_raw_write (regcache, M68K_D1_REGNUM, valbuf + (len - 4));
289     }
290   else
291     internal_error (__FILE__, __LINE__,
292                     _("Cannot store return value of %d bytes long."), len);
293 }
294
295 static void
296 m68k_svr4_store_return_value (struct type *type, struct regcache *regcache,
297                               const gdb_byte *valbuf)
298 {
299   int len = TYPE_LENGTH (type);
300   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
301
302   if (tdep->float_return && TYPE_CODE (type) == TYPE_CODE_FLT)
303     {
304       struct type *fpreg_type = register_type 
305         (current_gdbarch, M68K_FP0_REGNUM);
306       gdb_byte buf[M68K_MAX_REGISTER_SIZE];
307       convert_typed_floating (valbuf, type, buf, fpreg_type);
308       regcache_raw_write (regcache, M68K_FP0_REGNUM, buf);
309     }
310   else if (TYPE_CODE (type) == TYPE_CODE_PTR && len == 4)
311     {
312       regcache_raw_write (regcache, M68K_A0_REGNUM, valbuf);
313       regcache_raw_write (regcache, M68K_D0_REGNUM, valbuf);
314     }
315   else
316     m68k_store_return_value (type, regcache, valbuf);
317 }
318
319 /* Return non-zero if TYPE, which is assumed to be a structure or
320    union type, should be returned in registers for architecture
321    GDBARCH.  */
322
323 static int
324 m68k_reg_struct_return_p (struct gdbarch *gdbarch, struct type *type)
325 {
326   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
327   enum type_code code = TYPE_CODE (type);
328   int len = TYPE_LENGTH (type);
329
330   gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION);
331
332   if (tdep->struct_return == pcc_struct_return)
333     return 0;
334
335   return (len == 1 || len == 2 || len == 4 || len == 8);
336 }
337
338 /* Determine, for architecture GDBARCH, how a return value of TYPE
339    should be returned.  If it is supposed to be returned in registers,
340    and READBUF is non-zero, read the appropriate value from REGCACHE,
341    and copy it into READBUF.  If WRITEBUF is non-zero, write the value
342    from WRITEBUF into REGCACHE.  */
343
344 static enum return_value_convention
345 m68k_return_value (struct gdbarch *gdbarch, struct type *type,
346                    struct regcache *regcache, gdb_byte *readbuf,
347                    const gdb_byte *writebuf)
348 {
349   enum type_code code = TYPE_CODE (type);
350
351   /* GCC returns a `long double' in memory too.  */
352   if (((code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION)
353        && !m68k_reg_struct_return_p (gdbarch, type))
354       || (code == TYPE_CODE_FLT && TYPE_LENGTH (type) == 12))
355     {
356       /* The default on m68k is to return structures in static memory.
357          Consequently a function must return the address where we can
358          find the return value.  */
359
360       if (readbuf)
361         {
362           ULONGEST addr;
363
364           regcache_raw_read_unsigned (regcache, M68K_D0_REGNUM, &addr);
365           read_memory (addr, readbuf, TYPE_LENGTH (type));
366         }
367
368       return RETURN_VALUE_ABI_RETURNS_ADDRESS;
369     }
370
371   if (readbuf)
372     m68k_extract_return_value (type, regcache, readbuf);
373   if (writebuf)
374     m68k_store_return_value (type, regcache, writebuf);
375
376   return RETURN_VALUE_REGISTER_CONVENTION;
377 }
378
379 static enum return_value_convention
380 m68k_svr4_return_value (struct gdbarch *gdbarch, struct type *type,
381                         struct regcache *regcache, gdb_byte *readbuf,
382                         const gdb_byte *writebuf)
383 {
384   enum type_code code = TYPE_CODE (type);
385
386   if ((code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION)
387       && !m68k_reg_struct_return_p (gdbarch, type))
388     {
389       /* The System V ABI says that:
390
391          "A function returning a structure or union also sets %a0 to
392          the value it finds in %a0.  Thus when the caller receives
393          control again, the address of the returned object resides in
394          register %a0."
395
396          So the ABI guarantees that we can always find the return
397          value just after the function has returned.  */
398
399       if (readbuf)
400         {
401           ULONGEST addr;
402
403           regcache_raw_read_unsigned (regcache, M68K_A0_REGNUM, &addr);
404           read_memory (addr, readbuf, TYPE_LENGTH (type));
405         }
406
407       return RETURN_VALUE_ABI_RETURNS_ADDRESS;
408     }
409
410   /* This special case is for structures consisting of a single
411      `float' or `double' member.  These structures are returned in
412      %fp0.  For these structures, we call ourselves recursively,
413      changing TYPE into the type of the first member of the structure.
414      Since that should work for all structures that have only one
415      member, we don't bother to check the member's type here.  */
416   if (code == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1)
417     {
418       type = check_typedef (TYPE_FIELD_TYPE (type, 0));
419       return m68k_svr4_return_value (gdbarch, type, regcache,
420                                      readbuf, writebuf);
421     }
422
423   if (readbuf)
424     m68k_svr4_extract_return_value (type, regcache, readbuf);
425   if (writebuf)
426     m68k_svr4_store_return_value (type, regcache, writebuf);
427
428   return RETURN_VALUE_REGISTER_CONVENTION;
429 }
430 \f
431
432 /* Always align the frame to a 4-byte boundary.  This is required on
433    coldfire and harmless on the rest.  */
434
435 static CORE_ADDR
436 m68k_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
437 {
438   /* Align the stack to four bytes.  */
439   return sp & ~3;
440 }
441
442 static CORE_ADDR
443 m68k_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
444                       struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
445                       struct value **args, CORE_ADDR sp, int struct_return,
446                       CORE_ADDR struct_addr)
447 {
448   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
449   gdb_byte buf[4];
450   int i;
451
452   /* Push arguments in reverse order.  */
453   for (i = nargs - 1; i >= 0; i--)
454     {
455       struct type *value_type = value_enclosing_type (args[i]);
456       int len = TYPE_LENGTH (value_type);
457       int container_len = (len + 3) & ~3;
458       int offset;
459
460       /* Non-scalars bigger than 4 bytes are left aligned, others are
461          right aligned.  */
462       if ((TYPE_CODE (value_type) == TYPE_CODE_STRUCT
463            || TYPE_CODE (value_type) == TYPE_CODE_UNION
464            || TYPE_CODE (value_type) == TYPE_CODE_ARRAY)
465           && len > 4)
466         offset = 0;
467       else
468         offset = container_len - len;
469       sp -= container_len;
470       write_memory (sp + offset, value_contents_all (args[i]), len);
471     }
472
473   /* Store struct value address.  */
474   if (struct_return)
475     {
476       store_unsigned_integer (buf, 4, struct_addr);
477       regcache_cooked_write (regcache, tdep->struct_value_regnum, buf);
478     }
479
480   /* Store return address.  */
481   sp -= 4;
482   store_unsigned_integer (buf, 4, bp_addr);
483   write_memory (sp, buf, 4);
484
485   /* Finally, update the stack pointer...  */
486   store_unsigned_integer (buf, 4, sp);
487   regcache_cooked_write (regcache, M68K_SP_REGNUM, buf);
488
489   /* ...and fake a frame pointer.  */
490   regcache_cooked_write (regcache, M68K_FP_REGNUM, buf);
491
492   /* DWARF2/GCC uses the stack address *before* the function call as a
493      frame's CFA.  */
494   return sp + 8;
495 }
496
497 /* Convert a dwarf or dwarf2 regnumber to a GDB regnum.  */
498
499 static int
500 m68k_dwarf_reg_to_regnum (int num)
501 {
502   if (num < 8)
503     /* d0..7 */
504     return (num - 0) + M68K_D0_REGNUM;
505   else if (num < 16)
506     /* a0..7 */
507     return (num - 8) + M68K_A0_REGNUM;
508   else if (num < 24 && gdbarch_tdep (current_gdbarch)->fpregs_present)
509     /* fp0..7 */
510     return (num - 16) + M68K_FP0_REGNUM;
511   else if (num == 25)
512     /* pc */
513     return M68K_PC_REGNUM;
514   else
515     return gdbarch_num_regs (current_gdbarch)
516            + gdbarch_num_pseudo_regs (current_gdbarch);
517 }
518
519 \f
520 struct m68k_frame_cache
521 {
522   /* Base address.  */
523   CORE_ADDR base;
524   CORE_ADDR sp_offset;
525   CORE_ADDR pc;
526
527   /* Saved registers.  */
528   CORE_ADDR saved_regs[M68K_NUM_REGS];
529   CORE_ADDR saved_sp;
530
531   /* Stack space reserved for local variables.  */
532   long locals;
533 };
534
535 /* Allocate and initialize a frame cache.  */
536
537 static struct m68k_frame_cache *
538 m68k_alloc_frame_cache (void)
539 {
540   struct m68k_frame_cache *cache;
541   int i;
542
543   cache = FRAME_OBSTACK_ZALLOC (struct m68k_frame_cache);
544
545   /* Base address.  */
546   cache->base = 0;
547   cache->sp_offset = -4;
548   cache->pc = 0;
549
550   /* Saved registers.  We initialize these to -1 since zero is a valid
551      offset (that's where %fp is supposed to be stored).  */
552   for (i = 0; i < M68K_NUM_REGS; i++)
553     cache->saved_regs[i] = -1;
554
555   /* Frameless until proven otherwise.  */
556   cache->locals = -1;
557
558   return cache;
559 }
560
561 /* Check whether PC points at a code that sets up a new stack frame.
562    If so, it updates CACHE and returns the address of the first
563    instruction after the sequence that sets removes the "hidden"
564    argument from the stack or CURRENT_PC, whichever is smaller.
565    Otherwise, return PC.  */
566
567 static CORE_ADDR
568 m68k_analyze_frame_setup (CORE_ADDR pc, CORE_ADDR current_pc,
569                           struct m68k_frame_cache *cache)
570 {
571   int op;
572
573   if (pc >= current_pc)
574     return current_pc;
575
576   op = read_memory_unsigned_integer (pc, 2);
577
578   if (op == P_LINKW_FP || op == P_LINKL_FP || op == P_PEA_FP)
579     {
580       cache->saved_regs[M68K_FP_REGNUM] = 0;
581       cache->sp_offset += 4;
582       if (op == P_LINKW_FP)
583         {
584           /* link.w %fp, #-N */
585           /* link.w %fp, #0; adda.l #-N, %sp */
586           cache->locals = -read_memory_integer (pc + 2, 2);
587
588           if (pc + 4 < current_pc && cache->locals == 0)
589             {
590               op = read_memory_unsigned_integer (pc + 4, 2);
591               if (op == P_ADDAL_SP)
592                 {
593                   cache->locals = read_memory_integer (pc + 6, 4);
594                   return pc + 10;
595                 }
596             }
597
598           return pc + 4;
599         }
600       else if (op == P_LINKL_FP)
601         {
602           /* link.l %fp, #-N */
603           cache->locals = -read_memory_integer (pc + 2, 4);
604           return pc + 6;
605         }
606       else
607         {
608           /* pea (%fp); movea.l %sp, %fp */
609           cache->locals = 0;
610
611           if (pc + 2 < current_pc)
612             {
613               op = read_memory_unsigned_integer (pc + 2, 2);
614
615               if (op == P_MOVEAL_SP_FP)
616                 {
617                   /* move.l %sp, %fp */
618                   return pc + 4;
619                 }
620             }
621
622           return pc + 2;
623         }
624     }
625   else if ((op & 0170777) == P_SUBQW_SP || (op & 0170777) == P_SUBQL_SP)
626     {
627       /* subq.[wl] #N,%sp */
628       /* subq.[wl] #8,%sp; subq.[wl] #N,%sp */
629       cache->locals = (op & 07000) == 0 ? 8 : (op & 07000) >> 9;
630       if (pc + 2 < current_pc)
631         {
632           op = read_memory_unsigned_integer (pc + 2, 2);
633           if ((op & 0170777) == P_SUBQW_SP || (op & 0170777) == P_SUBQL_SP)
634             {
635               cache->locals += (op & 07000) == 0 ? 8 : (op & 07000) >> 9;
636               return pc + 4;
637             }
638         }
639       return pc + 2;
640     }
641   else if (op == P_ADDAW_SP || op == P_LEA_SP_SP)
642     {
643       /* adda.w #-N,%sp */
644       /* lea (-N,%sp),%sp */
645       cache->locals = -read_memory_integer (pc + 2, 2);
646       return pc + 4;
647     }
648   else if (op == P_ADDAL_SP)
649     {
650       /* adda.l #-N,%sp */
651       cache->locals = -read_memory_integer (pc + 2, 4);
652       return pc + 6;
653     }
654
655   return pc;
656 }
657
658 /* Check whether PC points at code that saves registers on the stack.
659    If so, it updates CACHE and returns the address of the first
660    instruction after the register saves or CURRENT_PC, whichever is
661    smaller.  Otherwise, return PC.  */
662
663 static CORE_ADDR
664 m68k_analyze_register_saves (CORE_ADDR pc, CORE_ADDR current_pc,
665                              struct m68k_frame_cache *cache)
666 {
667   if (cache->locals >= 0)
668     {
669       CORE_ADDR offset;
670       int op;
671       int i, mask, regno;
672
673       offset = -4 - cache->locals;
674       while (pc < current_pc)
675         {
676           op = read_memory_unsigned_integer (pc, 2);
677           if (op == P_FMOVEMX_SP
678               && gdbarch_tdep (current_gdbarch)->fpregs_present)
679             {
680               /* fmovem.x REGS,-(%sp) */
681               op = read_memory_unsigned_integer (pc + 2, 2);
682               if ((op & 0xff00) == 0xe000)
683                 {
684                   mask = op & 0xff;
685                   for (i = 0; i < 16; i++, mask >>= 1)
686                     {
687                       if (mask & 1)
688                         {
689                           cache->saved_regs[i + M68K_FP0_REGNUM] = offset;
690                           offset -= 12;
691                         }
692                     }
693                   pc += 4;
694                 }
695               else
696                 break;
697             }
698           else if ((op & 0177760) == P_MOVEL_SP)
699             {
700               /* move.l %R,-(%sp) */
701               regno = op & 017;
702               cache->saved_regs[regno] = offset;
703               offset -= 4;
704               pc += 2;
705             }
706           else if (op == P_MOVEML_SP)
707             {
708               /* movem.l REGS,-(%sp) */
709               mask = read_memory_unsigned_integer (pc + 2, 2);
710               for (i = 0; i < 16; i++, mask >>= 1)
711                 {
712                   if (mask & 1)
713                     {
714                       cache->saved_regs[15 - i] = offset;
715                       offset -= 4;
716                     }
717                 }
718               pc += 4;
719             }
720           else
721             break;
722         }
723     }
724
725   return pc;
726 }
727
728
729 /* Do a full analysis of the prologue at PC and update CACHE
730    accordingly.  Bail out early if CURRENT_PC is reached.  Return the
731    address where the analysis stopped.
732
733    We handle all cases that can be generated by gcc.
734
735    For allocating a stack frame:
736
737    link.w %a6,#-N
738    link.l %a6,#-N
739    pea (%fp); move.l %sp,%fp
740    link.w %a6,#0; add.l #-N,%sp
741    subq.l #N,%sp
742    subq.w #N,%sp
743    subq.w #8,%sp; subq.w #N-8,%sp
744    add.w #-N,%sp
745    lea (-N,%sp),%sp
746    add.l #-N,%sp
747
748    For saving registers:
749
750    fmovem.x REGS,-(%sp)
751    move.l R1,-(%sp)
752    move.l R1,-(%sp); move.l R2,-(%sp)
753    movem.l REGS,-(%sp)
754
755    For setting up the PIC register:
756
757    lea (%pc,N),%a5
758
759    */
760
761 static CORE_ADDR
762 m68k_analyze_prologue (CORE_ADDR pc, CORE_ADDR current_pc,
763                        struct m68k_frame_cache *cache)
764 {
765   unsigned int op;
766
767   pc = m68k_analyze_frame_setup (pc, current_pc, cache);
768   pc = m68k_analyze_register_saves (pc, current_pc, cache);
769   if (pc >= current_pc)
770     return current_pc;
771
772   /* Check for GOT setup.  */
773   op = read_memory_unsigned_integer (pc, 4);
774   if (op == P_LEA_PC_A5)
775     {
776       /* lea (%pc,N),%a5 */
777       return pc + 6;
778     }
779
780   return pc;
781 }
782
783 /* Return PC of first real instruction.  */
784
785 static CORE_ADDR
786 m68k_skip_prologue (CORE_ADDR start_pc)
787 {
788   struct m68k_frame_cache cache;
789   CORE_ADDR pc;
790   int op;
791
792   cache.locals = -1;
793   pc = m68k_analyze_prologue (start_pc, (CORE_ADDR) -1, &cache);
794   if (cache.locals < 0)
795     return start_pc;
796   return pc;
797 }
798
799 static CORE_ADDR
800 m68k_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
801 {
802   gdb_byte buf[8];
803
804   frame_unwind_register (next_frame, PC_REGNUM, buf);
805   return extract_typed_address (buf, builtin_type_void_func_ptr);
806 }
807 \f
808 /* Normal frames.  */
809
810 static struct m68k_frame_cache *
811 m68k_frame_cache (struct frame_info *next_frame, void **this_cache)
812 {
813   struct m68k_frame_cache *cache;
814   gdb_byte buf[4];
815   int i;
816
817   if (*this_cache)
818     return *this_cache;
819
820   cache = m68k_alloc_frame_cache ();
821   *this_cache = cache;
822
823   /* In principle, for normal frames, %fp holds the frame pointer,
824      which holds the base address for the current stack frame.
825      However, for functions that don't need it, the frame pointer is
826      optional.  For these "frameless" functions the frame pointer is
827      actually the frame pointer of the calling frame.  Signal
828      trampolines are just a special case of a "frameless" function.
829      They (usually) share their frame pointer with the frame that was
830      in progress when the signal occurred.  */
831
832   frame_unwind_register (next_frame, M68K_FP_REGNUM, buf);
833   cache->base = extract_unsigned_integer (buf, 4);
834   if (cache->base == 0)
835     return cache;
836
837   /* For normal frames, %pc is stored at 4(%fp).  */
838   cache->saved_regs[M68K_PC_REGNUM] = 4;
839
840   cache->pc = frame_func_unwind (next_frame, NORMAL_FRAME);
841   if (cache->pc != 0)
842     m68k_analyze_prologue (cache->pc, frame_pc_unwind (next_frame), cache);
843
844   if (cache->locals < 0)
845     {
846       /* We didn't find a valid frame, which means that CACHE->base
847          currently holds the frame pointer for our calling frame.  If
848          we're at the start of a function, or somewhere half-way its
849          prologue, the function's frame probably hasn't been fully
850          setup yet.  Try to reconstruct the base address for the stack
851          frame by looking at the stack pointer.  For truly "frameless"
852          functions this might work too.  */
853
854       frame_unwind_register (next_frame, M68K_SP_REGNUM, buf);
855       cache->base = extract_unsigned_integer (buf, 4) + cache->sp_offset;
856     }
857
858   /* Now that we have the base address for the stack frame we can
859      calculate the value of %sp in the calling frame.  */
860   cache->saved_sp = cache->base + 8;
861
862   /* Adjust all the saved registers such that they contain addresses
863      instead of offsets.  */
864   for (i = 0; i < M68K_NUM_REGS; i++)
865     if (cache->saved_regs[i] != -1)
866       cache->saved_regs[i] += cache->base;
867
868   return cache;
869 }
870
871 static void
872 m68k_frame_this_id (struct frame_info *next_frame, void **this_cache,
873                     struct frame_id *this_id)
874 {
875   struct m68k_frame_cache *cache = m68k_frame_cache (next_frame, this_cache);
876
877   /* This marks the outermost frame.  */
878   if (cache->base == 0)
879     return;
880
881   /* See the end of m68k_push_dummy_call.  */
882   *this_id = frame_id_build (cache->base + 8, cache->pc);
883 }
884
885 static void
886 m68k_frame_prev_register (struct frame_info *next_frame, void **this_cache,
887                           int regnum, int *optimizedp,
888                           enum lval_type *lvalp, CORE_ADDR *addrp,
889                           int *realnump, gdb_byte *valuep)
890 {
891   struct m68k_frame_cache *cache = m68k_frame_cache (next_frame, this_cache);
892
893   gdb_assert (regnum >= 0);
894
895   if (regnum == M68K_SP_REGNUM && cache->saved_sp)
896     {
897       *optimizedp = 0;
898       *lvalp = not_lval;
899       *addrp = 0;
900       *realnump = -1;
901       if (valuep)
902         {
903           /* Store the value.  */
904           store_unsigned_integer (valuep, 4, cache->saved_sp);
905         }
906       return;
907     }
908
909   if (regnum < M68K_NUM_REGS && cache->saved_regs[regnum] != -1)
910     {
911       *optimizedp = 0;
912       *lvalp = lval_memory;
913       *addrp = cache->saved_regs[regnum];
914       *realnump = -1;
915       if (valuep)
916         {
917           /* Read the value in from memory.  */
918           read_memory (*addrp, valuep,
919                        register_size (current_gdbarch, regnum));
920         }
921       return;
922     }
923
924   *optimizedp = 0;
925   *lvalp = lval_register;
926   *addrp = 0;
927   *realnump = regnum;
928   if (valuep)
929     frame_unwind_register (next_frame, (*realnump), valuep);
930 }
931
932 static const struct frame_unwind m68k_frame_unwind =
933 {
934   NORMAL_FRAME,
935   m68k_frame_this_id,
936   m68k_frame_prev_register
937 };
938
939 static const struct frame_unwind *
940 m68k_frame_sniffer (struct frame_info *next_frame)
941 {
942   return &m68k_frame_unwind;
943 }
944 \f
945 static CORE_ADDR
946 m68k_frame_base_address (struct frame_info *next_frame, void **this_cache)
947 {
948   struct m68k_frame_cache *cache = m68k_frame_cache (next_frame, this_cache);
949
950   return cache->base;
951 }
952
953 static const struct frame_base m68k_frame_base =
954 {
955   &m68k_frame_unwind,
956   m68k_frame_base_address,
957   m68k_frame_base_address,
958   m68k_frame_base_address
959 };
960
961 static struct frame_id
962 m68k_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
963 {
964   gdb_byte buf[4];
965   CORE_ADDR fp;
966
967   frame_unwind_register (next_frame, M68K_FP_REGNUM, buf);
968   fp = extract_unsigned_integer (buf, 4);
969
970   /* See the end of m68k_push_dummy_call.  */
971   return frame_id_build (fp + 8, frame_pc_unwind (next_frame));
972 }
973 \f
974
975 /* Figure out where the longjmp will land.  Slurp the args out of the stack.
976    We expect the first arg to be a pointer to the jmp_buf structure from which
977    we extract the pc (JB_PC) that we will land at.  The pc is copied into PC.
978    This routine returns true on success. */
979
980 static int
981 m68k_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
982 {
983   gdb_byte *buf;
984   CORE_ADDR sp, jb_addr;
985   struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (frame));
986
987   if (tdep->jb_pc < 0)
988     {
989       internal_error (__FILE__, __LINE__,
990                       _("m68k_get_longjmp_target: not implemented"));
991       return 0;
992     }
993
994   buf = alloca (gdbarch_ptr_bit (current_gdbarch) / TARGET_CHAR_BIT);
995   sp = get_frame_register_unsigned (frame, SP_REGNUM);
996
997   if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack */
998                           buf,
999                           gdbarch_ptr_bit (current_gdbarch) / TARGET_CHAR_BIT))
1000     return 0;
1001
1002   jb_addr = extract_unsigned_integer (buf, gdbarch_ptr_bit (current_gdbarch)
1003                                              / TARGET_CHAR_BIT);
1004
1005   if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
1006                           gdbarch_ptr_bit (current_gdbarch) / TARGET_CHAR_BIT))
1007     return 0;
1008
1009   *pc = extract_unsigned_integer (buf, gdbarch_ptr_bit (current_gdbarch)
1010                                          / TARGET_CHAR_BIT);
1011   return 1;
1012 }
1013 \f
1014
1015 /* System V Release 4 (SVR4).  */
1016
1017 void
1018 m68k_svr4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1019 {
1020   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1021
1022   /* SVR4 uses a different calling convention.  */
1023   set_gdbarch_return_value (gdbarch, m68k_svr4_return_value);
1024
1025   /* SVR4 uses %a0 instead of %a1.  */
1026   tdep->struct_value_regnum = M68K_A0_REGNUM;
1027 }
1028 \f
1029
1030 /* Function: m68k_gdbarch_init
1031    Initializer function for the m68k gdbarch vector.
1032    Called by gdbarch.  Sets up the gdbarch vector(s) for this target. */
1033
1034 static struct gdbarch *
1035 m68k_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1036 {
1037   struct gdbarch_tdep *tdep = NULL;
1038   struct gdbarch *gdbarch;
1039   struct gdbarch_list *best_arch;
1040   struct tdesc_arch_data *tdesc_data = NULL;
1041   int i;
1042   enum m68k_flavour flavour = m68k_no_flavour;
1043   int has_fp = 1;
1044   const struct floatformat **long_double_format = floatformats_m68881_ext;
1045
1046   /* Check any target description for validity.  */
1047   if (tdesc_has_registers (info.target_desc))
1048     {
1049       const struct tdesc_feature *feature;
1050       int valid_p;
1051
1052       feature = tdesc_find_feature (info.target_desc,
1053                                     "org.gnu.gdb.m68k.core");
1054       if (feature != NULL)
1055         /* Do nothing.  */
1056         ;
1057
1058       if (feature == NULL)
1059         {
1060           feature = tdesc_find_feature (info.target_desc,
1061                                         "org.gnu.gdb.coldfire.core");
1062           if (feature != NULL)
1063             flavour = m68k_coldfire_flavour;
1064         }
1065
1066       if (feature == NULL)
1067         {
1068           feature = tdesc_find_feature (info.target_desc,
1069                                         "org.gnu.gdb.fido.core");
1070           if (feature != NULL)
1071             flavour = m68k_fido_flavour;
1072         }
1073
1074       if (feature == NULL)
1075         return NULL;
1076
1077       tdesc_data = tdesc_data_alloc ();
1078
1079       valid_p = 1;
1080       for (i = 0; i <= M68K_PC_REGNUM; i++)
1081         valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
1082                                             m68k_register_names[i]);
1083
1084       if (!valid_p)
1085         {
1086           tdesc_data_cleanup (tdesc_data);
1087           return NULL;
1088         }
1089
1090       feature = tdesc_find_feature (info.target_desc,
1091                                     "org.gnu.gdb.coldfire.fp");
1092       if (feature != NULL)
1093         {
1094           valid_p = 1;
1095           for (i = M68K_FP0_REGNUM; i <= M68K_FPI_REGNUM; i++)
1096             valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
1097                                                 m68k_register_names[i]);
1098           if (!valid_p)
1099             {
1100               tdesc_data_cleanup (tdesc_data);
1101               return NULL;
1102             }
1103         }
1104       else
1105         has_fp = 0;
1106     }
1107
1108   /* The mechanism for returning floating values from function
1109      and the type of long double depend on whether we're
1110      on ColdFire or standard m68k. */
1111
1112   if (info.bfd_arch_info)
1113     {
1114       const bfd_arch_info_type *coldfire_arch = 
1115         bfd_lookup_arch (bfd_arch_m68k, bfd_mach_mcf_isa_a_nodiv);
1116
1117       if (coldfire_arch
1118           && (*info.bfd_arch_info->compatible) 
1119           (info.bfd_arch_info, coldfire_arch))
1120         flavour = m68k_coldfire_flavour;
1121     }
1122   
1123   /* If there is already a candidate, use it.  */
1124   for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
1125        best_arch != NULL;
1126        best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
1127     {
1128       if (flavour != gdbarch_tdep (best_arch->gdbarch)->flavour)
1129         continue;
1130
1131       if (has_fp != gdbarch_tdep (best_arch->gdbarch)->fpregs_present)
1132         continue;
1133
1134       break;
1135     }
1136
1137   tdep = xmalloc (sizeof (struct gdbarch_tdep));
1138   gdbarch = gdbarch_alloc (&info, tdep);
1139   tdep->fpregs_present = has_fp;
1140   tdep->flavour = flavour;
1141
1142   if (flavour == m68k_coldfire_flavour || flavour == m68k_fido_flavour)
1143     long_double_format = floatformats_ieee_double;
1144   set_gdbarch_long_double_format (gdbarch, long_double_format);
1145   set_gdbarch_long_double_bit (gdbarch, long_double_format[0]->totalsize);
1146
1147   set_gdbarch_skip_prologue (gdbarch, m68k_skip_prologue);
1148   set_gdbarch_breakpoint_from_pc (gdbarch, m68k_local_breakpoint_from_pc);
1149
1150   /* Stack grows down. */
1151   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1152   set_gdbarch_frame_align (gdbarch, m68k_frame_align);
1153
1154   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
1155   if (flavour == m68k_coldfire_flavour || flavour == m68k_fido_flavour)
1156     set_gdbarch_decr_pc_after_break (gdbarch, 2);
1157
1158   set_gdbarch_frame_args_skip (gdbarch, 8);
1159   set_gdbarch_dwarf_reg_to_regnum (gdbarch, m68k_dwarf_reg_to_regnum);
1160   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, m68k_dwarf_reg_to_regnum);
1161
1162   set_gdbarch_register_type (gdbarch, m68k_register_type);
1163   set_gdbarch_register_name (gdbarch, m68k_register_name);
1164   set_gdbarch_num_regs (gdbarch, M68K_NUM_REGS);
1165   set_gdbarch_sp_regnum (gdbarch, M68K_SP_REGNUM);
1166   set_gdbarch_pc_regnum (gdbarch, M68K_PC_REGNUM);
1167   set_gdbarch_ps_regnum (gdbarch, M68K_PS_REGNUM);
1168   set_gdbarch_fp0_regnum (gdbarch, M68K_FP0_REGNUM);
1169   set_gdbarch_convert_register_p (gdbarch, m68k_convert_register_p);
1170   set_gdbarch_register_to_value (gdbarch,  m68k_register_to_value);
1171   set_gdbarch_value_to_register (gdbarch, m68k_value_to_register);
1172
1173   if (has_fp)
1174     set_gdbarch_fp0_regnum (gdbarch, M68K_FP0_REGNUM);
1175
1176   /* Try to figure out if the arch uses floating registers to return
1177      floating point values from functions.  */
1178   if (has_fp)
1179     {
1180       /* On ColdFire, floating point values are returned in D0.  */
1181       if (flavour == m68k_coldfire_flavour)
1182         tdep->float_return = 0;
1183       else
1184         tdep->float_return = 1;
1185     }
1186   else
1187     {
1188       /* No floating registers, so can't use them for returning values.  */
1189       tdep->float_return = 0;
1190     }
1191
1192   /* Function call & return */
1193   set_gdbarch_push_dummy_call (gdbarch, m68k_push_dummy_call);
1194   set_gdbarch_return_value (gdbarch, m68k_return_value);
1195
1196
1197   /* Disassembler.  */
1198   set_gdbarch_print_insn (gdbarch, print_insn_m68k);
1199
1200 #if defined JB_PC && defined JB_ELEMENT_SIZE
1201   tdep->jb_pc = JB_PC;
1202   tdep->jb_elt_size = JB_ELEMENT_SIZE;
1203 #else
1204   tdep->jb_pc = -1;
1205 #endif
1206   tdep->struct_value_regnum = M68K_A1_REGNUM;
1207   tdep->struct_return = reg_struct_return;
1208
1209   /* Frame unwinder.  */
1210   set_gdbarch_unwind_dummy_id (gdbarch, m68k_unwind_dummy_id);
1211   set_gdbarch_unwind_pc (gdbarch, m68k_unwind_pc);
1212
1213   /* Hook in the DWARF CFI frame unwinder.  */
1214   frame_unwind_append_sniffer (gdbarch, dwarf2_frame_sniffer);
1215
1216   frame_base_set_default (gdbarch, &m68k_frame_base);
1217
1218   /* Hook in ABI-specific overrides, if they have been registered.  */
1219   gdbarch_init_osabi (info, gdbarch);
1220
1221   /* Now we have tuned the configuration, set a few final things,
1222      based on what the OS ABI has told us.  */
1223
1224   if (tdep->jb_pc >= 0)
1225     set_gdbarch_get_longjmp_target (gdbarch, m68k_get_longjmp_target);
1226
1227   frame_unwind_append_sniffer (gdbarch, m68k_frame_sniffer);
1228
1229   if (tdesc_data)
1230     tdesc_use_registers (gdbarch, tdesc_data);
1231
1232   return gdbarch;
1233 }
1234
1235
1236 static void
1237 m68k_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file)
1238 {
1239   struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
1240
1241   if (tdep == NULL)
1242     return;
1243 }
1244
1245 extern initialize_file_ftype _initialize_m68k_tdep; /* -Wmissing-prototypes */
1246
1247 void
1248 _initialize_m68k_tdep (void)
1249 {
1250   gdbarch_register (bfd_arch_m68k, m68k_gdbarch_init, m68k_dump_tdep);
1251 }