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