* ns32k-tdep.c (ns32k_saved_pc_after_call,
[deliverable/binutils-gdb.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 static void flip_bytes (void *, int);
30
31 char *
32 ns32k_register_name_32082 (int regno)
33 {
34 static char *register_names[] =
35 {
36 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
37 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
38 "sp", "fp", "pc", "ps",
39 "l0", "l1", "l2", "l3", "xx",
40 };
41
42 if (regno < 0)
43 return NULL;
44 if (regno >= sizeof (register_names) / sizeof (*register_names))
45 return NULL;
46
47 return (register_names[regno]);
48 }
49
50 char *
51 ns32k_register_name_32382 (int regno)
52 {
53 static char *register_names[] =
54 {
55 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
56 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
57 "sp", "fp", "pc", "ps",
58 "fsr",
59 "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7", "xx",
60 };
61
62 if (regno < 0)
63 return NULL;
64 if (regno >= sizeof (register_names) / sizeof (*register_names))
65 return NULL;
66
67 return (register_names[regno]);
68 }
69
70 int
71 ns32k_register_byte_32082 (int regno)
72 {
73 if (regno >= LP0_REGNUM)
74 return (LP0_REGNUM * 4) + ((regno - LP0_REGNUM) * 8);
75
76 return (regno * 4);
77 }
78
79 int
80 ns32k_register_byte_32382 (int regno)
81 {
82 /* This is a bit yuk. The even numbered double precision floating
83 point long registers occupy the same space as the even:odd numbered
84 single precision floating point registers, but the extra 32381 FPU
85 registers are at the end. Doing it this way is compatible for both
86 32081 and 32381 equipped machines. */
87
88 return ((regno < LP0_REGNUM ? regno
89 : (regno - LP0_REGNUM) & 1 ? regno - 1
90 : (regno - LP0_REGNUM + FP0_REGNUM)) * 4);
91 }
92
93 int
94 ns32k_register_raw_size (int regno)
95 {
96 /* All registers are 4 bytes, except for the doubled floating
97 registers. */
98
99 return ((regno >= LP0_REGNUM) ? 8 : 4);
100 }
101
102 int
103 ns32k_register_virtual_size (int regno)
104 {
105 return ((regno >= LP0_REGNUM) ? 8 : 4);
106 }
107
108 struct type *
109 ns32k_register_virtual_type (int regno)
110 {
111 if (regno < FP0_REGNUM)
112 return (builtin_type_int);
113
114 if (regno < FP0_REGNUM + 8)
115 return (builtin_type_float);
116
117 if (regno < LP0_REGNUM)
118 return (builtin_type_int);
119
120 return (builtin_type_double);
121 }
122
123 /* Immediately after a function call, return the saved PC. Can't
124 always go through the frames for this because on some systems,
125 the new frame is not set up until the new function executes some
126 instructions. */
127
128 CORE_ADDR
129 ns32k_saved_pc_after_call (struct frame_info *frame)
130 {
131 return (read_memory_integer (read_register (SP_REGNUM), 4));
132 }
133
134 /* Advance PC across any function entry prologue instructions
135 to reach some "real" code. */
136
137 CORE_ADDR
138 umax_skip_prologue (CORE_ADDR pc)
139 {
140 register unsigned char op = read_memory_integer (pc, 1);
141 if (op == 0x82)
142 {
143 op = read_memory_integer (pc + 2, 1);
144 if ((op & 0x80) == 0)
145 pc += 3;
146 else if ((op & 0xc0) == 0x80)
147 pc += 4;
148 else
149 pc += 6;
150 }
151 return pc;
152 }
153 \f
154 const unsigned char *
155 ns32k_breakpoint_from_pc (CORE_ADDR *pcp, int *lenp)
156 {
157 static const unsigned char breakpoint_insn[] = { 0xf2 };
158
159 *lenp = sizeof (breakpoint_insn);
160 return breakpoint_insn;
161 }
162
163 /* Return number of args passed to a frame.
164 Can return -1, meaning no way to tell.
165 Encore's C compiler often reuses same area on stack for args,
166 so this will often not work properly. If the arg names
167 are known, it's likely most of them will be printed. */
168
169 int
170 umax_frame_num_args (struct frame_info *fi)
171 {
172 int numargs;
173 CORE_ADDR pc;
174 CORE_ADDR enter_addr;
175 unsigned int insn;
176 unsigned int addr_mode;
177 int width;
178
179 numargs = -1;
180 enter_addr = ns32k_get_enter_addr ((fi)->pc);
181 if (enter_addr > 0)
182 {
183 pc = ((enter_addr == 1)
184 ? SAVED_PC_AFTER_CALL (fi)
185 : FRAME_SAVED_PC (fi));
186 insn = read_memory_integer (pc, 2);
187 addr_mode = (insn >> 11) & 0x1f;
188 insn = insn & 0x7ff;
189 if ((insn & 0x7fc) == 0x57c
190 && addr_mode == 0x14) /* immediate */
191 {
192 if (insn == 0x57c) /* adjspb */
193 width = 1;
194 else if (insn == 0x57d) /* adjspw */
195 width = 2;
196 else if (insn == 0x57f) /* adjspd */
197 width = 4;
198 else
199 internal_error (__FILE__, __LINE__, "bad else");
200 numargs = read_memory_integer (pc + 2, width);
201 if (width > 1)
202 flip_bytes (&numargs, width);
203 numargs = -sign_extend (numargs, width * 8) / 4;
204 }
205 }
206 return numargs;
207 }
208
209 static int
210 sign_extend (int value, int bits)
211 {
212 value = value & ((1 << bits) - 1);
213 return (value & (1 << (bits - 1))
214 ? value | (~((1 << bits) - 1))
215 : value);
216 }
217
218 static void
219 flip_bytes (void *p, int count)
220 {
221 char tmp;
222 char *ptr = 0;
223
224 while (count > 0)
225 {
226 tmp = *ptr;
227 ptr[0] = ptr[count - 1];
228 ptr[count - 1] = tmp;
229 ptr++;
230 count -= 2;
231 }
232 }
233
234 /* Return the number of locals in the current frame given a
235 pc pointing to the enter instruction. This is used by
236 ns32k_frame_init_saved_regs. */
237
238 static int
239 ns32k_localcount (CORE_ADDR enter_pc)
240 {
241 unsigned char localtype;
242 int localcount;
243
244 localtype = read_memory_integer (enter_pc + 2, 1);
245 if ((localtype & 0x80) == 0)
246 localcount = localtype;
247 else if ((localtype & 0xc0) == 0x80)
248 localcount = (((localtype & 0x3f) << 8)
249 | (read_memory_integer (enter_pc + 3, 1) & 0xff));
250 else
251 localcount = (((localtype & 0x3f) << 24)
252 | ((read_memory_integer (enter_pc + 3, 1) & 0xff) << 16)
253 | ((read_memory_integer (enter_pc + 4, 1) & 0xff) << 8)
254 | (read_memory_integer (enter_pc + 5, 1) & 0xff));
255 return localcount;
256 }
257
258
259 /* Nonzero if instruction at PC is a return instruction. */
260
261 static int
262 ns32k_about_to_return (CORE_ADDR pc)
263 {
264 return (read_memory_integer (pc, 1) == 0x12);
265 }
266
267 /* Get the address of the enter opcode for this function, if it is active.
268 Returns positive address > 1 if pc is between enter/exit,
269 1 if pc before enter or after exit, 0 otherwise. */
270 static CORE_ADDR
271 ns32k_get_enter_addr (CORE_ADDR pc)
272 {
273 CORE_ADDR enter_addr;
274 unsigned char op;
275
276 if (pc == 0)
277 return 0;
278
279 if (ns32k_about_to_return (pc))
280 return 1; /* after exit */
281
282 enter_addr = get_pc_function_start (pc);
283
284 if (pc == enter_addr)
285 return 1; /* before enter */
286
287 op = read_memory_integer (enter_addr, 1);
288
289 if (op != 0x82)
290 return 0; /* function has no enter/exit */
291
292 return enter_addr; /* pc is between enter and exit */
293 }
294
295 CORE_ADDR
296 ns32k_frame_chain (struct frame_info *frame)
297 {
298 /* In the case of the NS32000 series, the frame's nominal address is the
299 FP value, and that address is saved at the previous FP value as a
300 4-byte word. */
301
302 if (inside_entry_file (frame->pc))
303 return 0;
304
305 return (read_memory_integer (frame->frame, 4));
306 }
307
308 CORE_ADDR
309 ns32k_frame_saved_pc (struct frame_info *frame)
310 {
311 if (frame->signal_handler_caller)
312 return (sigtramp_saved_pc (frame)); /* XXXJRT */
313
314 return (read_memory_integer (frame->frame + 4, 4));
315 }
316
317 CORE_ADDR
318 ns32k_frame_args_address (struct frame_info *frame)
319 {
320 if (ns32k_get_enter_addr (frame->pc) > 1)
321 return (frame->frame);
322
323 return (read_register (SP_REGNUM) - 4);
324 }
325
326 CORE_ADDR
327 ns32k_frame_locals_address (struct frame_info *frame)
328 {
329 return (frame->frame);
330 }
331
332 /* Code to initialize the addresses of the saved registers of frame described
333 by FRAME_INFO. This includes special registers such as pc and fp saved in
334 special ways in the stack frame. sp is even more special: the address we
335 return for it IS the sp for the next frame. */
336
337 void
338 ns32k_frame_init_saved_regs (struct frame_info *frame)
339 {
340 int regmask, regnum;
341 int localcount;
342 CORE_ADDR enter_addr, next_addr;
343
344 if (frame->saved_regs)
345 return;
346
347 frame_saved_regs_zalloc (frame);
348
349 enter_addr = ns32k_get_enter_addr (frame->pc);
350 if (enter_addr > 1)
351 {
352 regmask = read_memory_integer (enter_addr + 1, 1) & 0xff;
353 localcount = ns32k_localcount (enter_addr);
354 next_addr = frame->frame + localcount;
355
356 for (regnum = 0; regnum < 8; regnum++)
357 {
358 if (regmask & (1 << regnum))
359 frame->saved_regs[regnum] = next_addr -= 4;
360 }
361
362 frame->saved_regs[SP_REGNUM] = frame->frame + 4;
363 frame->saved_regs[PC_REGNUM] = frame->frame + 4;
364 frame->saved_regs[FP_REGNUM] = read_memory_integer (frame->frame, 4);
365 }
366 else if (enter_addr == 1)
367 {
368 CORE_ADDR sp = read_register (SP_REGNUM);
369 frame->saved_regs[PC_REGNUM] = sp;
370 frame->saved_regs[SP_REGNUM] = sp + 4;
371 }
372 }
373
374 void
375 ns32k_push_dummy_frame (void)
376 {
377 CORE_ADDR sp = read_register (SP_REGNUM);
378 int regnum;
379
380 sp = push_word (sp, read_register (PC_REGNUM));
381 sp = push_word (sp, read_register (FP_REGNUM));
382 write_register (FP_REGNUM, sp);
383
384 for (regnum = 0; regnum < 8; regnum++)
385 sp = push_word (sp, read_register (regnum));
386
387 write_register (SP_REGNUM, sp);
388 }
389
390 void
391 ns32k_pop_frame (void)
392 {
393 struct frame_info *frame = get_current_frame ();
394 CORE_ADDR fp;
395 int regnum;
396
397 fp = frame->frame;
398 FRAME_INIT_SAVED_REGS (frame);
399
400 for (regnum = 0; regnum < 8; regnum++)
401 if (frame->saved_regs[regnum])
402 write_register (regnum,
403 read_memory_integer (frame->saved_regs[regnum], 4));
404
405 write_register (FP_REGNUM, read_memory_integer (fp, 4));
406 write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
407 write_register (SP_REGNUM, fp + 8);
408 flush_cached_frames ();
409 }
410 \f
411 /* The NS32000 call dummy sequence:
412
413 enter 0xff,0 82 ff 00
414 jsr @0x00010203 7f ae c0 01 02 03
415 adjspd 0x69696969 7f a5 01 02 03 04
416 bpt f2
417
418 It is 16 bytes long. */
419
420 LONGEST ns32k_call_dummy_words[] =
421 {
422 0x7f00ff82,
423 0x0201c0ae,
424 0x01a57f03,
425 0xf2040302
426 };
427 int sizeof_ns32k_call_dummy_words = sizeof (ns32k_call_dummy_words);
428
429 #define NS32K_CALL_DUMMY_ADDR 5
430 #define NS32K_CALL_DUMMY_NARGS 11
431
432 void
433 ns32k_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs,
434 struct value **args, struct type *type, int gcc_p)
435 {
436 int flipped;
437
438 flipped = fun | 0xc0000000;
439 flip_bytes (&flipped, 4);
440 store_unsigned_integer (dummy + NS32K_CALL_DUMMY_ADDR, 4, flipped);
441
442 flipped = - nargs * 4;
443 flip_bytes (&flipped, 4);
444 store_unsigned_integer (dummy + NS32K_CALL_DUMMY_NARGS, 4, flipped);
445 }
446 \f
447 void
448 ns32k_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
449 {
450 /* On this machine, this is a no-op (Encore Umax didn't use GCC). */
451 }
452
453 void
454 ns32k_extract_return_value (struct type *valtype, char *regbuf, char *valbuf)
455 {
456 memcpy (valbuf,
457 regbuf + REGISTER_BYTE (TYPE_CODE (valtype) == TYPE_CODE_FLT ?
458 FP0_REGNUM : 0), TYPE_LENGTH (valtype));
459 }
460
461 void
462 ns32k_store_return_value (struct type *valtype, char *valbuf)
463 {
464 write_register_bytes (TYPE_CODE (valtype) == TYPE_CODE_FLT ?
465 FP0_REGNUM : 0, valbuf, TYPE_LENGTH (valtype));
466 }
467
468 CORE_ADDR
469 ns32k_extract_struct_value_address (char *regbuf)
470 {
471 return (extract_address (regbuf + REGISTER_BYTE (0), REGISTER_RAW_SIZE (0)));
472 }
473 \f
474 void
475 _initialize_ns32k_tdep (void)
476 {
477 tm_print_insn = print_insn_ns32k;
478 }
This page took 0.043577 seconds and 4 git commands to generate.