h8300-tdep.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  1. /* Target-machine dependent code for Renesas H8/300, for GDB.
  2. Copyright (C) 1988-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. /*
  15. Contributed by Steve Chamberlain
  16. sac@cygnus.com
  17. */
  18. #include "defs.h"
  19. #include "value.h"
  20. #include "arch-utils.h"
  21. #include "regcache.h"
  22. #include "gdbcore.h"
  23. #include "objfiles.h"
  24. #include "dis-asm.h"
  25. #include "dwarf2/frame.h"
  26. #include "frame-base.h"
  27. #include "frame-unwind.h"
  28. enum gdb_regnum
  29. {
  30. E_R0_REGNUM, E_ER0_REGNUM = E_R0_REGNUM, E_ARG0_REGNUM = E_R0_REGNUM,
  31. E_RET0_REGNUM = E_R0_REGNUM,
  32. E_R1_REGNUM, E_ER1_REGNUM = E_R1_REGNUM, E_RET1_REGNUM = E_R1_REGNUM,
  33. E_R2_REGNUM, E_ER2_REGNUM = E_R2_REGNUM, E_ARGLAST_REGNUM = E_R2_REGNUM,
  34. E_R3_REGNUM, E_ER3_REGNUM = E_R3_REGNUM,
  35. E_R4_REGNUM, E_ER4_REGNUM = E_R4_REGNUM,
  36. E_R5_REGNUM, E_ER5_REGNUM = E_R5_REGNUM,
  37. E_R6_REGNUM, E_ER6_REGNUM = E_R6_REGNUM, E_FP_REGNUM = E_R6_REGNUM,
  38. E_SP_REGNUM,
  39. E_CCR_REGNUM,
  40. E_PC_REGNUM,
  41. E_CYCLES_REGNUM,
  42. E_TICK_REGNUM, E_EXR_REGNUM = E_TICK_REGNUM,
  43. E_INST_REGNUM, E_TICKS_REGNUM = E_INST_REGNUM,
  44. E_INSTS_REGNUM,
  45. E_MACH_REGNUM,
  46. E_MACL_REGNUM,
  47. E_SBR_REGNUM,
  48. E_VBR_REGNUM
  49. };
  50. #define H8300_MAX_NUM_REGS 18
  51. #define E_PSEUDO_CCR_REGNUM(gdbarch) (gdbarch_num_regs (gdbarch))
  52. #define E_PSEUDO_EXR_REGNUM(gdbarch) (gdbarch_num_regs (gdbarch)+1)
  53. struct h8300_frame_cache
  54. {
  55. /* Base address. */
  56. CORE_ADDR base;
  57. CORE_ADDR sp_offset;
  58. CORE_ADDR pc;
  59. /* Flag showing that a frame has been created in the prologue code. */
  60. int uses_fp;
  61. /* Saved registers. */
  62. CORE_ADDR saved_regs[H8300_MAX_NUM_REGS];
  63. CORE_ADDR saved_sp;
  64. };
  65. enum
  66. {
  67. h8300_reg_size = 2,
  68. h8300h_reg_size = 4,
  69. h8300_max_reg_size = 4,
  70. };
  71. static int is_h8300hmode (struct gdbarch *gdbarch);
  72. static int is_h8300smode (struct gdbarch *gdbarch);
  73. static int is_h8300sxmode (struct gdbarch *gdbarch);
  74. static int is_h8300_normal_mode (struct gdbarch *gdbarch);
  75. #define BINWORD(gdbarch) ((is_h8300hmode (gdbarch) \
  76. && !is_h8300_normal_mode (gdbarch)) \
  77. ? h8300h_reg_size : h8300_reg_size)
  78. /* Normal frames. */
  79. /* Allocate and initialize a frame cache. */
  80. static void
  81. h8300_init_frame_cache (struct gdbarch *gdbarch,
  82. struct h8300_frame_cache *cache)
  83. {
  84. int i;
  85. /* Base address. */
  86. cache->base = 0;
  87. cache->sp_offset = 0;
  88. cache->pc = 0;
  89. /* Frameless until proven otherwise. */
  90. cache->uses_fp = 0;
  91. /* Saved registers. We initialize these to -1 since zero is a valid
  92. offset (that's where %fp is supposed to be stored). */
  93. for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
  94. cache->saved_regs[i] = -1;
  95. }
  96. #define IS_MOVB_RnRm(x) (((x) & 0xff88) == 0x0c88)
  97. #define IS_MOVW_RnRm(x) (((x) & 0xff88) == 0x0d00)
  98. #define IS_MOVL_RnRm(x) (((x) & 0xff88) == 0x0f80)
  99. #define IS_MOVB_Rn16_SP(x) (((x) & 0xfff0) == 0x6ee0)
  100. #define IS_MOVB_EXT(x) ((x) == 0x7860)
  101. #define IS_MOVB_Rn24_SP(x) (((x) & 0xfff0) == 0x6aa0)
  102. #define IS_MOVW_Rn16_SP(x) (((x) & 0xfff0) == 0x6fe0)
  103. #define IS_MOVW_EXT(x) ((x) == 0x78e0)
  104. #define IS_MOVW_Rn24_SP(x) (((x) & 0xfff0) == 0x6ba0)
  105. /* Same instructions as mov.w, just prefixed with 0x0100. */
  106. #define IS_MOVL_PRE(x) ((x) == 0x0100)
  107. #define IS_MOVL_Rn16_SP(x) (((x) & 0xfff0) == 0x6fe0)
  108. #define IS_MOVL_EXT(x) ((x) == 0x78e0)
  109. #define IS_MOVL_Rn24_SP(x) (((x) & 0xfff0) == 0x6ba0)
  110. #define IS_PUSHFP_MOVESPFP(x) ((x) == 0x6df60d76)
  111. #define IS_PUSH_FP(x) ((x) == 0x01006df6)
  112. #define IS_MOV_SP_FP(x) ((x) == 0x0ff6)
  113. #define IS_SUB2_SP(x) ((x) == 0x1b87)
  114. #define IS_SUB4_SP(x) ((x) == 0x1b97)
  115. #define IS_ADD_IMM_SP(x) ((x) == 0x7a1f)
  116. #define IS_SUB_IMM_SP(x) ((x) == 0x7a3f)
  117. #define IS_SUBL4_SP(x) ((x) == 0x1acf)
  118. #define IS_MOV_IMM_Rn(x) (((x) & 0xfff0) == 0x7905)
  119. #define IS_SUB_RnSP(x) (((x) & 0xff0f) == 0x1907)
  120. #define IS_ADD_RnSP(x) (((x) & 0xff0f) == 0x0907)
  121. #define IS_PUSH(x) (((x) & 0xfff0) == 0x6df0)
  122. /* If the instruction at PC is an argument register spill, return its
  123. length. Otherwise, return zero.
  124. An argument register spill is an instruction that moves an argument
  125. from the register in which it was passed to the stack slot in which
  126. it really lives. It is a byte, word, or longword move from an
  127. argument register to a negative offset from the frame pointer.
  128. CV, 2003-06-16: Or, in optimized code or when the `register' qualifier
  129. is used, it could be a byte, word or long move to registers r3-r5. */
  130. static int
  131. h8300_is_argument_spill (struct gdbarch *gdbarch, CORE_ADDR pc)
  132. {
  133. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  134. int w = read_memory_unsigned_integer (pc, 2, byte_order);
  135. if ((IS_MOVB_RnRm (w) || IS_MOVW_RnRm (w) || IS_MOVL_RnRm (w))
  136. && (w & 0x70) <= 0x20 /* Rs is R0, R1 or R2 */
  137. && (w & 0x7) >= 0x3 && (w & 0x7) <= 0x5) /* Rd is R3, R4 or R5 */
  138. return 2;
  139. if (IS_MOVB_Rn16_SP (w)
  140. && 8 <= (w & 0xf) && (w & 0xf) <= 10) /* Rs is R0L, R1L, or R2L */
  141. {
  142. /* ... and d:16 is negative. */
  143. if (read_memory_integer (pc + 2, 2, byte_order) < 0)
  144. return 4;
  145. }
  146. else if (IS_MOVB_EXT (w))
  147. {
  148. if (IS_MOVB_Rn24_SP (read_memory_unsigned_integer (pc + 2,
  149. 2, byte_order)))
  150. {
  151. ULONGEST disp = read_memory_unsigned_integer (pc + 4, 4, byte_order);
  152. /* ... and d:24 is negative. */
  153. if ((disp & 0x00800000) != 0)
  154. return 8;
  155. }
  156. }
  157. else if (IS_MOVW_Rn16_SP (w)
  158. && (w & 0xf) <= 2) /* Rs is R0, R1, or R2 */
  159. {
  160. /* ... and d:16 is negative. */
  161. if (read_memory_integer (pc + 2, 2, byte_order) < 0)
  162. return 4;
  163. }
  164. else if (IS_MOVW_EXT (w))
  165. {
  166. if (IS_MOVW_Rn24_SP (read_memory_unsigned_integer (pc + 2,
  167. 2, byte_order)))
  168. {
  169. ULONGEST disp = read_memory_unsigned_integer (pc + 4, 4, byte_order);
  170. /* ... and d:24 is negative. */
  171. if ((disp & 0x00800000) != 0)
  172. return 8;
  173. }
  174. }
  175. else if (IS_MOVL_PRE (w))
  176. {
  177. int w2 = read_memory_integer (pc + 2, 2, byte_order);
  178. if (IS_MOVL_Rn16_SP (w2)
  179. && (w2 & 0xf) <= 2) /* Rs is ER0, ER1, or ER2 */
  180. {
  181. /* ... and d:16 is negative. */
  182. if (read_memory_integer (pc + 4, 2, byte_order) < 0)
  183. return 6;
  184. }
  185. else if (IS_MOVL_EXT (w2))
  186. {
  187. if (IS_MOVL_Rn24_SP (read_memory_integer (pc + 4, 2, byte_order)))
  188. {
  189. ULONGEST disp = read_memory_unsigned_integer (pc + 6, 4,
  190. byte_order);
  191. /* ... and d:24 is negative. */
  192. if ((disp & 0x00800000) != 0)
  193. return 10;
  194. }
  195. }
  196. }
  197. return 0;
  198. }
  199. /* Do a full analysis of the prologue at PC and update CACHE
  200. accordingly. Bail out early if CURRENT_PC is reached. Return the
  201. address where the analysis stopped.
  202. We handle all cases that can be generated by gcc.
  203. For allocating a stack frame:
  204. mov.w r6,@-sp
  205. mov.w sp,r6
  206. mov.w #-n,rN
  207. add.w rN,sp
  208. mov.w r6,@-sp
  209. mov.w sp,r6
  210. subs #2,sp
  211. (repeat)
  212. mov.l er6,@-sp
  213. mov.l sp,er6
  214. add.l #-n,sp
  215. mov.w r6,@-sp
  216. mov.w sp,r6
  217. subs #4,sp
  218. (repeat)
  219. For saving registers:
  220. mov.w rN,@-sp
  221. mov.l erN,@-sp
  222. stm.l reglist,@-sp
  223. */
  224. static CORE_ADDR
  225. h8300_analyze_prologue (struct gdbarch *gdbarch,
  226. CORE_ADDR pc, CORE_ADDR current_pc,
  227. struct h8300_frame_cache *cache)
  228. {
  229. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  230. unsigned int op;
  231. int regno, i, spill_size;
  232. cache->sp_offset = 0;
  233. if (pc >= current_pc)
  234. return current_pc;
  235. op = read_memory_unsigned_integer (pc, 4, byte_order);
  236. if (IS_PUSHFP_MOVESPFP (op))
  237. {
  238. cache->saved_regs[E_FP_REGNUM] = 0;
  239. cache->uses_fp = 1;
  240. pc += 4;
  241. }
  242. else if (IS_PUSH_FP (op))
  243. {
  244. cache->saved_regs[E_FP_REGNUM] = 0;
  245. pc += 4;
  246. if (pc >= current_pc)
  247. return current_pc;
  248. op = read_memory_unsigned_integer (pc, 2, byte_order);
  249. if (IS_MOV_SP_FP (op))
  250. {
  251. cache->uses_fp = 1;
  252. pc += 2;
  253. }
  254. }
  255. while (pc < current_pc)
  256. {
  257. op = read_memory_unsigned_integer (pc, 2, byte_order);
  258. if (IS_SUB2_SP (op))
  259. {
  260. cache->sp_offset += 2;
  261. pc += 2;
  262. }
  263. else if (IS_SUB4_SP (op))
  264. {
  265. cache->sp_offset += 4;
  266. pc += 2;
  267. }
  268. else if (IS_ADD_IMM_SP (op))
  269. {
  270. cache->sp_offset += -read_memory_integer (pc + 2, 2, byte_order);
  271. pc += 4;
  272. }
  273. else if (IS_SUB_IMM_SP (op))
  274. {
  275. cache->sp_offset += read_memory_integer (pc + 2, 2, byte_order);
  276. pc += 4;
  277. }
  278. else if (IS_SUBL4_SP (op))
  279. {
  280. cache->sp_offset += 4;
  281. pc += 2;
  282. }
  283. else if (IS_MOV_IMM_Rn (op))
  284. {
  285. int offset = read_memory_integer (pc + 2, 2, byte_order);
  286. regno = op & 0x000f;
  287. op = read_memory_unsigned_integer (pc + 4, 2, byte_order);
  288. if (IS_ADD_RnSP (op) && (op & 0x00f0) == regno)
  289. {
  290. cache->sp_offset -= offset;
  291. pc += 6;
  292. }
  293. else if (IS_SUB_RnSP (op) && (op & 0x00f0) == regno)
  294. {
  295. cache->sp_offset += offset;
  296. pc += 6;
  297. }
  298. else
  299. break;
  300. }
  301. else if (IS_PUSH (op))
  302. {
  303. regno = op & 0x000f;
  304. cache->sp_offset += 2;
  305. cache->saved_regs[regno] = cache->sp_offset;
  306. pc += 2;
  307. }
  308. else if (op == 0x0100)
  309. {
  310. op = read_memory_unsigned_integer (pc + 2, 2, byte_order);
  311. if (IS_PUSH (op))
  312. {
  313. regno = op & 0x000f;
  314. cache->sp_offset += 4;
  315. cache->saved_regs[regno] = cache->sp_offset;
  316. pc += 4;
  317. }
  318. else
  319. break;
  320. }
  321. else if ((op & 0xffcf) == 0x0100)
  322. {
  323. int op1;
  324. op1 = read_memory_unsigned_integer (pc + 2, 2, byte_order);
  325. if (IS_PUSH (op1))
  326. {
  327. /* Since the prefix is 0x01x0, this is not a simple pushm but a
  328. stm.l reglist,@-sp */
  329. i = ((op & 0x0030) >> 4) + 1;
  330. regno = op1 & 0x000f;
  331. for (; i > 0; regno++, --i)
  332. {
  333. cache->sp_offset += 4;
  334. cache->saved_regs[regno] = cache->sp_offset;
  335. }
  336. pc += 4;
  337. }
  338. else
  339. break;
  340. }
  341. else
  342. break;
  343. }
  344. /* Check for spilling an argument register to the stack frame.
  345. This could also be an initializing store from non-prologue code,
  346. but I don't think there's any harm in skipping that. */
  347. while ((spill_size = h8300_is_argument_spill (gdbarch, pc)) > 0
  348. && pc + spill_size <= current_pc)
  349. pc += spill_size;
  350. return pc;
  351. }
  352. static struct h8300_frame_cache *
  353. h8300_frame_cache (struct frame_info *this_frame, void **this_cache)
  354. {
  355. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  356. struct h8300_frame_cache *cache;
  357. int i;
  358. CORE_ADDR current_pc;
  359. if (*this_cache)
  360. return (struct h8300_frame_cache *) *this_cache;
  361. cache = FRAME_OBSTACK_ZALLOC (struct h8300_frame_cache);
  362. h8300_init_frame_cache (gdbarch, cache);
  363. *this_cache = cache;
  364. /* In principle, for normal frames, %fp holds the frame pointer,
  365. which holds the base address for the current stack frame.
  366. However, for functions that don't need it, the frame pointer is
  367. optional. For these "frameless" functions the frame pointer is
  368. actually the frame pointer of the calling frame. */
  369. cache->base = get_frame_register_unsigned (this_frame, E_FP_REGNUM);
  370. if (cache->base == 0)
  371. return cache;
  372. cache->saved_regs[E_PC_REGNUM] = -BINWORD (gdbarch);
  373. cache->pc = get_frame_func (this_frame);
  374. current_pc = get_frame_pc (this_frame);
  375. if (cache->pc != 0)
  376. h8300_analyze_prologue (gdbarch, cache->pc, current_pc, cache);
  377. if (!cache->uses_fp)
  378. {
  379. /* We didn't find a valid frame, which means that CACHE->base
  380. currently holds the frame pointer for our calling frame. If
  381. we're at the start of a function, or somewhere half-way its
  382. prologue, the function's frame probably hasn't been fully
  383. setup yet. Try to reconstruct the base address for the stack
  384. frame by looking at the stack pointer. For truly "frameless"
  385. functions this might work too. */
  386. cache->base = get_frame_register_unsigned (this_frame, E_SP_REGNUM)
  387. + cache->sp_offset;
  388. cache->saved_sp = cache->base + BINWORD (gdbarch);
  389. cache->saved_regs[E_PC_REGNUM] = 0;
  390. }
  391. else
  392. {
  393. cache->saved_sp = cache->base + 2 * BINWORD (gdbarch);
  394. cache->saved_regs[E_PC_REGNUM] = -BINWORD (gdbarch);
  395. }
  396. /* Adjust all the saved registers such that they contain addresses
  397. instead of offsets. */
  398. for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
  399. if (cache->saved_regs[i] != -1)
  400. cache->saved_regs[i] = cache->base - cache->saved_regs[i];
  401. return cache;
  402. }
  403. static void
  404. h8300_frame_this_id (struct frame_info *this_frame, void **this_cache,
  405. struct frame_id *this_id)
  406. {
  407. struct h8300_frame_cache *cache =
  408. h8300_frame_cache (this_frame, this_cache);
  409. /* This marks the outermost frame. */
  410. if (cache->base == 0)
  411. return;
  412. *this_id = frame_id_build (cache->saved_sp, cache->pc);
  413. }
  414. static struct value *
  415. h8300_frame_prev_register (struct frame_info *this_frame, void **this_cache,
  416. int regnum)
  417. {
  418. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  419. struct h8300_frame_cache *cache =
  420. h8300_frame_cache (this_frame, this_cache);
  421. gdb_assert (regnum >= 0);
  422. if (regnum == E_SP_REGNUM && cache->saved_sp)
  423. return frame_unwind_got_constant (this_frame, regnum, cache->saved_sp);
  424. if (regnum < gdbarch_num_regs (gdbarch)
  425. && cache->saved_regs[regnum] != -1)
  426. return frame_unwind_got_memory (this_frame, regnum,
  427. cache->saved_regs[regnum]);
  428. return frame_unwind_got_register (this_frame, regnum, regnum);
  429. }
  430. static const struct frame_unwind h8300_frame_unwind = {
  431. "h8300 prologue",
  432. NORMAL_FRAME,
  433. default_frame_unwind_stop_reason,
  434. h8300_frame_this_id,
  435. h8300_frame_prev_register,
  436. NULL,
  437. default_frame_sniffer
  438. };
  439. static CORE_ADDR
  440. h8300_frame_base_address (struct frame_info *this_frame, void **this_cache)
  441. {
  442. struct h8300_frame_cache *cache = h8300_frame_cache (this_frame, this_cache);
  443. return cache->base;
  444. }
  445. static const struct frame_base h8300_frame_base = {
  446. &h8300_frame_unwind,
  447. h8300_frame_base_address,
  448. h8300_frame_base_address,
  449. h8300_frame_base_address
  450. };
  451. static CORE_ADDR
  452. h8300_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  453. {
  454. CORE_ADDR func_addr = 0 , func_end = 0;
  455. if (find_pc_partial_function (pc, NULL, &func_addr, &func_end))
  456. {
  457. struct symtab_and_line sal;
  458. struct h8300_frame_cache cache;
  459. /* Found a function. */
  460. sal = find_pc_line (func_addr, 0);
  461. if (sal.end && sal.end < func_end)
  462. /* Found a line number, use it as end of prologue. */
  463. return sal.end;
  464. /* No useable line symbol. Use prologue parsing method. */
  465. h8300_init_frame_cache (gdbarch, &cache);
  466. return h8300_analyze_prologue (gdbarch, func_addr, func_end, &cache);
  467. }
  468. /* No function symbol -- just return the PC. */
  469. return (CORE_ADDR) pc;
  470. }
  471. /* Function: push_dummy_call
  472. Setup the function arguments for calling a function in the inferior.
  473. In this discussion, a `word' is 16 bits on the H8/300s, and 32 bits
  474. on the H8/300H.
  475. There are actually two ABI's here: -mquickcall (the default) and
  476. -mno-quickcall. With -mno-quickcall, all arguments are passed on
  477. the stack after the return address, word-aligned. With
  478. -mquickcall, GCC tries to use r0 -- r2 to pass registers. Since
  479. GCC doesn't indicate in the object file which ABI was used to
  480. compile it, GDB only supports the default --- -mquickcall.
  481. Here are the rules for -mquickcall, in detail:
  482. Each argument, whether scalar or aggregate, is padded to occupy a
  483. whole number of words. Arguments smaller than a word are padded at
  484. the most significant end; those larger than a word are padded at
  485. the least significant end.
  486. The initial arguments are passed in r0 -- r2. Earlier arguments go in
  487. lower-numbered registers. Multi-word arguments are passed in
  488. consecutive registers, with the most significant end in the
  489. lower-numbered register.
  490. If an argument doesn't fit entirely in the remaining registers, it
  491. is passed entirely on the stack. Stack arguments begin just after
  492. the return address. Once an argument has overflowed onto the stack
  493. this way, all subsequent arguments are passed on the stack.
  494. The above rule has odd consequences. For example, on the h8/300s,
  495. if a function takes two longs and an int as arguments:
  496. - the first long will be passed in r0/r1,
  497. - the second long will be passed entirely on the stack, since it
  498. doesn't fit in r2,
  499. - and the int will be passed on the stack, even though it could fit
  500. in r2.
  501. A weird exception: if an argument is larger than a word, but not a
  502. whole number of words in length (before padding), it is passed on
  503. the stack following the rules for stack arguments above, even if
  504. there are sufficient registers available to hold it. Stranger
  505. still, the argument registers are still `used up' --- even though
  506. there's nothing in them.
  507. So, for example, on the h8/300s, if a function expects a three-byte
  508. structure and an int, the structure will go on the stack, and the
  509. int will go in r2, not r0.
  510. If the function returns an aggregate type (struct, union, or class)
  511. by value, the caller must allocate space to hold the return value,
  512. and pass the callee a pointer to this space as an invisible first
  513. argument, in R0.
  514. For varargs functions, the last fixed argument and all the variable
  515. arguments are always passed on the stack. This means that calls to
  516. varargs functions don't work properly unless there is a prototype
  517. in scope.
  518. Basically, this ABI is not good, for the following reasons:
  519. - You can't call vararg functions properly unless a prototype is in scope.
  520. - Structure passing is inconsistent, to no purpose I can see.
  521. - It often wastes argument registers, of which there are only three
  522. to begin with. */
  523. static CORE_ADDR
  524. h8300_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  525. struct regcache *regcache, CORE_ADDR bp_addr,
  526. int nargs, struct value **args, CORE_ADDR sp,
  527. function_call_return_method return_method,
  528. CORE_ADDR struct_addr)
  529. {
  530. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  531. int stack_alloc = 0, stack_offset = 0;
  532. int wordsize = BINWORD (gdbarch);
  533. int reg = E_ARG0_REGNUM;
  534. int argument;
  535. /* First, make sure the stack is properly aligned. */
  536. sp = align_down (sp, wordsize);
  537. /* Now make sure there's space on the stack for the arguments. We
  538. may over-allocate a little here, but that won't hurt anything. */
  539. for (argument = 0; argument < nargs; argument++)
  540. stack_alloc += align_up (TYPE_LENGTH (value_type (args[argument])),
  541. wordsize);
  542. sp -= stack_alloc;
  543. /* Now load as many arguments as possible into registers, and push
  544. the rest onto the stack.
  545. If we're returning a structure by value, then we must pass a
  546. pointer to the buffer for the return value as an invisible first
  547. argument. */
  548. if (return_method == return_method_struct)
  549. regcache_cooked_write_unsigned (regcache, reg++, struct_addr);
  550. for (argument = 0; argument < nargs; argument++)
  551. {
  552. struct type *type = value_type (args[argument]);
  553. int len = TYPE_LENGTH (type);
  554. char *contents = (char *) value_contents (args[argument]).data ();
  555. /* Pad the argument appropriately. */
  556. int padded_len = align_up (len, wordsize);
  557. /* Use std::vector here to get zero initialization. */
  558. std::vector<gdb_byte> padded (padded_len);
  559. memcpy ((len < wordsize ? padded.data () + padded_len - len
  560. : padded.data ()),
  561. contents, len);
  562. /* Could the argument fit in the remaining registers? */
  563. if (padded_len <= (E_ARGLAST_REGNUM - reg + 1) * wordsize)
  564. {
  565. /* Are we going to pass it on the stack anyway, for no good
  566. reason? */
  567. if (len > wordsize && len % wordsize)
  568. {
  569. /* I feel so unclean. */
  570. write_memory (sp + stack_offset, padded.data (), padded_len);
  571. stack_offset += padded_len;
  572. /* That's right --- even though we passed the argument
  573. on the stack, we consume the registers anyway! Love
  574. me, love my dog. */
  575. reg += padded_len / wordsize;
  576. }
  577. else
  578. {
  579. /* Heavens to Betsy --- it's really going in registers!
  580. Note that on the h8/300s, there are gaps between the
  581. registers in the register file. */
  582. int offset;
  583. for (offset = 0; offset < padded_len; offset += wordsize)
  584. {
  585. ULONGEST word
  586. = extract_unsigned_integer (&padded[offset],
  587. wordsize, byte_order);
  588. regcache_cooked_write_unsigned (regcache, reg++, word);
  589. }
  590. }
  591. }
  592. else
  593. {
  594. /* It doesn't fit in registers! Onto the stack it goes. */
  595. write_memory (sp + stack_offset, padded.data (), padded_len);
  596. stack_offset += padded_len;
  597. /* Once one argument has spilled onto the stack, all
  598. subsequent arguments go on the stack. */
  599. reg = E_ARGLAST_REGNUM + 1;
  600. }
  601. }
  602. /* Store return address. */
  603. sp -= wordsize;
  604. write_memory_unsigned_integer (sp, wordsize, byte_order, bp_addr);
  605. /* Update stack pointer. */
  606. regcache_cooked_write_unsigned (regcache, E_SP_REGNUM, sp);
  607. /* Return the new stack pointer minus the return address slot since
  608. that's what DWARF2/GCC uses as the frame's CFA. */
  609. return sp + wordsize;
  610. }
  611. /* Function: extract_return_value
  612. Figure out where in REGBUF the called function has left its return value.
  613. Copy that into VALBUF. Be sure to account for CPU type. */
  614. static void
  615. h8300_extract_return_value (struct type *type, struct regcache *regcache,
  616. gdb_byte *valbuf)
  617. {
  618. struct gdbarch *gdbarch = regcache->arch ();
  619. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  620. int len = TYPE_LENGTH (type);
  621. ULONGEST c, addr;
  622. switch (len)
  623. {
  624. case 1:
  625. case 2:
  626. regcache_cooked_read_unsigned (regcache, E_RET0_REGNUM, &c);
  627. store_unsigned_integer (valbuf, len, byte_order, c);
  628. break;
  629. case 4: /* Needs two registers on plain H8/300 */
  630. regcache_cooked_read_unsigned (regcache, E_RET0_REGNUM, &c);
  631. store_unsigned_integer (valbuf, 2, byte_order, c);
  632. regcache_cooked_read_unsigned (regcache, E_RET1_REGNUM, &c);
  633. store_unsigned_integer (valbuf + 2, 2, byte_order, c);
  634. break;
  635. case 8: /* long long is now 8 bytes. */
  636. if (type->code () == TYPE_CODE_INT)
  637. {
  638. regcache_cooked_read_unsigned (regcache, E_RET0_REGNUM, &addr);
  639. c = read_memory_unsigned_integer ((CORE_ADDR) addr, len, byte_order);
  640. store_unsigned_integer (valbuf, len, byte_order, c);
  641. }
  642. else
  643. {
  644. error (_("I don't know how this 8 byte value is returned."));
  645. }
  646. break;
  647. }
  648. }
  649. static void
  650. h8300h_extract_return_value (struct type *type, struct regcache *regcache,
  651. gdb_byte *valbuf)
  652. {
  653. struct gdbarch *gdbarch = regcache->arch ();
  654. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  655. ULONGEST c;
  656. switch (TYPE_LENGTH (type))
  657. {
  658. case 1:
  659. case 2:
  660. case 4:
  661. regcache_cooked_read_unsigned (regcache, E_RET0_REGNUM, &c);
  662. store_unsigned_integer (valbuf, TYPE_LENGTH (type), byte_order, c);
  663. break;
  664. case 8: /* long long is now 8 bytes. */
  665. if (type->code () == TYPE_CODE_INT)
  666. {
  667. regcache_cooked_read_unsigned (regcache, E_RET0_REGNUM, &c);
  668. store_unsigned_integer (valbuf, 4, byte_order, c);
  669. regcache_cooked_read_unsigned (regcache, E_RET1_REGNUM, &c);
  670. store_unsigned_integer (valbuf + 4, 4, byte_order, c);
  671. }
  672. else
  673. {
  674. error (_("I don't know how this 8 byte value is returned."));
  675. }
  676. break;
  677. }
  678. }
  679. static int
  680. h8300_use_struct_convention (struct type *value_type)
  681. {
  682. /* Types of 1, 2 or 4 bytes are returned in R0/R1, everything else on the
  683. stack. */
  684. if (value_type->code () == TYPE_CODE_STRUCT
  685. || value_type->code () == TYPE_CODE_UNION)
  686. return 1;
  687. return !(TYPE_LENGTH (value_type) == 1
  688. || TYPE_LENGTH (value_type) == 2
  689. || TYPE_LENGTH (value_type) == 4);
  690. }
  691. static int
  692. h8300h_use_struct_convention (struct type *value_type)
  693. {
  694. /* Types of 1, 2 or 4 bytes are returned in R0, INT types of 8 bytes are
  695. returned in R0/R1, everything else on the stack. */
  696. if (value_type->code () == TYPE_CODE_STRUCT
  697. || value_type->code () == TYPE_CODE_UNION)
  698. return 1;
  699. return !(TYPE_LENGTH (value_type) == 1
  700. || TYPE_LENGTH (value_type) == 2
  701. || TYPE_LENGTH (value_type) == 4
  702. || (TYPE_LENGTH (value_type) == 8
  703. && value_type->code () == TYPE_CODE_INT));
  704. }
  705. /* Function: store_return_value
  706. Place the appropriate value in the appropriate registers.
  707. Primarily used by the RETURN command. */
  708. static void
  709. h8300_store_return_value (struct type *type, struct regcache *regcache,
  710. const gdb_byte *valbuf)
  711. {
  712. struct gdbarch *gdbarch = regcache->arch ();
  713. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  714. ULONGEST val;
  715. switch (TYPE_LENGTH (type))
  716. {
  717. case 1:
  718. case 2: /* short... */
  719. val = extract_unsigned_integer (valbuf, TYPE_LENGTH (type), byte_order);
  720. regcache_cooked_write_unsigned (regcache, E_RET0_REGNUM, val);
  721. break;
  722. case 4: /* long, float */
  723. val = extract_unsigned_integer (valbuf, TYPE_LENGTH (type), byte_order);
  724. regcache_cooked_write_unsigned (regcache, E_RET0_REGNUM,
  725. (val >> 16) & 0xffff);
  726. regcache_cooked_write_unsigned (regcache, E_RET1_REGNUM, val & 0xffff);
  727. break;
  728. case 8: /* long long, double and long double
  729. are all defined as 4 byte types so
  730. far so this shouldn't happen. */
  731. error (_("I don't know how to return an 8 byte value."));
  732. break;
  733. }
  734. }
  735. static void
  736. h8300h_store_return_value (struct type *type, struct regcache *regcache,
  737. const gdb_byte *valbuf)
  738. {
  739. struct gdbarch *gdbarch = regcache->arch ();
  740. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  741. ULONGEST val;
  742. switch (TYPE_LENGTH (type))
  743. {
  744. case 1:
  745. case 2:
  746. case 4: /* long, float */
  747. val = extract_unsigned_integer (valbuf, TYPE_LENGTH (type), byte_order);
  748. regcache_cooked_write_unsigned (regcache, E_RET0_REGNUM, val);
  749. break;
  750. case 8:
  751. val = extract_unsigned_integer (valbuf, TYPE_LENGTH (type), byte_order);
  752. regcache_cooked_write_unsigned (regcache, E_RET0_REGNUM,
  753. (val >> 32) & 0xffffffff);
  754. regcache_cooked_write_unsigned (regcache, E_RET1_REGNUM,
  755. val & 0xffffffff);
  756. break;
  757. }
  758. }
  759. static enum return_value_convention
  760. h8300_return_value (struct gdbarch *gdbarch, struct value *function,
  761. struct type *type, struct regcache *regcache,
  762. gdb_byte *readbuf, const gdb_byte *writebuf)
  763. {
  764. if (h8300_use_struct_convention (type))
  765. return RETURN_VALUE_STRUCT_CONVENTION;
  766. if (writebuf)
  767. h8300_store_return_value (type, regcache, writebuf);
  768. else if (readbuf)
  769. h8300_extract_return_value (type, regcache, readbuf);
  770. return RETURN_VALUE_REGISTER_CONVENTION;
  771. }
  772. static enum return_value_convention
  773. h8300h_return_value (struct gdbarch *gdbarch, struct value *function,
  774. struct type *type, struct regcache *regcache,
  775. gdb_byte *readbuf, const gdb_byte *writebuf)
  776. {
  777. if (h8300h_use_struct_convention (type))
  778. {
  779. if (readbuf)
  780. {
  781. ULONGEST addr;
  782. regcache_raw_read_unsigned (regcache, E_R0_REGNUM, &addr);
  783. read_memory (addr, readbuf, TYPE_LENGTH (type));
  784. }
  785. return RETURN_VALUE_ABI_RETURNS_ADDRESS;
  786. }
  787. if (writebuf)
  788. h8300h_store_return_value (type, regcache, writebuf);
  789. else if (readbuf)
  790. h8300h_extract_return_value (type, regcache, readbuf);
  791. return RETURN_VALUE_REGISTER_CONVENTION;
  792. }
  793. /* Implementation of 'register_sim_regno' gdbarch method. */
  794. static int
  795. h8300_register_sim_regno (struct gdbarch *gdbarch, int regnum)
  796. {
  797. /* Only makes sense to supply raw registers. */
  798. gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (gdbarch));
  799. /* We hide the raw ccr from the user by making it nameless. Because
  800. the default register_sim_regno hook returns
  801. LEGACY_SIM_REGNO_IGNORE for unnamed registers, we need to
  802. override it. The sim register numbering is compatible with
  803. gdb's. */
  804. return regnum;
  805. }
  806. static const char *
  807. h8300_register_name_common (const char *regnames[], int numregs,
  808. struct gdbarch *gdbarch, int regno)
  809. {
  810. if (regno < 0
  811. || regno >= numregs)
  812. internal_error (__FILE__, __LINE__,
  813. _("h8300_register_name_common: illegal register number %d"),
  814. regno);
  815. else
  816. return regnames[regno];
  817. }
  818. static const char *
  819. h8300_register_name (struct gdbarch *gdbarch, int regno)
  820. {
  821. /* The register names change depending on which h8300 processor
  822. type is selected. */
  823. static const char *register_names[] = {
  824. "r0", "r1", "r2", "r3", "r4", "r5", "r6",
  825. "sp", "", "pc", "cycles", "tick", "inst",
  826. "ccr", /* pseudo register */
  827. };
  828. return h8300_register_name_common(register_names, ARRAY_SIZE(register_names),
  829. gdbarch, regno);
  830. }
  831. static const char *
  832. h8300h_register_name (struct gdbarch *gdbarch, int regno)
  833. {
  834. static const char *register_names[] = {
  835. "er0", "er1", "er2", "er3", "er4", "er5", "er6",
  836. "sp", "", "pc", "cycles", "tick", "inst",
  837. "ccr", /* pseudo register */
  838. };
  839. return h8300_register_name_common(register_names, ARRAY_SIZE(register_names),
  840. gdbarch, regno);
  841. }
  842. static const char *
  843. h8300s_register_name (struct gdbarch *gdbarch, int regno)
  844. {
  845. static const char *register_names[] = {
  846. "er0", "er1", "er2", "er3", "er4", "er5", "er6",
  847. "sp", "", "pc", "cycles", "", "tick", "inst",
  848. "mach", "macl",
  849. "ccr", "exr" /* pseudo registers */
  850. };
  851. return h8300_register_name_common(register_names, ARRAY_SIZE(register_names),
  852. gdbarch, regno);
  853. }
  854. static const char *
  855. h8300sx_register_name (struct gdbarch *gdbarch, int regno)
  856. {
  857. static const char *register_names[] = {
  858. "er0", "er1", "er2", "er3", "er4", "er5", "er6",
  859. "sp", "", "pc", "cycles", "", "tick", "inst",
  860. "mach", "macl", "sbr", "vbr",
  861. "ccr", "exr" /* pseudo registers */
  862. };
  863. return h8300_register_name_common(register_names, ARRAY_SIZE(register_names),
  864. gdbarch, regno);
  865. }
  866. static void
  867. h8300_print_register (struct gdbarch *gdbarch, struct ui_file *file,
  868. struct frame_info *frame, int regno)
  869. {
  870. LONGEST rval;
  871. const char *name = gdbarch_register_name (gdbarch, regno);
  872. if (!name || !*name)
  873. return;
  874. rval = get_frame_register_signed (frame, regno);
  875. gdb_printf (file, "%-14s ", name);
  876. if ((regno == E_PSEUDO_CCR_REGNUM (gdbarch)) || \
  877. (regno == E_PSEUDO_EXR_REGNUM (gdbarch) && is_h8300smode (gdbarch)))
  878. {
  879. gdb_printf (file, "0x%02x ", (unsigned char) rval);
  880. print_longest (file, 'u', 1, rval);
  881. }
  882. else
  883. {
  884. gdb_printf (file, "0x%s ", phex ((ULONGEST) rval,
  885. BINWORD (gdbarch)));
  886. print_longest (file, 'd', 1, rval);
  887. }
  888. if (regno == E_PSEUDO_CCR_REGNUM (gdbarch))
  889. {
  890. /* CCR register */
  891. int C, Z, N, V;
  892. unsigned char l = rval & 0xff;
  893. gdb_printf (file, "\t");
  894. gdb_printf (file, "I-%d ", (l & 0x80) != 0);
  895. gdb_printf (file, "UI-%d ", (l & 0x40) != 0);
  896. gdb_printf (file, "H-%d ", (l & 0x20) != 0);
  897. gdb_printf (file, "U-%d ", (l & 0x10) != 0);
  898. N = (l & 0x8) != 0;
  899. Z = (l & 0x4) != 0;
  900. V = (l & 0x2) != 0;
  901. C = (l & 0x1) != 0;
  902. gdb_printf (file, "N-%d ", N);
  903. gdb_printf (file, "Z-%d ", Z);
  904. gdb_printf (file, "V-%d ", V);
  905. gdb_printf (file, "C-%d ", C);
  906. if ((C | Z) == 0)
  907. gdb_printf (file, "u> ");
  908. if ((C | Z) == 1)
  909. gdb_printf (file, "u<= ");
  910. if (C == 0)
  911. gdb_printf (file, "u>= ");
  912. if (C == 1)
  913. gdb_printf (file, "u< ");
  914. if (Z == 0)
  915. gdb_printf (file, "!= ");
  916. if (Z == 1)
  917. gdb_printf (file, "== ");
  918. if ((N ^ V) == 0)
  919. gdb_printf (file, ">= ");
  920. if ((N ^ V) == 1)
  921. gdb_printf (file, "< ");
  922. if ((Z | (N ^ V)) == 0)
  923. gdb_printf (file, "> ");
  924. if ((Z | (N ^ V)) == 1)
  925. gdb_printf (file, "<= ");
  926. }
  927. else if (regno == E_PSEUDO_EXR_REGNUM (gdbarch) && is_h8300smode (gdbarch))
  928. {
  929. /* EXR register */
  930. unsigned char l = rval & 0xff;
  931. gdb_printf (file, "\t");
  932. gdb_printf (file, "T-%d - - - ", (l & 0x80) != 0);
  933. gdb_printf (file, "I2-%d ", (l & 4) != 0);
  934. gdb_printf (file, "I1-%d ", (l & 2) != 0);
  935. gdb_printf (file, "I0-%d", (l & 1) != 0);
  936. }
  937. gdb_printf (file, "\n");
  938. }
  939. static void
  940. h8300_print_registers_info (struct gdbarch *gdbarch, struct ui_file *file,
  941. struct frame_info *frame, int regno, int cpregs)
  942. {
  943. if (regno < 0)
  944. {
  945. for (regno = E_R0_REGNUM; regno <= E_SP_REGNUM; ++regno)
  946. h8300_print_register (gdbarch, file, frame, regno);
  947. h8300_print_register (gdbarch, file, frame,
  948. E_PSEUDO_CCR_REGNUM (gdbarch));
  949. h8300_print_register (gdbarch, file, frame, E_PC_REGNUM);
  950. if (is_h8300smode (gdbarch))
  951. {
  952. h8300_print_register (gdbarch, file, frame,
  953. E_PSEUDO_EXR_REGNUM (gdbarch));
  954. if (is_h8300sxmode (gdbarch))
  955. {
  956. h8300_print_register (gdbarch, file, frame, E_SBR_REGNUM);
  957. h8300_print_register (gdbarch, file, frame, E_VBR_REGNUM);
  958. }
  959. h8300_print_register (gdbarch, file, frame, E_MACH_REGNUM);
  960. h8300_print_register (gdbarch, file, frame, E_MACL_REGNUM);
  961. h8300_print_register (gdbarch, file, frame, E_CYCLES_REGNUM);
  962. h8300_print_register (gdbarch, file, frame, E_TICKS_REGNUM);
  963. h8300_print_register (gdbarch, file, frame, E_INSTS_REGNUM);
  964. }
  965. else
  966. {
  967. h8300_print_register (gdbarch, file, frame, E_CYCLES_REGNUM);
  968. h8300_print_register (gdbarch, file, frame, E_TICK_REGNUM);
  969. h8300_print_register (gdbarch, file, frame, E_INST_REGNUM);
  970. }
  971. }
  972. else
  973. {
  974. if (regno == E_CCR_REGNUM)
  975. h8300_print_register (gdbarch, file, frame,
  976. E_PSEUDO_CCR_REGNUM (gdbarch));
  977. else if (regno == E_PSEUDO_EXR_REGNUM (gdbarch)
  978. && is_h8300smode (gdbarch))
  979. h8300_print_register (gdbarch, file, frame,
  980. E_PSEUDO_EXR_REGNUM (gdbarch));
  981. else
  982. h8300_print_register (gdbarch, file, frame, regno);
  983. }
  984. }
  985. static struct type *
  986. h8300_register_type (struct gdbarch *gdbarch, int regno)
  987. {
  988. if (regno < 0 || regno >= gdbarch_num_cooked_regs (gdbarch))
  989. internal_error (__FILE__, __LINE__,
  990. _("h8300_register_type: illegal register number %d"),
  991. regno);
  992. else
  993. {
  994. switch (regno)
  995. {
  996. case E_PC_REGNUM:
  997. return builtin_type (gdbarch)->builtin_func_ptr;
  998. case E_SP_REGNUM:
  999. case E_FP_REGNUM:
  1000. return builtin_type (gdbarch)->builtin_data_ptr;
  1001. default:
  1002. if (regno == E_PSEUDO_CCR_REGNUM (gdbarch))
  1003. return builtin_type (gdbarch)->builtin_uint8;
  1004. else if (regno == E_PSEUDO_EXR_REGNUM (gdbarch))
  1005. return builtin_type (gdbarch)->builtin_uint8;
  1006. else if (is_h8300hmode (gdbarch))
  1007. return builtin_type (gdbarch)->builtin_int32;
  1008. else
  1009. return builtin_type (gdbarch)->builtin_int16;
  1010. }
  1011. }
  1012. }
  1013. /* Helpers for h8300_pseudo_register_read. We expose ccr/exr as
  1014. pseudo-registers to users with smaller sizes than the corresponding
  1015. raw registers. These helpers extend/narrow the values. */
  1016. static enum register_status
  1017. pseudo_from_raw_register (struct gdbarch *gdbarch, readable_regcache *regcache,
  1018. gdb_byte *buf, int pseudo_regno, int raw_regno)
  1019. {
  1020. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1021. enum register_status status;
  1022. ULONGEST val;
  1023. status = regcache->raw_read (raw_regno, &val);
  1024. if (status == REG_VALID)
  1025. store_unsigned_integer (buf,
  1026. register_size (gdbarch, pseudo_regno),
  1027. byte_order, val);
  1028. return status;
  1029. }
  1030. /* See pseudo_from_raw_register. */
  1031. static void
  1032. raw_from_pseudo_register (struct gdbarch *gdbarch, struct regcache *regcache,
  1033. const gdb_byte *buf, int raw_regno, int pseudo_regno)
  1034. {
  1035. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1036. ULONGEST val;
  1037. val = extract_unsigned_integer (buf, register_size (gdbarch, pseudo_regno),
  1038. byte_order);
  1039. regcache_raw_write_unsigned (regcache, raw_regno, val);
  1040. }
  1041. static enum register_status
  1042. h8300_pseudo_register_read (struct gdbarch *gdbarch,
  1043. readable_regcache *regcache, int regno,
  1044. gdb_byte *buf)
  1045. {
  1046. if (regno == E_PSEUDO_CCR_REGNUM (gdbarch))
  1047. {
  1048. return pseudo_from_raw_register (gdbarch, regcache, buf,
  1049. regno, E_CCR_REGNUM);
  1050. }
  1051. else if (regno == E_PSEUDO_EXR_REGNUM (gdbarch))
  1052. {
  1053. return pseudo_from_raw_register (gdbarch, regcache, buf,
  1054. regno, E_EXR_REGNUM);
  1055. }
  1056. else
  1057. return regcache->raw_read (regno, buf);
  1058. }
  1059. static void
  1060. h8300_pseudo_register_write (struct gdbarch *gdbarch,
  1061. struct regcache *regcache, int regno,
  1062. const gdb_byte *buf)
  1063. {
  1064. if (regno == E_PSEUDO_CCR_REGNUM (gdbarch))
  1065. raw_from_pseudo_register (gdbarch, regcache, buf, E_CCR_REGNUM, regno);
  1066. else if (regno == E_PSEUDO_EXR_REGNUM (gdbarch))
  1067. raw_from_pseudo_register (gdbarch, regcache, buf, E_EXR_REGNUM, regno);
  1068. else
  1069. regcache->raw_write (regno, buf);
  1070. }
  1071. static int
  1072. h8300_dbg_reg_to_regnum (struct gdbarch *gdbarch, int regno)
  1073. {
  1074. if (regno == E_CCR_REGNUM)
  1075. return E_PSEUDO_CCR_REGNUM (gdbarch);
  1076. return regno;
  1077. }
  1078. static int
  1079. h8300s_dbg_reg_to_regnum (struct gdbarch *gdbarch, int regno)
  1080. {
  1081. if (regno == E_CCR_REGNUM)
  1082. return E_PSEUDO_CCR_REGNUM (gdbarch);
  1083. if (regno == E_EXR_REGNUM)
  1084. return E_PSEUDO_EXR_REGNUM (gdbarch);
  1085. return regno;
  1086. }
  1087. /*static unsigned char breakpoint[] = { 0x7A, 0xFF }; *//* ??? */
  1088. constexpr gdb_byte h8300_break_insn[] = { 0x01, 0x80 }; /* Sleep */
  1089. typedef BP_MANIPULATION (h8300_break_insn) h8300_breakpoint;
  1090. static struct gdbarch *
  1091. h8300_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  1092. {
  1093. struct gdbarch *gdbarch;
  1094. arches = gdbarch_list_lookup_by_info (arches, &info);
  1095. if (arches != NULL)
  1096. return arches->gdbarch;
  1097. if (info.bfd_arch_info->arch != bfd_arch_h8300)
  1098. return NULL;
  1099. gdbarch = gdbarch_alloc (&info, 0);
  1100. set_gdbarch_register_sim_regno (gdbarch, h8300_register_sim_regno);
  1101. switch (info.bfd_arch_info->mach)
  1102. {
  1103. case bfd_mach_h8300:
  1104. set_gdbarch_num_regs (gdbarch, 13);
  1105. set_gdbarch_num_pseudo_regs (gdbarch, 1);
  1106. set_gdbarch_dwarf2_reg_to_regnum (gdbarch, h8300_dbg_reg_to_regnum);
  1107. set_gdbarch_stab_reg_to_regnum (gdbarch, h8300_dbg_reg_to_regnum);
  1108. set_gdbarch_register_name (gdbarch, h8300_register_name);
  1109. set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1110. set_gdbarch_addr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1111. set_gdbarch_return_value (gdbarch, h8300_return_value);
  1112. break;
  1113. case bfd_mach_h8300h:
  1114. case bfd_mach_h8300hn:
  1115. set_gdbarch_num_regs (gdbarch, 13);
  1116. set_gdbarch_num_pseudo_regs (gdbarch, 1);
  1117. set_gdbarch_dwarf2_reg_to_regnum (gdbarch, h8300_dbg_reg_to_regnum);
  1118. set_gdbarch_stab_reg_to_regnum (gdbarch, h8300_dbg_reg_to_regnum);
  1119. set_gdbarch_register_name (gdbarch, h8300h_register_name);
  1120. if (info.bfd_arch_info->mach != bfd_mach_h8300hn)
  1121. {
  1122. set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1123. set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1124. }
  1125. else
  1126. {
  1127. set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1128. set_gdbarch_addr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1129. }
  1130. set_gdbarch_return_value (gdbarch, h8300h_return_value);
  1131. break;
  1132. case bfd_mach_h8300s:
  1133. case bfd_mach_h8300sn:
  1134. set_gdbarch_num_regs (gdbarch, 16);
  1135. set_gdbarch_num_pseudo_regs (gdbarch, 2);
  1136. set_gdbarch_dwarf2_reg_to_regnum (gdbarch, h8300s_dbg_reg_to_regnum);
  1137. set_gdbarch_stab_reg_to_regnum (gdbarch, h8300s_dbg_reg_to_regnum);
  1138. set_gdbarch_register_name (gdbarch, h8300s_register_name);
  1139. if (info.bfd_arch_info->mach != bfd_mach_h8300sn)
  1140. {
  1141. set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1142. set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1143. }
  1144. else
  1145. {
  1146. set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1147. set_gdbarch_addr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1148. }
  1149. set_gdbarch_return_value (gdbarch, h8300h_return_value);
  1150. break;
  1151. case bfd_mach_h8300sx:
  1152. case bfd_mach_h8300sxn:
  1153. set_gdbarch_num_regs (gdbarch, 18);
  1154. set_gdbarch_num_pseudo_regs (gdbarch, 2);
  1155. set_gdbarch_dwarf2_reg_to_regnum (gdbarch, h8300s_dbg_reg_to_regnum);
  1156. set_gdbarch_stab_reg_to_regnum (gdbarch, h8300s_dbg_reg_to_regnum);
  1157. set_gdbarch_register_name (gdbarch, h8300sx_register_name);
  1158. if (info.bfd_arch_info->mach != bfd_mach_h8300sxn)
  1159. {
  1160. set_gdbarch_ptr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1161. set_gdbarch_addr_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1162. }
  1163. else
  1164. {
  1165. set_gdbarch_ptr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1166. set_gdbarch_addr_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1167. }
  1168. set_gdbarch_return_value (gdbarch, h8300h_return_value);
  1169. break;
  1170. }
  1171. set_gdbarch_pseudo_register_read (gdbarch, h8300_pseudo_register_read);
  1172. set_gdbarch_pseudo_register_write (gdbarch, h8300_pseudo_register_write);
  1173. /*
  1174. * Basic register fields and methods.
  1175. */
  1176. set_gdbarch_sp_regnum (gdbarch, E_SP_REGNUM);
  1177. set_gdbarch_pc_regnum (gdbarch, E_PC_REGNUM);
  1178. set_gdbarch_register_type (gdbarch, h8300_register_type);
  1179. set_gdbarch_print_registers_info (gdbarch, h8300_print_registers_info);
  1180. /*
  1181. * Frame Info
  1182. */
  1183. set_gdbarch_skip_prologue (gdbarch, h8300_skip_prologue);
  1184. /* Frame unwinder. */
  1185. frame_base_set_default (gdbarch, &h8300_frame_base);
  1186. /*
  1187. * Miscellany
  1188. */
  1189. /* Stack grows up. */
  1190. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  1191. set_gdbarch_breakpoint_kind_from_pc (gdbarch,
  1192. h8300_breakpoint::kind_from_pc);
  1193. set_gdbarch_sw_breakpoint_from_kind (gdbarch,
  1194. h8300_breakpoint::bp_from_kind);
  1195. set_gdbarch_push_dummy_call (gdbarch, h8300_push_dummy_call);
  1196. set_gdbarch_char_signed (gdbarch, 0);
  1197. set_gdbarch_int_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1198. set_gdbarch_long_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1199. set_gdbarch_long_long_bit (gdbarch, 8 * TARGET_CHAR_BIT);
  1200. set_gdbarch_wchar_bit (gdbarch, 2 * TARGET_CHAR_BIT);
  1201. set_gdbarch_wchar_signed (gdbarch, 0);
  1202. set_gdbarch_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1203. set_gdbarch_double_format (gdbarch, floatformats_ieee_single);
  1204. set_gdbarch_long_double_bit (gdbarch, 4 * TARGET_CHAR_BIT);
  1205. set_gdbarch_long_double_format (gdbarch, floatformats_ieee_single);
  1206. set_gdbarch_believe_pcc_promotion (gdbarch, 1);
  1207. /* Hook in the DWARF CFI frame unwinder. */
  1208. dwarf2_append_unwinders (gdbarch);
  1209. frame_unwind_append_unwinder (gdbarch, &h8300_frame_unwind);
  1210. return gdbarch;
  1211. }
  1212. void _initialize_h8300_tdep ();
  1213. void
  1214. _initialize_h8300_tdep ()
  1215. {
  1216. register_gdbarch_init (bfd_arch_h8300, h8300_gdbarch_init);
  1217. }
  1218. static int
  1219. is_h8300hmode (struct gdbarch *gdbarch)
  1220. {
  1221. return gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sx
  1222. || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sxn
  1223. || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300s
  1224. || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sn
  1225. || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300h
  1226. || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300hn;
  1227. }
  1228. static int
  1229. is_h8300smode (struct gdbarch *gdbarch)
  1230. {
  1231. return gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sx
  1232. || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sxn
  1233. || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300s
  1234. || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sn;
  1235. }
  1236. static int
  1237. is_h8300sxmode (struct gdbarch *gdbarch)
  1238. {
  1239. return gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sx
  1240. || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sxn;
  1241. }
  1242. static int
  1243. is_h8300_normal_mode (struct gdbarch *gdbarch)
  1244. {
  1245. return gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sxn
  1246. || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300sn
  1247. || gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_h8300hn;
  1248. }