1 /* ARC target-dependent stuff.
2 Copyright (C) 1995 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, Boston, MA 02111-1307, USA. */
25 #include "floatformat.h"
29 /* Current CPU, set with the "set cpu" command. */
30 static int arc_bfd_mach_type;
32 char *tmp_arc_cpu_type;
34 /* Table of cpu names. */
38 } arc_cpu_type_table[] = {
39 { "base", bfd_mach_arc_base },
40 { "host", bfd_mach_arc_host },
41 { "graphics", bfd_mach_arc_graphics },
42 { "audio", bfd_mach_arc_audio },
46 /* Used by simulator. */
47 int display_pipeline_p;
49 /* This one must have the same type as used in the emulator.
50 It's currently an enum so this should be ok for now. */
53 #define ARC_CALL_SAVED_REG(r) ((r) >= 16 && (r) < 24)
55 #define OPMASK 0xf8000000
57 /* Instruction field accessor macros.
58 See the Programmer's Reference Manual. */
59 #define X_OP(i) (((i) >> 27) & 0x1f)
60 #define X_A(i) (((i) >> 21) & 0x3f)
61 #define X_B(i) (((i) >> 15) & 0x3f)
62 #define X_C(i) (((i) >> 9) & 0x3f)
63 #define X_D(i) ((((i) & 0x1ff) ^ 0x100) - 0x100)
64 #define X_L(i) (((((i) >> 5) & 0x3ffffc) ^ 0x200000) - 0x200000)
65 #define X_N(i) (((i) >> 5) & 3)
66 #define X_Q(i) ((i) & 0x1f)
68 /* Return non-zero if X is a short immediate data indicator. */
69 #define SHIMM_P(x) ((x) == 61 || (x) == 63)
71 /* Return non-zero if X is a "long" (32 bit) immediate data indicator. */
72 #define LIMM_P(x) ((x) == 62)
74 /* Build a simple instruction. */
75 #define BUILD_INSN(op, a, b, c, d) \
76 ((((op) & 31) << 27) \
77 | (((a) & 63) << 21) \
78 | (((b) & 63) << 15) \
82 /* Codestream stuff. */
83 static void codestream_read PARAMS ((unsigned int *, int));
84 static void codestream_seek PARAMS ((CORE_ADDR));
85 static unsigned int codestream_fill PARAMS ((int));
87 #define CODESTREAM_BUFSIZ 16
88 static CORE_ADDR codestream_next_addr;
89 static CORE_ADDR codestream_addr;
90 static unsigned int codestream_buf[CODESTREAM_BUFSIZ];
91 static int codestream_off;
92 static int codestream_cnt;
94 #define codestream_tell() \
95 (codestream_addr + codestream_off * sizeof (codestream_buf[0]))
96 #define codestream_peek() \
97 (codestream_cnt == 0 \
98 ? codestream_fill (1) \
99 : codestream_buf[codestream_off])
100 #define codestream_get() \
101 (codestream_cnt-- == 0 \
102 ? codestream_fill (0) \
103 : codestream_buf[codestream_off++])
106 codestream_fill (peek_flag)
109 codestream_addr = codestream_next_addr;
110 codestream_next_addr += CODESTREAM_BUFSIZ * sizeof (codestream_buf[0]);
112 codestream_cnt = CODESTREAM_BUFSIZ;
113 read_memory (codestream_addr, (char *) codestream_buf,
114 CODESTREAM_BUFSIZ * sizeof (codestream_buf[0]));
115 /* FIXME: check return code? */
117 /* Handle byte order differences. */
118 if (HOST_BYTE_ORDER != TARGET_BYTE_ORDER)
120 register unsigned int i, j, n = sizeof (codestream_buf[0]);
121 register char tmp, *p;
122 for (i = 0, p = (char *) codestream_buf; i < CODESTREAM_BUFSIZ;
124 for (j = 0; j < n / 2; ++j)
125 tmp = p[j], p[j] = p[n - 1 - j], p[n - 1 - j] = tmp;
129 return codestream_peek ();
131 return codestream_get ();
135 codestream_seek (place)
138 codestream_next_addr = place / CODESTREAM_BUFSIZ;
139 codestream_next_addr *= CODESTREAM_BUFSIZ;
142 while (codestream_tell () != place)
146 /* This function is currently unused but leave in for now. */
149 codestream_read (buf, count)
156 for (i = 0; i < count; i++)
157 *p++ = codestream_get ();
160 /* Set up prologue scanning and return the first insn. */
163 setup_prologue_scan (pc)
168 codestream_seek (pc);
169 insn = codestream_get ();
175 * Find & return amount a local space allocated, and advance codestream to
176 * first register push (if any).
177 * If entry sequence doesn't make sense, return -1, and leave
178 * codestream pointer random.
182 arc_get_frame_setup (pc)
186 /* Size of frame or -1 if unrecognizable prologue. */
188 /* An initial "sub sp,sp,N" may or may not be for a stdarg fn. */
189 int maybe_stdarg_decr = -1;
191 insn = setup_prologue_scan (pc);
193 /* The authority for what appears here is the home-grown ABI.
194 The most recent version is 1.2. */
196 /* First insn may be "sub sp,sp,N" if stdarg fn. */
197 if ((insn & BUILD_INSN (-1, -1, -1, -1, 0))
198 == BUILD_INSN (10, SP_REGNUM, SP_REGNUM, SHIMM_REGNUM, 0))
200 maybe_stdarg_decr = X_D (insn);
201 insn = codestream_get ();
204 if ((insn & BUILD_INSN (-1, 0, -1, -1, -1)) /* st blink,[sp,4] */
205 == BUILD_INSN (2, 0, SP_REGNUM, BLINK_REGNUM, 4))
207 insn = codestream_get ();
208 /* Frame may not be necessary, even though blink is saved.
209 At least this is something we recognize. */
213 if ((insn & BUILD_INSN (-1, 0, -1, -1, -1)) /* st fp,[sp] */
214 == BUILD_INSN (2, 0, SP_REGNUM, FP_REGNUM, 0))
216 insn = codestream_get ();
217 if ((insn & BUILD_INSN (-1, -1, -1, -1, 0))
218 != BUILD_INSN (12, FP_REGNUM, SP_REGNUM, SP_REGNUM, 0))
221 /* Check for stack adjustment sub sp,sp,N. */
222 insn = codestream_peek ();
223 if ((insn & BUILD_INSN (-1, -1, -1, 0, 0))
224 == BUILD_INSN (10, SP_REGNUM, SP_REGNUM, 0, 0))
226 if (LIMM_P (X_C (insn)))
227 frame_size = codestream_get ();
228 else if (SHIMM_P (X_C (insn)))
229 frame_size = X_D (insn);
237 /* This sequence is used to get the address of the return
238 buffer for a function that returns a structure. */
239 insn = codestream_peek ();
240 if (insn & OPMASK == 0x60000000)
250 /* If we found a "sub sp,sp,N" and nothing else, it may or may not be a
251 stdarg fn. The stdarg decrement is not treated as part of the frame size,
252 so we have a dilemma: what do we return? For now, if we get a
253 "sub sp,sp,N" and nothing else assume this isn't a stdarg fn. One way
254 to fix this completely would be to add a bit to the function descriptor
255 that says the function is a stdarg function. */
257 if (frame_size < 0 && maybe_stdarg_decr > 0)
258 return maybe_stdarg_decr;
262 /* Given a pc value, skip it forward past the function prologue by
263 disassembling instructions that appear to be a prologue.
265 If FRAMELESS_P is set, we are only testing to see if the function
266 is frameless. If it is a frameless function, return PC unchanged.
267 This allows a quicker answer. */
270 skip_prologue (pc, frameless_p)
277 if ((frame_size = arc_get_frame_setup (pc)) < 0)
281 return frame_size == 0 ? pc : codestream_tell ();
283 /* Skip over register saves. */
284 for (i = 0; i < 8; i++)
286 insn = codestream_peek ();
287 if ((insn & BUILD_INSN (-1, 0, -1, 0, 0))
288 != BUILD_INSN (2, 0, SP_REGNUM, 0, 0))
289 break; /* not st insn */
290 if (! ARC_CALL_SAVED_REG (X_C (insn)))
295 return codestream_tell ();
298 /* Return the return address for a frame.
299 This is used to implement FRAME_SAVED_PC.
300 This is taken from frameless_look_for_prologue. */
303 arc_frame_saved_pc (frame)
304 struct frame_info *frame;
306 CORE_ADDR func_start;
309 func_start = get_pc_function_start (frame->pc) + FUNCTION_START_OFFSET;
313 return ARC_PC_TO_REAL_ADDRESS (read_memory_integer (FRAME_FP (frame) + 4, 4));
316 /* The authority for what appears here is the home-grown ABI.
317 The most recent version is 1.2. */
319 insn = setup_prologue_scan (func_start);
321 /* First insn may be "sub sp,sp,N" if stdarg fn. */
322 if ((insn & BUILD_INSN (-1, -1, -1, -1, 0))
323 == BUILD_INSN (10, SP_REGNUM, SP_REGNUM, SHIMM_REGNUM, 0))
324 insn = codestream_get ();
326 /* If the next insn is "st blink,[sp,4]" we can get blink from there.
327 Otherwise this is a leaf function and we can use blink. Note that
328 this still allows for the case where a leaf function saves/clobbers/
331 if ((insn & BUILD_INSN (-1, 0, -1, -1, -1)) /* st blink,[sp,4] */
332 != BUILD_INSN (2, 0, SP_REGNUM, BLINK_REGNUM, 4))
333 return ARC_PC_TO_REAL_ADDRESS (read_register (BLINK_REGNUM));
335 return ARC_PC_TO_REAL_ADDRESS (read_memory_integer (FRAME_FP (frame) + 4, 4));
339 * Parse the first few instructions of the function to see
340 * what registers were stored.
342 * The startup sequence can be at the start of the function.
343 * 'st blink,[sp+4], st fp,[sp], mov fp,sp'
345 * Local space is allocated just below by sub sp,sp,nnn.
346 * Next, the registers used by this function are stored (as offsets from sp).
350 frame_find_saved_regs (fip, fsrp)
351 struct frame_info *fip;
352 struct frame_saved_regs *fsrp;
356 CORE_ADDR dummy_bottom;
358 int i, regnum, offset;
360 memset (fsrp, 0, sizeof *fsrp);
362 /* If frame is the end of a dummy, compute where the beginning would be. */
363 dummy_bottom = fip->frame - 4 - REGISTER_BYTES - CALL_DUMMY_LENGTH;
365 /* Check if the PC is in the stack, in a dummy frame. */
366 if (dummy_bottom <= fip->pc && fip->pc <= fip->frame)
368 /* all regs were saved by push_call_dummy () */
370 for (i = 0; i < NUM_REGS; i++)
372 adr -= REGISTER_RAW_SIZE (i);
378 locals = arc_get_frame_setup (get_pc_function_start (fip->pc));
382 /* Set `adr' to the value of `sp'. */
383 adr = fip->frame - locals;
384 for (i = 0; i < 8; i++)
386 insn = codestream_get ();
387 if ((insn & BUILD_INSN (-1, 0, -1, 0, 0))
388 != BUILD_INSN (2, 0, SP_REGNUM, 0, 0))
392 fsrp->regs[regnum] = adr + offset;
396 fsrp->regs[PC_REGNUM] = fip->frame + 4;
397 fsrp->regs[FP_REGNUM] = fip->frame;
403 CORE_ADDR sp = read_register (SP_REGNUM);
405 char regbuf[MAX_REGISTER_RAW_SIZE];
407 read_register_gen (PC_REGNUM, regbuf);
408 write_memory (sp+4, regbuf, REGISTER_SIZE);
409 read_register_gen (FP_REGNUM, regbuf);
410 write_memory (sp, regbuf, REGISTER_SIZE);
411 write_register (FP_REGNUM, sp);
412 for (regnum = 0; regnum < NUM_REGS; regnum++)
414 read_register_gen (regnum, regbuf);
415 sp = push_bytes (sp, regbuf, REGISTER_RAW_SIZE (regnum));
417 sp += (2*REGISTER_SIZE);
418 write_register (SP_REGNUM, sp);
424 struct frame_info *frame = get_current_frame ();
427 struct frame_saved_regs fsr;
428 char regbuf[MAX_REGISTER_RAW_SIZE];
430 fp = FRAME_FP (frame);
431 get_frame_saved_regs (frame, &fsr);
432 for (regnum = 0; regnum < NUM_REGS; regnum++)
435 adr = fsr.regs[regnum];
438 read_memory (adr, regbuf, REGISTER_RAW_SIZE (regnum));
439 write_register_bytes (REGISTER_BYTE (regnum), regbuf,
440 REGISTER_RAW_SIZE (regnum));
443 write_register (FP_REGNUM, read_memory_integer (fp, 4));
444 write_register (PC_REGNUM, read_memory_integer (fp + 4, 4));
445 write_register (SP_REGNUM, fp + 8);
446 flush_cached_frames ();
449 /* Simulate single-step. */
453 NORMAL4, /* a normal 4 byte insn */
454 NORMAL8, /* a normal 8 byte insn */
455 BRANCH4, /* a 4 byte branch insn, including ones without delay slots */
456 BRANCH8, /* an 8 byte branch insn, including ones with delay slots */
459 /* Return the type of INSN and store in TARGET the destination address of a
460 branch if this is one. */
461 /* ??? Need to verify all cases are properly handled. */
464 get_insn_type (insn, pc, target)
466 CORE_ADDR pc, *target;
472 case 0 : case 1 : case 2 : /* load/store insns */
473 if (LIMM_P (X_A (insn))
474 || LIMM_P (X_B (insn))
475 || LIMM_P (X_C (insn)))
478 case 4 : case 5 : case 6 : /* branch insns */
479 *target = pc + 4 + X_L (insn);
480 /* ??? It isn't clear that this is always the right answer.
481 The problem occurs when the next insn is an 8 byte insn. If the
482 branch is conditional there's no worry as there shouldn't be an 8
483 byte insn following. The programmer may be cheating if s/he knows
484 the branch will never be taken, but we don't deal with that.
485 Note that the programmer is also allowed to play games by putting
486 an insn with long immediate data in the delay slot and then duplicate
487 the long immediate data at the branch target. Ugh! */
491 case 7 : /* jump insns */
492 if (LIMM_P (X_B (insn)))
494 limm = read_memory_integer (pc + 4, 4);
495 *target = ARC_PC_TO_REAL_ADDRESS (limm);
498 if (SHIMM_P (X_B (insn)))
499 *target = ARC_PC_TO_REAL_ADDRESS (X_D (insn));
501 *target = ARC_PC_TO_REAL_ADDRESS (read_register (X_B (insn)));
502 if (X_Q (insn) == 0 && X_N (insn) == 0)
505 default : /* arithmetic insns, etc. */
506 if (LIMM_P (X_A (insn))
507 || LIMM_P (X_B (insn))
508 || LIMM_P (X_C (insn)))
514 /* Non-zero if we just simulated a single-step. This is needed because we
515 cannot remove the breakpoints in the inferior process until after the
516 `wait' in `wait_for_inferior'. */
520 /* single_step() is called just before we want to resume the inferior, if we
521 want to single-step it but there is no hardware or kernel single-step
522 support. We find all the possible targets of the coming instruction and
525 single_step is also called just after the inferior stops. If we had
526 set up a simulated single-step, we undo our damage. */
530 int ignore; /* sig, but we don't need it */
532 static CORE_ADDR next_pc, target;
534 typedef char binsn_quantum[BREAKPOINT_MAX];
535 static binsn_quantum break_mem[2];
543 pc = read_register (PC_REGNUM);
544 insn = read_memory_integer (pc, 4);
545 type = get_insn_type (insn, pc, &target);
547 /* Always set a breakpoint for the insn after the branch. */
548 next_pc = pc + ((type == NORMAL8 || type == BRANCH8) ? 8 : 4);
549 target_insert_breakpoint (next_pc, break_mem[0]);
553 if ((type == BRANCH4 || type == BRANCH8)
554 /* Watch out for branches to the following location.
555 We just stored a breakpoint there and another call to
556 target_insert_breakpoint will think the real insn is the
557 breakpoint we just stored there. */
558 && target != next_pc)
561 target_insert_breakpoint (target, break_mem[1]);
564 /* We are ready to let it go. */
569 /* Remove breakpoints. */
570 target_remove_breakpoint (next_pc, break_mem[0]);
573 target_remove_breakpoint (target, break_mem[1]);
576 stop_pc -= DECR_PC_AFTER_BREAK;
583 #ifdef GET_LONGJMP_TARGET
584 /* Figure out where the longjmp will land. Slurp the args out of the stack.
585 We expect the first arg to be a pointer to the jmp_buf structure from which
586 we extract the pc (JB_PC) that we will land at. The pc is copied into PC.
587 This routine returns true on success. */
590 get_longjmp_target(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 (vma, info)
620 disassemble_info *info;
622 static int current_mach;
623 static int current_endian;
624 static disassembler_ftype current_disasm;
626 if (current_disasm == NULL
627 || arc_bfd_mach_type != current_mach
628 || TARGET_BYTE_ORDER != current_endian)
630 current_mach = arc_bfd_mach_type;
631 current_endian = TARGET_BYTE_ORDER;
632 current_disasm = arc_get_disassembler (current_mach,
633 current_endian == BIG_ENDIAN);
636 return (*current_disasm) (vma, info);
639 /* Command to set cpu type. */
642 arc_set_cpu_type_command (args, from_tty)
648 if (tmp_arc_cpu_type == NULL || *tmp_arc_cpu_type == '\0')
650 printf_unfiltered ("The known ARC cpu types are as follows:\n");
651 for (i = 0; arc_cpu_type_table[i].name != NULL; ++i)
652 printf_unfiltered ("%s\n", arc_cpu_type_table[i].name);
654 /* Restore the value. */
655 tmp_arc_cpu_type = strsave (arc_cpu_type);
660 if (!arc_set_cpu_type (tmp_arc_cpu_type))
662 error ("Unknown cpu type `%s'.", tmp_arc_cpu_type);
663 /* Restore its value. */
664 tmp_arc_cpu_type = strsave (arc_cpu_type);
669 arc_show_cpu_type_command (args, from_tty)
675 /* Modify the actual cpu type.
676 Result is a boolean indicating success. */
679 arc_set_cpu_type (str)
687 for (i = 0; arc_cpu_type_table[i].name != NULL; ++i)
689 if (strcasecmp (str, arc_cpu_type_table[i].name) == 0)
692 arc_bfd_mach_type = arc_cpu_type_table[i].value;
701 _initialize_arc_tdep ()
703 struct cmd_list_element *c;
705 c = add_set_cmd ("cpu", class_support, var_string_noescape,
706 (char *) &tmp_arc_cpu_type,
707 "Set the type of ARC cpu in use.\n\
708 This command has two purposes. In a multi-cpu system it lets one\n\
709 change the cpu being debugged. It also gives one access to\n\
710 cpu-type-specific registers and recognize cpu-type-specific instructions.\
713 c->function.cfunc = arc_set_cpu_type_command;
714 c = add_show_from_set (c, &showlist);
715 c->function.cfunc = arc_show_cpu_type_command;
717 /* We have to use strsave here because the `set' command frees it before
718 setting a new value. */
719 tmp_arc_cpu_type = strsave (DEFAULT_ARC_CPU_TYPE);
720 arc_set_cpu_type (tmp_arc_cpu_type);
722 c = add_set_cmd ("displaypipeline", class_support, var_zinteger,
723 (char *) &display_pipeline_p,
724 "Set pipeline display (simulator only).\n\
725 When enabled, the state of the pipeline after each cycle is displayed.",
727 c = add_show_from_set (c, &showlist);
729 c = add_set_cmd ("debugpipeline", class_support, var_zinteger,
730 (char *) &debug_pipeline_p,
731 "Set pipeline debug display (simulator only).\n\
732 When enabled, debugging information about the pipeline is displayed.",
734 c = add_show_from_set (c, &showlist);
736 c = add_set_cmd ("cputimer", class_support, var_zinteger,
738 "Set maximum cycle count (simulator only).\n\
739 Control will return to gdb if the timer expires.\n\
740 A negative value disables the timer.",
742 c = add_show_from_set (c, &showlist);
744 tm_print_insn = arc_print_insn;