1 /* Target-dependent code for the S+core architecture, for GDB,
4 Copyright (C) 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
6 Contributed by Qinwei (qinwei@sunnorth.com.cn)
7 Contributed by Ching-Peng Lin (cplin@sunplus.com)
9 This file is part of GDB.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>. */
25 #include "gdb_assert.h"
31 #include "arch-utils.h"
35 #include "frame-unwind.h"
36 #include "frame-base.h"
37 #include "trad-frame.h"
38 #include "dwarf2-frame.h"
39 #include "score-tdep.h"
41 #define G_FLD(_i,_ms,_ls) \
42 ((unsigned)((_i) << (31 - (_ms))) >> (31 - (_ms) + (_ls)))
46 unsigned long long raw;
50 struct score_frame_cache
54 struct trad_frame_saved_reg *saved_regs;
57 static int target_mach = bfd_mach_score7;
61 score_target_can_use_watch (int type, int cnt, int othertype)
63 if (strcmp (current_target.to_shortname, "sim") == 0)
64 return soc_gh_can_use_watch (type, cnt);
65 return (*current_target.to_can_use_hw_breakpoint) (type, cnt, othertype);
69 score_stopped_by_watch (void)
71 if (strcmp (current_target.to_shortname, "sim") == 0)
72 return soc_gh_stopped_by_watch ();
73 return (*current_target.to_stopped_by_watchpoint) ();
77 score_target_insert_watchpoint (CORE_ADDR addr, int len, int type)
79 if (strcmp (current_target.to_shortname, "sim") == 0)
80 return soc_gh_add_watch (addr, len, type);
81 return (*current_target.to_insert_watchpoint) (addr, len, type);
85 score_target_remove_watchpoint (CORE_ADDR addr, int len, int type)
87 if (strcmp (current_target.to_shortname, "sim") == 0)
88 return soc_gh_del_watch (addr, len, type);
89 return (*current_target.to_remove_watchpoint) (addr, len, type);
93 score_target_insert_hw_breakpoint (struct gdbarch *gdbarch,
94 struct bp_target_info * bp_tgt)
96 if (strcmp (current_target.to_shortname, "sim") == 0)
97 return soc_gh_add_hardbp (bp_tgt->placed_address);
98 return (*current_target.to_insert_hw_breakpoint) (gdbarch, bp_tgt);
102 score_target_remove_hw_breakpoint (struct gdbarch *gdbarch,
103 struct bp_target_info * bp_tgt)
105 if (strcmp (current_target.to_shortname, "sim") == 0)
106 return soc_gh_del_hardbp (bp_tgt->placed_address);
107 return (*current_target.to_remove_hw_breakpoint) (gdbarch, bp_tgt);
112 score_register_type (struct gdbarch *gdbarch, int regnum)
114 gdb_assert (regnum >= 0
115 && regnum < ((target_mach == bfd_mach_score7) ? SCORE7_NUM_REGS : SCORE3_NUM_REGS));
116 return builtin_type (gdbarch)->builtin_uint32;
120 score_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
122 return frame_unwind_register_unsigned (next_frame, SCORE_SP_REGNUM);
126 score_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
128 return frame_unwind_register_unsigned (next_frame, SCORE_PC_REGNUM);
132 score7_register_name (struct gdbarch *gdbarch, int regnum)
134 const char *score_register_names[] = {
135 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
136 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
137 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
138 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
140 "PSR", "COND", "ECR", "EXCPVEC", "CCR",
141 "EPC", "EMA", "TLBLOCK", "TLBPT", "PEADDR",
142 "TLBRPT", "PEVN", "PECTX", "LIMPFN", "LDMPFN",
143 "PREV", "DREG", "PC", "DSAVE", "COUNTER",
144 "LDCR", "STCR", "CEH", "CEL",
147 gdb_assert (regnum >= 0 && regnum < SCORE7_NUM_REGS);
148 return score_register_names[regnum];
152 score3_register_name (struct gdbarch *gdbarch, int regnum)
154 const char *score_register_names[] = {
155 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
156 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
157 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
158 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
160 "PSR", "COND", "ECR", "EXCPVEC", "CCR",
161 "EPC", "EMA", "PREV", "DREG", "DSAVE",
162 "COUNTER", "LDCR", "STCR", "CEH", "CEL",
166 gdb_assert (regnum >= 0 && regnum < SCORE3_NUM_REGS);
167 return score_register_names[regnum];
172 score_register_sim_regno (struct gdbarch *gdbarch, int regnum)
174 gdb_assert (regnum >= 0
175 && regnum < ((target_mach == bfd_mach_score7) ? SCORE7_NUM_REGS : SCORE3_NUM_REGS));
181 score_print_insn (bfd_vma memaddr, struct disassemble_info *info)
183 if (info->endian == BFD_ENDIAN_BIG)
184 return print_insn_big_score (memaddr, info);
186 return print_insn_little_score (memaddr, info);
190 score7_fetch_inst (struct gdbarch *gdbarch, CORE_ADDR addr, char *memblock)
192 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
193 static inst_t inst = { 0, 0, 0 };
194 char buf[SCORE_INSTLEN] = { 0 };
198 if (target_has_execution && memblock != NULL)
200 /* Fetch instruction from local MEMBLOCK. */
201 memcpy (buf, memblock, SCORE_INSTLEN);
205 /* Fetch instruction from target. */
206 ret = target_read_memory (addr & ~0x3, buf, SCORE_INSTLEN);
209 error ("Error: target_read_memory in file:%s, line:%d!",
215 inst.raw = extract_unsigned_integer (buf, SCORE_INSTLEN, byte_order);
216 inst.len = (inst.raw & 0x80008000) ? 4 : 2;
217 inst.v = ((inst.raw >> 16 & 0x7FFF) << 15) | (inst.raw & 0x7FFF);
218 big = (byte_order == BFD_ENDIAN_BIG);
221 if (big ^ ((addr & 0x2) == 2))
222 inst.v = G_FLD (inst.v, 29, 15);
224 inst.v = G_FLD (inst.v, 14, 0);
230 score3_adjust_pc_and_fetch_inst (CORE_ADDR *pcptr, int *lenptr,
231 enum bfd_endian byte_order)
233 static inst_t inst = { 0, 0, 0 };
240 /* raw table 1 (column 2, 3, 4)
244 table 2 (column 1, 2, 3)
250 static const struct breakplace bk_table[16] =
272 #define EXTRACT_LEN 2
273 CORE_ADDR adjust_pc = *pcptr & ~0x1;
275 gdb_byte buf[5][EXTRACT_LEN] =
285 unsigned int cbits = 0;
294 for (i = 0; i < 5; i++)
296 ret = target_read_memory (adjust_pc + 2 * i, buf[i], EXTRACT_LEN);
302 error ("Error: target_read_memory in file:%s, line:%d!",
306 raw = extract_unsigned_integer (buf[i], EXTRACT_LEN, byte_order);
307 cbits = (cbits << 1) | (raw >> 15);
314 cbits = (cbits >> 1) & 0x7;
320 cbits = (cbits >> 2) & 0x7;
321 bk_index = cbits + 8;
324 gdb_assert (!((bk_table[bk_index].break_offset == 0)
325 && (bk_table[bk_index].inst_len == 0)));
327 inst.len = bk_table[bk_index].inst_len;
329 i = (bk_table[bk_index].break_offset + 4) / 2;
330 count = inst.len / 2;
331 for (; count > 0; i++, count--)
333 inst.raw = (inst.raw << 16)
334 | extract_unsigned_integer (buf[i], EXTRACT_LEN, byte_order);
340 inst.v = inst.raw & 0x7FFF;
343 inst.v = ((inst.raw >> 16 & 0x7FFF) << 15) | (inst.raw & 0x7FFF);
346 inst.v = ((inst.raw >> 32 & 0x7FFF) << 30)
347 | ((inst.raw >> 16 & 0x7FFF) << 15) | (inst.raw & 0x7FFF);
352 *pcptr = adjust_pc + bk_table[bk_index].break_offset;
354 *lenptr = bk_table[bk_index].inst_len;
361 static const gdb_byte *
362 score7_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
365 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
366 gdb_byte buf[SCORE_INSTLEN] = { 0 };
370 if ((ret = target_read_memory (*pcptr & ~0x3, buf, SCORE_INSTLEN)) != 0)
372 error ("Error: target_read_memory in file:%s, line:%d!",
375 raw = extract_unsigned_integer (buf, SCORE_INSTLEN, byte_order);
377 if (byte_order == BFD_ENDIAN_BIG)
379 if (!(raw & 0x80008000))
381 /* 16bits instruction. */
382 static gdb_byte big_breakpoint16[] = { 0x60, 0x02 };
384 *lenptr = sizeof (big_breakpoint16);
385 return big_breakpoint16;
389 /* 32bits instruction. */
390 static gdb_byte big_breakpoint32[] = { 0x80, 0x00, 0x80, 0x06 };
392 *lenptr = sizeof (big_breakpoint32);
393 return big_breakpoint32;
398 if (!(raw & 0x80008000))
400 /* 16bits instruction. */
401 static gdb_byte little_breakpoint16[] = { 0x02, 0x60 };
403 *lenptr = sizeof (little_breakpoint16);
404 return little_breakpoint16;
408 /* 32bits instruction. */
409 static gdb_byte little_breakpoint32[] = { 0x06, 0x80, 0x00, 0x80 };
411 *lenptr = sizeof (little_breakpoint32);
412 return little_breakpoint32;
417 static const gdb_byte *
418 score3_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
421 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
422 CORE_ADDR adjust_pc = *pcptr;
424 static gdb_byte score_break_insns[6][6] = {
425 /* The following three instructions are big endian. */
427 { 0x80, 0x00, 0x00, 0x06 },
428 { 0x80, 0x00, 0x80, 0x00, 0x00, 0x00 },
429 /* The following three instructions are little endian. */
431 { 0x00, 0x80, 0x06, 0x00 },
432 { 0x00, 0x80, 0x00, 0x80, 0x00, 0x00 }};
437 score3_adjust_pc_and_fetch_inst (&adjust_pc, &len, byte_order);
439 index = ((byte_order == BFD_ENDIAN_BIG) ? 0 : 3) + (len / 2 - 1);
440 p = score_break_insns[index];
449 score_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
451 CORE_ADDR adjust_pc = bpaddr;
453 if (target_mach == bfd_mach_score3)
454 score3_adjust_pc_and_fetch_inst (&adjust_pc, NULL,
455 gdbarch_byte_order (gdbarch));
457 adjust_pc = align_down (adjust_pc, 2);
463 score_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
465 return align_down (addr, 16);
469 score_xfer_register (struct regcache *regcache, int regnum, int length,
470 enum bfd_endian endian, gdb_byte *readbuf,
471 const gdb_byte *writebuf, int buf_offset)
474 gdb_assert (regnum >= 0
475 && regnum < ((target_mach == bfd_mach_score7) ? SCORE7_NUM_REGS : SCORE3_NUM_REGS));
480 reg_offset = SCORE_REGSIZE - length;
482 case BFD_ENDIAN_LITTLE:
485 case BFD_ENDIAN_UNKNOWN:
489 error ("Error: score_xfer_register in file:%s, line:%d!",
494 regcache_cooked_read_part (regcache, regnum, reg_offset, length,
495 readbuf + buf_offset);
496 if (writebuf != NULL)
497 regcache_cooked_write_part (regcache, regnum, reg_offset, length,
498 writebuf + buf_offset);
501 static enum return_value_convention
502 score_return_value (struct gdbarch *gdbarch, struct type *func_type,
503 struct type *type, struct regcache *regcache,
504 gdb_byte * readbuf, const gdb_byte * writebuf)
506 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
507 || TYPE_CODE (type) == TYPE_CODE_UNION
508 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
509 return RETURN_VALUE_STRUCT_CONVENTION;
514 for (offset = 0, regnum = SCORE_A0_REGNUM;
515 offset < TYPE_LENGTH (type);
516 offset += SCORE_REGSIZE, regnum++)
518 int xfer = SCORE_REGSIZE;
519 if (offset + xfer > TYPE_LENGTH (type))
520 xfer = TYPE_LENGTH (type) - offset;
521 score_xfer_register (regcache, regnum, xfer,
522 gdbarch_byte_order(gdbarch),
523 readbuf, writebuf, offset);
525 return RETURN_VALUE_REGISTER_CONVENTION;
529 static struct frame_id
530 score_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
532 return frame_id_build (
533 get_frame_register_unsigned (this_frame, SCORE_SP_REGNUM),
534 get_frame_pc (this_frame));
538 score_type_needs_double_align (struct type *type)
540 enum type_code typecode = TYPE_CODE (type);
542 if ((typecode == TYPE_CODE_INT && TYPE_LENGTH (type) == 8)
543 || (typecode == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8))
545 else if (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION)
549 n = TYPE_NFIELDS (type);
550 for (i = 0; i < n; i++)
551 if (score_type_needs_double_align (TYPE_FIELD_TYPE (type, i)))
559 score_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
560 struct regcache *regcache, CORE_ADDR bp_addr,
561 int nargs, struct value **args, CORE_ADDR sp,
562 int struct_return, CORE_ADDR struct_addr)
564 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
568 CORE_ADDR stack_offset = 0;
571 /* Step 1, Save RA. */
572 regcache_cooked_write_unsigned (regcache, SCORE_RA_REGNUM, bp_addr);
574 /* Step 2, Make space on the stack for the args. */
575 struct_addr = align_down (struct_addr, 16);
576 sp = align_down (sp, 16);
577 for (argnum = 0; argnum < nargs; argnum++)
578 arglen += align_up (TYPE_LENGTH (value_type (args[argnum])),
580 sp -= align_up (arglen, 16);
582 argreg = SCORE_BEGIN_ARG_REGNUM;
584 /* Step 3, Check if struct return then save the struct address to
585 r4 and increase the stack_offset by 4. */
588 regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
589 stack_offset += SCORE_REGSIZE;
592 /* Step 4, Load arguments:
593 If arg length is too long (> 4 bytes), then split the arg and
595 for (argnum = 0; argnum < nargs; argnum++)
597 struct value *arg = args[argnum];
598 struct type *arg_type = check_typedef (value_type (arg));
599 enum type_code typecode = TYPE_CODE (arg_type);
600 const gdb_byte *val = value_contents (arg);
601 int downward_offset = 0;
602 int odd_sized_struct_p;
603 int arg_last_part_p = 0;
605 arglen = TYPE_LENGTH (arg_type);
606 odd_sized_struct_p = (arglen > SCORE_REGSIZE
607 && arglen % SCORE_REGSIZE != 0);
609 /* If a arg should be aligned to 8 bytes (long long or double),
610 the value should be put to even register numbers. */
611 if (score_type_needs_double_align (arg_type))
617 /* If sizeof a block < SCORE_REGSIZE, then Score GCC will chose
618 the default "downward"/"upward" method:
624 char a; char b; char c;
625 } s = {'a', 'b', 'c'};
627 Big endian: s = {X, 'a', 'b', 'c'}
628 Little endian: s = {'a', 'b', 'c', X}
630 Where X is a hole. */
632 if (gdbarch_byte_order(gdbarch) == BFD_ENDIAN_BIG
633 && (typecode == TYPE_CODE_STRUCT
634 || typecode == TYPE_CODE_UNION)
635 && argreg > SCORE_LAST_ARG_REGNUM
636 && arglen < SCORE_REGSIZE)
637 downward_offset += (SCORE_REGSIZE - arglen);
641 int partial_len = arglen < SCORE_REGSIZE ? arglen : SCORE_REGSIZE;
642 ULONGEST regval = extract_unsigned_integer (val, partial_len,
645 /* The last part of a arg should shift left when
646 gdbarch_byte_order is BFD_ENDIAN_BIG. */
647 if (byte_order == BFD_ENDIAN_BIG
648 && arg_last_part_p == 1
649 && (typecode == TYPE_CODE_STRUCT
650 || typecode == TYPE_CODE_UNION))
651 regval <<= ((SCORE_REGSIZE - partial_len) * TARGET_CHAR_BIT);
653 /* Always increase the stack_offset and save args to stack. */
654 addr = sp + stack_offset + downward_offset;
655 write_memory (addr, val, partial_len);
657 if (argreg <= SCORE_LAST_ARG_REGNUM)
659 regcache_cooked_write_unsigned (regcache, argreg++, regval);
660 if (arglen > SCORE_REGSIZE && arglen < SCORE_REGSIZE * 2)
665 arglen -= partial_len;
666 stack_offset += align_up (partial_len, SCORE_REGSIZE);
670 /* Step 5, Save SP. */
671 regcache_cooked_write_unsigned (regcache, SCORE_SP_REGNUM, sp);
677 score7_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
680 int iscan = 32, stack_sub = 0;
683 inst_t *inst = score7_fetch_inst (gdbarch, cpc, NULL);
686 if ((inst->len == 4) && !stack_sub
687 && (G_FLD (inst->v, 29, 25) == 0x1
688 && G_FLD (inst->v, 24, 20) == 0x0))
690 /* addi r0, offset */
691 stack_sub = cpc + SCORE_INSTLEN;
692 pc = cpc + SCORE_INSTLEN;
694 else if ((inst->len == 4)
695 && (G_FLD (inst->v, 29, 25) == 0x0)
696 && (G_FLD (inst->v, 24, 20) == 0x2)
697 && (G_FLD (inst->v, 19, 15) == 0x0)
698 && (G_FLD (inst->v, 14, 10) == 0xF)
699 && (G_FLD (inst->v, 9, 0) == 0x56))
702 pc = cpc + SCORE_INSTLEN;
705 else if ((inst->len == 2)
706 && (G_FLD (inst->v, 14, 12) == 0x0)
707 && (G_FLD (inst->v, 11, 8) == 0x2)
708 && (G_FLD (inst->v, 7, 4) == 0x0)
709 && (G_FLD (inst->v, 3, 0) == 0x3))
712 pc = cpc + SCORE16_INSTLEN;
715 else if ((inst->len == 2)
716 && ((G_FLD (inst->v, 14, 12) == 3) /* j15 form */
717 || (G_FLD (inst->v, 14, 12) == 4) /* b15 form */
718 || (G_FLD (inst->v, 14, 12) == 0x0
719 && G_FLD (inst->v, 3, 0) == 0x4))) /* br! */
721 else if ((inst->len == 4)
722 && ((G_FLD (inst->v, 29, 25) == 2) /* j32 form */
723 || (G_FLD (inst->v, 29, 25) == 4) /* b32 form */
724 || (G_FLD (inst->v, 29, 25) == 0x0
725 && G_FLD (inst->v, 6, 1) == 0x4))) /* br */
728 cpc += (inst->len == 2) ? SCORE16_INSTLEN : SCORE_INSTLEN;
734 score3_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
737 int iscan = 32, stack_sub = 0;
741 = score3_adjust_pc_and_fetch_inst (&cpc, NULL, gdbarch_byte_order (gdbarch));
745 if (inst->len == 4 && !stack_sub
746 && (G_FLD (inst->v, 29, 25) == 0x1)
747 && (G_FLD (inst->v, 19, 17) == 0x0)
748 && (G_FLD (inst->v, 24, 20) == 0x0))
750 /* addi r0, offset */
751 stack_sub = cpc + inst->len;
752 pc = cpc + inst->len;
754 else if (inst->len == 4
755 && (G_FLD (inst->v, 29, 25) == 0x0)
756 && (G_FLD (inst->v, 24, 20) == 0x2)
757 && (G_FLD (inst->v, 19, 15) == 0x0)
758 && (G_FLD (inst->v, 14, 10) == 0xF)
759 && (G_FLD (inst->v, 9, 0) == 0x56))
762 pc = cpc + inst->len;
765 else if ((inst->len == 2)
766 && (G_FLD (inst->v, 14, 10) == 0x10)
767 && (G_FLD (inst->v, 9, 5) == 0x2)
768 && (G_FLD (inst->v, 4, 0) == 0x0))
771 pc = cpc + inst->len;
774 else if (inst->len == 2
775 && ((G_FLD (inst->v, 14, 12) == 3) /* b15 form */
776 || (G_FLD (inst->v, 14, 12) == 0x0
777 && G_FLD (inst->v, 11, 5) == 0x4))) /* br! */
779 else if (inst->len == 4
780 && ((G_FLD (inst->v, 29, 25) == 2) /* j32 form */
781 || (G_FLD (inst->v, 29, 25) == 4))) /* b32 form */
790 score7_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR cur_pc)
792 inst_t *inst = score7_fetch_inst (gdbarch, cur_pc, NULL);
795 return 1; /* mv! r0, r2 */
796 else if (G_FLD (inst->v, 14, 12) == 0x2
797 && G_FLD (inst->v, 3, 0) == 0xa)
799 else if (G_FLD (inst->v, 14, 12) == 0x0
800 && G_FLD (inst->v, 7, 0) == 0x34)
801 return 1; /* br! r3 */
802 else if (G_FLD (inst->v, 29, 15) == 0x2
803 && G_FLD (inst->v, 6, 1) == 0x2b)
804 return 1; /* mv r0, r2 */
805 else if (G_FLD (inst->v, 29, 25) == 0x0
806 && G_FLD (inst->v, 6, 1) == 0x4
807 && G_FLD (inst->v, 19, 15) == 0x3)
808 return 1; /* br r3 */
814 score3_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR cur_pc)
816 CORE_ADDR pc = cur_pc;
818 = score3_adjust_pc_and_fetch_inst (&pc, NULL, gdbarch_byte_order (gdbarch));
821 && (G_FLD (inst->v, 14, 10) == 0x10)
822 && (G_FLD (inst->v, 9, 5) == 0x0)
823 && (G_FLD (inst->v, 4, 0) == 0x2))
824 return 1; /* mv! r0, r2 */
825 else if (inst->len == 4
826 && (G_FLD (inst->v, 29, 25) == 0x0)
827 && (G_FLD (inst->v, 24, 20) == 0x2)
828 && (G_FLD (inst->v, 19, 15) == 0x0)
829 && (G_FLD (inst->v, 14, 10) == 0xF)
830 && (G_FLD (inst->v, 9, 0) == 0x56))
831 return 1; /* mv r0, r2 */
832 else if (inst->len == 2
833 && (G_FLD (inst->v, 14, 12) == 0x0)
834 && (G_FLD (inst->v, 11, 5) == 0x2))
836 else if (inst->len == 2
837 && (G_FLD (inst->v, 14, 12) == 0x0)
838 && (G_FLD (inst->v, 11, 7) == 0x0)
839 && (G_FLD (inst->v, 6, 5) == 0x2))
840 return 1; /* rpop! */
841 else if (inst->len == 2
842 && (G_FLD (inst->v, 14, 12) == 0x0)
843 && (G_FLD (inst->v, 11, 5) == 0x4)
844 && (G_FLD (inst->v, 4, 0) == 0x3))
845 return 1; /* br! r3 */
846 else if (inst->len == 4
847 && (G_FLD (inst->v, 29, 25) == 0x0)
848 && (G_FLD (inst->v, 24, 20) == 0x0)
849 && (G_FLD (inst->v, 19, 15) == 0x3)
850 && (G_FLD (inst->v, 14, 10) == 0xF)
851 && (G_FLD (inst->v, 9, 0) == 0x8))
852 return 1; /* br r3 */
858 score7_malloc_and_get_memblock (CORE_ADDR addr, CORE_ADDR size)
861 char *memblock = NULL;
865 error ("Error: malloc size < 0 in file:%s, line:%d!",
872 memblock = (char *) xmalloc (size);
873 memset (memblock, 0, size);
874 ret = target_read_memory (addr & ~0x3, memblock, size);
877 error ("Error: target_read_memory in file:%s, line:%d!",
885 score7_free_memblock (char *memblock)
891 score7_adjust_memblock_ptr (char **memblock, CORE_ADDR prev_pc,
896 /* First time call this function, do nothing. */
898 else if (cur_pc - prev_pc == 2 && (cur_pc & 0x3) == 0)
900 /* First 16-bit instruction, then 32-bit instruction. */
901 *memblock += SCORE_INSTLEN;
903 else if (cur_pc - prev_pc == 4)
905 /* Is 32-bit instruction, increase MEMBLOCK by 4. */
906 *memblock += SCORE_INSTLEN;
911 score7_analyze_prologue (CORE_ADDR startaddr, CORE_ADDR pc,
912 struct frame_info *this_frame,
913 struct score_frame_cache *this_cache)
915 struct gdbarch *gdbarch = get_frame_arch (this_frame);
918 CORE_ADDR cur_pc = startaddr;
927 char *memblock = NULL;
928 char *memblock_ptr = NULL;
929 CORE_ADDR prev_pc = -1;
931 /* Allocate MEMBLOCK if PC - STARTADDR > 0. */
932 memblock_ptr = memblock =
933 score7_malloc_and_get_memblock (startaddr, pc - startaddr);
935 sp = get_frame_register_unsigned (this_frame, SCORE_SP_REGNUM);
936 fp = get_frame_register_unsigned (this_frame, SCORE_FP_REGNUM);
938 for (; cur_pc < pc; prev_pc = cur_pc, cur_pc += inst_len)
941 if (memblock != NULL)
943 /* Reading memory block from target succefully and got all
944 the instructions(from STARTADDR to PC) needed. */
945 score7_adjust_memblock_ptr (&memblock, prev_pc, cur_pc);
946 inst = score7_fetch_inst (gdbarch, cur_pc, memblock);
950 /* Otherwise, we fetch 4 bytes from target, and GDB also
952 inst = score7_fetch_inst (gdbarch, cur_pc, NULL);
955 /* FIXME: make a full-power prologue analyzer */
958 inst_len = SCORE16_INSTLEN;
960 if (G_FLD (inst->v, 14, 12) == 0x2
961 && G_FLD (inst->v, 3, 0) == 0xe)
966 if (G_FLD (inst->v, 11, 7) == 0x6
970 ra_offset = sp_offset;
973 else if (G_FLD (inst->v, 11, 7) == 0x4
977 fp_offset = sp_offset;
981 else if (G_FLD (inst->v, 14, 12) == 0x2
982 && G_FLD (inst->v, 3, 0) == 0xa)
987 else if (G_FLD (inst->v, 14, 7) == 0xc1
988 && G_FLD (inst->v, 2, 0) == 0x0)
991 sp_offset += (int) pow (2, G_FLD (inst->v, 6, 3));
993 else if (G_FLD (inst->v, 14, 7) == 0xc0
994 && G_FLD (inst->v, 2, 0) == 0x0)
997 sp_offset -= (int) pow (2, G_FLD (inst->v, 6, 3));
1002 inst_len = SCORE_INSTLEN;
1004 if (G_FLD(inst->v, 29, 25) == 0x3
1005 && G_FLD(inst->v, 2, 0) == 0x4
1006 && G_FLD(inst->v, 19, 15) == 0)
1008 /* sw rD, [r0, offset]+ */
1009 sp_offset += SCORE_INSTLEN;
1011 if (G_FLD(inst->v, 24, 20) == 0x3)
1014 if (ra_offset_p == 0)
1016 ra_offset = sp_offset;
1020 else if (G_FLD(inst->v, 24, 20) == 0x2)
1023 if (fp_offset_p == 0)
1025 fp_offset = sp_offset;
1030 else if (G_FLD(inst->v, 29, 25) == 0x14
1031 && G_FLD(inst->v, 19,15) == 0)
1033 /* sw rD, [r0, offset] */
1034 if (G_FLD(inst->v, 24, 20) == 0x3)
1037 ra_offset = sp_offset - G_FLD(inst->v, 14, 0);
1040 else if (G_FLD(inst->v, 24, 20) == 0x2)
1043 fp_offset = sp_offset - G_FLD(inst->v, 14, 0);
1047 else if (G_FLD (inst->v, 29, 15) == 0x1c60
1048 && G_FLD (inst->v, 2, 0) == 0x0)
1050 /* lw r3, [r0]+, 4 */
1051 sp_offset -= SCORE_INSTLEN;
1054 else if (G_FLD (inst->v, 29, 15) == 0x1c40
1055 && G_FLD (inst->v, 2, 0) == 0x0)
1057 /* lw r2, [r0]+, 4 */
1058 sp_offset -= SCORE_INSTLEN;
1062 else if (G_FLD (inst->v, 29, 17) == 0x100
1063 && G_FLD (inst->v, 0, 0) == 0x0)
1065 /* addi r0, -offset */
1066 sp_offset += 65536 - G_FLD (inst->v, 16, 1);
1068 else if (G_FLD (inst->v, 29, 17) == 0x110
1069 && G_FLD (inst->v, 0, 0) == 0x0)
1071 /* addi r2, offset */
1072 if (pc - cur_pc > 4)
1074 unsigned int save_v = inst->v;
1076 score7_fetch_inst (gdbarch, cur_pc + SCORE_INSTLEN, NULL);
1077 if (inst2->v == 0x23)
1080 sp_offset -= G_FLD (save_v, 16, 1);
1088 if (ra_offset_p == 1)
1090 if (this_cache->saved_regs[SCORE_PC_REGNUM].addr == -1)
1091 this_cache->saved_regs[SCORE_PC_REGNUM].addr =
1092 sp + sp_offset - ra_offset;
1096 this_cache->saved_regs[SCORE_PC_REGNUM] =
1097 this_cache->saved_regs[SCORE_RA_REGNUM];
1101 if (fp_offset_p == 1)
1103 if (this_cache->saved_regs[SCORE_FP_REGNUM].addr == -1)
1104 this_cache->saved_regs[SCORE_FP_REGNUM].addr =
1105 sp + sp_offset - fp_offset;
1108 /* Save SP and FP. */
1109 this_cache->base = sp + sp_offset;
1110 this_cache->fp = fp;
1112 /* Don't forget to free MEMBLOCK if we allocated it. */
1113 if (memblock_ptr != NULL)
1114 score7_free_memblock (memblock_ptr);
1118 score3_analyze_prologue (CORE_ADDR startaddr, CORE_ADDR pc,
1119 struct frame_info *this_frame,
1120 struct score_frame_cache *this_cache)
1124 CORE_ADDR cur_pc = startaddr;
1125 enum bfd_endian byte_order = gdbarch_byte_order (get_frame_arch (this_frame));
1130 int ra_offset_p = 0;
1131 int fp_offset_p = 0;
1134 CORE_ADDR prev_pc = -1;
1136 sp = get_frame_register_unsigned (this_frame, SCORE_SP_REGNUM);
1137 fp = get_frame_register_unsigned (this_frame, SCORE_FP_REGNUM);
1139 for (; cur_pc < pc; prev_pc = cur_pc, cur_pc += inst_len)
1141 inst_t *inst = NULL;
1143 inst = score3_adjust_pc_and_fetch_inst (&cur_pc, &inst_len, byte_order);
1145 /* FIXME: make a full-power prologue analyzer */
1148 if (G_FLD (inst->v, 14, 12) == 0x0
1149 && G_FLD (inst->v, 11, 7) == 0x0
1150 && G_FLD (inst->v, 6, 5) == 0x3)
1155 if (G_FLD (inst->v, 4, 0) == 0x3
1156 && ra_offset_p == 0)
1158 /* push! r3, [r0] */
1159 ra_offset = sp_offset;
1162 else if (G_FLD (inst->v, 4, 0) == 0x2
1163 && fp_offset_p == 0)
1165 /* push! r2, [r0] */
1166 fp_offset = sp_offset;
1170 else if (G_FLD (inst->v, 14, 12) == 0x6
1171 && G_FLD (inst->v, 11, 10) == 0x3)
1174 int start_r = G_FLD (inst->v, 9, 5);
1175 int cnt = G_FLD (inst->v, 4, 0);
1177 if ((ra_offset_p == 0)
1178 && (start_r <= SCORE_RA_REGNUM)
1179 && (SCORE_RA_REGNUM < start_r + cnt))
1181 /* rpush! contains r3 */
1183 ra_offset = sp_offset + 4 * (SCORE_RA_REGNUM - start_r) + 4;
1186 if ((fp_offset_p == 0)
1187 && (start_r <= SCORE_FP_REGNUM)
1188 && (SCORE_FP_REGNUM < start_r + cnt))
1190 /* rpush! contains r2 */
1192 fp_offset = sp_offset + 4 * (SCORE_FP_REGNUM - start_r) + 4;
1195 sp_offset += 4 * cnt;
1197 else if (G_FLD (inst->v, 14, 12) == 0x0
1198 && G_FLD (inst->v, 11, 7) == 0x0
1199 && G_FLD (inst->v, 6, 5) == 0x2)
1204 else if (G_FLD (inst->v, 14, 12) == 0x6
1205 && G_FLD (inst->v, 11, 10) == 0x2)
1208 sp_offset -= 4 * G_FLD (inst->v, 4, 0);
1210 else if (G_FLD (inst->v, 14, 12) == 0x5
1211 && G_FLD (inst->v, 11, 10) == 0x3
1212 && G_FLD (inst->v, 9, 6) == 0x0)
1214 /* addi! r0, -offset */
1215 int imm = G_FLD (inst->v, 5, 0);
1217 imm = -(0x3F - imm + 1);
1220 else if (G_FLD (inst->v, 14, 12) == 0x5
1221 && G_FLD (inst->v, 11, 10) == 0x3
1222 && G_FLD (inst->v, 9, 6) == 0x2)
1224 /* addi! r2, offset */
1225 if (pc - cur_pc >= 2)
1227 unsigned int save_v = inst->v;
1230 cur_pc += inst->len;
1231 inst2 = score3_adjust_pc_and_fetch_inst (&cur_pc, NULL, byte_order);
1234 && G_FLD (inst2->v, 14, 10) == 0x10
1235 && G_FLD (inst2->v, 9, 5) == 0x0
1236 && G_FLD (inst2->v, 4, 0) == 0x2)
1239 int imm = G_FLD (inst->v, 5, 0);
1241 imm = -(0x3F - imm + 1);
1247 else if (inst->len == 4)
1249 if (G_FLD (inst->v, 29, 25) == 0x3
1250 && G_FLD (inst->v, 2, 0) == 0x4
1251 && G_FLD (inst->v, 24, 20) == 0x3
1252 && G_FLD (inst->v, 19, 15) == 0x0)
1254 /* sw r3, [r0, offset]+ */
1255 sp_offset += inst->len;
1256 if (ra_offset_p == 0)
1258 ra_offset = sp_offset;
1262 else if (G_FLD (inst->v, 29, 25) == 0x3
1263 && G_FLD (inst->v, 2, 0) == 0x4
1264 && G_FLD (inst->v, 24, 20) == 0x2
1265 && G_FLD (inst->v, 19, 15) == 0x0)
1267 /* sw r2, [r0, offset]+ */
1268 sp_offset += inst->len;
1269 if (fp_offset_p == 0)
1271 fp_offset = sp_offset;
1275 else if (G_FLD (inst->v, 29, 25) == 0x7
1276 && G_FLD (inst->v, 2, 0) == 0x0
1277 && G_FLD (inst->v, 24, 20) == 0x3
1278 && G_FLD (inst->v, 19, 15) == 0x0)
1280 /* lw r3, [r0]+, 4 */
1281 sp_offset -= inst->len;
1284 else if (G_FLD (inst->v, 29, 25) == 0x7
1285 && G_FLD (inst->v, 2, 0) == 0x0
1286 && G_FLD (inst->v, 24, 20) == 0x2
1287 && G_FLD (inst->v, 19, 15) == 0x0)
1289 /* lw r2, [r0]+, 4 */
1290 sp_offset -= inst->len;
1293 else if (G_FLD (inst->v, 29, 25) == 0x1
1294 && G_FLD (inst->v, 19, 17) == 0x0
1295 && G_FLD (inst->v, 24, 20) == 0x0
1296 && G_FLD (inst->v, 0, 0) == 0x0)
1298 /* addi r0, -offset */
1299 int imm = G_FLD (inst->v, 16, 1);
1301 imm = -(0xFFFF - imm + 1);
1304 else if (G_FLD (inst->v, 29, 25) == 0x1
1305 && G_FLD (inst->v, 19, 17) == 0x0
1306 && G_FLD (inst->v, 24, 20) == 0x2
1307 && G_FLD (inst->v, 0, 0) == 0x0)
1309 /* addi r2, offset */
1310 if (pc - cur_pc >= 2)
1312 unsigned int save_v = inst->v;
1315 cur_pc += inst->len;
1316 inst2 = score3_adjust_pc_and_fetch_inst (&cur_pc, NULL, byte_order);
1319 && G_FLD (inst2->v, 14, 10) == 0x10
1320 && G_FLD (inst2->v, 9, 5) == 0x0
1321 && G_FLD (inst2->v, 4, 0) == 0x2)
1324 int imm = G_FLD (inst->v, 16, 1);
1326 imm = -(0xFFFF - imm + 1);
1335 if (ra_offset_p == 1)
1337 if (this_cache->saved_regs[SCORE_PC_REGNUM].addr == -1)
1338 this_cache->saved_regs[SCORE_PC_REGNUM].addr =
1339 sp + sp_offset - ra_offset;
1343 this_cache->saved_regs[SCORE_PC_REGNUM] =
1344 this_cache->saved_regs[SCORE_RA_REGNUM];
1348 if (fp_offset_p == 1)
1350 if (this_cache->saved_regs[SCORE_FP_REGNUM].addr == -1)
1351 this_cache->saved_regs[SCORE_FP_REGNUM].addr =
1352 sp + sp_offset - fp_offset;
1355 /* Save SP and FP. */
1356 this_cache->base = sp + sp_offset;
1357 this_cache->fp = fp;
1360 static struct score_frame_cache *
1361 score_make_prologue_cache (struct frame_info *this_frame, void **this_cache)
1363 struct score_frame_cache *cache;
1365 if ((*this_cache) != NULL)
1366 return (*this_cache);
1368 cache = FRAME_OBSTACK_ZALLOC (struct score_frame_cache);
1369 (*this_cache) = cache;
1370 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
1372 /* Analyze the prologue. */
1374 const CORE_ADDR pc = get_frame_pc (this_frame);
1375 CORE_ADDR start_addr;
1377 find_pc_partial_function (pc, NULL, &start_addr, NULL);
1378 if (start_addr == 0)
1381 if (target_mach == bfd_mach_score3)
1382 score3_analyze_prologue (start_addr, pc, this_frame, *this_cache);
1384 score7_analyze_prologue (start_addr, pc, this_frame, *this_cache);
1388 trad_frame_set_value (cache->saved_regs, SCORE_SP_REGNUM, cache->base);
1390 return (*this_cache);
1394 score_prologue_this_id (struct frame_info *this_frame, void **this_cache,
1395 struct frame_id *this_id)
1397 struct score_frame_cache *info = score_make_prologue_cache (this_frame,
1399 (*this_id) = frame_id_build (info->base, get_frame_func (this_frame));
1402 static struct value *
1403 score_prologue_prev_register (struct frame_info *this_frame,
1404 void **this_cache, int regnum)
1406 struct score_frame_cache *info = score_make_prologue_cache (this_frame,
1408 return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
1411 static const struct frame_unwind score_prologue_unwind =
1414 score_prologue_this_id,
1415 score_prologue_prev_register,
1417 default_frame_sniffer,
1422 score_prologue_frame_base_address (struct frame_info *this_frame,
1425 struct score_frame_cache *info =
1426 score_make_prologue_cache (this_frame, this_cache);
1430 static const struct frame_base score_prologue_frame_base =
1432 &score_prologue_unwind,
1433 score_prologue_frame_base_address,
1434 score_prologue_frame_base_address,
1435 score_prologue_frame_base_address,
1438 static const struct frame_base *
1439 score_prologue_frame_base_sniffer (struct frame_info *this_frame)
1441 return &score_prologue_frame_base;
1444 /* Core file support (dirty hack)
1446 The core file MUST be generated by GNU/Linux on S+core */
1449 score7_linux_supply_gregset(const struct regset *regset,
1450 struct regcache *regcache,
1451 int regnum, const void *gregs_buf, size_t len)
1454 elf_gregset_t *gregs;
1456 gdb_assert (regset != NULL);
1457 gdb_assert ((regcache != NULL) && (gregs_buf != NULL));
1459 gregs = (elf_gregset_t *) gregs_buf;
1461 for (regno = 0; regno < 32; regno++)
1462 if (regnum == -1 || regnum == regno)
1463 regcache_raw_supply (regcache, regno, gregs->regs + regno);
1470 { 55, &(gregs->cel) }, /* CEL */
1471 { 54, &(gregs->ceh) }, /* CEH */
1472 { 53, &(gregs->sr0) }, /* sr0, i.e. cnt or COUNTER */
1473 { 52, &(gregs->sr1) }, /* sr1, i.e. lcr or LDCR */
1474 { 51, &(gregs->sr1) }, /* sr2, i.e. scr or STCR */
1476 /* Exception occured at this address, exactly the PC we want */
1477 { 49, &(gregs->cp0_epc) }, /* PC */
1479 { 38, &(gregs->cp0_ema) }, /* EMA */
1480 { 37, &(gregs->cp0_epc) }, /* EPC */
1481 { 34, &(gregs->cp0_ecr) }, /* ECR */
1482 { 33, &(gregs->cp0_condition) }, /* COND */
1483 { 32, &(gregs->cp0_psr) }, /* PSR */
1486 for (regno = 0; regno < sizeof(sregs)/sizeof(sregs[0]); regno++)
1487 if (regnum == -1 || regnum == sregs[regno].regnum)
1488 regcache_raw_supply (regcache, sregs[regno].regnum, sregs[regno].buf);
1492 /* Return the appropriate register set from the core section identified
1493 by SECT_NAME and SECT_SIZE. */
1495 static const struct regset *
1496 score7_linux_regset_from_core_section(struct gdbarch *gdbarch,
1497 const char *sect_name, size_t sect_size)
1499 struct gdbarch_tdep *tdep;
1501 gdb_assert (gdbarch != NULL);
1502 gdb_assert (sect_name != NULL);
1504 tdep = gdbarch_tdep (gdbarch);
1506 if (strcmp(sect_name, ".reg") == 0 && sect_size == sizeof(elf_gregset_t))
1508 if (tdep->gregset == NULL)
1509 tdep->gregset = regset_alloc (gdbarch, score7_linux_supply_gregset, NULL);
1510 return tdep->gregset;
1516 static struct gdbarch *
1517 score_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1519 struct gdbarch *gdbarch;
1520 struct gdbarch_tdep *tdep;
1521 target_mach = info.bfd_arch_info->mach;
1523 arches = gdbarch_list_lookup_by_info (arches, &info);
1526 return (arches->gdbarch);
1528 tdep = xcalloc(1, sizeof(struct gdbarch_tdep));
1529 gdbarch = gdbarch_alloc (&info, tdep);
1531 set_gdbarch_short_bit (gdbarch, 16);
1532 set_gdbarch_int_bit (gdbarch, 32);
1533 set_gdbarch_float_bit (gdbarch, 32);
1534 set_gdbarch_double_bit (gdbarch, 64);
1535 set_gdbarch_long_double_bit (gdbarch, 64);
1537 set_gdbarch_register_sim_regno (gdbarch, score_register_sim_regno);
1539 set_gdbarch_pc_regnum (gdbarch, SCORE_PC_REGNUM);
1540 set_gdbarch_sp_regnum (gdbarch, SCORE_SP_REGNUM);
1541 set_gdbarch_adjust_breakpoint_address (gdbarch, score_adjust_breakpoint_address);
1542 set_gdbarch_register_type (gdbarch, score_register_type);
1543 set_gdbarch_frame_align (gdbarch, score_frame_align);
1544 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1545 set_gdbarch_unwind_sp (gdbarch, score_unwind_sp);
1546 set_gdbarch_unwind_pc (gdbarch, score_unwind_pc);
1547 set_gdbarch_print_insn (gdbarch, score_print_insn);
1549 switch (target_mach)
1551 case bfd_mach_score7:
1552 set_gdbarch_breakpoint_from_pc (gdbarch, score7_breakpoint_from_pc);
1553 set_gdbarch_skip_prologue (gdbarch, score7_skip_prologue);
1554 set_gdbarch_in_function_epilogue_p (gdbarch, score7_in_function_epilogue_p);
1555 set_gdbarch_register_name (gdbarch, score7_register_name);
1556 set_gdbarch_num_regs (gdbarch, SCORE7_NUM_REGS);
1557 /* Core file support. */
1558 set_gdbarch_regset_from_core_section (gdbarch, score7_linux_regset_from_core_section);
1561 case bfd_mach_score3:
1562 set_gdbarch_breakpoint_from_pc (gdbarch, score3_breakpoint_from_pc);
1563 set_gdbarch_skip_prologue (gdbarch, score3_skip_prologue);
1564 set_gdbarch_in_function_epilogue_p (gdbarch, score3_in_function_epilogue_p);
1565 set_gdbarch_register_name (gdbarch, score3_register_name);
1566 set_gdbarch_num_regs (gdbarch, SCORE3_NUM_REGS);
1570 /* Watchpoint hooks. */
1571 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
1573 /* Dummy frame hooks. */
1574 set_gdbarch_return_value (gdbarch, score_return_value);
1575 set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
1576 set_gdbarch_dummy_id (gdbarch, score_dummy_id);
1577 set_gdbarch_push_dummy_call (gdbarch, score_push_dummy_call);
1579 /* Normal frame hooks. */
1580 dwarf2_append_unwinders (gdbarch);
1581 frame_base_append_sniffer (gdbarch, dwarf2_frame_base_sniffer);
1582 frame_unwind_append_unwinder (gdbarch, &score_prologue_unwind);
1583 frame_base_append_sniffer (gdbarch, score_prologue_frame_base_sniffer);
1588 extern initialize_file_ftype _initialize_score_tdep;
1591 _initialize_score_tdep (void)
1593 gdbarch_register (bfd_arch_score, score_gdbarch_init, NULL);