*** empty log message ***
[deliverable/binutils-gdb.git] / gdb / m68k-tdep.c
CommitLineData
748894bf 1/* Target-dependent code for the Motorola 68000 series.
c6f0559b 2
6aba47ca
DJ
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1999, 2000, 2001,
4 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
c906108c 5
c5aa993b 6 This file is part of GDB.
c906108c 7
c5aa993b
JM
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.
c906108c 12
c5aa993b
JM
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.
c906108c 17
c5aa993b
JM
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
197e01b6
EZ
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
c906108c
SS
22
23#include "defs.h"
3f244638 24#include "dwarf2-frame.h"
c906108c 25#include "frame.h"
8de307e0
AS
26#include "frame-base.h"
27#include "frame-unwind.h"
e6bb342a 28#include "gdbtypes.h"
c906108c
SS
29#include "symtab.h"
30#include "gdbcore.h"
31#include "value.h"
32#include "gdb_string.h"
8de307e0 33#include "gdb_assert.h"
7a292a7a 34#include "inferior.h"
4e052eda 35#include "regcache.h"
5d3ed2e3 36#include "arch-utils.h"
55809acb 37#include "osabi.h"
a89aa300 38#include "dis-asm.h"
32eeb91a
AS
39
40#include "m68k-tdep.h"
c906108c 41\f
c5aa993b 42
89c3b6d3
PDM
43#define P_LINKL_FP 0x480e
44#define P_LINKW_FP 0x4e56
45#define P_PEA_FP 0x4856
8de307e0
AS
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
89c3b6d3 56
103a1597 57
103a1597
GS
58#define REGISTER_BYTES_FP (16*4 + 8 + 8*12 + 3*4)
59#define REGISTER_BYTES_NOFP (16*4 + 8)
60
103a1597 61/* Offset from SP to first arg on stack at first instruction of a function */
103a1597
GS
62#define SP_ARG0 (1 * 4)
63
103a1597
GS
64#if !defined (BPT_VECTOR)
65#define BPT_VECTOR 0xf
66#endif
67
f5cf7aa1 68static const gdb_byte *
103a1597
GS
69m68k_local_breakpoint_from_pc (CORE_ADDR *pcptr, int *lenptr)
70{
f5cf7aa1 71 static gdb_byte break_insn[] = {0x4e, (0x40 | BPT_VECTOR)};
103a1597
GS
72 *lenptr = sizeof (break_insn);
73 return break_insn;
74}
75
76
942dc0e9 77static int
5ae5f592 78m68k_register_bytes_ok (long numbytes)
942dc0e9
GS
79{
80 return ((numbytes == REGISTER_BYTES_FP)
81 || (numbytes == REGISTER_BYTES_NOFP));
82}
83
d85fe7f7
AS
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. */
5d3ed2e3
GS
91
92static struct type *
8de307e0 93m68k_register_type (struct gdbarch *gdbarch, int regnum)
5d3ed2e3 94{
03dac896
AS
95 if (regnum >= FP0_REGNUM && regnum <= FP0_REGNUM + 7)
96 return builtin_type_m68881_ext;
97
32eeb91a 98 if (regnum == M68K_FPI_REGNUM || regnum == PC_REGNUM)
03dac896
AS
99 return builtin_type_void_func_ptr;
100
32eeb91a
AS
101 if (regnum == M68K_FPC_REGNUM || regnum == M68K_FPS_REGNUM
102 || regnum == PS_REGNUM)
03dac896
AS
103 return builtin_type_int32;
104
32eeb91a 105 if (regnum >= M68K_A0_REGNUM && regnum <= M68K_A0_REGNUM + 7)
03dac896
AS
106 return builtin_type_void_data_ptr;
107
108 return builtin_type_int32;
5d3ed2e3
GS
109}
110
111/* Function: m68k_register_name
112 Returns the name of the standard m68k register regnum. */
113
114static const char *
115m68k_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
07652652 125 if (regnum < 0 || regnum >= ARRAY_SIZE (register_names))
5d3ed2e3 126 internal_error (__FILE__, __LINE__,
e2e0b3e5 127 _("m68k_register_name: illegal register number %d"), regnum);
5d3ed2e3
GS
128 else
129 return register_names[regnum];
130}
e47577ab
MK
131\f
132/* Return nonzero if a value of type TYPE stored in register REGNUM
133 needs any special handling. */
134
135static int
136m68k_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
144static void
145m68k_register_to_value (struct frame_info *frame, int regnum,
f5cf7aa1 146 struct type *type, gdb_byte *to)
e47577ab 147{
f5cf7aa1 148 gdb_byte from[M68K_MAX_REGISTER_SIZE];
e47577ab
MK
149
150 /* We only support floating-point values. */
151 if (TYPE_CODE (type) != TYPE_CODE_FLT)
152 {
8a3fe4f8
AC
153 warning (_("Cannot convert floating-point register value "
154 "to non-floating-point type."));
e47577ab
MK
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
167static void
168m68k_value_to_register (struct frame_info *frame, int regnum,
f5cf7aa1 169 struct type *type, const gdb_byte *from)
e47577ab 170{
f5cf7aa1 171 gdb_byte to[M68K_MAX_REGISTER_SIZE];
e47577ab
MK
172
173 /* We only support floating-point values. */
174 if (TYPE_CODE (type) != TYPE_CODE_FLT)
175 {
8a3fe4f8
AC
176 warning (_("Cannot convert non-floating-point type "
177 "to floating-point register value."));
e47577ab
MK
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
8de307e0 187\f
f595cb19
MK
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
8de307e0 218 into VALBUF. */
942dc0e9
GS
219
220static void
8de307e0 221m68k_extract_return_value (struct type *type, struct regcache *regcache,
f5cf7aa1 222 gdb_byte *valbuf)
942dc0e9 223{
8de307e0 224 int len = TYPE_LENGTH (type);
f5cf7aa1 225 gdb_byte buf[M68K_MAX_REGISTER_SIZE];
942dc0e9 226
8de307e0
AS
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);
f5cf7aa1 236 regcache_raw_read (regcache, M68K_D1_REGNUM, valbuf + (len - 4));
8de307e0
AS
237 }
238 else
239 internal_error (__FILE__, __LINE__,
e2e0b3e5 240 _("Cannot extract return value of %d bytes long."), len);
942dc0e9
GS
241}
242
942dc0e9 243static void
f595cb19 244m68k_svr4_extract_return_value (struct type *type, struct regcache *regcache,
f5cf7aa1 245 gdb_byte *valbuf)
942dc0e9 246{
8de307e0 247 int len = TYPE_LENGTH (type);
f5cf7aa1 248 gdb_byte buf[M68K_MAX_REGISTER_SIZE];
942dc0e9 249
f595cb19 250 if (TYPE_CODE (type) == TYPE_CODE_FLT)
8de307e0 251 {
f595cb19
MK
252 regcache_raw_read (regcache, M68K_FP0_REGNUM, buf);
253 convert_typed_floating (buf, builtin_type_m68881_ext, valbuf, type);
8de307e0 254 }
f595cb19
MK
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
263static void
264m68k_store_return_value (struct type *type, struct regcache *regcache,
f5cf7aa1 265 const gdb_byte *valbuf)
f595cb19
MK
266{
267 int len = TYPE_LENGTH (type);
942dc0e9 268
8de307e0
AS
269 if (len <= 4)
270 regcache_raw_write_part (regcache, M68K_D0_REGNUM, 4 - len, len, valbuf);
271 else if (len <= 8)
272 {
f595cb19 273 regcache_raw_write_part (regcache, M68K_D0_REGNUM, 8 - len,
8de307e0 274 len - 4, valbuf);
f5cf7aa1 275 regcache_raw_write (regcache, M68K_D1_REGNUM, valbuf + (len - 4));
8de307e0
AS
276 }
277 else
278 internal_error (__FILE__, __LINE__,
e2e0b3e5 279 _("Cannot store return value of %d bytes long."), len);
8de307e0 280}
942dc0e9 281
f595cb19
MK
282static void
283m68k_svr4_store_return_value (struct type *type, struct regcache *regcache,
f5cf7aa1 284 const gdb_byte *valbuf)
942dc0e9 285{
f595cb19 286 int len = TYPE_LENGTH (type);
8de307e0 287
f595cb19
MK
288 if (TYPE_CODE (type) == TYPE_CODE_FLT)
289 {
f5cf7aa1 290 gdb_byte buf[M68K_MAX_REGISTER_SIZE];
f595cb19
MK
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);
942dc0e9
GS
301}
302
f595cb19
MK
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
c481dac7 307static int
f595cb19 308m68k_reg_struct_return_p (struct gdbarch *gdbarch, struct type *type)
c481dac7 309{
f595cb19
MK
310 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
311 enum type_code code = TYPE_CODE (type);
312 int len = TYPE_LENGTH (type);
c481dac7 313
f595cb19
MK
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);
c481dac7
AS
320}
321
f595cb19
MK
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
328static enum return_value_convention
329m68k_return_value (struct gdbarch *gdbarch, struct type *type,
f5cf7aa1
MK
330 struct regcache *regcache, gdb_byte *readbuf,
331 const gdb_byte *writebuf)
f595cb19
MK
332{
333 enum type_code code = TYPE_CODE (type);
334
1c845060
MK
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. */
f595cb19 343
1c845060
MK
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 }
f595cb19
MK
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
363static enum return_value_convention
364m68k_svr4_return_value (struct gdbarch *gdbarch, struct type *type,
f5cf7aa1
MK
365 struct regcache *regcache, gdb_byte *readbuf,
366 const gdb_byte *writebuf)
f595cb19
MK
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))
51da707a
MK
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 }
f595cb19
MK
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
392a587b 415
9bb47d95
NS
416/* Always align the frame to a 4-byte boundary. This is required on
417 coldfire and harmless on the rest. */
418
419static CORE_ADDR
420m68k_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
421{
422 /* Align the stack to four bytes. */
423 return sp & ~3;
424}
425
8de307e0 426static CORE_ADDR
7d9b040b 427m68k_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
8de307e0
AS
428 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
429 struct value **args, CORE_ADDR sp, int struct_return,
430 CORE_ADDR struct_addr)
7f8e7424 431{
f595cb19 432 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
f5cf7aa1 433 gdb_byte buf[4];
8de307e0
AS
434 int i;
435
436 /* Push arguments in reverse order. */
437 for (i = nargs - 1; i >= 0; i--)
438 {
4754a64e 439 struct type *value_type = value_enclosing_type (args[i]);
c481dac7 440 int len = TYPE_LENGTH (value_type);
8de307e0 441 int container_len = (len + 3) & ~3;
c481dac7
AS
442 int offset;
443
444 /* Non-scalars bigger than 4 bytes are left aligned, others are
445 right aligned. */
446 if ((TYPE_CODE (value_type) == TYPE_CODE_STRUCT
447 || TYPE_CODE (value_type) == TYPE_CODE_UNION
448 || TYPE_CODE (value_type) == TYPE_CODE_ARRAY)
449 && len > 4)
450 offset = 0;
451 else
452 offset = container_len - len;
8de307e0 453 sp -= container_len;
46615f07 454 write_memory (sp + offset, value_contents_all (args[i]), len);
8de307e0
AS
455 }
456
c481dac7 457 /* Store struct value address. */
8de307e0
AS
458 if (struct_return)
459 {
8de307e0 460 store_unsigned_integer (buf, 4, struct_addr);
f595cb19 461 regcache_cooked_write (regcache, tdep->struct_value_regnum, buf);
8de307e0
AS
462 }
463
464 /* Store return address. */
465 sp -= 4;
466 store_unsigned_integer (buf, 4, bp_addr);
467 write_memory (sp, buf, 4);
468
469 /* Finally, update the stack pointer... */
470 store_unsigned_integer (buf, 4, sp);
471 regcache_cooked_write (regcache, M68K_SP_REGNUM, buf);
472
473 /* ...and fake a frame pointer. */
474 regcache_cooked_write (regcache, M68K_FP_REGNUM, buf);
475
476 /* DWARF2/GCC uses the stack address *before* the function call as a
477 frame's CFA. */
478 return sp + 8;
7f8e7424 479}
6dd0fba6
NS
480
481/* Convert a dwarf or dwarf2 regnumber to a GDB regnum. */
482
483static int
484m68k_dwarf_reg_to_regnum (int num)
485{
486 if (num < 8)
487 /* d0..7 */
488 return (num - 0) + M68K_D0_REGNUM;
489 else if (num < 16)
490 /* a0..7 */
491 return (num - 8) + M68K_A0_REGNUM;
492 else if (num < 24)
493 /* fp0..7 */
494 return (num - 16) + M68K_FP0_REGNUM;
495 else if (num == 25)
496 /* pc */
497 return M68K_PC_REGNUM;
498 else
499 return NUM_REGS + NUM_PSEUDO_REGS;
500}
501
8de307e0
AS
502\f
503struct m68k_frame_cache
504{
505 /* Base address. */
506 CORE_ADDR base;
507 CORE_ADDR sp_offset;
508 CORE_ADDR pc;
7f8e7424 509
8de307e0
AS
510 /* Saved registers. */
511 CORE_ADDR saved_regs[M68K_NUM_REGS];
512 CORE_ADDR saved_sp;
7f8e7424 513
8de307e0
AS
514 /* Stack space reserved for local variables. */
515 long locals;
516};
c906108c 517
8de307e0
AS
518/* Allocate and initialize a frame cache. */
519
520static struct m68k_frame_cache *
521m68k_alloc_frame_cache (void)
c906108c 522{
8de307e0
AS
523 struct m68k_frame_cache *cache;
524 int i;
c906108c 525
8de307e0 526 cache = FRAME_OBSTACK_ZALLOC (struct m68k_frame_cache);
c906108c 527
8de307e0
AS
528 /* Base address. */
529 cache->base = 0;
530 cache->sp_offset = -4;
531 cache->pc = 0;
c906108c 532
8de307e0
AS
533 /* Saved registers. We initialize these to -1 since zero is a valid
534 offset (that's where %fp is supposed to be stored). */
535 for (i = 0; i < M68K_NUM_REGS; i++)
536 cache->saved_regs[i] = -1;
537
538 /* Frameless until proven otherwise. */
539 cache->locals = -1;
540
541 return cache;
c906108c
SS
542}
543
8de307e0
AS
544/* Check whether PC points at a code that sets up a new stack frame.
545 If so, it updates CACHE and returns the address of the first
546 instruction after the sequence that sets removes the "hidden"
547 argument from the stack or CURRENT_PC, whichever is smaller.
548 Otherwise, return PC. */
c906108c 549
8de307e0
AS
550static CORE_ADDR
551m68k_analyze_frame_setup (CORE_ADDR pc, CORE_ADDR current_pc,
552 struct m68k_frame_cache *cache)
c906108c 553{
8de307e0
AS
554 int op;
555
556 if (pc >= current_pc)
557 return current_pc;
c906108c 558
8de307e0
AS
559 op = read_memory_unsigned_integer (pc, 2);
560
561 if (op == P_LINKW_FP || op == P_LINKL_FP || op == P_PEA_FP)
c906108c 562 {
8de307e0
AS
563 cache->saved_regs[M68K_FP_REGNUM] = 0;
564 cache->sp_offset += 4;
565 if (op == P_LINKW_FP)
566 {
567 /* link.w %fp, #-N */
568 /* link.w %fp, #0; adda.l #-N, %sp */
569 cache->locals = -read_memory_integer (pc + 2, 2);
570
571 if (pc + 4 < current_pc && cache->locals == 0)
572 {
573 op = read_memory_unsigned_integer (pc + 4, 2);
574 if (op == P_ADDAL_SP)
575 {
576 cache->locals = read_memory_integer (pc + 6, 4);
577 return pc + 10;
578 }
579 }
580
581 return pc + 4;
582 }
583 else if (op == P_LINKL_FP)
c906108c 584 {
8de307e0
AS
585 /* link.l %fp, #-N */
586 cache->locals = -read_memory_integer (pc + 2, 4);
587 return pc + 6;
588 }
589 else
590 {
591 /* pea (%fp); movea.l %sp, %fp */
592 cache->locals = 0;
593
594 if (pc + 2 < current_pc)
595 {
596 op = read_memory_unsigned_integer (pc + 2, 2);
597
598 if (op == P_MOVEAL_SP_FP)
599 {
600 /* move.l %sp, %fp */
601 return pc + 4;
602 }
603 }
604
605 return pc + 2;
c906108c
SS
606 }
607 }
8de307e0 608 else if ((op & 0170777) == P_SUBQW_SP || (op & 0170777) == P_SUBQL_SP)
c906108c 609 {
8de307e0
AS
610 /* subq.[wl] #N,%sp */
611 /* subq.[wl] #8,%sp; subq.[wl] #N,%sp */
612 cache->locals = (op & 07000) == 0 ? 8 : (op & 07000) >> 9;
613 if (pc + 2 < current_pc)
c906108c 614 {
8de307e0
AS
615 op = read_memory_unsigned_integer (pc + 2, 2);
616 if ((op & 0170777) == P_SUBQW_SP || (op & 0170777) == P_SUBQL_SP)
617 {
618 cache->locals += (op & 07000) == 0 ? 8 : (op & 07000) >> 9;
619 return pc + 4;
620 }
c906108c 621 }
8de307e0
AS
622 return pc + 2;
623 }
624 else if (op == P_ADDAW_SP || op == P_LEA_SP_SP)
625 {
626 /* adda.w #-N,%sp */
627 /* lea (-N,%sp),%sp */
628 cache->locals = -read_memory_integer (pc + 2, 2);
629 return pc + 4;
c906108c 630 }
8de307e0 631 else if (op == P_ADDAL_SP)
c906108c 632 {
8de307e0
AS
633 /* adda.l #-N,%sp */
634 cache->locals = -read_memory_integer (pc + 2, 4);
635 return pc + 6;
c906108c 636 }
8de307e0
AS
637
638 return pc;
c906108c 639}
c5aa993b 640
8de307e0
AS
641/* Check whether PC points at code that saves registers on the stack.
642 If so, it updates CACHE and returns the address of the first
643 instruction after the register saves or CURRENT_PC, whichever is
644 smaller. Otherwise, return PC. */
c906108c 645
8de307e0
AS
646static CORE_ADDR
647m68k_analyze_register_saves (CORE_ADDR pc, CORE_ADDR current_pc,
648 struct m68k_frame_cache *cache)
649{
650 if (cache->locals >= 0)
651 {
652 CORE_ADDR offset;
653 int op;
654 int i, mask, regno;
c906108c 655
8de307e0
AS
656 offset = -4 - cache->locals;
657 while (pc < current_pc)
658 {
659 op = read_memory_unsigned_integer (pc, 2);
660 if (op == P_FMOVEMX_SP)
661 {
662 /* fmovem.x REGS,-(%sp) */
663 op = read_memory_unsigned_integer (pc + 2, 2);
664 if ((op & 0xff00) == 0xe000)
665 {
666 mask = op & 0xff;
667 for (i = 0; i < 16; i++, mask >>= 1)
668 {
669 if (mask & 1)
670 {
671 cache->saved_regs[i + M68K_FP0_REGNUM] = offset;
672 offset -= 12;
673 }
674 }
675 pc += 4;
676 }
677 else
678 break;
679 }
0ba5a932 680 else if ((op & 0177760) == P_MOVEL_SP)
8de307e0
AS
681 {
682 /* move.l %R,-(%sp) */
0ba5a932 683 regno = op & 017;
8de307e0
AS
684 cache->saved_regs[regno] = offset;
685 offset -= 4;
686 pc += 2;
687 }
688 else if (op == P_MOVEML_SP)
689 {
690 /* movem.l REGS,-(%sp) */
691 mask = read_memory_unsigned_integer (pc + 2, 2);
692 for (i = 0; i < 16; i++, mask >>= 1)
693 {
694 if (mask & 1)
695 {
696 cache->saved_regs[15 - i] = offset;
697 offset -= 4;
698 }
699 }
700 pc += 4;
701 }
702 else
703 break;
704 }
705 }
706
707 return pc;
708}
c906108c 709
c906108c 710
8de307e0
AS
711/* Do a full analysis of the prologue at PC and update CACHE
712 accordingly. Bail out early if CURRENT_PC is reached. Return the
713 address where the analysis stopped.
c906108c 714
8de307e0 715 We handle all cases that can be generated by gcc.
c906108c 716
8de307e0 717 For allocating a stack frame:
c906108c 718
8de307e0
AS
719 link.w %a6,#-N
720 link.l %a6,#-N
721 pea (%fp); move.l %sp,%fp
722 link.w %a6,#0; add.l #-N,%sp
723 subq.l #N,%sp
724 subq.w #N,%sp
725 subq.w #8,%sp; subq.w #N-8,%sp
726 add.w #-N,%sp
727 lea (-N,%sp),%sp
728 add.l #-N,%sp
c906108c 729
8de307e0 730 For saving registers:
c906108c 731
8de307e0
AS
732 fmovem.x REGS,-(%sp)
733 move.l R1,-(%sp)
734 move.l R1,-(%sp); move.l R2,-(%sp)
735 movem.l REGS,-(%sp)
c906108c 736
8de307e0 737 For setting up the PIC register:
c906108c 738
8de307e0 739 lea (%pc,N),%a5
c906108c 740
8de307e0 741 */
c906108c 742
eb2e12d7 743static CORE_ADDR
8de307e0
AS
744m68k_analyze_prologue (CORE_ADDR pc, CORE_ADDR current_pc,
745 struct m68k_frame_cache *cache)
c906108c 746{
8de307e0 747 unsigned int op;
c906108c 748
8de307e0
AS
749 pc = m68k_analyze_frame_setup (pc, current_pc, cache);
750 pc = m68k_analyze_register_saves (pc, current_pc, cache);
751 if (pc >= current_pc)
752 return current_pc;
c906108c 753
8de307e0
AS
754 /* Check for GOT setup. */
755 op = read_memory_unsigned_integer (pc, 4);
756 if (op == P_LEA_PC_A5)
c906108c 757 {
8de307e0
AS
758 /* lea (%pc,N),%a5 */
759 return pc + 6;
c906108c 760 }
8de307e0
AS
761
762 return pc;
c906108c
SS
763}
764
8de307e0 765/* Return PC of first real instruction. */
7f8e7424 766
8de307e0
AS
767static CORE_ADDR
768m68k_skip_prologue (CORE_ADDR start_pc)
c906108c 769{
8de307e0
AS
770 struct m68k_frame_cache cache;
771 CORE_ADDR pc;
772 int op;
c906108c 773
8de307e0
AS
774 cache.locals = -1;
775 pc = m68k_analyze_prologue (start_pc, (CORE_ADDR) -1, &cache);
776 if (cache.locals < 0)
777 return start_pc;
778 return pc;
779}
c906108c 780
8de307e0
AS
781static CORE_ADDR
782m68k_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
783{
f5cf7aa1 784 gdb_byte buf[8];
7f8e7424 785
8de307e0
AS
786 frame_unwind_register (next_frame, PC_REGNUM, buf);
787 return extract_typed_address (buf, builtin_type_void_func_ptr);
788}
789\f
790/* Normal frames. */
7f8e7424 791
8de307e0
AS
792static struct m68k_frame_cache *
793m68k_frame_cache (struct frame_info *next_frame, void **this_cache)
794{
795 struct m68k_frame_cache *cache;
f5cf7aa1 796 gdb_byte buf[4];
8de307e0
AS
797 int i;
798
799 if (*this_cache)
800 return *this_cache;
801
802 cache = m68k_alloc_frame_cache ();
803 *this_cache = cache;
804
805 /* In principle, for normal frames, %fp holds the frame pointer,
806 which holds the base address for the current stack frame.
807 However, for functions that don't need it, the frame pointer is
808 optional. For these "frameless" functions the frame pointer is
809 actually the frame pointer of the calling frame. Signal
810 trampolines are just a special case of a "frameless" function.
811 They (usually) share their frame pointer with the frame that was
812 in progress when the signal occurred. */
813
814 frame_unwind_register (next_frame, M68K_FP_REGNUM, buf);
815 cache->base = extract_unsigned_integer (buf, 4);
816 if (cache->base == 0)
817 return cache;
818
819 /* For normal frames, %pc is stored at 4(%fp). */
820 cache->saved_regs[M68K_PC_REGNUM] = 4;
821
93d42b30 822 cache->pc = frame_func_unwind (next_frame, NORMAL_FRAME);
8de307e0
AS
823 if (cache->pc != 0)
824 m68k_analyze_prologue (cache->pc, frame_pc_unwind (next_frame), cache);
825
826 if (cache->locals < 0)
827 {
828 /* We didn't find a valid frame, which means that CACHE->base
829 currently holds the frame pointer for our calling frame. If
830 we're at the start of a function, or somewhere half-way its
831 prologue, the function's frame probably hasn't been fully
832 setup yet. Try to reconstruct the base address for the stack
833 frame by looking at the stack pointer. For truly "frameless"
834 functions this might work too. */
835
836 frame_unwind_register (next_frame, M68K_SP_REGNUM, buf);
837 cache->base = extract_unsigned_integer (buf, 4) + cache->sp_offset;
838 }
7f8e7424 839
8de307e0
AS
840 /* Now that we have the base address for the stack frame we can
841 calculate the value of %sp in the calling frame. */
842 cache->saved_sp = cache->base + 8;
7f8e7424 843
8de307e0
AS
844 /* Adjust all the saved registers such that they contain addresses
845 instead of offsets. */
846 for (i = 0; i < M68K_NUM_REGS; i++)
847 if (cache->saved_regs[i] != -1)
848 cache->saved_regs[i] += cache->base;
c906108c 849
8de307e0
AS
850 return cache;
851}
c906108c 852
8de307e0
AS
853static void
854m68k_frame_this_id (struct frame_info *next_frame, void **this_cache,
855 struct frame_id *this_id)
856{
857 struct m68k_frame_cache *cache = m68k_frame_cache (next_frame, this_cache);
c906108c 858
8de307e0
AS
859 /* This marks the outermost frame. */
860 if (cache->base == 0)
861 return;
c5aa993b 862
8de307e0
AS
863 /* See the end of m68k_push_dummy_call. */
864 *this_id = frame_id_build (cache->base + 8, cache->pc);
865}
c5aa993b 866
8de307e0
AS
867static void
868m68k_frame_prev_register (struct frame_info *next_frame, void **this_cache,
869 int regnum, int *optimizedp,
870 enum lval_type *lvalp, CORE_ADDR *addrp,
60b04da5 871 int *realnump, gdb_byte *valuep)
8de307e0
AS
872{
873 struct m68k_frame_cache *cache = m68k_frame_cache (next_frame, this_cache);
874
875 gdb_assert (regnum >= 0);
876
877 if (regnum == M68K_SP_REGNUM && cache->saved_sp)
c5aa993b 878 {
8de307e0
AS
879 *optimizedp = 0;
880 *lvalp = not_lval;
881 *addrp = 0;
882 *realnump = -1;
883 if (valuep)
c906108c 884 {
8de307e0
AS
885 /* Store the value. */
886 store_unsigned_integer (valuep, 4, cache->saved_sp);
89c3b6d3 887 }
8de307e0
AS
888 return;
889 }
890
891 if (regnum < M68K_NUM_REGS && cache->saved_regs[regnum] != -1)
892 {
893 *optimizedp = 0;
894 *lvalp = lval_memory;
895 *addrp = cache->saved_regs[regnum];
896 *realnump = -1;
897 if (valuep)
89c3b6d3 898 {
8de307e0
AS
899 /* Read the value in from memory. */
900 read_memory (*addrp, valuep,
901 register_size (current_gdbarch, regnum));
89c3b6d3 902 }
8de307e0 903 return;
c906108c 904 }
8de307e0 905
00b25ff3
AC
906 *optimizedp = 0;
907 *lvalp = lval_register;
908 *addrp = 0;
909 *realnump = regnum;
910 if (valuep)
911 frame_unwind_register (next_frame, (*realnump), valuep);
8de307e0
AS
912}
913
914static const struct frame_unwind m68k_frame_unwind =
915{
916 NORMAL_FRAME,
917 m68k_frame_this_id,
918 m68k_frame_prev_register
919};
920
921static const struct frame_unwind *
336d1bba 922m68k_frame_sniffer (struct frame_info *next_frame)
8de307e0
AS
923{
924 return &m68k_frame_unwind;
925}
926\f
8de307e0
AS
927static CORE_ADDR
928m68k_frame_base_address (struct frame_info *next_frame, void **this_cache)
929{
930 struct m68k_frame_cache *cache = m68k_frame_cache (next_frame, this_cache);
931
932 return cache->base;
933}
934
935static const struct frame_base m68k_frame_base =
936{
937 &m68k_frame_unwind,
938 m68k_frame_base_address,
939 m68k_frame_base_address,
940 m68k_frame_base_address
941};
942
943static struct frame_id
944m68k_unwind_dummy_id (struct gdbarch *gdbarch, struct frame_info *next_frame)
945{
f5cf7aa1 946 gdb_byte buf[4];
8de307e0 947 CORE_ADDR fp;
c906108c 948
8de307e0
AS
949 frame_unwind_register (next_frame, M68K_FP_REGNUM, buf);
950 fp = extract_unsigned_integer (buf, 4);
c906108c 951
8de307e0
AS
952 /* See the end of m68k_push_dummy_call. */
953 return frame_id_build (fp + 8, frame_pc_unwind (next_frame));
954}
955\f
c906108c 956
c906108c
SS
957/* Figure out where the longjmp will land. Slurp the args out of the stack.
958 We expect the first arg to be a pointer to the jmp_buf structure from which
959 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
960 This routine returns true on success. */
961
c34d127c 962static int
f4281f55 963m68k_get_longjmp_target (CORE_ADDR *pc)
c906108c 964{
f5cf7aa1 965 gdb_byte *buf;
c906108c 966 CORE_ADDR sp, jb_addr;
eb2e12d7
AS
967 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
968
969 if (tdep->jb_pc < 0)
970 {
971 internal_error (__FILE__, __LINE__,
e2e0b3e5 972 _("m68k_get_longjmp_target: not implemented"));
eb2e12d7
AS
973 return 0;
974 }
c906108c 975
35fc8285 976 buf = alloca (TARGET_PTR_BIT / TARGET_CHAR_BIT);
c5aa993b 977 sp = read_register (SP_REGNUM);
c906108c 978
b5d78d39
GS
979 if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack */
980 buf, TARGET_PTR_BIT / TARGET_CHAR_BIT))
c906108c
SS
981 return 0;
982
7c0b4a20 983 jb_addr = extract_unsigned_integer (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
c906108c 984
eb2e12d7 985 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
c906108c
SS
986 TARGET_PTR_BIT / TARGET_CHAR_BIT))
987 return 0;
988
7c0b4a20 989 *pc = extract_unsigned_integer (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
c906108c
SS
990 return 1;
991}
f595cb19
MK
992\f
993
994/* System V Release 4 (SVR4). */
995
996void
997m68k_svr4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
998{
999 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1000
1001 /* SVR4 uses a different calling convention. */
1002 set_gdbarch_return_value (gdbarch, m68k_svr4_return_value);
1003
1004 /* SVR4 uses %a0 instead of %a1. */
1005 tdep->struct_value_regnum = M68K_A0_REGNUM;
1006}
1007\f
c906108c 1008
152d9db6
GS
1009/* Function: m68k_gdbarch_init
1010 Initializer function for the m68k gdbarch vector.
1011 Called by gdbarch. Sets up the gdbarch vector(s) for this target. */
1012
1013static struct gdbarch *
1014m68k_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1015{
1016 struct gdbarch_tdep *tdep = NULL;
1017 struct gdbarch *gdbarch;
1018
1019 /* find a candidate among the list of pre-declared architectures. */
1020 arches = gdbarch_list_lookup_by_info (arches, &info);
1021 if (arches != NULL)
1022 return (arches->gdbarch);
1023
eb2e12d7
AS
1024 tdep = xmalloc (sizeof (struct gdbarch_tdep));
1025 gdbarch = gdbarch_alloc (&info, tdep);
152d9db6 1026
8da61cc4 1027 set_gdbarch_long_double_format (gdbarch, floatformats_m68881_ext);
5d3ed2e3
GS
1028 set_gdbarch_long_double_bit (gdbarch, 96);
1029
5d3ed2e3 1030 set_gdbarch_skip_prologue (gdbarch, m68k_skip_prologue);
103a1597 1031 set_gdbarch_breakpoint_from_pc (gdbarch, m68k_local_breakpoint_from_pc);
5d3ed2e3
GS
1032
1033 /* Stack grows down. */
1034 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
9bb47d95 1035 set_gdbarch_frame_align (gdbarch, m68k_frame_align);
6300c360
GS
1036
1037 set_gdbarch_believe_pcc_promotion (gdbarch, 1);
942dc0e9 1038
6300c360 1039 set_gdbarch_frame_args_skip (gdbarch, 8);
6dd0fba6
NS
1040 set_gdbarch_dwarf_reg_to_regnum (gdbarch, m68k_dwarf_reg_to_regnum);
1041 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, m68k_dwarf_reg_to_regnum);
942dc0e9 1042
8de307e0 1043 set_gdbarch_register_type (gdbarch, m68k_register_type);
5d3ed2e3 1044 set_gdbarch_register_name (gdbarch, m68k_register_name);
6dd0fba6 1045 set_gdbarch_num_regs (gdbarch, M68K_NUM_REGS);
942dc0e9 1046 set_gdbarch_register_bytes_ok (gdbarch, m68k_register_bytes_ok);
32eeb91a 1047 set_gdbarch_sp_regnum (gdbarch, M68K_SP_REGNUM);
32eeb91a
AS
1048 set_gdbarch_pc_regnum (gdbarch, M68K_PC_REGNUM);
1049 set_gdbarch_ps_regnum (gdbarch, M68K_PS_REGNUM);
1050 set_gdbarch_fp0_regnum (gdbarch, M68K_FP0_REGNUM);
e47577ab
MK
1051 set_gdbarch_convert_register_p (gdbarch, m68k_convert_register_p);
1052 set_gdbarch_register_to_value (gdbarch, m68k_register_to_value);
1053 set_gdbarch_value_to_register (gdbarch, m68k_value_to_register);
a2c6a6d5 1054
8de307e0 1055 set_gdbarch_push_dummy_call (gdbarch, m68k_push_dummy_call);
f595cb19 1056 set_gdbarch_return_value (gdbarch, m68k_return_value);
6c0e89ed 1057
650fcc91
AS
1058 /* Disassembler. */
1059 set_gdbarch_print_insn (gdbarch, print_insn_m68k);
1060
eb2e12d7
AS
1061#if defined JB_PC && defined JB_ELEMENT_SIZE
1062 tdep->jb_pc = JB_PC;
1063 tdep->jb_elt_size = JB_ELEMENT_SIZE;
1064#else
1065 tdep->jb_pc = -1;
1066#endif
f595cb19 1067 tdep->struct_value_regnum = M68K_A1_REGNUM;
66894781 1068 tdep->struct_return = reg_struct_return;
8de307e0
AS
1069
1070 /* Frame unwinder. */
1071 set_gdbarch_unwind_dummy_id (gdbarch, m68k_unwind_dummy_id);
1072 set_gdbarch_unwind_pc (gdbarch, m68k_unwind_pc);
3f244638
AS
1073
1074 /* Hook in the DWARF CFI frame unwinder. */
1075 frame_unwind_append_sniffer (gdbarch, dwarf2_frame_sniffer);
1076
8de307e0 1077 frame_base_set_default (gdbarch, &m68k_frame_base);
eb2e12d7 1078
55809acb
AS
1079 /* Hook in ABI-specific overrides, if they have been registered. */
1080 gdbarch_init_osabi (info, gdbarch);
1081
eb2e12d7
AS
1082 /* Now we have tuned the configuration, set a few final things,
1083 based on what the OS ABI has told us. */
1084
1085 if (tdep->jb_pc >= 0)
1086 set_gdbarch_get_longjmp_target (gdbarch, m68k_get_longjmp_target);
1087
336d1bba 1088 frame_unwind_append_sniffer (gdbarch, m68k_frame_sniffer);
8de307e0 1089
152d9db6
GS
1090 return gdbarch;
1091}
1092
1093
1094static void
1095m68k_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file)
1096{
eb2e12d7 1097 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
152d9db6 1098
eb2e12d7
AS
1099 if (tdep == NULL)
1100 return;
152d9db6 1101}
2acceee2 1102
a78f21af
AC
1103extern initialize_file_ftype _initialize_m68k_tdep; /* -Wmissing-prototypes */
1104
c906108c 1105void
fba45db2 1106_initialize_m68k_tdep (void)
c906108c 1107{
152d9db6 1108 gdbarch_register (bfd_arch_m68k, m68k_gdbarch_init, m68k_dump_tdep);
c906108c 1109}
This page took 0.615623 seconds and 4 git commands to generate.