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