or1k-asm.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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 "or1k-desc.h"
  27. #include "or1k-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. static const char * MISSING_CLOSING_PARENTHESIS = N_("missing `)'");
  41. static const char * INVALID_STORE_RELOC = N_("relocation invalid for store");
  42. static const char * INVALID_RELOC_TYPE = N_("internal relocation type invalid");
  43. #define CGEN_VERBOSE_ASSEMBLER_ERRORS
  44. static const char *
  45. parse_disp26 (CGEN_CPU_DESC cd,
  46. const char ** strp,
  47. int opindex,
  48. int opinfo ATTRIBUTE_UNUSED,
  49. enum cgen_parse_operand_result * resultp,
  50. bfd_vma * valuep)
  51. {
  52. const char *str = *strp;
  53. const char *errmsg = NULL;
  54. bfd_reloc_code_real_type reloc = BFD_RELOC_OR1K_REL_26;
  55. if (strncasecmp (str, "plta(", 5) == 0)
  56. {
  57. *strp = str + 5;
  58. reloc = BFD_RELOC_OR1K_PLTA26;
  59. }
  60. else if (strncasecmp (str, "plt(", 4) == 0)
  61. {
  62. *strp = str + 4;
  63. reloc = BFD_RELOC_OR1K_PLT26;
  64. }
  65. errmsg = cgen_parse_address (cd, strp, opindex, reloc, resultp, valuep);
  66. if (reloc != BFD_RELOC_OR1K_REL_26)
  67. {
  68. if (**strp != ')')
  69. errmsg = MISSING_CLOSING_PARENTHESIS;
  70. else
  71. ++*strp;
  72. }
  73. return errmsg;
  74. }
  75. static const char *
  76. parse_disp21 (CGEN_CPU_DESC cd,
  77. const char ** strp,
  78. int opindex,
  79. int opinfo ATTRIBUTE_UNUSED,
  80. enum cgen_parse_operand_result * resultp,
  81. bfd_vma * valuep)
  82. {
  83. const char *str = *strp;
  84. const char *errmsg = NULL;
  85. bfd_reloc_code_real_type reloc = BFD_RELOC_OR1K_PCREL_PG21;
  86. if (strncasecmp (str, "got(", 4) == 0)
  87. {
  88. *strp = str + 4;
  89. reloc = BFD_RELOC_OR1K_GOT_PG21;
  90. }
  91. else if (strncasecmp (str, "tlsgd(", 6) == 0)
  92. {
  93. *strp = str + 6;
  94. reloc = BFD_RELOC_OR1K_TLS_GD_PG21;
  95. }
  96. else if (strncasecmp (str, "tlsldm(", 7) == 0)
  97. {
  98. *strp = str + 7;
  99. reloc = BFD_RELOC_OR1K_TLS_LDM_PG21;
  100. }
  101. else if (strncasecmp (str, "gottp(", 6) == 0)
  102. {
  103. *strp = str + 6;
  104. reloc = BFD_RELOC_OR1K_TLS_IE_PG21;
  105. }
  106. errmsg = cgen_parse_address (cd, strp, opindex, reloc, resultp, valuep);
  107. if (reloc != BFD_RELOC_OR1K_PCREL_PG21)
  108. {
  109. if (**strp != ')')
  110. errmsg = MISSING_CLOSING_PARENTHESIS;
  111. else
  112. ++*strp;
  113. }
  114. return errmsg;
  115. }
  116. enum or1k_rclass
  117. {
  118. RCLASS_DIRECT = 0,
  119. RCLASS_GOT = 1,
  120. RCLASS_GOTPC = 2,
  121. RCLASS_GOTOFF = 3,
  122. RCLASS_TLSGD = 4,
  123. RCLASS_TLSLDM = 5,
  124. RCLASS_DTPOFF = 6,
  125. RCLASS_GOTTPOFF = 7,
  126. RCLASS_TPOFF = 8,
  127. };
  128. enum or1k_rtype
  129. {
  130. RTYPE_LO = 0,
  131. RTYPE_SLO = 1,
  132. RTYPE_PO = 2,
  133. RTYPE_SPO = 3,
  134. RTYPE_HI = 4,
  135. RTYPE_AHI = 5,
  136. };
  137. #define RCLASS_SHIFT 3
  138. #define RTYPE_MASK 7
  139. static const bfd_reloc_code_real_type or1k_imm16_relocs[][6] = {
  140. { BFD_RELOC_LO16,
  141. BFD_RELOC_OR1K_SLO16,
  142. BFD_RELOC_OR1K_LO13,
  143. BFD_RELOC_OR1K_SLO13,
  144. BFD_RELOC_HI16,
  145. BFD_RELOC_HI16_S, },
  146. { BFD_RELOC_OR1K_GOT16,
  147. BFD_RELOC_UNUSED,
  148. BFD_RELOC_OR1K_GOT_LO13,
  149. BFD_RELOC_UNUSED,
  150. BFD_RELOC_UNUSED,
  151. BFD_RELOC_OR1K_GOT_AHI16 },
  152. { BFD_RELOC_OR1K_GOTPC_LO16,
  153. BFD_RELOC_UNUSED,
  154. BFD_RELOC_UNUSED,
  155. BFD_RELOC_UNUSED,
  156. BFD_RELOC_OR1K_GOTPC_HI16,
  157. BFD_RELOC_UNUSED },
  158. { BFD_RELOC_LO16_GOTOFF,
  159. BFD_RELOC_OR1K_GOTOFF_SLO16,
  160. BFD_RELOC_UNUSED,
  161. BFD_RELOC_UNUSED,
  162. BFD_RELOC_HI16_GOTOFF,
  163. BFD_RELOC_HI16_S_GOTOFF },
  164. { BFD_RELOC_OR1K_TLS_GD_LO16,
  165. BFD_RELOC_UNUSED,
  166. BFD_RELOC_OR1K_TLS_GD_LO13,
  167. BFD_RELOC_UNUSED,
  168. BFD_RELOC_OR1K_TLS_GD_HI16,
  169. BFD_RELOC_UNUSED },
  170. { BFD_RELOC_OR1K_TLS_LDM_LO16,
  171. BFD_RELOC_UNUSED,
  172. BFD_RELOC_OR1K_TLS_LDM_LO13,
  173. BFD_RELOC_UNUSED,
  174. BFD_RELOC_OR1K_TLS_LDM_HI16,
  175. BFD_RELOC_UNUSED },
  176. { BFD_RELOC_OR1K_TLS_LDO_LO16,
  177. BFD_RELOC_UNUSED,
  178. BFD_RELOC_UNUSED,
  179. BFD_RELOC_UNUSED,
  180. BFD_RELOC_OR1K_TLS_LDO_HI16,
  181. BFD_RELOC_UNUSED },
  182. { BFD_RELOC_OR1K_TLS_IE_LO16,
  183. BFD_RELOC_UNUSED,
  184. BFD_RELOC_OR1K_TLS_IE_LO13,
  185. BFD_RELOC_UNUSED,
  186. BFD_RELOC_OR1K_TLS_IE_HI16,
  187. BFD_RELOC_OR1K_TLS_IE_AHI16 },
  188. { BFD_RELOC_OR1K_TLS_LE_LO16,
  189. BFD_RELOC_OR1K_TLS_LE_SLO16,
  190. BFD_RELOC_UNUSED,
  191. BFD_RELOC_UNUSED,
  192. BFD_RELOC_OR1K_TLS_LE_HI16,
  193. BFD_RELOC_OR1K_TLS_LE_AHI16 },
  194. };
  195. static int
  196. parse_reloc (const char **strp)
  197. {
  198. const char *str = *strp;
  199. enum or1k_rclass cls = RCLASS_DIRECT;
  200. enum or1k_rtype typ;
  201. if (strncasecmp (str, "got(", 4) == 0)
  202. {
  203. *strp = str + 4;
  204. return (RCLASS_GOT << RCLASS_SHIFT) | RTYPE_LO;
  205. }
  206. if (strncasecmp (str, "gotpo(", 6) == 0)
  207. {
  208. *strp = str + 6;
  209. return (RCLASS_GOT << RCLASS_SHIFT) | RTYPE_PO;
  210. }
  211. if (strncasecmp (str, "gottppo(", 8) == 0)
  212. {
  213. *strp = str + 8;
  214. return (RCLASS_GOTTPOFF << RCLASS_SHIFT) | RTYPE_PO;
  215. }
  216. if (strncasecmp (str, "gotpc", 5) == 0)
  217. {
  218. str += 5;
  219. cls = RCLASS_GOTPC;
  220. }
  221. else if (strncasecmp (str, "gotoff", 6) == 0)
  222. {
  223. str += 6;
  224. cls = RCLASS_GOTOFF;
  225. }
  226. else if (strncasecmp (str, "tlsgd", 5) == 0)
  227. {
  228. str += 5;
  229. cls = RCLASS_TLSGD;
  230. }
  231. else if (strncasecmp (str, "tlsldm", 6) == 0)
  232. {
  233. str += 6;
  234. cls = RCLASS_TLSLDM;
  235. }
  236. else if (strncasecmp (str, "dtpoff", 6) == 0)
  237. {
  238. str += 6;
  239. cls = RCLASS_DTPOFF;
  240. }
  241. else if (strncasecmp (str, "gottpoff", 8) == 0)
  242. {
  243. str += 8;
  244. cls = RCLASS_GOTTPOFF;
  245. }
  246. else if (strncasecmp (str, "tpoff", 5) == 0)
  247. {
  248. str += 5;
  249. cls = RCLASS_TPOFF;
  250. }
  251. else if (strncasecmp (str, "got", 3) == 0)
  252. {
  253. str += 3;
  254. cls = RCLASS_GOT;
  255. }
  256. if (strncasecmp (str, "hi(", 3) == 0)
  257. {
  258. str += 3;
  259. typ = RTYPE_HI;
  260. }
  261. else if (strncasecmp (str, "lo(", 3) == 0)
  262. {
  263. str += 3;
  264. typ = RTYPE_LO;
  265. }
  266. else if (strncasecmp (str, "ha(", 3) == 0)
  267. {
  268. str += 3;
  269. typ = RTYPE_AHI;
  270. }
  271. else if (strncasecmp (str, "po(", 3) == 0 && cls != RCLASS_GOTTPOFF)
  272. {
  273. str += 3;
  274. typ = RTYPE_PO;
  275. }
  276. else
  277. return -1;
  278. *strp = str;
  279. return (cls << RCLASS_SHIFT) | typ;
  280. }
  281. static const char *
  282. parse_imm16 (CGEN_CPU_DESC cd, const char **strp, int opindex,
  283. long *valuep, int splitp)
  284. {
  285. const char *errmsg;
  286. enum cgen_parse_operand_result result_type;
  287. bfd_reloc_code_real_type reloc = BFD_RELOC_UNUSED;
  288. enum or1k_rtype reloc_type;
  289. int reloc_code;
  290. bfd_vma ret;
  291. if (**strp == '#')
  292. ++*strp;
  293. reloc_code = parse_reloc (strp);
  294. reloc_type = reloc_code & RTYPE_MASK;
  295. if (reloc_code >= 0)
  296. {
  297. enum or1k_rclass reloc_class = reloc_code >> RCLASS_SHIFT;
  298. if (splitp)
  299. {
  300. if ((reloc_type == RTYPE_LO || reloc_type == RTYPE_PO)
  301. && reloc_class != RCLASS_GOT)
  302. /* If split we or up the type to RTYPE_SLO or RTYPE_SPO. */
  303. reloc_type |= 1;
  304. else
  305. return INVALID_STORE_RELOC;
  306. }
  307. reloc = or1k_imm16_relocs[reloc_class][reloc_type];
  308. }
  309. if (reloc != BFD_RELOC_UNUSED)
  310. {
  311. bfd_vma value;
  312. errmsg = cgen_parse_address (cd, strp, opindex, reloc,
  313. &result_type, &value);
  314. if (**strp != ')')
  315. errmsg = MISSING_CLOSING_PARENTHESIS;
  316. ++*strp;
  317. ret = value;
  318. if (errmsg == NULL && result_type == CGEN_PARSE_OPERAND_RESULT_NUMBER)
  319. switch (reloc_type)
  320. {
  321. case RTYPE_AHI:
  322. ret += 0x8000;
  323. /* FALLTHRU */
  324. case RTYPE_HI:
  325. ret >>= 16;
  326. /* FALLTHRU */
  327. case RTYPE_LO:
  328. case RTYPE_SLO:
  329. ret &= 0xffff;
  330. ret = (ret ^ 0x8000) - 0x8000;
  331. break;
  332. case RTYPE_PO:
  333. case RTYPE_SPO:
  334. ret &= 0x1fff;
  335. break;
  336. default:
  337. errmsg = INVALID_RELOC_TYPE;
  338. }
  339. }
  340. else
  341. {
  342. long value;
  343. errmsg = cgen_parse_signed_integer (cd, strp, opindex, &value);
  344. ret = value;
  345. }
  346. if (errmsg == NULL)
  347. *valuep = ret;
  348. return errmsg;
  349. }
  350. static const char *
  351. parse_simm16 (CGEN_CPU_DESC cd, const char **strp, int opindex, long *valuep)
  352. {
  353. return parse_imm16(cd, strp, opindex, (long *) valuep, 0);
  354. }
  355. static const char *
  356. parse_simm16_split (CGEN_CPU_DESC cd, const char **strp, int opindex,
  357. long *valuep)
  358. {
  359. return parse_imm16(cd, strp, opindex, (long *) valuep, 1);
  360. }
  361. static const char *
  362. parse_uimm16 (CGEN_CPU_DESC cd, const char **strp, int opindex,
  363. unsigned long *valuep)
  364. {
  365. const char *errmsg = parse_imm16(cd, strp, opindex, (long *) valuep, 0);
  366. if (errmsg == NULL)
  367. *valuep &= 0xffff;
  368. return errmsg;
  369. }
  370. static const char *
  371. parse_uimm16_split (CGEN_CPU_DESC cd, const char **strp, int opindex,
  372. unsigned long *valuep)
  373. {
  374. const char *errmsg = parse_imm16(cd, strp, opindex, (long *) valuep, 1);
  375. if (errmsg == NULL)
  376. *valuep &= 0xffff;
  377. return errmsg;
  378. }
  379. /* Parse register pairs with syntax rA,rB to a flag + rA value. */
  380. static const char *
  381. parse_regpair (CGEN_CPU_DESC cd, const char **strp,
  382. int opindex ATTRIBUTE_UNUSED, unsigned long *valuep)
  383. {
  384. long reg1_index;
  385. long reg2_index;
  386. const char *errmsg;
  387. /* The first part should just be a register. */
  388. errmsg = cgen_parse_keyword (cd, strp, &or1k_cgen_opval_h_gpr,
  389. &reg1_index);
  390. /* If that worked skip the comma separator. */
  391. if (errmsg == NULL)
  392. {
  393. if (**strp == ',')
  394. ++*strp;
  395. else
  396. errmsg = "Unexpected character, expected ','";
  397. }
  398. /* If that worked the next part is just another register. */
  399. if (errmsg == NULL)
  400. errmsg = cgen_parse_keyword (cd, strp, &or1k_cgen_opval_h_gpr,
  401. &reg2_index);
  402. /* Validate the register pair is valid and create the output value. */
  403. if (errmsg == NULL)
  404. {
  405. int regoffset = reg2_index - reg1_index;
  406. if (regoffset == 1 || regoffset == 2)
  407. {
  408. unsigned short offsetmask;
  409. unsigned short value;
  410. offsetmask = ((regoffset == 2 ? 1 : 0) << 5);
  411. value = offsetmask | reg1_index;
  412. *valuep = value;
  413. }
  414. else
  415. errmsg = "Invalid register pair, offset not 1 or 2.";
  416. }
  417. return errmsg;
  418. }
  419. /* -- */
  420. const char * or1k_cgen_parse_operand
  421. (CGEN_CPU_DESC, int, const char **, CGEN_FIELDS *);
  422. /* Main entry point for operand parsing.
  423. This function is basically just a big switch statement. Earlier versions
  424. used tables to look up the function to use, but
  425. - if the table contains both assembler and disassembler functions then
  426. the disassembler contains much of the assembler and vice-versa,
  427. - there's a lot of inlining possibilities as things grow,
  428. - using a switch statement avoids the function call overhead.
  429. This function could be moved into `parse_insn_normal', but keeping it
  430. separate makes clear the interface between `parse_insn_normal' and each of
  431. the handlers. */
  432. const char *
  433. or1k_cgen_parse_operand (CGEN_CPU_DESC cd,
  434. int opindex,
  435. const char ** strp,
  436. CGEN_FIELDS * fields)
  437. {
  438. const char * errmsg = NULL;
  439. /* Used by scalar operands that still need to be parsed. */
  440. long junk ATTRIBUTE_UNUSED;
  441. switch (opindex)
  442. {
  443. case OR1K_OPERAND_DISP21 :
  444. {
  445. bfd_vma value = 0;
  446. errmsg = parse_disp21 (cd, strp, OR1K_OPERAND_DISP21, 0, NULL, & value);
  447. fields->f_disp21 = value;
  448. }
  449. break;
  450. case OR1K_OPERAND_DISP26 :
  451. {
  452. bfd_vma value = 0;
  453. errmsg = parse_disp26 (cd, strp, OR1K_OPERAND_DISP26, 0, NULL, & value);
  454. fields->f_disp26 = value;
  455. }
  456. break;
  457. case OR1K_OPERAND_RA :
  458. errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_gpr, & fields->f_r2);
  459. break;
  460. case OR1K_OPERAND_RAD32F :
  461. errmsg = parse_regpair (cd, strp, OR1K_OPERAND_RAD32F, (unsigned long *) (& fields->f_rad32));
  462. break;
  463. case OR1K_OPERAND_RADI :
  464. errmsg = parse_regpair (cd, strp, OR1K_OPERAND_RADI, (unsigned long *) (& fields->f_rad32));
  465. break;
  466. case OR1K_OPERAND_RASF :
  467. errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_fsr, & fields->f_r2);
  468. break;
  469. case OR1K_OPERAND_RB :
  470. errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_gpr, & fields->f_r3);
  471. break;
  472. case OR1K_OPERAND_RBD32F :
  473. errmsg = parse_regpair (cd, strp, OR1K_OPERAND_RBD32F, (unsigned long *) (& fields->f_rbd32));
  474. break;
  475. case OR1K_OPERAND_RBDI :
  476. errmsg = parse_regpair (cd, strp, OR1K_OPERAND_RBDI, (unsigned long *) (& fields->f_rbd32));
  477. break;
  478. case OR1K_OPERAND_RBSF :
  479. errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_fsr, & fields->f_r3);
  480. break;
  481. case OR1K_OPERAND_RD :
  482. errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_gpr, & fields->f_r1);
  483. break;
  484. case OR1K_OPERAND_RDD32F :
  485. errmsg = parse_regpair (cd, strp, OR1K_OPERAND_RDD32F, (unsigned long *) (& fields->f_rdd32));
  486. break;
  487. case OR1K_OPERAND_RDDI :
  488. errmsg = parse_regpair (cd, strp, OR1K_OPERAND_RDDI, (unsigned long *) (& fields->f_rdd32));
  489. break;
  490. case OR1K_OPERAND_RDSF :
  491. errmsg = cgen_parse_keyword (cd, strp, & or1k_cgen_opval_h_fsr, & fields->f_r1);
  492. break;
  493. case OR1K_OPERAND_SIMM16 :
  494. errmsg = parse_simm16 (cd, strp, OR1K_OPERAND_SIMM16, (long *) (& fields->f_simm16));
  495. break;
  496. case OR1K_OPERAND_SIMM16_SPLIT :
  497. errmsg = parse_simm16_split (cd, strp, OR1K_OPERAND_SIMM16_SPLIT, (long *) (& fields->f_simm16_split));
  498. break;
  499. case OR1K_OPERAND_UIMM16 :
  500. errmsg = parse_uimm16 (cd, strp, OR1K_OPERAND_UIMM16, (unsigned long *) (& fields->f_uimm16));
  501. break;
  502. case OR1K_OPERAND_UIMM16_SPLIT :
  503. errmsg = parse_uimm16_split (cd, strp, OR1K_OPERAND_UIMM16_SPLIT, (unsigned long *) (& fields->f_uimm16_split));
  504. break;
  505. case OR1K_OPERAND_UIMM6 :
  506. errmsg = cgen_parse_unsigned_integer (cd, strp, OR1K_OPERAND_UIMM6, (unsigned long *) (& fields->f_uimm6));
  507. break;
  508. default :
  509. /* xgettext:c-format */
  510. opcodes_error_handler
  511. (_("internal error: unrecognized field %d while parsing"),
  512. opindex);
  513. abort ();
  514. }
  515. return errmsg;
  516. }
  517. cgen_parse_fn * const or1k_cgen_parse_handlers[] =
  518. {
  519. parse_insn_normal,
  520. };
  521. void
  522. or1k_cgen_init_asm (CGEN_CPU_DESC cd)
  523. {
  524. or1k_cgen_init_opcode_table (cd);
  525. or1k_cgen_init_ibld_table (cd);
  526. cd->parse_handlers = & or1k_cgen_parse_handlers[0];
  527. cd->parse_operand = or1k_cgen_parse_operand;
  528. #ifdef CGEN_ASM_INIT_HOOK
  529. CGEN_ASM_INIT_HOOK
  530. #endif
  531. }
  532. /* Regex construction routine.
  533. This translates an opcode syntax string into a regex string,
  534. by replacing any non-character syntax element (such as an
  535. opcode) with the pattern '.*'
  536. It then compiles the regex and stores it in the opcode, for
  537. later use by or1k_cgen_assemble_insn
  538. Returns NULL for success, an error message for failure. */
  539. char *
  540. or1k_cgen_build_insn_regex (CGEN_INSN *insn)
  541. {
  542. CGEN_OPCODE *opc = (CGEN_OPCODE *) CGEN_INSN_OPCODE (insn);
  543. const char *mnem = CGEN_INSN_MNEMONIC (insn);
  544. char rxbuf[CGEN_MAX_RX_ELEMENTS];
  545. char *rx = rxbuf;
  546. const CGEN_SYNTAX_CHAR_TYPE *syn;
  547. int reg_err;
  548. syn = CGEN_SYNTAX_STRING (CGEN_OPCODE_SYNTAX (opc));
  549. /* Mnemonics come first in the syntax string. */
  550. if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
  551. return _("missing mnemonic in syntax string");
  552. ++syn;
  553. /* Generate a case sensitive regular expression that emulates case
  554. insensitive matching in the "C" locale. We cannot generate a case
  555. insensitive regular expression because in Turkish locales, 'i' and 'I'
  556. are not equal modulo case conversion. */
  557. /* Copy the literal mnemonic out of the insn. */
  558. for (; *mnem; mnem++)
  559. {
  560. char c = *mnem;
  561. if (ISALPHA (c))
  562. {
  563. *rx++ = '[';
  564. *rx++ = TOLOWER (c);
  565. *rx++ = TOUPPER (c);
  566. *rx++ = ']';
  567. }
  568. else
  569. *rx++ = c;
  570. }
  571. /* Copy any remaining literals from the syntax string into the rx. */
  572. for(; * syn != 0 && rx <= rxbuf + (CGEN_MAX_RX_ELEMENTS - 7 - 4); ++syn)
  573. {
  574. if (CGEN_SYNTAX_CHAR_P (* syn))
  575. {
  576. char c = CGEN_SYNTAX_CHAR (* syn);
  577. switch (c)
  578. {
  579. /* Escape any regex metacharacters in the syntax. */
  580. case '.': case '[': case '\\':
  581. case '*': case '^': case '$':
  582. #ifdef CGEN_ESCAPE_EXTENDED_REGEX
  583. case '?': case '{': case '}':
  584. case '(': case ')': case '*':
  585. case '|': case '+': case ']':
  586. #endif
  587. *rx++ = '\\';
  588. *rx++ = c;
  589. break;
  590. default:
  591. if (ISALPHA (c))
  592. {
  593. *rx++ = '[';
  594. *rx++ = TOLOWER (c);
  595. *rx++ = TOUPPER (c);
  596. *rx++ = ']';
  597. }
  598. else
  599. *rx++ = c;
  600. break;
  601. }
  602. }
  603. else
  604. {
  605. /* Replace non-syntax fields with globs. */
  606. *rx++ = '.';
  607. *rx++ = '*';
  608. }
  609. }
  610. /* Trailing whitespace ok. */
  611. * rx++ = '[';
  612. * rx++ = ' ';
  613. * rx++ = '\t';
  614. * rx++ = ']';
  615. * rx++ = '*';
  616. /* But anchor it after that. */
  617. * rx++ = '$';
  618. * rx = '\0';
  619. CGEN_INSN_RX (insn) = xmalloc (sizeof (regex_t));
  620. reg_err = regcomp ((regex_t *) CGEN_INSN_RX (insn), rxbuf, REG_NOSUB);
  621. if (reg_err == 0)
  622. return NULL;
  623. else
  624. {
  625. static char msg[80];
  626. regerror (reg_err, (regex_t *) CGEN_INSN_RX (insn), msg, 80);
  627. regfree ((regex_t *) CGEN_INSN_RX (insn));
  628. free (CGEN_INSN_RX (insn));
  629. (CGEN_INSN_RX (insn)) = NULL;
  630. return msg;
  631. }
  632. }
  633. /* Default insn parser.
  634. The syntax string is scanned and operands are parsed and stored in FIELDS.
  635. Relocs are queued as we go via other callbacks.
  636. ??? Note that this is currently an all-or-nothing parser. If we fail to
  637. parse the instruction, we return 0 and the caller will start over from
  638. the beginning. Backtracking will be necessary in parsing subexpressions,
  639. but that can be handled there. Not handling backtracking here may get
  640. expensive in the case of the m68k. Deal with later.
  641. Returns NULL for success, an error message for failure. */
  642. static const char *
  643. parse_insn_normal (CGEN_CPU_DESC cd,
  644. const CGEN_INSN *insn,
  645. const char **strp,
  646. CGEN_FIELDS *fields)
  647. {
  648. /* ??? Runtime added insns not handled yet. */
  649. const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
  650. const char *str = *strp;
  651. const char *errmsg;
  652. const char *p;
  653. const CGEN_SYNTAX_CHAR_TYPE * syn;
  654. #ifdef CGEN_MNEMONIC_OPERANDS
  655. /* FIXME: wip */
  656. int past_opcode_p;
  657. #endif
  658. /* For now we assume the mnemonic is first (there are no leading operands).
  659. We can parse it without needing to set up operand parsing.
  660. GAS's input scrubber will ensure mnemonics are lowercase, but we may
  661. not be called from GAS. */
  662. p = CGEN_INSN_MNEMONIC (insn);
  663. while (*p && TOLOWER (*p) == TOLOWER (*str))
  664. ++p, ++str;
  665. if (* p)
  666. return _("unrecognized instruction");
  667. #ifndef CGEN_MNEMONIC_OPERANDS
  668. if (* str && ! ISSPACE (* str))
  669. return _("unrecognized instruction");
  670. #endif
  671. CGEN_INIT_PARSE (cd);
  672. cgen_init_parse_operand (cd);
  673. #ifdef CGEN_MNEMONIC_OPERANDS
  674. past_opcode_p = 0;
  675. #endif
  676. /* We don't check for (*str != '\0') here because we want to parse
  677. any trailing fake arguments in the syntax string. */
  678. syn = CGEN_SYNTAX_STRING (syntax);
  679. /* Mnemonics come first for now, ensure valid string. */
  680. if (! CGEN_SYNTAX_MNEMONIC_P (* syn))
  681. abort ();
  682. ++syn;
  683. while (* syn != 0)
  684. {
  685. /* Non operand chars must match exactly. */
  686. if (CGEN_SYNTAX_CHAR_P (* syn))
  687. {
  688. /* FIXME: While we allow for non-GAS callers above, we assume the
  689. first char after the mnemonic part is a space. */
  690. /* FIXME: We also take inappropriate advantage of the fact that
  691. GAS's input scrubber will remove extraneous blanks. */
  692. if (TOLOWER (*str) == TOLOWER (CGEN_SYNTAX_CHAR (* syn)))
  693. {
  694. #ifdef CGEN_MNEMONIC_OPERANDS
  695. if (CGEN_SYNTAX_CHAR(* syn) == ' ')
  696. past_opcode_p = 1;
  697. #endif
  698. ++ syn;
  699. ++ str;
  700. }
  701. else if (*str)
  702. {
  703. /* Syntax char didn't match. Can't be this insn. */
  704. static char msg [80];
  705. /* xgettext:c-format */
  706. sprintf (msg, _("syntax error (expected char `%c', found `%c')"),
  707. CGEN_SYNTAX_CHAR(*syn), *str);
  708. return msg;
  709. }
  710. else
  711. {
  712. /* Ran out of input. */
  713. static char msg [80];
  714. /* xgettext:c-format */
  715. sprintf (msg, _("syntax error (expected char `%c', found end of instruction)"),
  716. CGEN_SYNTAX_CHAR(*syn));
  717. return msg;
  718. }
  719. continue;
  720. }
  721. #ifdef CGEN_MNEMONIC_OPERANDS
  722. (void) past_opcode_p;
  723. #endif
  724. /* We have an operand of some sort. */
  725. errmsg = cd->parse_operand (cd, CGEN_SYNTAX_FIELD (*syn), &str, fields);
  726. if (errmsg)
  727. return errmsg;
  728. /* Done with this operand, continue with next one. */
  729. ++ syn;
  730. }
  731. /* If we're at the end of the syntax string, we're done. */
  732. if (* syn == 0)
  733. {
  734. /* FIXME: For the moment we assume a valid `str' can only contain
  735. blanks now. IE: We needn't try again with a longer version of
  736. the insn and it is assumed that longer versions of insns appear
  737. before shorter ones (eg: lsr r2,r3,1 vs lsr r2,r3). */
  738. while (ISSPACE (* str))
  739. ++ str;
  740. if (* str != '\0')
  741. return _("junk at end of line"); /* FIXME: would like to include `str' */
  742. return NULL;
  743. }
  744. /* We couldn't parse it. */
  745. return _("unrecognized instruction");
  746. }
  747. /* Main entry point.
  748. This routine is called for each instruction to be assembled.
  749. STR points to the insn to be assembled.
  750. We assume all necessary tables have been initialized.
  751. The assembled instruction, less any fixups, is stored in BUF.
  752. Remember that if CGEN_INT_INSN_P then BUF is an int and thus the value
  753. still needs to be converted to target byte order, otherwise BUF is an array
  754. of bytes in target byte order.
  755. The result is a pointer to the insn's entry in the opcode table,
  756. or NULL if an error occured (an error message will have already been
  757. printed).
  758. Note that when processing (non-alias) macro-insns,
  759. this function recurses.
  760. ??? It's possible to make this cpu-independent.
  761. One would have to deal with a few minor things.
  762. At this point in time doing so would be more of a curiosity than useful
  763. [for example this file isn't _that_ big], but keeping the possibility in
  764. mind helps keep the design clean. */
  765. const CGEN_INSN *
  766. or1k_cgen_assemble_insn (CGEN_CPU_DESC cd,
  767. const char *str,
  768. CGEN_FIELDS *fields,
  769. CGEN_INSN_BYTES_PTR buf,
  770. char **errmsg)
  771. {
  772. const char *start;
  773. CGEN_INSN_LIST *ilist;
  774. const char *parse_errmsg = NULL;
  775. const char *insert_errmsg = NULL;
  776. int recognized_mnemonic = 0;
  777. /* Skip leading white space. */
  778. while (ISSPACE (* str))
  779. ++ str;
  780. /* The instructions are stored in hashed lists.
  781. Get the first in the list. */
  782. ilist = CGEN_ASM_LOOKUP_INSN (cd, str);
  783. /* Keep looking until we find a match. */
  784. start = str;
  785. for ( ; ilist != NULL ; ilist = CGEN_ASM_NEXT_INSN (ilist))
  786. {
  787. const CGEN_INSN *insn = ilist->insn;
  788. recognized_mnemonic = 1;
  789. #ifdef CGEN_VALIDATE_INSN_SUPPORTED
  790. /* Not usually needed as unsupported opcodes
  791. shouldn't be in the hash lists. */
  792. /* Is this insn supported by the selected cpu? */
  793. if (! or1k_cgen_insn_supported (cd, insn))
  794. continue;
  795. #endif
  796. /* If the RELAXED attribute is set, this is an insn that shouldn't be
  797. chosen immediately. Instead, it is used during assembler/linker
  798. relaxation if possible. */
  799. if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXED) != 0)
  800. continue;
  801. str = start;
  802. /* Skip this insn if str doesn't look right lexically. */
  803. if (CGEN_INSN_RX (insn) != NULL &&
  804. regexec ((regex_t *) CGEN_INSN_RX (insn), str, 0, NULL, 0) == REG_NOMATCH)
  805. continue;
  806. /* Allow parse/insert handlers to obtain length of insn. */
  807. CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
  808. parse_errmsg = CGEN_PARSE_FN (cd, insn) (cd, insn, & str, fields);
  809. if (parse_errmsg != NULL)
  810. continue;
  811. /* ??? 0 is passed for `pc'. */
  812. insert_errmsg = CGEN_INSERT_FN (cd, insn) (cd, insn, fields, buf,
  813. (bfd_vma) 0);
  814. if (insert_errmsg != NULL)
  815. continue;
  816. /* It is up to the caller to actually output the insn and any
  817. queued relocs. */
  818. return insn;
  819. }
  820. {
  821. static char errbuf[150];
  822. const char *tmp_errmsg;
  823. #ifdef CGEN_VERBOSE_ASSEMBLER_ERRORS
  824. #define be_verbose 1
  825. #else
  826. #define be_verbose 0
  827. #endif
  828. if (be_verbose)
  829. {
  830. /* If requesting verbose error messages, use insert_errmsg.
  831. Failing that, use parse_errmsg. */
  832. tmp_errmsg = (insert_errmsg ? insert_errmsg :
  833. parse_errmsg ? parse_errmsg :
  834. recognized_mnemonic ?
  835. _("unrecognized form of instruction") :
  836. _("unrecognized instruction"));
  837. if (strlen (start) > 50)
  838. /* xgettext:c-format */
  839. sprintf (errbuf, "%s `%.50s...'", tmp_errmsg, start);
  840. else
  841. /* xgettext:c-format */
  842. sprintf (errbuf, "%s `%.50s'", tmp_errmsg, start);
  843. }
  844. else
  845. {
  846. if (strlen (start) > 50)
  847. /* xgettext:c-format */
  848. sprintf (errbuf, _("bad instruction `%.50s...'"), start);
  849. else
  850. /* xgettext:c-format */
  851. sprintf (errbuf, _("bad instruction `%.50s'"), start);
  852. }
  853. *errmsg = errbuf;
  854. return NULL;
  855. }
  856. }