1 /* Target-dependent code for the S+core architecture, for GDB,
4 Copyright (C) 2006-2014 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;
60 score_register_type (struct gdbarch *gdbarch, int regnum)
62 gdb_assert (regnum >= 0
63 && regnum < ((target_mach == bfd_mach_score7)
64 ? SCORE7_NUM_REGS : SCORE3_NUM_REGS));
65 return builtin_type (gdbarch)->builtin_uint32;
69 score_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
71 return frame_unwind_register_unsigned (next_frame, SCORE_SP_REGNUM);
75 score_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
77 return frame_unwind_register_unsigned (next_frame, SCORE_PC_REGNUM);
81 score7_register_name (struct gdbarch *gdbarch, int regnum)
83 const char *score_register_names[] = {
84 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
85 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
86 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
87 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
89 "PSR", "COND", "ECR", "EXCPVEC", "CCR",
90 "EPC", "EMA", "TLBLOCK", "TLBPT", "PEADDR",
91 "TLBRPT", "PEVN", "PECTX", "LIMPFN", "LDMPFN",
92 "PREV", "DREG", "PC", "DSAVE", "COUNTER",
93 "LDCR", "STCR", "CEH", "CEL",
96 gdb_assert (regnum >= 0 && regnum < SCORE7_NUM_REGS);
97 return score_register_names[regnum];
101 score3_register_name (struct gdbarch *gdbarch, int regnum)
103 const char *score_register_names[] = {
104 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
105 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
106 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
107 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
109 "PSR", "COND", "ECR", "EXCPVEC", "CCR",
110 "EPC", "EMA", "PREV", "DREG", "DSAVE",
111 "COUNTER", "LDCR", "STCR", "CEH", "CEL",
115 gdb_assert (regnum >= 0 && regnum < SCORE3_NUM_REGS);
116 return score_register_names[regnum];
121 score_register_sim_regno (struct gdbarch *gdbarch, int regnum)
123 gdb_assert (regnum >= 0
124 && regnum < ((target_mach == bfd_mach_score7)
125 ? SCORE7_NUM_REGS : SCORE3_NUM_REGS));
131 score_print_insn (bfd_vma memaddr, struct disassemble_info *info)
133 if (info->endian == BFD_ENDIAN_BIG)
134 return print_insn_big_score (memaddr, info);
136 return print_insn_little_score (memaddr, info);
140 score7_fetch_inst (struct gdbarch *gdbarch, CORE_ADDR addr, gdb_byte *memblock)
142 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
143 static inst_t inst = { 0, 0, 0 };
144 gdb_byte buf[SCORE_INSTLEN] = { 0 };
148 if (target_has_execution && memblock != NULL)
150 /* Fetch instruction from local MEMBLOCK. */
151 memcpy (buf, memblock, SCORE_INSTLEN);
155 /* Fetch instruction from target. */
156 ret = target_read_memory (addr & ~0x3, buf, SCORE_INSTLEN);
159 error (_("Error: target_read_memory in file:%s, line:%d!"),
165 inst.raw = extract_unsigned_integer (buf, SCORE_INSTLEN, byte_order);
166 inst.len = (inst.raw & 0x80008000) ? 4 : 2;
167 inst.v = ((inst.raw >> 16 & 0x7FFF) << 15) | (inst.raw & 0x7FFF);
168 big = (byte_order == BFD_ENDIAN_BIG);
171 if (big ^ ((addr & 0x2) == 2))
172 inst.v = G_FLD (inst.v, 29, 15);
174 inst.v = G_FLD (inst.v, 14, 0);
180 score3_adjust_pc_and_fetch_inst (CORE_ADDR *pcptr, int *lenptr,
181 enum bfd_endian byte_order)
183 static inst_t inst = { 0, 0, 0 };
190 /* raw table 1 (column 2, 3, 4)
194 table 2 (column 1, 2, 3)
200 static const struct breakplace bk_table[16] =
222 #define EXTRACT_LEN 2
223 CORE_ADDR adjust_pc = *pcptr & ~0x1;
224 gdb_byte buf[5][EXTRACT_LEN] =
234 unsigned int cbits = 0;
243 for (i = 0; i < 5; i++)
245 ret = target_read_memory (adjust_pc + 2 * i, buf[i], EXTRACT_LEN);
251 error (_("Error: target_read_memory in file:%s, line:%d!"),
255 raw = extract_unsigned_integer (buf[i], EXTRACT_LEN, byte_order);
256 cbits = (cbits << 1) | (raw >> 15);
263 cbits = (cbits >> 1) & 0x7;
269 cbits = (cbits >> 2) & 0x7;
270 bk_index = cbits + 8;
273 gdb_assert (!((bk_table[bk_index].break_offset == 0)
274 && (bk_table[bk_index].inst_len == 0)));
276 inst.len = bk_table[bk_index].inst_len;
278 i = (bk_table[bk_index].break_offset + 4) / 2;
279 count = inst.len / 2;
280 for (; count > 0; i++, count--)
282 inst.raw = (inst.raw << 16)
283 | extract_unsigned_integer (buf[i], EXTRACT_LEN, byte_order);
289 inst.v = inst.raw & 0x7FFF;
292 inst.v = ((inst.raw >> 16 & 0x7FFF) << 15) | (inst.raw & 0x7FFF);
295 inst.v = ((inst.raw >> 32 & 0x7FFF) << 30)
296 | ((inst.raw >> 16 & 0x7FFF) << 15) | (inst.raw & 0x7FFF);
301 *pcptr = adjust_pc + bk_table[bk_index].break_offset;
303 *lenptr = bk_table[bk_index].inst_len;
310 static const gdb_byte *
311 score7_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
314 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
315 gdb_byte buf[SCORE_INSTLEN] = { 0 };
319 if ((ret = target_read_memory (*pcptr & ~0x3, buf, SCORE_INSTLEN)) != 0)
321 error (_("Error: target_read_memory in file:%s, line:%d!"),
324 raw = extract_unsigned_integer (buf, SCORE_INSTLEN, byte_order);
326 if (byte_order == BFD_ENDIAN_BIG)
328 if (!(raw & 0x80008000))
330 /* 16bits instruction. */
331 static gdb_byte big_breakpoint16[] = { 0x60, 0x02 };
333 *lenptr = sizeof (big_breakpoint16);
334 return big_breakpoint16;
338 /* 32bits instruction. */
339 static gdb_byte big_breakpoint32[] = { 0x80, 0x00, 0x80, 0x06 };
341 *lenptr = sizeof (big_breakpoint32);
342 return big_breakpoint32;
347 if (!(raw & 0x80008000))
349 /* 16bits instruction. */
350 static gdb_byte little_breakpoint16[] = { 0x02, 0x60 };
352 *lenptr = sizeof (little_breakpoint16);
353 return little_breakpoint16;
357 /* 32bits instruction. */
358 static gdb_byte little_breakpoint32[] = { 0x06, 0x80, 0x00, 0x80 };
360 *lenptr = sizeof (little_breakpoint32);
361 return little_breakpoint32;
366 static const gdb_byte *
367 score3_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
370 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
371 CORE_ADDR adjust_pc = *pcptr;
373 static gdb_byte score_break_insns[6][6] = {
374 /* The following three instructions are big endian. */
376 { 0x80, 0x00, 0x00, 0x06 },
377 { 0x80, 0x00, 0x80, 0x00, 0x00, 0x00 },
378 /* The following three instructions are little endian. */
380 { 0x00, 0x80, 0x06, 0x00 },
381 { 0x00, 0x80, 0x00, 0x80, 0x00, 0x00 }};
386 score3_adjust_pc_and_fetch_inst (&adjust_pc, &len, byte_order);
388 index = ((byte_order == BFD_ENDIAN_BIG) ? 0 : 3) + (len / 2 - 1);
389 p = score_break_insns[index];
398 score_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
400 CORE_ADDR adjust_pc = bpaddr;
402 if (target_mach == bfd_mach_score3)
403 score3_adjust_pc_and_fetch_inst (&adjust_pc, NULL,
404 gdbarch_byte_order (gdbarch));
406 adjust_pc = align_down (adjust_pc, 2);
412 score_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
414 return align_down (addr, 16);
418 score_xfer_register (struct regcache *regcache, int regnum, int length,
419 enum bfd_endian endian, gdb_byte *readbuf,
420 const gdb_byte *writebuf, int buf_offset)
423 gdb_assert (regnum >= 0
424 && regnum < ((target_mach == bfd_mach_score7)
425 ? SCORE7_NUM_REGS : SCORE3_NUM_REGS));
430 reg_offset = SCORE_REGSIZE - length;
432 case BFD_ENDIAN_LITTLE:
435 case BFD_ENDIAN_UNKNOWN:
439 error (_("Error: score_xfer_register in file:%s, line:%d!"),
444 regcache_cooked_read_part (regcache, regnum, reg_offset, length,
445 readbuf + buf_offset);
446 if (writebuf != NULL)
447 regcache_cooked_write_part (regcache, regnum, reg_offset, length,
448 writebuf + buf_offset);
451 static enum return_value_convention
452 score_return_value (struct gdbarch *gdbarch, struct value *function,
453 struct type *type, struct regcache *regcache,
454 gdb_byte * readbuf, const gdb_byte * writebuf)
456 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
457 || TYPE_CODE (type) == TYPE_CODE_UNION
458 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
459 return RETURN_VALUE_STRUCT_CONVENTION;
464 for (offset = 0, regnum = SCORE_A0_REGNUM;
465 offset < TYPE_LENGTH (type);
466 offset += SCORE_REGSIZE, regnum++)
468 int xfer = SCORE_REGSIZE;
470 if (offset + xfer > TYPE_LENGTH (type))
471 xfer = TYPE_LENGTH (type) - offset;
472 score_xfer_register (regcache, regnum, xfer,
473 gdbarch_byte_order(gdbarch),
474 readbuf, writebuf, offset);
476 return RETURN_VALUE_REGISTER_CONVENTION;
480 static struct frame_id
481 score_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
483 return frame_id_build (get_frame_register_unsigned (this_frame,
485 get_frame_pc (this_frame));
489 score_type_needs_double_align (struct type *type)
491 enum type_code typecode = TYPE_CODE (type);
493 if ((typecode == TYPE_CODE_INT && TYPE_LENGTH (type) == 8)
494 || (typecode == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8))
496 else if (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION)
500 n = TYPE_NFIELDS (type);
501 for (i = 0; i < n; i++)
502 if (score_type_needs_double_align (TYPE_FIELD_TYPE (type, i)))
510 score_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
511 struct regcache *regcache, CORE_ADDR bp_addr,
512 int nargs, struct value **args, CORE_ADDR sp,
513 int struct_return, CORE_ADDR struct_addr)
515 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
519 CORE_ADDR stack_offset = 0;
522 /* Step 1, Save RA. */
523 regcache_cooked_write_unsigned (regcache, SCORE_RA_REGNUM, bp_addr);
525 /* Step 2, Make space on the stack for the args. */
526 struct_addr = align_down (struct_addr, 16);
527 sp = align_down (sp, 16);
528 for (argnum = 0; argnum < nargs; argnum++)
529 arglen += align_up (TYPE_LENGTH (value_type (args[argnum])),
531 sp -= align_up (arglen, 16);
533 argreg = SCORE_BEGIN_ARG_REGNUM;
535 /* Step 3, Check if struct return then save the struct address to
536 r4 and increase the stack_offset by 4. */
539 regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
540 stack_offset += SCORE_REGSIZE;
543 /* Step 4, Load arguments:
544 If arg length is too long (> 4 bytes), then split the arg and
546 for (argnum = 0; argnum < nargs; argnum++)
548 struct value *arg = args[argnum];
549 struct type *arg_type = check_typedef (value_type (arg));
550 enum type_code typecode = TYPE_CODE (arg_type);
551 const gdb_byte *val = value_contents (arg);
552 int downward_offset = 0;
553 int odd_sized_struct_p;
554 int arg_last_part_p = 0;
556 arglen = TYPE_LENGTH (arg_type);
557 odd_sized_struct_p = (arglen > SCORE_REGSIZE
558 && arglen % SCORE_REGSIZE != 0);
560 /* If a arg should be aligned to 8 bytes (long long or double),
561 the value should be put to even register numbers. */
562 if (score_type_needs_double_align (arg_type))
568 /* If sizeof a block < SCORE_REGSIZE, then Score GCC will chose
569 the default "downward"/"upward" method:
575 char a; char b; char c;
576 } s = {'a', 'b', 'c'};
578 Big endian: s = {X, 'a', 'b', 'c'}
579 Little endian: s = {'a', 'b', 'c', X}
581 Where X is a hole. */
583 if (gdbarch_byte_order(gdbarch) == BFD_ENDIAN_BIG
584 && (typecode == TYPE_CODE_STRUCT
585 || typecode == TYPE_CODE_UNION)
586 && argreg > SCORE_LAST_ARG_REGNUM
587 && arglen < SCORE_REGSIZE)
588 downward_offset += (SCORE_REGSIZE - arglen);
592 int partial_len = arglen < SCORE_REGSIZE ? arglen : SCORE_REGSIZE;
593 ULONGEST regval = extract_unsigned_integer (val, partial_len,
596 /* The last part of a arg should shift left when
597 gdbarch_byte_order is BFD_ENDIAN_BIG. */
598 if (byte_order == BFD_ENDIAN_BIG
599 && arg_last_part_p == 1
600 && (typecode == TYPE_CODE_STRUCT
601 || typecode == TYPE_CODE_UNION))
602 regval <<= ((SCORE_REGSIZE - partial_len) * TARGET_CHAR_BIT);
604 /* Always increase the stack_offset and save args to stack. */
605 addr = sp + stack_offset + downward_offset;
606 write_memory (addr, val, partial_len);
608 if (argreg <= SCORE_LAST_ARG_REGNUM)
610 regcache_cooked_write_unsigned (regcache, argreg++, regval);
611 if (arglen > SCORE_REGSIZE && arglen < SCORE_REGSIZE * 2)
616 arglen -= partial_len;
617 stack_offset += align_up (partial_len, SCORE_REGSIZE);
621 /* Step 5, Save SP. */
622 regcache_cooked_write_unsigned (regcache, SCORE_SP_REGNUM, sp);
628 score7_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
631 int iscan = 32, stack_sub = 0;
634 inst_t *inst = score7_fetch_inst (gdbarch, cpc, NULL);
637 if ((inst->len == 4) && !stack_sub
638 && (G_FLD (inst->v, 29, 25) == 0x1
639 && G_FLD (inst->v, 24, 20) == 0x0))
641 /* addi r0, offset */
642 stack_sub = cpc + SCORE_INSTLEN;
643 pc = cpc + SCORE_INSTLEN;
645 else if ((inst->len == 4)
646 && (G_FLD (inst->v, 29, 25) == 0x0)
647 && (G_FLD (inst->v, 24, 20) == 0x2)
648 && (G_FLD (inst->v, 19, 15) == 0x0)
649 && (G_FLD (inst->v, 14, 10) == 0xF)
650 && (G_FLD (inst->v, 9, 0) == 0x56))
653 pc = cpc + SCORE_INSTLEN;
656 else if ((inst->len == 2)
657 && (G_FLD (inst->v, 14, 12) == 0x0)
658 && (G_FLD (inst->v, 11, 8) == 0x2)
659 && (G_FLD (inst->v, 7, 4) == 0x0)
660 && (G_FLD (inst->v, 3, 0) == 0x3))
663 pc = cpc + SCORE16_INSTLEN;
666 else if ((inst->len == 2)
667 && ((G_FLD (inst->v, 14, 12) == 3) /* j15 form */
668 || (G_FLD (inst->v, 14, 12) == 4) /* b15 form */
669 || (G_FLD (inst->v, 14, 12) == 0x0
670 && G_FLD (inst->v, 3, 0) == 0x4))) /* br! */
672 else if ((inst->len == 4)
673 && ((G_FLD (inst->v, 29, 25) == 2) /* j32 form */
674 || (G_FLD (inst->v, 29, 25) == 4) /* b32 form */
675 || (G_FLD (inst->v, 29, 25) == 0x0
676 && G_FLD (inst->v, 6, 1) == 0x4))) /* br */
679 cpc += (inst->len == 2) ? SCORE16_INSTLEN : SCORE_INSTLEN;
685 score3_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
688 int iscan = 32, stack_sub = 0;
692 = score3_adjust_pc_and_fetch_inst (&cpc, NULL,
693 gdbarch_byte_order (gdbarch));
697 if (inst->len == 4 && !stack_sub
698 && (G_FLD (inst->v, 29, 25) == 0x1)
699 && (G_FLD (inst->v, 19, 17) == 0x0)
700 && (G_FLD (inst->v, 24, 20) == 0x0))
702 /* addi r0, offset */
703 stack_sub = cpc + inst->len;
704 pc = cpc + inst->len;
706 else if (inst->len == 4
707 && (G_FLD (inst->v, 29, 25) == 0x0)
708 && (G_FLD (inst->v, 24, 20) == 0x2)
709 && (G_FLD (inst->v, 19, 15) == 0x0)
710 && (G_FLD (inst->v, 14, 10) == 0xF)
711 && (G_FLD (inst->v, 9, 0) == 0x56))
714 pc = cpc + inst->len;
717 else if ((inst->len == 2)
718 && (G_FLD (inst->v, 14, 10) == 0x10)
719 && (G_FLD (inst->v, 9, 5) == 0x2)
720 && (G_FLD (inst->v, 4, 0) == 0x0))
723 pc = cpc + inst->len;
726 else if (inst->len == 2
727 && ((G_FLD (inst->v, 14, 12) == 3) /* b15 form */
728 || (G_FLD (inst->v, 14, 12) == 0x0
729 && G_FLD (inst->v, 11, 5) == 0x4))) /* br! */
731 else if (inst->len == 4
732 && ((G_FLD (inst->v, 29, 25) == 2) /* j32 form */
733 || (G_FLD (inst->v, 29, 25) == 4))) /* b32 form */
742 score7_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR cur_pc)
744 inst_t *inst = score7_fetch_inst (gdbarch, cur_pc, NULL);
747 return 1; /* mv! r0, r2 */
748 else if (G_FLD (inst->v, 14, 12) == 0x2
749 && G_FLD (inst->v, 3, 0) == 0xa)
751 else if (G_FLD (inst->v, 14, 12) == 0x0
752 && G_FLD (inst->v, 7, 0) == 0x34)
753 return 1; /* br! r3 */
754 else if (G_FLD (inst->v, 29, 15) == 0x2
755 && G_FLD (inst->v, 6, 1) == 0x2b)
756 return 1; /* mv r0, r2 */
757 else if (G_FLD (inst->v, 29, 25) == 0x0
758 && G_FLD (inst->v, 6, 1) == 0x4
759 && G_FLD (inst->v, 19, 15) == 0x3)
760 return 1; /* br r3 */
766 score3_in_function_epilogue_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 = 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 CORE_ADDR prev_pc = -1;
1090 sp = get_frame_register_unsigned (this_frame, SCORE_SP_REGNUM);
1091 fp = get_frame_register_unsigned (this_frame, SCORE_FP_REGNUM);
1093 for (; cur_pc < pc; prev_pc = cur_pc, cur_pc += inst_len)
1095 inst_t *inst = NULL;
1097 inst = score3_adjust_pc_and_fetch_inst (&cur_pc, &inst_len, byte_order);
1099 /* FIXME: make a full-power prologue analyzer. */
1102 if (G_FLD (inst->v, 14, 12) == 0x0
1103 && G_FLD (inst->v, 11, 7) == 0x0
1104 && G_FLD (inst->v, 6, 5) == 0x3)
1109 if (G_FLD (inst->v, 4, 0) == 0x3
1110 && ra_offset_p == 0)
1112 /* push! r3, [r0] */
1113 ra_offset = sp_offset;
1116 else if (G_FLD (inst->v, 4, 0) == 0x2
1117 && fp_offset_p == 0)
1119 /* push! r2, [r0] */
1120 fp_offset = sp_offset;
1124 else if (G_FLD (inst->v, 14, 12) == 0x6
1125 && G_FLD (inst->v, 11, 10) == 0x3)
1128 int start_r = G_FLD (inst->v, 9, 5);
1129 int cnt = G_FLD (inst->v, 4, 0);
1131 if ((ra_offset_p == 0)
1132 && (start_r <= SCORE_RA_REGNUM)
1133 && (SCORE_RA_REGNUM < start_r + cnt))
1135 /* rpush! contains r3 */
1137 ra_offset = sp_offset + 4 * (SCORE_RA_REGNUM - start_r) + 4;
1140 if ((fp_offset_p == 0)
1141 && (start_r <= SCORE_FP_REGNUM)
1142 && (SCORE_FP_REGNUM < start_r + cnt))
1144 /* rpush! contains r2 */
1146 fp_offset = sp_offset + 4 * (SCORE_FP_REGNUM - start_r) + 4;
1149 sp_offset += 4 * cnt;
1151 else if (G_FLD (inst->v, 14, 12) == 0x0
1152 && G_FLD (inst->v, 11, 7) == 0x0
1153 && G_FLD (inst->v, 6, 5) == 0x2)
1158 else if (G_FLD (inst->v, 14, 12) == 0x6
1159 && G_FLD (inst->v, 11, 10) == 0x2)
1162 sp_offset -= 4 * G_FLD (inst->v, 4, 0);
1164 else if (G_FLD (inst->v, 14, 12) == 0x5
1165 && G_FLD (inst->v, 11, 10) == 0x3
1166 && G_FLD (inst->v, 9, 6) == 0x0)
1168 /* addi! r0, -offset */
1169 int imm = G_FLD (inst->v, 5, 0);
1171 imm = -(0x3F - imm + 1);
1174 else if (G_FLD (inst->v, 14, 12) == 0x5
1175 && G_FLD (inst->v, 11, 10) == 0x3
1176 && G_FLD (inst->v, 9, 6) == 0x2)
1178 /* addi! r2, offset */
1179 if (pc - cur_pc >= 2)
1181 unsigned int save_v = inst->v;
1184 cur_pc += inst->len;
1185 inst2 = score3_adjust_pc_and_fetch_inst (&cur_pc, NULL,
1189 && G_FLD (inst2->v, 14, 10) == 0x10
1190 && G_FLD (inst2->v, 9, 5) == 0x0
1191 && G_FLD (inst2->v, 4, 0) == 0x2)
1194 int imm = G_FLD (inst->v, 5, 0);
1196 imm = -(0x3F - imm + 1);
1202 else if (inst->len == 4)
1204 if (G_FLD (inst->v, 29, 25) == 0x3
1205 && G_FLD (inst->v, 2, 0) == 0x4
1206 && G_FLD (inst->v, 24, 20) == 0x3
1207 && G_FLD (inst->v, 19, 15) == 0x0)
1209 /* sw r3, [r0, offset]+ */
1210 sp_offset += inst->len;
1211 if (ra_offset_p == 0)
1213 ra_offset = sp_offset;
1217 else if (G_FLD (inst->v, 29, 25) == 0x3
1218 && G_FLD (inst->v, 2, 0) == 0x4
1219 && G_FLD (inst->v, 24, 20) == 0x2
1220 && G_FLD (inst->v, 19, 15) == 0x0)
1222 /* sw r2, [r0, offset]+ */
1223 sp_offset += inst->len;
1224 if (fp_offset_p == 0)
1226 fp_offset = sp_offset;
1230 else if (G_FLD (inst->v, 29, 25) == 0x7
1231 && G_FLD (inst->v, 2, 0) == 0x0
1232 && G_FLD (inst->v, 24, 20) == 0x3
1233 && G_FLD (inst->v, 19, 15) == 0x0)
1235 /* lw r3, [r0]+, 4 */
1236 sp_offset -= inst->len;
1239 else if (G_FLD (inst->v, 29, 25) == 0x7
1240 && G_FLD (inst->v, 2, 0) == 0x0
1241 && G_FLD (inst->v, 24, 20) == 0x2
1242 && G_FLD (inst->v, 19, 15) == 0x0)
1244 /* lw r2, [r0]+, 4 */
1245 sp_offset -= inst->len;
1248 else if (G_FLD (inst->v, 29, 25) == 0x1
1249 && G_FLD (inst->v, 19, 17) == 0x0
1250 && G_FLD (inst->v, 24, 20) == 0x0
1251 && G_FLD (inst->v, 0, 0) == 0x0)
1253 /* addi r0, -offset */
1254 int imm = G_FLD (inst->v, 16, 1);
1256 imm = -(0xFFFF - imm + 1);
1259 else if (G_FLD (inst->v, 29, 25) == 0x1
1260 && G_FLD (inst->v, 19, 17) == 0x0
1261 && G_FLD (inst->v, 24, 20) == 0x2
1262 && G_FLD (inst->v, 0, 0) == 0x0)
1264 /* addi r2, offset */
1265 if (pc - cur_pc >= 2)
1267 unsigned int save_v = inst->v;
1270 cur_pc += inst->len;
1271 inst2 = score3_adjust_pc_and_fetch_inst (&cur_pc, NULL,
1275 && G_FLD (inst2->v, 14, 10) == 0x10
1276 && G_FLD (inst2->v, 9, 5) == 0x0
1277 && G_FLD (inst2->v, 4, 0) == 0x2)
1280 int imm = G_FLD (inst->v, 16, 1);
1282 imm = -(0xFFFF - imm + 1);
1291 if (ra_offset_p == 1)
1293 if (this_cache->saved_regs[SCORE_PC_REGNUM].addr == -1)
1294 this_cache->saved_regs[SCORE_PC_REGNUM].addr =
1295 sp + sp_offset - ra_offset;
1299 this_cache->saved_regs[SCORE_PC_REGNUM] =
1300 this_cache->saved_regs[SCORE_RA_REGNUM];
1304 if (fp_offset_p == 1)
1306 if (this_cache->saved_regs[SCORE_FP_REGNUM].addr == -1)
1307 this_cache->saved_regs[SCORE_FP_REGNUM].addr =
1308 sp + sp_offset - fp_offset;
1311 /* Save SP and FP. */
1312 this_cache->base = sp + sp_offset;
1313 this_cache->fp = fp;
1316 static struct score_frame_cache *
1317 score_make_prologue_cache (struct frame_info *this_frame, void **this_cache)
1319 struct score_frame_cache *cache;
1321 if ((*this_cache) != NULL)
1322 return (*this_cache);
1324 cache = FRAME_OBSTACK_ZALLOC (struct score_frame_cache);
1325 (*this_cache) = cache;
1326 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
1328 /* Analyze the prologue. */
1330 const CORE_ADDR pc = get_frame_pc (this_frame);
1331 CORE_ADDR start_addr;
1333 find_pc_partial_function (pc, NULL, &start_addr, NULL);
1334 if (start_addr == 0)
1337 if (target_mach == bfd_mach_score3)
1338 score3_analyze_prologue (start_addr, pc, this_frame, *this_cache);
1340 score7_analyze_prologue (start_addr, pc, this_frame, *this_cache);
1344 trad_frame_set_value (cache->saved_regs, SCORE_SP_REGNUM, cache->base);
1346 return (*this_cache);
1350 score_prologue_this_id (struct frame_info *this_frame, void **this_cache,
1351 struct frame_id *this_id)
1353 struct score_frame_cache *info = score_make_prologue_cache (this_frame,
1355 (*this_id) = frame_id_build (info->base, get_frame_func (this_frame));
1358 static struct value *
1359 score_prologue_prev_register (struct frame_info *this_frame,
1360 void **this_cache, int regnum)
1362 struct score_frame_cache *info = score_make_prologue_cache (this_frame,
1364 return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
1367 static const struct frame_unwind score_prologue_unwind =
1370 default_frame_unwind_stop_reason,
1371 score_prologue_this_id,
1372 score_prologue_prev_register,
1374 default_frame_sniffer,
1379 score_prologue_frame_base_address (struct frame_info *this_frame,
1382 struct score_frame_cache *info =
1383 score_make_prologue_cache (this_frame, this_cache);
1387 static const struct frame_base score_prologue_frame_base =
1389 &score_prologue_unwind,
1390 score_prologue_frame_base_address,
1391 score_prologue_frame_base_address,
1392 score_prologue_frame_base_address,
1395 static const struct frame_base *
1396 score_prologue_frame_base_sniffer (struct frame_info *this_frame)
1398 return &score_prologue_frame_base;
1401 /* Core file support (dirty hack)
1403 The core file MUST be generated by GNU/Linux on S+core. */
1406 score7_linux_supply_gregset(const struct regset *regset,
1407 struct regcache *regcache,
1408 int regnum, const void *gregs_buf, size_t len)
1411 elf_gregset_t *gregs;
1413 gdb_assert (regset != NULL);
1414 gdb_assert ((regcache != NULL) && (gregs_buf != NULL));
1416 gregs = (elf_gregset_t *) gregs_buf;
1418 for (regno = 0; regno < 32; regno++)
1419 if (regnum == -1 || regnum == regno)
1420 regcache_raw_supply (regcache, regno, gregs->regs + regno);
1427 { 55, &(gregs->cel) }, /* CEL */
1428 { 54, &(gregs->ceh) }, /* CEH */
1429 { 53, &(gregs->sr0) }, /* sr0, i.e. cnt or COUNTER */
1430 { 52, &(gregs->sr1) }, /* sr1, i.e. lcr or LDCR */
1431 { 51, &(gregs->sr1) }, /* sr2, i.e. scr or STCR */
1433 /* Exception occured at this address, exactly the PC we want */
1434 { 49, &(gregs->cp0_epc) }, /* PC */
1436 { 38, &(gregs->cp0_ema) }, /* EMA */
1437 { 37, &(gregs->cp0_epc) }, /* EPC */
1438 { 34, &(gregs->cp0_ecr) }, /* ECR */
1439 { 33, &(gregs->cp0_condition) }, /* COND */
1440 { 32, &(gregs->cp0_psr) }, /* PSR */
1443 for (regno = 0; regno < sizeof(sregs)/sizeof(sregs[0]); regno++)
1444 if (regnum == -1 || regnum == sregs[regno].regnum)
1445 regcache_raw_supply (regcache,
1446 sregs[regno].regnum, sregs[regno].buf);
1450 static const struct regset score7_linux_gregset =
1453 score7_linux_supply_gregset, NULL
1456 /* Return the appropriate register set from the core section identified
1457 by SECT_NAME and SECT_SIZE. */
1459 static const struct regset *
1460 score7_linux_regset_from_core_section(struct gdbarch *gdbarch,
1461 const char *sect_name, size_t sect_size)
1463 gdb_assert (gdbarch != NULL);
1464 gdb_assert (sect_name != NULL);
1466 if (strcmp(sect_name, ".reg") == 0 && sect_size == sizeof(elf_gregset_t))
1467 return &score7_linux_gregset;
1472 static struct gdbarch *
1473 score_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1475 struct gdbarch *gdbarch;
1476 target_mach = info.bfd_arch_info->mach;
1478 arches = gdbarch_list_lookup_by_info (arches, &info);
1481 return (arches->gdbarch);
1483 gdbarch = gdbarch_alloc (&info, NULL);
1485 set_gdbarch_short_bit (gdbarch, 16);
1486 set_gdbarch_int_bit (gdbarch, 32);
1487 set_gdbarch_float_bit (gdbarch, 32);
1488 set_gdbarch_double_bit (gdbarch, 64);
1489 set_gdbarch_long_double_bit (gdbarch, 64);
1491 set_gdbarch_register_sim_regno (gdbarch, score_register_sim_regno);
1493 set_gdbarch_pc_regnum (gdbarch, SCORE_PC_REGNUM);
1494 set_gdbarch_sp_regnum (gdbarch, SCORE_SP_REGNUM);
1495 set_gdbarch_adjust_breakpoint_address (gdbarch,
1496 score_adjust_breakpoint_address);
1497 set_gdbarch_register_type (gdbarch, score_register_type);
1498 set_gdbarch_frame_align (gdbarch, score_frame_align);
1499 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1500 set_gdbarch_unwind_sp (gdbarch, score_unwind_sp);
1501 set_gdbarch_unwind_pc (gdbarch, score_unwind_pc);
1502 set_gdbarch_print_insn (gdbarch, score_print_insn);
1504 switch (target_mach)
1506 case bfd_mach_score7:
1507 set_gdbarch_breakpoint_from_pc (gdbarch, score7_breakpoint_from_pc);
1508 set_gdbarch_skip_prologue (gdbarch, score7_skip_prologue);
1509 set_gdbarch_in_function_epilogue_p (gdbarch,
1510 score7_in_function_epilogue_p);
1511 set_gdbarch_register_name (gdbarch, score7_register_name);
1512 set_gdbarch_num_regs (gdbarch, SCORE7_NUM_REGS);
1513 /* Core file support. */
1514 set_gdbarch_regset_from_core_section (gdbarch,
1515 score7_linux_regset_from_core_section);
1518 case bfd_mach_score3:
1519 set_gdbarch_breakpoint_from_pc (gdbarch, score3_breakpoint_from_pc);
1520 set_gdbarch_skip_prologue (gdbarch, score3_skip_prologue);
1521 set_gdbarch_in_function_epilogue_p (gdbarch,
1522 score3_in_function_epilogue_p);
1523 set_gdbarch_register_name (gdbarch, score3_register_name);
1524 set_gdbarch_num_regs (gdbarch, SCORE3_NUM_REGS);
1528 /* Watchpoint hooks. */
1529 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
1531 /* Dummy frame hooks. */
1532 set_gdbarch_return_value (gdbarch, score_return_value);
1533 set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
1534 set_gdbarch_dummy_id (gdbarch, score_dummy_id);
1535 set_gdbarch_push_dummy_call (gdbarch, score_push_dummy_call);
1537 /* Normal frame hooks. */
1538 dwarf2_append_unwinders (gdbarch);
1539 frame_base_append_sniffer (gdbarch, dwarf2_frame_base_sniffer);
1540 frame_unwind_append_unwinder (gdbarch, &score_prologue_unwind);
1541 frame_base_append_sniffer (gdbarch, score_prologue_frame_base_sniffer);
1546 extern initialize_file_ftype _initialize_score_tdep;
1549 _initialize_score_tdep (void)
1551 gdbarch_register (bfd_arch_score, score_gdbarch_init, NULL);