1 /* Target-dependent code for the S+core architecture, for GDB,
4 Copyright (C) 2006-2016 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/>. */
30 #include "arch-utils.h"
34 #include "frame-unwind.h"
35 #include "frame-base.h"
36 #include "trad-frame.h"
37 #include "dwarf2-frame.h"
38 #include "score-tdep.h"
40 #define G_FLD(_i,_ms,_ls) \
41 ((unsigned)((_i) << (31 - (_ms))) >> (31 - (_ms) + (_ls)))
45 unsigned long long raw;
49 struct score_frame_cache
53 struct trad_frame_saved_reg *saved_regs;
56 static int target_mach = bfd_mach_score7;
59 score_register_type (struct gdbarch *gdbarch, int regnum)
61 gdb_assert (regnum >= 0
62 && regnum < ((target_mach == bfd_mach_score7)
63 ? SCORE7_NUM_REGS : SCORE3_NUM_REGS));
64 return builtin_type (gdbarch)->builtin_uint32;
68 score_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
70 return frame_unwind_register_unsigned (next_frame, SCORE_SP_REGNUM);
74 score_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
76 return frame_unwind_register_unsigned (next_frame, SCORE_PC_REGNUM);
80 score7_register_name (struct gdbarch *gdbarch, int regnum)
82 const char *score_register_names[] = {
83 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
84 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
85 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
86 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
88 "PSR", "COND", "ECR", "EXCPVEC", "CCR",
89 "EPC", "EMA", "TLBLOCK", "TLBPT", "PEADDR",
90 "TLBRPT", "PEVN", "PECTX", "LIMPFN", "LDMPFN",
91 "PREV", "DREG", "PC", "DSAVE", "COUNTER",
92 "LDCR", "STCR", "CEH", "CEL",
95 gdb_assert (regnum >= 0 && regnum < SCORE7_NUM_REGS);
96 return score_register_names[regnum];
100 score3_register_name (struct gdbarch *gdbarch, int regnum)
102 const char *score_register_names[] = {
103 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
104 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
105 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
106 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
108 "PSR", "COND", "ECR", "EXCPVEC", "CCR",
109 "EPC", "EMA", "PREV", "DREG", "DSAVE",
110 "COUNTER", "LDCR", "STCR", "CEH", "CEL",
114 gdb_assert (regnum >= 0 && regnum < SCORE3_NUM_REGS);
115 return score_register_names[regnum];
120 score_register_sim_regno (struct gdbarch *gdbarch, int regnum)
122 gdb_assert (regnum >= 0
123 && regnum < ((target_mach == bfd_mach_score7)
124 ? SCORE7_NUM_REGS : SCORE3_NUM_REGS));
130 score_print_insn (bfd_vma memaddr, struct disassemble_info *info)
132 if (info->endian == BFD_ENDIAN_BIG)
133 return print_insn_big_score (memaddr, info);
135 return print_insn_little_score (memaddr, info);
139 score7_fetch_inst (struct gdbarch *gdbarch, CORE_ADDR addr, gdb_byte *memblock)
141 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
142 static inst_t inst = { 0, 0, 0 };
143 gdb_byte buf[SCORE_INSTLEN] = { 0 };
147 if (target_has_execution && memblock != NULL)
149 /* Fetch instruction from local MEMBLOCK. */
150 memcpy (buf, memblock, SCORE_INSTLEN);
154 /* Fetch instruction from target. */
155 ret = target_read_memory (addr & ~0x3, buf, SCORE_INSTLEN);
158 error (_("Error: target_read_memory in file:%s, line:%d!"),
164 inst.raw = extract_unsigned_integer (buf, SCORE_INSTLEN, byte_order);
165 inst.len = (inst.raw & 0x80008000) ? 4 : 2;
166 inst.v = ((inst.raw >> 16 & 0x7FFF) << 15) | (inst.raw & 0x7FFF);
167 big = (byte_order == BFD_ENDIAN_BIG);
170 if (big ^ ((addr & 0x2) == 2))
171 inst.v = G_FLD (inst.v, 29, 15);
173 inst.v = G_FLD (inst.v, 14, 0);
179 score3_adjust_pc_and_fetch_inst (CORE_ADDR *pcptr, int *lenptr,
180 enum bfd_endian byte_order)
182 static inst_t inst = { 0, 0, 0 };
189 /* raw table 1 (column 2, 3, 4)
193 table 2 (column 1, 2, 3)
199 static const struct breakplace bk_table[16] =
221 #define EXTRACT_LEN 2
222 CORE_ADDR adjust_pc = *pcptr & ~0x1;
223 gdb_byte buf[5][EXTRACT_LEN] =
233 unsigned int cbits = 0;
242 for (i = 0; i < 5; i++)
244 ret = target_read_memory (adjust_pc + 2 * i, buf[i], EXTRACT_LEN);
250 error (_("Error: target_read_memory in file:%s, line:%d!"),
254 raw = extract_unsigned_integer (buf[i], EXTRACT_LEN, byte_order);
255 cbits = (cbits << 1) | (raw >> 15);
262 cbits = (cbits >> 1) & 0x7;
268 cbits = (cbits >> 2) & 0x7;
269 bk_index = cbits + 8;
272 gdb_assert (!((bk_table[bk_index].break_offset == 0)
273 && (bk_table[bk_index].inst_len == 0)));
275 inst.len = bk_table[bk_index].inst_len;
277 i = (bk_table[bk_index].break_offset + 4) / 2;
278 count = inst.len / 2;
279 for (; count > 0; i++, count--)
281 inst.raw = (inst.raw << 16)
282 | extract_unsigned_integer (buf[i], EXTRACT_LEN, byte_order);
288 inst.v = inst.raw & 0x7FFF;
291 inst.v = ((inst.raw >> 16 & 0x7FFF) << 15) | (inst.raw & 0x7FFF);
294 inst.v = ((inst.raw >> 32 & 0x7FFF) << 30)
295 | ((inst.raw >> 16 & 0x7FFF) << 15) | (inst.raw & 0x7FFF);
300 *pcptr = adjust_pc + bk_table[bk_index].break_offset;
302 *lenptr = bk_table[bk_index].inst_len;
309 static const gdb_byte *
310 score7_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
313 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
314 gdb_byte buf[SCORE_INSTLEN] = { 0 };
318 if ((ret = target_read_memory (*pcptr & ~0x3, buf, SCORE_INSTLEN)) != 0)
320 error (_("Error: target_read_memory in file:%s, line:%d!"),
323 raw = extract_unsigned_integer (buf, SCORE_INSTLEN, byte_order);
325 if (byte_order == BFD_ENDIAN_BIG)
327 if (!(raw & 0x80008000))
329 /* 16bits instruction. */
330 static gdb_byte big_breakpoint16[] = { 0x60, 0x02 };
332 *lenptr = sizeof (big_breakpoint16);
333 return big_breakpoint16;
337 /* 32bits instruction. */
338 static gdb_byte big_breakpoint32[] = { 0x80, 0x00, 0x80, 0x06 };
340 *lenptr = sizeof (big_breakpoint32);
341 return big_breakpoint32;
346 if (!(raw & 0x80008000))
348 /* 16bits instruction. */
349 static gdb_byte little_breakpoint16[] = { 0x02, 0x60 };
351 *lenptr = sizeof (little_breakpoint16);
352 return little_breakpoint16;
356 /* 32bits instruction. */
357 static gdb_byte little_breakpoint32[] = { 0x06, 0x80, 0x00, 0x80 };
359 *lenptr = sizeof (little_breakpoint32);
360 return little_breakpoint32;
365 static const gdb_byte *
366 score3_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
369 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
370 CORE_ADDR adjust_pc = *pcptr;
372 static gdb_byte score_break_insns[6][6] = {
373 /* The following three instructions are big endian. */
375 { 0x80, 0x00, 0x00, 0x06 },
376 { 0x80, 0x00, 0x80, 0x00, 0x00, 0x00 },
377 /* The following three instructions are little endian. */
379 { 0x00, 0x80, 0x06, 0x00 },
380 { 0x00, 0x80, 0x00, 0x80, 0x00, 0x00 }};
385 score3_adjust_pc_and_fetch_inst (&adjust_pc, &len, byte_order);
387 index = ((byte_order == BFD_ENDIAN_BIG) ? 0 : 3) + (len / 2 - 1);
388 p = score_break_insns[index];
397 score_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
399 CORE_ADDR adjust_pc = bpaddr;
401 if (target_mach == bfd_mach_score3)
402 score3_adjust_pc_and_fetch_inst (&adjust_pc, NULL,
403 gdbarch_byte_order (gdbarch));
405 adjust_pc = align_down (adjust_pc, 2);
411 score_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
413 return align_down (addr, 16);
417 score_xfer_register (struct regcache *regcache, int regnum, int length,
418 enum bfd_endian endian, gdb_byte *readbuf,
419 const gdb_byte *writebuf, int buf_offset)
422 gdb_assert (regnum >= 0
423 && regnum < ((target_mach == bfd_mach_score7)
424 ? SCORE7_NUM_REGS : SCORE3_NUM_REGS));
429 reg_offset = SCORE_REGSIZE - length;
431 case BFD_ENDIAN_LITTLE:
434 case BFD_ENDIAN_UNKNOWN:
438 error (_("Error: score_xfer_register in file:%s, line:%d!"),
443 regcache_cooked_read_part (regcache, regnum, reg_offset, length,
444 readbuf + buf_offset);
445 if (writebuf != NULL)
446 regcache_cooked_write_part (regcache, regnum, reg_offset, length,
447 writebuf + buf_offset);
450 static enum return_value_convention
451 score_return_value (struct gdbarch *gdbarch, struct value *function,
452 struct type *type, struct regcache *regcache,
453 gdb_byte * readbuf, const gdb_byte * writebuf)
455 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
456 || TYPE_CODE (type) == TYPE_CODE_UNION
457 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
458 return RETURN_VALUE_STRUCT_CONVENTION;
463 for (offset = 0, regnum = SCORE_A0_REGNUM;
464 offset < TYPE_LENGTH (type);
465 offset += SCORE_REGSIZE, regnum++)
467 int xfer = SCORE_REGSIZE;
469 if (offset + xfer > TYPE_LENGTH (type))
470 xfer = TYPE_LENGTH (type) - offset;
471 score_xfer_register (regcache, regnum, xfer,
472 gdbarch_byte_order(gdbarch),
473 readbuf, writebuf, offset);
475 return RETURN_VALUE_REGISTER_CONVENTION;
479 static struct frame_id
480 score_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
482 return frame_id_build (get_frame_register_unsigned (this_frame,
484 get_frame_pc (this_frame));
488 score_type_needs_double_align (struct type *type)
490 enum type_code typecode = TYPE_CODE (type);
492 if ((typecode == TYPE_CODE_INT && TYPE_LENGTH (type) == 8)
493 || (typecode == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8))
495 else if (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION)
499 n = TYPE_NFIELDS (type);
500 for (i = 0; i < n; i++)
501 if (score_type_needs_double_align (TYPE_FIELD_TYPE (type, i)))
509 score_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
510 struct regcache *regcache, CORE_ADDR bp_addr,
511 int nargs, struct value **args, CORE_ADDR sp,
512 int struct_return, CORE_ADDR struct_addr)
514 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
518 CORE_ADDR stack_offset = 0;
521 /* Step 1, Save RA. */
522 regcache_cooked_write_unsigned (regcache, SCORE_RA_REGNUM, bp_addr);
524 /* Step 2, Make space on the stack for the args. */
525 struct_addr = align_down (struct_addr, 16);
526 sp = align_down (sp, 16);
527 for (argnum = 0; argnum < nargs; argnum++)
528 arglen += align_up (TYPE_LENGTH (value_type (args[argnum])),
530 sp -= align_up (arglen, 16);
532 argreg = SCORE_BEGIN_ARG_REGNUM;
534 /* Step 3, Check if struct return then save the struct address to
535 r4 and increase the stack_offset by 4. */
538 regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
539 stack_offset += SCORE_REGSIZE;
542 /* Step 4, Load arguments:
543 If arg length is too long (> 4 bytes), then split the arg and
545 for (argnum = 0; argnum < nargs; argnum++)
547 struct value *arg = args[argnum];
548 struct type *arg_type = check_typedef (value_type (arg));
549 enum type_code typecode = TYPE_CODE (arg_type);
550 const gdb_byte *val = value_contents (arg);
551 int downward_offset = 0;
552 int arg_last_part_p = 0;
554 arglen = TYPE_LENGTH (arg_type);
556 /* If a arg should be aligned to 8 bytes (long long or double),
557 the value should be put to even register numbers. */
558 if (score_type_needs_double_align (arg_type))
564 /* If sizeof a block < SCORE_REGSIZE, then Score GCC will chose
565 the default "downward"/"upward" method:
571 char a; char b; char c;
572 } s = {'a', 'b', 'c'};
574 Big endian: s = {X, 'a', 'b', 'c'}
575 Little endian: s = {'a', 'b', 'c', X}
577 Where X is a hole. */
579 if (gdbarch_byte_order(gdbarch) == BFD_ENDIAN_BIG
580 && (typecode == TYPE_CODE_STRUCT
581 || typecode == TYPE_CODE_UNION)
582 && argreg > SCORE_LAST_ARG_REGNUM
583 && arglen < SCORE_REGSIZE)
584 downward_offset += (SCORE_REGSIZE - arglen);
588 int partial_len = arglen < SCORE_REGSIZE ? arglen : SCORE_REGSIZE;
589 ULONGEST regval = extract_unsigned_integer (val, partial_len,
592 /* The last part of a arg should shift left when
593 gdbarch_byte_order is BFD_ENDIAN_BIG. */
594 if (byte_order == BFD_ENDIAN_BIG
595 && arg_last_part_p == 1
596 && (typecode == TYPE_CODE_STRUCT
597 || typecode == TYPE_CODE_UNION))
598 regval <<= ((SCORE_REGSIZE - partial_len) * TARGET_CHAR_BIT);
600 /* Always increase the stack_offset and save args to stack. */
601 addr = sp + stack_offset + downward_offset;
602 write_memory (addr, val, partial_len);
604 if (argreg <= SCORE_LAST_ARG_REGNUM)
606 regcache_cooked_write_unsigned (regcache, argreg++, regval);
607 if (arglen > SCORE_REGSIZE && arglen < SCORE_REGSIZE * 2)
612 arglen -= partial_len;
613 stack_offset += align_up (partial_len, SCORE_REGSIZE);
617 /* Step 5, Save SP. */
618 regcache_cooked_write_unsigned (regcache, SCORE_SP_REGNUM, sp);
624 score7_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
627 int iscan = 32, stack_sub = 0;
630 inst_t *inst = score7_fetch_inst (gdbarch, cpc, NULL);
633 if ((inst->len == 4) && !stack_sub
634 && (G_FLD (inst->v, 29, 25) == 0x1
635 && G_FLD (inst->v, 24, 20) == 0x0))
637 /* addi r0, offset */
638 stack_sub = cpc + SCORE_INSTLEN;
639 pc = cpc + SCORE_INSTLEN;
641 else if ((inst->len == 4)
642 && (G_FLD (inst->v, 29, 25) == 0x0)
643 && (G_FLD (inst->v, 24, 20) == 0x2)
644 && (G_FLD (inst->v, 19, 15) == 0x0)
645 && (G_FLD (inst->v, 14, 10) == 0xF)
646 && (G_FLD (inst->v, 9, 0) == 0x56))
649 pc = cpc + SCORE_INSTLEN;
652 else if ((inst->len == 2)
653 && (G_FLD (inst->v, 14, 12) == 0x0)
654 && (G_FLD (inst->v, 11, 8) == 0x2)
655 && (G_FLD (inst->v, 7, 4) == 0x0)
656 && (G_FLD (inst->v, 3, 0) == 0x3))
659 pc = cpc + SCORE16_INSTLEN;
662 else if ((inst->len == 2)
663 && ((G_FLD (inst->v, 14, 12) == 3) /* j15 form */
664 || (G_FLD (inst->v, 14, 12) == 4) /* b15 form */
665 || (G_FLD (inst->v, 14, 12) == 0x0
666 && G_FLD (inst->v, 3, 0) == 0x4))) /* br! */
668 else if ((inst->len == 4)
669 && ((G_FLD (inst->v, 29, 25) == 2) /* j32 form */
670 || (G_FLD (inst->v, 29, 25) == 4) /* b32 form */
671 || (G_FLD (inst->v, 29, 25) == 0x0
672 && G_FLD (inst->v, 6, 1) == 0x4))) /* br */
675 cpc += (inst->len == 2) ? SCORE16_INSTLEN : SCORE_INSTLEN;
681 score3_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
684 int iscan = 32, stack_sub = 0;
688 = score3_adjust_pc_and_fetch_inst (&cpc, NULL,
689 gdbarch_byte_order (gdbarch));
693 if (inst->len == 4 && !stack_sub
694 && (G_FLD (inst->v, 29, 25) == 0x1)
695 && (G_FLD (inst->v, 19, 17) == 0x0)
696 && (G_FLD (inst->v, 24, 20) == 0x0))
698 /* addi r0, offset */
699 stack_sub = cpc + inst->len;
700 pc = cpc + inst->len;
702 else if (inst->len == 4
703 && (G_FLD (inst->v, 29, 25) == 0x0)
704 && (G_FLD (inst->v, 24, 20) == 0x2)
705 && (G_FLD (inst->v, 19, 15) == 0x0)
706 && (G_FLD (inst->v, 14, 10) == 0xF)
707 && (G_FLD (inst->v, 9, 0) == 0x56))
710 pc = cpc + inst->len;
713 else if ((inst->len == 2)
714 && (G_FLD (inst->v, 14, 10) == 0x10)
715 && (G_FLD (inst->v, 9, 5) == 0x2)
716 && (G_FLD (inst->v, 4, 0) == 0x0))
719 pc = cpc + inst->len;
722 else if (inst->len == 2
723 && ((G_FLD (inst->v, 14, 12) == 3) /* b15 form */
724 || (G_FLD (inst->v, 14, 12) == 0x0
725 && G_FLD (inst->v, 11, 5) == 0x4))) /* br! */
727 else if (inst->len == 4
728 && ((G_FLD (inst->v, 29, 25) == 2) /* j32 form */
729 || (G_FLD (inst->v, 29, 25) == 4))) /* b32 form */
737 /* Implement the stack_frame_destroyed_p gdbarch method. */
740 score7_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR cur_pc)
742 inst_t *inst = score7_fetch_inst (gdbarch, cur_pc, NULL);
745 return 1; /* mv! r0, r2 */
746 else if (G_FLD (inst->v, 14, 12) == 0x2
747 && G_FLD (inst->v, 3, 0) == 0xa)
749 else if (G_FLD (inst->v, 14, 12) == 0x0
750 && G_FLD (inst->v, 7, 0) == 0x34)
751 return 1; /* br! r3 */
752 else if (G_FLD (inst->v, 29, 15) == 0x2
753 && G_FLD (inst->v, 6, 1) == 0x2b)
754 return 1; /* mv r0, r2 */
755 else if (G_FLD (inst->v, 29, 25) == 0x0
756 && G_FLD (inst->v, 6, 1) == 0x4
757 && G_FLD (inst->v, 19, 15) == 0x3)
758 return 1; /* br r3 */
763 /* Implement the stack_frame_destroyed_p gdbarch method. */
766 score3_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR cur_pc)
768 CORE_ADDR pc = cur_pc;
770 = score3_adjust_pc_and_fetch_inst (&pc, NULL,
771 gdbarch_byte_order (gdbarch));
774 && (G_FLD (inst->v, 14, 10) == 0x10)
775 && (G_FLD (inst->v, 9, 5) == 0x0)
776 && (G_FLD (inst->v, 4, 0) == 0x2))
777 return 1; /* mv! r0, r2 */
778 else if (inst->len == 4
779 && (G_FLD (inst->v, 29, 25) == 0x0)
780 && (G_FLD (inst->v, 24, 20) == 0x2)
781 && (G_FLD (inst->v, 19, 15) == 0x0)
782 && (G_FLD (inst->v, 14, 10) == 0xF)
783 && (G_FLD (inst->v, 9, 0) == 0x56))
784 return 1; /* mv r0, r2 */
785 else if (inst->len == 2
786 && (G_FLD (inst->v, 14, 12) == 0x0)
787 && (G_FLD (inst->v, 11, 5) == 0x2))
789 else if (inst->len == 2
790 && (G_FLD (inst->v, 14, 12) == 0x0)
791 && (G_FLD (inst->v, 11, 7) == 0x0)
792 && (G_FLD (inst->v, 6, 5) == 0x2))
793 return 1; /* rpop! */
794 else if (inst->len == 2
795 && (G_FLD (inst->v, 14, 12) == 0x0)
796 && (G_FLD (inst->v, 11, 5) == 0x4)
797 && (G_FLD (inst->v, 4, 0) == 0x3))
798 return 1; /* br! r3 */
799 else if (inst->len == 4
800 && (G_FLD (inst->v, 29, 25) == 0x0)
801 && (G_FLD (inst->v, 24, 20) == 0x0)
802 && (G_FLD (inst->v, 19, 15) == 0x3)
803 && (G_FLD (inst->v, 14, 10) == 0xF)
804 && (G_FLD (inst->v, 9, 0) == 0x8))
805 return 1; /* br r3 */
811 score7_malloc_and_get_memblock (CORE_ADDR addr, CORE_ADDR size)
814 gdb_byte *memblock = NULL;
818 error (_("Error: malloc size < 0 in file:%s, line:%d!"),
825 memblock = (gdb_byte *) xmalloc (size);
826 memset (memblock, 0, size);
827 ret = target_read_memory (addr & ~0x3, memblock, size);
830 error (_("Error: target_read_memory in file:%s, line:%d!"),
838 score7_free_memblock (gdb_byte *memblock)
844 score7_adjust_memblock_ptr (gdb_byte **memblock, CORE_ADDR prev_pc,
849 /* First time call this function, do nothing. */
851 else if (cur_pc - prev_pc == 2 && (cur_pc & 0x3) == 0)
853 /* First 16-bit instruction, then 32-bit instruction. */
854 *memblock += SCORE_INSTLEN;
856 else if (cur_pc - prev_pc == 4)
858 /* Is 32-bit instruction, increase MEMBLOCK by 4. */
859 *memblock += SCORE_INSTLEN;
864 score7_analyze_prologue (CORE_ADDR startaddr, CORE_ADDR pc,
865 struct frame_info *this_frame,
866 struct score_frame_cache *this_cache)
868 struct gdbarch *gdbarch = get_frame_arch (this_frame);
871 CORE_ADDR cur_pc = startaddr;
880 gdb_byte *memblock = NULL;
881 gdb_byte *memblock_ptr = NULL;
882 CORE_ADDR prev_pc = -1;
884 /* Allocate MEMBLOCK if PC - STARTADDR > 0. */
885 memblock_ptr = memblock =
886 score7_malloc_and_get_memblock (startaddr, pc - startaddr);
888 sp = get_frame_register_unsigned (this_frame, SCORE_SP_REGNUM);
889 fp = get_frame_register_unsigned (this_frame, SCORE_FP_REGNUM);
891 for (; cur_pc < pc; prev_pc = cur_pc, cur_pc += inst_len)
894 if (memblock != NULL)
896 /* Reading memory block from target succefully and got all
897 the instructions(from STARTADDR to PC) needed. */
898 score7_adjust_memblock_ptr (&memblock, prev_pc, cur_pc);
899 inst = score7_fetch_inst (gdbarch, cur_pc, memblock);
903 /* Otherwise, we fetch 4 bytes from target, and GDB also
905 inst = score7_fetch_inst (gdbarch, cur_pc, NULL);
908 /* FIXME: make a full-power prologue analyzer. */
911 inst_len = SCORE16_INSTLEN;
913 if (G_FLD (inst->v, 14, 12) == 0x2
914 && G_FLD (inst->v, 3, 0) == 0xe)
919 if (G_FLD (inst->v, 11, 7) == 0x6
923 ra_offset = sp_offset;
926 else if (G_FLD (inst->v, 11, 7) == 0x4
930 fp_offset = sp_offset;
934 else if (G_FLD (inst->v, 14, 12) == 0x2
935 && G_FLD (inst->v, 3, 0) == 0xa)
940 else if (G_FLD (inst->v, 14, 7) == 0xc1
941 && G_FLD (inst->v, 2, 0) == 0x0)
944 sp_offset += (int) pow (2, G_FLD (inst->v, 6, 3));
946 else if (G_FLD (inst->v, 14, 7) == 0xc0
947 && G_FLD (inst->v, 2, 0) == 0x0)
950 sp_offset -= (int) pow (2, G_FLD (inst->v, 6, 3));
955 inst_len = SCORE_INSTLEN;
957 if (G_FLD(inst->v, 29, 25) == 0x3
958 && G_FLD(inst->v, 2, 0) == 0x4
959 && G_FLD(inst->v, 19, 15) == 0)
961 /* sw rD, [r0, offset]+ */
962 sp_offset += SCORE_INSTLEN;
964 if (G_FLD(inst->v, 24, 20) == 0x3)
967 if (ra_offset_p == 0)
969 ra_offset = sp_offset;
973 else if (G_FLD(inst->v, 24, 20) == 0x2)
976 if (fp_offset_p == 0)
978 fp_offset = sp_offset;
983 else if (G_FLD(inst->v, 29, 25) == 0x14
984 && G_FLD(inst->v, 19,15) == 0)
986 /* sw rD, [r0, offset] */
987 if (G_FLD(inst->v, 24, 20) == 0x3)
990 ra_offset = sp_offset - G_FLD(inst->v, 14, 0);
993 else if (G_FLD(inst->v, 24, 20) == 0x2)
996 fp_offset = sp_offset - G_FLD(inst->v, 14, 0);
1000 else if (G_FLD (inst->v, 29, 15) == 0x1c60
1001 && G_FLD (inst->v, 2, 0) == 0x0)
1003 /* lw r3, [r0]+, 4 */
1004 sp_offset -= SCORE_INSTLEN;
1007 else if (G_FLD (inst->v, 29, 15) == 0x1c40
1008 && G_FLD (inst->v, 2, 0) == 0x0)
1010 /* lw r2, [r0]+, 4 */
1011 sp_offset -= SCORE_INSTLEN;
1015 else if (G_FLD (inst->v, 29, 17) == 0x100
1016 && G_FLD (inst->v, 0, 0) == 0x0)
1018 /* addi r0, -offset */
1019 sp_offset += 65536 - G_FLD (inst->v, 16, 1);
1021 else if (G_FLD (inst->v, 29, 17) == 0x110
1022 && G_FLD (inst->v, 0, 0) == 0x0)
1024 /* addi r2, offset */
1025 if (pc - cur_pc > 4)
1027 unsigned int save_v = inst->v;
1029 score7_fetch_inst (gdbarch, cur_pc + SCORE_INSTLEN, NULL);
1030 if (inst2->v == 0x23)
1033 sp_offset -= G_FLD (save_v, 16, 1);
1041 if (ra_offset_p == 1)
1043 if (this_cache->saved_regs[SCORE_PC_REGNUM].addr == -1)
1044 this_cache->saved_regs[SCORE_PC_REGNUM].addr =
1045 sp + sp_offset - ra_offset;
1049 this_cache->saved_regs[SCORE_PC_REGNUM] =
1050 this_cache->saved_regs[SCORE_RA_REGNUM];
1054 if (fp_offset_p == 1)
1056 if (this_cache->saved_regs[SCORE_FP_REGNUM].addr == -1)
1057 this_cache->saved_regs[SCORE_FP_REGNUM].addr =
1058 sp + sp_offset - fp_offset;
1061 /* Save SP and FP. */
1062 this_cache->base = sp + sp_offset;
1063 this_cache->fp = fp;
1065 /* Don't forget to free MEMBLOCK if we allocated it. */
1066 if (memblock_ptr != NULL)
1067 score7_free_memblock (memblock_ptr);
1071 score3_analyze_prologue (CORE_ADDR startaddr, CORE_ADDR pc,
1072 struct frame_info *this_frame,
1073 struct score_frame_cache *this_cache)
1077 CORE_ADDR cur_pc = startaddr;
1078 enum bfd_endian byte_order
1079 = gdbarch_byte_order (get_frame_arch (this_frame));
1084 int ra_offset_p = 0;
1085 int fp_offset_p = 0;
1088 sp = get_frame_register_unsigned (this_frame, SCORE_SP_REGNUM);
1089 fp = get_frame_register_unsigned (this_frame, SCORE_FP_REGNUM);
1091 for (; cur_pc < pc; cur_pc += inst_len)
1093 inst_t *inst = NULL;
1095 inst = score3_adjust_pc_and_fetch_inst (&cur_pc, &inst_len, byte_order);
1097 /* FIXME: make a full-power prologue analyzer. */
1100 if (G_FLD (inst->v, 14, 12) == 0x0
1101 && G_FLD (inst->v, 11, 7) == 0x0
1102 && G_FLD (inst->v, 6, 5) == 0x3)
1107 if (G_FLD (inst->v, 4, 0) == 0x3
1108 && ra_offset_p == 0)
1110 /* push! r3, [r0] */
1111 ra_offset = sp_offset;
1114 else if (G_FLD (inst->v, 4, 0) == 0x2
1115 && fp_offset_p == 0)
1117 /* push! r2, [r0] */
1118 fp_offset = sp_offset;
1122 else if (G_FLD (inst->v, 14, 12) == 0x6
1123 && G_FLD (inst->v, 11, 10) == 0x3)
1126 int start_r = G_FLD (inst->v, 9, 5);
1127 int cnt = G_FLD (inst->v, 4, 0);
1129 if ((ra_offset_p == 0)
1130 && (start_r <= SCORE_RA_REGNUM)
1131 && (SCORE_RA_REGNUM < start_r + cnt))
1133 /* rpush! contains r3 */
1135 ra_offset = sp_offset + 4 * (SCORE_RA_REGNUM - start_r) + 4;
1138 if ((fp_offset_p == 0)
1139 && (start_r <= SCORE_FP_REGNUM)
1140 && (SCORE_FP_REGNUM < start_r + cnt))
1142 /* rpush! contains r2 */
1144 fp_offset = sp_offset + 4 * (SCORE_FP_REGNUM - start_r) + 4;
1147 sp_offset += 4 * cnt;
1149 else if (G_FLD (inst->v, 14, 12) == 0x0
1150 && G_FLD (inst->v, 11, 7) == 0x0
1151 && G_FLD (inst->v, 6, 5) == 0x2)
1156 else if (G_FLD (inst->v, 14, 12) == 0x6
1157 && G_FLD (inst->v, 11, 10) == 0x2)
1160 sp_offset -= 4 * G_FLD (inst->v, 4, 0);
1162 else if (G_FLD (inst->v, 14, 12) == 0x5
1163 && G_FLD (inst->v, 11, 10) == 0x3
1164 && G_FLD (inst->v, 9, 6) == 0x0)
1166 /* addi! r0, -offset */
1167 int imm = G_FLD (inst->v, 5, 0);
1169 imm = -(0x3F - imm + 1);
1172 else if (G_FLD (inst->v, 14, 12) == 0x5
1173 && G_FLD (inst->v, 11, 10) == 0x3
1174 && G_FLD (inst->v, 9, 6) == 0x2)
1176 /* addi! r2, offset */
1177 if (pc - cur_pc >= 2)
1181 cur_pc += inst->len;
1182 inst2 = score3_adjust_pc_and_fetch_inst (&cur_pc, NULL,
1186 && G_FLD (inst2->v, 14, 10) == 0x10
1187 && G_FLD (inst2->v, 9, 5) == 0x0
1188 && G_FLD (inst2->v, 4, 0) == 0x2)
1191 int imm = G_FLD (inst->v, 5, 0);
1193 imm = -(0x3F - imm + 1);
1199 else if (inst->len == 4)
1201 if (G_FLD (inst->v, 29, 25) == 0x3
1202 && G_FLD (inst->v, 2, 0) == 0x4
1203 && G_FLD (inst->v, 24, 20) == 0x3
1204 && G_FLD (inst->v, 19, 15) == 0x0)
1206 /* sw r3, [r0, offset]+ */
1207 sp_offset += inst->len;
1208 if (ra_offset_p == 0)
1210 ra_offset = sp_offset;
1214 else if (G_FLD (inst->v, 29, 25) == 0x3
1215 && G_FLD (inst->v, 2, 0) == 0x4
1216 && G_FLD (inst->v, 24, 20) == 0x2
1217 && G_FLD (inst->v, 19, 15) == 0x0)
1219 /* sw r2, [r0, offset]+ */
1220 sp_offset += inst->len;
1221 if (fp_offset_p == 0)
1223 fp_offset = sp_offset;
1227 else if (G_FLD (inst->v, 29, 25) == 0x7
1228 && G_FLD (inst->v, 2, 0) == 0x0
1229 && G_FLD (inst->v, 24, 20) == 0x3
1230 && G_FLD (inst->v, 19, 15) == 0x0)
1232 /* lw r3, [r0]+, 4 */
1233 sp_offset -= inst->len;
1236 else if (G_FLD (inst->v, 29, 25) == 0x7
1237 && G_FLD (inst->v, 2, 0) == 0x0
1238 && G_FLD (inst->v, 24, 20) == 0x2
1239 && G_FLD (inst->v, 19, 15) == 0x0)
1241 /* lw r2, [r0]+, 4 */
1242 sp_offset -= inst->len;
1245 else if (G_FLD (inst->v, 29, 25) == 0x1
1246 && G_FLD (inst->v, 19, 17) == 0x0
1247 && G_FLD (inst->v, 24, 20) == 0x0
1248 && G_FLD (inst->v, 0, 0) == 0x0)
1250 /* addi r0, -offset */
1251 int imm = G_FLD (inst->v, 16, 1);
1253 imm = -(0xFFFF - imm + 1);
1256 else if (G_FLD (inst->v, 29, 25) == 0x1
1257 && G_FLD (inst->v, 19, 17) == 0x0
1258 && G_FLD (inst->v, 24, 20) == 0x2
1259 && G_FLD (inst->v, 0, 0) == 0x0)
1261 /* addi r2, offset */
1262 if (pc - cur_pc >= 2)
1266 cur_pc += inst->len;
1267 inst2 = score3_adjust_pc_and_fetch_inst (&cur_pc, NULL,
1271 && G_FLD (inst2->v, 14, 10) == 0x10
1272 && G_FLD (inst2->v, 9, 5) == 0x0
1273 && G_FLD (inst2->v, 4, 0) == 0x2)
1276 int imm = G_FLD (inst->v, 16, 1);
1278 imm = -(0xFFFF - imm + 1);
1287 if (ra_offset_p == 1)
1289 if (this_cache->saved_regs[SCORE_PC_REGNUM].addr == -1)
1290 this_cache->saved_regs[SCORE_PC_REGNUM].addr =
1291 sp + sp_offset - ra_offset;
1295 this_cache->saved_regs[SCORE_PC_REGNUM] =
1296 this_cache->saved_regs[SCORE_RA_REGNUM];
1300 if (fp_offset_p == 1)
1302 if (this_cache->saved_regs[SCORE_FP_REGNUM].addr == -1)
1303 this_cache->saved_regs[SCORE_FP_REGNUM].addr =
1304 sp + sp_offset - fp_offset;
1307 /* Save SP and FP. */
1308 this_cache->base = sp + sp_offset;
1309 this_cache->fp = fp;
1312 static struct score_frame_cache *
1313 score_make_prologue_cache (struct frame_info *this_frame, void **this_cache)
1315 struct score_frame_cache *cache;
1317 if ((*this_cache) != NULL)
1318 return (struct score_frame_cache *) (*this_cache);
1320 cache = FRAME_OBSTACK_ZALLOC (struct score_frame_cache);
1321 (*this_cache) = cache;
1322 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
1324 /* Analyze the prologue. */
1326 const CORE_ADDR pc = get_frame_pc (this_frame);
1327 CORE_ADDR start_addr;
1329 find_pc_partial_function (pc, NULL, &start_addr, NULL);
1330 if (start_addr == 0)
1333 if (target_mach == bfd_mach_score3)
1334 score3_analyze_prologue (start_addr, pc, this_frame,
1335 (struct score_frame_cache *) *this_cache);
1337 score7_analyze_prologue (start_addr, pc, this_frame,
1338 (struct score_frame_cache *) *this_cache);
1342 trad_frame_set_value (cache->saved_regs, SCORE_SP_REGNUM, cache->base);
1344 return (struct score_frame_cache *) (*this_cache);
1348 score_prologue_this_id (struct frame_info *this_frame, void **this_cache,
1349 struct frame_id *this_id)
1351 struct score_frame_cache *info = score_make_prologue_cache (this_frame,
1353 (*this_id) = frame_id_build (info->base, get_frame_func (this_frame));
1356 static struct value *
1357 score_prologue_prev_register (struct frame_info *this_frame,
1358 void **this_cache, int regnum)
1360 struct score_frame_cache *info = score_make_prologue_cache (this_frame,
1362 return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
1365 static const struct frame_unwind score_prologue_unwind =
1368 default_frame_unwind_stop_reason,
1369 score_prologue_this_id,
1370 score_prologue_prev_register,
1372 default_frame_sniffer,
1377 score_prologue_frame_base_address (struct frame_info *this_frame,
1380 struct score_frame_cache *info =
1381 score_make_prologue_cache (this_frame, this_cache);
1385 static const struct frame_base score_prologue_frame_base =
1387 &score_prologue_unwind,
1388 score_prologue_frame_base_address,
1389 score_prologue_frame_base_address,
1390 score_prologue_frame_base_address,
1393 static const struct frame_base *
1394 score_prologue_frame_base_sniffer (struct frame_info *this_frame)
1396 return &score_prologue_frame_base;
1399 /* Core file support. */
1401 static const struct regcache_map_entry score7_linux_gregmap[] =
1403 /* FIXME: According to the current Linux kernel, r0 is preceded by
1404 9 rather than 7 words. */
1405 { 7, REGCACHE_MAP_SKIP, 4 },
1406 { 32, 0, 4 }, /* r0 ... r31 */
1407 { 1, 55, 4 }, /* CEL */
1408 { 1, 54, 4 }, /* CEH */
1409 { 1, 53, 4 }, /* sr0, i.e. cnt or COUNTER */
1410 { 1, 52, 4 }, /* sr1, i.e. lcr or LDCR */
1411 { 1, 51, 4 }, /* sr2, i.e. scr or STCR */
1412 { 1, 49, 4 }, /* PC (same slot as EPC) */
1413 { 1, 38, 4 }, /* EMA */
1414 { 1, 32, 4 }, /* PSR */
1415 { 1, 34, 4 }, /* ECR */
1416 { 1, 33, 4 }, /* COND */
1420 #define SCORE7_LINUX_EPC_OFFSET (44 * 4)
1421 #define SCORE7_LINUX_SIZEOF_GREGSET (49 * 4)
1424 score7_linux_supply_gregset(const struct regset *regset,
1425 struct regcache *regcache,
1426 int regnum, const void *buf,
1429 regcache_supply_regset (regset, regcache, regnum, buf, size);
1431 /* Supply the EPC from the same slot as the PC. Note that the
1432 collect function will store the PC in that slot. */
1433 if ((regnum == -1 || regnum == SCORE_EPC_REGNUM)
1434 && size >= SCORE7_LINUX_EPC_OFFSET + 4)
1435 regcache_raw_supply (regcache, SCORE_EPC_REGNUM,
1436 (const gdb_byte *) buf
1437 + SCORE7_LINUX_EPC_OFFSET);
1440 static const struct regset score7_linux_gregset =
1442 score7_linux_gregmap,
1443 score7_linux_supply_gregset,
1444 regcache_collect_regset
1447 /* Iterate over core file register note sections. */
1450 score7_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
1451 iterate_over_regset_sections_cb *cb,
1453 const struct regcache *regcache)
1455 cb (".reg", SCORE7_LINUX_SIZEOF_GREGSET, &score7_linux_gregset,
1459 static struct gdbarch *
1460 score_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1462 struct gdbarch *gdbarch;
1463 target_mach = info.bfd_arch_info->mach;
1465 arches = gdbarch_list_lookup_by_info (arches, &info);
1468 return (arches->gdbarch);
1470 gdbarch = gdbarch_alloc (&info, NULL);
1472 set_gdbarch_short_bit (gdbarch, 16);
1473 set_gdbarch_int_bit (gdbarch, 32);
1474 set_gdbarch_float_bit (gdbarch, 32);
1475 set_gdbarch_double_bit (gdbarch, 64);
1476 set_gdbarch_long_double_bit (gdbarch, 64);
1478 set_gdbarch_register_sim_regno (gdbarch, score_register_sim_regno);
1480 set_gdbarch_pc_regnum (gdbarch, SCORE_PC_REGNUM);
1481 set_gdbarch_sp_regnum (gdbarch, SCORE_SP_REGNUM);
1482 set_gdbarch_adjust_breakpoint_address (gdbarch,
1483 score_adjust_breakpoint_address);
1484 set_gdbarch_register_type (gdbarch, score_register_type);
1485 set_gdbarch_frame_align (gdbarch, score_frame_align);
1486 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1487 set_gdbarch_unwind_sp (gdbarch, score_unwind_sp);
1488 set_gdbarch_unwind_pc (gdbarch, score_unwind_pc);
1489 set_gdbarch_print_insn (gdbarch, score_print_insn);
1491 switch (target_mach)
1493 case bfd_mach_score7:
1494 set_gdbarch_breakpoint_from_pc (gdbarch, score7_breakpoint_from_pc);
1495 set_gdbarch_skip_prologue (gdbarch, score7_skip_prologue);
1496 set_gdbarch_stack_frame_destroyed_p (gdbarch,
1497 score7_stack_frame_destroyed_p);
1498 set_gdbarch_register_name (gdbarch, score7_register_name);
1499 set_gdbarch_num_regs (gdbarch, SCORE7_NUM_REGS);
1500 /* Core file support. */
1501 set_gdbarch_iterate_over_regset_sections
1502 (gdbarch, score7_linux_iterate_over_regset_sections);
1505 case bfd_mach_score3:
1506 set_gdbarch_breakpoint_from_pc (gdbarch, score3_breakpoint_from_pc);
1507 set_gdbarch_skip_prologue (gdbarch, score3_skip_prologue);
1508 set_gdbarch_stack_frame_destroyed_p (gdbarch,
1509 score3_stack_frame_destroyed_p);
1510 set_gdbarch_register_name (gdbarch, score3_register_name);
1511 set_gdbarch_num_regs (gdbarch, SCORE3_NUM_REGS);
1515 /* Watchpoint hooks. */
1516 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
1518 /* Dummy frame hooks. */
1519 set_gdbarch_return_value (gdbarch, score_return_value);
1520 set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
1521 set_gdbarch_dummy_id (gdbarch, score_dummy_id);
1522 set_gdbarch_push_dummy_call (gdbarch, score_push_dummy_call);
1524 /* Normal frame hooks. */
1525 dwarf2_append_unwinders (gdbarch);
1526 frame_base_append_sniffer (gdbarch, dwarf2_frame_base_sniffer);
1527 frame_unwind_append_unwinder (gdbarch, &score_prologue_unwind);
1528 frame_base_append_sniffer (gdbarch, score_prologue_frame_base_sniffer);
1533 extern initialize_file_ftype _initialize_score_tdep;
1536 _initialize_score_tdep (void)
1538 gdbarch_register (bfd_arch_score, score_gdbarch_init, NULL);