Coverage Report

Created: 2026-10-02 09:53

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
37
#define WAIT_PREFIX 0
63
4.21k
#define SEG_PREFIX  1
64
29.7k
#define ADDR_PREFIX 2
65
3.81k
#define DATA_PREFIX 3
66
99
#define REP_PREFIX  4
67
0
#define HLE_PREFIX  REP_PREFIX
68
80
#define BND_PREFIX  REP_PREFIX
69
3.46k
#define LOCK_PREFIX 5
70
6.97k
#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
142k
#define REGISTER_PREFIX '%'
75
2.39k
#define IMMEDIATE_PREFIX '$'
76
6.96k
#define ABSOLUTE_PREFIX '*'
77
78
/* these are the instruction mnemonic suffixes in AT&T syntax or
79
   memory operand size in Intel syntax.  */
80
3.60k
#define WORD_MNEM_SUFFIX  'w'
81
10.4k
#define BYTE_MNEM_SUFFIX  'b'
82
14.6k
#define SHORT_MNEM_SUFFIX 's'
83
42.5k
#define LONG_MNEM_SUFFIX  'l'
84
82.3k
#define QWORD_MNEM_SUFFIX  'q'
85
86
501k
#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
317
#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
438
#define Operand_PCrel 1
338
20.6k
#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
10
#define OSZC_CF 1
464
0
#define OSZC_ZF 2
465
4
#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
6.31k
#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
613k
#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
21.8k
  do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
635
#define RESTORE_END_STRING(s) \
636
21.7k
  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
23.3k
#define dot_insn() (i.tm.mnem_off == MN__insn)
653
654
static enum i386_flag_code i386_flag_code;
655
272k
#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
281
#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
0
#define VSZ128 0
916
34.3k
#define VSZ256 1
917
7.02k
#define VSZ512 2
918
3
#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
0
#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
28
#define CODE16  1
981
42
#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
21
  ((relax_substateT) (((type) << 2) | (size)))
996
#define TYPE_FROM_RELAX_STATE(s) \
997
0
  ((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 (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1211
  SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
1212
  SUBARCH (amx_fp16, AMX_FP16, ANY_AMX_FP16, false),
1213
  SUBARCH (amx_complex, AMX_COMPLEX, ANY_AMX_COMPLEX, false),
1214
  SUBARCH (amx_transpose, AMX_TRANSPOSE, ANY_AMX_TRANSPOSE, false),
1215
  SUBARCH (amx_tf32, AMX_TF32, ANY_AMX_TF32, false),
1216
  SUBARCH (amx_fp8, AMX_FP8, ANY_AMX_FP8, false),
1217
  SUBARCH (amx_movrs, AMX_MOVRS, ANY_AMX_MOVRS, false),
1218
  SUBARCH (amx_avx512, AMX_AVX512, ANY_AMX_AVX512, false),
1219
  SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
1220
  SUBARCH (movdiri, MOVDIRI, MOVDIRI, false),
1221
  SUBARCH (movdir64b, MOVDIR64B, MOVDIR64B, false),
1222
  VECARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, reset),
1223
  VECARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1224
     ANY_AVX512_VP2INTERSECT, reset),
1225
  VECARCH (avx512_bmm, AVX512_BMM, ANY_AVX512_BMM, reset),
1226
  SUBARCH (tdx, TDX, TDX, false),
1227
  SUBARCH (enqcmd, ENQCMD, ENQCMD, false),
1228
  SUBARCH (serialize, SERIALIZE, SERIALIZE, false),
1229
  SUBARCH (rdpru, RDPRU, RDPRU, false),
1230
  SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
1231
  SUBARCH (sev_es, SEV_ES, ANY_SEV_ES, false),
1232
  SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
1233
  SUBARCH (kl, KL, ANY_KL, false),
1234
  SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
1235
  SUBARCH (uintr, UINTR, UINTR, false),
1236
  SUBARCH (hreset, HRESET, HRESET, false),
1237
  VECARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, reset),
1238
  SUBARCH (prefetchi, PREFETCHI, PREFETCHI, false),
1239
  VECARCH (avx_ifma, AVX_IFMA, ANY_AVX_IFMA, reset),
1240
  VECARCH (avx_vnni_int8, AVX_VNNI_INT8, ANY_AVX_VNNI_INT8, reset),
1241
  SUBARCH (cmpccxadd, CMPCCXADD, CMPCCXADD, false),
1242
  SUBARCH (wrmsrns, WRMSRNS, WRMSRNS, false),
1243
  SUBARCH (msrlist, MSRLIST, MSRLIST, false),
1244
  VECARCH (avx_ne_convert, AVX_NE_CONVERT, ANY_AVX_NE_CONVERT, reset),
1245
  SUBARCH (rao_int, RAO_INT, RAO_INT, false),
1246
  SUBARCH (rmpquery, RMPQUERY, ANY_RMPQUERY, false),
1247
  SUBARCH (rmpread, RMPREAD, ANY_RMPREAD, false),
1248
  SUBARCH (rmpdirty, RMPDIRTY, ANY_RMPDIRTY, false),
1249
  SUBARCH (rmpopt, RMPOPT, ANY_RMPOPT, false),
1250
  SUBARCH (fred, FRED, ANY_FRED, false),
1251
  SUBARCH (lkgs, LKGS, ANY_LKGS, false),
1252
  VECARCH (avx_vnni_int16, AVX_VNNI_INT16, ANY_AVX_VNNI_INT16, reset),
1253
  VECARCH (sha512, SHA512, ANY_SHA512, reset),
1254
  VECARCH (sm3, SM3, ANY_SM3, reset),
1255
  VECARCH (sm4, SM4, ANY_SM4, reset),
1256
  SUBARCH (pbndkb, PBNDKB, PBNDKB, false),
1257
  VECARCH (avx10.1, AVX10_1, ANY_AVX512F, set),
1258
  VECARCH (avx10.1aux, AVX10_1_AUX, ANY_AVX10_1_AUX, set),
1259
  VECARCH (avx10v1aux, AVX10_1_AUX, ANY_AVX10_1_AUX, set),
1260
  VECARCH (avx10.2, AVX10_2, ANY_AVX10_2, set),
1261
  VECARCH (avx10v2aux, AVX10_V2_AUX, ANY_AVX10_V2_AUX, set),
1262
  SUBARCH (user_msr, USER_MSR, USER_MSR, false),
1263
  SUBARCH (apx_f, APX_F, ANY_APX_F, false),
1264
  SUBARCH (apx_nci, APX_NCI, ANY_APX_NCI, false),
1265
  SUBARCH (apx_ndd, APX_NDD, ANY_APX_NDD, false),
1266
  SUBARCH (apx_nf, APX_NF, ANY_APX_NF, false),
1267
  SUBARCH (apx_nci_ndd_nf, APX_NCI_NDD_NF, ANY_APX_NCI_NDD_NF, false),
1268
  SUBARCH (gmism2, GMISM2, GMISM2, false),
1269
  SUBARCH (gmiccs, GMICCS, GMICCS, false),
1270
  SUBARCH (msr_imm, MSR_IMM, MSR_IMM, false),
1271
  SUBARCH (padlockrng2, PADLOCKRNG2, PADLOCKRNG2, false),
1272
  SUBARCH (padlockphe2, PADLOCKPHE2, PADLOCKPHE2, false),
1273
  SUBARCH (padlockxmodx, PADLOCKXMODX, PADLOCKXMODX, false),
1274
  SUBARCH (movrs, MOVRS, MOVRS, false),
1275
};
1276
1277
#undef SUBARCH
1278
#undef ARCH
1279
1280
#ifdef I386COFF
1281
/* Like s_lcomm_internal in gas/read.c but the alignment string
1282
   is allowed to be optional.  */
1283
1284
static symbolS *
1285
pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1286
{
1287
  addressT align = 0;
1288
1289
  SKIP_WHITESPACE ();
1290
1291
  if (needs_align
1292
      && *input_line_pointer == ',')
1293
    {
1294
      align = parse_align (needs_align - 1);
1295
1296
      if (align == (addressT) -1)
1297
  return NULL;
1298
    }
1299
  else
1300
    {
1301
      if (size >= 8)
1302
  align = 3;
1303
      else if (size >= 4)
1304
  align = 2;
1305
      else if (size >= 2)
1306
  align = 1;
1307
      else
1308
  align = 0;
1309
    }
1310
1311
  bss_alloc (symbolP, size, align);
1312
  return symbolP;
1313
}
1314
1315
static void
1316
pe_lcomm (int needs_align)
1317
{
1318
  s_comm_internal (needs_align * 2, pe_lcomm_internal);
1319
}
1320
#endif
1321
1322
const pseudo_typeS md_pseudo_table[] =
1323
{
1324
#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1325
  {"align", s_align_bytes, 0},
1326
#else
1327
  {"align", s_align_ptwo, 0},
1328
#endif
1329
  {"arch", set_cpu_arch, 0},
1330
#ifdef OBJ_AOUT
1331
  {"bss", s_bss, 0},
1332
#endif
1333
#ifdef I386COFF
1334
  {"lcomm", pe_lcomm, 1},
1335
#endif
1336
  {"ffloat", float_cons, 'f'},
1337
  {"dfloat", float_cons, 'd'},
1338
  {"tfloat", float_cons, 'x'},
1339
  {"hfloat", float_cons, 'h'},
1340
  {"bfloat16", float_cons, 'b'},
1341
  {"value", cons, 2},
1342
  {"slong", signed_cons, 4},
1343
  {"insn", s_insn, 0},
1344
  {"noopt", s_noopt, 0},
1345
  {"optim", s_ignore, 0},
1346
  {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1347
  {"code16", set_code_flag, CODE_16BIT},
1348
  {"code32", set_code_flag, CODE_32BIT},
1349
#ifdef BFD64
1350
  {"code64", set_code_flag, CODE_64BIT},
1351
#endif
1352
  {"intel_syntax", set_intel_syntax, 1},
1353
  {"att_syntax", set_intel_syntax, 0},
1354
  {"intel_mnemonic", set_intel_mnemonic, 1},
1355
  {"att_mnemonic", set_intel_mnemonic, 0},
1356
  {"allow_index_reg", set_allow_index_reg, 1},
1357
  {"disallow_index_reg", set_allow_index_reg, 0},
1358
  {"sse_check", set_check, 0},
1359
  {"operand_check", set_check, 1},
1360
#ifdef OBJ_ELF
1361
  {"largecomm", handle_large_common, 0},
1362
#else
1363
  {"file", dwarf2_directive_file, 0},
1364
  {"loc", dwarf2_directive_loc, 0},
1365
  {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
1366
#endif
1367
#ifdef TE_PE
1368
  {"secrel32", pe_directive_secrel, 0},
1369
  {"secidx", pe_directive_secidx, 0},
1370
#endif
1371
  {0, 0, 0}
1372
};
1373
1374
/* For interface with expression ().  */
1375
extern char *input_line_pointer;
1376
1377
/* Hash table for instruction mnemonic lookup.  */
1378
static htab_t op_hash;
1379
1380
/* Hash table for register lookup.  */
1381
static htab_t reg_hash;
1382
1383
#include "opcodes/i386-tbl.h"
1384
1385
#if (defined (OBJ_ELF) || defined (OBJ_MACH_O) || defined (TE_PE))
1386
static const struct
1387
{
1388
  const char *str;
1389
  unsigned int len;
1390
  const enum bfd_reloc_code_real rel[2];
1391
  const i386_operand_type types64;
1392
  bool need_GOT_symbol;
1393
}
1394
gotrel[] =
1395
{
1396
#define OPERAND_TYPE_IMM32_32S_DISP32 { .bitfield = \
1397
      { .imm32 = 1, .imm32s = 1, .disp32 = 1 } }
1398
#define OPERAND_TYPE_IMM32_32S_64_DISP32 { .bitfield = \
1399
      { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1 } }
1400
#define OPERAND_TYPE_IMM32_32S_64_DISP32_64 { .bitfield = \
1401
      { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1, .disp64 = 1 } }
1402
#define OPERAND_TYPE_IMM64_DISP64 { .bitfield = \
1403
      { .imm64 = 1, .disp64 = 1 } }
1404
1405
#ifndef TE_PE
1406
#ifdef OBJ_ELF
1407
    { STRING_COMMA_LEN ("SIZE"),      { BFD_RELOC_SIZE32,
1408
          BFD_RELOC_SIZE32 },
1409
    { .bitfield = { .imm32 = 1, .imm64 = 1 } }, false },
1410
#endif
1411
    { STRING_COMMA_LEN ("PLTOFF"),   { _dummy_first_bfd_reloc_code_real,
1412
               BFD_RELOC_64_PLTOFF },
1413
    { .bitfield = { .imm64 = 1 } }, true },
1414
    { STRING_COMMA_LEN ("PLT"),      { BFD_RELOC_386_PLT32,
1415
               BFD_RELOC_32_PLT_PCREL },
1416
    OPERAND_TYPE_IMM32_32S_DISP32, false },
1417
    { STRING_COMMA_LEN ("GOTPLT"),   { _dummy_first_bfd_reloc_code_real,
1418
               BFD_RELOC_X86_64_GOTPLT64 },
1419
    OPERAND_TYPE_IMM64_DISP64, true },
1420
    { STRING_COMMA_LEN ("GOTOFF"),   { BFD_RELOC_32_GOTOFF,
1421
               BFD_RELOC_64_GOTOFF },
1422
    OPERAND_TYPE_IMM64_DISP64, true },
1423
    { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
1424
               BFD_RELOC_X86_64_GOTPCREL },
1425
    OPERAND_TYPE_IMM32_32S_DISP32, true },
1426
    { STRING_COMMA_LEN ("TLSGD"),    { BFD_RELOC_386_TLS_GD,
1427
               BFD_RELOC_X86_64_TLSGD    },
1428
    OPERAND_TYPE_IMM32_32S_DISP32, true },
1429
    { STRING_COMMA_LEN ("TLSLDM"),   { BFD_RELOC_386_TLS_LDM,
1430
               _dummy_first_bfd_reloc_code_real },
1431
    OPERAND_TYPE_NONE, true },
1432
    { STRING_COMMA_LEN ("TLSLD"),    { _dummy_first_bfd_reloc_code_real,
1433
               BFD_RELOC_X86_64_TLSLD    },
1434
    OPERAND_TYPE_IMM32_32S_DISP32, true },
1435
    { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
1436
               BFD_RELOC_X86_64_GOTTPOFF },
1437
    OPERAND_TYPE_IMM32_32S_DISP32, true },
1438
    { STRING_COMMA_LEN ("TPOFF"),    { BFD_RELOC_386_TLS_LE_32,
1439
               BFD_RELOC_X86_64_TPOFF32  },
1440
    OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
1441
    { STRING_COMMA_LEN ("NTPOFF"),   { BFD_RELOC_386_TLS_LE,
1442
               _dummy_first_bfd_reloc_code_real },
1443
    OPERAND_TYPE_NONE, true },
1444
    { STRING_COMMA_LEN ("DTPOFF"),   { BFD_RELOC_386_TLS_LDO_32,
1445
               BFD_RELOC_X86_64_DTPOFF32 },
1446
    OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
1447
    { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
1448
               _dummy_first_bfd_reloc_code_real },
1449
    OPERAND_TYPE_NONE, true },
1450
    { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
1451
               _dummy_first_bfd_reloc_code_real },
1452
    OPERAND_TYPE_NONE, true },
1453
    { STRING_COMMA_LEN ("GOT"),      { BFD_RELOC_386_GOT32,
1454
               BFD_RELOC_X86_64_GOT32    },
1455
    OPERAND_TYPE_IMM32_32S_64_DISP32, true },
1456
    { STRING_COMMA_LEN ("TLSDESC"),  { BFD_RELOC_386_TLS_GOTDESC,
1457
               BFD_RELOC_X86_64_GOTPC32_TLSDESC },
1458
    OPERAND_TYPE_IMM32_32S_DISP32, true },
1459
    { STRING_COMMA_LEN ("TLSCALL"),  { BFD_RELOC_386_TLS_DESC_CALL,
1460
               BFD_RELOC_X86_64_TLSDESC_CALL },
1461
    OPERAND_TYPE_IMM32_32S_DISP32, true },
1462
#else /* TE_PE */
1463
    { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
1464
               BFD_RELOC_32_SECREL },
1465
    OPERAND_TYPE_IMM32_32S_DISP32, false },
1466
    { STRING_COMMA_LEN ("SECIDX16"), { BFD_RELOC_16_SECIDX,
1467
               BFD_RELOC_16_SECIDX },
1468
    { .bitfield = { .imm16 = 1, .disp16 = 1 } }, false },
1469
    { STRING_COMMA_LEN ("RVA"), { BFD_RELOC_RVA,
1470
               BFD_RELOC_RVA },
1471
    OPERAND_TYPE_IMM32_32S_DISP32, false },
1472
    { STRING_COMMA_LEN ("IMGREL"), { BFD_RELOC_RVA,
1473
               BFD_RELOC_RVA },
1474
    OPERAND_TYPE_IMM32_32S_DISP32, false },
1475
#endif
1476
1477
#undef OPERAND_TYPE_IMM32_32S_DISP32
1478
#undef OPERAND_TYPE_IMM32_32S_64_DISP32
1479
#undef OPERAND_TYPE_IMM32_32S_64_DISP32_64
1480
#undef OPERAND_TYPE_IMM64_DISP64
1481
};
1482
#endif
1483

1484
  /* Various efficient no-op patterns for aligning code labels.
1485
     Note: Don't try to assemble the instructions in the comments.
1486
     0L and 0w are not legal.  */
1487
static const unsigned char f32_1[] =
1488
  {0x90};       /* nop      */
1489
static const unsigned char f32_2[] =
1490
  {0x66,0x90};        /* xchg %ax,%ax   */
1491
static const unsigned char f32_3[] =
1492
  {0x8d,0x76,0x00};     /* leal 0(%esi),%esi  */
1493
#define f32_4 (f32_5 + 1) /* leal 0(%esi,%eiz),%esi */
1494
static const unsigned char f32_5[] =
1495
  {0x2e,0x8d,0x74,0x26,0x00};   /* leal %cs:0(%esi,%eiz),%esi */
1496
static const unsigned char f32_6[] =
1497
  {0x8d,0xb6,0x00,0x00,0x00,0x00};  /* leal 0L(%esi),%esi */
1498
#define f32_7 (f32_8 + 1) /* leal 0L(%esi,%eiz),%esi */
1499
static const unsigned char f32_8[] =
1500
  {0x2e,0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal %cs:0L(%esi,%eiz),%esi */
1501
static const unsigned char f64_3[] =
1502
  {0x48,0x89,0xf6};     /* mov %rsi,%rsi  */
1503
static const unsigned char f64_4[] =
1504
  {0x48,0x8d,0x76,0x00};    /* lea 0(%rsi),%rsi */
1505
#define f64_5 (f64_6 + 1)   /* lea 0(%rsi,%riz),%rsi  */
1506
static const unsigned char f64_6[] =
1507
  {0x2e,0x48,0x8d,0x74,0x26,0x00};  /* lea %cs:0(%rsi,%riz),%rsi  */
1508
static const unsigned char f64_7[] =
1509
  {0x48,0x8d,0xb6,0x00,0x00,0x00,0x00}; /* lea 0L(%rsi),%rsi  */
1510
#define f64_8 (f64_9 + 1)   /* lea 0L(%rsi,%riz),%rsi */
1511
static const unsigned char f64_9[] =
1512
  {0x2e,0x48,0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* lea %cs:0L(%rsi,%riz),%rsi */
1513
#define f16_2 (f64_3 + 1)   /* mov %si,%si  */
1514
static const unsigned char f16_3[] =
1515
  {0x8d,0x74,0x00};     /* lea 0(%si),%si */
1516
#define f16_4 (f16_5 + 1)   /* lea 0W(%si),%si */
1517
static const unsigned char f16_5[] =
1518
  {0x2e,0x8d,0xb4,0x00,0x00};   /* lea %cs:0W(%si),%si  */
1519
static const unsigned char jump_disp8[] =
1520
  {0xeb};       /* jmp disp8         */
1521
static const unsigned char jump32_disp32[] =
1522
  {0xe9};       /* jmp disp32        */
1523
static const unsigned char jump16_disp32[] =
1524
  {0x66,0xe9};        /* jmp disp32        */
1525
/* 32-bit NOPs patterns.  */
1526
static const unsigned char *const f32_patt[] = {
1527
  f32_1, f32_2, f32_3, f32_4, f32_5, f32_6, f32_7, f32_8
1528
};
1529
/* 64-bit NOPs patterns.  */
1530
static const unsigned char *const f64_patt[] = {
1531
  f32_1, f32_2, f64_3, f64_4, f64_5, f64_6, f64_7, f64_8, f64_9
1532
};
1533
/* 16-bit NOPs patterns.  */
1534
static const unsigned char *const f16_patt[] = {
1535
  f32_1, f16_2, f16_3, f16_4, f16_5
1536
};
1537
/* nopl (%[re]ax) */
1538
static const unsigned char alt_3[] =
1539
  {0x0f,0x1f,0x00};
1540
/* nopl 0(%[re]ax) */
1541
static const unsigned char alt_4[] =
1542
  {0x0f,0x1f,0x40,0x00};
1543
/* nopl 0(%[re]ax,%[re]ax,1) */
1544
#define alt_5 (alt_6 + 1)
1545
/* nopw 0(%[re]ax,%[re]ax,1) */
1546
static const unsigned char alt_6[] =
1547
  {0x66,0x0f,0x1f,0x44,0x00,0x00};
1548
/* nopl 0L(%[re]ax) */
1549
static const unsigned char alt_7[] =
1550
  {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1551
/* nopl 0L(%[re]ax,%[re]ax,1) */
1552
#define alt_8 (alt_9 + 1)
1553
/* nopw 0L(%[re]ax,%[re]ax,1) */
1554
static const unsigned char alt_9[] =
1555
  {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1556
/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1557
#define alt_10 (alt_11 + 1)
1558
/* data16 nopw %cs:0L(%eax,%eax,1) */
1559
static const unsigned char alt_11[] =
1560
  {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1561
/* 32-bit and 64-bit NOPs patterns.  */
1562
static const unsigned char *const alt_patt[] = {
1563
  f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
1564
  alt_9, alt_10, alt_11
1565
};
1566
#define alt64_9 (alt64_15 + 6)    /* nopq 0L(%rax,%rax,1)  */
1567
#define alt64_10 (alt64_15 + 5)   /* cs nopq 0L(%rax,%rax,1)  */
1568
/* data16 cs nopq 0L(%rax,%rax,1)  */
1569
#define alt64_11 (alt64_15 + 4)
1570
/* data16 data16 cs nopq 0L(%rax,%rax,1)  */
1571
#define alt64_12 (alt64_15 + 3)
1572
/* data16 data16 data16 cs nopq 0L(%rax,%rax,1)  */
1573
#define alt64_13 (alt64_15 + 2)
1574
/* data16 data16 data16 data16 cs nopq 0L(%rax,%rax,1)  */
1575
#define alt64_14 (alt64_15 + 1)
1576
/* data16 data16 data16 data16 data16 cs nopq 0L(%rax,%rax,1)  */
1577
static const unsigned char alt64_15[] =
1578
  {0x66,0x66,0x66,0x66,0x66,0x2e,0x48,
1579
   0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1580
/* Long 64-bit NOPs patterns.  */
1581
static const unsigned char *const alt64_patt[] = {
1582
  f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
1583
  alt64_9, alt64_10, alt64_11,alt64_12, alt64_13, alt64_14, alt64_15
1584
};
1585
1586
static INLINE int
1587
fits_in_imm7 (offsetT num)
1588
0
{
1589
0
  return (num & 0x7f) == num;
1590
0
}
1591
1592
static INLINE int
1593
fits_in_imm31 (offsetT num)
1594
0
{
1595
0
  return (num & 0x7fffffff) == num;
1596
0
}
1597
1598
/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1599
   single NOP instruction LIMIT.  */
1600
1601
void
1602
i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
1603
0
{
1604
0
  const unsigned char *const *patt = NULL;
1605
0
  int max_single_nop_size;
1606
  /* Maximum number of NOPs before switching to jump over NOPs.  */
1607
0
  int max_number_of_nops;
1608
1609
0
  switch (fragP->fr_type)
1610
0
    {
1611
0
    case rs_fill_nop:
1612
0
    case rs_align_code:
1613
0
      break;
1614
0
    case rs_machine_dependent:
1615
      /* Allow NOP padding for jumps and calls.  */
1616
0
      if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1617
0
    || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1618
0
  break;
1619
      /* Fall through.  */
1620
0
    default:
1621
0
      return;
1622
0
    }
1623
1624
  /* We need to decide which NOP sequence to use for 32bit and
1625
     64bit. When -mtune= is used:
1626
1627
     1. For PROCESSOR_I?86, PROCESSOR_PENTIUM, PROCESSOR_IAMCU, and
1628
     PROCESSOR_GENERIC32, f32_patt will be used.
1629
     2. For the rest, alt_patt will be used.
1630
1631
     When -mtune= isn't used, alt_patt will be used if
1632
     cpu_arch_isa_flags has CpuNop.  Otherwise, f32_patt/f64_patt will
1633
     be used.
1634
1635
     When -march= or .arch is used, we can't use anything beyond
1636
     cpu_arch_isa_flags.   */
1637
1638
0
  if (fragP->tc_frag_data.code == CODE_16BIT)
1639
0
    {
1640
0
      patt = f16_patt;
1641
0
      max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1642
      /* Limit number of NOPs to 2 in 16-bit mode.  */
1643
0
      max_number_of_nops = 2;
1644
0
    }
1645
0
  else
1646
0
    {
1647
0
      patt = fragP->tc_frag_data.code == CODE_64BIT ? f64_patt : f32_patt;
1648
0
      if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
1649
0
  {
1650
    /* PROCESSOR_UNKNOWN means that all ISAs may be used, unless
1651
       explicitly disabled.  */
1652
0
    switch (fragP->tc_frag_data.tune)
1653
0
      {
1654
0
      case PROCESSOR_UNKNOWN:
1655
        /* We use cpu_arch_isa_flags to check if we SHOULD
1656
     optimize with nops.  */
1657
0
        if (fragP->tc_frag_data.isanop)
1658
0
    patt = alt_patt;
1659
0
        break;
1660
1661
0
      case PROCESSOR_CORE:
1662
0
      case PROCESSOR_CORE2:
1663
0
      case PROCESSOR_COREI7:
1664
0
        if (fragP->tc_frag_data.cpunop)
1665
0
    {
1666
0
      if (fragP->tc_frag_data.code == CODE_64BIT)
1667
0
        patt = alt64_patt;
1668
0
      else
1669
0
        patt = alt_patt;
1670
0
    }
1671
0
        break;
1672
1673
0
      case PROCESSOR_PENTIUMPRO:
1674
0
      case PROCESSOR_PENTIUM4:
1675
0
      case PROCESSOR_NOCONA:
1676
0
      case PROCESSOR_GENERIC64:
1677
0
      case PROCESSOR_K6:
1678
0
      case PROCESSOR_ATHLON:
1679
0
      case PROCESSOR_K8:
1680
0
      case PROCESSOR_AMDFAM10:
1681
0
      case PROCESSOR_BD:
1682
0
      case PROCESSOR_ZNVER:
1683
0
      case PROCESSOR_BT:
1684
0
        if (fragP->tc_frag_data.cpunop)
1685
0
    patt = alt_patt;
1686
0
        break;
1687
1688
0
      case PROCESSOR_I386:
1689
0
      case PROCESSOR_I486:
1690
0
      case PROCESSOR_PENTIUM:
1691
0
      case PROCESSOR_I686:
1692
0
      case PROCESSOR_IAMCU:
1693
0
      case PROCESSOR_GENERIC32:
1694
0
        break;
1695
0
      case PROCESSOR_NONE:
1696
0
        abort ();
1697
0
      }
1698
0
  }
1699
0
      else
1700
0
  {
1701
0
    switch (fragP->tc_frag_data.tune)
1702
0
      {
1703
0
      case PROCESSOR_UNKNOWN:
1704
        /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
1705
     PROCESSOR_UNKNOWN.  */
1706
0
        abort ();
1707
0
        break;
1708
1709
0
      default:
1710
        /* We use cpu_arch_isa_flags to check if we CAN optimize
1711
     with nops.  */
1712
0
        if (fragP->tc_frag_data.isanop)
1713
0
    patt = alt_patt;
1714
0
        break;
1715
1716
0
      case PROCESSOR_NONE:
1717
0
        abort ();
1718
0
      }
1719
0
  }
1720
1721
0
      if (patt != alt_patt && patt != alt64_patt)
1722
0
  {
1723
0
    max_single_nop_size = patt == f32_patt ? ARRAY_SIZE (f32_patt)
1724
0
             : ARRAY_SIZE (f64_patt);
1725
    /* Limit number of NOPs to 2 for older processors.  */
1726
0
    max_number_of_nops = 2;
1727
0
  }
1728
0
      else
1729
0
  {
1730
0
    max_single_nop_size = patt == alt_patt
1731
0
        ? ARRAY_SIZE (alt_patt)
1732
0
        : ARRAY_SIZE (alt64_patt);
1733
    /* Limit number of NOPs to 7 for newer processors.  */
1734
0
    max_number_of_nops = 7;
1735
0
  }
1736
0
    }
1737
1738
0
  if (limit == 0)
1739
0
    limit = max_single_nop_size;
1740
1741
0
  if (limit > max_single_nop_size || limit < 1)
1742
0
    {
1743
0
      as_bad_where (fragP->fr_file, fragP->fr_line,
1744
0
        _("invalid single nop size: %d "
1745
0
          "(expect within [0, %d])"),
1746
0
        limit, max_single_nop_size);
1747
0
      return;
1748
0
    }
1749
1750
  /* Emit a plain NOP first when the last thing we saw may not have been
1751
     a proper instruction (e.g. a stand-alone prefix or .byte).  */
1752
0
  if (!fragP->tc_frag_data.last_insn_normal)
1753
0
    {
1754
0
      *where++ = 0x90;
1755
0
      --count;
1756
0
    }
1757
1758
0
  if ((count / max_single_nop_size) > max_number_of_nops)
1759
0
    {
1760
      /* Generate jump over NOPs.  */
1761
0
      offsetT disp = count - 2;
1762
0
      if (fits_in_imm7 (disp))
1763
0
  {
1764
    /* Use "jmp disp8" if possible.  */
1765
0
    count = disp;
1766
0
    where[0] = jump_disp8[0];
1767
0
    where[1] = count;
1768
0
    where += 2;
1769
0
  }
1770
0
      else
1771
0
  {
1772
0
    unsigned int size_of_jump;
1773
1774
0
    if (flag_code == CODE_16BIT)
1775
0
      {
1776
0
        where[0] = jump16_disp32[0];
1777
0
        where[1] = jump16_disp32[1];
1778
0
        size_of_jump = 2;
1779
0
      }
1780
0
    else
1781
0
      {
1782
0
        where[0] = jump32_disp32[0];
1783
0
        size_of_jump = 1;
1784
0
      }
1785
1786
0
    count -= size_of_jump + 4;
1787
0
    if (!fits_in_imm31 (count))
1788
0
      {
1789
0
        as_bad_where (fragP->fr_file, fragP->fr_line,
1790
0
          _("jump over nop padding out of range"));
1791
0
        return;
1792
0
      }
1793
1794
0
    md_number_to_chars (where + size_of_jump, count, 4);
1795
0
    where += size_of_jump + 4;
1796
0
  }
1797
0
    }
1798
1799
0
  int non_repeat = count % limit;
1800
0
  if (non_repeat)
1801
0
    {
1802
0
      memcpy (where, patt[non_repeat - 1], non_repeat);
1803
0
      where += non_repeat;
1804
0
      count -= non_repeat;
1805
0
    }
1806
1807
0
  if (fragP->fr_type != rs_machine_dependent)
1808
0
    {
1809
      /* Set up the frag so that everything we have emitted so far is
1810
   included in fr_fix.  The repeating larger nop only needs to
1811
   be written once to the frag memory.  */
1812
0
      fragP->fr_fix = where - fragP->fr_literal;
1813
0
      if (count != 0)
1814
0
  {
1815
0
    fragP->fr_var = limit;
1816
0
    count = limit;
1817
0
  }
1818
0
    }
1819
1820
0
  const unsigned char *nops = patt[limit - 1];
1821
0
  while (count)
1822
0
    {
1823
0
      memcpy (where, nops, limit);
1824
0
      where += limit;
1825
0
      count -= limit;
1826
0
    }
1827
0
}
1828
1829
static INLINE int
1830
operand_type_all_zero (const union i386_operand_type *x)
1831
30.6k
{
1832
30.6k
  switch (ARRAY_SIZE(x->array))
1833
30.6k
    {
1834
0
    case 3:
1835
0
      if (x->array[2])
1836
0
  return 0;
1837
      /* Fall through.  */
1838
0
    case 2:
1839
0
      if (x->array[1])
1840
0
  return 0;
1841
      /* Fall through.  */
1842
30.6k
    case 1:
1843
30.6k
      return !x->array[0];
1844
0
    default:
1845
0
      abort ();
1846
30.6k
    }
1847
30.6k
}
1848
1849
static INLINE void
1850
operand_type_set (union i386_operand_type *x, unsigned int v)
1851
23.8k
{
1852
23.8k
  switch (ARRAY_SIZE(x->array))
1853
23.8k
    {
1854
0
    case 3:
1855
0
      x->array[2] = v;
1856
      /* Fall through.  */
1857
0
    case 2:
1858
0
      x->array[1] = v;
1859
      /* Fall through.  */
1860
23.8k
    case 1:
1861
23.8k
      x->array[0] = v;
1862
      /* Fall through.  */
1863
23.8k
      break;
1864
0
    default:
1865
0
      abort ();
1866
23.8k
    }
1867
1868
23.8k
  x->bitfield.class = ClassNone;
1869
23.8k
  x->bitfield.instance = InstanceNone;
1870
23.8k
}
1871
1872
static INLINE int
1873
operand_type_equal (const union i386_operand_type *x,
1874
        const union i386_operand_type *y)
1875
3.58k
{
1876
3.58k
  switch (ARRAY_SIZE(x->array))
1877
3.58k
    {
1878
0
    case 3:
1879
0
      if (x->array[2] != y->array[2])
1880
0
  return 0;
1881
      /* Fall through.  */
1882
0
    case 2:
1883
0
      if (x->array[1] != y->array[1])
1884
0
  return 0;
1885
      /* Fall through.  */
1886
3.58k
    case 1:
1887
3.58k
      return x->array[0] == y->array[0];
1888
0
      break;
1889
0
    default:
1890
0
      abort ();
1891
3.58k
    }
1892
3.58k
}
1893
1894
static INLINE bool
1895
_is_cpu (const i386_cpu_attr *a, enum i386_cpu cpu)
1896
147k
{
1897
147k
  switch (cpu)
1898
147k
    {
1899
3.67k
    case Cpu287:      return a->bitfield.cpu287;
1900
3.67k
    case Cpu387:      return a->bitfield.cpu387;
1901
0
    case Cpu3dnow:    return a->bitfield.cpu3dnow;
1902
0
    case Cpu3dnowA:   return a->bitfield.cpu3dnowa;
1903
7.00k
    case CpuAVX:      return a->bitfield.cpuavx;
1904
19
    case CpuHLE:      return a->bitfield.cpuhle;
1905
3.96k
    case CpuAVX512F:  return a->bitfield.cpuavx512f;
1906
3.67k
    case CpuAVX512VL: return a->bitfield.cpuavx512vl;
1907
5.96k
    case CpuAPX_F:    return a->bitfield.cpuapx_f;
1908
0
    case CpuAVX10_2:  return a->bitfield.cpuavx10_2;
1909
0
    case CpuAMX_TRANSPOSE:  return a->bitfield.cpuamx_transpose;
1910
0
    case Cpu64:       return a->bitfield.cpu64;
1911
0
    case CpuNo64:     return a->bitfield.cpuno64;
1912
119k
    default:
1913
119k
      gas_assert (cpu < CpuAttrEnums);
1914
147k
    }
1915
119k
  return a->bitfield.isa == cpu + 1u;
1916
147k
}
1917
1918
static INLINE bool
1919
is_cpu (const insn_template *t, enum i386_cpu cpu)
1920
143k
{
1921
143k
  return _is_cpu(&t->cpu, cpu);
1922
143k
}
1923
1924
static INLINE bool
1925
maybe_cpu (const insn_template *t, enum i386_cpu cpu)
1926
3.33k
{
1927
3.33k
  return _is_cpu(&t->cpu_any, cpu);
1928
3.33k
}
1929
1930
static i386_cpu_flags cpu_flags_from_attr (i386_cpu_attr a)
1931
129k
{
1932
129k
  const unsigned int bps = sizeof (a.array[0]) * CHAR_BIT;
1933
129k
  i386_cpu_flags f = { .array[0] = 0 };
1934
1935
129k
  switch (ARRAY_SIZE (a.array))
1936
129k
    {
1937
129k
    case 1:
1938
129k
      f.array[CpuAttrEnums / bps]
1939
129k
#ifndef WORDS_BIGENDIAN
1940
129k
  |= (a.array[0] >> CpuIsaBits) << (CpuAttrEnums % bps);
1941
#else
1942
  |= (a.array[0] << CpuIsaBits) >> (CpuAttrEnums % bps);
1943
#endif
1944
129k
      if (CpuMax / bps > CpuAttrEnums / bps)
1945
129k
  f.array[CpuAttrEnums / bps + 1]
1946
129k
#ifndef WORDS_BIGENDIAN
1947
129k
    = (a.array[0] >> CpuIsaBits) >> (bps - CpuAttrEnums % bps);
1948
#else
1949
    = (a.array[0] << CpuIsaBits) << (bps - CpuAttrEnums % bps);
1950
#endif
1951
129k
      break;
1952
1953
0
    default:
1954
0
      abort ();
1955
129k
    }
1956
1957
129k
  if (a.bitfield.isa)
1958
18.5k
#ifndef WORDS_BIGENDIAN
1959
18.5k
    f.array[(a.bitfield.isa - 1) / bps] |= 1u << ((a.bitfield.isa - 1) % bps);
1960
#else
1961
    f.array[(a.bitfield.isa - 1) / bps] |= 1u << (~(a.bitfield.isa - 1) % bps);
1962
#endif
1963
1964
129k
  return f;
1965
129k
}
1966
1967
static INLINE int
1968
cpu_flags_all_zero (const union i386_cpu_flags *x)
1969
123k
{
1970
123k
  switch (ARRAY_SIZE(x->array))
1971
123k
    {
1972
123k
    case 6:
1973
123k
      if (x->array[5])
1974
758
  return 0;
1975
      /* Fall through.  */
1976
122k
    case 5:
1977
122k
      if (x->array[4])
1978
4.46k
  return 0;
1979
      /* Fall through.  */
1980
118k
    case 4:
1981
118k
      if (x->array[3])
1982
31
  return 0;
1983
      /* Fall through.  */
1984
118k
    case 3:
1985
118k
      if (x->array[2])
1986
4
  return 0;
1987
      /* Fall through.  */
1988
118k
    case 2:
1989
118k
      if (x->array[1])
1990
19
  return 0;
1991
      /* Fall through.  */
1992
118k
    case 1:
1993
118k
      return !x->array[0];
1994
0
    default:
1995
0
      abort ();
1996
123k
    }
1997
123k
}
1998
1999
static INLINE int
2000
cpu_flags_equal (const union i386_cpu_flags *x,
2001
     const union i386_cpu_flags *y)
2002
18.4k
{
2003
18.4k
  switch (ARRAY_SIZE(x->array))
2004
18.4k
    {
2005
18.4k
    case 6:
2006
18.4k
      if (x->array[5] != y->array[5])
2007
130
  return 0;
2008
      /* Fall through.  */
2009
18.3k
    case 5:
2010
18.3k
      if (x->array[4] != y->array[4])
2011
3.65k
  return 0;
2012
      /* Fall through.  */
2013
14.6k
    case 4:
2014
14.6k
      if (x->array[3] != y->array[3])
2015
17
  return 0;
2016
      /* Fall through.  */
2017
14.6k
    case 3:
2018
14.6k
      if (x->array[2] != y->array[2])
2019
4
  return 0;
2020
      /* Fall through.  */
2021
14.6k
    case 2:
2022
14.6k
      if (x->array[1] != y->array[1])
2023
2
  return 0;
2024
      /* Fall through.  */
2025
14.6k
    case 1:
2026
14.6k
      return x->array[0] == y->array[0];
2027
0
      break;
2028
0
    default:
2029
0
      abort ();
2030
18.4k
    }
2031
18.4k
}
2032
2033
static INLINE int
2034
cpu_flags_check_cpu64 (const insn_template *t)
2035
61.6k
{
2036
61.6k
  return flag_code == CODE_64BIT
2037
61.6k
   ? !t->cpu.bitfield.cpuno64
2038
61.6k
   : !t->cpu.bitfield.cpu64;
2039
61.6k
}
2040
2041
static INLINE i386_cpu_flags
2042
cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
2043
36.4k
{
2044
36.4k
  switch (ARRAY_SIZE (x.array))
2045
36.4k
    {
2046
36.4k
    case 6:
2047
36.4k
      x.array [5] &= y.array [5];
2048
      /* Fall through.  */
2049
36.4k
    case 5:
2050
36.4k
      x.array [4] &= y.array [4];
2051
      /* Fall through.  */
2052
36.4k
    case 4:
2053
36.4k
      x.array [3] &= y.array [3];
2054
      /* Fall through.  */
2055
36.4k
    case 3:
2056
36.4k
      x.array [2] &= y.array [2];
2057
      /* Fall through.  */
2058
36.4k
    case 2:
2059
36.4k
      x.array [1] &= y.array [1];
2060
      /* Fall through.  */
2061
36.4k
    case 1:
2062
36.4k
      x.array [0] &= y.array [0];
2063
36.4k
      break;
2064
0
    default:
2065
0
      abort ();
2066
36.4k
    }
2067
36.4k
  return x;
2068
36.4k
}
2069
2070
static INLINE i386_cpu_flags
2071
cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
2072
3.35k
{
2073
3.35k
  switch (ARRAY_SIZE (x.array))
2074
3.35k
    {
2075
3.35k
    case 6:
2076
3.35k
      x.array [5] |= y.array [5];
2077
      /* Fall through.  */
2078
3.35k
    case 5:
2079
3.35k
      x.array [4] |= y.array [4];
2080
      /* Fall through.  */
2081
3.35k
    case 4:
2082
3.35k
      x.array [3] |= y.array [3];
2083
      /* Fall through.  */
2084
3.35k
    case 3:
2085
3.35k
      x.array [2] |= y.array [2];
2086
      /* Fall through.  */
2087
3.35k
    case 2:
2088
3.35k
      x.array [1] |= y.array [1];
2089
      /* Fall through.  */
2090
3.35k
    case 1:
2091
3.35k
      x.array [0] |= y.array [0];
2092
3.35k
      break;
2093
0
    default:
2094
0
      abort ();
2095
3.35k
    }
2096
3.35k
  return x;
2097
3.35k
}
2098
2099
static INLINE i386_cpu_flags
2100
cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
2101
2.36k
{
2102
2.36k
  switch (ARRAY_SIZE (x.array))
2103
2.36k
    {
2104
2.36k
    case 6:
2105
2.36k
      x.array [5] &= ~y.array [5];
2106
      /* Fall through.  */
2107
2.36k
    case 5:
2108
2.36k
      x.array [4] &= ~y.array [4];
2109
      /* Fall through.  */
2110
2.36k
    case 4:
2111
2.36k
      x.array [3] &= ~y.array [3];
2112
      /* Fall through.  */
2113
2.36k
    case 3:
2114
2.36k
      x.array [2] &= ~y.array [2];
2115
      /* Fall through.  */
2116
2.36k
    case 2:
2117
2.36k
      x.array [1] &= ~y.array [1];
2118
      /* Fall through.  */
2119
2.36k
    case 1:
2120
2.36k
      x.array [0] &= ~y.array [0];
2121
2.36k
      break;
2122
0
    default:
2123
0
      abort ();
2124
2.36k
    }
2125
2.36k
  return x;
2126
2.36k
}
2127
2128
static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
2129
2130
static INLINE bool need_evex_encoding (const insn_template *t)
2131
173
{
2132
173
  return pp.encoding == encoding_evex
2133
168
  || pp.encoding == encoding_evex512
2134
168
  || pp.has_nf
2135
168
  || (t->opcode_modifier.vex && pp.encoding == encoding_egpr)
2136
168
  || i.mask.reg;
2137
173
}
2138
2139
119k
#define CPU_FLAGS_ARCH_MATCH    0x1
2140
93.4k
#define CPU_FLAGS_64BIT_MATCH   0x2
2141
2142
#define CPU_FLAGS_PERFECT_MATCH \
2143
61.6k
  (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
2144
2145
static INLINE bool set_oszc_flags (unsigned int oszc_shift)
2146
17
{
2147
17
  if (i.oszc_flags & oszc_shift)
2148
3
    {
2149
3
      as_bad (_("same oszc flag used twice"));
2150
3
      return false;
2151
3
    }
2152
14
  i.oszc_flags |= oszc_shift;
2153
14
  return true;
2154
17
}
2155
2156
/* Handle SCC OSZC flags.  */
2157
2158
static int
2159
check_Scc_OszcOperations (const char *l)
2160
42
{
2161
42
  const char *suffix_string = l;
2162
2163
210
  while (is_whitespace (*suffix_string))
2164
168
    suffix_string++;
2165
2166
  /* If {oszc flags} is absent, just return.  */
2167
42
  if (*suffix_string != '{')
2168
4
    return 0;
2169
2170
  /* Skip '{'.  */
2171
38
  suffix_string++;
2172
2173
  /* For .insn require 'scc=' as the first element.  */
2174
38
  if (dot_insn ())
2175
8
    {
2176
8
      char *copy;
2177
8
      valueT val;
2178
2179
8
      while (is_whitespace (*suffix_string))
2180
0
  suffix_string++;
2181
2182
8
      if (strncasecmp (suffix_string, "scc", 3) == 0)
2183
8
  suffix_string += 3;
2184
0
      else
2185
0
  {
2186
0
    as_bad (_("unrecognized pseudo-suffix"));
2187
0
    return -1;
2188
0
  }
2189
2190
8
      while (is_whitespace (*suffix_string))
2191
0
  suffix_string++;
2192
2193
8
      if (*suffix_string == '=')
2194
8
  suffix_string++;
2195
0
      else
2196
0
  {
2197
0
    as_bad (_("unrecognized pseudo-suffix"));
2198
0
    return -1;
2199
0
  }
2200
2201
8
      copy = xstrdup (suffix_string);
2202
      /* No need to save/restore input_line_pointer; that's done in the
2203
   caller already.  */
2204
8
      input_line_pointer = copy;
2205
8
      val = get_absolute_expression ();
2206
8
      suffix_string += input_line_pointer - copy;
2207
8
      free (copy);
2208
2209
8
      if (val > 0xf)
2210
0
  {
2211
0
    as_bad (_("scc= value must be between 0 and 15 (decimal)"));
2212
0
    return -1;
2213
0
  }
2214
2215
8
      i.scc = val;
2216
2217
      /* Permit dfv= to be absent (implying all flag values being zero).  */
2218
8
      if (*suffix_string == '}')
2219
8
  return suffix_string + 1 - l;
2220
2221
0
      if (*suffix_string != ',')
2222
0
  goto bad;
2223
0
      suffix_string++;
2224
0
    }
2225
2226
  /* Parse 'dfv='.  */
2227
30
  while (is_whitespace (*suffix_string))
2228
0
    suffix_string++;
2229
2230
30
  if (strncasecmp (suffix_string, "dfv", 3) == 0)
2231
14
    suffix_string += 3;
2232
16
  else
2233
16
    {
2234
16
      as_bad (_("unrecognized pseudo-suffix"));
2235
16
      return -1;
2236
16
    }
2237
2238
14
  while (is_whitespace (*suffix_string))
2239
0
    suffix_string++;
2240
2241
14
  if (*suffix_string == '=')
2242
14
    suffix_string++;
2243
0
  else
2244
0
    {
2245
0
      as_bad (_("unrecognized pseudo-suffix"));
2246
0
      return -1;
2247
0
    }
2248
2249
  /* Parse 'of, sf, zf, cf}'.  */
2250
17
  while (*suffix_string)
2251
17
    {
2252
17
      while (is_whitespace (*suffix_string))
2253
0
  suffix_string++;
2254
2255
      /* Return for '{dfv=}'.  */
2256
17
      if (*suffix_string == '}')
2257
0
  return suffix_string + 1 - l;
2258
2259
17
      if (strncasecmp (suffix_string, "of", 2) == 0)
2260
3
  {
2261
3
    if (!set_oszc_flags (OSZC_OF))
2262
0
      return -1;
2263
3
  }
2264
14
      else if (strncasecmp (suffix_string, "sf", 2) == 0)
2265
4
  {
2266
4
    if (!set_oszc_flags (OSZC_SF))
2267
0
      return -1;
2268
4
  }
2269
10
      else if (strncasecmp (suffix_string, "zf", 2) == 0)
2270
0
  {
2271
0
    if (!set_oszc_flags (OSZC_ZF))
2272
0
      return -1;
2273
0
  }
2274
10
      else if (strncasecmp (suffix_string, "cf", 2) == 0)
2275
10
  {
2276
10
    if (!set_oszc_flags (OSZC_CF))
2277
3
      return -1;
2278
10
  }
2279
0
      else
2280
0
  {
2281
0
    as_bad (_("unrecognized oszc flags or illegal `,' in pseudo-suffix"));
2282
0
    return -1;
2283
0
  }
2284
2285
14
      suffix_string += 2;
2286
2287
14
      while (is_whitespace (*suffix_string))
2288
0
  suffix_string++;
2289
2290
14
      if (*suffix_string == '}')
2291
4
  return ++suffix_string - l;
2292
2293
10
      if (*suffix_string != ',')
2294
7
  break;
2295
3
      suffix_string ++;
2296
3
    }
2297
2298
7
 bad:
2299
7
  as_bad (_("missing `}' or `,' in pseudo-suffix"));
2300
7
  return -1;
2301
14
}
2302
2303
/* Return CPU flags match bits. */
2304
2305
static int
2306
cpu_flags_match (const insn_template *t)
2307
61.6k
{
2308
61.6k
  i386_cpu_flags cpu, active, all = cpu_flags_from_attr (t->cpu);
2309
61.6k
  i386_cpu_flags any = cpu_flags_from_attr (t->cpu_any);
2310
61.6k
  int match = cpu_flags_check_cpu64 (t) ? CPU_FLAGS_64BIT_MATCH : 0;
2311
2312
61.6k
  all.bitfield.cpu64 = 0;
2313
61.6k
  all.bitfield.cpuno64 = 0;
2314
61.6k
  gas_assert (!any.bitfield.cpu64);
2315
61.6k
  gas_assert (!any.bitfield.cpuno64);
2316
2317
61.6k
  if (cpu_flags_all_zero (&all) && cpu_flags_all_zero (&any))
2318
43.2k
    {
2319
      /* This instruction is available on all archs.  */
2320
43.2k
      return match | CPU_FLAGS_ARCH_MATCH;
2321
43.2k
    }
2322
2323
  /* This instruction is available only on some archs.  */
2324
2325
  /* Dual VEX/EVEX templates may need stripping of one of the flags.  */
2326
18.4k
  if (t->opcode_modifier.vex && t->opcode_modifier.evex)
2327
173
    {
2328
      /* Dual AVX/AVX512 templates need to retain AVX512* only if we already
2329
   know that EVEX encoding will be needed.  */
2330
173
      if ((any.bitfield.cpuavx || any.bitfield.cpuavx2
2331
11
     || any.bitfield.cpufma || any.bitfield.cpuf16c)
2332
162
    && (any.bitfield.cpuavx512f || any.bitfield.cpuavx512vl))
2333
162
  {
2334
162
    if (need_evex_encoding (t)
2335
157
        || (any.bitfield.cpufma && !cpu_arch_flags.bitfield.cpufma)
2336
157
        || (any.bitfield.cpuf16c && !cpu_arch_flags.bitfield.cpuf16c))
2337
5
      {
2338
5
        any.bitfield.cpuavx = 0;
2339
5
        any.bitfield.cpuavx2 = 0;
2340
5
        any.bitfield.cpufma = 0;
2341
5
        any.bitfield.cpuf16c = 0;
2342
5
      }
2343
    /* need_evex_encoding(t) isn't reliable before operands were
2344
       parsed.  */
2345
157
    else if (i.operands)
2346
0
      {
2347
0
        any.bitfield.cpuavx512f = 0;
2348
0
        any.bitfield.cpuavx512vl = 0;
2349
0
      }
2350
162
  }
2351
2352
      /* Dual non-APX/APX templates need massaging from what APX_F() in the
2353
         opcode table has produced.  While the direct transformation of the
2354
         incoming cpuid&(cpuid|APX_F) would be to cpuid&(cpuid) / cpuid&(APX_F)
2355
         respectively, it's cheaper to move to just cpuid / cpuid&APX_F
2356
         instead.  */
2357
173
      if (any.bitfield.cpuapx_f
2358
11
    && (any.bitfield.cpubmi || any.bitfield.cpubmi2
2359
11
        || any.bitfield.cpuavx512f || any.bitfield.cpuavx512bw
2360
11
        || any.bitfield.cpuavx512dq || any.bitfield.cpuamx_tile
2361
7
        || any.bitfield.cpucmpccxadd || any.bitfield.cpuuser_msr
2362
6
        || any.bitfield.cpumsr_imm || any.bitfield.cpuamx_transpose
2363
0
        || any.bitfield.cpuamx_movrs))
2364
11
  {
2365
    /* These checks (verifying that APX_F() was properly used in the
2366
       opcode table entry) make sure there's no need for an "else" to
2367
       the "if()" below.  */
2368
11
    gas_assert (!cpu_flags_all_zero (&all));
2369
2370
11
    cpu = cpu_flags_and (all, any);
2371
11
    gas_assert (cpu_flags_equal (&cpu, &all));
2372
2373
11
    if (need_evex_encoding (t))
2374
0
      all = any;
2375
2376
11
    memset (&any, 0, sizeof (any));
2377
11
  }
2378
173
    }
2379
18.2k
  else if (t->opcode_modifier.evex
2380
     /* Implicitly !t->opcode_modifier.vex.  */
2381
4.17k
     && all.bitfield.cpuapx_f
2382
1
     && (t->opcode_modifier.nf
2383
1
         || (all.bitfield.cpuadx && t->opcode_modifier.vexvvvv)))
2384
0
    {
2385
      /* APX_NDD can't be combined with other ISAs in the opcode table.
2386
   Respective entries (ADCX, ADOX, LZCNT, POPCNT, and TZCNT) use APX_F
2387
   instead, which are amended here.  No need to clear cpuapx_f, though. */
2388
0
      all.bitfield.cpuapx_ndd = true;
2389
0
    }
2390
2391
18.4k
  if (flag_code != CODE_64BIT)
2392
2.35k
    active = cpu_flags_and_not (cpu_arch_flags, cpu_64_flags);
2393
16.0k
  else
2394
16.0k
    active = cpu_arch_flags;
2395
18.4k
  cpu = cpu_flags_and (all, active);
2396
18.4k
  if (cpu_flags_equal (&cpu, &all))
2397
14.6k
    {
2398
      /* AVX and AVX2 present at the same time express an operand size
2399
   dependency - strip AVX2 for the purposes here.  The operand size
2400
   dependent check occurs in check_vecOperands().  */
2401
14.6k
      if (any.bitfield.cpuavx && any.bitfield.cpuavx2)
2402
0
  any.bitfield.cpuavx2 = 0;
2403
2404
14.6k
      cpu = cpu_flags_and (any, active);
2405
14.6k
      if (cpu_flags_all_zero (&any) || !cpu_flags_all_zero (&cpu))
2406
14.5k
  match |= CPU_FLAGS_ARCH_MATCH;
2407
14.6k
    }
2408
18.4k
  return match;
2409
18.4k
}
2410
2411
static INLINE i386_operand_type
2412
operand_type_and (i386_operand_type x, i386_operand_type y)
2413
51.7k
{
2414
51.7k
  if (x.bitfield.class != y.bitfield.class)
2415
21.6k
    x.bitfield.class = ClassNone;
2416
51.7k
  if (x.bitfield.instance != y.bitfield.instance)
2417
4.49k
    x.bitfield.instance = InstanceNone;
2418
2419
51.7k
  switch (ARRAY_SIZE (x.array))
2420
51.7k
    {
2421
0
    case 3:
2422
0
      x.array [2] &= y.array [2];
2423
      /* Fall through.  */
2424
0
    case 2:
2425
0
      x.array [1] &= y.array [1];
2426
      /* Fall through.  */
2427
51.7k
    case 1:
2428
51.7k
      x.array [0] &= y.array [0];
2429
51.7k
      break;
2430
0
    default:
2431
0
      abort ();
2432
51.7k
    }
2433
51.7k
  return x;
2434
51.7k
}
2435
2436
static INLINE i386_operand_type
2437
operand_type_and_not (i386_operand_type x, i386_operand_type y)
2438
8.57k
{
2439
8.57k
  gas_assert (y.bitfield.class == ClassNone);
2440
8.57k
  gas_assert (y.bitfield.instance == InstanceNone);
2441
2442
8.57k
  switch (ARRAY_SIZE (x.array))
2443
8.57k
    {
2444
0
    case 3:
2445
0
      x.array [2] &= ~y.array [2];
2446
      /* Fall through.  */
2447
0
    case 2:
2448
0
      x.array [1] &= ~y.array [1];
2449
      /* Fall through.  */
2450
8.57k
    case 1:
2451
8.57k
      x.array [0] &= ~y.array [0];
2452
8.57k
      break;
2453
0
    default:
2454
0
      abort ();
2455
8.57k
    }
2456
8.57k
  return x;
2457
8.57k
}
2458
2459
static INLINE i386_operand_type
2460
operand_type_or (i386_operand_type x, i386_operand_type y)
2461
25.7k
{
2462
25.7k
  gas_assert (x.bitfield.class == ClassNone ||
2463
25.7k
              y.bitfield.class == ClassNone ||
2464
25.7k
              x.bitfield.class == y.bitfield.class);
2465
25.7k
  gas_assert (x.bitfield.instance == InstanceNone ||
2466
25.7k
              y.bitfield.instance == InstanceNone ||
2467
25.7k
              x.bitfield.instance == y.bitfield.instance);
2468
2469
25.7k
  switch (ARRAY_SIZE (x.array))
2470
25.7k
    {
2471
0
    case 3:
2472
0
      x.array [2] |= y.array [2];
2473
      /* Fall through.  */
2474
0
    case 2:
2475
0
      x.array [1] |= y.array [1];
2476
      /* Fall through.  */
2477
25.7k
    case 1:
2478
25.7k
      x.array [0] |= y.array [0];
2479
25.7k
      break;
2480
0
    default:
2481
0
      abort ();
2482
25.7k
    }
2483
25.7k
  return x;
2484
25.7k
}
2485
2486
static INLINE i386_operand_type
2487
operand_type_xor (i386_operand_type x, i386_operand_type y)
2488
0
{
2489
0
  gas_assert (y.bitfield.class == ClassNone);
2490
0
  gas_assert (y.bitfield.instance == InstanceNone);
2491
2492
0
  switch (ARRAY_SIZE (x.array))
2493
0
    {
2494
0
    case 3:
2495
0
      x.array [2] ^= y.array [2];
2496
      /* Fall through.  */
2497
0
    case 2:
2498
0
      x.array [1] ^= y.array [1];
2499
      /* Fall through.  */
2500
0
    case 1:
2501
0
      x.array [0] ^= y.array [0];
2502
0
      break;
2503
0
    default:
2504
0
      abort ();
2505
0
    }
2506
0
  return x;
2507
0
}
2508
2509
static const i386_operand_type anydisp = {
2510
  .bitfield = { .disp8 = 1, .disp16 = 1, .disp32 = 1, .disp64 = 1 }
2511
};
2512
2513
enum operand_type
2514
{
2515
  reg,
2516
  imm,
2517
  disp,
2518
  anymem
2519
};
2520
2521
static INLINE int
2522
operand_type_check (i386_operand_type t, enum operand_type c)
2523
63.2k
{
2524
63.2k
  switch (c)
2525
63.2k
    {
2526
5
    case reg:
2527
5
      return t.bitfield.class == Reg;
2528
2529
15.7k
    case imm:
2530
15.7k
      return (t.bitfield.imm8
2531
12.7k
        || t.bitfield.imm8s
2532
12.5k
        || t.bitfield.imm16
2533
11.9k
        || t.bitfield.imm32
2534
11.7k
        || t.bitfield.imm32s
2535
10.5k
        || t.bitfield.imm64);
2536
2537
43.6k
    case disp:
2538
43.6k
      return (t.bitfield.disp8
2539
39.2k
        || t.bitfield.disp16
2540
38.7k
        || t.bitfield.disp32
2541
35.5k
        || t.bitfield.disp64);
2542
2543
3.81k
    case anymem:
2544
3.81k
      return (t.bitfield.disp8
2545
1.71k
        || t.bitfield.disp16
2546
1.65k
        || t.bitfield.disp32
2547
1.65k
        || t.bitfield.disp64
2548
1.57k
        || t.bitfield.baseindex);
2549
2550
0
    default:
2551
0
      abort ();
2552
63.2k
    }
2553
2554
0
  return 0;
2555
63.2k
}
2556
2557
static INLINE const i386_operand_type *
2558
get_operand_types (const insn_template *t)
2559
59.0k
{
2560
59.0k
  return &i386_operand_types[t->operand_ref];
2561
59.0k
}
2562
2563
/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit size
2564
   between operand GIVEN and operand WANTED for instruction template T.  */
2565
2566
static INLINE int
2567
match_operand_size (const insn_template *t, unsigned int wanted,
2568
        unsigned int given)
2569
8.72k
{
2570
8.72k
  const i386_operand_type *t_types = get_operand_types (t);
2571
2572
8.72k
  return !((i.types[given].bitfield.byte
2573
114
      && !t_types[wanted].bitfield.byte)
2574
8.69k
     || (i.types[given].bitfield.word
2575
5
         && !t_types[wanted].bitfield.word)
2576
8.68k
     || (i.types[given].bitfield.dword
2577
6.02k
         && !t_types[wanted].bitfield.dword)
2578
7.30k
     || (i.types[given].bitfield.qword
2579
353
         && (!t_types[wanted].bitfield.qword
2580
       /* Don't allow 64-bit (memory) operands outside of 64-bit
2581
          mode, when they're used where a 64-bit GPR could also
2582
          be used.  Checking is needed for Intel Syntax only.  */
2583
279
       || (intel_syntax
2584
265
           && flag_code != CODE_64BIT
2585
0
           && (t_types[wanted].bitfield.class == Reg
2586
0
         || t->opcode_modifier.isstring)))));
2587
8.72k
}
2588
2589
/* Return 1 if there is no conflict in 80bit size
2590
   between operand GIVEN and operand WANTED for instruction template T.  */
2591
2592
static INLINE int
2593
match_fp_size (const i386_operand_type *t_types, unsigned int wanted,
2594
        unsigned int given)
2595
2
{
2596
2
  return !i.types[given].bitfield.tbyte
2597
2
   || t_types[wanted].bitfield.tbyte;
2598
2
}
2599
2600
/* Return 1 if there is no conflict in SIMD register between operand
2601
   GIVEN and operand WANTED for instruction template T.  */
2602
2603
static INLINE int
2604
match_simd_size (const i386_operand_type *t_types, unsigned int wanted,
2605
     unsigned int given)
2606
2.15k
{
2607
2.15k
  return !((i.types[given].bitfield.xmmword
2608
54
      && !t_types[wanted].bitfield.xmmword)
2609
2.15k
     || (i.types[given].bitfield.ymmword
2610
0
         && !t_types[wanted].bitfield.ymmword)
2611
2.15k
     || (i.types[given].bitfield.zmmword
2612
0
         && !t_types[wanted].bitfield.zmmword)
2613
2.15k
     || (i.types[given].bitfield.tmmword
2614
0
         && !t_types[wanted].bitfield.tmmword));
2615
2.15k
}
2616
2617
/* Return 1 if there is no conflict in any size between operand GIVEN
2618
   and operand WANTED for instruction template T.  */
2619
2620
static INLINE int
2621
match_mem_size (const insn_template *t, unsigned int wanted,
2622
    unsigned int given)
2623
2.23k
{
2624
2.23k
  const i386_operand_type *t_types = get_operand_types (t);
2625
2626
2.23k
  return (match_operand_size (t, wanted, given)
2627
2.23k
    && (!i.types[given].bitfield.tbyte
2628
0
        || t_types[wanted].bitfield.tbyte)
2629
2.23k
    && !((i.types[given].bitfield.unspecified
2630
2.23k
    && !i.broadcast.type
2631
2.19k
    && !i.broadcast.bytes
2632
2.19k
    && !t_types[wanted].bitfield.unspecified)
2633
2.16k
         || (i.types[given].bitfield.fword
2634
0
       && !t_types[wanted].bitfield.fword)
2635
         /* For scalar opcode templates to allow register and memory
2636
      operands at the same time, some special casing is needed
2637
      here.  Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2638
      down-conversion vpmov*.  */
2639
2.16k
         || ((t_types[wanted].bitfield.class == RegSIMD
2640
57
        && t_types[wanted].bitfield.byte
2641
57
           + t_types[wanted].bitfield.word
2642
57
           + t_types[wanted].bitfield.dword
2643
57
           + t_types[wanted].bitfield.qword
2644
57
           > !!t->opcode_modifier.broadcast)
2645
2.16k
       ? (i.types[given].bitfield.xmmword
2646
57
          || i.types[given].bitfield.ymmword
2647
57
          || i.types[given].bitfield.zmmword)
2648
2.16k
       : !match_simd_size(t_types, wanted, given))));
2649
2.23k
}
2650
2651
/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2652
   operands for instruction template T, and it has MATCH_REVERSE set if there
2653
   is no size conflict on any operands for the template with operands reversed
2654
   (and the template allows for reversing in the first place).  */
2655
2656
16.5k
#define MATCH_STRAIGHT 1
2657
4.12k
#define MATCH_REVERSE  2
2658
2659
static INLINE unsigned int
2660
operand_size_match (const insn_template *t)
2661
12.5k
{
2662
12.5k
  const i386_operand_type *t_types = get_operand_types (t);
2663
12.5k
  unsigned int j, match = MATCH_STRAIGHT;
2664
2665
  /* Don't check non-absolute jump instructions.  */
2666
12.5k
  if (t->opcode_modifier.jump
2667
79
      && t->opcode_modifier.jump != JUMP_ABSOLUTE)
2668
72
    return match;
2669
2670
18.8k
  for (j = 0; j < i.imm_operands; j++)
2671
    /* Instruction templates with only sign-extended 8-bit immediate
2672
       operand also have a second template with full-operand-size
2673
       immediate operand under a different opcode.  Don't match the
2674
       first template if sign-extended 8-bit immediate operand should
2675
       be excluded.  */
2676
6.33k
    if (pp.no_imm8s
2677
0
        && !t_types[j].bitfield.imm8
2678
0
        && t_types[j].bitfield.imm8s)
2679
0
      {
2680
0
  gas_assert (!t->opcode_modifier.d);
2681
0
  return 0;
2682
0
      }
2683
2684
  /* Check memory and accumulator operand size.  */
2685
20.9k
  for (; j < i.operands; j++)
2686
10.0k
    {
2687
10.0k
      if (i.types[j].bitfield.class == Reg
2688
7.13k
    && (t_types[j].bitfield.class == Reg
2689
3.20k
        || (t_types[j].bitfield.instance == Accum
2690
447
      && (t_types[j].bitfield.byte
2691
0
          || t_types[j].bitfield.word
2692
0
          || t_types[j].bitfield.dword
2693
0
          || t_types[j].bitfield.qword)))
2694
4.38k
    && !match_operand_size (t, j, j))
2695
1.49k
  {
2696
1.49k
    match = 0;
2697
1.49k
    break;
2698
1.49k
  }
2699
2700
8.51k
      if (i.types[j].bitfield.class == RegFP
2701
3
    && (t_types[j].bitfield.class == RegFP
2702
1
        || (t_types[j].bitfield.instance == Accum
2703
0
      && t_types[j].bitfield.tbyte))
2704
2
    && !match_fp_size (t_types, j, j))
2705
0
  {
2706
0
    match = 0;
2707
0
    break;
2708
0
  }
2709
2710
8.51k
      if (i.types[j].bitfield.class == RegSIMD
2711
116
    && (t_types[j].bitfield.class == RegSIMD
2712
62
        || (t_types[j].bitfield.instance == Accum
2713
      /* Note: %ymm0, %zmm0, and %tmm0 aren't marked Accum.  */
2714
0
      && t_types[j].bitfield.xmmword))
2715
54
    && !match_simd_size (t_types, j, j))
2716
0
  {
2717
0
    match = 0;
2718
0
    break;
2719
0
  }
2720
2721
8.51k
      if ((i.flags[j] & Operand_Mem)
2722
2.75k
    && operand_type_check (t_types[j], anymem)
2723
1.83k
    && t->opcode_modifier.operandconstraint != ANY_SIZE
2724
1.83k
    && !match_mem_size (t, j, j))
2725
0
  {
2726
0
    match = 0;
2727
0
    break;
2728
0
  }
2729
8.51k
    }
2730
2731
12.4k
  if (!t->opcode_modifier.d)
2732
9.83k
    return match;
2733
2734
  /* Check reverse.  */
2735
2.64k
  gas_assert (i.operands >= 2);
2736
2737
7.06k
  for (j = i.imm_operands; j < i.operands; j++)
2738
4.49k
    {
2739
4.49k
      unsigned int given = i.operands - j - 1;
2740
2741
      /* For FMA4 and XOP insns VEX.W controls just the first two register
2742
   operands.  And APX_F / APX_NDD insns just swap the two source operands,
2743
   with the 3rd one being the destination.  */
2744
4.49k
      if (is_cpu (t, CpuFMA4) || is_cpu (t, CpuXOP)
2745
4.49k
    || is_cpu (t, CpuAPX_F)|| is_cpu (t, CpuAPX_NDD))
2746
33
  given = j < 2 ? 1 - j : j;
2747
2748
4.49k
      if (i.types[given].bitfield.class == Reg
2749
2.71k
    && (t_types[j].bitfield.class == Reg
2750
1.16k
        || (t_types[j].bitfield.instance == Accum
2751
556
      && (t_types[j].bitfield.byte
2752
0
          || t_types[j].bitfield.word
2753
0
          || t_types[j].bitfield.dword
2754
0
          || t_types[j].bitfield.qword
2755
0
          || t_types[j].bitfield.tbyte)))
2756
2.10k
    && !match_operand_size (t, j, given))
2757
0
  return match;
2758
2759
4.49k
      if (i.types[given].bitfield.class == RegFP
2760
0
    && (t_types[j].bitfield.class == RegFP
2761
0
        || (t_types[j].bitfield.instance == Accum
2762
0
      && t_types[j].bitfield.tbyte))
2763
0
    && !match_fp_size (t_types, j, given))
2764
0
  return match;
2765
2766
      /* No need to check for Accum here: There are no such templates with D
2767
   set.  */
2768
4.49k
      if (i.types[given].bitfield.class == RegSIMD
2769
2
    && t_types[j].bitfield.class == RegSIMD
2770
0
    && !match_simd_size (t_types, j, given))
2771
0
  return match;
2772
2773
4.49k
      if ((i.flags[given] & Operand_Mem)
2774
1.06k
    && operand_type_check (t_types[j], anymem)
2775
403
    && !match_mem_size (t, j, given))
2776
74
  return match;
2777
4.49k
    }
2778
2779
2.57k
  return match | MATCH_REVERSE;
2780
2.64k
}
2781
2782
static INLINE int
2783
operand_type_match (i386_operand_type overlap,
2784
        i386_operand_type given)
2785
14.6k
{
2786
14.6k
  i386_operand_type temp = overlap;
2787
2788
14.6k
  temp.bitfield.unspecified = 0;
2789
14.6k
  temp.bitfield.byte = 0;
2790
14.6k
  temp.bitfield.word = 0;
2791
14.6k
  temp.bitfield.dword = 0;
2792
14.6k
  temp.bitfield.fword = 0;
2793
14.6k
  temp.bitfield.qword = 0;
2794
14.6k
  temp.bitfield.tbyte = 0;
2795
14.6k
  temp.bitfield.xmmword = 0;
2796
14.6k
  temp.bitfield.ymmword = 0;
2797
14.6k
  temp.bitfield.zmmword = 0;
2798
14.6k
  temp.bitfield.tmmword = 0;
2799
14.6k
  if (operand_type_all_zero (&temp))
2800
9.02k
    goto mismatch;
2801
2802
  /* When a (register) instance is expected, operand size needs checking
2803
     to disambiguate.  */
2804
5.67k
  if (overlap.bitfield.instance != InstanceNone
2805
27
      && !overlap.bitfield.byte
2806
27
      && !overlap.bitfield.word
2807
27
      && !overlap.bitfield.dword
2808
16
      && !overlap.bitfield.qword
2809
0
      && !overlap.bitfield.tbyte
2810
0
      && !overlap.bitfield.xmmword
2811
0
      && !overlap.bitfield.ymmword
2812
0
      && !overlap.bitfield.zmmword
2813
0
      && !overlap.bitfield.tmmword)
2814
0
    {
2815
0
      gas_assert (overlap.bitfield.class == ClassNone);
2816
0
      goto mismatch;
2817
0
    }
2818
2819
5.67k
  if (given.bitfield.baseindex == overlap.bitfield.baseindex)
2820
5.66k
    return 1;
2821
2822
9.02k
 mismatch:
2823
9.02k
  i.error = operand_type_mismatch;
2824
9.02k
  return 0;
2825
5.67k
}
2826
2827
/* If given types g0 and g1 are registers they must be of the same type
2828
   unless the expected operand type register overlap is null.
2829
   Intel syntax sized memory operands are also checked here.  */
2830
2831
static INLINE int
2832
operand_type_register_match (i386_operand_type g0,
2833
           i386_operand_type t0,
2834
           i386_operand_type g1,
2835
           i386_operand_type t1)
2836
1.51k
{
2837
1.51k
  if (g0.bitfield.class != Reg
2838
282
      && g0.bitfield.class != RegSIMD
2839
282
      && (g0.bitfield.unspecified
2840
0
    || !operand_type_check (g0, anymem)))
2841
282
    return 1;
2842
2843
1.23k
  if (g1.bitfield.class != Reg
2844
648
      && g1.bitfield.class != RegSIMD
2845
648
      && (g1.bitfield.unspecified
2846
0
    || !operand_type_check (g1, anymem)))
2847
648
    return 1;
2848
2849
583
  if (g0.bitfield.byte == g1.bitfield.byte
2850
583
      && g0.bitfield.word == g1.bitfield.word
2851
583
      && g0.bitfield.dword == g1.bitfield.dword
2852
583
      && g0.bitfield.qword == g1.bitfield.qword
2853
583
      && g0.bitfield.xmmword == g1.bitfield.xmmword
2854
583
      && g0.bitfield.ymmword == g1.bitfield.ymmword
2855
583
      && g0.bitfield.zmmword == g1.bitfield.zmmword)
2856
583
    return 1;
2857
2858
  /* If expectations overlap in no more than a single size, all is fine. */
2859
0
  g0 = operand_type_and (t0, t1);
2860
0
  if (g0.bitfield.byte
2861
0
      + g0.bitfield.word
2862
0
      + g0.bitfield.dword
2863
0
      + g0.bitfield.qword
2864
0
      + g0.bitfield.xmmword
2865
0
      + g0.bitfield.ymmword
2866
0
      + g0.bitfield.zmmword <= 1)
2867
0
    return 1;
2868
2869
0
  i.error = register_type_mismatch;
2870
2871
0
  return 0;
2872
0
}
2873
2874
static INLINE unsigned int
2875
register_number (const reg_entry *r)
2876
0
{
2877
0
  unsigned int nr = r->reg_num;
2878
2879
0
  if (r->reg_flags & RegRex)
2880
0
    nr += 8;
2881
2882
0
  if (r->reg_flags & (RegVRex | RegRex2))
2883
0
    nr += 16;
2884
2885
0
  return nr;
2886
0
}
2887
2888
static INLINE unsigned int
2889
mode_from_disp_size (i386_operand_type t)
2890
147
{
2891
147
  if (t.bitfield.disp8)
2892
7
    return 1;
2893
140
  else if (t.bitfield.disp16
2894
140
     || t.bitfield.disp32)
2895
0
    return 2;
2896
140
  else
2897
140
    return 0;
2898
147
}
2899
2900
static INLINE int
2901
fits_in_signed_byte (addressT num)
2902
1.26k
{
2903
1.26k
  return num + 0x80 <= 0xff;
2904
1.26k
}
2905
2906
static INLINE int
2907
fits_in_unsigned_byte (addressT num)
2908
1.16k
{
2909
1.16k
  return num <= 0xff;
2910
1.16k
}
2911
2912
static INLINE int
2913
fits_in_unsigned_word (addressT num)
2914
340
{
2915
340
  return num <= 0xffff;
2916
340
}
2917
2918
static INLINE int
2919
fits_in_signed_word (addressT num)
2920
611
{
2921
611
  return num + 0x8000 <= 0xffff;
2922
611
}
2923
2924
static INLINE int
2925
fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
2926
79
{
2927
#ifndef BFD64
2928
  return 1;
2929
#else
2930
79
  return num + 0x80000000 <= 0xffffffff;
2931
79
#endif
2932
79
}        /* fits_in_signed_long() */
2933
2934
static INLINE int
2935
fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
2936
30.8k
{
2937
#ifndef BFD64
2938
  return 1;
2939
#else
2940
30.8k
  return num <= 0xffffffff;
2941
30.8k
#endif
2942
30.8k
}        /* fits_in_unsigned_long() */
2943
2944
static INLINE valueT extend_to_32bit_address (addressT num)
2945
0
{
2946
0
#ifdef BFD64
2947
0
  if (fits_in_unsigned_long(num))
2948
0
    return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2949
2950
0
  if (!fits_in_signed_long (num))
2951
0
    return num & 0xffffffff;
2952
0
#endif
2953
2954
0
  return num;
2955
0
}
2956
2957
static INLINE int
2958
fits_in_disp8 (offsetT num)
2959
108
{
2960
108
  int shift = i.memshift;
2961
108
  unsigned int mask;
2962
2963
108
  if (shift == -1)
2964
0
    abort ();
2965
2966
108
  mask = (1 << shift) - 1;
2967
2968
  /* Return 0 if NUM isn't properly aligned.  */
2969
108
  if ((num & mask))
2970
0
    return 0;
2971
2972
  /* Check if NUM will fit in 8bit after shift.  */
2973
108
  return fits_in_signed_byte (num >> shift);
2974
108
}
2975
2976
static INLINE int
2977
fits_in_imm4 (offsetT num)
2978
0
{
2979
  /* Despite the name, check for imm3 if we're dealing with EVEX.  */
2980
0
  return (num & (pp.encoding != encoding_evex
2981
0
     && pp.encoding != encoding_egpr ? 0xf : 7)) == num;
2982
0
}
2983
2984
static i386_operand_type
2985
smallest_imm_type (offsetT num)
2986
1.16k
{
2987
1.16k
  i386_operand_type t;
2988
2989
1.16k
  operand_type_set (&t, 0);
2990
1.16k
  t.bitfield.imm64 = 1;
2991
2992
1.16k
  if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
2993
4
    {
2994
      /* This code is disabled on the 486 because all the Imm1 forms
2995
   in the opcode table are slower on the i486.  They're the
2996
   versions with the implicitly specified single-position
2997
   displacement, which has another syntax if you really want to
2998
   use that form.  */
2999
4
      t.bitfield.imm1 = 1;
3000
4
      t.bitfield.imm8 = 1;
3001
4
      t.bitfield.imm8s = 1;
3002
4
      t.bitfield.imm16 = 1;
3003
4
      t.bitfield.imm32 = 1;
3004
4
      t.bitfield.imm32s = 1;
3005
4
    }
3006
1.16k
  else if (fits_in_signed_byte (num))
3007
549
    {
3008
549
      if (fits_in_unsigned_byte (num))
3009
407
  t.bitfield.imm8 = 1;
3010
549
      t.bitfield.imm8s = 1;
3011
549
      t.bitfield.imm16 = 1;
3012
549
      if (flag_code != CODE_64BIT || fits_in_unsigned_long (num))
3013
532
  t.bitfield.imm32 = 1;
3014
549
      t.bitfield.imm32s = 1;
3015
549
    }
3016
611
  else if (fits_in_unsigned_byte (num))
3017
0
    {
3018
0
      t.bitfield.imm8 = 1;
3019
0
      t.bitfield.imm16 = 1;
3020
0
      t.bitfield.imm32 = 1;
3021
0
      t.bitfield.imm32s = 1;
3022
0
    }
3023
611
  else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
3024
601
    {
3025
601
      t.bitfield.imm16 = 1;
3026
601
      if (flag_code != CODE_64BIT || fits_in_unsigned_long (num))
3027
140
  t.bitfield.imm32 = 1;
3028
601
      t.bitfield.imm32s = 1;
3029
601
    }
3030
10
  else if (fits_in_signed_long (num))
3031
5
    {
3032
5
      if (flag_code != CODE_64BIT || fits_in_unsigned_long (num))
3033
1
  t.bitfield.imm32 = 1;
3034
5
      t.bitfield.imm32s = 1;
3035
5
    }
3036
5
  else if (fits_in_unsigned_long (num))
3037
0
    t.bitfield.imm32 = 1;
3038
3039
1.16k
  return t;
3040
1.16k
}
3041
3042
static offsetT
3043
offset_in_range (offsetT val, int size)
3044
526
{
3045
526
  addressT mask;
3046
3047
526
  switch (size)
3048
526
    {
3049
26
    case 1: mask = ((addressT) 1 <<  8) - 1; break;
3050
462
    case 2: mask = ((addressT) 1 << 16) - 1; break;
3051
0
#ifdef BFD64
3052
38
    case 4: mask = ((addressT) 1 << 32) - 1; break;
3053
0
#endif
3054
0
    case sizeof (val): return val;
3055
0
    default: abort ();
3056
526
    }
3057
3058
526
  if ((val & ~mask) != 0 && (-(addressT) val & ~mask) != 0)
3059
0
    as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
3060
0
       (uint64_t) val, (uint64_t) (val & mask));
3061
3062
526
  return val & mask;
3063
526
}
3064
3065
static INLINE const char *insn_name (const insn_template *t)
3066
747k
{
3067
747k
  return &i386_mnemonics[t->mnem_off];
3068
747k
}
3069
3070
enum PREFIX_GROUP
3071
{
3072
  PREFIX_EXIST = 0,
3073
  PREFIX_LOCK,
3074
  PREFIX_REP,
3075
  PREFIX_DS,
3076
  PREFIX_OTHER
3077
};
3078
3079
/* Returns
3080
   a. PREFIX_EXIST if attempting to add a prefix where one from the
3081
   same class already exists.
3082
   b. PREFIX_LOCK if lock prefix is added.
3083
   c. PREFIX_REP if rep/repne prefix is added.
3084
   d. PREFIX_DS if ds prefix is added.
3085
   e. PREFIX_OTHER if other prefix is added.
3086
 */
3087
3088
static enum PREFIX_GROUP
3089
add_prefix (unsigned int prefix)
3090
894
{
3091
894
  enum PREFIX_GROUP ret = PREFIX_OTHER;
3092
894
  unsigned int q;
3093
3094
894
  if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
3095
83
      && flag_code == CODE_64BIT)
3096
83
    {
3097
83
      if ((i.prefix[REX_PREFIX] & prefix & REX_W)
3098
79
    || (i.prefix[REX_PREFIX] & prefix & REX_R)
3099
79
    || (i.prefix[REX_PREFIX] & prefix & REX_X)
3100
79
    || (i.prefix[REX_PREFIX] & prefix & REX_B))
3101
4
  ret = PREFIX_EXIST;
3102
83
      q = REX_PREFIX;
3103
83
    }
3104
811
  else
3105
811
    {
3106
811
      switch (prefix)
3107
811
  {
3108
0
  default:
3109
0
    abort ();
3110
3111
3
  case DS_PREFIX_OPCODE:
3112
3
    ret = PREFIX_DS;
3113
    /* Fall through.  */
3114
54
  case CS_PREFIX_OPCODE:
3115
54
  case ES_PREFIX_OPCODE:
3116
54
  case FS_PREFIX_OPCODE:
3117
54
  case GS_PREFIX_OPCODE:
3118
54
  case SS_PREFIX_OPCODE:
3119
54
    q = SEG_PREFIX;
3120
54
    break;
3121
3122
0
  case REPNE_PREFIX_OPCODE:
3123
19
  case REPE_PREFIX_OPCODE:
3124
19
    q = REP_PREFIX;
3125
19
    ret = PREFIX_REP;
3126
19
    break;
3127
3128
11
  case LOCK_PREFIX_OPCODE:
3129
11
    q = LOCK_PREFIX;
3130
11
    ret = PREFIX_LOCK;
3131
11
    break;
3132
3133
37
  case FWAIT_OPCODE:
3134
37
    q = WAIT_PREFIX;
3135
37
    break;
3136
3137
0
  case ADDR_PREFIX_OPCODE:
3138
0
    q = ADDR_PREFIX;
3139
0
    break;
3140
3141
690
  case DATA_PREFIX_OPCODE:
3142
690
    q = DATA_PREFIX;
3143
690
    break;
3144
811
  }
3145
811
      if (i.prefix[q] != 0)
3146
1
  ret = PREFIX_EXIST;
3147
811
    }
3148
3149
894
  if (ret)
3150
889
    {
3151
889
      if (!i.prefix[q])
3152
887
  ++i.prefixes;
3153
889
      i.prefix[q] |= prefix;
3154
889
    }
3155
5
  else
3156
5
    as_bad (_("same type of prefix used twice"));
3157
3158
894
  return ret;
3159
894
}
3160
3161
static void
3162
update_code_flag (int value, int check)
3163
444
{
3164
444
  PRINTF_LIKE ((*as_error)) = check ? as_fatal : as_bad;
3165
3166
444
  if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpu64 )
3167
0
    {
3168
0
      as_error (_("64bit mode not supported on `%s'."),
3169
0
    cpu_arch_name ? cpu_arch_name : default_arch);
3170
0
      return;
3171
0
    }
3172
3173
444
  if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3174
0
    {
3175
0
      as_error (_("32bit mode not supported on `%s'."),
3176
0
    cpu_arch_name ? cpu_arch_name : default_arch);
3177
0
      return;
3178
0
    }
3179
3180
444
  flag_code = (enum flag_code) value;
3181
3182
444
  stackop_size = '\0';
3183
444
}
3184
3185
static void
3186
set_code_flag (int value)
3187
163
{
3188
163
  update_code_flag (value, 0);
3189
163
}
3190
3191
static void
3192
set_16bit_gcc_code_flag (int new_code_flag)
3193
1
{
3194
1
  flag_code = (enum flag_code) new_code_flag;
3195
1
  if (flag_code != CODE_16BIT)
3196
0
    abort ();
3197
1
  stackop_size = LONG_MNEM_SUFFIX;
3198
1
}
3199
3200
static void
3201
_set_intel_syntax (int syntax_flag)
3202
41
{
3203
41
  intel_syntax = syntax_flag;
3204
3205
41
  expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
3206
3207
41
  register_prefix = allow_naked_reg ? "" : "%";
3208
41
}
3209
3210
static void
3211
set_intel_syntax (int syntax_flag)
3212
41
{
3213
  /* Find out if register prefixing is specified.  */
3214
41
  int ask_naked_reg = 0;
3215
3216
41
  SKIP_WHITESPACE ();
3217
41
  if (!is_end_of_stmt (*input_line_pointer))
3218
18
    {
3219
18
      char *string;
3220
18
      int e = get_symbol_name (&string);
3221
3222
18
      if (strcmp (string, "prefix") == 0)
3223
0
  ask_naked_reg = 1;
3224
18
      else if (strcmp (string, "noprefix") == 0)
3225
5
  ask_naked_reg = -1;
3226
13
      else
3227
13
  as_bad (_("bad argument to syntax directive."));
3228
18
      (void) restore_line_pointer (e);
3229
18
    }
3230
41
  demand_empty_rest_of_line ();
3231
3232
41
  if (ask_naked_reg == 0)
3233
36
    allow_naked_reg = (syntax_flag
3234
33
           && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
3235
5
  else
3236
5
    allow_naked_reg = (ask_naked_reg < 0);
3237
3238
41
  _set_intel_syntax (syntax_flag);
3239
41
}
3240
3241
static void
3242
set_intel_mnemonic (int mnemonic_flag)
3243
0
{
3244
0
  intel_mnemonic = mnemonic_flag;
3245
0
}
3246
3247
static void
3248
set_allow_index_reg (int flag)
3249
17
{
3250
17
  allow_index_reg = flag;
3251
17
}
3252
3253
static void
3254
set_check (int what)
3255
0
{
3256
0
  enum check_kind *kind;
3257
0
  const char *str;
3258
3259
0
  if (what)
3260
0
    {
3261
0
      kind = &operand_check;
3262
0
      str = "operand";
3263
0
    }
3264
0
  else
3265
0
    {
3266
0
      kind = &sse_check;
3267
0
      str = "sse";
3268
0
    }
3269
3270
0
  SKIP_WHITESPACE ();
3271
3272
0
  if (!is_end_of_stmt (*input_line_pointer))
3273
0
    {
3274
0
      char *string;
3275
0
      int e = get_symbol_name (&string);
3276
3277
0
      if (strcmp (string, "none") == 0)
3278
0
  *kind = check_none;
3279
0
      else if (strcmp (string, "warning") == 0)
3280
0
  *kind = check_warning;
3281
0
      else if (strcmp (string, "error") == 0)
3282
0
  *kind = check_error;
3283
0
      else
3284
0
  as_bad (_("bad argument to %s_check directive."), str);
3285
0
      (void) restore_line_pointer (e);
3286
0
    }
3287
0
  else
3288
0
    as_bad (_("missing argument for %s_check directive"), str);
3289
3290
0
  demand_empty_rest_of_line ();
3291
0
}
3292
3293
static void
3294
check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
3295
         i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
3296
3
{
3297
  /* Intel MCU is only supported on ELF.  */
3298
3
#ifdef OBJ_ELF
3299
3
  static const char *arch;
3300
3301
3
  if (!arch)
3302
1
    {
3303
      /* Use cpu_arch_name if it is set in md_parse_option.  Otherwise
3304
   use default_arch.  */
3305
1
      arch = cpu_arch_name;
3306
1
      if (!arch)
3307
1
  arch = default_arch;
3308
1
    }
3309
3310
  /* If we are targeting Intel MCU, we must enable it.  */
3311
3
  if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
3312
3
      == new_flag.bitfield.cpuiamcu)
3313
3
    return;
3314
3315
0
  as_bad (_("`%s' is not supported on `%s'"), name, arch);
3316
0
#endif
3317
0
}
3318
3319
static void
3320
extend_cpu_sub_arch_name (const char *pfx, const char *name)
3321
5
{
3322
5
  if (cpu_sub_arch_name)
3323
2
    cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
3324
2
          pfx, name, (const char *) NULL);
3325
3
  else
3326
3
    cpu_sub_arch_name = concat (pfx, name, (const char *) NULL);
3327
5
}
3328
3329
static void isa_enable (unsigned int idx)
3330
9
{
3331
9
  i386_cpu_flags flags = cpu_flags_or (cpu_arch_flags, cpu_arch[idx].enable);
3332
3333
9
  if (!cpu_flags_equal (&flags, &cpu_arch_flags))
3334
2
    {
3335
2
      extend_cpu_sub_arch_name (".", cpu_arch[idx].name);
3336
2
      cpu_arch_flags = flags;
3337
2
    }
3338
3339
9
  cpu_arch_isa_flags = cpu_flags_or (cpu_arch_isa_flags, cpu_arch[idx].enable);
3340
9
}
3341
3342
static void isa_disable (unsigned int idx)
3343
3
{
3344
3
  i386_cpu_flags flags
3345
3
    = cpu_flags_and_not (cpu_arch_flags, cpu_arch[idx].disable);
3346
3347
3
  if (!cpu_flags_equal (&flags, &cpu_arch_flags))
3348
3
    {
3349
3
      extend_cpu_sub_arch_name (".no", cpu_arch[idx].name);
3350
3
      cpu_arch_flags = flags;
3351
3
    }
3352
3353
3
  cpu_arch_isa_flags
3354
3
    = cpu_flags_and_not (cpu_arch_isa_flags, cpu_arch[idx].disable);
3355
3
}
3356
3357
static void
3358
set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
3359
109
{
3360
109
  typedef struct arch_stack_entry
3361
109
  {
3362
109
    const struct arch_stack_entry *prev;
3363
109
    const char *name;
3364
109
    char *sub_name;
3365
109
    i386_cpu_flags flags;
3366
109
    i386_cpu_flags isa_flags;
3367
109
    enum processor_type isa;
3368
109
    enum flag_code flag_code;
3369
109
    unsigned int vector_size;
3370
109
    char stackop_size;
3371
109
    bool no_cond_jump_promotion;
3372
109
  } arch_stack_entry;
3373
109
  static const arch_stack_entry *arch_stack_top;
3374
109
  char *s;
3375
109
  int e;
3376
109
  const char *string;
3377
109
  unsigned int j = 0;
3378
3379
109
  SKIP_WHITESPACE ();
3380
3381
109
  if (is_end_of_stmt (*input_line_pointer))
3382
0
    {
3383
0
      as_bad (_("missing cpu architecture"));
3384
0
      input_line_pointer++;
3385
0
      return;
3386
0
    }
3387
3388
109
  e = get_symbol_name (&s);
3389
109
  string = s;
3390
3391
109
  if (strcmp (string, "push") == 0)
3392
2
    {
3393
2
      arch_stack_entry *top = XNEW (arch_stack_entry);
3394
3395
2
      top->name = cpu_arch_name;
3396
2
      if (cpu_sub_arch_name)
3397
2
  top->sub_name = xstrdup (cpu_sub_arch_name);
3398
0
      else
3399
0
  top->sub_name = NULL;
3400
2
      top->flags = cpu_arch_flags;
3401
2
      top->isa = cpu_arch_isa;
3402
2
      top->isa_flags = cpu_arch_isa_flags;
3403
2
      top->flag_code = flag_code;
3404
2
      top->vector_size = vector_size;
3405
2
      top->stackop_size = stackop_size;
3406
2
      top->no_cond_jump_promotion = no_cond_jump_promotion;
3407
3408
2
      top->prev = arch_stack_top;
3409
2
      arch_stack_top = top;
3410
3411
2
      (void) restore_line_pointer (e);
3412
2
      demand_empty_rest_of_line ();
3413
2
      return;
3414
2
    }
3415
3416
107
  if (strcmp (string, "pop") == 0)
3417
1
    {
3418
1
      const arch_stack_entry *top = arch_stack_top;
3419
3420
1
      if (!top)
3421
0
  {
3422
0
    as_bad (_(".arch stack is empty"));
3423
93
  restore_bad:
3424
93
    (void) restore_line_pointer (e);
3425
93
    ignore_rest_of_line ();
3426
93
    return;
3427
0
  }
3428
3429
1
      if (top->flag_code != flag_code
3430
0
    || top->stackop_size != stackop_size)
3431
1
  {
3432
1
    static const unsigned int bits[] = {
3433
1
      [CODE_16BIT] = 16,
3434
1
      [CODE_32BIT] = 32,
3435
1
      [CODE_64BIT] = 64,
3436
1
    };
3437
3438
1
    as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
3439
1
      bits[top->flag_code],
3440
1
      top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
3441
1
    goto restore_bad;
3442
1
  }
3443
3444
0
      arch_stack_top = top->prev;
3445
3446
0
      cpu_arch_name = top->name;
3447
0
      free (cpu_sub_arch_name);
3448
0
      cpu_sub_arch_name = top->sub_name;
3449
0
      cpu_arch_flags = top->flags;
3450
0
      cpu_arch_isa = top->isa;
3451
0
      cpu_arch_isa_flags = top->isa_flags;
3452
0
      vector_size = top->vector_size;
3453
0
      no_cond_jump_promotion = top->no_cond_jump_promotion;
3454
3455
0
      XDELETE (top);
3456
3457
0
      (void) restore_line_pointer (e);
3458
0
      demand_empty_rest_of_line ();
3459
0
      return;
3460
1
    }
3461
3462
106
  if (strcmp (string, "default") == 0)
3463
0
    {
3464
0
      if (strcmp (default_arch, "iamcu") == 0)
3465
0
  string = default_arch;
3466
0
      else
3467
0
  {
3468
0
    static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
3469
3470
0
    cpu_arch_name = NULL;
3471
0
    free (cpu_sub_arch_name);
3472
0
    cpu_sub_arch_name = NULL;
3473
0
    cpu_arch_flags = cpu_unknown_flags;
3474
0
    cpu_arch_isa = PROCESSOR_UNKNOWN;
3475
0
    cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
3476
0
    if (!cpu_arch_tune_set)
3477
0
      cpu_arch_tune = PROCESSOR_UNKNOWN;
3478
3479
0
    vector_size = VSZ_DEFAULT;
3480
3481
0
    j = ARRAY_SIZE (cpu_arch) + 1;
3482
0
  }
3483
0
    }
3484
3485
20.7k
  for (; j < ARRAY_SIZE (cpu_arch); j++)
3486
20.6k
    {
3487
20.6k
      if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
3488
12
    && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
3489
12
  {
3490
12
    if (*string != '.')
3491
3
      {
3492
3
        check_cpu_arch_compatible (string, cpu_arch[j].enable);
3493
3494
3
        if (flag_code == CODE_64BIT && !cpu_arch[j].enable.bitfield.cpu64 )
3495
0
    {
3496
0
      as_bad (_("64bit mode not supported on `%s'."),
3497
0
        cpu_arch[j].name);
3498
0
      goto restore_bad;
3499
0
    }
3500
3501
3
        if (flag_code == CODE_32BIT && !cpu_arch[j].enable.bitfield.cpui386)
3502
0
    {
3503
0
      as_bad (_("32bit mode not supported on `%s'."),
3504
0
        cpu_arch[j].name);
3505
0
      goto restore_bad;
3506
0
    }
3507
3508
3
        cpu_arch_name = cpu_arch[j].name;
3509
3
        free (cpu_sub_arch_name);
3510
3
        cpu_sub_arch_name = NULL;
3511
3
        cpu_arch_flags = cpu_arch[j].enable;
3512
3
        cpu_arch_isa = cpu_arch[j].type;
3513
3
        cpu_arch_isa_flags = cpu_arch[j].enable;
3514
3
        if (!cpu_arch_tune_set)
3515
3
    cpu_arch_tune = cpu_arch_isa;
3516
3517
3
        vector_size = VSZ_DEFAULT;
3518
3519
3
        pre_386_16bit_warned = false;
3520
3
        break;
3521
3
      }
3522
3523
9
    if (cpu_flags_all_zero (&cpu_arch[j].enable))
3524
0
      continue;
3525
3526
9
    isa_enable (j);
3527
3528
9
    (void) restore_line_pointer (e);
3529
3530
9
    switch (cpu_arch[j].vsz)
3531
9
      {
3532
7
      default:
3533
7
        break;
3534
3535
7
      case vsz_set:
3536
#ifdef SVR4_COMMENT_CHARS
3537
        if (*input_line_pointer == ':' || *input_line_pointer == '/')
3538
#else
3539
2
        if (*input_line_pointer == '/')
3540
2
#endif
3541
2
    {
3542
2
      ++input_line_pointer;
3543
2
      switch (get_absolute_expression ())
3544
2
        {
3545
0
        case 512: vector_size = VSZ512; break;
3546
2
        case 256: vector_size = VSZ256; break;
3547
0
        case 128: vector_size = VSZ128; break;
3548
0
        default:
3549
0
          as_bad (_("Unrecognized vector size specifier"));
3550
0
          ignore_rest_of_line ();
3551
0
          return;
3552
2
        }
3553
2
      break;
3554
2
    }
3555
    /* Fall through.  */
3556
0
      case vsz_reset:
3557
0
        vector_size = VSZ_DEFAULT;
3558
0
        break;
3559
9
      }
3560
3561
9
    demand_empty_rest_of_line ();
3562
9
    return;
3563
9
  }
3564
20.6k
    }
3565
3566
97
  if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
3567
3
    {
3568
      /* Disable an ISA extension.  */
3569
174
      for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
3570
174
  if (cpu_arch[j].type == PROCESSOR_NONE
3571
51
      && strcmp (string + 3, cpu_arch[j].name) == 0)
3572
3
    {
3573
3
      isa_disable (j);
3574
3575
3
      if (cpu_arch[j].vsz == vsz_set)
3576
0
        vector_size = VSZ_DEFAULT;
3577
3578
3
      (void) restore_line_pointer (e);
3579
3
      demand_empty_rest_of_line ();
3580
3
      return;
3581
3
    }
3582
3
    }
3583
3584
94
  if (j == ARRAY_SIZE (cpu_arch))
3585
91
    {
3586
91
      as_bad (_("no such architecture: `%s'"), string);
3587
91
      goto restore_bad;
3588
91
    }
3589
3590
3
  no_cond_jump_promotion = 0;
3591
3
  if (restore_line_pointer (e) == ','
3592
1
      && !is_end_of_stmt (input_line_pointer[1]))
3593
1
    {
3594
1
      ++input_line_pointer;
3595
1
      e = get_symbol_name (&s);
3596
1
      string = s;
3597
3598
1
      if (strcmp (string, "nojumps") == 0)
3599
0
  {
3600
0
    if (cpu_arch_flags.bitfield.cpui386)
3601
0
      as_bad (_("`%s' only supported with 16-bit architectures"), string);
3602
0
    else
3603
0
      no_cond_jump_promotion = true;
3604
0
  }
3605
1
      else if (strcmp (string, "jumps") != 0)
3606
1
  {
3607
1
    as_bad (_("no such architecture modifier: `%s'"), string);
3608
1
    goto restore_bad;
3609
1
  }
3610
3611
0
      (void) restore_line_pointer (e);
3612
0
    }
3613
3614
2
  demand_empty_rest_of_line ();
3615
2
}
3616
3617
enum bfd_architecture
3618
i386_arch (void)
3619
281
{
3620
281
  if (cpu_arch_isa == PROCESSOR_IAMCU)
3621
0
    {
3622
0
      if (!IS_ELF || flag_code == CODE_64BIT)
3623
0
  as_fatal (_("Intel MCU is 32bit ELF only"));
3624
0
      return bfd_arch_iamcu;
3625
0
    }
3626
281
  else
3627
281
    return bfd_arch_i386;
3628
281
}
3629
3630
unsigned long
3631
i386_mach (void)
3632
281
{
3633
281
  if (startswith (default_arch, "x86_64"))
3634
281
    {
3635
281
      if (default_arch[6] == '\0')
3636
281
  return bfd_mach_x86_64;
3637
0
      else
3638
0
  return bfd_mach_x64_32;
3639
281
    }
3640
0
  else if (!strcmp (default_arch, "i386")
3641
0
     || !strcmp (default_arch, "iamcu"))
3642
0
    {
3643
0
      if (cpu_arch_isa == PROCESSOR_IAMCU)
3644
0
  {
3645
0
    if (!IS_ELF)
3646
0
      as_fatal (_("Intel MCU is 32bit ELF only"));
3647
0
    return bfd_mach_i386_iamcu;
3648
0
  }
3649
0
      else
3650
0
  return bfd_mach_i386_i386;
3651
0
    }
3652
0
  else
3653
0
    as_fatal (_("unknown architecture"));
3654
281
}
3655

3656
static void
3657
op_lookup (const char *mnemonic)
3658
29.8k
{
3659
29.8k
   i386_op_off_t *pos = str_hash_find (op_hash, mnemonic);
3660
3661
29.8k
   if (pos != NULL)
3662
14.5k
     {
3663
14.5k
       current_templates.start = &i386_optab[pos[0]];
3664
14.5k
       current_templates.end = &i386_optab[pos[1]];
3665
14.5k
     }
3666
15.2k
   else
3667
15.2k
     current_templates.end = current_templates.start = NULL;
3668
29.8k
}
3669
3670
void
3671
md_begin (void)
3672
281
{
3673
  /* Make sure possible padding space is clear.  */
3674
281
  memset (&pp, 0, sizeof (pp));
3675
3676
  /* Initialize op_hash hash table.  */
3677
281
  op_hash = str_htab_create ();
3678
3679
281
  {
3680
281
    const i386_op_off_t *cur = i386_op_sets;
3681
281
    const i386_op_off_t *end = cur + ARRAY_SIZE (i386_op_sets) - 1;
3682
3683
746k
    for (; cur < end; ++cur)
3684
745k
      if (str_hash_insert (op_hash, insn_name (&i386_optab[*cur]), cur, 0))
3685
0
  as_fatal (_("duplicate %s"), insn_name (&i386_optab[*cur]));
3686
281
  }
3687
3688
  /* Initialize reg_hash hash table.  */
3689
281
  reg_hash = str_htab_create ();
3690
281
  {
3691
281
    const reg_entry *regtab;
3692
281
    unsigned int regtab_size = i386_regtab_size;
3693
3694
99.1k
    for (regtab = i386_regtab; regtab_size--; regtab++)
3695
98.9k
      {
3696
98.9k
  switch (regtab->reg_type.bitfield.class)
3697
98.9k
    {
3698
38.2k
    case Reg:
3699
38.2k
      if (regtab->reg_type.bitfield.dword)
3700
8.99k
        {
3701
8.99k
    if (regtab->reg_type.bitfield.instance == Accum)
3702
281
      reg_eax = regtab;
3703
8.99k
        }
3704
38.2k
      break;
3705
3706
2.24k
    case RegFP:
3707
      /* There's no point inserting st(<N>) in the hash table, as
3708
         parentheses aren't included in register_chars[] anyway.  */
3709
2.24k
      if (regtab->reg_type.bitfield.instance != Accum)
3710
1.96k
        continue;
3711
281
      reg_st0 = regtab;
3712
281
      break;
3713
3714
1.96k
    case SReg:
3715
1.96k
      switch (regtab->reg_num)
3716
1.96k
        {
3717
281
        case 0: reg_es = regtab; break;
3718
281
        case 2: reg_ss = regtab; break;
3719
281
        case 3: reg_ds = regtab; break;
3720
1.96k
        }
3721
1.96k
      break;
3722
3723
2.24k
    case RegMask:
3724
2.24k
      if (!regtab->reg_num)
3725
281
        reg_k0 = regtab;
3726
2.24k
      break;
3727
98.9k
    }
3728
3729
96.9k
  if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3730
0
    as_fatal (_("duplicate %s"), regtab->reg_name);
3731
96.9k
      }
3732
281
  }
3733
3734
  /* Fill in lexical tables:  mnemonic_chars, operand_chars.  */
3735
281
  {
3736
281
    int c;
3737
281
    const char *p;
3738
3739
72.2k
    for (c = 0; c < 256; c++)
3740
71.9k
      {
3741
71.9k
  if (ISDIGIT (c) || ISLOWER (c))
3742
10.1k
    {
3743
10.1k
      mnemonic_chars[c] = c;
3744
10.1k
      register_chars[c] = c;
3745
10.1k
      operand_chars[c] = c;
3746
10.1k
    }
3747
61.8k
  else if (ISUPPER (c))
3748
7.30k
    {
3749
7.30k
      mnemonic_chars[c] = TOLOWER (c);
3750
7.30k
      register_chars[c] = mnemonic_chars[c];
3751
7.30k
      operand_chars[c] = c;
3752
7.30k
    }
3753
#ifdef SVR4_COMMENT_CHARS
3754
  else if (c == '\\' && strchr (i386_comment_chars, '/'))
3755
    operand_chars[c] = c;
3756
#endif
3757
3758
71.9k
  if (c >= 128)
3759
35.9k
    operand_chars[c] = c;
3760
71.9k
      }
3761
3762
281
    mnemonic_chars['_'] = '_';
3763
281
    mnemonic_chars['-'] = '-';
3764
281
    mnemonic_chars['.'] = '.';
3765
3766
1.68k
    for (p = extra_symbol_chars; *p != '\0'; p++)
3767
1.40k
      operand_chars[(unsigned char) *p] = *p;
3768
5.90k
    for (p = operand_special_chars; *p != '\0'; p++)
3769
5.62k
      operand_chars[(unsigned char) *p] = *p;
3770
281
  }
3771
3772
281
  if (object_64bit)
3773
281
    {
3774
#if defined (OBJ_COFF) && defined (TE_PE)
3775
      x86_dwarf2_return_column = 32;
3776
#else
3777
281
      x86_dwarf2_return_column = REG_RA;
3778
281
#endif
3779
281
      x86_cie_data_alignment = -8;
3780
281
    }
3781
0
  else
3782
0
    {
3783
0
      x86_dwarf2_return_column = 8;
3784
0
      x86_cie_data_alignment = -4;
3785
0
    }
3786
3787
  /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3788
     can be turned into BRANCH_PREFIX frag.  */
3789
281
  if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3790
0
    abort ();
3791
281
}
3792
3793
void
3794
i386_print_statistics (FILE *file)
3795
0
{
3796
0
  htab_print_statistics (file, "i386 opcode", op_hash);
3797
0
  htab_print_statistics (file, "i386 register", reg_hash);
3798
0
}
3799
3800
void
3801
i386_md_end (void)
3802
281
{
3803
281
  if (!ENABLE_LEAK_CHECK)
3804
0
    return;
3805
281
  htab_delete (op_hash);
3806
281
  htab_delete (reg_hash);
3807
281
  GOT_symbol = NULL;
3808
281
}
3809

3810
#ifdef DEBUG386
3811
3812
/* Debugging routines for md_assemble.  */
3813
static void pte (insn_template *);
3814
static void pt (i386_operand_type);
3815
static void pe (expressionS *);
3816
static void ps (symbolS *);
3817
3818
static void
3819
pi (const char *line, i386_insn *x)
3820
{
3821
  unsigned int j;
3822
3823
  fprintf (stdout, "%s: template ", line);
3824
  pte (&x->tm);
3825
  fprintf (stdout, "  address: base %s  index %s  scale %x\n",
3826
     x->base_reg ? x->base_reg->reg_name : "none",
3827
     x->index_reg ? x->index_reg->reg_name : "none",
3828
     x->log2_scale_factor);
3829
  fprintf (stdout, "  modrm:  mode %x  reg %x  reg/mem %x\n",
3830
     x->rm.mode, x->rm.reg, x->rm.regmem);
3831
  fprintf (stdout, "  sib:  base %x  index %x  scale %x\n",
3832
     x->sib.base, x->sib.index, x->sib.scale);
3833
  fprintf (stdout, "  rex: 64bit %x  extX %x  extY %x  extZ %x\n",
3834
     (x->rex & REX_W) != 0,
3835
     (x->rex & REX_R) != 0,
3836
     (x->rex & REX_X) != 0,
3837
     (x->rex & REX_B) != 0);
3838
  for (j = 0; j < x->operands; j++)
3839
    {
3840
      fprintf (stdout, "    #%d:  ", j + 1);
3841
      pt (x->types[j]);
3842
      fprintf (stdout, "\n");
3843
      if (x->types[j].bitfield.class == Reg
3844
    || x->types[j].bitfield.class == RegFP
3845
    || x->types[j].bitfield.class == RegMMX
3846
    || x->types[j].bitfield.class == RegSIMD
3847
    || x->types[j].bitfield.class == RegMask
3848
    || x->types[j].bitfield.class == SReg
3849
    || x->types[j].bitfield.class == RegCR
3850
    || x->types[j].bitfield.class == RegDR
3851
    || x->types[j].bitfield.class == RegTR
3852
    || x->types[j].bitfield.class == RegBND)
3853
  fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3854
      if (operand_type_check (x->types[j], imm))
3855
  pe (x->op[j].imms);
3856
      if (operand_type_check (x->types[j], disp))
3857
  pe (x->op[j].disps);
3858
    }
3859
}
3860
3861
static void
3862
pte (insn_template *t)
3863
{
3864
  static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
3865
  static const char *const opc_spc[] = {
3866
    NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
3867
    "XOP08", "XOP09", "XOP0A",
3868
  };
3869
  unsigned int j;
3870
3871
  fprintf (stdout, " %d operands ", t->operands);
3872
  if (opc_pfx[t->opcode_modifier.opcodeprefix])
3873
    fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3874
  if (opc_spc[t->opcode_space])
3875
    fprintf (stdout, "space %s ", opc_spc[t->opcode_space]);
3876
  fprintf (stdout, "opcode %x ", t->base_opcode);
3877
  if (t->extension_opcode != None)
3878
    fprintf (stdout, "ext %x ", t->extension_opcode);
3879
  if (t->opcode_modifier.d)
3880
    fprintf (stdout, "D");
3881
  if (t->opcode_modifier.w)
3882
    fprintf (stdout, "W");
3883
  fprintf (stdout, "\n");
3884
  const i386_operand_type *t_types = get_operand_types (t);
3885
  for (j = 0; j < t->operands; j++)
3886
    {
3887
      fprintf (stdout, "    #%d type ", j + 1);
3888
      pt (t_types[j]);
3889
      fprintf (stdout, "\n");
3890
    }
3891
}
3892
3893
static void
3894
pe (expressionS *e)
3895
{
3896
  fprintf (stdout, "    operation     %d\n", e->X_op);
3897
  fprintf (stdout, "    add_number    %" PRId64 " (%" PRIx64 ")\n",
3898
     (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
3899
  if (e->X_add_symbol)
3900
    {
3901
      fprintf (stdout, "    add_symbol    ");
3902
      ps (e->X_add_symbol);
3903
      fprintf (stdout, "\n");
3904
    }
3905
  if (e->X_op_symbol)
3906
    {
3907
      fprintf (stdout, "    op_symbol    ");
3908
      ps (e->X_op_symbol);
3909
      fprintf (stdout, "\n");
3910
    }
3911
}
3912
3913
static void
3914
ps (symbolS *s)
3915
{
3916
  fprintf (stdout, "%s type %s%s",
3917
     S_GET_NAME (s),
3918
     S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3919
     segment_name (S_GET_SEGMENT (s)));
3920
}
3921
3922
static struct type_name
3923
  {
3924
    i386_operand_type mask;
3925
    const char *name;
3926
  }
3927
const type_names[] =
3928
{
3929
  { { .bitfield = { .class = Reg, .byte = 1 } }, "r8" },
3930
  { { .bitfield = { .class = Reg, .word = 1 } }, "r16" },
3931
  { { .bitfield = { .class = Reg, .dword = 1 } }, "r32" },
3932
  { { .bitfield = { .class = Reg, .qword = 1 } }, "r64" },
3933
  { { .bitfield = { .instance = Accum, .byte = 1 } }, "acc8" },
3934
  { { .bitfield = { .instance = Accum, .word = 1 } }, "acc16" },
3935
  { { .bitfield = { .instance = Accum, .dword = 1 } }, "acc32" },
3936
  { { .bitfield = { .instance = Accum, .qword = 1 } }, "acc64" },
3937
  { { .bitfield = { .imm8 = 1 } }, "i8" },
3938
  { { .bitfield = { .imm8s = 1 } }, "i8s" },
3939
  { { .bitfield = { .imm16 = 1 } }, "i16" },
3940
  { { .bitfield = { .imm32 = 1 } }, "i32" },
3941
  { { .bitfield = { .imm32s = 1 } }, "i32s" },
3942
  { { .bitfield = { .imm64 = 1 } }, "i64" },
3943
  { { .bitfield = { .imm1 = 1 } }, "i1" },
3944
  { { .bitfield = { .baseindex = 1 } }, "BaseIndex" },
3945
  { { .bitfield = { .disp8 = 1 } }, "d8" },
3946
  { { .bitfield = { .disp16 = 1 } }, "d16" },
3947
  { { .bitfield = { .disp32 = 1 } }, "d32" },
3948
  { { .bitfield = { .disp64 = 1 } }, "d64" },
3949
  { { .bitfield = { .instance = RegD, .word = 1 } }, "InOutPortReg" },
3950
  { { .bitfield = { .instance = RegC, .byte = 1 } }, "ShiftCount" },
3951
  { { .bitfield = { .class = RegCR } }, "control reg" },
3952
  { { .bitfield = { .class = RegTR } }, "test reg" },
3953
  { { .bitfield = { .class = RegDR } }, "debug reg" },
3954
  { { .bitfield = { .class = RegFP, .tbyte = 1 } }, "FReg" },
3955
  { { .bitfield = { .instance = Accum, .tbyte = 1 } }, "FAcc" },
3956
  { { .bitfield = { .class = SReg } }, "SReg" },
3957
  { { .bitfield = { .class = RegMMX } }, "rMMX" },
3958
  { { .bitfield = { .class = RegSIMD, .xmmword = 1 } }, "rXMM" },
3959
  { { .bitfield = { .class = RegSIMD, .ymmword = 1 } }, "rYMM" },
3960
  { { .bitfield = { .class = RegSIMD, .zmmword = 1 } }, "rZMM" },
3961
  { { .bitfield = { .class = RegSIMD, .tmmword = 1 } }, "rTMM" },
3962
  { { .bitfield = { .class = RegMask } }, "Mask reg" },
3963
  { { .bitfield = { .class = RegBND } }, "rBND" },
3964
};
3965
3966
static void
3967
pt (i386_operand_type t)
3968
{
3969
  unsigned int j;
3970
  i386_operand_type a;
3971
3972
  for (j = 0; j < ARRAY_SIZE (type_names); j++)
3973
    {
3974
      a = operand_type_and (t, type_names[j].mask);
3975
      if (operand_type_equal (&a, &type_names[j].mask))
3976
  fprintf (stdout, "%s, ",  type_names[j].name);
3977
    }
3978
  fflush (stdout);
3979
}
3980
3981
#endif /* DEBUG386 */
3982

3983
static bfd_reloc_code_real_type
3984
_reloc (unsigned int size,
3985
  bool pcrel,
3986
  int sign,
3987
  bfd_reloc_code_real_type other,
3988
  bool code64,
3989
  const char *file,
3990
  unsigned int line)
3991
4.83k
{
3992
4.83k
  if (other != NO_RELOC)
3993
45
    {
3994
45
      reloc_howto_type *rel;
3995
3996
45
      if (size == 8)
3997
2
  switch (other)
3998
2
    {
3999
0
    case BFD_RELOC_64_PLTOFF:
4000
0
    case BFD_RELOC_X86_64_GOTPLT64:
4001
0
      return other;
4002
0
    case BFD_RELOC_X86_64_GOT32:
4003
0
      return BFD_RELOC_X86_64_GOT64;
4004
0
    case BFD_RELOC_X86_64_GOTPC32:
4005
0
      other = BFD_RELOC_64_GOT_PCREL;
4006
0
      break;
4007
0
    case BFD_RELOC_X86_64_GOTPCREL:
4008
0
      other = BFD_RELOC_X86_64_GOTPCREL64;
4009
0
      break;
4010
0
    case BFD_RELOC_X86_64_TPOFF32:
4011
0
      other = BFD_RELOC_X86_64_TPOFF64;
4012
0
      break;
4013
0
    case BFD_RELOC_X86_64_DTPOFF32:
4014
0
      other = BFD_RELOC_X86_64_DTPOFF64;
4015
0
      break;
4016
2
    default:
4017
2
      break;
4018
2
    }
4019
4020
45
#ifdef OBJ_ELF
4021
45
      if (other == BFD_RELOC_SIZE32)
4022
2
  {
4023
2
    if (size == 8)
4024
2
      other = BFD_RELOC_SIZE64;
4025
2
    if (pcrel)
4026
0
      {
4027
0
        as_bad_where (file, line,
4028
0
          _("there are no pc-relative size relocations"));
4029
0
        return NO_RELOC;
4030
0
      }
4031
2
  }
4032
45
#endif
4033
4034
      /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless.  */
4035
45
      if (size == 4 && (!code64 || disallow_64bit_reloc))
4036
0
  sign = -1;
4037
4038
45
      rel = bfd_reloc_type_lookup (stdoutput, other);
4039
45
      if (!rel)
4040
0
  as_bad_where (file, line, _("unknown relocation (%u)"), other);
4041
45
      else if (size != bfd_get_reloc_size (rel))
4042
14
  as_bad_where (file, line,
4043
14
          _("%u-byte relocation cannot be applied to %u-byte field"),
4044
14
          bfd_get_reloc_size (rel), size);
4045
31
      else if (pcrel && !rel->pc_relative)
4046
0
  as_bad_where (file, line,
4047
0
          _("non-pc-relative relocation for pc-relative field"));
4048
31
      else if ((rel->complain_on_overflow == complain_overflow_signed
4049
29
    && !sign)
4050
31
         || (rel->complain_on_overflow == complain_overflow_unsigned
4051
0
       && sign > 0))
4052
0
  as_bad_where (file, line,
4053
0
          _("relocated field and relocation type differ in signedness"));
4054
31
      else
4055
31
  return other;
4056
14
      return NO_RELOC;
4057
45
    }
4058
4059
4.79k
  if (pcrel)
4060
20
    {
4061
20
      if (!sign)
4062
0
  as_bad_where (file, line,
4063
0
          _("there are no unsigned pc-relative relocations"));
4064
20
      switch (size)
4065
20
  {
4066
0
  case 1: return BFD_RELOC_8_PCREL;
4067
17
  case 2: return BFD_RELOC_16_PCREL;
4068
3
  case 4: return BFD_RELOC_32_PCREL;
4069
0
  case 8: return BFD_RELOC_64_PCREL;
4070
20
  }
4071
0
      as_bad_where (file, line,
4072
0
        _("cannot do %u byte pc-relative relocation"), size);
4073
0
    }
4074
4.77k
  else
4075
4.77k
    {
4076
4.77k
      if (sign > 0)
4077
1.54k
  switch (size)
4078
1.54k
    {
4079
1.54k
    case 4: return BFD_RELOC_X86_64_32S;
4080
1.54k
    }
4081
3.22k
      else
4082
3.22k
  switch (size)
4083
3.22k
    {
4084
35
    case 1: return BFD_RELOC_8;
4085
1.16k
    case 2: return BFD_RELOC_16;
4086
1.86k
    case 4: return BFD_RELOC_32;
4087
161
    case 8: return BFD_RELOC_64;
4088
3.22k
    }
4089
5
      as_bad_where (file, line, _("cannot do %s %u byte relocation"),
4090
5
        sign > 0 ? "signed" : "unsigned", size);
4091
5
    }
4092
4093
5
  return NO_RELOC;
4094
4.79k
}
4095
4096
static bfd_reloc_code_real_type
4097
reloc (unsigned int size,
4098
       bool pcrel,
4099
       int sign,
4100
       bfd_reloc_code_real_type other)
4101
4.83k
{
4102
4.83k
  return _reloc (size, pcrel, sign, other, flag_code == CODE_64BIT, NULL, 0);
4103
4.83k
}
4104
4105
#ifdef OBJ_ELF
4106
/* Here we decide which fixups can be adjusted to make them relative to
4107
   the beginning of the section instead of the symbol.  Basically we need
4108
   to make sure that the dynamic relocations are done correctly, so in
4109
   some cases we force the original symbol to be used.  */
4110
4111
int
4112
tc_i386_fix_adjustable (fixS *fixP)
4113
0
{
4114
  /* Don't adjust pc-relative references to merge sections in 64-bit
4115
     mode.  */
4116
0
  if (use_rela_relocations
4117
0
      && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
4118
0
      && fixP->fx_pcrel)
4119
0
    return 0;
4120
4121
  /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
4122
     and changed later by validate_fix.  */
4123
0
  if (GOT_symbol && fixP->fx_subsy == GOT_symbol
4124
0
      && fixP->fx_r_type == BFD_RELOC_32_PCREL)
4125
0
    return 0;
4126
4127
  /* Adjust_reloc_syms doesn't know about the GOT.  Need to keep symbol
4128
     for size relocations.  */
4129
0
  if (fixP->fx_r_type == BFD_RELOC_SIZE32
4130
0
      || fixP->fx_r_type == BFD_RELOC_SIZE64
4131
0
      || fixP->fx_r_type == BFD_RELOC_32_GOTOFF
4132
0
      || fixP->fx_r_type == BFD_RELOC_386_GOT32
4133
0
      || fixP->fx_r_type == BFD_RELOC_386_GOT32X
4134
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
4135
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
4136
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
4137
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
4138
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
4139
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
4140
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
4141
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
4142
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
4143
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
4144
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
4145
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
4146
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
4147
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
4148
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_4_GOTPCRELX
4149
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_5_GOTPCRELX
4150
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_6_GOTPCRELX
4151
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
4152
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
4153
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
4154
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
4155
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
4156
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_4_GOTTPOFF
4157
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_5_GOTTPOFF
4158
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_6_GOTTPOFF
4159
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
4160
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
4161
0
      || fixP->fx_r_type == BFD_RELOC_64_GOTOFF
4162
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOT64
4163
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
4164
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC
4165
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_5_GOTPC32_TLSDESC
4166
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_6_GOTPC32_TLSDESC
4167
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
4168
0
      || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
4169
0
      || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
4170
0
    return 0;
4171
  /* Resolve PLT32 relocation against local symbol to section only for
4172
     PC-relative relocations.  */
4173
0
  if (fixP->fx_r_type == BFD_RELOC_386_PLT32
4174
0
      || fixP->fx_r_type == BFD_RELOC_32_PLT_PCREL)
4175
0
    return fixP->fx_pcrel;
4176
0
  return 1;
4177
0
}
4178
#endif
4179
4180
static INLINE bool
4181
want_disp32 (const insn_template *t)
4182
2.02k
{
4183
2.02k
  return flag_code != CODE_64BIT
4184
1.43k
   || i.prefix[ADDR_PREFIX]
4185
1.42k
   || ((t->mnem_off == MN_lea
4186
1.41k
        || (i.tm.base_opcode == 0x8d && i.tm.opcode_space == SPACE_BASE))
4187
9
       && (!i.types[1].bitfield.qword
4188
0
     || t->opcode_modifier.size == SIZE32));
4189
2.02k
}
4190
4191
static INLINE bool is_padlock (const insn_template *t)
4192
3.41k
{
4193
  /* (Ab)use the PrefixRepe attribute of PadLock insns as long as no
4194
     others use it.  */
4195
3.41k
  return t->opcode_modifier.prefixok == PrefixRepe;
4196
3.41k
}
4197
4198
static int
4199
intel_float_operand (const char *mnemonic)
4200
20.2k
{
4201
  /* Note that the value returned is meaningful only for opcodes with (memory)
4202
     operands, hence the code here is free to improperly handle opcodes that
4203
     have no operands (for better performance and smaller code). */
4204
4205
20.2k
  if (mnemonic[0] != 'f')
4206
20.2k
    return 0; /* non-math */
4207
4208
39
  switch (mnemonic[1])
4209
39
    {
4210
    /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
4211
       the fs segment override prefix not currently handled because no
4212
       call path can make opcodes without operands get here */
4213
0
    case 'i':
4214
0
      return 2 /* integer op */;
4215
0
    case 'l':
4216
0
      if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
4217
0
  return 3; /* fldcw/fldenv */
4218
0
      break;
4219
0
    case 'n':
4220
0
      if (mnemonic[2] != 'o' /* fnop */)
4221
0
  return 3; /* non-waiting control op */
4222
0
      break;
4223
0
    case 'r':
4224
0
      if (mnemonic[2] == 's')
4225
0
  return 3; /* frstor/frstpm */
4226
0
      break;
4227
21
    case 's':
4228
21
      if (mnemonic[2] == 'a')
4229
0
  return 3; /* fsave */
4230
21
      if (mnemonic[2] == 't')
4231
20
  {
4232
20
    switch (mnemonic[3])
4233
20
      {
4234
0
      case 'c': /* fstcw */
4235
0
      case 'd': /* fstdw */
4236
0
      case 'e': /* fstenv */
4237
0
      case 's': /* fsts[gw] */
4238
0
        return 3;
4239
20
      }
4240
20
  }
4241
21
      break;
4242
21
    case 'x':
4243
0
      if (mnemonic[2] == 'r' || mnemonic[2] == 's')
4244
0
  return 0; /* fxsave/fxrstor are not really math ops */
4245
0
      break;
4246
39
    }
4247
4248
39
  return 1;
4249
39
}
4250
4251
static INLINE void
4252
install_template (const insn_template *t)
4253
3.44k
{
4254
3.44k
  unsigned int l;
4255
4256
3.44k
  i.tm = *t;
4257
4258
3.44k
  const i386_operand_type *t_types = get_operand_types (t);
4259
8.64k
  for (l = 0; l < t->operands; ++l)
4260
5.19k
    i.tm_types[l] = t_types[l];
4261
4262
  /* Dual VEX/EVEX templates need stripping one of the possible variants.  */
4263
3.44k
  if (t->opcode_modifier.vex && t->opcode_modifier.evex)
4264
0
    {
4265
0
      if ((maybe_cpu (t, CpuAVX) || maybe_cpu (t, CpuAVX2)
4266
0
     || maybe_cpu (t, CpuFMA) || maybe_cpu (t, CpuF16C))
4267
0
    && (maybe_cpu (t, CpuAVX512F) || maybe_cpu (t, CpuAVX512VL)))
4268
0
  {
4269
0
    bool evex = need_evex_encoding (t) || pp.encoding == encoding_egpr;
4270
4271
0
    if (!evex
4272
0
        && ((maybe_cpu (t, CpuFMA) && !cpu_arch_flags.bitfield.cpufma)
4273
0
      || (maybe_cpu (t, CpuF16C) && !cpu_arch_flags.bitfield.cpuf16c)))
4274
0
      {
4275
0
        if (!cpu_arch_isa_flags.bitfield.cpuavx512vl
4276
0
      && !i.types[i.operands - 1].bitfield.zmmword)
4277
0
    as_warn(_("%s: will use AVX512VL encoding; use {evex} to silence"),
4278
0
      insn_name (t));
4279
4280
0
        evex = true;
4281
0
      }
4282
4283
0
    if (evex)
4284
0
      {
4285
0
        i.tm.opcode_modifier.vex = 0;
4286
0
        i.tm.cpu.bitfield.cpuavx512f = i.tm.cpu_any.bitfield.cpuavx512f;
4287
0
        i.tm.cpu.bitfield.cpuavx512vl = i.tm.cpu_any.bitfield.cpuavx512vl;
4288
0
      }
4289
0
    else
4290
0
      {
4291
0
        i.tm.opcode_modifier.evex = 0;
4292
0
        if (i.tm.cpu_any.bitfield.cpuavx)
4293
0
    i.tm.cpu.bitfield.cpuavx = 1;
4294
0
        else if (!i.tm.cpu.bitfield.isa)
4295
0
    i.tm.cpu.bitfield.isa = i.tm.cpu_any.bitfield.isa;
4296
0
        else
4297
0
    gas_assert (i.tm.cpu.bitfield.isa == i.tm.cpu_any.bitfield.isa);
4298
0
      }
4299
0
  }
4300
4301
0
      if ((maybe_cpu (t, CpuCMPCCXADD) || maybe_cpu (t, CpuAMX_TILE)
4302
0
     || maybe_cpu (t, CpuAVX512F) || maybe_cpu (t, CpuAVX512DQ)
4303
0
     || maybe_cpu (t, CpuAVX512BW) || maybe_cpu (t, CpuBMI)
4304
0
     || maybe_cpu (t, CpuBMI2) || maybe_cpu (t, CpuUSER_MSR)
4305
0
     || maybe_cpu (t, CpuMSR_IMM) || maybe_cpu (t, CpuAMX_TRANSPOSE)
4306
0
     || maybe_cpu (t, CpuAMX_MOVRS))
4307
0
    && maybe_cpu (t, CpuAPX_F))
4308
0
  {
4309
0
    if (need_evex_encoding (t))
4310
0
      i.tm.opcode_modifier.vex = 0;
4311
0
    else
4312
0
      i.tm.opcode_modifier.evex = 0;
4313
0
  }
4314
0
    }
4315
4316
  /* The various VNNI extensions are somewhat special:
4317
     - AVX512-VNNI pre-dates AVX-VNNI,
4318
     - AVX-VNNI-INT{8,16} have EVEX counterparts added by AVX10-V1-AUX.
4319
     In each case, when the former is disabled, warn about the use of a
4320
     potentially unexpected encoding unless
4321
     - a disambiguating pseudo-prefix or operand is in use, or
4322
     - the newer ISA extension was explicitly enabled.  */
4323
3.44k
  if (is_cpu (t, CpuAVX_VNNI)
4324
0
      && !cpu_arch_isa_flags.bitfield.cpuavx_vnni
4325
0
      && pp.encoding != encoding_vex
4326
0
      && pp.encoding != encoding_vex3
4327
0
      && (!cpu_arch_flags.bitfield.cpuavx512_vnni
4328
0
    || !cpu_arch_flags.bitfield.cpuavx512vl))
4329
0
    as_warn (_("%s: will use AVX-VNNI encoding; use {vex} to silence"),
4330
0
       insn_name (t));
4331
4332
3.44k
  if (is_cpu (t, CpuAVX10_1_AUX)
4333
0
      && !cpu_arch_isa_flags.bitfield.cpuavx10_1_aux
4334
0
      && !need_evex_encoding (t)
4335
0
      && pp.encoding != encoding_egpr
4336
0
      && ((!cpu_arch_flags.bitfield.cpuavx_vnni_int8
4337
0
     && is_cpu (t - 1, CpuAVX_VNNI_INT8))
4338
0
    || (!cpu_arch_flags.bitfield.cpuavx_vnni_int16
4339
0
     && is_cpu (t - 1, CpuAVX_VNNI_INT16))))
4340
0
    as_warn (_("%s: will use AVX10 encoding; use {evex} to silence"),
4341
0
       insn_name (t));
4342
4343
  /* CRC32 is also somewhat special, as its APX form is dependent upon only
4344
     APX_F.  */
4345
3.44k
  if (t->mnem_off == MN_crc32
4346
0
      && is_cpu (t, CpuAPX_F)
4347
0
      && !cpu_arch_isa_flags.bitfield.cpuapx_f
4348
0
      && !cpu_arch_flags.bitfield.cpusse4_2
4349
0
      && !need_evex_encoding (t)
4350
0
      && pp.encoding != encoding_egpr)
4351
0
    as_warn (_("%s: will use APX encoding; use {evex} to silence"),
4352
0
       insn_name (t));
4353
4354
  /* For CCMP and CTEST the template has EVEX.SCC in base_opcode. Move it out of
4355
     there, to then adjust base_opcode to obtain its normal meaning.  */
4356
3.44k
  if (i.tm.opcode_modifier.operandconstraint == SCC)
4357
0
    {
4358
      /* Get EVEX.SCC value from the lower 4 bits of base_opcode.  */
4359
0
      i.scc = i.tm.base_opcode & 0xf;
4360
0
      i.tm.base_opcode >>= 8;
4361
0
    }
4362
4363
  /* For CMOVcc having undergone NDD-to-legacy optimization with its source
4364
     operands being swapped, we need to invert the encoded condition.  */
4365
3.44k
  if (i.invert_cond)
4366
0
    i.tm.base_opcode ^= 1;
4367
4368
  /* Note that for pseudo prefixes this produces a length of 1. But for them
4369
     the length isn't interesting at all.  */
4370
3.48k
  for (l = 1; l < 4; ++l)
4371
3.48k
    if (!(i.tm.base_opcode >> (8 * l)))
4372
3.44k
      break;
4373
4374
3.44k
  i.opcode_length = l;
4375
3.44k
}
4376
4377
/* Build the VEX prefix (2- or 3-byte)
4378
4379
   | C5h |
4380
   | `R3 | `vvvv | L | pp |
4381
4382
   | C4h |
4383
   | `R3 | `X3 | `B3 | mmmmm |
4384
   | W | `vvvv | L | pp |
4385
*/
4386
static void
4387
build_vex_prefix (const insn_template *t)
4388
293
{
4389
293
  unsigned int register_specifier;
4390
293
  unsigned int vector_length;
4391
293
  bool w;
4392
4393
  /* Check register specifier.  */
4394
293
  if (i.vex.register_specifier)
4395
0
    {
4396
0
      register_specifier =
4397
0
  ~register_number (i.vex.register_specifier) & 0xf;
4398
0
      gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
4399
0
    }
4400
293
  else
4401
293
    register_specifier = 0xf;
4402
4403
  /* Use 2-byte VEX prefix by swapping destination and source operand
4404
     if there are more than 1 register operand.  */
4405
293
  if (i.reg_operands > 1
4406
248
      && pp.encoding != encoding_vex3
4407
248
      && pp.dir_encoding == dir_encoding_default
4408
248
      && i.operands == i.reg_operands
4409
248
      && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
4410
248
      && i.tm.opcode_space == SPACE_0F
4411
0
      && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
4412
0
      && i.rex == REX_B)
4413
0
    {
4414
0
      unsigned int xchg;
4415
4416
0
      swap_2_operands (0, i.operands - 1);
4417
4418
0
      gas_assert (i.rm.mode == 3);
4419
4420
0
      i.rex = REX_R;
4421
0
      xchg = i.rm.regmem;
4422
0
      i.rm.regmem = i.rm.reg;
4423
0
      i.rm.reg = xchg;
4424
4425
0
      if (i.tm.opcode_modifier.d)
4426
0
  i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
4427
0
          ? Opcode_ExtD : Opcode_SIMD_IntD;
4428
0
      else /* Use the next insn.  */
4429
0
  install_template (&t[1]);
4430
0
    }
4431
4432
  /* Use 2-byte VEX prefix by swapping commutative source operands if there
4433
     are no memory operands and at least 3 register ones.  */
4434
293
  if (i.reg_operands >= 3
4435
0
      && pp.encoding != encoding_vex3
4436
0
      && i.reg_operands == i.operands - i.imm_operands
4437
0
      && i.tm.opcode_modifier.vex
4438
0
      && i.tm.opcode_modifier.commutative
4439
      /* .commutative aliases .staticrounding; disambiguate.  */
4440
0
      && !i.tm.opcode_modifier.sae
4441
0
      && (i.tm.opcode_modifier.sse2avx
4442
0
    || (optimize > 1 && !pp.no_optimize))
4443
0
      && i.rex == REX_B
4444
0
      && i.vex.register_specifier
4445
0
      && !(i.vex.register_specifier->reg_flags & RegRex))
4446
0
    {
4447
0
      unsigned int xchg = i.operands - i.reg_operands;
4448
4449
0
      gas_assert (i.tm.opcode_space == SPACE_0F);
4450
0
      gas_assert (!i.tm.opcode_modifier.sae);
4451
0
      gas_assert (operand_type_equal (&i.types[i.operands - 2],
4452
0
                                      &i.types[i.operands - 3]));
4453
0
      gas_assert (i.rm.mode == 3);
4454
4455
0
      swap_2_operands (xchg, xchg + 1);
4456
4457
0
      i.rex = 0;
4458
0
      xchg = i.rm.regmem | 8;
4459
0
      i.rm.regmem = ~register_specifier & 0xf;
4460
0
      gas_assert (!(i.rm.regmem & 8));
4461
0
      i.vex.register_specifier += xchg - i.rm.regmem;
4462
0
      register_specifier = ~xchg & 0xf;
4463
0
    }
4464
4465
293
  if (i.tm.opcode_modifier.vex == VEXScalar)
4466
42
    vector_length = avxscalar;
4467
251
  else if (i.tm.opcode_modifier.vex == VEX256)
4468
0
    vector_length = 1;
4469
251
  else if (dot_insn () && i.tm.opcode_modifier.vex == VEX128)
4470
248
    vector_length = 0;
4471
3
  else
4472
3
    {
4473
3
      unsigned int op;
4474
4475
      /* Determine vector length from the last multi-length vector
4476
   operand.  */
4477
3
      vector_length = 0;
4478
6
      for (op = t->operands; op--;)
4479
3
  if (i.tm_types[op].bitfield.xmmword
4480
0
      && i.tm_types[op].bitfield.ymmword
4481
0
      && i.types[op].bitfield.ymmword)
4482
0
    {
4483
0
      vector_length = 1;
4484
0
      break;
4485
0
    }
4486
3
    }
4487
4488
  /* Check the REX.W bit and VEXW.  */
4489
293
  if (i.tm.opcode_modifier.vexw == VEXWIG)
4490
253
    w = vexwig == vexw1 || (i.rex & REX_W);
4491
40
  else if (i.tm.opcode_modifier.vexw && !(i.rex & REX_W))
4492
6
    w = i.tm.opcode_modifier.vexw == VEXW1;
4493
34
  else
4494
34
    w = flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1;
4495
4496
  /* Use 2-byte VEX prefix if possible.  */
4497
293
  if (w == 0
4498
288
      && pp.encoding != encoding_vex3
4499
288
      && i.tm.opcode_space == SPACE_0F
4500
3
      && (i.rex & (REX_W | REX_X | REX_B)) == 0)
4501
3
    {
4502
      /* 2-byte VEX prefix.  */
4503
3
      bool r;
4504
4505
3
      i.vex.length = 2;
4506
3
      i.vex.bytes[0] = 0xc5;
4507
4508
      /* Check the REX.R bit.  */
4509
3
      r = !(i.rex & REX_R);
4510
3
      i.vex.bytes[1] = (r << 7
4511
3
      | register_specifier << 3
4512
3
      | vector_length << 2
4513
3
      | i.tm.opcode_modifier.opcodeprefix);
4514
3
    }
4515
290
  else
4516
290
    {
4517
      /* 3-byte VEX prefix.  */
4518
290
      i.vex.length = 3;
4519
4520
290
      switch (i.tm.opcode_space)
4521
290
  {
4522
0
  case SPACE_0F:
4523
290
  case SPACE_0F38:
4524
290
  case SPACE_0F3A:
4525
290
  case SPACE_MAP5:
4526
290
  case SPACE_MAP7:
4527
290
    i.vex.bytes[0] = 0xc4;
4528
290
    break;
4529
0
  case SPACE_XOP08:
4530
0
  case SPACE_XOP09:
4531
0
  case SPACE_XOP0A:
4532
0
    i.vex.bytes[0] = 0x8f;
4533
0
    break;
4534
0
  default:
4535
0
    abort ();
4536
290
  }
4537
4538
      /* The high 3 bits of the second VEX byte are 1's compliment
4539
   of RXB bits from REX.  */
4540
290
      i.vex.bytes[1] = ((~i.rex & 7) << 5)
4541
290
           | (!dot_insn () ? i.tm.opcode_space
4542
290
               : i.insn_opcode_space);
4543
4544
290
      i.vex.bytes[2] = (w << 7
4545
290
      | register_specifier << 3
4546
290
      | vector_length << 2
4547
290
      | i.tm.opcode_modifier.opcodeprefix);
4548
290
    }
4549
293
}
4550
4551
static INLINE bool
4552
is_any_vex_encoding (const insn_template *t)
4553
14.1k
{
4554
14.1k
  return t->opcode_modifier.vex || t->opcode_modifier.evex;
4555
14.1k
}
4556
4557
/* We can use this function only when the current encoding is evex.  */
4558
static INLINE bool
4559
is_apx_evex_encoding (void)
4560
320
{
4561
320
  return (i.rex2 & REX_B) || i.tm.opcode_space == SPACE_MAP4 || pp.has_nf;
4562
320
}
4563
4564
static INLINE bool
4565
is_apx_rex2_encoding (void)
4566
9.73k
{
4567
9.73k
  return i.rex2 || pp.rex2_encoding
4568
9.73k
  || i.tm.opcode_modifier.rex2;
4569
9.73k
}
4570
4571
static unsigned int
4572
get_broadcast_bytes (const insn_template *t, bool diag)
4573
0
{
4574
0
  unsigned int op, bytes;
4575
0
  const i386_operand_type *t_types = get_operand_types (t);
4576
0
  const i386_operand_type *types;
4577
4578
0
  if (i.broadcast.type)
4579
0
    return (1 << (t->opcode_modifier.broadcast - 1)) * i.broadcast.type;
4580
4581
0
  gas_assert (intel_syntax);
4582
4583
0
  for (op = 0; op < t->operands; ++op)
4584
0
    if (t_types[op].bitfield.baseindex)
4585
0
      break;
4586
4587
0
  gas_assert (op < t->operands);
4588
4589
0
  if (t->opcode_modifier.evex != EVEXDYN)
4590
0
    switch (i.broadcast.bytes)
4591
0
      {
4592
0
      case 1:
4593
0
  if (t_types[op].bitfield.word)
4594
0
    return 2;
4595
      /* Fall through.  */
4596
0
      case 2:
4597
0
  if (t_types[op].bitfield.dword)
4598
0
    return 4;
4599
      /* Fall through.  */
4600
0
      case 4:
4601
0
  if (t_types[op].bitfield.qword)
4602
0
    return 8;
4603
      /* Fall through.  */
4604
0
      case 8:
4605
0
  if (t_types[op].bitfield.xmmword)
4606
0
    return 16;
4607
0
  if (t_types[op].bitfield.ymmword)
4608
0
    return 32;
4609
0
  if (t_types[op].bitfield.zmmword)
4610
0
    return 64;
4611
      /* Fall through.  */
4612
0
      default:
4613
0
        abort ();
4614
0
      }
4615
4616
0
  gas_assert (op + 1 < t->operands);
4617
4618
0
  if (t_types[op + 1].bitfield.xmmword
4619
0
      + t_types[op + 1].bitfield.ymmword
4620
0
      + t_types[op + 1].bitfield.zmmword > 1)
4621
0
    {
4622
0
      types = &i.types[op + 1];
4623
0
      diag = false;
4624
0
    }
4625
0
  else /* Ambiguous - guess with a preference to non-AVX512VL forms.  */
4626
0
    types = &t_types[op];
4627
4628
0
  if (types->bitfield.zmmword)
4629
0
    bytes = 64;
4630
0
  else if (types->bitfield.ymmword)
4631
0
    bytes = 32;
4632
0
  else
4633
0
    bytes = 16;
4634
4635
0
  if (diag)
4636
0
    as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
4637
0
       insn_name (t), bytes * 8);
4638
4639
0
  return bytes;
4640
0
}
4641
4642
/* Build the EVEX prefix (4-byte) for evex insn
4643
   | 62h |
4644
   | `R3 | `X3 | `B3 | `R4 | B4 | mmm |
4645
   | W | `vvvv | U | pp |
4646
   | z | L'L | b | `V4 | aaa |
4647
*/
4648
static void
4649
build_evex_prefix (void)
4650
27
{
4651
27
  unsigned int register_specifier;
4652
27
  bool w;
4653
27
  rex_byte vrex_used = 0;
4654
4655
  /* Check register specifier.  */
4656
27
  if (i.vex.register_specifier)
4657
11
    {
4658
11
      gas_assert ((i.vrex & REX_X) == 0);
4659
4660
11
      register_specifier = i.vex.register_specifier->reg_num;
4661
11
      if ((i.vex.register_specifier->reg_flags & RegRex))
4662
0
  register_specifier += 8;
4663
      /* The upper 16 registers are encoded in the fourth byte of the
4664
   EVEX prefix.  */
4665
11
      if (!(i.vex.register_specifier->reg_flags & (RegVRex | RegRex2)))
4666
11
  i.vex.bytes[3] = 0x8;
4667
11
      register_specifier = ~register_specifier & 0xf;
4668
11
    }
4669
16
  else
4670
16
    {
4671
16
      register_specifier = 0xf;
4672
4673
      /* Encode upper 16 vector index register in the fourth byte of
4674
   the EVEX prefix.  */
4675
16
      if (!(i.vrex & REX_X))
4676
16
  i.vex.bytes[3] = 0x8;
4677
0
      else
4678
0
  vrex_used |= REX_X;
4679
16
    }
4680
4681
  /* 4 byte EVEX prefix.  */
4682
27
  i.vex.length = 4;
4683
27
  i.vex.bytes[0] = 0x62;
4684
4685
  /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
4686
     bits from REX.  */
4687
27
  gas_assert (i.tm.opcode_space >= SPACE_0F);
4688
27
  gas_assert (i.tm.opcode_space <= SPACE_MAP7);
4689
27
  i.vex.bytes[1] = ((~i.rex & 7) << 5)
4690
27
       | (!dot_insn () ? i.tm.opcode_space
4691
27
           : i.insn_opcode_space);
4692
4693
  /* The fifth bit of the second EVEX byte is 1's compliment of the
4694
     REX_R bit in VREX.  */
4695
27
  if (!((i.vrex | i.rex2) & REX_R))
4696
27
    i.vex.bytes[1] |= 0x10;
4697
27
  vrex_used |= i.vrex & REX_R;
4698
4699
27
  if ((i.reg_operands + i.imm_operands) == i.operands)
4700
25
    {
4701
      /* When all operands are registers, the REX_X bit in REX is not
4702
   used.  We reuse it to encode the upper 16 registers, which is
4703
   indicated by the REX_B bit in VREX.  The REX_X bit is encoded
4704
   as 1's compliment.  */
4705
25
      if ((i.vrex & REX_B))
4706
0
  {
4707
0
    vrex_used |= REX_B;
4708
0
    i.vex.bytes[1] &= ~0x40;
4709
0
  }
4710
25
    }
4711
4712
  /* EVEX instructions shouldn't need the REX prefix.  */
4713
27
  i.vrex &= ~vrex_used;
4714
27
  gas_assert (i.vrex == 0);
4715
4716
  /* Check the REX.W bit and VEXW.  */
4717
27
  if (i.tm.opcode_modifier.vexw == VEXWIG)
4718
14
    w = evexwig == evexw1 || (i.rex & REX_W);
4719
13
  else if (i.tm.opcode_modifier.vexw && !(i.rex & REX_W))
4720
2
    w = i.tm.opcode_modifier.vexw == VEXW1;
4721
11
  else
4722
11
    w = flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1;
4723
4724
27
  if (i.tm.opcode_modifier.evex == EVEXDYN)
4725
0
    {
4726
0
      unsigned int op;
4727
4728
      /* Determine vector length from the last multi-length vector operand.  */
4729
0
      for (op = i.operands; op--;)
4730
0
  if (i.tm_types[op].bitfield.xmmword
4731
0
      + i.tm_types[op].bitfield.ymmword
4732
0
      + i.tm_types[op].bitfield.zmmword > 1)
4733
0
    {
4734
0
      if (i.types[op].bitfield.zmmword)
4735
0
        {
4736
0
    i.tm.opcode_modifier.evex = EVEX512;
4737
0
    break;
4738
0
        }
4739
0
      else if (i.types[op].bitfield.ymmword)
4740
0
        {
4741
0
    i.tm.opcode_modifier.evex = EVEX256;
4742
0
    break;
4743
0
        }
4744
0
      else if (i.types[op].bitfield.xmmword)
4745
0
        {
4746
0
    i.tm.opcode_modifier.evex = EVEX128;
4747
0
    break;
4748
0
        }
4749
0
      else if ((i.broadcast.type || i.broadcast.bytes)
4750
0
          && op == i.broadcast.operand)
4751
0
        {
4752
0
    switch (get_broadcast_bytes (&i.tm, true))
4753
0
      {
4754
0
        case 64:
4755
0
          i.tm.opcode_modifier.evex = EVEX512;
4756
0
          break;
4757
0
        case 32:
4758
0
          i.tm.opcode_modifier.evex = EVEX256;
4759
0
          break;
4760
0
        case 16:
4761
0
          i.tm.opcode_modifier.evex = EVEX128;
4762
0
          break;
4763
0
        default:
4764
0
          abort ();
4765
0
      }
4766
0
    break;
4767
0
        }
4768
0
    }
4769
4770
0
      if (op >= MAX_OPERANDS)
4771
0
  abort ();
4772
0
    }
4773
4774
  /* The third byte of the EVEX prefix.  */
4775
27
  i.vex.bytes[2] = ((w << 7)
4776
27
        | (register_specifier << 3)
4777
27
        | (i.rex2 & REX_X ? 0 : 4) /* Encode the U bit.  */
4778
27
        | i.tm.opcode_modifier.opcodeprefix);
4779
4780
  /* The fourth byte of the EVEX prefix.  */
4781
  /* The zeroing-masking bit.  */
4782
27
  if (i.mask.reg && i.mask.zeroing)
4783
0
    i.vex.bytes[3] |= 0x80;
4784
4785
  /* Don't always set the broadcast bit if there is no RC.  */
4786
27
  if (i.rounding.type == rc_none)
4787
27
    {
4788
      /* Encode the vector length.  */
4789
27
      unsigned int vec_length;
4790
4791
27
      switch (i.tm.opcode_modifier.evex)
4792
27
  {
4793
15
  case EVEXLIG: /* LL' is ignored */
4794
15
    vec_length = evexlig << 5;
4795
15
    break;
4796
9
  case EVEX128:
4797
9
    vec_length = 0 << 5;
4798
9
    break;
4799
3
  case EVEX256:
4800
3
    vec_length = 1 << 5;
4801
3
    break;
4802
0
  case EVEX512:
4803
0
    vec_length = 2 << 5;
4804
0
    break;
4805
0
  case EVEX_L3:
4806
0
    if (dot_insn ())
4807
0
      {
4808
0
        vec_length = 3 << 5;
4809
0
        break;
4810
0
      }
4811
    /* Fall through.  */
4812
0
  default:
4813
0
    abort ();
4814
0
    break;
4815
27
  }
4816
27
      i.vex.bytes[3] |= vec_length;
4817
      /* Encode the broadcast bit.  */
4818
27
      if (i.broadcast.type || i.broadcast.bytes)
4819
0
  i.vex.bytes[3] |= 0x10;
4820
27
    }
4821
0
  else if (i.rounding.type != saeonly)
4822
0
    i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
4823
0
  else
4824
0
    i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
4825
4826
27
  if (i.mask.reg)
4827
0
    i.vex.bytes[3] |= i.mask.reg->reg_num;
4828
27
}
4829
4830
/* Build (2 bytes) rex2 prefix.
4831
   | D5h |
4832
   | m | R4 X4 B4 | W R3 X3 B3 |
4833
4834
   Rex2 reuses i.vex as they both encode i.tm.opcode_space in their prefixes.
4835
 */
4836
static void
4837
build_rex2_prefix (void)
4838
3
{
4839
3
  i.vex.length = 2;
4840
3
  i.vex.bytes[0] = 0xd5;
4841
  /* For the W R X B bits, the variables of rex prefix will be reused.  */
4842
3
  i.vex.bytes[1] = ((i.tm.opcode_space << 7)
4843
3
        | (i.rex2 << 4)
4844
3
        | ((i.rex | i.prefix[REX_PREFIX]) & 0xf));
4845
3
}
4846
4847
/* Build the EVEX prefix (4-byte) for APX insn.  Apart from the basic form
4848
   (see build_evex_prefix()) there are two new forms:
4849
4850
   | 62h |
4851
   | `R3 | `X3 | `B3 | `R4 | B4 | mmm |
4852
   | W | `vvvv | `X4 | pp |
4853
   | 00 | L | ND | `V4 | NF | 00 |
4854
4855
   and for NCI:
4856
4857
   | 62h |
4858
   | `R3 | `X3 | `B3 | `R4 | B4 | mmm |
4859
   | W | OSZC | `X4 | pp |
4860
   | 0000 | SC3 SC2 SC1 SC0 |
4861
*/
4862
static bool
4863
build_apx_evex_prefix (bool force_nd)
4864
9
{
4865
  /* To mimic behavior for legacy insns, transform use of DATA16 and REX64 into
4866
     their embedded-prefix representations.  */
4867
9
  if (i.tm.opcode_space == SPACE_MAP4)
4868
1
    {
4869
1
      if (i.prefix[DATA_PREFIX])
4870
0
  {
4871
0
    if (i.tm.opcode_modifier.opcodeprefix)
4872
0
      {
4873
0
        as_bad (i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66
4874
0
          ? _("same type of prefix used twice")
4875
0
          : _("conflicting use of `data16' prefix"));
4876
0
        return false;
4877
0
      }
4878
0
    i.tm.opcode_modifier.opcodeprefix = PREFIX_0X66;
4879
0
    i.prefix[DATA_PREFIX] = 0;
4880
0
  }
4881
1
      if (i.prefix[REX_PREFIX] & REX_W)
4882
0
  {
4883
0
    if (i.suffix == QWORD_MNEM_SUFFIX)
4884
0
      {
4885
0
        as_bad (_("same type of prefix used twice"));
4886
0
        return false;
4887
0
      }
4888
0
    i.tm.opcode_modifier.vexw = VEXW1;
4889
0
    i.prefix[REX_PREFIX] = 0;
4890
0
  }
4891
1
    }
4892
4893
9
  build_evex_prefix ();
4894
9
  if (i.rex2 & REX_B)
4895
0
    i.vex.bytes[1] |= 0x08;
4896
4897
  /* Encode the ND bit of instructions promoted from legacy space.
4898
     ZU shares the bit with ND.  */
4899
9
  if ((i.vex.register_specifier && i.tm.opcode_space == SPACE_MAP4)
4900
8
      || i.tm.opcode_modifier.operandconstraint == ZERO_UPPER
4901
8
      || force_nd)
4902
1
    {
4903
      /* Incoming ND and aaa bits should be 0.  */
4904
1
      know (!(i.vex.bytes[3] & 0x17));
4905
4906
1
      i.vex.bytes[3] |= 0x10;
4907
1
    }
4908
4909
  /* Encode SCC and oszc flags bits.  */
4910
9
  if (i.tm.opcode_modifier.operandconstraint == SCC)
4911
8
    {
4912
      /* Incoming ND and aaa bits should (still) be 0.  */
4913
8
      know (!(i.vex.bytes[3] & 0x17));
4914
4915
      /* The incoming value of vvvv is 1111, i.e. bits may need clearing.  */
4916
8
      i.vex.bytes[2] &= (i.oszc_flags << 3) | 0x87;
4917
      /* The incoming value of V4 is 1 and needs to be cleared.  */
4918
8
      i.vex.bytes[3] = (i.vex.bytes[3] & ~0x08) | i.scc;
4919
8
    }
4920
4921
  /* Encode the NF bit.  */
4922
9
  if (pp.has_nf || i.tm.opcode_modifier.operandconstraint == EVEX_NF)
4923
0
    {
4924
      /* Incoming aaa bits should (still) be 0.  */
4925
0
      know (!(i.vex.bytes[3] & 7));
4926
4927
0
      i.vex.bytes[3] |= 0x04;
4928
0
    }
4929
4930
9
  return true;
4931
9
}
4932
4933
static void establish_rex (void)
4934
4.90k
{
4935
  /* Note that legacy encodings have at most 2 non-immediate operands.  */
4936
4.90k
  unsigned int first = i.imm_operands;
4937
4.90k
  unsigned int last = i.operands > first ? i.operands - first - 1 : first;
4938
4939
  /* Respect a user-specified REX prefix.  */
4940
4.90k
  i.rex |= i.prefix[REX_PREFIX] & REX_OPCODE;
4941
4942
  /* For 8 bit RegRex64 registers without a prefix, we need an empty rex prefix.  */
4943
4.90k
  if (((i.types[first].bitfield.class == Reg
4944
1.44k
  && (i.op[first].regs->reg_flags & RegRex64) != 0)
4945
4.89k
       || (i.types[last].bitfield.class == Reg
4946
729
     && (i.op[last].regs->reg_flags & RegRex64) != 0))
4947
8
      && !is_apx_rex2_encoding () && !is_any_vex_encoding (&i.tm))
4948
8
    i.rex |= REX_OPCODE;
4949
4950
  /* For REX/REX2/EVEX prefix instructions, we need to convert old registers
4951
     (AL, CL, DL and BL) to new ones (AXL, CXL, DXL and BXL) and reject AH,
4952
     CH, DH and BH.  */
4953
4.90k
  if (i.rex || i.rex2 || i.tm.opcode_modifier.evex)
4954
61
    {
4955
117
      for (unsigned int x = first; x <= last; x++)
4956
56
  {
4957
    /* Look for 8 bit operand that uses old registers.  */
4958
56
    if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
4959
2
        && !(i.op[x].regs->reg_flags & (RegRex | RegRex2 | RegRex64)))
4960
0
      {
4961
        /* In case it is "hi" register, give up.  */
4962
0
        if (i.op[x].regs->reg_num > 3)
4963
0
    as_bad (_("can't encode register '%s%s' in an "
4964
0
        "instruction requiring %s prefix"),
4965
0
      register_prefix, i.op[x].regs->reg_name,
4966
0
      i.tm.opcode_modifier.evex ? "EVEX" : "REX/REX2");
4967
4968
        /* Otherwise it is equivalent to the extended register.
4969
     Since the encoding doesn't change this is merely
4970
     cosmetic cleanup for debug output.  */
4971
0
        i.op[x].regs += 8;
4972
0
      }
4973
56
  }
4974
61
    }
4975
4976
4.90k
  if (i.rex == 0 && i.rex2 == 0 && (pp.rex_encoding || pp.rex2_encoding))
4977
12
    {
4978
      /* Check if we can add a REX_OPCODE byte.  Look for 8 bit operand
4979
   that uses legacy register.  If it is "hi" register, don't add
4980
   rex and rex2 prefix.  */
4981
12
      unsigned int x;
4982
4983
24
      for (x = first; x <= last; x++)
4984
12
  if (i.types[x].bitfield.class == Reg
4985
0
      && i.types[x].bitfield.byte
4986
0
      && !(i.op[x].regs->reg_flags & (RegRex | RegRex2 | RegRex64))
4987
0
      && i.op[x].regs->reg_num > 3)
4988
0
    {
4989
0
      pp.rex_encoding = false;
4990
0
      pp.rex2_encoding = false;
4991
0
      break;
4992
0
    }
4993
4994
12
      if (pp.rex_encoding)
4995
12
  i.rex = REX_OPCODE;
4996
12
    }
4997
4998
4.90k
  if (is_apx_rex2_encoding ())
4999
3
    {
5000
      /* Most prefixes are not permitted with JMPABS.  */
5001
3
      if (i.tm.mnem_off == MN_jmpabs)
5002
0
  {
5003
0
    if (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
5004
0
      {
5005
0
        as_bad (_("size override not allowed with `%s'"),
5006
0
          insn_name (&i.tm));
5007
0
        i.prefix[DATA_PREFIX] = 0;
5008
0
        i.prefix[REX_PREFIX] &= ~REX_W;
5009
0
      }
5010
0
    if (i.prefix[ADDR_PREFIX])
5011
0
      {
5012
0
        as_bad (_("address override not allowed with `%s'"),
5013
0
          insn_name (&i.tm));
5014
0
        i.prefix[ADDR_PREFIX] = 0;
5015
0
      }
5016
0
  }
5017
5018
3
      build_rex2_prefix ();
5019
      /* The individual REX.RXBW bits got consumed.  */
5020
3
      i.rex &= REX_OPCODE;
5021
3
      i.prefix[REX_PREFIX] = 0;
5022
3
    }
5023
4.89k
  else if (i.rex != 0)
5024
69
    add_prefix (REX_OPCODE | i.rex);
5025
4.90k
}
5026
5027
static void
5028
process_immext (void)
5029
0
{
5030
0
  expressionS *exp;
5031
5032
  /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
5033
     which is coded in the same place as an 8-bit immediate field
5034
     would be.  Here we fake an 8-bit immediate operand from the
5035
     opcode suffix stored in tm.extension_opcode.
5036
5037
     AVX instructions also use this encoding, for some of
5038
     3 argument instructions.  */
5039
5040
0
  gas_assert (i.imm_operands <= 1
5041
0
        && (i.operands <= 2
5042
0
      || (is_any_vex_encoding (&i.tm)
5043
0
          && i.operands <= 4)));
5044
5045
0
  exp = &im_expressions[i.imm_operands++];
5046
0
  i.op[i.operands].imms = exp;
5047
0
  i.types[i.operands].bitfield.imm8 = 1;
5048
0
  i.operands++;
5049
0
  exp->X_op = O_constant;
5050
0
  exp->X_add_number = i.tm.extension_opcode;
5051
0
  i.tm.extension_opcode = None;
5052
0
}
5053
5054
5055
static int
5056
check_hle (void)
5057
0
{
5058
0
  switch (i.tm.opcode_modifier.prefixok)
5059
0
    {
5060
0
    default:
5061
0
      as_bad (_("invalid instruction `%s' after `%s'"),
5062
0
        insn_name (&i.tm), i.hle_prefix);
5063
0
      return 0;
5064
0
    case PrefixHLELock:
5065
0
      if (i.prefix[LOCK_PREFIX])
5066
0
  return 1;
5067
0
      as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
5068
0
      return 0;
5069
0
    case PrefixHLEAny:
5070
0
      return 1;
5071
0
    case PrefixHLERelease:
5072
0
      if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
5073
0
  {
5074
0
    as_bad (_("instruction `%s' after `xacquire' not allowed"),
5075
0
      insn_name (&i.tm));
5076
0
    return 0;
5077
0
  }
5078
0
      if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
5079
0
  {
5080
0
    as_bad (_("memory destination needed for instruction `%s'"
5081
0
        " after `xrelease'"), insn_name (&i.tm));
5082
0
    return 0;
5083
0
  }
5084
0
      return 1;
5085
0
    }
5086
0
}
5087
5088
/* Helper for optimization (running ahead of process_suffix()), to make sure we
5089
   convert only well-formed insns.  @OP is the sized operand to cross check
5090
   against (typically a register).  Checking against a single operand typically
5091
   suffices, as match_template() has already honored CheckOperandSize.  */
5092
5093
static bool is_plausible_suffix (unsigned int op)
5094
0
{
5095
0
  return !i.suffix
5096
0
   || (i.suffix == BYTE_MNEM_SUFFIX && i.types[op].bitfield.byte)
5097
0
   || (i.suffix == WORD_MNEM_SUFFIX && i.types[op].bitfield.word)
5098
0
   || (i.suffix == LONG_MNEM_SUFFIX && i.types[op].bitfield.dword)
5099
0
   || (i.suffix == QWORD_MNEM_SUFFIX && i.types[op].bitfield.qword);
5100
0
}
5101
5102
/* Encode aligned vector move as unaligned vector move.  */
5103
5104
static void
5105
encode_with_unaligned_vector_move (void)
5106
0
{
5107
0
  switch (i.tm.base_opcode)
5108
0
    {
5109
0
    case 0x28:  /* Load instructions.  */
5110
0
    case 0x29:  /* Store instructions.  */
5111
      /* movaps/movapd/vmovaps/vmovapd.  */
5112
0
      if (i.tm.opcode_space == SPACE_0F
5113
0
    && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
5114
0
  i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
5115
0
      break;
5116
0
    case 0x6f:  /* Load instructions.  */
5117
0
    case 0x7f:  /* Store instructions.  */
5118
      /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
5119
0
      if (i.tm.opcode_space == SPACE_0F
5120
0
    && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
5121
0
  i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
5122
0
      break;
5123
0
    default:
5124
0
      break;
5125
0
    }
5126
0
}
5127
5128
/* Try the shortest encoding by shortening operand size.  */
5129
5130
static void
5131
optimize_encoding (void)
5132
0
{
5133
0
  unsigned int j;
5134
5135
0
  if (i.tm.mnem_off == MN_lea)
5136
0
    {
5137
      /* Optimize: -O:
5138
     lea symbol, %rN    -> mov $symbol, %rN
5139
     lea (%rM), %rN     -> mov %rM, %rN
5140
     lea (,%rM,1), %rN  -> mov %rM, %rN
5141
5142
     and in 32-bit mode for 16-bit addressing
5143
5144
     lea (%rM), %rN     -> movzx %rM, %rN
5145
5146
     and in 64-bit mode zap 32-bit addressing in favor of using a
5147
     32-bit (or less) destination.
5148
       */
5149
0
      if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5150
0
  {
5151
0
    if (!i.types[1].bitfield.word)
5152
0
      i.tm.opcode_modifier.size = SIZE32;
5153
0
    i.prefix[ADDR_PREFIX] = 0;
5154
0
  }
5155
5156
0
      if (!i.index_reg && !i.base_reg)
5157
0
  {
5158
    /* Handle:
5159
         lea symbol, %rN    -> mov $symbol, %rN
5160
     */
5161
0
    if (flag_code == CODE_64BIT)
5162
0
      {
5163
        /* Don't transform a relocation to a 16-bit one.  */
5164
0
        if (i.op[0].disps
5165
0
      && i.op[0].disps->X_op != O_constant
5166
0
      && i.types[1].bitfield.word)
5167
0
    return;
5168
5169
0
        if (!i.types[1].bitfield.qword
5170
0
      || i.tm.opcode_modifier.size == SIZE32)
5171
0
    {
5172
0
      i.tm.base_opcode = 0xb8;
5173
0
      i.tm.opcode_modifier.modrm = 0;
5174
0
      if (!i.types[1].bitfield.word)
5175
0
        i.types[0].bitfield.imm32 = 1;
5176
0
      else
5177
0
        {
5178
0
          i.tm.opcode_modifier.size = SIZE16;
5179
0
          i.types[0].bitfield.imm16 = 1;
5180
0
        }
5181
0
    }
5182
0
        else
5183
0
    {
5184
      /* Subject to further optimization below.  */
5185
0
      i.tm.base_opcode = 0xc7;
5186
0
      i.tm.extension_opcode = 0;
5187
0
      i.types[0].bitfield.imm32s = 1;
5188
0
      i.types[0].bitfield.baseindex = 0;
5189
0
    }
5190
0
      }
5191
    /* Outside of 64-bit mode address and operand sizes have to match if
5192
       a relocation is involved, as otherwise we wouldn't (currently) or
5193
       even couldn't express the relocation correctly.  */
5194
0
    else if (i.op[0].disps
5195
0
       && i.op[0].disps->X_op != O_constant
5196
0
       && ((!i.prefix[ADDR_PREFIX])
5197
0
           != (flag_code == CODE_32BIT
5198
0
         ? i.types[1].bitfield.dword
5199
0
         : i.types[1].bitfield.word)))
5200
0
      return;
5201
    /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
5202
       destination is going to grow encoding size.  */
5203
0
    else if (flag_code == CODE_16BIT
5204
0
       && (optimize <= 1 || optimize_for_space)
5205
0
       && !i.prefix[ADDR_PREFIX]
5206
0
       && i.types[1].bitfield.dword)
5207
0
      return;
5208
0
    else
5209
0
      {
5210
0
        i.tm.base_opcode = 0xb8;
5211
0
        i.tm.opcode_modifier.modrm = 0;
5212
0
        if (i.types[1].bitfield.dword)
5213
0
    i.types[0].bitfield.imm32 = 1;
5214
0
        else
5215
0
    i.types[0].bitfield.imm16 = 1;
5216
5217
0
        if (i.op[0].disps
5218
0
      && i.op[0].disps->X_op == O_constant
5219
0
      && i.types[1].bitfield.dword
5220
      /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
5221
         GCC 5. */
5222
0
      && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
5223
0
    i.op[0].disps->X_add_number &= 0xffff;
5224
0
      }
5225
5226
0
    i.tm_types[0] = i.types[0];
5227
0
    i.imm_operands = 1;
5228
0
    if (!i.op[0].imms)
5229
0
      {
5230
0
        i.op[0].imms = &im_expressions[0];
5231
0
        i.op[0].imms->X_op = O_absent;
5232
0
      }
5233
0
  }
5234
0
      else if (i.op[0].disps
5235
0
      && (i.op[0].disps->X_op != O_constant
5236
0
          || i.op[0].disps->X_add_number))
5237
0
  return;
5238
0
      else
5239
0
  {
5240
    /* Handle:
5241
         lea (%rM), %rN     -> mov %rM, %rN
5242
         lea (,%rM,1), %rN  -> mov %rM, %rN
5243
         lea (%rM), %rN     -> movzx %rM, %rN
5244
     */
5245
0
    const reg_entry *addr_reg;
5246
5247
0
    if (!i.index_reg && i.base_reg->reg_num != RegIP)
5248
0
      addr_reg = i.base_reg;
5249
0
    else if (!i.base_reg
5250
0
       && i.index_reg->reg_num != RegIZ
5251
0
       && !i.log2_scale_factor)
5252
0
      addr_reg = i.index_reg;
5253
0
    else
5254
0
      return;
5255
5256
0
    if (addr_reg->reg_type.bitfield.word
5257
0
        && i.types[1].bitfield.dword)
5258
0
      {
5259
0
        if (flag_code != CODE_32BIT)
5260
0
    return;
5261
0
        i.tm.opcode_space = SPACE_0F;
5262
0
        i.tm.base_opcode = 0xb7;
5263
0
      }
5264
0
    else
5265
0
      i.tm.base_opcode = 0x8b;
5266
5267
0
    if (addr_reg->reg_type.bitfield.dword
5268
0
        && i.types[1].bitfield.qword)
5269
0
      i.tm.opcode_modifier.size = SIZE32;
5270
5271
0
    i.op[0].regs = addr_reg;
5272
0
    i.reg_operands = 2;
5273
0
  }
5274
5275
0
      i.mem_operands = 0;
5276
0
      i.disp_operands = 0;
5277
0
      i.prefix[ADDR_PREFIX] = 0;
5278
0
      i.prefix[SEG_PREFIX] = 0;
5279
0
      i.seg[0] = NULL;
5280
0
    }
5281
5282
0
  if (optimize_for_disabled_optimizations
5283
0
      && i.tm.mnem_off == MN_xchg
5284
0
      && i.reg_operands == 2
5285
0
      && i.op[0].regs == i.op[1].regs)
5286
0
    {
5287
      /* Optimize: -O:
5288
     xchg %rN, %rN     -> mov %rN, %rN
5289
       */
5290
0
      i.tm.base_opcode = pp.dir_encoding == dir_encoding_load ? 0x8a : 0x88;
5291
0
    }
5292
5293
0
  if (i.tm.mnem_off == MN_xadd
5294
0
      && i.reg_operands == 2
5295
0
      && i.op[0].regs == i.op[1].regs)
5296
0
    {
5297
      /* Optimize: -O:
5298
     xadd %rN, %rN     -> add %rN, %rN
5299
       */
5300
0
      i.tm.opcode_space = SPACE_BASE;
5301
0
      i.tm.base_opcode = 0x00;
5302
0
    }
5303
5304
0
  if (((i.tm.opcode_space == SPACE_0F
5305
0
        && (i.tm.base_opcode | 1) == 0xbf
5306
0
        && (i.types[0].bitfield.byte
5307
0
      ? i.types[1].bitfield.word
5308
0
      : i.types[1].bitfield.dword))
5309
0
       || (i.tm.opcode_space == SPACE_BASE
5310
0
     && i.tm.base_opcode == 0x63
5311
0
     && i.types[1].bitfield.qword))
5312
0
      && i.reg_operands == 2
5313
0
      && i.op[0].regs->reg_type.bitfield.instance == Accum
5314
0
      && i.op[1].regs->reg_type.bitfield.instance == Accum
5315
0
      && (cpu_arch_tune != PROCESSOR_K6 || optimize_for_space))
5316
0
    {
5317
      /* Optimize: -O:
5318
     movsb     %al, %ax    -> cbw
5319
     movsw     %ax, %eax   -> cwde
5320
     movsl     %eax, %rax  -> cdqe
5321
       */
5322
0
      i.tm.opcode_space = SPACE_BASE;
5323
0
      i.tm.base_opcode = 0x98;
5324
0
      i.tm.opcode_modifier.modrm = 0;
5325
      /* Leave the destination register in place for process_suffix() to take
5326
   care of operand sizing.  This will end up as short_form encoding,
5327
   with the register number being 0 (i.e. not altering the opcode).  */
5328
0
      i.reg_operands = 1;
5329
0
      i.op[0].regs = i.op[1].regs;
5330
0
      i.tm_types[1].bitfield.class = ClassNone;
5331
0
      return;
5332
0
    }
5333
5334
0
  if (optimize_for_space
5335
0
      && i.tm.opcode_space == SPACE_0F
5336
0
      && (i.tm.base_opcode | 1) == 0xb7
5337
0
      && i.reg_operands == 2
5338
0
      && !i.op[0].regs->reg_flags
5339
0
      && !i.op[1].regs->reg_flags
5340
0
      && (i.types[0].bitfield.byte
5341
0
    ? i.types[1].bitfield.word
5342
0
      && i.op[0].regs->reg_num < 4
5343
0
      && i.op[1].regs->reg_num == i.op[0].regs->reg_num
5344
0
      && (!i.suffix || i.suffix == WORD_MNEM_SUFFIX)
5345
0
    : i.types[1].bitfield.dword
5346
0
      && flag_code == CODE_16BIT
5347
0
      && i.op[0].regs->reg_type.bitfield.baseindex
5348
0
      && i.op[0].regs->reg_num != EBP_REG_NUM))
5349
0
    {
5350
      /* Optimize: -Os:
5351
     movzb     %r8, %r16    -> mov $0, %r8h
5352
5353
     %r8 being one of %al, %cl, %dl, or %bl, with %r16 being the
5354
     matching 16-bit reg.
5355
       */
5356
5357
0
      i.tm.opcode_space = SPACE_BASE;
5358
0
      i.tm.opcode_modifier.w = 0;
5359
0
      i.reg_operands = 1;
5360
0
      if (i.types[0].bitfield.byte)
5361
0
  {
5362
0
    i.tm.base_opcode = 0xb0;
5363
0
    i.tm.opcode_modifier.modrm = 0;
5364
0
    copy_operand (1, 0);
5365
0
    i.op[1].regs += 4;
5366
5367
0
    im_expressions[0].X_op = O_constant;
5368
0
    im_expressions[0].X_add_number = 0;
5369
0
    i.op[0].imms = &im_expressions[0];
5370
0
    operand_type_set (&i.types[0], 0);
5371
0
    i.types[0].bitfield.imm8 = 1;
5372
0
    i.tm_types[0] = i.types[0];
5373
0
    i.tm_types[0].bitfield.class = ClassNone;
5374
0
    i.imm_operands = 1;
5375
5376
0
    i.suffix = 0;
5377
0
    return;
5378
0
  }
5379
5380
      /* In 16-bit mode, optimize: -Os:
5381
     movzw     %r16, %r32   -> lea (%r16), %r32
5382
5383
     %r16 being one of %bx, %si, or %di.
5384
       */
5385
0
      i.tm.base_opcode = 0x8d;
5386
5387
0
      i.base_reg = i.op[0].regs;
5388
0
      operand_type_set (&i.types[0], 0);
5389
0
      i.types[0].bitfield.baseindex = 1;
5390
0
      i.tm_types[0] = i.types[0];
5391
0
      i.op[0].disps = NULL;
5392
0
      i.flags[0] = Operand_Mem;
5393
0
      i.mem_operands = 1;
5394
0
      return;
5395
0
    }
5396
5397
0
  if (optimize_for_space
5398
0
      && (i.tm.mnem_off == MN_test
5399
0
          || (i.tm.base_opcode == 0xf6
5400
0
              && i.tm.opcode_space == SPACE_MAP4))
5401
0
      && i.reg_operands == 1
5402
0
      && i.imm_operands == 1
5403
0
      && !i.types[1].bitfield.byte
5404
0
      && is_plausible_suffix (1)
5405
0
      && i.op[0].imms->X_op == O_constant
5406
0
      && fits_in_imm7 (i.op[0].imms->X_add_number))
5407
0
    {
5408
      /* Optimize: -Os:
5409
     test      $imm7, %r64/%r32/%r16  -> test      $imm7, %r8
5410
     ctest<cc> $imm7, %r64/%r32/%r16  -> ctest<cc> $imm7, %r8
5411
       */
5412
0
      unsigned int base_regnum = i.op[1].regs->reg_num;
5413
5414
0
      gas_assert (!i.tm.opcode_modifier.modrm || i.tm.extension_opcode == 0);
5415
5416
0
      if (flag_code == CODE_64BIT || base_regnum < 4)
5417
0
  {
5418
0
    i.types[1].bitfield.byte = 1;
5419
    /* Squash the suffix.  */
5420
0
    i.suffix = 0;
5421
    /* Convert to byte registers. 8-bit registers are special,
5422
       RegRex64 and non-RegRex* each have 8 registers.  */
5423
0
    if (i.types[1].bitfield.word)
5424
      /* 32 (or 40) 8-bit registers.  */
5425
0
      j = 32;
5426
0
    else if (i.types[1].bitfield.dword)
5427
      /* 32 (or 40) 8-bit registers + 32 16-bit registers.  */
5428
0
      j = 64;
5429
0
    else
5430
      /* 32 (or 40) 8-bit registers + 32 16-bit registers
5431
         + 32 32-bit registers.  */
5432
0
      j = 96;
5433
5434
    /* In 64-bit mode, the following byte registers cannot be accessed
5435
       if using the Rex and Rex2 prefix: AH, BH, CH, DH */
5436
0
    if (!(i.op[1].regs->reg_flags & (RegRex | RegRex2)) && base_regnum < 4)
5437
0
      j += 8;
5438
0
    i.op[1].regs -= j;
5439
0
  }
5440
0
    }
5441
0
  else if (flag_code == CODE_64BIT
5442
0
     && i.tm.opcode_space == SPACE_BASE
5443
0
     && i.types[i.operands - 1].bitfield.qword
5444
0
     && ((i.reg_operands == 1
5445
0
    && i.imm_operands == 1
5446
0
    && i.op[0].imms->X_op == O_constant
5447
0
    && ((i.tm.base_opcode == 0xb8
5448
0
         && i.tm.extension_opcode == None
5449
0
         && fits_in_unsigned_long (i.op[0].imms->X_add_number))
5450
0
        || (fits_in_imm31 (i.op[0].imms->X_add_number)
5451
0
      && (i.tm.base_opcode == 0x24
5452
0
          || (((i.tm.base_opcode == 0x80
5453
0
          && i.tm.extension_opcode == 0x4)
5454
0
         || i.tm.mnem_off == MN_test)
5455
0
        && !(i.op[1].regs->reg_flags
5456
0
             & (RegRex | RegRex2)))
5457
0
          || ((i.tm.base_opcode | 1) == 0xc7
5458
0
        && i.tm.extension_opcode == 0x0)))
5459
0
        || (fits_in_imm7 (i.op[0].imms->X_add_number)
5460
0
      && i.tm.base_opcode == 0x83
5461
0
      && i.tm.extension_opcode == 0x4
5462
0
      && !(i.op[1].regs->reg_flags & (RegRex | RegRex2)))))
5463
0
         || ((i.reg_operands == 2
5464
0
        && i.op[0].regs == i.op[1].regs
5465
0
        && (i.tm.mnem_off == MN_xor
5466
0
      || i.tm.mnem_off == MN_sub))
5467
0
       || i.tm.mnem_off == MN_clr)))
5468
0
    {
5469
      /* Optimize: -O:
5470
     andq $imm31, %r64   -> andl $imm31, %r32
5471
     andq $imm7, %r64    -> andl $imm7, %r32
5472
     testq $imm31, %r64  -> testl $imm31, %r32
5473
     xorq %r64, %r64     -> xorl %r32, %r32
5474
     clrq %r64           -> clrl %r32
5475
     subq %r64, %r64     -> subl %r32, %r32
5476
     movq $imm31, %r64   -> movl $imm31, %r32
5477
     movq $imm32, %r64   -> movl $imm32, %r32
5478
        */
5479
0
      i.tm.opcode_modifier.size = SIZE32;
5480
0
      if (i.imm_operands)
5481
0
  {
5482
0
    i.types[0].bitfield.imm32 = 1;
5483
0
    i.types[0].bitfield.imm32s = 0;
5484
0
    i.types[0].bitfield.imm64 = 0;
5485
0
  }
5486
0
      else
5487
0
  {
5488
0
    i.types[0].bitfield.dword = 1;
5489
0
    i.types[0].bitfield.qword = 0;
5490
0
  }
5491
0
      i.types[1].bitfield.dword = 1;
5492
0
      i.types[1].bitfield.qword = 0;
5493
0
      if (i.tm.mnem_off == MN_mov || i.tm.mnem_off == MN_lea)
5494
0
  {
5495
    /* Handle
5496
         movq $imm31, %r64   -> movl $imm31, %r32
5497
         movq $imm32, %r64   -> movl $imm32, %r32
5498
     */
5499
0
    i.tm_types[0].bitfield.imm32 = 1;
5500
0
    i.tm_types[0].bitfield.imm32s = 0;
5501
0
    i.tm_types[0].bitfield.imm64 = 0;
5502
0
    if ((i.tm.base_opcode | 1) == 0xc7)
5503
0
      {
5504
        /* Handle
5505
       movq $imm31, %r64   -> movl $imm31, %r32
5506
         */
5507
0
        i.tm.base_opcode = 0xb8;
5508
0
        i.tm.extension_opcode = None;
5509
0
        i.tm.opcode_modifier.w = 0;
5510
0
        i.tm.opcode_modifier.modrm = 0;
5511
0
      }
5512
0
  }
5513
0
    }
5514
0
  else if (i.reg_operands == 3
5515
0
     && i.op[0].regs == i.op[1].regs
5516
0
     && pp.encoding != encoding_evex
5517
0
     && (i.tm.mnem_off == MN_xor
5518
0
         || i.tm.mnem_off == MN_sub))
5519
0
    {
5520
      /* Optimize: -O:
5521
     xorb %rNb, %rNb, %rMb  -> xorl %rMd, %rMd
5522
     xorw %rNw, %rNw, %rMw  -> xorl %rMd, %rMd
5523
     xorl %rNd, %rNd, %rMd  -> xorl %rMd, %rMd
5524
     xorq %rN,  %rN,  %rM   -> xorl %rMd, %rMd
5525
     subb %rNb, %rNb, %rMb  -> subl %rMd, %rMd
5526
     subw %rNw, %rNw, %rMw  -> subl %rMd, %rMd
5527
     subl %rNd, %rNd, %rMd  -> subl %rMd, %rMd
5528
     subq %rN,  %rN,  %rM   -> subl %rMd, %rMd
5529
        */
5530
0
      i.tm.opcode_space = SPACE_BASE;
5531
0
      i.tm.opcode_modifier.evex = 0;
5532
0
      i.tm.opcode_modifier.size = SIZE32;
5533
0
      i.types[0].bitfield.byte = 0;
5534
0
      i.types[0].bitfield.word = 0;
5535
0
      i.types[0].bitfield.dword = 1;
5536
0
      i.types[0].bitfield.qword = 0;
5537
0
      i.op[0].regs = i.op[2].regs;
5538
0
      i.types[1] = i.types[0];
5539
0
      i.op[1].regs = i.op[2].regs;
5540
0
      i.reg_operands = 2;
5541
0
    }
5542
0
  else if (optimize > 1
5543
0
     && !optimize_for_space
5544
0
     && i.reg_operands == 2
5545
0
     && i.op[0].regs == i.op[1].regs
5546
0
     && (i.tm.mnem_off == MN_and || i.tm.mnem_off == MN_or)
5547
0
     && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
5548
0
    {
5549
      /* Optimize: -O2:
5550
     andb %rN, %rN  -> testb %rN, %rN
5551
     andw %rN, %rN  -> testw %rN, %rN
5552
     andq %rN, %rN  -> testq %rN, %rN
5553
     orb %rN, %rN   -> testb %rN, %rN
5554
     orw %rN, %rN   -> testw %rN, %rN
5555
     orq %rN, %rN   -> testq %rN, %rN
5556
5557
     and outside of 64-bit mode
5558
5559
     andl %rN, %rN  -> testl %rN, %rN
5560
     orl %rN, %rN   -> testl %rN, %rN
5561
       */
5562
0
      i.tm.base_opcode = 0x84;
5563
0
    }
5564
0
  else if (!optimize_for_space
5565
0
     && i.tm.base_opcode == 0xd0
5566
0
     && i.tm.extension_opcode == 4
5567
0
     && (i.tm.opcode_space == SPACE_BASE
5568
0
         || i.tm.opcode_space == SPACE_MAP4)
5569
0
     && !i.mem_operands)
5570
0
    {
5571
      /* Optimize: -O:
5572
     shlb $1, %rN  -> addb %rN, %rN
5573
     shlw $1, %rN  -> addw %rN, %rN
5574
     shll $1, %rN  -> addl %rN, %rN
5575
     shlq $1, %rN  -> addq %rN, %rN
5576
5577
     shlb $1, %rN, %rM  -> addb %rN, %rN, %rM
5578
     shlw $1, %rN, %rM  -> addw %rN, %rN, %rM
5579
     shll $1, %rN, %rM  -> addl %rN, %rN, %rM
5580
     shlq $1, %rN, %rM  -> addq %rN, %rN, %rM
5581
       */
5582
0
      i.tm.base_opcode = 0x00;
5583
0
      i.tm.extension_opcode = None;
5584
0
      if (i.operands >= 2)
5585
0
  copy_operand (0, 1);
5586
0
      else
5587
0
  {
5588
    /* Legacy form with omitted shift count operand.  */
5589
0
    copy_operand (1, 0);
5590
0
    i.operands = 2;
5591
0
  }
5592
0
      i.reg_operands++;
5593
0
      i.imm_operands = 0;
5594
0
    }
5595
0
  else if (i.tm.base_opcode == 0xba
5596
0
     && i.tm.opcode_space == SPACE_0F
5597
0
     && i.reg_operands == 1
5598
0
     && i.op[0].imms->X_op == O_constant
5599
0
     && i.op[0].imms->X_add_number >= 0)
5600
0
    {
5601
      /* Optimize: -O:
5602
     btw $n, %rN -> btl $n, %rN (outside of 16-bit mode, n < 16)
5603
     btq $n, %rN -> btl $n, %rN (in 64-bit mode, n < 32, N < 8)
5604
     btl $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
5605
5606
     With <BT> one of bts, btr, and bts also:
5607
     <BT>w $n, %rN -> btl $n, %rN (in 32-bit mode, n < 16)
5608
     <BT>l $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
5609
       */
5610
0
      switch (flag_code)
5611
0
  {
5612
0
  case CODE_64BIT:
5613
0
    if (i.tm.extension_opcode != 4)
5614
0
      break;
5615
0
    if (i.types[1].bitfield.qword
5616
0
        && i.op[0].imms->X_add_number < 32
5617
0
        && !(i.op[1].regs->reg_flags & RegRex))
5618
0
      i.tm.opcode_modifier.size = SIZE32;
5619
    /* Fall through.  */
5620
0
  case CODE_32BIT:
5621
0
    if (i.types[1].bitfield.word
5622
0
        && i.op[0].imms->X_add_number < 16)
5623
0
      i.tm.opcode_modifier.size = SIZE32;
5624
0
    break;
5625
0
  case CODE_16BIT:
5626
0
    if (i.op[0].imms->X_add_number < 16)
5627
0
      i.tm.opcode_modifier.size = SIZE16;
5628
0
    break;
5629
0
  }
5630
0
    }
5631
0
  else if (optimize > 1
5632
0
     && (i.tm.base_opcode | 0xf) == 0x4f
5633
0
     && i.tm.opcode_space == SPACE_MAP4
5634
0
     && i.reg_operands == 3
5635
0
     && i.tm.opcode_modifier.operandconstraint == EVEX_NF
5636
0
     && !i.types[0].bitfield.word)
5637
0
    {
5638
      /* Optimize: -O2:
5639
     cfcmov<cc> %rM, %rN, %rN -> cmov<cc> %rM, %rN
5640
     cfcmov<cc> %rM, %rN, %rM -> cmov<!cc> %rN, %rM
5641
     cfcmov<cc> %rN, %rN, %rN -> nop %rN
5642
       */
5643
0
      if (i.op[0].regs == i.op[2].regs)
5644
0
  {
5645
0
    i.tm.base_opcode ^= 1;
5646
0
    i.op[0].regs = i.op[1].regs;
5647
0
    i.op[1].regs = i.op[2].regs;
5648
0
  }
5649
0
      else if (i.op[1].regs != i.op[2].regs)
5650
0
  return;
5651
5652
0
      i.tm.opcode_space = SPACE_0F;
5653
0
      i.tm.opcode_modifier.evex = 0;
5654
0
      i.tm.opcode_modifier.vexvvvv = 0;
5655
0
      i.tm.opcode_modifier.operandconstraint = 0;
5656
0
      i.reg_operands = 2;
5657
5658
      /* While at it, convert to NOP if all three regs match.  */
5659
0
      if (i.op[0].regs == i.op[1].regs)
5660
0
  {
5661
0
    i.tm.base_opcode = 0x1f;
5662
0
    i.tm.extension_opcode = 0;
5663
0
    i.reg_operands = 1;
5664
0
  }
5665
0
    }
5666
0
  else if (i.reg_operands == 3
5667
0
     && i.op[0].regs == i.op[1].regs
5668
0
     && !i.types[2].bitfield.xmmword
5669
0
     && (i.tm.opcode_modifier.vex
5670
0
         || ((!i.mask.reg || i.mask.zeroing)
5671
0
       && i.tm.opcode_modifier.evex
5672
0
       && (pp.encoding != encoding_evex
5673
0
           || cpu_arch_isa_flags.bitfield.cpuavx512vl
5674
0
           || is_cpu (&i.tm, CpuAVX512VL)
5675
0
           || (i.tm_types[2].bitfield.zmmword
5676
0
         && i.types[2].bitfield.ymmword))))
5677
0
     && i.tm.opcode_space == SPACE_0F
5678
0
     && ((i.tm.base_opcode | 2) == 0x57
5679
0
         || i.tm.base_opcode == 0xdf
5680
0
         || i.tm.base_opcode == 0xef
5681
0
         || (i.tm.base_opcode | 3) == 0xfb
5682
0
         || i.tm.base_opcode == 0x42
5683
0
         || i.tm.base_opcode == 0x47))
5684
0
    {
5685
      /* Optimize: -O1:
5686
     VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
5687
     vpsubq and vpsubw:
5688
       EVEX VOP %zmmM, %zmmM, %zmmN
5689
         -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
5690
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
5691
       EVEX VOP %ymmM, %ymmM, %ymmN
5692
         -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
5693
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
5694
       VEX VOP %ymmM, %ymmM, %ymmN
5695
         -> VEX VOP %xmmM, %xmmM, %xmmN
5696
     VOP, one of vpandn and vpxor:
5697
       VEX VOP %ymmM, %ymmM, %ymmN
5698
         -> VEX VOP %xmmM, %xmmM, %xmmN
5699
     VOP, one of vpandnd and vpandnq:
5700
       EVEX VOP %zmmM, %zmmM, %zmmN
5701
         -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
5702
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
5703
       EVEX VOP %ymmM, %ymmM, %ymmN
5704
         -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
5705
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
5706
     VOP, one of vpxord and vpxorq:
5707
       EVEX VOP %zmmM, %zmmM, %zmmN
5708
         -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
5709
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
5710
       EVEX VOP %ymmM, %ymmM, %ymmN
5711
         -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
5712
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
5713
     VOP, one of kxord and kxorq:
5714
       VEX VOP %kM, %kM, %kN
5715
         -> VEX kxorw %kM, %kM, %kN
5716
     VOP, one of kandnd and kandnq:
5717
       VEX VOP %kM, %kM, %kN
5718
         -> VEX kandnw %kM, %kM, %kN
5719
       */
5720
0
      if (i.tm.opcode_modifier.evex)
5721
0
  {
5722
0
    if (pp.encoding != encoding_evex)
5723
0
      {
5724
0
        i.tm.opcode_modifier.vex = VEX128;
5725
0
        i.tm.opcode_modifier.vexw = VEXW0;
5726
0
        i.tm.opcode_modifier.evex = 0;
5727
0
        pp.encoding = encoding_vex;
5728
0
        i.mask.reg = NULL;
5729
0
      }
5730
0
    else if (optimize > 1)
5731
0
      i.tm.opcode_modifier.evex = EVEX128;
5732
0
    else
5733
0
      return;
5734
0
  }
5735
0
      else if (i.tm_types[0].bitfield.class == RegMask)
5736
0
  {
5737
0
    i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
5738
0
    i.tm.opcode_modifier.vexw = VEXW0;
5739
0
  }
5740
0
      else
5741
0
  i.tm.opcode_modifier.vex = VEX128;
5742
5743
0
      if (i.tm.opcode_modifier.vex)
5744
0
  for (j = 0; j < 3; j++)
5745
0
    {
5746
0
      i.types[j].bitfield.xmmword = 1;
5747
0
      i.types[j].bitfield.ymmword = 0;
5748
0
    }
5749
0
    }
5750
0
  else if (pp.encoding != encoding_evex
5751
0
     && pp.encoding != encoding_egpr
5752
0
     && !i.types[0].bitfield.zmmword
5753
0
     && !i.types[1].bitfield.zmmword
5754
0
     && !i.mask.reg
5755
0
     && !i.broadcast.type
5756
0
     && !i.broadcast.bytes
5757
0
     && i.tm.opcode_modifier.evex
5758
0
     && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
5759
0
         || (i.tm.base_opcode & ~4) == 0xdb
5760
0
         || (i.tm.base_opcode & ~4) == 0xeb)
5761
0
     && i.tm.extension_opcode == None)
5762
0
    {
5763
      /* Optimize: -O1:
5764
     VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
5765
     vmovdqu32 and vmovdqu64:
5766
       EVEX VOP %xmmM, %xmmN
5767
         -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
5768
       EVEX VOP %ymmM, %ymmN
5769
         -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
5770
       EVEX VOP %xmmM, mem
5771
         -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
5772
       EVEX VOP %ymmM, mem
5773
         -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
5774
       EVEX VOP mem, %xmmN
5775
         -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
5776
       EVEX VOP mem, %ymmN
5777
         -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
5778
     VOP, one of vpand, vpandn, vpor, vpxor:
5779
       EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
5780
         -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
5781
       EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
5782
         -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
5783
       EVEX VOP{d,q} mem, %xmmM, %xmmN
5784
         -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
5785
       EVEX VOP{d,q} mem, %ymmM, %ymmN
5786
         -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
5787
       */
5788
0
      for (j = 0; j < i.operands; j++)
5789
0
  if (operand_type_check (i.types[j], disp)
5790
0
      && i.op[j].disps->X_op == O_constant)
5791
0
    {
5792
      /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
5793
         has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
5794
         bytes, we choose EVEX Disp8 over VEX Disp32.  */
5795
0
      int evex_disp8, vex_disp8;
5796
0
      unsigned int memshift = i.memshift;
5797
0
      offsetT n = i.op[j].disps->X_add_number;
5798
5799
0
      evex_disp8 = fits_in_disp8 (n);
5800
0
      i.memshift = 0;
5801
0
      vex_disp8 = fits_in_disp8 (n);
5802
0
      if (evex_disp8 != vex_disp8)
5803
0
        {
5804
0
    i.memshift = memshift;
5805
0
    return;
5806
0
        }
5807
5808
0
      i.types[j].bitfield.disp8 = vex_disp8;
5809
0
      break;
5810
0
    }
5811
0
      if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
5812
0
    && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
5813
0
  i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
5814
0
      i.tm.opcode_modifier.vex
5815
0
  = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
5816
0
      i.tm.opcode_modifier.vexw = VEXW0;
5817
      /* VPAND, VPOR, and VPXOR are commutative.  */
5818
0
      if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
5819
0
  i.tm.opcode_modifier.commutative = 1;
5820
0
      i.tm.opcode_modifier.evex = 0;
5821
0
      i.tm.opcode_modifier.masking = 0;
5822
0
      i.tm.opcode_modifier.broadcast = 0;
5823
0
      i.tm.opcode_modifier.disp8memshift = 0;
5824
0
      i.memshift = 0;
5825
0
      if (j < i.operands)
5826
0
  i.types[j].bitfield.disp8
5827
0
    = fits_in_disp8 (i.op[j].disps->X_add_number);
5828
0
    }
5829
0
  else if (optimize_for_space
5830
0
     && i.tm.base_opcode == 0x29
5831
0
     && i.tm.opcode_space == SPACE_0F38
5832
0
     && i.operands == i.reg_operands
5833
0
     && i.op[0].regs == i.op[1].regs
5834
0
     && (!i.tm.opcode_modifier.vex
5835
0
         || !(i.op[0].regs->reg_flags & RegRex))
5836
0
     && !i.tm.opcode_modifier.evex)
5837
0
    {
5838
      /* Optimize: -Os:
5839
         pcmpeqq %xmmN, %xmmN          -> pcmpeqd %xmmN, %xmmN
5840
         vpcmpeqq %xmmN, %xmmN, %xmmM  -> vpcmpeqd %xmmN, %xmmN, %xmmM (N < 8)
5841
         vpcmpeqq %ymmN, %ymmN, %ymmM  -> vpcmpeqd %ymmN, %ymmN, %ymmM (N < 8)
5842
       */
5843
0
      i.tm.opcode_space = SPACE_0F;
5844
0
      i.tm.base_opcode = 0x76;
5845
0
    }
5846
0
  else if (((i.tm.base_opcode >= 0x64
5847
0
       && i.tm.base_opcode <= 0x66
5848
0
       && i.tm.opcode_space == SPACE_0F)
5849
0
      || (i.tm.base_opcode == 0x37
5850
0
    && i.tm.opcode_space == SPACE_0F38))
5851
0
     && i.operands == i.reg_operands
5852
0
     && i.op[0].regs == i.op[1].regs
5853
0
     && !i.tm.opcode_modifier.evex)
5854
0
    {
5855
      /* Optimize: -O:
5856
         pcmpgt[bwd] %mmN, %mmN             -> pxor %mmN, %mmN
5857
         pcmpgt[bwdq] %xmmN, %xmmN          -> pxor %xmmN, %xmmN
5858
         vpcmpgt[bwdq] %xmmN, %xmmN, %xmmM  -> vpxor %xmmN, %xmmN, %xmmM (N < 8)
5859
         vpcmpgt[bwdq] %xmmN, %xmmN, %xmmM  -> vpxor %xmm0, %xmm0, %xmmM (N > 7)
5860
         vpcmpgt[bwdq] %ymmN, %ymmN, %ymmM  -> vpxor %ymmN, %ymmN, %ymmM (N < 8)
5861
         vpcmpgt[bwdq] %ymmN, %ymmN, %ymmM  -> vpxor %ymm0, %ymm0, %ymmM (N > 7)
5862
       */
5863
0
      i.tm.opcode_space = SPACE_0F;
5864
0
      i.tm.base_opcode = 0xef;
5865
0
      if (i.tm.opcode_modifier.vex && (i.op[0].regs->reg_flags & RegRex))
5866
0
  {
5867
0
    if (i.operands == 2)
5868
0
      {
5869
0
        gas_assert (i.tm.opcode_modifier.sse2avx);
5870
5871
0
        i.operands = 3;
5872
0
        i.reg_operands = 3;
5873
0
        i.tm.operands = 3;
5874
5875
0
        copy_operand (2, 0);
5876
5877
0
        i.tm.opcode_modifier.sse2avx = 0;
5878
0
      }
5879
0
    i.op[0].regs -= i.op[0].regs->reg_num + 8;
5880
0
    i.op[1].regs = i.op[0].regs;
5881
0
  }
5882
0
    }
5883
0
  else if (i.tm.extension_opcode == 6
5884
0
     && i.tm.base_opcode >= 0x71
5885
0
     && i.tm.base_opcode <= 0x73
5886
0
     && i.tm.opcode_space == SPACE_0F
5887
0
     && i.op[0].imms->X_op == O_constant
5888
0
     && i.op[0].imms->X_add_number == 1
5889
0
     && !i.mem_operands)
5890
0
    {
5891
      /* Optimize: -O:
5892
     psllw $1, %mmxN          -> paddw %mmxN, %mmxN
5893
     psllw $1, %xmmN          -> paddw %xmmN, %xmmN
5894
     vpsllw $1, %xmmN, %xmmM  -> vpaddw %xmmN, %xmmN, %xmmM
5895
     vpsllw $1, %ymmN, %ymmM  -> vpaddw %ymmN, %ymmN, %ymmM
5896
     vpsllw $1, %zmmN, %zmmM  -> vpaddw %zmmN, %zmmN, %zmmM
5897
5898
     pslld $1, %mmxN          -> paddd %mmxN, %mmxN
5899
     pslld $1, %xmmN          -> paddd %xmmN, %xmmN
5900
     vpslld $1, %xmmN, %xmmM  -> vpaddd %xmmN, %xmmN, %xmmM
5901
     vpslld $1, %ymmN, %ymmM  -> vpaddd %ymmN, %ymmN, %ymmM
5902
     vpslld $1, %zmmN, %zmmM  -> vpaddd %zmmN, %zmmN, %zmmM
5903
5904
     psllq $1, %xmmN          -> paddq %xmmN, %xmmN
5905
     vpsllq $1, %xmmN, %xmmM  -> vpaddq %xmmN, %xmmN, %xmmM
5906
     vpsllq $1, %ymmN, %ymmM  -> vpaddq %ymmN, %ymmN, %ymmM
5907
     vpsllq $1, %zmmN, %zmmM  -> vpaddq %zmmN, %zmmN, %zmmM
5908
    */
5909
0
      if (i.tm.base_opcode != 0x73)
5910
0
  i.tm.base_opcode |= 0xfc; /* {,v}padd{w,d} */
5911
0
      else
5912
0
  {
5913
0
    gas_assert (i.tm_types[1].bitfield.class != RegMMX);
5914
0
    i.tm.base_opcode = 0xd4; /* {,v}paddq */
5915
0
  }
5916
0
      i.tm.extension_opcode = None;
5917
0
      if (i.tm.opcode_modifier.vexvvvv)
5918
0
  i.tm.opcode_modifier.vexvvvv = VexVVVV_SRC1;
5919
0
      copy_operand (0, 1);
5920
0
      i.reg_operands++;
5921
0
      i.imm_operands = 0;
5922
0
    }
5923
0
  else if (optimize_for_space
5924
0
     && i.tm.base_opcode == 0x59
5925
0
     && i.tm.opcode_space == SPACE_0F38
5926
0
     && i.operands == i.reg_operands
5927
0
     && i.tm.opcode_modifier.vex
5928
0
     && !(i.op[0].regs->reg_flags & RegRex)
5929
0
     && i.types[1].bitfield.xmmword
5930
0
     && pp.encoding != encoding_vex3)
5931
0
    {
5932
      /* Optimize: -Os:
5933
         vpbroadcastq %xmmN, %xmmM  -> vpunpcklqdq %xmmN, %xmmN, %xmmM (N < 8)
5934
       */
5935
0
      i.tm.opcode_space = SPACE_0F;
5936
0
      i.tm.base_opcode = 0x6c;
5937
0
      i.tm.opcode_modifier.vexvvvv = VexVVVV_SRC1;
5938
5939
0
      ++i.operands;
5940
0
      ++i.reg_operands;
5941
0
      ++i.tm.operands;
5942
5943
0
      copy_operand (2, 0);
5944
0
      swap_2_operands (1, 2);
5945
0
    }
5946
0
  else if (i.tm.base_opcode == 0x16
5947
0
     && i.tm.opcode_space == SPACE_0F3A
5948
0
     && i.op[0].imms->X_op == O_constant
5949
0
     && i.op[0].imms->X_add_number == 0)
5950
0
    {
5951
      /* Optimize: -O:
5952
         pextrd $0, %xmmN, ...   -> movd %xmmN, ...
5953
         pextrq $0, %xmmN, ...   -> movq %xmmN, ...
5954
         vpextrd $0, %xmmN, ...  -> vmovd %xmmN, ...
5955
         vpextrq $0, %xmmN, ...  -> vmovq %xmmN, ...
5956
       */
5957
0
      i.tm.opcode_space = SPACE_0F;
5958
0
      if (!i.mem_operands
5959
0
    || i.tm.opcode_modifier.evex
5960
0
    || (i.tm.opcode_modifier.vexw != VEXW1
5961
0
        && i.tm.opcode_modifier.size != SIZE64))
5962
0
  i.tm.base_opcode = 0x7e;
5963
0
      else
5964
0
  {
5965
0
    i.tm.base_opcode = 0xd6;
5966
0
    i.tm.opcode_modifier.size = 0;
5967
0
    i.tm.opcode_modifier.vexw
5968
0
      = i.tm.opcode_modifier.sse2avx ? VEXW0 : VEXWIG;
5969
0
  }
5970
5971
0
      copy_operand (0, 1);
5972
0
      copy_operand (1, 2);
5973
5974
0
      i.operands = 2;
5975
0
      i.imm_operands = 0;
5976
0
    }
5977
0
  else if (i.tm.base_opcode == 0x17
5978
0
     && i.tm.opcode_space == SPACE_0F3A
5979
0
     && i.op[0].imms->X_op == O_constant
5980
0
     && i.op[0].imms->X_add_number == 0)
5981
0
    {
5982
      /* Optimize: -O:
5983
         extractps $0, %xmmN, %rM   -> movd %xmmN, %rM
5984
         extractps $0, %xmmN, mem   -> movss %xmmN, mem
5985
         vextractps $0, %xmmN, %rM  -> vmovd %xmmN, %rM
5986
         vextractps $0, %xmmN, mem  -> vmovss %xmmN, mem
5987
       */
5988
0
      i.tm.opcode_space = SPACE_0F;
5989
0
      i.tm.opcode_modifier.vexw = VEXW0;
5990
5991
0
      if (!i.mem_operands)
5992
0
  i.tm.base_opcode = 0x7e;
5993
0
      else
5994
0
  {
5995
0
    i.tm.base_opcode = 0x11;
5996
0
    i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
5997
0
  }
5998
5999
0
      copy_operand (0, 1);
6000
0
      copy_operand (1, 2);
6001
6002
0
      i.operands = 2;
6003
0
      i.imm_operands = 0;
6004
0
    }
6005
0
  else if ((i.tm.base_opcode | 0x22) == 0x3b
6006
0
     && i.tm.opcode_space == SPACE_0F3A
6007
0
     && i.op[0].imms->X_op == O_constant
6008
0
     && i.op[0].imms->X_add_number == 0)
6009
0
    {
6010
      /* Optimize: -O:
6011
         vextractf128 $0, %ymmN, %xmmM      -> vmovaps %xmmN, %xmmM
6012
         vextractf128 $0, %ymmN, mem        -> vmovups %xmmN, mem
6013
         vextractf32x4 $0, %[yz]mmN, %xmmM  -> vmovaps %xmmN, %xmmM
6014
         vextractf32x4 $0, %[yz]mmN, mem    -> vmovups %xmmN, mem
6015
         vextractf64x2 $0, %[yz]mmN, %xmmM  -> vmovapd %xmmN, %xmmM
6016
         vextractf64x2 $0, %[yz]mmN, mem    -> vmovupd %xmmN, mem
6017
         vextractf32x8 $0, %zmmN, %ymmM     -> vmovaps %ymmN, %ymmM
6018
         vextractf32x8 $0, %zmmN, mem       -> vmovups %ymmN, mem
6019
         vextractf64x4 $0, %zmmN, %ymmM     -> vmovapd %ymmN, %ymmM
6020
         vextractf64x4 $0, %zmmN, mem       -> vmovupd %ymmN, mem
6021
         vextracti128 $0, %ymmN, %xmmM      -> vmovdqa %xmmN, %xmmM
6022
         vextracti128 $0, %ymmN, mem        -> vmovdqu %xmmN, mem
6023
         vextracti32x4 $0, %[yz]mmN, %xmmM  -> vmovdqa{,32} %xmmN, %xmmM
6024
         vextracti32x4 $0, %[yz]mmN, mem    -> vmovdqu{,32} %xmmN, mem
6025
         vextracti64x2 $0, %[yz]mmN, %xmmM  -> vmovdqa{,64} %xmmN, %xmmM
6026
         vextracti64x2 $0, %[yz]mmN, mem    -> vmovdqu{,64} %xmmN, mem
6027
         vextracti32x8 $0, %zmmN, %ymmM     -> vmovdqa{,32} %ymmN, %ymmM
6028
         vextracti32x8 $0, %zmmN, mem       -> vmovdqu{,32} %ymmN, mem
6029
         vextracti64x4 $0, %zmmN, %ymmM     -> vmovdqa{,64} %ymmN, %ymmM
6030
         vextracti64x4 $0, %zmmN, mem       -> vmovdqu{,64} %ymmN, mem
6031
       */
6032
0
      i.tm.opcode_space = SPACE_0F;
6033
6034
0
      if (!i.mask.reg
6035
0
    && (pp.encoding <= encoding_vex3
6036
0
        || (pp.encoding == encoding_evex512
6037
0
      && (!i.base_reg || !(i.base_reg->reg_flags & RegRex2))
6038
0
      && (!i.index_reg || !(i.index_reg->reg_flags & RegRex2)))))
6039
0
  {
6040
0
    i.tm.opcode_modifier.vex = i.tm.base_opcode & 2 ? VEX256 : VEX128;
6041
0
    i.tm.opcode_modifier.evex = 0;
6042
0
  }
6043
0
      else
6044
0
  i.tm.opcode_modifier.evex = i.tm.base_opcode & 2 ? EVEX256 : EVEX128;
6045
6046
0
      if (i.tm.base_opcode & 0x20)
6047
0
  {
6048
0
    i.tm.base_opcode = 0x7f;
6049
0
    if (i.reg_operands != 2)
6050
0
      i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
6051
0
  }
6052
0
      else
6053
0
  {
6054
0
    if (i.reg_operands == 2)
6055
0
      i.tm.base_opcode = 0x29;
6056
0
    else
6057
0
      i.tm.base_opcode = 0x11;
6058
0
    if (i.tm.opcode_modifier.vexw != VEXW1)
6059
0
      i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
6060
0
  }
6061
6062
0
      if (i.tm.opcode_modifier.vex)
6063
0
  i.tm.opcode_modifier.vexw = VEXWIG;
6064
6065
0
      copy_operand (0, 1);
6066
0
      copy_operand (1, 2);
6067
6068
0
      i.operands = 2;
6069
0
      i.imm_operands = 0;
6070
0
    }
6071
0
  else if (i.tm.base_opcode == 0x21
6072
0
     && i.tm.opcode_space == SPACE_0F3A
6073
0
     && i.op[0].imms->X_op == O_constant
6074
0
     && (i.operands == i.reg_operands + 1
6075
0
         ? i.op[0].imms->X_add_number == 0
6076
0
     || (i.op[0].imms->X_add_number & 0xf) == 0xf
6077
0
         : (i.op[0].imms->X_add_number & 0x3f) == 0x0e
6078
0
      && (i.reg_operands == 1 || i.op[2].regs == i.op[3].regs)))
6079
0
    {
6080
      /* Optimize: -O:
6081
         insertps $0b....1111, %xmmN, %xmmM          -> xorps %xmmM, %xmmM
6082
         insertps $0b00000000, %xmmN, %xmmM          -> movss %xmmN, %xmmM
6083
         insertps $0b..001110, mem, %xmmN            -> movss mem, %xmmN
6084
         vinsertps $0b....1111, %xmmN, %xmmM, %xmmK  -> vxorps %xmm?, %xmm?, %xmmK
6085
         vinsertps $0b00000000, %xmmN, %xmmM, %xmmK  -> vmovss %xmmN, %xmmM, %xmmK
6086
         vinsertps $0b..001110, mem, %xmmN, %xmmN    -> vmovss mem, %xmmN
6087
       */
6088
0
      i.tm.opcode_space = SPACE_0F;
6089
0
      if ((i.op[0].imms->X_add_number & 0xf) == 0xf)
6090
0
  {
6091
0
    i.tm.base_opcode = 0x57;
6092
0
    i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
6093
6094
0
    --i.operands;
6095
6096
0
    copy_operand (i.operands - 1, i.operands);
6097
0
    copy_operand (1, i.operands - 1);
6098
0
    copy_operand (0, 1);
6099
6100
    /* Switch from EVEX to VEX encoding if possible.  Sadly we can't
6101
       (always) tell use of the {evex} pseudo-prefix (which otherwise
6102
       we'd like to respect) from use of %xmm16-%xmm31.  */
6103
0
    if (pp.encoding == encoding_evex)
6104
0
      pp.encoding = encoding_default;
6105
0
    if (i.tm.opcode_modifier.evex
6106
0
        && pp.encoding <= encoding_vex3
6107
0
        && !(i.op[0].regs->reg_flags & RegVRex))
6108
0
      {
6109
0
        i.tm.opcode_modifier.evex = 0;
6110
0
        i.tm.opcode_modifier.vex = VEX128;
6111
0
      }
6112
6113
    /* Switch from VEX3 to VEX2 encoding if possible.  */
6114
0
    if (i.tm.opcode_modifier.vex
6115
0
        && pp.encoding <= encoding_vex
6116
0
        && (i.op[0].regs->reg_flags & RegRex))
6117
0
      {
6118
0
        i.op[0].regs -= 8;
6119
0
        i.op[1].regs = i.op[0].regs;
6120
0
      }
6121
0
  }
6122
0
      else
6123
0
  {
6124
0
    i.tm.base_opcode = 0x10;
6125
0
    i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
6126
6127
0
    if (i.op[0].imms->X_add_number == 0)
6128
0
      --i.operands;
6129
0
    else
6130
0
      {
6131
0
        i.operands = 2;
6132
0
        i.tm.opcode_modifier.vexvvvv = 0;
6133
0
      }
6134
0
    copy_operand (0, 1);
6135
0
    copy_operand (1, 2);
6136
0
    copy_operand (2, 3);
6137
0
  }
6138
6139
0
      i.imm_operands = 0;
6140
0
    }
6141
0
}
6142
6143
/* Check whether the promoted (to address size) register is usable as index
6144
   register in ModR/M SIB addressing.  */
6145
6146
static bool is_index (const reg_entry *r)
6147
0
{
6148
0
  gas_assert (flag_code == CODE_64BIT);
6149
6150
0
  if (r->reg_type.bitfield.byte)
6151
0
    {
6152
0
      if (!(r->reg_flags & (RegRex | RegRex2 | RegRex64)))
6153
0
  {
6154
0
    if (r->reg_num >= 4)
6155
0
      return false;
6156
0
    r += 8;
6157
0
  }
6158
0
      r += 32;
6159
0
    }
6160
0
  if (r->reg_type.bitfield.word)
6161
0
    r += 32;
6162
  /* No need to further check .dword here.  */
6163
6164
0
  return r->reg_type.bitfield.baseindex;
6165
0
}
6166
6167
/* Try to shorten {nf} encodings, by shortening operand size or switching to
6168
   functionally identical encodings.  */
6169
6170
static void
6171
optimize_nf_encoding (void)
6172
0
{
6173
0
  if (i.tm.base_opcode == 0x80
6174
0
      && (i.tm.extension_opcode == 0 || i.tm.extension_opcode == 5)
6175
0
      && i.suffix != BYTE_MNEM_SUFFIX
6176
0
      && !i.types[1].bitfield.byte
6177
0
      && !i.types[2].bitfield.byte
6178
0
      && i.op[0].imms->X_op == O_constant
6179
0
      && i.op[0].imms->X_add_number == 0x80)
6180
0
    {
6181
      /* Optimize: -O:
6182
     {nf} addw $0x80, ...  -> {nf} subw $-0x80, ...
6183
     {nf} addl $0x80, ...  -> {nf} subl $-0x80, ...
6184
     {nf} addq $0x80, ...  -> {nf} subq $-0x80, ...
6185
6186
     {nf} subw $0x80, ...  -> {nf} addw $-0x80, ...
6187
     {nf} subl $0x80, ...  -> {nf} addl $-0x80, ...
6188
     {nf} subq $0x80, ...  -> {nf} addq $-0x80, ...
6189
       */
6190
0
      i.tm.base_opcode |= 3;
6191
0
      i.tm.extension_opcode ^= 5;
6192
0
      i.tm.opcode_modifier.w = 0;
6193
0
      i.op[0].imms->X_add_number = -i.op[0].imms->X_add_number;
6194
6195
0
      i.tm_types[0].bitfield.imm8 = 0;
6196
0
      i.tm_types[0].bitfield.imm8s = 1;
6197
0
      i.tm_types[0].bitfield.imm16 = 0;
6198
0
      i.tm_types[0].bitfield.imm32 = 0;
6199
0
      i.tm_types[0].bitfield.imm32s = 0;
6200
6201
0
      i.types[0] = i.tm_types[0];
6202
0
    }
6203
0
  else if ((i.tm.base_opcode | 3) == 0x83
6204
0
      && (i.tm.extension_opcode == 0 || i.tm.extension_opcode == 5)
6205
0
      && i.op[0].imms->X_op == O_constant
6206
0
      && (i.op[0].imms->X_add_number == 1
6207
0
    || i.op[0].imms->X_add_number == -1
6208
    /* While for wider than byte operations immediates were suitably
6209
       adjusted earlier on, 0xff in the byte case needs covering
6210
       explicitly.  */
6211
0
    || (i.op[0].imms->X_add_number == 0xff
6212
0
        && (i.suffix == BYTE_MNEM_SUFFIX
6213
0
      || i.types[i.operands - 1].bitfield.byte))))
6214
0
    {
6215
      /* Optimize: -O:
6216
     {nf} add $1, ...        -> {nf} inc ...
6217
     {nf} add $-1, ...       -> {nf} dec ...
6218
     {nf} add $0xf...f, ...  -> {nf} dec ...
6219
6220
     {nf} sub $1, ...        -> {nf} dec ...
6221
     {nf} sub $-1, ...       -> {nf} inc ...
6222
     {nf} sub $0xf...f, ...  -> {nf} inc ...
6223
       */
6224
0
      i.tm.base_opcode = 0xfe;
6225
0
      i.tm.extension_opcode
6226
0
  = (i.op[0].imms->X_add_number == 1) != (i.tm.extension_opcode == 0);
6227
0
      i.tm.opcode_modifier.w = 1;
6228
6229
0
      copy_operand (0, 1);
6230
0
      copy_operand (1, 2);
6231
6232
0
      i.imm_operands = 0;
6233
0
      --i.operands;
6234
0
    }
6235
0
  else if (i.tm.base_opcode == 0xc0
6236
0
     && i.op[0].imms->X_op == O_constant
6237
0
     && i.op[0].imms->X_add_number
6238
0
        == (i.types[i.operands - 1].bitfield.byte
6239
0
      || i.suffix == BYTE_MNEM_SUFFIX
6240
0
      ? 7 : i.types[i.operands - 1].bitfield.word
6241
0
      || i.suffix == WORD_MNEM_SUFFIX
6242
0
      ? 15 : 63 >> (i.types[i.operands - 1].bitfield.dword
6243
0
              || i.suffix == LONG_MNEM_SUFFIX)))
6244
0
    {
6245
      /* Optimize: -O:
6246
     {nf} rol $osz-1, ...   -> {nf} ror $1, ...
6247
     {nf} ror $osz-1, ...   -> {nf} rol $1, ...
6248
       */
6249
0
      gas_assert (i.tm.extension_opcode <= 1);
6250
0
      i.tm.extension_opcode ^= 1;
6251
0
      i.tm.base_opcode = 0xd0;
6252
0
      i.tm_types[0].bitfield.imm1 = 1;
6253
0
      i.imm_operands = 0;
6254
0
    }
6255
0
  else if ((i.tm.base_opcode | 2) == 0x6b
6256
0
     && i.op[0].imms->X_op == O_constant
6257
0
     && (i.op[0].imms->X_add_number > 0
6258
0
         ? !(i.op[0].imms->X_add_number & (i.op[0].imms->X_add_number - 1))
6259
         /* optimize_imm() converts to sign-extended representation where
6260
      possible (and input can also come with these specific numbers).  */
6261
0
         : (i.types[i.operands - 1].bitfield.word
6262
0
      && i.op[0].imms->X_add_number == -0x8000)
6263
0
     || (i.types[i.operands - 1].bitfield.dword
6264
0
         && i.op[0].imms->X_add_number + 1 == -0x7fffffff))
6265
     /* 16-bit 3-operand non-ZU forms need leaviong alone, to prevent
6266
        zero-extension of the result.  Unless, of course, both non-
6267
        immediate operands match (which can be converted to the non-NDD
6268
        form).  */
6269
0
     && (i.operands < 3
6270
0
         || !i.types[2].bitfield.word
6271
0
         || i.tm.mnem_off == MN_imulzu
6272
0
         || i.op[2].regs == i.op[1].regs)
6273
     /* When merely optimizing for size, exclude cases where we'd convert
6274
        from Imm8S to Imm8 encoding, thus not actually reducing size.  */
6275
0
     && (!optimize_for_space
6276
0
         || i.tm.base_opcode == 0x69
6277
0
         || !(i.op[0].imms->X_add_number & 0x7d)))
6278
0
    {
6279
      /* Optimize: -O:
6280
     {nf} imul   $1<<N, ...   -> {nf} shl $N, ...
6281
     {nf} imulzu $1<<N, ...   -> {nf} shl $N, ...
6282
       */
6283
0
      if (i.op[0].imms->X_add_number != 2)
6284
0
  {
6285
0
    i.tm.base_opcode = 0xc0;
6286
0
    i.op[0].imms->X_add_number = ffs (i.op[0].imms->X_add_number) - 1;
6287
0
    i.tm_types[0].bitfield.imm8 = 1;
6288
0
    i.tm_types[0].bitfield.imm16 = 0;
6289
0
    i.tm_types[0].bitfield.imm32 = 0;
6290
0
    i.tm_types[0].bitfield.imm32s = 0;
6291
0
  }
6292
0
      else
6293
0
  {
6294
0
    i.tm.base_opcode = 0xd0;
6295
0
    i.tm_types[0].bitfield.imm1 = 1;
6296
0
  }
6297
0
      i.types[0] = i.tm_types[0];
6298
0
      i.tm.extension_opcode = 4;
6299
0
      i.tm.opcode_modifier.w = 1;
6300
0
      i.tm.opcode_modifier.operandconstraint = 0;
6301
0
      if (i.operands == 3)
6302
0
  {
6303
0
    if (i.op[2].regs == i.op[1].regs && i.tm.mnem_off != MN_imulzu)
6304
0
      {
6305
        /* Convert to non-NDD form.  This is required for 16-bit insns
6306
           (to prevent zero-extension) and benign for others.  */
6307
0
        i.operands = 2;
6308
0
        i.reg_operands = 1;
6309
0
      }
6310
0
    else
6311
0
      i.tm.opcode_modifier.vexvvvv = VexVVVV_DST;
6312
0
  }
6313
0
      else if (i.tm.mnem_off == MN_imulzu)
6314
0
  {
6315
    /* Convert to NDD form, to effect zero-extension of the result.  */
6316
0
    i.tm.opcode_modifier.vexvvvv = VexVVVV_DST;
6317
0
    i.operands = 3;
6318
0
    i.reg_operands = 2;
6319
0
    copy_operand (2, 1);
6320
0
  }
6321
0
    }
6322
6323
0
  if (optimize_for_space
6324
0
      && pp.encoding != encoding_evex
6325
0
      && (i.tm.base_opcode == 0x00
6326
0
    || (i.tm.base_opcode == 0xd0 && i.tm.extension_opcode == 4))
6327
0
      && !i.mem_operands
6328
0
      && !i.types[1].bitfield.byte
6329
      /* 16-bit operand size has extra restrictions: If REX2 was needed,
6330
   no size reduction would be possible.  Plus 3-operand forms zero-
6331
   extend the result, which can't be expressed with LEA.  */
6332
0
      && (!i.types[1].bitfield.word
6333
0
    || (i.operands == 2 && pp.encoding != encoding_egpr))
6334
0
      && is_plausible_suffix (1)
6335
      /* %rsp can't be the index.  */
6336
0
      && (is_index (i.op[1].regs)
6337
0
    || (i.imm_operands == 0 && is_index (i.op[0].regs)))
6338
      /* While %rbp, %r13, %r21, and %r29 can be made the index in order to
6339
   avoid the otherwise necessary Disp8, if the other operand is also
6340
   from that set and REX2 would be required to encode the insn, the
6341
   resulting encoding would be no smaller than the EVEX one.  */
6342
0
      && (i.op[1].regs->reg_num != 5
6343
0
    || pp.encoding != encoding_egpr
6344
0
    || i.imm_operands > 0
6345
0
    || i.op[0].regs->reg_num != 5))
6346
0
    {
6347
      /* Optimize: -Os:
6348
     {nf} addw %N, %M    -> leaw (%rM,%rN), %M
6349
     {nf} addl %eN, %eM  -> leal (%rM,%rN), %eM
6350
     {nf} addq %rN, %rM  -> leaq (%rM,%rN), %rM
6351
6352
     {nf} shlw $1, %N   -> leaw (%rN,%rN), %N
6353
     {nf} shll $1, %eN  -> leal (%rN,%rN), %eN
6354
     {nf} shlq $1, %rN  -> leaq (%rN,%rN), %rN
6355
6356
     {nf} addl %eK, %eN, %eM  -> leal (%rN,%rK), %eM
6357
     {nf} addq %rK, %rN, %rM  -> leaq (%rN,%rK), %rM
6358
6359
     {nf} shll $1, %eN, %eM  -> leal (%rN,%rN), %eM
6360
     {nf} shlq $1, %rN, %rM  -> leaq (%rN,%rN), %rM
6361
       */
6362
0
      i.tm.opcode_space = SPACE_BASE;
6363
0
      i.tm.base_opcode = 0x8d;
6364
0
      i.tm.extension_opcode = None;
6365
0
      i.tm.opcode_modifier.evex = 0;
6366
0
      i.tm.opcode_modifier.vexvvvv = 0;
6367
0
      if (i.imm_operands != 0)
6368
0
  i.index_reg = i.base_reg = i.op[1].regs;
6369
0
      else if (!is_index (i.op[0].regs)
6370
0
         || (i.op[1].regs->reg_num == 5
6371
0
       && i.op[0].regs->reg_num != 5))
6372
0
  {
6373
0
    i.base_reg = i.op[0].regs;
6374
0
    i.index_reg = i.op[1].regs;
6375
0
  }
6376
0
      else
6377
0
  {
6378
0
    i.base_reg = i.op[1].regs;
6379
0
    i.index_reg = i.op[0].regs;
6380
0
  }
6381
0
      if (i.types[1].bitfield.word)
6382
0
  {
6383
    /* NB: No similar adjustment is needed when operand size is 32-bit.  */
6384
0
    i.base_reg += 64;
6385
0
    i.index_reg += 64;
6386
0
  }
6387
0
      i.op[1].regs = i.op[i.operands - 1].regs;
6388
6389
0
      operand_type_set (&i.types[0], 0);
6390
0
      i.types[0].bitfield.baseindex = 1;
6391
0
      i.tm_types[0] = i.types[0];
6392
0
      i.op[0].disps = NULL;
6393
0
      i.flags[0] = Operand_Mem;
6394
6395
0
      i.operands = 2;
6396
0
      i.mem_operands = i.reg_operands = 1;
6397
0
      i.imm_operands = 0;
6398
0
      pp.has_nf = false;
6399
0
    }
6400
0
  else if (optimize_for_space
6401
0
     && pp.encoding != encoding_evex
6402
0
     && (i.tm.base_opcode == 0x80 || i.tm.base_opcode == 0x83)
6403
0
     && (i.tm.extension_opcode == 0
6404
0
         || (i.tm.extension_opcode == 5
6405
0
       && i.op[0].imms->X_op == O_constant
6406
       /* Subtraction of -0x80 will end up smaller only if neither
6407
          operand size nor REX/REX2 prefixes are needed.  */
6408
0
       && (i.op[0].imms->X_add_number != -0x80
6409
0
           || (i.types[1].bitfield.dword
6410
0
               && !(i.op[1].regs->reg_flags & RegRex)
6411
0
               && !(i.op[i.operands - 1].regs->reg_flags & RegRex)
6412
0
               && pp.encoding != encoding_egpr))))
6413
0
     && !i.mem_operands
6414
0
     && !i.types[1].bitfield.byte
6415
     /* 16-bit operand size has extra restrictions: If REX2 was needed,
6416
        no size reduction would be possible.  Plus 3-operand forms zero-
6417
        extend the result, which can't be expressed with LEA.  */
6418
0
     && (!i.types[1].bitfield.word
6419
0
         || (i.operands == 2 && pp.encoding != encoding_egpr))
6420
0
     && is_plausible_suffix (1))
6421
0
    {
6422
      /* Optimize: -Os:
6423
     {nf} addw $N, %M   -> leaw N(%rM), %M
6424
     {nf} addl $N, %eM  -> leal N(%rM), %eM
6425
     {nf} addq $N, %rM  -> leaq N(%rM), %rM
6426
6427
     {nf} subw $N, %M   -> leaw -N(%rM), %M
6428
     {nf} subl $N, %eM  -> leal -N(%rM), %eM
6429
     {nf} subq $N, %rM  -> leaq -N(%rM), %rM
6430
6431
     {nf} addl $N, %eK, %eM  -> leal N(%rK), %eM
6432
     {nf} addq $N, %rK, %rM  -> leaq N(%rK), %rM
6433
6434
     {nf} subl $N, %eK, %eM  -> leal -N(%rK), %eM
6435
     {nf} subq $N, %rK, %rM  -> leaq -N(%rK), %rM
6436
       */
6437
0
      i.tm.opcode_space = SPACE_BASE;
6438
0
      i.tm.base_opcode = 0x8d;
6439
0
      if (i.tm.extension_opcode == 5)
6440
0
  i.op[0].imms->X_add_number = -i.op[0].imms->X_add_number;
6441
0
      i.tm.extension_opcode = None;
6442
0
      i.tm.opcode_modifier.evex = 0;
6443
0
      i.tm.opcode_modifier.vexvvvv = 0;
6444
0
      i.base_reg = i.op[1].regs;
6445
0
      if (i.types[1].bitfield.word)
6446
0
  {
6447
    /* NB: No similar adjustment is needed when operand size is 32-bit.  */
6448
0
    i.base_reg += 64;
6449
0
  }
6450
0
      i.op[1].regs = i.op[i.operands - 1].regs;
6451
6452
0
      operand_type_set (&i.types[0], 0);
6453
0
      i.types[0].bitfield.baseindex = 1;
6454
0
      i.types[0].bitfield.disp32 = 1;
6455
0
      i.op[0].disps = i.op[0].imms;
6456
0
      i.flags[0] = Operand_Mem;
6457
0
      optimize_disp (&i.tm);
6458
0
      i.tm_types[0] = i.types[0];
6459
6460
0
      i.operands = 2;
6461
0
      i.disp_operands = i.mem_operands = i.reg_operands = 1;
6462
0
      i.imm_operands = 0;
6463
0
      pp.has_nf = false;
6464
0
    }
6465
0
  else if (i.tm.base_opcode == 0x6b
6466
0
     && !i.mem_operands
6467
0
     && pp.encoding != encoding_evex
6468
0
     && i.tm.mnem_off != MN_imulzu
6469
0
     && is_plausible_suffix (1)
6470
     /* %rsp can't be the index.  */
6471
0
     && is_index (i.op[1].regs)
6472
     /* There's no reduction in size for 16-bit forms requiring Disp8 and
6473
        REX2.  */
6474
0
     && (!optimize_for_space
6475
0
         || !i.types[1].bitfield.word
6476
0
         || i.op[1].regs->reg_num != 5
6477
0
         || pp.encoding != encoding_egpr)
6478
0
     && i.op[0].imms->X_op == O_constant
6479
0
     && (i.op[0].imms->X_add_number == 3
6480
0
         || i.op[0].imms->X_add_number == 5
6481
0
         || i.op[0].imms->X_add_number == 9))
6482
0
    {
6483
      /* Optimize: -O:
6484
        For n one of 3, 5, or 9
6485
     {nf} imulw $n, %N, %M    -> leaw (%rN,%rN,n-1), %M
6486
     {nf} imull $n, %eN, %eM  -> leal (%rN,%rN,n-1), %eM
6487
     {nf} imulq $n, %rN, %rM  -> leaq (%rN,%rN,n-1), %rM
6488
6489
     {nf} imulw $n, %N   -> leaw (%rN,%rN,s), %N
6490
     {nf} imull $n, %eN  -> leal (%rN,%rN,s), %eN
6491
     {nf} imulq $n, %rN  -> leaq (%rN,%rN,s), %rN
6492
       */
6493
0
      i.tm.opcode_space = SPACE_BASE;
6494
0
      i.tm.base_opcode = 0x8d;
6495
0
      i.tm.extension_opcode = None;
6496
0
      i.tm.opcode_modifier.evex = 0;
6497
0
      i.base_reg = i.op[1].regs;
6498
      /* NB: No similar adjustment is needed when operand size is 32 bits.  */
6499
0
      if (i.types[1].bitfield.word)
6500
0
  i.base_reg += 64;
6501
0
      i.index_reg = i.base_reg;
6502
0
      i.log2_scale_factor = i.op[0].imms->X_add_number == 9
6503
0
          ? 3 : i.op[0].imms->X_add_number >> 1;
6504
6505
0
      operand_type_set (&i.types[0], 0);
6506
0
      i.types[0].bitfield.baseindex = 1;
6507
0
      i.tm_types[0] = i.types[0];
6508
0
      i.op[0].disps = NULL;
6509
0
      i.flags[0] = Operand_Mem;
6510
6511
0
      copy_operand (1, i.operands - 1);
6512
6513
0
      i.operands = 2;
6514
0
      i.mem_operands = i.reg_operands = 1;
6515
0
      i.imm_operands = 0;
6516
0
      pp.has_nf = false;
6517
0
    }
6518
0
  else if (cpu_arch_isa_flags.bitfield.cpubmi2
6519
0
     && pp.encoding == encoding_default
6520
0
     && (i.operands > 2 || !i.mem_operands)
6521
0
     && (i.types[i.operands - 1].bitfield.dword
6522
0
         || i.types[i.operands - 1].bitfield.qword))
6523
0
    {
6524
0
      if (i.tm.base_opcode == 0xd2)
6525
0
  {
6526
    /* Optimize: -O:
6527
         <OP> one of sal, sar, shl, shr:
6528
         {nf} <OP> %cl, %rN       -> <OP>x %{e,r}cx, %rN, %rN (N < 16)
6529
         {nf} <OP> %cl, ..., %rN  -> <OP>x %{e,r}cx, ..., %rN (no eGPR used)
6530
     */
6531
0
    gas_assert (i.tm.extension_opcode & 4);
6532
0
    i.tm_types[0] = i.tm_types[i.operands - 1];
6533
    /* NB: i.op[0].regs specifying %cl is good enough.  */
6534
0
    i.types[0] = i.types[i.operands - 1];
6535
0
    if (i.operands == 2)
6536
0
      {
6537
0
        i.tm_types[0].bitfield.baseindex = 0;
6538
0
        i.tm_types[2] = i.tm_types[0];
6539
0
        i.op[2].regs = i.op[1].regs;
6540
0
        i.types[2] = i.types[1];
6541
0
        i.reg_operands = i.operands = 3;
6542
0
      }
6543
0
    pp.has_nf = false;
6544
0
    i.tm.opcode_modifier.w = 0;
6545
0
    i.tm.opcode_modifier.evex = 0;
6546
0
    i.tm.opcode_modifier.vex = VEX128;
6547
0
    i.tm.opcode_modifier.vexvvvv = VexVVVV_SRC2;
6548
0
    i.tm.opcode_space = SPACE_0F38;
6549
0
    i.tm.base_opcode = 0xf7;
6550
0
    i.tm.opcode_modifier.opcodeprefix
6551
0
      = !(i.tm.extension_opcode & 1)
6552
0
        ? PREFIX_0X66 /* shlx */
6553
0
        : i.tm.extension_opcode & 2
6554
0
    ? PREFIX_0XF3 /* sarx */
6555
0
    : PREFIX_0XF2 /* shrx */;
6556
0
    i.tm.extension_opcode = None;
6557
0
  }
6558
0
      else if (i.tm.base_opcode == 0xc0
6559
0
         && i.tm.extension_opcode <= 1
6560
0
         && i.op[0].imms->X_op == O_constant)
6561
0
  {
6562
    /* Optimize: -O:
6563
         {nf} rol $I, %rN       -> rorx $osz-I, %rN, %rN (I != osz-1, N < 16)
6564
         {nf} rol $I, ..., %rN  -> rorx $osz-I, ..., %rN (I != osz-1, no eGPR used)
6565
         {nf} ror $I, %rN       -> rorx $I, %rN, %rN (I != 1, N < 16)
6566
         {nf} ror $I, ..., %rN  -> rorx $I,..., %rN (I != 1, no eGPR used)
6567
       NB: rol -> ror transformation for I == osz-1 was already handled above.
6568
       NB2: ror with an immediate of 1 uses a different base opcode.
6569
     */
6570
0
    if (i.operands == 2)
6571
0
      {
6572
0
        copy_operand (2, 1);
6573
0
        i.tm_types[2].bitfield.baseindex = 0;
6574
0
        i.reg_operands = 2;
6575
0
        i.operands = 3;
6576
0
      }
6577
0
    pp.has_nf = false;
6578
0
    i.tm.opcode_modifier.w = 0;
6579
0
    i.tm.opcode_modifier.evex = 0;
6580
0
    i.tm.opcode_modifier.vex = VEX128;
6581
0
    i.tm.opcode_modifier.vexvvvv = 0;
6582
0
    i.tm.opcode_space = SPACE_0F3A;
6583
0
    i.tm.base_opcode = 0xf0;
6584
0
    i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF2;
6585
0
    if (!i.tm.extension_opcode)
6586
0
      i.op[0].imms->X_add_number =
6587
0
        (i.types[i.operands - 1].bitfield.byte
6588
0
         ? 8 : i.types[i.operands - 1].bitfield.word
6589
0
         ? 16 : 64 >> i.types[i.operands - 1].bitfield.dword)
6590
0
        - i.op[0].imms->X_add_number;
6591
0
    i.tm.extension_opcode = None;
6592
0
  }
6593
0
      else if (i.tm.base_opcode == 0xf6
6594
0
         && i.tm.extension_opcode == 4
6595
0
         && !i.mem_operands
6596
0
         && i.op[0].regs->reg_num == 2
6597
0
         && !(i.op[0].regs->reg_flags & RegRex) )
6598
0
  {
6599
    /* Optimize: -O:
6600
         {nf} mul %edx  -> mulx %eax, %eax, %edx
6601
         {nf} mul %rdx  -> mulx %rax, %rax, %rdx
6602
     */
6603
0
    i.tm_types[1] = i.tm_types[0];
6604
0
    i.tm_types[1].bitfield.baseindex = 0;
6605
0
    i.tm_types[2] = i.tm_types[1];
6606
0
    i.op[2].regs = i.op[0].regs;
6607
    /* NB: %eax is good enough also for 64-bit operand size.  */
6608
0
    i.op[1].regs = i.op[0].regs = reg_eax;
6609
0
    i.types[2] = i.types[1] = i.types[0];
6610
0
    i.reg_operands = i.operands = 3;
6611
6612
0
    pp.has_nf = false;
6613
0
    i.tm.opcode_modifier.w = 0;
6614
0
    i.tm.opcode_modifier.evex = 0;
6615
0
    i.tm.opcode_modifier.vex = VEX128;
6616
0
    i.tm.opcode_modifier.vexvvvv = VexVVVV_SRC1;
6617
0
    i.tm.opcode_space = SPACE_0F38;
6618
0
    i.tm.base_opcode = 0xf6;
6619
0
    i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF2;
6620
0
    i.tm.extension_opcode = None;
6621
0
  }
6622
0
    }
6623
0
}
6624
6625
static void
6626
s_noopt (int dummy ATTRIBUTE_UNUSED)
6627
137
{
6628
137
  if (!is_it_end_of_statement ())
6629
121
    as_warn (_("`.noopt' arguments ignored"));
6630
6631
137
  optimize = 0;
6632
137
  optimize_for_space = 0;
6633
6634
137
  ignore_rest_of_line ();
6635
137
}
6636
6637
/* Return non-zero for load instruction.  */
6638
6639
static int
6640
load_insn_p (void)
6641
0
{
6642
0
  unsigned int dest;
6643
0
  int any_vex_p = is_any_vex_encoding (&i.tm);
6644
0
  unsigned int base_opcode = i.tm.base_opcode | 1;
6645
6646
0
  if (!any_vex_p)
6647
0
    {
6648
      /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
6649
   bndcn, bndstx, bndldx, clflushopt, clwb, cldemote.  */
6650
0
      if (i.tm.opcode_modifier.operandconstraint == ANY_SIZE)
6651
0
  return 0;
6652
6653
      /* pop.   */
6654
0
      if (i.tm.mnem_off == MN_pop)
6655
0
  return 1;
6656
0
    }
6657
6658
0
  if (i.tm.opcode_space == SPACE_BASE)
6659
0
    {
6660
      /* popf, popa.   */
6661
0
      if (i.tm.base_opcode == 0x9d
6662
0
    || i.tm.base_opcode == 0x61)
6663
0
  return 1;
6664
6665
      /* movs, cmps, lods, scas.  */
6666
0
      if ((i.tm.base_opcode | 0xb) == 0xaf)
6667
0
  return 1;
6668
6669
      /* outs, xlatb.  */
6670
0
      if (base_opcode == 0x6f
6671
0
    || i.tm.base_opcode == 0xd7)
6672
0
  return 1;
6673
      /* NB: For AMD-specific insns with implicit memory operands,
6674
   they're intentionally not covered.  */
6675
0
    }
6676
6677
  /* No memory operand.  */
6678
0
  if (!i.mem_operands)
6679
0
    return 0;
6680
6681
0
  if (any_vex_p)
6682
0
    {
6683
0
      if (i.tm.mnem_off == MN_vldmxcsr)
6684
0
  return 1;
6685
0
    }
6686
0
  else if (i.tm.opcode_space == SPACE_BASE)
6687
0
    {
6688
      /* test, not, neg, mul, imul, div, idiv.  */
6689
0
      if (base_opcode == 0xf7 && i.tm.extension_opcode != 1)
6690
0
  return 1;
6691
6692
      /* inc, dec.  */
6693
0
      if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
6694
0
  return 1;
6695
6696
      /* add, or, adc, sbb, and, sub, xor, cmp.  */
6697
0
      if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
6698
0
  return 1;
6699
6700
      /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
6701
0
      if ((base_opcode == 0xc1 || (base_opcode | 2) == 0xd3)
6702
0
    && i.tm.extension_opcode != 6)
6703
0
  return 1;
6704
6705
      /* Check for x87 instructions.  */
6706
0
      if ((base_opcode | 6) == 0xdf)
6707
0
  {
6708
    /* Skip fst, fstp, fstenv, fstcw.  */
6709
0
    if (i.tm.base_opcode == 0xd9
6710
0
        && (i.tm.extension_opcode == 2
6711
0
      || i.tm.extension_opcode == 3
6712
0
      || i.tm.extension_opcode == 6
6713
0
      || i.tm.extension_opcode == 7))
6714
0
      return 0;
6715
6716
    /* Skip fisttp, fist, fistp, fstp.  */
6717
0
    if (i.tm.base_opcode == 0xdb
6718
0
        && (i.tm.extension_opcode == 1
6719
0
      || i.tm.extension_opcode == 2
6720
0
      || i.tm.extension_opcode == 3
6721
0
      || i.tm.extension_opcode == 7))
6722
0
      return 0;
6723
6724
    /* Skip fisttp, fst, fstp, fsave, fstsw.  */
6725
0
    if (i.tm.base_opcode == 0xdd
6726
0
        && (i.tm.extension_opcode == 1
6727
0
      || i.tm.extension_opcode == 2
6728
0
      || i.tm.extension_opcode == 3
6729
0
      || i.tm.extension_opcode == 6
6730
0
      || i.tm.extension_opcode == 7))
6731
0
      return 0;
6732
6733
    /* Skip fisttp, fist, fistp, fbstp, fistp.  */
6734
0
    if (i.tm.base_opcode == 0xdf
6735
0
        && (i.tm.extension_opcode == 1
6736
0
      || i.tm.extension_opcode == 2
6737
0
      || i.tm.extension_opcode == 3
6738
0
      || i.tm.extension_opcode == 6
6739
0
      || i.tm.extension_opcode == 7))
6740
0
      return 0;
6741
6742
0
    return 1;
6743
0
  }
6744
0
    }
6745
0
  else if (i.tm.opcode_space == SPACE_0F)
6746
0
    {
6747
      /* bt, bts, btr, btc.  */
6748
0
      if (i.tm.base_opcode == 0xba
6749
0
    && (i.tm.extension_opcode | 3) == 7)
6750
0
  return 1;
6751
6752
      /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld.  */
6753
0
      if (i.tm.base_opcode == 0xc7
6754
0
    && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
6755
0
    && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
6756
0
        || i.tm.extension_opcode == 6))
6757
0
  return 1;
6758
6759
      /* fxrstor, ldmxcsr, xrstor.  */
6760
0
      if (i.tm.base_opcode == 0xae
6761
0
    && (i.tm.extension_opcode == 1
6762
0
        || i.tm.extension_opcode == 2
6763
0
        || i.tm.extension_opcode == 5))
6764
0
  return 1;
6765
6766
      /* lgdt, lidt, lmsw.  */
6767
0
      if (i.tm.base_opcode == 0x01
6768
0
    && (i.tm.extension_opcode == 2
6769
0
        || i.tm.extension_opcode == 3
6770
0
        || i.tm.extension_opcode == 6))
6771
0
  return 1;
6772
0
    }
6773
6774
0
  dest = i.operands - 1;
6775
6776
  /* Check fake imm8 operand and 3 source operands.  */
6777
0
  if ((i.tm.opcode_modifier.immext
6778
0
       || i.reg_operands + i.mem_operands == 4)
6779
0
      && i.types[dest].bitfield.imm8)
6780
0
    dest--;
6781
6782
  /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg.  */
6783
0
  if (i.tm.opcode_space == SPACE_BASE
6784
0
      && ((base_opcode | 0x38) == 0x39
6785
0
    || (base_opcode | 2) == 0x87))
6786
0
    return 1;
6787
6788
0
  if (i.tm.mnem_off == MN_xadd)
6789
0
    return 1;
6790
6791
  /* Check for load instruction.  */
6792
0
  return (i.types[dest].bitfield.class != ClassNone
6793
0
    || i.types[dest].bitfield.instance == Accum);
6794
0
}
6795
6796
/* Output lfence, 0xfaee8, after instruction.  */
6797
6798
static void
6799
insert_lfence_after (void)
6800
3.38k
{
6801
3.38k
  if (lfence_after_load && load_insn_p ())
6802
0
    {
6803
      /* There are also two REP string instructions that require
6804
   special treatment. Specifically, the compare string (CMPS)
6805
   and scan string (SCAS) instructions set EFLAGS in a manner
6806
   that depends on the data being compared/scanned. When used
6807
   with a REP prefix, the number of iterations may therefore
6808
   vary depending on this data. If the data is a program secret
6809
   chosen by the adversary using an LVI method,
6810
   then this data-dependent behavior may leak some aspect
6811
   of the secret.  */
6812
0
      if (((i.tm.base_opcode | 0x9) == 0xaf)
6813
0
    && i.prefix[REP_PREFIX])
6814
0
  {
6815
0
      as_warn (_("`%s` changes flags which would affect control flow behavior"),
6816
0
         insn_name (&i.tm));
6817
0
  }
6818
0
      char *p = frag_more (3);
6819
0
      *p++ = 0xf;
6820
0
      *p++ = 0xae;
6821
0
      *p = 0xe8;
6822
0
    }
6823
3.38k
}
6824
6825
/* Output lfence, 0xfaee8, before instruction.  */
6826
6827
static void
6828
insert_lfence_before (const struct last_insn *last_insn)
6829
3.38k
{
6830
3.38k
  char *p;
6831
6832
3.38k
  if (i.tm.opcode_space != SPACE_BASE)
6833
296
    return;
6834
6835
3.09k
  if (i.tm.base_opcode == 0xff
6836
14
      && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
6837
7
    {
6838
      /* Insert lfence before indirect branch if needed.  */
6839
6840
7
      if (lfence_before_indirect_branch == lfence_branch_none)
6841
7
  return;
6842
6843
0
      if (i.operands != 1)
6844
0
  abort ();
6845
6846
0
      if (i.reg_operands == 1)
6847
0
  {
6848
    /* Indirect branch via register.  Don't insert lfence with
6849
       -mlfence-after-load=yes.  */
6850
0
    if (lfence_after_load
6851
0
        || lfence_before_indirect_branch == lfence_branch_memory)
6852
0
      return;
6853
0
  }
6854
0
      else if (i.mem_operands == 1
6855
0
         && lfence_before_indirect_branch != lfence_branch_register)
6856
0
  {
6857
0
    as_warn (_("indirect `%s` with memory operand should be avoided"),
6858
0
       insn_name (&i.tm));
6859
0
    return;
6860
0
  }
6861
0
      else
6862
0
  return;
6863
6864
0
      if (last_insn->kind != last_insn_other)
6865
0
  {
6866
0
    as_warn_where (last_insn->file, last_insn->line,
6867
0
       _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
6868
0
       last_insn->name, insn_name (&i.tm));
6869
0
    return;
6870
0
  }
6871
6872
0
      p = frag_more (3);
6873
0
      *p++ = 0xf;
6874
0
      *p++ = 0xae;
6875
0
      *p = 0xe8;
6876
0
      return;
6877
0
    }
6878
6879
  /* Output or/not/shl and lfence before near ret.  */
6880
3.08k
  if (lfence_before_ret != lfence_before_ret_none
6881
0
      && (i.tm.base_opcode | 1) == 0xc3)
6882
0
    {
6883
0
      if (last_insn->kind != last_insn_other)
6884
0
  {
6885
0
    as_warn_where (last_insn->file, last_insn->line,
6886
0
       _("`%s` skips -mlfence-before-ret on `%s`"),
6887
0
       last_insn->name, insn_name (&i.tm));
6888
0
    return;
6889
0
  }
6890
6891
      /* Near ret ingore operand size override under CPU64.  */
6892
0
      char prefix = flag_code == CODE_64BIT
6893
0
        ? 0x48
6894
0
        : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
6895
6896
0
      if (lfence_before_ret == lfence_before_ret_not)
6897
0
  {
6898
    /* not: 0xf71424, may add prefix
6899
       for operand size override or 64-bit code.  */
6900
0
    p = frag_more ((prefix ? 2 : 0) + 6 + 3);
6901
0
    if (prefix)
6902
0
      *p++ = prefix;
6903
0
    *p++ = 0xf7;
6904
0
    *p++ = 0x14;
6905
0
    *p++ = 0x24;
6906
0
    if (prefix)
6907
0
      *p++ = prefix;
6908
0
    *p++ = 0xf7;
6909
0
    *p++ = 0x14;
6910
0
    *p++ = 0x24;
6911
0
  }
6912
0
      else
6913
0
  {
6914
0
    p = frag_more ((prefix ? 1 : 0) + 4 + 3);
6915
0
    if (prefix)
6916
0
      *p++ = prefix;
6917
0
    if (lfence_before_ret == lfence_before_ret_or)
6918
0
      {
6919
        /* or: 0x830c2400, may add prefix
6920
     for operand size override or 64-bit code.  */
6921
0
        *p++ = 0x83;
6922
0
        *p++ = 0x0c;
6923
0
      }
6924
0
    else
6925
0
      {
6926
        /* shl: 0xc1242400, may add prefix
6927
     for operand size override or 64-bit code.  */
6928
0
        *p++ = 0xc1;
6929
0
        *p++ = 0x24;
6930
0
      }
6931
6932
0
    *p++ = 0x24;
6933
0
    *p++ = 0x0;
6934
0
  }
6935
6936
0
      *p++ = 0xf;
6937
0
      *p++ = 0xae;
6938
0
      *p = 0xe8;
6939
0
    }
6940
3.08k
}
6941
6942
/* Shared helper for md_assemble() and s_insn().  */
6943
static void init_globals (void)
6944
49.2k
{
6945
49.2k
  unsigned int j;
6946
6947
49.2k
  memset (&i, '\0', sizeof (i));
6948
49.2k
  i.rounding.type = rc_none;
6949
295k
  for (j = 0; j < MAX_OPERANDS; j++)
6950
246k
    i.reloc[j] = NO_RELOC;
6951
49.2k
  memset (disp_expressions, '\0', sizeof (disp_expressions));
6952
49.2k
  memset (im_expressions, '\0', sizeof (im_expressions));
6953
49.2k
  save_stack_p = save_stack;
6954
49.2k
}
6955
6956
/* Helper for md_assemble() to decide whether to prepare for a possible 2nd
6957
   parsing pass. Instead of introducing a rarely used new insn attribute this
6958
   utilizes a common pattern between affected templates. It is deemed
6959
   acceptable that this will lead to unnecessary pass 2 preparations in a
6960
   limited set of cases.  */
6961
static INLINE bool may_need_pass2 (const insn_template *t)
6962
12.2k
{
6963
12.2k
  return t->opcode_modifier.sse2avx
6964
   /* Note that all SSE2AVX templates have at least one operand.  */
6965
12.2k
   ? get_operand_types (t)[t->operands - 1].bitfield.class == RegSIMD
6966
12.2k
   : (t->opcode_space == SPACE_0F
6967
1.04k
      && (t->base_opcode | 1) == 0xbf)
6968
12.0k
     || (t->opcode_space == SPACE_BASE
6969
7.52k
         && t->base_opcode == 0x63)
6970
12.0k
     || (intel_syntax /* shld / shrd may mean suffixed shl / shr.  */
6971
10.1k
         && t->opcode_space == SPACE_MAP4
6972
2.50k
         && (t->base_opcode | 8) == 0x2c);
6973
12.2k
}
6974
6975
#ifdef OBJ_ELF
6976
static enum x86_tls_error_type
6977
x86_check_tls_relocation (enum bfd_reloc_code_real r_type)
6978
1
{
6979
1
  switch (r_type)
6980
1
    {
6981
0
    case BFD_RELOC_386_TLS_GOTDESC:
6982
      /* Check GDesc access model:
6983
6984
   leal x@tlsdesc(%ebx), %reg32 --> Memory reg must be %ebx and
6985
            SIB is not supported.
6986
       */
6987
0
      if (i.tm.mnem_off != MN_lea)
6988
0
  return x86_tls_error_insn;
6989
0
      if (i.index_reg)
6990
0
  return x86_tls_error_sib;
6991
0
      if (!i.base_reg)
6992
0
  return x86_tls_error_no_base_reg;
6993
0
      if (i.base_reg->reg_type.bitfield.instance != RegB)
6994
0
  return x86_tls_error_ebx;
6995
0
      if (!i.types[1].bitfield.dword)
6996
0
  return x86_tls_error_dest_32bit_reg_size;
6997
0
      break;
6998
6999
0
    case BFD_RELOC_386_TLS_GD:
7000
      /* Check GD access model:
7001
7002
   leal foo@tlsgd(,%ebx,1), %eax   --> Only this fixed format is supported.
7003
   leal foo@tlsgd(%reg32), %eax    --> Dest reg must be '%eax'
7004
               Memory reg can't be %eax.
7005
       */
7006
0
      if (i.tm.mnem_off != MN_lea)
7007
0
  return x86_tls_error_insn;
7008
0
      if (i.types[1].bitfield.instance != Accum)
7009
0
  return x86_tls_error_dest_eax;
7010
0
      if (!i.types[1].bitfield.dword)
7011
0
  return x86_tls_error_dest_32bit_reg_size;
7012
0
      if (i.index_reg)
7013
0
  {
7014
0
    if (i.base_reg)
7015
0
      return x86_tls_error_base_reg;
7016
0
    if (i.index_reg->reg_type.bitfield.instance != RegB)
7017
0
      return x86_tls_error_index_ebx;
7018
0
    if (i.log2_scale_factor)
7019
0
      return x86_tls_error_scale_factor;
7020
0
  }
7021
0
      else
7022
0
  {
7023
0
    if (!i.base_reg)
7024
0
      return x86_tls_error_no_base_reg;
7025
0
    if (i.base_reg->reg_type.bitfield.instance == Accum)
7026
0
      return x86_tls_error_eax;
7027
0
  }
7028
0
      break;
7029
7030
0
    case BFD_RELOC_386_TLS_LDM:
7031
      /*  Check LDM access model:
7032
7033
    leal foo@tlsldm(%reg32), %eax --> Dest reg must be '%eax'
7034
                    Memory reg can't be %eax and SIB
7035
              is not supported.
7036
       */
7037
0
      if (i.tm.mnem_off != MN_lea)
7038
0
  return x86_tls_error_insn;
7039
0
      if (i.index_reg)
7040
0
  return x86_tls_error_sib;
7041
0
      if (!i.base_reg)
7042
0
  return x86_tls_error_no_base_reg;
7043
0
      if (i.base_reg->reg_type.bitfield.instance == Accum)
7044
0
  return x86_tls_error_eax;
7045
0
      if (i.types[1].bitfield.instance != Accum)
7046
0
  return x86_tls_error_dest_eax;
7047
0
      if (!i.types[1].bitfield.dword)
7048
0
  return x86_tls_error_dest_32bit_reg_size;
7049
0
      break;
7050
7051
0
    case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
7052
      /* Check GOTPC32 TLSDESC access model:
7053
7054
   --- LP64 mode ---
7055
   leaq x@tlsdesc(%rip), %reg64 --> Memory reg must be %rip.
7056
7057
   --- X32 mode ---
7058
   rex/rex2 leal x@tlsdesc(%rip), %reg32 --> Memory reg must be %rip.
7059
7060
   In X32 mode, gas will add rex/rex2 for it later, no need to check
7061
   here.
7062
       */
7063
0
      if (i.tm.mnem_off != MN_lea)
7064
0
  return x86_tls_error_insn;
7065
0
      if (!i.base_reg)
7066
0
  return x86_tls_error_no_base_reg;
7067
0
      if (i.base_reg->reg_num != RegIP
7068
0
    || !i.base_reg->reg_type.bitfield.qword)
7069
0
  return x86_tls_error_rip;
7070
0
      if (x86_elf_abi == X86_64_ABI)
7071
0
  {
7072
0
    if (!i.types[1].bitfield.qword)
7073
0
      return x86_tls_error_dest_64bit_reg_size;
7074
0
  }
7075
0
      else if (!i.types[1].bitfield.dword
7076
0
         && !i.types[1].bitfield.qword)
7077
0
  return x86_tls_error_dest_32bit_or_64bit_reg_size;
7078
0
    break;
7079
7080
0
    case BFD_RELOC_X86_64_TLSGD:
7081
      /* Check GD access model:
7082
7083
   leaq foo@tlsgd(%rip), %rdi --> Only this fixed format is supported.
7084
       */
7085
0
    case BFD_RELOC_X86_64_TLSLD:
7086
      /* Check LD access model:
7087
7088
   leaq foo@tlsld(%rip), %rdi --> Only this fixed format is supported.
7089
       */
7090
0
      if (i.tm.mnem_off != MN_lea)
7091
0
  return x86_tls_error_insn;
7092
0
      if (!i.base_reg)
7093
0
  return x86_tls_error_no_base_reg;
7094
0
      if (i.base_reg->reg_num != RegIP
7095
0
    || !i.base_reg->reg_type.bitfield.qword)
7096
0
  return x86_tls_error_rip;
7097
0
      if (!i.types[1].bitfield.qword
7098
0
    || i.op[1].regs->reg_num != EDI_REG_NUM
7099
0
    || i.op[1].regs->reg_flags)
7100
0
  return x86_tls_error_dest_rdi;
7101
0
      break;
7102
7103
0
    case BFD_RELOC_386_TLS_GOTIE:
7104
      /* Check GOTIE access model:
7105
7106
   subl foo@gotntpoff(%reg1), %reg2
7107
   movl foo@gotntpoff(%reg1), %reg2
7108
   addl foo@gotntpoff(%reg1), %reg2
7109
7110
   Memory operand: SIB is not supported.
7111
       */
7112
0
    case BFD_RELOC_386_TLS_IE_32:
7113
      /* Check IE_32 access model:
7114
7115
   subl foo@gottpoff(%reg1), %reg2
7116
   movl foo@gottpoff(%reg1), %reg2
7117
   addl foo@gottpoff(%reg1), %reg2
7118
7119
   Memory operand: SIB is not supported.
7120
       */
7121
0
      if (i.tm.mnem_off != MN_sub
7122
0
    && i.tm.mnem_off != MN_add
7123
0
    && i.tm.mnem_off != MN_mov)
7124
0
  return x86_tls_error_insn;
7125
0
      if (i.imm_operands
7126
0
    || i.disp_operands != 1
7127
0
    || i.reg_operands != 1
7128
0
    || i.types[1].bitfield.class != Reg)
7129
0
  return x86_tls_error_opcode;
7130
0
      if (!i.base_reg)
7131
0
  return x86_tls_error_no_base_reg;
7132
0
      if (i.index_reg)
7133
0
  return x86_tls_error_sib;
7134
0
      if (!i.base_reg->reg_type.bitfield.dword)
7135
0
  return x86_tls_error_base_reg_size;
7136
0
      if (!i.types[1].bitfield.dword)
7137
0
  return x86_tls_error_dest_32bit_reg_size;
7138
0
      break;
7139
7140
0
    case BFD_RELOC_386_TLS_IE:
7141
      /* Check IE access model:
7142
7143
   movl foo@indntpoff, %reg32 --> Mod == 00 && r/m == 5
7144
   addl foo@indntpoff, %reg32 --> Mod == 00 && r/m == 5
7145
       */
7146
0
      if (i.tm.mnem_off != MN_add && i.tm.mnem_off != MN_mov)
7147
0
  return x86_tls_error_insn;
7148
0
      if (i.imm_operands
7149
0
    || i.disp_operands != 1
7150
0
    || i.reg_operands != 1
7151
0
    || i.types[1].bitfield.class != Reg)
7152
0
  return x86_tls_error_opcode;
7153
0
      if (i.base_reg || i.index_reg)
7154
0
  return x86_tls_error_require_no_base_index_reg;
7155
0
      if (!i.types[1].bitfield.dword)
7156
0
  return x86_tls_error_dest_32bit_reg_size;
7157
0
      break;
7158
7159
0
    case BFD_RELOC_X86_64_GOTTPOFF:
7160
      /* Check GOTTPOFF access model:
7161
7162
   mov foo@gottpoff(%rip), %reg --> Memory Reg must be %rip.
7163
   movrs foo@gottpoff(%rip), %reg --> Memory Reg must be %rip.
7164
   add foo@gottpoff(%rip), %reg --> Memory Reg must be %rip.
7165
   add %reg1, foo@gottpoff(%rip), %reg2 --> Memory Reg must be %rip.
7166
   add foo@gottpoff(%rip), %reg1, %reg2 --> Memory Reg must be %rip.
7167
       */
7168
0
      if (i.tm.mnem_off != MN_add && i.tm.mnem_off != MN_mov
7169
0
    && i.tm.mnem_off != MN_movrs)
7170
0
  return x86_tls_error_insn;
7171
0
      if (i.imm_operands
7172
0
    || i.disp_operands != 1
7173
0
    || i.types[i.operands - 1].bitfield.class != Reg)
7174
0
  return x86_tls_error_opcode;
7175
0
      if (!i.base_reg)
7176
0
  return x86_tls_error_no_base_reg;
7177
0
      if (i.base_reg->reg_num != RegIP
7178
0
    || !i.base_reg->reg_type.bitfield.qword)
7179
0
  return x86_tls_error_rip;
7180
0
      if (x86_elf_abi == X86_64_ABI)
7181
0
  {
7182
0
    if (!i.types[i.operands - 1].bitfield.qword)
7183
0
      return x86_tls_error_dest_64bit_reg_size;
7184
0
  }
7185
0
      else if (!i.types[i.operands - 1].bitfield.dword
7186
0
         && !i.types[i.operands - 1].bitfield.qword)
7187
0
  return x86_tls_error_dest_32bit_or_64bit_reg_size;
7188
0
      break;
7189
7190
0
    case BFD_RELOC_386_TLS_DESC_CALL:
7191
      /* Check GDesc access model:
7192
7193
   call *x@tlscall(%eax) --> Memory reg must be %eax and
7194
           SIB is not supported.
7195
       */
7196
0
    case BFD_RELOC_X86_64_TLSDESC_CALL:
7197
      /* Check GDesc access model:
7198
7199
   call *x@tlscall(%rax) <--- LP64 mode.
7200
   call *x@tlscall(%eax) <--- X32 mode.
7201
7202
   Only these fixed formats are supported.
7203
       */
7204
0
      if (i.tm.mnem_off != MN_call)
7205
0
  return x86_tls_error_insn;
7206
0
      if (i.index_reg)
7207
0
  return x86_tls_error_sib;
7208
0
      if (!i.base_reg)
7209
0
  return x86_tls_error_no_base_reg;
7210
0
      if (i.base_reg->reg_type.bitfield.instance != Accum)
7211
0
  return x86_tls_error_RegA;
7212
0
      break;
7213
7214
0
    case BFD_RELOC_NONE:
7215
      /* This isn't a relocation.  */
7216
0
      return x86_tls_error_continue;
7217
7218
1
    default:
7219
1
      break;
7220
1
    }
7221
7222
  /* This relocation is OK.  */
7223
1
  return x86_tls_error_none;
7224
1
}
7225
7226
static void
7227
x86_report_tls_error (enum x86_tls_error_type tls_error,
7228
          enum bfd_reloc_code_real r_type)
7229
0
{
7230
0
  unsigned int k;
7231
0
  for (k = 0; k < ARRAY_SIZE (gotrel); k++)
7232
0
    if (gotrel[k].rel[object_64bit] == r_type)
7233
0
      break;
7234
7235
0
  switch (tls_error)
7236
0
    {
7237
0
    case x86_tls_error_insn:
7238
0
      as_bad (_("@%s operator cannot be used with `%s'"),
7239
0
        gotrel[k].str, insn_name (&i.tm));
7240
0
      return;
7241
7242
0
    case x86_tls_error_opcode:
7243
0
      as_bad (_("@%s operator can be used with `%s', but format is wrong"),
7244
0
        gotrel[k].str, insn_name (&i.tm));
7245
0
      return;
7246
7247
0
    case x86_tls_error_sib:
7248
0
      as_bad (_("@%s operator requires no SIB"), gotrel[k].str);
7249
0
      return;
7250
7251
0
    case x86_tls_error_no_base_reg:
7252
0
      as_bad (_("@%s operator requires base register"), gotrel[k].str);
7253
0
      return;
7254
7255
0
    case x86_tls_error_require_no_base_index_reg:
7256
0
      as_bad (_("@%s operator requires no base/index register"),
7257
0
        gotrel[k].str);
7258
0
      return;
7259
7260
0
    case x86_tls_error_base_reg:
7261
0
      as_bad (_("@%s operator requires no base register"), gotrel[k].str);
7262
0
      return;
7263
7264
0
    case x86_tls_error_index_ebx:
7265
0
      as_bad (_("@%s operator requires `%sebx' as index register"),
7266
0
        gotrel[k].str, register_prefix);
7267
0
      return;
7268
7269
0
    case x86_tls_error_eax:
7270
0
      as_bad (_("@%s operator requires `%seax' as base register"),
7271
0
        gotrel[k].str, register_prefix);
7272
0
      return;
7273
7274
0
    case x86_tls_error_RegA:
7275
0
      as_bad (_("@%s operator requires `%seax/%srax' as base register"),
7276
0
        gotrel[k].str, register_prefix, register_prefix);
7277
0
      return;
7278
7279
0
    case x86_tls_error_ebx:
7280
0
      as_bad (_("@%s operator requires `%sebx' as base register"),
7281
0
        gotrel[k].str, register_prefix);
7282
0
      return;
7283
7284
0
    case x86_tls_error_rip:
7285
0
      as_bad (_("@%s operator requires `%srip' as base register"),
7286
0
        gotrel[k].str, register_prefix);
7287
0
      return;
7288
7289
0
    case x86_tls_error_dest_eax:
7290
0
      as_bad (_("@%s operator requires `%seax' as dest register"),
7291
0
        gotrel[k].str, register_prefix);
7292
0
      return;
7293
7294
0
    case x86_tls_error_dest_rdi:
7295
0
      as_bad (_("@%s operator requires `%srdi' as dest register"),
7296
0
        gotrel[k].str, register_prefix);
7297
0
      return;
7298
7299
0
    case x86_tls_error_scale_factor:
7300
0
      as_bad (_("@%s operator requires scale factor of 1"),
7301
0
        gotrel[k].str);
7302
0
      return;
7303
7304
0
    case x86_tls_error_base_reg_size:
7305
0
      as_bad (_("@%s operator requires 32-bit base register"),
7306
0
        gotrel[k].str);
7307
0
      return;
7308
7309
0
    case x86_tls_error_dest_32bit_reg_size:
7310
0
      as_bad (_("@%s operator requires 32-bit dest register"),
7311
0
        gotrel[k].str);
7312
0
      return;
7313
7314
0
    case x86_tls_error_dest_64bit_reg_size:
7315
0
      as_bad (_("@%s operator requires 64-bit dest register"),
7316
0
        gotrel[k].str);
7317
0
      return;
7318
7319
0
    case x86_tls_error_dest_32bit_or_64bit_reg_size:
7320
0
      as_bad (_("@%s operator requires 32-bit or 64-bit dest register"),
7321
0
        gotrel[k].str);
7322
0
      return;
7323
7324
0
    default:
7325
0
      abort ();
7326
0
    }
7327
0
}
7328
#endif
7329
7330
/* This is the guts of the machine-dependent assembler.  LINE points to a
7331
   machine dependent instruction.  This function is supposed to emit
7332
   the frags/bytes it assembles to.  */
7333
7334
static void
7335
i386_assemble (char *line)
7336
41.6k
{
7337
41.6k
  unsigned int j;
7338
41.6k
  char mnemonic[MAX_MNEM_SIZE], mnem_suffix = 0, *copy = NULL;
7339
41.6k
  char *xstrdup_copy = NULL;
7340
41.6k
  const char *end, *pass1_mnem = NULL;
7341
41.6k
  enum i386_error pass1_err = 0;
7342
41.6k
  struct pseudo_prefixes orig_pp = pp;
7343
41.6k
  const insn_template *t;
7344
41.6k
  struct last_insn *last_insn
7345
41.6k
    = &seg_info(now_seg)->tc_segment_info_data.last_insn;
7346
7347
  /* Initialize globals.  */
7348
41.6k
  current_templates.end = current_templates.start = NULL;
7349
41.7k
 retry:
7350
41.7k
  init_globals ();
7351
7352
  /* Suppress optimization when the last thing we saw may not have been
7353
     a proper instruction (e.g. a stand-alone prefix or .byte).  */
7354
41.7k
  if (last_insn->kind != last_insn_other)
7355
11.2k
    pp.no_optimize = true;
7356
7357
  /* First parse an instruction mnemonic & call i386_operand for the operands.
7358
     We assume that the scrubber has arranged it so that line[0] is the valid
7359
     start of a (possibly prefixed) mnemonic.  */
7360
7361
41.7k
  end = parse_insn (line, mnemonic, parse_all);
7362
41.7k
  if (end == NULL)
7363
29.4k
    {
7364
29.4k
      if (pass1_mnem != NULL)
7365
36
  goto match_error;
7366
29.4k
      if (i.error != no_error)
7367
40
  {
7368
40
    gas_assert (current_templates.start != NULL);
7369
40
    if (may_need_pass2 (current_templates.start) && !i.suffix)
7370
2
      goto no_match;
7371
    /* No point in trying a 2nd pass - it'll only find the same suffix
7372
       again.  */
7373
38
    mnem_suffix = i.suffix;
7374
38
    goto match_error;
7375
40
  }
7376
29.4k
      return;
7377
29.4k
    }
7378
12.2k
  t = current_templates.start;
7379
  /* NB: LINE may be change to be the same as XSTRDUP_COPY.  */
7380
12.2k
  if (xstrdup_copy != line && may_need_pass2 (t))
7381
210
    {
7382
      /* Make a copy of the full line in case we need to retry.  */
7383
210
      xstrdup_copy = xstrdup (line);
7384
210
      copy = xstrdup_copy;
7385
210
    }
7386
12.2k
  line += end - line;
7387
12.2k
  mnem_suffix = i.suffix;
7388
7389
12.2k
  line = parse_operands (line, mnemonic);
7390
12.2k
  this_operand = -1;
7391
12.2k
  if (line == NULL)
7392
7.50k
    {
7393
7.50k
      free (xstrdup_copy);
7394
7.50k
      return;
7395
7.50k
    }
7396
7397
  /* Now we've parsed the mnemonic into a set of templates, and have the
7398
     operands at hand.  */
7399
7400
  /* All Intel opcodes have reversed operands except for "bound", "enter",
7401
     "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
7402
     "rmpadjust", "rmpquery", "rmpopt", "rmpchkd", and deprecated forms of
7403
     "rmpupdate".
7404
     We also don't reverse intersegment "jmp" and "call" instructions with
7405
     2 immediate operands so that the immediate segment precedes the offset
7406
     consistently in Intel and AT&T modes.  */
7407
4.78k
  if (intel_syntax
7408
3.70k
      && i.operands > 1
7409
1.17k
      && (t->mnem_off != MN_bound)
7410
1.17k
      && !startswith (mnemonic, "invlpg")
7411
1.17k
      && !startswith (mnemonic, "monitor")
7412
1.17k
      && !startswith (mnemonic, "mwait")
7413
1.17k
      && (t->mnem_off != MN_pvalidate)
7414
1.17k
      && (!startswith (mnemonic, "rmp") || i.mem_operands)
7415
1.17k
      && (t->mnem_off != MN_tpause)
7416
1.17k
      && (t->mnem_off != MN_umwait)
7417
1.17k
      && !(i.operands == 2
7418
1.16k
     && operand_type_check (i.types[0], imm)
7419
10
     && operand_type_check (i.types[1], imm)))
7420
1.16k
    swap_operands ();
7421
7422
  /* The order of the immediates should be reversed for 2-immediates EXTRQ
7423
     and INSERTQ instructions.  Also OUT, UWRMSR, and WRMSRNS want their
7424
     immediate to be in the "canonical" place (first), despite it appearing
7425
     last (in AT&T syntax, or because of the swapping above) in the incoming
7426
     set of operands.  */
7427
4.78k
  if ((i.imm_operands == 2
7428
13
       && (t->mnem_off == MN_extrq || t->mnem_off == MN_insertq))
7429
4.78k
      || ((t->mnem_off == MN_out || t->mnem_off == MN_uwrmsr
7430
4.78k
     || t->mnem_off == MN_wrmsrns)
7431
0
    && i.imm_operands && i.operands > i.imm_operands))
7432
0
      swap_2_operands (0, 1);
7433
7434
  /* All legitimate immediates are placed first now.  Others, if any, will be
7435
     rejected by match_template() anyway.  */
7436
4.78k
  if (operand_type_check (i.types[0], imm))
7437
1.97k
    {
7438
      /* For USER_MSR and MSR_IMM instructions, imm32 stands for the name of a
7439
   model specific register (MSR). That's an unsigned quantity, whereas all
7440
   other insns with 32-bit immediate and 64-bit operand size use
7441
   sign-extended immediates (imm32s). Therefore these insns are
7442
   special-cased, bypassing the normal handling of immediates here.  */
7443
1.97k
      if (is_cpu(current_templates.start, CpuUSER_MSR)
7444
1.97k
    || t->mnem_off == MN_rdmsr
7445
1.96k
    || t->mnem_off == MN_wrmsrns)
7446
12
  i.types[0] = smallest_imm_type (i.op[0].imms->X_add_number);
7447
1.96k
      else
7448
1.96k
  optimize_imm ();
7449
1.97k
    }
7450
7451
4.78k
  if (i.disp_operands && !optimize_disp (t))
7452
0
    return;
7453
7454
  /* Next, we find a template that matches the given insn,
7455
     making sure the overlap of the given operands types is consistent
7456
     with the template operand types.  */
7457
7458
4.78k
  if (!(t = match_template (mnem_suffix)))
7459
1.33k
    {
7460
1.33k
      const char *err_msg;
7461
7462
1.33k
      if (copy && !mnem_suffix)
7463
142
  {
7464
142
    line = copy;
7465
142
    copy = NULL;
7466
144
  no_match:
7467
144
    pass1_err = i.error;
7468
144
    pass1_mnem = insn_name (current_templates.start);
7469
144
    pp = orig_pp;
7470
144
    goto retry;
7471
142
  }
7472
7473
      /* If a non-/only-64bit template (group) was found in pass 1, and if
7474
   _some_ template (group) was found in pass 2, squash pass 1's
7475
   error.  */
7476
1.19k
      if (pass1_err == unsupported_64bit)
7477
0
  pass1_mnem = NULL;
7478
7479
1.27k
  match_error:
7480
1.27k
      free (xstrdup_copy);
7481
7482
1.27k
      switch (pass1_mnem ? pass1_err : i.error)
7483
1.27k
  {
7484
0
  default:
7485
0
    abort ();
7486
5
  case operand_size_mismatch:
7487
5
    err_msg = _("operand size mismatch");
7488
5
    break;
7489
729
  case operand_type_mismatch:
7490
729
    err_msg = _("operand type mismatch");
7491
729
    break;
7492
0
  case register_type_mismatch:
7493
0
    err_msg = _("register type mismatch");
7494
0
    break;
7495
440
  case number_of_operands_mismatch:
7496
440
    err_msg = _("number of operands mismatch");
7497
440
    break;
7498
2
  case invalid_instruction_suffix:
7499
2
    err_msg = _("invalid instruction suffix");
7500
2
    break;
7501
0
  case bad_imm4:
7502
0
    err_msg = _("constant doesn't fit in 4 bits");
7503
0
    break;
7504
0
  case unsupported_with_intel_mnemonic:
7505
0
    err_msg = _("unsupported with Intel mnemonic");
7506
0
    break;
7507
5
  case unsupported_syntax:
7508
5
    err_msg = _("unsupported syntax");
7509
5
    break;
7510
0
  case unsupported_EGPR_for_addressing:
7511
0
    err_msg = _("extended GPR cannot be used as base/index");
7512
0
    break;
7513
0
  case unsupported_nf:
7514
0
    err_msg = _("{nf} unsupported");
7515
0
    break;
7516
5
  case unsupported:
7517
5
    as_bad (_("unsupported instruction `%s'"),
7518
5
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
7519
5
    return;
7520
25
  case unsupported_on_arch:
7521
25
    as_bad (_("`%s' is not supported on `%s%s'"),
7522
25
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start),
7523
25
      cpu_arch_name ? cpu_arch_name : default_arch,
7524
25
      cpu_sub_arch_name ? cpu_sub_arch_name : "");
7525
25
    return;
7526
15
  case unsupported_64bit:
7527
15
    if (ISLOWER (mnem_suffix))
7528
0
      {
7529
0
        if (flag_code == CODE_64BIT)
7530
0
    as_bad (_("`%s%c' is not supported in 64-bit mode"),
7531
0
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start),
7532
0
      mnem_suffix);
7533
0
        else
7534
0
    as_bad (_("`%s%c' is only supported in 64-bit mode"),
7535
0
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start),
7536
0
      mnem_suffix);
7537
0
      }
7538
15
    else
7539
15
      {
7540
15
        if (flag_code == CODE_64BIT)
7541
13
    as_bad (_("`%s' is not supported in 64-bit mode"),
7542
13
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
7543
2
        else
7544
2
    as_bad (_("`%s' is only supported in 64-bit mode"),
7545
2
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
7546
15
      }
7547
15
    return;
7548
32
  case no_vex_encoding:
7549
32
    err_msg = _("no VEX/XOP encoding");
7550
32
    break;
7551
13
  case no_evex_encoding:
7552
13
    err_msg = _("no EVEX encoding");
7553
13
    break;
7554
0
  case invalid_sib_address:
7555
0
    err_msg = _("invalid SIB address");
7556
0
    break;
7557
0
  case invalid_vsib_address:
7558
0
    err_msg = _("invalid VSIB address");
7559
0
    break;
7560
0
  case invalid_vector_register_set:
7561
0
    err_msg = _("mask, index, and destination registers must be distinct");
7562
0
    break;
7563
0
  case invalid_tmm_register_set:
7564
0
    err_msg = _("all tmm registers must be distinct");
7565
0
    break;
7566
0
  case invalid_dest_and_src_register_set:
7567
0
    err_msg = _("destination and source registers must be distinct");
7568
0
    break;
7569
0
  case invalid_dest_register_set:
7570
0
    err_msg = _("two dest registers must be distinct");
7571
0
    break;
7572
0
  case invalid_pseudo_prefix:
7573
0
    err_msg = _("rex2 pseudo prefix cannot be used");
7574
0
    break;
7575
0
  case unsupported_vector_index_register:
7576
0
    err_msg = _("unsupported vector index register");
7577
0
    break;
7578
0
  case unsupported_broadcast:
7579
0
    err_msg = _("unsupported broadcast");
7580
0
    break;
7581
0
  case broadcast_needed:
7582
0
    err_msg = _("broadcast is needed for operand of such type");
7583
0
    break;
7584
0
  case unsupported_masking:
7585
0
    err_msg = _("unsupported masking");
7586
0
    break;
7587
0
  case mask_not_on_destination:
7588
0
    err_msg = _("mask not on destination operand");
7589
0
    break;
7590
0
  case no_default_mask:
7591
0
    err_msg = _("default mask isn't allowed");
7592
0
    break;
7593
0
  case unsupported_rc_sae:
7594
0
    err_msg = _("unsupported static rounding/sae");
7595
0
    break;
7596
0
  case unsupported_vector_size:
7597
0
    as_bad (_("vector size above %u required for `%s'"), 128u << vector_size,
7598
0
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
7599
0
    return;
7600
0
  case unsupported_rsp_register:
7601
0
    err_msg = _("'rsp' register cannot be used");
7602
0
    break;
7603
0
  case internal_error:
7604
0
    err_msg = _("internal error");
7605
0
    break;
7606
1.27k
  }
7607
1.22k
      as_bad (_("%s for `%s'"), err_msg,
7608
1.22k
        pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
7609
1.22k
      return;
7610
1.27k
    }
7611
7612
3.44k
  free (xstrdup_copy);
7613
7614
3.44k
  const i386_operand_type *t_types = get_operand_types (t);
7615
7616
3.44k
  if (sse_check != check_none
7617
      /* The opcode space check isn't strictly needed; it's there only to
7618
   bypass the logic below when easily possible.  */
7619
0
      && t->opcode_space >= SPACE_0F
7620
0
      && t->opcode_space <= SPACE_0F3A
7621
0
      && !is_cpu (&i.tm, CpuSSE4a)
7622
0
      && !is_any_vex_encoding (t))
7623
0
    {
7624
      /* Some KL and all WideKL insns have only implicit %xmm operands.  */
7625
0
      bool simd = is_cpu (t, CpuKL) || is_cpu (t, CpuWideKL);
7626
7627
0
      for (j = 0; j < t->operands; ++j)
7628
0
  {
7629
0
    if (t_types[j].bitfield.class == RegMMX)
7630
0
      break;
7631
0
    if (t_types[j].bitfield.class == RegSIMD)
7632
0
      simd = true;
7633
0
  }
7634
7635
0
      if (j >= t->operands && simd)
7636
0
  (sse_check == check_warning
7637
0
   ? as_warn
7638
0
   : as_bad) (_("SSE instruction `%s' is used"), insn_name (&i.tm));
7639
0
    }
7640
7641
3.44k
  if (i.tm.opcode_modifier.fwait)
7642
0
    if (!add_prefix (FWAIT_OPCODE))
7643
0
      return;
7644
7645
  /* Check if REP prefix is OK.  */
7646
3.44k
  if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep
7647
0
      && (i.prefix[REP_PREFIX] != REPE_PREFIX_OPCODE
7648
0
    || i.tm.opcode_modifier.prefixok != PrefixRepe))
7649
0
    {
7650
0
      as_bad (_("invalid instruction `%s' after `%s'"),
7651
0
    insn_name (&i.tm), i.rep_prefix);
7652
0
      return;
7653
0
    }
7654
7655
  /* Check for lock without a lockable instruction.  Destination operand
7656
     must be memory unless it is xchg (0x86).  */
7657
3.44k
  if (i.prefix[LOCK_PREFIX])
7658
0
    {
7659
0
      if (i.tm.opcode_modifier.prefixok < PrefixLock
7660
0
    || i.mem_operands == 0
7661
0
    || (i.tm.base_opcode != 0x86
7662
0
        && !(i.flags[i.operands - 1] & Operand_Mem)))
7663
0
  {
7664
0
    as_bad (_("expecting lockable instruction after `lock'"));
7665
0
    return;
7666
0
  }
7667
7668
      /* Zap the redundant prefix from XCHG when optimizing.  */
7669
0
      if (i.tm.base_opcode == 0x86 && optimize && !pp.no_optimize)
7670
0
  i.prefix[LOCK_PREFIX] = 0;
7671
0
    }
7672
7673
3.44k
#ifdef OBJ_ELF
7674
3.44k
  if (i.has_gotrel && tls_check)
7675
1
    {
7676
1
      enum x86_tls_error_type tls_error;
7677
1
      for (j = 0; j < i.operands; ++j)
7678
1
  {
7679
1
    tls_error = x86_check_tls_relocation (i.reloc[j]);
7680
1
    if (tls_error == x86_tls_error_continue)
7681
0
      continue;
7682
7683
1
    if (tls_error != x86_tls_error_none)
7684
0
      x86_report_tls_error (tls_error, i.reloc[j]);
7685
1
    break;
7686
1
  }
7687
1
    }
7688
3.44k
#endif
7689
7690
3.44k
  if ((is_any_vex_encoding (&i.tm) && i.tm.opcode_space != SPACE_MAP4)
7691
3.44k
      || i.tm_types[i.imm_operands].bitfield.class >= RegMMX
7692
3.41k
      || i.tm_types[i.imm_operands + 1].bitfield.class >= RegMMX
7693
3.41k
      || is_padlock(&i.tm))
7694
30
    {
7695
      /* Check for data size prefix on VEX/XOP/EVEX encoded, SIMD, and
7696
   PadLock insns.  */
7697
30
      if (i.prefix[DATA_PREFIX])
7698
0
  {
7699
0
    as_bad (_("data size prefix invalid with `%s'"), insn_name (&i.tm));
7700
0
    return;
7701
0
  }
7702
30
    }
7703
7704
  /* Check if HLE prefix is OK.  */
7705
3.44k
  if (i.hle_prefix && !check_hle ())
7706
0
    return;
7707
7708
  /* Check BND prefix.  */
7709
3.44k
  if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
7710
0
    as_bad (_("expecting valid branch instruction after `bnd'"));
7711
7712
  /* Check NOTRACK prefix.  */
7713
3.44k
  if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
7714
0
    as_bad (_("expecting indirect branch instruction after `notrack'"));
7715
7716
3.44k
  if (is_cpu (&i.tm, CpuMPX))
7717
0
    {
7718
0
      if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
7719
0
  as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
7720
0
      else if (flag_code != CODE_16BIT
7721
0
         ? i.prefix[ADDR_PREFIX]
7722
0
         : i.mem_operands && !i.prefix[ADDR_PREFIX])
7723
0
  as_bad (_("16-bit address isn't allowed in MPX instructions"));
7724
0
    }
7725
7726
  /* Insert BND prefix.  */
7727
3.44k
  if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
7728
0
    {
7729
0
      if (!i.prefix[BND_PREFIX])
7730
0
  add_prefix (BND_PREFIX_OPCODE);
7731
0
      else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
7732
0
  {
7733
0
    as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
7734
0
    i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
7735
0
  }
7736
0
    }
7737
7738
  /* Check string instruction segment overrides.  */
7739
3.44k
  if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
7740
22
    {
7741
22
      gas_assert (i.mem_operands);
7742
22
      if (!check_string ())
7743
0
  return;
7744
22
      i.disp_operands = 0;
7745
22
    }
7746
7747
  /* The memory operand of (%dx) should be only used with input/output
7748
     instructions (base opcodes: 0x6c, 0x6e, 0xec, 0xee).  */
7749
3.44k
  if (i.input_output_operand
7750
0
      && ((i.tm.base_opcode | 0x82) != 0xee
7751
0
    || i.tm.opcode_space != SPACE_BASE))
7752
0
    {
7753
0
      as_bad (_("input/output port address isn't allowed with `%s'"),
7754
0
        insn_name (&i.tm));
7755
0
      return;
7756
0
    }
7757
7758
3.44k
  if (optimize && !pp.no_optimize && i.tm.opcode_modifier.optimize)
7759
0
    {
7760
0
      if (pp.has_nf)
7761
0
  optimize_nf_encoding ();
7762
0
      optimize_encoding ();
7763
0
    }
7764
7765
  /* Past optimization there's no need to distinguish encoding_evex,
7766
     encoding_evex512, and encoding_egpr anymore.  */
7767
3.44k
  if (pp.encoding == encoding_evex512)
7768
0
    pp.encoding = encoding_evex;
7769
3.44k
  else if (pp.encoding == encoding_egpr)
7770
3
    pp.encoding = is_any_vex_encoding (&i.tm) ? encoding_evex
7771
3
               : encoding_default;
7772
7773
  /* Similarly {nf} can now be taken to imply {evex}.  */
7774
3.44k
  if (pp.has_nf && pp.encoding == encoding_default)
7775
0
    pp.encoding = encoding_evex;
7776
7777
3.44k
  if (use_unaligned_vector_move)
7778
0
    encode_with_unaligned_vector_move ();
7779
7780
3.44k
  if (!process_suffix (t))
7781
61
    return;
7782
7783
  /* Check if IP-relative addressing requirements can be satisfied.  */
7784
3.38k
  if (is_cpu (&i.tm, CpuPREFETCHI)
7785
0
      && !(i.base_reg && i.base_reg->reg_num == RegIP))
7786
0
    as_warn (_("'%s' only supports RIP-relative address"), insn_name (&i.tm));
7787
7788
  /* Update operand types and check extended states.  */
7789
8.46k
  for (j = 0; j < i.operands; j++)
7790
5.07k
    {
7791
5.07k
      enum operand_class class = i.types[j].bitfield.class;
7792
7793
5.07k
      i.types[j] = operand_type_and (i.types[j], i.tm_types[j]);
7794
5.07k
      switch (i.tm_types[j].bitfield.class)
7795
5.07k
  {
7796
2.95k
  default:
7797
2.95k
    break;
7798
2.95k
  case RegMMX:
7799
0
    i.xstate |= xstate_mmx;
7800
0
    break;
7801
0
  case RegMask:
7802
0
    i.xstate |= xstate_mask;
7803
0
    break;
7804
54
  case RegSIMD:
7805
54
    if (i.tm_types[j].bitfield.tmmword)
7806
0
      i.xstate |= xstate_tmm;
7807
54
    else if (i.tm_types[j].bitfield.zmmword
7808
0
       && !i.tm.opcode_modifier.vex
7809
0
       && vector_size >= VSZ512)
7810
0
      i.xstate |= xstate_zmm;
7811
54
    else if (i.tm_types[j].bitfield.ymmword
7812
0
       && vector_size >= VSZ256)
7813
0
      i.xstate |= xstate_ymm;
7814
54
    else if (i.tm_types[j].bitfield.xmmword)
7815
54
      i.xstate |= xstate_xmm;
7816
54
    break;
7817
2.07k
  case ClassNone:
7818
2.07k
    i.types[j].bitfield.class = class;
7819
2.07k
    break;
7820
5.07k
  }
7821
5.07k
    }
7822
7823
  /* Make still unresolved immediate matches conform to size of immediate
7824
     given in i.suffix.  */
7825
3.38k
  if (!finalize_imm ())
7826
0
    return;
7827
7828
3.38k
  if (i.types[0].bitfield.imm1)
7829
0
    i.imm_operands = 0; /* kludge for shift insns.  */
7830
7831
  /* For insns with operands there are more diddles to do to the opcode.  */
7832
3.38k
  if (i.operands)
7833
3.27k
    {
7834
3.27k
      if (!process_operands ())
7835
0
  return;
7836
3.27k
    }
7837
115
  else if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
7838
0
    {
7839
      /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc.  */
7840
0
      as_warn (_("translating to `%sp'"), insn_name (&i.tm));
7841
0
    }
7842
7843
3.38k
  if (is_any_vex_encoding (&i.tm))
7844
4
    {
7845
4
      if (!cpu_arch_flags.bitfield.cpui286)
7846
0
  {
7847
0
    as_bad (_("instruction `%s' isn't supported outside of protected mode."),
7848
0
      insn_name (&i.tm));
7849
0
    return;
7850
0
  }
7851
7852
      /* Check for explicit REX prefix.  */
7853
4
      if ((i.prefix[REX_PREFIX]
7854
0
     && (i.tm.opcode_space != SPACE_MAP4
7855
         /* To mimic behavior for legacy insns, permit use of REX64 for promoted
7856
      legacy instructions.  */
7857
0
         || i.prefix[REX_PREFIX] != (REX_OPCODE | REX_W)))
7858
4
    || pp.rex_encoding)
7859
0
  {
7860
0
    as_bad (_("REX prefix invalid with `%s'"), insn_name (&i.tm));
7861
0
    return;
7862
0
  }
7863
7864
      /* Check for explicit REX2 prefix.  */
7865
4
      if (pp.rex2_encoding)
7866
0
  {
7867
0
    as_bad (_("{rex2} prefix invalid with `%s'"), insn_name (&i.tm));
7868
0
    return;
7869
0
  }
7870
7871
4
      if (is_apx_evex_encoding ())
7872
1
  {
7873
1
    if (!build_apx_evex_prefix (false))
7874
0
      return;
7875
1
  }
7876
3
      else if (i.tm.opcode_modifier.vex)
7877
3
  build_vex_prefix (t);
7878
0
      else
7879
0
  build_evex_prefix ();
7880
7881
      /* The individual REX.RXBW bits got consumed.  */
7882
4
      i.rex &= REX_OPCODE;
7883
7884
      /* The rex2 bits got consumed.  */
7885
4
      i.rex2 = 0;
7886
4
    }
7887
7888
  /* Handle conversion of 'int $3' --> special int3 insn.  */
7889
3.38k
  if (i.tm.mnem_off == MN_int
7890
1
      && i.op[0].imms->X_add_number == 3)
7891
0
    {
7892
0
      i.tm.base_opcode = INT3_OPCODE;
7893
0
      i.imm_operands = 0;
7894
0
    }
7895
7896
3.38k
  if ((i.tm.opcode_modifier.jump == JUMP
7897
3.36k
       || i.tm.opcode_modifier.jump == JUMP_BYTE
7898
3.36k
       || i.tm.opcode_modifier.jump == JUMP_DWORD)
7899
59
      && i.op[0].disps->X_op == O_constant)
7900
18
    {
7901
      /* Convert "jmp constant" (and "call constant") to a jump (call) to
7902
   the absolute address given by the constant.  Since ix86 jumps and
7903
   calls are pc relative, we need to generate a reloc.  */
7904
18
      i.op[0].disps->X_add_symbol = &abs_symbol;
7905
18
      i.op[0].disps->X_op = O_symbol;
7906
18
    }
7907
7908
3.38k
  establish_rex ();
7909
7910
3.38k
  insert_lfence_before (last_insn);
7911
7912
  /* We are ready to output the insn.  */
7913
3.38k
  output_insn (last_insn);
7914
7915
3.38k
#ifdef OBJ_ELF
7916
  /* PS: SCFI is enabled only for System V AMD64 ABI.  The ABI check has been
7917
     performed in i386_target_format.  */
7918
3.38k
  if (flag_synth_cfi)
7919
0
    {
7920
0
      ginsnS *ginsn;
7921
0
      ginsn = x86_ginsn_new (symbol_temp_new_now (), frch_ginsn_gen_mode ());
7922
0
      frch_ginsn_data_append (ginsn);
7923
0
    }
7924
3.38k
#endif
7925
7926
3.38k
  insert_lfence_after ();
7927
7928
3.38k
  if (i.tm.opcode_modifier.isprefix)
7929
28
    {
7930
28
      last_insn->kind = last_insn_prefix;
7931
28
      last_insn->name = insn_name (&i.tm);
7932
28
      last_insn->file = as_where (&last_insn->line);
7933
28
    }
7934
3.36k
  else
7935
3.36k
    last_insn->kind = last_insn_other;
7936
3.38k
}
7937
7938
void
7939
md_assemble (char *line)
7940
41.6k
{
7941
41.6k
  i386_assemble (line);
7942
41.6k
  current_templates.start = NULL;
7943
41.6k
  memset (&pp, 0, sizeof (pp));
7944
41.6k
}
7945
7946
/* The Q suffix is generally valid only in 64-bit mode, with very few
7947
   exceptions: fild, fistp, fisttp, and cmpxchg8b.  Note that for fild
7948
   and fisttp only one of their two templates is matched below: That's
7949
   sufficient since other relevant attributes are the same between both
7950
   respective templates.  */
7951
static INLINE bool q_suffix_allowed(const insn_template *t)
7952
120
{
7953
120
  return flag_code == CODE_64BIT
7954
0
   || (t->opcode_space == SPACE_BASE
7955
0
       && t->base_opcode == 0xdf
7956
0
       && (t->extension_opcode & 1)) /* fild / fistp / fisttp */
7957
0
   || t->mnem_off == MN_cmpxchg8b;
7958
120
}
7959
7960
static const char *
7961
parse_insn (const char *line, char *mnemonic, enum parse_mode mode)
7962
51.1k
{
7963
51.1k
  const char *l = line, *token_start = l;
7964
51.1k
  char *mnem_p;
7965
51.1k
  bool pass1 = !current_templates.start;
7966
51.1k
  int supported;
7967
51.1k
  const insn_template *t;
7968
51.1k
  char *dot_p = NULL;
7969
7970
53.1k
  while (1)
7971
53.1k
    {
7972
53.1k
      const char *split;
7973
7974
53.1k
      mnem_p = mnemonic;
7975
      /* Pseudo-prefixes start with an opening figure brace.  */
7976
53.1k
      if ((*mnem_p = *l) == '{')
7977
1.89k
  {
7978
1.89k
    ++mnem_p;
7979
1.89k
    ++l;
7980
1.89k
    if (is_whitespace (*l))
7981
3
      ++l;
7982
1.89k
  }
7983
51.2k
      else if (mode == parse_pseudo_prefix)
7984
1.79k
  break;
7985
185k
      while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
7986
134k
  {
7987
134k
    if (*mnem_p == '.')
7988
5.32k
      dot_p = mnem_p;
7989
134k
    mnem_p++;
7990
134k
    if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
7991
172
      {
7992
175
      too_long:
7993
175
        as_bad (_("no such instruction: `%s'"), token_start);
7994
175
        return NULL;
7995
172
      }
7996
134k
    l++;
7997
134k
  }
7998
51.1k
      split = l;
7999
51.1k
      if (is_whitespace (*l))
8000
21.8k
  ++l;
8001
      /* Pseudo-prefixes end with a closing figure brace.  */
8002
51.1k
      if (*mnemonic == '{' && *l == '}')
8003
1.81k
  {
8004
1.81k
    *mnem_p++ = *l++;
8005
1.81k
    if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
8006
3
      goto too_long;
8007
1.81k
    *mnem_p = '\0';
8008
8009
1.81k
    if (is_whitespace (*l))
8010
1.32k
      ++l;
8011
1.81k
  }
8012
49.3k
      else if (l == split
8013
27.5k
         && *l != END_OF_INSN
8014
24.9k
         && (intel_syntax
8015
3.13k
       || (*l != PREFIX_SEPARATOR && *l != ',')))
8016
24.8k
  {
8017
24.8k
    if (mode != parse_all)
8018
4.78k
      break;
8019
20.1k
    as_bad (_("invalid character %s in mnemonic"),
8020
20.1k
      output_invalid (*split));
8021
20.1k
    return NULL;
8022
24.8k
  }
8023
26.2k
      if (token_start == l)
8024
8
  {
8025
8
    if (!intel_syntax && *l == PREFIX_SEPARATOR)
8026
0
      as_bad (_("expecting prefix; got nothing"));
8027
8
    else
8028
8
      as_bad (_("expecting mnemonic; got nothing"));
8029
8
    return NULL;
8030
8
  }
8031
8032
      /* Look up instruction (or prefix) via hash table.  */
8033
26.2k
      op_lookup (mnemonic);
8034
8035
26.2k
      if (*l != END_OF_INSN
8036
23.6k
    && current_templates.start
8037
13.5k
    && current_templates.start->opcode_modifier.isprefix)
8038
2.00k
  {
8039
2.00k
    supported = cpu_flags_match (current_templates.start);
8040
2.00k
    if (!(supported & CPU_FLAGS_64BIT_MATCH))
8041
2
      {
8042
2
        as_bad ((flag_code != CODE_64BIT
8043
2
           ? _("`%s' is only supported in 64-bit mode")
8044
2
           : _("`%s' is not supported in 64-bit mode")),
8045
2
          insn_name (current_templates.start));
8046
2
        return NULL;
8047
2
      }
8048
2.00k
    if (supported != CPU_FLAGS_PERFECT_MATCH)
8049
11
      {
8050
11
        as_bad (_("`%s' is not supported on `%s%s'"),
8051
11
          insn_name (current_templates.start),
8052
11
          cpu_arch_name ? cpu_arch_name : default_arch,
8053
11
          cpu_sub_arch_name ? cpu_sub_arch_name : "");
8054
11
        return NULL;
8055
11
      }
8056
    /* If we are in 16-bit mode, do not allow addr16 or data16.
8057
       Similarly, in 32-bit mode, do not allow addr32 or data32.  */
8058
1.99k
    if ((current_templates.start->opcode_modifier.size == SIZE16
8059
1.92k
         || current_templates.start->opcode_modifier.size == SIZE32)
8060
75
        && flag_code != CODE_64BIT
8061
4
        && ((current_templates.start->opcode_modifier.size == SIZE32)
8062
4
      ^ (flag_code == CODE_16BIT)))
8063
4
      {
8064
4
        as_bad (_("redundant %s prefix"),
8065
4
          insn_name (current_templates.start));
8066
4
        return NULL;
8067
4
      }
8068
8069
1.99k
    if (current_templates.start->base_opcode == PSEUDO_PREFIX)
8070
1.79k
      {
8071
        /* Handle pseudo prefixes.  */
8072
1.79k
        switch (current_templates.start->extension_opcode)
8073
1.79k
    {
8074
0
    case Prefix_Disp8:
8075
      /* {disp8} */
8076
0
      pp.disp_encoding = disp_encoding_8bit;
8077
0
      break;
8078
0
    case Prefix_Disp16:
8079
      /* {disp16} */
8080
0
      pp.disp_encoding = disp_encoding_16bit;
8081
0
      break;
8082
6
    case Prefix_Disp32:
8083
      /* {disp32} */
8084
6
      pp.disp_encoding = disp_encoding_32bit;
8085
6
      break;
8086
0
    case Prefix_Load:
8087
      /* {load} */
8088
0
      pp.dir_encoding = dir_encoding_load;
8089
0
      break;
8090
0
    case Prefix_Store:
8091
      /* {store} */
8092
0
      pp.dir_encoding = dir_encoding_store;
8093
0
      break;
8094
41
    case Prefix_VEX:
8095
      /* {vex} */
8096
41
      pp.encoding = encoding_vex;
8097
41
      break;
8098
612
    case Prefix_VEX3:
8099
      /* {vex3} */
8100
612
      pp.encoding = encoding_vex3;
8101
612
      break;
8102
1.12k
    case Prefix_EVEX:
8103
      /* {evex} */
8104
1.12k
      pp.encoding = encoding_evex;
8105
1.12k
      break;
8106
12
    case Prefix_REX:
8107
      /* {rex} */
8108
12
      pp.rex_encoding = true;
8109
12
      break;
8110
0
    case Prefix_REX2:
8111
      /* {rex2} */
8112
0
      pp.rex2_encoding = true;
8113
0
      break;
8114
0
    case Prefix_NF:
8115
      /* {nf} */
8116
0
      pp.has_nf = true;
8117
0
      break;
8118
0
    case Prefix_NoOptimize:
8119
      /* {nooptimize} */
8120
0
      pp.no_optimize = true;
8121
0
      break;
8122
0
    case Prefix_NoImm8s:
8123
      /* {noimm8s} */
8124
0
      pp.no_imm8s = true;
8125
0
      break;
8126
0
    default:
8127
0
      abort ();
8128
1.79k
    }
8129
1.79k
        if (pp.has_nf
8130
0
      && pp.encoding != encoding_default
8131
0
      && pp.encoding != encoding_evex)
8132
0
    {
8133
0
      as_bad (_("{nf} cannot be combined with {vex}/{vex3}"));
8134
0
      return NULL;
8135
0
    }
8136
1.79k
      }
8137
200
    else
8138
200
      {
8139
        /* Add prefix, checking for repeated prefixes.  */
8140
200
        switch (add_prefix (current_templates.start->base_opcode))
8141
200
    {
8142
5
    case PREFIX_EXIST:
8143
5
      return NULL;
8144
1
    case PREFIX_DS:
8145
1
      if (is_cpu (current_templates.start, CpuIBT))
8146
0
        i.notrack_prefix = insn_name (current_templates.start);
8147
1
      break;
8148
19
    case PREFIX_REP:
8149
19
      if (is_cpu (current_templates.start, CpuHLE))
8150
0
        i.hle_prefix = insn_name (current_templates.start);
8151
19
      else if (is_cpu (current_templates.start, CpuMPX))
8152
0
        i.bnd_prefix = insn_name (current_templates.start);
8153
19
      else
8154
19
        i.rep_prefix = insn_name (current_templates.start);
8155
19
      break;
8156
175
    default:
8157
175
      break;
8158
200
    }
8159
200
      }
8160
    /* Skip past PREFIX_SEPARATOR and reset token_start.  */
8161
1.98k
    l += (!intel_syntax && *l == PREFIX_SEPARATOR);
8162
1.98k
    if (is_whitespace (*l))
8163
561
      ++l;
8164
1.98k
    token_start = l;
8165
1.98k
  }
8166
24.2k
      else
8167
24.2k
  break;
8168
26.2k
    }
8169
8170
30.8k
  if (mode != parse_all)
8171
9.37k
    return token_start;
8172
8173
21.4k
  if (!current_templates.start)
8174
9.71k
    {
8175
#ifdef TE_SOLARIS
8176
      /* Sun specifies an alternative form for CMOVcc: Size suffix (if any)
8177
   first, then a dot, then the condition code mnemonic.  */
8178
      if ((mnemonic + 4 == dot_p && !memcmp (mnemonic, "cmov", 4))
8179
    /* While doc doesn't say so, gcc assumes it: Same for FCMOVcc,
8180
       except that there's no size suffix to care about.  */
8181
    || (mnemonic + 5 == dot_p && !memcmp (mnemonic, "fcmov", 5)))
8182
  {
8183
    /* Simply strip the dot.  */
8184
    memmove (dot_p, dot_p + 1, mnem_p - dot_p);
8185
    dot_p = mnem_p - 1;
8186
  }
8187
      else if (!intel_syntax
8188
         && mnemonic + 5 == dot_p
8189
         && !memcmp (mnemonic, "cmov", 4)
8190
         && strchr ("lqw", TOLOWER (dot_p[-1])))
8191
  {
8192
    /* Strip the dot, while moving the suffix.  */
8193
    char suffix = dot_p[-1];
8194
8195
    memmove (dot_p - 1, dot_p + 1, mnem_p - dot_p);
8196
    mnem_p[-2] = suffix;
8197
    dot_p = mnem_p - 1;
8198
  }
8199
      else
8200
#endif
8201
      /* Deprecated functionality (new code should use pseudo-prefixes instead):
8202
   Check if we should swap operand or force 32bit displacement in
8203
   encoding.  */
8204
9.71k
      if (mnem_p - 2 == dot_p && dot_p[1] == 's')
8205
1
  {
8206
1
    if (pp.dir_encoding == dir_encoding_default)
8207
1
      pp.dir_encoding = dir_encoding_swap;
8208
0
    else
8209
0
      as_warn (_("ignoring `.s' suffix due to earlier `{%s}'"),
8210
0
         pp.dir_encoding == dir_encoding_load ? "load" : "store");
8211
1
  }
8212
9.71k
      else if (mnem_p - 3 == dot_p
8213
974
         && dot_p[1] == 'd'
8214
969
         && dot_p[2] == '8')
8215
0
  {
8216
0
    if (pp.disp_encoding == disp_encoding_default)
8217
0
      pp.disp_encoding = disp_encoding_8bit;
8218
0
    else if (pp.disp_encoding != disp_encoding_8bit)
8219
0
      as_warn (_("ignoring `.d8' suffix due to earlier `{disp<N>}'"));
8220
0
  }
8221
9.71k
      else if (mnem_p - 4 == dot_p
8222
4
         && dot_p[1] == 'd'
8223
0
         && dot_p[2] == '3'
8224
0
         && dot_p[3] == '2')
8225
0
  {
8226
0
    if (pp.disp_encoding == disp_encoding_default)
8227
0
      pp.disp_encoding = disp_encoding_32bit;
8228
0
    else if (pp.disp_encoding != disp_encoding_32bit)
8229
0
      as_warn (_("ignoring `.d32' suffix due to earlier `{disp<N>}'"));
8230
0
  }
8231
9.71k
      else
8232
9.71k
  goto check_suffix;
8233
1
      mnem_p = dot_p;
8234
1
      *dot_p = '\0';
8235
1
      op_lookup (mnemonic);
8236
1
    }
8237
8238
11.7k
  if (!current_templates.start || !pass1)
8239
145
    {
8240
145
      current_templates.start = NULL;
8241
8242
9.85k
    check_suffix:
8243
9.85k
      if (mnem_p > mnemonic)
8244
9.85k
  {
8245
    /* See if we can get a match by trimming off a suffix.  */
8246
9.85k
    switch (mnem_p[-1])
8247
9.85k
      {
8248
74
      case WORD_MNEM_SUFFIX:
8249
74
        if (intel_syntax && (intel_float_operand (mnemonic) & 2))
8250
0
    i.suffix = SHORT_MNEM_SUFFIX;
8251
74
        else
8252
    /* Fall through.  */
8253
616
        case BYTE_MNEM_SUFFIX:
8254
683
        case QWORD_MNEM_SUFFIX:
8255
683
    i.suffix = mnem_p[-1];
8256
683
        mnem_p[-1] = '\0';
8257
683
        op_lookup (mnemonic);
8258
683
        break;
8259
133
      case SHORT_MNEM_SUFFIX:
8260
1.22k
      case LONG_MNEM_SUFFIX:
8261
1.22k
        if (!intel_syntax)
8262
174
    {
8263
174
      i.suffix = mnem_p[-1];
8264
174
      mnem_p[-1] = '\0';
8265
174
      op_lookup (mnemonic);
8266
174
    }
8267
1.22k
        break;
8268
8269
        /* Intel Syntax.  */
8270
2.66k
      case 'd':
8271
2.66k
        if (intel_syntax)
8272
2.63k
    {
8273
2.63k
      if (intel_float_operand (mnemonic) == 1)
8274
0
        i.suffix = SHORT_MNEM_SUFFIX;
8275
2.63k
      else
8276
2.63k
        i.suffix = LONG_MNEM_SUFFIX;
8277
2.63k
      mnem_p[-1] = '\0';
8278
2.63k
      op_lookup (mnemonic);
8279
2.63k
    }
8280
        /* For compatibility reasons accept MOVSD and CMPSD without
8281
           operands even in AT&T mode.  */
8282
31
        else if (*l == END_OF_INSN)
8283
31
    {
8284
31
      mnem_p[-1] = '\0';
8285
31
      op_lookup (mnemonic);
8286
31
      if (current_templates.start != NULL
8287
          /* MOVS or CMPS */
8288
0
          && (current_templates.start->base_opcode | 2) == 0xa6
8289
0
          && current_templates.start->opcode_space
8290
0
       == SPACE_BASE
8291
0
          && mnem_p[-2] == 's')
8292
0
        {
8293
0
          as_warn (_("found `%sd'; assuming `%sl' was meant"),
8294
0
             mnemonic, mnemonic);
8295
0
          i.suffix = LONG_MNEM_SUFFIX;
8296
0
        }
8297
31
      else
8298
31
        {
8299
31
          current_templates.start = NULL;
8300
31
          mnem_p[-1] = 'd';
8301
31
        }
8302
31
    }
8303
2.66k
        break;
8304
9.85k
      }
8305
9.85k
  }
8306
8307
9.85k
      if (!current_templates.start)
8308
9.10k
  {
8309
9.10k
    if (pass1)
8310
9.07k
      as_bad (_("no such instruction: `%s'"), token_start);
8311
9.10k
    return NULL;
8312
9.10k
  }
8313
9.85k
    }
8314
8315
  /* Handle SCC OSZC flgs.  */
8316
12.3k
  if (current_templates.start->opcode_modifier.operandconstraint == SCC)
8317
34
    {
8318
34
      int length = check_Scc_OszcOperations (l);
8319
34
      if (length < 0)
8320
26
  return NULL;
8321
8
      l += length;
8322
8
    }
8323
8324
12.3k
  if ((current_templates.start->opcode_modifier.jump == JUMP
8325
11.0k
       || current_templates.start->opcode_modifier.jump == JUMP_BYTE)
8326
1.30k
      && *l == ',')
8327
5
    {
8328
      /* Check for a branch hint.  We allow ",pt" and ",pn" for
8329
   predict taken and predict not taken respectively.
8330
   I'm not sure that branch hints actually do anything on loop
8331
   and jcxz insns (JumpByte) for current Pentium4 chips.  They
8332
   may work in the future and it doesn't hurt to accept them
8333
   now.  */
8334
5
      token_start = l++;
8335
5
      if (is_whitespace (*l))
8336
0
  ++l;
8337
5
      if (TOLOWER (*l) == 'p' && ISALPHA (l[1])
8338
2
    && (l[2] == END_OF_INSN || is_whitespace (l[2])))
8339
2
  {
8340
2
    if (TOLOWER (l[1]) == 't')
8341
2
      {
8342
2
        if (!add_prefix (DS_PREFIX_OPCODE))
8343
0
    return NULL;
8344
2
        l += 2;
8345
2
      }
8346
0
    else if (TOLOWER (l[1]) == 'n')
8347
0
      {
8348
0
        if (!add_prefix (CS_PREFIX_OPCODE))
8349
0
    return NULL;
8350
0
        l += 2;
8351
0
      }
8352
0
    else
8353
0
      l = token_start;
8354
2
  }
8355
3
      else
8356
3
  l = token_start;
8357
5
    }
8358
  /* Any other comma loses.  */
8359
12.3k
  if (*l == ',')
8360
3
    {
8361
3
      as_bad (_("invalid character %s in mnemonic"),
8362
3
        output_invalid (*l));
8363
3
      return NULL;
8364
3
    }
8365
8366
  /* Check if instruction is supported on specified architecture.  */
8367
12.3k
  supported = 0;
8368
36.6k
  for (t = current_templates.start; t < current_templates.end; ++t)
8369
36.6k
    {
8370
36.6k
      supported |= cpu_flags_match (t);
8371
8372
36.6k
      if (i.suffix == QWORD_MNEM_SUFFIX && !q_suffix_allowed (t))
8373
0
  supported &= ~CPU_FLAGS_64BIT_MATCH;
8374
8375
36.6k
      if (supported == CPU_FLAGS_PERFECT_MATCH)
8376
12.2k
  return l;
8377
36.6k
    }
8378
8379
40
  if (pass1)
8380
40
    {
8381
40
      if (supported & CPU_FLAGS_64BIT_MATCH)
8382
25
        i.error = unsupported_on_arch;
8383
15
      else
8384
15
        i.error = unsupported_64bit;
8385
40
    }
8386
8387
40
  return NULL;
8388
12.3k
}
8389
8390
static char *
8391
parse_operands (char *l, const char *mnemonic)
8392
18.0k
{
8393
18.0k
  char *token_start;
8394
8395
  /* 1 if operand is pending after ','.  */
8396
18.0k
  unsigned int expecting_operand = 0;
8397
8398
32.4k
  while (*l != END_OF_INSN)
8399
26.8k
    {
8400
      /* Non-zero if operand parens not balanced.  */
8401
26.8k
      unsigned int paren_not_balanced = 0;
8402
      /* True if inside double quotes.  */
8403
26.8k
      bool in_quotes = false;
8404
8405
      /* Skip optional white space before operand.  */
8406
26.8k
      if (is_whitespace (*l))
8407
1.13k
  ++l;
8408
26.8k
      if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
8409
75
  {
8410
75
    as_bad (_("invalid character %s before operand %d"),
8411
75
      output_invalid (*l),
8412
75
      i.operands + 1);
8413
75
    return NULL;
8414
75
  }
8415
26.7k
      token_start = l;  /* After white space.  */
8416
312k
      while (in_quotes || paren_not_balanced || *l != ',')
8417
302k
  {
8418
302k
    if (*l == END_OF_INSN)
8419
11.5k
      {
8420
11.5k
        if (in_quotes)
8421
29
    {
8422
29
      as_bad (_("unbalanced double quotes in operand %d."),
8423
29
        i.operands + 1);
8424
29
      return NULL;
8425
29
    }
8426
11.5k
        if (paren_not_balanced)
8427
134
    {
8428
134
      know (!intel_syntax);
8429
134
      as_bad (_("unbalanced parenthesis in operand %d."),
8430
134
        i.operands + 1);
8431
134
      return NULL;
8432
134
    }
8433
11.4k
        else
8434
11.4k
    break; /* we are done */
8435
11.5k
      }
8436
291k
    else if (*l == '\\' && l[1] == '"')
8437
0
      ++l;
8438
291k
    else if (*l == '"')
8439
2.51k
      in_quotes = !in_quotes;
8440
288k
    else if (!in_quotes && !is_operand_char (*l) && !is_whitespace (*l))
8441
5.06k
      {
8442
5.06k
        as_bad (_("invalid character %s in operand %d"),
8443
5.06k
          output_invalid (*l),
8444
5.06k
          i.operands + 1);
8445
5.06k
        return NULL;
8446
5.06k
      }
8447
285k
    if (!intel_syntax && !in_quotes)
8448
32.5k
      {
8449
32.5k
        if (*l == '(')
8450
409
    ++paren_not_balanced;
8451
32.5k
        if (*l == ')')
8452
284
    --paren_not_balanced;
8453
32.5k
      }
8454
285k
    l++;
8455
285k
  }
8456
21.5k
      if (l != token_start)
8457
21.2k
  {     /* Yes, we've read in another operand.  */
8458
21.2k
    unsigned int operand_ok;
8459
21.2k
    this_operand = i.operands++;
8460
21.2k
    if (i.operands > MAX_OPERANDS)
8461
0
      {
8462
0
        as_bad (_("spurious operands; (%d operands/instruction max)"),
8463
0
          MAX_OPERANDS);
8464
0
        return NULL;
8465
0
      }
8466
21.2k
    i.types[this_operand].bitfield.unspecified = 1;
8467
    /* Now parse operand adding info to 'i' as we go along.  */
8468
21.2k
    END_STRING_AND_SAVE (l);
8469
8470
21.2k
    if (i.mem_operands > 1)
8471
173
      {
8472
173
        as_bad (_("too many memory references for `%s'"),
8473
173
          mnemonic);
8474
173
        return 0;
8475
173
      }
8476
8477
21.0k
    if (intel_syntax)
8478
17.5k
      operand_ok =
8479
17.5k
        i386_intel_operand (token_start,
8480
17.5k
          intel_float_operand (mnemonic));
8481
3.47k
    else
8482
3.47k
      operand_ok = i386_att_operand (token_start);
8483
8484
21.0k
    RESTORE_END_STRING (l);
8485
21.0k
    if (!operand_ok)
8486
6.64k
      return NULL;
8487
21.0k
  }
8488
269
      else
8489
269
  {
8490
269
    if (expecting_operand)
8491
268
      {
8492
273
      expecting_operand_after_comma:
8493
273
        as_bad (_("expecting operand after ','; got nothing"));
8494
273
        return NULL;
8495
268
      }
8496
1
    if (*l == ',')
8497
1
      {
8498
1
        as_bad (_("expecting operand before ','; got nothing"));
8499
1
        return NULL;
8500
1
      }
8501
1
  }
8502
8503
      /* Now *l must be either ',' or END_OF_INSN.  */
8504
14.4k
      if (*l == ',')
8505
9.00k
  {
8506
9.00k
    if (*++l == END_OF_INSN)
8507
5
      {
8508
        /* Just skip it, if it's \n complain.  */
8509
5
        goto expecting_operand_after_comma;
8510
5
      }
8511
8.99k
    expecting_operand = 1;
8512
8.99k
  }
8513
14.4k
    }
8514
5.61k
  return l;
8515
18.0k
}
8516
8517
static void
8518
copy_operand (unsigned int to, unsigned int from)
8519
0
{
8520
0
  i.types[to] = i.types[from];
8521
0
  i.tm_types[to] = i.tm_types[from];
8522
0
  i.flags[to] = i.flags[from];
8523
0
  i.op[to] = i.op[from];
8524
0
  i.reloc[to] = i.reloc[from];
8525
0
  i.imm_bits[to] = i.imm_bits[from];
8526
  /* Note: i.mask and i.broadcast aren't handled here, as what (if
8527
     anything) to do there depends on context.  */
8528
0
}
8529
8530
static void
8531
swap_2_operands (unsigned int xchg1, unsigned int xchg2)
8532
1.48k
{
8533
1.48k
  union i386_op temp_op;
8534
1.48k
  i386_operand_type temp_type;
8535
1.48k
  unsigned int temp_flags;
8536
1.48k
  enum bfd_reloc_code_real temp_reloc;
8537
8538
1.48k
  temp_type = i.types[xchg2];
8539
1.48k
  i.types[xchg2] = i.types[xchg1];
8540
1.48k
  i.types[xchg1] = temp_type;
8541
8542
1.48k
  temp_flags = i.flags[xchg2];
8543
1.48k
  i.flags[xchg2] = i.flags[xchg1];
8544
1.48k
  i.flags[xchg1] = temp_flags;
8545
8546
1.48k
  temp_op = i.op[xchg2];
8547
1.48k
  i.op[xchg2] = i.op[xchg1];
8548
1.48k
  i.op[xchg1] = temp_op;
8549
8550
1.48k
  temp_reloc = i.reloc[xchg2];
8551
1.48k
  i.reloc[xchg2] = i.reloc[xchg1];
8552
1.48k
  i.reloc[xchg1] = temp_reloc;
8553
8554
1.48k
  temp_flags = i.imm_bits[xchg2];
8555
1.48k
  i.imm_bits[xchg2] = i.imm_bits[xchg1];
8556
1.48k
  i.imm_bits[xchg1] = temp_flags;
8557
8558
1.48k
  if (i.mask.reg)
8559
0
    {
8560
0
      if (i.mask.operand == xchg1)
8561
0
  i.mask.operand = xchg2;
8562
0
      else if (i.mask.operand == xchg2)
8563
0
  i.mask.operand = xchg1;
8564
0
    }
8565
1.48k
  if (i.broadcast.type || i.broadcast.bytes)
8566
82
    {
8567
82
      if (i.broadcast.operand == xchg1)
8568
12
  i.broadcast.operand = xchg2;
8569
70
      else if (i.broadcast.operand == xchg2)
8570
28
  i.broadcast.operand = xchg1;
8571
82
    }
8572
1.48k
}
8573
8574
static void
8575
swap_operands (void)
8576
1.16k
{
8577
1.16k
  switch (i.operands)
8578
1.16k
    {
8579
0
    case 5:
8580
0
    case 4:
8581
0
      swap_2_operands (1, i.operands - 2);
8582
      /* Fall through.  */
8583
12
    case 3:
8584
1.16k
    case 2:
8585
1.16k
      swap_2_operands (0, i.operands - 1);
8586
1.16k
      break;
8587
0
    default:
8588
0
      abort ();
8589
1.16k
    }
8590
8591
1.16k
  if (i.mem_operands == 2)
8592
1
    {
8593
1
      const reg_entry *temp_seg;
8594
1
      temp_seg = i.seg[0];
8595
1
      i.seg[0] = i.seg[1];
8596
1
      i.seg[1] = temp_seg;
8597
1
    }
8598
1.16k
}
8599
8600
/* Try to ensure constant immediates are represented in the smallest
8601
   opcode possible.  */
8602
static void
8603
optimize_imm (void)
8604
1.96k
{
8605
1.96k
  char guess_suffix = 0;
8606
1.96k
  int op;
8607
8608
1.96k
  if (i.suffix)
8609
165
    guess_suffix = i.suffix;
8610
1.80k
  else if (i.reg_operands)
8611
284
    {
8612
      /* Figure out a suffix from the last register operand specified.
8613
   We can't do this properly yet, i.e. excluding special register
8614
   instances, but the following works for instructions with
8615
   immediates.  In any case, we can't set i.suffix yet.  */
8616
284
      for (op = i.operands; --op >= 0;)
8617
284
  if (i.types[op].bitfield.class != Reg)
8618
0
    continue;
8619
284
  else if (i.types[op].bitfield.byte)
8620
6
    {
8621
6
      guess_suffix = BYTE_MNEM_SUFFIX;
8622
6
      break;
8623
6
    }
8624
278
  else if (i.types[op].bitfield.word)
8625
0
    {
8626
0
      guess_suffix = WORD_MNEM_SUFFIX;
8627
0
      break;
8628
0
    }
8629
278
  else if (i.types[op].bitfield.dword)
8630
278
    {
8631
278
      guess_suffix = LONG_MNEM_SUFFIX;
8632
278
      break;
8633
278
    }
8634
0
  else if (i.types[op].bitfield.qword)
8635
0
    {
8636
0
      guess_suffix = QWORD_MNEM_SUFFIX;
8637
0
      break;
8638
0
    }
8639
284
    }
8640
1.51k
  else if ((flag_code == CODE_16BIT)
8641
1.51k
      ^ (i.prefix[DATA_PREFIX] != 0 && !(i.prefix[REX_PREFIX] & REX_W)))
8642
266
    guess_suffix = WORD_MNEM_SUFFIX;
8643
1.25k
  else if (flag_code != CODE_64BIT
8644
1.24k
     || (!(i.prefix[REX_PREFIX] & REX_W)
8645
         /* A more generic (but also more involved) way of dealing
8646
      with the special case(s) would be to go look for
8647
      DefaultSize attributes on any of the templates.  */
8648
1.24k
         && current_templates.start->mnem_off != MN_push
8649
137
         && current_templates.start->mnem_off != MN_jmpabs))
8650
138
    guess_suffix = LONG_MNEM_SUFFIX;
8651
8652
3.93k
  for (op = i.imm_operands; --op >= 0;)
8653
1.97k
    if (operand_type_check (i.types[op], imm))
8654
1.97k
      {
8655
1.97k
  switch (i.op[op].imms->X_op)
8656
1.97k
    {
8657
613
    case O_constant:
8658
      /* If a suffix is given, this operand may be shortened.  */
8659
613
      switch (guess_suffix)
8660
613
        {
8661
322
        case LONG_MNEM_SUFFIX:
8662
322
    i.types[op].bitfield.imm32 = 1;
8663
322
    i.types[op].bitfield.imm64 = 1;
8664
322
    break;
8665
264
        case WORD_MNEM_SUFFIX:
8666
264
    i.types[op].bitfield.imm16 = 1;
8667
264
    i.types[op].bitfield.imm32 = 1;
8668
264
    i.types[op].bitfield.imm32s = 1;
8669
264
    i.types[op].bitfield.imm64 = 1;
8670
264
    break;
8671
4
        case BYTE_MNEM_SUFFIX:
8672
4
    i.types[op].bitfield.imm8 = 1;
8673
4
    i.types[op].bitfield.imm8s = 1;
8674
4
    i.types[op].bitfield.imm16 = 1;
8675
4
    i.types[op].bitfield.imm32 = 1;
8676
4
    i.types[op].bitfield.imm32s = 1;
8677
4
    i.types[op].bitfield.imm64 = 1;
8678
4
    break;
8679
613
        }
8680
8681
      /* If this operand is at most 16 bits, convert it
8682
         to a signed 16 bit number before trying to see
8683
         whether it will fit in an even smaller size.
8684
         This allows a 16-bit operand such as $0xffe0 to
8685
         be recognised as within Imm8S range.  */
8686
613
      if ((i.types[op].bitfield.imm16)
8687
268
    && fits_in_unsigned_word (i.op[op].imms->X_add_number))
8688
143
        {
8689
143
    i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
8690
143
            ^ 0x8000) - 0x8000);
8691
143
        }
8692
613
#ifdef BFD64
8693
      /* Store 32-bit immediate in 64-bit for 64-bit BFD.  */
8694
613
      if ((i.types[op].bitfield.imm32)
8695
590
    && fits_in_unsigned_long (i.op[op].imms->X_add_number))
8696
439
        {
8697
439
    i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
8698
439
            ^ ((offsetT) 1 << 31))
8699
439
                 - ((offsetT) 1 << 31));
8700
439
        }
8701
613
#endif
8702
613
      i.types[op]
8703
613
        = operand_type_or (i.types[op],
8704
613
         smallest_imm_type (i.op[op].imms->X_add_number));
8705
8706
      /* We must avoid matching of Imm32 templates when 64bit
8707
         only immediate is available.  */
8708
613
      if (guess_suffix == QWORD_MNEM_SUFFIX)
8709
23
        i.types[op].bitfield.imm32 = 0;
8710
613
      break;
8711
8712
0
    case O_absent:
8713
0
    case O_register:
8714
0
      abort ();
8715
8716
      /* Symbols and expressions.  */
8717
1.36k
    default:
8718
      /* Convert symbolic operand to proper sizes for matching, but don't
8719
         prevent matching a set of insns that only supports sizes other
8720
         than those matching the insn suffix.  */
8721
1.36k
      {
8722
1.36k
        i386_operand_type mask, allowed;
8723
1.36k
        const insn_template *t = current_templates.start;
8724
8725
1.36k
        operand_type_set (&mask, 0);
8726
1.36k
        switch (guess_suffix)
8727
1.36k
    {
8728
0
    case QWORD_MNEM_SUFFIX:
8729
0
      mask.bitfield.imm64 = 1;
8730
0
      mask.bitfield.imm32s = 1;
8731
0
      break;
8732
235
    case LONG_MNEM_SUFFIX:
8733
235
      mask.bitfield.imm32 = 1;
8734
235
      break;
8735
7
    case WORD_MNEM_SUFFIX:
8736
7
      mask.bitfield.imm16 = 1;
8737
7
      break;
8738
6
    case BYTE_MNEM_SUFFIX:
8739
6
      mask.bitfield.imm8 = 1;
8740
6
      break;
8741
1.11k
    default:
8742
1.11k
      break;
8743
1.36k
    }
8744
8745
1.36k
        allowed = operand_type_and (get_operand_types (t)[op], mask);
8746
13.8k
        while (++t < current_templates.end)
8747
12.5k
    {
8748
12.5k
      allowed = operand_type_or (allowed, get_operand_types (t)[op]);
8749
12.5k
      allowed = operand_type_and (allowed, mask);
8750
12.5k
    }
8751
8752
1.36k
        if (!operand_type_all_zero (&allowed))
8753
210
    i.types[op] = operand_type_and (i.types[op], mask);
8754
1.36k
      }
8755
0
      break;
8756
1.97k
    }
8757
1.97k
      }
8758
1.96k
}
8759
8760
/* Try to use the smallest displacement type too.  */
8761
static bool
8762
optimize_disp (const insn_template *t)
8763
1.54k
{
8764
1.54k
  unsigned int op;
8765
8766
1.54k
  if (!want_disp32 (t)
8767
947
      && (!t->opcode_modifier.jump
8768
57
    || i.jumpabsolute || i.types[0].bitfield.baseindex))
8769
890
    {
8770
2.51k
      for (op = i.imm_operands; op < i.operands; ++op)
8771
1.62k
  {
8772
1.62k
    const expressionS *exp = i.op[op].disps;
8773
8774
1.62k
    if (!operand_type_check (i.types[op], disp))
8775
737
      continue;
8776
8777
889
    if (exp->X_op != O_constant)
8778
866
      continue;
8779
8780
    /* Since displacement is signed extended to 64bit, don't allow
8781
       disp32 if it is out of range.  */
8782
23
    if (fits_in_signed_long (exp->X_add_number))
8783
22
      continue;
8784
8785
1
    i.types[op].bitfield.disp32 = 0;
8786
1
    if (i.types[op].bitfield.baseindex)
8787
0
      {
8788
0
        as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
8789
0
          (uint64_t) exp->X_add_number);
8790
0
        return false;
8791
0
      }
8792
1
  }
8793
890
    }
8794
8795
  /* Don't optimize displacement for movabs / jmpabs since they only take
8796
     64-bit displacement.  */
8797
1.54k
  if (pp.disp_encoding > disp_encoding_8bit
8798
1.54k
      || t->mnem_off == MN_movabs || t->mnem_off == MN_jmpabs)
8799
5
    return true;
8800
8801
4.10k
  for (op = i.operands; op-- > 0;)
8802
2.56k
    if (operand_type_check (i.types[op], disp))
8803
1.54k
      {
8804
1.54k
  if (i.op[op].disps->X_op == O_constant)
8805
110
    {
8806
110
      offsetT op_disp = i.op[op].disps->X_add_number;
8807
8808
110
      if (!op_disp && i.types[op].bitfield.baseindex)
8809
1
        {
8810
1
    i.types[op] = operand_type_and_not (i.types[op], anydisp);
8811
1
    i.op[op].disps = NULL;
8812
1
    i.disp_operands--;
8813
1
    continue;
8814
1
        }
8815
8816
109
      if (i.types[op].bitfield.disp16
8817
62
    && fits_in_unsigned_word (op_disp))
8818
62
        {
8819
    /* If this operand is at most 16 bits, convert
8820
       to a signed 16 bit number and don't use 64bit
8821
       displacement.  */
8822
62
    op_disp = ((op_disp ^ 0x8000) - 0x8000);
8823
62
    i.types[op].bitfield.disp64 = 0;
8824
62
        }
8825
8826
109
#ifdef BFD64
8827
      /* Optimize 64-bit displacement to 32-bit for 64-bit BFD.  */
8828
109
      if ((flag_code != CODE_64BIT
8829
109
     ? i.types[op].bitfield.disp32
8830
109
     : want_disp32 (t)
8831
7
       && (!t->opcode_modifier.jump
8832
7
           || i.jumpabsolute || i.types[op].bitfield.baseindex))
8833
8
    && fits_in_unsigned_long (op_disp))
8834
7
        {
8835
    /* If this operand is at most 32 bits, convert
8836
       to a signed 32 bit number and don't use 64bit
8837
       displacement.  */
8838
7
    op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
8839
7
    i.types[op].bitfield.disp64 = 0;
8840
7
    i.types[op].bitfield.disp32 = 1;
8841
7
        }
8842
8843
109
      if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
8844
45
        {
8845
45
    i.types[op].bitfield.disp64 = 0;
8846
45
    i.types[op].bitfield.disp32 = 1;
8847
45
        }
8848
109
#endif
8849
109
      if ((i.types[op].bitfield.disp32
8850
63
     || i.types[op].bitfield.disp16)
8851
108
    && fits_in_disp8 (op_disp))
8852
101
        i.types[op].bitfield.disp8 = 1;
8853
8854
109
      i.op[op].disps->X_add_number = op_disp;
8855
109
    }
8856
1.43k
  else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8857
1.43k
     || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
8858
0
    {
8859
0
      fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
8860
0
       i.op[op].disps, 0, i.reloc[op]);
8861
0
      i.types[op] = operand_type_and_not (i.types[op], anydisp);
8862
0
    }
8863
1.43k
  else
8864
    /* We only support 64bit displacement on constants.  */
8865
1.43k
    i.types[op].bitfield.disp64 = 0;
8866
1.54k
      }
8867
8868
1.54k
  return true;
8869
1.54k
}
8870
8871
/* Return 1 if there is a match in broadcast bytes between operand
8872
   GIVEN and instruction template T.   */
8873
8874
static INLINE int
8875
match_broadcast_size (const insn_template *t, unsigned int given)
8876
0
{
8877
0
  return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
8878
0
     && i.types[given].bitfield.byte)
8879
0
    || (t->opcode_modifier.broadcast == WORD_BROADCAST
8880
0
        && i.types[given].bitfield.word)
8881
0
    || (t->opcode_modifier.broadcast == DWORD_BROADCAST
8882
0
        && i.types[given].bitfield.dword)
8883
0
    || (t->opcode_modifier.broadcast == QWORD_BROADCAST
8884
0
        && i.types[given].bitfield.qword));
8885
0
}
8886
8887
/* Check if operands are valid for the instruction.  */
8888
8889
static int
8890
check_VecOperands (const insn_template *t)
8891
3.33k
{
8892
3.33k
  unsigned int op;
8893
3.33k
  i386_cpu_flags cpu;
8894
3.33k
  const i386_operand_type *t_types = get_operand_types (t);
8895
8896
  /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
8897
     any one operand are implicity requiring AVX512VL support if the actual
8898
     operand size is YMMword or XMMword.  Since this function runs after
8899
     template matching, there's no need to check for YMMword/XMMword in
8900
     the template.  */
8901
3.33k
  cpu = cpu_flags_or (cpu_flags_from_attr (t->cpu),
8902
3.33k
          cpu_flags_from_attr (t->cpu_any));
8903
3.33k
  cpu = cpu_flags_and (cpu, avx512);
8904
3.33k
  if (!cpu_flags_all_zero (&cpu)
8905
0
      && !cpu.bitfield.cpuavx512vl
8906
0
      && !cpu_arch_flags.bitfield.cpuavx512vl
8907
0
      && (!t->opcode_modifier.vex || need_evex_encoding (t)
8908
    /* Note: No need to check F16C here.  Those insns have distinct
8909
       templates for distinct VEX.L / EVEX.L'L.  */
8910
0
    || (maybe_cpu (t, CpuFMA) && !cpu_arch_flags.bitfield.cpufma)))
8911
0
    {
8912
0
      for (op = 0; op < t->operands; ++op)
8913
0
  {
8914
0
    if (t_types[op].bitfield.zmmword
8915
0
        && (i.types[op].bitfield.ymmword
8916
0
      || i.types[op].bitfield.xmmword))
8917
0
      {
8918
0
        i.error = operand_size_mismatch;
8919
0
        return 1;
8920
0
      }
8921
0
  }
8922
0
    }
8923
8924
  /* Somewhat similarly, templates specifying both AVX and AVX2 are
8925
     requiring AVX2 support if the actual operand size is YMMword.  */
8926
3.33k
  if (maybe_cpu (t, CpuAVX) && maybe_cpu (t, CpuAVX2)
8927
0
      && !cpu_arch_flags.bitfield.cpuavx2)
8928
0
    {
8929
0
      for (op = 0; op < t->operands; ++op)
8930
0
  {
8931
0
    if (t_types[op].bitfield.xmmword
8932
0
        && i.types[op].bitfield.ymmword)
8933
0
      {
8934
0
        i.error = operand_size_mismatch;
8935
0
        return 1;
8936
0
      }
8937
0
  }
8938
0
    }
8939
8940
  /* Without VSIB byte, we can't have a vector register for index.  */
8941
3.33k
  if (!t->opcode_modifier.sib
8942
3.33k
      && i.index_reg
8943
0
      && (i.index_reg->reg_type.bitfield.xmmword
8944
0
    || i.index_reg->reg_type.bitfield.ymmword
8945
0
    || i.index_reg->reg_type.bitfield.zmmword))
8946
0
    {
8947
0
      i.error = unsupported_vector_index_register;
8948
0
      return 1;
8949
0
    }
8950
8951
  /* Check if default mask is allowed.  */
8952
3.33k
  if (t->opcode_modifier.operandconstraint == NO_DEFAULT_MASK
8953
0
      && (!i.mask.reg || i.mask.reg->reg_num == 0))
8954
0
    {
8955
0
      i.error = no_default_mask;
8956
0
      return 1;
8957
0
    }
8958
8959
  /* For VSIB byte, we need a vector register for index, and all vector
8960
     registers must be distinct.  */
8961
3.33k
  if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
8962
0
    {
8963
0
      if (!i.index_reg
8964
0
    || !((t->opcode_modifier.sib == VECSIB128
8965
0
    && i.index_reg->reg_type.bitfield.xmmword)
8966
0
         || (t->opcode_modifier.sib == VECSIB256
8967
0
       && i.index_reg->reg_type.bitfield.ymmword)
8968
0
         || (t->opcode_modifier.sib == VECSIB512
8969
0
       && i.index_reg->reg_type.bitfield.zmmword)))
8970
0
      {
8971
0
  i.error = invalid_vsib_address;
8972
0
  return 1;
8973
0
      }
8974
8975
0
      gas_assert (i.reg_operands == 2 || i.mask.reg);
8976
0
      if (i.reg_operands == 2 && !i.mask.reg)
8977
0
  {
8978
0
    gas_assert (i.types[0].bitfield.class == RegSIMD);
8979
0
    gas_assert (i.types[0].bitfield.xmmword
8980
0
          || i.types[0].bitfield.ymmword);
8981
0
    gas_assert (i.types[2].bitfield.class == RegSIMD);
8982
0
    gas_assert (i.types[2].bitfield.xmmword
8983
0
          || i.types[2].bitfield.ymmword);
8984
0
    if (operand_check == check_none)
8985
0
      return 0;
8986
0
    if (register_number (i.op[0].regs)
8987
0
        != register_number (i.index_reg)
8988
0
        && register_number (i.op[2].regs)
8989
0
     != register_number (i.index_reg)
8990
0
        && register_number (i.op[0].regs)
8991
0
     != register_number (i.op[2].regs))
8992
0
      return 0;
8993
0
    if (operand_check == check_error)
8994
0
      {
8995
0
        i.error = invalid_vector_register_set;
8996
0
        return 1;
8997
0
      }
8998
0
    as_warn (_("mask, index, and destination registers should be distinct"));
8999
0
  }
9000
0
      else if (i.reg_operands == 1 && i.mask.reg)
9001
0
  {
9002
0
    if (i.types[1].bitfield.class == RegSIMD
9003
0
        && (i.types[1].bitfield.xmmword
9004
0
            || i.types[1].bitfield.ymmword
9005
0
            || i.types[1].bitfield.zmmword)
9006
0
        && (register_number (i.op[1].regs)
9007
0
      == register_number (i.index_reg)))
9008
0
      {
9009
0
        if (operand_check == check_error)
9010
0
    {
9011
0
      i.error = invalid_vector_register_set;
9012
0
      return 1;
9013
0
    }
9014
0
        if (operand_check != check_none)
9015
0
    as_warn (_("index and destination registers should be distinct"));
9016
0
      }
9017
0
  }
9018
0
    }
9019
9020
  /* For AMX instructions with 3 TMM register operands, all operands
9021
      must be distinct.  */
9022
3.33k
  if (i.reg_operands == 3
9023
1
      && t_types[0].bitfield.tmmword
9024
0
      && (i.op[0].regs == i.op[1].regs
9025
0
          || i.op[0].regs == i.op[2].regs
9026
0
          || i.op[1].regs == i.op[2].regs))
9027
0
    {
9028
0
      i.error = invalid_tmm_register_set;
9029
0
      return 1;
9030
0
    }
9031
9032
  /* For some special instructions require that destination must be distinct
9033
     from source registers.  */
9034
3.33k
  if (t->opcode_modifier.operandconstraint == DISTINCT_DEST)
9035
0
    {
9036
0
      unsigned int dest_reg = i.operands - 1;
9037
9038
0
      know (i.operands >= 3);
9039
9040
      /* #UD if dest_reg == src1_reg or dest_reg == src2_reg.  */
9041
0
      if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
9042
0
    || (i.reg_operands > 2
9043
0
        && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
9044
0
  {
9045
0
    i.error = invalid_dest_and_src_register_set;
9046
0
    return 1;
9047
0
  }
9048
0
    }
9049
9050
  /* Check if broadcast is supported by the instruction and is applied
9051
     to the memory operand.  */
9052
3.33k
  if (i.broadcast.type || i.broadcast.bytes)
9053
0
    {
9054
0
      i386_operand_type type, overlap;
9055
9056
      /* Check if specified broadcast is supported in this instruction,
9057
   and its broadcast bytes match the memory operand.  */
9058
0
      op = i.broadcast.operand;
9059
0
      if (!t->opcode_modifier.broadcast
9060
0
    || !(i.flags[op] & Operand_Mem)
9061
0
    || (!i.types[op].bitfield.unspecified
9062
0
        && !match_broadcast_size (t, op)))
9063
0
  {
9064
0
  bad_broadcast:
9065
0
    i.error = unsupported_broadcast;
9066
0
    return 1;
9067
0
  }
9068
9069
0
      operand_type_set (&type, 0);
9070
0
      switch (get_broadcast_bytes (t, false))
9071
0
  {
9072
0
  case 2:
9073
0
    type.bitfield.word = 1;
9074
0
    break;
9075
0
  case 4:
9076
0
    type.bitfield.dword = 1;
9077
0
    break;
9078
0
  case 8:
9079
0
    type.bitfield.qword = 1;
9080
0
    break;
9081
0
  case 16:
9082
0
    type.bitfield.xmmword = 1;
9083
0
    break;
9084
0
  case 32:
9085
0
    if (vector_size < VSZ256)
9086
0
      goto bad_broadcast;
9087
0
    type.bitfield.ymmword = 1;
9088
0
    break;
9089
0
  case 64:
9090
0
    if (vector_size < VSZ512)
9091
0
      goto bad_broadcast;
9092
0
    type.bitfield.zmmword = 1;
9093
0
    break;
9094
0
  default:
9095
0
    goto bad_broadcast;
9096
0
  }
9097
9098
0
      overlap = operand_type_and (type, t_types[op]);
9099
0
      if (t_types[op].bitfield.class == RegSIMD
9100
0
    && t_types[op].bitfield.byte
9101
0
       + t_types[op].bitfield.word
9102
0
       + t_types[op].bitfield.dword
9103
0
       + t_types[op].bitfield.qword > 1)
9104
0
  {
9105
0
    overlap.bitfield.xmmword = 0;
9106
0
    overlap.bitfield.ymmword = 0;
9107
0
    overlap.bitfield.zmmword = 0;
9108
0
  }
9109
0
      if (operand_type_all_zero (&overlap))
9110
0
    goto bad_broadcast;
9111
9112
0
      if (t->opcode_modifier.checkoperandsize)
9113
0
  {
9114
0
    unsigned int j;
9115
9116
0
    type.bitfield.baseindex = 1;
9117
0
    for (j = i.imm_operands; j < i.operands; ++j)
9118
0
      {
9119
0
        if (j != op
9120
0
      && !operand_type_register_match(i.types[j],
9121
0
              t_types[j],
9122
0
              type,
9123
0
              t_types[op]))
9124
0
    goto bad_broadcast;
9125
0
      }
9126
0
  }
9127
0
    }
9128
  /* If broadcast is supported in this instruction, we need to check if
9129
     operand of one-element size isn't specified without broadcast.  */
9130
3.33k
  else if (t->opcode_modifier.broadcast && i.mem_operands)
9131
0
    {
9132
      /* Find memory operand.  */
9133
0
      for (op = i.imm_operands; op < i.operands; op++)
9134
0
  if (i.flags[op] & Operand_Mem)
9135
0
    break;
9136
0
      gas_assert (op < i.operands);
9137
      /* Check size of the memory operand.  */
9138
0
      if (match_broadcast_size (t, op))
9139
0
  {
9140
0
    i.error = broadcast_needed;
9141
0
    return 1;
9142
0
  }
9143
0
    }
9144
3.33k
  else
9145
3.33k
    op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning.  */
9146
9147
  /* Check if requested masking is supported.  */
9148
3.33k
  if (i.mask.reg)
9149
0
    {
9150
0
      if (!t->opcode_modifier.masking)
9151
0
  {
9152
0
    i.error = unsupported_masking;
9153
0
    return 1;
9154
0
  }
9155
9156
      /* Common rules for masking:
9157
   - mask register destinations permit only zeroing-masking, without
9158
     that actually being expressed by a {z} operand suffix or EVEX.z,
9159
   - memory destinations allow only merging-masking,
9160
   - scatter/gather insns (i.e. ones using vSIB) only allow merging-
9161
     masking.  */
9162
0
      if (i.mask.zeroing
9163
0
    && (t_types[t->operands - 1].bitfield.class == RegMask
9164
0
        || (i.flags[t->operands - 1] & Operand_Mem)
9165
0
        || t->opcode_modifier.sib))
9166
0
  {
9167
0
    i.error = unsupported_masking;
9168
0
    return 1;
9169
0
  }
9170
0
    }
9171
9172
  /* Check if masking is applied to dest operand.  */
9173
3.33k
  if (i.mask.reg && (i.mask.operand != i.operands - 1))
9174
0
    {
9175
0
      i.error = mask_not_on_destination;
9176
0
      return 1;
9177
0
    }
9178
9179
  /* Check RC/SAE.  */
9180
3.33k
  if (i.rounding.type != rc_none)
9181
0
    {
9182
0
      if (!t->opcode_modifier.sae
9183
0
    || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
9184
0
    || i.mem_operands)
9185
0
  {
9186
0
    i.error = unsupported_rc_sae;
9187
0
    return 1;
9188
0
  }
9189
9190
      /* Non-EVEX.{LIG,512} forms need to have a ZMM or YMM register as at
9191
   least one operand.  There's no need to check all operands, though:
9192
   Either of the last two operands will be of the right size in all
9193
   relevant templates.  */
9194
0
      if (t->opcode_modifier.evex != EVEXLIG
9195
0
    && t->opcode_modifier.evex != EVEX512
9196
0
    && !i.types[t->operands - 1].bitfield.zmmword
9197
0
    && !i.types[t->operands - 2].bitfield.zmmword)
9198
0
  {
9199
0
    i.error = operand_size_mismatch;
9200
0
    return 1;
9201
0
  }
9202
0
    }
9203
9204
  /* Check the special Imm4 cases; must be the first operand.  */
9205
3.33k
  if ((is_cpu (t, CpuXOP) && t->operands == 5)
9206
3.33k
      || (t->opcode_space == SPACE_0F3A
9207
0
    && (t->base_opcode | 3) == 0x0b
9208
0
    && (is_cpu (t, CpuAPX_F)
9209
0
     || (t->opcode_modifier.sse2avx && t->opcode_modifier.evex
9210
0
         && (!t->opcode_modifier.vex
9211
0
       || (pp.encoding != encoding_default
9212
0
           && pp.encoding != encoding_vex
9213
0
           && pp.encoding != encoding_vex3))))))
9214
0
    {
9215
0
      if (i.op[0].imms->X_op != O_constant
9216
0
    || !fits_in_imm4 (i.op[0].imms->X_add_number))
9217
0
  {
9218
0
    i.error = bad_imm4;
9219
0
    return 1;
9220
0
  }
9221
9222
      /* Turn off Imm<N> so that update_imm won't complain.  */
9223
0
      if (t->operands == 5)
9224
0
  operand_type_set (&i.types[0], 0);
9225
0
    }
9226
9227
  /* Check vector Disp8 operand.  */
9228
3.33k
  if (t->opcode_modifier.disp8memshift
9229
0
      && (!t->opcode_modifier.vex
9230
0
    || need_evex_encoding (t))
9231
0
      && pp.disp_encoding <= disp_encoding_8bit)
9232
0
    {
9233
0
      if (i.broadcast.type || i.broadcast.bytes)
9234
0
  i.memshift = t->opcode_modifier.broadcast - 1;
9235
0
      else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
9236
0
  i.memshift = t->opcode_modifier.disp8memshift;
9237
0
      else
9238
0
  {
9239
0
    const i386_operand_type *type = NULL, *fallback = NULL;
9240
9241
0
    i.memshift = 0;
9242
0
    for (op = i.imm_operands; op < i.operands; op++)
9243
0
      if (i.flags[op] & Operand_Mem)
9244
0
        {
9245
0
    if (t->opcode_modifier.evex == EVEXLIG)
9246
0
      i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
9247
0
    else if (t_types[op].bitfield.xmmword
9248
0
       + t_types[op].bitfield.ymmword
9249
0
       + t_types[op].bitfield.zmmword <= 1)
9250
0
      type = &t_types[op];
9251
0
    else if (!i.types[op].bitfield.unspecified)
9252
0
      type = &i.types[op];
9253
0
    else /* Ambiguities get resolved elsewhere.  */
9254
0
      fallback = &t_types[op];
9255
0
        }
9256
0
      else if (i.types[op].bitfield.class == RegSIMD
9257
0
         && t->opcode_modifier.evex != EVEXLIG)
9258
0
        {
9259
0
    if (i.types[op].bitfield.zmmword)
9260
0
      i.memshift = 6;
9261
0
    else if (i.types[op].bitfield.ymmword && i.memshift < 5)
9262
0
      i.memshift = 5;
9263
0
    else if (i.types[op].bitfield.xmmword && i.memshift < 4)
9264
0
      i.memshift = 4;
9265
0
        }
9266
9267
0
    if (!type && !i.memshift)
9268
0
      type = fallback;
9269
0
    if (type)
9270
0
      {
9271
0
        if (type->bitfield.zmmword)
9272
0
    i.memshift = 6;
9273
0
        else if (type->bitfield.ymmword)
9274
0
    i.memshift = 5;
9275
0
        else if (type->bitfield.xmmword)
9276
0
    i.memshift = 4;
9277
0
      }
9278
9279
    /* For the check in fits_in_disp8().  */
9280
0
    if (i.memshift == 0)
9281
0
      i.memshift = -1;
9282
0
  }
9283
9284
0
      for (op = i.imm_operands; op < i.operands; op++)
9285
0
  if (operand_type_check (i.types[op], disp)
9286
0
      && i.op[op].disps->X_op == O_constant)
9287
0
    {
9288
      /* Make sure to leave i.types[op].bitfield.disp8 alone upon
9289
         secondary invocations of match_template().  */
9290
0
      if (fits_in_disp8 (i.op[op].disps->X_add_number))
9291
0
        {
9292
0
    if (!i.tm.mnem_off)
9293
0
      i.types[op].bitfield.disp8 = 1;
9294
0
    return 0;
9295
0
        }
9296
0
      if (!i.tm.mnem_off)
9297
0
        i.types[op].bitfield.disp8 = 0;
9298
0
    }
9299
0
    }
9300
9301
3.33k
  i.memshift = 0;
9302
9303
3.33k
  return 0;
9304
3.33k
}
9305
9306
/* Check if encoding requirements are met by the instruction.  */
9307
9308
static int
9309
VEX_check_encoding (const insn_template *t)
9310
3.50k
{
9311
3.50k
  if (pp.encoding == encoding_error)
9312
0
    {
9313
0
      i.error = unsupported;
9314
0
      return 1;
9315
0
    }
9316
9317
  /* Vector size restrictions.  */
9318
3.50k
  if ((vector_size < VSZ512
9319
9
       && t->opcode_modifier.evex == EVEX512)
9320
3.50k
      || (vector_size < VSZ256
9321
0
    && (t->opcode_modifier.evex == EVEX256
9322
0
        || t->opcode_modifier.vex == VEX256)))
9323
0
    {
9324
0
      i.error = unsupported_vector_size;
9325
0
      return 1;
9326
0
    }
9327
9328
3.50k
  switch (pp.encoding)
9329
3.50k
    {
9330
32
    case encoding_vex:
9331
32
    case encoding_vex3:
9332
      /* This instruction must be encoded with VEX prefix.  */
9333
32
      if (!t->opcode_modifier.vex)
9334
32
  {
9335
32
    i.error = no_vex_encoding;
9336
32
    return 1;
9337
32
  }
9338
0
      break;
9339
9340
3.44k
    case encoding_default:
9341
3.44k
      if (!pp.has_nf)
9342
3.44k
  break;
9343
      /* Fall through.  */
9344
25
    case encoding_evex:
9345
25
    case encoding_evex512:
9346
      /* This instruction must be encoded with EVEX prefix.  */
9347
25
      if (!t->opcode_modifier.evex)
9348
25
  {
9349
25
    i.error = no_evex_encoding;
9350
25
    return 1;
9351
25
  }
9352
0
      break;
9353
9354
3
    case encoding_egpr:
9355
      /* This instruction must be encoded with REX2 or EVEX prefix.  */
9356
3
      if (t->opcode_modifier.vex && !t->opcode_modifier.evex)
9357
0
  {
9358
0
    i.error = no_evex_encoding;
9359
0
    return 1;
9360
0
  }
9361
3
      break;
9362
9363
3
    default:
9364
0
      abort ();
9365
3.50k
    }
9366
9367
3.45k
  return 0;
9368
3.50k
}
9369
9370
/* Check if Egprs operands are valid for the instruction.  */
9371
9372
static bool
9373
check_EgprOperands (const insn_template *t)
9374
3.33k
{
9375
3.33k
  if (!t->opcode_modifier.noegpr)
9376
3.25k
    return false;
9377
9378
168
  for (unsigned int op = i.imm_operands; op < i.operands; op++)
9379
84
    {
9380
84
      if (i.types[op].bitfield.class != Reg)
9381
84
  continue;
9382
9383
0
      if (i.op[op].regs->reg_flags & RegRex2)
9384
0
  {
9385
0
    i.error = register_type_mismatch;
9386
0
    return true;
9387
0
  }
9388
0
    }
9389
9390
84
  if ((i.index_reg && (i.index_reg->reg_flags & RegRex2))
9391
84
      || (i.base_reg && (i.base_reg->reg_flags & RegRex2)))
9392
0
    {
9393
0
      i.error = unsupported_EGPR_for_addressing;
9394
0
      return true;
9395
0
    }
9396
9397
  /* Check if pseudo prefix {rex2} is valid.  */
9398
84
  if (pp.rex2_encoding && !t->opcode_modifier.sse2avx)
9399
0
    {
9400
0
      i.error = invalid_pseudo_prefix;
9401
0
      return true;
9402
0
    }
9403
9404
84
  return false;
9405
84
}
9406
9407
/* Check if APX operands are valid for the instruction.  */
9408
static bool
9409
check_APX_operands (const insn_template *t)
9410
3.33k
{
9411
  /* Push2* and Pop2* cannot use RSP and Pop2* cannot pop two same registers.
9412
   */
9413
3.33k
  switch (t->mnem_off)
9414
3.33k
    {
9415
0
    case MN_pop2:
9416
0
    case MN_pop2p:
9417
0
      if (register_number (i.op[0].regs) == register_number (i.op[1].regs))
9418
0
  {
9419
0
    i.error = invalid_dest_register_set;
9420
0
    return 1;
9421
0
  }
9422
    /* fall through */
9423
0
    case MN_push2:
9424
0
    case MN_push2p:
9425
0
      if (register_number (i.op[0].regs) == 4
9426
0
    || register_number (i.op[1].regs) == 4)
9427
0
  {
9428
0
    i.error = unsupported_rsp_register;
9429
0
    return 1;
9430
0
  }
9431
0
      break;
9432
3.33k
    }
9433
3.33k
  return 0;
9434
3.33k
}
9435
9436
/* Check if the instruction use the REX registers or REX prefix.  */
9437
static bool
9438
check_Rex_required (void)
9439
0
{
9440
0
  for (unsigned int op = i.imm_operands; op < i.operands; op++)
9441
0
    {
9442
0
      if (i.types[op].bitfield.class != Reg)
9443
0
  continue;
9444
9445
0
      if (i.op[op].regs->reg_flags & (RegRex | RegRex64))
9446
0
  return true;
9447
0
    }
9448
9449
0
  if ((i.index_reg && (i.index_reg->reg_flags & RegRex))
9450
0
      || (i.base_reg && (i.base_reg->reg_flags & RegRex)))
9451
0
    return true;
9452
9453
  /* Check pseudo prefix {rex} are valid.  */
9454
0
  return pp.rex_encoding;
9455
0
}
9456
9457
/* Optimize APX NDD insns to legacy insns.  */
9458
static unsigned int
9459
can_convert_NDD_to_legacy (const insn_template *t)
9460
0
{
9461
0
  unsigned int match_dest_op = ~0;
9462
9463
0
  if (!pp.has_nf && i.reg_operands >= 2)
9464
0
    {
9465
0
      unsigned int dest = i.operands - 1;
9466
0
      unsigned int src1 = i.operands - 2;
9467
0
      unsigned int src2 = (i.operands > 3) ? i.operands - 3 : 0;
9468
9469
0
      if (i.types[src1].bitfield.class == Reg
9470
0
    && i.op[src1].regs == i.op[dest].regs)
9471
0
  match_dest_op = src1;
9472
      /* If the first operand is the same as the third operand,
9473
   these instructions need to support the ability to commutative
9474
   the first two operands and still not change the semantics in order
9475
   to be optimized.  */
9476
0
      else if (optimize > 1
9477
0
         && t->opcode_modifier.commutative
9478
0
         && i.types[src2].bitfield.class == Reg
9479
0
         && i.op[src2].regs == i.op[dest].regs)
9480
0
  match_dest_op = src2;
9481
0
    }
9482
0
  return match_dest_op;
9483
0
}
9484
9485
/* Helper function for the progress() macro in match_template().  */
9486
static INLINE enum i386_error progress (enum i386_error new,
9487
          enum i386_error last,
9488
          unsigned int line, unsigned int *line_p)
9489
117k
{
9490
117k
  if (line <= *line_p)
9491
78.3k
    return last;
9492
38.9k
  *line_p = line;
9493
38.9k
  return new;
9494
117k
}
9495
9496
static const insn_template *
9497
match_template (char mnem_suffix)
9498
4.91k
{
9499
  /* Points to template once we've found it.  */
9500
4.91k
  const insn_template *t;
9501
4.91k
  i386_operand_type overlap0, overlap1, overlap2, overlap3;
9502
4.91k
  i386_operand_type overlap4;
9503
4.91k
  unsigned int found_reverse_match;
9504
4.91k
  i386_operand_type operand_types [MAX_OPERANDS];
9505
4.91k
  int addr_prefix_disp;
9506
4.91k
  unsigned int j, size_match, check_register, errline = __LINE__;
9507
4.91k
  enum i386_error specific_error = number_of_operands_mismatch;
9508
117k
#define progress(err) progress (err, specific_error, __LINE__, &errline)
9509
9510
#if MAX_OPERANDS != 5
9511
# error "MAX_OPERANDS must be 5."
9512
#endif
9513
9514
4.91k
  found_reverse_match = 0;
9515
4.91k
  addr_prefix_disp = -1;
9516
9517
29.9k
  for (t = current_templates.start; t < current_templates.end; t++)
9518
28.4k
    {
9519
28.4k
      addr_prefix_disp = -1;
9520
28.4k
      found_reverse_match = 0;
9521
9522
      /* Must have right number of operands.  */
9523
28.4k
      if (i.operands != t->operands)
9524
5.04k
  continue;
9525
9526
      /* Skip SSE2AVX templates when inapplicable.  */
9527
23.4k
      if (t->opcode_modifier.sse2avx
9528
388
    && (!sse2avx || i.prefix[DATA_PREFIX]))
9529
388
  {
9530
    /* Another non-SSE2AVX template has to follow.  */
9531
388
    gas_assert (t + 1 < current_templates.end);
9532
388
    continue;
9533
388
  }
9534
9535
      /* Check processor support.  */
9536
23.0k
      specific_error = progress (unsupported);
9537
23.0k
      if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
9538
10.4k
  continue;
9539
9540
      /* Check AT&T mnemonic.   */
9541
12.5k
      specific_error = progress (unsupported_with_intel_mnemonic);
9542
12.5k
      if (!intel_syntax && intel_mnemonic
9543
0
    && t->opcode_modifier.dialect == ATT_MNEMONIC)
9544
0
  continue;
9545
9546
      /* Check AT&T/Intel syntax.  */
9547
12.5k
      specific_error = progress (unsupported_syntax);
9548
12.5k
      if (intel_syntax
9549
12.5k
     ? t->opcode_modifier.dialect >= ATT_SYNTAX
9550
12.5k
     : t->opcode_modifier.dialect == INTEL_SYNTAX)
9551
5
  continue;
9552
9553
      /* Check NF support.  */
9554
12.5k
      specific_error = progress (unsupported_nf);
9555
12.5k
      if (pp.has_nf && !t->opcode_modifier.nf)
9556
0
  continue;
9557
9558
      /* Check Intel64/AMD64 ISA.   */
9559
12.5k
      switch (isa64)
9560
12.5k
  {
9561
12.5k
  default:
9562
    /* Default: Don't accept Intel64.  */
9563
12.5k
    if (t->opcode_modifier.isa64 == INTEL64)
9564
8
      continue;
9565
12.5k
    break;
9566
12.5k
  case amd64:
9567
    /* -mamd64: Don't accept Intel64 and Intel64 only.  */
9568
0
    if (t->opcode_modifier.isa64 >= INTEL64)
9569
0
      continue;
9570
0
    break;
9571
0
  case intel64:
9572
    /* -mintel64: Don't accept AMD64.  */
9573
0
    if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
9574
0
      continue;
9575
0
    break;
9576
12.5k
  }
9577
9578
      /* Check the suffix.  */
9579
12.5k
      specific_error = progress (invalid_instruction_suffix);
9580
12.5k
      if ((t->opcode_modifier.no_bsuf && mnem_suffix == BYTE_MNEM_SUFFIX)
9581
12.5k
    || (t->opcode_modifier.no_wsuf && mnem_suffix == WORD_MNEM_SUFFIX)
9582
12.5k
    || (t->opcode_modifier.no_lsuf && mnem_suffix == LONG_MNEM_SUFFIX)
9583
12.5k
    || (t->opcode_modifier.no_ssuf && mnem_suffix == SHORT_MNEM_SUFFIX)
9584
12.5k
    || (t->opcode_modifier.no_qsuf && mnem_suffix == QWORD_MNEM_SUFFIX))
9585
29
  continue;
9586
9587
12.5k
      specific_error = progress (operand_size_mismatch);
9588
12.5k
      size_match = operand_size_match (t);
9589
12.5k
      if (!size_match)
9590
1.38k
  continue;
9591
9592
      /* This is intentionally not
9593
9594
   if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
9595
9596
   as the case of a missing * on the operand is accepted (perhaps with
9597
   a warning, issued further down).  */
9598
11.1k
      specific_error = progress (operand_type_mismatch);
9599
11.1k
      if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
9600
0
  continue;
9601
9602
11.1k
      const i386_operand_type *t_types = get_operand_types (t);
9603
26.3k
      for (j = 0; j < t->operands; ++j)
9604
15.1k
  operand_types[j] = t_types[j];
9605
51.8k
      for (; j < MAX_OPERANDS; ++j)
9606
40.6k
  operand_types[j] = (i386_operand_type){ .array[0] = 0 };
9607
9608
      /* In Intel syntax, normally we can check for memory operand size when
9609
   there is no mnemonic suffix.  But jmp and call have 2 different
9610
   encodings with Dword memory operand size.  Skip the "near" one
9611
   (permitting a register operand) when "far" was requested.  */
9612
11.1k
      if (i.far_branch
9613
0
    && t->opcode_modifier.jump == JUMP_ABSOLUTE
9614
0
    && operand_types[0].bitfield.class == Reg)
9615
0
  continue;
9616
9617
      /* In general, don't allow 32-bit operands on pre-386.  */
9618
11.1k
      specific_error = progress (mnem_suffix ? invalid_instruction_suffix
9619
11.1k
               : operand_size_mismatch);
9620
11.1k
      j = i.imm_operands + (t->operands > i.imm_operands + 1);
9621
11.1k
      if (i.suffix == LONG_MNEM_SUFFIX
9622
628
    && !cpu_arch_flags.bitfield.cpui386
9623
0
    && (intel_syntax
9624
0
        ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
9625
0
     && !intel_float_operand (insn_name (t)))
9626
0
        : intel_float_operand (insn_name (t)) != 2)
9627
0
    && (t->operands == i.imm_operands
9628
0
        || (operand_types[i.imm_operands].bitfield.class != RegMMX
9629
0
         && operand_types[i.imm_operands].bitfield.class != RegSIMD
9630
0
         && operand_types[i.imm_operands].bitfield.class != RegMask)
9631
0
        || (operand_types[j].bitfield.class != RegMMX
9632
0
      && operand_types[j].bitfield.class != RegSIMD
9633
0
      && operand_types[j].bitfield.class != RegMask))
9634
0
    && !t->opcode_modifier.sib)
9635
0
  continue;
9636
9637
      /* Do not verify operands when there are none.  */
9638
11.1k
      if (!t->operands)
9639
147
  {
9640
147
    if (VEX_check_encoding (t))
9641
32
      {
9642
32
        specific_error = progress (i.error);
9643
32
        continue;
9644
32
      }
9645
9646
    /* Check if pseudo prefix {rex2} is valid.  */
9647
115
    if (t->opcode_modifier.noegpr && pp.rex2_encoding)
9648
0
      {
9649
0
        specific_error = progress (invalid_pseudo_prefix);
9650
0
        continue;
9651
0
      }
9652
9653
    /* We've found a match; break out of loop.  */
9654
115
    break;
9655
115
  }
9656
9657
11.0k
      if (!t->opcode_modifier.jump
9658
79
    || t->opcode_modifier.jump == JUMP_ABSOLUTE)
9659
10.9k
  {
9660
    /* There should be only one Disp operand.  */
9661
42.8k
    for (j = 0; j < MAX_OPERANDS; j++)
9662
36.9k
      if (operand_type_check (operand_types[j], disp))
9663
5.06k
        break;
9664
10.9k
    if (j < MAX_OPERANDS)
9665
5.06k
      {
9666
5.06k
        bool override = (i.prefix[ADDR_PREFIX] != 0);
9667
9668
5.06k
        addr_prefix_disp = j;
9669
9670
        /* Address size prefix will turn Disp64 operand into Disp32 and
9671
     Disp32/Disp16 one into Disp16/Disp32 respectively.  */
9672
5.06k
        switch (flag_code)
9673
5.06k
    {
9674
1.24k
    case CODE_16BIT:
9675
1.24k
      override = !override;
9676
      /* Fall through.  */
9677
1.50k
    case CODE_32BIT:
9678
1.50k
      if (operand_types[j].bitfield.disp32
9679
1.50k
          && operand_types[j].bitfield.disp16)
9680
1.50k
        {
9681
1.50k
          operand_types[j].bitfield.disp16 = override;
9682
1.50k
          operand_types[j].bitfield.disp32 = !override;
9683
1.50k
        }
9684
1.50k
      gas_assert (!operand_types[j].bitfield.disp64);
9685
1.50k
      break;
9686
9687
3.55k
    case CODE_64BIT:
9688
3.55k
      if (operand_types[j].bitfield.disp64)
9689
540
        {
9690
540
          gas_assert (!operand_types[j].bitfield.disp32);
9691
540
          operand_types[j].bitfield.disp32 = override;
9692
540
          operand_types[j].bitfield.disp64 = !override;
9693
540
        }
9694
3.55k
      operand_types[j].bitfield.disp16 = 0;
9695
3.55k
      break;
9696
5.06k
    }
9697
5.06k
      }
9698
10.9k
  }
9699
9700
      /* We check register size if needed.  */
9701
11.0k
      if (t->opcode_modifier.checkoperandsize)
9702
2.49k
  {
9703
2.49k
    check_register = (1 << t->operands) - 1;
9704
2.49k
    if (i.broadcast.type || i.broadcast.bytes)
9705
12
      check_register &= ~(1 << i.broadcast.operand);
9706
2.49k
  }
9707
8.52k
      else
9708
8.52k
  check_register = 0;
9709
9710
11.0k
      overlap0 = operand_type_and (i.types[0], operand_types[0]);
9711
11.0k
      switch (t->operands)
9712
11.0k
  {
9713
7.06k
  case 1:
9714
7.06k
    if (!operand_type_match (overlap0, i.types[0]))
9715
5.45k
      {
9716
5.45k
        specific_error = progress (i.error);
9717
5.45k
        continue;
9718
5.45k
      }
9719
9720
    /* Allow the ModR/M encoding to be requested by using the {load} or
9721
       {store} pseudo prefix on an applicable insn.  */
9722
1.61k
    if (!t->opcode_modifier.modrm
9723
1.32k
        && i.reg_operands == 1
9724
126
        && ((pp.dir_encoding == dir_encoding_load
9725
0
       && t->mnem_off != MN_pop)
9726
126
      || (pp.dir_encoding == dir_encoding_store
9727
0
          && t->mnem_off != MN_push))
9728
        /* Avoid BSWAP.  */
9729
0
        && t->mnem_off != MN_bswap)
9730
0
      continue;
9731
1.61k
    break;
9732
9733
3.78k
  case 2:
9734
    /* xchg %eax, %eax is a special case. It is an alias for nop
9735
       only in 32bit mode and we can use opcode 0x90.  In 64bit
9736
       mode, we can't use 0x90 for xchg %eax, %eax since it should
9737
       zero-extend %eax to %rax.  */
9738
3.78k
    if (t->base_opcode == 0x90
9739
0
        && t->opcode_space == SPACE_BASE)
9740
0
      {
9741
0
        if (flag_code == CODE_64BIT
9742
0
      && i.types[0].bitfield.instance == Accum
9743
0
      && i.types[0].bitfield.dword
9744
0
      && i.types[1].bitfield.instance == Accum)
9745
0
    continue;
9746
9747
        /* Allow the ModR/M encoding to be requested by using the
9748
     {load} or {store} pseudo prefix.  */
9749
0
        if (pp.dir_encoding == dir_encoding_load
9750
0
      || pp.dir_encoding == dir_encoding_store)
9751
0
    continue;
9752
0
      }
9753
9754
3.78k
    if (t->base_opcode == MOV_AX_DISP32
9755
727
        && t->opcode_space == SPACE_BASE
9756
727
        && t->mnem_off != MN_movabs)
9757
727
      {
9758
        /* Force 0x8b encoding for "mov foo@GOT, %eax".  */
9759
727
        if (i.reloc[0] == BFD_RELOC_386_GOT32)
9760
0
    continue;
9761
9762
        /* xrelease mov %eax, <disp> is another special case. It must not
9763
     match the accumulator-only encoding of mov.  */
9764
727
        if (i.hle_prefix)
9765
0
    continue;
9766
9767
        /* Allow the ModR/M encoding to be requested by using a suitable
9768
     {load} or {store} pseudo prefix.  */
9769
727
        if (pp.dir_encoding == (i.types[0].bitfield.instance == Accum
9770
727
             ? dir_encoding_store
9771
727
             : dir_encoding_load)
9772
0
      && !i.types[0].bitfield.disp64
9773
0
      && !i.types[1].bitfield.disp64)
9774
0
    continue;
9775
727
      }
9776
9777
    /* Allow the ModR/M encoding to be requested by using the {load} or
9778
       {store} pseudo prefix on an applicable insn.  */
9779
3.78k
    if (!t->opcode_modifier.modrm
9780
1.04k
        && i.reg_operands == 1
9781
168
        && i.imm_operands == 1
9782
25
        && (pp.dir_encoding == dir_encoding_load
9783
25
      || pp.dir_encoding == dir_encoding_store)
9784
0
        && t->opcode_space == SPACE_BASE)
9785
0
      {
9786
0
        if (t->base_opcode == 0xb0 /* mov $imm, %reg */
9787
0
      && pp.dir_encoding == dir_encoding_store)
9788
0
    continue;
9789
9790
0
        if ((t->base_opcode | 0x38) == 0x3c /* <alu> $imm, %acc */
9791
0
      && (t->base_opcode != 0x3c /* cmp $imm, %acc */
9792
0
          || pp.dir_encoding == dir_encoding_load))
9793
0
    continue;
9794
9795
0
        if (t->base_opcode == 0xa8 /* test $imm, %acc */
9796
0
      && pp.dir_encoding == dir_encoding_load)
9797
0
    continue;
9798
0
      }
9799
    /* Fall through.  */
9800
9801
3.96k
  case 3:
9802
3.96k
    if (!(size_match & MATCH_STRAIGHT))
9803
110
      goto check_reverse;
9804
    /* Reverse direction of operands if swapping is possible in the first
9805
       place (operands need to be symmetric) and
9806
       - the load form is requested, and the template is a store form,
9807
       - the store form is requested, and the template is a load form,
9808
       - the non-default (swapped) form is requested.  */
9809
3.85k
    overlap1 = operand_type_and (operand_types[0], operand_types[1]);
9810
9811
3.85k
    j = i.operands - 1 - (t->opcode_space == SPACE_MAP4
9812
69
        && t->opcode_modifier.vexvvvv);
9813
9814
3.85k
    if (t->opcode_modifier.d && i.reg_operands == i.operands
9815
869
        && !operand_type_all_zero (&overlap1))
9816
869
      switch (pp.dir_encoding)
9817
869
        {
9818
0
        case dir_encoding_load:
9819
0
    if (operand_type_check (operand_types[j], anymem)
9820
0
        || t->opcode_modifier.regmem)
9821
0
      goto check_reverse;
9822
0
    break;
9823
9824
0
        case dir_encoding_store:
9825
0
    if (!operand_type_check (operand_types[j], anymem)
9826
0
        && !t->opcode_modifier.regmem)
9827
0
      goto check_reverse;
9828
0
    break;
9829
9830
0
        case dir_encoding_swap:
9831
0
    goto check_reverse;
9832
9833
869
        case dir_encoding_default:
9834
869
    break;
9835
869
        }
9836
9837
    /* If we want store form, we skip the current load.  */
9838
3.85k
    if ((pp.dir_encoding == dir_encoding_store
9839
3.85k
         || pp.dir_encoding == dir_encoding_swap)
9840
0
        && i.mem_operands == 0
9841
0
        && t->opcode_modifier.load)
9842
0
      continue;
9843
    /* Fall through.  */
9844
3.85k
  case 4:
9845
3.85k
  case 5:
9846
3.85k
    overlap1 = operand_type_and (i.types[1], operand_types[1]);
9847
3.85k
    if (!operand_type_match (overlap0, i.types[0])
9848
2.13k
        || !operand_type_match (overlap1, i.types[1])
9849
1.73k
        || ((check_register & 3) == 3
9850
1.22k
      && !operand_type_register_match (i.types[0],
9851
1.22k
               operand_types[0],
9852
1.22k
               i.types[1],
9853
1.22k
               operand_types[1])))
9854
2.11k
      {
9855
2.11k
        specific_error = progress (i.error);
9856
9857
        /* Check if other direction is valid ...  */
9858
2.11k
        if (!t->opcode_modifier.d)
9859
672
    continue;
9860
9861
1.55k
      check_reverse:
9862
1.55k
        if (!(size_match & MATCH_REVERSE))
9863
74
    continue;
9864
        /* Try reversing direction of operands.  */
9865
1.47k
        j = is_cpu (t, CpuFMA4)
9866
1.47k
      || is_cpu (t, CpuXOP)
9867
1.47k
      || is_cpu (t, CpuAPX_F)
9868
1.47k
      || is_cpu (t, CpuAPX_NDD) ? 1 : i.operands - 1;
9869
1.47k
        overlap0 = operand_type_and (i.types[0], operand_types[j]);
9870
1.47k
        overlap1 = operand_type_and (i.types[j], operand_types[0]);
9871
1.47k
        overlap2 = operand_type_and (i.types[1], operand_types[1]);
9872
1.47k
        gas_assert (t->operands != 3 || !check_register
9873
1.47k
        || is_cpu (t, CpuAPX_F) || is_cpu (t, CpuAPX_NDD));
9874
1.47k
        if (!operand_type_match (overlap0, i.types[0])
9875
39
      || !operand_type_match (overlap1, i.types[j])
9876
12
      || (t->operands == 3
9877
0
          && !operand_type_match (overlap2, i.types[1]))
9878
12
      || (check_register
9879
12
          && !operand_type_register_match (i.types[0],
9880
12
                   operand_types[j],
9881
12
                   i.types[j],
9882
12
                   operand_types[0])))
9883
1.46k
    {
9884
      /* Does not match either direction.  */
9885
1.46k
      specific_error = progress (i.error);
9886
1.46k
      continue;
9887
1.46k
    }
9888
        /* found_reverse_match holds which variant of D
9889
     we've found.  */
9890
12
        if (!t->opcode_modifier.d)
9891
0
    found_reverse_match = 0;
9892
12
        else if (operand_types[0].bitfield.tbyte)
9893
0
    {
9894
0
      if (t->opcode_modifier.operandconstraint != UGH)
9895
0
        found_reverse_match = Opcode_FloatD;
9896
0
      else
9897
0
        found_reverse_match = ~0;
9898
      /* FSUB{,R} and FDIV{,R} may need a 2nd bit flipped.  */
9899
0
      if ((t->extension_opcode & 4)
9900
0
          && (intel_syntax || intel_mnemonic))
9901
0
        found_reverse_match |= Opcode_FloatR;
9902
0
    }
9903
12
        else if (is_cpu (t, CpuFMA4) || is_cpu (t, CpuXOP))
9904
0
    {
9905
0
      found_reverse_match = Opcode_VexW;
9906
0
      goto check_operands_345;
9907
0
    }
9908
12
        else if (t->opcode_space == SPACE_MAP4
9909
0
           && t->operands >= 3)
9910
0
    {
9911
0
      found_reverse_match = Opcode_D;
9912
0
      goto check_operands_345;
9913
0
    }
9914
12
        else if (t->opcode_modifier.commutative
9915
           /* CFCMOVcc also wants its major opcode unaltered.  */
9916
12
           || (t->opcode_space == SPACE_MAP4
9917
0
         && (t->base_opcode | 0xf) == 0x4f))
9918
0
    found_reverse_match = ~0;
9919
12
        else if (t->opcode_space != SPACE_BASE
9920
0
           && (t->opcode_space != SPACE_MAP4
9921
         /* MOVBE, originating from SPACE_0F38, also
9922
            belongs here.  */
9923
0
         || t->mnem_off == MN_movbe)
9924
0
           && (t->opcode_space != SPACE_0F
9925
         /* MOV to/from CR/DR/TR, as an exception, follow
9926
            the base opcode space encoding model.  */
9927
0
         || (t->base_opcode | 7) != 0x27))
9928
0
    found_reverse_match = (t->base_opcode & 0xee) != 0x6e
9929
0
              ? Opcode_ExtD : Opcode_SIMD_IntD;
9930
12
        else
9931
12
    found_reverse_match = Opcode_D;
9932
12
      }
9933
1.73k
    else
9934
1.73k
      {
9935
        /* Found a forward 2 operand match here.  */
9936
1.73k
      check_operands_345:
9937
1.73k
        switch (t->operands)
9938
1.73k
    {
9939
0
    case 5:
9940
0
      overlap4 = operand_type_and (i.types[4], operand_types[4]);
9941
0
      if (!operand_type_match (overlap4, i.types[4])
9942
0
          || !operand_type_register_match (i.types[3],
9943
0
                   operand_types[3],
9944
0
                   i.types[4],
9945
0
                   operand_types[4]))
9946
0
        {
9947
0
          specific_error = progress (i.error);
9948
0
          continue;
9949
0
        }
9950
      /* Fall through.  */
9951
0
    case 4:
9952
0
      overlap3 = operand_type_and (i.types[3], operand_types[3]);
9953
0
      if (!operand_type_match (overlap3, i.types[3])
9954
0
          || ((check_register & 0xa) == 0xa
9955
0
        && !operand_type_register_match (i.types[1],
9956
0
                  operand_types[1],
9957
0
                  i.types[3],
9958
0
                  operand_types[3]))
9959
0
          || ((check_register & 0xc) == 0xc
9960
0
        && !operand_type_register_match (i.types[2],
9961
0
                  operand_types[2],
9962
0
                  i.types[3],
9963
0
                  operand_types[3])))
9964
0
        {
9965
0
          specific_error = progress (i.error);
9966
0
          continue;
9967
0
        }
9968
      /* Fall through.  */
9969
136
    case 3:
9970
136
      overlap2 = operand_type_and (i.types[2], operand_types[2]);
9971
136
      if (!operand_type_match (overlap2, i.types[2])
9972
136
          || ((check_register & 5) == 5
9973
136
        && !operand_type_register_match (i.types[0],
9974
136
                  operand_types[0],
9975
136
                  i.types[2],
9976
136
                  operand_types[2]))
9977
136
          || ((check_register & 6) == 6
9978
136
        && !operand_type_register_match (i.types[1],
9979
136
                  operand_types[1],
9980
136
                  i.types[2],
9981
136
                  operand_types[2])))
9982
0
        {
9983
0
          specific_error = progress (i.error);
9984
0
          continue;
9985
0
        }
9986
136
      break;
9987
1.73k
    }
9988
1.73k
      }
9989
    /* Found either forward/reverse 2, 3 or 4 operand match here:
9990
       slip through to break.  */
9991
11.0k
  }
9992
9993
      /* Check if VEX/EVEX encoding requirements can be satisfied.  */
9994
3.36k
      if (VEX_check_encoding (t))
9995
25
  {
9996
25
    specific_error = progress (i.error);
9997
25
    continue;
9998
25
  }
9999
10000
      /* Check if EGPR operands(r16-r31) are valid.  */
10001
3.33k
      if (check_EgprOperands (t))
10002
0
  {
10003
0
    specific_error = progress (i.error);
10004
0
    continue;
10005
0
  }
10006
10007
      /* Check if vector operands are valid.  */
10008
3.33k
      if (check_VecOperands (t))
10009
0
  {
10010
0
    specific_error = progress (i.error);
10011
0
    continue;
10012
0
  }
10013
10014
      /* Check if APX operands are valid.  */
10015
3.33k
      if (check_APX_operands (t))
10016
0
  {
10017
0
    specific_error = progress (i.error);
10018
0
    continue;
10019
0
  }
10020
10021
      /* Check whether to use the shorter VEX encoding for certain insns where
10022
   the EVEX encoding comes first in the table.  This requires the respective
10023
   AVX-* feature to be explicitly enabled.
10024
10025
   Most of the respective insns have just a single EVEX and a single VEX
10026
   template.  The one that's presently different is generated using the
10027
   Vxy / Exy constructs: There are 3 suffix-less EVEX forms, the latter
10028
   two of which may fall back to their two corresponding VEX forms.  */
10029
3.33k
      j = t->mnem_off != MN_vcvtneps2bf16 ? 1 : 2;
10030
3.33k
      if ((t == current_templates.start || j > 1)
10031
438
    && t->opcode_modifier.disp8memshift
10032
0
    && !t->opcode_modifier.vex
10033
0
    && !need_evex_encoding (t)
10034
0
    && t + j < current_templates.end
10035
0
    && t[j].opcode_modifier.vex)
10036
0
  {
10037
0
    i386_cpu_flags cpu;
10038
0
    unsigned int memshift = i.memshift;
10039
10040
0
    i.memshift = 0;
10041
0
    cpu = cpu_flags_and (cpu_flags_from_attr (t[j].cpu),
10042
0
             cpu_arch_isa_flags);
10043
0
    if (!cpu_flags_all_zero (&cpu)
10044
0
        && (!i.types[0].bitfield.disp8
10045
0
      || !operand_type_check (i.types[0], disp)
10046
0
      || i.op[0].disps->X_op != O_constant
10047
0
      || fits_in_disp8 (i.op[0].disps->X_add_number)))
10048
0
      {
10049
0
        specific_error = progress (internal_error);
10050
0
        t += j - 1;
10051
0
        continue;
10052
0
      }
10053
0
    i.memshift = memshift;
10054
0
  }
10055
10056
      /* If we can optimize a NDD insn to legacy insn, like
10057
   add %r16, %r8, %r8 -> add %r16, %r8,
10058
   add  %r8, %r16, %r8 -> add %r16, %r8, then rematch template.
10059
   Note that the semantics have not been changed.  */
10060
3.33k
      if (optimize
10061
0
    && !pp.no_optimize
10062
0
    && pp.encoding != encoding_evex
10063
0
    && ((t + 1 < current_templates.end
10064
0
         && !t[1].opcode_modifier.evex
10065
0
         && t[1].opcode_space <= SPACE_0F38
10066
0
         && t->opcode_modifier.vexvvvv == VexVVVV_DST)
10067
0
        || t->mnem_off == MN_movbe)
10068
0
    && (i.types[i.operands - 1].bitfield.dword
10069
0
        || i.types[i.operands - 1].bitfield.qword))
10070
0
  {
10071
0
    unsigned int match_dest_op = can_convert_NDD_to_legacy (t);
10072
10073
0
    if (match_dest_op != (unsigned int) ~0)
10074
0
      {
10075
0
        size_match = true;
10076
        /* We ensure that the next template has the same input
10077
     operands as the original matching template by the first
10078
     operand (ATT).  To avoid someone support new NDD insns and
10079
     put it in the wrong position.  */
10080
0
        const i386_operand_type *t1_types = get_operand_types (&t[1]);
10081
0
        overlap0 = operand_type_and (i.types[0], t1_types[0]);
10082
0
        if (t->opcode_modifier.d)
10083
0
    overlap1 = operand_type_and (i.types[0], t1_types[1]);
10084
0
        if (!operand_type_match (overlap0, i.types[0])
10085
0
      && (!t->opcode_modifier.d
10086
0
          || !operand_type_match (overlap1, i.types[0])))
10087
0
    size_match = false;
10088
10089
0
        if (size_match
10090
0
      && (t[1].opcode_space <= SPACE_0F
10091
          /* Some non-legacy-map0/1 insns can be shorter when
10092
       legacy-encoded and when no REX prefix is required.  */
10093
0
          || (!check_EgprOperands (t + 1)
10094
0
        && !check_Rex_required ()
10095
0
        && !i.types[i.operands - 1].bitfield.qword)))
10096
0
    {
10097
0
      if (i.operands > 2 && match_dest_op == i.operands - 3)
10098
0
        {
10099
0
          swap_2_operands (match_dest_op, i.operands - 2);
10100
10101
          /* CMOVcc is marked commutative, but then also needs its
10102
       encoded condition inverted.  */
10103
0
          if ((t->base_opcode | 0xf) == 0x4f)
10104
0
      i.invert_cond = true;
10105
0
        }
10106
10107
0
      --i.operands;
10108
0
      --i.reg_operands;
10109
10110
0
      if (t->mnem_off == MN_movbe)
10111
0
        {
10112
0
          gas_assert (t[1].mnem_off == MN_bswap);
10113
0
          ++current_templates.end;
10114
0
        }
10115
10116
0
      specific_error = progress (internal_error);
10117
0
      continue;
10118
0
    }
10119
10120
0
      }
10121
0
  }
10122
10123
      /* We've found a match; break out of loop.  */
10124
3.33k
      break;
10125
3.33k
    }
10126
10127
4.91k
#undef progress
10128
10129
4.91k
  if (t == current_templates.end)
10130
1.46k
    {
10131
      /* We found no match.  */
10132
1.46k
      i.error = specific_error;
10133
1.46k
      return NULL;
10134
1.46k
    }
10135
10136
  /* Don't emit diagnostics or install the template when one was already
10137
     installed, i.e. when called from process_suffix().  */
10138
3.45k
  if (i.tm.mnem_off)
10139
1
    return t;
10140
10141
3.44k
  if (!quiet_warnings)
10142
3.44k
    {
10143
3.44k
      if (!intel_syntax
10144
873
    && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
10145
7
  as_warn (_("indirect %s without `*'"), insn_name (t));
10146
10147
3.44k
      if (t->opcode_modifier.isprefix
10148
28
    && t->opcode_modifier.mnemonicsize == IGNORESIZE)
10149
3
  {
10150
    /* Warn them that a data or address size prefix doesn't
10151
       affect assembly of the next line of code.  */
10152
3
    as_warn (_("stand-alone `%s' prefix"), insn_name (t));
10153
3
  }
10154
10155
3.44k
      if (intel_syntax && mnem_suffix && !t->opcode_modifier.intelsuffix)
10156
76
  {
10157
76
    static bool noticed;
10158
10159
76
    as_warn (_("mnemonic suffix used with `%s'"), insn_name (t));
10160
76
    if (!noticed)
10161
1
      {
10162
1
        noticed = true;
10163
1
        as_warn (_(
10164
1
"NOTE: Such forms are deprecated and will be rejected by a future version of the assembler"));
10165
1
      }
10166
76
  }
10167
3.44k
    }
10168
10169
  /* Copy the template we found.  */
10170
3.44k
  install_template (t);
10171
10172
3.44k
  if (addr_prefix_disp != -1)
10173
2.00k
    i.tm_types[addr_prefix_disp]
10174
2.00k
      = operand_types[addr_prefix_disp];
10175
10176
  /* APX insns acting on byte operands are WIG, yet that can't be expressed
10177
     in the templates (they're also covering word/dword/qword operands).  */
10178
3.44k
  if (t->opcode_space == SPACE_MAP4 && !t->opcode_modifier.vexw &&
10179
1
      i.types[i.operands - 1].bitfield.byte)
10180
0
    {
10181
0
      gas_assert (t->opcode_modifier.w);
10182
0
      i.tm.opcode_modifier.vexw = VEXWIG;
10183
0
    }
10184
10185
3.44k
  switch (found_reverse_match)
10186
3.44k
    {
10187
3.43k
    case 0:
10188
3.43k
      break;
10189
10190
0
    case Opcode_FloatR:
10191
0
    case Opcode_FloatR | Opcode_FloatD:
10192
0
      i.tm.extension_opcode ^= Opcode_FloatR >> 3;
10193
0
      found_reverse_match &= Opcode_FloatD;
10194
10195
      /* Fall through.  */
10196
12
    default:
10197
      /* If we found a reverse match we must alter the opcode direction
10198
   bit and clear/flip the regmem modifier one.  found_reverse_match
10199
   holds bits to change (different for int & float insns).  */
10200
10201
12
      i.tm.base_opcode ^= found_reverse_match;
10202
10203
12
      if (i.tm.opcode_space == SPACE_MAP4)
10204
0
  goto swap_first_2;
10205
10206
      /* Certain SIMD insns have their load forms specified in the opcode
10207
   table, and hence we need to _set_ RegMem instead of clearing it.
10208
   We need to avoid setting the bit though on insns like KMOVW.  */
10209
12
      i.tm.opcode_modifier.regmem
10210
12
  = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
10211
12
    && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
10212
0
    && !i.tm.opcode_modifier.regmem;
10213
10214
      /* Fall through.  */
10215
12
    case ~0:
10216
12
      if (i.tm.opcode_space == SPACE_MAP4
10217
0
    && !t->opcode_modifier.commutative)
10218
0
  i.tm.opcode_modifier.operandconstraint = EVEX_NF;
10219
12
      i.tm_types[0] = operand_types[i.operands - 1];
10220
12
      i.tm_types[i.operands - 1] = operand_types[0];
10221
12
      break;
10222
10223
0
    case Opcode_VexW:
10224
      /* Only the first two register operands need reversing, alongside
10225
   flipping VEX.W.  */
10226
0
      i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
10227
10228
      /* In 3-operand insns XOP.W changes which operand goes into XOP.vvvv.  */
10229
0
      i.tm.opcode_modifier.vexvvvv = VexVVVV_SRC1;
10230
10231
0
    swap_first_2:
10232
0
      j = i.tm_types[0].bitfield.imm8;
10233
0
      i.tm_types[j] = operand_types[j + 1];
10234
0
      i.tm_types[j + 1] = operand_types[j];
10235
0
      break;
10236
3.44k
    }
10237
10238
3.44k
  return t;
10239
3.44k
}
10240
10241
static int
10242
check_string (void)
10243
22
{
10244
22
  unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
10245
22
  unsigned int op = i.tm_types[0].bitfield.baseindex ? es_op : 0;
10246
10247
22
  if (i.seg[op] != NULL && i.seg[op] != reg_es)
10248
0
    {
10249
0
      as_bad (_("`%s' operand %u must use `%ses' segment"),
10250
0
        insn_name (&i.tm),
10251
0
        intel_syntax ? i.tm.operands - es_op : es_op + 1,
10252
0
        register_prefix);
10253
0
      return 0;
10254
0
    }
10255
10256
  /* There's only ever one segment override allowed per instruction.
10257
     This instruction possibly has a legal segment override on the
10258
     second operand, so copy the segment to where non-string
10259
     instructions store it, allowing common code.  */
10260
22
  i.seg[op] = i.seg[1];
10261
10262
22
  return 1;
10263
22
}
10264
10265
static int
10266
process_suffix (const insn_template *t)
10267
3.44k
{
10268
3.44k
  bool is_movx = false;
10269
10270
  /* If matched instruction specifies an explicit instruction mnemonic
10271
     suffix, use it.  */
10272
3.44k
  if (i.tm.opcode_modifier.size == SIZE16)
10273
3
    i.suffix = WORD_MNEM_SUFFIX;
10274
3.44k
  else if (i.tm.opcode_modifier.size == SIZE32)
10275
0
    i.suffix = LONG_MNEM_SUFFIX;
10276
3.44k
  else if (i.tm.opcode_modifier.size == SIZE64)
10277
1
    i.suffix = QWORD_MNEM_SUFFIX;
10278
3.44k
  else if (i.reg_operands
10279
1.54k
     && (i.operands > 1 || i.types[0].bitfield.class == Reg)
10280
1.54k
     && i.tm.opcode_modifier.operandconstraint != ADDR_PREFIX_OP_REG)
10281
1.54k
    {
10282
1.54k
      unsigned int numop = i.operands;
10283
10284
      /* MOVSX/MOVZX */
10285
1.54k
      is_movx = (i.tm.opcode_space == SPACE_0F
10286
27
     && (i.tm.base_opcode | 8) == 0xbe)
10287
1.54k
    || (i.tm.opcode_space == SPACE_BASE
10288
1.51k
        && i.tm.base_opcode == 0x63
10289
0
        && is_cpu (&i.tm, Cpu64));
10290
10291
      /* movsx/movzx want only their source operand considered here, for the
10292
   ambiguity checking below.  The suffix will be replaced afterwards
10293
   to represent the destination (register).  */
10294
1.54k
      if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
10295
0
  --i.operands;
10296
10297
      /* crc32 needs REX.W set regardless of suffix / source operand size.  */
10298
1.54k
      if (i.tm.mnem_off == MN_crc32 && i.tm_types[1].bitfield.qword)
10299
0
        i.rex |= REX_W;
10300
10301
      /* If there's no instruction mnemonic suffix we try to invent one
10302
   based on GPR operands.  */
10303
1.54k
      if (!i.suffix)
10304
1.47k
  {
10305
    /* We take i.suffix from the last register operand specified,
10306
       Destination register type is more significant than source
10307
       register type.  crc32 in SSE4.2 prefers source register
10308
       type. */
10309
1.47k
    unsigned int op = i.tm.mnem_off == MN_crc32 ? 1 : i.operands;
10310
10311
2.10k
    while (op--)
10312
2.07k
      if (i.tm_types[op].bitfield.instance == InstanceNone
10313
11
    || i.tm_types[op].bitfield.instance == Accum)
10314
2.07k
        {
10315
2.07k
    if (i.types[op].bitfield.class != Reg)
10316
631
      continue;
10317
1.44k
    if (i.types[op].bitfield.byte)
10318
8
      i.suffix = BYTE_MNEM_SUFFIX;
10319
1.43k
    else if (i.types[op].bitfield.word)
10320
0
      i.suffix = WORD_MNEM_SUFFIX;
10321
1.43k
    else if (i.types[op].bitfield.dword)
10322
1.43k
      i.suffix = LONG_MNEM_SUFFIX;
10323
7
    else if (i.types[op].bitfield.qword)
10324
7
      i.suffix = QWORD_MNEM_SUFFIX;
10325
0
    else
10326
0
      continue;
10327
1.44k
    break;
10328
1.44k
        }
10329
10330
    /* As an exception, movsx/movzx silently default to a byte source
10331
       in AT&T mode.  */
10332
1.47k
    if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
10333
0
      i.suffix = BYTE_MNEM_SUFFIX;
10334
1.47k
  }
10335
74
      else if (i.suffix == BYTE_MNEM_SUFFIX)
10336
0
  {
10337
0
    if (!check_byte_reg ())
10338
0
      return 0;
10339
0
  }
10340
74
      else if (i.suffix == LONG_MNEM_SUFFIX)
10341
40
  {
10342
40
    if (!check_long_reg ())
10343
37
      return 0;
10344
40
  }
10345
34
      else if (i.suffix == QWORD_MNEM_SUFFIX)
10346
34
  {
10347
34
    if (!check_qword_reg ())
10348
18
      return 0;
10349
34
  }
10350
0
      else if (i.suffix == WORD_MNEM_SUFFIX)
10351
0
  {
10352
0
    if (!check_word_reg ())
10353
0
      return 0;
10354
0
  }
10355
0
      else if (intel_syntax
10356
0
         && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
10357
  /* Do nothing if the instruction is going to ignore the prefix.  */
10358
0
  ;
10359
0
      else
10360
0
  abort ();
10361
10362
      /* Undo the movsx/movzx change done above.  */
10363
1.49k
      i.operands = numop;
10364
1.49k
    }
10365
1.89k
  else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
10366
1.17k
     && !i.suffix)
10367
1.14k
    {
10368
1.14k
      i.suffix = stackop_size;
10369
1.14k
      if (stackop_size == LONG_MNEM_SUFFIX)
10370
5
  {
10371
    /* stackop_size is set to LONG_MNEM_SUFFIX for the
10372
       .code16gcc directive to support 16-bit mode with
10373
       32-bit address.  For IRET without a suffix, generate
10374
       16-bit IRET (opcode 0xcf) to return from an interrupt
10375
       handler.  */
10376
5
    if (i.tm.base_opcode == 0xcf)
10377
0
      {
10378
0
        i.suffix = WORD_MNEM_SUFFIX;
10379
0
        as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
10380
0
      }
10381
    /* Warn about changed behavior for segment register push/pop.  */
10382
5
    else if ((i.tm.base_opcode | 1) == 0x07)
10383
0
      as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
10384
0
         insn_name (&i.tm));
10385
5
  }
10386
1.14k
    }
10387
751
  else if (!i.suffix
10388
555
     && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
10389
555
         || i.tm.opcode_modifier.jump == JUMP_BYTE
10390
555
         || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
10391
555
         || (i.tm.opcode_space == SPACE_0F
10392
268
       && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
10393
263
       && i.tm.extension_opcode <= 3)))
10394
263
    {
10395
263
      switch (flag_code)
10396
263
  {
10397
3
  case CODE_64BIT:
10398
3
    if (!i.tm.opcode_modifier.no_qsuf)
10399
3
      {
10400
3
        if (i.tm.opcode_modifier.jump == JUMP_BYTE
10401
3
      || i.tm.opcode_modifier.no_lsuf)
10402
3
    i.suffix = QWORD_MNEM_SUFFIX;
10403
3
        break;
10404
3
      }
10405
    /* Fall through.  */
10406
260
  case CODE_32BIT:
10407
260
    if (!i.tm.opcode_modifier.no_lsuf)
10408
260
      i.suffix = LONG_MNEM_SUFFIX;
10409
260
    break;
10410
0
  case CODE_16BIT:
10411
0
    if (!i.tm.opcode_modifier.no_wsuf)
10412
0
      i.suffix = WORD_MNEM_SUFFIX;
10413
0
    break;
10414
263
  }
10415
263
    }
10416
10417
3.39k
  if (!i.suffix
10418
1.46k
      && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
10419
    /* Also cover lret/retf/iret in 64-bit mode.  */
10420
1.14k
    || (flag_code == CODE_64BIT
10421
1.14k
        && !i.tm.opcode_modifier.no_lsuf
10422
0
        && !i.tm.opcode_modifier.no_qsuf))
10423
319
      && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
10424
      /* Explicit sizing prefixes are assumed to disambiguate insns.  */
10425
319
      && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
10426
      /* Accept FLDENV et al without suffix.  */
10427
291
      && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
10428
291
    {
10429
291
      unsigned int suffixes, evex = 0;
10430
10431
291
      suffixes = !i.tm.opcode_modifier.no_bsuf;
10432
291
      if (!i.tm.opcode_modifier.no_wsuf)
10433
141
  suffixes |= 1 << 1;
10434
291
      if (!i.tm.opcode_modifier.no_lsuf)
10435
142
  suffixes |= 1 << 2;
10436
291
      if (!i.tm.opcode_modifier.no_ssuf)
10437
1
  suffixes |= 1 << 4;
10438
291
      if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
10439
16
  suffixes |= 1 << 5;
10440
10441
      /* Operand size may be ambiguous only across multiple templates.  Avoid
10442
   the extra effort though if we already know that multiple suffixes /
10443
   operand sizes are allowed.  Also limit this to non-SIMD operand sizes
10444
   (i.e. ones expressable via suffixes) for now.
10445
   There's one special case though that needs excluding: Insns taking
10446
   Disp<N> operands also match templates permitting BaseIndex.  JMP in
10447
   particular would thus wrongly trigger the check further down.  Cover
10448
   JUMP_DWORD insns here as well, just in case.  */
10449
291
      if (i.tm.opcode_modifier.jump != JUMP
10450
270
    && i.tm.opcode_modifier.jump != JUMP_DWORD)
10451
271
  while (!(suffixes & (suffixes - 1)))
10452
129
    {
10453
      /* Sadly check_VecOperands(), running ahead of install_template(),
10454
         may update i.memshift.  Save and restore the value here.  */
10455
129
      unsigned int memshift = i.memshift;
10456
10457
129
      current_templates.start = t + 1;
10458
129
      t = match_template (0);
10459
129
      i.memshift = memshift;
10460
129
      if (t == NULL)
10461
128
        break;
10462
1
      if (!t->opcode_modifier.no_bsuf)
10463
0
        suffixes |= 1 << 0;
10464
1
      if (!t->opcode_modifier.no_wsuf)
10465
0
        suffixes |= 1 << 1;
10466
1
      if (!t->opcode_modifier.no_lsuf)
10467
1
        suffixes |= 1 << 2;
10468
1
      if (!t->opcode_modifier.no_ssuf)
10469
0
        suffixes |= 1 << 4;
10470
1
      if (flag_code == CODE_64BIT && !t->opcode_modifier.no_qsuf)
10471
0
        suffixes |= 1 << 5;
10472
1
    }
10473
10474
      /* For [XYZ]MMWORD operands inspect operand sizes.  While generally
10475
   also suitable for AT&T syntax mode, it was requested that this be
10476
   restricted to just Intel syntax.  */
10477
291
      if (intel_syntax && is_any_vex_encoding (&i.tm)
10478
3
    && !i.broadcast.type && !i.broadcast.bytes)
10479
3
  {
10480
3
    unsigned int op;
10481
10482
6
    for (op = 0; op < i.tm.operands; ++op)
10483
3
      {
10484
3
        if (vector_size < VSZ512)
10485
3
    {
10486
3
      i.tm_types[op].bitfield.zmmword = 0;
10487
3
      if (vector_size < VSZ256)
10488
0
        {
10489
0
          i.tm_types[op].bitfield.ymmword = 0;
10490
0
          if (i.tm_types[op].bitfield.xmmword
10491
0
        && i.tm.opcode_modifier.evex == EVEXDYN)
10492
0
      i.tm.opcode_modifier.evex = EVEX128;
10493
0
        }
10494
3
      else if (i.tm_types[op].bitfield.ymmword
10495
0
         && !i.tm_types[op].bitfield.xmmword
10496
0
         && i.tm.opcode_modifier.evex == EVEXDYN)
10497
0
        i.tm.opcode_modifier.evex = EVEX256;
10498
3
    }
10499
0
        else if (i.tm.opcode_modifier.evex
10500
0
           && !cpu_arch_flags.bitfield.cpuavx512vl)
10501
0
    {
10502
0
      if (i.tm_types[op].bitfield.ymmword)
10503
0
        i.tm_types[op].bitfield.xmmword = 0;
10504
0
      if (i.tm_types[op].bitfield.zmmword)
10505
0
        i.tm_types[op].bitfield.ymmword = 0;
10506
0
      if (i.tm.opcode_modifier.evex == EVEXDYN)
10507
0
        i.tm.opcode_modifier.evex = EVEX512;
10508
0
    }
10509
10510
3
        if (i.tm_types[op].bitfield.xmmword
10511
3
      + i.tm_types[op].bitfield.ymmword
10512
3
      + i.tm_types[op].bitfield.zmmword < 2)
10513
3
    continue;
10514
10515
        /* Any properly sized operand disambiguates the insn.  */
10516
0
        if (i.types[op].bitfield.xmmword
10517
0
      || i.types[op].bitfield.ymmword
10518
0
      || i.types[op].bitfield.zmmword)
10519
0
    {
10520
0
      suffixes &= ~(7 << 6);
10521
0
      evex = 0;
10522
0
      break;
10523
0
    }
10524
10525
0
        if ((i.flags[op] & Operand_Mem)
10526
0
      && i.tm_types[op].bitfield.unspecified)
10527
0
    {
10528
0
      if (i.tm_types[op].bitfield.xmmword)
10529
0
        suffixes |= 1 << 6;
10530
0
      if (i.tm_types[op].bitfield.ymmword)
10531
0
        suffixes |= 1 << 7;
10532
0
      if (i.tm_types[op].bitfield.zmmword)
10533
0
        suffixes |= 1 << 8;
10534
0
      if (i.tm.opcode_modifier.evex)
10535
0
        evex = EVEX512;
10536
0
    }
10537
0
      }
10538
3
  }
10539
10540
      /* Are multiple suffixes / operand sizes allowed?  */
10541
291
      if (suffixes & (suffixes - 1))
10542
142
  {
10543
142
    if (intel_syntax
10544
6
        && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
10545
0
      || operand_check == check_error))
10546
6
      {
10547
6
        as_bad (_("ambiguous operand size for `%s'"), insn_name (&i.tm));
10548
6
        return 0;
10549
6
      }
10550
136
    if (operand_check == check_error)
10551
0
      {
10552
0
        as_bad (_("no instruction mnemonic suffix given and "
10553
0
      "no register operands; can't size `%s'"), insn_name (&i.tm));
10554
0
        return 0;
10555
0
      }
10556
136
    if (operand_check == check_warning)
10557
136
      as_warn (_("%s; using default for `%s'"),
10558
136
           intel_syntax
10559
136
           ? _("ambiguous operand size")
10560
136
           : _("no instruction mnemonic suffix given and "
10561
136
         "no register operands"),
10562
136
           insn_name (&i.tm));
10563
10564
136
    if (i.tm.opcode_modifier.floatmf)
10565
1
      i.suffix = SHORT_MNEM_SUFFIX;
10566
135
    else if (is_movx)
10567
0
      /* handled below */;
10568
135
    else if (evex)
10569
0
      i.tm.opcode_modifier.evex = evex;
10570
135
    else if (flag_code == CODE_16BIT)
10571
125
      i.suffix = WORD_MNEM_SUFFIX;
10572
10
    else if (!i.tm.opcode_modifier.no_lsuf)
10573
10
      i.suffix = LONG_MNEM_SUFFIX;
10574
0
    else
10575
0
      i.suffix = QWORD_MNEM_SUFFIX;
10576
136
  }
10577
291
    }
10578
10579
3.38k
  if (is_movx)
10580
0
    {
10581
      /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
10582
   In AT&T syntax, if there is no suffix (warned about above), the default
10583
   will be byte extension.  */
10584
0
      if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
10585
0
  i.tm.base_opcode |= 1;
10586
10587
      /* For further processing, the suffix should represent the destination
10588
   (register).  This is already the case when one was used with
10589
   mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
10590
   no suffix to begin with.  */
10591
0
      if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
10592
0
  {
10593
0
    if (i.types[1].bitfield.word)
10594
0
      i.suffix = WORD_MNEM_SUFFIX;
10595
0
    else if (i.types[1].bitfield.qword)
10596
0
      i.suffix = QWORD_MNEM_SUFFIX;
10597
0
    else
10598
0
      i.suffix = LONG_MNEM_SUFFIX;
10599
10600
0
    i.tm.opcode_modifier.w = 0;
10601
0
  }
10602
0
    }
10603
10604
3.38k
  if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
10605
143
    i.short_form = (i.tm_types[0].bitfield.class == Reg)
10606
143
       != (i.tm_types[1].bitfield.class == Reg);
10607
10608
  /* Change the opcode based on the operand size given by i.suffix.  */
10609
3.38k
  switch (i.suffix)
10610
3.38k
    {
10611
    /* Size floating point instruction.  */
10612
1.84k
    case LONG_MNEM_SUFFIX:
10613
1.84k
      if (i.tm.opcode_modifier.floatmf)
10614
0
  {
10615
0
    i.tm.base_opcode ^= 4;
10616
0
    break;
10617
0
  }
10618
    /* fall through */
10619
1.99k
    case WORD_MNEM_SUFFIX:
10620
2.04k
    case QWORD_MNEM_SUFFIX:
10621
      /* It's not a byte, select word/dword operation.  */
10622
2.04k
      if (i.tm.opcode_modifier.w)
10623
1.35k
  {
10624
1.35k
    if (i.short_form)
10625
0
      i.tm.base_opcode |= 8;
10626
1.35k
    else
10627
1.35k
      i.tm.base_opcode |= 1;
10628
1.35k
  }
10629
10630
      /* Set mode64 for an operand.  */
10631
2.04k
      if (i.suffix == QWORD_MNEM_SUFFIX)
10632
50
  {
10633
50
    if (flag_code == CODE_64BIT
10634
50
        && !i.tm.opcode_modifier.norex64
10635
47
        && !i.tm.opcode_modifier.vexw
10636
        /* Special case for xchg %rax,%rax.  It is NOP and doesn't
10637
     need rex64. */
10638
47
        && ! (i.operands == 2
10639
46
        && i.tm.base_opcode == 0x90
10640
0
        && i.tm.opcode_space == SPACE_BASE
10641
0
        && i.types[0].bitfield.instance == Accum
10642
0
        && i.types[1].bitfield.instance == Accum))
10643
47
      i.rex |= REX_W;
10644
10645
50
    break;
10646
50
  }
10647
10648
    /* fall through */
10649
1.99k
    case SHORT_MNEM_SUFFIX:
10650
      /* Now select between word & dword operations via the operand
10651
   size prefix, except for instructions that will ignore this
10652
   prefix anyway.  */
10653
1.99k
      if (i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
10654
1.99k
    && !i.tm.opcode_modifier.floatmf
10655
1.99k
    && (!is_any_vex_encoding (&i.tm)
10656
1
        || i.tm.opcode_space == SPACE_MAP4)
10657
1.99k
    && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
10658
1.40k
        || (flag_code == CODE_64BIT
10659
1.01k
      && i.tm.opcode_modifier.jump == JUMP_BYTE)))
10660
591
  {
10661
591
    unsigned int prefix = DATA_PREFIX_OPCODE;
10662
10663
591
    if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
10664
0
      prefix = ADDR_PREFIX_OPCODE;
10665
10666
    /* The DATA PREFIX of EVEX promoted from legacy APX instructions
10667
       needs to be adjusted.  */
10668
591
    if (i.tm.opcode_space == SPACE_MAP4)
10669
0
      {
10670
0
        gas_assert (!i.tm.opcode_modifier.opcodeprefix);
10671
0
        i.tm.opcode_modifier.opcodeprefix = PREFIX_0X66;
10672
0
      }
10673
591
    else if (!add_prefix (prefix))
10674
0
      return 0;
10675
591
  }
10676
10677
1.99k
      break;
10678
10679
1.99k
    case 0:
10680
      /* Select word/dword/qword operation with explicit data sizing prefix
10681
   when there are no suitable register operands.  */
10682
1.32k
      if (i.tm.opcode_modifier.w
10683
23
    && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
10684
23
    && (!i.reg_operands
10685
0
        || (i.reg_operands == 1
10686
          /* ShiftCount */
10687
0
      && (i.tm_types[0].bitfield.instance == RegC
10688
          /* InOutPortReg */
10689
0
          || i.tm_types[0].bitfield.instance == RegD
10690
0
          || i.tm_types[1].bitfield.instance == RegD
10691
0
          || i.tm.mnem_off == MN_crc32))))
10692
23
  i.tm.base_opcode |= 1;
10693
1.32k
      break;
10694
3.38k
    }
10695
10696
3.38k
  if (i.tm.opcode_modifier.operandconstraint == ADDR_PREFIX_OP_REG)
10697
0
    {
10698
0
      gas_assert (!i.suffix);
10699
0
      gas_assert (i.reg_operands);
10700
10701
0
      if (i.tm_types[0].bitfield.instance == Accum
10702
0
    || i.operands == 1)
10703
0
  {
10704
    /* The address size override prefix changes the size of the
10705
       first operand.  */
10706
0
    if (flag_code == CODE_64BIT
10707
0
        && i.types[0].bitfield.word)
10708
0
      {
10709
0
        as_bad (_("16-bit addressing unavailable for `%s'"),
10710
0
          insn_name (&i.tm));
10711
0
        return 0;
10712
0
      }
10713
10714
0
    if ((flag_code == CODE_32BIT
10715
0
         ? i.types[0].bitfield.word
10716
0
         : i.types[0].bitfield.dword)
10717
0
        && !add_prefix (ADDR_PREFIX_OPCODE))
10718
0
      return 0;
10719
0
  }
10720
0
      else
10721
0
  {
10722
    /* Check invalid register operand when the address size override
10723
       prefix changes the size of register operands.  */
10724
0
    unsigned int op;
10725
0
    enum { need_word, need_dword, need_qword } need;
10726
10727
    /* Check the register operand for the address size prefix if
10728
       the memory operand has no real registers, like symbol, DISP
10729
       or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant.  */
10730
0
    if (i.mem_operands == 1
10731
0
        && i.reg_operands == 1
10732
0
        && i.operands == 2
10733
0
        && i.types[1].bitfield.class == Reg
10734
0
        && (flag_code == CODE_32BIT
10735
0
      ? i.types[1].bitfield.word
10736
0
      : i.types[1].bitfield.dword)
10737
0
        && ((i.base_reg == NULL && i.index_reg == NULL)
10738
0
#ifdef OBJ_ELF
10739
0
      || (x86_elf_abi == X86_64_X32_ABI
10740
0
          && i.base_reg
10741
0
          && i.base_reg->reg_num == RegIP
10742
0
          && i.base_reg->reg_type.bitfield.qword))
10743
#else
10744
      || 0)
10745
#endif
10746
0
        && !add_prefix (ADDR_PREFIX_OPCODE))
10747
0
      return 0;
10748
10749
0
    if (flag_code == CODE_32BIT)
10750
0
      need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
10751
0
    else if (i.prefix[ADDR_PREFIX])
10752
0
      need = need_dword;
10753
0
    else
10754
0
      need = flag_code == CODE_64BIT ? need_qword : need_word;
10755
10756
0
    for (op = i.imm_operands; op < i.operands; op++)
10757
0
      {
10758
0
        if (i.types[op].bitfield.class != Reg)
10759
0
    continue;
10760
10761
0
        switch (need)
10762
0
    {
10763
0
    case need_word:
10764
0
      if (i.types[op].bitfield.word)
10765
0
        continue;
10766
0
      break;
10767
0
    case need_dword:
10768
0
      if (i.types[op].bitfield.dword)
10769
0
        continue;
10770
0
      break;
10771
0
    case need_qword:
10772
0
      if (i.types[op].bitfield.qword)
10773
0
        continue;
10774
0
      break;
10775
0
    }
10776
10777
0
        as_bad (_("invalid register operand size for `%s'"),
10778
0
          insn_name (&i.tm));
10779
0
        return 0;
10780
0
      }
10781
0
  }
10782
0
    }
10783
10784
3.38k
  return 1;
10785
3.38k
}
10786
10787
static int
10788
check_byte_reg (void)
10789
0
{
10790
0
  int op;
10791
10792
0
  for (op = i.operands; --op >= 0;)
10793
0
    {
10794
      /* Skip non-register operands. */
10795
0
      if (i.types[op].bitfield.class != Reg)
10796
0
  continue;
10797
10798
      /* If this is an eight bit register, it's OK.  */
10799
0
      if (i.types[op].bitfield.byte)
10800
0
  {
10801
0
    if (i.tm.opcode_modifier.checkoperandsize)
10802
0
      break;
10803
0
    continue;
10804
0
  }
10805
10806
      /* I/O port address operands are OK too.  */
10807
0
      if (i.tm_types[op].bitfield.instance == RegD
10808
0
    && i.tm_types[op].bitfield.word)
10809
0
  continue;
10810
10811
      /* crc32 only wants its source operand checked here.  */
10812
0
      if (i.tm.mnem_off == MN_crc32 && op != 0)
10813
0
  continue;
10814
10815
      /* Any other register is bad.  */
10816
0
      as_bad (_("`%s%s' not allowed with `%s%c'"),
10817
0
        register_prefix, i.op[op].regs->reg_name,
10818
0
        insn_name (&i.tm), i.suffix);
10819
0
      return 0;
10820
0
    }
10821
0
  return 1;
10822
0
}
10823
10824
static int
10825
check_long_reg (void)
10826
40
{
10827
40
  int op;
10828
10829
77
  for (op = i.operands; --op >= 0;)
10830
    /* Skip non-register operands. */
10831
77
    if (i.types[op].bitfield.class != Reg)
10832
37
      continue;
10833
    /* Reject eight bit registers, except where the template requires
10834
       them. (eg. movzb)  */
10835
40
    else if (i.types[op].bitfield.byte
10836
0
       && (i.tm_types[op].bitfield.word
10837
0
     || i.tm_types[op].bitfield.dword
10838
0
     || i.tm_types[op].bitfield.qword))
10839
0
      {
10840
0
  as_bad (_("`%s%s' not allowed with `%s%c'"),
10841
0
    register_prefix,
10842
0
    i.op[op].regs->reg_name,
10843
0
    insn_name (&i.tm),
10844
0
    i.suffix);
10845
0
  return 0;
10846
0
      }
10847
    /* Error if the e prefix on a general reg is missing, or if the r
10848
       prefix on a general reg is present.  */
10849
40
    else if ((i.types[op].bitfield.word
10850
40
        || i.types[op].bitfield.qword)
10851
37
       && i.tm_types[op].bitfield.dword)
10852
37
      {
10853
37
  as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
10854
37
    register_prefix, i.op[op].regs->reg_name,
10855
37
    i.suffix);
10856
37
  return 0;
10857
37
      }
10858
3
    else if (i.tm.opcode_modifier.checkoperandsize)
10859
3
      break;
10860
10861
3
  return 1;
10862
40
}
10863
10864
static int
10865
check_qword_reg (void)
10866
34
{
10867
34
  int op;
10868
10869
68
  for (op = i.operands; --op >= 0; )
10870
    /* Skip non-register operands. */
10871
68
    if (i.types[op].bitfield.class != Reg)
10872
34
      continue;
10873
    /* Reject eight bit registers, except where the template requires
10874
       them. (eg. movzb)  */
10875
34
    else if (i.types[op].bitfield.byte
10876
18
       && (i.tm_types[op].bitfield.word
10877
0
     || i.tm_types[op].bitfield.dword
10878
0
     || i.tm_types[op].bitfield.qword))
10879
18
      {
10880
18
  as_bad (_("`%s%s' not allowed with `%s%c'"),
10881
18
    register_prefix,
10882
18
    i.op[op].regs->reg_name,
10883
18
    insn_name (&i.tm),
10884
18
    i.suffix);
10885
18
  return 0;
10886
18
      }
10887
    /* Error if the r prefix on a general reg is missing.  */
10888
16
    else if ((i.types[op].bitfield.word
10889
16
        || i.types[op].bitfield.dword)
10890
0
       && i.tm_types[op].bitfield.qword)
10891
0
      {
10892
0
  as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
10893
0
    register_prefix, i.op[op].regs->reg_name, i.suffix);
10894
0
  return 0;
10895
0
      }
10896
16
    else if (i.tm.opcode_modifier.checkoperandsize)
10897
16
      break;
10898
10899
16
  return 1;
10900
34
}
10901
10902
static int
10903
check_word_reg (void)
10904
0
{
10905
0
  int op;
10906
0
  for (op = i.operands; --op >= 0;)
10907
    /* Skip non-register operands. */
10908
0
    if (i.types[op].bitfield.class != Reg)
10909
0
      continue;
10910
    /* Reject eight bit registers, except where the template requires
10911
       them. (eg. movzb)  */
10912
0
    else if (i.types[op].bitfield.byte
10913
0
       && (i.tm_types[op].bitfield.word
10914
0
     || i.tm_types[op].bitfield.dword
10915
0
     || i.tm_types[op].bitfield.qword))
10916
0
      {
10917
0
  as_bad (_("`%s%s' not allowed with `%s%c'"),
10918
0
    register_prefix,
10919
0
    i.op[op].regs->reg_name,
10920
0
    insn_name (&i.tm),
10921
0
    i.suffix);
10922
0
  return 0;
10923
0
      }
10924
    /* Error if the e or r prefix on a general reg is present.  */
10925
0
    else if ((i.types[op].bitfield.dword
10926
0
     || i.types[op].bitfield.qword)
10927
0
       && i.tm_types[op].bitfield.word)
10928
0
      {
10929
0
  as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
10930
0
    register_prefix, i.op[op].regs->reg_name,
10931
0
    i.suffix);
10932
0
  return 0;
10933
0
      }
10934
0
    else if (i.tm.opcode_modifier.checkoperandsize)
10935
0
      break;
10936
10937
0
  return 1;
10938
0
}
10939
10940
static int
10941
update_imm (unsigned int j)
10942
4.94k
{
10943
4.94k
  i386_operand_type overlap = i.types[j];
10944
10945
4.94k
  if (i.tm_types[j].bitfield.imm8
10946
314
      && i.tm_types[j].bitfield.imm8s
10947
0
      && overlap.bitfield.imm8 && overlap.bitfield.imm8s)
10948
0
    {
10949
      /* This combination is used on 8-bit immediates where e.g. $~0 is
10950
   desirable to permit.  We're past operand type matching, so simply
10951
   put things back in the shape they were before introducing the
10952
   distinction between Imm8, Imm8S, and Imm8|Imm8S.  */
10953
0
      overlap.bitfield.imm8s = 0;
10954
0
    }
10955
10956
4.94k
  if (overlap.bitfield.imm8
10957
4.94k
      + overlap.bitfield.imm8s
10958
4.94k
      + overlap.bitfield.imm16
10959
4.94k
      + overlap.bitfield.imm32
10960
4.94k
      + overlap.bitfield.imm32s
10961
4.94k
      + overlap.bitfield.imm64 > 1)
10962
1.27k
    {
10963
1.27k
      static const i386_operand_type imm16 = { .bitfield = { .imm16 = 1 } };
10964
1.27k
      static const i386_operand_type imm32 = { .bitfield = { .imm32 = 1 } };
10965
1.27k
      static const i386_operand_type imm32s = { .bitfield = { .imm32s = 1 } };
10966
1.27k
      static const i386_operand_type imm16_32 = { .bitfield =
10967
1.27k
  { .imm16 = 1, .imm32 = 1 }
10968
1.27k
      };
10969
1.27k
      static const i386_operand_type imm16_32s =  { .bitfield =
10970
1.27k
  { .imm16 = 1, .imm32s = 1 }
10971
1.27k
      };
10972
1.27k
      static const i386_operand_type imm16_32_32s = { .bitfield =
10973
1.27k
  { .imm16 = 1, .imm32 = 1, .imm32s = 1 }
10974
1.27k
      };
10975
10976
1.27k
      if (i.suffix)
10977
161
  {
10978
161
    i386_operand_type temp;
10979
10980
161
    operand_type_set (&temp, 0);
10981
161
    if (i.suffix == BYTE_MNEM_SUFFIX)
10982
4
      {
10983
4
        temp.bitfield.imm8 = overlap.bitfield.imm8;
10984
4
        temp.bitfield.imm8s = overlap.bitfield.imm8s;
10985
4
      }
10986
157
    else if (i.suffix == WORD_MNEM_SUFFIX)
10987
125
      temp.bitfield.imm16 = overlap.bitfield.imm16;
10988
32
    else if (i.suffix == QWORD_MNEM_SUFFIX)
10989
23
      {
10990
23
        temp.bitfield.imm64 = overlap.bitfield.imm64;
10991
23
        temp.bitfield.imm32s = overlap.bitfield.imm32s;
10992
23
      }
10993
9
    else
10994
9
      temp.bitfield.imm32 = overlap.bitfield.imm32;
10995
161
    overlap = temp;
10996
161
  }
10997
1.11k
      else if (operand_type_equal (&overlap, &imm16_32_32s)
10998
1.11k
         || operand_type_equal (&overlap, &imm16_32)
10999
1.11k
         || operand_type_equal (&overlap, &imm16_32s))
11000
1.11k
  {
11001
1.11k
    if ((flag_code == CODE_16BIT)
11002
1.11k
        ^ (i.prefix[DATA_PREFIX] != 0 && !(i.prefix[REX_PREFIX] & REX_W)))
11003
0
      overlap = imm16;
11004
1.11k
    else
11005
1.11k
      overlap = imm32s;
11006
1.11k
  }
11007
0
      else if (i.prefix[REX_PREFIX] & REX_W)
11008
0
  overlap = operand_type_and (overlap, imm32s);
11009
0
      else if (i.prefix[DATA_PREFIX])
11010
0
  overlap = operand_type_and (overlap,
11011
0
            flag_code != CODE_16BIT ? imm16 : imm32);
11012
1.27k
      if (overlap.bitfield.imm8
11013
1.27k
    + overlap.bitfield.imm8s
11014
1.27k
    + overlap.bitfield.imm16
11015
1.27k
    + overlap.bitfield.imm32
11016
1.27k
    + overlap.bitfield.imm32s
11017
1.27k
    + overlap.bitfield.imm64 != 1)
11018
0
  {
11019
0
    as_bad (_("no instruction mnemonic suffix given; "
11020
0
        "can't determine immediate size"));
11021
0
    return 0;
11022
0
  }
11023
1.27k
    }
11024
4.94k
  i.types[j] = overlap;
11025
11026
4.94k
  return 1;
11027
4.94k
}
11028
11029
static int
11030
finalize_imm (void)
11031
3.38k
{
11032
3.38k
  unsigned int j, n;
11033
11034
  /* Update the first 2 immediate operands.  */
11035
3.38k
  n = i.operands > 2 ? 2 : i.operands;
11036
3.38k
  if (n)
11037
3.27k
    {
11038
8.21k
      for (j = 0; j < n; j++)
11039
4.94k
  if (update_imm (j) == 0)
11040
0
    return 0;
11041
11042
      /* The 3rd operand can't be immediate operand.  */
11043
3.27k
      gas_assert (operand_type_check (i.types[2], imm) == 0);
11044
3.27k
    }
11045
11046
3.38k
  return 1;
11047
3.38k
}
11048
11049
static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
11050
         bool do_sse2avx)
11051
2.60k
{
11052
2.60k
  if (r->reg_flags & RegRex)
11053
0
    {
11054
0
      if (i.rex & rex_bit)
11055
0
  as_bad (_("same type of prefix used twice"));
11056
0
      i.rex |= rex_bit;
11057
0
    }
11058
2.60k
  else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
11059
0
    {
11060
0
      gas_assert (i.vex.register_specifier == r);
11061
0
      i.vex.register_specifier += 8;
11062
0
    }
11063
11064
2.60k
  if (r->reg_flags & RegVRex)
11065
0
    i.vrex |= rex_bit;
11066
11067
2.60k
  if (r->reg_flags & RegRex2)
11068
0
    i.rex2 |= rex_bit;
11069
2.60k
}
11070
11071
static INLINE void
11072
set_rex_rex2 (const reg_entry *r, unsigned int rex_bit)
11073
147
{
11074
147
  if ((r->reg_flags & RegRex) != 0)
11075
0
    i.rex |= rex_bit;
11076
147
  if ((r->reg_flags & RegRex2) != 0)
11077
3
    i.rex2 |= rex_bit;
11078
147
}
11079
11080
static int
11081
process_operands (void)
11082
4.08k
{
11083
  /* Default segment register this instruction will use for memory
11084
     accesses.  0 means unknown.  This is only for optimizing out
11085
     unnecessary segment overrides.  */
11086
4.08k
  const reg_entry *default_seg = NULL;
11087
11088
8.01k
  for (unsigned int j = i.imm_operands; j < i.operands; j++)
11089
3.92k
    if (i.types[j].bitfield.instance != InstanceNone)
11090
11
      i.reg_operands--;
11091
11092
4.08k
  if (i.tm.opcode_modifier.sse2avx)
11093
0
    {
11094
      /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
11095
   need converting.  */
11096
0
      i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
11097
0
      i.prefix[REX_PREFIX] = 0;
11098
0
      pp.rex_encoding = 0;
11099
0
      pp.rex2_encoding = 0;
11100
0
    }
11101
  /* ImmExt should be processed after SSE2AVX.  */
11102
4.08k
  else if (i.tm.opcode_modifier.immext)
11103
0
    process_immext ();
11104
11105
  /* TILEZERO is unusual in that it has a single operand encoded in ModR/M.reg,
11106
     not ModR/M.rm.  To avoid special casing this in build_modrm_byte(), fake a
11107
     new destination operand here, while converting the source one to register
11108
     number 0.  */
11109
4.08k
  if (i.tm.mnem_off == MN_tilezero)
11110
0
    {
11111
0
      copy_operand (1, 0);
11112
0
      i.op[0].regs -= i.op[0].regs->reg_num;
11113
0
      i.operands++;
11114
0
      i.reg_operands++;
11115
0
      i.tm.operands++;
11116
0
    }
11117
11118
4.08k
  if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
11119
0
    {
11120
0
      static const i386_operand_type regxmm = {
11121
0
        .bitfield = { .class = RegSIMD, .xmmword = 1 }
11122
0
      };
11123
0
      unsigned int dupl = i.operands;
11124
0
      unsigned int dest = dupl - 1;
11125
0
      unsigned int j;
11126
11127
      /* The destination must be an xmm register.  */
11128
0
      gas_assert (i.reg_operands
11129
0
      && MAX_OPERANDS > dupl
11130
0
      && operand_type_equal (&i.types[dest], &regxmm));
11131
11132
0
      if (i.tm_types[0].bitfield.instance == Accum
11133
0
    && i.tm_types[0].bitfield.xmmword)
11134
0
  {
11135
    /* Keep xmm0 for instructions with VEX prefix and 3
11136
       sources.  */
11137
0
    i.tm_types[0].bitfield.instance = InstanceNone;
11138
0
    i.tm_types[0].bitfield.class = RegSIMD;
11139
0
    i.reg_operands++;
11140
0
    goto duplicate;
11141
0
  }
11142
11143
0
      if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_1ST_XMM0)
11144
0
  {
11145
0
    gas_assert ((MAX_OPERANDS - 1) > dupl);
11146
11147
    /* Add the implicit xmm0 for instructions with VEX prefix
11148
       and 3 sources.  */
11149
0
    for (j = i.operands; j > 0; j--)
11150
0
      copy_operand (j, j - 1);
11151
0
    i.op[0].regs = str_hash_find (reg_hash, "xmm0");
11152
0
    i.types[0] = regxmm;
11153
0
    i.tm_types[0] = regxmm;
11154
11155
0
    i.operands += 2;
11156
0
    i.reg_operands += 2;
11157
0
    i.tm.operands += 2;
11158
11159
0
    dupl++;
11160
0
    dest++;
11161
0
  }
11162
0
      else
11163
0
  {
11164
0
  duplicate:
11165
0
    i.operands++;
11166
0
    i.reg_operands++;
11167
0
    i.tm.operands++;
11168
0
  }
11169
11170
0
      copy_operand (dupl, dest);
11171
11172
0
      if (i.tm.opcode_modifier.immext)
11173
0
  process_immext ();
11174
0
    }
11175
4.08k
  else if (i.tm_types[0].bitfield.instance == Accum
11176
0
     && i.tm.opcode_modifier.modrm)
11177
0
    {
11178
0
      unsigned int j;
11179
11180
0
      for (j = 1; j < i.operands; j++)
11181
0
  copy_operand (j - 1, j);
11182
11183
      /* No adjustment to i.reg_operands: This was already done at the top
11184
   of the function.  */
11185
0
      i.operands--;
11186
0
      i.tm.operands--;
11187
0
    }
11188
4.08k
  else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_GROUP)
11189
0
    {
11190
0
      unsigned int op, extra;
11191
0
      const reg_entry *first;
11192
11193
      /* The second operand must be {x,y,z,t}mmN.  */
11194
0
      gas_assert ((i.operands == 2 || i.operands == 3)
11195
0
      && i.types[1].bitfield.class == RegSIMD);
11196
11197
0
      switch (i.types[i.operands - 1].bitfield.class)
11198
0
  {
11199
0
  case RegSIMD:
11200
0
    op = 1;
11201
0
    if (i.operands == 2)
11202
0
      {
11203
        /* AMX-TRANSPOSE operand 2: N must be a multiple of 2. */
11204
0
        extra = 1;
11205
0
      }
11206
0
    else
11207
0
      {
11208
        /* AVX512-{4FMAPS,4VNNIW} operand 2: N must be a multiple of 4. */
11209
0
        extra = 3;
11210
0
      }
11211
0
    break;
11212
11213
0
  case RegMask:
11214
    /* AVX512-VP2INTERSECT operand 3: N must be a multiple of 2. */
11215
0
    op = 2;
11216
0
    extra = 1;
11217
0
    break;
11218
11219
0
  default:
11220
0
    abort ();
11221
0
  }
11222
11223
0
      first = i.op[op].regs - (register_number (i.op[op].regs) & extra);
11224
0
      if (i.op[op].regs != first)
11225
0
  as_warn (_("operand %u `%s%s' implicitly denotes"
11226
0
       " `%s%s' to `%s%s' group in `%s'"),
11227
0
     intel_syntax ? i.operands - op : op + 1,
11228
0
     register_prefix, i.op[op].regs->reg_name,
11229
0
     register_prefix, first[0].reg_name,
11230
0
     register_prefix, first[extra].reg_name,
11231
0
     insn_name (&i.tm));
11232
0
    }
11233
4.08k
  else if (i.tm.opcode_modifier.operandconstraint == REG_KLUDGE)
11234
5
    {
11235
      /* The imul $imm, %reg instruction is converted into
11236
   imul $imm, %reg, %reg, and the clr %reg instruction
11237
   is converted into xor %reg, %reg.  */
11238
11239
5
      unsigned int first_reg_op;
11240
11241
5
      if (operand_type_check (i.types[0], reg))
11242
0
  first_reg_op = 0;
11243
5
      else
11244
5
  first_reg_op = 1;
11245
      /* Pretend we saw the extra register operand.  */
11246
5
      gas_assert (i.reg_operands == 1
11247
5
      && i.op[first_reg_op + 1].regs == 0);
11248
5
      i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
11249
5
      i.types[first_reg_op + 1] = i.types[first_reg_op];
11250
5
      i.operands++;
11251
5
      i.reg_operands++;
11252
11253
      /* For IMULZU switch around the constraint.  */
11254
5
      if (i.tm.mnem_off == MN_imulzu)
11255
0
  i.tm.opcode_modifier.operandconstraint = ZERO_UPPER;
11256
5
    }
11257
11258
4.08k
  if (i.tm.opcode_modifier.modrm)
11259
2.20k
    {
11260
      /* The opcode is completed (modulo i.tm.extension_opcode which
11261
   must be put into the modrm byte).  Now, we make the modrm and
11262
   index base bytes based on all the info we've collected.  */
11263
11264
2.20k
      default_seg = build_modrm_byte ();
11265
11266
2.20k
      if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
11267
0
  {
11268
    /* Warn about some common errors, but press on regardless.  */
11269
0
    if (i.operands == 2)
11270
0
      {
11271
        /* Reversed arguments on faddp or fmulp.  */
11272
0
        as_warn (_("translating to `%s %s%s,%s%s'"), insn_name (&i.tm),
11273
0
           register_prefix, i.op[!intel_syntax].regs->reg_name,
11274
0
           register_prefix, i.op[intel_syntax].regs->reg_name);
11275
0
      }
11276
0
    else if (i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
11277
0
      {
11278
        /* Extraneous `l' suffix on fp insn.  */
11279
0
        as_warn (_("translating to `%s %s%s'"), insn_name (&i.tm),
11280
0
           register_prefix, i.op[0].regs->reg_name);
11281
0
      }
11282
0
  }
11283
2.20k
    }
11284
1.88k
  else if (i.types[0].bitfield.class == SReg && !dot_insn ())
11285
0
    {
11286
0
      if (flag_code != CODE_64BIT
11287
0
    ? i.tm.base_opcode == POP_SEG_SHORT
11288
0
      && i.op[0].regs->reg_num == 1
11289
0
    : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
11290
0
      && i.op[0].regs->reg_num < 4)
11291
0
  {
11292
0
    as_bad (_("you can't `%s %s%s'"),
11293
0
      insn_name (&i.tm), register_prefix, i.op[0].regs->reg_name);
11294
0
    return 0;
11295
0
  }
11296
0
      if (i.op[0].regs->reg_num > 3
11297
0
    && i.tm.opcode_space == SPACE_BASE )
11298
0
  {
11299
0
    i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
11300
0
    i.tm.opcode_space = SPACE_0F;
11301
0
  }
11302
0
      i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
11303
0
    }
11304
1.88k
  else if (i.tm.opcode_space == SPACE_BASE
11305
1.87k
     && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
11306
0
    {
11307
0
      default_seg = reg_ds;
11308
0
    }
11309
1.88k
  else if (i.tm.opcode_modifier.isstring)
11310
22
    {
11311
      /* For the string instructions that allow a segment override
11312
   on one of their operands, the default segment is ds.  */
11313
22
      default_seg = reg_ds;
11314
22
    }
11315
1.86k
  else if (i.short_form)
11316
132
    {
11317
      /* The register operand is in the 1st or 2nd non-immediate operand.  */
11318
132
      const reg_entry *r = i.op[i.imm_operands].regs;
11319
11320
132
      if (!dot_insn ()
11321
132
    && r->reg_type.bitfield.instance == Accum
11322
6
    && i.op[i.imm_operands + 1].regs)
11323
0
  r = i.op[i.imm_operands + 1].regs;
11324
      /* Register goes in low 3 bits of opcode.  */
11325
132
      i.tm.base_opcode |= r->reg_num;
11326
132
      set_rex_vrex (r, REX_B, false);
11327
11328
132
      if (dot_insn () && i.reg_operands == 2)
11329
0
  {
11330
0
    gas_assert (is_any_vex_encoding (&i.tm)
11331
0
          || pp.encoding != encoding_default);
11332
0
    i.vex.register_specifier = i.op[i.operands - 1].regs;
11333
0
  }
11334
132
    }
11335
1.73k
  else if (i.reg_operands == 1
11336
8
     && !i.flags[i.operands - 1]
11337
8
     && i.tm_types[i.operands - 1].bitfield.instance
11338
8
        == InstanceNone)
11339
8
    {
11340
8
      gas_assert (is_any_vex_encoding (&i.tm)
11341
8
      || pp.encoding != encoding_default);
11342
8
      i.vex.register_specifier = i.op[i.operands - 1].regs;
11343
8
    }
11344
11345
4.08k
  if ((i.seg[0] || i.prefix[SEG_PREFIX])
11346
16
      && i.tm.mnem_off == MN_lea)
11347
0
    {
11348
0
      if (!quiet_warnings)
11349
0
  as_warn (_("segment override on `%s' is ineffectual"), insn_name (&i.tm));
11350
0
      if (optimize && !pp.no_optimize)
11351
0
  {
11352
0
    i.seg[0] = NULL;
11353
0
    i.prefix[SEG_PREFIX] = 0;
11354
0
  }
11355
0
    }
11356
11357
  /* If a segment was explicitly specified, and the specified segment
11358
     is neither the default nor the one already recorded from a prefix,
11359
     use an opcode prefix to select it.  If we never figured out what
11360
     the default segment is, then default_seg will be zero at this
11361
     point, and the specified segment prefix will always be used.  */
11362
4.08k
  if (i.seg[0]
11363
4
      && i.seg[0] != default_seg
11364
4
      && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
11365
4
    {
11366
4
      if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
11367
0
  return 0;
11368
4
    }
11369
4.08k
  return 1;
11370
4.08k
}
11371
11372
static const reg_entry *
11373
build_modrm_byte (void)
11374
2.20k
{
11375
2.20k
  const reg_entry *default_seg = NULL;
11376
2.20k
  unsigned int source = i.imm_operands - i.tm.opcode_modifier.immext
11377
      /* Compensate for kludge in md_assemble().  */
11378
2.20k
      + i.tm_types[0].bitfield.imm1;
11379
2.20k
  unsigned int dest = i.operands - 1 - i.tm.opcode_modifier.immext;
11380
2.20k
  unsigned int v, op, reg_slot;
11381
11382
  /* Accumulator (in particular %st), shift count (%cl), and alike need
11383
     to be skipped just like immediate operands do.  */
11384
2.20k
  if (i.tm_types[source].bitfield.instance)
11385
0
    ++source;
11386
2.20k
  while (i.tm_types[dest].bitfield.instance)
11387
0
    --dest;
11388
11389
3.74k
  for (op = source; op < i.operands; ++op)
11390
3.49k
    if (i.tm_types[op].bitfield.baseindex)
11391
1.94k
      break;
11392
11393
2.20k
  if (i.reg_operands + i.mem_operands + (i.tm.extension_opcode != None)
11394
2.20k
      + (i.tm.opcode_modifier.operandconstraint == SCC) == 4)
11395
0
    {
11396
0
      expressionS *exp;
11397
11398
      /* There are 2 kinds of instructions:
11399
   1. 5 operands: 4 register operands or 3 register operands
11400
   plus 1 memory operand plus one Imm4 operand, VexXDS, and
11401
   VexW0 or VexW1.  The destination must be either XMM, YMM or
11402
   ZMM register.
11403
   2. 4 operands: 4 register operands or 3 register operands
11404
   plus 1 memory operand, with VexXDS.
11405
   3. Other equivalent combinations when coming from s_insn().  */
11406
0
      if (!dot_insn ())
11407
0
  {
11408
0
    gas_assert (i.tm.opcode_modifier.vexvvvv
11409
0
          && i.tm.opcode_modifier.vexw);
11410
0
    gas_assert (i.tm_types[dest].bitfield.class == RegSIMD);
11411
0
  }
11412
11413
      /* Of the first two non-immediate operands the one with the template
11414
   not allowing for a memory one is encoded in the immediate operand.  */
11415
0
      if (source == op)
11416
0
  reg_slot = source + 1;
11417
0
      else
11418
0
  reg_slot = source++;
11419
11420
0
      if (!dot_insn ())
11421
0
  {
11422
0
    gas_assert (i.tm_types[reg_slot].bitfield.class == RegSIMD);
11423
0
    gas_assert (!(i.op[reg_slot].regs->reg_flags & RegVRex));
11424
0
  }
11425
0
      else
11426
0
  gas_assert (i.tm_types[reg_slot].bitfield.class != ClassNone);
11427
11428
0
      if (i.imm_operands == 0)
11429
0
  {
11430
    /* When there is no immediate operand, generate an 8bit
11431
       immediate operand to encode the first operand.  */
11432
0
    exp = &im_expressions[i.imm_operands++];
11433
0
    i.op[i.operands].imms = exp;
11434
0
    i.types[i.operands].bitfield.imm8 = 1;
11435
0
    i.operands++;
11436
11437
0
    exp->X_op = O_constant;
11438
0
  }
11439
0
      else
11440
0
  {
11441
0
    gas_assert (i.imm_operands == 1);
11442
0
    gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
11443
0
    gas_assert (!i.tm.opcode_modifier.immext);
11444
11445
    /* Turn on Imm8 again so that output_imm will generate it.  */
11446
0
    i.types[0].bitfield.imm8 = 1;
11447
11448
0
    exp = i.op[0].imms;
11449
0
  }
11450
0
      exp->X_add_number |= register_number (i.op[reg_slot].regs)
11451
0
         << (3 + !(i.tm.opcode_modifier.evex
11452
0
             || pp.encoding == encoding_evex));
11453
0
    }
11454
11455
2.20k
  switch (i.tm.opcode_modifier.vexvvvv)
11456
2.20k
    {
11457
    /* VEX.vvvv encodes the last source register operand.  */
11458
0
    case VexVVVV_SRC2:
11459
0
      v = source++;
11460
0
      break;
11461
    /* VEX.vvvv encodes the first source register operand.  */
11462
14
    case VexVVVV_SRC1:
11463
14
      v =  dest - 1;
11464
14
      break;
11465
    /* VEX.vvvv encodes the destination register operand.  */
11466
3
    case VexVVVV_DST:
11467
3
      v = dest--;
11468
3
      break;
11469
2.18k
    default:
11470
2.18k
      v = ~0;
11471
2.18k
      break;
11472
2.20k
     }
11473
11474
2.20k
  if (dest == source)
11475
715
    dest = ~0;
11476
11477
2.20k
  gas_assert (source < dest);
11478
11479
2.20k
  if (v < MAX_OPERANDS)
11480
17
    {
11481
17
      gas_assert (i.tm.opcode_modifier.vexvvvv);
11482
17
      i.vex.register_specifier = i.op[v].regs;
11483
17
    }
11484
11485
2.20k
  if (op < i.operands)
11486
1.94k
    {
11487
1.94k
      if (i.mem_operands)
11488
1.20k
  {
11489
1.20k
    unsigned int fake_zero_displacement = 0;
11490
11491
1.20k
    gas_assert (i.flags[op] & Operand_Mem);
11492
11493
1.20k
    if (i.tm.opcode_modifier.sib)
11494
0
      {
11495
        /* The index register of VSIB shouldn't be RegIZ.  */
11496
0
        if (i.tm.opcode_modifier.sib != SIBMEM
11497
0
      && i.index_reg->reg_num == RegIZ)
11498
0
    abort ();
11499
11500
0
        i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
11501
0
        if (!i.base_reg)
11502
0
    {
11503
0
      i.sib.base = NO_BASE_REGISTER;
11504
0
      i.sib.scale = i.log2_scale_factor;
11505
0
      i.types[op] = operand_type_and_not (i.types[op], anydisp);
11506
0
      i.types[op].bitfield.disp32 = 1;
11507
0
    }
11508
11509
        /* Since the mandatory SIB always has index register, so
11510
     the code logic remains unchanged. The non-mandatory SIB
11511
     without index register is allowed and will be handled
11512
     later.  */
11513
0
        if (i.index_reg)
11514
0
    {
11515
0
      if (i.index_reg->reg_num == RegIZ)
11516
0
        i.sib.index = NO_INDEX_REGISTER;
11517
0
      else
11518
0
        i.sib.index = i.index_reg->reg_num;
11519
0
      set_rex_vrex (i.index_reg, REX_X, false);
11520
0
    }
11521
0
      }
11522
11523
1.20k
    default_seg = reg_ds;
11524
11525
1.20k
    if (i.base_reg == 0)
11526
1.06k
      {
11527
1.06k
        i.rm.mode = 0;
11528
1.06k
        if (!i.disp_operands)
11529
1
    fake_zero_displacement = 1;
11530
1.06k
        if (i.index_reg == 0)
11531
1.06k
    {
11532
      /* Both check for VSIB and mandatory non-vector SIB. */
11533
1.06k
      gas_assert (!i.tm.opcode_modifier.sib
11534
1.06k
            || i.tm.opcode_modifier.sib == SIBMEM);
11535
      /* Operand is just <disp>  */
11536
1.06k
      i.types[op] = operand_type_and_not (i.types[op], anydisp);
11537
1.06k
      if (flag_code == CODE_64BIT)
11538
613
        {
11539
          /* 64bit mode overwrites the 32bit absolute
11540
       addressing by RIP relative addressing and
11541
       absolute addressing is encoded by one of the
11542
       redundant SIB forms.  */
11543
613
          i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
11544
613
          i.sib.base = NO_BASE_REGISTER;
11545
613
          i.sib.index = NO_INDEX_REGISTER;
11546
613
          i.types[op].bitfield.disp32 = 1;
11547
613
        }
11548
448
      else if ((flag_code == CODE_16BIT)
11549
448
         ^ (i.prefix[ADDR_PREFIX] != 0))
11550
188
        {
11551
188
          i.rm.regmem = NO_BASE_REGISTER_16;
11552
188
          i.types[op].bitfield.disp16 = 1;
11553
188
        }
11554
260
      else
11555
260
        {
11556
260
          i.rm.regmem = NO_BASE_REGISTER;
11557
260
          i.types[op].bitfield.disp32 = 1;
11558
260
        }
11559
1.06k
    }
11560
0
        else if (!i.tm.opcode_modifier.sib)
11561
0
    {
11562
      /* !i.base_reg && i.index_reg  */
11563
0
      if (i.index_reg->reg_num == RegIZ)
11564
0
        i.sib.index = NO_INDEX_REGISTER;
11565
0
      else
11566
0
        i.sib.index = i.index_reg->reg_num;
11567
0
      i.sib.base = NO_BASE_REGISTER;
11568
0
      i.sib.scale = i.log2_scale_factor;
11569
0
      i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
11570
0
      i.types[op] = operand_type_and_not (i.types[op], anydisp);
11571
0
      i.types[op].bitfield.disp32 = 1;
11572
0
      set_rex_rex2 (i.index_reg, REX_X);
11573
0
    }
11574
1.06k
      }
11575
    /* RIP addressing for 64bit mode.  */
11576
147
    else if (i.base_reg->reg_num == RegIP)
11577
0
      {
11578
0
        gas_assert (!i.tm.opcode_modifier.sib);
11579
0
        i.rm.regmem = NO_BASE_REGISTER;
11580
0
        i.types[op].bitfield.disp8 = 0;
11581
0
        i.types[op].bitfield.disp16 = 0;
11582
0
        i.types[op].bitfield.disp32 = 1;
11583
0
        i.types[op].bitfield.disp64 = 0;
11584
0
        i.flags[op] |= Operand_PCrel;
11585
0
        if (! i.disp_operands)
11586
0
    fake_zero_displacement = 1;
11587
0
      }
11588
147
    else if (i.base_reg->reg_type.bitfield.word)
11589
0
      {
11590
0
        gas_assert (!i.tm.opcode_modifier.sib);
11591
0
        switch (i.base_reg->reg_num)
11592
0
    {
11593
0
    case 3: /* (%bx)  */
11594
0
      if (i.index_reg == 0)
11595
0
        i.rm.regmem = 7;
11596
0
      else /* (%bx,%si) -> 0, or (%bx,%di) -> 1  */
11597
0
        i.rm.regmem = i.index_reg->reg_num - 6;
11598
0
      break;
11599
0
    case 5: /* (%bp)  */
11600
0
      default_seg = reg_ss;
11601
0
      if (i.index_reg == 0)
11602
0
        {
11603
0
          i.rm.regmem = 6;
11604
0
          if (operand_type_check (i.types[op], disp) == 0)
11605
0
      {
11606
        /* fake (%bp) into 0(%bp)  */
11607
0
        if (pp.disp_encoding == disp_encoding_16bit)
11608
0
          i.types[op].bitfield.disp16 = 1;
11609
0
        else
11610
0
          i.types[op].bitfield.disp8 = 1;
11611
0
        fake_zero_displacement = 1;
11612
0
      }
11613
0
        }
11614
0
      else /* (%bp,%si) -> 2, or (%bp,%di) -> 3  */
11615
0
        i.rm.regmem = i.index_reg->reg_num - 6 + 2;
11616
0
      break;
11617
0
    default: /* (%si) -> 4 or (%di) -> 5  */
11618
0
      i.rm.regmem = i.base_reg->reg_num - 6 + 4;
11619
0
    }
11620
0
        if (!fake_zero_displacement
11621
0
      && !i.disp_operands
11622
0
      && pp.disp_encoding)
11623
0
    {
11624
0
      fake_zero_displacement = 1;
11625
0
      if (pp.disp_encoding == disp_encoding_8bit)
11626
0
        i.types[op].bitfield.disp8 = 1;
11627
0
      else
11628
0
        i.types[op].bitfield.disp16 = 1;
11629
0
    }
11630
0
        i.rm.mode = mode_from_disp_size (i.types[op]);
11631
0
      }
11632
147
    else /* i.base_reg and 32/64 bit mode  */
11633
147
      {
11634
147
        if (operand_type_check (i.types[op], disp))
11635
7
    {
11636
7
      i.types[op].bitfield.disp16 = 0;
11637
7
      i.types[op].bitfield.disp64 = 0;
11638
7
      i.types[op].bitfield.disp32 = 1;
11639
7
    }
11640
11641
147
        if (!i.tm.opcode_modifier.sib)
11642
147
    i.rm.regmem = i.base_reg->reg_num;
11643
147
        set_rex_rex2 (i.base_reg, REX_B);
11644
147
        i.sib.base = i.base_reg->reg_num;
11645
        /* x86-64 ignores REX prefix bit here to avoid decoder
11646
     complications.  */
11647
147
        if (!(i.base_reg->reg_flags & RegRex)
11648
147
      && (i.base_reg->reg_num == EBP_REG_NUM
11649
147
       || i.base_reg->reg_num == ESP_REG_NUM))
11650
137
      default_seg = reg_ss;
11651
147
        if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
11652
0
    {
11653
0
      fake_zero_displacement = 1;
11654
0
      if (pp.disp_encoding == disp_encoding_32bit)
11655
0
        i.types[op].bitfield.disp32 = 1;
11656
0
      else
11657
0
        i.types[op].bitfield.disp8 = 1;
11658
0
    }
11659
147
        i.sib.scale = i.log2_scale_factor;
11660
147
        if (i.index_reg == 0)
11661
147
    {
11662
      /* Only check for VSIB. */
11663
147
      gas_assert (i.tm.opcode_modifier.sib != VECSIB128
11664
147
            && i.tm.opcode_modifier.sib != VECSIB256
11665
147
            && i.tm.opcode_modifier.sib != VECSIB512);
11666
11667
      /* <disp>(%esp) becomes two byte modrm with no index
11668
         register.  We've already stored the code for esp
11669
         in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
11670
         Any base register besides %esp will not use the
11671
         extra modrm byte.  */
11672
147
      i.sib.index = NO_INDEX_REGISTER;
11673
147
    }
11674
0
        else if (!i.tm.opcode_modifier.sib)
11675
0
    {
11676
0
      if (i.index_reg->reg_num == RegIZ)
11677
0
        i.sib.index = NO_INDEX_REGISTER;
11678
0
      else
11679
0
        i.sib.index = i.index_reg->reg_num;
11680
0
      i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
11681
0
      set_rex_rex2 (i.index_reg, REX_X);
11682
0
    }
11683
11684
147
        if (i.disp_operands
11685
7
      && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
11686
7
          || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
11687
0
    i.rm.mode = 0;
11688
147
        else
11689
147
    {
11690
147
      if (!fake_zero_displacement
11691
147
          && !i.disp_operands
11692
140
          && pp.disp_encoding)
11693
0
        {
11694
0
          fake_zero_displacement = 1;
11695
0
          if (pp.disp_encoding == disp_encoding_8bit)
11696
0
      i.types[op].bitfield.disp8 = 1;
11697
0
          else
11698
0
      i.types[op].bitfield.disp32 = 1;
11699
0
        }
11700
147
      i.rm.mode = mode_from_disp_size (i.types[op]);
11701
147
    }
11702
147
      }
11703
11704
1.20k
    if (fake_zero_displacement)
11705
1
      {
11706
        /* Fakes a zero displacement assuming that i.types[op]
11707
     holds the correct displacement size.  */
11708
1
        expressionS *exp;
11709
11710
1
        gas_assert (i.op[op].disps == 0);
11711
1
        exp = &disp_expressions[i.disp_operands++];
11712
1
        i.op[op].disps = exp;
11713
1
        exp->X_op = O_constant;
11714
1
        exp->X_add_number = 0;
11715
1
        exp->X_add_symbol = NULL;
11716
1
        exp->X_op_symbol = NULL;
11717
1
      }
11718
1.20k
  }
11719
735
    else
11720
735
  {
11721
735
      i.rm.mode = 3;
11722
735
      i.rm.regmem = i.op[op].regs->reg_num;
11723
735
      set_rex_vrex (i.op[op].regs, REX_B, false);
11724
735
  }
11725
11726
1.94k
      if (op == dest)
11727
1.03k
  dest = ~0;
11728
1.94k
      if (op == source)
11729
904
  source = ~0;
11730
1.94k
    }
11731
257
  else
11732
257
    {
11733
257
      i.rm.mode = 3;
11734
257
      if (!i.tm.opcode_modifier.regmem)
11735
257
  {
11736
257
    gas_assert (source < MAX_OPERANDS);
11737
257
    i.rm.regmem = i.op[source].regs->reg_num;
11738
257
    set_rex_vrex (i.op[source].regs, REX_B,
11739
257
      dest >= MAX_OPERANDS && i.tm.opcode_modifier.sse2avx);
11740
257
    source = ~0;
11741
257
  }
11742
0
      else
11743
0
  {
11744
0
    gas_assert (dest < MAX_OPERANDS);
11745
0
    i.rm.regmem = i.op[dest].regs->reg_num;
11746
0
    set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
11747
0
    dest = ~0;
11748
0
  }
11749
257
    }
11750
11751
  /* Fill in i.rm.reg field with extension opcode (if any) or the
11752
     appropriate register.  */
11753
2.20k
  if (i.tm.extension_opcode != None)
11754
715
    i.rm.reg = i.tm.extension_opcode;
11755
1.48k
  else if (!i.tm.opcode_modifier.regmem && dest < MAX_OPERANDS)
11756
446
    {
11757
446
      i.rm.reg = i.op[dest].regs->reg_num;
11758
446
      set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
11759
446
    }
11760
1.03k
  else
11761
1.03k
    {
11762
1.03k
      gas_assert (source < MAX_OPERANDS);
11763
1.03k
      i.rm.reg = i.op[source].regs->reg_num;
11764
1.03k
      set_rex_vrex (i.op[source].regs, REX_R, false);
11765
1.03k
    }
11766
11767
2.20k
  if (flag_code != CODE_64BIT && (i.rex & REX_R))
11768
0
    {
11769
0
      gas_assert (i.types[!i.tm.opcode_modifier.regmem].bitfield.class == RegCR);
11770
0
      gas_assert (i.op[!i.tm.opcode_modifier.regmem].regs->reg_num == 0);
11771
0
      i.rex &= ~REX_R;
11772
0
      add_prefix (LOCK_PREFIX_OPCODE);
11773
0
    }
11774
11775
2.20k
  return default_seg;
11776
2.20k
}
11777
11778
static INLINE void
11779
frag_opcode_byte (unsigned char byte)
11780
3.79k
{
11781
3.79k
  if (now_seg != absolute_section)
11782
1.70k
    FRAG_APPEND_1_CHAR (byte);
11783
2.09k
  else
11784
2.09k
    ++abs_section_offset;
11785
3.79k
}
11786
11787
static unsigned int
11788
flip_code16 (unsigned int code16)
11789
16
{
11790
16
  gas_assert (i.tm.operands == 1);
11791
11792
16
  return !(i.prefix[REX_PREFIX] & REX_W)
11793
16
   && (code16 ? i.tm_types[0].bitfield.disp32
11794
16
        : i.tm_types[0].bitfield.disp16)
11795
16
   ? CODE16 : 0;
11796
16
}
11797
11798
static void
11799
output_branch (void)
11800
21
{
11801
21
  char *p;
11802
21
  int size;
11803
21
  int code16;
11804
21
  int prefix;
11805
21
  relax_substateT subtype;
11806
21
  symbolS *sym;
11807
21
  offsetT off;
11808
11809
21
  if (now_seg == absolute_section)
11810
0
    {
11811
0
      as_bad (_("relaxable branches not supported in absolute section"));
11812
0
      return;
11813
0
    }
11814
11815
21
  code16 = flag_code == CODE_16BIT ? CODE16 : 0;
11816
21
  size = pp.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
11817
11818
21
  prefix = 0;
11819
21
  if (i.prefix[DATA_PREFIX] != 0)
11820
0
    {
11821
0
      prefix = 1;
11822
0
      i.prefixes -= 1;
11823
0
      code16 ^= flip_code16(code16);
11824
0
    }
11825
  /* Pentium4 branch hints.  */
11826
21
  if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
11827
9
      || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
11828
12
    {
11829
12
      prefix++;
11830
12
      i.prefixes--;
11831
12
    }
11832
21
  if (i.prefix[REX_PREFIX] != 0)
11833
0
    {
11834
0
      prefix++;
11835
0
      i.prefixes--;
11836
0
    }
11837
11838
  /* BND prefixed jump.  */
11839
21
  if (i.prefix[BND_PREFIX] != 0)
11840
0
    {
11841
0
      prefix++;
11842
0
      i.prefixes--;
11843
0
    }
11844
11845
21
  if (i.prefixes != 0)
11846
0
    as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
11847
11848
  /* It's always a symbol;  End frag & setup for relax.
11849
     Make sure there is enough room in this frag for the largest
11850
     instruction we may generate in md_convert_frag.  This is 2
11851
     bytes for the opcode and room for the prefix and largest
11852
     displacement.  */
11853
21
  frag_grow (prefix + 2 + 4);
11854
  /* Prefix and 1 opcode byte go in fr_fix.  */
11855
21
  p = frag_more (prefix + 1);
11856
21
  if (i.prefix[DATA_PREFIX] != 0)
11857
0
    *p++ = DATA_PREFIX_OPCODE;
11858
21
  if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
11859
9
      || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
11860
12
    *p++ = i.prefix[SEG_PREFIX];
11861
21
  if (i.prefix[BND_PREFIX] != 0)
11862
0
    *p++ = BND_PREFIX_OPCODE;
11863
21
  if (i.prefix[REX_PREFIX] != 0)
11864
0
    *p++ = i.prefix[REX_PREFIX];
11865
21
  *p = i.tm.base_opcode;
11866
11867
21
  if ((unsigned char) *p == JUMP_PC_RELATIVE)
11868
0
    subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
11869
21
  else if (cpu_arch_flags.bitfield.cpui386)
11870
21
    subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
11871
0
  else
11872
0
    subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
11873
21
  subtype |= code16;
11874
11875
21
  sym = i.op[0].disps->X_add_symbol;
11876
21
  off = i.op[0].disps->X_add_number;
11877
11878
21
  if (i.op[0].disps->X_op != O_constant
11879
21
      && i.op[0].disps->X_op != O_symbol)
11880
2
    {
11881
      /* Handle complex expressions.  */
11882
2
      sym = make_expr_symbol (i.op[0].disps);
11883
2
      off = 0;
11884
2
    }
11885
11886
  /* 1 possible extra opcode + 4 byte displacement go in var part.
11887
     Pass reloc in fr_var.  */
11888
21
  frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
11889
21
}
11890
11891
/* PLT32 relocation is ELF only.  */
11892
#ifdef OBJ_ELF
11893
/* Return TRUE iff PLT32 relocation should be used for branching to
11894
   symbol S.  */
11895
11896
static bool
11897
need_plt32_p (symbolS *s)
11898
19
{
11899
#ifdef TE_SOLARIS
11900
  /* Don't emit PLT32 relocation on Solaris: neither native linker nor
11901
     krtld support it.  */
11902
  return false;
11903
#endif
11904
11905
  /* Since there is no need to prepare for PLT branch on x86-64, we
11906
     can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
11907
     be used as a marker for 32-bit PC-relative branches.  */
11908
19
  if (!object_64bit)
11909
0
    return false;
11910
11911
19
  if (s == NULL)
11912
0
    return false;
11913
11914
  /* Weak or undefined symbol need PLT32 relocation.  */
11915
19
  if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
11916
17
    return true;
11917
11918
  /* Non-global symbol doesn't need PLT32 relocation.  */
11919
2
  if (! S_IS_EXTERNAL (s))
11920
2
    return false;
11921
11922
  /* Other global symbols need PLT32 relocation.  NB: Symbol with
11923
     non-default visibilities are treated as normal global symbol
11924
     so that PLT32 relocation can be used as a marker for 32-bit
11925
     PC-relative branches.  It is useful for linker relaxation.  */
11926
0
  return true;
11927
2
}
11928
#endif
11929
11930
static void
11931
output_jump (void)
11932
38
{
11933
38
  char *p;
11934
38
  int size;
11935
38
  fixS *fixP;
11936
38
  bfd_reloc_code_real_type jump_reloc = i.reloc[0];
11937
11938
38
  if (i.tm.opcode_modifier.jump == JUMP_BYTE)
11939
0
    {
11940
      /* This is a loop or jecxz type instruction.  */
11941
0
      size = 1;
11942
0
      if (i.prefix[ADDR_PREFIX] != 0)
11943
0
  {
11944
0
    frag_opcode_byte (ADDR_PREFIX_OPCODE);
11945
0
    i.prefixes -= 1;
11946
0
  }
11947
      /* Pentium4 branch hints.  */
11948
0
      if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
11949
0
    || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
11950
0
  {
11951
0
    frag_opcode_byte (i.prefix[SEG_PREFIX]);
11952
0
    i.prefixes--;
11953
0
  }
11954
0
    }
11955
38
  else
11956
38
    {
11957
38
      int code16;
11958
11959
38
      code16 = 0;
11960
38
      if (flag_code == CODE_16BIT)
11961
1
  code16 = CODE16;
11962
11963
38
      if (i.prefix[DATA_PREFIX] != 0)
11964
16
  {
11965
16
    frag_opcode_byte (DATA_PREFIX_OPCODE);
11966
16
    i.prefixes -= 1;
11967
16
    code16 ^= flip_code16(code16);
11968
16
  }
11969
11970
38
      size = 4;
11971
38
      if (code16)
11972
17
  size = 2;
11973
38
    }
11974
11975
  /* BND prefixed jump.  */
11976
38
  if (i.prefix[BND_PREFIX] != 0)
11977
0
    {
11978
0
      frag_opcode_byte (i.prefix[BND_PREFIX]);
11979
0
      i.prefixes -= 1;
11980
0
    }
11981
11982
38
  if (i.prefix[REX_PREFIX] != 0)
11983
0
    {
11984
0
      frag_opcode_byte (i.prefix[REX_PREFIX]);
11985
0
      i.prefixes -= 1;
11986
0
    }
11987
11988
38
  if (i.prefixes != 0)
11989
0
    as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
11990
11991
38
  if (now_seg == absolute_section)
11992
1
    {
11993
1
      abs_section_offset += i.opcode_length + size;
11994
1
      return;
11995
1
    }
11996
11997
37
  p = frag_more (i.opcode_length + size);
11998
37
  switch (i.opcode_length)
11999
37
    {
12000
0
    case 2:
12001
0
      *p++ = i.tm.base_opcode >> 8;
12002
      /* Fall through.  */
12003
37
    case 1:
12004
37
      *p++ = i.tm.base_opcode;
12005
37
      break;
12006
0
    default:
12007
0
      abort ();
12008
37
    }
12009
12010
37
#ifdef OBJ_ELF
12011
37
  if (flag_code == CODE_64BIT && size == 4
12012
19
      && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
12013
19
      && need_plt32_p (i.op[0].disps->X_add_symbol))
12014
17
    jump_reloc = BFD_RELOC_32_PLT_PCREL;
12015
37
#endif
12016
12017
37
  jump_reloc = reloc (size, 1, 1, jump_reloc);
12018
12019
37
  fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
12020
37
          i.op[0].disps, 1, jump_reloc);
12021
12022
  /* All jumps handled here are signed, but don't unconditionally use a
12023
     signed limit check for 32 and 16 bit jumps as we want to allow wrap
12024
     around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
12025
     respectively.  */
12026
37
  switch (size)
12027
37
    {
12028
0
    case 1:
12029
0
      fixP->fx_signed = 1;
12030
0
      break;
12031
12032
17
    case 2:
12033
17
      if (i.tm.mnem_off == MN_xbegin)
12034
0
  fixP->fx_signed = 1;
12035
17
      break;
12036
12037
20
    case 4:
12038
20
      if (flag_code == CODE_64BIT)
12039
19
  fixP->fx_signed = 1;
12040
20
      break;
12041
37
    }
12042
37
}
12043
12044
static void
12045
output_interseg_jump (void)
12046
5
{
12047
5
  char *p;
12048
5
  int size;
12049
5
  int prefix;
12050
5
  int code16;
12051
12052
5
  code16 = 0;
12053
5
  if (flag_code == CODE_16BIT)
12054
5
    code16 = CODE16;
12055
12056
5
  prefix = 0;
12057
5
  if (i.prefix[DATA_PREFIX] != 0)
12058
5
    {
12059
5
      prefix = 1;
12060
5
      i.prefixes -= 1;
12061
5
      code16 ^= CODE16;
12062
5
    }
12063
12064
5
  gas_assert (!i.prefix[REX_PREFIX]);
12065
12066
5
  size = 4;
12067
5
  if (code16)
12068
0
    size = 2;
12069
12070
5
  if (i.prefixes != 0)
12071
0
    as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
12072
12073
5
  if (now_seg == absolute_section)
12074
0
    {
12075
0
      abs_section_offset += prefix + 1 + 2 + size;
12076
0
      return;
12077
0
    }
12078
12079
  /* 1 opcode; 2 segment; offset  */
12080
5
  p = frag_more (prefix + 1 + 2 + size);
12081
12082
5
  if (i.prefix[DATA_PREFIX] != 0)
12083
5
    *p++ = DATA_PREFIX_OPCODE;
12084
12085
5
  if (i.prefix[REX_PREFIX] != 0)
12086
0
    *p++ = i.prefix[REX_PREFIX];
12087
12088
5
  *p++ = i.tm.base_opcode;
12089
5
  if (i.op[1].imms->X_op == O_constant)
12090
4
    {
12091
4
      offsetT n = i.op[1].imms->X_add_number;
12092
12093
4
      if (size == 2
12094
0
    && !fits_in_unsigned_word (n)
12095
0
    && !fits_in_signed_word (n))
12096
0
  {
12097
0
    as_bad (_("16-bit jump out of range"));
12098
0
    return;
12099
0
  }
12100
4
      md_number_to_chars (p, n, size);
12101
4
    }
12102
1
  else
12103
1
    fix_new_exp (frag_now, p - frag_now->fr_literal, size,
12104
1
     i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
12105
12106
5
  p += size;
12107
5
  if (i.op[0].imms->X_op == O_constant)
12108
0
    md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
12109
5
  else
12110
5
    fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
12111
5
     i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
12112
5
}
12113
12114
/* Hook used to reject pseudo-prefixes misplaced at the start of a line.  */
12115
12116
void i386_start_line (void)
12117
274k
{
12118
274k
  struct pseudo_prefixes last_pp;
12119
12120
274k
  memcpy (&last_pp, &pp, sizeof (pp));
12121
274k
  memset (&pp, 0, sizeof (pp));
12122
274k
  if (memcmp (&pp, &last_pp, sizeof (pp)))
12123
412
    as_bad_where (frag_now->fr_file, frag_now->fr_line,
12124
412
      _("pseudo prefix without instruction"));
12125
274k
}
12126
12127
/* Hook used to warn about pseudo-prefixes ahead of a label.  */
12128
12129
bool i386_check_label (void)
12130
7.81k
{
12131
7.81k
  struct pseudo_prefixes last_pp;
12132
12133
7.81k
  memcpy (&last_pp, &pp, sizeof (pp));
12134
7.81k
  memset (&pp, 0, sizeof (pp));
12135
7.81k
  if (memcmp (&pp, &last_pp, sizeof (pp)))
12136
0
    as_warn (_("pseudo prefix ahead of label; ignoring"));
12137
7.81k
  return true;
12138
7.81k
}
12139
12140
/* Hook used to parse pseudo-prefixes off of the start of a line.  */
12141
12142
int
12143
i386_unrecognized_line (int ch)
12144
28.2k
{
12145
28.2k
  char mnemonic[MAX_MNEM_SIZE];
12146
28.2k
  const char *end;
12147
12148
28.2k
  if (ch != '{')
12149
26.3k
    return 0;
12150
12151
1.88k
  --input_line_pointer;
12152
1.88k
  know (*input_line_pointer == ch);
12153
12154
1.88k
  end = parse_insn (input_line_pointer, mnemonic, parse_pseudo_prefix);
12155
1.88k
  if (end == NULL)
12156
14
    {
12157
      /* Diagnostic was already issued.  */
12158
14
      ignore_rest_of_line ();
12159
14
      memset (&pp, 0, sizeof (pp));
12160
14
      return 1;
12161
14
    }
12162
12163
1.87k
  if (end == input_line_pointer)
12164
82
    {
12165
82
      ++input_line_pointer;
12166
82
      return 0;
12167
82
    }
12168
12169
1.79k
  input_line_pointer += end - input_line_pointer;
12170
1.79k
  return 1;
12171
1.87k
}
12172
12173
#ifdef OBJ_ELF
12174
void
12175
x86_cleanup (void)
12176
281
{
12177
281
  char *p;
12178
281
  asection *seg = now_seg;
12179
281
  subsegT subseg = now_subseg;
12180
281
  asection *sec;
12181
281
  unsigned int alignment, align_size_1;
12182
281
  unsigned int isa_1_descsz, feature_2_descsz, descsz;
12183
281
  unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
12184
281
  unsigned int padding;
12185
12186
281
  if (!x86_used_note)
12187
0
    return;
12188
12189
281
  x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
12190
12191
  /* The .note.gnu.property section layout:
12192
12193
     Field  Length    Contents
12194
     ---- ----    ----
12195
     n_namsz  4   4
12196
     n_descsz 4   The note descriptor size
12197
     n_type 4   NT_GNU_PROPERTY_TYPE_0
12198
     n_name 4   "GNU"
12199
     n_desc n_descsz  The program property array
12200
     .... ....    ....
12201
   */
12202
12203
  /* Create the .note.gnu.property section.  */
12204
281
  sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
12205
281
  bfd_set_section_flags (sec,
12206
281
       (SEC_ALLOC
12207
281
        | SEC_LOAD
12208
281
        | SEC_DATA
12209
281
        | SEC_HAS_CONTENTS
12210
281
        | SEC_READONLY));
12211
12212
281
  if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
12213
281
    {
12214
281
      align_size_1 = 7;
12215
281
      alignment = 3;
12216
281
    }
12217
0
  else
12218
0
    {
12219
0
      align_size_1 = 3;
12220
0
      alignment = 2;
12221
0
    }
12222
12223
281
  bfd_set_section_alignment (sec, alignment);
12224
281
  elf_section_type (sec) = SHT_NOTE;
12225
12226
  /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
12227
          + 4-byte data  */
12228
281
  isa_1_descsz_raw = 4 + 4 + 4;
12229
  /* Align GNU_PROPERTY_X86_ISA_1_USED.  */
12230
281
  isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
12231
12232
281
  feature_2_descsz_raw = isa_1_descsz;
12233
  /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
12234
              + 4-byte data  */
12235
281
  feature_2_descsz_raw += 4 + 4 + 4;
12236
  /* Align GNU_PROPERTY_X86_FEATURE_2_USED.  */
12237
281
  feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
12238
281
          & ~align_size_1);
12239
12240
281
  descsz = feature_2_descsz;
12241
  /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz.  */
12242
281
  p = frag_more (4 + 4 + 4 + 4 + descsz);
12243
12244
  /* Write n_namsz.  */
12245
281
  md_number_to_chars (p, (valueT) 4, 4);
12246
12247
  /* Write n_descsz.  */
12248
281
  md_number_to_chars (p + 4, (valueT) descsz, 4);
12249
12250
  /* Write n_type.  */
12251
281
  md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
12252
12253
  /* Write n_name.  */
12254
281
  memcpy (p + 4 * 3, "GNU", 4);
12255
12256
  /* Write 4-byte type.  */
12257
281
  md_number_to_chars (p + 4 * 4,
12258
281
          (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
12259
12260
  /* Write 4-byte data size.  */
12261
281
  md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
12262
12263
  /* Write 4-byte data.  */
12264
281
  md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
12265
12266
  /* Zero out paddings.  */
12267
281
  padding = isa_1_descsz - isa_1_descsz_raw;
12268
281
  if (padding)
12269
281
    memset (p + 4 * 7, 0, padding);
12270
12271
  /* Write 4-byte type.  */
12272
281
  md_number_to_chars (p + isa_1_descsz + 4 * 4,
12273
281
          (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
12274
12275
  /* Write 4-byte data size.  */
12276
281
  md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
12277
12278
  /* Write 4-byte data.  */
12279
281
  md_number_to_chars (p + isa_1_descsz + 4 * 6,
12280
281
          (valueT) x86_feature_2_used, 4);
12281
12282
  /* Zero out paddings.  */
12283
281
  padding = feature_2_descsz - feature_2_descsz_raw;
12284
281
  if (padding)
12285
281
    memset (p + isa_1_descsz + 4 * 7, 0, padding);
12286
12287
  /* We probably can't restore the current segment, for there likely
12288
     isn't one yet...  */
12289
281
  if (seg && subseg)
12290
5
    subseg_set (seg, subseg);
12291
281
}
12292
12293
#include "tc-i386-ginsn.c"
12294
12295
/* Whether SFrame stack trace info is supported.  */
12296
bool
12297
x86_support_sframe_p (void)
12298
133
{
12299
  /* At this time, SFrame stack trace is supported for AMD64 ABI only.  */
12300
133
  return (x86_elf_abi == X86_64_ABI);
12301
133
}
12302
12303
/* The fixed offset from CFA for SFrame to recover the return address.
12304
   (useful only when SFrame RA tracking is not needed).  */
12305
offsetT
12306
x86_sframe_cfa_ra_offset (void)
12307
45
{
12308
45
  gas_assert (x86_elf_abi == X86_64_ABI);
12309
45
  return (offsetT) -8;
12310
45
}
12311
12312
/* The abi/arch identifier for SFrame.  */
12313
unsigned char
12314
x86_sframe_get_abi_arch (void)
12315
110
{
12316
110
  unsigned char sframe_abi_arch = 0;
12317
12318
110
  if (x86_support_sframe_p ())
12319
110
    {
12320
110
      gas_assert (!target_big_endian);
12321
110
      sframe_abi_arch = SFRAME_ABI_AMD64_ENDIAN_LITTLE;
12322
110
    }
12323
12324
110
  return sframe_abi_arch;
12325
110
}
12326
12327
#endif
12328
12329
static unsigned int
12330
encoding_length (const fragS *start_frag, offsetT start_off,
12331
     const char *frag_now_ptr)
12332
3.60k
{
12333
3.60k
  unsigned int len = 0;
12334
12335
3.60k
  if (start_frag != frag_now)
12336
1
    {
12337
1
      const fragS *fr = start_frag;
12338
12339
1
      do {
12340
1
  len += fr->fr_fix;
12341
1
  fr = fr->fr_next;
12342
1
      } while (fr && fr != frag_now);
12343
1
    }
12344
12345
3.60k
  return len - start_off + (frag_now_ptr - frag_now->fr_literal);
12346
3.60k
}
12347
12348
/* Return 1 for test, and, cmp, add, sub, inc and dec which may
12349
   be macro-fused with conditional jumps.
12350
   NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
12351
   or is one of the following format:
12352
12353
    cmp m, imm
12354
    add m, imm
12355
    sub m, imm
12356
   test m, imm
12357
    and m, imm
12358
    inc m
12359
    dec m
12360
12361
   it is unfusible.  */
12362
12363
static int
12364
maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
12365
0
{
12366
  /* No RIP address.  */
12367
0
  if (i.base_reg && i.base_reg->reg_num == RegIP)
12368
0
    return 0;
12369
12370
  /* No opcodes outside of base encoding space.  */
12371
0
  if (i.tm.opcode_space != SPACE_BASE)
12372
0
    return 0;
12373
12374
  /* add, sub without add/sub m, imm.  */
12375
0
  if (i.tm.base_opcode <= 5
12376
0
      || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
12377
0
      || ((i.tm.base_opcode | 3) == 0x83
12378
0
    && (i.tm.extension_opcode == 0x5
12379
0
        || i.tm.extension_opcode == 0x0)))
12380
0
    {
12381
0
      *mf_cmp_p = mf_cmp_alu_cmp;
12382
0
      return !(i.mem_operands && i.imm_operands);
12383
0
    }
12384
12385
  /* and without and m, imm.  */
12386
0
  if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
12387
0
      || ((i.tm.base_opcode | 3) == 0x83
12388
0
    && i.tm.extension_opcode == 0x4))
12389
0
    {
12390
0
      *mf_cmp_p = mf_cmp_test_and;
12391
0
      return !(i.mem_operands && i.imm_operands);
12392
0
    }
12393
12394
  /* test without test m imm.  */
12395
0
  if ((i.tm.base_opcode | 1) == 0x85
12396
0
      || (i.tm.base_opcode | 1) == 0xa9
12397
0
      || ((i.tm.base_opcode | 1) == 0xf7
12398
0
    && i.tm.extension_opcode == 0))
12399
0
    {
12400
0
      *mf_cmp_p = mf_cmp_test_and;
12401
0
      return !(i.mem_operands && i.imm_operands);
12402
0
    }
12403
12404
  /* cmp without cmp m, imm.  */
12405
0
  if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
12406
0
      || ((i.tm.base_opcode | 3) == 0x83
12407
0
    && (i.tm.extension_opcode == 0x7)))
12408
0
    {
12409
0
      *mf_cmp_p = mf_cmp_alu_cmp;
12410
0
      return !(i.mem_operands && i.imm_operands);
12411
0
    }
12412
12413
  /* inc, dec without inc/dec m.   */
12414
0
  if ((is_cpu (&i.tm, CpuNo64)
12415
0
       && (i.tm.base_opcode | 0xf) == 0x4f)
12416
0
      || ((i.tm.base_opcode | 1) == 0xff
12417
0
    && i.tm.extension_opcode <= 0x1))
12418
0
    {
12419
0
      *mf_cmp_p = mf_cmp_incdec;
12420
0
      return !i.mem_operands;
12421
0
    }
12422
12423
0
  return 0;
12424
0
}
12425
12426
/* Return 1 if a FUSED_JCC_PADDING frag should be generated.  */
12427
12428
static int
12429
add_fused_jcc_padding_frag_p (enum mf_cmp_kind *mf_cmp_p,
12430
            const struct last_insn *last_insn)
12431
5.15k
{
12432
  /* NB: Don't work with COND_JUMP86 without i386.  */
12433
5.15k
  if (!align_branch_power
12434
0
      || now_seg == absolute_section
12435
0
      || !cpu_arch_flags.bitfield.cpui386
12436
0
      || !(align_branch & align_branch_fused_bit))
12437
5.15k
    return 0;
12438
12439
0
  if (maybe_fused_with_jcc_p (mf_cmp_p))
12440
0
    {
12441
0
      if (last_insn->kind == last_insn_other)
12442
0
  return 1;
12443
0
      if (flag_debug)
12444
0
  as_warn_where (last_insn->file, last_insn->line,
12445
0
           _("`%s` skips -malign-branch-boundary on `%s`"),
12446
0
           last_insn->name, insn_name (&i.tm));
12447
0
    }
12448
12449
0
  return 0;
12450
0
}
12451
12452
/* Return 1 if a BRANCH_PREFIX frag should be generated.  */
12453
12454
static int
12455
add_branch_prefix_frag_p (const struct last_insn *last_insn)
12456
5.15k
{
12457
  /* NB: Don't work with COND_JUMP86 without i386.  Don't add prefix
12458
     to PadLock instructions since they include prefixes in opcode.  */
12459
5.15k
  if (!align_branch_power
12460
0
      || !align_branch_prefix_size
12461
0
      || now_seg == absolute_section
12462
0
      || is_padlock (&i.tm)
12463
0
      || !cpu_arch_flags.bitfield.cpui386)
12464
5.15k
    return 0;
12465
12466
  /* Don't add prefix if it is a prefix or there is no operand in case
12467
     that segment prefix is special.  */
12468
0
  if (!i.operands || i.tm.opcode_modifier.isprefix)
12469
0
    return 0;
12470
12471
0
  if (last_insn->kind == last_insn_other)
12472
0
    return 1;
12473
12474
0
  if (flag_debug)
12475
0
    as_warn_where (last_insn->file, last_insn->line,
12476
0
       _("`%s` skips -malign-branch-boundary on `%s`"),
12477
0
       last_insn->name, insn_name (&i.tm));
12478
12479
0
  return 0;
12480
0
}
12481
12482
/* Return 1 if a BRANCH_PADDING frag should be generated.  */
12483
12484
static int
12485
add_branch_padding_frag_p (enum align_branch_kind *branch_p,
12486
         enum mf_jcc_kind *mf_jcc_p,
12487
         const struct last_insn *last_insn)
12488
5.21k
{
12489
5.21k
  int add_padding;
12490
12491
  /* NB: Don't work with COND_JUMP86 without i386.  */
12492
5.21k
  if (!align_branch_power
12493
0
      || now_seg == absolute_section
12494
0
      || !cpu_arch_flags.bitfield.cpui386
12495
0
      || i.tm.opcode_space != SPACE_BASE)
12496
5.21k
    return 0;
12497
12498
0
  add_padding = 0;
12499
12500
  /* Check for jcc and direct jmp.  */
12501
0
  if (i.tm.opcode_modifier.jump == JUMP)
12502
0
    {
12503
0
      if (i.tm.base_opcode == JUMP_PC_RELATIVE)
12504
0
  {
12505
0
    *branch_p = align_branch_jmp;
12506
0
    add_padding = align_branch & align_branch_jmp_bit;
12507
0
  }
12508
0
      else
12509
0
  {
12510
    /* Because J<cc> and JN<cc> share same group in macro-fusible table,
12511
       igore the lowest bit.  */
12512
0
    *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
12513
0
    *branch_p = align_branch_jcc;
12514
0
    if ((align_branch & align_branch_jcc_bit))
12515
0
      add_padding = 1;
12516
0
  }
12517
0
    }
12518
0
  else if ((i.tm.base_opcode | 1) == 0xc3)
12519
0
    {
12520
      /* Near ret.  */
12521
0
      *branch_p = align_branch_ret;
12522
0
      if ((align_branch & align_branch_ret_bit))
12523
0
  add_padding = 1;
12524
0
    }
12525
0
  else
12526
0
    {
12527
      /* Check for indirect jmp, direct and indirect calls.  */
12528
0
      if (i.tm.base_opcode == 0xe8)
12529
0
  {
12530
    /* Direct call.  */
12531
0
    *branch_p = align_branch_call;
12532
0
    if ((align_branch & align_branch_call_bit))
12533
0
      add_padding = 1;
12534
0
  }
12535
0
      else if (i.tm.base_opcode == 0xff
12536
0
         && (i.tm.extension_opcode == 2
12537
0
       || i.tm.extension_opcode == 4))
12538
0
  {
12539
    /* Indirect call and jmp.  */
12540
0
    *branch_p = align_branch_indirect;
12541
0
    if ((align_branch & align_branch_indirect_bit))
12542
0
      add_padding = 1;
12543
0
  }
12544
12545
0
      if (add_padding
12546
0
    && i.disp_operands
12547
0
    && tls_get_addr
12548
0
    && (i.op[0].disps->X_op == O_symbol
12549
0
        || (i.op[0].disps->X_op == O_subtract
12550
0
      && i.op[0].disps->X_op_symbol == GOT_symbol)))
12551
0
  {
12552
0
    symbolS *s = i.op[0].disps->X_add_symbol;
12553
    /* No padding to call to global or undefined tls_get_addr.  */
12554
0
    if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
12555
0
        && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
12556
0
      return 0;
12557
0
  }
12558
0
    }
12559
12560
0
  if (add_padding
12561
0
      && last_insn->kind != last_insn_other)
12562
0
    {
12563
0
      if (flag_debug)
12564
0
  as_warn_where (last_insn->file, last_insn->line,
12565
0
           _("`%s` skips -malign-branch-boundary on `%s`"),
12566
0
           last_insn->name, insn_name (&i.tm));
12567
0
      return 0;
12568
0
    }
12569
12570
0
  return add_padding;
12571
0
}
12572
12573
static void
12574
output_insn (const struct last_insn *last_insn)
12575
5.21k
{
12576
5.21k
  fragS *insn_start_frag;
12577
5.21k
  offsetT insn_start_off;
12578
5.21k
  fragS *fragP = NULL;
12579
5.21k
  enum align_branch_kind branch = align_branch_none;
12580
  /* The initializer is arbitrary just to avoid uninitialized error.
12581
     it's actually either assigned in add_branch_padding_frag_p
12582
     or never be used.  */
12583
5.21k
  enum mf_jcc_kind mf_jcc = mf_jcc_jo;
12584
12585
5.21k
#ifdef OBJ_ELF
12586
5.21k
  if (x86_used_note && now_seg != absolute_section)
12587
3.67k
    {
12588
3.67k
      unsigned int feature_2_used = 0;
12589
12590
3.67k
      if ((i.xstate & xstate_tmm) == xstate_tmm
12591
3.67k
    || is_cpu (&i.tm, CpuAMX_TILE))
12592
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
12593
12594
3.67k
      if (is_cpu (&i.tm, Cpu8087)
12595
3.67k
    || is_cpu (&i.tm, Cpu287)
12596
3.67k
    || is_cpu (&i.tm, Cpu387)
12597
3.67k
    || is_cpu (&i.tm, Cpu687)
12598
3.63k
    || is_cpu (&i.tm, CpuFISTTP))
12599
32
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
12600
12601
3.67k
      if ((i.xstate & xstate_mmx)
12602
3.67k
    || i.tm.mnem_off == MN_emms
12603
3.67k
    || i.tm.mnem_off == MN_femms)
12604
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
12605
12606
3.67k
      if (i.index_reg)
12607
0
  {
12608
0
    if (i.index_reg->reg_type.bitfield.zmmword)
12609
0
      i.xstate |= xstate_zmm;
12610
0
    else if (i.index_reg->reg_type.bitfield.ymmword)
12611
0
      i.xstate |= xstate_ymm;
12612
0
    else if (i.index_reg->reg_type.bitfield.xmmword)
12613
0
      i.xstate |= xstate_xmm;
12614
0
  }
12615
12616
      /* vzeroall / vzeroupper */
12617
3.67k
      if (i.tm.base_opcode == 0x77 && is_cpu (&i.tm, CpuAVX))
12618
0
  i.xstate |= xstate_ymm;
12619
12620
3.67k
      if ((i.xstate & xstate_xmm)
12621
    /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
12622
3.64k
    || (i.tm.base_opcode == 0xae
12623
5
        && (is_cpu (&i.tm, CpuSSE)
12624
3
      || is_cpu (&i.tm, CpuAVX)))
12625
3.63k
    || is_cpu (&i.tm, CpuWideKL)
12626
3.63k
    || is_cpu (&i.tm, CpuKL))
12627
32
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
12628
12629
3.67k
      if ((i.xstate & xstate_ymm) == xstate_ymm)
12630
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
12631
3.67k
      if ((i.xstate & xstate_zmm) == xstate_zmm)
12632
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
12633
3.67k
      if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
12634
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
12635
3.67k
      if (is_cpu (&i.tm, CpuFXSR))
12636
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
12637
3.67k
      if (is_cpu (&i.tm, CpuXsave))
12638
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
12639
3.67k
      if (is_cpu (&i.tm, CpuXsaveopt))
12640
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
12641
3.67k
      if (is_cpu (&i.tm, CpuXSAVEC))
12642
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
12643
12644
3.67k
      x86_feature_2_used |= feature_2_used;
12645
12646
3.67k
      if (object_64bit
12647
0
    || (feature_2_used
12648
0
        & (GNU_PROPERTY_X86_FEATURE_2_XMM
12649
0
     | GNU_PROPERTY_X86_FEATURE_2_FXSR)) != 0
12650
0
    || is_cpu (&i.tm, CpuCMOV)
12651
0
    || is_cpu (&i.tm, CpuSYSCALL)
12652
0
    || i.tm.mnem_off == MN_cmpxchg8b)
12653
3.67k
  x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
12654
3.67k
      if (is_cpu (&i.tm, CpuSSE3)
12655
3.67k
    || is_cpu (&i.tm, CpuSSSE3)
12656
3.67k
    || is_cpu (&i.tm, CpuSSE4_1)
12657
3.67k
    || is_cpu (&i.tm, CpuSSE4_2)
12658
3.67k
    || is_cpu (&i.tm, CpuCX16)
12659
3.67k
    || is_cpu (&i.tm, CpuPOPCNT)
12660
    /* LAHF-SAHF insns in 64-bit mode.  */
12661
3.67k
    || (flag_code == CODE_64BIT
12662
3.63k
        && (i.tm.base_opcode | 1) == 0x9f
12663
0
        && i.tm.opcode_space == SPACE_BASE))
12664
0
  x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
12665
3.67k
      if (is_cpu (&i.tm, CpuAVX)
12666
3.66k
    || is_cpu (&i.tm, CpuAVX2)
12667
    /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
12668
       XOP, FMA4, LPW, TBM, and AMX.  */
12669
3.66k
    || (i.tm.opcode_modifier.vex
12670
290
        && !is_cpu (&i.tm, CpuAVX512F)
12671
290
        && !is_cpu (&i.tm, CpuAVX512BW)
12672
290
        && !is_cpu (&i.tm, CpuAVX512DQ)
12673
290
        && !is_cpu (&i.tm, CpuXOP)
12674
290
        && !is_cpu (&i.tm, CpuFMA4)
12675
290
        && !is_cpu (&i.tm, CpuLWP)
12676
290
        && !is_cpu (&i.tm, CpuTBM)
12677
290
        && !(feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
12678
3.37k
    || is_cpu (&i.tm, CpuLZCNT)
12679
3.37k
    || is_cpu (&i.tm, CpuMovbe)
12680
3.37k
    || is_cpu (&i.tm, CpuXSAVES)
12681
3.37k
    || (feature_2_used
12682
3.37k
        & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
12683
3.37k
     | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
12684
3.37k
     | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
12685
293
  x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
12686
3.67k
      if (is_cpu (&i.tm, CpuAVX512F)
12687
3.67k
    || is_cpu (&i.tm, CpuAVX512BW)
12688
3.67k
    || is_cpu (&i.tm, CpuAVX512DQ)
12689
3.67k
    || is_cpu (&i.tm, CpuAVX512VL)
12690
    /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
12691
       AVX512-4FMAPS, and AVX512-4VNNIW.  */
12692
3.67k
    || (i.tm.opcode_modifier.evex
12693
18
        && !is_cpu (&i.tm, CpuAVX512ER)
12694
18
        && !is_cpu (&i.tm, CpuAVX512PF)
12695
18
        && !is_cpu (&i.tm, CpuAVX512_4FMAPS)
12696
18
        && !is_cpu (&i.tm, CpuAVX512_4VNNIW)))
12697
18
  x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
12698
3.67k
    }
12699
5.21k
#endif
12700
12701
  /* Tie dwarf2 debug info to the address at the start of the insn.
12702
     We can't do this after the insn has been output as the current
12703
     frag may have been closed off.  eg. by frag_var.  */
12704
5.21k
  dwarf2_emit_insn (0);
12705
12706
5.21k
  insn_start_frag = frag_now;
12707
5.21k
  insn_start_off = frag_now_fix ();
12708
12709
5.21k
  if (add_branch_padding_frag_p (&branch, &mf_jcc, last_insn))
12710
0
    {
12711
0
      char *p;
12712
      /* Branch can be 8 bytes.  Leave some room for prefixes.  */
12713
0
      unsigned int max_branch_padding_size = 14;
12714
12715
      /* Align section to boundary.  */
12716
0
      record_alignment (now_seg, align_branch_power);
12717
12718
      /* Make room for padding.  */
12719
0
      frag_grow (max_branch_padding_size);
12720
12721
      /* Start of the padding.  */
12722
0
      p = frag_more (0);
12723
12724
0
      fragP = frag_now;
12725
12726
0
      frag_var (rs_machine_dependent, max_branch_padding_size, 0,
12727
0
    ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
12728
0
    NULL, 0, p);
12729
12730
0
      fragP->tc_frag_data.mf_type = mf_jcc;
12731
0
      fragP->tc_frag_data.branch_type = branch;
12732
0
      fragP->tc_frag_data.max_bytes = max_branch_padding_size;
12733
0
    }
12734
12735
5.21k
  if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
12736
0
      && !pre_386_16bit_warned)
12737
0
    {
12738
0
      as_warn (_("use .code16 to ensure correct addressing mode"));
12739
0
      pre_386_16bit_warned = true;
12740
0
    }
12741
12742
  /* Output jumps.  */
12743
5.21k
  if (i.tm.opcode_modifier.jump == JUMP)
12744
21
    output_branch ();
12745
5.19k
  else if (i.tm.opcode_modifier.jump == JUMP_BYTE
12746
5.19k
     || i.tm.opcode_modifier.jump == JUMP_DWORD)
12747
38
    output_jump ();
12748
5.15k
  else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
12749
5
    output_interseg_jump ();
12750
5.15k
  else
12751
5.15k
    {
12752
      /* Output normal instructions here.  */
12753
5.15k
      char *p;
12754
5.15k
      unsigned char *q;
12755
5.15k
      unsigned int j;
12756
5.15k
      enum mf_cmp_kind mf_cmp;
12757
12758
5.15k
      if (avoid_fence
12759
0
    && (i.tm.base_opcode == 0xaee8
12760
0
        || i.tm.base_opcode == 0xaef0
12761
0
        || i.tm.base_opcode == 0xaef8))
12762
0
  {
12763
    /* Encode lfence, mfence, and sfence as
12764
       f0 83 04 24 00   lock addl $0x0, (%{re}sp).  */
12765
0
    if (flag_code == CODE_16BIT)
12766
0
      as_bad (_("Cannot convert `%s' in 16-bit mode"), insn_name (&i.tm));
12767
0
    else if (omit_lock_prefix)
12768
0
      as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
12769
0
        insn_name (&i.tm));
12770
0
    else if (now_seg != absolute_section)
12771
0
      {
12772
0
        offsetT val = 0x240483f0ULL;
12773
12774
0
        p = frag_more (5);
12775
0
        md_number_to_chars (p, val, 5);
12776
0
      }
12777
0
    else
12778
0
      abs_section_offset += 5;
12779
0
    return;
12780
0
  }
12781
12782
      /* Some processors fail on LOCK prefix. This options makes
12783
   assembler ignore LOCK prefix and serves as a workaround.  */
12784
5.15k
      if (omit_lock_prefix)
12785
0
  {
12786
0
    if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
12787
0
        && i.tm.opcode_modifier.isprefix)
12788
0
      return;
12789
0
    i.prefix[LOCK_PREFIX] = 0;
12790
0
  }
12791
12792
5.15k
      if (branch)
12793
  /* Skip if this is a branch.  */
12794
0
  ;
12795
5.15k
      else if (add_fused_jcc_padding_frag_p (&mf_cmp, last_insn))
12796
0
  {
12797
    /* Make room for padding.  */
12798
0
    frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
12799
0
    p = frag_more (0);
12800
12801
0
    fragP = frag_now;
12802
12803
0
    frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
12804
0
        ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
12805
0
        NULL, 0, p);
12806
12807
0
    fragP->tc_frag_data.mf_type = mf_cmp;
12808
0
    fragP->tc_frag_data.branch_type = align_branch_fused;
12809
0
    fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
12810
0
  }
12811
5.15k
      else if (add_branch_prefix_frag_p (last_insn))
12812
0
  {
12813
0
    unsigned int max_prefix_size = align_branch_prefix_size;
12814
12815
    /* Make room for padding.  */
12816
0
    frag_grow (max_prefix_size);
12817
0
    p = frag_more (0);
12818
12819
0
    fragP = frag_now;
12820
12821
0
    frag_var (rs_machine_dependent, max_prefix_size, 0,
12822
0
        ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
12823
0
        NULL, 0, p);
12824
12825
0
    fragP->tc_frag_data.max_bytes = max_prefix_size;
12826
0
  }
12827
12828
      /* Since the VEX/EVEX prefix contains the implicit prefix, we
12829
   don't need the explicit prefix.  */
12830
5.15k
      if (!is_any_vex_encoding (&i.tm))
12831
4.83k
  {
12832
4.83k
    switch (i.tm.opcode_modifier.opcodeprefix)
12833
4.83k
      {
12834
27
      case PREFIX_0X66:
12835
27
        add_prefix (0x66);
12836
27
        break;
12837
0
      case PREFIX_0XF2:
12838
0
        add_prefix (0xf2);
12839
0
        break;
12840
0
      case PREFIX_0XF3:
12841
0
        if (!is_padlock (&i.tm)
12842
0
      || (i.prefix[REP_PREFIX] != 0xf3))
12843
0
    add_prefix (0xf3);
12844
0
        break;
12845
4.80k
      case PREFIX_NONE:
12846
4.80k
        switch (i.opcode_length)
12847
4.80k
    {
12848
38
    case 2:
12849
38
      break;
12850
4.76k
    case 1:
12851
      /* Check for pseudo prefixes.  */
12852
4.76k
      if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
12853
4.76k
        break;
12854
4
      as_bad_where (insn_start_frag->fr_file,
12855
4
        insn_start_frag->fr_line,
12856
4
        _("pseudo prefix without instruction"));
12857
4
      return;
12858
0
    default:
12859
0
      abort ();
12860
4.80k
    }
12861
4.80k
        break;
12862
4.80k
      default:
12863
0
        abort ();
12864
4.83k
      }
12865
12866
4.82k
#ifdef OBJ_ELF
12867
    /* For x32, add a dummy REX_OPCODE prefix for mov/add with
12868
       R_X86_64_GOTTPOFF relocation so that linker can safely
12869
       perform IE->LE optimization.  A dummy REX_OPCODE prefix
12870
       is also needed for lea with R_X86_64_GOTPC32_TLSDESC
12871
       relocation for GDesc -> IE/LE optimization.  */
12872
4.82k
    if (x86_elf_abi == X86_64_X32_ABI
12873
0
        && !is_apx_rex2_encoding ()
12874
0
        && (dot_insn () ? i.insn_opcode_space
12875
0
            : i.tm.opcode_space) == SPACE_BASE
12876
0
        && i.operands == 2
12877
0
        && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
12878
0
      || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
12879
0
        && i.prefix[REX_PREFIX] == 0)
12880
0
      add_prefix (REX_OPCODE);
12881
4.82k
#endif
12882
12883
    /* The prefix bytes.  */
12884
38.6k
    for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
12885
33.8k
      if (*q)
12886
848
        frag_opcode_byte (*q);
12887
12888
4.82k
    if (is_apx_rex2_encoding ())
12889
3
      {
12890
3
        frag_opcode_byte (i.vex.bytes[0]);
12891
3
        frag_opcode_byte (i.vex.bytes[1]);
12892
3
      }
12893
4.82k
  }
12894
320
      else
12895
320
  {
12896
2.56k
    for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
12897
2.24k
      if (*q)
12898
2
        switch (j)
12899
2
    {
12900
2
    case SEG_PREFIX:
12901
2
    case ADDR_PREFIX:
12902
2
      frag_opcode_byte (*q);
12903
2
      break;
12904
0
    default:
12905
      /* There should be no other prefixes for instructions
12906
         with VEX prefix.  */
12907
0
      abort ();
12908
2
    }
12909
12910
    /* For EVEX instructions i.vrex should become 0 after
12911
       build_evex_prefix.  For VEX instructions upper 16 registers
12912
       aren't available, so VREX should be 0.  */
12913
320
    if (i.vrex)
12914
0
      abort ();
12915
    /* Now the VEX prefix.  */
12916
320
    if (now_seg != absolute_section)
12917
311
      {
12918
311
        p = frag_more (i.vex.length);
12919
1.25k
        for (j = 0; j < i.vex.length; j++)
12920
948
    p[j] = i.vex.bytes[j];
12921
311
      }
12922
9
    else
12923
9
      abs_section_offset += i.vex.length;
12924
320
  }
12925
12926
      /* Now the opcode; be careful about word order here!  */
12927
5.14k
      j = i.opcode_length;
12928
5.14k
      if (!i.vex.length)
12929
4.82k
  switch (i.tm.opcode_space)
12930
4.82k
    {
12931
4.03k
    case SPACE_BASE:
12932
4.03k
      break;
12933
788
    case SPACE_0F:
12934
788
      ++j;
12935
788
      break;
12936
0
    case SPACE_0F38:
12937
0
    case SPACE_0F3A:
12938
0
      j += 2;
12939
0
      break;
12940
0
    default:
12941
0
      abort ();
12942
4.82k
    }
12943
12944
5.14k
      if (now_seg == absolute_section)
12945
1.54k
  abs_section_offset += j;
12946
3.60k
      else if (j == 1)
12947
3.03k
  {
12948
3.03k
    FRAG_APPEND_1_CHAR (i.tm.base_opcode);
12949
3.03k
  }
12950
571
      else
12951
571
  {
12952
571
    p = frag_more (j);
12953
571
    if (!i.vex.length
12954
571
        && i.tm.opcode_space != SPACE_BASE)
12955
533
      {
12956
533
        *p++ = 0x0f;
12957
533
        if (i.tm.opcode_space != SPACE_0F)
12958
0
    *p++ = i.tm.opcode_space == SPACE_0F38
12959
0
           ? 0x38 : 0x3a;
12960
533
      }
12961
12962
571
    switch (i.opcode_length)
12963
571
      {
12964
38
      case 2:
12965
        /* Put out high byte first: can't use md_number_to_chars!  */
12966
38
        *p++ = (i.tm.base_opcode >> 8) & 0xff;
12967
        /* Fall through.  */
12968
571
      case 1:
12969
571
        *p = i.tm.base_opcode & 0xff;
12970
571
        break;
12971
0
      default:
12972
0
        abort ();
12973
0
        break;
12974
571
      }
12975
12976
571
  }
12977
12978
      /* Now the modrm byte and sib byte (if present).  */
12979
5.14k
      if (i.tm.opcode_modifier.modrm)
12980
2.18k
  {
12981
2.18k
    frag_opcode_byte ((i.rm.regmem << 0)
12982
2.18k
           | (i.rm.reg << 3)
12983
2.18k
           | (i.rm.mode << 6));
12984
    /* If i.rm.regmem == ESP (4)
12985
       && i.rm.mode != (Register mode)
12986
       && not 16 bit
12987
       ==> need second modrm byte.  */
12988
2.18k
    if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
12989
1.27k
        && i.rm.mode != 3
12990
736
        && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
12991
736
      frag_opcode_byte ((i.sib.base << 0)
12992
736
            | (i.sib.index << 3)
12993
736
            | (i.sib.scale << 6));
12994
2.18k
  }
12995
12996
5.14k
      if (i.disp_operands)
12997
1.05k
  output_disp (insn_start_frag, insn_start_off);
12998
12999
5.14k
      if (i.imm_operands)
13000
2.24k
  output_imm (insn_start_frag, insn_start_off);
13001
13002
      /*
13003
       * frag_now_fix () returning plain abs_section_offset when we're in the
13004
       * absolute section, and abs_section_offset not getting updated as data
13005
       * gets added to the frag breaks the logic below.
13006
       */
13007
5.14k
      if (now_seg != absolute_section)
13008
3.60k
  {
13009
3.60k
    j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
13010
3.60k
    if (j > 15)
13011
2
      {
13012
2
        if (dot_insn ())
13013
2
    as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
13014
2
      j);
13015
0
        else
13016
0
    as_bad (_("instruction length of %u bytes exceeds the limit of 15"),
13017
0
      j);
13018
2
      }
13019
3.60k
    else if (fragP)
13020
0
      {
13021
        /* NB: Don't add prefix with GOTPC relocation since
13022
     output_disp() above depends on the fixed encoding
13023
     length.  Can't add prefix with TLS relocation since
13024
     it breaks TLS linker optimization.  */
13025
0
        unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
13026
        /* Prefix count on the current instruction.  */
13027
0
        unsigned int count = i.vex.length;
13028
0
        unsigned int k;
13029
0
        for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
13030
    /* REX byte is encoded in VEX/EVEX prefix.  */
13031
0
    if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
13032
0
      count++;
13033
13034
        /* Count prefixes for extended opcode maps.  */
13035
0
        if (!i.vex.length)
13036
0
    switch (i.tm.opcode_space)
13037
0
      {
13038
0
      case SPACE_BASE:
13039
0
        break;
13040
0
      case SPACE_0F:
13041
0
        count++;
13042
0
        break;
13043
0
      case SPACE_0F38:
13044
0
      case SPACE_0F3A:
13045
0
        count += 2;
13046
0
        break;
13047
0
      default:
13048
0
        abort ();
13049
0
      }
13050
13051
0
        if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
13052
0
      == BRANCH_PREFIX)
13053
0
    {
13054
      /* Set the maximum prefix size in BRANCH_PREFIX
13055
         frag.  */
13056
0
      if (fragP->tc_frag_data.max_bytes > max)
13057
0
        fragP->tc_frag_data.max_bytes = max;
13058
0
      if (fragP->tc_frag_data.max_bytes > count)
13059
0
        fragP->tc_frag_data.max_bytes -= count;
13060
0
      else
13061
0
        fragP->tc_frag_data.max_bytes = 0;
13062
0
    }
13063
0
        else
13064
0
    {
13065
      /* Remember the maximum prefix size in FUSED_JCC_PADDING
13066
         frag.  */
13067
0
      unsigned int max_prefix_size;
13068
0
      if (align_branch_prefix_size > max)
13069
0
        max_prefix_size = max;
13070
0
      else
13071
0
        max_prefix_size = align_branch_prefix_size;
13072
0
      if (max_prefix_size > count)
13073
0
        fragP->tc_frag_data.max_prefix_length
13074
0
          = max_prefix_size - count;
13075
0
    }
13076
13077
        /* Use existing segment prefix if possible.  Use CS
13078
     segment prefix in 64-bit mode.  In 32-bit mode, use SS
13079
     segment prefix with ESP/EBP base register and use DS
13080
     segment prefix without ESP/EBP base register.  */
13081
0
        if (i.prefix[SEG_PREFIX])
13082
0
    fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
13083
0
        else if (flag_code == CODE_64BIT)
13084
0
    fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
13085
0
        else if (i.base_reg
13086
0
           && (i.base_reg->reg_num == 4
13087
0
         || i.base_reg->reg_num == 5))
13088
0
    fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
13089
0
        else
13090
0
    fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
13091
0
      }
13092
3.60k
  }
13093
5.14k
    }
13094
13095
  /* NB: Don't work with COND_JUMP86 without i386.  */
13096
5.21k
  if (align_branch_power
13097
0
      && now_seg != absolute_section
13098
0
      && cpu_arch_flags.bitfield.cpui386)
13099
0
    {
13100
      /* Terminate each frag so that we can add prefix and check for
13101
         fused jcc.  */
13102
0
      frag_wane (frag_now);
13103
0
      frag_new (0);
13104
0
    }
13105
13106
#ifdef DEBUG386
13107
  if (flag_debug)
13108
    {
13109
      pi ("" /*line*/, &i);
13110
    }
13111
#endif /* DEBUG386  */
13112
5.21k
}
13113
13114
/* Return the size of the displacement operand N.  */
13115
13116
static int
13117
disp_size (unsigned int n)
13118
1.05k
{
13119
1.05k
  int size = 4;
13120
13121
1.05k
  if (i.types[n].bitfield.disp64)
13122
0
    size = 8;
13123
1.05k
  else if (i.types[n].bitfield.disp8)
13124
7
    size = 1;
13125
1.04k
  else if (i.types[n].bitfield.disp16)
13126
188
    size = 2;
13127
1.05k
  return size;
13128
1.05k
}
13129
13130
/* Return the size of the immediate operand N.  */
13131
13132
static int
13133
imm_size (unsigned int n)
13134
2.32k
{
13135
2.32k
  int size = 4;
13136
2.32k
  if (i.types[n].bitfield.imm64)
13137
0
    size = 8;
13138
2.32k
  else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
13139
354
    size = 1;
13140
1.97k
  else if (i.types[n].bitfield.imm16)
13141
594
    size = 2;
13142
2.32k
  return size;
13143
2.32k
}
13144
13145
static void
13146
output_disp (fragS *insn_start_frag, offsetT insn_start_off)
13147
1.05k
{
13148
1.05k
  char *p;
13149
1.05k
  unsigned int n;
13150
13151
2.88k
  for (n = 0; n < i.operands; n++)
13152
1.82k
    {
13153
1.82k
      if (operand_type_check (i.types[n], disp))
13154
1.05k
  {
13155
1.05k
    int size = disp_size (n);
13156
13157
1.05k
    if (now_seg == absolute_section)
13158
598
      abs_section_offset += size;
13159
456
    else if (i.op[n].disps->X_op == O_constant)
13160
18
      {
13161
18
        offsetT val = i.op[n].disps->X_add_number;
13162
13163
18
        val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
13164
18
             size);
13165
18
        p = frag_more (size);
13166
18
        md_number_to_chars (p, val, size);
13167
18
      }
13168
438
    else
13169
438
      {
13170
438
        enum bfd_reloc_code_real reloc_type;
13171
438
        bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
13172
438
        bool sign = (flag_code == CODE_64BIT && size == 4
13173
433
         && (!want_disp32 (&i.tm)
13174
3
             || (i.tm.opcode_modifier.jump && !i.jumpabsolute
13175
0
           && !i.types[n].bitfield.baseindex)))
13176
8
        || pcrel;
13177
438
        fixS *fixP;
13178
13179
        /* We can't have 8 bit displacement here.  */
13180
438
        gas_assert (!i.types[n].bitfield.disp8);
13181
13182
        /* The PC relative address is computed relative
13183
     to the instruction boundary, so in case immediate
13184
     fields follows, we need to adjust the value.  */
13185
438
        if (pcrel && i.imm_operands)
13186
0
    {
13187
0
      unsigned int n1;
13188
0
      int sz = 0;
13189
13190
0
      for (n1 = 0; n1 < i.operands; n1++)
13191
0
        if (operand_type_check (i.types[n1], imm))
13192
0
          {
13193
      /* Only one immediate is allowed for PC
13194
         relative address, except with .insn.  */
13195
0
      gas_assert (sz == 0 || dot_insn ());
13196
0
      sz += imm_size (n1);
13197
0
          }
13198
      /* We should find at least one immediate.  */
13199
0
      gas_assert (sz != 0);
13200
0
      i.op[n].disps->X_add_number -= sz;
13201
0
    }
13202
13203
438
        p = frag_more (size);
13204
438
        reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
13205
438
        if (GOT_symbol
13206
6
      && GOT_symbol == i.op[n].disps->X_add_symbol
13207
0
      && (((reloc_type == BFD_RELOC_32
13208
0
      || reloc_type == BFD_RELOC_X86_64_32S
13209
0
      || (reloc_type == BFD_RELOC_64
13210
0
          && object_64bit))
13211
0
           && (i.op[n].disps->X_op == O_symbol
13212
0
         || (i.op[n].disps->X_op == O_add
13213
0
             && ((symbol_get_value_expression
13214
0
            (i.op[n].disps->X_op_symbol)->X_op)
13215
0
           == O_subtract))))
13216
0
          || reloc_type == BFD_RELOC_32_PCREL))
13217
0
    {
13218
0
      if (!object_64bit)
13219
0
        {
13220
0
          reloc_type = BFD_RELOC_32_GOT_PCREL;
13221
0
          i.has_gotpc_tls_reloc = true;
13222
0
          i.op[n].disps->X_add_number +=
13223
0
      encoding_length (insn_start_frag, insn_start_off, p);
13224
0
        }
13225
0
      else if (reloc_type == BFD_RELOC_64)
13226
0
        reloc_type = BFD_RELOC_64_GOT_PCREL;
13227
0
      else
13228
        /* Don't do the adjustment for x86-64, as there
13229
           the pcrel addressing is relative to the _next_
13230
           insn, and that is taken care of in other code.  */
13231
0
        reloc_type = BFD_RELOC_X86_64_GOTPC32;
13232
0
    }
13233
438
        else if (align_branch_power)
13234
0
    {
13235
0
      switch (reloc_type)
13236
0
        {
13237
0
        case BFD_RELOC_386_TLS_GD:
13238
0
        case BFD_RELOC_386_TLS_LDM:
13239
0
        case BFD_RELOC_386_TLS_IE:
13240
0
        case BFD_RELOC_386_TLS_IE_32:
13241
0
        case BFD_RELOC_386_TLS_GOTIE:
13242
0
        case BFD_RELOC_386_TLS_GOTDESC:
13243
0
        case BFD_RELOC_386_TLS_DESC_CALL:
13244
0
        case BFD_RELOC_X86_64_TLSGD:
13245
0
        case BFD_RELOC_X86_64_TLSLD:
13246
0
        case BFD_RELOC_X86_64_GOTTPOFF:
13247
0
        case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
13248
0
        case BFD_RELOC_X86_64_CODE_5_GOTTPOFF:
13249
0
        case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
13250
0
        case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
13251
0
        case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
13252
0
        case BFD_RELOC_X86_64_CODE_5_GOTPC32_TLSDESC:
13253
0
        case BFD_RELOC_X86_64_CODE_6_GOTPC32_TLSDESC:
13254
0
        case BFD_RELOC_X86_64_TLSDESC_CALL:
13255
0
          i.has_gotpc_tls_reloc = true;
13256
0
        default:
13257
0
          break;
13258
0
        }
13259
0
    }
13260
438
        fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
13261
438
          size, i.op[n].disps, pcrel,
13262
438
          reloc_type);
13263
13264
438
        if (flag_code == CODE_64BIT && size == 4 && pcrel
13265
0
      && !i.prefix[ADDR_PREFIX])
13266
0
    fixP->fx_signed = 1;
13267
13268
438
        if (i.base_reg && i.base_reg->reg_num == RegIP)
13269
0
    {
13270
0
      if (reloc_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
13271
0
        {
13272
          /* Set fx_tcbit for REX2 prefix.  */
13273
0
          if (is_apx_rex2_encoding ())
13274
0
      fixP->fx_tcbit = 1;
13275
0
          continue;
13276
0
        }
13277
0
    }
13278
        /* In 64-bit, i386_validate_fix updates only (%rip)
13279
     relocations.  */
13280
438
        else if (object_64bit)
13281
438
    continue;
13282
13283
0
#ifdef OBJ_ELF
13284
        /* Check for "call/jmp *mem", "push mem", "mov mem, %reg",
13285
     "movrs mem, %reg", "test %reg, mem" and "binop mem, %reg" where
13286
     binop is one of adc, add, and, cmp, or, sbb, sub, xor, or imul
13287
     instructions without data prefix.  Always generate
13288
     R_386_GOT32X for "sym*GOT" operand in 32-bit mode.  */
13289
0
        unsigned int space = dot_insn () ? i.insn_opcode_space
13290
0
                 : i.tm.opcode_space;
13291
0
        if (i.prefix[DATA_PREFIX] == 0
13292
0
      && (i.rm.mode == 2
13293
0
          || (i.rm.mode == 0 && i.rm.regmem == 5))
13294
0
      && ((space == SPACE_BASE
13295
0
           && i.tm.base_opcode == 0xff
13296
0
           && (i.rm.reg == 2 || i.rm.reg == 4 || i.rm.reg == 6))
13297
0
          || ((space == SPACE_BASE
13298
0
         || space == SPACE_0F38
13299
0
         || space == SPACE_MAP4)
13300
0
        && i.tm.base_opcode == 0x8b)
13301
0
          || ((space == SPACE_BASE
13302
0
         || space == SPACE_MAP4)
13303
0
        && (i.tm.base_opcode == 0x85
13304
0
            || (i.tm.base_opcode
13305
0
          | (i.operands > 2 ? 0x3a : 0x38)) == 0x3b))
13306
0
          || (((space == SPACE_0F
13307
          /* Because of the 0F prefix, no suitable relocation
13308
             exists for this unless it's REX2-encoded.  */
13309
0
          && is_apx_rex2_encoding ())
13310
0
         || space == SPACE_MAP4)
13311
0
        && i.tm.base_opcode == 0xaf)))
13312
0
    {
13313
0
      if (object_64bit)
13314
0
        {
13315
0
          if (reloc_type == BFD_RELOC_X86_64_GOTTPOFF)
13316
0
      {
13317
0
        if (space == SPACE_MAP4)
13318
0
          fixP->fx_tcbit3 = 1;
13319
0
        else if (space == SPACE_0F38 && i.rex)
13320
0
          fixP->fx_tcbit2 = 1;
13321
0
        else if (space == SPACE_0F38 || is_apx_rex2_encoding ())
13322
0
          fixP->fx_tcbit = 1;
13323
0
      }
13324
0
          else if (generate_relax_relocations)
13325
0
      {
13326
0
        if (space == SPACE_MAP4)
13327
0
          {
13328
0
            fixP->fx_tcbit3 = 1;
13329
0
            fixP->fx_tcbit2 = 1;
13330
0
          }
13331
0
        else if (space == SPACE_0F38)
13332
0
          {
13333
0
            fixP->fx_tcbit3 = 1;
13334
0
            if (i.rex)
13335
0
        fixP->fx_tcbit = 1;
13336
0
          }
13337
0
        else if (is_apx_rex2_encoding ())
13338
0
          fixP->fx_tcbit3 = 1;
13339
0
        else if (i.rex)
13340
0
          fixP->fx_tcbit2 = 1;
13341
0
        else
13342
0
          fixP->fx_tcbit = 1;
13343
0
      }
13344
0
        }
13345
0
      else if (generate_relax_relocations
13346
0
         ? (!shared || i.rm.mode != 0 || i.rm.regmem != 5)
13347
0
         : (!shared && i.rm.mode == 0 && i.rm.regmem == 5))
13348
0
        fixP->fx_tcbit2 = 1;
13349
0
    }
13350
0
#endif
13351
0
      }
13352
1.05k
  }
13353
1.82k
    }
13354
1.05k
}
13355
13356
static void
13357
output_imm (fragS *insn_start_frag, offsetT insn_start_off)
13358
2.24k
{
13359
2.24k
  char *p;
13360
2.24k
  unsigned int n;
13361
13362
5.31k
  for (n = 0; n < i.operands; n++)
13363
3.06k
    {
13364
3.06k
      if (operand_type_check (i.types[n], imm))
13365
2.32k
  {
13366
2.32k
    int size = imm_size (n);
13367
13368
2.32k
    if (now_seg == absolute_section)
13369
654
      abs_section_offset += size;
13370
1.67k
    else if (i.op[n].imms->X_op == O_constant)
13371
508
      {
13372
508
        offsetT val;
13373
13374
508
        val = offset_in_range (i.op[n].imms->X_add_number,
13375
508
             size);
13376
508
        p = frag_more (size);
13377
508
        md_number_to_chars (p, val, size);
13378
508
      }
13379
1.16k
    else
13380
1.16k
      {
13381
        /* Not absolute_section.
13382
     Need a 32-bit fixup (don't support 8bit
13383
     non-absolute imms).  Try to support other
13384
     sizes ...  */
13385
1.16k
        enum bfd_reloc_code_real reloc_type;
13386
1.16k
        int sign;
13387
13388
1.16k
        if (i.types[n].bitfield.imm32s
13389
1.11k
      && (i.suffix == QWORD_MNEM_SUFFIX
13390
1.11k
          || (!i.suffix && i.tm.opcode_modifier.no_lsuf)
13391
7
          || (i.prefix[REX_PREFIX] & REX_W)
13392
7
          || dot_insn ()))
13393
1.11k
    sign = 1;
13394
44
        else
13395
44
    sign = 0;
13396
13397
1.16k
        p = frag_more (size);
13398
1.16k
        reloc_type = reloc (size, 0, sign, i.reloc[n]);
13399
13400
        /*   This is tough to explain.  We end up with this one if we
13401
         * have operands that look like
13402
         * "_GLOBAL_OFFSET_TABLE_+[.-.L284]".  The goal here is to
13403
         * obtain the absolute address of the GOT, and it is strongly
13404
         * preferable from a performance point of view to avoid using
13405
         * a runtime relocation for this.  The actual sequence of
13406
         * instructions often look something like:
13407
         *
13408
         *  call  .L66
13409
         * .L66:
13410
         *  popl  %ebx
13411
         *  addl  $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
13412
         *
13413
         *   The call and pop essentially return the absolute address
13414
         * of the label .L66 and store it in %ebx.  The linker itself
13415
         * will ultimately change the first operand of the addl so
13416
         * that %ebx points to the GOT, but to keep things simple, the
13417
         * .o file must have this operand set so that it generates not
13418
         * the absolute address of .L66, but the absolute address of
13419
         * itself.  This allows the linker itself simply treat a GOTPC
13420
         * relocation as asking for a pcrel offset to the GOT to be
13421
         * added in, and the addend of the relocation is stored in the
13422
         * operand field for the instruction itself.
13423
         *
13424
         *   Our job here is to fix the operand so that it would add
13425
         * the correct offset so that %ebx would point to itself.  The
13426
         * thing that is tricky is that .-.L66 will point to the
13427
         * beginning of the instruction, so we need to further modify
13428
         * the operand so that it will point to itself.  There are
13429
         * other cases where you have something like:
13430
         *
13431
         *  .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
13432
         *
13433
         * and here no correction would be required.  Internally in
13434
         * the assembler we treat operands of this form as not being
13435
         * pcrel since the '.' is explicitly mentioned, and I wonder
13436
         * whether it would simplify matters to do it this way.  Who
13437
         * knows.  In earlier versions of the PIC patches, the
13438
         * pcrel_adjust field was used to store the correction, but
13439
         * since the expression is not pcrel, I felt it would be
13440
         * confusing to do it this way.  */
13441
13442
1.16k
        if ((reloc_type == BFD_RELOC_32
13443
1.13k
       || reloc_type == BFD_RELOC_X86_64_32S
13444
14
       || reloc_type == BFD_RELOC_64)
13445
1.14k
      && GOT_symbol
13446
0
      && GOT_symbol == i.op[n].imms->X_add_symbol
13447
0
      && (i.op[n].imms->X_op == O_symbol
13448
0
          || (i.op[n].imms->X_op == O_add
13449
0
        && ((symbol_get_value_expression
13450
0
             (i.op[n].imms->X_op_symbol)->X_op)
13451
0
            == O_subtract))))
13452
0
    {
13453
0
      if (!object_64bit)
13454
0
        reloc_type = BFD_RELOC_32_GOT_PCREL;
13455
0
      else if (size == 4)
13456
0
        reloc_type = BFD_RELOC_X86_64_GOTPC32;
13457
0
      else if (size == 8)
13458
0
        reloc_type = BFD_RELOC_64_GOT_PCREL;
13459
0
      i.has_gotpc_tls_reloc = true;
13460
0
      i.op[n].imms->X_add_number +=
13461
0
        encoding_length (insn_start_frag, insn_start_off, p);
13462
0
    }
13463
1.16k
        fix_new_exp (frag_now, p - frag_now->fr_literal, size,
13464
1.16k
         i.op[n].imms, 0, reloc_type);
13465
1.16k
      }
13466
2.32k
  }
13467
3.06k
    }
13468
2.24k
}
13469

13470
/* x86_cons_fix_new is called via the expression parsing code when a
13471
   reloc is needed.  We use this hook to get the correct .got reloc.  */
13472
static int cons_sign = -1;
13473
13474
void
13475
x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
13476
      expressionS *exp, bfd_reloc_code_real_type r)
13477
3.19k
{
13478
3.19k
  r = reloc (len, 0, cons_sign, r);
13479
13480
#ifdef TE_PE
13481
  if (exp->X_op == O_secrel)
13482
    {
13483
      exp->X_op = O_symbol;
13484
      r = BFD_RELOC_32_SECREL;
13485
    }
13486
  else if (exp->X_op == O_secidx)
13487
    r = BFD_RELOC_16_SECIDX;
13488
#endif
13489
13490
3.19k
  fix_new_exp (frag, off, len, exp, 0, r);
13491
3.19k
}
13492
13493
/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
13494
   purpose of the `.dc.a' internal pseudo-op.  */
13495
13496
int
13497
x86_address_bytes (void)
13498
224
{
13499
224
  if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
13500
0
    return 4;
13501
224
  return stdoutput->arch_info->bits_per_address / 8;
13502
224
}
13503
13504
#if (defined (OBJ_ELF) || defined (OBJ_MACH_O) || defined (TE_PE))
13505
/* Parse operands of the form
13506
   <symbol>@GOTOFF+<nnn>
13507
   and similar .plt or .got references.
13508
13509
   If we find one, set up the correct relocation in RELOC and copy the
13510
   input string, minus the `@GOTOFF' into a malloc'd buffer for
13511
   parsing by the calling routine.  Return this buffer, and if ADJUST
13512
   is non-null set it to the length of the string we removed from the
13513
   input line.  Otherwise return NULL.  */
13514
static char *
13515
lex_got (enum bfd_reloc_code_real *rel,
13516
   int *adjust,
13517
   i386_operand_type *types)
13518
2.82k
{
13519
  /* Some of the relocations depend on the size of what field is to
13520
     be relocated.  But in our callers i386_immediate and i386_displacement
13521
     we don't yet know the operand size (this will be set by insn
13522
     matching).  Hence we record the word32 relocation here,
13523
     and adjust the reloc according to the real size in reloc().  */
13524
2.82k
  char *cp;
13525
2.82k
  unsigned int j;
13526
13527
24.9k
  for (cp = input_line_pointer; *cp != '@'; cp++)
13528
24.6k
    if (is_end_of_stmt (*cp) || *cp == ',')
13529
2.50k
      return NULL;
13530
13531
2.35k
  for (j = 0; j < ARRAY_SIZE (gotrel); j++)
13532
2.33k
    {
13533
2.33k
      int len = gotrel[j].len;
13534
2.33k
      if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
13535
299
  {
13536
299
    if (gotrel[j].rel[object_64bit] != 0)
13537
297
      {
13538
297
        int first, second;
13539
297
        char *tmpbuf, *past_reloc;
13540
13541
297
        i.has_gotrel = true;
13542
297
        *rel = gotrel[j].rel[object_64bit];
13543
13544
297
        if (types)
13545
267
    {
13546
267
      if (flag_code != CODE_64BIT)
13547
259
        {
13548
259
          types->bitfield.imm32 = 1;
13549
259
          types->bitfield.disp32 = 1;
13550
259
        }
13551
8
      else
13552
8
        *types = gotrel[j].types64;
13553
267
    }
13554
13555
297
        if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
13556
11
    GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
13557
13558
        /* The length of the first part of our input line.  */
13559
297
        first = cp - input_line_pointer;
13560
13561
        /* The second part goes from after the reloc token until
13562
     (and including) an end_of_line char or comma.  */
13563
297
        past_reloc = cp + 1 + len;
13564
297
        cp = past_reloc;
13565
5.83k
        while (!is_end_of_stmt (*cp) && *cp != ',')
13566
5.53k
    ++cp;
13567
297
        second = cp + 1 - past_reloc;
13568
13569
        /* Allocate and copy string.  The trailing NUL shouldn't
13570
     be necessary, but be safe.  */
13571
297
        tmpbuf = XNEWVEC (char, first + second + 2);
13572
297
        memcpy (tmpbuf, input_line_pointer, first);
13573
297
        if (second != 0 && !is_whitespace (*past_reloc))
13574
    /* Replace the relocation token with ' ', so that
13575
       errors like foo@GOTOFF1 will be detected.  */
13576
36
    tmpbuf[first++] = ' ';
13577
261
        else
13578
    /* Increment length by 1 if the relocation token is
13579
       removed.  */
13580
261
    len++;
13581
297
        if (adjust)
13582
294
    *adjust = len;
13583
297
        memcpy (tmpbuf + first, past_reloc, second);
13584
297
        tmpbuf[first + second] = '\0';
13585
297
        return tmpbuf;
13586
297
      }
13587
13588
2
    as_bad (_("@%s reloc is not supported with %d-bit output format"),
13589
2
      gotrel[j].str, 1 << (5 + object_64bit));
13590
2
    return NULL;
13591
299
  }
13592
2.33k
    }
13593
13594
  /* Might be a symbol version string.  Don't as_bad here.  */
13595
26
  return NULL;
13596
325
}
13597
#else
13598
# define lex_got(reloc, adjust, types) NULL
13599
#endif
13600
13601
bfd_reloc_code_real_type
13602
x86_cons (expressionS *exp, int size)
13603
1.38k
{
13604
1.38k
  bfd_reloc_code_real_type got_reloc = NO_RELOC;
13605
13606
1.38k
  intel_syntax = -intel_syntax;
13607
1.38k
  exp->X_md = 0;
13608
1.38k
  expr_mode = expr_operator_none;
13609
13610
1.38k
#if defined (OBJ_ELF) || defined (TE_PE)
13611
1.38k
  if (size == 4
13612
# ifdef TE_PE
13613
      || (size == 2)
13614
# endif
13615
946
      || (object_64bit && size == 8))
13616
445
    {
13617
      /* Handle @GOTOFF and the like in an expression.  */
13618
445
      char *save;
13619
445
      char *gotfree_input_line;
13620
445
      int adjust = 0;
13621
13622
445
      save = input_line_pointer;
13623
445
      gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
13624
445
      if (gotfree_input_line)
13625
30
  input_line_pointer = gotfree_input_line;
13626
13627
445
      expression (exp);
13628
13629
445
      if (gotfree_input_line)
13630
30
  {
13631
    /* expression () has merrily parsed up to the end of line,
13632
       or a comma - in the wrong buffer.  Transfer how far
13633
       input_line_pointer has moved to the right buffer.  */
13634
30
    input_line_pointer = (save
13635
30
        + (input_line_pointer - gotfree_input_line)
13636
30
        + adjust);
13637
30
    free (gotfree_input_line);
13638
30
    if (exp->X_op == O_constant
13639
22
        || exp->X_op == O_absent
13640
22
        || exp->X_op == O_illegal
13641
22
        || exp->X_op == O_register
13642
9
        || exp->X_op == O_big)
13643
22
      {
13644
22
        char c = *input_line_pointer;
13645
22
        *input_line_pointer = 0;
13646
22
        as_bad (_("missing or invalid expression `%s'"), save);
13647
22
        *input_line_pointer = c;
13648
22
      }
13649
8
    else if ((got_reloc == BFD_RELOC_386_PLT32
13650
8
        || got_reloc == BFD_RELOC_32_PLT_PCREL)
13651
6
       && exp->X_op != O_symbol)
13652
2
      {
13653
      /* Allow directives like ".long foo@PLT - .L4".
13654
         BFD_RELOC_X86_64_PC32_TO_PLT32 has an explicit addend and
13655
         BFD_RELOC_386_PC32_TO_PLT32 has an implicit addend.  */
13656
2
        if (size == 4 && exp->X_op == O_subtract)
13657
0
    got_reloc = (object_64bit
13658
0
           ? BFD_RELOC_X86_64_PC32_TO_PLT32
13659
0
           : BFD_RELOC_386_PC32_TO_PLT32);
13660
2
        else
13661
2
    {
13662
2
      char c = *input_line_pointer;
13663
2
      *input_line_pointer = 0;
13664
2
      as_bad (_("invalid PLT expression `%s'"), save);
13665
2
      *input_line_pointer = c;
13666
2
    }
13667
2
      }
13668
30
  }
13669
445
    }
13670
943
  else
13671
943
#endif
13672
943
    expression (exp);
13673
13674
1.38k
  intel_syntax = -intel_syntax;
13675
13676
1.38k
  if (intel_syntax)
13677
1.24k
    i386_intel_simplify (exp);
13678
13679
  /* If not 64bit, massage value, to account for wraparound when !BFD64.  */
13680
1.38k
  if (size <= 4 && expr_mode == expr_operator_present
13681
562
      && exp->X_op == O_constant && !object_64bit)
13682
0
    exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
13683
13684
1.38k
  return got_reloc;
13685
1.38k
}
13686
13687
static void
13688
signed_cons (int size)
13689
0
{
13690
0
  if (object_64bit)
13691
0
    cons_sign = 1;
13692
0
  cons (size);
13693
0
  cons_sign = -1;
13694
0
}
13695
13696
static void
13697
s_insn (int dummy ATTRIBUTE_UNUSED)
13698
7.50k
{
13699
7.50k
  char mnemonic[MAX_MNEM_SIZE], *line = input_line_pointer, *ptr;
13700
7.50k
  char *saved_ilp = find_end_of_line (line, false), saved_char;
13701
7.50k
  const char *end;
13702
7.50k
  unsigned int j;
13703
7.50k
  valueT val;
13704
7.50k
  bool vex = false, xop = false;
13705
7.50k
  enum { evex_none, evex_basic, evex_nd } evex = evex_none;
13706
7.50k
  struct last_insn *last_insn;
13707
13708
7.50k
  init_globals ();
13709
13710
7.50k
  saved_char = *saved_ilp;
13711
7.50k
  *saved_ilp = 0;
13712
13713
7.50k
  end = parse_insn (line, mnemonic, parse_prefix);
13714
7.50k
  if (end == NULL)
13715
0
    {
13716
5.09k
  bad:
13717
5.09k
      *saved_ilp = saved_char;
13718
5.09k
      input_line_pointer = saved_ilp;
13719
5.09k
      ignore_rest_of_line ();
13720
5.09k
      i.tm.mnem_off = 0;
13721
5.09k
      memset (&pp, 0, sizeof (pp));
13722
5.09k
      return;
13723
0
    }
13724
7.50k
  line += end - line;
13725
13726
7.50k
  current_templates.start = &i.tm;
13727
7.50k
  current_templates.end = &i.tm + 1;
13728
7.50k
  i.tm.mnem_off = MN__insn;
13729
7.50k
  i.tm.extension_opcode = None;
13730
13731
7.50k
  if (startswith (line, "VEX")
13732
296
      && (line[3] == '.' || is_whitespace (line[3])))
13733
296
    {
13734
296
      vex = true;
13735
296
      line += 3;
13736
296
    }
13737
7.20k
  else if (startswith (line, "XOP") && ISDIGIT (line[3]))
13738
534
    {
13739
534
      char *e;
13740
534
      unsigned long n = strtoul (line + 3, &e, 16);
13741
13742
534
      if (e == line + 5 && n >= 0x08 && n <= 0x1f
13743
0
    && (*e == '.' || is_whitespace (*e)))
13744
0
  {
13745
0
    xop = true;
13746
    /* Arrange for build_vex_prefix() to emit 0x8f.  */
13747
0
    i.tm.opcode_space = SPACE_XOP08;
13748
0
    i.insn_opcode_space = n;
13749
0
    line = e;
13750
0
  }
13751
534
    }
13752
6.67k
  else if (startswith (line, "EVEX")
13753
3.03k
     && (line[4] == '.' || is_whitespace (line[4])))
13754
3.03k
    {
13755
3.03k
      evex = evex_basic;
13756
3.03k
      line += 4;
13757
3.03k
    }
13758
13759
7.50k
  if (vex || xop
13760
7.50k
      ? pp.encoding == encoding_evex
13761
7.50k
      : evex
13762
7.20k
  ? pp.encoding == encoding_vex
13763
3.03k
    || pp.encoding == encoding_vex3
13764
7.20k
  : pp.encoding != encoding_default)
13765
207
    {
13766
207
      as_bad (_("pseudo-prefix conflicts with encoding specifier"));
13767
207
      goto bad;
13768
207
    }
13769
13770
7.29k
  if (line > end && pp.encoding == encoding_default)
13771
2.01k
    pp.encoding = evex ? encoding_evex : encoding_vex;
13772
13773
7.29k
  if (pp.encoding != encoding_default)
13774
3.12k
    {
13775
      /* Only address size and segment override prefixes are permitted with
13776
         VEX/XOP/EVEX encodings.  */
13777
3.12k
      const unsigned char *p = i.prefix;
13778
13779
24.9k
      for (j = 0; j < ARRAY_SIZE (i.prefix); ++j, ++p)
13780
21.8k
  {
13781
21.8k
    if (!*p)
13782
21.8k
      continue;
13783
13784
0
    switch (j)
13785
0
      {
13786
0
      case SEG_PREFIX:
13787
0
      case ADDR_PREFIX:
13788
0
        break;
13789
0
      default:
13790
0
      as_bad (_("illegal prefix used with VEX/XOP/EVEX"));
13791
0
      goto bad;
13792
0
      }
13793
0
  }
13794
3.12k
    }
13795
13796
7.29k
  if (line > end && *line == '.')
13797
1.67k
    {
13798
      /* Length specifier (VEX.L, XOP.L, EVEX.L'L).  */
13799
1.67k
      switch (line[1])
13800
1.67k
  {
13801
1.11k
  case 'L':
13802
1.11k
    switch (line[2])
13803
1.11k
      {
13804
0
      case '0':
13805
0
        if (evex)
13806
0
    i.tm.opcode_modifier.evex = EVEX128;
13807
0
        else
13808
0
    i.tm.opcode_modifier.vex = VEX128;
13809
0
        break;
13810
13811
1.11k
      case '1':
13812
1.11k
        if (evex)
13813
1.11k
    i.tm.opcode_modifier.evex = EVEX256;
13814
0
        else
13815
0
    i.tm.opcode_modifier.vex = VEX256;
13816
1.11k
        break;
13817
13818
0
      case '2':
13819
0
        if (evex)
13820
0
    i.tm.opcode_modifier.evex = EVEX512;
13821
0
        break;
13822
13823
0
      case '3':
13824
0
        if (evex)
13825
0
    i.tm.opcode_modifier.evex = EVEX_L3;
13826
0
        break;
13827
13828
0
      case 'I':
13829
0
        if (line[3] == 'G')
13830
0
    {
13831
0
      if (evex)
13832
0
        i.tm.opcode_modifier.evex = EVEXLIG;
13833
0
      else
13834
0
        i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
13835
0
      ++line;
13836
0
    }
13837
0
        break;
13838
1.11k
      }
13839
13840
1.11k
    if (i.tm.opcode_modifier.vex || i.tm.opcode_modifier.evex)
13841
1.11k
      line += 3;
13842
1.11k
    break;
13843
13844
276
  case '1':
13845
276
    if (line[2] == '2' && line[3] == '8')
13846
0
      {
13847
0
        if (evex)
13848
0
    i.tm.opcode_modifier.evex = EVEX128;
13849
0
        else
13850
0
    i.tm.opcode_modifier.vex = VEX128;
13851
0
        line += 4;
13852
0
      }
13853
276
    break;
13854
13855
0
  case '2':
13856
0
    if (line[2] == '5' && line[3] == '6')
13857
0
      {
13858
0
        if (evex)
13859
0
    i.tm.opcode_modifier.evex = EVEX256;
13860
0
        else
13861
0
    i.tm.opcode_modifier.vex = VEX256;
13862
0
        line += 4;
13863
0
      }
13864
0
    break;
13865
13866
2
  case '5':
13867
2
    if (evex && line[2] == '1' && line[3] == '2')
13868
0
      {
13869
0
        i.tm.opcode_modifier.evex = EVEX512;
13870
0
        line += 4;
13871
0
      }
13872
2
    break;
13873
1.67k
  }
13874
1.67k
    }
13875
13876
7.29k
  if (line > end && *line == '.')
13877
561
    {
13878
      /* embedded prefix (VEX.pp, XOP.pp, EVEX.pp).  */
13879
561
      switch (line[1])
13880
561
  {
13881
0
  case 'N':
13882
0
    if (line[2] == 'P')
13883
0
      line += 3;
13884
0
    break;
13885
13886
1
  case '6':
13887
1
    if (line[2] == '6')
13888
1
      {
13889
1
        i.tm.opcode_modifier.opcodeprefix = PREFIX_0X66;
13890
1
        line += 3;
13891
1
      }
13892
1
    break;
13893
13894
0
  case 'F': case 'f':
13895
0
    if (line[2] == '3')
13896
0
      {
13897
0
        i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
13898
0
        line += 3;
13899
0
      }
13900
0
    else if (line[2] == '2')
13901
0
      {
13902
0
        i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF2;
13903
0
        line += 3;
13904
0
      }
13905
0
    break;
13906
561
  }
13907
561
    }
13908
13909
7.29k
  if (line > end && !xop && *line == '.')
13910
560
    {
13911
      /* Encoding space (VEX.mmmmm, EVEX.mmmm).  */
13912
560
      switch (line[1])
13913
560
  {
13914
0
  case '0':
13915
0
    if (TOUPPER (line[2]) != 'F')
13916
0
      break;
13917
0
    if (line[3] == '.' || is_whitespace (line[3]))
13918
0
      {
13919
0
        i.insn_opcode_space = SPACE_0F;
13920
0
        line += 3;
13921
0
      }
13922
0
    else if (line[3] == '3'
13923
0
       && (line[4] == '8' || TOUPPER (line[4]) == 'A')
13924
0
       && (line[5] == '.' || is_whitespace (line[5])))
13925
0
      {
13926
0
        i.insn_opcode_space = line[4] == '8' ? SPACE_0F38 : SPACE_0F3A;
13927
0
        line += 5;
13928
0
      }
13929
0
    break;
13930
13931
0
  case 'M':
13932
0
    if (ISDIGIT (line[2]) && line[2] != '0')
13933
0
      {
13934
0
        char *e;
13935
0
        unsigned long n = strtoul (line + 2, &e, 10);
13936
13937
0
        if (n <= (evex ? 15 : 31)
13938
0
      && (*e == '.' || is_whitespace (*e)))
13939
0
    {
13940
0
      i.insn_opcode_space = n;
13941
0
      line = e;
13942
0
    }
13943
0
      }
13944
0
    break;
13945
560
  }
13946
560
    }
13947
13948
7.29k
  if (line > end && *line == '.' && line[1] == 'W')
13949
6
    {
13950
      /* VEX.W, XOP.W, EVEX.W  */
13951
6
      switch (line[2])
13952
6
  {
13953
1
  case '0':
13954
1
    i.tm.opcode_modifier.vexw = VEXW0;
13955
1
    break;
13956
13957
5
  case '1':
13958
5
    i.tm.opcode_modifier.vexw = VEXW1;
13959
5
    break;
13960
13961
0
  case 'I':
13962
0
    if (line[3] == 'G')
13963
0
      {
13964
0
        i.tm.opcode_modifier.vexw = VEXWIG;
13965
0
        ++line;
13966
0
      }
13967
0
    break;
13968
6
  }
13969
13970
6
      if (i.tm.opcode_modifier.vexw)
13971
6
  line += 3;
13972
6
    }
13973
13974
7.29k
  if (line > end && evex && *line == '.')
13975
554
    {
13976
554
      if (line[1] == 'N' && line[2] == 'D')
13977
0
  {
13978
0
    evex = evex_nd;
13979
0
    line += 3;
13980
0
  }
13981
554
      else if (line[1] == 'Z' && line[2] == 'U')
13982
0
  {
13983
0
    i.tm.opcode_modifier.operandconstraint = ZERO_UPPER;
13984
0
    line += 3;
13985
0
  }
13986
554
    }
13987
13988
7.29k
  if (line > end && *line && !is_whitespace (*line))
13989
555
    {
13990
      /* Improve diagnostic a little.  */
13991
555
      if (*line == '.' && line[1] && !is_whitespace (line[1]))
13992
554
  ++line;
13993
555
      goto done;
13994
555
    }
13995
13996
  /* Before processing the opcode expression, find trailing "+r" or
13997
     "/<digit>" specifiers.  */
13998
6.73k
  for (ptr = line; ; ++ptr)
13999
9.81k
    {
14000
9.81k
      unsigned long n;
14001
9.81k
      char *e;
14002
14003
9.81k
      ptr = strpbrk (ptr, "+/,");
14004
9.81k
      if (ptr == NULL || *ptr == ',')
14005
6.71k
  break;
14006
14007
3.09k
      if (*ptr == '+' && ptr[1] == 'r'
14008
4
    && (ptr[2] == ',' || (is_whitespace (ptr[2]) && ptr[3] == ',')))
14009
2
  {
14010
2
    *ptr = ' ';
14011
2
    ptr[1] = ' ';
14012
2
    i.short_form = true;
14013
2
    break;
14014
2
  }
14015
14016
3.09k
      if (*ptr == '/' && ISDIGIT (ptr[1])
14017
17
    && (n = strtoul (ptr + 1, &e, 8)) < 8
14018
17
    && e == ptr + 2
14019
17
    && (ptr[2] == ',' || (is_whitespace (ptr[2]) && ptr[3] == ',')))
14020
17
  {
14021
17
    *ptr = ' ';
14022
17
    ptr[1] = ' ';
14023
17
    i.tm.extension_opcode = n;
14024
17
    i.tm.opcode_modifier.modrm = 1;
14025
17
    break;
14026
17
  }
14027
3.09k
    }
14028
14029
6.73k
  input_line_pointer = line;
14030
6.73k
  val = get_absolute_expression ();
14031
6.73k
  line = input_line_pointer;
14032
14033
6.73k
  if (i.short_form && (val & 7))
14034
0
    as_warn ("`+r' assumes low three opcode bits to be clear");
14035
14036
7.23k
  for (j = 1; j < sizeof(val); ++j)
14037
7.23k
    if (!(val >> (j * 8)))
14038
6.73k
      break;
14039
14040
  /* Trim off a prefix if present.  */
14041
6.73k
  if (j > 1 && !vex && !xop && !evex)
14042
496
    {
14043
496
      uint8_t byte = val >> ((j - 1) * 8);
14044
14045
496
      switch (byte)
14046
496
  {
14047
0
  case DATA_PREFIX_OPCODE:
14048
0
  case REPE_PREFIX_OPCODE:
14049
0
  case REPNE_PREFIX_OPCODE:
14050
0
    if (!add_prefix (byte))
14051
0
      goto bad;
14052
0
    val &= ((uint64_t)1 << (--j * 8)) - 1;
14053
0
    break;
14054
496
  }
14055
496
    }
14056
14057
6.73k
  if (evex == evex_basic && *line == '{')
14058
8
    {
14059
8
      int length = check_Scc_OszcOperations (line);
14060
14061
8
      if (length > 0)
14062
8
  {
14063
8
    line += length;
14064
8
    if (is_whitespace (*line))
14065
0
      ++line;
14066
14067
8
    if (i.tm.opcode_modifier.operandconstraint)
14068
0
      {
14069
0
        as_bad (_("SCC/OSZC specifier cannot be used here"));
14070
0
        goto bad;
14071
0
      }
14072
8
    i.tm.opcode_modifier.operandconstraint = SCC;
14073
8
  }
14074
8
    }
14075
14076
  /* Parse operands, if any, before evaluating encoding space.  */
14077
6.73k
  if (*line == ',')
14078
5.71k
    {
14079
5.71k
      i.memshift = -1;
14080
14081
5.71k
      ptr = parse_operands (line + 1, &i386_mnemonics[MN__insn]);
14082
5.71k
      this_operand = -1;
14083
5.71k
      if (!ptr)
14084
4.88k
  goto bad;
14085
823
      line = ptr;
14086
14087
823
      if (!i.operands)
14088
0
  {
14089
0
    as_bad (_("expecting operand after ','; got nothing"));
14090
0
    goto done;
14091
0
  }
14092
14093
823
      if (i.mem_operands > 1)
14094
5
  {
14095
5
    as_bad (_("too many memory references for `%s'"),
14096
5
      &i386_mnemonics[MN__insn]);
14097
5
    goto done;
14098
5
  }
14099
14100
      /* No need to distinguish encoding_evex and encoding_evex512.  */
14101
818
      if (pp.encoding == encoding_evex512)
14102
0
  pp.encoding = encoding_evex;
14103
818
    }
14104
14105
  /* Trim off encoding space.  */
14106
1.84k
  if (j > 1 && !i.insn_opcode_space && (val >> ((j - 1) * 8)) == 0x0f)
14107
496
    {
14108
496
      uint8_t byte = val >> ((--j - 1) * 8);
14109
14110
496
      i.insn_opcode_space = SPACE_0F;
14111
496
      switch (byte & -(j > 1 && !pp.rex2_encoding
14112
0
           && (pp.encoding != encoding_egpr || evex)))
14113
496
  {
14114
0
  case 0x38:
14115
0
    i.insn_opcode_space = SPACE_0F38;
14116
0
    --j;
14117
0
    break;
14118
0
  case 0x3a:
14119
0
    i.insn_opcode_space = SPACE_0F3A;
14120
0
    --j;
14121
0
    break;
14122
496
  }
14123
496
      i.tm.opcode_space = i.insn_opcode_space;
14124
496
      val &= ((uint64_t)1 << (j * 8)) - 1;
14125
496
    }
14126
1.84k
  if (!i.tm.opcode_space && (vex || evex))
14127
    /* Arrange for build_vex_prefix() to properly emit 0xC4/0xC5.
14128
       Also avoid hitting abort() there or in build_evex_prefix().  */
14129
331
    i.tm.opcode_space = i.insn_opcode_space == SPACE_0F ? SPACE_0F
14130
331
               : SPACE_0F38;
14131
14132
1.84k
  if (j > 2)
14133
0
    {
14134
0
      as_bad (_("opcode residual (%#"PRIx64") too wide"), (uint64_t) val);
14135
0
      goto done;
14136
0
    }
14137
1.84k
  i.opcode_length = j;
14138
14139
  /* Handle operands, if any.  */
14140
1.84k
  if (i.operands)
14141
818
    {
14142
818
      i386_operand_type combined;
14143
818
      expressionS *disp_exp = NULL;
14144
818
      bool changed;
14145
14146
818
      if (pp.encoding == encoding_egpr)
14147
0
  {
14148
0
    if (vex || xop)
14149
0
      {
14150
0
        as_bad (_("eGPR use conflicts with encoding specifier"));
14151
0
        goto done;
14152
0
      }
14153
0
    if (evex)
14154
0
      pp.encoding = encoding_evex;
14155
0
    else
14156
0
      pp.encoding = encoding_default;
14157
0
  }
14158
14159
      /* Are we to emit ModR/M encoding?  */
14160
818
      if (!i.short_form
14161
818
    && (i.mem_operands
14162
798
        || i.reg_operands > (pp.encoding != encoding_default)
14163
550
        || i.tm.extension_opcode != None))
14164
271
  i.tm.opcode_modifier.modrm = 1;
14165
14166
818
      if (!i.tm.opcode_modifier.modrm
14167
547
    && (i.reg_operands
14168
547
        > i.short_form + 0U + (pp.encoding != encoding_default)
14169
547
        || i.mem_operands))
14170
0
  {
14171
0
    as_bad (_("too many register/memory operands"));
14172
0
    goto done;
14173
0
  }
14174
14175
      /* Enforce certain constraints on operands.  */
14176
818
      switch (i.reg_operands + i.mem_operands
14177
818
        + (i.tm.extension_opcode != None)
14178
818
        + (i.tm.opcode_modifier.operandconstraint == SCC))
14179
818
  {
14180
539
  case 0:
14181
539
    if (i.short_form)
14182
0
      {
14183
0
        as_bad (_("too few register/memory operands"));
14184
0
        goto done;
14185
0
      }
14186
    /* Fall through.  */
14187
540
  case 1:
14188
540
    if (i.tm.opcode_modifier.modrm)
14189
1
      {
14190
1
        as_bad (_("too few register/memory operands"));
14191
1
        goto done;
14192
1
      }
14193
    /* Fall through.  */
14194
800
  case 2:
14195
800
    if (evex == evex_nd)
14196
0
      {
14197
0
        as_bad (_("too few register/memory operands"));
14198
0
        goto done;
14199
0
      }
14200
800
    break;
14201
14202
800
  case 4:
14203
1
    if (i.imm_operands
14204
1
        && (i.op[0].imms->X_op != O_constant
14205
0
      || !fits_in_imm4 (i.op[0].imms->X_add_number)))
14206
1
      {
14207
1
        as_bad (_("constant doesn't fit in %d bits"), evex ? 3 : 4);
14208
1
        goto done;
14209
1
      }
14210
    /* Fall through.  */
14211
16
  case 3:
14212
16
    if (i.tm.opcode_modifier.operandconstraint == SCC)
14213
0
      break;
14214
16
    if (pp.encoding != encoding_default)
14215
16
      {
14216
16
        i.tm.opcode_modifier.vexvvvv = (i.tm.extension_opcode == None
14217
14
                && evex != evex_nd)
14218
16
               ? VexVVVV_SRC1 : VexVVVV_DST;
14219
16
        break;
14220
16
      }
14221
    /* Fall through.  */
14222
0
  default:
14223
0
    as_bad (_("too many register/memory operands"));
14224
0
    goto done;
14225
818
  }
14226
14227
      /* Bring operands into canonical order (imm, mem, reg).  */
14228
816
      do
14229
848
  {
14230
848
    changed = false;
14231
14232
1.32k
    for (j = 1; j < i.operands; ++j)
14233
474
      {
14234
474
        if ((!operand_type_check (i.types[j - 1], imm)
14235
368
       && operand_type_check (i.types[j], imm))
14236
446
      || (i.types[j - 1].bitfield.class != ClassNone
14237
322
          && i.types[j].bitfield.class == ClassNone))
14238
60
    {
14239
60
      swap_2_operands (j - 1, j);
14240
60
      changed = true;
14241
60
    }
14242
474
      }
14243
848
  }
14244
848
      while (changed);
14245
14246
      /* For Intel syntax swap the order of register operands.  */
14247
816
      if (intel_syntax)
14248
350
  switch (i.reg_operands)
14249
350
    {
14250
78
    case 0:
14251
88
    case 1:
14252
88
      break;
14253
14254
0
    case 4:
14255
0
      swap_2_operands (i.imm_operands + i.mem_operands + 1, i.operands - 2);
14256
      /* Fall through.  */
14257
0
    case 3:
14258
262
    case 2:
14259
262
      swap_2_operands (i.imm_operands + i.mem_operands, i.operands - 1);
14260
262
      break;
14261
14262
0
    default:
14263
0
      abort ();
14264
350
    }
14265
14266
      /* Enforce constraints when using VSIB.  */
14267
816
      if (i.index_reg
14268
0
    && (i.index_reg->reg_type.bitfield.xmmword
14269
0
        || i.index_reg->reg_type.bitfield.ymmword
14270
0
        || i.index_reg->reg_type.bitfield.zmmword))
14271
0
  {
14272
0
    if (pp.encoding == encoding_default)
14273
0
      {
14274
0
        as_bad (_("VSIB unavailable with legacy encoding"));
14275
0
        goto done;
14276
0
      }
14277
14278
0
    if (pp.encoding == encoding_evex
14279
0
        && i.reg_operands > 1)
14280
0
      {
14281
        /* We could allow two register operands, encoding the 2nd one in
14282
     an 8-bit immediate like for 4-register-operand insns, but that
14283
     would require ugly fiddling with process_operands() and/or
14284
     build_modrm_byte().  */
14285
0
        as_bad (_("too many register operands with VSIB"));
14286
0
        goto done;
14287
0
      }
14288
14289
0
    i.tm.opcode_modifier.sib = 1;
14290
0
  }
14291
14292
      /* Establish operand size encoding.  */
14293
816
      operand_type_set (&combined, 0);
14294
14295
1.37k
      for (j = i.imm_operands; j < i.operands; ++j)
14296
557
  {
14297
    /* Look for 8-bit operands that use old registers.  */
14298
557
    if (pp.encoding != encoding_default
14299
553
        && flag_code == CODE_64BIT
14300
545
        && i.types[j].bitfield.class == Reg
14301
2
        && i.types[j].bitfield.byte
14302
0
        && !(i.op[j].regs->reg_flags & (RegRex | RegRex2 | RegRex64))
14303
0
        && i.op[j].regs->reg_num > 3)
14304
0
      as_bad (_("can't encode register '%s%s' with VEX/XOP/EVEX"),
14305
0
        register_prefix, i.op[j].regs->reg_name);
14306
14307
557
    i.types[j].bitfield.instance = InstanceNone;
14308
14309
557
    if (operand_type_check (i.types[j], disp))
14310
18
      {
14311
18
        i.types[j].bitfield.baseindex = 1;
14312
18
        disp_exp = i.op[j].disps;
14313
18
      }
14314
14315
557
    if (evex && i.types[j].bitfield.baseindex)
14316
16
      {
14317
16
        unsigned int n = i.memshift;
14318
14319
16
        if (i.types[j].bitfield.byte)
14320
0
    n = 0;
14321
16
        else if (i.types[j].bitfield.word)
14322
0
    n = 1;
14323
16
        else if (i.types[j].bitfield.dword)
14324
0
    n = 2;
14325
16
        else if (i.types[j].bitfield.qword)
14326
0
    n = 3;
14327
16
        else if (i.types[j].bitfield.xmmword)
14328
0
    n = 4;
14329
16
        else if (i.types[j].bitfield.ymmword)
14330
0
    n = 5;
14331
16
        else if (i.types[j].bitfield.zmmword)
14332
0
    n = 6;
14333
14334
16
        if (i.memshift < 32 && n != i.memshift)
14335
0
    as_warn ("conflicting memory operand size specifiers");
14336
16
        i.memshift = n;
14337
16
      }
14338
14339
557
    if ((i.broadcast.type || i.broadcast.bytes)
14340
42
        && j == i.broadcast.operand)
14341
0
      continue;
14342
14343
557
    combined = operand_type_or (combined, i.types[j]);
14344
557
    combined.bitfield.class = ClassNone;
14345
557
  }
14346
14347
816
      switch ((i.broadcast.type ? i.broadcast.type : 1)
14348
816
        << (i.memshift < 32 ? i.memshift : 0))
14349
816
  {
14350
0
  case 64: combined.bitfield.zmmword = 1; break;
14351
0
  case 32: combined.bitfield.ymmword = 1; break;
14352
0
  case 16: combined.bitfield.xmmword = 1; break;
14353
0
  case  8: combined.bitfield.qword = 1; break;
14354
3
  case  4: combined.bitfield.dword = 1; break;
14355
816
  }
14356
14357
816
      if (pp.encoding == encoding_default)
14358
536
  {
14359
536
    if (flag_code == CODE_64BIT && combined.bitfield.qword)
14360
0
      i.rex |= REX_W;
14361
536
    else if ((flag_code == CODE_16BIT ? combined.bitfield.dword
14362
536
              : combined.bitfield.word)
14363
1
             && !add_prefix (DATA_PREFIX_OPCODE))
14364
0
      goto done;
14365
536
  }
14366
280
      else if (!i.tm.opcode_modifier.vexw)
14367
280
  {
14368
280
    if (flag_code == CODE_64BIT)
14369
272
      {
14370
272
        if (combined.bitfield.qword)
14371
0
          i.tm.opcode_modifier.vexw = VEXW1;
14372
272
        else if (combined.bitfield.dword)
14373
5
          i.tm.opcode_modifier.vexw = VEXW0;
14374
272
      }
14375
14376
280
    if (!i.tm.opcode_modifier.vexw)
14377
275
      i.tm.opcode_modifier.vexw = VEXWIG;
14378
280
  }
14379
14380
816
      if (vex || xop)
14381
250
  {
14382
250
    if (!i.tm.opcode_modifier.vex)
14383
250
      {
14384
250
        if (combined.bitfield.ymmword)
14385
0
          i.tm.opcode_modifier.vex = VEX256;
14386
250
        else if (combined.bitfield.xmmword)
14387
248
          i.tm.opcode_modifier.vex = VEX128;
14388
250
      }
14389
250
  }
14390
566
      else if (evex)
14391
30
  {
14392
30
    if (!i.tm.opcode_modifier.evex)
14393
30
      {
14394
        /* Do _not_ consider AVX512VL here.  */
14395
30
        if (combined.bitfield.zmmword)
14396
0
          i.tm.opcode_modifier.evex = EVEX512;
14397
30
        else if (combined.bitfield.ymmword)
14398
3
          i.tm.opcode_modifier.evex = EVEX256;
14399
27
        else if (combined.bitfield.xmmword)
14400
22
          i.tm.opcode_modifier.evex = EVEX128;
14401
30
      }
14402
14403
30
    if (i.memshift >= 32)
14404
30
      {
14405
30
        unsigned int n = 0;
14406
14407
30
        switch (i.tm.opcode_modifier.evex)
14408
30
    {
14409
0
    case EVEX512: n = 64; break;
14410
3
    case EVEX256: n = 32; break;
14411
22
    case EVEX128: n = 16; break;
14412
30
    }
14413
14414
30
        if (i.broadcast.type)
14415
14
    n /= i.broadcast.type;
14416
14417
30
        if (n > 0)
14418
111
    for (i.memshift = 0; !(n & 1); n >>= 1)
14419
86
      ++i.memshift;
14420
5
        else if (disp_exp != NULL && disp_exp->X_op == O_constant
14421
2
           && disp_exp->X_add_number != 0
14422
2
           && pp.disp_encoding != disp_encoding_32bit)
14423
2
    {
14424
2
      if (!quiet_warnings)
14425
2
        as_warn ("cannot determine memory operand size");
14426
2
      pp.disp_encoding = disp_encoding_32bit;
14427
2
    }
14428
30
      }
14429
30
  }
14430
14431
816
      if (i.memshift >= 32)
14432
791
  i.memshift = 0;
14433
25
      else if (!evex)
14434
0
  pp.encoding = encoding_error;
14435
14436
816
      if (i.disp_operands && !optimize_disp (&i.tm))
14437
0
  goto done;
14438
14439
      /* Establish size for immediate operands.  */
14440
1.45k
      for (j = 0; j < i.imm_operands; ++j)
14441
643
  {
14442
643
    expressionS *expP = i.op[j].imms;
14443
14444
643
    gas_assert (operand_type_check (i.types[j], imm));
14445
643
    operand_type_set (&i.types[j], 0);
14446
14447
643
    if (i.imm_bits[j] > 32)
14448
0
      i.types[j].bitfield.imm64 = 1;
14449
643
    else if (i.imm_bits[j] > 16)
14450
0
      {
14451
0
        if (flag_code == CODE_64BIT && (i.flags[j] & Operand_Signed))
14452
0
    i.types[j].bitfield.imm32s = 1;
14453
0
        else
14454
0
    i.types[j].bitfield.imm32 = 1;
14455
0
      }
14456
643
    else if (i.imm_bits[j] > 8)
14457
0
      i.types[j].bitfield.imm16 = 1;
14458
643
    else if (i.imm_bits[j] > 0)
14459
0
      {
14460
0
        if (i.flags[j] & Operand_Signed)
14461
0
    i.types[j].bitfield.imm8s = 1;
14462
0
        else
14463
0
    i.types[j].bitfield.imm8 = 1;
14464
0
      }
14465
643
    else if (expP->X_op == O_constant)
14466
539
      {
14467
539
        i.types[j] = smallest_imm_type (expP->X_add_number);
14468
539
        i.types[j].bitfield.imm1 = 0;
14469
        /* Oddly enough imm_size() checks imm64 first, so the bit needs
14470
     zapping since smallest_imm_type() sets it unconditionally.  */
14471
539
        if (flag_code != CODE_64BIT)
14472
0
    {
14473
0
      i.types[j].bitfield.imm64 = 0;
14474
0
      i.types[j].bitfield.imm32s = 0;
14475
0
      i.types[j].bitfield.imm32 = 1;
14476
0
    }
14477
539
        else if (i.types[j].bitfield.imm32 || i.types[j].bitfield.imm32s)
14478
539
    i.types[j].bitfield.imm64 = 0;
14479
539
      }
14480
104
    else
14481
      /* Non-constant expressions are sized heuristically.  */
14482
104
      switch (flag_code)
14483
104
        {
14484
96
        case CODE_64BIT: i.types[j].bitfield.imm32s = 1; break;
14485
0
        case CODE_32BIT: i.types[j].bitfield.imm32 = 1; break;
14486
8
        case CODE_16BIT: i.types[j].bitfield.imm16 = 1; break;
14487
104
        }
14488
643
  }
14489
14490
2.01k
      for (j = 0; j < i.operands; ++j)
14491
1.20k
  i.tm_types[j] = i.types[j];
14492
14493
816
      process_operands ();
14494
816
    }
14495
14496
  /* Don't set opcode until after processing operands, to avoid any
14497
     potential special casing there.  */
14498
1.84k
  i.tm.base_opcode |= val;
14499
14500
1.84k
  if (pp.encoding == encoding_error
14501
1.84k
      || (pp.encoding != encoding_evex
14502
1.84k
    ? i.broadcast.type || i.broadcast.bytes
14503
1.80k
      || i.rounding.type != rc_none
14504
1.80k
      || i.mask.reg
14505
1.84k
    : (i.mem_operands && i.rounding.type != rc_none)
14506
40
      || ((i.broadcast.type || i.broadcast.bytes)
14507
14
    && !(i.flags[i.broadcast.operand] & Operand_Mem))))
14508
14
    {
14509
14
      as_bad (_("conflicting .insn operands"));
14510
14
      goto done;
14511
14
    }
14512
14513
1.82k
  if (vex || xop)
14514
290
    {
14515
290
      if (is_apx_evex_encoding ())
14516
0
  {
14517
0
    as_bad (_("APX functionality cannot be used with %s encodings"),
14518
0
      vex ? "VEX" : "XOP");
14519
0
    goto done;
14520
0
  }
14521
14522
290
      if (!i.tm.opcode_modifier.vex)
14523
42
  i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
14524
14525
290
      build_vex_prefix (NULL);
14526
290
      i.rex &= REX_OPCODE;
14527
290
    }
14528
1.53k
  else if (evex)
14529
26
    {
14530
26
      if (!i.tm.opcode_modifier.evex)
14531
15
  i.tm.opcode_modifier.evex = EVEXLIG;
14532
14533
      /* To keep earlier .insn uses working as far as possible, take the
14534
   legacy path when opcode space is 4 bits wide (impossible to encode in
14535
   extended EVEX), and when no "extended" syntax elements are used.  */
14536
26
      if ((!is_apx_evex_encoding () || i.insn_opcode_space > 7)
14537
26
    && evex == evex_basic
14538
26
    && !i.tm.opcode_modifier.operandconstraint)
14539
18
  build_evex_prefix ();
14540
8
      else if (i.insn_opcode_space > 7)
14541
0
  {
14542
0
    as_bad (_("opcode space cannot be larger than 7"));
14543
0
    goto done;
14544
0
  }
14545
8
      else if (evex == evex_nd && (i.broadcast.type || i.broadcast.bytes))
14546
0
  {
14547
0
    as_bad (_("ND and broadcast cannot be used at the same time"));
14548
0
    goto done;
14549
0
  }
14550
8
      else if (pp.has_nf && i.mask.reg)
14551
0
  {
14552
0
    as_bad (_("{nf} and masking cannot be used at the same time"));
14553
0
    goto done;
14554
0
  }
14555
8
      else if (i.tm.opcode_modifier.operandconstraint == SCC
14556
8
         && (pp.has_nf || i.mask.reg))
14557
0
  {
14558
0
    as_bad (_("SCC cannot be used at the same time {nf} / masking"));
14559
0
    goto done;
14560
0
  }
14561
8
      else if (!build_apx_evex_prefix (evex == evex_nd))
14562
0
  goto done;
14563
26
      i.rex &= REX_OPCODE;
14564
26
    }
14565
1.51k
  else
14566
1.51k
    establish_rex ();
14567
14568
1.82k
  last_insn = &seg_info(now_seg)->tc_segment_info_data.last_insn;
14569
1.82k
  output_insn (last_insn);
14570
1.82k
  last_insn->kind = last_insn_directive;
14571
1.82k
  last_insn->name = ".insn directive";
14572
1.82k
  last_insn->file = as_where (&last_insn->line);
14573
14574
1.82k
#ifdef OBJ_ELF
14575
  /* PS: SCFI is enabled only for System V AMD64 ABI.  The ABI check has been
14576
     performed in i386_target_format.  */
14577
1.82k
  if (flag_synth_cfi)
14578
0
    as_bad (_("SCFI: hand-crafting instructions not supported"));
14579
1.82k
#endif
14580
14581
2.40k
 done:
14582
2.40k
  *saved_ilp = saved_char;
14583
2.40k
  input_line_pointer = line;
14584
14585
2.40k
  demand_empty_rest_of_line ();
14586
14587
  /* Make sure dot_insn() won't yield "true" anymore.  */
14588
2.40k
  i.tm.mnem_off = 0;
14589
14590
2.40k
  current_templates.start = NULL;
14591
2.40k
  memset (&pp, 0, sizeof (pp));
14592
2.40k
}
14593
14594
#ifdef TE_PE
14595
static void
14596
pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
14597
{
14598
  expressionS exp;
14599
14600
  do
14601
    {
14602
      expression (&exp);
14603
      if (exp.X_op == O_symbol)
14604
  exp.X_op = O_secrel;
14605
14606
      emit_expr (&exp, 4);
14607
    }
14608
  while (*input_line_pointer++ == ',');
14609
14610
  input_line_pointer--;
14611
  demand_empty_rest_of_line ();
14612
}
14613
14614
static void
14615
pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
14616
{
14617
  expressionS exp;
14618
14619
  do
14620
    {
14621
      expression (&exp);
14622
      if (exp.X_op == O_symbol)
14623
  exp.X_op = O_secidx;
14624
14625
      emit_expr (&exp, 2);
14626
    }
14627
  while (*input_line_pointer++ == ',');
14628
14629
  input_line_pointer--;
14630
  demand_empty_rest_of_line ();
14631
}
14632
#endif
14633
14634
/* Handle Rounding Control / SAE specifiers.  */
14635
14636
static char *
14637
RC_SAE_specifier (const char *pstr)
14638
1.11k
{
14639
1.11k
  unsigned int j;
14640
14641
6.70k
  for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
14642
5.59k
    {
14643
5.59k
      if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
14644
1
  {
14645
1
    if (i.rounding.type != rc_none)
14646
0
      {
14647
0
        as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
14648
0
        return NULL;
14649
0
      }
14650
14651
1
    switch (pp.encoding)
14652
1
      {
14653
1
      case encoding_default:
14654
1
      case encoding_egpr:
14655
1
        pp.encoding = encoding_evex512;
14656
1
        break;
14657
0
      case encoding_evex:
14658
0
      case encoding_evex512:
14659
0
        break;
14660
0
      default:
14661
0
        return NULL;
14662
1
      }
14663
14664
1
    i.rounding.type = RC_NamesTable[j].type;
14665
14666
1
    return (char *)(pstr + RC_NamesTable[j].len);
14667
1
  }
14668
5.59k
    }
14669
14670
1.11k
  return NULL;
14671
1.11k
}
14672
14673
/* Handle Vector operations.  */
14674
14675
static char *
14676
check_VecOperations (char *op_string)
14677
3.41k
{
14678
3.41k
  const reg_entry *mask;
14679
3.41k
  const char *saved;
14680
3.41k
  char *end_op;
14681
14682
3.46k
  while (*op_string)
14683
3.42k
    {
14684
3.42k
      saved = op_string;
14685
3.42k
      if (*op_string == '{')
14686
3.42k
  {
14687
3.42k
    op_string++;
14688
3.42k
    if (is_whitespace (*op_string))
14689
11
      op_string++;
14690
14691
    /* Check broadcasts.  */
14692
3.42k
    if (startswith (op_string, "1to"))
14693
26
      {
14694
26
        unsigned int bcst_type;
14695
14696
26
        if (i.broadcast.type)
14697
0
    goto duplicated_vec_op;
14698
14699
26
        op_string += 3;
14700
26
        if (*op_string == '8')
14701
0
    bcst_type = 8;
14702
26
        else if (*op_string == '4')
14703
3
    bcst_type = 4;
14704
23
        else if (*op_string == '2')
14705
23
    bcst_type = 2;
14706
0
        else if (*op_string == '1'
14707
0
           && *(op_string+1) == '6')
14708
0
    {
14709
0
      bcst_type = 16;
14710
0
      op_string++;
14711
0
    }
14712
0
        else if (*op_string == '3'
14713
0
           && *(op_string+1) == '2')
14714
0
    {
14715
0
      bcst_type = 32;
14716
0
      op_string++;
14717
0
    }
14718
0
        else
14719
0
    {
14720
0
      as_bad (_("Unsupported broadcast: `%s'"), saved);
14721
0
      return NULL;
14722
0
    }
14723
26
        op_string++;
14724
14725
26
        switch (pp.encoding)
14726
26
    {
14727
12
    case encoding_default:
14728
12
    case encoding_egpr:
14729
12
      pp.encoding = encoding_evex;
14730
12
      break;
14731
14
    case encoding_evex:
14732
14
    case encoding_evex512:
14733
14
      break;
14734
0
    default:
14735
0
      goto unknown_vec_op;
14736
26
    }
14737
14738
26
        i.broadcast.type = bcst_type;
14739
26
        i.broadcast.operand = this_operand;
14740
14741
        /* For .insn a data size specifier may be appended.  */
14742
26
        if (dot_insn () && *op_string == ':')
14743
0
    goto dot_insn_modifier;
14744
26
      }
14745
    /* Check .insn special cases.  */
14746
3.39k
    else if (dot_insn () && *op_string == ':')
14747
0
      {
14748
0
      dot_insn_modifier:
14749
0
        switch (op_string[1])
14750
0
    {
14751
0
      unsigned long n;
14752
14753
0
    case 'd':
14754
0
      if (i.memshift < 32)
14755
0
        goto duplicated_vec_op;
14756
14757
0
      n = strtoul (op_string + 2, &end_op, 0);
14758
0
      if (n)
14759
0
        for (i.memshift = 0; !(n & 1); n >>= 1)
14760
0
          ++i.memshift;
14761
0
      if (i.memshift < 32 && n == 1)
14762
0
        op_string = end_op;
14763
0
      break;
14764
14765
0
    case 's': case 'u':
14766
      /* This isn't really a "vector" operation, but a sign/size
14767
         specifier for immediate operands of .insn.  Note that AT&T
14768
         syntax handles the same in i386_immediate().  */
14769
0
      if (!intel_syntax)
14770
0
        break;
14771
14772
0
      if (i.imm_bits[this_operand])
14773
0
        goto duplicated_vec_op;
14774
14775
0
      n = strtoul (op_string + 2, &end_op, 0);
14776
0
      if (n && n <= (flag_code == CODE_64BIT ? 64 : 32))
14777
0
        {
14778
0
          i.imm_bits[this_operand] = n;
14779
0
          if (op_string[1] == 's')
14780
0
      i.flags[this_operand] |= Operand_Signed;
14781
0
          op_string = end_op;
14782
0
        }
14783
0
      break;
14784
0
    }
14785
0
      }
14786
    /* Check masking operation.  */
14787
3.39k
    else if ((mask = parse_register (op_string, &end_op)) != NULL)
14788
2.25k
      {
14789
2.25k
        if (mask == &bad_reg)
14790
0
    return NULL;
14791
14792
        /* k0 can't be used for write mask.  */
14793
2.25k
        if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
14794
2.25k
    {
14795
2.25k
      as_bad (_("`%s%s' can't be used for write mask"),
14796
2.25k
        register_prefix, mask->reg_name);
14797
2.25k
      return NULL;
14798
2.25k
    }
14799
14800
0
        if (!i.mask.reg)
14801
0
    {
14802
0
      i.mask.reg = mask;
14803
0
      i.mask.operand = this_operand;
14804
0
    }
14805
0
        else if (i.mask.reg->reg_num)
14806
0
    goto duplicated_vec_op;
14807
0
        else
14808
0
    {
14809
0
      i.mask.reg = mask;
14810
14811
      /* Only "{z}" is allowed here.  No need to check
14812
         zeroing mask explicitly.  */
14813
0
      if (i.mask.operand != (unsigned int) this_operand)
14814
0
        {
14815
0
          as_bad (_("invalid write mask `%s'"), saved);
14816
0
          return NULL;
14817
0
        }
14818
0
    }
14819
14820
0
        op_string = end_op;
14821
0
      }
14822
    /* Check zeroing-flag for masking operation.  */
14823
1.14k
    else if (*op_string == 'z')
14824
26
      {
14825
26
        if (!i.mask.reg)
14826
21
    {
14827
21
      i.mask.reg = reg_k0;
14828
21
      i.mask.zeroing = 1;
14829
21
      i.mask.operand = this_operand;
14830
21
    }
14831
5
        else
14832
5
    {
14833
5
      if (i.mask.zeroing)
14834
5
        {
14835
5
        duplicated_vec_op:
14836
5
          as_bad (_("duplicated `%s'"), saved);
14837
5
          return NULL;
14838
5
        }
14839
14840
0
      i.mask.zeroing = 1;
14841
14842
      /* Only "{%k}" is allowed here.  No need to check mask
14843
         register explicitly.  */
14844
0
      if (i.mask.operand != (unsigned int) this_operand)
14845
0
        {
14846
0
          as_bad (_("invalid zeroing-masking `%s'"),
14847
0
            saved);
14848
0
          return NULL;
14849
0
        }
14850
0
    }
14851
14852
21
        op_string++;
14853
21
      }
14854
1.11k
    else if (intel_syntax
14855
1.11k
       && (op_string = RC_SAE_specifier (op_string)) != NULL)
14856
0
      i.rounding.modifier = true;
14857
1.11k
    else
14858
1.11k
      goto unknown_vec_op;
14859
14860
47
    if (is_whitespace (*op_string))
14861
0
      op_string++;
14862
47
    if (*op_string != '}')
14863
0
      {
14864
0
        as_bad (_("missing `}' in `%s'"), saved);
14865
0
        return NULL;
14866
0
      }
14867
47
    op_string++;
14868
14869
47
    if (is_whitespace (*op_string))
14870
0
      ++op_string;
14871
14872
47
    continue;
14873
47
  }
14874
1.11k
    unknown_vec_op:
14875
      /* We don't know this one.  */
14876
1.11k
      as_bad (_("unknown vector operation: `%s'"), saved);
14877
1.11k
      return NULL;
14878
3.42k
    }
14879
14880
42
  if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
14881
16
    {
14882
16
      as_bad (_("zeroing-masking only allowed with write mask"));
14883
16
      return NULL;
14884
16
    }
14885
14886
26
  return op_string;
14887
42
}
14888
14889
static int
14890
i386_immediate (char *imm_start)
14891
1.45k
{
14892
1.45k
  char *save_input_line_pointer;
14893
1.45k
  char *gotfree_input_line;
14894
1.45k
  segT exp_seg = 0;
14895
1.45k
  expressionS *exp;
14896
1.45k
  i386_operand_type types;
14897
14898
1.45k
  operand_type_set (&types, ~0);
14899
14900
1.45k
  if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
14901
0
    {
14902
0
      as_bad (_("at most %d immediate operands are allowed"),
14903
0
        MAX_IMMEDIATE_OPERANDS);
14904
0
      return 0;
14905
0
    }
14906
14907
1.45k
  exp = &im_expressions[i.imm_operands++];
14908
1.45k
  i.op[this_operand].imms = exp;
14909
14910
1.45k
  if (is_whitespace (*imm_start))
14911
1
    ++imm_start;
14912
14913
1.45k
  save_input_line_pointer = input_line_pointer;
14914
1.45k
  input_line_pointer = imm_start;
14915
14916
1.45k
  gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
14917
1.45k
  if (gotfree_input_line)
14918
0
    input_line_pointer = gotfree_input_line;
14919
14920
1.45k
  expr_mode = expr_operator_none;
14921
1.45k
  exp_seg = expression (exp);
14922
14923
  /* For .insn immediates there may be a size specifier.  */
14924
1.45k
  if (dot_insn () && *input_line_pointer == '{' && input_line_pointer[1] == ':'
14925
0
      && (input_line_pointer[2] == 's' || input_line_pointer[2] == 'u'))
14926
0
    {
14927
0
      char *e;
14928
0
      unsigned long n = strtoul (input_line_pointer + 3, &e, 0);
14929
14930
0
      if (*e == '}' && n && n <= (flag_code == CODE_64BIT ? 64 : 32))
14931
0
  {
14932
0
    i.imm_bits[this_operand] = n;
14933
0
    if (input_line_pointer[2] == 's')
14934
0
      i.flags[this_operand] |= Operand_Signed;
14935
0
    input_line_pointer = e + 1;
14936
0
  }
14937
0
    }
14938
14939
1.45k
  SKIP_WHITESPACE ();
14940
1.45k
  if (*input_line_pointer)
14941
731
    as_bad (_("junk `%s' after expression"), input_line_pointer);
14942
14943
1.45k
  input_line_pointer = save_input_line_pointer;
14944
1.45k
  if (gotfree_input_line)
14945
0
    {
14946
0
      free (gotfree_input_line);
14947
14948
0
      if (exp->X_op == O_constant)
14949
0
  exp->X_op = O_illegal;
14950
0
    }
14951
14952
1.45k
  if (exp_seg == reg_section)
14953
0
    {
14954
0
      as_bad (_("illegal immediate register operand %s"), imm_start);
14955
0
      return 0;
14956
0
    }
14957
14958
1.45k
  return i386_finalize_immediate (exp_seg, exp, types, imm_start);
14959
1.45k
}
14960
14961
static int
14962
i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
14963
       i386_operand_type types, const char *imm_start)
14964
3.01k
{
14965
3.01k
  if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
14966
7
    {
14967
7
      if (imm_start)
14968
7
  as_bad (_("missing or invalid immediate expression `%s'"),
14969
7
    imm_start);
14970
7
      return 0;
14971
7
    }
14972
3.00k
  else if (exp->X_op == O_constant)
14973
1.26k
    {
14974
      /* Size it properly later.  */
14975
1.26k
      i.types[this_operand].bitfield.imm64 = 1;
14976
14977
      /* If not 64bit, sign/zero extend val, to account for wraparound
14978
   when !BFD64.  */
14979
1.26k
      if (expr_mode == expr_operator_present
14980
619
    && flag_code != CODE_64BIT && !object_64bit)
14981
0
  exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
14982
1.26k
    }
14983
#ifdef OBJ_AOUT
14984
  else if (exp_seg != absolute_section
14985
     && exp_seg != text_section
14986
     && exp_seg != data_section
14987
     && exp_seg != bss_section
14988
     && exp_seg != undefined_section
14989
     && !bfd_is_com_section (exp_seg))
14990
    {
14991
      as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
14992
      return 0;
14993
    }
14994
#endif
14995
1.74k
  else
14996
1.74k
    {
14997
      /* This is an address.  The size of the address will be
14998
   determined later, depending on destination register,
14999
   suffix, or the default for the section.  */
15000
1.74k
      i.types[this_operand].bitfield.imm8 = 1;
15001
1.74k
      i.types[this_operand].bitfield.imm16 = 1;
15002
1.74k
      i.types[this_operand].bitfield.imm32 = 1;
15003
1.74k
      i.types[this_operand].bitfield.imm32s = 1;
15004
1.74k
      i.types[this_operand].bitfield.imm64 = 1;
15005
1.74k
      i.types[this_operand] = operand_type_and (i.types[this_operand],
15006
1.74k
            types);
15007
1.74k
    }
15008
15009
3.00k
  return 1;
15010
3.01k
}
15011
15012
static char *
15013
i386_scale (char *scale)
15014
0
{
15015
0
  offsetT val;
15016
0
  char *save = input_line_pointer;
15017
15018
0
  input_line_pointer = scale;
15019
0
  val = get_absolute_expression ();
15020
15021
0
  switch (val)
15022
0
    {
15023
0
    case 1:
15024
0
      i.log2_scale_factor = 0;
15025
0
      break;
15026
0
    case 2:
15027
0
      i.log2_scale_factor = 1;
15028
0
      break;
15029
0
    case 4:
15030
0
      i.log2_scale_factor = 2;
15031
0
      break;
15032
0
    case 8:
15033
0
      i.log2_scale_factor = 3;
15034
0
      break;
15035
0
    default:
15036
0
      {
15037
0
  char sep = *input_line_pointer;
15038
15039
0
  *input_line_pointer = '\0';
15040
0
  as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
15041
0
    scale);
15042
0
  *input_line_pointer = sep;
15043
0
  input_line_pointer = save;
15044
0
  return NULL;
15045
0
      }
15046
0
    }
15047
0
  if (i.log2_scale_factor != 0 && i.index_reg == 0)
15048
0
    {
15049
0
      as_warn (_("scale factor of %d without an index register"),
15050
0
         1 << i.log2_scale_factor);
15051
0
      i.log2_scale_factor = 0;
15052
0
    }
15053
0
  scale = input_line_pointer;
15054
0
  input_line_pointer = save;
15055
0
  return scale;
15056
0
}
15057
15058
static int
15059
i386_displacement (char *disp_start, char *disp_end)
15060
637
{
15061
637
  expressionS *exp;
15062
637
  segT exp_seg = 0;
15063
637
  char *save_input_line_pointer;
15064
637
  char *gotfree_input_line;
15065
637
  int override;
15066
637
  i386_operand_type bigdisp, types = anydisp;
15067
637
  int ret;
15068
15069
637
  if (i.disp_operands == MAX_MEMORY_OPERANDS)
15070
0
    {
15071
0
      as_bad (_("at most %d displacement operands are allowed"),
15072
0
        MAX_MEMORY_OPERANDS);
15073
0
      return 0;
15074
0
    }
15075
15076
637
  operand_type_set (&bigdisp, 0);
15077
637
  if (i.jumpabsolute
15078
637
      || i.types[this_operand].bitfield.baseindex
15079
630
      || (current_templates.start->opcode_modifier.jump != JUMP
15080
628
    && current_templates.start->opcode_modifier.jump != JUMP_DWORD))
15081
605
    {
15082
605
      i386_addressing_mode ();
15083
605
      override = (i.prefix[ADDR_PREFIX] != 0);
15084
605
      if (flag_code == CODE_64BIT)
15085
399
  {
15086
399
    bigdisp.bitfield.disp32 = 1;
15087
399
    if (!override)
15088
392
      bigdisp.bitfield.disp64 = 1;
15089
399
  }
15090
206
      else if ((flag_code == CODE_16BIT) ^ override)
15091
205
    bigdisp.bitfield.disp16 = 1;
15092
1
      else
15093
1
    bigdisp.bitfield.disp32 = 1;
15094
605
    }
15095
32
  else
15096
32
    {
15097
      /* For PC-relative branches, the width of the displacement may be
15098
   dependent upon data size, but is never dependent upon address size.
15099
   Also make sure to not unintentionally match against a non-PC-relative
15100
   branch template.  */
15101
32
      const insn_template *t = current_templates.start;
15102
32
      bool has_intel64 = false;
15103
15104
94
      while (++t < current_templates.end)
15105
92
  {
15106
92
    if (t->opcode_modifier.jump
15107
92
        != current_templates.start->opcode_modifier.jump)
15108
30
      break;
15109
62
    if ((t->opcode_modifier.isa64 >= INTEL64))
15110
32
      has_intel64 = true;
15111
62
  }
15112
32
      current_templates.end = t;
15113
15114
32
      override = (i.prefix[DATA_PREFIX] != 0);
15115
32
      if (flag_code == CODE_64BIT)
15116
31
  {
15117
31
    if ((override || i.suffix == WORD_MNEM_SUFFIX)
15118
14
        && (!intel64 || !has_intel64))
15119
0
      bigdisp.bitfield.disp16 = 1;
15120
31
    else
15121
31
      bigdisp.bitfield.disp32 = 1;
15122
31
  }
15123
1
      else
15124
1
  {
15125
1
    if (!override)
15126
1
      override = (i.suffix == (flag_code != CODE_16BIT
15127
1
             ? WORD_MNEM_SUFFIX
15128
1
             : LONG_MNEM_SUFFIX));
15129
1
    bigdisp.bitfield.disp32 = 1;
15130
1
    if ((flag_code == CODE_16BIT) ^ override)
15131
1
      {
15132
1
        bigdisp.bitfield.disp32 = 0;
15133
1
        bigdisp.bitfield.disp16 = 1;
15134
1
      }
15135
1
  }
15136
32
    }
15137
637
  i.types[this_operand] = operand_type_or (i.types[this_operand],
15138
637
             bigdisp);
15139
15140
637
  exp = &disp_expressions[i.disp_operands];
15141
637
  i.op[this_operand].disps = exp;
15142
637
  i.disp_operands++;
15143
637
  save_input_line_pointer = input_line_pointer;
15144
637
  input_line_pointer = disp_start;
15145
637
  END_STRING_AND_SAVE (disp_end);
15146
15147
637
#ifndef GCC_ASM_O_HACK
15148
637
#define GCC_ASM_O_HACK 0
15149
637
#endif
15150
#if GCC_ASM_O_HACK
15151
  END_STRING_AND_SAVE (disp_end + 1);
15152
  if (i.types[this_operand].bitfield.baseIndex
15153
      && displacement_string_end[-1] == '+')
15154
    {
15155
      /* This hack is to avoid a warning when using the "o"
15156
   constraint within gcc asm statements.
15157
   For instance:
15158
15159
   #define _set_tssldt_desc(n,addr,limit,type) \
15160
   __asm__ __volatile__ ( \
15161
   "movw %w2,%0\n\t" \
15162
   "movw %w1,2+%0\n\t" \
15163
   "rorl $16,%1\n\t" \
15164
   "movb %b1,4+%0\n\t" \
15165
   "movb %4,5+%0\n\t" \
15166
   "movb $0,6+%0\n\t" \
15167
   "movb %h1,7+%0\n\t" \
15168
   "rorl $16,%1" \
15169
   : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
15170
15171
   This works great except that the output assembler ends
15172
   up looking a bit weird if it turns out that there is
15173
   no offset.  You end up producing code that looks like:
15174
15175
   #APP
15176
   movw $235,(%eax)
15177
   movw %dx,2+(%eax)
15178
   rorl $16,%edx
15179
   movb %dl,4+(%eax)
15180
   movb $137,5+(%eax)
15181
   movb $0,6+(%eax)
15182
   movb %dh,7+(%eax)
15183
   rorl $16,%edx
15184
   #NO_APP
15185
15186
   So here we provide the missing zero.  */
15187
15188
      *displacement_string_end = '0';
15189
    }
15190
#endif
15191
637
  gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
15192
637
  if (gotfree_input_line)
15193
3
    input_line_pointer = gotfree_input_line;
15194
15195
637
  expr_mode = expr_operator_none;
15196
637
  exp_seg = expression (exp);
15197
15198
637
  SKIP_WHITESPACE ();
15199
637
  if (*input_line_pointer)
15200
120
    as_bad (_("junk `%s' after expression"), input_line_pointer);
15201
#if GCC_ASM_O_HACK
15202
  RESTORE_END_STRING (disp_end + 1);
15203
#endif
15204
637
  input_line_pointer = save_input_line_pointer;
15205
637
  if (gotfree_input_line)
15206
3
    {
15207
3
      free (gotfree_input_line);
15208
15209
3
      if (exp->X_op == O_constant || exp->X_op == O_register)
15210
0
  exp->X_op = O_illegal;
15211
3
    }
15212
15213
637
  ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
15214
15215
637
  RESTORE_END_STRING (disp_end);
15216
15217
637
  return ret;
15218
637
}
15219
15220
static int
15221
i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
15222
          i386_operand_type types, const char *disp_start)
15223
7.54k
{
15224
7.54k
  int ret = 1;
15225
15226
  /* We do this to make sure that the section symbol is in
15227
     the symbol table.  We will ultimately change the relocation
15228
     to be relative to the beginning of the section.  */
15229
7.54k
  if (i.reloc[this_operand] == BFD_RELOC_32_GOTOFF
15230
7.54k
      || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
15231
7.53k
      || i.reloc[this_operand] == BFD_RELOC_64_GOTOFF)
15232
3
    {
15233
3
      if (exp->X_op != O_symbol
15234
0
    && exp->X_op != O_add
15235
0
    && exp->X_op != O_subtract)
15236
0
  goto inv_disp;
15237
15238
3
      if (S_IS_LOCAL (exp->X_add_symbol)
15239
3
    && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
15240
0
    && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
15241
0
  section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
15242
15243
3
      if (exp->X_op != O_symbol)
15244
0
  {
15245
0
    if (S_IS_LOCAL (exp->X_op_symbol)
15246
0
        && S_GET_SEGMENT (exp->X_op_symbol) != undefined_section
15247
0
        && S_GET_SEGMENT (exp->X_op_symbol) != expr_section)
15248
0
      section_symbol (S_GET_SEGMENT (exp->X_op_symbol));
15249
15250
0
    exp->X_add_symbol = make_expr_symbol (exp);
15251
0
  }
15252
15253
3
      exp->X_op = O_subtract;
15254
3
      exp->X_op_symbol = GOT_symbol;
15255
3
      if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
15256
3
  i.reloc[this_operand] = BFD_RELOC_32_PCREL;
15257
0
      else if (i.reloc[this_operand] == BFD_RELOC_64_GOTOFF)
15258
0
  i.reloc[this_operand] = BFD_RELOC_64;
15259
0
      else
15260
0
  i.reloc[this_operand] = BFD_RELOC_32;
15261
3
    }
15262
15263
7.53k
  else if (exp->X_op == O_absent
15264
7.53k
     || exp->X_op == O_illegal
15265
7.53k
     || exp->X_op == O_big)
15266
5
    {
15267
5
    inv_disp:
15268
5
      as_bad (_("missing or invalid displacement expression `%s'"),
15269
5
        disp_start);
15270
5
      ret = 0;
15271
5
    }
15272
15273
7.53k
  else if (exp->X_op == O_constant)
15274
1.50k
    {
15275
      /* Sizing gets taken care of by optimize_disp().
15276
15277
   If not 64bit, sign/zero extend val, to account for wraparound
15278
   when !BFD64.  */
15279
1.50k
      if (expr_mode == expr_operator_present
15280
1.14k
    && flag_code != CODE_64BIT && !object_64bit)
15281
0
  exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
15282
1.50k
    }
15283
15284
#ifdef OBJ_AOUT
15285
  else if (exp_seg != absolute_section
15286
     && exp_seg != text_section
15287
     && exp_seg != data_section
15288
     && exp_seg != bss_section
15289
     && exp_seg != undefined_section
15290
     && !bfd_is_com_section (exp_seg))
15291
    {
15292
      as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
15293
      ret = 0;
15294
    }
15295
#endif
15296
15297
6.02k
  else if (current_templates.start->opcode_modifier.jump == JUMP_BYTE)
15298
0
    i.types[this_operand].bitfield.disp8 = 1;
15299
15300
  /* Check if this is a displacement only operand.  */
15301
7.54k
  if (!i.types[this_operand].bitfield.baseindex)
15302
7.51k
    i.types[this_operand] =
15303
7.51k
      operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
15304
7.51k
           operand_type_and (i.types[this_operand], types));
15305
15306
7.54k
  return ret;
15307
7.54k
}
15308
15309
/* Return the active addressing mode, taking address override and
15310
   registers forming the address into consideration.  Update the
15311
   address override prefix if necessary.  */
15312
15313
static enum flag_code
15314
i386_addressing_mode (void)
15315
15.1k
{
15316
15.1k
  enum flag_code addr_mode;
15317
15318
15.1k
  if (i.prefix[ADDR_PREFIX])
15319
7
    addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
15320
15.1k
  else if (flag_code == CODE_16BIT
15321
560
     && is_cpu (current_templates.start, CpuMPX)
15322
     /* Avoid replacing the "16-bit addressing not allowed" diagnostic
15323
        from md_assemble() by "is not a valid base/index expression"
15324
        when there is a base and/or index.  */
15325
0
     && !i.types[this_operand].bitfield.baseindex)
15326
0
    {
15327
      /* MPX insn memory operands with neither base nor index must be forced
15328
   to use 32-bit addressing in 16-bit mode.  */
15329
0
      addr_mode = CODE_32BIT;
15330
0
      i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
15331
0
      ++i.prefixes;
15332
0
      gas_assert (!i.types[this_operand].bitfield.disp16);
15333
0
      gas_assert (!i.types[this_operand].bitfield.disp32);
15334
0
    }
15335
15.1k
  else
15336
15.1k
    {
15337
15.1k
      addr_mode = flag_code;
15338
15339
15.1k
#if INFER_ADDR_PREFIX
15340
15.1k
      if (i.mem_operands == 0)
15341
14.7k
  {
15342
    /* Infer address prefix from the first memory operand.  */
15343
14.7k
    const reg_entry *addr_reg = i.base_reg;
15344
15345
14.7k
    if (addr_reg == NULL)
15346
14.5k
      addr_reg = i.index_reg;
15347
15348
14.7k
    if (addr_reg)
15349
188
      {
15350
188
        if (addr_reg->reg_type.bitfield.dword)
15351
147
    addr_mode = CODE_32BIT;
15352
41
        else if (flag_code != CODE_64BIT
15353
1
           && addr_reg->reg_type.bitfield.word)
15354
1
    addr_mode = CODE_16BIT;
15355
15356
188
        if (addr_mode != flag_code)
15357
148
    {
15358
148
      i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
15359
148
      i.prefixes += 1;
15360
      /* Change the size of any displacement too.  At most one
15361
         of Disp16 or Disp32 is set.
15362
         FIXME.  There doesn't seem to be any real need for
15363
         separate Disp16 and Disp32 flags.  The same goes for
15364
         Imm16 and Imm32.  Removing them would probably clean
15365
         up the code quite a lot.  */
15366
148
      if (flag_code != CODE_64BIT
15367
126
          && (i.types[this_operand].bitfield.disp16
15368
126
        || i.types[this_operand].bitfield.disp32))
15369
0
        {
15370
0
          static const i386_operand_type disp16_32 = {
15371
0
      .bitfield = { .disp16 = 1, .disp32 = 1 }
15372
0
          };
15373
15374
0
          i.types[this_operand]
15375
0
      = operand_type_xor (i.types[this_operand], disp16_32);
15376
0
        }
15377
148
    }
15378
188
      }
15379
14.7k
  }
15380
15.1k
#endif
15381
15.1k
    }
15382
15383
15.1k
  return addr_mode;
15384
15.1k
}
15385
15386
/* Make sure the memory operand we've been dealt is valid.
15387
   Return 1 on success, 0 on a failure.  */
15388
15389
static int
15390
i386_index_check (const char *operand_string)
15391
7.60k
{
15392
7.60k
  const char *kind = "base/index";
15393
7.60k
  enum flag_code addr_mode = i386_addressing_mode ();
15394
7.60k
  const insn_template *t = current_templates.end - 1;
15395
15396
7.60k
  if (t->opcode_modifier.isstring)
15397
29
    {
15398
29
      const i386_operand_type *t_types = get_operand_types (t);
15399
      /* Memory operands of string insns are special in that they only allow
15400
   a single register (rDI or rSI) as their memory address.  */
15401
29
      const reg_entry *expected_reg;
15402
29
      static const char di_si[][2][4] =
15403
29
  {
15404
29
    { "esi", "edi" },
15405
29
    { "si", "di" },
15406
29
    { "rsi", "rdi" }
15407
29
  };
15408
      /* For a few other insns with fixed register addressing we (ab)use the
15409
   IsString attribute as well.  */
15410
29
      static const char loregs[][4][4] =
15411
29
  {
15412
29
    { "eax", "ecx", "edx", "ebx" },
15413
29
    {  "ax",  "cx",  "dx",  "bx" },
15414
29
    { "rax", "rcx", "rdx", "rbx" }
15415
29
  };
15416
15417
29
      kind = "string address";
15418
15419
29
      if (t->opcode_modifier.prefixok == PrefixRep)
15420
29
  {
15421
29
    int es_op = t->opcode_modifier.isstring - IS_STRING_ES_OP0;
15422
29
    int op = 0;
15423
15424
29
    if (!t_types[0].bitfield.baseindex
15425
29
        || ((!i.mem_operands != !intel_syntax)
15426
21
      && t_types[1].bitfield.baseindex))
15427
5
      op = 1;
15428
29
    expected_reg = str_hash_find (reg_hash,
15429
29
          di_si[addr_mode][op == es_op]);
15430
29
  }
15431
0
      else
15432
0
  {
15433
0
    unsigned int op = t_types[0].bitfield.baseindex ? 0 : 1;
15434
15435
0
    if (!t_types[op].bitfield.instance)
15436
0
      return 1; /* Operand mismatch will be detected elsewhere.  */
15437
0
    expected_reg
15438
0
      = str_hash_find (reg_hash,
15439
0
           loregs[addr_mode][t_types[op]
15440
0
                 .bitfield.instance - 1]);
15441
0
  }
15442
15443
29
      if (i.base_reg != expected_reg
15444
0
    || i.index_reg
15445
0
    || operand_type_check (i.types[this_operand], disp))
15446
29
  {
15447
    /* The second memory operand must have the same size as
15448
       the first one.  */
15449
29
    if (i.mem_operands
15450
2
        && i.base_reg
15451
2
        && !((addr_mode == CODE_64BIT
15452
2
        && i.base_reg->reg_type.bitfield.qword)
15453
2
       || (addr_mode == CODE_32BIT
15454
2
           ? i.base_reg->reg_type.bitfield.dword
15455
2
           : i.base_reg->reg_type.bitfield.word)))
15456
2
      goto bad_address;
15457
15458
27
    as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
15459
27
       operand_string,
15460
27
       intel_syntax ? '[' : '(',
15461
27
       register_prefix,
15462
27
       expected_reg->reg_name,
15463
27
       intel_syntax ? ']' : ')');
15464
27
    return 1;
15465
29
  }
15466
0
      else
15467
0
  return 1;
15468
15469
25
    bad_address:
15470
25
      as_bad (_("`%s' is not a valid %s expression"),
15471
25
        operand_string, kind);
15472
25
      return 0;
15473
29
    }
15474
7.57k
  else
15475
7.57k
    {
15476
7.57k
      t = current_templates.start;
15477
15478
7.57k
      if (addr_mode != CODE_16BIT)
15479
7.35k
  {
15480
    /* 32-bit/64-bit checks.  */
15481
7.35k
    if (pp.disp_encoding == disp_encoding_16bit)
15482
0
      {
15483
0
      bad_disp:
15484
0
        as_bad (_("invalid `%s' prefix"),
15485
0
          addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
15486
0
        return 0;
15487
0
      }
15488
15489
7.35k
    if ((i.base_reg
15490
169
         && ((addr_mode == CODE_64BIT
15491
169
        ? !i.base_reg->reg_type.bitfield.qword
15492
169
        : !i.base_reg->reg_type.bitfield.dword)
15493
147
       || (i.index_reg && i.base_reg->reg_num == RegIP)
15494
147
       || i.base_reg->reg_num == RegIZ))
15495
7.33k
        || (i.index_reg
15496
0
      && !i.index_reg->reg_type.bitfield.xmmword
15497
0
      && !i.index_reg->reg_type.bitfield.ymmword
15498
0
      && !i.index_reg->reg_type.bitfield.zmmword
15499
0
      && ((addr_mode == CODE_64BIT
15500
0
           ? !i.index_reg->reg_type.bitfield.qword
15501
0
           : !i.index_reg->reg_type.bitfield.dword)
15502
0
          || !i.index_reg->reg_type.bitfield.baseindex)))
15503
22
      goto bad_address;
15504
15505
    /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
15506
7.33k
    if (t->mnem_off == MN_bndmk
15507
7.33k
        || t->mnem_off == MN_bndldx
15508
7.33k
        || t->mnem_off == MN_bndstx
15509
7.33k
        || t->opcode_modifier.sib == SIBMEM)
15510
0
      {
15511
        /* They cannot use RIP-relative addressing. */
15512
0
        if (i.base_reg && i.base_reg->reg_num == RegIP)
15513
0
    {
15514
0
      as_bad (_("`%s' cannot be used here"), operand_string);
15515
0
      return 0;
15516
0
    }
15517
15518
        /* bndldx and bndstx ignore their scale factor. */
15519
0
        if ((t->mnem_off == MN_bndldx || t->mnem_off == MN_bndstx)
15520
0
      && i.log2_scale_factor)
15521
0
    as_warn (_("register scaling is being ignored here"));
15522
0
      }
15523
7.33k
  }
15524
219
      else
15525
219
  {
15526
    /* 16-bit checks.  */
15527
219
    if (pp.disp_encoding == disp_encoding_32bit)
15528
0
      goto bad_disp;
15529
15530
219
    if ((i.base_reg
15531
1
         && (!i.base_reg->reg_type.bitfield.word
15532
1
       || !i.base_reg->reg_type.bitfield.baseindex))
15533
218
        || (i.index_reg
15534
0
      && (!i.index_reg->reg_type.bitfield.word
15535
0
          || !i.index_reg->reg_type.bitfield.baseindex
15536
0
          || !(i.base_reg
15537
0
         && i.base_reg->reg_num < 6
15538
0
         && i.index_reg->reg_num >= 6
15539
0
         && i.log2_scale_factor == 0))))
15540
1
      goto bad_address;
15541
219
  }
15542
7.57k
    }
15543
7.54k
  return 1;
15544
7.60k
}
15545
15546
/* Handle vector immediates.  */
15547
15548
static int
15549
RC_SAE_immediate (const char *imm_start)
15550
18.5k
{
15551
18.5k
  const char *pstr = imm_start;
15552
15553
18.5k
  if (*pstr != '{')
15554
18.5k
    return 0;
15555
15556
2
  pstr++;
15557
2
  if (is_whitespace (*pstr))
15558
0
    pstr++;
15559
15560
2
  pstr = RC_SAE_specifier (pstr);
15561
2
  if (pstr == NULL)
15562
1
    return 0;
15563
15564
1
  if (is_whitespace (*pstr))
15565
0
    pstr++;
15566
15567
1
  if (*pstr++ != '}')
15568
0
    {
15569
0
      as_bad (_("Missing '}': '%s'"), imm_start);
15570
0
      return 0;
15571
0
    }
15572
1
  /* RC/SAE immediate string should contain nothing more.  */;
15573
1
  if (*pstr != 0)
15574
1
    {
15575
1
      as_bad (_("Junk after '}': '%s'"), imm_start);
15576
1
      return 0;
15577
1
    }
15578
15579
  /* Internally this doesn't count as an operand.  */
15580
0
  --i.operands;
15581
15582
0
  return 1;
15583
1
}
15584
15585
static INLINE bool starts_memory_operand (char c)
15586
947
{
15587
947
  return ISDIGIT (c)
15588
456
   || is_name_beginner (c)
15589
189
   || (c && strchr ("([\"+-!~", c));
15590
947
}
15591
15592
/* Parse OPERAND_STRING into the i386_insn structure I.  Returns zero
15593
   on error.  */
15594
15595
static int
15596
i386_att_operand (char *operand_string)
15597
3.47k
{
15598
3.47k
  const reg_entry *r;
15599
3.47k
  char *end_op;
15600
3.47k
  char *op_string = operand_string;
15601
15602
3.47k
  if (is_whitespace (*op_string))
15603
0
    ++op_string;
15604
15605
  /* We check for an absolute prefix (differentiating,
15606
     for example, 'jmp pc_relative_label' from 'jmp *absolute_label'.  */
15607
3.47k
  if (*op_string == ABSOLUTE_PREFIX
15608
14
      && current_templates.start->opcode_modifier.jump)
15609
0
    {
15610
0
      ++op_string;
15611
0
      if (is_whitespace (*op_string))
15612
0
  ++op_string;
15613
0
      i.jumpabsolute = true;
15614
0
    }
15615
15616
  /* Check if operand is a register.  */
15617
3.47k
  if ((r = parse_register (op_string, &end_op)) != NULL)
15618
1.06k
    {
15619
1.06k
      i386_operand_type temp;
15620
15621
1.06k
      if (r == &bad_reg)
15622
17
  return 0;
15623
15624
      /* Check for a segment override by searching for ':' after a
15625
   segment register.  */
15626
1.04k
      op_string = end_op;
15627
1.04k
      if (is_whitespace (*op_string))
15628
3
  ++op_string;
15629
1.04k
      if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
15630
7
  {
15631
7
    i.seg[i.mem_operands] = r;
15632
15633
    /* Skip the ':' and whitespace.  */
15634
7
    ++op_string;
15635
7
    if (is_whitespace (*op_string))
15636
0
      ++op_string;
15637
15638
    /* Handle case of %es:*foo.  */
15639
7
    if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX
15640
5
        && current_templates.start->opcode_modifier.jump)
15641
0
      {
15642
0
        ++op_string;
15643
0
        if (is_whitespace (*op_string))
15644
0
    ++op_string;
15645
0
        i.jumpabsolute = true;
15646
0
      }
15647
15648
7
    if (!starts_memory_operand (*op_string))
15649
5
      {
15650
5
        as_bad (_("bad memory operand `%s'"), op_string);
15651
5
        return 0;
15652
5
      }
15653
2
    goto do_memory_reference;
15654
7
  }
15655
15656
      /* Handle vector operations.  */
15657
1.03k
      if (*op_string == '{')
15658
0
  {
15659
0
    op_string = check_VecOperations (op_string);
15660
0
    if (op_string == NULL)
15661
0
      return 0;
15662
0
  }
15663
15664
1.03k
      if (*op_string)
15665
129
  {
15666
129
    as_bad (_("junk `%s' after register"), op_string);
15667
129
    return 0;
15668
129
  }
15669
15670
       /* Reject pseudo registers for .insn.  */
15671
907
      if (dot_insn () && r->reg_type.bitfield.class == ClassNone)
15672
0
  {
15673
0
    as_bad (_("`%s%s' cannot be used here"),
15674
0
      register_prefix, r->reg_name);
15675
0
    return 0;
15676
0
  }
15677
15678
907
      temp = r->reg_type;
15679
907
      temp.bitfield.baseindex = 0;
15680
907
      i.types[this_operand] = operand_type_or (i.types[this_operand],
15681
907
                 temp);
15682
907
      i.types[this_operand].bitfield.unspecified = 0;
15683
907
      i.op[this_operand].regs = r;
15684
907
      i.reg_operands++;
15685
15686
      /* A GPR may follow an RC or SAE immediate only if a (vector) register
15687
         operand was also present earlier on.  */
15688
907
      if (i.rounding.type != rc_none && temp.bitfield.class == Reg
15689
0
          && i.reg_operands == 1)
15690
0
  {
15691
0
    unsigned int j;
15692
15693
0
    for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
15694
0
      if (i.rounding.type == RC_NamesTable[j].type)
15695
0
        break;
15696
0
    as_bad (_("`%s': misplaced `{%s}'"),
15697
0
      insn_name (current_templates.start), RC_NamesTable[j].name);
15698
0
    return 0;
15699
0
  }
15700
907
    }
15701
2.41k
  else if (*op_string == REGISTER_PREFIX)
15702
17
    {
15703
17
      as_bad (_("bad register name `%s'"), op_string);
15704
17
      return 0;
15705
17
    }
15706
2.39k
  else if (*op_string == IMMEDIATE_PREFIX)
15707
1.45k
    {
15708
1.45k
      ++op_string;
15709
1.45k
      if (i.jumpabsolute)
15710
0
  {
15711
0
    as_bad (_("immediate operand illegal with absolute jump"));
15712
0
    return 0;
15713
0
  }
15714
1.45k
      if (!i386_immediate (op_string))
15715
7
  return 0;
15716
1.45k
      if (i.rounding.type != rc_none)
15717
0
  {
15718
0
    as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
15719
0
      insn_name (current_templates.start));
15720
0
    return 0;
15721
0
  }
15722
1.45k
    }
15723
940
  else if (RC_SAE_immediate (operand_string))
15724
0
    {
15725
      /* If it is a RC or SAE immediate, do the necessary placement check:
15726
   Only another immediate or a GPR may precede it.  */
15727
0
      if (i.mem_operands || i.reg_operands + i.imm_operands > 1
15728
0
    || (i.reg_operands == 1
15729
0
        && i.types[0].bitfield.class != Reg))
15730
0
  {
15731
0
    as_bad (_("`%s': misplaced `%s'"),
15732
0
      insn_name (current_templates.start), operand_string);
15733
0
    return 0;
15734
0
  }
15735
0
    }
15736
940
  else if (starts_memory_operand (*op_string))
15737
925
    {
15738
      /* This is a memory reference of some sort.  */
15739
925
      char *base_string;
15740
15741
      /* Start and end of displacement string expression (if found).  */
15742
925
      char *displacement_string_start;
15743
925
      char *displacement_string_end;
15744
15745
927
    do_memory_reference:
15746
      /* Check for base index form.  We detect the base index form by
15747
   looking for an ')' at the end of the operand, searching
15748
   for the '(' matching it, and finding a REGISTER_PREFIX or ','
15749
   after the '('.  */
15750
927
      base_string = op_string + strlen (op_string);
15751
15752
      /* Handle vector operations.  */
15753
927
      --base_string;
15754
927
      if (is_whitespace (*base_string))
15755
0
  --base_string;
15756
15757
927
      if (*base_string == '}')
15758
23
  {
15759
23
    char *vop_start = NULL;
15760
15761
75
    while (base_string-- > op_string)
15762
73
      {
15763
73
        if (*base_string == '"')
15764
0
    break;
15765
73
        if (*base_string != '{')
15766
47
    continue;
15767
15768
26
        vop_start = base_string;
15769
15770
26
        --base_string;
15771
26
        if (is_whitespace (*base_string))
15772
0
    --base_string;
15773
15774
26
        if (*base_string != '}')
15775
21
    break;
15776
15777
5
        vop_start = NULL;
15778
5
      }
15779
15780
23
    if (!vop_start)
15781
2
      {
15782
2
        as_bad (_("unbalanced figure braces"));
15783
2
        return 0;
15784
2
      }
15785
15786
21
    if (check_VecOperations (vop_start) == NULL)
15787
21
      return 0;
15788
21
  }
15789
15790
      /* If we only have a displacement, set-up for it to be parsed later.  */
15791
904
      displacement_string_start = op_string;
15792
904
      displacement_string_end = base_string + 1;
15793
15794
904
      if (*base_string == ')')
15795
274
  {
15796
274
    char *temp_string;
15797
274
    unsigned int parens_not_balanced = 0;
15798
274
    bool in_quotes = false;
15799
15800
    /* We've already checked that the number of left & right ()'s are
15801
       equal, and that there's a matching set of double quotes.  */
15802
274
    end_op = base_string;
15803
2.71k
    for (temp_string = op_string; temp_string < end_op; temp_string++)
15804
2.43k
      {
15805
2.43k
        if (*temp_string == '\\' && temp_string[1] == '"')
15806
0
    ++temp_string;
15807
2.43k
        else if (*temp_string == '"')
15808
0
    in_quotes = !in_quotes;
15809
2.43k
        else if (!in_quotes)
15810
2.43k
    {
15811
2.43k
      if (*temp_string == '(' && !parens_not_balanced++)
15812
274
        base_string = temp_string;
15813
2.43k
      if (*temp_string == ')')
15814
0
        --parens_not_balanced;
15815
2.43k
    }
15816
2.43k
      }
15817
15818
274
    temp_string = base_string;
15819
15820
    /* Skip past '(' and whitespace.  */
15821
274
    gas_assert (*base_string == '(');
15822
274
    ++base_string;
15823
274
    if (is_whitespace (*base_string))
15824
0
      ++base_string;
15825
15826
274
    if (*base_string == ','
15827
274
        || ((i.base_reg = parse_register (base_string, &end_op))
15828
274
      != NULL))
15829
274
      {
15830
274
        displacement_string_end = temp_string;
15831
15832
274
        i.types[this_operand].bitfield.baseindex = 1;
15833
15834
274
        if (i.base_reg)
15835
274
    {
15836
274
      if (i.base_reg == &bad_reg)
15837
0
        return 0;
15838
274
      base_string = end_op;
15839
274
      if (is_whitespace (*base_string))
15840
125
        ++base_string;
15841
274
    }
15842
15843
        /* There may be an index reg or scale factor here.  */
15844
274
        if (*base_string == ',')
15845
0
    {
15846
0
      ++base_string;
15847
0
      if (is_whitespace (*base_string))
15848
0
        ++base_string;
15849
15850
0
      if ((i.index_reg = parse_register (base_string, &end_op))
15851
0
          != NULL)
15852
0
        {
15853
0
          if (i.index_reg == &bad_reg)
15854
0
      return 0;
15855
0
          base_string = end_op;
15856
0
          if (is_whitespace (*base_string))
15857
0
      ++base_string;
15858
0
          if (*base_string == ',')
15859
0
      {
15860
0
        ++base_string;
15861
0
        if (is_whitespace (*base_string))
15862
0
          ++base_string;
15863
0
      }
15864
0
          else if (*base_string != ')')
15865
0
      {
15866
0
        as_bad (_("expecting `,' or `)' "
15867
0
            "after index register in `%s'"),
15868
0
          operand_string);
15869
0
        return 0;
15870
0
      }
15871
0
        }
15872
0
      else if (*base_string == REGISTER_PREFIX)
15873
0
        {
15874
0
          end_op = strchr (base_string, ',');
15875
0
          if (end_op)
15876
0
      *end_op = '\0';
15877
0
          as_bad (_("bad register name `%s'"), base_string);
15878
0
          return 0;
15879
0
        }
15880
15881
      /* Check for scale factor.  */
15882
0
      if (*base_string != ')')
15883
0
        {
15884
0
          char *end_scale = i386_scale (base_string);
15885
15886
0
          if (!end_scale)
15887
0
      return 0;
15888
15889
0
          base_string = end_scale;
15890
0
          if (is_whitespace (*base_string))
15891
0
      ++base_string;
15892
0
          if (*base_string != ')')
15893
0
      {
15894
0
        as_bad (_("expecting `)' "
15895
0
            "after scale factor in `%s'"),
15896
0
          operand_string);
15897
0
        return 0;
15898
0
      }
15899
0
        }
15900
0
      else if (!i.index_reg)
15901
0
        {
15902
0
          as_bad (_("expecting index register or scale factor "
15903
0
        "after `,'; got '%c'"),
15904
0
            *base_string);
15905
0
          return 0;
15906
0
        }
15907
0
    }
15908
274
        else if (*base_string != ')')
15909
125
    {
15910
125
      as_bad (_("expecting `,' or `)' "
15911
125
          "after base register in `%s'"),
15912
125
        operand_string);
15913
125
      return 0;
15914
125
    }
15915
274
      }
15916
0
    else if (*base_string == REGISTER_PREFIX)
15917
0
      {
15918
0
        end_op = strchr (base_string, ',');
15919
0
        if (end_op)
15920
0
    *end_op = '\0';
15921
0
        as_bad (_("bad register name `%s'"), base_string);
15922
0
        return 0;
15923
0
      }
15924
274
  }
15925
15926
      /* If there's an expression beginning the operand, parse it,
15927
   assuming displacement_string_start and
15928
   displacement_string_end are meaningful.  */
15929
779
      if (displacement_string_start != displacement_string_end)
15930
637
  {
15931
637
    if (!i386_displacement (displacement_string_start,
15932
637
          displacement_string_end))
15933
5
      return 0;
15934
637
  }
15935
15936
      /* Special case for (%dx) while doing input/output op.  */
15937
774
      if (i.base_reg
15938
149
    && i.base_reg->reg_type.bitfield.instance == RegD
15939
0
    && i.base_reg->reg_type.bitfield.word
15940
0
    && i.index_reg == 0
15941
0
    && i.log2_scale_factor == 0
15942
0
    && i.seg[i.mem_operands] == 0
15943
0
    && !operand_type_check (i.types[this_operand], disp))
15944
0
  {
15945
0
    i.types[this_operand] = i.base_reg->reg_type;
15946
0
    i.op[this_operand].regs = i.base_reg;
15947
0
    i.base_reg = NULL;
15948
0
    i.input_output_operand = true;
15949
0
    return 1;
15950
0
  }
15951
15952
774
      if (i386_index_check (operand_string) == 0)
15953
2
  return 0;
15954
772
      i.flags[this_operand] |= Operand_Mem;
15955
772
      i.mem_operands++;
15956
772
    }
15957
15
  else
15958
15
    {
15959
      /* It's not a memory operand; argh!  */
15960
15
      as_bad (_("invalid char %s beginning operand %d `%s'"),
15961
15
        output_invalid (*op_string),
15962
15
        this_operand + 1,
15963
15
        op_string);
15964
15
      return 0;
15965
15
    }
15966
3.12k
  return 1;     /* Normal return.  */
15967
3.47k
}
15968

15969
/* Initialize the tc_frag_data field of a fragment.  */
15970
15971
void i386_frag_init (fragS *fragP, size_t max_bytes)
15972
1.63k
{
15973
1.63k
  memset (&fragP->tc_frag_data, 0, sizeof (fragP->tc_frag_data));
15974
1.63k
  fragP->tc_frag_data.isa = cpu_arch_isa;
15975
1.63k
  fragP->tc_frag_data.tune = cpu_arch_tune;
15976
1.63k
  fragP->tc_frag_data.cpunop = cpu_arch_flags.bitfield.cpunop;
15977
1.63k
  fragP->tc_frag_data.isanop = cpu_arch_isa_flags.bitfield.cpunop;
15978
1.63k
  fragP->tc_frag_data.code = i386_flag_code;
15979
1.63k
  fragP->tc_frag_data.max_bytes = max_bytes;
15980
1.63k
  fragP->tc_frag_data.last_insn_normal
15981
1.63k
    = (seg_info(now_seg)->tc_segment_info_data.last_insn.kind
15982
1.63k
       == last_insn_other);
15983
1.63k
  fragP->tc_frag_data.no_cond_jump_promotion = no_cond_jump_promotion;
15984
1.63k
}
15985
15986
/* Calculate the maximum variable size (i.e., excluding fr_fix)
15987
   that an rs_machine_dependent frag may reach.  */
15988
15989
unsigned int
15990
i386_frag_max_var (fragS *frag)
15991
0
{
15992
  /* The only relaxable frags are for jumps.
15993
     Unconditional jumps can grow by 4 bytes and others by 5 bytes.  */
15994
0
  gas_assert (frag->fr_type == rs_machine_dependent);
15995
0
  return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
15996
0
}
15997
15998
#ifdef OBJ_ELF
15999
static int
16000
elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
16001
0
{
16002
  /* STT_GNU_IFUNC symbol must go through PLT.  */
16003
0
  if ((symbol_get_bfdsym (fr_symbol)->flags
16004
0
       & BSF_GNU_INDIRECT_FUNCTION) != 0)
16005
0
    return 0;
16006
16007
0
  if (!S_IS_EXTERNAL (fr_symbol))
16008
    /* Symbol may be weak or local.  */
16009
0
    return !S_IS_WEAK (fr_symbol);
16010
16011
  /* Global symbols with non-default visibility can't be preempted. */
16012
0
  if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
16013
0
    return 1;
16014
16015
0
  if (fr_var != NO_RELOC)
16016
0
    switch ((enum bfd_reloc_code_real) fr_var)
16017
0
      {
16018
0
      case BFD_RELOC_386_PLT32:
16019
0
      case BFD_RELOC_32_PLT_PCREL:
16020
  /* Symbol with PLT relocation may be preempted. */
16021
0
  return 0;
16022
0
      default:
16023
0
  abort ();
16024
0
      }
16025
16026
  /* Global symbols with default visibility in a shared library may be
16027
     preempted by another definition.  */
16028
0
  return !shared;
16029
0
}
16030
#endif
16031
16032
/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
16033
   Note also work for Skylake and Cascadelake.
16034
---------------------------------------------------------------------
16035
|   JCC   | ADD/SUB/CMP | INC/DEC | TEST/AND |
16036
| ------  | ----------- | ------- | -------- |
16037
|   Jo    |      N      |    N    |     Y    |
16038
|   Jno   |      N      |    N    |     Y    |
16039
|  Jc/Jb  |      Y      |    N    |     Y    |
16040
| Jae/Jnb |      Y      |    N    |     Y    |
16041
|  Je/Jz  |      Y      |    Y    |     Y    |
16042
| Jne/Jnz |      Y      |    Y    |     Y    |
16043
| Jna/Jbe |      Y      |    N    |     Y    |
16044
| Ja/Jnbe |      Y      |    N    |     Y    |
16045
|   Js    |      N      |    N    |     Y    |
16046
|   Jns   |      N      |    N    |     Y    |
16047
|  Jp/Jpe |      N      |    N    |     Y    |
16048
| Jnp/Jpo |      N      |    N    |     Y    |
16049
| Jl/Jnge |      Y      |    Y    |     Y    |
16050
| Jge/Jnl |      Y      |    Y    |     Y    |
16051
| Jle/Jng |      Y      |    Y    |     Y    |
16052
| Jg/Jnle |      Y      |    Y    |     Y    |
16053
---------------------------------------------------------------------  */
16054
static int
16055
i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
16056
0
{
16057
0
  if (mf_cmp == mf_cmp_alu_cmp)
16058
0
    return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
16059
0
      || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
16060
0
  if (mf_cmp == mf_cmp_incdec)
16061
0
    return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
16062
0
      || mf_jcc == mf_jcc_jle);
16063
0
  if (mf_cmp == mf_cmp_test_and)
16064
0
    return 1;
16065
0
  return 0;
16066
0
}
16067
16068
/* Return the next non-empty frag.  */
16069
16070
static fragS *
16071
i386_next_non_empty_frag (fragS *fragP)
16072
0
{
16073
  /* There may be a frag with a ".fill 0" when there is no room in
16074
     the current frag for frag_grow in output_insn.  */
16075
0
  for (fragP = fragP->fr_next;
16076
0
       (fragP != NULL
16077
0
  && fragP->fr_type == rs_fill
16078
0
  && fragP->fr_fix == 0);
16079
0
       fragP = fragP->fr_next)
16080
0
    ;
16081
0
  return fragP;
16082
0
}
16083
16084
/* Return the next jcc frag after BRANCH_PADDING.  */
16085
16086
static fragS *
16087
i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
16088
0
{
16089
0
  fragS *branch_fragP;
16090
0
  if (!pad_fragP)
16091
0
    return NULL;
16092
16093
0
  if (pad_fragP->fr_type == rs_machine_dependent
16094
0
      && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
16095
0
    == BRANCH_PADDING))
16096
0
    {
16097
0
      branch_fragP = i386_next_non_empty_frag (pad_fragP);
16098
0
      if (branch_fragP->fr_type != rs_machine_dependent)
16099
0
  return NULL;
16100
0
      if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
16101
0
    && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
16102
0
           pad_fragP->tc_frag_data.mf_type))
16103
0
  return branch_fragP;
16104
0
    }
16105
16106
0
  return NULL;
16107
0
}
16108
16109
/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags.  */
16110
16111
static void
16112
i386_classify_machine_dependent_frag (fragS *fragP)
16113
0
{
16114
0
  fragS *cmp_fragP;
16115
0
  fragS *pad_fragP;
16116
0
  fragS *branch_fragP;
16117
0
  fragS *next_fragP;
16118
0
  unsigned int max_prefix_length;
16119
16120
0
  if (fragP->tc_frag_data.classified)
16121
0
    return;
16122
16123
  /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING.  Convert
16124
     FUSED_JCC_PADDING and merge BRANCH_PADDING.  */
16125
0
  for (next_fragP = fragP;
16126
0
       next_fragP != NULL;
16127
0
       next_fragP = next_fragP->fr_next)
16128
0
    {
16129
0
      next_fragP->tc_frag_data.classified = 1;
16130
0
      if (next_fragP->fr_type == rs_machine_dependent)
16131
0
  switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
16132
0
    {
16133
0
    case BRANCH_PADDING:
16134
      /* The BRANCH_PADDING frag must be followed by a branch
16135
         frag.  */
16136
0
      branch_fragP = i386_next_non_empty_frag (next_fragP);
16137
0
      next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
16138
0
      break;
16139
0
    case FUSED_JCC_PADDING:
16140
      /* Check if this is a fused jcc:
16141
         FUSED_JCC_PADDING
16142
         CMP like instruction
16143
         BRANCH_PADDING
16144
         COND_JUMP
16145
         */
16146
0
      cmp_fragP = i386_next_non_empty_frag (next_fragP);
16147
0
      pad_fragP = i386_next_non_empty_frag (cmp_fragP);
16148
0
      branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
16149
0
      if (branch_fragP)
16150
0
        {
16151
    /* The BRANCH_PADDING frag is merged with the
16152
       FUSED_JCC_PADDING frag.  */
16153
0
    next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
16154
    /* CMP like instruction size.  */
16155
0
    next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
16156
0
    frag_wane (pad_fragP);
16157
    /* Skip to branch_fragP.  */
16158
0
    next_fragP = branch_fragP;
16159
0
        }
16160
0
      else if (next_fragP->tc_frag_data.max_prefix_length)
16161
0
        {
16162
    /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
16163
       a fused jcc.  */
16164
0
    next_fragP->fr_subtype
16165
0
      = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
16166
0
    next_fragP->tc_frag_data.max_bytes
16167
0
      = next_fragP->tc_frag_data.max_prefix_length;
16168
    /* This will be updated in the BRANCH_PREFIX scan.  */
16169
0
    next_fragP->tc_frag_data.max_prefix_length = 0;
16170
0
        }
16171
0
      else
16172
0
        frag_wane (next_fragP);
16173
0
      break;
16174
0
    }
16175
0
    }
16176
16177
  /* Stop if there is no BRANCH_PREFIX.  */
16178
0
  if (!align_branch_prefix_size)
16179
0
    return;
16180
16181
  /* Scan for BRANCH_PREFIX.  */
16182
0
  for (; fragP != NULL; fragP = fragP->fr_next)
16183
0
    {
16184
0
      if (fragP->fr_type != rs_machine_dependent
16185
0
    || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
16186
0
        != BRANCH_PREFIX))
16187
0
  continue;
16188
16189
      /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
16190
   COND_JUMP_PREFIX.  */
16191
0
      max_prefix_length = 0;
16192
0
      for (next_fragP = fragP;
16193
0
     next_fragP != NULL;
16194
0
     next_fragP = next_fragP->fr_next)
16195
0
  {
16196
0
    if (next_fragP->fr_type == rs_fill)
16197
      /* Skip rs_fill frags.  */
16198
0
      continue;
16199
0
    else if (next_fragP->fr_type != rs_machine_dependent)
16200
      /* Stop for all other frags.  */
16201
0
      break;
16202
16203
    /* rs_machine_dependent frags.  */
16204
0
    if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
16205
0
        == BRANCH_PREFIX)
16206
0
      {
16207
        /* Count BRANCH_PREFIX frags.  */
16208
0
        if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
16209
0
    {
16210
0
      max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
16211
0
      frag_wane (next_fragP);
16212
0
    }
16213
0
        else
16214
0
    max_prefix_length
16215
0
      += next_fragP->tc_frag_data.max_bytes;
16216
0
      }
16217
0
    else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
16218
0
        == BRANCH_PADDING)
16219
0
       || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
16220
0
           == FUSED_JCC_PADDING))
16221
0
      {
16222
        /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING.  */
16223
0
        fragP->tc_frag_data.u.padding_fragP = next_fragP;
16224
0
        break;
16225
0
      }
16226
0
    else
16227
      /* Stop for other rs_machine_dependent frags.  */
16228
0
      break;
16229
0
  }
16230
16231
0
      fragP->tc_frag_data.max_prefix_length = max_prefix_length;
16232
16233
      /* Skip to the next frag.  */
16234
0
      fragP = next_fragP;
16235
0
    }
16236
0
}
16237
16238
/* Compute padding size for
16239
16240
  FUSED_JCC_PADDING
16241
  CMP like instruction
16242
  BRANCH_PADDING
16243
  COND_JUMP/UNCOND_JUMP
16244
16245
   or
16246
16247
  BRANCH_PADDING
16248
  COND_JUMP/UNCOND_JUMP
16249
 */
16250
16251
static int
16252
i386_branch_padding_size (fragS *fragP, offsetT address)
16253
0
{
16254
0
  unsigned int offset, size, padding_size;
16255
0
  fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
16256
16257
  /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag.  */
16258
0
  if (!address)
16259
0
    address = fragP->fr_address;
16260
0
  address += fragP->fr_fix;
16261
16262
  /* CMP like instrunction size.  */
16263
0
  size = fragP->tc_frag_data.cmp_size;
16264
16265
  /* The base size of the branch frag.  */
16266
0
  size += branch_fragP->fr_fix;
16267
16268
  /* Add opcode and displacement bytes for the rs_machine_dependent
16269
     branch frag.  */
16270
0
  if (branch_fragP->fr_type == rs_machine_dependent)
16271
0
    size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
16272
16273
  /* Check if branch is within boundary and doesn't end at the last
16274
     byte.  */
16275
0
  offset = address & ((1U << align_branch_power) - 1);
16276
0
  if ((offset + size) >= (1U << align_branch_power))
16277
    /* Padding needed to avoid crossing boundary.  */
16278
0
    padding_size = (1U << align_branch_power) - offset;
16279
0
  else
16280
    /* No padding needed.  */
16281
0
    padding_size = 0;
16282
16283
  /* The return value may be saved in tc_frag_data.length which is
16284
     unsigned byte.  */
16285
0
  if (!fits_in_unsigned_byte (padding_size))
16286
0
    abort ();
16287
16288
0
  return padding_size;
16289
0
}
16290
16291
/* i386_generic_table_relax_frag()
16292
16293
   Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
16294
   grow/shrink padding to align branch frags.  Hand others to
16295
   relax_frag().  */
16296
16297
long
16298
i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
16299
0
{
16300
0
  if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
16301
0
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
16302
0
    {
16303
0
      long padding_size = i386_branch_padding_size (fragP, 0);
16304
0
      long grow = padding_size - fragP->tc_frag_data.length;
16305
16306
      /* When the BRANCH_PREFIX frag is used, the computed address
16307
         must match the actual address and there should be no padding.  */
16308
0
      if (fragP->tc_frag_data.padding_address
16309
0
    && (fragP->tc_frag_data.padding_address != fragP->fr_address
16310
0
        || padding_size))
16311
0
  abort ();
16312
16313
      /* Update the padding size.  */
16314
0
      if (grow)
16315
0
  fragP->tc_frag_data.length = padding_size;
16316
16317
0
      return grow;
16318
0
    }
16319
0
  else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
16320
0
    {
16321
0
      fragS *padding_fragP, *next_fragP;
16322
0
      long padding_size, left_size, last_size;
16323
16324
0
      padding_fragP = fragP->tc_frag_data.u.padding_fragP;
16325
0
      if (!padding_fragP)
16326
  /* Use the padding set by the leading BRANCH_PREFIX frag.  */
16327
0
  return (fragP->tc_frag_data.length
16328
0
    - fragP->tc_frag_data.last_length);
16329
16330
      /* Compute the relative address of the padding frag in the very
16331
        first time where the BRANCH_PREFIX frag sizes are zero.  */
16332
0
      if (!fragP->tc_frag_data.padding_address)
16333
0
  fragP->tc_frag_data.padding_address
16334
0
    = padding_fragP->fr_address - (fragP->fr_address - stretch);
16335
16336
      /* First update the last length from the previous interation.  */
16337
0
      left_size = fragP->tc_frag_data.prefix_length;
16338
0
      for (next_fragP = fragP;
16339
0
     next_fragP != padding_fragP;
16340
0
     next_fragP = next_fragP->fr_next)
16341
0
  if (next_fragP->fr_type == rs_machine_dependent
16342
0
      && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
16343
0
    == BRANCH_PREFIX))
16344
0
    {
16345
0
      if (left_size)
16346
0
        {
16347
0
    int max = next_fragP->tc_frag_data.max_bytes;
16348
0
    if (max)
16349
0
      {
16350
0
        int size;
16351
0
        if (max > left_size)
16352
0
          size = left_size;
16353
0
        else
16354
0
          size = max;
16355
0
        left_size -= size;
16356
0
        next_fragP->tc_frag_data.last_length = size;
16357
0
      }
16358
0
        }
16359
0
      else
16360
0
        next_fragP->tc_frag_data.last_length = 0;
16361
0
    }
16362
16363
      /* Check the padding size for the padding frag.  */
16364
0
      padding_size = i386_branch_padding_size
16365
0
  (padding_fragP, (fragP->fr_address
16366
0
       + fragP->tc_frag_data.padding_address));
16367
16368
0
      last_size = fragP->tc_frag_data.prefix_length;
16369
      /* Check if there is change from the last interation.  */
16370
0
      if (padding_size == last_size)
16371
0
  {
16372
    /* Update the expected address of the padding frag.  */
16373
0
    padding_fragP->tc_frag_data.padding_address
16374
0
      = (fragP->fr_address + padding_size
16375
0
         + fragP->tc_frag_data.padding_address);
16376
0
    return 0;
16377
0
  }
16378
16379
0
      if (padding_size > fragP->tc_frag_data.max_prefix_length)
16380
0
  {
16381
    /* No padding if there is no sufficient room.  Clear the
16382
       expected address of the padding frag.  */
16383
0
    padding_fragP->tc_frag_data.padding_address = 0;
16384
0
    padding_size = 0;
16385
0
  }
16386
0
      else
16387
  /* Store the expected address of the padding frag.  */
16388
0
  padding_fragP->tc_frag_data.padding_address
16389
0
    = (fragP->fr_address + padding_size
16390
0
       + fragP->tc_frag_data.padding_address);
16391
16392
0
      fragP->tc_frag_data.prefix_length = padding_size;
16393
16394
      /* Update the length for the current interation.  */
16395
0
      left_size = padding_size;
16396
0
      for (next_fragP = fragP;
16397
0
     next_fragP != padding_fragP;
16398
0
     next_fragP = next_fragP->fr_next)
16399
0
  if (next_fragP->fr_type == rs_machine_dependent
16400
0
      && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
16401
0
    == BRANCH_PREFIX))
16402
0
    {
16403
0
      if (left_size)
16404
0
        {
16405
0
    int max = next_fragP->tc_frag_data.max_bytes;
16406
0
    if (max)
16407
0
      {
16408
0
        int size;
16409
0
        if (max > left_size)
16410
0
          size = left_size;
16411
0
        else
16412
0
          size = max;
16413
0
        left_size -= size;
16414
0
        next_fragP->tc_frag_data.length = size;
16415
0
      }
16416
0
        }
16417
0
      else
16418
0
        next_fragP->tc_frag_data.length = 0;
16419
0
    }
16420
16421
0
      return (fragP->tc_frag_data.length
16422
0
        - fragP->tc_frag_data.last_length);
16423
0
    }
16424
0
  return relax_frag (segment, fragP, stretch);
16425
0
}
16426
16427
/* md_estimate_size_before_relax()
16428
16429
   Called just before relax() for rs_machine_dependent frags.  The x86
16430
   assembler uses these frags to handle variable size jump
16431
   instructions.
16432
16433
   Any symbol that is now undefined will not become defined.
16434
   Return the correct fr_subtype in the frag.
16435
   Return the initial "guess for variable size of frag" to caller.
16436
   The guess is actually the growth beyond the fixed part.  Whatever
16437
   we do to grow the fixed or variable part contributes to our
16438
   returned value.  */
16439
16440
int
16441
md_estimate_size_before_relax (fragS *fragP, segT segment)
16442
0
{
16443
0
  if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
16444
0
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
16445
0
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
16446
0
    {
16447
0
      i386_classify_machine_dependent_frag (fragP);
16448
0
      return fragP->tc_frag_data.length;
16449
0
    }
16450
16451
  /* We've already got fragP->fr_subtype right;  all we have to do is
16452
     check for un-relaxable symbols.  On an ELF system, we can't relax
16453
     an externally visible symbol, because it may be overridden by a
16454
     shared library.  */
16455
0
  if (S_GET_SEGMENT (fragP->fr_symbol) != segment
16456
0
#ifdef OBJ_ELF
16457
0
      || !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
16458
0
              fragP->fr_var)
16459
0
#endif
16460
#if defined (OBJ_COFF) && defined (TE_PE)
16461
      || S_IS_WEAK (fragP->fr_symbol)
16462
#endif
16463
0
      )
16464
0
    {
16465
      /* Symbol is undefined in this segment, or we need to keep a
16466
   reloc so that weak symbols can be overridden.  */
16467
0
      int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
16468
0
      enum bfd_reloc_code_real reloc_type;
16469
0
      unsigned char *opcode;
16470
0
      int old_fr_fix;
16471
0
      fixS *fixP = NULL;
16472
16473
0
      reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
16474
0
#ifdef OBJ_ELF
16475
0
      if (reloc_type == NO_RELOC
16476
0
    && size != 2
16477
0
    && fragP->tc_frag_data.code == CODE_64BIT
16478
0
    && fragP->fr_offset == 0
16479
0
    && need_plt32_p (fragP->fr_symbol))
16480
0
  reloc_type = BFD_RELOC_32_PLT_PCREL;
16481
0
#endif
16482
16483
0
      old_fr_fix = fragP->fr_fix;
16484
0
      opcode = (unsigned char *) fragP->fr_opcode;
16485
16486
0
      switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
16487
0
  {
16488
0
  case UNCOND_JUMP:
16489
    /* Make jmp (0xeb) a (d)word displacement jump.  */
16490
0
    opcode[0] = 0xe9;
16491
0
    fragP->fr_fix += size;
16492
0
    fixP = fix_new (fragP, old_fr_fix, size,
16493
0
        fragP->fr_symbol,
16494
0
        fragP->fr_offset, 1,
16495
0
        _reloc (size, 1, 1, reloc_type,
16496
0
          fragP->tc_frag_data.code == CODE_64BIT,
16497
0
          fragP->fr_file, fragP->fr_line));
16498
0
    break;
16499
16500
0
  case COND_JUMP86:
16501
0
    if (fragP->tc_frag_data.no_cond_jump_promotion
16502
0
        && fragP->fr_var == NO_RELOC)
16503
0
      {
16504
0
        fragP->fr_fix += 1;
16505
0
        fixP = fix_new (fragP, old_fr_fix, 1,
16506
0
            fragP->fr_symbol,
16507
0
            fragP->fr_offset, 1,
16508
0
            BFD_RELOC_8_PCREL);
16509
0
        fixP->fx_signed = 1;
16510
0
        break;
16511
0
      }
16512
16513
0
    if (size == 2)
16514
0
      {
16515
        /* Negate the condition, and branch past an
16516
     unconditional jump.  */
16517
0
        opcode[0] ^= 1;
16518
0
        opcode[1] = 3;
16519
        /* Insert an unconditional jump.  */
16520
0
        opcode[2] = 0xe9;
16521
        /* We added two extra opcode bytes, and have a two byte
16522
     offset.  */
16523
0
        fragP->fr_fix += 2 + 2;
16524
0
        fix_new (fragP, old_fr_fix + 2, 2,
16525
0
           fragP->fr_symbol,
16526
0
           fragP->fr_offset, 1,
16527
0
           _reloc (size, 1, 1, reloc_type,
16528
0
             fragP->tc_frag_data.code == CODE_64BIT,
16529
0
             fragP->fr_file, fragP->fr_line));
16530
0
        break;
16531
0
      }
16532
    /* Fall through.  */
16533
16534
0
  case COND_JUMP:
16535
    /* This changes the byte-displacement jump 0x7N
16536
       to the (d)word-displacement jump 0x0f,0x8N.  */
16537
0
    opcode[1] = opcode[0] + 0x10;
16538
0
    opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
16539
    /* We've added an opcode byte.  */
16540
0
    fragP->fr_fix += 1 + size;
16541
0
    fixP = fix_new (fragP, old_fr_fix + 1, size,
16542
0
        fragP->fr_symbol,
16543
0
        fragP->fr_offset, 1,
16544
0
        _reloc (size, 1, 1, reloc_type,
16545
0
          fragP->tc_frag_data.code == CODE_64BIT,
16546
0
          fragP->fr_file, fragP->fr_line));
16547
0
    break;
16548
16549
0
  default:
16550
0
    BAD_CASE (fragP->fr_subtype);
16551
0
    break;
16552
0
  }
16553
16554
      /* All jumps handled here are signed, but don't unconditionally use a
16555
   signed limit check for 32 and 16 bit jumps as we want to allow wrap
16556
   around at 4G (outside of 64-bit mode) and 64k.  */
16557
0
      if (size == 4 && flag_code == CODE_64BIT)
16558
0
  fixP->fx_signed = 1;
16559
16560
0
      frag_wane (fragP);
16561
0
      return fragP->fr_fix - old_fr_fix;
16562
0
    }
16563
16564
  /* Guess size depending on current relax state.  Initially the relax
16565
     state will correspond to a short jump and we return 1, because
16566
     the variable part of the frag (the branch offset) is one byte
16567
     long.  However, we can relax a section more than once and in that
16568
     case we must either set fr_subtype back to the unrelaxed state,
16569
     or return the value for the appropriate branch.  */
16570
0
  return md_relax_table[fragP->fr_subtype].rlx_length;
16571
0
}
16572
16573
/* Called after relax() is finished.
16574
16575
   In:  Address of frag.
16576
  fr_type == rs_machine_dependent.
16577
  fr_subtype is what the address relaxed to.
16578
16579
   Out: Any fixSs and constants are set up.
16580
  Caller will turn frag into a ".space 0".  */
16581
16582
void
16583
md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
16584
                 fragS *fragP)
16585
0
{
16586
0
  unsigned char *opcode;
16587
0
  unsigned char *where_to_put_displacement = NULL;
16588
0
  offsetT target_address;
16589
0
  offsetT opcode_address;
16590
0
  unsigned int extension = 0;
16591
0
  offsetT displacement_from_opcode_start;
16592
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
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
16596
0
    {
16597
      /* Generate nop padding.  */
16598
0
      unsigned int size = fragP->tc_frag_data.length;
16599
0
      if (size)
16600
0
  {
16601
0
    if (size > fragP->tc_frag_data.max_bytes)
16602
0
      abort ();
16603
16604
0
    if (flag_debug)
16605
0
      {
16606
0
        const char *msg;
16607
0
        const char *branch = "branch";
16608
0
        const char *prefix = "";
16609
0
        fragS *padding_fragP;
16610
0
        if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
16611
0
      == BRANCH_PREFIX)
16612
0
    {
16613
0
      padding_fragP = fragP->tc_frag_data.u.padding_fragP;
16614
0
      switch (fragP->tc_frag_data.default_prefix)
16615
0
        {
16616
0
        default:
16617
0
          abort ();
16618
0
          break;
16619
0
        case CS_PREFIX_OPCODE:
16620
0
          prefix = " cs";
16621
0
          break;
16622
0
        case DS_PREFIX_OPCODE:
16623
0
          prefix = " ds";
16624
0
          break;
16625
0
        case ES_PREFIX_OPCODE:
16626
0
          prefix = " es";
16627
0
          break;
16628
0
        case FS_PREFIX_OPCODE:
16629
0
          prefix = " fs";
16630
0
          break;
16631
0
        case GS_PREFIX_OPCODE:
16632
0
          prefix = " gs";
16633
0
          break;
16634
0
        case SS_PREFIX_OPCODE:
16635
0
          prefix = " ss";
16636
0
          break;
16637
0
        }
16638
0
      if (padding_fragP)
16639
0
        msg = _("%s:%u: add %d%s at 0x%llx to align "
16640
0
          "%s within %d-byte boundary\n");
16641
0
      else
16642
0
        msg = _("%s:%u: add additional %d%s at 0x%llx to "
16643
0
          "align %s within %d-byte boundary\n");
16644
0
    }
16645
0
        else
16646
0
    {
16647
0
      padding_fragP = fragP;
16648
0
      msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
16649
0
        "%s within %d-byte boundary\n");
16650
0
    }
16651
16652
0
        if (padding_fragP)
16653
0
    switch (padding_fragP->tc_frag_data.branch_type)
16654
0
      {
16655
0
      case align_branch_jcc:
16656
0
        branch = "jcc";
16657
0
        break;
16658
0
      case align_branch_fused:
16659
0
        branch = "fused jcc";
16660
0
        break;
16661
0
      case align_branch_jmp:
16662
0
        branch = "jmp";
16663
0
        break;
16664
0
      case align_branch_call:
16665
0
        branch = "call";
16666
0
        break;
16667
0
      case align_branch_indirect:
16668
0
        branch = "indiret branch";
16669
0
        break;
16670
0
      case align_branch_ret:
16671
0
        branch = "ret";
16672
0
        break;
16673
0
      default:
16674
0
        break;
16675
0
      }
16676
16677
0
        fprintf (stdout, msg,
16678
0
           fragP->fr_file, fragP->fr_line, size, prefix,
16679
0
           (long long) fragP->fr_address, branch,
16680
0
           1 << align_branch_power);
16681
0
      }
16682
0
    if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
16683
0
      memset (fragP->fr_opcode,
16684
0
        fragP->tc_frag_data.default_prefix, size);
16685
0
    else
16686
0
      i386_generate_nops (fragP, (char *) fragP->fr_opcode,
16687
0
        size, 0);
16688
0
    fragP->fr_fix += size;
16689
0
  }
16690
0
      return;
16691
0
    }
16692
16693
0
  opcode = (unsigned char *) fragP->fr_opcode;
16694
16695
  /* Address we want to reach in file space.  */
16696
0
  target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
16697
16698
  /* Address opcode resides at in file space.  */
16699
0
  opcode_address = fragP->fr_address + fragP->fr_fix;
16700
16701
  /* Displacement from opcode start to fill into instruction.  */
16702
0
  displacement_from_opcode_start = target_address - opcode_address;
16703
16704
0
  if ((fragP->fr_subtype & BIG) == 0)
16705
0
    {
16706
      /* Don't have to change opcode.  */
16707
0
      extension = 1;    /* 1 opcode + 1 displacement  */
16708
0
      where_to_put_displacement = &opcode[1];
16709
0
    }
16710
0
  else
16711
0
    {
16712
0
      if (fragP->tc_frag_data.no_cond_jump_promotion
16713
0
    && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
16714
0
  as_warn_where (fragP->fr_file, fragP->fr_line,
16715
0
           _("long jump required"));
16716
16717
0
      switch (fragP->fr_subtype)
16718
0
  {
16719
0
  case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
16720
0
    extension = 4;    /* 1 opcode + 4 displacement  */
16721
0
    opcode[0] = 0xe9;
16722
0
    where_to_put_displacement = &opcode[1];
16723
0
    break;
16724
16725
0
  case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
16726
0
    extension = 2;    /* 1 opcode + 2 displacement  */
16727
0
    opcode[0] = 0xe9;
16728
0
    where_to_put_displacement = &opcode[1];
16729
0
    break;
16730
16731
0
  case ENCODE_RELAX_STATE (COND_JUMP, BIG):
16732
0
  case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
16733
0
    extension = 5;    /* 2 opcode + 4 displacement  */
16734
0
    opcode[1] = opcode[0] + 0x10;
16735
0
    opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
16736
0
    where_to_put_displacement = &opcode[2];
16737
0
    break;
16738
16739
0
  case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
16740
0
    extension = 3;    /* 2 opcode + 2 displacement  */
16741
0
    opcode[1] = opcode[0] + 0x10;
16742
0
    opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
16743
0
    where_to_put_displacement = &opcode[2];
16744
0
    break;
16745
16746
0
  case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
16747
0
    extension = 4;
16748
0
    opcode[0] ^= 1;
16749
0
    opcode[1] = 3;
16750
0
    opcode[2] = 0xe9;
16751
0
    where_to_put_displacement = &opcode[3];
16752
0
    break;
16753
16754
0
  default:
16755
0
    BAD_CASE (fragP->fr_subtype);
16756
0
    break;
16757
0
  }
16758
0
    }
16759
16760
  /* If size if less then four we are sure that the operand fits,
16761
     but if it's 4, then it could be that the displacement is larger
16762
     then -/+ 2GB.  */
16763
0
  if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
16764
0
      && object_64bit
16765
0
      && ((addressT) (displacement_from_opcode_start - extension
16766
0
          + ((addressT) 1 << 31))
16767
0
    > (((addressT) 2 << 31) - 1)))
16768
0
    {
16769
0
      as_bad_where (fragP->fr_file, fragP->fr_line,
16770
0
        _("jump target out of range"));
16771
      /* Make us emit 0.  */
16772
0
      displacement_from_opcode_start = extension;
16773
0
    }
16774
  /* Now put displacement after opcode.  */
16775
0
  md_number_to_chars ((char *) where_to_put_displacement,
16776
0
          displacement_from_opcode_start - extension,
16777
0
          DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
16778
0
  fragP->fr_fix += extension;
16779
0
}
16780

16781
/* Apply a fixup (fixP) to segment data, once it has been determined
16782
   by our caller that we have all the info we need to fix it up.
16783
16784
   Parameter valP is the pointer to the value of the bits.
16785
16786
   On the 386, immediates, displacements, and data pointers are all in
16787
   the same (little-endian) format, so we don't need to care about which
16788
   we are handling.  */
16789
16790
void
16791
md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
16792
0
{
16793
0
  char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
16794
0
  valueT value = *valP;
16795
16796
0
#if !defined (TE_Mach)
16797
0
  if (fixP->fx_pcrel)
16798
0
    {
16799
0
      switch (fixP->fx_r_type)
16800
0
  {
16801
0
  default:
16802
0
    break;
16803
16804
0
  case BFD_RELOC_64:
16805
0
    fixP->fx_r_type = BFD_RELOC_64_PCREL;
16806
0
    break;
16807
0
  case BFD_RELOC_32:
16808
0
  case BFD_RELOC_X86_64_32S:
16809
0
    fixP->fx_r_type = BFD_RELOC_32_PCREL;
16810
0
    break;
16811
0
  case BFD_RELOC_16:
16812
0
    fixP->fx_r_type = BFD_RELOC_16_PCREL;
16813
0
    break;
16814
0
  case BFD_RELOC_8:
16815
0
    fixP->fx_r_type = BFD_RELOC_8_PCREL;
16816
0
    break;
16817
0
  }
16818
0
    }
16819
16820
0
  if (fixP->fx_addsy != NULL
16821
0
      && (fixP->fx_r_type == BFD_RELOC_32_PCREL
16822
0
    || fixP->fx_r_type == BFD_RELOC_64_PCREL
16823
0
    || fixP->fx_r_type == BFD_RELOC_16_PCREL
16824
0
    || fixP->fx_r_type == BFD_RELOC_8_PCREL)
16825
0
      && !use_rela_relocations)
16826
0
    {
16827
      /* This is a hack.  There should be a better way to handle this.
16828
   This covers for the fact that bfd_install_relocation will
16829
   subtract the current location (for partial_inplace, PC relative
16830
   relocations); see more below.  */
16831
0
#if defined (OBJ_ELF) || defined (TE_PE)
16832
0
      value += fixP->fx_where + fixP->fx_frag->fr_address;
16833
0
#endif
16834
0
#ifdef OBJ_ELF
16835
0
      segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
16836
16837
0
      if ((sym_seg == seg
16838
0
     || (symbol_section_p (fixP->fx_addsy)
16839
0
         && sym_seg != absolute_section))
16840
0
    && !generic_force_reloc (fixP))
16841
0
  {
16842
    /* Yes, we add the values in twice.  This is because
16843
       bfd_install_relocation subtracts them out again.  I think
16844
       bfd_install_relocation is broken, but I don't dare change
16845
       it.  FIXME.  */
16846
0
    value += fixP->fx_where + fixP->fx_frag->fr_address;
16847
0
  }
16848
0
#endif
16849
#if defined (OBJ_COFF) && defined (TE_PE)
16850
      /* For some reason, the PE format does not store a
16851
   section address offset for a PC relative symbol.  */
16852
      if (S_GET_SEGMENT (fixP->fx_addsy) != seg
16853
    || S_IS_WEAK (fixP->fx_addsy))
16854
  value += md_pcrel_from (fixP);
16855
#endif
16856
0
    }
16857
#if defined (OBJ_COFF) && defined (TE_PE)
16858
  if (fixP->fx_addsy != NULL
16859
      && S_IS_WEAK (fixP->fx_addsy)
16860
      /* PR 16858: Do not modify weak function references.  */
16861
      && ! fixP->fx_pcrel)
16862
    {
16863
#if !defined (TE_PEP)
16864
      /* For x86 PE weak function symbols are neither PC-relative
16865
   nor do they set S_IS_FUNCTION.  So the only reliable way
16866
   to detect them is to check the flags of their containing
16867
   section.  */
16868
      if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
16869
    && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
16870
  ;
16871
      else
16872
#endif
16873
      value -= S_GET_VALUE (fixP->fx_addsy);
16874
    }
16875
#endif
16876
16877
  /* Fix a few things - the dynamic linker expects certain values here,
16878
     and we must not disappoint it.  */
16879
0
#ifdef OBJ_ELF
16880
0
  if (fixP->fx_addsy)
16881
0
    switch (fixP->fx_r_type)
16882
0
      {
16883
0
      case BFD_RELOC_386_PLT32:
16884
0
      case BFD_RELOC_32_PLT_PCREL:
16885
  /* Make the jump instruction point to the address of the operand.
16886
     At runtime we merely add the offset to the actual PLT entry.
16887
     NB: Subtract the offset size only for jump instructions.  */
16888
0
  if (fixP->fx_pcrel)
16889
0
    value = -4;
16890
0
  break;
16891
16892
0
      case BFD_RELOC_386_PC32_TO_PLT32:
16893
  /* Set the implicit addend.  */
16894
0
  value = (seg->vma - fixP->fx_size + fixP->fx_addnumber
16895
0
     + md_pcrel_from (fixP));
16896
0
  break;
16897
16898
0
      case BFD_RELOC_386_TLS_GD:
16899
0
      case BFD_RELOC_386_TLS_LDM:
16900
0
      case BFD_RELOC_386_TLS_IE_32:
16901
0
      case BFD_RELOC_386_TLS_IE:
16902
0
      case BFD_RELOC_386_TLS_GOTIE:
16903
0
      case BFD_RELOC_386_TLS_GOTDESC:
16904
0
      case BFD_RELOC_X86_64_TLSGD:
16905
0
      case BFD_RELOC_X86_64_TLSLD:
16906
0
      case BFD_RELOC_X86_64_GOTTPOFF:
16907
0
      case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
16908
0
      case BFD_RELOC_X86_64_CODE_5_GOTTPOFF:
16909
0
      case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
16910
0
      case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
16911
0
      case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
16912
0
      case BFD_RELOC_X86_64_CODE_5_GOTPC32_TLSDESC:
16913
0
      case BFD_RELOC_X86_64_CODE_6_GOTPC32_TLSDESC:
16914
0
  value = 0; /* Fully resolved at runtime.  No addend.  */
16915
  /* Fallthrough */
16916
0
      case BFD_RELOC_386_TLS_LE:
16917
0
      case BFD_RELOC_386_TLS_LDO_32:
16918
0
      case BFD_RELOC_386_TLS_LE_32:
16919
0
      case BFD_RELOC_X86_64_DTPOFF32:
16920
0
      case BFD_RELOC_X86_64_DTPOFF64:
16921
0
      case BFD_RELOC_X86_64_TPOFF32:
16922
0
      case BFD_RELOC_X86_64_TPOFF64:
16923
0
  S_SET_THREAD_LOCAL (fixP->fx_addsy);
16924
0
  break;
16925
16926
0
      case BFD_RELOC_386_TLS_DESC_CALL:
16927
0
      case BFD_RELOC_X86_64_TLSDESC_CALL:
16928
0
  value = 0; /* Fully resolved at runtime.  No addend.  */
16929
0
  S_SET_THREAD_LOCAL (fixP->fx_addsy);
16930
0
  fixP->fx_done = 0;
16931
0
  return;
16932
16933
0
      case BFD_RELOC_VTABLE_INHERIT:
16934
0
      case BFD_RELOC_VTABLE_ENTRY:
16935
0
  fixP->fx_done = 0;
16936
0
  return;
16937
16938
0
      default:
16939
0
  break;
16940
0
      }
16941
0
#endif /* OBJ_ELF  */
16942
16943
  /* If not 64bit, massage value, to account for wraparound when !BFD64.  */
16944
0
  if (!object_64bit)
16945
0
    value = extend_to_32bit_address (value);
16946
16947
0
  *valP = value;
16948
0
#endif /* !defined (TE_Mach)  */
16949
16950
  /* Are we finished with this relocation now?  */
16951
0
  if (fixP->fx_addsy == NULL)
16952
0
    {
16953
0
      fixP->fx_done = 1;
16954
0
      switch (fixP->fx_r_type)
16955
0
  {
16956
0
  case BFD_RELOC_X86_64_32S:
16957
0
    fixP->fx_signed = 1;
16958
0
    break;
16959
16960
0
  default:
16961
0
    break;
16962
0
  }
16963
0
    }
16964
#if defined (OBJ_COFF) && defined (TE_PE)
16965
  else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
16966
    {
16967
      fixP->fx_done = 0;
16968
      /* Remember value for tc_gen_reloc.  */
16969
      fixP->fx_addnumber = value;
16970
      /* Clear out the frag for now.  */
16971
      value = 0;
16972
    }
16973
#endif
16974
0
  else if (use_rela_relocations)
16975
0
    {
16976
0
      if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
16977
0
  fixP->fx_no_overflow = 1;
16978
      /* Remember value for tc_gen_reloc.  */
16979
0
      fixP->fx_addnumber = value;
16980
0
      value = 0;
16981
0
    }
16982
16983
0
  md_number_to_chars (p, value, fixP->fx_size);
16984
0
}
16985

16986
const char *
16987
md_atof (int type, char *litP, int *sizeP)
16988
362
{
16989
  /* This outputs the LITTLENUMs in REVERSE order;
16990
     in accord with the bigendian 386.  */
16991
362
  return ieee_md_atof (type, litP, sizeP, false);
16992
362
}
16993

16994
static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
16995
16996
static char *
16997
output_invalid (int c)
16998
25.2k
{
16999
25.2k
  if (ISPRINT (c))
17000
6.57k
    snprintf (output_invalid_buf, sizeof (output_invalid_buf),
17001
6.57k
        "'%c'", c);
17002
18.6k
  else
17003
18.6k
    snprintf (output_invalid_buf, sizeof (output_invalid_buf),
17004
18.6k
        "(0x%x)", (unsigned char) c);
17005
25.2k
  return output_invalid_buf;
17006
25.2k
}
17007
17008
/* Verify that @r can be used in the current context.  */
17009
17010
static bool check_register (const reg_entry *r)
17011
13.8k
{
17012
13.8k
  if (allow_pseudo_reg)
17013
142
    return true;
17014
17015
13.6k
  if (operand_type_all_zero (&r->reg_type))
17016
0
    return false;
17017
17018
13.6k
  if ((r->reg_type.bitfield.dword
17019
7.92k
       || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
17020
6.34k
       || r->reg_type.bitfield.class == RegCR
17021
6.34k
       || r->reg_type.bitfield.class == RegDR)
17022
7.33k
      && !cpu_arch_flags.bitfield.cpui386)
17023
0
    return false;
17024
17025
13.6k
  if (r->reg_type.bitfield.class == RegTR
17026
0
      && (flag_code == CODE_64BIT
17027
0
    || !cpu_arch_flags.bitfield.cpui386
17028
0
    || cpu_arch_isa_flags.bitfield.cpui586
17029
0
    || cpu_arch_isa_flags.bitfield.cpui686))
17030
0
    return false;
17031
17032
13.6k
  if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
17033
0
    return false;
17034
17035
13.6k
  if (!cpu_arch_flags.bitfield.cpuavx512f)
17036
12.9k
    {
17037
12.9k
      if (r->reg_type.bitfield.zmmword
17038
12.9k
    || r->reg_type.bitfield.class == RegMask)
17039
17
  return false;
17040
17041
12.9k
      if (!cpu_arch_flags.bitfield.cpuavx)
17042
2.77k
  {
17043
2.77k
    if (r->reg_type.bitfield.ymmword)
17044
15
      return false;
17045
17046
2.76k
    if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
17047
0
      return false;
17048
2.76k
  }
17049
12.9k
    }
17050
17051
13.6k
  if (r->reg_type.bitfield.zmmword)
17052
0
    {
17053
0
      if (vector_size < VSZ512)
17054
0
  return false;
17055
17056
      /* Don't update pp when not dealing with insn operands.  */
17057
0
      switch (current_templates.start ? pp.encoding : encoding_evex)
17058
0
  {
17059
0
  case encoding_default:
17060
0
  case encoding_egpr:
17061
0
    pp.encoding = encoding_evex512;
17062
0
    break;
17063
0
  case encoding_evex:
17064
0
  case encoding_evex512:
17065
0
    break;
17066
0
  default:
17067
0
    pp.encoding = encoding_error;
17068
0
    break;
17069
0
  }
17070
0
    }
17071
17072
13.6k
  if (vector_size < VSZ256 && r->reg_type.bitfield.ymmword)
17073
0
    return false;
17074
17075
13.6k
  if (r->reg_type.bitfield.tmmword
17076
2
      && (!cpu_arch_flags.bitfield.cpuamx_tile
17077
0
          || flag_code != CODE_64BIT))
17078
2
    return false;
17079
17080
13.6k
  if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
17081
1
    return false;
17082
17083
  /* Don't allow fake index register unless allow_index_reg isn't 0. */
17084
13.6k
  if (!allow_index_reg && r->reg_num == RegIZ)
17085
0
    return false;
17086
17087
  /* Upper 16 vector registers are only available with VREX in 64bit
17088
     mode, and require EVEX encoding.  */
17089
13.6k
  if (r->reg_flags & RegVRex)
17090
2
    {
17091
2
      if (!cpu_arch_flags.bitfield.cpuavx512f
17092
0
    || flag_code != CODE_64BIT)
17093
2
  return false;
17094
17095
      /* Don't update pp when not dealing with insn operands.  */
17096
0
      switch (current_templates.start ? pp.encoding : encoding_evex)
17097
0
  {
17098
0
    case encoding_default:
17099
0
    case encoding_egpr:
17100
0
    case encoding_evex512:
17101
0
      pp.encoding = encoding_evex;
17102
0
      break;
17103
0
    case encoding_evex:
17104
0
      break;
17105
0
    default:
17106
0
      pp.encoding = encoding_error;
17107
0
      break;
17108
0
  }
17109
0
    }
17110
17111
13.6k
  if (r->reg_flags & RegRex2)
17112
4
    {
17113
4
      if (!cpu_arch_flags.bitfield.cpuapx_f
17114
3
    || flag_code != CODE_64BIT)
17115
1
  return false;
17116
17117
      /* Don't update pp when not dealing with insn operands.  */
17118
3
      switch (current_templates.start ? pp.encoding : encoding_egpr)
17119
3
  {
17120
3
  case encoding_default:
17121
3
    pp.encoding = encoding_egpr;
17122
3
    break;
17123
0
  case encoding_egpr:
17124
0
  case encoding_evex:
17125
0
  case encoding_evex512:
17126
0
    break;
17127
0
  default:
17128
0
    pp.encoding = encoding_error;
17129
0
    break;
17130
3
  }
17131
3
    }
17132
17133
13.6k
  if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
17134
155
      && flag_code != CODE_64BIT
17135
17
      && (!cpu_arch_flags.bitfield.cpualtmovcr8
17136
0
    || r->reg_type.bitfield.class != RegCR
17137
0
    || r->reg_num != 0
17138
0
    || dot_insn ()))
17139
17
    return false;
17140
17141
13.6k
  if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
17142
474
      && !intel_syntax)
17143
0
    return false;
17144
17145
13.6k
  return true;
17146
13.6k
}
17147
17148
/* REG_STRING starts *before* REGISTER_PREFIX.  */
17149
17150
static const reg_entry *
17151
parse_real_register (const char *reg_string, char **end_op)
17152
14.4k
{
17153
14.4k
  const char *s = reg_string;
17154
14.4k
  char *p;
17155
14.4k
  char reg_name_given[MAX_REG_NAME_SIZE + 1];
17156
14.4k
  const reg_entry *r;
17157
17158
  /* Skip possible REGISTER_PREFIX and possible whitespace.  */
17159
14.4k
  if (*s == REGISTER_PREFIX)
17160
14.2k
    ++s;
17161
17162
14.4k
  if (is_whitespace (*s))
17163
13
    ++s;
17164
17165
14.4k
  p = reg_name_given;
17166
38.3k
  while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
17167
23.9k
    {
17168
23.9k
      if (p >= reg_name_given + MAX_REG_NAME_SIZE)
17169
30
  return NULL;
17170
23.8k
      s++;
17171
23.8k
    }
17172
17173
14.3k
  if (is_part_of_name (*s))
17174
363
    return NULL;
17175
17176
14.0k
  *end_op = (char *) s;
17177
17178
14.0k
  r = str_hash_find (reg_hash, reg_name_given);
17179
17180
  /* Handle floating point regs, allowing spaces in the (i) part.  */
17181
14.0k
  if (r == reg_st0)
17182
305
    {
17183
305
      if (!cpu_arch_flags.bitfield.cpu8087
17184
304
    && !cpu_arch_flags.bitfield.cpu287
17185
304
    && !cpu_arch_flags.bitfield.cpu387
17186
0
    && !allow_pseudo_reg)
17187
0
  return NULL;
17188
17189
305
      if (is_whitespace (*s))
17190
9
  ++s;
17191
305
      if (*s == '(')
17192
18
  {
17193
18
    ++s;
17194
18
    if (is_whitespace (*s))
17195
0
      ++s;
17196
18
    if (*s >= '0' && *s <= '7')
17197
16
      {
17198
16
        int fpr = *s - '0';
17199
16
        ++s;
17200
16
        if (is_whitespace (*s))
17201
0
    ++s;
17202
16
        if (*s == ')')
17203
13
    {
17204
13
      *end_op = (char *) s + 1;
17205
13
      know (r[fpr].reg_num == fpr);
17206
13
      return r + fpr;
17207
13
    }
17208
16
      }
17209
    /* We have "%st(" then garbage.  */
17210
5
    return NULL;
17211
18
  }
17212
305
    }
17213
17214
14.0k
  return r && check_register (r) ? r : NULL;
17215
14.0k
}
17216
17217
/* REG_STRING starts *before* REGISTER_PREFIX.  */
17218
17219
static const reg_entry *
17220
parse_register (const char *reg_string, char **end_op)
17221
7.14k
{
17222
7.14k
  const reg_entry *r;
17223
17224
7.14k
  if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
17225
1.32k
    r = parse_real_register (reg_string, end_op);
17226
5.81k
  else
17227
5.81k
    r = NULL;
17228
7.14k
  if (!r)
17229
5.83k
    {
17230
5.83k
      char *save = input_line_pointer;
17231
5.83k
      char *buf = xstrdup (reg_string), *name;
17232
5.83k
      symbolS *symbolP;
17233
5.83k
      offsetT off;
17234
17235
5.83k
      input_line_pointer = buf;
17236
5.83k
      get_symbol_name (&name);
17237
5.83k
      symbolP = symbol_find (name);
17238
5.83k
      symbolP = symbol_equated_to (symbolP, &off);
17239
5.83k
      if (symbolP && off == 0 && S_GET_SEGMENT (symbolP) == reg_section)
17240
2.27k
  {
17241
2.27k
    const expressionS *e = symbol_get_value_expression (symbolP);
17242
17243
2.27k
    if (e->X_op == O_register)
17244
2.27k
      {
17245
2.27k
        know ((valueT) e->X_add_number < i386_regtab_size);
17246
2.27k
        r = i386_regtab + e->X_add_number;
17247
2.27k
        *end_op = (char *) reg_string + (input_line_pointer - buf);
17248
2.27k
      }
17249
2.27k
    if (r && !check_register (r))
17250
17
      {
17251
17
        as_bad (_("register '%s%s' cannot be used here"),
17252
17
          register_prefix, r->reg_name);
17253
17
        r = &bad_reg;
17254
17
      }
17255
2.27k
  }
17256
5.83k
      input_line_pointer = save;
17257
5.83k
      free (buf);
17258
5.83k
    }
17259
7.14k
  return r;
17260
7.14k
}
17261
17262
int
17263
i386_parse_name (char *name,
17264
     expressionS *e,
17265
     enum expr_mode mode,
17266
     char *nextcharP)
17267
64.2k
{
17268
64.2k
  const reg_entry *r = NULL;
17269
64.2k
  char *end = input_line_pointer;
17270
17271
  /* We only know the terminating character here.  It being double quote could
17272
     be the closing one of a quoted symbol name, or an opening one from a
17273
     following string (or another quoted symbol name).  Since the latter can't
17274
     be valid syntax for anything, bailing in either case is good enough.  */
17275
64.2k
  if (*nextcharP == '"')
17276
15.2k
    return 0;
17277
17278
48.9k
  *end = *nextcharP;
17279
48.9k
  if (*name == REGISTER_PREFIX || allow_naked_reg)
17280
128
    r = parse_real_register (name, &input_line_pointer);
17281
48.9k
  if (r && end <= input_line_pointer)
17282
0
    {
17283
0
      *nextcharP = *input_line_pointer;
17284
0
      *input_line_pointer = 0;
17285
0
      e->X_op = O_register;
17286
0
      e->X_add_number = r - i386_regtab;
17287
0
      return 1;
17288
0
    }
17289
48.9k
  input_line_pointer = end;
17290
48.9k
  *end = 0;
17291
48.9k
  return intel_syntax ? i386_intel_parse_name (name, e, mode) : 0;
17292
48.9k
}
17293
17294
void
17295
md_operand (expressionS *e)
17296
25.1k
{
17297
25.1k
  char *end;
17298
25.1k
  const reg_entry *r;
17299
17300
25.1k
  switch (*input_line_pointer)
17301
25.1k
    {
17302
12.9k
    case REGISTER_PREFIX:
17303
12.9k
      r = parse_real_register (input_line_pointer, &end);
17304
12.9k
      if (r)
17305
4.99k
  {
17306
4.99k
    e->X_op = O_register;
17307
4.99k
    e->X_add_number = r - i386_regtab;
17308
4.99k
    input_line_pointer = end;
17309
4.99k
  }
17310
12.9k
      break;
17311
17312
31
    case '[':
17313
31
      gas_assert (intel_syntax);
17314
31
      end = input_line_pointer++;
17315
31
      expression (e);
17316
31
      if (*input_line_pointer == ']')
17317
0
  {
17318
0
    ++input_line_pointer;
17319
0
    e->X_op_symbol = make_expr_symbol (e);
17320
0
    e->X_add_symbol = NULL;
17321
0
    e->X_add_number = 0;
17322
0
    e->X_op = O_index;
17323
0
  }
17324
31
      else
17325
31
  {
17326
31
    e->X_op = O_absent;
17327
31
    input_line_pointer = end;
17328
31
  }
17329
31
      break;
17330
25.1k
    }
17331
25.1k
}
17332
17333
#ifdef BFD64
17334
/* To maintain consistency with !BFD64 builds of gas record, whether any
17335
   (binary) operator was involved in an expression.  As expressions are
17336
   evaluated in only 32 bits when !BFD64, we use this to decide whether to
17337
   truncate results.  */
17338
bool i386_record_operator (operatorT op,
17339
         const expressionS *left,
17340
         const expressionS *right)
17341
58.3k
{
17342
58.3k
  if (op == O_absent)
17343
1.62k
    return false;
17344
17345
56.7k
  if (!left)
17346
19.3k
    {
17347
      /* Since the expression parser applies unary operators fine to bignum
17348
   operands, we don't need to be concerned of respective operands not
17349
   fitting in 32 bits.  */
17350
19.3k
      if (right->X_op == O_constant && right->X_unsigned
17351
2.57k
    && !fits_in_unsigned_long (right->X_add_number))
17352
345
  return false;
17353
19.3k
    }
17354
  /* This isn't entirely right: The pattern can also result when constant
17355
     expressions are folded (e.g. 0xffffffff + 1).  */
17356
37.4k
  else if ((left->X_op == O_constant && left->X_unsigned
17357
8.64k
      && !fits_in_unsigned_long (left->X_add_number))
17358
37.4k
     || (right->X_op == O_constant && right->X_unsigned
17359
18.3k
         && !fits_in_unsigned_long (right->X_add_number)))
17360
45
    expr_mode = expr_large_value;
17361
17362
56.4k
  if (expr_mode != expr_large_value)
17363
55.9k
    expr_mode = expr_operator_present;
17364
17365
56.4k
  return false;
17366
56.7k
}
17367
#endif
17368

17369
const char md_shortopts[] =
17370
#ifdef OBJ_ELF
17371
  "kVQ:"
17372
# ifdef TE_SOLARIS
17373
  "s"
17374
# endif
17375
#endif
17376
  "qnO::";
17377
17378
0
#define OPTION_32 (OPTION_MD_BASE + 0)
17379
0
#define OPTION_64 (OPTION_MD_BASE + 1)
17380
0
#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
17381
0
#define OPTION_MARCH (OPTION_MD_BASE + 3)
17382
0
#define OPTION_MTUNE (OPTION_MD_BASE + 4)
17383
0
#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
17384
0
#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
17385
0
#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
17386
0
#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
17387
0
#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
17388
0
#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
17389
0
#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
17390
0
#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
17391
0
#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
17392
0
#define OPTION_X32 (OPTION_MD_BASE + 14)
17393
0
#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
17394
0
#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
17395
0
#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
17396
#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
17397
0
#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
17398
0
#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
17399
0
#define OPTION_MSHARED (OPTION_MD_BASE + 21)
17400
0
#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
17401
0
#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
17402
0
#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
17403
0
#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
17404
0
#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
17405
0
#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
17406
0
#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
17407
0
#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
17408
0
#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
17409
0
#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
17410
0
#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
17411
0
#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
17412
0
#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
17413
0
#define OPTION_MTLS_CHECK (OPTION_MD_BASE + 35)
17414
17415
const struct option md_longopts[] =
17416
{
17417
  {"32", no_argument, NULL, OPTION_32},
17418
#if (defined (OBJ_ELF) || defined (TE_PE) || defined (OBJ_MACH_O)) \
17419
    && defined (BFD64)
17420
  {"64", no_argument, NULL, OPTION_64},
17421
#endif
17422
#ifdef OBJ_ELF
17423
# ifdef BFD64
17424
  {"x32", no_argument, NULL, OPTION_X32},
17425
# endif
17426
  {"mshared", no_argument, NULL, OPTION_MSHARED},
17427
  {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
17428
#endif
17429
  {"divide", no_argument, NULL, OPTION_DIVIDE},
17430
  {"march", required_argument, NULL, OPTION_MARCH},
17431
  {"mtune", required_argument, NULL, OPTION_MTUNE},
17432
  {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
17433
  {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
17434
  {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
17435
  {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
17436
  {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
17437
  {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
17438
  {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
17439
  {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
17440
  {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
17441
  {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
17442
  {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
17443
  {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
17444
  {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
17445
# if defined (TE_PE) || defined (TE_PEP)
17446
  {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
17447
#endif
17448
  {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
17449
  {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
17450
  {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
17451
  {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
17452
  {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
17453
  {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
17454
  {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
17455
  {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
17456
  {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
17457
  {"mlfence-before-indirect-branch", required_argument, NULL,
17458
   OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
17459
  {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
17460
  {"mamd64", no_argument, NULL, OPTION_MAMD64},
17461
  {"mintel64", no_argument, NULL, OPTION_MINTEL64},
17462
  {"mtls-check", required_argument, NULL, OPTION_MTLS_CHECK},
17463
  {NULL, no_argument, NULL, 0}
17464
};
17465
const size_t md_longopts_size = sizeof (md_longopts);
17466
17467
int
17468
md_parse_option (int c, const char *arg)
17469
0
{
17470
0
  unsigned int j;
17471
0
  char *arch, *next, *saved, *type;
17472
17473
0
  switch (c)
17474
0
    {
17475
0
    case 'n':
17476
0
      optimize_align_code = 0;
17477
0
      break;
17478
17479
0
    case 'q':
17480
0
      quiet_warnings = 1;
17481
0
      break;
17482
17483
0
#ifdef OBJ_ELF
17484
      /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
17485
   should be emitted or not.  FIXME: Not implemented.  */
17486
0
    case 'Q':
17487
0
      if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
17488
0
  return 0;
17489
0
      break;
17490
17491
      /* -V: SVR4 argument to print version ID.  */
17492
0
    case 'V':
17493
0
      print_version_id ();
17494
0
      break;
17495
17496
      /* -k: Ignore for FreeBSD compatibility.  */
17497
0
    case 'k':
17498
0
      break;
17499
17500
# ifdef TE_SOLARIS
17501
    case 's':
17502
      /* -s: On i386 Solaris, this tells the native assembler to use
17503
   .stab instead of .stab.excl.  We always use .stab anyhow.  */
17504
      break;
17505
# endif
17506
17507
0
    case OPTION_MSHARED:
17508
0
      shared = 1;
17509
0
      break;
17510
17511
0
    case OPTION_X86_USED_NOTE:
17512
0
      if (strcasecmp (arg, "yes") == 0)
17513
0
        x86_used_note = 1;
17514
0
      else if (strcasecmp (arg, "no") == 0)
17515
0
        x86_used_note = 0;
17516
0
      else
17517
0
        as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
17518
0
      break;
17519
0
#endif
17520
17521
0
#ifdef BFD64
17522
17523
0
#if (defined (OBJ_ELF) || defined (TE_PE) || defined (OBJ_MACH_O))
17524
0
    case OPTION_64:
17525
0
      {
17526
0
  const char **list, **l;
17527
17528
0
  list = bfd_target_list ();
17529
0
  for (l = list; *l != NULL; l++)
17530
0
#if defined (OBJ_ELF)
17531
0
    if (strcmp (*l, ELF_TARGET_FORMAT64) == 0)
17532
#elif defined (TE_PE)
17533
    if (strcmp (*l, "pe-x86-64") == 0)
17534
#else
17535
    if (strcmp (*l, "mach-o-x86-64") == 0)
17536
#endif
17537
0
      {
17538
0
        default_arch = "x86_64";
17539
0
        break;
17540
0
      }
17541
0
  if (*l == NULL)
17542
0
    as_fatal (_("no compiled in support for x86_64"));
17543
0
  free (list);
17544
0
      }
17545
0
      break;
17546
0
#endif
17547
17548
0
#ifdef OBJ_ELF
17549
0
    case OPTION_X32:
17550
0
      {
17551
0
  const char **list, **l;
17552
17553
0
  list = bfd_target_list ();
17554
0
  for (l = list; *l != NULL; l++)
17555
0
    if (strcmp (*l, ELF_TARGET_FORMAT32) == 0)
17556
0
      {
17557
0
        default_arch = "x86_64:32";
17558
0
        break;
17559
0
      }
17560
0
  if (*l == NULL)
17561
0
    as_fatal (_("no compiled in support for 32bit x86_64"));
17562
0
  free (list);
17563
0
      }
17564
0
      break;
17565
0
#endif
17566
17567
0
#endif /* BFD64 */
17568
17569
0
    case OPTION_32:
17570
0
      {
17571
0
  const char **list, **l;
17572
17573
0
  list = bfd_target_list ();
17574
0
  for (l = list; *l != NULL; l++)
17575
0
    if (strstr (*l, "-i386")
17576
0
        || strstr (*l, "-go32"))
17577
0
      {
17578
0
        default_arch = "i386";
17579
0
        break;
17580
0
      }
17581
0
  if (*l == NULL)
17582
0
    as_fatal (_("no compiled in support for ix86"));
17583
0
  free (list);
17584
0
      }
17585
0
      break;
17586
17587
0
    case OPTION_DIVIDE:
17588
#ifdef SVR4_COMMENT_CHARS
17589
      {
17590
  char *n, *t;
17591
  const char *s;
17592
17593
  n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
17594
  t = n;
17595
  for (s = i386_comment_chars; *s != '\0'; s++)
17596
    if (*s != '/')
17597
      *t++ = *s;
17598
  *t = '\0';
17599
  i386_comment_chars = n;
17600
      }
17601
#endif
17602
0
      break;
17603
17604
0
    case OPTION_MARCH:
17605
0
      saved = xstrdup (arg);
17606
0
      arch = saved;
17607
      /* Allow -march=+nosse.  */
17608
0
      if (*arch == '+')
17609
0
  arch++;
17610
0
      do
17611
0
  {
17612
0
    char *vsz;
17613
17614
0
    if (*arch == '.')
17615
0
      as_fatal (_("invalid -march= option: `%s'"), arg);
17616
0
    next = strchr (arch, '+');
17617
0
    if (next)
17618
0
      *next++ = '\0';
17619
0
    vsz = strchr (arch, '/');
17620
0
    if (vsz)
17621
0
      *vsz++ = '\0';
17622
0
    for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
17623
0
      {
17624
0
        if (vsz && cpu_arch[j].vsz != vsz_set)
17625
0
    continue;
17626
17627
0
        if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
17628
0
            && strcmp (arch, cpu_arch[j].name) == 0)
17629
0
    {
17630
      /* Processor.  */
17631
0
      if (! cpu_arch[j].enable.bitfield.cpui386)
17632
0
        continue;
17633
17634
0
      cpu_arch_name = cpu_arch[j].name;
17635
0
      free (cpu_sub_arch_name);
17636
0
      cpu_sub_arch_name = NULL;
17637
0
      cpu_arch_flags = cpu_arch[j].enable;
17638
0
      cpu_arch_isa = cpu_arch[j].type;
17639
0
      cpu_arch_isa_flags = cpu_arch[j].enable;
17640
0
      if (!cpu_arch_tune_set)
17641
0
        cpu_arch_tune = cpu_arch_isa;
17642
0
      vector_size = VSZ_DEFAULT;
17643
0
      break;
17644
0
    }
17645
0
        else if (cpu_arch[j].type == PROCESSOR_NONE
17646
0
           && strcmp (arch, cpu_arch[j].name) == 0
17647
0
           && !cpu_flags_all_zero (&cpu_arch[j].enable))
17648
0
    {
17649
      /* ISA extension.  */
17650
0
      isa_enable (j);
17651
17652
0
      switch (cpu_arch[j].vsz)
17653
0
        {
17654
0
        default:
17655
0
          break;
17656
17657
0
        case vsz_set:
17658
0
          if (vsz)
17659
0
      {
17660
0
        char *end;
17661
0
        unsigned long val = strtoul (vsz, &end, 0);
17662
17663
0
        if (*end)
17664
0
          val = 0;
17665
0
        switch (val)
17666
0
          {
17667
0
          case 512: vector_size = VSZ512; break;
17668
0
          case 256: vector_size = VSZ256; break;
17669
0
          case 128: vector_size = VSZ128; break;
17670
0
          default:
17671
0
            as_warn (_("Unrecognized vector size specifier ignored"));
17672
0
            break;
17673
0
          }
17674
0
        break;
17675
0
      }
17676
      /* Fall through.  */
17677
0
        case vsz_reset:
17678
0
          vector_size = VSZ_DEFAULT;
17679
0
          break;
17680
0
        }
17681
17682
0
      break;
17683
0
    }
17684
0
      }
17685
17686
0
    if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
17687
0
      {
17688
        /* Disable an ISA extension.  */
17689
0
        for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
17690
0
    if (cpu_arch[j].type == PROCESSOR_NONE
17691
0
        && strcmp (arch + 2, cpu_arch[j].name) == 0)
17692
0
      {
17693
0
        isa_disable (j);
17694
0
        if (cpu_arch[j].vsz == vsz_set)
17695
0
          vector_size = VSZ_DEFAULT;
17696
0
        break;
17697
0
      }
17698
0
      }
17699
17700
0
    if (j >= ARRAY_SIZE (cpu_arch))
17701
0
      as_fatal (_("invalid -march= option: `%s'"), arg);
17702
17703
0
    arch = next;
17704
0
  }
17705
0
      while (next != NULL);
17706
0
      free (saved);
17707
0
      break;
17708
17709
0
    case OPTION_MTUNE:
17710
0
      if (*arg == '.')
17711
0
  as_fatal (_("invalid -mtune= option: `%s'"), arg);
17712
0
      for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
17713
0
  {
17714
0
    if (cpu_arch[j].type != PROCESSOR_NONE
17715
0
        && strcmp (arg, cpu_arch[j].name) == 0)
17716
0
      {
17717
0
        cpu_arch_tune_set = 1;
17718
0
        cpu_arch_tune = cpu_arch [j].type;
17719
0
        break;
17720
0
      }
17721
0
  }
17722
0
      if (j >= ARRAY_SIZE (cpu_arch))
17723
0
  as_fatal (_("invalid -mtune= option: `%s'"), arg);
17724
0
      break;
17725
17726
0
    case OPTION_MMNEMONIC:
17727
0
      if (strcasecmp (arg, "att") == 0)
17728
0
  intel_mnemonic = 0;
17729
0
      else if (strcasecmp (arg, "intel") == 0)
17730
0
  intel_mnemonic = 1;
17731
0
      else
17732
0
  as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
17733
0
      break;
17734
17735
0
    case OPTION_MSYNTAX:
17736
0
      if (strcasecmp (arg, "att") == 0)
17737
0
  _set_intel_syntax (0);
17738
0
      else if (strcasecmp (arg, "intel") == 0)
17739
0
  _set_intel_syntax (1);
17740
0
      else
17741
0
  as_fatal (_("invalid -msyntax= option: `%s'"), arg);
17742
0
      break;
17743
17744
0
    case OPTION_MINDEX_REG:
17745
0
      allow_index_reg = 1;
17746
0
      break;
17747
17748
0
    case OPTION_MNAKED_REG:
17749
0
      allow_naked_reg = 1;
17750
0
      register_prefix = "";
17751
0
      break;
17752
17753
0
    case OPTION_MSSE2AVX:
17754
0
      sse2avx = 1;
17755
0
      break;
17756
17757
0
    case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
17758
0
      use_unaligned_vector_move = 1;
17759
0
      break;
17760
17761
0
    case OPTION_MSSE_CHECK:
17762
0
      if (strcasecmp (arg, "error") == 0)
17763
0
  sse_check = check_error;
17764
0
      else if (strcasecmp (arg, "warning") == 0)
17765
0
  sse_check = check_warning;
17766
0
      else if (strcasecmp (arg, "none") == 0)
17767
0
  sse_check = check_none;
17768
0
      else
17769
0
  as_fatal (_("invalid -msse-check= option: `%s'"), arg);
17770
0
      break;
17771
17772
0
    case OPTION_MOPERAND_CHECK:
17773
0
      if (strcasecmp (arg, "error") == 0)
17774
0
  operand_check = check_error;
17775
0
      else if (strcasecmp (arg, "warning") == 0)
17776
0
  operand_check = check_warning;
17777
0
      else if (strcasecmp (arg, "none") == 0)
17778
0
  operand_check = check_none;
17779
0
      else
17780
0
  as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
17781
0
      break;
17782
17783
0
    case OPTION_MAVXSCALAR:
17784
0
      if (strcasecmp (arg, "128") == 0)
17785
0
  avxscalar = vex128;
17786
0
      else if (strcasecmp (arg, "256") == 0)
17787
0
  avxscalar = vex256;
17788
0
      else
17789
0
  as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
17790
0
      break;
17791
17792
0
    case OPTION_MVEXWIG:
17793
0
      if (strcmp (arg, "0") == 0)
17794
0
  vexwig = vexw0;
17795
0
      else if (strcmp (arg, "1") == 0)
17796
0
  vexwig = vexw1;
17797
0
      else
17798
0
  as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
17799
0
      break;
17800
17801
0
    case OPTION_MADD_BND_PREFIX:
17802
0
      add_bnd_prefix = 1;
17803
0
      break;
17804
17805
0
    case OPTION_MEVEXLIG:
17806
0
      if (strcmp (arg, "128") == 0)
17807
0
  evexlig = evexl128;
17808
0
      else if (strcmp (arg, "256") == 0)
17809
0
  evexlig = evexl256;
17810
0
      else  if (strcmp (arg, "512") == 0)
17811
0
  evexlig = evexl512;
17812
0
      else
17813
0
  as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
17814
0
      break;
17815
17816
0
    case OPTION_MEVEXRCIG:
17817
0
      if (strcmp (arg, "rne") == 0)
17818
0
  evexrcig = rne;
17819
0
      else if (strcmp (arg, "rd") == 0)
17820
0
  evexrcig = rd;
17821
0
      else if (strcmp (arg, "ru") == 0)
17822
0
  evexrcig = ru;
17823
0
      else if (strcmp (arg, "rz") == 0)
17824
0
  evexrcig = rz;
17825
0
      else
17826
0
  as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
17827
0
      break;
17828
17829
0
    case OPTION_MEVEXWIG:
17830
0
      if (strcmp (arg, "0") == 0)
17831
0
  evexwig = evexw0;
17832
0
      else if (strcmp (arg, "1") == 0)
17833
0
  evexwig = evexw1;
17834
0
      else
17835
0
  as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
17836
0
      break;
17837
17838
# if defined (TE_PE) || defined (TE_PEP)
17839
    case OPTION_MBIG_OBJ:
17840
      use_big_obj = 1;
17841
      break;
17842
#endif
17843
17844
0
    case OPTION_MOMIT_LOCK_PREFIX:
17845
0
      if (strcasecmp (arg, "yes") == 0)
17846
0
        omit_lock_prefix = 1;
17847
0
      else if (strcasecmp (arg, "no") == 0)
17848
0
        omit_lock_prefix = 0;
17849
0
      else
17850
0
        as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
17851
0
      break;
17852
17853
0
    case OPTION_MFENCE_AS_LOCK_ADD:
17854
0
      if (strcasecmp (arg, "yes") == 0)
17855
0
        avoid_fence = 1;
17856
0
      else if (strcasecmp (arg, "no") == 0)
17857
0
        avoid_fence = 0;
17858
0
      else
17859
0
        as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
17860
0
      break;
17861
17862
0
    case OPTION_MLFENCE_AFTER_LOAD:
17863
0
      if (strcasecmp (arg, "yes") == 0)
17864
0
  lfence_after_load = 1;
17865
0
      else if (strcasecmp (arg, "no") == 0)
17866
0
  lfence_after_load = 0;
17867
0
      else
17868
0
        as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
17869
0
      break;
17870
17871
0
    case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
17872
0
      if (strcasecmp (arg, "all") == 0)
17873
0
  {
17874
0
    lfence_before_indirect_branch = lfence_branch_all;
17875
0
    if (lfence_before_ret == lfence_before_ret_none)
17876
0
      lfence_before_ret = lfence_before_ret_shl;
17877
0
  }
17878
0
      else if (strcasecmp (arg, "memory") == 0)
17879
0
  lfence_before_indirect_branch = lfence_branch_memory;
17880
0
      else if (strcasecmp (arg, "register") == 0)
17881
0
  lfence_before_indirect_branch = lfence_branch_register;
17882
0
      else if (strcasecmp (arg, "none") == 0)
17883
0
  lfence_before_indirect_branch = lfence_branch_none;
17884
0
      else
17885
0
        as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
17886
0
      arg);
17887
0
      break;
17888
17889
0
    case OPTION_MLFENCE_BEFORE_RET:
17890
0
      if (strcasecmp (arg, "or") == 0)
17891
0
  lfence_before_ret = lfence_before_ret_or;
17892
0
      else if (strcasecmp (arg, "not") == 0)
17893
0
  lfence_before_ret = lfence_before_ret_not;
17894
0
      else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
17895
0
  lfence_before_ret = lfence_before_ret_shl;
17896
0
      else if (strcasecmp (arg, "none") == 0)
17897
0
  lfence_before_ret = lfence_before_ret_none;
17898
0
      else
17899
0
        as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
17900
0
      arg);
17901
0
      break;
17902
17903
0
    case OPTION_MRELAX_RELOCATIONS:
17904
0
      if (strcasecmp (arg, "yes") == 0)
17905
0
        generate_relax_relocations = 1;
17906
0
      else if (strcasecmp (arg, "no") == 0)
17907
0
        generate_relax_relocations = 0;
17908
0
      else
17909
0
        as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
17910
0
      break;
17911
17912
0
    case OPTION_MALIGN_BRANCH_BOUNDARY:
17913
0
      {
17914
0
  char *end;
17915
0
  long int align = strtoul (arg, &end, 0);
17916
0
  if (*end == '\0')
17917
0
    {
17918
0
      if (align == 0)
17919
0
        {
17920
0
    align_branch_power = 0;
17921
0
    break;
17922
0
        }
17923
0
      else if (align >= 16)
17924
0
        {
17925
0
    int align_power;
17926
0
    for (align_power = 0;
17927
0
         (align & 1) == 0;
17928
0
         align >>= 1, align_power++)
17929
0
      continue;
17930
    /* Limit alignment power to 31.  */
17931
0
    if (align == 1 && align_power < 32)
17932
0
      {
17933
0
        align_branch_power = align_power;
17934
0
        break;
17935
0
      }
17936
0
        }
17937
0
    }
17938
0
  as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
17939
0
      }
17940
0
      break;
17941
17942
0
    case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
17943
0
      {
17944
0
  char *end;
17945
0
  int align = strtoul (arg, &end, 0);
17946
  /* Some processors only support 5 prefixes.  */
17947
0
  if (*end == '\0' && align >= 0 && align < 6)
17948
0
    {
17949
0
      align_branch_prefix_size = align;
17950
0
      break;
17951
0
    }
17952
0
  as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
17953
0
      arg);
17954
0
      }
17955
0
      break;
17956
17957
0
    case OPTION_MALIGN_BRANCH:
17958
0
      align_branch = 0;
17959
0
      saved = xstrdup (arg);
17960
0
      type = saved;
17961
0
      do
17962
0
  {
17963
0
    next = strchr (type, '+');
17964
0
    if (next)
17965
0
      *next++ = '\0';
17966
0
    if (strcasecmp (type, "jcc") == 0)
17967
0
      align_branch |= align_branch_jcc_bit;
17968
0
    else if (strcasecmp (type, "fused") == 0)
17969
0
      align_branch |= align_branch_fused_bit;
17970
0
    else if (strcasecmp (type, "jmp") == 0)
17971
0
      align_branch |= align_branch_jmp_bit;
17972
0
    else if (strcasecmp (type, "call") == 0)
17973
0
      align_branch |= align_branch_call_bit;
17974
0
    else if (strcasecmp (type, "ret") == 0)
17975
0
      align_branch |= align_branch_ret_bit;
17976
0
    else if (strcasecmp (type, "indirect") == 0)
17977
0
      align_branch |= align_branch_indirect_bit;
17978
0
    else
17979
0
      as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
17980
0
    type = next;
17981
0
  }
17982
0
      while (next != NULL);
17983
0
      free (saved);
17984
0
      break;
17985
17986
0
    case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
17987
0
      align_branch_power = 5;
17988
0
      align_branch_prefix_size = 5;
17989
0
      align_branch = (align_branch_jcc_bit
17990
0
          | align_branch_fused_bit
17991
0
          | align_branch_jmp_bit);
17992
0
      break;
17993
17994
0
    case OPTION_MAMD64:
17995
0
      isa64 = amd64;
17996
0
      break;
17997
17998
0
    case OPTION_MINTEL64:
17999
0
      isa64 = intel64;
18000
0
      break;
18001
18002
0
    case 'O':
18003
0
      if (arg == NULL)
18004
0
  {
18005
0
    optimize = 1;
18006
    /* Turn off -Os.  */
18007
0
    optimize_for_space = 0;
18008
0
  }
18009
0
      else if (*arg == 's')
18010
0
  {
18011
0
    optimize_for_space = 1;
18012
    /* Turn on all encoding optimizations.  */
18013
0
    optimize = INT_MAX;
18014
0
  }
18015
0
      else
18016
0
  {
18017
0
    optimize = atoi (arg);
18018
    /* Turn off -Os.  */
18019
0
    optimize_for_space = 0;
18020
0
  }
18021
0
      break;
18022
0
    case OPTION_MTLS_CHECK:
18023
0
      if (strcasecmp (arg, "yes") == 0)
18024
0
  tls_check = true;
18025
0
      else if (strcasecmp (arg, "no") == 0)
18026
0
  tls_check = false;
18027
0
      else
18028
0
  as_fatal (_("invalid -mtls-check= option: `%s'"), arg);
18029
0
      break;
18030
18031
0
    default:
18032
0
      return 0;
18033
0
    }
18034
0
  return 1;
18035
0
}
18036
18037
0
#define MESSAGE_TEMPLATE \
18038
0
"                                                                                "
18039
18040
static char *
18041
output_message (FILE *stream, char *p, char *message, char *start,
18042
    int *left_p, const char *name, int len)
18043
0
{
18044
0
  int size = sizeof (MESSAGE_TEMPLATE);
18045
0
  int left = *left_p;
18046
18047
  /* Reserve 2 spaces for ", " or ",\0" */
18048
0
  left -= len + 2;
18049
18050
  /* Check if there is any room.  */
18051
0
  if (left >= 0)
18052
0
    {
18053
0
      if (p != start)
18054
0
  {
18055
0
    *p++ = ',';
18056
0
    *p++ = ' ';
18057
0
  }
18058
0
      p = mempcpy (p, name, len);
18059
0
    }
18060
0
  else
18061
0
    {
18062
      /* Output the current message now and start a new one.  */
18063
0
      *p++ = ',';
18064
0
      *p = '\0';
18065
0
      fprintf (stream, "%s\n", message);
18066
0
      p = start;
18067
0
      left = size - (start - message) - len - 2;
18068
18069
0
      gas_assert (left >= 0);
18070
18071
0
      p = mempcpy (p, name, len);
18072
0
    }
18073
18074
0
  *left_p = left;
18075
0
  return p;
18076
0
}
18077
18078
static void
18079
show_arch (FILE *stream, int ext, int check)
18080
0
{
18081
0
  static char message[] = MESSAGE_TEMPLATE;
18082
0
  char *start = message + 27;
18083
0
  char *p;
18084
0
  int size = sizeof (MESSAGE_TEMPLATE);
18085
0
  int left;
18086
0
  const char *name;
18087
0
  int len;
18088
0
  unsigned int j;
18089
18090
0
  p = start;
18091
0
  left = size - (start - message);
18092
18093
0
  if (!ext && check)
18094
0
    {
18095
0
      p = output_message (stream, p, message, start, &left,
18096
0
        STRING_COMMA_LEN ("default"));
18097
0
      p = output_message (stream, p, message, start, &left,
18098
0
        STRING_COMMA_LEN ("push"));
18099
0
      p = output_message (stream, p, message, start, &left,
18100
0
        STRING_COMMA_LEN ("pop"));
18101
0
    }
18102
18103
0
  for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
18104
0
    {
18105
      /* Should it be skipped?  */
18106
0
      if (cpu_arch [j].skip)
18107
0
  continue;
18108
18109
0
      name = cpu_arch [j].name;
18110
0
      len = cpu_arch [j].len;
18111
0
      if (cpu_arch[j].type == PROCESSOR_NONE)
18112
0
  {
18113
    /* It is an extension.  Skip if we aren't asked to show it.  */
18114
0
    if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
18115
0
      continue;
18116
0
  }
18117
0
      else if (ext)
18118
0
  {
18119
    /* It is an processor.  Skip if we show only extension.  */
18120
0
    continue;
18121
0
  }
18122
0
      else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
18123
0
  {
18124
    /* It is an impossible processor - skip.  */
18125
0
    continue;
18126
0
  }
18127
18128
0
      p = output_message (stream, p, message, start, &left, name, len);
18129
0
    }
18130
18131
  /* Display disabled extensions.  */
18132
0
  if (ext)
18133
0
    for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
18134
0
      {
18135
0
  char *str;
18136
18137
0
  if (cpu_arch[j].type != PROCESSOR_NONE
18138
0
      || !cpu_flags_all_zero (&cpu_arch[j].enable))
18139
0
    continue;
18140
0
  str = xasprintf ("no%s", cpu_arch[j].name);
18141
0
  p = output_message (stream, p, message, start, &left, str,
18142
0
          strlen (str));
18143
0
  free (str);
18144
0
      }
18145
18146
0
  *p = '\0';
18147
0
  fprintf (stream, "%s\n", message);
18148
0
}
18149
18150
void
18151
md_show_usage (FILE *stream)
18152
0
{
18153
0
#ifdef OBJ_ELF
18154
0
  fprintf (stream, _("\
18155
0
  -Qy, -Qn                ignored\n\
18156
0
  -V                      print assembler version number\n\
18157
0
  -k                      ignored\n"));
18158
0
#endif
18159
0
  fprintf (stream, _("\
18160
0
  -n                      do not optimize code alignment\n\
18161
0
  -O{012s}                attempt some code optimizations\n\
18162
0
  -q                      quieten some warnings\n"));
18163
0
#ifdef OBJ_ELF
18164
0
  fprintf (stream, _("\
18165
0
  -s                      ignored\n"));
18166
0
#endif
18167
0
#ifdef BFD64
18168
0
# ifdef OBJ_ELF
18169
0
  fprintf (stream, _("\
18170
0
  --32/--64/--x32         generate 32bit/64bit/x32 object\n"));
18171
# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
18172
  fprintf (stream, _("\
18173
  --32/--64               generate 32bit/64bit object\n"));
18174
# endif
18175
0
#endif
18176
#ifdef SVR4_COMMENT_CHARS
18177
  fprintf (stream, _("\
18178
  --divide                do not treat `/' as a comment character\n"));
18179
#else
18180
0
  fprintf (stream, _("\
18181
0
  --divide                ignored\n"));
18182
0
#endif
18183
0
  fprintf (stream, _("\
18184
0
  -march=CPU[,+EXTENSION...]\n\
18185
0
                          generate code for CPU and EXTENSION, CPU is one of:\n"));
18186
0
  show_arch (stream, 0, 1);
18187
0
  fprintf (stream, _("\
18188
0
                          EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
18189
0
  show_arch (stream, 1, 0);
18190
0
  fprintf (stream, _("\
18191
0
  -mtune=CPU              optimize for CPU, CPU is one of:\n"));
18192
0
  show_arch (stream, 0, 0);
18193
0
  fprintf (stream, _("\
18194
0
  -msse2avx               encode SSE instructions with VEX prefix\n"));
18195
0
  fprintf (stream, _("\
18196
0
  -muse-unaligned-vector-move\n\
18197
0
                          encode aligned vector move as unaligned vector move\n"));
18198
0
  fprintf (stream, _("\
18199
0
  -msse-check=[none|error|warning] (default: none)\n\
18200
0
                          check SSE instructions\n"));
18201
0
  fprintf (stream, _("\
18202
0
  -moperand-check=[none|error|warning] (default: warning)\n\
18203
0
                          check operand combinations for validity\n"));
18204
0
  fprintf (stream, _("\
18205
0
  -mavxscalar=[128|256] (default: 128)\n\
18206
0
                          encode scalar AVX instructions with specific vector\n\
18207
0
                           length\n"));
18208
0
  fprintf (stream, _("\
18209
0
  -mvexwig=[0|1] (default: 0)\n\
18210
0
                          encode VEX instructions with specific VEX.W value\n\
18211
0
                           for VEX.W bit ignored instructions\n"));
18212
0
  fprintf (stream, _("\
18213
0
  -mevexlig=[128|256|512] (default: 128)\n\
18214
0
                          encode scalar EVEX instructions with specific vector\n\
18215
0
                           length\n"));
18216
0
  fprintf (stream, _("\
18217
0
  -mevexwig=[0|1] (default: 0)\n\
18218
0
                          encode EVEX instructions with specific EVEX.W value\n\
18219
0
                           for EVEX.W bit ignored instructions\n"));
18220
0
  fprintf (stream, _("\
18221
0
  -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
18222
0
                          encode EVEX instructions with specific EVEX.RC value\n\
18223
0
                           for SAE-only ignored instructions\n"));
18224
0
  fprintf (stream, _("\
18225
0
  -mmnemonic=[att|intel] "));
18226
0
  if (SYSV386_COMPAT)
18227
0
    fprintf (stream, _("(default: att)\n"));
18228
0
  else
18229
0
    fprintf (stream, _("(default: intel)\n"));
18230
0
  fprintf (stream, _("\
18231
0
                          use AT&T/Intel mnemonic (AT&T syntax only)\n"));
18232
0
  fprintf (stream, _("\
18233
0
  -msyntax=[att|intel] (default: att)\n\
18234
0
                          use AT&T/Intel syntax\n"));
18235
0
  fprintf (stream, _("\
18236
0
  -mindex-reg             support pseudo index registers\n"));
18237
0
  fprintf (stream, _("\
18238
0
  -mnaked-reg             don't require `%%' prefix for registers\n"));
18239
0
  fprintf (stream, _("\
18240
0
  -madd-bnd-prefix        add BND prefix for all valid branches\n"));
18241
0
#ifdef OBJ_ELF
18242
0
  fprintf (stream, _("\
18243
0
  -mshared                disable branch optimization for shared code\n"));
18244
0
  fprintf (stream, _("\
18245
0
  -mx86-used-note=[no|yes] "));
18246
0
  if (DEFAULT_X86_USED_NOTE)
18247
0
    fprintf (stream, _("(default: yes)\n"));
18248
0
  else
18249
0
    fprintf (stream, _("(default: no)\n"));
18250
0
  fprintf (stream, _("\
18251
0
                          generate x86 used ISA and feature properties\n"));
18252
0
#endif
18253
#if defined (TE_PE) || defined (TE_PEP)
18254
  fprintf (stream, _("\
18255
  -mbig-obj               generate big object files\n"));
18256
#endif
18257
0
  fprintf (stream, _("\
18258
0
  -momit-lock-prefix=[no|yes] (default: no)\n\
18259
0
                          strip all lock prefixes\n"));
18260
0
  fprintf (stream, _("\
18261
0
  -mfence-as-lock-add=[no|yes] (default: no)\n\
18262
0
                          encode lfence, mfence and sfence as\n\
18263
0
                           lock addl $0x0, (%%{re}sp)\n"));
18264
0
  fprintf (stream, _("\
18265
0
  -mrelax-relocations=[no|yes] "));
18266
0
  if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
18267
0
    fprintf (stream, _("(default: yes)\n"));
18268
0
  else
18269
0
    fprintf (stream, _("(default: no)\n"));
18270
0
  fprintf (stream, _("\
18271
0
                          generate relax relocations\n"));
18272
0
#ifdef OBJ_ELF
18273
0
  fprintf (stream, _("\
18274
0
  -mtls-check=[no|yes] "));
18275
0
  if (DEFAULT_X86_TLS_CHECK)
18276
0
    fprintf (stream, _("(default: yes)\n"));
18277
0
  else
18278
0
    fprintf (stream, _("(default: no)\n"));
18279
0
  fprintf (stream, _("\
18280
0
                          check TLS relocation\n"));
18281
0
#endif
18282
0
  fprintf (stream, _("\
18283
0
  -malign-branch-boundary=NUM (default: 0)\n\
18284
0
                          align branches within NUM byte boundary\n"));
18285
0
  fprintf (stream, _("\
18286
0
  -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
18287
0
                          TYPE is combination of jcc, fused, jmp, call, ret,\n\
18288
0
                           indirect\n\
18289
0
                          specify types of branches to align\n"));
18290
0
  fprintf (stream, _("\
18291
0
  -malign-branch-prefix-size=NUM (default: 5)\n\
18292
0
                          align branches with NUM prefixes per instruction\n"));
18293
0
  fprintf (stream, _("\
18294
0
  -mbranches-within-32B-boundaries\n\
18295
0
                          align branches within 32 byte boundary\n"));
18296
0
  fprintf (stream, _("\
18297
0
  -mlfence-after-load=[no|yes] (default: no)\n\
18298
0
                          generate lfence after load\n"));
18299
0
  fprintf (stream, _("\
18300
0
  -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
18301
0
                          generate lfence before indirect near branch\n"));
18302
0
  fprintf (stream, _("\
18303
0
  -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
18304
0
                          generate lfence before ret\n"));
18305
0
  fprintf (stream, _("\
18306
0
  -mamd64                 accept only AMD64 ISA [default]\n"));
18307
0
  fprintf (stream, _("\
18308
0
  -mintel64               accept only Intel64 ISA\n"));
18309
0
}
18310
18311
#if (defined (OBJ_ELF) || defined (TE_PE) || defined (OBJ_MACH_O))
18312
18313
/* Pick the target format to use.  */
18314
18315
const char *
18316
i386_target_format (void)
18317
281
{
18318
281
  if (startswith (default_arch, "x86_64"))
18319
281
    {
18320
281
      update_code_flag (CODE_64BIT, 1);
18321
281
#ifdef OBJ_ELF
18322
281
      if (default_arch[6] == '\0')
18323
281
  x86_elf_abi = X86_64_ABI;
18324
0
      else
18325
0
  x86_elf_abi = X86_64_X32_ABI;
18326
281
#endif
18327
281
    }
18328
0
  else if (!strcmp (default_arch, "i386"))
18329
0
    update_code_flag (CODE_32BIT, 1);
18330
0
  else if (!strcmp (default_arch, "iamcu"))
18331
0
    {
18332
0
      update_code_flag (CODE_32BIT, 1);
18333
0
      if (cpu_arch_isa == PROCESSOR_UNKNOWN)
18334
0
  {
18335
0
    static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
18336
0
    cpu_arch_name = "iamcu";
18337
0
    free (cpu_sub_arch_name);
18338
0
    cpu_sub_arch_name = NULL;
18339
0
    cpu_arch_flags = iamcu_flags;
18340
0
    cpu_arch_isa = PROCESSOR_IAMCU;
18341
0
    cpu_arch_isa_flags = iamcu_flags;
18342
0
    if (!cpu_arch_tune_set)
18343
0
      cpu_arch_tune = PROCESSOR_IAMCU;
18344
0
  }
18345
0
      else if (cpu_arch_isa != PROCESSOR_IAMCU)
18346
0
  as_fatal (_("Intel MCU doesn't support `%s' architecture"),
18347
0
      cpu_arch_name);
18348
0
    }
18349
0
  else
18350
0
    as_fatal (_("unknown architecture"));
18351
18352
281
#ifdef OBJ_ELF
18353
281
  if (flag_synth_cfi && x86_elf_abi != X86_64_ABI)
18354
0
    as_fatal (_("SCFI is not supported for this ABI"));
18355
281
#endif
18356
18357
281
  if (cpu_flags_all_zero (&cpu_arch_isa_flags))
18358
1
    cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
18359
18360
281
  switch (OUTPUT_FLAVOR)
18361
281
    {
18362
#ifdef TE_PE
18363
    case bfd_target_coff_flavour:
18364
      if (flag_code == CODE_64BIT)
18365
  {
18366
    object_64bit = 1;
18367
    return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
18368
  }
18369
      return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
18370
#endif
18371
0
#ifdef OBJ_ELF
18372
281
    case bfd_target_elf_flavour:
18373
281
      {
18374
281
  const char *format;
18375
18376
281
  switch (x86_elf_abi)
18377
281
    {
18378
0
    default:
18379
0
      format = ELF_TARGET_FORMAT;
18380
0
#ifndef TE_SOLARIS
18381
0
      tls_get_addr = "___tls_get_addr";
18382
0
#endif
18383
0
      break;
18384
281
    case X86_64_ABI:
18385
281
      use_rela_relocations = 1;
18386
281
      object_64bit = 1;
18387
281
#ifndef TE_SOLARIS
18388
281
      tls_get_addr = "__tls_get_addr";
18389
281
#endif
18390
281
      format = ELF_TARGET_FORMAT64;
18391
281
      break;
18392
0
    case X86_64_X32_ABI:
18393
0
      use_rela_relocations = 1;
18394
0
      object_64bit = 1;
18395
0
#ifndef TE_SOLARIS
18396
0
      tls_get_addr = "__tls_get_addr";
18397
0
#endif
18398
0
      disallow_64bit_reloc = 1;
18399
0
      format = ELF_TARGET_FORMAT32;
18400
0
      break;
18401
281
    }
18402
281
  if (cpu_arch_isa == PROCESSOR_IAMCU)
18403
0
    {
18404
0
      if (x86_elf_abi != I386_ABI)
18405
0
        as_fatal (_("Intel MCU is 32bit only"));
18406
0
      return ELF_TARGET_IAMCU_FORMAT;
18407
0
    }
18408
281
  else
18409
281
    return format;
18410
281
      }
18411
0
#endif
18412
#if defined (OBJ_MACH_O)
18413
    case bfd_target_mach_o_flavour:
18414
      if (flag_code == CODE_64BIT)
18415
  {
18416
    use_rela_relocations = 1;
18417
    object_64bit = 1;
18418
    return "mach-o-x86-64";
18419
  }
18420
      else
18421
  return "mach-o-i386";
18422
#endif
18423
0
    default:
18424
0
      abort ();
18425
0
      return NULL;
18426
281
    }
18427
281
}
18428
18429
#endif /* ELF / PE / MACH_O  */
18430

18431
#ifdef OBJ_ELF
18432
symbolS *
18433
md_undefined_symbol (char *name)
18434
2.59k
{
18435
2.59k
  if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
18436
27
      && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
18437
11
      && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
18438
11
      && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
18439
11
    {
18440
11
      if (!GOT_symbol)
18441
11
  {
18442
11
    if (symbol_find (name))
18443
0
      as_bad (_("GOT already in symbol table"));
18444
11
    GOT_symbol = symbol_new (name, undefined_section,
18445
11
           &zero_address_frag, 0);
18446
11
  };
18447
11
      return GOT_symbol;
18448
11
    }
18449
2.58k
  return NULL;
18450
2.59k
}
18451
#endif
18452
18453
#ifdef OBJ_AOUT
18454
/* Round up a section size to the appropriate boundary.  */
18455
18456
valueT
18457
md_section_align (segT segment, valueT size)
18458
{
18459
  /* For a.out, force the section size to be aligned.  If we don't do
18460
     this, BFD will align it for us, but it will not write out the
18461
     final bytes of the section.  This may be a bug in BFD, but it is
18462
     easier to fix it here since that is how the other a.out targets
18463
     work.  */
18464
  int align = bfd_section_alignment (segment);
18465
18466
  return (size + ((valueT) 1 << align) - 1) & -((valueT) 1 << align);
18467
}
18468
#endif
18469
18470
/* On the i386, PC-relative offsets are relative to the start of the
18471
   next instruction.  That is, the address of the offset, plus its
18472
   size, since the offset is always the last part of the insn.  */
18473
18474
long
18475
md_pcrel_from (fixS *fixP)
18476
0
{
18477
0
  return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
18478
0
}
18479
18480
#ifdef OBJ_AOUT
18481
18482
static void
18483
s_bss (int ignore ATTRIBUTE_UNUSED)
18484
{
18485
  int temp;
18486
18487
  temp = get_absolute_expression ();
18488
  subseg_set (bss_section, temp);
18489
  demand_empty_rest_of_line ();
18490
}
18491
18492
#endif
18493
18494
/* Remember constant directive.  */
18495
18496
void
18497
i386_cons_align (int ignore ATTRIBUTE_UNUSED)
18498
3.39k
{
18499
3.39k
  struct last_insn *last_insn
18500
3.39k
    = &seg_info(now_seg)->tc_segment_info_data.last_insn;
18501
18502
3.39k
  if (bfd_section_flags (now_seg) & SEC_CODE)
18503
2.25k
    {
18504
2.25k
      last_insn->kind = last_insn_directive;
18505
2.25k
      last_insn->name = "constant directive";
18506
2.25k
      last_insn->file = as_where (&last_insn->line);
18507
2.25k
    }
18508
3.39k
}
18509
18510
int
18511
i386_validate_fix (fixS *fixp)
18512
0
{
18513
0
  if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
18514
0
    {
18515
0
      reloc_howto_type *howto;
18516
18517
0
      howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
18518
0
      as_bad_where (fixp->fx_file, fixp->fx_line,
18519
0
        _("invalid %s relocation against register"),
18520
0
        howto ? howto->name : "<unknown>");
18521
0
      return 0;
18522
0
    }
18523
18524
0
#ifdef OBJ_ELF
18525
0
  if (fixp->fx_r_type == BFD_RELOC_SIZE32
18526
0
      || fixp->fx_r_type == BFD_RELOC_SIZE64)
18527
0
    return fixp->fx_addsy
18528
0
     && (!S_IS_DEFINED (fixp->fx_addsy)
18529
0
         || S_IS_EXTERNAL (fixp->fx_addsy));
18530
18531
  /* BFD_RELOC_X86_64_GOTTPOFF:
18532
      1. fx_tcbit -> BFD_RELOC_X86_64_CODE_4_GOTTPOFF
18533
      2. fx_tcbit2 -> BFD_RELOC_X86_64_CODE_5_GOTTPOFF
18534
      3. fx_tcbit3 -> BFD_RELOC_X86_64_CODE_6_GOTTPOFF
18535
    BFD_RELOC_X86_64_GOTPC32_TLSDESC:
18536
      1. fx_tcbit -> BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC
18537
    BFD_RELOC_32_PCREL:
18538
      1. fx_tcbit && fx_tcbit3 -> BFD_RELOC_X86_64_CODE_5_GOTPCRELX
18539
      2. fx_tcbit -> BFD_RELOC_X86_64_GOTPCRELX
18540
      3. fx_tcbit2 && fx_tcbit3 -> BFD_RELOC_X86_64_CODE_6_GOTPCRELX
18541
      4. fx_tcbit2 -> BFD_RELOC_X86_64_REX_GOTPCRELX
18542
      5. fx_tcbit3 -> BFD_RELOC_X86_64_CODE_4_GOTPCRELX
18543
      6. else -> BFD_RELOC_X86_64_GOTPCREL
18544
   */
18545
0
  if (fixp->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF)
18546
0
    {
18547
0
      if (fixp->fx_tcbit)
18548
0
  fixp->fx_r_type = BFD_RELOC_X86_64_CODE_4_GOTTPOFF;
18549
0
      else if (fixp->fx_tcbit2)
18550
0
  fixp->fx_r_type = BFD_RELOC_X86_64_CODE_5_GOTTPOFF;
18551
0
      else if (fixp->fx_tcbit3)
18552
0
  fixp->fx_r_type = BFD_RELOC_X86_64_CODE_6_GOTTPOFF;
18553
0
    }
18554
0
  else if (fixp->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
18555
0
     && fixp->fx_tcbit)
18556
0
    fixp->fx_r_type = BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC;
18557
0
#endif
18558
18559
0
  if (fixp->fx_subsy)
18560
0
    {
18561
0
      if (fixp->fx_subsy == GOT_symbol)
18562
0
  {
18563
0
    if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
18564
0
      {
18565
0
        if (!object_64bit)
18566
0
    abort ();
18567
0
#ifdef OBJ_ELF
18568
0
        if (fixp->fx_tcbit)
18569
0
    fixp->fx_r_type = fixp->fx_tcbit3
18570
0
          ? BFD_RELOC_X86_64_CODE_5_GOTPCRELX
18571
0
          : BFD_RELOC_X86_64_GOTPCRELX;
18572
0
        else if (fixp->fx_tcbit2)
18573
0
    fixp->fx_r_type = fixp->fx_tcbit3
18574
0
          ? BFD_RELOC_X86_64_CODE_6_GOTPCRELX
18575
0
          : BFD_RELOC_X86_64_REX_GOTPCRELX;
18576
0
        else if (fixp->fx_tcbit3)
18577
0
    fixp->fx_r_type = BFD_RELOC_X86_64_CODE_4_GOTPCRELX;
18578
0
        else
18579
0
#endif
18580
0
    fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
18581
0
      }
18582
0
    else
18583
0
      {
18584
0
        if (!object_64bit)
18585
0
    fixp->fx_r_type = BFD_RELOC_32_GOTOFF;
18586
0
        else
18587
0
    fixp->fx_r_type = BFD_RELOC_64_GOTOFF;
18588
0
      }
18589
0
    fixp->fx_subsy = 0;
18590
0
  }
18591
0
    }
18592
0
#ifdef OBJ_ELF
18593
0
  else
18594
0
    {
18595
      /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
18596
   to section.  Since PLT32 relocation must be against symbols,
18597
   turn such PLT32 relocation into PC32 relocation.  NB: We can
18598
   turn PLT32 relocation into PC32 relocation only for PC-relative
18599
   relocations since non-PC-relative relocations need PLT entries.
18600
       */
18601
0
      if (fixp->fx_addsy
18602
0
    && fixp->fx_pcrel
18603
0
    && (fixp->fx_r_type == BFD_RELOC_386_PLT32
18604
0
        || fixp->fx_r_type == BFD_RELOC_32_PLT_PCREL)
18605
0
    && symbol_section_p (fixp->fx_addsy))
18606
0
  fixp->fx_r_type = BFD_RELOC_32_PCREL;
18607
0
      if (!object_64bit)
18608
0
  {
18609
0
    if (fixp->fx_r_type == BFD_RELOC_386_GOT32
18610
0
        && fixp->fx_tcbit2)
18611
0
      fixp->fx_r_type = BFD_RELOC_386_GOT32X;
18612
0
  }
18613
0
    }
18614
0
#endif
18615
18616
0
  return 1;
18617
0
}
18618
18619
arelent *
18620
tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
18621
0
{
18622
0
  arelent *rel;
18623
0
  bfd_reloc_code_real_type code;
18624
18625
0
  switch (fixp->fx_r_type)
18626
0
    {
18627
0
#ifdef OBJ_ELF
18628
0
      symbolS *sym;
18629
18630
0
    case BFD_RELOC_SIZE32:
18631
0
    case BFD_RELOC_SIZE64:
18632
0
      if (fixp->fx_addsy
18633
0
    && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
18634
0
    && (!fixp->fx_subsy
18635
0
        || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
18636
0
  sym = fixp->fx_addsy;
18637
0
      else if (fixp->fx_subsy
18638
0
         && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
18639
0
         && (!fixp->fx_addsy
18640
0
       || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
18641
0
  sym = fixp->fx_subsy;
18642
0
      else
18643
0
  sym = NULL;
18644
0
      if (sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
18645
0
  {
18646
    /* Resolve size relocation against local symbol to size of
18647
       the symbol plus addend.  */
18648
0
    valueT value = S_GET_SIZE (sym);
18649
18650
0
    if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
18651
0
      value = bfd_section_size (S_GET_SEGMENT (sym));
18652
0
    if (sym == fixp->fx_subsy)
18653
0
      {
18654
0
        value = -value;
18655
0
        if (fixp->fx_addsy)
18656
0
          value += S_GET_VALUE (fixp->fx_addsy);
18657
0
      }
18658
0
    else if (fixp->fx_subsy)
18659
0
      value -= S_GET_VALUE (fixp->fx_subsy);
18660
0
    value += fixp->fx_offset;
18661
0
    if (fixp->fx_r_type == BFD_RELOC_SIZE32
18662
0
        && object_64bit
18663
0
        && !fits_in_unsigned_long (value))
18664
0
      as_bad_where (fixp->fx_file, fixp->fx_line,
18665
0
        _("symbol size computation overflow"));
18666
0
    fixp->fx_addsy = NULL;
18667
0
    fixp->fx_subsy = NULL;
18668
0
    md_apply_fix (fixp, &value, NULL);
18669
0
    return NULL;
18670
0
  }
18671
0
      if (!fixp->fx_addsy || fixp->fx_subsy)
18672
0
  {
18673
0
    as_bad_where (fixp->fx_file, fixp->fx_line,
18674
0
      "unsupported expression involving @size");
18675
0
    return NULL;
18676
0
  }
18677
0
#endif
18678
      /* Fall through.  */
18679
18680
0
    case BFD_RELOC_32_PLT_PCREL:
18681
0
    case BFD_RELOC_X86_64_GOT32:
18682
0
    case BFD_RELOC_X86_64_GOTPCREL:
18683
0
    case BFD_RELOC_X86_64_GOTPCRELX:
18684
0
    case BFD_RELOC_X86_64_REX_GOTPCRELX:
18685
0
    case BFD_RELOC_X86_64_CODE_4_GOTPCRELX:
18686
0
    case BFD_RELOC_X86_64_CODE_5_GOTPCRELX:
18687
0
    case BFD_RELOC_X86_64_CODE_6_GOTPCRELX:
18688
0
    case BFD_RELOC_386_PLT32:
18689
0
    case BFD_RELOC_386_GOT32:
18690
0
    case BFD_RELOC_386_GOT32X:
18691
0
    case BFD_RELOC_32_GOTOFF:
18692
0
    case BFD_RELOC_32_GOT_PCREL:
18693
0
    case BFD_RELOC_386_TLS_GD:
18694
0
    case BFD_RELOC_386_TLS_LDM:
18695
0
    case BFD_RELOC_386_TLS_LDO_32:
18696
0
    case BFD_RELOC_386_TLS_IE_32:
18697
0
    case BFD_RELOC_386_TLS_IE:
18698
0
    case BFD_RELOC_386_TLS_GOTIE:
18699
0
    case BFD_RELOC_386_TLS_LE_32:
18700
0
    case BFD_RELOC_386_TLS_LE:
18701
0
    case BFD_RELOC_386_TLS_GOTDESC:
18702
0
    case BFD_RELOC_386_TLS_DESC_CALL:
18703
0
    case BFD_RELOC_386_PC32_TO_PLT32:
18704
0
    case BFD_RELOC_X86_64_TLSGD:
18705
0
    case BFD_RELOC_X86_64_TLSLD:
18706
0
    case BFD_RELOC_X86_64_DTPOFF32:
18707
0
    case BFD_RELOC_X86_64_DTPOFF64:
18708
0
    case BFD_RELOC_X86_64_GOTTPOFF:
18709
0
    case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
18710
0
    case BFD_RELOC_X86_64_CODE_5_GOTTPOFF:
18711
0
    case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
18712
0
    case BFD_RELOC_X86_64_TPOFF32:
18713
0
    case BFD_RELOC_X86_64_TPOFF64:
18714
0
    case BFD_RELOC_64_GOTOFF:
18715
0
    case BFD_RELOC_X86_64_GOTPC32:
18716
0
    case BFD_RELOC_X86_64_GOT64:
18717
0
    case BFD_RELOC_X86_64_GOTPCREL64:
18718
0
    case BFD_RELOC_64_GOT_PCREL:
18719
0
    case BFD_RELOC_X86_64_GOTPLT64:
18720
0
    case BFD_RELOC_64_PLTOFF:
18721
0
    case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
18722
0
    case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
18723
0
    case BFD_RELOC_X86_64_CODE_5_GOTPC32_TLSDESC:
18724
0
    case BFD_RELOC_X86_64_CODE_6_GOTPC32_TLSDESC:
18725
0
    case BFD_RELOC_X86_64_TLSDESC_CALL:
18726
0
    case BFD_RELOC_X86_64_PC32_TO_PLT32:
18727
0
    case BFD_RELOC_RVA:
18728
0
    case BFD_RELOC_VTABLE_ENTRY:
18729
0
    case BFD_RELOC_VTABLE_INHERIT:
18730
#ifdef TE_PE
18731
    case BFD_RELOC_32_SECREL:
18732
    case BFD_RELOC_16_SECIDX:
18733
#endif
18734
0
      code = fixp->fx_r_type;
18735
0
      break;
18736
0
    case BFD_RELOC_X86_64_32S:
18737
0
      if (!fixp->fx_pcrel)
18738
0
  {
18739
    /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32.  */
18740
0
    code = fixp->fx_r_type;
18741
0
    break;
18742
0
  }
18743
      /* Fall through.  */
18744
0
    default:
18745
0
      if (fixp->fx_pcrel)
18746
0
  {
18747
0
    switch (fixp->fx_size)
18748
0
      {
18749
0
      default:
18750
0
        as_bad_where (fixp->fx_file, fixp->fx_line,
18751
0
          _("can not do %d byte pc-relative relocation"),
18752
0
          fixp->fx_size);
18753
0
        code = BFD_RELOC_32_PCREL;
18754
0
        break;
18755
0
      case 1: code = BFD_RELOC_8_PCREL;  break;
18756
0
      case 2: code = BFD_RELOC_16_PCREL; break;
18757
0
      case 4: code = BFD_RELOC_32_PCREL; break;
18758
0
#ifdef BFD64
18759
0
      case 8: code = BFD_RELOC_64_PCREL; break;
18760
0
#endif
18761
0
      }
18762
0
  }
18763
0
      else
18764
0
  {
18765
0
    switch (fixp->fx_size)
18766
0
      {
18767
0
      default:
18768
0
        as_bad_where (fixp->fx_file, fixp->fx_line,
18769
0
          _("can not do %d byte relocation"),
18770
0
          fixp->fx_size);
18771
0
        code = BFD_RELOC_32;
18772
0
        break;
18773
0
      case 1: code = BFD_RELOC_8;  break;
18774
0
      case 2: code = BFD_RELOC_16; break;
18775
0
      case 4: code = BFD_RELOC_32; break;
18776
0
#ifdef BFD64
18777
0
      case 8: code = BFD_RELOC_64; break;
18778
0
#endif
18779
0
      }
18780
0
  }
18781
0
      break;
18782
0
    }
18783
18784
0
  if ((code == BFD_RELOC_32
18785
0
       || code == BFD_RELOC_32_PCREL
18786
0
       || code == BFD_RELOC_X86_64_32S)
18787
0
      && GOT_symbol
18788
0
      && fixp->fx_addsy == GOT_symbol)
18789
0
    {
18790
0
      if (!object_64bit)
18791
0
  code = BFD_RELOC_32_GOT_PCREL;
18792
0
      else
18793
0
  code = BFD_RELOC_X86_64_GOTPC32;
18794
0
    }
18795
0
  if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
18796
0
      && GOT_symbol
18797
0
      && fixp->fx_addsy == GOT_symbol)
18798
0
    {
18799
0
      code = BFD_RELOC_64_GOT_PCREL;
18800
0
    }
18801
18802
0
  rel = notes_alloc (sizeof (arelent));
18803
0
  rel->sym_ptr_ptr = notes_alloc (sizeof (asymbol *));
18804
0
  *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
18805
18806
0
  rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
18807
18808
0
  if (!use_rela_relocations)
18809
0
    {
18810
      /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
18811
   vtable entry to be used in the relocation's section offset.  */
18812
0
      if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18813
0
  rel->address = fixp->fx_offset;
18814
#if defined (OBJ_COFF) && defined (TE_PE)
18815
      else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
18816
  rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
18817
      else
18818
#endif
18819
0
      rel->addend = 0;
18820
0
    }
18821
  /* Use the rela in 64bit mode.  */
18822
0
  else
18823
0
    {
18824
0
      if (disallow_64bit_reloc)
18825
0
  switch (code)
18826
0
    {
18827
0
    case BFD_RELOC_X86_64_DTPOFF64:
18828
0
    case BFD_RELOC_X86_64_TPOFF64:
18829
0
    case BFD_RELOC_64_PCREL:
18830
0
    case BFD_RELOC_64_GOTOFF:
18831
0
    case BFD_RELOC_X86_64_GOT64:
18832
0
    case BFD_RELOC_X86_64_GOTPCREL64:
18833
0
    case BFD_RELOC_64_GOT_PCREL:
18834
0
    case BFD_RELOC_X86_64_GOTPLT64:
18835
0
    case BFD_RELOC_64_PLTOFF:
18836
0
      as_bad_where (fixp->fx_file, fixp->fx_line,
18837
0
        _("cannot represent relocation type %s in x32 mode"),
18838
0
        bfd_get_reloc_code_name (code));
18839
0
      break;
18840
0
    default:
18841
0
      break;
18842
0
    }
18843
18844
0
      if (!fixp->fx_pcrel)
18845
0
  rel->addend = fixp->fx_offset;
18846
0
      else
18847
0
  switch (code)
18848
0
    {
18849
0
    case BFD_RELOC_32_PLT_PCREL:
18850
0
    case BFD_RELOC_X86_64_GOT32:
18851
0
    case BFD_RELOC_X86_64_GOTPCREL:
18852
0
    case BFD_RELOC_X86_64_GOTPCRELX:
18853
0
    case BFD_RELOC_X86_64_REX_GOTPCRELX:
18854
0
    case BFD_RELOC_X86_64_CODE_4_GOTPCRELX:
18855
0
    case BFD_RELOC_X86_64_CODE_5_GOTPCRELX:
18856
0
    case BFD_RELOC_X86_64_CODE_6_GOTPCRELX:
18857
0
    case BFD_RELOC_X86_64_TLSGD:
18858
0
    case BFD_RELOC_X86_64_TLSLD:
18859
0
    case BFD_RELOC_X86_64_GOTTPOFF:
18860
0
    case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
18861
0
    case BFD_RELOC_X86_64_CODE_5_GOTTPOFF:
18862
0
    case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
18863
0
    case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
18864
0
    case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
18865
0
    case BFD_RELOC_X86_64_CODE_5_GOTPC32_TLSDESC:
18866
0
    case BFD_RELOC_X86_64_CODE_6_GOTPC32_TLSDESC:
18867
0
    case BFD_RELOC_X86_64_TLSDESC_CALL:
18868
0
      rel->addend = fixp->fx_offset - fixp->fx_size;
18869
0
      break;
18870
0
    case BFD_RELOC_X86_64_PC32_TO_PLT32:
18871
      /* This came from a directive like ".long foo@PLT - .L4".
18872
         Generate R_X86_64_PLT32 with addend computed like
18873
         R_X86_64_PC32 so that PLT entry is used to resolve
18874
         this PC32 relocation.   */
18875
0
      code = BFD_RELOC_32_PLT_PCREL;
18876
      /* Fall through.  */
18877
0
    default:
18878
0
      rel->addend = (section->vma
18879
0
         - fixp->fx_size
18880
0
         + fixp->fx_addnumber
18881
0
         + md_pcrel_from (fixp));
18882
0
      break;
18883
0
    }
18884
0
    }
18885
18886
0
  rel->howto = bfd_reloc_type_lookup (stdoutput, code);
18887
0
  if (rel->howto == NULL)
18888
0
    {
18889
0
      as_bad_where (fixp->fx_file, fixp->fx_line,
18890
0
        _("cannot represent relocation type %s"),
18891
0
        bfd_get_reloc_code_name (code));
18892
      /* Set howto to a garbage value so that we can keep going.  */
18893
0
      rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
18894
0
      gas_assert (rel->howto != NULL);
18895
0
    }
18896
18897
0
  return rel;
18898
0
}
18899
18900
#include "tc-i386-intel.c"
18901
18902
void
18903
tc_x86_parse_to_dw2regnum (expressionS *exp)
18904
492
{
18905
492
  int saved_naked_reg;
18906
492
  char saved_register_dot;
18907
18908
492
  saved_naked_reg = allow_naked_reg;
18909
492
  allow_naked_reg = 1;
18910
492
  saved_register_dot = register_chars['.'];
18911
492
  register_chars['.'] = '.';
18912
492
  allow_pseudo_reg = 1;
18913
492
  expression_and_evaluate (exp);
18914
492
  allow_pseudo_reg = 0;
18915
492
  register_chars['.'] = saved_register_dot;
18916
492
  allow_naked_reg = saved_naked_reg;
18917
18918
492
  if (exp->X_op == O_register && exp->X_add_number >= 0)
18919
142
    {
18920
142
      exp->X_op = O_illegal;
18921
142
      if ((addressT) exp->X_add_number < i386_regtab_size)
18922
142
  {
18923
142
    exp->X_add_number = i386_regtab[exp->X_add_number]
18924
142
            .dw2_regnum[object_64bit];
18925
142
    if (exp->X_add_number != Dw2Inval)
18926
0
      exp->X_op = O_constant;
18927
142
  }
18928
142
    }
18929
492
}
18930
18931
void
18932
tc_x86_frame_initial_instructions (void)
18933
347
{
18934
347
  cfi_add_CFA_def_cfa (object_64bit ? REG_SP : 4, -x86_cie_data_alignment);
18935
347
  cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
18936
347
}
18937
18938
int
18939
x86_dwarf2_addr_size (void)
18940
62
{
18941
62
#ifdef OBJ_ELF
18942
62
  if (x86_elf_abi == X86_64_X32_ABI)
18943
0
    return 4;
18944
62
#endif
18945
62
  return bfd_arch_bits_per_address (stdoutput) / 8;
18946
62
}
18947
18948
#ifdef TE_PE
18949
void
18950
tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
18951
{
18952
  expressionS exp;
18953
18954
  exp.X_op = O_secrel;
18955
  exp.X_add_symbol = symbol;
18956
  exp.X_add_number = 0;
18957
  emit_expr (&exp, size);
18958
}
18959
#endif
18960
18961
#ifdef OBJ_ELF
18962
int
18963
i386_elf_section_type (const char *str, size_t len)
18964
24
{
18965
24
  if (flag_code == CODE_64BIT
18966
20
      && len == sizeof ("unwind") - 1
18967
9
      && startswith (str, "unwind"))
18968
9
    return SHT_X86_64_UNWIND;
18969
18970
15
  return -1;
18971
24
}
18972
18973
void
18974
i386_elf_section_change_hook (void)
18975
32.2k
{
18976
32.2k
  struct i386_segment_info *info = &seg_info(now_seg)->tc_segment_info_data;
18977
32.2k
  struct i386_segment_info *curr, *prev;
18978
18979
32.2k
  if (info->subseg == now_subseg)
18980
32.2k
    return;
18981
18982
  /* Find the (or make a) list entry to save state into.  */
18983
32
  for (prev = info; (curr = prev->next) != NULL; prev = curr)
18984
19
    if (curr->subseg == info->subseg)
18985
2
      break;
18986
15
  if (!curr)
18987
13
    {
18988
13
      curr = notes_alloc (sizeof (*curr));
18989
13
      curr->subseg = info->subseg;
18990
13
      curr->next = NULL;
18991
13
      prev->next = curr;
18992
13
    }
18993
15
  curr->last_insn = info->last_insn;
18994
18995
  /* Find the list entry to load state from.  */
18996
43
  for (curr = info->next; curr; curr = curr->next)
18997
31
    if (curr->subseg == now_subseg)
18998
3
      break;
18999
15
  if (curr)
19000
3
    info->last_insn = curr->last_insn;
19001
12
  else
19002
12
    memset (&info->last_insn, 0, sizeof (info->last_insn));
19003
15
  info->subseg = now_subseg;
19004
15
}
19005
19006
#ifdef TE_SOLARIS
19007
void
19008
i386_solaris_fix_up_eh_frame (segT sec)
19009
{
19010
  if (flag_code == CODE_64BIT)
19011
    elf_section_type (sec) = SHT_X86_64_UNWIND;
19012
}
19013
#endif
19014
19015
/* For ELF on x86-64, add support for SHF_X86_64_LARGE.  */
19016
19017
bfd_vma
19018
x86_64_section_letter (int letter, const char **extra)
19019
116k
{
19020
116k
  if (flag_code == CODE_64BIT)
19021
116k
    {
19022
116k
      if (letter == 'l')
19023
0
  return SHF_X86_64_LARGE;
19024
19025
116k
      *extra = "l";
19026
116k
    }
19027
116k
  return -1;
19028
116k
}
19029
19030
static void
19031
handle_large_common (int small ATTRIBUTE_UNUSED)
19032
0
{
19033
0
  if (flag_code != CODE_64BIT)
19034
0
    {
19035
0
      s_comm_internal (0, elf_common_parse);
19036
0
      as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
19037
0
    }
19038
0
  else
19039
0
    {
19040
0
      static segT lbss_section;
19041
0
      asection *saved_com_section_ptr = elf_com_section_ptr;
19042
0
      asection *saved_bss_section = bss_section;
19043
19044
0
      if (lbss_section == NULL)
19045
0
  {
19046
0
    flagword applicable;
19047
0
    segT seg = now_seg;
19048
0
    subsegT subseg = now_subseg;
19049
19050
    /* The .lbss section is for local .largecomm symbols.  */
19051
0
    lbss_section = subseg_new (".lbss", 0);
19052
0
    applicable = bfd_applicable_section_flags (stdoutput);
19053
0
    bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
19054
0
    seg_info (lbss_section)->bss = 1;
19055
19056
0
    subseg_set (seg, subseg);
19057
0
  }
19058
19059
0
      elf_com_section_ptr = &bfd_elf_large_com_section;
19060
0
      bss_section = lbss_section;
19061
19062
0
      s_comm_internal (0, elf_common_parse);
19063
19064
0
      elf_com_section_ptr = saved_com_section_ptr;
19065
0
      bss_section = saved_bss_section;
19066
0
    }
19067
0
}
19068
#endif /* OBJ_ELF */