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