OSDN Git Service

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