mn10300-tdep.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  1. /* Target-dependent code for the Matsushita MN10300 for GDB, the GNU debugger.
  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 "arch-utils.h"
  16. #include "dis-asm.h"
  17. #include "gdbtypes.h"
  18. #include "regcache.h"
  19. #include "gdbcore.h" /* For write_memory_unsigned_integer. */
  20. #include "value.h"
  21. #include "frame.h"
  22. #include "frame-unwind.h"
  23. #include "frame-base.h"
  24. #include "symtab.h"
  25. #include "dwarf2/frame.h"
  26. #include "osabi.h"
  27. #include "infcall.h"
  28. #include "prologue-value.h"
  29. #include "target.h"
  30. #include "mn10300-tdep.h"
  31. /* The am33-2 has 64 registers. */
  32. #define MN10300_MAX_NUM_REGS 64
  33. /* Big enough to hold the size of the largest register in bytes. */
  34. #define MN10300_MAX_REGISTER_SIZE 64
  35. /* This structure holds the results of a prologue analysis. */
  36. struct mn10300_prologue
  37. {
  38. /* The architecture for which we generated this prologue info. */
  39. struct gdbarch *gdbarch;
  40. /* The offset from the frame base to the stack pointer --- always
  41. zero or negative.
  42. Calling this a "size" is a bit misleading, but given that the
  43. stack grows downwards, using offsets for everything keeps one
  44. from going completely sign-crazy: you never change anything's
  45. sign for an ADD instruction; always change the second operand's
  46. sign for a SUB instruction; and everything takes care of
  47. itself. */
  48. int frame_size;
  49. /* Non-zero if this function has initialized the frame pointer from
  50. the stack pointer, zero otherwise. */
  51. int has_frame_ptr;
  52. /* If has_frame_ptr is non-zero, this is the offset from the frame
  53. base to where the frame pointer points. This is always zero or
  54. negative. */
  55. int frame_ptr_offset;
  56. /* The address of the first instruction at which the frame has been
  57. set up and the arguments are where the debug info says they are
  58. --- as best as we can tell. */
  59. CORE_ADDR prologue_end;
  60. /* reg_offset[R] is the offset from the CFA at which register R is
  61. saved, or 1 if register R has not been saved. (Real values are
  62. always zero or negative.) */
  63. int reg_offset[MN10300_MAX_NUM_REGS];
  64. };
  65. /* Compute the alignment required by a type. */
  66. static int
  67. mn10300_type_align (struct type *type)
  68. {
  69. int i, align = 1;
  70. switch (type->code ())
  71. {
  72. case TYPE_CODE_INT:
  73. case TYPE_CODE_ENUM:
  74. case TYPE_CODE_SET:
  75. case TYPE_CODE_RANGE:
  76. case TYPE_CODE_CHAR:
  77. case TYPE_CODE_BOOL:
  78. case TYPE_CODE_FLT:
  79. case TYPE_CODE_PTR:
  80. case TYPE_CODE_REF:
  81. case TYPE_CODE_RVALUE_REF:
  82. return TYPE_LENGTH (type);
  83. case TYPE_CODE_COMPLEX:
  84. return TYPE_LENGTH (type) / 2;
  85. case TYPE_CODE_STRUCT:
  86. case TYPE_CODE_UNION:
  87. for (i = 0; i < type->num_fields (); i++)
  88. {
  89. int falign = mn10300_type_align (type->field (i).type ());
  90. while (align < falign)
  91. align <<= 1;
  92. }
  93. return align;
  94. case TYPE_CODE_ARRAY:
  95. /* HACK! Structures containing arrays, even small ones, are not
  96. eligible for returning in registers. */
  97. return 256;
  98. case TYPE_CODE_TYPEDEF:
  99. return mn10300_type_align (check_typedef (type));
  100. default:
  101. internal_error (__FILE__, __LINE__, _("bad switch"));
  102. }
  103. }
  104. /* Should call_function allocate stack space for a struct return? */
  105. static int
  106. mn10300_use_struct_convention (struct type *type)
  107. {
  108. /* Structures bigger than a pair of words can't be returned in
  109. registers. */
  110. if (TYPE_LENGTH (type) > 8)
  111. return 1;
  112. switch (type->code ())
  113. {
  114. case TYPE_CODE_STRUCT:
  115. case TYPE_CODE_UNION:
  116. /* Structures with a single field are handled as the field
  117. itself. */
  118. if (type->num_fields () == 1)
  119. return mn10300_use_struct_convention (type->field (0).type ());
  120. /* Structures with word or double-word size are passed in memory, as
  121. long as they require at least word alignment. */
  122. if (mn10300_type_align (type) >= 4)
  123. return 0;
  124. return 1;
  125. /* Arrays are addressable, so they're never returned in
  126. registers. This condition can only hold when the array is
  127. the only field of a struct or union. */
  128. case TYPE_CODE_ARRAY:
  129. return 1;
  130. case TYPE_CODE_TYPEDEF:
  131. return mn10300_use_struct_convention (check_typedef (type));
  132. default:
  133. return 0;
  134. }
  135. }
  136. static void
  137. mn10300_store_return_value (struct gdbarch *gdbarch, struct type *type,
  138. struct regcache *regcache, const gdb_byte *valbuf)
  139. {
  140. int len = TYPE_LENGTH (type);
  141. int reg, regsz;
  142. if (type->code () == TYPE_CODE_PTR)
  143. reg = 4;
  144. else
  145. reg = 0;
  146. regsz = register_size (gdbarch, reg);
  147. if (len <= regsz)
  148. regcache->raw_write_part (reg, 0, len, valbuf);
  149. else if (len <= 2 * regsz)
  150. {
  151. regcache->raw_write (reg, valbuf);
  152. gdb_assert (regsz == register_size (gdbarch, reg + 1));
  153. regcache->raw_write_part (reg + 1, 0, len - regsz, valbuf + regsz);
  154. }
  155. else
  156. internal_error (__FILE__, __LINE__,
  157. _("Cannot store return value %d bytes long."), len);
  158. }
  159. static void
  160. mn10300_extract_return_value (struct gdbarch *gdbarch, struct type *type,
  161. struct regcache *regcache, void *valbuf)
  162. {
  163. gdb_byte buf[MN10300_MAX_REGISTER_SIZE];
  164. int len = TYPE_LENGTH (type);
  165. int reg, regsz;
  166. if (type->code () == TYPE_CODE_PTR)
  167. reg = 4;
  168. else
  169. reg = 0;
  170. regsz = register_size (gdbarch, reg);
  171. gdb_assert (regsz <= MN10300_MAX_REGISTER_SIZE);
  172. if (len <= regsz)
  173. {
  174. regcache->raw_read (reg, buf);
  175. memcpy (valbuf, buf, len);
  176. }
  177. else if (len <= 2 * regsz)
  178. {
  179. regcache->raw_read (reg, buf);
  180. memcpy (valbuf, buf, regsz);
  181. gdb_assert (regsz == register_size (gdbarch, reg + 1));
  182. regcache->raw_read (reg + 1, buf);
  183. memcpy ((char *) valbuf + regsz, buf, len - regsz);
  184. }
  185. else
  186. internal_error (__FILE__, __LINE__,
  187. _("Cannot extract return value %d bytes long."), len);
  188. }
  189. /* Determine, for architecture GDBARCH, how a return value of TYPE
  190. should be returned. If it is supposed to be returned in registers,
  191. and READBUF is non-zero, read the appropriate value from REGCACHE,
  192. and copy it into READBUF. If WRITEBUF is non-zero, write the value
  193. from WRITEBUF into REGCACHE. */
  194. static enum return_value_convention
  195. mn10300_return_value (struct gdbarch *gdbarch, struct value *function,
  196. struct type *type, struct regcache *regcache,
  197. gdb_byte *readbuf, const gdb_byte *writebuf)
  198. {
  199. if (mn10300_use_struct_convention (type))
  200. return RETURN_VALUE_STRUCT_CONVENTION;
  201. if (readbuf)
  202. mn10300_extract_return_value (gdbarch, type, regcache, readbuf);
  203. if (writebuf)
  204. mn10300_store_return_value (gdbarch, type, regcache, writebuf);
  205. return RETURN_VALUE_REGISTER_CONVENTION;
  206. }
  207. static const char *
  208. register_name (int reg, const char **regs, long sizeof_regs)
  209. {
  210. if (reg < 0 || reg >= sizeof_regs / sizeof (regs[0]))
  211. return NULL;
  212. else
  213. return regs[reg];
  214. }
  215. static const char *
  216. mn10300_generic_register_name (struct gdbarch *gdbarch, int reg)
  217. {
  218. static const char *regs[] =
  219. { "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",
  220. "sp", "pc", "mdr", "psw", "lir", "lar", "", "",
  221. "", "", "", "", "", "", "", "",
  222. "", "", "", "", "", "", "", "fp"
  223. };
  224. return register_name (reg, regs, sizeof regs);
  225. }
  226. static const char *
  227. am33_register_name (struct gdbarch *gdbarch, int reg)
  228. {
  229. static const char *regs[] =
  230. { "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",
  231. "sp", "pc", "mdr", "psw", "lir", "lar", "",
  232. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  233. "ssp", "msp", "usp", "mcrh", "mcrl", "mcvf", "", "", ""
  234. };
  235. return register_name (reg, regs, sizeof regs);
  236. }
  237. static const char *
  238. am33_2_register_name (struct gdbarch *gdbarch, int reg)
  239. {
  240. static const char *regs[] =
  241. {
  242. "d0", "d1", "d2", "d3", "a0", "a1", "a2", "a3",
  243. "sp", "pc", "mdr", "psw", "lir", "lar", "mdrq", "r0",
  244. "r1", "r2", "r3", "r4", "r5", "r6", "r7", "ssp",
  245. "msp", "usp", "mcrh", "mcrl", "mcvf", "fpcr", "", "",
  246. "fs0", "fs1", "fs2", "fs3", "fs4", "fs5", "fs6", "fs7",
  247. "fs8", "fs9", "fs10", "fs11", "fs12", "fs13", "fs14", "fs15",
  248. "fs16", "fs17", "fs18", "fs19", "fs20", "fs21", "fs22", "fs23",
  249. "fs24", "fs25", "fs26", "fs27", "fs28", "fs29", "fs30", "fs31"
  250. };
  251. return register_name (reg, regs, sizeof regs);
  252. }
  253. static struct type *
  254. mn10300_register_type (struct gdbarch *gdbarch, int reg)
  255. {
  256. return builtin_type (gdbarch)->builtin_int;
  257. }
  258. /* The breakpoint instruction must be the same size as the smallest
  259. instruction in the instruction set.
  260. The Matsushita mn10x00 processors have single byte instructions
  261. so we need a single byte breakpoint. Matsushita hasn't defined
  262. one, so we defined it ourselves. */
  263. constexpr gdb_byte mn10300_break_insn[] = {0xff};
  264. typedef BP_MANIPULATION (mn10300_break_insn) mn10300_breakpoint;
  265. /* Model the semantics of pushing a register onto the stack. This
  266. is a helper function for mn10300_analyze_prologue, below. */
  267. static void
  268. push_reg (pv_t *regs, struct pv_area *stack, int regnum)
  269. {
  270. regs[E_SP_REGNUM] = pv_add_constant (regs[E_SP_REGNUM], -4);
  271. stack->store (regs[E_SP_REGNUM], 4, regs[regnum]);
  272. }
  273. /* Translate an "r" register number extracted from an instruction encoding
  274. into a GDB register number. Adapted from a simulator function
  275. of the same name; see am33.igen. */
  276. static int
  277. translate_rreg (int rreg)
  278. {
  279. /* The higher register numbers actually correspond to the
  280. basic machine's address and data registers. */
  281. if (rreg > 7 && rreg < 12)
  282. return E_A0_REGNUM + rreg - 8;
  283. else if (rreg > 11 && rreg < 16)
  284. return E_D0_REGNUM + rreg - 12;
  285. else
  286. return E_E0_REGNUM + rreg;
  287. }
  288. /* Find saved registers in a 'struct pv_area'; we pass this to pv_area::scan.
  289. If VALUE is a saved register, ADDR says it was saved at a constant
  290. offset from the frame base, and SIZE indicates that the whole
  291. register was saved, record its offset in RESULT_UNTYPED. */
  292. static void
  293. check_for_saved (void *result_untyped, pv_t addr, CORE_ADDR size, pv_t value)
  294. {
  295. struct mn10300_prologue *result = (struct mn10300_prologue *) result_untyped;
  296. if (value.kind == pvk_register
  297. && value.k == 0
  298. && pv_is_register (addr, E_SP_REGNUM)
  299. && size == register_size (result->gdbarch, value.reg))
  300. result->reg_offset[value.reg] = addr.k;
  301. }
  302. /* Analyze the prologue to determine where registers are saved,
  303. the end of the prologue, etc. The result of this analysis is
  304. returned in RESULT. See struct mn10300_prologue above for more
  305. information. */
  306. static void
  307. mn10300_analyze_prologue (struct gdbarch *gdbarch,
  308. CORE_ADDR start_pc, CORE_ADDR limit_pc,
  309. struct mn10300_prologue *result)
  310. {
  311. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  312. CORE_ADDR pc;
  313. int rn;
  314. pv_t regs[MN10300_MAX_NUM_REGS];
  315. CORE_ADDR after_last_frame_setup_insn = start_pc;
  316. int am33_mode = get_am33_mode (gdbarch);
  317. memset (result, 0, sizeof (*result));
  318. result->gdbarch = gdbarch;
  319. for (rn = 0; rn < MN10300_MAX_NUM_REGS; rn++)
  320. {
  321. regs[rn] = pv_register (rn, 0);
  322. result->reg_offset[rn] = 1;
  323. }
  324. pv_area stack (E_SP_REGNUM, gdbarch_addr_bit (gdbarch));
  325. /* The typical call instruction will have saved the return address on the
  326. stack. Space for the return address has already been preallocated in
  327. the caller's frame. It's possible, such as when using -mrelax with gcc
  328. that other registers were saved as well. If this happens, we really
  329. have no chance of deciphering the frame. DWARF info can save the day
  330. when this happens. */
  331. stack.store (regs[E_SP_REGNUM], 4, regs[E_PC_REGNUM]);
  332. pc = start_pc;
  333. while (pc < limit_pc)
  334. {
  335. int status;
  336. gdb_byte instr[2];
  337. /* Instructions can be as small as one byte; however, we usually
  338. need at least two bytes to do the decoding, so fetch that many
  339. to begin with. */
  340. status = target_read_memory (pc, instr, 2);
  341. if (status != 0)
  342. break;
  343. /* movm [regs], sp */
  344. if (instr[0] == 0xcf)
  345. {
  346. gdb_byte save_mask;
  347. save_mask = instr[1];
  348. if ((save_mask & movm_exreg0_bit) && am33_mode)
  349. {
  350. push_reg (regs, &stack, E_E2_REGNUM);
  351. push_reg (regs, &stack, E_E3_REGNUM);
  352. }
  353. if ((save_mask & movm_exreg1_bit) && am33_mode)
  354. {
  355. push_reg (regs, &stack, E_E4_REGNUM);
  356. push_reg (regs, &stack, E_E5_REGNUM);
  357. push_reg (regs, &stack, E_E6_REGNUM);
  358. push_reg (regs, &stack, E_E7_REGNUM);
  359. }
  360. if ((save_mask & movm_exother_bit) && am33_mode)
  361. {
  362. push_reg (regs, &stack, E_E0_REGNUM);
  363. push_reg (regs, &stack, E_E1_REGNUM);
  364. push_reg (regs, &stack, E_MDRQ_REGNUM);
  365. push_reg (regs, &stack, E_MCRH_REGNUM);
  366. push_reg (regs, &stack, E_MCRL_REGNUM);
  367. push_reg (regs, &stack, E_MCVF_REGNUM);
  368. }
  369. if (save_mask & movm_d2_bit)
  370. push_reg (regs, &stack, E_D2_REGNUM);
  371. if (save_mask & movm_d3_bit)
  372. push_reg (regs, &stack, E_D3_REGNUM);
  373. if (save_mask & movm_a2_bit)
  374. push_reg (regs, &stack, E_A2_REGNUM);
  375. if (save_mask & movm_a3_bit)
  376. push_reg (regs, &stack, E_A3_REGNUM);
  377. if (save_mask & movm_other_bit)
  378. {
  379. push_reg (regs, &stack, E_D0_REGNUM);
  380. push_reg (regs, &stack, E_D1_REGNUM);
  381. push_reg (regs, &stack, E_A0_REGNUM);
  382. push_reg (regs, &stack, E_A1_REGNUM);
  383. push_reg (regs, &stack, E_MDR_REGNUM);
  384. push_reg (regs, &stack, E_LIR_REGNUM);
  385. push_reg (regs, &stack, E_LAR_REGNUM);
  386. /* The `other' bit leaves a blank area of four bytes at
  387. the beginning of its block of saved registers, making
  388. it 32 bytes long in total. */
  389. regs[E_SP_REGNUM] = pv_add_constant (regs[E_SP_REGNUM], -4);
  390. }
  391. pc += 2;
  392. after_last_frame_setup_insn = pc;
  393. }
  394. /* mov sp, aN */
  395. else if ((instr[0] & 0xfc) == 0x3c)
  396. {
  397. int aN = instr[0] & 0x03;
  398. regs[E_A0_REGNUM + aN] = regs[E_SP_REGNUM];
  399. pc += 1;
  400. if (aN == 3)
  401. after_last_frame_setup_insn = pc;
  402. }
  403. /* mov aM, aN */
  404. else if ((instr[0] & 0xf0) == 0x90
  405. && (instr[0] & 0x03) != ((instr[0] & 0x0c) >> 2))
  406. {
  407. int aN = instr[0] & 0x03;
  408. int aM = (instr[0] & 0x0c) >> 2;
  409. regs[E_A0_REGNUM + aN] = regs[E_A0_REGNUM + aM];
  410. pc += 1;
  411. }
  412. /* mov dM, dN */
  413. else if ((instr[0] & 0xf0) == 0x80
  414. && (instr[0] & 0x03) != ((instr[0] & 0x0c) >> 2))
  415. {
  416. int dN = instr[0] & 0x03;
  417. int dM = (instr[0] & 0x0c) >> 2;
  418. regs[E_D0_REGNUM + dN] = regs[E_D0_REGNUM + dM];
  419. pc += 1;
  420. }
  421. /* mov aM, dN */
  422. else if (instr[0] == 0xf1 && (instr[1] & 0xf0) == 0xd0)
  423. {
  424. int dN = instr[1] & 0x03;
  425. int aM = (instr[1] & 0x0c) >> 2;
  426. regs[E_D0_REGNUM + dN] = regs[E_A0_REGNUM + aM];
  427. pc += 2;
  428. }
  429. /* mov dM, aN */
  430. else if (instr[0] == 0xf1 && (instr[1] & 0xf0) == 0xe0)
  431. {
  432. int aN = instr[1] & 0x03;
  433. int dM = (instr[1] & 0x0c) >> 2;
  434. regs[E_A0_REGNUM + aN] = regs[E_D0_REGNUM + dM];
  435. pc += 2;
  436. }
  437. /* add imm8, SP */
  438. else if (instr[0] == 0xf8 && instr[1] == 0xfe)
  439. {
  440. gdb_byte buf[1];
  441. LONGEST imm8;
  442. status = target_read_memory (pc + 2, buf, 1);
  443. if (status != 0)
  444. break;
  445. imm8 = extract_signed_integer (buf, 1, byte_order);
  446. regs[E_SP_REGNUM] = pv_add_constant (regs[E_SP_REGNUM], imm8);
  447. pc += 3;
  448. /* Stack pointer adjustments are frame related. */
  449. after_last_frame_setup_insn = pc;
  450. }
  451. /* add imm16, SP */
  452. else if (instr[0] == 0xfa && instr[1] == 0xfe)
  453. {
  454. gdb_byte buf[2];
  455. LONGEST imm16;
  456. status = target_read_memory (pc + 2, buf, 2);
  457. if (status != 0)
  458. break;
  459. imm16 = extract_signed_integer (buf, 2, byte_order);
  460. regs[E_SP_REGNUM] = pv_add_constant (regs[E_SP_REGNUM], imm16);
  461. pc += 4;
  462. /* Stack pointer adjustments are frame related. */
  463. after_last_frame_setup_insn = pc;
  464. }
  465. /* add imm32, SP */
  466. else if (instr[0] == 0xfc && instr[1] == 0xfe)
  467. {
  468. gdb_byte buf[4];
  469. LONGEST imm32;
  470. status = target_read_memory (pc + 2, buf, 4);
  471. if (status != 0)
  472. break;
  473. imm32 = extract_signed_integer (buf, 4, byte_order);
  474. regs[E_SP_REGNUM] = pv_add_constant (regs[E_SP_REGNUM], imm32);
  475. pc += 6;
  476. /* Stack pointer adjustments are frame related. */
  477. after_last_frame_setup_insn = pc;
  478. }
  479. /* add imm8, aN */
  480. else if ((instr[0] & 0xfc) == 0x20)
  481. {
  482. int aN;
  483. LONGEST imm8;
  484. aN = instr[0] & 0x03;
  485. imm8 = extract_signed_integer (&instr[1], 1, byte_order);
  486. regs[E_A0_REGNUM + aN] = pv_add_constant (regs[E_A0_REGNUM + aN],
  487. imm8);
  488. pc += 2;
  489. }
  490. /* add imm16, aN */
  491. else if (instr[0] == 0xfa && (instr[1] & 0xfc) == 0xd0)
  492. {
  493. int aN;
  494. LONGEST imm16;
  495. gdb_byte buf[2];
  496. aN = instr[1] & 0x03;
  497. status = target_read_memory (pc + 2, buf, 2);
  498. if (status != 0)
  499. break;
  500. imm16 = extract_signed_integer (buf, 2, byte_order);
  501. regs[E_A0_REGNUM + aN] = pv_add_constant (regs[E_A0_REGNUM + aN],
  502. imm16);
  503. pc += 4;
  504. }
  505. /* add imm32, aN */
  506. else if (instr[0] == 0xfc && (instr[1] & 0xfc) == 0xd0)
  507. {
  508. int aN;
  509. LONGEST imm32;
  510. gdb_byte buf[4];
  511. aN = instr[1] & 0x03;
  512. status = target_read_memory (pc + 2, buf, 4);
  513. if (status != 0)
  514. break;
  515. imm32 = extract_signed_integer (buf, 2, byte_order);
  516. regs[E_A0_REGNUM + aN] = pv_add_constant (regs[E_A0_REGNUM + aN],
  517. imm32);
  518. pc += 6;
  519. }
  520. /* fmov fsM, (rN) */
  521. else if (instr[0] == 0xf9 && (instr[1] & 0xfd) == 0x30)
  522. {
  523. int fsM, sM, Y, rN;
  524. gdb_byte buf[1];
  525. Y = (instr[1] & 0x02) >> 1;
  526. status = target_read_memory (pc + 2, buf, 1);
  527. if (status != 0)
  528. break;
  529. sM = (buf[0] & 0xf0) >> 4;
  530. rN = buf[0] & 0x0f;
  531. fsM = (Y << 4) | sM;
  532. stack.store (regs[translate_rreg (rN)], 4,
  533. regs[E_FS0_REGNUM + fsM]);
  534. pc += 3;
  535. }
  536. /* fmov fsM, (sp) */
  537. else if (instr[0] == 0xf9 && (instr[1] & 0xfd) == 0x34)
  538. {
  539. int fsM, sM, Y;
  540. gdb_byte buf[1];
  541. Y = (instr[1] & 0x02) >> 1;
  542. status = target_read_memory (pc + 2, buf, 1);
  543. if (status != 0)
  544. break;
  545. sM = (buf[0] & 0xf0) >> 4;
  546. fsM = (Y << 4) | sM;
  547. stack.store (regs[E_SP_REGNUM], 4,
  548. regs[E_FS0_REGNUM + fsM]);
  549. pc += 3;
  550. }
  551. /* fmov fsM, (rN, rI) */
  552. else if (instr[0] == 0xfb && instr[1] == 0x37)
  553. {
  554. int fsM, sM, Z, rN, rI;
  555. gdb_byte buf[2];
  556. status = target_read_memory (pc + 2, buf, 2);
  557. if (status != 0)
  558. break;
  559. rI = (buf[0] & 0xf0) >> 4;
  560. rN = buf[0] & 0x0f;
  561. sM = (buf[1] & 0xf0) >> 4;
  562. Z = (buf[1] & 0x02) >> 1;
  563. fsM = (Z << 4) | sM;
  564. stack.store (pv_add (regs[translate_rreg (rN)],
  565. regs[translate_rreg (rI)]),
  566. 4, regs[E_FS0_REGNUM + fsM]);
  567. pc += 4;
  568. }
  569. /* fmov fsM, (d8, rN) */
  570. else if (instr[0] == 0xfb && (instr[1] & 0xfd) == 0x30)
  571. {
  572. int fsM, sM, Y, rN;
  573. LONGEST d8;
  574. gdb_byte buf[2];
  575. Y = (instr[1] & 0x02) >> 1;
  576. status = target_read_memory (pc + 2, buf, 2);
  577. if (status != 0)
  578. break;
  579. sM = (buf[0] & 0xf0) >> 4;
  580. rN = buf[0] & 0x0f;
  581. fsM = (Y << 4) | sM;
  582. d8 = extract_signed_integer (&buf[1], 1, byte_order);
  583. stack.store (pv_add_constant (regs[translate_rreg (rN)], d8),
  584. 4, regs[E_FS0_REGNUM + fsM]);
  585. pc += 4;
  586. }
  587. /* fmov fsM, (d24, rN) */
  588. else if (instr[0] == 0xfd && (instr[1] & 0xfd) == 0x30)
  589. {
  590. int fsM, sM, Y, rN;
  591. LONGEST d24;
  592. gdb_byte buf[4];
  593. Y = (instr[1] & 0x02) >> 1;
  594. status = target_read_memory (pc + 2, buf, 4);
  595. if (status != 0)
  596. break;
  597. sM = (buf[0] & 0xf0) >> 4;
  598. rN = buf[0] & 0x0f;
  599. fsM = (Y << 4) | sM;
  600. d24 = extract_signed_integer (&buf[1], 3, byte_order);
  601. stack.store (pv_add_constant (regs[translate_rreg (rN)], d24),
  602. 4, regs[E_FS0_REGNUM + fsM]);
  603. pc += 6;
  604. }
  605. /* fmov fsM, (d32, rN) */
  606. else if (instr[0] == 0xfe && (instr[1] & 0xfd) == 0x30)
  607. {
  608. int fsM, sM, Y, rN;
  609. LONGEST d32;
  610. gdb_byte buf[5];
  611. Y = (instr[1] & 0x02) >> 1;
  612. status = target_read_memory (pc + 2, buf, 5);
  613. if (status != 0)
  614. break;
  615. sM = (buf[0] & 0xf0) >> 4;
  616. rN = buf[0] & 0x0f;
  617. fsM = (Y << 4) | sM;
  618. d32 = extract_signed_integer (&buf[1], 4, byte_order);
  619. stack.store (pv_add_constant (regs[translate_rreg (rN)], d32),
  620. 4, regs[E_FS0_REGNUM + fsM]);
  621. pc += 7;
  622. }
  623. /* fmov fsM, (d8, SP) */
  624. else if (instr[0] == 0xfb && (instr[1] & 0xfd) == 0x34)
  625. {
  626. int fsM, sM, Y;
  627. LONGEST d8;
  628. gdb_byte buf[2];
  629. Y = (instr[1] & 0x02) >> 1;
  630. status = target_read_memory (pc + 2, buf, 2);
  631. if (status != 0)
  632. break;
  633. sM = (buf[0] & 0xf0) >> 4;
  634. fsM = (Y << 4) | sM;
  635. d8 = extract_signed_integer (&buf[1], 1, byte_order);
  636. stack.store (pv_add_constant (regs[E_SP_REGNUM], d8),
  637. 4, regs[E_FS0_REGNUM + fsM]);
  638. pc += 4;
  639. }
  640. /* fmov fsM, (d24, SP) */
  641. else if (instr[0] == 0xfd && (instr[1] & 0xfd) == 0x34)
  642. {
  643. int fsM, sM, Y;
  644. LONGEST d24;
  645. gdb_byte buf[4];
  646. Y = (instr[1] & 0x02) >> 1;
  647. status = target_read_memory (pc + 2, buf, 4);
  648. if (status != 0)
  649. break;
  650. sM = (buf[0] & 0xf0) >> 4;
  651. fsM = (Y << 4) | sM;
  652. d24 = extract_signed_integer (&buf[1], 3, byte_order);
  653. stack.store (pv_add_constant (regs[E_SP_REGNUM], d24),
  654. 4, regs[E_FS0_REGNUM + fsM]);
  655. pc += 6;
  656. }
  657. /* fmov fsM, (d32, SP) */
  658. else if (instr[0] == 0xfe && (instr[1] & 0xfd) == 0x34)
  659. {
  660. int fsM, sM, Y;
  661. LONGEST d32;
  662. gdb_byte buf[5];
  663. Y = (instr[1] & 0x02) >> 1;
  664. status = target_read_memory (pc + 2, buf, 5);
  665. if (status != 0)
  666. break;
  667. sM = (buf[0] & 0xf0) >> 4;
  668. fsM = (Y << 4) | sM;
  669. d32 = extract_signed_integer (&buf[1], 4, byte_order);
  670. stack.store (pv_add_constant (regs[E_SP_REGNUM], d32),
  671. 4, regs[E_FS0_REGNUM + fsM]);
  672. pc += 7;
  673. }
  674. /* fmov fsM, (rN+) */
  675. else if (instr[0] == 0xf9 && (instr[1] & 0xfd) == 0x31)
  676. {
  677. int fsM, sM, Y, rN, rN_regnum;
  678. gdb_byte buf[1];
  679. Y = (instr[1] & 0x02) >> 1;
  680. status = target_read_memory (pc + 2, buf, 1);
  681. if (status != 0)
  682. break;
  683. sM = (buf[0] & 0xf0) >> 4;
  684. rN = buf[0] & 0x0f;
  685. fsM = (Y << 4) | sM;
  686. rN_regnum = translate_rreg (rN);
  687. stack.store (regs[rN_regnum], 4,
  688. regs[E_FS0_REGNUM + fsM]);
  689. regs[rN_regnum] = pv_add_constant (regs[rN_regnum], 4);
  690. pc += 3;
  691. }
  692. /* fmov fsM, (rN+, imm8) */
  693. else if (instr[0] == 0xfb && (instr[1] & 0xfd) == 0x31)
  694. {
  695. int fsM, sM, Y, rN, rN_regnum;
  696. LONGEST imm8;
  697. gdb_byte buf[2];
  698. Y = (instr[1] & 0x02) >> 1;
  699. status = target_read_memory (pc + 2, buf, 2);
  700. if (status != 0)
  701. break;
  702. sM = (buf[0] & 0xf0) >> 4;
  703. rN = buf[0] & 0x0f;
  704. fsM = (Y << 4) | sM;
  705. imm8 = extract_signed_integer (&buf[1], 1, byte_order);
  706. rN_regnum = translate_rreg (rN);
  707. stack.store (regs[rN_regnum], 4, regs[E_FS0_REGNUM + fsM]);
  708. regs[rN_regnum] = pv_add_constant (regs[rN_regnum], imm8);
  709. pc += 4;
  710. }
  711. /* fmov fsM, (rN+, imm24) */
  712. else if (instr[0] == 0xfd && (instr[1] & 0xfd) == 0x31)
  713. {
  714. int fsM, sM, Y, rN, rN_regnum;
  715. LONGEST imm24;
  716. gdb_byte buf[4];
  717. Y = (instr[1] & 0x02) >> 1;
  718. status = target_read_memory (pc + 2, buf, 4);
  719. if (status != 0)
  720. break;
  721. sM = (buf[0] & 0xf0) >> 4;
  722. rN = buf[0] & 0x0f;
  723. fsM = (Y << 4) | sM;
  724. imm24 = extract_signed_integer (&buf[1], 3, byte_order);
  725. rN_regnum = translate_rreg (rN);
  726. stack.store (regs[rN_regnum], 4, regs[E_FS0_REGNUM + fsM]);
  727. regs[rN_regnum] = pv_add_constant (regs[rN_regnum], imm24);
  728. pc += 6;
  729. }
  730. /* fmov fsM, (rN+, imm32) */
  731. else if (instr[0] == 0xfe && (instr[1] & 0xfd) == 0x31)
  732. {
  733. int fsM, sM, Y, rN, rN_regnum;
  734. LONGEST imm32;
  735. gdb_byte buf[5];
  736. Y = (instr[1] & 0x02) >> 1;
  737. status = target_read_memory (pc + 2, buf, 5);
  738. if (status != 0)
  739. break;
  740. sM = (buf[0] & 0xf0) >> 4;
  741. rN = buf[0] & 0x0f;
  742. fsM = (Y << 4) | sM;
  743. imm32 = extract_signed_integer (&buf[1], 4, byte_order);
  744. rN_regnum = translate_rreg (rN);
  745. stack.store (regs[rN_regnum], 4, regs[E_FS0_REGNUM + fsM]);
  746. regs[rN_regnum] = pv_add_constant (regs[rN_regnum], imm32);
  747. pc += 7;
  748. }
  749. /* mov imm8, aN */
  750. else if ((instr[0] & 0xf0) == 0x90)
  751. {
  752. int aN = instr[0] & 0x03;
  753. LONGEST imm8;
  754. imm8 = extract_signed_integer (&instr[1], 1, byte_order);
  755. regs[E_A0_REGNUM + aN] = pv_constant (imm8);
  756. pc += 2;
  757. }
  758. /* mov imm16, aN */
  759. else if ((instr[0] & 0xfc) == 0x24)
  760. {
  761. int aN = instr[0] & 0x03;
  762. gdb_byte buf[2];
  763. LONGEST imm16;
  764. status = target_read_memory (pc + 1, buf, 2);
  765. if (status != 0)
  766. break;
  767. imm16 = extract_signed_integer (buf, 2, byte_order);
  768. regs[E_A0_REGNUM + aN] = pv_constant (imm16);
  769. pc += 3;
  770. }
  771. /* mov imm32, aN */
  772. else if (instr[0] == 0xfc && ((instr[1] & 0xfc) == 0xdc))
  773. {
  774. int aN = instr[1] & 0x03;
  775. gdb_byte buf[4];
  776. LONGEST imm32;
  777. status = target_read_memory (pc + 2, buf, 4);
  778. if (status != 0)
  779. break;
  780. imm32 = extract_signed_integer (buf, 4, byte_order);
  781. regs[E_A0_REGNUM + aN] = pv_constant (imm32);
  782. pc += 6;
  783. }
  784. /* mov imm8, dN */
  785. else if ((instr[0] & 0xf0) == 0x80)
  786. {
  787. int dN = instr[0] & 0x03;
  788. LONGEST imm8;
  789. imm8 = extract_signed_integer (&instr[1], 1, byte_order);
  790. regs[E_D0_REGNUM + dN] = pv_constant (imm8);
  791. pc += 2;
  792. }
  793. /* mov imm16, dN */
  794. else if ((instr[0] & 0xfc) == 0x2c)
  795. {
  796. int dN = instr[0] & 0x03;
  797. gdb_byte buf[2];
  798. LONGEST imm16;
  799. status = target_read_memory (pc + 1, buf, 2);
  800. if (status != 0)
  801. break;
  802. imm16 = extract_signed_integer (buf, 2, byte_order);
  803. regs[E_D0_REGNUM + dN] = pv_constant (imm16);
  804. pc += 3;
  805. }
  806. /* mov imm32, dN */
  807. else if (instr[0] == 0xfc && ((instr[1] & 0xfc) == 0xcc))
  808. {
  809. int dN = instr[1] & 0x03;
  810. gdb_byte buf[4];
  811. LONGEST imm32;
  812. status = target_read_memory (pc + 2, buf, 4);
  813. if (status != 0)
  814. break;
  815. imm32 = extract_signed_integer (buf, 4, byte_order);
  816. regs[E_D0_REGNUM + dN] = pv_constant (imm32);
  817. pc += 6;
  818. }
  819. else
  820. {
  821. /* We've hit some instruction that we don't recognize. Hopefully,
  822. we have enough to do prologue analysis. */
  823. break;
  824. }
  825. }
  826. /* Is the frame size (offset, really) a known constant? */
  827. if (pv_is_register (regs[E_SP_REGNUM], E_SP_REGNUM))
  828. result->frame_size = regs[E_SP_REGNUM].k;
  829. /* Was the frame pointer initialized? */
  830. if (pv_is_register (regs[E_A3_REGNUM], E_SP_REGNUM))
  831. {
  832. result->has_frame_ptr = 1;
  833. result->frame_ptr_offset = regs[E_A3_REGNUM].k;
  834. }
  835. /* Record where all the registers were saved. */
  836. stack.scan (check_for_saved, (void *) result);
  837. result->prologue_end = after_last_frame_setup_insn;
  838. }
  839. /* Function: skip_prologue
  840. Return the address of the first inst past the prologue of the function. */
  841. static CORE_ADDR
  842. mn10300_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  843. {
  844. const char *name;
  845. CORE_ADDR func_addr, func_end;
  846. struct mn10300_prologue p;
  847. /* Try to find the extent of the function that contains PC. */
  848. if (!find_pc_partial_function (pc, &name, &func_addr, &func_end))
  849. return pc;
  850. mn10300_analyze_prologue (gdbarch, pc, func_end, &p);
  851. return p.prologue_end;
  852. }
  853. /* Wrapper for mn10300_analyze_prologue: find the function start;
  854. use the current frame PC as the limit, then
  855. invoke mn10300_analyze_prologue and return its result. */
  856. static struct mn10300_prologue *
  857. mn10300_analyze_frame_prologue (struct frame_info *this_frame,
  858. void **this_prologue_cache)
  859. {
  860. if (!*this_prologue_cache)
  861. {
  862. CORE_ADDR func_start, stop_addr;
  863. *this_prologue_cache = FRAME_OBSTACK_ZALLOC (struct mn10300_prologue);
  864. func_start = get_frame_func (this_frame);
  865. stop_addr = get_frame_pc (this_frame);
  866. /* If we couldn't find any function containing the PC, then
  867. just initialize the prologue cache, but don't do anything. */
  868. if (!func_start)
  869. stop_addr = func_start;
  870. mn10300_analyze_prologue (get_frame_arch (this_frame),
  871. func_start, stop_addr,
  872. ((struct mn10300_prologue *)
  873. *this_prologue_cache));
  874. }
  875. return (struct mn10300_prologue *) *this_prologue_cache;
  876. }
  877. /* Given the next frame and a prologue cache, return this frame's
  878. base. */
  879. static CORE_ADDR
  880. mn10300_frame_base (struct frame_info *this_frame, void **this_prologue_cache)
  881. {
  882. struct mn10300_prologue *p
  883. = mn10300_analyze_frame_prologue (this_frame, this_prologue_cache);
  884. /* In functions that use alloca, the distance between the stack
  885. pointer and the frame base varies dynamically, so we can't use
  886. the SP plus static information like prologue analysis to find the
  887. frame base. However, such functions must have a frame pointer,
  888. to be able to restore the SP on exit. So whenever we do have a
  889. frame pointer, use that to find the base. */
  890. if (p->has_frame_ptr)
  891. {
  892. CORE_ADDR fp = get_frame_register_unsigned (this_frame, E_A3_REGNUM);
  893. return fp - p->frame_ptr_offset;
  894. }
  895. else
  896. {
  897. CORE_ADDR sp = get_frame_register_unsigned (this_frame, E_SP_REGNUM);
  898. return sp - p->frame_size;
  899. }
  900. }
  901. static void
  902. mn10300_frame_this_id (struct frame_info *this_frame,
  903. void **this_prologue_cache,
  904. struct frame_id *this_id)
  905. {
  906. *this_id = frame_id_build (mn10300_frame_base (this_frame,
  907. this_prologue_cache),
  908. get_frame_func (this_frame));
  909. }
  910. static struct value *
  911. mn10300_frame_prev_register (struct frame_info *this_frame,
  912. void **this_prologue_cache, int regnum)
  913. {
  914. struct mn10300_prologue *p
  915. = mn10300_analyze_frame_prologue (this_frame, this_prologue_cache);
  916. CORE_ADDR frame_base = mn10300_frame_base (this_frame, this_prologue_cache);
  917. if (regnum == E_SP_REGNUM)
  918. return frame_unwind_got_constant (this_frame, regnum, frame_base);
  919. /* If prologue analysis says we saved this register somewhere,
  920. return a description of the stack slot holding it. */
  921. if (p->reg_offset[regnum] != 1)
  922. return frame_unwind_got_memory (this_frame, regnum,
  923. frame_base + p->reg_offset[regnum]);
  924. /* Otherwise, presume we haven't changed the value of this
  925. register, and get it from the next frame. */
  926. return frame_unwind_got_register (this_frame, regnum, regnum);
  927. }
  928. static const struct frame_unwind mn10300_frame_unwind = {
  929. "mn10300 prologue",
  930. NORMAL_FRAME,
  931. default_frame_unwind_stop_reason,
  932. mn10300_frame_this_id,
  933. mn10300_frame_prev_register,
  934. NULL,
  935. default_frame_sniffer
  936. };
  937. static void
  938. mn10300_frame_unwind_init (struct gdbarch *gdbarch)
  939. {
  940. dwarf2_append_unwinders (gdbarch);
  941. frame_unwind_append_unwinder (gdbarch, &mn10300_frame_unwind);
  942. }
  943. /* Function: push_dummy_call
  944. *
  945. * Set up machine state for a target call, including
  946. * function arguments, stack, return address, etc.
  947. *
  948. */
  949. static CORE_ADDR
  950. mn10300_push_dummy_call (struct gdbarch *gdbarch,
  951. struct value *target_func,
  952. struct regcache *regcache,
  953. CORE_ADDR bp_addr,
  954. int nargs, struct value **args,
  955. CORE_ADDR sp,
  956. function_call_return_method return_method,
  957. CORE_ADDR struct_addr)
  958. {
  959. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  960. const int push_size = register_size (gdbarch, E_PC_REGNUM);
  961. int regs_used;
  962. int len, arg_len;
  963. int stack_offset = 0;
  964. int argnum;
  965. const gdb_byte *val;
  966. gdb_byte valbuf[MN10300_MAX_REGISTER_SIZE];
  967. /* This should be a nop, but align the stack just in case something
  968. went wrong. Stacks are four byte aligned on the mn10300. */
  969. sp &= ~3;
  970. /* Now make space on the stack for the args.
  971. XXX This doesn't appear to handle pass-by-invisible reference
  972. arguments. */
  973. regs_used = (return_method == return_method_struct) ? 1 : 0;
  974. for (len = 0, argnum = 0; argnum < nargs; argnum++)
  975. {
  976. arg_len = (TYPE_LENGTH (value_type (args[argnum])) + 3) & ~3;
  977. while (regs_used < 2 && arg_len > 0)
  978. {
  979. regs_used++;
  980. arg_len -= push_size;
  981. }
  982. len += arg_len;
  983. }
  984. /* Allocate stack space. */
  985. sp -= len;
  986. if (return_method == return_method_struct)
  987. {
  988. regs_used = 1;
  989. regcache_cooked_write_unsigned (regcache, E_D0_REGNUM, struct_addr);
  990. }
  991. else
  992. regs_used = 0;
  993. /* Push all arguments onto the stack. */
  994. for (argnum = 0; argnum < nargs; argnum++)
  995. {
  996. /* FIXME what about structs? Unions? */
  997. if (value_type (*args)->code () == TYPE_CODE_STRUCT
  998. && TYPE_LENGTH (value_type (*args)) > 8)
  999. {
  1000. /* Change to pointer-to-type. */
  1001. arg_len = push_size;
  1002. gdb_assert (push_size <= MN10300_MAX_REGISTER_SIZE);
  1003. store_unsigned_integer (valbuf, push_size, byte_order,
  1004. value_address (*args));
  1005. val = &valbuf[0];
  1006. }
  1007. else
  1008. {
  1009. arg_len = TYPE_LENGTH (value_type (*args));
  1010. val = value_contents (*args).data ();
  1011. }
  1012. while (regs_used < 2 && arg_len > 0)
  1013. {
  1014. regcache_cooked_write_unsigned (regcache, regs_used,
  1015. extract_unsigned_integer (val, push_size, byte_order));
  1016. val += push_size;
  1017. arg_len -= push_size;
  1018. regs_used++;
  1019. }
  1020. while (arg_len > 0)
  1021. {
  1022. write_memory (sp + stack_offset, val, push_size);
  1023. arg_len -= push_size;
  1024. val += push_size;
  1025. stack_offset += push_size;
  1026. }
  1027. args++;
  1028. }
  1029. /* Make space for the flushback area. */
  1030. sp -= 8;
  1031. /* Push the return address that contains the magic breakpoint. */
  1032. sp -= 4;
  1033. write_memory_unsigned_integer (sp, push_size, byte_order, bp_addr);
  1034. /* The CPU also writes the return address always into the
  1035. MDR register on "call". */
  1036. regcache_cooked_write_unsigned (regcache, E_MDR_REGNUM, bp_addr);
  1037. /* Update $sp. */
  1038. regcache_cooked_write_unsigned (regcache, E_SP_REGNUM, sp);
  1039. /* On the mn10300, it's possible to move some of the stack adjustment
  1040. and saving of the caller-save registers out of the prologue and
  1041. into the call sites. (When using gcc, this optimization can
  1042. occur when using the -mrelax switch.) If this occurs, the dwarf2
  1043. info will reflect this fact. We can test to see if this is the
  1044. case by creating a new frame using the current stack pointer and
  1045. the address of the function that we're about to call. We then
  1046. unwind SP and see if it's different than the SP of our newly
  1047. created frame. If the SP values are the same, the caller is not
  1048. expected to allocate any additional stack. On the other hand, if
  1049. the SP values are different, the difference determines the
  1050. additional stack that must be allocated.
  1051. Note that we don't update the return value though because that's
  1052. the value of the stack just after pushing the arguments, but prior
  1053. to performing the call. This value is needed in order to
  1054. construct the frame ID of the dummy call. */
  1055. {
  1056. CORE_ADDR func_addr = find_function_addr (target_func, NULL);
  1057. CORE_ADDR unwound_sp
  1058. = gdbarch_unwind_sp (gdbarch, create_new_frame (sp, func_addr));
  1059. if (sp != unwound_sp)
  1060. regcache_cooked_write_unsigned (regcache, E_SP_REGNUM,
  1061. sp - (unwound_sp - sp));
  1062. }
  1063. return sp;
  1064. }
  1065. /* If DWARF2 is a register number appearing in Dwarf2 debug info, then
  1066. mn10300_dwarf2_reg_to_regnum (DWARF2) is the corresponding GDB
  1067. register number. Why don't Dwarf2 and GDB use the same numbering?
  1068. Who knows? But since people have object files lying around with
  1069. the existing Dwarf2 numbering, and other people have written stubs
  1070. to work with the existing GDB, neither of them can change. So we
  1071. just have to cope. */
  1072. static int
  1073. mn10300_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int dwarf2)
  1074. {
  1075. /* This table is supposed to be shaped like the gdbarch_register_name
  1076. initializer in gcc/config/mn10300/mn10300.h. Registers which
  1077. appear in GCC's numbering, but have no counterpart in GDB's
  1078. world, are marked with a -1. */
  1079. static int dwarf2_to_gdb[] = {
  1080. E_D0_REGNUM, E_D1_REGNUM, E_D2_REGNUM, E_D3_REGNUM,
  1081. E_A0_REGNUM, E_A1_REGNUM, E_A2_REGNUM, E_A3_REGNUM,
  1082. -1, E_SP_REGNUM,
  1083. E_E0_REGNUM, E_E1_REGNUM, E_E2_REGNUM, E_E3_REGNUM,
  1084. E_E4_REGNUM, E_E5_REGNUM, E_E6_REGNUM, E_E7_REGNUM,
  1085. E_FS0_REGNUM + 0, E_FS0_REGNUM + 1, E_FS0_REGNUM + 2, E_FS0_REGNUM + 3,
  1086. E_FS0_REGNUM + 4, E_FS0_REGNUM + 5, E_FS0_REGNUM + 6, E_FS0_REGNUM + 7,
  1087. E_FS0_REGNUM + 8, E_FS0_REGNUM + 9, E_FS0_REGNUM + 10, E_FS0_REGNUM + 11,
  1088. E_FS0_REGNUM + 12, E_FS0_REGNUM + 13, E_FS0_REGNUM + 14, E_FS0_REGNUM + 15,
  1089. E_FS0_REGNUM + 16, E_FS0_REGNUM + 17, E_FS0_REGNUM + 18, E_FS0_REGNUM + 19,
  1090. E_FS0_REGNUM + 20, E_FS0_REGNUM + 21, E_FS0_REGNUM + 22, E_FS0_REGNUM + 23,
  1091. E_FS0_REGNUM + 24, E_FS0_REGNUM + 25, E_FS0_REGNUM + 26, E_FS0_REGNUM + 27,
  1092. E_FS0_REGNUM + 28, E_FS0_REGNUM + 29, E_FS0_REGNUM + 30, E_FS0_REGNUM + 31,
  1093. E_MDR_REGNUM, E_PSW_REGNUM, E_PC_REGNUM
  1094. };
  1095. if (dwarf2 < 0
  1096. || dwarf2 >= ARRAY_SIZE (dwarf2_to_gdb))
  1097. return -1;
  1098. return dwarf2_to_gdb[dwarf2];
  1099. }
  1100. static struct gdbarch *
  1101. mn10300_gdbarch_init (struct gdbarch_info info,
  1102. struct gdbarch_list *arches)
  1103. {
  1104. struct gdbarch *gdbarch;
  1105. int num_regs;
  1106. arches = gdbarch_list_lookup_by_info (arches, &info);
  1107. if (arches != NULL)
  1108. return arches->gdbarch;
  1109. mn10300_gdbarch_tdep *tdep = new mn10300_gdbarch_tdep;
  1110. gdbarch = gdbarch_alloc (&info, tdep);
  1111. switch (info.bfd_arch_info->mach)
  1112. {
  1113. case 0:
  1114. case bfd_mach_mn10300:
  1115. set_gdbarch_register_name (gdbarch, mn10300_generic_register_name);
  1116. tdep->am33_mode = 0;
  1117. num_regs = 32;
  1118. break;
  1119. case bfd_mach_am33:
  1120. set_gdbarch_register_name (gdbarch, am33_register_name);
  1121. tdep->am33_mode = 1;
  1122. num_regs = 32;
  1123. break;
  1124. case bfd_mach_am33_2:
  1125. set_gdbarch_register_name (gdbarch, am33_2_register_name);
  1126. tdep->am33_mode = 2;
  1127. num_regs = 64;
  1128. set_gdbarch_fp0_regnum (gdbarch, 32);
  1129. break;
  1130. default:
  1131. internal_error (__FILE__, __LINE__,
  1132. _("mn10300_gdbarch_init: Unknown mn10300 variant"));
  1133. break;
  1134. }
  1135. /* By default, chars are unsigned. */
  1136. set_gdbarch_char_signed (gdbarch, 0);
  1137. /* Registers. */
  1138. set_gdbarch_num_regs (gdbarch, num_regs);
  1139. set_gdbarch_register_type (gdbarch, mn10300_register_type);
  1140. set_gdbarch_skip_prologue (gdbarch, mn10300_skip_prologue);
  1141. set_gdbarch_pc_regnum (gdbarch, E_PC_REGNUM);
  1142. set_gdbarch_sp_regnum (gdbarch, E_SP_REGNUM);
  1143. set_gdbarch_dwarf2_reg_to_regnum (gdbarch, mn10300_dwarf2_reg_to_regnum);
  1144. /* Stack unwinding. */
  1145. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  1146. /* Breakpoints. */
  1147. set_gdbarch_breakpoint_kind_from_pc (gdbarch,
  1148. mn10300_breakpoint::kind_from_pc);
  1149. set_gdbarch_sw_breakpoint_from_kind (gdbarch,
  1150. mn10300_breakpoint::bp_from_kind);
  1151. /* decr_pc_after_break? */
  1152. /* Stage 2 */
  1153. set_gdbarch_return_value (gdbarch, mn10300_return_value);
  1154. /* Stage 3 -- get target calls working. */
  1155. set_gdbarch_push_dummy_call (gdbarch, mn10300_push_dummy_call);
  1156. /* set_gdbarch_return_value (store, extract) */
  1157. mn10300_frame_unwind_init (gdbarch);
  1158. /* Hook in ABI-specific overrides, if they have been registered. */
  1159. gdbarch_init_osabi (info, gdbarch);
  1160. return gdbarch;
  1161. }
  1162. /* Dump out the mn10300 specific architecture information. */
  1163. static void
  1164. mn10300_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
  1165. {
  1166. mn10300_gdbarch_tdep *tdep = (mn10300_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1167. gdb_printf (file, "mn10300_dump_tdep: am33_mode = %d\n",
  1168. tdep->am33_mode);
  1169. }
  1170. void _initialize_mn10300_tdep ();
  1171. void
  1172. _initialize_mn10300_tdep ()
  1173. {
  1174. gdbarch_register (bfd_arch_mn10300, mn10300_gdbarch_init, mn10300_dump_tdep);
  1175. }