bfin-tdep.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /* Target-dependent code for Analog Devices Blackfin processor, for GDB.
  2. Copyright (C) 2005-2022 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. /* Big enough to hold the size of the largest register in bytes. */
  221. #define BFIN_MAX_REGISTER_SIZE 4
  222. struct bfin_frame_cache
  223. {
  224. /* Base address. */
  225. CORE_ADDR base;
  226. CORE_ADDR sp_offset;
  227. CORE_ADDR pc;
  228. int frameless_pc_value;
  229. /* Saved registers. */
  230. CORE_ADDR saved_regs[BFIN_NUM_REGS];
  231. CORE_ADDR saved_sp;
  232. /* Stack space reserved for local variables. */
  233. long locals;
  234. };
  235. /* Allocate and initialize a frame cache. */
  236. static struct bfin_frame_cache *
  237. bfin_alloc_frame_cache (void)
  238. {
  239. struct bfin_frame_cache *cache;
  240. int i;
  241. cache = FRAME_OBSTACK_ZALLOC (struct bfin_frame_cache);
  242. /* Base address. */
  243. cache->base = 0;
  244. cache->sp_offset = -4;
  245. cache->pc = 0;
  246. cache->frameless_pc_value = 0;
  247. /* Saved registers. We initialize these to -1 since zero is a valid
  248. offset (that's where fp is supposed to be stored). */
  249. for (i = 0; i < BFIN_NUM_REGS; i++)
  250. cache->saved_regs[i] = -1;
  251. /* Frameless until proven otherwise. */
  252. cache->locals = -1;
  253. return cache;
  254. }
  255. static struct bfin_frame_cache *
  256. bfin_frame_cache (struct frame_info *this_frame, void **this_cache)
  257. {
  258. struct bfin_frame_cache *cache;
  259. int i;
  260. if (*this_cache)
  261. return (struct bfin_frame_cache *) *this_cache;
  262. cache = bfin_alloc_frame_cache ();
  263. *this_cache = cache;
  264. cache->base = get_frame_register_unsigned (this_frame, BFIN_FP_REGNUM);
  265. if (cache->base == 0)
  266. return cache;
  267. /* For normal frames, PC is stored at [FP + 4]. */
  268. cache->saved_regs[BFIN_PC_REGNUM] = 4;
  269. cache->saved_regs[BFIN_FP_REGNUM] = 0;
  270. /* Adjust all the saved registers such that they contain addresses
  271. instead of offsets. */
  272. for (i = 0; i < BFIN_NUM_REGS; i++)
  273. if (cache->saved_regs[i] != -1)
  274. cache->saved_regs[i] += cache->base;
  275. cache->pc = get_frame_func (this_frame) ;
  276. if (cache->pc == 0 || cache->pc == get_frame_pc (this_frame))
  277. {
  278. /* Either there is no prologue (frameless function) or we are at
  279. the start of a function. In short we do not have a frame.
  280. PC is stored in rets register. FP points to previous frame. */
  281. cache->saved_regs[BFIN_PC_REGNUM] =
  282. get_frame_register_unsigned (this_frame, BFIN_RETS_REGNUM);
  283. cache->frameless_pc_value = 1;
  284. cache->base = get_frame_register_unsigned (this_frame, BFIN_FP_REGNUM);
  285. cache->saved_regs[BFIN_FP_REGNUM] = cache->base;
  286. cache->saved_sp = cache->base;
  287. }
  288. else
  289. {
  290. cache->frameless_pc_value = 0;
  291. /* Now that we have the base address for the stack frame we can
  292. calculate the value of SP in the calling frame. */
  293. cache->saved_sp = cache->base + 8;
  294. }
  295. return cache;
  296. }
  297. static void
  298. bfin_frame_this_id (struct frame_info *this_frame,
  299. void **this_cache,
  300. struct frame_id *this_id)
  301. {
  302. struct bfin_frame_cache *cache = bfin_frame_cache (this_frame, this_cache);
  303. /* This marks the outermost frame. */
  304. if (cache->base == 0)
  305. return;
  306. /* See the end of bfin_push_dummy_call. */
  307. *this_id = frame_id_build (cache->base + 8, cache->pc);
  308. }
  309. static struct value *
  310. bfin_frame_prev_register (struct frame_info *this_frame,
  311. void **this_cache,
  312. int regnum)
  313. {
  314. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  315. struct bfin_frame_cache *cache = bfin_frame_cache (this_frame, this_cache);
  316. if (regnum == gdbarch_sp_regnum (gdbarch) && cache->saved_sp)
  317. return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
  318. if (regnum < BFIN_NUM_REGS && cache->saved_regs[regnum] != -1)
  319. return frame_unwind_got_memory (this_frame, regnum,
  320. cache->saved_regs[regnum]);
  321. return frame_unwind_got_register (this_frame, regnum, regnum);
  322. }
  323. static const struct frame_unwind bfin_frame_unwind =
  324. {
  325. "bfin prologue",
  326. NORMAL_FRAME,
  327. default_frame_unwind_stop_reason,
  328. bfin_frame_this_id,
  329. bfin_frame_prev_register,
  330. NULL,
  331. default_frame_sniffer
  332. };
  333. /* Check for "[--SP] = <reg>;" insns. These are appear in function
  334. prologues to save misc registers onto the stack. */
  335. static int
  336. is_minus_minus_sp (int op)
  337. {
  338. op &= 0xFFC0;
  339. if ((op == P_MINUS_SP1) || (op == P_MINUS_SP2)
  340. || (op == P_MINUS_SP3) || (op == P_MINUS_SP4))
  341. return 1;
  342. return 0;
  343. }
  344. /* Skip all the insns that appear in generated function prologues. */
  345. static CORE_ADDR
  346. bfin_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  347. {
  348. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  349. int op = read_memory_unsigned_integer (pc, 2, byte_order);
  350. CORE_ADDR orig_pc = pc;
  351. int done = 0;
  352. /* The new gcc prologue generates the register saves BEFORE the link
  353. or RETS saving instruction.
  354. So, our job is to stop either at those instructions or some upper
  355. limit saying there is no frame! */
  356. while (!done)
  357. {
  358. if (is_minus_minus_sp (op))
  359. {
  360. while (is_minus_minus_sp (op))
  361. {
  362. pc += 2;
  363. op = read_memory_unsigned_integer (pc, 2, byte_order);
  364. }
  365. if (op == P_LINKAGE)
  366. pc += 4;
  367. done = 1;
  368. }
  369. else if (op == P_LINKAGE)
  370. {
  371. pc += 4;
  372. done = 1;
  373. }
  374. else if (op == P_MINUS_MINUS_SP_EQ_RETS)
  375. {
  376. pc += 2;
  377. done = 1;
  378. }
  379. else if (op == P_RTS)
  380. {
  381. done = 1;
  382. }
  383. else if ((op >= P_JUMP_PREG_MIN && op <= P_JUMP_PREG_MAX)
  384. || (op >= P_JUMP_PC_PLUS_PREG_MIN
  385. && op <= P_JUMP_PC_PLUS_PREG_MAX)
  386. || (op == P_JUMP_S_MIN && op <= P_JUMP_S_MAX))
  387. {
  388. done = 1;
  389. }
  390. else if (pc - orig_pc >= UPPER_LIMIT)
  391. {
  392. warning (_("Function Prologue not recognised; "
  393. "pc will point to ENTRY_POINT of the function"));
  394. pc = orig_pc + 2;
  395. done = 1;
  396. }
  397. else
  398. {
  399. pc += 2; /* Not a terminating instruction go on. */
  400. op = read_memory_unsigned_integer (pc, 2, byte_order);
  401. }
  402. }
  403. /* TODO:
  404. Dwarf2 uses entry point value AFTER some register initializations.
  405. We should perhaps skip such asssignments as well (R6 = R1, ...). */
  406. return pc;
  407. }
  408. /* Return the GDB type object for the "standard" data type of data in
  409. register N. This should be void pointer for P0-P5, SP, FP;
  410. void pointer to function for PC; int otherwise. */
  411. static struct type *
  412. bfin_register_type (struct gdbarch *gdbarch, int regnum)
  413. {
  414. if ((regnum >= BFIN_P0_REGNUM && regnum <= BFIN_FP_REGNUM)
  415. || regnum == BFIN_USP_REGNUM)
  416. return builtin_type (gdbarch)->builtin_data_ptr;
  417. if (regnum == BFIN_PC_REGNUM || regnum == BFIN_RETS_REGNUM
  418. || regnum == BFIN_RETI_REGNUM || regnum == BFIN_RETX_REGNUM
  419. || regnum == BFIN_RETN_REGNUM || regnum == BFIN_RETE_REGNUM
  420. || regnum == BFIN_LT0_REGNUM || regnum == BFIN_LB0_REGNUM
  421. || regnum == BFIN_LT1_REGNUM || regnum == BFIN_LB1_REGNUM)
  422. return builtin_type (gdbarch)->builtin_func_ptr;
  423. return builtin_type (gdbarch)->builtin_int32;
  424. }
  425. static CORE_ADDR
  426. bfin_push_dummy_call (struct gdbarch *gdbarch,
  427. struct value *function,
  428. struct regcache *regcache,
  429. CORE_ADDR bp_addr,
  430. int nargs,
  431. struct value **args,
  432. CORE_ADDR sp,
  433. function_call_return_method return_method,
  434. CORE_ADDR struct_addr)
  435. {
  436. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  437. int i;
  438. long reg_r0, reg_r1, reg_r2;
  439. int total_len = 0;
  440. for (i = nargs - 1; i >= 0; i--)
  441. {
  442. struct type *value_type = value_enclosing_type (args[i]);
  443. total_len += align_up (TYPE_LENGTH (value_type), 4);
  444. }
  445. /* At least twelve bytes of stack space must be allocated for the function's
  446. arguments, even for functions that have less than 12 bytes of argument
  447. data. */
  448. if (total_len < 12)
  449. sp -= 12 - total_len;
  450. /* Push arguments in reverse order. */
  451. for (i = nargs - 1; i >= 0; i--)
  452. {
  453. struct type *value_type = value_enclosing_type (args[i]);
  454. struct type *arg_type = check_typedef (value_type);
  455. int container_len = align_up (TYPE_LENGTH (arg_type), 4);
  456. sp -= container_len;
  457. write_memory (sp, value_contents (args[i]).data (), container_len);
  458. }
  459. /* Initialize R0, R1, and R2 to the first 3 words of parameters. */
  460. reg_r0 = read_memory_integer (sp, 4, byte_order);
  461. regcache_cooked_write_unsigned (regcache, BFIN_R0_REGNUM, reg_r0);
  462. reg_r1 = read_memory_integer (sp + 4, 4, byte_order);
  463. regcache_cooked_write_unsigned (regcache, BFIN_R1_REGNUM, reg_r1);
  464. reg_r2 = read_memory_integer (sp + 8, 4, byte_order);
  465. regcache_cooked_write_unsigned (regcache, BFIN_R2_REGNUM, reg_r2);
  466. /* Store struct value address. */
  467. if (return_method == return_method_struct)
  468. regcache_cooked_write_unsigned (regcache, BFIN_P0_REGNUM, struct_addr);
  469. /* Set the dummy return value to bp_addr.
  470. A dummy breakpoint will be setup to execute the call. */
  471. regcache_cooked_write_unsigned (regcache, BFIN_RETS_REGNUM, bp_addr);
  472. /* Finally, update the stack pointer. */
  473. regcache_cooked_write_unsigned (regcache, BFIN_SP_REGNUM, sp);
  474. return sp;
  475. }
  476. /* Convert DWARF2 register number REG to the appropriate register number
  477. used by GDB. */
  478. static int
  479. bfin_reg_to_regnum (struct gdbarch *gdbarch, int reg)
  480. {
  481. if (reg < 0 || reg >= ARRAY_SIZE (map_gcc_gdb))
  482. return -1;
  483. return map_gcc_gdb[reg];
  484. }
  485. /* Implement the breakpoint_kind_from_pc gdbarch method. */
  486. static int
  487. bfin_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
  488. {
  489. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  490. unsigned short iw;
  491. iw = read_memory_unsigned_integer (*pcptr, 2, byte_order);
  492. if ((iw & 0xf000) >= 0xc000)
  493. /* 32-bit instruction. */
  494. return 4;
  495. else
  496. return 2;
  497. }
  498. /* Implement the sw_breakpoint_from_kind gdbarch method. */
  499. static const gdb_byte *
  500. bfin_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
  501. {
  502. static unsigned char bfin_breakpoint[] = {0xa1, 0x00, 0x00, 0x00};
  503. static unsigned char bfin_sim_breakpoint[] = {0x25, 0x00, 0x00, 0x00};
  504. *size = kind;
  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 = regs->arch ();
  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. regs->cooked_write (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, readable_regcache *regcache,
  580. int regnum, gdb_byte *buffer)
  581. {
  582. gdb_byte buf[BFIN_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 (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[BFIN_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 (BFIN_ASTAT_REGNUM, buf);
  606. buf[0] = (buf[0] & ~ASTAT_CC) | ((buffer[0] & 1) << ASTAT_CC_POS);
  607. regcache->raw_write (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 CORE_ADDR
  635. bfin_frame_align (struct gdbarch *gdbarch, CORE_ADDR address)
  636. {
  637. return align_down (address, 4);
  638. }
  639. enum bfin_abi
  640. bfin_abi (struct gdbarch *gdbarch)
  641. {
  642. bfin_gdbarch_tdep *tdep = (bfin_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  643. return tdep->bfin_abi;
  644. }
  645. /* Initialize the current architecture based on INFO. If possible,
  646. re-use an architecture from ARCHES, which is a list of
  647. architectures already created during this debugging session.
  648. Called e.g. at program startup, when reading a core file, and when
  649. reading a binary file. */
  650. static struct gdbarch *
  651. bfin_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  652. {
  653. struct gdbarch *gdbarch;
  654. enum bfin_abi abi;
  655. abi = BFIN_ABI_FLAT;
  656. /* If there is already a candidate, use it. */
  657. for (arches = gdbarch_list_lookup_by_info (arches, &info);
  658. arches != NULL;
  659. arches = gdbarch_list_lookup_by_info (arches->next, &info))
  660. {
  661. bfin_gdbarch_tdep *tdep
  662. = (bfin_gdbarch_tdep *) gdbarch_tdep (arches->gdbarch);
  663. if (tdep->bfin_abi != abi)
  664. continue;
  665. return arches->gdbarch;
  666. }
  667. bfin_gdbarch_tdep *tdep = new bfin_gdbarch_tdep;
  668. gdbarch = gdbarch_alloc (&info, tdep);
  669. tdep->bfin_abi = abi;
  670. set_gdbarch_num_regs (gdbarch, BFIN_NUM_REGS);
  671. set_gdbarch_pseudo_register_read (gdbarch, bfin_pseudo_register_read);
  672. set_gdbarch_pseudo_register_write (gdbarch, bfin_pseudo_register_write);
  673. set_gdbarch_num_pseudo_regs (gdbarch, BFIN_NUM_PSEUDO_REGS);
  674. set_gdbarch_sp_regnum (gdbarch, BFIN_SP_REGNUM);
  675. set_gdbarch_pc_regnum (gdbarch, BFIN_PC_REGNUM);
  676. set_gdbarch_ps_regnum (gdbarch, BFIN_ASTAT_REGNUM);
  677. set_gdbarch_dwarf2_reg_to_regnum (gdbarch, bfin_reg_to_regnum);
  678. set_gdbarch_register_name (gdbarch, bfin_register_name);
  679. set_gdbarch_register_type (gdbarch, bfin_register_type);
  680. set_gdbarch_push_dummy_call (gdbarch, bfin_push_dummy_call);
  681. set_gdbarch_believe_pcc_promotion (gdbarch, 1);
  682. set_gdbarch_return_value (gdbarch, bfin_return_value);
  683. set_gdbarch_skip_prologue (gdbarch, bfin_skip_prologue);
  684. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  685. set_gdbarch_breakpoint_kind_from_pc (gdbarch, bfin_breakpoint_kind_from_pc);
  686. set_gdbarch_sw_breakpoint_from_kind (gdbarch, bfin_sw_breakpoint_from_kind);
  687. set_gdbarch_decr_pc_after_break (gdbarch, 2);
  688. set_gdbarch_frame_args_skip (gdbarch, 8);
  689. set_gdbarch_frame_align (gdbarch, bfin_frame_align);
  690. /* Hook in ABI-specific overrides, if they have been registered. */
  691. gdbarch_init_osabi (info, gdbarch);
  692. dwarf2_append_unwinders (gdbarch);
  693. frame_base_set_default (gdbarch, &bfin_frame_base);
  694. frame_unwind_append_unwinder (gdbarch, &bfin_frame_unwind);
  695. return gdbarch;
  696. }
  697. void _initialize_bfin_tdep ();
  698. void
  699. _initialize_bfin_tdep ()
  700. {
  701. register_gdbarch_init (bfd_arch_bfin, bfin_gdbarch_init);
  702. }