nios2-tdep.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406
  1. /* Target-machine dependent code for Nios II, for GDB.
  2. Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. Contributed by Peter Brookes (pbrookes@altera.com)
  4. and Andrew Draper (adraper@altera.com).
  5. Contributed by Mentor Graphics, Inc.
  6. This file is part of GDB.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  17. #include "defs.h"
  18. #include "frame.h"
  19. #include "frame-unwind.h"
  20. #include "frame-base.h"
  21. #include "trad-frame.h"
  22. #include "dwarf2/frame.h"
  23. #include "symtab.h"
  24. #include "inferior.h"
  25. #include "gdbtypes.h"
  26. #include "gdbcore.h"
  27. #include "gdbcmd.h"
  28. #include "osabi.h"
  29. #include "target.h"
  30. #include "dis-asm.h"
  31. #include "regcache.h"
  32. #include "value.h"
  33. #include "symfile.h"
  34. #include "arch-utils.h"
  35. #include "infcall.h"
  36. #include "regset.h"
  37. #include "target-descriptions.h"
  38. /* To get entry_point_address. */
  39. #include "objfiles.h"
  40. #include <algorithm>
  41. /* Nios II specific header. */
  42. #include "nios2-tdep.h"
  43. #include "features/nios2.c"
  44. /* Control debugging information emitted in this file. */
  45. static bool nios2_debug = false;
  46. /* The following structures are used in the cache for prologue
  47. analysis; see the reg_value and reg_saved tables in
  48. struct nios2_unwind_cache, respectively. */
  49. /* struct reg_value is used to record that a register has reg's initial
  50. value at the start of a function plus the given constant offset.
  51. If reg == 0, then the value is just the offset.
  52. If reg < 0, then the value is unknown. */
  53. struct reg_value
  54. {
  55. int reg;
  56. int offset;
  57. };
  58. /* struct reg_saved is used to record that a register value has been saved at
  59. basereg + addr, for basereg >= 0. If basereg < 0, that indicates
  60. that the register is not known to have been saved. Note that when
  61. basereg == NIOS2_Z_REGNUM (that is, r0, which holds value 0),
  62. addr is an absolute address. */
  63. struct reg_saved
  64. {
  65. int basereg;
  66. CORE_ADDR addr;
  67. };
  68. struct nios2_unwind_cache
  69. {
  70. /* The frame's base, optionally used by the high-level debug info. */
  71. CORE_ADDR base;
  72. /* The previous frame's inner most stack address. Used as this
  73. frame ID's stack_addr. */
  74. CORE_ADDR cfa;
  75. /* The address of the first instruction in this function. */
  76. CORE_ADDR pc;
  77. /* Which register holds the return address for the frame. */
  78. int return_regnum;
  79. /* Table indicating what changes have been made to each register. */
  80. struct reg_value reg_value[NIOS2_NUM_REGS];
  81. /* Table indicating where each register has been saved. */
  82. struct reg_saved reg_saved[NIOS2_NUM_REGS];
  83. };
  84. /* This array is a mapping from Dwarf-2 register numbering to GDB's. */
  85. static int nios2_dwarf2gdb_regno_map[] =
  86. {
  87. 0, 1, 2, 3,
  88. 4, 5, 6, 7,
  89. 8, 9, 10, 11,
  90. 12, 13, 14, 15,
  91. 16, 17, 18, 19,
  92. 20, 21, 22, 23,
  93. 24, 25,
  94. NIOS2_GP_REGNUM, /* 26 */
  95. NIOS2_SP_REGNUM, /* 27 */
  96. NIOS2_FP_REGNUM, /* 28 */
  97. NIOS2_EA_REGNUM, /* 29 */
  98. NIOS2_BA_REGNUM, /* 30 */
  99. NIOS2_RA_REGNUM, /* 31 */
  100. NIOS2_PC_REGNUM, /* 32 */
  101. NIOS2_STATUS_REGNUM, /* 33 */
  102. NIOS2_ESTATUS_REGNUM, /* 34 */
  103. NIOS2_BSTATUS_REGNUM, /* 35 */
  104. NIOS2_IENABLE_REGNUM, /* 36 */
  105. NIOS2_IPENDING_REGNUM, /* 37 */
  106. NIOS2_CPUID_REGNUM, /* 38 */
  107. 39, /* CTL6 */ /* 39 */
  108. NIOS2_EXCEPTION_REGNUM, /* 40 */
  109. NIOS2_PTEADDR_REGNUM, /* 41 */
  110. NIOS2_TLBACC_REGNUM, /* 42 */
  111. NIOS2_TLBMISC_REGNUM, /* 43 */
  112. NIOS2_ECCINJ_REGNUM, /* 44 */
  113. NIOS2_BADADDR_REGNUM, /* 45 */
  114. NIOS2_CONFIG_REGNUM, /* 46 */
  115. NIOS2_MPUBASE_REGNUM, /* 47 */
  116. NIOS2_MPUACC_REGNUM /* 48 */
  117. };
  118. gdb_static_assert (ARRAY_SIZE (nios2_dwarf2gdb_regno_map) == NIOS2_NUM_REGS);
  119. /* Implement the dwarf2_reg_to_regnum gdbarch method. */
  120. static int
  121. nios2_dwarf_reg_to_regnum (struct gdbarch *gdbarch, int dw_reg)
  122. {
  123. if (dw_reg < 0 || dw_reg >= NIOS2_NUM_REGS)
  124. return -1;
  125. return nios2_dwarf2gdb_regno_map[dw_reg];
  126. }
  127. /* Canonical names for the 49 registers. */
  128. static const char *const nios2_reg_names[NIOS2_NUM_REGS] =
  129. {
  130. "zero", "at", "r2", "r3", "r4", "r5", "r6", "r7",
  131. "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15",
  132. "r16", "r17", "r18", "r19", "r20", "r21", "r22", "r23",
  133. "et", "bt", "gp", "sp", "fp", "ea", "sstatus", "ra",
  134. "pc",
  135. "status", "estatus", "bstatus", "ienable",
  136. "ipending", "cpuid", "ctl6", "exception",
  137. "pteaddr", "tlbacc", "tlbmisc", "eccinj",
  138. "badaddr", "config", "mpubase", "mpuacc"
  139. };
  140. /* Implement the register_name gdbarch method. */
  141. static const char *
  142. nios2_register_name (struct gdbarch *gdbarch, int regno)
  143. {
  144. /* Use mnemonic aliases for GPRs. */
  145. if (regno >= 0 && regno < NIOS2_NUM_REGS)
  146. return nios2_reg_names[regno];
  147. else
  148. return tdesc_register_name (gdbarch, regno);
  149. }
  150. /* Implement the register_type gdbarch method. */
  151. static struct type *
  152. nios2_register_type (struct gdbarch *gdbarch, int regno)
  153. {
  154. /* If the XML description has register information, use that to
  155. determine the register type. */
  156. if (tdesc_has_registers (gdbarch_target_desc (gdbarch)))
  157. return tdesc_register_type (gdbarch, regno);
  158. if (regno == NIOS2_PC_REGNUM)
  159. return builtin_type (gdbarch)->builtin_func_ptr;
  160. else if (regno == NIOS2_SP_REGNUM)
  161. return builtin_type (gdbarch)->builtin_data_ptr;
  162. else
  163. return builtin_type (gdbarch)->builtin_uint32;
  164. }
  165. /* Given a return value in REGCACHE with a type VALTYPE,
  166. extract and copy its value into VALBUF. */
  167. static void
  168. nios2_extract_return_value (struct gdbarch *gdbarch, struct type *valtype,
  169. struct regcache *regcache, gdb_byte *valbuf)
  170. {
  171. int len = TYPE_LENGTH (valtype);
  172. /* Return values of up to 8 bytes are returned in $r2 $r3. */
  173. if (len <= register_size (gdbarch, NIOS2_R2_REGNUM))
  174. regcache->cooked_read (NIOS2_R2_REGNUM, valbuf);
  175. else
  176. {
  177. gdb_assert (len <= (register_size (gdbarch, NIOS2_R2_REGNUM)
  178. + register_size (gdbarch, NIOS2_R3_REGNUM)));
  179. regcache->cooked_read (NIOS2_R2_REGNUM, valbuf);
  180. regcache->cooked_read (NIOS2_R3_REGNUM, valbuf + 4);
  181. }
  182. }
  183. /* Write into appropriate registers a function return value
  184. of type TYPE, given in virtual format. */
  185. static void
  186. nios2_store_return_value (struct gdbarch *gdbarch, struct type *valtype,
  187. struct regcache *regcache, const gdb_byte *valbuf)
  188. {
  189. int len = TYPE_LENGTH (valtype);
  190. /* Return values of up to 8 bytes are returned in $r2 $r3. */
  191. if (len <= register_size (gdbarch, NIOS2_R2_REGNUM))
  192. regcache->cooked_write (NIOS2_R2_REGNUM, valbuf);
  193. else
  194. {
  195. gdb_assert (len <= (register_size (gdbarch, NIOS2_R2_REGNUM)
  196. + register_size (gdbarch, NIOS2_R3_REGNUM)));
  197. regcache->cooked_write (NIOS2_R2_REGNUM, valbuf);
  198. regcache->cooked_write (NIOS2_R3_REGNUM, valbuf + 4);
  199. }
  200. }
  201. /* Set up the default values of the registers. */
  202. static void
  203. nios2_setup_default (struct nios2_unwind_cache *cache)
  204. {
  205. int i;
  206. for (i = 0; i < NIOS2_NUM_REGS; i++)
  207. {
  208. /* All registers start off holding their previous values. */
  209. cache->reg_value[i].reg = i;
  210. cache->reg_value[i].offset = 0;
  211. /* All registers start off not saved. */
  212. cache->reg_saved[i].basereg = -1;
  213. cache->reg_saved[i].addr = 0;
  214. }
  215. }
  216. /* Initialize the unwind cache. */
  217. static void
  218. nios2_init_cache (struct nios2_unwind_cache *cache, CORE_ADDR pc)
  219. {
  220. cache->base = 0;
  221. cache->cfa = 0;
  222. cache->pc = pc;
  223. cache->return_regnum = NIOS2_RA_REGNUM;
  224. nios2_setup_default (cache);
  225. }
  226. /* Read and identify an instruction at PC. If INSNP is non-null,
  227. store the instruction word into that location. Return the opcode
  228. pointer or NULL if the memory couldn't be read or disassembled. */
  229. static const struct nios2_opcode *
  230. nios2_fetch_insn (struct gdbarch *gdbarch, CORE_ADDR pc,
  231. unsigned int *insnp)
  232. {
  233. LONGEST memword;
  234. unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
  235. unsigned int insn;
  236. if (mach == bfd_mach_nios2r2)
  237. {
  238. if (!safe_read_memory_integer (pc, NIOS2_OPCODE_SIZE,
  239. BFD_ENDIAN_LITTLE, &memword)
  240. && !safe_read_memory_integer (pc, NIOS2_CDX_OPCODE_SIZE,
  241. BFD_ENDIAN_LITTLE, &memword))
  242. return NULL;
  243. }
  244. else if (!safe_read_memory_integer (pc, NIOS2_OPCODE_SIZE,
  245. gdbarch_byte_order (gdbarch), &memword))
  246. return NULL;
  247. insn = (unsigned int) memword;
  248. if (insnp)
  249. *insnp = insn;
  250. return nios2_find_opcode_hash (insn, mach);
  251. }
  252. /* Match and disassemble an ADD-type instruction, with 3 register operands.
  253. Returns true on success, and fills in the operand pointers. */
  254. static int
  255. nios2_match_add (uint32_t insn, const struct nios2_opcode *op,
  256. unsigned long mach, int *ra, int *rb, int *rc)
  257. {
  258. int is_r2 = (mach == bfd_mach_nios2r2);
  259. if (!is_r2 && (op->match == MATCH_R1_ADD || op->match == MATCH_R1_MOV))
  260. {
  261. *ra = GET_IW_R_A (insn);
  262. *rb = GET_IW_R_B (insn);
  263. *rc = GET_IW_R_C (insn);
  264. return 1;
  265. }
  266. else if (!is_r2)
  267. return 0;
  268. else if (op->match == MATCH_R2_ADD || op->match == MATCH_R2_MOV)
  269. {
  270. *ra = GET_IW_F3X6L5_A (insn);
  271. *rb = GET_IW_F3X6L5_B (insn);
  272. *rc = GET_IW_F3X6L5_C (insn);
  273. return 1;
  274. }
  275. else if (op->match == MATCH_R2_ADD_N)
  276. {
  277. *ra = nios2_r2_reg3_mappings[GET_IW_T3X1_A3 (insn)];
  278. *rb = nios2_r2_reg3_mappings[GET_IW_T3X1_B3 (insn)];
  279. *rc = nios2_r2_reg3_mappings[GET_IW_T3X1_C3 (insn)];
  280. return 1;
  281. }
  282. else if (op->match == MATCH_R2_MOV_N)
  283. {
  284. *ra = GET_IW_F2_A (insn);
  285. *rb = 0;
  286. *rc = GET_IW_F2_B (insn);
  287. return 1;
  288. }
  289. return 0;
  290. }
  291. /* Match and disassemble a SUB-type instruction, with 3 register operands.
  292. Returns true on success, and fills in the operand pointers. */
  293. static int
  294. nios2_match_sub (uint32_t insn, const struct nios2_opcode *op,
  295. unsigned long mach, int *ra, int *rb, int *rc)
  296. {
  297. int is_r2 = (mach == bfd_mach_nios2r2);
  298. if (!is_r2 && op->match == MATCH_R1_SUB)
  299. {
  300. *ra = GET_IW_R_A (insn);
  301. *rb = GET_IW_R_B (insn);
  302. *rc = GET_IW_R_C (insn);
  303. return 1;
  304. }
  305. else if (!is_r2)
  306. return 0;
  307. else if (op->match == MATCH_R2_SUB)
  308. {
  309. *ra = GET_IW_F3X6L5_A (insn);
  310. *rb = GET_IW_F3X6L5_B (insn);
  311. *rc = GET_IW_F3X6L5_C (insn);
  312. return 1;
  313. }
  314. else if (op->match == MATCH_R2_SUB_N)
  315. {
  316. *ra = nios2_r2_reg3_mappings[GET_IW_T3X1_A3 (insn)];
  317. *rb = nios2_r2_reg3_mappings[GET_IW_T3X1_B3 (insn)];
  318. *rc = nios2_r2_reg3_mappings[GET_IW_T3X1_C3 (insn)];
  319. return 1;
  320. }
  321. return 0;
  322. }
  323. /* Match and disassemble an ADDI-type instruction, with 2 register operands
  324. and one immediate operand.
  325. Returns true on success, and fills in the operand pointers. */
  326. static int
  327. nios2_match_addi (uint32_t insn, const struct nios2_opcode *op,
  328. unsigned long mach, int *ra, int *rb, int *imm)
  329. {
  330. int is_r2 = (mach == bfd_mach_nios2r2);
  331. if (!is_r2 && op->match == MATCH_R1_ADDI)
  332. {
  333. *ra = GET_IW_I_A (insn);
  334. *rb = GET_IW_I_B (insn);
  335. *imm = (signed) (GET_IW_I_IMM16 (insn) << 16) >> 16;
  336. return 1;
  337. }
  338. else if (!is_r2)
  339. return 0;
  340. else if (op->match == MATCH_R2_ADDI)
  341. {
  342. *ra = GET_IW_F2I16_A (insn);
  343. *rb = GET_IW_F2I16_B (insn);
  344. *imm = (signed) (GET_IW_F2I16_IMM16 (insn) << 16) >> 16;
  345. return 1;
  346. }
  347. else if (op->match == MATCH_R2_ADDI_N || op->match == MATCH_R2_SUBI_N)
  348. {
  349. *ra = nios2_r2_reg3_mappings[GET_IW_T2X1I3_A3 (insn)];
  350. *rb = nios2_r2_reg3_mappings[GET_IW_T2X1I3_B3 (insn)];
  351. *imm = nios2_r2_asi_n_mappings[GET_IW_T2X1I3_IMM3 (insn)];
  352. if (op->match == MATCH_R2_SUBI_N)
  353. *imm = - (*imm);
  354. return 1;
  355. }
  356. else if (op->match == MATCH_R2_SPADDI_N)
  357. {
  358. *ra = nios2_r2_reg3_mappings[GET_IW_T1I7_A3 (insn)];
  359. *rb = NIOS2_SP_REGNUM;
  360. *imm = GET_IW_T1I7_IMM7 (insn) << 2;
  361. return 1;
  362. }
  363. else if (op->match == MATCH_R2_SPINCI_N || op->match == MATCH_R2_SPDECI_N)
  364. {
  365. *ra = NIOS2_SP_REGNUM;
  366. *rb = NIOS2_SP_REGNUM;
  367. *imm = GET_IW_X1I7_IMM7 (insn) << 2;
  368. if (op->match == MATCH_R2_SPDECI_N)
  369. *imm = - (*imm);
  370. return 1;
  371. }
  372. return 0;
  373. }
  374. /* Match and disassemble an ORHI-type instruction, with 2 register operands
  375. and one unsigned immediate operand.
  376. Returns true on success, and fills in the operand pointers. */
  377. static int
  378. nios2_match_orhi (uint32_t insn, const struct nios2_opcode *op,
  379. unsigned long mach, int *ra, int *rb, unsigned int *uimm)
  380. {
  381. int is_r2 = (mach == bfd_mach_nios2r2);
  382. if (!is_r2 && op->match == MATCH_R1_ORHI)
  383. {
  384. *ra = GET_IW_I_A (insn);
  385. *rb = GET_IW_I_B (insn);
  386. *uimm = GET_IW_I_IMM16 (insn);
  387. return 1;
  388. }
  389. else if (!is_r2)
  390. return 0;
  391. else if (op->match == MATCH_R2_ORHI)
  392. {
  393. *ra = GET_IW_F2I16_A (insn);
  394. *rb = GET_IW_F2I16_B (insn);
  395. *uimm = GET_IW_F2I16_IMM16 (insn);
  396. return 1;
  397. }
  398. return 0;
  399. }
  400. /* Match and disassemble a STW-type instruction, with 2 register operands
  401. and one immediate operand.
  402. Returns true on success, and fills in the operand pointers. */
  403. static int
  404. nios2_match_stw (uint32_t insn, const struct nios2_opcode *op,
  405. unsigned long mach, int *ra, int *rb, int *imm)
  406. {
  407. int is_r2 = (mach == bfd_mach_nios2r2);
  408. if (!is_r2 && (op->match == MATCH_R1_STW || op->match == MATCH_R1_STWIO))
  409. {
  410. *ra = GET_IW_I_A (insn);
  411. *rb = GET_IW_I_B (insn);
  412. *imm = (signed) (GET_IW_I_IMM16 (insn) << 16) >> 16;
  413. return 1;
  414. }
  415. else if (!is_r2)
  416. return 0;
  417. else if (op->match == MATCH_R2_STW)
  418. {
  419. *ra = GET_IW_F2I16_A (insn);
  420. *rb = GET_IW_F2I16_B (insn);
  421. *imm = (signed) (GET_IW_F2I16_IMM16 (insn) << 16) >> 16;
  422. return 1;
  423. }
  424. else if (op->match == MATCH_R2_STWIO)
  425. {
  426. *ra = GET_IW_F2X4I12_A (insn);
  427. *rb = GET_IW_F2X4I12_B (insn);
  428. *imm = (signed) (GET_IW_F2X4I12_IMM12 (insn) << 20) >> 20;
  429. return 1;
  430. }
  431. else if (op->match == MATCH_R2_STW_N)
  432. {
  433. *ra = nios2_r2_reg3_mappings[GET_IW_T2I4_A3 (insn)];
  434. *rb = nios2_r2_reg3_mappings[GET_IW_T2I4_B3 (insn)];
  435. *imm = GET_IW_T2I4_IMM4 (insn) << 2;
  436. return 1;
  437. }
  438. else if (op->match == MATCH_R2_STWSP_N)
  439. {
  440. *ra = NIOS2_SP_REGNUM;
  441. *rb = GET_IW_F1I5_B (insn);
  442. *imm = GET_IW_F1I5_IMM5 (insn) << 2;
  443. return 1;
  444. }
  445. else if (op->match == MATCH_R2_STWZ_N)
  446. {
  447. *ra = nios2_r2_reg3_mappings[GET_IW_T1X1I6_A3 (insn)];
  448. *rb = 0;
  449. *imm = GET_IW_T1X1I6_IMM6 (insn) << 2;
  450. return 1;
  451. }
  452. return 0;
  453. }
  454. /* Match and disassemble a LDW-type instruction, with 2 register operands
  455. and one immediate operand.
  456. Returns true on success, and fills in the operand pointers. */
  457. static int
  458. nios2_match_ldw (uint32_t insn, const struct nios2_opcode *op,
  459. unsigned long mach, int *ra, int *rb, int *imm)
  460. {
  461. int is_r2 = (mach == bfd_mach_nios2r2);
  462. if (!is_r2 && (op->match == MATCH_R1_LDW || op->match == MATCH_R1_LDWIO))
  463. {
  464. *ra = GET_IW_I_A (insn);
  465. *rb = GET_IW_I_B (insn);
  466. *imm = (signed) (GET_IW_I_IMM16 (insn) << 16) >> 16;
  467. return 1;
  468. }
  469. else if (!is_r2)
  470. return 0;
  471. else if (op->match == MATCH_R2_LDW)
  472. {
  473. *ra = GET_IW_F2I16_A (insn);
  474. *rb = GET_IW_F2I16_B (insn);
  475. *imm = (signed) (GET_IW_F2I16_IMM16 (insn) << 16) >> 16;
  476. return 1;
  477. }
  478. else if (op->match == MATCH_R2_LDWIO)
  479. {
  480. *ra = GET_IW_F2X4I12_A (insn);
  481. *rb = GET_IW_F2X4I12_B (insn);
  482. *imm = (signed) (GET_IW_F2X4I12_IMM12 (insn) << 20) >> 20;
  483. return 1;
  484. }
  485. else if (op->match == MATCH_R2_LDW_N)
  486. {
  487. *ra = nios2_r2_reg3_mappings[GET_IW_T2I4_A3 (insn)];
  488. *rb = nios2_r2_reg3_mappings[GET_IW_T2I4_B3 (insn)];
  489. *imm = GET_IW_T2I4_IMM4 (insn) << 2;
  490. return 1;
  491. }
  492. else if (op->match == MATCH_R2_LDWSP_N)
  493. {
  494. *ra = NIOS2_SP_REGNUM;
  495. *rb = GET_IW_F1I5_B (insn);
  496. *imm = GET_IW_F1I5_IMM5 (insn) << 2;
  497. return 1;
  498. }
  499. return 0;
  500. }
  501. /* Match and disassemble a RDCTL instruction, with 2 register operands.
  502. Returns true on success, and fills in the operand pointers. */
  503. static int
  504. nios2_match_rdctl (uint32_t insn, const struct nios2_opcode *op,
  505. unsigned long mach, int *ra, int *rc)
  506. {
  507. int is_r2 = (mach == bfd_mach_nios2r2);
  508. if (!is_r2 && (op->match == MATCH_R1_RDCTL))
  509. {
  510. *ra = GET_IW_R_IMM5 (insn);
  511. *rc = GET_IW_R_C (insn);
  512. return 1;
  513. }
  514. else if (!is_r2)
  515. return 0;
  516. else if (op->match == MATCH_R2_RDCTL)
  517. {
  518. *ra = GET_IW_F3X6L5_IMM5 (insn);
  519. *rc = GET_IW_F3X6L5_C (insn);
  520. return 1;
  521. }
  522. return 0;
  523. }
  524. /* Match and disassemble a PUSH.N or STWM instruction.
  525. Returns true on success, and fills in the operand pointers. */
  526. static int
  527. nios2_match_stwm (uint32_t insn, const struct nios2_opcode *op,
  528. unsigned long mach, unsigned int *reglist,
  529. int *ra, int *imm, int *wb, int *id)
  530. {
  531. int is_r2 = (mach == bfd_mach_nios2r2);
  532. if (!is_r2)
  533. return 0;
  534. else if (op->match == MATCH_R2_PUSH_N)
  535. {
  536. *reglist = 1 << 31;
  537. if (GET_IW_L5I4X1_FP (insn))
  538. *reglist |= (1 << 28);
  539. if (GET_IW_L5I4X1_CS (insn))
  540. {
  541. int val = GET_IW_L5I4X1_REGRANGE (insn);
  542. *reglist |= nios2_r2_reg_range_mappings[val];
  543. }
  544. *ra = NIOS2_SP_REGNUM;
  545. *imm = GET_IW_L5I4X1_IMM4 (insn) << 2;
  546. *wb = 1;
  547. *id = 0;
  548. return 1;
  549. }
  550. else if (op->match == MATCH_R2_STWM)
  551. {
  552. unsigned int rawmask = GET_IW_F1X4L17_REGMASK (insn);
  553. if (GET_IW_F1X4L17_RS (insn))
  554. {
  555. *reglist = ((rawmask << 14) & 0x00ffc000);
  556. if (rawmask & (1 << 10))
  557. *reglist |= (1 << 28);
  558. if (rawmask & (1 << 11))
  559. *reglist |= (1 << 31);
  560. }
  561. else
  562. *reglist = rawmask << 2;
  563. *ra = GET_IW_F1X4L17_A (insn);
  564. *imm = 0;
  565. *wb = GET_IW_F1X4L17_WB (insn);
  566. *id = GET_IW_F1X4L17_ID (insn);
  567. return 1;
  568. }
  569. return 0;
  570. }
  571. /* Match and disassemble a POP.N or LDWM instruction.
  572. Returns true on success, and fills in the operand pointers. */
  573. static int
  574. nios2_match_ldwm (uint32_t insn, const struct nios2_opcode *op,
  575. unsigned long mach, unsigned int *reglist,
  576. int *ra, int *imm, int *wb, int *id, int *ret)
  577. {
  578. int is_r2 = (mach == bfd_mach_nios2r2);
  579. if (!is_r2)
  580. return 0;
  581. else if (op->match == MATCH_R2_POP_N)
  582. {
  583. *reglist = 1 << 31;
  584. if (GET_IW_L5I4X1_FP (insn))
  585. *reglist |= (1 << 28);
  586. if (GET_IW_L5I4X1_CS (insn))
  587. {
  588. int val = GET_IW_L5I4X1_REGRANGE (insn);
  589. *reglist |= nios2_r2_reg_range_mappings[val];
  590. }
  591. *ra = NIOS2_SP_REGNUM;
  592. *imm = GET_IW_L5I4X1_IMM4 (insn) << 2;
  593. *wb = 1;
  594. *id = 1;
  595. *ret = 1;
  596. return 1;
  597. }
  598. else if (op->match == MATCH_R2_LDWM)
  599. {
  600. unsigned int rawmask = GET_IW_F1X4L17_REGMASK (insn);
  601. if (GET_IW_F1X4L17_RS (insn))
  602. {
  603. *reglist = ((rawmask << 14) & 0x00ffc000);
  604. if (rawmask & (1 << 10))
  605. *reglist |= (1 << 28);
  606. if (rawmask & (1 << 11))
  607. *reglist |= (1 << 31);
  608. }
  609. else
  610. *reglist = rawmask << 2;
  611. *ra = GET_IW_F1X4L17_A (insn);
  612. *imm = 0;
  613. *wb = GET_IW_F1X4L17_WB (insn);
  614. *id = GET_IW_F1X4L17_ID (insn);
  615. *ret = GET_IW_F1X4L17_PC (insn);
  616. return 1;
  617. }
  618. return 0;
  619. }
  620. /* Match and disassemble a branch instruction, with (potentially)
  621. 2 register operands and one immediate operand.
  622. Returns true on success, and fills in the operand pointers. */
  623. enum branch_condition {
  624. branch_none,
  625. branch_eq,
  626. branch_ne,
  627. branch_ge,
  628. branch_geu,
  629. branch_lt,
  630. branch_ltu
  631. };
  632. static int
  633. nios2_match_branch (uint32_t insn, const struct nios2_opcode *op,
  634. unsigned long mach, int *ra, int *rb, int *imm,
  635. enum branch_condition *cond)
  636. {
  637. int is_r2 = (mach == bfd_mach_nios2r2);
  638. if (!is_r2)
  639. {
  640. switch (op->match)
  641. {
  642. case MATCH_R1_BR:
  643. *cond = branch_none;
  644. break;
  645. case MATCH_R1_BEQ:
  646. *cond = branch_eq;
  647. break;
  648. case MATCH_R1_BNE:
  649. *cond = branch_ne;
  650. break;
  651. case MATCH_R1_BGE:
  652. *cond = branch_ge;
  653. break;
  654. case MATCH_R1_BGEU:
  655. *cond = branch_geu;
  656. break;
  657. case MATCH_R1_BLT:
  658. *cond = branch_lt;
  659. break;
  660. case MATCH_R1_BLTU:
  661. *cond = branch_ltu;
  662. break;
  663. default:
  664. return 0;
  665. }
  666. *imm = (signed) (GET_IW_I_IMM16 (insn) << 16) >> 16;
  667. *ra = GET_IW_I_A (insn);
  668. *rb = GET_IW_I_B (insn);
  669. return 1;
  670. }
  671. else
  672. {
  673. switch (op->match)
  674. {
  675. case MATCH_R2_BR_N:
  676. *cond = branch_none;
  677. *ra = NIOS2_Z_REGNUM;
  678. *rb = NIOS2_Z_REGNUM;
  679. *imm = (signed) ((GET_IW_I10_IMM10 (insn) << 1) << 21) >> 21;
  680. return 1;
  681. case MATCH_R2_BEQZ_N:
  682. *cond = branch_eq;
  683. *ra = nios2_r2_reg3_mappings[GET_IW_T1I7_A3 (insn)];
  684. *rb = NIOS2_Z_REGNUM;
  685. *imm = (signed) ((GET_IW_T1I7_IMM7 (insn) << 1) << 24) >> 24;
  686. return 1;
  687. case MATCH_R2_BNEZ_N:
  688. *cond = branch_ne;
  689. *ra = nios2_r2_reg3_mappings[GET_IW_T1I7_A3 (insn)];
  690. *rb = NIOS2_Z_REGNUM;
  691. *imm = (signed) ((GET_IW_T1I7_IMM7 (insn) << 1) << 24) >> 24;
  692. return 1;
  693. case MATCH_R2_BR:
  694. *cond = branch_none;
  695. break;
  696. case MATCH_R2_BEQ:
  697. *cond = branch_eq;
  698. break;
  699. case MATCH_R2_BNE:
  700. *cond = branch_ne;
  701. break;
  702. case MATCH_R2_BGE:
  703. *cond = branch_ge;
  704. break;
  705. case MATCH_R2_BGEU:
  706. *cond = branch_geu;
  707. break;
  708. case MATCH_R2_BLT:
  709. *cond = branch_lt;
  710. break;
  711. case MATCH_R2_BLTU:
  712. *cond = branch_ltu;
  713. break;
  714. default:
  715. return 0;
  716. }
  717. *ra = GET_IW_F2I16_A (insn);
  718. *rb = GET_IW_F2I16_B (insn);
  719. *imm = (signed) (GET_IW_F2I16_IMM16 (insn) << 16) >> 16;
  720. return 1;
  721. }
  722. return 0;
  723. }
  724. /* Match and disassemble a direct jump instruction, with an
  725. unsigned operand. Returns true on success, and fills in the operand
  726. pointer. */
  727. static int
  728. nios2_match_jmpi (uint32_t insn, const struct nios2_opcode *op,
  729. unsigned long mach, unsigned int *uimm)
  730. {
  731. int is_r2 = (mach == bfd_mach_nios2r2);
  732. if (!is_r2 && op->match == MATCH_R1_JMPI)
  733. {
  734. *uimm = GET_IW_J_IMM26 (insn) << 2;
  735. return 1;
  736. }
  737. else if (!is_r2)
  738. return 0;
  739. else if (op->match == MATCH_R2_JMPI)
  740. {
  741. *uimm = GET_IW_L26_IMM26 (insn) << 2;
  742. return 1;
  743. }
  744. return 0;
  745. }
  746. /* Match and disassemble a direct call instruction, with an
  747. unsigned operand. Returns true on success, and fills in the operand
  748. pointer. */
  749. static int
  750. nios2_match_calli (uint32_t insn, const struct nios2_opcode *op,
  751. unsigned long mach, unsigned int *uimm)
  752. {
  753. int is_r2 = (mach == bfd_mach_nios2r2);
  754. if (!is_r2 && op->match == MATCH_R1_CALL)
  755. {
  756. *uimm = GET_IW_J_IMM26 (insn) << 2;
  757. return 1;
  758. }
  759. else if (!is_r2)
  760. return 0;
  761. else if (op->match == MATCH_R2_CALL)
  762. {
  763. *uimm = GET_IW_L26_IMM26 (insn) << 2;
  764. return 1;
  765. }
  766. return 0;
  767. }
  768. /* Match and disassemble an indirect jump instruction, with a
  769. (possibly implicit) register operand. Returns true on success, and fills
  770. in the operand pointer. */
  771. static int
  772. nios2_match_jmpr (uint32_t insn, const struct nios2_opcode *op,
  773. unsigned long mach, int *ra)
  774. {
  775. int is_r2 = (mach == bfd_mach_nios2r2);
  776. if (!is_r2)
  777. switch (op->match)
  778. {
  779. case MATCH_R1_JMP:
  780. *ra = GET_IW_I_A (insn);
  781. return 1;
  782. case MATCH_R1_RET:
  783. *ra = NIOS2_RA_REGNUM;
  784. return 1;
  785. case MATCH_R1_ERET:
  786. *ra = NIOS2_EA_REGNUM;
  787. return 1;
  788. case MATCH_R1_BRET:
  789. *ra = NIOS2_BA_REGNUM;
  790. return 1;
  791. default:
  792. return 0;
  793. }
  794. else
  795. switch (op->match)
  796. {
  797. case MATCH_R2_JMP:
  798. *ra = GET_IW_F2I16_A (insn);
  799. return 1;
  800. case MATCH_R2_JMPR_N:
  801. *ra = GET_IW_F1X1_A (insn);
  802. return 1;
  803. case MATCH_R2_RET:
  804. case MATCH_R2_RET_N:
  805. *ra = NIOS2_RA_REGNUM;
  806. return 1;
  807. case MATCH_R2_ERET:
  808. *ra = NIOS2_EA_REGNUM;
  809. return 1;
  810. case MATCH_R2_BRET:
  811. *ra = NIOS2_BA_REGNUM;
  812. return 1;
  813. default:
  814. return 0;
  815. }
  816. return 0;
  817. }
  818. /* Match and disassemble an indirect call instruction, with a register
  819. operand. Returns true on success, and fills in the operand pointer. */
  820. static int
  821. nios2_match_callr (uint32_t insn, const struct nios2_opcode *op,
  822. unsigned long mach, int *ra)
  823. {
  824. int is_r2 = (mach == bfd_mach_nios2r2);
  825. if (!is_r2 && op->match == MATCH_R1_CALLR)
  826. {
  827. *ra = GET_IW_I_A (insn);
  828. return 1;
  829. }
  830. else if (!is_r2)
  831. return 0;
  832. else if (op->match == MATCH_R2_CALLR)
  833. {
  834. *ra = GET_IW_F2I16_A (insn);
  835. return 1;
  836. }
  837. else if (op->match == MATCH_R2_CALLR_N)
  838. {
  839. *ra = GET_IW_F1X1_A (insn);
  840. return 1;
  841. }
  842. return 0;
  843. }
  844. /* Match and disassemble a break instruction, with an unsigned operand.
  845. Returns true on success, and fills in the operand pointer. */
  846. static int
  847. nios2_match_break (uint32_t insn, const struct nios2_opcode *op,
  848. unsigned long mach, unsigned int *uimm)
  849. {
  850. int is_r2 = (mach == bfd_mach_nios2r2);
  851. if (!is_r2 && op->match == MATCH_R1_BREAK)
  852. {
  853. *uimm = GET_IW_R_IMM5 (insn);
  854. return 1;
  855. }
  856. else if (!is_r2)
  857. return 0;
  858. else if (op->match == MATCH_R2_BREAK)
  859. {
  860. *uimm = GET_IW_F3X6L5_IMM5 (insn);
  861. return 1;
  862. }
  863. else if (op->match == MATCH_R2_BREAK_N)
  864. {
  865. *uimm = GET_IW_X2L5_IMM5 (insn);
  866. return 1;
  867. }
  868. return 0;
  869. }
  870. /* Match and disassemble a trap instruction, with an unsigned operand.
  871. Returns true on success, and fills in the operand pointer. */
  872. static int
  873. nios2_match_trap (uint32_t insn, const struct nios2_opcode *op,
  874. unsigned long mach, unsigned int *uimm)
  875. {
  876. int is_r2 = (mach == bfd_mach_nios2r2);
  877. if (!is_r2 && op->match == MATCH_R1_TRAP)
  878. {
  879. *uimm = GET_IW_R_IMM5 (insn);
  880. return 1;
  881. }
  882. else if (!is_r2)
  883. return 0;
  884. else if (op->match == MATCH_R2_TRAP)
  885. {
  886. *uimm = GET_IW_F3X6L5_IMM5 (insn);
  887. return 1;
  888. }
  889. else if (op->match == MATCH_R2_TRAP_N)
  890. {
  891. *uimm = GET_IW_X2L5_IMM5 (insn);
  892. return 1;
  893. }
  894. return 0;
  895. }
  896. /* Helper function to identify when we're in a function epilogue;
  897. that is, the part of the function from the point at which the
  898. stack adjustments are made, to the return or sibcall.
  899. Note that we may have several stack adjustment instructions, and
  900. this function needs to test whether the stack teardown has already
  901. started before current_pc, not whether it has completed. */
  902. static int
  903. nios2_in_epilogue_p (struct gdbarch *gdbarch,
  904. CORE_ADDR current_pc,
  905. CORE_ADDR start_pc)
  906. {
  907. unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
  908. int is_r2 = (mach == bfd_mach_nios2r2);
  909. /* Maximum number of possibly-epilogue instructions to check.
  910. Note that this number should not be too large, else we can
  911. potentially end up iterating through unmapped memory. */
  912. int ninsns, max_insns = 5;
  913. unsigned int insn;
  914. const struct nios2_opcode *op = NULL;
  915. unsigned int uimm;
  916. int imm;
  917. int wb, id, ret;
  918. int ra, rb, rc;
  919. enum branch_condition cond;
  920. CORE_ADDR pc;
  921. /* There has to be a previous instruction in the function. */
  922. if (current_pc <= start_pc)
  923. return 0;
  924. /* Find the previous instruction before current_pc. For R2, it might
  925. be either a 16-bit or 32-bit instruction; the only way to know for
  926. sure is to scan through from the beginning of the function,
  927. disassembling as we go. */
  928. if (is_r2)
  929. for (pc = start_pc; ; )
  930. {
  931. op = nios2_fetch_insn (gdbarch, pc, &insn);
  932. if (op == NULL)
  933. return 0;
  934. if (pc + op->size < current_pc)
  935. pc += op->size;
  936. else
  937. break;
  938. /* We can skip over insns to a forward branch target. Since
  939. the branch offset is relative to the next instruction,
  940. it's correct to do this after incrementing the pc above. */
  941. if (nios2_match_branch (insn, op, mach, &ra, &rb, &imm, &cond)
  942. && imm > 0
  943. && pc + imm < current_pc)
  944. pc += imm;
  945. }
  946. /* Otherwise just go back to the previous 32-bit insn. */
  947. else
  948. pc = current_pc - NIOS2_OPCODE_SIZE;
  949. /* Beginning with the previous instruction we just located, check whether
  950. we are in a sequence of at least one stack adjustment instruction.
  951. Possible instructions here include:
  952. ADDI sp, sp, n
  953. ADD sp, sp, rn
  954. LDW sp, n(sp)
  955. SPINCI.N n
  956. LDWSP.N sp, n(sp)
  957. LDWM {reglist}, (sp)++, wb */
  958. for (ninsns = 0; ninsns < max_insns; ninsns++)
  959. {
  960. int ok = 0;
  961. /* Fetch the insn at pc. */
  962. op = nios2_fetch_insn (gdbarch, pc, &insn);
  963. if (op == NULL)
  964. return 0;
  965. pc += op->size;
  966. /* Was it a stack adjustment? */
  967. if (nios2_match_addi (insn, op, mach, &ra, &rb, &imm))
  968. ok = (rb == NIOS2_SP_REGNUM);
  969. else if (nios2_match_add (insn, op, mach, &ra, &rb, &rc))
  970. ok = (rc == NIOS2_SP_REGNUM);
  971. else if (nios2_match_ldw (insn, op, mach, &ra, &rb, &imm))
  972. ok = (rb == NIOS2_SP_REGNUM);
  973. else if (nios2_match_ldwm (insn, op, mach, &uimm, &ra,
  974. &imm, &wb, &ret, &id))
  975. ok = (ra == NIOS2_SP_REGNUM && wb && id);
  976. if (!ok)
  977. break;
  978. }
  979. /* No stack adjustments found. */
  980. if (ninsns == 0)
  981. return 0;
  982. /* We found more stack adjustments than we expect GCC to be generating.
  983. Since it looks like a stack unwind might be in progress tell GDB to
  984. treat it as such. */
  985. if (ninsns == max_insns)
  986. return 1;
  987. /* The next instruction following the stack adjustments must be a
  988. return, jump, or unconditional branch, or a CDX pop.n or ldwm
  989. that does an implicit return. */
  990. if (nios2_match_jmpr (insn, op, mach, &ra)
  991. || nios2_match_jmpi (insn, op, mach, &uimm)
  992. || (nios2_match_ldwm (insn, op, mach, &uimm, &ra, &imm, &wb, &id, &ret)
  993. && ret)
  994. || (nios2_match_branch (insn, op, mach, &ra, &rb, &imm, &cond)
  995. && cond == branch_none))
  996. return 1;
  997. return 0;
  998. }
  999. /* Implement the stack_frame_destroyed_p gdbarch method. */
  1000. static int
  1001. nios2_stack_frame_destroyed_p (struct gdbarch *gdbarch, CORE_ADDR pc)
  1002. {
  1003. CORE_ADDR func_addr;
  1004. if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
  1005. return nios2_in_epilogue_p (gdbarch, pc, func_addr);
  1006. return 0;
  1007. }
  1008. /* Do prologue analysis, returning the PC of the first instruction
  1009. after the function prologue. Assumes CACHE has already been
  1010. initialized. THIS_FRAME can be null, in which case we are only
  1011. interested in skipping the prologue. Otherwise CACHE is filled in
  1012. from the frame information.
  1013. The prologue may consist of the following parts:
  1014. 1) Profiling instrumentation. For non-PIC code it looks like:
  1015. mov r8, ra
  1016. call mcount
  1017. mov ra, r8
  1018. 2) A stack adjustment and save of R4-R7 for varargs functions.
  1019. For R2 CDX this is typically handled with a STWM, otherwise
  1020. this is typically merged with item 3.
  1021. 3) A stack adjustment and save of the callee-saved registers.
  1022. For R2 CDX these are typically handled with a PUSH.N or STWM,
  1023. otherwise as an explicit SP decrement and individual register
  1024. saves.
  1025. There may also be a stack switch here in an exception handler
  1026. in place of a stack adjustment. It looks like:
  1027. movhi rx, %hiadj(newstack)
  1028. addhi rx, rx, %lo(newstack)
  1029. stw sp, constant(rx)
  1030. mov sp, rx
  1031. 4) A frame pointer save, which can be either a MOV or ADDI.
  1032. 5) A further stack pointer adjustment. This is normally included
  1033. adjustment in step 3 unless the total adjustment is too large
  1034. to be done in one step.
  1035. 7) A stack overflow check, which can take either of these forms:
  1036. bgeu sp, rx, +8
  1037. trap 3
  1038. or
  1039. bltu sp, rx, .Lstack_overflow
  1040. ...
  1041. .Lstack_overflow:
  1042. trap 3
  1043. Older versions of GCC emitted "break 3" instead of "trap 3" here,
  1044. so we check for both cases.
  1045. Older GCC versions emitted stack overflow checks after the SP
  1046. adjustments in both steps 3 and 4. Starting with GCC 6, there is
  1047. at most one overflow check, which is placed before the first
  1048. stack adjustment for R2 CDX and after the first stack adjustment
  1049. otherwise.
  1050. The prologue instructions may be combined or interleaved with other
  1051. instructions.
  1052. To cope with all this variability we decode all the instructions
  1053. from the start of the prologue until we hit an instruction that
  1054. cannot possibly be a prologue instruction, such as a branch, call,
  1055. return, or epilogue instruction. The prologue is considered to end
  1056. at the last instruction that can definitely be considered a
  1057. prologue instruction. */
  1058. static CORE_ADDR
  1059. nios2_analyze_prologue (struct gdbarch *gdbarch, const CORE_ADDR start_pc,
  1060. const CORE_ADDR current_pc,
  1061. struct nios2_unwind_cache *cache,
  1062. struct frame_info *this_frame)
  1063. {
  1064. /* Maximum number of possibly-prologue instructions to check.
  1065. Note that this number should not be too large, else we can
  1066. potentially end up iterating through unmapped memory. */
  1067. int ninsns, max_insns = 50;
  1068. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1069. unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
  1070. /* Does the frame set up the FP register? */
  1071. int base_reg = 0;
  1072. struct reg_value *value = cache->reg_value;
  1073. struct reg_value temp_value[NIOS2_NUM_REGS];
  1074. /* Save the starting PC so we can correct the pc after running
  1075. through the prolog, using symbol info. */
  1076. CORE_ADDR pc = start_pc;
  1077. /* Is this an exception handler? */
  1078. int exception_handler = 0;
  1079. /* What was the original value of SP (or fake original value for
  1080. functions which switch stacks? */
  1081. CORE_ADDR frame_high;
  1082. /* The last definitely-prologue instruction seen. */
  1083. CORE_ADDR prologue_end;
  1084. /* Is this the innermost function? */
  1085. int innermost = (this_frame ? (frame_relative_level (this_frame) == 0) : 1);
  1086. if (nios2_debug)
  1087. gdb_printf (gdb_stdlog,
  1088. "{ nios2_analyze_prologue start=%s, current=%s ",
  1089. paddress (gdbarch, start_pc),
  1090. paddress (gdbarch, current_pc));
  1091. /* Set up the default values of the registers. */
  1092. nios2_setup_default (cache);
  1093. /* Find the prologue instructions. */
  1094. prologue_end = start_pc;
  1095. for (ninsns = 0; ninsns < max_insns; ninsns++)
  1096. {
  1097. /* Present instruction. */
  1098. uint32_t insn;
  1099. const struct nios2_opcode *op;
  1100. int ra, rb, rc, imm;
  1101. unsigned int uimm;
  1102. unsigned int reglist;
  1103. int wb, id, ret;
  1104. enum branch_condition cond;
  1105. if (pc == current_pc)
  1106. {
  1107. /* When we reach the current PC we must save the current
  1108. register state (for the backtrace) but keep analysing
  1109. because there might be more to find out (eg. is this an
  1110. exception handler). */
  1111. memcpy (temp_value, value, sizeof (temp_value));
  1112. value = temp_value;
  1113. if (nios2_debug)
  1114. gdb_printf (gdb_stdlog, "*");
  1115. }
  1116. op = nios2_fetch_insn (gdbarch, pc, &insn);
  1117. /* Unknown opcode? Stop scanning. */
  1118. if (op == NULL)
  1119. break;
  1120. pc += op->size;
  1121. if (nios2_debug)
  1122. {
  1123. if (op->size == 2)
  1124. gdb_printf (gdb_stdlog, "[%04X]", insn & 0xffff);
  1125. else
  1126. gdb_printf (gdb_stdlog, "[%08X]", insn);
  1127. }
  1128. /* The following instructions can appear in the prologue. */
  1129. if (nios2_match_add (insn, op, mach, &ra, &rb, &rc))
  1130. {
  1131. /* ADD rc, ra, rb (also used for MOV) */
  1132. if (rc == NIOS2_SP_REGNUM
  1133. && rb == 0
  1134. && value[ra].reg == cache->reg_saved[NIOS2_SP_REGNUM].basereg)
  1135. {
  1136. /* If the previous value of SP is available somewhere
  1137. near the new stack pointer value then this is a
  1138. stack switch. */
  1139. /* If any registers were saved on the stack before then
  1140. we can't backtrace into them now. */
  1141. for (int i = 0 ; i < NIOS2_NUM_REGS ; i++)
  1142. {
  1143. if (cache->reg_saved[i].basereg == NIOS2_SP_REGNUM)
  1144. cache->reg_saved[i].basereg = -1;
  1145. if (value[i].reg == NIOS2_SP_REGNUM)
  1146. value[i].reg = -1;
  1147. }
  1148. /* Create a fake "high water mark" 4 bytes above where SP
  1149. was stored and fake up the registers to be consistent
  1150. with that. */
  1151. value[NIOS2_SP_REGNUM].reg = NIOS2_SP_REGNUM;
  1152. value[NIOS2_SP_REGNUM].offset
  1153. = (value[ra].offset
  1154. - cache->reg_saved[NIOS2_SP_REGNUM].addr
  1155. - 4);
  1156. cache->reg_saved[NIOS2_SP_REGNUM].basereg = NIOS2_SP_REGNUM;
  1157. cache->reg_saved[NIOS2_SP_REGNUM].addr = -4;
  1158. }
  1159. else if (rc == NIOS2_SP_REGNUM && ra == NIOS2_FP_REGNUM)
  1160. /* This is setting SP from FP. This only happens in the
  1161. function epilogue. */
  1162. break;
  1163. else if (rc != 0)
  1164. {
  1165. if (value[rb].reg == 0)
  1166. value[rc].reg = value[ra].reg;
  1167. else if (value[ra].reg == 0)
  1168. value[rc].reg = value[rb].reg;
  1169. else
  1170. value[rc].reg = -1;
  1171. value[rc].offset = value[ra].offset + value[rb].offset;
  1172. }
  1173. /* The add/move is only considered a prologue instruction
  1174. if the destination is SP or FP. */
  1175. if (rc == NIOS2_SP_REGNUM || rc == NIOS2_FP_REGNUM)
  1176. prologue_end = pc;
  1177. }
  1178. else if (nios2_match_sub (insn, op, mach, &ra, &rb, &rc))
  1179. {
  1180. /* SUB rc, ra, rb */
  1181. if (rc == NIOS2_SP_REGNUM && rb == NIOS2_SP_REGNUM
  1182. && value[rc].reg != 0)
  1183. /* If we are decrementing the SP by a non-constant amount,
  1184. this is alloca, not part of the prologue. */
  1185. break;
  1186. else if (rc != 0)
  1187. {
  1188. if (value[rb].reg == 0)
  1189. value[rc].reg = value[ra].reg;
  1190. else
  1191. value[rc].reg = -1;
  1192. value[rc].offset = value[ra].offset - value[rb].offset;
  1193. }
  1194. }
  1195. else if (nios2_match_addi (insn, op, mach, &ra, &rb, &imm))
  1196. {
  1197. /* ADDI rb, ra, imm */
  1198. /* A positive stack adjustment has to be part of the epilogue. */
  1199. if (rb == NIOS2_SP_REGNUM
  1200. && (imm > 0 || value[ra].reg != NIOS2_SP_REGNUM))
  1201. break;
  1202. /* Likewise restoring SP from FP. */
  1203. else if (rb == NIOS2_SP_REGNUM && ra == NIOS2_FP_REGNUM)
  1204. break;
  1205. if (rb != 0)
  1206. {
  1207. value[rb].reg = value[ra].reg;
  1208. value[rb].offset = value[ra].offset + imm;
  1209. }
  1210. /* The add is only considered a prologue instruction
  1211. if the destination is SP or FP. */
  1212. if (rb == NIOS2_SP_REGNUM || rb == NIOS2_FP_REGNUM)
  1213. prologue_end = pc;
  1214. }
  1215. else if (nios2_match_orhi (insn, op, mach, &ra, &rb, &uimm))
  1216. {
  1217. /* ORHI rb, ra, uimm (also used for MOVHI) */
  1218. if (rb != 0)
  1219. {
  1220. value[rb].reg = (value[ra].reg == 0) ? 0 : -1;
  1221. value[rb].offset = value[ra].offset | (uimm << 16);
  1222. }
  1223. }
  1224. else if (nios2_match_stw (insn, op, mach, &ra, &rb, &imm))
  1225. {
  1226. /* STW rb, imm(ra) */
  1227. /* Are we storing the original value of a register to the stack?
  1228. For exception handlers the value of EA-4 (return
  1229. address from interrupts etc) is sometimes stored. */
  1230. int orig = value[rb].reg;
  1231. if (orig > 0
  1232. && (value[rb].offset == 0
  1233. || (orig == NIOS2_EA_REGNUM && value[rb].offset == -4))
  1234. && value[ra].reg == NIOS2_SP_REGNUM)
  1235. {
  1236. if (pc < current_pc)
  1237. {
  1238. /* Save off callee saved registers. */
  1239. cache->reg_saved[orig].basereg = value[ra].reg;
  1240. cache->reg_saved[orig].addr = value[ra].offset + imm;
  1241. }
  1242. prologue_end = pc;
  1243. if (orig == NIOS2_EA_REGNUM || orig == NIOS2_ESTATUS_REGNUM)
  1244. exception_handler = 1;
  1245. }
  1246. else
  1247. /* Non-stack memory writes cannot appear in the prologue. */
  1248. break;
  1249. }
  1250. else if (nios2_match_stwm (insn, op, mach,
  1251. &reglist, &ra, &imm, &wb, &id))
  1252. {
  1253. /* PUSH.N {reglist}, adjust
  1254. or
  1255. STWM {reglist}, --(SP)[, writeback] */
  1256. int off = 0;
  1257. if (ra != NIOS2_SP_REGNUM || id != 0)
  1258. /* This is a non-stack-push memory write and cannot be
  1259. part of the prologue. */
  1260. break;
  1261. for (int i = 31; i >= 0; i--)
  1262. if (reglist & (1 << i))
  1263. {
  1264. int orig = value[i].reg;
  1265. off += 4;
  1266. if (orig > 0 && value[i].offset == 0 && pc < current_pc)
  1267. {
  1268. cache->reg_saved[orig].basereg
  1269. = value[NIOS2_SP_REGNUM].reg;
  1270. cache->reg_saved[orig].addr
  1271. = value[NIOS2_SP_REGNUM].offset - off;
  1272. }
  1273. }
  1274. if (wb)
  1275. value[NIOS2_SP_REGNUM].offset -= off;
  1276. value[NIOS2_SP_REGNUM].offset -= imm;
  1277. prologue_end = pc;
  1278. }
  1279. else if (nios2_match_rdctl (insn, op, mach, &ra, &rc))
  1280. {
  1281. /* RDCTL rC, ctlN
  1282. This can appear in exception handlers in combination with
  1283. a subsequent save to the stack frame. */
  1284. if (rc != 0)
  1285. {
  1286. value[rc].reg = NIOS2_STATUS_REGNUM + ra;
  1287. value[rc].offset = 0;
  1288. }
  1289. }
  1290. else if (nios2_match_calli (insn, op, mach, &uimm))
  1291. {
  1292. if (value[8].reg == NIOS2_RA_REGNUM
  1293. && value[8].offset == 0
  1294. && value[NIOS2_SP_REGNUM].reg == NIOS2_SP_REGNUM
  1295. && value[NIOS2_SP_REGNUM].offset == 0)
  1296. {
  1297. /* A CALL instruction. This is treated as a call to mcount
  1298. if ra has been stored into r8 beforehand and if it's
  1299. before the stack adjust.
  1300. Note mcount corrupts r2-r3, r9-r15 & ra. */
  1301. for (int i = 2 ; i <= 3 ; i++)
  1302. value[i].reg = -1;
  1303. for (int i = 9 ; i <= 15 ; i++)
  1304. value[i].reg = -1;
  1305. value[NIOS2_RA_REGNUM].reg = -1;
  1306. prologue_end = pc;
  1307. }
  1308. /* Other calls are not part of the prologue. */
  1309. else
  1310. break;
  1311. }
  1312. else if (nios2_match_branch (insn, op, mach, &ra, &rb, &imm, &cond))
  1313. {
  1314. /* Branches not involving a stack overflow check aren't part of
  1315. the prologue. */
  1316. if (ra != NIOS2_SP_REGNUM)
  1317. break;
  1318. else if (cond == branch_geu)
  1319. {
  1320. /* BGEU sp, rx, +8
  1321. TRAP 3 (or BREAK 3)
  1322. This instruction sequence is used in stack checking;
  1323. we can ignore it. */
  1324. unsigned int next_insn;
  1325. const struct nios2_opcode *next_op
  1326. = nios2_fetch_insn (gdbarch, pc, &next_insn);
  1327. if (next_op != NULL
  1328. && (nios2_match_trap (next_insn, op, mach, &uimm)
  1329. || nios2_match_break (next_insn, op, mach, &uimm)))
  1330. pc += next_op->size;
  1331. else
  1332. break;
  1333. }
  1334. else if (cond == branch_ltu)
  1335. {
  1336. /* BLTU sp, rx, .Lstackoverflow
  1337. If the location branched to holds a TRAP or BREAK
  1338. instruction then this is also stack overflow detection. */
  1339. unsigned int next_insn;
  1340. const struct nios2_opcode *next_op
  1341. = nios2_fetch_insn (gdbarch, pc + imm, &next_insn);
  1342. if (next_op != NULL
  1343. && (nios2_match_trap (next_insn, op, mach, &uimm)
  1344. || nios2_match_break (next_insn, op, mach, &uimm)))
  1345. ;
  1346. else
  1347. break;
  1348. }
  1349. else
  1350. break;
  1351. }
  1352. /* All other calls, jumps, returns, TRAPs, or BREAKs terminate
  1353. the prologue. */
  1354. else if (nios2_match_callr (insn, op, mach, &ra)
  1355. || nios2_match_jmpr (insn, op, mach, &ra)
  1356. || nios2_match_jmpi (insn, op, mach, &uimm)
  1357. || (nios2_match_ldwm (insn, op, mach, &reglist, &ra,
  1358. &imm, &wb, &id, &ret)
  1359. && ret)
  1360. || nios2_match_trap (insn, op, mach, &uimm)
  1361. || nios2_match_break (insn, op, mach, &uimm))
  1362. break;
  1363. }
  1364. /* If THIS_FRAME is NULL, we are being called from skip_prologue
  1365. and are only interested in the PROLOGUE_END value, so just
  1366. return that now and skip over the cache updates, which depend
  1367. on having frame information. */
  1368. if (this_frame == NULL)
  1369. return prologue_end;
  1370. /* If we are in the function epilogue and have already popped
  1371. registers off the stack in preparation for returning, then we
  1372. want to go back to the original register values. */
  1373. if (innermost && nios2_in_epilogue_p (gdbarch, current_pc, start_pc))
  1374. nios2_setup_default (cache);
  1375. /* Exception handlers use a different return address register. */
  1376. if (exception_handler)
  1377. cache->return_regnum = NIOS2_EA_REGNUM;
  1378. if (nios2_debug)
  1379. gdb_printf (gdb_stdlog, "\n-> retreg=%d, ", cache->return_regnum);
  1380. if (cache->reg_value[NIOS2_FP_REGNUM].reg == NIOS2_SP_REGNUM)
  1381. /* If the FP now holds an offset from the CFA then this is a
  1382. normal frame which uses the frame pointer. */
  1383. base_reg = NIOS2_FP_REGNUM;
  1384. else if (cache->reg_value[NIOS2_SP_REGNUM].reg == NIOS2_SP_REGNUM)
  1385. /* FP doesn't hold an offset from the CFA. If SP still holds an
  1386. offset from the CFA then we might be in a function which omits
  1387. the frame pointer, or we might be partway through the prologue.
  1388. In both cases we can find the CFA using SP. */
  1389. base_reg = NIOS2_SP_REGNUM;
  1390. else
  1391. {
  1392. /* Somehow the stack pointer has been corrupted.
  1393. We can't return. */
  1394. if (nios2_debug)
  1395. gdb_printf (gdb_stdlog, "<can't reach cfa> }\n");
  1396. return 0;
  1397. }
  1398. if (cache->reg_value[base_reg].offset == 0
  1399. || cache->reg_saved[NIOS2_RA_REGNUM].basereg != NIOS2_SP_REGNUM
  1400. || cache->reg_saved[cache->return_regnum].basereg != NIOS2_SP_REGNUM)
  1401. {
  1402. /* If the frame didn't adjust the stack, didn't save RA or
  1403. didn't save EA in an exception handler then it must either
  1404. be a leaf function (doesn't call any other functions) or it
  1405. can't return. If it has called another function then it
  1406. can't be a leaf, so set base == 0 to indicate that we can't
  1407. backtrace past it. */
  1408. if (!innermost)
  1409. {
  1410. /* If it isn't the innermost function then it can't be a
  1411. leaf, unless it was interrupted. Check whether RA for
  1412. this frame is the same as PC. If so then it probably
  1413. wasn't interrupted. */
  1414. CORE_ADDR ra
  1415. = get_frame_register_unsigned (this_frame, NIOS2_RA_REGNUM);
  1416. if (ra == current_pc)
  1417. {
  1418. if (nios2_debug)
  1419. gdb_printf
  1420. (gdb_stdlog,
  1421. "<noreturn ADJUST %s, r31@r%d+?>, r%d@r%d+?> }\n",
  1422. paddress (gdbarch, cache->reg_value[base_reg].offset),
  1423. cache->reg_saved[NIOS2_RA_REGNUM].basereg,
  1424. cache->return_regnum,
  1425. cache->reg_saved[cache->return_regnum].basereg);
  1426. return 0;
  1427. }
  1428. }
  1429. }
  1430. /* Get the value of whichever register we are using for the
  1431. base. */
  1432. cache->base = get_frame_register_unsigned (this_frame, base_reg);
  1433. /* What was the value of SP at the start of this function (or just
  1434. after the stack switch). */
  1435. frame_high = cache->base - cache->reg_value[base_reg].offset;
  1436. /* Adjust all the saved registers such that they contain addresses
  1437. instead of offsets. */
  1438. for (int i = 0; i < NIOS2_NUM_REGS; i++)
  1439. if (cache->reg_saved[i].basereg == NIOS2_SP_REGNUM)
  1440. {
  1441. cache->reg_saved[i].basereg = NIOS2_Z_REGNUM;
  1442. cache->reg_saved[i].addr += frame_high;
  1443. }
  1444. for (int i = 0; i < NIOS2_NUM_REGS; i++)
  1445. if (cache->reg_saved[i].basereg == NIOS2_GP_REGNUM)
  1446. {
  1447. CORE_ADDR gp = get_frame_register_unsigned (this_frame,
  1448. NIOS2_GP_REGNUM);
  1449. for ( ; i < NIOS2_NUM_REGS; i++)
  1450. if (cache->reg_saved[i].basereg == NIOS2_GP_REGNUM)
  1451. {
  1452. cache->reg_saved[i].basereg = NIOS2_Z_REGNUM;
  1453. cache->reg_saved[i].addr += gp;
  1454. }
  1455. }
  1456. /* Work out what the value of SP was on the first instruction of
  1457. this function. If we didn't switch stacks then this can be
  1458. trivially computed from the base address. */
  1459. if (cache->reg_saved[NIOS2_SP_REGNUM].basereg == NIOS2_Z_REGNUM)
  1460. cache->cfa
  1461. = read_memory_unsigned_integer (cache->reg_saved[NIOS2_SP_REGNUM].addr,
  1462. 4, byte_order);
  1463. else
  1464. cache->cfa = frame_high;
  1465. /* Exception handlers restore ESTATUS into STATUS. */
  1466. if (exception_handler)
  1467. {
  1468. cache->reg_saved[NIOS2_STATUS_REGNUM]
  1469. = cache->reg_saved[NIOS2_ESTATUS_REGNUM];
  1470. cache->reg_saved[NIOS2_ESTATUS_REGNUM].basereg = -1;
  1471. }
  1472. if (nios2_debug)
  1473. gdb_printf (gdb_stdlog, "cfa=%s }\n",
  1474. paddress (gdbarch, cache->cfa));
  1475. return prologue_end;
  1476. }
  1477. /* Implement the skip_prologue gdbarch hook. */
  1478. static CORE_ADDR
  1479. nios2_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR start_pc)
  1480. {
  1481. CORE_ADDR func_addr;
  1482. struct nios2_unwind_cache cache;
  1483. /* See if we can determine the end of the prologue via the symbol
  1484. table. If so, then return either PC, or the PC after the
  1485. prologue, whichever is greater. */
  1486. if (find_pc_partial_function (start_pc, NULL, &func_addr, NULL))
  1487. {
  1488. CORE_ADDR post_prologue_pc
  1489. = skip_prologue_using_sal (gdbarch, func_addr);
  1490. if (post_prologue_pc != 0)
  1491. return std::max (start_pc, post_prologue_pc);
  1492. }
  1493. /* Prologue analysis does the rest.... */
  1494. nios2_init_cache (&cache, start_pc);
  1495. return nios2_analyze_prologue (gdbarch, start_pc, start_pc, &cache, NULL);
  1496. }
  1497. /* Implement the breakpoint_kind_from_pc gdbarch method. */
  1498. static int
  1499. nios2_breakpoint_kind_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr)
  1500. {
  1501. unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
  1502. if (mach == bfd_mach_nios2r2)
  1503. {
  1504. unsigned int insn;
  1505. const struct nios2_opcode *op
  1506. = nios2_fetch_insn (gdbarch, *pcptr, &insn);
  1507. if (op && op->size == NIOS2_CDX_OPCODE_SIZE)
  1508. return NIOS2_CDX_OPCODE_SIZE;
  1509. else
  1510. return NIOS2_OPCODE_SIZE;
  1511. }
  1512. else
  1513. return NIOS2_OPCODE_SIZE;
  1514. }
  1515. /* Implement the sw_breakpoint_from_kind gdbarch method. */
  1516. static const gdb_byte *
  1517. nios2_sw_breakpoint_from_kind (struct gdbarch *gdbarch, int kind, int *size)
  1518. {
  1519. /* The Nios II ABI for Linux says: "Userspace programs should not use
  1520. the break instruction and userspace debuggers should not insert
  1521. one." and "Userspace breakpoints are accomplished using the trap
  1522. instruction with immediate operand 31 (all ones)."
  1523. So, we use "trap 31" consistently as the breakpoint on bare-metal
  1524. as well as Linux targets. */
  1525. /* R2 trap encoding:
  1526. ((0x2d << 26) | (0x1f << 21) | (0x1d << 16) | (0x20 << 0))
  1527. 0xb7fd0020
  1528. CDX trap.n encoding:
  1529. ((0xd << 12) | (0x1f << 6) | (0x9 << 0))
  1530. 0xd7c9
  1531. Note that code is always little-endian on R2. */
  1532. *size = kind;
  1533. if (kind == NIOS2_CDX_OPCODE_SIZE)
  1534. {
  1535. static const gdb_byte cdx_breakpoint_le[] = {0xc9, 0xd7};
  1536. return cdx_breakpoint_le;
  1537. }
  1538. else
  1539. {
  1540. unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
  1541. if (mach == bfd_mach_nios2r2)
  1542. {
  1543. static const gdb_byte r2_breakpoint_le[] = {0x20, 0x00, 0xfd, 0xb7};
  1544. return r2_breakpoint_le;
  1545. }
  1546. else
  1547. {
  1548. enum bfd_endian byte_order_for_code
  1549. = gdbarch_byte_order_for_code (gdbarch);
  1550. /* R1 trap encoding:
  1551. ((0x1d << 17) | (0x2d << 11) | (0x1f << 6) | (0x3a << 0))
  1552. 0x003b6ffa */
  1553. static const gdb_byte r1_breakpoint_le[] = {0xfa, 0x6f, 0x3b, 0x0};
  1554. static const gdb_byte r1_breakpoint_be[] = {0x0, 0x3b, 0x6f, 0xfa};
  1555. if (byte_order_for_code == BFD_ENDIAN_BIG)
  1556. return r1_breakpoint_be;
  1557. else
  1558. return r1_breakpoint_le;
  1559. }
  1560. }
  1561. }
  1562. /* Implement the frame_align gdbarch method. */
  1563. static CORE_ADDR
  1564. nios2_frame_align (struct gdbarch *gdbarch, CORE_ADDR addr)
  1565. {
  1566. return align_down (addr, 4);
  1567. }
  1568. /* Implement the return_value gdbarch method. */
  1569. static enum return_value_convention
  1570. nios2_return_value (struct gdbarch *gdbarch, struct value *function,
  1571. struct type *type, struct regcache *regcache,
  1572. gdb_byte *readbuf, const gdb_byte *writebuf)
  1573. {
  1574. if (TYPE_LENGTH (type) > 8)
  1575. return RETURN_VALUE_STRUCT_CONVENTION;
  1576. if (readbuf)
  1577. nios2_extract_return_value (gdbarch, type, regcache, readbuf);
  1578. if (writebuf)
  1579. nios2_store_return_value (gdbarch, type, regcache, writebuf);
  1580. return RETURN_VALUE_REGISTER_CONVENTION;
  1581. }
  1582. /* Implement the push_dummy_call gdbarch method. */
  1583. static CORE_ADDR
  1584. nios2_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  1585. struct regcache *regcache, CORE_ADDR bp_addr,
  1586. int nargs, struct value **args, CORE_ADDR sp,
  1587. function_call_return_method return_method,
  1588. CORE_ADDR struct_addr)
  1589. {
  1590. int argreg;
  1591. int argnum;
  1592. int arg_space = 0;
  1593. int stack_offset = 0;
  1594. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1595. /* Set the return address register to point to the entry point of
  1596. the program, where a breakpoint lies in wait. */
  1597. regcache_cooked_write_signed (regcache, NIOS2_RA_REGNUM, bp_addr);
  1598. /* Now make space on the stack for the args. */
  1599. for (argnum = 0; argnum < nargs; argnum++)
  1600. arg_space += align_up (TYPE_LENGTH (value_type (args[argnum])), 4);
  1601. sp -= arg_space;
  1602. /* Initialize the register pointer. */
  1603. argreg = NIOS2_FIRST_ARGREG;
  1604. /* The struct_return pointer occupies the first parameter-passing
  1605. register. */
  1606. if (return_method == return_method_struct)
  1607. regcache_cooked_write_unsigned (regcache, argreg++, struct_addr);
  1608. /* Now load as many as possible of the first arguments into
  1609. registers, and push the rest onto the stack. Loop through args
  1610. from first to last. */
  1611. for (argnum = 0; argnum < nargs; argnum++)
  1612. {
  1613. const gdb_byte *val;
  1614. struct value *arg = args[argnum];
  1615. struct type *arg_type = check_typedef (value_type (arg));
  1616. int len = TYPE_LENGTH (arg_type);
  1617. val = value_contents (arg).data ();
  1618. /* Copy the argument to general registers or the stack in
  1619. register-sized pieces. Large arguments are split between
  1620. registers and stack. */
  1621. while (len > 0)
  1622. {
  1623. int partial_len = (len < 4 ? len : 4);
  1624. if (argreg <= NIOS2_LAST_ARGREG)
  1625. {
  1626. /* The argument is being passed in a register. */
  1627. CORE_ADDR regval = extract_unsigned_integer (val, partial_len,
  1628. byte_order);
  1629. regcache_cooked_write_unsigned (regcache, argreg, regval);
  1630. argreg++;
  1631. }
  1632. else
  1633. {
  1634. /* The argument is being passed on the stack. */
  1635. CORE_ADDR addr = sp + stack_offset;
  1636. write_memory (addr, val, partial_len);
  1637. stack_offset += align_up (partial_len, 4);
  1638. }
  1639. len -= partial_len;
  1640. val += partial_len;
  1641. }
  1642. }
  1643. regcache_cooked_write_signed (regcache, NIOS2_SP_REGNUM, sp);
  1644. /* Return adjusted stack pointer. */
  1645. return sp;
  1646. }
  1647. /* Implement the unwind_pc gdbarch method. */
  1648. static CORE_ADDR
  1649. nios2_unwind_pc (struct gdbarch *gdbarch, struct frame_info *next_frame)
  1650. {
  1651. gdb_byte buf[4];
  1652. frame_unwind_register (next_frame, NIOS2_PC_REGNUM, buf);
  1653. return extract_typed_address (buf, builtin_type (gdbarch)->builtin_func_ptr);
  1654. }
  1655. /* Use prologue analysis to fill in the register cache
  1656. *THIS_PROLOGUE_CACHE for THIS_FRAME. This function initializes
  1657. *THIS_PROLOGUE_CACHE first. */
  1658. static struct nios2_unwind_cache *
  1659. nios2_frame_unwind_cache (struct frame_info *this_frame,
  1660. void **this_prologue_cache)
  1661. {
  1662. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1663. CORE_ADDR current_pc;
  1664. struct nios2_unwind_cache *cache;
  1665. if (*this_prologue_cache)
  1666. return (struct nios2_unwind_cache *) *this_prologue_cache;
  1667. cache = FRAME_OBSTACK_ZALLOC (struct nios2_unwind_cache);
  1668. *this_prologue_cache = cache;
  1669. /* Zero all fields. */
  1670. nios2_init_cache (cache, get_frame_func (this_frame));
  1671. /* Prologue analysis does the rest... */
  1672. current_pc = get_frame_pc (this_frame);
  1673. if (cache->pc != 0)
  1674. nios2_analyze_prologue (gdbarch, cache->pc, current_pc, cache, this_frame);
  1675. return cache;
  1676. }
  1677. /* Implement the this_id function for the normal unwinder. */
  1678. static void
  1679. nios2_frame_this_id (struct frame_info *this_frame, void **this_cache,
  1680. struct frame_id *this_id)
  1681. {
  1682. struct nios2_unwind_cache *cache =
  1683. nios2_frame_unwind_cache (this_frame, this_cache);
  1684. /* This marks the outermost frame. */
  1685. if (cache->base == 0)
  1686. return;
  1687. *this_id = frame_id_build (cache->cfa, cache->pc);
  1688. }
  1689. /* Implement the prev_register function for the normal unwinder. */
  1690. static struct value *
  1691. nios2_frame_prev_register (struct frame_info *this_frame, void **this_cache,
  1692. int regnum)
  1693. {
  1694. struct nios2_unwind_cache *cache =
  1695. nios2_frame_unwind_cache (this_frame, this_cache);
  1696. gdb_assert (regnum >= 0 && regnum < NIOS2_NUM_REGS);
  1697. /* The PC of the previous frame is stored in the RA register of
  1698. the current frame. Frob regnum so that we pull the value from
  1699. the correct place. */
  1700. if (regnum == NIOS2_PC_REGNUM)
  1701. regnum = cache->return_regnum;
  1702. if (regnum == NIOS2_SP_REGNUM && cache->cfa)
  1703. return frame_unwind_got_constant (this_frame, regnum, cache->cfa);
  1704. /* If we've worked out where a register is stored then load it from
  1705. there. */
  1706. if (cache->reg_saved[regnum].basereg == NIOS2_Z_REGNUM)
  1707. return frame_unwind_got_memory (this_frame, regnum,
  1708. cache->reg_saved[regnum].addr);
  1709. return frame_unwind_got_register (this_frame, regnum, regnum);
  1710. }
  1711. /* Implement the this_base, this_locals, and this_args hooks
  1712. for the normal unwinder. */
  1713. static CORE_ADDR
  1714. nios2_frame_base_address (struct frame_info *this_frame, void **this_cache)
  1715. {
  1716. struct nios2_unwind_cache *info
  1717. = nios2_frame_unwind_cache (this_frame, this_cache);
  1718. return info->base;
  1719. }
  1720. /* Data structures for the normal prologue-analysis-based
  1721. unwinder. */
  1722. static const struct frame_unwind nios2_frame_unwind =
  1723. {
  1724. "nios2 prologue",
  1725. NORMAL_FRAME,
  1726. default_frame_unwind_stop_reason,
  1727. nios2_frame_this_id,
  1728. nios2_frame_prev_register,
  1729. NULL,
  1730. default_frame_sniffer
  1731. };
  1732. static const struct frame_base nios2_frame_base =
  1733. {
  1734. &nios2_frame_unwind,
  1735. nios2_frame_base_address,
  1736. nios2_frame_base_address,
  1737. nios2_frame_base_address
  1738. };
  1739. /* Fill in the register cache *THIS_CACHE for THIS_FRAME for use
  1740. in the stub unwinder. */
  1741. static struct trad_frame_cache *
  1742. nios2_stub_frame_cache (struct frame_info *this_frame, void **this_cache)
  1743. {
  1744. CORE_ADDR pc;
  1745. CORE_ADDR start_addr;
  1746. CORE_ADDR stack_addr;
  1747. struct trad_frame_cache *this_trad_cache;
  1748. struct gdbarch *gdbarch = get_frame_arch (this_frame);
  1749. if (*this_cache != NULL)
  1750. return (struct trad_frame_cache *) *this_cache;
  1751. this_trad_cache = trad_frame_cache_zalloc (this_frame);
  1752. *this_cache = this_trad_cache;
  1753. /* The return address is in the link register. */
  1754. trad_frame_set_reg_realreg (this_trad_cache,
  1755. gdbarch_pc_regnum (gdbarch),
  1756. NIOS2_RA_REGNUM);
  1757. /* Frame ID, since it's a frameless / stackless function, no stack
  1758. space is allocated and SP on entry is the current SP. */
  1759. pc = get_frame_pc (this_frame);
  1760. find_pc_partial_function (pc, NULL, &start_addr, NULL);
  1761. stack_addr = get_frame_register_unsigned (this_frame, NIOS2_SP_REGNUM);
  1762. trad_frame_set_id (this_trad_cache, frame_id_build (start_addr, stack_addr));
  1763. /* Assume that the frame's base is the same as the stack pointer. */
  1764. trad_frame_set_this_base (this_trad_cache, stack_addr);
  1765. return this_trad_cache;
  1766. }
  1767. /* Implement the this_id function for the stub unwinder. */
  1768. static void
  1769. nios2_stub_frame_this_id (struct frame_info *this_frame, void **this_cache,
  1770. struct frame_id *this_id)
  1771. {
  1772. struct trad_frame_cache *this_trad_cache
  1773. = nios2_stub_frame_cache (this_frame, this_cache);
  1774. trad_frame_get_id (this_trad_cache, this_id);
  1775. }
  1776. /* Implement the prev_register function for the stub unwinder. */
  1777. static struct value *
  1778. nios2_stub_frame_prev_register (struct frame_info *this_frame,
  1779. void **this_cache, int regnum)
  1780. {
  1781. struct trad_frame_cache *this_trad_cache
  1782. = nios2_stub_frame_cache (this_frame, this_cache);
  1783. return trad_frame_get_register (this_trad_cache, this_frame, regnum);
  1784. }
  1785. /* Implement the sniffer function for the stub unwinder.
  1786. This unwinder is used for cases where the normal
  1787. prologue-analysis-based unwinder can't work,
  1788. such as PLT stubs. */
  1789. static int
  1790. nios2_stub_frame_sniffer (const struct frame_unwind *self,
  1791. struct frame_info *this_frame, void **cache)
  1792. {
  1793. gdb_byte dummy[4];
  1794. CORE_ADDR pc = get_frame_address_in_block (this_frame);
  1795. /* Use the stub unwinder for unreadable code. */
  1796. if (target_read_memory (get_frame_pc (this_frame), dummy, 4) != 0)
  1797. return 1;
  1798. if (in_plt_section (pc))
  1799. return 1;
  1800. return 0;
  1801. }
  1802. /* Define the data structures for the stub unwinder. */
  1803. static const struct frame_unwind nios2_stub_frame_unwind =
  1804. {
  1805. "nios2 stub",
  1806. NORMAL_FRAME,
  1807. default_frame_unwind_stop_reason,
  1808. nios2_stub_frame_this_id,
  1809. nios2_stub_frame_prev_register,
  1810. NULL,
  1811. nios2_stub_frame_sniffer
  1812. };
  1813. /* Determine where to set a single step breakpoint while considering
  1814. branch prediction. */
  1815. static CORE_ADDR
  1816. nios2_get_next_pc (struct regcache *regcache, CORE_ADDR pc)
  1817. {
  1818. struct gdbarch *gdbarch = regcache->arch ();
  1819. nios2_gdbarch_tdep *tdep = (nios2_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1820. unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
  1821. unsigned int insn;
  1822. const struct nios2_opcode *op = nios2_fetch_insn (gdbarch, pc, &insn);
  1823. int ra;
  1824. int rb;
  1825. int imm;
  1826. unsigned int uimm;
  1827. int wb, id, ret;
  1828. enum branch_condition cond;
  1829. /* Do something stupid if we can't disassemble the insn at pc. */
  1830. if (op == NULL)
  1831. return pc + NIOS2_OPCODE_SIZE;
  1832. if (nios2_match_branch (insn, op, mach, &ra, &rb, &imm, &cond))
  1833. {
  1834. int ras = regcache_raw_get_signed (regcache, ra);
  1835. int rbs = regcache_raw_get_signed (regcache, rb);
  1836. unsigned int rau = regcache_raw_get_unsigned (regcache, ra);
  1837. unsigned int rbu = regcache_raw_get_unsigned (regcache, rb);
  1838. pc += op->size;
  1839. switch (cond)
  1840. {
  1841. case branch_none:
  1842. pc += imm;
  1843. break;
  1844. case branch_eq:
  1845. if (ras == rbs)
  1846. pc += imm;
  1847. break;
  1848. case branch_ne:
  1849. if (ras != rbs)
  1850. pc += imm;
  1851. break;
  1852. case branch_ge:
  1853. if (ras >= rbs)
  1854. pc += imm;
  1855. break;
  1856. case branch_geu:
  1857. if (rau >= rbu)
  1858. pc += imm;
  1859. break;
  1860. case branch_lt:
  1861. if (ras < rbs)
  1862. pc += imm;
  1863. break;
  1864. case branch_ltu:
  1865. if (rau < rbu)
  1866. pc += imm;
  1867. break;
  1868. default:
  1869. break;
  1870. }
  1871. }
  1872. else if (nios2_match_jmpi (insn, op, mach, &uimm))
  1873. pc = (pc & 0xf0000000) | uimm;
  1874. else if (nios2_match_calli (insn, op, mach, &uimm))
  1875. {
  1876. CORE_ADDR callto = (pc & 0xf0000000) | uimm;
  1877. if (tdep->is_kernel_helper != NULL
  1878. && tdep->is_kernel_helper (callto))
  1879. /* Step over call to kernel helper, which we cannot debug
  1880. from user space. */
  1881. pc += op->size;
  1882. else
  1883. pc = callto;
  1884. }
  1885. else if (nios2_match_jmpr (insn, op, mach, &ra))
  1886. pc = regcache_raw_get_unsigned (regcache, ra);
  1887. else if (nios2_match_callr (insn, op, mach, &ra))
  1888. {
  1889. CORE_ADDR callto = regcache_raw_get_unsigned (regcache, ra);
  1890. if (tdep->is_kernel_helper != NULL
  1891. && tdep->is_kernel_helper (callto))
  1892. /* Step over call to kernel helper. */
  1893. pc += op->size;
  1894. else
  1895. pc = callto;
  1896. }
  1897. else if (nios2_match_ldwm (insn, op, mach, &uimm, &ra, &imm, &wb, &id, &ret)
  1898. && ret)
  1899. {
  1900. /* If ra is in the reglist, we have to use the value saved in the
  1901. stack frame rather than the current value. */
  1902. if (uimm & (1 << NIOS2_RA_REGNUM))
  1903. pc = nios2_unwind_pc (gdbarch, get_current_frame ());
  1904. else
  1905. pc = regcache_raw_get_unsigned (regcache, NIOS2_RA_REGNUM);
  1906. }
  1907. else if (nios2_match_trap (insn, op, mach, &uimm) && uimm == 0)
  1908. {
  1909. if (tdep->syscall_next_pc != NULL)
  1910. return tdep->syscall_next_pc (get_current_frame (), op);
  1911. }
  1912. else
  1913. pc += op->size;
  1914. return pc;
  1915. }
  1916. /* Implement the software_single_step gdbarch method. */
  1917. static std::vector<CORE_ADDR>
  1918. nios2_software_single_step (struct regcache *regcache)
  1919. {
  1920. CORE_ADDR next_pc = nios2_get_next_pc (regcache, regcache_read_pc (regcache));
  1921. return {next_pc};
  1922. }
  1923. /* Implement the get_longjump_target gdbarch method. */
  1924. static int
  1925. nios2_get_longjmp_target (struct frame_info *frame, CORE_ADDR *pc)
  1926. {
  1927. struct gdbarch *gdbarch = get_frame_arch (frame);
  1928. nios2_gdbarch_tdep *tdep = (nios2_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1929. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1930. CORE_ADDR jb_addr = get_frame_register_unsigned (frame, NIOS2_R4_REGNUM);
  1931. gdb_byte buf[4];
  1932. if (target_read_memory (jb_addr + (tdep->jb_pc * 4), buf, 4))
  1933. return 0;
  1934. *pc = extract_unsigned_integer (buf, 4, byte_order);
  1935. return 1;
  1936. }
  1937. /* Implement the type_align gdbarch function. */
  1938. static ULONGEST
  1939. nios2_type_align (struct gdbarch *gdbarch, struct type *type)
  1940. {
  1941. switch (type->code ())
  1942. {
  1943. case TYPE_CODE_PTR:
  1944. case TYPE_CODE_FUNC:
  1945. case TYPE_CODE_FLAGS:
  1946. case TYPE_CODE_INT:
  1947. case TYPE_CODE_RANGE:
  1948. case TYPE_CODE_FLT:
  1949. case TYPE_CODE_ENUM:
  1950. case TYPE_CODE_REF:
  1951. case TYPE_CODE_RVALUE_REF:
  1952. case TYPE_CODE_CHAR:
  1953. case TYPE_CODE_BOOL:
  1954. case TYPE_CODE_DECFLOAT:
  1955. case TYPE_CODE_METHODPTR:
  1956. case TYPE_CODE_MEMBERPTR:
  1957. type = check_typedef (type);
  1958. return std::min<ULONGEST> (4, TYPE_LENGTH (type));
  1959. default:
  1960. return 0;
  1961. }
  1962. }
  1963. /* Implement the gcc_target_options gdbarch method. */
  1964. static std::string
  1965. nios2_gcc_target_options (struct gdbarch *gdbarch)
  1966. {
  1967. /* GCC doesn't know "-m32". */
  1968. return {};
  1969. }
  1970. /* Initialize the Nios II gdbarch. */
  1971. static struct gdbarch *
  1972. nios2_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  1973. {
  1974. struct gdbarch *gdbarch;
  1975. int i;
  1976. tdesc_arch_data_up tdesc_data;
  1977. const struct target_desc *tdesc = info.target_desc;
  1978. if (!tdesc_has_registers (tdesc))
  1979. /* Pick a default target description. */
  1980. tdesc = tdesc_nios2;
  1981. /* Check any target description for validity. */
  1982. if (tdesc_has_registers (tdesc))
  1983. {
  1984. const struct tdesc_feature *feature;
  1985. int valid_p;
  1986. feature = tdesc_find_feature (tdesc, "org.gnu.gdb.nios2.cpu");
  1987. if (feature == NULL)
  1988. return NULL;
  1989. tdesc_data = tdesc_data_alloc ();
  1990. valid_p = 1;
  1991. for (i = 0; i < NIOS2_NUM_REGS; i++)
  1992. valid_p &= tdesc_numbered_register (feature, tdesc_data.get (), i,
  1993. nios2_reg_names[i]);
  1994. if (!valid_p)
  1995. return NULL;
  1996. }
  1997. /* Find a candidate among the list of pre-declared architectures. */
  1998. arches = gdbarch_list_lookup_by_info (arches, &info);
  1999. if (arches != NULL)
  2000. return arches->gdbarch;
  2001. /* None found, create a new architecture from the information
  2002. provided. */
  2003. nios2_gdbarch_tdep *tdep = new nios2_gdbarch_tdep;
  2004. gdbarch = gdbarch_alloc (&info, tdep);
  2005. /* longjmp support not enabled by default. */
  2006. tdep->jb_pc = -1;
  2007. /* Data type sizes. */
  2008. set_gdbarch_ptr_bit (gdbarch, 32);
  2009. set_gdbarch_addr_bit (gdbarch, 32);
  2010. set_gdbarch_short_bit (gdbarch, 16);
  2011. set_gdbarch_int_bit (gdbarch, 32);
  2012. set_gdbarch_long_bit (gdbarch, 32);
  2013. set_gdbarch_long_long_bit (gdbarch, 64);
  2014. set_gdbarch_float_bit (gdbarch, 32);
  2015. set_gdbarch_double_bit (gdbarch, 64);
  2016. set_gdbarch_type_align (gdbarch, nios2_type_align);
  2017. set_gdbarch_float_format (gdbarch, floatformats_ieee_single);
  2018. set_gdbarch_double_format (gdbarch, floatformats_ieee_double);
  2019. /* The register set. */
  2020. set_gdbarch_num_regs (gdbarch, NIOS2_NUM_REGS);
  2021. set_gdbarch_sp_regnum (gdbarch, NIOS2_SP_REGNUM);
  2022. set_gdbarch_pc_regnum (gdbarch, NIOS2_PC_REGNUM); /* Pseudo register PC */
  2023. set_gdbarch_register_name (gdbarch, nios2_register_name);
  2024. set_gdbarch_register_type (gdbarch, nios2_register_type);
  2025. /* Provide register mappings for stabs and dwarf2. */
  2026. set_gdbarch_stab_reg_to_regnum (gdbarch, nios2_dwarf_reg_to_regnum);
  2027. set_gdbarch_dwarf2_reg_to_regnum (gdbarch, nios2_dwarf_reg_to_regnum);
  2028. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  2029. /* Call dummy code. */
  2030. set_gdbarch_frame_align (gdbarch, nios2_frame_align);
  2031. set_gdbarch_return_value (gdbarch, nios2_return_value);
  2032. set_gdbarch_skip_prologue (gdbarch, nios2_skip_prologue);
  2033. set_gdbarch_stack_frame_destroyed_p (gdbarch, nios2_stack_frame_destroyed_p);
  2034. set_gdbarch_breakpoint_kind_from_pc (gdbarch, nios2_breakpoint_kind_from_pc);
  2035. set_gdbarch_sw_breakpoint_from_kind (gdbarch, nios2_sw_breakpoint_from_kind);
  2036. set_gdbarch_unwind_pc (gdbarch, nios2_unwind_pc);
  2037. /* The dwarf2 unwinder will normally produce the best results if
  2038. the debug information is available, so register it first. */
  2039. dwarf2_append_unwinders (gdbarch);
  2040. frame_unwind_append_unwinder (gdbarch, &nios2_stub_frame_unwind);
  2041. frame_unwind_append_unwinder (gdbarch, &nios2_frame_unwind);
  2042. /* Single stepping. */
  2043. set_gdbarch_software_single_step (gdbarch, nios2_software_single_step);
  2044. /* Target options for compile. */
  2045. set_gdbarch_gcc_target_options (gdbarch, nios2_gcc_target_options);
  2046. /* Hook in ABI-specific overrides, if they have been registered. */
  2047. gdbarch_init_osabi (info, gdbarch);
  2048. if (tdep->jb_pc >= 0)
  2049. set_gdbarch_get_longjmp_target (gdbarch, nios2_get_longjmp_target);
  2050. frame_base_set_default (gdbarch, &nios2_frame_base);
  2051. /* Enable inferior call support. */
  2052. set_gdbarch_push_dummy_call (gdbarch, nios2_push_dummy_call);
  2053. if (tdesc_data != nullptr)
  2054. tdesc_use_registers (gdbarch, tdesc, std::move (tdesc_data));
  2055. return gdbarch;
  2056. }
  2057. void _initialize_nios2_tdep ();
  2058. void
  2059. _initialize_nios2_tdep ()
  2060. {
  2061. gdbarch_register (bfd_arch_nios2, nios2_gdbarch_init, NULL);
  2062. initialize_tdesc_nios2 ();
  2063. /* Allow debugging this file's internals. */
  2064. add_setshow_boolean_cmd ("nios2", class_maintenance, &nios2_debug,
  2065. _("Set Nios II debugging."),
  2066. _("Show Nios II debugging."),
  2067. _("When on, Nios II specific debugging is enabled."),
  2068. NULL,
  2069. NULL,
  2070. &setdebuglist, &showdebuglist);
  2071. }