1 /* ARC target-dependent stuff.
2 Copyright 1995, 1996, 1999, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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. */
26 #include "floatformat.h"
33 static int arc_set_cpu_type (char *str);
35 /* Current CPU, set with the "set cpu" command. */
36 static int arc_bfd_mach_type;
38 char *tmp_arc_cpu_type;
40 /* Table of cpu names. */
46 arc_cpu_type_table[] =
48 { "arc5", bfd_mach_arc_5 },
49 { "arc6", bfd_mach_arc_6 },
50 { "arc7", bfd_mach_arc_7 },
51 { "arc8", bfd_mach_arc_8 },
55 /* Used by simulator. */
56 int display_pipeline_p;
58 /* This one must have the same type as used in the emulator.
59 It's currently an enum so this should be ok for now. */
62 #define ARC_CALL_SAVED_REG(r) ((r) >= 16 && (r) < 24)
64 #define OPMASK 0xf8000000
66 /* Instruction field accessor macros.
67 See the Programmer's Reference Manual. */
68 #define X_OP(i) (((i) >> 27) & 0x1f)
69 #define X_A(i) (((i) >> 21) & 0x3f)
70 #define X_B(i) (((i) >> 15) & 0x3f)
71 #define X_C(i) (((i) >> 9) & 0x3f)
72 #define X_D(i) ((((i) & 0x1ff) ^ 0x100) - 0x100)
73 #define X_L(i) (((((i) >> 5) & 0x3ffffc) ^ 0x200000) - 0x200000)
74 #define X_N(i) (((i) >> 5) & 3)
75 #define X_Q(i) ((i) & 0x1f)
77 /* Return non-zero if X is a short immediate data indicator. */
78 #define SHIMM_P(x) ((x) == 61 || (x) == 63)
80 /* Return non-zero if X is a "long" (32 bit) immediate data indicator. */
81 #define LIMM_P(x) ((x) == 62)
83 /* Build a simple instruction. */
84 #define BUILD_INSN(op, a, b, c, d) \
85 ((((op) & 31) << 27) \
86 | (((a) & 63) << 21) \
87 | (((b) & 63) << 15) \
91 /* Codestream stuff. */
92 static void codestream_read (unsigned int *, int);
93 static void codestream_seek (CORE_ADDR);
94 static unsigned int codestream_fill (int);
96 #define CODESTREAM_BUFSIZ 16
97 static CORE_ADDR codestream_next_addr;
98 static CORE_ADDR codestream_addr;
99 /* FIXME assumes sizeof (int) == 32? */
100 static unsigned int codestream_buf[CODESTREAM_BUFSIZ];
101 static int codestream_off;
102 static int codestream_cnt;
104 #define codestream_tell() \
105 (codestream_addr + codestream_off * sizeof (codestream_buf[0]))
106 #define codestream_peek() \
107 (codestream_cnt == 0 \
108 ? codestream_fill (1) \
109 : codestream_buf[codestream_off])
110 #define codestream_get() \
111 (codestream_cnt-- == 0 \
112 ? codestream_fill (0) \
113 : codestream_buf[codestream_off++])
116 codestream_fill (int peek_flag)
118 codestream_addr = codestream_next_addr;
119 codestream_next_addr += CODESTREAM_BUFSIZ * sizeof (codestream_buf[0]);
121 codestream_cnt = CODESTREAM_BUFSIZ;
122 read_memory (codestream_addr, (char *) codestream_buf,
123 CODESTREAM_BUFSIZ * sizeof (codestream_buf[0]));
124 /* FIXME: check return code? */
127 /* Handle byte order differences -> convert to host byte ordering. */
130 for (i = 0; i < CODESTREAM_BUFSIZ; i++)
132 extract_unsigned_integer (&codestream_buf[i],
133 sizeof (codestream_buf[i]));
137 return codestream_peek ();
139 return codestream_get ();
143 codestream_seek (CORE_ADDR place)
145 codestream_next_addr = place / CODESTREAM_BUFSIZ;
146 codestream_next_addr *= CODESTREAM_BUFSIZ;
149 while (codestream_tell () != place)
153 /* This function is currently unused but leave in for now. */
156 codestream_read (unsigned int *buf, int count)
161 for (i = 0; i < count; i++)
162 *p++ = codestream_get ();
165 /* Set up prologue scanning and return the first insn. */
168 setup_prologue_scan (CORE_ADDR pc)
172 codestream_seek (pc);
173 insn = codestream_get ();
179 * Find & return amount a local space allocated, and advance codestream to
180 * first register push (if any).
181 * If entry sequence doesn't make sense, return -1, and leave
182 * codestream pointer random.
186 arc_get_frame_setup (CORE_ADDR pc)
189 /* Size of frame or -1 if unrecognizable prologue. */
191 /* An initial "sub sp,sp,N" may or may not be for a stdarg fn. */
192 int maybe_stdarg_decr = -1;
194 insn = setup_prologue_scan (pc);
196 /* The authority for what appears here is the home-grown ABI.
197 The most recent version is 1.2. */
199 /* First insn may be "sub sp,sp,N" if stdarg fn. */
200 if ((insn & BUILD_INSN (-1, -1, -1, -1, 0))
201 == BUILD_INSN (10, SP_REGNUM, SP_REGNUM, SHIMM_REGNUM, 0))
203 maybe_stdarg_decr = X_D (insn);
204 insn = codestream_get ();
207 if ((insn & BUILD_INSN (-1, 0, -1, -1, -1)) /* st blink,[sp,4] */
208 == BUILD_INSN (2, 0, SP_REGNUM, BLINK_REGNUM, 4))
210 insn = codestream_get ();
211 /* Frame may not be necessary, even though blink is saved.
212 At least this is something we recognize. */
216 if ((insn & BUILD_INSN (-1, 0, -1, -1, -1)) /* st fp,[sp] */
217 == BUILD_INSN (2, 0, SP_REGNUM, FP_REGNUM, 0))
219 insn = codestream_get ();
220 if ((insn & BUILD_INSN (-1, -1, -1, -1, 0))
221 != BUILD_INSN (12, FP_REGNUM, SP_REGNUM, SP_REGNUM, 0))
224 /* Check for stack adjustment sub sp,sp,N. */
225 insn = codestream_peek ();
226 if ((insn & BUILD_INSN (-1, -1, -1, 0, 0))
227 == BUILD_INSN (10, SP_REGNUM, SP_REGNUM, 0, 0))
229 if (LIMM_P (X_C (insn)))
230 frame_size = codestream_get ();
231 else if (SHIMM_P (X_C (insn)))
232 frame_size = X_D (insn);
240 /* This sequence is used to get the address of the return
241 buffer for a function that returns a structure. */
242 insn = codestream_peek ();
243 if ((insn & OPMASK) == 0x60000000)
253 /* If we found a "sub sp,sp,N" and nothing else, it may or may not be a
254 stdarg fn. The stdarg decrement is not treated as part of the frame size,
255 so we have a dilemma: what do we return? For now, if we get a
256 "sub sp,sp,N" and nothing else assume this isn't a stdarg fn. One way
257 to fix this completely would be to add a bit to the function descriptor
258 that says the function is a stdarg function. */
260 if (frame_size < 0 && maybe_stdarg_decr > 0)
261 return maybe_stdarg_decr;
265 /* Given a pc value, skip it forward past the function prologue by
266 disassembling instructions that appear to be a prologue.
268 If FRAMELESS_P is set, we are only testing to see if the function
269 is frameless. If it is a frameless function, return PC unchanged.
270 This allows a quicker answer. */
273 arc_skip_prologue (CORE_ADDR pc, int frameless_p)
278 if ((frame_size = arc_get_frame_setup (pc)) < 0)
282 return frame_size == 0 ? pc : codestream_tell ();
284 /* Skip over register saves. */
285 for (i = 0; i < 8; i++)
287 insn = codestream_peek ();
288 if ((insn & BUILD_INSN (-1, 0, -1, 0, 0))
289 != BUILD_INSN (2, 0, SP_REGNUM, 0, 0))
290 break; /* not st insn */
291 if (!ARC_CALL_SAVED_REG (X_C (insn)))
296 return codestream_tell ();
299 /* Is the prologue at PC frameless? */
302 arc_prologue_frameless_p (CORE_ADDR pc)
304 return (pc == arc_skip_prologue (pc, 1));
307 /* Return the return address for a frame.
308 This is used to implement FRAME_SAVED_PC.
309 This is taken from frameless_look_for_prologue. */
312 arc_frame_saved_pc (struct frame_info *frame)
314 CORE_ADDR func_start;
317 func_start = get_pc_function_start (frame->pc) + FUNCTION_START_OFFSET;
321 return ARC_PC_TO_REAL_ADDRESS (read_memory_integer (FRAME_FP (frame) + 4, 4));
324 /* The authority for what appears here is the home-grown ABI.
325 The most recent version is 1.2. */
327 insn = setup_prologue_scan (func_start);
329 /* First insn may be "sub sp,sp,N" if stdarg fn. */
330 if ((insn & BUILD_INSN (-1, -1, -1, -1, 0))
331 == BUILD_INSN (10, SP_REGNUM, SP_REGNUM, SHIMM_REGNUM, 0))
332 insn = codestream_get ();
334 /* If the next insn is "st blink,[sp,4]" we can get blink from there.
335 Otherwise this is a leaf function and we can use blink. Note that
336 this still allows for the case where a leaf function saves/clobbers/
339 if ((insn & BUILD_INSN (-1, 0, -1, -1, -1)) /* st blink,[sp,4] */
340 != BUILD_INSN (2, 0, SP_REGNUM, BLINK_REGNUM, 4))
341 return ARC_PC_TO_REAL_ADDRESS (read_register (BLINK_REGNUM));
343 return ARC_PC_TO_REAL_ADDRESS (read_memory_integer (FRAME_FP (frame) + 4, 4));
347 * Parse the first few instructions of the function to see
348 * what registers were stored.
350 * The startup sequence can be at the start of the function.
351 * 'st blink,[sp+4], st fp,[sp], mov fp,sp'
353 * Local space is allocated just below by sub sp,sp,nnn.
354 * Next, the registers used by this function are stored (as offsets from sp).
358 frame_find_saved_regs (struct frame_info *fip, struct frame_saved_regs *fsrp)
362 CORE_ADDR dummy_bottom;
364 int i, regnum, offset;
366 memset (fsrp, 0, sizeof *fsrp);
368 /* If frame is the end of a dummy, compute where the beginning would be. */
369 dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH;
371 /* Check if the PC is in the stack, in a dummy frame. */
372 if (dummy_bottom <= fip->pc && fip->pc <= fip->frame)
374 /* all regs were saved by push_call_dummy () */
376 for (i = 0; i < NUM_REGS; i++)
378 adr -= REGISTER_RAW_SIZE (i);
384 locals = arc_get_frame_setup (get_pc_function_start (fip->pc));
388 /* Set `adr' to the value of `sp'. */
389 adr = fip->frame - locals;
390 for (i = 0; i < 8; i++)
392 insn = codestream_get ();
393 if ((insn & BUILD_INSN (-1, 0, -1, 0, 0))
394 != BUILD_INSN (2, 0, SP_REGNUM, 0, 0))
398 fsrp->regs[regnum] = adr + offset;
402 fsrp->regs[PC_REGNUM] = fip->frame + 4;
403 fsrp->regs[FP_REGNUM] = fip->frame;
407 arc_push_dummy_frame (void)
409 CORE_ADDR sp = read_register (SP_REGNUM);
411 char regbuf[MAX_REGISTER_RAW_SIZE];
413 read_register_gen (PC_REGNUM, regbuf);
414 write_memory (sp + 4, regbuf, REGISTER_SIZE);
415 read_register_gen (FP_REGNUM, regbuf);
416 write_memory (sp, regbuf, REGISTER_SIZE);
417 write_register (FP_REGNUM, sp);
418 for (regnum = 0; regnum < NUM_REGS; regnum++)
420 read_register_gen (regnum, regbuf);
421 sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum));
423 sp += (2 * REGISTER_SIZE);
424 write_register (SP_REGNUM, sp);
430 struct frame_info *frame = get_current_frame ();
433 struct frame_saved_regs fsr;
434 char regbuf[MAX_REGISTER_RAW_SIZE];
436 fp = FRAME_FP (frame);
437 get_frame_saved_regs (frame, &fsr);
438 for (regnum = 0; regnum < NUM_REGS; regnum++)
441 adr = fsr.regs[regnum];
444 read_memory (adr, regbuf, REGISTER_RAW_SIZE (regnum));
445 write_register_bytes (REGISTER_BYTE (regnum), regbuf,
446 REGISTER_RAW_SIZE (regnum));
449 write_register (FP_REGNUM, read_memory_integer (fp, 4));
450 write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
451 write_register (SP_REGNUM, fp + 8);
452 flush_cached_frames ();
455 /* Simulate single-step. */
459 NORMAL4, /* a normal 4 byte insn */
460 NORMAL8, /* a normal 8 byte insn */
461 BRANCH4, /* a 4 byte branch insn, including ones without delay slots */
462 BRANCH8, /* an 8 byte branch insn, including ones with delay slots */
466 /* Return the type of INSN and store in TARGET the destination address of a
467 branch if this is one. */
468 /* ??? Need to verify all cases are properly handled. */
471 get_insn_type (unsigned long insn, CORE_ADDR pc, CORE_ADDR *target)
479 case 2: /* load/store insns */
480 if (LIMM_P (X_A (insn))
481 || LIMM_P (X_B (insn))
482 || LIMM_P (X_C (insn)))
487 case 6: /* branch insns */
488 *target = pc + 4 + X_L (insn);
489 /* ??? It isn't clear that this is always the right answer.
490 The problem occurs when the next insn is an 8 byte insn. If the
491 branch is conditional there's no worry as there shouldn't be an 8
492 byte insn following. The programmer may be cheating if s/he knows
493 the branch will never be taken, but we don't deal with that.
494 Note that the programmer is also allowed to play games by putting
495 an insn with long immediate data in the delay slot and then duplicate
496 the long immediate data at the branch target. Ugh! */
500 case 7: /* jump insns */
501 if (LIMM_P (X_B (insn)))
503 limm = read_memory_integer (pc + 4, 4);
504 *target = ARC_PC_TO_REAL_ADDRESS (limm);
507 if (SHIMM_P (X_B (insn)))
508 *target = ARC_PC_TO_REAL_ADDRESS (X_D (insn));
510 *target = ARC_PC_TO_REAL_ADDRESS (read_register (X_B (insn)));
511 if (X_Q (insn) == 0 && X_N (insn) == 0)
514 default: /* arithmetic insns, etc. */
515 if (LIMM_P (X_A (insn))
516 || LIMM_P (X_B (insn))
517 || LIMM_P (X_C (insn)))
523 /* single_step() is called just before we want to resume the inferior, if we
524 want to single-step it but there is no hardware or kernel single-step
525 support. We find all the possible targets of the coming instruction and
528 single_step is also called just after the inferior stops. If we had
529 set up a simulated single-step, we undo our damage. */
532 arc_software_single_step (enum target_signal ignore, /* sig but we don't need it */
533 int insert_breakpoints_p)
535 static CORE_ADDR next_pc, target;
537 typedef char binsn_quantum[BREAKPOINT_MAX];
538 static binsn_quantum break_mem[2];
540 if (insert_breakpoints_p)
546 pc = read_register (PC_REGNUM);
547 insn = read_memory_integer (pc, 4);
548 type = get_insn_type (insn, pc, &target);
550 /* Always set a breakpoint for the insn after the branch. */
551 next_pc = pc + ((type == NORMAL8 || type == BRANCH8) ? 8 : 4);
552 target_insert_breakpoint (next_pc, break_mem[0]);
556 if ((type == BRANCH4 || type == BRANCH8)
557 /* Watch out for branches to the following location.
558 We just stored a breakpoint there and another call to
559 target_insert_breakpoint will think the real insn is the
560 breakpoint we just stored there. */
561 && target != next_pc)
564 target_insert_breakpoint (target, break_mem[1]);
570 /* Remove breakpoints. */
571 target_remove_breakpoint (next_pc, break_mem[0]);
574 target_remove_breakpoint (target, break_mem[1]);
577 stop_pc -= DECR_PC_AFTER_BREAK;
582 /* Because of Multi-arch, GET_LONGJMP_TARGET is always defined. So test
583 for a definition of JB_PC. */
585 /* Figure out where the longjmp will land. Slurp the args out of the stack.
586 We expect the first arg to be a pointer to the jmp_buf structure from which
587 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
588 This routine returns true on success. */
591 get_longjmp_target (CORE_ADDR *pc)
593 char buf[TARGET_PTR_BIT / TARGET_CHAR_BIT];
594 CORE_ADDR sp, jb_addr;
596 sp = read_register (SP_REGNUM);
598 if (target_read_memory (sp + SP_ARG0, /* Offset of first arg on stack */
600 TARGET_PTR_BIT / TARGET_CHAR_BIT))
603 jb_addr = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
605 if (target_read_memory (jb_addr + JB_PC * JB_ELEMENT_SIZE, buf,
606 TARGET_PTR_BIT / TARGET_CHAR_BIT))
609 *pc = extract_address (buf, TARGET_PTR_BIT / TARGET_CHAR_BIT);
613 #endif /* GET_LONGJMP_TARGET */
615 /* Disassemble one instruction. */
618 arc_print_insn (bfd_vma vma, disassemble_info *info)
620 static int current_mach;
621 static int current_endian;
622 static disassembler_ftype current_disasm;
624 if (current_disasm == NULL
625 || arc_bfd_mach_type != current_mach
626 || TARGET_BYTE_ORDER != current_endian)
628 current_mach = arc_bfd_mach_type;
629 current_endian = TARGET_BYTE_ORDER;
630 current_disasm = arc_get_disassembler (NULL);
633 return (*current_disasm) (vma, info);
636 /* Command to set cpu type. */
639 arc_set_cpu_type_command (char *args, int from_tty)
643 if (tmp_arc_cpu_type == NULL || *tmp_arc_cpu_type == '\0')
645 printf_unfiltered ("The known ARC cpu types are as follows:\n");
646 for (i = 0; arc_cpu_type_table[i].name != NULL; ++i)
647 printf_unfiltered ("%s\n", arc_cpu_type_table[i].name);
649 /* Restore the value. */
650 tmp_arc_cpu_type = xstrdup (arc_cpu_type);
655 if (!arc_set_cpu_type (tmp_arc_cpu_type))
657 error ("Unknown cpu type `%s'.", tmp_arc_cpu_type);
658 /* Restore its value. */
659 tmp_arc_cpu_type = xstrdup (arc_cpu_type);
664 arc_show_cpu_type_command (char *args, int from_tty)
668 /* Modify the actual cpu type.
669 Result is a boolean indicating success. */
672 arc_set_cpu_type (char *str)
679 for (i = 0; arc_cpu_type_table[i].name != NULL; ++i)
681 if (strcasecmp (str, arc_cpu_type_table[i].name) == 0)
684 arc_bfd_mach_type = arc_cpu_type_table[i].value;
693 _initialize_arc_tdep (void)
695 struct cmd_list_element *c;
697 c = add_set_cmd ("cpu", class_support, var_string_noescape,
698 (char *) &tmp_arc_cpu_type,
699 "Set the type of ARC cpu in use.\n\
700 This command has two purposes. In a multi-cpu system it lets one\n\
701 change the cpu being debugged. It also gives one access to\n\
702 cpu-type-specific registers and recognize cpu-type-specific instructions.\
705 set_cmd_cfunc (c, arc_set_cpu_type_command);
706 c = add_show_from_set (c, &showlist);
707 set_cmd_cfunc (c, arc_show_cpu_type_command);
709 /* We have to use xstrdup() here because the `set' command frees it
710 before setting a new value. */
711 tmp_arc_cpu_type = xstrdup (DEFAULT_ARC_CPU_TYPE);
712 arc_set_cpu_type (tmp_arc_cpu_type);
714 c = add_set_cmd ("displaypipeline", class_support, var_zinteger,
715 (char *) &display_pipeline_p,
716 "Set pipeline display (simulator only).\n\
717 When enabled, the state of the pipeline after each cycle is displayed.",
719 c = add_show_from_set (c, &showlist);
721 c = add_set_cmd ("debugpipeline", class_support, var_zinteger,
722 (char *) &debug_pipeline_p,
723 "Set pipeline debug display (simulator only).\n\
724 When enabled, debugging information about the pipeline is displayed.",
726 c = add_show_from_set (c, &showlist);
728 c = add_set_cmd ("cputimer", class_support, var_zinteger,
730 "Set maximum cycle count (simulator only).\n\
731 Control will return to gdb if the timer expires.\n\
732 A negative value disables the timer.",
734 c = add_show_from_set (c, &showlist);
736 tm_print_insn = arc_print_insn;