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