m68k-tdep.c revision 1.1.1.1 1 /* Target-dependent code for the Motorola 68000 series.
2
3 Copyright (C) 1990-2014 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "dwarf2-frame.h"
22 #include "frame.h"
23 #include "frame-base.h"
24 #include "frame-unwind.h"
25 #include "gdbtypes.h"
26 #include "symtab.h"
27 #include "gdbcore.h"
28 #include "value.h"
29 #include <string.h>
30 #include "gdb_assert.h"
31 #include "inferior.h"
32 #include "regcache.h"
33 #include "arch-utils.h"
34 #include "osabi.h"
35 #include "dis-asm.h"
36 #include "target-descriptions.h"
37
38 #include "m68k-tdep.h"
39
40
42 #define P_LINKL_FP 0x480e
43 #define P_LINKW_FP 0x4e56
44 #define P_PEA_FP 0x4856
45 #define P_MOVEAL_SP_FP 0x2c4f
46 #define P_ADDAW_SP 0xdefc
47 #define P_ADDAL_SP 0xdffc
48 #define P_SUBQW_SP 0x514f
49 #define P_SUBQL_SP 0x518f
50 #define P_LEA_SP_SP 0x4fef
51 #define P_LEA_PC_A5 0x4bfb0170
52 #define P_FMOVEMX_SP 0xf227
53 #define P_MOVEL_SP 0x2f00
54 #define P_MOVEML_SP 0x48e7
55
56 /* Offset from SP to first arg on stack at first instruction of a function. */
57 #define SP_ARG0 (1 * 4)
58
59 #if !defined (BPT_VECTOR)
60 #define BPT_VECTOR 0xf
61 #endif
62
63 static const gdb_byte *
64 m68k_local_breakpoint_from_pc (struct gdbarch *gdbarch,
65 CORE_ADDR *pcptr, int *lenptr)
66 {
67 static gdb_byte break_insn[] = {0x4e, (0x40 | BPT_VECTOR)};
68 *lenptr = sizeof (break_insn);
69 return break_insn;
70 }
71
72
74 /* Construct types for ISA-specific registers. */
75 static struct type *
76 m68k_ps_type (struct gdbarch *gdbarch)
77 {
78 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
79
80 if (!tdep->m68k_ps_type)
81 {
82 struct type *type;
83
84 type = arch_flags_type (gdbarch, "builtin_type_m68k_ps", 4);
85 append_flags_type_flag (type, 0, "C");
86 append_flags_type_flag (type, 1, "V");
87 append_flags_type_flag (type, 2, "Z");
88 append_flags_type_flag (type, 3, "N");
89 append_flags_type_flag (type, 4, "X");
90 append_flags_type_flag (type, 8, "I0");
91 append_flags_type_flag (type, 9, "I1");
92 append_flags_type_flag (type, 10, "I2");
93 append_flags_type_flag (type, 12, "M");
94 append_flags_type_flag (type, 13, "S");
95 append_flags_type_flag (type, 14, "T0");
96 append_flags_type_flag (type, 15, "T1");
97
98 tdep->m68k_ps_type = type;
99 }
100
101 return tdep->m68k_ps_type;
102 }
103
104 static struct type *
105 m68881_ext_type (struct gdbarch *gdbarch)
106 {
107 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
108
109 if (!tdep->m68881_ext_type)
110 tdep->m68881_ext_type
111 = arch_float_type (gdbarch, -1, "builtin_type_m68881_ext",
112 floatformats_m68881_ext);
113
114 return tdep->m68881_ext_type;
115 }
116
117 /* Return the GDB type object for the "standard" data type of data in
118 register N. This should be int for D0-D7, SR, FPCONTROL and
119 FPSTATUS, long double for FP0-FP7, and void pointer for all others
120 (A0-A7, PC, FPIADDR). Note, for registers which contain
121 addresses return pointer to void, not pointer to char, because we
122 don't want to attempt to print the string after printing the
123 address. */
124
125 static struct type *
126 m68k_register_type (struct gdbarch *gdbarch, int regnum)
127 {
128 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
129
130 if (tdep->fpregs_present)
131 {
132 if (regnum >= gdbarch_fp0_regnum (gdbarch)
133 && regnum <= gdbarch_fp0_regnum (gdbarch) + 7)
134 {
135 if (tdep->flavour == m68k_coldfire_flavour)
136 return builtin_type (gdbarch)->builtin_double;
137 else
138 return m68881_ext_type (gdbarch);
139 }
140
141 if (regnum == M68K_FPI_REGNUM)
142 return builtin_type (gdbarch)->builtin_func_ptr;
143
144 if (regnum == M68K_FPC_REGNUM || regnum == M68K_FPS_REGNUM)
145 return builtin_type (gdbarch)->builtin_int32;
146 }
147 else
148 {
149 if (regnum >= M68K_FP0_REGNUM && regnum <= M68K_FPI_REGNUM)
150 return builtin_type (gdbarch)->builtin_int0;
151 }
152
153 if (regnum == gdbarch_pc_regnum (gdbarch))
154 return builtin_type (gdbarch)->builtin_func_ptr;
155
156 if (regnum >= M68K_A0_REGNUM && regnum <= M68K_A0_REGNUM + 7)
157 return builtin_type (gdbarch)->builtin_data_ptr;
158
159 if (regnum == M68K_PS_REGNUM)
160 return m68k_ps_type (gdbarch);
161
162 return builtin_type (gdbarch)->builtin_int32;
163 }
164
165 static const char *m68k_register_names[] = {
166 "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7",
167 "a0", "a1", "a2", "a3", "a4", "a5", "fp", "sp",
168 "ps", "pc",
169 "fp0", "fp1", "fp2", "fp3", "fp4", "fp5", "fp6", "fp7",
170 "fpcontrol", "fpstatus", "fpiaddr"
171 };
172
173 /* Function: m68k_register_name
174 Returns the name of the standard m68k register regnum. */
175
176 static const char *
177 m68k_register_name (struct gdbarch *gdbarch, int regnum)
178 {
179 if (regnum < 0 || regnum >= ARRAY_SIZE (m68k_register_names))
180 internal_error (__FILE__, __LINE__,
181 _("m68k_register_name: illegal register number %d"),
182 regnum);
183 else if (regnum >= M68K_FP0_REGNUM && regnum <= M68K_FPI_REGNUM
184 && gdbarch_tdep (gdbarch)->fpregs_present == 0)
185 return "";
186 else
187 return m68k_register_names[regnum];
188 }
189
190 /* Return nonzero if a value of type TYPE stored in register REGNUM
192 needs any special handling. */
193
194 static int
195 m68k_convert_register_p (struct gdbarch *gdbarch,
196 int regnum, struct type *type)
197 {
198 if (!gdbarch_tdep (gdbarch)->fpregs_present)
199 return 0;
200 return (regnum >= M68K_FP0_REGNUM && regnum <= M68K_FP0_REGNUM + 7
201 && type != register_type (gdbarch, M68K_FP0_REGNUM));
202 }
203
204 /* Read a value of type TYPE from register REGNUM in frame FRAME, and
205 return its contents in TO. */
206
207 static int
208 m68k_register_to_value (struct frame_info *frame, int regnum,
209 struct type *type, gdb_byte *to,
210 int *optimizedp, int *unavailablep)
211 {
212 gdb_byte from[M68K_MAX_REGISTER_SIZE];
213 struct type *fpreg_type = register_type (get_frame_arch (frame),
214 M68K_FP0_REGNUM);
215
216 /* We only support floating-point values. */
217 if (TYPE_CODE (type) != TYPE_CODE_FLT)
218 {
219 warning (_("Cannot convert floating-point register value "
220 "to non-floating-point type."));
221 *optimizedp = *unavailablep = 0;
222 return 0;
223 }
224
225 /* Convert to TYPE. */
226
227 /* Convert to TYPE. */
228 if (!get_frame_register_bytes (frame, regnum, 0, TYPE_LENGTH (type),
229 from, optimizedp, unavailablep))
230 return 0;
231
232 convert_typed_floating (from, fpreg_type, to, type);
233 *optimizedp = *unavailablep = 0;
234 return 1;
235 }
236
237 /* Write the contents FROM of a value of type TYPE into register
238 REGNUM in frame FRAME. */
239
240 static void
241 m68k_value_to_register (struct frame_info *frame, int regnum,
242 struct type *type, const gdb_byte *from)
243 {
244 gdb_byte to[M68K_MAX_REGISTER_SIZE];
245 struct type *fpreg_type = register_type (get_frame_arch (frame),
246 M68K_FP0_REGNUM);
247
248 /* We only support floating-point values. */
249 if (TYPE_CODE (type) != TYPE_CODE_FLT)
250 {
251 warning (_("Cannot convert non-floating-point type "
252 "to floating-point register value."));
253 return;
254 }
255
256 /* Convert from TYPE. */
257 convert_typed_floating (from, type, to, fpreg_type);
258 put_frame_register (frame, regnum, to);
259 }
260
261
262 /* There is a fair number of calling conventions that are in somewhat
264 wide use. The 68000/08/10 don't support an FPU, not even as a
265 coprocessor. All function return values are stored in %d0/%d1.
266 Structures are returned in a static buffer, a pointer to which is
267 returned in %d0. This means that functions returning a structure
268 are not re-entrant. To avoid this problem some systems use a
269 convention where the caller passes a pointer to a buffer in %a1
270 where the return values is to be stored. This convention is the
271 default, and is implemented in the function m68k_return_value.
272
273 The 68020/030/040/060 do support an FPU, either as a coprocessor
274 (68881/2) or built-in (68040/68060). That's why System V release 4
275 (SVR4) instroduces a new calling convention specified by the SVR4
276 psABI. Integer values are returned in %d0/%d1, pointer return
277 values in %a0 and floating values in %fp0. When calling functions
278 returning a structure the caller should pass a pointer to a buffer
279 for the return value in %a0. This convention is implemented in the
280 function m68k_svr4_return_value, and by appropriately setting the
281 struct_value_regnum member of `struct gdbarch_tdep'.
282
283 GNU/Linux returns values in the same way as SVR4 does, but uses %a1
284 for passing the structure return value buffer.
285
286 GCC can also generate code where small structures are returned in
287 %d0/%d1 instead of in memory by using -freg-struct-return. This is
288 the default on NetBSD a.out, OpenBSD and GNU/Linux and several
289 embedded systems. This convention is implemented by setting the
290 struct_return member of `struct gdbarch_tdep' to reg_struct_return. */
291
292 /* Read a function return value of TYPE from REGCACHE, and copy that
293 into VALBUF. */
294
295 static void
296 m68k_extract_return_value (struct type *type, struct regcache *regcache,
297 gdb_byte *valbuf)
298 {
299 int len = TYPE_LENGTH (type);
300 gdb_byte buf[M68K_MAX_REGISTER_SIZE];
301
302 if (len <= 4)
303 {
304 regcache_raw_read (regcache, M68K_D0_REGNUM, buf);
305 memcpy (valbuf, buf + (4 - len), len);
306 }
307 else if (len <= 8)
308 {
309 regcache_raw_read (regcache, M68K_D0_REGNUM, buf);
310 memcpy (valbuf, buf + (8 - len), len - 4);
311 regcache_raw_read (regcache, M68K_D1_REGNUM, valbuf + (len - 4));
312 }
313 else
314 internal_error (__FILE__, __LINE__,
315 _("Cannot extract return value of %d bytes long."), len);
316 }
317
318 static void
319 m68k_svr4_extract_return_value (struct type *type, struct regcache *regcache,
320 gdb_byte *valbuf)
321 {
322 gdb_byte buf[M68K_MAX_REGISTER_SIZE];
323 struct gdbarch *gdbarch = get_regcache_arch (regcache);
324 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
325
326 if (tdep->float_return && TYPE_CODE (type) == TYPE_CODE_FLT)
327 {
328 struct type *fpreg_type = register_type (gdbarch, M68K_FP0_REGNUM);
329 regcache_raw_read (regcache, M68K_FP0_REGNUM, buf);
330 convert_typed_floating (buf, fpreg_type, valbuf, type);
331 }
332 else if (TYPE_CODE (type) == TYPE_CODE_PTR && TYPE_LENGTH (type) == 4)
333 regcache_raw_read (regcache, M68K_A0_REGNUM, valbuf);
334 else
335 m68k_extract_return_value (type, regcache, valbuf);
336 }
337
338 /* Write a function return value of TYPE from VALBUF into REGCACHE. */
339
340 static void
341 m68k_store_return_value (struct type *type, struct regcache *regcache,
342 const gdb_byte *valbuf)
343 {
344 int len = TYPE_LENGTH (type);
345
346 if (len <= 4)
347 regcache_raw_write_part (regcache, M68K_D0_REGNUM, 4 - len, len, valbuf);
348 else if (len <= 8)
349 {
350 regcache_raw_write_part (regcache, M68K_D0_REGNUM, 8 - len,
351 len - 4, valbuf);
352 regcache_raw_write (regcache, M68K_D1_REGNUM, valbuf + (len - 4));
353 }
354 else
355 internal_error (__FILE__, __LINE__,
356 _("Cannot store return value of %d bytes long."), len);
357 }
358
359 static void
360 m68k_svr4_store_return_value (struct type *type, struct regcache *regcache,
361 const gdb_byte *valbuf)
362 {
363 struct gdbarch *gdbarch = get_regcache_arch (regcache);
364 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
365
366 if (tdep->float_return && TYPE_CODE (type) == TYPE_CODE_FLT)
367 {
368 struct type *fpreg_type = register_type (gdbarch, M68K_FP0_REGNUM);
369 gdb_byte buf[M68K_MAX_REGISTER_SIZE];
370 convert_typed_floating (valbuf, type, buf, fpreg_type);
371 regcache_raw_write (regcache, M68K_FP0_REGNUM, buf);
372 }
373 else if (TYPE_CODE (type) == TYPE_CODE_PTR && TYPE_LENGTH (type) == 4)
374 {
375 regcache_raw_write (regcache, M68K_A0_REGNUM, valbuf);
376 regcache_raw_write (regcache, M68K_D0_REGNUM, valbuf);
377 }
378 else
379 m68k_store_return_value (type, regcache, valbuf);
380 }
381
382 /* Return non-zero if TYPE, which is assumed to be a structure, union or
383 complex type, should be returned in registers for architecture
384 GDBARCH. */
385
386 static int
387 m68k_reg_struct_return_p (struct gdbarch *gdbarch, struct type *type)
388 {
389 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
390 enum type_code code = TYPE_CODE (type);
391 int len = TYPE_LENGTH (type);
392
393 gdb_assert (code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION
394 || code == TYPE_CODE_COMPLEX);
395
396 if (tdep->struct_return == pcc_struct_return)
397 return 0;
398
399 return (len == 1 || len == 2 || len == 4 || len == 8);
400 }
401
402 /* Determine, for architecture GDBARCH, how a return value of TYPE
403 should be returned. If it is supposed to be returned in registers,
404 and READBUF is non-zero, read the appropriate value from REGCACHE,
405 and copy it into READBUF. If WRITEBUF is non-zero, write the value
406 from WRITEBUF into REGCACHE. */
407
408 static enum return_value_convention
409 m68k_return_value (struct gdbarch *gdbarch, struct value *function,
410 struct type *type, struct regcache *regcache,
411 gdb_byte *readbuf, const gdb_byte *writebuf)
412 {
413 enum type_code code = TYPE_CODE (type);
414
415 /* GCC returns a `long double' in memory too. */
416 if (((code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION
417 || code == TYPE_CODE_COMPLEX)
418 && !m68k_reg_struct_return_p (gdbarch, type))
419 || (code == TYPE_CODE_FLT && TYPE_LENGTH (type) == 12))
420 {
421 /* The default on m68k is to return structures in static memory.
422 Consequently a function must return the address where we can
423 find the return value. */
424
425 if (readbuf)
426 {
427 ULONGEST addr;
428
429 regcache_raw_read_unsigned (regcache, M68K_D0_REGNUM, &addr);
430 read_memory (addr, readbuf, TYPE_LENGTH (type));
431 }
432
433 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
434 }
435
436 if (readbuf)
437 m68k_extract_return_value (type, regcache, readbuf);
438 if (writebuf)
439 m68k_store_return_value (type, regcache, writebuf);
440
441 return RETURN_VALUE_REGISTER_CONVENTION;
442 }
443
444 static enum return_value_convention
445 m68k_svr4_return_value (struct gdbarch *gdbarch, struct value *function,
446 struct type *type, struct regcache *regcache,
447 gdb_byte *readbuf, const gdb_byte *writebuf)
448 {
449 enum type_code code = TYPE_CODE (type);
450
451 if ((code == TYPE_CODE_STRUCT || code == TYPE_CODE_UNION
452 || code == TYPE_CODE_COMPLEX)
453 && !m68k_reg_struct_return_p (gdbarch, type))
454 {
455 /* The System V ABI says that:
456
457 "A function returning a structure or union also sets %a0 to
458 the value it finds in %a0. Thus when the caller receives
459 control again, the address of the returned object resides in
460 register %a0."
461
462 So the ABI guarantees that we can always find the return
463 value just after the function has returned. */
464
465 if (readbuf)
466 {
467 ULONGEST addr;
468
469 regcache_raw_read_unsigned (regcache, M68K_A0_REGNUM, &addr);
470 read_memory (addr, readbuf, TYPE_LENGTH (type));
471 }
472
473 return RETURN_VALUE_ABI_RETURNS_ADDRESS;
474 }
475
476 /* This special case is for structures consisting of a single
477 `float' or `double' member. These structures are returned in
478 %fp0. For these structures, we call ourselves recursively,
479 changing TYPE into the type of the first member of the structure.
480 Since that should work for all structures that have only one
481 member, we don't bother to check the member's type here. */
482 if (code == TYPE_CODE_STRUCT && TYPE_NFIELDS (type) == 1)
483 {
484 type = check_typedef (TYPE_FIELD_TYPE (type, 0));
485 return m68k_svr4_return_value (gdbarch, function, type, regcache,
486 readbuf, writebuf);
487 }
488
489 if (readbuf)
490 m68k_svr4_extract_return_value (type, regcache, readbuf);
491 if (writebuf)
492 m68k_svr4_store_return_value (type, regcache, writebuf);
493
494 return RETURN_VALUE_REGISTER_CONVENTION;
495 }
496
497
499 /* Always align the frame to a 4-byte boundary. This is required on
500 coldfire and harmless on the rest. */
501
502 static CORE_ADDR
503 m68k_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
504 {
505 /* Align the stack to four bytes. */
506 return sp & ~3;
507 }
508
509 static CORE_ADDR
510 m68k_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
511 struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
512 struct value **args, CORE_ADDR sp, int struct_return,
513 CORE_ADDR struct_addr)
514 {
515 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
516 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
517 gdb_byte buf[4];
518 int i;
519
520 /* Push arguments in reverse order. */
521 for (i = nargs - 1; i >= 0; i--)
522 {
523 struct type *value_type = value_enclosing_type (args[i]);
524 int len = TYPE_LENGTH (value_type);
525 int container_len = (len + 3) & ~3;
526 int offset;
527
528 /* Non-scalars bigger than 4 bytes are left aligned, others are
529 right aligned. */
530 if ((TYPE_CODE (value_type) == TYPE_CODE_STRUCT
531 || TYPE_CODE (value_type) == TYPE_CODE_UNION
532 || TYPE_CODE (value_type) == TYPE_CODE_ARRAY)
533 && len > 4)
534 offset = 0;
535 else
536 offset = container_len - len;
537 sp -= container_len;
538 write_memory (sp + offset, value_contents_all (args[i]), len);
539 }
540
541 /* Store struct value address. */
542 if (struct_return)
543 {
544 store_unsigned_integer (buf, 4, byte_order, struct_addr);
545 regcache_cooked_write (regcache, tdep->struct_value_regnum, buf);
546 }
547
548 /* Store return address. */
549 sp -= 4;
550 store_unsigned_integer (buf, 4, byte_order, bp_addr);
551 write_memory (sp, buf, 4);
552
553 /* Finally, update the stack pointer... */
554 store_unsigned_integer (buf, 4, byte_order, sp);
555 regcache_cooked_write (regcache, M68K_SP_REGNUM, buf);
556
557 /* ...and fake a frame pointer. */
558 regcache_cooked_write (regcache, M68K_FP_REGNUM, buf);
559
560 /* DWARF2/GCC uses the stack address *before* the function call as a
561 frame's CFA. */
562 return sp + 8;
563 }
564
565 /* Convert a dwarf or dwarf2 regnumber to a GDB regnum. */
566
567 static int
568 m68k_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int num)
569 {
570 if (num < 8)
571 /* d0..7 */
572 return (num - 0) + M68K_D0_REGNUM;
573 else if (num < 16)
574 /* a0..7 */
575 return (num - 8) + M68K_A0_REGNUM;
576 else if (num < 24 && gdbarch_tdep (gdbarch)->fpregs_present)
577 /* fp0..7 */
578 return (num - 16) + M68K_FP0_REGNUM;
579 else if (num == 25)
580 /* pc */
581 return M68K_PC_REGNUM;
582 else
583 return gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
584 }
585
586
587 struct m68k_frame_cache
589 {
590 /* Base address. */
591 CORE_ADDR base;
592 CORE_ADDR sp_offset;
593 CORE_ADDR pc;
594
595 /* Saved registers. */
596 CORE_ADDR saved_regs[M68K_NUM_REGS];
597 CORE_ADDR saved_sp;
598
599 /* Stack space reserved for local variables. */
600 long locals;
601 };
602
603 /* Allocate and initialize a frame cache. */
604
605 static struct m68k_frame_cache *
606 m68k_alloc_frame_cache (void)
607 {
608 struct m68k_frame_cache *cache;
609 int i;
610
611 cache = FRAME_OBSTACK_ZALLOC (struct m68k_frame_cache);
612
613 /* Base address. */
614 cache->base = 0;
615 cache->sp_offset = -4;
616 cache->pc = 0;
617
618 /* Saved registers. We initialize these to -1 since zero is a valid
619 offset (that's where %fp is supposed to be stored). */
620 for (i = 0; i < M68K_NUM_REGS; i++)
621 cache->saved_regs[i] = -1;
622
623 /* Frameless until proven otherwise. */
624 cache->locals = -1;
625
626 return cache;
627 }
628
629 /* Check whether PC points at a code that sets up a new stack frame.
630 If so, it updates CACHE and returns the address of the first
631 instruction after the sequence that sets removes the "hidden"
632 argument from the stack or CURRENT_PC, whichever is smaller.
633 Otherwise, return PC. */
634
635 static CORE_ADDR
636 m68k_analyze_frame_setup (struct gdbarch *gdbarch,
637 CORE_ADDR pc, CORE_ADDR current_pc,
638 struct m68k_frame_cache *cache)
639 {
640 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
641 int op;
642
643 if (pc >= current_pc)
644 return current_pc;
645
646 op = read_memory_unsigned_integer (pc, 2, byte_order);
647
648 if (op == P_LINKW_FP || op == P_LINKL_FP || op == P_PEA_FP)
649 {
650 cache->saved_regs[M68K_FP_REGNUM] = 0;
651 cache->sp_offset += 4;
652 if (op == P_LINKW_FP)
653 {
654 /* link.w %fp, #-N */
655 /* link.w %fp, #0; adda.l #-N, %sp */
656 cache->locals = -read_memory_integer (pc + 2, 2, byte_order);
657
658 if (pc + 4 < current_pc && cache->locals == 0)
659 {
660 op = read_memory_unsigned_integer (pc + 4, 2, byte_order);
661 if (op == P_ADDAL_SP)
662 {
663 cache->locals = read_memory_integer (pc + 6, 4, byte_order);
664 return pc + 10;
665 }
666 }
667
668 return pc + 4;
669 }
670 else if (op == P_LINKL_FP)
671 {
672 /* link.l %fp, #-N */
673 cache->locals = -read_memory_integer (pc + 2, 4, byte_order);
674 return pc + 6;
675 }
676 else
677 {
678 /* pea (%fp); movea.l %sp, %fp */
679 cache->locals = 0;
680
681 if (pc + 2 < current_pc)
682 {
683 op = read_memory_unsigned_integer (pc + 2, 2, byte_order);
684
685 if (op == P_MOVEAL_SP_FP)
686 {
687 /* move.l %sp, %fp */
688 return pc + 4;
689 }
690 }
691
692 return pc + 2;
693 }
694 }
695 else if ((op & 0170777) == P_SUBQW_SP || (op & 0170777) == P_SUBQL_SP)
696 {
697 /* subq.[wl] #N,%sp */
698 /* subq.[wl] #8,%sp; subq.[wl] #N,%sp */
699 cache->locals = (op & 07000) == 0 ? 8 : (op & 07000) >> 9;
700 if (pc + 2 < current_pc)
701 {
702 op = read_memory_unsigned_integer (pc + 2, 2, byte_order);
703 if ((op & 0170777) == P_SUBQW_SP || (op & 0170777) == P_SUBQL_SP)
704 {
705 cache->locals += (op & 07000) == 0 ? 8 : (op & 07000) >> 9;
706 return pc + 4;
707 }
708 }
709 return pc + 2;
710 }
711 else if (op == P_ADDAW_SP || op == P_LEA_SP_SP)
712 {
713 /* adda.w #-N,%sp */
714 /* lea (-N,%sp),%sp */
715 cache->locals = -read_memory_integer (pc + 2, 2, byte_order);
716 return pc + 4;
717 }
718 else if (op == P_ADDAL_SP)
719 {
720 /* adda.l #-N,%sp */
721 cache->locals = -read_memory_integer (pc + 2, 4, byte_order);
722 return pc + 6;
723 }
724
725 return pc;
726 }
727
728 /* Check whether PC points at code that saves registers on the stack.
729 If so, it updates CACHE and returns the address of the first
730 instruction after the register saves or CURRENT_PC, whichever is
731 smaller. Otherwise, return PC. */
732
733 static CORE_ADDR
734 m68k_analyze_register_saves (struct gdbarch *gdbarch, CORE_ADDR pc,
735 CORE_ADDR current_pc,
736 struct m68k_frame_cache *cache)
737 {
738 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
739
740 if (cache->locals >= 0)
741 {
742 CORE_ADDR offset;
743 int op;
744 int i, mask, regno;
745
746 offset = -4 - cache->locals;
747 while (pc < current_pc)
748 {
749 op = read_memory_unsigned_integer (pc, 2, byte_order);
750 if (op == P_FMOVEMX_SP
751 && gdbarch_tdep (gdbarch)->fpregs_present)
752 {
753 /* fmovem.x REGS,-(%sp) */
754 op = read_memory_unsigned_integer (pc + 2, 2, byte_order);
755 if ((op & 0xff00) == 0xe000)
756 {
757 mask = op & 0xff;
758 for (i = 0; i < 16; i++, mask >>= 1)
759 {
760 if (mask & 1)
761 {
762 cache->saved_regs[i + M68K_FP0_REGNUM] = offset;
763 offset -= 12;
764 }
765 }
766 pc += 4;
767 }
768 else
769 break;
770 }
771 else if ((op & 0177760) == P_MOVEL_SP)
772 {
773 /* move.l %R,-(%sp) */
774 regno = op & 017;
775 cache->saved_regs[regno] = offset;
776 offset -= 4;
777 pc += 2;
778 }
779 else if (op == P_MOVEML_SP)
780 {
781 /* movem.l REGS,-(%sp) */
782 mask = read_memory_unsigned_integer (pc + 2, 2, byte_order);
783 for (i = 0; i < 16; i++, mask >>= 1)
784 {
785 if (mask & 1)
786 {
787 cache->saved_regs[15 - i] = offset;
788 offset -= 4;
789 }
790 }
791 pc += 4;
792 }
793 else
794 break;
795 }
796 }
797
798 return pc;
799 }
800
801
802 /* Do a full analysis of the prologue at PC and update CACHE
803 accordingly. Bail out early if CURRENT_PC is reached. Return the
804 address where the analysis stopped.
805
806 We handle all cases that can be generated by gcc.
807
808 For allocating a stack frame:
809
810 link.w %a6,#-N
811 link.l %a6,#-N
812 pea (%fp); move.l %sp,%fp
813 link.w %a6,#0; add.l #-N,%sp
814 subq.l #N,%sp
815 subq.w #N,%sp
816 subq.w #8,%sp; subq.w #N-8,%sp
817 add.w #-N,%sp
818 lea (-N,%sp),%sp
819 add.l #-N,%sp
820
821 For saving registers:
822
823 fmovem.x REGS,-(%sp)
824 move.l R1,-(%sp)
825 move.l R1,-(%sp); move.l R2,-(%sp)
826 movem.l REGS,-(%sp)
827
828 For setting up the PIC register:
829
830 lea (%pc,N),%a5
831
832 */
833
834 static CORE_ADDR
835 m68k_analyze_prologue (struct gdbarch *gdbarch, CORE_ADDR pc,
836 CORE_ADDR current_pc, struct m68k_frame_cache *cache)
837 {
838 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
839 unsigned int op;
840
841 pc = m68k_analyze_frame_setup (gdbarch, pc, current_pc, cache);
842 pc = m68k_analyze_register_saves (gdbarch, pc, current_pc, cache);
843 if (pc >= current_pc)
844 return current_pc;
845
846 /* Check for GOT setup. */
847 op = read_memory_unsigned_integer (pc, 4, byte_order);
848 if (op == P_LEA_PC_A5)
849 {
850 /* lea (%pc,N),%a5 */
851 return pc + 8;
852 }
853
854 return pc;
855 }
856
857 /* Return PC of first real instruction. */
858
859 static CORE_ADDR
860 m68k_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
861 {
862 struct m68k_frame_cache cache;
863 CORE_ADDR pc;
864
865 cache.locals = -1;
866 pc = m68k_analyze_prologue (gdbarch, start_pc, (CORE_ADDR) -1, &cache);
867 if (cache.locals < 0)
868 return start_pc;
869 return pc;
870 }
871
872 static CORE_ADDR
873 m68k_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
874 {
875 gdb_byte buf[8];
876
877 frame_unwind_register (next_frame, gdbarch_pc_regnum (gdbarch), buf);
878 return extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
879 }
880
881 /* Normal frames. */
883
884 static struct m68k_frame_cache *
885 m68k_frame_cache (struct frame_info *this_frame, void **this_cache)
886 {
887 struct gdbarch *gdbarch = get_frame_arch (this_frame);
888 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
889 struct m68k_frame_cache *cache;
890 gdb_byte buf[4];
891 int i;
892
893 if (*this_cache)
894 return *this_cache;
895
896 cache = m68k_alloc_frame_cache ();
897 *this_cache = cache;
898
899 /* In principle, for normal frames, %fp holds the frame pointer,
900 which holds the base address for the current stack frame.
901 However, for functions that don't need it, the frame pointer is
902 optional. For these "frameless" functions the frame pointer is
903 actually the frame pointer of the calling frame. Signal
904 trampolines are just a special case of a "frameless" function.
905 They (usually) share their frame pointer with the frame that was
906 in progress when the signal occurred. */
907
908 get_frame_register (this_frame, M68K_FP_REGNUM, buf);
909 cache->base = extract_unsigned_integer (buf, 4, byte_order);
910 if (cache->base == 0)
911 return cache;
912
913 /* For normal frames, %pc is stored at 4(%fp). */
914 cache->saved_regs[M68K_PC_REGNUM] = 4;
915
916 cache->pc = get_frame_func (this_frame);
917 if (cache->pc != 0)
918 m68k_analyze_prologue (get_frame_arch (this_frame), cache->pc,
919 get_frame_pc (this_frame), cache);
920
921 if (cache->locals < 0)
922 {
923 /* We didn't find a valid frame, which means that CACHE->base
924 currently holds the frame pointer for our calling frame. If
925 we're at the start of a function, or somewhere half-way its
926 prologue, the function's frame probably hasn't been fully
927 setup yet. Try to reconstruct the base address for the stack
928 frame by looking at the stack pointer. For truly "frameless"
929 functions this might work too. */
930
931 get_frame_register (this_frame, M68K_SP_REGNUM, buf);
932 cache->base = extract_unsigned_integer (buf, 4, byte_order)
933 + cache->sp_offset;
934 }
935
936 /* Now that we have the base address for the stack frame we can
937 calculate the value of %sp in the calling frame. */
938 cache->saved_sp = cache->base + 8;
939
940 /* Adjust all the saved registers such that they contain addresses
941 instead of offsets. */
942 for (i = 0; i < M68K_NUM_REGS; i++)
943 if (cache->saved_regs[i] != -1)
944 cache->saved_regs[i] += cache->base;
945
946 return cache;
947 }
948
949 static void
950 m68k_frame_this_id (struct frame_info *this_frame, void **this_cache,
951 struct frame_id *this_id)
952 {
953 struct m68k_frame_cache *cache = m68k_frame_cache (this_frame, this_cache);
954
955 /* This marks the outermost frame. */
956 if (cache->base == 0)
957 return;
958
959 /* See the end of m68k_push_dummy_call. */
960 *this_id = frame_id_build (cache->base + 8, cache->pc);
961 }
962
963 static struct value *
964 m68k_frame_prev_register (struct frame_info *this_frame, void **this_cache,
965 int regnum)
966 {
967 struct m68k_frame_cache *cache = m68k_frame_cache (this_frame, this_cache);
968
969 gdb_assert (regnum >= 0);
970
971 if (regnum == M68K_SP_REGNUM && cache->saved_sp)
972 return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
973
974 if (regnum < M68K_NUM_REGS && cache->saved_regs[regnum] != -1)
975 return frame_unwind_got_memory (this_frame, regnum,
976 cache->saved_regs[regnum]);
977
978 return frame_unwind_got_register (this_frame, regnum, regnum);
979 }
980
981 static const struct frame_unwind m68k_frame_unwind =
982 {
983 NORMAL_FRAME,
984 default_frame_unwind_stop_reason,
985 m68k_frame_this_id,
986 m68k_frame_prev_register,
987 NULL,
988 default_frame_sniffer
989 };
990
991 static CORE_ADDR
993 m68k_frame_base_address (struct frame_info *this_frame, void **this_cache)
994 {
995 struct m68k_frame_cache *cache = m68k_frame_cache (this_frame, this_cache);
996
997 return cache->base;
998 }
999
1000 static const struct frame_base m68k_frame_base =
1001 {
1002 &m68k_frame_unwind,
1003 m68k_frame_base_address,
1004 m68k_frame_base_address,
1005 m68k_frame_base_address
1006 };
1007
1008 static struct frame_id
1009 m68k_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
1010 {
1011 CORE_ADDR fp;
1012
1013 fp = get_frame_register_unsigned (this_frame, M68K_FP_REGNUM);
1014
1015 /* See the end of m68k_push_dummy_call. */
1016 return frame_id_build (fp + 8, get_frame_pc (this_frame));
1017 }
1018
1019
1021 /* Figure out where the longjmp will land. Slurp the args out of the stack.
1022 We expect the first arg to be a pointer to the jmp_buf structure from which
1023 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
1024 This routine returns true on success. */
1025
1026 static int
1027 m68k_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
1028 {
1029 gdb_byte *buf;
1030 CORE_ADDR sp, jb_addr;
1031 struct gdbarch *gdbarch = get_frame_arch (frame);
1032 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1033 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1034
1035 if (tdep->jb_pc < 0)
1036 {
1037 internal_error (__FILE__, __LINE__,
1038 _("m68k_get_longjmp_target: not implemented"));
1039 return 0;
1040 }
1041
1042 buf = alloca (gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT);
1043 sp = get_frame_register_unsigned (frame, gdbarch_sp_regnum (gdbarch));
1044
1045 if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack. */
1046 buf, gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT))
1047 return 0;
1048
1049 jb_addr = extract_unsigned_integer (buf, gdbarch_ptr_bit (gdbarch)
1050 / TARGET_CHAR_BIT, byte_order);
1051
1052 if (target_read_memory (jb_addr + tdep->jb_pc * tdep->jb_elt_size, buf,
1053 gdbarch_ptr_bit (gdbarch) / TARGET_CHAR_BIT),
1054 byte_order)
1055 return 0;
1056
1057 *pc = extract_unsigned_integer (buf, gdbarch_ptr_bit (gdbarch)
1058 / TARGET_CHAR_BIT, byte_order);
1059 return 1;
1060 }
1061
1062
1064 /* This is the implementation of gdbarch method
1065 return_in_first_hidden_param_p. */
1066
1067 static int
1068 m68k_return_in_first_hidden_param_p (struct gdbarch *gdbarch,
1069 struct type *type)
1070 {
1071 return 0;
1072 }
1073
1074 /* System V Release 4 (SVR4). */
1075
1076 void
1077 m68k_svr4_init_abi (struct gdbarch_info info, struct gdbarch *gdbarch)
1078 {
1079 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1080
1081 /* SVR4 uses a different calling convention. */
1082 set_gdbarch_return_value (gdbarch, m68k_svr4_return_value);
1083
1084 /* SVR4 uses %a0 instead of %a1. */
1085 tdep->struct_value_regnum = M68K_A0_REGNUM;
1086 }
1087
1088
1090 /* Function: m68k_gdbarch_init
1091 Initializer function for the m68k gdbarch vector.
1092 Called by gdbarch. Sets up the gdbarch vector(s) for this target. */
1093
1094 static struct gdbarch *
1095 m68k_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1096 {
1097 struct gdbarch_tdep *tdep = NULL;
1098 struct gdbarch *gdbarch;
1099 struct gdbarch_list *best_arch;
1100 struct tdesc_arch_data *tdesc_data = NULL;
1101 int i;
1102 enum m68k_flavour flavour = m68k_no_flavour;
1103 int has_fp = 1;
1104 const struct floatformat **long_double_format = floatformats_m68881_ext;
1105
1106 /* Check any target description for validity. */
1107 if (tdesc_has_registers (info.target_desc))
1108 {
1109 const struct tdesc_feature *feature;
1110 int valid_p;
1111
1112 feature = tdesc_find_feature (info.target_desc,
1113 "org.gnu.gdb.m68k.core");
1114
1115 if (feature == NULL)
1116 {
1117 feature = tdesc_find_feature (info.target_desc,
1118 "org.gnu.gdb.coldfire.core");
1119 if (feature != NULL)
1120 flavour = m68k_coldfire_flavour;
1121 }
1122
1123 if (feature == NULL)
1124 {
1125 feature = tdesc_find_feature (info.target_desc,
1126 "org.gnu.gdb.fido.core");
1127 if (feature != NULL)
1128 flavour = m68k_fido_flavour;
1129 }
1130
1131 if (feature == NULL)
1132 return NULL;
1133
1134 tdesc_data = tdesc_data_alloc ();
1135
1136 valid_p = 1;
1137 for (i = 0; i <= M68K_PC_REGNUM; i++)
1138 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
1139 m68k_register_names[i]);
1140
1141 if (!valid_p)
1142 {
1143 tdesc_data_cleanup (tdesc_data);
1144 return NULL;
1145 }
1146
1147 feature = tdesc_find_feature (info.target_desc,
1148 "org.gnu.gdb.coldfire.fp");
1149 if (feature != NULL)
1150 {
1151 valid_p = 1;
1152 for (i = M68K_FP0_REGNUM; i <= M68K_FPI_REGNUM; i++)
1153 valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
1154 m68k_register_names[i]);
1155 if (!valid_p)
1156 {
1157 tdesc_data_cleanup (tdesc_data);
1158 return NULL;
1159 }
1160 }
1161 else
1162 has_fp = 0;
1163 }
1164
1165 /* The mechanism for returning floating values from function
1166 and the type of long double depend on whether we're
1167 on ColdFire or standard m68k. */
1168
1169 if (info.bfd_arch_info && info.bfd_arch_info->mach != 0)
1170 {
1171 const bfd_arch_info_type *coldfire_arch =
1172 bfd_lookup_arch (bfd_arch_m68k, bfd_mach_mcf_isa_a_nodiv);
1173
1174 if (coldfire_arch
1175 && ((*info.bfd_arch_info->compatible)
1176 (info.bfd_arch_info, coldfire_arch)))
1177 flavour = m68k_coldfire_flavour;
1178 }
1179
1180 /* If there is already a candidate, use it. */
1181 for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
1182 best_arch != NULL;
1183 best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
1184 {
1185 if (flavour != gdbarch_tdep (best_arch->gdbarch)->flavour)
1186 continue;
1187
1188 if (has_fp != gdbarch_tdep (best_arch->gdbarch)->fpregs_present)
1189 continue;
1190
1191 break;
1192 }
1193
1194 if (best_arch != NULL)
1195 {
1196 if (tdesc_data != NULL)
1197 tdesc_data_cleanup (tdesc_data);
1198 return best_arch->gdbarch;
1199 }
1200
1201 tdep = xzalloc (sizeof (struct gdbarch_tdep));
1202 gdbarch = gdbarch_alloc (&info, tdep);
1203 tdep->fpregs_present = has_fp;
1204 tdep->flavour = flavour;
1205
1206 if (flavour == m68k_coldfire_flavour || flavour == m68k_fido_flavour)
1207 long_double_format = floatformats_ieee_double;
1208 set_gdbarch_long_double_format (gdbarch, long_double_format);
1209 set_gdbarch_long_double_bit (gdbarch, long_double_format[0]->totalsize);
1210
1211 set_gdbarch_skip_prologue (gdbarch, m68k_skip_prologue);
1212 set_gdbarch_breakpoint_from_pc (gdbarch, m68k_local_breakpoint_from_pc);
1213
1214 /* Stack grows down. */
1215 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1216 set_gdbarch_frame_align (gdbarch, m68k_frame_align);
1217
1218 set_gdbarch_believe_pcc_promotion (gdbarch, 1);
1219 if (flavour == m68k_coldfire_flavour || flavour == m68k_fido_flavour)
1220 set_gdbarch_decr_pc_after_break (gdbarch, 2);
1221
1222 set_gdbarch_frame_args_skip (gdbarch, 8);
1223 set_gdbarch_dwarf2_reg_to_regnum (gdbarch, m68k_dwarf_reg_to_regnum);
1224
1225 set_gdbarch_register_type (gdbarch, m68k_register_type);
1226 set_gdbarch_register_name (gdbarch, m68k_register_name);
1227 set_gdbarch_num_regs (gdbarch, M68K_NUM_REGS);
1228 set_gdbarch_sp_regnum (gdbarch, M68K_SP_REGNUM);
1229 set_gdbarch_pc_regnum (gdbarch, M68K_PC_REGNUM);
1230 set_gdbarch_ps_regnum (gdbarch, M68K_PS_REGNUM);
1231 set_gdbarch_convert_register_p (gdbarch, m68k_convert_register_p);
1232 set_gdbarch_register_to_value (gdbarch, m68k_register_to_value);
1233 set_gdbarch_value_to_register (gdbarch, m68k_value_to_register);
1234
1235 if (has_fp)
1236 set_gdbarch_fp0_regnum (gdbarch, M68K_FP0_REGNUM);
1237
1238 /* Try to figure out if the arch uses floating registers to return
1239 floating point values from functions. */
1240 if (has_fp)
1241 {
1242 /* On ColdFire, floating point values are returned in D0. */
1243 if (flavour == m68k_coldfire_flavour)
1244 tdep->float_return = 0;
1245 else
1246 tdep->float_return = 1;
1247 }
1248 else
1249 {
1250 /* No floating registers, so can't use them for returning values. */
1251 tdep->float_return = 0;
1252 }
1253
1254 /* Function call & return. */
1255 set_gdbarch_push_dummy_call (gdbarch, m68k_push_dummy_call);
1256 set_gdbarch_return_value (gdbarch, m68k_return_value);
1257 set_gdbarch_return_in_first_hidden_param_p (gdbarch,
1258 m68k_return_in_first_hidden_param_p);
1259
1260
1261 /* Disassembler. */
1262 set_gdbarch_print_insn (gdbarch, print_insn_m68k);
1263
1264 #if defined JB_PC && defined JB_ELEMENT_SIZE
1265 tdep->jb_pc = JB_PC;
1266 tdep->jb_elt_size = JB_ELEMENT_SIZE;
1267 #else
1268 tdep->jb_pc = -1;
1269 #endif
1270 tdep->struct_value_regnum = M68K_A1_REGNUM;
1271 tdep->struct_return = reg_struct_return;
1272
1273 /* Frame unwinder. */
1274 set_gdbarch_dummy_id (gdbarch, m68k_dummy_id);
1275 set_gdbarch_unwind_pc (gdbarch, m68k_unwind_pc);
1276
1277 /* Hook in the DWARF CFI frame unwinder. */
1278 dwarf2_append_unwinders (gdbarch);
1279
1280 frame_base_set_default (gdbarch, &m68k_frame_base);
1281
1282 /* Hook in ABI-specific overrides, if they have been registered. */
1283 gdbarch_init_osabi (info, gdbarch);
1284
1285 /* Now we have tuned the configuration, set a few final things,
1286 based on what the OS ABI has told us. */
1287
1288 if (tdep->jb_pc >= 0)
1289 set_gdbarch_get_longjmp_target (gdbarch, m68k_get_longjmp_target);
1290
1291 frame_unwind_append_unwinder (gdbarch, &m68k_frame_unwind);
1292
1293 if (tdesc_data)
1294 tdesc_use_registers (gdbarch, info.target_desc, tdesc_data);
1295
1296 return gdbarch;
1297 }
1298
1299
1300 static void
1301 m68k_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
1302 {
1303 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
1304
1305 if (tdep == NULL)
1306 return;
1307 }
1308
1309 extern initialize_file_ftype _initialize_m68k_tdep; /* -Wmissing-prototypes */
1310
1311 void
1312 _initialize_m68k_tdep (void)
1313 {
1314 gdbarch_register (bfd_arch_m68k, m68k_gdbarch_init, m68k_dump_tdep);
1315 }
1316