mep-tdep.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469
  1. /* Target-dependent code for the Toshiba MeP for GDB, the GNU debugger.
  2. Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. Contributed by Red Hat, Inc.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "frame.h"
  17. #include "frame-unwind.h"
  18. #include "frame-base.h"
  19. #include "symtab.h"
  20. #include "gdbtypes.h"
  21. #include "gdbcmd.h"
  22. #include "gdbcore.h"
  23. #include "value.h"
  24. #include "inferior.h"
  25. #include "dis-asm.h"
  26. #include "symfile.h"
  27. #include "objfiles.h"
  28. #include "language.h"
  29. #include "arch-utils.h"
  30. #include "regcache.h"
  31. #include "remote.h"
  32. #include "sim-regno.h"
  33. #include "trad-frame.h"
  34. #include "reggroups.h"
  35. #include "elf-bfd.h"
  36. #include "elf/mep.h"
  37. #include "prologue-value.h"
  38. #include "cgen/bitset.h"
  39. #include "infcall.h"
  40. #include "gdbarch.h"
  41. /* Get the user's customized MeP coprocessor register names from
  42. libopcodes. */
  43. #include "opcodes/mep-desc.h"
  44. #include "opcodes/mep-opc.h"
  45. /* The gdbarch_tdep structure. */
  46. /* A quick recap for GDB hackers not familiar with the whole Toshiba
  47. Media Processor story:
  48. The MeP media engine is a configureable processor: users can design
  49. their own coprocessors, implement custom instructions, adjust cache
  50. sizes, select optional standard facilities like add-and-saturate
  51. instructions, and so on. Then, they can build custom versions of
  52. the GNU toolchain to support their customized chips. The
  53. MeP-Integrator program (see utils/mep) takes a GNU toolchain source
  54. tree, and a config file pointing to various files provided by the
  55. user describing their customizations, and edits the source tree to
  56. produce a compiler that can generate their custom instructions, an
  57. assembler that can assemble them and recognize their custom
  58. register names, and so on.
  59. Furthermore, the user can actually specify several of these custom
  60. configurations, called 'me_modules', and get a toolchain which can
  61. produce code for any of them, given a compiler/assembler switch;
  62. you say something like 'gcc -mconfig=mm_max' to generate code for
  63. the me_module named 'mm_max'.
  64. GDB, in particular, needs to:
  65. - use the coprocessor control register names provided by the user
  66. in their hardware description, in expressions, 'info register'
  67. output, and disassembly,
  68. - know the number, names, and types of the coprocessor's
  69. general-purpose registers, adjust the 'info all-registers' output
  70. accordingly, and print error messages if the user refers to one
  71. that doesn't exist
  72. - allow access to the control bus space only when the configuration
  73. actually has a control bus, and recognize which regions of the
  74. control bus space are actually populated,
  75. - disassemble using the user's provided mnemonics for their custom
  76. instructions, and
  77. - recognize whether the $hi and $lo registers are present, and
  78. allow access to them only when they are actually there.
  79. There are three sources of information about what sort of me_module
  80. we're actually dealing with:
  81. - A MeP executable file indicates which me_module it was compiled
  82. for, and libopcodes has tables describing each module. So, given
  83. an executable file, we can find out about the processor it was
  84. compiled for.
  85. - There are SID command-line options to select a particular
  86. me_module, overriding the one specified in the ELF file. SID
  87. provides GDB with a fake read-only register, 'module', which
  88. indicates which me_module GDB is communicating with an instance
  89. of.
  90. - There are SID command-line options to enable or disable certain
  91. optional processor features, overriding the defaults for the
  92. selected me_module. The MeP $OPT register indicates which
  93. options are present on the current processor. */
  94. struct mep_gdbarch_tdep : gdbarch_tdep
  95. {
  96. /* A CGEN cpu descriptor for this BFD architecture and machine.
  97. Note: this is *not* customized for any particular me_module; the
  98. MeP libopcodes machinery actually puts off module-specific
  99. customization until the last minute. So this contains
  100. information about all supported me_modules. */
  101. CGEN_CPU_DESC cpu_desc = nullptr;
  102. /* The me_module index from the ELF file we used to select this
  103. architecture, or CONFIG_NONE if there was none.
  104. Note that we should prefer to use the me_module number available
  105. via the 'module' register, whenever we're actually talking to a
  106. real target.
  107. In the absence of live information, we'd like to get the
  108. me_module number from the ELF file. But which ELF file: the
  109. executable file, the core file, ... ? The answer is, "the last
  110. ELF file we used to set the current architecture". Thus, we
  111. create a separate instance of the gdbarch structure for each
  112. me_module value mep_gdbarch_init sees, and store the me_module
  113. value from the ELF file here. */
  114. CONFIG_ATTR me_module {};
  115. };
  116. /* Getting me_module information from the CGEN tables. */
  117. /* Find an entry in the DESC's hardware table whose name begins with
  118. PREFIX, and whose ISA mask intersects COPRO_ISA_MASK, but does not
  119. intersect with GENERIC_ISA_MASK. If there is no matching entry,
  120. return zero. */
  121. static const CGEN_HW_ENTRY *
  122. find_hw_entry_by_prefix_and_isa (CGEN_CPU_DESC desc,
  123. const char *prefix,
  124. CGEN_BITSET *copro_isa_mask,
  125. CGEN_BITSET *generic_isa_mask)
  126. {
  127. int prefix_len = strlen (prefix);
  128. int i;
  129. for (i = 0; i < desc->hw_table.num_entries; i++)
  130. {
  131. const CGEN_HW_ENTRY *hw = desc->hw_table.entries[i];
  132. if (strncmp (prefix, hw->name, prefix_len) == 0)
  133. {
  134. CGEN_BITSET *hw_isa_mask
  135. = ((CGEN_BITSET *)
  136. &CGEN_ATTR_CGEN_HW_ISA_VALUE (CGEN_HW_ATTRS (hw)));
  137. if (cgen_bitset_intersect_p (hw_isa_mask, copro_isa_mask)
  138. && ! cgen_bitset_intersect_p (hw_isa_mask, generic_isa_mask))
  139. return hw;
  140. }
  141. }
  142. return 0;
  143. }
  144. /* Find an entry in DESC's hardware table whose type is TYPE. Return
  145. zero if there is none. */
  146. static const CGEN_HW_ENTRY *
  147. find_hw_entry_by_type (CGEN_CPU_DESC desc, CGEN_HW_TYPE type)
  148. {
  149. int i;
  150. for (i = 0; i < desc->hw_table.num_entries; i++)
  151. {
  152. const CGEN_HW_ENTRY *hw = desc->hw_table.entries[i];
  153. if (hw->type == type)
  154. return hw;
  155. }
  156. return 0;
  157. }
  158. /* Return the CGEN hardware table entry for the coprocessor register
  159. set for ME_MODULE, whose name prefix is PREFIX. If ME_MODULE has
  160. no such register set, return zero. If ME_MODULE is the generic
  161. me_module CONFIG_NONE, return the table entry for the register set
  162. whose hardware type is GENERIC_TYPE. */
  163. static const CGEN_HW_ENTRY *
  164. me_module_register_set (CONFIG_ATTR me_module,
  165. const char *prefix,
  166. CGEN_HW_TYPE generic_type)
  167. {
  168. /* This is kind of tricky, because the hardware table is constructed
  169. in a way that isn't very helpful. Perhaps we can fix that, but
  170. here's how it works at the moment:
  171. The configuration map, `mep_config_map', is indexed by me_module
  172. number, and indicates which coprocessor and core ISAs that
  173. me_module supports. The 'core_isa' mask includes all the core
  174. ISAs, and the 'cop_isa' mask includes all the coprocessor ISAs.
  175. The entry for the generic me_module, CONFIG_NONE, has an empty
  176. 'cop_isa', and its 'core_isa' selects only the standard MeP
  177. instruction set.
  178. The CGEN CPU descriptor's hardware table, desc->hw_table, has
  179. entries for all the register sets, for all me_modules. Each
  180. entry has a mask indicating which ISAs use that register set.
  181. So, if an me_module supports some coprocessor ISA, we can find
  182. applicable register sets by scanning the hardware table for
  183. register sets whose masks include (at least some of) those ISAs.
  184. Each hardware table entry also has a name, whose prefix says
  185. whether it's a general-purpose ("h-cr") or control ("h-ccr")
  186. coprocessor register set. It might be nicer to have an attribute
  187. indicating what sort of register set it was, that we could use
  188. instead of pattern-matching on the name.
  189. When there is no hardware table entry whose mask includes a
  190. particular coprocessor ISA and whose name starts with a given
  191. prefix, then that means that that coprocessor doesn't have any
  192. registers of that type. In such cases, this function must return
  193. a null pointer.
  194. Coprocessor register sets' masks may or may not include the core
  195. ISA for the me_module they belong to. Those generated by a2cgen
  196. do, but the sample me_module included in the unconfigured tree,
  197. 'ccfx', does not.
  198. There are generic coprocessor register sets, intended only for
  199. use with the generic me_module. Unfortunately, their masks
  200. include *all* ISAs --- even those for coprocessors that don't
  201. have such register sets. This makes detecting the case where a
  202. coprocessor lacks a particular register set more complicated.
  203. So, here's the approach we take:
  204. - For CONFIG_NONE, we return the generic coprocessor register set.
  205. - For any other me_module, we search for a register set whose
  206. mask contains any of the me_module's coprocessor ISAs,
  207. specifically excluding the generic coprocessor register sets. */
  208. mep_gdbarch_tdep *tdep
  209. = (mep_gdbarch_tdep *) gdbarch_tdep (target_gdbarch ());
  210. CGEN_CPU_DESC desc = tdep->cpu_desc;
  211. const CGEN_HW_ENTRY *hw;
  212. if (me_module == CONFIG_NONE)
  213. hw = find_hw_entry_by_type (desc, generic_type);
  214. else
  215. {
  216. CGEN_BITSET *cop = &mep_config_map[me_module].cop_isa;
  217. CGEN_BITSET *core = &mep_config_map[me_module].core_isa;
  218. CGEN_BITSET *generic = &mep_config_map[CONFIG_NONE].core_isa;
  219. CGEN_BITSET *cop_and_core;
  220. /* The coprocessor ISAs include the ISA for the specific core which
  221. has that coprocessor. */
  222. cop_and_core = cgen_bitset_copy (cop);
  223. cgen_bitset_union (cop, core, cop_and_core);
  224. hw = find_hw_entry_by_prefix_and_isa (desc, prefix, cop_and_core, generic);
  225. }
  226. return hw;
  227. }
  228. /* Given a hardware table entry HW representing a register set, return
  229. a pointer to the keyword table with all the register names. If HW
  230. is NULL, return NULL, to propagate the "no such register set" info
  231. along. */
  232. static CGEN_KEYWORD *
  233. register_set_keyword_table (const CGEN_HW_ENTRY *hw)
  234. {
  235. if (! hw)
  236. return NULL;
  237. /* Check that HW is actually a keyword table. */
  238. gdb_assert (hw->asm_type == CGEN_ASM_KEYWORD);
  239. /* The 'asm_data' field of a register set's hardware table entry
  240. refers to a keyword table. */
  241. return (CGEN_KEYWORD *) hw->asm_data;
  242. }
  243. /* Given a keyword table KEYWORD and a register number REGNUM, return
  244. the name of the register, or "" if KEYWORD contains no register
  245. whose number is REGNUM. */
  246. static const char *
  247. register_name_from_keyword (CGEN_KEYWORD *keyword_table, int regnum)
  248. {
  249. const CGEN_KEYWORD_ENTRY *entry
  250. = cgen_keyword_lookup_value (keyword_table, regnum);
  251. if (entry)
  252. {
  253. char *name = entry->name;
  254. /* The CGEN keyword entries for register names include the
  255. leading $, which appears in MeP assembly as well as in GDB.
  256. But we don't want to return that; GDB core code adds that
  257. itself. */
  258. if (name[0] == '$')
  259. name++;
  260. return name;
  261. }
  262. else
  263. return "";
  264. }
  265. /* Masks for option bits in the OPT special-purpose register. */
  266. enum {
  267. MEP_OPT_DIV = 1 << 25, /* 32-bit divide instruction option */
  268. MEP_OPT_MUL = 1 << 24, /* 32-bit multiply instruction option */
  269. MEP_OPT_BIT = 1 << 23, /* bit manipulation instruction option */
  270. MEP_OPT_SAT = 1 << 22, /* saturation instruction option */
  271. MEP_OPT_CLP = 1 << 21, /* clip instruction option */
  272. MEP_OPT_MIN = 1 << 20, /* min/max instruction option */
  273. MEP_OPT_AVE = 1 << 19, /* average instruction option */
  274. MEP_OPT_ABS = 1 << 18, /* absolute difference instruction option */
  275. MEP_OPT_LDZ = 1 << 16, /* leading zero instruction option */
  276. MEP_OPT_VL64 = 1 << 6, /* 64-bit VLIW operation mode option */
  277. MEP_OPT_VL32 = 1 << 5, /* 32-bit VLIW operation mode option */
  278. MEP_OPT_COP = 1 << 4, /* coprocessor option */
  279. MEP_OPT_DSP = 1 << 2, /* DSP option */
  280. MEP_OPT_UCI = 1 << 1, /* UCI option */
  281. MEP_OPT_DBG = 1 << 0, /* DBG function option */
  282. };
  283. /* Given the option_mask value for a particular entry in
  284. mep_config_map, produce the value the processor's OPT register
  285. would use to represent the same set of options. */
  286. static unsigned int
  287. opt_from_option_mask (unsigned int option_mask)
  288. {
  289. /* A table mapping OPT register bits onto CGEN config map option
  290. bits. */
  291. struct {
  292. unsigned int opt_bit, option_mask_bit;
  293. } bits[] = {
  294. { MEP_OPT_DIV, 1 << CGEN_INSN_OPTIONAL_DIV_INSN },
  295. { MEP_OPT_MUL, 1 << CGEN_INSN_OPTIONAL_MUL_INSN },
  296. { MEP_OPT_DIV, 1 << CGEN_INSN_OPTIONAL_DIV_INSN },
  297. { MEP_OPT_DBG, 1 << CGEN_INSN_OPTIONAL_DEBUG_INSN },
  298. { MEP_OPT_LDZ, 1 << CGEN_INSN_OPTIONAL_LDZ_INSN },
  299. { MEP_OPT_ABS, 1 << CGEN_INSN_OPTIONAL_ABS_INSN },
  300. { MEP_OPT_AVE, 1 << CGEN_INSN_OPTIONAL_AVE_INSN },
  301. { MEP_OPT_MIN, 1 << CGEN_INSN_OPTIONAL_MINMAX_INSN },
  302. { MEP_OPT_CLP, 1 << CGEN_INSN_OPTIONAL_CLIP_INSN },
  303. { MEP_OPT_SAT, 1 << CGEN_INSN_OPTIONAL_SAT_INSN },
  304. { MEP_OPT_UCI, 1 << CGEN_INSN_OPTIONAL_UCI_INSN },
  305. { MEP_OPT_DSP, 1 << CGEN_INSN_OPTIONAL_DSP_INSN },
  306. { MEP_OPT_COP, 1 << CGEN_INSN_OPTIONAL_CP_INSN },
  307. };
  308. int i;
  309. unsigned int opt = 0;
  310. for (i = 0; i < (sizeof (bits) / sizeof (bits[0])); i++)
  311. if (option_mask & bits[i].option_mask_bit)
  312. opt |= bits[i].opt_bit;
  313. return opt;
  314. }
  315. /* Return the value the $OPT register would use to represent the set
  316. of options for ME_MODULE. */
  317. static unsigned int
  318. me_module_opt (CONFIG_ATTR me_module)
  319. {
  320. return opt_from_option_mask (mep_config_map[me_module].option_mask);
  321. }
  322. /* Return the width of ME_MODULE's coprocessor data bus, in bits.
  323. This is either 32 or 64. */
  324. static int
  325. me_module_cop_data_bus_width (CONFIG_ATTR me_module)
  326. {
  327. if (mep_config_map[me_module].option_mask
  328. & (1 << CGEN_INSN_OPTIONAL_CP64_INSN))
  329. return 64;
  330. else
  331. return 32;
  332. }
  333. /* Return true if ME_MODULE is big-endian, false otherwise. */
  334. static int
  335. me_module_big_endian (CONFIG_ATTR me_module)
  336. {
  337. return mep_config_map[me_module].big_endian;
  338. }
  339. /* Return the name of ME_MODULE, or NULL if it has no name. */
  340. static const char *
  341. me_module_name (CONFIG_ATTR me_module)
  342. {
  343. /* The default me_module has "" as its name, but it's easier for our
  344. callers to test for NULL. */
  345. if (! mep_config_map[me_module].name
  346. || mep_config_map[me_module].name[0] == '\0')
  347. return NULL;
  348. else
  349. return mep_config_map[me_module].name;
  350. }
  351. /* Register set. */
  352. /* The MeP spec defines the following registers:
  353. 16 general purpose registers (r0-r15)
  354. 32 control/special registers (csr0-csr31)
  355. 32 coprocessor general-purpose registers (c0 -- c31)
  356. 64 coprocessor control registers (ccr0 -- ccr63)
  357. For the raw registers, we assign numbers here explicitly, instead
  358. of letting the enum assign them for us; the numbers are a matter of
  359. external protocol, and shouldn't shift around as things are edited.
  360. We access the control/special registers via pseudoregisters, to
  361. enforce read-only portions that some registers have.
  362. We access the coprocessor general purpose and control registers via
  363. pseudoregisters, to make sure they appear in the proper order in
  364. the 'info all-registers' command (which uses the register number
  365. ordering), and also to allow them to be renamed and resized
  366. depending on the me_module in use.
  367. The MeP allows coprocessor general-purpose registers to be either
  368. 32 or 64 bits long, depending on the configuration. Since we don't
  369. want the format of the 'g' packet to vary from one core to another,
  370. the raw coprocessor GPRs are always 64 bits. GDB doesn't allow the
  371. types of registers to change (see the implementation of
  372. register_type), so we have four banks of pseudoregisters for the
  373. coprocessor gprs --- 32-bit vs. 64-bit, and integer
  374. vs. floating-point --- and we show or hide them depending on the
  375. configuration. */
  376. enum
  377. {
  378. MEP_FIRST_RAW_REGNUM = 0,
  379. MEP_FIRST_GPR_REGNUM = 0,
  380. MEP_R0_REGNUM = 0,
  381. MEP_R1_REGNUM = 1,
  382. MEP_R2_REGNUM = 2,
  383. MEP_R3_REGNUM = 3,
  384. MEP_R4_REGNUM = 4,
  385. MEP_R5_REGNUM = 5,
  386. MEP_R6_REGNUM = 6,
  387. MEP_R7_REGNUM = 7,
  388. MEP_R8_REGNUM = 8,
  389. MEP_R9_REGNUM = 9,
  390. MEP_R10_REGNUM = 10,
  391. MEP_R11_REGNUM = 11,
  392. MEP_R12_REGNUM = 12,
  393. MEP_FP_REGNUM = MEP_R8_REGNUM,
  394. MEP_R13_REGNUM = 13,
  395. MEP_TP_REGNUM = MEP_R13_REGNUM, /* (r13) Tiny data pointer */
  396. MEP_R14_REGNUM = 14,
  397. MEP_GP_REGNUM = MEP_R14_REGNUM, /* (r14) Global pointer */
  398. MEP_R15_REGNUM = 15,
  399. MEP_SP_REGNUM = MEP_R15_REGNUM, /* (r15) Stack pointer */
  400. MEP_LAST_GPR_REGNUM = MEP_R15_REGNUM,
  401. /* The raw control registers. These are the values as received via
  402. the remote protocol, directly from the target; we only let user
  403. code touch the via the pseudoregisters, which enforce read-only
  404. bits. */
  405. MEP_FIRST_RAW_CSR_REGNUM = 16,
  406. MEP_RAW_PC_REGNUM = 16, /* Program counter */
  407. MEP_RAW_LP_REGNUM = 17, /* Link pointer */
  408. MEP_RAW_SAR_REGNUM = 18, /* Raw shift amount */
  409. MEP_RAW_CSR3_REGNUM = 19, /* csr3: reserved */
  410. MEP_RAW_RPB_REGNUM = 20, /* Raw repeat begin address */
  411. MEP_RAW_RPE_REGNUM = 21, /* Repeat end address */
  412. MEP_RAW_RPC_REGNUM = 22, /* Repeat count */
  413. MEP_RAW_HI_REGNUM = 23, /* Upper 32 bits of result of 64 bit mult/div */
  414. MEP_RAW_LO_REGNUM = 24, /* Lower 32 bits of result of 64 bit mult/div */
  415. MEP_RAW_CSR9_REGNUM = 25, /* csr3: reserved */
  416. MEP_RAW_CSR10_REGNUM = 26, /* csr3: reserved */
  417. MEP_RAW_CSR11_REGNUM = 27, /* csr3: reserved */
  418. MEP_RAW_MB0_REGNUM = 28, /* Raw modulo begin address 0 */
  419. MEP_RAW_ME0_REGNUM = 29, /* Raw modulo end address 0 */
  420. MEP_RAW_MB1_REGNUM = 30, /* Raw modulo begin address 1 */
  421. MEP_RAW_ME1_REGNUM = 31, /* Raw modulo end address 1 */
  422. MEP_RAW_PSW_REGNUM = 32, /* Raw program status word */
  423. MEP_RAW_ID_REGNUM = 33, /* Raw processor ID/revision */
  424. MEP_RAW_TMP_REGNUM = 34, /* Temporary */
  425. MEP_RAW_EPC_REGNUM = 35, /* Exception program counter */
  426. MEP_RAW_EXC_REGNUM = 36, /* Raw exception cause */
  427. MEP_RAW_CFG_REGNUM = 37, /* Raw processor configuration*/
  428. MEP_RAW_CSR22_REGNUM = 38, /* csr3: reserved */
  429. MEP_RAW_NPC_REGNUM = 39, /* Nonmaskable interrupt PC */
  430. MEP_RAW_DBG_REGNUM = 40, /* Raw debug */
  431. MEP_RAW_DEPC_REGNUM = 41, /* Debug exception PC */
  432. MEP_RAW_OPT_REGNUM = 42, /* Raw options */
  433. MEP_RAW_RCFG_REGNUM = 43, /* Raw local ram config */
  434. MEP_RAW_CCFG_REGNUM = 44, /* Raw cache config */
  435. MEP_RAW_CSR29_REGNUM = 45, /* csr3: reserved */
  436. MEP_RAW_CSR30_REGNUM = 46, /* csr3: reserved */
  437. MEP_RAW_CSR31_REGNUM = 47, /* csr3: reserved */
  438. MEP_LAST_RAW_CSR_REGNUM = MEP_RAW_CSR31_REGNUM,
  439. /* The raw coprocessor general-purpose registers. These are all 64
  440. bits wide. */
  441. MEP_FIRST_RAW_CR_REGNUM = 48,
  442. MEP_LAST_RAW_CR_REGNUM = MEP_FIRST_RAW_CR_REGNUM + 31,
  443. MEP_FIRST_RAW_CCR_REGNUM = 80,
  444. MEP_LAST_RAW_CCR_REGNUM = MEP_FIRST_RAW_CCR_REGNUM + 63,
  445. /* The module number register. This is the index of the me_module
  446. of which the current target is an instance. (This is not a real
  447. MeP-specified register; it's provided by SID.) */
  448. MEP_MODULE_REGNUM,
  449. MEP_LAST_RAW_REGNUM = MEP_MODULE_REGNUM,
  450. MEP_NUM_RAW_REGS = MEP_LAST_RAW_REGNUM + 1,
  451. /* Pseudoregisters. See mep_pseudo_register_read and
  452. mep_pseudo_register_write. */
  453. MEP_FIRST_PSEUDO_REGNUM = MEP_NUM_RAW_REGS,
  454. /* We have a pseudoregister for every control/special register, to
  455. implement registers with read-only bits. */
  456. MEP_FIRST_CSR_REGNUM = MEP_FIRST_PSEUDO_REGNUM,
  457. MEP_PC_REGNUM = MEP_FIRST_CSR_REGNUM, /* Program counter */
  458. MEP_LP_REGNUM, /* Link pointer */
  459. MEP_SAR_REGNUM, /* shift amount */
  460. MEP_CSR3_REGNUM, /* csr3: reserved */
  461. MEP_RPB_REGNUM, /* repeat begin address */
  462. MEP_RPE_REGNUM, /* Repeat end address */
  463. MEP_RPC_REGNUM, /* Repeat count */
  464. MEP_HI_REGNUM, /* Upper 32 bits of the result of 64 bit mult/div */
  465. MEP_LO_REGNUM, /* Lower 32 bits of the result of 64 bit mult/div */
  466. MEP_CSR9_REGNUM, /* csr3: reserved */
  467. MEP_CSR10_REGNUM, /* csr3: reserved */
  468. MEP_CSR11_REGNUM, /* csr3: reserved */
  469. MEP_MB0_REGNUM, /* modulo begin address 0 */
  470. MEP_ME0_REGNUM, /* modulo end address 0 */
  471. MEP_MB1_REGNUM, /* modulo begin address 1 */
  472. MEP_ME1_REGNUM, /* modulo end address 1 */
  473. MEP_PSW_REGNUM, /* program status word */
  474. MEP_ID_REGNUM, /* processor ID/revision */
  475. MEP_TMP_REGNUM, /* Temporary */
  476. MEP_EPC_REGNUM, /* Exception program counter */
  477. MEP_EXC_REGNUM, /* exception cause */
  478. MEP_CFG_REGNUM, /* processor configuration*/
  479. MEP_CSR22_REGNUM, /* csr3: reserved */
  480. MEP_NPC_REGNUM, /* Nonmaskable interrupt PC */
  481. MEP_DBG_REGNUM, /* debug */
  482. MEP_DEPC_REGNUM, /* Debug exception PC */
  483. MEP_OPT_REGNUM, /* options */
  484. MEP_RCFG_REGNUM, /* local ram config */
  485. MEP_CCFG_REGNUM, /* cache config */
  486. MEP_CSR29_REGNUM, /* csr3: reserved */
  487. MEP_CSR30_REGNUM, /* csr3: reserved */
  488. MEP_CSR31_REGNUM, /* csr3: reserved */
  489. MEP_LAST_CSR_REGNUM = MEP_CSR31_REGNUM,
  490. /* The 32-bit integer view of the coprocessor GPR's. */
  491. MEP_FIRST_CR32_REGNUM,
  492. MEP_LAST_CR32_REGNUM = MEP_FIRST_CR32_REGNUM + 31,
  493. /* The 32-bit floating-point view of the coprocessor GPR's. */
  494. MEP_FIRST_FP_CR32_REGNUM,
  495. MEP_LAST_FP_CR32_REGNUM = MEP_FIRST_FP_CR32_REGNUM + 31,
  496. /* The 64-bit integer view of the coprocessor GPR's. */
  497. MEP_FIRST_CR64_REGNUM,
  498. MEP_LAST_CR64_REGNUM = MEP_FIRST_CR64_REGNUM + 31,
  499. /* The 64-bit floating-point view of the coprocessor GPR's. */
  500. MEP_FIRST_FP_CR64_REGNUM,
  501. MEP_LAST_FP_CR64_REGNUM = MEP_FIRST_FP_CR64_REGNUM + 31,
  502. MEP_FIRST_CCR_REGNUM,
  503. MEP_LAST_CCR_REGNUM = MEP_FIRST_CCR_REGNUM + 63,
  504. MEP_LAST_PSEUDO_REGNUM = MEP_LAST_CCR_REGNUM,
  505. MEP_NUM_PSEUDO_REGS = (MEP_LAST_PSEUDO_REGNUM - MEP_LAST_RAW_REGNUM),
  506. MEP_NUM_REGS = MEP_NUM_RAW_REGS + MEP_NUM_PSEUDO_REGS
  507. };
  508. #define IN_SET(set, n) \
  509. (MEP_FIRST_ ## set ## _REGNUM <= (n) && (n) <= MEP_LAST_ ## set ## _REGNUM)
  510. #define IS_GPR_REGNUM(n) (IN_SET (GPR, (n)))
  511. #define IS_RAW_CSR_REGNUM(n) (IN_SET (RAW_CSR, (n)))
  512. #define IS_RAW_CR_REGNUM(n) (IN_SET (RAW_CR, (n)))
  513. #define IS_RAW_CCR_REGNUM(n) (IN_SET (RAW_CCR, (n)))
  514. #define IS_CSR_REGNUM(n) (IN_SET (CSR, (n)))
  515. #define IS_CR32_REGNUM(n) (IN_SET (CR32, (n)))
  516. #define IS_FP_CR32_REGNUM(n) (IN_SET (FP_CR32, (n)))
  517. #define IS_CR64_REGNUM(n) (IN_SET (CR64, (n)))
  518. #define IS_FP_CR64_REGNUM(n) (IN_SET (FP_CR64, (n)))
  519. #define IS_CR_REGNUM(n) (IS_CR32_REGNUM (n) || IS_FP_CR32_REGNUM (n) \
  520. || IS_CR64_REGNUM (n) || IS_FP_CR64_REGNUM (n))
  521. #define IS_CCR_REGNUM(n) (IN_SET (CCR, (n)))
  522. #define IS_RAW_REGNUM(n) (IN_SET (RAW, (n)))
  523. #define IS_PSEUDO_REGNUM(n) (IN_SET (PSEUDO, (n)))
  524. #define NUM_REGS_IN_SET(set) \
  525. (MEP_LAST_ ## set ## _REGNUM - MEP_FIRST_ ## set ## _REGNUM + 1)
  526. #define MEP_GPR_SIZE (4) /* Size of a MeP general-purpose register. */
  527. #define MEP_PSW_SIZE (4) /* Size of the PSW register. */
  528. #define MEP_LP_SIZE (4) /* Size of the LP register. */
  529. /* Many of the control/special registers contain bits that cannot be
  530. written to; some are entirely read-only. So we present them all as
  531. pseudoregisters.
  532. The following table describes the special properties of each CSR. */
  533. struct mep_csr_register
  534. {
  535. /* The number of this CSR's raw register. */
  536. int raw;
  537. /* The number of this CSR's pseudoregister. */
  538. int pseudo;
  539. /* A mask of the bits that are writeable: if a bit is set here, then
  540. it can be modified; if the bit is clear, then it cannot. */
  541. LONGEST writeable_bits;
  542. };
  543. /* mep_csr_registers[i] describes the i'th CSR.
  544. We just list the register numbers here explicitly to help catch
  545. typos. */
  546. #define CSR(name) MEP_RAW_ ## name ## _REGNUM, MEP_ ## name ## _REGNUM
  547. static mep_csr_register mep_csr_registers[] = {
  548. { CSR(PC), 0xffffffff }, /* manual says r/o, but we can write it */
  549. { CSR(LP), 0xffffffff },
  550. { CSR(SAR), 0x0000003f },
  551. { CSR(CSR3), 0xffffffff },
  552. { CSR(RPB), 0xfffffffe },
  553. { CSR(RPE), 0xffffffff },
  554. { CSR(RPC), 0xffffffff },
  555. { CSR(HI), 0xffffffff },
  556. { CSR(LO), 0xffffffff },
  557. { CSR(CSR9), 0xffffffff },
  558. { CSR(CSR10), 0xffffffff },
  559. { CSR(CSR11), 0xffffffff },
  560. { CSR(MB0), 0x0000ffff },
  561. { CSR(ME0), 0x0000ffff },
  562. { CSR(MB1), 0x0000ffff },
  563. { CSR(ME1), 0x0000ffff },
  564. { CSR(PSW), 0x000003ff },
  565. { CSR(ID), 0x00000000 },
  566. { CSR(TMP), 0xffffffff },
  567. { CSR(EPC), 0xffffffff },
  568. { CSR(EXC), 0x000030f0 },
  569. { CSR(CFG), 0x00c0001b },
  570. { CSR(CSR22), 0xffffffff },
  571. { CSR(NPC), 0xffffffff },
  572. { CSR(DBG), 0x00000580 },
  573. { CSR(DEPC), 0xffffffff },
  574. { CSR(OPT), 0x00000000 },
  575. { CSR(RCFG), 0x00000000 },
  576. { CSR(CCFG), 0x00000000 },
  577. { CSR(CSR29), 0xffffffff },
  578. { CSR(CSR30), 0xffffffff },
  579. { CSR(CSR31), 0xffffffff },
  580. };
  581. /* If R is the number of a raw register, then mep_raw_to_pseudo[R] is
  582. the number of the corresponding pseudoregister. Otherwise,
  583. mep_raw_to_pseudo[R] == R. */
  584. static int mep_raw_to_pseudo[MEP_NUM_REGS];
  585. /* If R is the number of a pseudoregister, then mep_pseudo_to_raw[R]
  586. is the number of the underlying raw register. Otherwise
  587. mep_pseudo_to_raw[R] == R. */
  588. static int mep_pseudo_to_raw[MEP_NUM_REGS];
  589. static void
  590. mep_init_pseudoregister_maps (void)
  591. {
  592. int i;
  593. /* Verify that mep_csr_registers covers all the CSRs, in order. */
  594. gdb_assert (ARRAY_SIZE (mep_csr_registers) == NUM_REGS_IN_SET (CSR));
  595. gdb_assert (ARRAY_SIZE (mep_csr_registers) == NUM_REGS_IN_SET (RAW_CSR));
  596. /* Verify that the raw and pseudo ranges have matching sizes. */
  597. gdb_assert (NUM_REGS_IN_SET (RAW_CSR) == NUM_REGS_IN_SET (CSR));
  598. gdb_assert (NUM_REGS_IN_SET (RAW_CR) == NUM_REGS_IN_SET (CR32));
  599. gdb_assert (NUM_REGS_IN_SET (RAW_CR) == NUM_REGS_IN_SET (CR64));
  600. gdb_assert (NUM_REGS_IN_SET (RAW_CCR) == NUM_REGS_IN_SET (CCR));
  601. for (i = 0; i < ARRAY_SIZE (mep_csr_registers); i++)
  602. {
  603. struct mep_csr_register *r = &mep_csr_registers[i];
  604. gdb_assert (r->pseudo == MEP_FIRST_CSR_REGNUM + i);
  605. gdb_assert (r->raw == MEP_FIRST_RAW_CSR_REGNUM + i);
  606. }
  607. /* Set up the initial raw<->pseudo mappings. */
  608. for (i = 0; i < MEP_NUM_REGS; i++)
  609. {
  610. mep_raw_to_pseudo[i] = i;
  611. mep_pseudo_to_raw[i] = i;
  612. }
  613. /* Add the CSR raw<->pseudo mappings. */
  614. for (i = 0; i < ARRAY_SIZE (mep_csr_registers); i++)
  615. {
  616. struct mep_csr_register *r = &mep_csr_registers[i];
  617. mep_raw_to_pseudo[r->raw] = r->pseudo;
  618. mep_pseudo_to_raw[r->pseudo] = r->raw;
  619. }
  620. /* Add the CR raw<->pseudo mappings. */
  621. for (i = 0; i < NUM_REGS_IN_SET (RAW_CR); i++)
  622. {
  623. int raw = MEP_FIRST_RAW_CR_REGNUM + i;
  624. int pseudo32 = MEP_FIRST_CR32_REGNUM + i;
  625. int pseudofp32 = MEP_FIRST_FP_CR32_REGNUM + i;
  626. int pseudo64 = MEP_FIRST_CR64_REGNUM + i;
  627. int pseudofp64 = MEP_FIRST_FP_CR64_REGNUM + i;
  628. /* Truly, the raw->pseudo mapping depends on the current module.
  629. But we use the raw->pseudo mapping when we read the debugging
  630. info; at that point, we don't know what module we'll actually
  631. be running yet. So, we always supply the 64-bit register
  632. numbers; GDB knows how to pick a smaller value out of a
  633. larger register properly. */
  634. mep_raw_to_pseudo[raw] = pseudo64;
  635. mep_pseudo_to_raw[pseudo32] = raw;
  636. mep_pseudo_to_raw[pseudofp32] = raw;
  637. mep_pseudo_to_raw[pseudo64] = raw;
  638. mep_pseudo_to_raw[pseudofp64] = raw;
  639. }
  640. /* Add the CCR raw<->pseudo mappings. */
  641. for (i = 0; i < NUM_REGS_IN_SET (CCR); i++)
  642. {
  643. int raw = MEP_FIRST_RAW_CCR_REGNUM + i;
  644. int pseudo = MEP_FIRST_CCR_REGNUM + i;
  645. mep_raw_to_pseudo[raw] = pseudo;
  646. mep_pseudo_to_raw[pseudo] = raw;
  647. }
  648. }
  649. static int
  650. mep_debug_reg_to_regnum (struct gdbarch *gdbarch, int debug_reg)
  651. {
  652. /* The debug info uses the raw register numbers. */
  653. if (debug_reg >= 0 && debug_reg < ARRAY_SIZE (mep_raw_to_pseudo))
  654. return mep_raw_to_pseudo[debug_reg];
  655. return -1;
  656. }
  657. /* Return the size, in bits, of the coprocessor pseudoregister
  658. numbered PSEUDO. */
  659. static int
  660. mep_pseudo_cr_size (int pseudo)
  661. {
  662. if (IS_CR32_REGNUM (pseudo)
  663. || IS_FP_CR32_REGNUM (pseudo))
  664. return 32;
  665. else if (IS_CR64_REGNUM (pseudo)
  666. || IS_FP_CR64_REGNUM (pseudo))
  667. return 64;
  668. else
  669. gdb_assert_not_reached ("unexpected coprocessor pseudo register");
  670. }
  671. /* If the coprocessor pseudoregister numbered PSEUDO is a
  672. floating-point register, return non-zero; if it is an integer
  673. register, return zero. */
  674. static int
  675. mep_pseudo_cr_is_float (int pseudo)
  676. {
  677. return (IS_FP_CR32_REGNUM (pseudo)
  678. || IS_FP_CR64_REGNUM (pseudo));
  679. }
  680. /* Given a coprocessor GPR pseudoregister number, return its index
  681. within that register bank. */
  682. static int
  683. mep_pseudo_cr_index (int pseudo)
  684. {
  685. if (IS_CR32_REGNUM (pseudo))
  686. return pseudo - MEP_FIRST_CR32_REGNUM;
  687. else if (IS_FP_CR32_REGNUM (pseudo))
  688. return pseudo - MEP_FIRST_FP_CR32_REGNUM;
  689. else if (IS_CR64_REGNUM (pseudo))
  690. return pseudo - MEP_FIRST_CR64_REGNUM;
  691. else if (IS_FP_CR64_REGNUM (pseudo))
  692. return pseudo - MEP_FIRST_FP_CR64_REGNUM;
  693. else
  694. gdb_assert_not_reached ("unexpected coprocessor pseudo register");
  695. }
  696. /* Return the me_module index describing the current target.
  697. If the current target has registers (e.g., simulator, remote
  698. target), then this uses the value of the 'module' register, raw
  699. register MEP_MODULE_REGNUM. Otherwise, this retrieves the value
  700. from the ELF header's e_flags field of the current executable
  701. file. */
  702. static CONFIG_ATTR
  703. current_me_module (void)
  704. {
  705. if (target_has_registers ())
  706. {
  707. ULONGEST regval;
  708. regcache_cooked_read_unsigned (get_current_regcache (),
  709. MEP_MODULE_REGNUM, &regval);
  710. return (CONFIG_ATTR) regval;
  711. }
  712. else
  713. {
  714. mep_gdbarch_tdep *tdep
  715. = (mep_gdbarch_tdep *) gdbarch_tdep (target_gdbarch ());
  716. return tdep->me_module;
  717. }
  718. }
  719. /* Return the set of options for the current target, in the form that
  720. the OPT register would use.
  721. If the current target has registers (e.g., simulator, remote
  722. target), then this is the actual value of the OPT register. If the
  723. current target does not have registers (e.g., an executable file),
  724. then use the 'module_opt' field we computed when we build the
  725. gdbarch object for this module. */
  726. static unsigned int
  727. current_options (void)
  728. {
  729. if (target_has_registers ())
  730. {
  731. ULONGEST regval;
  732. regcache_cooked_read_unsigned (get_current_regcache (),
  733. MEP_OPT_REGNUM, &regval);
  734. return regval;
  735. }
  736. else
  737. return me_module_opt (current_me_module ());
  738. }
  739. /* Return the width of the current me_module's coprocessor data bus,
  740. in bits. This is either 32 or 64. */
  741. static int
  742. current_cop_data_bus_width (void)
  743. {
  744. return me_module_cop_data_bus_width (current_me_module ());
  745. }
  746. /* Return the keyword table of coprocessor general-purpose register
  747. names appropriate for the me_module we're dealing with. */
  748. static CGEN_KEYWORD *
  749. current_cr_names (void)
  750. {
  751. const CGEN_HW_ENTRY *hw
  752. = me_module_register_set (current_me_module (), "h-cr-", HW_H_CR);
  753. return register_set_keyword_table (hw);
  754. }
  755. /* Return non-zero if the coprocessor general-purpose registers are
  756. floating-point values, zero otherwise. */
  757. static int
  758. current_cr_is_float (void)
  759. {
  760. const CGEN_HW_ENTRY *hw
  761. = me_module_register_set (current_me_module (), "h-cr-", HW_H_CR);
  762. return CGEN_ATTR_CGEN_HW_IS_FLOAT_VALUE (CGEN_HW_ATTRS (hw));
  763. }
  764. /* Return the keyword table of coprocessor control register names
  765. appropriate for the me_module we're dealing with. */
  766. static CGEN_KEYWORD *
  767. current_ccr_names (void)
  768. {
  769. const CGEN_HW_ENTRY *hw
  770. = me_module_register_set (current_me_module (), "h-ccr-", HW_H_CCR);
  771. return register_set_keyword_table (hw);
  772. }
  773. static const char *
  774. mep_register_name (struct gdbarch *gdbarch, int regnr)
  775. {
  776. /* General-purpose registers. */
  777. static const char *gpr_names[] = {
  778. "r0", "r1", "r2", "r3", /* 0 */
  779. "r4", "r5", "r6", "r7", /* 4 */
  780. "fp", "r9", "r10", "r11", /* 8 */
  781. "r12", "tp", "gp", "sp" /* 12 */
  782. };
  783. /* Special-purpose registers. */
  784. static const char *csr_names[] = {
  785. "pc", "lp", "sar", "", /* 0 csr3: reserved */
  786. "rpb", "rpe", "rpc", "hi", /* 4 */
  787. "lo", "", "", "", /* 8 csr9-csr11: reserved */
  788. "mb0", "me0", "mb1", "me1", /* 12 */
  789. "psw", "id", "tmp", "epc", /* 16 */
  790. "exc", "cfg", "", "npc", /* 20 csr22: reserved */
  791. "dbg", "depc", "opt", "rcfg", /* 24 */
  792. "ccfg", "", "", "" /* 28 csr29-csr31: reserved */
  793. };
  794. if (IS_GPR_REGNUM (regnr))
  795. return gpr_names[regnr - MEP_R0_REGNUM];
  796. else if (IS_CSR_REGNUM (regnr))
  797. {
  798. /* The 'hi' and 'lo' registers are only present on processors
  799. that have the 'MUL' or 'DIV' instructions enabled. */
  800. if ((regnr == MEP_HI_REGNUM || regnr == MEP_LO_REGNUM)
  801. && (! (current_options () & (MEP_OPT_MUL | MEP_OPT_DIV))))
  802. return "";
  803. return csr_names[regnr - MEP_FIRST_CSR_REGNUM];
  804. }
  805. else if (IS_CR_REGNUM (regnr))
  806. {
  807. CGEN_KEYWORD *names;
  808. int cr_size;
  809. int cr_is_float;
  810. /* Does this module have a coprocessor at all? */
  811. if (! (current_options () & MEP_OPT_COP))
  812. return "";
  813. names = current_cr_names ();
  814. if (! names)
  815. /* This module's coprocessor has no general-purpose registers. */
  816. return "";
  817. cr_size = current_cop_data_bus_width ();
  818. if (cr_size != mep_pseudo_cr_size (regnr))
  819. /* This module's coprocessor's GPR's are of a different size. */
  820. return "";
  821. cr_is_float = current_cr_is_float ();
  822. /* The extra ! operators ensure we get boolean equality, not
  823. numeric equality. */
  824. if (! cr_is_float != ! mep_pseudo_cr_is_float (regnr))
  825. /* This module's coprocessor's GPR's are of a different type. */
  826. return "";
  827. return register_name_from_keyword (names, mep_pseudo_cr_index (regnr));
  828. }
  829. else if (IS_CCR_REGNUM (regnr))
  830. {
  831. /* Does this module have a coprocessor at all? */
  832. if (! (current_options () & MEP_OPT_COP))
  833. return "";
  834. {
  835. CGEN_KEYWORD *names = current_ccr_names ();
  836. if (! names)
  837. /* This me_module's coprocessor has no control registers. */
  838. return "";
  839. return register_name_from_keyword (names, regnr-MEP_FIRST_CCR_REGNUM);
  840. }
  841. }
  842. /* It might be nice to give the 'module' register a name, but that
  843. would affect the output of 'info all-registers', which would
  844. disturb the test suites. So we leave it invisible. */
  845. else
  846. return NULL;
  847. }
  848. /* Custom register groups for the MeP. */
  849. static const reggroup *mep_csr_reggroup; /* control/special */
  850. static const reggroup *mep_cr_reggroup; /* coprocessor general-purpose */
  851. static const reggroup *mep_ccr_reggroup; /* coprocessor control */
  852. static int
  853. mep_register_reggroup_p (struct gdbarch *gdbarch, int regnum,
  854. const struct reggroup *group)
  855. {
  856. /* Filter reserved or unused register numbers. */
  857. {
  858. const char *name = mep_register_name (gdbarch, regnum);
  859. if (! name || name[0] == '\0')
  860. return 0;
  861. }
  862. /* We could separate the GPRs and the CSRs. Toshiba has approved of
  863. the existing behavior, so we'd want to run that by them. */
  864. if (group == general_reggroup)
  865. return (IS_GPR_REGNUM (regnum)
  866. || IS_CSR_REGNUM (regnum));
  867. /* Everything is in the 'all' reggroup, except for the raw CSR's. */
  868. else if (group == all_reggroup)
  869. return (IS_GPR_REGNUM (regnum)
  870. || IS_CSR_REGNUM (regnum)
  871. || IS_CR_REGNUM (regnum)
  872. || IS_CCR_REGNUM (regnum));
  873. /* All registers should be saved and restored, except for the raw
  874. CSR's.
  875. This is probably right if the coprocessor is something like a
  876. floating-point unit, but would be wrong if the coprocessor is
  877. something that does I/O, where register accesses actually cause
  878. externally-visible actions. But I get the impression that the
  879. coprocessor isn't supposed to do things like that --- you'd use a
  880. hardware engine, perhaps. */
  881. else if (group == save_reggroup || group == restore_reggroup)
  882. return (IS_GPR_REGNUM (regnum)
  883. || IS_CSR_REGNUM (regnum)
  884. || IS_CR_REGNUM (regnum)
  885. || IS_CCR_REGNUM (regnum));
  886. else if (group == mep_csr_reggroup)
  887. return IS_CSR_REGNUM (regnum);
  888. else if (group == mep_cr_reggroup)
  889. return IS_CR_REGNUM (regnum);
  890. else if (group == mep_ccr_reggroup)
  891. return IS_CCR_REGNUM (regnum);
  892. else
  893. return 0;
  894. }
  895. static struct type *
  896. mep_register_type (struct gdbarch *gdbarch, int reg_nr)
  897. {
  898. /* Coprocessor general-purpose registers may be either 32 or 64 bits
  899. long. So for them, the raw registers are always 64 bits long (to
  900. keep the 'g' packet format fixed), and the pseudoregisters vary
  901. in length. */
  902. if (IS_RAW_CR_REGNUM (reg_nr))
  903. return builtin_type (gdbarch)->builtin_uint64;
  904. /* Since GDB doesn't allow registers to change type, we have two
  905. banks of pseudoregisters for the coprocessor general-purpose
  906. registers: one that gives a 32-bit view, and one that gives a
  907. 64-bit view. We hide or show one or the other depending on the
  908. current module. */
  909. if (IS_CR_REGNUM (reg_nr))
  910. {
  911. int size = mep_pseudo_cr_size (reg_nr);
  912. if (size == 32)
  913. {
  914. if (mep_pseudo_cr_is_float (reg_nr))
  915. return builtin_type (gdbarch)->builtin_float;
  916. else
  917. return builtin_type (gdbarch)->builtin_uint32;
  918. }
  919. else if (size == 64)
  920. {
  921. if (mep_pseudo_cr_is_float (reg_nr))
  922. return builtin_type (gdbarch)->builtin_double;
  923. else
  924. return builtin_type (gdbarch)->builtin_uint64;
  925. }
  926. else
  927. gdb_assert_not_reached ("unexpected cr size");
  928. }
  929. /* All other registers are 32 bits long. */
  930. else
  931. return builtin_type (gdbarch)->builtin_uint32;
  932. }
  933. static enum register_status
  934. mep_pseudo_cr32_read (struct gdbarch *gdbarch,
  935. readable_regcache *regcache,
  936. int cookednum,
  937. gdb_byte *buf)
  938. {
  939. enum register_status status;
  940. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  941. /* Read the raw register into a 64-bit buffer, and then return the
  942. appropriate end of that buffer. */
  943. int rawnum = mep_pseudo_to_raw[cookednum];
  944. gdb_byte buf64[8];
  945. gdb_assert (TYPE_LENGTH (register_type (gdbarch, rawnum)) == sizeof (buf64));
  946. gdb_assert (TYPE_LENGTH (register_type (gdbarch, cookednum)) == 4);
  947. status = regcache->raw_read (rawnum, buf64);
  948. if (status == REG_VALID)
  949. {
  950. /* Slow, but legible. */
  951. store_unsigned_integer (buf, 4, byte_order,
  952. extract_unsigned_integer (buf64, 8, byte_order));
  953. }
  954. return status;
  955. }
  956. static enum register_status
  957. mep_pseudo_cr64_read (struct gdbarch *gdbarch,
  958. readable_regcache *regcache,
  959. int cookednum,
  960. gdb_byte *buf)
  961. {
  962. return regcache->raw_read (mep_pseudo_to_raw[cookednum], buf);
  963. }
  964. static enum register_status
  965. mep_pseudo_register_read (struct gdbarch *gdbarch,
  966. readable_regcache *regcache,
  967. int cookednum,
  968. gdb_byte *buf)
  969. {
  970. if (IS_CSR_REGNUM (cookednum)
  971. || IS_CCR_REGNUM (cookednum))
  972. return regcache->raw_read (mep_pseudo_to_raw[cookednum], buf);
  973. else if (IS_CR32_REGNUM (cookednum)
  974. || IS_FP_CR32_REGNUM (cookednum))
  975. return mep_pseudo_cr32_read (gdbarch, regcache, cookednum, buf);
  976. else if (IS_CR64_REGNUM (cookednum)
  977. || IS_FP_CR64_REGNUM (cookednum))
  978. return mep_pseudo_cr64_read (gdbarch, regcache, cookednum, buf);
  979. else
  980. gdb_assert_not_reached ("unexpected pseudo register");
  981. }
  982. static void
  983. mep_pseudo_csr_write (struct gdbarch *gdbarch,
  984. struct regcache *regcache,
  985. int cookednum,
  986. const gdb_byte *buf)
  987. {
  988. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  989. int size = register_size (gdbarch, cookednum);
  990. struct mep_csr_register *r
  991. = &mep_csr_registers[cookednum - MEP_FIRST_CSR_REGNUM];
  992. if (r->writeable_bits == 0)
  993. /* A completely read-only register; avoid the read-modify-
  994. write cycle, and juts ignore the entire write. */
  995. ;
  996. else
  997. {
  998. /* A partially writeable register; do a read-modify-write cycle. */
  999. ULONGEST old_bits;
  1000. ULONGEST new_bits;
  1001. ULONGEST mixed_bits;
  1002. regcache_raw_read_unsigned (regcache, r->raw, &old_bits);
  1003. new_bits = extract_unsigned_integer (buf, size, byte_order);
  1004. mixed_bits = ((r->writeable_bits & new_bits)
  1005. | (~r->writeable_bits & old_bits));
  1006. regcache_raw_write_unsigned (regcache, r->raw, mixed_bits);
  1007. }
  1008. }
  1009. static void
  1010. mep_pseudo_cr32_write (struct gdbarch *gdbarch,
  1011. struct regcache *regcache,
  1012. int cookednum,
  1013. const gdb_byte *buf)
  1014. {
  1015. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1016. /* Expand the 32-bit value into a 64-bit value, and write that to
  1017. the pseudoregister. */
  1018. int rawnum = mep_pseudo_to_raw[cookednum];
  1019. gdb_byte buf64[8];
  1020. gdb_assert (TYPE_LENGTH (register_type (gdbarch, rawnum)) == sizeof (buf64));
  1021. gdb_assert (TYPE_LENGTH (register_type (gdbarch, cookednum)) == 4);
  1022. /* Slow, but legible. */
  1023. store_unsigned_integer (buf64, 8, byte_order,
  1024. extract_unsigned_integer (buf, 4, byte_order));
  1025. regcache->raw_write (rawnum, buf64);
  1026. }
  1027. static void
  1028. mep_pseudo_cr64_write (struct gdbarch *gdbarch,
  1029. struct regcache *regcache,
  1030. int cookednum,
  1031. const gdb_byte *buf)
  1032. {
  1033. regcache->raw_write (mep_pseudo_to_raw[cookednum], buf);
  1034. }
  1035. static void
  1036. mep_pseudo_register_write (struct gdbarch *gdbarch,
  1037. struct regcache *regcache,
  1038. int cookednum,
  1039. const gdb_byte *buf)
  1040. {
  1041. if (IS_CSR_REGNUM (cookednum))
  1042. mep_pseudo_csr_write (gdbarch, regcache, cookednum, buf);
  1043. else if (IS_CR32_REGNUM (cookednum)
  1044. || IS_FP_CR32_REGNUM (cookednum))
  1045. mep_pseudo_cr32_write (gdbarch, regcache, cookednum, buf);
  1046. else if (IS_CR64_REGNUM (cookednum)
  1047. || IS_FP_CR64_REGNUM (cookednum))
  1048. mep_pseudo_cr64_write (gdbarch, regcache, cookednum, buf);
  1049. else if (IS_CCR_REGNUM (cookednum))
  1050. regcache->raw_write (mep_pseudo_to_raw[cookednum], buf);
  1051. else
  1052. gdb_assert_not_reached ("unexpected pseudo register");
  1053. }
  1054. /* Disassembly. */
  1055. static int
  1056. mep_gdb_print_insn (bfd_vma pc, disassemble_info * info)
  1057. {
  1058. struct obj_section * s = find_pc_section (pc);
  1059. info->arch = bfd_arch_mep;
  1060. if (s)
  1061. {
  1062. /* The libopcodes disassembly code uses the section to find the
  1063. BFD, the BFD to find the ELF header, the ELF header to find
  1064. the me_module index, and the me_module index to select the
  1065. right instructions to print. */
  1066. info->section = s->the_bfd_section;
  1067. }
  1068. return print_insn_mep (pc, info);
  1069. }
  1070. /* Prologue analysis. */
  1071. /* The MeP has two classes of instructions: "core" instructions, which
  1072. are pretty normal RISC chip stuff, and "coprocessor" instructions,
  1073. which are mostly concerned with moving data in and out of
  1074. coprocessor registers, and branching on coprocessor condition
  1075. codes. There's space in the instruction set for custom coprocessor
  1076. instructions, too.
  1077. Instructions can be 16 or 32 bits long; the top two bits of the
  1078. first byte indicate the length. The coprocessor instructions are
  1079. mixed in with the core instructions, and there's no easy way to
  1080. distinguish them; you have to completely decode them to tell one
  1081. from the other.
  1082. The MeP also supports a "VLIW" operation mode, where instructions
  1083. always occur in fixed-width bundles. The bundles are either 32
  1084. bits or 64 bits long, depending on a fixed configuration flag. You
  1085. decode the first part of the bundle as normal; if it's a core
  1086. instruction, and there's any space left in the bundle, the
  1087. remainder of the bundle is a coprocessor instruction, which will
  1088. execute in parallel with the core instruction. If the first part
  1089. of the bundle is a coprocessor instruction, it occupies the entire
  1090. bundle.
  1091. So, here are all the cases:
  1092. - 32-bit VLIW mode:
  1093. Every bundle is four bytes long, and naturally aligned, and can hold
  1094. one or two instructions:
  1095. - 16-bit core instruction; 16-bit coprocessor instruction
  1096. These execute in parallel.
  1097. - 32-bit core instruction
  1098. - 32-bit coprocessor instruction
  1099. - 64-bit VLIW mode:
  1100. Every bundle is eight bytes long, and naturally aligned, and can hold
  1101. one or two instructions:
  1102. - 16-bit core instruction; 48-bit (!) coprocessor instruction
  1103. These execute in parallel.
  1104. - 32-bit core instruction; 32-bit coprocessor instruction
  1105. These execute in parallel.
  1106. - 64-bit coprocessor instruction
  1107. Now, the MeP manual doesn't define any 48- or 64-bit coprocessor
  1108. instruction, so I don't really know what's up there; perhaps these
  1109. are always the user-defined coprocessor instructions. */
  1110. /* Return non-zero if PC is in a VLIW code section, zero
  1111. otherwise. */
  1112. static int
  1113. mep_pc_in_vliw_section (CORE_ADDR pc)
  1114. {
  1115. struct obj_section *s = find_pc_section (pc);
  1116. if (s)
  1117. return (s->the_bfd_section->flags & SEC_MEP_VLIW);
  1118. return 0;
  1119. }
  1120. /* Set *INSN to the next core instruction at PC, and return the
  1121. address of the next instruction.
  1122. The MeP instruction encoding is endian-dependent. 16- and 32-bit
  1123. instructions are encoded as one or two two-byte parts, and each
  1124. part is byte-swapped independently. Thus:
  1125. void
  1126. foo (void)
  1127. {
  1128. asm ("movu $1, 0x123456");
  1129. asm ("sb $1,0x5678($2)");
  1130. asm ("clip $1, 19");
  1131. }
  1132. compiles to this big-endian code:
  1133. 0: d1 56 12 34 movu $1,0x123456
  1134. 4: c1 28 56 78 sb $1,22136($2)
  1135. 8: f1 01 10 98 clip $1,0x13
  1136. c: 70 02 ret
  1137. and this little-endian code:
  1138. 0: 56 d1 34 12 movu $1,0x123456
  1139. 4: 28 c1 78 56 sb $1,22136($2)
  1140. 8: 01 f1 98 10 clip $1,0x13
  1141. c: 02 70 ret
  1142. Instructions are returned in *INSN in an endian-independent form: a
  1143. given instruction always appears in *INSN the same way, regardless
  1144. of whether the instruction stream is big-endian or little-endian.
  1145. *INSN's most significant 16 bits are the first (i.e., at lower
  1146. addresses) 16 bit part of the instruction. Its least significant
  1147. 16 bits are the second (i.e., higher-addressed) 16 bit part of the
  1148. instruction, or zero for a 16-bit instruction. Both 16-bit parts
  1149. are fetched using the current endianness.
  1150. So, the *INSN values for the instruction sequence above would be
  1151. the following, in either endianness:
  1152. 0xd1561234 movu $1,0x123456
  1153. 0xc1285678 sb $1,22136($2)
  1154. 0xf1011098 clip $1,0x13
  1155. 0x70020000 ret
  1156. (In a sense, it would be more natural to return 16-bit instructions
  1157. in the least significant 16 bits of *INSN, but that would be
  1158. ambiguous. In order to tell whether you're looking at a 16- or a
  1159. 32-bit instruction, you have to consult the major opcode field ---
  1160. the most significant four bits of the instruction's first 16-bit
  1161. part. But if we put 16-bit instructions at the least significant
  1162. end of *INSN, then you don't know where to find the major opcode
  1163. field until you know if it's a 16- or a 32-bit instruction ---
  1164. which is where we started.)
  1165. If PC points to a core / coprocessor bundle in a VLIW section, set
  1166. *INSN to the core instruction, and return the address of the next
  1167. bundle. This has the effect of skipping the bundled coprocessor
  1168. instruction. That's okay, since coprocessor instructions aren't
  1169. significant to prologue analysis --- for the time being,
  1170. anyway. */
  1171. static CORE_ADDR
  1172. mep_get_insn (struct gdbarch *gdbarch, CORE_ADDR pc, unsigned long *insn)
  1173. {
  1174. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1175. int pc_in_vliw_section;
  1176. int vliw_mode;
  1177. int insn_len;
  1178. gdb_byte buf[2];
  1179. *insn = 0;
  1180. /* Are we in a VLIW section? */
  1181. pc_in_vliw_section = mep_pc_in_vliw_section (pc);
  1182. if (pc_in_vliw_section)
  1183. {
  1184. /* Yes, find out which bundle size. */
  1185. vliw_mode = current_options () & (MEP_OPT_VL32 | MEP_OPT_VL64);
  1186. /* If PC is in a VLIW section, but the current core doesn't say
  1187. that it supports either VLIW mode, then we don't have enough
  1188. information to parse the instruction stream it contains.
  1189. Since the "undifferentiated" standard core doesn't have
  1190. either VLIW mode bit set, this could happen.
  1191. But it shouldn't be an error to (say) set a breakpoint in a
  1192. VLIW section, if you know you'll never reach it. (Perhaps
  1193. you have a script that sets a bunch of standard breakpoints.)
  1194. So we'll just return zero here, and hope for the best. */
  1195. if (! (vliw_mode & (MEP_OPT_VL32 | MEP_OPT_VL64)))
  1196. return 0;
  1197. /* If both VL32 and VL64 are set, that's bogus, too. */
  1198. if (vliw_mode == (MEP_OPT_VL32 | MEP_OPT_VL64))
  1199. return 0;
  1200. }
  1201. else
  1202. vliw_mode = 0;
  1203. read_memory (pc, buf, sizeof (buf));
  1204. *insn = extract_unsigned_integer (buf, 2, byte_order) << 16;
  1205. /* The major opcode --- the top four bits of the first 16-bit
  1206. part --- indicates whether this instruction is 16 or 32 bits
  1207. long. All 32-bit instructions have a major opcode whose top
  1208. two bits are 11; all the rest are 16-bit instructions. */
  1209. if ((*insn & 0xc0000000) == 0xc0000000)
  1210. {
  1211. /* Fetch the second 16-bit part of the instruction. */
  1212. read_memory (pc + 2, buf, sizeof (buf));
  1213. *insn = *insn | extract_unsigned_integer (buf, 2, byte_order);
  1214. }
  1215. /* If we're in VLIW code, then the VLIW width determines the address
  1216. of the next instruction. */
  1217. if (vliw_mode)
  1218. {
  1219. /* In 32-bit VLIW code, all bundles are 32 bits long. We ignore the
  1220. coprocessor half of a core / copro bundle. */
  1221. if (vliw_mode == MEP_OPT_VL32)
  1222. insn_len = 4;
  1223. /* In 64-bit VLIW code, all bundles are 64 bits long. We ignore the
  1224. coprocessor half of a core / copro bundle. */
  1225. else if (vliw_mode == MEP_OPT_VL64)
  1226. insn_len = 8;
  1227. /* We'd better be in either core, 32-bit VLIW, or 64-bit VLIW mode. */
  1228. else
  1229. gdb_assert_not_reached ("unexpected vliw mode");
  1230. }
  1231. /* Otherwise, the top two bits of the major opcode are (again) what
  1232. we need to check. */
  1233. else if ((*insn & 0xc0000000) == 0xc0000000)
  1234. insn_len = 4;
  1235. else
  1236. insn_len = 2;
  1237. return pc + insn_len;
  1238. }
  1239. /* Sign-extend the LEN-bit value N. */
  1240. #define SEXT(n, len) ((((int) (n)) ^ (1 << ((len) - 1))) - (1 << ((len) - 1)))
  1241. /* Return the LEN-bit field at POS from I. */
  1242. #define FIELD(i, pos, len) (((i) >> (pos)) & ((1 << (len)) - 1))
  1243. /* Like FIELD, but sign-extend the field's value. */
  1244. #define SFIELD(i, pos, len) (SEXT (FIELD ((i), (pos), (len)), (len)))
  1245. /* Macros for decoding instructions.
  1246. Remember that 16-bit instructions are placed in bits 16..31 of i,
  1247. not at the least significant end; this means that the major opcode
  1248. field is always in the same place, regardless of the width of the
  1249. instruction. As a reminder of this, we show the lower 16 bits of a
  1250. 16-bit instruction as xxxx_xxxx_xxxx_xxxx. */
  1251. /* SB Rn,(Rm) 0000_nnnn_mmmm_1000 */
  1252. /* SH Rn,(Rm) 0000_nnnn_mmmm_1001 */
  1253. /* SW Rn,(Rm) 0000_nnnn_mmmm_1010 */
  1254. /* SW Rn,disp16(Rm) 1100_nnnn_mmmm_1010 dddd_dddd_dddd_dddd */
  1255. #define IS_SW(i) (((i) & 0xf00f0000) == 0xc00a0000)
  1256. /* SB Rn,disp16(Rm) 1100_nnnn_mmmm_1000 dddd_dddd_dddd_dddd */
  1257. #define IS_SB(i) (((i) & 0xf00f0000) == 0xc0080000)
  1258. /* SH Rn,disp16(Rm) 1100_nnnn_mmmm_1001 dddd_dddd_dddd_dddd */
  1259. #define IS_SH(i) (((i) & 0xf00f0000) == 0xc0090000)
  1260. #define SWBH_32_BASE(i) (FIELD (i, 20, 4))
  1261. #define SWBH_32_SOURCE(i) (FIELD (i, 24, 4))
  1262. #define SWBH_32_OFFSET(i) (SFIELD (i, 0, 16))
  1263. /* SW Rn,disp7.align4(SP) 0100_nnnn_0ddd_dd10 xxxx_xxxx_xxxx_xxxx */
  1264. #define IS_SW_IMMD(i) (((i) & 0xf0830000) == 0x40020000)
  1265. #define SW_IMMD_SOURCE(i) (FIELD (i, 24, 4))
  1266. #define SW_IMMD_OFFSET(i) (FIELD (i, 18, 5) << 2)
  1267. /* SW Rn,(Rm) 0000_nnnn_mmmm_1010 xxxx_xxxx_xxxx_xxxx */
  1268. #define IS_SW_REG(i) (((i) & 0xf00f0000) == 0x000a0000)
  1269. #define SW_REG_SOURCE(i) (FIELD (i, 24, 4))
  1270. #define SW_REG_BASE(i) (FIELD (i, 20, 4))
  1271. /* ADD3 Rl,Rn,Rm 1001_nnnn_mmmm_llll xxxx_xxxx_xxxx_xxxx */
  1272. #define IS_ADD3_16_REG(i) (((i) & 0xf0000000) == 0x90000000)
  1273. #define ADD3_16_REG_SRC1(i) (FIELD (i, 20, 4)) /* n */
  1274. #define ADD3_16_REG_SRC2(i) (FIELD (i, 24, 4)) /* m */
  1275. /* ADD3 Rn,Rm,imm16 1100_nnnn_mmmm_0000 iiii_iiii_iiii_iiii */
  1276. #define IS_ADD3_32(i) (((i) & 0xf00f0000) == 0xc0000000)
  1277. #define ADD3_32_TARGET(i) (FIELD (i, 24, 4))
  1278. #define ADD3_32_SOURCE(i) (FIELD (i, 20, 4))
  1279. #define ADD3_32_OFFSET(i) (SFIELD (i, 0, 16))
  1280. /* ADD3 Rn,SP,imm7.align4 0100_nnnn_0iii_ii00 xxxx_xxxx_xxxx_xxxx */
  1281. #define IS_ADD3_16(i) (((i) & 0xf0830000) == 0x40000000)
  1282. #define ADD3_16_TARGET(i) (FIELD (i, 24, 4))
  1283. #define ADD3_16_OFFSET(i) (FIELD (i, 18, 5) << 2)
  1284. /* ADD Rn,imm6 0110_nnnn_iiii_ii00 xxxx_xxxx_xxxx_xxxx */
  1285. #define IS_ADD(i) (((i) & 0xf0030000) == 0x60000000)
  1286. #define ADD_TARGET(i) (FIELD (i, 24, 4))
  1287. #define ADD_OFFSET(i) (SFIELD (i, 18, 6))
  1288. /* LDC Rn,imm5 0111_nnnn_iiii_101I xxxx_xxxx_xxxx_xxxx
  1289. imm5 = I||i[7:4] */
  1290. #define IS_LDC(i) (((i) & 0xf00e0000) == 0x700a0000)
  1291. #define LDC_IMM(i) ((FIELD (i, 16, 1) << 4) | FIELD (i, 20, 4))
  1292. #define LDC_TARGET(i) (FIELD (i, 24, 4))
  1293. /* LW Rn,disp16(Rm) 1100_nnnn_mmmm_1110 dddd_dddd_dddd_dddd */
  1294. #define IS_LW(i) (((i) & 0xf00f0000) == 0xc00e0000)
  1295. #define LW_TARGET(i) (FIELD (i, 24, 4))
  1296. #define LW_BASE(i) (FIELD (i, 20, 4))
  1297. #define LW_OFFSET(i) (SFIELD (i, 0, 16))
  1298. /* MOV Rn,Rm 0000_nnnn_mmmm_0000 xxxx_xxxx_xxxx_xxxx */
  1299. #define IS_MOV(i) (((i) & 0xf00f0000) == 0x00000000)
  1300. #define MOV_TARGET(i) (FIELD (i, 24, 4))
  1301. #define MOV_SOURCE(i) (FIELD (i, 20, 4))
  1302. /* BRA disp12.align2 1011_dddd_dddd_ddd0 xxxx_xxxx_xxxx_xxxx */
  1303. #define IS_BRA(i) (((i) & 0xf0010000) == 0xb0000000)
  1304. #define BRA_DISP(i) (SFIELD (i, 17, 11) << 1)
  1305. /* This structure holds the results of a prologue analysis. */
  1306. struct mep_prologue
  1307. {
  1308. /* The architecture for which we generated this prologue info. */
  1309. struct gdbarch *gdbarch;
  1310. /* The offset from the frame base to the stack pointer --- always
  1311. zero or negative.
  1312. Calling this a "size" is a bit misleading, but given that the
  1313. stack grows downwards, using offsets for everything keeps one
  1314. from going completely sign-crazy: you never change anything's
  1315. sign for an ADD instruction; always change the second operand's
  1316. sign for a SUB instruction; and everything takes care of
  1317. itself. */
  1318. int frame_size;
  1319. /* Non-zero if this function has initialized the frame pointer from
  1320. the stack pointer, zero otherwise. */
  1321. int has_frame_ptr;
  1322. /* If has_frame_ptr is non-zero, this is the offset from the frame
  1323. base to where the frame pointer points. This is always zero or
  1324. negative. */
  1325. int frame_ptr_offset;
  1326. /* The address of the first instruction at which the frame has been
  1327. set up and the arguments are where the debug info says they are
  1328. --- as best as we can tell. */
  1329. CORE_ADDR prologue_end;
  1330. /* reg_offset[R] is the offset from the CFA at which register R is
  1331. saved, or 1 if register R has not been saved. (Real values are
  1332. always zero or negative.) */
  1333. int reg_offset[MEP_NUM_REGS];
  1334. };
  1335. /* Return non-zero if VALUE is an incoming argument register. */
  1336. static int
  1337. is_arg_reg (pv_t value)
  1338. {
  1339. return (value.kind == pvk_register
  1340. && MEP_R1_REGNUM <= value.reg && value.reg <= MEP_R4_REGNUM
  1341. && value.k == 0);
  1342. }
  1343. /* Return non-zero if a store of REG's current value VALUE to ADDR is
  1344. probably spilling an argument register to its stack slot in STACK.
  1345. Such instructions should be included in the prologue, if possible.
  1346. The store is a spill if:
  1347. - the value being stored is REG's original value;
  1348. - the value has not already been stored somewhere in STACK; and
  1349. - ADDR is a stack slot's address (e.g., relative to the original
  1350. value of the SP). */
  1351. static int
  1352. is_arg_spill (struct gdbarch *gdbarch, pv_t value, pv_t addr,
  1353. struct pv_area *stack)
  1354. {
  1355. return (is_arg_reg (value)
  1356. && pv_is_register (addr, MEP_SP_REGNUM)
  1357. && ! stack->find_reg (gdbarch, value.reg, 0));
  1358. }
  1359. /* Function for finding saved registers in a 'struct pv_area'; we pass
  1360. this to pv_area::scan.
  1361. If VALUE is a saved register, ADDR says it was saved at a constant
  1362. offset from the frame base, and SIZE indicates that the whole
  1363. register was saved, record its offset in RESULT_UNTYPED. */
  1364. static void
  1365. check_for_saved (void *result_untyped, pv_t addr, CORE_ADDR size, pv_t value)
  1366. {
  1367. struct mep_prologue *result = (struct mep_prologue *) result_untyped;
  1368. if (value.kind == pvk_register
  1369. && value.k == 0
  1370. && pv_is_register (addr, MEP_SP_REGNUM)
  1371. && size == register_size (result->gdbarch, value.reg))
  1372. result->reg_offset[value.reg] = addr.k;
  1373. }
  1374. /* Analyze a prologue starting at START_PC, going no further than
  1375. LIMIT_PC. Fill in RESULT as appropriate. */
  1376. static void
  1377. mep_analyze_prologue (struct gdbarch *gdbarch,
  1378. CORE_ADDR start_pc, CORE_ADDR limit_pc,
  1379. struct mep_prologue *result)
  1380. {
  1381. CORE_ADDR pc;
  1382. unsigned long insn;
  1383. pv_t reg[MEP_NUM_REGS];
  1384. CORE_ADDR after_last_frame_setup_insn = start_pc;
  1385. memset (result, 0, sizeof (*result));
  1386. result->gdbarch = gdbarch;
  1387. for (int rn = 0; rn < MEP_NUM_REGS; rn++)
  1388. {
  1389. reg[rn] = pv_register (rn, 0);
  1390. result->reg_offset[rn] = 1;
  1391. }
  1392. pv_area stack (MEP_SP_REGNUM, gdbarch_addr_bit (gdbarch));
  1393. pc = start_pc;
  1394. while (pc < limit_pc)
  1395. {
  1396. CORE_ADDR next_pc;
  1397. pv_t pre_insn_fp, pre_insn_sp;
  1398. next_pc = mep_get_insn (gdbarch, pc, &insn);
  1399. /* A zero return from mep_get_insn means that either we weren't
  1400. able to read the instruction from memory, or that we don't
  1401. have enough information to be able to reliably decode it. So
  1402. we'll store here and hope for the best. */
  1403. if (! next_pc)
  1404. break;
  1405. /* Note the current values of the SP and FP, so we can tell if
  1406. this instruction changed them, below. */
  1407. pre_insn_fp = reg[MEP_FP_REGNUM];
  1408. pre_insn_sp = reg[MEP_SP_REGNUM];
  1409. if (IS_ADD (insn))
  1410. {
  1411. int rn = ADD_TARGET (insn);
  1412. CORE_ADDR imm6 = ADD_OFFSET (insn);
  1413. reg[rn] = pv_add_constant (reg[rn], imm6);
  1414. }
  1415. else if (IS_ADD3_16 (insn))
  1416. {
  1417. int rn = ADD3_16_TARGET (insn);
  1418. int imm7 = ADD3_16_OFFSET (insn);
  1419. reg[rn] = pv_add_constant (reg[MEP_SP_REGNUM], imm7);
  1420. }
  1421. else if (IS_ADD3_32 (insn))
  1422. {
  1423. int rn = ADD3_32_TARGET (insn);
  1424. int rm = ADD3_32_SOURCE (insn);
  1425. int imm16 = ADD3_32_OFFSET (insn);
  1426. reg[rn] = pv_add_constant (reg[rm], imm16);
  1427. }
  1428. else if (IS_SW_REG (insn))
  1429. {
  1430. int rn = SW_REG_SOURCE (insn);
  1431. int rm = SW_REG_BASE (insn);
  1432. /* If simulating this store would require us to forget
  1433. everything we know about the stack frame in the name of
  1434. accuracy, it would be better to just quit now. */
  1435. if (stack.store_would_trash (reg[rm]))
  1436. break;
  1437. if (is_arg_spill (gdbarch, reg[rn], reg[rm], &stack))
  1438. after_last_frame_setup_insn = next_pc;
  1439. stack.store (reg[rm], 4, reg[rn]);
  1440. }
  1441. else if (IS_SW_IMMD (insn))
  1442. {
  1443. int rn = SW_IMMD_SOURCE (insn);
  1444. int offset = SW_IMMD_OFFSET (insn);
  1445. pv_t addr = pv_add_constant (reg[MEP_SP_REGNUM], offset);
  1446. /* If simulating this store would require us to forget
  1447. everything we know about the stack frame in the name of
  1448. accuracy, it would be better to just quit now. */
  1449. if (stack.store_would_trash (addr))
  1450. break;
  1451. if (is_arg_spill (gdbarch, reg[rn], addr, &stack))
  1452. after_last_frame_setup_insn = next_pc;
  1453. stack.store (addr, 4, reg[rn]);
  1454. }
  1455. else if (IS_MOV (insn))
  1456. {
  1457. int rn = MOV_TARGET (insn);
  1458. int rm = MOV_SOURCE (insn);
  1459. reg[rn] = reg[rm];
  1460. if (pv_is_register (reg[rm], rm) && is_arg_reg (reg[rm]))
  1461. after_last_frame_setup_insn = next_pc;
  1462. }
  1463. else if (IS_SB (insn) || IS_SH (insn) || IS_SW (insn))
  1464. {
  1465. int rn = SWBH_32_SOURCE (insn);
  1466. int rm = SWBH_32_BASE (insn);
  1467. int disp = SWBH_32_OFFSET (insn);
  1468. int size = (IS_SB (insn) ? 1
  1469. : IS_SH (insn) ? 2
  1470. : (gdb_assert (IS_SW (insn)), 4));
  1471. pv_t addr = pv_add_constant (reg[rm], disp);
  1472. if (stack.store_would_trash (addr))
  1473. break;
  1474. if (is_arg_spill (gdbarch, reg[rn], addr, &stack))
  1475. after_last_frame_setup_insn = next_pc;
  1476. stack.store (addr, size, reg[rn]);
  1477. }
  1478. else if (IS_LDC (insn))
  1479. {
  1480. int rn = LDC_TARGET (insn);
  1481. int cr = LDC_IMM (insn) + MEP_FIRST_CSR_REGNUM;
  1482. reg[rn] = reg[cr];
  1483. }
  1484. else if (IS_LW (insn))
  1485. {
  1486. int rn = LW_TARGET (insn);
  1487. int rm = LW_BASE (insn);
  1488. int offset = LW_OFFSET (insn);
  1489. pv_t addr = pv_add_constant (reg[rm], offset);
  1490. reg[rn] = stack.fetch (addr, 4);
  1491. }
  1492. else if (IS_BRA (insn) && BRA_DISP (insn) > 0)
  1493. {
  1494. /* When a loop appears as the first statement of a function
  1495. body, gcc 4.x will use a BRA instruction to branch to the
  1496. loop condition checking code. This BRA instruction is
  1497. marked as part of the prologue. We therefore set next_pc
  1498. to this branch target and also stop the prologue scan.
  1499. The instructions at and beyond the branch target should
  1500. no longer be associated with the prologue.
  1501. Note that we only consider forward branches here. We
  1502. presume that a forward branch is being used to skip over
  1503. a loop body.
  1504. A backwards branch is covered by the default case below.
  1505. If we were to encounter a backwards branch, that would
  1506. most likely mean that we've scanned through a loop body.
  1507. We definitely want to stop the prologue scan when this
  1508. happens and that is precisely what is done by the default
  1509. case below. */
  1510. next_pc = pc + BRA_DISP (insn);
  1511. after_last_frame_setup_insn = next_pc;
  1512. break;
  1513. }
  1514. else
  1515. /* We've hit some instruction we don't know how to simulate.
  1516. Strictly speaking, we should set every value we're
  1517. tracking to "unknown". But we'll be optimistic, assume
  1518. that we have enough information already, and stop
  1519. analysis here. */
  1520. break;
  1521. /* If this instruction changed the FP or decreased the SP (i.e.,
  1522. allocated more stack space), then this may be a good place to
  1523. declare the prologue finished. However, there are some
  1524. exceptions:
  1525. - If the instruction just changed the FP back to its original
  1526. value, then that's probably a restore instruction. The
  1527. prologue should definitely end before that.
  1528. - If the instruction increased the value of the SP (that is,
  1529. shrunk the frame), then it's probably part of a frame
  1530. teardown sequence, and the prologue should end before that. */
  1531. if (! pv_is_identical (reg[MEP_FP_REGNUM], pre_insn_fp))
  1532. {
  1533. if (! pv_is_register_k (reg[MEP_FP_REGNUM], MEP_FP_REGNUM, 0))
  1534. after_last_frame_setup_insn = next_pc;
  1535. }
  1536. else if (! pv_is_identical (reg[MEP_SP_REGNUM], pre_insn_sp))
  1537. {
  1538. /* The comparison of constants looks odd, there, because .k
  1539. is unsigned. All it really means is that the new value
  1540. is lower than it was before the instruction. */
  1541. if (pv_is_register (pre_insn_sp, MEP_SP_REGNUM)
  1542. && pv_is_register (reg[MEP_SP_REGNUM], MEP_SP_REGNUM)
  1543. && ((pre_insn_sp.k - reg[MEP_SP_REGNUM].k)
  1544. < (reg[MEP_SP_REGNUM].k - pre_insn_sp.k)))
  1545. after_last_frame_setup_insn = next_pc;
  1546. }
  1547. pc = next_pc;
  1548. }
  1549. /* Is the frame size (offset, really) a known constant? */
  1550. if (pv_is_register (reg[MEP_SP_REGNUM], MEP_SP_REGNUM))
  1551. result->frame_size = reg[MEP_SP_REGNUM].k;
  1552. /* Was the frame pointer initialized? */
  1553. if (pv_is_register (reg[MEP_FP_REGNUM], MEP_SP_REGNUM))
  1554. {
  1555. result->has_frame_ptr = 1;
  1556. result->frame_ptr_offset = reg[MEP_FP_REGNUM].k;
  1557. }
  1558. /* Record where all the registers were saved. */
  1559. stack.scan (check_for_saved, (void *) result);
  1560. result->prologue_end = after_last_frame_setup_insn;
  1561. }
  1562. static CORE_ADDR
  1563. mep_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
  1564. {
  1565. const char *name;
  1566. CORE_ADDR func_addr, func_end;
  1567. struct mep_prologue p;
  1568. /* Try to find the extent of the function that contains PC. */
  1569. if (! find_pc_partial_function (pc, &name, &func_addr, &func_end))
  1570. return pc;
  1571. mep_analyze_prologue (gdbarch, pc, func_end, &p);
  1572. return p.prologue_end;
  1573. }
  1574. /* Breakpoints. */
  1575. constexpr gdb_byte mep_break_insn[] = { 0x70, 0x32 };
  1576. typedef BP_MANIPULATION (mep_break_insn) mep_breakpoint;
  1577. /* Frames and frame unwinding. */
  1578. static struct mep_prologue *
  1579. mep_analyze_frame_prologue (struct frame_info *this_frame,
  1580. void **this_prologue_cache)
  1581. {
  1582. if (! *this_prologue_cache)
  1583. {
  1584. CORE_ADDR func_start, stop_addr;
  1585. *this_prologue_cache
  1586. = FRAME_OBSTACK_ZALLOC (struct mep_prologue);
  1587. func_start = get_frame_func (this_frame);
  1588. stop_addr = get_frame_pc (this_frame);
  1589. /* If we couldn't find any function containing the PC, then
  1590. just initialize the prologue cache, but don't do anything. */
  1591. if (! func_start)
  1592. stop_addr = func_start;
  1593. mep_analyze_prologue (get_frame_arch (this_frame),
  1594. func_start, stop_addr,
  1595. (struct mep_prologue *) *this_prologue_cache);
  1596. }
  1597. return (struct mep_prologue *) *this_prologue_cache;
  1598. }
  1599. /* Given the next frame and a prologue cache, return this frame's
  1600. base. */
  1601. static CORE_ADDR
  1602. mep_frame_base (struct frame_info *this_frame,
  1603. void **this_prologue_cache)
  1604. {
  1605. struct mep_prologue *p
  1606. = mep_analyze_frame_prologue (this_frame, this_prologue_cache);
  1607. /* In functions that use alloca, the distance between the stack
  1608. pointer and the frame base varies dynamically, so we can't use
  1609. the SP plus static information like prologue analysis to find the
  1610. frame base. However, such functions must have a frame pointer,
  1611. to be able to restore the SP on exit. So whenever we do have a
  1612. frame pointer, use that to find the base. */
  1613. if (p->has_frame_ptr)
  1614. {
  1615. CORE_ADDR fp
  1616. = get_frame_register_unsigned (this_frame, MEP_FP_REGNUM);
  1617. return fp - p->frame_ptr_offset;
  1618. }
  1619. else
  1620. {
  1621. CORE_ADDR sp
  1622. = get_frame_register_unsigned (this_frame, MEP_SP_REGNUM);
  1623. return sp - p->frame_size;
  1624. }
  1625. }
  1626. static void
  1627. mep_frame_this_id (struct frame_info *this_frame,
  1628. void **this_prologue_cache,
  1629. struct frame_id *this_id)
  1630. {
  1631. *this_id = frame_id_build (mep_frame_base (this_frame, this_prologue_cache),
  1632. get_frame_func (this_frame));
  1633. }
  1634. static struct value *
  1635. mep_frame_prev_register (struct frame_info *this_frame,
  1636. void **this_prologue_cache, int regnum)
  1637. {
  1638. struct mep_prologue *p
  1639. = mep_analyze_frame_prologue (this_frame, this_prologue_cache);
  1640. /* There are a number of complications in unwinding registers on the
  1641. MeP, having to do with core functions calling VLIW functions and
  1642. vice versa.
  1643. The least significant bit of the link register, LP.LTOM, is the
  1644. VLIW mode toggle bit: it's set if a core function called a VLIW
  1645. function, or vice versa, and clear when the caller and callee
  1646. were both in the same mode.
  1647. So, if we're asked to unwind the PC, then we really want to
  1648. unwind the LP and clear the least significant bit. (Real return
  1649. addresses are always even.) And if we want to unwind the program
  1650. status word (PSW), we need to toggle PSW.OM if LP.LTOM is set.
  1651. Tweaking the register values we return in this way means that the
  1652. bits in BUFFERP[] are not the same as the bits you'd find at
  1653. ADDRP in the inferior, so we make sure lvalp is not_lval when we
  1654. do this. */
  1655. if (regnum == MEP_PC_REGNUM)
  1656. {
  1657. struct value *value;
  1658. CORE_ADDR lp;
  1659. value = mep_frame_prev_register (this_frame, this_prologue_cache,
  1660. MEP_LP_REGNUM);
  1661. lp = value_as_long (value);
  1662. release_value (value);
  1663. return frame_unwind_got_constant (this_frame, regnum, lp & ~1);
  1664. }
  1665. else
  1666. {
  1667. CORE_ADDR frame_base = mep_frame_base (this_frame, this_prologue_cache);
  1668. struct value *value;
  1669. /* Our caller's SP is our frame base. */
  1670. if (regnum == MEP_SP_REGNUM)
  1671. return frame_unwind_got_constant (this_frame, regnum, frame_base);
  1672. /* If prologue analysis says we saved this register somewhere,
  1673. return a description of the stack slot holding it. */
  1674. if (p->reg_offset[regnum] != 1)
  1675. value = frame_unwind_got_memory (this_frame, regnum,
  1676. frame_base + p->reg_offset[regnum]);
  1677. /* Otherwise, presume we haven't changed the value of this
  1678. register, and get it from the next frame. */
  1679. else
  1680. value = frame_unwind_got_register (this_frame, regnum, regnum);
  1681. /* If we need to toggle the operating mode, do so. */
  1682. if (regnum == MEP_PSW_REGNUM)
  1683. {
  1684. CORE_ADDR psw, lp;
  1685. psw = value_as_long (value);
  1686. release_value (value);
  1687. /* Get the LP's value, too. */
  1688. value = get_frame_register_value (this_frame, MEP_LP_REGNUM);
  1689. lp = value_as_long (value);
  1690. release_value (value);
  1691. /* If LP.LTOM is set, then toggle PSW.OM. */
  1692. if (lp & 0x1)
  1693. psw ^= 0x1000;
  1694. return frame_unwind_got_constant (this_frame, regnum, psw);
  1695. }
  1696. return value;
  1697. }
  1698. }
  1699. static const struct frame_unwind mep_frame_unwind = {
  1700. "mep prologue",
  1701. NORMAL_FRAME,
  1702. default_frame_unwind_stop_reason,
  1703. mep_frame_this_id,
  1704. mep_frame_prev_register,
  1705. NULL,
  1706. default_frame_sniffer
  1707. };
  1708. /* Return values. */
  1709. static int
  1710. mep_use_struct_convention (struct type *type)
  1711. {
  1712. return (TYPE_LENGTH (type) > MEP_GPR_SIZE);
  1713. }
  1714. static void
  1715. mep_extract_return_value (struct gdbarch *arch,
  1716. struct type *type,
  1717. struct regcache *regcache,
  1718. gdb_byte *valbuf)
  1719. {
  1720. int byte_order = gdbarch_byte_order (arch);
  1721. /* Values that don't occupy a full register appear at the less
  1722. significant end of the value. This is the offset to where the
  1723. value starts. */
  1724. int offset;
  1725. /* Return values > MEP_GPR_SIZE bytes are returned in memory,
  1726. pointed to by R0. */
  1727. gdb_assert (TYPE_LENGTH (type) <= MEP_GPR_SIZE);
  1728. if (byte_order == BFD_ENDIAN_BIG)
  1729. offset = MEP_GPR_SIZE - TYPE_LENGTH (type);
  1730. else
  1731. offset = 0;
  1732. /* Return values that do fit in a single register are returned in R0. */
  1733. regcache->cooked_read_part (MEP_R0_REGNUM, offset, TYPE_LENGTH (type),
  1734. valbuf);
  1735. }
  1736. static void
  1737. mep_store_return_value (struct gdbarch *arch,
  1738. struct type *type,
  1739. struct regcache *regcache,
  1740. const gdb_byte *valbuf)
  1741. {
  1742. int byte_order = gdbarch_byte_order (arch);
  1743. /* Values that fit in a single register go in R0. */
  1744. if (TYPE_LENGTH (type) <= MEP_GPR_SIZE)
  1745. {
  1746. /* Values that don't occupy a full register appear at the least
  1747. significant end of the value. This is the offset to where the
  1748. value starts. */
  1749. int offset;
  1750. if (byte_order == BFD_ENDIAN_BIG)
  1751. offset = MEP_GPR_SIZE - TYPE_LENGTH (type);
  1752. else
  1753. offset = 0;
  1754. regcache->cooked_write_part (MEP_R0_REGNUM, offset, TYPE_LENGTH (type),
  1755. valbuf);
  1756. }
  1757. /* Return values larger than a single register are returned in
  1758. memory, pointed to by R0. Unfortunately, we can't count on R0
  1759. pointing to the return buffer, so we raise an error here. */
  1760. else
  1761. error (_("\
  1762. GDB cannot set return values larger than four bytes; the Media Processor's\n\
  1763. calling conventions do not provide enough information to do this.\n\
  1764. Try using the 'return' command with no argument."));
  1765. }
  1766. static enum return_value_convention
  1767. mep_return_value (struct gdbarch *gdbarch, struct value *function,
  1768. struct type *type, struct regcache *regcache,
  1769. gdb_byte *readbuf, const gdb_byte *writebuf)
  1770. {
  1771. if (mep_use_struct_convention (type))
  1772. {
  1773. if (readbuf)
  1774. {
  1775. ULONGEST addr;
  1776. /* Although the address of the struct buffer gets passed in R1, it's
  1777. returned in R0. Fetch R0's value and then read the memory
  1778. at that address. */
  1779. regcache_raw_read_unsigned (regcache, MEP_R0_REGNUM, &addr);
  1780. read_memory (addr, readbuf, TYPE_LENGTH (type));
  1781. }
  1782. if (writebuf)
  1783. {
  1784. /* Return values larger than a single register are returned in
  1785. memory, pointed to by R0. Unfortunately, we can't count on R0
  1786. pointing to the return buffer, so we raise an error here. */
  1787. error (_("\
  1788. GDB cannot set return values larger than four bytes; the Media Processor's\n\
  1789. calling conventions do not provide enough information to do this.\n\
  1790. Try using the 'return' command with no argument."));
  1791. }
  1792. return RETURN_VALUE_ABI_RETURNS_ADDRESS;
  1793. }
  1794. if (readbuf)
  1795. mep_extract_return_value (gdbarch, type, regcache, readbuf);
  1796. if (writebuf)
  1797. mep_store_return_value (gdbarch, type, regcache, writebuf);
  1798. return RETURN_VALUE_REGISTER_CONVENTION;
  1799. }
  1800. /* Inferior calls. */
  1801. static CORE_ADDR
  1802. mep_frame_align (struct gdbarch *gdbarch, CORE_ADDR sp)
  1803. {
  1804. /* Require word alignment. */
  1805. return sp & -4;
  1806. }
  1807. /* From "lang_spec2.txt":
  1808. 4.2 Calling conventions
  1809. 4.2.1 Core register conventions
  1810. - Parameters should be evaluated from left to right, and they
  1811. should be held in $1,$2,$3,$4 in order. The fifth parameter or
  1812. after should be held in the stack. If the size is larger than 4
  1813. bytes in the first four parameters, the pointer should be held in
  1814. the registers instead. If the size is larger than 4 bytes in the
  1815. fifth parameter or after, the pointer should be held in the stack.
  1816. - Return value of a function should be held in register $0. If the
  1817. size of return value is larger than 4 bytes, $1 should hold the
  1818. pointer pointing memory that would hold the return value. In this
  1819. case, the first parameter should be held in $2, the second one in
  1820. $3, and the third one in $4, and the forth parameter or after
  1821. should be held in the stack.
  1822. [This doesn't say so, but arguments shorter than four bytes are
  1823. passed in the least significant end of a four-byte word when
  1824. they're passed on the stack.] */
  1825. /* Traverse the list of ARGC arguments ARGV; for every ARGV[i] too
  1826. large to fit in a register, save it on the stack, and place its
  1827. address in COPY[i]. SP is the initial stack pointer; return the
  1828. new stack pointer. */
  1829. static CORE_ADDR
  1830. push_large_arguments (CORE_ADDR sp, int argc, struct value **argv,
  1831. CORE_ADDR copy[])
  1832. {
  1833. int i;
  1834. for (i = 0; i < argc; i++)
  1835. {
  1836. unsigned arg_len = TYPE_LENGTH (value_type (argv[i]));
  1837. if (arg_len > MEP_GPR_SIZE)
  1838. {
  1839. /* Reserve space for the copy, and then round the SP down, to
  1840. make sure it's all aligned properly. */
  1841. sp = (sp - arg_len) & -4;
  1842. write_memory (sp, value_contents (argv[i]).data (), arg_len);
  1843. copy[i] = sp;
  1844. }
  1845. }
  1846. return sp;
  1847. }
  1848. static CORE_ADDR
  1849. mep_push_dummy_call (struct gdbarch *gdbarch, struct value *function,
  1850. struct regcache *regcache, CORE_ADDR bp_addr,
  1851. int argc, struct value **argv, CORE_ADDR sp,
  1852. function_call_return_method return_method,
  1853. CORE_ADDR struct_addr)
  1854. {
  1855. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  1856. CORE_ADDR *copy = (CORE_ADDR *) alloca (argc * sizeof (copy[0]));
  1857. int i;
  1858. /* The number of the next register available to hold an argument. */
  1859. int arg_reg;
  1860. /* The address of the next stack slot available to hold an argument. */
  1861. CORE_ADDR arg_stack;
  1862. /* The address of the end of the stack area for arguments. This is
  1863. just for error checking. */
  1864. CORE_ADDR arg_stack_end;
  1865. sp = push_large_arguments (sp, argc, argv, copy);
  1866. /* Reserve space for the stack arguments, if any. */
  1867. arg_stack_end = sp;
  1868. if (argc + (struct_addr ? 1 : 0) > 4)
  1869. sp -= ((argc + (struct_addr ? 1 : 0)) - 4) * MEP_GPR_SIZE;
  1870. arg_reg = MEP_R1_REGNUM;
  1871. arg_stack = sp;
  1872. /* If we're returning a structure by value, push the pointer to the
  1873. buffer as the first argument. */
  1874. if (return_method == return_method_struct)
  1875. {
  1876. regcache_cooked_write_unsigned (regcache, arg_reg, struct_addr);
  1877. arg_reg++;
  1878. }
  1879. for (i = 0; i < argc; i++)
  1880. {
  1881. ULONGEST value;
  1882. /* Arguments that fit in a GPR get expanded to fill the GPR. */
  1883. if (TYPE_LENGTH (value_type (argv[i])) <= MEP_GPR_SIZE)
  1884. value = extract_unsigned_integer (value_contents (argv[i]).data (),
  1885. TYPE_LENGTH (value_type (argv[i])),
  1886. byte_order);
  1887. /* Arguments too large to fit in a GPR get copied to the stack,
  1888. and we pass a pointer to the copy. */
  1889. else
  1890. value = copy[i];
  1891. /* We use $1 -- $4 for passing arguments, then use the stack. */
  1892. if (arg_reg <= MEP_R4_REGNUM)
  1893. {
  1894. regcache_cooked_write_unsigned (regcache, arg_reg, value);
  1895. arg_reg++;
  1896. }
  1897. else
  1898. {
  1899. gdb_byte buf[MEP_GPR_SIZE];
  1900. store_unsigned_integer (buf, MEP_GPR_SIZE, byte_order, value);
  1901. write_memory (arg_stack, buf, MEP_GPR_SIZE);
  1902. arg_stack += MEP_GPR_SIZE;
  1903. }
  1904. }
  1905. gdb_assert (arg_stack <= arg_stack_end);
  1906. /* Set the return address. */
  1907. regcache_cooked_write_unsigned (regcache, MEP_LP_REGNUM, bp_addr);
  1908. /* Update the stack pointer. */
  1909. regcache_cooked_write_unsigned (regcache, MEP_SP_REGNUM, sp);
  1910. return sp;
  1911. }
  1912. /* Initialization. */
  1913. static struct gdbarch *
  1914. mep_gdbarch_init (struct gdbarch_info info, struct gdbarch_list *arches)
  1915. {
  1916. struct gdbarch *gdbarch;
  1917. /* Which me_module are we building a gdbarch object for? */
  1918. CONFIG_ATTR me_module;
  1919. /* If we have a BFD in hand, figure out which me_module it was built
  1920. for. Otherwise, use the no-particular-me_module code. */
  1921. if (info.abfd)
  1922. {
  1923. /* The way to get the me_module code depends on the object file
  1924. format. At the moment, we only know how to handle ELF. */
  1925. if (bfd_get_flavour (info.abfd) == bfd_target_elf_flavour)
  1926. {
  1927. int flag = elf_elfheader (info.abfd)->e_flags & EF_MEP_INDEX_MASK;
  1928. me_module = (CONFIG_ATTR) flag;
  1929. }
  1930. else
  1931. me_module = CONFIG_NONE;
  1932. }
  1933. else
  1934. me_module = CONFIG_NONE;
  1935. /* If we're setting the architecture from a file, check the
  1936. endianness of the file against that of the me_module. */
  1937. if (info.abfd)
  1938. {
  1939. /* The negations on either side make the comparison treat all
  1940. non-zero (true) values as equal. */
  1941. if (! bfd_big_endian (info.abfd) != ! me_module_big_endian (me_module))
  1942. {
  1943. const char *module_name = me_module_name (me_module);
  1944. const char *module_endianness
  1945. = me_module_big_endian (me_module) ? "big" : "little";
  1946. const char *file_name = bfd_get_filename (info.abfd);
  1947. const char *file_endianness
  1948. = bfd_big_endian (info.abfd) ? "big" : "little";
  1949. gdb_putc ('\n', gdb_stderr);
  1950. if (module_name)
  1951. warning (_("the MeP module '%s' is %s-endian, but the executable\n"
  1952. "%s is %s-endian."),
  1953. module_name, module_endianness,
  1954. file_name, file_endianness);
  1955. else
  1956. warning (_("the selected MeP module is %s-endian, but the "
  1957. "executable\n"
  1958. "%s is %s-endian."),
  1959. module_endianness, file_name, file_endianness);
  1960. }
  1961. }
  1962. /* Find a candidate among the list of architectures we've created
  1963. already. info->bfd_arch_info needs to match, but we also want
  1964. the right me_module: the ELF header's e_flags field needs to
  1965. match as well. */
  1966. for (arches = gdbarch_list_lookup_by_info (arches, &info);
  1967. arches != NULL;
  1968. arches = gdbarch_list_lookup_by_info (arches->next, &info))
  1969. {
  1970. mep_gdbarch_tdep *tdep
  1971. = (mep_gdbarch_tdep *) gdbarch_tdep (arches->gdbarch);
  1972. if (tdep->me_module == me_module)
  1973. return arches->gdbarch;
  1974. }
  1975. mep_gdbarch_tdep *tdep = new mep_gdbarch_tdep;
  1976. gdbarch = gdbarch_alloc (&info, tdep);
  1977. /* Get a CGEN CPU descriptor for this architecture. */
  1978. {
  1979. const char *mach_name = info.bfd_arch_info->printable_name;
  1980. enum cgen_endian endian = (info.byte_order == BFD_ENDIAN_BIG
  1981. ? CGEN_ENDIAN_BIG
  1982. : CGEN_ENDIAN_LITTLE);
  1983. tdep->cpu_desc = mep_cgen_cpu_open (CGEN_CPU_OPEN_BFDMACH, mach_name,
  1984. CGEN_CPU_OPEN_ENDIAN, endian,
  1985. CGEN_CPU_OPEN_END);
  1986. }
  1987. tdep->me_module = me_module;
  1988. /* Register set. */
  1989. set_gdbarch_num_regs (gdbarch, MEP_NUM_RAW_REGS);
  1990. set_gdbarch_pc_regnum (gdbarch, MEP_PC_REGNUM);
  1991. set_gdbarch_sp_regnum (gdbarch, MEP_SP_REGNUM);
  1992. set_gdbarch_register_name (gdbarch, mep_register_name);
  1993. set_gdbarch_register_type (gdbarch, mep_register_type);
  1994. set_gdbarch_num_pseudo_regs (gdbarch, MEP_NUM_PSEUDO_REGS);
  1995. set_gdbarch_pseudo_register_read (gdbarch, mep_pseudo_register_read);
  1996. set_gdbarch_pseudo_register_write (gdbarch, mep_pseudo_register_write);
  1997. set_gdbarch_dwarf2_reg_to_regnum (gdbarch, mep_debug_reg_to_regnum);
  1998. set_gdbarch_stab_reg_to_regnum (gdbarch, mep_debug_reg_to_regnum);
  1999. set_gdbarch_register_reggroup_p (gdbarch, mep_register_reggroup_p);
  2000. reggroup_add (gdbarch, mep_csr_reggroup);
  2001. reggroup_add (gdbarch, mep_cr_reggroup);
  2002. reggroup_add (gdbarch, mep_ccr_reggroup);
  2003. /* Disassembly. */
  2004. set_gdbarch_print_insn (gdbarch, mep_gdb_print_insn);
  2005. /* Breakpoints. */
  2006. set_gdbarch_breakpoint_kind_from_pc (gdbarch, mep_breakpoint::kind_from_pc);
  2007. set_gdbarch_sw_breakpoint_from_kind (gdbarch, mep_breakpoint::bp_from_kind);
  2008. set_gdbarch_decr_pc_after_break (gdbarch, 0);
  2009. set_gdbarch_skip_prologue (gdbarch, mep_skip_prologue);
  2010. /* Frames and frame unwinding. */
  2011. frame_unwind_append_unwinder (gdbarch, &mep_frame_unwind);
  2012. set_gdbarch_inner_than (gdbarch, core_addr_lessthan);
  2013. set_gdbarch_frame_args_skip (gdbarch, 0);
  2014. /* Return values. */
  2015. set_gdbarch_return_value (gdbarch, mep_return_value);
  2016. /* Inferior function calls. */
  2017. set_gdbarch_frame_align (gdbarch, mep_frame_align);
  2018. set_gdbarch_push_dummy_call (gdbarch, mep_push_dummy_call);
  2019. return gdbarch;
  2020. }
  2021. void _initialize_mep_tdep ();
  2022. void
  2023. _initialize_mep_tdep ()
  2024. {
  2025. mep_csr_reggroup = reggroup_new ("csr", USER_REGGROUP);
  2026. mep_cr_reggroup = reggroup_new ("cr", USER_REGGROUP);
  2027. mep_ccr_reggroup = reggroup_new ("ccr", USER_REGGROUP);
  2028. register_gdbarch_init (bfd_arch_mep, mep_gdbarch_init);
  2029. mep_init_pseudoregister_maps ();
  2030. }