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