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