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