nds32-tdep.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100
  1. /* Target-dependent code for the NDS32 architecture, for GDB.
  2. Copyright (C) 2013-2022 Free Software Foundation, Inc.
  3. Contributed by Andes Technology Corporation.
  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 "frame.h"
  17. #include "frame-unwind.h"
  18. #include "frame-base.h"
  19. #include "symtab.h"
  20. #include "gdbtypes.h"
  21. #include "gdbcore.h"
  22. #include "value.h"
  23. #include "reggroups.h"
  24. #include "inferior.h"
  25. #include "osabi.h"
  26. #include "arch-utils.h"
  27. #include "regcache.h"
  28. #include "dis-asm.h"
  29. #include "user-regs.h"
  30. #include "elf-bfd.h"
  31. #include "dwarf2/frame.h"
  32. #include "remote.h"
  33. #include "target-descriptions.h"
  34. #include "nds32-tdep.h"
  35. #include "elf/nds32.h"
  36. #include "opcode/nds32.h"
  37. #include <algorithm>
  38. #include "features/nds32.c"
  39. /* Simple macros for instruction analysis. */
  40. #define CHOP_BITS(insn, n) (insn & ~__MASK (n))
  41. #define N32_LSMW_ENABLE4(insn) (((insn) >> 6) & 0xf)
  42. #define N32_SMW_ADM \
  43. N32_TYPE4 (LSMW, 0, 0, 0, 1, (N32_LSMW_ADM << 2) | N32_LSMW_LSMW)
  44. #define N32_LMW_BIM \
  45. N32_TYPE4 (LSMW, 0, 0, 0, 0, (N32_LSMW_BIM << 2) | N32_LSMW_LSMW)
  46. #define N32_FLDI_SP \
  47. N32_TYPE2 (LDC, 0, REG_SP, 0)
  48. /* Use an invalid address value as 'not available' marker. */
  49. enum { REG_UNAVAIL = (CORE_ADDR) -1 };
  50. /* Use an impossible value as invalid offset. */
  51. enum { INVALID_OFFSET = (CORE_ADDR) -1 };
  52. /* Instruction groups for NDS32 epilogue analysis. */
  53. enum
  54. {
  55. /* Instructions used everywhere, not only in epilogue. */
  56. INSN_NORMAL,
  57. /* Instructions used to reset sp for local vars, arguments, etc. */
  58. INSN_RESET_SP,
  59. /* Instructions used to recover saved regs and to recover padding. */
  60. INSN_RECOVER,
  61. /* Instructions used to return to the caller. */
  62. INSN_RETURN,
  63. /* Instructions used to recover saved regs and to return to the caller. */
  64. INSN_RECOVER_RETURN,
  65. };
  66. static const char *const nds32_register_names[] =
  67. {
  68. /* 32 GPRs. */
  69. "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7",
  70. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  71. "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  72. "r24", "r25", "r26", "r27", "fp", "gp", "lp", "sp",
  73. /* PC. */
  74. "pc",
  75. };
  76. static const char *const nds32_fdr_register_names[] =
  77. {
  78. "fd0", "fd1", "fd2", "fd3", "fd4", "fd5", "fd6", "fd7",
  79. "fd8", "fd9", "fd10", "fd11", "fd12", "fd13", "fd14", "fd15",
  80. "fd16", "fd17", "fd18", "fd19", "fd20", "fd21", "fd22", "fd23",
  81. "fd24", "fd25", "fd26", "fd27", "fd28", "fd29", "fd30", "fd31"
  82. };
  83. static const char *const nds32_fsr_register_names[] =
  84. {
  85. "fs0", "fs1", "fs2", "fs3", "fs4", "fs5", "fs6", "fs7",
  86. "fs8", "fs9", "fs10", "fs11", "fs12", "fs13", "fs14", "fs15",
  87. "fs16", "fs17", "fs18", "fs19", "fs20", "fs21", "fs22", "fs23",
  88. "fs24", "fs25", "fs26", "fs27", "fs28", "fs29", "fs30", "fs31"
  89. };
  90. /* The number of registers for four FPU configuration options. */
  91. const int num_fdr_map[] = { 4, 8, 16, 32 };
  92. const int num_fsr_map[] = { 8, 16, 32, 32 };
  93. /* Aliases for registers. */
  94. static const struct
  95. {
  96. const char *name;
  97. const char *alias;
  98. } nds32_register_aliases[] =
  99. {
  100. {"r15", "ta"},
  101. {"r26", "p0"},
  102. {"r27", "p1"},
  103. {"fp", "r28"},
  104. {"gp", "r29"},
  105. {"lp", "r30"},
  106. {"sp", "r31"},
  107. {"cr0", "cpu_ver"},
  108. {"cr1", "icm_cfg"},
  109. {"cr2", "dcm_cfg"},
  110. {"cr3", "mmu_cfg"},
  111. {"cr4", "msc_cfg"},
  112. {"cr5", "core_id"},
  113. {"cr6", "fucop_exist"},
  114. {"cr7", "msc_cfg2"},
  115. {"ir0", "psw"},
  116. {"ir1", "ipsw"},
  117. {"ir2", "p_psw"},
  118. {"ir3", "ivb"},
  119. {"ir4", "eva"},
  120. {"ir5", "p_eva"},
  121. {"ir6", "itype"},
  122. {"ir7", "p_itype"},
  123. {"ir8", "merr"},
  124. {"ir9", "ipc"},
  125. {"ir10", "p_ipc"},
  126. {"ir11", "oipc"},
  127. {"ir12", "p_p0"},
  128. {"ir13", "p_p1"},
  129. {"ir14", "int_mask"},
  130. {"ir15", "int_pend"},
  131. {"ir16", "sp_usr"},
  132. {"ir17", "sp_priv"},
  133. {"ir18", "int_pri"},
  134. {"ir19", "int_ctrl"},
  135. {"ir20", "sp_usr1"},
  136. {"ir21", "sp_priv1"},
  137. {"ir22", "sp_usr2"},
  138. {"ir23", "sp_priv2"},
  139. {"ir24", "sp_usr3"},
  140. {"ir25", "sp_priv3"},
  141. {"ir26", "int_mask2"},
  142. {"ir27", "int_pend2"},
  143. {"ir28", "int_pri2"},
  144. {"ir29", "int_trigger"},
  145. {"mr0", "mmu_ctl"},
  146. {"mr1", "l1_pptb"},
  147. {"mr2", "tlb_vpn"},
  148. {"mr3", "tlb_data"},
  149. {"mr4", "tlb_misc"},
  150. {"mr5", "vlpt_idx"},
  151. {"mr6", "ilmb"},
  152. {"mr7", "dlmb"},
  153. {"mr8", "cache_ctl"},
  154. {"mr9", "hsmp_saddr"},
  155. {"mr10", "hsmp_eaddr"},
  156. {"mr11", "bg_region"},
  157. {"dr0", "bpc0"},
  158. {"dr1", "bpc1"},
  159. {"dr2", "bpc2"},
  160. {"dr3", "bpc3"},
  161. {"dr4", "bpc4"},
  162. {"dr5", "bpc5"},
  163. {"dr6", "bpc6"},
  164. {"dr7", "bpc7"},
  165. {"dr8", "bpa0"},
  166. {"dr9", "bpa1"},
  167. {"dr10", "bpa2"},
  168. {"dr11", "bpa3"},
  169. {"dr12", "bpa4"},
  170. {"dr13", "bpa5"},
  171. {"dr14", "bpa6"},
  172. {"dr15", "bpa7"},
  173. {"dr16", "bpam0"},
  174. {"dr17", "bpam1"},
  175. {"dr18", "bpam2"},
  176. {"dr19", "bpam3"},
  177. {"dr20", "bpam4"},
  178. {"dr21", "bpam5"},
  179. {"dr22", "bpam6"},
  180. {"dr23", "bpam7"},
  181. {"dr24", "bpv0"},
  182. {"dr25", "bpv1"},
  183. {"dr26", "bpv2"},
  184. {"dr27", "bpv3"},
  185. {"dr28", "bpv4"},
  186. {"dr29", "bpv5"},
  187. {"dr30", "bpv6"},
  188. {"dr31", "bpv7"},
  189. {"dr32", "bpcid0"},
  190. {"dr33", "bpcid1"},
  191. {"dr34", "bpcid2"},
  192. {"dr35", "bpcid3"},
  193. {"dr36", "bpcid4"},
  194. {"dr37", "bpcid5"},
  195. {"dr38", "bpcid6"},
  196. {"dr39", "bpcid7"},
  197. {"dr40", "edm_cfg"},
  198. {"dr41", "edmsw"},
  199. {"dr42", "edm_ctl"},
  200. {"dr43", "edm_dtr"},
  201. {"dr44", "bpmtc"},
  202. {"dr45", "dimbr"},
  203. {"dr46", "tecr0"},
  204. {"dr47", "tecr1"},
  205. {"hspr0", "hsp_ctl"},
  206. {"hspr1", "sp_bound"},
  207. {"hspr2", "sp_bound_priv"},
  208. {"pfr0", "pfmc0"},
  209. {"pfr1", "pfmc1"},
  210. {"pfr2", "pfmc2"},
  211. {"pfr3", "pfm_ctl"},
  212. {"pfr4", "pft_ctl"},
  213. {"dmar0", "dma_cfg"},
  214. {"dmar1", "dma_gcsw"},
  215. {"dmar2", "dma_chnsel"},
  216. {"dmar3", "dma_act"},
  217. {"dmar4", "dma_setup"},
  218. {"dmar5", "dma_isaddr"},
  219. {"dmar6", "dma_esaddr"},
  220. {"dmar7", "dma_tcnt"},
  221. {"dmar8", "dma_status"},
  222. {"dmar9", "dma_2dset"},
  223. {"dmar10", "dma_2dsctl"},
  224. {"dmar11", "dma_rcnt"},
  225. {"dmar12", "dma_hstatus"},
  226. {"racr0", "prusr_acc_ctl"},
  227. {"fucpr", "fucop_ctl"},
  228. {"idr0", "sdz_ctl"},
  229. {"idr1", "misc_ctl"},
  230. {"idr2", "ecc_misc"},
  231. {"secur0", "sfcr"},
  232. {"secur1", "sign"},
  233. {"secur2", "isign"},
  234. {"secur3", "p_isign"},
  235. };
  236. /* Value of a register alias. BATON is the regnum of the corresponding
  237. register. */
  238. static struct value *
  239. value_of_nds32_reg (struct frame_info *frame, const void *baton)
  240. {
  241. return value_of_register ((int) (intptr_t) baton, frame);
  242. }
  243. /* Implement the "frame_align" gdbarch method. */
  244. static CORE_ADDR
  245. nds32_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
  246. {
  247. /* 8-byte aligned. */
  248. return align_down (sp, 8);
  249. }
  250. /* The same insn machine code is used for little-endian and big-endian. */
  251. constexpr gdb_byte nds32_break_insn[] = { 0xEA, 0x00 };
  252. typedef BP_MANIPULATION (nds32_break_insn) nds32_breakpoint;
  253. /* Implement the "dwarf2_reg_to_regnum" gdbarch method. */
  254. static int
  255. nds32_dwarf2_reg_to_regnum (struct gdbarch *gdbarch, int num)
  256. {
  257. nds32_gdbarch_tdep *tdep = (nds32_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  258. const int FSR = 38;
  259. const int FDR = FSR + 32;
  260. if (num >= 0 && num < 32)
  261. {
  262. /* General-purpose registers (R0 - R31). */
  263. return num;
  264. }
  265. else if (num >= FSR && num < FSR + 32)
  266. {
  267. /* Single precision floating-point registers (FS0 - FS31). */
  268. return num - FSR + tdep->fs0_regnum;
  269. }
  270. else if (num >= FDR && num < FDR + 32)
  271. {
  272. /* Double precision floating-point registers (FD0 - FD31). */
  273. return num - FDR + NDS32_FD0_REGNUM;
  274. }
  275. /* No match, return a inaccessible register number. */
  276. return -1;
  277. }
  278. /* NDS32 register groups. */
  279. static const reggroup *nds32_cr_reggroup;
  280. static const reggroup *nds32_ir_reggroup;
  281. static const reggroup *nds32_mr_reggroup;
  282. static const reggroup *nds32_dr_reggroup;
  283. static const reggroup *nds32_pfr_reggroup;
  284. static const reggroup *nds32_hspr_reggroup;
  285. static const reggroup *nds32_dmar_reggroup;
  286. static const reggroup *nds32_racr_reggroup;
  287. static const reggroup *nds32_idr_reggroup;
  288. static const reggroup *nds32_secur_reggroup;
  289. static void
  290. nds32_init_reggroups (void)
  291. {
  292. nds32_cr_reggroup = reggroup_new ("cr", USER_REGGROUP);
  293. nds32_ir_reggroup = reggroup_new ("ir", USER_REGGROUP);
  294. nds32_mr_reggroup = reggroup_new ("mr", USER_REGGROUP);
  295. nds32_dr_reggroup = reggroup_new ("dr", USER_REGGROUP);
  296. nds32_pfr_reggroup = reggroup_new ("pfr", USER_REGGROUP);
  297. nds32_hspr_reggroup = reggroup_new ("hspr", USER_REGGROUP);
  298. nds32_dmar_reggroup = reggroup_new ("dmar", USER_REGGROUP);
  299. nds32_racr_reggroup = reggroup_new ("racr", USER_REGGROUP);
  300. nds32_idr_reggroup = reggroup_new ("idr", USER_REGGROUP);
  301. nds32_secur_reggroup = reggroup_new ("secur", USER_REGGROUP);
  302. }
  303. static void
  304. nds32_add_reggroups (struct gdbarch *gdbarch)
  305. {
  306. /* Add NDS32 register groups. */
  307. reggroup_add (gdbarch, nds32_cr_reggroup);
  308. reggroup_add (gdbarch, nds32_ir_reggroup);
  309. reggroup_add (gdbarch, nds32_mr_reggroup);
  310. reggroup_add (gdbarch, nds32_dr_reggroup);
  311. reggroup_add (gdbarch, nds32_pfr_reggroup);
  312. reggroup_add (gdbarch, nds32_hspr_reggroup);
  313. reggroup_add (gdbarch, nds32_dmar_reggroup);
  314. reggroup_add (gdbarch, nds32_racr_reggroup);
  315. reggroup_add (gdbarch, nds32_idr_reggroup);
  316. reggroup_add (gdbarch, nds32_secur_reggroup);
  317. }
  318. /* Implement the "register_reggroup_p" gdbarch method. */
  319. static int
  320. nds32_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
  321. const struct reggroup *reggroup)
  322. {
  323. const char *reg_name;
  324. const char *group_name;
  325. int ret;
  326. if (reggroup == all_reggroup)
  327. return 1;
  328. /* General reggroup contains only GPRs and PC. */
  329. if (reggroup == general_reggroup)
  330. return regnum <= NDS32_PC_REGNUM;
  331. if (reggroup == float_reggroup || reggroup == save_reggroup
  332. || reggroup == restore_reggroup)
  333. {
  334. ret = tdesc_register_in_reggroup_p (gdbarch, regnum, reggroup);
  335. if (ret != -1)
  336. return ret;
  337. return default_register_reggroup_p (gdbarch, regnum, reggroup);
  338. }
  339. if (reggroup == system_reggroup)
  340. return (regnum > NDS32_PC_REGNUM)
  341. && !nds32_register_reggroup_p (gdbarch, regnum, float_reggroup);
  342. /* The NDS32 reggroup contains registers whose name is prefixed
  343. by reggroup name. */
  344. reg_name = gdbarch_register_name (gdbarch, regnum);
  345. group_name = reggroup->name ();
  346. return !strncmp (reg_name, group_name, strlen (group_name));
  347. }
  348. /* Implement the "pseudo_register_type" tdesc_arch_data method. */
  349. static struct type *
  350. nds32_pseudo_register_type (struct gdbarch *gdbarch, int regnum)
  351. {
  352. regnum -= gdbarch_num_regs (gdbarch);
  353. /* Currently, only FSRs could be defined as pseudo registers. */
  354. if (regnum < gdbarch_num_pseudo_regs (gdbarch))
  355. return arch_float_type (gdbarch, -1, "builtin_type_ieee_single",
  356. floatformats_ieee_single);
  357. warning (_("Unknown nds32 pseudo register %d."), regnum);
  358. return NULL;
  359. }
  360. /* Implement the "pseudo_register_name" tdesc_arch_data method. */
  361. static const char *
  362. nds32_pseudo_register_name (struct gdbarch *gdbarch, int regnum)
  363. {
  364. regnum -= gdbarch_num_regs (gdbarch);
  365. /* Currently, only FSRs could be defined as pseudo registers. */
  366. if (regnum < gdbarch_num_pseudo_regs (gdbarch))
  367. return nds32_fsr_register_names[regnum];
  368. warning (_("Unknown nds32 pseudo register %d."), regnum);
  369. return NULL;
  370. }
  371. /* Implement the "pseudo_register_read" gdbarch method. */
  372. static enum register_status
  373. nds32_pseudo_register_read (struct gdbarch *gdbarch,
  374. readable_regcache *regcache, int regnum,
  375. gdb_byte *buf)
  376. {
  377. nds32_gdbarch_tdep *tdep = (nds32_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  378. gdb_byte reg_buf[8];
  379. int offset, fdr_regnum;
  380. enum register_status status;
  381. /* This function is registered in nds32_gdbarch_init only after these are
  382. set. */
  383. gdb_assert (tdep->fpu_freg != -1);
  384. gdb_assert (tdep->use_pseudo_fsrs != 0);
  385. regnum -= gdbarch_num_regs (gdbarch);
  386. /* Currently, only FSRs could be defined as pseudo registers. */
  387. if (regnum < gdbarch_num_pseudo_regs (gdbarch))
  388. {
  389. /* fs0 is always the most significant half of fd0. */
  390. if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  391. offset = (regnum & 1) ? 4 : 0;
  392. else
  393. offset = (regnum & 1) ? 0 : 4;
  394. fdr_regnum = NDS32_FD0_REGNUM + (regnum >> 1);
  395. status = regcache->raw_read (fdr_regnum, reg_buf);
  396. if (status == REG_VALID)
  397. memcpy (buf, reg_buf + offset, 4);
  398. return status;
  399. }
  400. gdb_assert_not_reached ("invalid pseudo register number");
  401. }
  402. /* Implement the "pseudo_register_write" gdbarch method. */
  403. static void
  404. nds32_pseudo_register_write (struct gdbarch *gdbarch,
  405. struct regcache *regcache, int regnum,
  406. const gdb_byte *buf)
  407. {
  408. nds32_gdbarch_tdep *tdep = (nds32_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  409. gdb_byte reg_buf[8];
  410. int offset, fdr_regnum;
  411. /* This function is registered in nds32_gdbarch_init only after these are
  412. set. */
  413. gdb_assert (tdep->fpu_freg != -1);
  414. gdb_assert (tdep->use_pseudo_fsrs != 0);
  415. regnum -= gdbarch_num_regs (gdbarch);
  416. /* Currently, only FSRs could be defined as pseudo registers. */
  417. if (regnum < gdbarch_num_pseudo_regs (gdbarch))
  418. {
  419. /* fs0 is always the most significant half of fd0. */
  420. if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
  421. offset = (regnum & 1) ? 4 : 0;
  422. else
  423. offset = (regnum & 1) ? 0 : 4;
  424. fdr_regnum = NDS32_FD0_REGNUM + (regnum >> 1);
  425. regcache->raw_read (fdr_regnum, reg_buf);
  426. memcpy (reg_buf + offset, buf, 4);
  427. regcache->raw_write (fdr_regnum, reg_buf);
  428. return;
  429. }
  430. gdb_assert_not_reached ("invalid pseudo register number");
  431. }
  432. /* Helper function for NDS32 ABI. Return true if FPRs can be used
  433. to pass function arguments and return value. */
  434. static int
  435. nds32_abi_use_fpr (int elf_abi)
  436. {
  437. return elf_abi == E_NDS_ABI_V2FP_PLUS;
  438. }
  439. /* Helper function for NDS32 ABI. Return true if GPRs and stack
  440. can be used together to pass an argument. */
  441. static int
  442. nds32_abi_split (int elf_abi)
  443. {
  444. return elf_abi == E_NDS_ABI_AABI;
  445. }
  446. #define NDS32_NUM_SAVED_REGS (NDS32_LP_REGNUM + 1)
  447. struct nds32_frame_cache
  448. {
  449. /* The previous frame's inner most stack address. Used as this
  450. frame ID's stack_addr. */
  451. CORE_ADDR prev_sp;
  452. /* The frame's base, optionally used by the high-level debug info. */
  453. CORE_ADDR base;
  454. /* During prologue analysis, keep how far the SP and FP have been offset
  455. from the start of the stack frame (as defined by the previous frame's
  456. stack pointer).
  457. During epilogue analysis, keep how far the SP has been offset from the
  458. current stack pointer. */
  459. CORE_ADDR sp_offset;
  460. CORE_ADDR fp_offset;
  461. /* The address of the first instruction in this function. */
  462. CORE_ADDR pc;
  463. /* Saved registers. */
  464. CORE_ADDR saved_regs[NDS32_NUM_SAVED_REGS];
  465. };
  466. /* Allocate and initialize a frame cache. */
  467. static struct nds32_frame_cache *
  468. nds32_alloc_frame_cache (void)
  469. {
  470. struct nds32_frame_cache *cache;
  471. int i;
  472. cache = FRAME_OBSTACK_ZALLOC (struct nds32_frame_cache);
  473. /* Initialize fp_offset to check if FP is set in prologue. */
  474. cache->fp_offset = INVALID_OFFSET;
  475. /* Saved registers. We initialize these to -1 since zero is a valid
  476. offset. */
  477. for (i = 0; i < NDS32_NUM_SAVED_REGS; i++)
  478. cache->saved_regs[i] = REG_UNAVAIL;
  479. return cache;
  480. }
  481. /* Helper function for instructions used to push multiple words. */
  482. static void
  483. nds32_push_multiple_words (struct nds32_frame_cache *cache, int rb, int re,
  484. int enable4)
  485. {
  486. CORE_ADDR sp_offset = cache->sp_offset;
  487. int i;
  488. /* Check LP, GP, FP in enable4. */
  489. for (i = 1; i <= 3; i++)
  490. {
  491. if ((enable4 >> i) & 0x1)
  492. {
  493. sp_offset += 4;
  494. cache->saved_regs[NDS32_SP_REGNUM - i] = sp_offset;
  495. }
  496. }
  497. /* Skip case where re == rb == sp. */
  498. if ((rb < REG_FP) && (re < REG_FP))
  499. {
  500. for (i = re; i >= rb; i--)
  501. {
  502. sp_offset += 4;
  503. cache->saved_regs[i] = sp_offset;
  504. }
  505. }
  506. /* For sp, update the offset. */
  507. cache->sp_offset = sp_offset;
  508. }
  509. /* Analyze the instructions within the given address range. If CACHE
  510. is non-NULL, fill it in. Return the first address beyond the given
  511. address range. If CACHE is NULL, return the first address not
  512. recognized as a prologue instruction. */
  513. static CORE_ADDR
  514. nds32_analyze_prologue (struct gdbarch *gdbarch, CORE_ADDR pc,
  515. CORE_ADDR limit_pc, struct nds32_frame_cache *cache)
  516. {
  517. nds32_gdbarch_tdep *tdep = (nds32_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  518. int abi_use_fpr = nds32_abi_use_fpr (tdep->elf_abi);
  519. /* Current scanning status. */
  520. int in_prologue_bb = 0;
  521. int val_ta = 0;
  522. uint32_t insn, insn_len;
  523. for (; pc < limit_pc; pc += insn_len)
  524. {
  525. insn = read_memory_unsigned_integer (pc, 4, BFD_ENDIAN_BIG);
  526. if ((insn & 0x80000000) == 0)
  527. {
  528. /* 32-bit instruction */
  529. insn_len = 4;
  530. if (CHOP_BITS (insn, 15) == N32_TYPE2 (ADDI, REG_SP, REG_SP, 0))
  531. {
  532. /* addi $sp, $sp, imm15s */
  533. int imm15s = N32_IMM15S (insn);
  534. if (imm15s < 0)
  535. {
  536. if (cache != NULL)
  537. cache->sp_offset += -imm15s;
  538. in_prologue_bb = 1;
  539. continue;
  540. }
  541. }
  542. else if (CHOP_BITS (insn, 15) == N32_TYPE2 (ADDI, REG_FP, REG_SP, 0))
  543. {
  544. /* addi $fp, $sp, imm15s */
  545. int imm15s = N32_IMM15S (insn);
  546. if (imm15s > 0)
  547. {
  548. if (cache != NULL)
  549. cache->fp_offset = cache->sp_offset - imm15s;
  550. in_prologue_bb = 1;
  551. continue;
  552. }
  553. }
  554. else if ((insn & ~(__MASK (19) << 6)) == N32_SMW_ADM
  555. && N32_RA5 (insn) == REG_SP)
  556. {
  557. /* smw.adm Rb, [$sp], Re, enable4 */
  558. if (cache != NULL)
  559. nds32_push_multiple_words (cache, N32_RT5 (insn),
  560. N32_RB5 (insn),
  561. N32_LSMW_ENABLE4 (insn));
  562. in_prologue_bb = 1;
  563. continue;
  564. }
  565. else if (insn == N32_ALU1 (ADD, REG_SP, REG_SP, REG_TA)
  566. || insn == N32_ALU1 (ADD, REG_SP, REG_TA, REG_SP))
  567. {
  568. /* add $sp, $sp, $ta */
  569. /* add $sp, $ta, $sp */
  570. if (val_ta < 0)
  571. {
  572. if (cache != NULL)
  573. cache->sp_offset += -val_ta;
  574. in_prologue_bb = 1;
  575. continue;
  576. }
  577. }
  578. else if (CHOP_BITS (insn, 20) == N32_TYPE1 (MOVI, REG_TA, 0))
  579. {
  580. /* movi $ta, imm20s */
  581. if (cache != NULL)
  582. val_ta = N32_IMM20S (insn);
  583. continue;
  584. }
  585. else if (CHOP_BITS (insn, 20) == N32_TYPE1 (SETHI, REG_TA, 0))
  586. {
  587. /* sethi $ta, imm20u */
  588. if (cache != NULL)
  589. val_ta = N32_IMM20U (insn) << 12;
  590. continue;
  591. }
  592. else if (CHOP_BITS (insn, 15) == N32_TYPE2 (ORI, REG_TA, REG_TA, 0))
  593. {
  594. /* ori $ta, $ta, imm15u */
  595. if (cache != NULL)
  596. val_ta |= N32_IMM15U (insn);
  597. continue;
  598. }
  599. else if (CHOP_BITS (insn, 15) == N32_TYPE2 (ADDI, REG_TA, REG_TA, 0))
  600. {
  601. /* addi $ta, $ta, imm15s */
  602. if (cache != NULL)
  603. val_ta += N32_IMM15S (insn);
  604. continue;
  605. }
  606. if (insn == N32_ALU1 (ADD, REG_GP, REG_TA, REG_GP)
  607. || insn == N32_ALU1 (ADD, REG_GP, REG_GP, REG_TA))
  608. {
  609. /* add $gp, $ta, $gp */
  610. /* add $gp, $gp, $ta */
  611. in_prologue_bb = 1;
  612. continue;
  613. }
  614. else if (CHOP_BITS (insn, 20) == N32_TYPE1 (MOVI, REG_GP, 0))
  615. {
  616. /* movi $gp, imm20s */
  617. in_prologue_bb = 1;
  618. continue;
  619. }
  620. else if (CHOP_BITS (insn, 20) == N32_TYPE1 (SETHI, REG_GP, 0))
  621. {
  622. /* sethi $gp, imm20u */
  623. in_prologue_bb = 1;
  624. continue;
  625. }
  626. else if (CHOP_BITS (insn, 15) == N32_TYPE2 (ORI, REG_GP, REG_GP, 0))
  627. {
  628. /* ori $gp, $gp, imm15u */
  629. in_prologue_bb = 1;
  630. continue;
  631. }
  632. else
  633. {
  634. /* Jump/Branch insns never appear in prologue basic block.
  635. The loop can be escaped early when these insns are met. */
  636. if (in_prologue_bb == 1)
  637. {
  638. int op = N32_OP6 (insn);
  639. if (op == N32_OP6_JI
  640. || op == N32_OP6_JREG
  641. || op == N32_OP6_BR1
  642. || op == N32_OP6_BR2
  643. || op == N32_OP6_BR3)
  644. break;
  645. }
  646. }
  647. if (abi_use_fpr && N32_OP6 (insn) == N32_OP6_SDC
  648. && __GF (insn, 12, 3) == 0)
  649. {
  650. /* For FPU insns, CP (bit [13:14]) should be CP0, and only
  651. normal form (bit [12] == 0) is used. */
  652. /* fsdi FDt, [$sp + (imm12s << 2)] */
  653. if (N32_RA5 (insn) == REG_SP)
  654. continue;
  655. }
  656. /* The optimizer might shove anything into the prologue, if
  657. we build up cache (cache != NULL) from analyzing prologue,
  658. we just skip what we don't recognize and analyze further to
  659. make cache as complete as possible. However, if we skip
  660. prologue, we'll stop immediately on unrecognized
  661. instruction. */
  662. if (cache == NULL)
  663. break;
  664. }
  665. else
  666. {
  667. /* 16-bit instruction */
  668. insn_len = 2;
  669. insn >>= 16;
  670. if (CHOP_BITS (insn, 10) == N16_TYPE10 (ADDI10S, 0))
  671. {
  672. /* addi10s.sp */
  673. int imm10s = N16_IMM10S (insn);
  674. if (imm10s < 0)
  675. {
  676. if (cache != NULL)
  677. cache->sp_offset += -imm10s;
  678. in_prologue_bb = 1;
  679. continue;
  680. }
  681. }
  682. else if (__GF (insn, 7, 8) == N16_T25_PUSH25)
  683. {
  684. /* push25 */
  685. if (cache != NULL)
  686. {
  687. int imm8u = (insn & 0x1f) << 3;
  688. int re = (insn >> 5) & 0x3;
  689. const int reg_map[] = { 6, 8, 10, 14 };
  690. /* Operation 1 -- smw.adm R6, [$sp], Re, #0xe */
  691. nds32_push_multiple_words (cache, 6, reg_map[re], 0xe);
  692. /* Operation 2 -- sp = sp - (imm5u << 3) */
  693. cache->sp_offset += imm8u;
  694. }
  695. in_prologue_bb = 1;
  696. continue;
  697. }
  698. else if (insn == N16_TYPE5 (ADD5PC, REG_GP))
  699. {
  700. /* add5.pc $gp */
  701. in_prologue_bb = 1;
  702. continue;
  703. }
  704. else if (CHOP_BITS (insn, 5) == N16_TYPE55 (MOVI55, REG_GP, 0))
  705. {
  706. /* movi55 $gp, imm5s */
  707. in_prologue_bb = 1;
  708. continue;
  709. }
  710. else
  711. {
  712. /* Jump/Branch insns never appear in prologue basic block.
  713. The loop can be escaped early when these insns are met. */
  714. if (in_prologue_bb == 1)
  715. {
  716. uint32_t insn5 = CHOP_BITS (insn, 5);
  717. uint32_t insn8 = CHOP_BITS (insn, 8);
  718. uint32_t insn38 = CHOP_BITS (insn, 11);
  719. if (insn5 == N16_TYPE5 (JR5, 0)
  720. || insn5 == N16_TYPE5 (JRAL5, 0)
  721. || insn5 == N16_TYPE5 (RET5, 0)
  722. || insn8 == N16_TYPE8 (J8, 0)
  723. || insn8 == N16_TYPE8 (BEQZS8, 0)
  724. || insn8 == N16_TYPE8 (BNEZS8, 0)
  725. || insn38 == N16_TYPE38 (BEQZ38, 0, 0)
  726. || insn38 == N16_TYPE38 (BNEZ38, 0, 0)
  727. || insn38 == N16_TYPE38 (BEQS38, 0, 0)
  728. || insn38 == N16_TYPE38 (BNES38, 0, 0))
  729. break;
  730. }
  731. }
  732. /* The optimizer might shove anything into the prologue, if
  733. we build up cache (cache != NULL) from analyzing prologue,
  734. we just skip what we don't recognize and analyze further to
  735. make cache as complete as possible. However, if we skip
  736. prologue, we'll stop immediately on unrecognized
  737. instruction. */
  738. if (cache == NULL)
  739. break;
  740. }
  741. }
  742. return pc;
  743. }
  744. /* Implement the "skip_prologue" gdbarch method.
  745. Find the end of function prologue. */
  746. static CORE_ADDR
  747. nds32_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  748. {
  749. CORE_ADDR func_addr, limit_pc;
  750. /* See if we can determine the end of the prologue via the symbol table.
  751. If so, then return either PC, or the PC after the prologue, whichever
  752. is greater. */
  753. if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
  754. {
  755. CORE_ADDR post_prologue_pc
  756. = skip_prologue_using_sal (gdbarch, func_addr);
  757. if (post_prologue_pc != 0)
  758. return std::max (pc, post_prologue_pc);
  759. }
  760. /* Can't determine prologue from the symbol table, need to examine
  761. instructions. */
  762. /* Find an upper limit on the function prologue using the debug
  763. information. If the debug information could not be used to provide
  764. that bound, then use an arbitrary large number as the upper bound. */
  765. limit_pc = skip_prologue_using_sal (gdbarch, pc);
  766. if (limit_pc == 0)
  767. limit_pc = pc + 128; /* Magic. */
  768. /* Find the end of prologue. */
  769. return nds32_analyze_prologue (gdbarch, pc, limit_pc, NULL);
  770. }
  771. /* Allocate and fill in *THIS_CACHE with information about the prologue of
  772. *THIS_FRAME. Do not do this if *THIS_CACHE was already allocated. Return
  773. a pointer to the current nds32_frame_cache in *THIS_CACHE. */
  774. static struct nds32_frame_cache *
  775. nds32_frame_cache (struct frame_info *this_frame, void **this_cache)
  776. {
  777. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  778. struct nds32_frame_cache *cache;
  779. CORE_ADDR current_pc;
  780. ULONGEST prev_sp;
  781. ULONGEST this_base;
  782. int i;
  783. if (*this_cache)
  784. return (struct nds32_frame_cache *) *this_cache;
  785. cache = nds32_alloc_frame_cache ();
  786. *this_cache = cache;
  787. cache->pc = get_frame_func (this_frame);
  788. current_pc = get_frame_pc (this_frame);
  789. nds32_analyze_prologue (gdbarch, cache->pc, current_pc, cache);
  790. /* Compute the previous frame's stack pointer (which is also the
  791. frame's ID's stack address), and this frame's base pointer. */
  792. if (cache->fp_offset != INVALID_OFFSET)
  793. {
  794. /* FP is set in prologue, so it can be used to calculate other info. */
  795. this_base = get_frame_register_unsigned (this_frame, NDS32_FP_REGNUM);
  796. prev_sp = this_base + cache->fp_offset;
  797. }
  798. else
  799. {
  800. this_base = get_frame_register_unsigned (this_frame, NDS32_SP_REGNUM);
  801. prev_sp = this_base + cache->sp_offset;
  802. }
  803. cache->prev_sp = prev_sp;
  804. cache->base = this_base;
  805. /* Adjust all the saved registers such that they contain addresses
  806. instead of offsets. */
  807. for (i = 0; i < NDS32_NUM_SAVED_REGS; i++)
  808. if (cache->saved_regs[i] != REG_UNAVAIL)
  809. cache->saved_regs[i] = cache->prev_sp - cache->saved_regs[i];
  810. return cache;
  811. }
  812. /* Implement the "this_id" frame_unwind method.
  813. Our frame ID for a normal frame is the current function's starting
  814. PC and the caller's SP when we were called. */
  815. static void
  816. nds32_frame_this_id (struct frame_info *this_frame,
  817. void **this_cache, struct frame_id *this_id)
  818. {
  819. struct nds32_frame_cache *cache = nds32_frame_cache (this_frame, this_cache);
  820. /* This marks the outermost frame. */
  821. if (cache->prev_sp == 0)
  822. return;
  823. *this_id = frame_id_build (cache->prev_sp, cache->pc);
  824. }
  825. /* Implement the "prev_register" frame_unwind method. */
  826. static struct value *
  827. nds32_frame_prev_register (struct frame_info *this_frame, void **this_cache,
  828. int regnum)
  829. {
  830. struct nds32_frame_cache *cache = nds32_frame_cache (this_frame, this_cache);
  831. if (regnum == NDS32_SP_REGNUM)
  832. return frame_unwind_got_constant (this_frame, regnum, cache->prev_sp);
  833. /* The PC of the previous frame is stored in the LP register of
  834. the current frame. */
  835. if (regnum == NDS32_PC_REGNUM)
  836. regnum = NDS32_LP_REGNUM;
  837. if (regnum < NDS32_NUM_SAVED_REGS && cache->saved_regs[regnum] != REG_UNAVAIL)
  838. return frame_unwind_got_memory (this_frame, regnum,
  839. cache->saved_regs[regnum]);
  840. return frame_unwind_got_register (this_frame, regnum, regnum);
  841. }
  842. static const struct frame_unwind nds32_frame_unwind =
  843. {
  844. "nds32 prologue",
  845. NORMAL_FRAME,
  846. default_frame_unwind_stop_reason,
  847. nds32_frame_this_id,
  848. nds32_frame_prev_register,
  849. NULL,
  850. default_frame_sniffer,
  851. };
  852. /* Return the frame base address of *THIS_FRAME. */
  853. static CORE_ADDR
  854. nds32_frame_base_address (struct frame_info *this_frame, void **this_cache)
  855. {
  856. struct nds32_frame_cache *cache = nds32_frame_cache (this_frame, this_cache);
  857. return cache->base;
  858. }
  859. static const struct frame_base nds32_frame_base =
  860. {
  861. &nds32_frame_unwind,
  862. nds32_frame_base_address,
  863. nds32_frame_base_address,
  864. nds32_frame_base_address
  865. };
  866. /* Helper function for instructions used to pop multiple words. */
  867. static void
  868. nds32_pop_multiple_words (struct nds32_frame_cache *cache, int rb, int re,
  869. int enable4)
  870. {
  871. CORE_ADDR sp_offset = cache->sp_offset;
  872. int i;
  873. /* Skip case where re == rb == sp. */
  874. if ((rb < REG_FP) && (re < REG_FP))
  875. {
  876. for (i = rb; i <= re; i++)
  877. {
  878. cache->saved_regs[i] = sp_offset;
  879. sp_offset += 4;
  880. }
  881. }
  882. /* Check FP, GP, LP in enable4. */
  883. for (i = 3; i >= 1; i--)
  884. {
  885. if ((enable4 >> i) & 0x1)
  886. {
  887. cache->saved_regs[NDS32_SP_REGNUM - i] = sp_offset;
  888. sp_offset += 4;
  889. }
  890. }
  891. /* For sp, update the offset. */
  892. cache->sp_offset = sp_offset;
  893. }
  894. /* The instruction sequences in NDS32 epilogue are
  895. INSN_RESET_SP (optional)
  896. (If exists, this must be the first instruction in epilogue
  897. and the stack has not been destroyed.).
  898. INSN_RECOVER (optional).
  899. INSN_RETURN/INSN_RECOVER_RETURN (required). */
  900. /* Helper function for analyzing the given 32-bit INSN. If CACHE is non-NULL,
  901. the necessary information will be recorded. */
  902. static inline int
  903. nds32_analyze_epilogue_insn32 (int abi_use_fpr, uint32_t insn,
  904. struct nds32_frame_cache *cache)
  905. {
  906. if (CHOP_BITS (insn, 15) == N32_TYPE2 (ADDI, REG_SP, REG_SP, 0)
  907. && N32_IMM15S (insn) > 0)
  908. /* addi $sp, $sp, imm15s */
  909. return INSN_RESET_SP;
  910. else if (CHOP_BITS (insn, 15) == N32_TYPE2 (ADDI, REG_SP, REG_FP, 0)
  911. && N32_IMM15S (insn) < 0)
  912. /* addi $sp, $fp, imm15s */
  913. return INSN_RESET_SP;
  914. else if ((insn & ~(__MASK (19) << 6)) == N32_LMW_BIM
  915. && N32_RA5 (insn) == REG_SP)
  916. {
  917. /* lmw.bim Rb, [$sp], Re, enable4 */
  918. if (cache != NULL)
  919. nds32_pop_multiple_words (cache, N32_RT5 (insn),
  920. N32_RB5 (insn), N32_LSMW_ENABLE4 (insn));
  921. return INSN_RECOVER;
  922. }
  923. else if (insn == N32_JREG (JR, 0, REG_LP, 0, 1))
  924. /* ret $lp */
  925. return INSN_RETURN;
  926. else if (insn == N32_ALU1 (ADD, REG_SP, REG_SP, REG_TA)
  927. || insn == N32_ALU1 (ADD, REG_SP, REG_TA, REG_SP))
  928. /* add $sp, $sp, $ta */
  929. /* add $sp, $ta, $sp */
  930. return INSN_RESET_SP;
  931. else if (abi_use_fpr
  932. && (insn & ~(__MASK (5) << 20 | __MASK (13))) == N32_FLDI_SP)
  933. {
  934. if (__GF (insn, 12, 1) == 0)
  935. /* fldi FDt, [$sp + (imm12s << 2)] */
  936. return INSN_RECOVER;
  937. else
  938. {
  939. /* fldi.bi FDt, [$sp], (imm12s << 2) */
  940. int offset = N32_IMM12S (insn) << 2;
  941. if (offset == 8 || offset == 12)
  942. {
  943. if (cache != NULL)
  944. cache->sp_offset += offset;
  945. return INSN_RECOVER;
  946. }
  947. }
  948. }
  949. return INSN_NORMAL;
  950. }
  951. /* Helper function for analyzing the given 16-bit INSN. If CACHE is non-NULL,
  952. the necessary information will be recorded. */
  953. static inline int
  954. nds32_analyze_epilogue_insn16 (uint32_t insn, struct nds32_frame_cache *cache)
  955. {
  956. if (insn == N16_TYPE5 (RET5, REG_LP))
  957. /* ret5 $lp */
  958. return INSN_RETURN;
  959. else if (CHOP_BITS (insn, 10) == N16_TYPE10 (ADDI10S, 0))
  960. {
  961. /* addi10s.sp */
  962. int imm10s = N16_IMM10S (insn);
  963. if (imm10s > 0)
  964. {
  965. if (cache != NULL)
  966. cache->sp_offset += imm10s;
  967. return INSN_RECOVER;
  968. }
  969. }
  970. else if (__GF (insn, 7, 8) == N16_T25_POP25)
  971. {
  972. /* pop25 */
  973. if (cache != NULL)
  974. {
  975. int imm8u = (insn & 0x1f) << 3;
  976. int re = (insn >> 5) & 0x3;
  977. const int reg_map[] = { 6, 8, 10, 14 };
  978. /* Operation 1 -- sp = sp + (imm5u << 3) */
  979. cache->sp_offset += imm8u;
  980. /* Operation 2 -- lmw.bim R6, [$sp], Re, #0xe */
  981. nds32_pop_multiple_words (cache, 6, reg_map[re], 0xe);
  982. }
  983. /* Operation 3 -- ret $lp */
  984. return INSN_RECOVER_RETURN;
  985. }
  986. return INSN_NORMAL;
  987. }
  988. /* Analyze a reasonable amount of instructions from the given PC to find
  989. the instruction used to return to the caller. Return 1 if the 'return'
  990. instruction could be found, 0 otherwise.
  991. If CACHE is non-NULL, fill it in. */
  992. static int
  993. nds32_analyze_epilogue (struct gdbarch *gdbarch, CORE_ADDR pc,
  994. struct nds32_frame_cache *cache)
  995. {
  996. nds32_gdbarch_tdep *tdep = (nds32_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  997. int abi_use_fpr = nds32_abi_use_fpr (tdep->elf_abi);
  998. CORE_ADDR limit_pc;
  999. uint32_t insn, insn_len;
  1000. int insn_type = INSN_NORMAL;
  1001. if (abi_use_fpr)
  1002. limit_pc = pc + 48;
  1003. else
  1004. limit_pc = pc + 16;
  1005. for (; pc < limit_pc; pc += insn_len)
  1006. {
  1007. insn = read_memory_unsigned_integer (pc, 4, BFD_ENDIAN_BIG);
  1008. if ((insn & 0x80000000) == 0)
  1009. {
  1010. /* 32-bit instruction */
  1011. insn_len = 4;
  1012. insn_type = nds32_analyze_epilogue_insn32 (abi_use_fpr, insn, cache);
  1013. if (insn_type == INSN_RETURN)
  1014. return 1;
  1015. else if (insn_type == INSN_RECOVER)
  1016. continue;
  1017. }
  1018. else
  1019. {
  1020. /* 16-bit instruction */
  1021. insn_len = 2;
  1022. insn >>= 16;
  1023. insn_type = nds32_analyze_epilogue_insn16 (insn, cache);
  1024. if (insn_type == INSN_RETURN || insn_type == INSN_RECOVER_RETURN)
  1025. return 1;
  1026. else if (insn_type == INSN_RECOVER)
  1027. continue;
  1028. }
  1029. /* Stop the scan if this is an unexpected instruction. */
  1030. break;
  1031. }
  1032. return 0;
  1033. }
  1034. /* Implement the "stack_frame_destroyed_p" gdbarch method. */
  1035. static int
  1036. nds32_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR addr)
  1037. {
  1038. nds32_gdbarch_tdep *tdep = (nds32_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1039. int abi_use_fpr = nds32_abi_use_fpr (tdep->elf_abi);
  1040. int insn_type = INSN_NORMAL;
  1041. int ret_found = 0;
  1042. uint32_t insn;
  1043. insn = read_memory_unsigned_integer (addr, 4, BFD_ENDIAN_BIG);
  1044. if ((insn & 0x80000000) == 0)
  1045. {
  1046. /* 32-bit instruction */
  1047. insn_type = nds32_analyze_epilogue_insn32 (abi_use_fpr, insn, NULL);
  1048. }
  1049. else
  1050. {
  1051. /* 16-bit instruction */
  1052. insn >>= 16;
  1053. insn_type = nds32_analyze_epilogue_insn16 (insn, NULL);
  1054. }
  1055. if (insn_type == INSN_NORMAL || insn_type == INSN_RESET_SP)
  1056. return 0;
  1057. /* Search the required 'return' instruction within the following reasonable
  1058. instructions. */
  1059. ret_found = nds32_analyze_epilogue (gdbarch, addr, NULL);
  1060. if (ret_found == 0)
  1061. return 0;
  1062. /* Scan backwards to make sure that the last instruction has adjusted
  1063. stack. Both a 16-bit and a 32-bit instruction will be tried. This is
  1064. just a heuristic, so the false positives will be acceptable. */
  1065. insn = read_memory_unsigned_integer (addr - 2, 4, BFD_ENDIAN_BIG);
  1066. /* Only 16-bit instructions are possible at addr - 2. */
  1067. if ((insn & 0x80000000) != 0)
  1068. {
  1069. /* This may be a 16-bit instruction or part of a 32-bit instruction. */
  1070. insn_type = nds32_analyze_epilogue_insn16 (insn >> 16, NULL);
  1071. if (insn_type == INSN_RECOVER)
  1072. return 1;
  1073. }
  1074. insn = read_memory_unsigned_integer (addr - 4, 4, BFD_ENDIAN_BIG);
  1075. /* If this is a 16-bit instruction at addr - 4, then there must be another
  1076. 16-bit instruction at addr - 2, so only 32-bit instructions need to
  1077. be analyzed here. */
  1078. if ((insn & 0x80000000) == 0)
  1079. {
  1080. /* This may be a 32-bit instruction or part of a 32-bit instruction. */
  1081. insn_type = nds32_analyze_epilogue_insn32 (abi_use_fpr, insn, NULL);
  1082. if (insn_type == INSN_RECOVER || insn_type == INSN_RESET_SP)
  1083. return 1;
  1084. }
  1085. return 0;
  1086. }
  1087. /* Implement the "sniffer" frame_unwind method. */
  1088. static int
  1089. nds32_epilogue_frame_sniffer (const struct frame_unwind *self,
  1090. struct frame_info *this_frame, void **this_cache)
  1091. {
  1092. if (frame_relative_level (this_frame) == 0)
  1093. return nds32_stack_frame_destroyed_p (get_frame_arch (this_frame),
  1094. get_frame_pc (this_frame));
  1095. else
  1096. return 0;
  1097. }
  1098. /* Allocate and fill in *THIS_CACHE with information needed to unwind
  1099. *THIS_FRAME within epilogue. Do not do this if *THIS_CACHE was already
  1100. allocated. Return a pointer to the current nds32_frame_cache in
  1101. *THIS_CACHE. */
  1102. static struct nds32_frame_cache *
  1103. nds32_epilogue_frame_cache (struct frame_info *this_frame, void **this_cache)
  1104. {
  1105. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1106. struct nds32_frame_cache *cache;
  1107. CORE_ADDR current_pc, current_sp;
  1108. int i;
  1109. if (*this_cache)
  1110. return (struct nds32_frame_cache *) *this_cache;
  1111. cache = nds32_alloc_frame_cache ();
  1112. *this_cache = cache;
  1113. cache->pc = get_frame_func (this_frame);
  1114. current_pc = get_frame_pc (this_frame);
  1115. nds32_analyze_epilogue (gdbarch, current_pc, cache);
  1116. current_sp = get_frame_register_unsigned (this_frame, NDS32_SP_REGNUM);
  1117. cache->prev_sp = current_sp + cache->sp_offset;
  1118. /* Adjust all the saved registers such that they contain addresses
  1119. instead of offsets. */
  1120. for (i = 0; i < NDS32_NUM_SAVED_REGS; i++)
  1121. if (cache->saved_regs[i] != REG_UNAVAIL)
  1122. cache->saved_regs[i] = current_sp + cache->saved_regs[i];
  1123. return cache;
  1124. }
  1125. /* Implement the "this_id" frame_unwind method. */
  1126. static void
  1127. nds32_epilogue_frame_this_id (struct frame_info *this_frame,
  1128. void **this_cache, struct frame_id *this_id)
  1129. {
  1130. struct nds32_frame_cache *cache
  1131. = nds32_epilogue_frame_cache (this_frame, this_cache);
  1132. /* This marks the outermost frame. */
  1133. if (cache->prev_sp == 0)
  1134. return;
  1135. *this_id = frame_id_build (cache->prev_sp, cache->pc);
  1136. }
  1137. /* Implement the "prev_register" frame_unwind method. */
  1138. static struct value *
  1139. nds32_epilogue_frame_prev_register (struct frame_info *this_frame,
  1140. void **this_cache, int regnum)
  1141. {
  1142. struct nds32_frame_cache *cache
  1143. = nds32_epilogue_frame_cache (this_frame, this_cache);
  1144. if (regnum == NDS32_SP_REGNUM)
  1145. return frame_unwind_got_constant (this_frame, regnum, cache->prev_sp);
  1146. /* The PC of the previous frame is stored in the LP register of
  1147. the current frame. */
  1148. if (regnum == NDS32_PC_REGNUM)
  1149. regnum = NDS32_LP_REGNUM;
  1150. if (regnum < NDS32_NUM_SAVED_REGS && cache->saved_regs[regnum] != REG_UNAVAIL)
  1151. return frame_unwind_got_memory (this_frame, regnum,
  1152. cache->saved_regs[regnum]);
  1153. return frame_unwind_got_register (this_frame, regnum, regnum);
  1154. }
  1155. static const struct frame_unwind nds32_epilogue_frame_unwind =
  1156. {
  1157. "nds32 epilogue",
  1158. NORMAL_FRAME,
  1159. default_frame_unwind_stop_reason,
  1160. nds32_epilogue_frame_this_id,
  1161. nds32_epilogue_frame_prev_register,
  1162. NULL,
  1163. nds32_epilogue_frame_sniffer
  1164. };
  1165. /* Floating type and struct type that has only one floating type member
  1166. can pass value using FPU registers (when FPU ABI is used). */
  1167. static int
  1168. nds32_check_calling_use_fpr (struct type *type)
  1169. {
  1170. struct type *t;
  1171. enum type_code typecode;
  1172. t = type;
  1173. while (1)
  1174. {
  1175. t = check_typedef (t);
  1176. typecode = t->code ();
  1177. if (typecode != TYPE_CODE_STRUCT)
  1178. break;
  1179. else if (t->num_fields () != 1)
  1180. return 0;
  1181. else
  1182. t = t->field (0).type ();
  1183. }
  1184. return typecode == TYPE_CODE_FLT;
  1185. }
  1186. /* Implement the "push_dummy_call" gdbarch method. */
  1187. static CORE_ADDR
  1188. nds32_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  1189. struct regcache *regcache, CORE_ADDR bp_addr,
  1190. int nargs, struct value **args, CORE_ADDR sp,
  1191. function_call_return_method return_method,
  1192. CORE_ADDR struct_addr)
  1193. {
  1194. const int REND = 6; /* End for register offset. */
  1195. int goff = 0; /* Current gpr offset for argument. */
  1196. int foff = 0; /* Current fpr offset for argument. */
  1197. int soff = 0; /* Current stack offset for argument. */
  1198. int i;
  1199. ULONGEST regval;
  1200. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1201. nds32_gdbarch_tdep *tdep = (nds32_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1202. struct type *func_type = value_type (function);
  1203. int abi_use_fpr = nds32_abi_use_fpr (tdep->elf_abi);
  1204. int abi_split = nds32_abi_split (tdep->elf_abi);
  1205. /* Set the return address. For the NDS32, the return breakpoint is
  1206. always at BP_ADDR. */
  1207. regcache_cooked_write_unsigned (regcache, NDS32_LP_REGNUM, bp_addr);
  1208. /* If STRUCT_RETURN is true, then the struct return address (in
  1209. STRUCT_ADDR) will consume the first argument-passing register.
  1210. Both adjust the register count and store that value. */
  1211. if (return_method == return_method_struct)
  1212. {
  1213. regcache_cooked_write_unsigned (regcache, NDS32_R0_REGNUM, struct_addr);
  1214. goff++;
  1215. }
  1216. /* Now make sure there's space on the stack */
  1217. for (i = 0; i < nargs; i++)
  1218. {
  1219. struct type *type = value_type (args[i]);
  1220. int align = type_align (type);
  1221. /* If align is zero, it may be an empty struct.
  1222. Just ignore the argument of empty struct. */
  1223. if (align == 0)
  1224. continue;
  1225. sp -= TYPE_LENGTH (type);
  1226. sp = align_down (sp, align);
  1227. }
  1228. /* Stack must be 8-byte aligned. */
  1229. sp = align_down (sp, 8);
  1230. soff = 0;
  1231. for (i = 0; i < nargs; i++)
  1232. {
  1233. const gdb_byte *val;
  1234. int align, len;
  1235. struct type *type;
  1236. int calling_use_fpr;
  1237. int use_fpr = 0;
  1238. type = value_type (args[i]);
  1239. calling_use_fpr = nds32_check_calling_use_fpr (type);
  1240. len = TYPE_LENGTH (type);
  1241. align = type_align (type);
  1242. val = value_contents (args[i]).data ();
  1243. /* The size of a composite type larger than 4 bytes will be rounded
  1244. up to the nearest multiple of 4. */
  1245. if (len > 4)
  1246. len = align_up (len, 4);
  1247. /* Variadic functions are handled differently between AABI and ABI2FP+.
  1248. For AABI, the caller pushes arguments in registers, callee stores
  1249. unnamed arguments in stack, and then va_arg fetch arguments in stack.
  1250. Therefore, we don't have to handle variadic functions specially.
  1251. For ABI2FP+, the caller pushes only named arguments in registers
  1252. and pushes all unnamed arguments in stack. */
  1253. if (abi_use_fpr && func_type->has_varargs ()
  1254. && i >= func_type->num_fields ())
  1255. goto use_stack;
  1256. /* Try to use FPRs to pass arguments only when
  1257. 1. The program is built using toolchain with FPU support.
  1258. 2. The type of this argument can use FPR to pass value. */
  1259. use_fpr = abi_use_fpr && calling_use_fpr;
  1260. if (use_fpr)
  1261. {
  1262. if (tdep->fpu_freg == -1)
  1263. goto error_no_fpr;
  1264. /* Adjust alignment. */
  1265. if ((align >> 2) > 0)
  1266. foff = align_up (foff, align >> 2);
  1267. if (foff < REND)
  1268. {
  1269. switch (len)
  1270. {
  1271. case 4:
  1272. regcache->cooked_write (tdep->fs0_regnum + foff, val);
  1273. foff++;
  1274. break;
  1275. case 8:
  1276. regcache->cooked_write (NDS32_FD0_REGNUM + (foff >> 1), val);
  1277. foff += 2;
  1278. break;
  1279. default:
  1280. /* Long double? */
  1281. internal_error (__FILE__, __LINE__,
  1282. "Do not know how to handle %d-byte double.\n",
  1283. len);
  1284. break;
  1285. }
  1286. continue;
  1287. }
  1288. }
  1289. else
  1290. {
  1291. /*
  1292. When passing arguments using GPRs,
  1293. * A composite type not larger than 4 bytes is passed in $rN.
  1294. The format is as if the value is loaded with load instruction
  1295. of corresponding size (e.g., LB, LH, LW).
  1296. For example,
  1297. r0
  1298. 31 0
  1299. LITTLE: [x x b a]
  1300. BIG: [x x a b]
  1301. * Otherwise, a composite type is passed in consecutive registers.
  1302. The size is rounded up to the nearest multiple of 4.
  1303. The successive registers hold the parts of the argument as if
  1304. were loaded using lmw instructions.
  1305. For example,
  1306. r0 r1
  1307. 31 0 31 0
  1308. LITTLE: [d c b a] [x x x e]
  1309. BIG: [a b c d] [e x x x]
  1310. */
  1311. /* Adjust alignment. */
  1312. if ((align >> 2) > 0)
  1313. goff = align_up (goff, align >> 2);
  1314. if (len <= (REND - goff) * 4)
  1315. {
  1316. /* This argument can be passed wholly via GPRs. */
  1317. while (len > 0)
  1318. {
  1319. regval = extract_unsigned_integer (val, (len > 4) ? 4 : len,
  1320. byte_order);
  1321. regcache_cooked_write_unsigned (regcache,
  1322. NDS32_R0_REGNUM + goff,
  1323. regval);
  1324. len -= 4;
  1325. val += 4;
  1326. goff++;
  1327. }
  1328. continue;
  1329. }
  1330. else if (abi_split)
  1331. {
  1332. /* Some parts of this argument can be passed via GPRs. */
  1333. while (goff < REND)
  1334. {
  1335. regval = extract_unsigned_integer (val, (len > 4) ? 4 : len,
  1336. byte_order);
  1337. regcache_cooked_write_unsigned (regcache,
  1338. NDS32_R0_REGNUM + goff,
  1339. regval);
  1340. len -= 4;
  1341. val += 4;
  1342. goff++;
  1343. }
  1344. }
  1345. }
  1346. use_stack:
  1347. /*
  1348. When pushing (split parts of) an argument into stack,
  1349. * A composite type not larger than 4 bytes is copied to different
  1350. base address.
  1351. In little-endian, the first byte of this argument is aligned
  1352. at the low address of the next free word.
  1353. In big-endian, the last byte of this argument is aligned
  1354. at the high address of the next free word.
  1355. For example,
  1356. sp [ - ] [ c ] hi
  1357. [ c ] [ b ]
  1358. [ b ] [ a ]
  1359. [ a ] [ - ] lo
  1360. LITTLE BIG
  1361. */
  1362. /* Adjust alignment. */
  1363. soff = align_up (soff, align);
  1364. while (len > 0)
  1365. {
  1366. int rlen = (len > 4) ? 4 : len;
  1367. if (byte_order == BFD_ENDIAN_BIG)
  1368. write_memory (sp + soff + 4 - rlen, val, rlen);
  1369. else
  1370. write_memory (sp + soff, val, rlen);
  1371. len -= 4;
  1372. val += 4;
  1373. soff += 4;
  1374. }
  1375. }
  1376. /* Finally, update the SP register. */
  1377. regcache_cooked_write_unsigned (regcache, NDS32_SP_REGNUM, sp);
  1378. return sp;
  1379. error_no_fpr:
  1380. /* If use_fpr, but no floating-point register exists,
  1381. then it is an error. */
  1382. error (_("Fail to call. FPU registers are required."));
  1383. }
  1384. /* Read, for architecture GDBARCH, a function return value of TYPE
  1385. from REGCACHE, and copy that into VALBUF. */
  1386. static void
  1387. nds32_extract_return_value (struct gdbarch *gdbarch, struct type *type,
  1388. struct regcache *regcache, gdb_byte *valbuf)
  1389. {
  1390. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1391. nds32_gdbarch_tdep *tdep = (nds32_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1392. int abi_use_fpr = nds32_abi_use_fpr (tdep->elf_abi);
  1393. int calling_use_fpr;
  1394. int len;
  1395. calling_use_fpr = nds32_check_calling_use_fpr (type);
  1396. len = TYPE_LENGTH (type);
  1397. if (abi_use_fpr && calling_use_fpr)
  1398. {
  1399. if (len == 4)
  1400. regcache->cooked_read (tdep->fs0_regnum, valbuf);
  1401. else if (len == 8)
  1402. regcache->cooked_read (NDS32_FD0_REGNUM, valbuf);
  1403. else
  1404. internal_error (__FILE__, __LINE__,
  1405. _("Cannot extract return value of %d bytes "
  1406. "long floating-point."), len);
  1407. }
  1408. else
  1409. {
  1410. /*
  1411. When returning result,
  1412. * A composite type not larger than 4 bytes is returned in $r0.
  1413. The format is as if the result is loaded with load instruction
  1414. of corresponding size (e.g., LB, LH, LW).
  1415. For example,
  1416. r0
  1417. 31 0
  1418. LITTLE: [x x b a]
  1419. BIG: [x x a b]
  1420. * Otherwise, a composite type not larger than 8 bytes is returned
  1421. in $r0 and $r1.
  1422. In little-endian, the first word is loaded in $r0.
  1423. In big-endian, the last word is loaded in $r1.
  1424. For example,
  1425. r0 r1
  1426. 31 0 31 0
  1427. LITTLE: [d c b a] [x x x e]
  1428. BIG: [x x x a] [b c d e]
  1429. */
  1430. ULONGEST tmp;
  1431. if (len < 4)
  1432. {
  1433. /* By using store_unsigned_integer we avoid having to do
  1434. anything special for small big-endian values. */
  1435. regcache_cooked_read_unsigned (regcache, NDS32_R0_REGNUM, &tmp);
  1436. store_unsigned_integer (valbuf, len, byte_order, tmp);
  1437. }
  1438. else if (len == 4)
  1439. {
  1440. regcache->cooked_read (NDS32_R0_REGNUM, valbuf);
  1441. }
  1442. else if (len < 8)
  1443. {
  1444. int len1, len2;
  1445. len1 = byte_order == BFD_ENDIAN_BIG ? len - 4 : 4;
  1446. len2 = len - len1;
  1447. regcache_cooked_read_unsigned (regcache, NDS32_R0_REGNUM, &tmp);
  1448. store_unsigned_integer (valbuf, len1, byte_order, tmp);
  1449. regcache_cooked_read_unsigned (regcache, NDS32_R0_REGNUM + 1, &tmp);
  1450. store_unsigned_integer (valbuf + len1, len2, byte_order, tmp);
  1451. }
  1452. else
  1453. {
  1454. regcache->cooked_read (NDS32_R0_REGNUM, valbuf);
  1455. regcache->cooked_read (NDS32_R0_REGNUM + 1, valbuf + 4);
  1456. }
  1457. }
  1458. }
  1459. /* Write, for architecture GDBARCH, a function return value of TYPE
  1460. from VALBUF into REGCACHE. */
  1461. static void
  1462. nds32_store_return_value (struct gdbarch *gdbarch, struct type *type,
  1463. struct regcache *regcache, const gdb_byte *valbuf)
  1464. {
  1465. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1466. nds32_gdbarch_tdep *tdep = (nds32_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1467. int abi_use_fpr = nds32_abi_use_fpr (tdep->elf_abi);
  1468. int calling_use_fpr;
  1469. int len;
  1470. calling_use_fpr = nds32_check_calling_use_fpr (type);
  1471. len = TYPE_LENGTH (type);
  1472. if (abi_use_fpr && calling_use_fpr)
  1473. {
  1474. if (len == 4)
  1475. regcache->cooked_write (tdep->fs0_regnum, valbuf);
  1476. else if (len == 8)
  1477. regcache->cooked_write (NDS32_FD0_REGNUM, valbuf);
  1478. else
  1479. internal_error (__FILE__, __LINE__,
  1480. _("Cannot store return value of %d bytes "
  1481. "long floating-point."), len);
  1482. }
  1483. else
  1484. {
  1485. ULONGEST regval;
  1486. if (len < 4)
  1487. {
  1488. regval = extract_unsigned_integer (valbuf, len, byte_order);
  1489. regcache_cooked_write_unsigned (regcache, NDS32_R0_REGNUM, regval);
  1490. }
  1491. else if (len == 4)
  1492. {
  1493. regcache->cooked_write (NDS32_R0_REGNUM, valbuf);
  1494. }
  1495. else if (len < 8)
  1496. {
  1497. int len1, len2;
  1498. len1 = byte_order == BFD_ENDIAN_BIG ? len - 4 : 4;
  1499. len2 = len - len1;
  1500. regval = extract_unsigned_integer (valbuf, len1, byte_order);
  1501. regcache_cooked_write_unsigned (regcache, NDS32_R0_REGNUM, regval);
  1502. regval = extract_unsigned_integer (valbuf + len1, len2, byte_order);
  1503. regcache_cooked_write_unsigned (regcache, NDS32_R0_REGNUM + 1,
  1504. regval);
  1505. }
  1506. else
  1507. {
  1508. regcache->cooked_write (NDS32_R0_REGNUM, valbuf);
  1509. regcache->cooked_write (NDS32_R0_REGNUM + 1, valbuf + 4);
  1510. }
  1511. }
  1512. }
  1513. /* Implement the "return_value" gdbarch method.
  1514. Determine, for architecture GDBARCH, how a return value of TYPE
  1515. should be returned. If it is supposed to be returned in registers,
  1516. and READBUF is non-zero, read the appropriate value from REGCACHE,
  1517. and copy it into READBUF. If WRITEBUF is non-zero, write the value
  1518. from WRITEBUF into REGCACHE. */
  1519. static enum return_value_convention
  1520. nds32_return_value (struct gdbarch *gdbarch, struct value *func_type,
  1521. struct type *type, struct regcache *regcache,
  1522. gdb_byte *readbuf, const gdb_byte *writebuf)
  1523. {
  1524. if (TYPE_LENGTH (type) > 8)
  1525. {
  1526. return RETURN_VALUE_STRUCT_CONVENTION;
  1527. }
  1528. else
  1529. {
  1530. if (readbuf != NULL)
  1531. nds32_extract_return_value (gdbarch, type, regcache, readbuf);
  1532. if (writebuf != NULL)
  1533. nds32_store_return_value (gdbarch, type, regcache, writebuf);
  1534. return RETURN_VALUE_REGISTER_CONVENTION;
  1535. }
  1536. }
  1537. /* Implement the "get_longjmp_target" gdbarch method. */
  1538. static int
  1539. nds32_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
  1540. {
  1541. gdb_byte buf[4];
  1542. CORE_ADDR jb_addr;
  1543. struct gdbarch *gdbarch = get_frame_arch (frame);
  1544. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1545. jb_addr = get_frame_register_unsigned (frame, NDS32_R0_REGNUM);
  1546. if (target_read_memory (jb_addr + 11 * 4, buf, 4))
  1547. return 0;
  1548. *pc = extract_unsigned_integer (buf, 4, byte_order);
  1549. return 1;
  1550. }
  1551. /* Validate the given TDESC, and fixed-number some registers in it.
  1552. Return 0 if the given TDESC does not contain the required feature
  1553. or not contain required registers. */
  1554. static int
  1555. nds32_validate_tdesc_p (const struct target_desc *tdesc,
  1556. struct tdesc_arch_data *tdesc_data,
  1557. int *fpu_freg, int *use_pseudo_fsrs)
  1558. {
  1559. const struct tdesc_feature *feature;
  1560. int i, valid_p;
  1561. feature = tdesc_find_feature (tdesc, "org.gnu.gdb.nds32.core");
  1562. if (feature == NULL)
  1563. return 0;
  1564. valid_p = 1;
  1565. /* Validate and fixed-number R0-R10. */
  1566. for (i = NDS32_R0_REGNUM; i <= NDS32_R0_REGNUM + 10; i++)
  1567. valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
  1568. nds32_register_names[i]);
  1569. /* Validate R15. */
  1570. valid_p &= tdesc_unnumbered_register (feature,
  1571. nds32_register_names[NDS32_TA_REGNUM]);
  1572. /* Validate and fixed-number FP, GP, LP, SP, PC. */
  1573. for (i = NDS32_FP_REGNUM; i <= NDS32_PC_REGNUM; i++)
  1574. valid_p &= tdesc_numbered_register (feature, tdesc_data, i,
  1575. nds32_register_names[i]);
  1576. if (!valid_p)
  1577. return 0;
  1578. /* Fixed-number R11-R27. */
  1579. for (i = NDS32_R0_REGNUM + 11; i <= NDS32_R0_REGNUM + 27; i++)
  1580. tdesc_numbered_register (feature, tdesc_data, i, nds32_register_names[i]);
  1581. feature = tdesc_find_feature (tdesc, "org.gnu.gdb.nds32.fpu");
  1582. if (feature != NULL)
  1583. {
  1584. int num_fdr_regs, num_fsr_regs, fs0_regnum, num_listed_fsr;
  1585. int freg = -1;
  1586. /* Guess FPU configuration via listed registers. */
  1587. if (tdesc_unnumbered_register (feature, "fd31"))
  1588. freg = 3;
  1589. else if (tdesc_unnumbered_register (feature, "fd15"))
  1590. freg = 2;
  1591. else if (tdesc_unnumbered_register (feature, "fd7"))
  1592. freg = 1;
  1593. else if (tdesc_unnumbered_register (feature, "fd3"))
  1594. freg = 0;
  1595. if (freg == -1)
  1596. /* Required FDR is not found. */
  1597. return 0;
  1598. else
  1599. *fpu_freg = freg;
  1600. /* Validate and fixed-number required FDRs. */
  1601. num_fdr_regs = num_fdr_map[freg];
  1602. for (i = 0; i < num_fdr_regs; i++)
  1603. valid_p &= tdesc_numbered_register (feature, tdesc_data,
  1604. NDS32_FD0_REGNUM + i,
  1605. nds32_fdr_register_names[i]);
  1606. if (!valid_p)
  1607. return 0;
  1608. /* Count the number of listed FSRs, and fixed-number them if present. */
  1609. num_fsr_regs = num_fsr_map[freg];
  1610. fs0_regnum = NDS32_FD0_REGNUM + num_fdr_regs;
  1611. num_listed_fsr = 0;
  1612. for (i = 0; i < num_fsr_regs; i++)
  1613. num_listed_fsr += tdesc_numbered_register (feature, tdesc_data,
  1614. fs0_regnum + i,
  1615. nds32_fsr_register_names[i]);
  1616. if (num_listed_fsr == 0)
  1617. /* No required FSRs are listed explicitly, make them pseudo registers
  1618. of FDRs. */
  1619. *use_pseudo_fsrs = 1;
  1620. else if (num_listed_fsr == num_fsr_regs)
  1621. /* All required FSRs are listed explicitly. */
  1622. *use_pseudo_fsrs = 0;
  1623. else
  1624. /* Some required FSRs are missing. */
  1625. return 0;
  1626. }
  1627. return 1;
  1628. }
  1629. /* Initialize the current architecture based on INFO. If possible,
  1630. re-use an architecture from ARCHES, which is a list of
  1631. architectures already created during this debugging session.
  1632. Called e.g. at program startup, when reading a core file, and when
  1633. reading a binary file. */
  1634. static struct gdbarch *
  1635. nds32_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  1636. {
  1637. struct gdbarch *gdbarch;
  1638. struct gdbarch_list *best_arch;
  1639. tdesc_arch_data_up tdesc_data;
  1640. const struct target_desc *tdesc = info.target_desc;
  1641. int elf_abi = E_NDS_ABI_AABI;
  1642. int fpu_freg = -1;
  1643. int use_pseudo_fsrs = 0;
  1644. int i, num_regs, maxregs;
  1645. /* Extract the elf_flags if available. */
  1646. if (info.abfd && bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
  1647. elf_abi = elf_elfheader (info.abfd)->e_flags & EF_NDS_ABI;
  1648. /* If there is already a candidate, use it. */
  1649. for (best_arch = gdbarch_list_lookup_by_info (arches, &info);
  1650. best_arch != NULL;
  1651. best_arch = gdbarch_list_lookup_by_info (best_arch->next, &info))
  1652. {
  1653. nds32_gdbarch_tdep *idep
  1654. = (nds32_gdbarch_tdep *) gdbarch_tdep (best_arch->gdbarch);
  1655. if (idep->elf_abi != elf_abi)
  1656. continue;
  1657. /* Found a match. */
  1658. break;
  1659. }
  1660. if (best_arch != NULL)
  1661. return best_arch->gdbarch;
  1662. if (!tdesc_has_registers (tdesc))
  1663. tdesc = tdesc_nds32;
  1664. tdesc_data = tdesc_data_alloc ();
  1665. if (!nds32_validate_tdesc_p (tdesc, tdesc_data.get (), &fpu_freg,
  1666. &use_pseudo_fsrs))
  1667. return NULL;
  1668. /* Allocate space for the new architecture. */
  1669. nds32_gdbarch_tdep *tdep = new nds32_gdbarch_tdep;
  1670. tdep->fpu_freg = fpu_freg;
  1671. tdep->use_pseudo_fsrs = use_pseudo_fsrs;
  1672. tdep->fs0_regnum = -1;
  1673. tdep->elf_abi = elf_abi;
  1674. gdbarch = gdbarch_alloc (&info, tdep);
  1675. set_gdbarch_wchar_bit (gdbarch, 16);
  1676. set_gdbarch_wchar_signed (gdbarch, 0);
  1677. if (fpu_freg == -1)
  1678. num_regs = NDS32_NUM_REGS;
  1679. else if (use_pseudo_fsrs == 1)
  1680. {
  1681. set_gdbarch_pseudo_register_read (gdbarch, nds32_pseudo_register_read);
  1682. set_gdbarch_pseudo_register_write (gdbarch, nds32_pseudo_register_write);
  1683. set_tdesc_pseudo_register_name (gdbarch, nds32_pseudo_register_name);
  1684. set_tdesc_pseudo_register_type (gdbarch, nds32_pseudo_register_type);
  1685. set_gdbarch_num_pseudo_regs (gdbarch, num_fsr_map[fpu_freg]);
  1686. num_regs = NDS32_NUM_REGS + num_fdr_map[fpu_freg];
  1687. }
  1688. else
  1689. num_regs = NDS32_NUM_REGS + num_fdr_map[fpu_freg] + num_fsr_map[fpu_freg];
  1690. set_gdbarch_num_regs (gdbarch, num_regs);
  1691. tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
  1692. /* Cache the register number of fs0. */
  1693. if (fpu_freg != -1)
  1694. tdep->fs0_regnum = user_reg_map_name_to_regnum (gdbarch, "fs0", -1);
  1695. /* Add NDS32 register aliases. To avoid search in user register name space,
  1696. user_reg_map_name_to_regnum is not used. */
  1697. maxregs = gdbarch_num_cooked_regs (gdbarch);
  1698. for (i = 0; i < ARRAY_SIZE (nds32_register_aliases); i++)
  1699. {
  1700. int regnum, j;
  1701. regnum = -1;
  1702. /* Search register name space. */
  1703. for (j = 0; j < maxregs; j++)
  1704. {
  1705. const char *regname = gdbarch_register_name (gdbarch, j);
  1706. if (regname != NULL
  1707. && strcmp (regname, nds32_register_aliases[i].name) == 0)
  1708. {
  1709. regnum = j;
  1710. break;
  1711. }
  1712. }
  1713. /* Try next alias entry if the given name can not be found in register
  1714. name space. */
  1715. if (regnum == -1)
  1716. continue;
  1717. user_reg_add (gdbarch, nds32_register_aliases[i].alias,
  1718. value_of_nds32_reg, (const void *) (intptr_t) regnum);
  1719. }
  1720. nds32_add_reggroups (gdbarch);
  1721. /* Hook in ABI-specific overrides, if they have been registered. */
  1722. info.tdesc_data = tdesc_data.get ();
  1723. gdbarch_init_osabi (info, gdbarch);
  1724. /* Override tdesc_register callbacks for system registers. */
  1725. set_gdbarch_register_reggroup_p (gdbarch, nds32_register_reggroup_p);
  1726. set_gdbarch_sp_regnum (gdbarch, NDS32_SP_REGNUM);
  1727. set_gdbarch_pc_regnum (gdbarch, NDS32_PC_REGNUM);
  1728. set_gdbarch_stack_frame_destroyed_p (gdbarch, nds32_stack_frame_destroyed_p);
  1729. set_gdbarch_dwarf2_reg_to_regnum (gdbarch, nds32_dwarf2_reg_to_regnum);
  1730. set_gdbarch_push_dummy_call (gdbarch, nds32_push_dummy_call);
  1731. set_gdbarch_return_value (gdbarch, nds32_return_value);
  1732. set_gdbarch_skip_prologue (gdbarch, nds32_skip_prologue);
  1733. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  1734. set_gdbarch_breakpoint_kind_from_pc (gdbarch,
  1735. nds32_breakpoint::kind_from_pc);
  1736. set_gdbarch_sw_breakpoint_from_kind (gdbarch,
  1737. nds32_breakpoint::bp_from_kind);
  1738. set_gdbarch_frame_align (gdbarch, nds32_frame_align);
  1739. frame_base_set_default (gdbarch, &nds32_frame_base);
  1740. /* Handle longjmp. */
  1741. set_gdbarch_get_longjmp_target (gdbarch, nds32_get_longjmp_target);
  1742. /* The order of appending is the order it check frame. */
  1743. dwarf2_append_unwinders (gdbarch);
  1744. frame_unwind_append_unwinder (gdbarch, &nds32_epilogue_frame_unwind);
  1745. frame_unwind_append_unwinder (gdbarch, &nds32_frame_unwind);
  1746. return gdbarch;
  1747. }
  1748. void _initialize_nds32_tdep ();
  1749. void
  1750. _initialize_nds32_tdep ()
  1751. {
  1752. /* Initialize gdbarch. */
  1753. register_gdbarch_init (bfd_arch_nds32, nds32_gdbarch_init);
  1754. initialize_tdesc_nds32 ();
  1755. nds32_init_reggroups ();
  1756. }