rl78-tdep.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  1. /* Target-dependent code for the Renesas RL78 for GDB, the GNU debugger.
  2. Copyright (C) 2011-2022 Free Software Foundation, Inc.
  3. Contributed by Red Hat, Inc.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "arch-utils.h"
  17. #include "prologue-value.h"
  18. #include "target.h"
  19. #include "regcache.h"
  20. #include "opcode/rl78.h"
  21. #include "dis-asm.h"
  22. #include "gdbtypes.h"
  23. #include "frame.h"
  24. #include "frame-unwind.h"
  25. #include "frame-base.h"
  26. #include "value.h"
  27. #include "gdbcore.h"
  28. #include "dwarf2/frame.h"
  29. #include "reggroups.h"
  30. #include "gdbarch.h"
  31. #include "elf/rl78.h"
  32. #include "elf-bfd.h"
  33. /* Register Banks. */
  34. enum
  35. {
  36. RL78_BANK0 = 0,
  37. RL78_BANK1 = 1,
  38. RL78_BANK2 = 2,
  39. RL78_BANK3 = 3,
  40. RL78_NUMBANKS = 4,
  41. RL78_REGS_PER_BANK = 8
  42. };
  43. /* Register Numbers. */
  44. enum
  45. {
  46. /* All general purpose registers are 8 bits wide. */
  47. RL78_RAW_BANK0_R0_REGNUM = 0,
  48. RL78_RAW_BANK0_R1_REGNUM,
  49. RL78_RAW_BANK0_R2_REGNUM,
  50. RL78_RAW_BANK0_R3_REGNUM,
  51. RL78_RAW_BANK0_R4_REGNUM,
  52. RL78_RAW_BANK0_R5_REGNUM,
  53. RL78_RAW_BANK0_R6_REGNUM,
  54. RL78_RAW_BANK0_R7_REGNUM,
  55. RL78_RAW_BANK1_R0_REGNUM,
  56. RL78_RAW_BANK1_R1_REGNUM,
  57. RL78_RAW_BANK1_R2_REGNUM,
  58. RL78_RAW_BANK1_R3_REGNUM,
  59. RL78_RAW_BANK1_R4_REGNUM,
  60. RL78_RAW_BANK1_R5_REGNUM,
  61. RL78_RAW_BANK1_R6_REGNUM,
  62. RL78_RAW_BANK1_R7_REGNUM,
  63. RL78_RAW_BANK2_R0_REGNUM,
  64. RL78_RAW_BANK2_R1_REGNUM,
  65. RL78_RAW_BANK2_R2_REGNUM,
  66. RL78_RAW_BANK2_R3_REGNUM,
  67. RL78_RAW_BANK2_R4_REGNUM,
  68. RL78_RAW_BANK2_R5_REGNUM,
  69. RL78_RAW_BANK2_R6_REGNUM,
  70. RL78_RAW_BANK2_R7_REGNUM,
  71. RL78_RAW_BANK3_R0_REGNUM,
  72. RL78_RAW_BANK3_R1_REGNUM,
  73. RL78_RAW_BANK3_R2_REGNUM,
  74. RL78_RAW_BANK3_R3_REGNUM,
  75. RL78_RAW_BANK3_R4_REGNUM,
  76. RL78_RAW_BANK3_R5_REGNUM,
  77. RL78_RAW_BANK3_R6_REGNUM,
  78. RL78_RAW_BANK3_R7_REGNUM,
  79. RL78_PSW_REGNUM, /* 8 bits */
  80. RL78_ES_REGNUM, /* 8 bits */
  81. RL78_CS_REGNUM, /* 8 bits */
  82. RL78_RAW_PC_REGNUM, /* 20 bits; we'll use 32 bits for it. */
  83. /* Fixed address SFRs (some of those above are SFRs too.) */
  84. RL78_SPL_REGNUM, /* 8 bits; lower half of SP */
  85. RL78_SPH_REGNUM, /* 8 bits; upper half of SP */
  86. RL78_PMC_REGNUM, /* 8 bits */
  87. RL78_MEM_REGNUM, /* 8 bits ?? */
  88. RL78_NUM_REGS,
  89. /* Pseudo registers. */
  90. RL78_PC_REGNUM = RL78_NUM_REGS,
  91. RL78_SP_REGNUM,
  92. RL78_X_REGNUM,
  93. RL78_A_REGNUM,
  94. RL78_C_REGNUM,
  95. RL78_B_REGNUM,
  96. RL78_E_REGNUM,
  97. RL78_D_REGNUM,
  98. RL78_L_REGNUM,
  99. RL78_H_REGNUM,
  100. RL78_AX_REGNUM,
  101. RL78_BC_REGNUM,
  102. RL78_DE_REGNUM,
  103. RL78_HL_REGNUM,
  104. RL78_BANK0_R0_REGNUM,
  105. RL78_BANK0_R1_REGNUM,
  106. RL78_BANK0_R2_REGNUM,
  107. RL78_BANK0_R3_REGNUM,
  108. RL78_BANK0_R4_REGNUM,
  109. RL78_BANK0_R5_REGNUM,
  110. RL78_BANK0_R6_REGNUM,
  111. RL78_BANK0_R7_REGNUM,
  112. RL78_BANK1_R0_REGNUM,
  113. RL78_BANK1_R1_REGNUM,
  114. RL78_BANK1_R2_REGNUM,
  115. RL78_BANK1_R3_REGNUM,
  116. RL78_BANK1_R4_REGNUM,
  117. RL78_BANK1_R5_REGNUM,
  118. RL78_BANK1_R6_REGNUM,
  119. RL78_BANK1_R7_REGNUM,
  120. RL78_BANK2_R0_REGNUM,
  121. RL78_BANK2_R1_REGNUM,
  122. RL78_BANK2_R2_REGNUM,
  123. RL78_BANK2_R3_REGNUM,
  124. RL78_BANK2_R4_REGNUM,
  125. RL78_BANK2_R5_REGNUM,
  126. RL78_BANK2_R6_REGNUM,
  127. RL78_BANK2_R7_REGNUM,
  128. RL78_BANK3_R0_REGNUM,
  129. RL78_BANK3_R1_REGNUM,
  130. RL78_BANK3_R2_REGNUM,
  131. RL78_BANK3_R3_REGNUM,
  132. RL78_BANK3_R4_REGNUM,
  133. RL78_BANK3_R5_REGNUM,
  134. RL78_BANK3_R6_REGNUM,
  135. RL78_BANK3_R7_REGNUM,
  136. RL78_BANK0_RP0_REGNUM,
  137. RL78_BANK0_RP1_REGNUM,
  138. RL78_BANK0_RP2_REGNUM,
  139. RL78_BANK0_RP3_REGNUM,
  140. RL78_BANK1_RP0_REGNUM,
  141. RL78_BANK1_RP1_REGNUM,
  142. RL78_BANK1_RP2_REGNUM,
  143. RL78_BANK1_RP3_REGNUM,
  144. RL78_BANK2_RP0_REGNUM,
  145. RL78_BANK2_RP1_REGNUM,
  146. RL78_BANK2_RP2_REGNUM,
  147. RL78_BANK2_RP3_REGNUM,
  148. RL78_BANK3_RP0_REGNUM,
  149. RL78_BANK3_RP1_REGNUM,
  150. RL78_BANK3_RP2_REGNUM,
  151. RL78_BANK3_RP3_REGNUM,
  152. /* These are the same as the above 16 registers, but have
  153. a pointer type for use as base registers in expression
  154. evaluation. These are not user visible registers. */
  155. RL78_BANK0_RP0_PTR_REGNUM,
  156. RL78_BANK0_RP1_PTR_REGNUM,
  157. RL78_BANK0_RP2_PTR_REGNUM,
  158. RL78_BANK0_RP3_PTR_REGNUM,
  159. RL78_BANK1_RP0_PTR_REGNUM,
  160. RL78_BANK1_RP1_PTR_REGNUM,
  161. RL78_BANK1_RP2_PTR_REGNUM,
  162. RL78_BANK1_RP3_PTR_REGNUM,
  163. RL78_BANK2_RP0_PTR_REGNUM,
  164. RL78_BANK2_RP1_PTR_REGNUM,
  165. RL78_BANK2_RP2_PTR_REGNUM,
  166. RL78_BANK2_RP3_PTR_REGNUM,
  167. RL78_BANK3_RP0_PTR_REGNUM,
  168. RL78_BANK3_RP1_PTR_REGNUM,
  169. RL78_BANK3_RP2_PTR_REGNUM,
  170. RL78_BANK3_RP3_PTR_REGNUM,
  171. RL78_NUM_TOTAL_REGS,
  172. RL78_NUM_PSEUDO_REGS = RL78_NUM_TOTAL_REGS - RL78_NUM_REGS
  173. };
  174. #define RL78_SP_ADDR 0xffff8
  175. /* Architecture specific data. */
  176. struct rl78_gdbarch_tdep : gdbarch_tdep
  177. {
  178. /* The ELF header flags specify the multilib used. */
  179. int elf_flags = 0;
  180. struct type *rl78_void = nullptr,
  181. *rl78_uint8 = nullptr,
  182. *rl78_int8 = nullptr,
  183. *rl78_uint16 = nullptr,
  184. *rl78_int16 = nullptr,
  185. *rl78_uint32 = nullptr,
  186. *rl78_int32 = nullptr,
  187. *rl78_data_pointer = nullptr,
  188. *rl78_code_pointer = nullptr,
  189. *rl78_psw_type = nullptr;
  190. };
  191. /* This structure holds the results of a prologue analysis. */
  192. struct rl78_prologue
  193. {
  194. /* The offset from the frame base to the stack pointer --- always
  195. zero or negative.
  196. Calling this a "size" is a bit misleading, but given that the
  197. stack grows downwards, using offsets for everything keeps one
  198. from going completely sign-crazy: you never change anything's
  199. sign for an ADD instruction; always change the second operand's
  200. sign for a SUB instruction; and everything takes care of
  201. itself. */
  202. int frame_size;
  203. /* Non-zero if this function has initialized the frame pointer from
  204. the stack pointer, zero otherwise. */
  205. int has_frame_ptr;
  206. /* If has_frame_ptr is non-zero, this is the offset from the frame
  207. base to where the frame pointer points. This is always zero or
  208. negative. */
  209. int frame_ptr_offset;
  210. /* The address of the first instruction at which the frame has been
  211. set up and the arguments are where the debug info says they are
  212. --- as best as we can tell. */
  213. CORE_ADDR prologue_end;
  214. /* reg_offset[R] is the offset from the CFA at which register R is
  215. saved, or 1 if register R has not been saved. (Real values are
  216. always zero or negative.) */
  217. int reg_offset[RL78_NUM_TOTAL_REGS];
  218. };
  219. /* Construct type for PSW register. */
  220. static struct type *
  221. rl78_psw_type (struct gdbarch *gdbarch)
  222. {
  223. rl78_gdbarch_tdep *tdep = (rl78_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  224. if (tdep->rl78_psw_type == NULL)
  225. {
  226. tdep->rl78_psw_type = arch_flags_type (gdbarch,
  227. "builtin_type_rl78_psw", 8);
  228. append_flags_type_flag (tdep->rl78_psw_type, 0, "CY");
  229. append_flags_type_flag (tdep->rl78_psw_type, 1, "ISP0");
  230. append_flags_type_flag (tdep->rl78_psw_type, 2, "ISP1");
  231. append_flags_type_flag (tdep->rl78_psw_type, 3, "RBS0");
  232. append_flags_type_flag (tdep->rl78_psw_type, 4, "AC");
  233. append_flags_type_flag (tdep->rl78_psw_type, 5, "RBS1");
  234. append_flags_type_flag (tdep->rl78_psw_type, 6, "Z");
  235. append_flags_type_flag (tdep->rl78_psw_type, 7, "IE");
  236. }
  237. return tdep->rl78_psw_type;
  238. }
  239. /* Implement the "register_type" gdbarch method. */
  240. static struct type *
  241. rl78_register_type (struct gdbarch *gdbarch, int reg_nr)
  242. {
  243. rl78_gdbarch_tdep *tdep = (rl78_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  244. if (reg_nr == RL78_PC_REGNUM)
  245. return tdep->rl78_code_pointer;
  246. else if (reg_nr == RL78_RAW_PC_REGNUM)
  247. return tdep->rl78_uint32;
  248. else if (reg_nr == RL78_PSW_REGNUM)
  249. return rl78_psw_type (gdbarch);
  250. else if (reg_nr <= RL78_MEM_REGNUM
  251. || (RL78_X_REGNUM <= reg_nr && reg_nr <= RL78_H_REGNUM)
  252. || (RL78_BANK0_R0_REGNUM <= reg_nr
  253. && reg_nr <= RL78_BANK3_R7_REGNUM))
  254. return tdep->rl78_int8;
  255. else if (reg_nr == RL78_SP_REGNUM
  256. || (RL78_BANK0_RP0_PTR_REGNUM <= reg_nr
  257. && reg_nr <= RL78_BANK3_RP3_PTR_REGNUM))
  258. return tdep->rl78_data_pointer;
  259. else
  260. return tdep->rl78_int16;
  261. }
  262. /* Implement the "register_name" gdbarch method. */
  263. static const char *
  264. rl78_register_name (struct gdbarch *gdbarch, int regnr)
  265. {
  266. static const char *const reg_names[] =
  267. {
  268. "", /* bank0_r0 */
  269. "", /* bank0_r1 */
  270. "", /* bank0_r2 */
  271. "", /* bank0_r3 */
  272. "", /* bank0_r4 */
  273. "", /* bank0_r5 */
  274. "", /* bank0_r6 */
  275. "", /* bank0_r7 */
  276. "", /* bank1_r0 */
  277. "", /* bank1_r1 */
  278. "", /* bank1_r2 */
  279. "", /* bank1_r3 */
  280. "", /* bank1_r4 */
  281. "", /* bank1_r5 */
  282. "", /* bank1_r6 */
  283. "", /* bank1_r7 */
  284. "", /* bank2_r0 */
  285. "", /* bank2_r1 */
  286. "", /* bank2_r2 */
  287. "", /* bank2_r3 */
  288. "", /* bank2_r4 */
  289. "", /* bank2_r5 */
  290. "", /* bank2_r6 */
  291. "", /* bank2_r7 */
  292. "", /* bank3_r0 */
  293. "", /* bank3_r1 */
  294. "", /* bank3_r2 */
  295. "", /* bank3_r3 */
  296. "", /* bank3_r4 */
  297. "", /* bank3_r5 */
  298. "", /* bank3_r6 */
  299. "", /* bank3_r7 */
  300. "psw",
  301. "es",
  302. "cs",
  303. "",
  304. "", /* spl */
  305. "", /* sph */
  306. "pmc",
  307. "mem",
  308. "pc",
  309. "sp",
  310. "x",
  311. "a",
  312. "c",
  313. "b",
  314. "e",
  315. "d",
  316. "l",
  317. "h",
  318. "ax",
  319. "bc",
  320. "de",
  321. "hl",
  322. "bank0_r0",
  323. "bank0_r1",
  324. "bank0_r2",
  325. "bank0_r3",
  326. "bank0_r4",
  327. "bank0_r5",
  328. "bank0_r6",
  329. "bank0_r7",
  330. "bank1_r0",
  331. "bank1_r1",
  332. "bank1_r2",
  333. "bank1_r3",
  334. "bank1_r4",
  335. "bank1_r5",
  336. "bank1_r6",
  337. "bank1_r7",
  338. "bank2_r0",
  339. "bank2_r1",
  340. "bank2_r2",
  341. "bank2_r3",
  342. "bank2_r4",
  343. "bank2_r5",
  344. "bank2_r6",
  345. "bank2_r7",
  346. "bank3_r0",
  347. "bank3_r1",
  348. "bank3_r2",
  349. "bank3_r3",
  350. "bank3_r4",
  351. "bank3_r5",
  352. "bank3_r6",
  353. "bank3_r7",
  354. "bank0_rp0",
  355. "bank0_rp1",
  356. "bank0_rp2",
  357. "bank0_rp3",
  358. "bank1_rp0",
  359. "bank1_rp1",
  360. "bank1_rp2",
  361. "bank1_rp3",
  362. "bank2_rp0",
  363. "bank2_rp1",
  364. "bank2_rp2",
  365. "bank2_rp3",
  366. "bank3_rp0",
  367. "bank3_rp1",
  368. "bank3_rp2",
  369. "bank3_rp3",
  370. /* The 16 register slots would be named
  371. bank0_rp0_ptr_regnum ... bank3_rp3_ptr_regnum, but we don't
  372. want these to be user visible registers. */
  373. "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""
  374. };
  375. return reg_names[regnr];
  376. }
  377. /* Implement the "register_name" gdbarch method for the g10 variant. */
  378. static const char *
  379. rl78_g10_register_name (struct gdbarch *gdbarch, int regnr)
  380. {
  381. static const char *const reg_names[] =
  382. {
  383. "", /* bank0_r0 */
  384. "", /* bank0_r1 */
  385. "", /* bank0_r2 */
  386. "", /* bank0_r3 */
  387. "", /* bank0_r4 */
  388. "", /* bank0_r5 */
  389. "", /* bank0_r6 */
  390. "", /* bank0_r7 */
  391. "", /* bank1_r0 */
  392. "", /* bank1_r1 */
  393. "", /* bank1_r2 */
  394. "", /* bank1_r3 */
  395. "", /* bank1_r4 */
  396. "", /* bank1_r5 */
  397. "", /* bank1_r6 */
  398. "", /* bank1_r7 */
  399. "", /* bank2_r0 */
  400. "", /* bank2_r1 */
  401. "", /* bank2_r2 */
  402. "", /* bank2_r3 */
  403. "", /* bank2_r4 */
  404. "", /* bank2_r5 */
  405. "", /* bank2_r6 */
  406. "", /* bank2_r7 */
  407. "", /* bank3_r0 */
  408. "", /* bank3_r1 */
  409. "", /* bank3_r2 */
  410. "", /* bank3_r3 */
  411. "", /* bank3_r4 */
  412. "", /* bank3_r5 */
  413. "", /* bank3_r6 */
  414. "", /* bank3_r7 */
  415. "psw",
  416. "es",
  417. "cs",
  418. "",
  419. "", /* spl */
  420. "", /* sph */
  421. "pmc",
  422. "mem",
  423. "pc",
  424. "sp",
  425. "x",
  426. "a",
  427. "c",
  428. "b",
  429. "e",
  430. "d",
  431. "l",
  432. "h",
  433. "ax",
  434. "bc",
  435. "de",
  436. "hl",
  437. "bank0_r0",
  438. "bank0_r1",
  439. "bank0_r2",
  440. "bank0_r3",
  441. "bank0_r4",
  442. "bank0_r5",
  443. "bank0_r6",
  444. "bank0_r7",
  445. "",
  446. "",
  447. "",
  448. "",
  449. "",
  450. "",
  451. "",
  452. "",
  453. "",
  454. "",
  455. "",
  456. "",
  457. "",
  458. "",
  459. "",
  460. "",
  461. "",
  462. "",
  463. "",
  464. "",
  465. "",
  466. "",
  467. "",
  468. "",
  469. "bank0_rp0",
  470. "bank0_rp1",
  471. "bank0_rp2",
  472. "bank0_rp3",
  473. "",
  474. "",
  475. "",
  476. "",
  477. "",
  478. "",
  479. "",
  480. "",
  481. "",
  482. "",
  483. "",
  484. "",
  485. /* The 16 register slots would be named
  486. bank0_rp0_ptr_regnum ... bank3_rp3_ptr_regnum, but we don't
  487. want these to be user visible registers. */
  488. "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""
  489. };
  490. return reg_names[regnr];
  491. }
  492. /* Implement the "register_reggroup_p" gdbarch method. */
  493. static int
  494. rl78_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
  495. const struct reggroup *group)
  496. {
  497. if (group == all_reggroup)
  498. return 1;
  499. /* All other registers are saved and restored. */
  500. if (group == save_reggroup || group == restore_reggroup)
  501. {
  502. if ((regnum < RL78_NUM_REGS
  503. && regnum != RL78_SPL_REGNUM
  504. && regnum != RL78_SPH_REGNUM
  505. && regnum != RL78_RAW_PC_REGNUM)
  506. || regnum == RL78_SP_REGNUM
  507. || regnum == RL78_PC_REGNUM)
  508. return 1;
  509. else
  510. return 0;
  511. }
  512. if ((RL78_BANK0_R0_REGNUM <= regnum && regnum <= RL78_BANK3_R7_REGNUM)
  513. || regnum == RL78_ES_REGNUM
  514. || regnum == RL78_CS_REGNUM
  515. || regnum == RL78_SPL_REGNUM
  516. || regnum == RL78_SPH_REGNUM
  517. || regnum == RL78_PMC_REGNUM
  518. || regnum == RL78_MEM_REGNUM
  519. || regnum == RL78_RAW_PC_REGNUM
  520. || (RL78_BANK0_RP0_REGNUM <= regnum && regnum <= RL78_BANK3_RP3_REGNUM))
  521. return group == system_reggroup;
  522. return group == general_reggroup;
  523. }
  524. /* Strip bits to form an instruction address. (When fetching a
  525. 32-bit address from the stack, the high eight bits are garbage.
  526. This function strips off those unused bits.) */
  527. static CORE_ADDR
  528. rl78_make_instruction_address (CORE_ADDR addr)
  529. {
  530. return addr & 0xffffff;
  531. }
  532. /* Set / clear bits necessary to make a data address. */
  533. static CORE_ADDR
  534. rl78_make_data_address (CORE_ADDR addr)
  535. {
  536. return (addr & 0xffff) | 0xf0000;
  537. }
  538. /* Implement the "pseudo_register_read" gdbarch method. */
  539. static enum register_status
  540. rl78_pseudo_register_read (struct gdbarch *gdbarch,
  541. readable_regcache *regcache,
  542. int reg, gdb_byte *buffer)
  543. {
  544. enum register_status status;
  545. if (RL78_BANK0_R0_REGNUM <= reg && reg <= RL78_BANK3_R7_REGNUM)
  546. {
  547. int raw_regnum = RL78_RAW_BANK0_R0_REGNUM
  548. + (reg - RL78_BANK0_R0_REGNUM);
  549. status = regcache->raw_read (raw_regnum, buffer);
  550. }
  551. else if (RL78_BANK0_RP0_REGNUM <= reg && reg <= RL78_BANK3_RP3_REGNUM)
  552. {
  553. int raw_regnum = 2 * (reg - RL78_BANK0_RP0_REGNUM)
  554. + RL78_RAW_BANK0_R0_REGNUM;
  555. status = regcache->raw_read (raw_regnum, buffer);
  556. if (status == REG_VALID)
  557. status = regcache->raw_read (raw_regnum + 1, buffer + 1);
  558. }
  559. else if (RL78_BANK0_RP0_PTR_REGNUM <= reg && reg <= RL78_BANK3_RP3_PTR_REGNUM)
  560. {
  561. int raw_regnum = 2 * (reg - RL78_BANK0_RP0_PTR_REGNUM)
  562. + RL78_RAW_BANK0_R0_REGNUM;
  563. status = regcache->raw_read (raw_regnum, buffer);
  564. if (status == REG_VALID)
  565. status = regcache->raw_read (raw_regnum + 1, buffer + 1);
  566. }
  567. else if (reg == RL78_SP_REGNUM)
  568. {
  569. status = regcache->raw_read (RL78_SPL_REGNUM, buffer);
  570. if (status == REG_VALID)
  571. status = regcache->raw_read (RL78_SPH_REGNUM, buffer + 1);
  572. }
  573. else if (reg == RL78_PC_REGNUM)
  574. {
  575. gdb_byte rawbuf[4];
  576. status = regcache->raw_read (RL78_RAW_PC_REGNUM, rawbuf);
  577. memcpy (buffer, rawbuf, 3);
  578. }
  579. else if (RL78_X_REGNUM <= reg && reg <= RL78_H_REGNUM)
  580. {
  581. ULONGEST psw;
  582. status = regcache->raw_read (RL78_PSW_REGNUM, &psw);
  583. if (status == REG_VALID)
  584. {
  585. /* RSB0 is at bit 3; RSBS1 is at bit 5. */
  586. int bank = ((psw >> 3) & 1) | ((psw >> 4) & 1);
  587. int raw_regnum = RL78_RAW_BANK0_R0_REGNUM + bank * RL78_REGS_PER_BANK
  588. + (reg - RL78_X_REGNUM);
  589. status = regcache->raw_read (raw_regnum, buffer);
  590. }
  591. }
  592. else if (RL78_AX_REGNUM <= reg && reg <= RL78_HL_REGNUM)
  593. {
  594. ULONGEST psw;
  595. status = regcache->raw_read (RL78_PSW_REGNUM, &psw);
  596. if (status == REG_VALID)
  597. {
  598. /* RSB0 is at bit 3; RSBS1 is at bit 5. */
  599. int bank = ((psw >> 3) & 1) | ((psw >> 4) & 1);
  600. int raw_regnum = RL78_RAW_BANK0_R0_REGNUM + bank * RL78_REGS_PER_BANK
  601. + 2 * (reg - RL78_AX_REGNUM);
  602. status = regcache->raw_read (raw_regnum, buffer);
  603. if (status == REG_VALID)
  604. status = regcache->raw_read (raw_regnum + 1, buffer + 1);
  605. }
  606. }
  607. else
  608. gdb_assert_not_reached ("invalid pseudo register number");
  609. return status;
  610. }
  611. /* Implement the "pseudo_register_write" gdbarch method. */
  612. static void
  613. rl78_pseudo_register_write (struct gdbarch *gdbarch,
  614. struct regcache *regcache,
  615. int reg, const gdb_byte *buffer)
  616. {
  617. if (RL78_BANK0_R0_REGNUM <= reg && reg <= RL78_BANK3_R7_REGNUM)
  618. {
  619. int raw_regnum = RL78_RAW_BANK0_R0_REGNUM
  620. + (reg - RL78_BANK0_R0_REGNUM);
  621. regcache->raw_write (raw_regnum, buffer);
  622. }
  623. else if (RL78_BANK0_RP0_REGNUM <= reg && reg <= RL78_BANK3_RP3_REGNUM)
  624. {
  625. int raw_regnum = 2 * (reg - RL78_BANK0_RP0_REGNUM)
  626. + RL78_RAW_BANK0_R0_REGNUM;
  627. regcache->raw_write (raw_regnum, buffer);
  628. regcache->raw_write (raw_regnum + 1, buffer + 1);
  629. }
  630. else if (RL78_BANK0_RP0_PTR_REGNUM <= reg && reg <= RL78_BANK3_RP3_PTR_REGNUM)
  631. {
  632. int raw_regnum = 2 * (reg - RL78_BANK0_RP0_PTR_REGNUM)
  633. + RL78_RAW_BANK0_R0_REGNUM;
  634. regcache->raw_write (raw_regnum, buffer);
  635. regcache->raw_write (raw_regnum + 1, buffer + 1);
  636. }
  637. else if (reg == RL78_SP_REGNUM)
  638. {
  639. regcache->raw_write (RL78_SPL_REGNUM, buffer);
  640. regcache->raw_write (RL78_SPH_REGNUM, buffer + 1);
  641. }
  642. else if (reg == RL78_PC_REGNUM)
  643. {
  644. gdb_byte rawbuf[4];
  645. memcpy (rawbuf, buffer, 3);
  646. rawbuf[3] = 0;
  647. regcache->raw_write (RL78_RAW_PC_REGNUM, rawbuf);
  648. }
  649. else if (RL78_X_REGNUM <= reg && reg <= RL78_H_REGNUM)
  650. {
  651. ULONGEST psw;
  652. int bank;
  653. int raw_regnum;
  654. regcache_raw_read_unsigned (regcache, RL78_PSW_REGNUM, &psw);
  655. bank = ((psw >> 3) & 1) | ((psw >> 4) & 1);
  656. /* RSB0 is at bit 3; RSBS1 is at bit 5. */
  657. raw_regnum = RL78_RAW_BANK0_R0_REGNUM + bank * RL78_REGS_PER_BANK
  658. + (reg - RL78_X_REGNUM);
  659. regcache->raw_write (raw_regnum, buffer);
  660. }
  661. else if (RL78_AX_REGNUM <= reg && reg <= RL78_HL_REGNUM)
  662. {
  663. ULONGEST psw;
  664. int bank, raw_regnum;
  665. regcache_raw_read_unsigned (regcache, RL78_PSW_REGNUM, &psw);
  666. bank = ((psw >> 3) & 1) | ((psw >> 4) & 1);
  667. /* RSB0 is at bit 3; RSBS1 is at bit 5. */
  668. raw_regnum = RL78_RAW_BANK0_R0_REGNUM + bank * RL78_REGS_PER_BANK
  669. + 2 * (reg - RL78_AX_REGNUM);
  670. regcache->raw_write (raw_regnum, buffer);
  671. regcache->raw_write (raw_regnum + 1, buffer + 1);
  672. }
  673. else
  674. gdb_assert_not_reached ("invalid pseudo register number");
  675. }
  676. /* The documented BRK instruction is actually a two byte sequence,
  677. {0x61, 0xcc}, but instructions may be as short as one byte.
  678. Correspondence with Renesas revealed that the one byte sequence
  679. 0xff is used when a one byte breakpoint instruction is required. */
  680. constexpr gdb_byte rl78_break_insn[] = { 0xff };
  681. typedef BP_MANIPULATION (rl78_break_insn) rl78_breakpoint;
  682. /* Define a "handle" struct for fetching the next opcode. */
  683. struct rl78_get_opcode_byte_handle
  684. {
  685. CORE_ADDR pc;
  686. };
  687. static int
  688. opc_reg_to_gdb_regnum (int opcreg)
  689. {
  690. switch (opcreg)
  691. {
  692. case RL78_Reg_X:
  693. return RL78_X_REGNUM;
  694. case RL78_Reg_A:
  695. return RL78_A_REGNUM;
  696. case RL78_Reg_C:
  697. return RL78_C_REGNUM;
  698. case RL78_Reg_B:
  699. return RL78_B_REGNUM;
  700. case RL78_Reg_E:
  701. return RL78_E_REGNUM;
  702. case RL78_Reg_D:
  703. return RL78_D_REGNUM;
  704. case RL78_Reg_L:
  705. return RL78_L_REGNUM;
  706. case RL78_Reg_H:
  707. return RL78_H_REGNUM;
  708. case RL78_Reg_AX:
  709. return RL78_AX_REGNUM;
  710. case RL78_Reg_BC:
  711. return RL78_BC_REGNUM;
  712. case RL78_Reg_DE:
  713. return RL78_DE_REGNUM;
  714. case RL78_Reg_HL:
  715. return RL78_HL_REGNUM;
  716. case RL78_Reg_SP:
  717. return RL78_SP_REGNUM;
  718. case RL78_Reg_PSW:
  719. return RL78_PSW_REGNUM;
  720. case RL78_Reg_CS:
  721. return RL78_CS_REGNUM;
  722. case RL78_Reg_ES:
  723. return RL78_ES_REGNUM;
  724. case RL78_Reg_PMC:
  725. return RL78_PMC_REGNUM;
  726. case RL78_Reg_MEM:
  727. return RL78_MEM_REGNUM;
  728. default:
  729. internal_error (__FILE__, __LINE__,
  730. _("Undefined mapping for opc reg %d"),
  731. opcreg);
  732. }
  733. /* Not reached. */
  734. return 0;
  735. }
  736. /* Fetch a byte on behalf of the opcode decoder. HANDLE contains
  737. the memory address of the next byte to fetch. If successful,
  738. the address in the handle is updated and the byte fetched is
  739. returned as the value of the function. If not successful, -1
  740. is returned. */
  741. static int
  742. rl78_get_opcode_byte (void *handle)
  743. {
  744. struct rl78_get_opcode_byte_handle *opcdata
  745. = (struct rl78_get_opcode_byte_handle *) handle;
  746. int status;
  747. gdb_byte byte;
  748. status = target_read_memory (opcdata->pc, &byte, 1);
  749. if (status == 0)
  750. {
  751. opcdata->pc += 1;
  752. return byte;
  753. }
  754. else
  755. return -1;
  756. }
  757. /* Function for finding saved registers in a 'struct pv_area'; this
  758. function is passed to pv_area::scan.
  759. If VALUE is a saved register, ADDR says it was saved at a constant
  760. offset from the frame base, and SIZE indicates that the whole
  761. register was saved, record its offset. */
  762. static void
  763. check_for_saved (void *result_untyped, pv_t addr, CORE_ADDR size,
  764. pv_t value)
  765. {
  766. struct rl78_prologue *result = (struct rl78_prologue *) result_untyped;
  767. if (value.kind == pvk_register
  768. && value.k == 0
  769. && pv_is_register (addr, RL78_SP_REGNUM)
  770. && size == register_size (target_gdbarch (), value.reg))
  771. result->reg_offset[value.reg] = addr.k;
  772. }
  773. /* Analyze a prologue starting at START_PC, going no further than
  774. LIMIT_PC. Fill in RESULT as appropriate. */
  775. static void
  776. rl78_analyze_prologue (CORE_ADDR start_pc,
  777. CORE_ADDR limit_pc, struct rl78_prologue *result)
  778. {
  779. CORE_ADDR pc, next_pc;
  780. int rn;
  781. pv_t reg[RL78_NUM_TOTAL_REGS];
  782. CORE_ADDR after_last_frame_setup_insn = start_pc;
  783. int bank = 0;
  784. memset (result, 0, sizeof (*result));
  785. for (rn = 0; rn < RL78_NUM_TOTAL_REGS; rn++)
  786. {
  787. reg[rn] = pv_register (rn, 0);
  788. result->reg_offset[rn] = 1;
  789. }
  790. pv_area stack (RL78_SP_REGNUM, gdbarch_addr_bit (target_gdbarch ()));
  791. /* The call instruction has saved the return address on the stack. */
  792. reg[RL78_SP_REGNUM] = pv_add_constant (reg[RL78_SP_REGNUM], -4);
  793. stack.store (reg[RL78_SP_REGNUM], 4, reg[RL78_PC_REGNUM]);
  794. pc = start_pc;
  795. while (pc < limit_pc)
  796. {
  797. int bytes_read;
  798. struct rl78_get_opcode_byte_handle opcode_handle;
  799. RL78_Opcode_Decoded opc;
  800. opcode_handle.pc = pc;
  801. bytes_read = rl78_decode_opcode (pc, &opc, rl78_get_opcode_byte,
  802. &opcode_handle, RL78_ISA_DEFAULT);
  803. next_pc = pc + bytes_read;
  804. if (opc.id == RLO_sel)
  805. {
  806. bank = opc.op[1].addend;
  807. }
  808. else if (opc.id == RLO_mov
  809. && opc.op[0].type == RL78_Operand_PreDec
  810. && opc.op[0].reg == RL78_Reg_SP
  811. && opc.op[1].type == RL78_Operand_Register)
  812. {
  813. int rsrc = (bank * RL78_REGS_PER_BANK)
  814. + 2 * (opc.op[1].reg - RL78_Reg_AX);
  815. reg[RL78_SP_REGNUM] = pv_add_constant (reg[RL78_SP_REGNUM], -1);
  816. stack.store (reg[RL78_SP_REGNUM], 1, reg[rsrc]);
  817. reg[RL78_SP_REGNUM] = pv_add_constant (reg[RL78_SP_REGNUM], -1);
  818. stack.store (reg[RL78_SP_REGNUM], 1, reg[rsrc + 1]);
  819. after_last_frame_setup_insn = next_pc;
  820. }
  821. else if (opc.id == RLO_sub
  822. && opc.op[0].type == RL78_Operand_Register
  823. && opc.op[0].reg == RL78_Reg_SP
  824. && opc.op[1].type == RL78_Operand_Immediate)
  825. {
  826. int addend = opc.op[1].addend;
  827. reg[RL78_SP_REGNUM] = pv_add_constant (reg[RL78_SP_REGNUM],
  828. -addend);
  829. after_last_frame_setup_insn = next_pc;
  830. }
  831. else if (opc.id == RLO_mov
  832. && opc.size == RL78_Word
  833. && opc.op[0].type == RL78_Operand_Register
  834. && opc.op[1].type == RL78_Operand_Indirect
  835. && opc.op[1].addend == RL78_SP_ADDR)
  836. {
  837. reg[opc_reg_to_gdb_regnum (opc.op[0].reg)]
  838. = reg[RL78_SP_REGNUM];
  839. }
  840. else if (opc.id == RLO_sub
  841. && opc.size == RL78_Word
  842. && opc.op[0].type == RL78_Operand_Register
  843. && opc.op[1].type == RL78_Operand_Immediate)
  844. {
  845. int addend = opc.op[1].addend;
  846. int regnum = opc_reg_to_gdb_regnum (opc.op[0].reg);
  847. reg[regnum] = pv_add_constant (reg[regnum], -addend);
  848. }
  849. else if (opc.id == RLO_mov
  850. && opc.size == RL78_Word
  851. && opc.op[0].type == RL78_Operand_Indirect
  852. && opc.op[0].addend == RL78_SP_ADDR
  853. && opc.op[1].type == RL78_Operand_Register)
  854. {
  855. reg[RL78_SP_REGNUM]
  856. = reg[opc_reg_to_gdb_regnum (opc.op[1].reg)];
  857. after_last_frame_setup_insn = next_pc;
  858. }
  859. else
  860. {
  861. /* Terminate the prologue scan. */
  862. break;
  863. }
  864. pc = next_pc;
  865. }
  866. /* Is the frame size (offset, really) a known constant? */
  867. if (pv_is_register (reg[RL78_SP_REGNUM], RL78_SP_REGNUM))
  868. result->frame_size = reg[RL78_SP_REGNUM].k;
  869. /* Record where all the registers were saved. */
  870. stack.scan (check_for_saved, (void *) result);
  871. result->prologue_end = after_last_frame_setup_insn;
  872. }
  873. /* Implement the "addr_bits_remove" gdbarch method. */
  874. static CORE_ADDR
  875. rl78_addr_bits_remove (struct gdbarch *gdbarch, CORE_ADDR addr)
  876. {
  877. return addr & 0xffffff;
  878. }
  879. /* Implement the "address_to_pointer" gdbarch method. */
  880. static void
  881. rl78_address_to_pointer (struct gdbarch *gdbarch,
  882. struct type *type, gdb_byte *buf, CORE_ADDR addr)
  883. {
  884. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  885. store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order,
  886. addr & 0xffffff);
  887. }
  888. /* Implement the "pointer_to_address" gdbarch method. */
  889. static CORE_ADDR
  890. rl78_pointer_to_address (struct gdbarch *gdbarch,
  891. struct type *type, const gdb_byte *buf)
  892. {
  893. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  894. CORE_ADDR addr
  895. = extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);
  896. /* Is it a code address? */
  897. if (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_FUNC
  898. || TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_METHOD
  899. || TYPE_CODE_SPACE (TYPE_TARGET_TYPE (type))
  900. || TYPE_LENGTH (type) == 4)
  901. return rl78_make_instruction_address (addr);
  902. else
  903. return rl78_make_data_address (addr);
  904. }
  905. /* Implement the "skip_prologue" gdbarch method. */
  906. static CORE_ADDR
  907. rl78_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  908. {
  909. const char *name;
  910. CORE_ADDR func_addr, func_end;
  911. struct rl78_prologue p;
  912. /* Try to find the extent of the function that contains PC. */
  913. if (!find_pc_partial_function (pc, &name, &func_addr, &func_end))
  914. return pc;
  915. rl78_analyze_prologue (pc, func_end, &p);
  916. return p.prologue_end;
  917. }
  918. /* Implement the "unwind_pc" gdbarch method. */
  919. static CORE_ADDR
  920. rl78_unwind_pc (struct gdbarch *arch, struct frame_info *next_frame)
  921. {
  922. return rl78_addr_bits_remove
  923. (arch, frame_unwind_register_unsigned (next_frame,
  924. RL78_PC_REGNUM));
  925. }
  926. /* Given a frame described by THIS_FRAME, decode the prologue of its
  927. associated function if there is not cache entry as specified by
  928. THIS_PROLOGUE_CACHE. Save the decoded prologue in the cache and
  929. return that struct as the value of this function. */
  930. static struct rl78_prologue *
  931. rl78_analyze_frame_prologue (struct frame_info *this_frame,
  932. void **this_prologue_cache)
  933. {
  934. if (!*this_prologue_cache)
  935. {
  936. CORE_ADDR func_start, stop_addr;
  937. *this_prologue_cache = FRAME_OBSTACK_ZALLOC (struct rl78_prologue);
  938. func_start = get_frame_func (this_frame);
  939. stop_addr = get_frame_pc (this_frame);
  940. /* If we couldn't find any function containing the PC, then
  941. just initialize the prologue cache, but don't do anything. */
  942. if (!func_start)
  943. stop_addr = func_start;
  944. rl78_analyze_prologue (func_start, stop_addr,
  945. (struct rl78_prologue *) *this_prologue_cache);
  946. }
  947. return (struct rl78_prologue *) *this_prologue_cache;
  948. }
  949. /* Given a frame and a prologue cache, return this frame's base. */
  950. static CORE_ADDR
  951. rl78_frame_base (struct frame_info *this_frame, void **this_prologue_cache)
  952. {
  953. struct rl78_prologue *p
  954. = rl78_analyze_frame_prologue (this_frame, this_prologue_cache);
  955. CORE_ADDR sp = get_frame_register_unsigned (this_frame, RL78_SP_REGNUM);
  956. return rl78_make_data_address (sp - p->frame_size);
  957. }
  958. /* Implement the "frame_this_id" method for unwinding frames. */
  959. static void
  960. rl78_this_id (struct frame_info *this_frame,
  961. void **this_prologue_cache, struct frame_id *this_id)
  962. {
  963. *this_id = frame_id_build (rl78_frame_base (this_frame,
  964. this_prologue_cache),
  965. get_frame_func (this_frame));
  966. }
  967. /* Implement the "frame_prev_register" method for unwinding frames. */
  968. static struct value *
  969. rl78_prev_register (struct frame_info *this_frame,
  970. void **this_prologue_cache, int regnum)
  971. {
  972. struct rl78_prologue *p
  973. = rl78_analyze_frame_prologue (this_frame, this_prologue_cache);
  974. CORE_ADDR frame_base = rl78_frame_base (this_frame, this_prologue_cache);
  975. if (regnum == RL78_SP_REGNUM)
  976. return frame_unwind_got_constant (this_frame, regnum, frame_base);
  977. else if (regnum == RL78_SPL_REGNUM)
  978. return frame_unwind_got_constant (this_frame, regnum,
  979. (frame_base & 0xff));
  980. else if (regnum == RL78_SPH_REGNUM)
  981. return frame_unwind_got_constant (this_frame, regnum,
  982. ((frame_base >> 8) & 0xff));
  983. /* If prologue analysis says we saved this register somewhere,
  984. return a description of the stack slot holding it. */
  985. else if (p->reg_offset[regnum] != 1)
  986. {
  987. struct value *rv =
  988. frame_unwind_got_memory (this_frame, regnum,
  989. frame_base + p->reg_offset[regnum]);
  990. if (regnum == RL78_PC_REGNUM)
  991. {
  992. ULONGEST pc = rl78_make_instruction_address (value_as_long (rv));
  993. return frame_unwind_got_constant (this_frame, regnum, pc);
  994. }
  995. return rv;
  996. }
  997. /* Otherwise, presume we haven't changed the value of this
  998. register, and get it from the next frame. */
  999. else
  1000. return frame_unwind_got_register (this_frame, regnum, regnum);
  1001. }
  1002. static const struct frame_unwind rl78_unwind =
  1003. {
  1004. "rl78 prologue",
  1005. NORMAL_FRAME,
  1006. default_frame_unwind_stop_reason,
  1007. rl78_this_id,
  1008. rl78_prev_register,
  1009. NULL,
  1010. default_frame_sniffer
  1011. };
  1012. /* Implement the "dwarf_reg_to_regnum" gdbarch method. */
  1013. static int
  1014. rl78_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int reg)
  1015. {
  1016. if (0 <= reg && reg <= 31)
  1017. {
  1018. if ((reg & 1) == 0)
  1019. /* Map even registers to their 16-bit counterparts which have a
  1020. pointer type. This is usually what is required from the DWARF
  1021. info. */
  1022. return (reg >> 1) + RL78_BANK0_RP0_PTR_REGNUM;
  1023. else
  1024. return reg;
  1025. }
  1026. else if (reg == 32)
  1027. return RL78_SP_REGNUM;
  1028. else if (reg == 33)
  1029. return -1; /* ap */
  1030. else if (reg == 34)
  1031. return RL78_PSW_REGNUM;
  1032. else if (reg == 35)
  1033. return RL78_ES_REGNUM;
  1034. else if (reg == 36)
  1035. return RL78_CS_REGNUM;
  1036. else if (reg == 37)
  1037. return RL78_PC_REGNUM;
  1038. else
  1039. return -1;
  1040. }
  1041. /* Implement the `register_sim_regno' gdbarch method. */
  1042. static int
  1043. rl78_register_sim_regno (struct gdbarch *gdbarch, int regnum)
  1044. {
  1045. gdb_assert (regnum < RL78_NUM_REGS);
  1046. /* So long as regnum is in [0, RL78_NUM_REGS), it's valid. We
  1047. just want to override the default here which disallows register
  1048. numbers which have no names. */
  1049. return regnum;
  1050. }
  1051. /* Implement the "return_value" gdbarch method. */
  1052. static enum return_value_convention
  1053. rl78_return_value (struct gdbarch *gdbarch,
  1054. struct value *function,
  1055. struct type *valtype,
  1056. struct regcache *regcache,
  1057. gdb_byte *readbuf, const gdb_byte *writebuf)
  1058. {
  1059. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1060. ULONGEST valtype_len = TYPE_LENGTH (valtype);
  1061. rl78_gdbarch_tdep *tdep = (rl78_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1062. int is_g10 = tdep->elf_flags & E_FLAG_RL78_G10;
  1063. if (valtype_len > 8)
  1064. return RETURN_VALUE_STRUCT_CONVENTION;
  1065. if (readbuf)
  1066. {
  1067. ULONGEST u;
  1068. int argreg = RL78_RAW_BANK1_R0_REGNUM;
  1069. CORE_ADDR g10_raddr = 0xffec8;
  1070. int offset = 0;
  1071. while (valtype_len > 0)
  1072. {
  1073. if (is_g10)
  1074. u = read_memory_integer (g10_raddr, 1,
  1075. gdbarch_byte_order (gdbarch));
  1076. else
  1077. regcache_cooked_read_unsigned (regcache, argreg, &u);
  1078. store_unsigned_integer (readbuf + offset, 1, byte_order, u);
  1079. valtype_len -= 1;
  1080. offset += 1;
  1081. argreg++;
  1082. g10_raddr++;
  1083. }
  1084. }
  1085. if (writebuf)
  1086. {
  1087. ULONGEST u;
  1088. int argreg = RL78_RAW_BANK1_R0_REGNUM;
  1089. CORE_ADDR g10_raddr = 0xffec8;
  1090. int offset = 0;
  1091. while (valtype_len > 0)
  1092. {
  1093. u = extract_unsigned_integer (writebuf + offset, 1, byte_order);
  1094. if (is_g10) {
  1095. gdb_byte b = u & 0xff;
  1096. write_memory (g10_raddr, &b, 1);
  1097. }
  1098. else
  1099. regcache_cooked_write_unsigned (regcache, argreg, u);
  1100. valtype_len -= 1;
  1101. offset += 1;
  1102. argreg++;
  1103. g10_raddr++;
  1104. }
  1105. }
  1106. return RETURN_VALUE_REGISTER_CONVENTION;
  1107. }
  1108. /* Implement the "frame_align" gdbarch method. */
  1109. static CORE_ADDR
  1110. rl78_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
  1111. {
  1112. return rl78_make_data_address (align_down (sp, 2));
  1113. }
  1114. /* Implement the "dummy_id" gdbarch method. */
  1115. static struct frame_id
  1116. rl78_dummy_id (struct gdbarch *gdbarch, struct frame_info *this_frame)
  1117. {
  1118. return
  1119. frame_id_build (rl78_make_data_address
  1120. (get_frame_register_unsigned
  1121. (this_frame, RL78_SP_REGNUM)),
  1122. get_frame_pc (this_frame));
  1123. }
  1124. /* Implement the "push_dummy_call" gdbarch method. */
  1125. static CORE_ADDR
  1126. rl78_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  1127. struct regcache *regcache, CORE_ADDR bp_addr,
  1128. int nargs, struct value **args, CORE_ADDR sp,
  1129. function_call_return_method return_method,
  1130. CORE_ADDR struct_addr)
  1131. {
  1132. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1133. gdb_byte buf[4];
  1134. int i;
  1135. /* Push arguments in reverse order. */
  1136. for (i = nargs - 1; i >= 0; i--)
  1137. {
  1138. struct type *value_type = value_enclosing_type (args[i]);
  1139. int len = TYPE_LENGTH (value_type);
  1140. int container_len = (len + 1) & ~1;
  1141. sp -= container_len;
  1142. write_memory (rl78_make_data_address (sp),
  1143. value_contents_all (args[i]).data (), len);
  1144. }
  1145. /* Store struct value address. */
  1146. if (return_method == return_method_struct)
  1147. {
  1148. store_unsigned_integer (buf, 2, byte_order, struct_addr);
  1149. sp -= 2;
  1150. write_memory (rl78_make_data_address (sp), buf, 2);
  1151. }
  1152. /* Store return address. */
  1153. sp -= 4;
  1154. store_unsigned_integer (buf, 4, byte_order, bp_addr);
  1155. write_memory (rl78_make_data_address (sp), buf, 4);
  1156. /* Finally, update the stack pointer... */
  1157. regcache_cooked_write_unsigned (regcache, RL78_SP_REGNUM, sp);
  1158. /* DWARF2/GCC uses the stack address *before* the function call as a
  1159. frame's CFA. */
  1160. return rl78_make_data_address (sp + 4);
  1161. }
  1162. /* Allocate and initialize a gdbarch object. */
  1163. static struct gdbarch *
  1164. rl78_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  1165. {
  1166. struct gdbarch *gdbarch;
  1167. int elf_flags;
  1168. /* Extract the elf_flags if available. */
  1169. if (info.abfd != NULL
  1170. && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
  1171. elf_flags = elf_elfheader (info.abfd)->e_flags;
  1172. else
  1173. elf_flags = 0;
  1174. /* Try to find the architecture in the list of already defined
  1175. architectures. */
  1176. for (arches = gdbarch_list_lookup_by_info (arches, &info);
  1177. arches != NULL;
  1178. arches = gdbarch_list_lookup_by_info (arches->next, &info))
  1179. {
  1180. rl78_gdbarch_tdep *tdep
  1181. = (rl78_gdbarch_tdep *) gdbarch_tdep (arches->gdbarch);
  1182. if (tdep->elf_flags != elf_flags)
  1183. continue;
  1184. return arches->gdbarch;
  1185. }
  1186. /* None found, create a new architecture from the information
  1187. provided. */
  1188. rl78_gdbarch_tdep * tdep = new rl78_gdbarch_tdep;
  1189. gdbarch = gdbarch_alloc (&info, tdep);
  1190. tdep->elf_flags = elf_flags;
  1191. /* Initialize types. */
  1192. tdep->rl78_void = arch_type (gdbarch, TYPE_CODE_VOID, TARGET_CHAR_BIT,
  1193. "void");
  1194. tdep->rl78_uint8 = arch_integer_type (gdbarch, 8, 1, "uint8_t");
  1195. tdep->rl78_int8 = arch_integer_type (gdbarch, 8, 0, "int8_t");
  1196. tdep->rl78_uint16 = arch_integer_type (gdbarch, 16, 1, "uint16_t");
  1197. tdep->rl78_int16 = arch_integer_type (gdbarch, 16, 0, "int16_t");
  1198. tdep->rl78_uint32 = arch_integer_type (gdbarch, 32, 1, "uint32_t");
  1199. tdep->rl78_int32 = arch_integer_type (gdbarch, 32, 0, "int32_t");
  1200. tdep->rl78_data_pointer
  1201. = arch_pointer_type (gdbarch, 16, "rl78_data_addr_t", tdep->rl78_void);
  1202. tdep->rl78_code_pointer
  1203. = arch_pointer_type (gdbarch, 32, "rl78_code_addr_t", tdep->rl78_void);
  1204. /* Registers. */
  1205. set_gdbarch_num_regs (gdbarch, RL78_NUM_REGS);
  1206. set_gdbarch_num_pseudo_regs (gdbarch, RL78_NUM_PSEUDO_REGS);
  1207. if (tdep->elf_flags & E_FLAG_RL78_G10)
  1208. set_gdbarch_register_name (gdbarch, rl78_g10_register_name);
  1209. else
  1210. set_gdbarch_register_name (gdbarch, rl78_register_name);
  1211. set_gdbarch_register_type (gdbarch, rl78_register_type);
  1212. set_gdbarch_pc_regnum (gdbarch, RL78_PC_REGNUM);
  1213. set_gdbarch_sp_regnum (gdbarch, RL78_SP_REGNUM);
  1214. set_gdbarch_pseudo_register_read (gdbarch, rl78_pseudo_register_read);
  1215. set_gdbarch_pseudo_register_write (gdbarch, rl78_pseudo_register_write);
  1216. set_gdbarch_dwarf2_reg_to_regnum (gdbarch, rl78_dwarf_reg_to_regnum);
  1217. set_gdbarch_register_reggroup_p (gdbarch, rl78_register_reggroup_p);
  1218. set_gdbarch_register_sim_regno (gdbarch, rl78_register_sim_regno);
  1219. /* Data types. */
  1220. set_gdbarch_char_signed (gdbarch, 0);
  1221. set_gdbarch_short_bit (gdbarch, 16);
  1222. set_gdbarch_int_bit (gdbarch, 16);
  1223. set_gdbarch_long_bit (gdbarch, 32);
  1224. set_gdbarch_long_long_bit (gdbarch, 64);
  1225. set_gdbarch_ptr_bit (gdbarch, 16);
  1226. set_gdbarch_addr_bit (gdbarch, 32);
  1227. set_gdbarch_dwarf2_addr_size (gdbarch, 4);
  1228. set_gdbarch_float_bit (gdbarch, 32);
  1229. set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
  1230. set_gdbarch_double_bit (gdbarch, 32);
  1231. set_gdbarch_long_double_bit (gdbarch, 64);
  1232. set_gdbarch_double_format (gdbarch, floatformats_ieee_single);
  1233. set_gdbarch_long_double_format (gdbarch, floatformats_ieee_double);
  1234. set_gdbarch_pointer_to_address (gdbarch, rl78_pointer_to_address);
  1235. set_gdbarch_address_to_pointer (gdbarch, rl78_address_to_pointer);
  1236. set_gdbarch_addr_bits_remove (gdbarch, rl78_addr_bits_remove);
  1237. /* Breakpoints. */
  1238. set_gdbarch_breakpoint_kind_from_pc (gdbarch, rl78_breakpoint::kind_from_pc);
  1239. set_gdbarch_sw_breakpoint_from_kind (gdbarch, rl78_breakpoint::bp_from_kind);
  1240. set_gdbarch_decr_pc_after_break (gdbarch, 1);
  1241. /* Frames, prologues, etc. */
  1242. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  1243. set_gdbarch_skip_prologue (gdbarch, rl78_skip_prologue);
  1244. set_gdbarch_unwind_pc (gdbarch, rl78_unwind_pc);
  1245. set_gdbarch_frame_align (gdbarch, rl78_frame_align);
  1246. dwarf2_append_unwinders (gdbarch);
  1247. frame_unwind_append_unwinder (gdbarch, &rl78_unwind);
  1248. /* Dummy frames, return values. */
  1249. set_gdbarch_dummy_id (gdbarch, rl78_dummy_id);
  1250. set_gdbarch_push_dummy_call (gdbarch, rl78_push_dummy_call);
  1251. set_gdbarch_return_value (gdbarch, rl78_return_value);
  1252. /* Virtual tables. */
  1253. set_gdbarch_vbit_in_delta (gdbarch, 1);
  1254. return gdbarch;
  1255. }
  1256. /* Register the above initialization routine. */
  1257. void _initialize_rl78_tdep ();
  1258. void
  1259. _initialize_rl78_tdep ()
  1260. {
  1261. register_gdbarch_init (bfd_arch_rl78, rl78_gdbarch_init);
  1262. }