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