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 >= gdbarch_fp0_regnum (current_gdbarch)
89 && regnum <= gdbarch_fp0_regnum (current_gdbarch) + 7)
91 if (tdep->flavour == m68k_coldfire_flavour)
92 return builtin_type (gdbarch)->builtin_double;
94 return builtin_type_m68881_ext;
97 if (regnum == M68K_FPI_REGNUM)
98 return builtin_type_void_func_ptr;
100 if (regnum == M68K_FPC_REGNUM || regnum == M68K_FPS_REGNUM)
101 return builtin_type_int32;
105 if (regnum >= M68K_FP0_REGNUM && regnum <= M68K_FPI_REGNUM)
106 return builtin_type_int0;
109 if (regnum == gdbarch_pc_regnum (current_gdbarch))
110 return builtin_type_void_func_ptr;
112 if (regnum >= M68K_A0_REGNUM && regnum <= M68K_A0_REGNUM + 7)
113 return builtin_type_void_data_ptr;
115 return builtin_type_int32;
118 static const char *m68k_register_names[] = {
119 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
120 "a0", "a1", "a2", "a3", "a4", "a5", "fp", "sp",
122 "fp0", "fp1", "fp2", "fp3", "fp4", "fp5", "fp6", "fp7",
123 "fpcontrol", "fpstatus", "fpiaddr"
126 /* Function: m68k_register_name
127 Returns the name of the standard m68k register regnum. */
130 m68k_register_name (int regnum)
132 if (regnum < 0 || regnum >= ARRAY_SIZE (m68k_register_names))
133 internal_error (__FILE__, __LINE__,
134 _("m68k_register_name: illegal register number %d"), regnum);
136 return m68k_register_names[regnum];
139 /* Return nonzero if a value of type TYPE stored in register REGNUM
140 needs any special handling. */
143 m68k_convert_register_p (int regnum, struct type *type)
145 if (!gdbarch_tdep (current_gdbarch)->fpregs_present)
147 return (regnum >= M68K_FP0_REGNUM && regnum <= M68K_FP0_REGNUM + 7);
150 /* Read a value of type TYPE from register REGNUM in frame FRAME, and
151 return its contents in TO. */
154 m68k_register_to_value (struct frame_info *frame, int regnum,
155 struct type *type, gdb_byte *to)
157 gdb_byte from[M68K_MAX_REGISTER_SIZE];
158 struct type *fpreg_type = register_type (current_gdbarch, M68K_FP0_REGNUM);
160 /* We only support floating-point values. */
161 if (TYPE_CODE (type) != TYPE_CODE_FLT)
163 warning (_("Cannot convert floating-point register value "
164 "to non-floating-point type."));
168 /* Convert to TYPE. This should be a no-op if TYPE is equivalent to
169 the extended floating-point format used by the FPU. */
170 get_frame_register (frame, regnum, from);
171 convert_typed_floating (from, fpreg_type, to, type);
174 /* Write the contents FROM of a value of type TYPE into register
175 REGNUM in frame FRAME. */
178 m68k_value_to_register (struct frame_info *frame, int regnum,
179 struct type *type, const gdb_byte *from)
181 gdb_byte to[M68K_MAX_REGISTER_SIZE];
182 struct type *fpreg_type = register_type (current_gdbarch, M68K_FP0_REGNUM);
184 /* We only support floating-point values. */
185 if (TYPE_CODE (type) != TYPE_CODE_FLT)
187 warning (_("Cannot convert non-floating-point type "
188 "to floating-point register value."));
192 /* Convert from TYPE. This should be a no-op if TYPE is equivalent
193 to the extended floating-point format used by the FPU. */
194 convert_typed_floating (from, type, to, fpreg_type);
195 put_frame_register (frame, regnum, to);
199 /* There is a fair number of calling conventions that are in somewhat
200 wide use. The 68000/08/10 don't support an FPU, not even as a
201 coprocessor. All function return values are stored in %d0/%d1.
202 Structures are returned in a static buffer, a pointer to which is
203 returned in %d0. This means that functions returning a structure
204 are not re-entrant. To avoid this problem some systems use a
205 convention where the caller passes a pointer to a buffer in %a1
206 where the return values is to be stored. This convention is the
207 default, and is implemented in the function m68k_return_value.
209 The 68020/030/040/060 do support an FPU, either as a coprocessor
210 (68881/2) or built-in (68040/68060). That's why System V release 4
211 (SVR4) instroduces a new calling convention specified by the SVR4
212 psABI. Integer values are returned in %d0/%d1, pointer return
213 values in %a0 and floating values in %fp0. When calling functions
214 returning a structure the caller should pass a pointer to a buffer
215 for the return value in %a0. This convention is implemented in the
216 function m68k_svr4_return_value, and by appropriately setting the
217 struct_value_regnum member of `struct gdbarch_tdep'.
219 GNU/Linux returns values in the same way as SVR4 does, but uses %a1
220 for passing the structure return value buffer.
222 GCC can also generate code where small structures are returned in
223 %d0/%d1 instead of in memory by using -freg-struct-return. This is
224 the default on NetBSD a.out, OpenBSD and GNU/Linux and several
225 embedded systems. This convention is implemented by setting the
226 struct_return member of `struct gdbarch_tdep' to reg_struct_return. */
228 /* Read a function return value of TYPE from REGCACHE, and copy that
232 m68k_extract_return_value (struct type *type, struct regcache *regcache,
235 int len = TYPE_LENGTH (type);
236 gdb_byte buf[M68K_MAX_REGISTER_SIZE];
240 regcache_raw_read (regcache, M68K_D0_REGNUM, buf);
241 memcpy (valbuf, buf + (4 - len), len);
245 regcache_raw_read (regcache, M68K_D0_REGNUM, buf);
246 memcpy (valbuf, buf + (8 - len), len - 4);
247 regcache_raw_read (regcache, M68K_D1_REGNUM, valbuf + (len - 4));
250 internal_error (__FILE__, __LINE__,
251 _("Cannot extract return value of %d bytes long."), len);
255 m68k_svr4_extract_return_value (struct type *type, struct regcache *regcache,
258 int len = TYPE_LENGTH (type);
259 gdb_byte buf[M68K_MAX_REGISTER_SIZE];
260 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
262 if (tdep->float_return && TYPE_CODE (type) == TYPE_CODE_FLT)
264 struct type *fpreg_type = register_type
265 (current_gdbarch, M68K_FP0_REGNUM);
266 regcache_raw_read (regcache, M68K_FP0_REGNUM, buf);
267 convert_typed_floating (buf, fpreg_type, valbuf, type);
269 else if (TYPE_CODE (type) == TYPE_CODE_PTR && len == 4)
270 regcache_raw_read (regcache, M68K_A0_REGNUM, valbuf);
272 m68k_extract_return_value (type, regcache, valbuf);
275 /* Write a function return value of TYPE from VALBUF into REGCACHE. */
278 m68k_store_return_value (struct type *type, struct regcache *regcache,
279 const gdb_byte *valbuf)
281 int len = TYPE_LENGTH (type);
284 regcache_raw_write_part (regcache, M68K_D0_REGNUM, 4 - len, len, valbuf);
287 regcache_raw_write_part (regcache, M68K_D0_REGNUM, 8 - len,
289 regcache_raw_write (regcache, M68K_D1_REGNUM, valbuf + (len - 4));
292 internal_error (__FILE__, __LINE__,
293 _("Cannot store return value of %d bytes long."), len);
297 m68k_svr4_store_return_value (struct type *type, struct regcache *regcache,
298 const gdb_byte *valbuf)
300 int len = TYPE_LENGTH (type);
301 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
303 if (tdep->float_return && TYPE_CODE (type) == TYPE_CODE_FLT)
305 struct type *fpreg_type = register_type
306 (current_gdbarch, M68K_FP0_REGNUM);
307 gdb_byte buf[M68K_MAX_REGISTER_SIZE];
308 convert_typed_floating (valbuf, type, buf, fpreg_type);
309 regcache_raw_write (regcache, M68K_FP0_REGNUM, buf);
311 else if (TYPE_CODE (type) == TYPE_CODE_PTR && len == 4)
313 regcache_raw_write (regcache, M68K_A0_REGNUM, valbuf);
314 regcache_raw_write (regcache, M68K_D0_REGNUM, valbuf);
317 m68k_store_return_value (type, regcache, valbuf);
320 /* Return non-zero if TYPE, which is assumed to be a structure or
321 union type, should be returned in registers for architecture
325 m68k_reg_struct_return_p (struct gdbarch *gdbarch, struct type *type)
327 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
328 enum type_code code = TYPE_CODE (type);
329 int len = TYPE_LENGTH (type);
331 gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION);
333 if (tdep->struct_return == pcc_struct_return)
336 return (len == 1 || len == 2 || len == 4 || len == 8);
339 /* Determine, for architecture GDBARCH, how a return value of TYPE
340 should be returned. If it is supposed to be returned in registers,
341 and READBUF is non-zero, read the appropriate value from REGCACHE,
342 and copy it into READBUF. If WRITEBUF is non-zero, write the value
343 from WRITEBUF into REGCACHE. */
345 static enum return_value_convention
346 m68k_return_value (struct gdbarch *gdbarch, struct type *type,
347 struct regcache *regcache, gdb_byte *readbuf,
348 const gdb_byte *writebuf)
350 enum type_code code = TYPE_CODE (type);
352 /* GCC returns a `long double' in memory too. */
353 if (((code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION)
354 && !m68k_reg_struct_return_p (gdbarch, type))
355 || (code == TYPE_CODE_FLT && TYPE_LENGTH (type) == 12))
357 /* The default on m68k is to return structures in static memory.
358 Consequently a function must return the address where we can
359 find the return value. */
365 regcache_raw_read_unsigned (regcache, M68K_D0_REGNUM, &addr);
366 read_memory (addr, readbuf, TYPE_LENGTH (type));
369 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
373 m68k_extract_return_value (type, regcache, readbuf);
375 m68k_store_return_value (type, regcache, writebuf);
377 return RETURN_VALUE_REGISTER_CONVENTION;
380 static enum return_value_convention
381 m68k_svr4_return_value (struct gdbarch *gdbarch, struct type *type,
382 struct regcache *regcache, gdb_byte *readbuf,
383 const gdb_byte *writebuf)
385 enum type_code code = TYPE_CODE (type);
387 if ((code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION)
388 && !m68k_reg_struct_return_p (gdbarch, type))
390 /* The System V ABI says that:
392 "A function returning a structure or union also sets %a0 to
393 the value it finds in %a0. Thus when the caller receives
394 control again, the address of the returned object resides in
397 So the ABI guarantees that we can always find the return
398 value just after the function has returned. */
404 regcache_raw_read_unsigned (regcache, M68K_A0_REGNUM, &addr);
405 read_memory (addr, readbuf, TYPE_LENGTH (type));
408 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
411 /* This special case is for structures consisting of a single
412 `float' or `double' member. These structures are returned in
413 %fp0. For these structures, we call ourselves recursively,
414 changing TYPE into the type of the first member of the structure.
415 Since that should work for all structures that have only one
416 member, we don't bother to check the member's type here. */
417 if (code == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1)
419 type = check_typedef (TYPE_FIELD_TYPE (type, 0));
420 return m68k_svr4_return_value (gdbarch, type, regcache,
425 m68k_svr4_extract_return_value (type, regcache, readbuf);
427 m68k_svr4_store_return_value (type, regcache, writebuf);
429 return RETURN_VALUE_REGISTER_CONVENTION;
433 /* Always align the frame to a 4-byte boundary. This is required on
434 coldfire and harmless on the rest. */
437 m68k_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
439 /* Align the stack to four bytes. */
444 m68k_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
445 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
446 struct value **args, CORE_ADDR sp, int struct_return,
447 CORE_ADDR struct_addr)
449 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
453 /* Push arguments in reverse order. */
454 for (i = nargs - 1; i >= 0; i--)
456 struct type *value_type = value_enclosing_type (args[i]);
457 int len = TYPE_LENGTH (value_type);
458 int container_len = (len + 3) & ~3;
461 /* Non-scalars bigger than 4 bytes are left aligned, others are
463 if ((TYPE_CODE (value_type) == TYPE_CODE_STRUCT
464 || TYPE_CODE (value_type) == TYPE_CODE_UNION
465 || TYPE_CODE (value_type) == TYPE_CODE_ARRAY)
469 offset = container_len - len;
471 write_memory (sp + offset, value_contents_all (args[i]), len);
474 /* Store struct value address. */
477 store_unsigned_integer (buf, 4, struct_addr);
478 regcache_cooked_write (regcache, tdep->struct_value_regnum, buf);
481 /* Store return address. */
483 store_unsigned_integer (buf, 4, bp_addr);
484 write_memory (sp, buf, 4);
486 /* Finally, update the stack pointer... */
487 store_unsigned_integer (buf, 4, sp);
488 regcache_cooked_write (regcache, M68K_SP_REGNUM, buf);
490 /* ...and fake a frame pointer. */
491 regcache_cooked_write (regcache, M68K_FP_REGNUM, buf);
493 /* DWARF2/GCC uses the stack address *before* the function call as a
498 /* Convert a dwarf or dwarf2 regnumber to a GDB regnum. */
501 m68k_dwarf_reg_to_regnum (int num)
505 return (num - 0) + M68K_D0_REGNUM;
508 return (num - 8) + M68K_A0_REGNUM;
509 else if (num < 24 && gdbarch_tdep (current_gdbarch)->fpregs_present)
511 return (num - 16) + M68K_FP0_REGNUM;
514 return M68K_PC_REGNUM;
516 return gdbarch_num_regs (current_gdbarch)
517 + gdbarch_num_pseudo_regs (current_gdbarch);
521 struct m68k_frame_cache
528 /* Saved registers. */
529 CORE_ADDR saved_regs[M68K_NUM_REGS];
532 /* Stack space reserved for local variables. */
536 /* Allocate and initialize a frame cache. */
538 static struct m68k_frame_cache *
539 m68k_alloc_frame_cache (void)
541 struct m68k_frame_cache *cache;
544 cache = FRAME_OBSTACK_ZALLOC (struct m68k_frame_cache);
548 cache->sp_offset = -4;
551 /* Saved registers. We initialize these to -1 since zero is a valid
552 offset (that's where %fp is supposed to be stored). */
553 for (i = 0; i < M68K_NUM_REGS; i++)
554 cache->saved_regs[i] = -1;
556 /* Frameless until proven otherwise. */
562 /* Check whether PC points at a code that sets up a new stack frame.
563 If so, it updates CACHE and returns the address of the first
564 instruction after the sequence that sets removes the "hidden"
565 argument from the stack or CURRENT_PC, whichever is smaller.
566 Otherwise, return PC. */
569 m68k_analyze_frame_setup (CORE_ADDR pc, CORE_ADDR current_pc,
570 struct m68k_frame_cache *cache)
574 if (pc >= current_pc)
577 op = read_memory_unsigned_integer (pc, 2);
579 if (op == P_LINKW_FP || op == P_LINKL_FP || op == P_PEA_FP)
581 cache->saved_regs[M68K_FP_REGNUM] = 0;
582 cache->sp_offset += 4;
583 if (op == P_LINKW_FP)
585 /* link.w %fp, #-N */
586 /* link.w %fp, #0; adda.l #-N, %sp */
587 cache->locals = -read_memory_integer (pc + 2, 2);
589 if (pc + 4 < current_pc && cache->locals == 0)
591 op = read_memory_unsigned_integer (pc + 4, 2);
592 if (op == P_ADDAL_SP)
594 cache->locals = read_memory_integer (pc + 6, 4);
601 else if (op == P_LINKL_FP)
603 /* link.l %fp, #-N */
604 cache->locals = -read_memory_integer (pc + 2, 4);
609 /* pea (%fp); movea.l %sp, %fp */
612 if (pc + 2 < current_pc)
614 op = read_memory_unsigned_integer (pc + 2, 2);
616 if (op == P_MOVEAL_SP_FP)
618 /* move.l %sp, %fp */
626 else if ((op & 0170777) == P_SUBQW_SP || (op & 0170777) == P_SUBQL_SP)
628 /* subq.[wl] #N,%sp */
629 /* subq.[wl] #8,%sp; subq.[wl] #N,%sp */
630 cache->locals = (op & 07000) == 0 ? 8 : (op & 07000) >> 9;
631 if (pc + 2 < current_pc)
633 op = read_memory_unsigned_integer (pc + 2, 2);
634 if ((op & 0170777) == P_SUBQW_SP || (op & 0170777) == P_SUBQL_SP)
636 cache->locals += (op & 07000) == 0 ? 8 : (op & 07000) >> 9;
642 else if (op == P_ADDAW_SP || op == P_LEA_SP_SP)
645 /* lea (-N,%sp),%sp */
646 cache->locals = -read_memory_integer (pc + 2, 2);
649 else if (op == P_ADDAL_SP)
652 cache->locals = -read_memory_integer (pc + 2, 4);
659 /* Check whether PC points at code that saves registers on the stack.
660 If so, it updates CACHE and returns the address of the first
661 instruction after the register saves or CURRENT_PC, whichever is
662 smaller. Otherwise, return PC. */
665 m68k_analyze_register_saves (CORE_ADDR pc, CORE_ADDR current_pc,
666 struct m68k_frame_cache *cache)
668 if (cache->locals >= 0)
674 offset = -4 - cache->locals;
675 while (pc < current_pc)
677 op = read_memory_unsigned_integer (pc, 2);
678 if (op == P_FMOVEMX_SP
679 && gdbarch_tdep (current_gdbarch)->fpregs_present)
681 /* fmovem.x REGS,-(%sp) */
682 op = read_memory_unsigned_integer (pc + 2, 2);
683 if ((op & 0xff00) == 0xe000)
686 for (i = 0; i < 16; i++, mask >>= 1)
690 cache->saved_regs[i + M68K_FP0_REGNUM] = offset;
699 else if ((op & 0177760) == P_MOVEL_SP)
701 /* move.l %R,-(%sp) */
703 cache->saved_regs[regno] = offset;
707 else if (op == P_MOVEML_SP)
709 /* movem.l REGS,-(%sp) */
710 mask = read_memory_unsigned_integer (pc + 2, 2);
711 for (i = 0; i < 16; i++, mask >>= 1)
715 cache->saved_regs[15 - i] = offset;
730 /* Do a full analysis of the prologue at PC and update CACHE
731 accordingly. Bail out early if CURRENT_PC is reached. Return the
732 address where the analysis stopped.
734 We handle all cases that can be generated by gcc.
736 For allocating a stack frame:
740 pea (%fp); move.l %sp,%fp
741 link.w %a6,#0; add.l #-N,%sp
744 subq.w #8,%sp; subq.w #N-8,%sp
749 For saving registers:
753 move.l R1,-(%sp); move.l R2,-(%sp)
756 For setting up the PIC register:
763 m68k_analyze_prologue (CORE_ADDR pc, CORE_ADDR current_pc,
764 struct m68k_frame_cache *cache)
768 pc = m68k_analyze_frame_setup (pc, current_pc, cache);
769 pc = m68k_analyze_register_saves (pc, current_pc, cache);
770 if (pc >= current_pc)
773 /* Check for GOT setup. */
774 op = read_memory_unsigned_integer (pc, 4);
775 if (op == P_LEA_PC_A5)
777 /* lea (%pc,N),%a5 */
784 /* Return PC of first real instruction. */
787 m68k_skip_prologue (CORE_ADDR start_pc)
789 struct m68k_frame_cache cache;
794 pc = m68k_analyze_prologue (start_pc, (CORE_ADDR) -1, &cache);
795 if (cache.locals < 0)
801 m68k_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
805 frame_unwind_register (next_frame, gdbarch_pc_regnum (current_gdbarch), buf);
806 return extract_typed_address (buf, builtin_type_void_func_ptr);
811 static struct m68k_frame_cache *
812 m68k_frame_cache (struct frame_info *next_frame, void **this_cache)
814 struct m68k_frame_cache *cache;
821 cache = m68k_alloc_frame_cache ();
824 /* In principle, for normal frames, %fp holds the frame pointer,
825 which holds the base address for the current stack frame.
826 However, for functions that don't need it, the frame pointer is
827 optional. For these "frameless" functions the frame pointer is
828 actually the frame pointer of the calling frame. Signal
829 trampolines are just a special case of a "frameless" function.
830 They (usually) share their frame pointer with the frame that was
831 in progress when the signal occurred. */
833 frame_unwind_register (next_frame, M68K_FP_REGNUM, buf);
834 cache->base = extract_unsigned_integer (buf, 4);
835 if (cache->base == 0)
838 /* For normal frames, %pc is stored at 4(%fp). */
839 cache->saved_regs[M68K_PC_REGNUM] = 4;
841 cache->pc = frame_func_unwind (next_frame, NORMAL_FRAME);
843 m68k_analyze_prologue (cache->pc, frame_pc_unwind (next_frame), cache);
845 if (cache->locals < 0)
847 /* We didn't find a valid frame, which means that CACHE->base
848 currently holds the frame pointer for our calling frame. If
849 we're at the start of a function, or somewhere half-way its
850 prologue, the function's frame probably hasn't been fully
851 setup yet. Try to reconstruct the base address for the stack
852 frame by looking at the stack pointer. For truly "frameless"
853 functions this might work too. */
855 frame_unwind_register (next_frame, M68K_SP_REGNUM, buf);
856 cache->base = extract_unsigned_integer (buf, 4) + cache->sp_offset;
859 /* Now that we have the base address for the stack frame we can
860 calculate the value of %sp in the calling frame. */
861 cache->saved_sp = cache->base + 8;
863 /* Adjust all the saved registers such that they contain addresses
864 instead of offsets. */
865 for (i = 0; i < M68K_NUM_REGS; i++)
866 if (cache->saved_regs[i] != -1)
867 cache->saved_regs[i] += cache->base;
873 m68k_frame_this_id (struct frame_info *next_frame, void **this_cache,
874 struct frame_id *this_id)
876 struct m68k_frame_cache *cache = m68k_frame_cache (next_frame, this_cache);
878 /* This marks the outermost frame. */
879 if (cache->base == 0)
882 /* See the end of m68k_push_dummy_call. */
883 *this_id = frame_id_build (cache->base + 8, cache->pc);
887 m68k_frame_prev_register (struct frame_info *next_frame, void **this_cache,
888 int regnum, int *optimizedp,
889 enum lval_type *lvalp, CORE_ADDR *addrp,
890 int *realnump, gdb_byte *valuep)
892 struct m68k_frame_cache *cache = m68k_frame_cache (next_frame, this_cache);
894 gdb_assert (regnum >= 0);
896 if (regnum == M68K_SP_REGNUM && cache->saved_sp)
904 /* Store the value. */
905 store_unsigned_integer (valuep, 4, cache->saved_sp);
910 if (regnum < M68K_NUM_REGS && cache->saved_regs[regnum] != -1)
913 *lvalp = lval_memory;
914 *addrp = cache->saved_regs[regnum];
918 /* Read the value in from memory. */
919 read_memory (*addrp, valuep,
920 register_size (current_gdbarch, regnum));
926 *lvalp = lval_register;
930 frame_unwind_register (next_frame, (*realnump), valuep);
933 static const struct frame_unwind m68k_frame_unwind =
937 m68k_frame_prev_register
940 static const struct frame_unwind *
941 m68k_frame_sniffer (struct frame_info *next_frame)
943 return &m68k_frame_unwind;
947 m68k_frame_base_address (struct frame_info *next_frame, void **this_cache)
949 struct m68k_frame_cache *cache = m68k_frame_cache (next_frame, this_cache);
954 static const struct frame_base m68k_frame_base =
957 m68k_frame_base_address,
958 m68k_frame_base_address,
959 m68k_frame_base_address
962 static struct frame_id
963 m68k_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
968 frame_unwind_register (next_frame, M68K_FP_REGNUM, buf);
969 fp = extract_unsigned_integer (buf, 4);
971 /* See the end of m68k_push_dummy_call. */
972 return frame_id_build (fp + 8, frame_pc_unwind (next_frame));
976 /* Figure out where the longjmp will land. Slurp the args out of the stack.
977 We expect the first arg to be a pointer to the jmp_buf structure from which
978 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
979 This routine returns true on success. */
982 m68k_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
985 CORE_ADDR sp, jb_addr;
986 struct gdbarch_tdep *tdep = gdbarch_tdep (get_frame_arch (frame));
990 internal_error (__FILE__, __LINE__,
991 _("m68k_get_longjmp_target: not implemented"));
995 buf = alloca (gdbarch_ptr_bit (current_gdbarch) / TARGET_CHAR_BIT);
996 sp = get_frame_register_unsigned (frame, gdbarch_sp_regnum (current_gdbarch));
998 if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack */
1000 gdbarch_ptr_bit (current_gdbarch) / TARGET_CHAR_BIT))
1003 jb_addr = extract_unsigned_integer (buf, gdbarch_ptr_bit (current_gdbarch)
1006 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
1007 gdbarch_ptr_bit (current_gdbarch) / TARGET_CHAR_BIT))
1010 *pc = extract_unsigned_integer (buf, gdbarch_ptr_bit (current_gdbarch)
1016 /* System V Release 4 (SVR4). */
1019 m68k_svr4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1021 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1023 /* SVR4 uses a different calling convention. */
1024 set_gdbarch_return_value (gdbarch, m68k_svr4_return_value);
1026 /* SVR4 uses %a0 instead of %a1. */
1027 tdep->struct_value_regnum = M68K_A0_REGNUM;
1031 /* Function: m68k_gdbarch_init
1032 Initializer function for the m68k gdbarch vector.
1033 Called by gdbarch. Sets up the gdbarch vector(s) for this target. */
1035 static struct gdbarch *
1036 m68k_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1038 struct gdbarch_tdep *tdep = NULL;
1039 struct gdbarch *gdbarch;
1040 struct gdbarch_list *best_arch;
1041 struct tdesc_arch_data *tdesc_data = NULL;
1043 enum m68k_flavour flavour = m68k_no_flavour;
1045 const struct floatformat **long_double_format = floatformats_m68881_ext;
1047 /* Check any target description for validity. */
1048 if (tdesc_has_registers (info.target_desc))
1050 const struct tdesc_feature *feature;
1053 feature = tdesc_find_feature (info.target_desc,
1054 "org.gnu.gdb.m68k.core");
1055 if (feature != NULL)
1059 if (feature == NULL)
1061 feature = tdesc_find_feature (info.target_desc,
1062 "org.gnu.gdb.coldfire.core");
1063 if (feature != NULL)
1064 flavour = m68k_coldfire_flavour;
1067 if (feature == NULL)
1069 feature = tdesc_find_feature (info.target_desc,
1070 "org.gnu.gdb.fido.core");
1071 if (feature != NULL)
1072 flavour = m68k_fido_flavour;
1075 if (feature == NULL)
1078 tdesc_data = tdesc_data_alloc ();
1081 for (i = 0; i <= M68K_PC_REGNUM; i++)
1082 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
1083 m68k_register_names[i]);
1087 tdesc_data_cleanup (tdesc_data);
1091 feature = tdesc_find_feature (info.target_desc,
1092 "org.gnu.gdb.coldfire.fp");
1093 if (feature != NULL)
1096 for (i = M68K_FP0_REGNUM; i <= M68K_FPI_REGNUM; i++)
1097 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
1098 m68k_register_names[i]);
1101 tdesc_data_cleanup (tdesc_data);
1109 /* The mechanism for returning floating values from function
1110 and the type of long double depend on whether we're
1111 on ColdFire or standard m68k. */
1113 if (info.bfd_arch_info)
1115 const bfd_arch_info_type *coldfire_arch =
1116 bfd_lookup_arch (bfd_arch_m68k, bfd_mach_mcf_isa_a_nodiv);
1119 && (*info.bfd_arch_info->compatible)
1120 (info.bfd_arch_info, coldfire_arch))
1121 flavour = m68k_coldfire_flavour;
1124 /* If there is already a candidate, use it. */
1125 for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
1127 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
1129 if (flavour != gdbarch_tdep (best_arch->gdbarch)->flavour)
1132 if (has_fp != gdbarch_tdep (best_arch->gdbarch)->fpregs_present)
1138 tdep = xmalloc (sizeof (struct gdbarch_tdep));
1139 gdbarch = gdbarch_alloc (&info, tdep);
1140 tdep->fpregs_present = has_fp;
1141 tdep->flavour = flavour;
1143 if (flavour == m68k_coldfire_flavour || flavour == m68k_fido_flavour)
1144 long_double_format = floatformats_ieee_double;
1145 set_gdbarch_long_double_format (gdbarch, long_double_format);
1146 set_gdbarch_long_double_bit (gdbarch, long_double_format[0]->totalsize);
1148 set_gdbarch_skip_prologue (gdbarch, m68k_skip_prologue);
1149 set_gdbarch_breakpoint_from_pc (gdbarch, m68k_local_breakpoint_from_pc);
1151 /* Stack grows down. */
1152 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1153 set_gdbarch_frame_align (gdbarch, m68k_frame_align);
1155 set_gdbarch_believe_pcc_promotion (gdbarch, 1);
1156 if (flavour == m68k_coldfire_flavour || flavour == m68k_fido_flavour)
1157 set_gdbarch_decr_pc_after_break (gdbarch, 2);
1159 set_gdbarch_frame_args_skip (gdbarch, 8);
1160 set_gdbarch_dwarf_reg_to_regnum (gdbarch, m68k_dwarf_reg_to_regnum);
1161 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, m68k_dwarf_reg_to_regnum);
1163 set_gdbarch_register_type (gdbarch, m68k_register_type);
1164 set_gdbarch_register_name (gdbarch, m68k_register_name);
1165 set_gdbarch_num_regs (gdbarch, M68K_NUM_REGS);
1166 set_gdbarch_sp_regnum (gdbarch, M68K_SP_REGNUM);
1167 set_gdbarch_pc_regnum (gdbarch, M68K_PC_REGNUM);
1168 set_gdbarch_ps_regnum (gdbarch, M68K_PS_REGNUM);
1169 set_gdbarch_fp0_regnum (gdbarch, M68K_FP0_REGNUM);
1170 set_gdbarch_convert_register_p (gdbarch, m68k_convert_register_p);
1171 set_gdbarch_register_to_value (gdbarch, m68k_register_to_value);
1172 set_gdbarch_value_to_register (gdbarch, m68k_value_to_register);
1175 set_gdbarch_fp0_regnum (gdbarch, M68K_FP0_REGNUM);
1177 /* Try to figure out if the arch uses floating registers to return
1178 floating point values from functions. */
1181 /* On ColdFire, floating point values are returned in D0. */
1182 if (flavour == m68k_coldfire_flavour)
1183 tdep->float_return = 0;
1185 tdep->float_return = 1;
1189 /* No floating registers, so can't use them for returning values. */
1190 tdep->float_return = 0;
1193 /* Function call & return */
1194 set_gdbarch_push_dummy_call (gdbarch, m68k_push_dummy_call);
1195 set_gdbarch_return_value (gdbarch, m68k_return_value);
1199 set_gdbarch_print_insn (gdbarch, print_insn_m68k);
1201 #if defined JB_PC && defined JB_ELEMENT_SIZE
1202 tdep->jb_pc = JB_PC;
1203 tdep->jb_elt_size = JB_ELEMENT_SIZE;
1207 tdep->struct_value_regnum = M68K_A1_REGNUM;
1208 tdep->struct_return = reg_struct_return;
1210 /* Frame unwinder. */
1211 set_gdbarch_unwind_dummy_id (gdbarch, m68k_unwind_dummy_id);
1212 set_gdbarch_unwind_pc (gdbarch, m68k_unwind_pc);
1214 /* Hook in the DWARF CFI frame unwinder. */
1215 frame_unwind_append_sniffer (gdbarch, dwarf2_frame_sniffer);
1217 frame_base_set_default (gdbarch, &m68k_frame_base);
1219 /* Hook in ABI-specific overrides, if they have been registered. */
1220 gdbarch_init_osabi (info, gdbarch);
1222 /* Now we have tuned the configuration, set a few final things,
1223 based on what the OS ABI has told us. */
1225 if (tdep->jb_pc >= 0)
1226 set_gdbarch_get_longjmp_target (gdbarch, m68k_get_longjmp_target);
1228 frame_unwind_append_sniffer (gdbarch, m68k_frame_sniffer);
1231 tdesc_use_registers (gdbarch, tdesc_data);
1238 m68k_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file)
1240 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
1246 extern initialize_file_ftype _initialize_m68k_tdep; /* -Wmissing-prototypes */
1249 _initialize_m68k_tdep (void)
1251 gdbarch_register (bfd_arch_m68k, m68k_gdbarch_init, m68k_dump_tdep);