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