forked from bminor/binutils-gdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaarch64-opc.c
4318 lines (3948 loc) · 136 KB
/
aarch64-opc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* aarch64-opc.c -- AArch64 opcode support.
Copyright (C) 2009-2016 Free Software Foundation, Inc.
Contributed by ARM Ltd.
This file is part of the GNU opcodes library.
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING3. If not,
see <http://www.gnu.org/licenses/>. */
#include "sysdep.h"
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <inttypes.h>
#include "opintl.h"
#include "libiberty.h"
#include "aarch64-opc.h"
#ifdef DEBUG_AARCH64
int debug_dump = FALSE;
#endif /* DEBUG_AARCH64 */
/* The enumeration strings associated with each value of a 5-bit SVE
pattern operand. A null entry indicates a reserved meaning. */
const char *const aarch64_sve_pattern_array[32] = {
/* 0-7. */
"pow2",
"vl1",
"vl2",
"vl3",
"vl4",
"vl5",
"vl6",
"vl7",
/* 8-15. */
"vl8",
"vl16",
"vl32",
"vl64",
"vl128",
"vl256",
0,
0,
/* 16-23. */
0,
0,
0,
0,
0,
0,
0,
0,
/* 24-31. */
0,
0,
0,
0,
0,
"mul4",
"mul3",
"all"
};
/* The enumeration strings associated with each value of a 4-bit SVE
prefetch operand. A null entry indicates a reserved meaning. */
const char *const aarch64_sve_prfop_array[16] = {
/* 0-7. */
"pldl1keep",
"pldl1strm",
"pldl2keep",
"pldl2strm",
"pldl3keep",
"pldl3strm",
0,
0,
/* 8-15. */
"pstl1keep",
"pstl1strm",
"pstl2keep",
"pstl2strm",
"pstl3keep",
"pstl3strm",
0,
0
};
/* Helper functions to determine which operand to be used to encode/decode
the size:Q fields for AdvSIMD instructions. */
static inline bfd_boolean
vector_qualifier_p (enum aarch64_opnd_qualifier qualifier)
{
return ((qualifier >= AARCH64_OPND_QLF_V_8B
&& qualifier <= AARCH64_OPND_QLF_V_1Q) ? TRUE
: FALSE);
}
static inline bfd_boolean
fp_qualifier_p (enum aarch64_opnd_qualifier qualifier)
{
return ((qualifier >= AARCH64_OPND_QLF_S_B
&& qualifier <= AARCH64_OPND_QLF_S_Q) ? TRUE
: FALSE);
}
enum data_pattern
{
DP_UNKNOWN,
DP_VECTOR_3SAME,
DP_VECTOR_LONG,
DP_VECTOR_WIDE,
DP_VECTOR_ACROSS_LANES,
};
static const char significant_operand_index [] =
{
0, /* DP_UNKNOWN, by default using operand 0. */
0, /* DP_VECTOR_3SAME */
1, /* DP_VECTOR_LONG */
2, /* DP_VECTOR_WIDE */
1, /* DP_VECTOR_ACROSS_LANES */
};
/* Given a sequence of qualifiers in QUALIFIERS, determine and return
the data pattern.
N.B. QUALIFIERS is a possible sequence of qualifiers each of which
corresponds to one of a sequence of operands. */
static enum data_pattern
get_data_pattern (const aarch64_opnd_qualifier_seq_t qualifiers)
{
if (vector_qualifier_p (qualifiers[0]) == TRUE)
{
/* e.g. v.4s, v.4s, v.4s
or v.4h, v.4h, v.h[3]. */
if (qualifiers[0] == qualifiers[1]
&& vector_qualifier_p (qualifiers[2]) == TRUE
&& (aarch64_get_qualifier_esize (qualifiers[0])
== aarch64_get_qualifier_esize (qualifiers[1]))
&& (aarch64_get_qualifier_esize (qualifiers[0])
== aarch64_get_qualifier_esize (qualifiers[2])))
return DP_VECTOR_3SAME;
/* e.g. v.8h, v.8b, v.8b.
or v.4s, v.4h, v.h[2].
or v.8h, v.16b. */
if (vector_qualifier_p (qualifiers[1]) == TRUE
&& aarch64_get_qualifier_esize (qualifiers[0]) != 0
&& (aarch64_get_qualifier_esize (qualifiers[0])
== aarch64_get_qualifier_esize (qualifiers[1]) << 1))
return DP_VECTOR_LONG;
/* e.g. v.8h, v.8h, v.8b. */
if (qualifiers[0] == qualifiers[1]
&& vector_qualifier_p (qualifiers[2]) == TRUE
&& aarch64_get_qualifier_esize (qualifiers[0]) != 0
&& (aarch64_get_qualifier_esize (qualifiers[0])
== aarch64_get_qualifier_esize (qualifiers[2]) << 1)
&& (aarch64_get_qualifier_esize (qualifiers[0])
== aarch64_get_qualifier_esize (qualifiers[1])))
return DP_VECTOR_WIDE;
}
else if (fp_qualifier_p (qualifiers[0]) == TRUE)
{
/* e.g. SADDLV <V><d>, <Vn>.<T>. */
if (vector_qualifier_p (qualifiers[1]) == TRUE
&& qualifiers[2] == AARCH64_OPND_QLF_NIL)
return DP_VECTOR_ACROSS_LANES;
}
return DP_UNKNOWN;
}
/* Select the operand to do the encoding/decoding of the 'size:Q' fields in
the AdvSIMD instructions. */
/* N.B. it is possible to do some optimization that doesn't call
get_data_pattern each time when we need to select an operand. We can
either buffer the caculated the result or statically generate the data,
however, it is not obvious that the optimization will bring significant
benefit. */
int
aarch64_select_operand_for_sizeq_field_coding (const aarch64_opcode *opcode)
{
return
significant_operand_index [get_data_pattern (opcode->qualifiers_list[0])];
}
const aarch64_field fields[] =
{
{ 0, 0 }, /* NIL. */
{ 0, 4 }, /* cond2: condition in truly conditional-executed inst. */
{ 0, 4 }, /* nzcv: flag bit specifier, encoded in the "nzcv" field. */
{ 5, 5 }, /* defgh: d:e:f:g:h bits in AdvSIMD modified immediate. */
{ 16, 3 }, /* abc: a:b:c bits in AdvSIMD modified immediate. */
{ 5, 19 }, /* imm19: e.g. in CBZ. */
{ 5, 19 }, /* immhi: e.g. in ADRP. */
{ 29, 2 }, /* immlo: e.g. in ADRP. */
{ 22, 2 }, /* size: in most AdvSIMD and floating-point instructions. */
{ 10, 2 }, /* vldst_size: size field in the AdvSIMD load/store inst. */
{ 29, 1 }, /* op: in AdvSIMD modified immediate instructions. */
{ 30, 1 }, /* Q: in most AdvSIMD instructions. */
{ 0, 5 }, /* Rt: in load/store instructions. */
{ 0, 5 }, /* Rd: in many integer instructions. */
{ 5, 5 }, /* Rn: in many integer instructions. */
{ 10, 5 }, /* Rt2: in load/store pair instructions. */
{ 10, 5 }, /* Ra: in fp instructions. */
{ 5, 3 }, /* op2: in the system instructions. */
{ 8, 4 }, /* CRm: in the system instructions. */
{ 12, 4 }, /* CRn: in the system instructions. */
{ 16, 3 }, /* op1: in the system instructions. */
{ 19, 2 }, /* op0: in the system instructions. */
{ 10, 3 }, /* imm3: in add/sub extended reg instructions. */
{ 12, 4 }, /* cond: condition flags as a source operand. */
{ 12, 4 }, /* opcode: in advsimd load/store instructions. */
{ 12, 4 }, /* cmode: in advsimd modified immediate instructions. */
{ 13, 3 }, /* asisdlso_opcode: opcode in advsimd ld/st single element. */
{ 13, 2 }, /* len: in advsimd tbl/tbx instructions. */
{ 16, 5 }, /* Rm: in ld/st reg offset and some integer inst. */
{ 16, 5 }, /* Rs: in load/store exclusive instructions. */
{ 13, 3 }, /* option: in ld/st reg offset + add/sub extended reg inst. */
{ 12, 1 }, /* S: in load/store reg offset instructions. */
{ 21, 2 }, /* hw: in move wide constant instructions. */
{ 22, 2 }, /* opc: in load/store reg offset instructions. */
{ 23, 1 }, /* opc1: in load/store reg offset instructions. */
{ 22, 2 }, /* shift: in add/sub reg/imm shifted instructions. */
{ 22, 2 }, /* type: floating point type field in fp data inst. */
{ 30, 2 }, /* ldst_size: size field in ld/st reg offset inst. */
{ 10, 6 }, /* imm6: in add/sub reg shifted instructions. */
{ 11, 4 }, /* imm4: in advsimd ext and advsimd ins instructions. */
{ 16, 5 }, /* imm5: in conditional compare (immediate) instructions. */
{ 15, 7 }, /* imm7: in load/store pair pre/post index instructions. */
{ 13, 8 }, /* imm8: in floating-point scalar move immediate inst. */
{ 12, 9 }, /* imm9: in load/store pre/post index instructions. */
{ 10, 12 }, /* imm12: in ld/st unsigned imm or add/sub shifted inst. */
{ 5, 14 }, /* imm14: in test bit and branch instructions. */
{ 5, 16 }, /* imm16: in exception instructions. */
{ 0, 26 }, /* imm26: in unconditional branch instructions. */
{ 10, 6 }, /* imms: in bitfield and logical immediate instructions. */
{ 16, 6 }, /* immr: in bitfield and logical immediate instructions. */
{ 16, 3 }, /* immb: in advsimd shift by immediate instructions. */
{ 19, 4 }, /* immh: in advsimd shift by immediate instructions. */
{ 22, 1 }, /* S: in LDRAA and LDRAB instructions. */
{ 22, 1 }, /* N: in logical (immediate) instructions. */
{ 11, 1 }, /* index: in ld/st inst deciding the pre/post-index. */
{ 24, 1 }, /* index2: in ld/st pair inst deciding the pre/post-index. */
{ 31, 1 }, /* sf: in integer data processing instructions. */
{ 30, 1 }, /* lse_size: in LSE extension atomic instructions. */
{ 11, 1 }, /* H: in advsimd scalar x indexed element instructions. */
{ 21, 1 }, /* L: in advsimd scalar x indexed element instructions. */
{ 20, 1 }, /* M: in advsimd scalar x indexed element instructions. */
{ 31, 1 }, /* b5: in the test bit and branch instructions. */
{ 19, 5 }, /* b40: in the test bit and branch instructions. */
{ 10, 6 }, /* scale: in the fixed-point scalar to fp converting inst. */
{ 4, 1 }, /* SVE_M_4: Merge/zero select, bit 4. */
{ 14, 1 }, /* SVE_M_14: Merge/zero select, bit 14. */
{ 16, 1 }, /* SVE_M_16: Merge/zero select, bit 16. */
{ 17, 1 }, /* SVE_N: SVE equivalent of N. */
{ 0, 4 }, /* SVE_Pd: p0-p15, bits [3,0]. */
{ 10, 3 }, /* SVE_Pg3: p0-p7, bits [12,10]. */
{ 5, 4 }, /* SVE_Pg4_5: p0-p15, bits [8,5]. */
{ 10, 4 }, /* SVE_Pg4_10: p0-p15, bits [13,10]. */
{ 16, 4 }, /* SVE_Pg4_16: p0-p15, bits [19,16]. */
{ 16, 4 }, /* SVE_Pm: p0-p15, bits [19,16]. */
{ 5, 4 }, /* SVE_Pn: p0-p15, bits [8,5]. */
{ 0, 4 }, /* SVE_Pt: p0-p15, bits [3,0]. */
{ 5, 5 }, /* SVE_Rm: SVE alternative position for Rm. */
{ 16, 5 }, /* SVE_Rn: SVE alternative position for Rn. */
{ 0, 5 }, /* SVE_Vd: Scalar SIMD&FP register, bits [4,0]. */
{ 5, 5 }, /* SVE_Vm: Scalar SIMD&FP register, bits [9,5]. */
{ 5, 5 }, /* SVE_Vn: Scalar SIMD&FP register, bits [9,5]. */
{ 5, 5 }, /* SVE_Za_5: SVE vector register, bits [9,5]. */
{ 16, 5 }, /* SVE_Za_16: SVE vector register, bits [20,16]. */
{ 0, 5 }, /* SVE_Zd: SVE vector register. bits [4,0]. */
{ 5, 5 }, /* SVE_Zm_5: SVE vector register, bits [9,5]. */
{ 16, 5 }, /* SVE_Zm_16: SVE vector register, bits [20,16]. */
{ 5, 5 }, /* SVE_Zn: SVE vector register, bits [9,5]. */
{ 0, 5 }, /* SVE_Zt: SVE vector register, bits [4,0]. */
{ 5, 1 }, /* SVE_i1: single-bit immediate. */
{ 16, 3 }, /* SVE_imm3: 3-bit immediate field. */
{ 16, 4 }, /* SVE_imm4: 4-bit immediate field. */
{ 5, 5 }, /* SVE_imm5: 5-bit immediate field. */
{ 16, 5 }, /* SVE_imm5b: secondary 5-bit immediate field. */
{ 16, 6 }, /* SVE_imm6: 6-bit immediate field. */
{ 14, 7 }, /* SVE_imm7: 7-bit immediate field. */
{ 5, 8 }, /* SVE_imm8: 8-bit immediate field. */
{ 5, 9 }, /* SVE_imm9: 9-bit immediate field. */
{ 11, 6 }, /* SVE_immr: SVE equivalent of immr. */
{ 5, 6 }, /* SVE_imms: SVE equivalent of imms. */
{ 10, 2 }, /* SVE_msz: 2-bit shift amount for ADR. */
{ 5, 5 }, /* SVE_pattern: vector pattern enumeration. */
{ 0, 4 }, /* SVE_prfop: prefetch operation for SVE PRF[BHWD]. */
{ 22, 1 }, /* SVE_sz: 1-bit element size select. */
{ 16, 4 }, /* SVE_tsz: triangular size select. */
{ 22, 2 }, /* SVE_tszh: triangular size select high, bits [23,22]. */
{ 8, 2 }, /* SVE_tszl_8: triangular size select low, bits [9,8]. */
{ 19, 2 }, /* SVE_tszl_19: triangular size select low, bits [20,19]. */
{ 14, 1 }, /* SVE_xs_14: UXTW/SXTW select (bit 14). */
{ 22, 1 }, /* SVE_xs_22: UXTW/SXTW select (bit 22). */
{ 11, 2 }, /* rotate1: FCMLA immediate rotate. */
{ 13, 2 }, /* rotate2: Indexed element FCMLA immediate rotate. */
{ 12, 1 }, /* rotate3: FCADD immediate rotate. */
};
enum aarch64_operand_class
aarch64_get_operand_class (enum aarch64_opnd type)
{
return aarch64_operands[type].op_class;
}
const char *
aarch64_get_operand_name (enum aarch64_opnd type)
{
return aarch64_operands[type].name;
}
/* Get operand description string.
This is usually for the diagnosis purpose. */
const char *
aarch64_get_operand_desc (enum aarch64_opnd type)
{
return aarch64_operands[type].desc;
}
/* Table of all conditional affixes. */
const aarch64_cond aarch64_conds[16] =
{
{{"eq", "none"}, 0x0},
{{"ne", "any"}, 0x1},
{{"cs", "hs", "nlast"}, 0x2},
{{"cc", "lo", "ul", "last"}, 0x3},
{{"mi", "first"}, 0x4},
{{"pl", "nfrst"}, 0x5},
{{"vs"}, 0x6},
{{"vc"}, 0x7},
{{"hi", "pmore"}, 0x8},
{{"ls", "plast"}, 0x9},
{{"ge", "tcont"}, 0xa},
{{"lt", "tstop"}, 0xb},
{{"gt"}, 0xc},
{{"le"}, 0xd},
{{"al"}, 0xe},
{{"nv"}, 0xf},
};
const aarch64_cond *
get_cond_from_value (aarch64_insn value)
{
assert (value < 16);
return &aarch64_conds[(unsigned int) value];
}
const aarch64_cond *
get_inverted_cond (const aarch64_cond *cond)
{
return &aarch64_conds[cond->value ^ 0x1];
}
/* Table describing the operand extension/shifting operators; indexed by
enum aarch64_modifier_kind.
The value column provides the most common values for encoding modifiers,
which enables table-driven encoding/decoding for the modifiers. */
const struct aarch64_name_value_pair aarch64_operand_modifiers [] =
{
{"none", 0x0},
{"msl", 0x0},
{"ror", 0x3},
{"asr", 0x2},
{"lsr", 0x1},
{"lsl", 0x0},
{"uxtb", 0x0},
{"uxth", 0x1},
{"uxtw", 0x2},
{"uxtx", 0x3},
{"sxtb", 0x4},
{"sxth", 0x5},
{"sxtw", 0x6},
{"sxtx", 0x7},
{"mul", 0x0},
{"mul vl", 0x0},
{NULL, 0},
};
enum aarch64_modifier_kind
aarch64_get_operand_modifier (const struct aarch64_name_value_pair *desc)
{
return desc - aarch64_operand_modifiers;
}
aarch64_insn
aarch64_get_operand_modifier_value (enum aarch64_modifier_kind kind)
{
return aarch64_operand_modifiers[kind].value;
}
enum aarch64_modifier_kind
aarch64_get_operand_modifier_from_value (aarch64_insn value,
bfd_boolean extend_p)
{
if (extend_p == TRUE)
return AARCH64_MOD_UXTB + value;
else
return AARCH64_MOD_LSL - value;
}
bfd_boolean
aarch64_extend_operator_p (enum aarch64_modifier_kind kind)
{
return (kind > AARCH64_MOD_LSL && kind <= AARCH64_MOD_SXTX)
? TRUE : FALSE;
}
static inline bfd_boolean
aarch64_shift_operator_p (enum aarch64_modifier_kind kind)
{
return (kind >= AARCH64_MOD_ROR && kind <= AARCH64_MOD_LSL)
? TRUE : FALSE;
}
const struct aarch64_name_value_pair aarch64_barrier_options[16] =
{
{ "#0x00", 0x0 },
{ "oshld", 0x1 },
{ "oshst", 0x2 },
{ "osh", 0x3 },
{ "#0x04", 0x4 },
{ "nshld", 0x5 },
{ "nshst", 0x6 },
{ "nsh", 0x7 },
{ "#0x08", 0x8 },
{ "ishld", 0x9 },
{ "ishst", 0xa },
{ "ish", 0xb },
{ "#0x0c", 0xc },
{ "ld", 0xd },
{ "st", 0xe },
{ "sy", 0xf },
};
/* Table describing the operands supported by the aliases of the HINT
instruction.
The name column is the operand that is accepted for the alias. The value
column is the hint number of the alias. The list of operands is terminated
by NULL in the name column. */
const struct aarch64_name_value_pair aarch64_hint_options[] =
{
{ "csync", 0x11 }, /* PSB CSYNC. */
{ NULL, 0x0 },
};
/* op -> op: load = 0 instruction = 1 store = 2
l -> level: 1-3
t -> temporal: temporal (retained) = 0 non-temporal (streaming) = 1 */
#define B(op,l,t) (((op) << 3) | (((l) - 1) << 1) | (t))
const struct aarch64_name_value_pair aarch64_prfops[32] =
{
{ "pldl1keep", B(0, 1, 0) },
{ "pldl1strm", B(0, 1, 1) },
{ "pldl2keep", B(0, 2, 0) },
{ "pldl2strm", B(0, 2, 1) },
{ "pldl3keep", B(0, 3, 0) },
{ "pldl3strm", B(0, 3, 1) },
{ NULL, 0x06 },
{ NULL, 0x07 },
{ "plil1keep", B(1, 1, 0) },
{ "plil1strm", B(1, 1, 1) },
{ "plil2keep", B(1, 2, 0) },
{ "plil2strm", B(1, 2, 1) },
{ "plil3keep", B(1, 3, 0) },
{ "plil3strm", B(1, 3, 1) },
{ NULL, 0x0e },
{ NULL, 0x0f },
{ "pstl1keep", B(2, 1, 0) },
{ "pstl1strm", B(2, 1, 1) },
{ "pstl2keep", B(2, 2, 0) },
{ "pstl2strm", B(2, 2, 1) },
{ "pstl3keep", B(2, 3, 0) },
{ "pstl3strm", B(2, 3, 1) },
{ NULL, 0x16 },
{ NULL, 0x17 },
{ NULL, 0x18 },
{ NULL, 0x19 },
{ NULL, 0x1a },
{ NULL, 0x1b },
{ NULL, 0x1c },
{ NULL, 0x1d },
{ NULL, 0x1e },
{ NULL, 0x1f },
};
#undef B
/* Utilities on value constraint. */
static inline int
value_in_range_p (int64_t value, int low, int high)
{
return (value >= low && value <= high) ? 1 : 0;
}
/* Return true if VALUE is a multiple of ALIGN. */
static inline int
value_aligned_p (int64_t value, int align)
{
return (value % align) == 0;
}
/* A signed value fits in a field. */
static inline int
value_fit_signed_field_p (int64_t value, unsigned width)
{
assert (width < 32);
if (width < sizeof (value) * 8)
{
int64_t lim = (int64_t)1 << (width - 1);
if (value >= -lim && value < lim)
return 1;
}
return 0;
}
/* An unsigned value fits in a field. */
static inline int
value_fit_unsigned_field_p (int64_t value, unsigned width)
{
assert (width < 32);
if (width < sizeof (value) * 8)
{
int64_t lim = (int64_t)1 << width;
if (value >= 0 && value < lim)
return 1;
}
return 0;
}
/* Return 1 if OPERAND is SP or WSP. */
int
aarch64_stack_pointer_p (const aarch64_opnd_info *operand)
{
return ((aarch64_get_operand_class (operand->type)
== AARCH64_OPND_CLASS_INT_REG)
&& operand_maybe_stack_pointer (aarch64_operands + operand->type)
&& operand->reg.regno == 31);
}
/* Return 1 if OPERAND is XZR or WZP. */
int
aarch64_zero_register_p (const aarch64_opnd_info *operand)
{
return ((aarch64_get_operand_class (operand->type)
== AARCH64_OPND_CLASS_INT_REG)
&& !operand_maybe_stack_pointer (aarch64_operands + operand->type)
&& operand->reg.regno == 31);
}
/* Return true if the operand *OPERAND that has the operand code
OPERAND->TYPE and been qualified by OPERAND->QUALIFIER can be also
qualified by the qualifier TARGET. */
static inline int
operand_also_qualified_p (const struct aarch64_opnd_info *operand,
aarch64_opnd_qualifier_t target)
{
switch (operand->qualifier)
{
case AARCH64_OPND_QLF_W:
if (target == AARCH64_OPND_QLF_WSP && aarch64_stack_pointer_p (operand))
return 1;
break;
case AARCH64_OPND_QLF_X:
if (target == AARCH64_OPND_QLF_SP && aarch64_stack_pointer_p (operand))
return 1;
break;
case AARCH64_OPND_QLF_WSP:
if (target == AARCH64_OPND_QLF_W
&& operand_maybe_stack_pointer (aarch64_operands + operand->type))
return 1;
break;
case AARCH64_OPND_QLF_SP:
if (target == AARCH64_OPND_QLF_X
&& operand_maybe_stack_pointer (aarch64_operands + operand->type))
return 1;
break;
default:
break;
}
return 0;
}
/* Given qualifier sequence list QSEQ_LIST and the known qualifier KNOWN_QLF
for operand KNOWN_IDX, return the expected qualifier for operand IDX.
Return NIL if more than one expected qualifiers are found. */
aarch64_opnd_qualifier_t
aarch64_get_expected_qualifier (const aarch64_opnd_qualifier_seq_t *qseq_list,
int idx,
const aarch64_opnd_qualifier_t known_qlf,
int known_idx)
{
int i, saved_i;
/* Special case.
When the known qualifier is NIL, we have to assume that there is only
one qualifier sequence in the *QSEQ_LIST and return the corresponding
qualifier directly. One scenario is that for instruction
PRFM <prfop>, [<Xn|SP>, #:lo12:<symbol>]
which has only one possible valid qualifier sequence
NIL, S_D
the caller may pass NIL in KNOWN_QLF to obtain S_D so that it can
determine the correct relocation type (i.e. LDST64_LO12) for PRFM.
Because the qualifier NIL has dual roles in the qualifier sequence:
it can mean no qualifier for the operand, or the qualifer sequence is
not in use (when all qualifiers in the sequence are NILs), we have to
handle this special case here. */
if (known_qlf == AARCH64_OPND_NIL)
{
assert (qseq_list[0][known_idx] == AARCH64_OPND_NIL);
return qseq_list[0][idx];
}
for (i = 0, saved_i = -1; i < AARCH64_MAX_QLF_SEQ_NUM; ++i)
{
if (qseq_list[i][known_idx] == known_qlf)
{
if (saved_i != -1)
/* More than one sequences are found to have KNOWN_QLF at
KNOWN_IDX. */
return AARCH64_OPND_NIL;
saved_i = i;
}
}
return qseq_list[saved_i][idx];
}
enum operand_qualifier_kind
{
OQK_NIL,
OQK_OPD_VARIANT,
OQK_VALUE_IN_RANGE,
OQK_MISC,
};
/* Operand qualifier description. */
struct operand_qualifier_data
{
/* The usage of the three data fields depends on the qualifier kind. */
int data0;
int data1;
int data2;
/* Description. */
const char *desc;
/* Kind. */
enum operand_qualifier_kind kind;
};
/* Indexed by the operand qualifier enumerators. */
struct operand_qualifier_data aarch64_opnd_qualifiers[] =
{
{0, 0, 0, "NIL", OQK_NIL},
/* Operand variant qualifiers.
First 3 fields:
element size, number of elements and common value for encoding. */
{4, 1, 0x0, "w", OQK_OPD_VARIANT},
{8, 1, 0x1, "x", OQK_OPD_VARIANT},
{4, 1, 0x0, "wsp", OQK_OPD_VARIANT},
{8, 1, 0x1, "sp", OQK_OPD_VARIANT},
{1, 1, 0x0, "b", OQK_OPD_VARIANT},
{2, 1, 0x1, "h", OQK_OPD_VARIANT},
{4, 1, 0x2, "s", OQK_OPD_VARIANT},
{8, 1, 0x3, "d", OQK_OPD_VARIANT},
{16, 1, 0x4, "q", OQK_OPD_VARIANT},
{1, 8, 0x0, "8b", OQK_OPD_VARIANT},
{1, 16, 0x1, "16b", OQK_OPD_VARIANT},
{2, 2, 0x0, "2h", OQK_OPD_VARIANT},
{2, 4, 0x2, "4h", OQK_OPD_VARIANT},
{2, 8, 0x3, "8h", OQK_OPD_VARIANT},
{4, 2, 0x4, "2s", OQK_OPD_VARIANT},
{4, 4, 0x5, "4s", OQK_OPD_VARIANT},
{8, 1, 0x6, "1d", OQK_OPD_VARIANT},
{8, 2, 0x7, "2d", OQK_OPD_VARIANT},
{16, 1, 0x8, "1q", OQK_OPD_VARIANT},
{0, 0, 0, "z", OQK_OPD_VARIANT},
{0, 0, 0, "m", OQK_OPD_VARIANT},
/* Qualifiers constraining the value range.
First 3 fields:
Lower bound, higher bound, unused. */
{0, 15, 0, "CR", OQK_VALUE_IN_RANGE},
{0, 7, 0, "imm_0_7" , OQK_VALUE_IN_RANGE},
{0, 15, 0, "imm_0_15", OQK_VALUE_IN_RANGE},
{0, 31, 0, "imm_0_31", OQK_VALUE_IN_RANGE},
{0, 63, 0, "imm_0_63", OQK_VALUE_IN_RANGE},
{1, 32, 0, "imm_1_32", OQK_VALUE_IN_RANGE},
{1, 64, 0, "imm_1_64", OQK_VALUE_IN_RANGE},
/* Qualifiers for miscellaneous purpose.
First 3 fields:
unused, unused and unused. */
{0, 0, 0, "lsl", 0},
{0, 0, 0, "msl", 0},
{0, 0, 0, "retrieving", 0},
};
static inline bfd_boolean
operand_variant_qualifier_p (aarch64_opnd_qualifier_t qualifier)
{
return (aarch64_opnd_qualifiers[qualifier].kind == OQK_OPD_VARIANT)
? TRUE : FALSE;
}
static inline bfd_boolean
qualifier_value_in_range_constraint_p (aarch64_opnd_qualifier_t qualifier)
{
return (aarch64_opnd_qualifiers[qualifier].kind == OQK_VALUE_IN_RANGE)
? TRUE : FALSE;
}
const char*
aarch64_get_qualifier_name (aarch64_opnd_qualifier_t qualifier)
{
return aarch64_opnd_qualifiers[qualifier].desc;
}
/* Given an operand qualifier, return the expected data element size
of a qualified operand. */
unsigned char
aarch64_get_qualifier_esize (aarch64_opnd_qualifier_t qualifier)
{
assert (operand_variant_qualifier_p (qualifier) == TRUE);
return aarch64_opnd_qualifiers[qualifier].data0;
}
unsigned char
aarch64_get_qualifier_nelem (aarch64_opnd_qualifier_t qualifier)
{
assert (operand_variant_qualifier_p (qualifier) == TRUE);
return aarch64_opnd_qualifiers[qualifier].data1;
}
aarch64_insn
aarch64_get_qualifier_standard_value (aarch64_opnd_qualifier_t qualifier)
{
assert (operand_variant_qualifier_p (qualifier) == TRUE);
return aarch64_opnd_qualifiers[qualifier].data2;
}
static int
get_lower_bound (aarch64_opnd_qualifier_t qualifier)
{
assert (qualifier_value_in_range_constraint_p (qualifier) == TRUE);
return aarch64_opnd_qualifiers[qualifier].data0;
}
static int
get_upper_bound (aarch64_opnd_qualifier_t qualifier)
{
assert (qualifier_value_in_range_constraint_p (qualifier) == TRUE);
return aarch64_opnd_qualifiers[qualifier].data1;
}
#ifdef DEBUG_AARCH64
void
aarch64_verbose (const char *str, ...)
{
va_list ap;
va_start (ap, str);
printf ("#### ");
vprintf (str, ap);
printf ("\n");
va_end (ap);
}
static inline void
dump_qualifier_sequence (const aarch64_opnd_qualifier_t *qualifier)
{
int i;
printf ("#### \t");
for (i = 0; i < AARCH64_MAX_OPND_NUM; ++i, ++qualifier)
printf ("%s,", aarch64_get_qualifier_name (*qualifier));
printf ("\n");
}
static void
dump_match_qualifiers (const struct aarch64_opnd_info *opnd,
const aarch64_opnd_qualifier_t *qualifier)
{
int i;
aarch64_opnd_qualifier_t curr[AARCH64_MAX_OPND_NUM];
aarch64_verbose ("dump_match_qualifiers:");
for (i = 0; i < AARCH64_MAX_OPND_NUM; ++i)
curr[i] = opnd[i].qualifier;
dump_qualifier_sequence (curr);
aarch64_verbose ("against");
dump_qualifier_sequence (qualifier);
}
#endif /* DEBUG_AARCH64 */
/* TODO improve this, we can have an extra field at the runtime to
store the number of operands rather than calculating it every time. */
int
aarch64_num_of_operands (const aarch64_opcode *opcode)
{
int i = 0;
const enum aarch64_opnd *opnds = opcode->operands;
while (opnds[i++] != AARCH64_OPND_NIL)
;
--i;
assert (i >= 0 && i <= AARCH64_MAX_OPND_NUM);
return i;
}
/* Find the best matched qualifier sequence in *QUALIFIERS_LIST for INST.
If succeeds, fill the found sequence in *RET, return 1; otherwise return 0.
N.B. on the entry, it is very likely that only some operands in *INST
have had their qualifiers been established.
If STOP_AT is not -1, the function will only try to match
the qualifier sequence for operands before and including the operand
of index STOP_AT; and on success *RET will only be filled with the first
(STOP_AT+1) qualifiers.
A couple examples of the matching algorithm:
X,W,NIL should match
X,W,NIL
NIL,NIL should match
X ,NIL
Apart from serving the main encoding routine, this can also be called
during or after the operand decoding. */
int
aarch64_find_best_match (const aarch64_inst *inst,
const aarch64_opnd_qualifier_seq_t *qualifiers_list,
int stop_at, aarch64_opnd_qualifier_t *ret)
{
int found = 0;
int i, num_opnds;
const aarch64_opnd_qualifier_t *qualifiers;
num_opnds = aarch64_num_of_operands (inst->opcode);
if (num_opnds == 0)
{
DEBUG_TRACE ("SUCCEED: no operand");
return 1;
}
if (stop_at < 0 || stop_at >= num_opnds)
stop_at = num_opnds - 1;
/* For each pattern. */
for (i = 0; i < AARCH64_MAX_QLF_SEQ_NUM; ++i, ++qualifiers_list)
{
int j;
qualifiers = *qualifiers_list;
/* Start as positive. */
found = 1;
DEBUG_TRACE ("%d", i);
#ifdef DEBUG_AARCH64
if (debug_dump)
dump_match_qualifiers (inst->operands, qualifiers);
#endif
/* Most opcodes has much fewer patterns in the list.
First NIL qualifier indicates the end in the list. */
if (empty_qualifier_sequence_p (qualifiers) == TRUE)
{
DEBUG_TRACE_IF (i == 0, "SUCCEED: empty qualifier list");
if (i)
found = 0;
break;
}
for (j = 0; j < num_opnds && j <= stop_at; ++j, ++qualifiers)
{
if (inst->operands[j].qualifier == AARCH64_OPND_QLF_NIL)
{
/* Either the operand does not have qualifier, or the qualifier
for the operand needs to be deduced from the qualifier
sequence.
In the latter case, any constraint checking related with
the obtained qualifier should be done later in
operand_general_constraint_met_p. */
continue;
}
else if (*qualifiers != inst->operands[j].qualifier)
{
/* Unless the target qualifier can also qualify the operand
(which has already had a non-nil qualifier), non-equal
qualifiers are generally un-matched. */
if (operand_also_qualified_p (inst->operands + j, *qualifiers))
continue;
else
{
found = 0;
break;
}
}
else
continue; /* Equal qualifiers are certainly matched. */
}
/* Qualifiers established. */
if (found == 1)
break;
}
if (found == 1)
{
/* Fill the result in *RET. */
int j;
qualifiers = *qualifiers_list;
DEBUG_TRACE ("complete qualifiers using list %d", i);
#ifdef DEBUG_AARCH64
if (debug_dump)
dump_qualifier_sequence (qualifiers);
#endif
for (j = 0; j <= stop_at; ++j, ++qualifiers)
ret[j] = *qualifiers;
for (; j < AARCH64_MAX_OPND_NUM; ++j)
ret[j] = AARCH64_OPND_QLF_NIL;
DEBUG_TRACE ("SUCCESS");
return 1;
}
DEBUG_TRACE ("FAIL");
return 0;
}
/* Operand qualifier matching and resolving.
Return 1 if the operand qualifier(s) in *INST match one of the qualifier
sequences in INST->OPCODE->qualifiers_list; otherwise return 0.
if UPDATE_P == TRUE, update the qualifier(s) in *INST after the matching
succeeds. */
static int
match_operands_qualifier (aarch64_inst *inst, bfd_boolean update_p)
{
int i, nops;
aarch64_opnd_qualifier_seq_t qualifiers;
if (!aarch64_find_best_match (inst, inst->opcode->qualifiers_list, -1,
qualifiers))
{
DEBUG_TRACE ("matching FAIL");
return 0;
}
if (inst->opcode->flags & F_STRICT)
{
/* Require an exact qualifier match, even for NIL qualifiers. */
nops = aarch64_num_of_operands (inst->opcode);
for (i = 0; i < nops; ++i)
if (inst->operands[i].qualifier != qualifiers[i])
return FALSE;
}
/* Update the qualifiers. */
if (update_p == TRUE)
for (i = 0; i < AARCH64_MAX_OPND_NUM; ++i)
{