moxie-tdep.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. /* Target-dependent code for Moxie.
  2. Copyright (C) 2009-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-unwind.h"
  17. #include "frame-base.h"
  18. #include "symtab.h"
  19. #include "gdbtypes.h"
  20. #include "gdbcmd.h"
  21. #include "gdbcore.h"
  22. #include "value.h"
  23. #include "inferior.h"
  24. #include "symfile.h"
  25. #include "objfiles.h"
  26. #include "osabi.h"
  27. #include "language.h"
  28. #include "arch-utils.h"
  29. #include "regcache.h"
  30. #include "trad-frame.h"
  31. #include "dis-asm.h"
  32. #include "record.h"
  33. #include "record-full.h"
  34. #include "moxie-tdep.h"
  35. #include <algorithm>
  36. /* Use an invalid address value as 'not available' marker. */
  37. enum { REG_UNAVAIL = (CORE_ADDR) -1 };
  38. struct moxie_frame_cache
  39. {
  40. /* Base address. */
  41. CORE_ADDR base;
  42. CORE_ADDR pc;
  43. LONGEST framesize;
  44. CORE_ADDR saved_regs[MOXIE_NUM_REGS];
  45. CORE_ADDR saved_sp;
  46. };
  47. /* Implement the "frame_align" gdbarch method. */
  48. static CORE_ADDR
  49. moxie_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
  50. {
  51. /* Align to the size of an instruction (so that they can safely be
  52. pushed onto the stack. */
  53. return sp & ~1;
  54. }
  55. constexpr gdb_byte moxie_break_insn[] = { 0x35, 0x00 };
  56. typedef BP_MANIPULATION (moxie_break_insn) moxie_breakpoint;
  57. /* Moxie register names. */
  58. static const char * const moxie_register_names[] = {
  59. "$fp", "$sp", "$r0", "$r1", "$r2",
  60. "$r3", "$r4", "$r5", "$r6", "$r7",
  61. "$r8", "$r9", "$r10", "$r11", "$r12",
  62. "$r13", "$pc", "$cc" };
  63. /* Implement the "register_name" gdbarch method. */
  64. static const char *
  65. moxie_register_name (struct gdbarch *gdbarch, int reg_nr)
  66. {
  67. if (reg_nr < 0)
  68. return NULL;
  69. if (reg_nr >= MOXIE_NUM_REGS)
  70. return NULL;
  71. return moxie_register_names[reg_nr];
  72. }
  73. /* Implement the "register_type" gdbarch method. */
  74. static struct type *
  75. moxie_register_type (struct gdbarch *gdbarch, int reg_nr)
  76. {
  77. if (reg_nr == MOXIE_PC_REGNUM)
  78. return builtin_type (gdbarch)->builtin_func_ptr;
  79. else if (reg_nr == MOXIE_SP_REGNUM || reg_nr == MOXIE_FP_REGNUM)
  80. return builtin_type (gdbarch)->builtin_data_ptr;
  81. else
  82. return builtin_type (gdbarch)->builtin_int32;
  83. }
  84. /* Write into appropriate registers a function return value
  85. of type TYPE, given in virtual format. */
  86. static void
  87. moxie_store_return_value (struct type *type, struct regcache *regcache,
  88. const gdb_byte *valbuf)
  89. {
  90. struct gdbarch *gdbarch = regcache->arch ();
  91. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  92. CORE_ADDR regval;
  93. int len = TYPE_LENGTH (type);
  94. /* Things always get returned in RET1_REGNUM, RET2_REGNUM. */
  95. regval = extract_unsigned_integer (valbuf, len > 4 ? 4 : len, byte_order);
  96. regcache_cooked_write_unsigned (regcache, RET1_REGNUM, regval);
  97. if (len > 4)
  98. {
  99. regval = extract_unsigned_integer (valbuf + 4, len - 4, byte_order);
  100. regcache_cooked_write_unsigned (regcache, RET1_REGNUM + 1, regval);
  101. }
  102. }
  103. /* Decode the instructions within the given address range. Decide
  104. when we must have reached the end of the function prologue. If a
  105. frame_info pointer is provided, fill in its saved_regs etc.
  106. Returns the address of the first instruction after the prologue. */
  107. static CORE_ADDR
  108. moxie_analyze_prologue (CORE_ADDR start_addr, CORE_ADDR end_addr,
  109. struct moxie_frame_cache *cache,
  110. struct gdbarch *gdbarch)
  111. {
  112. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  113. CORE_ADDR next_addr;
  114. ULONGEST inst, inst2;
  115. LONGEST offset;
  116. int regnum;
  117. /* Record where the jsra instruction saves the PC and FP. */
  118. cache->saved_regs[MOXIE_PC_REGNUM] = -4;
  119. cache->saved_regs[MOXIE_FP_REGNUM] = 0;
  120. cache->framesize = 0;
  121. if (start_addr >= end_addr)
  122. return end_addr;
  123. for (next_addr = start_addr; next_addr < end_addr; )
  124. {
  125. inst = read_memory_unsigned_integer (next_addr, 2, byte_order);
  126. /* Match "push $sp $rN" where N is between 0 and 13 inclusive. */
  127. if (inst >= 0x0612 && inst <= 0x061f)
  128. {
  129. regnum = inst & 0x000f;
  130. cache->framesize += 4;
  131. cache->saved_regs[regnum] = cache->framesize;
  132. next_addr += 2;
  133. }
  134. else
  135. break;
  136. }
  137. inst = read_memory_unsigned_integer (next_addr, 2, byte_order);
  138. /* Optional stack allocation for args and local vars <= 4
  139. byte. */
  140. if (inst == 0x01e0) /* ldi.l $r12, X */
  141. {
  142. offset = read_memory_integer (next_addr + 2, 4, byte_order);
  143. inst2 = read_memory_unsigned_integer (next_addr + 6, 2, byte_order);
  144. if (inst2 == 0x291e) /* sub.l $sp, $r12 */
  145. {
  146. cache->framesize += offset;
  147. }
  148. return (next_addr + 8);
  149. }
  150. else if ((inst & 0xff00) == 0x9100) /* dec $sp, X */
  151. {
  152. cache->framesize += (inst & 0x00ff);
  153. next_addr += 2;
  154. while (next_addr < end_addr)
  155. {
  156. inst = read_memory_unsigned_integer (next_addr, 2, byte_order);
  157. if ((inst & 0xff00) != 0x9100) /* no more dec $sp, X */
  158. break;
  159. cache->framesize += (inst & 0x00ff);
  160. next_addr += 2;
  161. }
  162. }
  163. return next_addr;
  164. }
  165. /* Find the end of function prologue. */
  166. static CORE_ADDR
  167. moxie_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  168. {
  169. CORE_ADDR func_addr = 0, func_end = 0;
  170. const char *func_name;
  171. /* See if we can determine the end of the prologue via the symbol table.
  172. If so, then return either PC, or the PC after the prologue, whichever
  173. is greater. */
  174. if (find_pc_partial_function (pc, &func_name, &func_addr, &func_end))
  175. {
  176. CORE_ADDR post_prologue_pc
  177. = skip_prologue_using_sal (gdbarch, func_addr);
  178. if (post_prologue_pc != 0)
  179. return std::max (pc, post_prologue_pc);
  180. else
  181. {
  182. /* Can't determine prologue from the symbol table, need to examine
  183. instructions. */
  184. struct symtab_and_line sal;
  185. struct symbol *sym;
  186. struct moxie_frame_cache cache;
  187. CORE_ADDR plg_end;
  188. memset (&cache, 0, sizeof cache);
  189. plg_end = moxie_analyze_prologue (func_addr,
  190. func_end, &cache, gdbarch);
  191. /* Found a function. */
  192. sym = lookup_symbol (func_name, NULL, VAR_DOMAIN, NULL).symbol;
  193. /* Don't use line number debug info for assembly source
  194. files. */
  195. if (sym && sym->language () != language_asm)
  196. {
  197. sal = find_pc_line (func_addr, 0);
  198. if (sal.end && sal.end < func_end)
  199. {
  200. /* Found a line number, use it as end of
  201. prologue. */
  202. return sal.end;
  203. }
  204. }
  205. /* No useable line symbol. Use result of prologue parsing
  206. method. */
  207. return plg_end;
  208. }
  209. }
  210. /* No function symbol -- just return the PC. */
  211. return (CORE_ADDR) pc;
  212. }
  213. struct moxie_unwind_cache
  214. {
  215. /* The previous frame's inner most stack address. Used as this
  216. frame ID's stack_addr. */
  217. CORE_ADDR prev_sp;
  218. /* The frame's base, optionally used by the high-level debug info. */
  219. CORE_ADDR base;
  220. int size;
  221. /* How far the SP and r13 (FP) have been offset from the start of
  222. the stack frame (as defined by the previous frame's stack
  223. pointer). */
  224. LONGEST sp_offset;
  225. LONGEST r13_offset;
  226. int uses_frame;
  227. /* Table indicating the location of each and every register. */
  228. trad_frame_saved_reg *saved_regs;
  229. };
  230. /* Read an unsigned integer from the inferior, and adjust
  231. endianness. */
  232. static ULONGEST
  233. moxie_process_readu (CORE_ADDR addr, gdb_byte *buf,
  234. int length, enum bfd_endian byte_order)
  235. {
  236. if (target_read_memory (addr, buf, length))
  237. {
  238. if (record_debug)
  239. gdb_printf (gdb_stderr,
  240. _("Process record: error reading memory at "
  241. "addr 0x%s len = %d.\n"),
  242. paddress (target_gdbarch (), addr), length);
  243. return -1;
  244. }
  245. return extract_unsigned_integer (buf, length, byte_order);
  246. }
  247. /* Helper macro to extract the signed 10-bit offset from a 16-bit
  248. branch instruction. */
  249. #define INST2OFFSET(o) ((((signed short)((o & ((1<<10)-1))<<6))>>6)<<1)
  250. /* Insert a single step breakpoint. */
  251. static std::vector<CORE_ADDR>
  252. moxie_software_single_step (struct regcache *regcache)
  253. {
  254. struct gdbarch *gdbarch = regcache->arch ();
  255. CORE_ADDR addr;
  256. gdb_byte buf[4];
  257. uint16_t inst;
  258. uint32_t tmpu32;
  259. ULONGEST fp;
  260. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  261. std::vector<CORE_ADDR> next_pcs;
  262. addr = regcache_read_pc (regcache);
  263. inst = (uint16_t) moxie_process_readu (addr, buf, 2, byte_order);
  264. /* Decode instruction. */
  265. if (inst & (1 << 15))
  266. {
  267. if (inst & (1 << 14))
  268. {
  269. /* This is a Form 3 instruction. */
  270. int opcode = (inst >> 10 & 0xf);
  271. switch (opcode)
  272. {
  273. case 0x00: /* beq */
  274. case 0x01: /* bne */
  275. case 0x02: /* blt */
  276. case 0x03: /* bgt */
  277. case 0x04: /* bltu */
  278. case 0x05: /* bgtu */
  279. case 0x06: /* bge */
  280. case 0x07: /* ble */
  281. case 0x08: /* bgeu */
  282. case 0x09: /* bleu */
  283. /* Insert breaks on both branches, because we can't currently tell
  284. which way things will go. */
  285. next_pcs.push_back (addr + 2);
  286. next_pcs.push_back (addr + 2 + INST2OFFSET(inst));
  287. break;
  288. default:
  289. {
  290. /* Do nothing. */
  291. break;
  292. }
  293. }
  294. }
  295. else
  296. {
  297. /* This is a Form 2 instruction. They are all 16 bits. */
  298. next_pcs.push_back (addr + 2);
  299. }
  300. }
  301. else
  302. {
  303. /* This is a Form 1 instruction. */
  304. int opcode = inst >> 8;
  305. switch (opcode)
  306. {
  307. /* 16-bit instructions. */
  308. case 0x00: /* bad */
  309. case 0x02: /* mov (register-to-register) */
  310. case 0x05: /* add.l */
  311. case 0x06: /* push */
  312. case 0x07: /* pop */
  313. case 0x0a: /* ld.l (register indirect) */
  314. case 0x0b: /* st.l */
  315. case 0x0e: /* cmp */
  316. case 0x0f: /* nop */
  317. case 0x10: /* sex.b */
  318. case 0x11: /* sex.s */
  319. case 0x12: /* zex.b */
  320. case 0x13: /* zex.s */
  321. case 0x14: /* umul.x */
  322. case 0x15: /* mul.x */
  323. case 0x16:
  324. case 0x17:
  325. case 0x18:
  326. case 0x1c: /* ld.b (register indirect) */
  327. case 0x1e: /* st.b */
  328. case 0x21: /* ld.s (register indirect) */
  329. case 0x23: /* st.s */
  330. case 0x26: /* and */
  331. case 0x27: /* lshr */
  332. case 0x28: /* ashl */
  333. case 0x29: /* sub.l */
  334. case 0x2a: /* neg */
  335. case 0x2b: /* or */
  336. case 0x2c: /* not */
  337. case 0x2d: /* ashr */
  338. case 0x2e: /* xor */
  339. case 0x2f: /* mul.l */
  340. case 0x31: /* div.l */
  341. case 0x32: /* udiv.l */
  342. case 0x33: /* mod.l */
  343. case 0x34: /* umod.l */
  344. next_pcs.push_back (addr + 2);
  345. break;
  346. /* 32-bit instructions. */
  347. case 0x0c: /* ldo.l */
  348. case 0x0d: /* sto.l */
  349. case 0x36: /* ldo.b */
  350. case 0x37: /* sto.b */
  351. case 0x38: /* ldo.s */
  352. case 0x39: /* sto.s */
  353. next_pcs.push_back (addr + 4);
  354. break;
  355. /* 48-bit instructions. */
  356. case 0x01: /* ldi.l (immediate) */
  357. case 0x08: /* lda.l */
  358. case 0x09: /* sta.l */
  359. case 0x1b: /* ldi.b (immediate) */
  360. case 0x1d: /* lda.b */
  361. case 0x1f: /* sta.b */
  362. case 0x20: /* ldi.s (immediate) */
  363. case 0x22: /* lda.s */
  364. case 0x24: /* sta.s */
  365. next_pcs.push_back (addr + 6);
  366. break;
  367. /* Control flow instructions. */
  368. case 0x03: /* jsra */
  369. case 0x1a: /* jmpa */
  370. next_pcs.push_back (moxie_process_readu (addr + 2, buf, 4,
  371. byte_order));
  372. break;
  373. case 0x04: /* ret */
  374. regcache_cooked_read_unsigned (regcache, MOXIE_FP_REGNUM, &fp);
  375. next_pcs.push_back (moxie_process_readu (fp + 4, buf, 4, byte_order));
  376. break;
  377. case 0x19: /* jsr */
  378. case 0x25: /* jmp */
  379. regcache->raw_read ((inst >> 4) & 0xf, (gdb_byte *) & tmpu32);
  380. next_pcs.push_back (tmpu32);
  381. break;
  382. case 0x30: /* swi */
  383. case 0x35: /* brk */
  384. /* Unsupported, for now. */
  385. break;
  386. }
  387. }
  388. return next_pcs;
  389. }
  390. /* Given a return value in `regbuf' with a type `valtype',
  391. extract and copy its value into `valbuf'. */
  392. static void
  393. moxie_extract_return_value (struct type *type, struct regcache *regcache,
  394. gdb_byte *dst)
  395. {
  396. struct gdbarch *gdbarch = regcache->arch ();
  397. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  398. int len = TYPE_LENGTH (type);
  399. ULONGEST tmp;
  400. /* By using store_unsigned_integer we avoid having to do
  401. anything special for small big-endian values. */
  402. regcache_cooked_read_unsigned (regcache, RET1_REGNUM, &tmp);
  403. store_unsigned_integer (dst, (len > 4 ? len - 4 : len), byte_order, tmp);
  404. /* Ignore return values more than 8 bytes in size because the moxie
  405. returns anything more than 8 bytes in the stack. */
  406. if (len > 4)
  407. {
  408. regcache_cooked_read_unsigned (regcache, RET1_REGNUM + 1, &tmp);
  409. store_unsigned_integer (dst + len - 4, 4, byte_order, tmp);
  410. }
  411. }
  412. /* Implement the "return_value" gdbarch method. */
  413. static enum return_value_convention
  414. moxie_return_value (struct gdbarch *gdbarch, struct value *function,
  415. struct type *valtype, struct regcache *regcache,
  416. gdb_byte *readbuf, const gdb_byte *writebuf)
  417. {
  418. if (TYPE_LENGTH (valtype) > 8)
  419. return RETURN_VALUE_STRUCT_CONVENTION;
  420. else
  421. {
  422. if (readbuf != NULL)
  423. moxie_extract_return_value (valtype, regcache, readbuf);
  424. if (writebuf != NULL)
  425. moxie_store_return_value (valtype, regcache, writebuf);
  426. return RETURN_VALUE_REGISTER_CONVENTION;
  427. }
  428. }
  429. /* Allocate and initialize a moxie_frame_cache object. */
  430. static struct moxie_frame_cache *
  431. moxie_alloc_frame_cache (void)
  432. {
  433. struct moxie_frame_cache *cache;
  434. int i;
  435. cache = FRAME_OBSTACK_ZALLOC (struct moxie_frame_cache);
  436. cache->base = 0;
  437. cache->saved_sp = 0;
  438. cache->pc = 0;
  439. cache->framesize = 0;
  440. for (i = 0; i < MOXIE_NUM_REGS; ++i)
  441. cache->saved_regs[i] = REG_UNAVAIL;
  442. return cache;
  443. }
  444. /* Populate a moxie_frame_cache object for this_frame. */
  445. static struct moxie_frame_cache *
  446. moxie_frame_cache (struct frame_info *this_frame, void **this_cache)
  447. {
  448. struct moxie_frame_cache *cache;
  449. CORE_ADDR current_pc;
  450. int i;
  451. if (*this_cache)
  452. return (struct moxie_frame_cache *) *this_cache;
  453. cache = moxie_alloc_frame_cache ();
  454. *this_cache = cache;
  455. cache->base = get_frame_register_unsigned (this_frame, MOXIE_FP_REGNUM);
  456. if (cache->base == 0)
  457. return cache;
  458. cache->pc = get_frame_func (this_frame);
  459. current_pc = get_frame_pc (this_frame);
  460. if (cache->pc)
  461. {
  462. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  463. moxie_analyze_prologue (cache->pc, current_pc, cache, gdbarch);
  464. }
  465. cache->saved_sp = cache->base - cache->framesize;
  466. for (i = 0; i < MOXIE_NUM_REGS; ++i)
  467. if (cache->saved_regs[i] != REG_UNAVAIL)
  468. cache->saved_regs[i] = cache->base - cache->saved_regs[i];
  469. return cache;
  470. }
  471. /* Given a GDB frame, determine the address of the calling function's
  472. frame. This will be used to create a new GDB frame struct. */
  473. static void
  474. moxie_frame_this_id (struct frame_info *this_frame,
  475. void **this_prologue_cache, struct frame_id *this_id)
  476. {
  477. struct moxie_frame_cache *cache = moxie_frame_cache (this_frame,
  478. this_prologue_cache);
  479. /* This marks the outermost frame. */
  480. if (cache->base == 0)
  481. return;
  482. *this_id = frame_id_build (cache->saved_sp, cache->pc);
  483. }
  484. /* Get the value of register regnum in the previous stack frame. */
  485. static struct value *
  486. moxie_frame_prev_register (struct frame_info *this_frame,
  487. void **this_prologue_cache, int regnum)
  488. {
  489. struct moxie_frame_cache *cache = moxie_frame_cache (this_frame,
  490. this_prologue_cache);
  491. gdb_assert (regnum >= 0);
  492. if (regnum == MOXIE_SP_REGNUM && cache->saved_sp)
  493. return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
  494. if (regnum < MOXIE_NUM_REGS && cache->saved_regs[regnum] != REG_UNAVAIL)
  495. return frame_unwind_got_memory (this_frame, regnum,
  496. cache->saved_regs[regnum]);
  497. return frame_unwind_got_register (this_frame, regnum, regnum);
  498. }
  499. static const struct frame_unwind moxie_frame_unwind = {
  500. "moxie prologue",
  501. NORMAL_FRAME,
  502. default_frame_unwind_stop_reason,
  503. moxie_frame_this_id,
  504. moxie_frame_prev_register,
  505. NULL,
  506. default_frame_sniffer
  507. };
  508. /* Return the base address of this_frame. */
  509. static CORE_ADDR
  510. moxie_frame_base_address (struct frame_info *this_frame, void **this_cache)
  511. {
  512. struct moxie_frame_cache *cache = moxie_frame_cache (this_frame,
  513. this_cache);
  514. return cache->base;
  515. }
  516. static const struct frame_base moxie_frame_base = {
  517. &moxie_frame_unwind,
  518. moxie_frame_base_address,
  519. moxie_frame_base_address,
  520. moxie_frame_base_address
  521. };
  522. /* Parse the current instruction and record the values of the registers and
  523. memory that will be changed in current instruction to "record_arch_list".
  524. Return -1 if something wrong. */
  525. static int
  526. moxie_process_record (struct gdbarch *gdbarch, struct regcache *regcache,
  527. CORE_ADDR addr)
  528. {
  529. gdb_byte buf[4];
  530. uint16_t inst;
  531. uint32_t tmpu32;
  532. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  533. if (record_debug > 1)
  534. gdb_printf (gdb_stdlog, "Process record: moxie_process_record "
  535. "addr = 0x%s\n",
  536. paddress (target_gdbarch (), addr));
  537. inst = (uint16_t) moxie_process_readu (addr, buf, 2, byte_order);
  538. /* Decode instruction. */
  539. if (inst & (1 << 15))
  540. {
  541. if (inst & (1 << 14))
  542. {
  543. /* This is a Form 3 instruction. */
  544. int opcode = (inst >> 10 & 0xf);
  545. switch (opcode)
  546. {
  547. case 0x00: /* beq */
  548. case 0x01: /* bne */
  549. case 0x02: /* blt */
  550. case 0x03: /* bgt */
  551. case 0x04: /* bltu */
  552. case 0x05: /* bgtu */
  553. case 0x06: /* bge */
  554. case 0x07: /* ble */
  555. case 0x08: /* bgeu */
  556. case 0x09: /* bleu */
  557. /* Do nothing. */
  558. break;
  559. default:
  560. {
  561. /* Do nothing. */
  562. break;
  563. }
  564. }
  565. }
  566. else
  567. {
  568. /* This is a Form 2 instruction. */
  569. int opcode = (inst >> 12 & 0x3);
  570. switch (opcode)
  571. {
  572. case 0x00: /* inc */
  573. case 0x01: /* dec */
  574. case 0x02: /* gsr */
  575. {
  576. int reg = (inst >> 8) & 0xf;
  577. if (record_full_arch_list_add_reg (regcache, reg))
  578. return -1;
  579. }
  580. break;
  581. case 0x03: /* ssr */
  582. {
  583. /* Do nothing until GDB learns about moxie's special
  584. registers. */
  585. }
  586. break;
  587. default:
  588. /* Do nothing. */
  589. break;
  590. }
  591. }
  592. }
  593. else
  594. {
  595. /* This is a Form 1 instruction. */
  596. int opcode = inst >> 8;
  597. switch (opcode)
  598. {
  599. case 0x00: /* nop */
  600. /* Do nothing. */
  601. break;
  602. case 0x01: /* ldi.l (immediate) */
  603. case 0x02: /* mov (register-to-register) */
  604. {
  605. int reg = (inst >> 4) & 0xf;
  606. if (record_full_arch_list_add_reg (regcache, reg))
  607. return -1;
  608. }
  609. break;
  610. case 0x03: /* jsra */
  611. {
  612. regcache->raw_read (
  613. MOXIE_SP_REGNUM, (gdb_byte *) & tmpu32);
  614. tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
  615. 4, byte_order);
  616. if (record_full_arch_list_add_reg (regcache, MOXIE_FP_REGNUM)
  617. || (record_full_arch_list_add_reg (regcache,
  618. MOXIE_SP_REGNUM))
  619. || record_full_arch_list_add_mem (tmpu32 - 12, 12))
  620. return -1;
  621. }
  622. break;
  623. case 0x04: /* ret */
  624. {
  625. if (record_full_arch_list_add_reg (regcache, MOXIE_FP_REGNUM)
  626. || (record_full_arch_list_add_reg (regcache,
  627. MOXIE_SP_REGNUM)))
  628. return -1;
  629. }
  630. break;
  631. case 0x05: /* add.l */
  632. {
  633. int reg = (inst >> 4) & 0xf;
  634. if (record_full_arch_list_add_reg (regcache, reg))
  635. return -1;
  636. }
  637. break;
  638. case 0x06: /* push */
  639. {
  640. int reg = (inst >> 4) & 0xf;
  641. regcache->raw_read (reg, (gdb_byte *) & tmpu32);
  642. tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
  643. 4, byte_order);
  644. if (record_full_arch_list_add_reg (regcache, reg)
  645. || record_full_arch_list_add_mem (tmpu32 - 4, 4))
  646. return -1;
  647. }
  648. break;
  649. case 0x07: /* pop */
  650. {
  651. int a = (inst >> 4) & 0xf;
  652. int b = inst & 0xf;
  653. if (record_full_arch_list_add_reg (regcache, a)
  654. || record_full_arch_list_add_reg (regcache, b))
  655. return -1;
  656. }
  657. break;
  658. case 0x08: /* lda.l */
  659. {
  660. int reg = (inst >> 4) & 0xf;
  661. if (record_full_arch_list_add_reg (regcache, reg))
  662. return -1;
  663. }
  664. break;
  665. case 0x09: /* sta.l */
  666. {
  667. tmpu32 = (uint32_t) moxie_process_readu (addr+2, buf,
  668. 4, byte_order);
  669. if (record_full_arch_list_add_mem (tmpu32, 4))
  670. return -1;
  671. }
  672. break;
  673. case 0x0a: /* ld.l (register indirect) */
  674. {
  675. int reg = (inst >> 4) & 0xf;
  676. if (record_full_arch_list_add_reg (regcache, reg))
  677. return -1;
  678. }
  679. break;
  680. case 0x0b: /* st.l */
  681. {
  682. int reg = (inst >> 4) & 0xf;
  683. regcache->raw_read (reg, (gdb_byte *) & tmpu32);
  684. tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
  685. 4, byte_order);
  686. if (record_full_arch_list_add_mem (tmpu32, 4))
  687. return -1;
  688. }
  689. break;
  690. case 0x0c: /* ldo.l */
  691. {
  692. int reg = (inst >> 4) & 0xf;
  693. if (record_full_arch_list_add_reg (regcache, reg))
  694. return -1;
  695. }
  696. break;
  697. case 0x0d: /* sto.l */
  698. {
  699. int reg = (inst >> 4) & 0xf;
  700. uint32_t offset = (((int16_t) moxie_process_readu (addr+2, buf, 2,
  701. byte_order)) << 16 ) >> 16;
  702. regcache->raw_read (reg, (gdb_byte *) & tmpu32);
  703. tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
  704. 4, byte_order);
  705. tmpu32 += offset;
  706. if (record_full_arch_list_add_mem (tmpu32, 4))
  707. return -1;
  708. }
  709. break;
  710. case 0x0e: /* cmp */
  711. {
  712. if (record_full_arch_list_add_reg (regcache, MOXIE_CC_REGNUM))
  713. return -1;
  714. }
  715. break;
  716. case 0x0f: /* nop */
  717. {
  718. /* Do nothing. */
  719. break;
  720. }
  721. case 0x10: /* sex.b */
  722. case 0x11: /* sex.s */
  723. case 0x12: /* zex.b */
  724. case 0x13: /* zex.s */
  725. case 0x14: /* umul.x */
  726. case 0x15: /* mul.x */
  727. {
  728. int reg = (inst >> 4) & 0xf;
  729. if (record_full_arch_list_add_reg (regcache, reg))
  730. return -1;
  731. }
  732. break;
  733. case 0x16:
  734. case 0x17:
  735. case 0x18:
  736. {
  737. /* Do nothing. */
  738. break;
  739. }
  740. case 0x19: /* jsr */
  741. {
  742. regcache->raw_read (
  743. MOXIE_SP_REGNUM, (gdb_byte *) & tmpu32);
  744. tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
  745. 4, byte_order);
  746. if (record_full_arch_list_add_reg (regcache, MOXIE_FP_REGNUM)
  747. || (record_full_arch_list_add_reg (regcache,
  748. MOXIE_SP_REGNUM))
  749. || record_full_arch_list_add_mem (tmpu32 - 12, 12))
  750. return -1;
  751. }
  752. break;
  753. case 0x1a: /* jmpa */
  754. {
  755. /* Do nothing. */
  756. }
  757. break;
  758. case 0x1b: /* ldi.b (immediate) */
  759. case 0x1c: /* ld.b (register indirect) */
  760. case 0x1d: /* lda.b */
  761. {
  762. int reg = (inst >> 4) & 0xf;
  763. if (record_full_arch_list_add_reg (regcache, reg))
  764. return -1;
  765. }
  766. break;
  767. case 0x1e: /* st.b */
  768. {
  769. int reg = (inst >> 4) & 0xf;
  770. regcache->raw_read (reg, (gdb_byte *) & tmpu32);
  771. tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
  772. 4, byte_order);
  773. if (record_full_arch_list_add_mem (tmpu32, 1))
  774. return -1;
  775. }
  776. break;
  777. case 0x1f: /* sta.b */
  778. {
  779. tmpu32 = moxie_process_readu (addr+2, buf, 4, byte_order);
  780. if (record_full_arch_list_add_mem (tmpu32, 1))
  781. return -1;
  782. }
  783. break;
  784. case 0x20: /* ldi.s (immediate) */
  785. case 0x21: /* ld.s (register indirect) */
  786. case 0x22: /* lda.s */
  787. {
  788. int reg = (inst >> 4) & 0xf;
  789. if (record_full_arch_list_add_reg (regcache, reg))
  790. return -1;
  791. }
  792. break;
  793. case 0x23: /* st.s */
  794. {
  795. int reg = (inst >> 4) & 0xf;
  796. regcache->raw_read (reg, (gdb_byte *) & tmpu32);
  797. tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
  798. 4, byte_order);
  799. if (record_full_arch_list_add_mem (tmpu32, 2))
  800. return -1;
  801. }
  802. break;
  803. case 0x24: /* sta.s */
  804. {
  805. tmpu32 = moxie_process_readu (addr+2, buf, 4, byte_order);
  806. if (record_full_arch_list_add_mem (tmpu32, 2))
  807. return -1;
  808. }
  809. break;
  810. case 0x25: /* jmp */
  811. {
  812. /* Do nothing. */
  813. }
  814. break;
  815. case 0x26: /* and */
  816. case 0x27: /* lshr */
  817. case 0x28: /* ashl */
  818. case 0x29: /* sub */
  819. case 0x2a: /* neg */
  820. case 0x2b: /* or */
  821. case 0x2c: /* not */
  822. case 0x2d: /* ashr */
  823. case 0x2e: /* xor */
  824. case 0x2f: /* mul */
  825. {
  826. int reg = (inst >> 4) & 0xf;
  827. if (record_full_arch_list_add_reg (regcache, reg))
  828. return -1;
  829. }
  830. break;
  831. case 0x30: /* swi */
  832. {
  833. /* We currently implement support for libgloss'
  834. system calls. */
  835. int inum = moxie_process_readu (addr+2, buf, 4, byte_order);
  836. switch (inum)
  837. {
  838. case 0x1: /* SYS_exit */
  839. {
  840. /* Do nothing. */
  841. }
  842. break;
  843. case 0x2: /* SYS_open */
  844. {
  845. if (record_full_arch_list_add_reg (regcache, RET1_REGNUM))
  846. return -1;
  847. }
  848. break;
  849. case 0x4: /* SYS_read */
  850. {
  851. uint32_t length, ptr;
  852. /* Read buffer pointer is in $r1. */
  853. regcache->raw_read (3, (gdb_byte *) & ptr);
  854. ptr = extract_unsigned_integer ((gdb_byte *) & ptr,
  855. 4, byte_order);
  856. /* String length is at 0x12($fp). */
  857. regcache->raw_read (
  858. MOXIE_FP_REGNUM, (gdb_byte *) & tmpu32);
  859. tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
  860. 4, byte_order);
  861. length = moxie_process_readu (tmpu32+20, buf, 4, byte_order);
  862. if (record_full_arch_list_add_mem (ptr, length))
  863. return -1;
  864. }
  865. break;
  866. case 0x5: /* SYS_write */
  867. {
  868. if (record_full_arch_list_add_reg (regcache, RET1_REGNUM))
  869. return -1;
  870. }
  871. break;
  872. default:
  873. break;
  874. }
  875. }
  876. break;
  877. case 0x31: /* div.l */
  878. case 0x32: /* udiv.l */
  879. case 0x33: /* mod.l */
  880. case 0x34: /* umod.l */
  881. {
  882. int reg = (inst >> 4) & 0xf;
  883. if (record_full_arch_list_add_reg (regcache, reg))
  884. return -1;
  885. }
  886. break;
  887. case 0x35: /* brk */
  888. /* Do nothing. */
  889. break;
  890. case 0x36: /* ldo.b */
  891. {
  892. int reg = (inst >> 4) & 0xf;
  893. if (record_full_arch_list_add_reg (regcache, reg))
  894. return -1;
  895. }
  896. break;
  897. case 0x37: /* sto.b */
  898. {
  899. int reg = (inst >> 4) & 0xf;
  900. uint32_t offset = (((int16_t) moxie_process_readu (addr+2, buf, 2,
  901. byte_order)) << 16 ) >> 16;
  902. regcache->raw_read (reg, (gdb_byte *) & tmpu32);
  903. tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
  904. 4, byte_order);
  905. tmpu32 += offset;
  906. if (record_full_arch_list_add_mem (tmpu32, 1))
  907. return -1;
  908. }
  909. break;
  910. case 0x38: /* ldo.s */
  911. {
  912. int reg = (inst >> 4) & 0xf;
  913. if (record_full_arch_list_add_reg (regcache, reg))
  914. return -1;
  915. }
  916. break;
  917. case 0x39: /* sto.s */
  918. {
  919. int reg = (inst >> 4) & 0xf;
  920. uint32_t offset = (((int16_t) moxie_process_readu (addr+2, buf, 2,
  921. byte_order)) << 16 ) >> 16;
  922. regcache->raw_read (reg, (gdb_byte *) & tmpu32);
  923. tmpu32 = extract_unsigned_integer ((gdb_byte *) & tmpu32,
  924. 4, byte_order);
  925. tmpu32 += offset;
  926. if (record_full_arch_list_add_mem (tmpu32, 2))
  927. return -1;
  928. }
  929. break;
  930. default:
  931. /* Do nothing. */
  932. break;
  933. }
  934. }
  935. if (record_full_arch_list_add_reg (regcache, MOXIE_PC_REGNUM))
  936. return -1;
  937. if (record_full_arch_list_add_end ())
  938. return -1;
  939. return 0;
  940. }
  941. /* Allocate and initialize the moxie gdbarch object. */
  942. static struct gdbarch *
  943. moxie_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  944. {
  945. struct gdbarch *gdbarch;
  946. /* If there is already a candidate, use it. */
  947. arches = gdbarch_list_lookup_by_info (arches, &info);
  948. if (arches != NULL)
  949. return arches->gdbarch;
  950. /* Allocate space for the new architecture. */
  951. moxie_gdbarch_tdep *tdep = new moxie_gdbarch_tdep;
  952. gdbarch = gdbarch_alloc (&info, tdep);
  953. set_gdbarch_wchar_bit (gdbarch, 32);
  954. set_gdbarch_wchar_signed (gdbarch, 0);
  955. set_gdbarch_num_regs (gdbarch, MOXIE_NUM_REGS);
  956. set_gdbarch_sp_regnum (gdbarch, MOXIE_SP_REGNUM);
  957. set_gdbarch_pc_regnum (gdbarch, MOXIE_PC_REGNUM);
  958. set_gdbarch_register_name (gdbarch, moxie_register_name);
  959. set_gdbarch_register_type (gdbarch, moxie_register_type);
  960. set_gdbarch_return_value (gdbarch, moxie_return_value);
  961. set_gdbarch_skip_prologue (gdbarch, moxie_skip_prologue);
  962. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  963. set_gdbarch_breakpoint_kind_from_pc (gdbarch,
  964. moxie_breakpoint::kind_from_pc);
  965. set_gdbarch_sw_breakpoint_from_kind (gdbarch,
  966. moxie_breakpoint::bp_from_kind);
  967. set_gdbarch_frame_align (gdbarch, moxie_frame_align);
  968. frame_base_set_default (gdbarch, &moxie_frame_base);
  969. /* Hook in ABI-specific overrides, if they have been registered. */
  970. gdbarch_init_osabi (info, gdbarch);
  971. /* Hook in the default unwinders. */
  972. frame_unwind_append_unwinder (gdbarch, &moxie_frame_unwind);
  973. /* Single stepping. */
  974. set_gdbarch_software_single_step (gdbarch, moxie_software_single_step);
  975. /* Support simple overlay manager. */
  976. set_gdbarch_overlay_update (gdbarch, simple_overlay_update);
  977. /* Support reverse debugging. */
  978. set_gdbarch_process_record (gdbarch, moxie_process_record);
  979. return gdbarch;
  980. }
  981. /* Register this machine's init routine. */
  982. void _initialize_moxie_tdep ();
  983. void
  984. _initialize_moxie_tdep ()
  985. {
  986. register_gdbarch_init (bfd_arch_moxie, moxie_gdbarch_init);
  987. }