gdb/bfin-tdep.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Target-dependent code for Analog Devices Blackfin processor, for GDB.

  2.    Copyright (C) 2005-2015 Free Software Foundation, Inc.

  3.    Contributed by Analog Devices, Inc.

  4.    This file is part of GDB.

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

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

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

  15. #include "defs.h"
  16. #include "inferior.h"
  17. #include "gdbcore.h"
  18. #include "arch-utils.h"
  19. #include "regcache.h"
  20. #include "frame.h"
  21. #include "frame-unwind.h"
  22. #include "frame-base.h"
  23. #include "trad-frame.h"
  24. #include "dis-asm.h"
  25. #include "sim-regno.h"
  26. #include "gdb/sim-bfin.h"
  27. #include "dwarf2-frame.h"
  28. #include "symtab.h"
  29. #include "elf-bfd.h"
  30. #include "elf/bfin.h"
  31. #include "osabi.h"
  32. #include "infcall.h"
  33. #include "xml-syscall.h"
  34. #include "bfin-tdep.h"

  35. /* Macros used by prologue functions.  */
  36. #define P_LINKAGE                        0xE800
  37. #define P_MINUS_SP1                        0x0140
  38. #define P_MINUS_SP2                        0x05C0
  39. #define P_MINUS_SP3                        0x0540
  40. #define P_MINUS_SP4                        0x04C0
  41. #define P_SP_PLUS                        0x6C06
  42. #define P_P2_LOW                        0xE10A
  43. #define P_P2_HIGH                        0XE14A
  44. #define P_SP_EQ_SP_PLUS_P2                0X5BB2
  45. #define P_SP_EQ_P2_PLUS_SP                0x5B96
  46. #define P_MINUS_MINUS_SP_EQ_RETS        0x0167

  47. /* Macros used for program flow control.  */
  48. /* 16 bit instruction, max  */
  49. #define P_16_BIT_INSR_MAX                0xBFFF
  50. /* 32 bit instruction, min  */
  51. #define P_32_BIT_INSR_MIN                0xC000
  52. /* 32 bit instruction, max  */
  53. #define P_32_BIT_INSR_MAX                0xE801
  54. /* jump (preg), 16-bit, min  */
  55. #define P_JUMP_PREG_MIN                        0x0050
  56. /* jump (preg), 16-bit, max  */
  57. #define P_JUMP_PREG_MAX                        0x0057
  58. /* jump (pc+preg), 16-bit, min  */
  59. #define P_JUMP_PC_PLUS_PREG_MIN                0x0080
  60. /* jump (pc+preg), 16-bit, max  */
  61. #define P_JUMP_PC_PLUS_PREG_MAX                0x0087
  62. /* jump.s pcrel13m2, 16-bit, min  */
  63. #define P_JUMP_S_MIN                        0x2000
  64. /* jump.s pcrel13m2, 16-bit, max  */
  65. #define P_JUMP_S_MAX                        0x2FFF
  66. /* jump.l pcrel25m2, 32-bit, min  */
  67. #define P_JUMP_L_MIN                        0xE200
  68. /* jump.l pcrel25m2, 32-bit, max  */
  69. #define P_JUMP_L_MAX                        0xE2FF
  70. /* conditional jump pcrel11m2, 16-bit, min  */
  71. #define P_IF_CC_JUMP_MIN                0x1800
  72. /* conditional jump pcrel11m2, 16-bit, max  */
  73. #define P_IF_CC_JUMP_MAX                0x1BFF
  74. /* conditional jump(bp) pcrel11m2, 16-bit, min  */
  75. #define P_IF_CC_JUMP_BP_MIN                0x1C00
  76. /* conditional jump(bp) pcrel11m2, 16-bit, max  */
  77. #define P_IF_CC_JUMP_BP_MAX                0x1FFF
  78. /* conditional !jump pcrel11m2, 16-bit, min  */
  79. #define P_IF_NOT_CC_JUMP_MIN                0x1000
  80. /* conditional !jump pcrel11m2, 16-bit, max  */
  81. #define P_IF_NOT_CC_JUMP_MAX                0x13FF
  82. /* conditional jump(bp) pcrel11m2, 16-bit, min  */
  83. #define P_IF_NOT_CC_JUMP_BP_MIN                0x1400
  84. /* conditional jump(bp) pcrel11m2, 16-bit, max  */
  85. #define P_IF_NOT_CC_JUMP_BP_MAX                0x17FF
  86. /* call (preg), 16-bit, min  */
  87. #define P_CALL_PREG_MIN                        0x0060
  88. /* call (preg), 16-bit, max  */
  89. #define P_CALL_PREG_MAX                        0x0067
  90. /* call (pc+preg), 16-bit, min  */
  91. #define P_CALL_PC_PLUS_PREG_MIN                0x0070
  92. /* call (pc+preg), 16-bit, max  */
  93. #define P_CALL_PC_PLUS_PREG_MAX                0x0077
  94. /* call pcrel25m2, 32-bit, min  */
  95. #define P_CALL_MIN                        0xE300
  96. /* call pcrel25m2, 32-bit, max  */
  97. #define P_CALL_MAX                        0xE3FF
  98. /* RTS  */
  99. #define P_RTS                                0x0010
  100. /* MNOP  */
  101. #define P_MNOP                                0xC803
  102. /* EXCPT, 16-bit, min  */
  103. #define P_EXCPT_MIN                        0x00A0
  104. /* EXCPT, 16-bit, max  */
  105. #define P_EXCPT_MAX                        0x00AF
  106. /* multi instruction mask 1, 16-bit  */
  107. #define P_BIT_MULTI_INS_1                0xC000
  108. /* multi instruction mask 2, 16-bit  */
  109. #define P_BIT_MULTI_INS_2                0x0800

  110. /* The maximum bytes we search to skip the prologue.  */
  111. #define UPPER_LIMIT                        40

  112. /* ASTAT bits  */
  113. #define ASTAT_CC_POS                        5
  114. #define ASTAT_CC                        (1 << ASTAT_CC_POS)

  115. /* Initial value: Register names used in BFIN's ISA documentation.  */

  116. static const char * const bfin_register_name_strings[] =
  117. {
  118.   "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  119.   "p0", "p1", "p2", "p3", "p4", "p5", "sp", "fp",
  120.   "i0", "i1", "i2", "i3", "m0", "m1", "m2", "m3",
  121.   "b0", "b1", "b2", "b3", "l0", "l1", "l2", "l3",
  122.   "a0x", "a0w", "a1x", "a1w", "astat", "rets",
  123.   "lc0", "lt0", "lb0", "lc1", "lt1", "lb1", "cycles", "cycles2",
  124.   "usp", "seqstat", "syscfg", "reti", "retx", "retn", "rete",
  125.   "pc", "cc",
  126. };

  127. #define NUM_BFIN_REGNAMES ARRAY_SIZE (bfin_register_name_strings)


  128. /* In this diagram successive memory locations increase downwards or the
  129.    stack grows upwards with negative indices.  (PUSH analogy for stack.)

  130.    The top frame is the "frame" of the current function being executed.

  131.      +--------------+ SP    -
  132.      |  local vars  |       ^
  133.      +--------------+       |
  134.      |  save regs   |       |
  135.      +--------------+ FP    |
  136.      |   old FP    -|--    top
  137.      +--------------+  |  frame
  138.      |    RETS      |  |    |
  139.      +--------------+  |    |
  140.      |   param 1    |  |    |
  141.      |   param 2    |  |    |
  142.      |    ...       |  |    V
  143.      +--------------+  |    -
  144.      |  local vars  |  |    ^
  145.      +--------------+  |    |
  146.      |  save regs   |  |    |
  147.      +--------------+<-     |
  148.      |   old FP    -|--   next
  149.      +--------------+  |  frame
  150.      |    RETS      |  |    |
  151.      +--------------+  |    |
  152.      |   param 1    |  |    |
  153.      |   param 2    |  |    |
  154.      |    ...       |  |    V
  155.      +--------------+  |    -
  156.      |  local vars  |  |    ^
  157.      +--------------+  |    |
  158.      |  save regs   |  |    |
  159.      +--------------+<-  next frame
  160.      |   old FP     |       |
  161.      +--------------+       |
  162.      |    RETS      |       V
  163.      +--------------+       -

  164.    The frame chain is formed as following:

  165.      FP has the topmost frame.
  166.      FP + 4 has the previous FP and so on.  */


  167. /* Map from DWARF2 register number to GDB register number.  */

  168. static const int map_gcc_gdb[] =
  169. {
  170.   BFIN_R0_REGNUM,
  171.   BFIN_R1_REGNUM,
  172.   BFIN_R2_REGNUM,
  173.   BFIN_R3_REGNUM,
  174.   BFIN_R4_REGNUM,
  175.   BFIN_R5_REGNUM,
  176.   BFIN_R6_REGNUM,
  177.   BFIN_R7_REGNUM,
  178.   BFIN_P0_REGNUM,
  179.   BFIN_P1_REGNUM,
  180.   BFIN_P2_REGNUM,
  181.   BFIN_P3_REGNUM,
  182.   BFIN_P4_REGNUM,
  183.   BFIN_P5_REGNUM,
  184.   BFIN_SP_REGNUM,
  185.   BFIN_FP_REGNUM,
  186.   BFIN_I0_REGNUM,
  187.   BFIN_I1_REGNUM,
  188.   BFIN_I2_REGNUM,
  189.   BFIN_I3_REGNUM,
  190.   BFIN_B0_REGNUM,
  191.   BFIN_B1_REGNUM,
  192.   BFIN_B2_REGNUM,
  193.   BFIN_B3_REGNUM,
  194.   BFIN_L0_REGNUM,
  195.   BFIN_L1_REGNUM,
  196.   BFIN_L2_REGNUM,
  197.   BFIN_L3_REGNUM,
  198.   BFIN_M0_REGNUM,
  199.   BFIN_M1_REGNUM,
  200.   BFIN_M2_REGNUM,
  201.   BFIN_M3_REGNUM,
  202.   BFIN_A0_DOT_X_REGNUM,
  203.   BFIN_A1_DOT_X_REGNUM,
  204.   BFIN_CC_REGNUM,
  205.   BFIN_RETS_REGNUM,
  206.   BFIN_RETI_REGNUM,
  207.   BFIN_RETX_REGNUM,
  208.   BFIN_RETN_REGNUM,
  209.   BFIN_RETE_REGNUM,
  210.   BFIN_ASTAT_REGNUM,
  211.   BFIN_SEQSTAT_REGNUM,
  212.   BFIN_USP_REGNUM,
  213.   BFIN_LT0_REGNUM,
  214.   BFIN_LT1_REGNUM,
  215.   BFIN_LC0_REGNUM,
  216.   BFIN_LC1_REGNUM,
  217.   BFIN_LB0_REGNUM,
  218.   BFIN_LB1_REGNUM
  219. };


  220. struct bfin_frame_cache
  221. {
  222.   /* Base address.  */
  223.   CORE_ADDR base;
  224.   CORE_ADDR sp_offset;
  225.   CORE_ADDR pc;
  226.   int frameless_pc_value;

  227.   /* Saved registers.  */
  228.   CORE_ADDR saved_regs[BFIN_NUM_REGS];
  229.   CORE_ADDR saved_sp;

  230.   /* Stack space reserved for local variables.  */
  231.   long locals;
  232. };

  233. /* Allocate and initialize a frame cache.  */

  234. static struct bfin_frame_cache *
  235. bfin_alloc_frame_cache (void)
  236. {
  237.   struct bfin_frame_cache *cache;
  238.   int i;

  239.   cache = FRAME_OBSTACK_ZALLOC (struct bfin_frame_cache);

  240.   /* Base address.  */
  241.   cache->base = 0;
  242.   cache->sp_offset = -4;
  243.   cache->pc = 0;
  244.   cache->frameless_pc_value = 0;

  245.   /* Saved registers.  We initialize these to -1 since zero is a valid
  246.      offset (that's where fp is supposed to be stored).  */
  247.   for (i = 0; i < BFIN_NUM_REGS; i++)
  248.     cache->saved_regs[i] = -1;

  249.   /* Frameless until proven otherwise.  */
  250.   cache->locals = -1;

  251.   return cache;
  252. }

  253. static struct bfin_frame_cache *
  254. bfin_frame_cache (struct frame_info *this_frame, void **this_cache)
  255. {
  256.   struct bfin_frame_cache *cache;
  257.   int i;

  258.   if (*this_cache)
  259.     return *this_cache;

  260.   cache = bfin_alloc_frame_cache ();
  261.   *this_cache = cache;

  262.   cache->base = get_frame_register_unsigned (this_frame, BFIN_FP_REGNUM);
  263.   if (cache->base == 0)
  264.     return cache;

  265.   /* For normal frames, PC is stored at [FP + 4].  */
  266.   cache->saved_regs[BFIN_PC_REGNUM] = 4;
  267.   cache->saved_regs[BFIN_FP_REGNUM] = 0;

  268.   /* Adjust all the saved registers such that they contain addresses
  269.      instead of offsets.  */
  270.   for (i = 0; i < BFIN_NUM_REGS; i++)
  271.     if (cache->saved_regs[i] != -1)
  272.       cache->saved_regs[i] += cache->base;

  273.   cache->pc = get_frame_func (this_frame) ;
  274.   if (cache->pc == 0 || cache->pc == get_frame_pc (this_frame))
  275.     {
  276.       /* Either there is no prologue (frameless function) or we are at
  277.          the start of a function.  In short we do not have a frame.
  278.          PC is stored in rets register.  FP points to previous frame.  */

  279.       cache->saved_regs[BFIN_PC_REGNUM] =
  280.         get_frame_register_unsigned (this_frame, BFIN_RETS_REGNUM);
  281.       cache->frameless_pc_value = 1;
  282.       cache->base = get_frame_register_unsigned (this_frame, BFIN_FP_REGNUM);
  283.       cache->saved_regs[BFIN_FP_REGNUM] = cache->base;
  284.       cache->saved_sp = cache->base;
  285.     }
  286.   else
  287.     {
  288.       cache->frameless_pc_value = 0;

  289.       /* Now that we have the base address for the stack frame we can
  290.          calculate the value of SP in the calling frame.  */
  291.       cache->saved_sp = cache->base + 8;
  292.     }

  293.   return cache;
  294. }

  295. static void
  296. bfin_frame_this_id (struct frame_info *this_frame,
  297.                     void **this_cache,
  298.                     struct frame_id *this_id)
  299. {
  300.   struct bfin_frame_cache *cache = bfin_frame_cache (this_frame, this_cache);

  301.   /* This marks the outermost frame.  */
  302.   if (cache->base == 0)
  303.     return;

  304.   /* See the end of bfin_push_dummy_call.  */
  305.   *this_id = frame_id_build (cache->base + 8, cache->pc);
  306. }

  307. static struct value *
  308. bfin_frame_prev_register (struct frame_info *this_frame,
  309.                           void **this_cache,
  310.                           int regnum)
  311. {
  312.   struct gdbarch *gdbarch = get_frame_arch (this_frame);
  313.   struct bfin_frame_cache *cache = bfin_frame_cache (this_frame, this_cache);

  314.   if (regnum == gdbarch_sp_regnum (gdbarch) && cache->saved_sp)
  315.     return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);

  316.   if (regnum < BFIN_NUM_REGS && cache->saved_regs[regnum] != -1)
  317.     return frame_unwind_got_memory (this_frame, regnum,
  318.                                     cache->saved_regs[regnum]);

  319.   return frame_unwind_got_register (this_frame, regnum, regnum);
  320. }

  321. static const struct frame_unwind bfin_frame_unwind =
  322. {
  323.   NORMAL_FRAME,
  324.   default_frame_unwind_stop_reason,
  325.   bfin_frame_this_id,
  326.   bfin_frame_prev_register,
  327.   NULL,
  328.   default_frame_sniffer
  329. };

  330. /* Check for "[--SP] = <reg>;" insns.  These are appear in function
  331.    prologues to save misc registers onto the stack.  */

  332. static int
  333. is_minus_minus_sp (int op)
  334. {
  335.   op &= 0xFFC0;

  336.   if ((op == P_MINUS_SP1) || (op == P_MINUS_SP2)
  337.       || (op == P_MINUS_SP3) || (op == P_MINUS_SP4))
  338.     return 1;

  339.   return 0;
  340. }

  341. /* Skip all the insns that appear in generated function prologues.  */

  342. static CORE_ADDR
  343. bfin_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  344. {
  345.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  346.   int op = read_memory_unsigned_integer (pc, 2, byte_order);
  347.   CORE_ADDR orig_pc = pc;
  348.   int done = 0;

  349.   /* The new gcc prologue generates the register saves BEFORE the link
  350.      or RETS saving instruction.
  351.      So, our job is to stop either at those instructions or some upper
  352.      limit saying there is no frame!  */

  353.   while (!done)
  354.     {
  355.       if (is_minus_minus_sp (op))
  356.         {
  357.           while (is_minus_minus_sp (op))
  358.             {
  359.               pc += 2;
  360.               op = read_memory_unsigned_integer (pc, 2, byte_order);
  361.             }

  362.           if (op == P_LINKAGE)
  363.             pc += 4;

  364.           done = 1;
  365.         }
  366.       else if (op == P_LINKAGE)
  367.         {
  368.           pc += 4;
  369.           done = 1;
  370.         }
  371.       else if (op == P_MINUS_MINUS_SP_EQ_RETS)
  372.         {
  373.           pc += 2;
  374.           done = 1;
  375.         }
  376.       else if (op == P_RTS)
  377.         {
  378.           done = 1;
  379.         }
  380.       else if ((op >= P_JUMP_PREG_MIN && op <= P_JUMP_PREG_MAX)
  381.                || (op >= P_JUMP_PC_PLUS_PREG_MIN
  382.                    && op <= P_JUMP_PC_PLUS_PREG_MAX)
  383.                || (op == P_JUMP_S_MIN && op <= P_JUMP_S_MAX))
  384.         {
  385.           done = 1;
  386.         }
  387.       else if (pc - orig_pc >= UPPER_LIMIT)
  388.         {
  389.           warning (_("Function Prologue not recognised; "
  390.                      "pc will point to ENTRY_POINT of the function"));
  391.           pc = orig_pc + 2;
  392.           done = 1;
  393.         }
  394.       else
  395.         {
  396.           pc += 2; /* Not a terminating instruction go on.  */
  397.           op = read_memory_unsigned_integer (pc, 2, byte_order);
  398.         }
  399.     }

  400.    /* TODO:
  401.       Dwarf2 uses entry point value AFTER some register initializations.
  402.       We should perhaps skip such asssignments as well (R6 = R1, ...).  */

  403.   return pc;
  404. }

  405. /* Return the GDB type object for the "standard" data type of data in
  406.    register N.  This should be void pointer for P0-P5, SP, FP;
  407.    void pointer to function for PC; int otherwise.  */

  408. static struct type *
  409. bfin_register_type (struct gdbarch *gdbarch, int regnum)
  410. {
  411.   if ((regnum >= BFIN_P0_REGNUM && regnum <= BFIN_FP_REGNUM)
  412.       || regnum == BFIN_USP_REGNUM)
  413.     return builtin_type (gdbarch)->builtin_data_ptr;

  414.   if (regnum == BFIN_PC_REGNUM || regnum == BFIN_RETS_REGNUM
  415.       || regnum == BFIN_RETI_REGNUM || regnum == BFIN_RETX_REGNUM
  416.       || regnum == BFIN_RETN_REGNUM || regnum == BFIN_RETE_REGNUM
  417.       || regnum == BFIN_LT0_REGNUM || regnum == BFIN_LB0_REGNUM
  418.       || regnum == BFIN_LT1_REGNUM || regnum == BFIN_LB1_REGNUM)
  419.     return builtin_type (gdbarch)->builtin_func_ptr;

  420.   return builtin_type (gdbarch)->builtin_int32;
  421. }

  422. static CORE_ADDR
  423. bfin_push_dummy_call (struct gdbarch *gdbarch,
  424.                       struct value *function,
  425.                       struct regcache *regcache,
  426.                       CORE_ADDR bp_addr,
  427.                       int nargs,
  428.                       struct value **args,
  429.                       CORE_ADDR sp,
  430.                       int struct_return,
  431.                       CORE_ADDR struct_addr)
  432. {
  433.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  434.   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
  435.   gdb_byte buf[4];
  436.   int i;
  437.   long reg_r0, reg_r1, reg_r2;
  438.   int total_len = 0;
  439.   enum bfin_abi abi = bfin_abi (gdbarch);
  440.   CORE_ADDR func_addr = find_function_addr (function, NULL);

  441.   for (i = nargs - 1; i >= 0; i--)
  442.     {
  443.       struct type *value_type = value_enclosing_type (args[i]);

  444.       total_len += (TYPE_LENGTH (value_type) + 3) & ~3;
  445.     }

  446.   /* At least twelve bytes of stack space must be allocated for the function's
  447.      arguments, even for functions that have less than 12 bytes of argument
  448.      data.  */

  449.   if (total_len < 12)
  450.     sp -= 12 - total_len;

  451.   /* Push arguments in reverse order.  */

  452.   for (i = nargs - 1; i >= 0; i--)
  453.     {
  454.       struct type *value_type = value_enclosing_type (args[i]);
  455.       struct type *arg_type = check_typedef (value_type);
  456.       int container_len = (TYPE_LENGTH (value_type) + 3) & ~3;

  457.       sp -= container_len;
  458.       write_memory (sp, value_contents_writeable (args[i]), container_len);
  459.     }

  460.   /* Initialize R0, R1, and R2 to the first 3 words of parameters.  */

  461.   reg_r0 = read_memory_integer (sp, 4, byte_order);
  462.   regcache_cooked_write_unsigned (regcache, BFIN_R0_REGNUM, reg_r0);
  463.   reg_r1 = read_memory_integer (sp + 4, 4, byte_order);
  464.   regcache_cooked_write_unsigned (regcache, BFIN_R1_REGNUM, reg_r1);
  465.   reg_r2 = read_memory_integer (sp + 8, 4, byte_order);
  466.   regcache_cooked_write_unsigned (regcache, BFIN_R2_REGNUM, reg_r2);

  467.   /* Store struct value address.  */

  468.   if (struct_return)
  469.     regcache_cooked_write_unsigned (regcache, BFIN_P0_REGNUM, struct_addr);

  470.   /* Set the dummy return value to bp_addr.
  471.      A dummy breakpoint will be setup to execute the call.  */

  472.   regcache_cooked_write_unsigned (regcache, BFIN_RETS_REGNUM, bp_addr);

  473.   /* Finally, update the stack pointer.  */

  474.   regcache_cooked_write_unsigned (regcache, BFIN_SP_REGNUM, sp);

  475.   return sp;
  476. }

  477. /* Convert DWARF2 register number REG to the appropriate register number
  478.    used by GDB.  */

  479. static int
  480. bfin_reg_to_regnum (struct gdbarch *gdbarch, int reg)
  481. {
  482.   if (reg > ARRAY_SIZE (map_gcc_gdb))
  483.     return 0;

  484.   return map_gcc_gdb[reg];
  485. }

  486. /* This function implements the 'breakpoint_from_pc' gdbarch method.
  487.    It returns a pointer to a string of bytes that encode a breakpoint
  488.    instruction, stores the length of the string to *lenptr, and
  489.    adjusts the program counter (if necessary) to point to the actual
  490.    memory location where the breakpoint should be inserted.  */

  491. static const unsigned char *
  492. bfin_breakpoint_from_pc (struct gdbarch *gdbarch,
  493.                          CORE_ADDR *pcptr, int *lenptr)
  494. {
  495.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  496.   unsigned short iw;
  497.   static unsigned char bfin_breakpoint[] = {0xa1, 0x00, 0x00, 0x00};
  498.   static unsigned char bfin_sim_breakpoint[] = {0x25, 0x00, 0x00, 0x00};

  499.   iw = read_memory_unsigned_integer (*pcptr, 2, byte_order);

  500.   if ((iw & 0xf000) >= 0xc000)
  501.     /* 32-bit instruction.  */
  502.     *lenptr = 4;
  503.   else
  504.     *lenptr = 2;

  505.   if (strcmp (target_shortname, "sim") == 0)
  506.     return bfin_sim_breakpoint;
  507.   else
  508.     return bfin_breakpoint;
  509. }

  510. static void
  511. bfin_extract_return_value (struct type *type,
  512.                            struct regcache *regs,
  513.                            gdb_byte *dst)
  514. {
  515.   struct gdbarch *gdbarch = get_regcache_arch (regs);
  516.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  517.   bfd_byte *valbuf = dst;
  518.   int len = TYPE_LENGTH (type);
  519.   ULONGEST tmp;
  520.   int regno = BFIN_R0_REGNUM;

  521.   gdb_assert (len <= 8);

  522.   while (len > 0)
  523.     {
  524.       regcache_cooked_read_unsigned (regs, regno++, &tmp);
  525.       store_unsigned_integer (valbuf, (len > 4 ? 4 : len), byte_order, tmp);
  526.       len -= 4;
  527.       valbuf += 4;
  528.     }
  529. }

  530. /* Write into appropriate registers a function return value of type
  531.    TYPE, given in virtual format.  */

  532. static void
  533. bfin_store_return_value (struct type *type,
  534.                          struct regcache *regs,
  535.                          const gdb_byte *src)
  536. {
  537.   const bfd_byte *valbuf = src;

  538.   /* Integral values greater than one word are stored in consecutive
  539.      registers starting with R0.  This will always be a multiple of
  540.      the register size.  */

  541.   int len = TYPE_LENGTH (type);
  542.   int regno = BFIN_R0_REGNUM;

  543.   gdb_assert (len <= 8);

  544.   while (len > 0)
  545.     {
  546.       regcache_cooked_write (regs, regno++, valbuf);
  547.       len -= 4;
  548.       valbuf += 4;
  549.     }
  550. }

  551. /* Determine, for architecture GDBARCH, how a return value of TYPE
  552.    should be returned.  If it is supposed to be returned in registers,
  553.    and READBUF is nonzero, read the appropriate value from REGCACHE,
  554.    and copy it into READBUF.  If WRITEBUF is nonzero, write the value
  555.    from WRITEBUF into REGCACHE.  */

  556. static enum return_value_convention
  557. bfin_return_value (struct gdbarch *gdbarch,
  558.                    struct value *function,
  559.                    struct type *type,
  560.                    struct regcache *regcache,
  561.                    gdb_byte *readbuf,
  562.                    const gdb_byte *writebuf)
  563. {
  564.   if (TYPE_LENGTH (type) > 8)
  565.     return RETURN_VALUE_STRUCT_CONVENTION;

  566.   if (readbuf)
  567.     bfin_extract_return_value (type, regcache, readbuf);

  568.   if (writebuf)
  569.     bfin_store_return_value (type, regcache, writebuf);

  570.   return RETURN_VALUE_REGISTER_CONVENTION;
  571. }

  572. /* Return the BFIN register name corresponding to register I.  */

  573. static const char *
  574. bfin_register_name (struct gdbarch *gdbarch, int i)
  575. {
  576.   return bfin_register_name_strings[i];
  577. }

  578. static enum register_status
  579. bfin_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
  580.                            int regnum, gdb_byte *buffer)
  581. {
  582.   gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);
  583.   enum register_status status;

  584.   if (regnum != BFIN_CC_REGNUM)
  585.     internal_error (__FILE__, __LINE__,
  586.                     _("invalid register number %d"), regnum);

  587.   /* Extract the CC bit from the ASTAT register.  */
  588.   status = regcache_raw_read (regcache, BFIN_ASTAT_REGNUM, buf);
  589.   if (status == REG_VALID)
  590.     {
  591.       buffer[1] = buffer[2] = buffer[3] = 0;
  592.       buffer[0] = !!(buf[0] & ASTAT_CC);
  593.     }
  594.   return status;
  595. }

  596. static void
  597. bfin_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
  598.                             int regnum, const gdb_byte *buffer)
  599. {
  600.   gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);

  601.   if (regnum != BFIN_CC_REGNUM)
  602.     internal_error (__FILE__, __LINE__,
  603.                     _("invalid register number %d"), regnum);

  604.   /* Overlay the CC bit in the ASTAT register.  */
  605.   regcache_raw_read (regcache, BFIN_ASTAT_REGNUM, buf);
  606.   buf[0] = (buf[0] & ~ASTAT_CC) | ((buffer[0] & 1) << ASTAT_CC_POS);
  607.   regcache_raw_write (regcache, BFIN_ASTAT_REGNUM, buf);
  608. }

  609. static CORE_ADDR
  610. bfin_frame_base_address (struct frame_info *this_frame, void **this_cache)
  611. {
  612.   struct bfin_frame_cache *cache = bfin_frame_cache (this_frame, this_cache);

  613.   return cache->base;
  614. }

  615. static CORE_ADDR
  616. bfin_frame_local_address (struct frame_info *this_frame, void **this_cache)
  617. {
  618.   struct bfin_frame_cache *cache = bfin_frame_cache (this_frame, this_cache);

  619.   return cache->base - 4;
  620. }

  621. static CORE_ADDR
  622. bfin_frame_args_address (struct frame_info *this_frame, void **this_cache)
  623. {
  624.   struct bfin_frame_cache *cache = bfin_frame_cache (this_frame, this_cache);

  625.   return cache->base + 8;
  626. }

  627. static const struct frame_base bfin_frame_base =
  628. {
  629.   &bfin_frame_unwind,
  630.   bfin_frame_base_address,
  631.   bfin_frame_local_address,
  632.   bfin_frame_args_address
  633. };

  634. static struct frame_id
  635. bfin_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  636. {
  637.   CORE_ADDR sp;

  638.   sp = get_frame_register_unsigned (this_frame, BFIN_SP_REGNUM);

  639.   return frame_id_build (sp, get_frame_pc (this_frame));
  640. }

  641. static CORE_ADDR
  642. bfin_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  643. {
  644.   return frame_unwind_register_unsigned (next_frame, BFIN_PC_REGNUM);
  645. }

  646. static CORE_ADDR
  647. bfin_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
  648. {
  649.   return (address & ~0x3);
  650. }

  651. enum bfin_abi
  652. bfin_abi (struct gdbarch *gdbarch)
  653. {
  654.   return gdbarch_tdep (gdbarch)->bfin_abi;
  655. }

  656. /* Initialize the current architecture based on INFO.  If possible,
  657.    re-use an architecture from ARCHES, which is a list of
  658.    architectures already created during this debugging session.

  659.    Called e.g. at program startup, when reading a core file, and when
  660.    reading a binary file.  */

  661. static struct gdbarch *
  662. bfin_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  663. {
  664.   struct gdbarch_tdep *tdep;
  665.   struct gdbarch *gdbarch;
  666.   int elf_flags;
  667.   enum bfin_abi abi;

  668.   /* Extract the ELF flags, if available.  */
  669.   if (info.abfd && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
  670.     elf_flags = elf_elfheader (info.abfd)->e_flags;
  671.   else
  672.     elf_flags = 0;

  673.   abi = BFIN_ABI_FLAT;

  674.   /* If there is already a candidate, use it.  */

  675.   for (arches = gdbarch_list_lookup_by_info (arches, &info);
  676.        arches != NULL;
  677.        arches = gdbarch_list_lookup_by_info (arches->next, &info))
  678.     {
  679.       if (gdbarch_tdep (arches->gdbarch)->bfin_abi != abi)
  680.         continue;
  681.       return arches->gdbarch;
  682.     }

  683.   tdep = XNEW (struct gdbarch_tdep);
  684.   gdbarch = gdbarch_alloc (&info, tdep);

  685.   tdep->bfin_abi = abi;

  686.   set_gdbarch_num_regs (gdbarch, BFIN_NUM_REGS);
  687.   set_gdbarch_pseudo_register_read (gdbarch, bfin_pseudo_register_read);
  688.   set_gdbarch_pseudo_register_write (gdbarch, bfin_pseudo_register_write);
  689.   set_gdbarch_num_pseudo_regs (gdbarch, BFIN_NUM_PSEUDO_REGS);
  690.   set_gdbarch_sp_regnum (gdbarch, BFIN_SP_REGNUM);
  691.   set_gdbarch_pc_regnum (gdbarch, BFIN_PC_REGNUM);
  692.   set_gdbarch_ps_regnum (gdbarch, BFIN_ASTAT_REGNUM);
  693.   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, bfin_reg_to_regnum);
  694.   set_gdbarch_register_name (gdbarch, bfin_register_name);
  695.   set_gdbarch_register_type (gdbarch, bfin_register_type);
  696.   set_gdbarch_dummy_id (gdbarch, bfin_dummy_id);
  697.   set_gdbarch_push_dummy_call (gdbarch, bfin_push_dummy_call);
  698.   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
  699.   set_gdbarch_return_value (gdbarch, bfin_return_value);
  700.   set_gdbarch_skip_prologue (gdbarch, bfin_skip_prologue);
  701.   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  702.   set_gdbarch_breakpoint_from_pc (gdbarch, bfin_breakpoint_from_pc);
  703.   set_gdbarch_decr_pc_after_break (gdbarch, 2);
  704.   set_gdbarch_frame_args_skip (gdbarch, 8);
  705.   set_gdbarch_unwind_pc (gdbarch, bfin_unwind_pc);
  706.   set_gdbarch_frame_align (gdbarch, bfin_frame_align);
  707.   set_gdbarch_print_insn (gdbarch, print_insn_bfin);

  708.   /* Hook in ABI-specific overrides, if they have been registered.  */
  709.   gdbarch_init_osabi (info, gdbarch);

  710.   dwarf2_append_unwinders (gdbarch);

  711.   frame_base_set_default (gdbarch, &bfin_frame_base);

  712.   frame_unwind_append_unwinder (gdbarch, &bfin_frame_unwind);

  713.   return gdbarch;
  714. }

  715. /* Provide a prototype to silence -Wmissing-prototypes.  */
  716. extern initialize_file_ftype _initialize_bfin_tdep;

  717. void
  718. _initialize_bfin_tdep (void)
  719. {
  720.   register_gdbarch_init (bfd_arch_bfin, bfin_gdbarch_init);
  721. }