* ns32k-tdep.c (ns32k_breakpoint_from_pc, ns32k_frame_chain,
[external/binutils.git] / gdb / ns32k-tdep.c
1 /* Print NS 32000 instructions for GDB, the GNU debugger.
2    Copyright 1986, 1988, 1991, 1992, 1994, 1995, 1998, 1999, 2000, 2001,
3    2002 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include "defs.h"
23 #include "frame.h"
24 #include "gdbcore.h"
25
26 static int sign_extend (int value, int bits);
27 static CORE_ADDR ns32k_get_enter_addr (CORE_ADDR);
28 static int ns32k_localcount (CORE_ADDR enter_pc);
29
30 char *
31 ns32k_register_name_32082 (int regno)
32 {
33   static char *register_names[] =
34   {
35     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
36     "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
37     "sp", "fp", "pc", "ps",
38     "l0", "l1", "l2", "l3", "xx",
39   };
40
41   if (regno < 0)
42     return NULL;
43   if (regno >= sizeof (register_names) / sizeof (*register_names))
44     return NULL;
45
46   return (register_names[regno]);
47 }
48
49 char *
50 ns32k_register_name_32382 (int regno)
51 {
52   static char *register_names[] =
53   {
54     "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
55     "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
56     "sp", "fp", "pc", "ps",
57     "fsr",
58     "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7", "xx",
59   };
60
61   if (regno < 0)
62     return NULL;
63   if (regno >= sizeof (register_names) / sizeof (*register_names))
64     return NULL;
65
66   return (register_names[regno]);
67 }
68
69 int
70 ns32k_register_byte_32082 (int regno)
71 {
72   if (regno >= LP0_REGNUM)
73     return (LP0_REGNUM * 4) + ((regno - LP0_REGNUM) * 8);
74
75   return (regno * 4);
76 }
77
78 int
79 ns32k_register_byte_32382 (int regno)
80 {
81   /* This is a bit yuk.  The even numbered double precision floating
82      point long registers occupy the same space as the even:odd numbered
83      single precision floating point registers, but the extra 32381 FPU
84      registers are at the end.  Doing it this way is compatible for both
85      32081 and 32381 equipped machines.  */
86
87   return ((regno < LP0_REGNUM ? regno
88            : (regno - LP0_REGNUM) & 1 ? regno - 1
89            : (regno - LP0_REGNUM + FP0_REGNUM)) * 4);
90 }
91
92 int
93 ns32k_register_raw_size (int regno)
94 {
95   /* All registers are 4 bytes, except for the doubled floating
96      registers.  */
97
98   return ((regno >= LP0_REGNUM) ? 8 : 4);
99 }
100
101 int
102 ns32k_register_virtual_size (int regno)
103 {
104   return ((regno >= LP0_REGNUM) ? 8 : 4);
105 }
106
107 struct type *
108 ns32k_register_virtual_type (int regno)
109 {
110   if (regno < FP0_REGNUM)
111     return (builtin_type_int);
112
113   if (regno < FP0_REGNUM + 8)
114     return (builtin_type_float);
115
116   if (regno < LP0_REGNUM)
117     return (builtin_type_int); 
118
119   return (builtin_type_double);
120 }
121
122 /* Advance PC across any function entry prologue instructions
123    to reach some "real" code.  */
124
125 CORE_ADDR
126 umax_skip_prologue (CORE_ADDR pc)
127 {
128   register unsigned char op = read_memory_integer (pc, 1);
129   if (op == 0x82)
130     {
131       op = read_memory_integer (pc + 2, 1);
132       if ((op & 0x80) == 0)
133         pc += 3;
134       else if ((op & 0xc0) == 0x80)
135         pc += 4;
136       else
137         pc += 6;
138     }
139   return pc;
140 }
141 \f
142 const unsigned char *
143 ns32k_breakpoint_from_pc (CORE_ADDR *pcp, int *lenp)
144 {
145   static const unsigned char breakpoint_insn[] = { 0xf2 };
146
147   *lenp = sizeof (breakpoint_insn);
148   return breakpoint_insn;
149 }
150
151 /* Return number of args passed to a frame.
152    Can return -1, meaning no way to tell.
153    Encore's C compiler often reuses same area on stack for args,
154    so this will often not work properly.  If the arg names
155    are known, it's likely most of them will be printed. */
156
157 int
158 umax_frame_num_args (struct frame_info *fi)
159 {
160   int numargs;
161   CORE_ADDR pc;
162   CORE_ADDR enter_addr;
163   unsigned int insn;
164   unsigned int addr_mode;
165   int width;
166
167   numargs = -1;
168   enter_addr = ns32k_get_enter_addr ((fi)->pc);
169   if (enter_addr > 0)
170     {
171       pc = ((enter_addr == 1)
172             ? SAVED_PC_AFTER_CALL (fi)
173             : FRAME_SAVED_PC (fi));
174       insn = read_memory_integer (pc, 2);
175       addr_mode = (insn >> 11) & 0x1f;
176       insn = insn & 0x7ff;
177       if ((insn & 0x7fc) == 0x57c
178           && addr_mode == 0x14) /* immediate */
179         {
180           if (insn == 0x57c)    /* adjspb */
181             width = 1;
182           else if (insn == 0x57d)       /* adjspw */
183             width = 2;
184           else if (insn == 0x57f)       /* adjspd */
185             width = 4;
186           else
187             internal_error (__FILE__, __LINE__, "bad else");
188           numargs = read_memory_integer (pc + 2, width);
189           if (width > 1)
190             flip_bytes (&numargs, width);
191           numargs = -sign_extend (numargs, width * 8) / 4;
192         }
193     }
194   return numargs;
195 }
196
197 static int
198 sign_extend (int value, int bits)
199 {
200   value = value & ((1 << bits) - 1);
201   return (value & (1 << (bits - 1))
202           ? value | (~((1 << bits) - 1))
203           : value);
204 }
205
206 void
207 flip_bytes (void *p, int count)
208 {
209   char tmp;
210   char *ptr = 0;
211
212   while (count > 0)
213     {
214       tmp = *ptr;
215       ptr[0] = ptr[count - 1];
216       ptr[count - 1] = tmp;
217       ptr++;
218       count -= 2;
219     }
220 }
221
222 /* Return the number of locals in the current frame given a
223    pc pointing to the enter instruction.  This is used by
224    ns32k_frame_init_saved_regs.  */
225
226 static int
227 ns32k_localcount (CORE_ADDR enter_pc)
228 {
229   unsigned char localtype;
230   int localcount;
231
232   localtype = read_memory_integer (enter_pc + 2, 1);
233   if ((localtype & 0x80) == 0)
234     localcount = localtype;
235   else if ((localtype & 0xc0) == 0x80)
236     localcount = (((localtype & 0x3f) << 8)
237                   | (read_memory_integer (enter_pc + 3, 1) & 0xff));
238   else
239     localcount = (((localtype & 0x3f) << 24)
240                   | ((read_memory_integer (enter_pc + 3, 1) & 0xff) << 16)
241                   | ((read_memory_integer (enter_pc + 4, 1) & 0xff) << 8)
242                   | (read_memory_integer (enter_pc + 5, 1) & 0xff));
243   return localcount;
244 }
245
246
247 /* Nonzero if instruction at PC is a return instruction.  */
248
249 static int
250 ns32k_about_to_return (CORE_ADDR pc)
251 {
252   return (read_memory_integer (pc, 1) == 0x12);
253 }
254
255 /* Get the address of the enter opcode for this function, if it is active.
256    Returns positive address > 1 if pc is between enter/exit, 
257    1 if pc before enter or after exit, 0 otherwise. */
258 static CORE_ADDR
259 ns32k_get_enter_addr (CORE_ADDR pc)
260 {
261   CORE_ADDR enter_addr;
262   unsigned char op;
263
264   if (pc == 0)
265     return 0;
266
267   if (ns32k_about_to_return (pc))
268     return 1;                   /* after exit */
269
270   enter_addr = get_pc_function_start (pc);
271
272   if (pc == enter_addr)
273     return 1;                   /* before enter */
274
275   op = read_memory_integer (enter_addr, 1);
276
277   if (op != 0x82)
278     return 0;                   /* function has no enter/exit */
279
280   return enter_addr;            /* pc is between enter and exit */
281 }
282
283 CORE_ADDR
284 ns32k_frame_chain (struct frame_info *frame)
285 {
286   /* In the case of the NS32000 series, the frame's nominal address is the
287      FP value, and that address is saved at the previous FP value as a
288      4-byte word.  */
289
290   if (inside_entry_file (frame->pc))
291     return 0;
292
293   return (read_memory_integer (frame->frame, 4));
294 }
295
296 CORE_ADDR
297 ns32k_frame_saved_pc (struct frame_info *frame)
298 {
299   if (frame->signal_handler_caller)
300     return (sigtramp_saved_pc (frame)); /* XXXJRT */
301
302   return (read_memory_integer (frame->frame + 4, 4));
303 }
304
305 CORE_ADDR
306 ns32k_frame_args_address (struct frame_info *frame)
307 {
308   if (ns32k_get_enter_addr (frame->pc) > 1)
309     return (frame->frame);
310
311   return (read_register (SP_REGNUM) - 4);
312 }
313
314 CORE_ADDR
315 ns32k_frame_locals_address (struct frame_info *frame)
316 {
317   return (frame->frame);
318 }
319
320 /* Code to initialize the addresses of the saved registers of frame described
321    by FRAME_INFO.  This includes special registers such as pc and fp saved in
322    special ways in the stack frame.  sp is even more special: the address we
323    return for it IS the sp for the next frame.  */
324
325 void
326 ns32k_frame_init_saved_regs (struct frame_info *frame)
327 {
328   int regmask, regnum;
329   int localcount;
330   CORE_ADDR enter_addr, next_addr;
331
332   if (frame->saved_regs)
333     return;
334
335   frame_saved_regs_zalloc (frame);
336
337   enter_addr = ns32k_get_enter_addr (frame->pc);
338   if (enter_addr > 1)
339     {
340       regmask = read_memory_integer (enter_addr + 1, 1) & 0xff;
341       localcount = ns32k_localcount (enter_addr);
342       next_addr = frame->frame + localcount;
343
344       for (regnum = 0; regnum < 8; regnum++)
345         {
346           if (regmask & (1 << regnum))
347             frame->saved_regs[regnum] = next_addr -= 4;
348         }
349
350       frame->saved_regs[SP_REGNUM] = frame->frame + 4;
351       frame->saved_regs[PC_REGNUM] = frame->frame + 4;
352       frame->saved_regs[FP_REGNUM] = read_memory_integer (frame->frame, 4);
353     }
354   else if (enter_addr == 1)
355     {
356       CORE_ADDR sp = read_register (SP_REGNUM);
357       frame->saved_regs[PC_REGNUM] = sp;
358       frame->saved_regs[SP_REGNUM] = sp + 4;
359     }
360 }
361
362 void
363 ns32k_push_dummy_frame (void)
364 {
365   CORE_ADDR sp = read_register (SP_REGNUM);
366   int regnum;
367
368   sp = push_word (sp, read_register (PC_REGNUM));
369   sp = push_word (sp, read_register (FP_REGNUM));
370   write_register (FP_REGNUM, sp);
371
372   for (regnum = 0; regnum < 8; regnum++)
373     sp = push_word (sp, read_register (regnum));
374
375   write_register (SP_REGNUM, sp);
376 }
377
378 void
379 ns32k_pop_frame (void)
380 {
381   struct frame_info *frame = get_current_frame ();
382   CORE_ADDR fp;
383   int regnum;
384
385   fp = frame->frame;
386   FRAME_INIT_SAVED_REGS (frame);
387
388   for (regnum = 0; regnum < 8; regnum++)
389     if (frame->saved_regs[regnum])
390       write_register (regnum,
391                       read_memory_integer (frame->saved_regs[regnum], 4));
392
393   write_register (FP_REGNUM, read_memory_integer (fp, 4));
394   write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
395   write_register (SP_REGNUM, fp + 8);
396   flush_cached_frames ();
397 }
398
399 void
400 _initialize_ns32k_tdep (void)
401 {
402   tm_print_insn = print_insn_ns32k;
403 }