Support x86 Intel MSR_IMM
[binutils-gdb.git] / opcodes / aarch64-opc.h
blobe7495cdb5fb4cc85c4b14323c4bc57664c44f20f
1 /* aarch64-opc.h -- Header file for aarch64-opc.c and aarch64-opc-2.c.
2 Copyright (C) 2012-2024 Free Software Foundation, Inc.
3 Contributed by ARM Ltd.
5 This file is part of the GNU opcodes library.
7 This library is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 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.
17 You should have received a copy of the GNU General Public License
18 along with this program; see the file COPYING3. If not,
19 see <http://www.gnu.org/licenses/>. */
21 #ifndef OPCODES_AARCH64_OPC_H
22 #define OPCODES_AARCH64_OPC_H
24 #include <string.h>
25 #include "opcode/aarch64.h"
27 /* Instruction fields.
28 Keep this sorted alphanumerically and synced with the fields array
29 in aarch64-opc.c. */
30 enum aarch64_field_kind
32 FLD_NIL,
33 FLD_CRm,
34 FLD_CRm_dsb_nxs,
35 FLD_CRn,
36 FLD_CSSC_imm8,
37 FLD_H,
38 FLD_L,
39 FLD_LSE128_Rt,
40 FLD_LSE128_Rt2,
41 FLD_M,
42 FLD_N,
43 FLD_Q,
44 FLD_Ra,
45 FLD_Rd,
46 FLD_Rm,
47 FLD_Rn,
48 FLD_Rs,
49 FLD_Rt,
50 FLD_Rt2,
51 FLD_S,
52 FLD_SM3_imm2,
53 FLD_SME_Pdx2,
54 FLD_SME_Pm,
55 FLD_SME_PNd3,
56 FLD_SME_PNn3,
57 FLD_SME_Q,
58 FLD_SME_Rm,
59 FLD_SME_Rv,
60 FLD_SME_V,
61 FLD_SME_VL_10,
62 FLD_SME_VL_13,
63 FLD_SME_ZAda_1b,
64 FLD_SME_ZAda_2b,
65 FLD_SME_ZAda_3b,
66 FLD_SME_ZdnT,
67 FLD_SME_Zdn2,
68 FLD_SME_Zdn2_0,
69 FLD_SME_Zdn4,
70 FLD_SME_Zm,
71 FLD_SME_Zm2,
72 FLD_SME_Zm4,
73 FLD_SME_Zn2,
74 FLD_SME_Zn4,
75 FLD_SME_ZtT,
76 FLD_SME_Zt3,
77 FLD_SME_Zt2,
78 FLD_SME_i1,
79 FLD_SME_size_12,
80 FLD_SME_size_22,
81 FLD_SME_sz_23,
82 FLD_SME_tszh,
83 FLD_SME_tszl,
84 FLD_SME_zero_mask,
85 FLD_SVE_M_4,
86 FLD_SVE_M_14,
87 FLD_SVE_M_16,
88 FLD_SVE_N,
89 FLD_SVE_Pd,
90 FLD_SVE_Pg3,
91 FLD_SVE_Pg4_5,
92 FLD_SVE_Pg4_10,
93 FLD_SVE_Pg4_16,
94 FLD_SVE_Pm,
95 FLD_SVE_Pn,
96 FLD_SVE_Pt,
97 FLD_SVE_Rm,
98 FLD_SVE_Rn,
99 FLD_SVE_Vd,
100 FLD_SVE_Vm,
101 FLD_SVE_Vn,
102 FLD_SVE_Za_5,
103 FLD_SVE_Za_16,
104 FLD_SVE_Zd,
105 FLD_SVE_Zm_5,
106 FLD_SVE_Zm_16,
107 FLD_SVE_Zn,
108 FLD_SVE_Zt,
109 FLD_SVE_i1,
110 FLD_SVE_i1_23,
111 FLD_SVE_i2,
112 FLD_SVE_i2h,
113 FLD_SVE_i3h,
114 FLD_SVE_i3h2,
115 FLD_SVE_i3h3,
116 FLD_SVE_i3l,
117 FLD_SVE_i3l2,
118 FLD_SVE_i4l2,
119 FLD_SVE_imm3,
120 FLD_SVE_imm4,
121 FLD_SVE_imm5,
122 FLD_SVE_imm5b,
123 FLD_SVE_imm6,
124 FLD_SVE_imm7,
125 FLD_SVE_imm8,
126 FLD_SVE_imm9,
127 FLD_SVE_immr,
128 FLD_SVE_imms,
129 FLD_SVE_msz,
130 FLD_SVE_pattern,
131 FLD_SVE_prfop,
132 FLD_SVE_rot1,
133 FLD_SVE_rot2,
134 FLD_SVE_rot3,
135 FLD_SVE_size,
136 FLD_SVE_sz,
137 FLD_SVE_sz2,
138 FLD_SVE_tsz,
139 FLD_SVE_tszh,
140 FLD_SVE_tszl_8,
141 FLD_SVE_tszl_19,
142 FLD_SVE_xs_14,
143 FLD_SVE_xs_22,
144 FLD_S_imm10,
145 FLD_abc,
146 FLD_asisdlso_opcode,
147 FLD_b40,
148 FLD_b5,
149 FLD_cmode,
150 FLD_cond,
151 FLD_cond2,
152 FLD_defgh,
153 FLD_hw,
154 FLD_imm1_0,
155 FLD_imm1_2,
156 FLD_imm1_3,
157 FLD_imm1_8,
158 FLD_imm1_10,
159 FLD_imm1_14,
160 FLD_imm1_15,
161 FLD_imm1_16,
162 FLD_imm2_0,
163 FLD_imm2_1,
164 FLD_imm2_2,
165 FLD_imm2_8,
166 FLD_imm2_10,
167 FLD_imm2_12,
168 FLD_imm2_13,
169 FLD_imm2_15,
170 FLD_imm2_16,
171 FLD_imm2_19,
172 FLD_imm3_0,
173 FLD_imm3_5,
174 FLD_imm3_10,
175 FLD_imm3_12,
176 FLD_imm3_14,
177 FLD_imm3_15,
178 FLD_imm3_19,
179 FLD_imm4_0,
180 FLD_imm4_5,
181 FLD_imm4_10,
182 FLD_imm4_11,
183 FLD_imm4_14,
184 FLD_imm5,
185 FLD_imm6_10,
186 FLD_imm6_15,
187 FLD_imm7,
188 FLD_imm8,
189 FLD_imm9,
190 FLD_imm12,
191 FLD_imm14,
192 FLD_imm16_0,
193 FLD_imm16_5,
194 FLD_imm17_1,
195 FLD_imm17_2,
196 FLD_imm19,
197 FLD_imm26,
198 FLD_immb,
199 FLD_immh,
200 FLD_immhi,
201 FLD_immlo,
202 FLD_immr,
203 FLD_imms,
204 FLD_index,
205 FLD_index2,
206 FLD_ldst_size,
207 FLD_len,
208 FLD_lse_sz,
209 FLD_nzcv,
210 FLD_op,
211 FLD_op0,
212 FLD_op1,
213 FLD_op2,
214 FLD_opc,
215 FLD_opc1,
216 FLD_opcode,
217 FLD_option,
218 FLD_rotate1,
219 FLD_rotate2,
220 FLD_rotate3,
221 FLD_scale,
222 FLD_sf,
223 FLD_shift,
224 FLD_size,
225 FLD_sz,
226 FLD_type,
227 FLD_vldst_size,
228 FLD_off3,
229 FLD_off2,
230 FLD_ZAn_1,
231 FLD_ol,
232 FLD_ZAn_2,
233 FLD_ZAn_3,
234 FLD_ZAn,
235 FLD_opc2,
236 FLD_rcpc3_size,
237 FLD_brbop,
238 FLD_ZA8_1,
239 FLD_ZA7_2,
240 FLD_ZA6_3,
241 FLD_ZA5_4,
244 /* Field description. */
245 struct aarch64_field
247 int lsb;
248 int width;
251 typedef struct aarch64_field aarch64_field;
253 extern const aarch64_field fields[];
255 /* Operand description. */
257 struct aarch64_operand
259 enum aarch64_operand_class op_class;
261 /* Name of the operand code; used mainly for the purpose of internal
262 debugging. */
263 const char *name;
265 unsigned int flags;
267 /* The associated instruction bit-fields; no operand has more than 4
268 bit-fields */
269 enum aarch64_field_kind fields[5];
271 /* Brief description */
272 const char *desc;
275 typedef struct aarch64_operand aarch64_operand;
277 extern const aarch64_operand aarch64_operands[];
279 enum err_type
280 verify_constraints (const struct aarch64_inst *, const aarch64_insn, bfd_vma,
281 bool, aarch64_operand_error *, aarch64_instr_sequence*);
283 /* Operand flags. */
285 #define OPD_F_HAS_INSERTER 0x00000001
286 #define OPD_F_HAS_EXTRACTOR 0x00000002
287 #define OPD_F_SEXT 0x00000004 /* Require sign-extension. */
288 #define OPD_F_SHIFT_BY_2 0x00000008 /* Need to left shift the field
289 value by 2 to get the value
290 of an immediate operand. */
291 #define OPD_F_MAYBE_SP 0x00000010 /* May potentially be SP. */
292 #define OPD_F_OD_MASK 0x000001e0 /* Operand-dependent data. */
293 #define OPD_F_OD_LSB 5
294 #define OPD_F_NO_ZR 0x00000200 /* ZR index not allowed. */
295 #define OPD_F_SHIFT_BY_3 0x00000400 /* Need to left shift the field
296 value by 3 to get the value
297 of an immediate operand. */
298 #define OPD_F_SHIFT_BY_4 0x00000800 /* Need to left shift the field
299 value by 4 to get the value
300 of an immediate operand. */
301 #define OPD_F_UNSIGNED 0x00001000 /* Expect an unsigned value. */
304 /* Register flags. */
306 #undef F_DEPRECATED
307 #define F_DEPRECATED (1 << 0) /* Deprecated system register. */
309 #undef F_ARCHEXT
310 #define F_ARCHEXT (1 << 1) /* Architecture dependent system register. */
312 #undef F_HASXT
313 #define F_HASXT (1 << 2) /* System instruction register <Xt>
314 operand. */
316 #undef F_REG_READ
317 #define F_REG_READ (1 << 3) /* Register can only be used to read values
318 out of. */
320 #undef F_REG_WRITE
321 #define F_REG_WRITE (1 << 4) /* Register can only be written to but not
322 read from. */
324 #undef F_REG_IN_CRM
325 #define F_REG_IN_CRM (1 << 5) /* Register extra encoding in CRm. */
327 #undef F_REG_ALIAS
328 #define F_REG_ALIAS (1 << 6) /* Register name aliases another. */
330 #undef F_REG_128
331 #define F_REG_128 (1 << 7) /* System regsister implementable as 128-bit wide. */
334 /* PSTATE field name for the MSR instruction this is encoded in "op1:op2:CRm".
335 Part of CRm can be used to encode <pstatefield>. E.g. CRm[3:1] for SME.
336 In order to set/get full PSTATE field name use flag F_REG_IN_CRM and below
337 macros to encode and decode CRm encoding.
339 #define PSTATE_ENCODE_CRM(val) (val << 6)
340 #define PSTATE_DECODE_CRM(flags) ((flags >> 6) & 0x0f)
342 #undef F_IMM_IN_CRM
343 #define F_IMM_IN_CRM (1 << 10) /* Immediate extra encoding in CRm. */
345 /* Also CRm may contain, in addition to <pstatefield> immediate.
346 E.g. CRm[0] <imm1> at bit 0 for SME. Use below macros to encode and decode
347 immediate mask.
349 #define PSTATE_ENCODE_CRM_IMM(mask) (mask << 11)
350 #define PSTATE_DECODE_CRM_IMM(mask) ((mask >> 11) & 0x0f)
352 /* Helper macro to ENCODE CRm and its immediate. */
353 #define PSTATE_ENCODE_CRM_AND_IMM(CVAL,IMASK) \
354 (F_REG_IN_CRM | PSTATE_ENCODE_CRM(CVAL) \
355 | F_IMM_IN_CRM | PSTATE_ENCODE_CRM_IMM(IMASK))
357 /* Bits [15, 18] contain the maximum value for an immediate MSR. */
358 #define F_REG_MAX_VALUE(X) ((X) << 15)
359 #define F_GET_REG_MAX_VALUE(X) (((X) >> 15) & 0x0f)
361 /* HINT operand flags. */
362 #define HINT_OPD_F_NOPRINT (1 << 0) /* Should not be printed. */
364 /* Encode 7-bit HINT #imm in the lower 8 bits. Use higher bits for flags. */
365 #define HINT_ENCODE(flag, val) ((flag << 8) | val)
366 #define HINT_FLAG(val) (val >> 8)
367 #define HINT_VAL(val) (val & 0xff)
369 static inline bool
370 operand_has_inserter (const aarch64_operand *operand)
372 return (operand->flags & OPD_F_HAS_INSERTER) != 0;
375 static inline bool
376 operand_has_extractor (const aarch64_operand *operand)
378 return (operand->flags & OPD_F_HAS_EXTRACTOR) != 0;
381 static inline bool
382 operand_need_sign_extension (const aarch64_operand *operand)
384 return (operand->flags & OPD_F_SEXT) != 0;
387 static inline bool
388 operand_need_shift_by_two (const aarch64_operand *operand)
390 return (operand->flags & OPD_F_SHIFT_BY_2) != 0;
393 static inline bool
394 operand_need_shift_by_three (const aarch64_operand *operand)
396 return (operand->flags & OPD_F_SHIFT_BY_3) != 0;
399 static inline bool
400 operand_need_shift_by_four (const aarch64_operand *operand)
402 return (operand->flags & OPD_F_SHIFT_BY_4) != 0;
405 static inline bool
406 operand_need_unsigned_offset (const aarch64_operand *operand)
408 return (operand->flags & OPD_F_UNSIGNED) != 0;
411 static inline bool
412 operand_maybe_stack_pointer (const aarch64_operand *operand)
414 return (operand->flags & OPD_F_MAYBE_SP) != 0;
417 /* Return the value of the operand-specific data field (OPD_F_OD_MASK). */
418 static inline unsigned int
419 get_operand_specific_data (const aarch64_operand *operand)
421 return (operand->flags & OPD_F_OD_MASK) >> OPD_F_OD_LSB;
424 /* Return the width of field number N of operand *OPERAND. */
425 static inline unsigned
426 get_operand_field_width (const aarch64_operand *operand, unsigned n)
428 assert (operand->fields[n] != FLD_NIL);
429 return fields[operand->fields[n]].width;
432 /* Return the total width of the operand *OPERAND. */
433 static inline unsigned
434 get_operand_fields_width (const aarch64_operand *operand)
436 int i = 0;
437 unsigned width = 0;
438 while (operand->fields[i] != FLD_NIL)
439 width += fields[operand->fields[i++]].width;
440 assert (width > 0 && width < 32);
441 return width;
444 static inline const aarch64_operand *
445 get_operand_from_code (enum aarch64_opnd code)
447 return aarch64_operands + code;
450 /* Operand qualifier and operand constraint checking. */
452 bool aarch64_match_operands_constraint (aarch64_inst *,
453 aarch64_operand_error *);
455 /* Operand qualifier related functions. */
456 const char* aarch64_get_qualifier_name (aarch64_opnd_qualifier_t);
457 unsigned char aarch64_get_qualifier_nelem (aarch64_opnd_qualifier_t);
458 aarch64_insn aarch64_get_qualifier_standard_value (aarch64_opnd_qualifier_t);
459 int aarch64_find_best_match (const aarch64_inst *,
460 const aarch64_opnd_qualifier_seq_t *,
461 int, aarch64_opnd_qualifier_t *, int *);
463 static inline void
464 reset_operand_qualifier (aarch64_inst *inst, int idx)
466 assert (idx >=0 && idx < aarch64_num_of_operands (inst->opcode));
467 inst->operands[idx].qualifier = AARCH64_OPND_QLF_NIL;
470 /* Inline functions operating on instruction bit-field(s). */
472 /* Generate a mask that has WIDTH number of consecutive 1s. */
474 static inline aarch64_insn
475 gen_mask (int width)
477 return ((aarch64_insn) 1 << width) - 1;
480 /* LSB_REL is the relative location of the lsb in the sub field, starting from 0. */
481 static inline int
482 gen_sub_field (enum aarch64_field_kind kind, int lsb_rel, int width, aarch64_field *ret)
484 const aarch64_field *field = &fields[kind];
485 if (lsb_rel < 0 || width <= 0 || lsb_rel + width > field->width)
486 return 0;
487 ret->lsb = field->lsb + lsb_rel;
488 ret->width = width;
489 return 1;
492 /* Insert VALUE into FIELD of CODE. MASK can be zero or the base mask
493 of the opcode. */
495 static inline void
496 insert_field_2 (const aarch64_field *field, aarch64_insn *code,
497 aarch64_insn value, aarch64_insn mask)
499 assert (field->width < 32 && field->width >= 1 && field->lsb >= 0
500 && field->lsb + field->width <= 32);
501 value &= gen_mask (field->width);
502 value <<= field->lsb;
503 /* In some opcodes, field can be part of the base opcode, e.g. the size
504 field in FADD. The following helps avoid corrupt the base opcode. */
505 value &= ~mask;
506 *code |= value;
509 /* Extract FIELD of CODE and return the value. MASK can be zero or the base
510 mask of the opcode. */
512 static inline aarch64_insn
513 extract_field_2 (const aarch64_field *field, aarch64_insn code,
514 aarch64_insn mask)
516 aarch64_insn value;
517 /* Clear any bit that is a part of the base opcode. */
518 code &= ~mask;
519 value = (code >> field->lsb) & gen_mask (field->width);
520 return value;
523 /* Insert VALUE into field KIND of CODE. MASK can be zero or the base mask
524 of the opcode. */
526 static inline void
527 insert_field (enum aarch64_field_kind kind, aarch64_insn *code,
528 aarch64_insn value, aarch64_insn mask)
530 insert_field_2 (&fields[kind], code, value, mask);
533 /* Extract field KIND of CODE and return the value. MASK can be zero or the
534 base mask of the opcode. */
536 static inline aarch64_insn
537 extract_field (enum aarch64_field_kind kind, aarch64_insn code,
538 aarch64_insn mask)
540 return extract_field_2 (&fields[kind], code, mask);
543 extern aarch64_insn
544 extract_fields (aarch64_insn code, aarch64_insn mask, ...);
546 /* Inline functions selecting operand to do the encoding/decoding for a
547 certain instruction bit-field. */
549 /* Select the operand to do the encoding/decoding of the 'sf' field.
550 The heuristic-based rule is that the result operand is respected more. */
552 static inline int
553 select_operand_for_sf_field_coding (const aarch64_opcode *opcode)
555 int idx = -1;
556 if (aarch64_get_operand_class (opcode->operands[0])
557 == AARCH64_OPND_CLASS_INT_REG)
558 /* normal case. */
559 idx = 0;
560 else if (aarch64_get_operand_class (opcode->operands[1])
561 == AARCH64_OPND_CLASS_INT_REG)
562 /* e.g. float2fix. */
563 idx = 1;
564 else
565 { assert (0); abort (); }
566 return idx;
569 /* Select the operand to do the encoding/decoding of the 'type' field in
570 the floating-point instructions.
571 The heuristic-based rule is that the source operand is respected more. */
573 static inline int
574 select_operand_for_fptype_field_coding (const aarch64_opcode *opcode)
576 int idx;
577 if (aarch64_get_operand_class (opcode->operands[1])
578 == AARCH64_OPND_CLASS_FP_REG)
579 /* normal case. */
580 idx = 1;
581 else if (aarch64_get_operand_class (opcode->operands[0])
582 == AARCH64_OPND_CLASS_FP_REG)
583 /* e.g. float2fix. */
584 idx = 0;
585 else
586 { assert (0); abort (); }
587 return idx;
590 /* Select the operand to do the encoding/decoding of the 'size' field in
591 the AdvSIMD scalar instructions.
592 The heuristic-based rule is that the destination operand is respected
593 more. */
595 static inline int
596 select_operand_for_scalar_size_field_coding (const aarch64_opcode *opcode)
598 int src_size = 0, dst_size = 0;
599 if (aarch64_get_operand_class (opcode->operands[0])
600 == AARCH64_OPND_CLASS_SISD_REG)
601 dst_size = aarch64_get_qualifier_esize (opcode->qualifiers_list[0][0]);
602 if (aarch64_get_operand_class (opcode->operands[1])
603 == AARCH64_OPND_CLASS_SISD_REG)
604 src_size = aarch64_get_qualifier_esize (opcode->qualifiers_list[0][1]);
605 if (src_size == dst_size && src_size == 0)
606 { assert (0); abort (); }
607 /* When the result is not a sisd register or it is a long operantion. */
608 if (dst_size == 0 || dst_size == src_size << 1)
609 return 1;
610 else
611 return 0;
614 /* Select the operand to do the encoding/decoding of the 'size:Q' fields in
615 the AdvSIMD instructions. */
617 int aarch64_select_operand_for_sizeq_field_coding (const aarch64_opcode *);
619 /* Miscellaneous. */
621 aarch64_insn aarch64_get_operand_modifier_value (enum aarch64_modifier_kind);
622 enum aarch64_modifier_kind
623 aarch64_get_operand_modifier_from_value (aarch64_insn, bool);
626 bool aarch64_wide_constant_p (uint64_t, int, unsigned int *);
627 bool aarch64_logical_immediate_p (uint64_t, int, aarch64_insn *);
628 int aarch64_shrink_expanded_imm8 (uint64_t);
630 /* Copy the content of INST->OPERANDS[SRC] to INST->OPERANDS[DST]. */
631 static inline void
632 copy_operand_info (aarch64_inst *inst, int dst, int src)
634 assert (dst >= 0 && src >= 0 && dst < AARCH64_MAX_OPND_NUM
635 && src < AARCH64_MAX_OPND_NUM);
636 memcpy (&inst->operands[dst], &inst->operands[src],
637 sizeof (aarch64_opnd_info));
638 inst->operands[dst].idx = dst;
641 /* A primitive log caculator. */
643 static inline unsigned int
644 get_logsz (unsigned int size)
646 const unsigned char ls[16] =
647 {0, 1, -1, 2, -1, -1, -1, 3, -1, -1, -1, -1, -1, -1, -1, 4};
648 if (size > 16)
650 assert (0);
651 return -1;
653 assert (ls[size - 1] != (unsigned char)-1);
654 return ls[size - 1];
657 #endif /* OPCODES_AARCH64_OPC_H */