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