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