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