Coverage Report

Created: 2026-11-21 09:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/gas/config/tc-i386.c
Line
Count
Source
1
/* tc-i386.c -- Assemble code for the Intel 80386
2
   Copyright (C) 1989-2026 Free Software Foundation, Inc.
3
4
   This file is part of GAS, the GNU Assembler.
5
6
   GAS 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
11
   GAS is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with GAS; see the file COPYING.  If not, write to the Free
18
   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19
   02110-1301, USA.  */
20
21
/* Intel 80386 machine specific gas.
22
   Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
23
   x86_64 support by Jan Hubicka (jh@suse.cz)
24
   VIA PadLock support by Michal Ludvig (mludvig@suse.cz)
25
   Bugs & suggestions are completely welcome.  This is free software.
26
   Please help us make it better.  */
27
28
#include "as.h"
29
#include "safe-ctype.h"
30
#include "subsegs.h"
31
#include "dwarf2dbg.h"
32
#include "dw2gencfi.h"
33
#include "scfi.h"
34
#include "gen-sframe.h"
35
#include "sframe.h"
36
#include "elf/x86-64.h"
37
#include "opcodes/i386-init.h"
38
#include "opcodes/i386-mnem.h"
39
#include <limits.h>
40
41
#ifndef INFER_ADDR_PREFIX
42
#define INFER_ADDR_PREFIX 1
43
#endif
44
45
#ifndef DEFAULT_ARCH
46
#define DEFAULT_ARCH "i386"
47
#endif
48
49
#ifndef INLINE
50
#if __GNUC__ >= 2
51
#define INLINE __inline__
52
#else
53
#define INLINE
54
#endif
55
#endif
56
57
/* Prefixes will be emitted in the order defined below.
58
   WAIT_PREFIX must be the first prefix since FWAIT is really is an
59
   instruction, and so must come before any prefixes.
60
   The preferred prefix order is SEG_PREFIX, ADDR_PREFIX, DATA_PREFIX,
61
   REP_PREFIX/HLE_PREFIX, LOCK_PREFIX.  */
62
0
#define WAIT_PREFIX 0
63
5.61k
#define SEG_PREFIX  1
64
43.2k
#define ADDR_PREFIX 2
65
7.72k
#define DATA_PREFIX 3
66
725
#define REP_PREFIX  4
67
0
#define HLE_PREFIX  REP_PREFIX
68
209
#define BND_PREFIX  REP_PREFIX
69
6.72k
#define LOCK_PREFIX 5
70
11.3k
#define REX_PREFIX  6       /* must come last.  */
71
#define MAX_PREFIXES  7 /* max prefixes per opcode */
72
73
/* we define the syntax here (modulo base,index,scale syntax) */
74
119k
#define REGISTER_PREFIX '%'
75
1.93k
#define IMMEDIATE_PREFIX '$'
76
4.01k
#define ABSOLUTE_PREFIX '*'
77
78
/* these are the instruction mnemonic suffixes in AT&T syntax or
79
   memory operand size in Intel syntax.  */
80
4.50k
#define WORD_MNEM_SUFFIX  'w'
81
28.6k
#define BYTE_MNEM_SUFFIX  'b'
82
25.8k
#define SHORT_MNEM_SUFFIX 's'
83
66.6k
#define LONG_MNEM_SUFFIX  'l'
84
146k
#define QWORD_MNEM_SUFFIX  'q'
85
86
1.01M
#define END_OF_INSN '\0'
87
88
#define OPERAND_TYPE_NONE { .bitfield = { .class = ClassNone } }
89
90
/* This matches the C -> StaticRounding alias in the opcode table.  */
91
685
#define commutative staticrounding
92
93
/*
94
  'templates' is for grouping together 'template' structures for opcodes
95
  of the same name.  This is only used for storing the insns in the grand
96
  ole hash table of insns.
97
  The templates themselves start at START and range up to (but not including)
98
  END.
99
  */
100
typedef struct
101
{
102
  const insn_template *start;
103
  const insn_template *end;
104
}
105
templates;
106
107
/* 386 operand encoding bytes:  see 386 book for details of this.  */
108
typedef struct
109
{
110
  unsigned int regmem;  /* codes register or memory operand */
111
  unsigned int reg; /* codes register operand (or extended opcode) */
112
  unsigned int mode;  /* how to interpret regmem & reg */
113
}
114
modrm_byte;
115
116
/* x86-64 extension prefix.  */
117
typedef int rex_byte;
118
119
/* 386 opcode byte to code indirect addressing.  */
120
typedef struct
121
{
122
  unsigned base;
123
  unsigned index;
124
  unsigned scale;
125
}
126
sib_byte;
127
128
/* x86 arch names, types and features */
129
typedef struct
130
{
131
  const char *name;   /* arch name */
132
  unsigned int len:8;   /* arch string length */
133
  bool skip:1;      /* show_arch should skip this. */
134
  enum processor_type type; /* arch type */
135
  enum { vsz_none, vsz_set, vsz_reset } vsz; /* vector size control */
136
  i386_cpu_flags enable;    /* cpu feature enable flags */
137
  i386_cpu_flags disable; /* cpu feature disable flags */
138
}
139
arch_entry;
140
141
/* Modes for parse_insn() to operate in.  */
142
enum parse_mode {
143
  parse_all,
144
  parse_prefix,
145
  parse_pseudo_prefix,
146
};
147
148
static void update_code_flag (int, int);
149
static void s_insn (int);
150
static void s_noopt (int);
151
static void set_code_flag (int);
152
static void set_16bit_gcc_code_flag (int);
153
static void set_intel_syntax (int);
154
static void set_intel_mnemonic (int);
155
static void set_allow_index_reg (int);
156
static void set_check (int);
157
static void set_cpu_arch (int);
158
#ifdef TE_PE
159
static void pe_directive_secrel (int);
160
static void pe_directive_secidx (int);
161
#endif
162
static void signed_cons (int);
163
static char *output_invalid (int c);
164
static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
165
            const char *);
166
static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
167
               const char *);
168
static int i386_att_operand (char *);
169
static int i386_intel_operand (char *, int);
170
static int i386_intel_simplify (expressionS *);
171
static int i386_intel_parse_name (const char *, expressionS *, enum expr_mode);
172
static const reg_entry *parse_register (const char *, char **);
173
static const char *parse_insn (const char *, char *, enum parse_mode);
174
static char *parse_operands (char *, const char *);
175
static void copy_operand (unsigned int, unsigned int);
176
static void swap_operands (void);
177
static void swap_2_operands (unsigned int, unsigned int);
178
static enum i386_flag_code i386_addressing_mode (void);
179
static void optimize_imm (void);
180
static bool optimize_disp (const insn_template *t);
181
static const insn_template *match_template (char);
182
static int check_string (void);
183
static int process_suffix (const insn_template *);
184
static int check_byte_reg (void);
185
static int check_long_reg (void);
186
static int check_qword_reg (void);
187
static int check_word_reg (void);
188
static int finalize_imm (void);
189
static int process_operands (void);
190
static const reg_entry *build_modrm_byte (void);
191
static void output_insn (const struct last_insn *);
192
static void output_imm (fragS *, offsetT);
193
static void output_disp (fragS *, offsetT);
194
#ifdef OBJ_AOUT
195
static void s_bss (int);
196
#endif
197
#ifdef OBJ_ELF
198
static void handle_large_common (int small ATTRIBUTE_UNUSED);
199
200
/* GNU_PROPERTY_X86_ISA_1_USED.  */
201
static unsigned int x86_isa_1_used;
202
/* GNU_PROPERTY_X86_FEATURE_2_USED.  */
203
static unsigned int x86_feature_2_used;
204
/* Generate x86 used ISA and feature properties.  */
205
static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
206
#endif
207
208
static const char *default_arch = DEFAULT_ARCH;
209
210
/* parse_register() returns this when a register alias cannot be used.  */
211
static const reg_entry bad_reg = { "<bad>", OPERAND_TYPE_NONE, 0, 0,
212
           { Dw2Inval, Dw2Inval } };
213
214
static const reg_entry *reg_eax;
215
static const reg_entry *reg_ds;
216
static const reg_entry *reg_es;
217
static const reg_entry *reg_ss;
218
static const reg_entry *reg_st0;
219
static const reg_entry *reg_k0;
220
221
/* VEX prefix.  */
222
typedef struct
223
{
224
  /* VEX prefix is either 2 byte or 3 byte.  EVEX is 4 byte.  */
225
  unsigned char bytes[4];
226
  unsigned int length;
227
  /* Destination or source register specifier.  */
228
  const reg_entry *register_specifier;
229
} vex_prefix;
230
231
/* 'md_assemble ()' gathers together information and puts it into a
232
   i386_insn.  */
233
234
union i386_op
235
  {
236
    expressionS *disps;
237
    expressionS *imms;
238
    const reg_entry *regs;
239
  };
240
241
enum i386_error
242
  {
243
    no_error, /* Must be first.  */
244
    operand_size_mismatch,
245
    operand_type_mismatch,
246
    register_type_mismatch,
247
    number_of_operands_mismatch,
248
    invalid_instruction_suffix,
249
    bad_imm4,
250
    unsupported_with_intel_mnemonic,
251
    unsupported_syntax,
252
    unsupported_EGPR_for_addressing,
253
    unsupported_nf,
254
    unsupported,
255
    unsupported_on_arch,
256
    unsupported_64bit,
257
    no_vex_encoding,
258
    no_evex_encoding,
259
    invalid_sib_address,
260
    invalid_vsib_address,
261
    invalid_vector_register_set,
262
    invalid_tmm_register_set,
263
    invalid_dest_and_src_register_set,
264
    invalid_dest_register_set,
265
    invalid_pseudo_prefix,
266
    unsupported_vector_index_register,
267
    unsupported_broadcast,
268
    broadcast_needed,
269
    unsupported_masking,
270
    mask_not_on_destination,
271
    no_default_mask,
272
    unsupported_rc_sae,
273
    unsupported_vector_size,
274
    unsupported_rsp_register,
275
    internal_error,
276
  };
277
278
#ifdef OBJ_ELF
279
enum x86_tls_error_type
280
{
281
  x86_tls_error_continue,
282
  x86_tls_error_none,
283
  x86_tls_error_insn,
284
  x86_tls_error_opcode,
285
  x86_tls_error_sib,
286
  x86_tls_error_no_base_reg,
287
  x86_tls_error_require_no_base_index_reg,
288
  x86_tls_error_base_reg,
289
  x86_tls_error_index_ebx,
290
  x86_tls_error_eax,
291
  x86_tls_error_RegA,
292
  x86_tls_error_ebx,
293
  x86_tls_error_rip,
294
  x86_tls_error_dest_eax,
295
  x86_tls_error_dest_rdi,
296
  x86_tls_error_scale_factor,
297
  x86_tls_error_base_reg_size,
298
  x86_tls_error_dest_32bit_reg_size,
299
  x86_tls_error_dest_64bit_reg_size,
300
  x86_tls_error_dest_32bit_or_64bit_reg_size
301
};
302
#endif
303
304
struct _i386_insn
305
  {
306
    /* TM holds the template for the insn were currently assembling.  */
307
    insn_template tm;
308
309
    /* TM_TYPES are the operand types as referenced by TM.  */
310
    i386_operand_type tm_types[MAX_OPERANDS];
311
312
    /* SUFFIX holds the instruction size suffix for byte, word, dword
313
       or qword, if given.  */
314
    char suffix;
315
316
    /* OPCODE_LENGTH holds the number of base opcode bytes.  */
317
    unsigned char opcode_length;
318
319
    /* OPERANDS gives the number of given operands.  */
320
    unsigned int operands;
321
322
    /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
323
       of given register, displacement, memory operands and immediate
324
       operands.  */
325
    unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
326
327
    /* TYPES [i] is the type (see above #defines) which tells us how to
328
       use OP[i] for the corresponding operand.  */
329
    i386_operand_type types[MAX_OPERANDS];
330
331
    /* Displacement expression, immediate expression, or register for each
332
       operand.  */
333
    union i386_op op[MAX_OPERANDS];
334
335
    /* Flags for operands.  */
336
    unsigned int flags[MAX_OPERANDS];
337
1.87k
#define Operand_PCrel 1
338
29.4k
#define Operand_Mem   2
339
0
#define Operand_Signed 4 /* .insn only */
340
341
    /* Relocation type for operand */
342
    enum bfd_reloc_code_real reloc[MAX_OPERANDS];
343
344
    /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
345
       the base index byte below.  */
346
    const reg_entry *base_reg;
347
    const reg_entry *index_reg;
348
    unsigned int log2_scale_factor;
349
350
    /* SEG gives the seg_entries of this insn.  They are zero unless
351
       explicit segment overrides are given.  */
352
    const reg_entry *seg[2];
353
354
    /* PREFIX holds all the given prefix opcodes (usually null).
355
       PREFIXES is the number of prefix opcodes.  */
356
    unsigned int prefixes;
357
    unsigned char prefix[MAX_PREFIXES];
358
359
    /* .insn allows for reserved opcode spaces.  */
360
    unsigned char insn_opcode_space;
361
362
    /* .insn also allows (requires) specifying immediate size.  */
363
    unsigned char imm_bits[MAX_OPERANDS];
364
365
    /* Register is in low 3 bits of opcode.  */
366
    bool short_form;
367
368
    /* The operand to a branch insn indicates an absolute branch.  */
369
    bool jumpabsolute;
370
371
    /* The operand to a branch insn indicates a far branch.  */
372
    bool far_branch;
373
374
    /* There is a memory operand of (%dx) which should be only used
375
       with input/output instructions.  */
376
    bool input_output_operand;
377
378
    /* Extended states.  */
379
    enum
380
      {
381
  /* Use MMX state.  */
382
  xstate_mmx = 1 << 0,
383
  /* Use XMM state.  */
384
  xstate_xmm = 1 << 1,
385
  /* Use YMM state.  */
386
  xstate_ymm = 1 << 2 | xstate_xmm,
387
  /* Use ZMM state.  */
388
  xstate_zmm = 1 << 3 | xstate_ymm,
389
  /* Use TMM state.  */
390
  xstate_tmm = 1 << 4,
391
  /* Use MASK state.  */
392
  xstate_mask = 1 << 5
393
      } xstate;
394
395
    /* Has GOTPC or TLS relocation.  */
396
    bool has_gotpc_tls_reloc;
397
398
    /* Has relocation entry from the gotrel array.  */
399
    bool has_gotrel;
400
401
    /* RM and SIB are the modrm byte and the sib byte where the
402
       addressing modes of this insn are encoded.  */
403
    modrm_byte rm;
404
    rex_byte rex;
405
    rex_byte vrex;
406
    rex_byte rex2;
407
    sib_byte sib;
408
    vex_prefix vex;
409
410
    /* Masking attributes.
411
412
       The struct describes masking, applied to OPERAND in the instruction.
413
       REG is a pointer to the corresponding mask register.  ZEROING tells
414
       whether merging or zeroing mask is used.  */
415
    struct Mask_Operation
416
    {
417
      const reg_entry *reg;
418
      unsigned int zeroing;
419
      /* The operand where this operation is associated.  */
420
      unsigned int operand;
421
    } mask;
422
423
    /* Rounding control and SAE attributes.  */
424
    struct RC_Operation
425
    {
426
      enum rc_type
427
  {
428
    rc_none = -1,
429
    rne,
430
    rd,
431
    ru,
432
    rz,
433
    saeonly
434
  } type;
435
      /* In Intel syntax the operand modifier form is supposed to be used, but
436
   we continue to accept the immediate forms as well.  */
437
      bool modifier;
438
    } rounding;
439
440
    /* Broadcasting attributes.
441
442
       The struct describes broadcasting, applied to OPERAND.  TYPE is
443
       expresses the broadcast factor.  */
444
    struct Broadcast_Operation
445
    {
446
      /* Type of broadcast: {1to2}, {1to4}, {1to8}, {1to16} or {1to32}.  */
447
      unsigned int type;
448
449
      /* Index of broadcasted operand.  */
450
      unsigned int operand;
451
452
      /* Number of bytes to broadcast.  */
453
      unsigned int bytes;
454
    } broadcast;
455
456
    /* Compressed disp8*N attribute.  */
457
    unsigned int memshift;
458
459
    /* SCC = EVEX.[SC3,SC2,SC1,SC0].  */
460
    unsigned int scc;
461
462
    /* Store 4 bits of EVEX.[OF,SF,ZF,CF].  */
463
3
#define OSZC_CF 1
464
2
#define OSZC_ZF 2
465
0
#define OSZC_SF 4
466
3
#define OSZC_OF 8
467
    unsigned int oszc_flags;
468
469
    /* Invert the condition encoded in a base opcode.  */
470
    bool invert_cond;
471
472
    /* REP prefix.  */
473
    const char *rep_prefix;
474
475
    /* HLE prefix.  */
476
    const char *hle_prefix;
477
478
    /* Have BND prefix.  */
479
    const char *bnd_prefix;
480
481
    /* Have NOTRACK prefix.  */
482
    const char *notrack_prefix;
483
484
    /* Error message.  */
485
    enum i386_error error;
486
  };
487
488
typedef struct _i386_insn i386_insn;
489
490
/* Pseudo-prefix recording state, separate from i386_insn.  */
491
static struct pseudo_prefixes {
492
  /* How to encode instructions.  */
493
  enum {
494
    encoding_default = 0,
495
    encoding_vex,
496
    encoding_vex3,
497
    encoding_egpr, /* REX2 or EVEX.  */
498
    encoding_evex,
499
    encoding_evex512,
500
    encoding_error
501
  } encoding;
502
503
  /* Prefer load or store in encoding.  */
504
  enum {
505
    dir_encoding_default = 0,
506
    dir_encoding_load,
507
    dir_encoding_store,
508
    dir_encoding_swap
509
  } dir_encoding;
510
511
  /* Prefer 8bit, 16bit, 32bit displacement in encoding.  */
512
  enum {
513
    disp_encoding_default = 0,
514
    disp_encoding_8bit,
515
    disp_encoding_16bit,
516
    disp_encoding_32bit
517
  } disp_encoding;
518
519
  /* Exclude sign-extended 8bit immediate in encoding.  */
520
  bool no_imm8s;
521
522
  /* Prefer the REX byte in encoding.  */
523
  bool rex_encoding;
524
525
  /* Prefer the REX2 prefix in encoding.  */
526
  bool rex2_encoding;
527
528
  /* No CSPAZO flags update.  */
529
  bool has_nf;
530
531
  /* Disable instruction size optimization.  */
532
  bool no_optimize;
533
} pp;
534
535
/* Link RC type with corresponding string, that'll be looked for in
536
   asm.  */
537
struct RC_name
538
{
539
  enum rc_type type;
540
  const char *name;
541
  unsigned int len;
542
};
543
544
static const struct RC_name RC_NamesTable[] =
545
{
546
  {  rne, STRING_COMMA_LEN ("rn-sae") },
547
  {  rd,  STRING_COMMA_LEN ("rd-sae") },
548
  {  ru,  STRING_COMMA_LEN ("ru-sae") },
549
  {  rz,  STRING_COMMA_LEN ("rz-sae") },
550
  {  saeonly,  STRING_COMMA_LEN ("sae") },
551
};
552
553
/* To be indexed by segment register number.  */
554
static const unsigned char i386_seg_prefixes[] = {
555
  ES_PREFIX_OPCODE,
556
  CS_PREFIX_OPCODE,
557
  SS_PREFIX_OPCODE,
558
  DS_PREFIX_OPCODE,
559
  FS_PREFIX_OPCODE,
560
  GS_PREFIX_OPCODE
561
};
562
563
/* List of chars besides those in app.c:symbol_chars that can start an
564
   operand.  Used to prevent the scrubber eating vital white-space.  */
565
const char extra_symbol_chars[] = "*%-(["
566
#ifdef LEX_AT
567
  "@"
568
#endif
569
#ifdef LEX_QM
570
  "?"
571
#endif
572
  ;
573
574
#if (defined (OBJ_ELF)          \
575
     && !defined (TE_GNU)       \
576
     && !defined (TE_LINUX)       \
577
     && !defined (TE_Haiku)       \
578
     && !defined (TE_FreeBSD)       \
579
     && !defined (TE_DragonFly)       \
580
     && !defined (TE_NetBSD))
581
/* This array holds the chars that always start a comment.  If the
582
   pre-processor is disabled, these aren't very useful.  The option
583
   --divide will remove '/' from this list.  */
584
const char *i386_comment_chars = "#/";
585
#define SVR4_COMMENT_CHARS 1
586
#define PREFIX_SEPARATOR '\\'
587
588
#else
589
const char *i386_comment_chars = "#";
590
1.41k
#define PREFIX_SEPARATOR '/'
591
#endif
592
593
/* This array holds the chars that only start a comment at the beginning of
594
   a line.  If the line seems to have the form '# 123 filename'
595
   .line and .file directives will appear in the pre-processed output.
596
   Note that input_file.c hand checks for '#' at the beginning of the
597
   first line of the input file.  This is because the compiler outputs
598
   #NO_APP at the beginning of its output.
599
   Also note that comments started like this one will always work if
600
   '/' isn't otherwise defined.  */
601
const char line_comment_chars[] = "#/";
602
603
const char line_separator_chars[] = ";";
604
605
/* Chars that can be used to separate mant from exp in floating point
606
   nums.  */
607
const char EXP_CHARS[] = "eE";
608
609
/* Chars that mean this number is a floating point constant
610
   As in 0f12.456
611
   or    0d1.2345e12.  */
612
const char FLT_CHARS[] = "fFdDxXhHbB";
613
614
/* Tables for lexical analysis.  */
615
static char mnemonic_chars[256];
616
static char register_chars[256];
617
static char operand_chars[256];
618
619
/* Lexical macros.  */
620
1.29M
#define is_operand_char(x) (operand_chars[(unsigned char) x])
621
#define is_register_char(x) (register_chars[(unsigned char) x])
622
623
/* All non-digit non-letter characters that may occur in an operand and
624
   which aren't already in extra_symbol_chars[].  */
625
static const char operand_special_chars[] = "$+,)._~/<>|&^!=:@]{}";
626
627
/* md_assemble() always leaves the strings it's passed unaltered.  To
628
   effect this we maintain a stack of saved characters that we've smashed
629
   with '\0's (indicating end of strings for various sub-fields of the
630
   assembler instruction).  */
631
static char save_stack[32];
632
static char *save_stack_p;
633
#define END_STRING_AND_SAVE(s) \
634
31.6k
  do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
635
#define RESTORE_END_STRING(s) \
636
31.5k
  do { *(s) = *--save_stack_p; } while (0)
637
638
/* The instruction we're assembling.  */
639
static i386_insn i;
640
641
/* Possible templates for current insn.  */
642
static templates current_templates;
643
644
/* Per instruction expressionS buffers: max displacements & immediates.  */
645
static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
646
static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
647
648
/* Current operand we are working on.  */
649
static int this_operand = -1;
650
651
/* Are we processing a .insn directive?  */
652
26.0k
#define dot_insn() (i.tm.mnem_off == MN__insn)
653
654
static enum i386_flag_code i386_flag_code;
655
259k
#define flag_code i386_flag_code /* Permit to continue using original name.  */
656
static unsigned int object_64bit;
657
static unsigned int disallow_64bit_reloc;
658
static int use_rela_relocations = 0;
659
/* __tls_get_addr/___tls_get_addr symbol for TLS.  */
660
static const char *tls_get_addr;
661
662
#ifdef OBJ_ELF
663
664
/* The ELF ABI to use.  */
665
enum x86_elf_abi
666
{
667
  I386_ABI,
668
  X86_64_ABI,
669
  X86_64_X32_ABI
670
};
671
672
static enum x86_elf_abi x86_elf_abi = I386_ABI;
673
#endif
674
675
#if defined (TE_PE) || defined (TE_PEP)
676
/* Use big object file format.  */
677
static int use_big_obj = 0;
678
#endif
679
680
#ifdef OBJ_ELF
681
/* 1 if generating code for a shared library.  */
682
static int shared = 0;
683
684
const unsigned int x86_sframe_cfa_sp_reg = REG_SP;
685
/* The other CFA base register for SFrame stack trace info.  */
686
const unsigned int x86_sframe_cfa_fp_reg = REG_FP;
687
/* The return address register for SFrame stack trace info.  For AMD64, RA
688
   tracking is not needed, but some directives like .cfi_undefined may use
689
   RA to indicate the outermost frame.  */
690
const unsigned int x86_sframe_cfa_ra_reg = REG_RA;
691
692
static ginsnS *x86_ginsn_new (const symbolS *, enum ginsn_gen_mode);
693
#endif
694
695
/* 1 for intel syntax,
696
   0 if att syntax.  */
697
static int intel_syntax = 0;
698
699
static enum x86_64_isa
700
{
701
  amd64 = 1,  /* AMD64 ISA.  */
702
  intel64 /* Intel64 ISA.  */
703
} isa64;
704
705
/* 1 for intel mnemonic,
706
   0 if att mnemonic.  */
707
static int intel_mnemonic = !SYSV386_COMPAT;
708
709
/* 1 if pseudo registers are permitted.  */
710
static int allow_pseudo_reg = 0;
711
712
/* 1 if register prefix % not required.  */
713
static int allow_naked_reg = 0;
714
715
/* 1 if the assembler should add BND prefix for all control-transferring
716
   instructions supporting it, even if this prefix wasn't specified
717
   explicitly.  */
718
static int add_bnd_prefix = 0;
719
720
/* 1 if pseudo index register, eiz/riz, is allowed .  */
721
static int allow_index_reg = 0;
722
723
/* 1 if the assembler should ignore LOCK prefix, even if it was
724
   specified explicitly.  */
725
static int omit_lock_prefix = 0;
726
727
/* 1 if the assembler should encode lfence, mfence, and sfence as
728
   "lock addl $0, (%{re}sp)".  */
729
static int avoid_fence = 0;
730
731
/* 1 if lfence should be inserted after every load.  */
732
static int lfence_after_load = 0;
733
734
/* Non-zero if lfence should be inserted before indirect branch.  */
735
static enum lfence_before_indirect_branch_kind
736
  {
737
    lfence_branch_none = 0,
738
    lfence_branch_register,
739
    lfence_branch_memory,
740
    lfence_branch_all
741
  }
742
lfence_before_indirect_branch;
743
744
/* Non-zero if lfence should be inserted before ret.  */
745
static enum lfence_before_ret_kind
746
  {
747
    lfence_before_ret_none = 0,
748
    lfence_before_ret_not,
749
    lfence_before_ret_or,
750
    lfence_before_ret_shl
751
  }
752
lfence_before_ret;
753
754
/* 1 if the assembler should generate relax relocations.  */
755
756
#ifdef TE_SOLARIS
757
/* PR gas/19520: The Solaris/x86 linker cannot handle relax relocations
758
   before Solaris 11.4 which cannot easily be detected in cross
759
   configurations.  */
760
#define DEFAULT_GENERATE_X86_RELAX_RELOCATIONS 0
761
#else
762
0
#define DEFAULT_GENERATE_X86_RELAX_RELOCATIONS 1
763
#endif
764
765
static int generate_relax_relocations
766
  = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
767
768
/* 1 if the assembler should check tls relocation.  */
769
static bool tls_check = DEFAULT_X86_TLS_CHECK;
770
771
static enum check_kind
772
  {
773
    check_none = 0,
774
    check_warning,
775
    check_error
776
  }
777
sse_check, operand_check = check_warning;
778
779
/* Non-zero if branches should be aligned within power of 2 boundary.  */
780
static int align_branch_power = 0;
781
782
/* Types of branches to align.  */
783
enum align_branch_kind
784
  {
785
    align_branch_none = 0,
786
    align_branch_jcc = 1,
787
    align_branch_fused = 2,
788
    align_branch_jmp = 3,
789
    align_branch_call = 4,
790
    align_branch_indirect = 5,
791
    align_branch_ret = 6
792
  };
793
794
/* Type bits of branches to align.  */
795
enum align_branch_bit
796
  {
797
    align_branch_jcc_bit = 1 << align_branch_jcc,
798
    align_branch_fused_bit = 1 << align_branch_fused,
799
    align_branch_jmp_bit = 1 << align_branch_jmp,
800
    align_branch_call_bit = 1 << align_branch_call,
801
    align_branch_indirect_bit = 1 << align_branch_indirect,
802
    align_branch_ret_bit = 1 << align_branch_ret
803
  };
804
805
static unsigned int align_branch = (align_branch_jcc_bit
806
            | align_branch_fused_bit
807
            | align_branch_jmp_bit);
808
809
/* Types of condition jump used by macro-fusion.  */
810
enum mf_jcc_kind
811
  {
812
    mf_jcc_jo = 0,  /* base opcode 0x70  */
813
    mf_jcc_jc,      /* base opcode 0x72  */
814
    mf_jcc_je,      /* base opcode 0x74  */
815
    mf_jcc_jna,     /* base opcode 0x76  */
816
    mf_jcc_js,      /* base opcode 0x78  */
817
    mf_jcc_jp,      /* base opcode 0x7a  */
818
    mf_jcc_jl,      /* base opcode 0x7c  */
819
    mf_jcc_jle,     /* base opcode 0x7e  */
820
  };
821
822
/* Types of compare flag-modifying insntructions used by macro-fusion.  */
823
enum mf_cmp_kind
824
  {
825
    mf_cmp_test_and,  /* test/cmp */
826
    mf_cmp_alu_cmp,  /* add/sub/cmp */
827
    mf_cmp_incdec  /* inc/dec */
828
  };
829
830
/* The maximum padding size for fused jcc.  CMP like instruction can
831
   be 9 bytes and jcc can be 6 bytes.  Leave room just in case for
832
   prefixes.   */
833
368
#define MAX_FUSED_JCC_PADDING_SIZE 20
834
835
/* The maximum number of prefixes added for an instruction.  */
836
static unsigned int align_branch_prefix_size = 5;
837
838
/* Optimization:
839
   1. Clear the REX_W bit with register operand if possible.
840
   2. Above plus use 128bit vector instruction to clear the full vector
841
      register.
842
 */
843
static int optimize = 0;
844
845
/* Optimization:
846
   1. Clear the REX_W bit with register operand if possible.
847
   2. Above plus use 128bit vector instruction to clear the full vector
848
      register.
849
   3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
850
      "testb $imm7,%r8".
851
 */
852
static int optimize_for_space = 0;
853
854
/* Disabled optimizations.  */
855
static int optimize_for_disabled_optimizations = 0;
856
857
/* Register prefix used for error message.  */
858
static const char *register_prefix = "%";
859
860
/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
861
   leave, push, and pop instructions so that gcc has the same stack
862
   frame as in 32 bit mode.  */
863
static char stackop_size = '\0';
864
865
/* Non-zero to optimize code alignment.  */
866
int optimize_align_code = 1;
867
868
/* Non-zero to quieten some warnings.  */
869
static int quiet_warnings = 0;
870
871
/* Guard to avoid repeated warnings about non-16-bit code on 16-bit CPUs.  */
872
static bool pre_386_16bit_warned;
873
874
/* CPU name.  */
875
static const char *cpu_arch_name = NULL;
876
static char *cpu_sub_arch_name = NULL;
877
878
/* CPU feature flags.  */
879
static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
880
881
/* ISA extensions available in 64-bit mode only.  */
882
static const i386_cpu_flags cpu_64_flags = CPU_ANY_64_FLAGS;
883
884
/* If we have selected a cpu we are generating instructions for.  */
885
static int cpu_arch_tune_set = 0;
886
887
/* Cpu we are generating instructions for.  */
888
static enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
889
890
/* CPU instruction set architecture used.  */
891
static enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
892
893
/* CPU feature flags of instruction set architecture used.  */
894
static i386_cpu_flags cpu_arch_isa_flags;
895
896
/* If set, conditional jumps are not automatically promoted to handle
897
   larger than a byte offset.  */
898
static bool no_cond_jump_promotion = false;
899
900
/* This will be set from an expression parser hook if there's any
901
   applicable operator involved in an expression.  */
902
static enum {
903
  expr_operator_none,
904
  expr_operator_present,
905
  expr_large_value,
906
} expr_mode;
907
908
/* Encode SSE instructions with VEX prefix.  */
909
static unsigned int sse2avx;
910
911
/* Encode aligned vector move as unaligned vector move.  */
912
static unsigned int use_unaligned_vector_move;
913
914
/* Maximum permitted vector size. */
915
10
#define VSZ128 0
916
41.7k
#define VSZ256 1
917
13.5k
#define VSZ512 2
918
24
#define VSZ_DEFAULT VSZ512
919
static unsigned int vector_size = VSZ_DEFAULT;
920
921
/* Encode scalar AVX instructions with specific vector length.  */
922
static enum
923
  {
924
    vex128 = 0,
925
    vex256
926
  } avxscalar;
927
928
/* Encode VEX WIG instructions with specific vex.w.  */
929
static enum
930
  {
931
    vexw0 = 0,
932
    vexw1
933
  } vexwig;
934
935
/* Encode scalar EVEX LIG instructions with specific vector length.  */
936
static enum
937
  {
938
    evexl128 = 0,
939
    evexl256,
940
    evexl512
941
  } evexlig;
942
943
/* Encode EVEX WIG instructions with specific evex.w.  */
944
static enum
945
  {
946
    evexw0 = 0,
947
    evexw1
948
  } evexwig;
949
950
/* Value to encode in EVEX RC bits, for SAE-only instructions.  */
951
static enum rc_type evexrcig = rne;
952
953
/* Pre-defined "_GLOBAL_OFFSET_TABLE_".  */
954
static symbolS *GOT_symbol;
955
956
/* The dwarf2 return column, adjusted for 32 or 64 bit.  */
957
unsigned int x86_dwarf2_return_column;
958
959
/* The dwarf2 data alignment, adjusted for 32 or 64 bit.  */
960
int x86_cie_data_alignment;
961
962
/* Interface to relax_segment.
963
   There are 3 major relax states for 386 jump insns because the
964
   different types of jumps add different sizes to frags when we're
965
   figuring out what sort of jump to choose to reach a given label.
966
967
   BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
968
   branches which are handled by md_estimate_size_before_relax() and
969
   i386_generic_table_relax_frag().  */
970
971
/* Types.  */
972
1
#define UNCOND_JUMP 0
973
0
#define COND_JUMP 1
974
0
#define COND_JUMP86 2
975
0
#define BRANCH_PADDING 3
976
0
#define BRANCH_PREFIX 4
977
0
#define FUSED_JCC_PADDING 5
978
979
/* Sizes.  */
980
71
#define CODE16  1
981
154
#define SMALL 0
982
#define SMALL16 (SMALL | CODE16)
983
0
#define BIG 2
984
0
#define BIG16 (BIG | CODE16)
985
986
#ifndef INLINE
987
#ifdef __GNUC__
988
#define INLINE __inline__
989
#else
990
#define INLINE
991
#endif
992
#endif
993
994
#define ENCODE_RELAX_STATE(type, size) \
995
77
  ((relax_substateT) (((type) << 2) | (size)))
996
#define TYPE_FROM_RELAX_STATE(s) \
997
1
  ((s) >> 2)
998
#define DISP_SIZE_FROM_RELAX_STATE(s) \
999
0
    ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
1000
1001
/* This table is used by relax_frag to promote short jumps to long
1002
   ones where necessary.  SMALL (short) jumps may be promoted to BIG
1003
   (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long).  We
1004
   don't allow a short jump in a 32 bit code segment to be promoted to
1005
   a 16 bit offset jump because it's slower (requires data size
1006
   prefix), and doesn't work, unless the destination is in the bottom
1007
   64k of the code segment (The top 16 bits of eip are zeroed).  */
1008
1009
const relax_typeS md_relax_table[] =
1010
{
1011
  /* The fields are:
1012
     1) most positive reach of this state,
1013
     2) most negative reach of this state,
1014
     3) how many bytes this mode will have in the variable part of the frag
1015
     4) which index into the table to try if we can't fit into this one.  */
1016
1017
  /* UNCOND_JUMP states.  */
1018
  {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
1019
  {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
1020
  /* dword jmp adds 4 bytes to frag:
1021
     0 extra opcode bytes, 4 displacement bytes.  */
1022
  {0, 0, 4, 0},
1023
  /* word jmp adds 2 byte2 to frag:
1024
     0 extra opcode bytes, 2 displacement bytes.  */
1025
  {0, 0, 2, 0},
1026
1027
  /* COND_JUMP states.  */
1028
  {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
1029
  {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
1030
  /* dword conditionals adds 5 bytes to frag:
1031
     1 extra opcode byte, 4 displacement bytes.  */
1032
  {0, 0, 5, 0},
1033
  /* word conditionals add 3 bytes to frag:
1034
     1 extra opcode byte, 2 displacement bytes.  */
1035
  {0, 0, 3, 0},
1036
1037
  /* COND_JUMP86 states.  */
1038
  {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
1039
  {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
1040
  /* dword conditionals adds 5 bytes to frag:
1041
     1 extra opcode byte, 4 displacement bytes.  */
1042
  {0, 0, 5, 0},
1043
  /* word conditionals add 4 bytes to frag:
1044
     1 displacement byte and a 3 byte long branch insn.  */
1045
  {0, 0, 4, 0}
1046
};
1047
1048
#define ARCH(n, t, f, s) \
1049
  { STRING_COMMA_LEN (#n), s, PROCESSOR_ ## t, vsz_none, CPU_ ## f ## _FLAGS, \
1050
    CPU_NONE_FLAGS }
1051
#define SUBARCH(n, e, d, s) \
1052
  { STRING_COMMA_LEN (#n), s, PROCESSOR_NONE, vsz_none, CPU_ ## e ## _FLAGS, \
1053
    CPU_ ## d ## _FLAGS }
1054
#define VECARCH(n, e, d, v) \
1055
  { STRING_COMMA_LEN (#n), false, PROCESSOR_NONE, vsz_ ## v, \
1056
    CPU_ ## e ## _FLAGS, CPU_ ## d ## _FLAGS }
1057
1058
#define CPU_ANY_APX_NCI_NDD_NF_FLAGS \
1059
  { .bitfield = \
1060
    { .cpuapx_nci = true, \
1061
      .cpuapx_ndd = true, \
1062
      .cpuapx_nf = true } }
1063
1064
static const arch_entry cpu_arch[] =
1065
{
1066
  /* Do not replace the first two entries - i386_target_format() and
1067
     set_cpu_arch() rely on them being there in this order.  */
1068
  ARCH (generic32, GENERIC32, GENERIC32, false),
1069
  ARCH (generic64, GENERIC64, GENERIC64, false),
1070
  ARCH (i8086, UNKNOWN, NONE, false),
1071
  ARCH (i186, UNKNOWN, 186, false),
1072
  ARCH (i286, UNKNOWN, 286, false),
1073
  ARCH (i386, I386, 386, false),
1074
  ARCH (i486, I486, 486, false),
1075
  ARCH (i586, PENTIUM, 586, false),
1076
  ARCH (pentium, PENTIUM, 586, false),
1077
  ARCH (i686, I686, 686, false),
1078
  ARCH (pentiumpro, PENTIUMPRO, PENTIUMPRO, false),
1079
  ARCH (pentiumii, PENTIUMPRO, P2, false),
1080
  ARCH (pentiumiii, PENTIUMPRO, P3, false),
1081
  ARCH (pentium4, PENTIUM4, P4, false),
1082
  ARCH (prescott, NOCONA, CORE, false),
1083
  ARCH (nocona, NOCONA, NOCONA, false),
1084
  ARCH (yonah, CORE, CORE, true),
1085
  ARCH (core, CORE, CORE, false),
1086
  ARCH (merom, CORE2, CORE2, true),
1087
  ARCH (core2, CORE2, CORE2, false),
1088
  ARCH (corei7, COREI7, COREI7, false),
1089
  ARCH (iamcu, IAMCU, IAMCU, false),
1090
  ARCH (k6, K6, K6, false),
1091
  ARCH (k6_2, K6, K6_2, false),
1092
  ARCH (athlon, ATHLON, ATHLON, false),
1093
  ARCH (sledgehammer, K8, K8, true),
1094
  ARCH (opteron, K8, K8, false),
1095
  ARCH (k8, K8, K8, false),
1096
  ARCH (amdfam10, AMDFAM10, AMDFAM10, false),
1097
  ARCH (bdver1, BD, BDVER1, false),
1098
  ARCH (bdver2, BD, BDVER2, false),
1099
  ARCH (bdver3, BD, BDVER3, false),
1100
  ARCH (bdver4, BD, BDVER4, false),
1101
  ARCH (znver1, ZNVER, ZNVER1, false),
1102
  ARCH (znver2, ZNVER, ZNVER2, false),
1103
  ARCH (znver3, ZNVER, ZNVER3, false),
1104
  ARCH (znver4, ZNVER, ZNVER4, false),
1105
  ARCH (znver5, ZNVER, ZNVER5, false),
1106
  ARCH (znver6, ZNVER, ZNVER6, false),
1107
  ARCH (btver1, BT, BTVER1, false),
1108
  ARCH (btver2, BT, BTVER2, false),
1109
1110
  SUBARCH (8087, 8087, ANY_8087, false),
1111
  SUBARCH (87, NONE, ANY_8087, false), /* Disable only!  */
1112
  SUBARCH (287, 287, ANY_287, false),
1113
  SUBARCH (387, 387, ANY_387, false),
1114
  SUBARCH (687, 687, ANY_687, false),
1115
  SUBARCH (cmov, CMOV, CMOV, false),
1116
  SUBARCH (fxsr, FXSR, ANY_FXSR, false),
1117
  SUBARCH (mmx, MMX, ANY_MMX, false),
1118
  SUBARCH (sse, SSE, ANY_SSE, false),
1119
  SUBARCH (sse2, SSE2, ANY_SSE2, false),
1120
  SUBARCH (sse3, SSE3, ANY_SSE3, false),
1121
  SUBARCH (sse4a, SSE4A, ANY_SSE4A, false),
1122
  SUBARCH (ssse3, SSSE3, ANY_SSSE3, false),
1123
  SUBARCH (sse4.1, SSE4_1, ANY_SSE4_1, false),
1124
  SUBARCH (sse4.2, SSE4_2, ANY_SSE4_2, false),
1125
  SUBARCH (sse4, SSE4_2, ANY_SSE4_1, false),
1126
  VECARCH (avx, AVX, ANY_AVX, reset),
1127
  VECARCH (avx2, AVX2, ANY_AVX2, reset),
1128
  VECARCH (avx512f, AVX512F, ANY_AVX512F, reset),
1129
  VECARCH (avx512cd, AVX512CD, ANY_AVX512CD, reset),
1130
  VECARCH (avx512er, AVX512ER, ANY_AVX512ER, reset),
1131
  VECARCH (avx512pf, AVX512PF, ANY_AVX512PF, reset),
1132
  VECARCH (avx512dq, AVX512DQ, ANY_AVX512DQ, reset),
1133
  VECARCH (avx512bw, AVX512BW, ANY_AVX512BW, reset),
1134
  VECARCH (avx512vl, AVX512VL, ANY_AVX512VL, reset),
1135
  SUBARCH (monitor, MONITOR, MONITOR, false),
1136
  SUBARCH (vmx, VMX, ANY_VMX, false),
1137
  SUBARCH (vmfunc, VMFUNC, ANY_VMFUNC, false),
1138
  SUBARCH (smx, SMX, SMX, false),
1139
  SUBARCH (xsave, XSAVE, ANY_XSAVE, false),
1140
  SUBARCH (xsaveopt, XSAVEOPT, ANY_XSAVEOPT, false),
1141
  SUBARCH (xsavec, XSAVEC, ANY_XSAVEC, false),
1142
  SUBARCH (xsaves, XSAVES, ANY_XSAVES, false),
1143
  SUBARCH (aes, AES, ANY_AES, false),
1144
  SUBARCH (pclmul, PCLMULQDQ, ANY_PCLMULQDQ, false),
1145
  SUBARCH (clmul, PCLMULQDQ, ANY_PCLMULQDQ, true),
1146
  SUBARCH (fsgsbase, FSGSBASE, FSGSBASE, false),
1147
  SUBARCH (rdrnd, RDRND, RDRND, false),
1148
  SUBARCH (f16c, F16C, ANY_F16C, false),
1149
  SUBARCH (bmi2, BMI2, BMI2, false),
1150
  SUBARCH (fma, FMA, ANY_FMA, false),
1151
  SUBARCH (fma4, FMA4, ANY_FMA4, false),
1152
  SUBARCH (xop, XOP, ANY_XOP, false),
1153
  SUBARCH (lwp, LWP, ANY_LWP, false),
1154
  SUBARCH (movbe, MOVBE, MOVBE, false),
1155
  SUBARCH (cx16, CX16, CX16, false),
1156
  SUBARCH (altmovcr8, ALTMOVCR8, ALTMOVCR8, false),
1157
  SUBARCH (lahf_sahf, LAHF_SAHF, LAHF_SAHF, false),
1158
  SUBARCH (ept, EPT, ANY_EPT, false),
1159
  SUBARCH (lzcnt, LZCNT, LZCNT, false),
1160
  SUBARCH (popcnt, POPCNT, POPCNT, false),
1161
  SUBARCH (hle, HLE, HLE, false),
1162
  SUBARCH (rtm, RTM, ANY_RTM, false),
1163
  SUBARCH (tsx, TSX, TSX, false),
1164
  SUBARCH (invpcid, INVPCID, INVPCID, false),
1165
  SUBARCH (clflush, CLFLUSH, CLFLUSH, false),
1166
  SUBARCH (nop, NOP, NOP, false),
1167
  SUBARCH (syscall, SYSCALL, SYSCALL, false),
1168
  SUBARCH (rdtscp, RDTSCP, RDTSCP, false),
1169
  SUBARCH (3dnow, 3DNOW, ANY_3DNOW, false),
1170
  SUBARCH (3dnowa, 3DNOWA, ANY_3DNOWA, false),
1171
  SUBARCH (padlock, PADLOCK, PADLOCK, false),
1172
  SUBARCH (pacifica, SVME, ANY_SVME, true),
1173
  SUBARCH (svme, SVME, ANY_SVME, false),
1174
  SUBARCH (abm, ABM, ABM, false),
1175
  SUBARCH (bmi, BMI, BMI, false),
1176
  SUBARCH (tbm, TBM, TBM, false),
1177
  SUBARCH (adx, ADX, ADX, false),
1178
  SUBARCH (rdseed, RDSEED, RDSEED, false),
1179
  SUBARCH (prfchw, PRFCHW, PRFCHW, false),
1180
  SUBARCH (smap, SMAP, SMAP, false),
1181
  SUBARCH (mpx, MPX, ANY_MPX, false),
1182
  SUBARCH (sha, SHA, ANY_SHA, false),
1183
  SUBARCH (clflushopt, CLFLUSHOPT, CLFLUSHOPT, false),
1184
  SUBARCH (prefetchwt1, PREFETCHWT1, PREFETCHWT1, false),
1185
  SUBARCH (se1, SE1, SE1, false),
1186
  SUBARCH (clwb, CLWB, CLWB, false),
1187
  VECARCH (avx512ifma, AVX512IFMA, ANY_AVX512IFMA, reset),
1188
  VECARCH (avx512vbmi, AVX512VBMI, ANY_AVX512VBMI, reset),
1189
  VECARCH (avx512_4fmaps, AVX512_4FMAPS, ANY_AVX512_4FMAPS, reset),
1190
  VECARCH (avx512_4vnniw, AVX512_4VNNIW, ANY_AVX512_4VNNIW, reset),
1191
  VECARCH (avx512_vpopcntdq, AVX512_VPOPCNTDQ, ANY_AVX512_VPOPCNTDQ, reset),
1192
  VECARCH (avx512_vbmi2, AVX512_VBMI2, ANY_AVX512_VBMI2, reset),
1193
  VECARCH (avx512_vnni, AVX512_VNNI, ANY_AVX512_VNNI, reset),
1194
  VECARCH (avx512_bitalg, AVX512_BITALG, ANY_AVX512_BITALG, reset),
1195
  VECARCH (avx_vnni, AVX_VNNI, ANY_AVX_VNNI, reset),
1196
  SUBARCH (clzero, CLZERO, CLZERO, false),
1197
  SUBARCH (mwaitx, MWAITX, MWAITX, false),
1198
  SUBARCH (ospke, OSPKE, ANY_OSPKE, false),
1199
  SUBARCH (rdpid, RDPID, RDPID, false),
1200
  SUBARCH (ptwrite, PTWRITE, PTWRITE, false),
1201
  SUBARCH (ibt, IBT, IBT, false),
1202
  SUBARCH (shstk, SHSTK, SHSTK, false),
1203
  SUBARCH (gfni, GFNI, ANY_GFNI, false),
1204
  VECARCH (vaes, VAES, ANY_VAES, reset),
1205
  VECARCH (vpclmulqdq, VPCLMULQDQ, ANY_VPCLMULQDQ, reset),
1206
  SUBARCH (wbnoinvd, WBNOINVD, WBNOINVD, false),
1207
  SUBARCH (pconfig, PCONFIG, PCONFIG, false),
1208
  SUBARCH (waitpkg, WAITPKG, WAITPKG, false),
1209
  SUBARCH (cldemote, CLDEMOTE, CLDEMOTE, false),
1210
  SUBARCH (ace_v1, ACE_V1, ANY_ACE_V1, false),
1211
  SUBARCH (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1212
  SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
1213
  SUBARCH (amx_fp16, AMX_FP16, ANY_AMX_FP16, false),
1214
  SUBARCH (amx_complex, AMX_COMPLEX, ANY_AMX_COMPLEX, false),
1215
  SUBARCH (amx_transpose, AMX_TRANSPOSE, ANY_AMX_TRANSPOSE, false),
1216
  SUBARCH (amx_tf32, AMX_TF32, ANY_AMX_TF32, false),
1217
  SUBARCH (amx_fp8, AMX_FP8, ANY_AMX_FP8, false),
1218
  SUBARCH (amx_movrs, AMX_MOVRS, ANY_AMX_MOVRS, false),
1219
  SUBARCH (amx_avx512, AMX_AVX512, ANY_AMX_AVX512, false),
1220
  SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
1221
  SUBARCH (movdiri, MOVDIRI, MOVDIRI, false),
1222
  SUBARCH (movdir64b, MOVDIR64B, MOVDIR64B, false),
1223
  VECARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, reset),
1224
  VECARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1225
     ANY_AVX512_VP2INTERSECT, reset),
1226
  VECARCH (avx512_bmm, AVX512_BMM, ANY_AVX512_BMM, reset),
1227
  SUBARCH (tdx, TDX, TDX, false),
1228
  SUBARCH (enqcmd, ENQCMD, ENQCMD, false),
1229
  SUBARCH (serialize, SERIALIZE, SERIALIZE, false),
1230
  SUBARCH (rdpru, RDPRU, RDPRU, false),
1231
  SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
1232
  SUBARCH (sev_es, SEV_ES, ANY_SEV_ES, false),
1233
  SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
1234
  SUBARCH (kl, KL, ANY_KL, false),
1235
  SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
1236
  SUBARCH (uintr, UINTR, UINTR, false),
1237
  SUBARCH (hreset, HRESET, HRESET, false),
1238
  VECARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, reset),
1239
  SUBARCH (prefetchi, PREFETCHI, PREFETCHI, false),
1240
  VECARCH (avx_ifma, AVX_IFMA, ANY_AVX_IFMA, reset),
1241
  VECARCH (avx_vnni_int8, AVX_VNNI_INT8, ANY_AVX_VNNI_INT8, reset),
1242
  SUBARCH (cmpccxadd, CMPCCXADD, CMPCCXADD, false),
1243
  SUBARCH (wrmsrns, WRMSRNS, WRMSRNS, false),
1244
  SUBARCH (msrlist, MSRLIST, MSRLIST, false),
1245
  VECARCH (avx_ne_convert, AVX_NE_CONVERT, ANY_AVX_NE_CONVERT, reset),
1246
  SUBARCH (rao_int, RAO_INT, RAO_INT, false),
1247
  SUBARCH (rmpquery, RMPQUERY, ANY_RMPQUERY, false),
1248
  SUBARCH (rmpread, RMPREAD, ANY_RMPREAD, false),
1249
  SUBARCH (rmpdirty, RMPDIRTY, ANY_RMPDIRTY, false),
1250
  SUBARCH (rmpopt, RMPOPT, ANY_RMPOPT, false),
1251
  SUBARCH (fred, FRED, ANY_FRED, false),
1252
  SUBARCH (lkgs, LKGS, ANY_LKGS, false),
1253
  VECARCH (avx_vnni_int16, AVX_VNNI_INT16, ANY_AVX_VNNI_INT16, reset),
1254
  VECARCH (sha512, SHA512, ANY_SHA512, reset),
1255
  VECARCH (sm3, SM3, ANY_SM3, reset),
1256
  VECARCH (sm4, SM4, ANY_SM4, reset),
1257
  SUBARCH (pbndkb, PBNDKB, PBNDKB, false),
1258
  VECARCH (avx10.1, AVX10_1, ANY_AVX512F, set),
1259
  VECARCH (avx10.1aux, AVX10_1_AUX, ANY_AVX10_1_AUX, set),
1260
  VECARCH (avx10v1aux, AVX10_1_AUX, ANY_AVX10_1_AUX, set),
1261
  VECARCH (avx10.2, AVX10_2, ANY_AVX10_2, set),
1262
  VECARCH (avx10v2aux, AVX10_V2_AUX, ANY_AVX10_V2_AUX, set),
1263
  VECARCH (avx10_vpmldsabflydq, VPMLDSABFLYDQ, ANY_VPMLDSABFLYDQ,
1264
     reset),
1265
  SUBARCH (user_msr, USER_MSR, USER_MSR, false),
1266
  SUBARCH (apx_f, APX_F, ANY_APX_F, false),
1267
  SUBARCH (apx_nci, APX_NCI, ANY_APX_NCI, false),
1268
  SUBARCH (apx_ndd, APX_NDD, ANY_APX_NDD, false),
1269
  SUBARCH (apx_nf, APX_NF, ANY_APX_NF, false),
1270
  SUBARCH (apx_nci_ndd_nf, APX_NCI_NDD_NF, ANY_APX_NCI_NDD_NF, false),
1271
  SUBARCH (gmism2, GMISM2, GMISM2, false),
1272
  SUBARCH (gmiccs, GMICCS, GMICCS, false),
1273
  SUBARCH (msr_imm, MSR_IMM, MSR_IMM, false),
1274
  SUBARCH (padlockrng2, PADLOCKRNG2, PADLOCKRNG2, false),
1275
  SUBARCH (padlockphe2, PADLOCKPHE2, PADLOCKPHE2, false),
1276
  SUBARCH (padlockxmodx, PADLOCKXMODX, PADLOCKXMODX, false),
1277
  SUBARCH (movrs, MOVRS, MOVRS, false),
1278
  SUBARCH (doitm, DOITM, DOITM, false),
1279
};
1280
1281
#undef SUBARCH
1282
#undef ARCH
1283
1284
#ifdef I386COFF
1285
/* Like s_lcomm_internal in gas/read.c but the alignment string
1286
   is allowed to be optional.  */
1287
1288
static symbolS *
1289
pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1290
{
1291
  addressT align = 0;
1292
1293
  SKIP_WHITESPACE ();
1294
1295
  if (needs_align
1296
      && *input_line_pointer == ',')
1297
    {
1298
      align = parse_align (needs_align - 1);
1299
1300
      if (align == (addressT) -1)
1301
  return NULL;
1302
    }
1303
  else
1304
    {
1305
      if (size >= 8)
1306
  align = 3;
1307
      else if (size >= 4)
1308
  align = 2;
1309
      else if (size >= 2)
1310
  align = 1;
1311
      else
1312
  align = 0;
1313
    }
1314
1315
  bss_alloc (symbolP, size, align);
1316
  return symbolP;
1317
}
1318
1319
static void
1320
pe_lcomm (int needs_align)
1321
{
1322
  s_comm_internal (needs_align * 2, pe_lcomm_internal);
1323
}
1324
#endif
1325
1326
const pseudo_typeS md_pseudo_table[] =
1327
{
1328
#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1329
  {"align", s_align_bytes, 0},
1330
#else
1331
  {"align", s_align_ptwo, 0},
1332
#endif
1333
  {"arch", set_cpu_arch, 0},
1334
#ifdef OBJ_AOUT
1335
  {"bss", s_bss, 0},
1336
#endif
1337
#ifdef I386COFF
1338
  {"lcomm", pe_lcomm, 1},
1339
#endif
1340
  {"ffloat", float_cons, 'f'},
1341
  {"dfloat", float_cons, 'd'},
1342
  {"tfloat", float_cons, 'x'},
1343
  {"hfloat", float_cons, 'h'},
1344
  {"bfloat16", float_cons, 'b'},
1345
  {"value", cons, 2},
1346
  {"slong", signed_cons, 4},
1347
  {"insn", s_insn, 0},
1348
  {"noopt", s_noopt, 0},
1349
  {"optim", s_ignore, 0},
1350
  {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1351
  {"code16", set_code_flag, CODE_16BIT},
1352
  {"code32", set_code_flag, CODE_32BIT},
1353
#ifdef BFD64
1354
  {"code64", set_code_flag, CODE_64BIT},
1355
#endif
1356
  {"intel_syntax", set_intel_syntax, 1},
1357
  {"att_syntax", set_intel_syntax, 0},
1358
  {"intel_mnemonic", set_intel_mnemonic, 1},
1359
  {"att_mnemonic", set_intel_mnemonic, 0},
1360
  {"allow_index_reg", set_allow_index_reg, 1},
1361
  {"disallow_index_reg", set_allow_index_reg, 0},
1362
  {"sse_check", set_check, 0},
1363
  {"operand_check", set_check, 1},
1364
#ifdef OBJ_ELF
1365
  {"largecomm", handle_large_common, 0},
1366
#else
1367
  {"file", dwarf2_directive_file, 0},
1368
  {"loc", dwarf2_directive_loc, 0},
1369
  {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
1370
#endif
1371
#ifdef TE_PE
1372
  {"secrel32", pe_directive_secrel, 0},
1373
  {"secidx", pe_directive_secidx, 0},
1374
#endif
1375
  {0, 0, 0}
1376
};
1377
1378
/* For interface with expression ().  */
1379
extern char *input_line_pointer;
1380
1381
/* Hash table for instruction mnemonic lookup.  */
1382
static htab_t op_hash;
1383
1384
/* Hash table for register lookup.  */
1385
static htab_t reg_hash;
1386
1387
#include "opcodes/i386-tbl.h"
1388
1389
#if (defined (OBJ_ELF) || defined (OBJ_MACH_O) || defined (TE_PE))
1390
static const struct
1391
{
1392
  const char *str;
1393
  unsigned int len;
1394
  const enum bfd_reloc_code_real rel[2];
1395
  const i386_operand_type types64;
1396
  bool need_GOT_symbol;
1397
}
1398
gotrel[] =
1399
{
1400
#define OPERAND_TYPE_IMM32_32S_DISP32 { .bitfield = \
1401
      { .imm32 = 1, .imm32s = 1, .disp32 = 1 } }
1402
#define OPERAND_TYPE_IMM32_32S_64_DISP32 { .bitfield = \
1403
      { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1 } }
1404
#define OPERAND_TYPE_IMM32_32S_64_DISP32_64 { .bitfield = \
1405
      { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1, .disp64 = 1 } }
1406
#define OPERAND_TYPE_IMM64_DISP64 { .bitfield = \
1407
      { .imm64 = 1, .disp64 = 1 } }
1408
1409
#ifndef TE_PE
1410
#ifdef OBJ_ELF
1411
    { STRING_COMMA_LEN ("SIZE"),      { BFD_RELOC_SIZE32,
1412
          BFD_RELOC_SIZE32 },
1413
    { .bitfield = { .imm32 = 1, .imm64 = 1 } }, false },
1414
#endif
1415
    { STRING_COMMA_LEN ("PLTOFF"),   { _dummy_first_bfd_reloc_code_real,
1416
               BFD_RELOC_64_PLTOFF },
1417
    { .bitfield = { .imm64 = 1 } }, true },
1418
    { STRING_COMMA_LEN ("PLT"),      { BFD_RELOC_386_PLT32,
1419
               BFD_RELOC_32_PLT_PCREL },
1420
    OPERAND_TYPE_IMM32_32S_DISP32, false },
1421
    { STRING_COMMA_LEN ("GOTPLT"),   { _dummy_first_bfd_reloc_code_real,
1422
               BFD_RELOC_X86_64_GOTPLT64 },
1423
    OPERAND_TYPE_IMM64_DISP64, true },
1424
    { STRING_COMMA_LEN ("GOTOFF"),   { BFD_RELOC_32_GOTOFF,
1425
               BFD_RELOC_64_GOTOFF },
1426
    OPERAND_TYPE_IMM64_DISP64, true },
1427
    { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
1428
               BFD_RELOC_X86_64_GOTPCREL },
1429
    OPERAND_TYPE_IMM32_32S_DISP32, true },
1430
    { STRING_COMMA_LEN ("TLSGD"),    { BFD_RELOC_386_TLS_GD,
1431
               BFD_RELOC_X86_64_TLSGD    },
1432
    OPERAND_TYPE_IMM32_32S_DISP32, true },
1433
    { STRING_COMMA_LEN ("TLSLDM"),   { BFD_RELOC_386_TLS_LDM,
1434
               _dummy_first_bfd_reloc_code_real },
1435
    OPERAND_TYPE_NONE, true },
1436
    { STRING_COMMA_LEN ("TLSLD"),    { _dummy_first_bfd_reloc_code_real,
1437
               BFD_RELOC_X86_64_TLSLD    },
1438
    OPERAND_TYPE_IMM32_32S_DISP32, true },
1439
    { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
1440
               BFD_RELOC_X86_64_GOTTPOFF },
1441
    OPERAND_TYPE_IMM32_32S_DISP32, true },
1442
    { STRING_COMMA_LEN ("TPOFF"),    { BFD_RELOC_386_TLS_LE_32,
1443
               BFD_RELOC_X86_64_TPOFF32  },
1444
    OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
1445
    { STRING_COMMA_LEN ("NTPOFF"),   { BFD_RELOC_386_TLS_LE,
1446
               _dummy_first_bfd_reloc_code_real },
1447
    OPERAND_TYPE_NONE, true },
1448
    { STRING_COMMA_LEN ("DTPOFF"),   { BFD_RELOC_386_TLS_LDO_32,
1449
               BFD_RELOC_X86_64_DTPOFF32 },
1450
    OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
1451
    { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
1452
               _dummy_first_bfd_reloc_code_real },
1453
    OPERAND_TYPE_NONE, true },
1454
    { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
1455
               _dummy_first_bfd_reloc_code_real },
1456
    OPERAND_TYPE_NONE, true },
1457
    { STRING_COMMA_LEN ("GOT"),      { BFD_RELOC_386_GOT32,
1458
               BFD_RELOC_X86_64_GOT32    },
1459
    OPERAND_TYPE_IMM32_32S_64_DISP32, true },
1460
    { STRING_COMMA_LEN ("TLSDESC"),  { BFD_RELOC_386_TLS_GOTDESC,
1461
               BFD_RELOC_X86_64_GOTPC32_TLSDESC },
1462
    OPERAND_TYPE_IMM32_32S_DISP32, true },
1463
    { STRING_COMMA_LEN ("TLSCALL"),  { BFD_RELOC_386_TLS_DESC_CALL,
1464
               BFD_RELOC_X86_64_TLSDESC_CALL },
1465
    OPERAND_TYPE_IMM32_32S_DISP32, true },
1466
#else /* TE_PE */
1467
    { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
1468
               BFD_RELOC_32_SECREL },
1469
    OPERAND_TYPE_IMM32_32S_DISP32, false },
1470
    { STRING_COMMA_LEN ("SECIDX16"), { BFD_RELOC_16_SECIDX,
1471
               BFD_RELOC_16_SECIDX },
1472
    { .bitfield = { .imm16 = 1, .disp16 = 1 } }, false },
1473
    { STRING_COMMA_LEN ("RVA"), { BFD_RELOC_RVA,
1474
               BFD_RELOC_RVA },
1475
    OPERAND_TYPE_IMM32_32S_DISP32, false },
1476
    { STRING_COMMA_LEN ("IMGREL"), { BFD_RELOC_RVA,
1477
               BFD_RELOC_RVA },
1478
    OPERAND_TYPE_IMM32_32S_DISP32, false },
1479
#endif
1480
1481
#undef OPERAND_TYPE_IMM32_32S_DISP32
1482
#undef OPERAND_TYPE_IMM32_32S_64_DISP32
1483
#undef OPERAND_TYPE_IMM32_32S_64_DISP32_64
1484
#undef OPERAND_TYPE_IMM64_DISP64
1485
};
1486
#endif
1487

1488
  /* Various efficient no-op patterns for aligning code labels.
1489
     Note: Don't try to assemble the instructions in the comments.
1490
     0L and 0w are not legal.  */
1491
static const unsigned char f32_1[] =
1492
  {0x90};       /* nop      */
1493
static const unsigned char f32_2[] =
1494
  {0x66,0x90};        /* xchg %ax,%ax   */
1495
static const unsigned char f32_3[] =
1496
  {0x8d,0x76,0x00};     /* leal 0(%esi),%esi  */
1497
#define f32_4 (f32_5 + 1) /* leal 0(%esi,%eiz),%esi */
1498
static const unsigned char f32_5[] =
1499
  {0x2e,0x8d,0x74,0x26,0x00};   /* leal %cs:0(%esi,%eiz),%esi */
1500
static const unsigned char f32_6[] =
1501
  {0x8d,0xb6,0x00,0x00,0x00,0x00};  /* leal 0L(%esi),%esi */
1502
#define f32_7 (f32_8 + 1) /* leal 0L(%esi,%eiz),%esi */
1503
static const unsigned char f32_8[] =
1504
  {0x2e,0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal %cs:0L(%esi,%eiz),%esi */
1505
static const unsigned char f64_3[] =
1506
  {0x48,0x89,0xf6};     /* mov %rsi,%rsi  */
1507
static const unsigned char f64_4[] =
1508
  {0x48,0x8d,0x76,0x00};    /* lea 0(%rsi),%rsi */
1509
#define f64_5 (f64_6 + 1)   /* lea 0(%rsi,%riz),%rsi  */
1510
static const unsigned char f64_6[] =
1511
  {0x2e,0x48,0x8d,0x74,0x26,0x00};  /* lea %cs:0(%rsi,%riz),%rsi  */
1512
static const unsigned char f64_7[] =
1513
  {0x48,0x8d,0xb6,0x00,0x00,0x00,0x00}; /* lea 0L(%rsi),%rsi  */
1514
#define f64_8 (f64_9 + 1)   /* lea 0L(%rsi,%riz),%rsi */
1515
static const unsigned char f64_9[] =
1516
  {0x2e,0x48,0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* lea %cs:0L(%rsi,%riz),%rsi */
1517
#define f16_2 (f64_3 + 1)   /* mov %si,%si  */
1518
static const unsigned char f16_3[] =
1519
  {0x8d,0x74,0x00};     /* lea 0(%si),%si */
1520
#define f16_4 (f16_5 + 1)   /* lea 0W(%si),%si */
1521
static const unsigned char f16_5[] =
1522
  {0x2e,0x8d,0xb4,0x00,0x00};   /* lea %cs:0W(%si),%si  */
1523
static const unsigned char jump_disp8[] =
1524
  {0xeb};       /* jmp disp8         */
1525
static const unsigned char jump32_disp32[] =
1526
  {0xe9};       /* jmp disp32        */
1527
static const unsigned char jump16_disp32[] =
1528
  {0x66,0xe9};        /* jmp disp32        */
1529
/* 32-bit NOPs patterns.  */
1530
static const unsigned char *const f32_patt[] = {
1531
  f32_1, f32_2, f32_3, f32_4, f32_5, f32_6, f32_7, f32_8
1532
};
1533
/* 64-bit NOPs patterns.  */
1534
static const unsigned char *const f64_patt[] = {
1535
  f32_1, f32_2, f64_3, f64_4, f64_5, f64_6, f64_7, f64_8, f64_9
1536
};
1537
/* 16-bit NOPs patterns.  */
1538
static const unsigned char *const f16_patt[] = {
1539
  f32_1, f16_2, f16_3, f16_4, f16_5
1540
};
1541
/* nopl (%[re]ax) */
1542
static const unsigned char alt_3[] =
1543
  {0x0f,0x1f,0x00};
1544
/* nopl 0(%[re]ax) */
1545
static const unsigned char alt_4[] =
1546
  {0x0f,0x1f,0x40,0x00};
1547
/* nopl 0(%[re]ax,%[re]ax,1) */
1548
#define alt_5 (alt_6 + 1)
1549
/* nopw 0(%[re]ax,%[re]ax,1) */
1550
static const unsigned char alt_6[] =
1551
  {0x66,0x0f,0x1f,0x44,0x00,0x00};
1552
/* nopl 0L(%[re]ax) */
1553
static const unsigned char alt_7[] =
1554
  {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1555
/* nopl 0L(%[re]ax,%[re]ax,1) */
1556
#define alt_8 (alt_9 + 1)
1557
/* nopw 0L(%[re]ax,%[re]ax,1) */
1558
static const unsigned char alt_9[] =
1559
  {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1560
/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1561
#define alt_10 (alt_11 + 1)
1562
/* data16 nopw %cs:0L(%eax,%eax,1) */
1563
static const unsigned char alt_11[] =
1564
  {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1565
/* 32-bit and 64-bit NOPs patterns.  */
1566
static const unsigned char *const alt_patt[] = {
1567
  f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
1568
  alt_9, alt_10, alt_11
1569
};
1570
#define alt64_9 (alt64_15 + 6)    /* nopq 0L(%rax,%rax,1)  */
1571
#define alt64_10 (alt64_15 + 5)   /* cs nopq 0L(%rax,%rax,1)  */
1572
/* data16 cs nopq 0L(%rax,%rax,1)  */
1573
#define alt64_11 (alt64_15 + 4)
1574
/* data16 data16 cs nopq 0L(%rax,%rax,1)  */
1575
#define alt64_12 (alt64_15 + 3)
1576
/* data16 data16 data16 cs nopq 0L(%rax,%rax,1)  */
1577
#define alt64_13 (alt64_15 + 2)
1578
/* data16 data16 data16 data16 cs nopq 0L(%rax,%rax,1)  */
1579
#define alt64_14 (alt64_15 + 1)
1580
/* data16 data16 data16 data16 data16 cs nopq 0L(%rax,%rax,1)  */
1581
static const unsigned char alt64_15[] =
1582
  {0x66,0x66,0x66,0x66,0x66,0x2e,0x48,
1583
   0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1584
/* Long 64-bit NOPs patterns.  */
1585
static const unsigned char *const alt64_patt[] = {
1586
  f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
1587
  alt64_9, alt64_10, alt64_11,alt64_12, alt64_13, alt64_14, alt64_15
1588
};
1589
1590
static INLINE int
1591
fits_in_imm7 (offsetT num)
1592
0
{
1593
0
  return (num & 0x7f) == num;
1594
0
}
1595
1596
static INLINE int
1597
fits_in_imm31 (offsetT num)
1598
0
{
1599
0
  return (num & 0x7fffffff) == num;
1600
0
}
1601
1602
/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1603
   single NOP instruction LIMIT.  */
1604
1605
void
1606
i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
1607
0
{
1608
0
  const unsigned char *const *patt = NULL;
1609
0
  int max_single_nop_size;
1610
  /* Maximum number of NOPs before switching to jump over NOPs.  */
1611
0
  int max_number_of_nops;
1612
1613
0
  switch (fragP->fr_type)
1614
0
    {
1615
0
    case rs_fill_nop:
1616
0
    case rs_align_code:
1617
0
      break;
1618
0
    case rs_machine_dependent:
1619
      /* Allow NOP padding for jumps and calls.  */
1620
0
      if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1621
0
    || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1622
0
  break;
1623
      /* Fall through.  */
1624
0
    default:
1625
0
      return;
1626
0
    }
1627
1628
  /* We need to decide which NOP sequence to use for 32bit and
1629
     64bit. When -mtune= is used:
1630
1631
     1. For PROCESSOR_I?86, PROCESSOR_PENTIUM, PROCESSOR_IAMCU, and
1632
     PROCESSOR_GENERIC32, f32_patt will be used.
1633
     2. For the rest, alt_patt will be used.
1634
1635
     When -mtune= isn't used, alt_patt will be used if
1636
     cpu_arch_isa_flags has CpuNop.  Otherwise, f32_patt/f64_patt will
1637
     be used.
1638
1639
     When -march= or .arch is used, we can't use anything beyond
1640
     cpu_arch_isa_flags.   */
1641
1642
0
  if (fragP->tc_frag_data.code == CODE_16BIT)
1643
0
    {
1644
0
      patt = f16_patt;
1645
0
      max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1646
      /* Limit number of NOPs to 2 in 16-bit mode.  */
1647
0
      max_number_of_nops = 2;
1648
0
    }
1649
0
  else
1650
0
    {
1651
0
      patt = fragP->tc_frag_data.code == CODE_64BIT ? f64_patt : f32_patt;
1652
0
      if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
1653
0
  {
1654
    /* PROCESSOR_UNKNOWN means that all ISAs may be used, unless
1655
       explicitly disabled.  */
1656
0
    switch (fragP->tc_frag_data.tune)
1657
0
      {
1658
0
      case PROCESSOR_UNKNOWN:
1659
        /* We use cpu_arch_isa_flags to check if we SHOULD
1660
     optimize with nops.  */
1661
0
        if (fragP->tc_frag_data.isanop)
1662
0
    patt = alt_patt;
1663
0
        break;
1664
1665
0
      case PROCESSOR_CORE:
1666
0
      case PROCESSOR_CORE2:
1667
0
      case PROCESSOR_COREI7:
1668
0
        if (fragP->tc_frag_data.cpunop)
1669
0
    {
1670
0
      if (fragP->tc_frag_data.code == CODE_64BIT)
1671
0
        patt = alt64_patt;
1672
0
      else
1673
0
        patt = alt_patt;
1674
0
    }
1675
0
        break;
1676
1677
0
      case PROCESSOR_PENTIUMPRO:
1678
0
      case PROCESSOR_PENTIUM4:
1679
0
      case PROCESSOR_NOCONA:
1680
0
      case PROCESSOR_GENERIC64:
1681
0
      case PROCESSOR_K6:
1682
0
      case PROCESSOR_ATHLON:
1683
0
      case PROCESSOR_K8:
1684
0
      case PROCESSOR_AMDFAM10:
1685
0
      case PROCESSOR_BD:
1686
0
      case PROCESSOR_ZNVER:
1687
0
      case PROCESSOR_BT:
1688
0
        if (fragP->tc_frag_data.cpunop)
1689
0
    patt = alt_patt;
1690
0
        break;
1691
1692
0
      case PROCESSOR_I386:
1693
0
      case PROCESSOR_I486:
1694
0
      case PROCESSOR_PENTIUM:
1695
0
      case PROCESSOR_I686:
1696
0
      case PROCESSOR_IAMCU:
1697
0
      case PROCESSOR_GENERIC32:
1698
0
        break;
1699
0
      case PROCESSOR_NONE:
1700
0
        abort ();
1701
0
      }
1702
0
  }
1703
0
      else
1704
0
  {
1705
0
    switch (fragP->tc_frag_data.tune)
1706
0
      {
1707
0
      case PROCESSOR_UNKNOWN:
1708
        /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
1709
     PROCESSOR_UNKNOWN.  */
1710
0
        abort ();
1711
0
        break;
1712
1713
0
      default:
1714
        /* We use cpu_arch_isa_flags to check if we CAN optimize
1715
     with nops.  */
1716
0
        if (fragP->tc_frag_data.isanop)
1717
0
    patt = alt_patt;
1718
0
        break;
1719
1720
0
      case PROCESSOR_NONE:
1721
0
        abort ();
1722
0
      }
1723
0
  }
1724
1725
0
      if (patt != alt_patt && patt != alt64_patt)
1726
0
  {
1727
0
    max_single_nop_size = patt == f32_patt ? ARRAY_SIZE (f32_patt)
1728
0
             : ARRAY_SIZE (f64_patt);
1729
    /* Limit number of NOPs to 2 for older processors.  */
1730
0
    max_number_of_nops = 2;
1731
0
  }
1732
0
      else
1733
0
  {
1734
0
    max_single_nop_size = patt == alt_patt
1735
0
        ? ARRAY_SIZE (alt_patt)
1736
0
        : ARRAY_SIZE (alt64_patt);
1737
    /* Limit number of NOPs to 7 for newer processors.  */
1738
0
    max_number_of_nops = 7;
1739
0
  }
1740
0
    }
1741
1742
0
  if (limit == 0)
1743
0
    limit = max_single_nop_size;
1744
1745
0
  if (limit > max_single_nop_size || limit < 1)
1746
0
    {
1747
0
      as_bad_where (fragP->fr_file, fragP->fr_line,
1748
0
        _("invalid single nop size: %d "
1749
0
          "(expect within [0, %d])"),
1750
0
        limit, max_single_nop_size);
1751
0
      return;
1752
0
    }
1753
1754
  /* Emit a plain NOP first when the last thing we saw may not have been
1755
     a proper instruction (e.g. a stand-alone prefix or .byte).  */
1756
0
  if (!fragP->tc_frag_data.last_insn_normal)
1757
0
    {
1758
0
      *where++ = 0x90;
1759
0
      --count;
1760
0
    }
1761
1762
0
  if ((count / max_single_nop_size) > max_number_of_nops)
1763
0
    {
1764
      /* Generate jump over NOPs.  */
1765
0
      offsetT disp = count - 2;
1766
0
      if (fits_in_imm7 (disp))
1767
0
  {
1768
    /* Use "jmp disp8" if possible.  */
1769
0
    count = disp;
1770
0
    where[0] = jump_disp8[0];
1771
0
    where[1] = count;
1772
0
    where += 2;
1773
0
  }
1774
0
      else
1775
0
  {
1776
0
    unsigned int size_of_jump;
1777
1778
0
    if (flag_code == CODE_16BIT)
1779
0
      {
1780
0
        where[0] = jump16_disp32[0];
1781
0
        where[1] = jump16_disp32[1];
1782
0
        size_of_jump = 2;
1783
0
      }
1784
0
    else
1785
0
      {
1786
0
        where[0] = jump32_disp32[0];
1787
0
        size_of_jump = 1;
1788
0
      }
1789
1790
0
    count -= size_of_jump + 4;
1791
0
    if (!fits_in_imm31 (count))
1792
0
      {
1793
0
        as_bad_where (fragP->fr_file, fragP->fr_line,
1794
0
          _("jump over nop padding out of range"));
1795
0
        return;
1796
0
      }
1797
1798
0
    md_number_to_chars (where + size_of_jump, count, 4);
1799
0
    where += size_of_jump + 4;
1800
0
  }
1801
0
    }
1802
1803
0
  int non_repeat = count % limit;
1804
0
  if (non_repeat)
1805
0
    {
1806
0
      memcpy (where, patt[non_repeat - 1], non_repeat);
1807
0
      where += non_repeat;
1808
0
      count -= non_repeat;
1809
0
    }
1810
1811
0
  if (fragP->fr_type != rs_machine_dependent)
1812
0
    {
1813
      /* Set up the frag so that everything we have emitted so far is
1814
   included in fr_fix.  The repeating larger nop only needs to
1815
   be written once to the frag memory.  */
1816
0
      fragP->fr_fix = where - fragP->fr_literal;
1817
0
      if (count != 0)
1818
0
  {
1819
0
    fragP->fr_var = limit;
1820
0
    count = limit;
1821
0
  }
1822
0
    }
1823
1824
0
  const unsigned char *nops = patt[limit - 1];
1825
0
  while (count)
1826
0
    {
1827
0
      memcpy (where, nops, limit);
1828
0
      where += limit;
1829
0
      count -= limit;
1830
0
    }
1831
0
}
1832
1833
static INLINE int
1834
operand_type_all_zero (const union i386_operand_type *x)
1835
52.4k
{
1836
52.4k
  switch (ARRAY_SIZE(x->array))
1837
52.4k
    {
1838
0
    case 3:
1839
0
      if (x->array[2])
1840
0
  return 0;
1841
      /* Fall through.  */
1842
0
    case 2:
1843
0
      if (x->array[1])
1844
0
  return 0;
1845
      /* Fall through.  */
1846
52.4k
    case 1:
1847
52.4k
      return !x->array[0];
1848
0
    default:
1849
0
      abort ();
1850
52.4k
    }
1851
52.4k
}
1852
1853
static INLINE void
1854
operand_type_set (union i386_operand_type *x, unsigned int v)
1855
40.5k
{
1856
40.5k
  switch (ARRAY_SIZE(x->array))
1857
40.5k
    {
1858
0
    case 3:
1859
0
      x->array[2] = v;
1860
      /* Fall through.  */
1861
0
    case 2:
1862
0
      x->array[1] = v;
1863
      /* Fall through.  */
1864
40.5k
    case 1:
1865
40.5k
      x->array[0] = v;
1866
      /* Fall through.  */
1867
40.5k
      break;
1868
0
    default:
1869
0
      abort ();
1870
40.5k
    }
1871
1872
40.5k
  x->bitfield.class = ClassNone;
1873
40.5k
  x->bitfield.instance = InstanceNone;
1874
40.5k
}
1875
1876
static INLINE int
1877
operand_type_equal (const union i386_operand_type *x,
1878
        const union i386_operand_type *y)
1879
6.72k
{
1880
6.72k
  switch (ARRAY_SIZE(x->array))
1881
6.72k
    {
1882
0
    case 3:
1883
0
      if (x->array[2] != y->array[2])
1884
0
  return 0;
1885
      /* Fall through.  */
1886
0
    case 2:
1887
0
      if (x->array[1] != y->array[1])
1888
0
  return 0;
1889
      /* Fall through.  */
1890
6.72k
    case 1:
1891
6.72k
      return x->array[0] == y->array[0];
1892
0
      break;
1893
0
    default:
1894
0
      abort ();
1895
6.72k
    }
1896
6.72k
}
1897
1898
static INLINE bool
1899
_is_cpu (const i386_cpu_attr *a, enum i386_cpu cpu)
1900
264k
{
1901
264k
  switch (cpu)
1902
264k
    {
1903
6.59k
    case Cpu287:      return a->bitfield.cpu287;
1904
6.59k
    case Cpu387:      return a->bitfield.cpu387;
1905
0
    case Cpu3dnow:    return a->bitfield.cpu3dnow;
1906
0
    case Cpu3dnowA:   return a->bitfield.cpu3dnowa;
1907
12.1k
    case CpuAVX:      return a->bitfield.cpuavx;
1908
516
    case CpuHLE:      return a->bitfield.cpuhle;
1909
7.16k
    case CpuAVX512F:  return a->bitfield.cpuavx512f;
1910
6.59k
    case CpuAVX512VL: return a->bitfield.cpuavx512vl;
1911
9.87k
    case CpuAPX_F:    return a->bitfield.cpuapx_f;
1912
0
    case CpuAVX10_2:  return a->bitfield.cpuavx10_2;
1913
0
    case CpuAMX_TRANSPOSE:  return a->bitfield.cpuamx_transpose;
1914
31
    case CpuACE_V1:   return a->bitfield.cpuace_v1;
1915
0
    case Cpu64:       return a->bitfield.cpu64;
1916
0
    case CpuNo64:     return a->bitfield.cpuno64;
1917
215k
    default:
1918
215k
      gas_assert (cpu < CpuAttrEnums);
1919
264k
    }
1920
215k
  return a->bitfield.isa == cpu + 1u;
1921
264k
}
1922
1923
static INLINE bool
1924
is_cpu (const insn_template *t, enum i386_cpu cpu)
1925
259k
{
1926
259k
  return _is_cpu(&t->cpu, cpu);
1927
259k
}
1928
1929
static INLINE bool
1930
maybe_cpu (const insn_template *t, enum i386_cpu cpu)
1931
5.53k
{
1932
5.53k
  return _is_cpu(&t->cpu_any, cpu);
1933
5.53k
}
1934
1935
static i386_cpu_flags cpu_flags_from_attr (i386_cpu_attr a)
1936
236k
{
1937
236k
  const unsigned int bps = sizeof (a.array[0]) * CHAR_BIT;
1938
236k
  i386_cpu_flags f = { .array[0] = 0 };
1939
1940
236k
  switch (ARRAY_SIZE (a.array))
1941
236k
    {
1942
236k
    case 1:
1943
236k
      f.array[CpuAttrEnums / bps]
1944
236k
#ifndef WORDS_BIGENDIAN
1945
236k
  |= (a.array[0] >> CpuIsaBits) << (CpuAttrEnums % bps);
1946
#else
1947
  |= (a.array[0] << CpuIsaBits) >> (CpuAttrEnums % bps);
1948
#endif
1949
236k
      if (CpuMax / bps > CpuAttrEnums / bps)
1950
236k
  f.array[CpuAttrEnums / bps + 1]
1951
236k
#ifndef WORDS_BIGENDIAN
1952
236k
    = (a.array[0] >> CpuIsaBits) >> (bps - CpuAttrEnums % bps);
1953
#else
1954
    = (a.array[0] << CpuIsaBits) << (bps - CpuAttrEnums % bps);
1955
#endif
1956
236k
      break;
1957
1958
0
    default:
1959
0
      abort ();
1960
236k
    }
1961
1962
236k
  if (a.bitfield.isa)
1963
38.1k
#ifndef WORDS_BIGENDIAN
1964
38.1k
    f.array[(a.bitfield.isa - 1) / bps] |= 1u << ((a.bitfield.isa - 1) % bps);
1965
#else
1966
    f.array[(a.bitfield.isa - 1) / bps] |= 1u << (~(a.bitfield.isa - 1) % bps);
1967
#endif
1968
1969
236k
  return f;
1970
236k
}
1971
1972
static INLINE int
1973
cpu_flags_all_zero (const union i386_cpu_flags *x)
1974
212k
{
1975
212k
  switch (ARRAY_SIZE(x->array))
1976
212k
    {
1977
212k
    case 6:
1978
212k
      if (x->array[5])
1979
1.96k
  return 0;
1980
      /* Fall through.  */
1981
210k
    case 5:
1982
210k
      if (x->array[4])
1983
19.0k
  return 0;
1984
      /* Fall through.  */
1985
191k
    case 4:
1986
191k
      if (x->array[3])
1987
1.82k
  return 0;
1988
      /* Fall through.  */
1989
189k
    case 3:
1990
189k
      if (x->array[2])
1991
5
  return 0;
1992
      /* Fall through.  */
1993
189k
    case 2:
1994
189k
      if (x->array[1])
1995
7
  return 0;
1996
      /* Fall through.  */
1997
189k
    case 1:
1998
189k
      return !x->array[0];
1999
0
    default:
2000
0
      abort ();
2001
212k
    }
2002
212k
}
2003
2004
static INLINE int
2005
cpu_flags_equal (const union i386_cpu_flags *x,
2006
     const union i386_cpu_flags *y)
2007
39.4k
{
2008
39.4k
  switch (ARRAY_SIZE(x->array))
2009
39.4k
    {
2010
39.4k
    case 6:
2011
39.4k
      if (x->array[5] != y->array[5])
2012
884
  return 0;
2013
      /* Fall through.  */
2014
38.5k
    case 5:
2015
38.5k
      if (x->array[4] != y->array[4])
2016
18.1k
  return 0;
2017
      /* Fall through.  */
2018
20.4k
    case 4:
2019
20.4k
      if (x->array[3] != y->array[3])
2020
0
  return 0;
2021
      /* Fall through.  */
2022
20.4k
    case 3:
2023
20.4k
      if (x->array[2] != y->array[2])
2024
4
  return 0;
2025
      /* Fall through.  */
2026
20.4k
    case 2:
2027
20.4k
      if (x->array[1] != y->array[1])
2028
2
  return 0;
2029
      /* Fall through.  */
2030
20.4k
    case 1:
2031
20.4k
      return x->array[0] == y->array[0];
2032
0
      break;
2033
0
    default:
2034
0
      abort ();
2035
39.4k
    }
2036
39.4k
}
2037
2038
static INLINE int
2039
cpu_flags_check_cpu64 (const insn_template *t)
2040
112k
{
2041
112k
  return flag_code == CODE_64BIT
2042
112k
   ? !t->cpu.bitfield.cpuno64
2043
112k
   : !t->cpu.bitfield.cpu64;
2044
112k
}
2045
2046
static INLINE i386_cpu_flags
2047
cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
2048
65.2k
{
2049
65.2k
  switch (ARRAY_SIZE (x.array))
2050
65.2k
    {
2051
65.2k
    case 6:
2052
65.2k
      x.array [5] &= y.array [5];
2053
      /* Fall through.  */
2054
65.2k
    case 5:
2055
65.2k
      x.array [4] &= y.array [4];
2056
      /* Fall through.  */
2057
65.2k
    case 4:
2058
65.2k
      x.array [3] &= y.array [3];
2059
      /* Fall through.  */
2060
65.2k
    case 3:
2061
65.2k
      x.array [2] &= y.array [2];
2062
      /* Fall through.  */
2063
65.2k
    case 2:
2064
65.2k
      x.array [1] &= y.array [1];
2065
      /* Fall through.  */
2066
65.2k
    case 1:
2067
65.2k
      x.array [0] &= y.array [0];
2068
65.2k
      break;
2069
0
    default:
2070
0
      abort ();
2071
65.2k
    }
2072
65.2k
  return x;
2073
65.2k
}
2074
2075
static INLINE i386_cpu_flags
2076
cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
2077
5.67k
{
2078
5.67k
  switch (ARRAY_SIZE (x.array))
2079
5.67k
    {
2080
5.67k
    case 6:
2081
5.67k
      x.array [5] |= y.array [5];
2082
      /* Fall through.  */
2083
5.67k
    case 5:
2084
5.67k
      x.array [4] |= y.array [4];
2085
      /* Fall through.  */
2086
5.67k
    case 4:
2087
5.67k
      x.array [3] |= y.array [3];
2088
      /* Fall through.  */
2089
5.67k
    case 3:
2090
5.67k
      x.array [2] |= y.array [2];
2091
      /* Fall through.  */
2092
5.67k
    case 2:
2093
5.67k
      x.array [1] |= y.array [1];
2094
      /* Fall through.  */
2095
5.67k
    case 1:
2096
5.67k
      x.array [0] |= y.array [0];
2097
5.67k
      break;
2098
0
    default:
2099
0
      abort ();
2100
5.67k
    }
2101
5.67k
  return x;
2102
5.67k
}
2103
2104
static INLINE i386_cpu_flags
2105
cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
2106
2.10k
{
2107
2.10k
  switch (ARRAY_SIZE (x.array))
2108
2.10k
    {
2109
2.10k
    case 6:
2110
2.10k
      x.array [5] &= ~y.array [5];
2111
      /* Fall through.  */
2112
2.10k
    case 5:
2113
2.10k
      x.array [4] &= ~y.array [4];
2114
      /* Fall through.  */
2115
2.10k
    case 4:
2116
2.10k
      x.array [3] &= ~y.array [3];
2117
      /* Fall through.  */
2118
2.10k
    case 3:
2119
2.10k
      x.array [2] &= ~y.array [2];
2120
      /* Fall through.  */
2121
2.10k
    case 2:
2122
2.10k
      x.array [1] &= ~y.array [1];
2123
      /* Fall through.  */
2124
2.10k
    case 1:
2125
2.10k
      x.array [0] &= ~y.array [0];
2126
2.10k
      break;
2127
0
    default:
2128
0
      abort ();
2129
2.10k
    }
2130
2.10k
  return x;
2131
2.10k
}
2132
2133
static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
2134
2135
static INLINE bool need_evex_encoding (const insn_template *t)
2136
179
{
2137
179
  return pp.encoding == encoding_evex
2138
179
  || pp.encoding == encoding_evex512
2139
179
  || pp.has_nf
2140
179
  || (t->opcode_modifier.vex && pp.encoding == encoding_egpr)
2141
179
  || i.mask.reg;
2142
179
}
2143
2144
205k
#define CPU_FLAGS_ARCH_MATCH    0x1
2145
186k
#define CPU_FLAGS_64BIT_MATCH   0x2
2146
2147
#define CPU_FLAGS_PERFECT_MATCH \
2148
112k
  (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
2149
2150
static INLINE bool set_oszc_flags (unsigned int oszc_shift)
2151
8
{
2152
8
  if (i.oszc_flags & oszc_shift)
2153
0
    {
2154
0
      as_bad (_("same oszc flag used twice"));
2155
0
      return false;
2156
0
    }
2157
8
  i.oszc_flags |= oszc_shift;
2158
8
  return true;
2159
8
}
2160
2161
/* Handle SCC OSZC flags.  */
2162
2163
static int
2164
check_Scc_OszcOperations (const char *l)
2165
36
{
2166
36
  const char *suffix_string = l;
2167
2168
141
  while (is_whitespace (*suffix_string))
2169
105
    suffix_string++;
2170
2171
  /* If {oszc flags} is absent, just return.  */
2172
36
  if (*suffix_string != '{')
2173
13
    return 0;
2174
2175
  /* Skip '{'.  */
2176
23
  suffix_string++;
2177
2178
  /* For .insn require 'scc=' as the first element.  */
2179
23
  if (dot_insn ())
2180
0
    {
2181
0
      char *copy;
2182
0
      valueT val;
2183
2184
0
      while (is_whitespace (*suffix_string))
2185
0
  suffix_string++;
2186
2187
0
      if (strncasecmp (suffix_string, "scc", 3) == 0)
2188
0
  suffix_string += 3;
2189
0
      else
2190
0
  {
2191
0
    as_bad (_("unrecognized pseudo-suffix"));
2192
0
    return -1;
2193
0
  }
2194
2195
0
      while (is_whitespace (*suffix_string))
2196
0
  suffix_string++;
2197
2198
0
      if (*suffix_string == '=')
2199
0
  suffix_string++;
2200
0
      else
2201
0
  {
2202
0
    as_bad (_("unrecognized pseudo-suffix"));
2203
0
    return -1;
2204
0
  }
2205
2206
0
      copy = xstrdup (suffix_string);
2207
      /* No need to save/restore input_line_pointer; that's done in the
2208
   caller already.  */
2209
0
      input_line_pointer = copy;
2210
0
      val = get_absolute_expression ();
2211
0
      suffix_string += input_line_pointer - copy;
2212
0
      free (copy);
2213
2214
0
      if (val > 0xf)
2215
0
  {
2216
0
    as_bad (_("scc= value must be between 0 and 15 (decimal)"));
2217
0
    return -1;
2218
0
  }
2219
2220
0
      i.scc = val;
2221
2222
      /* Permit dfv= to be absent (implying all flag values being zero).  */
2223
0
      if (*suffix_string == '}')
2224
0
  return suffix_string + 1 - l;
2225
2226
0
      if (*suffix_string != ',')
2227
0
  goto bad;
2228
0
      suffix_string++;
2229
0
    }
2230
2231
  /* Parse 'dfv='.  */
2232
23
  while (is_whitespace (*suffix_string))
2233
0
    suffix_string++;
2234
2235
23
  if (strncasecmp (suffix_string, "dfv", 3) == 0)
2236
7
    suffix_string += 3;
2237
16
  else
2238
16
    {
2239
16
      as_bad (_("unrecognized pseudo-suffix"));
2240
16
      return -1;
2241
16
    }
2242
2243
7
  while (is_whitespace (*suffix_string))
2244
0
    suffix_string++;
2245
2246
7
  if (*suffix_string == '=')
2247
7
    suffix_string++;
2248
0
  else
2249
0
    {
2250
0
      as_bad (_("unrecognized pseudo-suffix"));
2251
0
      return -1;
2252
0
    }
2253
2254
  /* Parse 'of, sf, zf, cf}'.  */
2255
11
  while (*suffix_string)
2256
11
    {
2257
14
      while (is_whitespace (*suffix_string))
2258
3
  suffix_string++;
2259
2260
      /* Return for '{dfv=}'.  */
2261
11
      if (*suffix_string == '}')
2262
0
  return suffix_string + 1 - l;
2263
2264
11
      if (strncasecmp (suffix_string, "of", 2) == 0)
2265
3
  {
2266
3
    if (!set_oszc_flags (OSZC_OF))
2267
0
      return -1;
2268
3
  }
2269
8
      else if (strncasecmp (suffix_string, "sf", 2) == 0)
2270
0
  {
2271
0
    if (!set_oszc_flags (OSZC_SF))
2272
0
      return -1;
2273
0
  }
2274
8
      else if (strncasecmp (suffix_string, "zf", 2) == 0)
2275
2
  {
2276
2
    if (!set_oszc_flags (OSZC_ZF))
2277
0
      return -1;
2278
2
  }
2279
6
      else if (strncasecmp (suffix_string, "cf", 2) == 0)
2280
3
  {
2281
3
    if (!set_oszc_flags (OSZC_CF))
2282
0
      return -1;
2283
3
  }
2284
3
      else
2285
3
  {
2286
3
    as_bad (_("unrecognized oszc flags or illegal `,' in pseudo-suffix"));
2287
3
    return -1;
2288
3
  }
2289
2290
8
      suffix_string += 2;
2291
2292
8
      while (is_whitespace (*suffix_string))
2293
0
  suffix_string++;
2294
2295
8
      if (*suffix_string == '}')
2296
1
  return ++suffix_string - l;
2297
2298
7
      if (*suffix_string != ',')
2299
3
  break;
2300
4
      suffix_string ++;
2301
4
    }
2302
2303
3
 bad:
2304
3
  as_bad (_("missing `}' or `,' in pseudo-suffix"));
2305
3
  return -1;
2306
7
}
2307
2308
/* Return CPU flags match bits. */
2309
2310
static int
2311
cpu_flags_match (const insn_template *t)
2312
112k
{
2313
112k
  i386_cpu_flags cpu, active, all = cpu_flags_from_attr (t->cpu);
2314
112k
  i386_cpu_flags any = cpu_flags_from_attr (t->cpu_any);
2315
112k
  int match = cpu_flags_check_cpu64 (t) ? CPU_FLAGS_64BIT_MATCH : 0;
2316
2317
112k
  all.bitfield.cpu64 = 0;
2318
112k
  all.bitfield.cpuno64 = 0;
2319
112k
  gas_assert (!any.bitfield.cpu64);
2320
112k
  gas_assert (!any.bitfield.cpuno64);
2321
2322
112k
  if (cpu_flags_all_zero (&all) && cpu_flags_all_zero (&any))
2323
73.2k
    {
2324
      /* This instruction is available on all archs.  */
2325
73.2k
      return match | CPU_FLAGS_ARCH_MATCH;
2326
73.2k
    }
2327
2328
  /* This instruction is available only on some archs.  */
2329
2330
  /* Dual VEX/EVEX templates may need stripping of one of the flags.  */
2331
39.3k
  if (t->opcode_modifier.vex && t->opcode_modifier.evex)
2332
130
    {
2333
      /* Dual AVX/AVX512 templates need to retain AVX512* only if we already
2334
   know that EVEX encoding will be needed.  */
2335
130
      if ((any.bitfield.cpuavx || any.bitfield.cpuavx2
2336
1
     || any.bitfield.cpufma || any.bitfield.cpuf16c)
2337
129
    && (any.bitfield.cpuavx512f || any.bitfield.cpuavx512vl))
2338
129
  {
2339
129
    if (need_evex_encoding (t)
2340
129
        || (any.bitfield.cpufma && !cpu_arch_flags.bitfield.cpufma)
2341
129
        || (any.bitfield.cpuf16c && !cpu_arch_flags.bitfield.cpuf16c))
2342
0
      {
2343
0
        any.bitfield.cpuavx = 0;
2344
0
        any.bitfield.cpuavx2 = 0;
2345
0
        any.bitfield.cpufma = 0;
2346
0
        any.bitfield.cpuf16c = 0;
2347
0
      }
2348
    /* need_evex_encoding(t) isn't reliable before operands were
2349
       parsed.  */
2350
129
    else if (i.operands)
2351
0
      {
2352
0
        any.bitfield.cpuavx512f = 0;
2353
0
        any.bitfield.cpuavx512vl = 0;
2354
0
      }
2355
129
  }
2356
2357
      /* Dual non-APX/APX templates need massaging from what APX_F() in the
2358
         opcode table has produced.  While the direct transformation of the
2359
         incoming cpuid&(cpuid|APX_F) would be to cpuid&(cpuid) / cpuid&(APX_F)
2360
         respectively, it's cheaper to move to just cpuid / cpuid&APX_F
2361
         instead.  */
2362
130
      if (any.bitfield.cpuapx_f
2363
1
    && (any.bitfield.cpubmi || any.bitfield.cpubmi2
2364
1
        || any.bitfield.cpuavx512f || any.bitfield.cpuavx512bw
2365
1
        || any.bitfield.cpuavx512dq || any.bitfield.cpuamx_tile
2366
1
        || any.bitfield.cpucmpccxadd || any.bitfield.cpuuser_msr
2367
1
        || any.bitfield.cpumsr_imm || any.bitfield.cpuamx_transpose
2368
0
        || any.bitfield.cpuamx_movrs || any.bitfield.cputile))
2369
1
  {
2370
    /* These checks (verifying that APX_F() was properly used in the
2371
       opcode table entry) make sure there's no need for an "else" to
2372
       the "if()" below.  */
2373
1
    gas_assert (!cpu_flags_all_zero (&all));
2374
2375
1
    cpu = cpu_flags_and (all, any);
2376
1
    gas_assert (cpu_flags_equal (&cpu, &all));
2377
2378
1
    if (need_evex_encoding (t))
2379
0
      all = any;
2380
2381
1
    memset (&any, 0, sizeof (any));
2382
1
  }
2383
130
    }
2384
39.2k
  else if (t->opcode_modifier.evex
2385
     /* Implicitly !t->opcode_modifier.vex.  */
2386
18.7k
     && all.bitfield.cpuapx_f
2387
0
     && (t->opcode_modifier.nf
2388
0
         || (all.bitfield.cpuadx && t->opcode_modifier.vexvvvv)))
2389
0
    {
2390
      /* APX_NDD can't be combined with other ISAs in the opcode table.
2391
   Respective entries (ADCX, ADOX, LZCNT, POPCNT, and TZCNT) use APX_F
2392
   instead, which are amended here.  No need to clear cpuapx_f, though. */
2393
0
      all.bitfield.cpuapx_ndd = true;
2394
0
    }
2395
2396
39.3k
  if (flag_code != CODE_64BIT)
2397
2.05k
    active = cpu_flags_and_not (cpu_arch_flags, cpu_64_flags);
2398
37.3k
  else
2399
37.3k
    active = cpu_arch_flags;
2400
39.3k
  cpu = cpu_flags_and (all, active);
2401
39.3k
  if (cpu_flags_equal (&cpu, &all))
2402
20.3k
    {
2403
      /* AVX and AVX2 present at the same time express an operand size
2404
   dependency - strip AVX2 for the purposes here.  The operand size
2405
   dependent check occurs in check_vecOperands().  */
2406
20.3k
      if (any.bitfield.cpuavx && any.bitfield.cpuavx2)
2407
0
  any.bitfield.cpuavx2 = 0;
2408
2409
20.3k
      cpu = cpu_flags_and (any, active);
2410
20.3k
      if (cpu_flags_all_zero (&any) || !cpu_flags_all_zero (&cpu))
2411
20.3k
  match |= CPU_FLAGS_ARCH_MATCH;
2412
20.3k
    }
2413
39.3k
  return match;
2414
39.3k
}
2415
2416
static INLINE i386_operand_type
2417
operand_type_and (i386_operand_type x, i386_operand_type y)
2418
110k
{
2419
110k
  if (x.bitfield.class != y.bitfield.class)
2420
51.1k
    x.bitfield.class = ClassNone;
2421
110k
  if (x.bitfield.instance != y.bitfield.instance)
2422
10.3k
    x.bitfield.instance = InstanceNone;
2423
2424
110k
  switch (ARRAY_SIZE (x.array))
2425
110k
    {
2426
0
    case 3:
2427
0
      x.array [2] &= y.array [2];
2428
      /* Fall through.  */
2429
0
    case 2:
2430
0
      x.array [1] &= y.array [1];
2431
      /* Fall through.  */
2432
110k
    case 1:
2433
110k
      x.array [0] &= y.array [0];
2434
110k
      break;
2435
0
    default:
2436
0
      abort ();
2437
110k
    }
2438
110k
  return x;
2439
110k
}
2440
2441
static INLINE i386_operand_type
2442
operand_type_and_not (i386_operand_type x, i386_operand_type y)
2443
10.7k
{
2444
10.7k
  gas_assert (y.bitfield.class == ClassNone);
2445
10.7k
  gas_assert (y.bitfield.instance == InstanceNone);
2446
2447
10.7k
  switch (ARRAY_SIZE (x.array))
2448
10.7k
    {
2449
0
    case 3:
2450
0
      x.array [2] &= ~y.array [2];
2451
      /* Fall through.  */
2452
0
    case 2:
2453
0
      x.array [1] &= ~y.array [1];
2454
      /* Fall through.  */
2455
10.7k
    case 1:
2456
10.7k
      x.array [0] &= ~y.array [0];
2457
10.7k
      break;
2458
0
    default:
2459
0
      abort ();
2460
10.7k
    }
2461
10.7k
  return x;
2462
10.7k
}
2463
2464
static INLINE i386_operand_type
2465
operand_type_or (i386_operand_type x, i386_operand_type y)
2466
43.0k
{
2467
43.0k
  gas_assert (x.bitfield.class == ClassNone ||
2468
43.0k
              y.bitfield.class == ClassNone ||
2469
43.0k
              x.bitfield.class == y.bitfield.class);
2470
43.0k
  gas_assert (x.bitfield.instance == InstanceNone ||
2471
43.0k
              y.bitfield.instance == InstanceNone ||
2472
43.0k
              x.bitfield.instance == y.bitfield.instance);
2473
2474
43.0k
  switch (ARRAY_SIZE (x.array))
2475
43.0k
    {
2476
0
    case 3:
2477
0
      x.array [2] |= y.array [2];
2478
      /* Fall through.  */
2479
0
    case 2:
2480
0
      x.array [1] |= y.array [1];
2481
      /* Fall through.  */
2482
43.0k
    case 1:
2483
43.0k
      x.array [0] |= y.array [0];
2484
43.0k
      break;
2485
0
    default:
2486
0
      abort ();
2487
43.0k
    }
2488
43.0k
  return x;
2489
43.0k
}
2490
2491
static INLINE i386_operand_type
2492
operand_type_xor (i386_operand_type x, i386_operand_type y)
2493
0
{
2494
0
  gas_assert (y.bitfield.class == ClassNone);
2495
0
  gas_assert (y.bitfield.instance == InstanceNone);
2496
2497
0
  switch (ARRAY_SIZE (x.array))
2498
0
    {
2499
0
    case 3:
2500
0
      x.array [2] ^= y.array [2];
2501
      /* Fall through.  */
2502
0
    case 2:
2503
0
      x.array [1] ^= y.array [1];
2504
      /* Fall through.  */
2505
0
    case 1:
2506
0
      x.array [0] ^= y.array [0];
2507
0
      break;
2508
0
    default:
2509
0
      abort ();
2510
0
    }
2511
0
  return x;
2512
0
}
2513
2514
static const i386_operand_type anydisp = {
2515
  .bitfield = { .disp8 = 1, .disp16 = 1, .disp32 = 1, .disp64 = 1 }
2516
};
2517
2518
enum operand_type
2519
{
2520
  reg,
2521
  imm,
2522
  disp,
2523
  anymem
2524
};
2525
2526
static INLINE int
2527
operand_type_check (i386_operand_type t, enum operand_type c)
2528
132k
{
2529
132k
  switch (c)
2530
132k
    {
2531
0
    case reg:
2532
0
      return t.bitfield.class == Reg;
2533
2534
31.5k
    case imm:
2535
31.5k
      return (t.bitfield.imm8
2536
23.0k
        || t.bitfield.imm8s
2537
22.8k
        || t.bitfield.imm16
2538
22.8k
        || t.bitfield.imm32
2539
22.8k
        || t.bitfield.imm32s
2540
20.6k
        || t.bitfield.imm64);
2541
2542
92.0k
    case disp:
2543
92.0k
      return (t.bitfield.disp8
2544
81.5k
        || t.bitfield.disp16
2545
80.9k
        || t.bitfield.disp32
2546
70.4k
        || t.bitfield.disp64);
2547
2548
8.75k
    case anymem:
2549
8.75k
      return (t.bitfield.disp8
2550
3.16k
        || t.bitfield.disp16
2551
3.14k
        || t.bitfield.disp32
2552
3.14k
        || t.bitfield.disp64
2553
3.08k
        || t.bitfield.baseindex);
2554
2555
0
    default:
2556
0
      abort ();
2557
132k
    }
2558
2559
0
  return 0;
2560
132k
}
2561
2562
static INLINE const i386_operand_type *
2563
get_operand_types (const insn_template *t)
2564
115k
{
2565
115k
  return &i386_operand_types[t->operand_ref];
2566
115k
}
2567
2568
/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit size
2569
   between operand GIVEN and operand WANTED for instruction template T.  */
2570
2571
static INLINE int
2572
match_operand_size (const insn_template *t, unsigned int wanted,
2573
        unsigned int given)
2574
11.0k
{
2575
11.0k
  const i386_operand_type *t_types = get_operand_types (t);
2576
2577
11.0k
  return !((i.types[given].bitfield.byte
2578
131
      && !t_types[wanted].bitfield.byte)
2579
10.9k
     || (i.types[given].bitfield.word
2580
2.28k
         && !t_types[wanted].bitfield.word)
2581
10.2k
     || (i.types[given].bitfield.dword
2582
2.71k
         && !t_types[wanted].bitfield.dword)
2583
9.50k
     || (i.types[given].bitfield.qword
2584
218
         && (!t_types[wanted].bitfield.qword
2585
       /* Don't allow 64-bit (memory) operands outside of 64-bit
2586
          mode, when they're used where a 64-bit GPR could also
2587
          be used.  Checking is needed for Intel Syntax only.  */
2588
180
       || (intel_syntax
2589
180
           && flag_code != CODE_64BIT
2590
0
           && (t_types[wanted].bitfield.class == Reg
2591
0
         || t->opcode_modifier.isstring)))));
2592
11.0k
}
2593
2594
/* Return 1 if there is no conflict in 80bit size
2595
   between operand GIVEN and operand WANTED for instruction template T.  */
2596
2597
static INLINE int
2598
match_fp_size (const i386_operand_type *t_types, unsigned int wanted,
2599
        unsigned int given)
2600
2
{
2601
2
  return !i.types[given].bitfield.tbyte
2602
2
   || t_types[wanted].bitfield.tbyte;
2603
2
}
2604
2605
/* Return 1 if there is no conflict in SIMD register between operand
2606
   GIVEN and operand WANTED for instruction template T.  */
2607
2608
static INLINE int
2609
match_simd_size (const i386_operand_type *t_types, unsigned int wanted,
2610
     unsigned int given)
2611
6.03k
{
2612
6.03k
  return !((i.types[given].bitfield.xmmword
2613
450
      && !t_types[wanted].bitfield.xmmword)
2614
6.03k
     || (i.types[given].bitfield.ymmword
2615
2
         && !t_types[wanted].bitfield.ymmword)
2616
6.03k
     || (i.types[given].bitfield.zmmword
2617
0
         && !t_types[wanted].bitfield.zmmword)
2618
6.03k
     || (i.types[given].bitfield.tmmword
2619
0
         && !t_types[wanted].bitfield.tmmword)
2620
     /* RegSIMD|Byte marks %bsr0; it is not the operand size.  */
2621
6.03k
     || (i.types[given].bitfield.byte
2622
0
         && !t_types[wanted].bitfield.byte));
2623
6.03k
}
2624
2625
/* Return 1 if there is no conflict in any size between operand GIVEN
2626
   and operand WANTED for instruction template T.  */
2627
2628
static INLINE int
2629
match_mem_size (const insn_template *t, unsigned int wanted,
2630
    unsigned int given)
2631
5.66k
{
2632
5.66k
  const i386_operand_type *t_types = get_operand_types (t);
2633
2634
5.66k
  return (match_operand_size (t, wanted, given)
2635
5.66k
    && (!i.types[given].bitfield.tbyte
2636
0
        || t_types[wanted].bitfield.tbyte)
2637
5.66k
    && !((i.types[given].bitfield.unspecified
2638
5.66k
    && !i.broadcast.type
2639
5.63k
    && !i.broadcast.bytes
2640
5.63k
    && !t_types[wanted].bitfield.unspecified)
2641
5.63k
         || (i.types[given].bitfield.fword
2642
0
       && !t_types[wanted].bitfield.fword)
2643
         /* For scalar opcode templates to allow register and memory
2644
      operands at the same time, some special casing is needed
2645
      here.  Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2646
      down-conversion vpmov*.  */
2647
5.63k
         || ((t_types[wanted].bitfield.class == RegSIMD
2648
48
        && t_types[wanted].bitfield.byte
2649
48
           + t_types[wanted].bitfield.word
2650
48
           + t_types[wanted].bitfield.dword
2651
48
           + t_types[wanted].bitfield.qword
2652
48
           > !!t->opcode_modifier.broadcast)
2653
5.63k
       ? (i.types[given].bitfield.xmmword
2654
48
          || i.types[given].bitfield.ymmword
2655
48
          || i.types[given].bitfield.zmmword)
2656
5.63k
       : !match_simd_size(t_types, wanted, given))));
2657
5.66k
}
2658
2659
/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2660
   operands for instruction template T, and it has MATCH_REVERSE set if there
2661
   is no size conflict on any operands for the template with operands reversed
2662
   (and the template allows for reversing in the first place).  */
2663
2664
36.5k
#define MATCH_STRAIGHT 1
2665
8.13k
#define MATCH_REVERSE  2
2666
2667
static INLINE unsigned int
2668
operand_size_match (const insn_template *t)
2669
24.5k
{
2670
24.5k
  const i386_operand_type *t_types = get_operand_types (t);
2671
24.5k
  unsigned int j, match = MATCH_STRAIGHT;
2672
2673
  /* Don't check non-absolute jump instructions.  */
2674
24.5k
  if (t->opcode_modifier.jump
2675
206
      && t->opcode_modifier.jump != JUMP_ABSOLUTE)
2676
180
    return match;
2677
2678
44.6k
  for (j = 0; j < i.imm_operands; j++)
2679
    /* Instruction templates with only sign-extended 8-bit immediate
2680
       operand also have a second template with full-operand-size
2681
       immediate operand under a different opcode.  Don't match the
2682
       first template if sign-extended 8-bit immediate operand should
2683
       be excluded.  */
2684
20.2k
    if (pp.no_imm8s
2685
0
        && !t_types[j].bitfield.imm8
2686
0
        && t_types[j].bitfield.imm8s)
2687
0
      {
2688
0
  gas_assert (!t->opcode_modifier.d);
2689
0
  return 0;
2690
0
      }
2691
2692
  /* Check memory and accumulator operand size.  */
2693
39.4k
  for (; j < i.operands; j++)
2694
16.1k
    {
2695
16.1k
      if (i.types[j].bitfield.class == Reg
2696
6.94k
    && (t_types[j].bitfield.class == Reg
2697
3.59k
        || (t_types[j].bitfield.instance == Accum
2698
309
      && (t_types[j].bitfield.byte
2699
0
          || t_types[j].bitfield.word
2700
0
          || t_types[j].bitfield.dword
2701
0
          || t_types[j].bitfield.qword)))
2702
3.65k
    && !match_operand_size (t, j, j))
2703
1.01k
  {
2704
1.01k
    match = 0;
2705
1.01k
    break;
2706
1.01k
  }
2707
2708
15.1k
      if (i.types[j].bitfield.class == RegFP
2709
3
    && (t_types[j].bitfield.class == RegFP
2710
1
        || (t_types[j].bitfield.instance == Accum
2711
0
      && t_types[j].bitfield.tbyte))
2712
2
    && !match_fp_size (t_types, j, j))
2713
0
  {
2714
0
    match = 0;
2715
0
    break;
2716
0
  }
2717
2718
15.1k
      if (i.types[j].bitfield.class == RegSIMD
2719
268
    && (t_types[j].bitfield.class == RegSIMD
2720
28
        || (t_types[j].bitfield.instance == Accum
2721
      /* Note: %ymm0, %zmm0, and %tmm0 aren't marked Accum.  */
2722
0
      && t_types[j].bitfield.xmmword))
2723
240
    && !match_simd_size (t_types, j, j))
2724
0
  {
2725
0
    match = 0;
2726
0
    break;
2727
0
  }
2728
2729
15.1k
      if ((i.flags[j] & Operand_Mem)
2730
8.18k
    && operand_type_check (t_types[j], anymem)
2731
5.46k
    && t->opcode_modifier.operandconstraint != ANY_SIZE
2732
5.46k
    && !match_mem_size (t, j, j))
2733
0
  {
2734
0
    match = 0;
2735
0
    break;
2736
0
  }
2737
15.1k
    }
2738
2739
24.3k
  if (!t->opcode_modifier.d)
2740
19.5k
    return match;
2741
2742
  /* Check reverse.  */
2743
4.80k
  gas_assert (i.operands >= 2);
2744
2745
10.2k
  for (j = i.imm_operands; j < i.operands; j++)
2746
6.04k
    {
2747
6.04k
      unsigned int given = i.operands - j - 1;
2748
2749
      /* For FMA4 and XOP insns VEX.W controls just the first two register
2750
   operands.  And APX_F / APX_NDD insns just swap the two source operands,
2751
   with the 3rd one being the destination.  */
2752
6.04k
      if (is_cpu (t, CpuFMA4) || is_cpu (t, CpuXOP)
2753
6.04k
    || is_cpu (t, CpuAPX_F)|| is_cpu (t, CpuAPX_NDD))
2754
8
  given = j < 2 ? 1 - j : j;
2755
2756
6.04k
      if (i.types[given].bitfield.class == Reg
2757
3.07k
    && (t_types[j].bitfield.class == Reg
2758
1.69k
        || (t_types[j].bitfield.instance == Accum
2759
303
      && (t_types[j].bitfield.byte
2760
0
          || t_types[j].bitfield.word
2761
0
          || t_types[j].bitfield.dword
2762
0
          || t_types[j].bitfield.qword
2763
0
          || t_types[j].bitfield.tbyte)))
2764
1.68k
    && !match_operand_size (t, j, given))
2765
538
  return match;
2766
2767
5.50k
      if (i.types[given].bitfield.class == RegFP
2768
0
    && (t_types[j].bitfield.class == RegFP
2769
0
        || (t_types[j].bitfield.instance == Accum
2770
0
      && t_types[j].bitfield.tbyte))
2771
0
    && !match_fp_size (t_types, j, given))
2772
0
  return match;
2773
2774
      /* No need to check for Accum here: There are no such templates with D
2775
   set.  */
2776
5.50k
      if (i.types[given].bitfield.class == RegSIMD
2777
212
    && t_types[j].bitfield.class == RegSIMD
2778
212
    && !match_simd_size (t_types, j, given))
2779
0
  return match;
2780
2781
5.50k
      if ((i.flags[given] & Operand_Mem)
2782
567
    && operand_type_check (t_types[j], anymem)
2783
202
    && !match_mem_size (t, j, given))
2784
36
  return match;
2785
5.50k
    }
2786
2787
4.22k
  return match | MATCH_REVERSE;
2788
4.80k
}
2789
2790
static INLINE int
2791
operand_type_match (i386_operand_type overlap,
2792
        i386_operand_type given)
2793
33.5k
{
2794
33.5k
  i386_operand_type temp = overlap;
2795
2796
33.5k
  temp.bitfield.unspecified = 0;
2797
33.5k
  temp.bitfield.byte = 0;
2798
33.5k
  temp.bitfield.word = 0;
2799
33.5k
  temp.bitfield.dword = 0;
2800
33.5k
  temp.bitfield.fword = 0;
2801
33.5k
  temp.bitfield.qword = 0;
2802
33.5k
  temp.bitfield.tbyte = 0;
2803
33.5k
  temp.bitfield.xmmword = 0;
2804
33.5k
  temp.bitfield.ymmword = 0;
2805
33.5k
  temp.bitfield.zmmword = 0;
2806
33.5k
  temp.bitfield.tmmword = 0;
2807
33.5k
  if (operand_type_all_zero (&temp))
2808
20.3k
    goto mismatch;
2809
2810
  /* When a (register) instance is expected, operand size needs checking
2811
     to disambiguate.  */
2812
13.1k
  if (overlap.bitfield.instance != InstanceNone
2813
266
      && !overlap.bitfield.byte
2814
266
      && !overlap.bitfield.word
2815
266
      && !overlap.bitfield.dword
2816
16
      && !overlap.bitfield.qword
2817
0
      && !overlap.bitfield.tbyte
2818
0
      && !overlap.bitfield.xmmword
2819
0
      && !overlap.bitfield.ymmword
2820
0
      && !overlap.bitfield.zmmword
2821
0
      && !overlap.bitfield.tmmword)
2822
0
    {
2823
0
      gas_assert (overlap.bitfield.class == ClassNone);
2824
0
      goto mismatch;
2825
0
    }
2826
2827
13.1k
  if (given.bitfield.baseindex == overlap.bitfield.baseindex)
2828
13.1k
    return 1;
2829
2830
20.3k
 mismatch:
2831
20.3k
  i.error = operand_type_mismatch;
2832
20.3k
  return 0;
2833
13.1k
}
2834
2835
/* If given types g0 and g1 are registers they must be of the same type
2836
   unless the expected operand type register overlap is null.
2837
   Intel syntax sized memory operands are also checked here.  */
2838
2839
static INLINE int
2840
operand_type_register_match (i386_operand_type g0,
2841
           i386_operand_type t0,
2842
           i386_operand_type g1,
2843
           i386_operand_type t1)
2844
928
{
2845
928
  if (g0.bitfield.class != Reg
2846
139
      && g0.bitfield.class != RegSIMD
2847
33
      && (g0.bitfield.unspecified
2848
0
    || !operand_type_check (g0, anymem)))
2849
33
    return 1;
2850
2851
895
  if (g1.bitfield.class != Reg
2852
370
      && g1.bitfield.class != RegSIMD
2853
264
      && (g1.bitfield.unspecified
2854
0
    || !operand_type_check (g1, anymem)))
2855
264
    return 1;
2856
2857
631
  if (g0.bitfield.byte == g1.bitfield.byte
2858
631
      && g0.bitfield.word == g1.bitfield.word
2859
131
      && g0.bitfield.dword == g1.bitfield.dword
2860
131
      && g0.bitfield.qword == g1.bitfield.qword
2861
131
      && g0.bitfield.xmmword == g1.bitfield.xmmword
2862
129
      && g0.bitfield.ymmword == g1.bitfield.ymmword
2863
129
      && g0.bitfield.zmmword == g1.bitfield.zmmword)
2864
129
    return 1;
2865
2866
  /* If expectations overlap in no more than a single size, all is fine. */
2867
502
  g0 = operand_type_and (t0, t1);
2868
502
  if (g0.bitfield.byte
2869
502
      + g0.bitfield.word
2870
502
      + g0.bitfield.dword
2871
502
      + g0.bitfield.qword
2872
502
      + g0.bitfield.xmmword
2873
502
      + g0.bitfield.ymmword
2874
502
      + g0.bitfield.zmmword <= 1)
2875
0
    return 1;
2876
2877
502
  i.error = register_type_mismatch;
2878
2879
502
  return 0;
2880
502
}
2881
2882
static INLINE unsigned int
2883
register_number (const reg_entry *r)
2884
13
{
2885
13
  unsigned int nr = r->reg_num;
2886
2887
13
  if (r->reg_flags & RegRex)
2888
0
    nr += 8;
2889
2890
13
  if (r->reg_flags & (RegVRex | RegRex2))
2891
0
    nr += 16;
2892
2893
13
  return nr;
2894
13
}
2895
2896
static INLINE unsigned int
2897
mode_from_disp_size (i386_operand_type t)
2898
3
{
2899
3
  if (t.bitfield.disp8)
2900
1
    return 1;
2901
2
  else if (t.bitfield.disp16
2902
2
     || t.bitfield.disp32)
2903
0
    return 2;
2904
2
  else
2905
2
    return 0;
2906
3
}
2907
2908
static INLINE int
2909
fits_in_signed_byte (addressT num)
2910
3.51k
{
2911
3.51k
  return num + 0x80 <= 0xff;
2912
3.51k
}
2913
2914
static INLINE int
2915
fits_in_unsigned_byte (addressT num)
2916
3.49k
{
2917
3.49k
  return num <= 0xff;
2918
3.49k
}
2919
2920
static INLINE int
2921
fits_in_unsigned_word (addressT num)
2922
3.51k
{
2923
3.51k
  return num <= 0xffff;
2924
3.51k
}
2925
2926
static INLINE int
2927
fits_in_signed_word (addressT num)
2928
1.35k
{
2929
1.35k
  return num + 0x8000 <= 0xffff;
2930
1.35k
}
2931
2932
static INLINE int
2933
fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
2934
932
{
2935
#ifndef BFD64
2936
  return 1;
2937
#else
2938
932
  return num + 0x80000000 <= 0xffffffff;
2939
932
#endif
2940
932
}        /* fits_in_signed_long() */
2941
2942
static INLINE int
2943
fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
2944
52.9k
{
2945
#ifndef BFD64
2946
  return 1;
2947
#else
2948
52.9k
  return num <= 0xffffffff;
2949
52.9k
#endif
2950
52.9k
}        /* fits_in_unsigned_long() */
2951
2952
static INLINE valueT extend_to_32bit_address (addressT num)
2953
0
{
2954
0
#ifdef BFD64
2955
0
  if (fits_in_unsigned_long(num))
2956
0
    return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2957
2958
0
  if (!fits_in_signed_long (num))
2959
0
    return num & 0xffffffff;
2960
0
#endif
2961
2962
0
  return num;
2963
0
}
2964
2965
static INLINE int
2966
fits_in_disp8 (offsetT num)
2967
28
{
2968
28
  int shift = i.memshift;
2969
28
  unsigned int mask;
2970
2971
28
  if (shift == -1)
2972
0
    abort ();
2973
2974
28
  mask = (1 << shift) - 1;
2975
2976
  /* Return 0 if NUM isn't properly aligned.  */
2977
28
  if ((num & mask))
2978
0
    return 0;
2979
2980
  /* Check if NUM will fit in 8bit after shift.  */
2981
28
  return fits_in_signed_byte (num >> shift);
2982
28
}
2983
2984
static INLINE int
2985
fits_in_imm4 (offsetT num)
2986
2
{
2987
  /* Despite the name, check for imm3 if we're dealing with EVEX.  */
2988
2
  return (num & (pp.encoding != encoding_evex
2989
2
     && pp.encoding != encoding_egpr ? 0xf : 7)) == num;
2990
2
}
2991
2992
static i386_operand_type
2993
smallest_imm_type (offsetT num)
2994
4.14k
{
2995
4.14k
  i386_operand_type t;
2996
2997
4.14k
  operand_type_set (&t, 0);
2998
4.14k
  t.bitfield.imm64 = 1;
2999
3000
4.14k
  if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
3001
650
    {
3002
      /* This code is disabled on the 486 because all the Imm1 forms
3003
   in the opcode table are slower on the i486.  They're the
3004
   versions with the implicitly specified single-position
3005
   displacement, which has another syntax if you really want to
3006
   use that form.  */
3007
650
      t.bitfield.imm1 = 1;
3008
650
      t.bitfield.imm8 = 1;
3009
650
      t.bitfield.imm8s = 1;
3010
650
      t.bitfield.imm16 = 1;
3011
650
      t.bitfield.imm32 = 1;
3012
650
      t.bitfield.imm32s = 1;
3013
650
    }
3014
3.49k
  else if (fits_in_signed_byte (num))
3015
2.13k
    {
3016
2.13k
      if (fits_in_unsigned_byte (num))
3017
2.00k
  t.bitfield.imm8 = 1;
3018
2.13k
      t.bitfield.imm8s = 1;
3019
2.13k
      t.bitfield.imm16 = 1;
3020
2.13k
      if (flag_code != CODE_64BIT || fits_in_unsigned_long (num))
3021
2.00k
  t.bitfield.imm32 = 1;
3022
2.13k
      t.bitfield.imm32s = 1;
3023
2.13k
    }
3024
1.35k
  else if (fits_in_unsigned_byte (num))
3025
0
    {
3026
0
      t.bitfield.imm8 = 1;
3027
0
      t.bitfield.imm16 = 1;
3028
0
      t.bitfield.imm32 = 1;
3029
0
      t.bitfield.imm32s = 1;
3030
0
    }
3031
1.35k
  else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
3032
450
    {
3033
450
      t.bitfield.imm16 = 1;
3034
450
      if (flag_code != CODE_64BIT || fits_in_unsigned_long (num))
3035
448
  t.bitfield.imm32 = 1;
3036
450
      t.bitfield.imm32s = 1;
3037
450
    }
3038
908
  else if (fits_in_signed_long (num))
3039
455
    {
3040
455
      if (flag_code != CODE_64BIT || fits_in_unsigned_long (num))
3041
447
  t.bitfield.imm32 = 1;
3042
455
      t.bitfield.imm32s = 1;
3043
455
    }
3044
453
  else if (fits_in_unsigned_long (num))
3045
0
    t.bitfield.imm32 = 1;
3046
3047
4.14k
  return t;
3048
4.14k
}
3049
3050
static offsetT
3051
offset_in_range (offsetT val, int size)
3052
1.46k
{
3053
1.46k
  addressT mask;
3054
3055
1.46k
  switch (size)
3056
1.46k
    {
3057
1.44k
    case 1: mask = ((addressT) 1 <<  8) - 1; break;
3058
10
    case 2: mask = ((addressT) 1 << 16) - 1; break;
3059
0
#ifdef BFD64
3060
17
    case 4: mask = ((addressT) 1 << 32) - 1; break;
3061
0
#endif
3062
0
    case sizeof (val): return val;
3063
0
    default: abort ();
3064
1.46k
    }
3065
3066
1.46k
  if ((val & ~mask) != 0 && (-(addressT) val & ~mask) != 0)
3067
7
    as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
3068
7
       (uint64_t) val, (uint64_t) (val & mask));
3069
3070
1.46k
  return val & mask;
3071
1.46k
}
3072
3073
static INLINE const char *insn_name (const insn_template *t)
3074
991k
{
3075
991k
  return &i386_mnemonics[t->mnem_off];
3076
991k
}
3077
3078
enum PREFIX_GROUP
3079
{
3080
  PREFIX_EXIST = 0,
3081
  PREFIX_LOCK,
3082
  PREFIX_REP,
3083
  PREFIX_DS,
3084
  PREFIX_OTHER
3085
};
3086
3087
/* Returns
3088
   a. PREFIX_EXIST if attempting to add a prefix where one from the
3089
   same class already exists.
3090
   b. PREFIX_LOCK if lock prefix is added.
3091
   c. PREFIX_REP if rep/repne prefix is added.
3092
   d. PREFIX_DS if ds prefix is added.
3093
   e. PREFIX_OTHER if other prefix is added.
3094
 */
3095
3096
static enum PREFIX_GROUP
3097
add_prefix (unsigned int prefix)
3098
1.74k
{
3099
1.74k
  enum PREFIX_GROUP ret = PREFIX_OTHER;
3100
1.74k
  unsigned int q;
3101
3102
1.74k
  if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
3103
54
      && flag_code == CODE_64BIT)
3104
54
    {
3105
54
      if ((i.prefix[REX_PREFIX] & prefix & REX_W)
3106
54
    || (i.prefix[REX_PREFIX] & prefix & REX_R)
3107
54
    || (i.prefix[REX_PREFIX] & prefix & REX_X)
3108
54
    || (i.prefix[REX_PREFIX] & prefix & REX_B))
3109
0
  ret = PREFIX_EXIST;
3110
54
      q = REX_PREFIX;
3111
54
    }
3112
1.68k
  else
3113
1.68k
    {
3114
1.68k
      switch (prefix)
3115
1.68k
  {
3116
0
  default:
3117
0
    abort ();
3118
3119
2
  case DS_PREFIX_OPCODE:
3120
2
    ret = PREFIX_DS;
3121
    /* Fall through.  */
3122
79
  case CS_PREFIX_OPCODE:
3123
79
  case ES_PREFIX_OPCODE:
3124
80
  case FS_PREFIX_OPCODE:
3125
80
  case GS_PREFIX_OPCODE:
3126
82
  case SS_PREFIX_OPCODE:
3127
82
    q = SEG_PREFIX;
3128
82
    break;
3129
3130
0
  case REPNE_PREFIX_OPCODE:
3131
516
  case REPE_PREFIX_OPCODE:
3132
516
    q = REP_PREFIX;
3133
516
    ret = PREFIX_REP;
3134
516
    break;
3135
3136
254
  case LOCK_PREFIX_OPCODE:
3137
254
    q = LOCK_PREFIX;
3138
254
    ret = PREFIX_LOCK;
3139
254
    break;
3140
3141
0
  case FWAIT_OPCODE:
3142
0
    q = WAIT_PREFIX;
3143
0
    break;
3144
3145
0
  case ADDR_PREFIX_OPCODE:
3146
0
    q = ADDR_PREFIX;
3147
0
    break;
3148
3149
836
  case DATA_PREFIX_OPCODE:
3150
836
    q = DATA_PREFIX;
3151
836
    break;
3152
1.68k
  }
3153
1.68k
      if (i.prefix[q] != 0)
3154
1
  ret = PREFIX_EXIST;
3155
1.68k
    }
3156
3157
1.74k
  if (ret)
3158
1.74k
    {
3159
1.74k
      if (!i.prefix[q])
3160
1.74k
  ++i.prefixes;
3161
1.74k
      i.prefix[q] |= prefix;
3162
1.74k
    }
3163
1
  else
3164
1
    as_bad (_("same type of prefix used twice"));
3165
3166
1.74k
  return ret;
3167
1.74k
}
3168
3169
static void
3170
update_code_flag (int value, int check)
3171
409
{
3172
409
  PRINTF_LIKE ((*as_error)) = check ? as_fatal : as_bad;
3173
3174
409
  if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpu64 )
3175
0
    {
3176
0
      as_error (_("64bit mode not supported on `%s'."),
3177
0
    cpu_arch_name ? cpu_arch_name : default_arch);
3178
0
      return;
3179
0
    }
3180
3181
409
  if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3182
0
    {
3183
0
      as_error (_("32bit mode not supported on `%s'."),
3184
0
    cpu_arch_name ? cpu_arch_name : default_arch);
3185
0
      return;
3186
0
    }
3187
3188
409
  flag_code = (enum flag_code) value;
3189
3190
409
  stackop_size = '\0';
3191
409
}
3192
3193
static void
3194
set_code_flag (int value)
3195
41
{
3196
41
  update_code_flag (value, 0);
3197
41
}
3198
3199
static void
3200
set_16bit_gcc_code_flag (int new_code_flag)
3201
1
{
3202
1
  flag_code = (enum flag_code) new_code_flag;
3203
1
  if (flag_code != CODE_16BIT)
3204
0
    abort ();
3205
1
  stackop_size = LONG_MNEM_SUFFIX;
3206
1
}
3207
3208
static void
3209
_set_intel_syntax (int syntax_flag)
3210
40
{
3211
40
  intel_syntax = syntax_flag;
3212
3213
40
  expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
3214
3215
40
  register_prefix = allow_naked_reg ? "" : "%";
3216
40
}
3217
3218
static void
3219
set_intel_syntax (int syntax_flag)
3220
40
{
3221
  /* Find out if register prefixing is specified.  */
3222
40
  int ask_naked_reg = 0;
3223
3224
40
  SKIP_WHITESPACE ();
3225
40
  if (!is_end_of_stmt (*input_line_pointer))
3226
40
    {
3227
40
      char *string;
3228
40
      int e = get_symbol_name (&string);
3229
3230
40
      if (strcmp (string, "prefix") == 0)
3231
1
  ask_naked_reg = 1;
3232
39
      else if (strcmp (string, "noprefix") == 0)
3233
7
  ask_naked_reg = -1;
3234
32
      else
3235
32
  as_bad (_("bad argument to syntax directive."));
3236
40
      (void) restore_line_pointer (e);
3237
40
    }
3238
40
  demand_empty_rest_of_line ();
3239
3240
40
  if (ask_naked_reg == 0)
3241
32
    allow_naked_reg = (syntax_flag
3242
32
           && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
3243
8
  else
3244
8
    allow_naked_reg = (ask_naked_reg < 0);
3245
3246
40
  _set_intel_syntax (syntax_flag);
3247
40
}
3248
3249
static void
3250
set_intel_mnemonic (int mnemonic_flag)
3251
0
{
3252
0
  intel_mnemonic = mnemonic_flag;
3253
0
}
3254
3255
static void
3256
set_allow_index_reg (int flag)
3257
17
{
3258
17
  allow_index_reg = flag;
3259
17
}
3260
3261
static void
3262
set_check (int what)
3263
0
{
3264
0
  enum check_kind *kind;
3265
0
  const char *str;
3266
3267
0
  if (what)
3268
0
    {
3269
0
      kind = &operand_check;
3270
0
      str = "operand";
3271
0
    }
3272
0
  else
3273
0
    {
3274
0
      kind = &sse_check;
3275
0
      str = "sse";
3276
0
    }
3277
3278
0
  SKIP_WHITESPACE ();
3279
3280
0
  if (!is_end_of_stmt (*input_line_pointer))
3281
0
    {
3282
0
      char *string;
3283
0
      int e = get_symbol_name (&string);
3284
3285
0
      if (strcmp (string, "none") == 0)
3286
0
  *kind = check_none;
3287
0
      else if (strcmp (string, "warning") == 0)
3288
0
  *kind = check_warning;
3289
0
      else if (strcmp (string, "error") == 0)
3290
0
  *kind = check_error;
3291
0
      else
3292
0
  as_bad (_("bad argument to %s_check directive."), str);
3293
0
      (void) restore_line_pointer (e);
3294
0
    }
3295
0
  else
3296
0
    as_bad (_("missing argument for %s_check directive"), str);
3297
3298
0
  demand_empty_rest_of_line ();
3299
0
}
3300
3301
static void
3302
check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
3303
         i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
3304
8
{
3305
  /* Intel MCU is only supported on ELF.  */
3306
8
#ifdef OBJ_ELF
3307
8
  static const char *arch;
3308
3309
8
  if (!arch)
3310
1
    {
3311
      /* Use cpu_arch_name if it is set in md_parse_option.  Otherwise
3312
   use default_arch.  */
3313
1
      arch = cpu_arch_name;
3314
1
      if (!arch)
3315
1
  arch = default_arch;
3316
1
    }
3317
3318
  /* If we are targeting Intel MCU, we must enable it.  */
3319
8
  if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
3320
8
      == new_flag.bitfield.cpuiamcu)
3321
8
    return;
3322
3323
0
  as_bad (_("`%s' is not supported on `%s'"), name, arch);
3324
0
#endif
3325
0
}
3326
3327
static void
3328
extend_cpu_sub_arch_name (const char *pfx, const char *name)
3329
19
{
3330
19
  if (cpu_sub_arch_name)
3331
17
    cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
3332
17
          pfx, name, (const char *) NULL);
3333
2
  else
3334
2
    cpu_sub_arch_name = concat (pfx, name, (const char *) NULL);
3335
19
}
3336
3337
static void isa_enable (unsigned int idx)
3338
70
{
3339
70
  i386_cpu_flags flags = cpu_flags_or (cpu_arch_flags, cpu_arch[idx].enable);
3340
3341
70
  if (!cpu_flags_equal (&flags, &cpu_arch_flags))
3342
10
    {
3343
10
      extend_cpu_sub_arch_name (".", cpu_arch[idx].name);
3344
10
      cpu_arch_flags = flags;
3345
10
    }
3346
3347
70
  cpu_arch_isa_flags = cpu_flags_or (cpu_arch_isa_flags, cpu_arch[idx].enable);
3348
70
}
3349
3350
static void isa_disable (unsigned int idx)
3351
24
{
3352
24
  i386_cpu_flags flags
3353
24
    = cpu_flags_and_not (cpu_arch_flags, cpu_arch[idx].disable);
3354
3355
24
  if (!cpu_flags_equal (&flags, &cpu_arch_flags))
3356
9
    {
3357
9
      extend_cpu_sub_arch_name (".no", cpu_arch[idx].name);
3358
9
      cpu_arch_flags = flags;
3359
9
    }
3360
3361
24
  cpu_arch_isa_flags
3362
24
    = cpu_flags_and_not (cpu_arch_isa_flags, cpu_arch[idx].disable);
3363
3364
  /* TILE is internal. .noamx_tile and .noace_v1 do not automatically clear
3365
     it.  */
3366
24
  if (!cpu_arch_flags.bitfield.cpuamx_tile
3367
15
      && !cpu_arch_flags.bitfield.cpuace_v1)
3368
15
    {
3369
15
      cpu_arch_flags.bitfield.cputile = 0;
3370
15
      cpu_arch_isa_flags.bitfield.cputile = 0;
3371
15
    }
3372
24
}
3373
3374
static void
3375
set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
3376
196
{
3377
196
  typedef struct arch_stack_entry
3378
196
  {
3379
196
    const struct arch_stack_entry *prev;
3380
196
    const char *name;
3381
196
    char *sub_name;
3382
196
    i386_cpu_flags flags;
3383
196
    i386_cpu_flags isa_flags;
3384
196
    enum processor_type isa;
3385
196
    enum flag_code flag_code;
3386
196
    unsigned int vector_size;
3387
196
    char stackop_size;
3388
196
    bool no_cond_jump_promotion;
3389
196
  } arch_stack_entry;
3390
196
  static const arch_stack_entry *arch_stack_top;
3391
196
  char *s;
3392
196
  int e;
3393
196
  const char *string;
3394
196
  unsigned int j = 0;
3395
3396
196
  SKIP_WHITESPACE ();
3397
3398
196
  if (is_end_of_stmt (*input_line_pointer))
3399
1
    {
3400
1
      as_bad (_("missing cpu architecture"));
3401
1
      input_line_pointer++;
3402
1
      return;
3403
1
    }
3404
3405
195
  e = get_symbol_name (&s);
3406
195
  string = s;
3407
3408
195
  if (strcmp (string, "push") == 0)
3409
2
    {
3410
2
      arch_stack_entry *top = XNEW (arch_stack_entry);
3411
3412
2
      top->name = cpu_arch_name;
3413
2
      if (cpu_sub_arch_name)
3414
2
  top->sub_name = xstrdup (cpu_sub_arch_name);
3415
0
      else
3416
0
  top->sub_name = NULL;
3417
2
      top->flags = cpu_arch_flags;
3418
2
      top->isa = cpu_arch_isa;
3419
2
      top->isa_flags = cpu_arch_isa_flags;
3420
2
      top->flag_code = flag_code;
3421
2
      top->vector_size = vector_size;
3422
2
      top->stackop_size = stackop_size;
3423
2
      top->no_cond_jump_promotion = no_cond_jump_promotion;
3424
3425
2
      top->prev = arch_stack_top;
3426
2
      arch_stack_top = top;
3427
3428
2
      (void) restore_line_pointer (e);
3429
2
      demand_empty_rest_of_line ();
3430
2
      return;
3431
2
    }
3432
3433
193
  if (strcmp (string, "pop") == 0)
3434
0
    {
3435
0
      const arch_stack_entry *top = arch_stack_top;
3436
3437
0
      if (!top)
3438
0
  {
3439
0
    as_bad (_(".arch stack is empty"));
3440
91
  restore_bad:
3441
91
    (void) restore_line_pointer (e);
3442
91
    ignore_rest_of_line ();
3443
91
    return;
3444
0
  }
3445
3446
0
      if (top->flag_code != flag_code
3447
0
    || top->stackop_size != stackop_size)
3448
0
  {
3449
0
    static const unsigned int bits[] = {
3450
0
      [CODE_16BIT] = 16,
3451
0
      [CODE_32BIT] = 32,
3452
0
      [CODE_64BIT] = 64,
3453
0
    };
3454
3455
0
    as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
3456
0
      bits[top->flag_code],
3457
0
      top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
3458
0
    goto restore_bad;
3459
0
  }
3460
3461
0
      arch_stack_top = top->prev;
3462
3463
0
      cpu_arch_name = top->name;
3464
0
      free (cpu_sub_arch_name);
3465
0
      cpu_sub_arch_name = top->sub_name;
3466
0
      cpu_arch_flags = top->flags;
3467
0
      cpu_arch_isa = top->isa;
3468
0
      cpu_arch_isa_flags = top->isa_flags;
3469
0
      vector_size = top->vector_size;
3470
0
      no_cond_jump_promotion = top->no_cond_jump_promotion;
3471
3472
0
      XDELETE (top);
3473
3474
0
      (void) restore_line_pointer (e);
3475
0
      demand_empty_rest_of_line ();
3476
0
      return;
3477
0
    }
3478
3479
193
  if (strcmp (string, "default") == 0)
3480
0
    {
3481
0
      if (strcmp (default_arch, "iamcu") == 0)
3482
0
  string = default_arch;
3483
0
      else
3484
0
  {
3485
0
    static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
3486
3487
0
    cpu_arch_name = NULL;
3488
0
    free (cpu_sub_arch_name);
3489
0
    cpu_sub_arch_name = NULL;
3490
0
    cpu_arch_flags = cpu_unknown_flags;
3491
0
    cpu_arch_isa = PROCESSOR_UNKNOWN;
3492
0
    cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
3493
0
    if (!cpu_arch_tune_set)
3494
0
      cpu_arch_tune = PROCESSOR_UNKNOWN;
3495
3496
0
    vector_size = VSZ_DEFAULT;
3497
3498
0
    j = ARRAY_SIZE (cpu_arch) + 1;
3499
0
  }
3500
0
    }
3501
3502
36.9k
  for (; j < ARRAY_SIZE (cpu_arch); j++)
3503
36.8k
    {
3504
36.8k
      if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
3505
78
    && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
3506
78
  {
3507
78
    if (*string != '.')
3508
8
      {
3509
8
        check_cpu_arch_compatible (string, cpu_arch[j].enable);
3510
3511
8
        if (flag_code == CODE_64BIT && !cpu_arch[j].enable.bitfield.cpu64 )
3512
0
    {
3513
0
      as_bad (_("64bit mode not supported on `%s'."),
3514
0
        cpu_arch[j].name);
3515
0
      goto restore_bad;
3516
0
    }
3517
3518
8
        if (flag_code == CODE_32BIT && !cpu_arch[j].enable.bitfield.cpui386)
3519
0
    {
3520
0
      as_bad (_("32bit mode not supported on `%s'."),
3521
0
        cpu_arch[j].name);
3522
0
      goto restore_bad;
3523
0
    }
3524
3525
8
        cpu_arch_name = cpu_arch[j].name;
3526
8
        free (cpu_sub_arch_name);
3527
8
        cpu_sub_arch_name = NULL;
3528
8
        cpu_arch_flags = cpu_arch[j].enable;
3529
8
        cpu_arch_isa = cpu_arch[j].type;
3530
8
        cpu_arch_isa_flags = cpu_arch[j].enable;
3531
8
        if (!cpu_arch_tune_set)
3532
8
    cpu_arch_tune = cpu_arch_isa;
3533
3534
8
        vector_size = VSZ_DEFAULT;
3535
3536
8
        pre_386_16bit_warned = false;
3537
8
        break;
3538
8
      }
3539
3540
70
    if (cpu_flags_all_zero (&cpu_arch[j].enable))
3541
0
      continue;
3542
3543
70
    isa_enable (j);
3544
3545
70
    (void) restore_line_pointer (e);
3546
3547
70
    switch (cpu_arch[j].vsz)
3548
70
      {
3549
26
      default:
3550
26
        break;
3551
3552
43
      case vsz_set:
3553
#ifdef SVR4_COMMENT_CHARS
3554
        if (*input_line_pointer == ':' || *input_line_pointer == '/')
3555
#else
3556
43
        if (*input_line_pointer == '/')
3557
43
#endif
3558
43
    {
3559
43
      ++input_line_pointer;
3560
43
      switch (get_absolute_expression ())
3561
43
        {
3562
8
        case 512: vector_size = VSZ512; break;
3563
0
        case 256: vector_size = VSZ256; break;
3564
10
        case 128: vector_size = VSZ128; break;
3565
25
        default:
3566
25
          as_bad (_("Unrecognized vector size specifier"));
3567
25
          ignore_rest_of_line ();
3568
25
          return;
3569
43
        }
3570
18
      break;
3571
43
    }
3572
    /* Fall through.  */
3573
1
      case vsz_reset:
3574
1
        vector_size = VSZ_DEFAULT;
3575
1
        break;
3576
70
      }
3577
3578
45
    demand_empty_rest_of_line ();
3579
45
    return;
3580
70
  }
3581
36.8k
    }
3582
3583
123
  if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
3584
25
    {
3585
      /* Disable an ISA extension.  */
3586
3.97k
      for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
3587
3.97k
  if (cpu_arch[j].type == PROCESSOR_NONE
3588
2.94k
      && strcmp (string + 3, cpu_arch[j].name) == 0)
3589
24
    {
3590
24
      isa_disable (j);
3591
3592
24
      if (cpu_arch[j].vsz == vsz_set)
3593
15
        vector_size = VSZ_DEFAULT;
3594
3595
24
      (void) restore_line_pointer (e);
3596
24
      demand_empty_rest_of_line ();
3597
24
      return;
3598
24
    }
3599
25
    }
3600
3601
99
  if (j == ARRAY_SIZE (cpu_arch))
3602
91
    {
3603
91
      as_bad (_("no such architecture: `%s'"), string);
3604
91
      goto restore_bad;
3605
91
    }
3606
3607
8
  no_cond_jump_promotion = 0;
3608
8
  if (restore_line_pointer (e) == ','
3609
8
      && !is_end_of_stmt (input_line_pointer[1]))
3610
0
    {
3611
0
      ++input_line_pointer;
3612
0
      e = get_symbol_name (&s);
3613
0
      string = s;
3614
3615
0
      if (strcmp (string, "nojumps") == 0)
3616
0
  {
3617
0
    if (cpu_arch_flags.bitfield.cpui386)
3618
0
      as_bad (_("`%s' only supported with 16-bit architectures"), string);
3619
0
    else
3620
0
      no_cond_jump_promotion = true;
3621
0
  }
3622
0
      else if (strcmp (string, "jumps") != 0)
3623
0
  {
3624
0
    as_bad (_("no such architecture modifier: `%s'"), string);
3625
0
    goto restore_bad;
3626
0
  }
3627
3628
0
      (void) restore_line_pointer (e);
3629
0
    }
3630
3631
8
  demand_empty_rest_of_line ();
3632
8
}
3633
3634
enum bfd_architecture
3635
i386_arch (void)
3636
368
{
3637
368
  if (cpu_arch_isa == PROCESSOR_IAMCU)
3638
0
    {
3639
0
      if (!IS_ELF || flag_code == CODE_64BIT)
3640
0
  as_fatal (_("Intel MCU is 32bit ELF only"));
3641
0
      return bfd_arch_iamcu;
3642
0
    }
3643
368
  else
3644
368
    return bfd_arch_i386;
3645
368
}
3646
3647
unsigned long
3648
i386_mach (void)
3649
368
{
3650
368
  if (startswith (default_arch, "x86_64"))
3651
368
    {
3652
368
      if (default_arch[6] == '\0')
3653
368
  return bfd_mach_x86_64;
3654
0
      else
3655
0
  return bfd_mach_x64_32;
3656
368
    }
3657
0
  else if (!strcmp (default_arch, "i386")
3658
0
     || !strcmp (default_arch, "iamcu"))
3659
0
    {
3660
0
      if (cpu_arch_isa == PROCESSOR_IAMCU)
3661
0
  {
3662
0
    if (!IS_ELF)
3663
0
      as_fatal (_("Intel MCU is 32bit ELF only"));
3664
0
    return bfd_mach_i386_iamcu;
3665
0
  }
3666
0
      else
3667
0
  return bfd_mach_i386_i386;
3668
0
    }
3669
0
  else
3670
0
    as_fatal (_("unknown architecture"));
3671
368
}
3672

3673
static void
3674
op_lookup (const char *mnemonic)
3675
51.3k
{
3676
51.3k
   i386_op_off_t *pos = str_hash_find (op_hash, mnemonic);
3677
3678
51.3k
   if (pos != NULL)
3679
28.9k
     {
3680
28.9k
       current_templates.start = &i386_optab[pos[0]];
3681
28.9k
       current_templates.end = &i386_optab[pos[1]];
3682
28.9k
     }
3683
22.4k
   else
3684
22.4k
     current_templates.end = current_templates.start = NULL;
3685
51.3k
}
3686
3687
void
3688
md_begin (void)
3689
368
{
3690
  /* Make sure possible padding space is clear.  */
3691
368
  memset (&pp, 0, sizeof (pp));
3692
3693
  /* Initialize op_hash hash table.  */
3694
368
  op_hash = str_htab_create ();
3695
3696
368
  {
3697
368
    const i386_op_off_t *cur = i386_op_sets;
3698
368
    const i386_op_off_t *end = cur + ARRAY_SIZE (i386_op_sets) - 1;
3699
3700
984k
    for (; cur < end; ++cur)
3701
983k
      if (str_hash_insert (op_hash, insn_name (&i386_optab[*cur]), cur, 0))
3702
0
  as_fatal (_("duplicate %s"), insn_name (&i386_optab[*cur]));
3703
368
  }
3704
3705
  /* Initialize reg_hash hash table.  */
3706
368
  reg_hash = str_htab_create ();
3707
368
  {
3708
368
    const reg_entry *regtab;
3709
368
    unsigned int regtab_size = i386_regtab_size;
3710
3711
130k
    for (regtab = i386_regtab; regtab_size--; regtab++)
3712
129k
      {
3713
129k
  switch (regtab->reg_type.bitfield.class)
3714
129k
    {
3715
50.0k
    case Reg:
3716
50.0k
      if (regtab->reg_type.bitfield.dword)
3717
11.7k
        {
3718
11.7k
    if (regtab->reg_type.bitfield.instance == Accum)
3719
368
      reg_eax = regtab;
3720
11.7k
        }
3721
50.0k
      break;
3722
3723
2.94k
    case RegFP:
3724
      /* There's no point inserting st(<N>) in the hash table, as
3725
         parentheses aren't included in register_chars[] anyway.  */
3726
2.94k
      if (regtab->reg_type.bitfield.instance != Accum)
3727
2.57k
        continue;
3728
368
      reg_st0 = regtab;
3729
368
      break;
3730
3731
2.57k
    case SReg:
3732
2.57k
      switch (regtab->reg_num)
3733
2.57k
        {
3734
368
        case 0: reg_es = regtab; break;
3735
368
        case 2: reg_ss = regtab; break;
3736
368
        case 3: reg_ds = regtab; break;
3737
2.57k
        }
3738
2.57k
      break;
3739
3740
2.94k
    case RegMask:
3741
2.94k
      if (!regtab->reg_num)
3742
368
        reg_k0 = regtab;
3743
2.94k
      break;
3744
129k
    }
3745
3746
127k
  if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3747
0
    as_fatal (_("duplicate %s"), regtab->reg_name);
3748
127k
      }
3749
368
  }
3750
3751
  /* Fill in lexical tables:  mnemonic_chars, operand_chars.  */
3752
368
  {
3753
368
    int c;
3754
368
    const char *p;
3755
3756
94.5k
    for (c = 0; c < 256; c++)
3757
94.2k
      {
3758
94.2k
  if (ISDIGIT (c) || ISLOWER (c))
3759
13.2k
    {
3760
13.2k
      mnemonic_chars[c] = c;
3761
13.2k
      register_chars[c] = c;
3762
13.2k
      operand_chars[c] = c;
3763
13.2k
    }
3764
80.9k
  else if (ISUPPER (c))
3765
9.56k
    {
3766
9.56k
      mnemonic_chars[c] = TOLOWER (c);
3767
9.56k
      register_chars[c] = mnemonic_chars[c];
3768
9.56k
      operand_chars[c] = c;
3769
9.56k
    }
3770
#ifdef SVR4_COMMENT_CHARS
3771
  else if (c == '\\' && strchr (i386_comment_chars, '/'))
3772
    operand_chars[c] = c;
3773
#endif
3774
3775
94.2k
  if (c >= 128)
3776
47.1k
    operand_chars[c] = c;
3777
94.2k
      }
3778
3779
368
    mnemonic_chars['_'] = '_';
3780
368
    mnemonic_chars['-'] = '-';
3781
368
    mnemonic_chars['.'] = '.';
3782
3783
2.20k
    for (p = extra_symbol_chars; *p != '\0'; p++)
3784
1.84k
      operand_chars[(unsigned char) *p] = *p;
3785
7.72k
    for (p = operand_special_chars; *p != '\0'; p++)
3786
7.36k
      operand_chars[(unsigned char) *p] = *p;
3787
368
  }
3788
3789
368
  if (object_64bit)
3790
368
    {
3791
#if defined (OBJ_COFF) && defined (TE_PE)
3792
      x86_dwarf2_return_column = 32;
3793
#else
3794
368
      x86_dwarf2_return_column = REG_RA;
3795
368
#endif
3796
368
      x86_cie_data_alignment = -8;
3797
368
    }
3798
0
  else
3799
0
    {
3800
0
      x86_dwarf2_return_column = 8;
3801
0
      x86_cie_data_alignment = -4;
3802
0
    }
3803
3804
  /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3805
     can be turned into BRANCH_PREFIX frag.  */
3806
368
  if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3807
0
    abort ();
3808
368
}
3809
3810
void
3811
i386_print_statistics (FILE *file)
3812
0
{
3813
0
  htab_print_statistics (file, "i386 opcode", op_hash);
3814
0
  htab_print_statistics (file, "i386 register", reg_hash);
3815
0
}
3816
3817
void
3818
i386_md_end (void)
3819
368
{
3820
368
  if (!ENABLE_LEAK_CHECK)
3821
0
    return;
3822
368
  htab_delete (op_hash);
3823
368
  htab_delete (reg_hash);
3824
368
  GOT_symbol = NULL;
3825
368
}
3826

3827
#ifdef DEBUG386
3828
3829
/* Debugging routines for md_assemble.  */
3830
static void pte (insn_template *);
3831
static void pt (i386_operand_type);
3832
static void pe (expressionS *);
3833
static void ps (symbolS *);
3834
3835
static void
3836
pi (const char *line, i386_insn *x)
3837
{
3838
  unsigned int j;
3839
3840
  fprintf (stdout, "%s: template ", line);
3841
  pte (&x->tm);
3842
  fprintf (stdout, "  address: base %s  index %s  scale %x\n",
3843
     x->base_reg ? x->base_reg->reg_name : "none",
3844
     x->index_reg ? x->index_reg->reg_name : "none",
3845
     x->log2_scale_factor);
3846
  fprintf (stdout, "  modrm:  mode %x  reg %x  reg/mem %x\n",
3847
     x->rm.mode, x->rm.reg, x->rm.regmem);
3848
  fprintf (stdout, "  sib:  base %x  index %x  scale %x\n",
3849
     x->sib.base, x->sib.index, x->sib.scale);
3850
  fprintf (stdout, "  rex: 64bit %x  extX %x  extY %x  extZ %x\n",
3851
     (x->rex & REX_W) != 0,
3852
     (x->rex & REX_R) != 0,
3853
     (x->rex & REX_X) != 0,
3854
     (x->rex & REX_B) != 0);
3855
  for (j = 0; j < x->operands; j++)
3856
    {
3857
      fprintf (stdout, "    #%d:  ", j + 1);
3858
      pt (x->types[j]);
3859
      fprintf (stdout, "\n");
3860
      if (x->types[j].bitfield.class == Reg
3861
    || x->types[j].bitfield.class == RegFP
3862
    || x->types[j].bitfield.class == RegMMX
3863
    || x->types[j].bitfield.class == RegSIMD
3864
    || x->types[j].bitfield.class == RegMask
3865
    || x->types[j].bitfield.class == SReg
3866
    || x->types[j].bitfield.class == RegCR
3867
    || x->types[j].bitfield.class == RegDR
3868
    || x->types[j].bitfield.class == RegTR
3869
    || x->types[j].bitfield.class == RegBND)
3870
  fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3871
      if (operand_type_check (x->types[j], imm))
3872
  pe (x->op[j].imms);
3873
      if (operand_type_check (x->types[j], disp))
3874
  pe (x->op[j].disps);
3875
    }
3876
}
3877
3878
static void
3879
pte (insn_template *t)
3880
{
3881
  static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
3882
  static const char *const opc_spc[] = {
3883
    NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
3884
    "XOP08", "XOP09", "XOP0A",
3885
  };
3886
  unsigned int j;
3887
3888
  fprintf (stdout, " %d operands ", t->operands);
3889
  if (opc_pfx[t->opcode_modifier.opcodeprefix])
3890
    fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3891
  if (opc_spc[t->opcode_space])
3892
    fprintf (stdout, "space %s ", opc_spc[t->opcode_space]);
3893
  fprintf (stdout, "opcode %x ", t->base_opcode);
3894
  if (t->extension_opcode != None)
3895
    fprintf (stdout, "ext %x ", t->extension_opcode);
3896
  if (t->opcode_modifier.d)
3897
    fprintf (stdout, "D");
3898
  if (t->opcode_modifier.w)
3899
    fprintf (stdout, "W");
3900
  fprintf (stdout, "\n");
3901
  const i386_operand_type *t_types = get_operand_types (t);
3902
  for (j = 0; j < t->operands; j++)
3903
    {
3904
      fprintf (stdout, "    #%d type ", j + 1);
3905
      pt (t_types[j]);
3906
      fprintf (stdout, "\n");
3907
    }
3908
}
3909
3910
static void
3911
pe (expressionS *e)
3912
{
3913
  fprintf (stdout, "    operation     %d\n", e->X_op);
3914
  fprintf (stdout, "    add_number    %" PRId64 " (%" PRIx64 ")\n",
3915
     (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
3916
  if (e->X_add_symbol)
3917
    {
3918
      fprintf (stdout, "    add_symbol    ");
3919
      ps (e->X_add_symbol);
3920
      fprintf (stdout, "\n");
3921
    }
3922
  if (e->X_op_symbol)
3923
    {
3924
      fprintf (stdout, "    op_symbol    ");
3925
      ps (e->X_op_symbol);
3926
      fprintf (stdout, "\n");
3927
    }
3928
}
3929
3930
static void
3931
ps (symbolS *s)
3932
{
3933
  fprintf (stdout, "%s type %s%s",
3934
     S_GET_NAME (s),
3935
     S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3936
     segment_name (S_GET_SEGMENT (s)));
3937
}
3938
3939
static struct type_name
3940
  {
3941
    i386_operand_type mask;
3942
    const char *name;
3943
  }
3944
const type_names[] =
3945
{
3946
  { { .bitfield = { .class = Reg, .byte = 1 } }, "r8" },
3947
  { { .bitfield = { .class = Reg, .word = 1 } }, "r16" },
3948
  { { .bitfield = { .class = Reg, .dword = 1 } }, "r32" },
3949
  { { .bitfield = { .class = Reg, .qword = 1 } }, "r64" },
3950
  { { .bitfield = { .instance = Accum, .byte = 1 } }, "acc8" },
3951
  { { .bitfield = { .instance = Accum, .word = 1 } }, "acc16" },
3952
  { { .bitfield = { .instance = Accum, .dword = 1 } }, "acc32" },
3953
  { { .bitfield = { .instance = Accum, .qword = 1 } }, "acc64" },
3954
  { { .bitfield = { .imm8 = 1 } }, "i8" },
3955
  { { .bitfield = { .imm8s = 1 } }, "i8s" },
3956
  { { .bitfield = { .imm16 = 1 } }, "i16" },
3957
  { { .bitfield = { .imm32 = 1 } }, "i32" },
3958
  { { .bitfield = { .imm32s = 1 } }, "i32s" },
3959
  { { .bitfield = { .imm64 = 1 } }, "i64" },
3960
  { { .bitfield = { .imm1 = 1 } }, "i1" },
3961
  { { .bitfield = { .baseindex = 1 } }, "BaseIndex" },
3962
  { { .bitfield = { .disp8 = 1 } }, "d8" },
3963
  { { .bitfield = { .disp16 = 1 } }, "d16" },
3964
  { { .bitfield = { .disp32 = 1 } }, "d32" },
3965
  { { .bitfield = { .disp64 = 1 } }, "d64" },
3966
  { { .bitfield = { .instance = RegD, .word = 1 } }, "InOutPortReg" },
3967
  { { .bitfield = { .instance = RegC, .byte = 1 } }, "ShiftCount" },
3968
  { { .bitfield = { .class = RegCR } }, "control reg" },
3969
  { { .bitfield = { .class = RegTR } }, "test reg" },
3970
  { { .bitfield = { .class = RegDR } }, "debug reg" },
3971
  { { .bitfield = { .class = RegFP, .tbyte = 1 } }, "FReg" },
3972
  { { .bitfield = { .instance = Accum, .tbyte = 1 } }, "FAcc" },
3973
  { { .bitfield = { .class = SReg } }, "SReg" },
3974
  { { .bitfield = { .class = RegMMX } }, "rMMX" },
3975
  { { .bitfield = { .class = RegSIMD, .xmmword = 1 } }, "rXMM" },
3976
  { { .bitfield = { .class = RegSIMD, .ymmword = 1 } }, "rYMM" },
3977
  { { .bitfield = { .class = RegSIMD, .zmmword = 1 } }, "rZMM" },
3978
  { { .bitfield = { .class = RegSIMD, .tmmword = 1 } }, "rTMM" },
3979
  { { .bitfield = { .class = RegSIMD, .byte = 1 } }, "bsr" },
3980
  { { .bitfield = { .class = RegMask } }, "Mask reg" },
3981
  { { .bitfield = { .class = RegBND } }, "rBND" },
3982
};
3983
3984
static void
3985
pt (i386_operand_type t)
3986
{
3987
  unsigned int j;
3988
  i386_operand_type a;
3989
3990
  for (j = 0; j < ARRAY_SIZE (type_names); j++)
3991
    {
3992
      a = operand_type_and (t, type_names[j].mask);
3993
      if (operand_type_equal (&a, &type_names[j].mask))
3994
  fprintf (stdout, "%s, ",  type_names[j].name);
3995
    }
3996
  fflush (stdout);
3997
}
3998
3999
#endif /* DEBUG386 */
4000

4001
static bfd_reloc_code_real_type
4002
_reloc (unsigned int size,
4003
  bool pcrel,
4004
  int sign,
4005
  bfd_reloc_code_real_type other,
4006
  bool code64,
4007
  const char *file,
4008
  unsigned int line)
4009
5.43k
{
4010
5.43k
  if (other != NO_RELOC)
4011
54
    {
4012
54
      reloc_howto_type *rel;
4013
4014
54
      if (size == 8)
4015
18
  switch (other)
4016
18
    {
4017
0
    case BFD_RELOC_64_PLTOFF:
4018
0
    case BFD_RELOC_X86_64_GOTPLT64:
4019
0
      return other;
4020
8
    case BFD_RELOC_X86_64_GOT32:
4021
8
      return BFD_RELOC_X86_64_GOT64;
4022
0
    case BFD_RELOC_X86_64_GOTPC32:
4023
0
      other = BFD_RELOC_64_GOT_PCREL;
4024
0
      break;
4025
0
    case BFD_RELOC_X86_64_GOTPCREL:
4026
0
      other = BFD_RELOC_X86_64_GOTPCREL64;
4027
0
      break;
4028
0
    case BFD_RELOC_X86_64_TPOFF32:
4029
0
      other = BFD_RELOC_X86_64_TPOFF64;
4030
0
      break;
4031
8
    case BFD_RELOC_X86_64_DTPOFF32:
4032
8
      other = BFD_RELOC_X86_64_DTPOFF64;
4033
8
      break;
4034
2
    default:
4035
2
      break;
4036
18
    }
4037
4038
46
#ifdef OBJ_ELF
4039
46
      if (other == BFD_RELOC_SIZE32)
4040
2
  {
4041
2
    if (size == 8)
4042
2
      other = BFD_RELOC_SIZE64;
4043
2
    if (pcrel)
4044
0
      {
4045
0
        as_bad_where (file, line,
4046
0
          _("there are no pc-relative size relocations"));
4047
0
        return NO_RELOC;
4048
0
      }
4049
2
  }
4050
46
#endif
4051
4052
      /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless.  */
4053
46
      if (size == 4 && (!code64 || disallow_64bit_reloc))
4054
0
  sign = -1;
4055
4056
46
      rel = bfd_reloc_type_lookup (stdoutput, other);
4057
46
      if (!rel)
4058
0
  as_bad_where (file, line, _("unknown relocation (%u)"), other);
4059
46
      else if (size != bfd_get_reloc_size (rel))
4060
2
  as_bad_where (file, line,
4061
2
          _("%u-byte relocation cannot be applied to %u-byte field"),
4062
2
          bfd_get_reloc_size (rel), size);
4063
44
      else if (pcrel && !rel->pc_relative)
4064
1
  as_bad_where (file, line,
4065
1
          _("non-pc-relative relocation for pc-relative field"));
4066
43
      else if ((rel->complain_on_overflow == complain_overflow_signed
4067
33
    && !sign)
4068
43
         || (rel->complain_on_overflow == complain_overflow_unsigned
4069
0
       && sign > 0))
4070
0
  as_bad_where (file, line,
4071
0
          _("relocated field and relocation type differ in signedness"));
4072
43
      else
4073
43
  return other;
4074
3
      return NO_RELOC;
4075
46
    }
4076
4077
5.38k
  if (pcrel)
4078
35
    {
4079
35
      if (!sign)
4080
0
  as_bad_where (file, line,
4081
0
          _("there are no unsigned pc-relative relocations"));
4082
35
      switch (size)
4083
35
  {
4084
0
  case 1: return BFD_RELOC_8_PCREL;
4085
21
  case 2: return BFD_RELOC_16_PCREL;
4086
14
  case 4: return BFD_RELOC_32_PCREL;
4087
0
  case 8: return BFD_RELOC_64_PCREL;
4088
35
  }
4089
0
      as_bad_where (file, line,
4090
0
        _("cannot do %u byte pc-relative relocation"), size);
4091
0
    }
4092
5.34k
  else
4093
5.34k
    {
4094
5.34k
      if (sign > 0)
4095
4.08k
  switch (size)
4096
4.08k
    {
4097
4.08k
    case 4: return BFD_RELOC_X86_64_32S;
4098
4.08k
    }
4099
1.26k
      else
4100
1.26k
  switch (size)
4101
1.26k
    {
4102
370
    case 1: return BFD_RELOC_8;
4103
42
    case 2: return BFD_RELOC_16;
4104
720
    case 4: return BFD_RELOC_32;
4105
132
    case 8: return BFD_RELOC_64;
4106
1.26k
    }
4107
0
      as_bad_where (file, line, _("cannot do %s %u byte relocation"),
4108
0
        sign > 0 ? "signed" : "unsigned", size);
4109
0
    }
4110
4111
0
  return NO_RELOC;
4112
5.38k
}
4113
4114
static bfd_reloc_code_real_type
4115
reloc (unsigned int size,
4116
       bool pcrel,
4117
       int sign,
4118
       bfd_reloc_code_real_type other)
4119
5.43k
{
4120
5.43k
  return _reloc (size, pcrel, sign, other, flag_code == CODE_64BIT, NULL, 0);
4121
5.43k
}
4122
4123
#ifdef OBJ_ELF
4124
/* Here we decide which fixups can be adjusted to make them relative to
4125
   the beginning of the section instead of the symbol.  Basically we need
4126
   to make sure that the dynamic relocations are done correctly, so in
4127
   some cases we force the original symbol to be used.  */
4128
4129
int
4130
tc_i386_fix_adjustable (fixS *fixP)
4131
0
{
4132
  /* Don't adjust pc-relative references to merge sections in 64-bit
4133
     mode.  */
4134
0
  if (use_rela_relocations
4135
0
      && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
4136
0
      && fixP->fx_pcrel)
4137
0
    return 0;
4138
4139
  /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
4140
     and changed later by validate_fix.  */
4141
0
  if (GOT_symbol && fixP->fx_subsy == GOT_symbol
4142
0
      && fixP->fx_r_type == BFD_RELOC_32_PCREL)
4143
0
    return 0;
4144
4145
  /* Adjust_reloc_syms doesn't know about the GOT.  Need to keep symbol
4146
     for size relocations.  */
4147
0
  if (fixP->fx_r_type == BFD_RELOC_SIZE32
4148
0
      || fixP->fx_r_type == BFD_RELOC_SIZE64
4149
0
      || fixP->fx_r_type == BFD_RELOC_32_GOTOFF
4150
0
      || fixP->fx_r_type == BFD_RELOC_386_GOT32
4151
0
      || fixP->fx_r_type == BFD_RELOC_386_GOT32X
4152
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
4153
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
4154
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
4155
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
4156
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
4157
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
4158
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
4159
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
4160
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
4161
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
4162
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
4163
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
4164
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
4165
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
4166
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_4_GOTPCRELX
4167
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_5_GOTPCRELX
4168
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_6_GOTPCRELX
4169
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
4170
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
4171
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
4172
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
4173
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
4174
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_4_GOTTPOFF
4175
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_5_GOTTPOFF
4176
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_6_GOTTPOFF
4177
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
4178
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
4179
0
      || fixP->fx_r_type == BFD_RELOC_64_GOTOFF
4180
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOT64
4181
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
4182
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC
4183
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_5_GOTPC32_TLSDESC
4184
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_6_GOTPC32_TLSDESC
4185
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
4186
0
      || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
4187
0
      || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
4188
0
    return 0;
4189
  /* Resolve PLT32 relocation against local symbol to section only for
4190
     PC-relative relocations.  */
4191
0
  if (fixP->fx_r_type == BFD_RELOC_386_PLT32
4192
0
      || fixP->fx_r_type == BFD_RELOC_32_PLT_PCREL)
4193
0
    return fixP->fx_pcrel;
4194
0
  return 1;
4195
0
}
4196
#endif
4197
4198
static INLINE bool
4199
want_disp32 (const insn_template *t)
4200
6.39k
{
4201
6.39k
  return flag_code != CODE_64BIT
4202
5.96k
   || i.prefix[ADDR_PREFIX]
4203
5.96k
   || ((t->mnem_off == MN_lea
4204
5.96k
        || (i.tm.base_opcode == 0x8d && i.tm.opcode_space == SPACE_BASE))
4205
0
       && (!i.types[1].bitfield.qword
4206
0
     || t->opcode_modifier.size == SIZE32));
4207
6.39k
}
4208
4209
static INLINE bool is_padlock (const insn_template *t)
4210
6.40k
{
4211
  /* (Ab)use the PrefixRepe attribute of PadLock insns as long as no
4212
     others use it.  */
4213
6.40k
  return t->opcode_modifier.prefixok == PrefixRepe;
4214
6.40k
}
4215
4216
static int
4217
intel_float_operand (const char *mnemonic)
4218
30.9k
{
4219
  /* Note that the value returned is meaningful only for opcodes with (memory)
4220
     operands, hence the code here is free to improperly handle opcodes that
4221
     have no operands (for better performance and smaller code). */
4222
4223
30.9k
  if (mnemonic[0] != 'f')
4224
29.9k
    return 0; /* non-math */
4225
4226
998
  switch (mnemonic[1])
4227
998
    {
4228
    /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
4229
       the fs segment override prefix not currently handled because no
4230
       call path can make opcodes without operands get here */
4231
2
    case 'i':
4232
2
      return 2 /* integer op */;
4233
0
    case 'l':
4234
0
      if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
4235
0
  return 3; /* fldcw/fldenv */
4236
0
      break;
4237
0
    case 'n':
4238
0
      if (mnemonic[2] != 'o' /* fnop */)
4239
0
  return 3; /* non-waiting control op */
4240
0
      break;
4241
0
    case 'r':
4242
0
      if (mnemonic[2] == 's')
4243
0
  return 3; /* frstor/frstpm */
4244
0
      break;
4245
996
    case 's':
4246
996
      if (mnemonic[2] == 'a')
4247
0
  return 3; /* fsave */
4248
996
      if (mnemonic[2] == 't')
4249
32
  {
4250
32
    switch (mnemonic[3])
4251
32
      {
4252
0
      case 'c': /* fstcw */
4253
0
      case 'd': /* fstdw */
4254
0
      case 'e': /* fstenv */
4255
0
      case 's': /* fsts[gw] */
4256
0
        return 3;
4257
32
      }
4258
32
  }
4259
996
      break;
4260
996
    case 'x':
4261
0
      if (mnemonic[2] == 'r' || mnemonic[2] == 's')
4262
0
  return 0; /* fxsave/fxrstor are not really math ops */
4263
0
      break;
4264
998
    }
4265
4266
996
  return 1;
4267
998
}
4268
4269
static INLINE void
4270
install_template (const insn_template *t)
4271
6.47k
{
4272
6.47k
  unsigned int l;
4273
4274
6.47k
  i.tm = *t;
4275
4276
6.47k
  const i386_operand_type *t_types = get_operand_types (t);
4277
14.4k
  for (l = 0; l < t->operands; ++l)
4278
8.00k
    i.tm_types[l] = t_types[l];
4279
4280
  /* Dual VEX/EVEX templates need stripping one of the possible variants.  */
4281
6.47k
  if (t->opcode_modifier.vex && t->opcode_modifier.evex)
4282
0
    {
4283
0
      if ((maybe_cpu (t, CpuAVX) || maybe_cpu (t, CpuAVX2)
4284
0
     || maybe_cpu (t, CpuFMA) || maybe_cpu (t, CpuF16C))
4285
0
    && (maybe_cpu (t, CpuAVX512F) || maybe_cpu (t, CpuAVX512VL)))
4286
0
  {
4287
0
    bool evex = need_evex_encoding (t) || pp.encoding == encoding_egpr;
4288
4289
0
    if (!evex
4290
0
        && ((maybe_cpu (t, CpuFMA) && !cpu_arch_flags.bitfield.cpufma)
4291
0
      || (maybe_cpu (t, CpuF16C) && !cpu_arch_flags.bitfield.cpuf16c)))
4292
0
      {
4293
0
        if (!cpu_arch_isa_flags.bitfield.cpuavx512vl
4294
0
      && !i.types[i.operands - 1].bitfield.zmmword)
4295
0
    as_warn(_("%s: will use AVX512VL encoding; use {evex} to silence"),
4296
0
      insn_name (t));
4297
4298
0
        evex = true;
4299
0
      }
4300
4301
0
    if (evex)
4302
0
      {
4303
0
        i.tm.opcode_modifier.vex = 0;
4304
0
        i.tm.cpu.bitfield.cpuavx512f = i.tm.cpu_any.bitfield.cpuavx512f;
4305
0
        i.tm.cpu.bitfield.cpuavx512vl = i.tm.cpu_any.bitfield.cpuavx512vl;
4306
0
      }
4307
0
    else
4308
0
      {
4309
0
        i.tm.opcode_modifier.evex = 0;
4310
0
        if (i.tm.cpu_any.bitfield.cpuavx)
4311
0
    i.tm.cpu.bitfield.cpuavx = 1;
4312
0
        else if (!i.tm.cpu.bitfield.isa)
4313
0
    i.tm.cpu.bitfield.isa = i.tm.cpu_any.bitfield.isa;
4314
0
        else
4315
0
    gas_assert (i.tm.cpu.bitfield.isa == i.tm.cpu_any.bitfield.isa);
4316
0
      }
4317
0
  }
4318
4319
0
      if ((maybe_cpu (t, CpuCMPCCXADD) || maybe_cpu (t, CpuAMX_TILE)
4320
0
     || maybe_cpu (t, CpuAVX512F) || maybe_cpu (t, CpuAVX512DQ)
4321
0
     || maybe_cpu (t, CpuAVX512BW) || maybe_cpu (t, CpuBMI)
4322
0
     || maybe_cpu (t, CpuBMI2) || maybe_cpu (t, CpuUSER_MSR)
4323
0
     || maybe_cpu (t, CpuMSR_IMM) || maybe_cpu (t, CpuAMX_TRANSPOSE)
4324
0
     || maybe_cpu (t, CpuAMX_MOVRS) || maybe_cpu (t, CpuTILE))
4325
0
    && maybe_cpu (t, CpuAPX_F))
4326
0
  {
4327
0
    if (need_evex_encoding (t))
4328
0
      i.tm.opcode_modifier.vex = 0;
4329
0
    else
4330
0
      i.tm.opcode_modifier.evex = 0;
4331
0
  }
4332
0
    }
4333
4334
  /* The various VNNI extensions are somewhat special:
4335
     - AVX512-VNNI pre-dates AVX-VNNI,
4336
     - AVX-VNNI-INT{8,16} have EVEX counterparts added by AVX10-V1-AUX.
4337
     In each case, when the former is disabled, warn about the use of a
4338
     potentially unexpected encoding unless
4339
     - a disambiguating pseudo-prefix or operand is in use, or
4340
     - the newer ISA extension was explicitly enabled.  */
4341
6.47k
  if (is_cpu (t, CpuAVX_VNNI)
4342
0
      && !cpu_arch_isa_flags.bitfield.cpuavx_vnni
4343
0
      && pp.encoding != encoding_vex
4344
0
      && pp.encoding != encoding_vex3
4345
0
      && (!cpu_arch_flags.bitfield.cpuavx512_vnni
4346
0
    || !cpu_arch_flags.bitfield.cpuavx512vl))
4347
0
    as_warn (_("%s: will use AVX-VNNI encoding; use {vex} to silence"),
4348
0
       insn_name (t));
4349
4350
6.47k
  if (is_cpu (t, CpuAVX10_1_AUX)
4351
0
      && !cpu_arch_isa_flags.bitfield.cpuavx10_1_aux
4352
0
      && !need_evex_encoding (t)
4353
0
      && pp.encoding != encoding_egpr
4354
0
      && ((!cpu_arch_flags.bitfield.cpuavx_vnni_int8
4355
0
     && is_cpu (t - 1, CpuAVX_VNNI_INT8))
4356
0
    || (!cpu_arch_flags.bitfield.cpuavx_vnni_int16
4357
0
     && is_cpu (t - 1, CpuAVX_VNNI_INT16))))
4358
0
    as_warn (_("%s: will use AVX10 encoding; use {evex} to silence"),
4359
0
       insn_name (t));
4360
4361
  /* CRC32 is also somewhat special, as its APX form is dependent upon only
4362
     APX_F.  */
4363
6.47k
  if (t->mnem_off == MN_crc32
4364
0
      && is_cpu (t, CpuAPX_F)
4365
0
      && !cpu_arch_isa_flags.bitfield.cpuapx_f
4366
0
      && !cpu_arch_flags.bitfield.cpusse4_2
4367
0
      && !need_evex_encoding (t)
4368
0
      && pp.encoding != encoding_egpr)
4369
0
    as_warn (_("%s: will use APX encoding; use {evex} to silence"),
4370
0
       insn_name (t));
4371
4372
  /* For CCMP and CTEST the template has EVEX.SCC in base_opcode. Move it out of
4373
     there, to then adjust base_opcode to obtain its normal meaning.  */
4374
6.47k
  if (i.tm.opcode_modifier.operandconstraint == SCC)
4375
3
    {
4376
      /* Get EVEX.SCC value from the lower 4 bits of base_opcode.  */
4377
3
      i.scc = i.tm.base_opcode & 0xf;
4378
3
      i.tm.base_opcode >>= 8;
4379
3
    }
4380
4381
  /* For CMOVcc having undergone NDD-to-legacy optimization with its source
4382
     operands being swapped, we need to invert the encoded condition.  */
4383
6.47k
  if (i.invert_cond)
4384
0
    i.tm.base_opcode ^= 1;
4385
4386
  /* Note that for pseudo prefixes this produces a length of 1. But for them
4387
     the length isn't interesting at all.  */
4388
6.48k
  for (l = 1; l < 4; ++l)
4389
6.48k
    if (!(i.tm.base_opcode >> (8 * l)))
4390
6.47k
      break;
4391
4392
6.47k
  i.opcode_length = l;
4393
6.47k
}
4394
4395
/* Build the VEX prefix (2- or 3-byte)
4396
4397
   | C5h |
4398
   | `R3 | `vvvv | L | pp |
4399
4400
   | C4h |
4401
   | `R3 | `X3 | `B3 | mmmmm |
4402
   | W | `vvvv | L | pp |
4403
*/
4404
static void
4405
build_vex_prefix (const insn_template *t)
4406
623
{
4407
623
  unsigned int register_specifier;
4408
623
  unsigned int vector_length;
4409
623
  bool w;
4410
4411
  /* Check register specifier.  */
4412
623
  if (i.vex.register_specifier)
4413
12
    {
4414
12
      register_specifier =
4415
12
  ~register_number (i.vex.register_specifier) & 0xf;
4416
12
      gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
4417
12
    }
4418
611
  else
4419
611
    register_specifier = 0xf;
4420
4421
  /* Use 2-byte VEX prefix by swapping destination and source operand
4422
     if there are more than 1 register operand.  */
4423
623
  if (i.reg_operands > 1
4424
51
      && pp.encoding != encoding_vex3
4425
51
      && pp.dir_encoding == dir_encoding_default
4426
51
      && i.operands == i.reg_operands
4427
51
      && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
4428
51
      && i.tm.opcode_space == SPACE_0F
4429
51
      && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
4430
51
      && i.rex == REX_B)
4431
0
    {
4432
0
      unsigned int xchg;
4433
4434
0
      swap_2_operands (0, i.operands - 1);
4435
4436
0
      gas_assert (i.rm.mode == 3);
4437
4438
0
      i.rex = REX_R;
4439
0
      xchg = i.rm.regmem;
4440
0
      i.rm.regmem = i.rm.reg;
4441
0
      i.rm.reg = xchg;
4442
4443
0
      if (i.tm.opcode_modifier.d)
4444
0
  i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
4445
0
          ? Opcode_ExtD : Opcode_SIMD_IntD;
4446
0
      else /* Use the next insn.  */
4447
0
  install_template (&t[1]);
4448
0
    }
4449
4450
  /* Use 2-byte VEX prefix by swapping commutative source operands if there
4451
     are no memory operands and at least 3 register ones.  */
4452
623
  if (i.reg_operands >= 3
4453
0
      && pp.encoding != encoding_vex3
4454
0
      && i.reg_operands == i.operands - i.imm_operands
4455
0
      && i.tm.opcode_modifier.vex
4456
0
      && i.tm.opcode_modifier.commutative
4457
      /* .commutative aliases .staticrounding; disambiguate.  */
4458
0
      && !i.tm.opcode_modifier.sae
4459
0
      && (i.tm.opcode_modifier.sse2avx
4460
0
    || (optimize > 1 && !pp.no_optimize))
4461
0
      && i.rex == REX_B
4462
0
      && i.vex.register_specifier
4463
0
      && !(i.vex.register_specifier->reg_flags & RegRex))
4464
0
    {
4465
0
      unsigned int xchg = i.operands - i.reg_operands;
4466
4467
0
      gas_assert (i.tm.opcode_space == SPACE_0F);
4468
0
      gas_assert (!i.tm.opcode_modifier.sae);
4469
0
      gas_assert (operand_type_equal (&i.types[i.operands - 2],
4470
0
                                      &i.types[i.operands - 3]));
4471
0
      gas_assert (i.rm.mode == 3);
4472
4473
0
      swap_2_operands (xchg, xchg + 1);
4474
4475
0
      i.rex = 0;
4476
0
      xchg = i.rm.regmem | 8;
4477
0
      i.rm.regmem = ~register_specifier & 0xf;
4478
0
      gas_assert (!(i.rm.regmem & 8));
4479
0
      i.vex.register_specifier += xchg - i.rm.regmem;
4480
0
      register_specifier = ~xchg & 0xf;
4481
0
    }
4482
4483
623
  if (i.tm.opcode_modifier.vex == VEXScalar)
4484
556
    vector_length = avxscalar;
4485
67
  else if (i.tm.opcode_modifier.vex == VEX256)
4486
16
    vector_length = 1;
4487
51
  else if (dot_insn () && i.tm.opcode_modifier.vex == VEX128)
4488
0
    vector_length = 0;
4489
51
  else
4490
51
    {
4491
51
      unsigned int op;
4492
4493
      /* Determine vector length from the last multi-length vector
4494
   operand.  */
4495
51
      vector_length = 0;
4496
153
      for (op = t->operands; op--;)
4497
102
  if (i.tm_types[op].bitfield.xmmword
4498
102
      && i.tm_types[op].bitfield.ymmword
4499
4
      && i.types[op].bitfield.ymmword)
4500
0
    {
4501
0
      vector_length = 1;
4502
0
      break;
4503
0
    }
4504
51
    }
4505
4506
  /* Check the REX.W bit and VEXW.  */
4507
623
  if (i.tm.opcode_modifier.vexw == VEXWIG)
4508
442
    w = vexwig == vexw1 || (i.rex & REX_W);
4509
181
  else if (i.tm.opcode_modifier.vexw && !(i.rex & REX_W))
4510
19
    w = i.tm.opcode_modifier.vexw == VEXW1;
4511
162
  else
4512
162
    w = flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1;
4513
4514
  /* Use 2-byte VEX prefix if possible.  */
4515
623
  if (w == 0
4516
604
      && pp.encoding != encoding_vex3
4517
604
      && i.tm.opcode_space == SPACE_0F
4518
51
      && (i.rex & (REX_W | REX_X | REX_B)) == 0)
4519
42
    {
4520
      /* 2-byte VEX prefix.  */
4521
42
      bool r;
4522
4523
42
      i.vex.length = 2;
4524
42
      i.vex.bytes[0] = 0xc5;
4525
4526
      /* Check the REX.R bit.  */
4527
42
      r = !(i.rex & REX_R);
4528
42
      i.vex.bytes[1] = (r << 7
4529
42
      | register_specifier << 3
4530
42
      | vector_length << 2
4531
42
      | i.tm.opcode_modifier.opcodeprefix);
4532
42
    }
4533
581
  else
4534
581
    {
4535
      /* 3-byte VEX prefix.  */
4536
581
      i.vex.length = 3;
4537
4538
581
      switch (i.tm.opcode_space)
4539
581
  {
4540
9
  case SPACE_0F:
4541
581
  case SPACE_0F38:
4542
581
  case SPACE_0F3A:
4543
581
  case SPACE_MAP5:
4544
581
  case SPACE_MAP7:
4545
581
    i.vex.bytes[0] = 0xc4;
4546
581
    break;
4547
0
  case SPACE_XOP08:
4548
0
  case SPACE_XOP09:
4549
0
  case SPACE_XOP0A:
4550
0
    i.vex.bytes[0] = 0x8f;
4551
0
    break;
4552
0
  default:
4553
0
    abort ();
4554
581
  }
4555
4556
      /* The high 3 bits of the second VEX byte are 1's compliment
4557
   of RXB bits from REX.  */
4558
581
      i.vex.bytes[1] = ((~i.rex & 7) << 5)
4559
581
           | (!dot_insn () ? i.tm.opcode_space
4560
581
               : i.insn_opcode_space);
4561
4562
581
      i.vex.bytes[2] = (w << 7
4563
581
      | register_specifier << 3
4564
581
      | vector_length << 2
4565
581
      | i.tm.opcode_modifier.opcodeprefix);
4566
581
    }
4567
623
}
4568
4569
static INLINE bool
4570
is_any_vex_encoding (const insn_template *t)
4571
21.1k
{
4572
21.1k
  return t->opcode_modifier.vex || t->opcode_modifier.evex;
4573
21.1k
}
4574
4575
/* We can use this function only when the current encoding is evex.  */
4576
static INLINE bool
4577
is_apx_evex_encoding (void)
4578
679
{
4579
679
  return (i.rex2 & REX_B) || i.tm.opcode_space == SPACE_MAP4 || pp.has_nf;
4580
679
}
4581
4582
static INLINE bool
4583
is_apx_rex2_encoding (void)
4584
13.0k
{
4585
13.0k
  return i.rex2 || pp.rex2_encoding
4586
13.0k
  || i.tm.opcode_modifier.rex2;
4587
13.0k
}
4588
4589
static unsigned int
4590
get_broadcast_bytes (const insn_template *t, bool diag)
4591
0
{
4592
0
  unsigned int op, bytes;
4593
0
  const i386_operand_type *t_types = get_operand_types (t);
4594
0
  const i386_operand_type *types;
4595
4596
0
  if (i.broadcast.type)
4597
0
    return (1 << (t->opcode_modifier.broadcast - 1)) * i.broadcast.type;
4598
4599
0
  gas_assert (intel_syntax);
4600
4601
0
  for (op = 0; op < t->operands; ++op)
4602
0
    if (t_types[op].bitfield.baseindex)
4603
0
      break;
4604
4605
0
  gas_assert (op < t->operands);
4606
4607
0
  if (t->opcode_modifier.evex != EVEXDYN)
4608
0
    switch (i.broadcast.bytes)
4609
0
      {
4610
0
      case 1:
4611
0
  if (t_types[op].bitfield.word)
4612
0
    return 2;
4613
      /* Fall through.  */
4614
0
      case 2:
4615
0
  if (t_types[op].bitfield.dword)
4616
0
    return 4;
4617
      /* Fall through.  */
4618
0
      case 4:
4619
0
  if (t_types[op].bitfield.qword)
4620
0
    return 8;
4621
      /* Fall through.  */
4622
0
      case 8:
4623
0
  if (t_types[op].bitfield.xmmword)
4624
0
    return 16;
4625
0
  if (t_types[op].bitfield.ymmword)
4626
0
    return 32;
4627
0
  if (t_types[op].bitfield.zmmword)
4628
0
    return 64;
4629
      /* Fall through.  */
4630
0
      default:
4631
0
        abort ();
4632
0
      }
4633
4634
0
  gas_assert (op + 1 < t->operands);
4635
4636
0
  if (t_types[op + 1].bitfield.xmmword
4637
0
      + t_types[op + 1].bitfield.ymmword
4638
0
      + t_types[op + 1].bitfield.zmmword > 1)
4639
0
    {
4640
0
      types = &i.types[op + 1];
4641
0
      diag = false;
4642
0
    }
4643
0
  else /* Ambiguous - guess with a preference to non-AVX512VL forms.  */
4644
0
    types = &t_types[op];
4645
4646
0
  if (types->bitfield.zmmword)
4647
0
    bytes = 64;
4648
0
  else if (types->bitfield.ymmword)
4649
0
    bytes = 32;
4650
0
  else
4651
0
    bytes = 16;
4652
4653
0
  if (diag)
4654
0
    as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
4655
0
       insn_name (t), bytes * 8);
4656
4657
0
  return bytes;
4658
0
}
4659
4660
/* Build the EVEX prefix (4-byte) for evex insn
4661
   | 62h |
4662
   | `R3 | `X3 | `B3 | `R4 | B4 | mmm |
4663
   | W | `vvvv | U | pp |
4664
   | z | L'L | b | `V4 | aaa |
4665
*/
4666
static void
4667
build_evex_prefix (void)
4668
56
{
4669
56
  unsigned int register_specifier;
4670
56
  bool w;
4671
56
  rex_byte vrex_used = 0;
4672
4673
  /* Check register specifier.  */
4674
56
  if (i.vex.register_specifier)
4675
32
    {
4676
32
      gas_assert ((i.vrex & REX_X) == 0);
4677
4678
32
      register_specifier = i.vex.register_specifier->reg_num;
4679
32
      if ((i.vex.register_specifier->reg_flags & RegRex))
4680
8
  register_specifier += 8;
4681
      /* The upper 16 registers are encoded in the fourth byte of the
4682
   EVEX prefix.  */
4683
32
      if (!(i.vex.register_specifier->reg_flags & (RegVRex | RegRex2)))
4684
32
  i.vex.bytes[3] = 0x8;
4685
32
      register_specifier = ~register_specifier & 0xf;
4686
32
    }
4687
24
  else
4688
24
    {
4689
24
      register_specifier = 0xf;
4690
4691
      /* Encode upper 16 vector index register in the fourth byte of
4692
   the EVEX prefix.  */
4693
24
      if (!(i.vrex & REX_X))
4694
24
  i.vex.bytes[3] = 0x8;
4695
0
      else
4696
0
  vrex_used |= REX_X;
4697
24
    }
4698
4699
  /* 4 byte EVEX prefix.  */
4700
56
  i.vex.length = 4;
4701
56
  i.vex.bytes[0] = 0x62;
4702
4703
  /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
4704
     bits from REX.  */
4705
56
  gas_assert (i.tm.opcode_space >= SPACE_0F);
4706
56
  gas_assert (i.tm.opcode_space <= SPACE_MAP7);
4707
56
  i.vex.bytes[1] = ((~i.rex & 7) << 5)
4708
56
       | (!dot_insn () ? i.tm.opcode_space
4709
56
           : i.insn_opcode_space);
4710
4711
  /* The fifth bit of the second EVEX byte is 1's compliment of the
4712
     REX_R bit in VREX.  */
4713
56
  if (!((i.vrex | i.rex2) & REX_R))
4714
56
    i.vex.bytes[1] |= 0x10;
4715
56
  vrex_used |= i.vrex & REX_R;
4716
4717
56
  if ((i.reg_operands + i.imm_operands) == i.operands)
4718
29
    {
4719
      /* When all operands are registers, the REX_X bit in REX is not
4720
   used.  We reuse it to encode the upper 16 registers, which is
4721
   indicated by the REX_B bit in VREX.  The REX_X bit is encoded
4722
   as 1's compliment.  */
4723
29
      if ((i.vrex & REX_B))
4724
2
  {
4725
2
    vrex_used |= REX_B;
4726
2
    i.vex.bytes[1] &= ~0x40;
4727
2
  }
4728
29
    }
4729
4730
  /* EVEX instructions shouldn't need the REX prefix.  */
4731
56
  i.vrex &= ~vrex_used;
4732
56
  gas_assert (i.vrex == 0);
4733
4734
  /* Check the REX.W bit and VEXW.  */
4735
56
  if (i.tm.opcode_modifier.vexw == VEXWIG)
4736
8
    w = evexwig == evexw1 || (i.rex & REX_W);
4737
48
  else if (i.tm.opcode_modifier.vexw && !(i.rex & REX_W))
4738
26
    w = i.tm.opcode_modifier.vexw == VEXW1;
4739
22
  else
4740
22
    w = flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1;
4741
4742
56
  if (i.tm.opcode_modifier.evex == EVEXDYN)
4743
2
    {
4744
2
      unsigned int op;
4745
4746
      /* Determine vector length from the last multi-length vector operand.  */
4747
2
      for (op = i.operands; op--;)
4748
2
  if (i.tm_types[op].bitfield.xmmword
4749
2
      + i.tm_types[op].bitfield.ymmword
4750
2
      + i.tm_types[op].bitfield.zmmword > 1)
4751
2
    {
4752
2
      if (i.types[op].bitfield.zmmword)
4753
0
        {
4754
0
    i.tm.opcode_modifier.evex = EVEX512;
4755
0
    break;
4756
0
        }
4757
2
      else if (i.types[op].bitfield.ymmword)
4758
0
        {
4759
0
    i.tm.opcode_modifier.evex = EVEX256;
4760
0
    break;
4761
0
        }
4762
2
      else if (i.types[op].bitfield.xmmword)
4763
2
        {
4764
2
    i.tm.opcode_modifier.evex = EVEX128;
4765
2
    break;
4766
2
        }
4767
0
      else if ((i.broadcast.type || i.broadcast.bytes)
4768
0
          && op == i.broadcast.operand)
4769
0
        {
4770
0
    switch (get_broadcast_bytes (&i.tm, true))
4771
0
      {
4772
0
        case 64:
4773
0
          i.tm.opcode_modifier.evex = EVEX512;
4774
0
          break;
4775
0
        case 32:
4776
0
          i.tm.opcode_modifier.evex = EVEX256;
4777
0
          break;
4778
0
        case 16:
4779
0
          i.tm.opcode_modifier.evex = EVEX128;
4780
0
          break;
4781
0
        default:
4782
0
          abort ();
4783
0
      }
4784
0
    break;
4785
0
        }
4786
2
    }
4787
4788
2
      if (op >= MAX_OPERANDS)
4789
0
  abort ();
4790
2
    }
4791
4792
  /* The third byte of the EVEX prefix.  */
4793
56
  i.vex.bytes[2] = ((w << 7)
4794
56
        | (register_specifier << 3)
4795
56
        | (i.rex2 & REX_X ? 0 : 4) /* Encode the U bit.  */
4796
56
        | i.tm.opcode_modifier.opcodeprefix);
4797
4798
  /* The fourth byte of the EVEX prefix.  */
4799
  /* The zeroing-masking bit.  */
4800
56
  if (i.mask.reg && i.mask.zeroing)
4801
0
    i.vex.bytes[3] |= 0x80;
4802
4803
  /* Don't always set the broadcast bit if there is no RC.  */
4804
56
  if (i.rounding.type == rc_none)
4805
56
    {
4806
      /* Encode the vector length.  */
4807
56
      unsigned int vec_length;
4808
4809
56
      switch (i.tm.opcode_modifier.evex)
4810
56
  {
4811
43
  case EVEXLIG: /* LL' is ignored */
4812
43
    vec_length = evexlig << 5;
4813
43
    break;
4814
5
  case EVEX128:
4815
5
    vec_length = 0 << 5;
4816
5
    break;
4817
8
  case EVEX256:
4818
8
    vec_length = 1 << 5;
4819
8
    break;
4820
0
  case EVEX512:
4821
0
    vec_length = 2 << 5;
4822
0
    break;
4823
0
  case EVEX_L3:
4824
0
    if (dot_insn ())
4825
0
      {
4826
0
        vec_length = 3 << 5;
4827
0
        break;
4828
0
      }
4829
    /* Fall through.  */
4830
0
  default:
4831
0
    abort ();
4832
0
    break;
4833
56
  }
4834
56
      i.vex.bytes[3] |= vec_length;
4835
      /* Encode the broadcast bit.  */
4836
56
      if (i.broadcast.type || i.broadcast.bytes)
4837
0
  i.vex.bytes[3] |= 0x10;
4838
56
    }
4839
0
  else if (i.rounding.type != saeonly)
4840
0
    i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
4841
0
  else
4842
0
    i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
4843
4844
56
  if (i.mask.reg)
4845
0
    i.vex.bytes[3] |= i.mask.reg->reg_num;
4846
56
}
4847
4848
/* Build (2 bytes) rex2 prefix.
4849
   | D5h |
4850
   | m | R4 X4 B4 | W R3 X3 B3 |
4851
4852
   Rex2 reuses i.vex as they both encode i.tm.opcode_space in their prefixes.
4853
 */
4854
static void
4855
build_rex2_prefix (void)
4856
4
{
4857
4
  i.vex.length = 2;
4858
4
  i.vex.bytes[0] = 0xd5;
4859
  /* For the W R X B bits, the variables of rex prefix will be reused.  */
4860
4
  i.vex.bytes[1] = ((i.tm.opcode_space << 7)
4861
4
        | (i.rex2 << 4)
4862
4
        | ((i.rex | i.prefix[REX_PREFIX]) & 0xf));
4863
4
}
4864
4865
/* Build the EVEX prefix (4-byte) for APX insn.  Apart from the basic form
4866
   (see build_evex_prefix()) there are two new forms:
4867
4868
   | 62h |
4869
   | `R3 | `X3 | `B3 | `R4 | B4 | mmm |
4870
   | W | `vvvv | `X4 | pp |
4871
   | 00 | L | ND | `V4 | NF | 00 |
4872
4873
   and for NCI:
4874
4875
   | 62h |
4876
   | `R3 | `X3 | `B3 | `R4 | B4 | mmm |
4877
   | W | OSZC | `X4 | pp |
4878
   | 0000 | SC3 SC2 SC1 SC0 |
4879
*/
4880
static bool
4881
build_apx_evex_prefix (bool force_nd)
4882
3
{
4883
  /* To mimic behavior for legacy insns, transform use of DATA16 and REX64 into
4884
     their embedded-prefix representations.  */
4885
3
  if (i.tm.opcode_space == SPACE_MAP4)
4886
3
    {
4887
3
      if (i.prefix[DATA_PREFIX])
4888
0
  {
4889
0
    if (i.tm.opcode_modifier.opcodeprefix)
4890
0
      {
4891
0
        as_bad (i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66
4892
0
          ? _("same type of prefix used twice")
4893
0
          : _("conflicting use of `data16' prefix"));
4894
0
        return false;
4895
0
      }
4896
0
    i.tm.opcode_modifier.opcodeprefix = PREFIX_0X66;
4897
0
    i.prefix[DATA_PREFIX] = 0;
4898
0
  }
4899
3
      if (i.prefix[REX_PREFIX] & REX_W)
4900
0
  {
4901
0
    if (i.suffix == QWORD_MNEM_SUFFIX)
4902
0
      {
4903
0
        as_bad (_("same type of prefix used twice"));
4904
0
        return false;
4905
0
      }
4906
0
    i.tm.opcode_modifier.vexw = VEXW1;
4907
0
    i.prefix[REX_PREFIX] = 0;
4908
0
  }
4909
3
    }
4910
4911
3
  build_evex_prefix ();
4912
3
  if (i.rex2 & REX_B)
4913
0
    i.vex.bytes[1] |= 0x08;
4914
4915
  /* Encode the ND bit of instructions promoted from legacy space.
4916
     ZU shares the bit with ND.  */
4917
3
  if ((i.vex.register_specifier && i.tm.opcode_space == SPACE_MAP4)
4918
3
      || i.tm.opcode_modifier.operandconstraint == ZERO_UPPER
4919
3
      || force_nd)
4920
0
    {
4921
      /* Incoming ND and aaa bits should be 0.  */
4922
0
      know (!(i.vex.bytes[3] & 0x17));
4923
4924
0
      i.vex.bytes[3] |= 0x10;
4925
0
    }
4926
4927
  /* Encode SCC and oszc flags bits.  */
4928
3
  if (i.tm.opcode_modifier.operandconstraint == SCC)
4929
3
    {
4930
      /* Incoming ND and aaa bits should (still) be 0.  */
4931
3
      know (!(i.vex.bytes[3] & 0x17));
4932
4933
      /* The incoming value of vvvv is 1111, i.e. bits may need clearing.  */
4934
3
      i.vex.bytes[2] &= (i.oszc_flags << 3) | 0x87;
4935
      /* The incoming value of V4 is 1 and needs to be cleared.  */
4936
3
      i.vex.bytes[3] = (i.vex.bytes[3] & ~0x08) | i.scc;
4937
3
    }
4938
4939
  /* Encode the NF bit.  */
4940
3
  if (pp.has_nf || i.tm.opcode_modifier.operandconstraint == EVEX_NF)
4941
0
    {
4942
      /* Incoming aaa bits should (still) be 0.  */
4943
0
      know (!(i.vex.bytes[3] & 7));
4944
4945
0
      i.vex.bytes[3] |= 0x04;
4946
0
    }
4947
4948
3
  return true;
4949
3
}
4950
4951
static void establish_rex (void)
4952
6.62k
{
4953
  /* Note that legacy encodings have at most 2 non-immediate operands.  */
4954
6.62k
  unsigned int first = i.imm_operands;
4955
6.62k
  unsigned int last = i.operands > first ? i.operands - first - 1 : first;
4956
4957
  /* Respect a user-specified REX prefix.  */
4958
6.62k
  i.rex |= i.prefix[REX_PREFIX] & REX_OPCODE;
4959
4960
  /* For 8 bit RegRex64 registers without a prefix, we need an empty rex prefix.  */
4961
6.62k
  if (((i.types[first].bitfield.class == Reg
4962
416
  && (i.op[first].regs->reg_flags & RegRex64) != 0)
4963
6.61k
       || (i.types[last].bitfield.class == Reg
4964
167
     && (i.op[last].regs->reg_flags & RegRex64) != 0))
4965
10
      && !is_apx_rex2_encoding () && !is_any_vex_encoding (&i.tm))
4966
10
    i.rex |= REX_OPCODE;
4967
4968
  /* For REX/REX2/EVEX prefix instructions, we need to convert old registers
4969
     (AL, CL, DL and BL) to new ones (AXL, CXL, DXL and BXL) and reject AH,
4970
     CH, DH and BH.  */
4971
6.62k
  if (i.rex || i.rex2 || i.tm.opcode_modifier.evex)
4972
57
    {
4973
117
      for (unsigned int x = first; x <= last; x++)
4974
60
  {
4975
    /* Look for 8 bit operand that uses old registers.  */
4976
60
    if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
4977
3
        && !(i.op[x].regs->reg_flags & (RegRex | RegRex2 | RegRex64)))
4978
3
      {
4979
        /* In case it is "hi" register, give up.  */
4980
3
        if (i.op[x].regs->reg_num > 3)
4981
2
    as_bad (_("can't encode register '%s%s' in an "
4982
2
        "instruction requiring %s prefix"),
4983
2
      register_prefix, i.op[x].regs->reg_name,
4984
2
      i.tm.opcode_modifier.evex ? "EVEX" : "REX/REX2");
4985
4986
        /* Otherwise it is equivalent to the extended register.
4987
     Since the encoding doesn't change this is merely
4988
     cosmetic cleanup for debug output.  */
4989
3
        i.op[x].regs += 8;
4990
3
      }
4991
60
  }
4992
57
    }
4993
4994
6.62k
  if (i.rex == 0 && i.rex2 == 0 && (pp.rex_encoding || pp.rex2_encoding))
4995
2
    {
4996
      /* Check if we can add a REX_OPCODE byte.  Look for 8 bit operand
4997
   that uses legacy register.  If it is "hi" register, don't add
4998
   rex and rex2 prefix.  */
4999
2
      unsigned int x;
5000
5001
4
      for (x = first; x <= last; x++)
5002
2
  if (i.types[x].bitfield.class == Reg
5003
0
      && i.types[x].bitfield.byte
5004
0
      && !(i.op[x].regs->reg_flags & (RegRex | RegRex2 | RegRex64))
5005
0
      && i.op[x].regs->reg_num > 3)
5006
0
    {
5007
0
      pp.rex_encoding = false;
5008
0
      pp.rex2_encoding = false;
5009
0
      break;
5010
0
    }
5011
5012
2
      if (pp.rex_encoding)
5013
2
  i.rex = REX_OPCODE;
5014
2
    }
5015
5016
6.62k
  if (is_apx_rex2_encoding ())
5017
4
    {
5018
      /* Most prefixes are not permitted with JMPABS.  */
5019
4
      if (i.tm.mnem_off == MN_jmpabs)
5020
4
  {
5021
4
    if (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
5022
0
      {
5023
0
        as_bad (_("size override not allowed with `%s'"),
5024
0
          insn_name (&i.tm));
5025
0
        i.prefix[DATA_PREFIX] = 0;
5026
0
        i.prefix[REX_PREFIX] &= ~REX_W;
5027
0
      }
5028
4
    if (i.prefix[ADDR_PREFIX])
5029
0
      {
5030
0
        as_bad (_("address override not allowed with `%s'"),
5031
0
          insn_name (&i.tm));
5032
0
        i.prefix[ADDR_PREFIX] = 0;
5033
0
      }
5034
4
  }
5035
5036
4
      build_rex2_prefix ();
5037
      /* The individual REX.RXBW bits got consumed.  */
5038
4
      i.rex &= REX_OPCODE;
5039
4
      i.prefix[REX_PREFIX] = 0;
5040
4
    }
5041
6.62k
  else if (i.rex != 0)
5042
54
    add_prefix (REX_OPCODE | i.rex);
5043
6.62k
}
5044
5045
static void
5046
process_immext (void)
5047
0
{
5048
0
  expressionS *exp;
5049
5050
  /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
5051
     which is coded in the same place as an 8-bit immediate field
5052
     would be.  Here we fake an 8-bit immediate operand from the
5053
     opcode suffix stored in tm.extension_opcode.
5054
5055
     AVX instructions also use this encoding, for some of
5056
     3 argument instructions.  */
5057
5058
0
  gas_assert (i.imm_operands <= 1
5059
0
        && (i.operands <= 2
5060
0
      || (is_any_vex_encoding (&i.tm)
5061
0
          && i.operands <= 4)));
5062
5063
0
  exp = &im_expressions[i.imm_operands++];
5064
0
  i.op[i.operands].imms = exp;
5065
0
  i.types[i.operands].bitfield.imm8 = 1;
5066
0
  i.operands++;
5067
0
  exp->X_op = O_constant;
5068
0
  exp->X_add_number = i.tm.extension_opcode;
5069
0
  i.tm.extension_opcode = None;
5070
0
}
5071
5072
5073
static int
5074
check_hle (void)
5075
0
{
5076
0
  switch (i.tm.opcode_modifier.prefixok)
5077
0
    {
5078
0
    default:
5079
0
      as_bad (_("invalid instruction `%s' after `%s'"),
5080
0
        insn_name (&i.tm), i.hle_prefix);
5081
0
      return 0;
5082
0
    case PrefixHLELock:
5083
0
      if (i.prefix[LOCK_PREFIX])
5084
0
  return 1;
5085
0
      as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
5086
0
      return 0;
5087
0
    case PrefixHLEAny:
5088
0
      return 1;
5089
0
    case PrefixHLERelease:
5090
0
      if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
5091
0
  {
5092
0
    as_bad (_("instruction `%s' after `xacquire' not allowed"),
5093
0
      insn_name (&i.tm));
5094
0
    return 0;
5095
0
  }
5096
0
      if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
5097
0
  {
5098
0
    as_bad (_("memory destination needed for instruction `%s'"
5099
0
        " after `xrelease'"), insn_name (&i.tm));
5100
0
    return 0;
5101
0
  }
5102
0
      return 1;
5103
0
    }
5104
0
}
5105
5106
/* Helper for optimization (running ahead of process_suffix()), to make sure we
5107
   convert only well-formed insns.  @OP is the sized operand to cross check
5108
   against (typically a register).  Checking against a single operand typically
5109
   suffices, as match_template() has already honored CheckOperandSize.  */
5110
5111
static bool is_plausible_suffix (unsigned int op)
5112
0
{
5113
0
  return !i.suffix
5114
0
   || (i.suffix == BYTE_MNEM_SUFFIX && i.types[op].bitfield.byte)
5115
0
   || (i.suffix == WORD_MNEM_SUFFIX && i.types[op].bitfield.word)
5116
0
   || (i.suffix == LONG_MNEM_SUFFIX && i.types[op].bitfield.dword)
5117
0
   || (i.suffix == QWORD_MNEM_SUFFIX && i.types[op].bitfield.qword);
5118
0
}
5119
5120
/* Encode aligned vector move as unaligned vector move.  */
5121
5122
static void
5123
encode_with_unaligned_vector_move (void)
5124
0
{
5125
0
  switch (i.tm.base_opcode)
5126
0
    {
5127
0
    case 0x28:  /* Load instructions.  */
5128
0
    case 0x29:  /* Store instructions.  */
5129
      /* movaps/movapd/vmovaps/vmovapd.  */
5130
0
      if (i.tm.opcode_space == SPACE_0F
5131
0
    && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
5132
0
  i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
5133
0
      break;
5134
0
    case 0x6f:  /* Load instructions.  */
5135
0
    case 0x7f:  /* Store instructions.  */
5136
      /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
5137
0
      if (i.tm.opcode_space == SPACE_0F
5138
0
    && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
5139
0
  i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
5140
0
      break;
5141
0
    default:
5142
0
      break;
5143
0
    }
5144
0
}
5145
5146
/* Try the shortest encoding by shortening operand size.  */
5147
5148
static void
5149
optimize_encoding (void)
5150
0
{
5151
0
  unsigned int j;
5152
5153
0
  if (i.tm.mnem_off == MN_lea)
5154
0
    {
5155
      /* Optimize: -O:
5156
     lea symbol, %rN    -> mov $symbol, %rN
5157
     lea (%rM), %rN     -> mov %rM, %rN
5158
     lea (,%rM,1), %rN  -> mov %rM, %rN
5159
5160
     and in 32-bit mode for 16-bit addressing
5161
5162
     lea (%rM), %rN     -> movzx %rM, %rN
5163
5164
     and in 64-bit mode zap 32-bit addressing in favor of using a
5165
     32-bit (or less) destination.
5166
       */
5167
0
      if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5168
0
  {
5169
0
    if (!i.types[1].bitfield.word)
5170
0
      i.tm.opcode_modifier.size = SIZE32;
5171
0
    i.prefix[ADDR_PREFIX] = 0;
5172
0
  }
5173
5174
0
      if (!i.index_reg && !i.base_reg)
5175
0
  {
5176
    /* Handle:
5177
         lea symbol, %rN    -> mov $symbol, %rN
5178
     */
5179
0
    if (flag_code == CODE_64BIT)
5180
0
      {
5181
        /* Don't transform a relocation to a 16-bit one.  */
5182
0
        if (i.op[0].disps
5183
0
      && i.op[0].disps->X_op != O_constant
5184
0
      && i.types[1].bitfield.word)
5185
0
    return;
5186
5187
0
        if (!i.types[1].bitfield.qword
5188
0
      || i.tm.opcode_modifier.size == SIZE32)
5189
0
    {
5190
0
      i.tm.base_opcode = 0xb8;
5191
0
      i.tm.opcode_modifier.modrm = 0;
5192
0
      if (!i.types[1].bitfield.word)
5193
0
        i.types[0].bitfield.imm32 = 1;
5194
0
      else
5195
0
        {
5196
0
          i.tm.opcode_modifier.size = SIZE16;
5197
0
          i.types[0].bitfield.imm16 = 1;
5198
0
        }
5199
0
    }
5200
0
        else
5201
0
    {
5202
      /* Subject to further optimization below.  */
5203
0
      i.tm.base_opcode = 0xc7;
5204
0
      i.tm.extension_opcode = 0;
5205
0
      i.types[0].bitfield.imm32s = 1;
5206
0
      i.types[0].bitfield.baseindex = 0;
5207
0
    }
5208
0
      }
5209
    /* Outside of 64-bit mode address and operand sizes have to match if
5210
       a relocation is involved, as otherwise we wouldn't (currently) or
5211
       even couldn't express the relocation correctly.  */
5212
0
    else if (i.op[0].disps
5213
0
       && i.op[0].disps->X_op != O_constant
5214
0
       && ((!i.prefix[ADDR_PREFIX])
5215
0
           != (flag_code == CODE_32BIT
5216
0
         ? i.types[1].bitfield.dword
5217
0
         : i.types[1].bitfield.word)))
5218
0
      return;
5219
    /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
5220
       destination is going to grow encoding size.  */
5221
0
    else if (flag_code == CODE_16BIT
5222
0
       && (optimize <= 1 || optimize_for_space)
5223
0
       && !i.prefix[ADDR_PREFIX]
5224
0
       && i.types[1].bitfield.dword)
5225
0
      return;
5226
0
    else
5227
0
      {
5228
0
        i.tm.base_opcode = 0xb8;
5229
0
        i.tm.opcode_modifier.modrm = 0;
5230
0
        if (i.types[1].bitfield.dword)
5231
0
    i.types[0].bitfield.imm32 = 1;
5232
0
        else
5233
0
    i.types[0].bitfield.imm16 = 1;
5234
5235
0
        if (i.op[0].disps
5236
0
      && i.op[0].disps->X_op == O_constant
5237
0
      && i.types[1].bitfield.dword
5238
      /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
5239
         GCC 5. */
5240
0
      && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
5241
0
    i.op[0].disps->X_add_number &= 0xffff;
5242
0
      }
5243
5244
0
    i.tm_types[0] = i.types[0];
5245
0
    i.imm_operands = 1;
5246
0
    if (!i.op[0].imms)
5247
0
      {
5248
0
        i.op[0].imms = &im_expressions[0];
5249
0
        i.op[0].imms->X_op = O_absent;
5250
0
      }
5251
0
  }
5252
0
      else if (i.op[0].disps
5253
0
      && (i.op[0].disps->X_op != O_constant
5254
0
          || i.op[0].disps->X_add_number))
5255
0
  return;
5256
0
      else
5257
0
  {
5258
    /* Handle:
5259
         lea (%rM), %rN     -> mov %rM, %rN
5260
         lea (,%rM,1), %rN  -> mov %rM, %rN
5261
         lea (%rM), %rN     -> movzx %rM, %rN
5262
     */
5263
0
    const reg_entry *addr_reg;
5264
5265
0
    if (!i.index_reg && i.base_reg->reg_num != RegIP)
5266
0
      addr_reg = i.base_reg;
5267
0
    else if (!i.base_reg
5268
0
       && i.index_reg->reg_num != RegIZ
5269
0
       && !i.log2_scale_factor)
5270
0
      addr_reg = i.index_reg;
5271
0
    else
5272
0
      return;
5273
5274
0
    if (addr_reg->reg_type.bitfield.word
5275
0
        && i.types[1].bitfield.dword)
5276
0
      {
5277
0
        if (flag_code != CODE_32BIT)
5278
0
    return;
5279
0
        i.tm.opcode_space = SPACE_0F;
5280
0
        i.tm.base_opcode = 0xb7;
5281
0
      }
5282
0
    else
5283
0
      i.tm.base_opcode = 0x8b;
5284
5285
0
    if (addr_reg->reg_type.bitfield.dword
5286
0
        && i.types[1].bitfield.qword)
5287
0
      i.tm.opcode_modifier.size = SIZE32;
5288
5289
0
    i.op[0].regs = addr_reg;
5290
0
    i.reg_operands = 2;
5291
0
  }
5292
5293
0
      i.mem_operands = 0;
5294
0
      i.disp_operands = 0;
5295
0
      i.prefix[ADDR_PREFIX] = 0;
5296
0
      i.prefix[SEG_PREFIX] = 0;
5297
0
      i.seg[0] = NULL;
5298
0
    }
5299
5300
0
  if (optimize_for_disabled_optimizations
5301
0
      && i.tm.mnem_off == MN_xchg
5302
0
      && i.reg_operands == 2
5303
0
      && i.op[0].regs == i.op[1].regs)
5304
0
    {
5305
      /* Optimize: -O:
5306
     xchg %rN, %rN     -> mov %rN, %rN
5307
       */
5308
0
      i.tm.base_opcode = pp.dir_encoding == dir_encoding_load ? 0x8a : 0x88;
5309
0
    }
5310
5311
0
  if (i.tm.mnem_off == MN_xadd
5312
0
      && i.reg_operands == 2
5313
0
      && i.op[0].regs == i.op[1].regs)
5314
0
    {
5315
      /* Optimize: -O:
5316
     xadd %rN, %rN     -> add %rN, %rN
5317
       */
5318
0
      i.tm.opcode_space = SPACE_BASE;
5319
0
      i.tm.base_opcode = 0x00;
5320
0
    }
5321
5322
0
  if (((i.tm.opcode_space == SPACE_0F
5323
0
        && (i.tm.base_opcode | 1) == 0xbf
5324
0
        && (i.types[0].bitfield.byte
5325
0
      ? i.types[1].bitfield.word
5326
0
      : i.types[1].bitfield.dword))
5327
0
       || (i.tm.opcode_space == SPACE_BASE
5328
0
     && i.tm.base_opcode == 0x63
5329
0
     && i.types[1].bitfield.qword))
5330
0
      && i.reg_operands == 2
5331
0
      && i.op[0].regs->reg_type.bitfield.instance == Accum
5332
0
      && i.op[1].regs->reg_type.bitfield.instance == Accum
5333
0
      && (cpu_arch_tune != PROCESSOR_K6 || optimize_for_space))
5334
0
    {
5335
      /* Optimize: -O:
5336
     movsb     %al, %ax    -> cbw
5337
     movsw     %ax, %eax   -> cwde
5338
     movsl     %eax, %rax  -> cdqe
5339
       */
5340
0
      i.tm.opcode_space = SPACE_BASE;
5341
0
      i.tm.base_opcode = 0x98;
5342
0
      i.tm.opcode_modifier.modrm = 0;
5343
      /* Leave the destination register in place for process_suffix() to take
5344
   care of operand sizing.  This will end up as short_form encoding,
5345
   with the register number being 0 (i.e. not altering the opcode).  */
5346
0
      i.reg_operands = 1;
5347
0
      i.op[0].regs = i.op[1].regs;
5348
0
      i.tm_types[1].bitfield.class = ClassNone;
5349
0
      return;
5350
0
    }
5351
5352
0
  if (optimize_for_space
5353
0
      && i.tm.opcode_space == SPACE_0F
5354
0
      && (i.tm.base_opcode | 1) == 0xb7
5355
0
      && i.reg_operands == 2
5356
0
      && !i.op[0].regs->reg_flags
5357
0
      && !i.op[1].regs->reg_flags
5358
0
      && (i.types[0].bitfield.byte
5359
0
    ? i.types[1].bitfield.word
5360
0
      && i.op[0].regs->reg_num < 4
5361
0
      && i.op[1].regs->reg_num == i.op[0].regs->reg_num
5362
0
      && (!i.suffix || i.suffix == WORD_MNEM_SUFFIX)
5363
0
    : i.types[1].bitfield.dword
5364
0
      && flag_code == CODE_16BIT
5365
0
      && i.op[0].regs->reg_type.bitfield.baseindex
5366
0
      && i.op[0].regs->reg_num != EBP_REG_NUM))
5367
0
    {
5368
      /* Optimize: -Os:
5369
     movzb     %r8, %r16    -> mov $0, %r8h
5370
5371
     %r8 being one of %al, %cl, %dl, or %bl, with %r16 being the
5372
     matching 16-bit reg.
5373
       */
5374
5375
0
      i.tm.opcode_space = SPACE_BASE;
5376
0
      i.tm.opcode_modifier.w = 0;
5377
0
      i.reg_operands = 1;
5378
0
      if (i.types[0].bitfield.byte)
5379
0
  {
5380
0
    i.tm.base_opcode = 0xb0;
5381
0
    i.tm.opcode_modifier.modrm = 0;
5382
0
    copy_operand (1, 0);
5383
0
    i.op[1].regs += 4;
5384
5385
0
    im_expressions[0].X_op = O_constant;
5386
0
    im_expressions[0].X_add_number = 0;
5387
0
    i.op[0].imms = &im_expressions[0];
5388
0
    operand_type_set (&i.types[0], 0);
5389
0
    i.types[0].bitfield.imm8 = 1;
5390
0
    i.tm_types[0] = i.types[0];
5391
0
    i.tm_types[0].bitfield.class = ClassNone;
5392
0
    i.imm_operands = 1;
5393
5394
0
    i.suffix = 0;
5395
0
    return;
5396
0
  }
5397
5398
      /* In 16-bit mode, optimize: -Os:
5399
     movzw     %r16, %r32   -> lea (%r16), %r32
5400
5401
     %r16 being one of %bx, %si, or %di.
5402
       */
5403
0
      i.tm.base_opcode = 0x8d;
5404
5405
0
      i.base_reg = i.op[0].regs;
5406
0
      operand_type_set (&i.types[0], 0);
5407
0
      i.types[0].bitfield.baseindex = 1;
5408
0
      i.tm_types[0] = i.types[0];
5409
0
      i.op[0].disps = NULL;
5410
0
      i.flags[0] = Operand_Mem;
5411
0
      i.mem_operands = 1;
5412
0
      return;
5413
0
    }
5414
5415
0
  if (optimize_for_space
5416
0
      && (i.tm.mnem_off == MN_test
5417
0
          || (i.tm.base_opcode == 0xf6
5418
0
              && i.tm.opcode_space == SPACE_MAP4))
5419
0
      && i.reg_operands == 1
5420
0
      && i.imm_operands == 1
5421
0
      && !i.types[1].bitfield.byte
5422
0
      && is_plausible_suffix (1)
5423
0
      && i.op[0].imms->X_op == O_constant
5424
0
      && fits_in_imm7 (i.op[0].imms->X_add_number))
5425
0
    {
5426
      /* Optimize: -Os:
5427
     test      $imm7, %r64/%r32/%r16  -> test      $imm7, %r8
5428
     ctest<cc> $imm7, %r64/%r32/%r16  -> ctest<cc> $imm7, %r8
5429
       */
5430
0
      unsigned int base_regnum = i.op[1].regs->reg_num;
5431
5432
0
      gas_assert (!i.tm.opcode_modifier.modrm || i.tm.extension_opcode == 0);
5433
5434
0
      if (flag_code == CODE_64BIT || base_regnum < 4)
5435
0
  {
5436
0
    i.types[1].bitfield.byte = 1;
5437
    /* Squash the suffix.  */
5438
0
    i.suffix = 0;
5439
    /* Convert to byte registers. 8-bit registers are special,
5440
       RegRex64 and non-RegRex* each have 8 registers.  */
5441
0
    if (i.types[1].bitfield.word)
5442
      /* 32 (or 40) 8-bit registers.  */
5443
0
      j = 32;
5444
0
    else if (i.types[1].bitfield.dword)
5445
      /* 32 (or 40) 8-bit registers + 32 16-bit registers.  */
5446
0
      j = 64;
5447
0
    else
5448
      /* 32 (or 40) 8-bit registers + 32 16-bit registers
5449
         + 32 32-bit registers.  */
5450
0
      j = 96;
5451
5452
    /* In 64-bit mode, the following byte registers cannot be accessed
5453
       if using the Rex and Rex2 prefix: AH, BH, CH, DH */
5454
0
    if (!(i.op[1].regs->reg_flags & (RegRex | RegRex2)) && base_regnum < 4)
5455
0
      j += 8;
5456
0
    i.op[1].regs -= j;
5457
0
  }
5458
0
    }
5459
0
  else if (flag_code == CODE_64BIT
5460
0
     && i.tm.opcode_space == SPACE_BASE
5461
0
     && i.types[i.operands - 1].bitfield.qword
5462
0
     && ((i.reg_operands == 1
5463
0
    && i.imm_operands == 1
5464
0
    && i.op[0].imms->X_op == O_constant
5465
0
    && ((i.tm.base_opcode == 0xb8
5466
0
         && i.tm.extension_opcode == None
5467
0
         && fits_in_unsigned_long (i.op[0].imms->X_add_number))
5468
0
        || (fits_in_imm31 (i.op[0].imms->X_add_number)
5469
0
      && (i.tm.base_opcode == 0x24
5470
0
          || (((i.tm.base_opcode == 0x80
5471
0
          && i.tm.extension_opcode == 0x4)
5472
0
         || i.tm.mnem_off == MN_test)
5473
0
        && !(i.op[1].regs->reg_flags
5474
0
             & (RegRex | RegRex2)))
5475
0
          || ((i.tm.base_opcode | 1) == 0xc7
5476
0
        && i.tm.extension_opcode == 0x0)))
5477
0
        || (fits_in_imm7 (i.op[0].imms->X_add_number)
5478
0
      && i.tm.base_opcode == 0x83
5479
0
      && i.tm.extension_opcode == 0x4
5480
0
      && !(i.op[1].regs->reg_flags & (RegRex | RegRex2)))))
5481
0
         || ((i.reg_operands == 2
5482
0
        && i.op[0].regs == i.op[1].regs
5483
0
        && (i.tm.mnem_off == MN_xor
5484
0
      || i.tm.mnem_off == MN_sub))
5485
0
       || i.tm.mnem_off == MN_clr)))
5486
0
    {
5487
      /* Optimize: -O:
5488
     andq $imm31, %r64   -> andl $imm31, %r32
5489
     andq $imm7, %r64    -> andl $imm7, %r32
5490
     testq $imm31, %r64  -> testl $imm31, %r32
5491
     xorq %r64, %r64     -> xorl %r32, %r32
5492
     clrq %r64           -> clrl %r32
5493
     subq %r64, %r64     -> subl %r32, %r32
5494
     movq $imm31, %r64   -> movl $imm31, %r32
5495
     movq $imm32, %r64   -> movl $imm32, %r32
5496
        */
5497
0
      i.tm.opcode_modifier.size = SIZE32;
5498
0
      if (i.imm_operands)
5499
0
  {
5500
0
    i.types[0].bitfield.imm32 = 1;
5501
0
    i.types[0].bitfield.imm32s = 0;
5502
0
    i.types[0].bitfield.imm64 = 0;
5503
0
  }
5504
0
      else
5505
0
  {
5506
0
    i.types[0].bitfield.dword = 1;
5507
0
    i.types[0].bitfield.qword = 0;
5508
0
  }
5509
0
      i.types[1].bitfield.dword = 1;
5510
0
      i.types[1].bitfield.qword = 0;
5511
0
      if (i.tm.mnem_off == MN_mov || i.tm.mnem_off == MN_lea)
5512
0
  {
5513
    /* Handle
5514
         movq $imm31, %r64   -> movl $imm31, %r32
5515
         movq $imm32, %r64   -> movl $imm32, %r32
5516
     */
5517
0
    i.tm_types[0].bitfield.imm32 = 1;
5518
0
    i.tm_types[0].bitfield.imm32s = 0;
5519
0
    i.tm_types[0].bitfield.imm64 = 0;
5520
0
    if ((i.tm.base_opcode | 1) == 0xc7)
5521
0
      {
5522
        /* Handle
5523
       movq $imm31, %r64   -> movl $imm31, %r32
5524
         */
5525
0
        i.tm.base_opcode = 0xb8;
5526
0
        i.tm.extension_opcode = None;
5527
0
        i.tm.opcode_modifier.w = 0;
5528
0
        i.tm.opcode_modifier.modrm = 0;
5529
0
      }
5530
0
  }
5531
0
    }
5532
0
  else if (i.reg_operands == 3
5533
0
     && i.op[0].regs == i.op[1].regs
5534
0
     && pp.encoding != encoding_evex
5535
0
     && (i.tm.mnem_off == MN_xor
5536
0
         || i.tm.mnem_off == MN_sub))
5537
0
    {
5538
      /* Optimize: -O:
5539
     xorb %rNb, %rNb, %rMb  -> xorl %rMd, %rMd
5540
     xorw %rNw, %rNw, %rMw  -> xorl %rMd, %rMd
5541
     xorl %rNd, %rNd, %rMd  -> xorl %rMd, %rMd
5542
     xorq %rN,  %rN,  %rM   -> xorl %rMd, %rMd
5543
     subb %rNb, %rNb, %rMb  -> subl %rMd, %rMd
5544
     subw %rNw, %rNw, %rMw  -> subl %rMd, %rMd
5545
     subl %rNd, %rNd, %rMd  -> subl %rMd, %rMd
5546
     subq %rN,  %rN,  %rM   -> subl %rMd, %rMd
5547
        */
5548
0
      i.tm.opcode_space = SPACE_BASE;
5549
0
      i.tm.opcode_modifier.evex = 0;
5550
0
      i.tm.opcode_modifier.size = SIZE32;
5551
0
      i.types[0].bitfield.byte = 0;
5552
0
      i.types[0].bitfield.word = 0;
5553
0
      i.types[0].bitfield.dword = 1;
5554
0
      i.types[0].bitfield.qword = 0;
5555
0
      i.op[0].regs = i.op[2].regs;
5556
0
      i.types[1] = i.types[0];
5557
0
      i.op[1].regs = i.op[2].regs;
5558
0
      i.reg_operands = 2;
5559
0
    }
5560
0
  else if (optimize > 1
5561
0
     && !optimize_for_space
5562
0
     && i.reg_operands == 2
5563
0
     && i.op[0].regs == i.op[1].regs
5564
0
     && (i.tm.mnem_off == MN_and || i.tm.mnem_off == MN_or)
5565
0
     && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
5566
0
    {
5567
      /* Optimize: -O2:
5568
     andb %rN, %rN  -> testb %rN, %rN
5569
     andw %rN, %rN  -> testw %rN, %rN
5570
     andq %rN, %rN  -> testq %rN, %rN
5571
     orb %rN, %rN   -> testb %rN, %rN
5572
     orw %rN, %rN   -> testw %rN, %rN
5573
     orq %rN, %rN   -> testq %rN, %rN
5574
5575
     and outside of 64-bit mode
5576
5577
     andl %rN, %rN  -> testl %rN, %rN
5578
     orl %rN, %rN   -> testl %rN, %rN
5579
       */
5580
0
      i.tm.base_opcode = 0x84;
5581
0
    }
5582
0
  else if (!optimize_for_space
5583
0
     && i.tm.base_opcode == 0xd0
5584
0
     && i.tm.extension_opcode == 4
5585
0
     && (i.tm.opcode_space == SPACE_BASE
5586
0
         || i.tm.opcode_space == SPACE_MAP4)
5587
0
     && !i.mem_operands)
5588
0
    {
5589
      /* Optimize: -O:
5590
     shlb $1, %rN  -> addb %rN, %rN
5591
     shlw $1, %rN  -> addw %rN, %rN
5592
     shll $1, %rN  -> addl %rN, %rN
5593
     shlq $1, %rN  -> addq %rN, %rN
5594
5595
     shlb $1, %rN, %rM  -> addb %rN, %rN, %rM
5596
     shlw $1, %rN, %rM  -> addw %rN, %rN, %rM
5597
     shll $1, %rN, %rM  -> addl %rN, %rN, %rM
5598
     shlq $1, %rN, %rM  -> addq %rN, %rN, %rM
5599
       */
5600
0
      i.tm.base_opcode = 0x00;
5601
0
      i.tm.extension_opcode = None;
5602
0
      if (i.operands >= 2)
5603
0
  copy_operand (0, 1);
5604
0
      else
5605
0
  {
5606
    /* Legacy form with omitted shift count operand.  */
5607
0
    copy_operand (1, 0);
5608
0
    i.operands = 2;
5609
0
  }
5610
0
      i.reg_operands++;
5611
0
      i.imm_operands = 0;
5612
0
    }
5613
0
  else if (i.tm.base_opcode == 0xba
5614
0
     && i.tm.opcode_space == SPACE_0F
5615
0
     && i.reg_operands == 1
5616
0
     && i.op[0].imms->X_op == O_constant
5617
0
     && i.op[0].imms->X_add_number >= 0)
5618
0
    {
5619
      /* Optimize: -O:
5620
     btw $n, %rN -> btl $n, %rN (outside of 16-bit mode, n < 16)
5621
     btq $n, %rN -> btl $n, %rN (in 64-bit mode, n < 32, N < 8)
5622
     btl $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
5623
5624
     With <BT> one of bts, btr, and bts also:
5625
     <BT>w $n, %rN -> btl $n, %rN (in 32-bit mode, n < 16)
5626
     <BT>l $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
5627
       */
5628
0
      switch (flag_code)
5629
0
  {
5630
0
  case CODE_64BIT:
5631
0
    if (i.tm.extension_opcode != 4)
5632
0
      break;
5633
0
    if (i.types[1].bitfield.qword
5634
0
        && i.op[0].imms->X_add_number < 32
5635
0
        && !(i.op[1].regs->reg_flags & RegRex))
5636
0
      i.tm.opcode_modifier.size = SIZE32;
5637
    /* Fall through.  */
5638
0
  case CODE_32BIT:
5639
0
    if (i.types[1].bitfield.word
5640
0
        && i.op[0].imms->X_add_number < 16)
5641
0
      i.tm.opcode_modifier.size = SIZE32;
5642
0
    break;
5643
0
  case CODE_16BIT:
5644
0
    if (i.op[0].imms->X_add_number < 16)
5645
0
      i.tm.opcode_modifier.size = SIZE16;
5646
0
    break;
5647
0
  }
5648
0
    }
5649
0
  else if (optimize > 1
5650
0
     && (i.tm.base_opcode | 0xf) == 0x4f
5651
0
     && i.tm.opcode_space == SPACE_MAP4
5652
0
     && i.reg_operands == 3
5653
0
     && i.tm.opcode_modifier.operandconstraint == EVEX_NF
5654
0
     && !i.types[0].bitfield.word)
5655
0
    {
5656
      /* Optimize: -O2:
5657
     cfcmov<cc> %rM, %rN, %rN -> cmov<cc> %rM, %rN
5658
     cfcmov<cc> %rM, %rN, %rM -> cmov<!cc> %rN, %rM
5659
     cfcmov<cc> %rN, %rN, %rN -> nop %rN
5660
       */
5661
0
      if (i.op[0].regs == i.op[2].regs)
5662
0
  {
5663
0
    i.tm.base_opcode ^= 1;
5664
0
    i.op[0].regs = i.op[1].regs;
5665
0
    i.op[1].regs = i.op[2].regs;
5666
0
  }
5667
0
      else if (i.op[1].regs != i.op[2].regs)
5668
0
  return;
5669
5670
0
      i.tm.opcode_space = SPACE_0F;
5671
0
      i.tm.opcode_modifier.evex = 0;
5672
0
      i.tm.opcode_modifier.vexvvvv = 0;
5673
0
      i.tm.opcode_modifier.operandconstraint = 0;
5674
0
      i.reg_operands = 2;
5675
5676
      /* While at it, convert to NOP if all three regs match.  */
5677
0
      if (i.op[0].regs == i.op[1].regs)
5678
0
  {
5679
0
    i.tm.base_opcode = 0x1f;
5680
0
    i.tm.extension_opcode = 0;
5681
0
    i.reg_operands = 1;
5682
0
  }
5683
0
    }
5684
0
  else if (i.reg_operands == 3
5685
0
     && i.op[0].regs == i.op[1].regs
5686
0
     && !i.types[2].bitfield.xmmword
5687
0
     && (i.tm.opcode_modifier.vex
5688
0
         || ((!i.mask.reg || i.mask.zeroing)
5689
0
       && i.tm.opcode_modifier.evex
5690
0
       && (pp.encoding != encoding_evex
5691
0
           || cpu_arch_isa_flags.bitfield.cpuavx512vl
5692
0
           || is_cpu (&i.tm, CpuAVX512VL)
5693
0
           || (i.tm_types[2].bitfield.zmmword
5694
0
         && i.types[2].bitfield.ymmword))))
5695
0
     && i.tm.opcode_space == SPACE_0F
5696
0
     && ((i.tm.base_opcode | 2) == 0x57
5697
0
         || i.tm.base_opcode == 0xdf
5698
0
         || i.tm.base_opcode == 0xef
5699
0
         || (i.tm.base_opcode | 3) == 0xfb
5700
0
         || i.tm.base_opcode == 0x42
5701
0
         || i.tm.base_opcode == 0x47))
5702
0
    {
5703
      /* Optimize: -O1:
5704
     VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
5705
     vpsubq and vpsubw:
5706
       EVEX VOP %zmmM, %zmmM, %zmmN
5707
         -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
5708
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
5709
       EVEX VOP %ymmM, %ymmM, %ymmN
5710
         -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
5711
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
5712
       VEX VOP %ymmM, %ymmM, %ymmN
5713
         -> VEX VOP %xmmM, %xmmM, %xmmN
5714
     VOP, one of vpandn and vpxor:
5715
       VEX VOP %ymmM, %ymmM, %ymmN
5716
         -> VEX VOP %xmmM, %xmmM, %xmmN
5717
     VOP, one of vpandnd and vpandnq:
5718
       EVEX VOP %zmmM, %zmmM, %zmmN
5719
         -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
5720
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
5721
       EVEX VOP %ymmM, %ymmM, %ymmN
5722
         -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
5723
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
5724
     VOP, one of vpxord and vpxorq:
5725
       EVEX VOP %zmmM, %zmmM, %zmmN
5726
         -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
5727
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
5728
       EVEX VOP %ymmM, %ymmM, %ymmN
5729
         -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
5730
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
5731
     VOP, one of kxord and kxorq:
5732
       VEX VOP %kM, %kM, %kN
5733
         -> VEX kxorw %kM, %kM, %kN
5734
     VOP, one of kandnd and kandnq:
5735
       VEX VOP %kM, %kM, %kN
5736
         -> VEX kandnw %kM, %kM, %kN
5737
       */
5738
0
      if (i.tm.opcode_modifier.evex)
5739
0
  {
5740
0
    if (pp.encoding != encoding_evex)
5741
0
      {
5742
0
        i.tm.opcode_modifier.vex = VEX128;
5743
0
        i.tm.opcode_modifier.vexw = VEXW0;
5744
0
        i.tm.opcode_modifier.evex = 0;
5745
0
        pp.encoding = encoding_vex;
5746
0
        i.mask.reg = NULL;
5747
0
      }
5748
0
    else if (optimize > 1)
5749
0
      i.tm.opcode_modifier.evex = EVEX128;
5750
0
    else
5751
0
      return;
5752
0
  }
5753
0
      else if (i.tm_types[0].bitfield.class == RegMask)
5754
0
  {
5755
0
    i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
5756
0
    i.tm.opcode_modifier.vexw = VEXW0;
5757
0
  }
5758
0
      else
5759
0
  i.tm.opcode_modifier.vex = VEX128;
5760
5761
0
      if (i.tm.opcode_modifier.vex)
5762
0
  for (j = 0; j < 3; j++)
5763
0
    {
5764
0
      i.types[j].bitfield.xmmword = 1;
5765
0
      i.types[j].bitfield.ymmword = 0;
5766
0
    }
5767
0
    }
5768
0
  else if (pp.encoding != encoding_evex
5769
0
     && pp.encoding != encoding_egpr
5770
0
     && !i.types[0].bitfield.zmmword
5771
0
     && !i.types[1].bitfield.zmmword
5772
0
     && !i.mask.reg
5773
0
     && !i.broadcast.type
5774
0
     && !i.broadcast.bytes
5775
0
     && i.tm.opcode_modifier.evex
5776
0
     && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
5777
0
         || (i.tm.base_opcode & ~4) == 0xdb
5778
0
         || (i.tm.base_opcode & ~4) == 0xeb)
5779
0
     && i.tm.extension_opcode == None)
5780
0
    {
5781
      /* Optimize: -O1:
5782
     VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
5783
     vmovdqu32 and vmovdqu64:
5784
       EVEX VOP %xmmM, %xmmN
5785
         -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
5786
       EVEX VOP %ymmM, %ymmN
5787
         -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
5788
       EVEX VOP %xmmM, mem
5789
         -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
5790
       EVEX VOP %ymmM, mem
5791
         -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
5792
       EVEX VOP mem, %xmmN
5793
         -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
5794
       EVEX VOP mem, %ymmN
5795
         -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
5796
     VOP, one of vpand, vpandn, vpor, vpxor:
5797
       EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
5798
         -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
5799
       EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
5800
         -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
5801
       EVEX VOP{d,q} mem, %xmmM, %xmmN
5802
         -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
5803
       EVEX VOP{d,q} mem, %ymmM, %ymmN
5804
         -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
5805
       */
5806
0
      for (j = 0; j < i.operands; j++)
5807
0
  if (operand_type_check (i.types[j], disp)
5808
0
      && i.op[j].disps->X_op == O_constant)
5809
0
    {
5810
      /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
5811
         has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
5812
         bytes, we choose EVEX Disp8 over VEX Disp32.  */
5813
0
      int evex_disp8, vex_disp8;
5814
0
      unsigned int memshift = i.memshift;
5815
0
      offsetT n = i.op[j].disps->X_add_number;
5816
5817
0
      evex_disp8 = fits_in_disp8 (n);
5818
0
      i.memshift = 0;
5819
0
      vex_disp8 = fits_in_disp8 (n);
5820
0
      if (evex_disp8 != vex_disp8)
5821
0
        {
5822
0
    i.memshift = memshift;
5823
0
    return;
5824
0
        }
5825
5826
0
      i.types[j].bitfield.disp8 = vex_disp8;
5827
0
      break;
5828
0
    }
5829
0
      if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
5830
0
    && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
5831
0
  i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
5832
0
      i.tm.opcode_modifier.vex
5833
0
  = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
5834
0
      i.tm.opcode_modifier.vexw = VEXW0;
5835
      /* VPAND, VPOR, and VPXOR are commutative.  */
5836
0
      if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
5837
0
  i.tm.opcode_modifier.commutative = 1;
5838
0
      i.tm.opcode_modifier.evex = 0;
5839
0
      i.tm.opcode_modifier.masking = 0;
5840
0
      i.tm.opcode_modifier.broadcast = 0;
5841
0
      i.tm.opcode_modifier.disp8memshift = 0;
5842
0
      i.memshift = 0;
5843
0
      if (j < i.operands)
5844
0
  i.types[j].bitfield.disp8
5845
0
    = fits_in_disp8 (i.op[j].disps->X_add_number);
5846
0
    }
5847
0
  else if (optimize_for_space
5848
0
     && i.tm.base_opcode == 0x29
5849
0
     && i.tm.opcode_space == SPACE_0F38
5850
0
     && i.operands == i.reg_operands
5851
0
     && i.op[0].regs == i.op[1].regs
5852
0
     && (!i.tm.opcode_modifier.vex
5853
0
         || !(i.op[0].regs->reg_flags & RegRex))
5854
0
     && !i.tm.opcode_modifier.evex)
5855
0
    {
5856
      /* Optimize: -Os:
5857
         pcmpeqq %xmmN, %xmmN          -> pcmpeqd %xmmN, %xmmN
5858
         vpcmpeqq %xmmN, %xmmN, %xmmM  -> vpcmpeqd %xmmN, %xmmN, %xmmM (N < 8)
5859
         vpcmpeqq %ymmN, %ymmN, %ymmM  -> vpcmpeqd %ymmN, %ymmN, %ymmM (N < 8)
5860
       */
5861
0
      i.tm.opcode_space = SPACE_0F;
5862
0
      i.tm.base_opcode = 0x76;
5863
0
    }
5864
0
  else if (((i.tm.base_opcode >= 0x64
5865
0
       && i.tm.base_opcode <= 0x66
5866
0
       && i.tm.opcode_space == SPACE_0F)
5867
0
      || (i.tm.base_opcode == 0x37
5868
0
    && i.tm.opcode_space == SPACE_0F38))
5869
0
     && i.operands == i.reg_operands
5870
0
     && i.op[0].regs == i.op[1].regs
5871
0
     && !i.tm.opcode_modifier.evex)
5872
0
    {
5873
      /* Optimize: -O:
5874
         pcmpgt[bwd] %mmN, %mmN             -> pxor %mmN, %mmN
5875
         pcmpgt[bwdq] %xmmN, %xmmN          -> pxor %xmmN, %xmmN
5876
         vpcmpgt[bwdq] %xmmN, %xmmN, %xmmM  -> vpxor %xmmN, %xmmN, %xmmM (N < 8)
5877
         vpcmpgt[bwdq] %xmmN, %xmmN, %xmmM  -> vpxor %xmm0, %xmm0, %xmmM (N > 7)
5878
         vpcmpgt[bwdq] %ymmN, %ymmN, %ymmM  -> vpxor %ymmN, %ymmN, %ymmM (N < 8)
5879
         vpcmpgt[bwdq] %ymmN, %ymmN, %ymmM  -> vpxor %ymm0, %ymm0, %ymmM (N > 7)
5880
       */
5881
0
      i.tm.opcode_space = SPACE_0F;
5882
0
      i.tm.base_opcode = 0xef;
5883
0
      if (i.tm.opcode_modifier.vex && (i.op[0].regs->reg_flags & RegRex))
5884
0
  {
5885
0
    if (i.operands == 2)
5886
0
      {
5887
0
        gas_assert (i.tm.opcode_modifier.sse2avx);
5888
5889
0
        i.operands = 3;
5890
0
        i.reg_operands = 3;
5891
0
        i.tm.operands = 3;
5892
5893
0
        copy_operand (2, 0);
5894
5895
0
        i.tm.opcode_modifier.sse2avx = 0;
5896
0
      }
5897
0
    i.op[0].regs -= i.op[0].regs->reg_num + 8;
5898
0
    i.op[1].regs = i.op[0].regs;
5899
0
  }
5900
0
    }
5901
0
  else if (i.tm.extension_opcode == 6
5902
0
     && i.tm.base_opcode >= 0x71
5903
0
     && i.tm.base_opcode <= 0x73
5904
0
     && i.tm.opcode_space == SPACE_0F
5905
0
     && i.op[0].imms->X_op == O_constant
5906
0
     && i.op[0].imms->X_add_number == 1
5907
0
     && !i.mem_operands)
5908
0
    {
5909
      /* Optimize: -O:
5910
     psllw $1, %mmxN          -> paddw %mmxN, %mmxN
5911
     psllw $1, %xmmN          -> paddw %xmmN, %xmmN
5912
     vpsllw $1, %xmmN, %xmmM  -> vpaddw %xmmN, %xmmN, %xmmM
5913
     vpsllw $1, %ymmN, %ymmM  -> vpaddw %ymmN, %ymmN, %ymmM
5914
     vpsllw $1, %zmmN, %zmmM  -> vpaddw %zmmN, %zmmN, %zmmM
5915
5916
     pslld $1, %mmxN          -> paddd %mmxN, %mmxN
5917
     pslld $1, %xmmN          -> paddd %xmmN, %xmmN
5918
     vpslld $1, %xmmN, %xmmM  -> vpaddd %xmmN, %xmmN, %xmmM
5919
     vpslld $1, %ymmN, %ymmM  -> vpaddd %ymmN, %ymmN, %ymmM
5920
     vpslld $1, %zmmN, %zmmM  -> vpaddd %zmmN, %zmmN, %zmmM
5921
5922
     psllq $1, %xmmN          -> paddq %xmmN, %xmmN
5923
     vpsllq $1, %xmmN, %xmmM  -> vpaddq %xmmN, %xmmN, %xmmM
5924
     vpsllq $1, %ymmN, %ymmM  -> vpaddq %ymmN, %ymmN, %ymmM
5925
     vpsllq $1, %zmmN, %zmmM  -> vpaddq %zmmN, %zmmN, %zmmM
5926
    */
5927
0
      if (i.tm.base_opcode != 0x73)
5928
0
  i.tm.base_opcode |= 0xfc; /* {,v}padd{w,d} */
5929
0
      else
5930
0
  {
5931
0
    gas_assert (i.tm_types[1].bitfield.class != RegMMX);
5932
0
    i.tm.base_opcode = 0xd4; /* {,v}paddq */
5933
0
  }
5934
0
      i.tm.extension_opcode = None;
5935
0
      if (i.tm.opcode_modifier.vexvvvv)
5936
0
  i.tm.opcode_modifier.vexvvvv = VexVVVV_SRC1;
5937
0
      copy_operand (0, 1);
5938
0
      i.reg_operands++;
5939
0
      i.imm_operands = 0;
5940
0
    }
5941
0
  else if (optimize_for_space
5942
0
     && i.tm.base_opcode == 0x59
5943
0
     && i.tm.opcode_space == SPACE_0F38
5944
0
     && i.operands == i.reg_operands
5945
0
     && i.tm.opcode_modifier.vex
5946
0
     && !(i.op[0].regs->reg_flags & RegRex)
5947
0
     && i.types[1].bitfield.xmmword
5948
0
     && pp.encoding != encoding_vex3)
5949
0
    {
5950
      /* Optimize: -Os:
5951
         vpbroadcastq %xmmN, %xmmM  -> vpunpcklqdq %xmmN, %xmmN, %xmmM (N < 8)
5952
       */
5953
0
      i.tm.opcode_space = SPACE_0F;
5954
0
      i.tm.base_opcode = 0x6c;
5955
0
      i.tm.opcode_modifier.vexvvvv = VexVVVV_SRC1;
5956
5957
0
      ++i.operands;
5958
0
      ++i.reg_operands;
5959
0
      ++i.tm.operands;
5960
5961
0
      copy_operand (2, 0);
5962
0
      swap_2_operands (1, 2);
5963
0
    }
5964
0
  else if (i.tm.base_opcode == 0x16
5965
0
     && i.tm.opcode_space == SPACE_0F3A
5966
0
     && i.op[0].imms->X_op == O_constant
5967
0
     && i.op[0].imms->X_add_number == 0)
5968
0
    {
5969
      /* Optimize: -O:
5970
         pextrd $0, %xmmN, ...   -> movd %xmmN, ...
5971
         pextrq $0, %xmmN, ...   -> movq %xmmN, ...
5972
         vpextrd $0, %xmmN, ...  -> vmovd %xmmN, ...
5973
         vpextrq $0, %xmmN, ...  -> vmovq %xmmN, ...
5974
       */
5975
0
      i.tm.opcode_space = SPACE_0F;
5976
0
      if (!i.mem_operands
5977
0
    || i.tm.opcode_modifier.evex
5978
0
    || (i.tm.opcode_modifier.vexw != VEXW1
5979
0
        && i.tm.opcode_modifier.size != SIZE64))
5980
0
  i.tm.base_opcode = 0x7e;
5981
0
      else
5982
0
  {
5983
0
    i.tm.base_opcode = 0xd6;
5984
0
    i.tm.opcode_modifier.size = 0;
5985
0
    i.tm.opcode_modifier.vexw
5986
0
      = i.tm.opcode_modifier.sse2avx ? VEXW0 : VEXWIG;
5987
0
  }
5988
5989
0
      copy_operand (0, 1);
5990
0
      copy_operand (1, 2);
5991
5992
0
      i.operands = 2;
5993
0
      i.imm_operands = 0;
5994
0
    }
5995
0
  else if (i.tm.base_opcode == 0x17
5996
0
     && i.tm.opcode_space == SPACE_0F3A
5997
0
     && i.op[0].imms->X_op == O_constant
5998
0
     && i.op[0].imms->X_add_number == 0)
5999
0
    {
6000
      /* Optimize: -O:
6001
         extractps $0, %xmmN, %rM   -> movd %xmmN, %rM
6002
         extractps $0, %xmmN, mem   -> movss %xmmN, mem
6003
         vextractps $0, %xmmN, %rM  -> vmovd %xmmN, %rM
6004
         vextractps $0, %xmmN, mem  -> vmovss %xmmN, mem
6005
       */
6006
0
      i.tm.opcode_space = SPACE_0F;
6007
0
      i.tm.opcode_modifier.vexw = VEXW0;
6008
6009
0
      if (!i.mem_operands)
6010
0
  i.tm.base_opcode = 0x7e;
6011
0
      else
6012
0
  {
6013
0
    i.tm.base_opcode = 0x11;
6014
0
    i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
6015
0
  }
6016
6017
0
      copy_operand (0, 1);
6018
0
      copy_operand (1, 2);
6019
6020
0
      i.operands = 2;
6021
0
      i.imm_operands = 0;
6022
0
    }
6023
0
  else if ((i.tm.base_opcode | 0x22) == 0x3b
6024
0
     && i.tm.opcode_space == SPACE_0F3A
6025
0
     && i.op[0].imms->X_op == O_constant
6026
0
     && i.op[0].imms->X_add_number == 0)
6027
0
    {
6028
      /* Optimize: -O:
6029
         vextractf128 $0, %ymmN, %xmmM      -> vmovaps %xmmN, %xmmM
6030
         vextractf128 $0, %ymmN, mem        -> vmovups %xmmN, mem
6031
         vextractf32x4 $0, %[yz]mmN, %xmmM  -> vmovaps %xmmN, %xmmM
6032
         vextractf32x4 $0, %[yz]mmN, mem    -> vmovups %xmmN, mem
6033
         vextractf64x2 $0, %[yz]mmN, %xmmM  -> vmovapd %xmmN, %xmmM
6034
         vextractf64x2 $0, %[yz]mmN, mem    -> vmovupd %xmmN, mem
6035
         vextractf32x8 $0, %zmmN, %ymmM     -> vmovaps %ymmN, %ymmM
6036
         vextractf32x8 $0, %zmmN, mem       -> vmovups %ymmN, mem
6037
         vextractf64x4 $0, %zmmN, %ymmM     -> vmovapd %ymmN, %ymmM
6038
         vextractf64x4 $0, %zmmN, mem       -> vmovupd %ymmN, mem
6039
         vextracti128 $0, %ymmN, %xmmM      -> vmovdqa %xmmN, %xmmM
6040
         vextracti128 $0, %ymmN, mem        -> vmovdqu %xmmN, mem
6041
         vextracti32x4 $0, %[yz]mmN, %xmmM  -> vmovdqa{,32} %xmmN, %xmmM
6042
         vextracti32x4 $0, %[yz]mmN, mem    -> vmovdqu{,32} %xmmN, mem
6043
         vextracti64x2 $0, %[yz]mmN, %xmmM  -> vmovdqa{,64} %xmmN, %xmmM
6044
         vextracti64x2 $0, %[yz]mmN, mem    -> vmovdqu{,64} %xmmN, mem
6045
         vextracti32x8 $0, %zmmN, %ymmM     -> vmovdqa{,32} %ymmN, %ymmM
6046
         vextracti32x8 $0, %zmmN, mem       -> vmovdqu{,32} %ymmN, mem
6047
         vextracti64x4 $0, %zmmN, %ymmM     -> vmovdqa{,64} %ymmN, %ymmM
6048
         vextracti64x4 $0, %zmmN, mem       -> vmovdqu{,64} %ymmN, mem
6049
       */
6050
0
      i.tm.opcode_space = SPACE_0F;
6051
6052
0
      if (!i.mask.reg
6053
0
    && (pp.encoding <= encoding_vex3
6054
0
        || (pp.encoding == encoding_evex512
6055
0
      && (!i.base_reg || !(i.base_reg->reg_flags & RegRex2))
6056
0
      && (!i.index_reg || !(i.index_reg->reg_flags & RegRex2)))))
6057
0
  {
6058
0
    i.tm.opcode_modifier.vex = i.tm.base_opcode & 2 ? VEX256 : VEX128;
6059
0
    i.tm.opcode_modifier.evex = 0;
6060
0
  }
6061
0
      else
6062
0
  i.tm.opcode_modifier.evex = i.tm.base_opcode & 2 ? EVEX256 : EVEX128;
6063
6064
0
      if (i.tm.base_opcode & 0x20)
6065
0
  {
6066
0
    i.tm.base_opcode = 0x7f;
6067
0
    if (i.reg_operands != 2)
6068
0
      i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
6069
0
  }
6070
0
      else
6071
0
  {
6072
0
    if (i.reg_operands == 2)
6073
0
      i.tm.base_opcode = 0x29;
6074
0
    else
6075
0
      i.tm.base_opcode = 0x11;
6076
0
    if (i.tm.opcode_modifier.vexw != VEXW1)
6077
0
      i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
6078
0
  }
6079
6080
0
      if (i.tm.opcode_modifier.vex)
6081
0
  i.tm.opcode_modifier.vexw = VEXWIG;
6082
6083
0
      copy_operand (0, 1);
6084
0
      copy_operand (1, 2);
6085
6086
0
      i.operands = 2;
6087
0
      i.imm_operands = 0;
6088
0
    }
6089
0
  else if (i.tm.base_opcode == 0x21
6090
0
     && i.tm.opcode_space == SPACE_0F3A
6091
0
     && i.op[0].imms->X_op == O_constant
6092
0
     && (i.operands == i.reg_operands + 1
6093
0
         ? i.op[0].imms->X_add_number == 0
6094
0
     || (i.op[0].imms->X_add_number & 0xf) == 0xf
6095
0
         : (i.op[0].imms->X_add_number & 0x3f) == 0x0e
6096
0
      && (i.reg_operands == 1 || i.op[2].regs == i.op[3].regs)))
6097
0
    {
6098
      /* Optimize: -O:
6099
         insertps $0b....1111, %xmmN, %xmmM          -> xorps %xmmM, %xmmM
6100
         insertps $0b00000000, %xmmN, %xmmM          -> movss %xmmN, %xmmM
6101
         insertps $0b..001110, mem, %xmmN            -> movss mem, %xmmN
6102
         vinsertps $0b....1111, %xmmN, %xmmM, %xmmK  -> vxorps %xmm?, %xmm?, %xmmK
6103
         vinsertps $0b00000000, %xmmN, %xmmM, %xmmK  -> vmovss %xmmN, %xmmM, %xmmK
6104
         vinsertps $0b..001110, mem, %xmmN, %xmmN    -> vmovss mem, %xmmN
6105
       */
6106
0
      i.tm.opcode_space = SPACE_0F;
6107
0
      if ((i.op[0].imms->X_add_number & 0xf) == 0xf)
6108
0
  {
6109
0
    i.tm.base_opcode = 0x57;
6110
0
    i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
6111
6112
0
    --i.operands;
6113
6114
0
    copy_operand (i.operands - 1, i.operands);
6115
0
    copy_operand (1, i.operands - 1);
6116
0
    copy_operand (0, 1);
6117
6118
    /* Switch from EVEX to VEX encoding if possible.  Sadly we can't
6119
       (always) tell use of the {evex} pseudo-prefix (which otherwise
6120
       we'd like to respect) from use of %xmm16-%xmm31.  */
6121
0
    if (pp.encoding == encoding_evex)
6122
0
      pp.encoding = encoding_default;
6123
0
    if (i.tm.opcode_modifier.evex
6124
0
        && pp.encoding <= encoding_vex3
6125
0
        && !(i.op[0].regs->reg_flags & RegVRex))
6126
0
      {
6127
0
        i.tm.opcode_modifier.evex = 0;
6128
0
        i.tm.opcode_modifier.vex = VEX128;
6129
0
      }
6130
6131
    /* Switch from VEX3 to VEX2 encoding if possible.  */
6132
0
    if (i.tm.opcode_modifier.vex
6133
0
        && pp.encoding <= encoding_vex
6134
0
        && (i.op[0].regs->reg_flags & RegRex))
6135
0
      {
6136
0
        i.op[0].regs -= 8;
6137
0
        i.op[1].regs = i.op[0].regs;
6138
0
      }
6139
0
  }
6140
0
      else
6141
0
  {
6142
0
    i.tm.base_opcode = 0x10;
6143
0
    i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
6144
6145
0
    if (i.op[0].imms->X_add_number == 0)
6146
0
      --i.operands;
6147
0
    else
6148
0
      {
6149
0
        i.operands = 2;
6150
0
        i.tm.opcode_modifier.vexvvvv = 0;
6151
0
      }
6152
0
    copy_operand (0, 1);
6153
0
    copy_operand (1, 2);
6154
0
    copy_operand (2, 3);
6155
0
  }
6156
6157
0
      i.imm_operands = 0;
6158
0
    }
6159
0
}
6160
6161
/* Check whether the promoted (to address size) register is usable as index
6162
   register in ModR/M SIB addressing.  */
6163
6164
static bool is_index (const reg_entry *r)
6165
0
{
6166
0
  gas_assert (flag_code == CODE_64BIT);
6167
6168
0
  if (r->reg_type.bitfield.byte)
6169
0
    {
6170
0
      if (!(r->reg_flags & (RegRex | RegRex2 | RegRex64)))
6171
0
  {
6172
0
    if (r->reg_num >= 4)
6173
0
      return false;
6174
0
    r += 8;
6175
0
  }
6176
0
      r += 32;
6177
0
    }
6178
0
  if (r->reg_type.bitfield.word)
6179
0
    r += 32;
6180
  /* No need to further check .dword here.  */
6181
6182
0
  return r->reg_type.bitfield.baseindex;
6183
0
}
6184
6185
/* Try to shorten {nf} encodings, by shortening operand size or switching to
6186
   functionally identical encodings.  */
6187
6188
static void
6189
optimize_nf_encoding (void)
6190
0
{
6191
0
  if (i.tm.base_opcode == 0x80
6192
0
      && (i.tm.extension_opcode == 0 || i.tm.extension_opcode == 5)
6193
0
      && i.suffix != BYTE_MNEM_SUFFIX
6194
0
      && !i.types[1].bitfield.byte
6195
0
      && !i.types[2].bitfield.byte
6196
0
      && i.op[0].imms->X_op == O_constant
6197
0
      && i.op[0].imms->X_add_number == 0x80)
6198
0
    {
6199
      /* Optimize: -O:
6200
     {nf} addw $0x80, ...  -> {nf} subw $-0x80, ...
6201
     {nf} addl $0x80, ...  -> {nf} subl $-0x80, ...
6202
     {nf} addq $0x80, ...  -> {nf} subq $-0x80, ...
6203
6204
     {nf} subw $0x80, ...  -> {nf} addw $-0x80, ...
6205
     {nf} subl $0x80, ...  -> {nf} addl $-0x80, ...
6206
     {nf} subq $0x80, ...  -> {nf} addq $-0x80, ...
6207
       */
6208
0
      i.tm.base_opcode |= 3;
6209
0
      i.tm.extension_opcode ^= 5;
6210
0
      i.tm.opcode_modifier.w = 0;
6211
0
      i.op[0].imms->X_add_number = -i.op[0].imms->X_add_number;
6212
6213
0
      i.tm_types[0].bitfield.imm8 = 0;
6214
0
      i.tm_types[0].bitfield.imm8s = 1;
6215
0
      i.tm_types[0].bitfield.imm16 = 0;
6216
0
      i.tm_types[0].bitfield.imm32 = 0;
6217
0
      i.tm_types[0].bitfield.imm32s = 0;
6218
6219
0
      i.types[0] = i.tm_types[0];
6220
0
    }
6221
0
  else if ((i.tm.base_opcode | 3) == 0x83
6222
0
      && (i.tm.extension_opcode == 0 || i.tm.extension_opcode == 5)
6223
0
      && i.op[0].imms->X_op == O_constant
6224
0
      && (i.op[0].imms->X_add_number == 1
6225
0
    || i.op[0].imms->X_add_number == -1
6226
    /* While for wider than byte operations immediates were suitably
6227
       adjusted earlier on, 0xff in the byte case needs covering
6228
       explicitly.  */
6229
0
    || (i.op[0].imms->X_add_number == 0xff
6230
0
        && (i.suffix == BYTE_MNEM_SUFFIX
6231
0
      || i.types[i.operands - 1].bitfield.byte))))
6232
0
    {
6233
      /* Optimize: -O:
6234
     {nf} add $1, ...        -> {nf} inc ...
6235
     {nf} add $-1, ...       -> {nf} dec ...
6236
     {nf} add $0xf...f, ...  -> {nf} dec ...
6237
6238
     {nf} sub $1, ...        -> {nf} dec ...
6239
     {nf} sub $-1, ...       -> {nf} inc ...
6240
     {nf} sub $0xf...f, ...  -> {nf} inc ...
6241
       */
6242
0
      i.tm.base_opcode = 0xfe;
6243
0
      i.tm.extension_opcode
6244
0
  = (i.op[0].imms->X_add_number == 1) != (i.tm.extension_opcode == 0);
6245
0
      i.tm.opcode_modifier.w = 1;
6246
6247
0
      copy_operand (0, 1);
6248
0
      copy_operand (1, 2);
6249
6250
0
      i.imm_operands = 0;
6251
0
      --i.operands;
6252
0
    }
6253
0
  else if ((i.tm.base_opcode | 3) == 0x83
6254
0
      && i.tm.extension_opcode == 6
6255
0
      && i.op[0].imms->X_op == O_constant
6256
0
      && (i.op[0].imms->X_add_number == -1
6257
    /* See comment above.  */
6258
0
    || (i.op[0].imms->X_add_number == 0xff
6259
0
        && (i.suffix == BYTE_MNEM_SUFFIX
6260
0
      || i.types[i.operands - 1].bitfield.byte))))
6261
0
    {
6262
      /* Optimize: -O:
6263
     {nf} xorb $~0, ...       -> notb ...
6264
     {nf} xorb $0xff, ...     -> notb ...
6265
6266
     {nf} xorw $~0, ...       -> notw ...
6267
6268
     {nf} xorl $~0, %N, %N    -> notl %N
6269
     {nf} xorl $~0, %M/m, %N  -> notl %M/m, %N
6270
     {nf} xorl $~0, %N/m      -> notl %N/m
6271
6272
     {nf} xorq $~0, %N, %N    -> notq %N
6273
     {nf} xorq $~0, %M/m, %N  -> notq %M/m, %N
6274
     {nf} xorq $~0, %N/m      -> notq %N/m
6275
       */
6276
0
      i.tm.base_opcode = 0xf6;
6277
0
      i.tm.extension_opcode = 2;
6278
0
      i.tm.opcode_modifier.w = 1;
6279
0
      pp.has_nf = false;
6280
6281
0
      copy_operand (0, 1);
6282
0
      copy_operand (1, 2);
6283
6284
0
      i.imm_operands = 0;
6285
0
      if (i.operands == 2
6286
0
    || (i.reg_operands == 2
6287
0
        && (i.types[1].bitfield.dword || i.types[1].bitfield.qword)
6288
0
        && i.op[0].regs == i.op[1].regs))
6289
0
  {
6290
0
       i.tm.opcode_space = SPACE_BASE;
6291
0
       i.tm.opcode_modifier.evex = 0;
6292
0
       i.tm.opcode_modifier.vexvvvv = 0;
6293
0
    i.operands = 1;
6294
0
  }
6295
0
      else
6296
0
  i.operands = 2;
6297
0
      i.reg_operands = i.operands - i.mem_operands;
6298
0
    }
6299
0
  else if (i.tm.base_opcode == 0x80
6300
0
     && i.tm.extension_opcode == 4
6301
0
     && i.reg_operands + 1 == i.operands
6302
0
     && !i.prefix[DATA_PREFIX]
6303
0
     && !i.prefix[REX_PREFIX]
6304
0
     && i.op[0].imms->X_op == O_constant
6305
0
     && i.op[0].imms->X_add_number >= 0
6306
0
     && i.op[0].imms->X_add_number <= (optimize_for_space ? 0xffff : 0xff)
6307
0
     && is_plausible_suffix (1)
6308
0
     && !i.types[1].bitfield.byte
6309
0
     && (!i.types[1].bitfield.word
6310
0
         || (i.reg_operands == 2
6311
0
       && i.op[0].imms->X_add_number <= 0xff)))
6312
0
    {
6313
      /* Optimize: -O:
6314
     {nf} andq $imm8, %rN, %rM   -> {nf} andb $imm8, %rN, %rM
6315
     {nf} andl $imm8, %rN, %rM   -> {nf} andb $imm8, %rN, %rM
6316
     {nf} andw $imm8, %rN, %rM   -> {nf} andb $imm8, %rN, %rM
6317
6318
     {nf} andq $imm8, %rN        -> {nf} andb $imm8, %rN, %rN
6319
     {nf} andl $imm8, %rN        -> {nf} andb $imm8, %rN, %rN
6320
6321
   Optimize: -Os:
6322
     {nf} andq $imm16, %rN, %rM  -> {nf} andw $imm16, %rN, %rM
6323
     {nf} andl $imm16, %rN, %rM  -> {nf} andw $imm16, %rN, %rM
6324
6325
     {nf} andq $imm16, %rN       -> {nf} andw $imm16, %rN, %rN
6326
     {nf} andl $imm16, %rN       -> {nf} andw $imm16, %rN, %rN
6327
       */
6328
0
      if (i.reg_operands == 1)
6329
0
  {
6330
0
    copy_operand (2, 1);
6331
0
    i.tm.opcode_modifier.vexvvvv = VexVVVV_DST;
6332
0
    i.reg_operands = 2;
6333
0
    i.operands = 3;
6334
0
  }
6335
6336
0
      if (i.op[0].imms->X_add_number <= 0xff)
6337
0
  {
6338
0
    i.types[0].bitfield.imm8 = 1;
6339
0
    i.types[0].bitfield.imm16 = 0;
6340
0
    i.types[2].bitfield.byte = 1;
6341
0
    i.suffix = 0;
6342
0
    i.tm.opcode_modifier.w = 0;
6343
0
    i.tm.opcode_modifier.vexw = VEXWIG;
6344
0
  }
6345
0
      else
6346
0
  {
6347
0
    i.types[0].bitfield.imm16 = 1;
6348
0
    i.tm.opcode_modifier.size = SIZE16;
6349
0
    i.tm.opcode_modifier.vexw = VEXW0;
6350
0
  }
6351
6352
0
      i.types[0].bitfield.imm32 = 0;
6353
0
      i.types[0].bitfield.imm32s = 0;
6354
0
    }
6355
0
  else if (optimize_for_space
6356
0
     && i.tm.base_opcode == 0x80
6357
0
     && i.tm.extension_opcode == 4
6358
0
     && i.reg_operands + 1 == i.operands
6359
0
     && !i.prefix[DATA_PREFIX]
6360
0
     && !i.prefix[REX_PREFIX]
6361
0
     && i.op[0].imms->X_op == O_constant
6362
0
     && i.op[0].imms->X_add_number < 0
6363
0
     && i.op[0].imms->X_add_number >= ~0xffff
6364
0
     && is_plausible_suffix (1)
6365
0
     && (i.types[1].bitfield.qword
6366
0
         ? (i.reg_operands == 1
6367
0
      || i.op[1].regs == i.op[2].regs)
6368
0
         : (i.types[1].bitfield.word
6369
0
      && i.reg_operands == 1
6370
0
      && i.op[0].imms->X_add_number >= ~0xff)))
6371
0
    {
6372
      /* Optimize: -Os:
6373
     {nf} andq $~imm8, %rN, %rN   -> {nf} andb $~imm8, %rN
6374
     {nf} andq $~imm8, %rN        -> {nf} andb $~imm8, %rN
6375
     {nf} andw $~imm8, %rN        -> {nf} andb $~imm8, %rN
6376
6377
     {nf} andq $~imm16, %rN, %rN  -> {nf} andw $~imm16, %rN
6378
     {nf} andq $~imm16, %rN       -> {nf} andw $~imm16, %rN
6379
       */
6380
0
      if (i.reg_operands == 2)
6381
0
  {
6382
0
    i.tm.opcode_modifier.vexvvvv = 0;
6383
0
    i.reg_operands = 1;
6384
0
    i.operands = 2;
6385
0
  }
6386
6387
0
      if (i.op[0].imms->X_add_number >= ~0xff)
6388
0
  {
6389
0
    i.types[0].bitfield.imm8 = 1;
6390
0
    i.types[0].bitfield.imm16 = 0;
6391
0
    i.types[1].bitfield.byte = 1;
6392
0
    i.suffix = 0;
6393
0
    i.tm.opcode_modifier.w = 0;
6394
0
    i.tm.opcode_modifier.vexw = VEXWIG;
6395
0
  }
6396
0
      else
6397
0
  {
6398
0
    i.types[0].bitfield.imm16 = 1;
6399
0
    i.tm.opcode_modifier.size = SIZE16;
6400
0
    i.tm.opcode_modifier.vexw = VEXW0;
6401
0
  }
6402
6403
0
      i.types[0].bitfield.imm32 = 0;
6404
0
      i.types[0].bitfield.imm32s = 0;
6405
0
    }
6406
0
  else if (optimize_for_space
6407
0
     && i.tm.base_opcode == 0x80
6408
0
     && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 6)
6409
0
     && i.reg_operands + 1 == i.operands
6410
0
     && !i.prefix[DATA_PREFIX]
6411
0
     && !i.prefix[REX_PREFIX]
6412
0
     && i.op[0].imms->X_op == O_constant
6413
0
     && i.op[0].imms->X_add_number >= 0
6414
0
     && i.op[0].imms->X_add_number <= 0xffff
6415
0
     && is_plausible_suffix (1)
6416
0
     && (i.types[1].bitfield.qword
6417
0
         ? (i.reg_operands == 1
6418
0
      || i.op[1].regs == i.op[2].regs)
6419
0
         : (i.types[1].bitfield.word
6420
0
      && i.reg_operands == 1
6421
0
      && i.op[0].imms->X_add_number <= 0xff)))
6422
0
    {
6423
      /* Optimize: -Os:
6424
     OR one of or / xor:
6425
     {nf} ORq $imm8, %rN, %rN   -> {nf} ORb $imm8, %rN
6426
     {nf} ORq $imm8, %rN        -> {nf} ORb $imm8, %rN
6427
     {nf} ORw $imm8, %rN        -> {nf} ORb $imm8, %rN
6428
6429
     {nf} ORq $imm16, %rN, %rN  -> {nf} ORw $imm16, %rN
6430
     {nf} ORq $imm16, %rN       -> {nf} ORw $imm16, %rN
6431
       */
6432
0
      if (i.reg_operands == 2)
6433
0
  {
6434
0
    i.tm.opcode_modifier.vexvvvv = 0;
6435
0
    i.reg_operands = 1;
6436
0
    i.operands = 2;
6437
0
  }
6438
6439
0
      if (i.op[0].imms->X_add_number <= 0xff)
6440
0
  {
6441
0
    i.types[0].bitfield.imm8 = 1;
6442
0
    i.types[0].bitfield.imm16 = 0;
6443
0
    i.types[1].bitfield.byte = 1;
6444
0
    i.suffix = 0;
6445
0
    i.tm.opcode_modifier.w = 0;
6446
0
    i.tm.opcode_modifier.vexw = VEXWIG;
6447
0
  }
6448
0
      else
6449
0
  {
6450
0
    i.types[0].bitfield.imm16 = 1;
6451
0
    i.tm.opcode_modifier.size = SIZE16;
6452
0
    i.tm.opcode_modifier.vexw = VEXW0;
6453
0
  }
6454
6455
0
      i.types[0].bitfield.imm32 = 0;
6456
0
      i.types[0].bitfield.imm32s = 0;
6457
0
    }
6458
0
  else if (i.tm.base_opcode == 0xc0
6459
0
     && i.op[0].imms->X_op == O_constant
6460
0
     && i.op[0].imms->X_add_number
6461
0
        == (i.types[i.operands - 1].bitfield.byte
6462
0
      || i.suffix == BYTE_MNEM_SUFFIX
6463
0
      ? 7 : i.types[i.operands - 1].bitfield.word
6464
0
      || i.suffix == WORD_MNEM_SUFFIX
6465
0
      ? 15 : 63 >> (i.types[i.operands - 1].bitfield.dword
6466
0
              || i.suffix == LONG_MNEM_SUFFIX)))
6467
0
    {
6468
      /* Optimize: -O:
6469
     {nf} rol $osz-1, ...   -> {nf} ror $1, ...
6470
     {nf} ror $osz-1, ...   -> {nf} rol $1, ...
6471
       */
6472
0
      gas_assert (i.tm.extension_opcode <= 1);
6473
0
      i.tm.extension_opcode ^= 1;
6474
0
      i.tm.base_opcode = 0xd0;
6475
0
      i.tm_types[0].bitfield.imm1 = 1;
6476
0
      i.imm_operands = 0;
6477
0
    }
6478
0
  else if ((i.tm.base_opcode | 2) == 0x6b
6479
0
     && i.op[0].imms->X_op == O_constant
6480
0
     && (i.op[0].imms->X_add_number > 0
6481
0
         ? !(i.op[0].imms->X_add_number & (i.op[0].imms->X_add_number - 1))
6482
         /* optimize_imm() converts to sign-extended representation where
6483
      possible (and input can also come with these specific numbers).  */
6484
0
         : (i.types[i.operands - 1].bitfield.word
6485
0
      && i.op[0].imms->X_add_number == -0x8000)
6486
0
     || (i.types[i.operands - 1].bitfield.dword
6487
0
         && i.op[0].imms->X_add_number + 1 == -0x7fffffff))
6488
     /* 16-bit 3-operand non-ZU forms need leaviong alone, to prevent
6489
        zero-extension of the result.  Unless, of course, both non-
6490
        immediate operands match (which can be converted to the non-NDD
6491
        form).  */
6492
0
     && (i.operands < 3
6493
0
         || !i.types[2].bitfield.word
6494
0
         || i.tm.mnem_off == MN_imulzu
6495
0
         || i.op[2].regs == i.op[1].regs)
6496
     /* When merely optimizing for size, exclude cases where we'd convert
6497
        from Imm8S to Imm8 encoding, thus not actually reducing size.  */
6498
0
     && (!optimize_for_space
6499
0
         || i.tm.base_opcode == 0x69
6500
0
         || !(i.op[0].imms->X_add_number & 0x7d)))
6501
0
    {
6502
      /* Optimize: -O:
6503
     {nf} imul   $1<<N, ...   -> {nf} shl $N, ...
6504
     {nf} imulzu $1<<N, ...   -> {nf} shl $N, ...
6505
       */
6506
0
      if (i.op[0].imms->X_add_number != 2)
6507
0
  {
6508
0
    i.tm.base_opcode = 0xc0;
6509
0
    i.op[0].imms->X_add_number = ffs (i.op[0].imms->X_add_number) - 1;
6510
0
    i.tm_types[0].bitfield.imm8 = 1;
6511
0
    i.tm_types[0].bitfield.imm16 = 0;
6512
0
    i.tm_types[0].bitfield.imm32 = 0;
6513
0
    i.tm_types[0].bitfield.imm32s = 0;
6514
0
  }
6515
0
      else
6516
0
  {
6517
0
    i.tm.base_opcode = 0xd0;
6518
0
    i.tm_types[0].bitfield.imm1 = 1;
6519
0
  }
6520
0
      i.types[0] = i.tm_types[0];
6521
0
      i.tm.extension_opcode = 4;
6522
0
      i.tm.opcode_modifier.w = 1;
6523
0
      i.tm.opcode_modifier.operandconstraint = 0;
6524
0
      if (i.operands == 3)
6525
0
  {
6526
0
    if (i.op[2].regs == i.op[1].regs && i.tm.mnem_off != MN_imulzu)
6527
0
      {
6528
        /* Convert to non-NDD form.  This is required for 16-bit insns
6529
           (to prevent zero-extension) and benign for others.  */
6530
0
        i.operands = 2;
6531
0
        i.reg_operands = 1;
6532
0
      }
6533
0
    else
6534
0
      i.tm.opcode_modifier.vexvvvv = VexVVVV_DST;
6535
0
  }
6536
0
      else if (i.tm.mnem_off == MN_imulzu)
6537
0
  {
6538
    /* Convert to NDD form, to effect zero-extension of the result.  */
6539
0
    i.tm.opcode_modifier.vexvvvv = VexVVVV_DST;
6540
0
    i.operands = 3;
6541
0
    i.reg_operands = 2;
6542
0
    copy_operand (2, 1);
6543
0
  }
6544
0
    }
6545
0
  else if (i.tm.base_opcode == 0x6b
6546
0
     && i.op[0].imms->X_op == O_constant
6547
0
     && i.op[0].imms->X_add_number == -1
6548
0
     && (!i.types[i.operands - 1].bitfield.word
6549
0
         || i.tm.mnem_off == MN_imulzu
6550
0
         || i.operands == 2
6551
0
         || (i.reg_operands == 2
6552
0
       && i.op[1].regs == i.op[2].regs)))
6553
0
    {
6554
      /* Optimize: -O:
6555
     {nf} imulzuw $~0, %M/m, %N  -> {nf} negw %M/m, %N
6556
     {nf} imulzuw $~0, %N        -> {nf} negw %N, %N
6557
     {nf} imulw   $~0, %N, %N    -> {nf} negw %N
6558
     {nf} imulw   $~0, %N        -> {nf} negw %N
6559
6560
     {nf} imull   $~0, ...       -> {nf} negl ...
6561
     {nf} imulq   $~0, ...       -> {nf} negq ...
6562
       */
6563
0
      i.tm.base_opcode = 0xf7;
6564
0
      i.tm.extension_opcode = 3;
6565
0
      i.tm.opcode_modifier.operandconstraint = 0;
6566
6567
0
      copy_operand (0, 1);
6568
0
      if (i.operands == 3)
6569
0
  copy_operand (1, 2);
6570
6571
0
      i.imm_operands = 0;
6572
0
      if (!i.types[1].bitfield.word)
6573
0
  --i.operands;
6574
0
      else
6575
0
  i.operands = 1 + (i.tm.mnem_off == MN_imulzu);
6576
0
      i.reg_operands = i.operands - i.mem_operands;
6577
6578
0
      if (i.operands == 2)
6579
0
  i.tm.opcode_modifier.vexvvvv = VexVVVV_DST;
6580
0
    }
6581
6582
0
  if (optimize_for_space
6583
0
      && pp.encoding != encoding_evex
6584
0
      && (i.tm.base_opcode == 0x00
6585
0
    || (i.tm.base_opcode == 0xd0 && i.tm.extension_opcode == 4))
6586
0
      && !i.mem_operands
6587
0
      && !i.types[1].bitfield.byte
6588
      /* 16-bit operand size has extra restrictions: If REX2 was needed,
6589
   no size reduction would be possible.  Plus 3-operand forms zero-
6590
   extend the result, which can't be expressed with LEA.  */
6591
0
      && (!i.types[1].bitfield.word
6592
0
    || (i.operands == 2 && pp.encoding != encoding_egpr))
6593
0
      && is_plausible_suffix (1)
6594
      /* %rsp can't be the index.  */
6595
0
      && (is_index (i.op[1].regs)
6596
0
    || (i.imm_operands == 0 && is_index (i.op[0].regs)))
6597
      /* While %rbp, %r13, %r21, and %r29 can be made the index in order to
6598
   avoid the otherwise necessary Disp8, if the other operand is also
6599
   from that set and REX2 would be required to encode the insn, the
6600
   resulting encoding would be no smaller than the EVEX one.  */
6601
0
      && (i.op[1].regs->reg_num != 5
6602
0
    || pp.encoding != encoding_egpr
6603
0
    || i.imm_operands > 0
6604
0
    || i.op[0].regs->reg_num != 5))
6605
0
    {
6606
      /* Optimize: -Os:
6607
     {nf} addw %N, %M    -> leaw (%rM,%rN), %M
6608
     {nf} addl %eN, %eM  -> leal (%rM,%rN), %eM
6609
     {nf} addq %rN, %rM  -> leaq (%rM,%rN), %rM
6610
6611
     {nf} shlw $1, %N   -> leaw (%rN,%rN), %N
6612
     {nf} shll $1, %eN  -> leal (%rN,%rN), %eN
6613
     {nf} shlq $1, %rN  -> leaq (%rN,%rN), %rN
6614
6615
     {nf} addl %eK, %eN, %eM  -> leal (%rN,%rK), %eM
6616
     {nf} addq %rK, %rN, %rM  -> leaq (%rN,%rK), %rM
6617
6618
     {nf} shll $1, %eN, %eM  -> leal (%rN,%rN), %eM
6619
     {nf} shlq $1, %rN, %rM  -> leaq (%rN,%rN), %rM
6620
       */
6621
0
      i.tm.opcode_space = SPACE_BASE;
6622
0
      i.tm.base_opcode = 0x8d;
6623
0
      i.tm.extension_opcode = None;
6624
0
      i.tm.opcode_modifier.evex = 0;
6625
0
      i.tm.opcode_modifier.vexvvvv = 0;
6626
0
      if (i.imm_operands != 0)
6627
0
  i.index_reg = i.base_reg = i.op[1].regs;
6628
0
      else if (!is_index (i.op[0].regs)
6629
0
         || (i.op[1].regs->reg_num == 5
6630
0
       && i.op[0].regs->reg_num != 5))
6631
0
  {
6632
0
    i.base_reg = i.op[0].regs;
6633
0
    i.index_reg = i.op[1].regs;
6634
0
  }
6635
0
      else
6636
0
  {
6637
0
    i.base_reg = i.op[1].regs;
6638
0
    i.index_reg = i.op[0].regs;
6639
0
  }
6640
0
      if (i.types[1].bitfield.word)
6641
0
  {
6642
    /* NB: No similar adjustment is needed when operand size is 32-bit.  */
6643
0
    i.base_reg += 64;
6644
0
    i.index_reg += 64;
6645
0
  }
6646
0
      i.op[1].regs = i.op[i.operands - 1].regs;
6647
6648
0
      operand_type_set (&i.types[0], 0);
6649
0
      i.types[0].bitfield.baseindex = 1;
6650
0
      i.tm_types[0] = i.types[0];
6651
0
      i.op[0].disps = NULL;
6652
0
      i.flags[0] = Operand_Mem;
6653
6654
0
      i.operands = 2;
6655
0
      i.mem_operands = i.reg_operands = 1;
6656
0
      i.imm_operands = 0;
6657
0
      pp.has_nf = false;
6658
0
    }
6659
0
  else if (optimize_for_space
6660
0
     && pp.encoding != encoding_evex
6661
0
     && (i.tm.base_opcode == 0x80 || i.tm.base_opcode == 0x83)
6662
0
     && (i.tm.extension_opcode == 0
6663
0
         || (i.tm.extension_opcode == 5
6664
0
       && i.op[0].imms->X_op == O_constant
6665
       /* Subtraction of -0x80 will end up smaller only if neither
6666
          operand size nor REX/REX2 prefixes are needed.  */
6667
0
       && (i.op[0].imms->X_add_number != -0x80
6668
0
           || (i.types[1].bitfield.dword
6669
0
               && !(i.op[1].regs->reg_flags & RegRex)
6670
0
               && !(i.op[i.operands - 1].regs->reg_flags & RegRex)
6671
0
               && pp.encoding != encoding_egpr))))
6672
0
     && !i.mem_operands
6673
0
     && !i.types[1].bitfield.byte
6674
     /* 16-bit operand size has extra restrictions: If REX2 was needed,
6675
        no size reduction would be possible.  Plus 3-operand forms zero-
6676
        extend the result, which can't be expressed with LEA.  */
6677
0
     && (!i.types[1].bitfield.word
6678
0
         || (i.operands == 2 && pp.encoding != encoding_egpr))
6679
0
     && is_plausible_suffix (1))
6680
0
    {
6681
      /* Optimize: -Os:
6682
     {nf} addw $N, %M   -> leaw N(%rM), %M
6683
     {nf} addl $N, %eM  -> leal N(%rM), %eM
6684
     {nf} addq $N, %rM  -> leaq N(%rM), %rM
6685
6686
     {nf} subw $N, %M   -> leaw -N(%rM), %M
6687
     {nf} subl $N, %eM  -> leal -N(%rM), %eM
6688
     {nf} subq $N, %rM  -> leaq -N(%rM), %rM
6689
6690
     {nf} addl $N, %eK, %eM  -> leal N(%rK), %eM
6691
     {nf} addq $N, %rK, %rM  -> leaq N(%rK), %rM
6692
6693
     {nf} subl $N, %eK, %eM  -> leal -N(%rK), %eM
6694
     {nf} subq $N, %rK, %rM  -> leaq -N(%rK), %rM
6695
       */
6696
0
      i.tm.opcode_space = SPACE_BASE;
6697
0
      i.tm.base_opcode = 0x8d;
6698
0
      if (i.tm.extension_opcode == 5)
6699
0
  i.op[0].imms->X_add_number = -i.op[0].imms->X_add_number;
6700
0
      i.tm.extension_opcode = None;
6701
0
      i.tm.opcode_modifier.evex = 0;
6702
0
      i.tm.opcode_modifier.vexvvvv = 0;
6703
0
      i.base_reg = i.op[1].regs;
6704
0
      if (i.types[1].bitfield.word)
6705
0
  {
6706
    /* NB: No similar adjustment is needed when operand size is 32-bit.  */
6707
0
    i.base_reg += 64;
6708
0
  }
6709
0
      i.op[1].regs = i.op[i.operands - 1].regs;
6710
6711
0
      operand_type_set (&i.types[0], 0);
6712
0
      i.types[0].bitfield.baseindex = 1;
6713
0
      i.types[0].bitfield.disp32 = 1;
6714
0
      i.op[0].disps = i.op[0].imms;
6715
0
      i.flags[0] = Operand_Mem;
6716
0
      optimize_disp (&i.tm);
6717
0
      i.tm_types[0] = i.types[0];
6718
6719
0
      i.operands = 2;
6720
0
      i.disp_operands = i.mem_operands = i.reg_operands = 1;
6721
0
      i.imm_operands = 0;
6722
0
      pp.has_nf = false;
6723
0
    }
6724
0
  else if (i.tm.base_opcode == 0x6b
6725
0
     && !i.mem_operands
6726
0
     && pp.encoding != encoding_evex
6727
0
     && i.tm.mnem_off != MN_imulzu
6728
0
     && is_plausible_suffix (1)
6729
     /* %rsp can't be the index.  */
6730
0
     && is_index (i.op[1].regs)
6731
     /* There's no reduction in size for 16-bit forms requiring Disp8 and
6732
        REX2.  */
6733
0
     && (!optimize_for_space
6734
0
         || !i.types[1].bitfield.word
6735
0
         || i.op[1].regs->reg_num != 5
6736
0
         || pp.encoding != encoding_egpr)
6737
0
     && i.op[0].imms->X_op == O_constant
6738
0
     && (i.op[0].imms->X_add_number == 3
6739
0
         || i.op[0].imms->X_add_number == 5
6740
0
         || i.op[0].imms->X_add_number == 9))
6741
0
    {
6742
      /* Optimize: -O:
6743
        For n one of 3, 5, or 9
6744
     {nf} imulw $n, %N, %M    -> leaw (%rN,%rN,n-1), %M
6745
     {nf} imull $n, %eN, %eM  -> leal (%rN,%rN,n-1), %eM
6746
     {nf} imulq $n, %rN, %rM  -> leaq (%rN,%rN,n-1), %rM
6747
6748
     {nf} imulw $n, %N   -> leaw (%rN,%rN,s), %N
6749
     {nf} imull $n, %eN  -> leal (%rN,%rN,s), %eN
6750
     {nf} imulq $n, %rN  -> leaq (%rN,%rN,s), %rN
6751
       */
6752
0
      i.tm.opcode_space = SPACE_BASE;
6753
0
      i.tm.base_opcode = 0x8d;
6754
0
      i.tm.extension_opcode = None;
6755
0
      i.tm.opcode_modifier.evex = 0;
6756
0
      i.base_reg = i.op[1].regs;
6757
      /* NB: No similar adjustment is needed when operand size is 32 bits.  */
6758
0
      if (i.types[1].bitfield.word)
6759
0
  i.base_reg += 64;
6760
0
      i.index_reg = i.base_reg;
6761
0
      i.log2_scale_factor = i.op[0].imms->X_add_number == 9
6762
0
          ? 3 : i.op[0].imms->X_add_number >> 1;
6763
6764
0
      operand_type_set (&i.types[0], 0);
6765
0
      i.types[0].bitfield.baseindex = 1;
6766
0
      i.tm_types[0] = i.types[0];
6767
0
      i.op[0].disps = NULL;
6768
0
      i.flags[0] = Operand_Mem;
6769
6770
0
      copy_operand (1, i.operands - 1);
6771
6772
0
      i.operands = 2;
6773
0
      i.mem_operands = i.reg_operands = 1;
6774
0
      i.imm_operands = 0;
6775
0
      pp.has_nf = false;
6776
0
    }
6777
0
  else if (cpu_arch_isa_flags.bitfield.cpubmi2
6778
0
     && pp.encoding == encoding_default
6779
0
     && (i.operands > 2 || !i.mem_operands)
6780
0
     && (i.types[i.operands - 1].bitfield.dword
6781
0
         || i.types[i.operands - 1].bitfield.qword))
6782
0
    {
6783
0
      if (i.tm.base_opcode == 0xd2)
6784
0
  {
6785
    /* Optimize: -O:
6786
         <OP> one of sal, sar, shl, shr:
6787
         {nf} <OP> %cl, %rN       -> <OP>x %{e,r}cx, %rN, %rN (N < 16)
6788
         {nf} <OP> %cl, ..., %rN  -> <OP>x %{e,r}cx, ..., %rN (no eGPR used)
6789
     */
6790
0
    gas_assert (i.tm.extension_opcode & 4);
6791
0
    i.tm_types[0] = i.tm_types[i.operands - 1];
6792
    /* NB: i.op[0].regs specifying %cl is good enough.  */
6793
0
    i.types[0] = i.types[i.operands - 1];
6794
0
    if (i.operands == 2)
6795
0
      {
6796
0
        i.tm_types[0].bitfield.baseindex = 0;
6797
0
        i.tm_types[2] = i.tm_types[0];
6798
0
        i.op[2].regs = i.op[1].regs;
6799
0
        i.types[2] = i.types[1];
6800
0
        i.reg_operands = i.operands = 3;
6801
0
      }
6802
0
    pp.has_nf = false;
6803
0
    i.tm.opcode_modifier.w = 0;
6804
0
    i.tm.opcode_modifier.evex = 0;
6805
0
    i.tm.opcode_modifier.vex = VEX128;
6806
0
    i.tm.opcode_modifier.vexvvvv = VexVVVV_SRC2;
6807
0
    i.tm.opcode_space = SPACE_0F38;
6808
0
    i.tm.base_opcode = 0xf7;
6809
0
    i.tm.opcode_modifier.opcodeprefix
6810
0
      = !(i.tm.extension_opcode & 1)
6811
0
        ? PREFIX_0X66 /* shlx */
6812
0
        : i.tm.extension_opcode & 2
6813
0
    ? PREFIX_0XF3 /* sarx */
6814
0
    : PREFIX_0XF2 /* shrx */;
6815
0
    i.tm.extension_opcode = None;
6816
0
  }
6817
0
      else if (i.tm.base_opcode == 0xc0
6818
0
         && i.tm.extension_opcode <= 1
6819
0
         && i.op[0].imms->X_op == O_constant)
6820
0
  {
6821
    /* Optimize: -O:
6822
         {nf} rol $I, %rN       -> rorx $osz-I, %rN, %rN (I != osz-1, N < 16)
6823
         {nf} rol $I, ..., %rN  -> rorx $osz-I, ..., %rN (I != osz-1, no eGPR used)
6824
         {nf} ror $I, %rN       -> rorx $I, %rN, %rN (I != 1, N < 16)
6825
         {nf} ror $I, ..., %rN  -> rorx $I,..., %rN (I != 1, no eGPR used)
6826
       NB: rol -> ror transformation for I == osz-1 was already handled above.
6827
       NB2: ror with an immediate of 1 uses a different base opcode.
6828
     */
6829
0
    if (i.operands == 2)
6830
0
      {
6831
0
        copy_operand (2, 1);
6832
0
        i.tm_types[2].bitfield.baseindex = 0;
6833
0
        i.reg_operands = 2;
6834
0
        i.operands = 3;
6835
0
      }
6836
0
    pp.has_nf = false;
6837
0
    i.tm.opcode_modifier.w = 0;
6838
0
    i.tm.opcode_modifier.evex = 0;
6839
0
    i.tm.opcode_modifier.vex = VEX128;
6840
0
    i.tm.opcode_modifier.vexvvvv = 0;
6841
0
    i.tm.opcode_space = SPACE_0F3A;
6842
0
    i.tm.base_opcode = 0xf0;
6843
0
    i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF2;
6844
0
    if (!i.tm.extension_opcode)
6845
0
      i.op[0].imms->X_add_number =
6846
0
        (i.types[i.operands - 1].bitfield.byte
6847
0
         ? 8 : i.types[i.operands - 1].bitfield.word
6848
0
         ? 16 : 64 >> i.types[i.operands - 1].bitfield.dword)
6849
0
        - i.op[0].imms->X_add_number;
6850
0
    i.tm.extension_opcode = None;
6851
0
  }
6852
0
      else if (i.tm.base_opcode == 0xf6
6853
0
         && i.tm.extension_opcode == 4
6854
0
         && !i.mem_operands
6855
0
         && i.op[0].regs->reg_num == 2
6856
0
         && !(i.op[0].regs->reg_flags & RegRex) )
6857
0
  {
6858
    /* Optimize: -O:
6859
         {nf} mul %edx  -> mulx %eax, %eax, %edx
6860
         {nf} mul %rdx  -> mulx %rax, %rax, %rdx
6861
     */
6862
0
    i.tm_types[1] = i.tm_types[0];
6863
0
    i.tm_types[1].bitfield.baseindex = 0;
6864
0
    i.tm_types[2] = i.tm_types[1];
6865
0
    i.op[2].regs = i.op[0].regs;
6866
    /* NB: %eax is good enough also for 64-bit operand size.  */
6867
0
    i.op[1].regs = i.op[0].regs = reg_eax;
6868
0
    i.types[2] = i.types[1] = i.types[0];
6869
0
    i.reg_operands = i.operands = 3;
6870
6871
0
    pp.has_nf = false;
6872
0
    i.tm.opcode_modifier.w = 0;
6873
0
    i.tm.opcode_modifier.evex = 0;
6874
0
    i.tm.opcode_modifier.vex = VEX128;
6875
0
    i.tm.opcode_modifier.vexvvvv = VexVVVV_SRC1;
6876
0
    i.tm.opcode_space = SPACE_0F38;
6877
0
    i.tm.base_opcode = 0xf6;
6878
0
    i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF2;
6879
0
    i.tm.extension_opcode = None;
6880
0
  }
6881
0
    }
6882
0
}
6883
6884
static void
6885
s_noopt (int dummy ATTRIBUTE_UNUSED)
6886
0
{
6887
0
  if (!is_it_end_of_statement ())
6888
0
    as_warn (_("`.noopt' arguments ignored"));
6889
6890
0
  optimize = 0;
6891
0
  optimize_for_space = 0;
6892
6893
0
  ignore_rest_of_line ();
6894
0
}
6895
6896
/* Return non-zero for load instruction.  */
6897
6898
static int
6899
load_insn_p (void)
6900
0
{
6901
0
  unsigned int dest;
6902
0
  unsigned int base_opcode = i.tm.base_opcode | 1;
6903
6904
  /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
6905
     bndcn, bndstx, bndldx, clflushopt, clwb, cldemote.  */
6906
0
  if (i.tm.opcode_modifier.operandconstraint == ANY_SIZE)
6907
0
    return 0;
6908
6909
0
  if (i.tm.opcode_space == SPACE_BASE)
6910
0
    {
6911
      /* popf, popa.   */
6912
0
      if (i.tm.base_opcode == 0x9d
6913
0
    || i.tm.base_opcode == 0x61)
6914
0
  return 1;
6915
6916
      /* movs, cmps, lods, scas.  */
6917
0
      if ((base_opcode | 0xa) == 0xaf)
6918
0
  return 1;
6919
6920
      /* outs, xlatb.  */
6921
0
      if (base_opcode == 0x6f
6922
0
    || i.tm.base_opcode == 0xd7)
6923
0
  return 1;
6924
      /* NB: For AMD-specific insns with implicit memory operands,
6925
   they're intentionally not covered.  */
6926
0
    }
6927
6928
  /* No memory operand.  */
6929
0
  if (!i.mem_operands)
6930
0
    {
6931
      /* pop.   */
6932
0
      if (i.tm.mnem_off == MN_pop)
6933
0
  return 1;
6934
6935
0
    return 0;
6936
0
    }
6937
6938
0
  if (is_any_vex_encoding (&i.tm))
6939
0
    {
6940
0
      if (i.tm.mnem_off == MN_vldmxcsr)
6941
0
  return 1;
6942
0
    }
6943
0
  else if (i.tm.opcode_space == SPACE_BASE)
6944
0
    {
6945
      /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg.  */
6946
0
      if ((base_opcode | 0x38) == 0x39
6947
0
    || (base_opcode | 2) == 0x87)
6948
0
  return 1;
6949
6950
      /* test, neg, mul, imul, div, idiv.  */
6951
0
      if (base_opcode == 0xf7 && i.tm.extension_opcode != 2)
6952
0
  return 1;
6953
6954
      /* inc, dec.  */
6955
0
      if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
6956
0
  return 1;
6957
6958
      /* add, or, adc, sbb, and, sub, xor, cmp.  */
6959
0
      if ((base_opcode | 2) == 0x83)
6960
0
  return 1;
6961
6962
      /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
6963
0
      if ((base_opcode == 0xc1 || (base_opcode | 2) == 0xd3)
6964
0
    && (!operand_type_check (i.types[0], imm)
6965
0
        || i.op[0].imms->X_op != O_constant
6966
0
        || i.op[0].imms->X_add_number != 0))
6967
0
  return 1;
6968
6969
      /* arpl.  (Also movxsd, which otherwise would be covered towards
6970
   the end of the function.)  */
6971
0
      if (i.tm.base_opcode == 0x63)
6972
0
  return 1;
6973
6974
      /* Check for x87 instructions.  */
6975
0
      if ((base_opcode | 6) == 0xdf)
6976
0
  {
6977
    /* Skip fst, fstp, fstenv, fstcw.  */
6978
0
    if (i.tm.base_opcode == 0xd9
6979
0
        && (i.tm.extension_opcode == 2
6980
0
      || i.tm.extension_opcode == 3
6981
0
      || i.tm.extension_opcode == 6
6982
0
      || i.tm.extension_opcode == 7))
6983
0
      return 0;
6984
6985
    /* Skip fisttp, fist, fistp, fstp.  */
6986
0
    if (i.tm.base_opcode == 0xdb
6987
0
        && (i.tm.extension_opcode == 1
6988
0
      || i.tm.extension_opcode == 2
6989
0
      || i.tm.extension_opcode == 3
6990
0
      || i.tm.extension_opcode == 7))
6991
0
      return 0;
6992
6993
    /* Skip fisttp, fst, fstp, fsave, fstsw.  */
6994
0
    if (i.tm.base_opcode == 0xdd
6995
0
        && (i.tm.extension_opcode == 1
6996
0
      || i.tm.extension_opcode == 2
6997
0
      || i.tm.extension_opcode == 3
6998
0
      || i.tm.extension_opcode == 6
6999
0
      || i.tm.extension_opcode == 7))
7000
0
      return 0;
7001
7002
    /* Skip fisttp, fist, fistp, fbstp, fistp.  */
7003
0
    if (i.tm.base_opcode == 0xdf
7004
0
        && (i.tm.extension_opcode == 1
7005
0
      || i.tm.extension_opcode == 2
7006
0
      || i.tm.extension_opcode == 3
7007
0
      || i.tm.extension_opcode == 6
7008
0
      || i.tm.extension_opcode == 7))
7009
0
      return 0;
7010
7011
0
    return 1;
7012
0
  }
7013
0
    }
7014
0
  else if (i.tm.opcode_space == SPACE_0F)
7015
0
    {
7016
      /* bt, bts, btr, btc.  */
7017
0
      if ((i.tm.base_opcode == 0xba
7018
0
     && (i.tm.extension_opcode | 3) == 7)
7019
0
    || (i.tm.base_opcode | 0x18) == 0xbb)
7020
0
  return 1;
7021
7022
      /* shld, shrd.  */
7023
0
      if ((base_opcode | 8) == 0xad
7024
0
    && (!operand_type_check (i.types[0], imm)
7025
0
        || i.op[0].imms->X_op != O_constant
7026
0
        || i.op[0].imms->X_add_number != 0))
7027
0
  return 1;
7028
7029
      /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld, vmxon, vmclear, vmptrst.  */
7030
0
      if (i.tm.base_opcode == 0xc7
7031
0
    && ((i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
7032
0
         && ((i.tm.extension_opcode | 2) == 3))
7033
0
        || (i.tm.extension_opcode | 1) == 7))
7034
0
  return 1;
7035
7036
      /* vmread.  (Also vcvtts{s,d}2usi and vcvttp{s,d}2uqq, which otherwise
7037
   would be covered towards the end of the function.)  */
7038
0
      if (i.tm.base_opcode == 0x78)
7039
0
  return 1;
7040
7041
      /* fxrstor, ldmxcsr, xrstor, xsave, xsaveopt.  */
7042
0
      if (i.tm.base_opcode == 0xae
7043
0
    && (i.tm.extension_opcode == 1
7044
0
        || i.tm.extension_opcode == 2
7045
0
        || i.tm.extension_opcode == 5
7046
0
        || (i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
7047
0
      && (i.tm.extension_opcode | 2) == 6)))
7048
0
  return 1;
7049
7050
      /* lldt, verr, verw.  */
7051
0
      if (i.tm.base_opcode == 0x00
7052
0
    && (i.tm.extension_opcode == 2
7053
0
        || (i.tm.extension_opcode | 1) == 5))
7054
0
  return 1;
7055
7056
      /* lgdt, lmsw.  */
7057
0
      if (i.tm.base_opcode == 0x01
7058
0
    && (i.tm.extension_opcode | 4) == 6)
7059
0
  return 1;
7060
7061
      /* cmpxchg, xadd.  */
7062
0
      if (base_opcode == 0xb1 || base_opcode == 0xc1)
7063
0
  return 1;
7064
0
    }
7065
0
  else if (i.tm.opcode_space == SPACE_0F38)
7066
0
    {
7067
      /* invpcid.  */
7068
0
      if (i.tm.base_opcode == 0x82
7069
0
    && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
7070
0
  return 0;
7071
0
    }
7072
7073
0
  dest = i.operands - 1;
7074
7075
  /* Check fake imm8 operand and 3 source operands.  */
7076
0
  if ((i.tm.opcode_modifier.immext
7077
0
       || i.reg_operands + i.mem_operands == 4)
7078
0
      && i.types[dest].bitfield.imm8)
7079
0
    dest--;
7080
7081
  /* Check for load instruction.  */
7082
0
  return (i.types[dest].bitfield.class != ClassNone
7083
0
    || i.types[dest].bitfield.instance == Accum);
7084
0
}
7085
7086
/* Output lfence, 0xfaee8, after instruction.  */
7087
7088
static void
7089
insert_lfence_after (void)
7090
5.65k
{
7091
5.65k
  if (lfence_after_load && load_insn_p ())
7092
0
    {
7093
      /* There are also two REP string instructions that require
7094
   special treatment. Specifically, the compare string (CMPS)
7095
   and scan string (SCAS) instructions set EFLAGS in a manner
7096
   that depends on the data being compared/scanned. When used
7097
   with a REP prefix, the number of iterations may therefore
7098
   vary depending on this data. If the data is a program secret
7099
   chosen by the adversary using an LVI method,
7100
   then this data-dependent behavior may leak some aspect
7101
   of the secret.  */
7102
0
      if (((i.tm.base_opcode | 0x9) == 0xaf)
7103
0
    && i.prefix[REP_PREFIX])
7104
0
  {
7105
0
      as_warn (_("`%s` changes flags which would affect control flow behavior"),
7106
0
         insn_name (&i.tm));
7107
0
  }
7108
0
      char *p = frag_more (3);
7109
0
      *p++ = 0xf;
7110
0
      *p++ = 0xae;
7111
0
      *p = 0xe8;
7112
0
    }
7113
5.65k
}
7114
7115
/* Output lfence, 0xfaee8, before instruction.  */
7116
7117
static void
7118
insert_lfence_before (const struct last_insn *last_insn)
7119
5.65k
{
7120
5.65k
  char *p;
7121
7122
5.65k
  if (i.tm.opcode_space != SPACE_BASE)
7123
89
    return;
7124
7125
5.56k
  if (i.tm.base_opcode == 0xff
7126
27
      && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
7127
0
    {
7128
      /* Insert lfence before indirect branch if needed.  */
7129
7130
0
      if (lfence_before_indirect_branch == lfence_branch_none)
7131
0
  return;
7132
7133
0
      if (i.operands != 1)
7134
0
  abort ();
7135
7136
0
      if (i.reg_operands == 1)
7137
0
  {
7138
    /* Indirect branch via register.  Don't insert lfence with
7139
       -mlfence-after-load=yes.  */
7140
0
    if (lfence_after_load
7141
0
        || lfence_before_indirect_branch == lfence_branch_memory)
7142
0
      return;
7143
0
  }
7144
0
      else if (i.mem_operands == 1
7145
0
         && lfence_before_indirect_branch != lfence_branch_register)
7146
0
  {
7147
0
    as_warn (_("indirect `%s` with memory operand should be avoided"),
7148
0
       insn_name (&i.tm));
7149
0
    return;
7150
0
  }
7151
0
      else
7152
0
  return;
7153
7154
0
      if (last_insn->kind != last_insn_other)
7155
0
  {
7156
0
    as_warn_where (last_insn->file, last_insn->line,
7157
0
       _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
7158
0
       last_insn->name, insn_name (&i.tm));
7159
0
    return;
7160
0
  }
7161
7162
0
      p = frag_more (3);
7163
0
      *p++ = 0xf;
7164
0
      *p++ = 0xae;
7165
0
      *p = 0xe8;
7166
0
      return;
7167
0
    }
7168
7169
  /* Output or/not/shl and lfence before near ret.  */
7170
5.56k
  if (lfence_before_ret != lfence_before_ret_none
7171
0
      && (i.tm.base_opcode | 1) == 0xc3)
7172
0
    {
7173
0
      if (last_insn->kind != last_insn_other)
7174
0
  {
7175
0
    as_warn_where (last_insn->file, last_insn->line,
7176
0
       _("`%s` skips -mlfence-before-ret on `%s`"),
7177
0
       last_insn->name, insn_name (&i.tm));
7178
0
    return;
7179
0
  }
7180
7181
      /* Near ret ingore operand size override under CPU64.  */
7182
0
      char prefix = flag_code == CODE_64BIT
7183
0
        ? 0x48
7184
0
        : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
7185
7186
0
      if (lfence_before_ret == lfence_before_ret_not)
7187
0
  {
7188
    /* not: 0xf71424, may add prefix
7189
       for operand size override or 64-bit code.  */
7190
0
    p = frag_more ((prefix ? 2 : 0) + 6 + 3);
7191
0
    if (prefix)
7192
0
      *p++ = prefix;
7193
0
    *p++ = 0xf7;
7194
0
    *p++ = 0x14;
7195
0
    *p++ = 0x24;
7196
0
    if (prefix)
7197
0
      *p++ = prefix;
7198
0
    *p++ = 0xf7;
7199
0
    *p++ = 0x14;
7200
0
    *p++ = 0x24;
7201
0
  }
7202
0
      else
7203
0
  {
7204
0
    p = frag_more ((prefix ? 1 : 0) + 4 + 3);
7205
0
    if (prefix)
7206
0
      *p++ = prefix;
7207
0
    if (lfence_before_ret == lfence_before_ret_or)
7208
0
      {
7209
        /* or: 0x830c2400, may add prefix
7210
     for operand size override or 64-bit code.  */
7211
0
        *p++ = 0x83;
7212
0
        *p++ = 0x0c;
7213
0
      }
7214
0
    else
7215
0
      {
7216
        /* shl: 0xc1242400, may add prefix
7217
     for operand size override or 64-bit code.  */
7218
0
        *p++ = 0xc1;
7219
0
        *p++ = 0x24;
7220
0
      }
7221
7222
0
    *p++ = 0x24;
7223
0
    *p++ = 0x0;
7224
0
  }
7225
7226
0
      *p++ = 0xf;
7227
0
      *p++ = 0xae;
7228
0
      *p = 0xe8;
7229
0
    }
7230
5.56k
}
7231
7232
/* Shared helper for md_assemble() and s_insn().  */
7233
static void init_globals (void)
7234
111k
{
7235
111k
  unsigned int j;
7236
7237
111k
  memset (&i, '\0', sizeof (i));
7238
111k
  i.rounding.type = rc_none;
7239
666k
  for (j = 0; j < MAX_OPERANDS; j++)
7240
555k
    i.reloc[j] = NO_RELOC;
7241
111k
  memset (disp_expressions, '\0', sizeof (disp_expressions));
7242
111k
  memset (im_expressions, '\0', sizeof (im_expressions));
7243
111k
  save_stack_p = save_stack;
7244
111k
}
7245
7246
/* Helper for md_assemble() to decide whether to prepare for a possible 2nd
7247
   parsing pass. Instead of introducing a rarely used new insn attribute this
7248
   utilizes a common pattern between affected templates. It is deemed
7249
   acceptable that this will lead to unnecessary pass 2 preparations in a
7250
   limited set of cases.  */
7251
static INLINE bool may_need_pass2 (const insn_template *t)
7252
25.1k
{
7253
25.1k
  return t->opcode_modifier.sse2avx
7254
   /* Note that all SSE2AVX templates have at least one operand.  */
7255
25.1k
   ? get_operand_types (t)[t->operands - 1].bitfield.class == RegSIMD
7256
25.1k
   : (t->opcode_space == SPACE_0F
7257
504
      && (t->base_opcode | 1) == 0xbf)
7258
24.7k
     || (t->opcode_space == SPACE_BASE
7259
10.4k
         && t->base_opcode == 0x63)
7260
24.7k
     || (intel_syntax /* shld / shrd may mean suffixed shl / shr.  */
7261
22.5k
         && t->opcode_space == SPACE_MAP4
7262
11.8k
         && (t->base_opcode | 8) == 0x2c);
7263
25.1k
}
7264
7265
#ifdef OBJ_ELF
7266
static enum x86_tls_error_type
7267
x86_check_tls_relocation (enum bfd_reloc_code_real r_type)
7268
15
{
7269
15
  switch (r_type)
7270
15
    {
7271
0
    case BFD_RELOC_386_TLS_GOTDESC:
7272
      /* Check GDesc access model:
7273
7274
   leal x@tlsdesc(%ebx), %reg32 --> Memory reg must be %ebx and
7275
            SIB is not supported.
7276
       */
7277
0
      if (i.tm.mnem_off != MN_lea)
7278
0
  return x86_tls_error_insn;
7279
0
      if (i.index_reg)
7280
0
  return x86_tls_error_sib;
7281
0
      if (!i.base_reg)
7282
0
  return x86_tls_error_no_base_reg;
7283
0
      if (i.base_reg->reg_type.bitfield.instance != RegB)
7284
0
  return x86_tls_error_ebx;
7285
0
      if (!i.types[1].bitfield.dword)
7286
0
  return x86_tls_error_dest_32bit_reg_size;
7287
0
      break;
7288
7289
0
    case BFD_RELOC_386_TLS_GD:
7290
      /* Check GD access model:
7291
7292
   leal foo@tlsgd(,%ebx,1), %eax   --> Only this fixed format is supported.
7293
   leal foo@tlsgd(%reg32), %eax    --> Dest reg must be '%eax'
7294
               Memory reg can't be %eax.
7295
       */
7296
0
      if (i.tm.mnem_off != MN_lea)
7297
0
  return x86_tls_error_insn;
7298
0
      if (i.types[1].bitfield.instance != Accum)
7299
0
  return x86_tls_error_dest_eax;
7300
0
      if (!i.types[1].bitfield.dword)
7301
0
  return x86_tls_error_dest_32bit_reg_size;
7302
0
      if (i.index_reg)
7303
0
  {
7304
0
    if (i.base_reg)
7305
0
      return x86_tls_error_base_reg;
7306
0
    if (i.index_reg->reg_type.bitfield.instance != RegB)
7307
0
      return x86_tls_error_index_ebx;
7308
0
    if (i.log2_scale_factor)
7309
0
      return x86_tls_error_scale_factor;
7310
0
  }
7311
0
      else
7312
0
  {
7313
0
    if (!i.base_reg)
7314
0
      return x86_tls_error_no_base_reg;
7315
0
    if (i.base_reg->reg_type.bitfield.instance == Accum)
7316
0
      return x86_tls_error_eax;
7317
0
  }
7318
0
      break;
7319
7320
0
    case BFD_RELOC_386_TLS_LDM:
7321
      /*  Check LDM access model:
7322
7323
    leal foo@tlsldm(%reg32), %eax --> Dest reg must be '%eax'
7324
                    Memory reg can't be %eax and SIB
7325
              is not supported.
7326
       */
7327
0
      if (i.tm.mnem_off != MN_lea)
7328
0
  return x86_tls_error_insn;
7329
0
      if (i.index_reg)
7330
0
  return x86_tls_error_sib;
7331
0
      if (!i.base_reg)
7332
0
  return x86_tls_error_no_base_reg;
7333
0
      if (i.base_reg->reg_type.bitfield.instance == Accum)
7334
0
  return x86_tls_error_eax;
7335
0
      if (i.types[1].bitfield.instance != Accum)
7336
0
  return x86_tls_error_dest_eax;
7337
0
      if (!i.types[1].bitfield.dword)
7338
0
  return x86_tls_error_dest_32bit_reg_size;
7339
0
      break;
7340
7341
0
    case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
7342
      /* Check GOTPC32 TLSDESC access model:
7343
7344
   --- LP64 mode ---
7345
   leaq x@tlsdesc(%rip), %reg64 --> Memory reg must be %rip.
7346
7347
   --- X32 mode ---
7348
   rex/rex2 leal x@tlsdesc(%rip), %reg32 --> Memory reg must be %rip.
7349
7350
   In X32 mode, gas will add rex/rex2 for it later, no need to check
7351
   here.
7352
       */
7353
0
      if (i.tm.mnem_off != MN_lea)
7354
0
  return x86_tls_error_insn;
7355
0
      if (!i.base_reg)
7356
0
  return x86_tls_error_no_base_reg;
7357
0
      if (i.base_reg->reg_num != RegIP
7358
0
    || !i.base_reg->reg_type.bitfield.qword)
7359
0
  return x86_tls_error_rip;
7360
0
      if (x86_elf_abi == X86_64_ABI)
7361
0
  {
7362
0
    if (!i.types[1].bitfield.qword)
7363
0
      return x86_tls_error_dest_64bit_reg_size;
7364
0
  }
7365
0
      else if (!i.types[1].bitfield.dword
7366
0
         && !i.types[1].bitfield.qword)
7367
0
  return x86_tls_error_dest_32bit_or_64bit_reg_size;
7368
0
    break;
7369
7370
0
    case BFD_RELOC_X86_64_TLSGD:
7371
      /* Check GD access model:
7372
7373
   leaq foo@tlsgd(%rip), %rdi --> Only this fixed format is supported.
7374
       */
7375
1
    case BFD_RELOC_X86_64_TLSLD:
7376
      /* Check LD access model:
7377
7378
   leaq foo@tlsld(%rip), %rdi --> Only this fixed format is supported.
7379
       */
7380
1
      if (i.tm.mnem_off != MN_lea)
7381
1
  return x86_tls_error_insn;
7382
0
      if (!i.base_reg)
7383
0
  return x86_tls_error_no_base_reg;
7384
0
      if (i.base_reg->reg_num != RegIP
7385
0
    || !i.base_reg->reg_type.bitfield.qword)
7386
0
  return x86_tls_error_rip;
7387
0
      if (!i.types[1].bitfield.qword
7388
0
    || i.op[1].regs->reg_num != EDI_REG_NUM
7389
0
    || i.op[1].regs->reg_flags)
7390
0
  return x86_tls_error_dest_rdi;
7391
0
      break;
7392
7393
0
    case BFD_RELOC_386_TLS_GOTIE:
7394
      /* Check GOTIE access model:
7395
7396
   subl foo@gotntpoff(%reg1), %reg2
7397
   movl foo@gotntpoff(%reg1), %reg2
7398
   addl foo@gotntpoff(%reg1), %reg2
7399
7400
   Memory operand: SIB is not supported.
7401
       */
7402
0
    case BFD_RELOC_386_TLS_IE_32:
7403
      /* Check IE_32 access model:
7404
7405
   subl foo@gottpoff(%reg1), %reg2
7406
   movl foo@gottpoff(%reg1), %reg2
7407
   addl foo@gottpoff(%reg1), %reg2
7408
7409
   Memory operand: SIB is not supported.
7410
       */
7411
0
      if (i.tm.mnem_off != MN_sub
7412
0
    && i.tm.mnem_off != MN_add
7413
0
    && i.tm.mnem_off != MN_mov)
7414
0
  return x86_tls_error_insn;
7415
0
      if (i.imm_operands
7416
0
    || i.disp_operands != 1
7417
0
    || i.reg_operands != 1
7418
0
    || i.types[1].bitfield.class != Reg)
7419
0
  return x86_tls_error_opcode;
7420
0
      if (!i.base_reg)
7421
0
  return x86_tls_error_no_base_reg;
7422
0
      if (i.index_reg)
7423
0
  return x86_tls_error_sib;
7424
0
      if (!i.base_reg->reg_type.bitfield.dword)
7425
0
  return x86_tls_error_base_reg_size;
7426
0
      if (!i.types[1].bitfield.dword)
7427
0
  return x86_tls_error_dest_32bit_reg_size;
7428
0
      break;
7429
7430
0
    case BFD_RELOC_386_TLS_IE:
7431
      /* Check IE access model:
7432
7433
   movl foo@indntpoff, %reg32 --> Mod == 00 && r/m == 5
7434
   addl foo@indntpoff, %reg32 --> Mod == 00 && r/m == 5
7435
       */
7436
0
      if (i.tm.mnem_off != MN_add && i.tm.mnem_off != MN_mov)
7437
0
  return x86_tls_error_insn;
7438
0
      if (i.imm_operands
7439
0
    || i.disp_operands != 1
7440
0
    || i.reg_operands != 1
7441
0
    || i.types[1].bitfield.class != Reg)
7442
0
  return x86_tls_error_opcode;
7443
0
      if (i.base_reg || i.index_reg)
7444
0
  return x86_tls_error_require_no_base_index_reg;
7445
0
      if (!i.types[1].bitfield.dword)
7446
0
  return x86_tls_error_dest_32bit_reg_size;
7447
0
      break;
7448
7449
0
    case BFD_RELOC_X86_64_GOTTPOFF:
7450
      /* Check GOTTPOFF access model:
7451
7452
   mov foo@gottpoff(%rip), %reg --> Memory Reg must be %rip.
7453
   movrs foo@gottpoff(%rip), %reg --> Memory Reg must be %rip.
7454
   add foo@gottpoff(%rip), %reg --> Memory Reg must be %rip.
7455
   add %reg1, foo@gottpoff(%rip), %reg2 --> Memory Reg must be %rip.
7456
   add foo@gottpoff(%rip), %reg1, %reg2 --> Memory Reg must be %rip.
7457
       */
7458
0
      if (i.tm.mnem_off != MN_add && i.tm.mnem_off != MN_mov
7459
0
    && i.tm.mnem_off != MN_movrs)
7460
0
  return x86_tls_error_insn;
7461
0
      if (i.imm_operands
7462
0
    || i.disp_operands != 1
7463
0
    || i.types[i.operands - 1].bitfield.class != Reg)
7464
0
  return x86_tls_error_opcode;
7465
0
      if (!i.base_reg)
7466
0
  return x86_tls_error_no_base_reg;
7467
0
      if (i.base_reg->reg_num != RegIP
7468
0
    || !i.base_reg->reg_type.bitfield.qword)
7469
0
  return x86_tls_error_rip;
7470
0
      if (x86_elf_abi == X86_64_ABI)
7471
0
  {
7472
0
    if (!i.types[i.operands - 1].bitfield.qword)
7473
0
      return x86_tls_error_dest_64bit_reg_size;
7474
0
  }
7475
0
      else if (!i.types[i.operands - 1].bitfield.dword
7476
0
         && !i.types[i.operands - 1].bitfield.qword)
7477
0
  return x86_tls_error_dest_32bit_or_64bit_reg_size;
7478
0
      break;
7479
7480
0
    case BFD_RELOC_386_TLS_DESC_CALL:
7481
      /* Check GDesc access model:
7482
7483
   call *x@tlscall(%eax) --> Memory reg must be %eax and
7484
           SIB is not supported.
7485
       */
7486
0
    case BFD_RELOC_X86_64_TLSDESC_CALL:
7487
      /* Check GDesc access model:
7488
7489
   call *x@tlscall(%rax) <--- LP64 mode.
7490
   call *x@tlscall(%eax) <--- X32 mode.
7491
7492
   Only these fixed formats are supported.
7493
       */
7494
0
      if (i.tm.mnem_off != MN_call)
7495
0
  return x86_tls_error_insn;
7496
0
      if (i.index_reg)
7497
0
  return x86_tls_error_sib;
7498
0
      if (!i.base_reg)
7499
0
  return x86_tls_error_no_base_reg;
7500
0
      if (i.base_reg->reg_type.bitfield.instance != Accum)
7501
0
  return x86_tls_error_RegA;
7502
0
      break;
7503
7504
0
    case BFD_RELOC_NONE:
7505
      /* This isn't a relocation.  */
7506
0
      return x86_tls_error_continue;
7507
7508
14
    default:
7509
14
      break;
7510
15
    }
7511
7512
  /* This relocation is OK.  */
7513
14
  return x86_tls_error_none;
7514
15
}
7515
7516
static void
7517
x86_report_tls_error (enum x86_tls_error_type tls_error,
7518
          enum bfd_reloc_code_real r_type)
7519
1
{
7520
1
  unsigned int k;
7521
9
  for (k = 0; k < ARRAY_SIZE (gotrel); k++)
7522
9
    if (gotrel[k].rel[object_64bit] == r_type)
7523
1
      break;
7524
7525
1
  switch (tls_error)
7526
1
    {
7527
1
    case x86_tls_error_insn:
7528
1
      as_bad (_("@%s operator cannot be used with `%s'"),
7529
1
        gotrel[k].str, insn_name (&i.tm));
7530
1
      return;
7531
7532
0
    case x86_tls_error_opcode:
7533
0
      as_bad (_("@%s operator can be used with `%s', but format is wrong"),
7534
0
        gotrel[k].str, insn_name (&i.tm));
7535
0
      return;
7536
7537
0
    case x86_tls_error_sib:
7538
0
      as_bad (_("@%s operator requires no SIB"), gotrel[k].str);
7539
0
      return;
7540
7541
0
    case x86_tls_error_no_base_reg:
7542
0
      as_bad (_("@%s operator requires base register"), gotrel[k].str);
7543
0
      return;
7544
7545
0
    case x86_tls_error_require_no_base_index_reg:
7546
0
      as_bad (_("@%s operator requires no base/index register"),
7547
0
        gotrel[k].str);
7548
0
      return;
7549
7550
0
    case x86_tls_error_base_reg:
7551
0
      as_bad (_("@%s operator requires no base register"), gotrel[k].str);
7552
0
      return;
7553
7554
0
    case x86_tls_error_index_ebx:
7555
0
      as_bad (_("@%s operator requires `%sebx' as index register"),
7556
0
        gotrel[k].str, register_prefix);
7557
0
      return;
7558
7559
0
    case x86_tls_error_eax:
7560
0
      as_bad (_("@%s operator requires `%seax' as base register"),
7561
0
        gotrel[k].str, register_prefix);
7562
0
      return;
7563
7564
0
    case x86_tls_error_RegA:
7565
0
      as_bad (_("@%s operator requires `%seax/%srax' as base register"),
7566
0
        gotrel[k].str, register_prefix, register_prefix);
7567
0
      return;
7568
7569
0
    case x86_tls_error_ebx:
7570
0
      as_bad (_("@%s operator requires `%sebx' as base register"),
7571
0
        gotrel[k].str, register_prefix);
7572
0
      return;
7573
7574
0
    case x86_tls_error_rip:
7575
0
      as_bad (_("@%s operator requires `%srip' as base register"),
7576
0
        gotrel[k].str, register_prefix);
7577
0
      return;
7578
7579
0
    case x86_tls_error_dest_eax:
7580
0
      as_bad (_("@%s operator requires `%seax' as dest register"),
7581
0
        gotrel[k].str, register_prefix);
7582
0
      return;
7583
7584
0
    case x86_tls_error_dest_rdi:
7585
0
      as_bad (_("@%s operator requires `%srdi' as dest register"),
7586
0
        gotrel[k].str, register_prefix);
7587
0
      return;
7588
7589
0
    case x86_tls_error_scale_factor:
7590
0
      as_bad (_("@%s operator requires scale factor of 1"),
7591
0
        gotrel[k].str);
7592
0
      return;
7593
7594
0
    case x86_tls_error_base_reg_size:
7595
0
      as_bad (_("@%s operator requires 32-bit base register"),
7596
0
        gotrel[k].str);
7597
0
      return;
7598
7599
0
    case x86_tls_error_dest_32bit_reg_size:
7600
0
      as_bad (_("@%s operator requires 32-bit dest register"),
7601
0
        gotrel[k].str);
7602
0
      return;
7603
7604
0
    case x86_tls_error_dest_64bit_reg_size:
7605
0
      as_bad (_("@%s operator requires 64-bit dest register"),
7606
0
        gotrel[k].str);
7607
0
      return;
7608
7609
0
    case x86_tls_error_dest_32bit_or_64bit_reg_size:
7610
0
      as_bad (_("@%s operator requires 32-bit or 64-bit dest register"),
7611
0
        gotrel[k].str);
7612
0
      return;
7613
7614
0
    default:
7615
0
      abort ();
7616
1
    }
7617
1
}
7618
#endif
7619
7620
/* This is the guts of the machine-dependent assembler.  LINE points to a
7621
   machine dependent instruction.  This function is supposed to emit
7622
   the frags/bytes it assembles to.  */
7623
7624
static void
7625
i386_assemble (char *line)
7626
105k
{
7627
105k
  unsigned int j;
7628
105k
  char mnemonic[MAX_MNEM_SIZE], mnem_suffix = 0, *copy = NULL;
7629
105k
  char *xstrdup_copy = NULL;
7630
105k
  const char *end, *pass1_mnem = NULL;
7631
105k
  enum i386_error pass1_err = 0;
7632
105k
  struct pseudo_prefixes orig_pp = pp;
7633
105k
  const insn_template *t;
7634
105k
  struct last_insn *last_insn
7635
105k
    = &seg_info(now_seg)->tc_segment_info_data.last_insn;
7636
7637
  /* Initialize globals.  */
7638
105k
  current_templates.end = current_templates.start = NULL;
7639
106k
 retry:
7640
106k
  init_globals ();
7641
7642
  /* Suppress optimization when the last thing we saw may not have been
7643
     a proper instruction (e.g. a stand-alone prefix or .byte).  */
7644
106k
  if (last_insn->kind != last_insn_other)
7645
55.4k
    pp.no_optimize = true;
7646
7647
  /* First parse an instruction mnemonic & call i386_operand for the operands.
7648
     We assume that the scrubber has arranged it so that line[0] is the valid
7649
     start of a (possibly prefixed) mnemonic.  */
7650
7651
106k
  end = parse_insn (line, mnemonic, parse_all);
7652
106k
  if (end == NULL)
7653
80.5k
    {
7654
80.5k
      if (pass1_mnem != NULL)
7655
54
  goto match_error;
7656
80.5k
      if (i.error != no_error)
7657
31
  {
7658
31
    gas_assert (current_templates.start != NULL);
7659
31
    if (may_need_pass2 (current_templates.start) && !i.suffix)
7660
1
      goto no_match;
7661
    /* No point in trying a 2nd pass - it'll only find the same suffix
7662
       again.  */
7663
30
    mnem_suffix = i.suffix;
7664
30
    goto match_error;
7665
31
  }
7666
80.4k
      return;
7667
80.5k
    }
7668
25.4k
  t = current_templates.start;
7669
  /* NB: LINE may be change to be the same as XSTRDUP_COPY.  */
7670
25.4k
  if (xstrdup_copy != line && may_need_pass2 (t))
7671
436
    {
7672
      /* Make a copy of the full line in case we need to retry.  */
7673
436
      xstrdup_copy = xstrdup (line);
7674
436
      copy = xstrdup_copy;
7675
436
    }
7676
25.4k
  line += end - line;
7677
25.4k
  mnem_suffix = i.suffix;
7678
7679
25.4k
  line = parse_operands (line, mnemonic);
7680
25.4k
  this_operand = -1;
7681
25.4k
  if (line == NULL)
7682
14.7k
    {
7683
14.7k
      free (xstrdup_copy);
7684
14.7k
      return;
7685
14.7k
    }
7686
7687
  /* Now we've parsed the mnemonic into a set of templates, and have the
7688
     operands at hand.  */
7689
7690
  /* All Intel opcodes have reversed operands except for "bound", "enter",
7691
     "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
7692
     "rmpadjust", "rmpquery", "rmpopt", "rmpchkd", and deprecated forms of
7693
     "rmpupdate".
7694
     We also don't reverse intersegment "jmp" and "call" instructions with
7695
     2 immediate operands so that the immediate segment precedes the offset
7696
     consistently in Intel and AT&T modes.  */
7697
10.7k
  if (intel_syntax
7698
9.29k
      && i.operands > 1
7699
3.64k
      && (t->mnem_off != MN_bound)
7700
3.64k
      && !startswith (mnemonic, "invlpg")
7701
3.64k
      && !startswith (mnemonic, "monitor")
7702
3.64k
      && !startswith (mnemonic, "mwait")
7703
3.64k
      && (t->mnem_off != MN_pvalidate)
7704
3.64k
      && (!startswith (mnemonic, "rmp") || i.mem_operands)
7705
3.64k
      && (t->mnem_off != MN_tpause)
7706
3.64k
      && (t->mnem_off != MN_umwait)
7707
3.64k
      && !(i.operands == 2
7708
3.51k
     && operand_type_check (i.types[0], imm)
7709
686
     && operand_type_check (i.types[1], imm)))
7710
2.96k
    swap_operands ();
7711
7712
  /* The order of the immediates should be reversed for 2-immediates EXTRQ
7713
     and INSERTQ instructions.  Also OUT, UWRMSR, and WRMSRNS want their
7714
     immediate to be in the "canonical" place (first), despite it appearing
7715
     last (in AT&T syntax, or because of the swapping above) in the incoming
7716
     set of operands.  */
7717
10.7k
  if ((i.imm_operands == 2
7718
686
       && (t->mnem_off == MN_extrq || t->mnem_off == MN_insertq))
7719
10.7k
      || ((t->mnem_off == MN_out || t->mnem_off == MN_uwrmsr
7720
9.38k
     || t->mnem_off == MN_wrmsrns)
7721
1.34k
    && i.imm_operands && i.operands > i.imm_operands))
7722
0
      swap_2_operands (0, 1);
7723
7724
  /* All legitimate immediates are placed first now.  Others, if any, will be
7725
     rejected by match_template() anyway.  */
7726
10.7k
  if (operand_type_check (i.types[0], imm))
7727
6.73k
    {
7728
      /* For USER_MSR and MSR_IMM instructions, imm32 stands for the name of a
7729
   model specific register (MSR). That's an unsigned quantity, whereas all
7730
   other insns with 32-bit immediate and 64-bit operand size use
7731
   sign-extended immediates (imm32s). Therefore these insns are
7732
   special-cased, bypassing the normal handling of immediates here.  */
7733
6.73k
      if (is_cpu(current_templates.start, CpuUSER_MSR)
7734
5.39k
    || t->mnem_off == MN_rdmsr
7735
5.38k
    || t->mnem_off == MN_wrmsrns)
7736
1.35k
  i.types[0] = smallest_imm_type (i.op[0].imms->X_add_number);
7737
5.37k
      else
7738
5.37k
  optimize_imm ();
7739
6.73k
    }
7740
7741
10.7k
  if (i.disp_operands && !optimize_disp (t))
7742
0
    return;
7743
7744
  /* Next, we find a template that matches the given insn,
7745
     making sure the overlap of the given operands types is consistent
7746
     with the template operand types.  */
7747
7748
10.7k
  if (!(t = match_template (mnem_suffix)))
7749
4.25k
    {
7750
4.25k
      const char *err_msg;
7751
7752
4.25k
      if (copy && !mnem_suffix)
7753
373
  {
7754
373
    line = copy;
7755
373
    copy = NULL;
7756
374
  no_match:
7757
374
    pass1_err = i.error;
7758
374
    pass1_mnem = insn_name (current_templates.start);
7759
374
    pp = orig_pp;
7760
374
    goto retry;
7761
373
  }
7762
7763
      /* If a non-/only-64bit template (group) was found in pass 1, and if
7764
   _some_ template (group) was found in pass 2, squash pass 1's
7765
   error.  */
7766
3.88k
      if (pass1_err == unsupported_64bit)
7767
0
  pass1_mnem = NULL;
7768
7769
3.96k
  match_error:
7770
3.96k
      free (xstrdup_copy);
7771
7772
3.96k
      switch (pass1_mnem ? pass1_err : i.error)
7773
3.96k
  {
7774
0
  default:
7775
0
    abort ();
7776
2
  case operand_size_mismatch:
7777
2
    err_msg = _("operand size mismatch");
7778
2
    break;
7779
1.16k
  case operand_type_mismatch:
7780
1.16k
    err_msg = _("operand type mismatch");
7781
1.16k
    break;
7782
1
  case register_type_mismatch:
7783
1
    err_msg = _("register type mismatch");
7784
1
    break;
7785
2.74k
  case number_of_operands_mismatch:
7786
2.74k
    err_msg = _("number of operands mismatch");
7787
2.74k
    break;
7788
2
  case invalid_instruction_suffix:
7789
2
    err_msg = _("invalid instruction suffix");
7790
2
    break;
7791
0
  case bad_imm4:
7792
0
    err_msg = _("constant doesn't fit in 4 bits");
7793
0
    break;
7794
0
  case unsupported_with_intel_mnemonic:
7795
0
    err_msg = _("unsupported with Intel mnemonic");
7796
0
    break;
7797
4
  case unsupported_syntax:
7798
4
    err_msg = _("unsupported syntax");
7799
4
    break;
7800
0
  case unsupported_EGPR_for_addressing:
7801
0
    err_msg = _("extended GPR cannot be used as base/index");
7802
0
    break;
7803
0
  case unsupported_nf:
7804
0
    err_msg = _("{nf} unsupported");
7805
0
    break;
7806
5
  case unsupported:
7807
5
    as_bad (_("unsupported instruction `%s'"),
7808
5
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
7809
5
    return;
7810
15
  case unsupported_on_arch:
7811
15
    as_bad (_("`%s' is not supported on `%s%s'"),
7812
15
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start),
7813
15
      cpu_arch_name ? cpu_arch_name : default_arch,
7814
15
      cpu_sub_arch_name ? cpu_sub_arch_name : "");
7815
15
    return;
7816
16
  case unsupported_64bit:
7817
16
    if (ISLOWER (mnem_suffix))
7818
1
      {
7819
1
        if (flag_code == CODE_64BIT)
7820
0
    as_bad (_("`%s%c' is not supported in 64-bit mode"),
7821
0
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start),
7822
0
      mnem_suffix);
7823
1
        else
7824
1
    as_bad (_("`%s%c' is only supported in 64-bit mode"),
7825
1
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start),
7826
1
      mnem_suffix);
7827
1
      }
7828
15
    else
7829
15
      {
7830
15
        if (flag_code == CODE_64BIT)
7831
14
    as_bad (_("`%s' is not supported in 64-bit mode"),
7832
14
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
7833
1
        else
7834
1
    as_bad (_("`%s' is only supported in 64-bit mode"),
7835
1
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
7836
15
      }
7837
16
    return;
7838
0
  case no_vex_encoding:
7839
0
    err_msg = _("no VEX/XOP encoding");
7840
0
    break;
7841
0
  case no_evex_encoding:
7842
0
    err_msg = _("no EVEX encoding");
7843
0
    break;
7844
0
  case invalid_sib_address:
7845
0
    err_msg = _("invalid SIB address");
7846
0
    break;
7847
0
  case invalid_vsib_address:
7848
0
    err_msg = _("invalid VSIB address");
7849
0
    break;
7850
0
  case invalid_vector_register_set:
7851
0
    err_msg = _("mask, index, and destination registers must be distinct");
7852
0
    break;
7853
0
  case invalid_tmm_register_set:
7854
0
    err_msg = _("all tmm registers must be distinct");
7855
0
    break;
7856
0
  case invalid_dest_and_src_register_set:
7857
0
    err_msg = _("destination and source registers must be distinct");
7858
0
    break;
7859
0
  case invalid_dest_register_set:
7860
0
    err_msg = _("two dest registers must be distinct");
7861
0
    break;
7862
0
  case invalid_pseudo_prefix:
7863
0
    err_msg = _("rex2 pseudo prefix cannot be used");
7864
0
    break;
7865
0
  case unsupported_vector_index_register:
7866
0
    err_msg = _("unsupported vector index register");
7867
0
    break;
7868
5
  case unsupported_broadcast:
7869
5
    err_msg = _("unsupported broadcast");
7870
5
    break;
7871
0
  case broadcast_needed:
7872
0
    err_msg = _("broadcast is needed for operand of such type");
7873
0
    break;
7874
0
  case unsupported_masking:
7875
0
    err_msg = _("unsupported masking");
7876
0
    break;
7877
0
  case mask_not_on_destination:
7878
0
    err_msg = _("mask not on destination operand");
7879
0
    break;
7880
0
  case no_default_mask:
7881
0
    err_msg = _("default mask isn't allowed");
7882
0
    break;
7883
0
  case unsupported_rc_sae:
7884
0
    err_msg = _("unsupported static rounding/sae");
7885
0
    break;
7886
0
  case unsupported_vector_size:
7887
0
    as_bad (_("vector size above %u required for `%s'"), 128u << vector_size,
7888
0
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
7889
0
    return;
7890
0
  case unsupported_rsp_register:
7891
0
    err_msg = _("'rsp' register cannot be used");
7892
0
    break;
7893
0
  case internal_error:
7894
0
    err_msg = _("internal error");
7895
0
    break;
7896
3.96k
  }
7897
3.93k
      as_bad (_("%s for `%s'"), err_msg,
7898
3.93k
        pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
7899
3.93k
      return;
7900
3.96k
    }
7901
7902
6.47k
  free (xstrdup_copy);
7903
7904
6.47k
  const i386_operand_type *t_types = get_operand_types (t);
7905
7906
6.47k
  if (sse_check != check_none
7907
      /* The opcode space check isn't strictly needed; it's there only to
7908
   bypass the logic below when easily possible.  */
7909
0
      && t->opcode_space >= SPACE_0F
7910
0
      && t->opcode_space <= SPACE_0F3A
7911
0
      && !is_cpu (&i.tm, CpuSSE4a)
7912
0
      && !is_any_vex_encoding (t))
7913
0
    {
7914
      /* Some KL and all WideKL insns have only implicit %xmm operands.  */
7915
0
      bool simd = is_cpu (t, CpuKL) || is_cpu (t, CpuWideKL);
7916
7917
0
      for (j = 0; j < t->operands; ++j)
7918
0
  {
7919
0
    if (t_types[j].bitfield.class == RegMMX)
7920
0
      break;
7921
0
    if (t_types[j].bitfield.class == RegSIMD)
7922
0
      simd = true;
7923
0
  }
7924
7925
0
      if (j >= t->operands && simd)
7926
0
  (sse_check == check_warning
7927
0
   ? as_warn
7928
0
   : as_bad) (_("SSE instruction `%s' is used"), insn_name (&i.tm));
7929
0
    }
7930
7931
6.47k
  if (i.tm.opcode_modifier.fwait)
7932
0
    if (!add_prefix (FWAIT_OPCODE))
7933
0
      return;
7934
7935
  /* Check if REP prefix is OK.  */
7936
6.47k
  if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep
7937
0
      && (i.prefix[REP_PREFIX] != REPE_PREFIX_OPCODE
7938
0
    || i.tm.opcode_modifier.prefixok != PrefixRepe))
7939
0
    {
7940
0
      as_bad (_("invalid instruction `%s' after `%s'"),
7941
0
    insn_name (&i.tm), i.rep_prefix);
7942
0
      return;
7943
0
    }
7944
7945
  /* Check for lock without a lockable instruction.  Destination operand
7946
     must be memory unless it is xchg (0x86).  */
7947
6.47k
  if (i.prefix[LOCK_PREFIX])
7948
1
    {
7949
1
      if (i.tm.opcode_modifier.prefixok < PrefixLock
7950
1
    || i.mem_operands == 0
7951
1
    || (i.tm.base_opcode != 0x86
7952
1
        && !(i.flags[i.operands - 1] & Operand_Mem)))
7953
0
  {
7954
0
    as_bad (_("expecting lockable instruction after `lock'"));
7955
0
    return;
7956
0
  }
7957
7958
      /* Zap the redundant prefix from XCHG when optimizing.  */
7959
1
      if (i.tm.base_opcode == 0x86 && optimize && !pp.no_optimize)
7960
0
  i.prefix[LOCK_PREFIX] = 0;
7961
1
    }
7962
7963
6.47k
#ifdef OBJ_ELF
7964
6.47k
  if (i.has_gotrel && tls_check)
7965
15
    {
7966
15
      enum x86_tls_error_type tls_error;
7967
15
      for (j = 0; j < i.operands; ++j)
7968
15
  {
7969
15
    tls_error = x86_check_tls_relocation (i.reloc[j]);
7970
15
    if (tls_error == x86_tls_error_continue)
7971
0
      continue;
7972
7973
15
    if (tls_error != x86_tls_error_none)
7974
1
      x86_report_tls_error (tls_error, i.reloc[j]);
7975
15
    break;
7976
15
  }
7977
15
    }
7978
6.47k
#endif
7979
7980
6.47k
  if ((is_any_vex_encoding (&i.tm) && i.tm.opcode_space != SPACE_MAP4)
7981
6.41k
      || i.tm_types[i.imm_operands].bitfield.class >= RegMMX
7982
6.40k
      || i.tm_types[i.imm_operands + 1].bitfield.class >= RegMMX
7983
6.40k
      || is_padlock(&i.tm))
7984
68
    {
7985
      /* Check for data size prefix on VEX/XOP/EVEX encoded, SIMD, and
7986
   PadLock insns.  */
7987
68
      if (i.prefix[DATA_PREFIX])
7988
0
  {
7989
0
    as_bad (_("data size prefix invalid with `%s'"), insn_name (&i.tm));
7990
0
    return;
7991
0
  }
7992
68
    }
7993
7994
  /* Check if HLE prefix is OK.  */
7995
6.47k
  if (i.hle_prefix && !check_hle ())
7996
0
    return;
7997
7998
  /* Check BND prefix.  */
7999
6.47k
  if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
8000
0
    as_bad (_("expecting valid branch instruction after `bnd'"));
8001
8002
  /* Check NOTRACK prefix.  */
8003
6.47k
  if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
8004
0
    as_bad (_("expecting indirect branch instruction after `notrack'"));
8005
8006
6.47k
  if (is_cpu (&i.tm, CpuMPX))
8007
0
    {
8008
0
      if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
8009
0
  as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
8010
0
      else if (flag_code != CODE_16BIT
8011
0
         ? i.prefix[ADDR_PREFIX]
8012
0
         : i.mem_operands && !i.prefix[ADDR_PREFIX])
8013
0
  as_bad (_("16-bit address isn't allowed in MPX instructions"));
8014
0
    }
8015
8016
  /* Insert BND prefix.  */
8017
6.47k
  if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
8018
0
    {
8019
0
      if (!i.prefix[BND_PREFIX])
8020
0
  add_prefix (BND_PREFIX_OPCODE);
8021
0
      else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
8022
0
  {
8023
0
    as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
8024
0
    i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
8025
0
  }
8026
0
    }
8027
8028
  /* Check string instruction segment overrides.  */
8029
6.47k
  if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
8030
16
    {
8031
16
      gas_assert (i.mem_operands);
8032
16
      if (!check_string ())
8033
0
  return;
8034
16
      i.disp_operands = 0;
8035
16
    }
8036
8037
  /* The memory operand of (%dx) should be only used with input/output
8038
     instructions (base opcodes: 0x6c, 0x6e, 0xec, 0xee).  */
8039
6.47k
  if (i.input_output_operand
8040
0
      && ((i.tm.base_opcode | 0x82) != 0xee
8041
0
    || i.tm.opcode_space != SPACE_BASE))
8042
0
    {
8043
0
      as_bad (_("input/output port address isn't allowed with `%s'"),
8044
0
        insn_name (&i.tm));
8045
0
      return;
8046
0
    }
8047
8048
6.47k
  if (optimize && !pp.no_optimize && i.tm.opcode_modifier.optimize)
8049
0
    {
8050
0
      if (pp.has_nf)
8051
0
  optimize_nf_encoding ();
8052
0
      optimize_encoding ();
8053
0
    }
8054
8055
  /* Past optimization there's no need to distinguish encoding_evex,
8056
     encoding_evex512, and encoding_egpr anymore.  */
8057
6.47k
  if (pp.encoding == encoding_evex512)
8058
0
    pp.encoding = encoding_evex;
8059
6.47k
  else if (pp.encoding == encoding_egpr)
8060
0
    pp.encoding = is_any_vex_encoding (&i.tm) ? encoding_evex
8061
0
               : encoding_default;
8062
8063
  /* Similarly {nf} can now be taken to imply {evex}.  */
8064
6.47k
  if (pp.has_nf && pp.encoding == encoding_default)
8065
0
    pp.encoding = encoding_evex;
8066
8067
6.47k
  if (use_unaligned_vector_move)
8068
0
    encode_with_unaligned_vector_move ();
8069
8070
6.47k
  if (!process_suffix (t))
8071
818
    return;
8072
8073
  /* Check if IP-relative addressing requirements can be satisfied.  */
8074
5.65k
  if (is_cpu (&i.tm, CpuPREFETCHI)
8075
0
      && !(i.base_reg && i.base_reg->reg_num == RegIP))
8076
0
    as_warn (_("'%s' only supports RIP-relative address"), insn_name (&i.tm));
8077
8078
  /* Update operand types and check extended states.  */
8079
12.0k
  for (j = 0; j < i.operands; j++)
8080
6.37k
    {
8081
6.37k
      enum operand_class class = i.types[j].bitfield.class;
8082
8083
6.37k
      i.types[j] = operand_type_and (i.types[j], i.tm_types[j]);
8084
6.37k
      switch (i.tm_types[j].bitfield.class)
8085
6.37k
  {
8086
2.16k
  default:
8087
2.16k
    break;
8088
2.16k
  case RegMMX:
8089
0
    i.xstate |= xstate_mmx;
8090
0
    break;
8091
0
  case RegMask:
8092
0
    i.xstate |= xstate_mask;
8093
0
    break;
8094
136
  case RegSIMD:
8095
136
    if (i.tm_types[j].bitfield.tmmword)
8096
0
      i.xstate |= xstate_tmm;
8097
136
    else if (i.tm_types[j].bitfield.zmmword
8098
4
       && !i.tm.opcode_modifier.vex
8099
4
       && vector_size >= VSZ512)
8100
4
      i.xstate |= xstate_zmm;
8101
132
    else if (i.tm_types[j].bitfield.ymmword
8102
4
       && vector_size >= VSZ256)
8103
4
      i.xstate |= xstate_ymm;
8104
128
    else if (i.tm_types[j].bitfield.xmmword)
8105
128
      i.xstate |= xstate_xmm;
8106
136
    break;
8107
4.07k
  case ClassNone:
8108
4.07k
    i.types[j].bitfield.class = class;
8109
4.07k
    break;
8110
6.37k
  }
8111
6.37k
    }
8112
8113
  /* Make still unresolved immediate matches conform to size of immediate
8114
     given in i.suffix.  */
8115
5.65k
  if (!finalize_imm ())
8116
0
    return;
8117
8118
5.65k
  if (i.types[0].bitfield.imm1)
8119
0
    i.imm_operands = 0; /* kludge for shift insns.  */
8120
8121
  /* For insns with operands there are more diddles to do to the opcode.  */
8122
5.65k
  if (i.operands)
8123
4.66k
    {
8124
4.66k
      if (!process_operands ())
8125
0
  return;
8126
4.66k
    }
8127
992
  else if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
8128
0
    {
8129
      /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc.  */
8130
0
      as_warn (_("translating to `%sp'"), insn_name (&i.tm));
8131
0
    }
8132
8133
5.65k
  if (is_any_vex_encoding (&i.tm))
8134
56
    {
8135
56
      if (!cpu_arch_flags.bitfield.cpui286)
8136
0
  {
8137
0
    as_bad (_("instruction `%s' isn't supported outside of protected mode."),
8138
0
      insn_name (&i.tm));
8139
0
    return;
8140
0
  }
8141
8142
      /* Check for explicit REX prefix.  */
8143
56
      if ((i.prefix[REX_PREFIX]
8144
0
     && (i.tm.opcode_space != SPACE_MAP4
8145
         /* To mimic behavior for legacy insns, permit use of REX64 for promoted
8146
      legacy instructions.  */
8147
0
         || i.prefix[REX_PREFIX] != (REX_OPCODE | REX_W)))
8148
56
    || pp.rex_encoding)
8149
0
  {
8150
0
    as_bad (_("REX prefix invalid with `%s'"), insn_name (&i.tm));
8151
0
    return;
8152
0
  }
8153
8154
      /* Check for explicit REX2 prefix.  */
8155
56
      if (pp.rex2_encoding)
8156
0
  {
8157
0
    as_bad (_("{rex2} prefix invalid with `%s'"), insn_name (&i.tm));
8158
0
    return;
8159
0
  }
8160
8161
56
      if (is_apx_evex_encoding ())
8162
3
  {
8163
3
    if (!build_apx_evex_prefix (false))
8164
0
      return;
8165
3
  }
8166
53
      else if (i.tm.opcode_modifier.vex)
8167
51
  build_vex_prefix (t);
8168
2
      else
8169
2
  build_evex_prefix ();
8170
8171
      /* The individual REX.RXBW bits got consumed.  */
8172
56
      i.rex &= REX_OPCODE;
8173
8174
      /* The rex2 bits got consumed.  */
8175
56
      i.rex2 = 0;
8176
56
    }
8177
8178
  /* Handle conversion of 'int $3' --> special int3 insn.  */
8179
5.65k
  if (i.tm.mnem_off == MN_int
8180
1
      && i.op[0].imms->X_add_number == 3)
8181
0
    {
8182
0
      i.tm.base_opcode = INT3_OPCODE;
8183
0
      i.imm_operands = 0;
8184
0
    }
8185
8186
5.65k
  if ((i.tm.opcode_modifier.jump == JUMP
8187
5.57k
       || i.tm.opcode_modifier.jump == JUMP_BYTE
8188
5.57k
       || i.tm.opcode_modifier.jump == JUMP_DWORD)
8189
138
      && i.op[0].disps->X_op == O_constant)
8190
2
    {
8191
      /* Convert "jmp constant" (and "call constant") to a jump (call) to
8192
   the absolute address given by the constant.  Since ix86 jumps and
8193
   calls are pc relative, we need to generate a reloc.  */
8194
2
      i.op[0].disps->X_add_symbol = &abs_symbol;
8195
2
      i.op[0].disps->X_op = O_symbol;
8196
2
    }
8197
8198
5.65k
  establish_rex ();
8199
8200
5.65k
  insert_lfence_before (last_insn);
8201
8202
  /* We are ready to output the insn.  */
8203
5.65k
  output_insn (last_insn);
8204
8205
5.65k
#ifdef OBJ_ELF
8206
  /* PS: SCFI is enabled only for System V AMD64 ABI.  The ABI check has been
8207
     performed in i386_target_format.  */
8208
5.65k
  if (flag_synth_cfi)
8209
0
    {
8210
0
      ginsnS *ginsn;
8211
0
      ginsn = x86_ginsn_new (symbol_temp_new_now (), frch_ginsn_gen_mode ());
8212
0
      frch_ginsn_data_append (ginsn);
8213
0
    }
8214
5.65k
#endif
8215
8216
5.65k
  insert_lfence_after ();
8217
8218
5.65k
  if (i.tm.opcode_modifier.isprefix)
8219
165
    {
8220
165
      last_insn->kind = last_insn_prefix;
8221
165
      last_insn->name = insn_name (&i.tm);
8222
165
      last_insn->file = as_where (&last_insn->line);
8223
165
    }
8224
5.48k
  else
8225
5.48k
    last_insn->kind = last_insn_other;
8226
5.65k
}
8227
8228
void
8229
md_assemble (char *line)
8230
105k
{
8231
105k
  i386_assemble (line);
8232
105k
  current_templates.start = NULL;
8233
105k
  memset (&pp, 0, sizeof (pp));
8234
105k
}
8235
8236
/* The Q suffix is generally valid only in 64-bit mode, with very few
8237
   exceptions: fild, fistp, fisttp, and cmpxchg8b.  Note that for fild
8238
   and fisttp only one of their two templates is matched below: That's
8239
   sufficient since other relevant attributes are the same between both
8240
   respective templates.  */
8241
static INLINE bool q_suffix_allowed(const insn_template *t)
8242
609
{
8243
609
  return flag_code == CODE_64BIT
8244
1
   || (t->opcode_space == SPACE_BASE
8245
0
       && t->base_opcode == 0xdf
8246
0
       && (t->extension_opcode & 1)) /* fild / fistp / fisttp */
8247
1
   || t->mnem_off == MN_cmpxchg8b;
8248
609
}
8249
8250
static const char *
8251
parse_insn (const char *line, char *mnemonic, enum parse_mode mode)
8252
113k
{
8253
113k
  const char *l = line, *token_start = l;
8254
113k
  char *mnem_p;
8255
113k
  bool pass1 = !current_templates.start;
8256
113k
  int supported;
8257
113k
  const insn_template *t;
8258
113k
  char *dot_p = NULL;
8259
8260
115k
  while (1)
8261
115k
    {
8262
115k
      const char *split;
8263
8264
115k
      mnem_p = mnemonic;
8265
      /* Pseudo-prefixes start with an opening figure brace.  */
8266
115k
      if ((*mnem_p = *l) == '{')
8267
2.45k
  {
8268
2.45k
    ++mnem_p;
8269
2.45k
    ++l;
8270
2.45k
    if (is_whitespace (*l))
8271
9
      ++l;
8272
2.45k
  }
8273
113k
      else if (mode == parse_pseudo_prefix)
8274
1.14k
  break;
8275
445k
      while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
8276
331k
  {
8277
331k
    if (*mnem_p == '.')
8278
15.8k
      dot_p = mnem_p;
8279
331k
    mnem_p++;
8280
331k
    if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
8281
71
      {
8282
71
      too_long:
8283
71
        as_bad (_("no such instruction: `%s'"), token_start);
8284
71
        return NULL;
8285
71
      }
8286
331k
    l++;
8287
331k
  }
8288
114k
      split = l;
8289
114k
      if (is_whitespace (*l))
8290
32.7k
  ++l;
8291
      /* Pseudo-prefixes end with a closing figure brace.  */
8292
114k
      if (*mnemonic == '{' && *l == '}')
8293
2.03k
  {
8294
2.03k
    *mnem_p++ = *l++;
8295
2.03k
    if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
8296
0
      goto too_long;
8297
2.03k
    *mnem_p = '\0';
8298
8299
2.03k
    if (is_whitespace (*l))
8300
2.01k
      ++l;
8301
2.03k
  }
8302
112k
      else if (l == split
8303
79.6k
         && *l != END_OF_INSN
8304
71.8k
         && (intel_syntax
8305
700
       || (*l != PREFIX_SEPARATOR && *l != ',')))
8306
71.8k
  {
8307
71.8k
    if (mode != parse_all)
8308
1.28k
      break;
8309
70.5k
    as_bad (_("invalid character %s in mnemonic"),
8310
70.5k
      output_invalid (*split));
8311
70.5k
    return NULL;
8312
71.8k
  }
8313
42.5k
      if (token_start == l)
8314
4
  {
8315
4
    if (!intel_syntax && *l == PREFIX_SEPARATOR)
8316
0
      as_bad (_("expecting prefix; got nothing"));
8317
4
    else
8318
4
      as_bad (_("expecting mnemonic; got nothing"));
8319
4
    return NULL;
8320
4
  }
8321
8322
      /* Look up instruction (or prefix) via hash table.  */
8323
42.5k
      op_lookup (mnemonic);
8324
8325
42.5k
      if (*l != END_OF_INSN
8326
34.8k
    && current_templates.start
8327
20.6k
    && current_templates.start->opcode_modifier.isprefix)
8328
2.99k
  {
8329
2.99k
    supported = cpu_flags_match (current_templates.start);
8330
2.99k
    if (!(supported & CPU_FLAGS_64BIT_MATCH))
8331
414
      {
8332
414
        as_bad ((flag_code != CODE_64BIT
8333
414
           ? _("`%s' is only supported in 64-bit mode")
8334
414
           : _("`%s' is not supported in 64-bit mode")),
8335
414
          insn_name (current_templates.start));
8336
414
        return NULL;
8337
414
      }
8338
2.57k
    if (supported != CPU_FLAGS_PERFECT_MATCH)
8339
463
      {
8340
463
        as_bad (_("`%s' is not supported on `%s%s'"),
8341
463
          insn_name (current_templates.start),
8342
463
          cpu_arch_name ? cpu_arch_name : default_arch,
8343
463
          cpu_sub_arch_name ? cpu_sub_arch_name : "");
8344
463
        return NULL;
8345
463
      }
8346
    /* If we are in 16-bit mode, do not allow addr16 or data16.
8347
       Similarly, in 32-bit mode, do not allow addr32 or data32.  */
8348
2.11k
    if ((current_templates.start->opcode_modifier.size == SIZE16
8349
1.99k
         || current_templates.start->opcode_modifier.size == SIZE32)
8350
124
        && flag_code != CODE_64BIT
8351
32
        && ((current_templates.start->opcode_modifier.size == SIZE32)
8352
32
      ^ (flag_code == CODE_16BIT)))
8353
0
      {
8354
0
        as_bad (_("redundant %s prefix"),
8355
0
          insn_name (current_templates.start));
8356
0
        return NULL;
8357
0
      }
8358
8359
2.11k
    if (current_templates.start->base_opcode == PSEUDO_PREFIX)
8360
1.14k
      {
8361
        /* Handle pseudo prefixes.  */
8362
1.14k
        switch (current_templates.start->extension_opcode)
8363
1.14k
    {
8364
0
    case Prefix_Disp8:
8365
      /* {disp8} */
8366
0
      pp.disp_encoding = disp_encoding_8bit;
8367
0
      break;
8368
0
    case Prefix_Disp16:
8369
      /* {disp16} */
8370
0
      pp.disp_encoding = disp_encoding_16bit;
8371
0
      break;
8372
0
    case Prefix_Disp32:
8373
      /* {disp32} */
8374
0
      pp.disp_encoding = disp_encoding_32bit;
8375
0
      break;
8376
31
    case Prefix_Load:
8377
      /* {load} */
8378
31
      pp.dir_encoding = dir_encoding_load;
8379
31
      break;
8380
0
    case Prefix_Store:
8381
      /* {store} */
8382
0
      pp.dir_encoding = dir_encoding_store;
8383
0
      break;
8384
0
    case Prefix_VEX:
8385
      /* {vex} */
8386
0
      pp.encoding = encoding_vex;
8387
0
      break;
8388
0
    case Prefix_VEX3:
8389
      /* {vex3} */
8390
0
      pp.encoding = encoding_vex3;
8391
0
      break;
8392
1.11k
    case Prefix_EVEX:
8393
      /* {evex} */
8394
1.11k
      pp.encoding = encoding_evex;
8395
1.11k
      break;
8396
2
    case Prefix_REX:
8397
      /* {rex} */
8398
2
      pp.rex_encoding = true;
8399
2
      break;
8400
0
    case Prefix_REX2:
8401
      /* {rex2} */
8402
0
      pp.rex2_encoding = true;
8403
0
      break;
8404
0
    case Prefix_NF:
8405
      /* {nf} */
8406
0
      pp.has_nf = true;
8407
0
      break;
8408
0
    case Prefix_NoOptimize:
8409
      /* {nooptimize} */
8410
0
      pp.no_optimize = true;
8411
0
      break;
8412
0
    case Prefix_NoImm8s:
8413
      /* {noimm8s} */
8414
0
      pp.no_imm8s = true;
8415
0
      break;
8416
0
    default:
8417
0
      abort ();
8418
1.14k
    }
8419
1.14k
        if (pp.has_nf
8420
0
      && pp.encoding != encoding_default
8421
0
      && pp.encoding != encoding_evex)
8422
0
    {
8423
0
      as_bad (_("{nf} cannot be combined with {vex}/{vex3}"));
8424
0
      return NULL;
8425
0
    }
8426
1.14k
      }
8427
971
    else
8428
971
      {
8429
        /* Add prefix, checking for repeated prefixes.  */
8430
971
        switch (add_prefix (current_templates.start->base_opcode))
8431
971
    {
8432
1
    case PREFIX_EXIST:
8433
1
      return NULL;
8434
2
    case PREFIX_DS:
8435
2
      if (is_cpu (current_templates.start, CpuIBT))
8436
0
        i.notrack_prefix = insn_name (current_templates.start);
8437
2
      break;
8438
516
    case PREFIX_REP:
8439
516
      if (is_cpu (current_templates.start, CpuHLE))
8440
0
        i.hle_prefix = insn_name (current_templates.start);
8441
516
      else if (is_cpu (current_templates.start, CpuMPX))
8442
0
        i.bnd_prefix = insn_name (current_templates.start);
8443
516
      else
8444
516
        i.rep_prefix = insn_name (current_templates.start);
8445
516
      break;
8446
452
    default:
8447
452
      break;
8448
971
    }
8449
971
      }
8450
    /* Skip past PREFIX_SEPARATOR and reset token_start.  */
8451
2.11k
    l += (!intel_syntax && *l == PREFIX_SEPARATOR);
8452
2.11k
    if (is_whitespace (*l))
8453
557
      ++l;
8454
2.11k
    token_start = l;
8455
2.11k
  }
8456
39.5k
      else
8457
39.5k
  break;
8458
42.5k
    }
8459
8460
41.9k
  if (mode != parse_all)
8461
6.59k
    return token_start;
8462
8463
35.3k
  if (!current_templates.start)
8464
16.4k
    {
8465
#ifdef TE_SOLARIS
8466
      /* Sun specifies an alternative form for CMOVcc: Size suffix (if any)
8467
   first, then a dot, then the condition code mnemonic.  */
8468
      if ((mnemonic + 4 == dot_p && !memcmp (mnemonic, "cmov", 4))
8469
    /* While doc doesn't say so, gcc assumes it: Same for FCMOVcc,
8470
       except that there's no size suffix to care about.  */
8471
    || (mnemonic + 5 == dot_p && !memcmp (mnemonic, "fcmov", 5)))
8472
  {
8473
    /* Simply strip the dot.  */
8474
    memmove (dot_p, dot_p + 1, mnem_p - dot_p);
8475
    dot_p = mnem_p - 1;
8476
  }
8477
      else if (!intel_syntax
8478
         && mnemonic + 5 == dot_p
8479
         && !memcmp (mnemonic, "cmov", 4)
8480
         && strchr ("lqw", TOLOWER (dot_p[-1])))
8481
  {
8482
    /* Strip the dot, while moving the suffix.  */
8483
    char suffix = dot_p[-1];
8484
8485
    memmove (dot_p - 1, dot_p + 1, mnem_p - dot_p);
8486
    mnem_p[-2] = suffix;
8487
    dot_p = mnem_p - 1;
8488
  }
8489
      else
8490
#endif
8491
      /* Deprecated functionality (new code should use pseudo-prefixes instead):
8492
   Check if we should swap operand or force 32bit displacement in
8493
   encoding.  */
8494
16.4k
      if (mnem_p - 2 == dot_p && dot_p[1] == 's')
8495
0
  {
8496
0
    if (pp.dir_encoding == dir_encoding_default)
8497
0
      pp.dir_encoding = dir_encoding_swap;
8498
0
    else
8499
0
      as_warn (_("ignoring `.s' suffix due to earlier `{%s}'"),
8500
0
         pp.dir_encoding == dir_encoding_load ? "load" : "store");
8501
0
  }
8502
16.4k
      else if (mnem_p - 3 == dot_p
8503
157
         && dot_p[1] == 'd'
8504
6
         && dot_p[2] == '8')
8505
0
  {
8506
0
    if (pp.disp_encoding == disp_encoding_default)
8507
0
      pp.disp_encoding = disp_encoding_8bit;
8508
0
    else if (pp.disp_encoding != disp_encoding_8bit)
8509
0
      as_warn (_("ignoring `.d8' suffix due to earlier `{disp<N>}'"));
8510
0
  }
8511
16.4k
      else if (mnem_p - 4 == dot_p
8512
6
         && dot_p[1] == 'd'
8513
1
         && dot_p[2] == '3'
8514
1
         && dot_p[3] == '2')
8515
0
  {
8516
0
    if (pp.disp_encoding == disp_encoding_default)
8517
0
      pp.disp_encoding = disp_encoding_32bit;
8518
0
    else if (pp.disp_encoding != disp_encoding_32bit)
8519
0
      as_warn (_("ignoring `.d32' suffix due to earlier `{disp<N>}'"));
8520
0
  }
8521
16.4k
      else
8522
16.4k
  goto check_suffix;
8523
0
      mnem_p = dot_p;
8524
0
      *dot_p = '\0';
8525
0
      op_lookup (mnemonic);
8526
0
    }
8527
8528
18.8k
  if (!current_templates.start || !pass1)
8529
374
    {
8530
374
      current_templates.start = NULL;
8531
8532
16.8k
    check_suffix:
8533
16.8k
      if (mnem_p > mnemonic)
8534
16.8k
  {
8535
    /* See if we can get a match by trimming off a suffix.  */
8536
16.8k
    switch (mnem_p[-1])
8537
16.8k
      {
8538
1.49k
      case WORD_MNEM_SUFFIX:
8539
1.49k
        if (intel_syntax && (intel_float_operand (mnemonic) & 2))
8540
0
    i.suffix = SHORT_MNEM_SUFFIX;
8541
1.49k
        else
8542
    /* Fall through.  */
8543
8.40k
        case BYTE_MNEM_SUFFIX:
8544
8.71k
        case QWORD_MNEM_SUFFIX:
8545
8.71k
    i.suffix = mnem_p[-1];
8546
8.71k
        mnem_p[-1] = '\0';
8547
8.71k
        op_lookup (mnemonic);
8548
8.71k
        break;
8549
401
      case SHORT_MNEM_SUFFIX:
8550
1.14k
      case LONG_MNEM_SUFFIX:
8551
1.14k
        if (!intel_syntax)
8552
17
    {
8553
17
      i.suffix = mnem_p[-1];
8554
17
      mnem_p[-1] = '\0';
8555
17
      op_lookup (mnemonic);
8556
17
    }
8557
1.14k
        break;
8558
8559
        /* Intel Syntax.  */
8560
93
      case 'd':
8561
93
        if (intel_syntax)
8562
84
    {
8563
84
      if (intel_float_operand (mnemonic) == 1)
8564
0
        i.suffix = SHORT_MNEM_SUFFIX;
8565
84
      else
8566
84
        i.suffix = LONG_MNEM_SUFFIX;
8567
84
      mnem_p[-1] = '\0';
8568
84
      op_lookup (mnemonic);
8569
84
    }
8570
        /* For compatibility reasons accept MOVSD and CMPSD without
8571
           operands even in AT&T mode.  */
8572
9
        else if (*l == END_OF_INSN)
8573
9
    {
8574
9
      mnem_p[-1] = '\0';
8575
9
      op_lookup (mnemonic);
8576
9
      if (current_templates.start != NULL
8577
          /* MOVS or CMPS */
8578
0
          && (current_templates.start->base_opcode | 2) == 0xa6
8579
0
          && current_templates.start->opcode_space
8580
0
       == SPACE_BASE
8581
0
          && mnem_p[-2] == 's')
8582
0
        {
8583
0
          as_warn (_("found `%sd'; assuming `%sl' was meant"),
8584
0
             mnemonic, mnemonic);
8585
0
          i.suffix = LONG_MNEM_SUFFIX;
8586
0
        }
8587
9
      else
8588
9
        {
8589
9
          current_templates.start = NULL;
8590
9
          mnem_p[-1] = 'd';
8591
9
        }
8592
9
    }
8593
93
        break;
8594
16.8k
      }
8595
16.8k
  }
8596
8597
16.8k
      if (!current_templates.start)
8598
9.84k
  {
8599
9.84k
    if (pass1)
8600
9.78k
      as_bad (_("no such instruction: `%s'"), token_start);
8601
9.84k
    return NULL;
8602
9.84k
  }
8603
16.8k
    }
8604
8605
  /* Handle SCC OSZC flgs.  */
8606
25.5k
  if (current_templates.start->opcode_modifier.operandconstraint == SCC)
8607
36
    {
8608
36
      int length = check_Scc_OszcOperations (l);
8609
36
      if (length < 0)
8610
22
  return NULL;
8611
14
      l += length;
8612
14
    }
8613
8614
25.5k
  if ((current_templates.start->opcode_modifier.jump == JUMP
8615
23.4k
       || current_templates.start->opcode_modifier.jump == JUMP_BYTE)
8616
2.08k
      && *l == ',')
8617
0
    {
8618
      /* Check for a branch hint.  We allow ",pt" and ",pn" for
8619
   predict taken and predict not taken respectively.
8620
   I'm not sure that branch hints actually do anything on loop
8621
   and jcxz insns (JumpByte) for current Pentium4 chips.  They
8622
   may work in the future and it doesn't hurt to accept them
8623
   now.  */
8624
0
      token_start = l++;
8625
0
      if (is_whitespace (*l))
8626
0
  ++l;
8627
0
      if (TOLOWER (*l) == 'p' && ISALPHA (l[1])
8628
0
    && (l[2] == END_OF_INSN || is_whitespace (l[2])))
8629
0
  {
8630
0
    if (TOLOWER (l[1]) == 't')
8631
0
      {
8632
0
        if (!add_prefix (DS_PREFIX_OPCODE))
8633
0
    return NULL;
8634
0
        l += 2;
8635
0
      }
8636
0
    else if (TOLOWER (l[1]) == 'n')
8637
0
      {
8638
0
        if (!add_prefix (CS_PREFIX_OPCODE))
8639
0
    return NULL;
8640
0
        l += 2;
8641
0
      }
8642
0
    else
8643
0
      l = token_start;
8644
0
  }
8645
0
      else
8646
0
  l = token_start;
8647
0
    }
8648
  /* Any other comma loses.  */
8649
25.5k
  if (*l == ',')
8650
2
    {
8651
2
      as_bad (_("invalid character %s in mnemonic"),
8652
2
        output_invalid (*l));
8653
2
      return NULL;
8654
2
    }
8655
8656
  /* Check if instruction is supported on specified architecture.  */
8657
25.5k
  supported = 0;
8658
63.1k
  for (t = current_templates.start; t < current_templates.end; ++t)
8659
63.1k
    {
8660
63.1k
      supported |= cpu_flags_match (t);
8661
8662
63.1k
      if (i.suffix == QWORD_MNEM_SUFFIX && !q_suffix_allowed (t))
8663
1
  supported &= ~CPU_FLAGS_64BIT_MATCH;
8664
8665
63.1k
      if (supported == CPU_FLAGS_PERFECT_MATCH)
8666
25.4k
  return l;
8667
63.1k
    }
8668
8669
31
  if (pass1)
8670
31
    {
8671
31
      if (supported & CPU_FLAGS_64BIT_MATCH)
8672
15
        i.error = unsupported_on_arch;
8673
16
      else
8674
16
        i.error = unsupported_64bit;
8675
31
    }
8676
8677
31
  return NULL;
8678
25.5k
}
8679
8680
static char *
8681
parse_operands (char *l, const char *mnemonic)
8682
29.4k
{
8683
29.4k
  char *token_start;
8684
8685
  /* 1 if operand is pending after ','.  */
8686
29.4k
  unsigned int expecting_operand = 0;
8687
8688
48.0k
  while (*l != END_OF_INSN)
8689
36.7k
    {
8690
      /* Non-zero if operand parens not balanced.  */
8691
36.7k
      unsigned int paren_not_balanced = 0;
8692
      /* True if inside double quotes.  */
8693
36.7k
      bool in_quotes = false;
8694
8695
      /* Skip optional white space before operand.  */
8696
36.7k
      if (is_whitespace (*l))
8697
205
  ++l;
8698
36.7k
      if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
8699
75
  {
8700
75
    as_bad (_("invalid character %s before operand %d"),
8701
75
      output_invalid (*l),
8702
75
      i.operands + 1);
8703
75
    return NULL;
8704
75
  }
8705
36.6k
      token_start = l;  /* After white space.  */
8706
657k
      while (in_quotes || paren_not_balanced || *l != ',')
8707
647k
  {
8708
647k
    if (*l == END_OF_INSN)
8709
21.4k
      {
8710
21.4k
        if (in_quotes)
8711
308
    {
8712
308
      as_bad (_("unbalanced double quotes in operand %d."),
8713
308
        i.operands + 1);
8714
308
      return NULL;
8715
308
    }
8716
21.1k
        if (paren_not_balanced)
8717
3
    {
8718
3
      know (!intel_syntax);
8719
3
      as_bad (_("unbalanced parenthesis in operand %d."),
8720
3
        i.operands + 1);
8721
3
      return NULL;
8722
3
    }
8723
21.1k
        else
8724
21.1k
    break; /* we are done */
8725
21.1k
      }
8726
626k
    else if (*l == '\\' && l[1] == '"')
8727
0
      ++l;
8728
626k
    else if (*l == '"')
8729
2.98k
      in_quotes = !in_quotes;
8730
623k
    else if (!in_quotes && !is_operand_char (*l) && !is_whitespace (*l))
8731
4.96k
      {
8732
4.96k
        as_bad (_("invalid character %s in operand %d"),
8733
4.96k
          output_invalid (*l),
8734
4.96k
          i.operands + 1);
8735
4.96k
        return NULL;
8736
4.96k
      }
8737
621k
    if (!intel_syntax && !in_quotes)
8738
117k
      {
8739
117k
        if (*l == '(')
8740
7
    ++paren_not_balanced;
8741
117k
        if (*l == ')')
8742
4
    --paren_not_balanced;
8743
117k
      }
8744
621k
    l++;
8745
621k
  }
8746
31.3k
      if (l != token_start)
8747
31.3k
  {     /* Yes, we've read in another operand.  */
8748
31.3k
    unsigned int operand_ok;
8749
31.3k
    this_operand = i.operands++;
8750
31.3k
    if (i.operands > MAX_OPERANDS)
8751
0
      {
8752
0
        as_bad (_("spurious operands; (%d operands/instruction max)"),
8753
0
          MAX_OPERANDS);
8754
0
        return NULL;
8755
0
      }
8756
31.3k
    i.types[this_operand].bitfield.unspecified = 1;
8757
    /* Now parse operand adding info to 'i' as we go along.  */
8758
31.3k
    END_STRING_AND_SAVE (l);
8759
8760
31.3k
    if (i.mem_operands > 1)
8761
34
      {
8762
34
        as_bad (_("too many memory references for `%s'"),
8763
34
          mnemonic);
8764
34
        return 0;
8765
34
      }
8766
8767
31.3k
    if (intel_syntax)
8768
29.3k
      operand_ok =
8769
29.3k
        i386_intel_operand (token_start,
8770
29.3k
          intel_float_operand (mnemonic));
8771
2.00k
    else
8772
2.00k
      operand_ok = i386_att_operand (token_start);
8773
8774
31.3k
    RESTORE_END_STRING (l);
8775
31.3k
    if (!operand_ok)
8776
12.7k
      return NULL;
8777
31.3k
  }
8778
16
      else
8779
16
  {
8780
16
    if (expecting_operand)
8781
1
      {
8782
13
      expecting_operand_after_comma:
8783
13
        as_bad (_("expecting operand after ','; got nothing"));
8784
13
        return NULL;
8785
1
      }
8786
15
    if (*l == ',')
8787
15
      {
8788
15
        as_bad (_("expecting operand before ','; got nothing"));
8789
15
        return NULL;
8790
15
      }
8791
15
  }
8792
8793
      /* Now *l must be either ',' or END_OF_INSN.  */
8794
18.5k
      if (*l == ',')
8795
8.58k
  {
8796
8.58k
    if (*++l == END_OF_INSN)
8797
12
      {
8798
        /* Just skip it, if it's \n complain.  */
8799
12
        goto expecting_operand_after_comma;
8800
12
      }
8801
8.57k
    expecting_operand = 1;
8802
8.57k
  }
8803
18.5k
    }
8804
11.2k
  return l;
8805
29.4k
}
8806
8807
static void
8808
copy_operand (unsigned int to, unsigned int from)
8809
0
{
8810
0
  i.types[to] = i.types[from];
8811
0
  i.tm_types[to] = i.tm_types[from];
8812
0
  i.flags[to] = i.flags[from];
8813
0
  i.op[to] = i.op[from];
8814
0
  i.reloc[to] = i.reloc[from];
8815
0
  i.imm_bits[to] = i.imm_bits[from];
8816
  /* Note: i.mask and i.broadcast aren't handled here, as what (if
8817
     anything) to do there depends on context.  */
8818
0
}
8819
8820
static void
8821
swap_2_operands (unsigned int xchg1, unsigned int xchg2)
8822
3.05k
{
8823
3.05k
  union i386_op temp_op;
8824
3.05k
  i386_operand_type temp_type;
8825
3.05k
  unsigned int temp_flags;
8826
3.05k
  enum bfd_reloc_code_real temp_reloc;
8827
8828
3.05k
  temp_type = i.types[xchg2];
8829
3.05k
  i.types[xchg2] = i.types[xchg1];
8830
3.05k
  i.types[xchg1] = temp_type;
8831
8832
3.05k
  temp_flags = i.flags[xchg2];
8833
3.05k
  i.flags[xchg2] = i.flags[xchg1];
8834
3.05k
  i.flags[xchg1] = temp_flags;
8835
8836
3.05k
  temp_op = i.op[xchg2];
8837
3.05k
  i.op[xchg2] = i.op[xchg1];
8838
3.05k
  i.op[xchg1] = temp_op;
8839
8840
3.05k
  temp_reloc = i.reloc[xchg2];
8841
3.05k
  i.reloc[xchg2] = i.reloc[xchg1];
8842
3.05k
  i.reloc[xchg1] = temp_reloc;
8843
8844
3.05k
  temp_flags = i.imm_bits[xchg2];
8845
3.05k
  i.imm_bits[xchg2] = i.imm_bits[xchg1];
8846
3.05k
  i.imm_bits[xchg1] = temp_flags;
8847
8848
3.05k
  if (i.mask.reg)
8849
0
    {
8850
0
      if (i.mask.operand == xchg1)
8851
0
  i.mask.operand = xchg2;
8852
0
      else if (i.mask.operand == xchg2)
8853
0
  i.mask.operand = xchg1;
8854
0
    }
8855
3.05k
  if (i.broadcast.type || i.broadcast.bytes)
8856
106
    {
8857
106
      if (i.broadcast.operand == xchg1)
8858
41
  i.broadcast.operand = xchg2;
8859
65
      else if (i.broadcast.operand == xchg2)
8860
26
  i.broadcast.operand = xchg1;
8861
106
    }
8862
3.05k
}
8863
8864
static void
8865
swap_operands (void)
8866
2.96k
{
8867
2.96k
  switch (i.operands)
8868
2.96k
    {
8869
0
    case 5:
8870
0
    case 4:
8871
0
      swap_2_operands (1, i.operands - 2);
8872
      /* Fall through.  */
8873
130
    case 3:
8874
2.96k
    case 2:
8875
2.96k
      swap_2_operands (0, i.operands - 1);
8876
2.96k
      break;
8877
0
    default:
8878
0
      abort ();
8879
2.96k
    }
8880
8881
2.96k
  if (i.mem_operands == 2)
8882
37
    {
8883
37
      const reg_entry *temp_seg;
8884
37
      temp_seg = i.seg[0];
8885
37
      i.seg[0] = i.seg[1];
8886
37
      i.seg[1] = temp_seg;
8887
37
    }
8888
2.96k
}
8889
8890
/* Try to ensure constant immediates are represented in the smallest
8891
   opcode possible.  */
8892
static void
8893
optimize_imm (void)
8894
5.37k
{
8895
5.37k
  char guess_suffix = 0;
8896
5.37k
  int op;
8897
8898
5.37k
  if (i.suffix)
8899
2.20k
    guess_suffix = i.suffix;
8900
3.17k
  else if (i.reg_operands)
8901
20
    {
8902
      /* Figure out a suffix from the last register operand specified.
8903
   We can't do this properly yet, i.e. excluding special register
8904
   instances, but the following works for instructions with
8905
   immediates.  In any case, we can't set i.suffix yet.  */
8906
20
      for (op = i.operands; --op >= 0;)
8907
20
  if (i.types[op].bitfield.class != Reg)
8908
0
    continue;
8909
20
  else if (i.types[op].bitfield.byte)
8910
10
    {
8911
10
      guess_suffix = BYTE_MNEM_SUFFIX;
8912
10
      break;
8913
10
    }
8914
10
  else if (i.types[op].bitfield.word)
8915
7
    {
8916
7
      guess_suffix = WORD_MNEM_SUFFIX;
8917
7
      break;
8918
7
    }
8919
3
  else if (i.types[op].bitfield.dword)
8920
3
    {
8921
3
      guess_suffix = LONG_MNEM_SUFFIX;
8922
3
      break;
8923
3
    }
8924
0
  else if (i.types[op].bitfield.qword)
8925
0
    {
8926
0
      guess_suffix = QWORD_MNEM_SUFFIX;
8927
0
      break;
8928
0
    }
8929
20
    }
8930
3.15k
  else if ((flag_code == CODE_16BIT)
8931
3.15k
      ^ (i.prefix[DATA_PREFIX] != 0 && !(i.prefix[REX_PREFIX] & REX_W)))
8932
141
    guess_suffix = WORD_MNEM_SUFFIX;
8933
3.01k
  else if (flag_code != CODE_64BIT
8934
2.97k
     || (!(i.prefix[REX_PREFIX] & REX_W)
8935
         /* A more generic (but also more involved) way of dealing
8936
      with the special case(s) would be to go look for
8937
      DefaultSize attributes on any of the templates.  */
8938
2.97k
         && current_templates.start->mnem_off != MN_push
8939
754
         && current_templates.start->mnem_off != MN_jmpabs))
8940
786
    guess_suffix = LONG_MNEM_SUFFIX;
8941
8942
11.4k
  for (op = i.imm_operands; --op >= 0;)
8943
6.06k
    if (operand_type_check (i.types[op], imm))
8944
6.06k
      {
8945
6.06k
  switch (i.op[op].imms->X_op)
8946
6.06k
    {
8947
2.64k
    case O_constant:
8948
      /* If a suffix is given, this operand may be shortened.  */
8949
2.64k
      switch (guess_suffix)
8950
2.64k
        {
8951
31
        case LONG_MNEM_SUFFIX:
8952
31
    i.types[op].bitfield.imm32 = 1;
8953
31
    i.types[op].bitfield.imm64 = 1;
8954
31
    break;
8955
7
        case WORD_MNEM_SUFFIX:
8956
7
    i.types[op].bitfield.imm16 = 1;
8957
7
    i.types[op].bitfield.imm32 = 1;
8958
7
    i.types[op].bitfield.imm32s = 1;
8959
7
    i.types[op].bitfield.imm64 = 1;
8960
7
    break;
8961
2.59k
        case BYTE_MNEM_SUFFIX:
8962
2.59k
    i.types[op].bitfield.imm8 = 1;
8963
2.59k
    i.types[op].bitfield.imm8s = 1;
8964
2.59k
    i.types[op].bitfield.imm16 = 1;
8965
2.59k
    i.types[op].bitfield.imm32 = 1;
8966
2.59k
    i.types[op].bitfield.imm32s = 1;
8967
2.59k
    i.types[op].bitfield.imm64 = 1;
8968
2.59k
    break;
8969
2.64k
        }
8970
8971
      /* If this operand is at most 16 bits, convert it
8972
         to a signed 16 bit number before trying to see
8973
         whether it will fit in an even smaller size.
8974
         This allows a 16-bit operand such as $0xffe0 to
8975
         be recognised as within Imm8S range.  */
8976
2.64k
      if ((i.types[op].bitfield.imm16)
8977
2.59k
    && fits_in_unsigned_word (i.op[op].imms->X_add_number))
8978
2.59k
        {
8979
2.59k
    i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
8980
2.59k
            ^ 0x8000) - 0x8000);
8981
2.59k
        }
8982
2.64k
#ifdef BFD64
8983
      /* Store 32-bit immediate in 64-bit for 64-bit BFD.  */
8984
2.64k
      if ((i.types[op].bitfield.imm32)
8985
2.62k
    && fits_in_unsigned_long (i.op[op].imms->X_add_number))
8986
2.61k
        {
8987
2.61k
    i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
8988
2.61k
            ^ ((offsetT) 1 << 31))
8989
2.61k
                 - ((offsetT) 1 << 31));
8990
2.61k
        }
8991
2.64k
#endif
8992
2.64k
      i.types[op]
8993
2.64k
        = operand_type_or (i.types[op],
8994
2.64k
         smallest_imm_type (i.op[op].imms->X_add_number));
8995
8996
      /* We must avoid matching of Imm32 templates when 64bit
8997
         only immediate is available.  */
8998
2.64k
      if (guess_suffix == QWORD_MNEM_SUFFIX)
8999
13
        i.types[op].bitfield.imm32 = 0;
9000
2.64k
      break;
9001
9002
0
    case O_absent:
9003
0
    case O_register:
9004
0
      abort ();
9005
9006
      /* Symbols and expressions.  */
9007
3.42k
    default:
9008
      /* Convert symbolic operand to proper sizes for matching, but don't
9009
         prevent matching a set of insns that only supports sizes other
9010
         than those matching the insn suffix.  */
9011
3.42k
      {
9012
3.42k
        i386_operand_type mask, allowed;
9013
3.42k
        const insn_template *t = current_templates.start;
9014
9015
3.42k
        operand_type_set (&mask, 0);
9016
3.42k
        switch (guess_suffix)
9017
3.42k
    {
9018
0
    case QWORD_MNEM_SUFFIX:
9019
0
      mask.bitfield.imm64 = 1;
9020
0
      mask.bitfield.imm32s = 1;
9021
0
      break;
9022
797
    case LONG_MNEM_SUFFIX:
9023
797
      mask.bitfield.imm32 = 1;
9024
797
      break;
9025
391
    case WORD_MNEM_SUFFIX:
9026
391
      mask.bitfield.imm16 = 1;
9027
391
      break;
9028
10
    case BYTE_MNEM_SUFFIX:
9029
10
      mask.bitfield.imm8 = 1;
9030
10
      break;
9031
2.22k
    default:
9032
2.22k
      break;
9033
3.42k
    }
9034
9035
3.42k
        allowed = operand_type_and (get_operand_types (t)[op], mask);
9036
32.2k
        while (++t < current_templates.end)
9037
28.8k
    {
9038
28.8k
      allowed = operand_type_or (allowed, get_operand_types (t)[op]);
9039
28.8k
      allowed = operand_type_and (allowed, mask);
9040
28.8k
    }
9041
9042
3.42k
        if (!operand_type_all_zero (&allowed))
9043
858
    i.types[op] = operand_type_and (i.types[op], mask);
9044
3.42k
      }
9045
0
      break;
9046
6.06k
    }
9047
6.06k
      }
9048
5.37k
}
9049
9050
/* Try to use the smallest displacement type too.  */
9051
static bool
9052
optimize_disp (const insn_template *t)
9053
4.51k
{
9054
4.51k
  unsigned int op;
9055
9056
4.51k
  if (!want_disp32 (t)
9057
4.08k
      && (!t->opcode_modifier.jump
9058
143
    || i.jumpabsolute || i.types[0].bitfield.baseindex))
9059
3.95k
    {
9060
8.22k
      for (op = i.imm_operands; op < i.operands; ++op)
9061
4.27k
  {
9062
4.27k
    const expressionS *exp = i.op[op].disps;
9063
9064
4.27k
    if (!operand_type_check (i.types[op], disp))
9065
282
      continue;
9066
9067
3.99k
    if (exp->X_op != O_constant)
9068
3.97k
      continue;
9069
9070
    /* Since displacement is signed extended to 64bit, don't allow
9071
       disp32 if it is out of range.  */
9072
11
    if (fits_in_signed_long (exp->X_add_number))
9073
11
      continue;
9074
9075
0
    i.types[op].bitfield.disp32 = 0;
9076
0
    if (i.types[op].bitfield.baseindex)
9077
0
      {
9078
0
        as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
9079
0
          (uint64_t) exp->X_add_number);
9080
0
        return false;
9081
0
      }
9082
0
  }
9083
3.95k
    }
9084
9085
  /* Don't optimize displacement for movabs / jmpabs since they only take
9086
     64-bit displacement.  */
9087
4.51k
  if (pp.disp_encoding > disp_encoding_8bit
9088
4.51k
      || t->mnem_off == MN_movabs || t->mnem_off == MN_jmpabs)
9089
4
    return true;
9090
9091
11.8k
  for (op = i.operands; op-- > 0;)
9092
7.36k
    if (operand_type_check (i.types[op], disp))
9093
4.55k
      {
9094
4.55k
  if (i.op[op].disps->X_op == O_constant)
9095
31
    {
9096
31
      offsetT op_disp = i.op[op].disps->X_add_number;
9097
9098
31
      if (!op_disp && i.types[op].bitfield.baseindex)
9099
3
        {
9100
3
    i.types[op] = operand_type_and_not (i.types[op], anydisp);
9101
3
    i.op[op].disps = NULL;
9102
3
    i.disp_operands--;
9103
3
    continue;
9104
3
        }
9105
9106
28
      if (i.types[op].bitfield.disp16
9107
14
    && fits_in_unsigned_word (op_disp))
9108
14
        {
9109
    /* If this operand is at most 16 bits, convert
9110
       to a signed 16 bit number and don't use 64bit
9111
       displacement.  */
9112
14
    op_disp = ((op_disp ^ 0x8000) - 0x8000);
9113
14
    i.types[op].bitfield.disp64 = 0;
9114
14
        }
9115
9116
28
#ifdef BFD64
9117
      /* Optimize 64-bit displacement to 32-bit for 64-bit BFD.  */
9118
28
      if ((flag_code != CODE_64BIT
9119
28
     ? i.types[op].bitfield.disp32
9120
28
     : want_disp32 (t)
9121
1
       && (!t->opcode_modifier.jump
9122
0
           || i.jumpabsolute || i.types[op].bitfield.baseindex))
9123
2
    && fits_in_unsigned_long (op_disp))
9124
2
        {
9125
    /* If this operand is at most 32 bits, convert
9126
       to a signed 32 bit number and don't use 64bit
9127
       displacement.  */
9128
2
    op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
9129
2
    i.types[op].bitfield.disp64 = 0;
9130
2
    i.types[op].bitfield.disp32 = 1;
9131
2
        }
9132
9133
28
      if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
9134
13
        {
9135
13
    i.types[op].bitfield.disp64 = 0;
9136
13
    i.types[op].bitfield.disp32 = 1;
9137
13
        }
9138
28
#endif
9139
28
      if ((i.types[op].bitfield.disp32
9140
14
     || i.types[op].bitfield.disp16)
9141
28
    && fits_in_disp8 (op_disp))
9142
26
        i.types[op].bitfield.disp8 = 1;
9143
9144
28
      i.op[op].disps->X_add_number = op_disp;
9145
28
    }
9146
4.52k
  else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
9147
4.52k
     || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
9148
0
    {
9149
0
      fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
9150
0
       i.op[op].disps, 0, i.reloc[op]);
9151
0
      i.types[op] = operand_type_and_not (i.types[op], anydisp);
9152
0
    }
9153
4.52k
  else
9154
    /* We only support 64bit displacement on constants.  */
9155
4.52k
    i.types[op].bitfield.disp64 = 0;
9156
4.55k
      }
9157
9158
4.50k
  return true;
9159
4.51k
}
9160
9161
/* Return 1 if there is a match in broadcast bytes between operand
9162
   GIVEN and instruction template T.   */
9163
9164
static INLINE int
9165
match_broadcast_size (const insn_template *t, unsigned int given)
9166
0
{
9167
0
  return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
9168
0
     && i.types[given].bitfield.byte)
9169
0
    || (t->opcode_modifier.broadcast == WORD_BROADCAST
9170
0
        && i.types[given].bitfield.word)
9171
0
    || (t->opcode_modifier.broadcast == DWORD_BROADCAST
9172
0
        && i.types[given].bitfield.dword)
9173
0
    || (t->opcode_modifier.broadcast == QWORD_BROADCAST
9174
0
        && i.types[given].bitfield.qword));
9175
0
}
9176
9177
/* Check if operands are valid for the instruction.  */
9178
9179
static int
9180
check_VecOperands (const insn_template *t)
9181
5.53k
{
9182
5.53k
  unsigned int op;
9183
5.53k
  i386_cpu_flags cpu;
9184
5.53k
  const i386_operand_type *t_types = get_operand_types (t);
9185
9186
  /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
9187
     any one operand are implicity requiring AVX512VL support if the actual
9188
     operand size is YMMword or XMMword.  Since this function runs after
9189
     template matching, there's no need to check for YMMword/XMMword in
9190
     the template.  */
9191
5.53k
  cpu = cpu_flags_or (cpu_flags_from_attr (t->cpu),
9192
5.53k
          cpu_flags_from_attr (t->cpu_any));
9193
5.53k
  cpu = cpu_flags_and (cpu, avx512);
9194
5.53k
  if (!cpu_flags_all_zero (&cpu)
9195
51
      && !cpu.bitfield.cpuavx512vl
9196
51
      && !cpu_arch_flags.bitfield.cpuavx512vl
9197
0
      && (!t->opcode_modifier.vex || need_evex_encoding (t)
9198
    /* Note: No need to check F16C here.  Those insns have distinct
9199
       templates for distinct VEX.L / EVEX.L'L.  */
9200
0
    || (maybe_cpu (t, CpuFMA) && !cpu_arch_flags.bitfield.cpufma)))
9201
0
    {
9202
0
      for (op = 0; op < t->operands; ++op)
9203
0
  {
9204
0
    if (t_types[op].bitfield.zmmword
9205
0
        && (i.types[op].bitfield.ymmword
9206
0
      || i.types[op].bitfield.xmmword))
9207
0
      {
9208
0
        i.error = operand_size_mismatch;
9209
0
        return 1;
9210
0
      }
9211
0
  }
9212
0
    }
9213
9214
  /* Somewhat similarly, templates specifying both AVX and AVX2 are
9215
     requiring AVX2 support if the actual operand size is YMMword.  */
9216
5.53k
  if (maybe_cpu (t, CpuAVX) && maybe_cpu (t, CpuAVX2)
9217
0
      && !cpu_arch_flags.bitfield.cpuavx2)
9218
0
    {
9219
0
      for (op = 0; op < t->operands; ++op)
9220
0
  {
9221
0
    if (t_types[op].bitfield.xmmword
9222
0
        && i.types[op].bitfield.ymmword)
9223
0
      {
9224
0
        i.error = operand_size_mismatch;
9225
0
        return 1;
9226
0
      }
9227
0
  }
9228
0
    }
9229
9230
  /* Without VSIB byte, we can't have a vector register for index.  */
9231
5.53k
  if (!t->opcode_modifier.sib
9232
5.53k
      && i.index_reg
9233
1
      && (i.index_reg->reg_type.bitfield.xmmword
9234
1
    || i.index_reg->reg_type.bitfield.ymmword
9235
1
    || i.index_reg->reg_type.bitfield.zmmword))
9236
0
    {
9237
0
      i.error = unsupported_vector_index_register;
9238
0
      return 1;
9239
0
    }
9240
9241
  /* Check if default mask is allowed.  */
9242
5.53k
  if (t->opcode_modifier.operandconstraint == NO_DEFAULT_MASK
9243
0
      && (!i.mask.reg || i.mask.reg->reg_num == 0))
9244
0
    {
9245
0
      i.error = no_default_mask;
9246
0
      return 1;
9247
0
    }
9248
9249
  /* For VSIB byte, we need a vector register for index, and all vector
9250
     registers must be distinct.  */
9251
5.53k
  if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
9252
0
    {
9253
0
      if (!i.index_reg
9254
0
    || !((t->opcode_modifier.sib == VECSIB128
9255
0
    && i.index_reg->reg_type.bitfield.xmmword)
9256
0
         || (t->opcode_modifier.sib == VECSIB256
9257
0
       && i.index_reg->reg_type.bitfield.ymmword)
9258
0
         || (t->opcode_modifier.sib == VECSIB512
9259
0
       && i.index_reg->reg_type.bitfield.zmmword)))
9260
0
      {
9261
0
  i.error = invalid_vsib_address;
9262
0
  return 1;
9263
0
      }
9264
9265
0
      gas_assert (i.reg_operands == 2 || i.mask.reg);
9266
0
      if (i.reg_operands == 2 && !i.mask.reg)
9267
0
  {
9268
0
    gas_assert (i.types[0].bitfield.class == RegSIMD);
9269
0
    gas_assert (i.types[0].bitfield.xmmword
9270
0
          || i.types[0].bitfield.ymmword);
9271
0
    gas_assert (i.types[2].bitfield.class == RegSIMD);
9272
0
    gas_assert (i.types[2].bitfield.xmmword
9273
0
          || i.types[2].bitfield.ymmword);
9274
0
    if (operand_check == check_none)
9275
0
      return 0;
9276
0
    if (register_number (i.op[0].regs)
9277
0
        != register_number (i.index_reg)
9278
0
        && register_number (i.op[2].regs)
9279
0
     != register_number (i.index_reg)
9280
0
        && register_number (i.op[0].regs)
9281
0
     != register_number (i.op[2].regs))
9282
0
      return 0;
9283
0
    if (operand_check == check_error)
9284
0
      {
9285
0
        i.error = invalid_vector_register_set;
9286
0
        return 1;
9287
0
      }
9288
0
    as_warn (_("mask, index, and destination registers should be distinct"));
9289
0
  }
9290
0
      else if (i.reg_operands == 1 && i.mask.reg)
9291
0
  {
9292
0
    if (i.types[1].bitfield.class == RegSIMD
9293
0
        && (i.types[1].bitfield.xmmword
9294
0
            || i.types[1].bitfield.ymmword
9295
0
            || i.types[1].bitfield.zmmword)
9296
0
        && (register_number (i.op[1].regs)
9297
0
      == register_number (i.index_reg)))
9298
0
      {
9299
0
        if (operand_check == check_error)
9300
0
    {
9301
0
      i.error = invalid_vector_register_set;
9302
0
      return 1;
9303
0
    }
9304
0
        if (operand_check != check_none)
9305
0
    as_warn (_("index and destination registers should be distinct"));
9306
0
      }
9307
0
  }
9308
0
    }
9309
9310
  /* For AMX instructions with 3 TMM register operands, all operands
9311
      must be distinct.  */
9312
5.53k
  if (i.reg_operands == 3
9313
0
      && t_types[0].bitfield.tmmword
9314
0
      && (i.op[0].regs == i.op[1].regs
9315
0
          || i.op[0].regs == i.op[2].regs
9316
0
          || i.op[1].regs == i.op[2].regs))
9317
0
    {
9318
0
      i.error = invalid_tmm_register_set;
9319
0
      return 1;
9320
0
    }
9321
9322
  /* For some special instructions require that destination must be distinct
9323
     from source registers.  */
9324
5.53k
  if (t->opcode_modifier.operandconstraint == DISTINCT_DEST)
9325
0
    {
9326
0
      unsigned int dest_reg = i.operands - 1;
9327
9328
0
      know (i.operands >= 3);
9329
9330
      /* #UD if dest_reg == src1_reg or dest_reg == src2_reg.  */
9331
0
      if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
9332
0
    || (i.reg_operands > 2
9333
0
        && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
9334
0
  {
9335
0
    i.error = invalid_dest_and_src_register_set;
9336
0
    return 1;
9337
0
  }
9338
0
    }
9339
9340
  /* Check if broadcast is supported by the instruction and is applied
9341
     to the memory operand.  */
9342
5.53k
  if (i.broadcast.type || i.broadcast.bytes)
9343
8
    {
9344
8
      i386_operand_type type, overlap;
9345
9346
      /* Check if specified broadcast is supported in this instruction,
9347
   and its broadcast bytes match the memory operand.  */
9348
8
      op = i.broadcast.operand;
9349
8
      if (!t->opcode_modifier.broadcast
9350
0
    || !(i.flags[op] & Operand_Mem)
9351
0
    || (!i.types[op].bitfield.unspecified
9352
0
        && !match_broadcast_size (t, op)))
9353
8
  {
9354
8
  bad_broadcast:
9355
8
    i.error = unsupported_broadcast;
9356
8
    return 1;
9357
8
  }
9358
9359
0
      operand_type_set (&type, 0);
9360
0
      switch (get_broadcast_bytes (t, false))
9361
0
  {
9362
0
  case 2:
9363
0
    type.bitfield.word = 1;
9364
0
    break;
9365
0
  case 4:
9366
0
    type.bitfield.dword = 1;
9367
0
    break;
9368
0
  case 8:
9369
0
    type.bitfield.qword = 1;
9370
0
    break;
9371
0
  case 16:
9372
0
    type.bitfield.xmmword = 1;
9373
0
    break;
9374
0
  case 32:
9375
0
    if (vector_size < VSZ256)
9376
0
      goto bad_broadcast;
9377
0
    type.bitfield.ymmword = 1;
9378
0
    break;
9379
0
  case 64:
9380
0
    if (vector_size < VSZ512)
9381
0
      goto bad_broadcast;
9382
0
    type.bitfield.zmmword = 1;
9383
0
    break;
9384
0
  default:
9385
0
    goto bad_broadcast;
9386
0
  }
9387
9388
0
      overlap = operand_type_and (type, t_types[op]);
9389
0
      if (t_types[op].bitfield.class == RegSIMD
9390
0
    && t_types[op].bitfield.byte
9391
0
       + t_types[op].bitfield.word
9392
0
       + t_types[op].bitfield.dword
9393
0
       + t_types[op].bitfield.qword > 1)
9394
0
  {
9395
0
    overlap.bitfield.xmmword = 0;
9396
0
    overlap.bitfield.ymmword = 0;
9397
0
    overlap.bitfield.zmmword = 0;
9398
0
  }
9399
0
      if (operand_type_all_zero (&overlap))
9400
0
    goto bad_broadcast;
9401
9402
0
      if (t->opcode_modifier.checkoperandsize)
9403
0
  {
9404
0
    unsigned int j;
9405
9406
0
    type.bitfield.baseindex = 1;
9407
0
    for (j = i.imm_operands; j < i.operands; ++j)
9408
0
      {
9409
0
        if (j != op
9410
0
      && !operand_type_register_match(i.types[j],
9411
0
              t_types[j],
9412
0
              type,
9413
0
              t_types[op]))
9414
0
    goto bad_broadcast;
9415
0
      }
9416
0
  }
9417
0
    }
9418
  /* If broadcast is supported in this instruction, we need to check if
9419
     operand of one-element size isn't specified without broadcast.  */
9420
5.52k
  else if (t->opcode_modifier.broadcast && i.mem_operands)
9421
0
    {
9422
      /* Find memory operand.  */
9423
0
      for (op = i.imm_operands; op < i.operands; op++)
9424
0
  if (i.flags[op] & Operand_Mem)
9425
0
    break;
9426
0
      gas_assert (op < i.operands);
9427
      /* Check size of the memory operand.  */
9428
0
      if (match_broadcast_size (t, op))
9429
0
  {
9430
0
    i.error = broadcast_needed;
9431
0
    return 1;
9432
0
  }
9433
0
    }
9434
5.52k
  else
9435
5.52k
    op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning.  */
9436
9437
  /* Check if requested masking is supported.  */
9438
5.52k
  if (i.mask.reg)
9439
0
    {
9440
0
      if (!t->opcode_modifier.masking)
9441
0
  {
9442
0
    i.error = unsupported_masking;
9443
0
    return 1;
9444
0
  }
9445
9446
      /* Common rules for masking:
9447
   - mask register destinations permit only zeroing-masking, without
9448
     that actually being expressed by a {z} operand suffix or EVEX.z,
9449
   - memory destinations allow only merging-masking,
9450
   - scatter/gather insns (i.e. ones using vSIB) only allow merging-
9451
     masking.  */
9452
0
      if (i.mask.zeroing
9453
0
    && (t_types[t->operands - 1].bitfield.class == RegMask
9454
0
        || (i.flags[t->operands - 1] & Operand_Mem)
9455
0
        || t->opcode_modifier.sib))
9456
0
  {
9457
0
    i.error = unsupported_masking;
9458
0
    return 1;
9459
0
  }
9460
0
    }
9461
9462
  /* Check if masking is applied to dest operand.  */
9463
5.52k
  if (i.mask.reg && (i.mask.operand != i.operands - 1))
9464
0
    {
9465
0
      i.error = mask_not_on_destination;
9466
0
      return 1;
9467
0
    }
9468
9469
  /* Check RC/SAE.  */
9470
5.52k
  if (i.rounding.type != rc_none)
9471
0
    {
9472
0
      if (!t->opcode_modifier.sae
9473
0
    || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
9474
0
    || i.mem_operands)
9475
0
  {
9476
0
    i.error = unsupported_rc_sae;
9477
0
    return 1;
9478
0
  }
9479
9480
      /* Non-EVEX.{LIG,512} forms need to have a ZMM or YMM register as at
9481
   least one operand.  There's no need to check all operands, though:
9482
   Either of the last two operands will be of the right size in all
9483
   relevant templates.  */
9484
0
      if (t->opcode_modifier.evex != EVEXLIG
9485
0
    && t->opcode_modifier.evex != EVEX512
9486
0
    && !i.types[t->operands - 1].bitfield.zmmword
9487
0
    && !i.types[t->operands - 2].bitfield.zmmword)
9488
0
  {
9489
0
    i.error = operand_size_mismatch;
9490
0
    return 1;
9491
0
  }
9492
0
    }
9493
9494
  /* Check the special Imm4 cases; must be the first operand.  */
9495
5.52k
  if ((is_cpu (t, CpuXOP) && t->operands == 5)
9496
5.52k
      || (t->opcode_space == SPACE_0F3A
9497
0
    && (t->base_opcode | 3) == 0x0b
9498
0
    && (is_cpu (t, CpuAPX_F)
9499
0
     || (t->opcode_modifier.sse2avx && t->opcode_modifier.evex
9500
0
         && (!t->opcode_modifier.vex
9501
0
       || (pp.encoding != encoding_default
9502
0
           && pp.encoding != encoding_vex
9503
0
           && pp.encoding != encoding_vex3))))))
9504
0
    {
9505
0
      if (i.op[0].imms->X_op != O_constant
9506
0
    || !fits_in_imm4 (i.op[0].imms->X_add_number))
9507
0
  {
9508
0
    i.error = bad_imm4;
9509
0
    return 1;
9510
0
  }
9511
9512
      /* Turn off Imm<N> so that update_imm won't complain.  */
9513
0
      if (t->operands == 5)
9514
0
  operand_type_set (&i.types[0], 0);
9515
0
    }
9516
9517
  /* Check vector Disp8 operand.  */
9518
5.52k
  if (t->opcode_modifier.disp8memshift
9519
51
      && (!t->opcode_modifier.vex
9520
0
    || need_evex_encoding (t))
9521
51
      && pp.disp_encoding <= disp_encoding_8bit)
9522
51
    {
9523
51
      if (i.broadcast.type || i.broadcast.bytes)
9524
0
  i.memshift = t->opcode_modifier.broadcast - 1;
9525
51
      else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
9526
0
  i.memshift = t->opcode_modifier.disp8memshift;
9527
51
      else
9528
51
  {
9529
51
    const i386_operand_type *type = NULL, *fallback = NULL;
9530
9531
51
    i.memshift = 0;
9532
153
    for (op = i.imm_operands; op < i.operands; op++)
9533
102
      if (i.flags[op] & Operand_Mem)
9534
0
        {
9535
0
    if (t->opcode_modifier.evex == EVEXLIG)
9536
0
      i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
9537
0
    else if (t_types[op].bitfield.xmmword
9538
0
       + t_types[op].bitfield.ymmword
9539
0
       + t_types[op].bitfield.zmmword <= 1)
9540
0
      type = &t_types[op];
9541
0
    else if (!i.types[op].bitfield.unspecified)
9542
0
      type = &i.types[op];
9543
0
    else /* Ambiguities get resolved elsewhere.  */
9544
0
      fallback = &t_types[op];
9545
0
        }
9546
102
      else if (i.types[op].bitfield.class == RegSIMD
9547
102
         && t->opcode_modifier.evex != EVEXLIG)
9548
102
        {
9549
102
    if (i.types[op].bitfield.zmmword)
9550
0
      i.memshift = 6;
9551
102
    else if (i.types[op].bitfield.ymmword && i.memshift < 5)
9552
0
      i.memshift = 5;
9553
102
    else if (i.types[op].bitfield.xmmword && i.memshift < 4)
9554
51
      i.memshift = 4;
9555
102
        }
9556
9557
51
    if (!type && !i.memshift)
9558
0
      type = fallback;
9559
51
    if (type)
9560
0
      {
9561
0
        if (type->bitfield.zmmword)
9562
0
    i.memshift = 6;
9563
0
        else if (type->bitfield.ymmword)
9564
0
    i.memshift = 5;
9565
0
        else if (type->bitfield.xmmword)
9566
0
    i.memshift = 4;
9567
0
      }
9568
9569
    /* For the check in fits_in_disp8().  */
9570
51
    if (i.memshift == 0)
9571
0
      i.memshift = -1;
9572
51
  }
9573
9574
153
      for (op = i.imm_operands; op < i.operands; op++)
9575
102
  if (operand_type_check (i.types[op], disp)
9576
0
      && i.op[op].disps->X_op == O_constant)
9577
0
    {
9578
      /* Make sure to leave i.types[op].bitfield.disp8 alone upon
9579
         secondary invocations of match_template().  */
9580
0
      if (fits_in_disp8 (i.op[op].disps->X_add_number))
9581
0
        {
9582
0
    if (!i.tm.mnem_off)
9583
0
      i.types[op].bitfield.disp8 = 1;
9584
0
    return 0;
9585
0
        }
9586
0
      if (!i.tm.mnem_off)
9587
0
        i.types[op].bitfield.disp8 = 0;
9588
0
    }
9589
51
    }
9590
9591
5.52k
  i.memshift = 0;
9592
9593
5.52k
  return 0;
9594
5.52k
}
9595
9596
/* Check if encoding requirements are met by the instruction.  */
9597
9598
static int
9599
VEX_check_encoding (const insn_template *t)
9600
6.53k
{
9601
6.53k
  if (pp.encoding == encoding_error)
9602
0
    {
9603
0
      i.error = unsupported;
9604
0
      return 1;
9605
0
    }
9606
9607
  /* Vector size restrictions.  */
9608
6.53k
  if ((vector_size < VSZ512
9609
5.04k
       && t->opcode_modifier.evex == EVEX512)
9610
6.53k
      || (vector_size < VSZ256
9611
5.04k
    && (t->opcode_modifier.evex == EVEX256
9612
5.04k
        || t->opcode_modifier.vex == VEX256)))
9613
0
    {
9614
0
      i.error = unsupported_vector_size;
9615
0
      return 1;
9616
0
    }
9617
9618
6.53k
  switch (pp.encoding)
9619
6.53k
    {
9620
0
    case encoding_vex:
9621
0
    case encoding_vex3:
9622
      /* This instruction must be encoded with VEX prefix.  */
9623
0
      if (!t->opcode_modifier.vex)
9624
0
  {
9625
0
    i.error = no_vex_encoding;
9626
0
    return 1;
9627
0
  }
9628
0
      break;
9629
9630
6.51k
    case encoding_default:
9631
6.51k
      if (!pp.has_nf)
9632
6.51k
  break;
9633
      /* Fall through.  */
9634
20
    case encoding_evex:
9635
20
    case encoding_evex512:
9636
      /* This instruction must be encoded with EVEX prefix.  */
9637
20
      if (!t->opcode_modifier.evex)
9638
10
  {
9639
10
    i.error = no_evex_encoding;
9640
10
    return 1;
9641
10
  }
9642
10
      break;
9643
9644
10
    case encoding_egpr:
9645
      /* This instruction must be encoded with REX2 or EVEX prefix.  */
9646
0
      if (t->opcode_modifier.vex && !t->opcode_modifier.evex)
9647
0
  {
9648
0
    i.error = no_evex_encoding;
9649
0
    return 1;
9650
0
  }
9651
0
      break;
9652
9653
0
    default:
9654
0
      abort ();
9655
6.53k
    }
9656
9657
6.52k
  return 0;
9658
6.53k
}
9659
9660
/* Check if Egprs operands are valid for the instruction.  */
9661
9662
static bool
9663
check_EgprOperands (const insn_template *t)
9664
5.53k
{
9665
5.53k
  if (!t->opcode_modifier.noegpr)
9666
5.07k
    return false;
9667
9668
719
  for (unsigned int op = i.imm_operands; op < i.operands; op++)
9669
260
    {
9670
260
      if (i.types[op].bitfield.class != Reg)
9671
260
  continue;
9672
9673
0
      if (i.op[op].regs->reg_flags & RegRex2)
9674
0
  {
9675
0
    i.error = register_type_mismatch;
9676
0
    return true;
9677
0
  }
9678
0
    }
9679
9680
459
  if ((i.index_reg && (i.index_reg->reg_flags & RegRex2))
9681
459
      || (i.base_reg && (i.base_reg->reg_flags & RegRex2)))
9682
0
    {
9683
0
      i.error = unsupported_EGPR_for_addressing;
9684
0
      return true;
9685
0
    }
9686
9687
  /* Check if pseudo prefix {rex2} is valid.  */
9688
459
  if (pp.rex2_encoding && !t->opcode_modifier.sse2avx)
9689
0
    {
9690
0
      i.error = invalid_pseudo_prefix;
9691
0
      return true;
9692
0
    }
9693
9694
459
  return false;
9695
459
}
9696
9697
/* Check if APX operands are valid for the instruction.  */
9698
static bool
9699
check_APX_operands (const insn_template *t)
9700
5.52k
{
9701
  /* Push2* and Pop2* cannot use RSP and Pop2* cannot pop two same registers.
9702
   */
9703
5.52k
  switch (t->mnem_off)
9704
5.52k
    {
9705
0
    case MN_pop2:
9706
0
    case MN_pop2p:
9707
0
      if (register_number (i.op[0].regs) == register_number (i.op[1].regs))
9708
0
  {
9709
0
    i.error = invalid_dest_register_set;
9710
0
    return 1;
9711
0
  }
9712
    /* fall through */
9713
0
    case MN_push2:
9714
0
    case MN_push2p:
9715
0
      if (register_number (i.op[0].regs) == 4
9716
0
    || register_number (i.op[1].regs) == 4)
9717
0
  {
9718
0
    i.error = unsupported_rsp_register;
9719
0
    return 1;
9720
0
  }
9721
0
      break;
9722
5.52k
    }
9723
5.52k
  return 0;
9724
5.52k
}
9725
9726
/* Check if the instruction use the REX registers or REX prefix.  */
9727
static bool
9728
check_Rex_required (void)
9729
0
{
9730
0
  for (unsigned int op = i.imm_operands; op < i.operands; op++)
9731
0
    {
9732
0
      if (i.types[op].bitfield.class != Reg)
9733
0
  continue;
9734
9735
0
      if (i.op[op].regs->reg_flags & (RegRex | RegRex64))
9736
0
  return true;
9737
0
    }
9738
9739
0
  if ((i.index_reg && (i.index_reg->reg_flags & RegRex))
9740
0
      || (i.base_reg && (i.base_reg->reg_flags & RegRex)))
9741
0
    return true;
9742
9743
  /* Check pseudo prefix {rex} are valid.  */
9744
0
  return pp.rex_encoding;
9745
0
}
9746
9747
/* Optimize APX NDD insns to legacy insns.  */
9748
static unsigned int
9749
can_convert_NDD_to_legacy (const insn_template *t)
9750
0
{
9751
0
  unsigned int match_dest_op = ~0;
9752
9753
0
  if (!pp.has_nf && i.reg_operands >= 2)
9754
0
    {
9755
0
      unsigned int dest = i.operands - 1;
9756
0
      unsigned int src1 = i.operands - 2;
9757
0
      unsigned int src2 = (i.operands > 3) ? i.operands - 3 : 0;
9758
9759
0
      if (i.types[src1].bitfield.class == Reg
9760
0
    && i.op[src1].regs == i.op[dest].regs)
9761
0
  match_dest_op = src1;
9762
      /* If the first operand is the same as the third operand,
9763
   these instructions need to support the ability to commutative
9764
   the first two operands and still not change the semantics in order
9765
   to be optimized.  */
9766
0
      else if (optimize > 1
9767
0
         && t->opcode_modifier.commutative
9768
0
         && i.types[src2].bitfield.class == Reg
9769
0
         && i.op[src2].regs == i.op[dest].regs)
9770
0
  match_dest_op = src2;
9771
0
    }
9772
0
  return match_dest_op;
9773
0
}
9774
9775
/* Helper function for the progress() macro in match_template().  */
9776
static INLINE enum i386_error progress (enum i386_error new,
9777
          enum i386_error last,
9778
          unsigned int line, unsigned int *line_p)
9779
245k
{
9780
245k
  if (line <= *line_p)
9781
173k
    return last;
9782
71.2k
  *line_p = line;
9783
71.2k
  return new;
9784
245k
}
9785
9786
static const insn_template *
9787
match_template (char mnem_suffix)
9788
11.0k
{
9789
  /* Points to template once we've found it.  */
9790
11.0k
  const insn_template *t;
9791
11.0k
  i386_operand_type overlap0, overlap1, overlap2, overlap3;
9792
11.0k
  i386_operand_type overlap4;
9793
11.0k
  unsigned int found_reverse_match;
9794
11.0k
  i386_operand_type operand_types [MAX_OPERANDS];
9795
11.0k
  int addr_prefix_disp;
9796
11.0k
  unsigned int j, size_match, check_register, errline = __LINE__;
9797
11.0k
  enum i386_error specific_error = number_of_operands_mismatch;
9798
245k
#define progress(err) progress (err, specific_error, __LINE__, &errline)
9799
9800
#if MAX_OPERANDS != 5
9801
# error "MAX_OPERANDS must be 5."
9802
#endif
9803
9804
11.0k
  found_reverse_match = 0;
9805
11.0k
  addr_prefix_disp = -1;
9806
9807
78.0k
  for (t = current_templates.start; t < current_templates.end; t++)
9808
73.5k
    {
9809
73.5k
      addr_prefix_disp = -1;
9810
73.5k
      found_reverse_match = 0;
9811
9812
      /* Must have right number of operands.  */
9813
73.5k
      if (i.operands != t->operands)
9814
26.7k
  continue;
9815
9816
      /* Skip SSE2AVX templates when inapplicable.  */
9817
46.7k
      if (t->opcode_modifier.sse2avx
9818
292
    && (!sse2avx || i.prefix[DATA_PREFIX]))
9819
292
  {
9820
    /* Another non-SSE2AVX template has to follow.  */
9821
292
    gas_assert (t + 1 < current_templates.end);
9822
292
    continue;
9823
292
  }
9824
9825
      /* Check processor support.  */
9826
46.4k
      specific_error = progress (unsupported);
9827
46.4k
      if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
9828
19.9k
  continue;
9829
9830
      /* Check AT&T mnemonic.   */
9831
26.5k
      specific_error = progress (unsupported_with_intel_mnemonic);
9832
26.5k
      if (!intel_syntax && intel_mnemonic
9833
0
    && t->opcode_modifier.dialect == ATT_MNEMONIC)
9834
0
  continue;
9835
9836
      /* Check AT&T/Intel syntax.  */
9837
26.5k
      specific_error = progress (unsupported_syntax);
9838
26.5k
      if (intel_syntax
9839
26.5k
     ? t->opcode_modifier.dialect >= ATT_SYNTAX
9840
26.5k
     : t->opcode_modifier.dialect == INTEL_SYNTAX)
9841
4
  continue;
9842
9843
      /* Check NF support.  */
9844
26.5k
      specific_error = progress (unsupported_nf);
9845
26.5k
      if (pp.has_nf && !t->opcode_modifier.nf)
9846
0
  continue;
9847
9848
      /* Check Intel64/AMD64 ISA.   */
9849
26.5k
      switch (isa64)
9850
26.5k
  {
9851
26.5k
  default:
9852
    /* Default: Don't accept Intel64.  */
9853
26.5k
    if (t->opcode_modifier.isa64 == INTEL64)
9854
11
      continue;
9855
26.5k
    break;
9856
26.5k
  case amd64:
9857
    /* -mamd64: Don't accept Intel64 and Intel64 only.  */
9858
0
    if (t->opcode_modifier.isa64 >= INTEL64)
9859
0
      continue;
9860
0
    break;
9861
0
  case intel64:
9862
    /* -mintel64: Don't accept AMD64.  */
9863
0
    if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
9864
0
      continue;
9865
0
    break;
9866
26.5k
  }
9867
9868
      /* Check the suffix.  */
9869
26.5k
      specific_error = progress (invalid_instruction_suffix);
9870
26.5k
      if ((t->opcode_modifier.no_bsuf && mnem_suffix == BYTE_MNEM_SUFFIX)
9871
24.5k
    || (t->opcode_modifier.no_wsuf && mnem_suffix == WORD_MNEM_SUFFIX)
9872
24.5k
    || (t->opcode_modifier.no_lsuf && mnem_suffix == LONG_MNEM_SUFFIX)
9873
24.5k
    || (t->opcode_modifier.no_ssuf && mnem_suffix == SHORT_MNEM_SUFFIX)
9874
24.5k
    || (t->opcode_modifier.no_qsuf && mnem_suffix == QWORD_MNEM_SUFFIX))
9875
1.96k
  continue;
9876
9877
24.5k
      specific_error = progress (operand_size_mismatch);
9878
24.5k
      size_match = operand_size_match (t);
9879
24.5k
      if (!size_match)
9880
974
  continue;
9881
9882
      /* This is intentionally not
9883
9884
   if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
9885
9886
   as the case of a missing * on the operand is accepted (perhaps with
9887
   a warning, issued further down).  */
9888
23.5k
      specific_error = progress (operand_type_mismatch);
9889
23.5k
      if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
9890
11
  continue;
9891
9892
23.5k
      const i386_operand_type *t_types = get_operand_types (t);
9893
58.4k
      for (j = 0; j < t->operands; ++j)
9894
34.8k
  operand_types[j] = t_types[j];
9895
106k
      for (; j < MAX_OPERANDS; ++j)
9896
83.0k
  operand_types[j] = (i386_operand_type){ .array[0] = 0 };
9897
9898
      /* In Intel syntax, normally we can check for memory operand size when
9899
   there is no mnemonic suffix.  But jmp and call have 2 different
9900
   encodings with Dword memory operand size.  Skip the "near" one
9901
   (permitting a register operand) when "far" was requested.  */
9902
23.5k
      if (i.far_branch
9903
0
    && t->opcode_modifier.jump == JUMP_ABSOLUTE
9904
0
    && operand_types[0].bitfield.class == Reg)
9905
0
  continue;
9906
9907
      /* In general, don't allow 32-bit operands on pre-386.  */
9908
23.5k
      specific_error = progress (mnem_suffix ? invalid_instruction_suffix
9909
23.5k
               : operand_size_mismatch);
9910
23.5k
      j = i.imm_operands + (t->operands > i.imm_operands + 1);
9911
23.5k
      if (i.suffix == LONG_MNEM_SUFFIX
9912
110
    && !cpu_arch_flags.bitfield.cpui386
9913
0
    && (intel_syntax
9914
0
        ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
9915
0
     && !intel_float_operand (insn_name (t)))
9916
0
        : intel_float_operand (insn_name (t)) != 2)
9917
0
    && (t->operands == i.imm_operands
9918
0
        || (operand_types[i.imm_operands].bitfield.class != RegMMX
9919
0
         && operand_types[i.imm_operands].bitfield.class != RegSIMD
9920
0
         && operand_types[i.imm_operands].bitfield.class != RegMask)
9921
0
        || (operand_types[j].bitfield.class != RegMMX
9922
0
      && operand_types[j].bitfield.class != RegSIMD
9923
0
      && operand_types[j].bitfield.class != RegMask))
9924
0
    && !t->opcode_modifier.sib)
9925
0
  continue;
9926
9927
      /* Do not verify operands when there are none.  */
9928
23.5k
      if (!t->operands)
9929
993
  {
9930
993
    if (VEX_check_encoding (t))
9931
0
      {
9932
0
        specific_error = progress (i.error);
9933
0
        continue;
9934
0
      }
9935
9936
    /* Check if pseudo prefix {rex2} is valid.  */
9937
993
    if (t->opcode_modifier.noegpr && pp.rex2_encoding)
9938
0
      {
9939
0
        specific_error = progress (invalid_pseudo_prefix);
9940
0
        continue;
9941
0
      }
9942
9943
    /* We've found a match; break out of loop.  */
9944
993
    break;
9945
993
  }
9946
9947
22.5k
      if (!t->opcode_modifier.jump
9948
195
    || t->opcode_modifier.jump == JUMP_ABSOLUTE)
9949
22.4k
  {
9950
    /* There should be only one Disp operand.  */
9951
87.6k
    for (j = 0; j < MAX_OPERANDS; j++)
9952
76.1k
      if (operand_type_check (operand_types[j], disp))
9953
10.9k
        break;
9954
22.4k
    if (j < MAX_OPERANDS)
9955
10.9k
      {
9956
10.9k
        bool override = (i.prefix[ADDR_PREFIX] != 0);
9957
9958
10.9k
        addr_prefix_disp = j;
9959
9960
        /* Address size prefix will turn Disp64 operand into Disp32 and
9961
     Disp32/Disp16 one into Disp16/Disp32 respectively.  */
9962
10.9k
        switch (flag_code)
9963
10.9k
    {
9964
952
    case CODE_16BIT:
9965
952
      override = !override;
9966
      /* Fall through.  */
9967
952
    case CODE_32BIT:
9968
952
      if (operand_types[j].bitfield.disp32
9969
952
          && operand_types[j].bitfield.disp16)
9970
952
        {
9971
952
          operand_types[j].bitfield.disp16 = override;
9972
952
          operand_types[j].bitfield.disp32 = !override;
9973
952
        }
9974
952
      gas_assert (!operand_types[j].bitfield.disp64);
9975
952
      break;
9976
9977
9.95k
    case CODE_64BIT:
9978
9.95k
      if (operand_types[j].bitfield.disp64)
9979
345
        {
9980
345
          gas_assert (!operand_types[j].bitfield.disp32);
9981
345
          operand_types[j].bitfield.disp32 = override;
9982
345
          operand_types[j].bitfield.disp64 = !override;
9983
345
        }
9984
9.95k
      operand_types[j].bitfield.disp16 = 0;
9985
9.95k
      break;
9986
10.9k
    }
9987
10.9k
      }
9988
22.4k
  }
9989
9990
      /* We check register size if needed.  */
9991
22.5k
      if (t->opcode_modifier.checkoperandsize)
9992
4.07k
  {
9993
4.07k
    check_register = (1 << t->operands) - 1;
9994
4.07k
    if (i.broadcast.type || i.broadcast.bytes)
9995
10
      check_register &= ~(1 << i.broadcast.operand);
9996
4.07k
  }
9997
18.5k
      else
9998
18.5k
  check_register = 0;
9999
10000
22.5k
      overlap0 = operand_type_and (i.types[0], operand_types[0]);
10001
22.5k
      switch (t->operands)
10002
22.5k
  {
10003
10.5k
  case 1:
10004
10.5k
    if (!operand_type_match (overlap0, i.types[0]))
10005
7.60k
      {
10006
7.60k
        specific_error = progress (i.error);
10007
7.60k
        continue;
10008
7.60k
      }
10009
10010
    /* Allow the ModR/M encoding to be requested by using the {load} or
10011
       {store} pseudo prefix on an applicable insn.  */
10012
2.95k
    if (!t->opcode_modifier.modrm
10013
2.89k
        && i.reg_operands == 1
10014
266
        && ((pp.dir_encoding == dir_encoding_load
10015
0
       && t->mnem_off != MN_pop)
10016
266
      || (pp.dir_encoding == dir_encoding_store
10017
0
          && t->mnem_off != MN_push))
10018
        /* Avoid BSWAP.  */
10019
0
        && t->mnem_off != MN_bswap)
10020
0
      continue;
10021
2.95k
    break;
10022
10023
11.7k
  case 2:
10024
    /* xchg %eax, %eax is a special case. It is an alias for nop
10025
       only in 32bit mode and we can use opcode 0x90.  In 64bit
10026
       mode, we can't use 0x90 for xchg %eax, %eax since it should
10027
       zero-extend %eax to %rax.  */
10028
11.7k
    if (t->base_opcode == 0x90
10029
0
        && t->opcode_space == SPACE_BASE)
10030
0
      {
10031
0
        if (flag_code == CODE_64BIT
10032
0
      && i.types[0].bitfield.instance == Accum
10033
0
      && i.types[0].bitfield.dword
10034
0
      && i.types[1].bitfield.instance == Accum)
10035
0
    continue;
10036
10037
        /* Allow the ModR/M encoding to be requested by using the
10038
     {load} or {store} pseudo prefix.  */
10039
0
        if (pp.dir_encoding == dir_encoding_load
10040
0
      || pp.dir_encoding == dir_encoding_store)
10041
0
    continue;
10042
0
      }
10043
10044
11.7k
    if (t->base_opcode == MOV_AX_DISP32
10045
365
        && t->opcode_space == SPACE_BASE
10046
365
        && t->mnem_off != MN_movabs)
10047
365
      {
10048
        /* Force 0x8b encoding for "mov foo@GOT, %eax".  */
10049
365
        if (i.reloc[0] == BFD_RELOC_386_GOT32)
10050
0
    continue;
10051
10052
        /* xrelease mov %eax, <disp> is another special case. It must not
10053
     match the accumulator-only encoding of mov.  */
10054
365
        if (i.hle_prefix)
10055
0
    continue;
10056
10057
        /* Allow the ModR/M encoding to be requested by using a suitable
10058
     {load} or {store} pseudo prefix.  */
10059
365
        if (pp.dir_encoding == (i.types[0].bitfield.instance == Accum
10060
365
             ? dir_encoding_store
10061
365
             : dir_encoding_load)
10062
0
      && !i.types[0].bitfield.disp64
10063
0
      && !i.types[1].bitfield.disp64)
10064
0
    continue;
10065
365
      }
10066
10067
    /* Allow the ModR/M encoding to be requested by using the {load} or
10068
       {store} pseudo prefix on an applicable insn.  */
10069
11.7k
    if (!t->opcode_modifier.modrm
10070
3.39k
        && i.reg_operands == 1
10071
91
        && i.imm_operands == 1
10072
34
        && (pp.dir_encoding == dir_encoding_load
10073
34
      || pp.dir_encoding == dir_encoding_store)
10074
0
        && t->opcode_space == SPACE_BASE)
10075
0
      {
10076
0
        if (t->base_opcode == 0xb0 /* mov $imm, %reg */
10077
0
      && pp.dir_encoding == dir_encoding_store)
10078
0
    continue;
10079
10080
0
        if ((t->base_opcode | 0x38) == 0x3c /* <alu> $imm, %acc */
10081
0
      && (t->base_opcode != 0x3c /* cmp $imm, %acc */
10082
0
          || pp.dir_encoding == dir_encoding_load))
10083
0
    continue;
10084
10085
0
        if (t->base_opcode == 0xa8 /* test $imm, %acc */
10086
0
      && pp.dir_encoding == dir_encoding_load)
10087
0
    continue;
10088
0
      }
10089
    /* Fall through.  */
10090
10091
12.0k
  case 3:
10092
12.0k
    if (!(size_match & MATCH_STRAIGHT))
10093
36
      goto check_reverse;
10094
    /* Reverse direction of operands if swapping is possible in the first
10095
       place (operands need to be symmetric) and
10096
       - the load form is requested, and the template is a store form,
10097
       - the store form is requested, and the template is a load form,
10098
       - the non-default (swapped) form is requested.  */
10099
11.9k
    overlap1 = operand_type_and (operand_types[0], operand_types[1]);
10100
10101
11.9k
    j = i.operands - 1 - (t->opcode_space == SPACE_MAP4
10102
23
        && t->opcode_modifier.vexvvvv);
10103
10104
11.9k
    if (t->opcode_modifier.d && i.reg_operands == i.operands
10105
899
        && !operand_type_all_zero (&overlap1))
10106
649
      switch (pp.dir_encoding)
10107
649
        {
10108
0
        case dir_encoding_load:
10109
0
    if (operand_type_check (operand_types[j], anymem)
10110
0
        || t->opcode_modifier.regmem)
10111
0
      goto check_reverse;
10112
0
    break;
10113
10114
0
        case dir_encoding_store:
10115
0
    if (!operand_type_check (operand_types[j], anymem)
10116
0
        && !t->opcode_modifier.regmem)
10117
0
      goto check_reverse;
10118
0
    break;
10119
10120
0
        case dir_encoding_swap:
10121
0
    goto check_reverse;
10122
10123
649
        case dir_encoding_default:
10124
649
    break;
10125
649
        }
10126
10127
    /* If we want store form, we skip the current load.  */
10128
11.9k
    if ((pp.dir_encoding == dir_encoding_store
10129
11.9k
         || pp.dir_encoding == dir_encoding_swap)
10130
0
        && i.mem_operands == 0
10131
0
        && t->opcode_modifier.load)
10132
0
      continue;
10133
    /* Fall through.  */
10134
11.9k
  case 4:
10135
11.9k
  case 5:
10136
11.9k
    overlap1 = operand_type_and (i.types[1], operand_types[1]);
10137
11.9k
    if (!operand_type_match (overlap0, i.types[0])
10138
6.58k
        || !operand_type_match (overlap1, i.types[1])
10139
2.80k
        || ((check_register & 3) == 3
10140
644
      && !operand_type_register_match (i.types[0],
10141
644
               operand_types[0],
10142
644
               i.types[1],
10143
644
               operand_types[1])))
10144
9.43k
      {
10145
9.43k
        specific_error = progress (i.error);
10146
10147
        /* Check if other direction is valid ...  */
10148
9.43k
        if (!t->opcode_modifier.d)
10149
5.56k
    continue;
10150
10151
3.90k
      check_reverse:
10152
3.90k
        if (!(size_match & MATCH_REVERSE))
10153
74
    continue;
10154
        /* Try reversing direction of operands.  */
10155
3.83k
        j = is_cpu (t, CpuFMA4)
10156
3.83k
      || is_cpu (t, CpuXOP)
10157
3.83k
      || is_cpu (t, CpuAPX_F)
10158
3.83k
      || is_cpu (t, CpuAPX_NDD) ? 1 : i.operands - 1;
10159
3.83k
        overlap0 = operand_type_and (i.types[0], operand_types[j]);
10160
3.83k
        overlap1 = operand_type_and (i.types[j], operand_types[0]);
10161
3.83k
        overlap2 = operand_type_and (i.types[1], operand_types[1]);
10162
3.83k
        gas_assert (t->operands != 3 || !check_register
10163
3.83k
        || is_cpu (t, CpuAPX_F) || is_cpu (t, CpuAPX_NDD));
10164
3.83k
        if (!operand_type_match (overlap0, i.types[0])
10165
557
      || !operand_type_match (overlap1, i.types[j])
10166
282
      || (t->operands == 3
10167
0
          && !operand_type_match (overlap2, i.types[1]))
10168
282
      || (check_register
10169
282
          && !operand_type_register_match (i.types[0],
10170
282
                   operand_types[j],
10171
282
                   i.types[j],
10172
282
                   operand_types[0])))
10173
3.80k
    {
10174
      /* Does not match either direction.  */
10175
3.80k
      specific_error = progress (i.error);
10176
3.80k
      continue;
10177
3.80k
    }
10178
        /* found_reverse_match holds which variant of D
10179
     we've found.  */
10180
31
        if (!t->opcode_modifier.d)
10181
0
    found_reverse_match = 0;
10182
31
        else if (operand_types[0].bitfield.tbyte)
10183
0
    {
10184
0
      if (t->opcode_modifier.operandconstraint != UGH)
10185
0
        found_reverse_match = Opcode_FloatD;
10186
0
      else
10187
0
        found_reverse_match = ~0;
10188
      /* FSUB{,R} and FDIV{,R} may need a 2nd bit flipped.  */
10189
0
      if ((t->extension_opcode & 4)
10190
0
          && (intel_syntax || intel_mnemonic))
10191
0
        found_reverse_match |= Opcode_FloatR;
10192
0
    }
10193
31
        else if (is_cpu (t, CpuFMA4) || is_cpu (t, CpuXOP)
10194
31
           || is_cpu (t, CpuACE_V1))
10195
0
    {
10196
0
      found_reverse_match = Opcode_VexW;
10197
0
      goto check_operands_345;
10198
0
    }
10199
31
        else if (t->opcode_space == SPACE_MAP4
10200
0
           && t->operands >= 3)
10201
0
    {
10202
0
      found_reverse_match = Opcode_D;
10203
0
      goto check_operands_345;
10204
0
    }
10205
31
        else if (t->opcode_modifier.commutative
10206
           /* CFCMOVcc also wants its major opcode unaltered.  */
10207
31
           || (t->opcode_space == SPACE_MAP4
10208
0
         && (t->base_opcode | 0xf) == 0x4f))
10209
0
    found_reverse_match = ~0;
10210
31
        else if (t->opcode_space != SPACE_BASE
10211
0
           && (t->opcode_space != SPACE_MAP4
10212
         /* MOVBE, originating from SPACE_0F38, also
10213
            belongs here.  */
10214
0
         || t->mnem_off == MN_movbe)
10215
0
           && (t->opcode_space != SPACE_0F
10216
         /* MOV to/from CR/DR/TR, as an exception, follow
10217
            the base opcode space encoding model.  */
10218
0
         || (t->base_opcode | 7) != 0x27))
10219
0
    found_reverse_match = (t->base_opcode & 0xee) != 0x6e
10220
0
              ? Opcode_ExtD : Opcode_SIMD_IntD;
10221
31
        else
10222
31
    found_reverse_match = Opcode_D;
10223
31
      }
10224
2.55k
    else
10225
2.55k
      {
10226
        /* Found a forward 2 operand match here.  */
10227
2.55k
      check_operands_345:
10228
2.55k
        switch (t->operands)
10229
2.55k
    {
10230
0
    case 5:
10231
0
      overlap4 = operand_type_and (i.types[4], operand_types[4]);
10232
0
      if (!operand_type_match (overlap4, i.types[4])
10233
0
          || !operand_type_register_match (i.types[3],
10234
0
                   operand_types[3],
10235
0
                   i.types[4],
10236
0
                   operand_types[4]))
10237
0
        {
10238
0
          specific_error = progress (i.error);
10239
0
          continue;
10240
0
        }
10241
      /* Fall through.  */
10242
0
    case 4:
10243
0
      overlap3 = operand_type_and (i.types[3], operand_types[3]);
10244
0
      if (!operand_type_match (overlap3, i.types[3])
10245
0
          || ((check_register & 0xa) == 0xa
10246
0
        && !operand_type_register_match (i.types[1],
10247
0
                  operand_types[1],
10248
0
                  i.types[3],
10249
0
                  operand_types[3]))
10250
0
          || ((check_register & 0xc) == 0xc
10251
0
        && !operand_type_register_match (i.types[2],
10252
0
                  operand_types[2],
10253
0
                  i.types[3],
10254
0
                  operand_types[3])))
10255
0
        {
10256
0
          specific_error = progress (i.error);
10257
0
          continue;
10258
0
        }
10259
      /* Fall through.  */
10260
1
    case 3:
10261
1
      overlap2 = operand_type_and (i.types[2], operand_types[2]);
10262
1
      if (!operand_type_match (overlap2, i.types[2])
10263
1
          || ((check_register & 5) == 5
10264
1
        && !operand_type_register_match (i.types[0],
10265
1
                  operand_types[0],
10266
1
                  i.types[2],
10267
1
                  operand_types[2]))
10268
1
          || ((check_register & 6) == 6
10269
1
        && !operand_type_register_match (i.types[1],
10270
1
                  operand_types[1],
10271
1
                  i.types[2],
10272
1
                  operand_types[2])))
10273
0
        {
10274
0
          specific_error = progress (i.error);
10275
0
          continue;
10276
0
        }
10277
1
      break;
10278
2.55k
    }
10279
2.55k
      }
10280
    /* Found either forward/reverse 2, 3 or 4 operand match here:
10281
       slip through to break.  */
10282
22.5k
  }
10283
10284
      /* Check if VEX/EVEX encoding requirements can be satisfied.  */
10285
5.54k
      if (VEX_check_encoding (t))
10286
10
  {
10287
10
    specific_error = progress (i.error);
10288
10
    continue;
10289
10
  }
10290
10291
      /* Check if EGPR operands(r16-r31) are valid.  */
10292
5.53k
      if (check_EgprOperands (t))
10293
0
  {
10294
0
    specific_error = progress (i.error);
10295
0
    continue;
10296
0
  }
10297
10298
      /* Check if vector operands are valid.  */
10299
5.53k
      if (check_VecOperands (t))
10300
8
  {
10301
8
    specific_error = progress (i.error);
10302
8
    continue;
10303
8
  }
10304
10305
      /* Check if APX operands are valid.  */
10306
5.52k
      if (check_APX_operands (t))
10307
0
  {
10308
0
    specific_error = progress (i.error);
10309
0
    continue;
10310
0
  }
10311
10312
      /* Check whether to use the shorter VEX encoding for certain insns where
10313
   the EVEX encoding comes first in the table.  This requires the respective
10314
   AVX-* feature to be explicitly enabled.
10315
10316
   Most of the respective insns have just a single EVEX and a single VEX
10317
   template.  The one that's presently different is generated using the
10318
   Vxy / Exy constructs: There are 3 suffix-less EVEX forms, the latter
10319
   two of which may fall back to their two corresponding VEX forms.  */
10320
5.52k
      j = t->mnem_off != MN_vcvtneps2bf16 ? 1 : 2;
10321
5.52k
      if ((t == current_templates.start || j > 1)
10322
322
    && t->opcode_modifier.disp8memshift
10323
49
    && !t->opcode_modifier.vex
10324
49
    && !need_evex_encoding (t)
10325
49
    && t + j < current_templates.end
10326
0
    && t[j].opcode_modifier.vex)
10327
0
  {
10328
0
    i386_cpu_flags cpu;
10329
0
    unsigned int memshift = i.memshift;
10330
10331
0
    i.memshift = 0;
10332
0
    cpu = cpu_flags_and (cpu_flags_from_attr (t[j].cpu),
10333
0
             cpu_arch_isa_flags);
10334
0
    if (!cpu_flags_all_zero (&cpu)
10335
0
        && (!i.types[0].bitfield.disp8
10336
0
      || !operand_type_check (i.types[0], disp)
10337
0
      || i.op[0].disps->X_op != O_constant
10338
0
      || fits_in_disp8 (i.op[0].disps->X_add_number)))
10339
0
      {
10340
0
        specific_error = progress (internal_error);
10341
0
        t += j - 1;
10342
0
        continue;
10343
0
      }
10344
0
    i.memshift = memshift;
10345
0
  }
10346
10347
      /* If we can optimize a NDD insn to legacy insn, like
10348
   add %r16, %r8, %r8 -> add %r16, %r8,
10349
   add  %r8, %r16, %r8 -> add %r16, %r8, then rematch template.
10350
   Note that the semantics have not been changed.  */
10351
5.52k
      if (optimize
10352
0
    && !pp.no_optimize
10353
0
    && pp.encoding != encoding_evex
10354
0
    && ((t + 1 < current_templates.end
10355
0
         && !t[1].opcode_modifier.evex
10356
0
         && t[1].opcode_space <= SPACE_0F38
10357
0
         && t->opcode_modifier.vexvvvv == VexVVVV_DST)
10358
0
        || t->mnem_off == MN_movbe)
10359
0
    && (i.types[i.operands - 1].bitfield.dword
10360
0
        || i.types[i.operands - 1].bitfield.qword))
10361
0
  {
10362
0
    unsigned int match_dest_op = can_convert_NDD_to_legacy (t);
10363
10364
0
    if (match_dest_op != (unsigned int) ~0)
10365
0
      {
10366
0
        size_match = true;
10367
        /* We ensure that the next template has the same input
10368
     operands as the original matching template by the first
10369
     operand (ATT).  To avoid someone support new NDD insns and
10370
     put it in the wrong position.  */
10371
0
        const i386_operand_type *t1_types = get_operand_types (&t[1]);
10372
0
        overlap0 = operand_type_and (i.types[0], t1_types[0]);
10373
0
        if (t->opcode_modifier.d)
10374
0
    overlap1 = operand_type_and (i.types[0], t1_types[1]);
10375
0
        if (!operand_type_match (overlap0, i.types[0])
10376
0
      && (!t->opcode_modifier.d
10377
0
          || !operand_type_match (overlap1, i.types[0])))
10378
0
    size_match = false;
10379
10380
0
        if (size_match
10381
0
      && (t[1].opcode_space <= SPACE_0F
10382
          /* Some non-legacy-map0/1 insns can be shorter when
10383
       legacy-encoded and when no REX prefix is required.  */
10384
0
          || (!check_EgprOperands (t + 1)
10385
0
        && !check_Rex_required ()
10386
0
        && !i.types[i.operands - 1].bitfield.qword)))
10387
0
    {
10388
0
      if (i.operands > 2 && match_dest_op == i.operands - 3)
10389
0
        {
10390
0
          swap_2_operands (match_dest_op, i.operands - 2);
10391
10392
          /* CMOVcc is marked commutative, but then also needs its
10393
       encoded condition inverted.  */
10394
0
          if ((t->base_opcode | 0xf) == 0x4f)
10395
0
      i.invert_cond = true;
10396
0
        }
10397
10398
0
      --i.operands;
10399
0
      --i.reg_operands;
10400
10401
0
      if (t->mnem_off == MN_movbe)
10402
0
        {
10403
0
          gas_assert (t[1].mnem_off == MN_bswap);
10404
0
          ++current_templates.end;
10405
0
        }
10406
10407
0
      specific_error = progress (internal_error);
10408
0
      continue;
10409
0
    }
10410
10411
0
      }
10412
0
  }
10413
10414
      /* We've found a match; break out of loop.  */
10415
5.52k
      break;
10416
5.52k
    }
10417
10418
11.0k
#undef progress
10419
10420
11.0k
  if (t == current_templates.end)
10421
4.52k
    {
10422
      /* We found no match.  */
10423
4.52k
      i.error = specific_error;
10424
4.52k
      return NULL;
10425
4.52k
    }
10426
10427
  /* Don't emit diagnostics or install the template when one was already
10428
     installed, i.e. when called from process_suffix().  */
10429
6.52k
  if (i.tm.mnem_off)
10430
50
    return t;
10431
10432
6.47k
  if (!quiet_warnings)
10433
6.47k
    {
10434
6.47k
      if (!intel_syntax
10435
57
    && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
10436
0
  as_warn (_("indirect %s without `*'"), insn_name (t));
10437
10438
6.47k
      if (t->opcode_modifier.isprefix
10439
165
    && t->opcode_modifier.mnemonicsize == IGNORESIZE)
10440
1
  {
10441
    /* Warn them that a data or address size prefix doesn't
10442
       affect assembly of the next line of code.  */
10443
1
    as_warn (_("stand-alone `%s' prefix"), insn_name (t));
10444
1
  }
10445
10446
6.47k
      if (intel_syntax && mnem_suffix && !t->opcode_modifier.intelsuffix)
10447
1.36k
  {
10448
1.36k
    static bool noticed;
10449
10450
1.36k
    as_warn (_("mnemonic suffix used with `%s'"), insn_name (t));
10451
1.36k
    if (!noticed)
10452
1
      {
10453
1
        noticed = true;
10454
1
        as_warn (_(
10455
1
"NOTE: Such forms are deprecated and will be rejected by a future version of the assembler"));
10456
1
      }
10457
1.36k
  }
10458
6.47k
    }
10459
10460
  /* Copy the template we found.  */
10461
6.47k
  install_template (t);
10462
10463
6.47k
  if (addr_prefix_disp != -1)
10464
2.55k
    i.tm_types[addr_prefix_disp]
10465
2.55k
      = operand_types[addr_prefix_disp];
10466
10467
  /* APX insns acting on byte operands are WIG, yet that can't be expressed
10468
     in the templates (they're also covering word/dword/qword operands).  */
10469
6.47k
  if (t->opcode_space == SPACE_MAP4 && !t->opcode_modifier.vexw &&
10470
3
      i.types[i.operands - 1].bitfield.byte)
10471
0
    {
10472
0
      gas_assert (t->opcode_modifier.w);
10473
0
      i.tm.opcode_modifier.vexw = VEXWIG;
10474
0
    }
10475
10476
6.47k
  switch (found_reverse_match)
10477
6.47k
    {
10478
6.44k
    case 0:
10479
6.44k
      break;
10480
10481
0
    case Opcode_FloatR:
10482
0
    case Opcode_FloatR | Opcode_FloatD:
10483
0
      i.tm.extension_opcode ^= Opcode_FloatR >> 3;
10484
0
      found_reverse_match &= Opcode_FloatD;
10485
10486
      /* Fall through.  */
10487
31
    default:
10488
      /* If we found a reverse match we must alter the opcode direction
10489
   bit and clear/flip the regmem modifier one.  found_reverse_match
10490
   holds bits to change (different for int & float insns).  */
10491
10492
31
      i.tm.base_opcode ^= found_reverse_match;
10493
10494
31
      if (i.tm.opcode_space == SPACE_MAP4)
10495
0
  goto swap_first_2;
10496
10497
      /* Certain SIMD insns have their load forms specified in the opcode
10498
   table, and hence we need to _set_ RegMem instead of clearing it.
10499
   We need to avoid setting the bit though on insns like KMOVW.  */
10500
31
      i.tm.opcode_modifier.regmem
10501
31
  = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
10502
31
    && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
10503
0
    && !i.tm.opcode_modifier.regmem;
10504
10505
      /* Fall through.  */
10506
31
    case ~0:
10507
31
      if (i.tm.opcode_space == SPACE_MAP4
10508
0
    && !t->opcode_modifier.commutative)
10509
0
  i.tm.opcode_modifier.operandconstraint = EVEX_NF;
10510
31
      i.tm_types[0] = operand_types[i.operands - 1];
10511
31
      i.tm_types[i.operands - 1] = operand_types[0];
10512
31
      break;
10513
10514
0
    case Opcode_VexW:
10515
      /* Only the first two register operands need reversing, alongside
10516
   flipping VEX.W.  */
10517
0
      i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
10518
10519
      /* In most 3-operand insns XOP.W changes which operand goes into
10520
   XOP.vvvv.  */
10521
0
      if (!is_cpu (t, CpuACE_V1))
10522
0
  i.tm.opcode_modifier.vexvvvv = VexVVVV_SRC1;
10523
10524
0
    swap_first_2:
10525
0
      j = i.tm_types[0].bitfield.imm8;
10526
0
      i.tm_types[j] = operand_types[j + 1];
10527
0
      i.tm_types[j + 1] = operand_types[j];
10528
0
      break;
10529
6.47k
    }
10530
10531
6.47k
  return t;
10532
6.47k
}
10533
10534
static int
10535
check_string (void)
10536
16
{
10537
16
  unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
10538
16
  unsigned int op = i.tm_types[0].bitfield.baseindex ? es_op : 0;
10539
10540
16
  if (i.seg[op] != NULL && i.seg[op] != reg_es)
10541
0
    {
10542
0
      as_bad (_("`%s' operand %u must use `%ses' segment"),
10543
0
        insn_name (&i.tm),
10544
0
        intel_syntax ? i.tm.operands - es_op : es_op + 1,
10545
0
        register_prefix);
10546
0
      return 0;
10547
0
    }
10548
10549
  /* There's only ever one segment override allowed per instruction.
10550
     This instruction possibly has a legal segment override on the
10551
     second operand, so copy the segment to where non-string
10552
     instructions store it, allowing common code.  */
10553
16
  i.seg[op] = i.seg[1];
10554
10555
16
  return 1;
10556
16
}
10557
10558
static int
10559
process_suffix (const insn_template *t)
10560
6.47k
{
10561
6.47k
  bool is_movx = false;
10562
10563
  /* If matched instruction specifies an explicit instruction mnemonic
10564
     suffix, use it.  */
10565
6.47k
  if (i.tm.opcode_modifier.size == SIZE16)
10566
1
    i.suffix = WORD_MNEM_SUFFIX;
10567
6.47k
  else if (i.tm.opcode_modifier.size == SIZE32)
10568
0
    i.suffix = LONG_MNEM_SUFFIX;
10569
6.47k
  else if (i.tm.opcode_modifier.size == SIZE64)
10570
8
    i.suffix = QWORD_MNEM_SUFFIX;
10571
6.46k
  else if (i.reg_operands
10572
675
     && (i.operands > 1 || i.types[0].bitfield.class == Reg)
10573
534
     && i.tm.opcode_modifier.operandconstraint != ADDR_PREFIX_OP_REG)
10574
534
    {
10575
534
      unsigned int numop = i.operands;
10576
10577
      /* MOVSX/MOVZX */
10578
534
      is_movx = (i.tm.opcode_space == SPACE_0F
10579
68
     && (i.tm.base_opcode | 8) == 0xbe)
10580
534
    || (i.tm.opcode_space == SPACE_BASE
10581
463
        && i.tm.base_opcode == 0x63
10582
0
        && is_cpu (&i.tm, Cpu64));
10583
10584
      /* movsx/movzx want only their source operand considered here, for the
10585
   ambiguity checking below.  The suffix will be replaced afterwards
10586
   to represent the destination (register).  */
10587
534
      if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
10588
0
  --i.operands;
10589
10590
      /* crc32 needs REX.W set regardless of suffix / source operand size.  */
10591
534
      if (i.tm.mnem_off == MN_crc32 && i.tm_types[1].bitfield.qword)
10592
0
        i.rex |= REX_W;
10593
10594
      /* If there's no instruction mnemonic suffix we try to invent one
10595
   based on GPR operands.  */
10596
534
      if (!i.suffix)
10597
474
  {
10598
    /* We take i.suffix from the last register operand specified,
10599
       Destination register type is more significant than source
10600
       register type.  crc32 in SSE4.2 prefers source register
10601
       type. */
10602
474
    unsigned int op = i.tm.mnem_off == MN_crc32 ? 1 : i.operands;
10603
10604
837
    while (op--)
10605
769
      if (i.tm_types[op].bitfield.instance == InstanceNone
10606
0
    || i.tm_types[op].bitfield.instance == Accum)
10607
769
        {
10608
769
    if (i.types[op].bitfield.class != Reg)
10609
363
      continue;
10610
406
    if (i.types[op].bitfield.byte)
10611
10
      i.suffix = BYTE_MNEM_SUFFIX;
10612
396
    else if (i.types[op].bitfield.word)
10613
7
      i.suffix = WORD_MNEM_SUFFIX;
10614
389
    else if (i.types[op].bitfield.dword)
10615
384
      i.suffix = LONG_MNEM_SUFFIX;
10616
5
    else if (i.types[op].bitfield.qword)
10617
5
      i.suffix = QWORD_MNEM_SUFFIX;
10618
0
    else
10619
0
      continue;
10620
406
    break;
10621
406
        }
10622
10623
    /* As an exception, movsx/movzx silently default to a byte source
10624
       in AT&T mode.  */
10625
474
    if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
10626
0
      i.suffix = BYTE_MNEM_SUFFIX;
10627
474
  }
10628
60
      else if (i.suffix == BYTE_MNEM_SUFFIX)
10629
3
  {
10630
3
    if (!check_byte_reg ())
10631
0
      return 0;
10632
3
  }
10633
57
      else if (i.suffix == LONG_MNEM_SUFFIX)
10634
18
  {
10635
18
    if (!check_long_reg ())
10636
18
      return 0;
10637
18
  }
10638
39
      else if (i.suffix == QWORD_MNEM_SUFFIX)
10639
35
  {
10640
35
    if (!check_qword_reg ())
10641
19
      return 0;
10642
35
  }
10643
4
      else if (i.suffix == WORD_MNEM_SUFFIX)
10644
4
  {
10645
4
    if (!check_word_reg ())
10646
0
      return 0;
10647
4
  }
10648
0
      else if (intel_syntax
10649
0
         && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
10650
  /* Do nothing if the instruction is going to ignore the prefix.  */
10651
0
  ;
10652
0
      else
10653
0
  abort ();
10654
10655
      /* Undo the movsx/movzx change done above.  */
10656
497
      i.operands = numop;
10657
497
    }
10658
5.92k
  else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
10659
2.98k
     && !i.suffix)
10660
2.96k
    {
10661
2.96k
      i.suffix = stackop_size;
10662
2.96k
      if (stackop_size == LONG_MNEM_SUFFIX)
10663
140
  {
10664
    /* stackop_size is set to LONG_MNEM_SUFFIX for the
10665
       .code16gcc directive to support 16-bit mode with
10666
       32-bit address.  For IRET without a suffix, generate
10667
       16-bit IRET (opcode 0xcf) to return from an interrupt
10668
       handler.  */
10669
140
    if (i.tm.base_opcode == 0xcf)
10670
0
      {
10671
0
        i.suffix = WORD_MNEM_SUFFIX;
10672
0
        as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
10673
0
      }
10674
    /* Warn about changed behavior for segment register push/pop.  */
10675
140
    else if ((i.tm.base_opcode | 1) == 0x07)
10676
140
      as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
10677
140
         insn_name (&i.tm));
10678
140
  }
10679
2.96k
    }
10680
2.96k
  else if (!i.suffix
10681
1.13k
     && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
10682
1.10k
         || i.tm.opcode_modifier.jump == JUMP_BYTE
10683
1.10k
         || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
10684
1.10k
         || (i.tm.opcode_space == SPACE_0F
10685
18
       && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
10686
18
       && i.tm.extension_opcode <= 3)))
10687
44
    {
10688
44
      switch (flag_code)
10689
44
  {
10690
44
  case CODE_64BIT:
10691
44
    if (!i.tm.opcode_modifier.no_qsuf)
10692
18
      {
10693
18
        if (i.tm.opcode_modifier.jump == JUMP_BYTE
10694
18
      || i.tm.opcode_modifier.no_lsuf)
10695
18
    i.suffix = QWORD_MNEM_SUFFIX;
10696
18
        break;
10697
18
      }
10698
    /* Fall through.  */
10699
26
  case CODE_32BIT:
10700
26
    if (!i.tm.opcode_modifier.no_lsuf)
10701
26
      i.suffix = LONG_MNEM_SUFFIX;
10702
26
    break;
10703
0
  case CODE_16BIT:
10704
0
    if (!i.tm.opcode_modifier.no_wsuf)
10705
0
      i.suffix = WORD_MNEM_SUFFIX;
10706
0
    break;
10707
44
  }
10708
44
    }
10709
10710
6.43k
  if (!i.suffix
10711
3.97k
      && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
10712
    /* Also cover lret/retf/iret in 64-bit mode.  */
10713
2.82k
    || (flag_code == CODE_64BIT
10714
2.79k
        && !i.tm.opcode_modifier.no_lsuf
10715
0
        && !i.tm.opcode_modifier.no_qsuf))
10716
1.15k
      && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
10717
      /* Explicit sizing prefixes are assumed to disambiguate insns.  */
10718
1.15k
      && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
10719
      /* Accept FLDENV et al without suffix.  */
10720
1.09k
      && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
10721
1.09k
    {
10722
1.09k
      unsigned int suffixes, evex = 0;
10723
10724
1.09k
      suffixes = !i.tm.opcode_modifier.no_bsuf;
10725
1.09k
      if (!i.tm.opcode_modifier.no_wsuf)
10726
775
  suffixes |= 1 << 1;
10727
1.09k
      if (!i.tm.opcode_modifier.no_lsuf)
10728
785
  suffixes |= 1 << 2;
10729
1.09k
      if (!i.tm.opcode_modifier.no_ssuf)
10730
10
  suffixes |= 1 << 4;
10731
1.09k
      if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
10732
650
  suffixes |= 1 << 5;
10733
10734
      /* Operand size may be ambiguous only across multiple templates.  Avoid
10735
   the extra effort though if we already know that multiple suffixes /
10736
   operand sizes are allowed.  Also limit this to non-SIMD operand sizes
10737
   (i.e. ones expressable via suffixes) for now.
10738
   There's one special case though that needs excluding: Insns taking
10739
   Disp<N> operands also match templates permitting BaseIndex.  JMP in
10740
   particular would thus wrongly trigger the check further down.  Cover
10741
   JUMP_DWORD insns here as well, just in case.  */
10742
1.09k
      if (i.tm.opcode_modifier.jump != JUMP
10743
1.05k
    && i.tm.opcode_modifier.jump != JUMP_DWORD)
10744
1.10k
  while (!(suffixes & (suffixes - 1)))
10745
322
    {
10746
      /* Sadly check_VecOperands(), running ahead of install_template(),
10747
         may update i.memshift.  Save and restore the value here.  */
10748
322
      unsigned int memshift = i.memshift;
10749
10750
322
      current_templates.start = t + 1;
10751
322
      t = match_template (0);
10752
322
      i.memshift = memshift;
10753
322
      if (t == NULL)
10754
272
        break;
10755
50
      if (!t->opcode_modifier.no_bsuf)
10756
0
        suffixes |= 1 << 0;
10757
50
      if (!t->opcode_modifier.no_wsuf)
10758
0
        suffixes |= 1 << 1;
10759
50
      if (!t->opcode_modifier.no_lsuf)
10760
1
        suffixes |= 1 << 2;
10761
50
      if (!t->opcode_modifier.no_ssuf)
10762
0
        suffixes |= 1 << 4;
10763
50
      if (flag_code == CODE_64BIT && !t->opcode_modifier.no_qsuf)
10764
0
        suffixes |= 1 << 5;
10765
50
    }
10766
10767
      /* For [XYZ]MMWORD operands inspect operand sizes.  While generally
10768
   also suitable for AT&T syntax mode, it was requested that this be
10769
   restricted to just Intel syntax.  */
10770
1.09k
      if (intel_syntax && is_any_vex_encoding (&i.tm)
10771
51
    && !i.broadcast.type && !i.broadcast.bytes)
10772
51
  {
10773
51
    unsigned int op;
10774
10775
149
    for (op = 0; op < i.tm.operands; ++op)
10776
100
      {
10777
100
        if (vector_size < VSZ512)
10778
98
    {
10779
98
      i.tm_types[op].bitfield.zmmword = 0;
10780
98
      if (vector_size < VSZ256)
10781
98
        {
10782
98
          i.tm_types[op].bitfield.ymmword = 0;
10783
98
          if (i.tm_types[op].bitfield.xmmword
10784
98
        && i.tm.opcode_modifier.evex == EVEXDYN)
10785
0
      i.tm.opcode_modifier.evex = EVEX128;
10786
98
        }
10787
0
      else if (i.tm_types[op].bitfield.ymmword
10788
0
         && !i.tm_types[op].bitfield.xmmword
10789
0
         && i.tm.opcode_modifier.evex == EVEXDYN)
10790
0
        i.tm.opcode_modifier.evex = EVEX256;
10791
98
    }
10792
2
        else if (i.tm.opcode_modifier.evex
10793
0
           && !cpu_arch_flags.bitfield.cpuavx512vl)
10794
0
    {
10795
0
      if (i.tm_types[op].bitfield.ymmword)
10796
0
        i.tm_types[op].bitfield.xmmword = 0;
10797
0
      if (i.tm_types[op].bitfield.zmmword)
10798
0
        i.tm_types[op].bitfield.ymmword = 0;
10799
0
      if (i.tm.opcode_modifier.evex == EVEXDYN)
10800
0
        i.tm.opcode_modifier.evex = EVEX512;
10801
0
    }
10802
10803
100
        if (i.tm_types[op].bitfield.xmmword
10804
100
      + i.tm_types[op].bitfield.ymmword
10805
100
      + i.tm_types[op].bitfield.zmmword < 2)
10806
98
    continue;
10807
10808
        /* Any properly sized operand disambiguates the insn.  */
10809
2
        if (i.types[op].bitfield.xmmword
10810
0
      || i.types[op].bitfield.ymmword
10811
0
      || i.types[op].bitfield.zmmword)
10812
2
    {
10813
2
      suffixes &= ~(7 << 6);
10814
2
      evex = 0;
10815
2
      break;
10816
2
    }
10817
10818
0
        if ((i.flags[op] & Operand_Mem)
10819
0
      && i.tm_types[op].bitfield.unspecified)
10820
0
    {
10821
0
      if (i.tm_types[op].bitfield.xmmword)
10822
0
        suffixes |= 1 << 6;
10823
0
      if (i.tm_types[op].bitfield.ymmword)
10824
0
        suffixes |= 1 << 7;
10825
0
      if (i.tm_types[op].bitfield.zmmword)
10826
0
        suffixes |= 1 << 8;
10827
0
      if (i.tm.opcode_modifier.evex)
10828
0
        evex = EVEX512;
10829
0
    }
10830
0
      }
10831
51
  }
10832
10833
      /* Are multiple suffixes / operand sizes allowed?  */
10834
1.09k
      if (suffixes & (suffixes - 1))
10835
785
  {
10836
785
    if (intel_syntax
10837
781
        && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
10838
0
      || operand_check == check_error))
10839
781
      {
10840
781
        as_bad (_("ambiguous operand size for `%s'"), insn_name (&i.tm));
10841
781
        return 0;
10842
781
      }
10843
4
    if (operand_check == check_error)
10844
0
      {
10845
0
        as_bad (_("no instruction mnemonic suffix given and "
10846
0
      "no register operands; can't size `%s'"), insn_name (&i.tm));
10847
0
        return 0;
10848
0
      }
10849
4
    if (operand_check == check_warning)
10850
4
      as_warn (_("%s; using default for `%s'"),
10851
4
           intel_syntax
10852
4
           ? _("ambiguous operand size")
10853
4
           : _("no instruction mnemonic suffix given and "
10854
4
         "no register operands"),
10855
4
           insn_name (&i.tm));
10856
10857
4
    if (i.tm.opcode_modifier.floatmf)
10858
4
      i.suffix = SHORT_MNEM_SUFFIX;
10859
0
    else if (is_movx)
10860
0
      /* handled below */;
10861
0
    else if (evex)
10862
0
      i.tm.opcode_modifier.evex = evex;
10863
0
    else if (flag_code == CODE_16BIT)
10864
0
      i.suffix = WORD_MNEM_SUFFIX;
10865
0
    else if (!i.tm.opcode_modifier.no_lsuf)
10866
0
      i.suffix = LONG_MNEM_SUFFIX;
10867
0
    else
10868
0
      i.suffix = QWORD_MNEM_SUFFIX;
10869
4
  }
10870
1.09k
    }
10871
10872
5.65k
  if (is_movx)
10873
0
    {
10874
      /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
10875
   In AT&T syntax, if there is no suffix (warned about above), the default
10876
   will be byte extension.  */
10877
0
      if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
10878
0
  i.tm.base_opcode |= 1;
10879
10880
      /* For further processing, the suffix should represent the destination
10881
   (register).  This is already the case when one was used with
10882
   mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
10883
   no suffix to begin with.  */
10884
0
      if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
10885
0
  {
10886
0
    if (i.types[1].bitfield.word)
10887
0
      i.suffix = WORD_MNEM_SUFFIX;
10888
0
    else if (i.types[1].bitfield.qword)
10889
0
      i.suffix = QWORD_MNEM_SUFFIX;
10890
0
    else
10891
0
      i.suffix = LONG_MNEM_SUFFIX;
10892
10893
0
    i.tm.opcode_modifier.w = 0;
10894
0
  }
10895
0
    }
10896
10897
5.65k
  if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
10898
283
    i.short_form = (i.tm_types[0].bitfield.class == Reg)
10899
283
       != (i.tm_types[1].bitfield.class == Reg);
10900
10901
  /* Change the opcode based on the operand size given by i.suffix.  */
10902
5.65k
  switch (i.suffix)
10903
5.65k
    {
10904
    /* Size floating point instruction.  */
10905
552
    case LONG_MNEM_SUFFIX:
10906
552
      if (i.tm.opcode_modifier.floatmf)
10907
0
  {
10908
0
    i.tm.base_opcode ^= 4;
10909
0
    break;
10910
0
  }
10911
    /* fall through */
10912
834
    case WORD_MNEM_SUFFIX:
10913
894
    case QWORD_MNEM_SUFFIX:
10914
      /* It's not a byte, select word/dword operation.  */
10915
894
      if (i.tm.opcode_modifier.w)
10916
551
  {
10917
551
    if (i.short_form)
10918
7
      i.tm.base_opcode |= 8;
10919
544
    else
10920
544
      i.tm.base_opcode |= 1;
10921
551
  }
10922
10923
      /* Set mode64 for an operand.  */
10924
894
      if (i.suffix == QWORD_MNEM_SUFFIX)
10925
60
  {
10926
60
    if (flag_code == CODE_64BIT
10927
60
        && !i.tm.opcode_modifier.norex64
10928
42
        && !i.tm.opcode_modifier.vexw
10929
        /* Special case for xchg %rax,%rax.  It is NOP and doesn't
10930
     need rex64. */
10931
42
        && ! (i.operands == 2
10932
34
        && i.tm.base_opcode == 0x90
10933
0
        && i.tm.opcode_space == SPACE_BASE
10934
0
        && i.types[0].bitfield.instance == Accum
10935
0
        && i.types[1].bitfield.instance == Accum))
10936
42
      i.rex |= REX_W;
10937
10938
60
    break;
10939
60
  }
10940
10941
    /* fall through */
10942
838
    case SHORT_MNEM_SUFFIX:
10943
      /* Now select between word & dword operations via the operand
10944
   size prefix, except for instructions that will ignore this
10945
   prefix anyway.  */
10946
838
      if (i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
10947
837
    && !i.tm.opcode_modifier.floatmf
10948
833
    && (!is_any_vex_encoding (&i.tm)
10949
0
        || i.tm.opcode_space == SPACE_MAP4)
10950
833
    && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
10951
138
        || (flag_code == CODE_64BIT
10952
137
      && i.tm.opcode_modifier.jump == JUMP_BYTE)))
10953
695
  {
10954
695
    unsigned int prefix = DATA_PREFIX_OPCODE;
10955
10956
695
    if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
10957
0
      prefix = ADDR_PREFIX_OPCODE;
10958
10959
    /* The DATA PREFIX of EVEX promoted from legacy APX instructions
10960
       needs to be adjusted.  */
10961
695
    if (i.tm.opcode_space == SPACE_MAP4)
10962
0
      {
10963
0
        gas_assert (!i.tm.opcode_modifier.opcodeprefix);
10964
0
        i.tm.opcode_modifier.opcodeprefix = PREFIX_0X66;
10965
0
      }
10966
695
    else if (!add_prefix (prefix))
10967
0
      return 0;
10968
695
  }
10969
10970
838
      break;
10971
10972
3.19k
    case 0:
10973
      /* Select word/dword/qword operation with explicit data sizing prefix
10974
   when there are no suitable register operands.  */
10975
3.19k
      if (i.tm.opcode_modifier.w
10976
16
    && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
10977
16
    && (!i.reg_operands
10978
0
        || (i.reg_operands == 1
10979
          /* ShiftCount */
10980
0
      && (i.tm_types[0].bitfield.instance == RegC
10981
          /* InOutPortReg */
10982
0
          || i.tm_types[0].bitfield.instance == RegD
10983
0
          || i.tm_types[1].bitfield.instance == RegD
10984
0
          || i.tm.mnem_off == MN_crc32))))
10985
16
  i.tm.base_opcode |= 1;
10986
3.19k
      break;
10987
5.65k
    }
10988
10989
5.65k
  if (i.tm.opcode_modifier.operandconstraint == ADDR_PREFIX_OP_REG)
10990
0
    {
10991
0
      gas_assert (!i.suffix);
10992
0
      gas_assert (i.reg_operands);
10993
10994
0
      if (i.tm_types[0].bitfield.instance == Accum
10995
0
    || i.operands == 1)
10996
0
  {
10997
    /* The address size override prefix changes the size of the
10998
       first operand.  */
10999
0
    if (flag_code == CODE_64BIT
11000
0
        && i.types[0].bitfield.word)
11001
0
      {
11002
0
        as_bad (_("16-bit addressing unavailable for `%s'"),
11003
0
          insn_name (&i.tm));
11004
0
        return 0;
11005
0
      }
11006
11007
0
    if ((flag_code == CODE_32BIT
11008
0
         ? i.types[0].bitfield.word
11009
0
         : i.types[0].bitfield.dword)
11010
0
        && !add_prefix (ADDR_PREFIX_OPCODE))
11011
0
      return 0;
11012
0
  }
11013
0
      else
11014
0
  {
11015
    /* Check invalid register operand when the address size override
11016
       prefix changes the size of register operands.  */
11017
0
    unsigned int op;
11018
0
    enum { need_word, need_dword, need_qword } need;
11019
11020
    /* Check the register operand for the address size prefix if
11021
       the memory operand has no real registers, like symbol, DISP
11022
       or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant.  */
11023
0
    if (i.mem_operands == 1
11024
0
        && i.reg_operands == 1
11025
0
        && i.operands == 2
11026
0
        && i.types[1].bitfield.class == Reg
11027
0
        && (flag_code == CODE_32BIT
11028
0
      ? i.types[1].bitfield.word
11029
0
      : i.types[1].bitfield.dword)
11030
0
        && ((i.base_reg == NULL && i.index_reg == NULL)
11031
0
#ifdef OBJ_ELF
11032
0
      || (x86_elf_abi == X86_64_X32_ABI
11033
0
          && i.base_reg
11034
0
          && i.base_reg->reg_num == RegIP
11035
0
          && i.base_reg->reg_type.bitfield.qword))
11036
#else
11037
      || 0)
11038
#endif
11039
0
        && !add_prefix (ADDR_PREFIX_OPCODE))
11040
0
      return 0;
11041
11042
0
    if (flag_code == CODE_32BIT)
11043
0
      need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
11044
0
    else if (i.prefix[ADDR_PREFIX])
11045
0
      need = need_dword;
11046
0
    else
11047
0
      need = flag_code == CODE_64BIT ? need_qword : need_word;
11048
11049
0
    for (op = i.imm_operands; op < i.operands; op++)
11050
0
      {
11051
0
        if (i.types[op].bitfield.class != Reg)
11052
0
    continue;
11053
11054
0
        switch (need)
11055
0
    {
11056
0
    case need_word:
11057
0
      if (i.types[op].bitfield.word)
11058
0
        continue;
11059
0
      break;
11060
0
    case need_dword:
11061
0
      if (i.types[op].bitfield.dword)
11062
0
        continue;
11063
0
      break;
11064
0
    case need_qword:
11065
0
      if (i.types[op].bitfield.qword)
11066
0
        continue;
11067
0
      break;
11068
0
    }
11069
11070
0
        as_bad (_("invalid register operand size for `%s'"),
11071
0
          insn_name (&i.tm));
11072
0
        return 0;
11073
0
      }
11074
0
  }
11075
0
    }
11076
11077
5.65k
  return 1;
11078
5.65k
}
11079
11080
static int
11081
check_byte_reg (void)
11082
3
{
11083
3
  int op;
11084
11085
6
  for (op = i.operands; --op >= 0;)
11086
6
    {
11087
      /* Skip non-register operands. */
11088
6
      if (i.types[op].bitfield.class != Reg)
11089
3
  continue;
11090
11091
      /* If this is an eight bit register, it's OK.  */
11092
3
      if (i.types[op].bitfield.byte)
11093
3
  {
11094
3
    if (i.tm.opcode_modifier.checkoperandsize)
11095
3
      break;
11096
0
    continue;
11097
3
  }
11098
11099
      /* I/O port address operands are OK too.  */
11100
0
      if (i.tm_types[op].bitfield.instance == RegD
11101
0
    && i.tm_types[op].bitfield.word)
11102
0
  continue;
11103
11104
      /* crc32 only wants its source operand checked here.  */
11105
0
      if (i.tm.mnem_off == MN_crc32 && op != 0)
11106
0
  continue;
11107
11108
      /* Any other register is bad.  */
11109
0
      as_bad (_("`%s%s' not allowed with `%s%c'"),
11110
0
        register_prefix, i.op[op].regs->reg_name,
11111
0
        insn_name (&i.tm), i.suffix);
11112
0
      return 0;
11113
0
    }
11114
3
  return 1;
11115
3
}
11116
11117
static int
11118
check_long_reg (void)
11119
18
{
11120
18
  int op;
11121
11122
36
  for (op = i.operands; --op >= 0;)
11123
    /* Skip non-register operands. */
11124
36
    if (i.types[op].bitfield.class != Reg)
11125
18
      continue;
11126
    /* Reject eight bit registers, except where the template requires
11127
       them. (eg. movzb)  */
11128
18
    else if (i.types[op].bitfield.byte
11129
0
       && (i.tm_types[op].bitfield.word
11130
0
     || i.tm_types[op].bitfield.dword
11131
0
     || i.tm_types[op].bitfield.qword))
11132
0
      {
11133
0
  as_bad (_("`%s%s' not allowed with `%s%c'"),
11134
0
    register_prefix,
11135
0
    i.op[op].regs->reg_name,
11136
0
    insn_name (&i.tm),
11137
0
    i.suffix);
11138
0
  return 0;
11139
0
      }
11140
    /* Error if the e prefix on a general reg is missing, or if the r
11141
       prefix on a general reg is present.  */
11142
18
    else if ((i.types[op].bitfield.word
11143
18
        || i.types[op].bitfield.qword)
11144
18
       && i.tm_types[op].bitfield.dword)
11145
18
      {
11146
18
  as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
11147
18
    register_prefix, i.op[op].regs->reg_name,
11148
18
    i.suffix);
11149
18
  return 0;
11150
18
      }
11151
0
    else if (i.tm.opcode_modifier.checkoperandsize)
11152
0
      break;
11153
11154
0
  return 1;
11155
18
}
11156
11157
static int
11158
check_qword_reg (void)
11159
35
{
11160
35
  int op;
11161
11162
51
  for (op = i.operands; --op >= 0; )
11163
    /* Skip non-register operands. */
11164
51
    if (i.types[op].bitfield.class != Reg)
11165
16
      continue;
11166
    /* Reject eight bit registers, except where the template requires
11167
       them. (eg. movzb)  */
11168
35
    else if (i.types[op].bitfield.byte
11169
19
       && (i.tm_types[op].bitfield.word
11170
0
     || i.tm_types[op].bitfield.dword
11171
0
     || i.tm_types[op].bitfield.qword))
11172
19
      {
11173
19
  as_bad (_("`%s%s' not allowed with `%s%c'"),
11174
19
    register_prefix,
11175
19
    i.op[op].regs->reg_name,
11176
19
    insn_name (&i.tm),
11177
19
    i.suffix);
11178
19
  return 0;
11179
19
      }
11180
    /* Error if the r prefix on a general reg is missing.  */
11181
16
    else if ((i.types[op].bitfield.word
11182
16
        || i.types[op].bitfield.dword)
11183
0
       && i.tm_types[op].bitfield.qword)
11184
0
      {
11185
0
  as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
11186
0
    register_prefix, i.op[op].regs->reg_name, i.suffix);
11187
0
  return 0;
11188
0
      }
11189
16
    else if (i.tm.opcode_modifier.checkoperandsize)
11190
16
      break;
11191
11192
16
  return 1;
11193
35
}
11194
11195
static int
11196
check_word_reg (void)
11197
4
{
11198
4
  int op;
11199
4
  for (op = i.operands; --op >= 0;)
11200
    /* Skip non-register operands. */
11201
4
    if (i.types[op].bitfield.class != Reg)
11202
0
      continue;
11203
    /* Reject eight bit registers, except where the template requires
11204
       them. (eg. movzb)  */
11205
4
    else if (i.types[op].bitfield.byte
11206
0
       && (i.tm_types[op].bitfield.word
11207
0
     || i.tm_types[op].bitfield.dword
11208
0
     || i.tm_types[op].bitfield.qword))
11209
0
      {
11210
0
  as_bad (_("`%s%s' not allowed with `%s%c'"),
11211
0
    register_prefix,
11212
0
    i.op[op].regs->reg_name,
11213
0
    insn_name (&i.tm),
11214
0
    i.suffix);
11215
0
  return 0;
11216
0
      }
11217
    /* Error if the e or r prefix on a general reg is present.  */
11218
4
    else if ((i.types[op].bitfield.dword
11219
4
     || i.types[op].bitfield.qword)
11220
0
       && i.tm_types[op].bitfield.word)
11221
0
      {
11222
0
  as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
11223
0
    register_prefix, i.op[op].regs->reg_name,
11224
0
    i.suffix);
11225
0
  return 0;
11226
0
      }
11227
4
    else if (i.tm.opcode_modifier.checkoperandsize)
11228
4
      break;
11229
11230
4
  return 1;
11231
4
}
11232
11233
static int
11234
update_imm (unsigned int j)
11235
6.37k
{
11236
6.37k
  i386_operand_type overlap = i.types[j];
11237
11238
6.37k
  if (i.tm_types[j].bitfield.imm8
11239
1.57k
      && i.tm_types[j].bitfield.imm8s
11240
0
      && overlap.bitfield.imm8 && overlap.bitfield.imm8s)
11241
0
    {
11242
      /* This combination is used on 8-bit immediates where e.g. $~0 is
11243
   desirable to permit.  We're past operand type matching, so simply
11244
   put things back in the shape they were before introducing the
11245
   distinction between Imm8, Imm8S, and Imm8|Imm8S.  */
11246
0
      overlap.bitfield.imm8s = 0;
11247
0
    }
11248
11249
6.37k
  if (overlap.bitfield.imm8
11250
6.37k
      + overlap.bitfield.imm8s
11251
6.37k
      + overlap.bitfield.imm16
11252
6.37k
      + overlap.bitfield.imm32
11253
6.37k
      + overlap.bitfield.imm32s
11254
6.37k
      + overlap.bitfield.imm64 > 1)
11255
3.54k
    {
11256
3.54k
      static const i386_operand_type imm16 = { .bitfield = { .imm16 = 1 } };
11257
3.54k
      static const i386_operand_type imm32 = { .bitfield = { .imm32 = 1 } };
11258
3.54k
      static const i386_operand_type imm32s = { .bitfield = { .imm32s = 1 } };
11259
3.54k
      static const i386_operand_type imm16_32 = { .bitfield =
11260
3.54k
  { .imm16 = 1, .imm32 = 1 }
11261
3.54k
      };
11262
3.54k
      static const i386_operand_type imm16_32s =  { .bitfield =
11263
3.54k
  { .imm16 = 1, .imm32s = 1 }
11264
3.54k
      };
11265
3.54k
      static const i386_operand_type imm16_32_32s = { .bitfield =
11266
3.54k
  { .imm16 = 1, .imm32 = 1, .imm32s = 1 }
11267
3.54k
      };
11268
11269
3.54k
      if (i.suffix)
11270
1.31k
  {
11271
1.31k
    i386_operand_type temp;
11272
11273
1.31k
    operand_type_set (&temp, 0);
11274
1.31k
    if (i.suffix == BYTE_MNEM_SUFFIX)
11275
1.29k
      {
11276
1.29k
        temp.bitfield.imm8 = overlap.bitfield.imm8;
11277
1.29k
        temp.bitfield.imm8s = overlap.bitfield.imm8s;
11278
1.29k
      }
11279
20
    else if (i.suffix == WORD_MNEM_SUFFIX)
11280
7
      temp.bitfield.imm16 = overlap.bitfield.imm16;
11281
13
    else if (i.suffix == QWORD_MNEM_SUFFIX)
11282
13
      {
11283
13
        temp.bitfield.imm64 = overlap.bitfield.imm64;
11284
13
        temp.bitfield.imm32s = overlap.bitfield.imm32s;
11285
13
      }
11286
0
    else
11287
0
      temp.bitfield.imm32 = overlap.bitfield.imm32;
11288
1.31k
    overlap = temp;
11289
1.31k
  }
11290
2.22k
      else if (operand_type_equal (&overlap, &imm16_32_32s)
11291
2.22k
         || operand_type_equal (&overlap, &imm16_32)
11292
2.22k
         || operand_type_equal (&overlap, &imm16_32s))
11293
2.22k
  {
11294
2.22k
    if ((flag_code == CODE_16BIT)
11295
2.22k
        ^ (i.prefix[DATA_PREFIX] != 0 && !(i.prefix[REX_PREFIX] & REX_W)))
11296
0
      overlap = imm16;
11297
2.22k
    else
11298
2.22k
      overlap = imm32s;
11299
2.22k
  }
11300
0
      else if (i.prefix[REX_PREFIX] & REX_W)
11301
0
  overlap = operand_type_and (overlap, imm32s);
11302
0
      else if (i.prefix[DATA_PREFIX])
11303
0
  overlap = operand_type_and (overlap,
11304
0
            flag_code != CODE_16BIT ? imm16 : imm32);
11305
3.54k
      if (overlap.bitfield.imm8
11306
3.54k
    + overlap.bitfield.imm8s
11307
3.54k
    + overlap.bitfield.imm16
11308
3.54k
    + overlap.bitfield.imm32
11309
3.54k
    + overlap.bitfield.imm32s
11310
3.54k
    + overlap.bitfield.imm64 != 1)
11311
0
  {
11312
0
    as_bad (_("no instruction mnemonic suffix given; "
11313
0
        "can't determine immediate size"));
11314
0
    return 0;
11315
0
  }
11316
3.54k
    }
11317
6.37k
  i.types[j] = overlap;
11318
11319
6.37k
  return 1;
11320
6.37k
}
11321
11322
static int
11323
finalize_imm (void)
11324
5.65k
{
11325
5.65k
  unsigned int j, n;
11326
11327
  /* Update the first 2 immediate operands.  */
11328
5.65k
  n = i.operands > 2 ? 2 : i.operands;
11329
5.65k
  if (n)
11330
4.66k
    {
11331
11.0k
      for (j = 0; j < n; j++)
11332
6.37k
  if (update_imm (j) == 0)
11333
0
    return 0;
11334
11335
      /* The 3rd operand can't be immediate operand.  */
11336
4.66k
      gas_assert (operand_type_check (i.types[2], imm) == 0);
11337
4.66k
    }
11338
11339
5.65k
  return 1;
11340
5.65k
}
11341
11342
static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
11343
         bool do_sse2avx)
11344
615
{
11345
615
  if (r->reg_flags & RegRex)
11346
60
    {
11347
60
      if (i.rex & rex_bit)
11348
0
  as_bad (_("same type of prefix used twice"));
11349
60
      i.rex |= rex_bit;
11350
60
    }
11351
555
  else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
11352
0
    {
11353
0
      gas_assert (i.vex.register_specifier == r);
11354
0
      i.vex.register_specifier += 8;
11355
0
    }
11356
11357
615
  if (r->reg_flags & RegVRex)
11358
4
    i.vrex |= rex_bit;
11359
11360
615
  if (r->reg_flags & RegRex2)
11361
0
    i.rex2 |= rex_bit;
11362
615
}
11363
11364
static INLINE void
11365
set_rex_rex2 (const reg_entry *r, unsigned int rex_bit)
11366
4
{
11367
4
  if ((r->reg_flags & RegRex) != 0)
11368
0
    i.rex |= rex_bit;
11369
4
  if ((r->reg_flags & RegRex2) != 0)
11370
0
    i.rex2 |= rex_bit;
11371
4
}
11372
11373
static int
11374
process_operands (void)
11375
5.22k
{
11376
  /* Default segment register this instruction will use for memory
11377
     accesses.  0 means unknown.  This is only for optimizing out
11378
     unnecessary segment overrides.  */
11379
5.22k
  const reg_entry *default_seg = NULL;
11380
11381
8.23k
  for (unsigned int j = i.imm_operands; j < i.operands; j++)
11382
3.00k
    if (i.types[j].bitfield.instance != InstanceNone)
11383
0
      i.reg_operands--;
11384
11385
5.22k
  if (i.tm.opcode_modifier.sse2avx)
11386
0
    {
11387
      /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
11388
   need converting.  */
11389
0
      i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
11390
0
      i.prefix[REX_PREFIX] = 0;
11391
0
      pp.rex_encoding = 0;
11392
0
      pp.rex2_encoding = 0;
11393
0
    }
11394
  /* ImmExt should be processed after SSE2AVX.  */
11395
5.22k
  else if (i.tm.opcode_modifier.immext)
11396
0
    process_immext ();
11397
11398
  /* TILEZERO and BSRINIT are unusual in that they have a single operand
11399
     encoded in ModR/M.reg, not ModR/M.rm.  To avoid special casing this in
11400
     build_modrm_byte(), fake a new destination operand here, while converting
11401
     the source one to register number 0.  */
11402
5.22k
  if (i.tm.mnem_off == MN_tilezero || i.tm.mnem_off == MN_bsrinit)
11403
0
    {
11404
0
      copy_operand (1, 0);
11405
0
      i.op[0].regs -= i.op[0].regs->reg_num;
11406
0
      i.operands++;
11407
0
      i.reg_operands++;
11408
0
      i.tm.operands++;
11409
0
    }
11410
11411
5.22k
  if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
11412
0
    {
11413
0
      static const i386_operand_type regxmm = {
11414
0
        .bitfield = { .class = RegSIMD, .xmmword = 1 }
11415
0
      };
11416
0
      unsigned int dupl = i.operands;
11417
0
      unsigned int dest = dupl - 1;
11418
0
      unsigned int j;
11419
11420
      /* The destination must be an xmm register.  */
11421
0
      gas_assert (i.reg_operands
11422
0
      && MAX_OPERANDS > dupl
11423
0
      && operand_type_equal (&i.types[dest], &regxmm));
11424
11425
0
      if (i.tm_types[0].bitfield.instance == Accum
11426
0
    && i.tm_types[0].bitfield.xmmword)
11427
0
  {
11428
    /* Keep xmm0 for instructions with VEX prefix and 3
11429
       sources.  */
11430
0
    i.tm_types[0].bitfield.instance = InstanceNone;
11431
0
    i.tm_types[0].bitfield.class = RegSIMD;
11432
0
    i.reg_operands++;
11433
0
    goto duplicate;
11434
0
  }
11435
11436
0
      if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_1ST_XMM0)
11437
0
  {
11438
0
    gas_assert ((MAX_OPERANDS - 1) > dupl);
11439
11440
    /* Add the implicit xmm0 for instructions with VEX prefix
11441
       and 3 sources.  */
11442
0
    for (j = i.operands; j > 0; j--)
11443
0
      copy_operand (j, j - 1);
11444
0
    i.op[0].regs = str_hash_find (reg_hash, "xmm0");
11445
0
    i.types[0] = regxmm;
11446
0
    i.tm_types[0] = regxmm;
11447
11448
0
    i.operands += 2;
11449
0
    i.reg_operands += 2;
11450
0
    i.tm.operands += 2;
11451
11452
0
    dupl++;
11453
0
    dest++;
11454
0
  }
11455
0
      else
11456
0
  {
11457
0
  duplicate:
11458
0
    i.operands++;
11459
0
    i.reg_operands++;
11460
0
    i.tm.operands++;
11461
0
  }
11462
11463
0
      copy_operand (dupl, dest);
11464
11465
0
      if (i.tm.opcode_modifier.immext)
11466
0
  process_immext ();
11467
0
    }
11468
5.22k
  else if (i.tm_types[0].bitfield.instance == Accum
11469
0
     && i.tm.opcode_modifier.modrm)
11470
0
    {
11471
0
      unsigned int j;
11472
11473
0
      for (j = 1; j < i.operands; j++)
11474
0
  copy_operand (j - 1, j);
11475
11476
      /* No adjustment to i.reg_operands: This was already done at the top
11477
   of the function.  */
11478
0
      i.operands--;
11479
0
      i.tm.operands--;
11480
0
    }
11481
5.22k
  else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_GROUP)
11482
0
    {
11483
0
      unsigned int op, extra;
11484
0
      const reg_entry *first;
11485
11486
      /* The second operand must be {x,y,z,t}mmN.  */
11487
0
      gas_assert ((i.operands == 2 || i.operands == 3)
11488
0
      && i.types[1].bitfield.class == RegSIMD);
11489
11490
0
      switch (i.types[i.operands - 1].bitfield.class)
11491
0
  {
11492
0
  case RegSIMD:
11493
0
    op = 1;
11494
0
    if (i.operands == 2)
11495
0
      {
11496
        /* AMX-TRANSPOSE operand 2: N must be a multiple of 2. */
11497
0
        extra = 1;
11498
0
      }
11499
0
    else
11500
0
      {
11501
        /* AVX512-{4FMAPS,4VNNIW} operand 2: N must be a multiple of 4. */
11502
0
        extra = 3;
11503
0
      }
11504
0
    break;
11505
11506
0
  case RegMask:
11507
    /* AVX512-VP2INTERSECT operand 3: N must be a multiple of 2. */
11508
0
    op = 2;
11509
0
    extra = 1;
11510
0
    break;
11511
11512
0
  default:
11513
0
    abort ();
11514
0
  }
11515
11516
0
      first = i.op[op].regs - (register_number (i.op[op].regs) & extra);
11517
0
      if (i.op[op].regs != first)
11518
0
  as_warn (_("operand %u `%s%s' implicitly denotes"
11519
0
       " `%s%s' to `%s%s' group in `%s'"),
11520
0
     intel_syntax ? i.operands - op : op + 1,
11521
0
     register_prefix, i.op[op].regs->reg_name,
11522
0
     register_prefix, first[0].reg_name,
11523
0
     register_prefix, first[extra].reg_name,
11524
0
     insn_name (&i.tm));
11525
0
    }
11526
5.22k
  else if (i.tm.opcode_modifier.operandconstraint == REG_KLUDGE)
11527
0
    {
11528
      /* The imul $imm, %reg instruction is converted into
11529
   imul $imm, %reg, %reg, and the clr %reg instruction
11530
   is converted into xor %reg, %reg.  */
11531
11532
0
      unsigned int first_reg_op;
11533
11534
0
      if (operand_type_check (i.types[0], reg))
11535
0
  first_reg_op = 0;
11536
0
      else
11537
0
  first_reg_op = 1;
11538
      /* Pretend we saw the extra register operand.  */
11539
0
      gas_assert (i.reg_operands == 1
11540
0
      && i.op[first_reg_op + 1].regs == 0);
11541
0
      i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
11542
0
      i.types[first_reg_op + 1] = i.types[first_reg_op];
11543
0
      i.operands++;
11544
0
      i.reg_operands++;
11545
11546
      /* For IMULZU switch around the constraint.  */
11547
0
      if (i.tm.mnem_off == MN_imulzu)
11548
0
  i.tm.opcode_modifier.operandconstraint = ZERO_UPPER;
11549
0
    }
11550
11551
5.22k
  if (i.tm.opcode_modifier.modrm)
11552
2.13k
    {
11553
      /* The opcode is completed (modulo i.tm.extension_opcode which
11554
   must be put into the modrm byte).  Now, we make the modrm and
11555
   index base bytes based on all the info we've collected.  */
11556
11557
2.13k
      default_seg = build_modrm_byte ();
11558
11559
2.13k
      if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
11560
0
  {
11561
    /* Warn about some common errors, but press on regardless.  */
11562
0
    if (i.operands == 2)
11563
0
      {
11564
        /* Reversed arguments on faddp or fmulp.  */
11565
0
        as_warn (_("translating to `%s %s%s,%s%s'"), insn_name (&i.tm),
11566
0
           register_prefix, i.op[!intel_syntax].regs->reg_name,
11567
0
           register_prefix, i.op[intel_syntax].regs->reg_name);
11568
0
      }
11569
0
    else if (i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
11570
0
      {
11571
        /* Extraneous `l' suffix on fp insn.  */
11572
0
        as_warn (_("translating to `%s %s%s'"), insn_name (&i.tm),
11573
0
           register_prefix, i.op[0].regs->reg_name);
11574
0
      }
11575
0
  }
11576
2.13k
    }
11577
3.08k
  else if (i.types[0].bitfield.class == SReg && !dot_insn ())
11578
140
    {
11579
140
      if (flag_code != CODE_64BIT
11580
140
    ? i.tm.base_opcode == POP_SEG_SHORT
11581
0
      && i.op[0].regs->reg_num == 1
11582
140
    : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
11583
0
      && i.op[0].regs->reg_num < 4)
11584
0
  {
11585
0
    as_bad (_("you can't `%s %s%s'"),
11586
0
      insn_name (&i.tm), register_prefix, i.op[0].regs->reg_name);
11587
0
    return 0;
11588
0
  }
11589
140
      if (i.op[0].regs->reg_num > 3
11590
0
    && i.tm.opcode_space == SPACE_BASE )
11591
0
  {
11592
0
    i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
11593
0
    i.tm.opcode_space = SPACE_0F;
11594
0
  }
11595
140
      i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
11596
140
    }
11597
2.94k
  else if (i.tm.opcode_space == SPACE_BASE
11598
2.92k
     && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
11599
4
    {
11600
4
      default_seg = reg_ds;
11601
4
    }
11602
2.94k
  else if (i.tm.opcode_modifier.isstring)
11603
16
    {
11604
      /* For the string instructions that allow a segment override
11605
   on one of their operands, the default segment is ds.  */
11606
16
      default_seg = reg_ds;
11607
16
    }
11608
2.92k
  else if (i.short_form)
11609
143
    {
11610
      /* The register operand is in the 1st or 2nd non-immediate operand.  */
11611
143
      const reg_entry *r = i.op[i.imm_operands].regs;
11612
11613
143
      if (!dot_insn ()
11614
143
    && r->reg_type.bitfield.instance == Accum
11615
17
    && i.op[i.imm_operands + 1].regs)
11616
0
  r = i.op[i.imm_operands + 1].regs;
11617
      /* Register goes in low 3 bits of opcode.  */
11618
143
      i.tm.base_opcode |= r->reg_num;
11619
143
      set_rex_vrex (r, REX_B, false);
11620
11621
143
      if (dot_insn () && i.reg_operands == 2)
11622
0
  {
11623
0
    gas_assert (is_any_vex_encoding (&i.tm)
11624
0
          || pp.encoding != encoding_default);
11625
0
    i.vex.register_specifier = i.op[i.operands - 1].regs;
11626
0
  }
11627
143
    }
11628
2.78k
  else if (i.reg_operands == 1
11629
21
     && !i.flags[i.operands - 1]
11630
21
     && i.tm_types[i.operands - 1].bitfield.instance
11631
21
        == InstanceNone)
11632
21
    {
11633
21
      gas_assert (is_any_vex_encoding (&i.tm)
11634
21
      || pp.encoding != encoding_default);
11635
21
      i.vex.register_specifier = i.op[i.operands - 1].regs;
11636
21
    }
11637
11638
5.22k
  if ((i.seg[0] || i.prefix[SEG_PREFIX])
11639
5
      && i.tm.mnem_off == MN_lea)
11640
0
    {
11641
0
      if (!quiet_warnings)
11642
0
  as_warn (_("segment override on `%s' is ineffectual"), insn_name (&i.tm));
11643
0
      if (optimize && !pp.no_optimize)
11644
0
  {
11645
0
    i.seg[0] = NULL;
11646
0
    i.prefix[SEG_PREFIX] = 0;
11647
0
  }
11648
0
    }
11649
11650
  /* If a segment was explicitly specified, and the specified segment
11651
     is neither the default nor the one already recorded from a prefix,
11652
     use an opcode prefix to select it.  If we never figured out what
11653
     the default segment is, then default_seg will be zero at this
11654
     point, and the specified segment prefix will always be used.  */
11655
5.22k
  if (i.seg[0]
11656
5
      && i.seg[0] != default_seg
11657
5
      && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
11658
5
    {
11659
5
      if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
11660
0
  return 0;
11661
5
    }
11662
5.22k
  return 1;
11663
5.22k
}
11664
11665
static const reg_entry *
11666
build_modrm_byte (void)
11667
2.13k
{
11668
2.13k
  const reg_entry *default_seg = NULL;
11669
2.13k
  unsigned int source = i.imm_operands - i.tm.opcode_modifier.immext
11670
      /* Compensate for kludge in md_assemble().  */
11671
2.13k
      + i.tm_types[0].bitfield.imm1;
11672
2.13k
  unsigned int dest = i.operands - 1 - i.tm.opcode_modifier.immext;
11673
2.13k
  unsigned int v, op, reg_slot;
11674
11675
  /* Accumulator (in particular %st), shift count (%cl), and alike need
11676
     to be skipped just like immediate operands do.  */
11677
2.13k
  if (i.tm_types[source].bitfield.instance)
11678
0
    ++source;
11679
2.13k
  while (i.tm_types[dest].bitfield.instance)
11680
0
    --dest;
11681
11682
2.41k
  for (op = source; op < i.operands; ++op)
11683
2.40k
    if (i.tm_types[op].bitfield.baseindex)
11684
2.13k
      break;
11685
11686
2.13k
  if (i.reg_operands + i.mem_operands + (i.tm.extension_opcode != None)
11687
2.13k
      + (i.tm.opcode_modifier.operandconstraint == SCC) == 4)
11688
1
    {
11689
1
      expressionS *exp;
11690
11691
      /* There are 2 kinds of instructions:
11692
   1. 5 operands: 4 register operands or 3 register operands
11693
   plus 1 memory operand plus one Imm4 operand, VexXDS, and
11694
   VexW0 or VexW1.  The destination must be either XMM, YMM or
11695
   ZMM register.
11696
   2. 4 operands: 4 register operands or 3 register operands
11697
   plus 1 memory operand, with VexXDS.
11698
   3. Other equivalent combinations when coming from s_insn().  */
11699
1
      if (!dot_insn ())
11700
0
  {
11701
0
    gas_assert (i.tm.opcode_modifier.vexvvvv
11702
0
          && i.tm.opcode_modifier.vexw);
11703
0
    gas_assert (i.tm_types[dest].bitfield.class == RegSIMD);
11704
0
  }
11705
11706
      /* Of the first two non-immediate operands the one with the template
11707
   not allowing for a memory one is encoded in the immediate operand.  */
11708
1
      if (source == op)
11709
1
  reg_slot = source + 1;
11710
0
      else
11711
0
  reg_slot = source++;
11712
11713
1
      if (!dot_insn ())
11714
0
  {
11715
0
    gas_assert (i.tm_types[reg_slot].bitfield.class == RegSIMD);
11716
0
    gas_assert (!(i.op[reg_slot].regs->reg_flags & RegVRex));
11717
0
  }
11718
1
      else
11719
1
  gas_assert (i.tm_types[reg_slot].bitfield.class != ClassNone);
11720
11721
1
      if (i.imm_operands == 0)
11722
0
  {
11723
    /* When there is no immediate operand, generate an 8bit
11724
       immediate operand to encode the first operand.  */
11725
0
    exp = &im_expressions[i.imm_operands++];
11726
0
    i.op[i.operands].imms = exp;
11727
0
    i.types[i.operands].bitfield.imm8 = 1;
11728
0
    i.operands++;
11729
11730
0
    exp->X_op = O_constant;
11731
0
  }
11732
1
      else
11733
1
  {
11734
1
    gas_assert (i.imm_operands == 1);
11735
1
    gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
11736
1
    gas_assert (!i.tm.opcode_modifier.immext);
11737
11738
    /* Turn on Imm8 again so that output_imm will generate it.  */
11739
1
    i.types[0].bitfield.imm8 = 1;
11740
11741
1
    exp = i.op[0].imms;
11742
1
  }
11743
1
      exp->X_add_number |= register_number (i.op[reg_slot].regs)
11744
1
         << (3 + !(i.tm.opcode_modifier.evex
11745
1
             || pp.encoding == encoding_evex));
11746
1
    }
11747
11748
2.13k
  switch (i.tm.opcode_modifier.vexvvvv)
11749
2.13k
    {
11750
    /* VEX.vvvv encodes the last source register operand.  */
11751
0
    case VexVVVV_SRC2:
11752
0
      v = source++;
11753
0
      break;
11754
    /* VEX.vvvv encodes the first source register operand.  */
11755
13
    case VexVVVV_SRC1:
11756
13
      v =  dest - 1;
11757
13
      break;
11758
    /* VEX.vvvv encodes the destination register operand.  */
11759
24
    case VexVVVV_DST:
11760
24
      v = dest--;
11761
24
      break;
11762
2.09k
    default:
11763
2.09k
      v = ~0;
11764
2.09k
      break;
11765
2.13k
     }
11766
11767
2.13k
  if (dest == source)
11768
1.76k
    dest = ~0;
11769
11770
2.13k
  gas_assert (source < dest);
11771
11772
2.13k
  if (v < MAX_OPERANDS)
11773
37
    {
11774
37
      gas_assert (i.tm.opcode_modifier.vexvvvv);
11775
37
      i.vex.register_specifier = i.op[v].regs;
11776
37
    }
11777
11778
2.13k
  if (op < i.operands)
11779
2.13k
    {
11780
2.13k
      if (i.mem_operands)
11781
2.03k
  {
11782
2.03k
    unsigned int fake_zero_displacement = 0;
11783
11784
2.03k
    gas_assert (i.flags[op] & Operand_Mem);
11785
11786
2.03k
    if (i.tm.opcode_modifier.sib)
11787
0
      {
11788
        /* The index register of VSIB shouldn't be RegIZ.  */
11789
0
        if (i.tm.opcode_modifier.sib != SIBMEM
11790
0
      && i.index_reg->reg_num == RegIZ)
11791
0
    abort ();
11792
11793
0
        i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
11794
0
        if (!i.base_reg)
11795
0
    {
11796
0
      i.sib.base = NO_BASE_REGISTER;
11797
0
      i.sib.scale = i.log2_scale_factor;
11798
0
      i.types[op] = operand_type_and_not (i.types[op], anydisp);
11799
0
      i.types[op].bitfield.disp32 = 1;
11800
0
    }
11801
11802
        /* Since the mandatory SIB always has index register, so
11803
     the code logic remains unchanged. The non-mandatory SIB
11804
     without index register is allowed and will be handled
11805
     later.  */
11806
0
        if (i.index_reg)
11807
0
    {
11808
0
      if (i.index_reg->reg_num == RegIZ)
11809
0
        i.sib.index = NO_INDEX_REGISTER;
11810
0
      else
11811
0
        i.sib.index = i.index_reg->reg_num;
11812
0
      set_rex_vrex (i.index_reg, REX_X, false);
11813
0
    }
11814
0
      }
11815
11816
2.03k
    default_seg = reg_ds;
11817
11818
2.03k
    if (i.base_reg == 0)
11819
2.03k
      {
11820
2.03k
        i.rm.mode = 0;
11821
2.03k
        if (!i.disp_operands)
11822
3
    fake_zero_displacement = 1;
11823
2.03k
        if (i.index_reg == 0)
11824
2.03k
    {
11825
      /* Both check for VSIB and mandatory non-vector SIB. */
11826
2.03k
      gas_assert (!i.tm.opcode_modifier.sib
11827
2.03k
            || i.tm.opcode_modifier.sib == SIBMEM);
11828
      /* Operand is just <disp>  */
11829
2.03k
      i.types[op] = operand_type_and_not (i.types[op], anydisp);
11830
2.03k
      if (flag_code == CODE_64BIT)
11831
1.89k
        {
11832
          /* 64bit mode overwrites the 32bit absolute
11833
       addressing by RIP relative addressing and
11834
       absolute addressing is encoded by one of the
11835
       redundant SIB forms.  */
11836
1.89k
          i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
11837
1.89k
          i.sib.base = NO_BASE_REGISTER;
11838
1.89k
          i.sib.index = NO_INDEX_REGISTER;
11839
1.89k
          i.types[op].bitfield.disp32 = 1;
11840
1.89k
        }
11841
135
      else if ((flag_code == CODE_16BIT)
11842
135
         ^ (i.prefix[ADDR_PREFIX] != 0))
11843
135
        {
11844
135
          i.rm.regmem = NO_BASE_REGISTER_16;
11845
135
          i.types[op].bitfield.disp16 = 1;
11846
135
        }
11847
0
      else
11848
0
        {
11849
0
          i.rm.regmem = NO_BASE_REGISTER;
11850
0
          i.types[op].bitfield.disp32 = 1;
11851
0
        }
11852
2.03k
    }
11853
0
        else if (!i.tm.opcode_modifier.sib)
11854
0
    {
11855
      /* !i.base_reg && i.index_reg  */
11856
0
      if (i.index_reg->reg_num == RegIZ)
11857
0
        i.sib.index = NO_INDEX_REGISTER;
11858
0
      else
11859
0
        i.sib.index = i.index_reg->reg_num;
11860
0
      i.sib.base = NO_BASE_REGISTER;
11861
0
      i.sib.scale = i.log2_scale_factor;
11862
0
      i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
11863
0
      i.types[op] = operand_type_and_not (i.types[op], anydisp);
11864
0
      i.types[op].bitfield.disp32 = 1;
11865
0
      set_rex_rex2 (i.index_reg, REX_X);
11866
0
    }
11867
2.03k
      }
11868
    /* RIP addressing for 64bit mode.  */
11869
3
    else if (i.base_reg->reg_num == RegIP)
11870
0
      {
11871
0
        gas_assert (!i.tm.opcode_modifier.sib);
11872
0
        i.rm.regmem = NO_BASE_REGISTER;
11873
0
        i.types[op].bitfield.disp8 = 0;
11874
0
        i.types[op].bitfield.disp16 = 0;
11875
0
        i.types[op].bitfield.disp32 = 1;
11876
0
        i.types[op].bitfield.disp64 = 0;
11877
0
        i.flags[op] |= Operand_PCrel;
11878
0
        if (! i.disp_operands)
11879
0
    fake_zero_displacement = 1;
11880
0
      }
11881
3
    else if (i.base_reg->reg_type.bitfield.word)
11882
0
      {
11883
0
        gas_assert (!i.tm.opcode_modifier.sib);
11884
0
        switch (i.base_reg->reg_num)
11885
0
    {
11886
0
    case 3: /* (%bx)  */
11887
0
      if (i.index_reg == 0)
11888
0
        i.rm.regmem = 7;
11889
0
      else /* (%bx,%si) -> 0, or (%bx,%di) -> 1  */
11890
0
        i.rm.regmem = i.index_reg->reg_num - 6;
11891
0
      break;
11892
0
    case 5: /* (%bp)  */
11893
0
      default_seg = reg_ss;
11894
0
      if (i.index_reg == 0)
11895
0
        {
11896
0
          i.rm.regmem = 6;
11897
0
          if (operand_type_check (i.types[op], disp) == 0)
11898
0
      {
11899
        /* fake (%bp) into 0(%bp)  */
11900
0
        if (pp.disp_encoding == disp_encoding_16bit)
11901
0
          i.types[op].bitfield.disp16 = 1;
11902
0
        else
11903
0
          i.types[op].bitfield.disp8 = 1;
11904
0
        fake_zero_displacement = 1;
11905
0
      }
11906
0
        }
11907
0
      else /* (%bp,%si) -> 2, or (%bp,%di) -> 3  */
11908
0
        i.rm.regmem = i.index_reg->reg_num - 6 + 2;
11909
0
      break;
11910
0
    default: /* (%si) -> 4 or (%di) -> 5  */
11911
0
      i.rm.regmem = i.base_reg->reg_num - 6 + 4;
11912
0
    }
11913
0
        if (!fake_zero_displacement
11914
0
      && !i.disp_operands
11915
0
      && pp.disp_encoding)
11916
0
    {
11917
0
      fake_zero_displacement = 1;
11918
0
      if (pp.disp_encoding == disp_encoding_8bit)
11919
0
        i.types[op].bitfield.disp8 = 1;
11920
0
      else
11921
0
        i.types[op].bitfield.disp16 = 1;
11922
0
    }
11923
0
        i.rm.mode = mode_from_disp_size (i.types[op]);
11924
0
      }
11925
3
    else /* i.base_reg and 32/64 bit mode  */
11926
3
      {
11927
3
        if (operand_type_check (i.types[op], disp))
11928
1
    {
11929
1
      i.types[op].bitfield.disp16 = 0;
11930
1
      i.types[op].bitfield.disp64 = 0;
11931
1
      i.types[op].bitfield.disp32 = 1;
11932
1
    }
11933
11934
3
        if (!i.tm.opcode_modifier.sib)
11935
3
    i.rm.regmem = i.base_reg->reg_num;
11936
3
        set_rex_rex2 (i.base_reg, REX_B);
11937
3
        i.sib.base = i.base_reg->reg_num;
11938
        /* x86-64 ignores REX prefix bit here to avoid decoder
11939
     complications.  */
11940
3
        if (!(i.base_reg->reg_flags & RegRex)
11941
3
      && (i.base_reg->reg_num == EBP_REG_NUM
11942
2
       || i.base_reg->reg_num == ESP_REG_NUM))
11943
2
      default_seg = reg_ss;
11944
3
        if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
11945
0
    {
11946
0
      fake_zero_displacement = 1;
11947
0
      if (pp.disp_encoding == disp_encoding_32bit)
11948
0
        i.types[op].bitfield.disp32 = 1;
11949
0
      else
11950
0
        i.types[op].bitfield.disp8 = 1;
11951
0
    }
11952
3
        i.sib.scale = i.log2_scale_factor;
11953
3
        if (i.index_reg == 0)
11954
2
    {
11955
      /* Only check for VSIB. */
11956
2
      gas_assert (i.tm.opcode_modifier.sib != VECSIB128
11957
2
            && i.tm.opcode_modifier.sib != VECSIB256
11958
2
            && i.tm.opcode_modifier.sib != VECSIB512);
11959
11960
      /* <disp>(%esp) becomes two byte modrm with no index
11961
         register.  We've already stored the code for esp
11962
         in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
11963
         Any base register besides %esp will not use the
11964
         extra modrm byte.  */
11965
2
      i.sib.index = NO_INDEX_REGISTER;
11966
2
    }
11967
1
        else if (!i.tm.opcode_modifier.sib)
11968
1
    {
11969
1
      if (i.index_reg->reg_num == RegIZ)
11970
0
        i.sib.index = NO_INDEX_REGISTER;
11971
1
      else
11972
1
        i.sib.index = i.index_reg->reg_num;
11973
1
      i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
11974
1
      set_rex_rex2 (i.index_reg, REX_X);
11975
1
    }
11976
11977
3
        if (i.disp_operands
11978
1
      && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
11979
1
          || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
11980
0
    i.rm.mode = 0;
11981
3
        else
11982
3
    {
11983
3
      if (!fake_zero_displacement
11984
3
          && !i.disp_operands
11985
2
          && pp.disp_encoding)
11986
0
        {
11987
0
          fake_zero_displacement = 1;
11988
0
          if (pp.disp_encoding == disp_encoding_8bit)
11989
0
      i.types[op].bitfield.disp8 = 1;
11990
0
          else
11991
0
      i.types[op].bitfield.disp32 = 1;
11992
0
        }
11993
3
      i.rm.mode = mode_from_disp_size (i.types[op]);
11994
3
    }
11995
3
      }
11996
11997
2.03k
    if (fake_zero_displacement)
11998
3
      {
11999
        /* Fakes a zero displacement assuming that i.types[op]
12000
     holds the correct displacement size.  */
12001
3
        expressionS *exp;
12002
12003
3
        gas_assert (i.op[op].disps == 0);
12004
3
        exp = &disp_expressions[i.disp_operands++];
12005
3
        i.op[op].disps = exp;
12006
3
        exp->X_op = O_constant;
12007
3
        exp->X_add_number = 0;
12008
3
        exp->X_add_symbol = NULL;
12009
3
        exp->X_op_symbol = NULL;
12010
3
      }
12011
2.03k
  }
12012
95
    else
12013
95
  {
12014
95
      i.rm.mode = 3;
12015
95
      i.rm.regmem = i.op[op].regs->reg_num;
12016
95
      set_rex_vrex (i.op[op].regs, REX_B, false);
12017
95
  }
12018
12019
2.13k
      if (op == dest)
12020
270
  dest = ~0;
12021
2.13k
      if (op == source)
12022
1.86k
  source = ~0;
12023
2.13k
    }
12024
3
  else
12025
3
    {
12026
3
      i.rm.mode = 3;
12027
3
      if (!i.tm.opcode_modifier.regmem)
12028
3
  {
12029
3
    gas_assert (source < MAX_OPERANDS);
12030
3
    i.rm.regmem = i.op[source].regs->reg_num;
12031
3
    set_rex_vrex (i.op[source].regs, REX_B,
12032
3
      dest >= MAX_OPERANDS && i.tm.opcode_modifier.sse2avx);
12033
3
    source = ~0;
12034
3
  }
12035
0
      else
12036
0
  {
12037
0
    gas_assert (dest < MAX_OPERANDS);
12038
0
    i.rm.regmem = i.op[dest].regs->reg_num;
12039
0
    set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
12040
0
    dest = ~0;
12041
0
  }
12042
3
    }
12043
12044
  /* Fill in i.rm.reg field with extension opcode (if any) or the
12045
     appropriate register.  */
12046
2.13k
  if (i.tm.extension_opcode != None)
12047
1.76k
    i.rm.reg = i.tm.extension_opcode;
12048
374
  else if (!i.tm.opcode_modifier.regmem && dest < MAX_OPERANDS)
12049
104
    {
12050
104
      i.rm.reg = i.op[dest].regs->reg_num;
12051
104
      set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
12052
104
    }
12053
270
  else
12054
270
    {
12055
270
      gas_assert (source < MAX_OPERANDS);
12056
270
      i.rm.reg = i.op[source].regs->reg_num;
12057
270
      set_rex_vrex (i.op[source].regs, REX_R, false);
12058
270
    }
12059
12060
2.13k
  if (flag_code != CODE_64BIT && (i.rex & REX_R))
12061
0
    {
12062
0
      gas_assert (i.types[!i.tm.opcode_modifier.regmem].bitfield.class == RegCR);
12063
0
      gas_assert (i.op[!i.tm.opcode_modifier.regmem].regs->reg_num == 0);
12064
0
      i.rex &= ~REX_R;
12065
0
      add_prefix (LOCK_PREFIX_OPCODE);
12066
0
    }
12067
12068
2.13k
  return default_seg;
12069
2.13k
}
12070
12071
static INLINE void
12072
frag_opcode_byte (unsigned char byte)
12073
5.18k
{
12074
5.18k
  if (now_seg != absolute_section)
12075
4.75k
    FRAG_APPEND_1_CHAR (byte);
12076
425
  else
12077
425
    ++abs_section_offset;
12078
5.18k
}
12079
12080
static unsigned int
12081
flip_code16 (unsigned int code16)
12082
70
{
12083
70
  gas_assert (i.tm.operands == 1);
12084
12085
70
  return !(i.prefix[REX_PREFIX] & REX_W)
12086
70
   && (code16 ? i.tm_types[0].bitfield.disp32
12087
70
        : i.tm_types[0].bitfield.disp16)
12088
70
   ? CODE16 : 0;
12089
70
}
12090
12091
static void
12092
output_branch (void)
12093
83
{
12094
83
  char *p;
12095
83
  int size;
12096
83
  int code16;
12097
83
  int prefix;
12098
83
  relax_substateT subtype;
12099
83
  symbolS *sym;
12100
83
  offsetT off;
12101
12102
83
  if (now_seg == absolute_section)
12103
6
    {
12104
6
      as_bad (_("relaxable branches not supported in absolute section"));
12105
6
      return;
12106
6
    }
12107
12108
77
  code16 = flag_code == CODE_16BIT ? CODE16 : 0;
12109
77
  size = pp.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
12110
12111
77
  prefix = 0;
12112
77
  if (i.prefix[DATA_PREFIX] != 0)
12113
50
    {
12114
50
      prefix = 1;
12115
50
      i.prefixes -= 1;
12116
50
      code16 ^= flip_code16(code16);
12117
50
    }
12118
  /* Pentium4 branch hints.  */
12119
77
  if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
12120
77
      || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
12121
0
    {
12122
0
      prefix++;
12123
0
      i.prefixes--;
12124
0
    }
12125
77
  if (i.prefix[REX_PREFIX] != 0)
12126
0
    {
12127
0
      prefix++;
12128
0
      i.prefixes--;
12129
0
    }
12130
12131
  /* BND prefixed jump.  */
12132
77
  if (i.prefix[BND_PREFIX] != 0)
12133
0
    {
12134
0
      prefix++;
12135
0
      i.prefixes--;
12136
0
    }
12137
12138
77
  if (i.prefixes != 0)
12139
0
    as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
12140
12141
  /* It's always a symbol;  End frag & setup for relax.
12142
     Make sure there is enough room in this frag for the largest
12143
     instruction we may generate in md_convert_frag.  This is 2
12144
     bytes for the opcode and room for the prefix and largest
12145
     displacement.  */
12146
77
  frag_grow (prefix + 2 + 4);
12147
  /* Prefix and 1 opcode byte go in fr_fix.  */
12148
77
  p = frag_more (prefix + 1);
12149
77
  if (i.prefix[DATA_PREFIX] != 0)
12150
50
    *p++ = DATA_PREFIX_OPCODE;
12151
77
  if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
12152
77
      || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
12153
0
    *p++ = i.prefix[SEG_PREFIX];
12154
77
  if (i.prefix[BND_PREFIX] != 0)
12155
0
    *p++ = BND_PREFIX_OPCODE;
12156
77
  if (i.prefix[REX_PREFIX] != 0)
12157
0
    *p++ = i.prefix[REX_PREFIX];
12158
77
  *p = i.tm.base_opcode;
12159
12160
77
  if ((unsigned char) *p == JUMP_PC_RELATIVE)
12161
70
    subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
12162
7
  else if (cpu_arch_flags.bitfield.cpui386)
12163
7
    subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
12164
0
  else
12165
0
    subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
12166
77
  subtype |= code16;
12167
12168
77
  sym = i.op[0].disps->X_add_symbol;
12169
77
  off = i.op[0].disps->X_add_number;
12170
12171
77
  if (i.op[0].disps->X_op != O_constant
12172
77
      && i.op[0].disps->X_op != O_symbol)
12173
3
    {
12174
      /* Handle complex expressions.  */
12175
3
      sym = make_expr_symbol (i.op[0].disps);
12176
3
      off = 0;
12177
3
    }
12178
12179
  /* 1 possible extra opcode + 4 byte displacement go in var part.
12180
     Pass reloc in fr_var.  */
12181
77
  frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
12182
77
}
12183
12184
/* PLT32 relocation is ELF only.  */
12185
#ifdef OBJ_ELF
12186
/* Return TRUE iff PLT32 relocation should be used for branching to
12187
   symbol S.  */
12188
12189
static bool
12190
need_plt32_p (symbolS *s)
12191
31
{
12192
#ifdef TE_SOLARIS
12193
  /* Don't emit PLT32 relocation on Solaris: neither native linker nor
12194
     krtld support it.  */
12195
  return false;
12196
#endif
12197
12198
  /* Since there is no need to prepare for PLT branch on x86-64, we
12199
     can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
12200
     be used as a marker for 32-bit PC-relative branches.  */
12201
31
  if (!object_64bit)
12202
0
    return false;
12203
12204
31
  if (s == NULL)
12205
0
    return false;
12206
12207
  /* Weak or undefined symbol need PLT32 relocation.  */
12208
31
  if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
12209
19
    return true;
12210
12211
  /* Non-global symbol doesn't need PLT32 relocation.  */
12212
12
  if (! S_IS_EXTERNAL (s))
12213
12
    return false;
12214
12215
  /* Other global symbols need PLT32 relocation.  NB: Symbol with
12216
     non-default visibilities are treated as normal global symbol
12217
     so that PLT32 relocation can be used as a marker for 32-bit
12218
     PC-relative branches.  It is useful for linker relaxation.  */
12219
0
  return true;
12220
12
}
12221
#endif
12222
12223
static void
12224
output_jump (void)
12225
55
{
12226
55
  char *p;
12227
55
  int size;
12228
55
  fixS *fixP;
12229
55
  bfd_reloc_code_real_type jump_reloc = i.reloc[0];
12230
12231
55
  if (i.tm.opcode_modifier.jump == JUMP_BYTE)
12232
0
    {
12233
      /* This is a loop or jecxz type instruction.  */
12234
0
      size = 1;
12235
0
      if (i.prefix[ADDR_PREFIX] != 0)
12236
0
  {
12237
0
    frag_opcode_byte (ADDR_PREFIX_OPCODE);
12238
0
    i.prefixes -= 1;
12239
0
  }
12240
      /* Pentium4 branch hints.  */
12241
0
      if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
12242
0
    || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
12243
0
  {
12244
0
    frag_opcode_byte (i.prefix[SEG_PREFIX]);
12245
0
    i.prefixes--;
12246
0
  }
12247
0
    }
12248
55
  else
12249
55
    {
12250
55
      int code16;
12251
12252
55
      code16 = 0;
12253
55
      if (flag_code == CODE_16BIT)
12254
1
  code16 = CODE16;
12255
12256
55
      if (i.prefix[DATA_PREFIX] != 0)
12257
20
  {
12258
20
    frag_opcode_byte (DATA_PREFIX_OPCODE);
12259
20
    i.prefixes -= 1;
12260
20
    code16 ^= flip_code16(code16);
12261
20
  }
12262
12263
55
      size = 4;
12264
55
      if (code16)
12265
21
  size = 2;
12266
55
    }
12267
12268
  /* BND prefixed jump.  */
12269
55
  if (i.prefix[BND_PREFIX] != 0)
12270
0
    {
12271
0
      frag_opcode_byte (i.prefix[BND_PREFIX]);
12272
0
      i.prefixes -= 1;
12273
0
    }
12274
12275
55
  if (i.prefix[REX_PREFIX] != 0)
12276
0
    {
12277
0
      frag_opcode_byte (i.prefix[REX_PREFIX]);
12278
0
      i.prefixes -= 1;
12279
0
    }
12280
12281
55
  if (i.prefixes != 0)
12282
0
    as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
12283
12284
55
  if (now_seg == absolute_section)
12285
0
    {
12286
0
      abs_section_offset += i.opcode_length + size;
12287
0
      return;
12288
0
    }
12289
12290
55
  p = frag_more (i.opcode_length + size);
12291
55
  switch (i.opcode_length)
12292
55
    {
12293
0
    case 2:
12294
0
      *p++ = i.tm.base_opcode >> 8;
12295
      /* Fall through.  */
12296
55
    case 1:
12297
55
      *p++ = i.tm.base_opcode;
12298
55
      break;
12299
0
    default:
12300
0
      abort ();
12301
55
    }
12302
12303
55
#ifdef OBJ_ELF
12304
55
  if (flag_code == CODE_64BIT && size == 4
12305
34
      && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
12306
31
      && need_plt32_p (i.op[0].disps->X_add_symbol))
12307
19
    jump_reloc = BFD_RELOC_32_PLT_PCREL;
12308
55
#endif
12309
12310
55
  jump_reloc = reloc (size, 1, 1, jump_reloc);
12311
12312
55
  fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
12313
55
          i.op[0].disps, 1, jump_reloc);
12314
12315
  /* All jumps handled here are signed, but don't unconditionally use a
12316
     signed limit check for 32 and 16 bit jumps as we want to allow wrap
12317
     around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
12318
     respectively.  */
12319
55
  switch (size)
12320
55
    {
12321
0
    case 1:
12322
0
      fixP->fx_signed = 1;
12323
0
      break;
12324
12325
21
    case 2:
12326
21
      if (i.tm.mnem_off == MN_xbegin)
12327
0
  fixP->fx_signed = 1;
12328
21
      break;
12329
12330
34
    case 4:
12331
34
      if (flag_code == CODE_64BIT)
12332
34
  fixP->fx_signed = 1;
12333
34
      break;
12334
55
    }
12335
55
}
12336
12337
static void
12338
output_interseg_jump (void)
12339
31
{
12340
31
  char *p;
12341
31
  int size;
12342
31
  int prefix;
12343
31
  int code16;
12344
12345
31
  code16 = 0;
12346
31
  if (flag_code == CODE_16BIT)
12347
0
    code16 = CODE16;
12348
12349
31
  prefix = 0;
12350
31
  if (i.prefix[DATA_PREFIX] != 0)
12351
0
    {
12352
0
      prefix = 1;
12353
0
      i.prefixes -= 1;
12354
0
      code16 ^= CODE16;
12355
0
    }
12356
12357
31
  gas_assert (!i.prefix[REX_PREFIX]);
12358
12359
31
  size = 4;
12360
31
  if (code16)
12361
0
    size = 2;
12362
12363
31
  if (i.prefixes != 0)
12364
0
    as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
12365
12366
31
  if (now_seg == absolute_section)
12367
9
    {
12368
9
      abs_section_offset += prefix + 1 + 2 + size;
12369
9
      return;
12370
9
    }
12371
12372
  /* 1 opcode; 2 segment; offset  */
12373
22
  p = frag_more (prefix + 1 + 2 + size);
12374
12375
22
  if (i.prefix[DATA_PREFIX] != 0)
12376
0
    *p++ = DATA_PREFIX_OPCODE;
12377
12378
22
  if (i.prefix[REX_PREFIX] != 0)
12379
0
    *p++ = i.prefix[REX_PREFIX];
12380
12381
22
  *p++ = i.tm.base_opcode;
12382
22
  if (i.op[1].imms->X_op == O_constant)
12383
0
    {
12384
0
      offsetT n = i.op[1].imms->X_add_number;
12385
12386
0
      if (size == 2
12387
0
    && !fits_in_unsigned_word (n)
12388
0
    && !fits_in_signed_word (n))
12389
0
  {
12390
0
    as_bad (_("16-bit jump out of range"));
12391
0
    return;
12392
0
  }
12393
0
      md_number_to_chars (p, n, size);
12394
0
    }
12395
22
  else
12396
22
    fix_new_exp (frag_now, p - frag_now->fr_literal, size,
12397
22
     i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
12398
12399
22
  p += size;
12400
22
  if (i.op[0].imms->X_op == O_constant)
12401
0
    md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
12402
22
  else
12403
22
    fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
12404
22
     i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
12405
22
}
12406
12407
/* Hook used to reject pseudo-prefixes misplaced at the start of a line.  */
12408
12409
void i386_start_line (void)
12410
771k
{
12411
771k
  struct pseudo_prefixes last_pp;
12412
12413
771k
  memcpy (&last_pp, &pp, sizeof (pp));
12414
771k
  memset (&pp, 0, sizeof (pp));
12415
771k
  if (memcmp (&pp, &last_pp, sizeof (pp)))
12416
0
    as_bad_where (frag_now->fr_file, frag_now->fr_line,
12417
0
      _("pseudo prefix without instruction"));
12418
771k
}
12419
12420
/* Hook used to warn about pseudo-prefixes ahead of a label.  */
12421
12422
bool i386_check_label (void)
12423
3.95k
{
12424
3.95k
  struct pseudo_prefixes last_pp;
12425
12426
3.95k
  memcpy (&last_pp, &pp, sizeof (pp));
12427
3.95k
  memset (&pp, 0, sizeof (pp));
12428
3.95k
  if (memcmp (&pp, &last_pp, sizeof (pp)))
12429
31
    as_warn (_("pseudo prefix ahead of label; ignoring"));
12430
3.95k
  return true;
12431
3.95k
}
12432
12433
/* Hook used to parse pseudo-prefixes off of the start of a line.  */
12434
12435
int
12436
i386_unrecognized_line (int ch)
12437
61.1k
{
12438
61.1k
  char mnemonic[MAX_MNEM_SIZE];
12439
61.1k
  const char *end;
12440
12441
61.1k
  if (ch != '{')
12442
58.6k
    return 0;
12443
12444
2.45k
  --input_line_pointer;
12445
2.45k
  know (*input_line_pointer == ch);
12446
12447
2.45k
  end = parse_insn (input_line_pointer, mnemonic, parse_pseudo_prefix);
12448
2.45k
  if (end == NULL)
12449
874
    {
12450
      /* Diagnostic was already issued.  */
12451
874
      ignore_rest_of_line ();
12452
874
      memset (&pp, 0, sizeof (pp));
12453
874
      return 1;
12454
874
    }
12455
12456
1.57k
  if (end == input_line_pointer)
12457
431
    {
12458
431
      ++input_line_pointer;
12459
431
      return 0;
12460
431
    }
12461
12462
1.14k
  input_line_pointer += end - input_line_pointer;
12463
1.14k
  return 1;
12464
1.57k
}
12465
12466
#ifdef OBJ_ELF
12467
void
12468
x86_cleanup (void)
12469
368
{
12470
368
  char *p;
12471
368
  asection *seg = now_seg;
12472
368
  subsegT subseg = now_subseg;
12473
368
  asection *sec;
12474
368
  unsigned int alignment, align_size_1;
12475
368
  unsigned int isa_1_descsz, feature_2_descsz, descsz;
12476
368
  unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
12477
368
  unsigned int padding;
12478
12479
368
  if (!x86_used_note)
12480
0
    return;
12481
12482
368
  x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
12483
12484
  /* The .note.gnu.property section layout:
12485
12486
     Field  Length    Contents
12487
     ---- ----    ----
12488
     n_namsz  4   4
12489
     n_descsz 4   The note descriptor size
12490
     n_type 4   NT_GNU_PROPERTY_TYPE_0
12491
     n_name 4   "GNU"
12492
     n_desc n_descsz  The program property array
12493
     .... ....    ....
12494
   */
12495
12496
  /* Create the .note.gnu.property section.  */
12497
368
  sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
12498
368
  bfd_set_section_flags (sec,
12499
368
       (SEC_ALLOC
12500
368
        | SEC_LOAD
12501
368
        | SEC_DATA
12502
368
        | SEC_HAS_CONTENTS
12503
368
        | SEC_READONLY));
12504
12505
368
  if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
12506
368
    {
12507
368
      align_size_1 = 7;
12508
368
      alignment = 3;
12509
368
    }
12510
0
  else
12511
0
    {
12512
0
      align_size_1 = 3;
12513
0
      alignment = 2;
12514
0
    }
12515
12516
368
  bfd_set_section_alignment (sec, alignment);
12517
368
  elf_section_type (sec) = SHT_NOTE;
12518
12519
  /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
12520
          + 4-byte data  */
12521
368
  isa_1_descsz_raw = 4 + 4 + 4;
12522
  /* Align GNU_PROPERTY_X86_ISA_1_USED.  */
12523
368
  isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
12524
12525
368
  feature_2_descsz_raw = isa_1_descsz;
12526
  /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
12527
              + 4-byte data  */
12528
368
  feature_2_descsz_raw += 4 + 4 + 4;
12529
  /* Align GNU_PROPERTY_X86_FEATURE_2_USED.  */
12530
368
  feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
12531
368
          & ~align_size_1);
12532
12533
368
  descsz = feature_2_descsz;
12534
  /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz.  */
12535
368
  p = frag_more (4 + 4 + 4 + 4 + descsz);
12536
12537
  /* Write n_namsz.  */
12538
368
  md_number_to_chars (p, (valueT) 4, 4);
12539
12540
  /* Write n_descsz.  */
12541
368
  md_number_to_chars (p + 4, (valueT) descsz, 4);
12542
12543
  /* Write n_type.  */
12544
368
  md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
12545
12546
  /* Write n_name.  */
12547
368
  memcpy (p + 4 * 3, "GNU", 4);
12548
12549
  /* Write 4-byte type.  */
12550
368
  md_number_to_chars (p + 4 * 4,
12551
368
          (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
12552
12553
  /* Write 4-byte data size.  */
12554
368
  md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
12555
12556
  /* Write 4-byte data.  */
12557
368
  md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
12558
12559
  /* Zero out paddings.  */
12560
368
  padding = isa_1_descsz - isa_1_descsz_raw;
12561
368
  if (padding)
12562
368
    memset (p + 4 * 7, 0, padding);
12563
12564
  /* Write 4-byte type.  */
12565
368
  md_number_to_chars (p + isa_1_descsz + 4 * 4,
12566
368
          (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
12567
12568
  /* Write 4-byte data size.  */
12569
368
  md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
12570
12571
  /* Write 4-byte data.  */
12572
368
  md_number_to_chars (p + isa_1_descsz + 4 * 6,
12573
368
          (valueT) x86_feature_2_used, 4);
12574
12575
  /* Zero out paddings.  */
12576
368
  padding = feature_2_descsz - feature_2_descsz_raw;
12577
368
  if (padding)
12578
368
    memset (p + isa_1_descsz + 4 * 7, 0, padding);
12579
12580
  /* We probably can't restore the current segment, for there likely
12581
     isn't one yet...  */
12582
368
  if (seg && subseg)
12583
7
    subseg_set (seg, subseg);
12584
368
}
12585
12586
#include "tc-i386-ginsn.c"
12587
12588
/* Whether SFrame stack trace info is supported.  */
12589
bool
12590
x86_support_sframe_p (void)
12591
132
{
12592
  /* At this time, SFrame stack trace is supported for AMD64 ABI only.  */
12593
132
  return (x86_elf_abi == X86_64_ABI);
12594
132
}
12595
12596
/* The fixed offset from CFA for SFrame to recover the return address.
12597
   (useful only when SFrame RA tracking is not needed).  */
12598
offsetT
12599
x86_sframe_cfa_ra_offset (void)
12600
39
{
12601
39
  gas_assert (x86_elf_abi == X86_64_ABI);
12602
39
  return (offsetT) -8;
12603
39
}
12604
12605
/* The abi/arch identifier for SFrame.  */
12606
unsigned char
12607
x86_sframe_get_abi_arch (void)
12608
112
{
12609
112
  unsigned char sframe_abi_arch = 0;
12610
12611
112
  if (x86_support_sframe_p ())
12612
112
    {
12613
112
      gas_assert (!target_big_endian);
12614
112
      sframe_abi_arch = SFRAME_ABI_AMD64_ENDIAN_LITTLE;
12615
112
    }
12616
12617
112
  return sframe_abi_arch;
12618
112
}
12619
12620
#endif
12621
12622
static unsigned int
12623
encoding_length (const fragS *start_frag, offsetT start_off,
12624
     const char *frag_now_ptr)
12625
6.44k
{
12626
6.44k
  unsigned int len = 0;
12627
12628
6.44k
  if (start_frag != frag_now)
12629
3
    {
12630
3
      const fragS *fr = start_frag;
12631
12632
3
      do {
12633
3
  len += fr->fr_fix;
12634
3
  fr = fr->fr_next;
12635
3
      } while (fr && fr != frag_now);
12636
3
    }
12637
12638
6.44k
  return len - start_off + (frag_now_ptr - frag_now->fr_literal);
12639
6.44k
}
12640
12641
/* Return 1 for test, and, cmp, add, sub, inc and dec which may
12642
   be macro-fused with conditional jumps.
12643
   NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
12644
   or is one of the following format:
12645
12646
    cmp m, imm
12647
    add m, imm
12648
    sub m, imm
12649
   test m, imm
12650
    and m, imm
12651
    inc m
12652
    dec m
12653
12654
   it is unfusible.  */
12655
12656
static int
12657
maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
12658
0
{
12659
  /* No RIP address.  */
12660
0
  if (i.base_reg && i.base_reg->reg_num == RegIP)
12661
0
    return 0;
12662
12663
  /* No opcodes outside of base encoding space.  */
12664
0
  if (i.tm.opcode_space != SPACE_BASE)
12665
0
    return 0;
12666
12667
  /* add, sub without add/sub m, imm.  */
12668
0
  if (i.tm.base_opcode <= 5
12669
0
      || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
12670
0
      || ((i.tm.base_opcode | 3) == 0x83
12671
0
    && (i.tm.extension_opcode == 0x5
12672
0
        || i.tm.extension_opcode == 0x0)))
12673
0
    {
12674
0
      *mf_cmp_p = mf_cmp_alu_cmp;
12675
0
      return !(i.mem_operands && i.imm_operands);
12676
0
    }
12677
12678
  /* and without and m, imm.  */
12679
0
  if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
12680
0
      || ((i.tm.base_opcode | 3) == 0x83
12681
0
    && i.tm.extension_opcode == 0x4))
12682
0
    {
12683
0
      *mf_cmp_p = mf_cmp_test_and;
12684
0
      return !(i.mem_operands && i.imm_operands);
12685
0
    }
12686
12687
  /* test without test m imm.  */
12688
0
  if ((i.tm.base_opcode | 1) == 0x85
12689
0
      || (i.tm.base_opcode | 1) == 0xa9
12690
0
      || ((i.tm.base_opcode | 1) == 0xf7
12691
0
    && i.tm.extension_opcode == 0))
12692
0
    {
12693
0
      *mf_cmp_p = mf_cmp_test_and;
12694
0
      return !(i.mem_operands && i.imm_operands);
12695
0
    }
12696
12697
  /* cmp without cmp m, imm.  */
12698
0
  if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
12699
0
      || ((i.tm.base_opcode | 3) == 0x83
12700
0
    && (i.tm.extension_opcode == 0x7)))
12701
0
    {
12702
0
      *mf_cmp_p = mf_cmp_alu_cmp;
12703
0
      return !(i.mem_operands && i.imm_operands);
12704
0
    }
12705
12706
  /* inc, dec without inc/dec m.   */
12707
0
  if ((is_cpu (&i.tm, CpuNo64)
12708
0
       && (i.tm.base_opcode | 0xf) == 0x4f)
12709
0
      || ((i.tm.base_opcode | 1) == 0xff
12710
0
    && i.tm.extension_opcode <= 0x1))
12711
0
    {
12712
0
      *mf_cmp_p = mf_cmp_incdec;
12713
0
      return !i.mem_operands;
12714
0
    }
12715
12716
0
  return 0;
12717
0
}
12718
12719
/* Return 1 if a FUSED_JCC_PADDING frag should be generated.  */
12720
12721
static int
12722
add_fused_jcc_padding_frag_p (enum mf_cmp_kind *mf_cmp_p,
12723
            const struct last_insn *last_insn)
12724
7.08k
{
12725
  /* NB: Don't work with COND_JUMP86 without i386.  */
12726
7.08k
  if (!align_branch_power
12727
0
      || now_seg == absolute_section
12728
0
      || !cpu_arch_flags.bitfield.cpui386
12729
0
      || !(align_branch & align_branch_fused_bit))
12730
7.08k
    return 0;
12731
12732
0
  if (maybe_fused_with_jcc_p (mf_cmp_p))
12733
0
    {
12734
0
      if (last_insn->kind == last_insn_other)
12735
0
  return 1;
12736
0
      if (flag_debug)
12737
0
  as_warn_where (last_insn->file, last_insn->line,
12738
0
           _("`%s` skips -malign-branch-boundary on `%s`"),
12739
0
           last_insn->name, insn_name (&i.tm));
12740
0
    }
12741
12742
0
  return 0;
12743
0
}
12744
12745
/* Return 1 if a BRANCH_PREFIX frag should be generated.  */
12746
12747
static int
12748
add_branch_prefix_frag_p (const struct last_insn *last_insn)
12749
7.08k
{
12750
  /* NB: Don't work with COND_JUMP86 without i386.  Don't add prefix
12751
     to PadLock instructions since they include prefixes in opcode.  */
12752
7.08k
  if (!align_branch_power
12753
0
      || !align_branch_prefix_size
12754
0
      || now_seg == absolute_section
12755
0
      || is_padlock (&i.tm)
12756
0
      || !cpu_arch_flags.bitfield.cpui386)
12757
7.08k
    return 0;
12758
12759
  /* Don't add prefix if it is a prefix or there is no operand in case
12760
     that segment prefix is special.  */
12761
0
  if (!i.operands || i.tm.opcode_modifier.isprefix)
12762
0
    return 0;
12763
12764
0
  if (last_insn->kind == last_insn_other)
12765
0
    return 1;
12766
12767
0
  if (flag_debug)
12768
0
    as_warn_where (last_insn->file, last_insn->line,
12769
0
       _("`%s` skips -malign-branch-boundary on `%s`"),
12770
0
       last_insn->name, insn_name (&i.tm));
12771
12772
0
  return 0;
12773
0
}
12774
12775
/* Return 1 if a BRANCH_PADDING frag should be generated.  */
12776
12777
static int
12778
add_branch_padding_frag_p (enum align_branch_kind *branch_p,
12779
         enum mf_jcc_kind *mf_jcc_p,
12780
         const struct last_insn *last_insn)
12781
7.25k
{
12782
7.25k
  int add_padding;
12783
12784
  /* NB: Don't work with COND_JUMP86 without i386.  */
12785
7.25k
  if (!align_branch_power
12786
0
      || now_seg == absolute_section
12787
0
      || !cpu_arch_flags.bitfield.cpui386
12788
0
      || i.tm.opcode_space != SPACE_BASE)
12789
7.25k
    return 0;
12790
12791
0
  add_padding = 0;
12792
12793
  /* Check for jcc and direct jmp.  */
12794
0
  if (i.tm.opcode_modifier.jump == JUMP)
12795
0
    {
12796
0
      if (i.tm.base_opcode == JUMP_PC_RELATIVE)
12797
0
  {
12798
0
    *branch_p = align_branch_jmp;
12799
0
    add_padding = align_branch & align_branch_jmp_bit;
12800
0
  }
12801
0
      else
12802
0
  {
12803
    /* Because J<cc> and JN<cc> share same group in macro-fusible table,
12804
       igore the lowest bit.  */
12805
0
    *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
12806
0
    *branch_p = align_branch_jcc;
12807
0
    if ((align_branch & align_branch_jcc_bit))
12808
0
      add_padding = 1;
12809
0
  }
12810
0
    }
12811
0
  else if ((i.tm.base_opcode | 1) == 0xc3)
12812
0
    {
12813
      /* Near ret.  */
12814
0
      *branch_p = align_branch_ret;
12815
0
      if ((align_branch & align_branch_ret_bit))
12816
0
  add_padding = 1;
12817
0
    }
12818
0
  else
12819
0
    {
12820
      /* Check for indirect jmp, direct and indirect calls.  */
12821
0
      if (i.tm.base_opcode == 0xe8)
12822
0
  {
12823
    /* Direct call.  */
12824
0
    *branch_p = align_branch_call;
12825
0
    if ((align_branch & align_branch_call_bit))
12826
0
      add_padding = 1;
12827
0
  }
12828
0
      else if (i.tm.base_opcode == 0xff
12829
0
         && (i.tm.extension_opcode == 2
12830
0
       || i.tm.extension_opcode == 4))
12831
0
  {
12832
    /* Indirect call and jmp.  */
12833
0
    *branch_p = align_branch_indirect;
12834
0
    if ((align_branch & align_branch_indirect_bit))
12835
0
      add_padding = 1;
12836
0
  }
12837
12838
0
      if (add_padding
12839
0
    && i.disp_operands
12840
0
    && tls_get_addr
12841
0
    && (i.op[0].disps->X_op == O_symbol
12842
0
        || (i.op[0].disps->X_op == O_subtract
12843
0
      && i.op[0].disps->X_op_symbol == GOT_symbol)))
12844
0
  {
12845
0
    symbolS *s = i.op[0].disps->X_add_symbol;
12846
    /* No padding to call to global or undefined tls_get_addr.  */
12847
0
    if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
12848
0
        && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
12849
0
      return 0;
12850
0
  }
12851
0
    }
12852
12853
0
  if (add_padding
12854
0
      && last_insn->kind != last_insn_other)
12855
0
    {
12856
0
      if (flag_debug)
12857
0
  as_warn_where (last_insn->file, last_insn->line,
12858
0
           _("`%s` skips -malign-branch-boundary on `%s`"),
12859
0
           last_insn->name, insn_name (&i.tm));
12860
0
      return 0;
12861
0
    }
12862
12863
0
  return add_padding;
12864
0
}
12865
12866
static void
12867
output_insn (const struct last_insn *last_insn)
12868
7.25k
{
12869
7.25k
  fragS *insn_start_frag;
12870
7.25k
  offsetT insn_start_off;
12871
7.25k
  fragS *fragP = NULL;
12872
7.25k
  enum align_branch_kind branch = align_branch_none;
12873
  /* The initializer is arbitrary just to avoid uninitialized error.
12874
     it's actually either assigned in add_branch_padding_frag_p
12875
     or never be used.  */
12876
7.25k
  enum mf_jcc_kind mf_jcc = mf_jcc_jo;
12877
12878
7.25k
#ifdef OBJ_ELF
12879
7.25k
  if (x86_used_note && now_seg != absolute_section)
12880
6.59k
    {
12881
6.59k
      unsigned int feature_2_used = 0;
12882
12883
6.59k
      if ((i.xstate & xstate_tmm) == xstate_tmm
12884
6.59k
    || is_cpu (&i.tm, CpuTILE))
12885
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
12886
12887
6.59k
      if (is_cpu (&i.tm, Cpu8087)
12888
6.59k
    || is_cpu (&i.tm, Cpu287)
12889
6.59k
    || is_cpu (&i.tm, Cpu387)
12890
6.59k
    || is_cpu (&i.tm, Cpu687)
12891
6.59k
    || is_cpu (&i.tm, CpuFISTTP))
12892
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
12893
12894
6.59k
      if ((i.xstate & xstate_mmx)
12895
6.59k
    || i.tm.mnem_off == MN_emms
12896
6.59k
    || i.tm.mnem_off == MN_femms)
12897
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
12898
12899
6.59k
      if (i.index_reg)
12900
1
  {
12901
1
    if (i.index_reg->reg_type.bitfield.zmmword)
12902
0
      i.xstate |= xstate_zmm;
12903
1
    else if (i.index_reg->reg_type.bitfield.ymmword)
12904
0
      i.xstate |= xstate_ymm;
12905
1
    else if (i.index_reg->reg_type.bitfield.xmmword)
12906
0
      i.xstate |= xstate_xmm;
12907
1
  }
12908
12909
      /* vzeroall / vzeroupper */
12910
6.59k
      if (i.tm.base_opcode == 0x77 && is_cpu (&i.tm, CpuAVX))
12911
0
  i.xstate |= xstate_ymm;
12912
12913
6.59k
      if ((i.xstate & xstate_xmm)
12914
    /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
12915
6.52k
    || (i.tm.base_opcode == 0xae
12916
0
        && (is_cpu (&i.tm, CpuSSE)
12917
0
      || is_cpu (&i.tm, CpuAVX)))
12918
6.52k
    || is_cpu (&i.tm, CpuWideKL)
12919
6.52k
    || is_cpu (&i.tm, CpuKL))
12920
68
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
12921
12922
6.59k
      if ((i.xstate & xstate_ymm) == xstate_ymm)
12923
4
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
12924
6.59k
      if ((i.xstate & xstate_zmm) == xstate_zmm)
12925
2
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
12926
6.59k
      if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
12927
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
12928
6.59k
      if (is_cpu (&i.tm, CpuFXSR))
12929
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
12930
6.59k
      if (is_cpu (&i.tm, CpuXsave))
12931
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
12932
6.59k
      if (is_cpu (&i.tm, CpuXsaveopt))
12933
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
12934
6.59k
      if (is_cpu (&i.tm, CpuXSAVEC))
12935
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
12936
12937
6.59k
      x86_feature_2_used |= feature_2_used;
12938
12939
6.59k
      if (object_64bit
12940
0
    || (feature_2_used
12941
0
        & (GNU_PROPERTY_X86_FEATURE_2_XMM
12942
0
     | GNU_PROPERTY_X86_FEATURE_2_FXSR)) != 0
12943
0
    || is_cpu (&i.tm, CpuCMOV)
12944
0
    || is_cpu (&i.tm, CpuSYSCALL)
12945
0
    || i.tm.mnem_off == MN_cmpxchg8b)
12946
6.59k
  x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
12947
6.59k
      if (is_cpu (&i.tm, CpuSSE3)
12948
6.59k
    || is_cpu (&i.tm, CpuSSSE3)
12949
6.59k
    || is_cpu (&i.tm, CpuSSE4_1)
12950
6.59k
    || is_cpu (&i.tm, CpuSSE4_2)
12951
6.59k
    || is_cpu (&i.tm, CpuCX16)
12952
6.59k
    || is_cpu (&i.tm, CpuPOPCNT)
12953
    /* LAHF-SAHF insns in 64-bit mode.  */
12954
6.59k
    || (flag_code == CODE_64BIT
12955
6.23k
        && (i.tm.base_opcode | 1) == 0x9f
12956
0
        && i.tm.opcode_space == SPACE_BASE))
12957
0
  x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
12958
6.59k
      if (is_cpu (&i.tm, CpuAVX)
12959
6.54k
    || is_cpu (&i.tm, CpuAVX2)
12960
    /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
12961
       XOP, FMA4, LPW, TBM, and AMX.  */
12962
6.54k
    || (i.tm.opcode_modifier.vex
12963
572
        && !is_cpu (&i.tm, CpuAVX512F)
12964
572
        && !is_cpu (&i.tm, CpuAVX512BW)
12965
572
        && !is_cpu (&i.tm, CpuAVX512DQ)
12966
572
        && !is_cpu (&i.tm, CpuXOP)
12967
572
        && !is_cpu (&i.tm, CpuFMA4)
12968
572
        && !is_cpu (&i.tm, CpuLWP)
12969
572
        && !is_cpu (&i.tm, CpuTBM)
12970
572
        && !(feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
12971
5.97k
    || is_cpu (&i.tm, CpuLZCNT)
12972
5.97k
    || is_cpu (&i.tm, CpuMovbe)
12973
5.97k
    || is_cpu (&i.tm, CpuXSAVES)
12974
5.97k
    || (feature_2_used
12975
5.97k
        & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
12976
5.97k
     | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
12977
5.97k
     | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
12978
623
  x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
12979
6.59k
      if (is_cpu (&i.tm, CpuAVX512F)
12980
6.59k
    || is_cpu (&i.tm, CpuAVX512BW)
12981
6.59k
    || is_cpu (&i.tm, CpuAVX512DQ)
12982
6.59k
    || is_cpu (&i.tm, CpuAVX512VL)
12983
    /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
12984
       AVX512-4FMAPS, and AVX512-4VNNIW.  */
12985
6.59k
    || (i.tm.opcode_modifier.evex
12986
54
        && !is_cpu (&i.tm, CpuAVX512ER)
12987
54
        && !is_cpu (&i.tm, CpuAVX512PF)
12988
54
        && !is_cpu (&i.tm, CpuAVX512_4FMAPS)
12989
54
        && !is_cpu (&i.tm, CpuAVX512_4VNNIW)))
12990
56
  x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
12991
6.59k
    }
12992
7.25k
#endif
12993
12994
  /* Tie dwarf2 debug info to the address at the start of the insn.
12995
     We can't do this after the insn has been output as the current
12996
     frag may have been closed off.  eg. by frag_var.  */
12997
7.25k
  dwarf2_emit_insn (0);
12998
12999
7.25k
  insn_start_frag = frag_now;
13000
7.25k
  insn_start_off = frag_now_fix ();
13001
13002
7.25k
  if (add_branch_padding_frag_p (&branch, &mf_jcc, last_insn))
13003
0
    {
13004
0
      char *p;
13005
      /* Branch can be 8 bytes.  Leave some room for prefixes.  */
13006
0
      unsigned int max_branch_padding_size = 14;
13007
13008
      /* Align section to boundary.  */
13009
0
      record_alignment (now_seg, align_branch_power);
13010
13011
      /* Make room for padding.  */
13012
0
      frag_grow (max_branch_padding_size);
13013
13014
      /* Start of the padding.  */
13015
0
      p = frag_more (0);
13016
13017
0
      fragP = frag_now;
13018
13019
0
      frag_var (rs_machine_dependent, max_branch_padding_size, 0,
13020
0
    ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
13021
0
    NULL, 0, p);
13022
13023
0
      fragP->tc_frag_data.mf_type = mf_jcc;
13024
0
      fragP->tc_frag_data.branch_type = branch;
13025
0
      fragP->tc_frag_data.max_bytes = max_branch_padding_size;
13026
0
    }
13027
13028
7.25k
  if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
13029
0
      && !pre_386_16bit_warned)
13030
0
    {
13031
0
      as_warn (_("use .code16 to ensure correct addressing mode"));
13032
0
      pre_386_16bit_warned = true;
13033
0
    }
13034
13035
  /* Output jumps.  */
13036
7.25k
  if (i.tm.opcode_modifier.jump == JUMP)
13037
83
    output_branch ();
13038
7.16k
  else if (i.tm.opcode_modifier.jump == JUMP_BYTE
13039
7.16k
     || i.tm.opcode_modifier.jump == JUMP_DWORD)
13040
55
    output_jump ();
13041
7.11k
  else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
13042
31
    output_interseg_jump ();
13043
7.08k
  else
13044
7.08k
    {
13045
      /* Output normal instructions here.  */
13046
7.08k
      char *p;
13047
7.08k
      unsigned char *q;
13048
7.08k
      unsigned int j;
13049
7.08k
      enum mf_cmp_kind mf_cmp;
13050
13051
7.08k
      if (avoid_fence
13052
0
    && (i.tm.base_opcode == 0xaee8
13053
0
        || i.tm.base_opcode == 0xaef0
13054
0
        || i.tm.base_opcode == 0xaef8))
13055
0
  {
13056
    /* Encode lfence, mfence, and sfence as
13057
       f0 83 04 24 00   lock addl $0x0, (%{re}sp).  */
13058
0
    if (flag_code == CODE_16BIT)
13059
0
      as_bad (_("Cannot convert `%s' in 16-bit mode"), insn_name (&i.tm));
13060
0
    else if (omit_lock_prefix)
13061
0
      as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
13062
0
        insn_name (&i.tm));
13063
0
    else if (now_seg != absolute_section)
13064
0
      {
13065
0
        offsetT val = 0x240483f0ULL;
13066
13067
0
        p = frag_more (5);
13068
0
        md_number_to_chars (p, val, 5);
13069
0
      }
13070
0
    else
13071
0
      abs_section_offset += 5;
13072
0
    return;
13073
0
  }
13074
13075
      /* Some processors fail on LOCK prefix. This options makes
13076
   assembler ignore LOCK prefix and serves as a workaround.  */
13077
7.08k
      if (omit_lock_prefix)
13078
0
  {
13079
0
    if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
13080
0
        && i.tm.opcode_modifier.isprefix)
13081
0
      return;
13082
0
    i.prefix[LOCK_PREFIX] = 0;
13083
0
  }
13084
13085
7.08k
      if (branch)
13086
  /* Skip if this is a branch.  */
13087
0
  ;
13088
7.08k
      else if (add_fused_jcc_padding_frag_p (&mf_cmp, last_insn))
13089
0
  {
13090
    /* Make room for padding.  */
13091
0
    frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
13092
0
    p = frag_more (0);
13093
13094
0
    fragP = frag_now;
13095
13096
0
    frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
13097
0
        ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
13098
0
        NULL, 0, p);
13099
13100
0
    fragP->tc_frag_data.mf_type = mf_cmp;
13101
0
    fragP->tc_frag_data.branch_type = align_branch_fused;
13102
0
    fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
13103
0
  }
13104
7.08k
      else if (add_branch_prefix_frag_p (last_insn))
13105
0
  {
13106
0
    unsigned int max_prefix_size = align_branch_prefix_size;
13107
13108
    /* Make room for padding.  */
13109
0
    frag_grow (max_prefix_size);
13110
0
    p = frag_more (0);
13111
13112
0
    fragP = frag_now;
13113
13114
0
    frag_var (rs_machine_dependent, max_prefix_size, 0,
13115
0
        ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
13116
0
        NULL, 0, p);
13117
13118
0
    fragP->tc_frag_data.max_bytes = max_prefix_size;
13119
0
  }
13120
13121
      /* Since the VEX/EVEX prefix contains the implicit prefix, we
13122
   don't need the explicit prefix.  */
13123
7.08k
      if (!is_any_vex_encoding (&i.tm))
13124
6.40k
  {
13125
6.40k
    switch (i.tm.opcode_modifier.opcodeprefix)
13126
6.40k
      {
13127
14
      case PREFIX_0X66:
13128
14
        add_prefix (0x66);
13129
14
        break;
13130
0
      case PREFIX_0XF2:
13131
0
        add_prefix (0xf2);
13132
0
        break;
13133
0
      case PREFIX_0XF3:
13134
0
        if (!is_padlock (&i.tm)
13135
0
      || (i.prefix[REP_PREFIX] != 0xf3))
13136
0
    add_prefix (0xf3);
13137
0
        break;
13138
6.38k
      case PREFIX_NONE:
13139
6.38k
        switch (i.opcode_length)
13140
6.38k
    {
13141
15
    case 2:
13142
15
      break;
13143
6.37k
    case 1:
13144
      /* Check for pseudo prefixes.  */
13145
6.37k
      if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
13146
6.37k
        break;
13147
0
      as_bad_where (insn_start_frag->fr_file,
13148
0
        insn_start_frag->fr_line,
13149
0
        _("pseudo prefix without instruction"));
13150
0
      return;
13151
0
    default:
13152
0
      abort ();
13153
6.38k
    }
13154
6.38k
        break;
13155
6.38k
      default:
13156
0
        abort ();
13157
6.40k
      }
13158
13159
6.40k
#ifdef OBJ_ELF
13160
    /* For x32, add a dummy REX_OPCODE prefix for mov/add with
13161
       R_X86_64_GOTTPOFF relocation so that linker can safely
13162
       perform IE->LE optimization.  A dummy REX_OPCODE prefix
13163
       is also needed for lea with R_X86_64_GOTPC32_TLSDESC
13164
       relocation for GDesc -> IE/LE optimization.  */
13165
6.40k
    if (x86_elf_abi == X86_64_X32_ABI
13166
0
        && !is_apx_rex2_encoding ()
13167
0
        && (dot_insn () ? i.insn_opcode_space
13168
0
            : i.tm.opcode_space) == SPACE_BASE
13169
0
        && i.operands == 2
13170
0
        && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
13171
0
      || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
13172
0
        && i.prefix[REX_PREFIX] == 0)
13173
0
      add_prefix (REX_OPCODE);
13174
6.40k
#endif
13175
13176
    /* The prefix bytes.  */
13177
51.2k
    for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
13178
44.8k
      if (*q)
13179
1.02k
        frag_opcode_byte (*q);
13180
13181
6.40k
    if (is_apx_rex2_encoding ())
13182
4
      {
13183
4
        frag_opcode_byte (i.vex.bytes[0]);
13184
4
        frag_opcode_byte (i.vex.bytes[1]);
13185
4
      }
13186
6.40k
  }
13187
679
      else
13188
679
  {
13189
5.43k
    for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
13190
4.75k
      if (*q)
13191
0
        switch (j)
13192
0
    {
13193
0
    case SEG_PREFIX:
13194
0
    case ADDR_PREFIX:
13195
0
      frag_opcode_byte (*q);
13196
0
      break;
13197
0
    default:
13198
      /* There should be no other prefixes for instructions
13199
         with VEX prefix.  */
13200
0
      abort ();
13201
0
    }
13202
13203
    /* For EVEX instructions i.vrex should become 0 after
13204
       build_evex_prefix.  For VEX instructions upper 16 registers
13205
       aren't available, so VREX should be 0.  */
13206
679
    if (i.vrex)
13207
0
      abort ();
13208
    /* Now the VEX prefix.  */
13209
679
    if (now_seg != absolute_section)
13210
679
      {
13211
679
        p = frag_more (i.vex.length);
13212
2.73k
        for (j = 0; j < i.vex.length; j++)
13213
2.05k
    p[j] = i.vex.bytes[j];
13214
679
      }
13215
0
    else
13216
0
      abs_section_offset += i.vex.length;
13217
679
  }
13218
13219
      /* Now the opcode; be careful about word order here!  */
13220
7.08k
      j = i.opcode_length;
13221
7.08k
      if (!i.vex.length)
13222
6.39k
  switch (i.tm.opcode_space)
13223
6.39k
    {
13224
6.36k
    case SPACE_BASE:
13225
6.36k
      break;
13226
33
    case SPACE_0F:
13227
33
      ++j;
13228
33
      break;
13229
0
    case SPACE_0F38:
13230
0
    case SPACE_0F3A:
13231
0
      j += 2;
13232
0
      break;
13233
0
    default:
13234
0
      abort ();
13235
6.39k
    }
13236
13237
7.08k
      if (now_seg == absolute_section)
13238
640
  abs_section_offset += j;
13239
6.44k
      else if (j == 1)
13240
6.39k
  {
13241
6.39k
    FRAG_APPEND_1_CHAR (i.tm.base_opcode);
13242
6.39k
  }
13243
48
      else
13244
48
  {
13245
48
    p = frag_more (j);
13246
48
    if (!i.vex.length
13247
48
        && i.tm.opcode_space != SPACE_BASE)
13248
33
      {
13249
33
        *p++ = 0x0f;
13250
33
        if (i.tm.opcode_space != SPACE_0F)
13251
0
    *p++ = i.tm.opcode_space == SPACE_0F38
13252
0
           ? 0x38 : 0x3a;
13253
33
      }
13254
13255
48
    switch (i.opcode_length)
13256
48
      {
13257
15
      case 2:
13258
        /* Put out high byte first: can't use md_number_to_chars!  */
13259
15
        *p++ = (i.tm.base_opcode >> 8) & 0xff;
13260
        /* Fall through.  */
13261
48
      case 1:
13262
48
        *p = i.tm.base_opcode & 0xff;
13263
48
        break;
13264
0
      default:
13265
0
        abort ();
13266
0
        break;
13267
48
      }
13268
13269
48
  }
13270
13271
      /* Now the modrm byte and sib byte (if present).  */
13272
7.08k
      if (i.tm.opcode_modifier.modrm)
13273
2.24k
  {
13274
2.24k
    frag_opcode_byte ((i.rm.regmem << 0)
13275
2.24k
           | (i.rm.reg << 3)
13276
2.24k
           | (i.rm.mode << 6));
13277
    /* If i.rm.regmem == ESP (4)
13278
       && i.rm.mode != (Register mode)
13279
       && not 16 bit
13280
       ==> need second modrm byte.  */
13281
2.24k
    if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
13282
1.90k
        && i.rm.mode != 3
13283
1.88k
        && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
13284
1.88k
      frag_opcode_byte ((i.sib.base << 0)
13285
1.88k
            | (i.sib.index << 3)
13286
1.88k
            | (i.sib.scale << 6));
13287
2.24k
  }
13288
13289
7.08k
      if (i.disp_operands)
13290
2.02k
  output_disp (insn_start_frag, insn_start_off);
13291
13292
7.08k
      if (i.imm_operands)
13293
3.94k
  output_imm (insn_start_frag, insn_start_off);
13294
13295
      /*
13296
       * frag_now_fix () returning plain abs_section_offset when we're in the
13297
       * absolute section, and abs_section_offset not getting updated as data
13298
       * gets added to the frag breaks the logic below.
13299
       */
13300
7.08k
      if (now_seg != absolute_section)
13301
6.44k
  {
13302
6.44k
    j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
13303
6.44k
    if (j > 15)
13304
0
      {
13305
0
        if (dot_insn ())
13306
0
    as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
13307
0
      j);
13308
0
        else
13309
0
    as_bad (_("instruction length of %u bytes exceeds the limit of 15"),
13310
0
      j);
13311
0
      }
13312
6.44k
    else if (fragP)
13313
0
      {
13314
        /* NB: Don't add prefix with GOTPC relocation since
13315
     output_disp() above depends on the fixed encoding
13316
     length.  Can't add prefix with TLS relocation since
13317
     it breaks TLS linker optimization.  */
13318
0
        unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
13319
        /* Prefix count on the current instruction.  */
13320
0
        unsigned int count = i.vex.length;
13321
0
        unsigned int k;
13322
0
        for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
13323
    /* REX byte is encoded in VEX/EVEX prefix.  */
13324
0
    if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
13325
0
      count++;
13326
13327
        /* Count prefixes for extended opcode maps.  */
13328
0
        if (!i.vex.length)
13329
0
    switch (i.tm.opcode_space)
13330
0
      {
13331
0
      case SPACE_BASE:
13332
0
        break;
13333
0
      case SPACE_0F:
13334
0
        count++;
13335
0
        break;
13336
0
      case SPACE_0F38:
13337
0
      case SPACE_0F3A:
13338
0
        count += 2;
13339
0
        break;
13340
0
      default:
13341
0
        abort ();
13342
0
      }
13343
13344
0
        if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
13345
0
      == BRANCH_PREFIX)
13346
0
    {
13347
      /* Set the maximum prefix size in BRANCH_PREFIX
13348
         frag.  */
13349
0
      if (fragP->tc_frag_data.max_bytes > max)
13350
0
        fragP->tc_frag_data.max_bytes = max;
13351
0
      if (fragP->tc_frag_data.max_bytes > count)
13352
0
        fragP->tc_frag_data.max_bytes -= count;
13353
0
      else
13354
0
        fragP->tc_frag_data.max_bytes = 0;
13355
0
    }
13356
0
        else
13357
0
    {
13358
      /* Remember the maximum prefix size in FUSED_JCC_PADDING
13359
         frag.  */
13360
0
      unsigned int max_prefix_size;
13361
0
      if (align_branch_prefix_size > max)
13362
0
        max_prefix_size = max;
13363
0
      else
13364
0
        max_prefix_size = align_branch_prefix_size;
13365
0
      if (max_prefix_size > count)
13366
0
        fragP->tc_frag_data.max_prefix_length
13367
0
          = max_prefix_size - count;
13368
0
    }
13369
13370
        /* Use existing segment prefix if possible.  Use CS
13371
     segment prefix in 64-bit mode.  In 32-bit mode, use SS
13372
     segment prefix with ESP/EBP base register and use DS
13373
     segment prefix without ESP/EBP base register.  */
13374
0
        if (i.prefix[SEG_PREFIX])
13375
0
    fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
13376
0
        else if (flag_code == CODE_64BIT)
13377
0
    fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
13378
0
        else if (i.base_reg
13379
0
           && (i.base_reg->reg_num == 4
13380
0
         || i.base_reg->reg_num == 5))
13381
0
    fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
13382
0
        else
13383
0
    fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
13384
0
      }
13385
6.44k
  }
13386
7.08k
    }
13387
13388
  /* NB: Don't work with COND_JUMP86 without i386.  */
13389
7.25k
  if (align_branch_power
13390
0
      && now_seg != absolute_section
13391
0
      && cpu_arch_flags.bitfield.cpui386)
13392
0
    {
13393
      /* Terminate each frag so that we can add prefix and check for
13394
         fused jcc.  */
13395
0
      frag_wane (frag_now);
13396
0
      frag_new (0);
13397
0
    }
13398
13399
#ifdef DEBUG386
13400
  if (flag_debug)
13401
    {
13402
      pi ("" /*line*/, &i);
13403
    }
13404
#endif /* DEBUG386  */
13405
7.25k
}
13406
13407
/* Return the size of the displacement operand N.  */
13408
13409
static int
13410
disp_size (unsigned int n)
13411
2.02k
{
13412
2.02k
  int size = 4;
13413
13414
2.02k
  if (i.types[n].bitfield.disp64)
13415
4
    size = 8;
13416
2.02k
  else if (i.types[n].bitfield.disp8)
13417
1
    size = 1;
13418
2.02k
  else if (i.types[n].bitfield.disp16)
13419
135
    size = 2;
13420
2.02k
  return size;
13421
2.02k
}
13422
13423
/* Return the size of the immediate operand N.  */
13424
13425
static int
13426
imm_size (unsigned int n)
13427
3.94k
{
13428
3.94k
  int size = 4;
13429
3.94k
  if (i.types[n].bitfield.imm64)
13430
0
    size = 8;
13431
3.94k
  else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
13432
1.70k
    size = 1;
13433
2.24k
  else if (i.types[n].bitfield.imm16)
13434
7
    size = 2;
13435
3.94k
  return size;
13436
3.94k
}
13437
13438
static void
13439
output_disp (fragS *insn_start_frag, offsetT insn_start_off)
13440
2.02k
{
13441
2.02k
  char *p;
13442
2.02k
  unsigned int n;
13443
13444
5.67k
  for (n = 0; n < i.operands; n++)
13445
3.65k
    {
13446
3.65k
      if (operand_type_check (i.types[n], disp))
13447
2.02k
  {
13448
2.02k
    int size = disp_size (n);
13449
13450
2.02k
    if (now_seg == absolute_section)
13451
141
      abs_section_offset += size;
13452
1.88k
    else if (i.op[n].disps->X_op == O_constant)
13453
8
      {
13454
8
        offsetT val = i.op[n].disps->X_add_number;
13455
13456
8
        val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
13457
8
             size);
13458
8
        p = frag_more (size);
13459
8
        md_number_to_chars (p, val, size);
13460
8
      }
13461
1.87k
    else
13462
1.87k
      {
13463
1.87k
        enum bfd_reloc_code_real reloc_type;
13464
1.87k
        bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
13465
1.87k
        bool sign = (flag_code == CODE_64BIT && size == 4
13466
1.86k
         && (!want_disp32 (&i.tm)
13467
0
             || (i.tm.opcode_modifier.jump && !i.jumpabsolute
13468
0
           && !i.types[n].bitfield.baseindex)))
13469
11
        || pcrel;
13470
1.87k
        fixS *fixP;
13471
13472
        /* We can't have 8 bit displacement here.  */
13473
1.87k
        gas_assert (!i.types[n].bitfield.disp8);
13474
13475
        /* The PC relative address is computed relative
13476
     to the instruction boundary, so in case immediate
13477
     fields follows, we need to adjust the value.  */
13478
1.87k
        if (pcrel && i.imm_operands)
13479
0
    {
13480
0
      unsigned int n1;
13481
0
      int sz = 0;
13482
13483
0
      for (n1 = 0; n1 < i.operands; n1++)
13484
0
        if (operand_type_check (i.types[n1], imm))
13485
0
          {
13486
      /* Only one immediate is allowed for PC
13487
         relative address, except with .insn.  */
13488
0
      gas_assert (sz == 0 || dot_insn ());
13489
0
      sz += imm_size (n1);
13490
0
          }
13491
      /* We should find at least one immediate.  */
13492
0
      gas_assert (sz != 0);
13493
0
      i.op[n].disps->X_add_number -= sz;
13494
0
    }
13495
13496
1.87k
        p = frag_more (size);
13497
1.87k
        reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
13498
1.87k
        if (GOT_symbol
13499
17
      && GOT_symbol == i.op[n].disps->X_add_symbol
13500
0
      && (((reloc_type == BFD_RELOC_32
13501
0
      || reloc_type == BFD_RELOC_X86_64_32S
13502
0
      || (reloc_type == BFD_RELOC_64
13503
0
          && object_64bit))
13504
0
           && (i.op[n].disps->X_op == O_symbol
13505
0
         || (i.op[n].disps->X_op == O_add
13506
0
             && ((symbol_get_value_expression
13507
0
            (i.op[n].disps->X_op_symbol)->X_op)
13508
0
           == O_subtract))))
13509
0
          || reloc_type == BFD_RELOC_32_PCREL))
13510
0
    {
13511
0
      if (!object_64bit)
13512
0
        {
13513
0
          reloc_type = BFD_RELOC_32_GOT_PCREL;
13514
0
          i.has_gotpc_tls_reloc = true;
13515
0
          i.op[n].disps->X_add_number +=
13516
0
      encoding_length (insn_start_frag, insn_start_off, p);
13517
0
        }
13518
0
      else if (reloc_type == BFD_RELOC_64)
13519
0
        reloc_type = BFD_RELOC_64_GOT_PCREL;
13520
0
      else
13521
        /* Don't do the adjustment for x86-64, as there
13522
           the pcrel addressing is relative to the _next_
13523
           insn, and that is taken care of in other code.  */
13524
0
        reloc_type = BFD_RELOC_X86_64_GOTPC32;
13525
0
    }
13526
1.87k
        else if (align_branch_power)
13527
0
    {
13528
0
      switch (reloc_type)
13529
0
        {
13530
0
        case BFD_RELOC_386_TLS_GD:
13531
0
        case BFD_RELOC_386_TLS_LDM:
13532
0
        case BFD_RELOC_386_TLS_IE:
13533
0
        case BFD_RELOC_386_TLS_IE_32:
13534
0
        case BFD_RELOC_386_TLS_GOTIE:
13535
0
        case BFD_RELOC_386_TLS_GOTDESC:
13536
0
        case BFD_RELOC_386_TLS_DESC_CALL:
13537
0
        case BFD_RELOC_X86_64_TLSGD:
13538
0
        case BFD_RELOC_X86_64_TLSLD:
13539
0
        case BFD_RELOC_X86_64_GOTTPOFF:
13540
0
        case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
13541
0
        case BFD_RELOC_X86_64_CODE_5_GOTTPOFF:
13542
0
        case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
13543
0
        case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
13544
0
        case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
13545
0
        case BFD_RELOC_X86_64_CODE_5_GOTPC32_TLSDESC:
13546
0
        case BFD_RELOC_X86_64_CODE_6_GOTPC32_TLSDESC:
13547
0
        case BFD_RELOC_X86_64_TLSDESC_CALL:
13548
0
          i.has_gotpc_tls_reloc = true;
13549
0
        default:
13550
0
          break;
13551
0
        }
13552
0
    }
13553
1.87k
        fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
13554
1.87k
          size, i.op[n].disps, pcrel,
13555
1.87k
          reloc_type);
13556
13557
1.87k
        if (flag_code == CODE_64BIT && size == 4 && pcrel
13558
0
      && !i.prefix[ADDR_PREFIX])
13559
0
    fixP->fx_signed = 1;
13560
13561
1.87k
        if (i.base_reg && i.base_reg->reg_num == RegIP)
13562
0
    {
13563
0
      if (reloc_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
13564
0
        {
13565
          /* Set fx_tcbit for REX2 prefix.  */
13566
0
          if (is_apx_rex2_encoding ())
13567
0
      fixP->fx_tcbit = 1;
13568
0
          continue;
13569
0
        }
13570
0
    }
13571
        /* In 64-bit, i386_validate_fix updates only (%rip)
13572
     relocations.  */
13573
1.87k
        else if (object_64bit)
13574
1.87k
    continue;
13575
13576
0
#ifdef OBJ_ELF
13577
        /* Check for "call/jmp *mem", "push mem", "mov mem, %reg",
13578
     "movrs mem, %reg", "test %reg, mem" and "binop mem, %reg" where
13579
     binop is one of adc, add, and, cmp, or, sbb, sub, xor, or imul
13580
     instructions without data prefix.  Always generate
13581
     R_386_GOT32X for "sym*GOT" operand in 32-bit mode.  */
13582
0
        unsigned int space = dot_insn () ? i.insn_opcode_space
13583
0
                 : i.tm.opcode_space;
13584
0
        if (i.prefix[DATA_PREFIX] == 0
13585
0
      && (i.rm.mode == 2
13586
0
          || (i.rm.mode == 0 && i.rm.regmem == 5))
13587
0
      && ((space == SPACE_BASE
13588
0
           && i.tm.base_opcode == 0xff
13589
0
           && (i.rm.reg == 2 || i.rm.reg == 4 || i.rm.reg == 6))
13590
0
          || ((space == SPACE_BASE
13591
0
         || space == SPACE_0F38
13592
0
         || space == SPACE_MAP4)
13593
0
        && i.tm.base_opcode == 0x8b)
13594
0
          || ((space == SPACE_BASE
13595
0
         || space == SPACE_MAP4)
13596
0
        && (i.tm.base_opcode == 0x85
13597
0
            || (i.tm.base_opcode
13598
0
          | (i.operands > 2 ? 0x3a : 0x38)) == 0x3b))
13599
0
          || (((space == SPACE_0F
13600
          /* Because of the 0F prefix, no suitable relocation
13601
             exists for this unless it's REX2-encoded.  */
13602
0
          && is_apx_rex2_encoding ())
13603
0
         || space == SPACE_MAP4)
13604
0
        && i.tm.base_opcode == 0xaf)))
13605
0
    {
13606
0
      if (object_64bit)
13607
0
        {
13608
0
          if (reloc_type == BFD_RELOC_X86_64_GOTTPOFF)
13609
0
      {
13610
0
        if (space == SPACE_MAP4)
13611
0
          fixP->fx_tcbit3 = 1;
13612
0
        else if (space == SPACE_0F38 && i.rex)
13613
0
          fixP->fx_tcbit2 = 1;
13614
0
        else if (space == SPACE_0F38 || is_apx_rex2_encoding ())
13615
0
          fixP->fx_tcbit = 1;
13616
0
      }
13617
0
          else if (generate_relax_relocations)
13618
0
      {
13619
0
        if (space == SPACE_MAP4)
13620
0
          {
13621
0
            fixP->fx_tcbit3 = 1;
13622
0
            fixP->fx_tcbit2 = 1;
13623
0
          }
13624
0
        else if (space == SPACE_0F38)
13625
0
          {
13626
0
            fixP->fx_tcbit3 = 1;
13627
0
            if (i.rex)
13628
0
        fixP->fx_tcbit = 1;
13629
0
          }
13630
0
        else if (is_apx_rex2_encoding ())
13631
0
          fixP->fx_tcbit3 = 1;
13632
0
        else if (i.rex)
13633
0
          fixP->fx_tcbit2 = 1;
13634
0
        else
13635
0
          fixP->fx_tcbit = 1;
13636
0
      }
13637
0
        }
13638
0
      else if (generate_relax_relocations
13639
0
         ? (!shared || i.rm.mode != 0 || i.rm.regmem != 5)
13640
0
         : (!shared && i.rm.mode == 0 && i.rm.regmem == 5))
13641
0
        fixP->fx_tcbit2 = 1;
13642
0
    }
13643
0
#endif
13644
0
      }
13645
2.02k
  }
13646
3.65k
    }
13647
2.02k
}
13648
13649
static void
13650
output_imm (fragS *insn_start_frag, offsetT insn_start_off)
13651
3.94k
{
13652
3.94k
  char *p;
13653
3.94k
  unsigned int n;
13654
13655
9.27k
  for (n = 0; n < i.operands; n++)
13656
5.32k
    {
13657
5.32k
      if (operand_type_check (i.types[n], imm))
13658
3.94k
  {
13659
3.94k
    int size = imm_size (n);
13660
13661
3.94k
    if (now_seg == absolute_section)
13662
2
      abs_section_offset += size;
13663
3.94k
    else if (i.op[n].imms->X_op == O_constant)
13664
1.46k
      {
13665
1.46k
        offsetT val;
13666
13667
1.46k
        val = offset_in_range (i.op[n].imms->X_add_number,
13668
1.46k
             size);
13669
1.46k
        p = frag_more (size);
13670
1.46k
        md_number_to_chars (p, val, size);
13671
1.46k
      }
13672
2.48k
    else
13673
2.48k
      {
13674
        /* Not absolute_section.
13675
     Need a 32-bit fixup (don't support 8bit
13676
     non-absolute imms).  Try to support other
13677
     sizes ...  */
13678
2.48k
        enum bfd_reloc_code_real reloc_type;
13679
2.48k
        int sign;
13680
13681
2.48k
        if (i.types[n].bitfield.imm32s
13682
2.22k
      && (i.suffix == QWORD_MNEM_SUFFIX
13683
2.22k
          || (!i.suffix && i.tm.opcode_modifier.no_lsuf)
13684
0
          || (i.prefix[REX_PREFIX] & REX_W)
13685
0
          || dot_insn ()))
13686
2.22k
    sign = 1;
13687
262
        else
13688
262
    sign = 0;
13689
13690
2.48k
        p = frag_more (size);
13691
2.48k
        reloc_type = reloc (size, 0, sign, i.reloc[n]);
13692
13693
        /*   This is tough to explain.  We end up with this one if we
13694
         * have operands that look like
13695
         * "_GLOBAL_OFFSET_TABLE_+[.-.L284]".  The goal here is to
13696
         * obtain the absolute address of the GOT, and it is strongly
13697
         * preferable from a performance point of view to avoid using
13698
         * a runtime relocation for this.  The actual sequence of
13699
         * instructions often look something like:
13700
         *
13701
         *  call  .L66
13702
         * .L66:
13703
         *  popl  %ebx
13704
         *  addl  $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
13705
         *
13706
         *   The call and pop essentially return the absolute address
13707
         * of the label .L66 and store it in %ebx.  The linker itself
13708
         * will ultimately change the first operand of the addl so
13709
         * that %ebx points to the GOT, but to keep things simple, the
13710
         * .o file must have this operand set so that it generates not
13711
         * the absolute address of .L66, but the absolute address of
13712
         * itself.  This allows the linker itself simply treat a GOTPC
13713
         * relocation as asking for a pcrel offset to the GOT to be
13714
         * added in, and the addend of the relocation is stored in the
13715
         * operand field for the instruction itself.
13716
         *
13717
         *   Our job here is to fix the operand so that it would add
13718
         * the correct offset so that %ebx would point to itself.  The
13719
         * thing that is tricky is that .-.L66 will point to the
13720
         * beginning of the instruction, so we need to further modify
13721
         * the operand so that it will point to itself.  There are
13722
         * other cases where you have something like:
13723
         *
13724
         *  .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
13725
         *
13726
         * and here no correction would be required.  Internally in
13727
         * the assembler we treat operands of this form as not being
13728
         * pcrel since the '.' is explicitly mentioned, and I wonder
13729
         * whether it would simplify matters to do it this way.  Who
13730
         * knows.  In earlier versions of the PIC patches, the
13731
         * pcrel_adjust field was used to store the correction, but
13732
         * since the expression is not pcrel, I felt it would be
13733
         * confusing to do it this way.  */
13734
13735
2.48k
        if ((reloc_type == BFD_RELOC_32
13736
2.48k
       || reloc_type == BFD_RELOC_X86_64_32S
13737
261
       || reloc_type == BFD_RELOC_64)
13738
2.22k
      && GOT_symbol
13739
2.22k
      && GOT_symbol == i.op[n].imms->X_add_symbol
13740
0
      && (i.op[n].imms->X_op == O_symbol
13741
0
          || (i.op[n].imms->X_op == O_add
13742
0
        && ((symbol_get_value_expression
13743
0
             (i.op[n].imms->X_op_symbol)->X_op)
13744
0
            == O_subtract))))
13745
0
    {
13746
0
      if (!object_64bit)
13747
0
        reloc_type = BFD_RELOC_32_GOT_PCREL;
13748
0
      else if (size == 4)
13749
0
        reloc_type = BFD_RELOC_X86_64_GOTPC32;
13750
0
      else if (size == 8)
13751
0
        reloc_type = BFD_RELOC_64_GOT_PCREL;
13752
0
      i.has_gotpc_tls_reloc = true;
13753
0
      i.op[n].imms->X_add_number +=
13754
0
        encoding_length (insn_start_frag, insn_start_off, p);
13755
0
    }
13756
2.48k
        fix_new_exp (frag_now, p - frag_now->fr_literal, size,
13757
2.48k
         i.op[n].imms, 0, reloc_type);
13758
2.48k
      }
13759
3.94k
  }
13760
5.32k
    }
13761
3.94k
}
13762

13763
/* x86_cons_fix_new is called via the expression parsing code when a
13764
   reloc is needed.  We use this hook to get the correct .got reloc.  */
13765
static int cons_sign = -1;
13766
13767
void
13768
x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
13769
      expressionS *exp, bfd_reloc_code_real_type r)
13770
975
{
13771
975
  r = reloc (len, 0, cons_sign, r);
13772
13773
#ifdef TE_PE
13774
  if (exp->X_op == O_secrel)
13775
    {
13776
      exp->X_op = O_symbol;
13777
      r = BFD_RELOC_32_SECREL;
13778
    }
13779
  else if (exp->X_op == O_secidx)
13780
    r = BFD_RELOC_16_SECIDX;
13781
#endif
13782
13783
975
  fix_new_exp (frag, off, len, exp, 0, r);
13784
975
}
13785
13786
/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
13787
   purpose of the `.dc.a' internal pseudo-op.  */
13788
13789
int
13790
x86_address_bytes (void)
13791
593
{
13792
593
  if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
13793
0
    return 4;
13794
593
  return stdoutput->arch_info->bits_per_address / 8;
13795
593
}
13796
13797
#if (defined (OBJ_ELF) || defined (OBJ_MACH_O) || defined (TE_PE))
13798
/* Parse operands of the form
13799
   <symbol>@GOTOFF+<nnn>
13800
   and similar .plt or .got references.
13801
13802
   If we find one, set up the correct relocation in RELOC and copy the
13803
   input string, minus the `@GOTOFF' into a malloc'd buffer for
13804
   parsing by the calling routine.  Return this buffer, and if ADJUST
13805
   is non-null set it to the length of the string we removed from the
13806
   input line.  Otherwise return NULL.  */
13807
static char *
13808
lex_got (enum bfd_reloc_code_real *rel,
13809
   int *adjust,
13810
   i386_operand_type *types)
13811
4.09k
{
13812
  /* Some of the relocations depend on the size of what field is to
13813
     be relocated.  But in our callers i386_immediate and i386_displacement
13814
     we don't yet know the operand size (this will be set by insn
13815
     matching).  Hence we record the word32 relocation here,
13816
     and adjust the reloc according to the real size in reloc().  */
13817
4.09k
  char *cp;
13818
4.09k
  unsigned int j;
13819
13820
64.7k
  for (cp = input_line_pointer; *cp != '@'; cp++)
13821
61.8k
    if (is_end_of_stmt (*cp) || *cp == ',')
13822
1.26k
      return NULL;
13823
13824
48.5k
  for (j = 0; j < ARRAY_SIZE (gotrel); j++)
13825
46.1k
    {
13826
46.1k
      int len = gotrel[j].len;
13827
46.1k
      if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
13828
397
  {
13829
397
    if (gotrel[j].rel[object_64bit] != 0)
13830
391
      {
13831
391
        int first, second;
13832
391
        char *tmpbuf, *past_reloc;
13833
13834
391
        i.has_gotrel = true;
13835
391
        *rel = gotrel[j].rel[object_64bit];
13836
13837
391
        if (types)
13838
368
    {
13839
368
      if (flag_code != CODE_64BIT)
13840
2
        {
13841
2
          types->bitfield.imm32 = 1;
13842
2
          types->bitfield.disp32 = 1;
13843
2
        }
13844
366
      else
13845
366
        *types = gotrel[j].types64;
13846
368
    }
13847
13848
391
        if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
13849
20
    GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
13850
13851
        /* The length of the first part of our input line.  */
13852
391
        first = cp - input_line_pointer;
13853
13854
        /* The second part goes from after the reloc token until
13855
     (and including) an end_of_line char or comma.  */
13856
391
        past_reloc = cp + 1 + len;
13857
391
        cp = past_reloc;
13858
3.75k
        while (!is_end_of_stmt (*cp) && *cp != ',')
13859
3.36k
    ++cp;
13860
391
        second = cp + 1 - past_reloc;
13861
13862
        /* Allocate and copy string.  The trailing NUL shouldn't
13863
     be necessary, but be safe.  */
13864
391
        tmpbuf = XNEWVEC (char, first + second + 2);
13865
391
        memcpy (tmpbuf, input_line_pointer, first);
13866
391
        if (second != 0 && !is_whitespace (*past_reloc))
13867
    /* Replace the relocation token with ' ', so that
13868
       errors like foo@GOTOFF1 will be detected.  */
13869
376
    tmpbuf[first++] = ' ';
13870
15
        else
13871
    /* Increment length by 1 if the relocation token is
13872
       removed.  */
13873
15
    len++;
13874
391
        if (adjust)
13875
378
    *adjust = len;
13876
391
        memcpy (tmpbuf + first, past_reloc, second);
13877
391
        tmpbuf[first + second] = '\0';
13878
391
        return tmpbuf;
13879
391
      }
13880
13881
6
    as_bad (_("@%s reloc is not supported with %d-bit output format"),
13882
6
      gotrel[j].str, 1 << (5 + object_64bit));
13883
6
    return NULL;
13884
397
  }
13885
46.1k
    }
13886
13887
  /* Might be a symbol version string.  Don't as_bad here.  */
13888
2.43k
  return NULL;
13889
2.83k
}
13890
#else
13891
# define lex_got(reloc, adjust, types) NULL
13892
#endif
13893
13894
bfd_reloc_code_real_type
13895
x86_cons (expressionS *exp, int size)
13896
506
{
13897
506
  bfd_reloc_code_real_type got_reloc = NO_RELOC;
13898
13899
506
  intel_syntax = -intel_syntax;
13900
506
  exp->X_md = 0;
13901
506
  expr_mode = expr_operator_none;
13902
13903
506
#if defined (OBJ_ELF) || defined (TE_PE)
13904
506
  if (size == 4
13905
# ifdef TE_PE
13906
      || (size == 2)
13907
# endif
13908
400
      || (object_64bit && size == 8))
13909
127
    {
13910
      /* Handle @GOTOFF and the like in an expression.  */
13911
127
      char *save;
13912
127
      char *gotfree_input_line;
13913
127
      int adjust = 0;
13914
13915
127
      save = input_line_pointer;
13916
127
      gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
13917
127
      if (gotfree_input_line)
13918
23
  input_line_pointer = gotfree_input_line;
13919
13920
127
      expression (exp);
13921
13922
127
      if (gotfree_input_line)
13923
23
  {
13924
    /* expression () has merrily parsed up to the end of line,
13925
       or a comma - in the wrong buffer.  Transfer how far
13926
       input_line_pointer has moved to the right buffer.  */
13927
23
    input_line_pointer = (save
13928
23
        + (input_line_pointer - gotfree_input_line)
13929
23
        + adjust);
13930
23
    free (gotfree_input_line);
13931
23
    if (exp->X_op == O_constant
13932
21
        || exp->X_op == O_absent
13933
20
        || exp->X_op == O_illegal
13934
20
        || exp->X_op == O_register
13935
20
        || exp->X_op == O_big)
13936
3
      {
13937
3
        char c = *input_line_pointer;
13938
3
        *input_line_pointer = 0;
13939
3
        as_bad (_("missing or invalid expression `%s'"), save);
13940
3
        *input_line_pointer = c;
13941
3
      }
13942
20
    else if ((got_reloc == BFD_RELOC_386_PLT32
13943
20
        || got_reloc == BFD_RELOC_32_PLT_PCREL)
13944
3
       && exp->X_op != O_symbol)
13945
2
      {
13946
      /* Allow directives like ".long foo@PLT - .L4".
13947
         BFD_RELOC_X86_64_PC32_TO_PLT32 has an explicit addend and
13948
         BFD_RELOC_386_PC32_TO_PLT32 has an implicit addend.  */
13949
2
        if (size == 4 && exp->X_op == O_subtract)
13950
0
    got_reloc = (object_64bit
13951
0
           ? BFD_RELOC_X86_64_PC32_TO_PLT32
13952
0
           : BFD_RELOC_386_PC32_TO_PLT32);
13953
2
        else
13954
2
    {
13955
2
      char c = *input_line_pointer;
13956
2
      *input_line_pointer = 0;
13957
2
      as_bad (_("invalid PLT expression `%s'"), save);
13958
2
      *input_line_pointer = c;
13959
2
    }
13960
2
      }
13961
23
  }
13962
127
    }
13963
379
  else
13964
379
#endif
13965
379
    expression (exp);
13966
13967
506
  intel_syntax = -intel_syntax;
13968
13969
506
  if (intel_syntax)
13970
476
    i386_intel_simplify (exp);
13971
13972
  /* If not 64bit, massage value, to account for wraparound when !BFD64.  */
13973
506
  if (size <= 4 && expr_mode == expr_operator_present
13974
153
      && exp->X_op == O_constant && !object_64bit)
13975
0
    exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
13976
13977
506
  return got_reloc;
13978
506
}
13979
13980
static void
13981
signed_cons (int size)
13982
8
{
13983
8
  if (object_64bit)
13984
8
    cons_sign = 1;
13985
8
  cons (size);
13986
8
  cons_sign = -1;
13987
8
}
13988
13989
static void
13990
s_insn (int dummy ATTRIBUTE_UNUSED)
13991
5.01k
{
13992
5.01k
  char mnemonic[MAX_MNEM_SIZE], *line = input_line_pointer, *ptr;
13993
5.01k
  char *saved_ilp = find_end_of_line (line, false), saved_char;
13994
5.01k
  const char *end;
13995
5.01k
  unsigned int j;
13996
5.01k
  valueT val;
13997
5.01k
  bool vex = false, xop = false;
13998
5.01k
  enum { evex_none, evex_basic, evex_nd } evex = evex_none;
13999
5.01k
  struct last_insn *last_insn;
14000
14001
5.01k
  init_globals ();
14002
14003
5.01k
  saved_char = *saved_ilp;
14004
5.01k
  *saved_ilp = 0;
14005
14006
5.01k
  end = parse_insn (line, mnemonic, parse_prefix);
14007
5.01k
  if (end == NULL)
14008
1
    {
14009
3.39k
  bad:
14010
3.39k
      *saved_ilp = saved_char;
14011
3.39k
      input_line_pointer = saved_ilp;
14012
3.39k
      ignore_rest_of_line ();
14013
3.39k
      i.tm.mnem_off = 0;
14014
3.39k
      memset (&pp, 0, sizeof (pp));
14015
3.39k
      return;
14016
1
    }
14017
5.01k
  line += end - line;
14018
14019
5.01k
  current_templates.start = &i.tm;
14020
5.01k
  current_templates.end = &i.tm + 1;
14021
5.01k
  i.tm.mnem_off = MN__insn;
14022
5.01k
  i.tm.extension_opcode = None;
14023
14024
5.01k
  if (startswith (line, "VEX")
14025
602
      && (line[3] == '.' || is_whitespace (line[3])))
14026
601
    {
14027
601
      vex = true;
14028
601
      line += 3;
14029
601
    }
14030
4.41k
  else if (startswith (line, "XOP") && ISDIGIT (line[3]))
14031
89
    {
14032
89
      char *e;
14033
89
      unsigned long n = strtoul (line + 3, &e, 16);
14034
14035
89
      if (e == line + 5 && n >= 0x08 && n <= 0x1f
14036
0
    && (*e == '.' || is_whitespace (*e)))
14037
0
  {
14038
0
    xop = true;
14039
    /* Arrange for build_vex_prefix() to emit 0x8f.  */
14040
0
    i.tm.opcode_space = SPACE_XOP08;
14041
0
    i.insn_opcode_space = n;
14042
0
    line = e;
14043
0
  }
14044
89
    }
14045
4.32k
  else if (startswith (line, "EVEX")
14046
3.41k
     && (line[4] == '.' || is_whitespace (line[4])))
14047
3.40k
    {
14048
3.40k
      evex = evex_basic;
14049
3.40k
      line += 4;
14050
3.40k
    }
14051
14052
5.01k
  if (vex || xop
14053
5.01k
      ? pp.encoding == encoding_evex
14054
5.01k
      : evex
14055
4.41k
  ? pp.encoding == encoding_vex
14056
3.40k
    || pp.encoding == encoding_vex3
14057
4.41k
  : pp.encoding != encoding_default)
14058
0
    {
14059
0
      as_bad (_("pseudo-prefix conflicts with encoding specifier"));
14060
0
      goto bad;
14061
0
    }
14062
14063
5.01k
  if (line > end && pp.encoding == encoding_default)
14064
2.89k
    pp.encoding = evex ? encoding_evex : encoding_vex;
14065
14066
5.01k
  if (pp.encoding != encoding_default)
14067
4.00k
    {
14068
      /* Only address size and segment override prefixes are permitted with
14069
         VEX/XOP/EVEX encodings.  */
14070
4.00k
      const unsigned char *p = i.prefix;
14071
14072
32.0k
      for (j = 0; j < ARRAY_SIZE (i.prefix); ++j, ++p)
14073
28.0k
  {
14074
28.0k
    if (!*p)
14075
28.0k
      continue;
14076
14077
0
    switch (j)
14078
0
      {
14079
0
      case SEG_PREFIX:
14080
0
      case ADDR_PREFIX:
14081
0
        break;
14082
0
      default:
14083
0
      as_bad (_("illegal prefix used with VEX/XOP/EVEX"));
14084
0
      goto bad;
14085
0
      }
14086
0
  }
14087
4.00k
    }
14088
14089
5.01k
  if (line > end && *line == '.')
14090
2.26k
    {
14091
      /* Length specifier (VEX.L, XOP.L, EVEX.L'L).  */
14092
2.26k
      switch (line[1])
14093
2.26k
  {
14094
2.24k
  case 'L':
14095
2.24k
    switch (line[2])
14096
2.24k
      {
14097
1
      case '0':
14098
1
        if (evex)
14099
1
    i.tm.opcode_modifier.evex = EVEX128;
14100
0
        else
14101
0
    i.tm.opcode_modifier.vex = VEX128;
14102
1
        break;
14103
14104
2.24k
      case '1':
14105
2.24k
        if (evex)
14106
2.23k
    i.tm.opcode_modifier.evex = EVEX256;
14107
12
        else
14108
12
    i.tm.opcode_modifier.vex = VEX256;
14109
2.24k
        break;
14110
14111
0
      case '2':
14112
0
        if (evex)
14113
0
    i.tm.opcode_modifier.evex = EVEX512;
14114
0
        break;
14115
14116
0
      case '3':
14117
0
        if (evex)
14118
0
    i.tm.opcode_modifier.evex = EVEX_L3;
14119
0
        break;
14120
14121
0
      case 'I':
14122
0
        if (line[3] == 'G')
14123
0
    {
14124
0
      if (evex)
14125
0
        i.tm.opcode_modifier.evex = EVEXLIG;
14126
0
      else
14127
0
        i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
14128
0
      ++line;
14129
0
    }
14130
0
        break;
14131
2.24k
      }
14132
14133
2.24k
    if (i.tm.opcode_modifier.vex || i.tm.opcode_modifier.evex)
14134
2.24k
      line += 3;
14135
2.24k
    break;
14136
14137
0
  case '1':
14138
0
    if (line[2] == '2' && line[3] == '8')
14139
0
      {
14140
0
        if (evex)
14141
0
    i.tm.opcode_modifier.evex = EVEX128;
14142
0
        else
14143
0
    i.tm.opcode_modifier.vex = VEX128;
14144
0
        line += 4;
14145
0
      }
14146
0
    break;
14147
14148
0
  case '2':
14149
0
    if (line[2] == '5' && line[3] == '6')
14150
0
      {
14151
0
        if (evex)
14152
0
    i.tm.opcode_modifier.evex = EVEX256;
14153
0
        else
14154
0
    i.tm.opcode_modifier.vex = VEX256;
14155
0
        line += 4;
14156
0
      }
14157
0
    break;
14158
14159
0
  case '5':
14160
0
    if (evex && line[2] == '1' && line[3] == '2')
14161
0
      {
14162
0
        i.tm.opcode_modifier.evex = EVEX512;
14163
0
        line += 4;
14164
0
      }
14165
0
    break;
14166
2.26k
  }
14167
2.26k
    }
14168
14169
5.01k
  if (line > end && *line == '.')
14170
22
    {
14171
      /* embedded prefix (VEX.pp, XOP.pp, EVEX.pp).  */
14172
22
      switch (line[1])
14173
22
  {
14174
0
  case 'N':
14175
0
    if (line[2] == 'P')
14176
0
      line += 3;
14177
0
    break;
14178
14179
0
  case '6':
14180
0
    if (line[2] == '6')
14181
0
      {
14182
0
        i.tm.opcode_modifier.opcodeprefix = PREFIX_0X66;
14183
0
        line += 3;
14184
0
      }
14185
0
    break;
14186
14187
1
  case 'F': case 'f':
14188
1
    if (line[2] == '3')
14189
0
      {
14190
0
        i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
14191
0
        line += 3;
14192
0
      }
14193
1
    else if (line[2] == '2')
14194
0
      {
14195
0
        i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF2;
14196
0
        line += 3;
14197
0
      }
14198
1
    break;
14199
22
  }
14200
22
    }
14201
14202
5.01k
  if (line > end && !xop && *line == '.')
14203
22
    {
14204
      /* Encoding space (VEX.mmmmm, EVEX.mmmm).  */
14205
22
      switch (line[1])
14206
22
  {
14207
0
  case '0':
14208
0
    if (TOUPPER (line[2]) != 'F')
14209
0
      break;
14210
0
    if (line[3] == '.' || is_whitespace (line[3]))
14211
0
      {
14212
0
        i.insn_opcode_space = SPACE_0F;
14213
0
        line += 3;
14214
0
      }
14215
0
    else if (line[3] == '3'
14216
0
       && (line[4] == '8' || TOUPPER (line[4]) == 'A')
14217
0
       && (line[5] == '.' || is_whitespace (line[5])))
14218
0
      {
14219
0
        i.insn_opcode_space = line[4] == '8' ? SPACE_0F38 : SPACE_0F3A;
14220
0
        line += 5;
14221
0
      }
14222
0
    break;
14223
14224
2
  case 'M':
14225
2
    if (ISDIGIT (line[2]) && line[2] != '0')
14226
2
      {
14227
2
        char *e;
14228
2
        unsigned long n = strtoul (line + 2, &e, 10);
14229
14230
2
        if (n <= (evex ? 15 : 31)
14231
0
      && (*e == '.' || is_whitespace (*e)))
14232
0
    {
14233
0
      i.insn_opcode_space = n;
14234
0
      line = e;
14235
0
    }
14236
2
      }
14237
2
    break;
14238
22
  }
14239
22
    }
14240
14241
5.01k
  if (line > end && *line == '.' && line[1] == 'W')
14242
19
    {
14243
      /* VEX.W, XOP.W, EVEX.W  */
14244
19
      switch (line[2])
14245
19
  {
14246
0
  case '0':
14247
0
    i.tm.opcode_modifier.vexw = VEXW0;
14248
0
    break;
14249
14250
19
  case '1':
14251
19
    i.tm.opcode_modifier.vexw = VEXW1;
14252
19
    break;
14253
14254
0
  case 'I':
14255
0
    if (line[3] == 'G')
14256
0
      {
14257
0
        i.tm.opcode_modifier.vexw = VEXWIG;
14258
0
        ++line;
14259
0
      }
14260
0
    break;
14261
19
  }
14262
14263
19
      if (i.tm.opcode_modifier.vexw)
14264
19
  line += 3;
14265
19
    }
14266
14267
5.01k
  if (line > end && evex && *line == '.')
14268
0
    {
14269
0
      if (line[1] == 'N' && line[2] == 'D')
14270
0
  {
14271
0
    evex = evex_nd;
14272
0
    line += 3;
14273
0
  }
14274
0
      else if (line[1] == 'Z' && line[2] == 'U')
14275
0
  {
14276
0
    i.tm.opcode_modifier.operandconstraint = ZERO_UPPER;
14277
0
    line += 3;
14278
0
  }
14279
0
    }
14280
14281
5.01k
  if (line > end && *line && !is_whitespace (*line))
14282
4
    {
14283
      /* Improve diagnostic a little.  */
14284
4
      if (*line == '.' && line[1] && !is_whitespace (line[1]))
14285
3
  ++line;
14286
4
      goto done;
14287
4
    }
14288
14289
  /* Before processing the opcode expression, find trailing "+r" or
14290
     "/<digit>" specifiers.  */
14291
5.01k
  for (ptr = line; ; ++ptr)
14292
6.50k
    {
14293
6.50k
      unsigned long n;
14294
6.50k
      char *e;
14295
14296
6.50k
      ptr = strpbrk (ptr, "+/,");
14297
6.50k
      if (ptr == NULL || *ptr == ',')
14298
4.48k
  break;
14299
14300
2.02k
      if (*ptr == '+' && ptr[1] == 'r'
14301
0
    && (ptr[2] == ',' || (is_whitespace (ptr[2]) && ptr[3] == ',')))
14302
0
  {
14303
0
    *ptr = ' ';
14304
0
    ptr[1] = ' ';
14305
0
    i.short_form = true;
14306
0
    break;
14307
0
  }
14308
14309
2.02k
      if (*ptr == '/' && ISDIGIT (ptr[1])
14310
534
    && (n = strtoul (ptr + 1, &e, 8)) < 8
14311
531
    && e == ptr + 2
14312
531
    && (ptr[2] == ',' || (is_whitespace (ptr[2]) && ptr[3] == ',')))
14313
531
  {
14314
531
    *ptr = ' ';
14315
531
    ptr[1] = ' ';
14316
531
    i.tm.extension_opcode = n;
14317
531
    i.tm.opcode_modifier.modrm = 1;
14318
531
    break;
14319
531
  }
14320
2.02k
    }
14321
14322
5.01k
  input_line_pointer = line;
14323
5.01k
  val = get_absolute_expression ();
14324
5.01k
  line = input_line_pointer;
14325
14326
5.01k
  if (i.short_form && (val & 7))
14327
0
    as_warn ("`+r' assumes low three opcode bits to be clear");
14328
14329
5.01k
  for (j = 1; j < sizeof(val); ++j)
14330
5.01k
    if (!(val >> (j * 8)))
14331
5.01k
      break;
14332
14333
  /* Trim off a prefix if present.  */
14334
5.01k
  if (j > 1 && !vex && !xop && !evex)
14335
1
    {
14336
1
      uint8_t byte = val >> ((j - 1) * 8);
14337
14338
1
      switch (byte)
14339
1
  {
14340
0
  case DATA_PREFIX_OPCODE:
14341
0
  case REPE_PREFIX_OPCODE:
14342
0
  case REPNE_PREFIX_OPCODE:
14343
0
    if (!add_prefix (byte))
14344
0
      goto bad;
14345
0
    val &= ((uint64_t)1 << (--j * 8)) - 1;
14346
0
    break;
14347
1
  }
14348
1
    }
14349
14350
5.01k
  if (evex == evex_basic && *line == '{')
14351
0
    {
14352
0
      int length = check_Scc_OszcOperations (line);
14353
14354
0
      if (length > 0)
14355
0
  {
14356
0
    line += length;
14357
0
    if (is_whitespace (*line))
14358
0
      ++line;
14359
14360
0
    if (i.tm.opcode_modifier.operandconstraint)
14361
0
      {
14362
0
        as_bad (_("SCC/OSZC specifier cannot be used here"));
14363
0
        goto bad;
14364
0
      }
14365
0
    i.tm.opcode_modifier.operandconstraint = SCC;
14366
0
  }
14367
0
    }
14368
14369
  /* Parse operands, if any, before evaluating encoding space.  */
14370
5.01k
  if (*line == ',')
14371
3.95k
    {
14372
3.95k
      i.memshift = -1;
14373
14374
3.95k
      ptr = parse_operands (line + 1, &i386_mnemonics[MN__insn]);
14375
3.95k
      this_operand = -1;
14376
3.95k
      if (!ptr)
14377
3.39k
  goto bad;
14378
561
      line = ptr;
14379
14380
561
      if (!i.operands)
14381
0
  {
14382
0
    as_bad (_("expecting operand after ','; got nothing"));
14383
0
    goto done;
14384
0
  }
14385
14386
561
      if (i.mem_operands > 1)
14387
0
  {
14388
0
    as_bad (_("too many memory references for `%s'"),
14389
0
      &i386_mnemonics[MN__insn]);
14390
0
    goto done;
14391
0
  }
14392
14393
      /* No need to distinguish encoding_evex and encoding_evex512.  */
14394
561
      if (pp.encoding == encoding_evex512)
14395
118
  pp.encoding = encoding_evex;
14396
561
    }
14397
14398
  /* Trim off encoding space.  */
14399
1.61k
  if (j > 1 && !i.insn_opcode_space && (val >> ((j - 1) * 8)) == 0x0f)
14400
0
    {
14401
0
      uint8_t byte = val >> ((--j - 1) * 8);
14402
14403
0
      i.insn_opcode_space = SPACE_0F;
14404
0
      switch (byte & -(j > 1 && !pp.rex2_encoding
14405
0
           && (pp.encoding != encoding_egpr || evex)))
14406
0
  {
14407
0
  case 0x38:
14408
0
    i.insn_opcode_space = SPACE_0F38;
14409
0
    --j;
14410
0
    break;
14411
0
  case 0x3a:
14412
0
    i.insn_opcode_space = SPACE_0F3A;
14413
0
    --j;
14414
0
    break;
14415
0
  }
14416
0
      i.tm.opcode_space = i.insn_opcode_space;
14417
0
      val &= ((uint64_t)1 << (j * 8)) - 1;
14418
0
    }
14419
1.61k
  if (!i.tm.opcode_space && (vex || evex))
14420
    /* Arrange for build_vex_prefix() to properly emit 0xC4/0xC5.
14421
       Also avoid hitting abort() there or in build_evex_prefix().  */
14422
638
    i.tm.opcode_space = i.insn_opcode_space == SPACE_0F ? SPACE_0F
14423
638
               : SPACE_0F38;
14424
14425
1.61k
  if (j > 2)
14426
0
    {
14427
0
      as_bad (_("opcode residual (%#"PRIx64") too wide"), (uint64_t) val);
14428
0
      goto done;
14429
0
    }
14430
1.61k
  i.opcode_length = j;
14431
14432
  /* Handle operands, if any.  */
14433
1.61k
  if (i.operands)
14434
561
    {
14435
561
      i386_operand_type combined;
14436
561
      expressionS *disp_exp = NULL;
14437
561
      bool changed;
14438
14439
561
      if (pp.encoding == encoding_egpr)
14440
0
  {
14441
0
    if (vex || xop)
14442
0
      {
14443
0
        as_bad (_("eGPR use conflicts with encoding specifier"));
14444
0
        goto done;
14445
0
      }
14446
0
    if (evex)
14447
0
      pp.encoding = encoding_evex;
14448
0
    else
14449
0
      pp.encoding = encoding_default;
14450
0
  }
14451
14452
      /* Are we to emit ModR/M encoding?  */
14453
561
      if (!i.short_form
14454
561
    && (i.mem_operands
14455
142
        || i.reg_operands > (pp.encoding != encoding_default)
14456
140
        || i.tm.extension_opcode != None))
14457
421
  i.tm.opcode_modifier.modrm = 1;
14458
14459
561
      if (!i.tm.opcode_modifier.modrm
14460
140
    && (i.reg_operands
14461
140
        > i.short_form + 0U + (pp.encoding != encoding_default)
14462
140
        || i.mem_operands))
14463
0
  {
14464
0
    as_bad (_("too many register/memory operands"));
14465
0
    goto done;
14466
0
  }
14467
14468
      /* Enforce certain constraints on operands.  */
14469
561
      switch (i.reg_operands + i.mem_operands
14470
561
        + (i.tm.extension_opcode != None)
14471
561
        + (i.tm.opcode_modifier.operandconstraint == SCC))
14472
561
  {
14473
119
  case 0:
14474
119
    if (i.short_form)
14475
0
      {
14476
0
        as_bad (_("too few register/memory operands"));
14477
0
        goto done;
14478
0
      }
14479
    /* Fall through.  */
14480
140
  case 1:
14481
140
    if (i.tm.opcode_modifier.modrm)
14482
0
      {
14483
0
        as_bad (_("too few register/memory operands"));
14484
0
        goto done;
14485
0
      }
14486
    /* Fall through.  */
14487
524
  case 2:
14488
524
    if (evex == evex_nd)
14489
0
      {
14490
0
        as_bad (_("too few register/memory operands"));
14491
0
        goto done;
14492
0
      }
14493
524
    break;
14494
14495
524
  case 4:
14496
1
    if (i.imm_operands
14497
1
        && (i.op[0].imms->X_op != O_constant
14498
1
      || !fits_in_imm4 (i.op[0].imms->X_add_number)))
14499
0
      {
14500
0
        as_bad (_("constant doesn't fit in %d bits"), evex ? 3 : 4);
14501
0
        goto done;
14502
0
      }
14503
    /* Fall through.  */
14504
37
  case 3:
14505
37
    if (i.tm.opcode_modifier.operandconstraint == SCC)
14506
0
      break;
14507
37
    if (pp.encoding != encoding_default)
14508
37
      {
14509
37
        i.tm.opcode_modifier.vexvvvv = (i.tm.extension_opcode == None
14510
13
                && evex != evex_nd)
14511
37
               ? VexVVVV_SRC1 : VexVVVV_DST;
14512
37
        break;
14513
37
      }
14514
    /* Fall through.  */
14515
0
  default:
14516
0
    as_bad (_("too many register/memory operands"));
14517
0
    goto done;
14518
561
  }
14519
14520
      /* Bring operands into canonical order (imm, mem, reg).  */
14521
561
      do
14522
616
  {
14523
616
    changed = false;
14524
14525
844
    for (j = 1; j < i.operands; ++j)
14526
228
      {
14527
228
        if ((!operand_type_check (i.types[j - 1], imm)
14528
167
       && operand_type_check (i.types[j], imm))
14529
202
      || (i.types[j - 1].bitfield.class != ClassNone
14530
97
          && i.types[j].bitfield.class == ClassNone))
14531
81
    {
14532
81
      swap_2_operands (j - 1, j);
14533
81
      changed = true;
14534
81
    }
14535
228
      }
14536
616
  }
14537
616
      while (changed);
14538
14539
      /* For Intel syntax swap the order of register operands.  */
14540
561
      if (intel_syntax)
14541
535
  switch (i.reg_operands)
14542
535
    {
14543
494
    case 0:
14544
519
    case 1:
14545
519
      break;
14546
14547
0
    case 4:
14548
0
      swap_2_operands (i.imm_operands + i.mem_operands + 1, i.operands - 2);
14549
      /* Fall through.  */
14550
0
    case 3:
14551
16
    case 2:
14552
16
      swap_2_operands (i.imm_operands + i.mem_operands, i.operands - 1);
14553
16
      break;
14554
14555
0
    default:
14556
0
      abort ();
14557
535
    }
14558
14559
      /* Enforce constraints when using VSIB.  */
14560
561
      if (i.index_reg
14561
0
    && (i.index_reg->reg_type.bitfield.xmmword
14562
0
        || i.index_reg->reg_type.bitfield.ymmword
14563
0
        || i.index_reg->reg_type.bitfield.zmmword))
14564
0
  {
14565
0
    if (pp.encoding == encoding_default)
14566
0
      {
14567
0
        as_bad (_("VSIB unavailable with legacy encoding"));
14568
0
        goto done;
14569
0
      }
14570
14571
0
    if (pp.encoding == encoding_evex
14572
0
        && i.reg_operands > 1)
14573
0
      {
14574
        /* We could allow two register operands, encoding the 2nd one in
14575
     an 8-bit immediate like for 4-register-operand insns, but that
14576
     would require ugly fiddling with process_operands() and/or
14577
     build_modrm_byte().  */
14578
0
        as_bad (_("too many register operands with VSIB"));
14579
0
        goto done;
14580
0
      }
14581
14582
0
    i.tm.opcode_modifier.sib = 1;
14583
0
  }
14584
14585
      /* Establish operand size encoding.  */
14586
561
      operand_type_set (&combined, 0);
14587
14588
1.06k
      for (j = i.imm_operands; j < i.operands; ++j)
14589
502
  {
14590
    /* Look for 8-bit operands that use old registers.  */
14591
502
    if (pp.encoding != encoding_default
14592
496
        && flag_code == CODE_64BIT
14593
496
        && i.types[j].bitfield.class == Reg
14594
33
        && i.types[j].bitfield.byte
14595
8
        && !(i.op[j].regs->reg_flags & (RegRex | RegRex2 | RegRex64))
14596
0
        && i.op[j].regs->reg_num > 3)
14597
0
      as_bad (_("can't encode register '%s%s' with VEX/XOP/EVEX"),
14598
0
        register_prefix, i.op[j].regs->reg_name);
14599
14600
502
    i.types[j].bitfield.instance = InstanceNone;
14601
14602
502
    if (operand_type_check (i.types[j], disp))
14603
419
      {
14604
419
        i.types[j].bitfield.baseindex = 1;
14605
419
        disp_exp = i.op[j].disps;
14606
419
      }
14607
14608
502
    if (evex && i.types[j].bitfield.baseindex)
14609
36
      {
14610
36
        unsigned int n = i.memshift;
14611
14612
36
        if (i.types[j].bitfield.byte)
14613
0
    n = 0;
14614
36
        else if (i.types[j].bitfield.word)
14615
0
    n = 1;
14616
36
        else if (i.types[j].bitfield.dword)
14617
0
    n = 2;
14618
36
        else if (i.types[j].bitfield.qword)
14619
0
    n = 3;
14620
36
        else if (i.types[j].bitfield.xmmword)
14621
0
    n = 4;
14622
36
        else if (i.types[j].bitfield.ymmword)
14623
0
    n = 5;
14624
36
        else if (i.types[j].bitfield.zmmword)
14625
0
    n = 6;
14626
14627
36
        if (i.memshift < 32 && n != i.memshift)
14628
0
    as_warn ("conflicting memory operand size specifiers");
14629
36
        i.memshift = n;
14630
36
      }
14631
14632
502
    if ((i.broadcast.type || i.broadcast.bytes)
14633
39
        && j == i.broadcast.operand)
14634
0
      continue;
14635
14636
502
    combined = operand_type_or (combined, i.types[j]);
14637
502
    combined.bitfield.class = ClassNone;
14638
502
  }
14639
14640
561
      switch ((i.broadcast.type ? i.broadcast.type : 1)
14641
561
        << (i.memshift < 32 ? i.memshift : 0))
14642
561
  {
14643
0
  case 64: combined.bitfield.zmmword = 1; break;
14644
0
  case 32: combined.bitfield.ymmword = 1; break;
14645
0
  case 16: combined.bitfield.xmmword = 1; break;
14646
0
  case  8: combined.bitfield.qword = 1; break;
14647
0
  case  4: combined.bitfield.dword = 1; break;
14648
561
  }
14649
14650
561
      if (pp.encoding == encoding_default)
14651
4
  {
14652
4
    if (flag_code == CODE_64BIT && combined.bitfield.qword)
14653
0
      i.rex |= REX_W;
14654
4
    else if ((flag_code == CODE_16BIT ? combined.bitfield.dword
14655
4
              : combined.bitfield.word)
14656
3
             && !add_prefix (DATA_PREFIX_OPCODE))
14657
0
      goto done;
14658
4
  }
14659
557
      else if (!i.tm.opcode_modifier.vexw)
14660
557
  {
14661
557
    if (flag_code == CODE_64BIT)
14662
557
      {
14663
557
        if (combined.bitfield.qword)
14664
0
          i.tm.opcode_modifier.vexw = VEXW1;
14665
557
        else if (combined.bitfield.dword)
14666
24
          i.tm.opcode_modifier.vexw = VEXW0;
14667
557
      }
14668
14669
557
    if (!i.tm.opcode_modifier.vexw)
14670
533
      i.tm.opcode_modifier.vexw = VEXWIG;
14671
557
  }
14672
14673
561
      if (vex || xop)
14674
394
  {
14675
394
    if (!i.tm.opcode_modifier.vex)
14676
382
      {
14677
382
        if (combined.bitfield.ymmword)
14678
4
          i.tm.opcode_modifier.vex = VEX256;
14679
378
        else if (combined.bitfield.xmmword)
14680
3
          i.tm.opcode_modifier.vex = VEX128;
14681
382
      }
14682
394
  }
14683
167
      else if (evex)
14684
44
  {
14685
44
    if (!i.tm.opcode_modifier.evex)
14686
36
      {
14687
        /* Do _not_ consider AVX512VL here.  */
14688
36
        if (combined.bitfield.zmmword)
14689
0
          i.tm.opcode_modifier.evex = EVEX512;
14690
36
        else if (combined.bitfield.ymmword)
14691
0
          i.tm.opcode_modifier.evex = EVEX256;
14692
36
        else if (combined.bitfield.xmmword)
14693
12
          i.tm.opcode_modifier.evex = EVEX128;
14694
36
      }
14695
14696
44
    if (i.memshift >= 32)
14697
44
      {
14698
44
        unsigned int n = 0;
14699
14700
44
        switch (i.tm.opcode_modifier.evex)
14701
44
    {
14702
0
    case EVEX512: n = 64; break;
14703
8
    case EVEX256: n = 32; break;
14704
12
    case EVEX128: n = 16; break;
14705
44
    }
14706
14707
44
        if (i.broadcast.type)
14708
12
    n /= i.broadcast.type;
14709
14710
44
        if (n > 0)
14711
96
    for (i.memshift = 0; !(n & 1); n >>= 1)
14712
76
      ++i.memshift;
14713
24
        else if (disp_exp != NULL && disp_exp->X_op == O_constant
14714
0
           && disp_exp->X_add_number != 0
14715
0
           && pp.disp_encoding != disp_encoding_32bit)
14716
0
    {
14717
0
      if (!quiet_warnings)
14718
0
        as_warn ("cannot determine memory operand size");
14719
0
      pp.disp_encoding = disp_encoding_32bit;
14720
0
    }
14721
44
      }
14722
44
  }
14723
14724
561
      if (i.memshift >= 32)
14725
541
  i.memshift = 0;
14726
20
      else if (!evex)
14727
0
  pp.encoding = encoding_error;
14728
14729
561
      if (i.disp_operands && !optimize_disp (&i.tm))
14730
0
  goto done;
14731
14732
      /* Establish size for immediate operands.  */
14733
719
      for (j = 0; j < i.imm_operands; ++j)
14734
158
  {
14735
158
    expressionS *expP = i.op[j].imms;
14736
14737
158
    gas_assert (operand_type_check (i.types[j], imm));
14738
158
    operand_type_set (&i.types[j], 0);
14739
14740
158
    if (i.imm_bits[j] > 32)
14741
0
      i.types[j].bitfield.imm64 = 1;
14742
158
    else if (i.imm_bits[j] > 16)
14743
0
      {
14744
0
        if (flag_code == CODE_64BIT && (i.flags[j] & Operand_Signed))
14745
0
    i.types[j].bitfield.imm32s = 1;
14746
0
        else
14747
0
    i.types[j].bitfield.imm32 = 1;
14748
0
      }
14749
158
    else if (i.imm_bits[j] > 8)
14750
0
      i.types[j].bitfield.imm16 = 1;
14751
158
    else if (i.imm_bits[j] > 0)
14752
0
      {
14753
0
        if (i.flags[j] & Operand_Signed)
14754
0
    i.types[j].bitfield.imm8s = 1;
14755
0
        else
14756
0
    i.types[j].bitfield.imm8 = 1;
14757
0
      }
14758
158
    else if (expP->X_op == O_constant)
14759
143
      {
14760
143
        i.types[j] = smallest_imm_type (expP->X_add_number);
14761
143
        i.types[j].bitfield.imm1 = 0;
14762
        /* Oddly enough imm_size() checks imm64 first, so the bit needs
14763
     zapping since smallest_imm_type() sets it unconditionally.  */
14764
143
        if (flag_code != CODE_64BIT)
14765
0
    {
14766
0
      i.types[j].bitfield.imm64 = 0;
14767
0
      i.types[j].bitfield.imm32s = 0;
14768
0
      i.types[j].bitfield.imm32 = 1;
14769
0
    }
14770
143
        else if (i.types[j].bitfield.imm32 || i.types[j].bitfield.imm32s)
14771
143
    i.types[j].bitfield.imm64 = 0;
14772
143
      }
14773
15
    else
14774
      /* Non-constant expressions are sized heuristically.  */
14775
15
      switch (flag_code)
14776
15
        {
14777
15
        case CODE_64BIT: i.types[j].bitfield.imm32s = 1; break;
14778
0
        case CODE_32BIT: i.types[j].bitfield.imm32 = 1; break;
14779
0
        case CODE_16BIT: i.types[j].bitfield.imm16 = 1; break;
14780
15
        }
14781
158
  }
14782
14783
1.22k
      for (j = 0; j < i.operands; ++j)
14784
660
  i.tm_types[j] = i.types[j];
14785
14786
561
      process_operands ();
14787
561
    }
14788
14789
  /* Don't set opcode until after processing operands, to avoid any
14790
     potential special casing there.  */
14791
1.61k
  i.tm.base_opcode |= val;
14792
14793
1.61k
  if (pp.encoding == encoding_error
14794
1.61k
      || (pp.encoding != encoding_evex
14795
1.61k
    ? i.broadcast.type || i.broadcast.bytes
14796
1.42k
      || i.rounding.type != rc_none
14797
1.42k
      || i.mask.reg
14798
1.61k
    : (i.mem_operands && i.rounding.type != rc_none)
14799
182
      || ((i.broadcast.type || i.broadcast.bytes)
14800
13
    && !(i.flags[i.broadcast.operand] & Operand_Mem))))
14801
16
    {
14802
16
      as_bad (_("conflicting .insn operands"));
14803
16
      goto done;
14804
16
    }
14805
14806
1.59k
  if (vex || xop)
14807
572
    {
14808
572
      if (is_apx_evex_encoding ())
14809
0
  {
14810
0
    as_bad (_("APX functionality cannot be used with %s encodings"),
14811
0
      vex ? "VEX" : "XOP");
14812
0
    goto done;
14813
0
  }
14814
14815
572
      if (!i.tm.opcode_modifier.vex)
14816
556
  i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
14817
14818
572
      build_vex_prefix (NULL);
14819
572
      i.rex &= REX_OPCODE;
14820
572
    }
14821
1.02k
  else if (evex)
14822
51
    {
14823
51
      if (!i.tm.opcode_modifier.evex)
14824
43
  i.tm.opcode_modifier.evex = EVEXLIG;
14825
14826
      /* To keep earlier .insn uses working as far as possible, take the
14827
   legacy path when opcode space is 4 bits wide (impossible to encode in
14828
   extended EVEX), and when no "extended" syntax elements are used.  */
14829
51
      if ((!is_apx_evex_encoding () || i.insn_opcode_space > 7)
14830
51
    && evex == evex_basic
14831
51
    && !i.tm.opcode_modifier.operandconstraint)
14832
51
  build_evex_prefix ();
14833
0
      else if (i.insn_opcode_space > 7)
14834
0
  {
14835
0
    as_bad (_("opcode space cannot be larger than 7"));
14836
0
    goto done;
14837
0
  }
14838
0
      else if (evex == evex_nd && (i.broadcast.type || i.broadcast.bytes))
14839
0
  {
14840
0
    as_bad (_("ND and broadcast cannot be used at the same time"));
14841
0
    goto done;
14842
0
  }
14843
0
      else if (pp.has_nf && i.mask.reg)
14844
0
  {
14845
0
    as_bad (_("{nf} and masking cannot be used at the same time"));
14846
0
    goto done;
14847
0
  }
14848
0
      else if (i.tm.opcode_modifier.operandconstraint == SCC
14849
0
         && (pp.has_nf || i.mask.reg))
14850
0
  {
14851
0
    as_bad (_("SCC cannot be used at the same time {nf} / masking"));
14852
0
    goto done;
14853
0
  }
14854
0
      else if (!build_apx_evex_prefix (evex == evex_nd))
14855
0
  goto done;
14856
51
      i.rex &= REX_OPCODE;
14857
51
    }
14858
974
  else
14859
974
    establish_rex ();
14860
14861
1.59k
  last_insn = &seg_info(now_seg)->tc_segment_info_data.last_insn;
14862
1.59k
  output_insn (last_insn);
14863
1.59k
  last_insn->kind = last_insn_directive;
14864
1.59k
  last_insn->name = ".insn directive";
14865
1.59k
  last_insn->file = as_where (&last_insn->line);
14866
14867
1.59k
#ifdef OBJ_ELF
14868
  /* PS: SCFI is enabled only for System V AMD64 ABI.  The ABI check has been
14869
     performed in i386_target_format.  */
14870
1.59k
  if (flag_synth_cfi)
14871
0
    as_bad (_("SCFI: hand-crafting instructions not supported"));
14872
1.59k
#endif
14873
14874
1.61k
 done:
14875
1.61k
  *saved_ilp = saved_char;
14876
1.61k
  input_line_pointer = line;
14877
14878
1.61k
  demand_empty_rest_of_line ();
14879
14880
  /* Make sure dot_insn() won't yield "true" anymore.  */
14881
1.61k
  i.tm.mnem_off = 0;
14882
14883
1.61k
  current_templates.start = NULL;
14884
1.61k
  memset (&pp, 0, sizeof (pp));
14885
1.61k
}
14886
14887
#ifdef TE_PE
14888
static void
14889
pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
14890
{
14891
  expressionS exp;
14892
14893
  do
14894
    {
14895
      expression (&exp);
14896
      if (exp.X_op == O_symbol)
14897
  exp.X_op = O_secrel;
14898
14899
      emit_expr (&exp, 4);
14900
    }
14901
  while (*input_line_pointer++ == ',');
14902
14903
  input_line_pointer--;
14904
  demand_empty_rest_of_line ();
14905
}
14906
14907
static void
14908
pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
14909
{
14910
  expressionS exp;
14911
14912
  do
14913
    {
14914
      expression (&exp);
14915
      if (exp.X_op == O_symbol)
14916
  exp.X_op = O_secidx;
14917
14918
      emit_expr (&exp, 2);
14919
    }
14920
  while (*input_line_pointer++ == ',');
14921
14922
  input_line_pointer--;
14923
  demand_empty_rest_of_line ();
14924
}
14925
#endif
14926
14927
/* Handle Rounding Control / SAE specifiers.  */
14928
14929
static char *
14930
RC_SAE_specifier (const char *pstr)
14931
1.27k
{
14932
1.27k
  unsigned int j;
14933
14934
7.64k
  for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
14935
6.37k
    {
14936
6.37k
      if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
14937
5
  {
14938
5
    if (i.rounding.type != rc_none)
14939
0
      {
14940
0
        as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
14941
0
        return NULL;
14942
0
      }
14943
14944
5
    switch (pp.encoding)
14945
5
      {
14946
5
      case encoding_default:
14947
5
      case encoding_egpr:
14948
5
        pp.encoding = encoding_evex512;
14949
5
        break;
14950
0
      case encoding_evex:
14951
0
      case encoding_evex512:
14952
0
        break;
14953
0
      default:
14954
0
        return NULL;
14955
5
      }
14956
14957
5
    i.rounding.type = RC_NamesTable[j].type;
14958
14959
5
    return (char *)(pstr + RC_NamesTable[j].len);
14960
5
  }
14961
6.37k
    }
14962
14963
1.27k
  return NULL;
14964
1.27k
}
14965
14966
/* Handle Vector operations.  */
14967
14968
static char *
14969
check_VecOperations (char *op_string)
14970
4.68k
{
14971
4.68k
  const reg_entry *mask;
14972
4.68k
  const char *saved;
14973
4.68k
  char *end_op;
14974
14975
4.87k
  while (*op_string)
14976
4.69k
    {
14977
4.69k
      saved = op_string;
14978
4.69k
      if (*op_string == '{')
14979
4.69k
  {
14980
4.69k
    op_string++;
14981
4.69k
    if (is_whitespace (*op_string))
14982
12
      op_string++;
14983
14984
    /* Check broadcasts.  */
14985
4.69k
    if (startswith (op_string, "1to"))
14986
202
      {
14987
202
        unsigned int bcst_type;
14988
14989
202
        if (i.broadcast.type)
14990
0
    goto duplicated_vec_op;
14991
14992
202
        op_string += 3;
14993
202
        if (*op_string == '8')
14994
0
    bcst_type = 8;
14995
202
        else if (*op_string == '4')
14996
0
    bcst_type = 4;
14997
202
        else if (*op_string == '2')
14998
181
    bcst_type = 2;
14999
21
        else if (*op_string == '1'
15000
20
           && *(op_string+1) == '6')
15001
0
    {
15002
0
      bcst_type = 16;
15003
0
      op_string++;
15004
0
    }
15005
21
        else if (*op_string == '3'
15006
1
           && *(op_string+1) == '2')
15007
0
    {
15008
0
      bcst_type = 32;
15009
0
      op_string++;
15010
0
    }
15011
21
        else
15012
21
    {
15013
21
      as_bad (_("Unsupported broadcast: `%s'"), saved);
15014
21
      return NULL;
15015
21
    }
15016
181
        op_string++;
15017
15018
181
        switch (pp.encoding)
15019
181
    {
15020
166
    case encoding_default:
15021
166
    case encoding_egpr:
15022
166
      pp.encoding = encoding_evex;
15023
166
      break;
15024
15
    case encoding_evex:
15025
15
    case encoding_evex512:
15026
15
      break;
15027
0
    default:
15028
0
      goto unknown_vec_op;
15029
181
    }
15030
15031
181
        i.broadcast.type = bcst_type;
15032
181
        i.broadcast.operand = this_operand;
15033
15034
        /* For .insn a data size specifier may be appended.  */
15035
181
        if (dot_insn () && *op_string == ':')
15036
0
    goto dot_insn_modifier;
15037
181
      }
15038
    /* Check .insn special cases.  */
15039
4.48k
    else if (dot_insn () && *op_string == ':')
15040
12
      {
15041
12
      dot_insn_modifier:
15042
12
        switch (op_string[1])
15043
12
    {
15044
0
      unsigned long n;
15045
15046
0
    case 'd':
15047
0
      if (i.memshift < 32)
15048
0
        goto duplicated_vec_op;
15049
15050
0
      n = strtoul (op_string + 2, &end_op, 0);
15051
0
      if (n)
15052
0
        for (i.memshift = 0; !(n & 1); n >>= 1)
15053
0
          ++i.memshift;
15054
0
      if (i.memshift < 32 && n == 1)
15055
0
        op_string = end_op;
15056
0
      break;
15057
15058
12
    case 's': case 'u':
15059
      /* This isn't really a "vector" operation, but a sign/size
15060
         specifier for immediate operands of .insn.  Note that AT&T
15061
         syntax handles the same in i386_immediate().  */
15062
12
      if (!intel_syntax)
15063
0
        break;
15064
15065
12
      if (i.imm_bits[this_operand])
15066
0
        goto duplicated_vec_op;
15067
15068
12
      n = strtoul (op_string + 2, &end_op, 0);
15069
12
      if (n && n <= (flag_code == CODE_64BIT ? 64 : 32))
15070
6
        {
15071
6
          i.imm_bits[this_operand] = n;
15072
6
          if (op_string[1] == 's')
15073
0
      i.flags[this_operand] |= Operand_Signed;
15074
6
          op_string = end_op;
15075
6
        }
15076
12
      break;
15077
12
    }
15078
12
      }
15079
    /* Check masking operation.  */
15080
4.47k
    else if ((mask = parse_register (op_string, &end_op)) != NULL)
15081
3.35k
      {
15082
3.35k
        if (mask == &bad_reg)
15083
0
    return NULL;
15084
15085
        /* k0 can't be used for write mask.  */
15086
3.35k
        if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
15087
3.35k
    {
15088
3.35k
      as_bad (_("`%s%s' can't be used for write mask"),
15089
3.35k
        register_prefix, mask->reg_name);
15090
3.35k
      return NULL;
15091
3.35k
    }
15092
15093
0
        if (!i.mask.reg)
15094
0
    {
15095
0
      i.mask.reg = mask;
15096
0
      i.mask.operand = this_operand;
15097
0
    }
15098
0
        else if (i.mask.reg->reg_num)
15099
0
    goto duplicated_vec_op;
15100
0
        else
15101
0
    {
15102
0
      i.mask.reg = mask;
15103
15104
      /* Only "{z}" is allowed here.  No need to check
15105
         zeroing mask explicitly.  */
15106
0
      if (i.mask.operand != (unsigned int) this_operand)
15107
0
        {
15108
0
          as_bad (_("invalid write mask `%s'"), saved);
15109
0
          return NULL;
15110
0
        }
15111
0
    }
15112
15113
0
        op_string = end_op;
15114
0
      }
15115
    /* Check zeroing-flag for masking operation.  */
15116
1.12k
    else if (*op_string == 'z')
15117
3
      {
15118
3
        if (!i.mask.reg)
15119
2
    {
15120
2
      i.mask.reg = reg_k0;
15121
2
      i.mask.zeroing = 1;
15122
2
      i.mask.operand = this_operand;
15123
2
    }
15124
1
        else
15125
1
    {
15126
1
      if (i.mask.zeroing)
15127
1
        {
15128
1
        duplicated_vec_op:
15129
1
          as_bad (_("duplicated `%s'"), saved);
15130
1
          return NULL;
15131
1
        }
15132
15133
0
      i.mask.zeroing = 1;
15134
15135
      /* Only "{%k}" is allowed here.  No need to check mask
15136
         register explicitly.  */
15137
0
      if (i.mask.operand != (unsigned int) this_operand)
15138
0
        {
15139
0
          as_bad (_("invalid zeroing-masking `%s'"),
15140
0
            saved);
15141
0
          return NULL;
15142
0
        }
15143
0
    }
15144
15145
2
        op_string++;
15146
2
      }
15147
1.11k
    else if (intel_syntax
15148
1.11k
       && (op_string = RC_SAE_specifier (op_string)) != NULL)
15149
0
      i.rounding.modifier = true;
15150
1.11k
    else
15151
1.11k
      goto unknown_vec_op;
15152
15153
195
    if (is_whitespace (*op_string))
15154
0
      op_string++;
15155
195
    if (*op_string != '}')
15156
8
      {
15157
8
        as_bad (_("missing `}' in `%s'"), saved);
15158
8
        return NULL;
15159
8
      }
15160
187
    op_string++;
15161
15162
187
    if (is_whitespace (*op_string))
15163
0
      ++op_string;
15164
15165
187
    continue;
15166
195
  }
15167
1.11k
    unknown_vec_op:
15168
      /* We don't know this one.  */
15169
1.11k
      as_bad (_("unknown vector operation: `%s'"), saved);
15170
1.11k
      return NULL;
15171
4.69k
    }
15172
15173
186
  if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
15174
0
    {
15175
0
      as_bad (_("zeroing-masking only allowed with write mask"));
15176
0
      return NULL;
15177
0
    }
15178
15179
186
  return op_string;
15180
186
}
15181
15182
static int
15183
i386_immediate (char *imm_start)
15184
1.38k
{
15185
1.38k
  char *save_input_line_pointer;
15186
1.38k
  char *gotfree_input_line;
15187
1.38k
  segT exp_seg = 0;
15188
1.38k
  expressionS *exp;
15189
1.38k
  i386_operand_type types;
15190
15191
1.38k
  operand_type_set (&types, ~0);
15192
15193
1.38k
  if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
15194
0
    {
15195
0
      as_bad (_("at most %d immediate operands are allowed"),
15196
0
        MAX_IMMEDIATE_OPERANDS);
15197
0
      return 0;
15198
0
    }
15199
15200
1.38k
  exp = &im_expressions[i.imm_operands++];
15201
1.38k
  i.op[this_operand].imms = exp;
15202
15203
1.38k
  if (is_whitespace (*imm_start))
15204
0
    ++imm_start;
15205
15206
1.38k
  save_input_line_pointer = input_line_pointer;
15207
1.38k
  input_line_pointer = imm_start;
15208
15209
1.38k
  gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
15210
1.38k
  if (gotfree_input_line)
15211
0
    input_line_pointer = gotfree_input_line;
15212
15213
1.38k
  expr_mode = expr_operator_none;
15214
1.38k
  exp_seg = expression (exp);
15215
15216
  /* For .insn immediates there may be a size specifier.  */
15217
1.38k
  if (dot_insn () && *input_line_pointer == '{' && input_line_pointer[1] == ':'
15218
0
      && (input_line_pointer[2] == 's' || input_line_pointer[2] == 'u'))
15219
0
    {
15220
0
      char *e;
15221
0
      unsigned long n = strtoul (input_line_pointer + 3, &e, 0);
15222
15223
0
      if (*e == '}' && n && n <= (flag_code == CODE_64BIT ? 64 : 32))
15224
0
  {
15225
0
    i.imm_bits[this_operand] = n;
15226
0
    if (input_line_pointer[2] == 's')
15227
0
      i.flags[this_operand] |= Operand_Signed;
15228
0
    input_line_pointer = e + 1;
15229
0
  }
15230
0
    }
15231
15232
1.38k
  SKIP_WHITESPACE ();
15233
1.38k
  if (*input_line_pointer)
15234
1.34k
    as_bad (_("junk `%s' after expression"), input_line_pointer);
15235
15236
1.38k
  input_line_pointer = save_input_line_pointer;
15237
1.38k
  if (gotfree_input_line)
15238
0
    {
15239
0
      free (gotfree_input_line);
15240
15241
0
      if (exp->X_op == O_constant)
15242
0
  exp->X_op = O_illegal;
15243
0
    }
15244
15245
1.38k
  if (exp_seg == reg_section)
15246
0
    {
15247
0
      as_bad (_("illegal immediate register operand %s"), imm_start);
15248
0
      return 0;
15249
0
    }
15250
15251
1.38k
  return i386_finalize_immediate (exp_seg, exp, types, imm_start);
15252
1.38k
}
15253
15254
static int
15255
i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
15256
       i386_operand_type types, const char *imm_start)
15257
7.90k
{
15258
7.90k
  if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
15259
0
    {
15260
0
      if (imm_start)
15261
0
  as_bad (_("missing or invalid immediate expression `%s'"),
15262
0
    imm_start);
15263
0
      return 0;
15264
0
    }
15265
7.90k
  else if (exp->X_op == O_constant)
15266
4.14k
    {
15267
      /* Size it properly later.  */
15268
4.14k
      i.types[this_operand].bitfield.imm64 = 1;
15269
15270
      /* If not 64bit, sign/zero extend val, to account for wraparound
15271
   when !BFD64.  */
15272
4.14k
      if (expr_mode == expr_operator_present
15273
142
    && flag_code != CODE_64BIT && !object_64bit)
15274
0
  exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
15275
4.14k
    }
15276
#ifdef OBJ_AOUT
15277
  else if (exp_seg != absolute_section
15278
     && exp_seg != text_section
15279
     && exp_seg != data_section
15280
     && exp_seg != bss_section
15281
     && exp_seg != undefined_section
15282
     && !bfd_is_com_section (exp_seg))
15283
    {
15284
      as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
15285
      return 0;
15286
    }
15287
#endif
15288
3.76k
  else
15289
3.76k
    {
15290
      /* This is an address.  The size of the address will be
15291
   determined later, depending on destination register,
15292
   suffix, or the default for the section.  */
15293
3.76k
      i.types[this_operand].bitfield.imm8 = 1;
15294
3.76k
      i.types[this_operand].bitfield.imm16 = 1;
15295
3.76k
      i.types[this_operand].bitfield.imm32 = 1;
15296
3.76k
      i.types[this_operand].bitfield.imm32s = 1;
15297
3.76k
      i.types[this_operand].bitfield.imm64 = 1;
15298
3.76k
      i.types[this_operand] = operand_type_and (i.types[this_operand],
15299
3.76k
            types);
15300
3.76k
    }
15301
15302
7.90k
  return 1;
15303
7.90k
}
15304
15305
static char *
15306
i386_scale (char *scale)
15307
1
{
15308
1
  offsetT val;
15309
1
  char *save = input_line_pointer;
15310
15311
1
  input_line_pointer = scale;
15312
1
  val = get_absolute_expression ();
15313
15314
1
  switch (val)
15315
1
    {
15316
0
    case 1:
15317
0
      i.log2_scale_factor = 0;
15318
0
      break;
15319
0
    case 2:
15320
0
      i.log2_scale_factor = 1;
15321
0
      break;
15322
1
    case 4:
15323
1
      i.log2_scale_factor = 2;
15324
1
      break;
15325
0
    case 8:
15326
0
      i.log2_scale_factor = 3;
15327
0
      break;
15328
0
    default:
15329
0
      {
15330
0
  char sep = *input_line_pointer;
15331
15332
0
  *input_line_pointer = '\0';
15333
0
  as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
15334
0
    scale);
15335
0
  *input_line_pointer = sep;
15336
0
  input_line_pointer = save;
15337
0
  return NULL;
15338
0
      }
15339
1
    }
15340
1
  if (i.log2_scale_factor != 0 && i.index_reg == 0)
15341
0
    {
15342
0
      as_warn (_("scale factor of %d without an index register"),
15343
0
         1 << i.log2_scale_factor);
15344
0
      i.log2_scale_factor = 0;
15345
0
    }
15346
1
  scale = input_line_pointer;
15347
1
  input_line_pointer = save;
15348
1
  return scale;
15349
1
}
15350
15351
static int
15352
i386_displacement (char *disp_start, char *disp_end)
15353
238
{
15354
238
  expressionS *exp;
15355
238
  segT exp_seg = 0;
15356
238
  char *save_input_line_pointer;
15357
238
  char *gotfree_input_line;
15358
238
  int override;
15359
238
  i386_operand_type bigdisp, types = anydisp;
15360
238
  int ret;
15361
15362
238
  if (i.disp_operands == MAX_MEMORY_OPERANDS)
15363
0
    {
15364
0
      as_bad (_("at most %d displacement operands are allowed"),
15365
0
        MAX_MEMORY_OPERANDS);
15366
0
      return 0;
15367
0
    }
15368
15369
238
  operand_type_set (&bigdisp, 0);
15370
238
  if (i.jumpabsolute
15371
237
      || i.types[this_operand].bitfield.baseindex
15372
235
      || (current_templates.start->opcode_modifier.jump != JUMP
15373
234
    && current_templates.start->opcode_modifier.jump != JUMP_DWORD))
15374
236
    {
15375
236
      i386_addressing_mode ();
15376
236
      override = (i.prefix[ADDR_PREFIX] != 0);
15377
236
      if (flag_code == CODE_64BIT)
15378
215
  {
15379
215
    bigdisp.bitfield.disp32 = 1;
15380
215
    if (!override)
15381
213
      bigdisp.bitfield.disp64 = 1;
15382
215
  }
15383
21
      else if ((flag_code == CODE_16BIT) ^ override)
15384
21
    bigdisp.bitfield.disp16 = 1;
15385
0
      else
15386
0
    bigdisp.bitfield.disp32 = 1;
15387
236
    }
15388
2
  else
15389
2
    {
15390
      /* For PC-relative branches, the width of the displacement may be
15391
   dependent upon data size, but is never dependent upon address size.
15392
   Also make sure to not unintentionally match against a non-PC-relative
15393
   branch template.  */
15394
2
      const insn_template *t = current_templates.start;
15395
2
      bool has_intel64 = false;
15396
15397
5
      while (++t < current_templates.end)
15398
4
  {
15399
4
    if (t->opcode_modifier.jump
15400
4
        != current_templates.start->opcode_modifier.jump)
15401
1
      break;
15402
3
    if ((t->opcode_modifier.isa64 >= INTEL64))
15403
2
      has_intel64 = true;
15404
3
  }
15405
2
      current_templates.end = t;
15406
15407
2
      override = (i.prefix[DATA_PREFIX] != 0);
15408
2
      if (flag_code == CODE_64BIT)
15409
1
  {
15410
1
    if ((override || i.suffix == WORD_MNEM_SUFFIX)
15411
0
        && (!intel64 || !has_intel64))
15412
0
      bigdisp.bitfield.disp16 = 1;
15413
1
    else
15414
1
      bigdisp.bitfield.disp32 = 1;
15415
1
  }
15416
1
      else
15417
1
  {
15418
1
    if (!override)
15419
1
      override = (i.suffix == (flag_code != CODE_16BIT
15420
1
             ? WORD_MNEM_SUFFIX
15421
1
             : LONG_MNEM_SUFFIX));
15422
1
    bigdisp.bitfield.disp32 = 1;
15423
1
    if ((flag_code == CODE_16BIT) ^ override)
15424
0
      {
15425
0
        bigdisp.bitfield.disp32 = 0;
15426
0
        bigdisp.bitfield.disp16 = 1;
15427
0
      }
15428
1
  }
15429
2
    }
15430
238
  i.types[this_operand] = operand_type_or (i.types[this_operand],
15431
238
             bigdisp);
15432
15433
238
  exp = &disp_expressions[i.disp_operands];
15434
238
  i.op[this_operand].disps = exp;
15435
238
  i.disp_operands++;
15436
238
  save_input_line_pointer = input_line_pointer;
15437
238
  input_line_pointer = disp_start;
15438
238
  END_STRING_AND_SAVE (disp_end);
15439
15440
238
#ifndef GCC_ASM_O_HACK
15441
238
#define GCC_ASM_O_HACK 0
15442
238
#endif
15443
#if GCC_ASM_O_HACK
15444
  END_STRING_AND_SAVE (disp_end + 1);
15445
  if (i.types[this_operand].bitfield.baseIndex
15446
      && displacement_string_end[-1] == '+')
15447
    {
15448
      /* This hack is to avoid a warning when using the "o"
15449
   constraint within gcc asm statements.
15450
   For instance:
15451
15452
   #define _set_tssldt_desc(n,addr,limit,type) \
15453
   __asm__ __volatile__ ( \
15454
   "movw %w2,%0\n\t" \
15455
   "movw %w1,2+%0\n\t" \
15456
   "rorl $16,%1\n\t" \
15457
   "movb %b1,4+%0\n\t" \
15458
   "movb %4,5+%0\n\t" \
15459
   "movb $0,6+%0\n\t" \
15460
   "movb %h1,7+%0\n\t" \
15461
   "rorl $16,%1" \
15462
   : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
15463
15464
   This works great except that the output assembler ends
15465
   up looking a bit weird if it turns out that there is
15466
   no offset.  You end up producing code that looks like:
15467
15468
   #APP
15469
   movw $235,(%eax)
15470
   movw %dx,2+(%eax)
15471
   rorl $16,%edx
15472
   movb %dl,4+(%eax)
15473
   movb $137,5+(%eax)
15474
   movb $0,6+(%eax)
15475
   movb %dh,7+(%eax)
15476
   rorl $16,%edx
15477
   #NO_APP
15478
15479
   So here we provide the missing zero.  */
15480
15481
      *displacement_string_end = '0';
15482
    }
15483
#endif
15484
238
  gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
15485
238
  if (gotfree_input_line)
15486
13
    input_line_pointer = gotfree_input_line;
15487
15488
238
  expr_mode = expr_operator_none;
15489
238
  exp_seg = expression (exp);
15490
15491
238
  SKIP_WHITESPACE ();
15492
238
  if (*input_line_pointer)
15493
34
    as_bad (_("junk `%s' after expression"), input_line_pointer);
15494
#if GCC_ASM_O_HACK
15495
  RESTORE_END_STRING (disp_end + 1);
15496
#endif
15497
238
  input_line_pointer = save_input_line_pointer;
15498
238
  if (gotfree_input_line)
15499
13
    {
15500
13
      free (gotfree_input_line);
15501
15502
13
      if (exp->X_op == O_constant || exp->X_op == O_register)
15503
0
  exp->X_op = O_illegal;
15504
13
    }
15505
15506
238
  ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
15507
15508
238
  RESTORE_END_STRING (disp_end);
15509
15510
238
  return ret;
15511
238
}
15512
15513
static int
15514
i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
15515
          i386_operand_type types, const char *disp_start)
15516
8.74k
{
15517
8.74k
  int ret = 1;
15518
15519
  /* We do this to make sure that the section symbol is in
15520
     the symbol table.  We will ultimately change the relocation
15521
     to be relative to the beginning of the section.  */
15522
8.74k
  if (i.reloc[this_operand] == BFD_RELOC_32_GOTOFF
15523
8.74k
      || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
15524
8.72k
      || i.reloc[this_operand] == BFD_RELOC_64_GOTOFF)
15525
14
    {
15526
14
      if (exp->X_op != O_symbol
15527
0
    && exp->X_op != O_add
15528
0
    && exp->X_op != O_subtract)
15529
0
  goto inv_disp;
15530
15531
14
      if (S_IS_LOCAL (exp->X_add_symbol)
15532
0
    && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
15533
0
    && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
15534
0
  section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
15535
15536
14
      if (exp->X_op != O_symbol)
15537
0
  {
15538
0
    if (S_IS_LOCAL (exp->X_op_symbol)
15539
0
        && S_GET_SEGMENT (exp->X_op_symbol) != undefined_section
15540
0
        && S_GET_SEGMENT (exp->X_op_symbol) != expr_section)
15541
0
      section_symbol (S_GET_SEGMENT (exp->X_op_symbol));
15542
15543
0
    exp->X_add_symbol = make_expr_symbol (exp);
15544
0
  }
15545
15546
14
      exp->X_op = O_subtract;
15547
14
      exp->X_op_symbol = GOT_symbol;
15548
14
      if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
15549
13
  i.reloc[this_operand] = BFD_RELOC_32_PCREL;
15550
1
      else if (i.reloc[this_operand] == BFD_RELOC_64_GOTOFF)
15551
1
  i.reloc[this_operand] = BFD_RELOC_64;
15552
0
      else
15553
0
  i.reloc[this_operand] = BFD_RELOC_32;
15554
14
    }
15555
15556
8.72k
  else if (exp->X_op == O_absent
15557
8.72k
     || exp->X_op == O_illegal
15558
8.72k
     || exp->X_op == O_big)
15559
0
    {
15560
0
    inv_disp:
15561
0
      as_bad (_("missing or invalid displacement expression `%s'"),
15562
0
        disp_start);
15563
0
      ret = 0;
15564
0
    }
15565
15566
8.72k
  else if (exp->X_op == O_constant)
15567
1.15k
    {
15568
      /* Sizing gets taken care of by optimize_disp().
15569
15570
   If not 64bit, sign/zero extend val, to account for wraparound
15571
   when !BFD64.  */
15572
1.15k
      if (expr_mode == expr_operator_present
15573
1.12k
    && flag_code != CODE_64BIT && !object_64bit)
15574
0
  exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
15575
1.15k
    }
15576
15577
#ifdef OBJ_AOUT
15578
  else if (exp_seg != absolute_section
15579
     && exp_seg != text_section
15580
     && exp_seg != data_section
15581
     && exp_seg != bss_section
15582
     && exp_seg != undefined_section
15583
     && !bfd_is_com_section (exp_seg))
15584
    {
15585
      as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
15586
      ret = 0;
15587
    }
15588
#endif
15589
15590
7.57k
  else if (current_templates.start->opcode_modifier.jump == JUMP_BYTE)
15591
0
    i.types[this_operand].bitfield.disp8 = 1;
15592
15593
  /* Check if this is a displacement only operand.  */
15594
8.74k
  if (!i.types[this_operand].bitfield.baseindex)
15595
8.72k
    i.types[this_operand] =
15596
8.72k
      operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
15597
8.72k
           operand_type_and (i.types[this_operand], types));
15598
15599
8.74k
  return ret;
15600
8.74k
}
15601
15602
/* Return the active addressing mode, taking address override and
15603
   registers forming the address into consideration.  Update the
15604
   address override prefix if necessary.  */
15605
15606
static enum flag_code
15607
i386_addressing_mode (void)
15608
17.4k
{
15609
17.4k
  enum flag_code addr_mode;
15610
15611
17.4k
  if (i.prefix[ADDR_PREFIX])
15612
2
    addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
15613
17.4k
  else if (flag_code == CODE_16BIT
15614
838
     && is_cpu (current_templates.start, CpuMPX)
15615
     /* Avoid replacing the "16-bit addressing not allowed" diagnostic
15616
        from md_assemble() by "is not a valid base/index expression"
15617
        when there is a base and/or index.  */
15618
0
     && !i.types[this_operand].bitfield.baseindex)
15619
0
    {
15620
      /* MPX insn memory operands with neither base nor index must be forced
15621
   to use 32-bit addressing in 16-bit mode.  */
15622
0
      addr_mode = CODE_32BIT;
15623
0
      i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
15624
0
      ++i.prefixes;
15625
0
      gas_assert (!i.types[this_operand].bitfield.disp16);
15626
0
      gas_assert (!i.types[this_operand].bitfield.disp32);
15627
0
    }
15628
17.4k
  else
15629
17.4k
    {
15630
17.4k
      addr_mode = flag_code;
15631
15632
17.4k
#if INFER_ADDR_PREFIX
15633
17.4k
      if (i.mem_operands == 0)
15634
17.2k
  {
15635
    /* Infer address prefix from the first memory operand.  */
15636
17.2k
    const reg_entry *addr_reg = i.base_reg;
15637
15638
17.2k
    if (addr_reg == NULL)
15639
17.2k
      addr_reg = i.index_reg;
15640
15641
17.2k
    if (addr_reg)
15642
58
      {
15643
58
        if (addr_reg->reg_type.bitfield.dword)
15644
18
    addr_mode = CODE_32BIT;
15645
40
        else if (flag_code != CODE_64BIT
15646
2
           && addr_reg->reg_type.bitfield.word)
15647
0
    addr_mode = CODE_16BIT;
15648
15649
58
        if (addr_mode != flag_code)
15650
18
    {
15651
18
      i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
15652
18
      i.prefixes += 1;
15653
      /* Change the size of any displacement too.  At most one
15654
         of Disp16 or Disp32 is set.
15655
         FIXME.  There doesn't seem to be any real need for
15656
         separate Disp16 and Disp32 flags.  The same goes for
15657
         Imm16 and Imm32.  Removing them would probably clean
15658
         up the code quite a lot.  */
15659
18
      if (flag_code != CODE_64BIT
15660
0
          && (i.types[this_operand].bitfield.disp16
15661
0
        || i.types[this_operand].bitfield.disp32))
15662
0
        {
15663
0
          static const i386_operand_type disp16_32 = {
15664
0
      .bitfield = { .disp16 = 1, .disp32 = 1 }
15665
0
          };
15666
15667
0
          i.types[this_operand]
15668
0
      = operand_type_xor (i.types[this_operand], disp16_32);
15669
0
        }
15670
18
    }
15671
58
      }
15672
17.2k
  }
15673
17.4k
#endif
15674
17.4k
    }
15675
15676
17.4k
  return addr_mode;
15677
17.4k
}
15678
15679
/* Make sure the memory operand we've been dealt is valid.
15680
   Return 1 on success, 0 on a failure.  */
15681
15682
static int
15683
i386_index_check (const char *operand_string)
15684
8.71k
{
15685
8.71k
  const char *kind = "base/index";
15686
8.71k
  enum flag_code addr_mode = i386_addressing_mode ();
15687
8.71k
  const insn_template *t = current_templates.end - 1;
15688
15689
8.71k
  if (t->opcode_modifier.isstring)
15690
16
    {
15691
16
      const i386_operand_type *t_types = get_operand_types (t);
15692
      /* Memory operands of string insns are special in that they only allow
15693
   a single register (rDI or rSI) as their memory address.  */
15694
16
      const reg_entry *expected_reg;
15695
16
      static const char di_si[][2][4] =
15696
16
  {
15697
16
    { "esi", "edi" },
15698
16
    { "si", "di" },
15699
16
    { "rsi", "rdi" }
15700
16
  };
15701
      /* For a few other insns with fixed register addressing we (ab)use the
15702
   IsString attribute as well.  */
15703
16
      static const char loregs[][4][4] =
15704
16
  {
15705
16
    { "eax", "ecx", "edx", "ebx" },
15706
16
    {  "ax",  "cx",  "dx",  "bx" },
15707
16
    { "rax", "rcx", "rdx", "rbx" }
15708
16
  };
15709
15710
16
      kind = "string address";
15711
15712
16
      if (t->opcode_modifier.prefixok == PrefixRep)
15713
16
  {
15714
16
    int es_op = t->opcode_modifier.isstring - IS_STRING_ES_OP0;
15715
16
    int op = 0;
15716
15717
16
    if (!t_types[0].bitfield.baseindex
15718
16
        || ((!i.mem_operands != !intel_syntax)
15719
16
      && t_types[1].bitfield.baseindex))
15720
0
      op = 1;
15721
16
    expected_reg = str_hash_find (reg_hash,
15722
16
          di_si[addr_mode][op == es_op]);
15723
16
  }
15724
0
      else
15725
0
  {
15726
0
    unsigned int op = t_types[0].bitfield.baseindex ? 0 : 1;
15727
15728
0
    if (!t_types[op].bitfield.instance)
15729
0
      return 1; /* Operand mismatch will be detected elsewhere.  */
15730
0
    expected_reg
15731
0
      = str_hash_find (reg_hash,
15732
0
           loregs[addr_mode][t_types[op]
15733
0
                 .bitfield.instance - 1]);
15734
0
  }
15735
15736
16
      if (i.base_reg != expected_reg
15737
0
    || i.index_reg
15738
0
    || operand_type_check (i.types[this_operand], disp))
15739
16
  {
15740
    /* The second memory operand must have the same size as
15741
       the first one.  */
15742
16
    if (i.mem_operands
15743
0
        && i.base_reg
15744
0
        && !((addr_mode == CODE_64BIT
15745
0
        && i.base_reg->reg_type.bitfield.qword)
15746
0
       || (addr_mode == CODE_32BIT
15747
0
           ? i.base_reg->reg_type.bitfield.dword
15748
0
           : i.base_reg->reg_type.bitfield.word)))
15749
0
      goto bad_address;
15750
15751
16
    as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
15752
16
       operand_string,
15753
16
       intel_syntax ? '[' : '(',
15754
16
       register_prefix,
15755
16
       expected_reg->reg_name,
15756
16
       intel_syntax ? ']' : ')');
15757
16
    return 1;
15758
16
  }
15759
0
      else
15760
0
  return 1;
15761
15762
41
    bad_address:
15763
41
      as_bad (_("`%s' is not a valid %s expression"),
15764
41
        operand_string, kind);
15765
41
      return 0;
15766
16
    }
15767
8.69k
  else
15768
8.69k
    {
15769
8.69k
      t = current_templates.start;
15770
15771
8.69k
      if (addr_mode != CODE_16BIT)
15772
8.27k
  {
15773
    /* 32-bit/64-bit checks.  */
15774
8.27k
    if (pp.disp_encoding == disp_encoding_16bit)
15775
0
      {
15776
0
      bad_disp:
15777
0
        as_bad (_("invalid `%s' prefix"),
15778
0
          addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
15779
0
        return 0;
15780
0
      }
15781
15782
8.27k
    if ((i.base_reg
15783
30
         && ((addr_mode == CODE_64BIT
15784
30
        ? !i.base_reg->reg_type.bitfield.qword
15785
30
        : !i.base_reg->reg_type.bitfield.dword)
15786
4
       || (i.index_reg && i.base_reg->reg_num == RegIP)
15787
4
       || i.base_reg->reg_num == RegIZ))
15788
8.25k
        || (i.index_reg
15789
15
      && !i.index_reg->reg_type.bitfield.xmmword
15790
15
      && !i.index_reg->reg_type.bitfield.ymmword
15791
15
      && !i.index_reg->reg_type.bitfield.zmmword
15792
15
      && ((addr_mode == CODE_64BIT
15793
15
           ? !i.index_reg->reg_type.bitfield.qword
15794
15
           : !i.index_reg->reg_type.bitfield.dword)
15795
15
          || !i.index_reg->reg_type.bitfield.baseindex)))
15796
40
      goto bad_address;
15797
15798
    /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
15799
8.23k
    if (t->mnem_off == MN_bndmk
15800
8.23k
        || t->mnem_off == MN_bndldx
15801
8.23k
        || t->mnem_off == MN_bndstx
15802
8.23k
        || t->opcode_modifier.sib == SIBMEM)
15803
0
      {
15804
        /* They cannot use RIP-relative addressing. */
15805
0
        if (i.base_reg && i.base_reg->reg_num == RegIP)
15806
0
    {
15807
0
      as_bad (_("`%s' cannot be used here"), operand_string);
15808
0
      return 0;
15809
0
    }
15810
15811
        /* bndldx and bndstx ignore their scale factor. */
15812
0
        if ((t->mnem_off == MN_bndldx || t->mnem_off == MN_bndstx)
15813
0
      && i.log2_scale_factor)
15814
0
    as_warn (_("register scaling is being ignored here"));
15815
0
      }
15816
8.23k
  }
15817
419
      else
15818
419
  {
15819
    /* 16-bit checks.  */
15820
419
    if (pp.disp_encoding == disp_encoding_32bit)
15821
0
      goto bad_disp;
15822
15823
419
    if ((i.base_reg
15824
1
         && (!i.base_reg->reg_type.bitfield.word
15825
0
       || !i.base_reg->reg_type.bitfield.baseindex))
15826
418
        || (i.index_reg
15827
0
      && (!i.index_reg->reg_type.bitfield.word
15828
0
          || !i.index_reg->reg_type.bitfield.baseindex
15829
0
          || !(i.base_reg
15830
0
         && i.base_reg->reg_num < 6
15831
0
         && i.index_reg->reg_num >= 6
15832
0
         && i.log2_scale_factor == 0))))
15833
1
      goto bad_address;
15834
419
  }
15835
8.69k
    }
15836
8.65k
  return 1;
15837
8.71k
}
15838
15839
/* Handle vector immediates.  */
15840
15841
static int
15842
RC_SAE_immediate (const char *imm_start)
15843
29.8k
{
15844
29.8k
  const char *pstr = imm_start;
15845
15846
29.8k
  if (*pstr != '{')
15847
29.7k
    return 0;
15848
15849
161
  pstr++;
15850
161
  if (is_whitespace (*pstr))
15851
0
    pstr++;
15852
15853
161
  pstr = RC_SAE_specifier (pstr);
15854
161
  if (pstr == NULL)
15855
156
    return 0;
15856
15857
5
  if (is_whitespace (*pstr))
15858
0
    pstr++;
15859
15860
5
  if (*pstr++ != '}')
15861
0
    {
15862
0
      as_bad (_("Missing '}': '%s'"), imm_start);
15863
0
      return 0;
15864
0
    }
15865
5
  /* RC/SAE immediate string should contain nothing more.  */;
15866
5
  if (*pstr != 0)
15867
5
    {
15868
5
      as_bad (_("Junk after '}': '%s'"), imm_start);
15869
5
      return 0;
15870
5
    }
15871
15872
  /* Internally this doesn't count as an operand.  */
15873
0
  --i.operands;
15874
15875
0
  return 1;
15876
5
}
15877
15878
static INLINE bool starts_memory_operand (char c)
15879
550
{
15880
550
  return ISDIGIT (c)
15881
527
   || is_name_beginner (c)
15882
158
   || (c && strchr ("([\"+-!~", c));
15883
550
}
15884
15885
/* Parse OPERAND_STRING into the i386_insn structure I.  Returns zero
15886
   on error.  */
15887
15888
static int
15889
i386_att_operand (char *operand_string)
15890
2.00k
{
15891
2.00k
  const reg_entry *r;
15892
2.00k
  char *end_op;
15893
2.00k
  char *op_string = operand_string;
15894
15895
2.00k
  if (is_whitespace (*op_string))
15896
0
    ++op_string;
15897
15898
  /* We check for an absolute prefix (differentiating,
15899
     for example, 'jmp pc_relative_label' from 'jmp *absolute_label'.  */
15900
2.00k
  if (*op_string == ABSOLUTE_PREFIX
15901
1
      && current_templates.start->opcode_modifier.jump)
15902
1
    {
15903
1
      ++op_string;
15904
1
      if (is_whitespace (*op_string))
15905
0
  ++op_string;
15906
1
      i.jumpabsolute = true;
15907
1
    }
15908
15909
  /* Check if operand is a register.  */
15910
2.00k
  if ((r = parse_register (op_string, &end_op)) != NULL)
15911
63
    {
15912
63
      i386_operand_type temp;
15913
15914
63
      if (r == &bad_reg)
15915
0
  return 0;
15916
15917
      /* Check for a segment override by searching for ':' after a
15918
   segment register.  */
15919
63
      op_string = end_op;
15920
63
      if (is_whitespace (*op_string))
15921
1
  ++op_string;
15922
63
      if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
15923
2
  {
15924
2
    i.seg[i.mem_operands] = r;
15925
15926
    /* Skip the ':' and whitespace.  */
15927
2
    ++op_string;
15928
2
    if (is_whitespace (*op_string))
15929
2
      ++op_string;
15930
15931
    /* Handle case of %es:*foo.  */
15932
2
    if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX
15933
0
        && current_templates.start->opcode_modifier.jump)
15934
0
      {
15935
0
        ++op_string;
15936
0
        if (is_whitespace (*op_string))
15937
0
    ++op_string;
15938
0
        i.jumpabsolute = true;
15939
0
      }
15940
15941
2
    if (!starts_memory_operand (*op_string))
15942
0
      {
15943
0
        as_bad (_("bad memory operand `%s'"), op_string);
15944
0
        return 0;
15945
0
      }
15946
2
    goto do_memory_reference;
15947
2
  }
15948
15949
      /* Handle vector operations.  */
15950
61
      if (*op_string == '{')
15951
0
  {
15952
0
    op_string = check_VecOperations (op_string);
15953
0
    if (op_string == NULL)
15954
0
      return 0;
15955
0
  }
15956
15957
61
      if (*op_string)
15958
1
  {
15959
1
    as_bad (_("junk `%s' after register"), op_string);
15960
1
    return 0;
15961
1
  }
15962
15963
       /* Reject pseudo registers for .insn.  */
15964
60
      if (dot_insn () && r->reg_type.bitfield.class == ClassNone)
15965
0
  {
15966
0
    as_bad (_("`%s%s' cannot be used here"),
15967
0
      register_prefix, r->reg_name);
15968
0
    return 0;
15969
0
  }
15970
15971
60
      temp = r->reg_type;
15972
60
      temp.bitfield.baseindex = 0;
15973
60
      i.types[this_operand] = operand_type_or (i.types[this_operand],
15974
60
                 temp);
15975
60
      i.types[this_operand].bitfield.unspecified = 0;
15976
60
      i.op[this_operand].regs = r;
15977
60
      i.reg_operands++;
15978
15979
      /* A GPR may follow an RC or SAE immediate only if a (vector) register
15980
         operand was also present earlier on.  */
15981
60
      if (i.rounding.type != rc_none && temp.bitfield.class == Reg
15982
0
          && i.reg_operands == 1)
15983
0
  {
15984
0
    unsigned int j;
15985
15986
0
    for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
15987
0
      if (i.rounding.type == RC_NamesTable[j].type)
15988
0
        break;
15989
0
    as_bad (_("`%s': misplaced `{%s}'"),
15990
0
      insn_name (current_templates.start), RC_NamesTable[j].name);
15991
0
    return 0;
15992
0
  }
15993
60
    }
15994
1.94k
  else if (*op_string == REGISTER_PREFIX)
15995
8
    {
15996
8
      as_bad (_("bad register name `%s'"), op_string);
15997
8
      return 0;
15998
8
    }
15999
1.93k
  else if (*op_string == IMMEDIATE_PREFIX)
16000
1.38k
    {
16001
1.38k
      ++op_string;
16002
1.38k
      if (i.jumpabsolute)
16003
1
  {
16004
1
    as_bad (_("immediate operand illegal with absolute jump"));
16005
1
    return 0;
16006
1
  }
16007
1.38k
      if (!i386_immediate (op_string))
16008
0
  return 0;
16009
1.38k
      if (i.rounding.type != rc_none)
16010
0
  {
16011
0
    as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
16012
0
      insn_name (current_templates.start));
16013
0
    return 0;
16014
0
  }
16015
1.38k
    }
16016
548
  else if (RC_SAE_immediate (operand_string))
16017
0
    {
16018
      /* If it is a RC or SAE immediate, do the necessary placement check:
16019
   Only another immediate or a GPR may precede it.  */
16020
0
      if (i.mem_operands || i.reg_operands + i.imm_operands > 1
16021
0
    || (i.reg_operands == 1
16022
0
        && i.types[0].bitfield.class != Reg))
16023
0
  {
16024
0
    as_bad (_("`%s': misplaced `%s'"),
16025
0
      insn_name (current_templates.start), operand_string);
16026
0
    return 0;
16027
0
  }
16028
0
    }
16029
548
  else if (starts_memory_operand (*op_string))
16030
393
    {
16031
      /* This is a memory reference of some sort.  */
16032
393
      char *base_string;
16033
16034
      /* Start and end of displacement string expression (if found).  */
16035
393
      char *displacement_string_start;
16036
393
      char *displacement_string_end;
16037
16038
395
    do_memory_reference:
16039
      /* Check for base index form.  We detect the base index form by
16040
   looking for an ')' at the end of the operand, searching
16041
   for the '(' matching it, and finding a REGISTER_PREFIX or ','
16042
   after the '('.  */
16043
395
      base_string = op_string + strlen (op_string);
16044
16045
      /* Handle vector operations.  */
16046
395
      --base_string;
16047
395
      if (is_whitespace (*base_string))
16048
2
  --base_string;
16049
16050
395
      if (*base_string == '}')
16051
155
  {
16052
155
    char *vop_start = NULL;
16053
16054
2.63k
    while (base_string-- > op_string)
16055
2.48k
      {
16056
2.48k
        if (*base_string == '"')
16057
0
    break;
16058
2.48k
        if (*base_string != '{')
16059
2.48k
    continue;
16060
16061
0
        vop_start = base_string;
16062
16063
0
        --base_string;
16064
0
        if (is_whitespace (*base_string))
16065
0
    --base_string;
16066
16067
0
        if (*base_string != '}')
16068
0
    break;
16069
16070
0
        vop_start = NULL;
16071
0
      }
16072
16073
155
    if (!vop_start)
16074
155
      {
16075
155
        as_bad (_("unbalanced figure braces"));
16076
155
        return 0;
16077
155
      }
16078
16079
0
    if (check_VecOperations (vop_start) == NULL)
16080
0
      return 0;
16081
0
  }
16082
16083
      /* If we only have a displacement, set-up for it to be parsed later.  */
16084
240
      displacement_string_start = op_string;
16085
240
      displacement_string_end = base_string + 1;
16086
16087
240
      if (*base_string == ')')
16088
4
  {
16089
4
    char *temp_string;
16090
4
    unsigned int parens_not_balanced = 0;
16091
4
    bool in_quotes = false;
16092
16093
    /* We've already checked that the number of left & right ()'s are
16094
       equal, and that there's a matching set of double quotes.  */
16095
4
    end_op = base_string;
16096
36
    for (temp_string = op_string; temp_string < end_op; temp_string++)
16097
32
      {
16098
32
        if (*temp_string == '\\' && temp_string[1] == '"')
16099
0
    ++temp_string;
16100
32
        else if (*temp_string == '"')
16101
0
    in_quotes = !in_quotes;
16102
32
        else if (!in_quotes)
16103
32
    {
16104
32
      if (*temp_string == '(' && !parens_not_balanced++)
16105
4
        base_string = temp_string;
16106
32
      if (*temp_string == ')')
16107
0
        --parens_not_balanced;
16108
32
    }
16109
32
      }
16110
16111
4
    temp_string = base_string;
16112
16113
    /* Skip past '(' and whitespace.  */
16114
4
    gas_assert (*base_string == '(');
16115
4
    ++base_string;
16116
4
    if (is_whitespace (*base_string))
16117
0
      ++base_string;
16118
16119
4
    if (*base_string == ','
16120
4
        || ((i.base_reg = parse_register (base_string, &end_op))
16121
4
      != NULL))
16122
4
      {
16123
4
        displacement_string_end = temp_string;
16124
16125
4
        i.types[this_operand].bitfield.baseindex = 1;
16126
16127
4
        if (i.base_reg)
16128
4
    {
16129
4
      if (i.base_reg == &bad_reg)
16130
0
        return 0;
16131
4
      base_string = end_op;
16132
4
      if (is_whitespace (*base_string))
16133
0
        ++base_string;
16134
4
    }
16135
16136
        /* There may be an index reg or scale factor here.  */
16137
4
        if (*base_string == ',')
16138
1
    {
16139
1
      ++base_string;
16140
1
      if (is_whitespace (*base_string))
16141
0
        ++base_string;
16142
16143
1
      if ((i.index_reg = parse_register (base_string, &end_op))
16144
1
          != NULL)
16145
1
        {
16146
1
          if (i.index_reg == &bad_reg)
16147
0
      return 0;
16148
1
          base_string = end_op;
16149
1
          if (is_whitespace (*base_string))
16150
0
      ++base_string;
16151
1
          if (*base_string == ',')
16152
1
      {
16153
1
        ++base_string;
16154
1
        if (is_whitespace (*base_string))
16155
0
          ++base_string;
16156
1
      }
16157
0
          else if (*base_string != ')')
16158
0
      {
16159
0
        as_bad (_("expecting `,' or `)' "
16160
0
            "after index register in `%s'"),
16161
0
          operand_string);
16162
0
        return 0;
16163
0
      }
16164
1
        }
16165
0
      else if (*base_string == REGISTER_PREFIX)
16166
0
        {
16167
0
          end_op = strchr (base_string, ',');
16168
0
          if (end_op)
16169
0
      *end_op = '\0';
16170
0
          as_bad (_("bad register name `%s'"), base_string);
16171
0
          return 0;
16172
0
        }
16173
16174
      /* Check for scale factor.  */
16175
1
      if (*base_string != ')')
16176
1
        {
16177
1
          char *end_scale = i386_scale (base_string);
16178
16179
1
          if (!end_scale)
16180
0
      return 0;
16181
16182
1
          base_string = end_scale;
16183
1
          if (is_whitespace (*base_string))
16184
0
      ++base_string;
16185
1
          if (*base_string != ')')
16186
0
      {
16187
0
        as_bad (_("expecting `)' "
16188
0
            "after scale factor in `%s'"),
16189
0
          operand_string);
16190
0
        return 0;
16191
0
      }
16192
1
        }
16193
0
      else if (!i.index_reg)
16194
0
        {
16195
0
          as_bad (_("expecting index register or scale factor "
16196
0
        "after `,'; got '%c'"),
16197
0
            *base_string);
16198
0
          return 0;
16199
0
        }
16200
1
    }
16201
3
        else if (*base_string != ')')
16202
0
    {
16203
0
      as_bad (_("expecting `,' or `)' "
16204
0
          "after base register in `%s'"),
16205
0
        operand_string);
16206
0
      return 0;
16207
0
    }
16208
4
      }
16209
0
    else if (*base_string == REGISTER_PREFIX)
16210
0
      {
16211
0
        end_op = strchr (base_string, ',');
16212
0
        if (end_op)
16213
0
    *end_op = '\0';
16214
0
        as_bad (_("bad register name `%s'"), base_string);
16215
0
        return 0;
16216
0
      }
16217
4
  }
16218
16219
      /* If there's an expression beginning the operand, parse it,
16220
   assuming displacement_string_start and
16221
   displacement_string_end are meaningful.  */
16222
240
      if (displacement_string_start != displacement_string_end)
16223
238
  {
16224
238
    if (!i386_displacement (displacement_string_start,
16225
238
          displacement_string_end))
16226
0
      return 0;
16227
238
  }
16228
16229
      /* Special case for (%dx) while doing input/output op.  */
16230
240
      if (i.base_reg
16231
4
    && i.base_reg->reg_type.bitfield.instance == RegD
16232
0
    && i.base_reg->reg_type.bitfield.word
16233
0
    && i.index_reg == 0
16234
0
    && i.log2_scale_factor == 0
16235
0
    && i.seg[i.mem_operands] == 0
16236
0
    && !operand_type_check (i.types[this_operand], disp))
16237
0
  {
16238
0
    i.types[this_operand] = i.base_reg->reg_type;
16239
0
    i.op[this_operand].regs = i.base_reg;
16240
0
    i.base_reg = NULL;
16241
0
    i.input_output_operand = true;
16242
0
    return 1;
16243
0
  }
16244
16245
240
      if (i386_index_check (operand_string) == 0)
16246
0
  return 0;
16247
240
      i.flags[this_operand] |= Operand_Mem;
16248
240
      i.mem_operands++;
16249
240
    }
16250
155
  else
16251
155
    {
16252
      /* It's not a memory operand; argh!  */
16253
155
      as_bad (_("invalid char %s beginning operand %d `%s'"),
16254
155
        output_invalid (*op_string),
16255
155
        this_operand + 1,
16256
155
        op_string);
16257
155
      return 0;
16258
155
    }
16259
1.68k
  return 1;     /* Normal return.  */
16260
2.00k
}
16261

16262
/* Initialize the tc_frag_data field of a fragment.  */
16263
16264
void i386_frag_init (fragS *fragP, size_t max_bytes)
16265
4.45k
{
16266
4.45k
  memset (&fragP->tc_frag_data, 0, sizeof (fragP->tc_frag_data));
16267
4.45k
  fragP->tc_frag_data.isa = cpu_arch_isa;
16268
4.45k
  fragP->tc_frag_data.tune = cpu_arch_tune;
16269
4.45k
  fragP->tc_frag_data.cpunop = cpu_arch_flags.bitfield.cpunop;
16270
4.45k
  fragP->tc_frag_data.isanop = cpu_arch_isa_flags.bitfield.cpunop;
16271
4.45k
  fragP->tc_frag_data.code = i386_flag_code;
16272
4.45k
  fragP->tc_frag_data.max_bytes = max_bytes;
16273
4.45k
  fragP->tc_frag_data.last_insn_normal
16274
4.45k
    = (seg_info(now_seg)->tc_segment_info_data.last_insn.kind
16275
4.45k
       == last_insn_other);
16276
4.45k
  fragP->tc_frag_data.no_cond_jump_promotion = no_cond_jump_promotion;
16277
4.45k
}
16278
16279
/* Calculate the maximum variable size (i.e., excluding fr_fix)
16280
   that an rs_machine_dependent frag may reach.  */
16281
16282
unsigned int
16283
i386_frag_max_var (fragS *frag)
16284
1
{
16285
  /* The only relaxable frags are for jumps.
16286
     Unconditional jumps can grow by 4 bytes and others by 5 bytes.  */
16287
1
  gas_assert (frag->fr_type == rs_machine_dependent);
16288
1
  return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
16289
1
}
16290
16291
#ifdef OBJ_ELF
16292
static int
16293
elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
16294
0
{
16295
  /* STT_GNU_IFUNC symbol must go through PLT.  */
16296
0
  if ((symbol_get_bfdsym (fr_symbol)->flags
16297
0
       & BSF_GNU_INDIRECT_FUNCTION) != 0)
16298
0
    return 0;
16299
16300
0
  if (!S_IS_EXTERNAL (fr_symbol))
16301
    /* Symbol may be weak or local.  */
16302
0
    return !S_IS_WEAK (fr_symbol);
16303
16304
  /* Global symbols with non-default visibility can't be preempted. */
16305
0
  if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
16306
0
    return 1;
16307
16308
0
  if (fr_var != NO_RELOC)
16309
0
    switch ((enum bfd_reloc_code_real) fr_var)
16310
0
      {
16311
0
      case BFD_RELOC_386_PLT32:
16312
0
      case BFD_RELOC_32_PLT_PCREL:
16313
  /* Symbol with PLT relocation may be preempted. */
16314
0
  return 0;
16315
0
      default:
16316
0
  abort ();
16317
0
      }
16318
16319
  /* Global symbols with default visibility in a shared library may be
16320
     preempted by another definition.  */
16321
0
  return !shared;
16322
0
}
16323
#endif
16324
16325
/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
16326
   Note also work for Skylake and Cascadelake.
16327
---------------------------------------------------------------------
16328
|   JCC   | ADD/SUB/CMP | INC/DEC | TEST/AND |
16329
| ------  | ----------- | ------- | -------- |
16330
|   Jo    |      N      |    N    |     Y    |
16331
|   Jno   |      N      |    N    |     Y    |
16332
|  Jc/Jb  |      Y      |    N    |     Y    |
16333
| Jae/Jnb |      Y      |    N    |     Y    |
16334
|  Je/Jz  |      Y      |    Y    |     Y    |
16335
| Jne/Jnz |      Y      |    Y    |     Y    |
16336
| Jna/Jbe |      Y      |    N    |     Y    |
16337
| Ja/Jnbe |      Y      |    N    |     Y    |
16338
|   Js    |      N      |    N    |     Y    |
16339
|   Jns   |      N      |    N    |     Y    |
16340
|  Jp/Jpe |      N      |    N    |     Y    |
16341
| Jnp/Jpo |      N      |    N    |     Y    |
16342
| Jl/Jnge |      Y      |    Y    |     Y    |
16343
| Jge/Jnl |      Y      |    Y    |     Y    |
16344
| Jle/Jng |      Y      |    Y    |     Y    |
16345
| Jg/Jnle |      Y      |    Y    |     Y    |
16346
---------------------------------------------------------------------  */
16347
static int
16348
i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
16349
0
{
16350
0
  if (mf_cmp == mf_cmp_alu_cmp)
16351
0
    return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
16352
0
      || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
16353
0
  if (mf_cmp == mf_cmp_incdec)
16354
0
    return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
16355
0
      || mf_jcc == mf_jcc_jle);
16356
0
  if (mf_cmp == mf_cmp_test_and)
16357
0
    return 1;
16358
0
  return 0;
16359
0
}
16360
16361
/* Return the next non-empty frag.  */
16362
16363
static fragS *
16364
i386_next_non_empty_frag (fragS *fragP)
16365
0
{
16366
  /* There may be a frag with a ".fill 0" when there is no room in
16367
     the current frag for frag_grow in output_insn.  */
16368
0
  for (fragP = fragP->fr_next;
16369
0
       (fragP != NULL
16370
0
  && fragP->fr_type == rs_fill
16371
0
  && fragP->fr_fix == 0);
16372
0
       fragP = fragP->fr_next)
16373
0
    ;
16374
0
  return fragP;
16375
0
}
16376
16377
/* Return the next jcc frag after BRANCH_PADDING.  */
16378
16379
static fragS *
16380
i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
16381
0
{
16382
0
  fragS *branch_fragP;
16383
0
  if (!pad_fragP)
16384
0
    return NULL;
16385
16386
0
  if (pad_fragP->fr_type == rs_machine_dependent
16387
0
      && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
16388
0
    == BRANCH_PADDING))
16389
0
    {
16390
0
      branch_fragP = i386_next_non_empty_frag (pad_fragP);
16391
0
      if (branch_fragP->fr_type != rs_machine_dependent)
16392
0
  return NULL;
16393
0
      if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
16394
0
    && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
16395
0
           pad_fragP->tc_frag_data.mf_type))
16396
0
  return branch_fragP;
16397
0
    }
16398
16399
0
  return NULL;
16400
0
}
16401
16402
/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags.  */
16403
16404
static void
16405
i386_classify_machine_dependent_frag (fragS *fragP)
16406
0
{
16407
0
  fragS *cmp_fragP;
16408
0
  fragS *pad_fragP;
16409
0
  fragS *branch_fragP;
16410
0
  fragS *next_fragP;
16411
0
  unsigned int max_prefix_length;
16412
16413
0
  if (fragP->tc_frag_data.classified)
16414
0
    return;
16415
16416
  /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING.  Convert
16417
     FUSED_JCC_PADDING and merge BRANCH_PADDING.  */
16418
0
  for (next_fragP = fragP;
16419
0
       next_fragP != NULL;
16420
0
       next_fragP = next_fragP->fr_next)
16421
0
    {
16422
0
      next_fragP->tc_frag_data.classified = 1;
16423
0
      if (next_fragP->fr_type == rs_machine_dependent)
16424
0
  switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
16425
0
    {
16426
0
    case BRANCH_PADDING:
16427
      /* The BRANCH_PADDING frag must be followed by a branch
16428
         frag.  */
16429
0
      branch_fragP = i386_next_non_empty_frag (next_fragP);
16430
0
      next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
16431
0
      break;
16432
0
    case FUSED_JCC_PADDING:
16433
      /* Check if this is a fused jcc:
16434
         FUSED_JCC_PADDING
16435
         CMP like instruction
16436
         BRANCH_PADDING
16437
         COND_JUMP
16438
         */
16439
0
      cmp_fragP = i386_next_non_empty_frag (next_fragP);
16440
0
      pad_fragP = i386_next_non_empty_frag (cmp_fragP);
16441
0
      branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
16442
0
      if (branch_fragP)
16443
0
        {
16444
    /* The BRANCH_PADDING frag is merged with the
16445
       FUSED_JCC_PADDING frag.  */
16446
0
    next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
16447
    /* CMP like instruction size.  */
16448
0
    next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
16449
0
    frag_wane (pad_fragP);
16450
    /* Skip to branch_fragP.  */
16451
0
    next_fragP = branch_fragP;
16452
0
        }
16453
0
      else if (next_fragP->tc_frag_data.max_prefix_length)
16454
0
        {
16455
    /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
16456
       a fused jcc.  */
16457
0
    next_fragP->fr_subtype
16458
0
      = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
16459
0
    next_fragP->tc_frag_data.max_bytes
16460
0
      = next_fragP->tc_frag_data.max_prefix_length;
16461
    /* This will be updated in the BRANCH_PREFIX scan.  */
16462
0
    next_fragP->tc_frag_data.max_prefix_length = 0;
16463
0
        }
16464
0
      else
16465
0
        frag_wane (next_fragP);
16466
0
      break;
16467
0
    }
16468
0
    }
16469
16470
  /* Stop if there is no BRANCH_PREFIX.  */
16471
0
  if (!align_branch_prefix_size)
16472
0
    return;
16473
16474
  /* Scan for BRANCH_PREFIX.  */
16475
0
  for (; fragP != NULL; fragP = fragP->fr_next)
16476
0
    {
16477
0
      if (fragP->fr_type != rs_machine_dependent
16478
0
    || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
16479
0
        != BRANCH_PREFIX))
16480
0
  continue;
16481
16482
      /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
16483
   COND_JUMP_PREFIX.  */
16484
0
      max_prefix_length = 0;
16485
0
      for (next_fragP = fragP;
16486
0
     next_fragP != NULL;
16487
0
     next_fragP = next_fragP->fr_next)
16488
0
  {
16489
0
    if (next_fragP->fr_type == rs_fill)
16490
      /* Skip rs_fill frags.  */
16491
0
      continue;
16492
0
    else if (next_fragP->fr_type != rs_machine_dependent)
16493
      /* Stop for all other frags.  */
16494
0
      break;
16495
16496
    /* rs_machine_dependent frags.  */
16497
0
    if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
16498
0
        == BRANCH_PREFIX)
16499
0
      {
16500
        /* Count BRANCH_PREFIX frags.  */
16501
0
        if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
16502
0
    {
16503
0
      max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
16504
0
      frag_wane (next_fragP);
16505
0
    }
16506
0
        else
16507
0
    max_prefix_length
16508
0
      += next_fragP->tc_frag_data.max_bytes;
16509
0
      }
16510
0
    else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
16511
0
        == BRANCH_PADDING)
16512
0
       || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
16513
0
           == FUSED_JCC_PADDING))
16514
0
      {
16515
        /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING.  */
16516
0
        fragP->tc_frag_data.u.padding_fragP = next_fragP;
16517
0
        break;
16518
0
      }
16519
0
    else
16520
      /* Stop for other rs_machine_dependent frags.  */
16521
0
      break;
16522
0
  }
16523
16524
0
      fragP->tc_frag_data.max_prefix_length = max_prefix_length;
16525
16526
      /* Skip to the next frag.  */
16527
0
      fragP = next_fragP;
16528
0
    }
16529
0
}
16530
16531
/* Compute padding size for
16532
16533
  FUSED_JCC_PADDING
16534
  CMP like instruction
16535
  BRANCH_PADDING
16536
  COND_JUMP/UNCOND_JUMP
16537
16538
   or
16539
16540
  BRANCH_PADDING
16541
  COND_JUMP/UNCOND_JUMP
16542
 */
16543
16544
static int
16545
i386_branch_padding_size (fragS *fragP, offsetT address)
16546
0
{
16547
0
  unsigned int offset, size, padding_size;
16548
0
  fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
16549
16550
  /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag.  */
16551
0
  if (!address)
16552
0
    address = fragP->fr_address;
16553
0
  address += fragP->fr_fix;
16554
16555
  /* CMP like instrunction size.  */
16556
0
  size = fragP->tc_frag_data.cmp_size;
16557
16558
  /* The base size of the branch frag.  */
16559
0
  size += branch_fragP->fr_fix;
16560
16561
  /* Add opcode and displacement bytes for the rs_machine_dependent
16562
     branch frag.  */
16563
0
  if (branch_fragP->fr_type == rs_machine_dependent)
16564
0
    size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
16565
16566
  /* Check if branch is within boundary and doesn't end at the last
16567
     byte.  */
16568
0
  offset = address & ((1U << align_branch_power) - 1);
16569
0
  if ((offset + size) >= (1U << align_branch_power))
16570
    /* Padding needed to avoid crossing boundary.  */
16571
0
    padding_size = (1U << align_branch_power) - offset;
16572
0
  else
16573
    /* No padding needed.  */
16574
0
    padding_size = 0;
16575
16576
  /* The return value may be saved in tc_frag_data.length which is
16577
     unsigned byte.  */
16578
0
  if (!fits_in_unsigned_byte (padding_size))
16579
0
    abort ();
16580
16581
0
  return padding_size;
16582
0
}
16583
16584
/* i386_generic_table_relax_frag()
16585
16586
   Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
16587
   grow/shrink padding to align branch frags.  Hand others to
16588
   relax_frag().  */
16589
16590
long
16591
i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
16592
0
{
16593
0
  if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
16594
0
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
16595
0
    {
16596
0
      long padding_size = i386_branch_padding_size (fragP, 0);
16597
0
      long grow = padding_size - fragP->tc_frag_data.length;
16598
16599
      /* When the BRANCH_PREFIX frag is used, the computed address
16600
         must match the actual address and there should be no padding.  */
16601
0
      if (fragP->tc_frag_data.padding_address
16602
0
    && (fragP->tc_frag_data.padding_address != fragP->fr_address
16603
0
        || padding_size))
16604
0
  abort ();
16605
16606
      /* Update the padding size.  */
16607
0
      if (grow)
16608
0
  fragP->tc_frag_data.length = padding_size;
16609
16610
0
      return grow;
16611
0
    }
16612
0
  else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
16613
0
    {
16614
0
      fragS *padding_fragP, *next_fragP;
16615
0
      long padding_size, left_size, last_size;
16616
16617
0
      padding_fragP = fragP->tc_frag_data.u.padding_fragP;
16618
0
      if (!padding_fragP)
16619
  /* Use the padding set by the leading BRANCH_PREFIX frag.  */
16620
0
  return (fragP->tc_frag_data.length
16621
0
    - fragP->tc_frag_data.last_length);
16622
16623
      /* Compute the relative address of the padding frag in the very
16624
        first time where the BRANCH_PREFIX frag sizes are zero.  */
16625
0
      if (!fragP->tc_frag_data.padding_address)
16626
0
  fragP->tc_frag_data.padding_address
16627
0
    = padding_fragP->fr_address - (fragP->fr_address - stretch);
16628
16629
      /* First update the last length from the previous interation.  */
16630
0
      left_size = fragP->tc_frag_data.prefix_length;
16631
0
      for (next_fragP = fragP;
16632
0
     next_fragP != padding_fragP;
16633
0
     next_fragP = next_fragP->fr_next)
16634
0
  if (next_fragP->fr_type == rs_machine_dependent
16635
0
      && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
16636
0
    == BRANCH_PREFIX))
16637
0
    {
16638
0
      if (left_size)
16639
0
        {
16640
0
    int max = next_fragP->tc_frag_data.max_bytes;
16641
0
    if (max)
16642
0
      {
16643
0
        int size;
16644
0
        if (max > left_size)
16645
0
          size = left_size;
16646
0
        else
16647
0
          size = max;
16648
0
        left_size -= size;
16649
0
        next_fragP->tc_frag_data.last_length = size;
16650
0
      }
16651
0
        }
16652
0
      else
16653
0
        next_fragP->tc_frag_data.last_length = 0;
16654
0
    }
16655
16656
      /* Check the padding size for the padding frag.  */
16657
0
      padding_size = i386_branch_padding_size
16658
0
  (padding_fragP, (fragP->fr_address
16659
0
       + fragP->tc_frag_data.padding_address));
16660
16661
0
      last_size = fragP->tc_frag_data.prefix_length;
16662
      /* Check if there is change from the last interation.  */
16663
0
      if (padding_size == last_size)
16664
0
  {
16665
    /* Update the expected address of the padding frag.  */
16666
0
    padding_fragP->tc_frag_data.padding_address
16667
0
      = (fragP->fr_address + padding_size
16668
0
         + fragP->tc_frag_data.padding_address);
16669
0
    return 0;
16670
0
  }
16671
16672
0
      if (padding_size > fragP->tc_frag_data.max_prefix_length)
16673
0
  {
16674
    /* No padding if there is no sufficient room.  Clear the
16675
       expected address of the padding frag.  */
16676
0
    padding_fragP->tc_frag_data.padding_address = 0;
16677
0
    padding_size = 0;
16678
0
  }
16679
0
      else
16680
  /* Store the expected address of the padding frag.  */
16681
0
  padding_fragP->tc_frag_data.padding_address
16682
0
    = (fragP->fr_address + padding_size
16683
0
       + fragP->tc_frag_data.padding_address);
16684
16685
0
      fragP->tc_frag_data.prefix_length = padding_size;
16686
16687
      /* Update the length for the current interation.  */
16688
0
      left_size = padding_size;
16689
0
      for (next_fragP = fragP;
16690
0
     next_fragP != padding_fragP;
16691
0
     next_fragP = next_fragP->fr_next)
16692
0
  if (next_fragP->fr_type == rs_machine_dependent
16693
0
      && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
16694
0
    == BRANCH_PREFIX))
16695
0
    {
16696
0
      if (left_size)
16697
0
        {
16698
0
    int max = next_fragP->tc_frag_data.max_bytes;
16699
0
    if (max)
16700
0
      {
16701
0
        int size;
16702
0
        if (max > left_size)
16703
0
          size = left_size;
16704
0
        else
16705
0
          size = max;
16706
0
        left_size -= size;
16707
0
        next_fragP->tc_frag_data.length = size;
16708
0
      }
16709
0
        }
16710
0
      else
16711
0
        next_fragP->tc_frag_data.length = 0;
16712
0
    }
16713
16714
0
      return (fragP->tc_frag_data.length
16715
0
        - fragP->tc_frag_data.last_length);
16716
0
    }
16717
0
  return relax_frag (segment, fragP, stretch);
16718
0
}
16719
16720
/* md_estimate_size_before_relax()
16721
16722
   Called just before relax() for rs_machine_dependent frags.  The x86
16723
   assembler uses these frags to handle variable size jump
16724
   instructions.
16725
16726
   Any symbol that is now undefined will not become defined.
16727
   Return the correct fr_subtype in the frag.
16728
   Return the initial "guess for variable size of frag" to caller.
16729
   The guess is actually the growth beyond the fixed part.  Whatever
16730
   we do to grow the fixed or variable part contributes to our
16731
   returned value.  */
16732
16733
int
16734
md_estimate_size_before_relax (fragS *fragP, segT segment)
16735
0
{
16736
0
  if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
16737
0
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
16738
0
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
16739
0
    {
16740
0
      i386_classify_machine_dependent_frag (fragP);
16741
0
      return fragP->tc_frag_data.length;
16742
0
    }
16743
16744
  /* We've already got fragP->fr_subtype right;  all we have to do is
16745
     check for un-relaxable symbols.  On an ELF system, we can't relax
16746
     an externally visible symbol, because it may be overridden by a
16747
     shared library.  */
16748
0
  if (S_GET_SEGMENT (fragP->fr_symbol) != segment
16749
0
#ifdef OBJ_ELF
16750
0
      || !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
16751
0
              fragP->fr_var)
16752
0
#endif
16753
#if defined (OBJ_COFF) && defined (TE_PE)
16754
      || S_IS_WEAK (fragP->fr_symbol)
16755
#endif
16756
0
      )
16757
0
    {
16758
      /* Symbol is undefined in this segment, or we need to keep a
16759
   reloc so that weak symbols can be overridden.  */
16760
0
      int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
16761
0
      enum bfd_reloc_code_real reloc_type;
16762
0
      unsigned char *opcode;
16763
0
      int old_fr_fix;
16764
0
      fixS *fixP = NULL;
16765
16766
0
      reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
16767
0
#ifdef OBJ_ELF
16768
0
      if (reloc_type == NO_RELOC
16769
0
    && size != 2
16770
0
    && fragP->tc_frag_data.code == CODE_64BIT
16771
0
    && fragP->fr_offset == 0
16772
0
    && need_plt32_p (fragP->fr_symbol))
16773
0
  reloc_type = BFD_RELOC_32_PLT_PCREL;
16774
0
#endif
16775
16776
0
      old_fr_fix = fragP->fr_fix;
16777
0
      opcode = (unsigned char *) fragP->fr_opcode;
16778
16779
0
      switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
16780
0
  {
16781
0
  case UNCOND_JUMP:
16782
    /* Make jmp (0xeb) a (d)word displacement jump.  */
16783
0
    opcode[0] = 0xe9;
16784
0
    fragP->fr_fix += size;
16785
0
    fixP = fix_new (fragP, old_fr_fix, size,
16786
0
        fragP->fr_symbol,
16787
0
        fragP->fr_offset, 1,
16788
0
        _reloc (size, 1, 1, reloc_type,
16789
0
          fragP->tc_frag_data.code == CODE_64BIT,
16790
0
          fragP->fr_file, fragP->fr_line));
16791
0
    break;
16792
16793
0
  case COND_JUMP86:
16794
0
    if (fragP->tc_frag_data.no_cond_jump_promotion
16795
0
        && fragP->fr_var == NO_RELOC)
16796
0
      {
16797
0
        fragP->fr_fix += 1;
16798
0
        fixP = fix_new (fragP, old_fr_fix, 1,
16799
0
            fragP->fr_symbol,
16800
0
            fragP->fr_offset, 1,
16801
0
            BFD_RELOC_8_PCREL);
16802
0
        fixP->fx_signed = 1;
16803
0
        break;
16804
0
      }
16805
16806
0
    if (size == 2)
16807
0
      {
16808
        /* Negate the condition, and branch past an
16809
     unconditional jump.  */
16810
0
        opcode[0] ^= 1;
16811
0
        opcode[1] = 3;
16812
        /* Insert an unconditional jump.  */
16813
0
        opcode[2] = 0xe9;
16814
        /* We added two extra opcode bytes, and have a two byte
16815
     offset.  */
16816
0
        fragP->fr_fix += 2 + 2;
16817
0
        fix_new (fragP, old_fr_fix + 2, 2,
16818
0
           fragP->fr_symbol,
16819
0
           fragP->fr_offset, 1,
16820
0
           _reloc (size, 1, 1, reloc_type,
16821
0
             fragP->tc_frag_data.code == CODE_64BIT,
16822
0
             fragP->fr_file, fragP->fr_line));
16823
0
        break;
16824
0
      }
16825
    /* Fall through.  */
16826
16827
0
  case COND_JUMP:
16828
    /* This changes the byte-displacement jump 0x7N
16829
       to the (d)word-displacement jump 0x0f,0x8N.  */
16830
0
    opcode[1] = opcode[0] + 0x10;
16831
0
    opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
16832
    /* We've added an opcode byte.  */
16833
0
    fragP->fr_fix += 1 + size;
16834
0
    fixP = fix_new (fragP, old_fr_fix + 1, size,
16835
0
        fragP->fr_symbol,
16836
0
        fragP->fr_offset, 1,
16837
0
        _reloc (size, 1, 1, reloc_type,
16838
0
          fragP->tc_frag_data.code == CODE_64BIT,
16839
0
          fragP->fr_file, fragP->fr_line));
16840
0
    break;
16841
16842
0
  default:
16843
0
    BAD_CASE (fragP->fr_subtype);
16844
0
    break;
16845
0
  }
16846
16847
      /* All jumps handled here are signed, but don't unconditionally use a
16848
   signed limit check for 32 and 16 bit jumps as we want to allow wrap
16849
   around at 4G (outside of 64-bit mode) and 64k.  */
16850
0
      if (size == 4 && flag_code == CODE_64BIT)
16851
0
  fixP->fx_signed = 1;
16852
16853
0
      frag_wane (fragP);
16854
0
      return fragP->fr_fix - old_fr_fix;
16855
0
    }
16856
16857
  /* Guess size depending on current relax state.  Initially the relax
16858
     state will correspond to a short jump and we return 1, because
16859
     the variable part of the frag (the branch offset) is one byte
16860
     long.  However, we can relax a section more than once and in that
16861
     case we must either set fr_subtype back to the unrelaxed state,
16862
     or return the value for the appropriate branch.  */
16863
0
  return md_relax_table[fragP->fr_subtype].rlx_length;
16864
0
}
16865
16866
/* Called after relax() is finished.
16867
16868
   In:  Address of frag.
16869
  fr_type == rs_machine_dependent.
16870
  fr_subtype is what the address relaxed to.
16871
16872
   Out: Any fixSs and constants are set up.
16873
  Caller will turn frag into a ".space 0".  */
16874
16875
void
16876
md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
16877
                 fragS *fragP)
16878
0
{
16879
0
  unsigned char *opcode;
16880
0
  unsigned char *where_to_put_displacement = NULL;
16881
0
  offsetT target_address;
16882
0
  offsetT opcode_address;
16883
0
  unsigned int extension = 0;
16884
0
  offsetT displacement_from_opcode_start;
16885
16886
0
  if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
16887
0
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
16888
0
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
16889
0
    {
16890
      /* Generate nop padding.  */
16891
0
      unsigned int size = fragP->tc_frag_data.length;
16892
0
      if (size)
16893
0
  {
16894
0
    if (size > fragP->tc_frag_data.max_bytes)
16895
0
      abort ();
16896
16897
0
    if (flag_debug)
16898
0
      {
16899
0
        const char *msg;
16900
0
        const char *branch = "branch";
16901
0
        const char *prefix = "";
16902
0
        fragS *padding_fragP;
16903
0
        if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
16904
0
      == BRANCH_PREFIX)
16905
0
    {
16906
0
      padding_fragP = fragP->tc_frag_data.u.padding_fragP;
16907
0
      switch (fragP->tc_frag_data.default_prefix)
16908
0
        {
16909
0
        default:
16910
0
          abort ();
16911
0
          break;
16912
0
        case CS_PREFIX_OPCODE:
16913
0
          prefix = " cs";
16914
0
          break;
16915
0
        case DS_PREFIX_OPCODE:
16916
0
          prefix = " ds";
16917
0
          break;
16918
0
        case ES_PREFIX_OPCODE:
16919
0
          prefix = " es";
16920
0
          break;
16921
0
        case FS_PREFIX_OPCODE:
16922
0
          prefix = " fs";
16923
0
          break;
16924
0
        case GS_PREFIX_OPCODE:
16925
0
          prefix = " gs";
16926
0
          break;
16927
0
        case SS_PREFIX_OPCODE:
16928
0
          prefix = " ss";
16929
0
          break;
16930
0
        }
16931
0
      if (padding_fragP)
16932
0
        msg = _("%s:%u: add %d%s at 0x%llx to align "
16933
0
          "%s within %d-byte boundary\n");
16934
0
      else
16935
0
        msg = _("%s:%u: add additional %d%s at 0x%llx to "
16936
0
          "align %s within %d-byte boundary\n");
16937
0
    }
16938
0
        else
16939
0
    {
16940
0
      padding_fragP = fragP;
16941
0
      msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
16942
0
        "%s within %d-byte boundary\n");
16943
0
    }
16944
16945
0
        if (padding_fragP)
16946
0
    switch (padding_fragP->tc_frag_data.branch_type)
16947
0
      {
16948
0
      case align_branch_jcc:
16949
0
        branch = "jcc";
16950
0
        break;
16951
0
      case align_branch_fused:
16952
0
        branch = "fused jcc";
16953
0
        break;
16954
0
      case align_branch_jmp:
16955
0
        branch = "jmp";
16956
0
        break;
16957
0
      case align_branch_call:
16958
0
        branch = "call";
16959
0
        break;
16960
0
      case align_branch_indirect:
16961
0
        branch = "indiret branch";
16962
0
        break;
16963
0
      case align_branch_ret:
16964
0
        branch = "ret";
16965
0
        break;
16966
0
      default:
16967
0
        break;
16968
0
      }
16969
16970
0
        fprintf (stdout, msg,
16971
0
           fragP->fr_file, fragP->fr_line, size, prefix,
16972
0
           (long long) fragP->fr_address, branch,
16973
0
           1 << align_branch_power);
16974
0
      }
16975
0
    if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
16976
0
      memset (fragP->fr_opcode,
16977
0
        fragP->tc_frag_data.default_prefix, size);
16978
0
    else
16979
0
      i386_generate_nops (fragP, (char *) fragP->fr_opcode,
16980
0
        size, 0);
16981
0
    fragP->fr_fix += size;
16982
0
  }
16983
0
      return;
16984
0
    }
16985
16986
0
  opcode = (unsigned char *) fragP->fr_opcode;
16987
16988
  /* Address we want to reach in file space.  */
16989
0
  target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
16990
16991
  /* Address opcode resides at in file space.  */
16992
0
  opcode_address = fragP->fr_address + fragP->fr_fix;
16993
16994
  /* Displacement from opcode start to fill into instruction.  */
16995
0
  displacement_from_opcode_start = target_address - opcode_address;
16996
16997
0
  if ((fragP->fr_subtype & BIG) == 0)
16998
0
    {
16999
      /* Don't have to change opcode.  */
17000
0
      extension = 1;    /* 1 opcode + 1 displacement  */
17001
0
      where_to_put_displacement = &opcode[1];
17002
0
    }
17003
0
  else
17004
0
    {
17005
0
      if (fragP->tc_frag_data.no_cond_jump_promotion
17006
0
    && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
17007
0
  as_warn_where (fragP->fr_file, fragP->fr_line,
17008
0
           _("long jump required"));
17009
17010
0
      switch (fragP->fr_subtype)
17011
0
  {
17012
0
  case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
17013
0
    extension = 4;    /* 1 opcode + 4 displacement  */
17014
0
    opcode[0] = 0xe9;
17015
0
    where_to_put_displacement = &opcode[1];
17016
0
    break;
17017
17018
0
  case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
17019
0
    extension = 2;    /* 1 opcode + 2 displacement  */
17020
0
    opcode[0] = 0xe9;
17021
0
    where_to_put_displacement = &opcode[1];
17022
0
    break;
17023
17024
0
  case ENCODE_RELAX_STATE (COND_JUMP, BIG):
17025
0
  case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
17026
0
    extension = 5;    /* 2 opcode + 4 displacement  */
17027
0
    opcode[1] = opcode[0] + 0x10;
17028
0
    opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
17029
0
    where_to_put_displacement = &opcode[2];
17030
0
    break;
17031
17032
0
  case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
17033
0
    extension = 3;    /* 2 opcode + 2 displacement  */
17034
0
    opcode[1] = opcode[0] + 0x10;
17035
0
    opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
17036
0
    where_to_put_displacement = &opcode[2];
17037
0
    break;
17038
17039
0
  case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
17040
0
    extension = 4;
17041
0
    opcode[0] ^= 1;
17042
0
    opcode[1] = 3;
17043
0
    opcode[2] = 0xe9;
17044
0
    where_to_put_displacement = &opcode[3];
17045
0
    break;
17046
17047
0
  default:
17048
0
    BAD_CASE (fragP->fr_subtype);
17049
0
    break;
17050
0
  }
17051
0
    }
17052
17053
  /* If size if less then four we are sure that the operand fits,
17054
     but if it's 4, then it could be that the displacement is larger
17055
     then -/+ 2GB.  */
17056
0
  if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
17057
0
      && object_64bit
17058
0
      && ((addressT) (displacement_from_opcode_start - extension
17059
0
          + ((addressT) 1 << 31))
17060
0
    > (((addressT) 2 << 31) - 1)))
17061
0
    {
17062
0
      as_bad_where (fragP->fr_file, fragP->fr_line,
17063
0
        _("jump target out of range"));
17064
      /* Make us emit 0.  */
17065
0
      displacement_from_opcode_start = extension;
17066
0
    }
17067
  /* Now put displacement after opcode.  */
17068
0
  md_number_to_chars ((char *) where_to_put_displacement,
17069
0
          displacement_from_opcode_start - extension,
17070
0
          DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
17071
0
  fragP->fr_fix += extension;
17072
0
}
17073

17074
/* Apply a fixup (fixP) to segment data, once it has been determined
17075
   by our caller that we have all the info we need to fix it up.
17076
17077
   Parameter valP is the pointer to the value of the bits.
17078
17079
   On the 386, immediates, displacements, and data pointers are all in
17080
   the same (little-endian) format, so we don't need to care about which
17081
   we are handling.  */
17082
17083
void
17084
md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
17085
0
{
17086
0
  char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
17087
0
  valueT value = *valP;
17088
17089
0
#if !defined (TE_Mach)
17090
0
  if (fixP->fx_pcrel)
17091
0
    {
17092
0
      switch (fixP->fx_r_type)
17093
0
  {
17094
0
  default:
17095
0
    break;
17096
17097
0
  case BFD_RELOC_64:
17098
0
    fixP->fx_r_type = BFD_RELOC_64_PCREL;
17099
0
    break;
17100
0
  case BFD_RELOC_32:
17101
0
  case BFD_RELOC_X86_64_32S:
17102
0
    fixP->fx_r_type = BFD_RELOC_32_PCREL;
17103
0
    break;
17104
0
  case BFD_RELOC_16:
17105
0
    fixP->fx_r_type = BFD_RELOC_16_PCREL;
17106
0
    break;
17107
0
  case BFD_RELOC_8:
17108
0
    fixP->fx_r_type = BFD_RELOC_8_PCREL;
17109
0
    break;
17110
0
  }
17111
0
    }
17112
17113
0
  if (fixP->fx_addsy != NULL
17114
0
      && (fixP->fx_r_type == BFD_RELOC_32_PCREL
17115
0
    || fixP->fx_r_type == BFD_RELOC_64_PCREL
17116
0
    || fixP->fx_r_type == BFD_RELOC_16_PCREL
17117
0
    || fixP->fx_r_type == BFD_RELOC_8_PCREL)
17118
0
      && !use_rela_relocations)
17119
0
    {
17120
      /* This is a hack.  There should be a better way to handle this.
17121
   This covers for the fact that bfd_install_relocation will
17122
   subtract the current location (for partial_inplace, PC relative
17123
   relocations); see more below.  */
17124
0
#if defined (OBJ_ELF) || defined (TE_PE)
17125
0
      value += fixP->fx_where + fixP->fx_frag->fr_address;
17126
0
#endif
17127
0
#ifdef OBJ_ELF
17128
0
      segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
17129
17130
0
      if ((sym_seg == seg
17131
0
     || (symbol_section_p (fixP->fx_addsy)
17132
0
         && sym_seg != absolute_section))
17133
0
    && !generic_force_reloc (fixP))
17134
0
  {
17135
    /* Yes, we add the values in twice.  This is because
17136
       bfd_install_relocation subtracts them out again.  I think
17137
       bfd_install_relocation is broken, but I don't dare change
17138
       it.  FIXME.  */
17139
0
    value += fixP->fx_where + fixP->fx_frag->fr_address;
17140
0
  }
17141
0
#endif
17142
#if defined (OBJ_COFF) && defined (TE_PE)
17143
      /* For some reason, the PE format does not store a
17144
   section address offset for a PC relative symbol.  */
17145
      if (S_GET_SEGMENT (fixP->fx_addsy) != seg
17146
    || S_IS_WEAK (fixP->fx_addsy))
17147
  value += md_pcrel_from (fixP);
17148
#endif
17149
0
    }
17150
#if defined (OBJ_COFF) && defined (TE_PE)
17151
  if (fixP->fx_addsy != NULL
17152
      && S_IS_WEAK (fixP->fx_addsy)
17153
      /* PR 16858: Do not modify weak function references.  */
17154
      && ! fixP->fx_pcrel)
17155
    {
17156
#if !defined (TE_PEP)
17157
      /* For x86 PE weak function symbols are neither PC-relative
17158
   nor do they set S_IS_FUNCTION.  So the only reliable way
17159
   to detect them is to check the flags of their containing
17160
   section.  */
17161
      if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
17162
    && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
17163
  ;
17164
      else
17165
#endif
17166
      value -= S_GET_VALUE (fixP->fx_addsy);
17167
    }
17168
#endif
17169
17170
  /* Fix a few things - the dynamic linker expects certain values here,
17171
     and we must not disappoint it.  */
17172
0
#ifdef OBJ_ELF
17173
0
  if (fixP->fx_addsy)
17174
0
    switch (fixP->fx_r_type)
17175
0
      {
17176
0
      case BFD_RELOC_386_PLT32:
17177
0
      case BFD_RELOC_32_PLT_PCREL:
17178
  /* Make the jump instruction point to the address of the operand.
17179
     At runtime we merely add the offset to the actual PLT entry.
17180
     NB: Subtract the offset size only for jump instructions.  */
17181
0
  if (fixP->fx_pcrel)
17182
0
    value = -4;
17183
0
  break;
17184
17185
0
      case BFD_RELOC_386_PC32_TO_PLT32:
17186
  /* Set the implicit addend.  */
17187
0
  value = (seg->vma - fixP->fx_size + fixP->fx_addnumber
17188
0
     + md_pcrel_from (fixP));
17189
0
  break;
17190
17191
0
      case BFD_RELOC_386_TLS_GD:
17192
0
      case BFD_RELOC_386_TLS_LDM:
17193
0
      case BFD_RELOC_386_TLS_IE_32:
17194
0
      case BFD_RELOC_386_TLS_IE:
17195
0
      case BFD_RELOC_386_TLS_GOTIE:
17196
0
      case BFD_RELOC_386_TLS_GOTDESC:
17197
0
      case BFD_RELOC_X86_64_TLSGD:
17198
0
      case BFD_RELOC_X86_64_TLSLD:
17199
0
      case BFD_RELOC_X86_64_GOTTPOFF:
17200
0
      case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
17201
0
      case BFD_RELOC_X86_64_CODE_5_GOTTPOFF:
17202
0
      case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
17203
0
      case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
17204
0
      case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
17205
0
      case BFD_RELOC_X86_64_CODE_5_GOTPC32_TLSDESC:
17206
0
      case BFD_RELOC_X86_64_CODE_6_GOTPC32_TLSDESC:
17207
0
  value = 0; /* Fully resolved at runtime.  No addend.  */
17208
  /* Fallthrough */
17209
0
      case BFD_RELOC_386_TLS_LE:
17210
0
      case BFD_RELOC_386_TLS_LDO_32:
17211
0
      case BFD_RELOC_386_TLS_LE_32:
17212
0
      case BFD_RELOC_X86_64_DTPOFF32:
17213
0
      case BFD_RELOC_X86_64_DTPOFF64:
17214
0
      case BFD_RELOC_X86_64_TPOFF32:
17215
0
      case BFD_RELOC_X86_64_TPOFF64:
17216
0
  S_SET_THREAD_LOCAL (fixP->fx_addsy);
17217
0
  break;
17218
17219
0
      case BFD_RELOC_386_TLS_DESC_CALL:
17220
0
      case BFD_RELOC_X86_64_TLSDESC_CALL:
17221
0
  value = 0; /* Fully resolved at runtime.  No addend.  */
17222
0
  S_SET_THREAD_LOCAL (fixP->fx_addsy);
17223
0
  fixP->fx_done = 0;
17224
0
  return;
17225
17226
0
      case BFD_RELOC_VTABLE_INHERIT:
17227
0
      case BFD_RELOC_VTABLE_ENTRY:
17228
0
  fixP->fx_done = 0;
17229
0
  return;
17230
17231
0
      default:
17232
0
  break;
17233
0
      }
17234
0
#endif /* OBJ_ELF  */
17235
17236
  /* If not 64bit, massage value, to account for wraparound when !BFD64.  */
17237
0
  if (!object_64bit)
17238
0
    value = extend_to_32bit_address (value);
17239
17240
0
  *valP = value;
17241
0
#endif /* !defined (TE_Mach)  */
17242
17243
  /* Are we finished with this relocation now?  */
17244
0
  if (fixP->fx_addsy == NULL)
17245
0
    {
17246
0
      fixP->fx_done = 1;
17247
0
      switch (fixP->fx_r_type)
17248
0
  {
17249
0
  case BFD_RELOC_X86_64_32S:
17250
0
    fixP->fx_signed = 1;
17251
0
    break;
17252
17253
0
  default:
17254
0
    break;
17255
0
  }
17256
0
    }
17257
#if defined (OBJ_COFF) && defined (TE_PE)
17258
  else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
17259
    {
17260
      fixP->fx_done = 0;
17261
      /* Remember value for tc_gen_reloc.  */
17262
      fixP->fx_addnumber = value;
17263
      /* Clear out the frag for now.  */
17264
      value = 0;
17265
    }
17266
#endif
17267
0
  else if (use_rela_relocations)
17268
0
    {
17269
0
      if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
17270
0
  fixP->fx_no_overflow = 1;
17271
      /* Remember value for tc_gen_reloc.  */
17272
0
      fixP->fx_addnumber = value;
17273
0
      value = 0;
17274
0
    }
17275
17276
0
  md_number_to_chars (p, value, fixP->fx_size);
17277
0
}
17278

17279
const char *
17280
md_atof (int type, char *litP, int *sizeP)
17281
173
{
17282
  /* This outputs the LITTLENUMs in REVERSE order;
17283
     in accord with the bigendian 386.  */
17284
173
  return ieee_md_atof (type, litP, sizeP, false);
17285
173
}
17286

17287
static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
17288
17289
static char *
17290
output_invalid (int c)
17291
75.7k
{
17292
75.7k
  if (ISPRINT (c))
17293
35.8k
    snprintf (output_invalid_buf, sizeof (output_invalid_buf),
17294
35.8k
        "'%c'", c);
17295
39.9k
  else
17296
39.9k
    snprintf (output_invalid_buf, sizeof (output_invalid_buf),
17297
39.9k
        "(0x%x)", (unsigned char) c);
17298
75.7k
  return output_invalid_buf;
17299
75.7k
}
17300
17301
/* Verify that @r can be used in the current context.  */
17302
17303
static bool check_register (const reg_entry *r)
17304
14.6k
{
17305
14.6k
  if (allow_pseudo_reg)
17306
3
    return true;
17307
17308
14.6k
  if (operand_type_all_zero (&r->reg_type))
17309
0
    return false;
17310
17311
14.6k
  if ((r->reg_type.bitfield.dword
17312
12.2k
       || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
17313
11.1k
       || r->reg_type.bitfield.class == RegCR
17314
11.1k
       || r->reg_type.bitfield.class == RegDR)
17315
4.78k
      && !cpu_arch_flags.bitfield.cpui386)
17316
0
    return false;
17317
17318
14.6k
  if (r->reg_type.bitfield.class == RegTR
17319
0
      && (flag_code == CODE_64BIT
17320
0
    || !cpu_arch_flags.bitfield.cpui386
17321
0
    || cpu_arch_isa_flags.bitfield.cpui586
17322
0
    || cpu_arch_isa_flags.bitfield.cpui686))
17323
0
    return false;
17324
17325
14.6k
  if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
17326
0
    return false;
17327
17328
14.6k
  if (!cpu_arch_flags.bitfield.cpuavx512f)
17329
861
    {
17330
861
      if (r->reg_type.bitfield.zmmword
17331
743
    || r->reg_type.bitfield.class == RegMask)
17332
118
  return false;
17333
17334
743
      if (!cpu_arch_flags.bitfield.cpuavx)
17335
0
  {
17336
0
    if (r->reg_type.bitfield.ymmword)
17337
0
      return false;
17338
17339
0
    if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
17340
0
      return false;
17341
0
  }
17342
743
    }
17343
17344
14.5k
  if (r->reg_type.bitfield.zmmword)
17345
354
    {
17346
354
      if (vector_size < VSZ512)
17347
236
  return false;
17348
17349
      /* Don't update pp when not dealing with insn operands.  */
17350
118
      switch (current_templates.start ? pp.encoding : encoding_evex)
17351
118
  {
17352
118
  case encoding_default:
17353
118
  case encoding_egpr:
17354
118
    pp.encoding = encoding_evex512;
17355
118
    break;
17356
0
  case encoding_evex:
17357
0
  case encoding_evex512:
17358
0
    break;
17359
0
  default:
17360
0
    pp.encoding = encoding_error;
17361
0
    break;
17362
118
  }
17363
118
    }
17364
17365
14.2k
  if (vector_size < VSZ256 && r->reg_type.bitfield.ymmword)
17366
15
    return false;
17367
17368
14.2k
  if (r->reg_type.bitfield.tmmword
17369
1
      && (!cpu_arch_flags.bitfield.cputile
17370
0
          || flag_code != CODE_64BIT))
17371
1
    return false;
17372
17373
14.2k
  if (r->reg_type.bitfield.class == RegSIMD
17374
860
      && r->reg_type.bitfield.byte
17375
0
      && (!cpu_arch_flags.bitfield.cpuace_v1
17376
0
    || flag_code != CODE_64BIT))
17377
0
    return false;
17378
17379
14.2k
  if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
17380
0
    return false;
17381
17382
  /* Don't allow fake index register unless allow_index_reg isn't 0. */
17383
14.2k
  if (!allow_index_reg && r->reg_num == RegIZ)
17384
0
    return false;
17385
17386
  /* Upper 16 vector registers are only available with VREX in 64bit
17387
     mode, and require EVEX encoding.  */
17388
14.2k
  if (r->reg_flags & RegVRex)
17389
108
    {
17390
108
      if (!cpu_arch_flags.bitfield.cpuavx512f
17391
98
    || flag_code != CODE_64BIT)
17392
10
  return false;
17393
17394
      /* Don't update pp when not dealing with insn operands.  */
17395
98
      switch (current_templates.start ? pp.encoding : encoding_evex)
17396
98
  {
17397
2
    case encoding_default:
17398
2
    case encoding_egpr:
17399
2
    case encoding_evex512:
17400
2
      pp.encoding = encoding_evex;
17401
2
      break;
17402
90
    case encoding_evex:
17403
90
      break;
17404
6
    default:
17405
6
      pp.encoding = encoding_error;
17406
6
      break;
17407
98
  }
17408
98
    }
17409
17410
14.2k
  if (r->reg_flags & RegRex2)
17411
234
    {
17412
234
      if (!cpu_arch_flags.bitfield.cpuapx_f
17413
1
    || flag_code != CODE_64BIT)
17414
233
  return false;
17415
17416
      /* Don't update pp when not dealing with insn operands.  */
17417
1
      switch (current_templates.start ? pp.encoding : encoding_egpr)
17418
1
  {
17419
1
  case encoding_default:
17420
1
    pp.encoding = encoding_egpr;
17421
1
    break;
17422
0
  case encoding_egpr:
17423
0
  case encoding_evex:
17424
0
  case encoding_evex512:
17425
0
    break;
17426
0
  default:
17427
0
    pp.encoding = encoding_error;
17428
0
    break;
17429
1
  }
17430
1
    }
17431
17432
14.0k
  if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
17433
1.53k
      && flag_code != CODE_64BIT
17434
15
      && (!cpu_arch_flags.bitfield.cpualtmovcr8
17435
0
    || r->reg_type.bitfield.class != RegCR
17436
0
    || r->reg_num != 0
17437
0
    || dot_insn ()))
17438
15
    return false;
17439
17440
13.9k
  if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
17441
4
      && !intel_syntax)
17442
0
    return false;
17443
17444
13.9k
  return true;
17445
13.9k
}
17446
17447
/* REG_STRING starts *before* REGISTER_PREFIX.  */
17448
17449
static const reg_entry *
17450
parse_real_register (const char *reg_string, char **end_op)
17451
6.65k
{
17452
6.65k
  const char *s = reg_string;
17453
6.65k
  char *p;
17454
6.65k
  char reg_name_given[MAX_REG_NAME_SIZE + 1];
17455
6.65k
  const reg_entry *r;
17456
17457
  /* Skip possible REGISTER_PREFIX and possible whitespace.  */
17458
6.65k
  if (*s == REGISTER_PREFIX)
17459
6.54k
    ++s;
17460
17461
6.65k
  if (is_whitespace (*s))
17462
20
    ++s;
17463
17464
6.65k
  p = reg_name_given;
17465
23.8k
  while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
17466
17.3k
    {
17467
17.3k
      if (p >= reg_name_given + MAX_REG_NAME_SIZE)
17468
245
  return NULL;
17469
17.1k
      s++;
17470
17.1k
    }
17471
17472
6.41k
  if (is_part_of_name (*s))
17473
321
    return NULL;
17474
17475
6.09k
  *end_op = (char *) s;
17476
17477
6.09k
  r = str_hash_find (reg_hash, reg_name_given);
17478
17479
  /* Handle floating point regs, allowing spaces in the (i) part.  */
17480
6.09k
  if (r == reg_st0)
17481
143
    {
17482
143
      if (!cpu_arch_flags.bitfield.cpu8087
17483
8
    && !cpu_arch_flags.bitfield.cpu287
17484
8
    && !cpu_arch_flags.bitfield.cpu387
17485
0
    && !allow_pseudo_reg)
17486
0
  return NULL;
17487
17488
143
      if (is_whitespace (*s))
17489
140
  ++s;
17490
143
      if (*s == '(')
17491
0
  {
17492
0
    ++s;
17493
0
    if (is_whitespace (*s))
17494
0
      ++s;
17495
0
    if (*s >= '0' && *s <= '7')
17496
0
      {
17497
0
        int fpr = *s - '0';
17498
0
        ++s;
17499
0
        if (is_whitespace (*s))
17500
0
    ++s;
17501
0
        if (*s == ')')
17502
0
    {
17503
0
      *end_op = (char *) s + 1;
17504
0
      know (r[fpr].reg_num == fpr);
17505
0
      return r + fpr;
17506
0
    }
17507
0
      }
17508
    /* We have "%st(" then garbage.  */
17509
0
    return NULL;
17510
0
  }
17511
143
    }
17512
17513
6.09k
  return r && check_register (r) ? r : NULL;
17514
6.09k
}
17515
17516
/* REG_STRING starts *before* REGISTER_PREFIX.  */
17517
17518
static const reg_entry *
17519
parse_register (const char *reg_string, char **end_op)
17520
6.48k
{
17521
6.48k
  const reg_entry *r;
17522
17523
6.48k
  if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
17524
77
    r = parse_real_register (reg_string, end_op);
17525
6.41k
  else
17526
6.41k
    r = NULL;
17527
6.48k
  if (!r)
17528
6.41k
    {
17529
6.41k
      char *save = input_line_pointer;
17530
6.41k
      char *buf = xstrdup (reg_string), *name;
17531
6.41k
      symbolS *symbolP;
17532
6.41k
      offsetT off;
17533
17534
6.41k
      input_line_pointer = buf;
17535
6.41k
      get_symbol_name (&name);
17536
6.41k
      symbolP = symbol_find (name);
17537
6.41k
      symbolP = symbol_equated_to (symbolP, &off);
17538
6.41k
      if (symbolP && off == 0 && S_GET_SEGMENT (symbolP) == reg_section)
17539
3.35k
  {
17540
3.35k
    const expressionS *e = symbol_get_value_expression (symbolP);
17541
17542
3.35k
    if (e->X_op == O_register)
17543
3.35k
      {
17544
3.35k
        know ((valueT) e->X_add_number < i386_regtab_size);
17545
3.35k
        r = i386_regtab + e->X_add_number;
17546
3.35k
        *end_op = (char *) reg_string + (input_line_pointer - buf);
17547
3.35k
      }
17548
3.35k
    if (r && !check_register (r))
17549
0
      {
17550
0
        as_bad (_("register '%s%s' cannot be used here"),
17551
0
          register_prefix, r->reg_name);
17552
0
        r = &bad_reg;
17553
0
      }
17554
3.35k
  }
17555
6.41k
      input_line_pointer = save;
17556
6.41k
      free (buf);
17557
6.41k
    }
17558
6.48k
  return r;
17559
6.48k
}
17560
17561
int
17562
i386_parse_name (char *name,
17563
     expressionS *e,
17564
     enum expr_mode mode,
17565
     char *nextcharP)
17566
66.2k
{
17567
66.2k
  const reg_entry *r = NULL;
17568
66.2k
  char *end = input_line_pointer;
17569
17570
  /* We only know the terminating character here.  It being double quote could
17571
     be the closing one of a quoted symbol name, or an opening one from a
17572
     following string (or another quoted symbol name).  Since the latter can't
17573
     be valid syntax for anything, bailing in either case is good enough.  */
17574
66.2k
  if (*nextcharP == '"')
17575
20.4k
    return 0;
17576
17577
45.7k
  *end = *nextcharP;
17578
45.7k
  if (*name == REGISTER_PREFIX || allow_naked_reg)
17579
115
    r = parse_real_register (name, &input_line_pointer);
17580
45.7k
  if (r && end <= input_line_pointer)
17581
0
    {
17582
0
      *nextcharP = *input_line_pointer;
17583
0
      *input_line_pointer = 0;
17584
0
      e->X_op = O_register;
17585
0
      e->X_add_number = r - i386_regtab;
17586
0
      return 1;
17587
0
    }
17588
45.7k
  input_line_pointer = end;
17589
45.7k
  *end = 0;
17590
45.7k
  return intel_syntax ? i386_intel_parse_name (name, e, mode) : 0;
17591
45.7k
}
17592
17593
void
17594
md_operand (expressionS *e)
17595
29.8k
{
17596
29.8k
  char *end;
17597
29.8k
  const reg_entry *r;
17598
17599
29.8k
  switch (*input_line_pointer)
17600
29.8k
    {
17601
6.46k
    case REGISTER_PREFIX:
17602
6.46k
      r = parse_real_register (input_line_pointer, &end);
17603
6.46k
      if (r)
17604
4.32k
  {
17605
4.32k
    e->X_op = O_register;
17606
4.32k
    e->X_add_number = r - i386_regtab;
17607
4.32k
    input_line_pointer = end;
17608
4.32k
  }
17609
6.46k
      break;
17610
17611
1.96k
    case '[':
17612
1.96k
      gas_assert (intel_syntax);
17613
1.96k
      end = input_line_pointer++;
17614
1.96k
      expression (e);
17615
1.96k
      if (*input_line_pointer == ']')
17616
0
  {
17617
0
    ++input_line_pointer;
17618
0
    e->X_op_symbol = make_expr_symbol (e);
17619
0
    e->X_add_symbol = NULL;
17620
0
    e->X_add_number = 0;
17621
0
    e->X_op = O_index;
17622
0
  }
17623
1.96k
      else
17624
1.96k
  {
17625
1.96k
    e->X_op = O_absent;
17626
1.96k
    input_line_pointer = end;
17627
1.96k
  }
17628
1.96k
      break;
17629
29.8k
    }
17630
29.8k
}
17631
17632
#ifdef BFD64
17633
/* To maintain consistency with !BFD64 builds of gas record, whether any
17634
   (binary) operator was involved in an expression.  As expressions are
17635
   evaluated in only 32 bits when !BFD64, we use this to decide whether to
17636
   truncate results.  */
17637
bool i386_record_operator (operatorT op,
17638
         const expressionS *left,
17639
         const expressionS *right)
17640
83.0k
{
17641
83.0k
  if (op == O_absent)
17642
2.72k
    return false;
17643
17644
80.3k
  if (!left)
17645
34.9k
    {
17646
      /* Since the expression parser applies unary operators fine to bignum
17647
   operands, we don't need to be concerned of respective operands not
17648
   fitting in 32 bits.  */
17649
34.9k
      if (right->X_op == O_constant && right->X_unsigned
17650
12.5k
    && !fits_in_unsigned_long (right->X_add_number))
17651
278
  return false;
17652
34.9k
    }
17653
  /* This isn't entirely right: The pattern can also result when constant
17654
     expressions are folded (e.g. 0xffffffff + 1).  */
17655
45.3k
  else if ((left->X_op == O_constant && left->X_unsigned
17656
7.31k
      && !fits_in_unsigned_long (left->X_add_number))
17657
44.8k
     || (right->X_op == O_constant && right->X_unsigned
17658
27.0k
         && !fits_in_unsigned_long (right->X_add_number)))
17659
590
    expr_mode = expr_large_value;
17660
17661
80.0k
  if (expr_mode != expr_large_value)
17662
78.0k
    expr_mode = expr_operator_present;
17663
17664
80.0k
  return false;
17665
80.3k
}
17666
#endif
17667

17668
const char md_shortopts[] =
17669
#ifdef OBJ_ELF
17670
  "kVQ:"
17671
# ifdef TE_SOLARIS
17672
  "s"
17673
# endif
17674
#endif
17675
  "qnO::";
17676
17677
0
#define OPTION_32 (OPTION_MD_BASE + 0)
17678
0
#define OPTION_64 (OPTION_MD_BASE + 1)
17679
0
#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
17680
0
#define OPTION_MARCH (OPTION_MD_BASE + 3)
17681
0
#define OPTION_MTUNE (OPTION_MD_BASE + 4)
17682
0
#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
17683
0
#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
17684
0
#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
17685
0
#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
17686
0
#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
17687
0
#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
17688
0
#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
17689
0
#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
17690
0
#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
17691
0
#define OPTION_X32 (OPTION_MD_BASE + 14)
17692
0
#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
17693
0
#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
17694
0
#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
17695
#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
17696
0
#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
17697
0
#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
17698
0
#define OPTION_MSHARED (OPTION_MD_BASE + 21)
17699
0
#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
17700
0
#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
17701
0
#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
17702
0
#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
17703
0
#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
17704
0
#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
17705
0
#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
17706
0
#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
17707
0
#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
17708
0
#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
17709
0
#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
17710
0
#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
17711
0
#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
17712
0
#define OPTION_MTLS_CHECK (OPTION_MD_BASE + 35)
17713
17714
const struct option md_longopts[] =
17715
{
17716
  {"32", no_argument, NULL, OPTION_32},
17717
#if (defined (OBJ_ELF) || defined (TE_PE) || defined (OBJ_MACH_O)) \
17718
    && defined (BFD64)
17719
  {"64", no_argument, NULL, OPTION_64},
17720
#endif
17721
#ifdef OBJ_ELF
17722
# ifdef BFD64
17723
  {"x32", no_argument, NULL, OPTION_X32},
17724
# endif
17725
  {"mshared", no_argument, NULL, OPTION_MSHARED},
17726
  {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
17727
#endif
17728
  {"divide", no_argument, NULL, OPTION_DIVIDE},
17729
  {"march", required_argument, NULL, OPTION_MARCH},
17730
  {"mtune", required_argument, NULL, OPTION_MTUNE},
17731
  {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
17732
  {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
17733
  {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
17734
  {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
17735
  {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
17736
  {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
17737
  {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
17738
  {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
17739
  {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
17740
  {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
17741
  {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
17742
  {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
17743
  {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
17744
# if defined (TE_PE) || defined (TE_PEP)
17745
  {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
17746
#endif
17747
  {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
17748
  {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
17749
  {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
17750
  {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
17751
  {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
17752
  {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
17753
  {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
17754
  {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
17755
  {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
17756
  {"mlfence-before-indirect-branch", required_argument, NULL,
17757
   OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
17758
  {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
17759
  {"mamd64", no_argument, NULL, OPTION_MAMD64},
17760
  {"mintel64", no_argument, NULL, OPTION_MINTEL64},
17761
  {"mtls-check", required_argument, NULL, OPTION_MTLS_CHECK},
17762
  {NULL, no_argument, NULL, 0}
17763
};
17764
const size_t md_longopts_size = sizeof (md_longopts);
17765
17766
int
17767
md_parse_option (int c, const char *arg)
17768
0
{
17769
0
  unsigned int j;
17770
0
  char *arch, *next, *saved, *type;
17771
17772
0
  switch (c)
17773
0
    {
17774
0
    case 'n':
17775
0
      optimize_align_code = 0;
17776
0
      break;
17777
17778
0
    case 'q':
17779
0
      quiet_warnings = 1;
17780
0
      break;
17781
17782
0
#ifdef OBJ_ELF
17783
      /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
17784
   should be emitted or not.  FIXME: Not implemented.  */
17785
0
    case 'Q':
17786
0
      if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
17787
0
  return 0;
17788
0
      break;
17789
17790
      /* -V: SVR4 argument to print version ID.  */
17791
0
    case 'V':
17792
0
      print_version_id ();
17793
0
      break;
17794
17795
      /* -k: Ignore for FreeBSD compatibility.  */
17796
0
    case 'k':
17797
0
      break;
17798
17799
# ifdef TE_SOLARIS
17800
    case 's':
17801
      /* -s: On i386 Solaris, this tells the native assembler to use
17802
   .stab instead of .stab.excl.  We always use .stab anyhow.  */
17803
      break;
17804
# endif
17805
17806
0
    case OPTION_MSHARED:
17807
0
      shared = 1;
17808
0
      break;
17809
17810
0
    case OPTION_X86_USED_NOTE:
17811
0
      if (strcasecmp (arg, "yes") == 0)
17812
0
        x86_used_note = 1;
17813
0
      else if (strcasecmp (arg, "no") == 0)
17814
0
        x86_used_note = 0;
17815
0
      else
17816
0
        as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
17817
0
      break;
17818
0
#endif
17819
17820
0
#ifdef BFD64
17821
17822
0
#if (defined (OBJ_ELF) || defined (TE_PE) || defined (OBJ_MACH_O))
17823
0
    case OPTION_64:
17824
0
      {
17825
0
  const char **list, **l;
17826
17827
0
  list = bfd_target_list ();
17828
0
  for (l = list; *l != NULL; l++)
17829
0
#if defined (OBJ_ELF)
17830
0
    if (strcmp (*l, ELF_TARGET_FORMAT64) == 0)
17831
#elif defined (TE_PE)
17832
    if (strcmp (*l, "pe-x86-64") == 0)
17833
#else
17834
    if (strcmp (*l, "mach-o-x86-64") == 0)
17835
#endif
17836
0
      {
17837
0
        default_arch = "x86_64";
17838
0
        break;
17839
0
      }
17840
0
  if (*l == NULL)
17841
0
    as_fatal (_("no compiled in support for x86_64"));
17842
0
  free (list);
17843
0
      }
17844
0
      break;
17845
0
#endif
17846
17847
0
#ifdef OBJ_ELF
17848
0
    case OPTION_X32:
17849
0
      {
17850
0
  const char **list, **l;
17851
17852
0
  list = bfd_target_list ();
17853
0
  for (l = list; *l != NULL; l++)
17854
0
    if (strcmp (*l, ELF_TARGET_FORMAT32) == 0)
17855
0
      {
17856
0
        default_arch = "x86_64:32";
17857
0
        break;
17858
0
      }
17859
0
  if (*l == NULL)
17860
0
    as_fatal (_("no compiled in support for 32bit x86_64"));
17861
0
  free (list);
17862
0
      }
17863
0
      break;
17864
0
#endif
17865
17866
0
#endif /* BFD64 */
17867
17868
0
    case OPTION_32:
17869
0
      {
17870
0
  const char **list, **l;
17871
17872
0
  list = bfd_target_list ();
17873
0
  for (l = list; *l != NULL; l++)
17874
0
    if (strstr (*l, "-i386")
17875
0
        || strstr (*l, "-go32"))
17876
0
      {
17877
0
        default_arch = "i386";
17878
0
        break;
17879
0
      }
17880
0
  if (*l == NULL)
17881
0
    as_fatal (_("no compiled in support for ix86"));
17882
0
  free (list);
17883
0
      }
17884
0
      break;
17885
17886
0
    case OPTION_DIVIDE:
17887
#ifdef SVR4_COMMENT_CHARS
17888
      {
17889
  char *n, *t;
17890
  const char *s;
17891
17892
  n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
17893
  t = n;
17894
  for (s = i386_comment_chars; *s != '\0'; s++)
17895
    if (*s != '/')
17896
      *t++ = *s;
17897
  *t = '\0';
17898
  i386_comment_chars = n;
17899
      }
17900
#endif
17901
0
      break;
17902
17903
0
    case OPTION_MARCH:
17904
0
      saved = xstrdup (arg);
17905
0
      arch = saved;
17906
      /* Allow -march=+nosse.  */
17907
0
      if (*arch == '+')
17908
0
  arch++;
17909
0
      do
17910
0
  {
17911
0
    char *vsz;
17912
17913
0
    if (*arch == '.')
17914
0
      as_fatal (_("invalid -march= option: `%s'"), arg);
17915
0
    next = strchr (arch, '+');
17916
0
    if (next)
17917
0
      *next++ = '\0';
17918
0
    vsz = strchr (arch, '/');
17919
0
    if (vsz)
17920
0
      *vsz++ = '\0';
17921
0
    for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
17922
0
      {
17923
0
        if (vsz && cpu_arch[j].vsz != vsz_set)
17924
0
    continue;
17925
17926
0
        if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
17927
0
            && strcmp (arch, cpu_arch[j].name) == 0)
17928
0
    {
17929
      /* Processor.  */
17930
0
      if (! cpu_arch[j].enable.bitfield.cpui386)
17931
0
        continue;
17932
17933
0
      cpu_arch_name = cpu_arch[j].name;
17934
0
      free (cpu_sub_arch_name);
17935
0
      cpu_sub_arch_name = NULL;
17936
0
      cpu_arch_flags = cpu_arch[j].enable;
17937
0
      cpu_arch_isa = cpu_arch[j].type;
17938
0
      cpu_arch_isa_flags = cpu_arch[j].enable;
17939
0
      if (!cpu_arch_tune_set)
17940
0
        cpu_arch_tune = cpu_arch_isa;
17941
0
      vector_size = VSZ_DEFAULT;
17942
0
      break;
17943
0
    }
17944
0
        else if (cpu_arch[j].type == PROCESSOR_NONE
17945
0
           && strcmp (arch, cpu_arch[j].name) == 0
17946
0
           && !cpu_flags_all_zero (&cpu_arch[j].enable))
17947
0
    {
17948
      /* ISA extension.  */
17949
0
      isa_enable (j);
17950
17951
0
      switch (cpu_arch[j].vsz)
17952
0
        {
17953
0
        default:
17954
0
          break;
17955
17956
0
        case vsz_set:
17957
0
          if (vsz)
17958
0
      {
17959
0
        char *end;
17960
0
        unsigned long val = strtoul (vsz, &end, 0);
17961
17962
0
        if (*end)
17963
0
          val = 0;
17964
0
        switch (val)
17965
0
          {
17966
0
          case 512: vector_size = VSZ512; break;
17967
0
          case 256: vector_size = VSZ256; break;
17968
0
          case 128: vector_size = VSZ128; break;
17969
0
          default:
17970
0
            as_warn (_("Unrecognized vector size specifier ignored"));
17971
0
            break;
17972
0
          }
17973
0
        break;
17974
0
      }
17975
      /* Fall through.  */
17976
0
        case vsz_reset:
17977
0
          vector_size = VSZ_DEFAULT;
17978
0
          break;
17979
0
        }
17980
17981
0
      break;
17982
0
    }
17983
0
      }
17984
17985
0
    if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
17986
0
      {
17987
        /* Disable an ISA extension.  */
17988
0
        for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
17989
0
    if (cpu_arch[j].type == PROCESSOR_NONE
17990
0
        && strcmp (arch + 2, cpu_arch[j].name) == 0)
17991
0
      {
17992
0
        isa_disable (j);
17993
0
        if (cpu_arch[j].vsz == vsz_set)
17994
0
          vector_size = VSZ_DEFAULT;
17995
0
        break;
17996
0
      }
17997
0
      }
17998
17999
0
    if (j >= ARRAY_SIZE (cpu_arch))
18000
0
      as_fatal (_("invalid -march= option: `%s'"), arg);
18001
18002
0
    arch = next;
18003
0
  }
18004
0
      while (next != NULL);
18005
0
      free (saved);
18006
0
      break;
18007
18008
0
    case OPTION_MTUNE:
18009
0
      if (*arg == '.')
18010
0
  as_fatal (_("invalid -mtune= option: `%s'"), arg);
18011
0
      for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
18012
0
  {
18013
0
    if (cpu_arch[j].type != PROCESSOR_NONE
18014
0
        && strcmp (arg, cpu_arch[j].name) == 0)
18015
0
      {
18016
0
        cpu_arch_tune_set = 1;
18017
0
        cpu_arch_tune = cpu_arch [j].type;
18018
0
        break;
18019
0
      }
18020
0
  }
18021
0
      if (j >= ARRAY_SIZE (cpu_arch))
18022
0
  as_fatal (_("invalid -mtune= option: `%s'"), arg);
18023
0
      break;
18024
18025
0
    case OPTION_MMNEMONIC:
18026
0
      if (strcasecmp (arg, "att") == 0)
18027
0
  intel_mnemonic = 0;
18028
0
      else if (strcasecmp (arg, "intel") == 0)
18029
0
  intel_mnemonic = 1;
18030
0
      else
18031
0
  as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
18032
0
      break;
18033
18034
0
    case OPTION_MSYNTAX:
18035
0
      if (strcasecmp (arg, "att") == 0)
18036
0
  _set_intel_syntax (0);
18037
0
      else if (strcasecmp (arg, "intel") == 0)
18038
0
  _set_intel_syntax (1);
18039
0
      else
18040
0
  as_fatal (_("invalid -msyntax= option: `%s'"), arg);
18041
0
      break;
18042
18043
0
    case OPTION_MINDEX_REG:
18044
0
      allow_index_reg = 1;
18045
0
      break;
18046
18047
0
    case OPTION_MNAKED_REG:
18048
0
      allow_naked_reg = 1;
18049
0
      register_prefix = "";
18050
0
      break;
18051
18052
0
    case OPTION_MSSE2AVX:
18053
0
      sse2avx = 1;
18054
0
      break;
18055
18056
0
    case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
18057
0
      use_unaligned_vector_move = 1;
18058
0
      break;
18059
18060
0
    case OPTION_MSSE_CHECK:
18061
0
      if (strcasecmp (arg, "error") == 0)
18062
0
  sse_check = check_error;
18063
0
      else if (strcasecmp (arg, "warning") == 0)
18064
0
  sse_check = check_warning;
18065
0
      else if (strcasecmp (arg, "none") == 0)
18066
0
  sse_check = check_none;
18067
0
      else
18068
0
  as_fatal (_("invalid -msse-check= option: `%s'"), arg);
18069
0
      break;
18070
18071
0
    case OPTION_MOPERAND_CHECK:
18072
0
      if (strcasecmp (arg, "error") == 0)
18073
0
  operand_check = check_error;
18074
0
      else if (strcasecmp (arg, "warning") == 0)
18075
0
  operand_check = check_warning;
18076
0
      else if (strcasecmp (arg, "none") == 0)
18077
0
  operand_check = check_none;
18078
0
      else
18079
0
  as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
18080
0
      break;
18081
18082
0
    case OPTION_MAVXSCALAR:
18083
0
      if (strcasecmp (arg, "128") == 0)
18084
0
  avxscalar = vex128;
18085
0
      else if (strcasecmp (arg, "256") == 0)
18086
0
  avxscalar = vex256;
18087
0
      else
18088
0
  as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
18089
0
      break;
18090
18091
0
    case OPTION_MVEXWIG:
18092
0
      if (strcmp (arg, "0") == 0)
18093
0
  vexwig = vexw0;
18094
0
      else if (strcmp (arg, "1") == 0)
18095
0
  vexwig = vexw1;
18096
0
      else
18097
0
  as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
18098
0
      break;
18099
18100
0
    case OPTION_MADD_BND_PREFIX:
18101
0
      add_bnd_prefix = 1;
18102
0
      break;
18103
18104
0
    case OPTION_MEVEXLIG:
18105
0
      if (strcmp (arg, "128") == 0)
18106
0
  evexlig = evexl128;
18107
0
      else if (strcmp (arg, "256") == 0)
18108
0
  evexlig = evexl256;
18109
0
      else  if (strcmp (arg, "512") == 0)
18110
0
  evexlig = evexl512;
18111
0
      else
18112
0
  as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
18113
0
      break;
18114
18115
0
    case OPTION_MEVEXRCIG:
18116
0
      if (strcmp (arg, "rne") == 0)
18117
0
  evexrcig = rne;
18118
0
      else if (strcmp (arg, "rd") == 0)
18119
0
  evexrcig = rd;
18120
0
      else if (strcmp (arg, "ru") == 0)
18121
0
  evexrcig = ru;
18122
0
      else if (strcmp (arg, "rz") == 0)
18123
0
  evexrcig = rz;
18124
0
      else
18125
0
  as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
18126
0
      break;
18127
18128
0
    case OPTION_MEVEXWIG:
18129
0
      if (strcmp (arg, "0") == 0)
18130
0
  evexwig = evexw0;
18131
0
      else if (strcmp (arg, "1") == 0)
18132
0
  evexwig = evexw1;
18133
0
      else
18134
0
  as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
18135
0
      break;
18136
18137
# if defined (TE_PE) || defined (TE_PEP)
18138
    case OPTION_MBIG_OBJ:
18139
      use_big_obj = 1;
18140
      break;
18141
#endif
18142
18143
0
    case OPTION_MOMIT_LOCK_PREFIX:
18144
0
      if (strcasecmp (arg, "yes") == 0)
18145
0
        omit_lock_prefix = 1;
18146
0
      else if (strcasecmp (arg, "no") == 0)
18147
0
        omit_lock_prefix = 0;
18148
0
      else
18149
0
        as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
18150
0
      break;
18151
18152
0
    case OPTION_MFENCE_AS_LOCK_ADD:
18153
0
      if (strcasecmp (arg, "yes") == 0)
18154
0
        avoid_fence = 1;
18155
0
      else if (strcasecmp (arg, "no") == 0)
18156
0
        avoid_fence = 0;
18157
0
      else
18158
0
        as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
18159
0
      break;
18160
18161
0
    case OPTION_MLFENCE_AFTER_LOAD:
18162
0
      if (strcasecmp (arg, "yes") == 0)
18163
0
  lfence_after_load = 1;
18164
0
      else if (strcasecmp (arg, "no") == 0)
18165
0
  lfence_after_load = 0;
18166
0
      else
18167
0
        as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
18168
0
      break;
18169
18170
0
    case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
18171
0
      if (strcasecmp (arg, "all") == 0)
18172
0
  {
18173
0
    lfence_before_indirect_branch = lfence_branch_all;
18174
0
    if (lfence_before_ret == lfence_before_ret_none)
18175
0
      lfence_before_ret = lfence_before_ret_shl;
18176
0
  }
18177
0
      else if (strcasecmp (arg, "memory") == 0)
18178
0
  lfence_before_indirect_branch = lfence_branch_memory;
18179
0
      else if (strcasecmp (arg, "register") == 0)
18180
0
  lfence_before_indirect_branch = lfence_branch_register;
18181
0
      else if (strcasecmp (arg, "none") == 0)
18182
0
  lfence_before_indirect_branch = lfence_branch_none;
18183
0
      else
18184
0
        as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
18185
0
      arg);
18186
0
      break;
18187
18188
0
    case OPTION_MLFENCE_BEFORE_RET:
18189
0
      if (strcasecmp (arg, "or") == 0)
18190
0
  lfence_before_ret = lfence_before_ret_or;
18191
0
      else if (strcasecmp (arg, "not") == 0)
18192
0
  lfence_before_ret = lfence_before_ret_not;
18193
0
      else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
18194
0
  lfence_before_ret = lfence_before_ret_shl;
18195
0
      else if (strcasecmp (arg, "none") == 0)
18196
0
  lfence_before_ret = lfence_before_ret_none;
18197
0
      else
18198
0
        as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
18199
0
      arg);
18200
0
      break;
18201
18202
0
    case OPTION_MRELAX_RELOCATIONS:
18203
0
      if (strcasecmp (arg, "yes") == 0)
18204
0
        generate_relax_relocations = 1;
18205
0
      else if (strcasecmp (arg, "no") == 0)
18206
0
        generate_relax_relocations = 0;
18207
0
      else
18208
0
        as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
18209
0
      break;
18210
18211
0
    case OPTION_MALIGN_BRANCH_BOUNDARY:
18212
0
      {
18213
0
  char *end;
18214
0
  long int align = strtoul (arg, &end, 0);
18215
0
  if (*end == '\0')
18216
0
    {
18217
0
      if (align == 0)
18218
0
        {
18219
0
    align_branch_power = 0;
18220
0
    break;
18221
0
        }
18222
0
      else if (align >= 16)
18223
0
        {
18224
0
    int align_power;
18225
0
    for (align_power = 0;
18226
0
         (align & 1) == 0;
18227
0
         align >>= 1, align_power++)
18228
0
      continue;
18229
    /* Limit alignment power to 31.  */
18230
0
    if (align == 1 && align_power < 32)
18231
0
      {
18232
0
        align_branch_power = align_power;
18233
0
        break;
18234
0
      }
18235
0
        }
18236
0
    }
18237
0
  as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
18238
0
      }
18239
0
      break;
18240
18241
0
    case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
18242
0
      {
18243
0
  char *end;
18244
0
  int align = strtoul (arg, &end, 0);
18245
  /* Some processors only support 5 prefixes.  */
18246
0
  if (*end == '\0' && align >= 0 && align < 6)
18247
0
    {
18248
0
      align_branch_prefix_size = align;
18249
0
      break;
18250
0
    }
18251
0
  as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
18252
0
      arg);
18253
0
      }
18254
0
      break;
18255
18256
0
    case OPTION_MALIGN_BRANCH:
18257
0
      align_branch = 0;
18258
0
      saved = xstrdup (arg);
18259
0
      type = saved;
18260
0
      do
18261
0
  {
18262
0
    next = strchr (type, '+');
18263
0
    if (next)
18264
0
      *next++ = '\0';
18265
0
    if (strcasecmp (type, "jcc") == 0)
18266
0
      align_branch |= align_branch_jcc_bit;
18267
0
    else if (strcasecmp (type, "fused") == 0)
18268
0
      align_branch |= align_branch_fused_bit;
18269
0
    else if (strcasecmp (type, "jmp") == 0)
18270
0
      align_branch |= align_branch_jmp_bit;
18271
0
    else if (strcasecmp (type, "call") == 0)
18272
0
      align_branch |= align_branch_call_bit;
18273
0
    else if (strcasecmp (type, "ret") == 0)
18274
0
      align_branch |= align_branch_ret_bit;
18275
0
    else if (strcasecmp (type, "indirect") == 0)
18276
0
      align_branch |= align_branch_indirect_bit;
18277
0
    else
18278
0
      as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
18279
0
    type = next;
18280
0
  }
18281
0
      while (next != NULL);
18282
0
      free (saved);
18283
0
      break;
18284
18285
0
    case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
18286
0
      align_branch_power = 5;
18287
0
      align_branch_prefix_size = 5;
18288
0
      align_branch = (align_branch_jcc_bit
18289
0
          | align_branch_fused_bit
18290
0
          | align_branch_jmp_bit);
18291
0
      break;
18292
18293
0
    case OPTION_MAMD64:
18294
0
      isa64 = amd64;
18295
0
      break;
18296
18297
0
    case OPTION_MINTEL64:
18298
0
      isa64 = intel64;
18299
0
      break;
18300
18301
0
    case 'O':
18302
0
      if (arg == NULL)
18303
0
  {
18304
0
    optimize = 1;
18305
    /* Turn off -Os.  */
18306
0
    optimize_for_space = 0;
18307
0
  }
18308
0
      else if (*arg == 's')
18309
0
  {
18310
0
    optimize_for_space = 1;
18311
    /* Turn on all encoding optimizations.  */
18312
0
    optimize = INT_MAX;
18313
0
  }
18314
0
      else
18315
0
  {
18316
0
    optimize = atoi (arg);
18317
    /* Turn off -Os.  */
18318
0
    optimize_for_space = 0;
18319
0
  }
18320
0
      break;
18321
0
    case OPTION_MTLS_CHECK:
18322
0
      if (strcasecmp (arg, "yes") == 0)
18323
0
  tls_check = true;
18324
0
      else if (strcasecmp (arg, "no") == 0)
18325
0
  tls_check = false;
18326
0
      else
18327
0
  as_fatal (_("invalid -mtls-check= option: `%s'"), arg);
18328
0
      break;
18329
18330
0
    default:
18331
0
      return 0;
18332
0
    }
18333
0
  return 1;
18334
0
}
18335
18336
0
#define MESSAGE_TEMPLATE \
18337
0
"                                                                                "
18338
18339
static char *
18340
output_message (FILE *stream, char *p, char *message, char *start,
18341
    int *left_p, const char *name, int len)
18342
0
{
18343
0
  int size = sizeof (MESSAGE_TEMPLATE);
18344
0
  int left = *left_p;
18345
18346
  /* Reserve 2 spaces for ", " or ",\0" */
18347
0
  left -= len + 2;
18348
18349
  /* Check if there is any room.  */
18350
0
  if (left >= 0)
18351
0
    {
18352
0
      if (p != start)
18353
0
  {
18354
0
    *p++ = ',';
18355
0
    *p++ = ' ';
18356
0
  }
18357
0
      p = mempcpy (p, name, len);
18358
0
    }
18359
0
  else
18360
0
    {
18361
      /* Output the current message now and start a new one.  */
18362
0
      *p++ = ',';
18363
0
      *p = '\0';
18364
0
      fprintf (stream, "%s\n", message);
18365
0
      p = start;
18366
0
      left = size - (start - message) - len - 2;
18367
18368
0
      gas_assert (left >= 0);
18369
18370
0
      p = mempcpy (p, name, len);
18371
0
    }
18372
18373
0
  *left_p = left;
18374
0
  return p;
18375
0
}
18376
18377
static void
18378
show_arch (FILE *stream, int ext, int check)
18379
0
{
18380
0
  static char message[] = MESSAGE_TEMPLATE;
18381
0
  char *start = message + 27;
18382
0
  char *p;
18383
0
  int size = sizeof (MESSAGE_TEMPLATE);
18384
0
  int left;
18385
0
  const char *name;
18386
0
  int len;
18387
0
  unsigned int j;
18388
18389
0
  p = start;
18390
0
  left = size - (start - message);
18391
18392
0
  if (!ext && check)
18393
0
    {
18394
0
      p = output_message (stream, p, message, start, &left,
18395
0
        STRING_COMMA_LEN ("default"));
18396
0
      p = output_message (stream, p, message, start, &left,
18397
0
        STRING_COMMA_LEN ("push"));
18398
0
      p = output_message (stream, p, message, start, &left,
18399
0
        STRING_COMMA_LEN ("pop"));
18400
0
    }
18401
18402
0
  for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
18403
0
    {
18404
      /* Should it be skipped?  */
18405
0
      if (cpu_arch [j].skip)
18406
0
  continue;
18407
18408
0
      name = cpu_arch [j].name;
18409
0
      len = cpu_arch [j].len;
18410
0
      if (cpu_arch[j].type == PROCESSOR_NONE)
18411
0
  {
18412
    /* It is an extension.  Skip if we aren't asked to show it.  */
18413
0
    if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
18414
0
      continue;
18415
0
  }
18416
0
      else if (ext)
18417
0
  {
18418
    /* It is an processor.  Skip if we show only extension.  */
18419
0
    continue;
18420
0
  }
18421
0
      else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
18422
0
  {
18423
    /* It is an impossible processor - skip.  */
18424
0
    continue;
18425
0
  }
18426
18427
0
      p = output_message (stream, p, message, start, &left, name, len);
18428
0
    }
18429
18430
  /* Display disabled extensions.  */
18431
0
  if (ext)
18432
0
    for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
18433
0
      {
18434
0
  char *str;
18435
18436
0
  if (cpu_arch[j].type != PROCESSOR_NONE
18437
0
      || !cpu_flags_all_zero (&cpu_arch[j].enable))
18438
0
    continue;
18439
0
  str = xasprintf ("no%s", cpu_arch[j].name);
18440
0
  p = output_message (stream, p, message, start, &left, str,
18441
0
          strlen (str));
18442
0
  free (str);
18443
0
      }
18444
18445
0
  *p = '\0';
18446
0
  fprintf (stream, "%s\n", message);
18447
0
}
18448
18449
void
18450
md_show_usage (FILE *stream)
18451
0
{
18452
0
#ifdef OBJ_ELF
18453
0
  fprintf (stream, _("\
18454
0
  -Qy, -Qn                ignored\n\
18455
0
  -V                      print assembler version number\n\
18456
0
  -k                      ignored\n"));
18457
0
#endif
18458
0
  fprintf (stream, _("\
18459
0
  -n                      do not optimize code alignment\n\
18460
0
  -O{012s}                attempt some code optimizations\n\
18461
0
  -q                      quieten some warnings\n"));
18462
0
#ifdef OBJ_ELF
18463
0
  fprintf (stream, _("\
18464
0
  -s                      ignored\n"));
18465
0
#endif
18466
0
#ifdef BFD64
18467
0
# ifdef OBJ_ELF
18468
0
  fprintf (stream, _("\
18469
0
  --32/--64/--x32         generate 32bit/64bit/x32 object\n"));
18470
# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
18471
  fprintf (stream, _("\
18472
  --32/--64               generate 32bit/64bit object\n"));
18473
# endif
18474
0
#endif
18475
#ifdef SVR4_COMMENT_CHARS
18476
  fprintf (stream, _("\
18477
  --divide                do not treat `/' as a comment character\n"));
18478
#else
18479
0
  fprintf (stream, _("\
18480
0
  --divide                ignored\n"));
18481
0
#endif
18482
0
  fprintf (stream, _("\
18483
0
  -march=CPU[,+EXTENSION...]\n\
18484
0
                          generate code for CPU and EXTENSION, CPU is one of:\n"));
18485
0
  show_arch (stream, 0, 1);
18486
0
  fprintf (stream, _("\
18487
0
                          EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
18488
0
  show_arch (stream, 1, 0);
18489
0
  fprintf (stream, _("\
18490
0
  -mtune=CPU              optimize for CPU, CPU is one of:\n"));
18491
0
  show_arch (stream, 0, 0);
18492
0
  fprintf (stream, _("\
18493
0
  -msse2avx               encode SSE instructions with VEX prefix\n"));
18494
0
  fprintf (stream, _("\
18495
0
  -muse-unaligned-vector-move\n\
18496
0
                          encode aligned vector move as unaligned vector move\n"));
18497
0
  fprintf (stream, _("\
18498
0
  -msse-check=[none|error|warning] (default: none)\n\
18499
0
                          check SSE instructions\n"));
18500
0
  fprintf (stream, _("\
18501
0
  -moperand-check=[none|error|warning] (default: warning)\n\
18502
0
                          check operand combinations for validity\n"));
18503
0
  fprintf (stream, _("\
18504
0
  -mavxscalar=[128|256] (default: 128)\n\
18505
0
                          encode scalar AVX instructions with specific vector\n\
18506
0
                           length\n"));
18507
0
  fprintf (stream, _("\
18508
0
  -mvexwig=[0|1] (default: 0)\n\
18509
0
                          encode VEX instructions with specific VEX.W value\n\
18510
0
                           for VEX.W bit ignored instructions\n"));
18511
0
  fprintf (stream, _("\
18512
0
  -mevexlig=[128|256|512] (default: 128)\n\
18513
0
                          encode scalar EVEX instructions with specific vector\n\
18514
0
                           length\n"));
18515
0
  fprintf (stream, _("\
18516
0
  -mevexwig=[0|1] (default: 0)\n\
18517
0
                          encode EVEX instructions with specific EVEX.W value\n\
18518
0
                           for EVEX.W bit ignored instructions\n"));
18519
0
  fprintf (stream, _("\
18520
0
  -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
18521
0
                          encode EVEX instructions with specific EVEX.RC value\n\
18522
0
                           for SAE-only ignored instructions\n"));
18523
0
  fprintf (stream, _("\
18524
0
  -mmnemonic=[att|intel] "));
18525
0
  if (SYSV386_COMPAT)
18526
0
    fprintf (stream, _("(default: att)\n"));
18527
0
  else
18528
0
    fprintf (stream, _("(default: intel)\n"));
18529
0
  fprintf (stream, _("\
18530
0
                          use AT&T/Intel mnemonic (AT&T syntax only)\n"));
18531
0
  fprintf (stream, _("\
18532
0
  -msyntax=[att|intel] (default: att)\n\
18533
0
                          use AT&T/Intel syntax\n"));
18534
0
  fprintf (stream, _("\
18535
0
  -mindex-reg             support pseudo index registers\n"));
18536
0
  fprintf (stream, _("\
18537
0
  -mnaked-reg             don't require `%%' prefix for registers\n"));
18538
0
  fprintf (stream, _("\
18539
0
  -madd-bnd-prefix        add BND prefix for all valid branches\n"));
18540
0
#ifdef OBJ_ELF
18541
0
  fprintf (stream, _("\
18542
0
  -mshared                disable branch optimization for shared code\n"));
18543
0
  fprintf (stream, _("\
18544
0
  -mx86-used-note=[no|yes] "));
18545
0
  if (DEFAULT_X86_USED_NOTE)
18546
0
    fprintf (stream, _("(default: yes)\n"));
18547
0
  else
18548
0
    fprintf (stream, _("(default: no)\n"));
18549
0
  fprintf (stream, _("\
18550
0
                          generate x86 used ISA and feature properties\n"));
18551
0
#endif
18552
#if defined (TE_PE) || defined (TE_PEP)
18553
  fprintf (stream, _("\
18554
  -mbig-obj               generate big object files\n"));
18555
#endif
18556
0
  fprintf (stream, _("\
18557
0
  -momit-lock-prefix=[no|yes] (default: no)\n\
18558
0
                          strip all lock prefixes\n"));
18559
0
  fprintf (stream, _("\
18560
0
  -mfence-as-lock-add=[no|yes] (default: no)\n\
18561
0
                          encode lfence, mfence and sfence as\n\
18562
0
                           lock addl $0x0, (%%{re}sp)\n"));
18563
0
  fprintf (stream, _("\
18564
0
  -mrelax-relocations=[no|yes] "));
18565
0
  if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
18566
0
    fprintf (stream, _("(default: yes)\n"));
18567
0
  else
18568
0
    fprintf (stream, _("(default: no)\n"));
18569
0
  fprintf (stream, _("\
18570
0
                          generate relax relocations\n"));
18571
0
#ifdef OBJ_ELF
18572
0
  fprintf (stream, _("\
18573
0
  -mtls-check=[no|yes] "));
18574
0
  if (DEFAULT_X86_TLS_CHECK)
18575
0
    fprintf (stream, _("(default: yes)\n"));
18576
0
  else
18577
0
    fprintf (stream, _("(default: no)\n"));
18578
0
  fprintf (stream, _("\
18579
0
                          check TLS relocation\n"));
18580
0
#endif
18581
0
  fprintf (stream, _("\
18582
0
  -malign-branch-boundary=NUM (default: 0)\n\
18583
0
                          align branches within NUM byte boundary\n"));
18584
0
  fprintf (stream, _("\
18585
0
  -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
18586
0
                          TYPE is combination of jcc, fused, jmp, call, ret,\n\
18587
0
                           indirect\n\
18588
0
                          specify types of branches to align\n"));
18589
0
  fprintf (stream, _("\
18590
0
  -malign-branch-prefix-size=NUM (default: 5)\n\
18591
0
                          align branches with NUM prefixes per instruction\n"));
18592
0
  fprintf (stream, _("\
18593
0
  -mbranches-within-32B-boundaries\n\
18594
0
                          align branches within 32 byte boundary\n"));
18595
0
  fprintf (stream, _("\
18596
0
  -mlfence-after-load=[no|yes] (default: no)\n\
18597
0
                          generate lfence after load\n"));
18598
0
  fprintf (stream, _("\
18599
0
  -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
18600
0
                          generate lfence before indirect near branch\n"));
18601
0
  fprintf (stream, _("\
18602
0
  -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
18603
0
                          generate lfence before ret\n"));
18604
0
  fprintf (stream, _("\
18605
0
  -mamd64                 accept only AMD64 ISA [default]\n"));
18606
0
  fprintf (stream, _("\
18607
0
  -mintel64               accept only Intel64 ISA\n"));
18608
0
}
18609
18610
#if (defined (OBJ_ELF) || defined (TE_PE) || defined (OBJ_MACH_O))
18611
18612
/* Pick the target format to use.  */
18613
18614
const char *
18615
i386_target_format (void)
18616
368
{
18617
368
  if (startswith (default_arch, "x86_64"))
18618
368
    {
18619
368
      update_code_flag (CODE_64BIT, 1);
18620
368
#ifdef OBJ_ELF
18621
368
      if (default_arch[6] == '\0')
18622
368
  x86_elf_abi = X86_64_ABI;
18623
0
      else
18624
0
  x86_elf_abi = X86_64_X32_ABI;
18625
368
#endif
18626
368
    }
18627
0
  else if (!strcmp (default_arch, "i386"))
18628
0
    update_code_flag (CODE_32BIT, 1);
18629
0
  else if (!strcmp (default_arch, "iamcu"))
18630
0
    {
18631
0
      update_code_flag (CODE_32BIT, 1);
18632
0
      if (cpu_arch_isa == PROCESSOR_UNKNOWN)
18633
0
  {
18634
0
    static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
18635
0
    cpu_arch_name = "iamcu";
18636
0
    free (cpu_sub_arch_name);
18637
0
    cpu_sub_arch_name = NULL;
18638
0
    cpu_arch_flags = iamcu_flags;
18639
0
    cpu_arch_isa = PROCESSOR_IAMCU;
18640
0
    cpu_arch_isa_flags = iamcu_flags;
18641
0
    if (!cpu_arch_tune_set)
18642
0
      cpu_arch_tune = PROCESSOR_IAMCU;
18643
0
  }
18644
0
      else if (cpu_arch_isa != PROCESSOR_IAMCU)
18645
0
  as_fatal (_("Intel MCU doesn't support `%s' architecture"),
18646
0
      cpu_arch_name);
18647
0
    }
18648
0
  else
18649
0
    as_fatal (_("unknown architecture"));
18650
18651
368
#ifdef OBJ_ELF
18652
368
  if (flag_synth_cfi && x86_elf_abi != X86_64_ABI)
18653
0
    as_fatal (_("SCFI is not supported for this ABI"));
18654
368
#endif
18655
18656
368
  if (cpu_flags_all_zero (&cpu_arch_isa_flags))
18657
1
    cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
18658
18659
368
  switch (OUTPUT_FLAVOR)
18660
368
    {
18661
#ifdef TE_PE
18662
    case bfd_target_coff_flavour:
18663
      if (flag_code == CODE_64BIT)
18664
  {
18665
    object_64bit = 1;
18666
    return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
18667
  }
18668
      return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
18669
#endif
18670
0
#ifdef OBJ_ELF
18671
368
    case bfd_target_elf_flavour:
18672
368
      {
18673
368
  const char *format;
18674
18675
368
  switch (x86_elf_abi)
18676
368
    {
18677
0
    default:
18678
0
      format = ELF_TARGET_FORMAT;
18679
0
#ifndef TE_SOLARIS
18680
0
      tls_get_addr = "___tls_get_addr";
18681
0
#endif
18682
0
      break;
18683
368
    case X86_64_ABI:
18684
368
      use_rela_relocations = 1;
18685
368
      object_64bit = 1;
18686
368
#ifndef TE_SOLARIS
18687
368
      tls_get_addr = "__tls_get_addr";
18688
368
#endif
18689
368
      format = ELF_TARGET_FORMAT64;
18690
368
      break;
18691
0
    case X86_64_X32_ABI:
18692
0
      use_rela_relocations = 1;
18693
0
      object_64bit = 1;
18694
0
#ifndef TE_SOLARIS
18695
0
      tls_get_addr = "__tls_get_addr";
18696
0
#endif
18697
0
      disallow_64bit_reloc = 1;
18698
0
      format = ELF_TARGET_FORMAT32;
18699
0
      break;
18700
368
    }
18701
368
  if (cpu_arch_isa == PROCESSOR_IAMCU)
18702
0
    {
18703
0
      if (x86_elf_abi != I386_ABI)
18704
0
        as_fatal (_("Intel MCU is 32bit only"));
18705
0
      return ELF_TARGET_IAMCU_FORMAT;
18706
0
    }
18707
368
  else
18708
368
    return format;
18709
368
      }
18710
0
#endif
18711
#if defined (OBJ_MACH_O)
18712
    case bfd_target_mach_o_flavour:
18713
      if (flag_code == CODE_64BIT)
18714
  {
18715
    use_rela_relocations = 1;
18716
    object_64bit = 1;
18717
    return "mach-o-x86-64";
18718
  }
18719
      else
18720
  return "mach-o-i386";
18721
#endif
18722
0
    default:
18723
0
      abort ();
18724
0
      return NULL;
18725
368
    }
18726
368
}
18727
18728
#endif /* ELF / PE / MACH_O  */
18729

18730
#ifdef OBJ_ELF
18731
symbolS *
18732
md_undefined_symbol (char *name)
18733
2.40k
{
18734
2.40k
  if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
18735
52
      && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
18736
22
      && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
18737
22
      && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
18738
22
    {
18739
22
      if (!GOT_symbol)
18740
21
  {
18741
21
    if (symbol_find (name))
18742
0
      as_bad (_("GOT already in symbol table"));
18743
21
    GOT_symbol = symbol_new (name, undefined_section,
18744
21
           &zero_address_frag, 0);
18745
21
  };
18746
22
      return GOT_symbol;
18747
22
    }
18748
2.38k
  return NULL;
18749
2.40k
}
18750
#endif
18751
18752
#ifdef OBJ_AOUT
18753
/* Round up a section size to the appropriate boundary.  */
18754
18755
valueT
18756
md_section_align (segT segment, valueT size)
18757
{
18758
  /* For a.out, force the section size to be aligned.  If we don't do
18759
     this, BFD will align it for us, but it will not write out the
18760
     final bytes of the section.  This may be a bug in BFD, but it is
18761
     easier to fix it here since that is how the other a.out targets
18762
     work.  */
18763
  int align = bfd_section_alignment (segment);
18764
18765
  return (size + ((valueT) 1 << align) - 1) & -((valueT) 1 << align);
18766
}
18767
#endif
18768
18769
/* On the i386, PC-relative offsets are relative to the start of the
18770
   next instruction.  That is, the address of the offset, plus its
18771
   size, since the offset is always the last part of the insn.  */
18772
18773
long
18774
md_pcrel_from (fixS *fixP)
18775
0
{
18776
0
  return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
18777
0
}
18778
18779
#ifdef OBJ_AOUT
18780
18781
static void
18782
s_bss (int ignore ATTRIBUTE_UNUSED)
18783
{
18784
  int temp;
18785
18786
  temp = get_absolute_expression ();
18787
  subseg_set (bss_section, temp);
18788
  demand_empty_rest_of_line ();
18789
}
18790
18791
#endif
18792
18793
/* Remember constant directive.  */
18794
18795
void
18796
i386_cons_align (int ignore ATTRIBUTE_UNUSED)
18797
11.7k
{
18798
11.7k
  struct last_insn *last_insn
18799
11.7k
    = &seg_info(now_seg)->tc_segment_info_data.last_insn;
18800
18801
11.7k
  if (bfd_section_flags (now_seg) & SEC_CODE)
18802
11.4k
    {
18803
11.4k
      last_insn->kind = last_insn_directive;
18804
11.4k
      last_insn->name = "constant directive";
18805
11.4k
      last_insn->file = as_where (&last_insn->line);
18806
11.4k
    }
18807
11.7k
}
18808
18809
int
18810
i386_validate_fix (fixS *fixp)
18811
0
{
18812
0
  if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
18813
0
    {
18814
0
      reloc_howto_type *howto;
18815
18816
0
      howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
18817
0
      as_bad_where (fixp->fx_file, fixp->fx_line,
18818
0
        _("invalid %s relocation against register"),
18819
0
        howto ? howto->name : "<unknown>");
18820
0
      return 0;
18821
0
    }
18822
18823
0
#ifdef OBJ_ELF
18824
0
  if (fixp->fx_r_type == BFD_RELOC_SIZE32
18825
0
      || fixp->fx_r_type == BFD_RELOC_SIZE64)
18826
0
    return fixp->fx_addsy
18827
0
     && (!S_IS_DEFINED (fixp->fx_addsy)
18828
0
         || S_IS_EXTERNAL (fixp->fx_addsy));
18829
18830
  /* BFD_RELOC_X86_64_GOTTPOFF:
18831
      1. fx_tcbit -> BFD_RELOC_X86_64_CODE_4_GOTTPOFF
18832
      2. fx_tcbit2 -> BFD_RELOC_X86_64_CODE_5_GOTTPOFF
18833
      3. fx_tcbit3 -> BFD_RELOC_X86_64_CODE_6_GOTTPOFF
18834
    BFD_RELOC_X86_64_GOTPC32_TLSDESC:
18835
      1. fx_tcbit -> BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC
18836
    BFD_RELOC_32_PCREL:
18837
      1. fx_tcbit && fx_tcbit3 -> BFD_RELOC_X86_64_CODE_5_GOTPCRELX
18838
      2. fx_tcbit -> BFD_RELOC_X86_64_GOTPCRELX
18839
      3. fx_tcbit2 && fx_tcbit3 -> BFD_RELOC_X86_64_CODE_6_GOTPCRELX
18840
      4. fx_tcbit2 -> BFD_RELOC_X86_64_REX_GOTPCRELX
18841
      5. fx_tcbit3 -> BFD_RELOC_X86_64_CODE_4_GOTPCRELX
18842
      6. else -> BFD_RELOC_X86_64_GOTPCREL
18843
   */
18844
0
  if (fixp->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF)
18845
0
    {
18846
0
      if (fixp->fx_tcbit)
18847
0
  fixp->fx_r_type = BFD_RELOC_X86_64_CODE_4_GOTTPOFF;
18848
0
      else if (fixp->fx_tcbit2)
18849
0
  fixp->fx_r_type = BFD_RELOC_X86_64_CODE_5_GOTTPOFF;
18850
0
      else if (fixp->fx_tcbit3)
18851
0
  fixp->fx_r_type = BFD_RELOC_X86_64_CODE_6_GOTTPOFF;
18852
0
    }
18853
0
  else if (fixp->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
18854
0
     && fixp->fx_tcbit)
18855
0
    fixp->fx_r_type = BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC;
18856
0
#endif
18857
18858
0
  if (fixp->fx_subsy)
18859
0
    {
18860
0
      if (fixp->fx_subsy == GOT_symbol)
18861
0
  {
18862
0
    if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
18863
0
      {
18864
0
        if (!object_64bit)
18865
0
    abort ();
18866
0
#ifdef OBJ_ELF
18867
0
        if (fixp->fx_tcbit)
18868
0
    fixp->fx_r_type = fixp->fx_tcbit3
18869
0
          ? BFD_RELOC_X86_64_CODE_5_GOTPCRELX
18870
0
          : BFD_RELOC_X86_64_GOTPCRELX;
18871
0
        else if (fixp->fx_tcbit2)
18872
0
    fixp->fx_r_type = fixp->fx_tcbit3
18873
0
          ? BFD_RELOC_X86_64_CODE_6_GOTPCRELX
18874
0
          : BFD_RELOC_X86_64_REX_GOTPCRELX;
18875
0
        else if (fixp->fx_tcbit3)
18876
0
    fixp->fx_r_type = BFD_RELOC_X86_64_CODE_4_GOTPCRELX;
18877
0
        else
18878
0
#endif
18879
0
    fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
18880
0
      }
18881
0
    else
18882
0
      {
18883
0
        if (!object_64bit)
18884
0
    fixp->fx_r_type = BFD_RELOC_32_GOTOFF;
18885
0
        else
18886
0
    fixp->fx_r_type = BFD_RELOC_64_GOTOFF;
18887
0
      }
18888
0
    fixp->fx_subsy = 0;
18889
0
  }
18890
0
    }
18891
0
#ifdef OBJ_ELF
18892
0
  else
18893
0
    {
18894
      /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
18895
   to section.  Since PLT32 relocation must be against symbols,
18896
   turn such PLT32 relocation into PC32 relocation.  NB: We can
18897
   turn PLT32 relocation into PC32 relocation only for PC-relative
18898
   relocations since non-PC-relative relocations need PLT entries.
18899
       */
18900
0
      if (fixp->fx_addsy
18901
0
    && fixp->fx_pcrel
18902
0
    && (fixp->fx_r_type == BFD_RELOC_386_PLT32
18903
0
        || fixp->fx_r_type == BFD_RELOC_32_PLT_PCREL)
18904
0
    && symbol_section_p (fixp->fx_addsy))
18905
0
  fixp->fx_r_type = BFD_RELOC_32_PCREL;
18906
0
      if (!object_64bit)
18907
0
  {
18908
0
    if (fixp->fx_r_type == BFD_RELOC_386_GOT32
18909
0
        && fixp->fx_tcbit2)
18910
0
      fixp->fx_r_type = BFD_RELOC_386_GOT32X;
18911
0
  }
18912
0
    }
18913
0
#endif
18914
18915
0
  return 1;
18916
0
}
18917
18918
arelent *
18919
tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
18920
0
{
18921
0
  arelent *rel;
18922
0
  bfd_reloc_code_real_type code;
18923
18924
0
  switch (fixp->fx_r_type)
18925
0
    {
18926
0
#ifdef OBJ_ELF
18927
0
      symbolS *sym;
18928
18929
0
    case BFD_RELOC_SIZE32:
18930
0
    case BFD_RELOC_SIZE64:
18931
0
      if (fixp->fx_addsy
18932
0
    && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
18933
0
    && (!fixp->fx_subsy
18934
0
        || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
18935
0
  sym = fixp->fx_addsy;
18936
0
      else if (fixp->fx_subsy
18937
0
         && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
18938
0
         && (!fixp->fx_addsy
18939
0
       || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
18940
0
  sym = fixp->fx_subsy;
18941
0
      else
18942
0
  sym = NULL;
18943
0
      if (sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
18944
0
  {
18945
    /* Resolve size relocation against local symbol to size of
18946
       the symbol plus addend.  */
18947
0
    valueT value = S_GET_SIZE (sym);
18948
18949
0
    if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
18950
0
      value = bfd_section_size (S_GET_SEGMENT (sym));
18951
0
    if (sym == fixp->fx_subsy)
18952
0
      {
18953
0
        value = -value;
18954
0
        if (fixp->fx_addsy)
18955
0
          value += S_GET_VALUE (fixp->fx_addsy);
18956
0
      }
18957
0
    else if (fixp->fx_subsy)
18958
0
      value -= S_GET_VALUE (fixp->fx_subsy);
18959
0
    value += fixp->fx_offset;
18960
0
    if (fixp->fx_r_type == BFD_RELOC_SIZE32
18961
0
        && object_64bit
18962
0
        && !fits_in_unsigned_long (value))
18963
0
      as_bad_where (fixp->fx_file, fixp->fx_line,
18964
0
        _("symbol size computation overflow"));
18965
0
    fixp->fx_addsy = NULL;
18966
0
    fixp->fx_subsy = NULL;
18967
0
    md_apply_fix (fixp, &value, NULL);
18968
0
    return NULL;
18969
0
  }
18970
0
      if (!fixp->fx_addsy || fixp->fx_subsy)
18971
0
  {
18972
0
    as_bad_where (fixp->fx_file, fixp->fx_line,
18973
0
      "unsupported expression involving @size");
18974
0
    return NULL;
18975
0
  }
18976
0
#endif
18977
      /* Fall through.  */
18978
18979
0
    case BFD_RELOC_32_PLT_PCREL:
18980
0
    case BFD_RELOC_X86_64_GOT32:
18981
0
    case BFD_RELOC_X86_64_GOTPCREL:
18982
0
    case BFD_RELOC_X86_64_GOTPCRELX:
18983
0
    case BFD_RELOC_X86_64_REX_GOTPCRELX:
18984
0
    case BFD_RELOC_X86_64_CODE_4_GOTPCRELX:
18985
0
    case BFD_RELOC_X86_64_CODE_5_GOTPCRELX:
18986
0
    case BFD_RELOC_X86_64_CODE_6_GOTPCRELX:
18987
0
    case BFD_RELOC_386_PLT32:
18988
0
    case BFD_RELOC_386_GOT32:
18989
0
    case BFD_RELOC_386_GOT32X:
18990
0
    case BFD_RELOC_32_GOTOFF:
18991
0
    case BFD_RELOC_32_GOT_PCREL:
18992
0
    case BFD_RELOC_386_TLS_GD:
18993
0
    case BFD_RELOC_386_TLS_LDM:
18994
0
    case BFD_RELOC_386_TLS_LDO_32:
18995
0
    case BFD_RELOC_386_TLS_IE_32:
18996
0
    case BFD_RELOC_386_TLS_IE:
18997
0
    case BFD_RELOC_386_TLS_GOTIE:
18998
0
    case BFD_RELOC_386_TLS_LE_32:
18999
0
    case BFD_RELOC_386_TLS_LE:
19000
0
    case BFD_RELOC_386_TLS_GOTDESC:
19001
0
    case BFD_RELOC_386_TLS_DESC_CALL:
19002
0
    case BFD_RELOC_386_PC32_TO_PLT32:
19003
0
    case BFD_RELOC_X86_64_TLSGD:
19004
0
    case BFD_RELOC_X86_64_TLSLD:
19005
0
    case BFD_RELOC_X86_64_DTPOFF32:
19006
0
    case BFD_RELOC_X86_64_DTPOFF64:
19007
0
    case BFD_RELOC_X86_64_GOTTPOFF:
19008
0
    case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
19009
0
    case BFD_RELOC_X86_64_CODE_5_GOTTPOFF:
19010
0
    case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
19011
0
    case BFD_RELOC_X86_64_TPOFF32:
19012
0
    case BFD_RELOC_X86_64_TPOFF64:
19013
0
    case BFD_RELOC_64_GOTOFF:
19014
0
    case BFD_RELOC_X86_64_GOTPC32:
19015
0
    case BFD_RELOC_X86_64_GOT64:
19016
0
    case BFD_RELOC_X86_64_GOTPCREL64:
19017
0
    case BFD_RELOC_64_GOT_PCREL:
19018
0
    case BFD_RELOC_X86_64_GOTPLT64:
19019
0
    case BFD_RELOC_64_PLTOFF:
19020
0
    case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
19021
0
    case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
19022
0
    case BFD_RELOC_X86_64_CODE_5_GOTPC32_TLSDESC:
19023
0
    case BFD_RELOC_X86_64_CODE_6_GOTPC32_TLSDESC:
19024
0
    case BFD_RELOC_X86_64_TLSDESC_CALL:
19025
0
    case BFD_RELOC_X86_64_PC32_TO_PLT32:
19026
0
    case BFD_RELOC_RVA:
19027
0
    case BFD_RELOC_VTABLE_ENTRY:
19028
0
    case BFD_RELOC_VTABLE_INHERIT:
19029
#ifdef TE_PE
19030
    case BFD_RELOC_32_SECREL:
19031
    case BFD_RELOC_16_SECIDX:
19032
#endif
19033
0
      code = fixp->fx_r_type;
19034
0
      break;
19035
0
    case BFD_RELOC_X86_64_32S:
19036
0
      if (!fixp->fx_pcrel)
19037
0
  {
19038
    /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32.  */
19039
0
    code = fixp->fx_r_type;
19040
0
    break;
19041
0
  }
19042
      /* Fall through.  */
19043
0
    default:
19044
0
      if (fixp->fx_pcrel)
19045
0
  {
19046
0
    switch (fixp->fx_size)
19047
0
      {
19048
0
      default:
19049
0
        as_bad_where (fixp->fx_file, fixp->fx_line,
19050
0
          _("can not do %d byte pc-relative relocation"),
19051
0
          fixp->fx_size);
19052
0
        code = BFD_RELOC_32_PCREL;
19053
0
        break;
19054
0
      case 1: code = BFD_RELOC_8_PCREL;  break;
19055
0
      case 2: code = BFD_RELOC_16_PCREL; break;
19056
0
      case 4: code = BFD_RELOC_32_PCREL; break;
19057
0
#ifdef BFD64
19058
0
      case 8: code = BFD_RELOC_64_PCREL; break;
19059
0
#endif
19060
0
      }
19061
0
  }
19062
0
      else
19063
0
  {
19064
0
    switch (fixp->fx_size)
19065
0
      {
19066
0
      default:
19067
0
        as_bad_where (fixp->fx_file, fixp->fx_line,
19068
0
          _("can not do %d byte relocation"),
19069
0
          fixp->fx_size);
19070
0
        code = BFD_RELOC_32;
19071
0
        break;
19072
0
      case 1: code = BFD_RELOC_8;  break;
19073
0
      case 2: code = BFD_RELOC_16; break;
19074
0
      case 4: code = BFD_RELOC_32; break;
19075
0
#ifdef BFD64
19076
0
      case 8: code = BFD_RELOC_64; break;
19077
0
#endif
19078
0
      }
19079
0
  }
19080
0
      break;
19081
0
    }
19082
19083
0
  if ((code == BFD_RELOC_32
19084
0
       || code == BFD_RELOC_32_PCREL
19085
0
       || code == BFD_RELOC_X86_64_32S)
19086
0
      && GOT_symbol
19087
0
      && fixp->fx_addsy == GOT_symbol)
19088
0
    {
19089
0
      if (!object_64bit)
19090
0
  code = BFD_RELOC_32_GOT_PCREL;
19091
0
      else
19092
0
  code = BFD_RELOC_X86_64_GOTPC32;
19093
0
    }
19094
0
  if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
19095
0
      && GOT_symbol
19096
0
      && fixp->fx_addsy == GOT_symbol)
19097
0
    {
19098
0
      code = BFD_RELOC_64_GOT_PCREL;
19099
0
    }
19100
19101
0
  rel = notes_alloc (sizeof (arelent));
19102
0
  rel->sym_ptr_ptr = notes_alloc (sizeof (asymbol *));
19103
0
  *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
19104
19105
0
  rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
19106
19107
0
  if (!use_rela_relocations)
19108
0
    {
19109
      /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
19110
   vtable entry to be used in the relocation's section offset.  */
19111
0
      if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
19112
0
  rel->address = fixp->fx_offset;
19113
#if defined (OBJ_COFF) && defined (TE_PE)
19114
      else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
19115
  rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
19116
      else
19117
#endif
19118
0
      rel->addend = 0;
19119
0
    }
19120
  /* Use the rela in 64bit mode.  */
19121
0
  else
19122
0
    {
19123
0
      if (disallow_64bit_reloc)
19124
0
  switch (code)
19125
0
    {
19126
0
    case BFD_RELOC_X86_64_DTPOFF64:
19127
0
    case BFD_RELOC_X86_64_TPOFF64:
19128
0
    case BFD_RELOC_64_PCREL:
19129
0
    case BFD_RELOC_64_GOTOFF:
19130
0
    case BFD_RELOC_X86_64_GOT64:
19131
0
    case BFD_RELOC_X86_64_GOTPCREL64:
19132
0
    case BFD_RELOC_64_GOT_PCREL:
19133
0
    case BFD_RELOC_X86_64_GOTPLT64:
19134
0
    case BFD_RELOC_64_PLTOFF:
19135
0
      as_bad_where (fixp->fx_file, fixp->fx_line,
19136
0
        _("cannot represent relocation type %s in x32 mode"),
19137
0
        bfd_get_reloc_code_name (code));
19138
0
      break;
19139
0
    default:
19140
0
      break;
19141
0
    }
19142
19143
0
      if (!fixp->fx_pcrel)
19144
0
  rel->addend = fixp->fx_offset;
19145
0
      else
19146
0
  switch (code)
19147
0
    {
19148
0
    case BFD_RELOC_32_PLT_PCREL:
19149
0
    case BFD_RELOC_X86_64_GOT32:
19150
0
    case BFD_RELOC_X86_64_GOTPCREL:
19151
0
    case BFD_RELOC_X86_64_GOTPCRELX:
19152
0
    case BFD_RELOC_X86_64_REX_GOTPCRELX:
19153
0
    case BFD_RELOC_X86_64_CODE_4_GOTPCRELX:
19154
0
    case BFD_RELOC_X86_64_CODE_5_GOTPCRELX:
19155
0
    case BFD_RELOC_X86_64_CODE_6_GOTPCRELX:
19156
0
    case BFD_RELOC_X86_64_TLSGD:
19157
0
    case BFD_RELOC_X86_64_TLSLD:
19158
0
    case BFD_RELOC_X86_64_GOTTPOFF:
19159
0
    case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
19160
0
    case BFD_RELOC_X86_64_CODE_5_GOTTPOFF:
19161
0
    case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
19162
0
    case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
19163
0
    case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
19164
0
    case BFD_RELOC_X86_64_CODE_5_GOTPC32_TLSDESC:
19165
0
    case BFD_RELOC_X86_64_CODE_6_GOTPC32_TLSDESC:
19166
0
    case BFD_RELOC_X86_64_TLSDESC_CALL:
19167
0
      rel->addend = fixp->fx_offset - fixp->fx_size;
19168
0
      break;
19169
0
    case BFD_RELOC_X86_64_PC32_TO_PLT32:
19170
      /* This came from a directive like ".long foo@PLT - .L4".
19171
         Generate R_X86_64_PLT32 with addend computed like
19172
         R_X86_64_PC32 so that PLT entry is used to resolve
19173
         this PC32 relocation.   */
19174
0
      code = BFD_RELOC_32_PLT_PCREL;
19175
      /* Fall through.  */
19176
0
    default:
19177
0
      rel->addend = (section->vma
19178
0
         - fixp->fx_size
19179
0
         + fixp->fx_addnumber
19180
0
         + md_pcrel_from (fixp));
19181
0
      break;
19182
0
    }
19183
0
    }
19184
19185
0
  rel->howto = bfd_reloc_type_lookup (stdoutput, code);
19186
0
  if (rel->howto == NULL)
19187
0
    {
19188
0
      as_bad_where (fixp->fx_file, fixp->fx_line,
19189
0
        _("cannot represent relocation type %s"),
19190
0
        bfd_get_reloc_code_name (code));
19191
      /* Set howto to a garbage value so that we can keep going.  */
19192
0
      rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
19193
0
      gas_assert (rel->howto != NULL);
19194
0
    }
19195
19196
0
  return rel;
19197
0
}
19198
19199
#include "tc-i386-intel.c"
19200
19201
void
19202
tc_x86_parse_to_dw2regnum (expressionS *exp)
19203
470
{
19204
470
  int saved_naked_reg;
19205
470
  char saved_register_dot;
19206
19207
470
  saved_naked_reg = allow_naked_reg;
19208
470
  allow_naked_reg = 1;
19209
470
  saved_register_dot = register_chars['.'];
19210
470
  register_chars['.'] = '.';
19211
470
  allow_pseudo_reg = 1;
19212
470
  expression_and_evaluate (exp);
19213
470
  allow_pseudo_reg = 0;
19214
470
  register_chars['.'] = saved_register_dot;
19215
470
  allow_naked_reg = saved_naked_reg;
19216
19217
470
  if (exp->X_op == O_register && exp->X_add_number >= 0)
19218
3
    {
19219
3
      exp->X_op = O_illegal;
19220
3
      if ((addressT) exp->X_add_number < i386_regtab_size)
19221
3
  {
19222
3
    exp->X_add_number = i386_regtab[exp->X_add_number]
19223
3
            .dw2_regnum[object_64bit];
19224
3
    if (exp->X_add_number != Dw2Inval)
19225
0
      exp->X_op = O_constant;
19226
3
  }
19227
3
    }
19228
470
}
19229
19230
void
19231
tc_x86_frame_initial_instructions (void)
19232
68
{
19233
68
  cfi_add_CFA_def_cfa (object_64bit ? REG_SP : 4, -x86_cie_data_alignment);
19234
68
  cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
19235
68
}
19236
19237
int
19238
x86_dwarf2_addr_size (void)
19239
50
{
19240
50
#ifdef OBJ_ELF
19241
50
  if (x86_elf_abi == X86_64_X32_ABI)
19242
0
    return 4;
19243
50
#endif
19244
50
  return bfd_arch_bits_per_address (stdoutput) / 8;
19245
50
}
19246
19247
#ifdef TE_PE
19248
void
19249
tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
19250
{
19251
  expressionS exp;
19252
19253
  exp.X_op = O_secrel;
19254
  exp.X_add_symbol = symbol;
19255
  exp.X_add_number = 0;
19256
  emit_expr (&exp, size);
19257
}
19258
#endif
19259
19260
#ifdef OBJ_ELF
19261
int
19262
i386_elf_section_type (const char *str, size_t len)
19263
24
{
19264
24
  if (flag_code == CODE_64BIT
19265
24
      && len == sizeof ("unwind") - 1
19266
12
      && startswith (str, "unwind"))
19267
1
    return SHT_X86_64_UNWIND;
19268
19269
23
  return -1;
19270
24
}
19271
19272
void
19273
i386_elf_section_change_hook (void)
19274
52.4k
{
19275
52.4k
  struct i386_segment_info *info = &seg_info(now_seg)->tc_segment_info_data;
19276
52.4k
  struct i386_segment_info *curr, *prev;
19277
19278
52.4k
  if (info->subseg == now_subseg)
19279
52.4k
    return;
19280
19281
  /* Find the (or make a) list entry to save state into.  */
19282
33
  for (prev = info; (curr = prev->next) != NULL; prev = curr)
19283
18
    if (curr->subseg == info->subseg)
19284
0
      break;
19285
15
  if (!curr)
19286
15
    {
19287
15
      curr = notes_alloc (sizeof (*curr));
19288
15
      curr->subseg = info->subseg;
19289
15
      curr->next = NULL;
19290
15
      prev->next = curr;
19291
15
    }
19292
15
  curr->last_insn = info->last_insn;
19293
19294
  /* Find the list entry to load state from.  */
19295
46
  for (curr = info->next; curr; curr = curr->next)
19296
32
    if (curr->subseg == now_subseg)
19297
1
      break;
19298
15
  if (curr)
19299
1
    info->last_insn = curr->last_insn;
19300
14
  else
19301
14
    memset (&info->last_insn, 0, sizeof (info->last_insn));
19302
15
  info->subseg = now_subseg;
19303
15
}
19304
19305
#ifdef TE_SOLARIS
19306
void
19307
i386_solaris_fix_up_eh_frame (segT sec)
19308
{
19309
  if (flag_code == CODE_64BIT)
19310
    elf_section_type (sec) = SHT_X86_64_UNWIND;
19311
}
19312
#endif
19313
19314
/* For ELF on x86-64, add support for SHF_X86_64_LARGE.  */
19315
19316
bfd_vma
19317
x86_64_section_letter (int letter, const char **extra)
19318
2.15k
{
19319
2.15k
  if (flag_code == CODE_64BIT)
19320
2.15k
    {
19321
2.15k
      if (letter == 'l')
19322
3
  return SHF_X86_64_LARGE;
19323
19324
2.15k
      *extra = "l";
19325
2.15k
    }
19326
2.15k
  return -1;
19327
2.15k
}
19328
19329
static void
19330
handle_large_common (int small ATTRIBUTE_UNUSED)
19331
0
{
19332
0
  if (flag_code != CODE_64BIT)
19333
0
    {
19334
0
      s_comm_internal (0, elf_common_parse);
19335
0
      as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
19336
0
    }
19337
0
  else
19338
0
    {
19339
0
      static segT lbss_section;
19340
0
      asection *saved_com_section_ptr = elf_com_section_ptr;
19341
0
      asection *saved_bss_section = bss_section;
19342
19343
0
      if (lbss_section == NULL)
19344
0
  {
19345
0
    flagword applicable;
19346
0
    segT seg = now_seg;
19347
0
    subsegT subseg = now_subseg;
19348
19349
    /* The .lbss section is for local .largecomm symbols.  */
19350
0
    lbss_section = subseg_new (".lbss", 0);
19351
0
    applicable = bfd_applicable_section_flags (stdoutput);
19352
0
    bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
19353
0
    seg_info (lbss_section)->bss = 1;
19354
19355
0
    subseg_set (seg, subseg);
19356
0
  }
19357
19358
0
      elf_com_section_ptr = &bfd_elf_large_com_section;
19359
0
      bss_section = lbss_section;
19360
19361
0
      s_comm_internal (0, elf_common_parse);
19362
19363
0
      elf_com_section_ptr = saved_com_section_ptr;
19364
0
      bss_section = saved_bss_section;
19365
0
    }
19366
0
}
19367
#endif /* OBJ_ELF */