mt-asm.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. /* DO NOT EDIT! -*- buffer-read-only: t -*- vi:set ro: */
  2. /* Assembler interface for targets using CGEN. -*- C -*-
  3. CGEN: Cpu tools GENerator
  4. THIS FILE IS MACHINE GENERATED WITH CGEN.
  5. - the resultant file is machine generated, cgen-asm.in isn't
  6. Copyright (C) 1996-2022 Free Software Foundation, Inc.
  7. This file is part of libopcodes.
  8. This library is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 3, or (at your option)
  11. any later version.
  12. It is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  14. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  15. License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software Foundation, Inc.,
  18. 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
  19. /* ??? Eventually more and more of this stuff can go to cpu-independent files.
  20. Keep that in mind. */
  21. #include "sysdep.h"
  22. #include <stdio.h>
  23. #include "ansidecl.h"
  24. #include "bfd.h"
  25. #include "symcat.h"
  26. #include "mt-desc.h"
  27. #include "mt-opc.h"
  28. #include "opintl.h"
  29. #include "xregex.h"
  30. #include "libiberty.h"
  31. #include "safe-ctype.h"
  32. #undef min
  33. #define min(a,b) ((a) < (b) ? (a) : (b))
  34. #undef max
  35. #define max(a,b) ((a) > (b) ? (a) : (b))
  36. static const char * parse_insn_normal
  37. (CGEN_CPU_DESC, const CGEN_INSN *, const char **, CGEN_FIELDS *);
  38. /* -- assembler routines inserted here. */
  39. /* -- asm.c */
  40. /* Range checking for signed numbers. Returns 0 if acceptable
  41. and 1 if the value is out of bounds for a signed quantity. */
  42. static int
  43. signed_out_of_bounds (long val)
  44. {
  45. if ((val < -32768) || (val > 32767))
  46. return 1;
  47. return 0;
  48. }
  49. static const char *
  50. parse_loopsize (CGEN_CPU_DESC cd,
  51. const char **strp,
  52. int opindex,
  53. void *arg)
  54. {
  55. signed long * valuep = (signed long *) arg;
  56. const char *errmsg;
  57. bfd_reloc_code_real_type code = BFD_RELOC_NONE;
  58. enum cgen_parse_operand_result result_type;
  59. bfd_vma value;
  60. /* Is it a control transfer instructions? */
  61. if (opindex == (CGEN_OPERAND_TYPE) MT_OPERAND_LOOPSIZE)
  62. {
  63. code = BFD_RELOC_MT_PCINSN8;
  64. errmsg = cgen_parse_address (cd, strp, opindex, code,
  65. & result_type, & value);
  66. *valuep = value;
  67. return errmsg;
  68. }
  69. abort ();
  70. }
  71. static const char *
  72. parse_imm16 (CGEN_CPU_DESC cd,
  73. const char **strp,
  74. int opindex,
  75. void *arg)
  76. {
  77. signed long * valuep = (signed long *) arg;
  78. const char *errmsg;
  79. enum cgen_parse_operand_result result_type;
  80. bfd_reloc_code_real_type code = BFD_RELOC_NONE;
  81. bfd_vma value;
  82. /* Is it a control transfer instructions? */
  83. if (opindex == (CGEN_OPERAND_TYPE) MT_OPERAND_IMM16O)
  84. {
  85. code = BFD_RELOC_16_PCREL;
  86. errmsg = cgen_parse_address (cd, strp, opindex, code,
  87. & result_type, & value);
  88. if (errmsg == NULL)
  89. {
  90. if (signed_out_of_bounds (value))
  91. errmsg = _("Operand out of range. Must be between -32768 and 32767.");
  92. }
  93. *valuep = value;
  94. return errmsg;
  95. }
  96. /* If it's not a control transfer instruction, then
  97. we have to check for %OP relocating operators. */
  98. if (opindex == (CGEN_OPERAND_TYPE) MT_OPERAND_IMM16L)
  99. ;
  100. else if (strncmp (*strp, "%hi16", 5) == 0)
  101. {
  102. *strp += 5;
  103. code = BFD_RELOC_HI16;
  104. }
  105. else if (strncmp (*strp, "%lo16", 5) == 0)
  106. {
  107. *strp += 5;
  108. code = BFD_RELOC_LO16;
  109. }
  110. /* If we found a %OP relocating operator, then parse it as an address.
  111. If not, we need to parse it as an integer, either signed or unsigned
  112. depending on which operand type we have. */
  113. if (code != BFD_RELOC_NONE)
  114. {
  115. /* %OP relocating operator found. */
  116. errmsg = cgen_parse_address (cd, strp, opindex, code,
  117. & result_type, & value);
  118. if (errmsg == NULL)
  119. {
  120. switch (result_type)
  121. {
  122. case (CGEN_PARSE_OPERAND_RESULT_NUMBER):
  123. if (code == BFD_RELOC_HI16)
  124. value = (value >> 16) & 0xFFFF;
  125. else if (code == BFD_RELOC_LO16)
  126. value = value & 0xFFFF;
  127. else
  128. errmsg = _("Biiiig Trouble in parse_imm16!");
  129. break;
  130. case (CGEN_PARSE_OPERAND_RESULT_QUEUED):
  131. /* No special processing for this case. */
  132. break;
  133. default:
  134. errmsg = _("The percent-operator's operand is not a symbol");
  135. break;
  136. }
  137. }
  138. *valuep = value;
  139. }
  140. else
  141. {
  142. /* Parse hex values like 0xffff as unsigned, and sign extend
  143. them manually. */
  144. int parse_signed = (opindex == (CGEN_OPERAND_TYPE)MT_OPERAND_IMM16);
  145. if ((*strp)[0] == '0'
  146. && ((*strp)[1] == 'x' || (*strp)[1] == 'X'))
  147. parse_signed = 0;
  148. /* No relocating operator. Parse as an number. */
  149. if (parse_signed)
  150. {
  151. /* Parse as as signed integer. */
  152. errmsg = cgen_parse_signed_integer (cd, strp, opindex, valuep);
  153. if (errmsg == NULL)
  154. {
  155. #if 0
  156. /* Manual range checking is needed for the signed case. */
  157. if (*valuep & 0x8000)
  158. value = 0xffff0000 | *valuep;
  159. else
  160. value = *valuep;
  161. if (signed_out_of_bounds (value))
  162. errmsg = _("Operand out of range. Must be between -32768 and 32767.");
  163. /* Truncate to 16 bits. This is necessary
  164. because cgen will have sign extended *valuep. */
  165. *valuep &= 0xFFFF;
  166. #endif
  167. }
  168. }
  169. else
  170. {
  171. /* MT_OPERAND_IMM16Z. Parse as an unsigned integer. */
  172. errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, (unsigned long *) valuep);
  173. if (opindex == (CGEN_OPERAND_TYPE) MT_OPERAND_IMM16
  174. && *valuep >= 0x8000
  175. && *valuep <= 0xffff)
  176. *valuep -= 0x10000;
  177. }
  178. }
  179. return errmsg;
  180. }
  181. static const char *
  182. parse_dup (CGEN_CPU_DESC cd,
  183. const char **strp,
  184. int opindex,
  185. unsigned long *valuep)
  186. {
  187. const char *errmsg = NULL;
  188. if (strncmp (*strp, "dup", 3) == 0 || strncmp (*strp, "DUP", 3) == 0)
  189. {
  190. *strp += 3;
  191. *valuep = 1;
  192. }
  193. else if (strncmp (*strp, "xx", 2) == 0 || strncmp (*strp, "XX", 2) == 0)
  194. {
  195. *strp += 2;
  196. *valuep = 0;
  197. }
  198. else
  199. errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
  200. return errmsg;
  201. }
  202. static const char *
  203. parse_ball (CGEN_CPU_DESC cd,
  204. const char **strp,
  205. int opindex,
  206. unsigned long *valuep)
  207. {
  208. const char *errmsg = NULL;
  209. if (strncmp (*strp, "all", 3) == 0 || strncmp (*strp, "ALL", 3) == 0)
  210. {
  211. *strp += 3;
  212. *valuep = 1;
  213. }
  214. else if (strncmp (*strp, "one", 3) == 0 || strncmp (*strp, "ONE", 3) == 0)
  215. {
  216. *strp += 3;
  217. *valuep = 0;
  218. }
  219. else
  220. errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
  221. return errmsg;
  222. }
  223. static const char *
  224. parse_xmode (CGEN_CPU_DESC cd,
  225. const char **strp,
  226. int opindex,
  227. unsigned long *valuep)
  228. {
  229. const char *errmsg = NULL;
  230. if (strncmp (*strp, "pm", 2) == 0 || strncmp (*strp, "PM", 2) == 0)
  231. {
  232. *strp += 2;
  233. *valuep = 1;
  234. }
  235. else if (strncmp (*strp, "xm", 2) == 0 || strncmp (*strp, "XM", 2) == 0)
  236. {
  237. *strp += 2;
  238. *valuep = 0;
  239. }
  240. else
  241. errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
  242. return errmsg;
  243. }
  244. static const char *
  245. parse_rc (CGEN_CPU_DESC cd,
  246. const char **strp,
  247. int opindex,
  248. unsigned long *valuep)
  249. {
  250. const char *errmsg = NULL;
  251. if (strncmp (*strp, "r", 1) == 0 || strncmp (*strp, "R", 1) == 0)
  252. {
  253. *strp += 1;
  254. *valuep = 1;
  255. }
  256. else if (strncmp (*strp, "c", 1) == 0 || strncmp (*strp, "C", 1) == 0)
  257. {
  258. *strp += 1;
  259. *valuep = 0;
  260. }
  261. else
  262. errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
  263. return errmsg;
  264. }
  265. static const char *
  266. parse_cbrb (CGEN_CPU_DESC cd,
  267. const char **strp,
  268. int opindex,
  269. unsigned long *valuep)
  270. {
  271. const char *errmsg = NULL;
  272. if (strncmp (*strp, "rb", 2) == 0 || strncmp (*strp, "RB", 2) == 0)
  273. {
  274. *strp += 2;
  275. *valuep = 1;
  276. }
  277. else if (strncmp (*strp, "cb", 2) == 0 || strncmp (*strp, "CB", 2) == 0)
  278. {
  279. *strp += 2;
  280. *valuep = 0;
  281. }
  282. else
  283. errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
  284. return errmsg;
  285. }
  286. static const char *
  287. parse_rbbc (CGEN_CPU_DESC cd,
  288. const char **strp,
  289. int opindex,
  290. unsigned long *valuep)
  291. {
  292. const char *errmsg = NULL;
  293. if (strncmp (*strp, "rt", 2) == 0 || strncmp (*strp, "RT", 2) == 0)
  294. {
  295. *strp += 2;
  296. *valuep = 0;
  297. }
  298. else if (strncmp (*strp, "br1", 3) == 0 || strncmp (*strp, "BR1", 3) == 0)
  299. {
  300. *strp += 3;
  301. *valuep = 1;
  302. }
  303. else if (strncmp (*strp, "br2", 3) == 0 || strncmp (*strp, "BR2", 3) == 0)
  304. {
  305. *strp += 3;
  306. *valuep = 2;
  307. }
  308. else if (strncmp (*strp, "cs", 2) == 0 || strncmp (*strp, "CS", 2) == 0)
  309. {
  310. *strp += 2;
  311. *valuep = 3;
  312. }
  313. else
  314. errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
  315. return errmsg;
  316. }
  317. static const char *
  318. parse_type (CGEN_CPU_DESC cd,
  319. const char **strp,
  320. int opindex,
  321. unsigned long *valuep)
  322. {
  323. const char *errmsg = NULL;
  324. if (strncmp (*strp, "odd", 3) == 0 || strncmp (*strp, "ODD", 3) == 0)
  325. {
  326. *strp += 3;
  327. *valuep = 0;
  328. }
  329. else if (strncmp (*strp, "even", 4) == 0 || strncmp (*strp, "EVEN", 4) == 0)
  330. {
  331. *strp += 4;
  332. *valuep = 1;
  333. }
  334. else if (strncmp (*strp, "oe", 2) == 0 || strncmp (*strp, "OE", 2) == 0)
  335. {
  336. *strp += 2;
  337. *valuep = 2;
  338. }
  339. else
  340. errmsg = cgen_parse_unsigned_integer (cd, strp, opindex, valuep);
  341. if ((errmsg == NULL) && (*valuep == 3))
  342. errmsg = _("invalid operand. type may have values 0,1,2 only.");
  343. return errmsg;
  344. }
  345. /* -- dis.c */
  346. const char * mt_cgen_parse_operand
  347. (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
  348. /* Main entry point for operand parsing.
  349. This function is basically just a big switch statement. Earlier versions
  350. used tables to look up the function to use, but
  351. - if the table contains both assembler and disassembler functions then
  352. the disassembler contains much of the assembler and vice-versa,
  353. - there's a lot of inlining possibilities as things grow,
  354. - using a switch statement avoids the function call overhead.
  355. This function could be moved into `parse_insn_normal', but keeping it
  356. separate makes clear the interface between `parse_insn_normal' and each of
  357. the handlers. */
  358. const char *
  359. mt_cgen_parse_operand (CGEN_CPU_DESC cd,
  360. int opindex,
  361. const char ** strp,
  362. CGEN_FIELDS * fields)
  363. {
  364. const char * errmsg = NULL;
  365. /* Used by scalar operands that still need to be parsed. */
  366. long junk ATTRIBUTE_UNUSED;
  367. switch (opindex)
  368. {
  369. case MT_OPERAND_A23 :
  370. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_A23, (unsigned long *) (& fields->f_a23));
  371. break;
  372. case MT_OPERAND_BALL :
  373. errmsg = parse_ball (cd, strp, MT_OPERAND_BALL, (unsigned long *) (& fields->f_ball));
  374. break;
  375. case MT_OPERAND_BALL2 :
  376. errmsg = parse_ball (cd, strp, MT_OPERAND_BALL2, (unsigned long *) (& fields->f_ball2));
  377. break;
  378. case MT_OPERAND_BANKADDR :
  379. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_BANKADDR, (unsigned long *) (& fields->f_bankaddr));
  380. break;
  381. case MT_OPERAND_BRC :
  382. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_BRC, (unsigned long *) (& fields->f_brc));
  383. break;
  384. case MT_OPERAND_BRC2 :
  385. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_BRC2, (unsigned long *) (& fields->f_brc2));
  386. break;
  387. case MT_OPERAND_CB1INCR :
  388. errmsg = cgen_parse_signed_integer (cd, strp, MT_OPERAND_CB1INCR, (long *) (& fields->f_cb1incr));
  389. break;
  390. case MT_OPERAND_CB1SEL :
  391. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CB1SEL, (unsigned long *) (& fields->f_cb1sel));
  392. break;
  393. case MT_OPERAND_CB2INCR :
  394. errmsg = cgen_parse_signed_integer (cd, strp, MT_OPERAND_CB2INCR, (long *) (& fields->f_cb2incr));
  395. break;
  396. case MT_OPERAND_CB2SEL :
  397. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CB2SEL, (unsigned long *) (& fields->f_cb2sel));
  398. break;
  399. case MT_OPERAND_CBRB :
  400. errmsg = parse_cbrb (cd, strp, MT_OPERAND_CBRB, (unsigned long *) (& fields->f_cbrb));
  401. break;
  402. case MT_OPERAND_CBS :
  403. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CBS, (unsigned long *) (& fields->f_cbs));
  404. break;
  405. case MT_OPERAND_CBX :
  406. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CBX, (unsigned long *) (& fields->f_cbx));
  407. break;
  408. case MT_OPERAND_CCB :
  409. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CCB, (unsigned long *) (& fields->f_ccb));
  410. break;
  411. case MT_OPERAND_CDB :
  412. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CDB, (unsigned long *) (& fields->f_cdb));
  413. break;
  414. case MT_OPERAND_CELL :
  415. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CELL, (unsigned long *) (& fields->f_cell));
  416. break;
  417. case MT_OPERAND_COLNUM :
  418. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_COLNUM, (unsigned long *) (& fields->f_colnum));
  419. break;
  420. case MT_OPERAND_CONTNUM :
  421. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CONTNUM, (unsigned long *) (& fields->f_contnum));
  422. break;
  423. case MT_OPERAND_CR :
  424. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CR, (unsigned long *) (& fields->f_cr));
  425. break;
  426. case MT_OPERAND_CTXDISP :
  427. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_CTXDISP, (unsigned long *) (& fields->f_ctxdisp));
  428. break;
  429. case MT_OPERAND_DUP :
  430. errmsg = parse_dup (cd, strp, MT_OPERAND_DUP, (unsigned long *) (& fields->f_dup));
  431. break;
  432. case MT_OPERAND_FBDISP :
  433. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_FBDISP, (unsigned long *) (& fields->f_fbdisp));
  434. break;
  435. case MT_OPERAND_FBINCR :
  436. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_FBINCR, (unsigned long *) (& fields->f_fbincr));
  437. break;
  438. case MT_OPERAND_FRDR :
  439. errmsg = cgen_parse_keyword (cd, strp, & mt_cgen_opval_h_spr, & fields->f_dr);
  440. break;
  441. case MT_OPERAND_FRDRRR :
  442. errmsg = cgen_parse_keyword (cd, strp, & mt_cgen_opval_h_spr, & fields->f_drrr);
  443. break;
  444. case MT_OPERAND_FRSR1 :
  445. errmsg = cgen_parse_keyword (cd, strp, & mt_cgen_opval_h_spr, & fields->f_sr1);
  446. break;
  447. case MT_OPERAND_FRSR2 :
  448. errmsg = cgen_parse_keyword (cd, strp, & mt_cgen_opval_h_spr, & fields->f_sr2);
  449. break;
  450. case MT_OPERAND_ID :
  451. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_ID, (unsigned long *) (& fields->f_id));
  452. break;
  453. case MT_OPERAND_IMM16 :
  454. errmsg = parse_imm16 (cd, strp, MT_OPERAND_IMM16, (long *) (& fields->f_imm16s));
  455. break;
  456. case MT_OPERAND_IMM16L :
  457. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_IMM16L, (unsigned long *) (& fields->f_imm16l));
  458. break;
  459. case MT_OPERAND_IMM16O :
  460. errmsg = parse_imm16 (cd, strp, MT_OPERAND_IMM16O, (unsigned long *) (& fields->f_imm16s));
  461. break;
  462. case MT_OPERAND_IMM16Z :
  463. errmsg = parse_imm16 (cd, strp, MT_OPERAND_IMM16Z, (unsigned long *) (& fields->f_imm16u));
  464. break;
  465. case MT_OPERAND_INCAMT :
  466. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_INCAMT, (unsigned long *) (& fields->f_incamt));
  467. break;
  468. case MT_OPERAND_INCR :
  469. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_INCR, (unsigned long *) (& fields->f_incr));
  470. break;
  471. case MT_OPERAND_LENGTH :
  472. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_LENGTH, (unsigned long *) (& fields->f_length));
  473. break;
  474. case MT_OPERAND_LOOPSIZE :
  475. errmsg = parse_loopsize (cd, strp, MT_OPERAND_LOOPSIZE, (unsigned long *) (& fields->f_loopo));
  476. break;
  477. case MT_OPERAND_MASK :
  478. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_MASK, (unsigned long *) (& fields->f_mask));
  479. break;
  480. case MT_OPERAND_MASK1 :
  481. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_MASK1, (unsigned long *) (& fields->f_mask1));
  482. break;
  483. case MT_OPERAND_MODE :
  484. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_MODE, (unsigned long *) (& fields->f_mode));
  485. break;
  486. case MT_OPERAND_PERM :
  487. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_PERM, (unsigned long *) (& fields->f_perm));
  488. break;
  489. case MT_OPERAND_RBBC :
  490. errmsg = parse_rbbc (cd, strp, MT_OPERAND_RBBC, (unsigned long *) (& fields->f_rbbc));
  491. break;
  492. case MT_OPERAND_RC :
  493. errmsg = parse_rc (cd, strp, MT_OPERAND_RC, (unsigned long *) (& fields->f_rc));
  494. break;
  495. case MT_OPERAND_RC1 :
  496. errmsg = parse_rc (cd, strp, MT_OPERAND_RC1, (unsigned long *) (& fields->f_rc1));
  497. break;
  498. case MT_OPERAND_RC2 :
  499. errmsg = parse_rc (cd, strp, MT_OPERAND_RC2, (unsigned long *) (& fields->f_rc2));
  500. break;
  501. case MT_OPERAND_RC3 :
  502. errmsg = parse_rc (cd, strp, MT_OPERAND_RC3, (unsigned long *) (& fields->f_rc3));
  503. break;
  504. case MT_OPERAND_RCNUM :
  505. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_RCNUM, (unsigned long *) (& fields->f_rcnum));
  506. break;
  507. case MT_OPERAND_RDA :
  508. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_RDA, (unsigned long *) (& fields->f_rda));
  509. break;
  510. case MT_OPERAND_ROWNUM :
  511. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_ROWNUM, (unsigned long *) (& fields->f_rownum));
  512. break;
  513. case MT_OPERAND_ROWNUM1 :
  514. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_ROWNUM1, (unsigned long *) (& fields->f_rownum1));
  515. break;
  516. case MT_OPERAND_ROWNUM2 :
  517. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_ROWNUM2, (unsigned long *) (& fields->f_rownum2));
  518. break;
  519. case MT_OPERAND_SIZE :
  520. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_SIZE, (unsigned long *) (& fields->f_size));
  521. break;
  522. case MT_OPERAND_TYPE :
  523. errmsg = parse_type (cd, strp, MT_OPERAND_TYPE, (unsigned long *) (& fields->f_type));
  524. break;
  525. case MT_OPERAND_WR :
  526. errmsg = cgen_parse_unsigned_integer (cd, strp, MT_OPERAND_WR, (unsigned long *) (& fields->f_wr));
  527. break;
  528. case MT_OPERAND_XMODE :
  529. errmsg = parse_xmode (cd, strp, MT_OPERAND_XMODE, (unsigned long *) (& fields->f_xmode));
  530. break;
  531. default :
  532. /* xgettext:c-format */
  533. opcodes_error_handler
  534. (_("internal error: unrecognized field %d while parsing"),
  535. opindex);
  536. abort ();
  537. }
  538. return errmsg;
  539. }
  540. cgen_parse_fn * const mt_cgen_parse_handlers[] =
  541. {
  542. parse_insn_normal,
  543. };
  544. void
  545. mt_cgen_init_asm (CGEN_CPU_DESC cd)
  546. {
  547. mt_cgen_init_opcode_table (cd);
  548. mt_cgen_init_ibld_table (cd);
  549. cd->parse_handlers = & mt_cgen_parse_handlers[0];
  550. cd->parse_operand = mt_cgen_parse_operand;
  551. #ifdef CGEN_ASM_INIT_HOOK
  552. CGEN_ASM_INIT_HOOK
  553. #endif
  554. }
  555. /* Regex construction routine.
  556. This translates an opcode syntax string into a regex string,
  557. by replacing any non-character syntax element (such as an
  558. opcode) with the pattern '.*'
  559. It then compiles the regex and stores it in the opcode, for
  560. later use by mt_cgen_assemble_insn
  561. Returns NULL for success, an error message for failure. */
  562. char *
  563. mt_cgen_build_insn_regex (CGEN_INSN *insn)
  564. {
  565. CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
  566. const char *mnem = CGEN_INSN_MNEMONIC (insn);
  567. char rxbuf[CGEN_MAX_RX_ELEMENTS];
  568. char *rx = rxbuf;
  569. const CGEN_SYNTAX_CHAR_TYPE *syn;
  570. int reg_err;
  571. syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
  572. /* Mnemonics come first in the syntax string. */
  573. if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
  574. return _("missing mnemonic in syntax string");
  575. ++syn;
  576. /* Generate a case sensitive regular expression that emulates case
  577. insensitive matching in the "C" locale. We cannot generate a case
  578. insensitive regular expression because in Turkish locales, 'i' and 'I'
  579. are not equal modulo case conversion. */
  580. /* Copy the literal mnemonic out of the insn. */
  581. for (; *mnem; mnem++)
  582. {
  583. char c = *mnem;
  584. if (ISALPHA (c))
  585. {
  586. *rx++ = '[';
  587. *rx++ = TOLOWER (c);
  588. *rx++ = TOUPPER (c);
  589. *rx++ = ']';
  590. }
  591. else
  592. *rx++ = c;
  593. }
  594. /* Copy any remaining literals from the syntax string into the rx. */
  595. for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
  596. {
  597. if (CGEN_SYNTAX_CHAR_P (* syn))
  598. {
  599. char c = CGEN_SYNTAX_CHAR (* syn);
  600. switch (c)
  601. {
  602. /* Escape any regex metacharacters in the syntax. */
  603. case '.': case '[': case '\\':
  604. case '*': case '^': case '$':
  605. #ifdef CGEN_ESCAPE_EXTENDED_REGEX
  606. case '?': case '{': case '}':
  607. case '(': case ')': case '*':
  608. case '|': case '+': case ']':
  609. #endif
  610. *rx++ = '\\';
  611. *rx++ = c;
  612. break;
  613. default:
  614. if (ISALPHA (c))
  615. {
  616. *rx++ = '[';
  617. *rx++ = TOLOWER (c);
  618. *rx++ = TOUPPER (c);
  619. *rx++ = ']';
  620. }
  621. else
  622. *rx++ = c;
  623. break;
  624. }
  625. }
  626. else
  627. {
  628. /* Replace non-syntax fields with globs. */
  629. *rx++ = '.';
  630. *rx++ = '*';
  631. }
  632. }
  633. /* Trailing whitespace ok. */
  634. * rx++ = '[';
  635. * rx++ = ' ';
  636. * rx++ = '\t';
  637. * rx++ = ']';
  638. * rx++ = '*';
  639. /* But anchor it after that. */
  640. * rx++ = '$';
  641. * rx = '\0';
  642. CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
  643. reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
  644. if (reg_err == 0)
  645. return NULL;
  646. else
  647. {
  648. static char msg[80];
  649. regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
  650. regfree ((regex_t *) CGEN_INSN_RX (insn));
  651. free (CGEN_INSN_RX (insn));
  652. (CGEN_INSN_RX (insn)) = NULL;
  653. return msg;
  654. }
  655. }
  656. /* Default insn parser.
  657. The syntax string is scanned and operands are parsed and stored in FIELDS.
  658. Relocs are queued as we go via other callbacks.
  659. ??? Note that this is currently an all-or-nothing parser. If we fail to
  660. parse the instruction, we return 0 and the caller will start over from
  661. the beginning. Backtracking will be necessary in parsing subexpressions,
  662. but that can be handled there. Not handling backtracking here may get
  663. expensive in the case of the m68k. Deal with later.
  664. Returns NULL for success, an error message for failure. */
  665. static const char *
  666. parse_insn_normal (CGEN_CPU_DESC cd,
  667. const CGEN_INSN *insn,
  668. const char **strp,
  669. CGEN_FIELDS *fields)
  670. {
  671. /* ??? Runtime added insns not handled yet. */
  672. const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
  673. const char *str = *strp;
  674. const char *errmsg;
  675. const char *p;
  676. const CGEN_SYNTAX_CHAR_TYPE * syn;
  677. #ifdef CGEN_MNEMONIC_OPERANDS
  678. /* FIXME: wip */
  679. int past_opcode_p;
  680. #endif
  681. /* For now we assume the mnemonic is first (there are no leading operands).
  682. We can parse it without needing to set up operand parsing.
  683. GAS's input scrubber will ensure mnemonics are lowercase, but we may
  684. not be called from GAS. */
  685. p = CGEN_INSN_MNEMONIC (insn);
  686. while (*p && TOLOWER (*p) == TOLOWER (*str))
  687. ++p, ++str;
  688. if (* p)
  689. return _("unrecognized instruction");
  690. #ifndef CGEN_MNEMONIC_OPERANDS
  691. if (* str && ! ISSPACE (* str))
  692. return _("unrecognized instruction");
  693. #endif
  694. CGEN_INIT_PARSE (cd);
  695. cgen_init_parse_operand (cd);
  696. #ifdef CGEN_MNEMONIC_OPERANDS
  697. past_opcode_p = 0;
  698. #endif
  699. /* We don't check for (*str != '\0') here because we want to parse
  700. any trailing fake arguments in the syntax string. */
  701. syn = CGEN_SYNTAX_STRING (syntax);
  702. /* Mnemonics come first for now, ensure valid string. */
  703. if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
  704. abort ();
  705. ++syn;
  706. while (* syn != 0)
  707. {
  708. /* Non operand chars must match exactly. */
  709. if (CGEN_SYNTAX_CHAR_P (* syn))
  710. {
  711. /* FIXME: While we allow for non-GAS callers above, we assume the
  712. first char after the mnemonic part is a space. */
  713. /* FIXME: We also take inappropriate advantage of the fact that
  714. GAS's input scrubber will remove extraneous blanks. */
  715. if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
  716. {
  717. #ifdef CGEN_MNEMONIC_OPERANDS
  718. if (CGEN_SYNTAX_CHAR(* syn) == ' ')
  719. past_opcode_p = 1;
  720. #endif
  721. ++ syn;
  722. ++ str;
  723. }
  724. else if (*str)
  725. {
  726. /* Syntax char didn't match. Can't be this insn. */
  727. static char msg [80];
  728. /* xgettext:c-format */
  729. sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
  730. CGEN_SYNTAX_CHAR(*syn), *str);
  731. return msg;
  732. }
  733. else
  734. {
  735. /* Ran out of input. */
  736. static char msg [80];
  737. /* xgettext:c-format */
  738. sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
  739. CGEN_SYNTAX_CHAR(*syn));
  740. return msg;
  741. }
  742. continue;
  743. }
  744. #ifdef CGEN_MNEMONIC_OPERANDS
  745. (void) past_opcode_p;
  746. #endif
  747. /* We have an operand of some sort. */
  748. errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
  749. if (errmsg)
  750. return errmsg;
  751. /* Done with this operand, continue with next one. */
  752. ++ syn;
  753. }
  754. /* If we're at the end of the syntax string, we're done. */
  755. if (* syn == 0)
  756. {
  757. /* FIXME: For the moment we assume a valid `str' can only contain
  758. blanks now. IE: We needn't try again with a longer version of
  759. the insn and it is assumed that longer versions of insns appear
  760. before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
  761. while (ISSPACE (* str))
  762. ++ str;
  763. if (* str != '\0')
  764. return _("junk at end of line"); /* FIXME: would like to include `str' */
  765. return NULL;
  766. }
  767. /* We couldn't parse it. */
  768. return _("unrecognized instruction");
  769. }
  770. /* Main entry point.
  771. This routine is called for each instruction to be assembled.
  772. STR points to the insn to be assembled.
  773. We assume all necessary tables have been initialized.
  774. The assembled instruction, less any fixups, is stored in BUF.
  775. Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
  776. still needs to be converted to target byte order, otherwise BUF is an array
  777. of bytes in target byte order.
  778. The result is a pointer to the insn's entry in the opcode table,
  779. or NULL if an error occured (an error message will have already been
  780. printed).
  781. Note that when processing (non-alias) macro-insns,
  782. this function recurses.
  783. ??? It's possible to make this cpu-independent.
  784. One would have to deal with a few minor things.
  785. At this point in time doing so would be more of a curiosity than useful
  786. [for example this file isn't _that_ big], but keeping the possibility in
  787. mind helps keep the design clean. */
  788. const CGEN_INSN *
  789. mt_cgen_assemble_insn (CGEN_CPU_DESC cd,
  790. const char *str,
  791. CGEN_FIELDS *fields,
  792. CGEN_INSN_BYTES_PTR buf,
  793. char **errmsg)
  794. {
  795. const char *start;
  796. CGEN_INSN_LIST *ilist;
  797. const char *parse_errmsg = NULL;
  798. const char *insert_errmsg = NULL;
  799. int recognized_mnemonic = 0;
  800. /* Skip leading white space. */
  801. while (ISSPACE (* str))
  802. ++ str;
  803. /* The instructions are stored in hashed lists.
  804. Get the first in the list. */
  805. ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
  806. /* Keep looking until we find a match. */
  807. start = str;
  808. for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
  809. {
  810. const CGEN_INSN *insn = ilist->insn;
  811. recognized_mnemonic = 1;
  812. #ifdef CGEN_VALIDATE_INSN_SUPPORTED
  813. /* Not usually needed as unsupported opcodes
  814. shouldn't be in the hash lists. */
  815. /* Is this insn supported by the selected cpu? */
  816. if (! mt_cgen_insn_supported (cd, insn))
  817. continue;
  818. #endif
  819. /* If the RELAXED attribute is set, this is an insn that shouldn't be
  820. chosen immediately. Instead, it is used during assembler/linker
  821. relaxation if possible. */
  822. if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
  823. continue;
  824. str = start;
  825. /* Skip this insn if str doesn't look right lexically. */
  826. if (CGEN_INSN_RX (insn) != NULL &&
  827. regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
  828. continue;
  829. /* Allow parse/insert handlers to obtain length of insn. */
  830. CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
  831. parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
  832. if (parse_errmsg != NULL)
  833. continue;
  834. /* ??? 0 is passed for `pc'. */
  835. insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
  836. (bfd_vma) 0);
  837. if (insert_errmsg != NULL)
  838. continue;
  839. /* It is up to the caller to actually output the insn and any
  840. queued relocs. */
  841. return insn;
  842. }
  843. {
  844. static char errbuf[150];
  845. const char *tmp_errmsg;
  846. #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
  847. #define be_verbose 1
  848. #else
  849. #define be_verbose 0
  850. #endif
  851. if (be_verbose)
  852. {
  853. /* If requesting verbose error messages, use insert_errmsg.
  854. Failing that, use parse_errmsg. */
  855. tmp_errmsg = (insert_errmsg ? insert_errmsg :
  856. parse_errmsg ? parse_errmsg :
  857. recognized_mnemonic ?
  858. _("unrecognized form of instruction") :
  859. _("unrecognized instruction"));
  860. if (strlen (start) > 50)
  861. /* xgettext:c-format */
  862. sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
  863. else
  864. /* xgettext:c-format */
  865. sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
  866. }
  867. else
  868. {
  869. if (strlen (start) > 50)
  870. /* xgettext:c-format */
  871. sprintf (errbuf, _("bad instruction `%.50s...'"), start);
  872. else
  873. /* xgettext:c-format */
  874. sprintf (errbuf, _("bad instruction `%.50s'"), start);
  875. }
  876. *errmsg = errbuf;
  877. return NULL;
  878. }
  879. }