tc-mep.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203
  1. /* tc-mep.c -- Assembler for the Toshiba Media Processor.
  2. Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GAS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GAS; see the file COPYING. If not, write to
  14. the Free Software Foundation, 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. #include "as.h"
  17. #include <stdio.h>
  18. #include "dwarf2dbg.h"
  19. #include "subsegs.h"
  20. #include "symcat.h"
  21. #include "opcodes/mep-desc.h"
  22. #include "opcodes/mep-opc.h"
  23. #include "cgen.h"
  24. #include "elf/common.h"
  25. #include "elf/mep.h"
  26. #include "xregex.h"
  27. /* Structure to hold all of the different components describing
  28. an individual instruction. */
  29. typedef struct
  30. {
  31. const CGEN_INSN * insn;
  32. const CGEN_INSN * orig_insn;
  33. CGEN_FIELDS fields;
  34. #if CGEN_INT_INSN_P
  35. CGEN_INSN_INT buffer [1];
  36. #define INSN_VALUE(buf) (*(buf))
  37. #else
  38. unsigned char buffer [CGEN_MAX_INSN_SIZE];
  39. #define INSN_VALUE(buf) (buf)
  40. #endif
  41. char * addr;
  42. fragS * frag;
  43. int num_fixups;
  44. fixS * fixups [GAS_CGEN_MAX_FIXUPS];
  45. int indices [MAX_OPERAND_INSTANCES];
  46. } mep_insn;
  47. static int mode = CORE; /* Start in core mode. */
  48. static int pluspresent = 0;
  49. static int allow_disabled_registers = 0;
  50. static int library_flag = 0;
  51. static int mep_cop = EF_MEP_COP_NONE;
  52. /* We're going to need to store all of the instructions along with
  53. their fixups so that we can parallelization grouping rules. */
  54. static mep_insn saved_insns[MAX_SAVED_FIXUP_CHAINS];
  55. static int num_insns_saved = 0;
  56. const char comment_chars[] = "#";
  57. const char line_comment_chars[] = ";#";
  58. const char line_separator_chars[] = ";";
  59. const char EXP_CHARS[] = "eE";
  60. const char FLT_CHARS[] = "dD";
  61. static void mep_switch_to_vliw_mode (int);
  62. static void mep_switch_to_core_mode (int);
  63. static void mep_s_vtext (int);
  64. static void mep_noregerr (int);
  65. /* The target specific pseudo-ops which we support. */
  66. const pseudo_typeS md_pseudo_table[] =
  67. {
  68. { "word", cons, 4 },
  69. { "vliw", mep_switch_to_vliw_mode, 0 },
  70. { "core", mep_switch_to_core_mode, 0 },
  71. { "vtext", mep_s_vtext, 0 },
  72. { "noregerr", mep_noregerr, 0 },
  73. { NULL, NULL, 0 }
  74. };
  75. /* Relocations against symbols are done in two
  76. parts, with a HI relocation and a LO relocation. Each relocation
  77. has only 16 bits of space to store an addend. This means that in
  78. order for the linker to handle carries correctly, it must be able
  79. to locate both the HI and the LO relocation. This means that the
  80. relocations must appear in order in the relocation table.
  81. In order to implement this, we keep track of each unmatched HI
  82. relocation. We then sort them so that they immediately precede the
  83. corresponding LO relocation. */
  84. struct mep_hi_fixup
  85. {
  86. struct mep_hi_fixup * next; /* Next HI fixup. */
  87. fixS * fixp; /* This fixup. */
  88. segT seg; /* The section this fixup is in. */
  89. };
  90. /* The list of unmatched HI relocs. */
  91. static struct mep_hi_fixup * mep_hi_fixup_list;
  92. #define OPTION_EB (OPTION_MD_BASE + 0)
  93. #define OPTION_EL (OPTION_MD_BASE + 1)
  94. #define OPTION_CONFIG (OPTION_MD_BASE + 2)
  95. #define OPTION_AVERAGE (OPTION_MD_BASE + 3)
  96. #define OPTION_NOAVERAGE (OPTION_MD_BASE + 4)
  97. #define OPTION_MULT (OPTION_MD_BASE + 5)
  98. #define OPTION_NOMULT (OPTION_MD_BASE + 6)
  99. #define OPTION_DIV (OPTION_MD_BASE + 7)
  100. #define OPTION_NODIV (OPTION_MD_BASE + 8)
  101. #define OPTION_BITOPS (OPTION_MD_BASE + 9)
  102. #define OPTION_NOBITOPS (OPTION_MD_BASE + 10)
  103. #define OPTION_LEADZ (OPTION_MD_BASE + 11)
  104. #define OPTION_NOLEADZ (OPTION_MD_BASE + 12)
  105. #define OPTION_ABSDIFF (OPTION_MD_BASE + 13)
  106. #define OPTION_NOABSDIFF (OPTION_MD_BASE + 14)
  107. #define OPTION_MINMAX (OPTION_MD_BASE + 15)
  108. #define OPTION_NOMINMAX (OPTION_MD_BASE + 16)
  109. #define OPTION_CLIP (OPTION_MD_BASE + 17)
  110. #define OPTION_NOCLIP (OPTION_MD_BASE + 18)
  111. #define OPTION_SATUR (OPTION_MD_BASE + 19)
  112. #define OPTION_NOSATUR (OPTION_MD_BASE + 20)
  113. #define OPTION_COP32 (OPTION_MD_BASE + 21)
  114. #define OPTION_REPEAT (OPTION_MD_BASE + 25)
  115. #define OPTION_NOREPEAT (OPTION_MD_BASE + 26)
  116. #define OPTION_DEBUG (OPTION_MD_BASE + 27)
  117. #define OPTION_NODEBUG (OPTION_MD_BASE + 28)
  118. #define OPTION_UCI (OPTION_MD_BASE + 29)
  119. #define OPTION_NOUCI (OPTION_MD_BASE + 30)
  120. #define OPTION_DSP (OPTION_MD_BASE + 31)
  121. #define OPTION_NODSP (OPTION_MD_BASE + 32)
  122. #define OPTION_LIBRARY (OPTION_MD_BASE + 33)
  123. struct option md_longopts[] = {
  124. { "EB", no_argument, NULL, OPTION_EB},
  125. { "EL", no_argument, NULL, OPTION_EL},
  126. { "mconfig", required_argument, NULL, OPTION_CONFIG},
  127. { "maverage", no_argument, NULL, OPTION_AVERAGE},
  128. { "mno-average", no_argument, NULL, OPTION_NOAVERAGE},
  129. { "mmult", no_argument, NULL, OPTION_MULT},
  130. { "mno-mult", no_argument, NULL, OPTION_NOMULT},
  131. { "mdiv", no_argument, NULL, OPTION_DIV},
  132. { "mno-div", no_argument, NULL, OPTION_NODIV},
  133. { "mbitops", no_argument, NULL, OPTION_BITOPS},
  134. { "mno-bitops", no_argument, NULL, OPTION_NOBITOPS},
  135. { "mleadz", no_argument, NULL, OPTION_LEADZ},
  136. { "mno-leadz", no_argument, NULL, OPTION_NOLEADZ},
  137. { "mabsdiff", no_argument, NULL, OPTION_ABSDIFF},
  138. { "mno-absdiff", no_argument, NULL, OPTION_NOABSDIFF},
  139. { "mminmax", no_argument, NULL, OPTION_MINMAX},
  140. { "mno-minmax", no_argument, NULL, OPTION_NOMINMAX},
  141. { "mclip", no_argument, NULL, OPTION_CLIP},
  142. { "mno-clip", no_argument, NULL, OPTION_NOCLIP},
  143. { "msatur", no_argument, NULL, OPTION_SATUR},
  144. { "mno-satur", no_argument, NULL, OPTION_NOSATUR},
  145. { "mcop32", no_argument, NULL, OPTION_COP32},
  146. { "mdebug", no_argument, NULL, OPTION_DEBUG},
  147. { "mno-debug", no_argument, NULL, OPTION_NODEBUG},
  148. { "muci", no_argument, NULL, OPTION_UCI},
  149. { "mno-uci", no_argument, NULL, OPTION_NOUCI},
  150. { "mdsp", no_argument, NULL, OPTION_DSP},
  151. { "mno-dsp", no_argument, NULL, OPTION_NODSP},
  152. { "mlibrary", no_argument, NULL, OPTION_LIBRARY},
  153. { NULL, 0, NULL, 0 } };
  154. size_t md_longopts_size = sizeof (md_longopts);
  155. /* Options which default to on/off together. See the comment where
  156. this is used for details. Note that CP and CP64 are not in this
  157. list because disabling those overrides the -mivc2 option. */
  158. #define OPTION_MASK \
  159. ( (1 << CGEN_INSN_OPTIONAL_BIT_INSN) \
  160. | (1 << CGEN_INSN_OPTIONAL_MUL_INSN) \
  161. | (1 << CGEN_INSN_OPTIONAL_DIV_INSN) \
  162. | (1 << CGEN_INSN_OPTIONAL_DEBUG_INSN) \
  163. | (1 << CGEN_INSN_OPTIONAL_LDZ_INSN) \
  164. | (1 << CGEN_INSN_OPTIONAL_ABS_INSN) \
  165. | (1 << CGEN_INSN_OPTIONAL_AVE_INSN) \
  166. | (1 << CGEN_INSN_OPTIONAL_MINMAX_INSN) \
  167. | (1 << CGEN_INSN_OPTIONAL_CLIP_INSN) \
  168. | (1 << CGEN_INSN_OPTIONAL_SAT_INSN) \
  169. | (1 << CGEN_INSN_OPTIONAL_UCI_INSN) \
  170. | (1 << CGEN_INSN_OPTIONAL_DSP_INSN) )
  171. const char * md_shortopts = "";
  172. static int optbits = 0;
  173. static int optbitset = 0;
  174. int
  175. md_parse_option (int c, const char *arg ATTRIBUTE_UNUSED)
  176. {
  177. int i, idx;
  178. switch (c)
  179. {
  180. case OPTION_EB:
  181. target_big_endian = 1;
  182. break;
  183. case OPTION_EL:
  184. target_big_endian = 0;
  185. break;
  186. case OPTION_CONFIG:
  187. idx = 0;
  188. for (i=1; mep_config_map[i].name; i++)
  189. if (strcmp (mep_config_map[i].name, arg) == 0)
  190. {
  191. idx = i;
  192. break;
  193. }
  194. if (!idx)
  195. {
  196. fprintf (stderr, "Error: unknown configuration %s\n", arg);
  197. return 0;
  198. }
  199. mep_config_index = idx;
  200. target_big_endian = mep_config_map[idx].big_endian;
  201. break;
  202. case OPTION_AVERAGE:
  203. optbits |= 1 << CGEN_INSN_OPTIONAL_AVE_INSN;
  204. optbitset |= 1 << CGEN_INSN_OPTIONAL_AVE_INSN;
  205. break;
  206. case OPTION_NOAVERAGE:
  207. optbits &= ~(1 << CGEN_INSN_OPTIONAL_AVE_INSN);
  208. optbitset |= 1 << CGEN_INSN_OPTIONAL_AVE_INSN;
  209. break;
  210. case OPTION_MULT:
  211. optbits |= 1 << CGEN_INSN_OPTIONAL_MUL_INSN;
  212. optbitset |= 1 << CGEN_INSN_OPTIONAL_MUL_INSN;
  213. break;
  214. case OPTION_NOMULT:
  215. optbits &= ~(1 << CGEN_INSN_OPTIONAL_MUL_INSN);
  216. optbitset |= 1 << CGEN_INSN_OPTIONAL_MUL_INSN;
  217. break;
  218. case OPTION_DIV:
  219. optbits |= 1 << CGEN_INSN_OPTIONAL_DIV_INSN;
  220. optbitset |= 1 << CGEN_INSN_OPTIONAL_DIV_INSN;
  221. break;
  222. case OPTION_NODIV:
  223. optbits &= ~(1 << CGEN_INSN_OPTIONAL_DIV_INSN);
  224. optbitset |= 1 << CGEN_INSN_OPTIONAL_DIV_INSN;
  225. break;
  226. case OPTION_BITOPS:
  227. optbits |= 1 << CGEN_INSN_OPTIONAL_BIT_INSN;
  228. optbitset |= 1 << CGEN_INSN_OPTIONAL_BIT_INSN;
  229. break;
  230. case OPTION_NOBITOPS:
  231. optbits &= ~(1 << CGEN_INSN_OPTIONAL_BIT_INSN);
  232. optbitset |= 1 << CGEN_INSN_OPTIONAL_BIT_INSN;
  233. break;
  234. case OPTION_LEADZ:
  235. optbits |= 1 << CGEN_INSN_OPTIONAL_LDZ_INSN;
  236. optbitset |= 1 << CGEN_INSN_OPTIONAL_LDZ_INSN;
  237. break;
  238. case OPTION_NOLEADZ:
  239. optbits &= ~(1 << CGEN_INSN_OPTIONAL_LDZ_INSN);
  240. optbitset |= 1 << CGEN_INSN_OPTIONAL_LDZ_INSN;
  241. break;
  242. case OPTION_ABSDIFF:
  243. optbits |= 1 << CGEN_INSN_OPTIONAL_ABS_INSN;
  244. optbitset |= 1 << CGEN_INSN_OPTIONAL_ABS_INSN;
  245. break;
  246. case OPTION_NOABSDIFF:
  247. optbits &= ~(1 << CGEN_INSN_OPTIONAL_ABS_INSN);
  248. optbitset |= 1 << CGEN_INSN_OPTIONAL_ABS_INSN;
  249. break;
  250. case OPTION_MINMAX:
  251. optbits |= 1 << CGEN_INSN_OPTIONAL_MINMAX_INSN;
  252. optbitset |= 1 << CGEN_INSN_OPTIONAL_MINMAX_INSN;
  253. break;
  254. case OPTION_NOMINMAX:
  255. optbits &= ~(1 << CGEN_INSN_OPTIONAL_MINMAX_INSN);
  256. optbitset |= 1 << CGEN_INSN_OPTIONAL_MINMAX_INSN;
  257. break;
  258. case OPTION_CLIP:
  259. optbits |= 1 << CGEN_INSN_OPTIONAL_CLIP_INSN;
  260. optbitset |= 1 << CGEN_INSN_OPTIONAL_CLIP_INSN;
  261. break;
  262. case OPTION_NOCLIP:
  263. optbits &= ~(1 << CGEN_INSN_OPTIONAL_CLIP_INSN);
  264. optbitset |= 1 << CGEN_INSN_OPTIONAL_CLIP_INSN;
  265. break;
  266. case OPTION_SATUR:
  267. optbits |= 1 << CGEN_INSN_OPTIONAL_SAT_INSN;
  268. optbitset |= 1 << CGEN_INSN_OPTIONAL_SAT_INSN;
  269. break;
  270. case OPTION_NOSATUR:
  271. optbits &= ~(1 << CGEN_INSN_OPTIONAL_SAT_INSN);
  272. optbitset |= 1 << CGEN_INSN_OPTIONAL_SAT_INSN;
  273. break;
  274. case OPTION_COP32:
  275. optbits |= 1 << CGEN_INSN_OPTIONAL_CP_INSN;
  276. optbitset |= 1 << CGEN_INSN_OPTIONAL_CP_INSN;
  277. break;
  278. case OPTION_DEBUG:
  279. optbits |= 1 << CGEN_INSN_OPTIONAL_DEBUG_INSN;
  280. optbitset |= 1 << CGEN_INSN_OPTIONAL_DEBUG_INSN;
  281. break;
  282. case OPTION_NODEBUG:
  283. optbits &= ~(1 << CGEN_INSN_OPTIONAL_DEBUG_INSN);
  284. optbitset |= 1 << CGEN_INSN_OPTIONAL_DEBUG_INSN;
  285. break;
  286. case OPTION_UCI:
  287. optbits |= 1 << CGEN_INSN_OPTIONAL_UCI_INSN;
  288. optbitset |= 1 << CGEN_INSN_OPTIONAL_UCI_INSN;
  289. break;
  290. case OPTION_NOUCI:
  291. optbits &= ~(1 << CGEN_INSN_OPTIONAL_UCI_INSN);
  292. optbitset |= 1 << CGEN_INSN_OPTIONAL_UCI_INSN;
  293. break;
  294. case OPTION_DSP:
  295. optbits |= 1 << CGEN_INSN_OPTIONAL_DSP_INSN;
  296. optbitset |= 1 << CGEN_INSN_OPTIONAL_DSP_INSN;
  297. break;
  298. case OPTION_NODSP:
  299. optbits &= ~(1 << CGEN_INSN_OPTIONAL_DSP_INSN);
  300. optbitset |= 1 << CGEN_INSN_OPTIONAL_DSP_INSN;
  301. break;
  302. case OPTION_LIBRARY:
  303. library_flag = EF_MEP_LIBRARY;
  304. break;
  305. case OPTION_REPEAT:
  306. case OPTION_NOREPEAT:
  307. break;
  308. default:
  309. return 0;
  310. }
  311. return 1;
  312. }
  313. void
  314. md_show_usage (FILE *stream)
  315. {
  316. fprintf (stream, _("MeP specific command line options:\n\
  317. -EB assemble for a big endian system\n\
  318. -EL assemble for a little endian system (default)\n\
  319. -mconfig=<name> specify a chip configuration to use\n\
  320. -maverage -mno-average -mmult -mno-mult -mdiv -mno-div\n\
  321. -mbitops -mno-bitops -mleadz -mno-leadz -mabsdiff -mno-absdiff\n\
  322. -mminmax -mno-minmax -mclip -mno-clip -msatur -mno-satur -mcop32\n\
  323. enable/disable the given opcodes\n\
  324. \n\
  325. If -mconfig is given, the other -m options modify it. Otherwise,\n\
  326. if no -m options are given, all core opcodes are enabled;\n\
  327. if any enabling -m options are given, only those are enabled;\n\
  328. if only disabling -m options are given, only those are disabled.\n\
  329. "));
  330. if (mep_config_map[1].name)
  331. {
  332. int i;
  333. fprintf (stream, " -mconfig=STR specify the configuration to use\n");
  334. fprintf (stream, " Configurations:");
  335. for (i=0; mep_config_map[i].name; i++)
  336. fprintf (stream, " %s", mep_config_map[i].name);
  337. fprintf (stream, "\n");
  338. }
  339. }
  340. static void
  341. mep_check_for_disabled_registers (mep_insn *insn)
  342. {
  343. static int initted = 0;
  344. static int has_mul_div = 0;
  345. static int has_cop = 0;
  346. static int has_debug = 0;
  347. unsigned int b, r;
  348. if (allow_disabled_registers)
  349. return;
  350. #if !CGEN_INT_INSN_P
  351. if (target_big_endian)
  352. b = insn->buffer[0] * 256 + insn->buffer[1];
  353. else
  354. b = insn->buffer[1] * 256 + insn->buffer[0];
  355. #else
  356. b = insn->buffer[0];
  357. #endif
  358. if ((b & 0xfffff00e) == 0x7008 /* stc */
  359. || (b & 0xfffff00e) == 0x700a /* ldc */)
  360. {
  361. if (!initted)
  362. {
  363. initted = 1;
  364. if ((MEP_OMASK & (1 << CGEN_INSN_OPTIONAL_MUL_INSN))
  365. || (MEP_OMASK & (1 << CGEN_INSN_OPTIONAL_DIV_INSN)))
  366. has_mul_div = 1;
  367. if (MEP_OMASK & (1 << CGEN_INSN_OPTIONAL_DEBUG_INSN))
  368. has_debug = 1;
  369. if (MEP_OMASK & (1 << CGEN_INSN_OPTIONAL_CP_INSN))
  370. has_cop = 1;
  371. }
  372. r = ((b & 0x00f0) >> 4) | ((b & 0x0001) << 4);
  373. switch (r)
  374. {
  375. case 7: /* $hi */
  376. case 8: /* $lo */
  377. if (!has_mul_div)
  378. as_bad (_("$hi and $lo are disabled when MUL and DIV are off"));
  379. break;
  380. case 12: /* $mb0 */
  381. case 13: /* $me0 */
  382. case 14: /* $mb1 */
  383. case 15: /* $me1 */
  384. if (!has_cop)
  385. as_bad (_("$mb0, $me0, $mb1, and $me1 are disabled when COP is off"));
  386. break;
  387. case 24: /* $dbg */
  388. case 25: /* $depc */
  389. if (!has_debug)
  390. as_bad (_("$dbg and $depc are disabled when DEBUG is off"));
  391. break;
  392. }
  393. }
  394. }
  395. static int
  396. mep_machine (void)
  397. {
  398. switch (MEP_CPU & EF_MEP_CPU_MASK)
  399. {
  400. default: break;
  401. case EF_MEP_CPU_C2: return bfd_mach_mep;
  402. case EF_MEP_CPU_C3: return bfd_mach_mep;
  403. case EF_MEP_CPU_C4: return bfd_mach_mep;
  404. case EF_MEP_CPU_C5: return bfd_mach_mep_c5;
  405. case EF_MEP_CPU_H1: return bfd_mach_mep_h1;
  406. }
  407. return bfd_mach_mep;
  408. }
  409. /* The MeP version of the cgen parse_operand function. The only difference
  410. from the standard version is that we want to avoid treating '$foo' and
  411. '($foo...)' as references to a symbol called '$foo'. The chances are
  412. that '$foo' is really a misspelled register. */
  413. static const char *
  414. mep_parse_operand (CGEN_CPU_DESC cd, enum cgen_parse_operand_type want,
  415. const char **strP, int opindex, int opinfo,
  416. enum cgen_parse_operand_result *resultP, bfd_vma *valueP)
  417. {
  418. if (want == CGEN_PARSE_OPERAND_INTEGER || want == CGEN_PARSE_OPERAND_ADDRESS)
  419. {
  420. const char *next;
  421. next = *strP;
  422. while (*next == '(')
  423. next++;
  424. if (*next == '$')
  425. return "Not a valid literal";
  426. }
  427. return gas_cgen_parse_operand (cd, want, strP, opindex, opinfo,
  428. resultP, valueP);
  429. }
  430. void
  431. md_begin (void)
  432. {
  433. /* Initialize the `cgen' interface. */
  434. /* If the user specifies no options, we default to allowing
  435. everything. If the user specifies any enabling options, we
  436. default to allowing only what is specified. If the user
  437. specifies only disabling options, we only disable what is
  438. specified. If the user specifies options and a config, the
  439. options modify the config. */
  440. if (optbits && mep_config_index == 0)
  441. {
  442. MEP_OMASK &= ~OPTION_MASK;
  443. MEP_OMASK |= optbits;
  444. }
  445. else
  446. MEP_OMASK = (MEP_OMASK & ~optbitset) | optbits;
  447. mep_cop = mep_config_map[mep_config_index].cpu_flag & EF_MEP_COP_MASK;
  448. /* Set the machine number and endian. */
  449. gas_cgen_cpu_desc = mep_cgen_cpu_open (CGEN_CPU_OPEN_MACHS, 0U,
  450. CGEN_CPU_OPEN_ENDIAN,
  451. target_big_endian
  452. ? CGEN_ENDIAN_BIG
  453. : CGEN_ENDIAN_LITTLE,
  454. CGEN_CPU_OPEN_ISAS, (CGEN_BITSET *) 0,
  455. CGEN_CPU_OPEN_END);
  456. mep_cgen_init_asm (gas_cgen_cpu_desc);
  457. /* This is a callback from cgen to gas to parse operands. */
  458. cgen_set_parse_operand_fn (gas_cgen_cpu_desc, mep_parse_operand);
  459. /* Identify the architecture. */
  460. bfd_default_set_arch_mach (stdoutput, bfd_arch_mep, mep_machine ());
  461. /* Store the configuration number and core. */
  462. bfd_set_private_flags (stdoutput, MEP_CPU | MEP_CONFIG | library_flag);
  463. /* Initialize the array we'll be using to store fixups. */
  464. gas_cgen_initialize_saved_fixups_array();
  465. }
  466. /* Variant of mep_cgen_assemble_insn. Assemble insn STR of cpu CD as a
  467. coprocessor instruction, if possible, into FIELDS, BUF, and INSN. */
  468. static const CGEN_INSN *
  469. mep_cgen_assemble_cop_insn (CGEN_CPU_DESC cd,
  470. const char *str,
  471. CGEN_FIELDS *fields,
  472. CGEN_INSN_BYTES_PTR buf,
  473. const struct cgen_insn *pinsn)
  474. {
  475. const char *start;
  476. CGEN_INSN_LIST *ilist;
  477. const char *errmsg = NULL;
  478. /* The instructions are stored in hashed lists. */
  479. ilist = CGEN_ASM_LOOKUP_INSN (gas_cgen_cpu_desc,
  480. CGEN_INSN_MNEMONIC (pinsn));
  481. start = str;
  482. for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
  483. {
  484. const CGEN_INSN *insn = ilist->insn;
  485. if (strcmp (CGEN_INSN_MNEMONIC (ilist->insn),
  486. CGEN_INSN_MNEMONIC (pinsn)) == 0
  487. && MEP_INSN_COP_P (ilist->insn)
  488. && mep_cgen_insn_supported (cd, insn))
  489. {
  490. str = start;
  491. /* skip this insn if str doesn't look right lexically */
  492. if (CGEN_INSN_RX (insn) != NULL &&
  493. regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
  494. continue;
  495. /* Allow parse/insert handlers to obtain length of insn. */
  496. CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
  497. errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
  498. if (errmsg != NULL)
  499. continue;
  500. errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
  501. (bfd_vma) 0);
  502. if (errmsg != NULL)
  503. continue;
  504. return insn;
  505. }
  506. }
  507. return pinsn;
  508. }
  509. static void
  510. mep_save_insn (mep_insn insn)
  511. {
  512. /* Consider change MAX_SAVED_FIXUP_CHAINS to MAX_PARALLEL_INSNS. */
  513. if (num_insns_saved < 0 || num_insns_saved >= MAX_SAVED_FIXUP_CHAINS)
  514. {
  515. as_fatal("index into saved_insns[] out of bounds.");
  516. return;
  517. }
  518. saved_insns[num_insns_saved] = insn;
  519. gas_cgen_save_fixups(num_insns_saved);
  520. num_insns_saved++;
  521. }
  522. static void
  523. mep_check_parallel32_scheduling (void)
  524. {
  525. int insn0iscopro, insn1iscopro, insn0length, insn1length;
  526. /* More than two instructions means that either someone is referring to
  527. an internally parallel core or an internally parallel coprocessor,
  528. neither of which are supported at this time. */
  529. if ( num_insns_saved > 2 )
  530. as_fatal("Internally paralleled cores and coprocessors not supported.");
  531. /* If there are no insns saved, that's ok. Just return. This will
  532. happen when mep_process_saved_insns is called when the end of the
  533. source file is reached and there are no insns left to be processed. */
  534. if (num_insns_saved == 0)
  535. return;
  536. /* Check some of the attributes of the first insn. */
  537. insn0iscopro = MEP_INSN_COP_P (saved_insns[0].insn);
  538. insn0length = CGEN_FIELDS_BITSIZE (& saved_insns[0].fields);
  539. if (num_insns_saved == 2)
  540. {
  541. /* Check some of the attributes of the first insn. */
  542. insn1iscopro = MEP_INSN_COP_P (saved_insns[1].insn);
  543. insn1length = CGEN_FIELDS_BITSIZE (& saved_insns[1].fields);
  544. if ((insn0iscopro && !insn1iscopro)
  545. || (insn1iscopro && !insn0iscopro))
  546. {
  547. /* We have one core and one copro insn. If their sizes
  548. add up to 32, then the combination is valid. */
  549. if (insn0length + insn1length == 32)
  550. return;
  551. else
  552. as_bad (_("core and copro insn lengths must total 32 bits."));
  553. }
  554. else
  555. as_bad (_("vliw group must consist of 1 core and 1 copro insn."));
  556. }
  557. else
  558. {
  559. /* If we arrive here, we have one saved instruction. There are a
  560. number of possible cases:
  561. 1. The instruction is a 32 bit core or coprocessor insn and
  562. can be executed by itself. Valid.
  563. 2. The instruction is a core instruction for which a cop nop
  564. exists. In this case, insert the cop nop into the saved
  565. insn array after the core insn and return. Valid.
  566. 3. The instruction is a coprocessor insn for which a core nop
  567. exists. In this case, move the coprocessor insn to the
  568. second element of the array and put the nop in the first
  569. element then return. Valid.
  570. 4. The instruction is a core or coprocessor instruction for
  571. which there is no matching coprocessor or core nop to use
  572. to form a valid vliw insn combination. In this case, we
  573. we have to abort. */
  574. if (insn0length > 32)
  575. as_fatal ("Cannot use 48- or 64-bit insns with a 32 bit datapath.");
  576. if (insn0length == 32)
  577. return;
  578. /* Insn is smaller than datapath. If there are no matching
  579. nops for this insn, then terminate assembly. */
  580. if (CGEN_INSN_ATTR_VALUE (saved_insns[0].insn,
  581. CGEN_INSN_VLIW32_NO_MATCHING_NOP))
  582. as_fatal ("No valid nop.");
  583. /* At this point we know that we have a single 16-bit insn that has
  584. a matching nop. We have to assemble it and put it into the saved
  585. insn and fixup chain arrays. */
  586. if (insn0iscopro)
  587. {
  588. char *errmsg;
  589. mep_insn insn;
  590. /* Move the insn and it's fixups to the second element of the
  591. saved insns array and insert a 16 bit core nope into the
  592. first element. */
  593. insn.insn = mep_cgen_assemble_insn (gas_cgen_cpu_desc, "nop",
  594. &insn.fields, insn.buffer,
  595. &errmsg);
  596. if (!insn.insn)
  597. {
  598. as_bad ("%s", errmsg);
  599. return;
  600. }
  601. /* Move the insn in element 0 to element 1 and insert the
  602. nop into element 0. Move the fixups in element 0 to
  603. element 1 and save the current fixups to element 0.
  604. Really there aren't any fixups at this point because we're
  605. inserting a nop but we might as well be general so that
  606. if there's ever a need to insert a general insn, we'll
  607. have an example. */
  608. saved_insns[1] = saved_insns[0];
  609. saved_insns[0] = insn;
  610. num_insns_saved++;
  611. gas_cgen_swap_fixups (0);
  612. gas_cgen_save_fixups (1);
  613. }
  614. else
  615. {
  616. char * errmsg;
  617. mep_insn insn;
  618. int insn_num = saved_insns[0].insn->base->num;
  619. /* Use 32 bit branches and skip the nop. */
  620. if (insn_num == MEP_INSN_BSR12
  621. || insn_num == MEP_INSN_BEQZ
  622. || insn_num == MEP_INSN_BNEZ)
  623. return;
  624. /* Insert a 16-bit coprocessor nop. Note that at the time */
  625. /* this was done, no 16-bit coprocessor nop was defined. */
  626. insn.insn = mep_cgen_assemble_insn (gas_cgen_cpu_desc, "cpnop16",
  627. &insn.fields, insn.buffer,
  628. &errmsg);
  629. if (!insn.insn)
  630. {
  631. as_bad ("%s", errmsg);
  632. return;
  633. }
  634. /* Now put the insn and fixups into the arrays. */
  635. mep_save_insn (insn);
  636. }
  637. }
  638. }
  639. static void
  640. mep_check_parallel64_scheduling (void)
  641. {
  642. int insn0iscopro, insn1iscopro, insn0length, insn1length;
  643. /* More than two instructions means that someone is referring to an
  644. internally parallel core or an internally parallel coprocessor. */
  645. /* These are not currently supported. */
  646. if (num_insns_saved > 2)
  647. as_fatal ("Internally parallel cores of coprocessors not supported.");
  648. /* If there are no insns saved, that's ok. Just return. This will
  649. happen when mep_process_saved_insns is called when the end of the
  650. source file is reached and there are no insns left to be processed. */
  651. if (num_insns_saved == 0)
  652. return;
  653. /* Check some of the attributes of the first insn. */
  654. insn0iscopro = MEP_INSN_COP_P (saved_insns[0].insn);
  655. insn0length = CGEN_FIELDS_BITSIZE (& saved_insns[0].fields);
  656. if (num_insns_saved == 2)
  657. {
  658. /* Check some of the attributes of the first insn. */
  659. insn1iscopro = MEP_INSN_COP_P (saved_insns[1].insn);
  660. insn1length = CGEN_FIELDS_BITSIZE (& saved_insns[1].fields);
  661. if ((insn0iscopro && !insn1iscopro)
  662. || (insn1iscopro && !insn0iscopro))
  663. {
  664. /* We have one core and one copro insn. If their sizes
  665. add up to 64, then the combination is valid. */
  666. if (insn0length + insn1length == 64)
  667. return;
  668. else
  669. as_bad (_("core and copro insn lengths must total 64 bits."));
  670. }
  671. else
  672. as_bad (_("vliw group must consist of 1 core and 1 copro insn."));
  673. }
  674. else
  675. {
  676. /* If we arrive here, we have one saved instruction. There are a
  677. number of possible cases:
  678. 1. The instruction is a 64 bit coprocessor insn and can be
  679. executed by itself. Valid.
  680. 2. The instruction is a core instruction for which a cop nop
  681. exists. In this case, insert the cop nop into the saved
  682. insn array after the core insn and return. Valid.
  683. 3. The instruction is a coprocessor insn for which a core nop
  684. exists. In this case, move the coprocessor insn to the
  685. second element of the array and put the nop in the first
  686. element then return. Valid.
  687. 4. The instruction is a core or coprocessor instruction for
  688. which there is no matching coprocessor or core nop to use
  689. to form a valid vliw insn combination. In this case, we
  690. we have to abort. */
  691. /* If the insn is 64 bits long, it can run alone. The size check
  692. is done independently of whether the insn is core or copro
  693. in case 64 bit coprocessor insns are added later. */
  694. if (insn0length == 64)
  695. return;
  696. /* Insn is smaller than datapath. If there are no matching
  697. nops for this insn, then terminate assembly. */
  698. if (CGEN_INSN_ATTR_VALUE (saved_insns[0].insn,
  699. CGEN_INSN_VLIW64_NO_MATCHING_NOP))
  700. as_fatal ("No valid nop.");
  701. if (insn0iscopro)
  702. {
  703. char *errmsg;
  704. mep_insn insn;
  705. /* Initialize the insn buffer. */
  706. memset (insn.buffer, 0, sizeof(insn.buffer));
  707. /* We have a coprocessor insn. At this point in time there
  708. are is 32-bit core nop. There is only a 16-bit core
  709. nop. The idea is to allow for a relatively arbitrary
  710. coprocessor to be specified. We aren't looking at
  711. trying to cover future changes in the core at this time
  712. since it is assumed that the core will remain fairly
  713. static. If there ever are 32 or 48 bit core nops added,
  714. they will require entries below. */
  715. if (insn0length == 48)
  716. {
  717. /* Move the insn and fixups to the second element of the
  718. arrays then assemble and insert a 16 bit core nop. */
  719. insn.insn = mep_cgen_assemble_insn (gas_cgen_cpu_desc, "nop",
  720. & insn.fields, insn.buffer,
  721. & errmsg);
  722. }
  723. else
  724. {
  725. /* If this is reached, then we have a single coprocessor
  726. insn that is not 48 bits long, but for which the assembler
  727. thinks there is a matching core nop. If a 32-bit core
  728. nop has been added, then make the necessary changes and
  729. handle its assembly and insertion here. Otherwise,
  730. go figure out why either:
  731. 1. The assembler thinks that there is a 32-bit core nop
  732. to match a 32-bit coprocessor insn, or
  733. 2. The assembler thinks that there is a 48-bit core nop
  734. to match a 16-bit coprocessor insn. */
  735. as_fatal ("Assembler expects a non-existent core nop.");
  736. }
  737. if (!insn.insn)
  738. {
  739. as_bad ("%s", errmsg);
  740. return;
  741. }
  742. /* Move the insn in element 0 to element 1 and insert the
  743. nop into element 0. Move the fixups in element 0 to
  744. element 1 and save the current fixups to element 0.
  745. Really there aren't any fixups at this point because we're
  746. inserting a nop but we might as well be general so that
  747. if there's ever a need to insert a general insn, we'll
  748. have an example. */
  749. saved_insns[1] = saved_insns[0];
  750. saved_insns[0] = insn;
  751. num_insns_saved++;
  752. gas_cgen_swap_fixups(0);
  753. gas_cgen_save_fixups(1);
  754. }
  755. else
  756. {
  757. char * errmsg;
  758. mep_insn insn;
  759. /* Initialize the insn buffer */
  760. memset (insn.buffer, 0, sizeof(insn.buffer));
  761. /* We have a core insn. We have to handle all possible nop
  762. lengths. If a coprocessor doesn't have a nop of a certain
  763. length but there exists core insns that when combined with
  764. a nop of that length would fill the datapath, those core
  765. insns will be flagged with the VLIW_NO_CORRESPONDING_NOP
  766. attribute. That will ensure that when used in a way that
  767. requires a nop to be inserted, assembly will terminate
  768. before reaching this section of code. This guarantees
  769. that cases below which would result in the attempted
  770. insertion of nop that doesn't exist will never be entered. */
  771. if (insn0length == 16)
  772. {
  773. /* Insert 48 bit coprocessor nop. */
  774. /* Assemble it and put it into the arrays. */
  775. insn.insn = mep_cgen_assemble_insn (gas_cgen_cpu_desc, "cpnop48",
  776. &insn.fields, insn.buffer,
  777. &errmsg);
  778. }
  779. else if (insn0length == 32)
  780. {
  781. /* Insert 32 bit coprocessor nop. */
  782. insn.insn = mep_cgen_assemble_insn (gas_cgen_cpu_desc, "cpnop32",
  783. &insn.fields, insn.buffer,
  784. &errmsg);
  785. }
  786. else if (insn0length == 48)
  787. {
  788. /* Insert 16 bit coprocessor nop. */
  789. insn.insn = mep_cgen_assemble_insn (gas_cgen_cpu_desc, "cpnop16",
  790. &insn.fields, insn.buffer,
  791. &errmsg);
  792. }
  793. else
  794. /* Core insn has an invalid length. Something has gone wrong. */
  795. as_fatal ("Core insn has invalid length! Something is wrong!");
  796. if (!insn.insn)
  797. {
  798. as_bad ("%s", errmsg);
  799. return;
  800. }
  801. /* Now put the insn and fixups into the arrays. */
  802. mep_save_insn (insn);
  803. }
  804. }
  805. }
  806. #ifdef MEP_IVC2_SUPPORTED
  807. /* IVC2 packing is different than other VLIW coprocessors. Many of
  808. the COP insns can be placed in any of three different types of
  809. slots, and each bundle can hold up to three insns - zero or one
  810. core insns and one or two IVC2 insns. The insns in CGEN are tagged
  811. with which slots they're allowed in, and we have to decide based on
  812. that whether or not the user had given us a possible bundling. */
  813. static int
  814. slot_ok (int idx, int slot)
  815. {
  816. const CGEN_INSN *insn = saved_insns[idx].insn;
  817. return CGEN_ATTR_CGEN_INSN_SLOTS_VALUE (CGEN_INSN_ATTRS (insn)) & (1 << slot);
  818. }
  819. static void
  820. mep_check_ivc2_scheduling (void)
  821. {
  822. /* VLIW modes:
  823. V1 [-----core-----][--------p0s-------][------------p1------------]
  824. V2 [-------------core-------------]xxxx[------------p1------------]
  825. V3 1111[--p0--]0111[--------p0--------][------------p1------------]
  826. */
  827. int slots[5]; /* Indexed off the SLOTS_ATTR enum. */
  828. int corelength, realcorelength;
  829. int i;
  830. bfd_byte temp[4];
  831. bfd_byte *f;
  832. int e = target_big_endian ? 0 : 1;
  833. /* If there are no insns saved, that's ok. Just return. This will
  834. happen when mep_process_saved_insns is called when the end of the
  835. source file is reached and there are no insns left to be processed. */
  836. if (num_insns_saved == 0)
  837. return;
  838. for (i=0; i<5; i++)
  839. slots[i] = -1;
  840. if (slot_ok (0, SLOTS_CORE))
  841. {
  842. slots[SLOTS_CORE] = 0;
  843. realcorelength = corelength = CGEN_FIELDS_BITSIZE (& saved_insns[0].fields);
  844. /* If we encounter one of these, it may get relaxed later into a
  845. longer instruction. We can't just push the other opcodes
  846. away, the bigger insn has to fit into the existing slot. So,
  847. we make room for the relaxed instruction here. */
  848. if (saved_insns[0].insn->base->num == MEP_INSN_BSR12
  849. || saved_insns[0].insn->base->num == MEP_INSN_BRA)
  850. corelength = 32;
  851. }
  852. else
  853. realcorelength = corelength = 0;
  854. if (corelength == 16)
  855. {
  856. /* V1 mode: we need a P0S slot and a P1 slot. */
  857. switch (num_insns_saved)
  858. {
  859. case 1:
  860. /* No other insns, fill with NOPs. */
  861. break;
  862. case 2:
  863. if (slot_ok (1, SLOTS_P1))
  864. slots[SLOTS_P1] = 1;
  865. else if (slot_ok (1, SLOTS_P0S))
  866. slots[SLOTS_P0S] = 1;
  867. else
  868. as_bad (_("cannot pack %s with a 16-bit insn"),
  869. CGEN_INSN_NAME (saved_insns[1].insn));
  870. break;
  871. case 3:
  872. if (slot_ok (1, SLOTS_P0S)
  873. && slot_ok (2, SLOTS_P1))
  874. {
  875. slots[SLOTS_P0S] = 1;
  876. slots[SLOTS_P1] = 2;
  877. }
  878. else if (slot_ok (1, SLOTS_P1)
  879. && slot_ok (2, SLOTS_P0S))
  880. {
  881. slots[SLOTS_P1] = 1;
  882. slots[SLOTS_P0S] = 2;
  883. }
  884. else
  885. as_bad (_("cannot pack %s and %s together with a 16-bit insn"),
  886. CGEN_INSN_NAME (saved_insns[1].insn),
  887. CGEN_INSN_NAME (saved_insns[2].insn));
  888. break;
  889. default:
  890. as_bad (_("too many IVC2 insns to pack with a 16-bit core insn"));
  891. break;
  892. }
  893. }
  894. else if (corelength == 32)
  895. {
  896. /* V2 mode: we need a P1 slot. */
  897. switch (num_insns_saved)
  898. {
  899. case 1:
  900. /* No other insns, fill with NOPs. */
  901. break;
  902. case 2:
  903. /* The other insn must allow P1. */
  904. if (!slot_ok (1, SLOTS_P1))
  905. as_bad (_("cannot pack %s into slot P1"),
  906. CGEN_INSN_NAME (saved_insns[1].insn));
  907. else
  908. slots[SLOTS_P1] = 1;
  909. break;
  910. default:
  911. as_bad (_("too many IVC2 insns to pack with a 32-bit core insn"));
  912. break;
  913. }
  914. }
  915. else if (corelength == 0)
  916. {
  917. /* V3 mode: we need a P0 slot and a P1 slot, or a P0S+P1 with a
  918. core NOP. */
  919. switch (num_insns_saved)
  920. {
  921. case 1:
  922. if (slot_ok (0, SLOTS_P0))
  923. slots[SLOTS_P0] = 0;
  924. else if (slot_ok (0, SLOTS_P1))
  925. slots[SLOTS_P1] = 0;
  926. else if (slot_ok (0, SLOTS_P0S))
  927. slots[SLOTS_P0S] = 0;
  928. else
  929. as_bad (_("unable to pack %s by itself?"),
  930. CGEN_INSN_NAME (saved_insns[0].insn));
  931. break;
  932. case 2:
  933. if (slot_ok (0, SLOTS_P0)
  934. && slot_ok (1, SLOTS_P1))
  935. {
  936. slots[SLOTS_P0] = 0;
  937. slots[SLOTS_P1] = 1;
  938. }
  939. else if (slot_ok (0, SLOTS_P1)
  940. && slot_ok (1, SLOTS_P0))
  941. {
  942. slots[SLOTS_P1] = 0;
  943. slots[SLOTS_P0] = 1;
  944. }
  945. else if (slot_ok (0, SLOTS_P0S)
  946. && slot_ok (1, SLOTS_P1))
  947. {
  948. slots[SLOTS_P0S] = 0;
  949. slots[SLOTS_P1] = 1;
  950. }
  951. else if (slot_ok (0, SLOTS_P1)
  952. && slot_ok (1, SLOTS_P0S))
  953. {
  954. slots[SLOTS_P1] = 0;
  955. slots[SLOTS_P0S] = 1;
  956. }
  957. else
  958. as_bad (_("cannot pack %s and %s together"),
  959. CGEN_INSN_NAME (saved_insns[0].insn),
  960. CGEN_INSN_NAME (saved_insns[1].insn));
  961. break;
  962. default:
  963. as_bad (_("too many IVC2 insns to pack together"));
  964. break;
  965. }
  966. }
  967. /* The core insn needs to be done normally so that fixups,
  968. relaxation, etc are done. Other IVC2 insns need only be resolved
  969. to bit patterns; there are no relocations for them. */
  970. if (slots[SLOTS_CORE] != -1)
  971. {
  972. gas_cgen_restore_fixups (0);
  973. gas_cgen_finish_insn (saved_insns[0].insn, saved_insns[0].buffer,
  974. CGEN_FIELDS_BITSIZE (& saved_insns[0].fields),
  975. 1, NULL);
  976. }
  977. /* Allocate whatever bytes remain in our insn word. Adjust the
  978. pointer to point (as if it were) to the beginning of the whole
  979. word, so that we don't have to adjust for it elsewhere. */
  980. f = (bfd_byte *) frag_more (8 - realcorelength / 8);
  981. /* Unused slots are filled with NOPs, which happen to be all zeros. */
  982. memset (f, 0, 8 - realcorelength / 8);
  983. f -= realcorelength / 8;
  984. for (i=1; i<5; i++)
  985. {
  986. mep_insn *m;
  987. if (slots[i] == -1)
  988. continue;
  989. m = & saved_insns[slots[i]];
  990. #if CGEN_INT_INSN_P
  991. cgen_put_insn_value (gas_cgen_cpu_desc, (unsigned char *) temp, 32,
  992. m->buffer[0], gas_cgen_cpu_desc->insn_endian);
  993. #else
  994. memcpy (temp, m->buffer, byte_len);
  995. #endif
  996. switch (i)
  997. {
  998. case SLOTS_P0S:
  999. f[2^e] = temp[1^e];
  1000. f[3^e] = temp[2^e];
  1001. f[4^e] |= temp[3^e] & 0xf0;
  1002. break;
  1003. case SLOTS_P0:
  1004. f[0^e] = 0xf0 | temp[0^e] >> 4;
  1005. f[1^e] = temp[0^e] << 4 | 0x07;
  1006. f[2^e] = temp[1^e];
  1007. f[3^e] = temp[2^e];
  1008. f[4^e] |= temp[3^e] & 0xf0;
  1009. break;
  1010. case SLOTS_P1:
  1011. f[4^e] |= temp[0^e] >> 4;
  1012. f[5^e] = temp[0^e] << 4 | temp[1^e] >> 4;
  1013. f[6^e] = temp[1^e] << 4 | temp[2^e] >> 4;
  1014. f[7^e] = temp[2^e] << 4 | temp[3^e] >> 4;
  1015. break;
  1016. default:
  1017. break;
  1018. }
  1019. }
  1020. }
  1021. #endif /* MEP_IVC2_SUPPORTED */
  1022. /* The scheduling functions are just filters for invalid combinations.
  1023. If there is a violation, they terminate assembly. Otherwise they
  1024. just fall through. Successful combinations cause no side effects
  1025. other than valid nop insertion. */
  1026. static void
  1027. mep_check_parallel_scheduling (void)
  1028. {
  1029. /* This is where we will eventually read the config information
  1030. and choose which scheduling checking function to call. */
  1031. #ifdef MEP_IVC2_SUPPORTED
  1032. if (mep_cop == EF_MEP_COP_IVC2)
  1033. mep_check_ivc2_scheduling ();
  1034. else
  1035. #endif /* MEP_IVC2_SUPPORTED */
  1036. if (MEP_VLIW64)
  1037. mep_check_parallel64_scheduling ();
  1038. else
  1039. mep_check_parallel32_scheduling ();
  1040. }
  1041. static void
  1042. mep_process_saved_insns (void)
  1043. {
  1044. int i;
  1045. gas_cgen_save_fixups (MAX_SAVED_FIXUP_CHAINS - 1);
  1046. /* We have to check for valid scheduling here. */
  1047. mep_check_parallel_scheduling ();
  1048. /* IVC2 has to pack instructions in a funny way, so it does it
  1049. itself. */
  1050. if (mep_cop != EF_MEP_COP_IVC2)
  1051. {
  1052. /* If the last call didn't cause assembly to terminate, we have
  1053. a valid vliw insn/insn pair saved. Restore this instructions'
  1054. fixups and process the insns. */
  1055. for (i = 0;i<num_insns_saved;i++)
  1056. {
  1057. gas_cgen_restore_fixups (i);
  1058. gas_cgen_finish_insn (saved_insns[i].insn, saved_insns[i].buffer,
  1059. CGEN_FIELDS_BITSIZE (& saved_insns[i].fields),
  1060. 1, NULL);
  1061. }
  1062. }
  1063. gas_cgen_restore_fixups (MAX_SAVED_FIXUP_CHAINS - 1);
  1064. /* Clear the fixups and reset the number insn saved to 0. */
  1065. gas_cgen_initialize_saved_fixups_array ();
  1066. num_insns_saved = 0;
  1067. listing_prev_line ();
  1068. }
  1069. void
  1070. md_assemble (char * str)
  1071. {
  1072. static CGEN_BITSET* isas = NULL;
  1073. char * errmsg;
  1074. /* Initialize GAS's cgen interface for a new instruction. */
  1075. gas_cgen_init_parse ();
  1076. /* There are two possible modes: core and vliw. We have to assemble
  1077. differently for each.
  1078. Core Mode: We assemble normally. All instructions are on a
  1079. single line and are made up of one mnemonic and one
  1080. set of operands.
  1081. VLIW Mode: Vliw combinations are indicated as follows:
  1082. core insn
  1083. + copro insn
  1084. We want to handle the general case where more than
  1085. one instruction can be preceded by a +. This will
  1086. happen later if we add support for internally parallel
  1087. coprocessors. We'll make the parsing nice and general
  1088. so that it can handle an arbitrary number of insns
  1089. with leading +'s. The actual checking for valid
  1090. combinations is done elsewhere. */
  1091. /* Initialize the isa to refer to the core. */
  1092. if (isas == NULL)
  1093. isas = cgen_bitset_copy (& MEP_CORE_ISA);
  1094. else
  1095. {
  1096. cgen_bitset_clear (isas);
  1097. cgen_bitset_union (isas, & MEP_CORE_ISA, isas);
  1098. }
  1099. gas_cgen_cpu_desc->isas = isas;
  1100. if (mode == VLIW)
  1101. {
  1102. /* VLIW mode. */
  1103. int thisInsnIsCopro = 0;
  1104. mep_insn insn;
  1105. int i;
  1106. /* Initialize the insn buffer */
  1107. if (! CGEN_INT_INSN_P)
  1108. for (i=0; i < CGEN_MAX_INSN_SIZE; i++)
  1109. insn.buffer[i]='\0';
  1110. /* IVC2 has two sets of coprocessor opcodes, one for CORE mode
  1111. and one for VLIW mode. They have the same names. To specify
  1112. which one we want, we use the COP isas - the 32 bit ISA is
  1113. for the core instructions (which are always 32 bits), and the
  1114. other ISAs are for the VLIW ones (which always pack into 64
  1115. bit insns). We use other attributes to determine slotting
  1116. later. */
  1117. if (mep_cop == EF_MEP_COP_IVC2)
  1118. {
  1119. cgen_bitset_union (isas, & MEP_COP16_ISA, isas);
  1120. cgen_bitset_union (isas, & MEP_COP48_ISA, isas);
  1121. cgen_bitset_union (isas, & MEP_COP64_ISA, isas);
  1122. }
  1123. else
  1124. {
  1125. /* Can't tell core / copro insns apart at parse time! */
  1126. cgen_bitset_union (isas, & MEP_COP_ISA, isas);
  1127. }
  1128. /* Assemble the insn so we can examine its attributes. */
  1129. insn.insn = mep_cgen_assemble_insn (gas_cgen_cpu_desc, str,
  1130. &insn.fields, insn.buffer,
  1131. &errmsg);
  1132. if (!insn.insn)
  1133. {
  1134. as_bad ("%s", errmsg);
  1135. return;
  1136. }
  1137. mep_check_for_disabled_registers (&insn);
  1138. /* Check to see if it's a coprocessor instruction. */
  1139. thisInsnIsCopro = MEP_INSN_COP_P (insn.insn);
  1140. if (!thisInsnIsCopro)
  1141. {
  1142. insn.insn = mep_cgen_assemble_cop_insn (gas_cgen_cpu_desc, str,
  1143. &insn.fields, insn.buffer,
  1144. insn.insn);
  1145. thisInsnIsCopro = MEP_INSN_COP_P (insn.insn);
  1146. mep_check_for_disabled_registers (&insn);
  1147. }
  1148. if (pluspresent)
  1149. {
  1150. /* A plus was present. */
  1151. /* Check for a + with a core insn and abort if found. */
  1152. if (!thisInsnIsCopro)
  1153. {
  1154. as_fatal("A core insn cannot be preceded by a +.\n");
  1155. return;
  1156. }
  1157. if (num_insns_saved > 0)
  1158. {
  1159. /* There are insns in the queue. Add this one. */
  1160. mep_save_insn (insn);
  1161. }
  1162. else
  1163. {
  1164. /* There are no insns in the queue and a plus is present.
  1165. This is a syntax error. Let's not tolerate this.
  1166. We can relax this later if necessary. */
  1167. as_bad (_("Invalid use of parallelization operator."));
  1168. return;
  1169. }
  1170. }
  1171. else
  1172. {
  1173. /* No plus was present. */
  1174. if (num_insns_saved > 0)
  1175. {
  1176. /* There are insns saved and we came across an insn without a
  1177. leading +. That's the signal to process the saved insns
  1178. before proceeding then treat the current insn as the first
  1179. in a new vliw group. */
  1180. mep_process_saved_insns ();
  1181. num_insns_saved = 0;
  1182. /* mep_save_insn (insn); */
  1183. }
  1184. mep_save_insn (insn);
  1185. #if 0
  1186. else
  1187. {
  1188. /* Core Insn. Add it to the beginning of the queue. */
  1189. mep_save_insn (insn);
  1190. /* gas_cgen_save_fixups(num_insns_saved); */
  1191. }
  1192. #endif
  1193. }
  1194. pluspresent = 0;
  1195. }
  1196. else
  1197. {
  1198. /* Core mode. */
  1199. /* Only single instructions are assembled in core mode. */
  1200. mep_insn insn;
  1201. /* See comment in the VLIW clause above about this. */
  1202. if (mep_cop & EF_MEP_COP_IVC2)
  1203. cgen_bitset_union (isas, & MEP_COP32_ISA, isas);
  1204. /* If a leading '+' was present, issue an error.
  1205. That's not allowed in core mode. */
  1206. if (pluspresent)
  1207. {
  1208. as_bad (_("Leading plus sign not allowed in core mode"));
  1209. return;
  1210. }
  1211. insn.insn = mep_cgen_assemble_insn
  1212. (gas_cgen_cpu_desc, str, & insn.fields, insn.buffer, & errmsg);
  1213. if (!insn.insn)
  1214. {
  1215. as_bad ("%s", errmsg);
  1216. return;
  1217. }
  1218. gas_cgen_finish_insn (insn.insn, insn.buffer,
  1219. CGEN_FIELDS_BITSIZE (& insn.fields), 1, NULL);
  1220. mep_check_for_disabled_registers (&insn);
  1221. }
  1222. }
  1223. valueT
  1224. md_section_align (segT segment, valueT size)
  1225. {
  1226. int align = bfd_section_alignment (segment);
  1227. return ((size + (1 << align) - 1) & -(1 << align));
  1228. }
  1229. symbolS *
  1230. md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
  1231. {
  1232. return 0;
  1233. }
  1234. /* Interface to relax_segment. */
  1235. const relax_typeS md_relax_table[] =
  1236. {
  1237. /* The fields are:
  1238. 1) most positive reach of this state,
  1239. 2) most negative reach of this state,
  1240. 3) how many bytes this mode will have in the variable part of the frag
  1241. 4) which index into the table to try if we can't fit into this one. */
  1242. /* Note that we use "beq" because "jmp" has a peculiarity - it cannot
  1243. jump to addresses with any bits 27..24 set. So, we use beq as a
  1244. 17-bit pc-relative branch to avoid using jmp, just in case. */
  1245. /* 0 */ { 0, 0, 0, 0 }, /* unused */
  1246. /* 1 */ { 0, 0, 0, 0 }, /* marker for "don't know yet" */
  1247. /* 2 */ { 2047, -2048, 0, 3 }, /* bsr12 */
  1248. /* 3 */ { 0, 0, 2, 0 }, /* bsr16 */
  1249. /* 4 */ { 2047, -2048, 0, 5 }, /* bra */
  1250. /* 5 */ { 65535, -65536, 2, 6 }, /* beq $0,$0 */
  1251. /* 6 */ { 0, 0, 2, 0 }, /* jmp24 */
  1252. /* 7 */ { 65535, -65536, 0, 8 }, /* beqi */
  1253. /* 8 */ { 0, 0, 4, 0 }, /* bnei/jmp */
  1254. /* 9 */ { 127, -128, 0, 10 }, /* beqz */
  1255. /* 10 */ { 65535, -65536, 2, 11 }, /* beqi */
  1256. /* 11 */ { 0, 0, 4, 0 }, /* bnei/jmp */
  1257. /* 12 */ { 65535, -65536, 0, 13 }, /* bnei */
  1258. /* 13 */ { 0, 0, 4, 0 }, /* beqi/jmp */
  1259. /* 14 */ { 127, -128, 0, 15 }, /* bnez */
  1260. /* 15 */ { 65535, -65536, 2, 16 }, /* bnei */
  1261. /* 16 */ { 0, 0, 4, 0 }, /* beqi/jmp */
  1262. /* 17 */ { 65535, -65536, 0, 13 }, /* bgei */
  1263. /* 18 */ { 0, 0, 4, 0 },
  1264. /* 19 */ { 65535, -65536, 0, 13 }, /* blti */
  1265. /* 20 */ { 0, 0, 4, 0 },
  1266. /* 19 */ { 65535, -65536, 0, 13 }, /* bcpeq */
  1267. /* 20 */ { 0, 0, 4, 0 },
  1268. /* 19 */ { 65535, -65536, 0, 13 }, /* bcpne */
  1269. /* 20 */ { 0, 0, 4, 0 },
  1270. /* 19 */ { 65535, -65536, 0, 13 }, /* bcpat */
  1271. /* 20 */ { 0, 0, 4, 0 },
  1272. /* 19 */ { 65535, -65536, 0, 13 }, /* bcpaf */
  1273. /* 20 */ { 0, 0, 4, 0 }
  1274. };
  1275. /* Pseudo-values for 64 bit "insns" which are combinations of two 32
  1276. bit insns. */
  1277. typedef enum {
  1278. MEP_PSEUDO64_NONE,
  1279. MEP_PSEUDO64_16BITCC,
  1280. MEP_PSEUDO64_32BITCC,
  1281. } MepPseudo64Values;
  1282. static struct {
  1283. int insn;
  1284. int growth;
  1285. int insn_for_extern;
  1286. } subtype_mappings[] = {
  1287. { 0, 0, 0 },
  1288. { 0, 0, 0 },
  1289. { MEP_INSN_BSR12, 0, MEP_INSN_BSR24 },
  1290. { MEP_INSN_BSR24, 2, MEP_INSN_BSR24 },
  1291. { MEP_INSN_BRA, 0, MEP_INSN_BRA },
  1292. { MEP_INSN_BEQ, 2, MEP_INSN_BEQ },
  1293. { MEP_INSN_JMP, 2, MEP_INSN_JMP },
  1294. { MEP_INSN_BEQI, 0, MEP_INSN_BEQI },
  1295. { -1, 4, MEP_PSEUDO64_32BITCC },
  1296. { MEP_INSN_BEQZ, 0, MEP_INSN_BEQZ },
  1297. { MEP_INSN_BEQI, 2, MEP_INSN_BEQI },
  1298. { -1, 4, MEP_PSEUDO64_16BITCC },
  1299. { MEP_INSN_BNEI, 0, MEP_INSN_BNEI },
  1300. { -1, 4, MEP_PSEUDO64_32BITCC },
  1301. { MEP_INSN_BNEZ, 0, MEP_INSN_BNEZ },
  1302. { MEP_INSN_BNEI, 2, MEP_INSN_BNEI },
  1303. { -1, 4, MEP_PSEUDO64_16BITCC },
  1304. { MEP_INSN_BGEI, 0, MEP_INSN_BGEI },
  1305. { -1, 4, MEP_PSEUDO64_32BITCC },
  1306. { MEP_INSN_BLTI, 0, MEP_INSN_BLTI },
  1307. { -1, 4, MEP_PSEUDO64_32BITCC },
  1308. { MEP_INSN_BCPEQ, 0, MEP_INSN_BCPEQ },
  1309. { -1, 4, MEP_PSEUDO64_32BITCC },
  1310. { MEP_INSN_BCPNE, 0, MEP_INSN_BCPNE },
  1311. { -1, 4, MEP_PSEUDO64_32BITCC },
  1312. { MEP_INSN_BCPAT, 0, MEP_INSN_BCPAT },
  1313. { -1, 4, MEP_PSEUDO64_32BITCC },
  1314. { MEP_INSN_BCPAF, 0, MEP_INSN_BCPAF },
  1315. { -1, 4, MEP_PSEUDO64_32BITCC }
  1316. };
  1317. #define NUM_MAPPINGS (sizeof (subtype_mappings) / sizeof (subtype_mappings[0]))
  1318. void
  1319. mep_prepare_relax_scan (fragS *fragP, offsetT *aim, relax_substateT this_state)
  1320. {
  1321. symbolS *symbolP = fragP->fr_symbol;
  1322. if (symbolP && !S_IS_DEFINED (symbolP))
  1323. *aim = 0;
  1324. /* Adjust for MeP pcrel not being relative to the next opcode. */
  1325. *aim += 2 + md_relax_table[this_state].rlx_length;
  1326. }
  1327. static int
  1328. insn_to_subtype (int insn)
  1329. {
  1330. unsigned int i;
  1331. for (i=0; i<NUM_MAPPINGS; i++)
  1332. if (insn == subtype_mappings[i].insn)
  1333. return i;
  1334. abort ();
  1335. }
  1336. /* Return an initial guess of the length by which a fragment must grow
  1337. to hold a branch to reach its destination. Also updates fr_type
  1338. and fr_subtype as necessary.
  1339. Called just before doing relaxation. Any symbol that is now
  1340. undefined will not become defined. The guess for fr_var is
  1341. ACTUALLY the growth beyond fr_fix. Whatever we do to grow fr_fix
  1342. or fr_var contributes to our returned value. Although it may not
  1343. be explicit in the frag, pretend fr_var starts with a 0 value. */
  1344. int
  1345. md_estimate_size_before_relax (fragS * fragP, segT segment)
  1346. {
  1347. if (fragP->fr_subtype == 1)
  1348. fragP->fr_subtype = insn_to_subtype (fragP->fr_cgen.insn->base->num);
  1349. if (S_GET_SEGMENT (fragP->fr_symbol) != segment
  1350. || S_IS_WEAK (fragP->fr_symbol)
  1351. #ifdef MEP_IVC2_SUPPORTED
  1352. || (mep_cop == EF_MEP_COP_IVC2
  1353. && bfd_section_flags (segment) & SEC_MEP_VLIW)
  1354. #endif /* MEP_IVC2_SUPPORTED */
  1355. )
  1356. {
  1357. int new_insn;
  1358. new_insn = subtype_mappings[fragP->fr_subtype].insn_for_extern;
  1359. fragP->fr_subtype = insn_to_subtype (new_insn);
  1360. }
  1361. if (MEP_VLIW && ! MEP_VLIW64
  1362. && (bfd_section_flags (segment) & SEC_MEP_VLIW))
  1363. {
  1364. /* Use 32 bit branches for vliw32 so the vliw word is not split. */
  1365. switch (fragP->fr_cgen.insn->base->num)
  1366. {
  1367. case MEP_INSN_BSR12:
  1368. fragP->fr_subtype = insn_to_subtype
  1369. (subtype_mappings[fragP->fr_subtype].insn_for_extern);
  1370. break;
  1371. case MEP_INSN_BEQZ:
  1372. fragP->fr_subtype ++;
  1373. break;
  1374. case MEP_INSN_BNEZ:
  1375. fragP->fr_subtype ++;
  1376. break;
  1377. }
  1378. }
  1379. if (fragP->fr_cgen.insn->base
  1380. && fragP->fr_cgen.insn->base->num
  1381. != subtype_mappings[fragP->fr_subtype].insn)
  1382. {
  1383. int new_insn= subtype_mappings[fragP->fr_subtype].insn;
  1384. if (new_insn != -1)
  1385. {
  1386. fragP->fr_cgen.insn = (fragP->fr_cgen.insn
  1387. - fragP->fr_cgen.insn->base->num
  1388. + new_insn);
  1389. }
  1390. }
  1391. #ifdef MEP_IVC2_SUPPORTED
  1392. if (mep_cop == EF_MEP_COP_IVC2
  1393. && bfd_section_flags (segment) & SEC_MEP_VLIW)
  1394. return 0;
  1395. #endif /* MEP_IVC2_SUPPORTED */
  1396. return subtype_mappings[fragP->fr_subtype].growth;
  1397. }
  1398. /* VLIW does relaxing, but not growth. */
  1399. long
  1400. mep_relax_frag (segT segment, fragS *fragP, long stretch)
  1401. {
  1402. long rv = relax_frag (segment, fragP, stretch);
  1403. #ifdef MEP_IVC2_SUPPORTED
  1404. if (mep_cop == EF_MEP_COP_IVC2
  1405. && bfd_section_flags (segment) & SEC_MEP_VLIW)
  1406. return 0;
  1407. #endif
  1408. return rv;
  1409. }
  1410. /* *fragP has been relaxed to its final size, and now needs to have
  1411. the bytes inside it modified to conform to the new size.
  1412. Called after relaxation is finished.
  1413. fragP->fr_type == rs_machine_dependent.
  1414. fragP->fr_subtype is the subtype of what the address relaxed to. */
  1415. static int
  1416. target_address_for (fragS *frag)
  1417. {
  1418. int rv = frag->fr_offset;
  1419. symbolS *sym = frag->fr_symbol;
  1420. if (sym)
  1421. rv += S_GET_VALUE (sym);
  1422. return rv;
  1423. }
  1424. void
  1425. md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED,
  1426. segT seg ATTRIBUTE_UNUSED,
  1427. fragS *fragP)
  1428. {
  1429. uint32_t addend, rn, bit = 0;
  1430. int operand;
  1431. int where = fragP->fr_opcode - fragP->fr_literal;
  1432. int e = target_big_endian ? 0 : 1;
  1433. int core_mode;
  1434. #ifdef MEP_IVC2_SUPPORTED
  1435. if (bfd_section_flags (seg) & SEC_MEP_VLIW
  1436. && mep_cop == EF_MEP_COP_IVC2)
  1437. core_mode = 0;
  1438. else
  1439. #endif /* MEP_IVC2_SUPPORTED */
  1440. core_mode = 1;
  1441. addend = target_address_for (fragP) - (fragP->fr_address + where);
  1442. if (subtype_mappings[fragP->fr_subtype].insn == -1)
  1443. {
  1444. if (core_mode)
  1445. fragP->fr_fix += subtype_mappings[fragP->fr_subtype].growth;
  1446. switch (subtype_mappings[fragP->fr_subtype].insn_for_extern)
  1447. {
  1448. case MEP_PSEUDO64_16BITCC:
  1449. fragP->fr_opcode[1^e] = ((fragP->fr_opcode[1^e] & 1) ^ 1) | 0x06;
  1450. fragP->fr_opcode[2^e] = 0xd8;
  1451. fragP->fr_opcode[3^e] = 0x08;
  1452. fragP->fr_opcode[4^e] = 0;
  1453. fragP->fr_opcode[5^e] = 0;
  1454. where += 2;
  1455. break;
  1456. case MEP_PSEUDO64_32BITCC:
  1457. if (fragP->fr_opcode[0^e] & 0x10)
  1458. fragP->fr_opcode[1^e] ^= 0x01;
  1459. else
  1460. fragP->fr_opcode[1^e] ^= 0x04;
  1461. fragP->fr_opcode[2^e] = 0;
  1462. fragP->fr_opcode[3^e] = 4;
  1463. fragP->fr_opcode[4^e] = 0xd8;
  1464. fragP->fr_opcode[5^e] = 0x08;
  1465. fragP->fr_opcode[6^e] = 0;
  1466. fragP->fr_opcode[7^e] = 0;
  1467. where += 4;
  1468. break;
  1469. default:
  1470. abort ();
  1471. }
  1472. fragP->fr_cgen.insn = (fragP->fr_cgen.insn
  1473. - fragP->fr_cgen.insn->base->num
  1474. + MEP_INSN_JMP);
  1475. operand = MEP_OPERAND_PCABS24A2;
  1476. }
  1477. else
  1478. switch (fragP->fr_cgen.insn->base->num)
  1479. {
  1480. case MEP_INSN_BSR12:
  1481. fragP->fr_opcode[0^e] = 0xb0 | ((addend >> 8) & 0x0f);
  1482. fragP->fr_opcode[1^e] = 0x01 | (addend & 0xfe);
  1483. operand = MEP_OPERAND_PCREL12A2;
  1484. break;
  1485. case MEP_INSN_BSR24:
  1486. if (core_mode)
  1487. fragP->fr_fix += 2;
  1488. fragP->fr_opcode[0^e] = 0xd8 | ((addend >> 5) & 0x07);
  1489. fragP->fr_opcode[1^e] = 0x09 | ((addend << 3) & 0xf0);
  1490. fragP->fr_opcode[2^e] = 0x00 | ((addend >>16) & 0xff);
  1491. fragP->fr_opcode[3^e] = 0x00 | ((addend >> 8) & 0xff);
  1492. operand = MEP_OPERAND_PCREL24A2;
  1493. break;
  1494. case MEP_INSN_BRA:
  1495. fragP->fr_opcode[0^e] = 0xb0 | ((addend >> 8) & 0x0f);
  1496. fragP->fr_opcode[1^e] = 0x00 | (addend & 0xfe);
  1497. operand = MEP_OPERAND_PCREL12A2;
  1498. break;
  1499. case MEP_INSN_BEQ:
  1500. /* The default relax_frag doesn't change the state if there is no
  1501. growth, so we must manually handle converting out-of-range BEQ
  1502. instructions to JMP. */
  1503. if (addend + 65536 < 131071)
  1504. {
  1505. if (core_mode)
  1506. fragP->fr_fix += 2;
  1507. fragP->fr_opcode[0^e] = 0xe0;
  1508. fragP->fr_opcode[1^e] = 0x01;
  1509. fragP->fr_opcode[2^e] = 0x00 | ((addend >> 9) & 0xff);
  1510. fragP->fr_opcode[3^e] = 0x00 | ((addend >> 1) & 0xff);
  1511. operand = MEP_OPERAND_PCREL17A2;
  1512. break;
  1513. }
  1514. /* Fall through. */
  1515. case MEP_INSN_JMP:
  1516. addend = target_address_for (fragP);
  1517. if (core_mode)
  1518. fragP->fr_fix += 2;
  1519. fragP->fr_opcode[0^e] = 0xd8 | ((addend >> 5) & 0x07);
  1520. fragP->fr_opcode[1^e] = 0x08 | ((addend << 3) & 0xf0);
  1521. fragP->fr_opcode[2^e] = 0x00 | ((addend >>16) & 0xff);
  1522. fragP->fr_opcode[3^e] = 0x00 | ((addend >> 8) & 0xff);
  1523. operand = MEP_OPERAND_PCABS24A2;
  1524. break;
  1525. case MEP_INSN_BNEZ:
  1526. bit = 1;
  1527. /* Fall through. */
  1528. case MEP_INSN_BEQZ:
  1529. fragP->fr_opcode[1^e] = bit | (addend & 0xfe);
  1530. operand = MEP_OPERAND_PCREL8A2;
  1531. break;
  1532. case MEP_INSN_BNEI:
  1533. bit = 4;
  1534. /* Fall through. */
  1535. case MEP_INSN_BEQI:
  1536. if (subtype_mappings[fragP->fr_subtype].growth)
  1537. {
  1538. if (core_mode)
  1539. fragP->fr_fix += subtype_mappings[fragP->fr_subtype].growth;
  1540. rn = fragP->fr_opcode[0^e] & 0x0f;
  1541. fragP->fr_opcode[0^e] = 0xe0 | rn;
  1542. fragP->fr_opcode[1^e] = bit;
  1543. }
  1544. fragP->fr_opcode[2^e] = 0x00 | ((addend >> 9) & 0xff);
  1545. fragP->fr_opcode[3^e] = 0x00 | ((addend >> 1) & 0xff);
  1546. operand = MEP_OPERAND_PCREL17A2;
  1547. break;
  1548. case MEP_INSN_BLTI:
  1549. case MEP_INSN_BGEI:
  1550. case MEP_INSN_BCPEQ:
  1551. case MEP_INSN_BCPNE:
  1552. case MEP_INSN_BCPAT:
  1553. case MEP_INSN_BCPAF:
  1554. /* No opcode change needed, just operand. */
  1555. fragP->fr_opcode[2^e] = (addend >> 9) & 0xff;
  1556. fragP->fr_opcode[3^e] = (addend >> 1) & 0xff;
  1557. operand = MEP_OPERAND_PCREL17A2;
  1558. break;
  1559. default:
  1560. abort ();
  1561. }
  1562. if (S_GET_SEGMENT (fragP->fr_symbol) != seg
  1563. || S_IS_WEAK (fragP->fr_symbol)
  1564. || operand == MEP_OPERAND_PCABS24A2)
  1565. {
  1566. gas_assert (fragP->fr_cgen.insn != 0);
  1567. gas_cgen_record_fixup (fragP,
  1568. where,
  1569. fragP->fr_cgen.insn,
  1570. (fragP->fr_fix - where) * 8,
  1571. cgen_operand_lookup_by_num (gas_cgen_cpu_desc,
  1572. operand),
  1573. fragP->fr_cgen.opinfo,
  1574. fragP->fr_symbol, fragP->fr_offset);
  1575. }
  1576. }
  1577. /* Functions concerning relocs. */
  1578. void
  1579. mep_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
  1580. {
  1581. /* If we already know the fixup value, adjust it in the same
  1582. way that the linker would have done. */
  1583. if (fixP->fx_addsy == 0)
  1584. switch (fixP->fx_cgen.opinfo)
  1585. {
  1586. case BFD_RELOC_MEP_LOW16:
  1587. *valP = ((*valP & 0xffff) ^ 0x8000) - 0x8000;
  1588. break;
  1589. case BFD_RELOC_MEP_HI16U:
  1590. *valP >>= 16;
  1591. break;
  1592. case BFD_RELOC_MEP_HI16S:
  1593. *valP = (*valP + 0x8000) >> 16;
  1594. break;
  1595. }
  1596. /* Now call cgen's md_apply_fix. */
  1597. gas_cgen_md_apply_fix (fixP, valP, seg);
  1598. }
  1599. long
  1600. md_pcrel_from_section (fixS *fixP, segT sec)
  1601. {
  1602. if (fixP->fx_addsy != (symbolS *) NULL
  1603. && (! S_IS_DEFINED (fixP->fx_addsy)
  1604. || S_IS_WEAK (fixP->fx_addsy)
  1605. || S_GET_SEGMENT (fixP->fx_addsy) != sec))
  1606. /* The symbol is undefined (or is defined but not in this section).
  1607. Let the linker figure it out. */
  1608. return 0;
  1609. /* If we've got other reasons for emitting this relocation, let the
  1610. linker handle pc-rel also. */
  1611. if (mep_force_relocation (fixP))
  1612. return 0;
  1613. /* Return the address of the opcode - cgen adjusts for opcode size
  1614. itself, to be consistent with the disassembler, which must do
  1615. so. */
  1616. return fixP->fx_where + fixP->fx_frag->fr_address;
  1617. }
  1618. /* Return the bfd reloc type for OPERAND of INSN at fixup FIXP.
  1619. Returns BFD_RELOC_NONE if no reloc type can be found.
  1620. *FIXP may be modified if desired. */
  1621. #if defined (__STDC__) || defined (ALMOST_STDC) || defined (HAVE_STRINGIZE)
  1622. #define MAP(n) case MEP_OPERAND_##n: return BFD_RELOC_MEP_##n;
  1623. #else
  1624. #define MAP(n) case MEP_OPERAND_/**/n: return BFD_RELOC_MEP_/**/n;
  1625. #endif
  1626. bfd_reloc_code_real_type
  1627. md_cgen_lookup_reloc (const CGEN_INSN *insn ATTRIBUTE_UNUSED,
  1628. const CGEN_OPERAND *operand,
  1629. fixS *fixP)
  1630. {
  1631. enum bfd_reloc_code_real reloc = fixP->fx_cgen.opinfo;
  1632. static char printed[MEP_OPERAND_MAX] = { 0 };
  1633. /* If there's a reloc here, it's because the parser saw a %foo() and
  1634. is giving us the correct reloc to use, or because we converted to
  1635. a different size reloc below and want to avoid "converting" more
  1636. than once. */
  1637. if (reloc && reloc != BFD_RELOC_NONE)
  1638. return reloc;
  1639. switch (operand->type)
  1640. {
  1641. MAP (PCREL8A2); /* beqz */
  1642. MAP (PCREL12A2); /* bsr16 */
  1643. MAP (PCREL17A2); /* beqi */
  1644. MAP (PCREL24A2); /* bsr24 */
  1645. MAP (PCABS24A2); /* jmp */
  1646. MAP (UIMM24); /* mov */
  1647. MAP (ADDR24A4); /* sw/lw */
  1648. /* The rest of the relocs should be generated by the parser,
  1649. for things such as %tprel(), etc. */
  1650. case MEP_OPERAND_SIMM16:
  1651. #ifdef OBJ_COMPLEX_RELC
  1652. /* coalescing this into RELOC_MEP_16 is actually a bug,
  1653. since it's a signed operand. let the relc code handle it. */
  1654. return BFD_RELOC_RELC;
  1655. #endif
  1656. case MEP_OPERAND_UIMM16:
  1657. case MEP_OPERAND_SDISP16:
  1658. case MEP_OPERAND_CODE16:
  1659. fixP->fx_where += 2;
  1660. /* to avoid doing the above add twice */
  1661. fixP->fx_cgen.opinfo = BFD_RELOC_MEP_16;
  1662. return BFD_RELOC_MEP_16;
  1663. default:
  1664. #ifdef OBJ_COMPLEX_RELC
  1665. /* this is not an error, yet.
  1666. pass it to the linker. */
  1667. return BFD_RELOC_RELC;
  1668. #endif
  1669. if (printed[operand->type])
  1670. return BFD_RELOC_NONE;
  1671. printed[operand->type] = 1;
  1672. as_bad_where (fixP->fx_file, fixP->fx_line,
  1673. _("Don't know how to relocate plain operands of type %s"),
  1674. operand->name);
  1675. /* Print some helpful hints for the user. */
  1676. switch (operand->type)
  1677. {
  1678. case MEP_OPERAND_UDISP7:
  1679. case MEP_OPERAND_UDISP7A2:
  1680. case MEP_OPERAND_UDISP7A4:
  1681. as_bad_where (fixP->fx_file, fixP->fx_line,
  1682. _("Perhaps you are missing %%tpoff()?"));
  1683. break;
  1684. default:
  1685. break;
  1686. }
  1687. return BFD_RELOC_NONE;
  1688. }
  1689. }
  1690. /* Called while parsing an instruction to create a fixup.
  1691. We need to check for HI16 relocs and queue them up for later sorting. */
  1692. fixS *
  1693. mep_cgen_record_fixup_exp (fragS *frag,
  1694. int where,
  1695. const CGEN_INSN *insn,
  1696. int length,
  1697. const CGEN_OPERAND *operand,
  1698. int opinfo,
  1699. expressionS *exp)
  1700. {
  1701. fixS * fixP = gas_cgen_record_fixup_exp (frag, where, insn, length,
  1702. operand, opinfo, exp);
  1703. return fixP;
  1704. }
  1705. /* Return BFD reloc type from opinfo field in a fixS.
  1706. It's tricky using fx_r_type in mep_frob_file because the values
  1707. are BFD_RELOC_UNUSED + operand number. */
  1708. #define FX_OPINFO_R_TYPE(f) ((f)->fx_cgen.opinfo)
  1709. /* Sort any unmatched HI16 relocs so that they immediately precede
  1710. the corresponding LO16 reloc. This is called before md_apply_fix and
  1711. tc_gen_reloc. */
  1712. void
  1713. mep_frob_file (void)
  1714. {
  1715. struct mep_hi_fixup * l;
  1716. for (l = mep_hi_fixup_list; l != NULL; l = l->next)
  1717. {
  1718. segment_info_type * seginfo;
  1719. int pass;
  1720. gas_assert (FX_OPINFO_R_TYPE (l->fixp) == BFD_RELOC_HI16
  1721. || FX_OPINFO_R_TYPE (l->fixp) == BFD_RELOC_LO16);
  1722. /* Check quickly whether the next fixup happens to be a matching low. */
  1723. if (l->fixp->fx_next != NULL
  1724. && FX_OPINFO_R_TYPE (l->fixp->fx_next) == BFD_RELOC_LO16
  1725. && l->fixp->fx_addsy == l->fixp->fx_next->fx_addsy
  1726. && l->fixp->fx_offset == l->fixp->fx_next->fx_offset)
  1727. continue;
  1728. /* Look through the fixups for this segment for a matching
  1729. `low'. When we find one, move the high just in front of it.
  1730. We do this in two passes. In the first pass, we try to find
  1731. a unique `low'. In the second pass, we permit multiple
  1732. high's relocs for a single `low'. */
  1733. seginfo = seg_info (l->seg);
  1734. for (pass = 0; pass < 2; pass++)
  1735. {
  1736. fixS * f;
  1737. fixS * prev;
  1738. prev = NULL;
  1739. for (f = seginfo->fix_root; f != NULL; f = f->fx_next)
  1740. {
  1741. /* Check whether this is a `low' fixup which matches l->fixp. */
  1742. if (FX_OPINFO_R_TYPE (f) == BFD_RELOC_LO16
  1743. && f->fx_addsy == l->fixp->fx_addsy
  1744. && f->fx_offset == l->fixp->fx_offset
  1745. && (pass == 1
  1746. || prev == NULL
  1747. || (FX_OPINFO_R_TYPE (prev) != BFD_RELOC_HI16)
  1748. || prev->fx_addsy != f->fx_addsy
  1749. || prev->fx_offset != f->fx_offset))
  1750. {
  1751. fixS ** pf;
  1752. /* Move l->fixp before f. */
  1753. for (pf = &seginfo->fix_root;
  1754. * pf != l->fixp;
  1755. pf = & (* pf)->fx_next)
  1756. gas_assert (* pf != NULL);
  1757. * pf = l->fixp->fx_next;
  1758. l->fixp->fx_next = f;
  1759. if (prev == NULL)
  1760. seginfo->fix_root = l->fixp;
  1761. else
  1762. prev->fx_next = l->fixp;
  1763. break;
  1764. }
  1765. prev = f;
  1766. }
  1767. if (f != NULL)
  1768. break;
  1769. if (pass == 1)
  1770. as_warn_where (l->fixp->fx_file, l->fixp->fx_line,
  1771. _("Unmatched high relocation"));
  1772. }
  1773. }
  1774. }
  1775. /* See whether we need to force a relocation into the output file. */
  1776. int
  1777. mep_force_relocation (fixS *fixp)
  1778. {
  1779. if ( fixp->fx_r_type == BFD_RELOC_VTABLE_INHERIT
  1780. || fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
  1781. return 1;
  1782. if (generic_force_reloc (fixp))
  1783. return 1;
  1784. /* Allow branches to global symbols to be resolved at assembly time.
  1785. This is consistent with way relaxable branches are handled, since
  1786. branches to both global and local symbols are relaxed. It also
  1787. corresponds to the assumptions made in md_pcrel_from_section. */
  1788. return S_FORCE_RELOC (fixp->fx_addsy, !fixp->fx_pcrel);
  1789. }
  1790. /* Write a value out to the object file, using the appropriate endianness. */
  1791. void
  1792. md_number_to_chars (char *buf, valueT val, int n)
  1793. {
  1794. if (target_big_endian)
  1795. number_to_chars_bigendian (buf, val, n);
  1796. else
  1797. number_to_chars_littleendian (buf, val, n);
  1798. }
  1799. const char *
  1800. md_atof (int type, char *litP, int *sizeP)
  1801. {
  1802. return ieee_md_atof (type, litP, sizeP, true);
  1803. }
  1804. bool
  1805. mep_fix_adjustable (fixS *fixP)
  1806. {
  1807. bfd_reloc_code_real_type reloc_type;
  1808. if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
  1809. {
  1810. const CGEN_INSN *insn = NULL;
  1811. int opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
  1812. const CGEN_OPERAND *operand
  1813. = cgen_operand_lookup_by_num(gas_cgen_cpu_desc, opindex);
  1814. reloc_type = md_cgen_lookup_reloc (insn, operand, fixP);
  1815. }
  1816. else
  1817. reloc_type = fixP->fx_r_type;
  1818. if (fixP->fx_addsy == NULL)
  1819. return 1;
  1820. /* Prevent all adjustments to global symbols. */
  1821. if (S_IS_EXTERNAL (fixP->fx_addsy))
  1822. return 0;
  1823. if (S_IS_WEAK (fixP->fx_addsy))
  1824. return 0;
  1825. /* We need the symbol name for the VTABLE entries */
  1826. if (reloc_type == BFD_RELOC_VTABLE_INHERIT
  1827. || reloc_type == BFD_RELOC_VTABLE_ENTRY)
  1828. return 0;
  1829. return 1;
  1830. }
  1831. bfd_vma
  1832. mep_elf_section_letter (int letter, const char **ptrmsg)
  1833. {
  1834. if (letter == 'v')
  1835. return SHF_MEP_VLIW;
  1836. *ptrmsg = _("bad .section directive: want a,v,w,x,M,S in string");
  1837. return -1;
  1838. }
  1839. flagword
  1840. mep_elf_section_flags (flagword flags, bfd_vma attr, int type ATTRIBUTE_UNUSED)
  1841. {
  1842. if (attr & SHF_MEP_VLIW)
  1843. flags |= SEC_MEP_VLIW;
  1844. return flags;
  1845. }
  1846. /* In vliw mode, the default section is .vtext. We have to be able
  1847. to switch into .vtext using only the .vtext directive. */
  1848. static segT
  1849. mep_vtext_section (void)
  1850. {
  1851. static segT vtext_section;
  1852. if (! vtext_section)
  1853. {
  1854. flagword applicable = bfd_applicable_section_flags (stdoutput);
  1855. vtext_section = subseg_new (VTEXT_SECTION_NAME, 0);
  1856. bfd_set_section_flags (vtext_section,
  1857. applicable & (SEC_ALLOC | SEC_LOAD | SEC_RELOC
  1858. | SEC_CODE | SEC_READONLY
  1859. | SEC_MEP_VLIW));
  1860. }
  1861. return vtext_section;
  1862. }
  1863. static void
  1864. mep_s_vtext (int ignore ATTRIBUTE_UNUSED)
  1865. {
  1866. int temp;
  1867. /* Record previous_section and previous_subsection. */
  1868. obj_elf_section_change_hook ();
  1869. temp = get_absolute_expression ();
  1870. subseg_set (mep_vtext_section (), (subsegT) temp);
  1871. demand_empty_rest_of_line ();
  1872. }
  1873. static void
  1874. mep_switch_to_core_mode (int dummy ATTRIBUTE_UNUSED)
  1875. {
  1876. mep_process_saved_insns ();
  1877. pluspresent = 0;
  1878. mode = CORE;
  1879. }
  1880. static void
  1881. mep_switch_to_vliw_mode (int dummy ATTRIBUTE_UNUSED)
  1882. {
  1883. if (! MEP_VLIW)
  1884. as_bad (_(".vliw unavailable when VLIW is disabled."));
  1885. mode = VLIW;
  1886. /* Switch into .vtext here too. */
  1887. /* mep_s_vtext(); */
  1888. }
  1889. /* This is an undocumented pseudo-op used to disable gas's
  1890. "disabled_registers" check. Used for code which checks for those
  1891. registers at runtime. */
  1892. static void
  1893. mep_noregerr (int i ATTRIBUTE_UNUSED)
  1894. {
  1895. allow_disabled_registers = 1;
  1896. }
  1897. /* mep_unrecognized_line: This is called when a line that can't be parsed
  1898. is encountered. We use it to check for a leading '+' sign which indicates
  1899. that the current instruction is a coprocessor instruction that is to be
  1900. parallelized with a previous core insn. This function accepts the '+' and
  1901. rejects all other characters that might indicate garbage at the beginning
  1902. of the line. The '+' character gets lost as the calling loop continues,
  1903. so we need to indicate that we saw it. */
  1904. int
  1905. mep_unrecognized_line (int ch)
  1906. {
  1907. switch (ch)
  1908. {
  1909. case '+':
  1910. pluspresent = 1;
  1911. return 1; /* '+' indicates an instruction to be parallelized. */
  1912. default:
  1913. return 0; /* If it's not a '+', the line can't be parsed. */
  1914. }
  1915. }
  1916. void
  1917. mep_cleanup (void)
  1918. {
  1919. /* Take care of any insns left to be parallelized when the file ends.
  1920. This is mainly here to handle the case where the file ends with an
  1921. insn preceded by a + or the file ends unexpectedly. */
  1922. if (mode == VLIW)
  1923. mep_process_saved_insns ();
  1924. }
  1925. int
  1926. mep_flush_pending_output (void)
  1927. {
  1928. if (mode == VLIW)
  1929. {
  1930. mep_process_saved_insns ();
  1931. pluspresent = 0;
  1932. }
  1933. return 1;
  1934. }