GDB (API)
/home/stan/gdb/src/gdb/bfin-tdep.c
Go to the documentation of this file.
00001 /* Target-dependent code for Analog Devices Blackfin processor, for GDB.
00002 
00003    Copyright (C) 2005-2013 Free Software Foundation, Inc.
00004 
00005    Contributed by Analog Devices, Inc.
00006 
00007    This file is part of GDB.
00008 
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 3 of the License, or
00012    (at your option) any later version.
00013 
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018 
00019    You should have received a copy of the GNU General Public License
00020    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00021 
00022 #include "defs.h"
00023 #include "gdb_string.h"
00024 #include "inferior.h"
00025 #include "gdbcore.h"
00026 #include "arch-utils.h"
00027 #include "regcache.h"
00028 #include "frame.h"
00029 #include "frame-unwind.h"
00030 #include "frame-base.h"
00031 #include "trad-frame.h"
00032 #include "dis-asm.h"
00033 #include "gdb_assert.h"
00034 #include "sim-regno.h"
00035 #include "gdb/sim-bfin.h"
00036 #include "dwarf2-frame.h"
00037 #include "symtab.h"
00038 #include "elf-bfd.h"
00039 #include "elf/bfin.h"
00040 #include "osabi.h"
00041 #include "infcall.h"
00042 #include "xml-syscall.h"
00043 #include "bfin-tdep.h"
00044 
00045 /* Macros used by prologue functions.  */
00046 #define P_LINKAGE                       0xE800
00047 #define P_MINUS_SP1                     0x0140
00048 #define P_MINUS_SP2                     0x05C0
00049 #define P_MINUS_SP3                     0x0540
00050 #define P_MINUS_SP4                     0x04C0
00051 #define P_SP_PLUS                       0x6C06
00052 #define P_P2_LOW                        0xE10A
00053 #define P_P2_HIGH                       0XE14A
00054 #define P_SP_EQ_SP_PLUS_P2              0X5BB2
00055 #define P_SP_EQ_P2_PLUS_SP              0x5B96
00056 #define P_MINUS_MINUS_SP_EQ_RETS        0x0167
00057 
00058 /* Macros used for program flow control.  */
00059 /* 16 bit instruction, max  */
00060 #define P_16_BIT_INSR_MAX               0xBFFF
00061 /* 32 bit instruction, min  */
00062 #define P_32_BIT_INSR_MIN               0xC000
00063 /* 32 bit instruction, max  */
00064 #define P_32_BIT_INSR_MAX               0xE801
00065 /* jump (preg), 16-bit, min  */
00066 #define P_JUMP_PREG_MIN                 0x0050
00067 /* jump (preg), 16-bit, max  */
00068 #define P_JUMP_PREG_MAX                 0x0057
00069 /* jump (pc+preg), 16-bit, min  */
00070 #define P_JUMP_PC_PLUS_PREG_MIN         0x0080
00071 /* jump (pc+preg), 16-bit, max  */
00072 #define P_JUMP_PC_PLUS_PREG_MAX         0x0087
00073 /* jump.s pcrel13m2, 16-bit, min  */
00074 #define P_JUMP_S_MIN                    0x2000
00075 /* jump.s pcrel13m2, 16-bit, max  */
00076 #define P_JUMP_S_MAX                    0x2FFF
00077 /* jump.l pcrel25m2, 32-bit, min  */
00078 #define P_JUMP_L_MIN                    0xE200
00079 /* jump.l pcrel25m2, 32-bit, max  */
00080 #define P_JUMP_L_MAX                    0xE2FF
00081 /* conditional jump pcrel11m2, 16-bit, min  */
00082 #define P_IF_CC_JUMP_MIN                0x1800
00083 /* conditional jump pcrel11m2, 16-bit, max  */
00084 #define P_IF_CC_JUMP_MAX                0x1BFF
00085 /* conditional jump(bp) pcrel11m2, 16-bit, min  */
00086 #define P_IF_CC_JUMP_BP_MIN             0x1C00
00087 /* conditional jump(bp) pcrel11m2, 16-bit, max  */
00088 #define P_IF_CC_JUMP_BP_MAX             0x1FFF
00089 /* conditional !jump pcrel11m2, 16-bit, min  */
00090 #define P_IF_NOT_CC_JUMP_MIN            0x1000
00091 /* conditional !jump pcrel11m2, 16-bit, max  */
00092 #define P_IF_NOT_CC_JUMP_MAX            0x13FF
00093 /* conditional jump(bp) pcrel11m2, 16-bit, min  */
00094 #define P_IF_NOT_CC_JUMP_BP_MIN         0x1400
00095 /* conditional jump(bp) pcrel11m2, 16-bit, max  */
00096 #define P_IF_NOT_CC_JUMP_BP_MAX         0x17FF
00097 /* call (preg), 16-bit, min  */
00098 #define P_CALL_PREG_MIN                 0x0060
00099 /* call (preg), 16-bit, max  */
00100 #define P_CALL_PREG_MAX                 0x0067
00101 /* call (pc+preg), 16-bit, min  */
00102 #define P_CALL_PC_PLUS_PREG_MIN         0x0070
00103 /* call (pc+preg), 16-bit, max  */
00104 #define P_CALL_PC_PLUS_PREG_MAX         0x0077
00105 /* call pcrel25m2, 32-bit, min  */
00106 #define P_CALL_MIN                      0xE300
00107 /* call pcrel25m2, 32-bit, max  */
00108 #define P_CALL_MAX                      0xE3FF
00109 /* RTS  */
00110 #define P_RTS                           0x0010
00111 /* MNOP  */
00112 #define P_MNOP                          0xC803
00113 /* EXCPT, 16-bit, min  */
00114 #define P_EXCPT_MIN                     0x00A0
00115 /* EXCPT, 16-bit, max  */
00116 #define P_EXCPT_MAX                     0x00AF
00117 /* multi instruction mask 1, 16-bit  */
00118 #define P_BIT_MULTI_INS_1               0xC000
00119 /* multi instruction mask 2, 16-bit  */
00120 #define P_BIT_MULTI_INS_2               0x0800
00121 
00122 /* The maximum bytes we search to skip the prologue.  */
00123 #define UPPER_LIMIT                     40
00124 
00125 /* ASTAT bits  */
00126 #define ASTAT_CC_POS                    5
00127 #define ASTAT_CC                        (1 << ASTAT_CC_POS)
00128 
00129 /* Initial value: Register names used in BFIN's ISA documentation.  */
00130 
00131 static const char * const bfin_register_name_strings[] =
00132 {
00133   "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
00134   "p0", "p1", "p2", "p3", "p4", "p5", "sp", "fp",
00135   "i0", "i1", "i2", "i3", "m0", "m1", "m2", "m3",
00136   "b0", "b1", "b2", "b3", "l0", "l1", "l2", "l3",
00137   "a0x", "a0w", "a1x", "a1w", "astat", "rets",
00138   "lc0", "lt0", "lb0", "lc1", "lt1", "lb1", "cycles", "cycles2",
00139   "usp", "seqstat", "syscfg", "reti", "retx", "retn", "rete",
00140   "pc", "cc",
00141 };
00142 
00143 #define NUM_BFIN_REGNAMES ARRAY_SIZE (bfin_register_name_strings)
00144 
00145 
00146 /* In this diagram successive memory locations increase downwards or the
00147    stack grows upwards with negative indices.  (PUSH analogy for stack.)
00148 
00149    The top frame is the "frame" of the current function being executed.
00150 
00151      +--------------+ SP    -
00152      |  local vars  |       ^
00153      +--------------+       |
00154      |  save regs   |       |
00155      +--------------+ FP    |
00156      |   old FP    -|--    top
00157      +--------------+  |  frame
00158      |    RETS      |  |    |
00159      +--------------+  |    |
00160      |   param 1    |  |    |
00161      |   param 2    |  |    |
00162      |    ...       |  |    V
00163      +--------------+  |    -
00164      |  local vars  |  |    ^
00165      +--------------+  |    |
00166      |  save regs   |  |    |
00167      +--------------+<-     |
00168      |   old FP    -|--   next
00169      +--------------+  |  frame
00170      |    RETS      |  |    |
00171      +--------------+  |    |
00172      |   param 1    |  |    |
00173      |   param 2    |  |    |
00174      |    ...       |  |    V
00175      +--------------+  |    -
00176      |  local vars  |  |    ^
00177      +--------------+  |    |
00178      |  save regs   |  |    |
00179      +--------------+<-  next frame
00180      |   old FP     |       |
00181      +--------------+       |
00182      |    RETS      |       V
00183      +--------------+       -
00184 
00185    The frame chain is formed as following:
00186 
00187      FP has the topmost frame.
00188      FP + 4 has the previous FP and so on.  */
00189 
00190 
00191 /* Map from DWARF2 register number to GDB register number.  */
00192 
00193 static const int map_gcc_gdb[] =
00194 {
00195   BFIN_R0_REGNUM,
00196   BFIN_R1_REGNUM,
00197   BFIN_R2_REGNUM,
00198   BFIN_R3_REGNUM,
00199   BFIN_R4_REGNUM,
00200   BFIN_R5_REGNUM,
00201   BFIN_R6_REGNUM,
00202   BFIN_R7_REGNUM,
00203   BFIN_P0_REGNUM,
00204   BFIN_P1_REGNUM,
00205   BFIN_P2_REGNUM,
00206   BFIN_P3_REGNUM,
00207   BFIN_P4_REGNUM,
00208   BFIN_P5_REGNUM,
00209   BFIN_SP_REGNUM,
00210   BFIN_FP_REGNUM,
00211   BFIN_I0_REGNUM,
00212   BFIN_I1_REGNUM,
00213   BFIN_I2_REGNUM,
00214   BFIN_I3_REGNUM,
00215   BFIN_B0_REGNUM,
00216   BFIN_B1_REGNUM,
00217   BFIN_B2_REGNUM,
00218   BFIN_B3_REGNUM,
00219   BFIN_L0_REGNUM,
00220   BFIN_L1_REGNUM,
00221   BFIN_L2_REGNUM,
00222   BFIN_L3_REGNUM,
00223   BFIN_M0_REGNUM,
00224   BFIN_M1_REGNUM,
00225   BFIN_M2_REGNUM,
00226   BFIN_M3_REGNUM,
00227   BFIN_A0_DOT_X_REGNUM,
00228   BFIN_A1_DOT_X_REGNUM,
00229   BFIN_CC_REGNUM,
00230   BFIN_RETS_REGNUM,
00231   BFIN_RETI_REGNUM,
00232   BFIN_RETX_REGNUM,
00233   BFIN_RETN_REGNUM,
00234   BFIN_RETE_REGNUM,
00235   BFIN_ASTAT_REGNUM,
00236   BFIN_SEQSTAT_REGNUM,
00237   BFIN_USP_REGNUM,
00238   BFIN_LT0_REGNUM,
00239   BFIN_LT1_REGNUM,
00240   BFIN_LC0_REGNUM,
00241   BFIN_LC1_REGNUM,
00242   BFIN_LB0_REGNUM,
00243   BFIN_LB1_REGNUM
00244 };
00245 
00246 
00247 struct bfin_frame_cache
00248 {
00249   /* Base address.  */
00250   CORE_ADDR base;
00251   CORE_ADDR sp_offset;
00252   CORE_ADDR pc;
00253   int frameless_pc_value;
00254 
00255   /* Saved registers.  */
00256   CORE_ADDR saved_regs[BFIN_NUM_REGS];
00257   CORE_ADDR saved_sp;
00258 
00259   /* Stack space reserved for local variables.  */
00260   long locals;
00261 };
00262 
00263 /* Allocate and initialize a frame cache.  */
00264 
00265 static struct bfin_frame_cache *
00266 bfin_alloc_frame_cache (void)
00267 {
00268   struct bfin_frame_cache *cache;
00269   int i;
00270 
00271   cache = FRAME_OBSTACK_ZALLOC (struct bfin_frame_cache);
00272 
00273   /* Base address.  */
00274   cache->base = 0;
00275   cache->sp_offset = -4;
00276   cache->pc = 0;
00277   cache->frameless_pc_value = 0;
00278 
00279   /* Saved registers.  We initialize these to -1 since zero is a valid
00280      offset (that's where fp is supposed to be stored).  */
00281   for (i = 0; i < BFIN_NUM_REGS; i++)
00282     cache->saved_regs[i] = -1;
00283 
00284   /* Frameless until proven otherwise.  */
00285   cache->locals = -1;
00286 
00287   return cache;
00288 }
00289 
00290 static struct bfin_frame_cache *
00291 bfin_frame_cache (struct frame_info *this_frame, void **this_cache)
00292 {
00293   struct bfin_frame_cache *cache;
00294   int i;
00295 
00296   if (*this_cache)
00297     return *this_cache;
00298 
00299   cache = bfin_alloc_frame_cache ();
00300   *this_cache = cache;
00301 
00302   cache->base = get_frame_register_unsigned (this_frame, BFIN_FP_REGNUM);
00303   if (cache->base == 0)
00304     return cache;
00305 
00306   /* For normal frames, PC is stored at [FP + 4].  */
00307   cache->saved_regs[BFIN_PC_REGNUM] = 4;
00308   cache->saved_regs[BFIN_FP_REGNUM] = 0;
00309 
00310   /* Adjust all the saved registers such that they contain addresses
00311      instead of offsets.  */
00312   for (i = 0; i < BFIN_NUM_REGS; i++)
00313     if (cache->saved_regs[i] != -1)
00314       cache->saved_regs[i] += cache->base;
00315 
00316   cache->pc = get_frame_func (this_frame) ;
00317   if (cache->pc == 0 || cache->pc == get_frame_pc (this_frame))
00318     {
00319       /* Either there is no prologue (frameless function) or we are at
00320          the start of a function.  In short we do not have a frame.
00321          PC is stored in rets register.  FP points to previous frame.  */
00322 
00323       cache->saved_regs[BFIN_PC_REGNUM] =
00324         get_frame_register_unsigned (this_frame, BFIN_RETS_REGNUM);
00325       cache->frameless_pc_value = 1;
00326       cache->base = get_frame_register_unsigned (this_frame, BFIN_FP_REGNUM);
00327       cache->saved_regs[BFIN_FP_REGNUM] = cache->base;
00328       cache->saved_sp = cache->base;
00329     }
00330   else
00331     {
00332       cache->frameless_pc_value = 0;
00333 
00334       /* Now that we have the base address for the stack frame we can
00335          calculate the value of SP in the calling frame.  */
00336       cache->saved_sp = cache->base + 8;
00337     }
00338 
00339   return cache;
00340 }
00341 
00342 static void
00343 bfin_frame_this_id (struct frame_info *this_frame,
00344                     void **this_cache,
00345                     struct frame_id *this_id)
00346 {
00347   struct bfin_frame_cache *cache = bfin_frame_cache (this_frame, this_cache);
00348 
00349   /* This marks the outermost frame.  */
00350   if (cache->base == 0)
00351     return;
00352 
00353   /* See the end of bfin_push_dummy_call.  */
00354   *this_id = frame_id_build (cache->base + 8, cache->pc);
00355 }
00356 
00357 static struct value *
00358 bfin_frame_prev_register (struct frame_info *this_frame,
00359                           void **this_cache,
00360                           int regnum)
00361 {
00362   struct gdbarch *gdbarch = get_frame_arch (this_frame);
00363   struct bfin_frame_cache *cache = bfin_frame_cache (this_frame, this_cache);
00364 
00365   if (regnum == gdbarch_sp_regnum (gdbarch) && cache->saved_sp)
00366     return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
00367 
00368   if (regnum < BFIN_NUM_REGS && cache->saved_regs[regnum] != -1)
00369     return frame_unwind_got_memory (this_frame, regnum,
00370                                     cache->saved_regs[regnum]);
00371 
00372   return frame_unwind_got_register (this_frame, regnum, regnum);
00373 }
00374 
00375 static const struct frame_unwind bfin_frame_unwind =
00376 {
00377   NORMAL_FRAME,
00378   default_frame_unwind_stop_reason,
00379   bfin_frame_this_id,
00380   bfin_frame_prev_register,
00381   NULL,
00382   default_frame_sniffer
00383 };
00384 
00385 /* Check for "[--SP] = <reg>;" insns.  These are appear in function
00386    prologues to save misc registers onto the stack.  */
00387 
00388 static int
00389 is_minus_minus_sp (int op)
00390 {
00391   op &= 0xFFC0;
00392 
00393   if ((op == P_MINUS_SP1) || (op == P_MINUS_SP2)
00394       || (op == P_MINUS_SP3) || (op == P_MINUS_SP4))
00395     return 1;
00396 
00397   return 0;
00398 }
00399 
00400 /* Skip all the insns that appear in generated function prologues.  */
00401 
00402 static CORE_ADDR
00403 bfin_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
00404 {
00405   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00406   int op = read_memory_unsigned_integer (pc, 2, byte_order);
00407   CORE_ADDR orig_pc = pc;
00408   int done = 0;
00409 
00410   /* The new gcc prologue generates the register saves BEFORE the link
00411      or RETS saving instruction.
00412      So, our job is to stop either at those instructions or some upper
00413      limit saying there is no frame!  */
00414 
00415   while (!done)
00416     {
00417       if (is_minus_minus_sp (op))
00418         {
00419           while (is_minus_minus_sp (op))
00420             {
00421               pc += 2;
00422               op = read_memory_unsigned_integer (pc, 2, byte_order);
00423             }
00424 
00425           if (op == P_LINKAGE)
00426             pc += 4;
00427 
00428           done = 1;
00429         }
00430       else if (op == P_LINKAGE)
00431         {
00432           pc += 4;
00433           done = 1;
00434         }
00435       else if (op == P_MINUS_MINUS_SP_EQ_RETS)
00436         {
00437           pc += 2;
00438           done = 1;
00439         }
00440       else if (op == P_RTS)
00441         {
00442           done = 1;
00443         }
00444       else if ((op >= P_JUMP_PREG_MIN && op <= P_JUMP_PREG_MAX)
00445                || (op >= P_JUMP_PC_PLUS_PREG_MIN
00446                    && op <= P_JUMP_PC_PLUS_PREG_MAX)
00447                || (op == P_JUMP_S_MIN && op <= P_JUMP_S_MAX))
00448         {
00449           done = 1;
00450         }
00451       else if (pc - orig_pc >= UPPER_LIMIT)
00452         {
00453           warning (_("Function Prologue not recognised; "
00454                      "pc will point to ENTRY_POINT of the function"));
00455           pc = orig_pc + 2;
00456           done = 1;
00457         }
00458       else
00459         {
00460           pc += 2; /* Not a terminating instruction go on.  */
00461           op = read_memory_unsigned_integer (pc, 2, byte_order);
00462         }
00463     }
00464 
00465    /* TODO:
00466       Dwarf2 uses entry point value AFTER some register initializations.
00467       We should perhaps skip such asssignments as well (R6 = R1, ...).  */
00468 
00469   return pc;
00470 }
00471 
00472 /* Return the GDB type object for the "standard" data type of data in
00473    register N.  This should be void pointer for P0-P5, SP, FP;
00474    void pointer to function for PC; int otherwise.  */
00475 
00476 static struct type *
00477 bfin_register_type (struct gdbarch *gdbarch, int regnum)
00478 {
00479   if ((regnum >= BFIN_P0_REGNUM && regnum <= BFIN_FP_REGNUM)
00480       || regnum == BFIN_USP_REGNUM)
00481     return builtin_type (gdbarch)->builtin_data_ptr;
00482 
00483   if (regnum == BFIN_PC_REGNUM || regnum == BFIN_RETS_REGNUM
00484       || regnum == BFIN_RETI_REGNUM || regnum == BFIN_RETX_REGNUM
00485       || regnum == BFIN_RETN_REGNUM || regnum == BFIN_RETE_REGNUM
00486       || regnum == BFIN_LT0_REGNUM || regnum == BFIN_LB0_REGNUM
00487       || regnum == BFIN_LT1_REGNUM || regnum == BFIN_LB1_REGNUM)
00488     return builtin_type (gdbarch)->builtin_func_ptr;
00489 
00490   return builtin_type (gdbarch)->builtin_int32;
00491 }
00492 
00493 static CORE_ADDR
00494 bfin_push_dummy_call (struct gdbarch *gdbarch,
00495                       struct value *function,
00496                       struct regcache *regcache,
00497                       CORE_ADDR bp_addr,
00498                       int nargs,
00499                       struct value **args,
00500                       CORE_ADDR sp,
00501                       int struct_return,
00502                       CORE_ADDR struct_addr)
00503 {
00504   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00505   struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
00506   gdb_byte buf[4];
00507   int i;
00508   long reg_r0, reg_r1, reg_r2;
00509   int total_len = 0;
00510   enum bfin_abi abi = bfin_abi (gdbarch);
00511   CORE_ADDR func_addr = find_function_addr (function, NULL);
00512 
00513   for (i = nargs - 1; i >= 0; i--)
00514     {
00515       struct type *value_type = value_enclosing_type (args[i]);
00516 
00517       total_len += (TYPE_LENGTH (value_type) + 3) & ~3;
00518     }
00519 
00520   /* At least twelve bytes of stack space must be allocated for the function's
00521      arguments, even for functions that have less than 12 bytes of argument
00522      data.  */
00523 
00524   if (total_len < 12)
00525     sp -= 12 - total_len;
00526 
00527   /* Push arguments in reverse order.  */
00528 
00529   for (i = nargs - 1; i >= 0; i--)
00530     {
00531       struct type *value_type = value_enclosing_type (args[i]);
00532       struct type *arg_type = check_typedef (value_type);
00533       int container_len = (TYPE_LENGTH (value_type) + 3) & ~3;
00534 
00535       sp -= container_len;
00536       write_memory (sp, value_contents_writeable (args[i]), container_len);
00537     }
00538 
00539   /* Initialize R0, R1, and R2 to the first 3 words of parameters.  */
00540 
00541   reg_r0 = read_memory_integer (sp, 4, byte_order);
00542   regcache_cooked_write_unsigned (regcache, BFIN_R0_REGNUM, reg_r0);
00543   reg_r1 = read_memory_integer (sp + 4, 4, byte_order);
00544   regcache_cooked_write_unsigned (regcache, BFIN_R1_REGNUM, reg_r1);
00545   reg_r2 = read_memory_integer (sp + 8, 4, byte_order);
00546   regcache_cooked_write_unsigned (regcache, BFIN_R2_REGNUM, reg_r2);
00547 
00548   /* Store struct value address.  */
00549 
00550   if (struct_return)
00551     regcache_cooked_write_unsigned (regcache, BFIN_P0_REGNUM, struct_addr);
00552 
00553   /* Set the dummy return value to bp_addr.
00554      A dummy breakpoint will be setup to execute the call.  */
00555 
00556   regcache_cooked_write_unsigned (regcache, BFIN_RETS_REGNUM, bp_addr);
00557 
00558   /* Finally, update the stack pointer.  */
00559 
00560   regcache_cooked_write_unsigned (regcache, BFIN_SP_REGNUM, sp);
00561 
00562   return sp;
00563 }
00564 
00565 /* Convert DWARF2 register number REG to the appropriate register number
00566    used by GDB.  */
00567 
00568 static int
00569 bfin_reg_to_regnum (struct gdbarch *gdbarch, int reg)
00570 {
00571   if (reg > ARRAY_SIZE (map_gcc_gdb))
00572     return 0;
00573 
00574   return map_gcc_gdb[reg];
00575 }
00576 
00577 /* This function implements the 'breakpoint_from_pc' gdbarch method.
00578    It returns a pointer to a string of bytes that encode a breakpoint
00579    instruction, stores the length of the string to *lenptr, and
00580    adjusts the program counter (if necessary) to point to the actual
00581    memory location where the breakpoint should be inserted.  */
00582 
00583 static const unsigned char *
00584 bfin_breakpoint_from_pc (struct gdbarch *gdbarch,
00585                          CORE_ADDR *pcptr, int *lenptr)
00586 {
00587   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00588   unsigned short iw;
00589   static unsigned char bfin_breakpoint[] = {0xa1, 0x00, 0x00, 0x00};
00590   static unsigned char bfin_sim_breakpoint[] = {0x25, 0x00, 0x00, 0x00};
00591 
00592   iw = read_memory_unsigned_integer (*pcptr, 2, byte_order);
00593 
00594   if ((iw & 0xf000) >= 0xc000)
00595     /* 32-bit instruction.  */
00596     *lenptr = 4;
00597   else
00598     *lenptr = 2;
00599 
00600   if (strcmp (target_shortname, "sim") == 0)
00601     return bfin_sim_breakpoint;
00602   else
00603     return bfin_breakpoint;
00604 }
00605 
00606 static void
00607 bfin_extract_return_value (struct type *type,
00608                            struct regcache *regs,
00609                            gdb_byte *dst)
00610 {
00611   struct gdbarch *gdbarch = get_regcache_arch (regs);
00612   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
00613   bfd_byte *valbuf = dst;
00614   int len = TYPE_LENGTH (type);
00615   ULONGEST tmp;
00616   int regno = BFIN_R0_REGNUM;
00617 
00618   gdb_assert (len <= 8);
00619 
00620   while (len > 0)
00621     {
00622       regcache_cooked_read_unsigned (regs, regno++, &tmp);
00623       store_unsigned_integer (valbuf, (len > 4 ? 4 : len), byte_order, tmp);
00624       len -= 4;
00625       valbuf += 4;
00626     }
00627 }
00628 
00629 /* Write into appropriate registers a function return value of type
00630    TYPE, given in virtual format.  */
00631 
00632 static void
00633 bfin_store_return_value (struct type *type,
00634                          struct regcache *regs,
00635                          const gdb_byte *src)
00636 {
00637   const bfd_byte *valbuf = src;
00638 
00639   /* Integral values greater than one word are stored in consecutive
00640      registers starting with R0.  This will always be a multiple of
00641      the register size.  */
00642 
00643   int len = TYPE_LENGTH (type);
00644   int regno = BFIN_R0_REGNUM;
00645 
00646   gdb_assert (len <= 8);
00647 
00648   while (len > 0)
00649     {
00650       regcache_cooked_write (regs, regno++, valbuf);
00651       len -= 4;
00652       valbuf += 4;
00653     }
00654 }
00655 
00656 /* Determine, for architecture GDBARCH, how a return value of TYPE
00657    should be returned.  If it is supposed to be returned in registers,
00658    and READBUF is nonzero, read the appropriate value from REGCACHE,
00659    and copy it into READBUF.  If WRITEBUF is nonzero, write the value
00660    from WRITEBUF into REGCACHE.  */
00661 
00662 static enum return_value_convention
00663 bfin_return_value (struct gdbarch *gdbarch,
00664                    struct value *function,
00665                    struct type *type,
00666                    struct regcache *regcache,
00667                    gdb_byte *readbuf,
00668                    const gdb_byte *writebuf)
00669 {
00670   if (TYPE_LENGTH (type) > 8)
00671     return RETURN_VALUE_STRUCT_CONVENTION;
00672 
00673   if (readbuf)
00674     bfin_extract_return_value (type, regcache, readbuf);
00675 
00676   if (writebuf)
00677     bfin_store_return_value (type, regcache, writebuf);
00678 
00679   return RETURN_VALUE_REGISTER_CONVENTION;
00680 }
00681 
00682 /* Return the BFIN register name corresponding to register I.  */
00683 
00684 static const char *
00685 bfin_register_name (struct gdbarch *gdbarch, int i)
00686 {
00687   return bfin_register_name_strings[i];
00688 }
00689 
00690 static enum register_status
00691 bfin_pseudo_register_read (struct gdbarch *gdbarch, struct regcache *regcache,
00692                            int regnum, gdb_byte *buffer)
00693 {
00694   gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);
00695   enum register_status status;
00696 
00697   if (regnum != BFIN_CC_REGNUM)
00698     internal_error (__FILE__, __LINE__,
00699                     _("invalid register number %d"), regnum);
00700 
00701   /* Extract the CC bit from the ASTAT register.  */
00702   status = regcache_raw_read (regcache, BFIN_ASTAT_REGNUM, buf);
00703   if (status == REG_VALID)
00704     {
00705       buffer[1] = buffer[2] = buffer[3] = 0;
00706       buffer[0] = !!(buf[0] & ASTAT_CC);
00707     }
00708   return status;
00709 }
00710 
00711 static void
00712 bfin_pseudo_register_write (struct gdbarch *gdbarch, struct regcache *regcache,
00713                             int regnum, const gdb_byte *buffer)
00714 {
00715   gdb_byte *buf = (gdb_byte *) alloca (MAX_REGISTER_SIZE);
00716 
00717   if (regnum != BFIN_CC_REGNUM)
00718     internal_error (__FILE__, __LINE__,
00719                     _("invalid register number %d"), regnum);
00720 
00721   /* Overlay the CC bit in the ASTAT register.  */
00722   regcache_raw_read (regcache, BFIN_ASTAT_REGNUM, buf);
00723   buf[0] = (buf[0] & ~ASTAT_CC) | ((buffer[0] & 1) << ASTAT_CC_POS);
00724   regcache_raw_write (regcache, BFIN_ASTAT_REGNUM, buf);
00725 }
00726 
00727 static CORE_ADDR
00728 bfin_frame_base_address (struct frame_info *this_frame, void **this_cache)
00729 {
00730   struct bfin_frame_cache *cache = bfin_frame_cache (this_frame, this_cache);
00731 
00732   return cache->base;
00733 }
00734 
00735 static CORE_ADDR
00736 bfin_frame_local_address (struct frame_info *this_frame, void **this_cache)
00737 {
00738   struct bfin_frame_cache *cache = bfin_frame_cache (this_frame, this_cache);
00739 
00740   return cache->base - 4;
00741 }
00742 
00743 static CORE_ADDR
00744 bfin_frame_args_address (struct frame_info *this_frame, void **this_cache)
00745 {
00746   struct bfin_frame_cache *cache = bfin_frame_cache (this_frame, this_cache);
00747 
00748   return cache->base + 8;
00749 }
00750 
00751 static const struct frame_base bfin_frame_base =
00752 {
00753   &bfin_frame_unwind,
00754   bfin_frame_base_address,
00755   bfin_frame_local_address,
00756   bfin_frame_args_address
00757 };
00758 
00759 static struct frame_id
00760 bfin_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
00761 {
00762   CORE_ADDR sp;
00763 
00764   sp = get_frame_register_unsigned (this_frame, BFIN_SP_REGNUM);
00765 
00766   return frame_id_build (sp, get_frame_pc (this_frame));
00767 }
00768 
00769 static CORE_ADDR
00770 bfin_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
00771 {
00772   return frame_unwind_register_unsigned (next_frame, BFIN_PC_REGNUM);
00773 }
00774 
00775 static CORE_ADDR
00776 bfin_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
00777 {
00778   return (address & ~0x3);
00779 }
00780 
00781 enum bfin_abi
00782 bfin_abi (struct gdbarch *gdbarch)
00783 {
00784   return gdbarch_tdep (gdbarch)->bfin_abi;
00785 }
00786 
00787 /* Initialize the current architecture based on INFO.  If possible,
00788    re-use an architecture from ARCHES, which is a list of
00789    architectures already created during this debugging session.
00790 
00791    Called e.g. at program startup, when reading a core file, and when
00792    reading a binary file.  */
00793 
00794 static struct gdbarch *
00795 bfin_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
00796 {
00797   struct gdbarch_tdep *tdep;
00798   struct gdbarch *gdbarch;
00799   int elf_flags;
00800   enum bfin_abi abi;
00801 
00802   /* Extract the ELF flags, if available.  */
00803   if (info.abfd && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
00804     elf_flags = elf_elfheader (info.abfd)->e_flags;
00805   else
00806     elf_flags = 0;
00807 
00808   abi = BFIN_ABI_FLAT;
00809 
00810   /* If there is already a candidate, use it.  */
00811 
00812   for (arches = gdbarch_list_lookup_by_info (arches, &info);
00813        arches != NULL;
00814        arches = gdbarch_list_lookup_by_info (arches->next, &info))
00815     {
00816       if (gdbarch_tdep (arches->gdbarch)->bfin_abi != abi)
00817         continue;
00818       return arches->gdbarch;
00819     }
00820 
00821   tdep = XMALLOC (struct gdbarch_tdep);
00822   gdbarch = gdbarch_alloc (&info, tdep);
00823 
00824   tdep->bfin_abi = abi;
00825 
00826   set_gdbarch_num_regs (gdbarch, BFIN_NUM_REGS);
00827   set_gdbarch_pseudo_register_read (gdbarch, bfin_pseudo_register_read);
00828   set_gdbarch_pseudo_register_write (gdbarch, bfin_pseudo_register_write);
00829   set_gdbarch_num_pseudo_regs (gdbarch, BFIN_NUM_PSEUDO_REGS);
00830   set_gdbarch_sp_regnum (gdbarch, BFIN_SP_REGNUM);
00831   set_gdbarch_pc_regnum (gdbarch, BFIN_PC_REGNUM);
00832   set_gdbarch_ps_regnum (gdbarch, BFIN_ASTAT_REGNUM);
00833   set_gdbarch_dwarf2_reg_to_regnum (gdbarch, bfin_reg_to_regnum);
00834   set_gdbarch_register_name (gdbarch, bfin_register_name);
00835   set_gdbarch_register_type (gdbarch, bfin_register_type);
00836   set_gdbarch_dummy_id (gdbarch, bfin_dummy_id);
00837   set_gdbarch_push_dummy_call (gdbarch, bfin_push_dummy_call);
00838   set_gdbarch_believe_pcc_promotion (gdbarch, 1);
00839   set_gdbarch_return_value (gdbarch, bfin_return_value);
00840   set_gdbarch_skip_prologue (gdbarch, bfin_skip_prologue);
00841   set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
00842   set_gdbarch_breakpoint_from_pc (gdbarch, bfin_breakpoint_from_pc);
00843   set_gdbarch_decr_pc_after_break (gdbarch, 2);
00844   set_gdbarch_frame_args_skip (gdbarch, 8);
00845   set_gdbarch_unwind_pc (gdbarch, bfin_unwind_pc);
00846   set_gdbarch_frame_align (gdbarch, bfin_frame_align);
00847   set_gdbarch_print_insn (gdbarch, print_insn_bfin);
00848 
00849   /* Hook in ABI-specific overrides, if they have been registered.  */
00850   gdbarch_init_osabi (info, gdbarch);
00851 
00852   dwarf2_append_unwinders (gdbarch);
00853 
00854   frame_base_set_default (gdbarch, &bfin_frame_base);
00855 
00856   frame_unwind_append_unwinder (gdbarch, &bfin_frame_unwind);
00857 
00858   return gdbarch;
00859 }
00860 
00861 /* Provide a prototype to silence -Wmissing-prototypes.  */
00862 extern initialize_file_ftype _initialize_bfin_tdep;
00863 
00864 void
00865 _initialize_bfin_tdep (void)
00866 {
00867   register_gdbarch_init (bfd_arch_bfin, bfin_gdbarch_init);
00868 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines