Commit | Line | Data |
---|---|---|
a332e593 | 1 | /* Target-machine dependent code for Zilog Z8000, for GDB. |
669caa9c | 2 | Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc. |
a332e593 SC |
3 | |
4 | This file is part of GDB. | |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
e4ebb8e5 | 20 | /* |
a332e593 | 21 | Contributed by Steve Chamberlain |
e4ebb8e5 | 22 | sac@cygnus.com |
a332e593 SC |
23 | */ |
24 | ||
a332e593 SC |
25 | #include "defs.h" |
26 | #include "frame.h" | |
27 | #include "obstack.h" | |
28 | #include "symtab.h" | |
e4ebb8e5 | 29 | #include "gdbcmd.h" |
a332e593 | 30 | #include "gdbtypes.h" |
52f8e6a0 | 31 | #include "dis-asm.h" |
669caa9c | 32 | |
a332e593 SC |
33 | /* Return the saved PC from this frame. |
34 | ||
35 | If the frame has a memory copy of SRP_REGNUM, use that. If not, | |
36 | just use the register SRP_REGNUM itself. */ | |
37 | ||
38 | CORE_ADDR | |
39 | frame_saved_pc (frame) | |
669caa9c | 40 | struct frame_info *frame; |
a332e593 | 41 | { |
669caa9c | 42 | return read_memory_pointer (frame->frame + (BIG ? 4 : 2)); |
a332e593 SC |
43 | } |
44 | ||
45 | #define IS_PUSHL(x) (BIG ? ((x & 0xfff0) == 0x91e0):((x & 0xfff0) == 0x91F0)) | |
46 | #define IS_PUSHW(x) (BIG ? ((x & 0xfff0) == 0x93e0):((x & 0xfff0)==0x93f0)) | |
47 | #define IS_MOVE_FP(x) (BIG ? x == 0xa1ea : x == 0xa1fa) | |
48 | #define IS_MOV_SP_FP(x) (BIG ? x == 0x94ea : x == 0x0d76) | |
49 | #define IS_SUB2_SP(x) (x==0x1b87) | |
50 | #define IS_MOVK_R5(x) (x==0x7905) | |
51 | #define IS_SUB_SP(x) ((x & 0xffff) == 0x020f) | |
52 | #define IS_PUSH_FP(x) (BIG ? (x == 0x93ea) : (x == 0x93fa)) | |
53 | ||
e4ebb8e5 | 54 | /* work out how much local space is on the stack and |
a332e593 SC |
55 | return the pc pointing to the first push */ |
56 | ||
e4ebb8e5 SC |
57 | static CORE_ADDR |
58 | skip_adjust (pc, size) | |
59 | CORE_ADDR pc; | |
60 | int *size; | |
a332e593 SC |
61 | { |
62 | *size = 0; | |
63 | ||
e4ebb8e5 SC |
64 | if (IS_PUSH_FP (read_memory_short (pc)) |
65 | && IS_MOV_SP_FP (read_memory_short (pc + 2))) | |
66 | { | |
67 | /* This is a function with an explict frame pointer */ | |
68 | pc += 4; | |
69 | *size += 2; /* remember the frame pointer */ | |
70 | } | |
a332e593 SC |
71 | |
72 | /* remember any stack adjustment */ | |
e4ebb8e5 SC |
73 | if (IS_SUB_SP (read_memory_short (pc))) |
74 | { | |
75 | *size += read_memory_short (pc + 2); | |
76 | pc += 4; | |
77 | } | |
a332e593 SC |
78 | return pc; |
79 | } | |
80 | ||
a332e593 | 81 | int |
e4ebb8e5 SC |
82 | examine_frame (pc, regs, sp) |
83 | CORE_ADDR pc; | |
84 | struct frame_saved_regs *regs; | |
85 | CORE_ADDR sp; | |
a332e593 | 86 | { |
e4ebb8e5 SC |
87 | int w = read_memory_short (pc); |
88 | int offset = 0; | |
89 | int regno; | |
a332e593 | 90 | |
e4ebb8e5 | 91 | for (regno = 0; regno < NUM_REGS; regno++) |
b2ff9b68 | 92 | regs->regs[regno] = 0; |
a332e593 | 93 | |
e4ebb8e5 | 94 | while (IS_PUSHW (w) || IS_PUSHL (w)) |
e4ebb8e5 | 95 | { |
b2ff9b68 SC |
96 | /* work out which register is being pushed to where */ |
97 | if (IS_PUSHL (w)) | |
98 | { | |
99 | regs->regs[w & 0xf] = offset; | |
100 | regs->regs[(w & 0xf) + 1] = offset + 2; | |
101 | offset += 4; | |
102 | } | |
103 | else | |
104 | { | |
105 | regs->regs[w & 0xf] = offset; | |
106 | offset += 2; | |
107 | } | |
108 | pc += 2; | |
109 | w = read_memory_short (pc); | |
a332e593 | 110 | } |
a332e593 | 111 | |
e4ebb8e5 | 112 | if (IS_MOVE_FP (w)) |
b2ff9b68 SC |
113 | { |
114 | /* We know the fp */ | |
e4ebb8e5 | 115 | |
b2ff9b68 | 116 | } |
e4ebb8e5 | 117 | else if (IS_SUB_SP (w)) |
b2ff9b68 SC |
118 | { |
119 | /* Subtracting a value from the sp, so were in a function | |
e4ebb8e5 SC |
120 | which needs stack space for locals, but has no fp. We fake up |
121 | the values as if we had an fp */ | |
b2ff9b68 SC |
122 | regs->regs[FP_REGNUM] = sp; |
123 | } | |
e4ebb8e5 | 124 | else |
b2ff9b68 SC |
125 | { |
126 | /* This one didn't have an fp, we'll fake it up */ | |
127 | regs->regs[SP_REGNUM] = sp; | |
128 | } | |
e4ebb8e5 SC |
129 | /* stack pointer contains address of next frame */ |
130 | /* regs->regs[fp_regnum()] = fp;*/ | |
a332e593 SC |
131 | regs->regs[SP_REGNUM] = sp; |
132 | return pc; | |
133 | } | |
134 | ||
b2ff9b68 | 135 | CORE_ADDR |
e4ebb8e5 SC |
136 | z8k_skip_prologue (start_pc) |
137 | CORE_ADDR start_pc; | |
a332e593 SC |
138 | { |
139 | struct frame_saved_regs dummy; | |
e4ebb8e5 SC |
140 | |
141 | return examine_frame (start_pc, &dummy, 0); | |
a332e593 SC |
142 | } |
143 | ||
b2ff9b68 | 144 | CORE_ADDR |
e4ebb8e5 SC |
145 | addr_bits_remove (x) |
146 | CORE_ADDR x; | |
a332e593 SC |
147 | { |
148 | return x & PTR_MASK; | |
149 | } | |
150 | ||
669caa9c | 151 | int |
e4ebb8e5 SC |
152 | read_memory_pointer (x) |
153 | CORE_ADDR x; | |
a332e593 | 154 | { |
e4ebb8e5 | 155 | return read_memory_integer (ADDR_BITS_REMOVE (x), BIG ? 4 : 2); |
a332e593 SC |
156 | } |
157 | ||
669caa9c | 158 | CORE_ADDR |
a332e593 | 159 | frame_chain (thisframe) |
669caa9c | 160 | struct frame_info *thisframe; |
a332e593 | 161 | { |
e4ebb8e5 SC |
162 | if (thisframe->prev == 0) |
163 | { | |
164 | /* This is the top of the stack, let's get the sp for real */ | |
165 | } | |
669caa9c | 166 | if (!inside_entry_file (thisframe->pc)) |
e4ebb8e5 | 167 | { |
669caa9c | 168 | return read_memory_pointer (thisframe->frame); |
e4ebb8e5 | 169 | } |
a332e593 SC |
170 | return 0; |
171 | } | |
172 | ||
e4ebb8e5 SC |
173 | init_frame_pc () |
174 | { | |
175 | abort (); | |
176 | } | |
a332e593 SC |
177 | |
178 | /* Put here the code to store, into a struct frame_saved_regs, | |
179 | the addresses of the saved registers of frame described by FRAME_INFO. | |
180 | This includes special registers such as pc and fp saved in special | |
181 | ways in the stack frame. sp is even more special: | |
182 | the address we return for it IS the sp for the next frame. */ | |
183 | ||
b2ff9b68 | 184 | void |
e4ebb8e5 | 185 | get_frame_saved_regs (frame_info, frame_saved_regs) |
a332e593 SC |
186 | struct frame_info *frame_info; |
187 | struct frame_saved_regs *frame_saved_regs; | |
188 | ||
189 | { | |
e4ebb8e5 SC |
190 | CORE_ADDR pc; |
191 | int w; | |
192 | ||
4ed97c9a | 193 | memset (frame_saved_regs, '\0', sizeof (*frame_saved_regs)); |
e4ebb8e5 | 194 | pc = get_pc_function_start (frame_info->pc); |
a332e593 SC |
195 | |
196 | /* wander down the instruction stream */ | |
e4ebb8e5 | 197 | examine_frame (pc, frame_saved_regs, frame_info->frame); |
a332e593 SC |
198 | |
199 | } | |
200 | ||
b2ff9b68 | 201 | void |
e4ebb8e5 SC |
202 | z8k_push_dummy_frame () |
203 | { | |
204 | abort (); | |
a332e593 | 205 | } |
a332e593 | 206 | |
b2ff9b68 | 207 | int |
e4ebb8e5 SC |
208 | print_insn (memaddr, stream) |
209 | CORE_ADDR memaddr; | |
199b2450 | 210 | GDB_FILE *stream; |
a332e593 | 211 | { |
00cea52f PB |
212 | disassemble_info info; |
213 | ||
669caa9c | 214 | GDB_INIT_DISASSEMBLE_INFO (info, stream); |
a332e593 | 215 | |
e4ebb8e5 SC |
216 | if (BIG) |
217 | { | |
337fd006 | 218 | return print_insn_z8001 ((bfd_vma) memaddr, &info); |
e4ebb8e5 SC |
219 | } |
220 | else | |
221 | { | |
337fd006 | 222 | return print_insn_z8002 ((bfd_vma) memaddr, &info); |
e4ebb8e5 | 223 | } |
a332e593 | 224 | } |
a332e593 SC |
225 | |
226 | /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or | |
227 | is not the address of a valid instruction, the address of the next | |
228 | instruction beyond ADDR otherwise. *PWORD1 receives the first word | |
229 | of the instruction.*/ | |
230 | ||
a332e593 | 231 | CORE_ADDR |
e4ebb8e5 SC |
232 | NEXT_PROLOGUE_INSN (addr, lim, pword1) |
233 | CORE_ADDR addr; | |
234 | CORE_ADDR lim; | |
235 | short *pword1; | |
a332e593 | 236 | { |
34df79fc | 237 | char buf[2]; |
e4ebb8e5 SC |
238 | if (addr < lim + 8) |
239 | { | |
34df79fc JK |
240 | read_memory (addr, buf, 2); |
241 | *pword1 = extract_signed_integer (buf, 2); | |
e4ebb8e5 SC |
242 | |
243 | return addr + 2; | |
244 | } | |
a332e593 | 245 | return 0; |
a332e593 SC |
246 | } |
247 | ||
a332e593 SC |
248 | /* Put here the code to store, into a struct frame_saved_regs, |
249 | the addresses of the saved registers of frame described by FRAME_INFO. | |
250 | This includes special registers such as pc and fp saved in special | |
251 | ways in the stack frame. sp is even more special: | |
252 | the address we return for it IS the sp for the next frame. | |
253 | ||
254 | We cache the result of doing this in the frame_cache_obstack, since | |
255 | it is fairly expensive. */ | |
256 | ||
257 | void | |
258 | frame_find_saved_regs (fip, fsrp) | |
259 | struct frame_info *fip; | |
260 | struct frame_saved_regs *fsrp; | |
261 | { | |
262 | int locals; | |
263 | CORE_ADDR pc; | |
264 | CORE_ADDR adr; | |
265 | int i; | |
e4ebb8e5 | 266 | |
a332e593 SC |
267 | memset (fsrp, 0, sizeof *fsrp); |
268 | ||
e4ebb8e5 SC |
269 | pc = skip_adjust (get_pc_function_start (fip->pc), &locals); |
270 | ||
271 | { | |
669caa9c | 272 | adr = FRAME_FP (fip) - locals; |
e4ebb8e5 SC |
273 | for (i = 0; i < 8; i++) |
274 | { | |
275 | int word = read_memory_short (pc); | |
276 | ||
277 | pc += 2; | |
278 | if (IS_PUSHL (word)) | |
279 | { | |
280 | fsrp->regs[word & 0xf] = adr; | |
281 | fsrp->regs[(word & 0xf) + 1] = adr - 2; | |
282 | adr -= 4; | |
283 | } | |
284 | else if (IS_PUSHW (word)) | |
285 | { | |
286 | fsrp->regs[word & 0xf] = adr; | |
287 | adr -= 2; | |
288 | } | |
289 | else | |
290 | break; | |
291 | } | |
292 | ||
293 | } | |
294 | ||
a332e593 SC |
295 | fsrp->regs[PC_REGNUM] = fip->frame + 4; |
296 | fsrp->regs[FP_REGNUM] = fip->frame; | |
297 | ||
298 | } | |
299 | ||
a332e593 | 300 | int |
e4ebb8e5 SC |
301 | saved_pc_after_call () |
302 | { | |
2d8d693a SC |
303 | return addr_bits_remove |
304 | (read_memory_integer (read_register (SP_REGNUM), PTR_SIZE)); | |
305 | } | |
306 | ||
307 | ||
669caa9c SS |
308 | extract_return_value (type, regbuf, valbuf) |
309 | struct type *type; | |
310 | char *regbuf; | |
311 | char *valbuf; | |
2d8d693a SC |
312 | { |
313 | int b; | |
669caa9c | 314 | int len = TYPE_LENGTH (type); |
2d8d693a | 315 | |
669caa9c SS |
316 | for (b = 0; b < len; b += 2) |
317 | { | |
318 | int todo = len - b; | |
319 | ||
320 | if (todo > 2) | |
321 | todo = 2; | |
322 | memcpy (valbuf + b, regbuf + b, todo); | |
323 | } | |
a332e593 SC |
324 | } |
325 | ||
2d8d693a | 326 | void |
669caa9c SS |
327 | write_return_value (type, valbuf) |
328 | struct type *type; | |
329 | char *valbuf; | |
2d8d693a SC |
330 | { |
331 | int reg; | |
332 | int len; | |
669caa9c SS |
333 | |
334 | for (len = 0; len < TYPE_LENGTH (type); len += 2) | |
335 | write_register_bytes (REGISTER_BYTE (len / 2 + 2), valbuf + len, 2); | |
2d8d693a SC |
336 | } |
337 | ||
338 | void | |
669caa9c SS |
339 | store_struct_return (addr, sp) |
340 | CORE_ADDR addr; | |
341 | CORE_ADDR sp; | |
2d8d693a | 342 | { |
669caa9c | 343 | write_register (2, addr); |
2d8d693a SC |
344 | } |
345 | ||
346 | ||
a332e593 | 347 | void |
e4ebb8e5 SC |
348 | print_register_hook (regno) |
349 | int regno; | |
a332e593 | 350 | { |
e4ebb8e5 SC |
351 | if ((regno & 1) == 0 && regno < 16) |
352 | { | |
353 | unsigned short l[2]; | |
a332e593 | 354 | |
e4ebb8e5 SC |
355 | read_relative_register_raw_bytes (regno, (char *) (l + 0)); |
356 | read_relative_register_raw_bytes (regno + 1, (char *) (l + 1)); | |
199b2450 TL |
357 | printf_unfiltered ("\t"); |
358 | printf_unfiltered ("%04x%04x", l[0], l[1]); | |
e4ebb8e5 SC |
359 | } |
360 | ||
361 | if ((regno & 3) == 0 && regno < 16) | |
362 | { | |
363 | unsigned short l[4]; | |
364 | ||
b2ff9b68 SC |
365 | read_relative_register_raw_bytes (regno, (char *) (l + 0)); |
366 | read_relative_register_raw_bytes (regno + 1, (char *) (l + 1)); | |
367 | read_relative_register_raw_bytes (regno + 2, (char *) (l + 2)); | |
368 | read_relative_register_raw_bytes (regno + 3, (char *) (l + 3)); | |
a332e593 | 369 | |
199b2450 TL |
370 | printf_unfiltered ("\t"); |
371 | printf_unfiltered ("%04x%04x%04x%04x", l[0], l[1], l[2], l[3]); | |
a332e593 | 372 | } |
e4ebb8e5 SC |
373 | if (regno == 15) |
374 | { | |
375 | unsigned short rval; | |
376 | int i; | |
a332e593 | 377 | |
e4ebb8e5 SC |
378 | read_relative_register_raw_bytes (regno, (char *) (&rval)); |
379 | ||
199b2450 | 380 | printf_unfiltered ("\n"); |
e4ebb8e5 SC |
381 | for (i = 0; i < 10; i += 2) |
382 | { | |
199b2450 | 383 | printf_unfiltered ("(sp+%d=%04x)", i, read_memory_short (rval + i)); |
e4ebb8e5 SC |
384 | } |
385 | } | |
386 | ||
387 | } | |
a332e593 | 388 | |
b2ff9b68 | 389 | void |
e4ebb8e5 | 390 | z8k_pop_frame () |
a332e593 | 391 | { |
a332e593 SC |
392 | } |
393 | ||
e4ebb8e5 SC |
394 | struct cmd_list_element *setmemorylist; |
395 | ||
b2ff9b68 | 396 | void |
e4ebb8e5 SC |
397 | z8k_set_pointer_size (newsize) |
398 | int newsize; | |
399 | { | |
400 | static int oldsize = 0; | |
a332e593 | 401 | |
e4ebb8e5 SC |
402 | if (oldsize != newsize) |
403 | { | |
199b2450 | 404 | printf_unfiltered ("pointer size set to %d bits\n", newsize); |
e4ebb8e5 SC |
405 | oldsize = newsize; |
406 | if (newsize == 32) | |
407 | { | |
408 | BIG = 1; | |
409 | } | |
410 | else | |
411 | { | |
412 | BIG = 0; | |
413 | } | |
414 | _initialize_gdbtypes (); | |
415 | } | |
416 | } | |
a332e593 | 417 | |
e4ebb8e5 SC |
418 | static void |
419 | segmented_command (args, from_tty) | |
420 | char *args; | |
421 | int from_tty; | |
422 | { | |
2d8d693a | 423 | z8k_set_pointer_size (32); |
e4ebb8e5 SC |
424 | } |
425 | ||
426 | static void | |
427 | unsegmented_command (args, from_tty) | |
428 | char *args; | |
429 | int from_tty; | |
430 | { | |
431 | z8k_set_pointer_size (16); | |
e4ebb8e5 SC |
432 | } |
433 | ||
434 | static void | |
435 | set_memory (args, from_tty) | |
436 | char *args; | |
437 | int from_tty; | |
438 | { | |
199b2450 TL |
439 | printf_unfiltered ("\"set memory\" must be followed by the name of a memory subcommand.\n"); |
440 | help_list (setmemorylist, "set memory ", -1, gdb_stdout); | |
e4ebb8e5 SC |
441 | } |
442 | ||
976bb0be | 443 | void |
e4ebb8e5 SC |
444 | _initialize_z8ktdep () |
445 | { | |
446 | add_prefix_cmd ("memory", no_class, set_memory, | |
447 | "set the memory model", &setmemorylist, "set memory ", 0, | |
448 | &setlist); | |
449 | add_cmd ("segmented", class_support, segmented_command, | |
450 | "Set segmented memory model.", &setmemorylist); | |
451 | add_cmd ("unsegmented", class_support, unsegmented_command, | |
452 | "Set unsegmented memory model.", &setmemorylist); | |
453 | ||
454 | } |