2002-11-13 Andrew Cagney <cagney@redhat.com>
[deliverable/binutils-gdb.git] / gdb / ns32k-tdep.c
1 /* Target dependent code for the NS32000, for GDB.
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 "gdbtypes.h"
25 #include "gdbcore.h"
26 #include "inferior.h"
27 #include "regcache.h"
28 #include "target.h"
29
30 #include "arch-utils.h"
31
32 #include "ns32k-tdep.h"
33 #include "gdb_string.h"
34
35 static int sign_extend (int value, int bits);
36 static CORE_ADDR ns32k_get_enter_addr (CORE_ADDR);
37 static int ns32k_localcount (CORE_ADDR enter_pc);
38 static void flip_bytes (void *, int);
39
40 static const char *
41 ns32k_register_name_32082 (int regno)
42 {
43 static char *register_names[] =
44 {
45 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
46 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
47 "sp", "fp", "pc", "ps",
48 "l0", "l1", "l2", "l3", "xx",
49 };
50
51 if (regno < 0)
52 return NULL;
53 if (regno >= sizeof (register_names) / sizeof (*register_names))
54 return NULL;
55
56 return (register_names[regno]);
57 }
58
59 static const char *
60 ns32k_register_name_32382 (int regno)
61 {
62 static char *register_names[] =
63 {
64 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
65 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
66 "sp", "fp", "pc", "ps",
67 "fsr",
68 "l0", "l1", "l2", "l3", "l4", "l5", "l6", "l7", "xx",
69 };
70
71 if (regno < 0)
72 return NULL;
73 if (regno >= sizeof (register_names) / sizeof (*register_names))
74 return NULL;
75
76 return (register_names[regno]);
77 }
78
79 static int
80 ns32k_register_byte_32082 (int regno)
81 {
82 if (regno >= NS32K_LP0_REGNUM)
83 return (NS32K_LP0_REGNUM * 4) + ((regno - NS32K_LP0_REGNUM) * 8);
84
85 return (regno * 4);
86 }
87
88 static int
89 ns32k_register_byte_32382 (int regno)
90 {
91 /* This is a bit yuk. The even numbered double precision floating
92 point long registers occupy the same space as the even:odd numbered
93 single precision floating point registers, but the extra 32381 FPU
94 registers are at the end. Doing it this way is compatible for both
95 32081 and 32381 equipped machines. */
96
97 return ((regno < NS32K_LP0_REGNUM ? regno
98 : (regno - NS32K_LP0_REGNUM) & 1 ? regno - 1
99 : (regno - NS32K_LP0_REGNUM + FP0_REGNUM)) * 4);
100 }
101
102 static int
103 ns32k_register_raw_size (int regno)
104 {
105 /* All registers are 4 bytes, except for the doubled floating
106 registers. */
107
108 return ((regno >= NS32K_LP0_REGNUM) ? 8 : 4);
109 }
110
111 static int
112 ns32k_register_virtual_size (int regno)
113 {
114 return ((regno >= NS32K_LP0_REGNUM) ? 8 : 4);
115 }
116
117 static struct type *
118 ns32k_register_virtual_type (int regno)
119 {
120 if (regno < FP0_REGNUM)
121 return (builtin_type_int);
122
123 if (regno < FP0_REGNUM + 8)
124 return (builtin_type_float);
125
126 if (regno < NS32K_LP0_REGNUM)
127 return (builtin_type_int);
128
129 return (builtin_type_double);
130 }
131
132 /* Immediately after a function call, return the saved PC. Can't
133 always go through the frames for this because on some systems,
134 the new frame is not set up until the new function executes some
135 instructions. */
136
137 static CORE_ADDR
138 ns32k_saved_pc_after_call (struct frame_info *frame)
139 {
140 return (read_memory_integer (read_register (SP_REGNUM), 4));
141 }
142
143 /* Advance PC across any function entry prologue instructions
144 to reach some "real" code. */
145
146 static CORE_ADDR
147 umax_skip_prologue (CORE_ADDR pc)
148 {
149 register unsigned char op = read_memory_integer (pc, 1);
150 if (op == 0x82)
151 {
152 op = read_memory_integer (pc + 2, 1);
153 if ((op & 0x80) == 0)
154 pc += 3;
155 else if ((op & 0xc0) == 0x80)
156 pc += 4;
157 else
158 pc += 6;
159 }
160 return pc;
161 }
162 \f
163 static const unsigned char *
164 ns32k_breakpoint_from_pc (CORE_ADDR *pcp, int *lenp)
165 {
166 static const unsigned char breakpoint_insn[] = { 0xf2 };
167
168 *lenp = sizeof (breakpoint_insn);
169 return breakpoint_insn;
170 }
171
172 /* Return number of args passed to a frame.
173 Can return -1, meaning no way to tell.
174 Encore's C compiler often reuses same area on stack for args,
175 so this will often not work properly. If the arg names
176 are known, it's likely most of them will be printed. */
177
178 static int
179 umax_frame_num_args (struct frame_info *fi)
180 {
181 int numargs;
182 CORE_ADDR pc;
183 CORE_ADDR enter_addr;
184 unsigned int insn;
185 unsigned int addr_mode;
186 int width;
187
188 numargs = -1;
189 enter_addr = ns32k_get_enter_addr ((fi)->pc);
190 if (enter_addr > 0)
191 {
192 pc = ((enter_addr == 1)
193 ? SAVED_PC_AFTER_CALL (fi)
194 : FRAME_SAVED_PC (fi));
195 insn = read_memory_integer (pc, 2);
196 addr_mode = (insn >> 11) & 0x1f;
197 insn = insn & 0x7ff;
198 if ((insn & 0x7fc) == 0x57c
199 && addr_mode == 0x14) /* immediate */
200 {
201 if (insn == 0x57c) /* adjspb */
202 width = 1;
203 else if (insn == 0x57d) /* adjspw */
204 width = 2;
205 else if (insn == 0x57f) /* adjspd */
206 width = 4;
207 else
208 internal_error (__FILE__, __LINE__, "bad else");
209 numargs = read_memory_integer (pc + 2, width);
210 if (width > 1)
211 flip_bytes (&numargs, width);
212 numargs = -sign_extend (numargs, width * 8) / 4;
213 }
214 }
215 return numargs;
216 }
217
218 static int
219 sign_extend (int value, int bits)
220 {
221 value = value & ((1 << bits) - 1);
222 return (value & (1 << (bits - 1))
223 ? value | (~((1 << bits) - 1))
224 : value);
225 }
226
227 static void
228 flip_bytes (void *p, int count)
229 {
230 char tmp;
231 char *ptr = 0;
232
233 while (count > 0)
234 {
235 tmp = *ptr;
236 ptr[0] = ptr[count - 1];
237 ptr[count - 1] = tmp;
238 ptr++;
239 count -= 2;
240 }
241 }
242
243 /* Return the number of locals in the current frame given a
244 pc pointing to the enter instruction. This is used by
245 ns32k_frame_init_saved_regs. */
246
247 static int
248 ns32k_localcount (CORE_ADDR enter_pc)
249 {
250 unsigned char localtype;
251 int localcount;
252
253 localtype = read_memory_integer (enter_pc + 2, 1);
254 if ((localtype & 0x80) == 0)
255 localcount = localtype;
256 else if ((localtype & 0xc0) == 0x80)
257 localcount = (((localtype & 0x3f) << 8)
258 | (read_memory_integer (enter_pc + 3, 1) & 0xff));
259 else
260 localcount = (((localtype & 0x3f) << 24)
261 | ((read_memory_integer (enter_pc + 3, 1) & 0xff) << 16)
262 | ((read_memory_integer (enter_pc + 4, 1) & 0xff) << 8)
263 | (read_memory_integer (enter_pc + 5, 1) & 0xff));
264 return localcount;
265 }
266
267
268 /* Nonzero if instruction at PC is a return instruction. */
269
270 static int
271 ns32k_about_to_return (CORE_ADDR pc)
272 {
273 return (read_memory_integer (pc, 1) == 0x12);
274 }
275
276 /* Get the address of the enter opcode for this function, if it is active.
277 Returns positive address > 1 if pc is between enter/exit,
278 1 if pc before enter or after exit, 0 otherwise. */
279 static CORE_ADDR
280 ns32k_get_enter_addr (CORE_ADDR pc)
281 {
282 CORE_ADDR enter_addr;
283 unsigned char op;
284
285 if (pc == 0)
286 return 0;
287
288 if (ns32k_about_to_return (pc))
289 return 1; /* after exit */
290
291 enter_addr = get_pc_function_start (pc);
292
293 if (pc == enter_addr)
294 return 1; /* before enter */
295
296 op = read_memory_integer (enter_addr, 1);
297
298 if (op != 0x82)
299 return 0; /* function has no enter/exit */
300
301 return enter_addr; /* pc is between enter and exit */
302 }
303
304 static CORE_ADDR
305 ns32k_frame_chain (struct frame_info *frame)
306 {
307 /* In the case of the NS32000 series, the frame's nominal address is the
308 FP value, and that address is saved at the previous FP value as a
309 4-byte word. */
310
311 if (inside_entry_file (frame->pc))
312 return 0;
313
314 return (read_memory_integer (frame->frame, 4));
315 }
316
317 static CORE_ADDR
318 ns32k_frame_saved_pc (struct frame_info *frame)
319 {
320 if (frame->signal_handler_caller)
321 return (sigtramp_saved_pc (frame)); /* XXXJRT */
322
323 return (read_memory_integer (frame->frame + 4, 4));
324 }
325
326 static CORE_ADDR
327 ns32k_frame_args_address (struct frame_info *frame)
328 {
329 if (ns32k_get_enter_addr (frame->pc) > 1)
330 return (frame->frame);
331
332 return (read_register (SP_REGNUM) - 4);
333 }
334
335 static CORE_ADDR
336 ns32k_frame_locals_address (struct frame_info *frame)
337 {
338 return (frame->frame);
339 }
340
341 /* Code to initialize the addresses of the saved registers of frame described
342 by FRAME_INFO. This includes special registers such as pc and fp saved in
343 special ways in the stack frame. sp is even more special: the address we
344 return for it IS the sp for the next frame. */
345
346 static void
347 ns32k_frame_init_saved_regs (struct frame_info *frame)
348 {
349 int regmask, regnum;
350 int localcount;
351 CORE_ADDR enter_addr, next_addr;
352
353 if (frame->saved_regs)
354 return;
355
356 frame_saved_regs_zalloc (frame);
357
358 enter_addr = ns32k_get_enter_addr (frame->pc);
359 if (enter_addr > 1)
360 {
361 regmask = read_memory_integer (enter_addr + 1, 1) & 0xff;
362 localcount = ns32k_localcount (enter_addr);
363 next_addr = frame->frame + localcount;
364
365 for (regnum = 0; regnum < 8; regnum++)
366 {
367 if (regmask & (1 << regnum))
368 frame->saved_regs[regnum] = next_addr -= 4;
369 }
370
371 frame->saved_regs[SP_REGNUM] = frame->frame + 4;
372 frame->saved_regs[PC_REGNUM] = frame->frame + 4;
373 frame->saved_regs[FP_REGNUM] = read_memory_integer (frame->frame, 4);
374 }
375 else if (enter_addr == 1)
376 {
377 CORE_ADDR sp = read_register (SP_REGNUM);
378 frame->saved_regs[PC_REGNUM] = sp;
379 frame->saved_regs[SP_REGNUM] = sp + 4;
380 }
381 }
382
383 static void
384 ns32k_push_dummy_frame (void)
385 {
386 CORE_ADDR sp = read_register (SP_REGNUM);
387 int regnum;
388
389 sp = push_word (sp, read_register (PC_REGNUM));
390 sp = push_word (sp, read_register (FP_REGNUM));
391 write_register (FP_REGNUM, sp);
392
393 for (regnum = 0; regnum < 8; regnum++)
394 sp = push_word (sp, read_register (regnum));
395
396 write_register (SP_REGNUM, sp);
397 }
398
399 static void
400 ns32k_pop_frame (void)
401 {
402 struct frame_info *frame = get_current_frame ();
403 CORE_ADDR fp;
404 int regnum;
405
406 fp = frame->frame;
407 FRAME_INIT_SAVED_REGS (frame);
408
409 for (regnum = 0; regnum < 8; regnum++)
410 if (frame->saved_regs[regnum])
411 write_register (regnum,
412 read_memory_integer (frame->saved_regs[regnum], 4));
413
414 write_register (FP_REGNUM, read_memory_integer (fp, 4));
415 write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
416 write_register (SP_REGNUM, fp + 8);
417 flush_cached_frames ();
418 }
419 \f
420 /* The NS32000 call dummy sequence:
421
422 enter 0xff,0 82 ff 00
423 jsr @0x00010203 7f ae c0 01 02 03
424 adjspd 0x69696969 7f a5 01 02 03 04
425 bpt f2
426
427 It is 16 bytes long. */
428
429 static LONGEST ns32k_call_dummy_words[] =
430 {
431 0x7f00ff82,
432 0x0201c0ae,
433 0x01a57f03,
434 0xf2040302
435 };
436 static int sizeof_ns32k_call_dummy_words = sizeof (ns32k_call_dummy_words);
437
438 #define NS32K_CALL_DUMMY_ADDR 5
439 #define NS32K_CALL_DUMMY_NARGS 11
440
441 static void
442 ns32k_fix_call_dummy (char *dummy, CORE_ADDR pc, CORE_ADDR fun, int nargs,
443 struct value **args, struct type *type, int gcc_p)
444 {
445 int flipped;
446
447 flipped = fun | 0xc0000000;
448 flip_bytes (&flipped, 4);
449 store_unsigned_integer (dummy + NS32K_CALL_DUMMY_ADDR, 4, flipped);
450
451 flipped = - nargs * 4;
452 flip_bytes (&flipped, 4);
453 store_unsigned_integer (dummy + NS32K_CALL_DUMMY_NARGS, 4, flipped);
454 }
455 \f
456 static void
457 ns32k_store_struct_return (CORE_ADDR addr, CORE_ADDR sp)
458 {
459 /* On this machine, this is a no-op (Encore Umax didn't use GCC). */
460 }
461
462 static void
463 ns32k_extract_return_value (struct type *valtype, char *regbuf, char *valbuf)
464 {
465 memcpy (valbuf,
466 regbuf + REGISTER_BYTE (TYPE_CODE (valtype) == TYPE_CODE_FLT ?
467 FP0_REGNUM : 0), TYPE_LENGTH (valtype));
468 }
469
470 static void
471 ns32k_store_return_value (struct type *valtype, char *valbuf)
472 {
473 deprecated_write_register_bytes (TYPE_CODE (valtype) == TYPE_CODE_FLT
474 ? FP0_REGNUM : 0, valbuf,
475 TYPE_LENGTH (valtype));
476 }
477
478 static CORE_ADDR
479 ns32k_extract_struct_value_address (char *regbuf)
480 {
481 return (extract_address (regbuf + REGISTER_BYTE (0), REGISTER_RAW_SIZE (0)));
482 }
483 \f
484 void
485 ns32k_gdbarch_init_32082 (struct gdbarch *gdbarch)
486 {
487 set_gdbarch_num_regs (gdbarch, NS32K_NUM_REGS_32082);
488
489 set_gdbarch_register_name (gdbarch, ns32k_register_name_32082);
490 set_gdbarch_register_bytes (gdbarch, NS32K_REGISTER_BYTES_32082);
491 set_gdbarch_register_byte (gdbarch, ns32k_register_byte_32082);
492 }
493
494 void
495 ns32k_gdbarch_init_32382 (struct gdbarch *gdbarch)
496 {
497 set_gdbarch_num_regs (gdbarch, NS32K_NUM_REGS_32382);
498
499 set_gdbarch_register_name (gdbarch, ns32k_register_name_32382);
500 set_gdbarch_register_bytes (gdbarch, NS32K_REGISTER_BYTES_32382);
501 set_gdbarch_register_byte (gdbarch, ns32k_register_byte_32382);
502 }
503
504 /* Initialize the current architecture based on INFO. If possible, re-use an
505 architecture from ARCHES, which is a list of architectures already created
506 during this debugging session.
507
508 Called e.g. at program startup, when reading a core file, and when reading
509 a binary file. */
510
511 static struct gdbarch *
512 ns32k_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
513 {
514 struct gdbarch_tdep *tdep;
515 struct gdbarch *gdbarch;
516 enum gdb_osabi osabi = GDB_OSABI_UNKNOWN;
517
518 /* Try to determine the OS ABI of the object we are loading. */
519 if (info.abfd != NULL)
520 {
521 osabi = gdbarch_lookup_osabi (info.abfd);
522 }
523
524 /* Find a candidate among extant architectures. */
525 for (arches = gdbarch_list_lookup_by_info (arches, &info);
526 arches != NULL;
527 arches = gdbarch_list_lookup_by_info (arches->next, &info))
528 {
529 /* Make sure the OS ABI selection matches. */
530 tdep = gdbarch_tdep (arches->gdbarch);
531 if (tdep && tdep->osabi == osabi)
532 return arches->gdbarch;
533 }
534
535 tdep = xmalloc (sizeof (struct gdbarch_tdep));
536 gdbarch = gdbarch_alloc (&info, tdep);
537
538 tdep->osabi = osabi;
539
540 /* Register info */
541 ns32k_gdbarch_init_32082 (gdbarch);
542 set_gdbarch_num_regs (gdbarch, NS32K_SP_REGNUM);
543 set_gdbarch_num_regs (gdbarch, NS32K_FP_REGNUM);
544 set_gdbarch_num_regs (gdbarch, NS32K_PC_REGNUM);
545 set_gdbarch_num_regs (gdbarch, NS32K_PS_REGNUM);
546
547 set_gdbarch_register_size (gdbarch, NS32K_REGISTER_SIZE);
548 set_gdbarch_register_raw_size (gdbarch, ns32k_register_raw_size);
549 set_gdbarch_max_register_raw_size (gdbarch, NS32K_MAX_REGISTER_RAW_SIZE);
550 set_gdbarch_register_virtual_size (gdbarch, ns32k_register_virtual_size);
551 set_gdbarch_max_register_virtual_size (gdbarch,
552 NS32K_MAX_REGISTER_VIRTUAL_SIZE);
553 set_gdbarch_register_virtual_type (gdbarch, ns32k_register_virtual_type);
554
555 /* Frame and stack info */
556 set_gdbarch_skip_prologue (gdbarch, umax_skip_prologue);
557 set_gdbarch_saved_pc_after_call (gdbarch, ns32k_saved_pc_after_call);
558
559 set_gdbarch_frame_num_args (gdbarch, umax_frame_num_args);
560 set_gdbarch_frameless_function_invocation (gdbarch,
561 generic_frameless_function_invocation_not);
562
563 set_gdbarch_frame_chain (gdbarch, ns32k_frame_chain);
564 set_gdbarch_frame_chain_valid (gdbarch, func_frame_chain_valid);
565 set_gdbarch_frame_saved_pc (gdbarch, ns32k_frame_saved_pc);
566
567 set_gdbarch_frame_args_address (gdbarch, ns32k_frame_args_address);
568 set_gdbarch_frame_locals_address (gdbarch, ns32k_frame_locals_address);
569
570 set_gdbarch_frame_init_saved_regs (gdbarch, ns32k_frame_init_saved_regs);
571
572 set_gdbarch_frame_args_skip (gdbarch, 8);
573
574 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
575
576 /* Return value info */
577 set_gdbarch_store_struct_return (gdbarch, ns32k_store_struct_return);
578 set_gdbarch_deprecated_extract_return_value (gdbarch, ns32k_extract_return_value);
579 set_gdbarch_deprecated_store_return_value (gdbarch, ns32k_store_return_value);
580 set_gdbarch_deprecated_extract_struct_value_address (gdbarch,
581 ns32k_extract_struct_value_address);
582
583 /* Call dummy info */
584 set_gdbarch_push_dummy_frame (gdbarch, ns32k_push_dummy_frame);
585 set_gdbarch_pop_frame (gdbarch, ns32k_pop_frame);
586 set_gdbarch_call_dummy_location (gdbarch, ON_STACK);
587 set_gdbarch_call_dummy_p (gdbarch, 1);
588 set_gdbarch_call_dummy_words (gdbarch, ns32k_call_dummy_words);
589 set_gdbarch_sizeof_call_dummy_words (gdbarch, sizeof_ns32k_call_dummy_words);
590 set_gdbarch_fix_call_dummy (gdbarch, ns32k_fix_call_dummy);
591 set_gdbarch_call_dummy_start_offset (gdbarch, 3);
592 set_gdbarch_call_dummy_breakpoint_offset_p (gdbarch, 0);
593 set_gdbarch_use_generic_dummy_frames (gdbarch, 0);
594 set_gdbarch_pc_in_call_dummy (gdbarch, pc_in_call_dummy_on_stack);
595 set_gdbarch_call_dummy_stack_adjust_p (gdbarch, 0);
596
597 /* Breakpoint info */
598 set_gdbarch_decr_pc_after_break (gdbarch, 0);
599 set_gdbarch_breakpoint_from_pc (gdbarch, ns32k_breakpoint_from_pc);
600
601 /* Misc info */
602 set_gdbarch_function_start_offset (gdbarch, 0);
603
604 /* Hook in OS ABI-specific overrides, if they have been registered. */
605 gdbarch_init_osabi (info, gdbarch, osabi);
606
607 return (gdbarch);
608 }
609
610 static void
611 ns32k_dump_tdep (struct gdbarch *current_gdbarch, struct ui_file *file)
612 {
613 struct gdbarch_tdep *tdep = gdbarch_tdep (current_gdbarch);
614
615 if (tdep == NULL)
616 return;
617
618 fprintf_unfiltered (file, "ns32k_dump_tdep: OS ABI = %s\n",
619 gdbarch_osabi_name (tdep->osabi));
620 }
621
622 void
623 _initialize_ns32k_tdep (void)
624 {
625 gdbarch_register (bfd_arch_ns32k, ns32k_gdbarch_init, ns32k_dump_tdep);
626
627 tm_print_insn = print_insn_ns32k;
628 }
This page took 0.122529 seconds and 5 git commands to generate.