1 /* Target-dependent code for the S+core architecture, for GDB,
4 Copyright (C) 2006-2019 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 score7_register_name (struct gdbarch *gdbarch, int regnum)
70 const char *score_register_names[] = {
71 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
72 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
73 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
74 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
76 "PSR", "COND", "ECR", "EXCPVEC", "CCR",
77 "EPC", "EMA", "TLBLOCK", "TLBPT", "PEADDR",
78 "TLBRPT", "PEVN", "PECTX", "LIMPFN", "LDMPFN",
79 "PREV", "DREG", "PC", "DSAVE", "COUNTER",
80 "LDCR", "STCR", "CEH", "CEL",
83 gdb_assert (regnum >= 0 && regnum < SCORE7_NUM_REGS);
84 return score_register_names[regnum];
88 score3_register_name (struct gdbarch *gdbarch, int regnum)
90 const char *score_register_names[] = {
91 "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
92 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
93 "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
94 "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",
96 "PSR", "COND", "ECR", "EXCPVEC", "CCR",
97 "EPC", "EMA", "PREV", "DREG", "DSAVE",
98 "COUNTER", "LDCR", "STCR", "CEH", "CEL",
102 gdb_assert (regnum >= 0 && regnum < SCORE3_NUM_REGS);
103 return score_register_names[regnum];
108 score_register_sim_regno (struct gdbarch *gdbarch, int regnum)
110 gdb_assert (regnum >= 0
111 && regnum < ((target_mach == bfd_mach_score7)
112 ? SCORE7_NUM_REGS : SCORE3_NUM_REGS));
118 score7_fetch_inst (struct gdbarch *gdbarch, CORE_ADDR addr, gdb_byte *memblock)
120 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
121 static inst_t inst = { 0, 0, 0 };
122 gdb_byte buf[SCORE_INSTLEN] = { 0 };
126 if (target_has_execution && memblock != NULL)
128 /* Fetch instruction from local MEMBLOCK. */
129 memcpy (buf, memblock, SCORE_INSTLEN);
133 /* Fetch instruction from target. */
134 ret = target_read_memory (addr & ~0x3, buf, SCORE_INSTLEN);
137 error (_("Error: target_read_memory in file:%s, line:%d!"),
143 inst.raw = extract_unsigned_integer (buf, SCORE_INSTLEN, byte_order);
144 inst.len = (inst.raw & 0x80008000) ? 4 : 2;
145 inst.v = ((inst.raw >> 16 & 0x7FFF) << 15) | (inst.raw & 0x7FFF);
146 big = (byte_order == BFD_ENDIAN_BIG);
149 if (big ^ ((addr & 0x2) == 2))
150 inst.v = G_FLD (inst.v, 29, 15);
152 inst.v = G_FLD (inst.v, 14, 0);
158 score3_adjust_pc_and_fetch_inst (CORE_ADDR *pcptr, int *lenptr,
159 enum bfd_endian byte_order)
161 static inst_t inst = { 0, 0, 0 };
168 /* raw table 1 (column 2, 3, 4)
172 table 2 (column 1, 2, 3)
178 static const struct breakplace bk_table[16] =
200 #define EXTRACT_LEN 2
201 CORE_ADDR adjust_pc = *pcptr & ~0x1;
202 gdb_byte buf[5][EXTRACT_LEN] =
212 unsigned int cbits = 0;
221 for (i = 0; i < 5; i++)
223 ret = target_read_memory (adjust_pc + 2 * i, buf[i], EXTRACT_LEN);
229 error (_("Error: target_read_memory in file:%s, line:%d!"),
233 raw = extract_unsigned_integer (buf[i], EXTRACT_LEN, byte_order);
234 cbits = (cbits << 1) | (raw >> 15);
241 cbits = (cbits >> 1) & 0x7;
247 cbits = (cbits >> 2) & 0x7;
248 bk_index = cbits + 8;
251 gdb_assert (!((bk_table[bk_index].break_offset == 0)
252 && (bk_table[bk_index].inst_len == 0)));
254 inst.len = bk_table[bk_index].inst_len;
256 i = (bk_table[bk_index].break_offset + 4) / 2;
257 count = inst.len / 2;
258 for (; count > 0; i++, count--)
260 inst.raw = (inst.raw << 16)
261 | extract_unsigned_integer (buf[i], EXTRACT_LEN, byte_order);
267 inst.v = inst.raw & 0x7FFF;
270 inst.v = ((inst.raw >> 16 & 0x7FFF) << 15) | (inst.raw & 0x7FFF);
273 inst.v = ((inst.raw >> 32 & 0x7FFF) << 30)
274 | ((inst.raw >> 16 & 0x7FFF) << 15) | (inst.raw & 0x7FFF);
279 *pcptr = adjust_pc + bk_table[bk_index].break_offset;
281 *lenptr = bk_table[bk_index].inst_len;
288 /* Implement the breakpoint_kind_from_pc gdbarch method. */
291 score7_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
295 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
296 gdb_byte buf[SCORE_INSTLEN] = { 0 };
298 if ((ret = target_read_memory (*pcptr & ~0x3, buf, SCORE_INSTLEN)) != 0)
300 error (_("Error: target_read_memory in file:%s, line:%d!"),
303 raw = extract_unsigned_integer (buf, SCORE_INSTLEN, byte_order);
305 if (!(raw & 0x80008000))
307 /* 16bits instruction. */
313 /* 32bits instruction. */
319 /* Implement the sw_breakpoint_from_kind gdbarch method. */
321 static const gdb_byte *
322 score7_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
324 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
330 static gdb_byte big_breakpoint32[] = { 0x80, 0x00, 0x80, 0x06 };
331 static gdb_byte little_breakpoint32[] = { 0x06, 0x80, 0x00, 0x80 };
333 if (byte_order == BFD_ENDIAN_BIG)
334 return big_breakpoint32;
336 return little_breakpoint32;
340 static gdb_byte big_breakpoint16[] = { 0x60, 0x02 };
341 static gdb_byte little_breakpoint16[] = { 0x02, 0x60 };
343 if (byte_order == BFD_ENDIAN_BIG)
344 return big_breakpoint16;
346 return little_breakpoint16;
350 /* Implement the breakpoint_kind_from_pc gdbarch method. */
353 score3_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
355 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
358 score3_adjust_pc_and_fetch_inst (pcptr, &len, byte_order);
363 /* Implement the sw_breakpoint_from_kind gdbarch method. */
365 static const gdb_byte *
366 score3_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
369 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
370 static gdb_byte score_break_insns[6][6] = {
371 /* The following three instructions are big endian. */
373 { 0x80, 0x00, 0x00, 0x06 },
374 { 0x80, 0x00, 0x80, 0x00, 0x00, 0x00 },
375 /* The following three instructions are little endian. */
377 { 0x00, 0x80, 0x06, 0x00 },
378 { 0x00, 0x80, 0x00, 0x80, 0x00, 0x00 }};
382 index = ((byte_order == BFD_ENDIAN_BIG) ? 0 : 3) + (kind / 2 - 1);
383 return score_break_insns[index];
387 score_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
389 CORE_ADDR adjust_pc = bpaddr;
391 if (target_mach == bfd_mach_score3)
392 score3_adjust_pc_and_fetch_inst (&adjust_pc, NULL,
393 gdbarch_byte_order (gdbarch));
395 adjust_pc = align_down (adjust_pc, 2);
401 score_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
403 return align_down (addr, 16);
407 score_xfer_register (struct regcache *regcache, int regnum, int length,
408 enum bfd_endian endian, gdb_byte *readbuf,
409 const gdb_byte *writebuf, int buf_offset)
412 gdb_assert (regnum >= 0
413 && regnum < ((target_mach == bfd_mach_score7)
414 ? SCORE7_NUM_REGS : SCORE3_NUM_REGS));
419 reg_offset = SCORE_REGSIZE - length;
421 case BFD_ENDIAN_LITTLE:
424 case BFD_ENDIAN_UNKNOWN:
428 error (_("Error: score_xfer_register in file:%s, line:%d!"),
433 regcache->cooked_read_part (regnum, reg_offset, length,
434 readbuf + buf_offset);
435 if (writebuf != NULL)
436 regcache->cooked_write_part (regnum, reg_offset, length,
437 writebuf + buf_offset);
440 static enum return_value_convention
441 score_return_value (struct gdbarch *gdbarch, struct value *function,
442 struct type *type, struct regcache *regcache,
443 gdb_byte * readbuf, const gdb_byte * writebuf)
445 if (TYPE_CODE (type) == TYPE_CODE_STRUCT
446 || TYPE_CODE (type) == TYPE_CODE_UNION
447 || TYPE_CODE (type) == TYPE_CODE_ARRAY)
448 return RETURN_VALUE_STRUCT_CONVENTION;
453 for (offset = 0, regnum = SCORE_A0_REGNUM;
454 offset < TYPE_LENGTH (type);
455 offset += SCORE_REGSIZE, regnum++)
457 int xfer = SCORE_REGSIZE;
459 if (offset + xfer > TYPE_LENGTH (type))
460 xfer = TYPE_LENGTH (type) - offset;
461 score_xfer_register (regcache, regnum, xfer,
462 gdbarch_byte_order(gdbarch),
463 readbuf, writebuf, offset);
465 return RETURN_VALUE_REGISTER_CONVENTION;
470 score_type_needs_double_align (struct type *type)
472 enum type_code typecode = TYPE_CODE (type);
474 if ((typecode == TYPE_CODE_INT && TYPE_LENGTH (type) == 8)
475 || (typecode == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8))
477 else if (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION)
481 n = TYPE_NFIELDS (type);
482 for (i = 0; i < n; i++)
483 if (score_type_needs_double_align (TYPE_FIELD_TYPE (type, i)))
491 score_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
492 struct regcache *regcache, CORE_ADDR bp_addr,
493 int nargs, struct value **args, CORE_ADDR sp,
494 function_call_return_method return_method,
495 CORE_ADDR struct_addr)
497 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
501 CORE_ADDR stack_offset = 0;
504 /* Step 1, Save RA. */
505 regcache_cooked_write_unsigned (regcache, SCORE_RA_REGNUM, bp_addr);
507 /* Step 2, Make space on the stack for the args. */
508 struct_addr = align_down (struct_addr, 16);
509 sp = align_down (sp, 16);
510 for (argnum = 0; argnum < nargs; argnum++)
511 arglen += align_up (TYPE_LENGTH (value_type (args[argnum])),
513 sp -= align_up (arglen, 16);
515 argreg = SCORE_BEGIN_ARG_REGNUM;
517 /* Step 3, Check if struct return then save the struct address to
518 r4 and increase the stack_offset by 4. */
519 if (return_method == return_method_struct)
521 regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
522 stack_offset += SCORE_REGSIZE;
525 /* Step 4, Load arguments:
526 If arg length is too long (> 4 bytes), then split the arg and
528 for (argnum = 0; argnum < nargs; argnum++)
530 struct value *arg = args[argnum];
531 struct type *arg_type = check_typedef (value_type (arg));
532 enum type_code typecode = TYPE_CODE (arg_type);
533 const gdb_byte *val = value_contents (arg);
534 int downward_offset = 0;
535 int arg_last_part_p = 0;
537 arglen = TYPE_LENGTH (arg_type);
539 /* If a arg should be aligned to 8 bytes (long long or double),
540 the value should be put to even register numbers. */
541 if (score_type_needs_double_align (arg_type))
547 /* If sizeof a block < SCORE_REGSIZE, then Score GCC will chose
548 the default "downward"/"upward" method:
554 char a; char b; char c;
555 } s = {'a', 'b', 'c'};
557 Big endian: s = {X, 'a', 'b', 'c'}
558 Little endian: s = {'a', 'b', 'c', X}
560 Where X is a hole. */
562 if (gdbarch_byte_order(gdbarch) == BFD_ENDIAN_BIG
563 && (typecode == TYPE_CODE_STRUCT
564 || typecode == TYPE_CODE_UNION)
565 && argreg > SCORE_LAST_ARG_REGNUM
566 && arglen < SCORE_REGSIZE)
567 downward_offset += (SCORE_REGSIZE - arglen);
571 int partial_len = arglen < SCORE_REGSIZE ? arglen : SCORE_REGSIZE;
572 ULONGEST regval = extract_unsigned_integer (val, partial_len,
575 /* The last part of a arg should shift left when
576 gdbarch_byte_order is BFD_ENDIAN_BIG. */
577 if (byte_order == BFD_ENDIAN_BIG
578 && arg_last_part_p == 1
579 && (typecode == TYPE_CODE_STRUCT
580 || typecode == TYPE_CODE_UNION))
581 regval <<= ((SCORE_REGSIZE - partial_len) * TARGET_CHAR_BIT);
583 /* Always increase the stack_offset and save args to stack. */
584 addr = sp + stack_offset + downward_offset;
585 write_memory (addr, val, partial_len);
587 if (argreg <= SCORE_LAST_ARG_REGNUM)
589 regcache_cooked_write_unsigned (regcache, argreg++, regval);
590 if (arglen > SCORE_REGSIZE && arglen < SCORE_REGSIZE * 2)
595 arglen -= partial_len;
596 stack_offset += align_up (partial_len, SCORE_REGSIZE);
600 /* Step 5, Save SP. */
601 regcache_cooked_write_unsigned (regcache, SCORE_SP_REGNUM, sp);
607 score7_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
610 int iscan = 32, stack_sub = 0;
613 inst_t *inst = score7_fetch_inst (gdbarch, cpc, NULL);
616 if ((inst->len == 4) && !stack_sub
617 && (G_FLD (inst->v, 29, 25) == 0x1
618 && G_FLD (inst->v, 24, 20) == 0x0))
620 /* addi r0, offset */
621 stack_sub = cpc + SCORE_INSTLEN;
622 pc = cpc + SCORE_INSTLEN;
624 else if ((inst->len == 4)
625 && (G_FLD (inst->v, 29, 25) == 0x0)
626 && (G_FLD (inst->v, 24, 20) == 0x2)
627 && (G_FLD (inst->v, 19, 15) == 0x0)
628 && (G_FLD (inst->v, 14, 10) == 0xF)
629 && (G_FLD (inst->v, 9, 0) == 0x56))
632 pc = cpc + SCORE_INSTLEN;
635 else if ((inst->len == 2)
636 && (G_FLD (inst->v, 14, 12) == 0x0)
637 && (G_FLD (inst->v, 11, 8) == 0x2)
638 && (G_FLD (inst->v, 7, 4) == 0x0)
639 && (G_FLD (inst->v, 3, 0) == 0x3))
642 pc = cpc + SCORE16_INSTLEN;
645 else if ((inst->len == 2)
646 && ((G_FLD (inst->v, 14, 12) == 3) /* j15 form */
647 || (G_FLD (inst->v, 14, 12) == 4) /* b15 form */
648 || (G_FLD (inst->v, 14, 12) == 0x0
649 && G_FLD (inst->v, 3, 0) == 0x4))) /* br! */
651 else if ((inst->len == 4)
652 && ((G_FLD (inst->v, 29, 25) == 2) /* j32 form */
653 || (G_FLD (inst->v, 29, 25) == 4) /* b32 form */
654 || (G_FLD (inst->v, 29, 25) == 0x0
655 && G_FLD (inst->v, 6, 1) == 0x4))) /* br */
658 cpc += (inst->len == 2) ? SCORE16_INSTLEN : SCORE_INSTLEN;
664 score3_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
667 int iscan = 32, stack_sub = 0;
671 = score3_adjust_pc_and_fetch_inst (&cpc, NULL,
672 gdbarch_byte_order (gdbarch));
676 if (inst->len == 4 && !stack_sub
677 && (G_FLD (inst->v, 29, 25) == 0x1)
678 && (G_FLD (inst->v, 19, 17) == 0x0)
679 && (G_FLD (inst->v, 24, 20) == 0x0))
681 /* addi r0, offset */
682 stack_sub = cpc + inst->len;
683 pc = cpc + inst->len;
685 else if (inst->len == 4
686 && (G_FLD (inst->v, 29, 25) == 0x0)
687 && (G_FLD (inst->v, 24, 20) == 0x2)
688 && (G_FLD (inst->v, 19, 15) == 0x0)
689 && (G_FLD (inst->v, 14, 10) == 0xF)
690 && (G_FLD (inst->v, 9, 0) == 0x56))
693 pc = cpc + inst->len;
696 else if ((inst->len == 2)
697 && (G_FLD (inst->v, 14, 10) == 0x10)
698 && (G_FLD (inst->v, 9, 5) == 0x2)
699 && (G_FLD (inst->v, 4, 0) == 0x0))
702 pc = cpc + inst->len;
705 else if (inst->len == 2
706 && ((G_FLD (inst->v, 14, 12) == 3) /* b15 form */
707 || (G_FLD (inst->v, 14, 12) == 0x0
708 && G_FLD (inst->v, 11, 5) == 0x4))) /* br! */
710 else if (inst->len == 4
711 && ((G_FLD (inst->v, 29, 25) == 2) /* j32 form */
712 || (G_FLD (inst->v, 29, 25) == 4))) /* b32 form */
720 /* Implement the stack_frame_destroyed_p gdbarch method. */
723 score7_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR cur_pc)
725 inst_t *inst = score7_fetch_inst (gdbarch, cur_pc, NULL);
728 return 1; /* mv! r0, r2 */
729 else if (G_FLD (inst->v, 14, 12) == 0x2
730 && G_FLD (inst->v, 3, 0) == 0xa)
732 else if (G_FLD (inst->v, 14, 12) == 0x0
733 && G_FLD (inst->v, 7, 0) == 0x34)
734 return 1; /* br! r3 */
735 else if (G_FLD (inst->v, 29, 15) == 0x2
736 && G_FLD (inst->v, 6, 1) == 0x2b)
737 return 1; /* mv r0, r2 */
738 else if (G_FLD (inst->v, 29, 25) == 0x0
739 && G_FLD (inst->v, 6, 1) == 0x4
740 && G_FLD (inst->v, 19, 15) == 0x3)
741 return 1; /* br r3 */
746 /* Implement the stack_frame_destroyed_p gdbarch method. */
749 score3_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR cur_pc)
751 CORE_ADDR pc = cur_pc;
753 = score3_adjust_pc_and_fetch_inst (&pc, NULL,
754 gdbarch_byte_order (gdbarch));
757 && (G_FLD (inst->v, 14, 10) == 0x10)
758 && (G_FLD (inst->v, 9, 5) == 0x0)
759 && (G_FLD (inst->v, 4, 0) == 0x2))
760 return 1; /* mv! r0, r2 */
761 else if (inst->len == 4
762 && (G_FLD (inst->v, 29, 25) == 0x0)
763 && (G_FLD (inst->v, 24, 20) == 0x2)
764 && (G_FLD (inst->v, 19, 15) == 0x0)
765 && (G_FLD (inst->v, 14, 10) == 0xF)
766 && (G_FLD (inst->v, 9, 0) == 0x56))
767 return 1; /* mv r0, r2 */
768 else if (inst->len == 2
769 && (G_FLD (inst->v, 14, 12) == 0x0)
770 && (G_FLD (inst->v, 11, 5) == 0x2))
772 else if (inst->len == 2
773 && (G_FLD (inst->v, 14, 12) == 0x0)
774 && (G_FLD (inst->v, 11, 7) == 0x0)
775 && (G_FLD (inst->v, 6, 5) == 0x2))
776 return 1; /* rpop! */
777 else if (inst->len == 2
778 && (G_FLD (inst->v, 14, 12) == 0x0)
779 && (G_FLD (inst->v, 11, 5) == 0x4)
780 && (G_FLD (inst->v, 4, 0) == 0x3))
781 return 1; /* br! r3 */
782 else if (inst->len == 4
783 && (G_FLD (inst->v, 29, 25) == 0x0)
784 && (G_FLD (inst->v, 24, 20) == 0x0)
785 && (G_FLD (inst->v, 19, 15) == 0x3)
786 && (G_FLD (inst->v, 14, 10) == 0xF)
787 && (G_FLD (inst->v, 9, 0) == 0x8))
788 return 1; /* br r3 */
794 score7_malloc_and_get_memblock (CORE_ADDR addr, CORE_ADDR size)
797 gdb_byte *memblock = NULL;
802 memblock = (gdb_byte *) xmalloc (size);
803 memset (memblock, 0, size);
804 ret = target_read_memory (addr & ~0x3, memblock, size);
807 error (_("Error: target_read_memory in file:%s, line:%d!"),
815 score7_free_memblock (gdb_byte *memblock)
821 score7_adjust_memblock_ptr (gdb_byte **memblock, CORE_ADDR prev_pc,
826 /* First time call this function, do nothing. */
828 else if (cur_pc - prev_pc == 2 && (cur_pc & 0x3) == 0)
830 /* First 16-bit instruction, then 32-bit instruction. */
831 *memblock += SCORE_INSTLEN;
833 else if (cur_pc - prev_pc == 4)
835 /* Is 32-bit instruction, increase MEMBLOCK by 4. */
836 *memblock += SCORE_INSTLEN;
841 score7_analyze_prologue (CORE_ADDR startaddr, CORE_ADDR pc,
842 struct frame_info *this_frame,
843 struct score_frame_cache *this_cache)
845 struct gdbarch *gdbarch = get_frame_arch (this_frame);
848 CORE_ADDR cur_pc = startaddr;
857 gdb_byte *memblock = NULL;
858 gdb_byte *memblock_ptr = NULL;
859 CORE_ADDR prev_pc = -1;
861 /* Allocate MEMBLOCK if PC - STARTADDR > 0. */
862 memblock_ptr = memblock =
863 score7_malloc_and_get_memblock (startaddr, pc - startaddr);
865 sp = get_frame_register_unsigned (this_frame, SCORE_SP_REGNUM);
866 fp = get_frame_register_unsigned (this_frame, SCORE_FP_REGNUM);
868 for (; cur_pc < pc; prev_pc = cur_pc, cur_pc += inst_len)
871 if (memblock != NULL)
873 /* Reading memory block from target succefully and got all
874 the instructions(from STARTADDR to PC) needed. */
875 score7_adjust_memblock_ptr (&memblock, prev_pc, cur_pc);
876 inst = score7_fetch_inst (gdbarch, cur_pc, memblock);
880 /* Otherwise, we fetch 4 bytes from target, and GDB also
882 inst = score7_fetch_inst (gdbarch, cur_pc, NULL);
885 /* FIXME: make a full-power prologue analyzer. */
888 inst_len = SCORE16_INSTLEN;
890 if (G_FLD (inst->v, 14, 12) == 0x2
891 && G_FLD (inst->v, 3, 0) == 0xe)
896 if (G_FLD (inst->v, 11, 7) == 0x6
900 ra_offset = sp_offset;
903 else if (G_FLD (inst->v, 11, 7) == 0x4
907 fp_offset = sp_offset;
911 else if (G_FLD (inst->v, 14, 12) == 0x2
912 && G_FLD (inst->v, 3, 0) == 0xa)
917 else if (G_FLD (inst->v, 14, 7) == 0xc1
918 && G_FLD (inst->v, 2, 0) == 0x0)
921 sp_offset += (int) pow (2, G_FLD (inst->v, 6, 3));
923 else if (G_FLD (inst->v, 14, 7) == 0xc0
924 && G_FLD (inst->v, 2, 0) == 0x0)
927 sp_offset -= (int) pow (2, G_FLD (inst->v, 6, 3));
932 inst_len = SCORE_INSTLEN;
934 if (G_FLD(inst->v, 29, 25) == 0x3
935 && G_FLD(inst->v, 2, 0) == 0x4
936 && G_FLD(inst->v, 19, 15) == 0)
938 /* sw rD, [r0, offset]+ */
939 sp_offset += SCORE_INSTLEN;
941 if (G_FLD(inst->v, 24, 20) == 0x3)
944 if (ra_offset_p == 0)
946 ra_offset = sp_offset;
950 else if (G_FLD(inst->v, 24, 20) == 0x2)
953 if (fp_offset_p == 0)
955 fp_offset = sp_offset;
960 else if (G_FLD(inst->v, 29, 25) == 0x14
961 && G_FLD(inst->v, 19,15) == 0)
963 /* sw rD, [r0, offset] */
964 if (G_FLD(inst->v, 24, 20) == 0x3)
967 ra_offset = sp_offset - G_FLD(inst->v, 14, 0);
970 else if (G_FLD(inst->v, 24, 20) == 0x2)
973 fp_offset = sp_offset - G_FLD(inst->v, 14, 0);
977 else if (G_FLD (inst->v, 29, 15) == 0x1c60
978 && G_FLD (inst->v, 2, 0) == 0x0)
980 /* lw r3, [r0]+, 4 */
981 sp_offset -= SCORE_INSTLEN;
984 else if (G_FLD (inst->v, 29, 15) == 0x1c40
985 && G_FLD (inst->v, 2, 0) == 0x0)
987 /* lw r2, [r0]+, 4 */
988 sp_offset -= SCORE_INSTLEN;
992 else if (G_FLD (inst->v, 29, 17) == 0x100
993 && G_FLD (inst->v, 0, 0) == 0x0)
995 /* addi r0, -offset */
996 sp_offset += 65536 - G_FLD (inst->v, 16, 1);
998 else if (G_FLD (inst->v, 29, 17) == 0x110
999 && G_FLD (inst->v, 0, 0) == 0x0)
1001 /* addi r2, offset */
1002 if (pc - cur_pc > 4)
1004 unsigned int save_v = inst->v;
1006 score7_fetch_inst (gdbarch, cur_pc + SCORE_INSTLEN, NULL);
1007 if (inst2->v == 0x23)
1010 sp_offset -= G_FLD (save_v, 16, 1);
1018 if (ra_offset_p == 1)
1020 if (this_cache->saved_regs[SCORE_PC_REGNUM].addr == -1)
1021 this_cache->saved_regs[SCORE_PC_REGNUM].addr =
1022 sp + sp_offset - ra_offset;
1026 this_cache->saved_regs[SCORE_PC_REGNUM] =
1027 this_cache->saved_regs[SCORE_RA_REGNUM];
1031 if (fp_offset_p == 1)
1033 if (this_cache->saved_regs[SCORE_FP_REGNUM].addr == -1)
1034 this_cache->saved_regs[SCORE_FP_REGNUM].addr =
1035 sp + sp_offset - fp_offset;
1038 /* Save SP and FP. */
1039 this_cache->base = sp + sp_offset;
1040 this_cache->fp = fp;
1042 /* Don't forget to free MEMBLOCK if we allocated it. */
1043 if (memblock_ptr != NULL)
1044 score7_free_memblock (memblock_ptr);
1048 score3_analyze_prologue (CORE_ADDR startaddr, CORE_ADDR pc,
1049 struct frame_info *this_frame,
1050 struct score_frame_cache *this_cache)
1054 CORE_ADDR cur_pc = startaddr;
1055 enum bfd_endian byte_order
1056 = gdbarch_byte_order (get_frame_arch (this_frame));
1061 int ra_offset_p = 0;
1062 int fp_offset_p = 0;
1065 sp = get_frame_register_unsigned (this_frame, SCORE_SP_REGNUM);
1066 fp = get_frame_register_unsigned (this_frame, SCORE_FP_REGNUM);
1068 for (; cur_pc < pc; cur_pc += inst_len)
1070 inst_t *inst = NULL;
1072 inst = score3_adjust_pc_and_fetch_inst (&cur_pc, &inst_len, byte_order);
1074 /* FIXME: make a full-power prologue analyzer. */
1077 if (G_FLD (inst->v, 14, 12) == 0x0
1078 && G_FLD (inst->v, 11, 7) == 0x0
1079 && G_FLD (inst->v, 6, 5) == 0x3)
1084 if (G_FLD (inst->v, 4, 0) == 0x3
1085 && ra_offset_p == 0)
1087 /* push! r3, [r0] */
1088 ra_offset = sp_offset;
1091 else if (G_FLD (inst->v, 4, 0) == 0x2
1092 && fp_offset_p == 0)
1094 /* push! r2, [r0] */
1095 fp_offset = sp_offset;
1099 else if (G_FLD (inst->v, 14, 12) == 0x6
1100 && G_FLD (inst->v, 11, 10) == 0x3)
1103 int start_r = G_FLD (inst->v, 9, 5);
1104 int cnt = G_FLD (inst->v, 4, 0);
1106 if ((ra_offset_p == 0)
1107 && (start_r <= SCORE_RA_REGNUM)
1108 && (SCORE_RA_REGNUM < start_r + cnt))
1110 /* rpush! contains r3 */
1112 ra_offset = sp_offset + 4 * (SCORE_RA_REGNUM - start_r) + 4;
1115 if ((fp_offset_p == 0)
1116 && (start_r <= SCORE_FP_REGNUM)
1117 && (SCORE_FP_REGNUM < start_r + cnt))
1119 /* rpush! contains r2 */
1121 fp_offset = sp_offset + 4 * (SCORE_FP_REGNUM - start_r) + 4;
1124 sp_offset += 4 * cnt;
1126 else if (G_FLD (inst->v, 14, 12) == 0x0
1127 && G_FLD (inst->v, 11, 7) == 0x0
1128 && G_FLD (inst->v, 6, 5) == 0x2)
1133 else if (G_FLD (inst->v, 14, 12) == 0x6
1134 && G_FLD (inst->v, 11, 10) == 0x2)
1137 sp_offset -= 4 * G_FLD (inst->v, 4, 0);
1139 else if (G_FLD (inst->v, 14, 12) == 0x5
1140 && G_FLD (inst->v, 11, 10) == 0x3
1141 && G_FLD (inst->v, 9, 6) == 0x0)
1143 /* addi! r0, -offset */
1144 int imm = G_FLD (inst->v, 5, 0);
1146 imm = -(0x3F - imm + 1);
1149 else if (G_FLD (inst->v, 14, 12) == 0x5
1150 && G_FLD (inst->v, 11, 10) == 0x3
1151 && G_FLD (inst->v, 9, 6) == 0x2)
1153 /* addi! r2, offset */
1154 if (pc - cur_pc >= 2)
1158 cur_pc += inst->len;
1159 inst2 = score3_adjust_pc_and_fetch_inst (&cur_pc, NULL,
1163 && G_FLD (inst2->v, 14, 10) == 0x10
1164 && G_FLD (inst2->v, 9, 5) == 0x0
1165 && G_FLD (inst2->v, 4, 0) == 0x2)
1168 int imm = G_FLD (inst->v, 5, 0);
1170 imm = -(0x3F - imm + 1);
1176 else if (inst->len == 4)
1178 if (G_FLD (inst->v, 29, 25) == 0x3
1179 && G_FLD (inst->v, 2, 0) == 0x4
1180 && G_FLD (inst->v, 24, 20) == 0x3
1181 && G_FLD (inst->v, 19, 15) == 0x0)
1183 /* sw r3, [r0, offset]+ */
1184 sp_offset += inst->len;
1185 if (ra_offset_p == 0)
1187 ra_offset = sp_offset;
1191 else if (G_FLD (inst->v, 29, 25) == 0x3
1192 && G_FLD (inst->v, 2, 0) == 0x4
1193 && G_FLD (inst->v, 24, 20) == 0x2
1194 && G_FLD (inst->v, 19, 15) == 0x0)
1196 /* sw r2, [r0, offset]+ */
1197 sp_offset += inst->len;
1198 if (fp_offset_p == 0)
1200 fp_offset = sp_offset;
1204 else if (G_FLD (inst->v, 29, 25) == 0x7
1205 && G_FLD (inst->v, 2, 0) == 0x0
1206 && G_FLD (inst->v, 24, 20) == 0x3
1207 && G_FLD (inst->v, 19, 15) == 0x0)
1209 /* lw r3, [r0]+, 4 */
1210 sp_offset -= inst->len;
1213 else if (G_FLD (inst->v, 29, 25) == 0x7
1214 && G_FLD (inst->v, 2, 0) == 0x0
1215 && G_FLD (inst->v, 24, 20) == 0x2
1216 && G_FLD (inst->v, 19, 15) == 0x0)
1218 /* lw r2, [r0]+, 4 */
1219 sp_offset -= inst->len;
1222 else if (G_FLD (inst->v, 29, 25) == 0x1
1223 && G_FLD (inst->v, 19, 17) == 0x0
1224 && G_FLD (inst->v, 24, 20) == 0x0
1225 && G_FLD (inst->v, 0, 0) == 0x0)
1227 /* addi r0, -offset */
1228 int imm = G_FLD (inst->v, 16, 1);
1230 imm = -(0xFFFF - imm + 1);
1233 else if (G_FLD (inst->v, 29, 25) == 0x1
1234 && G_FLD (inst->v, 19, 17) == 0x0
1235 && G_FLD (inst->v, 24, 20) == 0x2
1236 && G_FLD (inst->v, 0, 0) == 0x0)
1238 /* addi r2, offset */
1239 if (pc - cur_pc >= 2)
1243 cur_pc += inst->len;
1244 inst2 = score3_adjust_pc_and_fetch_inst (&cur_pc, NULL,
1248 && G_FLD (inst2->v, 14, 10) == 0x10
1249 && G_FLD (inst2->v, 9, 5) == 0x0
1250 && G_FLD (inst2->v, 4, 0) == 0x2)
1253 int imm = G_FLD (inst->v, 16, 1);
1255 imm = -(0xFFFF - imm + 1);
1264 if (ra_offset_p == 1)
1266 if (this_cache->saved_regs[SCORE_PC_REGNUM].addr == -1)
1267 this_cache->saved_regs[SCORE_PC_REGNUM].addr =
1268 sp + sp_offset - ra_offset;
1272 this_cache->saved_regs[SCORE_PC_REGNUM] =
1273 this_cache->saved_regs[SCORE_RA_REGNUM];
1277 if (fp_offset_p == 1)
1279 if (this_cache->saved_regs[SCORE_FP_REGNUM].addr == -1)
1280 this_cache->saved_regs[SCORE_FP_REGNUM].addr =
1281 sp + sp_offset - fp_offset;
1284 /* Save SP and FP. */
1285 this_cache->base = sp + sp_offset;
1286 this_cache->fp = fp;
1289 static struct score_frame_cache *
1290 score_make_prologue_cache (struct frame_info *this_frame, void **this_cache)
1292 struct score_frame_cache *cache;
1294 if ((*this_cache) != NULL)
1295 return (struct score_frame_cache *) (*this_cache);
1297 cache = FRAME_OBSTACK_ZALLOC (struct score_frame_cache);
1298 (*this_cache) = cache;
1299 cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);
1301 /* Analyze the prologue. */
1303 const CORE_ADDR pc = get_frame_pc (this_frame);
1304 CORE_ADDR start_addr;
1306 find_pc_partial_function (pc, NULL, &start_addr, NULL);
1307 if (start_addr == 0)
1310 if (target_mach == bfd_mach_score3)
1311 score3_analyze_prologue (start_addr, pc, this_frame,
1312 (struct score_frame_cache *) *this_cache);
1314 score7_analyze_prologue (start_addr, pc, this_frame,
1315 (struct score_frame_cache *) *this_cache);
1319 trad_frame_set_value (cache->saved_regs, SCORE_SP_REGNUM, cache->base);
1321 return (struct score_frame_cache *) (*this_cache);
1325 score_prologue_this_id (struct frame_info *this_frame, void **this_cache,
1326 struct frame_id *this_id)
1328 struct score_frame_cache *info = score_make_prologue_cache (this_frame,
1330 (*this_id) = frame_id_build (info->base, get_frame_func (this_frame));
1333 static struct value *
1334 score_prologue_prev_register (struct frame_info *this_frame,
1335 void **this_cache, int regnum)
1337 struct score_frame_cache *info = score_make_prologue_cache (this_frame,
1339 return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
1342 static const struct frame_unwind score_prologue_unwind =
1345 default_frame_unwind_stop_reason,
1346 score_prologue_this_id,
1347 score_prologue_prev_register,
1349 default_frame_sniffer,
1354 score_prologue_frame_base_address (struct frame_info *this_frame,
1357 struct score_frame_cache *info =
1358 score_make_prologue_cache (this_frame, this_cache);
1362 static const struct frame_base score_prologue_frame_base =
1364 &score_prologue_unwind,
1365 score_prologue_frame_base_address,
1366 score_prologue_frame_base_address,
1367 score_prologue_frame_base_address,
1370 static const struct frame_base *
1371 score_prologue_frame_base_sniffer (struct frame_info *this_frame)
1373 return &score_prologue_frame_base;
1376 /* Core file support. */
1378 static const struct regcache_map_entry score7_linux_gregmap[] =
1380 /* FIXME: According to the current Linux kernel, r0 is preceded by
1381 9 rather than 7 words. */
1382 { 7, REGCACHE_MAP_SKIP, 4 },
1383 { 32, 0, 4 }, /* r0 ... r31 */
1384 { 1, 55, 4 }, /* CEL */
1385 { 1, 54, 4 }, /* CEH */
1386 { 1, 53, 4 }, /* sr0, i.e. cnt or COUNTER */
1387 { 1, 52, 4 }, /* sr1, i.e. lcr or LDCR */
1388 { 1, 51, 4 }, /* sr2, i.e. scr or STCR */
1389 { 1, 49, 4 }, /* PC (same slot as EPC) */
1390 { 1, 38, 4 }, /* EMA */
1391 { 1, 32, 4 }, /* PSR */
1392 { 1, 34, 4 }, /* ECR */
1393 { 1, 33, 4 }, /* COND */
1397 #define SCORE7_LINUX_EPC_OFFSET (44 * 4)
1398 #define SCORE7_LINUX_SIZEOF_GREGSET (49 * 4)
1401 score7_linux_supply_gregset(const struct regset *regset,
1402 struct regcache *regcache,
1403 int regnum, const void *buf,
1406 regcache_supply_regset (regset, regcache, regnum, buf, size);
1408 /* Supply the EPC from the same slot as the PC. Note that the
1409 collect function will store the PC in that slot. */
1410 if ((regnum == -1 || regnum == SCORE_EPC_REGNUM)
1411 && size >= SCORE7_LINUX_EPC_OFFSET + 4)
1412 regcache->raw_supply
1413 (SCORE_EPC_REGNUM, (const gdb_byte *) buf + SCORE7_LINUX_EPC_OFFSET);
1416 static const struct regset score7_linux_gregset =
1418 score7_linux_gregmap,
1419 score7_linux_supply_gregset,
1420 regcache_collect_regset
1423 /* Iterate over core file register note sections. */
1426 score7_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
1427 iterate_over_regset_sections_cb *cb,
1429 const struct regcache *regcache)
1431 cb (".reg", SCORE7_LINUX_SIZEOF_GREGSET, SCORE7_LINUX_SIZEOF_GREGSET,
1432 &score7_linux_gregset, NULL, cb_data);
1435 static struct gdbarch *
1436 score_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
1438 struct gdbarch *gdbarch;
1439 target_mach = info.bfd_arch_info->mach;
1441 arches = gdbarch_list_lookup_by_info (arches, &info);
1444 return (arches->gdbarch);
1446 gdbarch = gdbarch_alloc (&info, NULL);
1448 set_gdbarch_short_bit (gdbarch, 16);
1449 set_gdbarch_int_bit (gdbarch, 32);
1450 set_gdbarch_float_bit (gdbarch, 32);
1451 set_gdbarch_double_bit (gdbarch, 64);
1452 set_gdbarch_long_double_bit (gdbarch, 64);
1454 set_gdbarch_register_sim_regno (gdbarch, score_register_sim_regno);
1456 set_gdbarch_pc_regnum (gdbarch, SCORE_PC_REGNUM);
1457 set_gdbarch_sp_regnum (gdbarch, SCORE_SP_REGNUM);
1458 set_gdbarch_adjust_breakpoint_address (gdbarch,
1459 score_adjust_breakpoint_address);
1460 set_gdbarch_register_type (gdbarch, score_register_type);
1461 set_gdbarch_frame_align (gdbarch, score_frame_align);
1462 set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
1464 switch (target_mach)
1466 case bfd_mach_score7:
1467 set_gdbarch_breakpoint_kind_from_pc (gdbarch,
1468 score7_breakpoint_kind_from_pc);
1469 set_gdbarch_sw_breakpoint_from_kind (gdbarch,
1470 score7_sw_breakpoint_from_kind);
1471 set_gdbarch_skip_prologue (gdbarch, score7_skip_prologue);
1472 set_gdbarch_stack_frame_destroyed_p (gdbarch,
1473 score7_stack_frame_destroyed_p);
1474 set_gdbarch_register_name (gdbarch, score7_register_name);
1475 set_gdbarch_num_regs (gdbarch, SCORE7_NUM_REGS);
1476 /* Core file support. */
1477 set_gdbarch_iterate_over_regset_sections
1478 (gdbarch, score7_linux_iterate_over_regset_sections);
1481 case bfd_mach_score3:
1482 set_gdbarch_breakpoint_kind_from_pc (gdbarch,
1483 score3_breakpoint_kind_from_pc);
1484 set_gdbarch_sw_breakpoint_from_kind (gdbarch,
1485 score3_sw_breakpoint_from_kind);
1486 set_gdbarch_skip_prologue (gdbarch, score3_skip_prologue);
1487 set_gdbarch_stack_frame_destroyed_p (gdbarch,
1488 score3_stack_frame_destroyed_p);
1489 set_gdbarch_register_name (gdbarch, score3_register_name);
1490 set_gdbarch_num_regs (gdbarch, SCORE3_NUM_REGS);
1494 /* Watchpoint hooks. */
1495 set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);
1497 /* Dummy frame hooks. */
1498 set_gdbarch_return_value (gdbarch, score_return_value);
1499 set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
1500 set_gdbarch_push_dummy_call (gdbarch, score_push_dummy_call);
1502 /* Normal frame hooks. */
1503 dwarf2_append_unwinders (gdbarch);
1504 frame_base_append_sniffer (gdbarch, dwarf2_frame_base_sniffer);
1505 frame_unwind_append_unwinder (gdbarch, &score_prologue_unwind);
1506 frame_base_append_sniffer (gdbarch, score_prologue_frame_base_sniffer);
1512 _initialize_score_tdep (void)
1514 gdbarch_register (bfd_arch_score, score_gdbarch_init, NULL);