m32r-tdep.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /* Target-dependent code for Renesas M32R, for GDB.
  2. Copyright (C) 1996-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 "m32r-tdep.h"
  33. #include <algorithm>
  34. /* The size of the argument registers (r0 - r3) in bytes. */
  35. #define M32R_ARG_REGISTER_SIZE 4
  36. /* Local functions */
  37. static CORE_ADDR
  38. m32r_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
  39. {
  40. /* Align to the size of an instruction (so that they can safely be
  41. pushed onto the stack. */
  42. return sp & ~3;
  43. }
  44. /* Breakpoints
  45. The little endian mode of M32R is unique. In most of architectures,
  46. two 16-bit instructions, A and B, are placed as the following:
  47. Big endian:
  48. A0 A1 B0 B1
  49. Little endian:
  50. A1 A0 B1 B0
  51. In M32R, they are placed like this:
  52. Big endian:
  53. A0 A1 B0 B1
  54. Little endian:
  55. B1 B0 A1 A0
  56. This is because M32R always fetches instructions in 32-bit.
  57. The following functions take care of this behavior. */
  58. static int
  59. m32r_memory_insert_breakpoint (struct gdbarch *gdbarch,
  60. struct bp_target_info *bp_tgt)
  61. {
  62. CORE_ADDR addr = bp_tgt->placed_address = bp_tgt->reqstd_address;
  63. int val;
  64. gdb_byte buf[4];
  65. gdb_byte contents_cache[4];
  66. gdb_byte bp_entry[] = { 0x10, 0xf1 }; /* dpt */
  67. /* Save the memory contents. */
  68. val = target_read_memory (addr & 0xfffffffc, contents_cache, 4);
  69. if (val != 0)
  70. return val; /* return error */
  71. memcpy (bp_tgt->shadow_contents, contents_cache, 4);
  72. bp_tgt->shadow_len = 4;
  73. /* Determine appropriate breakpoint contents and size for this address. */
  74. if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  75. {
  76. if ((addr & 3) == 0)
  77. {
  78. buf[0] = bp_entry[0];
  79. buf[1] = bp_entry[1];
  80. buf[2] = contents_cache[2] & 0x7f;
  81. buf[3] = contents_cache[3];
  82. }
  83. else
  84. {
  85. buf[0] = contents_cache[0];
  86. buf[1] = contents_cache[1];
  87. buf[2] = bp_entry[0];
  88. buf[3] = bp_entry[1];
  89. }
  90. }
  91. else /* little-endian */
  92. {
  93. if ((addr & 3) == 0)
  94. {
  95. buf[0] = contents_cache[0];
  96. buf[1] = contents_cache[1] & 0x7f;
  97. buf[2] = bp_entry[1];
  98. buf[3] = bp_entry[0];
  99. }
  100. else
  101. {
  102. buf[0] = bp_entry[1];
  103. buf[1] = bp_entry[0];
  104. buf[2] = contents_cache[2];
  105. buf[3] = contents_cache[3];
  106. }
  107. }
  108. /* Write the breakpoint. */
  109. val = target_write_memory (addr & 0xfffffffc, buf, 4);
  110. return val;
  111. }
  112. static int
  113. m32r_memory_remove_breakpoint (struct gdbarch *gdbarch,
  114. struct bp_target_info *bp_tgt)
  115. {
  116. CORE_ADDR addr = bp_tgt->placed_address;
  117. int val;
  118. gdb_byte buf[4];
  119. gdb_byte *contents_cache = bp_tgt->shadow_contents;
  120. buf[0] = contents_cache[0];
  121. buf[1] = contents_cache[1];
  122. buf[2] = contents_cache[2];
  123. buf[3] = contents_cache[3];
  124. /* Remove parallel bit. */
  125. if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  126. {
  127. if ((buf[0] & 0x80) == 0 && (buf[2] & 0x80) != 0)
  128. buf[2] &= 0x7f;
  129. }
  130. else /* little-endian */
  131. {
  132. if ((buf[3] & 0x80) == 0 && (buf[1] & 0x80) != 0)
  133. buf[1] &= 0x7f;
  134. }
  135. /* Write contents. */
  136. val = target_write_raw_memory (addr & 0xfffffffc, buf, 4);
  137. return val;
  138. }
  139. /* Implement the breakpoint_kind_from_pc gdbarch method. */
  140. static int
  141. m32r_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
  142. {
  143. if ((*pcptr & 3) == 0)
  144. return 4;
  145. else
  146. return 2;
  147. }
  148. /* Implement the sw_breakpoint_from_kind gdbarch method. */
  149. static const gdb_byte *
  150. m32r_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
  151. {
  152. static gdb_byte be_bp_entry[] = {
  153. 0x10, 0xf1, 0x70, 0x00
  154. }; /* dpt -> nop */
  155. static gdb_byte le_bp_entry[] = {
  156. 0x00, 0x70, 0xf1, 0x10
  157. }; /* dpt -> nop */
  158. *size = kind;
  159. /* Determine appropriate breakpoint. */
  160. if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  161. return be_bp_entry;
  162. else
  163. {
  164. if (kind == 4)
  165. return le_bp_entry;
  166. else
  167. return le_bp_entry + 2;
  168. }
  169. }
  170. static const char * const m32r_register_names[] = {
  171. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  172. "r8", "r9", "r10", "r11", "r12", "fp", "lr", "sp",
  173. "psw", "cbr", "spi", "spu", "bpc", "pc", "accl", "acch",
  174. "evb"
  175. };
  176. static const char *
  177. m32r_register_name (struct gdbarch *gdbarch, int reg_nr)
  178. {
  179. if (reg_nr < 0)
  180. return NULL;
  181. if (reg_nr >= M32R_NUM_REGS)
  182. return NULL;
  183. return m32r_register_names[reg_nr];
  184. }
  185. /* Return the GDB type object for the "standard" data type
  186. of data in register N. */
  187. static struct type *
  188. m32r_register_type (struct gdbarch *gdbarch, int reg_nr)
  189. {
  190. if (reg_nr == M32R_PC_REGNUM)
  191. return builtin_type (gdbarch)->builtin_func_ptr;
  192. else if (reg_nr == M32R_SP_REGNUM || reg_nr == M32R_FP_REGNUM)
  193. return builtin_type (gdbarch)->builtin_data_ptr;
  194. else
  195. return builtin_type (gdbarch)->builtin_int32;
  196. }
  197. /* Write into appropriate registers a function return value
  198. of type TYPE, given in virtual format.
  199. Things always get returned in RET1_REGNUM, RET2_REGNUM. */
  200. static void
  201. m32r_store_return_value (struct type *type, struct regcache *regcache,
  202. const gdb_byte *valbuf)
  203. {
  204. struct gdbarch *gdbarch = regcache->arch ();
  205. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  206. CORE_ADDR regval;
  207. int len = TYPE_LENGTH (type);
  208. regval = extract_unsigned_integer (valbuf, len > 4 ? 4 : len, byte_order);
  209. regcache_cooked_write_unsigned (regcache, RET1_REGNUM, regval);
  210. if (len > 4)
  211. {
  212. regval = extract_unsigned_integer (valbuf + 4,
  213. len - 4, byte_order);
  214. regcache_cooked_write_unsigned (regcache, RET1_REGNUM + 1, regval);
  215. }
  216. }
  217. /* This is required by skip_prologue. The results of decoding a prologue
  218. should be cached because this thrashing is getting nuts. */
  219. static int
  220. decode_prologue (struct gdbarch *gdbarch,
  221. CORE_ADDR start_pc, CORE_ADDR scan_limit,
  222. CORE_ADDR *pl_endptr, unsigned long *framelength)
  223. {
  224. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  225. unsigned long framesize;
  226. int insn;
  227. int op1;
  228. CORE_ADDR after_prologue = 0;
  229. CORE_ADDR after_push = 0;
  230. CORE_ADDR after_stack_adjust = 0;
  231. CORE_ADDR current_pc;
  232. LONGEST return_value;
  233. framesize = 0;
  234. after_prologue = 0;
  235. for (current_pc = start_pc; current_pc < scan_limit; current_pc += 2)
  236. {
  237. /* Check if current pc's location is readable. */
  238. if (!safe_read_memory_integer (current_pc, 2, byte_order, &return_value))
  239. return -1;
  240. insn = read_memory_unsigned_integer (current_pc, 2, byte_order);
  241. if (insn == 0x0000)
  242. break;
  243. /* If this is a 32 bit instruction, we dont want to examine its
  244. immediate data as though it were an instruction. */
  245. if (current_pc & 0x02)
  246. {
  247. /* Decode this instruction further. */
  248. insn &= 0x7fff;
  249. }
  250. else
  251. {
  252. if (insn & 0x8000)
  253. {
  254. if (current_pc == scan_limit)
  255. scan_limit += 2; /* extend the search */
  256. current_pc += 2; /* skip the immediate data */
  257. /* Check if current pc's location is readable. */
  258. if (!safe_read_memory_integer (current_pc, 2, byte_order,
  259. &return_value))
  260. return -1;
  261. if (insn == 0x8faf) /* add3 sp, sp, xxxx */
  262. /* add 16 bit sign-extended offset */
  263. {
  264. framesize +=
  265. -((short) read_memory_unsigned_integer (current_pc,
  266. 2, byte_order));
  267. }
  268. else
  269. {
  270. if (((insn >> 8) == 0xe4) /* ld24 r4, xxxxxx; sub sp, r4 */
  271. && safe_read_memory_integer (current_pc + 2,
  272. 2, byte_order,
  273. &return_value)
  274. && read_memory_unsigned_integer (current_pc + 2,
  275. 2, byte_order)
  276. == 0x0f24)
  277. {
  278. /* Subtract 24 bit sign-extended negative-offset. */
  279. insn = read_memory_unsigned_integer (current_pc - 2,
  280. 4, byte_order);
  281. if (insn & 0x00800000) /* sign extend */
  282. insn |= 0xff000000; /* negative */
  283. else
  284. insn &= 0x00ffffff; /* positive */
  285. framesize += insn;
  286. }
  287. }
  288. after_push = current_pc + 2;
  289. continue;
  290. }
  291. }
  292. op1 = insn & 0xf000; /* Isolate just the first nibble. */
  293. if ((insn & 0xf0ff) == 0x207f)
  294. { /* st reg, @-sp */
  295. framesize += 4;
  296. after_prologue = 0;
  297. continue;
  298. }
  299. if ((insn >> 8) == 0x4f) /* addi sp, xx */
  300. /* Add 8 bit sign-extended offset. */
  301. {
  302. int stack_adjust = (signed char) (insn & 0xff);
  303. /* there are probably two of these stack adjustments:
  304. 1) A negative one in the prologue, and
  305. 2) A positive one in the epilogue.
  306. We are only interested in the first one. */
  307. if (stack_adjust < 0)
  308. {
  309. framesize -= stack_adjust;
  310. after_prologue = 0;
  311. /* A frameless function may have no "mv fp, sp".
  312. In that case, this is the end of the prologue. */
  313. after_stack_adjust = current_pc + 2;
  314. }
  315. continue;
  316. }
  317. if (insn == 0x1d8f)
  318. { /* mv fp, sp */
  319. after_prologue = current_pc + 2;
  320. break; /* end of stack adjustments */
  321. }
  322. /* Nop looks like a branch, continue explicitly. */
  323. if (insn == 0x7000)
  324. {
  325. after_prologue = current_pc + 2;
  326. continue; /* nop occurs between pushes. */
  327. }
  328. /* End of prolog if any of these are trap instructions. */
  329. if ((insn & 0xfff0) == 0x10f0)
  330. {
  331. after_prologue = current_pc;
  332. break;
  333. }
  334. /* End of prolog if any of these are branch instructions. */
  335. if ((op1 == 0x7000) || (op1 == 0xb000) || (op1 == 0xf000))
  336. {
  337. after_prologue = current_pc;
  338. continue;
  339. }
  340. /* Some of the branch instructions are mixed with other types. */
  341. if (op1 == 0x1000)
  342. {
  343. int subop = insn & 0x0ff0;
  344. if ((subop == 0x0ec0) || (subop == 0x0fc0))
  345. {
  346. after_prologue = current_pc;
  347. continue; /* jmp , jl */
  348. }
  349. }
  350. }
  351. if (framelength)
  352. *framelength = framesize;
  353. if (current_pc >= scan_limit)
  354. {
  355. if (pl_endptr)
  356. {
  357. if (after_stack_adjust != 0)
  358. /* We did not find a "mv fp,sp", but we DID find
  359. a stack_adjust. Is it safe to use that as the
  360. end of the prologue? I just don't know. */
  361. {
  362. *pl_endptr = after_stack_adjust;
  363. }
  364. else if (after_push != 0)
  365. /* We did not find a "mv fp,sp", but we DID find
  366. a push. Is it safe to use that as the
  367. end of the prologue? I just don't know. */
  368. {
  369. *pl_endptr = after_push;
  370. }
  371. else
  372. /* We reached the end of the loop without finding the end
  373. of the prologue. No way to win -- we should report
  374. failure. The way we do that is to return the original
  375. start_pc. GDB will set a breakpoint at the start of
  376. the function (etc.) */
  377. *pl_endptr = start_pc;
  378. }
  379. return 0;
  380. }
  381. if (after_prologue == 0)
  382. after_prologue = current_pc;
  383. if (pl_endptr)
  384. *pl_endptr = after_prologue;
  385. return 0;
  386. } /* decode_prologue */
  387. /* Function: skip_prologue
  388. Find end of function prologue. */
  389. #define DEFAULT_SEARCH_LIMIT 128
  390. static CORE_ADDR
  391. m32r_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  392. {
  393. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  394. CORE_ADDR func_addr, func_end;
  395. struct symtab_and_line sal;
  396. LONGEST return_value;
  397. /* See what the symbol table says. */
  398. if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
  399. {
  400. sal = find_pc_line (func_addr, 0);
  401. if (sal.line != 0 && sal.end <= func_end)
  402. {
  403. func_end = sal.end;
  404. }
  405. else
  406. /* Either there's no line info, or the line after the prologue is after
  407. the end of the function. In this case, there probably isn't a
  408. prologue. */
  409. {
  410. func_end = std::min (func_end, func_addr + DEFAULT_SEARCH_LIMIT);
  411. }
  412. }
  413. else
  414. func_end = pc + DEFAULT_SEARCH_LIMIT;
  415. /* If pc's location is not readable, just quit. */
  416. if (!safe_read_memory_integer (pc, 4, byte_order, &return_value))
  417. return pc;
  418. /* Find the end of prologue. */
  419. if (decode_prologue (gdbarch, pc, func_end, &sal.end, NULL) < 0)
  420. return pc;
  421. return sal.end;
  422. }
  423. struct m32r_unwind_cache
  424. {
  425. /* The previous frame's inner most stack address. Used as this
  426. frame ID's stack_addr. */
  427. CORE_ADDR prev_sp;
  428. /* The frame's base, optionally used by the high-level debug info. */
  429. CORE_ADDR base;
  430. int size;
  431. /* How far the SP and r13 (FP) have been offset from the start of
  432. the stack frame (as defined by the previous frame's stack
  433. pointer). */
  434. LONGEST sp_offset;
  435. LONGEST r13_offset;
  436. int uses_frame;
  437. /* Table indicating the location of each and every register. */
  438. trad_frame_saved_reg *saved_regs;
  439. };
  440. /* Put here the code to store, into fi->saved_regs, the addresses of
  441. the saved registers of frame described by FRAME_INFO. This
  442. includes special registers such as pc and fp saved in special ways
  443. in the stack frame. sp is even more special: the address we return
  444. for it IS the sp for the next frame. */
  445. static struct m32r_unwind_cache *
  446. m32r_frame_unwind_cache (struct frame_info *this_frame,
  447. void **this_prologue_cache)
  448. {
  449. CORE_ADDR pc, scan_limit;
  450. ULONGEST prev_sp;
  451. ULONGEST this_base;
  452. unsigned long op;
  453. int i;
  454. struct m32r_unwind_cache *info;
  455. if ((*this_prologue_cache))
  456. return (struct m32r_unwind_cache *) (*this_prologue_cache);
  457. info = FRAME_OBSTACK_ZALLOC (struct m32r_unwind_cache);
  458. (*this_prologue_cache) = info;
  459. info->saved_regs = trad_frame_alloc_saved_regs (this_frame);
  460. info->size = 0;
  461. info->sp_offset = 0;
  462. info->uses_frame = 0;
  463. scan_limit = get_frame_pc (this_frame);
  464. for (pc = get_frame_func (this_frame);
  465. pc > 0 && pc < scan_limit; pc += 2)
  466. {
  467. if ((pc & 2) == 0)
  468. {
  469. op = get_frame_memory_unsigned (this_frame, pc, 4);
  470. if ((op & 0x80000000) == 0x80000000)
  471. {
  472. /* 32-bit instruction */
  473. if ((op & 0xffff0000) == 0x8faf0000)
  474. {
  475. /* add3 sp,sp,xxxx */
  476. short n = op & 0xffff;
  477. info->sp_offset += n;
  478. }
  479. else if (((op >> 8) == 0xe4)
  480. && get_frame_memory_unsigned (this_frame, pc + 2,
  481. 2) == 0x0f24)
  482. {
  483. /* ld24 r4, xxxxxx; sub sp, r4 */
  484. unsigned long n = op & 0xffffff;
  485. info->sp_offset += n;
  486. pc += 2; /* skip sub instruction */
  487. }
  488. if (pc == scan_limit)
  489. scan_limit += 2; /* extend the search */
  490. pc += 2; /* skip the immediate data */
  491. continue;
  492. }
  493. }
  494. /* 16-bit instructions */
  495. op = get_frame_memory_unsigned (this_frame, pc, 2) & 0x7fff;
  496. if ((op & 0xf0ff) == 0x207f)
  497. {
  498. /* st rn, @-sp */
  499. int regno = ((op >> 8) & 0xf);
  500. info->sp_offset -= 4;
  501. info->saved_regs[regno].set_addr (info->sp_offset);
  502. }
  503. else if ((op & 0xff00) == 0x4f00)
  504. {
  505. /* addi sp, xx */
  506. int n = (signed char) (op & 0xff);
  507. info->sp_offset += n;
  508. }
  509. else if (op == 0x1d8f)
  510. {
  511. /* mv fp, sp */
  512. info->uses_frame = 1;
  513. info->r13_offset = info->sp_offset;
  514. break; /* end of stack adjustments */
  515. }
  516. else if ((op & 0xfff0) == 0x10f0)
  517. {
  518. /* End of prologue if this is a trap instruction. */
  519. break; /* End of stack adjustments. */
  520. }
  521. }
  522. info->size = -info->sp_offset;
  523. /* Compute the previous frame's stack pointer (which is also the
  524. frame's ID's stack address), and this frame's base pointer. */
  525. if (info->uses_frame)
  526. {
  527. /* The SP was moved to the FP. This indicates that a new frame
  528. was created. Get THIS frame's FP value by unwinding it from
  529. the next frame. */
  530. this_base = get_frame_register_unsigned (this_frame, M32R_FP_REGNUM);
  531. /* The FP points at the last saved register. Adjust the FP back
  532. to before the first saved register giving the SP. */
  533. prev_sp = this_base + info->size;
  534. }
  535. else
  536. {
  537. /* Assume that the FP is this frame's SP but with that pushed
  538. stack space added back. */
  539. this_base = get_frame_register_unsigned (this_frame, M32R_SP_REGNUM);
  540. prev_sp = this_base + info->size;
  541. }
  542. /* Convert that SP/BASE into real addresses. */
  543. info->prev_sp = prev_sp;
  544. info->base = this_base;
  545. /* Adjust all the saved registers so that they contain addresses and
  546. not offsets. */
  547. for (i = 0; i < gdbarch_num_regs (get_frame_arch (this_frame)) - 1; i++)
  548. if (info->saved_regs[i].is_addr ())
  549. info->saved_regs[i].set_addr (info->prev_sp
  550. + info->saved_regs[i].addr ());
  551. /* The call instruction moves the caller's PC in the callee's LR.
  552. Since this is an unwind, do the reverse. Copy the location of LR
  553. into PC (the address / regnum) so that a request for PC will be
  554. converted into a request for the LR. */
  555. info->saved_regs[M32R_PC_REGNUM] = info->saved_regs[LR_REGNUM];
  556. /* The previous frame's SP needed to be computed. Save the computed
  557. value. */
  558. info->saved_regs[M32R_SP_REGNUM].set_value (prev_sp);
  559. return info;
  560. }
  561. static CORE_ADDR
  562. m32r_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  563. struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
  564. struct value **args, CORE_ADDR sp,
  565. function_call_return_method return_method,
  566. CORE_ADDR struct_addr)
  567. {
  568. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  569. int stack_offset, stack_alloc;
  570. int argreg = ARG1_REGNUM;
  571. int argnum;
  572. struct type *type;
  573. enum type_code typecode;
  574. CORE_ADDR regval;
  575. gdb_byte *val;
  576. gdb_byte valbuf[M32R_ARG_REGISTER_SIZE];
  577. int len;
  578. /* First force sp to a 4-byte alignment. */
  579. sp = sp & ~3;
  580. /* Set the return address. For the m32r, the return breakpoint is
  581. always at BP_ADDR. */
  582. regcache_cooked_write_unsigned (regcache, LR_REGNUM, bp_addr);
  583. /* If STRUCT_RETURN is true, then the struct return address (in
  584. STRUCT_ADDR) will consume the first argument-passing register.
  585. Both adjust the register count and store that value. */
  586. if (return_method == return_method_struct)
  587. {
  588. regcache_cooked_write_unsigned (regcache, argreg, struct_addr);
  589. argreg++;
  590. }
  591. /* Now make sure there's space on the stack. */
  592. for (argnum = 0, stack_alloc = 0; argnum < nargs; argnum++)
  593. stack_alloc += ((TYPE_LENGTH (value_type (args[argnum])) + 3) & ~3);
  594. sp -= stack_alloc; /* Make room on stack for args. */
  595. for (argnum = 0, stack_offset = 0; argnum < nargs; argnum++)
  596. {
  597. type = value_type (args[argnum]);
  598. typecode = type->code ();
  599. len = TYPE_LENGTH (type);
  600. memset (valbuf, 0, sizeof (valbuf));
  601. /* Passes structures that do not fit in 2 registers by reference. */
  602. if (len > 8
  603. && (typecode == TYPE_CODE_STRUCT || typecode == TYPE_CODE_UNION))
  604. {
  605. store_unsigned_integer (valbuf, 4, byte_order,
  606. value_address (args[argnum]));
  607. typecode = TYPE_CODE_PTR;
  608. len = 4;
  609. val = valbuf;
  610. }
  611. else if (len < 4)
  612. {
  613. /* Value gets right-justified in the register or stack word. */
  614. memcpy (valbuf + (register_size (gdbarch, argreg) - len),
  615. (gdb_byte *) value_contents (args[argnum]).data (), len);
  616. val = valbuf;
  617. }
  618. else
  619. val = (gdb_byte *) value_contents (args[argnum]).data ();
  620. while (len > 0)
  621. {
  622. if (argreg > ARGN_REGNUM)
  623. {
  624. /* Must go on the stack. */
  625. write_memory (sp + stack_offset, val, 4);
  626. stack_offset += 4;
  627. }
  628. else if (argreg <= ARGN_REGNUM)
  629. {
  630. /* There's room in a register. */
  631. regval =
  632. extract_unsigned_integer (val,
  633. register_size (gdbarch, argreg),
  634. byte_order);
  635. regcache_cooked_write_unsigned (regcache, argreg++, regval);
  636. }
  637. /* Store the value 4 bytes at a time. This means that things
  638. larger than 4 bytes may go partly in registers and partly
  639. on the stack. */
  640. len -= register_size (gdbarch, argreg);
  641. val += register_size (gdbarch, argreg);
  642. }
  643. }
  644. /* Finally, update the SP register. */
  645. regcache_cooked_write_unsigned (regcache, M32R_SP_REGNUM, sp);
  646. return sp;
  647. }
  648. /* Given a return value in `regbuf' with a type `valtype',
  649. extract and copy its value into `valbuf'. */
  650. static void
  651. m32r_extract_return_value (struct type *type, struct regcache *regcache,
  652. gdb_byte *dst)
  653. {
  654. struct gdbarch *gdbarch = regcache->arch ();
  655. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  656. int len = TYPE_LENGTH (type);
  657. ULONGEST tmp;
  658. /* By using store_unsigned_integer we avoid having to do
  659. anything special for small big-endian values. */
  660. regcache_cooked_read_unsigned (regcache, RET1_REGNUM, &tmp);
  661. store_unsigned_integer (dst, (len > 4 ? len - 4 : len), byte_order, tmp);
  662. /* Ignore return values more than 8 bytes in size because the m32r
  663. returns anything more than 8 bytes in the stack. */
  664. if (len > 4)
  665. {
  666. regcache_cooked_read_unsigned (regcache, RET1_REGNUM + 1, &tmp);
  667. store_unsigned_integer (dst + len - 4, 4, byte_order, tmp);
  668. }
  669. }
  670. static enum return_value_convention
  671. m32r_return_value (struct gdbarch *gdbarch, struct value *function,
  672. struct type *valtype, struct regcache *regcache,
  673. gdb_byte *readbuf, const gdb_byte *writebuf)
  674. {
  675. if (TYPE_LENGTH (valtype) > 8)
  676. return RETURN_VALUE_STRUCT_CONVENTION;
  677. else
  678. {
  679. if (readbuf != NULL)
  680. m32r_extract_return_value (valtype, regcache, readbuf);
  681. if (writebuf != NULL)
  682. m32r_store_return_value (valtype, regcache, writebuf);
  683. return RETURN_VALUE_REGISTER_CONVENTION;
  684. }
  685. }
  686. /* Given a GDB frame, determine the address of the calling function's
  687. frame. This will be used to create a new GDB frame struct. */
  688. static void
  689. m32r_frame_this_id (struct frame_info *this_frame,
  690. void **this_prologue_cache, struct frame_id *this_id)
  691. {
  692. struct m32r_unwind_cache *info
  693. = m32r_frame_unwind_cache (this_frame, this_prologue_cache);
  694. CORE_ADDR base;
  695. CORE_ADDR func;
  696. struct bound_minimal_symbol msym_stack;
  697. struct frame_id id;
  698. /* The FUNC is easy. */
  699. func = get_frame_func (this_frame);
  700. /* Check if the stack is empty. */
  701. msym_stack = lookup_minimal_symbol ("_stack", NULL, NULL);
  702. if (msym_stack.minsym && info->base == BMSYMBOL_VALUE_ADDRESS (msym_stack))
  703. return;
  704. /* Hopefully the prologue analysis either correctly determined the
  705. frame's base (which is the SP from the previous frame), or set
  706. that base to "NULL". */
  707. base = info->prev_sp;
  708. if (base == 0)
  709. return;
  710. id = frame_id_build (base, func);
  711. (*this_id) = id;
  712. }
  713. static struct value *
  714. m32r_frame_prev_register (struct frame_info *this_frame,
  715. void **this_prologue_cache, int regnum)
  716. {
  717. struct m32r_unwind_cache *info
  718. = m32r_frame_unwind_cache (this_frame, this_prologue_cache);
  719. return trad_frame_get_prev_register (this_frame, info->saved_regs, regnum);
  720. }
  721. static const struct frame_unwind m32r_frame_unwind = {
  722. "m32r prologue",
  723. NORMAL_FRAME,
  724. default_frame_unwind_stop_reason,
  725. m32r_frame_this_id,
  726. m32r_frame_prev_register,
  727. NULL,
  728. default_frame_sniffer
  729. };
  730. static CORE_ADDR
  731. m32r_frame_base_address (struct frame_info *this_frame, void **this_cache)
  732. {
  733. struct m32r_unwind_cache *info
  734. = m32r_frame_unwind_cache (this_frame, this_cache);
  735. return info->base;
  736. }
  737. static const struct frame_base m32r_frame_base = {
  738. &m32r_frame_unwind,
  739. m32r_frame_base_address,
  740. m32r_frame_base_address,
  741. m32r_frame_base_address
  742. };
  743. static gdbarch_init_ftype m32r_gdbarch_init;
  744. static struct gdbarch *
  745. m32r_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  746. {
  747. struct gdbarch *gdbarch;
  748. /* If there is already a candidate, use it. */
  749. arches = gdbarch_list_lookup_by_info (arches, &info);
  750. if (arches != NULL)
  751. return arches->gdbarch;
  752. /* Allocate space for the new architecture. */
  753. m32r_gdbarch_tdep *tdep = new m32r_gdbarch_tdep;
  754. gdbarch = gdbarch_alloc (&info, tdep);
  755. set_gdbarch_wchar_bit (gdbarch, 16);
  756. set_gdbarch_wchar_signed (gdbarch, 0);
  757. set_gdbarch_num_regs (gdbarch, M32R_NUM_REGS);
  758. set_gdbarch_pc_regnum (gdbarch, M32R_PC_REGNUM);
  759. set_gdbarch_sp_regnum (gdbarch, M32R_SP_REGNUM);
  760. set_gdbarch_register_name (gdbarch, m32r_register_name);
  761. set_gdbarch_register_type (gdbarch, m32r_register_type);
  762. set_gdbarch_push_dummy_call (gdbarch, m32r_push_dummy_call);
  763. set_gdbarch_return_value (gdbarch, m32r_return_value);
  764. set_gdbarch_skip_prologue (gdbarch, m32r_skip_prologue);
  765. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  766. set_gdbarch_breakpoint_kind_from_pc (gdbarch, m32r_breakpoint_kind_from_pc);
  767. set_gdbarch_sw_breakpoint_from_kind (gdbarch, m32r_sw_breakpoint_from_kind);
  768. set_gdbarch_memory_insert_breakpoint (gdbarch,
  769. m32r_memory_insert_breakpoint);
  770. set_gdbarch_memory_remove_breakpoint (gdbarch,
  771. m32r_memory_remove_breakpoint);
  772. set_gdbarch_frame_align (gdbarch, m32r_frame_align);
  773. frame_base_set_default (gdbarch, &m32r_frame_base);
  774. /* Hook in ABI-specific overrides, if they have been registered. */
  775. gdbarch_init_osabi (info, gdbarch);
  776. /* Hook in the default unwinders. */
  777. frame_unwind_append_unwinder (gdbarch, &m32r_frame_unwind);
  778. /* Support simple overlay manager. */
  779. set_gdbarch_overlay_update (gdbarch, simple_overlay_update);
  780. return gdbarch;
  781. }
  782. void _initialize_m32r_tdep ();
  783. void
  784. _initialize_m32r_tdep ()
  785. {
  786. register_gdbarch_init (bfd_arch_m32r, m32r_gdbarch_init);
  787. }