provide a new interface (using read_memory_func) to call the disassemblers
[binutils-gdb.git] / gdb / z8k-tdep.c
1 /* Target-machine dependent code for Zilog Z8000, for GDB.
2 Copyright (C) 1992,1993 Free Software Foundation, Inc.
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
20 /*
21 Contributed by Steve Chamberlain
22 sac@cygnus.com
23 */
24
25 #include "defs.h"
26 #include "frame.h"
27 #include "obstack.h"
28 #include "symtab.h"
29 #include "gdbcmd.h"
30 #include "gdbtypes.h"
31 #include "dis-asm.h"
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)
39 FRAME frame;
40 {
41 return (read_memory_pointer (frame->frame + (BIG ? 4 : 2)));
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
53 /* work out how much local space is on the stack and
54 return the pc pointing to the first push */
55
56 static CORE_ADDR
57 skip_adjust (pc, size)
58 CORE_ADDR pc;
59 int *size;
60 {
61 *size = 0;
62
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 }
70
71 /* remember any stack adjustment */
72 if (IS_SUB_SP (read_memory_short (pc)))
73 {
74 *size += read_memory_short (pc + 2);
75 pc += 4;
76 }
77 return pc;
78 }
79
80 int
81 examine_frame (pc, regs, sp)
82 CORE_ADDR pc;
83 struct frame_saved_regs *regs;
84 CORE_ADDR sp;
85 {
86 int w = read_memory_short (pc);
87 int offset = 0;
88 int regno;
89
90 for (regno = 0; regno < NUM_REGS; regno++)
91 regs->regs[regno] = 0;
92
93 while (IS_PUSHW (w) || IS_PUSHL (w))
94 {
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);
109 }
110
111 if (IS_MOVE_FP (w))
112 {
113 /* We know the fp */
114
115 }
116 else if (IS_SUB_SP (w))
117 {
118 /* Subtracting a value from the sp, so were in a function
119 which needs stack space for locals, but has no fp. We fake up
120 the values as if we had an fp */
121 regs->regs[FP_REGNUM] = sp;
122 }
123 else
124 {
125 /* This one didn't have an fp, we'll fake it up */
126 regs->regs[SP_REGNUM] = sp;
127 }
128 /* stack pointer contains address of next frame */
129 /* regs->regs[fp_regnum()] = fp;*/
130 regs->regs[SP_REGNUM] = sp;
131 return pc;
132 }
133
134 CORE_ADDR
135 z8k_skip_prologue (start_pc)
136 CORE_ADDR start_pc;
137 {
138 struct frame_saved_regs dummy;
139
140 return examine_frame (start_pc, &dummy, 0);
141 }
142
143 CORE_ADDR
144 addr_bits_remove (x)
145 CORE_ADDR x;
146 {
147 return x & PTR_MASK;
148 }
149
150 read_memory_pointer (x)
151 CORE_ADDR x;
152 {
153
154 return read_memory_integer (ADDR_BITS_REMOVE (x), BIG ? 4 : 2);
155 }
156
157 FRAME_ADDR
158 frame_chain (thisframe)
159 FRAME thisframe;
160 {
161 if (thisframe->prev == 0)
162 {
163 /* This is the top of the stack, let's get the sp for real */
164 }
165 if (!inside_entry_file ((thisframe)->pc))
166 {
167 return read_memory_pointer ((thisframe)->frame);
168 }
169 return 0;
170 }
171
172 init_frame_pc ()
173 {
174 abort ();
175 }
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
183 void
184 get_frame_saved_regs (frame_info, frame_saved_regs)
185 struct frame_info *frame_info;
186 struct frame_saved_regs *frame_saved_regs;
187
188 {
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);
194
195 /* wander down the instruction stream */
196 examine_frame (pc, frame_saved_regs, frame_info->frame);
197
198 }
199
200 void
201 z8k_push_dummy_frame ()
202 {
203 abort ();
204 }
205
206 int
207 print_insn (memaddr, stream)
208 CORE_ADDR memaddr;
209 FILE *stream;
210 {
211 disassemble_info info;
212
213 GDB_INIT_DISASSEMBLE_INFO(info, stream);
214
215 if (BIG)
216 {
217 return print_insn_z8001 (memaddr, &info);
218 }
219 else
220 {
221 return print_insn_z8002 (memaddr, &info);
222 }
223 }
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
230 CORE_ADDR
231 NEXT_PROLOGUE_INSN (addr, lim, pword1)
232 CORE_ADDR addr;
233 CORE_ADDR lim;
234 short *pword1;
235 {
236 if (addr < lim + 8)
237 {
238 read_memory (addr, pword1, sizeof (*pword1));
239 SWAP_TARGET_AND_HOST (pword1, sizeof (short));
240
241 return addr + 2;
242 }
243
244 return 0;
245
246 }
247
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;
266
267 memset (fsrp, 0, sizeof *fsrp);
268
269 pc = skip_adjust (get_pc_function_start (fip->pc), &locals);
270
271 {
272 adr = fip->frame - locals;
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
295 fsrp->regs[PC_REGNUM] = fip->frame + 4;
296 fsrp->regs[FP_REGNUM] = fip->frame;
297
298 }
299
300 void
301 addr_bits_set ()
302 {
303 abort ();
304 }
305
306 int
307 saved_pc_after_call ()
308 {
309 return addr_bits_remove
310 (read_memory_integer (read_register (SP_REGNUM), PTR_SIZE));
311 }
312
313
314 extract_return_value(type, regbuf, valbuf)
315 struct type *type;
316 char *regbuf;
317 char *valbuf;
318 {
319 int b;
320 int len = TYPE_LENGTH(type);
321
322 for (b = 0; b < len; b += 2) {
323 int todo = len - b;
324 if (todo > 2)
325 todo = 2;
326 memcpy(valbuf + b, regbuf + b, todo);
327 }
328 }
329
330 void
331 write_return_value(type, valbuf)
332 struct type *type;
333 char *valbuf;
334 {
335 int reg;
336 int len;
337 for (len = 0; len < TYPE_LENGTH(type); len += 2)
338 {
339 write_register_bytes(REGISTER_BYTE(len /2 + 2), valbuf + len, 2);
340 }
341 }
342
343 void
344 store_struct_return(addr, sp)
345 CORE_ADDR addr;
346 CORE_ADDR sp;
347 {
348 write_register(2, addr);
349 }
350
351
352 void
353 print_register_hook (regno)
354 int regno;
355 {
356 if ((regno & 1) == 0 && regno < 16)
357 {
358 unsigned short l[2];
359
360 read_relative_register_raw_bytes (regno, (char *) (l + 0));
361 read_relative_register_raw_bytes (regno + 1, (char *) (l + 1));
362 printf ("\t");
363 printf ("%04x%04x", l[0], l[1]);
364 }
365
366 if ((regno & 3) == 0 && regno < 16)
367 {
368 unsigned short l[4];
369
370 read_relative_register_raw_bytes (regno, (char *) (l + 0));
371 read_relative_register_raw_bytes (regno + 1, (char *) (l + 1));
372 read_relative_register_raw_bytes (regno + 2, (char *) (l + 2));
373 read_relative_register_raw_bytes (regno + 3, (char *) (l + 3));
374
375 printf ("\t");
376 printf ("%04x%04x%04x%04x", l[0], l[1], l[2], l[3]);
377 }
378 if (regno == 15)
379 {
380 unsigned short rval;
381 int i;
382
383 read_relative_register_raw_bytes (regno, (char *) (&rval));
384
385 printf ("\n");
386 for (i = 0; i < 10; i += 2)
387 {
388 printf ("(sp+%d=%04x)", i, read_memory_short (rval + i));
389 }
390 }
391
392 }
393
394 void
395 register_convert_to_virtual (regnum, from, to)
396 unsigned char *from;
397 unsigned char *to;
398 {
399 to[0] = from[0];
400 to[1] = from[1];
401 to[2] = from[2];
402 to[3] = from[3];
403 }
404
405 void
406 register_convert_to_raw (regnum, to, from)
407 char *to;
408 char *from;
409 {
410 to[0] = from[0];
411 to[1] = from[1];
412 to[2] = from[2];
413 to[3] = from[3];
414 }
415
416 void
417 z8k_pop_frame ()
418 {
419 }
420
421 struct cmd_list_element *setmemorylist;
422
423 void
424 z8k_set_pointer_size (newsize)
425 int newsize;
426 {
427 static int oldsize = 0;
428
429 if (oldsize != newsize)
430 {
431 printf ("pointer size set to %d bits\n", newsize);
432 oldsize = newsize;
433 if (newsize == 32)
434 {
435 BIG = 1;
436 }
437 else
438 {
439 BIG = 0;
440 }
441 _initialize_gdbtypes ();
442 }
443 }
444
445 static void
446 segmented_command (args, from_tty)
447 char *args;
448 int from_tty;
449 {
450 z8k_set_pointer_size (32);
451 }
452
453 static void
454 unsegmented_command (args, from_tty)
455 char *args;
456 int from_tty;
457 {
458 z8k_set_pointer_size (16);
459
460 }
461
462 static void
463 set_memory (args, from_tty)
464 char *args;
465 int from_tty;
466 {
467 printf ("\"set memory\" must be followed by the name of a memory subcommand.\n");
468 help_list (setmemorylist, "set memory ", -1, stdout);
469 }
470
471 _initialize_z8ktdep ()
472 {
473 add_prefix_cmd ("memory", no_class, set_memory,
474 "set the memory model", &setmemorylist, "set memory ", 0,
475 &setlist);
476 add_cmd ("segmented", class_support, segmented_command,
477 "Set segmented memory model.", &setmemorylist);
478 add_cmd ("unsegmented", class_support, unsegmented_command,
479 "Set unsegmented memory model.", &setmemorylist);
480
481 }