xstormy16-tdep.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /* Target-dependent code for the Sanyo Xstormy16a (LC590000) processor.
  2. Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "frame.h"
  16. #include "frame-base.h"
  17. #include "frame-unwind.h"
  18. #include "dwarf2/frame.h"
  19. #include "symtab.h"
  20. #include "gdbtypes.h"
  21. #include "gdbcmd.h"
  22. #include "gdbcore.h"
  23. #include "value.h"
  24. #include "dis-asm.h"
  25. #include "inferior.h"
  26. #include "arch-utils.h"
  27. #include "regcache.h"
  28. #include "osabi.h"
  29. #include "objfiles.h"
  30. #include "gdbsupport/byte-vector.h"
  31. enum gdb_regnum
  32. {
  33. /* Xstormy16 has 16 general purpose registers (R0-R15) plus PC.
  34. Functions will return their values in register R2-R7 as they fit.
  35. Otherwise a hidden pointer to an big enough area is given as argument
  36. to the function in r2. Further arguments are beginning in r3 then.
  37. R13 is used as frame pointer when GCC compiles w/o optimization
  38. R14 is used as "PSW", displaying the CPU status.
  39. R15 is used implicitly as stack pointer. */
  40. E_R0_REGNUM,
  41. E_R1_REGNUM,
  42. E_R2_REGNUM, E_1ST_ARG_REGNUM = E_R2_REGNUM, E_PTR_RET_REGNUM = E_R2_REGNUM,
  43. E_R3_REGNUM,
  44. E_R4_REGNUM,
  45. E_R5_REGNUM,
  46. E_R6_REGNUM,
  47. E_R7_REGNUM, E_LST_ARG_REGNUM = E_R7_REGNUM,
  48. E_R8_REGNUM,
  49. E_R9_REGNUM,
  50. E_R10_REGNUM,
  51. E_R11_REGNUM,
  52. E_R12_REGNUM,
  53. E_R13_REGNUM, E_FP_REGNUM = E_R13_REGNUM,
  54. E_R14_REGNUM, E_PSW_REGNUM = E_R14_REGNUM,
  55. E_R15_REGNUM, E_SP_REGNUM = E_R15_REGNUM,
  56. E_PC_REGNUM,
  57. E_NUM_REGS
  58. };
  59. /* Use an invalid address value as 'not available' marker. */
  60. enum { REG_UNAVAIL = (CORE_ADDR) -1 };
  61. struct xstormy16_frame_cache
  62. {
  63. /* Base address. */
  64. CORE_ADDR base;
  65. CORE_ADDR pc;
  66. LONGEST framesize;
  67. int uses_fp;
  68. CORE_ADDR saved_regs[E_NUM_REGS];
  69. CORE_ADDR saved_sp;
  70. };
  71. /* Size of instructions, registers, etc. */
  72. enum
  73. {
  74. xstormy16_inst_size = 2,
  75. xstormy16_reg_size = 2,
  76. xstormy16_pc_size = 4
  77. };
  78. /* Size of return datatype which fits into the remaining return registers. */
  79. #define E_MAX_RETTYPE_SIZE(regnum) ((E_LST_ARG_REGNUM - (regnum) + 1) \
  80. * xstormy16_reg_size)
  81. /* Size of return datatype which fits into all return registers. */
  82. enum
  83. {
  84. E_MAX_RETTYPE_SIZE_IN_REGS = E_MAX_RETTYPE_SIZE (E_R2_REGNUM)
  85. };
  86. /* Function: xstormy16_register_name
  87. Returns the name of the standard Xstormy16 register N. */
  88. static const char *
  89. xstormy16_register_name (struct gdbarch *gdbarch, int regnum)
  90. {
  91. static const char *register_names[] = {
  92. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  93. "r8", "r9", "r10", "r11", "r12", "r13",
  94. "psw", "sp", "pc"
  95. };
  96. if (regnum < 0 || regnum >= E_NUM_REGS)
  97. internal_error (__FILE__, __LINE__,
  98. _("xstormy16_register_name: illegal register number %d"),
  99. regnum);
  100. else
  101. return register_names[regnum];
  102. }
  103. static struct type *
  104. xstormy16_register_type (struct gdbarch *gdbarch, int regnum)
  105. {
  106. if (regnum == E_PC_REGNUM)
  107. return builtin_type (gdbarch)->builtin_uint32;
  108. else
  109. return builtin_type (gdbarch)->builtin_uint16;
  110. }
  111. /* Function: xstormy16_type_is_scalar
  112. Makes the decision if a given type is a scalar types. Scalar
  113. types are returned in the registers r2-r7 as they fit. */
  114. static int
  115. xstormy16_type_is_scalar (struct type *t)
  116. {
  117. return (t->code () != TYPE_CODE_STRUCT
  118. && t->code () != TYPE_CODE_UNION
  119. && t->code () != TYPE_CODE_ARRAY);
  120. }
  121. /* Function: xstormy16_use_struct_convention
  122. Returns non-zero if the given struct type will be returned using
  123. a special convention, rather than the normal function return method.
  124. 7sed in the contexts of the "return" command, and of
  125. target function calls from the debugger. */
  126. static int
  127. xstormy16_use_struct_convention (struct type *type)
  128. {
  129. return !xstormy16_type_is_scalar (type)
  130. || TYPE_LENGTH (type) > E_MAX_RETTYPE_SIZE_IN_REGS;
  131. }
  132. /* Function: xstormy16_extract_return_value
  133. Find a function's return value in the appropriate registers (in
  134. regbuf), and copy it into valbuf. */
  135. static void
  136. xstormy16_extract_return_value (struct type *type, struct regcache *regcache,
  137. gdb_byte *valbuf)
  138. {
  139. int len = TYPE_LENGTH (type);
  140. int i, regnum = E_1ST_ARG_REGNUM;
  141. for (i = 0; i < len; i += xstormy16_reg_size)
  142. regcache->raw_read (regnum++, valbuf + i);
  143. }
  144. /* Function: xstormy16_store_return_value
  145. Copy the function return value from VALBUF into the
  146. proper location for a function return.
  147. Called only in the context of the "return" command. */
  148. static void
  149. xstormy16_store_return_value (struct type *type, struct regcache *regcache,
  150. const gdb_byte *valbuf)
  151. {
  152. if (TYPE_LENGTH (type) == 1)
  153. {
  154. /* Add leading zeros to the value. */
  155. gdb_byte buf[xstormy16_reg_size];
  156. memset (buf, 0, xstormy16_reg_size);
  157. memcpy (buf, valbuf, 1);
  158. regcache->raw_write (E_1ST_ARG_REGNUM, buf);
  159. }
  160. else
  161. {
  162. int len = TYPE_LENGTH (type);
  163. int i, regnum = E_1ST_ARG_REGNUM;
  164. for (i = 0; i < len; i += xstormy16_reg_size)
  165. regcache->raw_write (regnum++, valbuf + i);
  166. }
  167. }
  168. static enum return_value_convention
  169. xstormy16_return_value (struct gdbarch *gdbarch, struct value *function,
  170. struct type *type, struct regcache *regcache,
  171. gdb_byte *readbuf, const gdb_byte *writebuf)
  172. {
  173. if (xstormy16_use_struct_convention (type))
  174. return RETURN_VALUE_STRUCT_CONVENTION;
  175. if (writebuf)
  176. xstormy16_store_return_value (type, regcache, writebuf);
  177. else if (readbuf)
  178. xstormy16_extract_return_value (type, regcache, readbuf);
  179. return RETURN_VALUE_REGISTER_CONVENTION;
  180. }
  181. static CORE_ADDR
  182. xstormy16_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
  183. {
  184. if (addr & 1)
  185. ++addr;
  186. return addr;
  187. }
  188. /* Function: xstormy16_push_dummy_call
  189. Setup the function arguments for GDB to call a function in the inferior.
  190. Called only in the context of a target function call from the debugger.
  191. Returns the value of the SP register after the args are pushed. */
  192. static CORE_ADDR
  193. xstormy16_push_dummy_call (struct gdbarch *gdbarch,
  194. struct value *function,
  195. struct regcache *regcache,
  196. CORE_ADDR bp_addr, int nargs,
  197. struct value **args,
  198. CORE_ADDR sp,
  199. function_call_return_method return_method,
  200. CORE_ADDR struct_addr)
  201. {
  202. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  203. CORE_ADDR stack_dest = sp;
  204. int argreg = E_1ST_ARG_REGNUM;
  205. int i, j;
  206. int typelen, slacklen;
  207. gdb_byte buf[xstormy16_pc_size];
  208. /* If returning a struct using target ABI method, then the struct return
  209. address will consume one argument-passing register. */
  210. if (return_method == return_method_struct)
  211. {
  212. regcache_cooked_write_unsigned (regcache, E_PTR_RET_REGNUM, struct_addr);
  213. argreg++;
  214. }
  215. /* Arguments are passed in R2-R7 as they fit. If an argument doesn't
  216. fit in the remaining registers we're switching over to the stack.
  217. No argument is put on stack partially and as soon as we switched
  218. over to stack no further argument is put in a register even if it
  219. would fit in the remaining unused registers. */
  220. for (i = 0; i < nargs && argreg <= E_LST_ARG_REGNUM; i++)
  221. {
  222. typelen = TYPE_LENGTH (value_enclosing_type (args[i]));
  223. if (typelen > E_MAX_RETTYPE_SIZE (argreg))
  224. break;
  225. /* Put argument into registers wordwise. */
  226. const gdb_byte *val = value_contents (args[i]).data ();
  227. for (j = 0; j < typelen; j += xstormy16_reg_size)
  228. {
  229. ULONGEST regval;
  230. int size = (typelen - j == 1) ? 1 : xstormy16_reg_size;
  231. regval = extract_unsigned_integer (val + j, size, byte_order);
  232. regcache_cooked_write_unsigned (regcache, argreg++, regval);
  233. }
  234. }
  235. /* Align SP */
  236. stack_dest = xstormy16_frame_align (gdbarch, stack_dest);
  237. /* Loop backwards through remaining arguments and push them on the stack,
  238. wordaligned. */
  239. for (j = nargs - 1; j >= i; j--)
  240. {
  241. const gdb_byte *bytes = value_contents (args[j]).data ();
  242. typelen = TYPE_LENGTH (value_enclosing_type (args[j]));
  243. slacklen = typelen & 1;
  244. gdb::byte_vector val (typelen + slacklen);
  245. memcpy (val.data (), bytes, typelen);
  246. memset (val.data () + typelen, 0, slacklen);
  247. /* Now write this data to the stack. The stack grows upwards. */
  248. write_memory (stack_dest, val.data (), typelen + slacklen);
  249. stack_dest += typelen + slacklen;
  250. }
  251. store_unsigned_integer (buf, xstormy16_pc_size, byte_order, bp_addr);
  252. write_memory (stack_dest, buf, xstormy16_pc_size);
  253. stack_dest += xstormy16_pc_size;
  254. /* Update stack pointer. */
  255. regcache_cooked_write_unsigned (regcache, E_SP_REGNUM, stack_dest);
  256. /* Return the new stack pointer minus the return address slot since
  257. that's what DWARF2/GCC uses as the frame's CFA. */
  258. return stack_dest - xstormy16_pc_size;
  259. }
  260. /* Function: xstormy16_scan_prologue
  261. Decode the instructions within the given address range.
  262. Decide when we must have reached the end of the function prologue.
  263. If a frame_info pointer is provided, fill in its saved_regs etc.
  264. Returns the address of the first instruction after the prologue. */
  265. static CORE_ADDR
  266. xstormy16_analyze_prologue (struct gdbarch *gdbarch,
  267. CORE_ADDR start_addr, CORE_ADDR end_addr,
  268. struct xstormy16_frame_cache *cache,
  269. struct frame_info *this_frame)
  270. {
  271. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  272. CORE_ADDR next_addr;
  273. ULONGEST inst, inst2;
  274. LONGEST offset;
  275. int regnum;
  276. /* Initialize framesize with size of PC put on stack by CALLF inst. */
  277. cache->saved_regs[E_PC_REGNUM] = 0;
  278. cache->framesize = xstormy16_pc_size;
  279. if (start_addr >= end_addr)
  280. return end_addr;
  281. for (next_addr = start_addr;
  282. next_addr < end_addr; next_addr += xstormy16_inst_size)
  283. {
  284. inst = read_memory_unsigned_integer (next_addr,
  285. xstormy16_inst_size, byte_order);
  286. inst2 = read_memory_unsigned_integer (next_addr + xstormy16_inst_size,
  287. xstormy16_inst_size, byte_order);
  288. if (inst >= 0x0082 && inst <= 0x008d) /* push r2 .. push r13 */
  289. {
  290. regnum = inst & 0x000f;
  291. cache->saved_regs[regnum] = cache->framesize;
  292. cache->framesize += xstormy16_reg_size;
  293. }
  294. /* Optional stack allocation for args and local vars <= 4 byte. */
  295. else if (inst == 0x301f || inst == 0x303f) /* inc r15, #0x1/#0x3 */
  296. {
  297. cache->framesize += ((inst & 0x0030) >> 4) + 1;
  298. }
  299. /* optional stack allocation for args and local vars > 4 && < 16 byte */
  300. else if ((inst & 0xff0f) == 0x510f) /* 51Hf add r15, #0xH */
  301. {
  302. cache->framesize += (inst & 0x00f0) >> 4;
  303. }
  304. /* Optional stack allocation for args and local vars >= 16 byte. */
  305. else if (inst == 0x314f && inst2 >= 0x0010) /* 314f HHHH add r15, #0xH */
  306. {
  307. cache->framesize += inst2;
  308. next_addr += xstormy16_inst_size;
  309. }
  310. else if (inst == 0x46fd) /* mov r13, r15 */
  311. {
  312. cache->uses_fp = 1;
  313. }
  314. /* optional copying of args in r2-r7 to r10-r13. */
  315. /* Probably only in optimized case but legal action for prologue. */
  316. else if ((inst & 0xff00) == 0x4600 /* 46SD mov rD, rS */
  317. && (inst & 0x00f0) >= 0x0020 && (inst & 0x00f0) <= 0x0070
  318. && (inst & 0x000f) >= 0x000a && (inst & 0x000f) <= 0x000d)
  319. ;
  320. /* Optional copying of args in r2-r7 to stack. */
  321. /* 72DS HHHH mov.b (rD, 0xHHHH), r(S-8)
  322. (bit3 always 1, bit2-0 = reg) */
  323. /* 73DS HHHH mov.w (rD, 0xHHHH), r(S-8) */
  324. else if ((inst & 0xfed8) == 0x72d8 && (inst & 0x0007) >= 2)
  325. {
  326. regnum = inst & 0x0007;
  327. /* Only 12 of 16 bits of the argument are used for the
  328. signed offset. */
  329. offset = (LONGEST) (inst2 & 0x0fff);
  330. if (offset & 0x0800)
  331. offset -= 0x1000;
  332. cache->saved_regs[regnum] = cache->framesize + offset;
  333. next_addr += xstormy16_inst_size;
  334. }
  335. else /* Not a prologue instruction. */
  336. break;
  337. }
  338. return next_addr;
  339. }
  340. /* Function: xstormy16_skip_prologue
  341. If the input address is in a function prologue,
  342. returns the address of the end of the prologue;
  343. else returns the input address.
  344. Note: the input address is likely to be the function start,
  345. since this function is mainly used for advancing a breakpoint
  346. to the first line, or stepping to the first line when we have
  347. stepped into a function call. */
  348. static CORE_ADDR
  349. xstormy16_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  350. {
  351. CORE_ADDR func_addr = 0, func_end = 0;
  352. const char *func_name;
  353. if (find_pc_partial_function (pc, &func_name, &func_addr, &func_end))
  354. {
  355. struct symtab_and_line sal;
  356. struct symbol *sym;
  357. struct xstormy16_frame_cache cache;
  358. CORE_ADDR plg_end;
  359. memset (&cache, 0, sizeof cache);
  360. /* Don't trust line number debug info in frameless functions. */
  361. plg_end = xstormy16_analyze_prologue (gdbarch, func_addr, func_end,
  362. &cache, NULL);
  363. if (!cache.uses_fp)
  364. return plg_end;
  365. /* Found a function. */
  366. sym = lookup_symbol (func_name, NULL, VAR_DOMAIN, NULL).symbol;
  367. /* Don't use line number debug info for assembly source files. */
  368. if (sym && sym->language () != language_asm)
  369. {
  370. sal = find_pc_line (func_addr, 0);
  371. if (sal.end && sal.end < func_end)
  372. {
  373. /* Found a line number, use it as end of prologue. */
  374. return sal.end;
  375. }
  376. }
  377. /* No useable line symbol. Use result of prologue parsing method. */
  378. return plg_end;
  379. }
  380. /* No function symbol -- just return the PC. */
  381. return (CORE_ADDR) pc;
  382. }
  383. /* Implement the stack_frame_destroyed_p gdbarch method.
  384. The epilogue is defined here as the area at the end of a function,
  385. either on the `ret' instruction itself or after an instruction which
  386. destroys the function's stack frame. */
  387. static int
  388. xstormy16_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  389. {
  390. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  391. CORE_ADDR func_addr = 0, func_end = 0;
  392. if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
  393. {
  394. ULONGEST inst, inst2;
  395. CORE_ADDR addr = func_end - xstormy16_inst_size;
  396. /* The Xstormy16 epilogue is max. 14 bytes long. */
  397. if (pc < func_end - 7 * xstormy16_inst_size)
  398. return 0;
  399. /* Check if we're on a `ret' instruction. Otherwise it's
  400. too dangerous to proceed. */
  401. inst = read_memory_unsigned_integer (addr,
  402. xstormy16_inst_size, byte_order);
  403. if (inst != 0x0003)
  404. return 0;
  405. while ((addr -= xstormy16_inst_size) >= func_addr)
  406. {
  407. inst = read_memory_unsigned_integer (addr,
  408. xstormy16_inst_size,
  409. byte_order);
  410. if (inst >= 0x009a && inst <= 0x009d) /* pop r10...r13 */
  411. continue;
  412. if (inst == 0x305f || inst == 0x307f) /* dec r15, #0x1/#0x3 */
  413. break;
  414. inst2 = read_memory_unsigned_integer (addr - xstormy16_inst_size,
  415. xstormy16_inst_size,
  416. byte_order);
  417. if (inst2 == 0x314f && inst >= 0x8000) /* add r15, neg. value */
  418. {
  419. addr -= xstormy16_inst_size;
  420. break;
  421. }
  422. return 0;
  423. }
  424. if (pc > addr)
  425. return 1;
  426. }
  427. return 0;
  428. }
  429. constexpr gdb_byte xstormy16_break_insn[] = { 0x06, 0x0 };
  430. typedef BP_MANIPULATION (xstormy16_break_insn) xstormy16_breakpoint;
  431. /* Given a pointer to a jump table entry, return the address
  432. of the function it jumps to. Return 0 if not found. */
  433. static CORE_ADDR
  434. xstormy16_resolve_jmp_table_entry (struct gdbarch *gdbarch, CORE_ADDR faddr)
  435. {
  436. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  437. struct obj_section *faddr_sect = find_pc_section (faddr);
  438. if (faddr_sect)
  439. {
  440. LONGEST inst, inst2, addr;
  441. gdb_byte buf[2 * xstormy16_inst_size];
  442. /* Return faddr if it's not pointing into the jump table. */
  443. if (strcmp (faddr_sect->the_bfd_section->name, ".plt"))
  444. return faddr;
  445. if (!target_read_memory (faddr, buf, sizeof buf))
  446. {
  447. inst = extract_unsigned_integer (buf,
  448. xstormy16_inst_size, byte_order);
  449. inst2 = extract_unsigned_integer (buf + xstormy16_inst_size,
  450. xstormy16_inst_size, byte_order);
  451. addr = inst2 << 8 | (inst & 0xff);
  452. return addr;
  453. }
  454. }
  455. return 0;
  456. }
  457. /* Given a function's address, attempt to find (and return) the
  458. address of the corresponding jump table entry. Return 0 if
  459. not found. */
  460. static CORE_ADDR
  461. xstormy16_find_jmp_table_entry (struct gdbarch *gdbarch, CORE_ADDR faddr)
  462. {
  463. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  464. struct obj_section *faddr_sect = find_pc_section (faddr);
  465. if (faddr_sect)
  466. {
  467. struct obj_section *osect;
  468. /* Return faddr if it's already a pointer to a jump table entry. */
  469. if (!strcmp (faddr_sect->the_bfd_section->name, ".plt"))
  470. return faddr;
  471. ALL_OBJFILE_OSECTIONS (faddr_sect->objfile, osect)
  472. {
  473. if (!strcmp (osect->the_bfd_section->name, ".plt"))
  474. break;
  475. }
  476. if (osect < faddr_sect->objfile->sections_end)
  477. {
  478. CORE_ADDR addr, endaddr;
  479. addr = osect->addr ();
  480. endaddr = osect->endaddr ();
  481. for (; addr < endaddr; addr += 2 * xstormy16_inst_size)
  482. {
  483. LONGEST inst, inst2, faddr2;
  484. gdb_byte buf[2 * xstormy16_inst_size];
  485. if (target_read_memory (addr, buf, sizeof buf))
  486. return 0;
  487. inst = extract_unsigned_integer (buf,
  488. xstormy16_inst_size,
  489. byte_order);
  490. inst2 = extract_unsigned_integer (buf + xstormy16_inst_size,
  491. xstormy16_inst_size,
  492. byte_order);
  493. faddr2 = inst2 << 8 | (inst & 0xff);
  494. if (faddr == faddr2)
  495. return addr;
  496. }
  497. }
  498. }
  499. return 0;
  500. }
  501. static CORE_ADDR
  502. xstormy16_skip_trampoline_code (struct frame_info *frame, CORE_ADDR pc)
  503. {
  504. struct gdbarch *gdbarch = get_frame_arch (frame);
  505. CORE_ADDR tmp = xstormy16_resolve_jmp_table_entry (gdbarch, pc);
  506. if (tmp && tmp != pc)
  507. return tmp;
  508. return 0;
  509. }
  510. /* Function pointers are 16 bit. The address space is 24 bit, using
  511. 32 bit addresses. Pointers to functions on the XStormy16 are implemented
  512. by using 16 bit pointers, which are either direct pointers in case the
  513. function begins below 0x10000, or indirect pointers into a jump table.
  514. The next two functions convert 16 bit pointers into 24 (32) bit addresses
  515. and vice versa. */
  516. static CORE_ADDR
  517. xstormy16_pointer_to_address (struct gdbarch *gdbarch,
  518. struct type *type, const gdb_byte *buf)
  519. {
  520. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  521. enum type_code target = TYPE_TARGET_TYPE (type)->code ();
  522. CORE_ADDR addr
  523. = extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);
  524. if (target == TYPE_CODE_FUNC || target == TYPE_CODE_METHOD)
  525. {
  526. CORE_ADDR addr2 = xstormy16_resolve_jmp_table_entry (gdbarch, addr);
  527. if (addr2)
  528. addr = addr2;
  529. }
  530. return addr;
  531. }
  532. static void
  533. xstormy16_address_to_pointer (struct gdbarch *gdbarch,
  534. struct type *type, gdb_byte *buf, CORE_ADDR addr)
  535. {
  536. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  537. enum type_code target = TYPE_TARGET_TYPE (type)->code ();
  538. if (target == TYPE_CODE_FUNC || target == TYPE_CODE_METHOD)
  539. {
  540. CORE_ADDR addr2 = xstormy16_find_jmp_table_entry (gdbarch, addr);
  541. if (addr2)
  542. addr = addr2;
  543. }
  544. store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order, addr);
  545. }
  546. static struct xstormy16_frame_cache *
  547. xstormy16_alloc_frame_cache (void)
  548. {
  549. struct xstormy16_frame_cache *cache;
  550. int i;
  551. cache = FRAME_OBSTACK_ZALLOC (struct xstormy16_frame_cache);
  552. cache->base = 0;
  553. cache->saved_sp = 0;
  554. cache->pc = 0;
  555. cache->uses_fp = 0;
  556. cache->framesize = 0;
  557. for (i = 0; i < E_NUM_REGS; ++i)
  558. cache->saved_regs[i] = REG_UNAVAIL;
  559. return cache;
  560. }
  561. static struct xstormy16_frame_cache *
  562. xstormy16_frame_cache (struct frame_info *this_frame, void **this_cache)
  563. {
  564. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  565. struct xstormy16_frame_cache *cache;
  566. CORE_ADDR current_pc;
  567. int i;
  568. if (*this_cache)
  569. return (struct xstormy16_frame_cache *) *this_cache;
  570. cache = xstormy16_alloc_frame_cache ();
  571. *this_cache = cache;
  572. cache->base = get_frame_register_unsigned (this_frame, E_FP_REGNUM);
  573. if (cache->base == 0)
  574. return cache;
  575. cache->pc = get_frame_func (this_frame);
  576. current_pc = get_frame_pc (this_frame);
  577. if (cache->pc)
  578. xstormy16_analyze_prologue (gdbarch, cache->pc, current_pc,
  579. cache, this_frame);
  580. if (!cache->uses_fp)
  581. cache->base = get_frame_register_unsigned (this_frame, E_SP_REGNUM);
  582. cache->saved_sp = cache->base - cache->framesize;
  583. for (i = 0; i < E_NUM_REGS; ++i)
  584. if (cache->saved_regs[i] != REG_UNAVAIL)
  585. cache->saved_regs[i] += cache->saved_sp;
  586. return cache;
  587. }
  588. static struct value *
  589. xstormy16_frame_prev_register (struct frame_info *this_frame,
  590. void **this_cache, int regnum)
  591. {
  592. struct xstormy16_frame_cache *cache = xstormy16_frame_cache (this_frame,
  593. this_cache);
  594. gdb_assert (regnum >= 0);
  595. if (regnum == E_SP_REGNUM && cache->saved_sp)
  596. return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
  597. if (regnum < E_NUM_REGS && cache->saved_regs[regnum] != REG_UNAVAIL)
  598. return frame_unwind_got_memory (this_frame, regnum,
  599. cache->saved_regs[regnum]);
  600. return frame_unwind_got_register (this_frame, regnum, regnum);
  601. }
  602. static void
  603. xstormy16_frame_this_id (struct frame_info *this_frame, void **this_cache,
  604. struct frame_id *this_id)
  605. {
  606. struct xstormy16_frame_cache *cache = xstormy16_frame_cache (this_frame,
  607. this_cache);
  608. /* This marks the outermost frame. */
  609. if (cache->base == 0)
  610. return;
  611. *this_id = frame_id_build (cache->saved_sp, cache->pc);
  612. }
  613. static CORE_ADDR
  614. xstormy16_frame_base_address (struct frame_info *this_frame, void **this_cache)
  615. {
  616. struct xstormy16_frame_cache *cache = xstormy16_frame_cache (this_frame,
  617. this_cache);
  618. return cache->base;
  619. }
  620. static const struct frame_unwind xstormy16_frame_unwind = {
  621. "xstormy16 prologue",
  622. NORMAL_FRAME,
  623. default_frame_unwind_stop_reason,
  624. xstormy16_frame_this_id,
  625. xstormy16_frame_prev_register,
  626. NULL,
  627. default_frame_sniffer
  628. };
  629. static const struct frame_base xstormy16_frame_base = {
  630. &xstormy16_frame_unwind,
  631. xstormy16_frame_base_address,
  632. xstormy16_frame_base_address,
  633. xstormy16_frame_base_address
  634. };
  635. /* Function: xstormy16_gdbarch_init
  636. Initializer function for the xstormy16 gdbarch vector.
  637. Called by gdbarch. Sets up the gdbarch vector(s) for this target. */
  638. static struct gdbarch *
  639. xstormy16_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  640. {
  641. struct gdbarch *gdbarch;
  642. /* find a candidate among the list of pre-declared architectures. */
  643. arches = gdbarch_list_lookup_by_info (arches, &info);
  644. if (arches != NULL)
  645. return (arches->gdbarch);
  646. gdbarch = gdbarch_alloc (&info, NULL);
  647. /*
  648. * Basic register fields and methods, datatype sizes and stuff.
  649. */
  650. set_gdbarch_num_regs (gdbarch, E_NUM_REGS);
  651. set_gdbarch_num_pseudo_regs (gdbarch, 0);
  652. set_gdbarch_sp_regnum (gdbarch, E_SP_REGNUM);
  653. set_gdbarch_pc_regnum (gdbarch, E_PC_REGNUM);
  654. set_gdbarch_register_name (gdbarch, xstormy16_register_name);
  655. set_gdbarch_register_type (gdbarch, xstormy16_register_type);
  656. set_gdbarch_char_signed (gdbarch, 0);
  657. set_gdbarch_short_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  658. set_gdbarch_int_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  659. set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  660. set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
  661. set_gdbarch_wchar_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  662. set_gdbarch_wchar_signed (gdbarch, 1);
  663. set_gdbarch_float_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  664. set_gdbarch_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
  665. set_gdbarch_long_double_bit (gdbarch, 8 * TARGET_CHAR_BIT);
  666. set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  667. set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  668. set_gdbarch_dwarf2_addr_size (gdbarch, 4);
  669. set_gdbarch_address_to_pointer (gdbarch, xstormy16_address_to_pointer);
  670. set_gdbarch_pointer_to_address (gdbarch, xstormy16_pointer_to_address);
  671. /* Stack grows up. */
  672. set_gdbarch_inner_than (gdbarch, core_addr_greaterthan);
  673. /*
  674. * Frame Info
  675. */
  676. set_gdbarch_frame_align (gdbarch, xstormy16_frame_align);
  677. frame_base_set_default (gdbarch, &xstormy16_frame_base);
  678. set_gdbarch_skip_prologue (gdbarch, xstormy16_skip_prologue);
  679. set_gdbarch_stack_frame_destroyed_p (gdbarch,
  680. xstormy16_stack_frame_destroyed_p);
  681. /* These values and methods are used when gdb calls a target function. */
  682. set_gdbarch_push_dummy_call (gdbarch, xstormy16_push_dummy_call);
  683. set_gdbarch_breakpoint_kind_from_pc (gdbarch,
  684. xstormy16_breakpoint::kind_from_pc);
  685. set_gdbarch_sw_breakpoint_from_kind (gdbarch,
  686. xstormy16_breakpoint::bp_from_kind);
  687. set_gdbarch_return_value (gdbarch, xstormy16_return_value);
  688. set_gdbarch_skip_trampoline_code (gdbarch, xstormy16_skip_trampoline_code);
  689. gdbarch_init_osabi (info, gdbarch);
  690. dwarf2_append_unwinders (gdbarch);
  691. frame_unwind_append_unwinder (gdbarch, &xstormy16_frame_unwind);
  692. return gdbarch;
  693. }
  694. /* Function: _initialize_xstormy16_tdep
  695. Initializer function for the Sanyo Xstormy16a module.
  696. Called by gdb at start-up. */
  697. void _initialize_xstormy16_tdep ();
  698. void
  699. _initialize_xstormy16_tdep ()
  700. {
  701. register_gdbarch_init (bfd_arch_xstormy16, xstormy16_gdbarch_init);
  702. }