gdb/score-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for the S+core architecture, for GDB,
  2.    the GNU Debugger.

  3.    Copyright (C) 2006-2015 Free Software Foundation, Inc.

  4.    Contributed by Qinwei (qinwei@sunnorth.com.cn)
  5.    Contributed by Ching-Peng Lin (cplin@sunplus.com)

  6.    This file is part of GDB.

  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 3 of the License, or
  10.    (at your option) any later version.

  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.

  15.    You should have received a copy of the GNU General Public License
  16.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  17. #include "defs.h"
  18. #include "inferior.h"
  19. #include "symtab.h"
  20. #include "objfiles.h"
  21. #include "gdbcore.h"
  22. #include "target.h"
  23. #include "arch-utils.h"
  24. #include "regcache.h"
  25. #include "regset.h"
  26. #include "dis-asm.h"
  27. #include "frame-unwind.h"
  28. #include "frame-base.h"
  29. #include "trad-frame.h"
  30. #include "dwarf2-frame.h"
  31. #include "score-tdep.h"

  32. #define G_FLD(_i,_ms,_ls) \
  33.     ((unsigned)((_i) << (31 - (_ms))) >> (31 - (_ms) + (_ls)))

  34. typedef struct{
  35.   unsigned long long v;
  36.   unsigned long long raw;
  37.   unsigned int len;
  38. }inst_t;

  39. struct score_frame_cache
  40. {
  41.   CORE_ADDR base;
  42.   CORE_ADDR fp;
  43.   struct trad_frame_saved_reg *saved_regs;
  44. };

  45. static int target_mach = bfd_mach_score7;

  46. static struct type *
  47. score_register_type (struct gdbarch *gdbarch, int regnum)
  48. {
  49.   gdb_assert (regnum >= 0
  50.               && regnum < ((target_mach == bfd_mach_score7)
  51.                            ? SCORE7_NUM_REGS : SCORE3_NUM_REGS));
  52.   return builtin_type (gdbarch)->builtin_uint32;
  53. }

  54. static CORE_ADDR
  55. score_unwind_sp (struct gdbarch *gdbarch, struct frame_info *next_frame)
  56. {
  57.   return frame_unwind_register_unsigned (next_frame, SCORE_SP_REGNUM);
  58. }

  59. static CORE_ADDR
  60. score_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  61. {
  62.   return frame_unwind_register_unsigned (next_frame, SCORE_PC_REGNUM);
  63. }

  64. static const char *
  65. score7_register_name (struct gdbarch *gdbarch, int regnum)
  66. {
  67.   const char *score_register_names[] = {
  68.     "r0""r1""r2""r3""r4""r5""r6""r7",
  69.     "r8""r9""r10", "r11", "r12", "r13", "r14", "r15",
  70.     "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  71.     "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",

  72.     "PSR",     "COND""ECR",     "EXCPVEC", "CCR",
  73.     "EPC",     "EMA",   "TLBLOCK", "TLBPT",   "PEADDR",
  74.     "TLBRPT""PEVN""PECTX",   "LIMPFN""LDMPFN",
  75.     "PREV",    "DREG""PC",      "DSAVE",   "COUNTER",
  76.     "LDCR",    "STCR""CEH",     "CEL",
  77.   };

  78.   gdb_assert (regnum >= 0 && regnum < SCORE7_NUM_REGS);
  79.   return score_register_names[regnum];
  80. }

  81. static const char *
  82. score3_register_name (struct gdbarch *gdbarch, int regnum)
  83. {
  84.   const char *score_register_names[] = {
  85.     "r0""r1""r2""r3""r4""r5""r6""r7",
  86.     "r8""r9""r10", "r11", "r12", "r13", "r14", "r15",
  87.     "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  88.     "r24", "r25", "r26", "r27", "r28", "r29", "r30", "r31",

  89.     "PSR",      "COND",   "ECR",   "EXCPVEC""CCR",
  90.     "EPC",      "EMA",    "PREV""DREG",     "DSAVE",
  91.     "COUNTER""LDCR",   "STCR""CEH",      "CEL",
  92.     "",         "",       "PC",
  93.   };

  94.   gdb_assert (regnum >= 0 && regnum < SCORE3_NUM_REGS);
  95.   return score_register_names[regnum];
  96. }

  97. #if WITH_SIM
  98. static int
  99. score_register_sim_regno (struct gdbarch *gdbarch, int regnum)
  100. {
  101.   gdb_assert (regnum >= 0
  102.               && regnum < ((target_mach == bfd_mach_score7)
  103.                            ? SCORE7_NUM_REGS : SCORE3_NUM_REGS));
  104.   return regnum;
  105. }
  106. #endif

  107. static int
  108. score_print_insn (bfd_vma memaddr, struct disassemble_info *info)
  109. {
  110.   if (info->endian == BFD_ENDIAN_BIG)
  111.     return print_insn_big_score (memaddr, info);
  112.   else
  113.     return print_insn_little_score (memaddr, info);
  114. }

  115. static inst_t *
  116. score7_fetch_inst (struct gdbarch *gdbarch, CORE_ADDR addr, gdb_byte *memblock)
  117. {
  118.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  119.   static inst_t inst = { 0, 0, 0 };
  120.   gdb_byte buf[SCORE_INSTLEN] = { 0 };
  121.   int big;
  122.   int ret;

  123.   if (target_has_execution && memblock != NULL)
  124.     {
  125.       /* Fetch instruction from local MEMBLOCK.  */
  126.       memcpy (buf, memblock, SCORE_INSTLEN);
  127.     }
  128.   else
  129.     {
  130.       /* Fetch instruction from target.  */
  131.       ret = target_read_memory (addr & ~0x3, buf, SCORE_INSTLEN);
  132.       if (ret)
  133.         {
  134.           error (_("Error: target_read_memory in file:%s, line:%d!"),
  135.                   __FILE__, __LINE__);
  136.           return 0;
  137.         }
  138.     }

  139.   inst.raw = extract_unsigned_integer (buf, SCORE_INSTLEN, byte_order);
  140.   inst.len = (inst.raw & 0x80008000) ? 4 : 2;
  141.   inst.v = ((inst.raw >> 16 & 0x7FFF) << 15) | (inst.raw & 0x7FFF);
  142.   big = (byte_order == BFD_ENDIAN_BIG);
  143.   if (inst.len == 2)
  144.     {
  145.       if (big ^ ((addr & 0x2) == 2))
  146.         inst.v = G_FLD (inst.v, 29, 15);
  147.       else
  148.         inst.v = G_FLD (inst.v, 14, 0);
  149.     }
  150.   return &inst;
  151. }

  152. static inst_t *
  153. score3_adjust_pc_and_fetch_inst (CORE_ADDR *pcptr, int *lenptr,
  154.                                  enum bfd_endian byte_order)
  155. {
  156.   static inst_t inst = { 0, 0, 0 };

  157.   struct breakplace
  158.   {
  159.     int break_offset;
  160.     int inst_len;
  161.   };
  162.   /*     raw        table 1 (column 2, 3, 4)
  163.     *  0  1  0  *   # 2
  164.     *  0  1  1  0   # 3
  165.     0  1  1  0  *   # 6
  166.                     table 2 (column 1, 2, 3)
  167.     *  0  0  *  *   # 0, 4
  168.     0  1  0  *  *   # 2
  169.     1  1  0  *  *   # 6
  170.    */

  171.   static const struct breakplace bk_table[16] =
  172.     {
  173.       /* table 1 */
  174.       {0, 0},
  175.       {0, 0},
  176.       {0, 4},
  177.       {0, 6},
  178.       {0, 0},
  179.       {0, 0},
  180.       {-2, 6},
  181.       {0, 0},
  182.       /* table 2 */
  183.       {0, 2},
  184.       {0, 0},
  185.       {-2, 4},
  186.       {0, 0},
  187.       {0, 2},
  188.       {0, 0},
  189.       {-4, 6},
  190.       {0, 0}
  191.     };

  192. #define EXTRACT_LEN 2
  193.   CORE_ADDR adjust_pc = *pcptr & ~0x1;
  194.   gdb_byte buf[5][EXTRACT_LEN] =
  195.     {
  196.       {'\0', '\0'},
  197.       {'\0', '\0'},
  198.       {'\0', '\0'},
  199.       {'\0', '\0'},
  200.       {'\0', '\0'}
  201.     };
  202.   int ret;
  203.   unsigned int raw;
  204.   unsigned int cbits = 0;
  205.   int bk_index;
  206.   int i, count;

  207.   inst.v = 0;
  208.   inst.raw = 0;
  209.   inst.len = 0;

  210.   adjust_pc -= 4;
  211.   for (i = 0; i < 5; i++)
  212.     {
  213.       ret = target_read_memory (adjust_pc + 2 * i, buf[i], EXTRACT_LEN);
  214.       if (ret != 0)
  215.         {
  216.           buf[i][0] = '\0';
  217.           buf[i][1] = '\0';
  218.           if (i == 2)
  219.             error (_("Error: target_read_memory in file:%s, line:%d!"),
  220.                    __FILE__, __LINE__);
  221.         }

  222.       raw = extract_unsigned_integer (buf[i], EXTRACT_LEN, byte_order);
  223.       cbits = (cbits << 1) | (raw >> 15);
  224.     }
  225.   adjust_pc += 4;

  226.   if (cbits & 0x4)
  227.     {
  228.       /* table 1 */
  229.       cbits = (cbits >> 1) & 0x7;
  230.       bk_index = cbits;
  231.     }
  232.   else
  233.     {
  234.       /* table 2 */
  235.       cbits = (cbits >> 2) & 0x7;
  236.       bk_index = cbits + 8;
  237.     }

  238.   gdb_assert (!((bk_table[bk_index].break_offset == 0)
  239.                 && (bk_table[bk_index].inst_len == 0)));

  240.   inst.len = bk_table[bk_index].inst_len;

  241.   i = (bk_table[bk_index].break_offset + 4) / 2;
  242.   count = inst.len / 2;
  243.   for (; count > 0; i++, count--)
  244.     {
  245.       inst.raw = (inst.raw << 16)
  246.                  | extract_unsigned_integer (buf[i], EXTRACT_LEN, byte_order);
  247.     }

  248.   switch (inst.len)
  249.     {
  250.     case 2:
  251.       inst.v = inst.raw & 0x7FFF;
  252.       break;
  253.     case 4:
  254.       inst.v = ((inst.raw >> 16 & 0x7FFF) << 15) | (inst.raw & 0x7FFF);
  255.       break;
  256.     case 6:
  257.       inst.v = ((inst.raw >> 32 & 0x7FFF) << 30)
  258.                | ((inst.raw >> 16 & 0x7FFF) << 15) | (inst.raw & 0x7FFF);
  259.       break;
  260.     }

  261.   if (pcptr)
  262.     *pcptr = adjust_pc + bk_table[bk_index].break_offset;
  263.   if (lenptr)
  264.     *lenptr = bk_table[bk_index].inst_len;

  265. #undef EXTRACT_LEN

  266.   return &inst;
  267. }

  268. static const gdb_byte *
  269. score7_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
  270.                            int *lenptr)
  271. {
  272.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  273.   gdb_byte buf[SCORE_INSTLEN] = { 0 };
  274.   int ret;
  275.   unsigned int raw;

  276.   if ((ret = target_read_memory (*pcptr & ~0x3, buf, SCORE_INSTLEN)) != 0)
  277.     {
  278.       error (_("Error: target_read_memory in file:%s, line:%d!"),
  279.              __FILE__, __LINE__);
  280.     }
  281.   raw = extract_unsigned_integer (buf, SCORE_INSTLEN, byte_order);

  282.   if (byte_order == BFD_ENDIAN_BIG)
  283.     {
  284.       if (!(raw & 0x80008000))
  285.         {
  286.           /* 16bits instruction.  */
  287.           static gdb_byte big_breakpoint16[] = { 0x60, 0x02 };
  288.           *pcptr &= ~0x1;
  289.           *lenptr = sizeof (big_breakpoint16);
  290.           return big_breakpoint16;
  291.         }
  292.       else
  293.         {
  294.           /* 32bits instruction.  */
  295.           static gdb_byte big_breakpoint32[] = { 0x80, 0x00, 0x80, 0x06 };
  296.           *pcptr &= ~0x3;
  297.           *lenptr = sizeof (big_breakpoint32);
  298.           return big_breakpoint32;
  299.         }
  300.     }
  301.   else
  302.     {
  303.       if (!(raw & 0x80008000))
  304.         {
  305.           /* 16bits instruction.  */
  306.           static gdb_byte little_breakpoint16[] = { 0x02, 0x60 };
  307.           *pcptr &= ~0x1;
  308.           *lenptr = sizeof (little_breakpoint16);
  309.           return little_breakpoint16;
  310.         }
  311.       else
  312.         {
  313.           /* 32bits instruction.  */
  314.           static gdb_byte little_breakpoint32[] = { 0x06, 0x80, 0x00, 0x80 };
  315.           *pcptr &= ~0x3;
  316.           *lenptr = sizeof (little_breakpoint32);
  317.           return little_breakpoint32;
  318.         }
  319.     }
  320. }

  321. static const gdb_byte *
  322. score3_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
  323.                            int *lenptr)
  324. {
  325.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  326.   CORE_ADDR adjust_pc = *pcptr;
  327.   int len;
  328.   static gdb_byte score_break_insns[6][6] = {
  329.     /* The following three instructions are big endian.  */
  330.     { 0x00, 0x20 },
  331.     { 0x80, 0x00, 0x00, 0x06 },
  332.     { 0x80, 0x00, 0x80, 0x00, 0x00, 0x00 },
  333.     /* The following three instructions are little endian.  */
  334.     { 0x20, 0x00 },
  335.     { 0x00, 0x80, 0x06, 0x00 },
  336.     { 0x00, 0x80, 0x00, 0x80, 0x00, 0x00 }};

  337.   gdb_byte *p = NULL;
  338.   int index = 0;

  339.   score3_adjust_pc_and_fetch_inst (&adjust_pc, &len, byte_order);

  340.   index = ((byte_order == BFD_ENDIAN_BIG) ? 0 : 3) + (len / 2 - 1);
  341.   p = score_break_insns[index];

  342.   *pcptr = adjust_pc;
  343.   *lenptr = len;

  344.   return p;
  345. }

  346. static CORE_ADDR
  347. score_adjust_breakpoint_address (struct gdbarch *gdbarch, CORE_ADDR bpaddr)
  348. {
  349.   CORE_ADDR adjust_pc = bpaddr;

  350.   if (target_mach == bfd_mach_score3)
  351.     score3_adjust_pc_and_fetch_inst (&adjust_pc, NULL,
  352.                                          gdbarch_byte_order (gdbarch));
  353.   else
  354.     adjust_pc = align_down (adjust_pc, 2);

  355.   return adjust_pc;
  356. }

  357. static CORE_ADDR
  358. score_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
  359. {
  360.   return align_down (addr, 16);
  361. }

  362. static void
  363. score_xfer_register (struct regcache *regcache, int regnum, int length,
  364.                      enum bfd_endian endian, gdb_byte *readbuf,
  365.                      const gdb_byte *writebuf, int buf_offset)
  366. {
  367.   int reg_offset = 0;
  368.   gdb_assert (regnum >= 0
  369.               && regnum < ((target_mach == bfd_mach_score7)
  370.                            ? SCORE7_NUM_REGS : SCORE3_NUM_REGS));

  371.   switch (endian)
  372.     {
  373.     case BFD_ENDIAN_BIG:
  374.       reg_offset = SCORE_REGSIZE - length;
  375.       break;
  376.     case BFD_ENDIAN_LITTLE:
  377.       reg_offset = 0;
  378.       break;
  379.     case BFD_ENDIAN_UNKNOWN:
  380.       reg_offset = 0;
  381.       break;
  382.     default:
  383.       error (_("Error: score_xfer_register in file:%s, line:%d!"),
  384.              __FILE__, __LINE__);
  385.     }

  386.   if (readbuf != NULL)
  387.     regcache_cooked_read_part (regcache, regnum, reg_offset, length,
  388.                                readbuf + buf_offset);
  389.   if (writebuf != NULL)
  390.     regcache_cooked_write_part (regcache, regnum, reg_offset, length,
  391.                                 writebuf + buf_offset);
  392. }

  393. static enum return_value_convention
  394. score_return_value (struct gdbarch *gdbarch, struct value *function,
  395.                     struct type *type, struct regcache *regcache,
  396.                     gdb_byte * readbuf, const gdb_byte * writebuf)
  397. {
  398.   if (TYPE_CODE (type) == TYPE_CODE_STRUCT
  399.       || TYPE_CODE (type) == TYPE_CODE_UNION
  400.       || TYPE_CODE (type) == TYPE_CODE_ARRAY)
  401.     return RETURN_VALUE_STRUCT_CONVENTION;
  402.   else
  403.     {
  404.       int offset;
  405.       int regnum;
  406.       for (offset = 0, regnum = SCORE_A0_REGNUM;
  407.            offset < TYPE_LENGTH (type);
  408.            offset += SCORE_REGSIZE, regnum++)
  409.         {
  410.           int xfer = SCORE_REGSIZE;

  411.           if (offset + xfer > TYPE_LENGTH (type))
  412.             xfer = TYPE_LENGTH (type) - offset;
  413.           score_xfer_register (regcache, regnum, xfer,
  414.                                gdbarch_byte_order(gdbarch),
  415.                                readbuf, writebuf, offset);
  416.         }
  417.       return RETURN_VALUE_REGISTER_CONVENTION;
  418.     }
  419. }

  420. static struct frame_id
  421. score_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  422. {
  423.   return frame_id_build (get_frame_register_unsigned (this_frame,
  424.                                                       SCORE_SP_REGNUM),
  425.                          get_frame_pc (this_frame));
  426. }

  427. static int
  428. score_type_needs_double_align (struct type *type)
  429. {
  430.   enum type_code typecode = TYPE_CODE (type);

  431.   if ((typecode == TYPE_CODE_INT && TYPE_LENGTH (type) == 8)
  432.       || (typecode == TYPE_CODE_FLT && TYPE_LENGTH (type) == 8))
  433.     return 1;
  434.   else if (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION)
  435.     {
  436.       int i, n;

  437.       n = TYPE_NFIELDS (type);
  438.       for (i = 0; i < n; i++)
  439.         if (score_type_needs_double_align (TYPE_FIELD_TYPE (type, i)))
  440.           return 1;
  441.       return 0;
  442.     }
  443.   return 0;
  444. }

  445. static CORE_ADDR
  446. score_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  447.                        struct regcache *regcache, CORE_ADDR bp_addr,
  448.                        int nargs, struct value **args, CORE_ADDR sp,
  449.                        int struct_return, CORE_ADDR struct_addr)
  450. {
  451.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  452.   int argnum;
  453.   int argreg;
  454.   int arglen = 0;
  455.   CORE_ADDR stack_offset = 0;
  456.   CORE_ADDR addr = 0;

  457.   /* Step 1, Save RA.  */
  458.   regcache_cooked_write_unsigned (regcache, SCORE_RA_REGNUM, bp_addr);

  459.   /* Step 2, Make space on the stack for the args.  */
  460.   struct_addr = align_down (struct_addr, 16);
  461.   sp = align_down (sp, 16);
  462.   for (argnum = 0; argnum < nargs; argnum++)
  463.     arglen += align_up (TYPE_LENGTH (value_type (args[argnum])),
  464.                         SCORE_REGSIZE);
  465.   sp -= align_up (arglen, 16);

  466.   argreg = SCORE_BEGIN_ARG_REGNUM;

  467.   /* Step 3, Check if struct return then save the struct address to
  468.      r4 and increase the stack_offset by 4.  */
  469.   if (struct_return)
  470.     {
  471.       regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
  472.       stack_offset += SCORE_REGSIZE;
  473.     }

  474.   /* Step 4, Load arguments:
  475.      If arg length is too long (> 4 bytes), then split the arg and
  476.      save every parts.  */
  477.   for (argnum = 0; argnum < nargs; argnum++)
  478.     {
  479.       struct value *arg = args[argnum];
  480.       struct type *arg_type = check_typedef (value_type (arg));
  481.       enum type_code typecode = TYPE_CODE (arg_type);
  482.       const gdb_byte *val = value_contents (arg);
  483.       int downward_offset = 0;
  484.       int odd_sized_struct_p;
  485.       int arg_last_part_p = 0;

  486.       arglen = TYPE_LENGTH (arg_type);
  487.       odd_sized_struct_p = (arglen > SCORE_REGSIZE
  488.                             && arglen % SCORE_REGSIZE != 0);

  489.       /* If a arg should be aligned to 8 bytes (long long or double),
  490.          the value should be put to even register numbers.  */
  491.       if (score_type_needs_double_align (arg_type))
  492.         {
  493.           if (argreg & 1)
  494.             argreg++;
  495.         }

  496.       /* If sizeof a block < SCORE_REGSIZE, then Score GCC will chose
  497.          the default "downward"/"upward" method:

  498.          Example:

  499.          struct struc
  500.          {
  501.            char a; char b; char c;
  502.          } s = {'a', 'b', 'c'};

  503.          Big endian:    s = {X, 'a', 'b', 'c'}
  504.          Little endian: s = {'a', 'b', 'c', X}

  505.          Where X is a hole.  */

  506.       if (gdbarch_byte_order(gdbarch) == BFD_ENDIAN_BIG
  507.           && (typecode == TYPE_CODE_STRUCT
  508.               || typecode == TYPE_CODE_UNION)
  509.           && argreg > SCORE_LAST_ARG_REGNUM
  510.           && arglen < SCORE_REGSIZE)
  511.         downward_offset += (SCORE_REGSIZE - arglen);

  512.       while (arglen > 0)
  513.         {
  514.           int partial_len = arglen < SCORE_REGSIZE ? arglen : SCORE_REGSIZE;
  515.           ULONGEST regval = extract_unsigned_integer (val, partial_len,
  516.                                                         byte_order);

  517.           /* The last part of a arg should shift left when
  518.              gdbarch_byte_order is BFD_ENDIAN_BIG.  */
  519.           if (byte_order == BFD_ENDIAN_BIG
  520.               && arg_last_part_p == 1
  521.               && (typecode == TYPE_CODE_STRUCT
  522.                   || typecode == TYPE_CODE_UNION))
  523.             regval <<= ((SCORE_REGSIZE - partial_len) * TARGET_CHAR_BIT);

  524.           /* Always increase the stack_offset and save args to stack.  */
  525.           addr = sp + stack_offset + downward_offset;
  526.           write_memory (addr, val, partial_len);

  527.           if (argreg <= SCORE_LAST_ARG_REGNUM)
  528.             {
  529.               regcache_cooked_write_unsigned (regcache, argreg++, regval);
  530.               if (arglen > SCORE_REGSIZE && arglen < SCORE_REGSIZE * 2)
  531.                 arg_last_part_p = 1;
  532.             }

  533.           val += partial_len;
  534.           arglen -= partial_len;
  535.           stack_offset += align_up (partial_len, SCORE_REGSIZE);
  536.         }
  537.     }

  538.   /* Step 5, Save SP.  */
  539.   regcache_cooked_write_unsigned (regcache, SCORE_SP_REGNUM, sp);

  540.   return sp;
  541. }

  542. static CORE_ADDR
  543. score7_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  544. {
  545.   CORE_ADDR cpc = pc;
  546.   int iscan = 32, stack_sub = 0;
  547.   while (iscan-- > 0)
  548.     {
  549.       inst_t *inst = score7_fetch_inst (gdbarch, cpc, NULL);
  550.       if (!inst)
  551.         break;
  552.       if ((inst->len == 4) && !stack_sub
  553.           && (G_FLD (inst->v, 29, 25) == 0x1
  554.               && G_FLD (inst->v, 24, 20) == 0x0))
  555.         {
  556.           /* addi r0, offset */
  557.           stack_sub = cpc + SCORE_INSTLEN;
  558.           pc = cpc + SCORE_INSTLEN;
  559.         }
  560.       else if ((inst->len == 4)
  561.                && (G_FLD (inst->v, 29, 25) == 0x0)
  562.                && (G_FLD (inst->v, 24, 20) == 0x2)
  563.                && (G_FLD (inst->v, 19, 15) == 0x0)
  564.                && (G_FLD (inst->v, 14, 10) == 0xF)
  565.                && (G_FLD (inst->v, 9, 0) == 0x56))
  566.         {
  567.           /* mv r2, r0  */
  568.           pc = cpc + SCORE_INSTLEN;
  569.           break;
  570.         }
  571.       else if ((inst->len == 2)
  572.                && (G_FLD (inst->v, 14, 12) == 0x0)
  573.                && (G_FLD (inst->v, 11, 8) == 0x2)
  574.                && (G_FLD (inst->v, 7, 4) == 0x0)
  575.                && (G_FLD (inst->v, 3, 0) == 0x3))
  576.         {
  577.           /* mv! r2, r0 */
  578.           pc = cpc + SCORE16_INSTLEN;
  579.           break;
  580.         }
  581.       else if ((inst->len == 2)
  582.                && ((G_FLD (inst->v, 14, 12) == 3)    /* j15 form */
  583.                    || (G_FLD (inst->v, 14, 12) == 4) /* b15 form */
  584.                    || (G_FLD (inst->v, 14, 12) == 0x0
  585.                        && G_FLD (inst->v, 3, 0) == 0x4))) /* br! */
  586.         break;
  587.       else if ((inst->len == 4)
  588.                && ((G_FLD (inst->v, 29, 25) == 2)    /* j32 form */
  589.                    || (G_FLD (inst->v, 29, 25) == 4) /* b32 form */
  590.                    || (G_FLD (inst->v, 29, 25) == 0x0
  591.                        && G_FLD (inst->v, 6, 1) == 0x4)))  /* br */
  592.         break;

  593.       cpc += (inst->len == 2) ? SCORE16_INSTLEN : SCORE_INSTLEN;
  594.     }
  595.   return pc;
  596. }

  597. static CORE_ADDR
  598. score3_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  599. {
  600.   CORE_ADDR cpc = pc;
  601.   int iscan = 32, stack_sub = 0;
  602.   while (iscan-- > 0)
  603.     {
  604.       inst_t *inst
  605.         = score3_adjust_pc_and_fetch_inst (&cpc, NULL,
  606.                                            gdbarch_byte_order (gdbarch));

  607.       if (!inst)
  608.         break;
  609.       if (inst->len == 4 && !stack_sub
  610.           && (G_FLD (inst->v, 29, 25) == 0x1)
  611.           && (G_FLD (inst->v, 19, 17) == 0x0)
  612.           && (G_FLD (inst->v, 24, 20) == 0x0))
  613.         {
  614.           /* addi r0, offset */
  615.           stack_sub = cpc + inst->len;
  616.           pc = cpc + inst->len;
  617.         }
  618.       else if (inst->len == 4
  619.                && (G_FLD (inst->v, 29, 25) == 0x0)
  620.                && (G_FLD (inst->v, 24, 20) == 0x2)
  621.                && (G_FLD (inst->v, 19, 15) == 0x0)
  622.                && (G_FLD (inst->v, 14, 10) == 0xF)
  623.                && (G_FLD (inst->v, 9, 0) == 0x56))
  624.         {
  625.           /* mv r2, r0  */
  626.           pc = cpc + inst->len;
  627.           break;
  628.         }
  629.       else if ((inst->len == 2)
  630.                && (G_FLD (inst->v, 14, 10) == 0x10)
  631.                && (G_FLD (inst->v, 9, 5) == 0x2)
  632.                && (G_FLD (inst->v, 4, 0) == 0x0))
  633.         {
  634.           /* mv! r2, r0 */
  635.           pc = cpc + inst->len;
  636.           break;
  637.         }
  638.       else if (inst->len == 2
  639.                && ((G_FLD (inst->v, 14, 12) == 3) /* b15 form */
  640.                    || (G_FLD (inst->v, 14, 12) == 0x0
  641.                        && G_FLD (inst->v, 11, 5) == 0x4))) /* br! */
  642.         break;
  643.       else if (inst->len == 4
  644.                && ((G_FLD (inst->v, 29, 25) == 2)    /* j32 form */
  645.                    || (G_FLD (inst->v, 29, 25) == 4))) /* b32 form */
  646.         break;

  647.       cpc += inst->len;
  648.     }
  649.   return pc;
  650. }

  651. static int
  652. score7_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR cur_pc)
  653. {
  654.   inst_t *inst = score7_fetch_inst (gdbarch, cur_pc, NULL);

  655.   if (inst->v == 0x23)
  656.     return 1;   /* mv! r0, r2 */
  657.   else if (G_FLD (inst->v, 14, 12) == 0x2
  658.            && G_FLD (inst->v, 3, 0) == 0xa)
  659.     return 1;   /* pop! */
  660.   else if (G_FLD (inst->v, 14, 12) == 0x0
  661.            && G_FLD (inst->v, 7, 0) == 0x34)
  662.     return 1;   /* br! r3 */
  663.   else if (G_FLD (inst->v, 29, 15) == 0x2
  664.            && G_FLD (inst->v, 6, 1) == 0x2b)
  665.     return 1;   /* mv r0, r2 */
  666.   else if (G_FLD (inst->v, 29, 25) == 0x0
  667.            && G_FLD (inst->v, 6, 1) == 0x4
  668.            && G_FLD (inst->v, 19, 15) == 0x3)
  669.     return 1;   /* br r3 */
  670.   else
  671.     return 0;
  672. }

  673. static int
  674. score3_in_function_epilogue_p (struct gdbarch *gdbarch, CORE_ADDR cur_pc)
  675. {
  676.   CORE_ADDR pc = cur_pc;
  677.   inst_t *inst
  678.     = score3_adjust_pc_and_fetch_inst (&pc, NULL,
  679.                                        gdbarch_byte_order (gdbarch));

  680.   if (inst->len == 2
  681.       && (G_FLD (inst->v, 14, 10) == 0x10)
  682.       && (G_FLD (inst->v, 9, 5) == 0x0)
  683.       && (G_FLD (inst->v, 4, 0) == 0x2))
  684.     return 1;   /* mv! r0, r2 */
  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))
  691.     return 1;   /* mv r0, r2 */
  692.   else if (inst->len == 2
  693.            && (G_FLD (inst->v, 14, 12) == 0x0)
  694.            && (G_FLD (inst->v, 11, 5) == 0x2))
  695.     return 1;   /* pop! */
  696.   else if (inst->len == 2
  697.            && (G_FLD (inst->v, 14, 12) == 0x0)
  698.            && (G_FLD (inst->v, 11, 7) == 0x0)
  699.            && (G_FLD (inst->v, 6, 5) == 0x2))
  700.     return 1;   /* rpop! */
  701.   else if (inst->len == 2
  702.            && (G_FLD (inst->v, 14, 12) == 0x0)
  703.            && (G_FLD (inst->v, 11, 5) == 0x4)
  704.            && (G_FLD (inst->v, 4, 0) == 0x3))
  705.     return 1;   /* br! r3 */
  706.   else if (inst->len == 4
  707.            && (G_FLD (inst->v, 29, 25) == 0x0)
  708.            && (G_FLD (inst->v, 24, 20) == 0x0)
  709.            && (G_FLD (inst->v, 19, 15) == 0x3)
  710.            && (G_FLD (inst->v, 14, 10) == 0xF)
  711.            && (G_FLD (inst->v, 9, 0) == 0x8))
  712.     return 1;   /* br r3 */
  713.   else
  714.     return 0;
  715. }

  716. static gdb_byte *
  717. score7_malloc_and_get_memblock (CORE_ADDR addr, CORE_ADDR size)
  718. {
  719.   int ret;
  720.   gdb_byte *memblock = NULL;

  721.   if (size < 0)
  722.     {
  723.       error (_("Error: malloc size < 0 in file:%s, line:%d!"),
  724.              __FILE__, __LINE__);
  725.       return NULL;
  726.     }
  727.   else if (size == 0)
  728.     return NULL;

  729.   memblock = xmalloc (size);
  730.   memset (memblock, 0, size);
  731.   ret = target_read_memory (addr & ~0x3, memblock, size);
  732.   if (ret)
  733.     {
  734.       error (_("Error: target_read_memory in file:%s, line:%d!"),
  735.              __FILE__, __LINE__);
  736.       return NULL;
  737.     }
  738.   return memblock;
  739. }

  740. static void
  741. score7_free_memblock (gdb_byte *memblock)
  742. {
  743.   xfree (memblock);
  744. }

  745. static void
  746. score7_adjust_memblock_ptr (gdb_byte **memblock, CORE_ADDR prev_pc,
  747.                            CORE_ADDR cur_pc)
  748. {
  749.   if (prev_pc == -1)
  750.     {
  751.       /* First time call this function, do nothing.  */
  752.     }
  753.   else if (cur_pc - prev_pc == 2 && (cur_pc & 0x3) == 0)
  754.     {
  755.       /* First 16-bit instruction, then 32-bit instruction.  */
  756.       *memblock += SCORE_INSTLEN;
  757.     }
  758.   else if (cur_pc - prev_pc == 4)
  759.     {
  760.       /* Is 32-bit instruction, increase MEMBLOCK by 4.  */
  761.       *memblock += SCORE_INSTLEN;
  762.     }
  763. }

  764. static void
  765. score7_analyze_prologue (CORE_ADDR startaddr, CORE_ADDR pc,
  766.                         struct frame_info *this_frame,
  767.                         struct score_frame_cache *this_cache)
  768. {
  769.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  770.   CORE_ADDR sp;
  771.   CORE_ADDR fp;
  772.   CORE_ADDR cur_pc = startaddr;

  773.   int sp_offset = 0;
  774.   int ra_offset = 0;
  775.   int fp_offset = 0;
  776.   int ra_offset_p = 0;
  777.   int fp_offset_p = 0;
  778.   int inst_len = 0;

  779.   gdb_byte *memblock = NULL;
  780.   gdb_byte *memblock_ptr = NULL;
  781.   CORE_ADDR prev_pc = -1;

  782.   /* Allocate MEMBLOCK if PC - STARTADDR > 0.  */
  783.   memblock_ptr = memblock =
  784.     score7_malloc_and_get_memblock (startaddr, pc - startaddr);

  785.   sp = get_frame_register_unsigned (this_frame, SCORE_SP_REGNUM);
  786.   fp = get_frame_register_unsigned (this_frame, SCORE_FP_REGNUM);

  787.   for (; cur_pc < pc; prev_pc = cur_pc, cur_pc += inst_len)
  788.     {
  789.       inst_t *inst = NULL;
  790.       if (memblock != NULL)
  791.         {
  792.           /* Reading memory block from target succefully and got all
  793.              the instructions(from STARTADDR to PC) needed.  */
  794.           score7_adjust_memblock_ptr (&memblock, prev_pc, cur_pc);
  795.           inst = score7_fetch_inst (gdbarch, cur_pc, memblock);
  796.         }
  797.       else
  798.         {
  799.           /* Otherwise, we fetch 4 bytes from target, and GDB also
  800.              work correctly.  */
  801.           inst = score7_fetch_inst (gdbarch, cur_pc, NULL);
  802.         }

  803.       /* FIXME: make a full-power prologue analyzer.  */
  804.       if (inst->len == 2)
  805.         {
  806.           inst_len = SCORE16_INSTLEN;

  807.           if (G_FLD (inst->v, 14, 12) == 0x2
  808.               && G_FLD (inst->v, 3, 0) == 0xe)
  809.             {
  810.               /* push! */
  811.               sp_offset += 4;

  812.               if (G_FLD (inst->v, 11, 7) == 0x6
  813.                   && ra_offset_p == 0)
  814.                 {
  815.                   /* push! r3, [r0] */
  816.                   ra_offset = sp_offset;
  817.                   ra_offset_p = 1;
  818.                 }
  819.               else if (G_FLD (inst->v, 11, 7) == 0x4
  820.                        && fp_offset_p == 0)
  821.                 {
  822.                   /* push! r2, [r0] */
  823.                   fp_offset = sp_offset;
  824.                   fp_offset_p = 1;
  825.                 }
  826.             }
  827.           else if (G_FLD (inst->v, 14, 12) == 0x2
  828.                    && G_FLD (inst->v, 3, 0) == 0xa)
  829.             {
  830.               /* pop! */
  831.               sp_offset -= 4;
  832.             }
  833.           else if (G_FLD (inst->v, 14, 7) == 0xc1
  834.                    && G_FLD (inst->v, 2, 0) == 0x0)
  835.             {
  836.               /* subei! r0, n */
  837.               sp_offset += (int) pow (2, G_FLD (inst->v, 6, 3));
  838.             }
  839.           else if (G_FLD (inst->v, 14, 7) == 0xc0
  840.                    && G_FLD (inst->v, 2, 0) == 0x0)
  841.             {
  842.               /* addei! r0, n */
  843.               sp_offset -= (int) pow (2, G_FLD (inst->v, 6, 3));
  844.             }
  845.         }
  846.       else
  847.         {
  848.           inst_len = SCORE_INSTLEN;

  849.           if (G_FLD(inst->v, 29, 25) == 0x3
  850.               && G_FLD(inst->v, 2, 0) == 0x4
  851.               && G_FLD(inst->v, 19, 15) == 0)
  852.             {
  853.                 /* sw rD, [r0, offset]+ */
  854.                 sp_offset += SCORE_INSTLEN;

  855.                 if (G_FLD(inst->v, 24, 20) == 0x3)
  856.                   {
  857.                       /* rD = r3 */
  858.                       if (ra_offset_p == 0)
  859.                         {
  860.                             ra_offset = sp_offset;
  861.                             ra_offset_p = 1;
  862.                         }
  863.                   }
  864.                 else if (G_FLD(inst->v, 24, 20) == 0x2)
  865.                   {
  866.                       /* rD = r2 */
  867.                       if (fp_offset_p == 0)
  868.                         {
  869.                             fp_offset = sp_offset;
  870.                             fp_offset_p = 1;
  871.                         }
  872.                   }
  873.             }
  874.           else if (G_FLD(inst->v, 29, 25) == 0x14
  875.                    && G_FLD(inst->v, 19,15) == 0)
  876.             {
  877.                 /* sw rD, [r0, offset] */
  878.                 if (G_FLD(inst->v, 24, 20) == 0x3)
  879.                   {
  880.                       /* rD = r3 */
  881.                       ra_offset = sp_offset - G_FLD(inst->v, 14, 0);
  882.                       ra_offset_p = 1;
  883.                   }
  884.                 else if (G_FLD(inst->v, 24, 20) == 0x2)
  885.                   {
  886.                       /* rD = r2 */
  887.                       fp_offset = sp_offset - G_FLD(inst->v, 14, 0);
  888.                       fp_offset_p = 1;
  889.                   }
  890.             }
  891.           else if (G_FLD (inst->v, 29, 15) == 0x1c60
  892.                    && G_FLD (inst->v, 2, 0) == 0x0)
  893.             {
  894.               /* lw r3, [r0]+, 4 */
  895.               sp_offset -= SCORE_INSTLEN;
  896.               ra_offset_p = 1;
  897.             }
  898.           else if (G_FLD (inst->v, 29, 15) == 0x1c40
  899.                    && G_FLD (inst->v, 2, 0) == 0x0)
  900.             {
  901.               /* lw r2, [r0]+, 4 */
  902.               sp_offset -= SCORE_INSTLEN;
  903.               fp_offset_p = 1;
  904.             }

  905.           else if (G_FLD (inst->v, 29, 17) == 0x100
  906.                    && G_FLD (inst->v, 0, 0) == 0x0)
  907.             {
  908.               /* addi r0, -offset */
  909.               sp_offset += 65536 - G_FLD (inst->v, 16, 1);
  910.             }
  911.           else if (G_FLD (inst->v, 29, 17) == 0x110
  912.                    && G_FLD (inst->v, 0, 0) == 0x0)
  913.             {
  914.               /* addi r2, offset */
  915.               if (pc - cur_pc > 4)
  916.                 {
  917.                   unsigned int save_v = inst->v;
  918.                   inst_t *inst2 =
  919.                     score7_fetch_inst (gdbarch, cur_pc + SCORE_INSTLEN, NULL);
  920.                   if (inst2->v == 0x23)
  921.                     {
  922.                       /* mv! r0, r2 */
  923.                       sp_offset -= G_FLD (save_v, 16, 1);
  924.                     }
  925.                 }
  926.             }
  927.         }
  928.     }

  929.   /* Save RA.  */
  930.   if (ra_offset_p == 1)
  931.     {
  932.       if (this_cache->saved_regs[SCORE_PC_REGNUM].addr == -1)
  933.         this_cache->saved_regs[SCORE_PC_REGNUM].addr =
  934.           sp + sp_offset - ra_offset;
  935.     }
  936.   else
  937.     {
  938.       this_cache->saved_regs[SCORE_PC_REGNUM] =
  939.         this_cache->saved_regs[SCORE_RA_REGNUM];
  940.     }

  941.   /* Save FP.  */
  942.   if (fp_offset_p == 1)
  943.     {
  944.       if (this_cache->saved_regs[SCORE_FP_REGNUM].addr == -1)
  945.         this_cache->saved_regs[SCORE_FP_REGNUM].addr =
  946.           sp + sp_offset - fp_offset;
  947.     }

  948.   /* Save SP and FP.  */
  949.   this_cache->base = sp + sp_offset;
  950.   this_cache->fp = fp;

  951.   /* Don't forget to free MEMBLOCK if we allocated it.  */
  952.   if (memblock_ptr != NULL)
  953.     score7_free_memblock (memblock_ptr);
  954. }

  955. static void
  956. score3_analyze_prologue (CORE_ADDR startaddr, CORE_ADDR pc,
  957.                         struct frame_info *this_frame,
  958.                         struct score_frame_cache *this_cache)
  959. {
  960.   CORE_ADDR sp;
  961.   CORE_ADDR fp;
  962.   CORE_ADDR cur_pc = startaddr;
  963.   enum bfd_endian byte_order
  964.     = gdbarch_byte_order (get_frame_arch (this_frame));

  965.   int sp_offset = 0;
  966.   int ra_offset = 0;
  967.   int fp_offset = 0;
  968.   int ra_offset_p = 0;
  969.   int fp_offset_p = 0;
  970.   int inst_len = 0;

  971.   CORE_ADDR prev_pc = -1;

  972.   sp = get_frame_register_unsigned (this_frame, SCORE_SP_REGNUM);
  973.   fp = get_frame_register_unsigned (this_frame, SCORE_FP_REGNUM);

  974.   for (; cur_pc < pc; prev_pc = cur_pc, cur_pc += inst_len)
  975.     {
  976.       inst_t *inst = NULL;

  977.       inst = score3_adjust_pc_and_fetch_inst (&cur_pc, &inst_len, byte_order);

  978.       /* FIXME: make a full-power prologue analyzer.  */
  979.       if (inst->len == 2)
  980.         {
  981.           if (G_FLD (inst->v, 14, 12) == 0x0
  982.               && G_FLD (inst->v, 11, 7) == 0x0
  983.               && G_FLD (inst->v, 6, 5) == 0x3)
  984.             {
  985.               /* push! */
  986.               sp_offset += 4;

  987.               if (G_FLD (inst->v, 4, 0) == 0x3
  988.                   && ra_offset_p == 0)
  989.                 {
  990.                   /* push! r3, [r0] */
  991.                   ra_offset = sp_offset;
  992.                   ra_offset_p = 1;
  993.                 }
  994.               else if (G_FLD (inst->v, 4, 0) == 0x2
  995.                        && fp_offset_p == 0)
  996.                 {
  997.                   /* push! r2, [r0] */
  998.                   fp_offset = sp_offset;
  999.                   fp_offset_p = 1;
  1000.                 }
  1001.             }
  1002.           else if (G_FLD (inst->v, 14, 12) == 0x6
  1003.                    && G_FLD (inst->v, 11, 10) == 0x3)
  1004.             {
  1005.               /* rpush! */
  1006.               int start_r = G_FLD (inst->v, 9, 5);
  1007.               int cnt = G_FLD (inst->v, 4, 0);

  1008.               if ((ra_offset_p == 0)
  1009.                   && (start_r <= SCORE_RA_REGNUM)
  1010.                   && (SCORE_RA_REGNUM < start_r + cnt))
  1011.                 {
  1012.                   /* rpush! contains r3 */
  1013.                   ra_offset_p = 1;
  1014.                   ra_offset = sp_offset + 4 * (SCORE_RA_REGNUM - start_r) + 4;
  1015.                 }

  1016.               if ((fp_offset_p == 0)
  1017.                   && (start_r <= SCORE_FP_REGNUM)
  1018.                   && (SCORE_FP_REGNUM < start_r + cnt))
  1019.                 {
  1020.                   /* rpush! contains r2 */
  1021.                   fp_offset_p = 1;
  1022.                   fp_offset = sp_offset + 4 * (SCORE_FP_REGNUM - start_r) + 4;
  1023.                 }

  1024.               sp_offset += 4 * cnt;
  1025.             }
  1026.           else if (G_FLD (inst->v, 14, 12) == 0x0
  1027.                    && G_FLD (inst->v, 11, 7) == 0x0
  1028.                    && G_FLD (inst->v, 6, 5) == 0x2)
  1029.             {
  1030.               /* pop! */
  1031.               sp_offset -= 4;
  1032.             }
  1033.           else if (G_FLD (inst->v, 14, 12) == 0x6
  1034.                    && G_FLD (inst->v, 11, 10) == 0x2)
  1035.             {
  1036.               /* rpop! */
  1037.               sp_offset -= 4 * G_FLD (inst->v, 4, 0);
  1038.             }
  1039.           else if (G_FLD (inst->v, 14, 12) == 0x5
  1040.                    && G_FLD (inst->v, 11, 10) == 0x3
  1041.                    && G_FLD (inst->v, 9, 6) == 0x0)
  1042.             {
  1043.               /* addi! r0, -offset */
  1044.               int imm = G_FLD (inst->v, 5, 0);
  1045.               if (imm >> 5)
  1046.                 imm = -(0x3F - imm + 1);
  1047.               sp_offset -= imm;
  1048.             }
  1049.           else if (G_FLD (inst->v, 14, 12) == 0x5
  1050.                    && G_FLD (inst->v, 11, 10) == 0x3
  1051.                    && G_FLD (inst->v, 9, 6) == 0x2)
  1052.             {
  1053.               /* addi! r2, offset */
  1054.               if (pc - cur_pc >= 2)
  1055.                 {
  1056.                   unsigned int save_v = inst->v;
  1057.                   inst_t *inst2;

  1058.                   cur_pc += inst->len;
  1059.                   inst2 = score3_adjust_pc_and_fetch_inst (&cur_pc, NULL,
  1060.                                                            byte_order);

  1061.                   if (inst2->len == 2
  1062.                       && G_FLD (inst2->v, 14, 10) == 0x10
  1063.                       && G_FLD (inst2->v, 9, 5) == 0x0
  1064.                       && G_FLD (inst2->v, 4, 0) == 0x2)
  1065.                     {
  1066.                       /* mv! r0, r2 */
  1067.                       int imm = G_FLD (inst->v, 5, 0);
  1068.                       if (imm >> 5)
  1069.                         imm = -(0x3F - imm + 1);
  1070.                       sp_offset -= imm;
  1071.                     }
  1072.                 }
  1073.             }
  1074.         }
  1075.       else if (inst->len == 4)
  1076.         {
  1077.           if (G_FLD (inst->v, 29, 25) == 0x3
  1078.               && G_FLD (inst->v, 2, 0) == 0x4
  1079.               && G_FLD (inst->v, 24, 20) == 0x3
  1080.               && G_FLD (inst->v, 19, 15) == 0x0)
  1081.             {
  1082.               /* sw r3, [r0, offset]+ */
  1083.               sp_offset += inst->len;
  1084.               if (ra_offset_p == 0)
  1085.                 {
  1086.                   ra_offset = sp_offset;
  1087.                   ra_offset_p = 1;
  1088.                 }
  1089.             }
  1090.           else if (G_FLD (inst->v, 29, 25) == 0x3
  1091.                    && G_FLD (inst->v, 2, 0) == 0x4
  1092.                    && G_FLD (inst->v, 24, 20) == 0x2
  1093.                    && G_FLD (inst->v, 19, 15) == 0x0)
  1094.             {
  1095.               /* sw r2, [r0, offset]+ */
  1096.               sp_offset += inst->len;
  1097.               if (fp_offset_p == 0)
  1098.                 {
  1099.                   fp_offset = sp_offset;
  1100.                   fp_offset_p = 1;
  1101.                 }
  1102.             }
  1103.           else if (G_FLD (inst->v, 29, 25) == 0x7
  1104.                    && G_FLD (inst->v, 2, 0) == 0x0
  1105.                    && G_FLD (inst->v, 24, 20) == 0x3
  1106.                    && G_FLD (inst->v, 19, 15) == 0x0)
  1107.             {
  1108.               /* lw r3, [r0]+, 4 */
  1109.               sp_offset -= inst->len;
  1110.               ra_offset_p = 1;
  1111.             }
  1112.           else if (G_FLD (inst->v, 29, 25) == 0x7
  1113.                    && G_FLD (inst->v, 2, 0) == 0x0
  1114.                    && G_FLD (inst->v, 24, 20) == 0x2
  1115.                    && G_FLD (inst->v, 19, 15) == 0x0)
  1116.             {
  1117.               /* lw r2, [r0]+, 4 */
  1118.               sp_offset -= inst->len;
  1119.               fp_offset_p = 1;
  1120.             }
  1121.           else if (G_FLD (inst->v, 29, 25) == 0x1
  1122.                    && G_FLD (inst->v, 19, 17) == 0x0
  1123.                    && G_FLD (inst->v, 24, 20) == 0x0
  1124.                    && G_FLD (inst->v, 0, 0) == 0x0)
  1125.             {
  1126.               /* addi r0, -offset */
  1127.               int imm = G_FLD (inst->v, 16, 1);
  1128.               if (imm >> 15)
  1129.                 imm = -(0xFFFF - imm + 1);
  1130.               sp_offset -= imm;
  1131.             }
  1132.           else if (G_FLD (inst->v, 29, 25) == 0x1
  1133.                    && G_FLD (inst->v, 19, 17) == 0x0
  1134.                    && G_FLD (inst->v, 24, 20) == 0x2
  1135.                    && G_FLD (inst->v, 0, 0) == 0x0)
  1136.             {
  1137.               /* addi r2, offset */
  1138.               if (pc - cur_pc >= 2)
  1139.                 {
  1140.                   unsigned int save_v = inst->v;
  1141.                   inst_t *inst2;

  1142.                   cur_pc += inst->len;
  1143.                   inst2 = score3_adjust_pc_and_fetch_inst (&cur_pc, NULL,
  1144.                                                            byte_order);

  1145.                   if (inst2->len == 2
  1146.                       && G_FLD (inst2->v, 14, 10) == 0x10
  1147.                       && G_FLD (inst2->v, 9, 5) == 0x0
  1148.                       && G_FLD (inst2->v, 4, 0) == 0x2)
  1149.                     {
  1150.                       /* mv! r0, r2 */
  1151.                       int imm = G_FLD (inst->v, 16, 1);
  1152.                       if (imm >> 15)
  1153.                         imm = -(0xFFFF - imm + 1);
  1154.                       sp_offset -= imm;
  1155.                     }
  1156.                 }
  1157.             }
  1158.         }
  1159.     }

  1160.   /* Save RA.  */
  1161.   if (ra_offset_p == 1)
  1162.     {
  1163.       if (this_cache->saved_regs[SCORE_PC_REGNUM].addr == -1)
  1164.         this_cache->saved_regs[SCORE_PC_REGNUM].addr =
  1165.           sp + sp_offset - ra_offset;
  1166.     }
  1167.   else
  1168.     {
  1169.       this_cache->saved_regs[SCORE_PC_REGNUM] =
  1170.         this_cache->saved_regs[SCORE_RA_REGNUM];
  1171.     }

  1172.   /* Save FP.  */
  1173.   if (fp_offset_p == 1)
  1174.     {
  1175.       if (this_cache->saved_regs[SCORE_FP_REGNUM].addr == -1)
  1176.         this_cache->saved_regs[SCORE_FP_REGNUM].addr =
  1177.           sp + sp_offset - fp_offset;
  1178.     }

  1179.   /* Save SP and FP.  */
  1180.   this_cache->base = sp + sp_offset;
  1181.   this_cache->fp = fp;
  1182. }

  1183. static struct score_frame_cache *
  1184. score_make_prologue_cache (struct frame_info *this_frame, void **this_cache)
  1185. {
  1186.   struct score_frame_cache *cache;

  1187.   if ((*this_cache) != NULL)
  1188.     return (*this_cache);

  1189.   cache = FRAME_OBSTACK_ZALLOC (struct score_frame_cache);
  1190.   (*this_cache) = cache;
  1191.   cache->saved_regs = trad_frame_alloc_saved_regs (this_frame);

  1192.   /* Analyze the prologue.  */
  1193.   {
  1194.     const CORE_ADDR pc = get_frame_pc (this_frame);
  1195.     CORE_ADDR start_addr;

  1196.     find_pc_partial_function (pc, NULL, &start_addr, NULL);
  1197.     if (start_addr == 0)
  1198.       return cache;

  1199.     if (target_mach == bfd_mach_score3)
  1200.       score3_analyze_prologue (start_addr, pc, this_frame, *this_cache);
  1201.     else
  1202.       score7_analyze_prologue (start_addr, pc, this_frame, *this_cache);
  1203.   }

  1204.   /* Save SP.  */
  1205.   trad_frame_set_value (cache->saved_regs, SCORE_SP_REGNUM, cache->base);

  1206.   return (*this_cache);
  1207. }

  1208. static void
  1209. score_prologue_this_id (struct frame_info *this_frame, void **this_cache,
  1210.                         struct frame_id *this_id)
  1211. {
  1212.   struct score_frame_cache *info = score_make_prologue_cache (this_frame,
  1213.                                                               this_cache);
  1214.   (*this_id) = frame_id_build (info->base, get_frame_func (this_frame));
  1215. }

  1216. static struct value *
  1217. score_prologue_prev_register (struct frame_info *this_frame,
  1218.                               void **this_cache, int regnum)
  1219. {
  1220.   struct score_frame_cache *info = score_make_prologue_cache (this_frame,
  1221.                                                               this_cache);
  1222.   return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
  1223. }

  1224. static const struct frame_unwind score_prologue_unwind =
  1225. {
  1226.   NORMAL_FRAME,
  1227.   default_frame_unwind_stop_reason,
  1228.   score_prologue_this_id,
  1229.   score_prologue_prev_register,
  1230.   NULL,
  1231.   default_frame_sniffer,
  1232.   NULL
  1233. };

  1234. static CORE_ADDR
  1235. score_prologue_frame_base_address (struct frame_info *this_frame,
  1236.                                    void **this_cache)
  1237. {
  1238.   struct score_frame_cache *info =
  1239.     score_make_prologue_cache (this_frame, this_cache);
  1240.   return info->fp;
  1241. }

  1242. static const struct frame_base score_prologue_frame_base =
  1243. {
  1244.   &score_prologue_unwind,
  1245.   score_prologue_frame_base_address,
  1246.   score_prologue_frame_base_address,
  1247.   score_prologue_frame_base_address,
  1248. };

  1249. static const struct frame_base *
  1250. score_prologue_frame_base_sniffer (struct frame_info *this_frame)
  1251. {
  1252.   return &score_prologue_frame_base;
  1253. }

  1254. /* Core file support.  */

  1255. static const struct regcache_map_entry score7_linux_gregmap[] =
  1256.   {
  1257.     /* FIXME: According to the current Linux kernel, r0 is preceded by
  1258.        9 rather than 7 words.  */
  1259.     { 7, REGCACHE_MAP_SKIP, 4 },
  1260.     { 32, 0, 4 },                /* r0 ... r31 */
  1261.     { 1, 55, 4 },                /* CEL */
  1262.     { 1, 54, 4 },                /* CEH */
  1263.     { 1, 53, 4 },                /* sr0, i.e. cnt or COUNTER */
  1264.     { 1, 52, 4 },                /* sr1, i.e. lcr or LDCR */
  1265.     { 1, 51, 4 },                /* sr2, i.e. scr or STCR */
  1266.     { 1, 49, 4 },                /* PC (same slot as EPC) */
  1267.     { 1, 38, 4 },                /* EMA */
  1268.     { 1, 32, 4 },                /* PSR */
  1269.     { 1, 34, 4 },                /* ECR */
  1270.     { 1, 33, 4 },                /* COND */
  1271.     { 0 }
  1272.   };

  1273. #define SCORE7_LINUX_EPC_OFFSET (44 * 4)
  1274. #define SCORE7_LINUX_SIZEOF_GREGSET (49 * 4)

  1275. static void
  1276. score7_linux_supply_gregset(const struct regset *regset,
  1277.                             struct regcache *regcache,
  1278.                             int regnum, const void *buf,
  1279.                             size_t size)
  1280. {
  1281.   regcache_supply_regset (regset, regcache, regnum, buf, size);

  1282.   /* Supply the EPC from the same slot as the PC.  Note that the
  1283.      collect function will store the PC in that slot.  */
  1284.   if ((regnum == -1 || regnum == SCORE_EPC_REGNUM)
  1285.       && size >= SCORE7_LINUX_EPC_OFFSET + 4)
  1286.     regcache_raw_supply (regcache, SCORE_EPC_REGNUM,
  1287.                          (const gdb_byte *) buf
  1288.                          + SCORE7_LINUX_EPC_OFFSET);
  1289. }

  1290. static const struct regset score7_linux_gregset =
  1291.   {
  1292.     score7_linux_gregmap,
  1293.     score7_linux_supply_gregset,
  1294.     regcache_collect_regset
  1295.   };

  1296. /* Iterate over core file register note sections.  */

  1297. static void
  1298. score7_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
  1299.                                            iterate_over_regset_sections_cb *cb,
  1300.                                            void *cb_data,
  1301.                                            const struct regcache *regcache)
  1302. {
  1303.   cb (".reg", SCORE7_LINUX_SIZEOF_GREGSET, &score7_linux_gregset,
  1304.       NULL, cb_data);
  1305. }

  1306. static struct gdbarch *
  1307. score_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  1308. {
  1309.   struct gdbarch *gdbarch;
  1310.   target_mach = info.bfd_arch_info->mach;

  1311.   arches = gdbarch_list_lookup_by_info (arches, &info);
  1312.   if (arches != NULL)
  1313.     {
  1314.       return (arches->gdbarch);
  1315.     }
  1316.   gdbarch = gdbarch_alloc (&info, NULL);

  1317.   set_gdbarch_short_bit (gdbarch, 16);
  1318.   set_gdbarch_int_bit (gdbarch, 32);
  1319.   set_gdbarch_float_bit (gdbarch, 32);
  1320.   set_gdbarch_double_bit (gdbarch, 64);
  1321.   set_gdbarch_long_double_bit (gdbarch, 64);
  1322. #if WITH_SIM
  1323.   set_gdbarch_register_sim_regno (gdbarch, score_register_sim_regno);
  1324. #endif
  1325.   set_gdbarch_pc_regnum (gdbarch, SCORE_PC_REGNUM);
  1326.   set_gdbarch_sp_regnum (gdbarch, SCORE_SP_REGNUM);
  1327.   set_gdbarch_adjust_breakpoint_address (gdbarch,
  1328.                                          score_adjust_breakpoint_address);
  1329.   set_gdbarch_register_type (gdbarch, score_register_type);
  1330.   set_gdbarch_frame_align (gdbarch, score_frame_align);
  1331.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  1332.   set_gdbarch_unwind_sp (gdbarch, score_unwind_sp);
  1333.   set_gdbarch_unwind_pc (gdbarch, score_unwind_pc);
  1334.   set_gdbarch_print_insn (gdbarch, score_print_insn);

  1335.   switch (target_mach)
  1336.     {
  1337.     case bfd_mach_score7:
  1338.       set_gdbarch_breakpoint_from_pc (gdbarch, score7_breakpoint_from_pc);
  1339.       set_gdbarch_skip_prologue (gdbarch, score7_skip_prologue);
  1340.       set_gdbarch_in_function_epilogue_p (gdbarch,
  1341.                                           score7_in_function_epilogue_p);
  1342.       set_gdbarch_register_name (gdbarch, score7_register_name);
  1343.       set_gdbarch_num_regs (gdbarch, SCORE7_NUM_REGS);
  1344.       /* Core file support.  */
  1345.       set_gdbarch_iterate_over_regset_sections
  1346.         (gdbarch, score7_linux_iterate_over_regset_sections);
  1347.       break;

  1348.     case bfd_mach_score3:
  1349.       set_gdbarch_breakpoint_from_pc (gdbarch, score3_breakpoint_from_pc);
  1350.       set_gdbarch_skip_prologue (gdbarch, score3_skip_prologue);
  1351.       set_gdbarch_in_function_epilogue_p (gdbarch,
  1352.                                           score3_in_function_epilogue_p);
  1353.       set_gdbarch_register_name (gdbarch, score3_register_name);
  1354.       set_gdbarch_num_regs (gdbarch, SCORE3_NUM_REGS);
  1355.       break;
  1356.     }

  1357.   /* Watchpoint hooks.  */
  1358.   set_gdbarch_have_nonsteppable_watchpoint (gdbarch, 1);

  1359.   /* Dummy frame hooks.  */
  1360.   set_gdbarch_return_value (gdbarch, score_return_value);
  1361.   set_gdbarch_call_dummy_location (gdbarch, AT_ENTRY_POINT);
  1362.   set_gdbarch_dummy_id (gdbarch, score_dummy_id);
  1363.   set_gdbarch_push_dummy_call (gdbarch, score_push_dummy_call);

  1364.   /* Normal frame hooks.  */
  1365.   dwarf2_append_unwinders (gdbarch);
  1366.   frame_base_append_sniffer (gdbarch, dwarf2_frame_base_sniffer);
  1367.   frame_unwind_append_unwinder (gdbarch, &score_prologue_unwind);
  1368.   frame_base_append_sniffer (gdbarch, score_prologue_frame_base_sniffer);

  1369.   return gdbarch;
  1370. }

  1371. extern initialize_file_ftype _initialize_score_tdep;

  1372. void
  1373. _initialize_score_tdep (void)
  1374. {
  1375.   gdbarch_register (bfd_arch_score, score_gdbarch_init, NULL);
  1376. }