cgen-ibld.in 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /* Instruction building/extraction support for @arch@. -*- C -*-
  2. THIS FILE IS MACHINE GENERATED WITH CGEN: Cpu tools GENerator.
  3. - the resultant file is machine generated, cgen-ibld.in isn't
  4. Copyright (C) 1996-2022 Free Software Foundation, Inc.
  5. This file is part of libopcodes.
  6. This library is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3, or (at your option)
  9. any later version.
  10. It is distributed in the hope that it will be useful, but WITHOUT
  11. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  13. License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software Foundation, Inc.,
  16. 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
  17. /* ??? Eventually more and more of this stuff can go to cpu-independent files.
  18. Keep that in mind. */
  19. #include "sysdep.h"
  20. #include <stdio.h>
  21. #include "ansidecl.h"
  22. #include "dis-asm.h"
  23. #include "bfd.h"
  24. #include "symcat.h"
  25. #include "@prefix@-desc.h"
  26. #include "@prefix@-opc.h"
  27. #include "cgen/basic-modes.h"
  28. #include "opintl.h"
  29. #include "safe-ctype.h"
  30. #undef min
  31. #define min(a,b) ((a) < (b) ? (a) : (b))
  32. #undef max
  33. #define max(a,b) ((a) > (b) ? (a) : (b))
  34. /* Used by the ifield rtx function. */
  35. #define FLD(f) (fields->f)
  36. static const char * insert_normal
  37. (CGEN_CPU_DESC, long, unsigned int, unsigned int, unsigned int,
  38. unsigned int, unsigned int, unsigned int, CGEN_INSN_BYTES_PTR);
  39. static const char * insert_insn_normal
  40. (CGEN_CPU_DESC, const CGEN_INSN *,
  41. CGEN_FIELDS *, CGEN_INSN_BYTES_PTR, bfd_vma);
  42. static int extract_normal
  43. (CGEN_CPU_DESC, CGEN_EXTRACT_INFO *, CGEN_INSN_INT,
  44. unsigned int, unsigned int, unsigned int, unsigned int,
  45. unsigned int, unsigned int, bfd_vma, long *);
  46. static int extract_insn_normal
  47. (CGEN_CPU_DESC, const CGEN_INSN *, CGEN_EXTRACT_INFO *,
  48. CGEN_INSN_INT, CGEN_FIELDS *, bfd_vma);
  49. #if CGEN_INT_INSN_P
  50. static void put_insn_int_value
  51. (CGEN_CPU_DESC, CGEN_INSN_BYTES_PTR, int, int, CGEN_INSN_INT);
  52. #endif
  53. #if ! CGEN_INT_INSN_P
  54. static CGEN_INLINE void insert_1
  55. (CGEN_CPU_DESC, unsigned long, int, int, int, unsigned char *);
  56. static CGEN_INLINE int fill_cache
  57. (CGEN_CPU_DESC, CGEN_EXTRACT_INFO *, int, int, bfd_vma);
  58. static CGEN_INLINE long extract_1
  59. (CGEN_CPU_DESC, CGEN_EXTRACT_INFO *, int, int, int, unsigned char *, bfd_vma);
  60. #endif
  61. /* Operand insertion. */
  62. #if ! CGEN_INT_INSN_P
  63. /* Subroutine of insert_normal. */
  64. static CGEN_INLINE void
  65. insert_1 (CGEN_CPU_DESC cd,
  66. unsigned long value,
  67. int start,
  68. int length,
  69. int word_length,
  70. unsigned char *bufp)
  71. {
  72. unsigned long x, mask;
  73. int shift;
  74. x = cgen_get_insn_value (cd, bufp, word_length, cd->endian);
  75. /* Written this way to avoid undefined behaviour. */
  76. mask = (1UL << (length - 1) << 1) - 1;
  77. if (CGEN_INSN_LSB0_P)
  78. shift = (start + 1) - length;
  79. else
  80. shift = (word_length - (start + length));
  81. x = (x & ~(mask << shift)) | ((value & mask) << shift);
  82. cgen_put_insn_value (cd, bufp, word_length, (bfd_vma) x, cd->endian);
  83. }
  84. #endif /* ! CGEN_INT_INSN_P */
  85. /* Default insertion routine.
  86. ATTRS is a mask of the boolean attributes.
  87. WORD_OFFSET is the offset in bits from the start of the insn of the value.
  88. WORD_LENGTH is the length of the word in bits in which the value resides.
  89. START is the starting bit number in the word, architecture origin.
  90. LENGTH is the length of VALUE in bits.
  91. TOTAL_LENGTH is the total length of the insn in bits.
  92. The result is an error message or NULL if success. */
  93. /* ??? This duplicates functionality with bfd's howto table and
  94. bfd_install_relocation. */
  95. /* ??? This doesn't handle bfd_vma's. Create another function when
  96. necessary. */
  97. static const char *
  98. insert_normal (CGEN_CPU_DESC cd,
  99. long value,
  100. unsigned int attrs,
  101. unsigned int word_offset,
  102. unsigned int start,
  103. unsigned int length,
  104. unsigned int word_length,
  105. unsigned int total_length,
  106. CGEN_INSN_BYTES_PTR buffer)
  107. {
  108. static char errbuf[100];
  109. unsigned long mask;
  110. /* If LENGTH is zero, this operand doesn't contribute to the value. */
  111. if (length == 0)
  112. return NULL;
  113. /* Written this way to avoid undefined behaviour. */
  114. mask = (1UL << (length - 1) << 1) - 1;
  115. if (word_length > 8 * sizeof (CGEN_INSN_INT))
  116. abort ();
  117. /* For architectures with insns smaller than the base-insn-bitsize,
  118. word_length may be too big. */
  119. if (cd->min_insn_bitsize < cd->base_insn_bitsize)
  120. {
  121. if (word_offset == 0
  122. && word_length > total_length)
  123. word_length = total_length;
  124. }
  125. /* Ensure VALUE will fit. */
  126. if (CGEN_BOOL_ATTR (attrs, CGEN_IFLD_SIGN_OPT))
  127. {
  128. long minval = - (1UL << (length - 1));
  129. unsigned long maxval = mask;
  130. if ((value > 0 && (unsigned long) value > maxval)
  131. || value < minval)
  132. {
  133. /* xgettext:c-format */
  134. sprintf (errbuf,
  135. _("operand out of range (%ld not between %ld and %lu)"),
  136. value, minval, maxval);
  137. return errbuf;
  138. }
  139. }
  140. else if (! CGEN_BOOL_ATTR (attrs, CGEN_IFLD_SIGNED))
  141. {
  142. unsigned long maxval = mask;
  143. unsigned long val = (unsigned long) value;
  144. /* For hosts with a word size > 32 check to see if value has been sign
  145. extended beyond 32 bits. If so then ignore these higher sign bits
  146. as the user is attempting to store a 32-bit signed value into an
  147. unsigned 32-bit field which is allowed. */
  148. if (sizeof (unsigned long) > 4 && ((value >> 32) == -1))
  149. val &= 0xFFFFFFFF;
  150. if (val > maxval)
  151. {
  152. /* xgettext:c-format */
  153. sprintf (errbuf,
  154. _("operand out of range (0x%lx not between 0 and 0x%lx)"),
  155. val, maxval);
  156. return errbuf;
  157. }
  158. }
  159. else
  160. {
  161. if (! cgen_signed_overflow_ok_p (cd))
  162. {
  163. long minval = - (1UL << (length - 1));
  164. long maxval = (1UL << (length - 1)) - 1;
  165. if (value < minval || value > maxval)
  166. {
  167. sprintf
  168. /* xgettext:c-format */
  169. (errbuf, _("operand out of range (%ld not between %ld and %ld)"),
  170. value, minval, maxval);
  171. return errbuf;
  172. }
  173. }
  174. }
  175. #if CGEN_INT_INSN_P
  176. {
  177. int shift_within_word, shift_to_word, shift;
  178. /* How to shift the value to BIT0 of the word. */
  179. shift_to_word = total_length - (word_offset + word_length);
  180. /* How to shift the value to the field within the word. */
  181. if (CGEN_INSN_LSB0_P)
  182. shift_within_word = start + 1 - length;
  183. else
  184. shift_within_word = word_length - start - length;
  185. /* The total SHIFT, then mask in the value. */
  186. shift = shift_to_word + shift_within_word;
  187. *buffer = (*buffer & ~(mask << shift)) | ((value & mask) << shift);
  188. }
  189. #else /* ! CGEN_INT_INSN_P */
  190. {
  191. unsigned char *bufp = (unsigned char *) buffer + word_offset / 8;
  192. insert_1 (cd, value, start, length, word_length, bufp);
  193. }
  194. #endif /* ! CGEN_INT_INSN_P */
  195. return NULL;
  196. }
  197. /* Default insn builder (insert handler).
  198. The instruction is recorded in CGEN_INT_INSN_P byte order (meaning
  199. that if CGEN_INSN_BYTES_PTR is an int * and thus, the value is
  200. recorded in host byte order, otherwise BUFFER is an array of bytes
  201. and the value is recorded in target byte order).
  202. The result is an error message or NULL if success. */
  203. static const char *
  204. insert_insn_normal (CGEN_CPU_DESC cd,
  205. const CGEN_INSN * insn,
  206. CGEN_FIELDS * fields,
  207. CGEN_INSN_BYTES_PTR buffer,
  208. bfd_vma pc)
  209. {
  210. const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
  211. unsigned long value;
  212. const CGEN_SYNTAX_CHAR_TYPE * syn;
  213. CGEN_INIT_INSERT (cd);
  214. value = CGEN_INSN_BASE_VALUE (insn);
  215. /* If we're recording insns as numbers (rather than a string of bytes),
  216. target byte order handling is deferred until later. */
  217. #if CGEN_INT_INSN_P
  218. put_insn_int_value (cd, buffer, cd->base_insn_bitsize,
  219. CGEN_FIELDS_BITSIZE (fields), value);
  220. #else
  221. cgen_put_insn_value (cd, buffer, min ((unsigned) cd->base_insn_bitsize,
  222. (unsigned) CGEN_FIELDS_BITSIZE (fields)),
  223. value, cd->insn_endian);
  224. #endif /* ! CGEN_INT_INSN_P */
  225. /* ??? It would be better to scan the format's fields.
  226. Still need to be able to insert a value based on the operand though;
  227. e.g. storing a branch displacement that got resolved later.
  228. Needs more thought first. */
  229. for (syn = CGEN_SYNTAX_STRING (syntax); * syn; ++ syn)
  230. {
  231. const char *errmsg;
  232. if (CGEN_SYNTAX_CHAR_P (* syn))
  233. continue;
  234. errmsg = (* cd->insert_operand) (cd, CGEN_SYNTAX_FIELD (*syn),
  235. fields, buffer, pc);
  236. if (errmsg)
  237. return errmsg;
  238. }
  239. return NULL;
  240. }
  241. #if CGEN_INT_INSN_P
  242. /* Cover function to store an insn value into an integral insn. Must go here
  243. because it needs <prefix>-desc.h for CGEN_INT_INSN_P. */
  244. static void
  245. put_insn_int_value (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  246. CGEN_INSN_BYTES_PTR buf,
  247. int length,
  248. int insn_length,
  249. CGEN_INSN_INT value)
  250. {
  251. /* For architectures with insns smaller than the base-insn-bitsize,
  252. length may be too big. */
  253. if (length > insn_length)
  254. *buf = value;
  255. else
  256. {
  257. int shift = insn_length - length;
  258. /* Written this way to avoid undefined behaviour. */
  259. CGEN_INSN_INT mask = length == 0 ? 0 : (1UL << (length - 1) << 1) - 1;
  260. *buf = (*buf & ~(mask << shift)) | ((value & mask) << shift);
  261. }
  262. }
  263. #endif
  264. /* Operand extraction. */
  265. #if ! CGEN_INT_INSN_P
  266. /* Subroutine of extract_normal.
  267. Ensure sufficient bytes are cached in EX_INFO.
  268. OFFSET is the offset in bytes from the start of the insn of the value.
  269. BYTES is the length of the needed value.
  270. Returns 1 for success, 0 for failure. */
  271. static CGEN_INLINE int
  272. fill_cache (CGEN_CPU_DESC cd ATTRIBUTE_UNUSED,
  273. CGEN_EXTRACT_INFO *ex_info,
  274. int offset,
  275. int bytes,
  276. bfd_vma pc)
  277. {
  278. /* It's doubtful that the middle part has already been fetched so
  279. we don't optimize that case. kiss. */
  280. unsigned int mask;
  281. disassemble_info *info = (disassemble_info *) ex_info->dis_info;
  282. /* First do a quick check. */
  283. mask = (1 << bytes) - 1;
  284. if (((ex_info->valid >> offset) & mask) == mask)
  285. return 1;
  286. /* Search for the first byte we need to read. */
  287. for (mask = 1 << offset; bytes > 0; --bytes, ++offset, mask <<= 1)
  288. if (! (mask & ex_info->valid))
  289. break;
  290. if (bytes)
  291. {
  292. int status;
  293. pc += offset;
  294. status = (*info->read_memory_func)
  295. (pc, ex_info->insn_bytes + offset, bytes, info);
  296. if (status != 0)
  297. {
  298. (*info->memory_error_func) (status, pc, info);
  299. return 0;
  300. }
  301. ex_info->valid |= ((1 << bytes) - 1) << offset;
  302. }
  303. return 1;
  304. }
  305. /* Subroutine of extract_normal. */
  306. static CGEN_INLINE long
  307. extract_1 (CGEN_CPU_DESC cd,
  308. CGEN_EXTRACT_INFO *ex_info ATTRIBUTE_UNUSED,
  309. int start,
  310. int length,
  311. int word_length,
  312. unsigned char *bufp,
  313. bfd_vma pc ATTRIBUTE_UNUSED)
  314. {
  315. unsigned long x;
  316. int shift;
  317. x = cgen_get_insn_value (cd, bufp, word_length, cd->endian);
  318. if (CGEN_INSN_LSB0_P)
  319. shift = (start + 1) - length;
  320. else
  321. shift = (word_length - (start + length));
  322. return x >> shift;
  323. }
  324. #endif /* ! CGEN_INT_INSN_P */
  325. /* Default extraction routine.
  326. INSN_VALUE is the first base_insn_bitsize bits of the insn in host order,
  327. or sometimes less for cases like the m32r where the base insn size is 32
  328. but some insns are 16 bits.
  329. ATTRS is a mask of the boolean attributes. We only need `SIGNED',
  330. but for generality we take a bitmask of all of them.
  331. WORD_OFFSET is the offset in bits from the start of the insn of the value.
  332. WORD_LENGTH is the length of the word in bits in which the value resides.
  333. START is the starting bit number in the word, architecture origin.
  334. LENGTH is the length of VALUE in bits.
  335. TOTAL_LENGTH is the total length of the insn in bits.
  336. Returns 1 for success, 0 for failure. */
  337. /* ??? The return code isn't properly used. wip. */
  338. /* ??? This doesn't handle bfd_vma's. Create another function when
  339. necessary. */
  340. static int
  341. extract_normal (CGEN_CPU_DESC cd,
  342. #if ! CGEN_INT_INSN_P
  343. CGEN_EXTRACT_INFO *ex_info,
  344. #else
  345. CGEN_EXTRACT_INFO *ex_info ATTRIBUTE_UNUSED,
  346. #endif
  347. CGEN_INSN_INT insn_value,
  348. unsigned int attrs,
  349. unsigned int word_offset,
  350. unsigned int start,
  351. unsigned int length,
  352. unsigned int word_length,
  353. unsigned int total_length,
  354. #if ! CGEN_INT_INSN_P
  355. bfd_vma pc,
  356. #else
  357. bfd_vma pc ATTRIBUTE_UNUSED,
  358. #endif
  359. long *valuep)
  360. {
  361. long value, mask;
  362. /* If LENGTH is zero, this operand doesn't contribute to the value
  363. so give it a standard value of zero. */
  364. if (length == 0)
  365. {
  366. *valuep = 0;
  367. return 1;
  368. }
  369. if (word_length > 8 * sizeof (CGEN_INSN_INT))
  370. abort ();
  371. /* For architectures with insns smaller than the insn-base-bitsize,
  372. word_length may be too big. */
  373. if (cd->min_insn_bitsize < cd->base_insn_bitsize)
  374. {
  375. if (word_offset + word_length > total_length)
  376. word_length = total_length - word_offset;
  377. }
  378. /* Does the value reside in INSN_VALUE, and at the right alignment? */
  379. if (CGEN_INT_INSN_P || (word_offset == 0 && word_length == total_length))
  380. {
  381. if (CGEN_INSN_LSB0_P)
  382. value = insn_value >> ((word_offset + start + 1) - length);
  383. else
  384. value = insn_value >> (total_length - ( word_offset + start + length));
  385. }
  386. #if ! CGEN_INT_INSN_P
  387. else
  388. {
  389. unsigned char *bufp = ex_info->insn_bytes + word_offset / 8;
  390. if (word_length > 8 * sizeof (CGEN_INSN_INT))
  391. abort ();
  392. if (fill_cache (cd, ex_info, word_offset / 8, word_length / 8, pc) == 0)
  393. {
  394. *valuep = 0;
  395. return 0;
  396. }
  397. value = extract_1 (cd, ex_info, start, length, word_length, bufp, pc);
  398. }
  399. #endif /* ! CGEN_INT_INSN_P */
  400. /* Written this way to avoid undefined behaviour. */
  401. mask = (1UL << (length - 1) << 1) - 1;
  402. value &= mask;
  403. /* sign extend? */
  404. if (CGEN_BOOL_ATTR (attrs, CGEN_IFLD_SIGNED)
  405. && (value & (1UL << (length - 1))))
  406. value |= ~mask;
  407. *valuep = value;
  408. return 1;
  409. }
  410. /* Default insn extractor.
  411. INSN_VALUE is the first base_insn_bitsize bits, translated to host order.
  412. The extracted fields are stored in FIELDS.
  413. EX_INFO is used to handle reading variable length insns.
  414. Return the length of the insn in bits, or 0 if no match,
  415. or -1 if an error occurs fetching data (memory_error_func will have
  416. been called). */
  417. static int
  418. extract_insn_normal (CGEN_CPU_DESC cd,
  419. const CGEN_INSN *insn,
  420. CGEN_EXTRACT_INFO *ex_info,
  421. CGEN_INSN_INT insn_value,
  422. CGEN_FIELDS *fields,
  423. bfd_vma pc)
  424. {
  425. const CGEN_SYNTAX *syntax = CGEN_INSN_SYNTAX (insn);
  426. const CGEN_SYNTAX_CHAR_TYPE *syn;
  427. CGEN_FIELDS_BITSIZE (fields) = CGEN_INSN_BITSIZE (insn);
  428. CGEN_INIT_EXTRACT (cd);
  429. for (syn = CGEN_SYNTAX_STRING (syntax); *syn; ++syn)
  430. {
  431. int length;
  432. if (CGEN_SYNTAX_CHAR_P (*syn))
  433. continue;
  434. length = (* cd->extract_operand) (cd, CGEN_SYNTAX_FIELD (*syn),
  435. ex_info, insn_value, fields, pc);
  436. if (length <= 0)
  437. return length;
  438. }
  439. /* We recognized and successfully extracted this insn. */
  440. return CGEN_INSN_BITSIZE (insn);
  441. }
  442. /* Machine generated code added here. */