1 /* Target-dependent code for the Motorola 68000 series.
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
6 This file is part of GDB.
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.
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.
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. */
24 #include "dwarf2-frame.h"
26 #include "frame-base.h"
27 #include "frame-unwind.h"
32 #include "gdb_string.h"
33 #include "gdb_assert.h"
36 #include "arch-utils.h"
39 #include "target-descriptions.h"
41 #include "m68k-tdep.h"
44 #define P_LINKL_FP 0x480e
45 #define P_LINKW_FP 0x4e56
46 #define P_PEA_FP 0x4856
47 #define P_MOVEAL_SP_FP 0x2c4f
48 #define P_ADDAW_SP 0xdefc
49 #define P_ADDAL_SP 0xdffc
50 #define P_SUBQW_SP 0x514f
51 #define P_SUBQL_SP 0x518f
52 #define P_LEA_SP_SP 0x4fef
53 #define P_LEA_PC_A5 0x4bfb0170
54 #define P_FMOVEMX_SP 0xf227
55 #define P_MOVEL_SP 0x2f00
56 #define P_MOVEML_SP 0x48e7
58 /* Offset from SP to first arg on stack at first instruction of a function */
59 #define SP_ARG0 (1 * 4)
61 #if !defined (BPT_VECTOR)
62 #define BPT_VECTOR 0xf
65 static const gdb_byte
*
66 m68k_local_breakpoint_from_pc (CORE_ADDR
*pcptr
, int *lenptr
)
68 static gdb_byte break_insn
[] = {0x4e, (0x40 | BPT_VECTOR
)};
69 *lenptr
= sizeof (break_insn
);
73 /* Return the GDB type object for the "standard" data type of data in
74 register N. This should be int for D0-D7, SR, FPCONTROL and
75 FPSTATUS, long double for FP0-FP7, and void pointer for all others
76 (A0-A7, PC, FPIADDR). Note, for registers which contain
77 addresses return pointer to void, not pointer to char, because we
78 don't want to attempt to print the string after printing the
82 m68k_register_type (struct gdbarch
*gdbarch
, int regnum
)
84 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
86 if (tdep
->fpregs_present
)
88 if (regnum
>= FP0_REGNUM
&& regnum
<= FP0_REGNUM
+ 7)
90 if (tdep
->flavour
== m68k_coldfire_flavour
)
91 return builtin_type (gdbarch
)->builtin_double
;
93 return builtin_type_m68881_ext
;
96 if (regnum
== M68K_FPI_REGNUM
)
97 return builtin_type_void_func_ptr
;
99 if (regnum
== M68K_FPC_REGNUM
|| regnum
== M68K_FPS_REGNUM
)
100 return builtin_type_int32
;
104 if (regnum
>= M68K_FP0_REGNUM
&& regnum
<= M68K_FPI_REGNUM
)
105 return builtin_type_int0
;
108 if (regnum
== PC_REGNUM
)
109 return builtin_type_void_func_ptr
;
111 if (regnum
>= M68K_A0_REGNUM
&& regnum
<= M68K_A0_REGNUM
+ 7)
112 return builtin_type_void_data_ptr
;
114 return builtin_type_int32
;
117 static const char *m68k_register_names
[] = {
118 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
119 "a0", "a1", "a2", "a3", "a4", "a5", "fp", "sp",
121 "fp0", "fp1", "fp2", "fp3", "fp4", "fp5", "fp6", "fp7",
122 "fpcontrol", "fpstatus", "fpiaddr"
125 /* Function: m68k_register_name
126 Returns the name of the standard m68k register regnum. */
129 m68k_register_name (int regnum
)
131 if (regnum
< 0 || regnum
>= ARRAY_SIZE (m68k_register_names
))
132 internal_error (__FILE__
, __LINE__
,
133 _("m68k_register_name: illegal register number %d"), regnum
);
135 return m68k_register_names
[regnum
];
138 /* Return nonzero if a value of type TYPE stored in register REGNUM
139 needs any special handling. */
142 m68k_convert_register_p (int regnum
, struct type
*type
)
144 if (!gdbarch_tdep (current_gdbarch
)->fpregs_present
)
146 return (regnum
>= M68K_FP0_REGNUM
&& regnum
<= M68K_FP0_REGNUM
+ 7);
149 /* Read a value of type TYPE from register REGNUM in frame FRAME, and
150 return its contents in TO. */
153 m68k_register_to_value (struct frame_info
*frame
, int regnum
,
154 struct type
*type
, gdb_byte
*to
)
156 gdb_byte from
[M68K_MAX_REGISTER_SIZE
];
157 struct type
*fpreg_type
= register_type (current_gdbarch
, M68K_FP0_REGNUM
);
159 /* We only support floating-point values. */
160 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
162 warning (_("Cannot convert floating-point register value "
163 "to non-floating-point type."));
167 /* Convert to TYPE. This should be a no-op if TYPE is equivalent to
168 the extended floating-point format used by the FPU. */
169 get_frame_register (frame
, regnum
, from
);
170 convert_typed_floating (from
, fpreg_type
, to
, type
);
173 /* Write the contents FROM of a value of type TYPE into register
174 REGNUM in frame FRAME. */
177 m68k_value_to_register (struct frame_info
*frame
, int regnum
,
178 struct type
*type
, const gdb_byte
*from
)
180 gdb_byte to
[M68K_MAX_REGISTER_SIZE
];
181 struct type
*fpreg_type
= register_type (current_gdbarch
, M68K_FP0_REGNUM
);
183 /* We only support floating-point values. */
184 if (TYPE_CODE (type
) != TYPE_CODE_FLT
)
186 warning (_("Cannot convert non-floating-point type "
187 "to floating-point register value."));
191 /* Convert from TYPE. This should be a no-op if TYPE is equivalent
192 to the extended floating-point format used by the FPU. */
193 convert_typed_floating (from
, type
, to
, fpreg_type
);
194 put_frame_register (frame
, regnum
, to
);
198 /* There is a fair number of calling conventions that are in somewhat
199 wide use. The 68000/08/10 don't support an FPU, not even as a
200 coprocessor. All function return values are stored in %d0/%d1.
201 Structures are returned in a static buffer, a pointer to which is
202 returned in %d0. This means that functions returning a structure
203 are not re-entrant. To avoid this problem some systems use a
204 convention where the caller passes a pointer to a buffer in %a1
205 where the return values is to be stored. This convention is the
206 default, and is implemented in the function m68k_return_value.
208 The 68020/030/040/060 do support an FPU, either as a coprocessor
209 (68881/2) or built-in (68040/68060). That's why System V release 4
210 (SVR4) instroduces a new calling convention specified by the SVR4
211 psABI. Integer values are returned in %d0/%d1, pointer return
212 values in %a0 and floating values in %fp0. When calling functions
213 returning a structure the caller should pass a pointer to a buffer
214 for the return value in %a0. This convention is implemented in the
215 function m68k_svr4_return_value, and by appropriately setting the
216 struct_value_regnum member of `struct gdbarch_tdep'.
218 GNU/Linux returns values in the same way as SVR4 does, but uses %a1
219 for passing the structure return value buffer.
221 GCC can also generate code where small structures are returned in
222 %d0/%d1 instead of in memory by using -freg-struct-return. This is
223 the default on NetBSD a.out, OpenBSD and GNU/Linux and several
224 embedded systems. This convention is implemented by setting the
225 struct_return member of `struct gdbarch_tdep' to reg_struct_return. */
227 /* Read a function return value of TYPE from REGCACHE, and copy that
231 m68k_extract_return_value (struct type
*type
, struct regcache
*regcache
,
234 int len
= TYPE_LENGTH (type
);
235 gdb_byte buf
[M68K_MAX_REGISTER_SIZE
];
239 regcache_raw_read (regcache
, M68K_D0_REGNUM
, buf
);
240 memcpy (valbuf
, buf
+ (4 - len
), len
);
244 regcache_raw_read (regcache
, M68K_D0_REGNUM
, buf
);
245 memcpy (valbuf
, buf
+ (8 - len
), len
- 4);
246 regcache_raw_read (regcache
, M68K_D1_REGNUM
, valbuf
+ (len
- 4));
249 internal_error (__FILE__
, __LINE__
,
250 _("Cannot extract return value of %d bytes long."), len
);
254 m68k_svr4_extract_return_value (struct type
*type
, struct regcache
*regcache
,
257 int len
= TYPE_LENGTH (type
);
258 gdb_byte buf
[M68K_MAX_REGISTER_SIZE
];
259 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
261 if (tdep
->float_return
&& TYPE_CODE (type
) == TYPE_CODE_FLT
)
263 struct type
*fpreg_type
= register_type
264 (current_gdbarch
, M68K_FP0_REGNUM
);
265 regcache_raw_read (regcache
, M68K_FP0_REGNUM
, buf
);
266 convert_typed_floating (buf
, fpreg_type
, valbuf
, type
);
268 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
&& len
== 4)
269 regcache_raw_read (regcache
, M68K_A0_REGNUM
, valbuf
);
271 m68k_extract_return_value (type
, regcache
, valbuf
);
274 /* Write a function return value of TYPE from VALBUF into REGCACHE. */
277 m68k_store_return_value (struct type
*type
, struct regcache
*regcache
,
278 const gdb_byte
*valbuf
)
280 int len
= TYPE_LENGTH (type
);
283 regcache_raw_write_part (regcache
, M68K_D0_REGNUM
, 4 - len
, len
, valbuf
);
286 regcache_raw_write_part (regcache
, M68K_D0_REGNUM
, 8 - len
,
288 regcache_raw_write (regcache
, M68K_D1_REGNUM
, valbuf
+ (len
- 4));
291 internal_error (__FILE__
, __LINE__
,
292 _("Cannot store return value of %d bytes long."), len
);
296 m68k_svr4_store_return_value (struct type
*type
, struct regcache
*regcache
,
297 const gdb_byte
*valbuf
)
299 int len
= TYPE_LENGTH (type
);
300 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
302 if (tdep
->float_return
&& TYPE_CODE (type
) == TYPE_CODE_FLT
)
304 struct type
*fpreg_type
= register_type
305 (current_gdbarch
, M68K_FP0_REGNUM
);
306 gdb_byte buf
[M68K_MAX_REGISTER_SIZE
];
307 convert_typed_floating (valbuf
, type
, buf
, fpreg_type
);
308 regcache_raw_write (regcache
, M68K_FP0_REGNUM
, buf
);
310 else if (TYPE_CODE (type
) == TYPE_CODE_PTR
&& len
== 4)
312 regcache_raw_write (regcache
, M68K_A0_REGNUM
, valbuf
);
313 regcache_raw_write (regcache
, M68K_D0_REGNUM
, valbuf
);
316 m68k_store_return_value (type
, regcache
, valbuf
);
319 /* Return non-zero if TYPE, which is assumed to be a structure or
320 union type, should be returned in registers for architecture
324 m68k_reg_struct_return_p (struct gdbarch
*gdbarch
, struct type
*type
)
326 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
327 enum type_code code
= TYPE_CODE (type
);
328 int len
= TYPE_LENGTH (type
);
330 gdb_assert (code
== TYPE_CODE_STRUCT
|| code
== TYPE_CODE_UNION
);
332 if (tdep
->struct_return
== pcc_struct_return
)
335 return (len
== 1 || len
== 2 || len
== 4 || len
== 8);
338 /* Determine, for architecture GDBARCH, how a return value of TYPE
339 should be returned. If it is supposed to be returned in registers,
340 and READBUF is non-zero, read the appropriate value from REGCACHE,
341 and copy it into READBUF. If WRITEBUF is non-zero, write the value
342 from WRITEBUF into REGCACHE. */
344 static enum return_value_convention
345 m68k_return_value (struct gdbarch
*gdbarch
, struct type
*type
,
346 struct regcache
*regcache
, gdb_byte
*readbuf
,
347 const gdb_byte
*writebuf
)
349 enum type_code code
= TYPE_CODE (type
);
351 /* GCC returns a `long double' in memory too. */
352 if (((code
== TYPE_CODE_STRUCT
|| code
== TYPE_CODE_UNION
)
353 && !m68k_reg_struct_return_p (gdbarch
, type
))
354 || (code
== TYPE_CODE_FLT
&& TYPE_LENGTH (type
) == 12))
356 /* The default on m68k is to return structures in static memory.
357 Consequently a function must return the address where we can
358 find the return value. */
364 regcache_raw_read_unsigned (regcache
, M68K_D0_REGNUM
, &addr
);
365 read_memory (addr
, readbuf
, TYPE_LENGTH (type
));
368 return RETURN_VALUE_ABI_RETURNS_ADDRESS
;
372 m68k_extract_return_value (type
, regcache
, readbuf
);
374 m68k_store_return_value (type
, regcache
, writebuf
);
376 return RETURN_VALUE_REGISTER_CONVENTION
;
379 static enum return_value_convention
380 m68k_svr4_return_value (struct gdbarch
*gdbarch
, struct type
*type
,
381 struct regcache
*regcache
, gdb_byte
*readbuf
,
382 const gdb_byte
*writebuf
)
384 enum type_code code
= TYPE_CODE (type
);
386 if ((code
== TYPE_CODE_STRUCT
|| code
== TYPE_CODE_UNION
)
387 && !m68k_reg_struct_return_p (gdbarch
, type
))
389 /* The System V ABI says that:
391 "A function returning a structure or union also sets %a0 to
392 the value it finds in %a0. Thus when the caller receives
393 control again, the address of the returned object resides in
396 So the ABI guarantees that we can always find the return
397 value just after the function has returned. */
403 regcache_raw_read_unsigned (regcache
, M68K_A0_REGNUM
, &addr
);
404 read_memory (addr
, readbuf
, TYPE_LENGTH (type
));
407 return RETURN_VALUE_ABI_RETURNS_ADDRESS
;
410 /* This special case is for structures consisting of a single
411 `float' or `double' member. These structures are returned in
412 %fp0. For these structures, we call ourselves recursively,
413 changing TYPE into the type of the first member of the structure.
414 Since that should work for all structures that have only one
415 member, we don't bother to check the member's type here. */
416 if (code
== TYPE_CODE_STRUCT
&& TYPE_NFIELDS (type
) == 1)
418 type
= check_typedef (TYPE_FIELD_TYPE (type
, 0));
419 return m68k_svr4_return_value (gdbarch
, type
, regcache
,
424 m68k_svr4_extract_return_value (type
, regcache
, readbuf
);
426 m68k_svr4_store_return_value (type
, regcache
, writebuf
);
428 return RETURN_VALUE_REGISTER_CONVENTION
;
432 /* Always align the frame to a 4-byte boundary. This is required on
433 coldfire and harmless on the rest. */
436 m68k_frame_align (struct gdbarch
*gdbarch
, CORE_ADDR sp
)
438 /* Align the stack to four bytes. */
443 m68k_push_dummy_call (struct gdbarch
*gdbarch
, struct value
*function
,
444 struct regcache
*regcache
, CORE_ADDR bp_addr
, int nargs
,
445 struct value
**args
, CORE_ADDR sp
, int struct_return
,
446 CORE_ADDR struct_addr
)
448 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
452 /* Push arguments in reverse order. */
453 for (i
= nargs
- 1; i
>= 0; i
--)
455 struct type
*value_type
= value_enclosing_type (args
[i
]);
456 int len
= TYPE_LENGTH (value_type
);
457 int container_len
= (len
+ 3) & ~3;
460 /* Non-scalars bigger than 4 bytes are left aligned, others are
462 if ((TYPE_CODE (value_type
) == TYPE_CODE_STRUCT
463 || TYPE_CODE (value_type
) == TYPE_CODE_UNION
464 || TYPE_CODE (value_type
) == TYPE_CODE_ARRAY
)
468 offset
= container_len
- len
;
470 write_memory (sp
+ offset
, value_contents_all (args
[i
]), len
);
473 /* Store struct value address. */
476 store_unsigned_integer (buf
, 4, struct_addr
);
477 regcache_cooked_write (regcache
, tdep
->struct_value_regnum
, buf
);
480 /* Store return address. */
482 store_unsigned_integer (buf
, 4, bp_addr
);
483 write_memory (sp
, buf
, 4);
485 /* Finally, update the stack pointer... */
486 store_unsigned_integer (buf
, 4, sp
);
487 regcache_cooked_write (regcache
, M68K_SP_REGNUM
, buf
);
489 /* ...and fake a frame pointer. */
490 regcache_cooked_write (regcache
, M68K_FP_REGNUM
, buf
);
492 /* DWARF2/GCC uses the stack address *before* the function call as a
497 /* Convert a dwarf or dwarf2 regnumber to a GDB regnum. */
500 m68k_dwarf_reg_to_regnum (int num
)
504 return (num
- 0) + M68K_D0_REGNUM
;
507 return (num
- 8) + M68K_A0_REGNUM
;
508 else if (num
< 24 && gdbarch_tdep (current_gdbarch
)->fpregs_present
)
510 return (num
- 16) + M68K_FP0_REGNUM
;
513 return M68K_PC_REGNUM
;
515 return gdbarch_num_regs (current_gdbarch
)
516 + gdbarch_num_pseudo_regs (current_gdbarch
);
520 struct m68k_frame_cache
527 /* Saved registers. */
528 CORE_ADDR saved_regs
[M68K_NUM_REGS
];
531 /* Stack space reserved for local variables. */
535 /* Allocate and initialize a frame cache. */
537 static struct m68k_frame_cache
*
538 m68k_alloc_frame_cache (void)
540 struct m68k_frame_cache
*cache
;
543 cache
= FRAME_OBSTACK_ZALLOC (struct m68k_frame_cache
);
547 cache
->sp_offset
= -4;
550 /* Saved registers. We initialize these to -1 since zero is a valid
551 offset (that's where %fp is supposed to be stored). */
552 for (i
= 0; i
< M68K_NUM_REGS
; i
++)
553 cache
->saved_regs
[i
] = -1;
555 /* Frameless until proven otherwise. */
561 /* Check whether PC points at a code that sets up a new stack frame.
562 If so, it updates CACHE and returns the address of the first
563 instruction after the sequence that sets removes the "hidden"
564 argument from the stack or CURRENT_PC, whichever is smaller.
565 Otherwise, return PC. */
568 m68k_analyze_frame_setup (CORE_ADDR pc
, CORE_ADDR current_pc
,
569 struct m68k_frame_cache
*cache
)
573 if (pc
>= current_pc
)
576 op
= read_memory_unsigned_integer (pc
, 2);
578 if (op
== P_LINKW_FP
|| op
== P_LINKL_FP
|| op
== P_PEA_FP
)
580 cache
->saved_regs
[M68K_FP_REGNUM
] = 0;
581 cache
->sp_offset
+= 4;
582 if (op
== P_LINKW_FP
)
584 /* link.w %fp, #-N */
585 /* link.w %fp, #0; adda.l #-N, %sp */
586 cache
->locals
= -read_memory_integer (pc
+ 2, 2);
588 if (pc
+ 4 < current_pc
&& cache
->locals
== 0)
590 op
= read_memory_unsigned_integer (pc
+ 4, 2);
591 if (op
== P_ADDAL_SP
)
593 cache
->locals
= read_memory_integer (pc
+ 6, 4);
600 else if (op
== P_LINKL_FP
)
602 /* link.l %fp, #-N */
603 cache
->locals
= -read_memory_integer (pc
+ 2, 4);
608 /* pea (%fp); movea.l %sp, %fp */
611 if (pc
+ 2 < current_pc
)
613 op
= read_memory_unsigned_integer (pc
+ 2, 2);
615 if (op
== P_MOVEAL_SP_FP
)
617 /* move.l %sp, %fp */
625 else if ((op
& 0170777) == P_SUBQW_SP
|| (op
& 0170777) == P_SUBQL_SP
)
627 /* subq.[wl] #N,%sp */
628 /* subq.[wl] #8,%sp; subq.[wl] #N,%sp */
629 cache
->locals
= (op
& 07000) == 0 ? 8 : (op
& 07000) >> 9;
630 if (pc
+ 2 < current_pc
)
632 op
= read_memory_unsigned_integer (pc
+ 2, 2);
633 if ((op
& 0170777) == P_SUBQW_SP
|| (op
& 0170777) == P_SUBQL_SP
)
635 cache
->locals
+= (op
& 07000) == 0 ? 8 : (op
& 07000) >> 9;
641 else if (op
== P_ADDAW_SP
|| op
== P_LEA_SP_SP
)
644 /* lea (-N,%sp),%sp */
645 cache
->locals
= -read_memory_integer (pc
+ 2, 2);
648 else if (op
== P_ADDAL_SP
)
651 cache
->locals
= -read_memory_integer (pc
+ 2, 4);
658 /* Check whether PC points at code that saves registers on the stack.
659 If so, it updates CACHE and returns the address of the first
660 instruction after the register saves or CURRENT_PC, whichever is
661 smaller. Otherwise, return PC. */
664 m68k_analyze_register_saves (CORE_ADDR pc
, CORE_ADDR current_pc
,
665 struct m68k_frame_cache
*cache
)
667 if (cache
->locals
>= 0)
673 offset
= -4 - cache
->locals
;
674 while (pc
< current_pc
)
676 op
= read_memory_unsigned_integer (pc
, 2);
677 if (op
== P_FMOVEMX_SP
678 && gdbarch_tdep (current_gdbarch
)->fpregs_present
)
680 /* fmovem.x REGS,-(%sp) */
681 op
= read_memory_unsigned_integer (pc
+ 2, 2);
682 if ((op
& 0xff00) == 0xe000)
685 for (i
= 0; i
< 16; i
++, mask
>>= 1)
689 cache
->saved_regs
[i
+ M68K_FP0_REGNUM
] = offset
;
698 else if ((op
& 0177760) == P_MOVEL_SP
)
700 /* move.l %R,-(%sp) */
702 cache
->saved_regs
[regno
] = offset
;
706 else if (op
== P_MOVEML_SP
)
708 /* movem.l REGS,-(%sp) */
709 mask
= read_memory_unsigned_integer (pc
+ 2, 2);
710 for (i
= 0; i
< 16; i
++, mask
>>= 1)
714 cache
->saved_regs
[15 - i
] = offset
;
729 /* Do a full analysis of the prologue at PC and update CACHE
730 accordingly. Bail out early if CURRENT_PC is reached. Return the
731 address where the analysis stopped.
733 We handle all cases that can be generated by gcc.
735 For allocating a stack frame:
739 pea (%fp); move.l %sp,%fp
740 link.w %a6,#0; add.l #-N,%sp
743 subq.w #8,%sp; subq.w #N-8,%sp
748 For saving registers:
752 move.l R1,-(%sp); move.l R2,-(%sp)
755 For setting up the PIC register:
762 m68k_analyze_prologue (CORE_ADDR pc
, CORE_ADDR current_pc
,
763 struct m68k_frame_cache
*cache
)
767 pc
= m68k_analyze_frame_setup (pc
, current_pc
, cache
);
768 pc
= m68k_analyze_register_saves (pc
, current_pc
, cache
);
769 if (pc
>= current_pc
)
772 /* Check for GOT setup. */
773 op
= read_memory_unsigned_integer (pc
, 4);
774 if (op
== P_LEA_PC_A5
)
776 /* lea (%pc,N),%a5 */
783 /* Return PC of first real instruction. */
786 m68k_skip_prologue (CORE_ADDR start_pc
)
788 struct m68k_frame_cache cache
;
793 pc
= m68k_analyze_prologue (start_pc
, (CORE_ADDR
) -1, &cache
);
794 if (cache
.locals
< 0)
800 m68k_unwind_pc (struct gdbarch
*gdbarch
, struct frame_info
*next_frame
)
804 frame_unwind_register (next_frame
, PC_REGNUM
, buf
);
805 return extract_typed_address (buf
, builtin_type_void_func_ptr
);
810 static struct m68k_frame_cache
*
811 m68k_frame_cache (struct frame_info
*next_frame
, void **this_cache
)
813 struct m68k_frame_cache
*cache
;
820 cache
= m68k_alloc_frame_cache ();
823 /* In principle, for normal frames, %fp holds the frame pointer,
824 which holds the base address for the current stack frame.
825 However, for functions that don't need it, the frame pointer is
826 optional. For these "frameless" functions the frame pointer is
827 actually the frame pointer of the calling frame. Signal
828 trampolines are just a special case of a "frameless" function.
829 They (usually) share their frame pointer with the frame that was
830 in progress when the signal occurred. */
832 frame_unwind_register (next_frame
, M68K_FP_REGNUM
, buf
);
833 cache
->base
= extract_unsigned_integer (buf
, 4);
834 if (cache
->base
== 0)
837 /* For normal frames, %pc is stored at 4(%fp). */
838 cache
->saved_regs
[M68K_PC_REGNUM
] = 4;
840 cache
->pc
= frame_func_unwind (next_frame
, NORMAL_FRAME
);
842 m68k_analyze_prologue (cache
->pc
, frame_pc_unwind (next_frame
), cache
);
844 if (cache
->locals
< 0)
846 /* We didn't find a valid frame, which means that CACHE->base
847 currently holds the frame pointer for our calling frame. If
848 we're at the start of a function, or somewhere half-way its
849 prologue, the function's frame probably hasn't been fully
850 setup yet. Try to reconstruct the base address for the stack
851 frame by looking at the stack pointer. For truly "frameless"
852 functions this might work too. */
854 frame_unwind_register (next_frame
, M68K_SP_REGNUM
, buf
);
855 cache
->base
= extract_unsigned_integer (buf
, 4) + cache
->sp_offset
;
858 /* Now that we have the base address for the stack frame we can
859 calculate the value of %sp in the calling frame. */
860 cache
->saved_sp
= cache
->base
+ 8;
862 /* Adjust all the saved registers such that they contain addresses
863 instead of offsets. */
864 for (i
= 0; i
< M68K_NUM_REGS
; i
++)
865 if (cache
->saved_regs
[i
] != -1)
866 cache
->saved_regs
[i
] += cache
->base
;
872 m68k_frame_this_id (struct frame_info
*next_frame
, void **this_cache
,
873 struct frame_id
*this_id
)
875 struct m68k_frame_cache
*cache
= m68k_frame_cache (next_frame
, this_cache
);
877 /* This marks the outermost frame. */
878 if (cache
->base
== 0)
881 /* See the end of m68k_push_dummy_call. */
882 *this_id
= frame_id_build (cache
->base
+ 8, cache
->pc
);
886 m68k_frame_prev_register (struct frame_info
*next_frame
, void **this_cache
,
887 int regnum
, int *optimizedp
,
888 enum lval_type
*lvalp
, CORE_ADDR
*addrp
,
889 int *realnump
, gdb_byte
*valuep
)
891 struct m68k_frame_cache
*cache
= m68k_frame_cache (next_frame
, this_cache
);
893 gdb_assert (regnum
>= 0);
895 if (regnum
== M68K_SP_REGNUM
&& cache
->saved_sp
)
903 /* Store the value. */
904 store_unsigned_integer (valuep
, 4, cache
->saved_sp
);
909 if (regnum
< M68K_NUM_REGS
&& cache
->saved_regs
[regnum
] != -1)
912 *lvalp
= lval_memory
;
913 *addrp
= cache
->saved_regs
[regnum
];
917 /* Read the value in from memory. */
918 read_memory (*addrp
, valuep
,
919 register_size (current_gdbarch
, regnum
));
925 *lvalp
= lval_register
;
929 frame_unwind_register (next_frame
, (*realnump
), valuep
);
932 static const struct frame_unwind m68k_frame_unwind
=
936 m68k_frame_prev_register
939 static const struct frame_unwind
*
940 m68k_frame_sniffer (struct frame_info
*next_frame
)
942 return &m68k_frame_unwind
;
946 m68k_frame_base_address (struct frame_info
*next_frame
, void **this_cache
)
948 struct m68k_frame_cache
*cache
= m68k_frame_cache (next_frame
, this_cache
);
953 static const struct frame_base m68k_frame_base
=
956 m68k_frame_base_address
,
957 m68k_frame_base_address
,
958 m68k_frame_base_address
961 static struct frame_id
962 m68k_unwind_dummy_id (struct gdbarch
*gdbarch
, struct frame_info
*next_frame
)
967 frame_unwind_register (next_frame
, M68K_FP_REGNUM
, buf
);
968 fp
= extract_unsigned_integer (buf
, 4);
970 /* See the end of m68k_push_dummy_call. */
971 return frame_id_build (fp
+ 8, frame_pc_unwind (next_frame
));
975 /* Figure out where the longjmp will land. Slurp the args out of the stack.
976 We expect the first arg to be a pointer to the jmp_buf structure from which
977 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
978 This routine returns true on success. */
981 m68k_get_longjmp_target (CORE_ADDR
*pc
)
984 CORE_ADDR sp
, jb_addr
;
985 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
989 internal_error (__FILE__
, __LINE__
,
990 _("m68k_get_longjmp_target: not implemented"));
994 buf
= alloca (gdbarch_ptr_bit (current_gdbarch
) / TARGET_CHAR_BIT
);
995 sp
= read_register (SP_REGNUM
);
997 if (target_read_memory (sp
+ SP_ARG0
, /* Offset of first arg on stack */
999 gdbarch_ptr_bit (current_gdbarch
) / TARGET_CHAR_BIT
))
1002 jb_addr
= extract_unsigned_integer (buf
, gdbarch_ptr_bit (current_gdbarch
)
1005 if (target_read_memory (jb_addr
+ tdep
->jb_pc
* tdep
->jb_elt_size
, buf
,
1006 gdbarch_ptr_bit (current_gdbarch
) / TARGET_CHAR_BIT
))
1009 *pc
= extract_unsigned_integer (buf
, gdbarch_ptr_bit (current_gdbarch
)
1015 /* System V Release 4 (SVR4). */
1018 m68k_svr4_init_abi (struct gdbarch_info info
, struct gdbarch
*gdbarch
)
1020 struct gdbarch_tdep
*tdep
= gdbarch_tdep (gdbarch
);
1022 /* SVR4 uses a different calling convention. */
1023 set_gdbarch_return_value (gdbarch
, m68k_svr4_return_value
);
1025 /* SVR4 uses %a0 instead of %a1. */
1026 tdep
->struct_value_regnum
= M68K_A0_REGNUM
;
1030 /* Function: m68k_gdbarch_init
1031 Initializer function for the m68k gdbarch vector.
1032 Called by gdbarch. Sets up the gdbarch vector(s) for this target. */
1034 static struct gdbarch
*
1035 m68k_gdbarch_init (struct gdbarch_info info
, struct gdbarch_list
*arches
)
1037 struct gdbarch_tdep
*tdep
= NULL
;
1038 struct gdbarch
*gdbarch
;
1039 struct gdbarch_list
*best_arch
;
1040 struct tdesc_arch_data
*tdesc_data
= NULL
;
1042 enum m68k_flavour flavour
= m68k_no_flavour
;
1044 const struct floatformat
**long_double_format
= floatformats_m68881_ext
;
1046 /* Check any target description for validity. */
1047 if (tdesc_has_registers (info
.target_desc
))
1049 const struct tdesc_feature
*feature
;
1052 feature
= tdesc_find_feature (info
.target_desc
,
1053 "org.gnu.gdb.m68k.core");
1054 if (feature
!= NULL
)
1058 if (feature
== NULL
)
1060 feature
= tdesc_find_feature (info
.target_desc
,
1061 "org.gnu.gdb.coldfire.core");
1062 if (feature
!= NULL
)
1063 flavour
= m68k_coldfire_flavour
;
1066 if (feature
== NULL
)
1068 feature
= tdesc_find_feature (info
.target_desc
,
1069 "org.gnu.gdb.fido.core");
1070 if (feature
!= NULL
)
1071 flavour
= m68k_fido_flavour
;
1074 if (feature
== NULL
)
1077 tdesc_data
= tdesc_data_alloc ();
1080 for (i
= 0; i
<= M68K_PC_REGNUM
; i
++)
1081 valid_p
&= tdesc_numbered_register (feature
, tdesc_data
, i
,
1082 m68k_register_names
[i
]);
1086 tdesc_data_cleanup (tdesc_data
);
1090 feature
= tdesc_find_feature (info
.target_desc
,
1091 "org.gnu.gdb.coldfire.fp");
1092 if (feature
!= NULL
)
1095 for (i
= M68K_FP0_REGNUM
; i
<= M68K_FPI_REGNUM
; i
++)
1096 valid_p
&= tdesc_numbered_register (feature
, tdesc_data
, i
,
1097 m68k_register_names
[i
]);
1100 tdesc_data_cleanup (tdesc_data
);
1108 /* The mechanism for returning floating values from function
1109 and the type of long double depend on whether we're
1110 on ColdFire or standard m68k. */
1112 if (info
.bfd_arch_info
)
1114 const bfd_arch_info_type
*coldfire_arch
=
1115 bfd_lookup_arch (bfd_arch_m68k
, bfd_mach_mcf_isa_a_nodiv
);
1118 && (*info
.bfd_arch_info
->compatible
)
1119 (info
.bfd_arch_info
, coldfire_arch
))
1120 flavour
= m68k_coldfire_flavour
;
1123 /* If there is already a candidate, use it. */
1124 for (best_arch
= gdbarch_list_lookup_by_info (arches
, &info
);
1126 best_arch
= gdbarch_list_lookup_by_info (best_arch
->next
, &info
))
1128 if (flavour
!= gdbarch_tdep (best_arch
->gdbarch
)->flavour
)
1131 if (has_fp
!= gdbarch_tdep (best_arch
->gdbarch
)->fpregs_present
)
1137 tdep
= xmalloc (sizeof (struct gdbarch_tdep
));
1138 gdbarch
= gdbarch_alloc (&info
, tdep
);
1139 tdep
->fpregs_present
= has_fp
;
1140 tdep
->flavour
= flavour
;
1142 if (flavour
== m68k_coldfire_flavour
|| flavour
== m68k_fido_flavour
)
1143 long_double_format
= floatformats_ieee_double
;
1144 set_gdbarch_long_double_format (gdbarch
, long_double_format
);
1145 set_gdbarch_long_double_bit (gdbarch
, long_double_format
[0]->totalsize
);
1147 set_gdbarch_skip_prologue (gdbarch
, m68k_skip_prologue
);
1148 set_gdbarch_breakpoint_from_pc (gdbarch
, m68k_local_breakpoint_from_pc
);
1150 /* Stack grows down. */
1151 set_gdbarch_inner_than (gdbarch
, core_addr_lessthan
);
1152 set_gdbarch_frame_align (gdbarch
, m68k_frame_align
);
1154 set_gdbarch_believe_pcc_promotion (gdbarch
, 1);
1155 if (flavour
== m68k_coldfire_flavour
|| flavour
== m68k_fido_flavour
)
1156 set_gdbarch_decr_pc_after_break (gdbarch
, 2);
1158 set_gdbarch_frame_args_skip (gdbarch
, 8);
1159 set_gdbarch_dwarf_reg_to_regnum (gdbarch
, m68k_dwarf_reg_to_regnum
);
1160 set_gdbarch_dwarf2_reg_to_regnum (gdbarch
, m68k_dwarf_reg_to_regnum
);
1162 set_gdbarch_register_type (gdbarch
, m68k_register_type
);
1163 set_gdbarch_register_name (gdbarch
, m68k_register_name
);
1164 set_gdbarch_num_regs (gdbarch
, M68K_NUM_REGS
);
1165 set_gdbarch_sp_regnum (gdbarch
, M68K_SP_REGNUM
);
1166 set_gdbarch_pc_regnum (gdbarch
, M68K_PC_REGNUM
);
1167 set_gdbarch_ps_regnum (gdbarch
, M68K_PS_REGNUM
);
1168 set_gdbarch_fp0_regnum (gdbarch
, M68K_FP0_REGNUM
);
1169 set_gdbarch_convert_register_p (gdbarch
, m68k_convert_register_p
);
1170 set_gdbarch_register_to_value (gdbarch
, m68k_register_to_value
);
1171 set_gdbarch_value_to_register (gdbarch
, m68k_value_to_register
);
1174 set_gdbarch_fp0_regnum (gdbarch
, M68K_FP0_REGNUM
);
1176 /* Try to figure out if the arch uses floating registers to return
1177 floating point values from functions. */
1180 /* On ColdFire, floating point values are returned in D0. */
1181 if (flavour
== m68k_coldfire_flavour
)
1182 tdep
->float_return
= 0;
1184 tdep
->float_return
= 1;
1188 /* No floating registers, so can't use them for returning values. */
1189 tdep
->float_return
= 0;
1192 /* Function call & return */
1193 set_gdbarch_push_dummy_call (gdbarch
, m68k_push_dummy_call
);
1194 set_gdbarch_return_value (gdbarch
, m68k_return_value
);
1198 set_gdbarch_print_insn (gdbarch
, print_insn_m68k
);
1200 #if defined JB_PC && defined JB_ELEMENT_SIZE
1201 tdep
->jb_pc
= JB_PC
;
1202 tdep
->jb_elt_size
= JB_ELEMENT_SIZE
;
1206 tdep
->struct_value_regnum
= M68K_A1_REGNUM
;
1207 tdep
->struct_return
= reg_struct_return
;
1209 /* Frame unwinder. */
1210 set_gdbarch_unwind_dummy_id (gdbarch
, m68k_unwind_dummy_id
);
1211 set_gdbarch_unwind_pc (gdbarch
, m68k_unwind_pc
);
1213 /* Hook in the DWARF CFI frame unwinder. */
1214 frame_unwind_append_sniffer (gdbarch
, dwarf2_frame_sniffer
);
1216 frame_base_set_default (gdbarch
, &m68k_frame_base
);
1218 /* Hook in ABI-specific overrides, if they have been registered. */
1219 gdbarch_init_osabi (info
, gdbarch
);
1221 /* Now we have tuned the configuration, set a few final things,
1222 based on what the OS ABI has told us. */
1224 if (tdep
->jb_pc
>= 0)
1225 set_gdbarch_get_longjmp_target (gdbarch
, m68k_get_longjmp_target
);
1227 frame_unwind_append_sniffer (gdbarch
, m68k_frame_sniffer
);
1230 tdesc_use_registers (gdbarch
, tdesc_data
);
1237 m68k_dump_tdep (struct gdbarch
*current_gdbarch
, struct ui_file
*file
)
1239 struct gdbarch_tdep
*tdep
= gdbarch_tdep (current_gdbarch
);
1245 extern initialize_file_ftype _initialize_m68k_tdep
; /* -Wmissing-prototypes */
1248 _initialize_m68k_tdep (void)
1250 gdbarch_register (bfd_arch_m68k
, m68k_gdbarch_init
, m68k_dump_tdep
);