This commit was generated by cvs2svn to track changes on a CVS vendor
[platform/upstream/binutils.git] / gdb / z8k-tdep.c
1 /* Target-machine dependent code for Zilog Z8000, for GDB.
2    Copyright (C) 1992, 1993, 1994 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., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 /*
22    Contributed by Steve Chamberlain
23    sac@cygnus.com
24  */
25
26 #include "defs.h"
27 #include "frame.h"
28 #include "obstack.h"
29 #include "symtab.h"
30 #include "gdbcmd.h"
31 #include "gdbtypes.h"
32 #include "dis-asm.h"
33 #include "gdbcore.h"
34
35 #include "value.h" /* For read_register() */
36
37
38 static int read_memory_pointer (CORE_ADDR x);
39
40 /* Return the saved PC from this frame.
41
42    If the frame has a memory copy of SRP_REGNUM, use that.  If not,
43    just use the register SRP_REGNUM itself.  */
44
45 CORE_ADDR
46 z8k_frame_saved_pc (frame)
47      struct frame_info *frame;
48 {
49   return read_memory_pointer (frame->frame + (BIG ? 4 : 2));
50 }
51
52 #define IS_PUSHL(x) (BIG ? ((x & 0xfff0) == 0x91e0):((x & 0xfff0) == 0x91F0))
53 #define IS_PUSHW(x) (BIG ? ((x & 0xfff0) == 0x93e0):((x & 0xfff0)==0x93f0))
54 #define IS_MOVE_FP(x) (BIG ? x == 0xa1ea : x == 0xa1fa)
55 #define IS_MOV_SP_FP(x) (BIG ? x == 0x94ea : x == 0x0d76)
56 #define IS_SUB2_SP(x) (x==0x1b87)
57 #define IS_MOVK_R5(x) (x==0x7905)
58 #define IS_SUB_SP(x) ((x & 0xffff) == 0x020f)
59 #define IS_PUSH_FP(x) (BIG ? (x == 0x93ea) : (x == 0x93fa))
60
61 /* work out how much local space is on the stack and
62    return the pc pointing to the first push */
63
64 static CORE_ADDR
65 skip_adjust (pc, size)
66      CORE_ADDR pc;
67      int *size;
68 {
69   *size = 0;
70
71   if (IS_PUSH_FP (read_memory_short (pc))
72       && IS_MOV_SP_FP (read_memory_short (pc + 2)))
73     {
74       /* This is a function with an explict frame pointer */
75       pc += 4;
76       *size += 2;               /* remember the frame pointer */
77     }
78
79   /* remember any stack adjustment */
80   if (IS_SUB_SP (read_memory_short (pc)))
81     {
82       *size += read_memory_short (pc + 2);
83       pc += 4;
84     }
85   return pc;
86 }
87
88 static CORE_ADDR examine_frame PARAMS ((CORE_ADDR, CORE_ADDR * regs, CORE_ADDR));
89 static CORE_ADDR
90 examine_frame (pc, regs, sp)
91      CORE_ADDR pc;
92      CORE_ADDR *regs;
93      CORE_ADDR sp;
94 {
95   int w = read_memory_short (pc);
96   int offset = 0;
97   int regno;
98
99   for (regno = 0; regno < NUM_REGS; regno++)
100     regs[regno] = 0;
101
102   while (IS_PUSHW (w) || IS_PUSHL (w))
103     {
104       /* work out which register is being pushed to where */
105       if (IS_PUSHL (w))
106         {
107           regs[w & 0xf] = offset;
108           regs[(w & 0xf) + 1] = offset + 2;
109           offset += 4;
110         }
111       else
112         {
113           regs[w & 0xf] = offset;
114           offset += 2;
115         }
116       pc += 2;
117       w = read_memory_short (pc);
118     }
119
120   if (IS_MOVE_FP (w))
121     {
122       /* We know the fp */
123
124     }
125   else if (IS_SUB_SP (w))
126     {
127       /* Subtracting a value from the sp, so were in a function
128          which needs stack space for locals, but has no fp.  We fake up
129          the values as if we had an fp */
130       regs[FP_REGNUM] = sp;
131     }
132   else
133     {
134       /* This one didn't have an fp, we'll fake it up */
135       regs[SP_REGNUM] = sp;
136     }
137   /* stack pointer contains address of next frame */
138   /*  regs[fp_regnum()] = fp; */
139   regs[SP_REGNUM] = sp;
140   return pc;
141 }
142
143 CORE_ADDR
144 z8k_skip_prologue (start_pc)
145      CORE_ADDR start_pc;
146 {
147   CORE_ADDR dummy[NUM_REGS];
148
149   return examine_frame (start_pc, dummy, 0);
150 }
151
152 CORE_ADDR
153 z8k_addr_bits_remove (addr)
154      CORE_ADDR addr;
155 {
156   return (addr & PTR_MASK);
157 }
158
159 static int
160 read_memory_pointer (CORE_ADDR x)
161 {
162   return read_memory_integer (ADDR_BITS_REMOVE (x), BIG ? 4 : 2);
163 }
164
165 CORE_ADDR
166 z8k_frame_chain (thisframe)
167      struct frame_info *thisframe;
168 {
169   if (thisframe->prev == 0)
170     {
171       /* This is the top of the stack, let's get the sp for real */
172     }
173   if (!inside_entry_file (thisframe->pc))
174     {
175       return read_memory_pointer (thisframe->frame);
176     }
177   return 0;
178 }
179
180 void
181 init_frame_pc ()
182 {
183   abort ();
184 }
185
186 /* Put here the code to store, into a struct frame_saved_regs,
187    the addresses of the saved registers of frame described by FRAME_INFO.
188    This includes special registers such as pc and fp saved in special
189    ways in the stack frame.  sp is even more special:
190    the address we return for it IS the sp for the next frame.  */
191
192 void
193 z8k_frame_init_saved_regs (frame_info)
194      struct frame_info *frame_info;
195 {
196   CORE_ADDR pc;
197   int w;
198
199   frame_saved_regs_zalloc (frame_info);
200   pc = get_pc_function_start (frame_info->pc);
201
202   /* wander down the instruction stream */
203   examine_frame (pc, frame_info->saved_regs, frame_info->frame);
204
205 }
206
207 void
208 z8k_push_dummy_frame ()
209 {
210   abort ();
211 }
212
213 int
214 gdb_print_insn_z8k (memaddr, info)
215      bfd_vma memaddr;
216      disassemble_info *info;
217 {
218   if (BIG)
219     return print_insn_z8001 (memaddr, info);
220   else
221     return print_insn_z8002 (memaddr, info);
222 }
223
224 /* Fetch the instruction at ADDR, returning 0 if ADDR is beyond LIM or
225    is not the address of a valid instruction, the address of the next
226    instruction beyond ADDR otherwise.  *PWORD1 receives the first word
227    of the instruction. */
228
229 CORE_ADDR
230 NEXT_PROLOGUE_INSN (addr, lim, pword1)
231      CORE_ADDR addr;
232      CORE_ADDR lim;
233      short *pword1;
234 {
235   char buf[2];
236   if (addr < lim + 8)
237     {
238       read_memory (addr, buf, 2);
239       *pword1 = extract_signed_integer (buf, 2);
240
241       return addr + 2;
242     }
243   return 0;
244 }
245
246 #if 0
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;
265
266   memset (fsrp, 0, sizeof *fsrp);
267
268   pc = skip_adjust (get_pc_function_start (fip->pc), &locals);
269
270   {
271     adr = FRAME_FP (fip) - 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
294   fsrp->regs[PC_REGNUM] = fip->frame + 4;
295   fsrp->regs[FP_REGNUM] = fip->frame;
296
297 }
298 #endif
299
300 int
301 z8k_saved_pc_after_call (struct frame_info *frame)
302 {
303   return ADDR_BITS_REMOVE
304     (read_memory_integer (read_register (SP_REGNUM), PTR_SIZE));
305 }
306
307
308 void
309 extract_return_value (type, regbuf, valbuf)
310      struct type *type;
311      char *regbuf;
312      char *valbuf;
313 {
314   int b;
315   int len = TYPE_LENGTH (type);
316
317   for (b = 0; b < len; b += 2)
318     {
319       int todo = len - b;
320
321       if (todo > 2)
322         todo = 2;
323       memcpy (valbuf + b, regbuf + b, todo);
324     }
325 }
326
327 void
328 write_return_value (type, valbuf)
329      struct type *type;
330      char *valbuf;
331 {
332   int reg;
333   int len;
334
335   for (len = 0; len < TYPE_LENGTH (type); len += 2)
336     write_register_bytes (REGISTER_BYTE (len / 2 + 2), valbuf + len, 2);
337 }
338
339 void
340 store_struct_return (addr, sp)
341      CORE_ADDR addr;
342      CORE_ADDR sp;
343 {
344   write_register (2, addr);
345 }
346
347
348 void
349 z8k_print_register_hook (regno)
350      int regno;
351 {
352   if ((regno & 1) == 0 && regno < 16)
353     {
354       unsigned short l[2];
355
356       read_relative_register_raw_bytes (regno, (char *) (l + 0));
357       read_relative_register_raw_bytes (regno + 1, (char *) (l + 1));
358       printf_unfiltered ("\t");
359       printf_unfiltered ("%04x%04x", l[0], l[1]);
360     }
361
362   if ((regno & 3) == 0 && regno < 16)
363     {
364       unsigned short l[4];
365
366       read_relative_register_raw_bytes (regno, (char *) (l + 0));
367       read_relative_register_raw_bytes (regno + 1, (char *) (l + 1));
368       read_relative_register_raw_bytes (regno + 2, (char *) (l + 2));
369       read_relative_register_raw_bytes (regno + 3, (char *) (l + 3));
370
371       printf_unfiltered ("\t");
372       printf_unfiltered ("%04x%04x%04x%04x", l[0], l[1], l[2], l[3]);
373     }
374   if (regno == 15)
375     {
376       unsigned short rval;
377       int i;
378
379       read_relative_register_raw_bytes (regno, (char *) (&rval));
380
381       printf_unfiltered ("\n");
382       for (i = 0; i < 10; i += 2)
383         {
384           printf_unfiltered ("(sp+%d=%04x)", i,
385                              (unsigned int)read_memory_short (rval + i));
386         }
387     }
388
389 }
390
391 void
392 z8k_pop_frame ()
393 {
394 }
395
396 struct cmd_list_element *setmemorylist;
397
398 void
399 z8k_set_pointer_size (newsize)
400      int newsize;
401 {
402   static int oldsize = 0;
403
404   if (oldsize != newsize)
405     {
406       printf_unfiltered ("pointer size set to %d bits\n", newsize);
407       oldsize = newsize;
408       if (newsize == 32)
409         {
410           BIG = 1;
411         }
412       else
413         {
414           BIG = 0;
415         }
416       /* FIXME: This code should be using the GDBARCH framework to
417          handle changed type sizes.  If this problem is ever fixed
418          (the direct reference to _initialize_gdbtypes() below
419          eliminated) then Makefile.in should be updated so that
420          z8k-tdep.c is again compiled with -Werror. */
421       _initialize_gdbtypes ();
422     }
423 }
424
425 static void
426 segmented_command (args, from_tty)
427      char *args;
428      int from_tty;
429 {
430   z8k_set_pointer_size (32);
431 }
432
433 static void
434 unsegmented_command (args, from_tty)
435      char *args;
436      int from_tty;
437 {
438   z8k_set_pointer_size (16);
439 }
440
441 static void
442 set_memory (args, from_tty)
443      char *args;
444      int from_tty;
445 {
446   printf_unfiltered ("\"set memory\" must be followed by the name of a memory subcommand.\n");
447   help_list (setmemorylist, "set memory ", -1, gdb_stdout);
448 }
449
450 void
451 _initialize_z8ktdep ()
452 {
453   tm_print_insn = gdb_print_insn_z8k;
454
455   add_prefix_cmd ("memory", no_class, set_memory,
456                   "set the memory model", &setmemorylist, "set memory ", 0,
457                   &setlist);
458   add_cmd ("segmented", class_support, segmented_command,
459            "Set segmented memory model.", &setmemorylist);
460   add_cmd ("unsegmented", class_support, unsegmented_command,
461            "Set unsegmented memory model.", &setmemorylist);
462
463 }