Replace "the the" with "the"
[external/binutils.git] / opcodes / aarch64-opc.h
1 /* aarch64-opc.h -- Header file for aarch64-opc.c and aarch64-opc-2.c.
2    Copyright (C) 2012-2019 Free Software Foundation, Inc.
3    Contributed by ARM Ltd.
4
5    This file is part of the GNU opcodes library.
6
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.
11
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
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/>.  */
20
21 #ifndef OPCODES_AARCH64_OPC_H
22 #define OPCODES_AARCH64_OPC_H
23
24 #include <string.h>
25 #include "opcode/aarch64.h"
26
27 /* Instruction fields.
28    Keep synced with fields.  */
29 enum aarch64_field_kind
30 {
31   FLD_NIL,
32   FLD_cond2,
33   FLD_nzcv,
34   FLD_defgh,
35   FLD_abc,
36   FLD_imm19,
37   FLD_immhi,
38   FLD_immlo,
39   FLD_size,
40   FLD_vldst_size,
41   FLD_op,
42   FLD_Q,
43   FLD_Rt,
44   FLD_Rd,
45   FLD_Rn,
46   FLD_Rt2,
47   FLD_Ra,
48   FLD_op2,
49   FLD_CRm,
50   FLD_CRn,
51   FLD_op1,
52   FLD_op0,
53   FLD_imm3,
54   FLD_cond,
55   FLD_opcode,
56   FLD_cmode,
57   FLD_asisdlso_opcode,
58   FLD_len,
59   FLD_Rm,
60   FLD_Rs,
61   FLD_option,
62   FLD_S,
63   FLD_hw,
64   FLD_opc,
65   FLD_opc1,
66   FLD_shift,
67   FLD_type,
68   FLD_ldst_size,
69   FLD_imm6,
70   FLD_imm6_2,
71   FLD_imm4,
72   FLD_imm4_2,
73   FLD_imm4_3,
74   FLD_imm5,
75   FLD_imm7,
76   FLD_imm8,
77   FLD_imm9,
78   FLD_imm12,
79   FLD_imm14,
80   FLD_imm16,
81   FLD_imm26,
82   FLD_imms,
83   FLD_immr,
84   FLD_immb,
85   FLD_immh,
86   FLD_S_imm10,
87   FLD_N,
88   FLD_index,
89   FLD_index2,
90   FLD_sf,
91   FLD_lse_sz,
92   FLD_H,
93   FLD_L,
94   FLD_M,
95   FLD_b5,
96   FLD_b40,
97   FLD_scale,
98   FLD_SVE_M_4,
99   FLD_SVE_M_14,
100   FLD_SVE_M_16,
101   FLD_SVE_N,
102   FLD_SVE_Pd,
103   FLD_SVE_Pg3,
104   FLD_SVE_Pg4_5,
105   FLD_SVE_Pg4_10,
106   FLD_SVE_Pg4_16,
107   FLD_SVE_Pm,
108   FLD_SVE_Pn,
109   FLD_SVE_Pt,
110   FLD_SVE_Rm,
111   FLD_SVE_Rn,
112   FLD_SVE_Vd,
113   FLD_SVE_Vm,
114   FLD_SVE_Vn,
115   FLD_SVE_Za_5,
116   FLD_SVE_Za_16,
117   FLD_SVE_Zd,
118   FLD_SVE_Zm_5,
119   FLD_SVE_Zm_16,
120   FLD_SVE_Zn,
121   FLD_SVE_Zt,
122   FLD_SVE_i1,
123   FLD_SVE_i3h,
124   FLD_SVE_imm3,
125   FLD_SVE_imm4,
126   FLD_SVE_imm5,
127   FLD_SVE_imm5b,
128   FLD_SVE_imm6,
129   FLD_SVE_imm7,
130   FLD_SVE_imm8,
131   FLD_SVE_imm9,
132   FLD_SVE_immr,
133   FLD_SVE_imms,
134   FLD_SVE_msz,
135   FLD_SVE_pattern,
136   FLD_SVE_prfop,
137   FLD_SVE_rot1,
138   FLD_SVE_rot2,
139   FLD_SVE_sz,
140   FLD_SVE_tsz,
141   FLD_SVE_tszh,
142   FLD_SVE_tszl_8,
143   FLD_SVE_tszl_19,
144   FLD_SVE_xs_14,
145   FLD_SVE_xs_22,
146   FLD_rotate1,
147   FLD_rotate2,
148   FLD_rotate3,
149   FLD_SM3_imm2
150 };
151
152 /* Field description.  */
153 struct aarch64_field
154 {
155   int lsb;
156   int width;
157 };
158
159 typedef struct aarch64_field aarch64_field;
160
161 extern const aarch64_field fields[];
162 \f
163 /* Operand description.  */
164
165 struct aarch64_operand
166 {
167   enum aarch64_operand_class op_class;
168
169   /* Name of the operand code; used mainly for the purpose of internal
170      debugging.  */
171   const char *name;
172
173   unsigned int flags;
174
175   /* The associated instruction bit-fields; no operand has more than 4
176      bit-fields */
177   enum aarch64_field_kind fields[4];
178
179   /* Brief description */
180   const char *desc;
181 };
182
183 typedef struct aarch64_operand aarch64_operand;
184
185 extern const aarch64_operand aarch64_operands[];
186
187 enum err_type
188 verify_constraints (const struct aarch64_inst *, const aarch64_insn, bfd_vma,
189                     bfd_boolean, aarch64_operand_error *, aarch64_instr_sequence*);
190
191 /* Operand flags.  */
192
193 #define OPD_F_HAS_INSERTER      0x00000001
194 #define OPD_F_HAS_EXTRACTOR     0x00000002
195 #define OPD_F_SEXT              0x00000004      /* Require sign-extension.  */
196 #define OPD_F_SHIFT_BY_2        0x00000008      /* Need to left shift the field
197                                                    value by 2 to get the value
198                                                    of an immediate operand.  */
199 #define OPD_F_MAYBE_SP          0x00000010      /* May potentially be SP.  */
200 #define OPD_F_OD_MASK           0x000000e0      /* Operand-dependent data.  */
201 #define OPD_F_OD_LSB            5
202 #define OPD_F_NO_ZR             0x00000100      /* ZR index not allowed.  */
203 #define OPD_F_SHIFT_BY_4        0x00000200      /* Need to left shift the field
204                                                    value by 4 to get the value
205                                                    of an immediate operand.  */
206
207
208 /* Register flags.  */
209
210 #undef F_DEPRECATED
211 #define F_DEPRECATED    (1 << 0)  /* Deprecated system register.  */
212
213 #undef F_ARCHEXT
214 #define F_ARCHEXT       (1 << 1)  /* Architecture dependent system register.  */
215
216 #undef F_HASXT
217 #define F_HASXT         (1 << 2)  /* System instruction register <Xt>
218                                      operand.  */
219
220 #undef F_REG_READ
221 #define F_REG_READ      (1 << 3)  /* Register can only be used to read values
222                                      out of.  */
223
224 #undef F_REG_WRITE
225 #define F_REG_WRITE     (1 << 4)  /* Register can only be written to but not
226                                      read from.  */
227
228 /* HINT operand flags.  */
229 #define HINT_OPD_F_NOPRINT      (1 << 0)  /* Should not be printed.  */
230
231 /* Encode 7-bit HINT #imm in the lower 8 bits.  Use higher bits for flags.  */
232 #define HINT_ENCODE(flag, val) ((flag << 8) | val)
233 #define HINT_FLAG(val) (val >> 8)
234 #define HINT_VAL(val) (val & 0xff)
235
236 static inline bfd_boolean
237 operand_has_inserter (const aarch64_operand *operand)
238 {
239   return (operand->flags & OPD_F_HAS_INSERTER) ? TRUE : FALSE;
240 }
241
242 static inline bfd_boolean
243 operand_has_extractor (const aarch64_operand *operand)
244 {
245   return (operand->flags & OPD_F_HAS_EXTRACTOR) ? TRUE : FALSE;
246 }
247
248 static inline bfd_boolean
249 operand_need_sign_extension (const aarch64_operand *operand)
250 {
251   return (operand->flags & OPD_F_SEXT) ? TRUE : FALSE;
252 }
253
254 static inline bfd_boolean
255 operand_need_shift_by_two (const aarch64_operand *operand)
256 {
257   return (operand->flags & OPD_F_SHIFT_BY_2) ? TRUE : FALSE;
258 }
259
260 static inline bfd_boolean
261 operand_need_shift_by_four (const aarch64_operand *operand)
262 {
263   return (operand->flags & OPD_F_SHIFT_BY_4) ? TRUE : FALSE;
264 }
265
266 static inline bfd_boolean
267 operand_maybe_stack_pointer (const aarch64_operand *operand)
268 {
269   return (operand->flags & OPD_F_MAYBE_SP) ? TRUE : FALSE;
270 }
271
272 /* Return the value of the operand-specific data field (OPD_F_OD_MASK).  */
273 static inline unsigned int
274 get_operand_specific_data (const aarch64_operand *operand)
275 {
276   return (operand->flags & OPD_F_OD_MASK) >> OPD_F_OD_LSB;
277 }
278
279 /* Return the width of field number N of operand *OPERAND.  */
280 static inline unsigned
281 get_operand_field_width (const aarch64_operand *operand, unsigned n)
282 {
283   assert (operand->fields[n] != FLD_NIL);
284   return fields[operand->fields[n]].width;
285 }
286
287 /* Return the total width of the operand *OPERAND.  */
288 static inline unsigned
289 get_operand_fields_width (const aarch64_operand *operand)
290 {
291   int i = 0;
292   unsigned width = 0;
293   while (operand->fields[i] != FLD_NIL)
294     width += fields[operand->fields[i++]].width;
295   assert (width > 0 && width < 32);
296   return width;
297 }
298
299 static inline const aarch64_operand *
300 get_operand_from_code (enum aarch64_opnd code)
301 {
302   return aarch64_operands + code;
303 }
304 \f
305 /* Operand qualifier and operand constraint checking.  */
306
307 int aarch64_match_operands_constraint (aarch64_inst *,
308                                        aarch64_operand_error *);
309
310 /* Operand qualifier related functions.  */
311 const char* aarch64_get_qualifier_name (aarch64_opnd_qualifier_t);
312 unsigned char aarch64_get_qualifier_nelem (aarch64_opnd_qualifier_t);
313 aarch64_insn aarch64_get_qualifier_standard_value (aarch64_opnd_qualifier_t);
314 int aarch64_find_best_match (const aarch64_inst *,
315                              const aarch64_opnd_qualifier_seq_t *,
316                              int, aarch64_opnd_qualifier_t *);
317
318 static inline void
319 reset_operand_qualifier (aarch64_inst *inst, int idx)
320 {
321   assert (idx >=0 && idx < aarch64_num_of_operands (inst->opcode));
322   inst->operands[idx].qualifier = AARCH64_OPND_QLF_NIL;
323 }
324 \f
325 /* Inline functions operating on instruction bit-field(s).  */
326
327 /* Generate a mask that has WIDTH number of consecutive 1s.  */
328
329 static inline aarch64_insn
330 gen_mask (int width)
331 {
332   return ((aarch64_insn) 1 << width) - 1;
333 }
334
335 /* LSB_REL is the relative location of the lsb in the sub field, starting from 0.  */
336 static inline int
337 gen_sub_field (enum aarch64_field_kind kind, int lsb_rel, int width, aarch64_field *ret)
338 {
339   const aarch64_field *field = &fields[kind];
340   if (lsb_rel < 0 || width <= 0 || lsb_rel + width > field->width)
341     return 0;
342   ret->lsb = field->lsb + lsb_rel;
343   ret->width = width;
344   return 1;
345 }
346
347 /* Insert VALUE into FIELD of CODE.  MASK can be zero or the base mask
348    of the opcode.  */
349
350 static inline void
351 insert_field_2 (const aarch64_field *field, aarch64_insn *code,
352                 aarch64_insn value, aarch64_insn mask)
353 {
354   assert (field->width < 32 && field->width >= 1 && field->lsb >= 0
355           && field->lsb + field->width <= 32);
356   value &= gen_mask (field->width);
357   value <<= field->lsb;
358   /* In some opcodes, field can be part of the base opcode, e.g. the size
359      field in FADD.  The following helps avoid corrupt the base opcode.  */
360   value &= ~mask;
361   *code |= value;
362 }
363
364 /* Extract FIELD of CODE and return the value.  MASK can be zero or the base
365    mask of the opcode.  */
366
367 static inline aarch64_insn
368 extract_field_2 (const aarch64_field *field, aarch64_insn code,
369                  aarch64_insn mask)
370 {
371   aarch64_insn value;
372   /* Clear any bit that is a part of the base opcode.  */
373   code &= ~mask;
374   value = (code >> field->lsb) & gen_mask (field->width);
375   return value;
376 }
377
378 /* Insert VALUE into field KIND of CODE.  MASK can be zero or the base mask
379    of the opcode.  */
380
381 static inline void
382 insert_field (enum aarch64_field_kind kind, aarch64_insn *code,
383               aarch64_insn value, aarch64_insn mask)
384 {
385   insert_field_2 (&fields[kind], code, value, mask);
386 }
387
388 /* Extract field KIND of CODE and return the value.  MASK can be zero or the
389    base mask of the opcode.  */
390
391 static inline aarch64_insn
392 extract_field (enum aarch64_field_kind kind, aarch64_insn code,
393                aarch64_insn mask)
394 {
395   return extract_field_2 (&fields[kind], code, mask);
396 }
397
398 extern aarch64_insn
399 extract_fields (aarch64_insn code, aarch64_insn mask, ...);
400 \f
401 /* Inline functions selecting operand to do the encoding/decoding for a
402    certain instruction bit-field.  */
403
404 /* Select the operand to do the encoding/decoding of the 'sf' field.
405    The heuristic-based rule is that the result operand is respected more.  */
406
407 static inline int
408 select_operand_for_sf_field_coding (const aarch64_opcode *opcode)
409 {
410   int idx = -1;
411   if (aarch64_get_operand_class (opcode->operands[0])
412       == AARCH64_OPND_CLASS_INT_REG)
413     /* normal case.  */
414     idx = 0;
415   else if (aarch64_get_operand_class (opcode->operands[1])
416            == AARCH64_OPND_CLASS_INT_REG)
417     /* e.g. float2fix.  */
418     idx = 1;
419   else
420     { assert (0); abort (); }
421   return idx;
422 }
423
424 /* Select the operand to do the encoding/decoding of the 'type' field in
425    the floating-point instructions.
426    The heuristic-based rule is that the source operand is respected more.  */
427
428 static inline int
429 select_operand_for_fptype_field_coding (const aarch64_opcode *opcode)
430 {
431   int idx;
432   if (aarch64_get_operand_class (opcode->operands[1])
433       == AARCH64_OPND_CLASS_FP_REG)
434     /* normal case.  */
435     idx = 1;
436   else if (aarch64_get_operand_class (opcode->operands[0])
437            == AARCH64_OPND_CLASS_FP_REG)
438     /* e.g. float2fix.  */
439     idx = 0;
440   else
441     { assert (0); abort (); }
442   return idx;
443 }
444
445 /* Select the operand to do the encoding/decoding of the 'size' field in
446    the AdvSIMD scalar instructions.
447    The heuristic-based rule is that the destination operand is respected
448    more.  */
449
450 static inline int
451 select_operand_for_scalar_size_field_coding (const aarch64_opcode *opcode)
452 {
453   int src_size = 0, dst_size = 0;
454   if (aarch64_get_operand_class (opcode->operands[0])
455       == AARCH64_OPND_CLASS_SISD_REG)
456     dst_size = aarch64_get_qualifier_esize (opcode->qualifiers_list[0][0]);
457   if (aarch64_get_operand_class (opcode->operands[1])
458       == AARCH64_OPND_CLASS_SISD_REG)
459     src_size = aarch64_get_qualifier_esize (opcode->qualifiers_list[0][1]);
460   if (src_size == dst_size && src_size == 0)
461     { assert (0); abort (); }
462   /* When the result is not a sisd register or it is a long operantion.  */
463   if (dst_size == 0 || dst_size == src_size << 1)
464     return 1;
465   else
466     return 0;
467 }
468
469 /* Select the operand to do the encoding/decoding of the 'size:Q' fields in
470    the AdvSIMD instructions.  */
471
472 int aarch64_select_operand_for_sizeq_field_coding (const aarch64_opcode *);
473 \f
474 /* Miscellaneous.  */
475
476 aarch64_insn aarch64_get_operand_modifier_value (enum aarch64_modifier_kind);
477 enum aarch64_modifier_kind
478 aarch64_get_operand_modifier_from_value (aarch64_insn, bfd_boolean);
479
480
481 bfd_boolean aarch64_wide_constant_p (int64_t, int, unsigned int *);
482 bfd_boolean aarch64_logical_immediate_p (uint64_t, int, aarch64_insn *);
483 int aarch64_shrink_expanded_imm8 (uint64_t);
484
485 /* Copy the content of INST->OPERANDS[SRC] to INST->OPERANDS[DST].  */
486 static inline void
487 copy_operand_info (aarch64_inst *inst, int dst, int src)
488 {
489   assert (dst >= 0 && src >= 0 && dst < AARCH64_MAX_OPND_NUM
490           && src < AARCH64_MAX_OPND_NUM);
491   memcpy (&inst->operands[dst], &inst->operands[src],
492           sizeof (aarch64_opnd_info));
493   inst->operands[dst].idx = dst;
494 }
495
496 /* A primitive log caculator.  */
497
498 static inline unsigned int
499 get_logsz (unsigned int size)
500 {
501   const unsigned char ls[16] =
502     {0, 1, -1, 2, -1, -1, -1, 3, -1, -1, -1, -1, -1, -1, -1, 4};
503   if (size > 16)
504     {
505       assert (0);
506       return -1;
507     }
508   assert (ls[size - 1] != (unsigned char)-1);
509   return ls[size - 1];
510 }
511
512 #endif /* OPCODES_AARCH64_OPC_H */