m32c-tdep.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664
  1. /* Renesas M32C target-dependent code for GDB, the GNU debugger.
  2. Copyright (C) 2004-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "gdb/sim-m32c.h"
  16. #include "gdbtypes.h"
  17. #include "regcache.h"
  18. #include "arch-utils.h"
  19. #include "frame.h"
  20. #include "frame-unwind.h"
  21. #include "symtab.h"
  22. #include "gdbcore.h"
  23. #include "value.h"
  24. #include "reggroups.h"
  25. #include "prologue-value.h"
  26. #include "objfiles.h"
  27. #include "gdbarch.h"
  28. /* The m32c tdep structure. */
  29. static const reggroup *m32c_dma_reggroup;
  30. /* The type of a function that moves the value of REG between CACHE or
  31. BUF --- in either direction. */
  32. typedef enum register_status (m32c_write_reg_t) (struct m32c_reg *reg,
  33. struct regcache *cache,
  34. const gdb_byte *buf);
  35. typedef enum register_status (m32c_read_reg_t) (struct m32c_reg *reg,
  36. readable_regcache *cache,
  37. gdb_byte *buf);
  38. struct m32c_reg
  39. {
  40. /* The name of this register. */
  41. const char *name;
  42. /* Its type. */
  43. struct type *type;
  44. /* The architecture this register belongs to. */
  45. struct gdbarch *arch;
  46. /* Its GDB register number. */
  47. int num;
  48. /* Its sim register number. */
  49. int sim_num;
  50. /* Its DWARF register number, or -1 if it doesn't have one. */
  51. int dwarf_num;
  52. /* Register group memberships. */
  53. unsigned int general_p : 1;
  54. unsigned int dma_p : 1;
  55. unsigned int system_p : 1;
  56. unsigned int save_restore_p : 1;
  57. /* Functions to read its value from a regcache, and write its value
  58. to a regcache. */
  59. m32c_read_reg_t *read;
  60. m32c_write_reg_t *write;
  61. /* Data for READ and WRITE functions. The exact meaning depends on
  62. the specific functions selected; see the comments for those
  63. functions. */
  64. struct m32c_reg *rx, *ry;
  65. int n;
  66. };
  67. /* An overestimate of the number of raw and pseudoregisters we will
  68. have. The exact answer depends on the variant of the architecture
  69. at hand, but we can use this to declare statically allocated
  70. arrays, and bump it up when needed. */
  71. #define M32C_MAX_NUM_REGS (75)
  72. /* The largest assigned DWARF register number. */
  73. #define M32C_MAX_DWARF_REGNUM (40)
  74. struct m32c_gdbarch_tdep : gdbarch_tdep
  75. {
  76. /* All the registers for this variant, indexed by GDB register
  77. number, and the number of registers present. */
  78. struct m32c_reg regs[M32C_MAX_NUM_REGS] {};
  79. /* The number of valid registers. */
  80. int num_regs = 0;
  81. /* Interesting registers. These are pointers into REGS. */
  82. struct m32c_reg *pc = nullptr, *flg = nullptr;
  83. struct m32c_reg *r0 = nullptr, *r1 = nullptr, *r2 = nullptr, *r3 = nullptr,
  84. *a0 = nullptr, *a1 = nullptr;
  85. struct m32c_reg *r2r0 = nullptr, *r3r2r1r0 = nullptr, *r3r1r2r0 = nullptr;
  86. struct m32c_reg *sb = nullptr, *fb = nullptr, *sp = nullptr;
  87. /* A table indexed by DWARF register numbers, pointing into
  88. REGS. */
  89. struct m32c_reg *dwarf_regs[M32C_MAX_DWARF_REGNUM + 1] {};
  90. /* Types for this architecture. We can't use the builtin_type_foo
  91. types, because they're not initialized when building a gdbarch
  92. structure. */
  93. struct type *voyd = nullptr, *ptr_voyd = nullptr, *func_voyd = nullptr;
  94. struct type *uint8 = nullptr, *uint16 = nullptr;
  95. struct type *int8 = nullptr, *int16 = nullptr, *int32 = nullptr,
  96. *int64 = nullptr;
  97. /* The types for data address and code address registers. */
  98. struct type *data_addr_reg_type = nullptr, *code_addr_reg_type = nullptr;
  99. /* The number of bytes a return address pushed by a 'jsr' instruction
  100. occupies on the stack. */
  101. int ret_addr_bytes = 0;
  102. /* The number of bytes an address register occupies on the stack
  103. when saved by an 'enter' or 'pushm' instruction. */
  104. int push_addr_bytes = 0;
  105. };
  106. /* Types. */
  107. static void
  108. make_types (struct gdbarch *arch)
  109. {
  110. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
  111. unsigned long mach = gdbarch_bfd_arch_info (arch)->mach;
  112. int data_addr_reg_bits, code_addr_reg_bits;
  113. char type_name[50];
  114. #if 0
  115. /* This is used to clip CORE_ADDR values, so this value is
  116. appropriate both on the m32c, where pointers are 32 bits long,
  117. and on the m16c, where pointers are sixteen bits long, but there
  118. may be code above the 64k boundary. */
  119. set_gdbarch_addr_bit (arch, 24);
  120. #else
  121. /* GCC uses 32 bits for addrs in the dwarf info, even though
  122. only 16/24 bits are used. Setting addr_bit to 24 causes
  123. errors in reading the dwarf addresses. */
  124. set_gdbarch_addr_bit (arch, 32);
  125. #endif
  126. set_gdbarch_int_bit (arch, 16);
  127. switch (mach)
  128. {
  129. case bfd_mach_m16c:
  130. data_addr_reg_bits = 16;
  131. code_addr_reg_bits = 24;
  132. set_gdbarch_ptr_bit (arch, 16);
  133. tdep->ret_addr_bytes = 3;
  134. tdep->push_addr_bytes = 2;
  135. break;
  136. case bfd_mach_m32c:
  137. data_addr_reg_bits = 24;
  138. code_addr_reg_bits = 24;
  139. set_gdbarch_ptr_bit (arch, 32);
  140. tdep->ret_addr_bytes = 4;
  141. tdep->push_addr_bytes = 4;
  142. break;
  143. default:
  144. gdb_assert_not_reached ("unexpected mach");
  145. }
  146. /* The builtin_type_mumble variables are sometimes uninitialized when
  147. this is called, so we avoid using them. */
  148. tdep->voyd = arch_type (arch, TYPE_CODE_VOID, TARGET_CHAR_BIT, "void");
  149. tdep->ptr_voyd
  150. = arch_pointer_type (arch, gdbarch_ptr_bit (arch), NULL, tdep->voyd);
  151. tdep->func_voyd = lookup_function_type (tdep->voyd);
  152. xsnprintf (type_name, sizeof (type_name), "%s_data_addr_t",
  153. gdbarch_bfd_arch_info (arch)->printable_name);
  154. tdep->data_addr_reg_type
  155. = arch_pointer_type (arch, data_addr_reg_bits, type_name, tdep->voyd);
  156. xsnprintf (type_name, sizeof (type_name), "%s_code_addr_t",
  157. gdbarch_bfd_arch_info (arch)->printable_name);
  158. tdep->code_addr_reg_type
  159. = arch_pointer_type (arch, code_addr_reg_bits, type_name, tdep->func_voyd);
  160. tdep->uint8 = arch_integer_type (arch, 8, 1, "uint8_t");
  161. tdep->uint16 = arch_integer_type (arch, 16, 1, "uint16_t");
  162. tdep->int8 = arch_integer_type (arch, 8, 0, "int8_t");
  163. tdep->int16 = arch_integer_type (arch, 16, 0, "int16_t");
  164. tdep->int32 = arch_integer_type (arch, 32, 0, "int32_t");
  165. tdep->int64 = arch_integer_type (arch, 64, 0, "int64_t");
  166. }
  167. /* Register set. */
  168. static const char *
  169. m32c_register_name (struct gdbarch *gdbarch, int num)
  170. {
  171. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  172. return tdep->regs[num].name;
  173. }
  174. static struct type *
  175. m32c_register_type (struct gdbarch *arch, int reg_nr)
  176. {
  177. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
  178. return tdep->regs[reg_nr].type;
  179. }
  180. static int
  181. m32c_register_sim_regno (struct gdbarch *gdbarch, int reg_nr)
  182. {
  183. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  184. return tdep->regs[reg_nr].sim_num;
  185. }
  186. static int
  187. m32c_debug_info_reg_to_regnum (struct gdbarch *gdbarch, int reg_nr)
  188. {
  189. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  190. if (0 <= reg_nr && reg_nr <= M32C_MAX_DWARF_REGNUM
  191. && tdep->dwarf_regs[reg_nr])
  192. return tdep->dwarf_regs[reg_nr]->num;
  193. else
  194. /* The DWARF CFI code expects to see -1 for invalid register
  195. numbers. */
  196. return -1;
  197. }
  198. static int
  199. m32c_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
  200. const struct reggroup *group)
  201. {
  202. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  203. struct m32c_reg *reg = &tdep->regs[regnum];
  204. /* The anonymous raw registers aren't in any groups. */
  205. if (! reg->name)
  206. return 0;
  207. if (group == all_reggroup)
  208. return 1;
  209. if (group == general_reggroup
  210. && reg->general_p)
  211. return 1;
  212. if (group == m32c_dma_reggroup
  213. && reg->dma_p)
  214. return 1;
  215. if (group == system_reggroup
  216. && reg->system_p)
  217. return 1;
  218. /* Since the m32c DWARF register numbers refer to cooked registers, not
  219. raw registers, and frame_pop depends on the save and restore groups
  220. containing registers the DWARF CFI will actually mention, our save
  221. and restore groups are cooked registers, not raw registers. (This is
  222. why we can't use the default reggroup function.) */
  223. if ((group == save_reggroup
  224. || group == restore_reggroup)
  225. && reg->save_restore_p)
  226. return 1;
  227. return 0;
  228. }
  229. /* Register move functions. We declare them here using
  230. m32c_{read,write}_reg_t to check the types. */
  231. static m32c_read_reg_t m32c_raw_read;
  232. static m32c_read_reg_t m32c_banked_read;
  233. static m32c_read_reg_t m32c_sb_read;
  234. static m32c_read_reg_t m32c_part_read;
  235. static m32c_read_reg_t m32c_cat_read;
  236. static m32c_read_reg_t m32c_r3r2r1r0_read;
  237. static m32c_write_reg_t m32c_raw_write;
  238. static m32c_write_reg_t m32c_banked_write;
  239. static m32c_write_reg_t m32c_sb_write;
  240. static m32c_write_reg_t m32c_part_write;
  241. static m32c_write_reg_t m32c_cat_write;
  242. static m32c_write_reg_t m32c_r3r2r1r0_write;
  243. /* Copy the value of the raw register REG from CACHE to BUF. */
  244. static enum register_status
  245. m32c_raw_read (struct m32c_reg *reg, readable_regcache *cache, gdb_byte *buf)
  246. {
  247. return cache->raw_read (reg->num, buf);
  248. }
  249. /* Copy the value of the raw register REG from BUF to CACHE. */
  250. static enum register_status
  251. m32c_raw_write (struct m32c_reg *reg, struct regcache *cache,
  252. const gdb_byte *buf)
  253. {
  254. cache->raw_write (reg->num, buf);
  255. return REG_VALID;
  256. }
  257. /* Return the value of the 'flg' register in CACHE. */
  258. static int
  259. m32c_read_flg (readable_regcache *cache)
  260. {
  261. gdbarch *arch = cache->arch ();
  262. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
  263. ULONGEST flg;
  264. cache->raw_read (tdep->flg->num, &flg);
  265. return flg & 0xffff;
  266. }
  267. /* Evaluate the real register number of a banked register. */
  268. static struct m32c_reg *
  269. m32c_banked_register (struct m32c_reg *reg, readable_regcache *cache)
  270. {
  271. return ((m32c_read_flg (cache) & reg->n) ? reg->ry : reg->rx);
  272. }
  273. /* Move the value of a banked register from CACHE to BUF.
  274. If the value of the 'flg' register in CACHE has any of the bits
  275. masked in REG->n set, then read REG->ry. Otherwise, read
  276. REG->rx. */
  277. static enum register_status
  278. m32c_banked_read (struct m32c_reg *reg, readable_regcache *cache, gdb_byte *buf)
  279. {
  280. struct m32c_reg *bank_reg = m32c_banked_register (reg, cache);
  281. return cache->raw_read (bank_reg->num, buf);
  282. }
  283. /* Move the value of a banked register from BUF to CACHE.
  284. If the value of the 'flg' register in CACHE has any of the bits
  285. masked in REG->n set, then write REG->ry. Otherwise, write
  286. REG->rx. */
  287. static enum register_status
  288. m32c_banked_write (struct m32c_reg *reg, struct regcache *cache,
  289. const gdb_byte *buf)
  290. {
  291. struct m32c_reg *bank_reg = m32c_banked_register (reg, cache);
  292. cache->raw_write (bank_reg->num, buf);
  293. return REG_VALID;
  294. }
  295. /* Move the value of SB from CACHE to BUF. On bfd_mach_m32c, SB is a
  296. banked register; on bfd_mach_m16c, it's not. */
  297. static enum register_status
  298. m32c_sb_read (struct m32c_reg *reg, readable_regcache *cache, gdb_byte *buf)
  299. {
  300. if (gdbarch_bfd_arch_info (reg->arch)->mach == bfd_mach_m16c)
  301. return m32c_raw_read (reg->rx, cache, buf);
  302. else
  303. return m32c_banked_read (reg, cache, buf);
  304. }
  305. /* Move the value of SB from BUF to CACHE. On bfd_mach_m32c, SB is a
  306. banked register; on bfd_mach_m16c, it's not. */
  307. static enum register_status
  308. m32c_sb_write (struct m32c_reg *reg, struct regcache *cache, const gdb_byte *buf)
  309. {
  310. if (gdbarch_bfd_arch_info (reg->arch)->mach == bfd_mach_m16c)
  311. m32c_raw_write (reg->rx, cache, buf);
  312. else
  313. m32c_banked_write (reg, cache, buf);
  314. return REG_VALID;
  315. }
  316. /* Assuming REG uses m32c_part_read and m32c_part_write, set *OFFSET_P
  317. and *LEN_P to the offset and length, in bytes, of the part REG
  318. occupies in its underlying register. The offset is from the
  319. lower-addressed end, regardless of the architecture's endianness.
  320. (The M32C family is always little-endian, but let's keep those
  321. assumptions out of here.) */
  322. static void
  323. m32c_find_part (struct m32c_reg *reg, int *offset_p, int *len_p)
  324. {
  325. /* The length of the containing register, of which REG is one part. */
  326. int containing_len = TYPE_LENGTH (reg->rx->type);
  327. /* The length of one "element" in our imaginary array. */
  328. int elt_len = TYPE_LENGTH (reg->type);
  329. /* The offset of REG's "element" from the least significant end of
  330. the containing register. */
  331. int elt_offset = reg->n * elt_len;
  332. /* If we extend off the end, trim the length of the element. */
  333. if (elt_offset + elt_len > containing_len)
  334. {
  335. elt_len = containing_len - elt_offset;
  336. /* We shouldn't be declaring partial registers that go off the
  337. end of their containing registers. */
  338. gdb_assert (elt_len > 0);
  339. }
  340. /* Flip the offset around if we're big-endian. */
  341. if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
  342. elt_offset = TYPE_LENGTH (reg->rx->type) - elt_offset - elt_len;
  343. *offset_p = elt_offset;
  344. *len_p = elt_len;
  345. }
  346. /* Move the value of a partial register (r0h, intbl, etc.) from CACHE
  347. to BUF. Treating the value of the register REG->rx as an array of
  348. REG->type values, where higher indices refer to more significant
  349. bits, read the value of the REG->n'th element. */
  350. static enum register_status
  351. m32c_part_read (struct m32c_reg *reg, readable_regcache *cache, gdb_byte *buf)
  352. {
  353. int offset, len;
  354. memset (buf, 0, TYPE_LENGTH (reg->type));
  355. m32c_find_part (reg, &offset, &len);
  356. return cache->cooked_read_part (reg->rx->num, offset, len, buf);
  357. }
  358. /* Move the value of a banked register from BUF to CACHE.
  359. Treating the value of the register REG->rx as an array of REG->type
  360. values, where higher indices refer to more significant bits, write
  361. the value of the REG->n'th element. */
  362. static enum register_status
  363. m32c_part_write (struct m32c_reg *reg, struct regcache *cache,
  364. const gdb_byte *buf)
  365. {
  366. int offset, len;
  367. m32c_find_part (reg, &offset, &len);
  368. cache->cooked_write_part (reg->rx->num, offset, len, buf);
  369. return REG_VALID;
  370. }
  371. /* Move the value of REG from CACHE to BUF. REG's value is the
  372. concatenation of the values of the registers REG->rx and REG->ry,
  373. with REG->rx contributing the more significant bits. */
  374. static enum register_status
  375. m32c_cat_read (struct m32c_reg *reg, readable_regcache *cache, gdb_byte *buf)
  376. {
  377. int high_bytes = TYPE_LENGTH (reg->rx->type);
  378. int low_bytes = TYPE_LENGTH (reg->ry->type);
  379. enum register_status status;
  380. gdb_assert (TYPE_LENGTH (reg->type) == high_bytes + low_bytes);
  381. if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
  382. {
  383. status = cache->cooked_read (reg->rx->num, buf);
  384. if (status == REG_VALID)
  385. status = cache->cooked_read (reg->ry->num, buf + high_bytes);
  386. }
  387. else
  388. {
  389. status = cache->cooked_read (reg->rx->num, buf + low_bytes);
  390. if (status == REG_VALID)
  391. status = cache->cooked_read (reg->ry->num, buf);
  392. }
  393. return status;
  394. }
  395. /* Move the value of REG from CACHE to BUF. REG's value is the
  396. concatenation of the values of the registers REG->rx and REG->ry,
  397. with REG->rx contributing the more significant bits. */
  398. static enum register_status
  399. m32c_cat_write (struct m32c_reg *reg, struct regcache *cache,
  400. const gdb_byte *buf)
  401. {
  402. int high_bytes = TYPE_LENGTH (reg->rx->type);
  403. int low_bytes = TYPE_LENGTH (reg->ry->type);
  404. gdb_assert (TYPE_LENGTH (reg->type) == high_bytes + low_bytes);
  405. if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
  406. {
  407. cache->cooked_write (reg->rx->num, buf);
  408. cache->cooked_write (reg->ry->num, buf + high_bytes);
  409. }
  410. else
  411. {
  412. cache->cooked_write (reg->rx->num, buf + low_bytes);
  413. cache->cooked_write (reg->ry->num, buf);
  414. }
  415. return REG_VALID;
  416. }
  417. /* Copy the value of the raw register REG from CACHE to BUF. REG is
  418. the concatenation (from most significant to least) of r3, r2, r1,
  419. and r0. */
  420. static enum register_status
  421. m32c_r3r2r1r0_read (struct m32c_reg *reg, readable_regcache *cache, gdb_byte *buf)
  422. {
  423. gdbarch *arch = reg->arch;
  424. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
  425. int len = TYPE_LENGTH (tdep->r0->type);
  426. enum register_status status;
  427. if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
  428. {
  429. status = cache->cooked_read (tdep->r0->num, buf + len * 3);
  430. if (status == REG_VALID)
  431. status = cache->cooked_read (tdep->r1->num, buf + len * 2);
  432. if (status == REG_VALID)
  433. status = cache->cooked_read (tdep->r2->num, buf + len * 1);
  434. if (status == REG_VALID)
  435. status = cache->cooked_read (tdep->r3->num, buf);
  436. }
  437. else
  438. {
  439. status = cache->cooked_read (tdep->r0->num, buf);
  440. if (status == REG_VALID)
  441. status = cache->cooked_read (tdep->r1->num, buf + len * 1);
  442. if (status == REG_VALID)
  443. status = cache->cooked_read (tdep->r2->num, buf + len * 2);
  444. if (status == REG_VALID)
  445. status = cache->cooked_read (tdep->r3->num, buf + len * 3);
  446. }
  447. return status;
  448. }
  449. /* Copy the value of the raw register REG from BUF to CACHE. REG is
  450. the concatenation (from most significant to least) of r3, r2, r1,
  451. and r0. */
  452. static enum register_status
  453. m32c_r3r2r1r0_write (struct m32c_reg *reg, struct regcache *cache,
  454. const gdb_byte *buf)
  455. {
  456. gdbarch *arch = reg->arch;
  457. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
  458. int len = TYPE_LENGTH (tdep->r0->type);
  459. if (gdbarch_byte_order (reg->arch) == BFD_ENDIAN_BIG)
  460. {
  461. cache->cooked_write (tdep->r0->num, buf + len * 3);
  462. cache->cooked_write (tdep->r1->num, buf + len * 2);
  463. cache->cooked_write (tdep->r2->num, buf + len * 1);
  464. cache->cooked_write (tdep->r3->num, buf);
  465. }
  466. else
  467. {
  468. cache->cooked_write (tdep->r0->num, buf);
  469. cache->cooked_write (tdep->r1->num, buf + len * 1);
  470. cache->cooked_write (tdep->r2->num, buf + len * 2);
  471. cache->cooked_write (tdep->r3->num, buf + len * 3);
  472. }
  473. return REG_VALID;
  474. }
  475. static enum register_status
  476. m32c_pseudo_register_read (struct gdbarch *arch,
  477. readable_regcache *cache,
  478. int cookednum,
  479. gdb_byte *buf)
  480. {
  481. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
  482. struct m32c_reg *reg;
  483. gdb_assert (0 <= cookednum && cookednum < tdep->num_regs);
  484. gdb_assert (arch == cache->arch ());
  485. gdb_assert (arch == tdep->regs[cookednum].arch);
  486. reg = &tdep->regs[cookednum];
  487. return reg->read (reg, cache, buf);
  488. }
  489. static void
  490. m32c_pseudo_register_write (struct gdbarch *arch,
  491. struct regcache *cache,
  492. int cookednum,
  493. const gdb_byte *buf)
  494. {
  495. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
  496. struct m32c_reg *reg;
  497. gdb_assert (0 <= cookednum && cookednum < tdep->num_regs);
  498. gdb_assert (arch == cache->arch ());
  499. gdb_assert (arch == tdep->regs[cookednum].arch);
  500. reg = &tdep->regs[cookednum];
  501. reg->write (reg, cache, buf);
  502. }
  503. /* Add a register with the given fields to the end of ARCH's table.
  504. Return a pointer to the newly added register. */
  505. static struct m32c_reg *
  506. add_reg (struct gdbarch *arch,
  507. const char *name,
  508. struct type *type,
  509. int sim_num,
  510. m32c_read_reg_t *read,
  511. m32c_write_reg_t *write,
  512. struct m32c_reg *rx,
  513. struct m32c_reg *ry,
  514. int n)
  515. {
  516. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
  517. struct m32c_reg *r = &tdep->regs[tdep->num_regs];
  518. gdb_assert (tdep->num_regs < M32C_MAX_NUM_REGS);
  519. r->name = name;
  520. r->type = type;
  521. r->arch = arch;
  522. r->num = tdep->num_regs;
  523. r->sim_num = sim_num;
  524. r->dwarf_num = -1;
  525. r->general_p = 0;
  526. r->dma_p = 0;
  527. r->system_p = 0;
  528. r->save_restore_p = 0;
  529. r->read = read;
  530. r->write = write;
  531. r->rx = rx;
  532. r->ry = ry;
  533. r->n = n;
  534. tdep->num_regs++;
  535. return r;
  536. }
  537. /* Record NUM as REG's DWARF register number. */
  538. static void
  539. set_dwarf_regnum (struct m32c_reg *reg, int num)
  540. {
  541. gdb_assert (num < M32C_MAX_NUM_REGS);
  542. /* Update the reg->DWARF mapping. Only count the first number
  543. assigned to this register. */
  544. if (reg->dwarf_num == -1)
  545. reg->dwarf_num = num;
  546. /* Update the DWARF->reg mapping. */
  547. gdbarch *arch = reg->arch;
  548. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
  549. tdep->dwarf_regs[num] = reg;
  550. }
  551. /* Mark REG as a general-purpose register, and return it. */
  552. static struct m32c_reg *
  553. mark_general (struct m32c_reg *reg)
  554. {
  555. reg->general_p = 1;
  556. return reg;
  557. }
  558. /* Mark REG as a DMA register. */
  559. static void
  560. mark_dma (struct m32c_reg *reg)
  561. {
  562. reg->dma_p = 1;
  563. }
  564. /* Mark REG as a SYSTEM register, and return it. */
  565. static struct m32c_reg *
  566. mark_system (struct m32c_reg *reg)
  567. {
  568. reg->system_p = 1;
  569. return reg;
  570. }
  571. /* Mark REG as a save-restore register, and return it. */
  572. static struct m32c_reg *
  573. mark_save_restore (struct m32c_reg *reg)
  574. {
  575. reg->save_restore_p = 1;
  576. return reg;
  577. }
  578. #define FLAGBIT_B 0x0010
  579. #define FLAGBIT_U 0x0080
  580. /* Handy macros for declaring registers. These all evaluate to
  581. pointers to the register declared. Macros that define two
  582. registers evaluate to a pointer to the first. */
  583. /* A raw register named NAME, with type TYPE and sim number SIM_NUM. */
  584. #define R(name, type, sim_num) \
  585. (add_reg (arch, (name), (type), (sim_num), \
  586. m32c_raw_read, m32c_raw_write, NULL, NULL, 0))
  587. /* The simulator register number for a raw register named NAME. */
  588. #define SIM(name) (m32c_sim_reg_ ## name)
  589. /* A raw unsigned 16-bit data register named NAME.
  590. NAME should be an identifier, not a string. */
  591. #define R16U(name) \
  592. (R(#name, tdep->uint16, SIM (name)))
  593. /* A raw data address register named NAME.
  594. NAME should be an identifier, not a string. */
  595. #define RA(name) \
  596. (R(#name, tdep->data_addr_reg_type, SIM (name)))
  597. /* A raw code address register named NAME. NAME should
  598. be an identifier, not a string. */
  599. #define RC(name) \
  600. (R(#name, tdep->code_addr_reg_type, SIM (name)))
  601. /* A pair of raw registers named NAME0 and NAME1, with type TYPE.
  602. NAME should be an identifier, not a string. */
  603. #define RP(name, type) \
  604. (R(#name "0", (type), SIM (name ## 0)), \
  605. R(#name "1", (type), SIM (name ## 1)) - 1)
  606. /* A raw banked general-purpose data register named NAME.
  607. NAME should be an identifier, not a string. */
  608. #define RBD(name) \
  609. (R(NULL, tdep->int16, SIM (name ## _bank0)), \
  610. R(NULL, tdep->int16, SIM (name ## _bank1)) - 1)
  611. /* A raw banked data address register named NAME.
  612. NAME should be an identifier, not a string. */
  613. #define RBA(name) \
  614. (R(NULL, tdep->data_addr_reg_type, SIM (name ## _bank0)), \
  615. R(NULL, tdep->data_addr_reg_type, SIM (name ## _bank1)) - 1)
  616. /* A cooked register named NAME referring to a raw banked register
  617. from the bank selected by the current value of FLG. RAW_PAIR
  618. should be a pointer to the first register in the banked pair.
  619. NAME must be an identifier, not a string. */
  620. #define CB(name, raw_pair) \
  621. (add_reg (arch, #name, (raw_pair)->type, 0, \
  622. m32c_banked_read, m32c_banked_write, \
  623. (raw_pair), (raw_pair + 1), FLAGBIT_B))
  624. /* A pair of registers named NAMEH and NAMEL, of type TYPE, that
  625. access the top and bottom halves of the register pointed to by
  626. NAME. NAME should be an identifier. */
  627. #define CHL(name, type) \
  628. (add_reg (arch, #name "h", (type), 0, \
  629. m32c_part_read, m32c_part_write, name, NULL, 1), \
  630. add_reg (arch, #name "l", (type), 0, \
  631. m32c_part_read, m32c_part_write, name, NULL, 0) - 1)
  632. /* A register constructed by concatenating the two registers HIGH and
  633. LOW, whose name is HIGHLOW and whose type is TYPE. */
  634. #define CCAT(high, low, type) \
  635. (add_reg (arch, #high #low, (type), 0, \
  636. m32c_cat_read, m32c_cat_write, (high), (low), 0))
  637. /* Abbreviations for marking register group membership. */
  638. #define G(reg) (mark_general (reg))
  639. #define S(reg) (mark_system (reg))
  640. #define DMA(reg) (mark_dma (reg))
  641. /* Construct the register set for ARCH. */
  642. static void
  643. make_regs (struct gdbarch *arch)
  644. {
  645. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
  646. int mach = gdbarch_bfd_arch_info (arch)->mach;
  647. int num_raw_regs;
  648. int num_cooked_regs;
  649. struct m32c_reg *r0;
  650. struct m32c_reg *r1;
  651. struct m32c_reg *r2;
  652. struct m32c_reg *r3;
  653. struct m32c_reg *a0;
  654. struct m32c_reg *a1;
  655. struct m32c_reg *fb;
  656. struct m32c_reg *sb;
  657. struct m32c_reg *sp;
  658. struct m32c_reg *r0hl;
  659. struct m32c_reg *r1hl;
  660. struct m32c_reg *r2r0;
  661. struct m32c_reg *r3r1;
  662. struct m32c_reg *r3r1r2r0;
  663. struct m32c_reg *r3r2r1r0;
  664. struct m32c_reg *a1a0;
  665. struct m32c_reg *raw_r0_pair = RBD (r0);
  666. struct m32c_reg *raw_r1_pair = RBD (r1);
  667. struct m32c_reg *raw_r2_pair = RBD (r2);
  668. struct m32c_reg *raw_r3_pair = RBD (r3);
  669. struct m32c_reg *raw_a0_pair = RBA (a0);
  670. struct m32c_reg *raw_a1_pair = RBA (a1);
  671. struct m32c_reg *raw_fb_pair = RBA (fb);
  672. /* sb is banked on the bfd_mach_m32c, but not on bfd_mach_m16c.
  673. We always declare both raw registers, and deal with the distinction
  674. in the pseudoregister. */
  675. struct m32c_reg *raw_sb_pair = RBA (sb);
  676. struct m32c_reg *usp = S (RA (usp));
  677. struct m32c_reg *isp = S (RA (isp));
  678. struct m32c_reg *intb = S (RC (intb));
  679. struct m32c_reg *pc = G (RC (pc));
  680. struct m32c_reg *flg = G (R16U (flg));
  681. if (mach == bfd_mach_m32c)
  682. {
  683. S (R16U (svf));
  684. S (RC (svp));
  685. S (RC (vct));
  686. DMA (RP (dmd, tdep->uint8));
  687. DMA (RP (dct, tdep->uint16));
  688. DMA (RP (drc, tdep->uint16));
  689. DMA (RP (dma, tdep->data_addr_reg_type));
  690. DMA (RP (dsa, tdep->data_addr_reg_type));
  691. DMA (RP (dra, tdep->data_addr_reg_type));
  692. }
  693. num_raw_regs = tdep->num_regs;
  694. r0 = G (CB (r0, raw_r0_pair));
  695. r1 = G (CB (r1, raw_r1_pair));
  696. r2 = G (CB (r2, raw_r2_pair));
  697. r3 = G (CB (r3, raw_r3_pair));
  698. a0 = G (CB (a0, raw_a0_pair));
  699. a1 = G (CB (a1, raw_a1_pair));
  700. fb = G (CB (fb, raw_fb_pair));
  701. /* sb is banked on the bfd_mach_m32c, but not on bfd_mach_m16c.
  702. Specify custom read/write functions that do the right thing. */
  703. sb = G (add_reg (arch, "sb", raw_sb_pair->type, 0,
  704. m32c_sb_read, m32c_sb_write,
  705. raw_sb_pair, raw_sb_pair + 1, 0));
  706. /* The current sp is either usp or isp, depending on the value of
  707. the FLG register's U bit. */
  708. sp = G (add_reg (arch, "sp", usp->type, 0,
  709. m32c_banked_read, m32c_banked_write,
  710. isp, usp, FLAGBIT_U));
  711. r0hl = CHL (r0, tdep->int8);
  712. r1hl = CHL (r1, tdep->int8);
  713. CHL (r2, tdep->int8);
  714. CHL (r3, tdep->int8);
  715. CHL (intb, tdep->int16);
  716. r2r0 = CCAT (r2, r0, tdep->int32);
  717. r3r1 = CCAT (r3, r1, tdep->int32);
  718. r3r1r2r0 = CCAT (r3r1, r2r0, tdep->int64);
  719. r3r2r1r0
  720. = add_reg (arch, "r3r2r1r0", tdep->int64, 0,
  721. m32c_r3r2r1r0_read, m32c_r3r2r1r0_write, NULL, NULL, 0);
  722. if (mach == bfd_mach_m16c)
  723. a1a0 = CCAT (a1, a0, tdep->int32);
  724. else
  725. a1a0 = NULL;
  726. num_cooked_regs = tdep->num_regs - num_raw_regs;
  727. tdep->pc = pc;
  728. tdep->flg = flg;
  729. tdep->r0 = r0;
  730. tdep->r1 = r1;
  731. tdep->r2 = r2;
  732. tdep->r3 = r3;
  733. tdep->r2r0 = r2r0;
  734. tdep->r3r2r1r0 = r3r2r1r0;
  735. tdep->r3r1r2r0 = r3r1r2r0;
  736. tdep->a0 = a0;
  737. tdep->a1 = a1;
  738. tdep->sb = sb;
  739. tdep->fb = fb;
  740. tdep->sp = sp;
  741. /* Set up the DWARF register table. */
  742. memset (tdep->dwarf_regs, 0, sizeof (tdep->dwarf_regs));
  743. set_dwarf_regnum (r0hl + 1, 0x01);
  744. set_dwarf_regnum (r0hl + 0, 0x02);
  745. set_dwarf_regnum (r1hl + 1, 0x03);
  746. set_dwarf_regnum (r1hl + 0, 0x04);
  747. set_dwarf_regnum (r0, 0x05);
  748. set_dwarf_regnum (r1, 0x06);
  749. set_dwarf_regnum (r2, 0x07);
  750. set_dwarf_regnum (r3, 0x08);
  751. set_dwarf_regnum (a0, 0x09);
  752. set_dwarf_regnum (a1, 0x0a);
  753. set_dwarf_regnum (fb, 0x0b);
  754. set_dwarf_regnum (sp, 0x0c);
  755. set_dwarf_regnum (pc, 0x0d); /* GCC's invention */
  756. set_dwarf_regnum (sb, 0x13);
  757. set_dwarf_regnum (r2r0, 0x15);
  758. set_dwarf_regnum (r3r1, 0x16);
  759. if (a1a0)
  760. set_dwarf_regnum (a1a0, 0x17);
  761. /* Enumerate the save/restore register group.
  762. The regcache_save and regcache_restore functions apply their read
  763. function to each register in this group.
  764. Since frame_pop supplies frame_unwind_register as its read
  765. function, the registers meaningful to the Dwarf unwinder need to
  766. be in this group.
  767. On the other hand, when we make inferior calls, save_inferior_status
  768. and restore_inferior_status use them to preserve the current register
  769. values across the inferior call. For this, you'd kind of like to
  770. preserve all the raw registers, to protect the interrupted code from
  771. any sort of bank switching the callee might have done. But we handle
  772. those cases so badly anyway --- for example, it matters whether we
  773. restore FLG before or after we restore the general-purpose registers,
  774. but there's no way to express that --- that it isn't worth worrying
  775. about.
  776. We omit control registers like inthl: if you call a function that
  777. changes those, it's probably because you wanted that change to be
  778. visible to the interrupted code. */
  779. mark_save_restore (r0);
  780. mark_save_restore (r1);
  781. mark_save_restore (r2);
  782. mark_save_restore (r3);
  783. mark_save_restore (a0);
  784. mark_save_restore (a1);
  785. mark_save_restore (sb);
  786. mark_save_restore (fb);
  787. mark_save_restore (sp);
  788. mark_save_restore (pc);
  789. mark_save_restore (flg);
  790. set_gdbarch_num_regs (arch, num_raw_regs);
  791. set_gdbarch_num_pseudo_regs (arch, num_cooked_regs);
  792. set_gdbarch_pc_regnum (arch, pc->num);
  793. set_gdbarch_sp_regnum (arch, sp->num);
  794. set_gdbarch_register_name (arch, m32c_register_name);
  795. set_gdbarch_register_type (arch, m32c_register_type);
  796. set_gdbarch_pseudo_register_read (arch, m32c_pseudo_register_read);
  797. set_gdbarch_pseudo_register_write (arch, m32c_pseudo_register_write);
  798. set_gdbarch_register_sim_regno (arch, m32c_register_sim_regno);
  799. set_gdbarch_stab_reg_to_regnum (arch, m32c_debug_info_reg_to_regnum);
  800. set_gdbarch_dwarf2_reg_to_regnum (arch, m32c_debug_info_reg_to_regnum);
  801. set_gdbarch_register_reggroup_p (arch, m32c_register_reggroup_p);
  802. reggroup_add (arch, m32c_dma_reggroup);
  803. }
  804. /* Breakpoints. */
  805. constexpr gdb_byte m32c_break_insn[] = { 0x00 }; /* brk */
  806. typedef BP_MANIPULATION (m32c_break_insn) m32c_breakpoint;
  807. /* Prologue analysis. */
  808. enum m32c_prologue_kind
  809. {
  810. /* This function uses a frame pointer. */
  811. prologue_with_frame_ptr,
  812. /* This function has no frame pointer. */
  813. prologue_sans_frame_ptr,
  814. /* This function sets up the stack, so its frame is the first
  815. frame on the stack. */
  816. prologue_first_frame
  817. };
  818. struct m32c_prologue
  819. {
  820. /* For consistency with the DWARF 2 .debug_frame info generated by
  821. GCC, a frame's CFA is the address immediately after the saved
  822. return address. */
  823. /* The architecture for which we generated this prologue info. */
  824. struct gdbarch *arch;
  825. enum m32c_prologue_kind kind;
  826. /* If KIND is prologue_with_frame_ptr, this is the offset from the
  827. CFA to where the frame pointer points. This is always zero or
  828. negative. */
  829. LONGEST frame_ptr_offset;
  830. /* If KIND is prologue_sans_frame_ptr, the offset from the CFA to
  831. the stack pointer --- always zero or negative.
  832. Calling this a "size" is a bit misleading, but given that the
  833. stack grows downwards, using offsets for everything keeps one
  834. from going completely sign-crazy: you never change anything's
  835. sign for an ADD instruction; always change the second operand's
  836. sign for a SUB instruction; and everything takes care of
  837. itself.
  838. Functions that use alloca don't have a constant frame size. But
  839. they always have frame pointers, so we must use that to find the
  840. CFA (and perhaps to unwind the stack pointer). */
  841. LONGEST frame_size;
  842. /* The address of the first instruction at which the frame has been
  843. set up and the arguments are where the debug info says they are
  844. --- as best as we can tell. */
  845. CORE_ADDR prologue_end;
  846. /* reg_offset[R] is the offset from the CFA at which register R is
  847. saved, or 1 if register R has not been saved. (Real values are
  848. always zero or negative.) */
  849. LONGEST reg_offset[M32C_MAX_NUM_REGS];
  850. };
  851. /* The longest I've seen, anyway. */
  852. #define M32C_MAX_INSN_LEN (9)
  853. /* Processor state, for the prologue analyzer. */
  854. struct m32c_pv_state
  855. {
  856. struct gdbarch *arch;
  857. pv_t r0, r1, r2, r3;
  858. pv_t a0, a1;
  859. pv_t sb, fb, sp;
  860. pv_t pc;
  861. struct pv_area *stack;
  862. /* Bytes from the current PC, the address they were read from,
  863. and the address of the next unconsumed byte. */
  864. gdb_byte insn[M32C_MAX_INSN_LEN];
  865. CORE_ADDR scan_pc, next_addr;
  866. };
  867. /* Push VALUE on STATE's stack, occupying SIZE bytes. Return zero if
  868. all went well, or non-zero if simulating the action would trash our
  869. state. */
  870. static int
  871. m32c_pv_push (struct m32c_pv_state *state, pv_t value, int size)
  872. {
  873. if (state->stack->store_would_trash (state->sp))
  874. return 1;
  875. state->sp = pv_add_constant (state->sp, -size);
  876. state->stack->store (state->sp, size, value);
  877. return 0;
  878. }
  879. enum srcdest_kind
  880. {
  881. srcdest_reg,
  882. srcdest_partial_reg,
  883. srcdest_mem
  884. };
  885. /* A source or destination location for an m16c or m32c
  886. instruction. */
  887. struct srcdest
  888. {
  889. /* If srcdest_reg, the location is a register pointed to by REG.
  890. If srcdest_partial_reg, the location is part of a register pointed
  891. to by REG. We don't try to handle this too well.
  892. If srcdest_mem, the location is memory whose address is ADDR. */
  893. enum srcdest_kind kind;
  894. pv_t *reg, addr;
  895. };
  896. /* Return the SIZE-byte value at LOC in STATE. */
  897. static pv_t
  898. m32c_srcdest_fetch (struct m32c_pv_state *state, struct srcdest loc, int size)
  899. {
  900. if (loc.kind == srcdest_mem)
  901. return state->stack->fetch (loc.addr, size);
  902. else if (loc.kind == srcdest_partial_reg)
  903. return pv_unknown ();
  904. else
  905. return *loc.reg;
  906. }
  907. /* Write VALUE, a SIZE-byte value, to LOC in STATE. Return zero if
  908. all went well, or non-zero if simulating the store would trash our
  909. state. */
  910. static int
  911. m32c_srcdest_store (struct m32c_pv_state *state, struct srcdest loc,
  912. pv_t value, int size)
  913. {
  914. if (loc.kind == srcdest_mem)
  915. {
  916. if (state->stack->store_would_trash (loc.addr))
  917. return 1;
  918. state->stack->store (loc.addr, size, value);
  919. }
  920. else if (loc.kind == srcdest_partial_reg)
  921. *loc.reg = pv_unknown ();
  922. else
  923. *loc.reg = value;
  924. return 0;
  925. }
  926. static int
  927. m32c_sign_ext (int v, int bits)
  928. {
  929. int mask = 1 << (bits - 1);
  930. return (v ^ mask) - mask;
  931. }
  932. static unsigned int
  933. m32c_next_byte (struct m32c_pv_state *st)
  934. {
  935. gdb_assert (st->next_addr - st->scan_pc < sizeof (st->insn));
  936. return st->insn[st->next_addr++ - st->scan_pc];
  937. }
  938. static int
  939. m32c_udisp8 (struct m32c_pv_state *st)
  940. {
  941. return m32c_next_byte (st);
  942. }
  943. static int
  944. m32c_sdisp8 (struct m32c_pv_state *st)
  945. {
  946. return m32c_sign_ext (m32c_next_byte (st), 8);
  947. }
  948. static int
  949. m32c_udisp16 (struct m32c_pv_state *st)
  950. {
  951. int low = m32c_next_byte (st);
  952. int high = m32c_next_byte (st);
  953. return low + (high << 8);
  954. }
  955. static int
  956. m32c_sdisp16 (struct m32c_pv_state *st)
  957. {
  958. int low = m32c_next_byte (st);
  959. int high = m32c_next_byte (st);
  960. return m32c_sign_ext (low + (high << 8), 16);
  961. }
  962. static int
  963. m32c_udisp24 (struct m32c_pv_state *st)
  964. {
  965. int low = m32c_next_byte (st);
  966. int mid = m32c_next_byte (st);
  967. int high = m32c_next_byte (st);
  968. return low + (mid << 8) + (high << 16);
  969. }
  970. /* Extract the 'source' field from an m32c MOV.size:G-format instruction. */
  971. static int
  972. m32c_get_src23 (unsigned char *i)
  973. {
  974. return (((i[0] & 0x70) >> 2)
  975. | ((i[1] & 0x30) >> 4));
  976. }
  977. /* Extract the 'dest' field from an m32c MOV.size:G-format instruction. */
  978. static int
  979. m32c_get_dest23 (unsigned char *i)
  980. {
  981. return (((i[0] & 0x0e) << 1)
  982. | ((i[1] & 0xc0) >> 6));
  983. }
  984. static struct srcdest
  985. m32c_decode_srcdest4 (struct m32c_pv_state *st,
  986. int code, int size)
  987. {
  988. struct srcdest sd;
  989. if (code < 6)
  990. sd.kind = (size == 2 ? srcdest_reg : srcdest_partial_reg);
  991. else
  992. sd.kind = srcdest_mem;
  993. sd.addr = pv_unknown ();
  994. sd.reg = 0;
  995. switch (code)
  996. {
  997. case 0x0: sd.reg = &st->r0; break;
  998. case 0x1: sd.reg = (size == 1 ? &st->r0 : &st->r1); break;
  999. case 0x2: sd.reg = (size == 1 ? &st->r1 : &st->r2); break;
  1000. case 0x3: sd.reg = (size == 1 ? &st->r1 : &st->r3); break;
  1001. case 0x4: sd.reg = &st->a0; break;
  1002. case 0x5: sd.reg = &st->a1; break;
  1003. case 0x6: sd.addr = st->a0; break;
  1004. case 0x7: sd.addr = st->a1; break;
  1005. case 0x8: sd.addr = pv_add_constant (st->a0, m32c_udisp8 (st)); break;
  1006. case 0x9: sd.addr = pv_add_constant (st->a1, m32c_udisp8 (st)); break;
  1007. case 0xa: sd.addr = pv_add_constant (st->sb, m32c_udisp8 (st)); break;
  1008. case 0xb: sd.addr = pv_add_constant (st->fb, m32c_sdisp8 (st)); break;
  1009. case 0xc: sd.addr = pv_add_constant (st->a0, m32c_udisp16 (st)); break;
  1010. case 0xd: sd.addr = pv_add_constant (st->a1, m32c_udisp16 (st)); break;
  1011. case 0xe: sd.addr = pv_add_constant (st->sb, m32c_udisp16 (st)); break;
  1012. case 0xf: sd.addr = pv_constant (m32c_udisp16 (st)); break;
  1013. default:
  1014. gdb_assert_not_reached ("unexpected srcdest4");
  1015. }
  1016. return sd;
  1017. }
  1018. static struct srcdest
  1019. m32c_decode_sd23 (struct m32c_pv_state *st, int code, int size, int ind)
  1020. {
  1021. struct srcdest sd;
  1022. sd.addr = pv_unknown ();
  1023. sd.reg = 0;
  1024. switch (code)
  1025. {
  1026. case 0x12:
  1027. case 0x13:
  1028. case 0x10:
  1029. case 0x11:
  1030. sd.kind = (size == 1) ? srcdest_partial_reg : srcdest_reg;
  1031. break;
  1032. case 0x02:
  1033. case 0x03:
  1034. sd.kind = (size == 4) ? srcdest_reg : srcdest_partial_reg;
  1035. break;
  1036. default:
  1037. sd.kind = srcdest_mem;
  1038. break;
  1039. }
  1040. switch (code)
  1041. {
  1042. case 0x12: sd.reg = &st->r0; break;
  1043. case 0x13: sd.reg = &st->r1; break;
  1044. case 0x10: sd.reg = ((size == 1) ? &st->r0 : &st->r2); break;
  1045. case 0x11: sd.reg = ((size == 1) ? &st->r1 : &st->r3); break;
  1046. case 0x02: sd.reg = &st->a0; break;
  1047. case 0x03: sd.reg = &st->a1; break;
  1048. case 0x00: sd.addr = st->a0; break;
  1049. case 0x01: sd.addr = st->a1; break;
  1050. case 0x04: sd.addr = pv_add_constant (st->a0, m32c_udisp8 (st)); break;
  1051. case 0x05: sd.addr = pv_add_constant (st->a1, m32c_udisp8 (st)); break;
  1052. case 0x06: sd.addr = pv_add_constant (st->sb, m32c_udisp8 (st)); break;
  1053. case 0x07: sd.addr = pv_add_constant (st->fb, m32c_sdisp8 (st)); break;
  1054. case 0x08: sd.addr = pv_add_constant (st->a0, m32c_udisp16 (st)); break;
  1055. case 0x09: sd.addr = pv_add_constant (st->a1, m32c_udisp16 (st)); break;
  1056. case 0x0a: sd.addr = pv_add_constant (st->sb, m32c_udisp16 (st)); break;
  1057. case 0x0b: sd.addr = pv_add_constant (st->fb, m32c_sdisp16 (st)); break;
  1058. case 0x0c: sd.addr = pv_add_constant (st->a0, m32c_udisp24 (st)); break;
  1059. case 0x0d: sd.addr = pv_add_constant (st->a1, m32c_udisp24 (st)); break;
  1060. case 0x0f: sd.addr = pv_constant (m32c_udisp16 (st)); break;
  1061. case 0x0e: sd.addr = pv_constant (m32c_udisp24 (st)); break;
  1062. default:
  1063. gdb_assert_not_reached ("unexpected sd23");
  1064. }
  1065. if (ind)
  1066. {
  1067. sd.addr = m32c_srcdest_fetch (st, sd, 4);
  1068. sd.kind = srcdest_mem;
  1069. }
  1070. return sd;
  1071. }
  1072. /* The r16c and r32c machines have instructions with similar
  1073. semantics, but completely different machine language encodings. So
  1074. we break out the semantics into their own functions, and leave
  1075. machine-specific decoding in m32c_analyze_prologue.
  1076. The following functions all expect their arguments already decoded,
  1077. and they all return zero if analysis should continue past this
  1078. instruction, or non-zero if analysis should stop. */
  1079. /* Simulate an 'enter SIZE' instruction in STATE. */
  1080. static int
  1081. m32c_pv_enter (struct m32c_pv_state *state, int size)
  1082. {
  1083. /* If simulating this store would require us to forget
  1084. everything we know about the stack frame in the name of
  1085. accuracy, it would be better to just quit now. */
  1086. if (state->stack->store_would_trash (state->sp))
  1087. return 1;
  1088. gdbarch *arch = state->arch;
  1089. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
  1090. if (m32c_pv_push (state, state->fb, tdep->push_addr_bytes))
  1091. return 1;
  1092. state->fb = state->sp;
  1093. state->sp = pv_add_constant (state->sp, -size);
  1094. return 0;
  1095. }
  1096. static int
  1097. m32c_pv_pushm_one (struct m32c_pv_state *state, pv_t reg,
  1098. int bit, int src, int size)
  1099. {
  1100. if (bit & src)
  1101. {
  1102. if (m32c_pv_push (state, reg, size))
  1103. return 1;
  1104. }
  1105. return 0;
  1106. }
  1107. /* Simulate a 'pushm SRC' instruction in STATE. */
  1108. static int
  1109. m32c_pv_pushm (struct m32c_pv_state *state, int src)
  1110. {
  1111. gdbarch *arch = state->arch;
  1112. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
  1113. /* The bits in SRC indicating which registers to save are:
  1114. r0 r1 r2 r3 a0 a1 sb fb */
  1115. return
  1116. ( m32c_pv_pushm_one (state, state->fb, 0x01, src, tdep->push_addr_bytes)
  1117. || m32c_pv_pushm_one (state, state->sb, 0x02, src, tdep->push_addr_bytes)
  1118. || m32c_pv_pushm_one (state, state->a1, 0x04, src, tdep->push_addr_bytes)
  1119. || m32c_pv_pushm_one (state, state->a0, 0x08, src, tdep->push_addr_bytes)
  1120. || m32c_pv_pushm_one (state, state->r3, 0x10, src, 2)
  1121. || m32c_pv_pushm_one (state, state->r2, 0x20, src, 2)
  1122. || m32c_pv_pushm_one (state, state->r1, 0x40, src, 2)
  1123. || m32c_pv_pushm_one (state, state->r0, 0x80, src, 2));
  1124. }
  1125. /* Return non-zero if VALUE is the first incoming argument register. */
  1126. static int
  1127. m32c_is_1st_arg_reg (struct m32c_pv_state *state, pv_t value)
  1128. {
  1129. gdbarch *arch = state->arch;
  1130. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
  1131. return (value.kind == pvk_register
  1132. && (gdbarch_bfd_arch_info (state->arch)->mach == bfd_mach_m16c
  1133. ? (value.reg == tdep->r1->num)
  1134. : (value.reg == tdep->r0->num))
  1135. && value.k == 0);
  1136. }
  1137. /* Return non-zero if VALUE is an incoming argument register. */
  1138. static int
  1139. m32c_is_arg_reg (struct m32c_pv_state *state, pv_t value)
  1140. {
  1141. gdbarch *arch = state->arch;
  1142. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
  1143. return (value.kind == pvk_register
  1144. && (gdbarch_bfd_arch_info (state->arch)->mach == bfd_mach_m16c
  1145. ? (value.reg == tdep->r1->num || value.reg == tdep->r2->num)
  1146. : (value.reg == tdep->r0->num))
  1147. && value.k == 0);
  1148. }
  1149. /* Return non-zero if a store of VALUE to LOC is probably spilling an
  1150. argument register to its stack slot in STATE. Such instructions
  1151. should be included in the prologue, if possible.
  1152. The store is a spill if:
  1153. - the value being stored is the original value of an argument register;
  1154. - the value has not already been stored somewhere in STACK; and
  1155. - LOC is a stack slot (e.g., a memory location whose address is
  1156. relative to the original value of the SP). */
  1157. static int
  1158. m32c_is_arg_spill (struct m32c_pv_state *st,
  1159. struct srcdest loc,
  1160. pv_t value)
  1161. {
  1162. gdbarch *arch = st->arch;
  1163. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
  1164. return (m32c_is_arg_reg (st, value)
  1165. && loc.kind == srcdest_mem
  1166. && pv_is_register (loc.addr, tdep->sp->num)
  1167. && ! st->stack->find_reg (st->arch, value.reg, 0));
  1168. }
  1169. /* Return non-zero if a store of VALUE to LOC is probably
  1170. copying the struct return address into an address register
  1171. for immediate use. This is basically a "spill" into the
  1172. address register, instead of onto the stack.
  1173. The prerequisites are:
  1174. - value being stored is original value of the FIRST arg register;
  1175. - value has not already been stored on stack; and
  1176. - LOC is an address register (a0 or a1). */
  1177. static int
  1178. m32c_is_struct_return (struct m32c_pv_state *st,
  1179. struct srcdest loc,
  1180. pv_t value)
  1181. {
  1182. gdbarch *arch = st->arch;
  1183. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
  1184. return (m32c_is_1st_arg_reg (st, value)
  1185. && !st->stack->find_reg (st->arch, value.reg, 0)
  1186. && loc.kind == srcdest_reg
  1187. && (pv_is_register (*loc.reg, tdep->a0->num)
  1188. || pv_is_register (*loc.reg, tdep->a1->num)));
  1189. }
  1190. /* Return non-zero if a 'pushm' saving the registers indicated by SRC
  1191. was a register save:
  1192. - all the named registers should have their original values, and
  1193. - the stack pointer should be at a constant offset from the
  1194. original stack pointer. */
  1195. static int
  1196. m32c_pushm_is_reg_save (struct m32c_pv_state *st, int src)
  1197. {
  1198. gdbarch *arch = st->arch;
  1199. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
  1200. /* The bits in SRC indicating which registers to save are:
  1201. r0 r1 r2 r3 a0 a1 sb fb */
  1202. return
  1203. (pv_is_register (st->sp, tdep->sp->num)
  1204. && (! (src & 0x01) || pv_is_register_k (st->fb, tdep->fb->num, 0))
  1205. && (! (src & 0x02) || pv_is_register_k (st->sb, tdep->sb->num, 0))
  1206. && (! (src & 0x04) || pv_is_register_k (st->a1, tdep->a1->num, 0))
  1207. && (! (src & 0x08) || pv_is_register_k (st->a0, tdep->a0->num, 0))
  1208. && (! (src & 0x10) || pv_is_register_k (st->r3, tdep->r3->num, 0))
  1209. && (! (src & 0x20) || pv_is_register_k (st->r2, tdep->r2->num, 0))
  1210. && (! (src & 0x40) || pv_is_register_k (st->r1, tdep->r1->num, 0))
  1211. && (! (src & 0x80) || pv_is_register_k (st->r0, tdep->r0->num, 0)));
  1212. }
  1213. /* Function for finding saved registers in a 'struct pv_area'; we pass
  1214. this to pv_area::scan.
  1215. If VALUE is a saved register, ADDR says it was saved at a constant
  1216. offset from the frame base, and SIZE indicates that the whole
  1217. register was saved, record its offset in RESULT_UNTYPED. */
  1218. static void
  1219. check_for_saved (void *prologue_untyped, pv_t addr, CORE_ADDR size, pv_t value)
  1220. {
  1221. struct m32c_prologue *prologue = (struct m32c_prologue *) prologue_untyped;
  1222. struct gdbarch *arch = prologue->arch;
  1223. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
  1224. /* Is this the unchanged value of some register being saved on the
  1225. stack? */
  1226. if (value.kind == pvk_register
  1227. && value.k == 0
  1228. && pv_is_register (addr, tdep->sp->num))
  1229. {
  1230. /* Some registers require special handling: they're saved as a
  1231. larger value than the register itself. */
  1232. CORE_ADDR saved_size = register_size (arch, value.reg);
  1233. if (value.reg == tdep->pc->num)
  1234. saved_size = tdep->ret_addr_bytes;
  1235. else if (register_type (arch, value.reg)
  1236. == tdep->data_addr_reg_type)
  1237. saved_size = tdep->push_addr_bytes;
  1238. if (size == saved_size)
  1239. {
  1240. /* Find which end of the saved value corresponds to our
  1241. register. */
  1242. if (gdbarch_byte_order (arch) == BFD_ENDIAN_BIG)
  1243. prologue->reg_offset[value.reg]
  1244. = (addr.k + saved_size - register_size (arch, value.reg));
  1245. else
  1246. prologue->reg_offset[value.reg] = addr.k;
  1247. }
  1248. }
  1249. }
  1250. /* Analyze the function prologue for ARCH at START, going no further
  1251. than LIMIT, and place a description of what we found in
  1252. PROLOGUE. */
  1253. static void
  1254. m32c_analyze_prologue (struct gdbarch *arch,
  1255. CORE_ADDR start, CORE_ADDR limit,
  1256. struct m32c_prologue *prologue)
  1257. {
  1258. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
  1259. unsigned long mach = gdbarch_bfd_arch_info (arch)->mach;
  1260. CORE_ADDR after_last_frame_related_insn;
  1261. struct m32c_pv_state st;
  1262. st.arch = arch;
  1263. st.r0 = pv_register (tdep->r0->num, 0);
  1264. st.r1 = pv_register (tdep->r1->num, 0);
  1265. st.r2 = pv_register (tdep->r2->num, 0);
  1266. st.r3 = pv_register (tdep->r3->num, 0);
  1267. st.a0 = pv_register (tdep->a0->num, 0);
  1268. st.a1 = pv_register (tdep->a1->num, 0);
  1269. st.sb = pv_register (tdep->sb->num, 0);
  1270. st.fb = pv_register (tdep->fb->num, 0);
  1271. st.sp = pv_register (tdep->sp->num, 0);
  1272. st.pc = pv_register (tdep->pc->num, 0);
  1273. pv_area stack (tdep->sp->num, gdbarch_addr_bit (arch));
  1274. st.stack = &stack;
  1275. /* Record that the call instruction has saved the return address on
  1276. the stack. */
  1277. m32c_pv_push (&st, st.pc, tdep->ret_addr_bytes);
  1278. memset (prologue, 0, sizeof (*prologue));
  1279. prologue->arch = arch;
  1280. {
  1281. int i;
  1282. for (i = 0; i < M32C_MAX_NUM_REGS; i++)
  1283. prologue->reg_offset[i] = 1;
  1284. }
  1285. st.scan_pc = after_last_frame_related_insn = start;
  1286. while (st.scan_pc < limit)
  1287. {
  1288. pv_t pre_insn_fb = st.fb;
  1289. pv_t pre_insn_sp = st.sp;
  1290. /* In theory we could get in trouble by trying to read ahead
  1291. here, when we only know we're expecting one byte. In
  1292. practice I doubt anyone will care, and it makes the rest of
  1293. the code easier. */
  1294. if (target_read_memory (st.scan_pc, st.insn, sizeof (st.insn)))
  1295. /* If we can't fetch the instruction from memory, stop here
  1296. and hope for the best. */
  1297. break;
  1298. st.next_addr = st.scan_pc;
  1299. /* The assembly instructions are written as they appear in the
  1300. section of the processor manuals that describe the
  1301. instruction encodings.
  1302. When a single assembly language instruction has several
  1303. different machine-language encodings, the manual
  1304. distinguishes them by a number in parens, before the
  1305. mnemonic. Those numbers are included, as well.
  1306. The srcdest decoding instructions have the same names as the
  1307. analogous functions in the simulator. */
  1308. if (mach == bfd_mach_m16c)
  1309. {
  1310. /* (1) ENTER #imm8 */
  1311. if (st.insn[0] == 0x7c && st.insn[1] == 0xf2)
  1312. {
  1313. if (m32c_pv_enter (&st, st.insn[2]))
  1314. break;
  1315. st.next_addr += 3;
  1316. }
  1317. /* (1) PUSHM src */
  1318. else if (st.insn[0] == 0xec)
  1319. {
  1320. int src = st.insn[1];
  1321. if (m32c_pv_pushm (&st, src))
  1322. break;
  1323. st.next_addr += 2;
  1324. if (m32c_pushm_is_reg_save (&st, src))
  1325. after_last_frame_related_insn = st.next_addr;
  1326. }
  1327. /* (6) MOV.size:G src, dest */
  1328. else if ((st.insn[0] & 0xfe) == 0x72)
  1329. {
  1330. int size = (st.insn[0] & 0x01) ? 2 : 1;
  1331. struct srcdest src;
  1332. struct srcdest dest;
  1333. pv_t src_value;
  1334. st.next_addr += 2;
  1335. src
  1336. = m32c_decode_srcdest4 (&st, (st.insn[1] >> 4) & 0xf, size);
  1337. dest
  1338. = m32c_decode_srcdest4 (&st, st.insn[1] & 0xf, size);
  1339. src_value = m32c_srcdest_fetch (&st, src, size);
  1340. if (m32c_is_arg_spill (&st, dest, src_value))
  1341. after_last_frame_related_insn = st.next_addr;
  1342. else if (m32c_is_struct_return (&st, dest, src_value))
  1343. after_last_frame_related_insn = st.next_addr;
  1344. if (m32c_srcdest_store (&st, dest, src_value, size))
  1345. break;
  1346. }
  1347. /* (1) LDC #IMM16, sp */
  1348. else if (st.insn[0] == 0xeb
  1349. && st.insn[1] == 0x50)
  1350. {
  1351. st.next_addr += 2;
  1352. st.sp = pv_constant (m32c_udisp16 (&st));
  1353. }
  1354. else
  1355. /* We've hit some instruction we don't know how to simulate.
  1356. Strictly speaking, we should set every value we're
  1357. tracking to "unknown". But we'll be optimistic, assume
  1358. that we have enough information already, and stop
  1359. analysis here. */
  1360. break;
  1361. }
  1362. else
  1363. {
  1364. int src_indirect = 0;
  1365. int dest_indirect = 0;
  1366. int i = 0;
  1367. gdb_assert (mach == bfd_mach_m32c);
  1368. /* Check for prefix bytes indicating indirect addressing. */
  1369. if (st.insn[0] == 0x41)
  1370. {
  1371. src_indirect = 1;
  1372. i++;
  1373. }
  1374. else if (st.insn[0] == 0x09)
  1375. {
  1376. dest_indirect = 1;
  1377. i++;
  1378. }
  1379. else if (st.insn[0] == 0x49)
  1380. {
  1381. src_indirect = dest_indirect = 1;
  1382. i++;
  1383. }
  1384. /* (1) ENTER #imm8 */
  1385. if (st.insn[i] == 0xec)
  1386. {
  1387. if (m32c_pv_enter (&st, st.insn[i + 1]))
  1388. break;
  1389. st.next_addr += 2;
  1390. }
  1391. /* (1) PUSHM src */
  1392. else if (st.insn[i] == 0x8f)
  1393. {
  1394. int src = st.insn[i + 1];
  1395. if (m32c_pv_pushm (&st, src))
  1396. break;
  1397. st.next_addr += 2;
  1398. if (m32c_pushm_is_reg_save (&st, src))
  1399. after_last_frame_related_insn = st.next_addr;
  1400. }
  1401. /* (7) MOV.size:G src, dest */
  1402. else if ((st.insn[i] & 0x80) == 0x80
  1403. && (st.insn[i + 1] & 0x0f) == 0x0b
  1404. && m32c_get_src23 (&st.insn[i]) < 20
  1405. && m32c_get_dest23 (&st.insn[i]) < 20)
  1406. {
  1407. struct srcdest src;
  1408. struct srcdest dest;
  1409. pv_t src_value;
  1410. int bw = st.insn[i] & 0x01;
  1411. int size = bw ? 2 : 1;
  1412. st.next_addr += 2;
  1413. src
  1414. = m32c_decode_sd23 (&st, m32c_get_src23 (&st.insn[i]),
  1415. size, src_indirect);
  1416. dest
  1417. = m32c_decode_sd23 (&st, m32c_get_dest23 (&st.insn[i]),
  1418. size, dest_indirect);
  1419. src_value = m32c_srcdest_fetch (&st, src, size);
  1420. if (m32c_is_arg_spill (&st, dest, src_value))
  1421. after_last_frame_related_insn = st.next_addr;
  1422. if (m32c_srcdest_store (&st, dest, src_value, size))
  1423. break;
  1424. }
  1425. /* (2) LDC #IMM24, sp */
  1426. else if (st.insn[i] == 0xd5
  1427. && st.insn[i + 1] == 0x29)
  1428. {
  1429. st.next_addr += 2;
  1430. st.sp = pv_constant (m32c_udisp24 (&st));
  1431. }
  1432. else
  1433. /* We've hit some instruction we don't know how to simulate.
  1434. Strictly speaking, we should set every value we're
  1435. tracking to "unknown". But we'll be optimistic, assume
  1436. that we have enough information already, and stop
  1437. analysis here. */
  1438. break;
  1439. }
  1440. /* If this instruction changed the FB or decreased the SP (i.e.,
  1441. allocated more stack space), then this may be a good place to
  1442. declare the prologue finished. However, there are some
  1443. exceptions:
  1444. - If the instruction just changed the FB back to its original
  1445. value, then that's probably a restore instruction. The
  1446. prologue should definitely end before that.
  1447. - If the instruction increased the value of the SP (that is,
  1448. shrunk the frame), then it's probably part of a frame
  1449. teardown sequence, and the prologue should end before
  1450. that. */
  1451. if (! pv_is_identical (st.fb, pre_insn_fb))
  1452. {
  1453. if (! pv_is_register_k (st.fb, tdep->fb->num, 0))
  1454. after_last_frame_related_insn = st.next_addr;
  1455. }
  1456. else if (! pv_is_identical (st.sp, pre_insn_sp))
  1457. {
  1458. /* The comparison of the constants looks odd, there, because
  1459. .k is unsigned. All it really means is that the SP is
  1460. lower than it was before the instruction. */
  1461. if ( pv_is_register (pre_insn_sp, tdep->sp->num)
  1462. && pv_is_register (st.sp, tdep->sp->num)
  1463. && ((pre_insn_sp.k - st.sp.k) < (st.sp.k - pre_insn_sp.k)))
  1464. after_last_frame_related_insn = st.next_addr;
  1465. }
  1466. st.scan_pc = st.next_addr;
  1467. }
  1468. /* Did we load a constant value into the stack pointer? */
  1469. if (pv_is_constant (st.sp))
  1470. prologue->kind = prologue_first_frame;
  1471. /* Alternatively, did we initialize the frame pointer? Remember
  1472. that the CFA is the address after the return address. */
  1473. if (pv_is_register (st.fb, tdep->sp->num))
  1474. {
  1475. prologue->kind = prologue_with_frame_ptr;
  1476. prologue->frame_ptr_offset = st.fb.k;
  1477. }
  1478. /* Is the frame size a known constant? Remember that frame_size is
  1479. actually the offset from the CFA to the SP (i.e., a negative
  1480. value). */
  1481. else if (pv_is_register (st.sp, tdep->sp->num))
  1482. {
  1483. prologue->kind = prologue_sans_frame_ptr;
  1484. prologue->frame_size = st.sp.k;
  1485. }
  1486. /* We haven't been able to make sense of this function's frame. Treat
  1487. it as the first frame. */
  1488. else
  1489. prologue->kind = prologue_first_frame;
  1490. /* Record where all the registers were saved. */
  1491. st.stack->scan (check_for_saved, (void *) prologue);
  1492. prologue->prologue_end = after_last_frame_related_insn;
  1493. }
  1494. static CORE_ADDR
  1495. m32c_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR ip)
  1496. {
  1497. const char *name;
  1498. CORE_ADDR func_addr, func_end, sal_end;
  1499. struct m32c_prologue p;
  1500. /* Try to find the extent of the function that contains IP. */
  1501. if (! find_pc_partial_function (ip, &name, &func_addr, &func_end))
  1502. return ip;
  1503. /* Find end by prologue analysis. */
  1504. m32c_analyze_prologue (gdbarch, ip, func_end, &p);
  1505. /* Find end by line info. */
  1506. sal_end = skip_prologue_using_sal (gdbarch, ip);
  1507. /* Return whichever is lower. */
  1508. if (sal_end != 0 && sal_end != ip && sal_end < p.prologue_end)
  1509. return sal_end;
  1510. else
  1511. return p.prologue_end;
  1512. }
  1513. /* Stack unwinding. */
  1514. static struct m32c_prologue *
  1515. m32c_analyze_frame_prologue (struct frame_info *this_frame,
  1516. void **this_prologue_cache)
  1517. {
  1518. if (! *this_prologue_cache)
  1519. {
  1520. CORE_ADDR func_start = get_frame_func (this_frame);
  1521. CORE_ADDR stop_addr = get_frame_pc (this_frame);
  1522. /* If we couldn't find any function containing the PC, then
  1523. just initialize the prologue cache, but don't do anything. */
  1524. if (! func_start)
  1525. stop_addr = func_start;
  1526. *this_prologue_cache = FRAME_OBSTACK_ZALLOC (struct m32c_prologue);
  1527. m32c_analyze_prologue (get_frame_arch (this_frame),
  1528. func_start, stop_addr,
  1529. (struct m32c_prologue *) *this_prologue_cache);
  1530. }
  1531. return (struct m32c_prologue *) *this_prologue_cache;
  1532. }
  1533. static CORE_ADDR
  1534. m32c_frame_base (struct frame_info *this_frame,
  1535. void **this_prologue_cache)
  1536. {
  1537. struct m32c_prologue *p
  1538. = m32c_analyze_frame_prologue (this_frame, this_prologue_cache);
  1539. gdbarch *arch = get_frame_arch (this_frame);
  1540. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
  1541. /* In functions that use alloca, the distance between the stack
  1542. pointer and the frame base varies dynamically, so we can't use
  1543. the SP plus static information like prologue analysis to find the
  1544. frame base. However, such functions must have a frame pointer,
  1545. to be able to restore the SP on exit. So whenever we do have a
  1546. frame pointer, use that to find the base. */
  1547. switch (p->kind)
  1548. {
  1549. case prologue_with_frame_ptr:
  1550. {
  1551. CORE_ADDR fb
  1552. = get_frame_register_unsigned (this_frame, tdep->fb->num);
  1553. return fb - p->frame_ptr_offset;
  1554. }
  1555. case prologue_sans_frame_ptr:
  1556. {
  1557. CORE_ADDR sp
  1558. = get_frame_register_unsigned (this_frame, tdep->sp->num);
  1559. return sp - p->frame_size;
  1560. }
  1561. case prologue_first_frame:
  1562. return 0;
  1563. default:
  1564. gdb_assert_not_reached ("unexpected prologue kind");
  1565. }
  1566. }
  1567. static void
  1568. m32c_this_id (struct frame_info *this_frame,
  1569. void **this_prologue_cache,
  1570. struct frame_id *this_id)
  1571. {
  1572. CORE_ADDR base = m32c_frame_base (this_frame, this_prologue_cache);
  1573. if (base)
  1574. *this_id = frame_id_build (base, get_frame_func (this_frame));
  1575. /* Otherwise, leave it unset, and that will terminate the backtrace. */
  1576. }
  1577. static struct value *
  1578. m32c_prev_register (struct frame_info *this_frame,
  1579. void **this_prologue_cache, int regnum)
  1580. {
  1581. gdbarch *arch = get_frame_arch (this_frame);
  1582. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (arch);
  1583. struct m32c_prologue *p
  1584. = m32c_analyze_frame_prologue (this_frame, this_prologue_cache);
  1585. CORE_ADDR frame_base = m32c_frame_base (this_frame, this_prologue_cache);
  1586. if (regnum == tdep->sp->num)
  1587. return frame_unwind_got_constant (this_frame, regnum, frame_base);
  1588. /* If prologue analysis says we saved this register somewhere,
  1589. return a description of the stack slot holding it. */
  1590. if (p->reg_offset[regnum] != 1)
  1591. return frame_unwind_got_memory (this_frame, regnum,
  1592. frame_base + p->reg_offset[regnum]);
  1593. /* Otherwise, presume we haven't changed the value of this
  1594. register, and get it from the next frame. */
  1595. return frame_unwind_got_register (this_frame, regnum, regnum);
  1596. }
  1597. static const struct frame_unwind m32c_unwind = {
  1598. "m32c prologue",
  1599. NORMAL_FRAME,
  1600. default_frame_unwind_stop_reason,
  1601. m32c_this_id,
  1602. m32c_prev_register,
  1603. NULL,
  1604. default_frame_sniffer
  1605. };
  1606. /* Inferior calls. */
  1607. /* The calling conventions, according to GCC:
  1608. r8c, m16c
  1609. ---------
  1610. First arg may be passed in r1l or r1 if it (1) fits (QImode or
  1611. HImode), (2) is named, and (3) is an integer or pointer type (no
  1612. structs, floats, etc). Otherwise, it's passed on the stack.
  1613. Second arg may be passed in r2, same restrictions (but not QImode),
  1614. even if the first arg is passed on the stack.
  1615. Third and further args are passed on the stack. No padding is
  1616. used, stack "alignment" is 8 bits.
  1617. m32cm, m32c
  1618. -----------
  1619. First arg may be passed in r0l or r0, same restrictions as above.
  1620. Second and further args are passed on the stack. Padding is used
  1621. after QImode parameters (i.e. lower-addressed byte is the value,
  1622. higher-addressed byte is the padding), stack "alignment" is 16
  1623. bits. */
  1624. /* Return true if TYPE is a type that can be passed in registers. (We
  1625. ignore the size, and pay attention only to the type code;
  1626. acceptable sizes depends on which register is being considered to
  1627. hold it.) */
  1628. static int
  1629. m32c_reg_arg_type (struct type *type)
  1630. {
  1631. enum type_code code = type->code ();
  1632. return (code == TYPE_CODE_INT
  1633. || code == TYPE_CODE_ENUM
  1634. || code == TYPE_CODE_PTR
  1635. || TYPE_IS_REFERENCE (type)
  1636. || code == TYPE_CODE_BOOL
  1637. || code == TYPE_CODE_CHAR);
  1638. }
  1639. static CORE_ADDR
  1640. m32c_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  1641. struct regcache *regcache, CORE_ADDR bp_addr, int nargs,
  1642. struct value **args, CORE_ADDR sp,
  1643. function_call_return_method return_method,
  1644. CORE_ADDR struct_addr)
  1645. {
  1646. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1647. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1648. unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
  1649. CORE_ADDR cfa;
  1650. int i;
  1651. /* The number of arguments given in this function's prototype, or
  1652. zero if it has a non-prototyped function type. The m32c ABI
  1653. passes arguments mentioned in the prototype differently from
  1654. those in the ellipsis of a varargs function, or from those passed
  1655. to a non-prototyped function. */
  1656. int num_prototyped_args = 0;
  1657. {
  1658. struct type *func_type = value_type (function);
  1659. /* Dereference function pointer types. */
  1660. if (func_type->code () == TYPE_CODE_PTR)
  1661. func_type = TYPE_TARGET_TYPE (func_type);
  1662. gdb_assert (func_type->code () == TYPE_CODE_FUNC ||
  1663. func_type->code () == TYPE_CODE_METHOD);
  1664. #if 0
  1665. /* The ABI description in gcc/config/m32c/m32c.abi says that
  1666. we need to handle prototyped and non-prototyped functions
  1667. separately, but the code in GCC doesn't actually do so. */
  1668. if (TYPE_PROTOTYPED (func_type))
  1669. #endif
  1670. num_prototyped_args = func_type->num_fields ();
  1671. }
  1672. /* First, if the function returns an aggregate by value, push a
  1673. pointer to a buffer for it. This doesn't affect the way
  1674. subsequent arguments are allocated to registers. */
  1675. if (return_method == return_method_struct)
  1676. {
  1677. int ptr_len = TYPE_LENGTH (tdep->ptr_voyd);
  1678. sp -= ptr_len;
  1679. write_memory_unsigned_integer (sp, ptr_len, byte_order, struct_addr);
  1680. }
  1681. /* Push the arguments. */
  1682. for (i = nargs - 1; i >= 0; i--)
  1683. {
  1684. struct value *arg = args[i];
  1685. const gdb_byte *arg_bits = value_contents (arg).data ();
  1686. struct type *arg_type = value_type (arg);
  1687. ULONGEST arg_size = TYPE_LENGTH (arg_type);
  1688. /* Can it go in r1 or r1l (for m16c) or r0 or r0l (for m32c)? */
  1689. if (i == 0
  1690. && arg_size <= 2
  1691. && i < num_prototyped_args
  1692. && m32c_reg_arg_type (arg_type))
  1693. {
  1694. /* Extract and re-store as an integer as a terse way to make
  1695. sure it ends up in the least significant end of r1. (GDB
  1696. should avoid assuming endianness, even on uni-endian
  1697. processors.) */
  1698. ULONGEST u = extract_unsigned_integer (arg_bits, arg_size,
  1699. byte_order);
  1700. struct m32c_reg *reg = (mach == bfd_mach_m16c) ? tdep->r1 : tdep->r0;
  1701. regcache_cooked_write_unsigned (regcache, reg->num, u);
  1702. }
  1703. /* Can it go in r2? */
  1704. else if (mach == bfd_mach_m16c
  1705. && i == 1
  1706. && arg_size == 2
  1707. && i < num_prototyped_args
  1708. && m32c_reg_arg_type (arg_type))
  1709. regcache->cooked_write (tdep->r2->num, arg_bits);
  1710. /* Everything else goes on the stack. */
  1711. else
  1712. {
  1713. sp -= arg_size;
  1714. /* Align the stack. */
  1715. if (mach == bfd_mach_m32c)
  1716. sp &= ~1;
  1717. write_memory (sp, arg_bits, arg_size);
  1718. }
  1719. }
  1720. /* This is the CFA we use to identify the dummy frame. */
  1721. cfa = sp;
  1722. /* Push the return address. */
  1723. sp -= tdep->ret_addr_bytes;
  1724. write_memory_unsigned_integer (sp, tdep->ret_addr_bytes, byte_order,
  1725. bp_addr);
  1726. /* Update the stack pointer. */
  1727. regcache_cooked_write_unsigned (regcache, tdep->sp->num, sp);
  1728. /* We need to borrow an odd trick from the i386 target here.
  1729. The value we return from this function gets used as the stack
  1730. address (the CFA) for the dummy frame's ID. The obvious thing is
  1731. to return the new TOS. However, that points at the return
  1732. address, saved on the stack, which is inconsistent with the CFA's
  1733. described by GCC's DWARF 2 .debug_frame information: DWARF 2
  1734. .debug_frame info uses the address immediately after the saved
  1735. return address. So you end up with a dummy frame whose CFA
  1736. points at the return address, but the frame for the function
  1737. being called has a CFA pointing after the return address: the
  1738. younger CFA is *greater than* the older CFA. The sanity checks
  1739. in frame.c don't like that.
  1740. So we try to be consistent with the CFA's used by DWARF 2.
  1741. Having a dummy frame and a real frame with the *same* CFA is
  1742. tolerable. */
  1743. return cfa;
  1744. }
  1745. /* Return values. */
  1746. /* Return value conventions, according to GCC:
  1747. r8c, m16c
  1748. ---------
  1749. QImode in r0l
  1750. HImode in r0
  1751. SImode in r2r0
  1752. near pointer in r0
  1753. far pointer in r2r0
  1754. Aggregate values (regardless of size) are returned by pushing a
  1755. pointer to a temporary area on the stack after the args are pushed.
  1756. The function fills in this area with the value. Note that this
  1757. pointer on the stack does not affect how register arguments, if any,
  1758. are configured.
  1759. m32cm, m32c
  1760. -----------
  1761. Same. */
  1762. /* Return non-zero if values of type TYPE are returned by storing them
  1763. in a buffer whose address is passed on the stack, ahead of the
  1764. other arguments. */
  1765. static int
  1766. m32c_return_by_passed_buf (struct type *type)
  1767. {
  1768. enum type_code code = type->code ();
  1769. return (code == TYPE_CODE_STRUCT
  1770. || code == TYPE_CODE_UNION);
  1771. }
  1772. static enum return_value_convention
  1773. m32c_return_value (struct gdbarch *gdbarch,
  1774. struct value *function,
  1775. struct type *valtype,
  1776. struct regcache *regcache,
  1777. gdb_byte *readbuf,
  1778. const gdb_byte *writebuf)
  1779. {
  1780. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1781. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1782. enum return_value_convention conv;
  1783. ULONGEST valtype_len = TYPE_LENGTH (valtype);
  1784. if (m32c_return_by_passed_buf (valtype))
  1785. conv = RETURN_VALUE_STRUCT_CONVENTION;
  1786. else
  1787. conv = RETURN_VALUE_REGISTER_CONVENTION;
  1788. if (readbuf)
  1789. {
  1790. /* We should never be called to find values being returned by
  1791. RETURN_VALUE_STRUCT_CONVENTION. Those can't be located,
  1792. unless we made the call ourselves. */
  1793. gdb_assert (conv == RETURN_VALUE_REGISTER_CONVENTION);
  1794. gdb_assert (valtype_len <= 8);
  1795. /* Anything that fits in r0 is returned there. */
  1796. if (valtype_len <= TYPE_LENGTH (tdep->r0->type))
  1797. {
  1798. ULONGEST u;
  1799. regcache_cooked_read_unsigned (regcache, tdep->r0->num, &u);
  1800. store_unsigned_integer (readbuf, valtype_len, byte_order, u);
  1801. }
  1802. else
  1803. {
  1804. /* Everything else is passed in mem0, using as many bytes as
  1805. needed. This is not what the Renesas tools do, but it's
  1806. what GCC does at the moment. */
  1807. struct bound_minimal_symbol mem0
  1808. = lookup_minimal_symbol ("mem0", NULL, NULL);
  1809. if (! mem0.minsym)
  1810. error (_("The return value is stored in memory at 'mem0', "
  1811. "but GDB cannot find\n"
  1812. "its address."));
  1813. read_memory (BMSYMBOL_VALUE_ADDRESS (mem0), readbuf, valtype_len);
  1814. }
  1815. }
  1816. if (writebuf)
  1817. {
  1818. /* We should never be called to store values to be returned
  1819. using RETURN_VALUE_STRUCT_CONVENTION. We have no way of
  1820. finding the buffer, unless we made the call ourselves. */
  1821. gdb_assert (conv == RETURN_VALUE_REGISTER_CONVENTION);
  1822. gdb_assert (valtype_len <= 8);
  1823. /* Anything that fits in r0 is returned there. */
  1824. if (valtype_len <= TYPE_LENGTH (tdep->r0->type))
  1825. {
  1826. ULONGEST u = extract_unsigned_integer (writebuf, valtype_len,
  1827. byte_order);
  1828. regcache_cooked_write_unsigned (regcache, tdep->r0->num, u);
  1829. }
  1830. else
  1831. {
  1832. /* Everything else is passed in mem0, using as many bytes as
  1833. needed. This is not what the Renesas tools do, but it's
  1834. what GCC does at the moment. */
  1835. struct bound_minimal_symbol mem0
  1836. = lookup_minimal_symbol ("mem0", NULL, NULL);
  1837. if (! mem0.minsym)
  1838. error (_("The return value is stored in memory at 'mem0', "
  1839. "but GDB cannot find\n"
  1840. " its address."));
  1841. write_memory (BMSYMBOL_VALUE_ADDRESS (mem0), writebuf, valtype_len);
  1842. }
  1843. }
  1844. return conv;
  1845. }
  1846. /* Trampolines. */
  1847. /* The m16c and m32c use a trampoline function for indirect function
  1848. calls. An indirect call looks like this:
  1849. ... push arguments ...
  1850. ... push target function address ...
  1851. jsr.a m32c_jsri16
  1852. The code for m32c_jsri16 looks like this:
  1853. m32c_jsri16:
  1854. # Save return address.
  1855. pop.w m32c_jsri_ret
  1856. pop.b m32c_jsri_ret+2
  1857. # Store target function address.
  1858. pop.w m32c_jsri_addr
  1859. # Re-push return address.
  1860. push.b m32c_jsri_ret+2
  1861. push.w m32c_jsri_ret
  1862. # Call the target function.
  1863. jmpi.a m32c_jsri_addr
  1864. Without further information, GDB will treat calls to m32c_jsri16
  1865. like calls to any other function. Since m32c_jsri16 doesn't have
  1866. debugging information, that normally means that GDB sets a step-
  1867. resume breakpoint and lets the program continue --- which is not
  1868. what the user wanted. (Giving the trampoline debugging info
  1869. doesn't help: the user expects the program to stop in the function
  1870. their program is calling, not in some trampoline code they've never
  1871. seen before.)
  1872. The gdbarch_skip_trampoline_code method tells GDB how to step
  1873. through such trampoline functions transparently to the user. When
  1874. given the address of a trampoline function's first instruction,
  1875. gdbarch_skip_trampoline_code should return the address of the first
  1876. instruction of the function really being called. If GDB decides it
  1877. wants to step into that function, it will set a breakpoint there
  1878. and silently continue to it.
  1879. We recognize the trampoline by name, and extract the target address
  1880. directly from the stack. This isn't great, but recognizing by its
  1881. code sequence seems more fragile. */
  1882. static CORE_ADDR
  1883. m32c_skip_trampoline_code (struct frame_info *frame, CORE_ADDR stop_pc)
  1884. {
  1885. struct gdbarch *gdbarch = get_frame_arch (frame);
  1886. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  1887. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1888. /* It would be nicer to simply look up the addresses of known
  1889. trampolines once, and then compare stop_pc with them. However,
  1890. we'd need to ensure that that cached address got invalidated when
  1891. someone loaded a new executable, and I'm not quite sure of the
  1892. best way to do that. find_pc_partial_function does do some
  1893. caching, so we'll see how this goes. */
  1894. const char *name;
  1895. CORE_ADDR start, end;
  1896. if (find_pc_partial_function (stop_pc, &name, &start, &end))
  1897. {
  1898. /* Are we stopped at the beginning of the trampoline function? */
  1899. if (strcmp (name, "m32c_jsri16") == 0
  1900. && stop_pc == start)
  1901. {
  1902. /* Get the stack pointer. The return address is at the top,
  1903. and the target function's address is just below that. We
  1904. know it's a two-byte address, since the trampoline is
  1905. m32c_jsri*16*. */
  1906. CORE_ADDR sp = get_frame_sp (get_current_frame ());
  1907. CORE_ADDR target
  1908. = read_memory_unsigned_integer (sp + tdep->ret_addr_bytes,
  1909. 2, byte_order);
  1910. /* What we have now is the address of a jump instruction.
  1911. What we need is the destination of that jump.
  1912. The opcode is 1 byte, and the destination is the next 3 bytes. */
  1913. target = read_memory_unsigned_integer (target + 1, 3, byte_order);
  1914. return target;
  1915. }
  1916. }
  1917. return 0;
  1918. }
  1919. /* Address/pointer conversions. */
  1920. /* On the m16c, there is a 24-bit address space, but only a very few
  1921. instructions can generate addresses larger than 0xffff: jumps,
  1922. jumps to subroutines, and the lde/std (load/store extended)
  1923. instructions.
  1924. Since GCC can only support one size of pointer, we can't have
  1925. distinct 'near' and 'far' pointer types; we have to pick one size
  1926. for everything. If we wanted to use 24-bit pointers, then GCC
  1927. would have to use lde and ste for all memory references, which
  1928. would be terrible for performance and code size. So the GNU
  1929. toolchain uses 16-bit pointers for everything, and gives up the
  1930. ability to have pointers point outside the first 64k of memory.
  1931. However, as a special hack, we let the linker place functions at
  1932. addresses above 0xffff, as long as it also places a trampoline in
  1933. the low 64k for every function whose address is taken. Each
  1934. trampoline consists of a single jmp.a instruction that jumps to the
  1935. function's real entry point. Pointers to functions can be 16 bits
  1936. long, even though the functions themselves are at higher addresses:
  1937. the pointers refer to the trampolines, not the functions.
  1938. This complicates things for GDB, however: given the address of a
  1939. function (from debug info or linker symbols, say) which could be
  1940. anywhere in the 24-bit address space, how can we find an
  1941. appropriate 16-bit value to use as a pointer to it?
  1942. If the linker has not generated a trampoline for the function,
  1943. we're out of luck. Well, I guess we could malloc some space and
  1944. write a jmp.a instruction to it, but I'm not going to get into that
  1945. at the moment.
  1946. If the linker has generated a trampoline for the function, then it
  1947. also emitted a symbol for the trampoline: if the function's linker
  1948. symbol is named NAME, then the function's trampoline's linker
  1949. symbol is named NAME.plt.
  1950. So, given a code address:
  1951. - We try to find a linker symbol at that address.
  1952. - If we find such a symbol named NAME, we look for a linker symbol
  1953. named NAME.plt.
  1954. - If we find such a symbol, we assume it is a trampoline, and use
  1955. its address as the pointer value.
  1956. And, given a function pointer:
  1957. - We try to find a linker symbol at that address named NAME.plt.
  1958. - If we find such a symbol, we look for a linker symbol named NAME.
  1959. - If we find that, we provide that as the function's address.
  1960. - If any of the above steps fail, we return the original address
  1961. unchanged; it might really be a function in the low 64k.
  1962. See? You *knew* there was a reason you wanted to be a computer
  1963. programmer! :) */
  1964. static void
  1965. m32c_m16c_address_to_pointer (struct gdbarch *gdbarch,
  1966. struct type *type, gdb_byte *buf, CORE_ADDR addr)
  1967. {
  1968. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1969. enum type_code target_code;
  1970. gdb_assert (type->code () == TYPE_CODE_PTR || TYPE_IS_REFERENCE (type));
  1971. target_code = TYPE_TARGET_TYPE (type)->code ();
  1972. if (target_code == TYPE_CODE_FUNC || target_code == TYPE_CODE_METHOD)
  1973. {
  1974. const char *func_name;
  1975. char *tramp_name;
  1976. struct bound_minimal_symbol tramp_msym;
  1977. /* Try to find a linker symbol at this address. */
  1978. struct bound_minimal_symbol func_msym
  1979. = lookup_minimal_symbol_by_pc (addr);
  1980. if (! func_msym.minsym)
  1981. error (_("Cannot convert code address %s to function pointer:\n"
  1982. "couldn't find a symbol at that address, to find trampoline."),
  1983. paddress (gdbarch, addr));
  1984. func_name = func_msym.minsym->linkage_name ();
  1985. tramp_name = (char *) xmalloc (strlen (func_name) + 5);
  1986. strcpy (tramp_name, func_name);
  1987. strcat (tramp_name, ".plt");
  1988. /* Try to find a linker symbol for the trampoline. */
  1989. tramp_msym = lookup_minimal_symbol (tramp_name, NULL, NULL);
  1990. /* We've either got another copy of the name now, or don't need
  1991. the name any more. */
  1992. xfree (tramp_name);
  1993. if (! tramp_msym.minsym)
  1994. {
  1995. CORE_ADDR ptrval;
  1996. /* No PLT entry found. Mask off the upper bits of the address
  1997. to make a pointer. As noted in the warning to the user
  1998. below, this value might be useful if converted back into
  1999. an address by GDB, but will otherwise, almost certainly,
  2000. be garbage.
  2001. Using this masked result does seem to be useful
  2002. in gdb.cp/cplusfuncs.exp in which ~40 FAILs turn into
  2003. PASSes. These results appear to be correct as well.
  2004. We print a warning here so that the user can make a
  2005. determination about whether the result is useful or not. */
  2006. ptrval = addr & 0xffff;
  2007. warning (_("Cannot convert code address %s to function pointer:\n"
  2008. "couldn't find trampoline named '%s.plt'.\n"
  2009. "Returning pointer value %s instead; this may produce\n"
  2010. "a useful result if converted back into an address by GDB,\n"
  2011. "but will most likely not be useful otherwise."),
  2012. paddress (gdbarch, addr), func_name,
  2013. paddress (gdbarch, ptrval));
  2014. addr = ptrval;
  2015. }
  2016. else
  2017. {
  2018. /* The trampoline's address is our pointer. */
  2019. addr = BMSYMBOL_VALUE_ADDRESS (tramp_msym);
  2020. }
  2021. }
  2022. store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order, addr);
  2023. }
  2024. static CORE_ADDR
  2025. m32c_m16c_pointer_to_address (struct gdbarch *gdbarch,
  2026. struct type *type, const gdb_byte *buf)
  2027. {
  2028. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  2029. CORE_ADDR ptr;
  2030. enum type_code target_code;
  2031. gdb_assert (type->code () == TYPE_CODE_PTR || TYPE_IS_REFERENCE (type));
  2032. ptr = extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);
  2033. target_code = TYPE_TARGET_TYPE (type)->code ();
  2034. if (target_code == TYPE_CODE_FUNC || target_code == TYPE_CODE_METHOD)
  2035. {
  2036. /* See if there is a minimal symbol at that address whose name is
  2037. "NAME.plt". */
  2038. struct bound_minimal_symbol ptr_msym = lookup_minimal_symbol_by_pc (ptr);
  2039. if (ptr_msym.minsym)
  2040. {
  2041. const char *ptr_msym_name = ptr_msym.minsym->linkage_name ();
  2042. int len = strlen (ptr_msym_name);
  2043. if (len > 4
  2044. && strcmp (ptr_msym_name + len - 4, ".plt") == 0)
  2045. {
  2046. struct bound_minimal_symbol func_msym;
  2047. /* We have a .plt symbol; try to find the symbol for the
  2048. corresponding function.
  2049. Since the trampoline contains a jump instruction, we
  2050. could also just extract the jump's target address. I
  2051. don't see much advantage one way or the other. */
  2052. char *func_name = (char *) xmalloc (len - 4 + 1);
  2053. memcpy (func_name, ptr_msym_name, len - 4);
  2054. func_name[len - 4] = '\0';
  2055. func_msym
  2056. = lookup_minimal_symbol (func_name, NULL, NULL);
  2057. /* If we do have such a symbol, return its value as the
  2058. function's true address. */
  2059. if (func_msym.minsym)
  2060. ptr = BMSYMBOL_VALUE_ADDRESS (func_msym);
  2061. }
  2062. }
  2063. else
  2064. {
  2065. int aspace;
  2066. for (aspace = 1; aspace <= 15; aspace++)
  2067. {
  2068. ptr_msym = lookup_minimal_symbol_by_pc ((aspace << 16) | ptr);
  2069. if (ptr_msym.minsym)
  2070. ptr |= aspace << 16;
  2071. }
  2072. }
  2073. }
  2074. return ptr;
  2075. }
  2076. static void
  2077. m32c_virtual_frame_pointer (struct gdbarch *gdbarch, CORE_ADDR pc,
  2078. int *frame_regnum,
  2079. LONGEST *frame_offset)
  2080. {
  2081. const char *name;
  2082. CORE_ADDR func_addr, func_end;
  2083. struct m32c_prologue p;
  2084. struct regcache *regcache = get_current_regcache ();
  2085. m32c_gdbarch_tdep *tdep = (m32c_gdbarch_tdep *) gdbarch_tdep (gdbarch);
  2086. if (!find_pc_partial_function (pc, &name, &func_addr, &func_end))
  2087. internal_error (__FILE__, __LINE__,
  2088. _("No virtual frame pointer available"));
  2089. m32c_analyze_prologue (gdbarch, func_addr, pc, &p);
  2090. switch (p.kind)
  2091. {
  2092. case prologue_with_frame_ptr:
  2093. *frame_regnum = m32c_banked_register (tdep->fb, regcache)->num;
  2094. *frame_offset = p.frame_ptr_offset;
  2095. break;
  2096. case prologue_sans_frame_ptr:
  2097. *frame_regnum = m32c_banked_register (tdep->sp, regcache)->num;
  2098. *frame_offset = p.frame_size;
  2099. break;
  2100. default:
  2101. *frame_regnum = m32c_banked_register (tdep->sp, regcache)->num;
  2102. *frame_offset = 0;
  2103. break;
  2104. }
  2105. /* Sanity check */
  2106. if (*frame_regnum > gdbarch_num_regs (gdbarch))
  2107. internal_error (__FILE__, __LINE__,
  2108. _("No virtual frame pointer available"));
  2109. }
  2110. /* Initialization. */
  2111. static struct gdbarch *
  2112. m32c_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  2113. {
  2114. struct gdbarch *gdbarch;
  2115. unsigned long mach = info.bfd_arch_info->mach;
  2116. /* Find a candidate among the list of architectures we've created
  2117. already. */
  2118. for (arches = gdbarch_list_lookup_by_info (arches, &info);
  2119. arches != NULL;
  2120. arches = gdbarch_list_lookup_by_info (arches->next, &info))
  2121. return arches->gdbarch;
  2122. m32c_gdbarch_tdep *tdep = new m32c_gdbarch_tdep;
  2123. gdbarch = gdbarch_alloc (&info, tdep);
  2124. /* Essential types. */
  2125. make_types (gdbarch);
  2126. /* Address/pointer conversions. */
  2127. if (mach == bfd_mach_m16c)
  2128. {
  2129. set_gdbarch_address_to_pointer (gdbarch, m32c_m16c_address_to_pointer);
  2130. set_gdbarch_pointer_to_address (gdbarch, m32c_m16c_pointer_to_address);
  2131. }
  2132. /* Register set. */
  2133. make_regs (gdbarch);
  2134. /* Breakpoints. */
  2135. set_gdbarch_breakpoint_kind_from_pc (gdbarch, m32c_breakpoint::kind_from_pc);
  2136. set_gdbarch_sw_breakpoint_from_kind (gdbarch, m32c_breakpoint::bp_from_kind);
  2137. /* Prologue analysis and unwinding. */
  2138. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  2139. set_gdbarch_skip_prologue (gdbarch, m32c_skip_prologue);
  2140. #if 0
  2141. /* I'm dropping the dwarf2 sniffer because it has a few problems.
  2142. They may be in the dwarf2 cfi code in GDB, or they may be in
  2143. the debug info emitted by the upstream toolchain. I don't
  2144. know which, but I do know that the prologue analyzer works better.
  2145. MVS 04/13/06 */
  2146. dwarf2_append_sniffers (gdbarch);
  2147. #endif
  2148. frame_unwind_append_unwinder (gdbarch, &m32c_unwind);
  2149. /* Inferior calls. */
  2150. set_gdbarch_push_dummy_call (gdbarch, m32c_push_dummy_call);
  2151. set_gdbarch_return_value (gdbarch, m32c_return_value);
  2152. /* Trampolines. */
  2153. set_gdbarch_skip_trampoline_code (gdbarch, m32c_skip_trampoline_code);
  2154. set_gdbarch_virtual_frame_pointer (gdbarch, m32c_virtual_frame_pointer);
  2155. /* m32c function boundary addresses are not necessarily even.
  2156. Therefore, the `vbit', which indicates a pointer to a virtual
  2157. member function, is stored in the delta field, rather than as
  2158. the low bit of a function pointer address.
  2159. In order to verify this, see the definition of
  2160. TARGET_PTRMEMFUNC_VBIT_LOCATION in gcc/defaults.h along with the
  2161. definition of FUNCTION_BOUNDARY in gcc/config/m32c/m32c.h. */
  2162. set_gdbarch_vbit_in_delta (gdbarch, 1);
  2163. return gdbarch;
  2164. }
  2165. void _initialize_m32c_tdep ();
  2166. void
  2167. _initialize_m32c_tdep ()
  2168. {
  2169. register_gdbarch_init (bfd_arch_m32c, m32c_gdbarch_init);
  2170. m32c_dma_reggroup = reggroup_new ("dma", USER_REGGROUP);
  2171. }