Coverage Report

Created: 2026-05-11 07:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/gas/config/tc-i386.c
Line
Count
Source
1
/* tc-i386.c -- Assemble code for the Intel 80386
2
   Copyright (C) 1989-2026 Free Software Foundation, Inc.
3
4
   This file is part of GAS, the GNU Assembler.
5
6
   GAS is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3, or (at your option)
9
   any later version.
10
11
   GAS is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with GAS; see the file COPYING.  If not, write to the Free
18
   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19
   02110-1301, USA.  */
20
21
/* Intel 80386 machine specific gas.
22
   Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
23
   x86_64 support by Jan Hubicka (jh@suse.cz)
24
   VIA PadLock support by Michal Ludvig (mludvig@suse.cz)
25
   Bugs & suggestions are completely welcome.  This is free software.
26
   Please help us make it better.  */
27
28
#include "as.h"
29
#include "safe-ctype.h"
30
#include "subsegs.h"
31
#include "dwarf2dbg.h"
32
#include "dw2gencfi.h"
33
#include "scfi.h"
34
#include "gen-sframe.h"
35
#include "sframe.h"
36
#include "elf/x86-64.h"
37
#include "opcodes/i386-init.h"
38
#include "opcodes/i386-mnem.h"
39
#include <limits.h>
40
41
#ifndef INFER_ADDR_PREFIX
42
#define INFER_ADDR_PREFIX 1
43
#endif
44
45
#ifndef DEFAULT_ARCH
46
#define DEFAULT_ARCH "i386"
47
#endif
48
49
#ifndef INLINE
50
#if __GNUC__ >= 2
51
#define INLINE __inline__
52
#else
53
#define INLINE
54
#endif
55
#endif
56
57
/* Prefixes will be emitted in the order defined below.
58
   WAIT_PREFIX must be the first prefix since FWAIT is really is an
59
   instruction, and so must come before any prefixes.
60
   The preferred prefix order is SEG_PREFIX, ADDR_PREFIX, DATA_PREFIX,
61
   REP_PREFIX/HLE_PREFIX, LOCK_PREFIX.  */
62
0
#define WAIT_PREFIX 0
63
10.1k
#define SEG_PREFIX  1
64
75.7k
#define ADDR_PREFIX 2
65
5.11k
#define DATA_PREFIX 3
66
274
#define REP_PREFIX  4
67
0
#define HLE_PREFIX  REP_PREFIX
68
256
#define BND_PREFIX  REP_PREFIX
69
11.4k
#define LOCK_PREFIX 5
70
16.6k
#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
163k
#define REGISTER_PREFIX '%'
75
13.7k
#define IMMEDIATE_PREFIX '$'
76
28.6k
#define ABSOLUTE_PREFIX '*'
77
78
/* these are the instruction mnemonic suffixes in AT&T syntax or
79
   memory operand size in Intel syntax.  */
80
14.5k
#define WORD_MNEM_SUFFIX  'w'
81
21.0k
#define BYTE_MNEM_SUFFIX  'b'
82
23.7k
#define SHORT_MNEM_SUFFIX 's'
83
59.0k
#define LONG_MNEM_SUFFIX  'l'
84
126k
#define QWORD_MNEM_SUFFIX  'q'
85
86
1.00M
#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
422
#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 *, bool);
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
    /* SUFFIX holds the instruction size suffix for byte, word, dword
310
       or qword, if given.  */
311
    char suffix;
312
313
    /* OPCODE_LENGTH holds the number of base opcode bytes.  */
314
    unsigned char opcode_length;
315
316
    /* OPERANDS gives the number of given operands.  */
317
    unsigned int operands;
318
319
    /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
320
       of given register, displacement, memory operands and immediate
321
       operands.  */
322
    unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
323
324
    /* TYPES [i] is the type (see above #defines) which tells us how to
325
       use OP[i] for the corresponding operand.  */
326
    i386_operand_type types[MAX_OPERANDS];
327
328
    /* Displacement expression, immediate expression, or register for each
329
       operand.  */
330
    union i386_op op[MAX_OPERANDS];
331
332
    /* Flags for operands.  */
333
    unsigned int flags[MAX_OPERANDS];
334
8.77k
#define Operand_PCrel 1
335
37.5k
#define Operand_Mem   2
336
0
#define Operand_Signed 4 /* .insn only */
337
338
    /* Relocation type for operand */
339
    enum bfd_reloc_code_real reloc[MAX_OPERANDS];
340
341
    /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
342
       the base index byte below.  */
343
    const reg_entry *base_reg;
344
    const reg_entry *index_reg;
345
    unsigned int log2_scale_factor;
346
347
    /* SEG gives the seg_entries of this insn.  They are zero unless
348
       explicit segment overrides are given.  */
349
    const reg_entry *seg[2];
350
351
    /* PREFIX holds all the given prefix opcodes (usually null).
352
       PREFIXES is the number of prefix opcodes.  */
353
    unsigned int prefixes;
354
    unsigned char prefix[MAX_PREFIXES];
355
356
    /* .insn allows for reserved opcode spaces.  */
357
    unsigned char insn_opcode_space;
358
359
    /* .insn also allows (requires) specifying immediate size.  */
360
    unsigned char imm_bits[MAX_OPERANDS];
361
362
    /* Register is in low 3 bits of opcode.  */
363
    bool short_form;
364
365
    /* The operand to a branch insn indicates an absolute branch.  */
366
    bool jumpabsolute;
367
368
    /* The operand to a branch insn indicates a far branch.  */
369
    bool far_branch;
370
371
    /* There is a memory operand of (%dx) which should be only used
372
       with input/output instructions.  */
373
    bool input_output_operand;
374
375
    /* Extended states.  */
376
    enum
377
      {
378
  /* Use MMX state.  */
379
  xstate_mmx = 1 << 0,
380
  /* Use XMM state.  */
381
  xstate_xmm = 1 << 1,
382
  /* Use YMM state.  */
383
  xstate_ymm = 1 << 2 | xstate_xmm,
384
  /* Use ZMM state.  */
385
  xstate_zmm = 1 << 3 | xstate_ymm,
386
  /* Use TMM state.  */
387
  xstate_tmm = 1 << 4,
388
  /* Use MASK state.  */
389
  xstate_mask = 1 << 5
390
      } xstate;
391
392
    /* Has GOTPC or TLS relocation.  */
393
    bool has_gotpc_tls_reloc;
394
395
    /* Has relocation entry from the gotrel array.  */
396
    bool has_gotrel;
397
398
    /* RM and SIB are the modrm byte and the sib byte where the
399
       addressing modes of this insn are encoded.  */
400
    modrm_byte rm;
401
    rex_byte rex;
402
    rex_byte vrex;
403
    rex_byte rex2;
404
    sib_byte sib;
405
    vex_prefix vex;
406
407
    /* Masking attributes.
408
409
       The struct describes masking, applied to OPERAND in the instruction.
410
       REG is a pointer to the corresponding mask register.  ZEROING tells
411
       whether merging or zeroing mask is used.  */
412
    struct Mask_Operation
413
    {
414
      const reg_entry *reg;
415
      unsigned int zeroing;
416
      /* The operand where this operation is associated.  */
417
      unsigned int operand;
418
    } mask;
419
420
    /* Rounding control and SAE attributes.  */
421
    struct RC_Operation
422
    {
423
      enum rc_type
424
  {
425
    rc_none = -1,
426
    rne,
427
    rd,
428
    ru,
429
    rz,
430
    saeonly
431
  } type;
432
      /* In Intel syntax the operand modifier form is supposed to be used, but
433
   we continue to accept the immediate forms as well.  */
434
      bool modifier;
435
    } rounding;
436
437
    /* Broadcasting attributes.
438
439
       The struct describes broadcasting, applied to OPERAND.  TYPE is
440
       expresses the broadcast factor.  */
441
    struct Broadcast_Operation
442
    {
443
      /* Type of broadcast: {1to2}, {1to4}, {1to8}, {1to16} or {1to32}.  */
444
      unsigned int type;
445
446
      /* Index of broadcasted operand.  */
447
      unsigned int operand;
448
449
      /* Number of bytes to broadcast.  */
450
      unsigned int bytes;
451
    } broadcast;
452
453
    /* Compressed disp8*N attribute.  */
454
    unsigned int memshift;
455
456
    /* SCC = EVEX.[SC3,SC2,SC1,SC0].  */
457
    unsigned int scc;
458
459
    /* Store 4 bits of EVEX.[OF,SF,ZF,CF].  */
460
0
#define OSZC_CF 1
461
0
#define OSZC_ZF 2
462
0
#define OSZC_SF 4
463
0
#define OSZC_OF 8
464
    unsigned int oszc_flags;
465
466
    /* Invert the condition encoded in a base opcode.  */
467
    bool invert_cond;
468
469
    /* REP prefix.  */
470
    const char *rep_prefix;
471
472
    /* HLE prefix.  */
473
    const char *hle_prefix;
474
475
    /* Have BND prefix.  */
476
    const char *bnd_prefix;
477
478
    /* Have NOTRACK prefix.  */
479
    const char *notrack_prefix;
480
481
    /* Error message.  */
482
    enum i386_error error;
483
  };
484
485
typedef struct _i386_insn i386_insn;
486
487
/* Pseudo-prefix recording state, separate from i386_insn.  */
488
static struct pseudo_prefixes {
489
  /* How to encode instructions.  */
490
  enum {
491
    encoding_default = 0,
492
    encoding_vex,
493
    encoding_vex3,
494
    encoding_egpr, /* REX2 or EVEX.  */
495
    encoding_evex,
496
    encoding_evex512,
497
    encoding_error
498
  } encoding;
499
500
  /* Prefer load or store in encoding.  */
501
  enum {
502
    dir_encoding_default = 0,
503
    dir_encoding_load,
504
    dir_encoding_store,
505
    dir_encoding_swap
506
  } dir_encoding;
507
508
  /* Prefer 8bit, 16bit, 32bit displacement in encoding.  */
509
  enum {
510
    disp_encoding_default = 0,
511
    disp_encoding_8bit,
512
    disp_encoding_16bit,
513
    disp_encoding_32bit
514
  } disp_encoding;
515
516
  /* Exclude sign-extended 8bit immediate in encoding.  */
517
  bool no_imm8s;
518
519
  /* Prefer the REX byte in encoding.  */
520
  bool rex_encoding;
521
522
  /* Prefer the REX2 prefix in encoding.  */
523
  bool rex2_encoding;
524
525
  /* No CSPAZO flags update.  */
526
  bool has_nf;
527
528
  /* Disable instruction size optimization.  */
529
  bool no_optimize;
530
} pp;
531
532
/* Link RC type with corresponding string, that'll be looked for in
533
   asm.  */
534
struct RC_name
535
{
536
  enum rc_type type;
537
  const char *name;
538
  unsigned int len;
539
};
540
541
static const struct RC_name RC_NamesTable[] =
542
{
543
  {  rne, STRING_COMMA_LEN ("rn-sae") },
544
  {  rd,  STRING_COMMA_LEN ("rd-sae") },
545
  {  ru,  STRING_COMMA_LEN ("ru-sae") },
546
  {  rz,  STRING_COMMA_LEN ("rz-sae") },
547
  {  saeonly,  STRING_COMMA_LEN ("sae") },
548
};
549
550
/* To be indexed by segment register number.  */
551
static const unsigned char i386_seg_prefixes[] = {
552
  ES_PREFIX_OPCODE,
553
  CS_PREFIX_OPCODE,
554
  SS_PREFIX_OPCODE,
555
  DS_PREFIX_OPCODE,
556
  FS_PREFIX_OPCODE,
557
  GS_PREFIX_OPCODE
558
};
559
560
/* List of chars besides those in app.c:symbol_chars that can start an
561
   operand.  Used to prevent the scrubber eating vital white-space.  */
562
const char extra_symbol_chars[] = "*%-(["
563
#ifdef LEX_AT
564
  "@"
565
#endif
566
#ifdef LEX_QM
567
  "?"
568
#endif
569
  ;
570
571
#if (defined (OBJ_ELF)          \
572
     && !defined (TE_GNU)       \
573
     && !defined (TE_LINUX)       \
574
     && !defined (TE_Haiku)       \
575
     && !defined (TE_FreeBSD)       \
576
     && !defined (TE_DragonFly)       \
577
     && !defined (TE_NetBSD))
578
/* This array holds the chars that always start a comment.  If the
579
   pre-processor is disabled, these aren't very useful.  The option
580
   --divide will remove '/' from this list.  */
581
const char *i386_comment_chars = "#/";
582
#define SVR4_COMMENT_CHARS 1
583
#define PREFIX_SEPARATOR '\\'
584
585
#else
586
const char *i386_comment_chars = "#";
587
45.0k
#define PREFIX_SEPARATOR '/'
588
#endif
589
590
/* This array holds the chars that only start a comment at the beginning of
591
   a line.  If the line seems to have the form '# 123 filename'
592
   .line and .file directives will appear in the pre-processed output.
593
   Note that input_file.c hand checks for '#' at the beginning of the
594
   first line of the input file.  This is because the compiler outputs
595
   #NO_APP at the beginning of its output.
596
   Also note that comments started like this one will always work if
597
   '/' isn't otherwise defined.  */
598
const char line_comment_chars[] = "#/";
599
600
const char line_separator_chars[] = ";";
601
602
/* Chars that can be used to separate mant from exp in floating point
603
   nums.  */
604
const char EXP_CHARS[] = "eE";
605
606
/* Chars that mean this number is a floating point constant
607
   As in 0f12.456
608
   or    0d1.2345e12.  */
609
const char FLT_CHARS[] = "fFdDxXhHbB";
610
611
/* Tables for lexical analysis.  */
612
static char mnemonic_chars[256];
613
static char register_chars[256];
614
static char operand_chars[256];
615
616
/* Lexical macros.  */
617
1.17M
#define is_operand_char(x) (operand_chars[(unsigned char) x])
618
#define is_register_char(x) (register_chars[(unsigned char) x])
619
620
/* All non-digit non-letter characters that may occur in an operand and
621
   which aren't already in extra_symbol_chars[].  */
622
static const char operand_special_chars[] = "$+,)._~/<>|&^!=:@]{}";
623
624
/* md_assemble() always leaves the strings it's passed unaltered.  To
625
   effect this we maintain a stack of saved characters that we've smashed
626
   with '\0's (indicating end of strings for various sub-fields of the
627
   assembler instruction).  */
628
static char save_stack[32];
629
static char *save_stack_p;
630
#define END_STRING_AND_SAVE(s) \
631
32.0k
  do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
632
#define RESTORE_END_STRING(s) \
633
32.0k
  do { *(s) = *--save_stack_p; } while (0)
634
635
/* The instruction we're assembling.  */
636
static i386_insn i;
637
638
/* Possible templates for current insn.  */
639
static templates current_templates;
640
641
/* Per instruction expressionS buffers: max displacements & immediates.  */
642
static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
643
static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
644
645
/* Current operand we are working on.  */
646
static int this_operand = -1;
647
648
/* Are we processing a .insn directive?  */
649
8.28k
#define dot_insn() (i.tm.mnem_off == MN__insn)
650
651
static enum i386_flag_code i386_flag_code;
652
336k
#define flag_code i386_flag_code /* Permit to continue using original name.  */
653
static unsigned int object_64bit;
654
static unsigned int disallow_64bit_reloc;
655
static int use_rela_relocations = 0;
656
/* __tls_get_addr/___tls_get_addr symbol for TLS.  */
657
static const char *tls_get_addr;
658
659
#ifdef OBJ_ELF
660
661
/* The ELF ABI to use.  */
662
enum x86_elf_abi
663
{
664
  I386_ABI,
665
  X86_64_ABI,
666
  X86_64_X32_ABI
667
};
668
669
static enum x86_elf_abi x86_elf_abi = I386_ABI;
670
#endif
671
672
#if defined (TE_PE) || defined (TE_PEP)
673
/* Use big object file format.  */
674
static int use_big_obj = 0;
675
#endif
676
677
#ifdef OBJ_ELF
678
/* 1 if generating code for a shared library.  */
679
static int shared = 0;
680
681
const unsigned int x86_sframe_cfa_sp_reg = REG_SP;
682
/* The other CFA base register for SFrame stack trace info.  */
683
const unsigned int x86_sframe_cfa_fp_reg = REG_FP;
684
/* The return address register for SFrame stack trace info.  For AMD64, RA
685
   tracking is not needed, but some directives like .cfi_undefined may use
686
   RA to indicate the outermost frame.  */
687
const unsigned int x86_sframe_cfa_ra_reg = REG_RA;
688
689
static ginsnS *x86_ginsn_new (const symbolS *, enum ginsn_gen_mode);
690
#endif
691
692
/* 1 for intel syntax,
693
   0 if att syntax.  */
694
static int intel_syntax = 0;
695
696
static enum x86_64_isa
697
{
698
  amd64 = 1,  /* AMD64 ISA.  */
699
  intel64 /* Intel64 ISA.  */
700
} isa64;
701
702
/* 1 for intel mnemonic,
703
   0 if att mnemonic.  */
704
static int intel_mnemonic = !SYSV386_COMPAT;
705
706
/* 1 if pseudo registers are permitted.  */
707
static int allow_pseudo_reg = 0;
708
709
/* 1 if register prefix % not required.  */
710
static int allow_naked_reg = 0;
711
712
/* 1 if the assembler should add BND prefix for all control-transferring
713
   instructions supporting it, even if this prefix wasn't specified
714
   explicitly.  */
715
static int add_bnd_prefix = 0;
716
717
/* 1 if pseudo index register, eiz/riz, is allowed .  */
718
static int allow_index_reg = 0;
719
720
/* 1 if the assembler should ignore LOCK prefix, even if it was
721
   specified explicitly.  */
722
static int omit_lock_prefix = 0;
723
724
/* 1 if the assembler should encode lfence, mfence, and sfence as
725
   "lock addl $0, (%{re}sp)".  */
726
static int avoid_fence = 0;
727
728
/* 1 if lfence should be inserted after every load.  */
729
static int lfence_after_load = 0;
730
731
/* Non-zero if lfence should be inserted before indirect branch.  */
732
static enum lfence_before_indirect_branch_kind
733
  {
734
    lfence_branch_none = 0,
735
    lfence_branch_register,
736
    lfence_branch_memory,
737
    lfence_branch_all
738
  }
739
lfence_before_indirect_branch;
740
741
/* Non-zero if lfence should be inserted before ret.  */
742
static enum lfence_before_ret_kind
743
  {
744
    lfence_before_ret_none = 0,
745
    lfence_before_ret_not,
746
    lfence_before_ret_or,
747
    lfence_before_ret_shl
748
  }
749
lfence_before_ret;
750
751
/* 1 if the assembler should generate relax relocations.  */
752
753
#ifdef TE_SOLARIS
754
/* PR gas/19520: The Solaris/x86 linker cannot handle relax relocations
755
   before Solaris 11.4 which cannot easily be detected in cross
756
   configurations.  */
757
#define DEFAULT_GENERATE_X86_RELAX_RELOCATIONS 0
758
#else
759
0
#define DEFAULT_GENERATE_X86_RELAX_RELOCATIONS 1
760
#endif
761
762
static int generate_relax_relocations
763
  = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
764
765
/* 1 if the assembler should check tls relocation.  */
766
static bool tls_check = DEFAULT_X86_TLS_CHECK;
767
768
static enum check_kind
769
  {
770
    check_none = 0,
771
    check_warning,
772
    check_error
773
  }
774
sse_check, operand_check = check_warning;
775
776
/* Non-zero if branches should be aligned within power of 2 boundary.  */
777
static int align_branch_power = 0;
778
779
/* Types of branches to align.  */
780
enum align_branch_kind
781
  {
782
    align_branch_none = 0,
783
    align_branch_jcc = 1,
784
    align_branch_fused = 2,
785
    align_branch_jmp = 3,
786
    align_branch_call = 4,
787
    align_branch_indirect = 5,
788
    align_branch_ret = 6
789
  };
790
791
/* Type bits of branches to align.  */
792
enum align_branch_bit
793
  {
794
    align_branch_jcc_bit = 1 << align_branch_jcc,
795
    align_branch_fused_bit = 1 << align_branch_fused,
796
    align_branch_jmp_bit = 1 << align_branch_jmp,
797
    align_branch_call_bit = 1 << align_branch_call,
798
    align_branch_indirect_bit = 1 << align_branch_indirect,
799
    align_branch_ret_bit = 1 << align_branch_ret
800
  };
801
802
static unsigned int align_branch = (align_branch_jcc_bit
803
            | align_branch_fused_bit
804
            | align_branch_jmp_bit);
805
806
/* Types of condition jump used by macro-fusion.  */
807
enum mf_jcc_kind
808
  {
809
    mf_jcc_jo = 0,  /* base opcode 0x70  */
810
    mf_jcc_jc,      /* base opcode 0x72  */
811
    mf_jcc_je,      /* base opcode 0x74  */
812
    mf_jcc_jna,     /* base opcode 0x76  */
813
    mf_jcc_js,      /* base opcode 0x78  */
814
    mf_jcc_jp,      /* base opcode 0x7a  */
815
    mf_jcc_jl,      /* base opcode 0x7c  */
816
    mf_jcc_jle,     /* base opcode 0x7e  */
817
  };
818
819
/* Types of compare flag-modifying insntructions used by macro-fusion.  */
820
enum mf_cmp_kind
821
  {
822
    mf_cmp_test_and,  /* test/cmp */
823
    mf_cmp_alu_cmp,  /* add/sub/cmp */
824
    mf_cmp_incdec  /* inc/dec */
825
  };
826
827
/* The maximum padding size for fused jcc.  CMP like instruction can
828
   be 9 bytes and jcc can be 6 bytes.  Leave room just in case for
829
   prefixes.   */
830
478
#define MAX_FUSED_JCC_PADDING_SIZE 20
831
832
/* The maximum number of prefixes added for an instruction.  */
833
static unsigned int align_branch_prefix_size = 5;
834
835
/* Optimization:
836
   1. Clear the REX_W bit with register operand if possible.
837
   2. Above plus use 128bit vector instruction to clear the full vector
838
      register.
839
 */
840
static int optimize = 0;
841
842
/* Optimization:
843
   1. Clear the REX_W bit with register operand if possible.
844
   2. Above plus use 128bit vector instruction to clear the full vector
845
      register.
846
   3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
847
      "testb $imm7,%r8".
848
 */
849
static int optimize_for_space = 0;
850
851
/* Register prefix used for error message.  */
852
static const char *register_prefix = "%";
853
854
/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
855
   leave, push, and pop instructions so that gcc has the same stack
856
   frame as in 32 bit mode.  */
857
static char stackop_size = '\0';
858
859
/* Non-zero to optimize code alignment.  */
860
int optimize_align_code = 1;
861
862
/* Non-zero to quieten some warnings.  */
863
static int quiet_warnings = 0;
864
865
/* Guard to avoid repeated warnings about non-16-bit code on 16-bit CPUs.  */
866
static bool pre_386_16bit_warned;
867
868
/* CPU name.  */
869
static const char *cpu_arch_name = NULL;
870
static char *cpu_sub_arch_name = NULL;
871
872
/* CPU feature flags.  */
873
static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
874
875
/* ISA extensions available in 64-bit mode only.  */
876
static const i386_cpu_flags cpu_64_flags = CPU_ANY_64_FLAGS;
877
878
/* If we have selected a cpu we are generating instructions for.  */
879
static int cpu_arch_tune_set = 0;
880
881
/* Cpu we are generating instructions for.  */
882
static enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
883
884
/* CPU instruction set architecture used.  */
885
static enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
886
887
/* CPU feature flags of instruction set architecture used.  */
888
static i386_cpu_flags cpu_arch_isa_flags;
889
890
/* If set, conditional jumps are not automatically promoted to handle
891
   larger than a byte offset.  */
892
static bool no_cond_jump_promotion = false;
893
894
/* This will be set from an expression parser hook if there's any
895
   applicable operator involved in an expression.  */
896
static enum {
897
  expr_operator_none,
898
  expr_operator_present,
899
  expr_large_value,
900
} expr_mode;
901
902
/* Encode SSE instructions with VEX prefix.  */
903
static unsigned int sse2avx;
904
905
/* Encode aligned vector move as unaligned vector move.  */
906
static unsigned int use_unaligned_vector_move;
907
908
/* Maximum permitted vector size. */
909
0
#define VSZ128 0
910
28.2k
#define VSZ256 1
911
23.1k
#define VSZ512 2
912
65
#define VSZ_DEFAULT VSZ512
913
static unsigned int vector_size = VSZ_DEFAULT;
914
915
/* Encode scalar AVX instructions with specific vector length.  */
916
static enum
917
  {
918
    vex128 = 0,
919
    vex256
920
  } avxscalar;
921
922
/* Encode VEX WIG instructions with specific vex.w.  */
923
static enum
924
  {
925
    vexw0 = 0,
926
    vexw1
927
  } vexwig;
928
929
/* Encode scalar EVEX LIG instructions with specific vector length.  */
930
static enum
931
  {
932
    evexl128 = 0,
933
    evexl256,
934
    evexl512
935
  } evexlig;
936
937
/* Encode EVEX WIG instructions with specific evex.w.  */
938
static enum
939
  {
940
    evexw0 = 0,
941
    evexw1
942
  } evexwig;
943
944
/* Value to encode in EVEX RC bits, for SAE-only instructions.  */
945
static enum rc_type evexrcig = rne;
946
947
/* Pre-defined "_GLOBAL_OFFSET_TABLE_".  */
948
static symbolS *GOT_symbol;
949
950
/* The dwarf2 return column, adjusted for 32 or 64 bit.  */
951
unsigned int x86_dwarf2_return_column;
952
953
/* The dwarf2 data alignment, adjusted for 32 or 64 bit.  */
954
int x86_cie_data_alignment;
955
956
/* Interface to relax_segment.
957
   There are 3 major relax states for 386 jump insns because the
958
   different types of jumps add different sizes to frags when we're
959
   figuring out what sort of jump to choose to reach a given label.
960
961
   BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
962
   branches which are handled by md_estimate_size_before_relax() and
963
   i386_generic_table_relax_frag().  */
964
965
/* Types.  */
966
3
#define UNCOND_JUMP 0
967
0
#define COND_JUMP 1
968
0
#define COND_JUMP86 2
969
0
#define BRANCH_PADDING 3
970
0
#define BRANCH_PREFIX 4
971
0
#define FUSED_JCC_PADDING 5
972
973
/* Sizes.  */
974
75
#define CODE16  1
975
92
#define SMALL 0
976
#define SMALL16 (SMALL | CODE16)
977
0
#define BIG 2
978
0
#define BIG16 (BIG | CODE16)
979
980
#ifndef INLINE
981
#ifdef __GNUC__
982
#define INLINE __inline__
983
#else
984
#define INLINE
985
#endif
986
#endif
987
988
#define ENCODE_RELAX_STATE(type, size) \
989
46
  ((relax_substateT) (((type) << 2) | (size)))
990
#define TYPE_FROM_RELAX_STATE(s) \
991
3
  ((s) >> 2)
992
#define DISP_SIZE_FROM_RELAX_STATE(s) \
993
0
    ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
994
995
/* This table is used by relax_frag to promote short jumps to long
996
   ones where necessary.  SMALL (short) jumps may be promoted to BIG
997
   (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long).  We
998
   don't allow a short jump in a 32 bit code segment to be promoted to
999
   a 16 bit offset jump because it's slower (requires data size
1000
   prefix), and doesn't work, unless the destination is in the bottom
1001
   64k of the code segment (The top 16 bits of eip are zeroed).  */
1002
1003
const relax_typeS md_relax_table[] =
1004
{
1005
  /* The fields are:
1006
     1) most positive reach of this state,
1007
     2) most negative reach of this state,
1008
     3) how many bytes this mode will have in the variable part of the frag
1009
     4) which index into the table to try if we can't fit into this one.  */
1010
1011
  /* UNCOND_JUMP states.  */
1012
  {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
1013
  {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
1014
  /* dword jmp adds 4 bytes to frag:
1015
     0 extra opcode bytes, 4 displacement bytes.  */
1016
  {0, 0, 4, 0},
1017
  /* word jmp adds 2 byte2 to frag:
1018
     0 extra opcode bytes, 2 displacement bytes.  */
1019
  {0, 0, 2, 0},
1020
1021
  /* COND_JUMP states.  */
1022
  {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
1023
  {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
1024
  /* dword conditionals adds 5 bytes to frag:
1025
     1 extra opcode byte, 4 displacement bytes.  */
1026
  {0, 0, 5, 0},
1027
  /* word conditionals add 3 bytes to frag:
1028
     1 extra opcode byte, 2 displacement bytes.  */
1029
  {0, 0, 3, 0},
1030
1031
  /* COND_JUMP86 states.  */
1032
  {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
1033
  {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
1034
  /* dword conditionals adds 5 bytes to frag:
1035
     1 extra opcode byte, 4 displacement bytes.  */
1036
  {0, 0, 5, 0},
1037
  /* word conditionals add 4 bytes to frag:
1038
     1 displacement byte and a 3 byte long branch insn.  */
1039
  {0, 0, 4, 0}
1040
};
1041
1042
#define ARCH(n, t, f, s) \
1043
  { STRING_COMMA_LEN (#n), s, PROCESSOR_ ## t, vsz_none, CPU_ ## f ## _FLAGS, \
1044
    CPU_NONE_FLAGS }
1045
#define SUBARCH(n, e, d, s) \
1046
  { STRING_COMMA_LEN (#n), s, PROCESSOR_NONE, vsz_none, CPU_ ## e ## _FLAGS, \
1047
    CPU_ ## d ## _FLAGS }
1048
#define VECARCH(n, e, d, v) \
1049
  { STRING_COMMA_LEN (#n), false, PROCESSOR_NONE, vsz_ ## v, \
1050
    CPU_ ## e ## _FLAGS, CPU_ ## d ## _FLAGS }
1051
1052
#define CPU_ANY_APX_NCI_NDD_NF_FLAGS \
1053
  { .bitfield = \
1054
    { .cpuapx_nci = true, \
1055
      .cpuapx_ndd = true, \
1056
      .cpuapx_nf = true } }
1057
1058
static const arch_entry cpu_arch[] =
1059
{
1060
  /* Do not replace the first two entries - i386_target_format() and
1061
     set_cpu_arch() rely on them being there in this order.  */
1062
  ARCH (generic32, GENERIC32, GENERIC32, false),
1063
  ARCH (generic64, GENERIC64, GENERIC64, false),
1064
  ARCH (i8086, UNKNOWN, NONE, false),
1065
  ARCH (i186, UNKNOWN, 186, false),
1066
  ARCH (i286, UNKNOWN, 286, false),
1067
  ARCH (i386, I386, 386, false),
1068
  ARCH (i486, I486, 486, false),
1069
  ARCH (i586, PENTIUM, 586, false),
1070
  ARCH (pentium, PENTIUM, 586, false),
1071
  ARCH (i686, I686, 686, false),
1072
  ARCH (pentiumpro, PENTIUMPRO, PENTIUMPRO, false),
1073
  ARCH (pentiumii, PENTIUMPRO, P2, false),
1074
  ARCH (pentiumiii, PENTIUMPRO, P3, false),
1075
  ARCH (pentium4, PENTIUM4, P4, false),
1076
  ARCH (prescott, NOCONA, CORE, false),
1077
  ARCH (nocona, NOCONA, NOCONA, false),
1078
  ARCH (yonah, CORE, CORE, true),
1079
  ARCH (core, CORE, CORE, false),
1080
  ARCH (merom, CORE2, CORE2, true),
1081
  ARCH (core2, CORE2, CORE2, false),
1082
  ARCH (corei7, COREI7, COREI7, false),
1083
  ARCH (iamcu, IAMCU, IAMCU, false),
1084
  ARCH (k6, K6, K6, false),
1085
  ARCH (k6_2, K6, K6_2, false),
1086
  ARCH (athlon, ATHLON, ATHLON, false),
1087
  ARCH (sledgehammer, K8, K8, true),
1088
  ARCH (opteron, K8, K8, false),
1089
  ARCH (k8, K8, K8, false),
1090
  ARCH (amdfam10, AMDFAM10, AMDFAM10, false),
1091
  ARCH (bdver1, BD, BDVER1, false),
1092
  ARCH (bdver2, BD, BDVER2, false),
1093
  ARCH (bdver3, BD, BDVER3, false),
1094
  ARCH (bdver4, BD, BDVER4, false),
1095
  ARCH (znver1, ZNVER, ZNVER1, false),
1096
  ARCH (znver2, ZNVER, ZNVER2, false),
1097
  ARCH (znver3, ZNVER, ZNVER3, false),
1098
  ARCH (znver4, ZNVER, ZNVER4, false),
1099
  ARCH (znver5, ZNVER, ZNVER5, false),
1100
  ARCH (znver6, ZNVER, ZNVER6, false),
1101
  ARCH (btver1, BT, BTVER1, false),
1102
  ARCH (btver2, BT, BTVER2, false),
1103
1104
  SUBARCH (8087, 8087, ANY_8087, false),
1105
  SUBARCH (87, NONE, ANY_8087, false), /* Disable only!  */
1106
  SUBARCH (287, 287, ANY_287, false),
1107
  SUBARCH (387, 387, ANY_387, false),
1108
  SUBARCH (687, 687, ANY_687, false),
1109
  SUBARCH (cmov, CMOV, CMOV, false),
1110
  SUBARCH (fxsr, FXSR, ANY_FXSR, false),
1111
  SUBARCH (mmx, MMX, ANY_MMX, false),
1112
  SUBARCH (sse, SSE, ANY_SSE, false),
1113
  SUBARCH (sse2, SSE2, ANY_SSE2, false),
1114
  SUBARCH (sse3, SSE3, ANY_SSE3, false),
1115
  SUBARCH (sse4a, SSE4A, ANY_SSE4A, false),
1116
  SUBARCH (ssse3, SSSE3, ANY_SSSE3, false),
1117
  SUBARCH (sse4.1, SSE4_1, ANY_SSE4_1, false),
1118
  SUBARCH (sse4.2, SSE4_2, ANY_SSE4_2, false),
1119
  SUBARCH (sse4, SSE4_2, ANY_SSE4_1, false),
1120
  VECARCH (avx, AVX, ANY_AVX, reset),
1121
  VECARCH (avx2, AVX2, ANY_AVX2, reset),
1122
  VECARCH (avx512f, AVX512F, ANY_AVX512F, reset),
1123
  VECARCH (avx512cd, AVX512CD, ANY_AVX512CD, reset),
1124
  VECARCH (avx512er, AVX512ER, ANY_AVX512ER, reset),
1125
  VECARCH (avx512pf, AVX512PF, ANY_AVX512PF, reset),
1126
  VECARCH (avx512dq, AVX512DQ, ANY_AVX512DQ, reset),
1127
  VECARCH (avx512bw, AVX512BW, ANY_AVX512BW, reset),
1128
  VECARCH (avx512vl, AVX512VL, ANY_AVX512VL, reset),
1129
  SUBARCH (monitor, MONITOR, MONITOR, false),
1130
  SUBARCH (vmx, VMX, ANY_VMX, false),
1131
  SUBARCH (vmfunc, VMFUNC, ANY_VMFUNC, false),
1132
  SUBARCH (smx, SMX, SMX, false),
1133
  SUBARCH (xsave, XSAVE, ANY_XSAVE, false),
1134
  SUBARCH (xsaveopt, XSAVEOPT, ANY_XSAVEOPT, false),
1135
  SUBARCH (xsavec, XSAVEC, ANY_XSAVEC, false),
1136
  SUBARCH (xsaves, XSAVES, ANY_XSAVES, false),
1137
  SUBARCH (aes, AES, ANY_AES, false),
1138
  SUBARCH (pclmul, PCLMULQDQ, ANY_PCLMULQDQ, false),
1139
  SUBARCH (clmul, PCLMULQDQ, ANY_PCLMULQDQ, true),
1140
  SUBARCH (fsgsbase, FSGSBASE, FSGSBASE, false),
1141
  SUBARCH (rdrnd, RDRND, RDRND, false),
1142
  SUBARCH (f16c, F16C, ANY_F16C, false),
1143
  SUBARCH (bmi2, BMI2, BMI2, false),
1144
  SUBARCH (fma, FMA, ANY_FMA, false),
1145
  SUBARCH (fma4, FMA4, ANY_FMA4, false),
1146
  SUBARCH (xop, XOP, ANY_XOP, false),
1147
  SUBARCH (lwp, LWP, ANY_LWP, false),
1148
  SUBARCH (movbe, MOVBE, MOVBE, false),
1149
  SUBARCH (cx16, CX16, CX16, false),
1150
  SUBARCH (lahf_sahf, LAHF_SAHF, LAHF_SAHF, false),
1151
  SUBARCH (ept, EPT, ANY_EPT, false),
1152
  SUBARCH (lzcnt, LZCNT, LZCNT, false),
1153
  SUBARCH (popcnt, POPCNT, POPCNT, false),
1154
  SUBARCH (hle, HLE, HLE, false),
1155
  SUBARCH (rtm, RTM, ANY_RTM, false),
1156
  SUBARCH (tsx, TSX, TSX, false),
1157
  SUBARCH (invpcid, INVPCID, INVPCID, false),
1158
  SUBARCH (clflush, CLFLUSH, CLFLUSH, false),
1159
  SUBARCH (nop, NOP, NOP, false),
1160
  SUBARCH (syscall, SYSCALL, SYSCALL, false),
1161
  SUBARCH (rdtscp, RDTSCP, RDTSCP, false),
1162
  SUBARCH (3dnow, 3DNOW, ANY_3DNOW, false),
1163
  SUBARCH (3dnowa, 3DNOWA, ANY_3DNOWA, false),
1164
  SUBARCH (padlock, PADLOCK, PADLOCK, false),
1165
  SUBARCH (pacifica, SVME, ANY_SVME, true),
1166
  SUBARCH (svme, SVME, ANY_SVME, false),
1167
  SUBARCH (abm, ABM, ABM, false),
1168
  SUBARCH (bmi, BMI, BMI, false),
1169
  SUBARCH (tbm, TBM, TBM, false),
1170
  SUBARCH (adx, ADX, ADX, false),
1171
  SUBARCH (rdseed, RDSEED, RDSEED, false),
1172
  SUBARCH (prfchw, PRFCHW, PRFCHW, false),
1173
  SUBARCH (smap, SMAP, SMAP, false),
1174
  SUBARCH (mpx, MPX, ANY_MPX, false),
1175
  SUBARCH (sha, SHA, ANY_SHA, false),
1176
  SUBARCH (clflushopt, CLFLUSHOPT, CLFLUSHOPT, false),
1177
  SUBARCH (prefetchwt1, PREFETCHWT1, PREFETCHWT1, false),
1178
  SUBARCH (se1, SE1, SE1, false),
1179
  SUBARCH (clwb, CLWB, CLWB, false),
1180
  VECARCH (avx512ifma, AVX512IFMA, ANY_AVX512IFMA, reset),
1181
  VECARCH (avx512vbmi, AVX512VBMI, ANY_AVX512VBMI, reset),
1182
  VECARCH (avx512_4fmaps, AVX512_4FMAPS, ANY_AVX512_4FMAPS, reset),
1183
  VECARCH (avx512_4vnniw, AVX512_4VNNIW, ANY_AVX512_4VNNIW, reset),
1184
  VECARCH (avx512_vpopcntdq, AVX512_VPOPCNTDQ, ANY_AVX512_VPOPCNTDQ, reset),
1185
  VECARCH (avx512_vbmi2, AVX512_VBMI2, ANY_AVX512_VBMI2, reset),
1186
  VECARCH (avx512_vnni, AVX512_VNNI, ANY_AVX512_VNNI, reset),
1187
  VECARCH (avx512_bitalg, AVX512_BITALG, ANY_AVX512_BITALG, reset),
1188
  VECARCH (avx_vnni, AVX_VNNI, ANY_AVX_VNNI, reset),
1189
  SUBARCH (clzero, CLZERO, CLZERO, false),
1190
  SUBARCH (mwaitx, MWAITX, MWAITX, false),
1191
  SUBARCH (ospke, OSPKE, ANY_OSPKE, false),
1192
  SUBARCH (rdpid, RDPID, RDPID, false),
1193
  SUBARCH (ptwrite, PTWRITE, PTWRITE, false),
1194
  SUBARCH (ibt, IBT, IBT, false),
1195
  SUBARCH (shstk, SHSTK, SHSTK, false),
1196
  SUBARCH (gfni, GFNI, ANY_GFNI, false),
1197
  VECARCH (vaes, VAES, ANY_VAES, reset),
1198
  VECARCH (vpclmulqdq, VPCLMULQDQ, ANY_VPCLMULQDQ, reset),
1199
  SUBARCH (wbnoinvd, WBNOINVD, WBNOINVD, false),
1200
  SUBARCH (pconfig, PCONFIG, PCONFIG, false),
1201
  SUBARCH (waitpkg, WAITPKG, WAITPKG, false),
1202
  SUBARCH (cldemote, CLDEMOTE, CLDEMOTE, false),
1203
  SUBARCH (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1204
  SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
1205
  SUBARCH (amx_fp16, AMX_FP16, ANY_AMX_FP16, false),
1206
  SUBARCH (amx_complex, AMX_COMPLEX, ANY_AMX_COMPLEX, false),
1207
  SUBARCH (amx_transpose, AMX_TRANSPOSE, ANY_AMX_TRANSPOSE, false),
1208
  SUBARCH (amx_tf32, AMX_TF32, ANY_AMX_TF32, false),
1209
  SUBARCH (amx_fp8, AMX_FP8, ANY_AMX_FP8, false),
1210
  SUBARCH (amx_movrs, AMX_MOVRS, ANY_AMX_MOVRS, false),
1211
  SUBARCH (amx_avx512, AMX_AVX512, ANY_AMX_AVX512, false),
1212
  SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
1213
  SUBARCH (movdiri, MOVDIRI, MOVDIRI, false),
1214
  SUBARCH (movdir64b, MOVDIR64B, MOVDIR64B, false),
1215
  VECARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, reset),
1216
  VECARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1217
     ANY_AVX512_VP2INTERSECT, reset),
1218
  VECARCH (avx512_bmm, AVX512_BMM, ANY_AVX512_BMM, reset),
1219
  SUBARCH (tdx, TDX, TDX, false),
1220
  SUBARCH (enqcmd, ENQCMD, ENQCMD, false),
1221
  SUBARCH (serialize, SERIALIZE, SERIALIZE, false),
1222
  SUBARCH (rdpru, RDPRU, RDPRU, false),
1223
  SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
1224
  SUBARCH (sev_es, SEV_ES, ANY_SEV_ES, false),
1225
  SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
1226
  SUBARCH (kl, KL, ANY_KL, false),
1227
  SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
1228
  SUBARCH (uintr, UINTR, UINTR, false),
1229
  SUBARCH (hreset, HRESET, HRESET, false),
1230
  VECARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, reset),
1231
  SUBARCH (prefetchi, PREFETCHI, PREFETCHI, false),
1232
  VECARCH (avx_ifma, AVX_IFMA, ANY_AVX_IFMA, reset),
1233
  VECARCH (avx_vnni_int8, AVX_VNNI_INT8, ANY_AVX_VNNI_INT8, reset),
1234
  SUBARCH (cmpccxadd, CMPCCXADD, CMPCCXADD, false),
1235
  SUBARCH (wrmsrns, WRMSRNS, WRMSRNS, false),
1236
  SUBARCH (msrlist, MSRLIST, MSRLIST, false),
1237
  VECARCH (avx_ne_convert, AVX_NE_CONVERT, ANY_AVX_NE_CONVERT, reset),
1238
  SUBARCH (rao_int, RAO_INT, RAO_INT, false),
1239
  SUBARCH (rmpquery, RMPQUERY, ANY_RMPQUERY, false),
1240
  SUBARCH (rmpread, RMPREAD, ANY_RMPREAD, false),
1241
  SUBARCH (fred, FRED, ANY_FRED, false),
1242
  SUBARCH (lkgs, LKGS, ANY_LKGS, false),
1243
  VECARCH (avx_vnni_int16, AVX_VNNI_INT16, ANY_AVX_VNNI_INT16, reset),
1244
  VECARCH (sha512, SHA512, ANY_SHA512, reset),
1245
  VECARCH (sm3, SM3, ANY_SM3, reset),
1246
  VECARCH (sm4, SM4, ANY_SM4, reset),
1247
  SUBARCH (pbndkb, PBNDKB, PBNDKB, false),
1248
  VECARCH (avx10.1, AVX10_1, ANY_AVX512F, set),
1249
  SUBARCH (user_msr, USER_MSR, USER_MSR, false),
1250
  SUBARCH (apx_f, APX_F, ANY_APX_F, false),
1251
  SUBARCH (apx_nci, APX_NCI, ANY_APX_NCI, false),
1252
  SUBARCH (apx_ndd, APX_NDD, ANY_APX_NDD, false),
1253
  SUBARCH (apx_nf, APX_NF, ANY_APX_NF, false),
1254
  SUBARCH (apx_nci_ndd_nf, APX_NCI_NDD_NF, ANY_APX_NCI_NDD_NF, false),
1255
  VECARCH (avx10.2, AVX10_2, ANY_AVX10_2, set),
1256
  SUBARCH (gmism2, GMISM2, GMISM2, false),
1257
  SUBARCH (gmiccs, GMICCS, GMICCS, false),
1258
  SUBARCH (msr_imm, MSR_IMM, MSR_IMM, false),
1259
  SUBARCH (padlockrng2, PADLOCKRNG2, PADLOCKRNG2, false),
1260
  SUBARCH (padlockphe2, PADLOCKPHE2, PADLOCKPHE2, false),
1261
  SUBARCH (padlockxmodx, PADLOCKXMODX, PADLOCKXMODX, false),
1262
  SUBARCH (movrs, MOVRS, MOVRS, false),
1263
};
1264
1265
#undef SUBARCH
1266
#undef ARCH
1267
1268
#ifdef I386COFF
1269
/* Like s_lcomm_internal in gas/read.c but the alignment string
1270
   is allowed to be optional.  */
1271
1272
static symbolS *
1273
pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1274
{
1275
  addressT align = 0;
1276
1277
  SKIP_WHITESPACE ();
1278
1279
  if (needs_align
1280
      && *input_line_pointer == ',')
1281
    {
1282
      align = parse_align (needs_align - 1);
1283
1284
      if (align == (addressT) -1)
1285
  return NULL;
1286
    }
1287
  else
1288
    {
1289
      if (size >= 8)
1290
  align = 3;
1291
      else if (size >= 4)
1292
  align = 2;
1293
      else if (size >= 2)
1294
  align = 1;
1295
      else
1296
  align = 0;
1297
    }
1298
1299
  bss_alloc (symbolP, size, align);
1300
  return symbolP;
1301
}
1302
1303
static void
1304
pe_lcomm (int needs_align)
1305
{
1306
  s_comm_internal (needs_align * 2, pe_lcomm_internal);
1307
}
1308
#endif
1309
1310
const pseudo_typeS md_pseudo_table[] =
1311
{
1312
#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1313
  {"align", s_align_bytes, 0},
1314
#else
1315
  {"align", s_align_ptwo, 0},
1316
#endif
1317
  {"arch", set_cpu_arch, 0},
1318
#ifdef OBJ_AOUT
1319
  {"bss", s_bss, 0},
1320
#endif
1321
#ifdef I386COFF
1322
  {"lcomm", pe_lcomm, 1},
1323
#endif
1324
  {"ffloat", float_cons, 'f'},
1325
  {"dfloat", float_cons, 'd'},
1326
  {"tfloat", float_cons, 'x'},
1327
  {"hfloat", float_cons, 'h'},
1328
  {"bfloat16", float_cons, 'b'},
1329
  {"value", cons, 2},
1330
  {"slong", signed_cons, 4},
1331
  {"insn", s_insn, 0},
1332
  {"noopt", s_noopt, 0},
1333
  {"optim", s_ignore, 0},
1334
  {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1335
  {"code16", set_code_flag, CODE_16BIT},
1336
  {"code32", set_code_flag, CODE_32BIT},
1337
#ifdef BFD64
1338
  {"code64", set_code_flag, CODE_64BIT},
1339
#endif
1340
  {"intel_syntax", set_intel_syntax, 1},
1341
  {"att_syntax", set_intel_syntax, 0},
1342
  {"intel_mnemonic", set_intel_mnemonic, 1},
1343
  {"att_mnemonic", set_intel_mnemonic, 0},
1344
  {"allow_index_reg", set_allow_index_reg, 1},
1345
  {"disallow_index_reg", set_allow_index_reg, 0},
1346
  {"sse_check", set_check, 0},
1347
  {"operand_check", set_check, 1},
1348
#ifdef OBJ_ELF
1349
  {"largecomm", handle_large_common, 0},
1350
#else
1351
  {"file", dwarf2_directive_file, 0},
1352
  {"loc", dwarf2_directive_loc, 0},
1353
  {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
1354
#endif
1355
#ifdef TE_PE
1356
  {"secrel32", pe_directive_secrel, 0},
1357
  {"secidx", pe_directive_secidx, 0},
1358
#endif
1359
  {0, 0, 0}
1360
};
1361
1362
/* For interface with expression ().  */
1363
extern char *input_line_pointer;
1364
1365
/* Hash table for instruction mnemonic lookup.  */
1366
static htab_t op_hash;
1367
1368
/* Hash table for register lookup.  */
1369
static htab_t reg_hash;
1370
1371
#if (defined (OBJ_ELF) || defined (OBJ_MACH_O) || defined (TE_PE))
1372
static const struct
1373
{
1374
  const char *str;
1375
  unsigned int len;
1376
  const enum bfd_reloc_code_real rel[2];
1377
  const i386_operand_type types64;
1378
  bool need_GOT_symbol;
1379
}
1380
gotrel[] =
1381
{
1382
#define OPERAND_TYPE_IMM32_32S_DISP32 { .bitfield = \
1383
      { .imm32 = 1, .imm32s = 1, .disp32 = 1 } }
1384
#define OPERAND_TYPE_IMM32_32S_64_DISP32 { .bitfield = \
1385
      { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1 } }
1386
#define OPERAND_TYPE_IMM32_32S_64_DISP32_64 { .bitfield = \
1387
      { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1, .disp64 = 1 } }
1388
#define OPERAND_TYPE_IMM64_DISP64 { .bitfield = \
1389
      { .imm64 = 1, .disp64 = 1 } }
1390
1391
#ifndef TE_PE
1392
#ifdef OBJ_ELF
1393
    { STRING_COMMA_LEN ("SIZE"),      { BFD_RELOC_SIZE32,
1394
          BFD_RELOC_SIZE32 },
1395
    { .bitfield = { .imm32 = 1, .imm64 = 1 } }, false },
1396
#endif
1397
    { STRING_COMMA_LEN ("PLTOFF"),   { _dummy_first_bfd_reloc_code_real,
1398
               BFD_RELOC_64_PLTOFF },
1399
    { .bitfield = { .imm64 = 1 } }, true },
1400
    { STRING_COMMA_LEN ("PLT"),      { BFD_RELOC_386_PLT32,
1401
               BFD_RELOC_32_PLT_PCREL },
1402
    OPERAND_TYPE_IMM32_32S_DISP32, false },
1403
    { STRING_COMMA_LEN ("GOTPLT"),   { _dummy_first_bfd_reloc_code_real,
1404
               BFD_RELOC_X86_64_GOTPLT64 },
1405
    OPERAND_TYPE_IMM64_DISP64, true },
1406
    { STRING_COMMA_LEN ("GOTOFF"),   { BFD_RELOC_32_GOTOFF,
1407
               BFD_RELOC_64_GOTOFF },
1408
    OPERAND_TYPE_IMM64_DISP64, true },
1409
    { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
1410
               BFD_RELOC_X86_64_GOTPCREL },
1411
    OPERAND_TYPE_IMM32_32S_DISP32, true },
1412
    { STRING_COMMA_LEN ("TLSGD"),    { BFD_RELOC_386_TLS_GD,
1413
               BFD_RELOC_X86_64_TLSGD    },
1414
    OPERAND_TYPE_IMM32_32S_DISP32, true },
1415
    { STRING_COMMA_LEN ("TLSLDM"),   { BFD_RELOC_386_TLS_LDM,
1416
               _dummy_first_bfd_reloc_code_real },
1417
    OPERAND_TYPE_NONE, true },
1418
    { STRING_COMMA_LEN ("TLSLD"),    { _dummy_first_bfd_reloc_code_real,
1419
               BFD_RELOC_X86_64_TLSLD    },
1420
    OPERAND_TYPE_IMM32_32S_DISP32, true },
1421
    { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
1422
               BFD_RELOC_X86_64_GOTTPOFF },
1423
    OPERAND_TYPE_IMM32_32S_DISP32, true },
1424
    { STRING_COMMA_LEN ("TPOFF"),    { BFD_RELOC_386_TLS_LE_32,
1425
               BFD_RELOC_X86_64_TPOFF32  },
1426
    OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
1427
    { STRING_COMMA_LEN ("NTPOFF"),   { BFD_RELOC_386_TLS_LE,
1428
               _dummy_first_bfd_reloc_code_real },
1429
    OPERAND_TYPE_NONE, true },
1430
    { STRING_COMMA_LEN ("DTPOFF"),   { BFD_RELOC_386_TLS_LDO_32,
1431
               BFD_RELOC_X86_64_DTPOFF32 },
1432
    OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
1433
    { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
1434
               _dummy_first_bfd_reloc_code_real },
1435
    OPERAND_TYPE_NONE, true },
1436
    { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
1437
               _dummy_first_bfd_reloc_code_real },
1438
    OPERAND_TYPE_NONE, true },
1439
    { STRING_COMMA_LEN ("GOT"),      { BFD_RELOC_386_GOT32,
1440
               BFD_RELOC_X86_64_GOT32    },
1441
    OPERAND_TYPE_IMM32_32S_64_DISP32, true },
1442
    { STRING_COMMA_LEN ("TLSDESC"),  { BFD_RELOC_386_TLS_GOTDESC,
1443
               BFD_RELOC_X86_64_GOTPC32_TLSDESC },
1444
    OPERAND_TYPE_IMM32_32S_DISP32, true },
1445
    { STRING_COMMA_LEN ("TLSCALL"),  { BFD_RELOC_386_TLS_DESC_CALL,
1446
               BFD_RELOC_X86_64_TLSDESC_CALL },
1447
    OPERAND_TYPE_IMM32_32S_DISP32, true },
1448
#else /* TE_PE */
1449
    { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
1450
               BFD_RELOC_32_SECREL },
1451
    OPERAND_TYPE_IMM32_32S_DISP32, false },
1452
    { STRING_COMMA_LEN ("SECIDX16"), { BFD_RELOC_16_SECIDX,
1453
               BFD_RELOC_16_SECIDX },
1454
    { .bitfield = { .imm16 = 1, .disp16 = 1 } }, false },
1455
    { STRING_COMMA_LEN ("RVA"), { BFD_RELOC_RVA,
1456
               BFD_RELOC_RVA },
1457
    OPERAND_TYPE_IMM32_32S_DISP32, false },
1458
    { STRING_COMMA_LEN ("IMGREL"), { BFD_RELOC_RVA,
1459
               BFD_RELOC_RVA },
1460
    OPERAND_TYPE_IMM32_32S_DISP32, false },
1461
#endif
1462
1463
#undef OPERAND_TYPE_IMM32_32S_DISP32
1464
#undef OPERAND_TYPE_IMM32_32S_64_DISP32
1465
#undef OPERAND_TYPE_IMM32_32S_64_DISP32_64
1466
#undef OPERAND_TYPE_IMM64_DISP64
1467
};
1468
#endif
1469

1470
  /* Various efficient no-op patterns for aligning code labels.
1471
     Note: Don't try to assemble the instructions in the comments.
1472
     0L and 0w are not legal.  */
1473
static const unsigned char f32_1[] =
1474
  {0x90};       /* nop      */
1475
static const unsigned char f32_2[] =
1476
  {0x66,0x90};        /* xchg %ax,%ax   */
1477
static const unsigned char f32_3[] =
1478
  {0x8d,0x76,0x00};     /* leal 0(%esi),%esi  */
1479
#define f32_4 (f32_5 + 1) /* leal 0(%esi,%eiz),%esi */
1480
static const unsigned char f32_5[] =
1481
  {0x2e,0x8d,0x74,0x26,0x00};   /* leal %cs:0(%esi,%eiz),%esi */
1482
static const unsigned char f32_6[] =
1483
  {0x8d,0xb6,0x00,0x00,0x00,0x00};  /* leal 0L(%esi),%esi */
1484
#define f32_7 (f32_8 + 1) /* leal 0L(%esi,%eiz),%esi */
1485
static const unsigned char f32_8[] =
1486
  {0x2e,0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal %cs:0L(%esi,%eiz),%esi */
1487
static const unsigned char f64_3[] =
1488
  {0x48,0x89,0xf6};     /* mov %rsi,%rsi  */
1489
static const unsigned char f64_4[] =
1490
  {0x48,0x8d,0x76,0x00};    /* lea 0(%rsi),%rsi */
1491
#define f64_5 (f64_6 + 1)   /* lea 0(%rsi,%riz),%rsi  */
1492
static const unsigned char f64_6[] =
1493
  {0x2e,0x48,0x8d,0x74,0x26,0x00};  /* lea %cs:0(%rsi,%riz),%rsi  */
1494
static const unsigned char f64_7[] =
1495
  {0x48,0x8d,0xb6,0x00,0x00,0x00,0x00}; /* lea 0L(%rsi),%rsi  */
1496
#define f64_8 (f64_9 + 1)   /* lea 0L(%rsi,%riz),%rsi */
1497
static const unsigned char f64_9[] =
1498
  {0x2e,0x48,0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* lea %cs:0L(%rsi,%riz),%rsi */
1499
#define f16_2 (f64_3 + 1)   /* mov %si,%si  */
1500
static const unsigned char f16_3[] =
1501
  {0x8d,0x74,0x00};     /* lea 0(%si),%si */
1502
#define f16_4 (f16_5 + 1)   /* lea 0W(%si),%si */
1503
static const unsigned char f16_5[] =
1504
  {0x2e,0x8d,0xb4,0x00,0x00};   /* lea %cs:0W(%si),%si  */
1505
static const unsigned char jump_disp8[] =
1506
  {0xeb};       /* jmp disp8         */
1507
static const unsigned char jump32_disp32[] =
1508
  {0xe9};       /* jmp disp32        */
1509
static const unsigned char jump16_disp32[] =
1510
  {0x66,0xe9};        /* jmp disp32        */
1511
/* 32-bit NOPs patterns.  */
1512
static const unsigned char *const f32_patt[] = {
1513
  f32_1, f32_2, f32_3, f32_4, f32_5, f32_6, f32_7, f32_8
1514
};
1515
/* 64-bit NOPs patterns.  */
1516
static const unsigned char *const f64_patt[] = {
1517
  f32_1, f32_2, f64_3, f64_4, f64_5, f64_6, f64_7, f64_8, f64_9
1518
};
1519
/* 16-bit NOPs patterns.  */
1520
static const unsigned char *const f16_patt[] = {
1521
  f32_1, f16_2, f16_3, f16_4, f16_5
1522
};
1523
/* nopl (%[re]ax) */
1524
static const unsigned char alt_3[] =
1525
  {0x0f,0x1f,0x00};
1526
/* nopl 0(%[re]ax) */
1527
static const unsigned char alt_4[] =
1528
  {0x0f,0x1f,0x40,0x00};
1529
/* nopl 0(%[re]ax,%[re]ax,1) */
1530
#define alt_5 (alt_6 + 1)
1531
/* nopw 0(%[re]ax,%[re]ax,1) */
1532
static const unsigned char alt_6[] =
1533
  {0x66,0x0f,0x1f,0x44,0x00,0x00};
1534
/* nopl 0L(%[re]ax) */
1535
static const unsigned char alt_7[] =
1536
  {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1537
/* nopl 0L(%[re]ax,%[re]ax,1) */
1538
#define alt_8 (alt_9 + 1)
1539
/* nopw 0L(%[re]ax,%[re]ax,1) */
1540
static const unsigned char alt_9[] =
1541
  {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1542
/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1543
#define alt_10 (alt_11 + 1)
1544
/* data16 nopw %cs:0L(%eax,%eax,1) */
1545
static const unsigned char alt_11[] =
1546
  {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1547
/* 32-bit and 64-bit NOPs patterns.  */
1548
static const unsigned char *const alt_patt[] = {
1549
  f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
1550
  alt_9, alt_10, alt_11
1551
};
1552
#define alt64_9 (alt64_15 + 6)    /* nopq 0L(%rax,%rax,1)  */
1553
#define alt64_10 (alt64_15 + 5)   /* cs nopq 0L(%rax,%rax,1)  */
1554
/* data16 cs nopq 0L(%rax,%rax,1)  */
1555
#define alt64_11 (alt64_15 + 4)
1556
/* data16 data16 cs nopq 0L(%rax,%rax,1)  */
1557
#define alt64_12 (alt64_15 + 3)
1558
/* data16 data16 data16 cs nopq 0L(%rax,%rax,1)  */
1559
#define alt64_13 (alt64_15 + 2)
1560
/* data16 data16 data16 data16 cs nopq 0L(%rax,%rax,1)  */
1561
#define alt64_14 (alt64_15 + 1)
1562
/* data16 data16 data16 data16 data16 cs nopq 0L(%rax,%rax,1)  */
1563
static const unsigned char alt64_15[] =
1564
  {0x66,0x66,0x66,0x66,0x66,0x2e,0x48,
1565
   0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1566
/* Long 64-bit NOPs patterns.  */
1567
static const unsigned char *const alt64_patt[] = {
1568
  f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
1569
  alt64_9, alt64_10, alt64_11,alt64_12, alt64_13, alt64_14, alt64_15
1570
};
1571
1572
static INLINE int
1573
fits_in_imm7 (offsetT num)
1574
0
{
1575
0
  return (num & 0x7f) == num;
1576
0
}
1577
1578
static INLINE int
1579
fits_in_imm31 (offsetT num)
1580
0
{
1581
0
  return (num & 0x7fffffff) == num;
1582
0
}
1583
1584
/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1585
   single NOP instruction LIMIT.  */
1586
1587
void
1588
i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
1589
0
{
1590
0
  const unsigned char *const *patt = NULL;
1591
0
  int max_single_nop_size;
1592
  /* Maximum number of NOPs before switching to jump over NOPs.  */
1593
0
  int max_number_of_nops;
1594
1595
0
  switch (fragP->fr_type)
1596
0
    {
1597
0
    case rs_fill_nop:
1598
0
    case rs_align_code:
1599
0
      break;
1600
0
    case rs_machine_dependent:
1601
      /* Allow NOP padding for jumps and calls.  */
1602
0
      if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1603
0
    || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1604
0
  break;
1605
      /* Fall through.  */
1606
0
    default:
1607
0
      return;
1608
0
    }
1609
1610
  /* We need to decide which NOP sequence to use for 32bit and
1611
     64bit. When -mtune= is used:
1612
1613
     1. For PROCESSOR_I?86, PROCESSOR_PENTIUM, PROCESSOR_IAMCU, and
1614
     PROCESSOR_GENERIC32, f32_patt will be used.
1615
     2. For the rest, alt_patt will be used.
1616
1617
     When -mtune= isn't used, alt_patt will be used if
1618
     cpu_arch_isa_flags has CpuNop.  Otherwise, f32_patt/f64_patt will
1619
     be used.
1620
1621
     When -march= or .arch is used, we can't use anything beyond
1622
     cpu_arch_isa_flags.   */
1623
1624
0
  if (fragP->tc_frag_data.code == CODE_16BIT)
1625
0
    {
1626
0
      patt = f16_patt;
1627
0
      max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1628
      /* Limit number of NOPs to 2 in 16-bit mode.  */
1629
0
      max_number_of_nops = 2;
1630
0
    }
1631
0
  else
1632
0
    {
1633
0
      patt = fragP->tc_frag_data.code == CODE_64BIT ? f64_patt : f32_patt;
1634
0
      if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
1635
0
  {
1636
    /* PROCESSOR_UNKNOWN means that all ISAs may be used, unless
1637
       explicitly disabled.  */
1638
0
    switch (fragP->tc_frag_data.tune)
1639
0
      {
1640
0
      case PROCESSOR_UNKNOWN:
1641
        /* We use cpu_arch_isa_flags to check if we SHOULD
1642
     optimize with nops.  */
1643
0
        if (fragP->tc_frag_data.isanop)
1644
0
    patt = alt_patt;
1645
0
        break;
1646
1647
0
      case PROCESSOR_CORE:
1648
0
      case PROCESSOR_CORE2:
1649
0
      case PROCESSOR_COREI7:
1650
0
        if (fragP->tc_frag_data.cpunop)
1651
0
    {
1652
0
      if (fragP->tc_frag_data.code == CODE_64BIT)
1653
0
        patt = alt64_patt;
1654
0
      else
1655
0
        patt = alt_patt;
1656
0
    }
1657
0
        break;
1658
1659
0
      case PROCESSOR_PENTIUMPRO:
1660
0
      case PROCESSOR_PENTIUM4:
1661
0
      case PROCESSOR_NOCONA:
1662
0
      case PROCESSOR_GENERIC64:
1663
0
      case PROCESSOR_K6:
1664
0
      case PROCESSOR_ATHLON:
1665
0
      case PROCESSOR_K8:
1666
0
      case PROCESSOR_AMDFAM10:
1667
0
      case PROCESSOR_BD:
1668
0
      case PROCESSOR_ZNVER:
1669
0
      case PROCESSOR_BT:
1670
0
        if (fragP->tc_frag_data.cpunop)
1671
0
    patt = alt_patt;
1672
0
        break;
1673
1674
0
      case PROCESSOR_I386:
1675
0
      case PROCESSOR_I486:
1676
0
      case PROCESSOR_PENTIUM:
1677
0
      case PROCESSOR_I686:
1678
0
      case PROCESSOR_IAMCU:
1679
0
      case PROCESSOR_GENERIC32:
1680
0
        break;
1681
0
      case PROCESSOR_NONE:
1682
0
        abort ();
1683
0
      }
1684
0
  }
1685
0
      else
1686
0
  {
1687
0
    switch (fragP->tc_frag_data.tune)
1688
0
      {
1689
0
      case PROCESSOR_UNKNOWN:
1690
        /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
1691
     PROCESSOR_UNKNOWN.  */
1692
0
        abort ();
1693
0
        break;
1694
1695
0
      default:
1696
        /* We use cpu_arch_isa_flags to check if we CAN optimize
1697
     with nops.  */
1698
0
        if (fragP->tc_frag_data.isanop)
1699
0
    patt = alt_patt;
1700
0
        break;
1701
1702
0
      case PROCESSOR_NONE:
1703
0
        abort ();
1704
0
      }
1705
0
  }
1706
1707
0
      if (patt != alt_patt && patt != alt64_patt)
1708
0
  {
1709
0
    max_single_nop_size = patt == f32_patt ? ARRAY_SIZE (f32_patt)
1710
0
             : ARRAY_SIZE (f64_patt);
1711
    /* Limit number of NOPs to 2 for older processors.  */
1712
0
    max_number_of_nops = 2;
1713
0
  }
1714
0
      else
1715
0
  {
1716
0
    max_single_nop_size = patt == alt_patt
1717
0
        ? ARRAY_SIZE (alt_patt)
1718
0
        : ARRAY_SIZE (alt64_patt);
1719
    /* Limit number of NOPs to 7 for newer processors.  */
1720
0
    max_number_of_nops = 7;
1721
0
  }
1722
0
    }
1723
1724
0
  if (limit == 0)
1725
0
    limit = max_single_nop_size;
1726
1727
0
  if (limit > max_single_nop_size || limit < 1)
1728
0
    {
1729
0
      as_bad_where (fragP->fr_file, fragP->fr_line,
1730
0
        _("invalid single nop size: %d "
1731
0
          "(expect within [0, %d])"),
1732
0
        limit, max_single_nop_size);
1733
0
      return;
1734
0
    }
1735
1736
  /* Emit a plain NOP first when the last thing we saw may not have been
1737
     a proper instruction (e.g. a stand-alone prefix or .byte).  */
1738
0
  if (!fragP->tc_frag_data.last_insn_normal)
1739
0
    {
1740
0
      *where++ = 0x90;
1741
0
      --count;
1742
0
    }
1743
1744
0
  if ((count / max_single_nop_size) > max_number_of_nops)
1745
0
    {
1746
      /* Generate jump over NOPs.  */
1747
0
      offsetT disp = count - 2;
1748
0
      if (fits_in_imm7 (disp))
1749
0
  {
1750
    /* Use "jmp disp8" if possible.  */
1751
0
    count = disp;
1752
0
    where[0] = jump_disp8[0];
1753
0
    where[1] = count;
1754
0
    where += 2;
1755
0
  }
1756
0
      else
1757
0
  {
1758
0
    unsigned int size_of_jump;
1759
1760
0
    if (flag_code == CODE_16BIT)
1761
0
      {
1762
0
        where[0] = jump16_disp32[0];
1763
0
        where[1] = jump16_disp32[1];
1764
0
        size_of_jump = 2;
1765
0
      }
1766
0
    else
1767
0
      {
1768
0
        where[0] = jump32_disp32[0];
1769
0
        size_of_jump = 1;
1770
0
      }
1771
1772
0
    count -= size_of_jump + 4;
1773
0
    if (!fits_in_imm31 (count))
1774
0
      {
1775
0
        as_bad_where (fragP->fr_file, fragP->fr_line,
1776
0
          _("jump over nop padding out of range"));
1777
0
        return;
1778
0
      }
1779
1780
0
    md_number_to_chars (where + size_of_jump, count, 4);
1781
0
    where += size_of_jump + 4;
1782
0
  }
1783
0
    }
1784
1785
0
  int non_repeat = count % limit;
1786
0
  if (non_repeat)
1787
0
    {
1788
0
      memcpy (where, patt[non_repeat - 1], non_repeat);
1789
0
      where += non_repeat;
1790
0
      count -= non_repeat;
1791
0
    }
1792
1793
0
  if (fragP->fr_type != rs_machine_dependent)
1794
0
    {
1795
      /* Set up the frag so that everything we have emitted so far is
1796
   included in fr_fix.  The repeating larger nop only needs to
1797
   be written once to the frag memory.  */
1798
0
      fragP->fr_fix = where - fragP->fr_literal;
1799
0
      if (count != 0)
1800
0
  {
1801
0
    fragP->fr_var = limit;
1802
0
    count = limit;
1803
0
  }
1804
0
    }
1805
1806
0
  const unsigned char *nops = patt[limit - 1];
1807
0
  while (count)
1808
0
    {
1809
0
      memcpy (where, nops, limit);
1810
0
      where += limit;
1811
0
      count -= limit;
1812
0
    }
1813
0
}
1814
1815
static INLINE int
1816
operand_type_all_zero (const union i386_operand_type *x)
1817
32.0k
{
1818
32.0k
  switch (ARRAY_SIZE(x->array))
1819
32.0k
    {
1820
0
    case 3:
1821
0
      if (x->array[2])
1822
0
  return 0;
1823
      /* Fall through.  */
1824
0
    case 2:
1825
0
      if (x->array[1])
1826
0
  return 0;
1827
      /* Fall through.  */
1828
32.0k
    case 1:
1829
32.0k
      return !x->array[0];
1830
0
    default:
1831
0
      abort ();
1832
32.0k
    }
1833
32.0k
}
1834
1835
static INLINE void
1836
operand_type_set (union i386_operand_type *x, unsigned int v)
1837
24.7k
{
1838
24.7k
  switch (ARRAY_SIZE(x->array))
1839
24.7k
    {
1840
0
    case 3:
1841
0
      x->array[2] = v;
1842
      /* Fall through.  */
1843
0
    case 2:
1844
0
      x->array[1] = v;
1845
      /* Fall through.  */
1846
24.7k
    case 1:
1847
24.7k
      x->array[0] = v;
1848
      /* Fall through.  */
1849
24.7k
      break;
1850
0
    default:
1851
0
      abort ();
1852
24.7k
    }
1853
1854
24.7k
  x->bitfield.class = ClassNone;
1855
24.7k
  x->bitfield.instance = InstanceNone;
1856
24.7k
}
1857
1858
static INLINE int
1859
operand_type_equal (const union i386_operand_type *x,
1860
        const union i386_operand_type *y)
1861
15
{
1862
15
  switch (ARRAY_SIZE(x->array))
1863
15
    {
1864
0
    case 3:
1865
0
      if (x->array[2] != y->array[2])
1866
0
  return 0;
1867
      /* Fall through.  */
1868
0
    case 2:
1869
0
      if (x->array[1] != y->array[1])
1870
0
  return 0;
1871
      /* Fall through.  */
1872
15
    case 1:
1873
15
      return x->array[0] == y->array[0];
1874
0
      break;
1875
0
    default:
1876
0
      abort ();
1877
15
    }
1878
15
}
1879
1880
static INLINE bool
1881
_is_cpu (const i386_cpu_attr *a, enum i386_cpu cpu)
1882
396k
{
1883
396k
  switch (cpu)
1884
396k
    {
1885
11.4k
    case Cpu287:      return a->bitfield.cpu287;
1886
11.4k
    case Cpu387:      return a->bitfield.cpu387;
1887
0
    case Cpu3dnow:    return a->bitfield.cpu3dnow;
1888
0
    case Cpu3dnowA:   return a->bitfield.cpu3dnowa;
1889
21.9k
    case CpuAVX:      return a->bitfield.cpuavx;
1890
5
    case CpuHLE:      return a->bitfield.cpuhle;
1891
11.6k
    case CpuAVX512F:  return a->bitfield.cpuavx512f;
1892
11.4k
    case CpuAVX512VL: return a->bitfield.cpuavx512vl;
1893
8.55k
    case CpuAPX_F:    return a->bitfield.cpuapx_f;
1894
0
    case CpuAVX10_2:  return a->bitfield.cpuavx10_2;
1895
0
    case CpuAMX_TRANSPOSE:  return a->bitfield.cpuamx_transpose;
1896
0
    case Cpu64:       return a->bitfield.cpu64;
1897
0
    case CpuNo64:     return a->bitfield.cpuno64;
1898
319k
    default:
1899
319k
      gas_assert (cpu < CpuAttrEnums);
1900
396k
    }
1901
319k
  return a->bitfield.isa == cpu + 1u;
1902
396k
}
1903
1904
static INLINE bool
1905
is_cpu (const insn_template *t, enum i386_cpu cpu)
1906
385k
{
1907
385k
  return _is_cpu(&t->cpu, cpu);
1908
385k
}
1909
1910
static INLINE bool
1911
maybe_cpu (const insn_template *t, enum i386_cpu cpu)
1912
10.4k
{
1913
10.4k
  return _is_cpu(&t->cpu_any, cpu);
1914
10.4k
}
1915
1916
static i386_cpu_flags cpu_flags_from_attr (i386_cpu_attr a)
1917
165k
{
1918
165k
  const unsigned int bps = sizeof (a.array[0]) * CHAR_BIT;
1919
165k
  i386_cpu_flags f = { .array[0] = 0 };
1920
1921
165k
  switch (ARRAY_SIZE (a.array))
1922
165k
    {
1923
165k
    case 1:
1924
165k
      f.array[CpuAttrEnums / bps]
1925
165k
#ifndef WORDS_BIGENDIAN
1926
165k
  |= (a.array[0] >> CpuIsaBits) << (CpuAttrEnums % bps);
1927
#else
1928
  |= (a.array[0] << CpuIsaBits) >> (CpuAttrEnums % bps);
1929
#endif
1930
165k
      if (CpuMax / bps > CpuAttrEnums / bps)
1931
165k
  f.array[CpuAttrEnums / bps + 1]
1932
165k
#ifndef WORDS_BIGENDIAN
1933
165k
    = (a.array[0] >> CpuIsaBits) >> (bps - CpuAttrEnums % bps);
1934
#else
1935
    = (a.array[0] << CpuIsaBits) << (bps - CpuAttrEnums % bps);
1936
#endif
1937
165k
      break;
1938
1939
0
    default:
1940
0
      abort ();
1941
165k
    }
1942
1943
165k
  if (a.bitfield.isa)
1944
34.8k
#ifndef WORDS_BIGENDIAN
1945
34.8k
    f.array[(a.bitfield.isa - 1) / bps] |= 1u << ((a.bitfield.isa - 1) % bps);
1946
#else
1947
    f.array[(a.bitfield.isa - 1) / bps] |= 1u << (~(a.bitfield.isa - 1) % bps);
1948
#endif
1949
1950
165k
  return f;
1951
165k
}
1952
1953
static INLINE int
1954
cpu_flags_all_zero (const union i386_cpu_flags *x)
1955
159k
{
1956
159k
  switch (ARRAY_SIZE(x->array))
1957
159k
    {
1958
159k
    case 6:
1959
159k
      if (x->array[5])
1960
160
  return 0;
1961
      /* Fall through.  */
1962
159k
    case 5:
1963
159k
      if (x->array[4])
1964
14.2k
  return 0;
1965
      /* Fall through.  */
1966
145k
    case 4:
1967
145k
      if (x->array[3])
1968
89
  return 0;
1969
      /* Fall through.  */
1970
145k
    case 3:
1971
145k
      if (x->array[2])
1972
140
  return 0;
1973
      /* Fall through.  */
1974
144k
    case 2:
1975
144k
      if (x->array[1])
1976
85
  return 0;
1977
      /* Fall through.  */
1978
144k
    case 1:
1979
144k
      return !x->array[0];
1980
0
    default:
1981
0
      abort ();
1982
159k
    }
1983
159k
}
1984
1985
static INLINE int
1986
cpu_flags_equal (const union i386_cpu_flags *x,
1987
     const union i386_cpu_flags *y)
1988
35.4k
{
1989
35.4k
  switch (ARRAY_SIZE(x->array))
1990
35.4k
    {
1991
35.4k
    case 6:
1992
35.4k
      if (x->array[5] != y->array[5])
1993
6
  return 0;
1994
      /* Fall through.  */
1995
35.4k
    case 5:
1996
35.4k
      if (x->array[4] != y->array[4])
1997
6.94k
  return 0;
1998
      /* Fall through.  */
1999
28.5k
    case 4:
2000
28.5k
      if (x->array[3] != y->array[3])
2001
10
  return 0;
2002
      /* Fall through.  */
2003
28.5k
    case 3:
2004
28.5k
      if (x->array[2] != y->array[2])
2005
108
  return 0;
2006
      /* Fall through.  */
2007
28.4k
    case 2:
2008
28.4k
      if (x->array[1] != y->array[1])
2009
4
  return 0;
2010
      /* Fall through.  */
2011
28.4k
    case 1:
2012
28.4k
      return x->array[0] == y->array[0];
2013
0
      break;
2014
0
    default:
2015
0
      abort ();
2016
35.4k
    }
2017
35.4k
}
2018
2019
static INLINE int
2020
cpu_flags_check_cpu64 (const insn_template *t)
2021
77.6k
{
2022
77.6k
  return flag_code == CODE_64BIT
2023
77.6k
   ? !t->cpu.bitfield.cpuno64
2024
77.6k
   : !t->cpu.bitfield.cpu64;
2025
77.6k
}
2026
2027
static INLINE i386_cpu_flags
2028
cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
2029
74.1k
{
2030
74.1k
  switch (ARRAY_SIZE (x.array))
2031
74.1k
    {
2032
74.1k
    case 6:
2033
74.1k
      x.array [5] &= y.array [5];
2034
      /* Fall through.  */
2035
74.1k
    case 5:
2036
74.1k
      x.array [4] &= y.array [4];
2037
      /* Fall through.  */
2038
74.1k
    case 4:
2039
74.1k
      x.array [3] &= y.array [3];
2040
      /* Fall through.  */
2041
74.1k
    case 3:
2042
74.1k
      x.array [2] &= y.array [2];
2043
      /* Fall through.  */
2044
74.1k
    case 2:
2045
74.1k
      x.array [1] &= y.array [1];
2046
      /* Fall through.  */
2047
74.1k
    case 1:
2048
74.1k
      x.array [0] &= y.array [0];
2049
74.1k
      break;
2050
0
    default:
2051
0
      abort ();
2052
74.1k
    }
2053
74.1k
  return x;
2054
74.1k
}
2055
2056
static INLINE i386_cpu_flags
2057
cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
2058
114
{
2059
114
  switch (ARRAY_SIZE (x.array))
2060
114
    {
2061
114
    case 6:
2062
114
      x.array [5] |= y.array [5];
2063
      /* Fall through.  */
2064
114
    case 5:
2065
114
      x.array [4] |= y.array [4];
2066
      /* Fall through.  */
2067
114
    case 4:
2068
114
      x.array [3] |= y.array [3];
2069
      /* Fall through.  */
2070
114
    case 3:
2071
114
      x.array [2] |= y.array [2];
2072
      /* Fall through.  */
2073
114
    case 2:
2074
114
      x.array [1] |= y.array [1];
2075
      /* Fall through.  */
2076
114
    case 1:
2077
114
      x.array [0] |= y.array [0];
2078
114
      break;
2079
0
    default:
2080
0
      abort ();
2081
114
    }
2082
114
  return x;
2083
114
}
2084
2085
static INLINE i386_cpu_flags
2086
cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
2087
9.70k
{
2088
9.70k
  switch (ARRAY_SIZE (x.array))
2089
9.70k
    {
2090
9.70k
    case 6:
2091
9.70k
      x.array [5] &= ~y.array [5];
2092
      /* Fall through.  */
2093
9.70k
    case 5:
2094
9.70k
      x.array [4] &= ~y.array [4];
2095
      /* Fall through.  */
2096
9.70k
    case 4:
2097
9.70k
      x.array [3] &= ~y.array [3];
2098
      /* Fall through.  */
2099
9.70k
    case 3:
2100
9.70k
      x.array [2] &= ~y.array [2];
2101
      /* Fall through.  */
2102
9.70k
    case 2:
2103
9.70k
      x.array [1] &= ~y.array [1];
2104
      /* Fall through.  */
2105
9.70k
    case 1:
2106
9.70k
      x.array [0] &= ~y.array [0];
2107
9.70k
      break;
2108
0
    default:
2109
0
      abort ();
2110
9.70k
    }
2111
9.70k
  return x;
2112
9.70k
}
2113
2114
static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
2115
2116
static INLINE bool need_evex_encoding (const insn_template *t)
2117
75
{
2118
75
  return pp.encoding == encoding_evex
2119
68
  || pp.encoding == encoding_evex512
2120
68
  || pp.has_nf
2121
68
  || (t->opcode_modifier.vex && pp.encoding == encoding_egpr)
2122
68
  || i.mask.reg;
2123
75
}
2124
2125
147k
#define CPU_FLAGS_ARCH_MATCH    0x1
2126
140k
#define CPU_FLAGS_64BIT_MATCH   0x2
2127
2128
#define CPU_FLAGS_PERFECT_MATCH \
2129
77.4k
  (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
2130
2131
static INLINE bool set_oszc_flags (unsigned int oszc_shift)
2132
0
{
2133
0
  if (i.oszc_flags & oszc_shift)
2134
0
    {
2135
0
      as_bad (_("same oszc flag used twice"));
2136
0
      return false;
2137
0
    }
2138
0
  i.oszc_flags |= oszc_shift;
2139
0
  return true;
2140
0
}
2141
2142
/* Handle SCC OSZC flags.  */
2143
2144
static int
2145
check_Scc_OszcOperations (const char *l)
2146
35
{
2147
35
  const char *suffix_string = l;
2148
2149
230
  while (is_whitespace (*suffix_string))
2150
195
    suffix_string++;
2151
2152
  /* If {oszc flags} is absent, just return.  */
2153
35
  if (*suffix_string != '{')
2154
7
    return 0;
2155
2156
  /* Skip '{'.  */
2157
28
  suffix_string++;
2158
2159
  /* For .insn require 'scc=' as the first element.  */
2160
28
  if (dot_insn ())
2161
18
    {
2162
18
      char *copy;
2163
18
      valueT val;
2164
2165
24
      while (is_whitespace (*suffix_string))
2166
6
  suffix_string++;
2167
2168
18
      if (strncasecmp (suffix_string, "scc", 3) == 0)
2169
0
  suffix_string += 3;
2170
18
      else
2171
18
  {
2172
18
    as_bad (_("unrecognized pseudo-suffix"));
2173
18
    return -1;
2174
18
  }
2175
2176
0
      while (is_whitespace (*suffix_string))
2177
0
  suffix_string++;
2178
2179
0
      if (*suffix_string == '=')
2180
0
  suffix_string++;
2181
0
      else
2182
0
  {
2183
0
    as_bad (_("unrecognized pseudo-suffix"));
2184
0
    return -1;
2185
0
  }
2186
2187
0
      copy = xstrdup (suffix_string);
2188
      /* No need to save/restore input_line_pointer; that's done in the
2189
   caller already.  */
2190
0
      input_line_pointer = copy;
2191
0
      val = get_absolute_expression ();
2192
0
      suffix_string += input_line_pointer - copy;
2193
0
      free (copy);
2194
2195
0
      if (val > 0xf)
2196
0
  {
2197
0
    as_bad (_("scc= value must be between 0 and 15 (decimal)"));
2198
0
    return -1;
2199
0
  }
2200
2201
0
      i.scc = val;
2202
2203
      /* Permit dfv= to be absent (implying all flag values being zero).  */
2204
0
      if (*suffix_string == '}')
2205
0
  return suffix_string + 1 - l;
2206
2207
0
      if (*suffix_string != ',')
2208
0
  goto bad;
2209
0
      suffix_string++;
2210
0
    }
2211
2212
  /* Parse 'dfv='.  */
2213
68
  while (is_whitespace (*suffix_string))
2214
58
    suffix_string++;
2215
2216
10
  if (strncasecmp (suffix_string, "dfv", 3) == 0)
2217
0
    suffix_string += 3;
2218
10
  else
2219
10
    {
2220
10
      as_bad (_("unrecognized pseudo-suffix"));
2221
10
      return -1;
2222
10
    }
2223
2224
0
  while (is_whitespace (*suffix_string))
2225
0
    suffix_string++;
2226
2227
0
  if (*suffix_string == '=')
2228
0
    suffix_string++;
2229
0
  else
2230
0
    {
2231
0
      as_bad (_("unrecognized pseudo-suffix"));
2232
0
      return -1;
2233
0
    }
2234
2235
  /* Parse 'of, sf, zf, cf}'.  */
2236
0
  while (*suffix_string)
2237
0
    {
2238
0
      while (is_whitespace (*suffix_string))
2239
0
  suffix_string++;
2240
2241
      /* Return for '{dfv=}'.  */
2242
0
      if (*suffix_string == '}')
2243
0
  return suffix_string + 1 - l;
2244
2245
0
      if (strncasecmp (suffix_string, "of", 2) == 0)
2246
0
  {
2247
0
    if (!set_oszc_flags (OSZC_OF))
2248
0
      return -1;
2249
0
  }
2250
0
      else if (strncasecmp (suffix_string, "sf", 2) == 0)
2251
0
  {
2252
0
    if (!set_oszc_flags (OSZC_SF))
2253
0
      return -1;
2254
0
  }
2255
0
      else if (strncasecmp (suffix_string, "zf", 2) == 0)
2256
0
  {
2257
0
    if (!set_oszc_flags (OSZC_ZF))
2258
0
      return -1;
2259
0
  }
2260
0
      else if (strncasecmp (suffix_string, "cf", 2) == 0)
2261
0
  {
2262
0
    if (!set_oszc_flags (OSZC_CF))
2263
0
      return -1;
2264
0
  }
2265
0
      else
2266
0
  {
2267
0
    as_bad (_("unrecognized oszc flags or illegal `,' in pseudo-suffix"));
2268
0
    return -1;
2269
0
  }
2270
2271
0
      suffix_string += 2;
2272
2273
0
      while (is_whitespace (*suffix_string))
2274
0
  suffix_string++;
2275
2276
0
      if (*suffix_string == '}')
2277
0
  return ++suffix_string - l;
2278
2279
0
      if (*suffix_string != ',')
2280
0
  break;
2281
0
      suffix_string ++;
2282
0
    }
2283
2284
0
 bad:
2285
0
  as_bad (_("missing `}' or `,' in pseudo-suffix"));
2286
0
  return -1;
2287
0
}
2288
2289
/* Return CPU flags match bits. */
2290
2291
static int
2292
cpu_flags_match (const insn_template *t)
2293
77.6k
{
2294
77.6k
  i386_cpu_flags cpu, active, all = cpu_flags_from_attr (t->cpu);
2295
77.6k
  i386_cpu_flags any = cpu_flags_from_attr (t->cpu_any);
2296
77.6k
  int match = cpu_flags_check_cpu64 (t) ? CPU_FLAGS_64BIT_MATCH : 0;
2297
2298
77.6k
  all.bitfield.cpu64 = 0;
2299
77.6k
  all.bitfield.cpuno64 = 0;
2300
77.6k
  gas_assert (!any.bitfield.cpu64);
2301
77.6k
  gas_assert (!any.bitfield.cpuno64);
2302
2303
77.6k
  if (cpu_flags_all_zero (&all) && cpu_flags_all_zero (&any))
2304
42.2k
    {
2305
      /* This instruction is available on all archs.  */
2306
42.2k
      return match | CPU_FLAGS_ARCH_MATCH;
2307
42.2k
    }
2308
2309
  /* This instruction is available only on some archs.  */
2310
2311
  /* Dual VEX/EVEX templates may need stripping of one of the flags.  */
2312
35.4k
  if (t->opcode_modifier.vex && t->opcode_modifier.evex)
2313
73
    {
2314
      /* Dual AVX/AVX512 templates need to retain AVX512* only if we already
2315
   know that EVEX encoding will be needed.  */
2316
73
      if ((any.bitfield.cpuavx || any.bitfield.cpuavx2 || any.bitfield.cpufma)
2317
63
    && (any.bitfield.cpuavx512f || any.bitfield.cpuavx512vl))
2318
63
  {
2319
63
    if (need_evex_encoding (t))
2320
4
      {
2321
4
        any.bitfield.cpuavx = 0;
2322
4
        any.bitfield.cpuavx2 = 0;
2323
4
        any.bitfield.cpufma = 0;
2324
4
      }
2325
    /* need_evex_encoding(t) isn't reliable before operands were
2326
       parsed.  */
2327
59
    else if (i.operands)
2328
0
      {
2329
0
        any.bitfield.cpuavx512f = 0;
2330
0
        any.bitfield.cpuavx512vl = 0;
2331
0
      }
2332
63
  }
2333
2334
      /* Dual non-APX/APX templates need massaging from what APX_F() in the
2335
         opcode table has produced.  While the direct transformation of the
2336
         incoming cpuid&(cpuid|APX_F) would be to cpuid&(cpuid) / cpuid&(APX_F)
2337
         respectively, it's cheaper to move to just cpuid / cpuid&APX_F
2338
         instead.  */
2339
73
      if (any.bitfield.cpuapx_f
2340
10
    && (any.bitfield.cpubmi || any.bitfield.cpubmi2
2341
9
        || any.bitfield.cpuavx512f || any.bitfield.cpuavx512bw
2342
9
        || any.bitfield.cpuavx512dq || any.bitfield.cpuamx_tile
2343
5
        || any.bitfield.cpucmpccxadd || any.bitfield.cpuuser_msr
2344
5
        || any.bitfield.cpumsr_imm || any.bitfield.cpuamx_transpose
2345
0
        || any.bitfield.cpuamx_movrs))
2346
10
  {
2347
    /* These checks (verifying that APX_F() was properly used in the
2348
       opcode table entry) make sure there's no need for an "else" to
2349
       the "if()" below.  */
2350
10
    gas_assert (!cpu_flags_all_zero (&all));
2351
2352
10
    cpu = cpu_flags_and (all, any);
2353
10
    gas_assert (cpu_flags_equal (&cpu, &all));
2354
2355
10
    if (need_evex_encoding (t))
2356
2
      all = any;
2357
2358
10
    memset (&any, 0, sizeof (any));
2359
10
  }
2360
73
    }
2361
35.3k
  else if (t->opcode_modifier.evex
2362
     /* Implicitly !t->opcode_modifier.vex.  */
2363
12.5k
     && all.bitfield.cpuapx_f
2364
0
     && (t->opcode_modifier.nf
2365
0
         || (all.bitfield.cpuadx && t->opcode_modifier.vexvvvv)))
2366
0
    {
2367
      /* APX_NDD can't be combined with other ISAs in the opcode table.
2368
   Respective entries (ADCX, ADOX, LZCNT, POPCNT, and TZCNT) use APX_F
2369
   instead, which are amended here.  No need to clear cpuapx_f, though. */
2370
0
      all.bitfield.cpuapx_ndd = true;
2371
0
    }
2372
2373
35.4k
  if (flag_code != CODE_64BIT)
2374
9.68k
    active = cpu_flags_and_not (cpu_arch_flags, cpu_64_flags);
2375
25.7k
  else
2376
25.7k
    active = cpu_arch_flags;
2377
35.4k
  cpu = cpu_flags_and (all, active);
2378
35.4k
  if (cpu_flags_equal (&cpu, &all))
2379
28.3k
    {
2380
      /* AVX and AVX2 present at the same time express an operand size
2381
   dependency - strip AVX2 for the purposes here.  The operand size
2382
   dependent check occurs in check_vecOperands().  */
2383
28.3k
      if (any.bitfield.cpuavx && any.bitfield.cpuavx2)
2384
0
  any.bitfield.cpuavx2 = 0;
2385
2386
28.3k
      cpu = cpu_flags_and (any, active);
2387
28.3k
      if (cpu_flags_all_zero (&any) || !cpu_flags_all_zero (&cpu))
2388
28.3k
  match |= CPU_FLAGS_ARCH_MATCH;
2389
28.3k
    }
2390
35.4k
  return match;
2391
35.4k
}
2392
2393
static INLINE i386_operand_type
2394
operand_type_and (i386_operand_type x, i386_operand_type y)
2395
91.3k
{
2396
91.3k
  if (x.bitfield.class != y.bitfield.class)
2397
32.7k
    x.bitfield.class = ClassNone;
2398
91.3k
  if (x.bitfield.instance != y.bitfield.instance)
2399
5.92k
    x.bitfield.instance = InstanceNone;
2400
2401
91.3k
  switch (ARRAY_SIZE (x.array))
2402
91.3k
    {
2403
0
    case 3:
2404
0
      x.array [2] &= y.array [2];
2405
      /* Fall through.  */
2406
0
    case 2:
2407
0
      x.array [1] &= y.array [1];
2408
      /* Fall through.  */
2409
91.3k
    case 1:
2410
91.3k
      x.array [0] &= y.array [0];
2411
91.3k
      break;
2412
0
    default:
2413
0
      abort ();
2414
91.3k
    }
2415
91.3k
  return x;
2416
91.3k
}
2417
2418
static INLINE i386_operand_type
2419
operand_type_and_not (i386_operand_type x, i386_operand_type y)
2420
22.3k
{
2421
22.3k
  gas_assert (y.bitfield.class == ClassNone);
2422
22.3k
  gas_assert (y.bitfield.instance == InstanceNone);
2423
2424
22.3k
  switch (ARRAY_SIZE (x.array))
2425
22.3k
    {
2426
0
    case 3:
2427
0
      x.array [2] &= ~y.array [2];
2428
      /* Fall through.  */
2429
0
    case 2:
2430
0
      x.array [1] &= ~y.array [1];
2431
      /* Fall through.  */
2432
22.3k
    case 1:
2433
22.3k
      x.array [0] &= ~y.array [0];
2434
22.3k
      break;
2435
0
    default:
2436
0
      abort ();
2437
22.3k
    }
2438
22.3k
  return x;
2439
22.3k
}
2440
2441
static INLINE i386_operand_type
2442
operand_type_or (i386_operand_type x, i386_operand_type y)
2443
38.8k
{
2444
38.8k
  gas_assert (x.bitfield.class == ClassNone ||
2445
38.8k
              y.bitfield.class == ClassNone ||
2446
38.8k
              x.bitfield.class == y.bitfield.class);
2447
38.8k
  gas_assert (x.bitfield.instance == InstanceNone ||
2448
38.8k
              y.bitfield.instance == InstanceNone ||
2449
38.8k
              x.bitfield.instance == y.bitfield.instance);
2450
2451
38.8k
  switch (ARRAY_SIZE (x.array))
2452
38.8k
    {
2453
0
    case 3:
2454
0
      x.array [2] |= y.array [2];
2455
      /* Fall through.  */
2456
0
    case 2:
2457
0
      x.array [1] |= y.array [1];
2458
      /* Fall through.  */
2459
38.8k
    case 1:
2460
38.8k
      x.array [0] |= y.array [0];
2461
38.8k
      break;
2462
0
    default:
2463
0
      abort ();
2464
38.8k
    }
2465
38.8k
  return x;
2466
38.8k
}
2467
2468
static INLINE i386_operand_type
2469
operand_type_xor (i386_operand_type x, i386_operand_type y)
2470
0
{
2471
0
  gas_assert (y.bitfield.class == ClassNone);
2472
0
  gas_assert (y.bitfield.instance == InstanceNone);
2473
2474
0
  switch (ARRAY_SIZE (x.array))
2475
0
    {
2476
0
    case 3:
2477
0
      x.array [2] ^= y.array [2];
2478
      /* Fall through.  */
2479
0
    case 2:
2480
0
      x.array [1] ^= y.array [1];
2481
      /* Fall through.  */
2482
0
    case 1:
2483
0
      x.array [0] ^= y.array [0];
2484
0
      break;
2485
0
    default:
2486
0
      abort ();
2487
0
    }
2488
0
  return x;
2489
0
}
2490
2491
static const i386_operand_type anydisp = {
2492
  .bitfield = { .disp8 = 1, .disp16 = 1, .disp32 = 1, .disp64 = 1 }
2493
};
2494
2495
enum operand_type
2496
{
2497
  reg,
2498
  imm,
2499
  disp,
2500
  anymem
2501
};
2502
2503
static INLINE int
2504
operand_type_check (i386_operand_type t, enum operand_type c)
2505
121k
{
2506
121k
  switch (c)
2507
121k
    {
2508
0
    case reg:
2509
0
      return t.bitfield.class == Reg;
2510
2511
32.3k
    case imm:
2512
32.3k
      return (t.bitfield.imm8
2513
29.1k
        || t.bitfield.imm8s
2514
28.7k
        || t.bitfield.imm16
2515
28.5k
        || t.bitfield.imm32
2516
28.5k
        || t.bitfield.imm32s
2517
28.5k
        || t.bitfield.imm64);
2518
2519
72.3k
    case disp:
2520
72.3k
      return (t.bitfield.disp8
2521
56.2k
        || t.bitfield.disp16
2522
53.5k
        || t.bitfield.disp32
2523
24.3k
        || t.bitfield.disp64);
2524
2525
16.5k
    case anymem:
2526
16.5k
      return (t.bitfield.disp8
2527
2.67k
        || t.bitfield.disp16
2528
2.59k
        || t.bitfield.disp32
2529
2.59k
        || t.bitfield.disp64
2530
2.51k
        || t.bitfield.baseindex);
2531
2532
0
    default:
2533
0
      abort ();
2534
121k
    }
2535
2536
0
  return 0;
2537
121k
}
2538
2539
/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit size
2540
   between operand GIVEN and operand WANTED for instruction template T.  */
2541
2542
static INLINE int
2543
match_operand_size (const insn_template *t, unsigned int wanted,
2544
        unsigned int given)
2545
15.9k
{
2546
15.9k
  return !((i.types[given].bitfield.byte
2547
221
      && !t->operand_types[wanted].bitfield.byte)
2548
15.7k
     || (i.types[given].bitfield.word
2549
1.15k
         && !t->operand_types[wanted].bitfield.word)
2550
15.4k
     || (i.types[given].bitfield.dword
2551
232
         && !t->operand_types[wanted].bitfield.dword)
2552
15.4k
     || (i.types[given].bitfield.qword
2553
236
         && (!t->operand_types[wanted].bitfield.qword
2554
       /* Don't allow 64-bit (memory) operands outside of 64-bit
2555
          mode, when they're used where a 64-bit GPR could also
2556
          be used.  Checking is needed for Intel Syntax only.  */
2557
236
       || (intel_syntax
2558
146
           && flag_code != CODE_64BIT
2559
0
           && (t->operand_types[wanted].bitfield.class == Reg
2560
0
         || t->opcode_modifier.isstring)))));
2561
15.9k
}
2562
2563
/* Return 1 if there is no conflict in 80bit size
2564
   between operand GIVEN and operand WANTED for instruction template T.  */
2565
2566
static INLINE int
2567
match_fp_size (const insn_template *t, unsigned int wanted,
2568
        unsigned int given)
2569
6
{
2570
6
  return !i.types[given].bitfield.tbyte
2571
6
   || t->operand_types[wanted].bitfield.tbyte;
2572
6
}
2573
2574
/* Return 1 if there is no conflict in SIMD register between operand
2575
   GIVEN and operand WANTED for instruction template T.  */
2576
2577
static INLINE int
2578
match_simd_size (const insn_template *t, unsigned int wanted,
2579
     unsigned int given)
2580
14.0k
{
2581
14.0k
  return !((i.types[given].bitfield.xmmword
2582
0
      && !t->operand_types[wanted].bitfield.xmmword)
2583
14.0k
     || (i.types[given].bitfield.ymmword
2584
0
         && !t->operand_types[wanted].bitfield.ymmword)
2585
14.0k
     || (i.types[given].bitfield.zmmword
2586
0
         && !t->operand_types[wanted].bitfield.zmmword)
2587
14.0k
     || (i.types[given].bitfield.tmmword
2588
1
         && !t->operand_types[wanted].bitfield.tmmword));
2589
14.0k
}
2590
2591
/* Return 1 if there is no conflict in any size between operand GIVEN
2592
   and operand WANTED for instruction template T.  */
2593
2594
static INLINE int
2595
match_mem_size (const insn_template *t, unsigned int wanted,
2596
    unsigned int given)
2597
14.0k
{
2598
14.0k
  return (match_operand_size (t, wanted, given)
2599
14.0k
    && (!i.types[given].bitfield.tbyte
2600
0
        || t->operand_types[wanted].bitfield.tbyte)
2601
14.0k
    && !((i.types[given].bitfield.unspecified
2602
14.0k
    && !i.broadcast.type
2603
14.0k
    && !i.broadcast.bytes
2604
14.0k
    && !t->operand_types[wanted].bitfield.unspecified)
2605
14.0k
         || (i.types[given].bitfield.fword
2606
0
       && !t->operand_types[wanted].bitfield.fword)
2607
         /* For scalar opcode templates to allow register and memory
2608
      operands at the same time, some special casing is needed
2609
      here.  Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2610
      down-conversion vpmov*.  */
2611
14.0k
         || ((t->operand_types[wanted].bitfield.class == RegSIMD
2612
10
        && t->operand_types[wanted].bitfield.byte
2613
10
           + t->operand_types[wanted].bitfield.word
2614
10
           + t->operand_types[wanted].bitfield.dword
2615
10
           + t->operand_types[wanted].bitfield.qword
2616
10
           > !!t->opcode_modifier.broadcast)
2617
14.0k
       ? (i.types[given].bitfield.xmmword
2618
8
          || i.types[given].bitfield.ymmword
2619
8
          || i.types[given].bitfield.zmmword)
2620
14.0k
       : !match_simd_size(t, wanted, given))));
2621
14.0k
}
2622
2623
/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2624
   operands for instruction template T, and it has MATCH_REVERSE set if there
2625
   is no size conflict on any operands for the template with operands reversed
2626
   (and the template allows for reversing in the first place).  */
2627
2628
30.9k
#define MATCH_STRAIGHT 1
2629
7.49k
#define MATCH_REVERSE  2
2630
2631
static INLINE unsigned int
2632
operand_size_match (const insn_template *t)
2633
20.6k
{
2634
20.6k
  unsigned int j, match = MATCH_STRAIGHT;
2635
2636
  /* Don't check non-absolute jump instructions.  */
2637
20.6k
  if (t->opcode_modifier.jump
2638
685
      && t->opcode_modifier.jump != JUMP_ABSOLUTE)
2639
676
    return match;
2640
2641
29.4k
  for (j = 0; j < i.imm_operands; j++)
2642
    /* Instruction templates with only sign-extended 8-bit immediate
2643
       operand also have a second template with full-operand-size
2644
       immediate operand under a different opcode.  Don't match the
2645
       first template if sign-extended 8-bit immediate operand should
2646
       be excluded.  */
2647
9.52k
    if (pp.no_imm8s
2648
0
        && !t->operand_types[j].bitfield.imm8
2649
0
        && t->operand_types[j].bitfield.imm8s)
2650
0
      {
2651
0
  gas_assert (!t->opcode_modifier.d);
2652
0
  return 0;
2653
0
      }
2654
2655
  /* Check memory and accumulator operand size.  */
2656
40.0k
  for (; j < i.operands; j++)
2657
20.2k
    {
2658
20.2k
      if (i.types[j].bitfield.class == Reg
2659
1.28k
    && (t->operand_types[j].bitfield.class == Reg
2660
512
        || (t->operand_types[j].bitfield.instance == Accum
2661
146
      && (t->operand_types[j].bitfield.byte
2662
0
          || t->operand_types[j].bitfield.word
2663
0
          || t->operand_types[j].bitfield.dword
2664
0
          || t->operand_types[j].bitfield.qword)))
2665
920
    && !match_operand_size (t, j, j))
2666
123
  {
2667
123
    match = 0;
2668
123
    break;
2669
123
  }
2670
2671
20.0k
      if (i.types[j].bitfield.class == RegFP
2672
212
    && (t->operand_types[j].bitfield.class == RegFP
2673
206
        || (t->operand_types[j].bitfield.instance == Accum
2674
11
      && t->operand_types[j].bitfield.tbyte))
2675
6
    && !match_fp_size (t, j, j))
2676
0
  {
2677
0
    match = 0;
2678
0
    break;
2679
0
  }
2680
2681
20.0k
      if (i.types[j].bitfield.class == RegSIMD
2682
2
    && (t->operand_types[j].bitfield.class == RegSIMD
2683
1
        || (t->operand_types[j].bitfield.instance == Accum
2684
      /* Note: %ymm0, %zmm0, and %tmm0 aren't marked Accum.  */
2685
0
      && t->operand_types[j].bitfield.xmmword))
2686
1
    && !match_simd_size (t, j, j))
2687
1
  {
2688
1
    match = 0;
2689
1
    break;
2690
1
  }
2691
2692
20.0k
      if ((i.flags[j] & Operand_Mem)
2693
15.2k
    && operand_type_check (t->operand_types[j], anymem)
2694
13.2k
    && t->opcode_modifier.operandconstraint != ANY_SIZE
2695
13.2k
    && !match_mem_size (t, j, j))
2696
26
  {
2697
26
    match = 0;
2698
26
    break;
2699
26
  }
2700
20.0k
    }
2701
2702
19.9k
  if (!t->opcode_modifier.d)
2703
15.8k
    return match;
2704
2705
  /* Check reverse.  */
2706
4.09k
  gas_assert (i.operands >= 2);
2707
2708
8.60k
  for (j = i.imm_operands; j < i.operands; j++)
2709
4.89k
    {
2710
4.89k
      unsigned int given = i.operands - j - 1;
2711
2712
      /* For FMA4 and XOP insns VEX.W controls just the first two register
2713
   operands.  And APX_F / APX_NDD insns just swap the two source operands,
2714
   with the 3rd one being the destination.  */
2715
4.89k
      if (is_cpu (t, CpuFMA4) || is_cpu (t, CpuXOP)
2716
4.88k
    || is_cpu (t, CpuAPX_F)|| is_cpu (t, CpuAPX_NDD))
2717
1.09k
  given = j < 2 ? 1 - j : j;
2718
2719
4.89k
      if (i.types[given].bitfield.class == Reg
2720
1.21k
    && (t->operand_types[j].bitfield.class == Reg
2721
473
        || (t->operand_types[j].bitfield.instance == Accum
2722
185
      && (t->operand_types[j].bitfield.byte
2723
0
          || t->operand_types[j].bitfield.word
2724
0
          || t->operand_types[j].bitfield.dword
2725
0
          || t->operand_types[j].bitfield.qword
2726
0
          || t->operand_types[j].bitfield.tbyte)))
2727
928
    && !match_operand_size (t, j, given))
2728
379
  return match;
2729
2730
4.51k
      if (i.types[given].bitfield.class == RegFP
2731
101
    && (t->operand_types[j].bitfield.class == RegFP
2732
101
        || (t->operand_types[j].bitfield.instance == Accum
2733
9
      && t->operand_types[j].bitfield.tbyte))
2734
0
    && !match_fp_size (t, j, given))
2735
0
  return match;
2736
2737
      /* No need to check for Accum here: There are no such templates with D
2738
   set.  */
2739
4.51k
      if (i.types[given].bitfield.class == RegSIMD
2740
4
    && t->operand_types[j].bitfield.class == RegSIMD
2741
0
    && !match_simd_size (t, j, given))
2742
0
  return match;
2743
2744
4.51k
      if ((i.flags[given] & Operand_Mem)
2745
1.33k
    && operand_type_check (t->operand_types[j], anymem)
2746
846
    && !match_mem_size (t, j, given))
2747
0
  return match;
2748
4.51k
    }
2749
2750
3.71k
  return match | MATCH_REVERSE;
2751
4.09k
}
2752
2753
static INLINE int
2754
operand_type_match (i386_operand_type overlap,
2755
        i386_operand_type given)
2756
27.8k
{
2757
27.8k
  i386_operand_type temp = overlap;
2758
2759
27.8k
  temp.bitfield.unspecified = 0;
2760
27.8k
  temp.bitfield.byte = 0;
2761
27.8k
  temp.bitfield.word = 0;
2762
27.8k
  temp.bitfield.dword = 0;
2763
27.8k
  temp.bitfield.fword = 0;
2764
27.8k
  temp.bitfield.qword = 0;
2765
27.8k
  temp.bitfield.tbyte = 0;
2766
27.8k
  temp.bitfield.xmmword = 0;
2767
27.8k
  temp.bitfield.ymmword = 0;
2768
27.8k
  temp.bitfield.zmmword = 0;
2769
27.8k
  temp.bitfield.tmmword = 0;
2770
27.8k
  if (operand_type_all_zero (&temp))
2771
12.4k
    goto mismatch;
2772
2773
  /* When a (register) instance is expected, operand size needs checking
2774
     to disambiguate.  */
2775
15.4k
  if (overlap.bitfield.instance != InstanceNone
2776
186
      && !overlap.bitfield.byte
2777
186
      && !overlap.bitfield.word
2778
13
      && !overlap.bitfield.dword
2779
9
      && !overlap.bitfield.qword
2780
9
      && !overlap.bitfield.tbyte
2781
9
      && !overlap.bitfield.xmmword
2782
9
      && !overlap.bitfield.ymmword
2783
9
      && !overlap.bitfield.zmmword
2784
9
      && !overlap.bitfield.tmmword)
2785
9
    {
2786
9
      gas_assert (overlap.bitfield.class == ClassNone);
2787
9
      goto mismatch;
2788
9
    }
2789
2790
15.4k
  if (given.bitfield.baseindex == overlap.bitfield.baseindex)
2791
15.4k
    return 1;
2792
2793
12.4k
 mismatch:
2794
12.4k
  i.error = operand_type_mismatch;
2795
12.4k
  return 0;
2796
15.4k
}
2797
2798
/* If given types g0 and g1 are registers they must be of the same type
2799
   unless the expected operand type register overlap is null.
2800
   Intel syntax sized memory operands are also checked here.  */
2801
2802
static INLINE int
2803
operand_type_register_match (i386_operand_type g0,
2804
           i386_operand_type t0,
2805
           i386_operand_type g1,
2806
           i386_operand_type t1)
2807
721
{
2808
721
  if (g0.bitfield.class != Reg
2809
545
      && g0.bitfield.class != RegSIMD
2810
545
      && (g0.bitfield.unspecified
2811
0
    || !operand_type_check (g0, anymem)))
2812
545
    return 1;
2813
2814
176
  if (g1.bitfield.class != Reg
2815
146
      && g1.bitfield.class != RegSIMD
2816
146
      && (g1.bitfield.unspecified
2817
0
    || !operand_type_check (g1, anymem)))
2818
146
    return 1;
2819
2820
30
  if (g0.bitfield.byte == g1.bitfield.byte
2821
4
      && g0.bitfield.word == g1.bitfield.word
2822
4
      && g0.bitfield.dword == g1.bitfield.dword
2823
4
      && g0.bitfield.qword == g1.bitfield.qword
2824
4
      && g0.bitfield.xmmword == g1.bitfield.xmmword
2825
4
      && g0.bitfield.ymmword == g1.bitfield.ymmword
2826
4
      && g0.bitfield.zmmword == g1.bitfield.zmmword)
2827
4
    return 1;
2828
2829
  /* If expectations overlap in no more than a single size, all is fine. */
2830
26
  g0 = operand_type_and (t0, t1);
2831
26
  if (g0.bitfield.byte
2832
26
      + g0.bitfield.word
2833
26
      + g0.bitfield.dword
2834
26
      + g0.bitfield.qword
2835
26
      + g0.bitfield.xmmword
2836
26
      + g0.bitfield.ymmword
2837
26
      + g0.bitfield.zmmword <= 1)
2838
0
    return 1;
2839
2840
26
  i.error = register_type_mismatch;
2841
2842
26
  return 0;
2843
26
}
2844
2845
static INLINE unsigned int
2846
register_number (const reg_entry *r)
2847
2
{
2848
2
  unsigned int nr = r->reg_num;
2849
2850
2
  if (r->reg_flags & RegRex)
2851
0
    nr += 8;
2852
2853
2
  if (r->reg_flags & (RegVRex | RegRex2))
2854
0
    nr += 16;
2855
2856
2
  return nr;
2857
2
}
2858
2859
static INLINE unsigned int
2860
mode_from_disp_size (i386_operand_type t)
2861
4
{
2862
4
  if (t.bitfield.disp8)
2863
2
    return 1;
2864
2
  else if (t.bitfield.disp16
2865
2
     || t.bitfield.disp32)
2866
0
    return 2;
2867
2
  else
2868
2
    return 0;
2869
4
}
2870
2871
static INLINE int
2872
fits_in_signed_byte (addressT num)
2873
3.71k
{
2874
3.71k
  return num + 0x80 <= 0xff;
2875
3.71k
}
2876
2877
static INLINE int
2878
fits_in_unsigned_byte (addressT num)
2879
2.29k
{
2880
2.29k
  return num <= 0xff;
2881
2.29k
}
2882
2883
static INLINE int
2884
fits_in_unsigned_word (addressT num)
2885
1.22k
{
2886
1.22k
  return num <= 0xffff;
2887
1.22k
}
2888
2889
static INLINE int
2890
fits_in_signed_word (addressT num)
2891
538
{
2892
538
  return num + 0x8000 <= 0xffff;
2893
538
}
2894
2895
static INLINE int
2896
fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
2897
2.63k
{
2898
#ifndef BFD64
2899
  return 1;
2900
#else
2901
2.63k
  return num + 0x80000000 <= 0xffffffff;
2902
2.63k
#endif
2903
2.63k
}        /* fits_in_signed_long() */
2904
2905
static INLINE int
2906
fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
2907
42.2k
{
2908
#ifndef BFD64
2909
  return 1;
2910
#else
2911
42.2k
  return num <= 0xffffffff;
2912
42.2k
#endif
2913
42.2k
}        /* fits_in_unsigned_long() */
2914
2915
static INLINE valueT extend_to_32bit_address (addressT num)
2916
0
{
2917
0
#ifdef BFD64
2918
0
  if (fits_in_unsigned_long(num))
2919
0
    return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2920
2921
0
  if (!fits_in_signed_long (num))
2922
0
    return num & 0xffffffff;
2923
0
#endif
2924
2925
0
  return num;
2926
0
}
2927
2928
static INLINE int
2929
fits_in_disp8 (offsetT num)
2930
1.41k
{
2931
1.41k
  int shift = i.memshift;
2932
1.41k
  unsigned int mask;
2933
2934
1.41k
  if (shift == -1)
2935
0
    abort ();
2936
2937
1.41k
  mask = (1 << shift) - 1;
2938
2939
  /* Return 0 if NUM isn't properly aligned.  */
2940
1.41k
  if ((num & mask))
2941
0
    return 0;
2942
2943
  /* Check if NUM will fit in 8bit after shift.  */
2944
1.41k
  return fits_in_signed_byte (num >> shift);
2945
1.41k
}
2946
2947
static INLINE int
2948
fits_in_imm4 (offsetT num)
2949
0
{
2950
  /* Despite the name, check for imm3 if we're dealing with EVEX.  */
2951
0
  return (num & (pp.encoding != encoding_evex
2952
0
     && pp.encoding != encoding_egpr ? 0xf : 7)) == num;
2953
0
}
2954
2955
static i386_operand_type
2956
smallest_imm_type (offsetT num)
2957
2.31k
{
2958
2.31k
  i386_operand_type t;
2959
2960
2.31k
  operand_type_set (&t, 0);
2961
2.31k
  t.bitfield.imm64 = 1;
2962
2963
2.31k
  if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
2964
13
    {
2965
      /* This code is disabled on the 486 because all the Imm1 forms
2966
   in the opcode table are slower on the i486.  They're the
2967
   versions with the implicitly specified single-position
2968
   displacement, which has another syntax if you really want to
2969
   use that form.  */
2970
13
      t.bitfield.imm1 = 1;
2971
13
      t.bitfield.imm8 = 1;
2972
13
      t.bitfield.imm8s = 1;
2973
13
      t.bitfield.imm16 = 1;
2974
13
      t.bitfield.imm32 = 1;
2975
13
      t.bitfield.imm32s = 1;
2976
13
    }
2977
2.29k
  else if (fits_in_signed_byte (num))
2978
1.76k
    {
2979
1.76k
      if (fits_in_unsigned_byte (num))
2980
1.49k
  t.bitfield.imm8 = 1;
2981
1.76k
      t.bitfield.imm8s = 1;
2982
1.76k
      t.bitfield.imm16 = 1;
2983
1.76k
      if (flag_code != CODE_64BIT || fits_in_unsigned_long (num))
2984
1.74k
  t.bitfield.imm32 = 1;
2985
1.76k
      t.bitfield.imm32s = 1;
2986
1.76k
    }
2987
533
  else if (fits_in_unsigned_byte (num))
2988
0
    {
2989
0
      t.bitfield.imm8 = 1;
2990
0
      t.bitfield.imm16 = 1;
2991
0
      t.bitfield.imm32 = 1;
2992
0
      t.bitfield.imm32s = 1;
2993
0
    }
2994
533
  else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2995
333
    {
2996
333
      t.bitfield.imm16 = 1;
2997
333
      if (flag_code != CODE_64BIT || fits_in_unsigned_long (num))
2998
328
  t.bitfield.imm32 = 1;
2999
333
      t.bitfield.imm32s = 1;
3000
333
    }
3001
200
  else if (fits_in_signed_long (num))
3002
97
    {
3003
97
      if (flag_code != CODE_64BIT || fits_in_unsigned_long (num))
3004
79
  t.bitfield.imm32 = 1;
3005
97
      t.bitfield.imm32s = 1;
3006
97
    }
3007
103
  else if (fits_in_unsigned_long (num))
3008
1
    t.bitfield.imm32 = 1;
3009
3010
2.31k
  return t;
3011
2.31k
}
3012
3013
static offsetT
3014
offset_in_range (offsetT val, int size)
3015
1.24k
{
3016
1.24k
  addressT mask;
3017
3018
1.24k
  switch (size)
3019
1.24k
    {
3020
739
    case 1: mask = ((addressT) 1 <<  8) - 1; break;
3021
2
    case 2: mask = ((addressT) 1 << 16) - 1; break;
3022
0
#ifdef BFD64
3023
508
    case 4: mask = ((addressT) 1 << 32) - 1; break;
3024
0
#endif
3025
0
    case sizeof (val): return val;
3026
0
    default: abort ();
3027
1.24k
    }
3028
3029
1.24k
  if ((val & ~mask) != 0 && (-(addressT) val & ~mask) != 0)
3030
170
    as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
3031
170
       (uint64_t) val, (uint64_t) (val & mask));
3032
3033
1.24k
  return val & mask;
3034
1.24k
}
3035
3036
static INLINE const char *insn_name (const insn_template *t)
3037
1.25M
{
3038
1.25M
  return &i386_mnemonics[t->mnem_off];
3039
1.25M
}
3040
3041
enum PREFIX_GROUP
3042
{
3043
  PREFIX_EXIST = 0,
3044
  PREFIX_LOCK,
3045
  PREFIX_REP,
3046
  PREFIX_DS,
3047
  PREFIX_OTHER
3048
};
3049
3050
/* Returns
3051
   a. PREFIX_EXIST if attempting to add a prefix where one from the
3052
   same class already exists.
3053
   b. PREFIX_LOCK if lock prefix is added.
3054
   c. PREFIX_REP if rep/repne prefix is added.
3055
   d. PREFIX_DS if ds prefix is added.
3056
   e. PREFIX_OTHER if other prefix is added.
3057
 */
3058
3059
static enum PREFIX_GROUP
3060
add_prefix (unsigned int prefix)
3061
564
{
3062
564
  enum PREFIX_GROUP ret = PREFIX_OTHER;
3063
564
  unsigned int q;
3064
3065
564
  if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
3066
245
      && flag_code == CODE_64BIT)
3067
245
    {
3068
245
      if ((i.prefix[REX_PREFIX] & prefix & REX_W)
3069
243
    || (i.prefix[REX_PREFIX] & prefix & REX_R)
3070
243
    || (i.prefix[REX_PREFIX] & prefix & REX_X)
3071
243
    || (i.prefix[REX_PREFIX] & prefix & REX_B))
3072
7
  ret = PREFIX_EXIST;
3073
245
      q = REX_PREFIX;
3074
245
    }
3075
319
  else
3076
319
    {
3077
319
      switch (prefix)
3078
319
  {
3079
0
  default:
3080
0
    abort ();
3081
3082
1
  case DS_PREFIX_OPCODE:
3083
1
    ret = PREFIX_DS;
3084
    /* Fall through.  */
3085
45
  case CS_PREFIX_OPCODE:
3086
45
  case ES_PREFIX_OPCODE:
3087
45
  case FS_PREFIX_OPCODE:
3088
45
  case GS_PREFIX_OPCODE:
3089
53
  case SS_PREFIX_OPCODE:
3090
53
    q = SEG_PREFIX;
3091
53
    break;
3092
3093
0
  case REPNE_PREFIX_OPCODE:
3094
18
  case REPE_PREFIX_OPCODE:
3095
18
    q = REP_PREFIX;
3096
18
    ret = PREFIX_REP;
3097
18
    break;
3098
3099
0
  case LOCK_PREFIX_OPCODE:
3100
0
    q = LOCK_PREFIX;
3101
0
    ret = PREFIX_LOCK;
3102
0
    break;
3103
3104
0
  case FWAIT_OPCODE:
3105
0
    q = WAIT_PREFIX;
3106
0
    break;
3107
3108
10
  case ADDR_PREFIX_OPCODE:
3109
10
    q = ADDR_PREFIX;
3110
10
    break;
3111
3112
238
  case DATA_PREFIX_OPCODE:
3113
238
    q = DATA_PREFIX;
3114
238
    break;
3115
319
  }
3116
319
      if (i.prefix[q] != 0)
3117
0
  ret = PREFIX_EXIST;
3118
319
    }
3119
3120
564
  if (ret)
3121
557
    {
3122
557
      if (!i.prefix[q])
3123
521
  ++i.prefixes;
3124
557
      i.prefix[q] |= prefix;
3125
557
    }
3126
7
  else
3127
7
    as_bad (_("same type of prefix used twice"));
3128
3129
564
  return ret;
3130
564
}
3131
3132
static void
3133
update_code_flag (int value, int check)
3134
1.45k
{
3135
1.45k
  PRINTF_LIKE ((*as_error)) = check ? as_fatal : as_bad;
3136
3137
1.45k
  if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpu64 )
3138
0
    {
3139
0
      as_error (_("64bit mode not supported on `%s'."),
3140
0
    cpu_arch_name ? cpu_arch_name : default_arch);
3141
0
      return;
3142
0
    }
3143
3144
1.45k
  if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
3145
0
    {
3146
0
      as_error (_("32bit mode not supported on `%s'."),
3147
0
    cpu_arch_name ? cpu_arch_name : default_arch);
3148
0
      return;
3149
0
    }
3150
3151
1.45k
  flag_code = (enum flag_code) value;
3152
3153
1.45k
  stackop_size = '\0';
3154
1.45k
}
3155
3156
static void
3157
set_code_flag (int value)
3158
976
{
3159
976
  update_code_flag (value, 0);
3160
976
}
3161
3162
static void
3163
set_16bit_gcc_code_flag (int new_code_flag)
3164
8
{
3165
8
  flag_code = (enum flag_code) new_code_flag;
3166
8
  if (flag_code != CODE_16BIT)
3167
0
    abort ();
3168
8
  stackop_size = LONG_MNEM_SUFFIX;
3169
8
}
3170
3171
static void
3172
_set_intel_syntax (int syntax_flag)
3173
52
{
3174
52
  intel_syntax = syntax_flag;
3175
3176
52
  expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
3177
3178
52
  register_prefix = allow_naked_reg ? "" : "%";
3179
52
}
3180
3181
static void
3182
set_intel_syntax (int syntax_flag)
3183
52
{
3184
  /* Find out if register prefixing is specified.  */
3185
52
  int ask_naked_reg = 0;
3186
3187
52
  SKIP_WHITESPACE ();
3188
52
  if (!is_end_of_stmt (*input_line_pointer))
3189
12
    {
3190
12
      char *string;
3191
12
      int e = get_symbol_name (&string);
3192
3193
12
      if (strcmp (string, "prefix") == 0)
3194
0
  ask_naked_reg = 1;
3195
12
      else if (strcmp (string, "noprefix") == 0)
3196
0
  ask_naked_reg = -1;
3197
12
      else
3198
12
  as_bad (_("bad argument to syntax directive."));
3199
12
      (void) restore_line_pointer (e);
3200
12
    }
3201
52
  demand_empty_rest_of_line ();
3202
3203
52
  if (ask_naked_reg == 0)
3204
52
    allow_naked_reg = (syntax_flag
3205
49
           && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
3206
0
  else
3207
0
    allow_naked_reg = (ask_naked_reg < 0);
3208
3209
52
  _set_intel_syntax (syntax_flag);
3210
52
}
3211
3212
static void
3213
set_intel_mnemonic (int mnemonic_flag)
3214
0
{
3215
0
  intel_mnemonic = mnemonic_flag;
3216
0
}
3217
3218
static void
3219
set_allow_index_reg (int flag)
3220
8
{
3221
8
  allow_index_reg = flag;
3222
8
}
3223
3224
static void
3225
set_check (int what)
3226
4
{
3227
4
  enum check_kind *kind;
3228
4
  const char *str;
3229
3230
4
  if (what)
3231
0
    {
3232
0
      kind = &operand_check;
3233
0
      str = "operand";
3234
0
    }
3235
4
  else
3236
4
    {
3237
4
      kind = &sse_check;
3238
4
      str = "sse";
3239
4
    }
3240
3241
4
  SKIP_WHITESPACE ();
3242
3243
4
  if (!is_end_of_stmt (*input_line_pointer))
3244
0
    {
3245
0
      char *string;
3246
0
      int e = get_symbol_name (&string);
3247
3248
0
      if (strcmp (string, "none") == 0)
3249
0
  *kind = check_none;
3250
0
      else if (strcmp (string, "warning") == 0)
3251
0
  *kind = check_warning;
3252
0
      else if (strcmp (string, "error") == 0)
3253
0
  *kind = check_error;
3254
0
      else
3255
0
  as_bad (_("bad argument to %s_check directive."), str);
3256
0
      (void) restore_line_pointer (e);
3257
0
    }
3258
4
  else
3259
4
    as_bad (_("missing argument for %s_check directive"), str);
3260
3261
4
  demand_empty_rest_of_line ();
3262
4
}
3263
3264
static void
3265
check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
3266
         i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
3267
47
{
3268
  /* Intel MCU is only supported on ELF.  */
3269
47
#ifdef OBJ_ELF
3270
47
  static const char *arch;
3271
3272
47
  if (!arch)
3273
1
    {
3274
      /* Use cpu_arch_name if it is set in md_parse_option.  Otherwise
3275
   use default_arch.  */
3276
1
      arch = cpu_arch_name;
3277
1
      if (!arch)
3278
1
  arch = default_arch;
3279
1
    }
3280
3281
  /* If we are targeting Intel MCU, we must enable it.  */
3282
47
  if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
3283
47
      == new_flag.bitfield.cpuiamcu)
3284
47
    return;
3285
3286
0
  as_bad (_("`%s' is not supported on `%s'"), name, arch);
3287
0
#endif
3288
0
}
3289
3290
static void
3291
extend_cpu_sub_arch_name (const char *pfx, const char *name)
3292
14
{
3293
14
  if (cpu_sub_arch_name)
3294
3
    cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
3295
3
          pfx, name, (const char *) NULL);
3296
11
  else
3297
11
    cpu_sub_arch_name = concat (pfx, name, (const char *) NULL);
3298
14
}
3299
3300
static void isa_enable (unsigned int idx)
3301
57
{
3302
57
  i386_cpu_flags flags = cpu_flags_or (cpu_arch_flags, cpu_arch[idx].enable);
3303
3304
57
  if (!cpu_flags_equal (&flags, &cpu_arch_flags))
3305
3
    {
3306
3
      extend_cpu_sub_arch_name (".", cpu_arch[idx].name);
3307
3
      cpu_arch_flags = flags;
3308
3
    }
3309
3310
57
  cpu_arch_isa_flags = cpu_flags_or (cpu_arch_isa_flags, cpu_arch[idx].enable);
3311
57
}
3312
3313
static void isa_disable (unsigned int idx)
3314
11
{
3315
11
  i386_cpu_flags flags
3316
11
    = cpu_flags_and_not (cpu_arch_flags, cpu_arch[idx].disable);
3317
3318
11
  if (!cpu_flags_equal (&flags, &cpu_arch_flags))
3319
11
    {
3320
11
      extend_cpu_sub_arch_name (".no", cpu_arch[idx].name);
3321
11
      cpu_arch_flags = flags;
3322
11
    }
3323
3324
11
  cpu_arch_isa_flags
3325
11
    = cpu_flags_and_not (cpu_arch_isa_flags, cpu_arch[idx].disable);
3326
11
}
3327
3328
static void
3329
set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
3330
144
{
3331
144
  typedef struct arch_stack_entry
3332
144
  {
3333
144
    const struct arch_stack_entry *prev;
3334
144
    const char *name;
3335
144
    char *sub_name;
3336
144
    i386_cpu_flags flags;
3337
144
    i386_cpu_flags isa_flags;
3338
144
    enum processor_type isa;
3339
144
    enum flag_code flag_code;
3340
144
    unsigned int vector_size;
3341
144
    char stackop_size;
3342
144
    bool no_cond_jump_promotion;
3343
144
  } arch_stack_entry;
3344
144
  static const arch_stack_entry *arch_stack_top;
3345
144
  char *s;
3346
144
  int e;
3347
144
  const char *string;
3348
144
  unsigned int j = 0;
3349
3350
144
  SKIP_WHITESPACE ();
3351
3352
144
  if (is_end_of_stmt (*input_line_pointer))
3353
1
    {
3354
1
      as_bad (_("missing cpu architecture"));
3355
1
      input_line_pointer++;
3356
1
      return;
3357
1
    }
3358
3359
143
  e = get_symbol_name (&s);
3360
143
  string = s;
3361
3362
143
  if (strcmp (string, "push") == 0)
3363
9
    {
3364
9
      arch_stack_entry *top = XNEW (arch_stack_entry);
3365
3366
9
      top->name = cpu_arch_name;
3367
9
      if (cpu_sub_arch_name)
3368
9
  top->sub_name = xstrdup (cpu_sub_arch_name);
3369
0
      else
3370
0
  top->sub_name = NULL;
3371
9
      top->flags = cpu_arch_flags;
3372
9
      top->isa = cpu_arch_isa;
3373
9
      top->isa_flags = cpu_arch_isa_flags;
3374
9
      top->flag_code = flag_code;
3375
9
      top->vector_size = vector_size;
3376
9
      top->stackop_size = stackop_size;
3377
9
      top->no_cond_jump_promotion = no_cond_jump_promotion;
3378
3379
9
      top->prev = arch_stack_top;
3380
9
      arch_stack_top = top;
3381
3382
9
      (void) restore_line_pointer (e);
3383
9
      demand_empty_rest_of_line ();
3384
9
      return;
3385
9
    }
3386
3387
134
  if (strcmp (string, "pop") == 0)
3388
0
    {
3389
0
      const arch_stack_entry *top = arch_stack_top;
3390
3391
0
      if (!top)
3392
0
  {
3393
0
    as_bad (_(".arch stack is empty"));
3394
53
  restore_bad:
3395
53
    (void) restore_line_pointer (e);
3396
53
    ignore_rest_of_line ();
3397
53
    return;
3398
0
  }
3399
3400
0
      if (top->flag_code != flag_code
3401
0
    || top->stackop_size != stackop_size)
3402
0
  {
3403
0
    static const unsigned int bits[] = {
3404
0
      [CODE_16BIT] = 16,
3405
0
      [CODE_32BIT] = 32,
3406
0
      [CODE_64BIT] = 64,
3407
0
    };
3408
3409
0
    as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
3410
0
      bits[top->flag_code],
3411
0
      top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
3412
0
    goto restore_bad;
3413
0
  }
3414
3415
0
      arch_stack_top = top->prev;
3416
3417
0
      cpu_arch_name = top->name;
3418
0
      free (cpu_sub_arch_name);
3419
0
      cpu_sub_arch_name = top->sub_name;
3420
0
      cpu_arch_flags = top->flags;
3421
0
      cpu_arch_isa = top->isa;
3422
0
      cpu_arch_isa_flags = top->isa_flags;
3423
0
      vector_size = top->vector_size;
3424
0
      no_cond_jump_promotion = top->no_cond_jump_promotion;
3425
3426
0
      XDELETE (top);
3427
3428
0
      (void) restore_line_pointer (e);
3429
0
      demand_empty_rest_of_line ();
3430
0
      return;
3431
0
    }
3432
3433
134
  if (strcmp (string, "default") == 0)
3434
0
    {
3435
0
      if (strcmp (default_arch, "iamcu") == 0)
3436
0
  string = default_arch;
3437
0
      else
3438
0
  {
3439
0
    static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
3440
3441
0
    cpu_arch_name = NULL;
3442
0
    free (cpu_sub_arch_name);
3443
0
    cpu_sub_arch_name = NULL;
3444
0
    cpu_arch_flags = cpu_unknown_flags;
3445
0
    cpu_arch_isa = PROCESSOR_UNKNOWN;
3446
0
    cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
3447
0
    if (!cpu_arch_tune_set)
3448
0
      cpu_arch_tune = PROCESSOR_UNKNOWN;
3449
3450
0
    vector_size = VSZ_DEFAULT;
3451
3452
0
    j = ARRAY_SIZE (cpu_arch) + 1;
3453
0
  }
3454
0
    }
3455
3456
12.1k
  for (; j < ARRAY_SIZE (cpu_arch); j++)
3457
12.1k
    {
3458
12.1k
      if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
3459
104
    && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
3460
104
  {
3461
104
    if (*string != '.')
3462
47
      {
3463
47
        check_cpu_arch_compatible (string, cpu_arch[j].enable);
3464
3465
47
        if (flag_code == CODE_64BIT && !cpu_arch[j].enable.bitfield.cpu64 )
3466
0
    {
3467
0
      as_bad (_("64bit mode not supported on `%s'."),
3468
0
        cpu_arch[j].name);
3469
0
      goto restore_bad;
3470
0
    }
3471
3472
47
        if (flag_code == CODE_32BIT && !cpu_arch[j].enable.bitfield.cpui386)
3473
0
    {
3474
0
      as_bad (_("32bit mode not supported on `%s'."),
3475
0
        cpu_arch[j].name);
3476
0
      goto restore_bad;
3477
0
    }
3478
3479
47
        cpu_arch_name = cpu_arch[j].name;
3480
47
        free (cpu_sub_arch_name);
3481
47
        cpu_sub_arch_name = NULL;
3482
47
        cpu_arch_flags = cpu_arch[j].enable;
3483
47
        cpu_arch_isa = cpu_arch[j].type;
3484
47
        cpu_arch_isa_flags = cpu_arch[j].enable;
3485
47
        if (!cpu_arch_tune_set)
3486
47
    cpu_arch_tune = cpu_arch_isa;
3487
3488
47
        vector_size = VSZ_DEFAULT;
3489
3490
47
        pre_386_16bit_warned = false;
3491
47
        break;
3492
47
      }
3493
3494
57
    if (cpu_flags_all_zero (&cpu_arch[j].enable))
3495
0
      continue;
3496
3497
57
    isa_enable (j);
3498
3499
57
    (void) restore_line_pointer (e);
3500
3501
57
    switch (cpu_arch[j].vsz)
3502
57
      {
3503
36
      default:
3504
36
        break;
3505
3506
36
      case vsz_set:
3507
#ifdef SVR4_COMMENT_CHARS
3508
        if (*input_line_pointer == ':' || *input_line_pointer == '/')
3509
#else
3510
5
        if (*input_line_pointer == '/')
3511
4
#endif
3512
4
    {
3513
4
      ++input_line_pointer;
3514
4
      switch (get_absolute_expression ())
3515
4
        {
3516
0
        case 512: vector_size = VSZ512; break;
3517
2
        case 256: vector_size = VSZ256; break;
3518
0
        case 128: vector_size = VSZ128; break;
3519
2
        default:
3520
2
          as_bad (_("Unrecognized vector size specifier"));
3521
2
          ignore_rest_of_line ();
3522
2
          return;
3523
4
        }
3524
2
      break;
3525
4
    }
3526
    /* Fall through.  */
3527
17
      case vsz_reset:
3528
17
        vector_size = VSZ_DEFAULT;
3529
17
        break;
3530
57
      }
3531
3532
55
    demand_empty_rest_of_line ();
3533
55
    return;
3534
57
  }
3535
12.1k
    }
3536
3537
77
  if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
3538
13
    {
3539
      /* Disable an ISA extension.  */
3540
1.17k
      for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
3541
1.17k
  if (cpu_arch[j].type == PROCESSOR_NONE
3542
637
      && strcmp (string + 3, cpu_arch[j].name) == 0)
3543
11
    {
3544
11
      isa_disable (j);
3545
3546
11
      if (cpu_arch[j].vsz == vsz_set)
3547
1
        vector_size = VSZ_DEFAULT;
3548
3549
11
      (void) restore_line_pointer (e);
3550
11
      demand_empty_rest_of_line ();
3551
11
      return;
3552
11
    }
3553
13
    }
3554
3555
66
  if (j == ARRAY_SIZE (cpu_arch))
3556
19
    {
3557
19
      as_bad (_("no such architecture: `%s'"), string);
3558
19
      goto restore_bad;
3559
19
    }
3560
3561
47
  no_cond_jump_promotion = 0;
3562
47
  if (restore_line_pointer (e) == ','
3563
37
      && !is_end_of_stmt (input_line_pointer[1]))
3564
34
    {
3565
34
      ++input_line_pointer;
3566
34
      e = get_symbol_name (&s);
3567
34
      string = s;
3568
3569
34
      if (strcmp (string, "nojumps") == 0)
3570
0
  {
3571
0
    if (cpu_arch_flags.bitfield.cpui386)
3572
0
      as_bad (_("`%s' only supported with 16-bit architectures"), string);
3573
0
    else
3574
0
      no_cond_jump_promotion = true;
3575
0
  }
3576
34
      else if (strcmp (string, "jumps") != 0)
3577
34
  {
3578
34
    as_bad (_("no such architecture modifier: `%s'"), string);
3579
34
    goto restore_bad;
3580
34
  }
3581
3582
0
      (void) restore_line_pointer (e);
3583
0
    }
3584
3585
13
  demand_empty_rest_of_line ();
3586
13
}
3587
3588
enum bfd_architecture
3589
i386_arch (void)
3590
478
{
3591
478
  if (cpu_arch_isa == PROCESSOR_IAMCU)
3592
0
    {
3593
0
      if (!IS_ELF || flag_code == CODE_64BIT)
3594
0
  as_fatal (_("Intel MCU is 32bit ELF only"));
3595
0
      return bfd_arch_iamcu;
3596
0
    }
3597
478
  else
3598
478
    return bfd_arch_i386;
3599
478
}
3600
3601
unsigned long
3602
i386_mach (void)
3603
478
{
3604
478
  if (startswith (default_arch, "x86_64"))
3605
478
    {
3606
478
      if (default_arch[6] == '\0')
3607
478
  return bfd_mach_x86_64;
3608
0
      else
3609
0
  return bfd_mach_x64_32;
3610
478
    }
3611
0
  else if (!strcmp (default_arch, "i386")
3612
0
     || !strcmp (default_arch, "iamcu"))
3613
0
    {
3614
0
      if (cpu_arch_isa == PROCESSOR_IAMCU)
3615
0
  {
3616
0
    if (!IS_ELF)
3617
0
      as_fatal (_("Intel MCU is 32bit ELF only"));
3618
0
    return bfd_mach_i386_iamcu;
3619
0
  }
3620
0
      else
3621
0
  return bfd_mach_i386_i386;
3622
0
    }
3623
0
  else
3624
0
    as_fatal (_("unknown architecture"));
3625
478
}
3626

3627
#include "opcodes/i386-tbl.h"
3628
3629
static void
3630
op_lookup (const char *mnemonic)
3631
73.0k
{
3632
73.0k
   i386_op_off_t *pos = str_hash_find (op_hash, mnemonic);
3633
3634
73.0k
   if (pos != NULL)
3635
24.4k
     {
3636
24.4k
       current_templates.start = &i386_optab[pos[0]];
3637
24.4k
       current_templates.end = &i386_optab[pos[1]];
3638
24.4k
     }
3639
48.6k
   else
3640
48.6k
     current_templates.end = current_templates.start = NULL;
3641
73.0k
}
3642
3643
void
3644
md_begin (void)
3645
478
{
3646
  /* Make sure possible padding space is clear.  */
3647
478
  memset (&pp, 0, sizeof (pp));
3648
3649
  /* Initialize op_hash hash table.  */
3650
478
  op_hash = str_htab_create ();
3651
3652
478
  {
3653
478
    const i386_op_off_t *cur = i386_op_sets;
3654
478
    const i386_op_off_t *end = cur + ARRAY_SIZE (i386_op_sets) - 1;
3655
3656
1.24M
    for (; cur < end; ++cur)
3657
1.24M
      if (str_hash_insert (op_hash, insn_name (&i386_optab[*cur]), cur, 0))
3658
0
  as_fatal (_("duplicate %s"), insn_name (&i386_optab[*cur]));
3659
478
  }
3660
3661
  /* Initialize reg_hash hash table.  */
3662
478
  reg_hash = str_htab_create ();
3663
478
  {
3664
478
    const reg_entry *regtab;
3665
478
    unsigned int regtab_size = i386_regtab_size;
3666
3667
168k
    for (regtab = i386_regtab; regtab_size--; regtab++)
3668
168k
      {
3669
168k
  switch (regtab->reg_type.bitfield.class)
3670
168k
    {
3671
65.0k
    case Reg:
3672
65.0k
      if (regtab->reg_type.bitfield.dword)
3673
15.2k
        {
3674
15.2k
    if (regtab->reg_type.bitfield.instance == Accum)
3675
478
      reg_eax = regtab;
3676
15.2k
        }
3677
65.0k
      break;
3678
3679
3.82k
    case RegFP:
3680
      /* There's no point inserting st(<N>) in the hash table, as
3681
         parentheses aren't included in register_chars[] anyway.  */
3682
3.82k
      if (regtab->reg_type.bitfield.instance != Accum)
3683
3.34k
        continue;
3684
478
      reg_st0 = regtab;
3685
478
      break;
3686
3687
3.34k
    case SReg:
3688
3.34k
      switch (regtab->reg_num)
3689
3.34k
        {
3690
478
        case 0: reg_es = regtab; break;
3691
478
        case 2: reg_ss = regtab; break;
3692
478
        case 3: reg_ds = regtab; break;
3693
3.34k
        }
3694
3.34k
      break;
3695
3696
3.82k
    case RegMask:
3697
3.82k
      if (!regtab->reg_num)
3698
478
        reg_k0 = regtab;
3699
3.82k
      break;
3700
168k
    }
3701
3702
164k
  if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3703
0
    as_fatal (_("duplicate %s"), regtab->reg_name);
3704
164k
      }
3705
478
  }
3706
3707
  /* Fill in lexical tables:  mnemonic_chars, operand_chars.  */
3708
478
  {
3709
478
    int c;
3710
478
    const char *p;
3711
3712
122k
    for (c = 0; c < 256; c++)
3713
122k
      {
3714
122k
  if (ISDIGIT (c) || ISLOWER (c))
3715
17.2k
    {
3716
17.2k
      mnemonic_chars[c] = c;
3717
17.2k
      register_chars[c] = c;
3718
17.2k
      operand_chars[c] = c;
3719
17.2k
    }
3720
105k
  else if (ISUPPER (c))
3721
12.4k
    {
3722
12.4k
      mnemonic_chars[c] = TOLOWER (c);
3723
12.4k
      register_chars[c] = mnemonic_chars[c];
3724
12.4k
      operand_chars[c] = c;
3725
12.4k
    }
3726
#ifdef SVR4_COMMENT_CHARS
3727
  else if (c == '\\' && strchr (i386_comment_chars, '/'))
3728
    operand_chars[c] = c;
3729
#endif
3730
3731
122k
  if (c >= 128)
3732
61.1k
    operand_chars[c] = c;
3733
122k
      }
3734
3735
478
    mnemonic_chars['_'] = '_';
3736
478
    mnemonic_chars['-'] = '-';
3737
478
    mnemonic_chars['.'] = '.';
3738
3739
2.86k
    for (p = extra_symbol_chars; *p != '\0'; p++)
3740
2.39k
      operand_chars[(unsigned char) *p] = *p;
3741
10.0k
    for (p = operand_special_chars; *p != '\0'; p++)
3742
9.56k
      operand_chars[(unsigned char) *p] = *p;
3743
478
  }
3744
3745
478
  if (object_64bit)
3746
478
    {
3747
#if defined (OBJ_COFF) && defined (TE_PE)
3748
      x86_dwarf2_return_column = 32;
3749
#else
3750
478
      x86_dwarf2_return_column = REG_RA;
3751
478
#endif
3752
478
      x86_cie_data_alignment = -8;
3753
478
    }
3754
0
  else
3755
0
    {
3756
0
      x86_dwarf2_return_column = 8;
3757
0
      x86_cie_data_alignment = -4;
3758
0
    }
3759
3760
  /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3761
     can be turned into BRANCH_PREFIX frag.  */
3762
478
  if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3763
0
    abort ();
3764
478
}
3765
3766
void
3767
i386_print_statistics (FILE *file)
3768
0
{
3769
0
  htab_print_statistics (file, "i386 opcode", op_hash);
3770
0
  htab_print_statistics (file, "i386 register", reg_hash);
3771
0
}
3772
3773
void
3774
i386_md_end (void)
3775
478
{
3776
478
  if (!ENABLE_LEAK_CHECK)
3777
0
    return;
3778
478
  htab_delete (op_hash);
3779
478
  htab_delete (reg_hash);
3780
478
  GOT_symbol = NULL;
3781
478
}
3782

3783
#ifdef DEBUG386
3784
3785
/* Debugging routines for md_assemble.  */
3786
static void pte (insn_template *);
3787
static void pt (i386_operand_type);
3788
static void pe (expressionS *);
3789
static void ps (symbolS *);
3790
3791
static void
3792
pi (const char *line, i386_insn *x)
3793
{
3794
  unsigned int j;
3795
3796
  fprintf (stdout, "%s: template ", line);
3797
  pte (&x->tm);
3798
  fprintf (stdout, "  address: base %s  index %s  scale %x\n",
3799
     x->base_reg ? x->base_reg->reg_name : "none",
3800
     x->index_reg ? x->index_reg->reg_name : "none",
3801
     x->log2_scale_factor);
3802
  fprintf (stdout, "  modrm:  mode %x  reg %x  reg/mem %x\n",
3803
     x->rm.mode, x->rm.reg, x->rm.regmem);
3804
  fprintf (stdout, "  sib:  base %x  index %x  scale %x\n",
3805
     x->sib.base, x->sib.index, x->sib.scale);
3806
  fprintf (stdout, "  rex: 64bit %x  extX %x  extY %x  extZ %x\n",
3807
     (x->rex & REX_W) != 0,
3808
     (x->rex & REX_R) != 0,
3809
     (x->rex & REX_X) != 0,
3810
     (x->rex & REX_B) != 0);
3811
  for (j = 0; j < x->operands; j++)
3812
    {
3813
      fprintf (stdout, "    #%d:  ", j + 1);
3814
      pt (x->types[j]);
3815
      fprintf (stdout, "\n");
3816
      if (x->types[j].bitfield.class == Reg
3817
    || x->types[j].bitfield.class == RegFP
3818
    || x->types[j].bitfield.class == RegMMX
3819
    || x->types[j].bitfield.class == RegSIMD
3820
    || x->types[j].bitfield.class == RegMask
3821
    || x->types[j].bitfield.class == SReg
3822
    || x->types[j].bitfield.class == RegCR
3823
    || x->types[j].bitfield.class == RegDR
3824
    || x->types[j].bitfield.class == RegTR
3825
    || x->types[j].bitfield.class == RegBND)
3826
  fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3827
      if (operand_type_check (x->types[j], imm))
3828
  pe (x->op[j].imms);
3829
      if (operand_type_check (x->types[j], disp))
3830
  pe (x->op[j].disps);
3831
    }
3832
}
3833
3834
static void
3835
pte (insn_template *t)
3836
{
3837
  static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
3838
  static const char *const opc_spc[] = {
3839
    NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
3840
    "XOP08", "XOP09", "XOP0A",
3841
  };
3842
  unsigned int j;
3843
3844
  fprintf (stdout, " %d operands ", t->operands);
3845
  if (opc_pfx[t->opcode_modifier.opcodeprefix])
3846
    fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3847
  if (opc_spc[t->opcode_space])
3848
    fprintf (stdout, "space %s ", opc_spc[t->opcode_space]);
3849
  fprintf (stdout, "opcode %x ", t->base_opcode);
3850
  if (t->extension_opcode != None)
3851
    fprintf (stdout, "ext %x ", t->extension_opcode);
3852
  if (t->opcode_modifier.d)
3853
    fprintf (stdout, "D");
3854
  if (t->opcode_modifier.w)
3855
    fprintf (stdout, "W");
3856
  fprintf (stdout, "\n");
3857
  for (j = 0; j < t->operands; j++)
3858
    {
3859
      fprintf (stdout, "    #%d type ", j + 1);
3860
      pt (t->operand_types[j]);
3861
      fprintf (stdout, "\n");
3862
    }
3863
}
3864
3865
static void
3866
pe (expressionS *e)
3867
{
3868
  fprintf (stdout, "    operation     %d\n", e->X_op);
3869
  fprintf (stdout, "    add_number    %" PRId64 " (%" PRIx64 ")\n",
3870
     (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
3871
  if (e->X_add_symbol)
3872
    {
3873
      fprintf (stdout, "    add_symbol    ");
3874
      ps (e->X_add_symbol);
3875
      fprintf (stdout, "\n");
3876
    }
3877
  if (e->X_op_symbol)
3878
    {
3879
      fprintf (stdout, "    op_symbol    ");
3880
      ps (e->X_op_symbol);
3881
      fprintf (stdout, "\n");
3882
    }
3883
}
3884
3885
static void
3886
ps (symbolS *s)
3887
{
3888
  fprintf (stdout, "%s type %s%s",
3889
     S_GET_NAME (s),
3890
     S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3891
     segment_name (S_GET_SEGMENT (s)));
3892
}
3893
3894
static struct type_name
3895
  {
3896
    i386_operand_type mask;
3897
    const char *name;
3898
  }
3899
const type_names[] =
3900
{
3901
  { { .bitfield = { .class = Reg, .byte = 1 } }, "r8" },
3902
  { { .bitfield = { .class = Reg, .word = 1 } }, "r16" },
3903
  { { .bitfield = { .class = Reg, .dword = 1 } }, "r32" },
3904
  { { .bitfield = { .class = Reg, .qword = 1 } }, "r64" },
3905
  { { .bitfield = { .instance = Accum, .byte = 1 } }, "acc8" },
3906
  { { .bitfield = { .instance = Accum, .word = 1 } }, "acc16" },
3907
  { { .bitfield = { .instance = Accum, .dword = 1 } }, "acc32" },
3908
  { { .bitfield = { .instance = Accum, .qword = 1 } }, "acc64" },
3909
  { { .bitfield = { .imm8 = 1 } }, "i8" },
3910
  { { .bitfield = { .imm8s = 1 } }, "i8s" },
3911
  { { .bitfield = { .imm16 = 1 } }, "i16" },
3912
  { { .bitfield = { .imm32 = 1 } }, "i32" },
3913
  { { .bitfield = { .imm32s = 1 } }, "i32s" },
3914
  { { .bitfield = { .imm64 = 1 } }, "i64" },
3915
  { { .bitfield = { .imm1 = 1 } }, "i1" },
3916
  { { .bitfield = { .baseindex = 1 } }, "BaseIndex" },
3917
  { { .bitfield = { .disp8 = 1 } }, "d8" },
3918
  { { .bitfield = { .disp16 = 1 } }, "d16" },
3919
  { { .bitfield = { .disp32 = 1 } }, "d32" },
3920
  { { .bitfield = { .disp64 = 1 } }, "d64" },
3921
  { { .bitfield = { .instance = RegD, .word = 1 } }, "InOutPortReg" },
3922
  { { .bitfield = { .instance = RegC, .byte = 1 } }, "ShiftCount" },
3923
  { { .bitfield = { .class = RegCR } }, "control reg" },
3924
  { { .bitfield = { .class = RegTR } }, "test reg" },
3925
  { { .bitfield = { .class = RegDR } }, "debug reg" },
3926
  { { .bitfield = { .class = RegFP, .tbyte = 1 } }, "FReg" },
3927
  { { .bitfield = { .instance = Accum, .tbyte = 1 } }, "FAcc" },
3928
  { { .bitfield = { .class = SReg } }, "SReg" },
3929
  { { .bitfield = { .class = RegMMX } }, "rMMX" },
3930
  { { .bitfield = { .class = RegSIMD, .xmmword = 1 } }, "rXMM" },
3931
  { { .bitfield = { .class = RegSIMD, .ymmword = 1 } }, "rYMM" },
3932
  { { .bitfield = { .class = RegSIMD, .zmmword = 1 } }, "rZMM" },
3933
  { { .bitfield = { .class = RegSIMD, .tmmword = 1 } }, "rTMM" },
3934
  { { .bitfield = { .class = RegMask } }, "Mask reg" },
3935
};
3936
3937
static void
3938
pt (i386_operand_type t)
3939
{
3940
  unsigned int j;
3941
  i386_operand_type a;
3942
3943
  for (j = 0; j < ARRAY_SIZE (type_names); j++)
3944
    {
3945
      a = operand_type_and (t, type_names[j].mask);
3946
      if (operand_type_equal (&a, &type_names[j].mask))
3947
  fprintf (stdout, "%s, ",  type_names[j].name);
3948
    }
3949
  fflush (stdout);
3950
}
3951
3952
#endif /* DEBUG386 */
3953

3954
static bfd_reloc_code_real_type
3955
_reloc (unsigned int size,
3956
  bool pcrel,
3957
  int sign,
3958
  bfd_reloc_code_real_type other,
3959
  bool code64,
3960
  const char *file,
3961
  unsigned int line)
3962
12.2k
{
3963
12.2k
  if (other != NO_RELOC)
3964
8.14k
    {
3965
8.14k
      reloc_howto_type *rel;
3966
3967
8.14k
      if (size == 8)
3968
6
  switch (other)
3969
6
    {
3970
0
    case BFD_RELOC_64_PLTOFF:
3971
3
    case BFD_RELOC_X86_64_GOTPLT64:
3972
3
      return other;
3973
0
    case BFD_RELOC_X86_64_GOT32:
3974
0
      return BFD_RELOC_X86_64_GOT64;
3975
0
    case BFD_RELOC_X86_64_GOTPC32:
3976
0
      other = BFD_RELOC_64_GOT_PCREL;
3977
0
      break;
3978
2
    case BFD_RELOC_X86_64_GOTPCREL:
3979
2
      other = BFD_RELOC_X86_64_GOTPCREL64;
3980
2
      break;
3981
1
    case BFD_RELOC_X86_64_TPOFF32:
3982
1
      other = BFD_RELOC_X86_64_TPOFF64;
3983
1
      break;
3984
0
    case BFD_RELOC_X86_64_DTPOFF32:
3985
0
      other = BFD_RELOC_X86_64_DTPOFF64;
3986
0
      break;
3987
0
    default:
3988
0
      break;
3989
6
    }
3990
3991
8.14k
#ifdef OBJ_ELF
3992
8.14k
      if (other == BFD_RELOC_SIZE32)
3993
0
  {
3994
0
    if (size == 8)
3995
0
      other = BFD_RELOC_SIZE64;
3996
0
    if (pcrel)
3997
0
      {
3998
0
        as_bad_where (file, line,
3999
0
          _("there are no pc-relative size relocations"));
4000
0
        return NO_RELOC;
4001
0
      }
4002
0
  }
4003
8.14k
#endif
4004
4005
      /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless.  */
4006
8.14k
      if (size == 4 && (!code64 || disallow_64bit_reloc))
4007
2
  sign = -1;
4008
4009
8.14k
      rel = bfd_reloc_type_lookup (stdoutput, other);
4010
8.14k
      if (!rel)
4011
0
  as_bad_where (file, line, _("unknown relocation (%u)"), other);
4012
8.14k
      else if (size != bfd_get_reloc_size (rel))
4013
2
  as_bad_where (file, line,
4014
2
          _("%u-byte relocation cannot be applied to %u-byte field"),
4015
2
          bfd_get_reloc_size (rel), size);
4016
8.14k
      else if (pcrel && !rel->pc_relative)
4017
0
  as_bad_where (file, line,
4018
0
          _("non-pc-relative relocation for pc-relative field"));
4019
8.14k
      else if ((rel->complain_on_overflow == complain_overflow_signed
4020
8.14k
    && !sign)
4021
8.14k
         || (rel->complain_on_overflow == complain_overflow_unsigned
4022
0
       && sign > 0))
4023
0
  as_bad_where (file, line,
4024
0
          _("relocated field and relocation type differ in signedness"));
4025
8.14k
      else
4026
8.14k
  return other;
4027
2
      return NO_RELOC;
4028
8.14k
    }
4029
4030
4.08k
  if (pcrel)
4031
47
    {
4032
47
      if (!sign)
4033
0
  as_bad_where (file, line,
4034
0
          _("there are no unsigned pc-relative relocations"));
4035
47
      switch (size)
4036
47
  {
4037
10
  case 1: return BFD_RELOC_8_PCREL;
4038
25
  case 2: return BFD_RELOC_16_PCREL;
4039
12
  case 4: return BFD_RELOC_32_PCREL;
4040
0
  case 8: return BFD_RELOC_64_PCREL;
4041
47
  }
4042
0
      as_bad_where (file, line,
4043
0
        _("cannot do %u byte pc-relative relocation"), size);
4044
0
    }
4045
4.03k
  else
4046
4.03k
    {
4047
4.03k
      if (sign > 0)
4048
279
  switch (size)
4049
279
    {
4050
279
    case 4: return BFD_RELOC_X86_64_32S;
4051
279
    }
4052
3.76k
      else
4053
3.76k
  switch (size)
4054
3.76k
    {
4055
2.08k
    case 1: return BFD_RELOC_8;
4056
459
    case 2: return BFD_RELOC_16;
4057
1.04k
    case 4: return BFD_RELOC_32;
4058
160
    case 8: return BFD_RELOC_64;
4059
3.76k
    }
4060
13
      as_bad_where (file, line, _("cannot do %s %u byte relocation"),
4061
13
        sign > 0 ? "signed" : "unsigned", size);
4062
13
    }
4063
4064
13
  return NO_RELOC;
4065
4.08k
}
4066
4067
static bfd_reloc_code_real_type
4068
reloc (unsigned int size,
4069
       bool pcrel,
4070
       int sign,
4071
       bfd_reloc_code_real_type other)
4072
12.2k
{
4073
12.2k
  return _reloc (size, pcrel, sign, other, flag_code == CODE_64BIT, NULL, 0);
4074
12.2k
}
4075
4076
#ifdef OBJ_ELF
4077
/* Here we decide which fixups can be adjusted to make them relative to
4078
   the beginning of the section instead of the symbol.  Basically we need
4079
   to make sure that the dynamic relocations are done correctly, so in
4080
   some cases we force the original symbol to be used.  */
4081
4082
int
4083
tc_i386_fix_adjustable (fixS *fixP)
4084
0
{
4085
  /* Don't adjust pc-relative references to merge sections in 64-bit
4086
     mode.  */
4087
0
  if (use_rela_relocations
4088
0
      && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
4089
0
      && fixP->fx_pcrel)
4090
0
    return 0;
4091
4092
  /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
4093
     and changed later by validate_fix.  */
4094
0
  if (GOT_symbol && fixP->fx_subsy == GOT_symbol
4095
0
      && fixP->fx_r_type == BFD_RELOC_32_PCREL)
4096
0
    return 0;
4097
4098
  /* Adjust_reloc_syms doesn't know about the GOT.  Need to keep symbol
4099
     for size relocations.  */
4100
0
  if (fixP->fx_r_type == BFD_RELOC_SIZE32
4101
0
      || fixP->fx_r_type == BFD_RELOC_SIZE64
4102
0
      || fixP->fx_r_type == BFD_RELOC_32_GOTOFF
4103
0
      || fixP->fx_r_type == BFD_RELOC_386_GOT32
4104
0
      || fixP->fx_r_type == BFD_RELOC_386_GOT32X
4105
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
4106
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
4107
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
4108
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
4109
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
4110
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
4111
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
4112
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
4113
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
4114
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
4115
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
4116
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
4117
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
4118
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
4119
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_4_GOTPCRELX
4120
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_5_GOTPCRELX
4121
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_6_GOTPCRELX
4122
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
4123
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
4124
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
4125
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
4126
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
4127
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_4_GOTTPOFF
4128
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_5_GOTTPOFF
4129
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_6_GOTTPOFF
4130
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
4131
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
4132
0
      || fixP->fx_r_type == BFD_RELOC_64_GOTOFF
4133
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOT64
4134
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
4135
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC
4136
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_5_GOTPC32_TLSDESC
4137
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_CODE_6_GOTPC32_TLSDESC
4138
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
4139
0
      || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
4140
0
      || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
4141
0
    return 0;
4142
  /* Resolve PLT32 relocation against local symbol to section only for
4143
     PC-relative relocations.  */
4144
0
  if (fixP->fx_r_type == BFD_RELOC_386_PLT32
4145
0
      || fixP->fx_r_type == BFD_RELOC_32_PLT_PCREL)
4146
0
    return fixP->fx_pcrel;
4147
0
  return 1;
4148
0
}
4149
#endif
4150
4151
static INLINE bool
4152
want_disp32 (const insn_template *t)
4153
22.0k
{
4154
22.0k
  return flag_code != CODE_64BIT
4155
20.1k
   || i.prefix[ADDR_PREFIX]
4156
20.1k
   || ((t->mnem_off == MN_lea
4157
20.1k
        || (i.tm.base_opcode == 0x8d && i.tm.opcode_space == SPACE_BASE))
4158
0
       && (!i.types[1].bitfield.qword
4159
0
     || t->opcode_modifier.size == SIZE32));
4160
22.0k
}
4161
4162
static INLINE bool is_padlock (const insn_template *t)
4163
11.4k
{
4164
  /* (Ab)use the PrefixRepe attribute of PadLock insns as long as no
4165
     others use it.  */
4166
11.4k
  return t->opcode_modifier.prefixok == PrefixRepe;
4167
11.4k
}
4168
4169
static int
4170
intel_float_operand (const char *mnemonic)
4171
7.22k
{
4172
  /* Note that the value returned is meaningful only for opcodes with (memory)
4173
     operands, hence the code here is free to improperly handle opcodes that
4174
     have no operands (for better performance and smaller code). */
4175
4176
7.22k
  if (mnemonic[0] != 'f')
4177
5.97k
    return 0; /* non-math */
4178
4179
1.24k
  switch (mnemonic[1])
4180
1.24k
    {
4181
    /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
4182
       the fs segment override prefix not currently handled because no
4183
       call path can make opcodes without operands get here */
4184
1.09k
    case 'i':
4185
1.09k
      return 2 /* integer op */;
4186
0
    case 'l':
4187
0
      if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
4188
0
  return 3; /* fldcw/fldenv */
4189
0
      break;
4190
11
    case 'n':
4191
11
      if (mnemonic[2] != 'o' /* fnop */)
4192
11
  return 3; /* non-waiting control op */
4193
0
      break;
4194
0
    case 'r':
4195
0
      if (mnemonic[2] == 's')
4196
0
  return 3; /* frstor/frstpm */
4197
0
      break;
4198
113
    case 's':
4199
113
      if (mnemonic[2] == 'a')
4200
3
  return 3; /* fsave */
4201
110
      if (mnemonic[2] == 't')
4202
61
  {
4203
61
    switch (mnemonic[3])
4204
61
      {
4205
0
      case 'c': /* fstcw */
4206
0
      case 'd': /* fstdw */
4207
0
      case 'e': /* fstenv */
4208
0
      case 's': /* fsts[gw] */
4209
0
        return 3;
4210
61
      }
4211
61
  }
4212
110
      break;
4213
110
    case 'x':
4214
0
      if (mnemonic[2] == 'r' || mnemonic[2] == 's')
4215
0
  return 0; /* fxsave/fxrstor are not really math ops */
4216
0
      break;
4217
1.24k
    }
4218
4219
141
  return 1;
4220
1.24k
}
4221
4222
static INLINE void
4223
install_template (const insn_template *t)
4224
11.4k
{
4225
11.4k
  unsigned int l;
4226
4227
11.4k
  i.tm = *t;
4228
4229
  /* Dual VEX/EVEX templates need stripping one of the possible variants.  */
4230
11.4k
  if (t->opcode_modifier.vex && t->opcode_modifier.evex)
4231
2
    {
4232
2
      if ((maybe_cpu (t, CpuAVX) || maybe_cpu (t, CpuAVX2)
4233
2
     || maybe_cpu (t, CpuFMA))
4234
0
    && (maybe_cpu (t, CpuAVX512F) || maybe_cpu (t, CpuAVX512VL)))
4235
0
  {
4236
0
    if (need_evex_encoding (t))
4237
0
      {
4238
0
        i.tm.opcode_modifier.vex = 0;
4239
0
        i.tm.cpu.bitfield.cpuavx512f = i.tm.cpu_any.bitfield.cpuavx512f;
4240
0
        i.tm.cpu.bitfield.cpuavx512vl = i.tm.cpu_any.bitfield.cpuavx512vl;
4241
0
      }
4242
0
    else
4243
0
      {
4244
0
        i.tm.opcode_modifier.evex = 0;
4245
0
        if (i.tm.cpu_any.bitfield.cpuavx)
4246
0
    i.tm.cpu.bitfield.cpuavx = 1;
4247
0
        else if (!i.tm.cpu.bitfield.isa)
4248
0
    i.tm.cpu.bitfield.isa = i.tm.cpu_any.bitfield.isa;
4249
0
        else
4250
0
    gas_assert (i.tm.cpu.bitfield.isa == i.tm.cpu_any.bitfield.isa);
4251
0
      }
4252
0
  }
4253
4254
2
      if ((maybe_cpu (t, CpuCMPCCXADD) || maybe_cpu (t, CpuAMX_TILE)
4255
0
     || maybe_cpu (t, CpuAVX512F) || maybe_cpu (t, CpuAVX512DQ)
4256
0
     || maybe_cpu (t, CpuAVX512BW) || maybe_cpu (t, CpuBMI)
4257
0
     || maybe_cpu (t, CpuBMI2) || maybe_cpu (t, CpuUSER_MSR)
4258
0
     || maybe_cpu (t, CpuMSR_IMM) || maybe_cpu (t, CpuAMX_TRANSPOSE)
4259
0
     || maybe_cpu (t, CpuAMX_MOVRS))
4260
2
    && maybe_cpu (t, CpuAPX_F))
4261
2
  {
4262
2
    if (need_evex_encoding (t))
4263
1
      i.tm.opcode_modifier.vex = 0;
4264
1
    else
4265
1
      i.tm.opcode_modifier.evex = 0;
4266
2
  }
4267
2
    }
4268
4269
  /* For CCMP and CTEST the template has EVEX.SCC in base_opcode. Move it out of
4270
     there, to then adjust base_opcode to obtain its normal meaning.  */
4271
11.4k
  if (i.tm.opcode_modifier.operandconstraint == SCC)
4272
1
    {
4273
      /* Get EVEX.SCC value from the lower 4 bits of base_opcode.  */
4274
1
      i.scc = i.tm.base_opcode & 0xf;
4275
1
      i.tm.base_opcode >>= 8;
4276
1
    }
4277
4278
  /* For CMOVcc having undergone NDD-to-legacy optimization with its source
4279
     operands being swapped, we need to invert the encoded condition.  */
4280
11.4k
  if (i.invert_cond)
4281
0
    i.tm.base_opcode ^= 1;
4282
4283
  /* Note that for pseudo prefixes this produces a length of 1. But for them
4284
     the length isn't interesting at all.  */
4285
11.4k
  for (l = 1; l < 4; ++l)
4286
11.4k
    if (!(i.tm.base_opcode >> (8 * l)))
4287
11.4k
      break;
4288
4289
11.4k
  i.opcode_length = l;
4290
11.4k
}
4291
4292
/* Build the VEX prefix.  */
4293
4294
static void
4295
build_vex_prefix (const insn_template *t)
4296
319
{
4297
319
  unsigned int register_specifier;
4298
319
  unsigned int vector_length;
4299
319
  bool w;
4300
4301
  /* Check register specifier.  */
4302
319
  if (i.vex.register_specifier)
4303
2
    {
4304
2
      register_specifier =
4305
2
  ~register_number (i.vex.register_specifier) & 0xf;
4306
2
      gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
4307
2
    }
4308
317
  else
4309
317
    register_specifier = 0xf;
4310
4311
  /* Use 2-byte VEX prefix by swapping destination and source operand
4312
     if there are more than 1 register operand.  */
4313
319
  if (i.reg_operands > 1
4314
0
      && pp.encoding != encoding_vex3
4315
0
      && pp.dir_encoding == dir_encoding_default
4316
0
      && i.operands == i.reg_operands
4317
0
      && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
4318
0
      && i.tm.opcode_space == SPACE_0F
4319
0
      && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
4320
0
      && i.rex == REX_B)
4321
0
    {
4322
0
      unsigned int xchg;
4323
4324
0
      swap_2_operands (0, i.operands - 1);
4325
4326
0
      gas_assert (i.rm.mode == 3);
4327
4328
0
      i.rex = REX_R;
4329
0
      xchg = i.rm.regmem;
4330
0
      i.rm.regmem = i.rm.reg;
4331
0
      i.rm.reg = xchg;
4332
4333
0
      if (i.tm.opcode_modifier.d)
4334
0
  i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
4335
0
          ? Opcode_ExtD : Opcode_SIMD_IntD;
4336
0
      else /* Use the next insn.  */
4337
0
  install_template (&t[1]);
4338
0
    }
4339
4340
  /* Use 2-byte VEX prefix by swapping commutative source operands if there
4341
     are no memory operands and at least 3 register ones.  */
4342
319
  if (i.reg_operands >= 3
4343
0
      && pp.encoding != encoding_vex3
4344
0
      && i.reg_operands == i.operands - i.imm_operands
4345
0
      && i.tm.opcode_modifier.vex
4346
0
      && i.tm.opcode_modifier.commutative
4347
      /* .commutative aliases .staticrounding; disambiguate.  */
4348
0
      && !i.tm.opcode_modifier.sae
4349
0
      && (i.tm.opcode_modifier.sse2avx
4350
0
    || (optimize > 1 && !pp.no_optimize))
4351
0
      && i.rex == REX_B
4352
0
      && i.vex.register_specifier
4353
0
      && !(i.vex.register_specifier->reg_flags & RegRex))
4354
0
    {
4355
0
      unsigned int xchg = i.operands - i.reg_operands;
4356
4357
0
      gas_assert (i.tm.opcode_space == SPACE_0F);
4358
0
      gas_assert (!i.tm.opcode_modifier.sae);
4359
0
      gas_assert (operand_type_equal (&i.types[i.operands - 2],
4360
0
                                      &i.types[i.operands - 3]));
4361
0
      gas_assert (i.rm.mode == 3);
4362
4363
0
      swap_2_operands (xchg, xchg + 1);
4364
4365
0
      i.rex = 0;
4366
0
      xchg = i.rm.regmem | 8;
4367
0
      i.rm.regmem = ~register_specifier & 0xf;
4368
0
      gas_assert (!(i.rm.regmem & 8));
4369
0
      i.vex.register_specifier += xchg - i.rm.regmem;
4370
0
      register_specifier = ~xchg & 0xf;
4371
0
    }
4372
4373
319
  if (i.tm.opcode_modifier.vex == VEXScalar)
4374
317
    vector_length = avxscalar;
4375
2
  else if (i.tm.opcode_modifier.vex == VEX256)
4376
0
    vector_length = 1;
4377
2
  else if (dot_insn () && i.tm.opcode_modifier.vex == VEX128)
4378
0
    vector_length = 0;
4379
2
  else
4380
2
    {
4381
2
      unsigned int op;
4382
4383
      /* Determine vector length from the last multi-length vector
4384
   operand.  */
4385
2
      vector_length = 0;
4386
3
      for (op = t->operands; op--;)
4387
1
  if (t->operand_types[op].bitfield.xmmword
4388
0
      && t->operand_types[op].bitfield.ymmword
4389
0
      && i.types[op].bitfield.ymmword)
4390
0
    {
4391
0
      vector_length = 1;
4392
0
      break;
4393
0
    }
4394
2
    }
4395
4396
  /* Check the REX.W bit and VEXW.  */
4397
319
  if (i.tm.opcode_modifier.vexw == VEXWIG)
4398
77
    w = vexwig == vexw1 || (i.rex & REX_W);
4399
242
  else if (i.tm.opcode_modifier.vexw && !(i.rex & REX_W))
4400
11
    w = i.tm.opcode_modifier.vexw == VEXW1;
4401
231
  else
4402
231
    w = flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1;
4403
4404
  /* Use 2-byte VEX prefix if possible.  */
4405
319
  if (w == 0
4406
318
      && pp.encoding != encoding_vex3
4407
318
      && i.tm.opcode_space == SPACE_0F
4408
1
      && (i.rex & (REX_W | REX_X | REX_B)) == 0)
4409
1
    {
4410
      /* 2-byte VEX prefix.  */
4411
1
      bool r;
4412
4413
1
      i.vex.length = 2;
4414
1
      i.vex.bytes[0] = 0xc5;
4415
4416
      /* Check the REX.R bit.  */
4417
1
      r = !(i.rex & REX_R);
4418
1
      i.vex.bytes[1] = (r << 7
4419
1
      | register_specifier << 3
4420
1
      | vector_length << 2
4421
1
      | i.tm.opcode_modifier.opcodeprefix);
4422
1
    }
4423
318
  else
4424
318
    {
4425
      /* 3-byte VEX prefix.  */
4426
318
      i.vex.length = 3;
4427
4428
318
      switch (i.tm.opcode_space)
4429
318
  {
4430
0
  case SPACE_0F:
4431
318
  case SPACE_0F38:
4432
318
  case SPACE_0F3A:
4433
318
  case SPACE_MAP5:
4434
318
  case SPACE_MAP7:
4435
318
    i.vex.bytes[0] = 0xc4;
4436
318
    break;
4437
0
  case SPACE_XOP08:
4438
0
  case SPACE_XOP09:
4439
0
  case SPACE_XOP0A:
4440
0
    i.vex.bytes[0] = 0x8f;
4441
0
    break;
4442
0
  default:
4443
0
    abort ();
4444
318
  }
4445
4446
      /* The high 3 bits of the second VEX byte are 1's compliment
4447
   of RXB bits from REX.  */
4448
318
      i.vex.bytes[1] = ((~i.rex & 7) << 5)
4449
318
           | (!dot_insn () ? i.tm.opcode_space
4450
318
               : i.insn_opcode_space);
4451
4452
318
      i.vex.bytes[2] = (w << 7
4453
318
      | register_specifier << 3
4454
318
      | vector_length << 2
4455
318
      | i.tm.opcode_modifier.opcodeprefix);
4456
318
    }
4457
319
}
4458
4459
static INLINE bool
4460
is_any_vex_encoding (const insn_template *t)
4461
36.3k
{
4462
36.3k
  return t->opcode_modifier.vex || t->opcode_modifier.evex;
4463
36.3k
}
4464
4465
/* We can use this function only when the current encoding is evex.  */
4466
static INLINE bool
4467
is_apx_evex_encoding (void)
4468
366
{
4469
366
  return i.rex2 || i.tm.opcode_space == SPACE_MAP4 || pp.has_nf
4470
360
    || (i.vex.register_specifier
4471
4
  && (i.vex.register_specifier->reg_flags & RegRex2));
4472
366
}
4473
4474
static INLINE bool
4475
is_apx_rex2_encoding (void)
4476
22.7k
{
4477
22.7k
  return i.rex2 || pp.rex2_encoding
4478
22.7k
  || i.tm.opcode_modifier.rex2;
4479
22.7k
}
4480
4481
static unsigned int
4482
get_broadcast_bytes (const insn_template *t, bool diag)
4483
0
{
4484
0
  unsigned int op, bytes;
4485
0
  const i386_operand_type *types;
4486
4487
0
  if (i.broadcast.type)
4488
0
    return (1 << (t->opcode_modifier.broadcast - 1)) * i.broadcast.type;
4489
4490
0
  gas_assert (intel_syntax);
4491
4492
0
  for (op = 0; op < t->operands; ++op)
4493
0
    if (t->operand_types[op].bitfield.baseindex)
4494
0
      break;
4495
4496
0
  gas_assert (op < t->operands);
4497
4498
0
  if (t->opcode_modifier.evex != EVEXDYN)
4499
0
    switch (i.broadcast.bytes)
4500
0
      {
4501
0
      case 1:
4502
0
  if (t->operand_types[op].bitfield.word)
4503
0
    return 2;
4504
      /* Fall through.  */
4505
0
      case 2:
4506
0
  if (t->operand_types[op].bitfield.dword)
4507
0
    return 4;
4508
      /* Fall through.  */
4509
0
      case 4:
4510
0
  if (t->operand_types[op].bitfield.qword)
4511
0
    return 8;
4512
      /* Fall through.  */
4513
0
      case 8:
4514
0
  if (t->operand_types[op].bitfield.xmmword)
4515
0
    return 16;
4516
0
  if (t->operand_types[op].bitfield.ymmword)
4517
0
    return 32;
4518
0
  if (t->operand_types[op].bitfield.zmmword)
4519
0
    return 64;
4520
      /* Fall through.  */
4521
0
      default:
4522
0
        abort ();
4523
0
      }
4524
4525
0
  gas_assert (op + 1 < t->operands);
4526
4527
0
  if (t->operand_types[op + 1].bitfield.xmmword
4528
0
      + t->operand_types[op + 1].bitfield.ymmword
4529
0
      + t->operand_types[op + 1].bitfield.zmmword > 1)
4530
0
    {
4531
0
      types = &i.types[op + 1];
4532
0
      diag = false;
4533
0
    }
4534
0
  else /* Ambiguous - guess with a preference to non-AVX512VL forms.  */
4535
0
    types = &t->operand_types[op];
4536
4537
0
  if (types->bitfield.zmmword)
4538
0
    bytes = 64;
4539
0
  else if (types->bitfield.ymmword)
4540
0
    bytes = 32;
4541
0
  else
4542
0
    bytes = 16;
4543
4544
0
  if (diag)
4545
0
    as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
4546
0
       insn_name (t), bytes * 8);
4547
4548
0
  return bytes;
4549
0
}
4550
4551
/* Build the EVEX prefix.  */
4552
4553
static void
4554
build_evex_prefix (void)
4555
47
{
4556
47
  unsigned int register_specifier;
4557
47
  bool w;
4558
47
  rex_byte vrex_used = 0;
4559
4560
  /* Check register specifier.  */
4561
47
  if (i.vex.register_specifier)
4562
5
    {
4563
5
      gas_assert ((i.vrex & REX_X) == 0);
4564
4565
5
      register_specifier = i.vex.register_specifier->reg_num;
4566
5
      if ((i.vex.register_specifier->reg_flags & RegRex))
4567
2
  register_specifier += 8;
4568
      /* The upper 16 registers are encoded in the fourth byte of the
4569
   EVEX prefix.  */
4570
5
      if (!(i.vex.register_specifier->reg_flags & RegVRex))
4571
5
  i.vex.bytes[3] = 0x8;
4572
5
      register_specifier = ~register_specifier & 0xf;
4573
5
    }
4574
42
  else
4575
42
    {
4576
42
      register_specifier = 0xf;
4577
4578
      /* Encode upper 16 vector index register in the fourth byte of
4579
   the EVEX prefix.  */
4580
42
      if (!(i.vrex & REX_X))
4581
42
  i.vex.bytes[3] = 0x8;
4582
0
      else
4583
0
  vrex_used |= REX_X;
4584
42
    }
4585
4586
  /* 4 byte EVEX prefix.  */
4587
47
  i.vex.length = 4;
4588
47
  i.vex.bytes[0] = 0x62;
4589
4590
  /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
4591
     bits from REX.  */
4592
47
  gas_assert (i.tm.opcode_space >= SPACE_0F);
4593
47
  gas_assert (i.tm.opcode_space <= SPACE_MAP7);
4594
47
  i.vex.bytes[1] = ((~i.rex & 7) << 5)
4595
47
       | (!dot_insn () ? i.tm.opcode_space
4596
47
           : i.insn_opcode_space);
4597
4598
  /* The fifth bit of the second EVEX byte is 1's compliment of the
4599
     REX_R bit in VREX.  */
4600
47
  if (!(i.vrex & REX_R))
4601
47
    i.vex.bytes[1] |= 0x10;
4602
0
  else
4603
0
    vrex_used |= REX_R;
4604
4605
47
  if ((i.reg_operands + i.imm_operands) == i.operands)
4606
40
    {
4607
      /* When all operands are registers, the REX_X bit in REX is not
4608
   used.  We reuse it to encode the upper 16 registers, which is
4609
   indicated by the REX_B bit in VREX.  The REX_X bit is encoded
4610
   as 1's compliment.  */
4611
40
      if ((i.vrex & REX_B))
4612
0
  {
4613
0
    vrex_used |= REX_B;
4614
0
    i.vex.bytes[1] &= ~0x40;
4615
0
  }
4616
40
    }
4617
4618
  /* EVEX instructions shouldn't need the REX prefix.  */
4619
47
  i.vrex &= ~vrex_used;
4620
47
  gas_assert (i.vrex == 0);
4621
4622
  /* Check the REX.W bit and VEXW.  */
4623
47
  if (i.tm.opcode_modifier.vexw == VEXWIG)
4624
0
    w = evexwig == evexw1 || (i.rex & REX_W);
4625
47
  else if (i.tm.opcode_modifier.vexw && !(i.rex & REX_W))
4626
4
    w = i.tm.opcode_modifier.vexw == VEXW1;
4627
43
  else
4628
43
    w = flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1;
4629
4630
47
  if (i.tm.opcode_modifier.evex == EVEXDYN)
4631
0
    {
4632
0
      unsigned int op;
4633
4634
      /* Determine vector length from the last multi-length vector operand.  */
4635
0
      for (op = i.operands; op--;)
4636
0
  if (i.tm.operand_types[op].bitfield.xmmword
4637
0
      + i.tm.operand_types[op].bitfield.ymmword
4638
0
      + i.tm.operand_types[op].bitfield.zmmword > 1)
4639
0
    {
4640
0
      if (i.types[op].bitfield.zmmword)
4641
0
        {
4642
0
    i.tm.opcode_modifier.evex = EVEX512;
4643
0
    break;
4644
0
        }
4645
0
      else if (i.types[op].bitfield.ymmword)
4646
0
        {
4647
0
    i.tm.opcode_modifier.evex = EVEX256;
4648
0
    break;
4649
0
        }
4650
0
      else if (i.types[op].bitfield.xmmword)
4651
0
        {
4652
0
    i.tm.opcode_modifier.evex = EVEX128;
4653
0
    break;
4654
0
        }
4655
0
      else if ((i.broadcast.type || i.broadcast.bytes)
4656
0
          && op == i.broadcast.operand)
4657
0
        {
4658
0
    switch (get_broadcast_bytes (&i.tm, true))
4659
0
      {
4660
0
        case 64:
4661
0
          i.tm.opcode_modifier.evex = EVEX512;
4662
0
          break;
4663
0
        case 32:
4664
0
          i.tm.opcode_modifier.evex = EVEX256;
4665
0
          break;
4666
0
        case 16:
4667
0
          i.tm.opcode_modifier.evex = EVEX128;
4668
0
          break;
4669
0
        default:
4670
0
          abort ();
4671
0
      }
4672
0
    break;
4673
0
        }
4674
0
    }
4675
4676
0
      if (op >= MAX_OPERANDS)
4677
0
  abort ();
4678
0
    }
4679
4680
  /* The third byte of the EVEX prefix.  */
4681
47
  i.vex.bytes[2] = ((w << 7)
4682
47
        | (register_specifier << 3)
4683
47
        | 4 /* Encode the U bit.  */
4684
47
        | i.tm.opcode_modifier.opcodeprefix);
4685
4686
  /* The fourth byte of the EVEX prefix.  */
4687
  /* The zeroing-masking bit.  */
4688
47
  if (i.mask.reg && i.mask.zeroing)
4689
0
    i.vex.bytes[3] |= 0x80;
4690
4691
  /* Don't always set the broadcast bit if there is no RC.  */
4692
47
  if (i.rounding.type == rc_none)
4693
47
    {
4694
      /* Encode the vector length.  */
4695
47
      unsigned int vec_length;
4696
4697
47
      switch (i.tm.opcode_modifier.evex)
4698
47
  {
4699
38
  case EVEXLIG: /* LL' is ignored */
4700
38
    vec_length = evexlig << 5;
4701
38
    break;
4702
6
  case EVEX128:
4703
6
    vec_length = 0 << 5;
4704
6
    break;
4705
3
  case EVEX256:
4706
3
    vec_length = 1 << 5;
4707
3
    break;
4708
0
  case EVEX512:
4709
0
    vec_length = 2 << 5;
4710
0
    break;
4711
0
  case EVEX_L3:
4712
0
    if (dot_insn ())
4713
0
      {
4714
0
        vec_length = 3 << 5;
4715
0
        break;
4716
0
      }
4717
    /* Fall through.  */
4718
0
  default:
4719
0
    abort ();
4720
0
    break;
4721
47
  }
4722
47
      i.vex.bytes[3] |= vec_length;
4723
      /* Encode the broadcast bit.  */
4724
47
      if (i.broadcast.type || i.broadcast.bytes)
4725
0
  i.vex.bytes[3] |= 0x10;
4726
47
    }
4727
0
  else if (i.rounding.type != saeonly)
4728
0
    i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
4729
0
  else
4730
0
    i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
4731
4732
47
  if (i.mask.reg)
4733
0
    i.vex.bytes[3] |= i.mask.reg->reg_num;
4734
47
}
4735
4736
/* Build (2 bytes) rex2 prefix.
4737
   | D5h |
4738
   | m | R4 X4 B4 | W R X B |
4739
4740
   Rex2 reuses i.vex as they both encode i.tm.opcode_space in their prefixes.
4741
 */
4742
static void
4743
build_rex2_prefix (void)
4744
11
{
4745
11
  i.vex.length = 2;
4746
11
  i.vex.bytes[0] = 0xd5;
4747
  /* For the W R X B bits, the variables of rex prefix will be reused.  */
4748
11
  i.vex.bytes[1] = ((i.tm.opcode_space << 7)
4749
11
        | (i.rex2 << 4)
4750
11
        | ((i.rex | i.prefix[REX_PREFIX]) & 0xf));
4751
11
}
4752
4753
/* Build the EVEX prefix (4-byte) for evex insn
4754
   | 62h |
4755
   | `R`X`B`R' | B'mmm |
4756
   | W | v`v`v`v | `x' | pp |
4757
   | z| L'L | b | `v | aaa |
4758
*/
4759
static bool
4760
build_apx_evex_prefix (bool force_nd)
4761
8
{
4762
  /* To mimic behavior for legacy insns, transform use of DATA16 and REX64 into
4763
     their embedded-prefix representations.  */
4764
8
  if (i.tm.opcode_space == SPACE_MAP4)
4765
5
    {
4766
5
      if (i.prefix[DATA_PREFIX])
4767
0
  {
4768
0
    if (i.tm.opcode_modifier.opcodeprefix)
4769
0
      {
4770
0
        as_bad (i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66
4771
0
          ? _("same type of prefix used twice")
4772
0
          : _("conflicting use of `data16' prefix"));
4773
0
        return false;
4774
0
      }
4775
0
    i.tm.opcode_modifier.opcodeprefix = PREFIX_0X66;
4776
0
    i.prefix[DATA_PREFIX] = 0;
4777
0
  }
4778
5
      if (i.prefix[REX_PREFIX] & REX_W)
4779
0
  {
4780
0
    if (i.suffix == QWORD_MNEM_SUFFIX)
4781
0
      {
4782
0
        as_bad (_("same type of prefix used twice"));
4783
0
        return false;
4784
0
      }
4785
0
    i.tm.opcode_modifier.vexw = VEXW1;
4786
0
    i.prefix[REX_PREFIX] = 0;
4787
0
  }
4788
5
    }
4789
4790
8
  build_evex_prefix ();
4791
8
  if (i.rex2 & REX_R)
4792
1
    i.vex.bytes[1] &= ~0x10;
4793
8
  if (i.rex2 & REX_B)
4794
0
    i.vex.bytes[1] |= 0x08;
4795
8
  if (i.rex2 & REX_X)
4796
0
    {
4797
0
      gas_assert (i.rm.mode != 3);
4798
0
      i.vex.bytes[2] &= ~0x04;
4799
0
    }
4800
8
  if (i.vex.register_specifier
4801
5
      && i.vex.register_specifier->reg_flags & RegRex2)
4802
2
    i.vex.bytes[3] &= ~0x08;
4803
4804
  /* Encode the NDD bit of the instruction promoted from the legacy
4805
     space. ZU shares the same bit with NDD.  */
4806
8
  if ((i.vex.register_specifier && i.tm.opcode_space == SPACE_MAP4)
4807
5
      || i.tm.opcode_modifier.operandconstraint == ZERO_UPPER
4808
5
      || force_nd)
4809
3
    i.vex.bytes[3] |= 0x10;
4810
4811
  /* Encode SCC and oszc flags bits.  */
4812
8
  if (i.tm.opcode_modifier.operandconstraint == SCC)
4813
1
    {
4814
      /* The default value of vvvv is 1111 and needs to be cleared.  */
4815
1
      i.vex.bytes[2] &= ~0x78;
4816
1
      i.vex.bytes[2] |= (i.oszc_flags << 3);
4817
      /* ND and aaa bits shold be 0.  */
4818
1
      know (!(i.vex.bytes[3] & 0x17));
4819
      /* The default value of V' is 1 and needs to be cleared.  */
4820
1
      i.vex.bytes[3] = (i.vex.bytes[3] & ~0x08) | i.scc;
4821
1
    }
4822
4823
  /* Encode the NF bit.  */
4824
8
  if (pp.has_nf || i.tm.opcode_modifier.operandconstraint == EVEX_NF)
4825
0
    i.vex.bytes[3] |= 0x04;
4826
4827
8
  return true;
4828
8
}
4829
4830
static void establish_rex (void)
4831
11.4k
{
4832
  /* Note that legacy encodings have at most 2 non-immediate operands.  */
4833
11.4k
  unsigned int first = i.imm_operands;
4834
11.4k
  unsigned int last = i.operands > first ? i.operands - first - 1 : first;
4835
4836
  /* Respect a user-specified REX prefix.  */
4837
11.4k
  i.rex |= i.prefix[REX_PREFIX] & REX_OPCODE;
4838
4839
  /* For 8 bit RegRex64 registers without a prefix, we need an empty rex prefix.  */
4840
11.4k
  if (((i.types[first].bitfield.class == Reg
4841
266
  && (i.op[first].regs->reg_flags & RegRex64) != 0)
4842
11.4k
       || (i.types[last].bitfield.class == Reg
4843
174
     && (i.op[last].regs->reg_flags & RegRex64) != 0))
4844
0
      && !is_apx_rex2_encoding () && !is_any_vex_encoding (&i.tm))
4845
0
    i.rex |= REX_OPCODE;
4846
4847
  /* For REX/REX2/EVEX prefix instructions, we need to convert old registers
4848
     (AL, CL, DL and BL) to new ones (AXL, CXL, DXL and BXL) and reject AH,
4849
     CH, DH and BH.  */
4850
11.4k
  if (i.rex || i.rex2 || i.tm.opcode_modifier.evex)
4851
191
    {
4852
489
      for (unsigned int x = first; x <= last; x++)
4853
298
  {
4854
    /* Look for 8 bit operand that uses old registers.  */
4855
298
    if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
4856
0
        && !(i.op[x].regs->reg_flags & (RegRex | RegRex2 | RegRex64)))
4857
0
      {
4858
        /* In case it is "hi" register, give up.  */
4859
0
        if (i.op[x].regs->reg_num > 3)
4860
0
    as_bad (_("can't encode register '%s%s' in an "
4861
0
        "instruction requiring %s prefix"),
4862
0
      register_prefix, i.op[x].regs->reg_name,
4863
0
      i.tm.opcode_modifier.evex ? "EVEX" : "REX/REX2");
4864
4865
        /* Otherwise it is equivalent to the extended register.
4866
     Since the encoding doesn't change this is merely
4867
     cosmetic cleanup for debug output.  */
4868
0
        i.op[x].regs += 8;
4869
0
      }
4870
298
  }
4871
191
    }
4872
4873
11.4k
  if (i.rex == 0 && i.rex2 == 0 && (pp.rex_encoding || pp.rex2_encoding))
4874
0
    {
4875
      /* Check if we can add a REX_OPCODE byte.  Look for 8 bit operand
4876
   that uses legacy register.  If it is "hi" register, don't add
4877
   rex and rex2 prefix.  */
4878
0
      unsigned int x;
4879
4880
0
      for (x = first; x <= last; x++)
4881
0
  if (i.types[x].bitfield.class == Reg
4882
0
      && i.types[x].bitfield.byte
4883
0
      && !(i.op[x].regs->reg_flags & (RegRex | RegRex2 | RegRex64))
4884
0
      && i.op[x].regs->reg_num > 3)
4885
0
    {
4886
0
      pp.rex_encoding = false;
4887
0
      pp.rex2_encoding = false;
4888
0
      break;
4889
0
    }
4890
4891
0
      if (pp.rex_encoding)
4892
0
  i.rex = REX_OPCODE;
4893
0
    }
4894
4895
11.4k
  if (is_apx_rex2_encoding ())
4896
11
    {
4897
      /* Most prefixes are not permitted with JMPABS.  */
4898
11
      if (i.tm.mnem_off == MN_jmpabs)
4899
0
  {
4900
0
    if (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
4901
0
      {
4902
0
        as_bad (_("size override not allowed with `%s'"),
4903
0
          insn_name (&i.tm));
4904
0
        i.prefix[DATA_PREFIX] = 0;
4905
0
        i.prefix[REX_PREFIX] &= ~REX_W;
4906
0
      }
4907
0
    if (i.prefix[ADDR_PREFIX])
4908
0
      {
4909
0
        as_bad (_("address override not allowed with `%s'"),
4910
0
          insn_name (&i.tm));
4911
0
        i.prefix[ADDR_PREFIX] = 0;
4912
0
      }
4913
0
  }
4914
4915
11
      build_rex2_prefix ();
4916
      /* The individual REX.RXBW bits got consumed.  */
4917
11
      i.rex &= REX_OPCODE;
4918
11
      i.prefix[REX_PREFIX] = 0;
4919
11
    }
4920
11.4k
  else if (i.rex != 0)
4921
174
    add_prefix (REX_OPCODE | i.rex);
4922
11.4k
}
4923
4924
static void
4925
process_immext (void)
4926
0
{
4927
0
  expressionS *exp;
4928
4929
  /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
4930
     which is coded in the same place as an 8-bit immediate field
4931
     would be.  Here we fake an 8-bit immediate operand from the
4932
     opcode suffix stored in tm.extension_opcode.
4933
4934
     AVX instructions also use this encoding, for some of
4935
     3 argument instructions.  */
4936
4937
0
  gas_assert (i.imm_operands <= 1
4938
0
        && (i.operands <= 2
4939
0
      || (is_any_vex_encoding (&i.tm)
4940
0
          && i.operands <= 4)));
4941
4942
0
  exp = &im_expressions[i.imm_operands++];
4943
0
  i.op[i.operands].imms = exp;
4944
0
  i.types[i.operands].bitfield.imm8 = 1;
4945
0
  i.operands++;
4946
0
  exp->X_op = O_constant;
4947
0
  exp->X_add_number = i.tm.extension_opcode;
4948
0
  i.tm.extension_opcode = None;
4949
0
}
4950
4951
4952
static int
4953
check_hle (void)
4954
0
{
4955
0
  switch (i.tm.opcode_modifier.prefixok)
4956
0
    {
4957
0
    default:
4958
0
      as_bad (_("invalid instruction `%s' after `%s'"),
4959
0
        insn_name (&i.tm), i.hle_prefix);
4960
0
      return 0;
4961
0
    case PrefixHLELock:
4962
0
      if (i.prefix[LOCK_PREFIX])
4963
0
  return 1;
4964
0
      as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
4965
0
      return 0;
4966
0
    case PrefixHLEAny:
4967
0
      return 1;
4968
0
    case PrefixHLERelease:
4969
0
      if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4970
0
  {
4971
0
    as_bad (_("instruction `%s' after `xacquire' not allowed"),
4972
0
      insn_name (&i.tm));
4973
0
    return 0;
4974
0
  }
4975
0
      if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
4976
0
  {
4977
0
    as_bad (_("memory destination needed for instruction `%s'"
4978
0
        " after `xrelease'"), insn_name (&i.tm));
4979
0
    return 0;
4980
0
  }
4981
0
      return 1;
4982
0
    }
4983
0
}
4984
4985
/* Helper for optimization (running ahead of process_suffix()), to make sure we
4986
   convert only well-formed insns.  @OP is the sized operand to cross check
4987
   against (typically a register).  Checking against a single operand typically
4988
   suffices, as match_template() has already honored CheckOperandSize.  */
4989
4990
static bool is_plausible_suffix (unsigned int op)
4991
0
{
4992
0
  return !i.suffix
4993
0
   || (i.suffix == BYTE_MNEM_SUFFIX && i.types[op].bitfield.byte)
4994
0
   || (i.suffix == WORD_MNEM_SUFFIX && i.types[op].bitfield.word)
4995
0
   || (i.suffix == LONG_MNEM_SUFFIX && i.types[op].bitfield.dword)
4996
0
   || (i.suffix == QWORD_MNEM_SUFFIX && i.types[op].bitfield.qword);
4997
0
}
4998
4999
/* Encode aligned vector move as unaligned vector move.  */
5000
5001
static void
5002
encode_with_unaligned_vector_move (void)
5003
0
{
5004
0
  switch (i.tm.base_opcode)
5005
0
    {
5006
0
    case 0x28:  /* Load instructions.  */
5007
0
    case 0x29:  /* Store instructions.  */
5008
      /* movaps/movapd/vmovaps/vmovapd.  */
5009
0
      if (i.tm.opcode_space == SPACE_0F
5010
0
    && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
5011
0
  i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
5012
0
      break;
5013
0
    case 0x6f:  /* Load instructions.  */
5014
0
    case 0x7f:  /* Store instructions.  */
5015
      /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
5016
0
      if (i.tm.opcode_space == SPACE_0F
5017
0
    && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
5018
0
  i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
5019
0
      break;
5020
0
    default:
5021
0
      break;
5022
0
    }
5023
0
}
5024
5025
/* Try the shortest encoding by shortening operand size.  */
5026
5027
static void
5028
optimize_encoding (void)
5029
0
{
5030
0
  unsigned int j;
5031
5032
0
  if (i.tm.mnem_off == MN_lea)
5033
0
    {
5034
      /* Optimize: -O:
5035
     lea symbol, %rN    -> mov $symbol, %rN
5036
     lea (%rM), %rN     -> mov %rM, %rN
5037
     lea (,%rM,1), %rN  -> mov %rM, %rN
5038
5039
     and in 32-bit mode for 16-bit addressing
5040
5041
     lea (%rM), %rN     -> movzx %rM, %rN
5042
5043
     and in 64-bit mode zap 32-bit addressing in favor of using a
5044
     32-bit (or less) destination.
5045
       */
5046
0
      if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5047
0
  {
5048
0
    if (!i.op[1].regs->reg_type.bitfield.word)
5049
0
      i.tm.opcode_modifier.size = SIZE32;
5050
0
    i.prefix[ADDR_PREFIX] = 0;
5051
0
  }
5052
5053
0
      if (!i.index_reg && !i.base_reg)
5054
0
  {
5055
    /* Handle:
5056
         lea symbol, %rN    -> mov $symbol, %rN
5057
     */
5058
0
    if (flag_code == CODE_64BIT)
5059
0
      {
5060
        /* Don't transform a relocation to a 16-bit one.  */
5061
0
        if (i.op[0].disps
5062
0
      && i.op[0].disps->X_op != O_constant
5063
0
      && i.op[1].regs->reg_type.bitfield.word)
5064
0
    return;
5065
5066
0
        if (!i.op[1].regs->reg_type.bitfield.qword
5067
0
      || i.tm.opcode_modifier.size == SIZE32)
5068
0
    {
5069
0
      i.tm.base_opcode = 0xb8;
5070
0
      i.tm.opcode_modifier.modrm = 0;
5071
0
      if (!i.op[1].regs->reg_type.bitfield.word)
5072
0
        i.types[0].bitfield.imm32 = 1;
5073
0
      else
5074
0
        {
5075
0
          i.tm.opcode_modifier.size = SIZE16;
5076
0
          i.types[0].bitfield.imm16 = 1;
5077
0
        }
5078
0
    }
5079
0
        else
5080
0
    {
5081
      /* Subject to further optimization below.  */
5082
0
      i.tm.base_opcode = 0xc7;
5083
0
      i.tm.extension_opcode = 0;
5084
0
      i.types[0].bitfield.imm32s = 1;
5085
0
      i.types[0].bitfield.baseindex = 0;
5086
0
    }
5087
0
      }
5088
    /* Outside of 64-bit mode address and operand sizes have to match if
5089
       a relocation is involved, as otherwise we wouldn't (currently) or
5090
       even couldn't express the relocation correctly.  */
5091
0
    else if (i.op[0].disps
5092
0
       && i.op[0].disps->X_op != O_constant
5093
0
       && ((!i.prefix[ADDR_PREFIX])
5094
0
           != (flag_code == CODE_32BIT
5095
0
         ? i.op[1].regs->reg_type.bitfield.dword
5096
0
         : i.op[1].regs->reg_type.bitfield.word)))
5097
0
      return;
5098
    /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
5099
       destination is going to grow encoding size.  */
5100
0
    else if (flag_code == CODE_16BIT
5101
0
       && (optimize <= 1 || optimize_for_space)
5102
0
       && !i.prefix[ADDR_PREFIX]
5103
0
       && i.op[1].regs->reg_type.bitfield.dword)
5104
0
      return;
5105
0
    else
5106
0
      {
5107
0
        i.tm.base_opcode = 0xb8;
5108
0
        i.tm.opcode_modifier.modrm = 0;
5109
0
        if (i.op[1].regs->reg_type.bitfield.dword)
5110
0
    i.types[0].bitfield.imm32 = 1;
5111
0
        else
5112
0
    i.types[0].bitfield.imm16 = 1;
5113
5114
0
        if (i.op[0].disps
5115
0
      && i.op[0].disps->X_op == O_constant
5116
0
      && i.op[1].regs->reg_type.bitfield.dword
5117
      /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
5118
         GCC 5. */
5119
0
      && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
5120
0
    i.op[0].disps->X_add_number &= 0xffff;
5121
0
      }
5122
5123
0
    i.tm.operand_types[0] = i.types[0];
5124
0
    i.imm_operands = 1;
5125
0
    if (!i.op[0].imms)
5126
0
      {
5127
0
        i.op[0].imms = &im_expressions[0];
5128
0
        i.op[0].imms->X_op = O_absent;
5129
0
      }
5130
0
  }
5131
0
      else if (i.op[0].disps
5132
0
      && (i.op[0].disps->X_op != O_constant
5133
0
          || i.op[0].disps->X_add_number))
5134
0
  return;
5135
0
      else
5136
0
  {
5137
    /* Handle:
5138
         lea (%rM), %rN     -> mov %rM, %rN
5139
         lea (,%rM,1), %rN  -> mov %rM, %rN
5140
         lea (%rM), %rN     -> movzx %rM, %rN
5141
     */
5142
0
    const reg_entry *addr_reg;
5143
5144
0
    if (!i.index_reg && i.base_reg->reg_num != RegIP)
5145
0
      addr_reg = i.base_reg;
5146
0
    else if (!i.base_reg
5147
0
       && i.index_reg->reg_num != RegIZ
5148
0
       && !i.log2_scale_factor)
5149
0
      addr_reg = i.index_reg;
5150
0
    else
5151
0
      return;
5152
5153
0
    if (addr_reg->reg_type.bitfield.word
5154
0
        && i.op[1].regs->reg_type.bitfield.dword)
5155
0
      {
5156
0
        if (flag_code != CODE_32BIT)
5157
0
    return;
5158
0
        i.tm.opcode_space = SPACE_0F;
5159
0
        i.tm.base_opcode = 0xb7;
5160
0
      }
5161
0
    else
5162
0
      i.tm.base_opcode = 0x8b;
5163
5164
0
    if (addr_reg->reg_type.bitfield.dword
5165
0
        && i.op[1].regs->reg_type.bitfield.qword)
5166
0
      i.tm.opcode_modifier.size = SIZE32;
5167
5168
0
    i.op[0].regs = addr_reg;
5169
0
    i.reg_operands = 2;
5170
0
  }
5171
5172
0
      i.mem_operands = 0;
5173
0
      i.disp_operands = 0;
5174
0
      i.prefix[ADDR_PREFIX] = 0;
5175
0
      i.prefix[SEG_PREFIX] = 0;
5176
0
      i.seg[0] = NULL;
5177
0
    }
5178
5179
0
  if (((i.tm.opcode_space == SPACE_0F
5180
0
        && (i.tm.base_opcode | 1) == 0xbf
5181
0
        && (i.types[0].bitfield.byte
5182
0
      ? i.types[1].bitfield.word
5183
0
      : i.types[1].bitfield.dword))
5184
0
       || (i.tm.opcode_space == SPACE_BASE
5185
0
     && i.tm.base_opcode == 0x63
5186
0
     && i.types[1].bitfield.qword))
5187
0
      && i.reg_operands == 2
5188
0
      && i.op[0].regs->reg_type.bitfield.instance == Accum
5189
0
      && i.op[1].regs->reg_type.bitfield.instance == Accum
5190
0
      && (cpu_arch_tune != PROCESSOR_K6 || optimize_for_space))
5191
0
    {
5192
      /* Optimize: -O:
5193
     movsb     %al, %ax    -> cbw
5194
     movsw     %ax, %eax   -> cwde
5195
     movsl     %eax, %rax  -> cdqe
5196
       */
5197
0
      i.tm.opcode_space = SPACE_BASE;
5198
0
      i.tm.base_opcode = 0x98;
5199
0
      i.tm.opcode_modifier.modrm = 0;
5200
      /* Leave the destination register in place for process_suffix() to take
5201
   care of operand sizing.  This will end up as short_form encoding,
5202
   with the register number being 0 (i.e. not altering the opcode).  */
5203
0
      i.reg_operands = 1;
5204
0
      i.op[0].regs = i.op[1].regs;
5205
0
      i.tm.operand_types[1].bitfield.class = ClassNone;
5206
0
      return;
5207
0
    }
5208
5209
0
  if (optimize_for_space
5210
0
      && i.tm.opcode_space == SPACE_0F
5211
0
      && (i.tm.base_opcode | 1) == 0xb7
5212
0
      && i.reg_operands == 2
5213
0
      && !i.op[0].regs->reg_flags
5214
0
      && !i.op[1].regs->reg_flags
5215
0
      && (i.types[0].bitfield.byte
5216
0
    ? i.types[1].bitfield.word
5217
0
      && i.op[0].regs->reg_num < 4
5218
0
      && i.op[1].regs->reg_num == i.op[0].regs->reg_num
5219
0
      && (!i.suffix || i.suffix == WORD_MNEM_SUFFIX)
5220
0
    : i.types[1].bitfield.dword
5221
0
      && flag_code == CODE_16BIT
5222
0
      && i.op[0].regs->reg_type.bitfield.baseindex
5223
0
      && i.op[0].regs->reg_num != EBP_REG_NUM))
5224
0
    {
5225
      /* Optimize: -Os:
5226
     movzb     %r8, %r16    -> mov $0, %r8h
5227
5228
     %r8 being one of %al, %cl, %dl, or %bl, with %r16 being the
5229
     matching 16-bit reg.
5230
       */
5231
5232
0
      i.tm.opcode_space = SPACE_BASE;
5233
0
      i.tm.opcode_modifier.w = 0;
5234
0
      i.reg_operands = 1;
5235
0
      if (i.types[0].bitfield.byte)
5236
0
  {
5237
0
    i.tm.base_opcode = 0xb0;
5238
0
    i.tm.opcode_modifier.modrm = 0;
5239
0
    copy_operand (1, 0);
5240
0
    i.op[1].regs += 4;
5241
5242
0
    im_expressions[0].X_op = O_constant;
5243
0
    im_expressions[0].X_add_number = 0;
5244
0
    i.op[0].imms = &im_expressions[0];
5245
0
    operand_type_set (&i.types[0], 0);
5246
0
    i.types[0].bitfield.imm8 = 1;
5247
0
    i.tm.operand_types[0] = i.types[0];
5248
0
    i.tm.operand_types[0].bitfield.class = ClassNone;
5249
0
    i.imm_operands = 1;
5250
5251
0
    i.suffix = 0;
5252
0
    return;
5253
0
  }
5254
5255
      /* In 16-bit mode, optimize: -Os:
5256
     movzw     %r16, %r32   -> lea (%r16), %r32
5257
5258
     %r16 being one of %bx, %si, or %di.
5259
       */
5260
0
      i.tm.base_opcode = 0x8d;
5261
5262
0
      i.base_reg = i.op[0].regs;
5263
0
      operand_type_set (&i.types[0], 0);
5264
0
      i.types[0].bitfield.baseindex = 1;
5265
0
      i.tm.operand_types[0] = i.types[0];
5266
0
      i.op[0].disps = NULL;
5267
0
      i.flags[0] = Operand_Mem;
5268
0
      i.mem_operands = 1;
5269
0
      return;
5270
0
    }
5271
5272
0
  if (optimize_for_space
5273
0
      && (i.tm.mnem_off == MN_test
5274
0
          || (i.tm.base_opcode == 0xf6
5275
0
              && i.tm.opcode_space == SPACE_MAP4))
5276
0
      && i.reg_operands == 1
5277
0
      && i.imm_operands == 1
5278
0
      && !i.types[1].bitfield.byte
5279
0
      && is_plausible_suffix (1)
5280
0
      && i.op[0].imms->X_op == O_constant
5281
0
      && fits_in_imm7 (i.op[0].imms->X_add_number))
5282
0
    {
5283
      /* Optimize: -Os:
5284
     test      $imm7, %r64/%r32/%r16  -> test      $imm7, %r8
5285
     ctest<cc> $imm7, %r64/%r32/%r16  -> ctest<cc> $imm7, %r8
5286
       */
5287
0
      unsigned int base_regnum = i.op[1].regs->reg_num;
5288
5289
0
      gas_assert (!i.tm.opcode_modifier.modrm || i.tm.extension_opcode == 0);
5290
5291
0
      if (flag_code == CODE_64BIT || base_regnum < 4)
5292
0
  {
5293
0
    i.types[1].bitfield.byte = 1;
5294
    /* Squash the suffix.  */
5295
0
    i.suffix = 0;
5296
    /* Convert to byte registers. 8-bit registers are special,
5297
       RegRex64 and non-RegRex* each have 8 registers.  */
5298
0
    if (i.types[1].bitfield.word)
5299
      /* 32 (or 40) 8-bit registers.  */
5300
0
      j = 32;
5301
0
    else if (i.types[1].bitfield.dword)
5302
      /* 32 (or 40) 8-bit registers + 32 16-bit registers.  */
5303
0
      j = 64;
5304
0
    else
5305
      /* 32 (or 40) 8-bit registers + 32 16-bit registers
5306
         + 32 32-bit registers.  */
5307
0
      j = 96;
5308
5309
    /* In 64-bit mode, the following byte registers cannot be accessed
5310
       if using the Rex and Rex2 prefix: AH, BH, CH, DH */
5311
0
    if (!(i.op[1].regs->reg_flags & (RegRex | RegRex2)) && base_regnum < 4)
5312
0
      j += 8;
5313
0
    i.op[1].regs -= j;
5314
0
  }
5315
0
    }
5316
0
  else if (flag_code == CODE_64BIT
5317
0
     && i.tm.opcode_space == SPACE_BASE
5318
0
     && i.types[i.operands - 1].bitfield.qword
5319
0
     && ((i.reg_operands == 1
5320
0
    && i.imm_operands == 1
5321
0
    && i.op[0].imms->X_op == O_constant
5322
0
    && ((i.tm.base_opcode == 0xb8
5323
0
         && i.tm.extension_opcode == None
5324
0
         && fits_in_unsigned_long (i.op[0].imms->X_add_number))
5325
0
        || (fits_in_imm31 (i.op[0].imms->X_add_number)
5326
0
      && (i.tm.base_opcode == 0x24
5327
0
          || (((i.tm.base_opcode == 0x80
5328
0
          && i.tm.extension_opcode == 0x4)
5329
0
         || i.tm.mnem_off == MN_test)
5330
0
        && !(i.op[1].regs->reg_flags
5331
0
             & (RegRex | RegRex2)))
5332
0
          || ((i.tm.base_opcode | 1) == 0xc7
5333
0
        && i.tm.extension_opcode == 0x0)))
5334
0
        || (fits_in_imm7 (i.op[0].imms->X_add_number)
5335
0
      && i.tm.base_opcode == 0x83
5336
0
      && i.tm.extension_opcode == 0x4
5337
0
      && !(i.op[1].regs->reg_flags & (RegRex | RegRex2)))))
5338
0
         || ((i.reg_operands == 2
5339
0
        && i.op[0].regs == i.op[1].regs
5340
0
        && (i.tm.mnem_off == MN_xor
5341
0
      || i.tm.mnem_off == MN_sub))
5342
0
       || i.tm.mnem_off == MN_clr)))
5343
0
    {
5344
      /* Optimize: -O:
5345
     andq $imm31, %r64   -> andl $imm31, %r32
5346
     andq $imm7, %r64    -> andl $imm7, %r32
5347
     testq $imm31, %r64  -> testl $imm31, %r32
5348
     xorq %r64, %r64     -> xorl %r32, %r32
5349
     clrq %r64           -> clrl %r32
5350
     subq %r64, %r64     -> subl %r32, %r32
5351
     movq $imm31, %r64   -> movl $imm31, %r32
5352
     movq $imm32, %r64   -> movl $imm32, %r32
5353
        */
5354
0
      i.tm.opcode_modifier.size = SIZE32;
5355
0
      if (i.imm_operands)
5356
0
  {
5357
0
    i.types[0].bitfield.imm32 = 1;
5358
0
    i.types[0].bitfield.imm32s = 0;
5359
0
    i.types[0].bitfield.imm64 = 0;
5360
0
  }
5361
0
      else
5362
0
  {
5363
0
    i.types[0].bitfield.dword = 1;
5364
0
    i.types[0].bitfield.qword = 0;
5365
0
  }
5366
0
      i.types[1].bitfield.dword = 1;
5367
0
      i.types[1].bitfield.qword = 0;
5368
0
      if (i.tm.mnem_off == MN_mov || i.tm.mnem_off == MN_lea)
5369
0
  {
5370
    /* Handle
5371
         movq $imm31, %r64   -> movl $imm31, %r32
5372
         movq $imm32, %r64   -> movl $imm32, %r32
5373
     */
5374
0
    i.tm.operand_types[0].bitfield.imm32 = 1;
5375
0
    i.tm.operand_types[0].bitfield.imm32s = 0;
5376
0
    i.tm.operand_types[0].bitfield.imm64 = 0;
5377
0
    if ((i.tm.base_opcode | 1) == 0xc7)
5378
0
      {
5379
        /* Handle
5380
       movq $imm31, %r64   -> movl $imm31, %r32
5381
         */
5382
0
        i.tm.base_opcode = 0xb8;
5383
0
        i.tm.extension_opcode = None;
5384
0
        i.tm.opcode_modifier.w = 0;
5385
0
        i.tm.opcode_modifier.modrm = 0;
5386
0
      }
5387
0
  }
5388
0
    }
5389
0
  else if (i.reg_operands == 3
5390
0
     && i.op[0].regs == i.op[1].regs
5391
0
     && pp.encoding != encoding_evex
5392
0
     && (i.tm.mnem_off == MN_xor
5393
0
         || i.tm.mnem_off == MN_sub))
5394
0
    {
5395
      /* Optimize: -O:
5396
     xorb %rNb, %rNb, %rMb  -> xorl %rMd, %rMd
5397
     xorw %rNw, %rNw, %rMw  -> xorl %rMd, %rMd
5398
     xorl %rNd, %rNd, %rMd  -> xorl %rMd, %rMd
5399
     xorq %rN,  %rN,  %rM   -> xorl %rMd, %rMd
5400
     subb %rNb, %rNb, %rMb  -> subl %rMd, %rMd
5401
     subw %rNw, %rNw, %rMw  -> subl %rMd, %rMd
5402
     subl %rNd, %rNd, %rMd  -> subl %rMd, %rMd
5403
     subq %rN,  %rN,  %rM   -> subl %rMd, %rMd
5404
        */
5405
0
      i.tm.opcode_space = SPACE_BASE;
5406
0
      i.tm.opcode_modifier.evex = 0;
5407
0
      i.tm.opcode_modifier.size = SIZE32;
5408
0
      i.types[0].bitfield.byte = 0;
5409
0
      i.types[0].bitfield.word = 0;
5410
0
      i.types[0].bitfield.dword = 1;
5411
0
      i.types[0].bitfield.qword = 0;
5412
0
      i.op[0].regs = i.op[2].regs;
5413
0
      i.types[1] = i.types[0];
5414
0
      i.op[1].regs = i.op[2].regs;
5415
0
      i.reg_operands = 2;
5416
0
    }
5417
0
  else if (optimize > 1
5418
0
     && !optimize_for_space
5419
0
     && i.reg_operands == 2
5420
0
     && i.op[0].regs == i.op[1].regs
5421
0
     && (i.tm.mnem_off == MN_and || i.tm.mnem_off == MN_or)
5422
0
     && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
5423
0
    {
5424
      /* Optimize: -O2:
5425
     andb %rN, %rN  -> testb %rN, %rN
5426
     andw %rN, %rN  -> testw %rN, %rN
5427
     andq %rN, %rN  -> testq %rN, %rN
5428
     orb %rN, %rN   -> testb %rN, %rN
5429
     orw %rN, %rN   -> testw %rN, %rN
5430
     orq %rN, %rN   -> testq %rN, %rN
5431
5432
     and outside of 64-bit mode
5433
5434
     andl %rN, %rN  -> testl %rN, %rN
5435
     orl %rN, %rN   -> testl %rN, %rN
5436
       */
5437
0
      i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
5438
0
    }
5439
0
  else if (!optimize_for_space
5440
0
     && i.tm.base_opcode == 0xd0
5441
0
     && i.tm.extension_opcode == 4
5442
0
     && (i.tm.opcode_space == SPACE_BASE
5443
0
         || i.tm.opcode_space == SPACE_MAP4)
5444
0
     && !i.mem_operands)
5445
0
    {
5446
      /* Optimize: -O:
5447
     shlb $1, %rN  -> addb %rN, %rN
5448
     shlw $1, %rN  -> addw %rN, %rN
5449
     shll $1, %rN  -> addl %rN, %rN
5450
     shlq $1, %rN  -> addq %rN, %rN
5451
5452
     shlb $1, %rN, %rM  -> addb %rN, %rN, %rM
5453
     shlw $1, %rN, %rM  -> addw %rN, %rN, %rM
5454
     shll $1, %rN, %rM  -> addl %rN, %rN, %rM
5455
     shlq $1, %rN, %rM  -> addq %rN, %rN, %rM
5456
       */
5457
0
      i.tm.base_opcode = 0x00;
5458
0
      i.tm.extension_opcode = None;
5459
0
      if (i.operands >= 2)
5460
0
  copy_operand (0, 1);
5461
0
      else
5462
0
  {
5463
    /* Legacy form with omitted shift count operand.  */
5464
0
    copy_operand (1, 0);
5465
0
    i.operands = 2;
5466
0
  }
5467
0
      i.reg_operands++;
5468
0
      i.imm_operands = 0;
5469
0
    }
5470
0
  else if (i.tm.base_opcode == 0xba
5471
0
     && i.tm.opcode_space == SPACE_0F
5472
0
     && i.reg_operands == 1
5473
0
     && i.op[0].imms->X_op == O_constant
5474
0
     && i.op[0].imms->X_add_number >= 0)
5475
0
    {
5476
      /* Optimize: -O:
5477
     btw $n, %rN -> btl $n, %rN (outside of 16-bit mode, n < 16)
5478
     btq $n, %rN -> btl $n, %rN (in 64-bit mode, n < 32, N < 8)
5479
     btl $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
5480
5481
     With <BT> one of bts, btr, and bts also:
5482
     <BT>w $n, %rN -> btl $n, %rN (in 32-bit mode, n < 16)
5483
     <BT>l $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
5484
       */
5485
0
      switch (flag_code)
5486
0
  {
5487
0
  case CODE_64BIT:
5488
0
    if (i.tm.extension_opcode != 4)
5489
0
      break;
5490
0
    if (i.types[1].bitfield.qword
5491
0
        && i.op[0].imms->X_add_number < 32
5492
0
        && !(i.op[1].regs->reg_flags & RegRex))
5493
0
      i.tm.opcode_modifier.size = SIZE32;
5494
    /* Fall through.  */
5495
0
  case CODE_32BIT:
5496
0
    if (i.types[1].bitfield.word
5497
0
        && i.op[0].imms->X_add_number < 16)
5498
0
      i.tm.opcode_modifier.size = SIZE32;
5499
0
    break;
5500
0
  case CODE_16BIT:
5501
0
    if (i.op[0].imms->X_add_number < 16)
5502
0
      i.tm.opcode_modifier.size = SIZE16;
5503
0
    break;
5504
0
  }
5505
0
    }
5506
0
  else if (optimize > 1
5507
0
     && (i.tm.base_opcode | 0xf) == 0x4f
5508
0
     && i.tm.opcode_space == SPACE_MAP4
5509
0
     && i.reg_operands == 3
5510
0
     && i.tm.opcode_modifier.operandconstraint == EVEX_NF
5511
0
     && !i.types[0].bitfield.word)
5512
0
    {
5513
      /* Optimize: -O2:
5514
     cfcmov<cc> %rM, %rN, %rN -> cmov<cc> %rM, %rN
5515
     cfcmov<cc> %rM, %rN, %rM -> cmov<!cc> %rN, %rM
5516
     cfcmov<cc> %rN, %rN, %rN -> nop %rN
5517
       */
5518
0
      if (i.op[0].regs == i.op[2].regs)
5519
0
  {
5520
0
    i.tm.base_opcode ^= 1;
5521
0
    i.op[0].regs = i.op[1].regs;
5522
0
    i.op[1].regs = i.op[2].regs;
5523
0
  }
5524
0
      else if (i.op[1].regs != i.op[2].regs)
5525
0
  return;
5526
5527
0
      i.tm.opcode_space = SPACE_0F;
5528
0
      i.tm.opcode_modifier.evex = 0;
5529
0
      i.tm.opcode_modifier.vexvvvv = 0;
5530
0
      i.tm.opcode_modifier.operandconstraint = 0;
5531
0
      i.reg_operands = 2;
5532
5533
      /* While at it, convert to NOP if all three regs match.  */
5534
0
      if (i.op[0].regs == i.op[1].regs)
5535
0
  {
5536
0
    i.tm.base_opcode = 0x1f;
5537
0
    i.tm.extension_opcode = 0;
5538
0
    i.reg_operands = 1;
5539
0
  }
5540
0
    }
5541
0
  else if (i.reg_operands == 3
5542
0
     && i.op[0].regs == i.op[1].regs
5543
0
     && !i.types[2].bitfield.xmmword
5544
0
     && (i.tm.opcode_modifier.vex
5545
0
         || ((!i.mask.reg || i.mask.zeroing)
5546
0
       && i.tm.opcode_modifier.evex
5547
0
       && (pp.encoding != encoding_evex
5548
0
           || cpu_arch_isa_flags.bitfield.cpuavx512vl
5549
0
           || is_cpu (&i.tm, CpuAVX512VL)
5550
0
           || (i.tm.operand_types[2].bitfield.zmmword
5551
0
         && i.types[2].bitfield.ymmword))))
5552
0
     && i.tm.opcode_space == SPACE_0F
5553
0
     && ((i.tm.base_opcode | 2) == 0x57
5554
0
         || i.tm.base_opcode == 0xdf
5555
0
         || i.tm.base_opcode == 0xef
5556
0
         || (i.tm.base_opcode | 3) == 0xfb
5557
0
         || i.tm.base_opcode == 0x42
5558
0
         || i.tm.base_opcode == 0x47))
5559
0
    {
5560
      /* Optimize: -O1:
5561
     VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
5562
     vpsubq and vpsubw:
5563
       EVEX VOP %zmmM, %zmmM, %zmmN
5564
         -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
5565
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
5566
       EVEX VOP %ymmM, %ymmM, %ymmN
5567
         -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
5568
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
5569
       VEX VOP %ymmM, %ymmM, %ymmN
5570
         -> VEX VOP %xmmM, %xmmM, %xmmN
5571
     VOP, one of vpandn and vpxor:
5572
       VEX VOP %ymmM, %ymmM, %ymmN
5573
         -> VEX VOP %xmmM, %xmmM, %xmmN
5574
     VOP, one of vpandnd and vpandnq:
5575
       EVEX VOP %zmmM, %zmmM, %zmmN
5576
         -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
5577
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
5578
       EVEX VOP %ymmM, %ymmM, %ymmN
5579
         -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
5580
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
5581
     VOP, one of vpxord and vpxorq:
5582
       EVEX VOP %zmmM, %zmmM, %zmmN
5583
         -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
5584
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
5585
       EVEX VOP %ymmM, %ymmM, %ymmN
5586
         -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
5587
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
5588
     VOP, one of kxord and kxorq:
5589
       VEX VOP %kM, %kM, %kN
5590
         -> VEX kxorw %kM, %kM, %kN
5591
     VOP, one of kandnd and kandnq:
5592
       VEX VOP %kM, %kM, %kN
5593
         -> VEX kandnw %kM, %kM, %kN
5594
       */
5595
0
      if (i.tm.opcode_modifier.evex)
5596
0
  {
5597
0
    if (pp.encoding != encoding_evex)
5598
0
      {
5599
0
        i.tm.opcode_modifier.vex = VEX128;
5600
0
        i.tm.opcode_modifier.vexw = VEXW0;
5601
0
        i.tm.opcode_modifier.evex = 0;
5602
0
        pp.encoding = encoding_vex;
5603
0
        i.mask.reg = NULL;
5604
0
      }
5605
0
    else if (optimize > 1)
5606
0
      i.tm.opcode_modifier.evex = EVEX128;
5607
0
    else
5608
0
      return;
5609
0
  }
5610
0
      else if (i.tm.operand_types[0].bitfield.class == RegMask)
5611
0
  {
5612
0
    i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
5613
0
    i.tm.opcode_modifier.vexw = VEXW0;
5614
0
  }
5615
0
      else
5616
0
  i.tm.opcode_modifier.vex = VEX128;
5617
5618
0
      if (i.tm.opcode_modifier.vex)
5619
0
  for (j = 0; j < 3; j++)
5620
0
    {
5621
0
      i.types[j].bitfield.xmmword = 1;
5622
0
      i.types[j].bitfield.ymmword = 0;
5623
0
    }
5624
0
    }
5625
0
  else if (pp.encoding != encoding_evex
5626
0
     && pp.encoding != encoding_egpr
5627
0
     && !i.types[0].bitfield.zmmword
5628
0
     && !i.types[1].bitfield.zmmword
5629
0
     && !i.mask.reg
5630
0
     && !i.broadcast.type
5631
0
     && !i.broadcast.bytes
5632
0
     && i.tm.opcode_modifier.evex
5633
0
     && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
5634
0
         || (i.tm.base_opcode & ~4) == 0xdb
5635
0
         || (i.tm.base_opcode & ~4) == 0xeb)
5636
0
     && i.tm.extension_opcode == None)
5637
0
    {
5638
      /* Optimize: -O1:
5639
     VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
5640
     vmovdqu32 and vmovdqu64:
5641
       EVEX VOP %xmmM, %xmmN
5642
         -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
5643
       EVEX VOP %ymmM, %ymmN
5644
         -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
5645
       EVEX VOP %xmmM, mem
5646
         -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
5647
       EVEX VOP %ymmM, mem
5648
         -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
5649
       EVEX VOP mem, %xmmN
5650
         -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
5651
       EVEX VOP mem, %ymmN
5652
         -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
5653
     VOP, one of vpand, vpandn, vpor, vpxor:
5654
       EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
5655
         -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
5656
       EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
5657
         -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
5658
       EVEX VOP{d,q} mem, %xmmM, %xmmN
5659
         -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
5660
       EVEX VOP{d,q} mem, %ymmM, %ymmN
5661
         -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
5662
       */
5663
0
      for (j = 0; j < i.operands; j++)
5664
0
  if (operand_type_check (i.types[j], disp)
5665
0
      && i.op[j].disps->X_op == O_constant)
5666
0
    {
5667
      /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
5668
         has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
5669
         bytes, we choose EVEX Disp8 over VEX Disp32.  */
5670
0
      int evex_disp8, vex_disp8;
5671
0
      unsigned int memshift = i.memshift;
5672
0
      offsetT n = i.op[j].disps->X_add_number;
5673
5674
0
      evex_disp8 = fits_in_disp8 (n);
5675
0
      i.memshift = 0;
5676
0
      vex_disp8 = fits_in_disp8 (n);
5677
0
      if (evex_disp8 != vex_disp8)
5678
0
        {
5679
0
    i.memshift = memshift;
5680
0
    return;
5681
0
        }
5682
5683
0
      i.types[j].bitfield.disp8 = vex_disp8;
5684
0
      break;
5685
0
    }
5686
0
      if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
5687
0
    && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
5688
0
  i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
5689
0
      i.tm.opcode_modifier.vex
5690
0
  = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
5691
0
      i.tm.opcode_modifier.vexw = VEXW0;
5692
      /* VPAND, VPOR, and VPXOR are commutative.  */
5693
0
      if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
5694
0
  i.tm.opcode_modifier.commutative = 1;
5695
0
      i.tm.opcode_modifier.evex = 0;
5696
0
      i.tm.opcode_modifier.masking = 0;
5697
0
      i.tm.opcode_modifier.broadcast = 0;
5698
0
      i.tm.opcode_modifier.disp8memshift = 0;
5699
0
      i.memshift = 0;
5700
0
      if (j < i.operands)
5701
0
  i.types[j].bitfield.disp8
5702
0
    = fits_in_disp8 (i.op[j].disps->X_add_number);
5703
0
    }
5704
0
  else if (optimize_for_space
5705
0
     && i.tm.base_opcode == 0x29
5706
0
     && i.tm.opcode_space == SPACE_0F38
5707
0
     && i.operands == i.reg_operands
5708
0
     && i.op[0].regs == i.op[1].regs
5709
0
     && (!i.tm.opcode_modifier.vex
5710
0
         || !(i.op[0].regs->reg_flags & RegRex))
5711
0
     && !i.tm.opcode_modifier.evex)
5712
0
    {
5713
      /* Optimize: -Os:
5714
         pcmpeqq %xmmN, %xmmN          -> pcmpeqd %xmmN, %xmmN
5715
         vpcmpeqq %xmmN, %xmmN, %xmmM  -> vpcmpeqd %xmmN, %xmmN, %xmmM (N < 8)
5716
         vpcmpeqq %ymmN, %ymmN, %ymmM  -> vpcmpeqd %ymmN, %ymmN, %ymmM (N < 8)
5717
       */
5718
0
      i.tm.opcode_space = SPACE_0F;
5719
0
      i.tm.base_opcode = 0x76;
5720
0
    }
5721
0
  else if (((i.tm.base_opcode >= 0x64
5722
0
       && i.tm.base_opcode <= 0x66
5723
0
       && i.tm.opcode_space == SPACE_0F)
5724
0
      || (i.tm.base_opcode == 0x37
5725
0
    && i.tm.opcode_space == SPACE_0F38))
5726
0
     && i.operands == i.reg_operands
5727
0
     && i.op[0].regs == i.op[1].regs
5728
0
     && !i.tm.opcode_modifier.evex)
5729
0
    {
5730
      /* Optimize: -O:
5731
         pcmpgt[bwd] %mmN, %mmN             -> pxor %mmN, %mmN
5732
         pcmpgt[bwdq] %xmmN, %xmmN          -> pxor %xmmN, %xmmN
5733
         vpcmpgt[bwdq] %xmmN, %xmmN, %xmmM  -> vpxor %xmmN, %xmmN, %xmmM (N < 8)
5734
         vpcmpgt[bwdq] %xmmN, %xmmN, %xmmM  -> vpxor %xmm0, %xmm0, %xmmM (N > 7)
5735
         vpcmpgt[bwdq] %ymmN, %ymmN, %ymmM  -> vpxor %ymmN, %ymmN, %ymmM (N < 8)
5736
         vpcmpgt[bwdq] %ymmN, %ymmN, %ymmM  -> vpxor %ymm0, %ymm0, %ymmM (N > 7)
5737
       */
5738
0
      i.tm.opcode_space = SPACE_0F;
5739
0
      i.tm.base_opcode = 0xef;
5740
0
      if (i.tm.opcode_modifier.vex && (i.op[0].regs->reg_flags & RegRex))
5741
0
  {
5742
0
    if (i.operands == 2)
5743
0
      {
5744
0
        gas_assert (i.tm.opcode_modifier.sse2avx);
5745
5746
0
        i.operands = 3;
5747
0
        i.reg_operands = 3;
5748
0
        i.tm.operands = 3;
5749
5750
0
        copy_operand (2, 0);
5751
5752
0
        i.tm.opcode_modifier.sse2avx = 0;
5753
0
      }
5754
0
    i.op[0].regs -= i.op[0].regs->reg_num + 8;
5755
0
    i.op[1].regs = i.op[0].regs;
5756
0
  }
5757
0
    }
5758
0
  else if (i.tm.extension_opcode == 6
5759
0
     && i.tm.base_opcode >= 0x71
5760
0
     && i.tm.base_opcode <= 0x73
5761
0
     && i.tm.opcode_space == SPACE_0F
5762
0
     && i.op[0].imms->X_op == O_constant
5763
0
     && i.op[0].imms->X_add_number == 1
5764
0
     && !i.mem_operands)
5765
0
    {
5766
      /* Optimize: -O:
5767
     psllw $1, %mmxN          -> paddw %mmxN, %mmxN
5768
     psllw $1, %xmmN          -> paddw %xmmN, %xmmN
5769
     vpsllw $1, %xmmN, %xmmM  -> vpaddw %xmmN, %xmmN, %xmmM
5770
     vpsllw $1, %ymmN, %ymmM  -> vpaddw %ymmN, %ymmN, %ymmM
5771
     vpsllw $1, %zmmN, %zmmM  -> vpaddw %zmmN, %zmmN, %zmmM
5772
5773
     pslld $1, %mmxN          -> paddd %mmxN, %mmxN
5774
     pslld $1, %xmmN          -> paddd %xmmN, %xmmN
5775
     vpslld $1, %xmmN, %xmmM  -> vpaddd %xmmN, %xmmN, %xmmM
5776
     vpslld $1, %ymmN, %ymmM  -> vpaddd %ymmN, %ymmN, %ymmM
5777
     vpslld $1, %zmmN, %zmmM  -> vpaddd %zmmN, %zmmN, %zmmM
5778
5779
     psllq $1, %xmmN          -> paddq %xmmN, %xmmN
5780
     vpsllq $1, %xmmN, %xmmM  -> vpaddq %xmmN, %xmmN, %xmmM
5781
     vpsllq $1, %ymmN, %ymmM  -> vpaddq %ymmN, %ymmN, %ymmM
5782
     vpsllq $1, %zmmN, %zmmM  -> vpaddq %zmmN, %zmmN, %zmmM
5783
    */
5784
0
      if (i.tm.base_opcode != 0x73)
5785
0
  i.tm.base_opcode |= 0xfc; /* {,v}padd{w,d} */
5786
0
      else
5787
0
  {
5788
0
    gas_assert (i.tm.operand_types[1].bitfield.class != RegMMX);
5789
0
    i.tm.base_opcode = 0xd4; /* {,v}paddq */
5790
0
  }
5791
0
      i.tm.extension_opcode = None;
5792
0
      if (i.tm.opcode_modifier.vexvvvv)
5793
0
  i.tm.opcode_modifier.vexvvvv = VexVVVV_SRC1;
5794
0
      copy_operand (0, 1);
5795
0
      i.reg_operands++;
5796
0
      i.imm_operands = 0;
5797
0
    }
5798
0
  else if (optimize_for_space
5799
0
     && i.tm.base_opcode == 0x59
5800
0
     && i.tm.opcode_space == SPACE_0F38
5801
0
     && i.operands == i.reg_operands
5802
0
     && i.tm.opcode_modifier.vex
5803
0
     && !(i.op[0].regs->reg_flags & RegRex)
5804
0
     && i.op[0].regs->reg_type.bitfield.xmmword
5805
0
     && pp.encoding != encoding_vex3)
5806
0
    {
5807
      /* Optimize: -Os:
5808
         vpbroadcastq %xmmN, %xmmM  -> vpunpcklqdq %xmmN, %xmmN, %xmmM (N < 8)
5809
       */
5810
0
      i.tm.opcode_space = SPACE_0F;
5811
0
      i.tm.base_opcode = 0x6c;
5812
0
      i.tm.opcode_modifier.vexvvvv = VexVVVV_SRC1;
5813
5814
0
      ++i.operands;
5815
0
      ++i.reg_operands;
5816
0
      ++i.tm.operands;
5817
5818
0
      copy_operand (2, 0);
5819
0
      swap_2_operands (1, 2);
5820
0
    }
5821
0
  else if (i.tm.base_opcode == 0x16
5822
0
     && i.tm.opcode_space == SPACE_0F3A
5823
0
     && i.op[0].imms->X_op == O_constant
5824
0
     && i.op[0].imms->X_add_number == 0)
5825
0
    {
5826
      /* Optimize: -O:
5827
         pextrd $0, %xmmN, ...   -> movd %xmmN, ...
5828
         pextrq $0, %xmmN, ...   -> movq %xmmN, ...
5829
         vpextrd $0, %xmmN, ...  -> vmovd %xmmN, ...
5830
         vpextrq $0, %xmmN, ...  -> vmovq %xmmN, ...
5831
       */
5832
0
      i.tm.opcode_space = SPACE_0F;
5833
0
      if (!i.mem_operands
5834
0
    || i.tm.opcode_modifier.evex
5835
0
    || (i.tm.opcode_modifier.vexw != VEXW1
5836
0
        && i.tm.opcode_modifier.size != SIZE64))
5837
0
  i.tm.base_opcode = 0x7e;
5838
0
      else
5839
0
  {
5840
0
    i.tm.base_opcode = 0xd6;
5841
0
    i.tm.opcode_modifier.size = 0;
5842
0
    i.tm.opcode_modifier.vexw
5843
0
      = i.tm.opcode_modifier.sse2avx ? VEXW0 : VEXWIG;
5844
0
  }
5845
5846
0
      copy_operand (0, 1);
5847
0
      copy_operand (1, 2);
5848
5849
0
      i.operands = 2;
5850
0
      i.imm_operands = 0;
5851
0
    }
5852
0
  else if (i.tm.base_opcode == 0x17
5853
0
     && i.tm.opcode_space == SPACE_0F3A
5854
0
     && i.op[0].imms->X_op == O_constant
5855
0
     && i.op[0].imms->X_add_number == 0)
5856
0
    {
5857
      /* Optimize: -O:
5858
         extractps $0, %xmmN, %rM   -> movd %xmmN, %rM
5859
         extractps $0, %xmmN, mem   -> movss %xmmN, mem
5860
         vextractps $0, %xmmN, %rM  -> vmovd %xmmN, %rM
5861
         vextractps $0, %xmmN, mem  -> vmovss %xmmN, mem
5862
       */
5863
0
      i.tm.opcode_space = SPACE_0F;
5864
0
      i.tm.opcode_modifier.vexw = VEXW0;
5865
5866
0
      if (!i.mem_operands)
5867
0
  i.tm.base_opcode = 0x7e;
5868
0
      else
5869
0
  {
5870
0
    i.tm.base_opcode = 0x11;
5871
0
    i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
5872
0
  }
5873
5874
0
      copy_operand (0, 1);
5875
0
      copy_operand (1, 2);
5876
5877
0
      i.operands = 2;
5878
0
      i.imm_operands = 0;
5879
0
    }
5880
0
  else if ((i.tm.base_opcode | 0x22) == 0x3b
5881
0
     && i.tm.opcode_space == SPACE_0F3A
5882
0
     && i.op[0].imms->X_op == O_constant
5883
0
     && i.op[0].imms->X_add_number == 0)
5884
0
    {
5885
      /* Optimize: -O:
5886
         vextractf128 $0, %ymmN, %xmmM      -> vmovaps %xmmN, %xmmM
5887
         vextractf128 $0, %ymmN, mem        -> vmovups %xmmN, mem
5888
         vextractf32x4 $0, %[yz]mmN, %xmmM  -> vmovaps %xmmN, %xmmM
5889
         vextractf32x4 $0, %[yz]mmN, mem    -> vmovups %xmmN, mem
5890
         vextractf64x2 $0, %[yz]mmN, %xmmM  -> vmovapd %xmmN, %xmmM
5891
         vextractf64x2 $0, %[yz]mmN, mem    -> vmovupd %xmmN, mem
5892
         vextractf32x8 $0, %zmmN, %ymmM     -> vmovaps %ymmN, %ymmM
5893
         vextractf32x8 $0, %zmmN, mem       -> vmovups %ymmN, mem
5894
         vextractf64x4 $0, %zmmN, %ymmM     -> vmovapd %ymmN, %ymmM
5895
         vextractf64x4 $0, %zmmN, mem       -> vmovupd %ymmN, mem
5896
         vextracti128 $0, %ymmN, %xmmM      -> vmovdqa %xmmN, %xmmM
5897
         vextracti128 $0, %ymmN, mem        -> vmovdqu %xmmN, mem
5898
         vextracti32x4 $0, %[yz]mmN, %xmmM  -> vmovdqa{,32} %xmmN, %xmmM
5899
         vextracti32x4 $0, %[yz]mmN, mem    -> vmovdqu{,32} %xmmN, mem
5900
         vextracti64x2 $0, %[yz]mmN, %xmmM  -> vmovdqa{,64} %xmmN, %xmmM
5901
         vextracti64x2 $0, %[yz]mmN, mem    -> vmovdqu{,64} %xmmN, mem
5902
         vextracti32x8 $0, %zmmN, %ymmM     -> vmovdqa{,32} %ymmN, %ymmM
5903
         vextracti32x8 $0, %zmmN, mem       -> vmovdqu{,32} %ymmN, mem
5904
         vextracti64x4 $0, %zmmN, %ymmM     -> vmovdqa{,64} %ymmN, %ymmM
5905
         vextracti64x4 $0, %zmmN, mem       -> vmovdqu{,64} %ymmN, mem
5906
       */
5907
0
      i.tm.opcode_space = SPACE_0F;
5908
5909
0
      if (!i.mask.reg
5910
0
    && (pp.encoding <= encoding_vex3
5911
0
        || (pp.encoding == encoding_evex512
5912
0
      && (!i.base_reg || !(i.base_reg->reg_flags & RegRex2))
5913
0
      && (!i.index_reg || !(i.index_reg->reg_flags & RegRex2)))))
5914
0
  {
5915
0
    i.tm.opcode_modifier.vex = i.tm.base_opcode & 2 ? VEX256 : VEX128;
5916
0
    i.tm.opcode_modifier.evex = 0;
5917
0
  }
5918
0
      else
5919
0
  i.tm.opcode_modifier.evex = i.tm.base_opcode & 2 ? EVEX256 : EVEX128;
5920
5921
0
      if (i.tm.base_opcode & 0x20)
5922
0
  {
5923
0
    i.tm.base_opcode = 0x7f;
5924
0
    if (i.reg_operands != 2)
5925
0
      i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
5926
0
  }
5927
0
      else
5928
0
  {
5929
0
    if (i.reg_operands == 2)
5930
0
      i.tm.base_opcode = 0x29;
5931
0
    else
5932
0
      i.tm.base_opcode = 0x11;
5933
0
    if (i.tm.opcode_modifier.vexw != VEXW1)
5934
0
      i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
5935
0
  }
5936
5937
0
      if (i.tm.opcode_modifier.vex)
5938
0
  i.tm.opcode_modifier.vexw = VEXWIG;
5939
5940
0
      copy_operand (0, 1);
5941
0
      copy_operand (1, 2);
5942
5943
0
      i.operands = 2;
5944
0
      i.imm_operands = 0;
5945
0
    }
5946
0
  else if (i.tm.base_opcode == 0x21
5947
0
     && i.tm.opcode_space == SPACE_0F3A
5948
0
     && i.op[0].imms->X_op == O_constant
5949
0
     && (i.operands == i.reg_operands + 1
5950
0
         ? i.op[0].imms->X_add_number == 0
5951
0
     || (i.op[0].imms->X_add_number & 0xf) == 0xf
5952
0
         : (i.op[0].imms->X_add_number & 0x3f) == 0x0e
5953
0
      && (i.reg_operands == 1 || i.op[2].regs == i.op[3].regs)))
5954
0
    {
5955
      /* Optimize: -O:
5956
         insertps $0b....1111, %xmmN, %xmmM          -> xorps %xmmM, %xmmM
5957
         insertps $0b00000000, %xmmN, %xmmM          -> movss %xmmN, %xmmM
5958
         insertps $0b..001110, mem, %xmmN            -> movss mem, %xmmN
5959
         vinsertps $0b....1111, %xmmN, %xmmM, %xmmK  -> vxorps %xmm?, %xmm?, %xmmK
5960
         vinsertps $0b00000000, %xmmN, %xmmM, %xmmK  -> vmovss %xmmN, %xmmM, %xmmK
5961
         vinsertps $0b..001110, mem, %xmmN, %xmmN    -> vmovss mem, %xmmN
5962
       */
5963
0
      i.tm.opcode_space = SPACE_0F;
5964
0
      if ((i.op[0].imms->X_add_number & 0xf) == 0xf)
5965
0
  {
5966
0
    i.tm.base_opcode = 0x57;
5967
0
    i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
5968
5969
0
    --i.operands;
5970
5971
0
    copy_operand (i.operands - 1, i.operands);
5972
0
    copy_operand (1, i.operands - 1);
5973
0
    copy_operand (0, 1);
5974
5975
    /* Switch from EVEX to VEX encoding if possible.  Sadly we can't
5976
       (always) tell use of the {evex} pseudo-prefix (which otherwise
5977
       we'd like to respect) from use of %xmm16-%xmm31.  */
5978
0
    if (pp.encoding == encoding_evex)
5979
0
      pp.encoding = encoding_default;
5980
0
    if (i.tm.opcode_modifier.evex
5981
0
        && pp.encoding <= encoding_vex3
5982
0
        && !(i.op[0].regs->reg_flags & RegVRex))
5983
0
      {
5984
0
        i.tm.opcode_modifier.evex = 0;
5985
0
        i.tm.opcode_modifier.vex = VEX128;
5986
0
      }
5987
5988
    /* Switch from VEX3 to VEX2 encoding if possible.  */
5989
0
    if (i.tm.opcode_modifier.vex
5990
0
        && pp.encoding <= encoding_vex
5991
0
        && (i.op[0].regs->reg_flags & RegRex))
5992
0
      {
5993
0
        i.op[0].regs -= 8;
5994
0
        i.op[1].regs = i.op[0].regs;
5995
0
      }
5996
0
  }
5997
0
      else
5998
0
  {
5999
0
    i.tm.base_opcode = 0x10;
6000
0
    i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
6001
6002
0
    if (i.op[0].imms->X_add_number == 0)
6003
0
      --i.operands;
6004
0
    else
6005
0
      {
6006
0
        i.operands = 2;
6007
0
        i.tm.opcode_modifier.vexvvvv = 0;
6008
0
      }
6009
0
    copy_operand (0, 1);
6010
0
    copy_operand (1, 2);
6011
0
    copy_operand (2, 3);
6012
0
  }
6013
6014
0
      i.imm_operands = 0;
6015
0
    }
6016
0
}
6017
6018
/* Check whether the promoted (to address size) register is usable as index
6019
   register in ModR/M SIB addressing.  */
6020
6021
static bool is_index (const reg_entry *r)
6022
0
{
6023
0
  gas_assert (flag_code == CODE_64BIT);
6024
6025
0
  if (r->reg_type.bitfield.byte)
6026
0
    {
6027
0
      if (!(r->reg_flags & (RegRex | RegRex2 | RegRex64)))
6028
0
  {
6029
0
    if (r->reg_num >= 4)
6030
0
      return false;
6031
0
    r += 8;
6032
0
  }
6033
0
      r += 32;
6034
0
    }
6035
0
  if (r->reg_type.bitfield.word)
6036
0
    r += 32;
6037
  /* No need to further check .dword here.  */
6038
6039
0
  return r->reg_type.bitfield.baseindex;
6040
0
}
6041
6042
/* Try to shorten {nf} encodings, by shortening operand size or switching to
6043
   functionally identical encodings.  */
6044
6045
static void
6046
optimize_nf_encoding (void)
6047
0
{
6048
0
  if (i.tm.base_opcode == 0x80
6049
0
      && (i.tm.extension_opcode == 0 || i.tm.extension_opcode == 5)
6050
0
      && i.suffix != BYTE_MNEM_SUFFIX
6051
0
      && !i.types[1].bitfield.byte
6052
0
      && !i.types[2].bitfield.byte
6053
0
      && i.op[0].imms->X_op == O_constant
6054
0
      && i.op[0].imms->X_add_number == 0x80)
6055
0
    {
6056
      /* Optimize: -O:
6057
     {nf} addw $0x80, ...  -> {nf} subw $-0x80, ...
6058
     {nf} addl $0x80, ...  -> {nf} subl $-0x80, ...
6059
     {nf} addq $0x80, ...  -> {nf} subq $-0x80, ...
6060
6061
     {nf} subw $0x80, ...  -> {nf} addw $-0x80, ...
6062
     {nf} subl $0x80, ...  -> {nf} addl $-0x80, ...
6063
     {nf} subq $0x80, ...  -> {nf} addq $-0x80, ...
6064
       */
6065
0
      i.tm.base_opcode |= 3;
6066
0
      i.tm.extension_opcode ^= 5;
6067
0
      i.tm.opcode_modifier.w = 0;
6068
0
      i.op[0].imms->X_add_number = -i.op[0].imms->X_add_number;
6069
6070
0
      i.tm.operand_types[0].bitfield.imm8 = 0;
6071
0
      i.tm.operand_types[0].bitfield.imm8s = 1;
6072
0
      i.tm.operand_types[0].bitfield.imm16 = 0;
6073
0
      i.tm.operand_types[0].bitfield.imm32 = 0;
6074
0
      i.tm.operand_types[0].bitfield.imm32s = 0;
6075
6076
0
      i.types[0] = i.tm.operand_types[0];
6077
0
    }
6078
0
  else if ((i.tm.base_opcode | 3) == 0x83
6079
0
      && (i.tm.extension_opcode == 0 || i.tm.extension_opcode == 5)
6080
0
      && i.op[0].imms->X_op == O_constant
6081
0
      && (i.op[0].imms->X_add_number == 1
6082
0
    || i.op[0].imms->X_add_number == -1
6083
    /* While for wider than byte operations immediates were suitably
6084
       adjusted earlier on, 0xff in the byte case needs covering
6085
       explicitly.  */
6086
0
    || (i.op[0].imms->X_add_number == 0xff
6087
0
        && (i.suffix == BYTE_MNEM_SUFFIX
6088
0
      || i.types[i.operands - 1].bitfield.byte))))
6089
0
    {
6090
      /* Optimize: -O:
6091
     {nf} add $1, ...        -> {nf} inc ...
6092
     {nf} add $-1, ...       -> {nf} dec ...
6093
     {nf} add $0xf...f, ...  -> {nf} dec ...
6094
6095
     {nf} sub $1, ...        -> {nf} dec ...
6096
     {nf} sub $-1, ...       -> {nf} inc ...
6097
     {nf} sub $0xf...f, ...  -> {nf} inc ...
6098
       */
6099
0
      i.tm.base_opcode = 0xfe;
6100
0
      i.tm.extension_opcode
6101
0
  = (i.op[0].imms->X_add_number == 1) != (i.tm.extension_opcode == 0);
6102
0
      i.tm.opcode_modifier.w = 1;
6103
6104
0
      copy_operand (0, 1);
6105
0
      copy_operand (1, 2);
6106
6107
0
      i.imm_operands = 0;
6108
0
      --i.operands;
6109
0
    }
6110
0
  else if (i.tm.base_opcode == 0xc0
6111
0
     && i.op[0].imms->X_op == O_constant
6112
0
     && i.op[0].imms->X_add_number
6113
0
        == (i.types[i.operands - 1].bitfield.byte
6114
0
      || i.suffix == BYTE_MNEM_SUFFIX
6115
0
      ? 7 : i.types[i.operands - 1].bitfield.word
6116
0
      || i.suffix == WORD_MNEM_SUFFIX
6117
0
      ? 15 : 63 >> (i.types[i.operands - 1].bitfield.dword
6118
0
              || i.suffix == LONG_MNEM_SUFFIX)))
6119
0
    {
6120
      /* Optimize: -O:
6121
     {nf} rol $osz-1, ...   -> {nf} ror $1, ...
6122
     {nf} ror $osz-1, ...   -> {nf} rol $1, ...
6123
       */
6124
0
      gas_assert (i.tm.extension_opcode <= 1);
6125
0
      i.tm.extension_opcode ^= 1;
6126
0
      i.tm.base_opcode = 0xd0;
6127
0
      i.tm.operand_types[0].bitfield.imm1 = 1;
6128
0
      i.imm_operands = 0;
6129
0
    }
6130
0
  else if ((i.tm.base_opcode | 2) == 0x6b
6131
0
     && i.op[0].imms->X_op == O_constant
6132
0
     && (i.op[0].imms->X_add_number > 0
6133
0
         ? !(i.op[0].imms->X_add_number & (i.op[0].imms->X_add_number - 1))
6134
         /* optimize_imm() converts to sign-extended representation where
6135
      possible (and input can also come with these specific numbers).  */
6136
0
         : (i.types[i.operands - 1].bitfield.word
6137
0
      && i.op[0].imms->X_add_number == -0x8000)
6138
0
     || (i.types[i.operands - 1].bitfield.dword
6139
0
         && i.op[0].imms->X_add_number + 1 == -0x7fffffff))
6140
     /* 16-bit 3-operand non-ZU forms need leaviong alone, to prevent
6141
        zero-extension of the result.  Unless, of course, both non-
6142
        immediate operands match (which can be converted to the non-NDD
6143
        form).  */
6144
0
     && (i.operands < 3
6145
0
         || !i.types[2].bitfield.word
6146
0
         || i.tm.mnem_off == MN_imulzu
6147
0
         || i.op[2].regs == i.op[1].regs)
6148
     /* When merely optimizing for size, exclude cases where we'd convert
6149
        from Imm8S to Imm8 encoding, thus not actually reducing size.  */
6150
0
     && (!optimize_for_space
6151
0
         || i.tm.base_opcode == 0x69
6152
0
         || !(i.op[0].imms->X_add_number & 0x7d)))
6153
0
    {
6154
      /* Optimize: -O:
6155
     {nf} imul   $1<<N, ...   -> {nf} shl $N, ...
6156
     {nf} imulzu $1<<N, ...   -> {nf} shl $N, ...
6157
       */
6158
0
      if (i.op[0].imms->X_add_number != 2)
6159
0
  {
6160
0
    i.tm.base_opcode = 0xc0;
6161
0
    i.op[0].imms->X_add_number = ffs (i.op[0].imms->X_add_number) - 1;
6162
0
    i.tm.operand_types[0].bitfield.imm8 = 1;
6163
0
    i.tm.operand_types[0].bitfield.imm16 = 0;
6164
0
    i.tm.operand_types[0].bitfield.imm32 = 0;
6165
0
    i.tm.operand_types[0].bitfield.imm32s = 0;
6166
0
  }
6167
0
      else
6168
0
  {
6169
0
    i.tm.base_opcode = 0xd0;
6170
0
    i.tm.operand_types[0].bitfield.imm1 = 1;
6171
0
  }
6172
0
      i.types[0] = i.tm.operand_types[0];
6173
0
      i.tm.extension_opcode = 4;
6174
0
      i.tm.opcode_modifier.w = 1;
6175
0
      i.tm.opcode_modifier.operandconstraint = 0;
6176
0
      if (i.operands == 3)
6177
0
  {
6178
0
    if (i.op[2].regs == i.op[1].regs && i.tm.mnem_off != MN_imulzu)
6179
0
      {
6180
        /* Convert to non-NDD form.  This is required for 16-bit insns
6181
           (to prevent zero-extension) and benign for others.  */
6182
0
        i.operands = 2;
6183
0
        i.reg_operands = 1;
6184
0
      }
6185
0
    else
6186
0
      i.tm.opcode_modifier.vexvvvv = VexVVVV_DST;
6187
0
  }
6188
0
      else if (i.tm.mnem_off == MN_imulzu)
6189
0
  {
6190
    /* Convert to NDD form, to effect zero-extension of the result.  */
6191
0
    i.tm.opcode_modifier.vexvvvv = VexVVVV_DST;
6192
0
    i.operands = 3;
6193
0
    i.reg_operands = 2;
6194
0
    copy_operand (2, 1);
6195
0
  }
6196
0
    }
6197
6198
0
  if (optimize_for_space
6199
0
      && pp.encoding != encoding_evex
6200
0
      && (i.tm.base_opcode == 0x00
6201
0
    || (i.tm.base_opcode == 0xd0 && i.tm.extension_opcode == 4))
6202
0
      && !i.mem_operands
6203
0
      && !i.types[1].bitfield.byte
6204
      /* 16-bit operand size has extra restrictions: If REX2 was needed,
6205
   no size reduction would be possible.  Plus 3-operand forms zero-
6206
   extend the result, which can't be expressed with LEA.  */
6207
0
      && (!i.types[1].bitfield.word
6208
0
    || (i.operands == 2 && pp.encoding != encoding_egpr))
6209
0
      && is_plausible_suffix (1)
6210
      /* %rsp can't be the index.  */
6211
0
      && (is_index (i.op[1].regs)
6212
0
    || (i.imm_operands == 0 && is_index (i.op[0].regs)))
6213
      /* While %rbp, %r13, %r21, and %r29 can be made the index in order to
6214
   avoid the otherwise necessary Disp8, if the other operand is also
6215
   from that set and REX2 would be required to encode the insn, the
6216
   resulting encoding would be no smaller than the EVEX one.  */
6217
0
      && (i.op[1].regs->reg_num != 5
6218
0
    || pp.encoding != encoding_egpr
6219
0
    || i.imm_operands > 0
6220
0
    || i.op[0].regs->reg_num != 5))
6221
0
    {
6222
      /* Optimize: -Os:
6223
     {nf} addw %N, %M    -> leaw (%rM,%rN), %M
6224
     {nf} addl %eN, %eM  -> leal (%rM,%rN), %eM
6225
     {nf} addq %rN, %rM  -> leaq (%rM,%rN), %rM
6226
6227
     {nf} shlw $1, %N   -> leaw (%rN,%rN), %N
6228
     {nf} shll $1, %eN  -> leal (%rN,%rN), %eN
6229
     {nf} shlq $1, %rN  -> leaq (%rN,%rN), %rN
6230
6231
     {nf} addl %eK, %eN, %eM  -> leal (%rN,%rK), %eM
6232
     {nf} addq %rK, %rN, %rM  -> leaq (%rN,%rK), %rM
6233
6234
     {nf} shll $1, %eN, %eM  -> leal (%rN,%rN), %eM
6235
     {nf} shlq $1, %rN, %rM  -> leaq (%rN,%rN), %rM
6236
       */
6237
0
      i.tm.opcode_space = SPACE_BASE;
6238
0
      i.tm.base_opcode = 0x8d;
6239
0
      i.tm.extension_opcode = None;
6240
0
      i.tm.opcode_modifier.evex = 0;
6241
0
      i.tm.opcode_modifier.vexvvvv = 0;
6242
0
      if (i.imm_operands != 0)
6243
0
  i.index_reg = i.base_reg = i.op[1].regs;
6244
0
      else if (!is_index (i.op[0].regs)
6245
0
         || (i.op[1].regs->reg_num == 5
6246
0
       && i.op[0].regs->reg_num != 5))
6247
0
  {
6248
0
    i.base_reg = i.op[0].regs;
6249
0
    i.index_reg = i.op[1].regs;
6250
0
  }
6251
0
      else
6252
0
  {
6253
0
    i.base_reg = i.op[1].regs;
6254
0
    i.index_reg = i.op[0].regs;
6255
0
  }
6256
0
      if (i.types[1].bitfield.word)
6257
0
  {
6258
    /* NB: No similar adjustment is needed when operand size is 32-bit.  */
6259
0
    i.base_reg += 64;
6260
0
    i.index_reg += 64;
6261
0
  }
6262
0
      i.op[1].regs = i.op[i.operands - 1].regs;
6263
6264
0
      operand_type_set (&i.types[0], 0);
6265
0
      i.types[0].bitfield.baseindex = 1;
6266
0
      i.tm.operand_types[0] = i.types[0];
6267
0
      i.op[0].disps = NULL;
6268
0
      i.flags[0] = Operand_Mem;
6269
6270
0
      i.operands = 2;
6271
0
      i.mem_operands = i.reg_operands = 1;
6272
0
      i.imm_operands = 0;
6273
0
      pp.has_nf = false;
6274
0
    }
6275
0
  else if (optimize_for_space
6276
0
     && pp.encoding != encoding_evex
6277
0
     && (i.tm.base_opcode == 0x80 || i.tm.base_opcode == 0x83)
6278
0
     && (i.tm.extension_opcode == 0
6279
0
         || (i.tm.extension_opcode == 5
6280
0
       && i.op[0].imms->X_op == O_constant
6281
       /* Subtraction of -0x80 will end up smaller only if neither
6282
          operand size nor REX/REX2 prefixes are needed.  */
6283
0
       && (i.op[0].imms->X_add_number != -0x80
6284
0
           || (i.types[1].bitfield.dword
6285
0
               && !(i.op[1].regs->reg_flags & RegRex)
6286
0
               && !(i.op[i.operands - 1].regs->reg_flags & RegRex)
6287
0
               && pp.encoding != encoding_egpr))))
6288
0
     && !i.mem_operands
6289
0
     && !i.types[1].bitfield.byte
6290
     /* 16-bit operand size has extra restrictions: If REX2 was needed,
6291
        no size reduction would be possible.  Plus 3-operand forms zero-
6292
        extend the result, which can't be expressed with LEA.  */
6293
0
     && (!i.types[1].bitfield.word
6294
0
         || (i.operands == 2 && pp.encoding != encoding_egpr))
6295
0
     && is_plausible_suffix (1))
6296
0
    {
6297
      /* Optimize: -Os:
6298
     {nf} addw $N, %M   -> leaw N(%rM), %M
6299
     {nf} addl $N, %eM  -> leal N(%rM), %eM
6300
     {nf} addq $N, %rM  -> leaq N(%rM), %rM
6301
6302
     {nf} subw $N, %M   -> leaw -N(%rM), %M
6303
     {nf} subl $N, %eM  -> leal -N(%rM), %eM
6304
     {nf} subq $N, %rM  -> leaq -N(%rM), %rM
6305
6306
     {nf} addl $N, %eK, %eM  -> leal N(%rK), %eM
6307
     {nf} addq $N, %rK, %rM  -> leaq N(%rK), %rM
6308
6309
     {nf} subl $N, %eK, %eM  -> leal -N(%rK), %eM
6310
     {nf} subq $N, %rK, %rM  -> leaq -N(%rK), %rM
6311
       */
6312
0
      i.tm.opcode_space = SPACE_BASE;
6313
0
      i.tm.base_opcode = 0x8d;
6314
0
      if (i.tm.extension_opcode == 5)
6315
0
  i.op[0].imms->X_add_number = -i.op[0].imms->X_add_number;
6316
0
      i.tm.extension_opcode = None;
6317
0
      i.tm.opcode_modifier.evex = 0;
6318
0
      i.tm.opcode_modifier.vexvvvv = 0;
6319
0
      i.base_reg = i.op[1].regs;
6320
0
      if (i.types[1].bitfield.word)
6321
0
  {
6322
    /* NB: No similar adjustment is needed when operand size is 32-bit.  */
6323
0
    i.base_reg += 64;
6324
0
  }
6325
0
      i.op[1].regs = i.op[i.operands - 1].regs;
6326
6327
0
      operand_type_set (&i.types[0], 0);
6328
0
      i.types[0].bitfield.baseindex = 1;
6329
0
      i.types[0].bitfield.disp32 = 1;
6330
0
      i.op[0].disps = i.op[0].imms;
6331
0
      i.flags[0] = Operand_Mem;
6332
0
      optimize_disp (&i.tm);
6333
0
      i.tm.operand_types[0] = i.types[0];
6334
6335
0
      i.operands = 2;
6336
0
      i.disp_operands = i.mem_operands = i.reg_operands = 1;
6337
0
      i.imm_operands = 0;
6338
0
      pp.has_nf = false;
6339
0
    }
6340
0
  else if (i.tm.base_opcode == 0x6b
6341
0
     && !i.mem_operands
6342
0
     && pp.encoding != encoding_evex
6343
0
     && i.tm.mnem_off != MN_imulzu
6344
0
     && is_plausible_suffix (1)
6345
     /* %rsp can't be the index.  */
6346
0
     && is_index (i.op[1].regs)
6347
     /* There's no reduction in size for 16-bit forms requiring Disp8 and
6348
        REX2.  */
6349
0
     && (!optimize_for_space
6350
0
         || !i.types[1].bitfield.word
6351
0
         || i.op[1].regs->reg_num != 5
6352
0
         || pp.encoding != encoding_egpr)
6353
0
     && i.op[0].imms->X_op == O_constant
6354
0
     && (i.op[0].imms->X_add_number == 3
6355
0
         || i.op[0].imms->X_add_number == 5
6356
0
         || i.op[0].imms->X_add_number == 9))
6357
0
    {
6358
      /* Optimize: -O:
6359
        For n one of 3, 5, or 9
6360
     {nf} imulw $n, %N, %M    -> leaw (%rN,%rN,n-1), %M
6361
     {nf} imull $n, %eN, %eM  -> leal (%rN,%rN,n-1), %eM
6362
     {nf} imulq $n, %rN, %rM  -> leaq (%rN,%rN,n-1), %rM
6363
6364
     {nf} imulw $n, %N   -> leaw (%rN,%rN,s), %N
6365
     {nf} imull $n, %eN  -> leal (%rN,%rN,s), %eN
6366
     {nf} imulq $n, %rN  -> leaq (%rN,%rN,s), %rN
6367
       */
6368
0
      i.tm.opcode_space = SPACE_BASE;
6369
0
      i.tm.base_opcode = 0x8d;
6370
0
      i.tm.extension_opcode = None;
6371
0
      i.tm.opcode_modifier.evex = 0;
6372
0
      i.base_reg = i.op[1].regs;
6373
      /* NB: No similar adjustment is needed when operand size is 32 bits.  */
6374
0
      if (i.types[1].bitfield.word)
6375
0
  i.base_reg += 64;
6376
0
      i.index_reg = i.base_reg;
6377
0
      i.log2_scale_factor = i.op[0].imms->X_add_number == 9
6378
0
          ? 3 : i.op[0].imms->X_add_number >> 1;
6379
6380
0
      operand_type_set (&i.types[0], 0);
6381
0
      i.types[0].bitfield.baseindex = 1;
6382
0
      i.tm.operand_types[0] = i.types[0];
6383
0
      i.op[0].disps = NULL;
6384
0
      i.flags[0] = Operand_Mem;
6385
6386
0
      copy_operand (1, i.operands - 1);
6387
6388
0
      i.operands = 2;
6389
0
      i.mem_operands = i.reg_operands = 1;
6390
0
      i.imm_operands = 0;
6391
0
      pp.has_nf = false;
6392
0
    }
6393
0
  else if (cpu_arch_isa_flags.bitfield.cpubmi2
6394
0
     && pp.encoding == encoding_default
6395
0
     && (i.operands > 2 || !i.mem_operands)
6396
0
     && (i.types[i.operands - 1].bitfield.dword
6397
0
         || i.types[i.operands - 1].bitfield.qword))
6398
0
    {
6399
0
      if (i.tm.base_opcode == 0xd2)
6400
0
  {
6401
    /* Optimize: -O:
6402
         <OP> one of sal, sar, shl, shr:
6403
         {nf} <OP> %cl, %rN       -> <OP>x %{e,r}cx, %rN, %rN (N < 16)
6404
         {nf} <OP> %cl, ..., %rN  -> <OP>x %{e,r}cx, ..., %rN (no eGPR used)
6405
     */
6406
0
    gas_assert (i.tm.extension_opcode & 4);
6407
0
    i.tm.operand_types[0] = i.tm.operand_types[i.operands - 1];
6408
    /* NB: i.op[0].regs specifying %cl is good enough.  */
6409
0
    i.types[0] = i.types[i.operands - 1];
6410
0
    if (i.operands == 2)
6411
0
      {
6412
0
        i.tm.operand_types[0].bitfield.baseindex = 0;
6413
0
        i.tm.operand_types[2] = i.tm.operand_types[0];
6414
0
        i.op[2].regs = i.op[1].regs;
6415
0
        i.types[2] = i.types[1];
6416
0
        i.reg_operands = i.operands = 3;
6417
0
      }
6418
0
    pp.has_nf = false;
6419
0
    i.tm.opcode_modifier.w = 0;
6420
0
    i.tm.opcode_modifier.evex = 0;
6421
0
    i.tm.opcode_modifier.vex = VEX128;
6422
0
    i.tm.opcode_modifier.vexvvvv = VexVVVV_SRC2;
6423
0
    i.tm.opcode_space = SPACE_0F38;
6424
0
    i.tm.base_opcode = 0xf7;
6425
0
    i.tm.opcode_modifier.opcodeprefix
6426
0
      = !(i.tm.extension_opcode & 1)
6427
0
        ? PREFIX_0X66 /* shlx */
6428
0
        : i.tm.extension_opcode & 2
6429
0
    ? PREFIX_0XF3 /* sarx */
6430
0
    : PREFIX_0XF2 /* shrx */;
6431
0
    i.tm.extension_opcode = None;
6432
0
  }
6433
0
      else if (i.tm.base_opcode == 0xc0
6434
0
         && i.tm.extension_opcode <= 1
6435
0
         && i.op[0].imms->X_op == O_constant)
6436
0
  {
6437
    /* Optimize: -O:
6438
         {nf} rol $I, %rN       -> rorx $osz-I, %rN, %rN (I != osz-1, N < 16)
6439
         {nf} rol $I, ..., %rN  -> rorx $osz-I, ..., %rN (I != osz-1, no eGPR used)
6440
         {nf} ror $I, %rN       -> rorx $I, %rN, %rN (I != 1, N < 16)
6441
         {nf} ror $I, ..., %rN  -> rorx $I,..., %rN (I != 1, no eGPR used)
6442
       NB: rol -> ror transformation for I == osz-1 was already handled above.
6443
       NB2: ror with an immediate of 1 uses a different base opcode.
6444
     */
6445
0
    if (i.operands == 2)
6446
0
      {
6447
0
        copy_operand (2, 1);
6448
0
        i.tm.operand_types[2].bitfield.baseindex = 0;
6449
0
        i.reg_operands = 2;
6450
0
        i.operands = 3;
6451
0
      }
6452
0
    pp.has_nf = false;
6453
0
    i.tm.opcode_modifier.w = 0;
6454
0
    i.tm.opcode_modifier.evex = 0;
6455
0
    i.tm.opcode_modifier.vex = VEX128;
6456
0
    i.tm.opcode_modifier.vexvvvv = 0;
6457
0
    i.tm.opcode_space = SPACE_0F3A;
6458
0
    i.tm.base_opcode = 0xf0;
6459
0
    i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF2;
6460
0
    if (!i.tm.extension_opcode)
6461
0
      i.op[0].imms->X_add_number =
6462
0
        (i.types[i.operands - 1].bitfield.byte
6463
0
         ? 8 : i.types[i.operands - 1].bitfield.word
6464
0
         ? 16 : 64 >> i.types[i.operands - 1].bitfield.dword)
6465
0
        - i.op[0].imms->X_add_number;
6466
0
    i.tm.extension_opcode = None;
6467
0
  }
6468
0
      else if (i.tm.base_opcode == 0xf6
6469
0
         && i.tm.extension_opcode == 4
6470
0
         && !i.mem_operands
6471
0
         && i.op[0].regs->reg_num == 2
6472
0
         && !(i.op[0].regs->reg_flags & RegRex) )
6473
0
  {
6474
    /* Optimize: -O:
6475
         {nf} mul %edx  -> mulx %eax, %eax, %edx
6476
         {nf} mul %rdx  -> mulx %rax, %rax, %rdx
6477
     */
6478
0
    i.tm.operand_types[1] = i.tm.operand_types[0];
6479
0
    i.tm.operand_types[1].bitfield.baseindex = 0;
6480
0
    i.tm.operand_types[2] = i.tm.operand_types[1];
6481
0
    i.op[2].regs = i.op[0].regs;
6482
    /* NB: %eax is good enough also for 64-bit operand size.  */
6483
0
    i.op[1].regs = i.op[0].regs = reg_eax;
6484
0
    i.types[2] = i.types[1] = i.types[0];
6485
0
    i.reg_operands = i.operands = 3;
6486
6487
0
    pp.has_nf = false;
6488
0
    i.tm.opcode_modifier.w = 0;
6489
0
    i.tm.opcode_modifier.evex = 0;
6490
0
    i.tm.opcode_modifier.vex = VEX128;
6491
0
    i.tm.opcode_modifier.vexvvvv = VexVVVV_SRC1;
6492
0
    i.tm.opcode_space = SPACE_0F38;
6493
0
    i.tm.base_opcode = 0xf6;
6494
0
    i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF2;
6495
0
    i.tm.extension_opcode = None;
6496
0
  }
6497
0
    }
6498
0
}
6499
6500
static void
6501
s_noopt (int dummy ATTRIBUTE_UNUSED)
6502
47
{
6503
47
  if (!is_it_end_of_statement ())
6504
29
    as_warn (_("`.noopt' arguments ignored"));
6505
6506
47
  optimize = 0;
6507
47
  optimize_for_space = 0;
6508
6509
47
  ignore_rest_of_line ();
6510
47
}
6511
6512
/* Return non-zero for load instruction.  */
6513
6514
static int
6515
load_insn_p (void)
6516
0
{
6517
0
  unsigned int dest;
6518
0
  int any_vex_p = is_any_vex_encoding (&i.tm);
6519
0
  unsigned int base_opcode = i.tm.base_opcode | 1;
6520
6521
0
  if (!any_vex_p)
6522
0
    {
6523
      /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
6524
   bndcn, bndstx, bndldx, clflushopt, clwb, cldemote.  */
6525
0
      if (i.tm.opcode_modifier.operandconstraint == ANY_SIZE)
6526
0
  return 0;
6527
6528
      /* pop.   */
6529
0
      if (i.tm.mnem_off == MN_pop)
6530
0
  return 1;
6531
0
    }
6532
6533
0
  if (i.tm.opcode_space == SPACE_BASE)
6534
0
    {
6535
      /* popf, popa.   */
6536
0
      if (i.tm.base_opcode == 0x9d
6537
0
    || i.tm.base_opcode == 0x61)
6538
0
  return 1;
6539
6540
      /* movs, cmps, lods, scas.  */
6541
0
      if ((i.tm.base_opcode | 0xb) == 0xaf)
6542
0
  return 1;
6543
6544
      /* outs, xlatb.  */
6545
0
      if (base_opcode == 0x6f
6546
0
    || i.tm.base_opcode == 0xd7)
6547
0
  return 1;
6548
      /* NB: For AMD-specific insns with implicit memory operands,
6549
   they're intentionally not covered.  */
6550
0
    }
6551
6552
  /* No memory operand.  */
6553
0
  if (!i.mem_operands)
6554
0
    return 0;
6555
6556
0
  if (any_vex_p)
6557
0
    {
6558
0
      if (i.tm.mnem_off == MN_vldmxcsr)
6559
0
  return 1;
6560
0
    }
6561
0
  else if (i.tm.opcode_space == SPACE_BASE)
6562
0
    {
6563
      /* test, not, neg, mul, imul, div, idiv.  */
6564
0
      if (base_opcode == 0xf7 && i.tm.extension_opcode != 1)
6565
0
  return 1;
6566
6567
      /* inc, dec.  */
6568
0
      if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
6569
0
  return 1;
6570
6571
      /* add, or, adc, sbb, and, sub, xor, cmp.  */
6572
0
      if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
6573
0
  return 1;
6574
6575
      /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
6576
0
      if ((base_opcode == 0xc1 || (base_opcode | 2) == 0xd3)
6577
0
    && i.tm.extension_opcode != 6)
6578
0
  return 1;
6579
6580
      /* Check for x87 instructions.  */
6581
0
      if ((base_opcode | 6) == 0xdf)
6582
0
  {
6583
    /* Skip fst, fstp, fstenv, fstcw.  */
6584
0
    if (i.tm.base_opcode == 0xd9
6585
0
        && (i.tm.extension_opcode == 2
6586
0
      || i.tm.extension_opcode == 3
6587
0
      || i.tm.extension_opcode == 6
6588
0
      || i.tm.extension_opcode == 7))
6589
0
      return 0;
6590
6591
    /* Skip fisttp, fist, fistp, fstp.  */
6592
0
    if (i.tm.base_opcode == 0xdb
6593
0
        && (i.tm.extension_opcode == 1
6594
0
      || i.tm.extension_opcode == 2
6595
0
      || i.tm.extension_opcode == 3
6596
0
      || i.tm.extension_opcode == 7))
6597
0
      return 0;
6598
6599
    /* Skip fisttp, fst, fstp, fsave, fstsw.  */
6600
0
    if (i.tm.base_opcode == 0xdd
6601
0
        && (i.tm.extension_opcode == 1
6602
0
      || i.tm.extension_opcode == 2
6603
0
      || i.tm.extension_opcode == 3
6604
0
      || i.tm.extension_opcode == 6
6605
0
      || i.tm.extension_opcode == 7))
6606
0
      return 0;
6607
6608
    /* Skip fisttp, fist, fistp, fbstp, fistp.  */
6609
0
    if (i.tm.base_opcode == 0xdf
6610
0
        && (i.tm.extension_opcode == 1
6611
0
      || i.tm.extension_opcode == 2
6612
0
      || i.tm.extension_opcode == 3
6613
0
      || i.tm.extension_opcode == 6
6614
0
      || i.tm.extension_opcode == 7))
6615
0
      return 0;
6616
6617
0
    return 1;
6618
0
  }
6619
0
    }
6620
0
  else if (i.tm.opcode_space == SPACE_0F)
6621
0
    {
6622
      /* bt, bts, btr, btc.  */
6623
0
      if (i.tm.base_opcode == 0xba
6624
0
    && (i.tm.extension_opcode | 3) == 7)
6625
0
  return 1;
6626
6627
      /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld.  */
6628
0
      if (i.tm.base_opcode == 0xc7
6629
0
    && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
6630
0
    && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
6631
0
        || i.tm.extension_opcode == 6))
6632
0
  return 1;
6633
6634
      /* fxrstor, ldmxcsr, xrstor.  */
6635
0
      if (i.tm.base_opcode == 0xae
6636
0
    && (i.tm.extension_opcode == 1
6637
0
        || i.tm.extension_opcode == 2
6638
0
        || i.tm.extension_opcode == 5))
6639
0
  return 1;
6640
6641
      /* lgdt, lidt, lmsw.  */
6642
0
      if (i.tm.base_opcode == 0x01
6643
0
    && (i.tm.extension_opcode == 2
6644
0
        || i.tm.extension_opcode == 3
6645
0
        || i.tm.extension_opcode == 6))
6646
0
  return 1;
6647
0
    }
6648
6649
0
  dest = i.operands - 1;
6650
6651
  /* Check fake imm8 operand and 3 source operands.  */
6652
0
  if ((i.tm.opcode_modifier.immext
6653
0
       || i.reg_operands + i.mem_operands == 4)
6654
0
      && i.types[dest].bitfield.imm8)
6655
0
    dest--;
6656
6657
  /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg.  */
6658
0
  if (i.tm.opcode_space == SPACE_BASE
6659
0
      && ((base_opcode | 0x38) == 0x39
6660
0
    || (base_opcode | 2) == 0x87))
6661
0
    return 1;
6662
6663
0
  if (i.tm.mnem_off == MN_xadd)
6664
0
    return 1;
6665
6666
  /* Check for load instruction.  */
6667
0
  return (i.types[dest].bitfield.class != ClassNone
6668
0
    || i.types[dest].bitfield.instance == Accum);
6669
0
}
6670
6671
/* Output lfence, 0xfaee8, after instruction.  */
6672
6673
static void
6674
insert_lfence_after (void)
6675
10.8k
{
6676
10.8k
  if (lfence_after_load && load_insn_p ())
6677
0
    {
6678
      /* There are also two REP string instructions that require
6679
   special treatment. Specifically, the compare string (CMPS)
6680
   and scan string (SCAS) instructions set EFLAGS in a manner
6681
   that depends on the data being compared/scanned. When used
6682
   with a REP prefix, the number of iterations may therefore
6683
   vary depending on this data. If the data is a program secret
6684
   chosen by the adversary using an LVI method,
6685
   then this data-dependent behavior may leak some aspect
6686
   of the secret.  */
6687
0
      if (((i.tm.base_opcode | 0x9) == 0xaf)
6688
0
    && i.prefix[REP_PREFIX])
6689
0
  {
6690
0
      as_warn (_("`%s` changes flags which would affect control flow behavior"),
6691
0
         insn_name (&i.tm));
6692
0
  }
6693
0
      char *p = frag_more (3);
6694
0
      *p++ = 0xf;
6695
0
      *p++ = 0xae;
6696
0
      *p = 0xe8;
6697
0
    }
6698
10.8k
}
6699
6700
/* Output lfence, 0xfaee8, before instruction.  */
6701
6702
static void
6703
insert_lfence_before (const struct last_insn *last_insn)
6704
10.8k
{
6705
10.8k
  char *p;
6706
6707
10.8k
  if (i.tm.opcode_space != SPACE_BASE)
6708
8.13k
    return;
6709
6710
2.73k
  if (i.tm.base_opcode == 0xff
6711
17
      && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
6712
9
    {
6713
      /* Insert lfence before indirect branch if needed.  */
6714
6715
9
      if (lfence_before_indirect_branch == lfence_branch_none)
6716
9
  return;
6717
6718
0
      if (i.operands != 1)
6719
0
  abort ();
6720
6721
0
      if (i.reg_operands == 1)
6722
0
  {
6723
    /* Indirect branch via register.  Don't insert lfence with
6724
       -mlfence-after-load=yes.  */
6725
0
    if (lfence_after_load
6726
0
        || lfence_before_indirect_branch == lfence_branch_memory)
6727
0
      return;
6728
0
  }
6729
0
      else if (i.mem_operands == 1
6730
0
         && lfence_before_indirect_branch != lfence_branch_register)
6731
0
  {
6732
0
    as_warn (_("indirect `%s` with memory operand should be avoided"),
6733
0
       insn_name (&i.tm));
6734
0
    return;
6735
0
  }
6736
0
      else
6737
0
  return;
6738
6739
0
      if (last_insn->kind != last_insn_other)
6740
0
  {
6741
0
    as_warn_where (last_insn->file, last_insn->line,
6742
0
       _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
6743
0
       last_insn->name, insn_name (&i.tm));
6744
0
    return;
6745
0
  }
6746
6747
0
      p = frag_more (3);
6748
0
      *p++ = 0xf;
6749
0
      *p++ = 0xae;
6750
0
      *p = 0xe8;
6751
0
      return;
6752
0
    }
6753
6754
  /* Output or/not/shl and lfence before near ret.  */
6755
2.72k
  if (lfence_before_ret != lfence_before_ret_none
6756
0
      && (i.tm.base_opcode | 1) == 0xc3)
6757
0
    {
6758
0
      if (last_insn->kind != last_insn_other)
6759
0
  {
6760
0
    as_warn_where (last_insn->file, last_insn->line,
6761
0
       _("`%s` skips -mlfence-before-ret on `%s`"),
6762
0
       last_insn->name, insn_name (&i.tm));
6763
0
    return;
6764
0
  }
6765
6766
      /* Near ret ingore operand size override under CPU64.  */
6767
0
      char prefix = flag_code == CODE_64BIT
6768
0
        ? 0x48
6769
0
        : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
6770
6771
0
      if (lfence_before_ret == lfence_before_ret_not)
6772
0
  {
6773
    /* not: 0xf71424, may add prefix
6774
       for operand size override or 64-bit code.  */
6775
0
    p = frag_more ((prefix ? 2 : 0) + 6 + 3);
6776
0
    if (prefix)
6777
0
      *p++ = prefix;
6778
0
    *p++ = 0xf7;
6779
0
    *p++ = 0x14;
6780
0
    *p++ = 0x24;
6781
0
    if (prefix)
6782
0
      *p++ = prefix;
6783
0
    *p++ = 0xf7;
6784
0
    *p++ = 0x14;
6785
0
    *p++ = 0x24;
6786
0
  }
6787
0
      else
6788
0
  {
6789
0
    p = frag_more ((prefix ? 1 : 0) + 4 + 3);
6790
0
    if (prefix)
6791
0
      *p++ = prefix;
6792
0
    if (lfence_before_ret == lfence_before_ret_or)
6793
0
      {
6794
        /* or: 0x830c2400, may add prefix
6795
     for operand size override or 64-bit code.  */
6796
0
        *p++ = 0x83;
6797
0
        *p++ = 0x0c;
6798
0
      }
6799
0
    else
6800
0
      {
6801
        /* shl: 0xc1242400, may add prefix
6802
     for operand size override or 64-bit code.  */
6803
0
        *p++ = 0xc1;
6804
0
        *p++ = 0x24;
6805
0
      }
6806
6807
0
    *p++ = 0x24;
6808
0
    *p++ = 0x0;
6809
0
  }
6810
6811
0
      *p++ = 0xf;
6812
0
      *p++ = 0xae;
6813
0
      *p = 0xe8;
6814
0
    }
6815
2.72k
}
6816
6817
/* Shared helper for md_assemble() and s_insn().  */
6818
static void init_globals (void)
6819
133k
{
6820
133k
  unsigned int j;
6821
6822
133k
  memset (&i, '\0', sizeof (i));
6823
133k
  i.rounding.type = rc_none;
6824
801k
  for (j = 0; j < MAX_OPERANDS; j++)
6825
668k
    i.reloc[j] = NO_RELOC;
6826
133k
  memset (disp_expressions, '\0', sizeof (disp_expressions));
6827
133k
  memset (im_expressions, '\0', sizeof (im_expressions));
6828
133k
  save_stack_p = save_stack;
6829
133k
}
6830
6831
/* Helper for md_assemble() to decide whether to prepare for a possible 2nd
6832
   parsing pass. Instead of introducing a rarely used new insn attribute this
6833
   utilizes a common pattern between affected templates. It is deemed
6834
   acceptable that this will lead to unnecessary pass 2 preparations in a
6835
   limited set of cases.  */
6836
static INLINE bool may_need_pass2 (const insn_template *t)
6837
21.2k
{
6838
21.2k
  return t->opcode_modifier.sse2avx
6839
   /* Note that all SSE2AVX templates have at least one operand.  */
6840
21.2k
   ? t->operand_types[t->operands - 1].bitfield.class == RegSIMD
6841
21.2k
   : (t->opcode_space == SPACE_0F
6842
9.27k
      && (t->base_opcode | 1) == 0xbf)
6843
20.5k
     || (t->opcode_space == SPACE_BASE
6844
3.09k
         && t->base_opcode == 0x63)
6845
20.5k
     || (intel_syntax /* shld / shrd may mean suffixed shl / shr.  */
6846
7.17k
         && t->opcode_space == SPACE_MAP4
6847
4.59k
         && (t->base_opcode | 8) == 0x2c);
6848
21.2k
}
6849
6850
#ifdef OBJ_ELF
6851
static enum x86_tls_error_type
6852
x86_check_tls_relocation (enum bfd_reloc_code_real r_type)
6853
8.09k
{
6854
8.09k
  switch (r_type)
6855
8.09k
    {
6856
0
    case BFD_RELOC_386_TLS_GOTDESC:
6857
      /* Check GDesc access model:
6858
6859
   leal x@tlsdesc(%ebx), %reg32 --> Memory reg must be %ebx and
6860
            SIB is not supported.
6861
       */
6862
0
      if (i.tm.mnem_off != MN_lea)
6863
0
  return x86_tls_error_insn;
6864
0
      if (i.index_reg)
6865
0
  return x86_tls_error_sib;
6866
0
      if (!i.base_reg)
6867
0
  return x86_tls_error_no_base_reg;
6868
0
      if (i.base_reg->reg_type.bitfield.instance != RegB)
6869
0
  return x86_tls_error_ebx;
6870
0
      if (!i.op[1].regs->reg_type.bitfield.dword)
6871
0
  return x86_tls_error_dest_32bit_reg_size;
6872
0
      break;
6873
6874
0
    case BFD_RELOC_386_TLS_GD:
6875
      /* Check GD access model:
6876
6877
   leal foo@tlsgd(,%ebx,1), %eax   --> Only this fixed format is supported.
6878
   leal foo@tlsgd(%reg32), %eax    --> Dest reg must be '%eax'
6879
               Memory reg can't be %eax.
6880
       */
6881
0
      if (i.tm.mnem_off != MN_lea)
6882
0
  return x86_tls_error_insn;
6883
0
      if (i.op[1].regs->reg_type.bitfield.instance != Accum)
6884
0
  return x86_tls_error_dest_eax;
6885
0
      if (!i.op[1].regs->reg_type.bitfield.dword)
6886
0
  return x86_tls_error_dest_32bit_reg_size;
6887
0
      if (i.index_reg)
6888
0
  {
6889
0
    if (i.base_reg)
6890
0
      return x86_tls_error_base_reg;
6891
0
    if (i.index_reg->reg_type.bitfield.instance != RegB)
6892
0
      return x86_tls_error_index_ebx;
6893
0
    if (i.log2_scale_factor)
6894
0
      return x86_tls_error_scale_factor;
6895
0
  }
6896
0
      else
6897
0
  {
6898
0
    if (!i.base_reg)
6899
0
      return x86_tls_error_no_base_reg;
6900
0
    if (i.base_reg->reg_type.bitfield.instance == Accum)
6901
0
      return x86_tls_error_eax;
6902
0
  }
6903
0
      break;
6904
6905
0
    case BFD_RELOC_386_TLS_LDM:
6906
      /*  Check LDM access model:
6907
6908
    leal foo@tlsldm(%reg32), %eax --> Dest reg must be '%eax'
6909
                    Memory reg can't be %eax and SIB
6910
              is not supported.
6911
       */
6912
0
      if (i.tm.mnem_off != MN_lea)
6913
0
  return x86_tls_error_insn;
6914
0
      if (i.index_reg)
6915
0
  return x86_tls_error_sib;
6916
0
      if (!i.base_reg)
6917
0
  return x86_tls_error_no_base_reg;
6918
0
      if (i.base_reg->reg_type.bitfield.instance == Accum)
6919
0
  return x86_tls_error_eax;
6920
0
      if (i.op[1].regs->reg_type.bitfield.instance != Accum)
6921
0
  return x86_tls_error_dest_eax;
6922
0
      if (!i.op[1].regs->reg_type.bitfield.dword)
6923
0
  return x86_tls_error_dest_32bit_reg_size;
6924
0
      break;
6925
6926
0
    case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
6927
      /* Check GOTPC32 TLSDESC access model:
6928
6929
   --- LP64 mode ---
6930
   leaq x@tlsdesc(%rip), %reg64 --> Memory reg must be %rip.
6931
6932
   --- X32 mode ---
6933
   rex/rex2 leal x@tlsdesc(%rip), %reg32 --> Memory reg must be %rip.
6934
6935
   In X32 mode, gas will add rex/rex2 for it later, no need to check
6936
   here.
6937
       */
6938
0
      if (i.tm.mnem_off != MN_lea)
6939
0
  return x86_tls_error_insn;
6940
0
      if (!i.base_reg)
6941
0
  return x86_tls_error_no_base_reg;
6942
0
      if (i.base_reg->reg_num != RegIP
6943
0
    || !i.base_reg->reg_type.bitfield.qword)
6944
0
  return x86_tls_error_rip;
6945
0
      if (x86_elf_abi == X86_64_ABI)
6946
0
  {
6947
0
    if (!i.op[1].regs->reg_type.bitfield.qword)
6948
0
      return x86_tls_error_dest_64bit_reg_size;
6949
0
  }
6950
0
      else if (!i.op[1].regs->reg_type.bitfield.dword
6951
0
         && !i.op[1].regs->reg_type.bitfield.qword)
6952
0
  return x86_tls_error_dest_32bit_or_64bit_reg_size;
6953
0
    break;
6954
6955
0
    case BFD_RELOC_X86_64_TLSGD:
6956
      /* Check GD access model:
6957
6958
   leaq foo@tlsgd(%rip), %rdi --> Only this fixed format is supported.
6959
       */
6960
6
    case BFD_RELOC_X86_64_TLSLD:
6961
      /* Check LD access model:
6962
6963
   leaq foo@tlsld(%rip), %rdi --> Only this fixed format is supported.
6964
       */
6965
6
      if (i.tm.mnem_off != MN_lea)
6966
6
  return x86_tls_error_insn;
6967
0
      if (!i.base_reg)
6968
0
  return x86_tls_error_no_base_reg;
6969
0
      if (i.base_reg->reg_num != RegIP
6970
0
    || !i.base_reg->reg_type.bitfield.qword)
6971
0
  return x86_tls_error_rip;
6972
0
      if (!i.op[1].regs->reg_type.bitfield.qword
6973
0
    || i.op[1].regs->reg_num != EDI_REG_NUM
6974
0
    || i.op[1].regs->reg_flags)
6975
0
  return x86_tls_error_dest_rdi;
6976
0
      break;
6977
6978
0
    case BFD_RELOC_386_TLS_GOTIE:
6979
      /* Check GOTIE access model:
6980
6981
   subl foo@gotntpoff(%reg1), %reg2
6982
   movl foo@gotntpoff(%reg1), %reg2
6983
   addl foo@gotntpoff(%reg1), %reg2
6984
6985
   Memory operand: SIB is not supported.
6986
       */
6987
0
    case BFD_RELOC_386_TLS_IE_32:
6988
      /* Check IE_32 access model:
6989
6990
   subl foo@gottpoff(%reg1), %reg2
6991
   movl foo@gottpoff(%reg1), %reg2
6992
   addl foo@gottpoff(%reg1), %reg2
6993
6994
   Memory operand: SIB is not supported.
6995
       */
6996
0
      if (i.tm.mnem_off != MN_sub
6997
0
    && i.tm.mnem_off != MN_add
6998
0
    && i.tm.mnem_off != MN_mov)
6999
0
  return x86_tls_error_insn;
7000
0
      if (i.imm_operands
7001
0
    || i.disp_operands != 1
7002
0
    || i.reg_operands != 1
7003
0
    || i.types[1].bitfield.class != Reg)
7004
0
  return x86_tls_error_opcode;
7005
0
      if (!i.base_reg)
7006
0
  return x86_tls_error_no_base_reg;
7007
0
      if (i.index_reg)
7008
0
  return x86_tls_error_sib;
7009
0
      if (!i.base_reg->reg_type.bitfield.dword)
7010
0
  return x86_tls_error_base_reg_size;
7011
0
      if (!i.op[1].regs->reg_type.bitfield.dword)
7012
0
  return x86_tls_error_dest_32bit_reg_size;
7013
0
      break;
7014
7015
0
    case BFD_RELOC_386_TLS_IE:
7016
      /* Check IE access model:
7017
7018
   movl foo@indntpoff, %reg32 --> Mod == 00 && r/m == 5
7019
   addl foo@indntpoff, %reg32 --> Mod == 00 && r/m == 5
7020
       */
7021
0
      if (i.tm.mnem_off != MN_add && i.tm.mnem_off != MN_mov)
7022
0
  return x86_tls_error_insn;
7023
0
      if (i.imm_operands
7024
0
    || i.disp_operands != 1
7025
0
    || i.reg_operands != 1
7026
0
    || i.types[1].bitfield.class != Reg)
7027
0
  return x86_tls_error_opcode;
7028
0
      if (i.base_reg || i.index_reg)
7029
0
  return x86_tls_error_require_no_base_index_reg;
7030
0
      if (!i.op[1].regs->reg_type.bitfield.dword)
7031
0
  return x86_tls_error_dest_32bit_reg_size;
7032
0
      break;
7033
7034
0
    case BFD_RELOC_X86_64_GOTTPOFF:
7035
      /* Check GOTTPOFF access model:
7036
7037
   mov foo@gottpoff(%rip), %reg --> Memory Reg must be %rip.
7038
   movrs foo@gottpoff(%rip), %reg --> Memory Reg must be %rip.
7039
   add foo@gottpoff(%rip), %reg --> Memory Reg must be %rip.
7040
   add %reg1, foo@gottpoff(%rip), %reg2 --> Memory Reg must be %rip.
7041
   add foo@gottpoff(%rip), %reg1, %reg2 --> Memory Reg must be %rip.
7042
       */
7043
0
      if (i.tm.mnem_off != MN_add && i.tm.mnem_off != MN_mov
7044
0
    && i.tm.mnem_off != MN_movrs)
7045
0
  return x86_tls_error_insn;
7046
0
      if (i.imm_operands
7047
0
    || i.disp_operands != 1
7048
0
    || i.types[i.operands - 1].bitfield.class != Reg)
7049
0
  return x86_tls_error_opcode;
7050
0
      if (!i.base_reg)
7051
0
  return x86_tls_error_no_base_reg;
7052
0
      if (i.base_reg->reg_num != RegIP
7053
0
    || !i.base_reg->reg_type.bitfield.qword)
7054
0
  return x86_tls_error_rip;
7055
0
      if (x86_elf_abi == X86_64_ABI)
7056
0
  {
7057
0
    if (!i.op[i.operands - 1].regs->reg_type.bitfield.qword)
7058
0
      return x86_tls_error_dest_64bit_reg_size;
7059
0
  }
7060
0
      else if (!i.op[i.operands - 1].regs->reg_type.bitfield.dword
7061
0
         && !i.op[i.operands - 1].regs->reg_type.bitfield.qword)
7062
0
  return x86_tls_error_dest_32bit_or_64bit_reg_size;
7063
0
      break;
7064
7065
0
    case BFD_RELOC_386_TLS_DESC_CALL:
7066
      /* Check GDesc access model:
7067
7068
   call *x@tlscall(%eax) --> Memory reg must be %eax and
7069
           SIB is not supported.
7070
       */
7071
0
    case BFD_RELOC_X86_64_TLSDESC_CALL:
7072
      /* Check GDesc access model:
7073
7074
   call *x@tlscall(%rax) <--- LP64 mode.
7075
   call *x@tlscall(%eax) <--- X32 mode.
7076
7077
   Only these fixed formats are supported.
7078
       */
7079
0
      if (i.tm.mnem_off != MN_call)
7080
0
  return x86_tls_error_insn;
7081
0
      if (i.index_reg)
7082
0
  return x86_tls_error_sib;
7083
0
      if (!i.base_reg)
7084
0
  return x86_tls_error_no_base_reg;
7085
0
      if (i.base_reg->reg_type.bitfield.instance != Accum)
7086
0
  return x86_tls_error_RegA;
7087
0
      break;
7088
7089
0
    case BFD_RELOC_NONE:
7090
      /* This isn't a relocation.  */
7091
0
      return x86_tls_error_continue;
7092
7093
8.09k
    default:
7094
8.09k
      break;
7095
8.09k
    }
7096
7097
  /* This relocation is OK.  */
7098
8.09k
  return x86_tls_error_none;
7099
8.09k
}
7100
7101
static void
7102
x86_report_tls_error (enum x86_tls_error_type tls_error,
7103
          enum bfd_reloc_code_real r_type)
7104
6
{
7105
6
  unsigned int k;
7106
54
  for (k = 0; k < ARRAY_SIZE (gotrel); k++)
7107
54
    if (gotrel[k].rel[object_64bit] == r_type)
7108
6
      break;
7109
7110
6
  switch (tls_error)
7111
6
    {
7112
6
    case x86_tls_error_insn:
7113
6
      as_bad (_("@%s operator cannot be used with `%s'"),
7114
6
        gotrel[k].str, insn_name (&i.tm));
7115
6
      return;
7116
7117
0
    case x86_tls_error_opcode:
7118
0
      as_bad (_("@%s operator can be used with `%s', but format is wrong"),
7119
0
        gotrel[k].str, insn_name (&i.tm));
7120
0
      return;
7121
7122
0
    case x86_tls_error_sib:
7123
0
      as_bad (_("@%s operator requires no SIB"), gotrel[k].str);
7124
0
      return;
7125
7126
0
    case x86_tls_error_no_base_reg:
7127
0
      as_bad (_("@%s operator requires base register"), gotrel[k].str);
7128
0
      return;
7129
7130
0
    case x86_tls_error_require_no_base_index_reg:
7131
0
      as_bad (_("@%s operator requires no base/index register"),
7132
0
        gotrel[k].str);
7133
0
      return;
7134
7135
0
    case x86_tls_error_base_reg:
7136
0
      as_bad (_("@%s operator requires no base register"), gotrel[k].str);
7137
0
      return;
7138
7139
0
    case x86_tls_error_index_ebx:
7140
0
      as_bad (_("@%s operator requires `%sebx' as index register"),
7141
0
        gotrel[k].str, register_prefix);
7142
0
      return;
7143
7144
0
    case x86_tls_error_eax:
7145
0
      as_bad (_("@%s operator requires `%seax' as base register"),
7146
0
        gotrel[k].str, register_prefix);
7147
0
      return;
7148
7149
0
    case x86_tls_error_RegA:
7150
0
      as_bad (_("@%s operator requires `%seax/%srax' as base register"),
7151
0
        gotrel[k].str, register_prefix, register_prefix);
7152
0
      return;
7153
7154
0
    case x86_tls_error_ebx:
7155
0
      as_bad (_("@%s operator requires `%sebx' as base register"),
7156
0
        gotrel[k].str, register_prefix);
7157
0
      return;
7158
7159
0
    case x86_tls_error_rip:
7160
0
      as_bad (_("@%s operator requires `%srip' as base register"),
7161
0
        gotrel[k].str, register_prefix);
7162
0
      return;
7163
7164
0
    case x86_tls_error_dest_eax:
7165
0
      as_bad (_("@%s operator requires `%seax' as dest register"),
7166
0
        gotrel[k].str, register_prefix);
7167
0
      return;
7168
7169
0
    case x86_tls_error_dest_rdi:
7170
0
      as_bad (_("@%s operator requires `%srdi' as dest register"),
7171
0
        gotrel[k].str, register_prefix);
7172
0
      return;
7173
7174
0
    case x86_tls_error_scale_factor:
7175
0
      as_bad (_("@%s operator requires scale factor of 1"),
7176
0
        gotrel[k].str);
7177
0
      return;
7178
7179
0
    case x86_tls_error_base_reg_size:
7180
0
      as_bad (_("@%s operator requires 32-bit base register"),
7181
0
        gotrel[k].str);
7182
0
      return;
7183
7184
0
    case x86_tls_error_dest_32bit_reg_size:
7185
0
      as_bad (_("@%s operator requires 32-bit dest register"),
7186
0
        gotrel[k].str);
7187
0
      return;
7188
7189
0
    case x86_tls_error_dest_64bit_reg_size:
7190
0
      as_bad (_("@%s operator requires 64-bit dest register"),
7191
0
        gotrel[k].str);
7192
0
      return;
7193
7194
0
    case x86_tls_error_dest_32bit_or_64bit_reg_size:
7195
0
      as_bad (_("@%s operator requires 32-bit or 64-bit dest register"),
7196
0
        gotrel[k].str);
7197
0
      return;
7198
7199
0
    default:
7200
0
      abort ();
7201
6
    }
7202
6
}
7203
#endif
7204
7205
/* This is the guts of the machine-dependent assembler.  LINE points to a
7206
   machine dependent instruction.  This function is supposed to emit
7207
   the frags/bytes it assembles to.  */
7208
7209
static void
7210
i386_assemble (char *line)
7211
129k
{
7212
129k
  unsigned int j;
7213
129k
  char mnemonic[MAX_MNEM_SIZE], mnem_suffix = 0, *copy = NULL;
7214
129k
  char *xstrdup_copy = NULL;
7215
129k
  const char *end, *pass1_mnem = NULL;
7216
129k
  enum i386_error pass1_err = 0;
7217
129k
  struct pseudo_prefixes orig_pp = pp;
7218
129k
  const insn_template *t;
7219
129k
  struct last_insn *last_insn
7220
129k
    = &seg_info(now_seg)->tc_segment_info_data.last_insn;
7221
7222
  /* Initialize globals.  */
7223
129k
  current_templates.end = current_templates.start = NULL;
7224
130k
 retry:
7225
130k
  init_globals ();
7226
7227
  /* Suppress optimization when the last thing we saw may not have been
7228
     a proper instruction (e.g. a stand-alone prefix or .byte).  */
7229
130k
  if (last_insn->kind != last_insn_other)
7230
16.3k
    pp.no_optimize = true;
7231
7232
  /* First parse an instruction mnemonic & call i386_operand for the operands.
7233
     We assume that the scrubber has arranged it so that line[0] is the valid
7234
     start of a (possibly prefixed) mnemonic.  */
7235
7236
130k
  end = parse_insn (line, mnemonic, parse_all);
7237
130k
  if (end == NULL)
7238
109k
    {
7239
109k
      if (pass1_mnem != NULL)
7240
661
  goto match_error;
7241
108k
      if (i.error != no_error)
7242
135
  {
7243
135
    gas_assert (current_templates.start != NULL);
7244
135
    if (may_need_pass2 (current_templates.start) && !i.suffix)
7245
0
      goto no_match;
7246
    /* No point in trying a 2nd pass - it'll only find the same suffix
7247
       again.  */
7248
135
    mnem_suffix = i.suffix;
7249
135
    goto match_error;
7250
135
  }
7251
108k
      return;
7252
108k
    }
7253
21.2k
  t = current_templates.start;
7254
  /* NB: LINE may be change to be the same as XSTRDUP_COPY.  */
7255
21.2k
  if (xstrdup_copy != line && may_need_pass2 (t))
7256
736
    {
7257
      /* Make a copy of the full line in case we need to retry.  */
7258
736
      xstrdup_copy = xstrdup (line);
7259
736
      copy = xstrdup_copy;
7260
736
    }
7261
21.2k
  line += end - line;
7262
21.2k
  mnem_suffix = i.suffix;
7263
7264
21.2k
  line = parse_operands (line, mnemonic);
7265
21.2k
  this_operand = -1;
7266
21.2k
  if (line == NULL)
7267
5.63k
    {
7268
5.63k
      free (xstrdup_copy);
7269
5.63k
      return;
7270
5.63k
    }
7271
7272
  /* Now we've parsed the mnemonic into a set of templates, and have the
7273
     operands at hand.  */
7274
7275
  /* All Intel opcodes have reversed operands except for "bound", "enter",
7276
     "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
7277
     "rmpadjust", "rmpquery", and deprecated forms of "rmpupdate".
7278
     We also don't reverse intersegment "jmp" and "call" instructions with
7279
     2 immediate operands so that the immediate segment precedes the offset
7280
     consistently in Intel and AT&T modes.  */
7281
15.5k
  if (intel_syntax
7282
3.26k
      && i.operands > 1
7283
1.03k
      && (t->mnem_off != MN_bound)
7284
1.03k
      && !startswith (mnemonic, "invlpg")
7285
1.03k
      && !startswith (mnemonic, "monitor")
7286
1.03k
      && !startswith (mnemonic, "mwait")
7287
1.03k
      && (t->mnem_off != MN_pvalidate)
7288
1.03k
      && (!startswith (mnemonic, "rmp") || i.mem_operands)
7289
1.03k
      && (t->mnem_off != MN_tpause)
7290
1.03k
      && (t->mnem_off != MN_umwait)
7291
1.03k
      && !(i.operands == 2
7292
1.03k
     && operand_type_check (i.types[0], imm)
7293
18
     && operand_type_check (i.types[1], imm)))
7294
1.01k
    swap_operands ();
7295
7296
  /* The order of the immediates should be reversed for 2-immediates EXTRQ
7297
     and INSERTQ instructions.  Also OUT, UWRMSR, and WRMSRNS want their
7298
     immediate to be in the "canonical" place (first), despite it appearing
7299
     last (in AT&T syntax, or because of the swapping above) in the incoming
7300
     set of operands.  */
7301
15.5k
  if ((i.imm_operands == 2
7302
220
       && (t->mnem_off == MN_extrq || t->mnem_off == MN_insertq))
7303
15.5k
      || ((t->mnem_off == MN_out || t->mnem_off == MN_uwrmsr
7304
15.5k
     || t->mnem_off == MN_wrmsrns)
7305
44
    && i.imm_operands && i.operands > i.imm_operands))
7306
0
      swap_2_operands (0, 1);
7307
7308
  /* All legitimate immediates are placed first now.  Others, if any, will be
7309
     rejected by match_template() anyway.  */
7310
15.5k
  if (operand_type_check (i.types[0], imm))
7311
3.65k
    {
7312
      /* For USER_MSR and MSR_IMM instructions, imm32 stands for the name of a
7313
   model specific register (MSR). That's an unsigned quantity, whereas all
7314
   other insns with 32-bit immediate and 64-bit operand size use
7315
   sign-extended immediates (imm32s). Therefore these insns are
7316
   special-cased, bypassing the normal handling of immediates here.  */
7317
3.65k
      if (is_cpu(current_templates.start, CpuUSER_MSR)
7318
3.64k
    || t->mnem_off == MN_rdmsr
7319
3.64k
    || t->mnem_off == MN_wrmsrns)
7320
39
  i.types[0] = smallest_imm_type (i.op[0].imms->X_add_number);
7321
3.61k
      else
7322
3.61k
  optimize_imm ();
7323
3.65k
    }
7324
7325
15.5k
  if (i.disp_operands && !optimize_disp (t))
7326
0
    return;
7327
7328
  /* Next, we find a template that matches the given insn,
7329
     making sure the overlap of the given operands types is consistent
7330
     with the template operand types.  */
7331
7332
15.5k
  if (!(t = match_template (mnem_suffix)))
7333
4.10k
    {
7334
4.10k
      const char *err_msg;
7335
7336
4.10k
      if (copy && !mnem_suffix)
7337
701
  {
7338
701
    line = copy;
7339
701
    copy = NULL;
7340
701
  no_match:
7341
701
    pass1_err = i.error;
7342
701
    pass1_mnem = insn_name (current_templates.start);
7343
701
    pp = orig_pp;
7344
701
    goto retry;
7345
701
  }
7346
7347
      /* If a non-/only-64bit template (group) was found in pass 1, and if
7348
   _some_ template (group) was found in pass 2, squash pass 1's
7349
   error.  */
7350
3.39k
      if (pass1_err == unsupported_64bit)
7351
0
  pass1_mnem = NULL;
7352
7353
4.19k
  match_error:
7354
4.19k
      free (xstrdup_copy);
7355
7356
4.19k
      switch (pass1_mnem ? pass1_err : i.error)
7357
4.19k
  {
7358
0
  default:
7359
0
    abort ();
7360
13
  case operand_size_mismatch:
7361
13
    err_msg = _("operand size mismatch");
7362
13
    break;
7363
1.34k
  case operand_type_mismatch:
7364
1.34k
    err_msg = _("operand type mismatch");
7365
1.34k
    break;
7366
0
  case register_type_mismatch:
7367
0
    err_msg = _("register type mismatch");
7368
0
    break;
7369
2.65k
  case number_of_operands_mismatch:
7370
2.65k
    err_msg = _("number of operands mismatch");
7371
2.65k
    break;
7372
6
  case invalid_instruction_suffix:
7373
6
    err_msg = _("invalid instruction suffix");
7374
6
    break;
7375
0
  case bad_imm4:
7376
0
    err_msg = _("constant doesn't fit in 4 bits");
7377
0
    break;
7378
0
  case unsupported_with_intel_mnemonic:
7379
0
    err_msg = _("unsupported with Intel mnemonic");
7380
0
    break;
7381
0
  case unsupported_syntax:
7382
0
    err_msg = _("unsupported syntax");
7383
0
    break;
7384
0
  case unsupported_EGPR_for_addressing:
7385
0
    err_msg = _("extended GPR cannot be used as base/index");
7386
0
    break;
7387
3
  case unsupported_nf:
7388
3
    err_msg = _("{nf} unsupported");
7389
3
    break;
7390
2
  case unsupported:
7391
2
    as_bad (_("unsupported instruction `%s'"),
7392
2
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
7393
2
    return;
7394
121
  case unsupported_on_arch:
7395
121
    as_bad (_("`%s' is not supported on `%s%s'"),
7396
121
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start),
7397
121
      cpu_arch_name ? cpu_arch_name : default_arch,
7398
121
      cpu_sub_arch_name ? cpu_sub_arch_name : "");
7399
121
    return;
7400
14
  case unsupported_64bit:
7401
14
    if (ISLOWER (mnem_suffix))
7402
0
      {
7403
0
        if (flag_code == CODE_64BIT)
7404
0
    as_bad (_("`%s%c' is not supported in 64-bit mode"),
7405
0
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start),
7406
0
      mnem_suffix);
7407
0
        else
7408
0
    as_bad (_("`%s%c' is only supported in 64-bit mode"),
7409
0
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start),
7410
0
      mnem_suffix);
7411
0
      }
7412
14
    else
7413
14
      {
7414
14
        if (flag_code == CODE_64BIT)
7415
11
    as_bad (_("`%s' is not supported in 64-bit mode"),
7416
11
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
7417
3
        else
7418
3
    as_bad (_("`%s' is only supported in 64-bit mode"),
7419
3
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
7420
14
      }
7421
14
    return;
7422
35
  case no_vex_encoding:
7423
35
    err_msg = _("no VEX/XOP encoding");
7424
35
    break;
7425
5
  case no_evex_encoding:
7426
5
    err_msg = _("no EVEX encoding");
7427
5
    break;
7428
0
  case invalid_sib_address:
7429
0
    err_msg = _("invalid SIB address");
7430
0
    break;
7431
0
  case invalid_vsib_address:
7432
0
    err_msg = _("invalid VSIB address");
7433
0
    break;
7434
0
  case invalid_vector_register_set:
7435
0
    err_msg = _("mask, index, and destination registers must be distinct");
7436
0
    break;
7437
0
  case invalid_tmm_register_set:
7438
0
    err_msg = _("all tmm registers must be distinct");
7439
0
    break;
7440
0
  case invalid_dest_and_src_register_set:
7441
0
    err_msg = _("destination and source registers must be distinct");
7442
0
    break;
7443
0
  case invalid_dest_register_set:
7444
0
    err_msg = _("two dest registers must be distinct");
7445
0
    break;
7446
0
  case invalid_pseudo_prefix:
7447
0
    err_msg = _("rex2 pseudo prefix cannot be used");
7448
0
    break;
7449
0
  case unsupported_vector_index_register:
7450
0
    err_msg = _("unsupported vector index register");
7451
0
    break;
7452
0
  case unsupported_broadcast:
7453
0
    err_msg = _("unsupported broadcast");
7454
0
    break;
7455
0
  case broadcast_needed:
7456
0
    err_msg = _("broadcast is needed for operand of such type");
7457
0
    break;
7458
0
  case unsupported_masking:
7459
0
    err_msg = _("unsupported masking");
7460
0
    break;
7461
0
  case mask_not_on_destination:
7462
0
    err_msg = _("mask not on destination operand");
7463
0
    break;
7464
0
  case no_default_mask:
7465
0
    err_msg = _("default mask isn't allowed");
7466
0
    break;
7467
0
  case unsupported_rc_sae:
7468
0
    err_msg = _("unsupported static rounding/sae");
7469
0
    break;
7470
0
  case unsupported_vector_size:
7471
0
    as_bad (_("vector size above %u required for `%s'"), 128u << vector_size,
7472
0
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
7473
0
    return;
7474
0
  case unsupported_rsp_register:
7475
0
    err_msg = _("'rsp' register cannot be used");
7476
0
    break;
7477
0
  case internal_error:
7478
0
    err_msg = _("internal error");
7479
0
    break;
7480
4.19k
  }
7481
4.05k
      as_bad (_("%s for `%s'"), err_msg,
7482
4.05k
        pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
7483
4.05k
      return;
7484
4.19k
    }
7485
7486
11.4k
  free (xstrdup_copy);
7487
7488
11.4k
  if (sse_check != check_none
7489
      /* The opcode space check isn't strictly needed; it's there only to
7490
   bypass the logic below when easily possible.  */
7491
0
      && t->opcode_space >= SPACE_0F
7492
0
      && t->opcode_space <= SPACE_0F3A
7493
0
      && !is_cpu (&i.tm, CpuSSE4a)
7494
0
      && !is_any_vex_encoding (t))
7495
0
    {
7496
      /* Some KL and all WideKL insns have only implicit %xmm operands.  */
7497
0
      bool simd = is_cpu (t, CpuKL) || is_cpu (t, CpuWideKL);
7498
7499
0
      for (j = 0; j < t->operands; ++j)
7500
0
  {
7501
0
    if (t->operand_types[j].bitfield.class == RegMMX)
7502
0
      break;
7503
0
    if (t->operand_types[j].bitfield.class == RegSIMD)
7504
0
      simd = true;
7505
0
  }
7506
7507
0
      if (j >= t->operands && simd)
7508
0
  (sse_check == check_warning
7509
0
   ? as_warn
7510
0
   : as_bad) (_("SSE instruction `%s' is used"), insn_name (&i.tm));
7511
0
    }
7512
7513
11.4k
  if (i.tm.opcode_modifier.fwait)
7514
0
    if (!add_prefix (FWAIT_OPCODE))
7515
0
      return;
7516
7517
  /* Check if REP prefix is OK.  */
7518
11.4k
  if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep
7519
0
      && (i.prefix[REP_PREFIX] != REPE_PREFIX_OPCODE
7520
0
    || i.tm.opcode_modifier.prefixok != PrefixRepe))
7521
0
    {
7522
0
      as_bad (_("invalid instruction `%s' after `%s'"),
7523
0
    insn_name (&i.tm), i.rep_prefix);
7524
0
      return;
7525
0
    }
7526
7527
  /* Check for lock without a lockable instruction.  Destination operand
7528
     must be memory unless it is xchg (0x86).  */
7529
11.4k
  if (i.prefix[LOCK_PREFIX])
7530
0
    {
7531
0
      if (i.tm.opcode_modifier.prefixok < PrefixLock
7532
0
    || i.mem_operands == 0
7533
0
    || (i.tm.base_opcode != 0x86
7534
0
        && !(i.flags[i.operands - 1] & Operand_Mem)))
7535
0
  {
7536
0
    as_bad (_("expecting lockable instruction after `lock'"));
7537
0
    return;
7538
0
  }
7539
7540
      /* Zap the redundant prefix from XCHG when optimizing.  */
7541
0
      if (i.tm.base_opcode == 0x86 && optimize && !pp.no_optimize)
7542
0
  i.prefix[LOCK_PREFIX] = 0;
7543
0
    }
7544
7545
11.4k
#ifdef OBJ_ELF
7546
11.4k
  if (i.has_gotrel && tls_check)
7547
8.09k
    {
7548
8.09k
      enum x86_tls_error_type tls_error;
7549
8.09k
      for (j = 0; j < i.operands; ++j)
7550
8.09k
  {
7551
8.09k
    tls_error = x86_check_tls_relocation (i.reloc[j]);
7552
8.09k
    if (tls_error == x86_tls_error_continue)
7553
0
      continue;
7554
7555
8.09k
    if (tls_error != x86_tls_error_none)
7556
6
      x86_report_tls_error (tls_error, i.reloc[j]);
7557
8.09k
    break;
7558
8.09k
  }
7559
8.09k
    }
7560
11.4k
#endif
7561
7562
11.4k
  if ((is_any_vex_encoding (&i.tm) && i.tm.opcode_space != SPACE_MAP4)
7563
11.4k
      || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
7564
11.4k
      || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX
7565
11.4k
      || is_padlock(&i.tm))
7566
3
    {
7567
      /* Check for data size prefix on VEX/XOP/EVEX encoded, SIMD, and
7568
   PadLock insns.  */
7569
3
      if (i.prefix[DATA_PREFIX])
7570
0
  {
7571
0
    as_bad (_("data size prefix invalid with `%s'"), insn_name (&i.tm));
7572
0
    return;
7573
0
  }
7574
3
    }
7575
7576
  /* Check if HLE prefix is OK.  */
7577
11.4k
  if (i.hle_prefix && !check_hle ())
7578
0
    return;
7579
7580
  /* Check BND prefix.  */
7581
11.4k
  if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
7582
0
    as_bad (_("expecting valid branch instruction after `bnd'"));
7583
7584
  /* Check NOTRACK prefix.  */
7585
11.4k
  if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
7586
0
    as_bad (_("expecting indirect branch instruction after `notrack'"));
7587
7588
11.4k
  if (is_cpu (&i.tm, CpuMPX))
7589
0
    {
7590
0
      if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
7591
0
  as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
7592
0
      else if (flag_code != CODE_16BIT
7593
0
         ? i.prefix[ADDR_PREFIX]
7594
0
         : i.mem_operands && !i.prefix[ADDR_PREFIX])
7595
0
  as_bad (_("16-bit address isn't allowed in MPX instructions"));
7596
0
    }
7597
7598
  /* Insert BND prefix.  */
7599
11.4k
  if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
7600
0
    {
7601
0
      if (!i.prefix[BND_PREFIX])
7602
0
  add_prefix (BND_PREFIX_OPCODE);
7603
0
      else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
7604
0
  {
7605
0
    as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
7606
0
    i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
7607
0
  }
7608
0
    }
7609
7610
  /* Check string instruction segment overrides.  */
7611
11.4k
  if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
7612
47
    {
7613
47
      gas_assert (i.mem_operands);
7614
47
      if (!check_string ())
7615
0
  return;
7616
47
      i.disp_operands = 0;
7617
47
    }
7618
7619
  /* The memory operand of (%dx) should be only used with input/output
7620
     instructions (base opcodes: 0x6c, 0x6e, 0xec, 0xee).  */
7621
11.4k
  if (i.input_output_operand
7622
0
      && ((i.tm.base_opcode | 0x82) != 0xee
7623
0
    || i.tm.opcode_space != SPACE_BASE))
7624
0
    {
7625
0
      as_bad (_("input/output port address isn't allowed with `%s'"),
7626
0
        insn_name (&i.tm));
7627
0
      return;
7628
0
    }
7629
7630
11.4k
  if (optimize && !pp.no_optimize && i.tm.opcode_modifier.optimize)
7631
0
    {
7632
0
      if (pp.has_nf)
7633
0
  optimize_nf_encoding ();
7634
0
      optimize_encoding ();
7635
0
    }
7636
7637
  /* Past optimization there's no need to distinguish encoding_evex,
7638
     encoding_evex512, and encoding_egpr anymore.  */
7639
11.4k
  if (pp.encoding == encoding_evex512)
7640
1
    pp.encoding = encoding_evex;
7641
11.4k
  else if (pp.encoding == encoding_egpr)
7642
11
    pp.encoding = is_any_vex_encoding (&i.tm) ? encoding_evex
7643
11
               : encoding_default;
7644
7645
  /* Similarly {nf} can now be taken to imply {evex}.  */
7646
11.4k
  if (pp.has_nf && pp.encoding == encoding_default)
7647
0
    pp.encoding = encoding_evex;
7648
7649
11.4k
  if (use_unaligned_vector_move)
7650
0
    encode_with_unaligned_vector_move ();
7651
7652
11.4k
  if (!process_suffix (t))
7653
608
    return;
7654
7655
  /* Check if IP-relative addressing requirements can be satisfied.  */
7656
10.8k
  if (is_cpu (&i.tm, CpuPREFETCHI)
7657
0
      && !(i.base_reg && i.base_reg->reg_num == RegIP))
7658
0
    as_warn (_("'%s' only supports RIP-relative address"), insn_name (&i.tm));
7659
7660
  /* Update operand types and check extended states.  */
7661
21.9k
  for (j = 0; j < i.operands; j++)
7662
11.1k
    {
7663
11.1k
      enum operand_class class = i.types[j].bitfield.class;
7664
7665
11.1k
      i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
7666
11.1k
      switch (i.tm.operand_types[j].bitfield.class)
7667
11.1k
  {
7668
1.49k
  default:
7669
1.49k
    break;
7670
1.49k
  case RegMMX:
7671
0
    i.xstate |= xstate_mmx;
7672
0
    break;
7673
0
  case RegMask:
7674
0
    i.xstate |= xstate_mask;
7675
0
    break;
7676
0
  case RegSIMD:
7677
0
    if (i.tm.operand_types[j].bitfield.tmmword)
7678
0
      i.xstate |= xstate_tmm;
7679
0
    else if (i.tm.operand_types[j].bitfield.zmmword
7680
0
       && !i.tm.opcode_modifier.vex
7681
0
       && vector_size >= VSZ512)
7682
0
      i.xstate |= xstate_zmm;
7683
0
    else if (i.tm.operand_types[j].bitfield.ymmword
7684
0
       && vector_size >= VSZ256)
7685
0
      i.xstate |= xstate_ymm;
7686
0
    else if (i.tm.operand_types[j].bitfield.xmmword)
7687
0
      i.xstate |= xstate_xmm;
7688
0
    break;
7689
9.64k
  case ClassNone:
7690
9.64k
    i.types[j].bitfield.class = class;
7691
9.64k
    break;
7692
11.1k
  }
7693
11.1k
    }
7694
7695
  /* Make still unresolved immediate matches conform to size of immediate
7696
     given in i.suffix.  */
7697
10.8k
  if (!finalize_imm ())
7698
0
    return;
7699
7700
10.8k
  if (i.types[0].bitfield.imm1)
7701
0
    i.imm_operands = 0; /* kludge for shift insns.  */
7702
7703
  /* For insns with operands there are more diddles to do to the opcode.  */
7704
10.8k
  if (i.operands)
7705
9.77k
    {
7706
9.77k
      if (!process_operands ())
7707
0
  return;
7708
9.77k
    }
7709
1.08k
  else if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
7710
6
    {
7711
      /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc.  */
7712
6
      as_warn (_("translating to `%sp'"), insn_name (&i.tm));
7713
6
    }
7714
7715
10.8k
  if (is_any_vex_encoding (&i.tm))
7716
8
    {
7717
8
      if (!cpu_arch_flags.bitfield.cpui286)
7718
0
  {
7719
0
    as_bad (_("instruction `%s' isn't supported outside of protected mode."),
7720
0
      insn_name (&i.tm));
7721
0
    return;
7722
0
  }
7723
7724
      /* Check for explicit REX prefix.  */
7725
8
      if ((i.prefix[REX_PREFIX]
7726
0
     && (i.tm.opcode_space != SPACE_MAP4
7727
         /* To mimic behavior for legacy insns, permit use of REX64 for promoted
7728
      legacy instructions.  */
7729
0
         || i.prefix[REX_PREFIX] != (REX_OPCODE | REX_W)))
7730
8
    || pp.rex_encoding)
7731
0
  {
7732
0
    as_bad (_("REX prefix invalid with `%s'"), insn_name (&i.tm));
7733
0
    return;
7734
0
  }
7735
7736
      /* Check for explicit REX2 prefix.  */
7737
8
      if (pp.rex2_encoding)
7738
0
  {
7739
0
    as_bad (_("{rex2} prefix invalid with `%s'"), insn_name (&i.tm));
7740
0
    return;
7741
0
  }
7742
7743
8
      if (is_apx_evex_encoding ())
7744
5
  {
7745
5
    if (!build_apx_evex_prefix (false))
7746
0
      return;
7747
5
  }
7748
3
      else if (i.tm.opcode_modifier.vex)
7749
2
  build_vex_prefix (t);
7750
1
      else
7751
1
  build_evex_prefix ();
7752
7753
      /* The individual REX.RXBW bits got consumed.  */
7754
8
      i.rex &= REX_OPCODE;
7755
7756
      /* The rex2 bits got consumed.  */
7757
8
      i.rex2 = 0;
7758
8
    }
7759
7760
  /* Handle conversion of 'int $3' --> special int3 insn.  */
7761
10.8k
  if (i.tm.mnem_off == MN_int
7762
1
      && i.op[0].imms->X_add_number == 3)
7763
0
    {
7764
0
      i.tm.base_opcode = INT3_OPCODE;
7765
0
      i.imm_operands = 0;
7766
0
    }
7767
7768
10.8k
  if ((i.tm.opcode_modifier.jump == JUMP
7769
10.8k
       || i.tm.opcode_modifier.jump == JUMP_BYTE
7770
10.8k
       || i.tm.opcode_modifier.jump == JUMP_DWORD)
7771
211
      && i.op[0].disps->X_op == O_constant)
7772
101
    {
7773
      /* Convert "jmp constant" (and "call constant") to a jump (call) to
7774
   the absolute address given by the constant.  Since ix86 jumps and
7775
   calls are pc relative, we need to generate a reloc.  */
7776
101
      i.op[0].disps->X_add_symbol = &abs_symbol;
7777
101
      i.op[0].disps->X_op = O_symbol;
7778
101
    }
7779
7780
10.8k
  establish_rex ();
7781
7782
10.8k
  insert_lfence_before (last_insn);
7783
7784
  /* We are ready to output the insn.  */
7785
10.8k
  output_insn (last_insn);
7786
7787
10.8k
#ifdef OBJ_ELF
7788
  /* PS: SCFI is enabled only for System V AMD64 ABI.  The ABI check has been
7789
     performed in i386_target_format.  */
7790
10.8k
  if (flag_synth_cfi)
7791
0
    {
7792
0
      ginsnS *ginsn;
7793
0
      ginsn = x86_ginsn_new (symbol_temp_new_now (), frch_ginsn_gen_mode ());
7794
0
      frch_ginsn_data_append (ginsn);
7795
0
    }
7796
10.8k
#endif
7797
7798
10.8k
  insert_lfence_after ();
7799
7800
10.8k
  if (i.tm.opcode_modifier.isprefix)
7801
64
    {
7802
64
      last_insn->kind = last_insn_prefix;
7803
64
      last_insn->name = insn_name (&i.tm);
7804
64
      last_insn->file = as_where (&last_insn->line);
7805
64
    }
7806
10.8k
  else
7807
10.8k
    last_insn->kind = last_insn_other;
7808
10.8k
}
7809
7810
void
7811
md_assemble (char *line)
7812
129k
{
7813
129k
  i386_assemble (line);
7814
129k
  current_templates.start = NULL;
7815
129k
  memset (&pp, 0, sizeof (pp));
7816
129k
}
7817
7818
/* The Q suffix is generally valid only in 64-bit mode, with very few
7819
   exceptions: fild, fistp, fisttp, and cmpxchg8b.  Note that for fild
7820
   and fisttp only one of their two templates is matched below: That's
7821
   sufficient since other relevant attributes are the same between both
7822
   respective templates.  */
7823
static INLINE bool q_suffix_allowed(const insn_template *t)
7824
9.93k
{
7825
9.93k
  return flag_code == CODE_64BIT
7826
9.10k
   || (t->opcode_space == SPACE_BASE
7827
5.85k
       && t->base_opcode == 0xdf
7828
0
       && (t->extension_opcode & 1)) /* fild / fistp / fisttp */
7829
9.10k
   || t->mnem_off == MN_cmpxchg8b;
7830
9.93k
}
7831
7832
static const char *
7833
parse_insn (const char *line, char *mnemonic, enum parse_mode mode)
7834
134k
{
7835
134k
  const char *l = line, *token_start = l;
7836
134k
  char *mnem_p;
7837
134k
  bool pass1 = !current_templates.start;
7838
134k
  int supported;
7839
134k
  const insn_template *t;
7840
134k
  char *dot_p = NULL;
7841
7842
135k
  while (1)
7843
135k
    {
7844
135k
      const char *split;
7845
7846
135k
      mnem_p = mnemonic;
7847
      /* Pseudo-prefixes start with an opening figure brace.  */
7848
135k
      if ((*mnem_p = *l) == '{')
7849
847
  {
7850
847
    ++mnem_p;
7851
847
    ++l;
7852
847
    if (is_whitespace (*l))
7853
2
      ++l;
7854
847
  }
7855
134k
      else if (mode == parse_pseudo_prefix)
7856
533
  break;
7857
371k
      while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
7858
237k
  {
7859
237k
    if (*mnem_p == '.')
7860
6.98k
      dot_p = mnem_p;
7861
237k
    mnem_p++;
7862
237k
    if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
7863
60
      {
7864
60
      too_long:
7865
60
        as_bad (_("no such instruction: `%s'"), token_start);
7866
60
        return NULL;
7867
60
      }
7868
236k
    l++;
7869
236k
  }
7870
134k
      split = l;
7871
134k
      if (is_whitespace (*l))
7872
46.6k
  ++l;
7873
      /* Pseudo-prefixes end with a closing figure brace.  */
7874
134k
      if (*mnemonic == '{' && *l == '}')
7875
591
  {
7876
591
    *mnem_p++ = *l++;
7877
591
    if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
7878
0
      goto too_long;
7879
591
    *mnem_p = '\0';
7880
7881
591
    if (is_whitespace (*l))
7882
476
      ++l;
7883
591
  }
7884
133k
      else if (l == split
7885
87.3k
         && *l != END_OF_INSN
7886
70.9k
         && (intel_syntax
7887
22.2k
       || (*l != PREFIX_SEPARATOR && *l != ',')))
7888
69.2k
  {
7889
69.2k
    if (mode != parse_all)
7890
2.36k
      break;
7891
66.9k
    as_bad (_("invalid character %s in mnemonic"),
7892
66.9k
      output_invalid (*split));
7893
66.9k
    return NULL;
7894
69.2k
  }
7895
65.2k
      if (token_start == l)
7896
32
  {
7897
32
    if (!intel_syntax && *l == PREFIX_SEPARATOR)
7898
1
      as_bad (_("expecting prefix; got nothing"));
7899
31
    else
7900
31
      as_bad (_("expecting mnemonic; got nothing"));
7901
32
    return NULL;
7902
32
  }
7903
7904
      /* Look up instruction (or prefix) via hash table.  */
7905
65.2k
      op_lookup (mnemonic);
7906
7907
65.2k
      if (*l != END_OF_INSN
7908
48.6k
    && current_templates.start
7909
17.6k
    && current_templates.start->opcode_modifier.isprefix)
7910
1.41k
  {
7911
1.41k
    supported = cpu_flags_match (current_templates.start);
7912
1.41k
    if (!(supported & CPU_FLAGS_64BIT_MATCH))
7913
156
      {
7914
156
        as_bad ((flag_code != CODE_64BIT
7915
156
           ? _("`%s' is only supported in 64-bit mode")
7916
156
           : _("`%s' is not supported in 64-bit mode")),
7917
156
          insn_name (current_templates.start));
7918
156
        return NULL;
7919
156
      }
7920
1.26k
    if (supported != CPU_FLAGS_PERFECT_MATCH)
7921
1
      {
7922
1
        as_bad (_("`%s' is not supported on `%s%s'"),
7923
1
          insn_name (current_templates.start),
7924
1
          cpu_arch_name ? cpu_arch_name : default_arch,
7925
1
          cpu_sub_arch_name ? cpu_sub_arch_name : "");
7926
1
        return NULL;
7927
1
      }
7928
    /* If we are in 16-bit mode, do not allow addr16 or data16.
7929
       Similarly, in 32-bit mode, do not allow addr32 or data32.  */
7930
1.26k
    if ((current_templates.start->opcode_modifier.size == SIZE16
7931
700
         || current_templates.start->opcode_modifier.size == SIZE32)
7932
563
        && flag_code != CODE_64BIT
7933
532
        && ((current_templates.start->opcode_modifier.size == SIZE32)
7934
532
      ^ (flag_code == CODE_16BIT)))
7935
530
      {
7936
530
        as_bad (_("redundant %s prefix"),
7937
530
          insn_name (current_templates.start));
7938
530
        return NULL;
7939
530
      }
7940
7941
731
    if (current_templates.start->base_opcode == PSEUDO_PREFIX)
7942
574
      {
7943
        /* Handle pseudo prefixes.  */
7944
574
        switch (current_templates.start->extension_opcode)
7945
574
    {
7946
0
    case Prefix_Disp8:
7947
      /* {disp8} */
7948
0
      pp.disp_encoding = disp_encoding_8bit;
7949
0
      break;
7950
14
    case Prefix_Disp16:
7951
      /* {disp16} */
7952
14
      pp.disp_encoding = disp_encoding_16bit;
7953
14
      break;
7954
1
    case Prefix_Disp32:
7955
      /* {disp32} */
7956
1
      pp.disp_encoding = disp_encoding_32bit;
7957
1
      break;
7958
0
    case Prefix_Load:
7959
      /* {load} */
7960
0
      pp.dir_encoding = dir_encoding_load;
7961
0
      break;
7962
20
    case Prefix_Store:
7963
      /* {store} */
7964
20
      pp.dir_encoding = dir_encoding_store;
7965
20
      break;
7966
41
    case Prefix_VEX:
7967
      /* {vex} */
7968
41
      pp.encoding = encoding_vex;
7969
41
      break;
7970
0
    case Prefix_VEX3:
7971
      /* {vex3} */
7972
0
      pp.encoding = encoding_vex3;
7973
0
      break;
7974
451
    case Prefix_EVEX:
7975
      /* {evex} */
7976
451
      pp.encoding = encoding_evex;
7977
451
      break;
7978
41
    case Prefix_REX:
7979
      /* {rex} */
7980
41
      pp.rex_encoding = true;
7981
41
      break;
7982
2
    case Prefix_REX2:
7983
      /* {rex2} */
7984
2
      pp.rex2_encoding = true;
7985
2
      break;
7986
3
    case Prefix_NF:
7987
      /* {nf} */
7988
3
      pp.has_nf = true;
7989
3
      break;
7990
0
    case Prefix_NoOptimize:
7991
      /* {nooptimize} */
7992
0
      pp.no_optimize = true;
7993
0
      break;
7994
1
    case Prefix_NoImm8s:
7995
      /* {noimm8s} */
7996
1
      pp.no_imm8s = true;
7997
1
      break;
7998
0
    default:
7999
0
      abort ();
8000
574
    }
8001
574
        if (pp.has_nf
8002
3
      && pp.encoding != encoding_default
8003
0
      && pp.encoding != encoding_evex)
8004
0
    {
8005
0
      as_bad (_("{nf} cannot be combined with {vex}/{vex3}"));
8006
0
      return NULL;
8007
0
    }
8008
574
      }
8009
157
    else
8010
157
      {
8011
        /* Add prefix, checking for repeated prefixes.  */
8012
157
        switch (add_prefix (current_templates.start->base_opcode))
8013
157
    {
8014
7
    case PREFIX_EXIST:
8015
7
      return NULL;
8016
1
    case PREFIX_DS:
8017
1
      if (is_cpu (current_templates.start, CpuIBT))
8018
0
        i.notrack_prefix = insn_name (current_templates.start);
8019
1
      break;
8020
5
    case PREFIX_REP:
8021
5
      if (is_cpu (current_templates.start, CpuHLE))
8022
0
        i.hle_prefix = insn_name (current_templates.start);
8023
5
      else if (is_cpu (current_templates.start, CpuMPX))
8024
0
        i.bnd_prefix = insn_name (current_templates.start);
8025
5
      else
8026
5
        i.rep_prefix = insn_name (current_templates.start);
8027
5
      break;
8028
144
    default:
8029
144
      break;
8030
157
    }
8031
157
      }
8032
    /* Skip past PREFIX_SEPARATOR and reset token_start.  */
8033
724
    l += (!intel_syntax && *l == PREFIX_SEPARATOR);
8034
724
    if (is_whitespace (*l))
8035
10
      ++l;
8036
724
    token_start = l;
8037
724
  }
8038
63.7k
      else
8039
63.7k
  break;
8040
65.2k
    }
8041
8042
66.6k
  if (mode != parse_all)
8043
4.06k
    return token_start;
8044
8045
62.6k
  if (!current_templates.start)
8046
45.1k
    {
8047
#ifdef TE_SOLARIS
8048
      /* Sun specifies an alternative form for CMOVcc: Size suffix (if any)
8049
   first, then a dot, then the condition code mnemonic.  */
8050
      if ((mnemonic + 4 == dot_p && !memcmp (mnemonic, "cmov", 4))
8051
    /* While doc doesn't say so, gcc assumes it: Same for FCMOVcc,
8052
       except that there's no size suffix to care about.  */
8053
    || (mnemonic + 5 == dot_p && !memcmp (mnemonic, "fcmov", 5)))
8054
  {
8055
    /* Simply strip the dot.  */
8056
    memmove (dot_p, dot_p + 1, mnem_p - dot_p);
8057
    dot_p = mnem_p - 1;
8058
  }
8059
      else if (!intel_syntax
8060
         && mnemonic + 5 == dot_p
8061
         && !memcmp (mnemonic, "cmov", 4)
8062
         && strchr ("lqw", TOLOWER (dot_p[-1])))
8063
  {
8064
    /* Strip the dot, while moving the suffix.  */
8065
    char suffix = dot_p[-1];
8066
8067
    memmove (dot_p - 1, dot_p + 1, mnem_p - dot_p);
8068
    mnem_p[-2] = suffix;
8069
    dot_p = mnem_p - 1;
8070
  }
8071
      else
8072
#endif
8073
      /* Deprecated functionality (new code should use pseudo-prefixes instead):
8074
   Check if we should swap operand or force 32bit displacement in
8075
   encoding.  */
8076
45.1k
      if (mnem_p - 2 == dot_p && dot_p[1] == 's')
8077
251
  {
8078
251
    if (pp.dir_encoding == dir_encoding_default)
8079
251
      pp.dir_encoding = dir_encoding_swap;
8080
0
    else
8081
0
      as_warn (_("ignoring `.s' suffix due to earlier `{%s}'"),
8082
0
         pp.dir_encoding == dir_encoding_load ? "load" : "store");
8083
251
  }
8084
44.9k
      else if (mnem_p - 3 == dot_p
8085
49
         && dot_p[1] == 'd'
8086
20
         && dot_p[2] == '8')
8087
17
  {
8088
17
    if (pp.disp_encoding == disp_encoding_default)
8089
17
      pp.disp_encoding = disp_encoding_8bit;
8090
0
    else if (pp.disp_encoding != disp_encoding_8bit)
8091
0
      as_warn (_("ignoring `.d8' suffix due to earlier `{disp<N>}'"));
8092
17
  }
8093
44.8k
      else if (mnem_p - 4 == dot_p
8094
22
         && dot_p[1] == 'd'
8095
0
         && dot_p[2] == '3'
8096
0
         && dot_p[3] == '2')
8097
0
  {
8098
0
    if (pp.disp_encoding == disp_encoding_default)
8099
0
      pp.disp_encoding = disp_encoding_32bit;
8100
0
    else if (pp.disp_encoding != disp_encoding_32bit)
8101
0
      as_warn (_("ignoring `.d32' suffix due to earlier `{disp<N>}'"));
8102
0
  }
8103
44.8k
      else
8104
44.8k
  goto check_suffix;
8105
268
      mnem_p = dot_p;
8106
268
      *dot_p = '\0';
8107
268
      op_lookup (mnemonic);
8108
268
    }
8109
8110
17.7k
  if (!current_templates.start || !pass1)
8111
969
    {
8112
969
      current_templates.start = NULL;
8113
8114
45.8k
    check_suffix:
8115
45.8k
      if (mnem_p > mnemonic)
8116
45.8k
  {
8117
    /* See if we can get a match by trimming off a suffix.  */
8118
45.8k
    switch (mnem_p[-1])
8119
45.8k
      {
8120
657
      case WORD_MNEM_SUFFIX:
8121
657
        if (intel_syntax && (intel_float_operand (mnemonic) & 2))
8122
1
    i.suffix = SHORT_MNEM_SUFFIX;
8123
656
        else
8124
    /* Fall through.  */
8125
5.59k
        case BYTE_MNEM_SUFFIX:
8126
6.70k
        case QWORD_MNEM_SUFFIX:
8127
6.70k
    i.suffix = mnem_p[-1];
8128
6.70k
        mnem_p[-1] = '\0';
8129
6.70k
        op_lookup (mnemonic);
8130
6.70k
        break;
8131
2.05k
      case SHORT_MNEM_SUFFIX:
8132
2.35k
      case LONG_MNEM_SUFFIX:
8133
2.35k
        if (!intel_syntax)
8134
723
    {
8135
723
      i.suffix = mnem_p[-1];
8136
723
      mnem_p[-1] = '\0';
8137
723
      op_lookup (mnemonic);
8138
723
    }
8139
2.35k
        break;
8140
8141
        /* Intel Syntax.  */
8142
159
      case 'd':
8143
159
        if (intel_syntax)
8144
90
    {
8145
90
      if (intel_float_operand (mnemonic) == 1)
8146
2
        i.suffix = SHORT_MNEM_SUFFIX;
8147
88
      else
8148
88
        i.suffix = LONG_MNEM_SUFFIX;
8149
90
      mnem_p[-1] = '\0';
8150
90
      op_lookup (mnemonic);
8151
90
    }
8152
        /* For compatibility reasons accept MOVSD and CMPSD without
8153
           operands even in AT&T mode.  */
8154
69
        else if (*l == END_OF_INSN)
8155
61
    {
8156
61
      mnem_p[-1] = '\0';
8157
61
      op_lookup (mnemonic);
8158
61
      if (current_templates.start != NULL
8159
          /* MOVS or CMPS */
8160
5
          && (current_templates.start->base_opcode | 2) == 0xa6
8161
1
          && current_templates.start->opcode_space
8162
1
       == SPACE_BASE
8163
1
          && mnem_p[-2] == 's')
8164
0
        {
8165
0
          as_warn (_("found `%sd'; assuming `%sl' was meant"),
8166
0
             mnemonic, mnemonic);
8167
0
          i.suffix = LONG_MNEM_SUFFIX;
8168
0
        }
8169
61
      else
8170
61
        {
8171
61
          current_templates.start = NULL;
8172
61
          mnem_p[-1] = 'd';
8173
61
        }
8174
61
    }
8175
159
        break;
8176
45.8k
      }
8177
45.8k
  }
8178
8179
45.8k
      if (!current_templates.start)
8180
40.3k
  {
8181
40.3k
    if (pass1)
8182
40.2k
      as_bad (_("no such instruction: `%s'"), token_start);
8183
40.3k
    return NULL;
8184
40.3k
  }
8185
45.8k
    }
8186
8187
  /* Handle SCC OSZC flgs.  */
8188
22.3k
  if (current_templates.start->opcode_modifier.operandconstraint == SCC)
8189
17
    {
8190
17
      int length = check_Scc_OszcOperations (l);
8191
17
      if (length < 0)
8192
10
  return NULL;
8193
7
      l += length;
8194
7
    }
8195
8196
22.3k
  if ((current_templates.start->opcode_modifier.jump == JUMP
8197
21.6k
       || current_templates.start->opcode_modifier.jump == JUMP_BYTE)
8198
652
      && *l == ',')
8199
47
    {
8200
      /* Check for a branch hint.  We allow ",pt" and ",pn" for
8201
   predict taken and predict not taken respectively.
8202
   I'm not sure that branch hints actually do anything on loop
8203
   and jcxz insns (JumpByte) for current Pentium4 chips.  They
8204
   may work in the future and it doesn't hurt to accept them
8205
   now.  */
8206
47
      token_start = l++;
8207
47
      if (is_whitespace (*l))
8208
0
  ++l;
8209
47
      if (TOLOWER (*l) == 'p' && ISALPHA (l[1])
8210
33
    && (l[2] == END_OF_INSN || is_whitespace (l[2])))
8211
0
  {
8212
0
    if (TOLOWER (l[1]) == 't')
8213
0
      {
8214
0
        if (!add_prefix (DS_PREFIX_OPCODE))
8215
0
    return NULL;
8216
0
        l += 2;
8217
0
      }
8218
0
    else if (TOLOWER (l[1]) == 'n')
8219
0
      {
8220
0
        if (!add_prefix (CS_PREFIX_OPCODE))
8221
0
    return NULL;
8222
0
        l += 2;
8223
0
      }
8224
0
    else
8225
0
      l = token_start;
8226
0
  }
8227
47
      else
8228
47
  l = token_start;
8229
47
    }
8230
  /* Any other comma loses.  */
8231
22.3k
  if (*l == ',')
8232
326
    {
8233
326
      as_bad (_("invalid character %s in mnemonic"),
8234
326
        output_invalid (*l));
8235
326
      return NULL;
8236
326
    }
8237
8238
  /* Check if instruction is supported on specified architecture.  */
8239
21.9k
  supported = 0;
8240
44.6k
  for (t = current_templates.start; t < current_templates.end; ++t)
8241
43.8k
    {
8242
43.8k
      supported |= cpu_flags_match (t);
8243
8244
43.8k
      if (i.suffix == QWORD_MNEM_SUFFIX && !q_suffix_allowed (t))
8245
9.10k
  supported &= ~CPU_FLAGS_64BIT_MATCH;
8246
8247
43.8k
      if (supported == CPU_FLAGS_PERFECT_MATCH)
8248
21.2k
  return l;
8249
43.8k
    }
8250
8251
785
  if (pass1)
8252
135
    {
8253
135
      if (supported & CPU_FLAGS_64BIT_MATCH)
8254
121
        i.error = unsupported_on_arch;
8255
14
      else
8256
14
        i.error = unsupported_64bit;
8257
135
    }
8258
8259
785
  return NULL;
8260
21.9k
}
8261
8262
static char *
8263
parse_operands (char *l, const char *mnemonic)
8264
21.3k
{
8265
21.3k
  char *token_start;
8266
8267
  /* 1 if operand is pending after ','.  */
8268
21.3k
  unsigned int expecting_operand = 0;
8269
8270
39.3k
  while (*l != END_OF_INSN)
8271
23.5k
    {
8272
      /* Non-zero if operand parens not balanced.  */
8273
23.5k
      unsigned int paren_not_balanced = 0;
8274
      /* True if inside double quotes.  */
8275
23.5k
      bool in_quotes = false;
8276
8277
      /* Skip optional white space before operand.  */
8278
23.5k
      if (is_whitespace (*l))
8279
95
  ++l;
8280
23.5k
      if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
8281
540
  {
8282
540
    as_bad (_("invalid character %s before operand %d"),
8283
540
      output_invalid (*l),
8284
540
      i.operands + 1);
8285
540
    return NULL;
8286
540
  }
8287
23.0k
      token_start = l;  /* After white space.  */
8288
591k
      while (in_quotes || paren_not_balanced || *l != ',')
8289
586k
  {
8290
586k
    if (*l == END_OF_INSN)
8291
17.7k
      {
8292
17.7k
        if (in_quotes)
8293
310
    {
8294
310
      as_bad (_("unbalanced double quotes in operand %d."),
8295
310
        i.operands + 1);
8296
310
      return NULL;
8297
310
    }
8298
17.4k
        if (paren_not_balanced)
8299
379
    {
8300
379
      know (!intel_syntax);
8301
379
      as_bad (_("unbalanced parenthesis in operand %d."),
8302
379
        i.operands + 1);
8303
379
      return NULL;
8304
379
    }
8305
17.0k
        else
8306
17.0k
    break; /* we are done */
8307
17.4k
      }
8308
569k
    else if (*l == '\\' && l[1] == '"')
8309
0
      ++l;
8310
569k
    else if (*l == '"')
8311
2.32k
      in_quotes = !in_quotes;
8312
566k
    else if (!in_quotes && !is_operand_char (*l) && !is_whitespace (*l))
8313
964
      {
8314
964
        as_bad (_("invalid character %s in operand %d"),
8315
964
          output_invalid (*l),
8316
964
          i.operands + 1);
8317
964
        return NULL;
8318
964
      }
8319
568k
    if (!intel_syntax && !in_quotes)
8320
417k
      {
8321
417k
        if (*l == '(')
8322
527
    ++paren_not_balanced;
8323
417k
        if (*l == ')')
8324
145
    --paren_not_balanced;
8325
417k
      }
8326
568k
    l++;
8327
568k
  }
8328
21.4k
      if (l != token_start)
8329
21.3k
  {     /* Yes, we've read in another operand.  */
8330
21.3k
    unsigned int operand_ok;
8331
21.3k
    this_operand = i.operands++;
8332
21.3k
    if (i.operands > MAX_OPERANDS)
8333
0
      {
8334
0
        as_bad (_("spurious operands; (%d operands/instruction max)"),
8335
0
          MAX_OPERANDS);
8336
0
        return NULL;
8337
0
      }
8338
21.3k
    i.types[this_operand].bitfield.unspecified = 1;
8339
    /* Now parse operand adding info to 'i' as we go along.  */
8340
21.3k
    END_STRING_AND_SAVE (l);
8341
8342
21.3k
    if (i.mem_operands > 1)
8343
23
      {
8344
23
        as_bad (_("too many memory references for `%s'"),
8345
23
          mnemonic);
8346
23
        return 0;
8347
23
      }
8348
8349
21.3k
    if (intel_syntax)
8350
7.05k
      operand_ok =
8351
7.05k
        i386_intel_operand (token_start,
8352
7.05k
          intel_float_operand (mnemonic));
8353
14.3k
    else
8354
14.3k
      operand_ok = i386_att_operand (token_start);
8355
8356
21.3k
    RESTORE_END_STRING (l);
8357
21.3k
    if (!operand_ok)
8358
3.42k
      return NULL;
8359
21.3k
  }
8360
15
      else
8361
15
  {
8362
15
    if (expecting_operand)
8363
14
      {
8364
17
      expecting_operand_after_comma:
8365
17
        as_bad (_("expecting operand after ','; got nothing"));
8366
17
        return NULL;
8367
14
      }
8368
1
    if (*l == ',')
8369
0
      {
8370
0
        as_bad (_("expecting operand before ','; got nothing"));
8371
0
        return NULL;
8372
0
      }
8373
1
  }
8374
8375
      /* Now *l must be either ',' or END_OF_INSN.  */
8376
17.9k
      if (*l == ',')
8377
3.47k
  {
8378
3.47k
    if (*++l == END_OF_INSN)
8379
3
      {
8380
        /* Just skip it, if it's \n complain.  */
8381
3
        goto expecting_operand_after_comma;
8382
3
      }
8383
3.47k
    expecting_operand = 1;
8384
3.47k
  }
8385
17.9k
    }
8386
15.7k
  return l;
8387
21.3k
}
8388
8389
static void
8390
copy_operand (unsigned int to, unsigned int from)
8391
0
{
8392
0
  i.types[to] = i.types[from];
8393
0
  i.tm.operand_types[to] = i.tm.operand_types[from];
8394
0
  i.flags[to] = i.flags[from];
8395
0
  i.op[to] = i.op[from];
8396
0
  i.reloc[to] = i.reloc[from];
8397
0
  i.imm_bits[to] = i.imm_bits[from];
8398
  /* Note: i.mask and i.broadcast aren't handled here, as what (if
8399
     anything) to do there depends on context.  */
8400
0
}
8401
8402
static void
8403
swap_2_operands (unsigned int xchg1, unsigned int xchg2)
8404
1.01k
{
8405
1.01k
  union i386_op temp_op;
8406
1.01k
  i386_operand_type temp_type;
8407
1.01k
  unsigned int temp_flags;
8408
1.01k
  enum bfd_reloc_code_real temp_reloc;
8409
8410
1.01k
  temp_type = i.types[xchg2];
8411
1.01k
  i.types[xchg2] = i.types[xchg1];
8412
1.01k
  i.types[xchg1] = temp_type;
8413
8414
1.01k
  temp_flags = i.flags[xchg2];
8415
1.01k
  i.flags[xchg2] = i.flags[xchg1];
8416
1.01k
  i.flags[xchg1] = temp_flags;
8417
8418
1.01k
  temp_op = i.op[xchg2];
8419
1.01k
  i.op[xchg2] = i.op[xchg1];
8420
1.01k
  i.op[xchg1] = temp_op;
8421
8422
1.01k
  temp_reloc = i.reloc[xchg2];
8423
1.01k
  i.reloc[xchg2] = i.reloc[xchg1];
8424
1.01k
  i.reloc[xchg1] = temp_reloc;
8425
8426
1.01k
  temp_flags = i.imm_bits[xchg2];
8427
1.01k
  i.imm_bits[xchg2] = i.imm_bits[xchg1];
8428
1.01k
  i.imm_bits[xchg1] = temp_flags;
8429
8430
1.01k
  if (i.mask.reg)
8431
0
    {
8432
0
      if (i.mask.operand == xchg1)
8433
0
  i.mask.operand = xchg2;
8434
0
      else if (i.mask.operand == xchg2)
8435
0
  i.mask.operand = xchg1;
8436
0
    }
8437
1.01k
  if (i.broadcast.type || i.broadcast.bytes)
8438
0
    {
8439
0
      if (i.broadcast.operand == xchg1)
8440
0
  i.broadcast.operand = xchg2;
8441
0
      else if (i.broadcast.operand == xchg2)
8442
0
  i.broadcast.operand = xchg1;
8443
0
    }
8444
1.01k
}
8445
8446
static void
8447
swap_operands (void)
8448
1.01k
{
8449
1.01k
  switch (i.operands)
8450
1.01k
    {
8451
0
    case 5:
8452
0
    case 4:
8453
0
      swap_2_operands (1, i.operands - 2);
8454
      /* Fall through.  */
8455
1
    case 3:
8456
1.01k
    case 2:
8457
1.01k
      swap_2_operands (0, i.operands - 1);
8458
1.01k
      break;
8459
0
    default:
8460
0
      abort ();
8461
1.01k
    }
8462
8463
1.01k
  if (i.mem_operands == 2)
8464
18
    {
8465
18
      const reg_entry *temp_seg;
8466
18
      temp_seg = i.seg[0];
8467
18
      i.seg[0] = i.seg[1];
8468
18
      i.seg[1] = temp_seg;
8469
18
    }
8470
1.01k
}
8471
8472
/* Try to ensure constant immediates are represented in the smallest
8473
   opcode possible.  */
8474
static void
8475
optimize_imm (void)
8476
3.61k
{
8477
3.61k
  char guess_suffix = 0;
8478
3.61k
  int op;
8479
8480
3.61k
  if (i.suffix)
8481
1.47k
    guess_suffix = i.suffix;
8482
2.13k
  else if (i.reg_operands)
8483
87
    {
8484
      /* Figure out a suffix from the last register operand specified.
8485
   We can't do this properly yet, i.e. excluding special register
8486
   instances, but the following works for instructions with
8487
   immediates.  In any case, we can't set i.suffix yet.  */
8488
90
      for (op = i.operands; --op >= 0;)
8489
89
  if (i.types[op].bitfield.class != Reg)
8490
3
    continue;
8491
86
  else if (i.types[op].bitfield.byte)
8492
1
    {
8493
1
      guess_suffix = BYTE_MNEM_SUFFIX;
8494
1
      break;
8495
1
    }
8496
85
  else if (i.types[op].bitfield.word)
8497
14
    {
8498
14
      guess_suffix = WORD_MNEM_SUFFIX;
8499
14
      break;
8500
14
    }
8501
71
  else if (i.types[op].bitfield.dword)
8502
71
    {
8503
71
      guess_suffix = LONG_MNEM_SUFFIX;
8504
71
      break;
8505
71
    }
8506
0
  else if (i.types[op].bitfield.qword)
8507
0
    {
8508
0
      guess_suffix = QWORD_MNEM_SUFFIX;
8509
0
      break;
8510
0
    }
8511
87
    }
8512
2.05k
  else if ((flag_code == CODE_16BIT)
8513
2.05k
      ^ (i.prefix[DATA_PREFIX] != 0 && !(i.prefix[REX_PREFIX] & REX_W)))
8514
837
    guess_suffix = WORD_MNEM_SUFFIX;
8515
1.21k
  else if (flag_code != CODE_64BIT
8516
1.21k
     || (!(i.prefix[REX_PREFIX] & REX_W)
8517
         /* A more generic (but also more involved) way of dealing
8518
      with the special case(s) would be to go look for
8519
      DefaultSize attributes on any of the templates.  */
8520
1.21k
         && current_templates.start->mnem_off != MN_push
8521
1.20k
         && current_templates.start->mnem_off != MN_jmpabs))
8522
1.21k
    guess_suffix = LONG_MNEM_SUFFIX;
8523
8524
7.43k
  for (op = i.imm_operands; --op >= 0;)
8525
3.82k
    if (operand_type_check (i.types[op], imm))
8526
3.62k
      {
8527
3.62k
  switch (i.op[op].imms->X_op)
8528
3.62k
    {
8529
2.26k
    case O_constant:
8530
      /* If a suffix is given, this operand may be shortened.  */
8531
2.26k
      switch (guess_suffix)
8532
2.26k
        {
8533
1.25k
        case LONG_MNEM_SUFFIX:
8534
1.25k
    i.types[op].bitfield.imm32 = 1;
8535
1.25k
    i.types[op].bitfield.imm64 = 1;
8536
1.25k
    break;
8537
359
        case WORD_MNEM_SUFFIX:
8538
359
    i.types[op].bitfield.imm16 = 1;
8539
359
    i.types[op].bitfield.imm32 = 1;
8540
359
    i.types[op].bitfield.imm32s = 1;
8541
359
    i.types[op].bitfield.imm64 = 1;
8542
359
    break;
8543
646
        case BYTE_MNEM_SUFFIX:
8544
646
    i.types[op].bitfield.imm8 = 1;
8545
646
    i.types[op].bitfield.imm8s = 1;
8546
646
    i.types[op].bitfield.imm16 = 1;
8547
646
    i.types[op].bitfield.imm32 = 1;
8548
646
    i.types[op].bitfield.imm32s = 1;
8549
646
    i.types[op].bitfield.imm64 = 1;
8550
646
    break;
8551
2.26k
        }
8552
8553
      /* If this operand is at most 16 bits, convert it
8554
         to a signed 16 bit number before trying to see
8555
         whether it will fit in an even smaller size.
8556
         This allows a 16-bit operand such as $0xffe0 to
8557
         be recognised as within Imm8S range.  */
8558
2.26k
      if ((i.types[op].bitfield.imm16)
8559
1.00k
    && fits_in_unsigned_word (i.op[op].imms->X_add_number))
8560
408
        {
8561
408
    i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
8562
408
            ^ 0x8000) - 0x8000);
8563
408
        }
8564
2.26k
#ifdef BFD64
8565
      /* Store 32-bit immediate in 64-bit for 64-bit BFD.  */
8566
2.26k
      if ((i.types[op].bitfield.imm32)
8567
2.25k
    && fits_in_unsigned_long (i.op[op].imms->X_add_number))
8568
1.72k
        {
8569
1.72k
    i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
8570
1.72k
            ^ ((offsetT) 1 << 31))
8571
1.72k
                 - ((offsetT) 1 << 31));
8572
1.72k
        }
8573
2.26k
#endif
8574
2.26k
      i.types[op]
8575
2.26k
        = operand_type_or (i.types[op],
8576
2.26k
         smallest_imm_type (i.op[op].imms->X_add_number));
8577
8578
      /* We must avoid matching of Imm32 templates when 64bit
8579
         only immediate is available.  */
8580
2.26k
      if (guess_suffix == QWORD_MNEM_SUFFIX)
8581
2
        i.types[op].bitfield.imm32 = 0;
8582
2.26k
      break;
8583
8584
0
    case O_absent:
8585
0
    case O_register:
8586
0
      abort ();
8587
8588
      /* Symbols and expressions.  */
8589
1.36k
    default:
8590
      /* Convert symbolic operand to proper sizes for matching, but don't
8591
         prevent matching a set of insns that only supports sizes other
8592
         than those matching the insn suffix.  */
8593
1.36k
      {
8594
1.36k
        i386_operand_type mask, allowed;
8595
1.36k
        const insn_template *t = current_templates.start;
8596
8597
1.36k
        operand_type_set (&mask, 0);
8598
1.36k
        switch (guess_suffix)
8599
1.36k
    {
8600
0
    case QWORD_MNEM_SUFFIX:
8601
0
      mask.bitfield.imm64 = 1;
8602
0
      mask.bitfield.imm32s = 1;
8603
0
      break;
8604
33
    case LONG_MNEM_SUFFIX:
8605
33
      mask.bitfield.imm32 = 1;
8606
33
      break;
8607
526
    case WORD_MNEM_SUFFIX:
8608
526
      mask.bitfield.imm16 = 1;
8609
526
      break;
8610
803
    case BYTE_MNEM_SUFFIX:
8611
803
      mask.bitfield.imm8 = 1;
8612
803
      break;
8613
4
    default:
8614
4
      break;
8615
1.36k
    }
8616
8617
1.36k
        allowed = operand_type_and (t->operand_types[op], mask);
8618
13.5k
        while (++t < current_templates.end)
8619
12.1k
    {
8620
12.1k
      allowed = operand_type_or (allowed, t->operand_types[op]);
8621
12.1k
      allowed = operand_type_and (allowed, mask);
8622
12.1k
    }
8623
8624
1.36k
        if (!operand_type_all_zero (&allowed))
8625
1.33k
    i.types[op] = operand_type_and (i.types[op], mask);
8626
1.36k
      }
8627
0
      break;
8628
3.62k
    }
8629
3.62k
      }
8630
3.61k
}
8631
8632
/* Try to use the smallest displacement type too.  */
8633
static bool
8634
optimize_disp (const insn_template *t)
8635
12.3k
{
8636
12.3k
  unsigned int op;
8637
8638
12.3k
  if (!want_disp32 (t)
8639
10.3k
      && (!t->opcode_modifier.jump
8640
225
    || i.jumpabsolute || i.types[0].bitfield.baseindex))
8641
10.1k
    {
8642
20.6k
      for (op = i.imm_operands; op < i.operands; ++op)
8643
10.5k
  {
8644
10.5k
    const expressionS *exp = i.op[op].disps;
8645
8646
10.5k
    if (!operand_type_check (i.types[op], disp))
8647
692
      continue;
8648
8649
9.81k
    if (exp->X_op != O_constant)
8650
8.78k
      continue;
8651
8652
    /* Since displacement is signed extended to 64bit, don't allow
8653
       disp32 if it is out of range.  */
8654
1.03k
    if (fits_in_signed_long (exp->X_add_number))
8655
1.03k
      continue;
8656
8657
2
    i.types[op].bitfield.disp32 = 0;
8658
2
    if (i.types[op].bitfield.baseindex)
8659
0
      {
8660
0
        as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
8661
0
          (uint64_t) exp->X_add_number);
8662
0
        return false;
8663
0
      }
8664
2
  }
8665
10.1k
    }
8666
8667
  /* Don't optimize displacement for movabs / jmpabs since they only take
8668
     64-bit displacement.  */
8669
12.3k
  if (pp.disp_encoding > disp_encoding_8bit
8670
12.3k
      || t->mnem_off == MN_movabs || t->mnem_off == MN_jmpabs)
8671
3
    return true;
8672
8673
27.2k
  for (op = i.operands; op-- > 0;)
8674
14.9k
    if (operand_type_check (i.types[op], disp))
8675
12.4k
      {
8676
12.4k
  if (i.op[op].disps->X_op == O_constant)
8677
1.41k
    {
8678
1.41k
      offsetT op_disp = i.op[op].disps->X_add_number;
8679
8680
1.41k
      if (!op_disp && i.types[op].bitfield.baseindex)
8681
0
        {
8682
0
    i.types[op] = operand_type_and_not (i.types[op], anydisp);
8683
0
    i.op[op].disps = NULL;
8684
0
    i.disp_operands--;
8685
0
    continue;
8686
0
        }
8687
8688
1.41k
      if (i.types[op].bitfield.disp16
8689
4
    && fits_in_unsigned_word (op_disp))
8690
3
        {
8691
    /* If this operand is at most 16 bits, convert
8692
       to a signed 16 bit number and don't use 64bit
8693
       displacement.  */
8694
3
    op_disp = ((op_disp ^ 0x8000) - 0x8000);
8695
3
    i.types[op].bitfield.disp64 = 0;
8696
3
        }
8697
8698
1.41k
#ifdef BFD64
8699
      /* Optimize 64-bit displacement to 32-bit for 64-bit BFD.  */
8700
1.41k
      if ((flag_code != CODE_64BIT
8701
1.41k
     ? i.types[op].bitfield.disp32
8702
1.41k
     : want_disp32 (t)
8703
2
       && (!t->opcode_modifier.jump
8704
2
           || i.jumpabsolute || i.types[op].bitfield.baseindex))
8705
11
    && fits_in_unsigned_long (op_disp))
8706
9
        {
8707
    /* If this operand is at most 32 bits, convert
8708
       to a signed 32 bit number and don't use 64bit
8709
       displacement.  */
8710
9
    op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
8711
9
    i.types[op].bitfield.disp64 = 0;
8712
9
    i.types[op].bitfield.disp32 = 1;
8713
9
        }
8714
8715
1.41k
      if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
8716
1.39k
        {
8717
1.39k
    i.types[op].bitfield.disp64 = 0;
8718
1.39k
    i.types[op].bitfield.disp32 = 1;
8719
1.39k
        }
8720
1.41k
#endif
8721
1.41k
      if ((i.types[op].bitfield.disp32
8722
5
     || i.types[op].bitfield.disp16)
8723
1.41k
    && fits_in_disp8 (op_disp))
8724
1.17k
        i.types[op].bitfield.disp8 = 1;
8725
8726
1.41k
      i.op[op].disps->X_add_number = op_disp;
8727
1.41k
    }
8728
11.0k
  else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8729
11.0k
     || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
8730
0
    {
8731
0
      fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
8732
0
       i.op[op].disps, 0, i.reloc[op]);
8733
0
      i.types[op] = operand_type_and_not (i.types[op], anydisp);
8734
0
    }
8735
11.0k
  else
8736
    /* We only support 64bit displacement on constants.  */
8737
11.0k
    i.types[op].bitfield.disp64 = 0;
8738
12.4k
      }
8739
8740
12.2k
  return true;
8741
12.3k
}
8742
8743
/* Return 1 if there is a match in broadcast bytes between operand
8744
   GIVEN and instruction template T.   */
8745
8746
static INLINE int
8747
match_broadcast_size (const insn_template *t, unsigned int given)
8748
0
{
8749
0
  return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
8750
0
     && i.types[given].bitfield.byte)
8751
0
    || (t->opcode_modifier.broadcast == WORD_BROADCAST
8752
0
        && i.types[given].bitfield.word)
8753
0
    || (t->opcode_modifier.broadcast == DWORD_BROADCAST
8754
0
        && i.types[given].bitfield.dword)
8755
0
    || (t->opcode_modifier.broadcast == QWORD_BROADCAST
8756
0
        && i.types[given].bitfield.qword));
8757
0
}
8758
8759
/* Check if operands are valid for the instruction.  */
8760
8761
static int
8762
check_VecOperands (const insn_template *t)
8763
10.3k
{
8764
10.3k
  unsigned int op;
8765
10.3k
  i386_cpu_flags cpu;
8766
8767
  /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
8768
     any one operand are implicity requiring AVX512VL support if the actual
8769
     operand size is YMMword or XMMword.  Since this function runs after
8770
     template matching, there's no need to check for YMMword/XMMword in
8771
     the template.  */
8772
10.3k
  cpu = cpu_flags_and (cpu_flags_from_attr (t->cpu), avx512);
8773
10.3k
  if (!cpu_flags_all_zero (&cpu)
8774
0
      && !is_cpu (t, CpuAVX512VL)
8775
0
      && !cpu_arch_flags.bitfield.cpuavx512vl
8776
0
      && (!t->opcode_modifier.vex || need_evex_encoding (t)))
8777
0
    {
8778
0
      for (op = 0; op < t->operands; ++op)
8779
0
  {
8780
0
    if (t->operand_types[op].bitfield.zmmword
8781
0
        && (i.types[op].bitfield.ymmword
8782
0
      || i.types[op].bitfield.xmmword))
8783
0
      {
8784
0
        i.error = operand_size_mismatch;
8785
0
        return 1;
8786
0
      }
8787
0
  }
8788
0
    }
8789
8790
  /* Somewhat similarly, templates specifying both AVX and AVX2 are
8791
     requiring AVX2 support if the actual operand size is YMMword.  */
8792
10.3k
  if (maybe_cpu (t, CpuAVX) && maybe_cpu (t, CpuAVX2)
8793
0
      && !cpu_arch_flags.bitfield.cpuavx2)
8794
0
    {
8795
0
      for (op = 0; op < t->operands; ++op)
8796
0
  {
8797
0
    if (t->operand_types[op].bitfield.xmmword
8798
0
        && i.types[op].bitfield.ymmword)
8799
0
      {
8800
0
        i.error = operand_size_mismatch;
8801
0
        return 1;
8802
0
      }
8803
0
  }
8804
0
    }
8805
8806
  /* Without VSIB byte, we can't have a vector register for index.  */
8807
10.3k
  if (!t->opcode_modifier.sib
8808
10.3k
      && i.index_reg
8809
0
      && (i.index_reg->reg_type.bitfield.xmmword
8810
0
    || i.index_reg->reg_type.bitfield.ymmword
8811
0
    || i.index_reg->reg_type.bitfield.zmmword))
8812
0
    {
8813
0
      i.error = unsupported_vector_index_register;
8814
0
      return 1;
8815
0
    }
8816
8817
  /* Check if default mask is allowed.  */
8818
10.3k
  if (t->opcode_modifier.operandconstraint == NO_DEFAULT_MASK
8819
0
      && (!i.mask.reg || i.mask.reg->reg_num == 0))
8820
0
    {
8821
0
      i.error = no_default_mask;
8822
0
      return 1;
8823
0
    }
8824
8825
  /* For VSIB byte, we need a vector register for index, and all vector
8826
     registers must be distinct.  */
8827
10.3k
  if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
8828
0
    {
8829
0
      if (!i.index_reg
8830
0
    || !((t->opcode_modifier.sib == VECSIB128
8831
0
    && i.index_reg->reg_type.bitfield.xmmword)
8832
0
         || (t->opcode_modifier.sib == VECSIB256
8833
0
       && i.index_reg->reg_type.bitfield.ymmword)
8834
0
         || (t->opcode_modifier.sib == VECSIB512
8835
0
       && i.index_reg->reg_type.bitfield.zmmword)))
8836
0
      {
8837
0
  i.error = invalid_vsib_address;
8838
0
  return 1;
8839
0
      }
8840
8841
0
      gas_assert (i.reg_operands == 2 || i.mask.reg);
8842
0
      if (i.reg_operands == 2 && !i.mask.reg)
8843
0
  {
8844
0
    gas_assert (i.types[0].bitfield.class == RegSIMD);
8845
0
    gas_assert (i.types[0].bitfield.xmmword
8846
0
          || i.types[0].bitfield.ymmword);
8847
0
    gas_assert (i.types[2].bitfield.class == RegSIMD);
8848
0
    gas_assert (i.types[2].bitfield.xmmword
8849
0
          || i.types[2].bitfield.ymmword);
8850
0
    if (operand_check == check_none)
8851
0
      return 0;
8852
0
    if (register_number (i.op[0].regs)
8853
0
        != register_number (i.index_reg)
8854
0
        && register_number (i.op[2].regs)
8855
0
     != register_number (i.index_reg)
8856
0
        && register_number (i.op[0].regs)
8857
0
     != register_number (i.op[2].regs))
8858
0
      return 0;
8859
0
    if (operand_check == check_error)
8860
0
      {
8861
0
        i.error = invalid_vector_register_set;
8862
0
        return 1;
8863
0
      }
8864
0
    as_warn (_("mask, index, and destination registers should be distinct"));
8865
0
  }
8866
0
      else if (i.reg_operands == 1 && i.mask.reg)
8867
0
  {
8868
0
    if (i.types[1].bitfield.class == RegSIMD
8869
0
        && (i.types[1].bitfield.xmmword
8870
0
            || i.types[1].bitfield.ymmword
8871
0
            || i.types[1].bitfield.zmmword)
8872
0
        && (register_number (i.op[1].regs)
8873
0
      == register_number (i.index_reg)))
8874
0
      {
8875
0
        if (operand_check == check_error)
8876
0
    {
8877
0
      i.error = invalid_vector_register_set;
8878
0
      return 1;
8879
0
    }
8880
0
        if (operand_check != check_none)
8881
0
    as_warn (_("index and destination registers should be distinct"));
8882
0
      }
8883
0
  }
8884
0
    }
8885
8886
  /* For AMX instructions with 3 TMM register operands, all operands
8887
      must be distinct.  */
8888
10.3k
  if (i.reg_operands == 3
8889
0
      && t->operand_types[0].bitfield.tmmword
8890
0
      && (i.op[0].regs == i.op[1].regs
8891
0
          || i.op[0].regs == i.op[2].regs
8892
0
          || i.op[1].regs == i.op[2].regs))
8893
0
    {
8894
0
      i.error = invalid_tmm_register_set;
8895
0
      return 1;
8896
0
    }
8897
8898
  /* For some special instructions require that destination must be distinct
8899
     from source registers.  */
8900
10.3k
  if (t->opcode_modifier.operandconstraint == DISTINCT_DEST)
8901
0
    {
8902
0
      unsigned int dest_reg = i.operands - 1;
8903
8904
0
      know (i.operands >= 3);
8905
8906
      /* #UD if dest_reg == src1_reg or dest_reg == src2_reg.  */
8907
0
      if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
8908
0
    || (i.reg_operands > 2
8909
0
        && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
8910
0
  {
8911
0
    i.error = invalid_dest_and_src_register_set;
8912
0
    return 1;
8913
0
  }
8914
0
    }
8915
8916
  /* Check if broadcast is supported by the instruction and is applied
8917
     to the memory operand.  */
8918
10.3k
  if (i.broadcast.type || i.broadcast.bytes)
8919
0
    {
8920
0
      i386_operand_type type, overlap;
8921
8922
      /* Check if specified broadcast is supported in this instruction,
8923
   and its broadcast bytes match the memory operand.  */
8924
0
      op = i.broadcast.operand;
8925
0
      if (!t->opcode_modifier.broadcast
8926
0
    || !(i.flags[op] & Operand_Mem)
8927
0
    || (!i.types[op].bitfield.unspecified
8928
0
        && !match_broadcast_size (t, op)))
8929
0
  {
8930
0
  bad_broadcast:
8931
0
    i.error = unsupported_broadcast;
8932
0
    return 1;
8933
0
  }
8934
8935
0
      operand_type_set (&type, 0);
8936
0
      switch (get_broadcast_bytes (t, false))
8937
0
  {
8938
0
  case 2:
8939
0
    type.bitfield.word = 1;
8940
0
    break;
8941
0
  case 4:
8942
0
    type.bitfield.dword = 1;
8943
0
    break;
8944
0
  case 8:
8945
0
    type.bitfield.qword = 1;
8946
0
    break;
8947
0
  case 16:
8948
0
    type.bitfield.xmmword = 1;
8949
0
    break;
8950
0
  case 32:
8951
0
    if (vector_size < VSZ256)
8952
0
      goto bad_broadcast;
8953
0
    type.bitfield.ymmword = 1;
8954
0
    break;
8955
0
  case 64:
8956
0
    if (vector_size < VSZ512)
8957
0
      goto bad_broadcast;
8958
0
    type.bitfield.zmmword = 1;
8959
0
    break;
8960
0
  default:
8961
0
    goto bad_broadcast;
8962
0
  }
8963
8964
0
      overlap = operand_type_and (type, t->operand_types[op]);
8965
0
      if (t->operand_types[op].bitfield.class == RegSIMD
8966
0
    && t->operand_types[op].bitfield.byte
8967
0
       + t->operand_types[op].bitfield.word
8968
0
       + t->operand_types[op].bitfield.dword
8969
0
       + t->operand_types[op].bitfield.qword > 1)
8970
0
  {
8971
0
    overlap.bitfield.xmmword = 0;
8972
0
    overlap.bitfield.ymmword = 0;
8973
0
    overlap.bitfield.zmmword = 0;
8974
0
  }
8975
0
      if (operand_type_all_zero (&overlap))
8976
0
    goto bad_broadcast;
8977
8978
0
      if (t->opcode_modifier.checkoperandsize)
8979
0
  {
8980
0
    unsigned int j;
8981
8982
0
    type.bitfield.baseindex = 1;
8983
0
    for (j = i.imm_operands; j < i.operands; ++j)
8984
0
      {
8985
0
        if (j != op
8986
0
      && !operand_type_register_match(i.types[j],
8987
0
              t->operand_types[j],
8988
0
              type,
8989
0
              t->operand_types[op]))
8990
0
    goto bad_broadcast;
8991
0
      }
8992
0
  }
8993
0
    }
8994
  /* If broadcast is supported in this instruction, we need to check if
8995
     operand of one-element size isn't specified without broadcast.  */
8996
10.3k
  else if (t->opcode_modifier.broadcast && i.mem_operands)
8997
0
    {
8998
      /* Find memory operand.  */
8999
0
      for (op = i.imm_operands; op < i.operands; op++)
9000
0
  if (i.flags[op] & Operand_Mem)
9001
0
    break;
9002
0
      gas_assert (op < i.operands);
9003
      /* Check size of the memory operand.  */
9004
0
      if (match_broadcast_size (t, op))
9005
0
  {
9006
0
    i.error = broadcast_needed;
9007
0
    return 1;
9008
0
  }
9009
0
    }
9010
10.3k
  else
9011
10.3k
    op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning.  */
9012
9013
  /* Check if requested masking is supported.  */
9014
10.3k
  if (i.mask.reg)
9015
0
    {
9016
0
      if (!t->opcode_modifier.masking)
9017
0
  {
9018
0
    i.error = unsupported_masking;
9019
0
    return 1;
9020
0
  }
9021
9022
      /* Common rules for masking:
9023
   - mask register destinations permit only zeroing-masking, without
9024
     that actually being expressed by a {z} operand suffix or EVEX.z,
9025
   - memory destinations allow only merging-masking,
9026
   - scatter/gather insns (i.e. ones using vSIB) only allow merging-
9027
     masking.  */
9028
0
      if (i.mask.zeroing
9029
0
    && (t->operand_types[t->operands - 1].bitfield.class == RegMask
9030
0
        || (i.flags[t->operands - 1] & Operand_Mem)
9031
0
        || t->opcode_modifier.sib))
9032
0
  {
9033
0
    i.error = unsupported_masking;
9034
0
    return 1;
9035
0
  }
9036
0
    }
9037
9038
  /* Check if masking is applied to dest operand.  */
9039
10.3k
  if (i.mask.reg && (i.mask.operand != i.operands - 1))
9040
0
    {
9041
0
      i.error = mask_not_on_destination;
9042
0
      return 1;
9043
0
    }
9044
9045
  /* Check RC/SAE.  */
9046
10.3k
  if (i.rounding.type != rc_none)
9047
0
    {
9048
0
      if (!t->opcode_modifier.sae
9049
0
    || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
9050
0
    || i.mem_operands)
9051
0
  {
9052
0
    i.error = unsupported_rc_sae;
9053
0
    return 1;
9054
0
  }
9055
9056
      /* Non-EVEX.{LIG,512} forms need to have a ZMM or YMM register as at
9057
   least one operand.  There's no need to check all operands, though:
9058
   Either of the last two operands will be of the right size in all
9059
   relevant templates.  */
9060
0
      if (t->opcode_modifier.evex != EVEXLIG
9061
0
    && t->opcode_modifier.evex != EVEX512
9062
0
    && !i.types[t->operands - 1].bitfield.zmmword
9063
0
    && !i.types[t->operands - 2].bitfield.zmmword)
9064
0
  {
9065
0
    i.error = operand_size_mismatch;
9066
0
    return 1;
9067
0
  }
9068
0
    }
9069
9070
  /* Check the special Imm4 cases; must be the first operand.  */
9071
10.3k
  if ((is_cpu (t, CpuXOP) && t->operands == 5)
9072
10.3k
      || (t->opcode_space == SPACE_0F3A
9073
0
    && (t->base_opcode | 3) == 0x0b
9074
0
    && (is_cpu (t, CpuAPX_F)
9075
0
     || (t->opcode_modifier.sse2avx && t->opcode_modifier.evex
9076
0
         && (!t->opcode_modifier.vex
9077
0
       || (pp.encoding != encoding_default
9078
0
           && pp.encoding != encoding_vex
9079
0
           && pp.encoding != encoding_vex3))))))
9080
0
    {
9081
0
      if (i.op[0].imms->X_op != O_constant
9082
0
    || !fits_in_imm4 (i.op[0].imms->X_add_number))
9083
0
  {
9084
0
    i.error = bad_imm4;
9085
0
    return 1;
9086
0
  }
9087
9088
      /* Turn off Imm<N> so that update_imm won't complain.  */
9089
0
      if (t->operands == 5)
9090
0
  operand_type_set (&i.types[0], 0);
9091
0
    }
9092
9093
  /* Check vector Disp8 operand.  */
9094
10.3k
  if (t->opcode_modifier.disp8memshift
9095
0
      && (!t->opcode_modifier.vex
9096
0
    || need_evex_encoding (t))
9097
0
      && pp.disp_encoding <= disp_encoding_8bit)
9098
0
    {
9099
0
      if (i.broadcast.type || i.broadcast.bytes)
9100
0
  i.memshift = t->opcode_modifier.broadcast - 1;
9101
0
      else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
9102
0
  i.memshift = t->opcode_modifier.disp8memshift;
9103
0
      else
9104
0
  {
9105
0
    const i386_operand_type *type = NULL, *fallback = NULL;
9106
9107
0
    i.memshift = 0;
9108
0
    for (op = i.imm_operands; op < i.operands; op++)
9109
0
      if (i.flags[op] & Operand_Mem)
9110
0
        {
9111
0
    if (t->opcode_modifier.evex == EVEXLIG)
9112
0
      i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
9113
0
    else if (t->operand_types[op].bitfield.xmmword
9114
0
       + t->operand_types[op].bitfield.ymmword
9115
0
       + t->operand_types[op].bitfield.zmmword <= 1)
9116
0
      type = &t->operand_types[op];
9117
0
    else if (!i.types[op].bitfield.unspecified)
9118
0
      type = &i.types[op];
9119
0
    else /* Ambiguities get resolved elsewhere.  */
9120
0
      fallback = &t->operand_types[op];
9121
0
        }
9122
0
      else if (i.types[op].bitfield.class == RegSIMD
9123
0
         && t->opcode_modifier.evex != EVEXLIG)
9124
0
        {
9125
0
    if (i.types[op].bitfield.zmmword)
9126
0
      i.memshift = 6;
9127
0
    else if (i.types[op].bitfield.ymmword && i.memshift < 5)
9128
0
      i.memshift = 5;
9129
0
    else if (i.types[op].bitfield.xmmword && i.memshift < 4)
9130
0
      i.memshift = 4;
9131
0
        }
9132
9133
0
    if (!type && !i.memshift)
9134
0
      type = fallback;
9135
0
    if (type)
9136
0
      {
9137
0
        if (type->bitfield.zmmword)
9138
0
    i.memshift = 6;
9139
0
        else if (type->bitfield.ymmword)
9140
0
    i.memshift = 5;
9141
0
        else if (type->bitfield.xmmword)
9142
0
    i.memshift = 4;
9143
0
      }
9144
9145
    /* For the check in fits_in_disp8().  */
9146
0
    if (i.memshift == 0)
9147
0
      i.memshift = -1;
9148
0
  }
9149
9150
0
      for (op = i.imm_operands; op < i.operands; op++)
9151
0
  if (operand_type_check (i.types[op], disp)
9152
0
      && i.op[op].disps->X_op == O_constant)
9153
0
    {
9154
      /* Make sure to leave i.types[op].bitfield.disp8 alone upon
9155
         secondary invocations of match_template().  */
9156
0
      if (fits_in_disp8 (i.op[op].disps->X_add_number))
9157
0
        {
9158
0
    if (!i.tm.mnem_off)
9159
0
      i.types[op].bitfield.disp8 = 1;
9160
0
    return 0;
9161
0
        }
9162
0
      if (!i.tm.mnem_off)
9163
0
        i.types[op].bitfield.disp8 = 0;
9164
0
    }
9165
0
    }
9166
9167
10.3k
  i.memshift = 0;
9168
9169
10.3k
  return 0;
9170
10.3k
}
9171
9172
/* Check if encoding requirements are met by the instruction.  */
9173
9174
static int
9175
VEX_check_encoding (const insn_template *t)
9176
11.5k
{
9177
11.5k
  if (pp.encoding == encoding_error)
9178
0
    {
9179
0
      i.error = unsupported;
9180
0
      return 1;
9181
0
    }
9182
9183
  /* Vector size restrictions.  */
9184
11.5k
  if ((vector_size < VSZ512
9185
0
       && t->opcode_modifier.evex == EVEX512)
9186
11.5k
      || (vector_size < VSZ256
9187
0
    && (t->opcode_modifier.evex == EVEX256
9188
0
        || t->opcode_modifier.vex == VEX256)))
9189
0
    {
9190
0
      i.error = unsupported_vector_size;
9191
0
      return 1;
9192
0
    }
9193
9194
11.5k
  switch (pp.encoding)
9195
11.5k
    {
9196
35
    case encoding_vex:
9197
35
    case encoding_vex3:
9198
      /* This instruction must be encoded with VEX prefix.  */
9199
35
      if (!t->opcode_modifier.vex)
9200
35
  {
9201
35
    i.error = no_vex_encoding;
9202
35
    return 1;
9203
35
  }
9204
0
      break;
9205
9206
11.4k
    case encoding_default:
9207
11.4k
      if (!pp.has_nf)
9208
11.4k
  break;
9209
      /* Fall through.  */
9210
1
    case encoding_evex:
9211
8
    case encoding_evex512:
9212
      /* This instruction must be encoded with EVEX prefix.  */
9213
8
      if (!t->opcode_modifier.evex)
9214
6
  {
9215
6
    i.error = no_evex_encoding;
9216
6
    return 1;
9217
6
  }
9218
2
      break;
9219
9220
11
    case encoding_egpr:
9221
      /* This instruction must be encoded with REX2 or EVEX prefix.  */
9222
11
      if (t->opcode_modifier.vex && !t->opcode_modifier.evex)
9223
0
  {
9224
0
    i.error = no_evex_encoding;
9225
0
    return 1;
9226
0
  }
9227
11
      break;
9228
9229
11
    default:
9230
0
      abort ();
9231
11.5k
    }
9232
9233
11.4k
  return 0;
9234
11.5k
}
9235
9236
/* Check if Egprs operands are valid for the instruction.  */
9237
9238
static bool
9239
check_EgprOperands (const insn_template *t)
9240
10.3k
{
9241
10.3k
  if (!t->opcode_modifier.noegpr)
9242
10.0k
    return false;
9243
9244
790
  for (unsigned int op = i.imm_operands; op < i.operands; op++)
9245
428
    {
9246
428
      if (i.types[op].bitfield.class != Reg)
9247
343
  continue;
9248
9249
85
      if (i.op[op].regs->reg_flags & RegRex2)
9250
0
  {
9251
0
    i.error = register_type_mismatch;
9252
0
    return true;
9253
0
  }
9254
85
    }
9255
9256
362
  if ((i.index_reg && (i.index_reg->reg_flags & RegRex2))
9257
362
      || (i.base_reg && (i.base_reg->reg_flags & RegRex2)))
9258
0
    {
9259
0
      i.error = unsupported_EGPR_for_addressing;
9260
0
      return true;
9261
0
    }
9262
9263
  /* Check if pseudo prefix {rex2} is valid.  */
9264
362
  if (pp.rex2_encoding && !t->opcode_modifier.sse2avx)
9265
0
    {
9266
0
      i.error = invalid_pseudo_prefix;
9267
0
      return true;
9268
0
    }
9269
9270
362
  return false;
9271
362
}
9272
9273
/* Check if APX operands are valid for the instruction.  */
9274
static bool
9275
check_APX_operands (const insn_template *t)
9276
10.3k
{
9277
  /* Push2* and Pop2* cannot use RSP and Pop2* cannot pop two same registers.
9278
   */
9279
10.3k
  switch (t->mnem_off)
9280
10.3k
    {
9281
0
    case MN_pop2:
9282
0
    case MN_pop2p:
9283
0
      if (register_number (i.op[0].regs) == register_number (i.op[1].regs))
9284
0
  {
9285
0
    i.error = invalid_dest_register_set;
9286
0
    return 1;
9287
0
  }
9288
    /* fall through */
9289
0
    case MN_push2:
9290
0
    case MN_push2p:
9291
0
      if (register_number (i.op[0].regs) == 4
9292
0
    || register_number (i.op[1].regs) == 4)
9293
0
  {
9294
0
    i.error = unsupported_rsp_register;
9295
0
    return 1;
9296
0
  }
9297
0
      break;
9298
10.3k
    }
9299
10.3k
  return 0;
9300
10.3k
}
9301
9302
/* Check if the instruction use the REX registers or REX prefix.  */
9303
static bool
9304
check_Rex_required (void)
9305
0
{
9306
0
  for (unsigned int op = i.imm_operands; op < i.operands; op++)
9307
0
    {
9308
0
      if (i.types[op].bitfield.class != Reg)
9309
0
  continue;
9310
9311
0
      if (i.op[op].regs->reg_flags & (RegRex | RegRex64))
9312
0
  return true;
9313
0
    }
9314
9315
0
  if ((i.index_reg && (i.index_reg->reg_flags & RegRex))
9316
0
      || (i.base_reg && (i.base_reg->reg_flags & RegRex)))
9317
0
    return true;
9318
9319
  /* Check pseudo prefix {rex} are valid.  */
9320
0
  return pp.rex_encoding;
9321
0
}
9322
9323
/* Optimize APX NDD insns to legacy insns.  */
9324
static unsigned int
9325
can_convert_NDD_to_legacy (const insn_template *t)
9326
0
{
9327
0
  unsigned int match_dest_op = ~0;
9328
9329
0
  if (!pp.has_nf && i.reg_operands >= 2)
9330
0
    {
9331
0
      unsigned int dest = i.operands - 1;
9332
0
      unsigned int src1 = i.operands - 2;
9333
0
      unsigned int src2 = (i.operands > 3) ? i.operands - 3 : 0;
9334
9335
0
      if (i.types[src1].bitfield.class == Reg
9336
0
    && i.op[src1].regs == i.op[dest].regs)
9337
0
  match_dest_op = src1;
9338
      /* If the first operand is the same as the third operand,
9339
   these instructions need to support the ability to commutative
9340
   the first two operands and still not change the semantics in order
9341
   to be optimized.  */
9342
0
      else if (optimize > 1
9343
0
         && t->opcode_modifier.commutative
9344
0
         && i.types[src2].bitfield.class == Reg
9345
0
         && i.op[src2].regs == i.op[dest].regs)
9346
0
  match_dest_op = src2;
9347
0
    }
9348
0
  return match_dest_op;
9349
0
}
9350
9351
/* Helper function for the progress() macro in match_template().  */
9352
static INLINE enum i386_error progress (enum i386_error new,
9353
          enum i386_error last,
9354
          unsigned int line, unsigned int *line_p)
9355
191k
{
9356
191k
  if (line <= *line_p)
9357
82.5k
    return last;
9358
109k
  *line_p = line;
9359
109k
  return new;
9360
191k
}
9361
9362
static const insn_template *
9363
match_template (char mnem_suffix)
9364
16.6k
{
9365
  /* Points to template once we've found it.  */
9366
16.6k
  const insn_template *t;
9367
16.6k
  i386_operand_type overlap0, overlap1, overlap2, overlap3;
9368
16.6k
  i386_operand_type overlap4;
9369
16.6k
  unsigned int found_reverse_match;
9370
16.6k
  i386_operand_type operand_types [MAX_OPERANDS];
9371
16.6k
  int addr_prefix_disp;
9372
16.6k
  unsigned int j, size_match, check_register, errline = __LINE__;
9373
16.6k
  enum i386_error specific_error = number_of_operands_mismatch;
9374
191k
#define progress(err) progress (err, specific_error, __LINE__, &errline)
9375
9376
#if MAX_OPERANDS != 5
9377
# error "MAX_OPERANDS must be 5."
9378
#endif
9379
9380
16.6k
  found_reverse_match = 0;
9381
16.6k
  addr_prefix_disp = -1;
9382
9383
71.1k
  for (t = current_templates.start; t < current_templates.end; t++)
9384
66.0k
    {
9385
66.0k
      addr_prefix_disp = -1;
9386
66.0k
      found_reverse_match = 0;
9387
9388
      /* Must have right number of operands.  */
9389
66.0k
      if (i.operands != t->operands)
9390
33.5k
  continue;
9391
9392
      /* Skip SSE2AVX templates when inapplicable.  */
9393
32.4k
      if (t->opcode_modifier.sse2avx
9394
121
    && (!sse2avx || i.prefix[DATA_PREFIX]))
9395
121
  {
9396
    /* Another non-SSE2AVX template has to follow.  */
9397
121
    gas_assert (t + 1 < current_templates.end);
9398
121
    continue;
9399
121
  }
9400
9401
      /* Check processor support.  */
9402
32.3k
      specific_error = progress (unsupported);
9403
32.3k
      if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
9404
11.0k
  continue;
9405
9406
      /* Check AT&T mnemonic.   */
9407
21.2k
      specific_error = progress (unsupported_with_intel_mnemonic);
9408
21.2k
      if (!intel_syntax && intel_mnemonic
9409
0
    && t->opcode_modifier.dialect == ATT_MNEMONIC)
9410
0
  continue;
9411
9412
      /* Check AT&T/Intel syntax.  */
9413
21.2k
      specific_error = progress (unsupported_syntax);
9414
21.2k
      if (intel_syntax
9415
21.2k
     ? t->opcode_modifier.dialect >= ATT_SYNTAX
9416
21.2k
     : t->opcode_modifier.dialect == INTEL_SYNTAX)
9417
0
  continue;
9418
9419
      /* Check NF support.  */
9420
21.2k
      specific_error = progress (unsupported_nf);
9421
21.2k
      if (pp.has_nf && !t->opcode_modifier.nf)
9422
3
  continue;
9423
9424
      /* Check Intel64/AMD64 ISA.   */
9425
21.2k
      switch (isa64)
9426
21.2k
  {
9427
21.2k
  default:
9428
    /* Default: Don't accept Intel64.  */
9429
21.2k
    if (t->opcode_modifier.isa64 == INTEL64)
9430
9
      continue;
9431
21.2k
    break;
9432
21.2k
  case amd64:
9433
    /* -mamd64: Don't accept Intel64 and Intel64 only.  */
9434
0
    if (t->opcode_modifier.isa64 >= INTEL64)
9435
0
      continue;
9436
0
    break;
9437
0
  case intel64:
9438
    /* -mintel64: Don't accept AMD64.  */
9439
0
    if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
9440
0
      continue;
9441
0
    break;
9442
21.2k
  }
9443
9444
      /* Check the suffix.  */
9445
21.2k
      specific_error = progress (invalid_instruction_suffix);
9446
21.2k
      if ((t->opcode_modifier.no_bsuf && mnem_suffix == BYTE_MNEM_SUFFIX)
9447
20.7k
    || (t->opcode_modifier.no_wsuf && mnem_suffix == WORD_MNEM_SUFFIX)
9448
20.7k
    || (t->opcode_modifier.no_lsuf && mnem_suffix == LONG_MNEM_SUFFIX)
9449
20.6k
    || (t->opcode_modifier.no_ssuf && mnem_suffix == SHORT_MNEM_SUFFIX)
9450
20.6k
    || (t->opcode_modifier.no_qsuf && mnem_suffix == QWORD_MNEM_SUFFIX))
9451
606
  continue;
9452
9453
20.6k
      specific_error = progress (operand_size_mismatch);
9454
20.6k
      size_match = operand_size_match (t);
9455
20.6k
      if (!size_match)
9456
107
  continue;
9457
9458
      /* This is intentionally not
9459
9460
   if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
9461
9462
   as the case of a missing * on the operand is accepted (perhaps with
9463
   a warning, issued further down).  */
9464
20.5k
      specific_error = progress (operand_type_mismatch);
9465
20.5k
      if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
9466
11
  continue;
9467
9468
      /* In Intel syntax, normally we can check for memory operand size when
9469
   there is no mnemonic suffix.  But jmp and call have 2 different
9470
   encodings with Dword memory operand size.  Skip the "near" one
9471
   (permitting a register operand) when "far" was requested.  */
9472
20.5k
      if (i.far_branch
9473
0
    && t->opcode_modifier.jump == JUMP_ABSOLUTE
9474
0
    && t->operand_types[0].bitfield.class == Reg)
9475
0
  continue;
9476
9477
123k
      for (j = 0; j < MAX_OPERANDS; j++)
9478
102k
  operand_types[j] = t->operand_types[j];
9479
9480
      /* In general, don't allow 32-bit operands on pre-386.  */
9481
20.5k
      specific_error = progress (mnem_suffix ? invalid_instruction_suffix
9482
20.5k
               : operand_size_mismatch);
9483
20.5k
      j = i.imm_operands + (t->operands > i.imm_operands + 1);
9484
20.5k
      if (i.suffix == LONG_MNEM_SUFFIX
9485
89
    && !cpu_arch_flags.bitfield.cpui386
9486
0
    && (intel_syntax
9487
0
        ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
9488
0
     && !intel_float_operand (insn_name (t)))
9489
0
        : intel_float_operand (insn_name (t)) != 2)
9490
0
    && (t->operands == i.imm_operands
9491
0
        || (operand_types[i.imm_operands].bitfield.class != RegMMX
9492
0
         && operand_types[i.imm_operands].bitfield.class != RegSIMD
9493
0
         && operand_types[i.imm_operands].bitfield.class != RegMask)
9494
0
        || (operand_types[j].bitfield.class != RegMMX
9495
0
      && operand_types[j].bitfield.class != RegSIMD
9496
0
      && operand_types[j].bitfield.class != RegMask))
9497
0
    && !t->opcode_modifier.sib)
9498
0
  continue;
9499
9500
      /* Do not verify operands when there are none.  */
9501
20.5k
      if (!t->operands)
9502
1.12k
  {
9503
1.12k
    if (VEX_check_encoding (t))
9504
35
      {
9505
35
        specific_error = progress (i.error);
9506
35
        continue;
9507
35
      }
9508
9509
    /* Check if pseudo prefix {rex2} is valid.  */
9510
1.08k
    if (t->opcode_modifier.noegpr && pp.rex2_encoding)
9511
0
      {
9512
0
        specific_error = progress (invalid_pseudo_prefix);
9513
0
        continue;
9514
0
      }
9515
9516
    /* We've found a match; break out of loop.  */
9517
1.08k
    break;
9518
1.08k
  }
9519
9520
19.4k
      if (!t->opcode_modifier.jump
9521
674
    || t->opcode_modifier.jump == JUMP_ABSOLUTE)
9522
18.7k
  {
9523
    /* There should be only one Disp operand.  */
9524
38.4k
    for (j = 0; j < MAX_OPERANDS; j++)
9525
36.0k
      if (operand_type_check (operand_types[j], disp))
9526
16.3k
        break;
9527
18.7k
    if (j < MAX_OPERANDS)
9528
16.3k
      {
9529
16.3k
        bool override = (i.prefix[ADDR_PREFIX] != 0);
9530
9531
16.3k
        addr_prefix_disp = j;
9532
9533
        /* Address size prefix will turn Disp64 operand into Disp32 and
9534
     Disp32/Disp16 one into Disp16/Disp32 respectively.  */
9535
16.3k
        switch (flag_code)
9536
16.3k
    {
9537
3.16k
    case CODE_16BIT:
9538
3.16k
      override = !override;
9539
      /* Fall through.  */
9540
3.17k
    case CODE_32BIT:
9541
3.17k
      if (operand_types[j].bitfield.disp32
9542
3.17k
          && operand_types[j].bitfield.disp16)
9543
3.17k
        {
9544
3.17k
          operand_types[j].bitfield.disp16 = override;
9545
3.17k
          operand_types[j].bitfield.disp32 = !override;
9546
3.17k
        }
9547
3.17k
      gas_assert (!operand_types[j].bitfield.disp64);
9548
3.17k
      break;
9549
9550
13.1k
    case CODE_64BIT:
9551
13.1k
      if (operand_types[j].bitfield.disp64)
9552
145
        {
9553
145
          gas_assert (!operand_types[j].bitfield.disp32);
9554
145
          operand_types[j].bitfield.disp32 = override;
9555
145
          operand_types[j].bitfield.disp64 = !override;
9556
145
        }
9557
13.1k
      operand_types[j].bitfield.disp16 = 0;
9558
13.1k
      break;
9559
16.3k
    }
9560
16.3k
      }
9561
18.7k
  }
9562
9563
      /* We check register size if needed.  */
9564
19.4k
      if (t->opcode_modifier.checkoperandsize)
9565
3.93k
  {
9566
3.93k
    check_register = (1 << t->operands) - 1;
9567
3.93k
    if (i.broadcast.type || i.broadcast.bytes)
9568
0
      check_register &= ~(1 << i.broadcast.operand);
9569
3.93k
  }
9570
15.4k
      else
9571
15.4k
  check_register = 0;
9572
9573
19.4k
      overlap0 = operand_type_and (i.types[0], operand_types[0]);
9574
19.4k
      switch (t->operands)
9575
19.4k
  {
9576
9.12k
  case 1:
9577
9.12k
    if (!operand_type_match (overlap0, i.types[0]))
9578
691
      {
9579
691
        specific_error = progress (i.error);
9580
691
        continue;
9581
691
      }
9582
9583
    /* Allow the ModR/M encoding to be requested by using the {load} or
9584
       {store} pseudo prefix on an applicable insn.  */
9585
8.43k
    if (!t->opcode_modifier.modrm
9586
276
        && i.reg_operands == 1
9587
0
        && ((pp.dir_encoding == dir_encoding_load
9588
0
       && t->mnem_off != MN_pop)
9589
0
      || (pp.dir_encoding == dir_encoding_store
9590
0
          && t->mnem_off != MN_push))
9591
        /* Avoid BSWAP.  */
9592
0
        && t->mnem_off != MN_bswap)
9593
0
      continue;
9594
8.43k
    break;
9595
9596
9.66k
  case 2:
9597
    /* xchg %eax, %eax is a special case. It is an alias for nop
9598
       only in 32bit mode and we can use opcode 0x90.  In 64bit
9599
       mode, we can't use 0x90 for xchg %eax, %eax since it should
9600
       zero-extend %eax to %rax.  */
9601
9.66k
    if (t->base_opcode == 0x90
9602
9
        && t->opcode_space == SPACE_BASE)
9603
9
      {
9604
9
        if (flag_code == CODE_64BIT
9605
0
      && i.types[0].bitfield.instance == Accum
9606
0
      && i.types[0].bitfield.dword
9607
0
      && i.types[1].bitfield.instance == Accum)
9608
0
    continue;
9609
9610
        /* Allow the ModR/M encoding to be requested by using the
9611
     {load} or {store} pseudo prefix.  */
9612
9
        if (pp.dir_encoding == dir_encoding_load
9613
9
      || pp.dir_encoding == dir_encoding_store)
9614
0
    continue;
9615
9
      }
9616
9617
9.66k
    if (t->base_opcode == MOV_AX_DISP32
9618
315
        && t->opcode_space == SPACE_BASE
9619
315
        && t->mnem_off != MN_movabs)
9620
314
      {
9621
        /* Force 0x8b encoding for "mov foo@GOT, %eax".  */
9622
314
        if (i.reloc[0] == BFD_RELOC_386_GOT32)
9623
0
    continue;
9624
9625
        /* xrelease mov %eax, <disp> is another special case. It must not
9626
     match the accumulator-only encoding of mov.  */
9627
314
        if (i.hle_prefix)
9628
0
    continue;
9629
9630
        /* Allow the ModR/M encoding to be requested by using a suitable
9631
     {load} or {store} pseudo prefix.  */
9632
314
        if (pp.dir_encoding == (i.types[0].bitfield.instance == Accum
9633
314
             ? dir_encoding_store
9634
314
             : dir_encoding_load)
9635
0
      && !i.types[0].bitfield.disp64
9636
0
      && !i.types[1].bitfield.disp64)
9637
0
    continue;
9638
314
      }
9639
9640
    /* Allow the ModR/M encoding to be requested by using the {load} or
9641
       {store} pseudo prefix on an applicable insn.  */
9642
9.66k
    if (!t->opcode_modifier.modrm
9643
1.90k
        && i.reg_operands == 1
9644
368
        && i.imm_operands == 1
9645
204
        && (pp.dir_encoding == dir_encoding_load
9646
204
      || pp.dir_encoding == dir_encoding_store)
9647
0
        && t->opcode_space == SPACE_BASE)
9648
0
      {
9649
0
        if (t->base_opcode == 0xb0 /* mov $imm, %reg */
9650
0
      && pp.dir_encoding == dir_encoding_store)
9651
0
    continue;
9652
9653
0
        if ((t->base_opcode | 0x38) == 0x3c /* <alu> $imm, %acc */
9654
0
      && (t->base_opcode != 0x3c /* cmp $imm, %acc */
9655
0
          || pp.dir_encoding == dir_encoding_load))
9656
0
    continue;
9657
9658
0
        if (t->base_opcode == 0xa8 /* test $imm, %acc */
9659
0
      && pp.dir_encoding == dir_encoding_load)
9660
0
    continue;
9661
0
      }
9662
    /* Fall through.  */
9663
9664
10.2k
  case 3:
9665
10.2k
    if (!(size_match & MATCH_STRAIGHT))
9666
43
      goto check_reverse;
9667
    /* Reverse direction of operands if swapping is possible in the first
9668
       place (operands need to be symmetric) and
9669
       - the load form is requested, and the template is a store form,
9670
       - the store form is requested, and the template is a load form,
9671
       - the non-default (swapped) form is requested.  */
9672
10.2k
    overlap1 = operand_type_and (operand_types[0], operand_types[1]);
9673
9674
10.2k
    j = i.operands - 1 - (t->opcode_space == SPACE_MAP4
9675
2.14k
        && t->opcode_modifier.vexvvvv);
9676
9677
10.2k
    if (t->opcode_modifier.d && i.reg_operands == i.operands
9678
170
        && !operand_type_all_zero (&overlap1))
9679
125
      switch (pp.dir_encoding)
9680
125
        {
9681
0
        case dir_encoding_load:
9682
0
    if (operand_type_check (operand_types[j], anymem)
9683
0
        || t->opcode_modifier.regmem)
9684
0
      goto check_reverse;
9685
0
    break;
9686
9687
0
        case dir_encoding_store:
9688
0
    if (!operand_type_check (operand_types[j], anymem)
9689
0
        && !t->opcode_modifier.regmem)
9690
0
      goto check_reverse;
9691
0
    break;
9692
9693
0
        case dir_encoding_swap:
9694
0
    goto check_reverse;
9695
9696
125
        case dir_encoding_default:
9697
125
    break;
9698
125
        }
9699
9700
    /* If we want store form, we skip the current load.  */
9701
10.2k
    if ((pp.dir_encoding == dir_encoding_store
9702
10.2k
         || pp.dir_encoding == dir_encoding_swap)
9703
0
        && i.mem_operands == 0
9704
0
        && t->opcode_modifier.load)
9705
0
      continue;
9706
    /* Fall through.  */
9707
10.2k
  case 4:
9708
10.2k
  case 5:
9709
10.2k
    overlap1 = operand_type_and (i.types[1], operand_types[1]);
9710
10.2k
    if (!operand_type_match (overlap0, i.types[0])
9711
3.61k
        || !operand_type_match (overlap1, i.types[1])
9712
2.31k
        || ((check_register & 3) == 3
9713
651
      && !operand_type_register_match (i.types[0],
9714
651
               operand_types[0],
9715
651
               i.types[1],
9716
651
               operand_types[1])))
9717
7.92k
      {
9718
7.92k
        specific_error = progress (i.error);
9719
9720
        /* Check if other direction is valid ...  */
9721
7.92k
        if (!t->opcode_modifier.d)
9722
4.18k
    continue;
9723
9724
3.78k
      check_reverse:
9725
3.78k
        if (!(size_match & MATCH_REVERSE))
9726
303
    continue;
9727
        /* Try reversing direction of operands.  */
9728
3.48k
        j = is_cpu (t, CpuFMA4)
9729
3.48k
      || is_cpu (t, CpuXOP)
9730
3.47k
      || is_cpu (t, CpuAPX_F)
9731
3.47k
      || is_cpu (t, CpuAPX_NDD) ? 1 : i.operands - 1;
9732
3.48k
        overlap0 = operand_type_and (i.types[0], operand_types[j]);
9733
3.48k
        overlap1 = operand_type_and (i.types[j], operand_types[0]);
9734
3.48k
        overlap2 = operand_type_and (i.types[1], operand_types[1]);
9735
3.48k
        gas_assert (t->operands != 3 || !check_register
9736
3.48k
        || is_cpu (t, CpuAPX_F) || is_cpu (t, CpuAPX_NDD));
9737
3.48k
        if (!operand_type_match (overlap0, i.types[0])
9738
998
      || !operand_type_match (overlap1, i.types[j])
9739
64
      || (t->operands == 3
9740
0
          && !operand_type_match (overlap2, i.types[1]))
9741
64
      || (check_register
9742
64
          && !operand_type_register_match (i.types[0],
9743
64
                   operand_types[j],
9744
64
                   i.types[j],
9745
64
                   operand_types[0])))
9746
3.42k
    {
9747
      /* Does not match either direction.  */
9748
3.42k
      specific_error = progress (i.error);
9749
3.42k
      continue;
9750
3.42k
    }
9751
        /* found_reverse_match holds which variant of D
9752
     we've found.  */
9753
51
        if (!t->opcode_modifier.d)
9754
0
    found_reverse_match = 0;
9755
51
        else if (operand_types[0].bitfield.tbyte)
9756
0
    {
9757
0
      if (t->opcode_modifier.operandconstraint != UGH)
9758
0
        found_reverse_match = Opcode_FloatD;
9759
0
      else
9760
0
        found_reverse_match = ~0;
9761
      /* FSUB{,R} and FDIV{,R} may need a 2nd bit flipped.  */
9762
0
      if ((t->extension_opcode & 4)
9763
0
          && (intel_syntax || intel_mnemonic))
9764
0
        found_reverse_match |= Opcode_FloatR;
9765
0
    }
9766
51
        else if (is_cpu (t, CpuFMA4) || is_cpu (t, CpuXOP))
9767
0
    {
9768
0
      found_reverse_match = Opcode_VexW;
9769
0
      goto check_operands_345;
9770
0
    }
9771
51
        else if (t->opcode_space == SPACE_MAP4
9772
1
           && t->operands >= 3)
9773
0
    {
9774
0
      found_reverse_match = Opcode_D;
9775
0
      goto check_operands_345;
9776
0
    }
9777
51
        else if (t->opcode_modifier.commutative
9778
           /* CFCMOVcc also wants its major opcode unaltered.  */
9779
50
           || (t->opcode_space == SPACE_MAP4
9780
0
         && (t->base_opcode | 0xf) == 0x4f))
9781
1
    found_reverse_match = ~0;
9782
50
        else if (t->opcode_space != SPACE_BASE
9783
0
           && (t->opcode_space != SPACE_MAP4
9784
         /* MOVBE, originating from SPACE_0F38, also
9785
            belongs here.  */
9786
0
         || t->mnem_off == MN_movbe)
9787
0
           && (t->opcode_space != SPACE_0F
9788
         /* MOV to/from CR/DR/TR, as an exception, follow
9789
            the base opcode space encoding model.  */
9790
0
         || (t->base_opcode | 7) != 0x27))
9791
0
    found_reverse_match = (t->base_opcode & 0xee) != 0x6e
9792
0
              ? Opcode_ExtD : Opcode_SIMD_IntD;
9793
50
        else
9794
50
    found_reverse_match = Opcode_D;
9795
51
      }
9796
2.30k
    else
9797
2.30k
      {
9798
        /* Found a forward 2 operand match here.  */
9799
2.30k
      check_operands_345:
9800
2.30k
        switch (t->operands)
9801
2.30k
    {
9802
0
    case 5:
9803
0
      overlap4 = operand_type_and (i.types[4], operand_types[4]);
9804
0
      if (!operand_type_match (overlap4, i.types[4])
9805
0
          || !operand_type_register_match (i.types[3],
9806
0
                   operand_types[3],
9807
0
                   i.types[4],
9808
0
                   operand_types[4]))
9809
0
        {
9810
0
          specific_error = progress (i.error);
9811
0
          continue;
9812
0
        }
9813
      /* Fall through.  */
9814
0
    case 4:
9815
0
      overlap3 = operand_type_and (i.types[3], operand_types[3]);
9816
0
      if (!operand_type_match (overlap3, i.types[3])
9817
0
          || ((check_register & 0xa) == 0xa
9818
0
        && !operand_type_register_match (i.types[1],
9819
0
                  operand_types[1],
9820
0
                  i.types[3],
9821
0
                  operand_types[3]))
9822
0
          || ((check_register & 0xc) == 0xc
9823
0
        && !operand_type_register_match (i.types[2],
9824
0
                  operand_types[2],
9825
0
                  i.types[3],
9826
0
                  operand_types[3])))
9827
0
        {
9828
0
          specific_error = progress (i.error);
9829
0
          continue;
9830
0
        }
9831
      /* Fall through.  */
9832
403
    case 3:
9833
403
      overlap2 = operand_type_and (i.types[2], operand_types[2]);
9834
403
      if (!operand_type_match (overlap2, i.types[2])
9835
3
          || ((check_register & 5) == 5
9836
3
        && !operand_type_register_match (i.types[0],
9837
3
                  operand_types[0],
9838
3
                  i.types[2],
9839
3
                  operand_types[2]))
9840
3
          || ((check_register & 6) == 6
9841
3
        && !operand_type_register_match (i.types[1],
9842
3
                  operand_types[1],
9843
3
                  i.types[2],
9844
3
                  operand_types[2])))
9845
400
        {
9846
400
          specific_error = progress (i.error);
9847
400
          continue;
9848
400
        }
9849
3
      break;
9850
2.30k
    }
9851
2.30k
      }
9852
    /* Found either forward/reverse 2, 3 or 4 operand match here:
9853
       slip through to break.  */
9854
19.4k
  }
9855
9856
      /* Check if VEX/EVEX encoding requirements can be satisfied.  */
9857
10.3k
      if (VEX_check_encoding (t))
9858
6
  {
9859
6
    specific_error = progress (i.error);
9860
6
    continue;
9861
6
  }
9862
9863
      /* Check if EGPR operands(r16-r31) are valid.  */
9864
10.3k
      if (check_EgprOperands (t))
9865
0
  {
9866
0
    specific_error = progress (i.error);
9867
0
    continue;
9868
0
  }
9869
9870
      /* Check if vector operands are valid.  */
9871
10.3k
      if (check_VecOperands (t))
9872
0
  {
9873
0
    specific_error = progress (i.error);
9874
0
    continue;
9875
0
  }
9876
9877
      /* Check if APX operands are valid.  */
9878
10.3k
      if (check_APX_operands (t))
9879
0
  {
9880
0
    specific_error = progress (i.error);
9881
0
    continue;
9882
0
  }
9883
9884
      /* Check whether to use the shorter VEX encoding for certain insns where
9885
   the EVEX encoding comes first in the table.  This requires the respective
9886
   AVX-* feature to be explicitly enabled.
9887
9888
   Most of the respective insns have just a single EVEX and a single VEX
9889
   template.  The one that's presently different is generated using the
9890
   Vxy / Exy constructs: There are 3 suffix-less EVEX forms, the latter
9891
   two of which may fall back to their two corresponding VEX forms.  */
9892
10.3k
      j = t->mnem_off != MN_vcvtneps2bf16 ? 1 : 2;
9893
10.3k
      if ((t == current_templates.start || j > 1)
9894
175
    && t->opcode_modifier.disp8memshift
9895
0
    && !t->opcode_modifier.vex
9896
0
    && !need_evex_encoding (t)
9897
0
    && t + j < current_templates.end
9898
0
    && t[j].opcode_modifier.vex)
9899
0
  {
9900
0
    i386_cpu_flags cpu;
9901
0
    unsigned int memshift = i.memshift;
9902
9903
0
    i.memshift = 0;
9904
0
    cpu = cpu_flags_and (cpu_flags_from_attr (t[j].cpu),
9905
0
             cpu_arch_isa_flags);
9906
0
    if (!cpu_flags_all_zero (&cpu)
9907
0
        && (!i.types[0].bitfield.disp8
9908
0
      || !operand_type_check (i.types[0], disp)
9909
0
      || i.op[0].disps->X_op != O_constant
9910
0
      || fits_in_disp8 (i.op[0].disps->X_add_number)))
9911
0
      {
9912
0
        specific_error = progress (internal_error);
9913
0
        t += j - 1;
9914
0
        continue;
9915
0
      }
9916
0
    i.memshift = memshift;
9917
0
  }
9918
9919
      /* If we can optimize a NDD insn to legacy insn, like
9920
   add %r16, %r8, %r8 -> add %r16, %r8,
9921
   add  %r8, %r16, %r8 -> add %r16, %r8, then rematch template.
9922
   Note that the semantics have not been changed.  */
9923
10.3k
      if (optimize
9924
0
    && !pp.no_optimize
9925
0
    && pp.encoding != encoding_evex
9926
0
    && ((t + 1 < current_templates.end
9927
0
         && !t[1].opcode_modifier.evex
9928
0
         && t[1].opcode_space <= SPACE_0F38
9929
0
         && t->opcode_modifier.vexvvvv == VexVVVV_DST)
9930
0
        || t->mnem_off == MN_movbe)
9931
0
    && (i.types[i.operands - 1].bitfield.dword
9932
0
        || i.types[i.operands - 1].bitfield.qword))
9933
0
  {
9934
0
    unsigned int match_dest_op = can_convert_NDD_to_legacy (t);
9935
9936
0
    if (match_dest_op != (unsigned int) ~0)
9937
0
      {
9938
0
        size_match = true;
9939
        /* We ensure that the next template has the same input
9940
     operands as the original matching template by the first
9941
     opernd (ATT). To avoid someone support new NDD insns and
9942
     put it in the wrong position.  */
9943
0
        overlap0 = operand_type_and (i.types[0],
9944
0
             t[1].operand_types[0]);
9945
0
        if (t->opcode_modifier.d)
9946
0
    overlap1 = operand_type_and (i.types[0],
9947
0
               t[1].operand_types[1]);
9948
0
        if (!operand_type_match (overlap0, i.types[0])
9949
0
      && (!t->opcode_modifier.d
9950
0
          || !operand_type_match (overlap1, i.types[0])))
9951
0
    size_match = false;
9952
9953
0
        if (size_match
9954
0
      && (t[1].opcode_space <= SPACE_0F
9955
          /* Some non-legacy-map0/1 insns can be shorter when
9956
       legacy-encoded and when no REX prefix is required.  */
9957
0
          || (!check_EgprOperands (t + 1)
9958
0
        && !check_Rex_required ()
9959
0
        && !i.op[i.operands - 1].regs->reg_type.bitfield.qword)))
9960
0
    {
9961
0
      if (i.operands > 2 && match_dest_op == i.operands - 3)
9962
0
        {
9963
0
          swap_2_operands (match_dest_op, i.operands - 2);
9964
9965
          /* CMOVcc is marked commutative, but then also needs its
9966
       encoded condition inverted.  */
9967
0
          if ((t->base_opcode | 0xf) == 0x4f)
9968
0
      i.invert_cond = true;
9969
0
        }
9970
9971
0
      --i.operands;
9972
0
      --i.reg_operands;
9973
9974
0
      if (t->mnem_off == MN_movbe)
9975
0
        {
9976
0
          gas_assert (t[1].mnem_off == MN_bswap);
9977
0
          ++current_templates.end;
9978
0
        }
9979
9980
0
      specific_error = progress (internal_error);
9981
0
      continue;
9982
0
    }
9983
9984
0
      }
9985
0
  }
9986
9987
      /* We've found a match; break out of loop.  */
9988
10.3k
      break;
9989
10.3k
    }
9990
9991
16.6k
#undef progress
9992
9993
16.6k
  if (t == current_templates.end)
9994
5.14k
    {
9995
      /* We found no match.  */
9996
5.14k
      i.error = specific_error;
9997
5.14k
      return NULL;
9998
5.14k
    }
9999
10000
  /* Don't emit diagnostics or install the template when one was already
10001
     installed, i.e. when called from process_suffix().  */
10002
11.4k
  if (i.tm.mnem_off)
10003
3
    return t;
10004
10005
11.4k
  if (!quiet_warnings)
10006
11.4k
    {
10007
11.4k
      if (!intel_syntax
10008
9.57k
    && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
10009
2
  as_warn (_("indirect %s without `*'"), insn_name (t));
10010
10011
11.4k
      if (t->opcode_modifier.isprefix
10012
64
    && t->opcode_modifier.mnemonicsize == IGNORESIZE)
10013
0
  {
10014
    /* Warn them that a data or address size prefix doesn't
10015
       affect assembly of the next line of code.  */
10016
0
    as_warn (_("stand-alone `%s' prefix"), insn_name (t));
10017
0
  }
10018
10019
11.4k
      if (intel_syntax && mnem_suffix && !t->opcode_modifier.intelsuffix)
10020
186
  {
10021
186
    static bool noticed;
10022
10023
186
    as_warn (_("mnemonic suffix used with `%s'"), insn_name (t));
10024
186
    if (!noticed)
10025
1
      {
10026
1
        noticed = true;
10027
1
        as_warn (_(
10028
1
"NOTE: Such forms are deprecated and will be rejected by a future version of the assembler"));
10029
1
      }
10030
186
  }
10031
11.4k
    }
10032
10033
  /* Copy the template we found.  */
10034
11.4k
  install_template (t);
10035
10036
11.4k
  if (addr_prefix_disp != -1)
10037
10.1k
    i.tm.operand_types[addr_prefix_disp]
10038
10.1k
      = operand_types[addr_prefix_disp];
10039
10040
  /* APX insns acting on byte operands are WIG, yet that can't be expressed
10041
     in the templates (they're also covering word/dword/qword operands).  */
10042
11.4k
  if (t->opcode_space == SPACE_MAP4 && !t->opcode_modifier.vexw &&
10043
5
      i.types[i.operands - 1].bitfield.byte)
10044
0
    {
10045
0
      gas_assert (t->opcode_modifier.w);
10046
0
      i.tm.opcode_modifier.vexw = VEXWIG;
10047
0
    }
10048
10049
11.4k
  switch (found_reverse_match)
10050
11.4k
    {
10051
11.4k
    case 0:
10052
11.4k
      break;
10053
10054
0
    case Opcode_FloatR:
10055
0
    case Opcode_FloatR | Opcode_FloatD:
10056
0
      i.tm.extension_opcode ^= Opcode_FloatR >> 3;
10057
0
      found_reverse_match &= Opcode_FloatD;
10058
10059
      /* Fall through.  */
10060
50
    default:
10061
      /* If we found a reverse match we must alter the opcode direction
10062
   bit and clear/flip the regmem modifier one.  found_reverse_match
10063
   holds bits to change (different for int & float insns).  */
10064
10065
50
      i.tm.base_opcode ^= found_reverse_match;
10066
10067
50
      if (i.tm.opcode_space == SPACE_MAP4)
10068
0
  goto swap_first_2;
10069
10070
      /* Certain SIMD insns have their load forms specified in the opcode
10071
   table, and hence we need to _set_ RegMem instead of clearing it.
10072
   We need to avoid setting the bit though on insns like KMOVW.  */
10073
50
      i.tm.opcode_modifier.regmem
10074
50
  = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
10075
50
    && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
10076
0
    && !i.tm.opcode_modifier.regmem;
10077
10078
      /* Fall through.  */
10079
51
    case ~0:
10080
51
      if (i.tm.opcode_space == SPACE_MAP4
10081
1
    && !t->opcode_modifier.commutative)
10082
0
  i.tm.opcode_modifier.operandconstraint = EVEX_NF;
10083
51
      i.tm.operand_types[0] = operand_types[i.operands - 1];
10084
51
      i.tm.operand_types[i.operands - 1] = operand_types[0];
10085
51
      break;
10086
10087
0
    case Opcode_VexW:
10088
      /* Only the first two register operands need reversing, alongside
10089
   flipping VEX.W.  */
10090
0
      i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
10091
10092
      /* In 3-operand insns XOP.W changes which operand goes into XOP.vvvv.  */
10093
0
      i.tm.opcode_modifier.vexvvvv = VexVVVV_SRC1;
10094
10095
0
    swap_first_2:
10096
0
      j = i.tm.operand_types[0].bitfield.imm8;
10097
0
      i.tm.operand_types[j] = operand_types[j + 1];
10098
0
      i.tm.operand_types[j + 1] = operand_types[j];
10099
0
      break;
10100
11.4k
    }
10101
10102
11.4k
  return t;
10103
11.4k
}
10104
10105
static int
10106
check_string (void)
10107
47
{
10108
47
  unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
10109
47
  unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
10110
10111
47
  if (i.seg[op] != NULL && i.seg[op] != reg_es)
10112
0
    {
10113
0
      as_bad (_("`%s' operand %u must use `%ses' segment"),
10114
0
        insn_name (&i.tm),
10115
0
        intel_syntax ? i.tm.operands - es_op : es_op + 1,
10116
0
        register_prefix);
10117
0
      return 0;
10118
0
    }
10119
10120
  /* There's only ever one segment override allowed per instruction.
10121
     This instruction possibly has a legal segment override on the
10122
     second operand, so copy the segment to where non-string
10123
     instructions store it, allowing common code.  */
10124
47
  i.seg[op] = i.seg[1];
10125
10126
47
  return 1;
10127
47
}
10128
10129
static int
10130
process_suffix (const insn_template *t)
10131
11.4k
{
10132
11.4k
  bool is_movx = false;
10133
10134
  /* If matched instruction specifies an explicit instruction mnemonic
10135
     suffix, use it.  */
10136
11.4k
  if (i.tm.opcode_modifier.size == SIZE16)
10137
0
    i.suffix = WORD_MNEM_SUFFIX;
10138
11.4k
  else if (i.tm.opcode_modifier.size == SIZE32)
10139
10
    i.suffix = LONG_MNEM_SUFFIX;
10140
11.4k
  else if (i.tm.opcode_modifier.size == SIZE64)
10141
20
    i.suffix = QWORD_MNEM_SUFFIX;
10142
11.4k
  else if (i.reg_operands
10143
418
     && (i.operands > 1 || i.types[0].bitfield.class == Reg)
10144
415
     && i.tm.opcode_modifier.operandconstraint != ADDR_PREFIX_OP_REG)
10145
415
    {
10146
415
      unsigned int numop = i.operands;
10147
10148
      /* MOVSX/MOVZX */
10149
415
      is_movx = (i.tm.opcode_space == SPACE_0F
10150
9
     && (i.tm.base_opcode | 8) == 0xbe)
10151
406
    || (i.tm.opcode_space == SPACE_BASE
10152
402
        && i.tm.base_opcode == 0x63
10153
0
        && is_cpu (&i.tm, Cpu64));
10154
10155
      /* movsx/movzx want only their source operand considered here, for the
10156
   ambiguity checking below.  The suffix will be replaced afterwards
10157
   to represent the destination (register).  */
10158
415
      if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
10159
0
  --i.operands;
10160
10161
      /* crc32 needs REX.W set regardless of suffix / source operand size.  */
10162
415
      if (i.tm.mnem_off == MN_crc32 && i.tm.operand_types[1].bitfield.qword)
10163
0
        i.rex |= REX_W;
10164
10165
      /* If there's no instruction mnemonic suffix we try to invent one
10166
   based on GPR operands.  */
10167
415
      if (!i.suffix)
10168
330
  {
10169
    /* We take i.suffix from the last register operand specified,
10170
       Destination register type is more significant than source
10171
       register type.  crc32 in SSE4.2 prefers source register
10172
       type. */
10173
330
    unsigned int op = i.tm.mnem_off == MN_crc32 ? 1 : i.operands;
10174
10175
418
    while (op--)
10176
418
      if (i.tm.operand_types[op].bitfield.instance == InstanceNone
10177
89
    || i.tm.operand_types[op].bitfield.instance == Accum)
10178
418
        {
10179
418
    if (i.types[op].bitfield.class != Reg)
10180
88
      continue;
10181
330
    if (i.types[op].bitfield.byte)
10182
1
      i.suffix = BYTE_MNEM_SUFFIX;
10183
329
    else if (i.types[op].bitfield.word)
10184
129
      i.suffix = WORD_MNEM_SUFFIX;
10185
200
    else if (i.types[op].bitfield.dword)
10186
75
      i.suffix = LONG_MNEM_SUFFIX;
10187
125
    else if (i.types[op].bitfield.qword)
10188
125
      i.suffix = QWORD_MNEM_SUFFIX;
10189
0
    else
10190
0
      continue;
10191
330
    break;
10192
330
        }
10193
10194
    /* As an exception, movsx/movzx silently default to a byte source
10195
       in AT&T mode.  */
10196
330
    if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
10197
0
      i.suffix = BYTE_MNEM_SUFFIX;
10198
330
  }
10199
85
      else if (i.suffix == BYTE_MNEM_SUFFIX)
10200
1
  {
10201
1
    if (!check_byte_reg ())
10202
0
      return 0;
10203
1
  }
10204
84
      else if (i.suffix == LONG_MNEM_SUFFIX)
10205
12
  {
10206
12
    if (!check_long_reg ())
10207
12
      return 0;
10208
12
  }
10209
72
      else if (i.suffix == QWORD_MNEM_SUFFIX)
10210
16
  {
10211
16
    if (!check_qword_reg ())
10212
16
      return 0;
10213
16
  }
10214
56
      else if (i.suffix == WORD_MNEM_SUFFIX)
10215
56
  {
10216
56
    if (!check_word_reg ())
10217
0
      return 0;
10218
56
  }
10219
0
      else if (intel_syntax
10220
0
         && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
10221
  /* Do nothing if the instruction is going to ignore the prefix.  */
10222
0
  ;
10223
0
      else
10224
0
  abort ();
10225
10226
      /* Undo the movsx/movzx change done above.  */
10227
387
      i.operands = numop;
10228
387
    }
10229
11.0k
  else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
10230
194
     && !i.suffix)
10231
130
    {
10232
130
      i.suffix = stackop_size;
10233
130
      if (stackop_size == LONG_MNEM_SUFFIX)
10234
21
  {
10235
    /* stackop_size is set to LONG_MNEM_SUFFIX for the
10236
       .code16gcc directive to support 16-bit mode with
10237
       32-bit address.  For IRET without a suffix, generate
10238
       16-bit IRET (opcode 0xcf) to return from an interrupt
10239
       handler.  */
10240
21
    if (i.tm.base_opcode == 0xcf)
10241
5
      {
10242
5
        i.suffix = WORD_MNEM_SUFFIX;
10243
5
        as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
10244
5
      }
10245
    /* Warn about changed behavior for segment register push/pop.  */
10246
16
    else if ((i.tm.base_opcode | 1) == 0x07)
10247
0
      as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
10248
0
         insn_name (&i.tm));
10249
21
  }
10250
130
    }
10251
10.8k
  else if (!i.suffix
10252
10.4k
     && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
10253
10.4k
         || i.tm.opcode_modifier.jump == JUMP_BYTE
10254
10.4k
         || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
10255
10.4k
         || (i.tm.opcode_space == SPACE_0F
10256
8.12k
       && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
10257
8.09k
       && i.tm.extension_opcode <= 3)))
10258
8.11k
    {
10259
8.11k
      switch (flag_code)
10260
8.11k
  {
10261
8.10k
  case CODE_64BIT:
10262
8.10k
    if (!i.tm.opcode_modifier.no_qsuf)
10263
8.10k
      {
10264
8.10k
        if (i.tm.opcode_modifier.jump == JUMP_BYTE
10265
8.10k
      || i.tm.opcode_modifier.no_lsuf)
10266
8.10k
    i.suffix = QWORD_MNEM_SUFFIX;
10267
8.10k
        break;
10268
8.10k
      }
10269
    /* Fall through.  */
10270
0
  case CODE_32BIT:
10271
0
    if (!i.tm.opcode_modifier.no_lsuf)
10272
0
      i.suffix = LONG_MNEM_SUFFIX;
10273
0
    break;
10274
5
  case CODE_16BIT:
10275
5
    if (!i.tm.opcode_modifier.no_wsuf)
10276
5
      i.suffix = WORD_MNEM_SUFFIX;
10277
5
    break;
10278
8.11k
  }
10279
8.11k
    }
10280
10281
11.4k
  if (!i.suffix
10282
2.48k
      && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
10283
    /* Also cover lret/retf/iret in 64-bit mode.  */
10284
109
    || (flag_code == CODE_64BIT
10285
106
        && !i.tm.opcode_modifier.no_lsuf
10286
3
        && !i.tm.opcode_modifier.no_qsuf))
10287
2.37k
      && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
10288
      /* Explicit sizing prefixes are assumed to disambiguate insns.  */
10289
2.36k
      && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
10290
      /* Accept FLDENV et al without suffix.  */
10291
2.30k
      && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
10292
2.30k
    {
10293
2.30k
      unsigned int suffixes, evex = 0;
10294
10295
2.30k
      suffixes = !i.tm.opcode_modifier.no_bsuf;
10296
2.30k
      if (!i.tm.opcode_modifier.no_wsuf)
10297
1.21k
  suffixes |= 1 << 1;
10298
2.30k
      if (!i.tm.opcode_modifier.no_lsuf)
10299
1.21k
  suffixes |= 1 << 2;
10300
2.30k
      if (!i.tm.opcode_modifier.no_ssuf)
10301
6
  suffixes |= 1 << 4;
10302
2.30k
      if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
10303
506
  suffixes |= 1 << 5;
10304
10305
      /* Operand size may be ambiguous only across multiple templates.  Avoid
10306
   the extra effort though if we already know that multiple suffixes /
10307
   operand sizes are allowed.  Also limit this to non-SIMD operand sizes
10308
   (i.e. ones expressable via suffixes) for now.
10309
   There's one special case though that needs excluding: Insns taking
10310
   Disp<N> operands also match templates permitting BaseIndex.  JMP in
10311
   particular would thus wrongly trigger the check further down.  Cover
10312
   JUMP_DWORD insns here as well, just in case.  */
10313
2.30k
      if (i.tm.opcode_modifier.jump != JUMP
10314
2.26k
    && i.tm.opcode_modifier.jump != JUMP_DWORD)
10315
2.26k
  while (!(suffixes & (suffixes - 1)))
10316
1.04k
    {
10317
      /* Sadly check_VecOperands(), running ahead of install_template(),
10318
         may update i.memshift.  Save and restore the value here.  */
10319
1.04k
      unsigned int memshift = i.memshift;
10320
10321
1.04k
      current_templates.start = t + 1;
10322
1.04k
      t = match_template (0);
10323
1.04k
      i.memshift = memshift;
10324
1.04k
      if (t == NULL)
10325
1.04k
        break;
10326
3
      if (!t->opcode_modifier.no_bsuf)
10327
0
        suffixes |= 1 << 0;
10328
3
      if (!t->opcode_modifier.no_wsuf)
10329
0
        suffixes |= 1 << 1;
10330
3
      if (!t->opcode_modifier.no_lsuf)
10331
3
        suffixes |= 1 << 2;
10332
3
      if (!t->opcode_modifier.no_ssuf)
10333
0
        suffixes |= 1 << 4;
10334
3
      if (flag_code == CODE_64BIT && !t->opcode_modifier.no_qsuf)
10335
0
        suffixes |= 1 << 5;
10336
3
    }
10337
10338
      /* For [XYZ]MMWORD operands inspect operand sizes.  While generally
10339
   also suitable for AT&T syntax mode, it was requested that this be
10340
   restricted to just Intel syntax.  */
10341
2.30k
      if (intel_syntax && is_any_vex_encoding (&i.tm)
10342
1
    && !i.broadcast.type && !i.broadcast.bytes)
10343
1
  {
10344
1
    unsigned int op;
10345
10346
1
    for (op = 0; op < i.tm.operands; ++op)
10347
0
      {
10348
0
        if (vector_size < VSZ512)
10349
0
    {
10350
0
      i.tm.operand_types[op].bitfield.zmmword = 0;
10351
0
      if (vector_size < VSZ256)
10352
0
        {
10353
0
          i.tm.operand_types[op].bitfield.ymmword = 0;
10354
0
          if (i.tm.operand_types[op].bitfield.xmmword
10355
0
        && i.tm.opcode_modifier.evex == EVEXDYN)
10356
0
      i.tm.opcode_modifier.evex = EVEX128;
10357
0
        }
10358
0
      else if (i.tm.operand_types[op].bitfield.ymmword
10359
0
         && !i.tm.operand_types[op].bitfield.xmmword
10360
0
         && i.tm.opcode_modifier.evex == EVEXDYN)
10361
0
        i.tm.opcode_modifier.evex = EVEX256;
10362
0
    }
10363
0
        else if (i.tm.opcode_modifier.evex
10364
0
           && !cpu_arch_flags.bitfield.cpuavx512vl)
10365
0
    {
10366
0
      if (i.tm.operand_types[op].bitfield.ymmword)
10367
0
        i.tm.operand_types[op].bitfield.xmmword = 0;
10368
0
      if (i.tm.operand_types[op].bitfield.zmmword)
10369
0
        i.tm.operand_types[op].bitfield.ymmword = 0;
10370
0
      if (i.tm.opcode_modifier.evex == EVEXDYN)
10371
0
        i.tm.opcode_modifier.evex = EVEX512;
10372
0
    }
10373
10374
0
        if (i.tm.operand_types[op].bitfield.xmmword
10375
0
      + i.tm.operand_types[op].bitfield.ymmword
10376
0
      + i.tm.operand_types[op].bitfield.zmmword < 2)
10377
0
    continue;
10378
10379
        /* Any properly sized operand disambiguates the insn.  */
10380
0
        if (i.types[op].bitfield.xmmword
10381
0
      || i.types[op].bitfield.ymmword
10382
0
      || i.types[op].bitfield.zmmword)
10383
0
    {
10384
0
      suffixes &= ~(7 << 6);
10385
0
      evex = 0;
10386
0
      break;
10387
0
    }
10388
10389
0
        if ((i.flags[op] & Operand_Mem)
10390
0
      && i.tm.operand_types[op].bitfield.unspecified)
10391
0
    {
10392
0
      if (i.tm.operand_types[op].bitfield.xmmword)
10393
0
        suffixes |= 1 << 6;
10394
0
      if (i.tm.operand_types[op].bitfield.ymmword)
10395
0
        suffixes |= 1 << 7;
10396
0
      if (i.tm.operand_types[op].bitfield.zmmword)
10397
0
        suffixes |= 1 << 8;
10398
0
      if (i.tm.opcode_modifier.evex)
10399
0
        evex = EVEX512;
10400
0
    }
10401
0
      }
10402
1
  }
10403
10404
      /* Are multiple suffixes / operand sizes allowed?  */
10405
2.30k
      if (suffixes & (suffixes - 1))
10406
1.21k
  {
10407
1.21k
    if (intel_syntax
10408
580
        && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
10409
0
      || operand_check == check_error))
10410
580
      {
10411
580
        as_bad (_("ambiguous operand size for `%s'"), insn_name (&i.tm));
10412
580
        return 0;
10413
580
      }
10414
638
    if (operand_check == check_error)
10415
0
      {
10416
0
        as_bad (_("no instruction mnemonic suffix given and "
10417
0
      "no register operands; can't size `%s'"), insn_name (&i.tm));
10418
0
        return 0;
10419
0
      }
10420
638
    if (operand_check == check_warning)
10421
638
      as_warn (_("%s; using default for `%s'"),
10422
638
           intel_syntax
10423
638
           ? _("ambiguous operand size")
10424
638
           : _("no instruction mnemonic suffix given and "
10425
638
         "no register operands"),
10426
638
           insn_name (&i.tm));
10427
10428
638
    if (i.tm.opcode_modifier.floatmf)
10429
6
      i.suffix = SHORT_MNEM_SUFFIX;
10430
632
    else if (is_movx)
10431
0
      /* handled below */;
10432
632
    else if (evex)
10433
0
      i.tm.opcode_modifier.evex = evex;
10434
632
    else if (flag_code == CODE_16BIT)
10435
130
      i.suffix = WORD_MNEM_SUFFIX;
10436
502
    else if (!i.tm.opcode_modifier.no_lsuf)
10437
502
      i.suffix = LONG_MNEM_SUFFIX;
10438
0
    else
10439
0
      i.suffix = QWORD_MNEM_SUFFIX;
10440
638
  }
10441
2.30k
    }
10442
10443
10.8k
  if (is_movx)
10444
4
    {
10445
      /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
10446
   In AT&T syntax, if there is no suffix (warned about above), the default
10447
   will be byte extension.  */
10448
4
      if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
10449
0
  i.tm.base_opcode |= 1;
10450
10451
      /* For further processing, the suffix should represent the destination
10452
   (register).  This is already the case when one was used with
10453
   mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
10454
   no suffix to begin with.  */
10455
4
      if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
10456
0
  {
10457
0
    if (i.types[1].bitfield.word)
10458
0
      i.suffix = WORD_MNEM_SUFFIX;
10459
0
    else if (i.types[1].bitfield.qword)
10460
0
      i.suffix = QWORD_MNEM_SUFFIX;
10461
0
    else
10462
0
      i.suffix = LONG_MNEM_SUFFIX;
10463
10464
0
    i.tm.opcode_modifier.w = 0;
10465
0
  }
10466
4
    }
10467
10468
10.8k
  if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
10469
101
    i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
10470
101
       != (i.tm.operand_types[1].bitfield.class == Reg);
10471
10472
  /* Change the opcode based on the operand size given by i.suffix.  */
10473
10.8k
  switch (i.suffix)
10474
10.8k
    {
10475
    /* Size floating point instruction.  */
10476
603
    case LONG_MNEM_SUFFIX:
10477
603
      if (i.tm.opcode_modifier.floatmf)
10478
0
  {
10479
0
    i.tm.base_opcode ^= 4;
10480
0
    break;
10481
0
  }
10482
    /* fall through */
10483
994
    case WORD_MNEM_SUFFIX:
10484
9.24k
    case QWORD_MNEM_SUFFIX:
10485
      /* It's not a byte, select word/dword operation.  */
10486
9.24k
      if (i.tm.opcode_modifier.w)
10487
573
  {
10488
573
    if (i.short_form)
10489
12
      i.tm.base_opcode |= 8;
10490
561
    else
10491
561
      i.tm.base_opcode |= 1;
10492
573
  }
10493
10494
      /* Set mode64 for an operand.  */
10495
9.24k
      if (i.suffix == QWORD_MNEM_SUFFIX)
10496
8.25k
  {
10497
8.25k
    if (flag_code == CODE_64BIT
10498
8.25k
        && !i.tm.opcode_modifier.norex64
10499
145
        && !i.tm.opcode_modifier.vexw
10500
        /* Special case for xchg %rax,%rax.  It is NOP and doesn't
10501
     need rex64. */
10502
145
        && ! (i.operands == 2
10503
109
        && i.tm.base_opcode == 0x90
10504
0
        && i.tm.opcode_space == SPACE_BASE
10505
0
        && i.types[0].bitfield.instance == Accum
10506
0
        && i.types[1].bitfield.instance == Accum))
10507
145
      i.rex |= REX_W;
10508
10509
8.25k
    break;
10510
8.25k
  }
10511
10512
    /* fall through */
10513
1.00k
    case SHORT_MNEM_SUFFIX:
10514
      /* Now select between word & dword operations via the operand
10515
   size prefix, except for instructions that will ignore this
10516
   prefix anyway.  */
10517
1.00k
      if (i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
10518
997
    && !i.tm.opcode_modifier.floatmf
10519
991
    && (!is_any_vex_encoding (&i.tm)
10520
5
        || i.tm.opcode_space == SPACE_MAP4)
10521
991
    && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
10522
816
        || (flag_code == CODE_64BIT
10523
587
      && i.tm.opcode_modifier.jump == JUMP_BYTE)))
10524
185
  {
10525
185
    unsigned int prefix = DATA_PREFIX_OPCODE;
10526
10527
185
    if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
10528
10
      prefix = ADDR_PREFIX_OPCODE;
10529
10530
    /* The DATA PREFIX of EVEX promoted from legacy APX instructions
10531
       needs to be adjusted.  */
10532
185
    if (i.tm.opcode_space == SPACE_MAP4)
10533
2
      {
10534
2
        gas_assert (!i.tm.opcode_modifier.opcodeprefix);
10535
2
        i.tm.opcode_modifier.opcodeprefix = PREFIX_0X66;
10536
2
      }
10537
183
    else if (!add_prefix (prefix))
10538
0
      return 0;
10539
185
  }
10540
10541
1.00k
      break;
10542
10543
1.26k
    case 0:
10544
      /* Select word/dword/qword operation with explicit data sizing prefix
10545
   when there are no suitable register operands.  */
10546
1.26k
      if (i.tm.opcode_modifier.w
10547
47
    && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
10548
47
    && (!i.reg_operands
10549
0
        || (i.reg_operands == 1
10550
          /* ShiftCount */
10551
0
      && (i.tm.operand_types[0].bitfield.instance == RegC
10552
          /* InOutPortReg */
10553
0
          || i.tm.operand_types[0].bitfield.instance == RegD
10554
0
          || i.tm.operand_types[1].bitfield.instance == RegD
10555
0
          || i.tm.mnem_off == MN_crc32))))
10556
47
  i.tm.base_opcode |= 1;
10557
1.26k
      break;
10558
10.8k
    }
10559
10560
10.8k
  if (i.tm.opcode_modifier.operandconstraint == ADDR_PREFIX_OP_REG)
10561
0
    {
10562
0
      gas_assert (!i.suffix);
10563
0
      gas_assert (i.reg_operands);
10564
10565
0
      if (i.tm.operand_types[0].bitfield.instance == Accum
10566
0
    || i.operands == 1)
10567
0
  {
10568
    /* The address size override prefix changes the size of the
10569
       first operand.  */
10570
0
    if (flag_code == CODE_64BIT
10571
0
        && i.op[0].regs->reg_type.bitfield.word)
10572
0
      {
10573
0
        as_bad (_("16-bit addressing unavailable for `%s'"),
10574
0
          insn_name (&i.tm));
10575
0
        return 0;
10576
0
      }
10577
10578
0
    if ((flag_code == CODE_32BIT
10579
0
         ? i.op[0].regs->reg_type.bitfield.word
10580
0
         : i.op[0].regs->reg_type.bitfield.dword)
10581
0
        && !add_prefix (ADDR_PREFIX_OPCODE))
10582
0
      return 0;
10583
0
  }
10584
0
      else
10585
0
  {
10586
    /* Check invalid register operand when the address size override
10587
       prefix changes the size of register operands.  */
10588
0
    unsigned int op;
10589
0
    enum { need_word, need_dword, need_qword } need;
10590
10591
    /* Check the register operand for the address size prefix if
10592
       the memory operand has no real registers, like symbol, DISP
10593
       or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant.  */
10594
0
    if (i.mem_operands == 1
10595
0
        && i.reg_operands == 1
10596
0
        && i.operands == 2
10597
0
        && i.types[1].bitfield.class == Reg
10598
0
        && (flag_code == CODE_32BIT
10599
0
      ? i.op[1].regs->reg_type.bitfield.word
10600
0
      : i.op[1].regs->reg_type.bitfield.dword)
10601
0
        && ((i.base_reg == NULL && i.index_reg == NULL)
10602
0
#ifdef OBJ_ELF
10603
0
      || (x86_elf_abi == X86_64_X32_ABI
10604
0
          && i.base_reg
10605
0
          && i.base_reg->reg_num == RegIP
10606
0
          && i.base_reg->reg_type.bitfield.qword))
10607
#else
10608
      || 0)
10609
#endif
10610
0
        && !add_prefix (ADDR_PREFIX_OPCODE))
10611
0
      return 0;
10612
10613
0
    if (flag_code == CODE_32BIT)
10614
0
      need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
10615
0
    else if (i.prefix[ADDR_PREFIX])
10616
0
      need = need_dword;
10617
0
    else
10618
0
      need = flag_code == CODE_64BIT ? need_qword : need_word;
10619
10620
0
    for (op = i.imm_operands; op < i.operands; op++)
10621
0
      {
10622
0
        if (i.types[op].bitfield.class != Reg)
10623
0
    continue;
10624
10625
0
        switch (need)
10626
0
    {
10627
0
    case need_word:
10628
0
      if (i.op[op].regs->reg_type.bitfield.word)
10629
0
        continue;
10630
0
      break;
10631
0
    case need_dword:
10632
0
      if (i.op[op].regs->reg_type.bitfield.dword)
10633
0
        continue;
10634
0
      break;
10635
0
    case need_qword:
10636
0
      if (i.op[op].regs->reg_type.bitfield.qword)
10637
0
        continue;
10638
0
      break;
10639
0
    }
10640
10641
0
        as_bad (_("invalid register operand size for `%s'"),
10642
0
          insn_name (&i.tm));
10643
0
        return 0;
10644
0
      }
10645
0
  }
10646
0
    }
10647
10648
10.8k
  return 1;
10649
10.8k
}
10650
10651
static int
10652
check_byte_reg (void)
10653
1
{
10654
1
  int op;
10655
10656
3
  for (op = i.operands; --op >= 0;)
10657
2
    {
10658
      /* Skip non-register operands. */
10659
2
      if (i.types[op].bitfield.class != Reg)
10660
1
  continue;
10661
10662
      /* If this is an eight bit register, it's OK.  */
10663
1
      if (i.types[op].bitfield.byte)
10664
1
  {
10665
1
    if (i.tm.opcode_modifier.checkoperandsize)
10666
0
      break;
10667
1
    continue;
10668
1
  }
10669
10670
      /* I/O port address operands are OK too.  */
10671
0
      if (i.tm.operand_types[op].bitfield.instance == RegD
10672
0
    && i.tm.operand_types[op].bitfield.word)
10673
0
  continue;
10674
10675
      /* crc32 only wants its source operand checked here.  */
10676
0
      if (i.tm.mnem_off == MN_crc32 && op != 0)
10677
0
  continue;
10678
10679
      /* Any other register is bad.  */
10680
0
      as_bad (_("`%s%s' not allowed with `%s%c'"),
10681
0
        register_prefix, i.op[op].regs->reg_name,
10682
0
        insn_name (&i.tm), i.suffix);
10683
0
      return 0;
10684
0
    }
10685
1
  return 1;
10686
1
}
10687
10688
static int
10689
check_long_reg (void)
10690
12
{
10691
12
  int op;
10692
10693
12
  for (op = i.operands; --op >= 0;)
10694
    /* Skip non-register operands. */
10695
12
    if (i.types[op].bitfield.class != Reg)
10696
0
      continue;
10697
    /* Reject eight bit registers, except where the template requires
10698
       them. (eg. movzb)  */
10699
12
    else if (i.types[op].bitfield.byte
10700
12
       && (i.tm.operand_types[op].bitfield.word
10701
0
     || i.tm.operand_types[op].bitfield.dword
10702
0
     || i.tm.operand_types[op].bitfield.qword))
10703
12
      {
10704
12
  as_bad (_("`%s%s' not allowed with `%s%c'"),
10705
12
    register_prefix,
10706
12
    i.op[op].regs->reg_name,
10707
12
    insn_name (&i.tm),
10708
12
    i.suffix);
10709
12
  return 0;
10710
12
      }
10711
    /* Error if the e prefix on a general reg is missing, or if the r
10712
       prefix on a general reg is present.  */
10713
0
    else if ((i.types[op].bitfield.word
10714
0
        || i.types[op].bitfield.qword)
10715
0
       && i.tm.operand_types[op].bitfield.dword)
10716
0
      {
10717
0
  as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
10718
0
    register_prefix, i.op[op].regs->reg_name,
10719
0
    i.suffix);
10720
0
  return 0;
10721
0
      }
10722
0
    else if (i.tm.opcode_modifier.checkoperandsize)
10723
0
      break;
10724
10725
0
  return 1;
10726
12
}
10727
10728
static int
10729
check_qword_reg (void)
10730
16
{
10731
16
  int op;
10732
10733
27
  for (op = i.operands; --op >= 0; )
10734
    /* Skip non-register operands. */
10735
27
    if (i.types[op].bitfield.class != Reg)
10736
11
      continue;
10737
    /* Reject eight bit registers, except where the template requires
10738
       them. (eg. movzb)  */
10739
16
    else if (i.types[op].bitfield.byte
10740
0
       && (i.tm.operand_types[op].bitfield.word
10741
0
     || i.tm.operand_types[op].bitfield.dword
10742
0
     || i.tm.operand_types[op].bitfield.qword))
10743
0
      {
10744
0
  as_bad (_("`%s%s' not allowed with `%s%c'"),
10745
0
    register_prefix,
10746
0
    i.op[op].regs->reg_name,
10747
0
    insn_name (&i.tm),
10748
0
    i.suffix);
10749
0
  return 0;
10750
0
      }
10751
    /* Error if the r prefix on a general reg is missing.  */
10752
16
    else if ((i.types[op].bitfield.word
10753
4
        || i.types[op].bitfield.dword)
10754
16
       && i.tm.operand_types[op].bitfield.qword)
10755
16
      {
10756
16
  as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
10757
16
    register_prefix, i.op[op].regs->reg_name, i.suffix);
10758
16
  return 0;
10759
16
      }
10760
0
    else if (i.tm.opcode_modifier.checkoperandsize)
10761
0
      break;
10762
10763
0
  return 1;
10764
16
}
10765
10766
static int
10767
check_word_reg (void)
10768
56
{
10769
56
  int op;
10770
112
  for (op = i.operands; --op >= 0;)
10771
    /* Skip non-register operands. */
10772
112
    if (i.types[op].bitfield.class != Reg)
10773
56
      continue;
10774
    /* Reject eight bit registers, except where the template requires
10775
       them. (eg. movzb)  */
10776
56
    else if (i.types[op].bitfield.byte
10777
0
       && (i.tm.operand_types[op].bitfield.word
10778
0
     || i.tm.operand_types[op].bitfield.dword
10779
0
     || i.tm.operand_types[op].bitfield.qword))
10780
0
      {
10781
0
  as_bad (_("`%s%s' not allowed with `%s%c'"),
10782
0
    register_prefix,
10783
0
    i.op[op].regs->reg_name,
10784
0
    insn_name (&i.tm),
10785
0
    i.suffix);
10786
0
  return 0;
10787
0
      }
10788
    /* Error if the e or r prefix on a general reg is present.  */
10789
56
    else if ((i.types[op].bitfield.dword
10790
56
     || i.types[op].bitfield.qword)
10791
0
       && i.tm.operand_types[op].bitfield.word)
10792
0
      {
10793
0
  as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
10794
0
    register_prefix, i.op[op].regs->reg_name,
10795
0
    i.suffix);
10796
0
  return 0;
10797
0
      }
10798
56
    else if (i.tm.opcode_modifier.checkoperandsize)
10799
56
      break;
10800
10801
56
  return 1;
10802
56
}
10803
10804
static int
10805
update_imm (unsigned int j)
10806
11.1k
{
10807
11.1k
  i386_operand_type overlap = i.types[j];
10808
10809
11.1k
  if (i.tm.operand_types[j].bitfield.imm8
10810
639
      && i.tm.operand_types[j].bitfield.imm8s
10811
126
      && overlap.bitfield.imm8 && overlap.bitfield.imm8s)
10812
125
    {
10813
      /* This combination is used on 8-bit immediates where e.g. $~0 is
10814
   desirable to permit.  We're past operand type matching, so simply
10815
   put things back in the shape they were before introducing the
10816
   distinction between Imm8, Imm8S, and Imm8|Imm8S.  */
10817
125
      overlap.bitfield.imm8s = 0;
10818
125
    }
10819
10820
11.1k
  if (overlap.bitfield.imm8
10821
11.1k
      + overlap.bitfield.imm8s
10822
11.1k
      + overlap.bitfield.imm16
10823
11.1k
      + overlap.bitfield.imm32
10824
11.1k
      + overlap.bitfield.imm32s
10825
11.1k
      + overlap.bitfield.imm64 > 1)
10826
324
    {
10827
324
      static const i386_operand_type imm16 = { .bitfield = { .imm16 = 1 } };
10828
324
      static const i386_operand_type imm32 = { .bitfield = { .imm32 = 1 } };
10829
324
      static const i386_operand_type imm32s = { .bitfield = { .imm32s = 1 } };
10830
324
      static const i386_operand_type imm16_32 = { .bitfield =
10831
324
  { .imm16 = 1, .imm32 = 1 }
10832
324
      };
10833
324
      static const i386_operand_type imm16_32s =  { .bitfield =
10834
324
  { .imm16 = 1, .imm32s = 1 }
10835
324
      };
10836
324
      static const i386_operand_type imm16_32_32s = { .bitfield =
10837
324
  { .imm16 = 1, .imm32 = 1, .imm32s = 1 }
10838
324
      };
10839
10840
324
      if (i.suffix)
10841
318
  {
10842
318
    i386_operand_type temp;
10843
10844
318
    operand_type_set (&temp, 0);
10845
318
    if (i.suffix == BYTE_MNEM_SUFFIX)
10846
175
      {
10847
175
        temp.bitfield.imm8 = overlap.bitfield.imm8;
10848
175
        temp.bitfield.imm8s = overlap.bitfield.imm8s;
10849
175
      }
10850
143
    else if (i.suffix == WORD_MNEM_SUFFIX)
10851
130
      temp.bitfield.imm16 = overlap.bitfield.imm16;
10852
13
    else if (i.suffix == QWORD_MNEM_SUFFIX)
10853
0
      {
10854
0
        temp.bitfield.imm64 = overlap.bitfield.imm64;
10855
0
        temp.bitfield.imm32s = overlap.bitfield.imm32s;
10856
0
      }
10857
13
    else
10858
13
      temp.bitfield.imm32 = overlap.bitfield.imm32;
10859
318
    overlap = temp;
10860
318
  }
10861
6
      else if (operand_type_equal (&overlap, &imm16_32_32s)
10862
6
         || operand_type_equal (&overlap, &imm16_32)
10863
3
         || operand_type_equal (&overlap, &imm16_32s))
10864
6
  {
10865
6
    if ((flag_code == CODE_16BIT)
10866
6
        ^ (i.prefix[DATA_PREFIX] != 0 && !(i.prefix[REX_PREFIX] & REX_W)))
10867
0
      overlap = imm16;
10868
6
    else
10869
6
      overlap = imm32s;
10870
6
  }
10871
0
      else if (i.prefix[REX_PREFIX] & REX_W)
10872
0
  overlap = operand_type_and (overlap, imm32s);
10873
0
      else if (i.prefix[DATA_PREFIX])
10874
0
  overlap = operand_type_and (overlap,
10875
0
            flag_code != CODE_16BIT ? imm16 : imm32);
10876
324
      if (overlap.bitfield.imm8
10877
324
    + overlap.bitfield.imm8s
10878
324
    + overlap.bitfield.imm16
10879
324
    + overlap.bitfield.imm32
10880
324
    + overlap.bitfield.imm32s
10881
324
    + overlap.bitfield.imm64 != 1)
10882
0
  {
10883
0
    as_bad (_("no instruction mnemonic suffix given; "
10884
0
        "can't determine immediate size"));
10885
0
    return 0;
10886
0
  }
10887
324
    }
10888
11.1k
  i.types[j] = overlap;
10889
10890
11.1k
  return 1;
10891
11.1k
}
10892
10893
static int
10894
finalize_imm (void)
10895
10.8k
{
10896
10.8k
  unsigned int j, n;
10897
10898
  /* Update the first 2 immediate operands.  */
10899
10.8k
  n = i.operands > 2 ? 2 : i.operands;
10900
10.8k
  if (n)
10901
9.77k
    {
10902
20.9k
      for (j = 0; j < n; j++)
10903
11.1k
  if (update_imm (j) == 0)
10904
0
    return 0;
10905
10906
      /* The 3rd operand can't be immediate operand.  */
10907
9.77k
      gas_assert (operand_type_check (i.types[2], imm) == 0);
10908
9.77k
    }
10909
10910
10.8k
  return 1;
10911
10.8k
}
10912
10913
static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
10914
         bool do_sse2avx)
10915
332
{
10916
332
  if (r->reg_flags & RegRex)
10917
14
    {
10918
14
      if (i.rex & rex_bit)
10919
0
  as_bad (_("same type of prefix used twice"));
10920
14
      i.rex |= rex_bit;
10921
14
    }
10922
318
  else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
10923
0
    {
10924
0
      gas_assert (i.vex.register_specifier == r);
10925
0
      i.vex.register_specifier += 8;
10926
0
    }
10927
10928
332
  if (r->reg_flags & RegVRex)
10929
0
    i.vrex |= rex_bit;
10930
10931
332
  if (r->reg_flags & RegRex2)
10932
8
    i.rex2 |= rex_bit;
10933
332
}
10934
10935
static INLINE void
10936
set_rex_rex2 (const reg_entry *r, unsigned int rex_bit)
10937
4
{
10938
4
  if ((r->reg_flags & RegRex) != 0)
10939
0
    i.rex |= rex_bit;
10940
4
  if ((r->reg_flags & RegRex2) != 0)
10941
4
    i.rex2 |= rex_bit;
10942
4
}
10943
10944
static int
10945
process_operands (void)
10946
9.88k
{
10947
  /* Default segment register this instruction will use for memory
10948
     accesses.  0 means unknown.  This is only for optimizing out
10949
     unnecessary segment overrides.  */
10950
9.88k
  const reg_entry *default_seg = NULL;
10951
10952
20.0k
  for (unsigned int j = i.imm_operands; j < i.operands; j++)
10953
10.1k
    if (i.types[j].bitfield.instance != InstanceNone)
10954
89
      i.reg_operands--;
10955
10956
9.88k
  if (i.tm.opcode_modifier.sse2avx)
10957
0
    {
10958
      /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
10959
   need converting.  */
10960
0
      i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
10961
0
      i.prefix[REX_PREFIX] = 0;
10962
0
      pp.rex_encoding = 0;
10963
0
      pp.rex2_encoding = 0;
10964
0
    }
10965
  /* ImmExt should be processed after SSE2AVX.  */
10966
9.88k
  else if (i.tm.opcode_modifier.immext)
10967
0
    process_immext ();
10968
10969
  /* TILEZERO is unusual in that it has a single operand encoded in ModR/M.reg,
10970
     not ModR/M.rm.  To avoid special casing this in build_modrm_byte(), fake a
10971
     new destination operand here, while converting the source one to register
10972
     number 0.  */
10973
9.88k
  if (i.tm.mnem_off == MN_tilezero)
10974
0
    {
10975
0
      copy_operand (1, 0);
10976
0
      i.op[0].regs -= i.op[0].regs->reg_num;
10977
0
      i.operands++;
10978
0
      i.reg_operands++;
10979
0
      i.tm.operands++;
10980
0
    }
10981
10982
9.88k
  if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
10983
0
    {
10984
0
      static const i386_operand_type regxmm = {
10985
0
        .bitfield = { .class = RegSIMD, .xmmword = 1 }
10986
0
      };
10987
0
      unsigned int dupl = i.operands;
10988
0
      unsigned int dest = dupl - 1;
10989
0
      unsigned int j;
10990
10991
      /* The destination must be an xmm register.  */
10992
0
      gas_assert (i.reg_operands
10993
0
      && MAX_OPERANDS > dupl
10994
0
      && operand_type_equal (&i.types[dest], &regxmm));
10995
10996
0
      if (i.tm.operand_types[0].bitfield.instance == Accum
10997
0
    && i.tm.operand_types[0].bitfield.xmmword)
10998
0
  {
10999
    /* Keep xmm0 for instructions with VEX prefix and 3
11000
       sources.  */
11001
0
    i.tm.operand_types[0].bitfield.instance = InstanceNone;
11002
0
    i.tm.operand_types[0].bitfield.class = RegSIMD;
11003
0
    i.reg_operands++;
11004
0
    goto duplicate;
11005
0
  }
11006
11007
0
      if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_1ST_XMM0)
11008
0
  {
11009
0
    gas_assert ((MAX_OPERANDS - 1) > dupl);
11010
11011
    /* Add the implicit xmm0 for instructions with VEX prefix
11012
       and 3 sources.  */
11013
0
    for (j = i.operands; j > 0; j--)
11014
0
      copy_operand (j, j - 1);
11015
0
    i.op[0].regs = str_hash_find (reg_hash, "xmm0");
11016
0
    i.types[0] = regxmm;
11017
0
    i.tm.operand_types[0] = regxmm;
11018
11019
0
    i.operands += 2;
11020
0
    i.reg_operands += 2;
11021
0
    i.tm.operands += 2;
11022
11023
0
    dupl++;
11024
0
    dest++;
11025
0
  }
11026
0
      else
11027
0
  {
11028
0
  duplicate:
11029
0
    i.operands++;
11030
0
    i.reg_operands++;
11031
0
    i.tm.operands++;
11032
0
  }
11033
11034
0
      copy_operand (dupl, dest);
11035
11036
0
      if (i.tm.opcode_modifier.immext)
11037
0
  process_immext ();
11038
0
    }
11039
9.88k
  else if (i.tm.operand_types[0].bitfield.instance == Accum
11040
0
     && i.tm.opcode_modifier.modrm)
11041
0
    {
11042
0
      unsigned int j;
11043
11044
0
      for (j = 1; j < i.operands; j++)
11045
0
  copy_operand (j - 1, j);
11046
11047
      /* No adjustment to i.reg_operands: This was already done at the top
11048
   of the function.  */
11049
0
      i.operands--;
11050
0
      i.tm.operands--;
11051
0
    }
11052
9.88k
  else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_GROUP)
11053
0
    {
11054
0
      unsigned int op, extra;
11055
0
      const reg_entry *first;
11056
11057
      /* The second operand must be {x,y,z,t}mmN.  */
11058
0
      gas_assert ((i.operands == 2 || i.operands == 3)
11059
0
      && i.types[1].bitfield.class == RegSIMD);
11060
11061
0
      switch (i.types[i.operands - 1].bitfield.class)
11062
0
  {
11063
0
  case RegSIMD:
11064
0
    op = 1;
11065
0
    if (i.operands == 2)
11066
0
      {
11067
        /* AMX-TRANSPOSE operand 2: N must be a multiple of 2. */
11068
0
        extra = 1;
11069
0
      }
11070
0
    else
11071
0
      {
11072
        /* AVX512-{4FMAPS,4VNNIW} operand 2: N must be a multiple of 4. */
11073
0
        extra = 3;
11074
0
      }
11075
0
    break;
11076
11077
0
  case RegMask:
11078
    /* AVX512-VP2INTERSECT operand 3: N must be a multiple of 2. */
11079
0
    op = 2;
11080
0
    extra = 1;
11081
0
    break;
11082
11083
0
  default:
11084
0
    abort ();
11085
0
  }
11086
11087
0
      first = i.op[op].regs - (register_number (i.op[op].regs) & extra);
11088
0
      if (i.op[op].regs != first)
11089
0
  as_warn (_("operand %u `%s%s' implicitly denotes"
11090
0
       " `%s%s' to `%s%s' group in `%s'"),
11091
0
     intel_syntax ? i.operands - op : op + 1,
11092
0
     register_prefix, i.op[op].regs->reg_name,
11093
0
     register_prefix, first[0].reg_name,
11094
0
     register_prefix, first[extra].reg_name,
11095
0
     insn_name (&i.tm));
11096
0
    }
11097
9.88k
  else if (i.tm.opcode_modifier.operandconstraint == REG_KLUDGE)
11098
0
    {
11099
      /* The imul $imm, %reg instruction is converted into
11100
   imul $imm, %reg, %reg, and the clr %reg instruction
11101
   is converted into xor %reg, %reg.  */
11102
11103
0
      unsigned int first_reg_op;
11104
11105
0
      if (operand_type_check (i.types[0], reg))
11106
0
  first_reg_op = 0;
11107
0
      else
11108
0
  first_reg_op = 1;
11109
      /* Pretend we saw the extra register operand.  */
11110
0
      gas_assert (i.reg_operands == 1
11111
0
      && i.op[first_reg_op + 1].regs == 0);
11112
0
      i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
11113
0
      i.types[first_reg_op + 1] = i.types[first_reg_op];
11114
0
      i.operands++;
11115
0
      i.reg_operands++;
11116
11117
      /* For IMULZU switch around the constraint.  */
11118
0
      if (i.tm.mnem_off == MN_imulzu)
11119
0
  i.tm.opcode_modifier.operandconstraint = ZERO_UPPER;
11120
0
    }
11121
11122
9.88k
  if (i.tm.opcode_modifier.modrm)
11123
9.47k
    {
11124
      /* The opcode is completed (modulo i.tm.extension_opcode which
11125
   must be put into the modrm byte).  Now, we make the modrm and
11126
   index base bytes based on all the info we've collected.  */
11127
11128
9.47k
      default_seg = build_modrm_byte ();
11129
11130
9.47k
      if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
11131
0
  {
11132
    /* Warn about some common errors, but press on regardless.  */
11133
0
    if (i.operands == 2)
11134
0
      {
11135
        /* Reversed arguments on faddp or fmulp.  */
11136
0
        as_warn (_("translating to `%s %s%s,%s%s'"), insn_name (&i.tm),
11137
0
           register_prefix, i.op[!intel_syntax].regs->reg_name,
11138
0
           register_prefix, i.op[intel_syntax].regs->reg_name);
11139
0
      }
11140
0
    else if (i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
11141
0
      {
11142
        /* Extraneous `l' suffix on fp insn.  */
11143
0
        as_warn (_("translating to `%s %s%s'"), insn_name (&i.tm),
11144
0
           register_prefix, i.op[0].regs->reg_name);
11145
0
      }
11146
0
  }
11147
9.47k
    }
11148
407
  else if (i.types[0].bitfield.class == SReg && !dot_insn ())
11149
0
    {
11150
0
      if (flag_code != CODE_64BIT
11151
0
    ? i.tm.base_opcode == POP_SEG_SHORT
11152
0
      && i.op[0].regs->reg_num == 1
11153
0
    : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
11154
0
      && i.op[0].regs->reg_num < 4)
11155
0
  {
11156
0
    as_bad (_("you can't `%s %s%s'"),
11157
0
      insn_name (&i.tm), register_prefix, i.op[0].regs->reg_name);
11158
0
    return 0;
11159
0
  }
11160
0
      if (i.op[0].regs->reg_num > 3
11161
0
    && i.tm.opcode_space == SPACE_BASE )
11162
0
  {
11163
0
    i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
11164
0
    i.tm.opcode_space = SPACE_0F;
11165
0
  }
11166
0
      i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
11167
0
    }
11168
407
  else if (i.tm.opcode_space == SPACE_BASE
11169
403
     && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
11170
85
    {
11171
85
      default_seg = reg_ds;
11172
85
    }
11173
322
  else if (i.tm.opcode_modifier.isstring)
11174
47
    {
11175
      /* For the string instructions that allow a segment override
11176
   on one of their operands, the default segment is ds.  */
11177
47
      default_seg = reg_ds;
11178
47
    }
11179
275
  else if (i.short_form)
11180
30
    {
11181
      /* The register operand is in the 1st or 2nd non-immediate operand.  */
11182
30
      const reg_entry *r = i.op[i.imm_operands].regs;
11183
11184
30
      if (!dot_insn ()
11185
12
    && r->reg_type.bitfield.instance == Accum
11186
0
    && i.op[i.imm_operands + 1].regs)
11187
0
  r = i.op[i.imm_operands + 1].regs;
11188
      /* Register goes in low 3 bits of opcode.  */
11189
30
      i.tm.base_opcode |= r->reg_num;
11190
30
      set_rex_vrex (r, REX_B, false);
11191
11192
30
      if (dot_insn () && i.reg_operands == 2)
11193
0
  {
11194
0
    gas_assert (is_any_vex_encoding (&i.tm)
11195
0
          || pp.encoding != encoding_default);
11196
0
    i.vex.register_specifier = i.op[i.operands - 1].regs;
11197
0
  }
11198
30
    }
11199
245
  else if (i.reg_operands == 1
11200
4
     && !i.flags[i.operands - 1]
11201
4
     && i.tm.operand_types[i.operands - 1].bitfield.instance
11202
4
        == InstanceNone)
11203
4
    {
11204
4
      gas_assert (is_any_vex_encoding (&i.tm)
11205
4
      || pp.encoding != encoding_default);
11206
4
      i.vex.register_specifier = i.op[i.operands - 1].regs;
11207
4
    }
11208
11209
9.88k
  if ((i.seg[0] || i.prefix[SEG_PREFIX])
11210
22
      && i.tm.mnem_off == MN_lea)
11211
0
    {
11212
0
      if (!quiet_warnings)
11213
0
  as_warn (_("segment override on `%s' is ineffectual"), insn_name (&i.tm));
11214
0
      if (optimize && !pp.no_optimize)
11215
0
  {
11216
0
    i.seg[0] = NULL;
11217
0
    i.prefix[SEG_PREFIX] = 0;
11218
0
  }
11219
0
    }
11220
11221
  /* If a segment was explicitly specified, and the specified segment
11222
     is neither the default nor the one already recorded from a prefix,
11223
     use an opcode prefix to select it.  If we never figured out what
11224
     the default segment is, then default_seg will be zero at this
11225
     point, and the specified segment prefix will always be used.  */
11226
9.88k
  if (i.seg[0]
11227
5
      && i.seg[0] != default_seg
11228
5
      && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
11229
5
    {
11230
5
      if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
11231
0
  return 0;
11232
5
    }
11233
9.88k
  return 1;
11234
9.88k
}
11235
11236
static const reg_entry *
11237
build_modrm_byte (void)
11238
9.47k
{
11239
9.47k
  const reg_entry *default_seg = NULL;
11240
9.47k
  unsigned int source = i.imm_operands - i.tm.opcode_modifier.immext
11241
      /* Compensate for kludge in md_assemble().  */
11242
9.47k
      + i.tm.operand_types[0].bitfield.imm1;
11243
9.47k
  unsigned int dest = i.operands - 1 - i.tm.opcode_modifier.immext;
11244
9.47k
  unsigned int v, op, reg_slot;
11245
11246
  /* Accumulator (in particular %st), shift count (%cl), and alike need
11247
     to be skipped just like immediate operands do.  */
11248
9.47k
  if (i.tm.operand_types[source].bitfield.instance)
11249
0
    ++source;
11250
9.47k
  while (i.tm.operand_types[dest].bitfield.instance)
11251
0
    --dest;
11252
11253
9.64k
  for (op = source; op < i.operands; ++op)
11254
9.63k
    if (i.tm.operand_types[op].bitfield.baseindex)
11255
9.46k
      break;
11256
11257
9.47k
  if (i.reg_operands + i.mem_operands + (i.tm.extension_opcode != None)
11258
9.47k
      + (i.tm.opcode_modifier.operandconstraint == SCC) == 4)
11259
0
    {
11260
0
      expressionS *exp;
11261
11262
      /* There are 2 kinds of instructions:
11263
   1. 5 operands: 4 register operands or 3 register operands
11264
   plus 1 memory operand plus one Imm4 operand, VexXDS, and
11265
   VexW0 or VexW1.  The destination must be either XMM, YMM or
11266
   ZMM register.
11267
   2. 4 operands: 4 register operands or 3 register operands
11268
   plus 1 memory operand, with VexXDS.
11269
   3. Other equivalent combinations when coming from s_insn().  */
11270
0
      if (!dot_insn ())
11271
0
  {
11272
0
    gas_assert (i.tm.opcode_modifier.vexvvvv
11273
0
          && i.tm.opcode_modifier.vexw);
11274
0
    gas_assert (i.tm.operand_types[dest].bitfield.class == RegSIMD);
11275
0
  }
11276
11277
      /* Of the first two non-immediate operands the one with the template
11278
   not allowing for a memory one is encoded in the immediate operand.  */
11279
0
      if (source == op)
11280
0
  reg_slot = source + 1;
11281
0
      else
11282
0
  reg_slot = source++;
11283
11284
0
      if (!dot_insn ())
11285
0
  {
11286
0
    gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
11287
0
    gas_assert (!(i.op[reg_slot].regs->reg_flags & RegVRex));
11288
0
  }
11289
0
      else
11290
0
  gas_assert (i.tm.operand_types[reg_slot].bitfield.class != ClassNone);
11291
11292
0
      if (i.imm_operands == 0)
11293
0
  {
11294
    /* When there is no immediate operand, generate an 8bit
11295
       immediate operand to encode the first operand.  */
11296
0
    exp = &im_expressions[i.imm_operands++];
11297
0
    i.op[i.operands].imms = exp;
11298
0
    i.types[i.operands].bitfield.imm8 = 1;
11299
0
    i.operands++;
11300
11301
0
    exp->X_op = O_constant;
11302
0
  }
11303
0
      else
11304
0
  {
11305
0
    gas_assert (i.imm_operands == 1);
11306
0
    gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
11307
0
    gas_assert (!i.tm.opcode_modifier.immext);
11308
11309
    /* Turn on Imm8 again so that output_imm will generate it.  */
11310
0
    i.types[0].bitfield.imm8 = 1;
11311
11312
0
    exp = i.op[0].imms;
11313
0
  }
11314
0
      exp->X_add_number |= register_number (i.op[reg_slot].regs)
11315
0
         << (3 + !(i.tm.opcode_modifier.evex
11316
0
             || pp.encoding == encoding_evex));
11317
0
    }
11318
11319
9.47k
  switch (i.tm.opcode_modifier.vexvvvv)
11320
9.47k
    {
11321
    /* VEX.vvvv encodes the last source register operand.  */
11322
0
    case VexVVVV_SRC2:
11323
0
      v = source++;
11324
0
      break;
11325
    /* VEX.vvvv encodes the first source register operand.  */
11326
0
    case VexVVVV_SRC1:
11327
0
      v =  dest - 1;
11328
0
      break;
11329
    /* VEX.vvvv encodes the destination register operand.  */
11330
3
    case VexVVVV_DST:
11331
3
      v = dest--;
11332
3
      break;
11333
9.47k
    default:
11334
9.47k
      v = ~0;
11335
9.47k
      break;
11336
9.47k
     }
11337
11338
9.47k
  if (dest == source)
11339
9.27k
    dest = ~0;
11340
11341
9.47k
  gas_assert (source < dest);
11342
11343
9.47k
  if (v < MAX_OPERANDS)
11344
3
    {
11345
3
      gas_assert (i.tm.opcode_modifier.vexvvvv);
11346
3
      i.vex.register_specifier = i.op[v].regs;
11347
3
    }
11348
11349
9.47k
  if (op < i.operands)
11350
9.46k
    {
11351
9.46k
      if (i.mem_operands)
11352
9.37k
  {
11353
9.37k
    unsigned int fake_zero_displacement = 0;
11354
11355
9.37k
    gas_assert (i.flags[op] & Operand_Mem);
11356
11357
9.37k
    if (i.tm.opcode_modifier.sib)
11358
0
      {
11359
        /* The index register of VSIB shouldn't be RegIZ.  */
11360
0
        if (i.tm.opcode_modifier.sib != SIBMEM
11361
0
      && i.index_reg->reg_num == RegIZ)
11362
0
    abort ();
11363
11364
0
        i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
11365
0
        if (!i.base_reg)
11366
0
    {
11367
0
      i.sib.base = NO_BASE_REGISTER;
11368
0
      i.sib.scale = i.log2_scale_factor;
11369
0
      i.types[op] = operand_type_and_not (i.types[op], anydisp);
11370
0
      i.types[op].bitfield.disp32 = 1;
11371
0
    }
11372
11373
        /* Since the mandatory SIB always has index register, so
11374
     the code logic remains unchanged. The non-mandatory SIB
11375
     without index register is allowed and will be handled
11376
     later.  */
11377
0
        if (i.index_reg)
11378
0
    {
11379
0
      if (i.index_reg->reg_num == RegIZ)
11380
0
        i.sib.index = NO_INDEX_REGISTER;
11381
0
      else
11382
0
        i.sib.index = i.index_reg->reg_num;
11383
0
      set_rex_vrex (i.index_reg, REX_X, false);
11384
0
    }
11385
0
      }
11386
11387
9.37k
    default_seg = reg_ds;
11388
11389
9.37k
    if (i.base_reg == 0)
11390
9.37k
      {
11391
9.37k
        i.rm.mode = 0;
11392
9.37k
        if (!i.disp_operands)
11393
0
    fake_zero_displacement = 1;
11394
9.37k
        if (i.index_reg == 0)
11395
9.37k
    {
11396
      /* Both check for VSIB and mandatory non-vector SIB. */
11397
9.37k
      gas_assert (!i.tm.opcode_modifier.sib
11398
9.37k
            || i.tm.opcode_modifier.sib == SIBMEM);
11399
      /* Operand is just <disp>  */
11400
9.37k
      i.types[op] = operand_type_and_not (i.types[op], anydisp);
11401
9.37k
      if (flag_code == CODE_64BIT)
11402
8.82k
        {
11403
          /* 64bit mode overwrites the 32bit absolute
11404
       addressing by RIP relative addressing and
11405
       absolute addressing is encoded by one of the
11406
       redundant SIB forms.  */
11407
8.82k
          i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
11408
8.82k
          i.sib.base = NO_BASE_REGISTER;
11409
8.82k
          i.sib.index = NO_INDEX_REGISTER;
11410
8.82k
          i.types[op].bitfield.disp32 = 1;
11411
8.82k
        }
11412
546
      else if ((flag_code == CODE_16BIT)
11413
546
         ^ (i.prefix[ADDR_PREFIX] != 0))
11414
545
        {
11415
545
          i.rm.regmem = NO_BASE_REGISTER_16;
11416
545
          i.types[op].bitfield.disp16 = 1;
11417
545
        }
11418
1
      else
11419
1
        {
11420
1
          i.rm.regmem = NO_BASE_REGISTER;
11421
1
          i.types[op].bitfield.disp32 = 1;
11422
1
        }
11423
9.37k
    }
11424
0
        else if (!i.tm.opcode_modifier.sib)
11425
0
    {
11426
      /* !i.base_reg && i.index_reg  */
11427
0
      if (i.index_reg->reg_num == RegIZ)
11428
0
        i.sib.index = NO_INDEX_REGISTER;
11429
0
      else
11430
0
        i.sib.index = i.index_reg->reg_num;
11431
0
      i.sib.base = NO_BASE_REGISTER;
11432
0
      i.sib.scale = i.log2_scale_factor;
11433
0
      i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
11434
0
      i.types[op] = operand_type_and_not (i.types[op], anydisp);
11435
0
      i.types[op].bitfield.disp32 = 1;
11436
0
      set_rex_rex2 (i.index_reg, REX_X);
11437
0
    }
11438
9.37k
      }
11439
    /* RIP addressing for 64bit mode.  */
11440
4
    else if (i.base_reg->reg_num == RegIP)
11441
0
      {
11442
0
        gas_assert (!i.tm.opcode_modifier.sib);
11443
0
        i.rm.regmem = NO_BASE_REGISTER;
11444
0
        i.types[op].bitfield.disp8 = 0;
11445
0
        i.types[op].bitfield.disp16 = 0;
11446
0
        i.types[op].bitfield.disp32 = 1;
11447
0
        i.types[op].bitfield.disp64 = 0;
11448
0
        i.flags[op] |= Operand_PCrel;
11449
0
        if (! i.disp_operands)
11450
0
    fake_zero_displacement = 1;
11451
0
      }
11452
4
    else if (i.base_reg->reg_type.bitfield.word)
11453
0
      {
11454
0
        gas_assert (!i.tm.opcode_modifier.sib);
11455
0
        switch (i.base_reg->reg_num)
11456
0
    {
11457
0
    case 3: /* (%bx)  */
11458
0
      if (i.index_reg == 0)
11459
0
        i.rm.regmem = 7;
11460
0
      else /* (%bx,%si) -> 0, or (%bx,%di) -> 1  */
11461
0
        i.rm.regmem = i.index_reg->reg_num - 6;
11462
0
      break;
11463
0
    case 5: /* (%bp)  */
11464
0
      default_seg = reg_ss;
11465
0
      if (i.index_reg == 0)
11466
0
        {
11467
0
          i.rm.regmem = 6;
11468
0
          if (operand_type_check (i.types[op], disp) == 0)
11469
0
      {
11470
        /* fake (%bp) into 0(%bp)  */
11471
0
        if (pp.disp_encoding == disp_encoding_16bit)
11472
0
          i.types[op].bitfield.disp16 = 1;
11473
0
        else
11474
0
          i.types[op].bitfield.disp8 = 1;
11475
0
        fake_zero_displacement = 1;
11476
0
      }
11477
0
        }
11478
0
      else /* (%bp,%si) -> 2, or (%bp,%di) -> 3  */
11479
0
        i.rm.regmem = i.index_reg->reg_num - 6 + 2;
11480
0
      break;
11481
0
    default: /* (%si) -> 4 or (%di) -> 5  */
11482
0
      i.rm.regmem = i.base_reg->reg_num - 6 + 4;
11483
0
    }
11484
0
        if (!fake_zero_displacement
11485
0
      && !i.disp_operands
11486
0
      && pp.disp_encoding)
11487
0
    {
11488
0
      fake_zero_displacement = 1;
11489
0
      if (pp.disp_encoding == disp_encoding_8bit)
11490
0
        i.types[op].bitfield.disp8 = 1;
11491
0
      else
11492
0
        i.types[op].bitfield.disp16 = 1;
11493
0
    }
11494
0
        i.rm.mode = mode_from_disp_size (i.types[op]);
11495
0
      }
11496
4
    else /* i.base_reg and 32/64 bit mode  */
11497
4
      {
11498
4
        if (operand_type_check (i.types[op], disp))
11499
2
    {
11500
2
      i.types[op].bitfield.disp16 = 0;
11501
2
      i.types[op].bitfield.disp64 = 0;
11502
2
      i.types[op].bitfield.disp32 = 1;
11503
2
    }
11504
11505
4
        if (!i.tm.opcode_modifier.sib)
11506
4
    i.rm.regmem = i.base_reg->reg_num;
11507
4
        set_rex_rex2 (i.base_reg, REX_B);
11508
4
        i.sib.base = i.base_reg->reg_num;
11509
        /* x86-64 ignores REX prefix bit here to avoid decoder
11510
     complications.  */
11511
4
        if (!(i.base_reg->reg_flags & RegRex)
11512
4
      && (i.base_reg->reg_num == EBP_REG_NUM
11513
4
       || i.base_reg->reg_num == ESP_REG_NUM))
11514
0
      default_seg = reg_ss;
11515
4
        if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
11516
0
    {
11517
0
      fake_zero_displacement = 1;
11518
0
      if (pp.disp_encoding == disp_encoding_32bit)
11519
0
        i.types[op].bitfield.disp32 = 1;
11520
0
      else
11521
0
        i.types[op].bitfield.disp8 = 1;
11522
0
    }
11523
4
        i.sib.scale = i.log2_scale_factor;
11524
4
        if (i.index_reg == 0)
11525
4
    {
11526
      /* Only check for VSIB. */
11527
4
      gas_assert (i.tm.opcode_modifier.sib != VECSIB128
11528
4
            && i.tm.opcode_modifier.sib != VECSIB256
11529
4
            && i.tm.opcode_modifier.sib != VECSIB512);
11530
11531
      /* <disp>(%esp) becomes two byte modrm with no index
11532
         register.  We've already stored the code for esp
11533
         in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
11534
         Any base register besides %esp will not use the
11535
         extra modrm byte.  */
11536
4
      i.sib.index = NO_INDEX_REGISTER;
11537
4
    }
11538
0
        else if (!i.tm.opcode_modifier.sib)
11539
0
    {
11540
0
      if (i.index_reg->reg_num == RegIZ)
11541
0
        i.sib.index = NO_INDEX_REGISTER;
11542
0
      else
11543
0
        i.sib.index = i.index_reg->reg_num;
11544
0
      i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
11545
0
      set_rex_rex2 (i.index_reg, REX_X);
11546
0
    }
11547
11548
4
        if (i.disp_operands
11549
2
      && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
11550
2
          || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
11551
0
    i.rm.mode = 0;
11552
4
        else
11553
4
    {
11554
4
      if (!fake_zero_displacement
11555
4
          && !i.disp_operands
11556
2
          && pp.disp_encoding)
11557
0
        {
11558
0
          fake_zero_displacement = 1;
11559
0
          if (pp.disp_encoding == disp_encoding_8bit)
11560
0
      i.types[op].bitfield.disp8 = 1;
11561
0
          else
11562
0
      i.types[op].bitfield.disp32 = 1;
11563
0
        }
11564
4
      i.rm.mode = mode_from_disp_size (i.types[op]);
11565
4
    }
11566
4
      }
11567
11568
9.37k
    if (fake_zero_displacement)
11569
0
      {
11570
        /* Fakes a zero displacement assuming that i.types[op]
11571
     holds the correct displacement size.  */
11572
0
        expressionS *exp;
11573
11574
0
        gas_assert (i.op[op].disps == 0);
11575
0
        exp = &disp_expressions[i.disp_operands++];
11576
0
        i.op[op].disps = exp;
11577
0
        exp->X_op = O_constant;
11578
0
        exp->X_add_number = 0;
11579
0
        exp->X_add_symbol = NULL;
11580
0
        exp->X_op_symbol = NULL;
11581
0
      }
11582
9.37k
  }
11583
88
    else
11584
88
  {
11585
88
      i.rm.mode = 3;
11586
88
      i.rm.regmem = i.op[op].regs->reg_num;
11587
88
      set_rex_vrex (i.op[op].regs, REX_B, false);
11588
88
  }
11589
11590
9.46k
      if (op == dest)
11591
145
  dest = ~0;
11592
9.46k
      if (op == source)
11593
9.32k
  source = ~0;
11594
9.46k
    }
11595
14
  else
11596
14
    {
11597
14
      i.rm.mode = 3;
11598
14
      if (!i.tm.opcode_modifier.regmem)
11599
6
  {
11600
6
    gas_assert (source < MAX_OPERANDS);
11601
6
    i.rm.regmem = i.op[source].regs->reg_num;
11602
6
    set_rex_vrex (i.op[source].regs, REX_B,
11603
6
      dest >= MAX_OPERANDS && i.tm.opcode_modifier.sse2avx);
11604
6
    source = ~0;
11605
6
  }
11606
8
      else
11607
8
  {
11608
8
    gas_assert (dest < MAX_OPERANDS);
11609
8
    i.rm.regmem = i.op[dest].regs->reg_num;
11610
8
    set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
11611
8
    dest = ~0;
11612
8
  }
11613
14
    }
11614
11615
  /* Fill in i.rm.reg field with extension opcode (if any) or the
11616
     appropriate register.  */
11617
9.47k
  if (i.tm.extension_opcode != None)
11618
9.27k
    i.rm.reg = i.tm.extension_opcode;
11619
200
  else if (!i.tm.opcode_modifier.regmem && dest < MAX_OPERANDS)
11620
47
    {
11621
47
      i.rm.reg = i.op[dest].regs->reg_num;
11622
47
      set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
11623
47
    }
11624
153
  else
11625
153
    {
11626
153
      gas_assert (source < MAX_OPERANDS);
11627
153
      i.rm.reg = i.op[source].regs->reg_num;
11628
153
      set_rex_vrex (i.op[source].regs, REX_R, false);
11629
153
    }
11630
11631
9.47k
  if (flag_code != CODE_64BIT && (i.rex & REX_R))
11632
0
    {
11633
0
      gas_assert (i.types[!i.tm.opcode_modifier.regmem].bitfield.class == RegCR);
11634
0
      i.rex &= ~REX_R;
11635
0
      add_prefix (LOCK_PREFIX_OPCODE);
11636
0
    }
11637
11638
9.47k
  return default_seg;
11639
9.47k
}
11640
11641
static INLINE void
11642
frag_opcode_byte (unsigned char byte)
11643
18.8k
{
11644
18.8k
  if (now_seg != absolute_section)
11645
18.5k
    FRAG_APPEND_1_CHAR (byte);
11646
301
  else
11647
301
    ++abs_section_offset;
11648
18.8k
}
11649
11650
static unsigned int
11651
flip_code16 (unsigned int code16)
11652
66
{
11653
66
  gas_assert (i.tm.operands == 1);
11654
11655
66
  return !(i.prefix[REX_PREFIX] & REX_W)
11656
66
   && (code16 ? i.tm.operand_types[0].bitfield.disp32
11657
66
        : i.tm.operand_types[0].bitfield.disp16)
11658
66
   ? CODE16 : 0;
11659
66
}
11660
11661
static void
11662
output_branch (void)
11663
47
{
11664
47
  char *p;
11665
47
  int size;
11666
47
  int code16;
11667
47
  int prefix;
11668
47
  relax_substateT subtype;
11669
47
  symbolS *sym;
11670
47
  offsetT off;
11671
11672
47
  if (now_seg == absolute_section)
11673
1
    {
11674
1
      as_bad (_("relaxable branches not supported in absolute section"));
11675
1
      return;
11676
1
    }
11677
11678
46
  code16 = flag_code == CODE_16BIT ? CODE16 : 0;
11679
46
  size = pp.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
11680
11681
46
  prefix = 0;
11682
46
  if (i.prefix[DATA_PREFIX] != 0)
11683
2
    {
11684
2
      prefix = 1;
11685
2
      i.prefixes -= 1;
11686
2
      code16 ^= flip_code16(code16);
11687
2
    }
11688
  /* Pentium4 branch hints.  */
11689
46
  if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
11690
39
      || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
11691
7
    {
11692
7
      prefix++;
11693
7
      i.prefixes--;
11694
7
    }
11695
46
  if (i.prefix[REX_PREFIX] != 0)
11696
0
    {
11697
0
      prefix++;
11698
0
      i.prefixes--;
11699
0
    }
11700
11701
  /* BND prefixed jump.  */
11702
46
  if (i.prefix[BND_PREFIX] != 0)
11703
0
    {
11704
0
      prefix++;
11705
0
      i.prefixes--;
11706
0
    }
11707
11708
46
  if (i.prefixes != 0)
11709
0
    as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
11710
11711
  /* It's always a symbol;  End frag & setup for relax.
11712
     Make sure there is enough room in this frag for the largest
11713
     instruction we may generate in md_convert_frag.  This is 2
11714
     bytes for the opcode and room for the prefix and largest
11715
     displacement.  */
11716
46
  frag_grow (prefix + 2 + 4);
11717
  /* Prefix and 1 opcode byte go in fr_fix.  */
11718
46
  p = frag_more (prefix + 1);
11719
46
  if (i.prefix[DATA_PREFIX] != 0)
11720
2
    *p++ = DATA_PREFIX_OPCODE;
11721
46
  if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
11722
39
      || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
11723
7
    *p++ = i.prefix[SEG_PREFIX];
11724
46
  if (i.prefix[BND_PREFIX] != 0)
11725
0
    *p++ = BND_PREFIX_OPCODE;
11726
46
  if (i.prefix[REX_PREFIX] != 0)
11727
0
    *p++ = i.prefix[REX_PREFIX];
11728
46
  *p = i.tm.base_opcode;
11729
11730
46
  if ((unsigned char) *p == JUMP_PC_RELATIVE)
11731
32
    subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
11732
14
  else if (cpu_arch_flags.bitfield.cpui386)
11733
14
    subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
11734
0
  else
11735
0
    subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
11736
46
  subtype |= code16;
11737
11738
46
  sym = i.op[0].disps->X_add_symbol;
11739
46
  off = i.op[0].disps->X_add_number;
11740
11741
46
  if (i.op[0].disps->X_op != O_constant
11742
46
      && i.op[0].disps->X_op != O_symbol)
11743
1
    {
11744
      /* Handle complex expressions.  */
11745
1
      sym = make_expr_symbol (i.op[0].disps);
11746
1
      off = 0;
11747
1
    }
11748
11749
  /* 1 possible extra opcode + 4 byte displacement go in var part.
11750
     Pass reloc in fr_var.  */
11751
46
  frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
11752
46
}
11753
11754
/* PLT32 relocation is ELF only.  */
11755
#ifdef OBJ_ELF
11756
/* Return TRUE iff PLT32 relocation should be used for branching to
11757
   symbol S.  */
11758
11759
static bool
11760
need_plt32_p (symbolS *s)
11761
43
{
11762
#ifdef TE_SOLARIS
11763
  /* Don't emit PLT32 relocation on Solaris: neither native linker nor
11764
     krtld support it.  */
11765
  return false;
11766
#endif
11767
11768
  /* Since there is no need to prepare for PLT branch on x86-64, we
11769
     can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
11770
     be used as a marker for 32-bit PC-relative branches.  */
11771
43
  if (!object_64bit)
11772
0
    return false;
11773
11774
43
  if (s == NULL)
11775
0
    return false;
11776
11777
  /* Weak or undefined symbol need PLT32 relocation.  */
11778
43
  if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
11779
43
    return true;
11780
11781
  /* Non-global symbol doesn't need PLT32 relocation.  */
11782
0
  if (! S_IS_EXTERNAL (s))
11783
0
    return false;
11784
11785
  /* Other global symbols need PLT32 relocation.  NB: Symbol with
11786
     non-default visibilities are treated as normal global symbol
11787
     so that PLT32 relocation can be used as a marker for 32-bit
11788
     PC-relative branches.  It is useful for linker relaxation.  */
11789
0
  return true;
11790
0
}
11791
#endif
11792
11793
static void
11794
output_jump (void)
11795
164
{
11796
164
  char *p;
11797
164
  int size;
11798
164
  fixS *fixP;
11799
164
  bfd_reloc_code_real_type jump_reloc = i.reloc[0];
11800
11801
164
  if (i.tm.opcode_modifier.jump == JUMP_BYTE)
11802
10
    {
11803
      /* This is a loop or jecxz type instruction.  */
11804
10
      size = 1;
11805
10
      if (i.prefix[ADDR_PREFIX] != 0)
11806
10
  {
11807
10
    frag_opcode_byte (ADDR_PREFIX_OPCODE);
11808
10
    i.prefixes -= 1;
11809
10
  }
11810
      /* Pentium4 branch hints.  */
11811
10
      if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
11812
0
    || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
11813
10
  {
11814
10
    frag_opcode_byte (i.prefix[SEG_PREFIX]);
11815
10
    i.prefixes--;
11816
10
  }
11817
10
    }
11818
154
  else
11819
154
    {
11820
154
      int code16;
11821
11822
154
      code16 = 0;
11823
154
      if (flag_code == CODE_16BIT)
11824
0
  code16 = CODE16;
11825
11826
154
      if (i.prefix[DATA_PREFIX] != 0)
11827
64
  {
11828
64
    frag_opcode_byte (DATA_PREFIX_OPCODE);
11829
64
    i.prefixes -= 1;
11830
64
    code16 ^= flip_code16(code16);
11831
64
  }
11832
11833
154
      size = 4;
11834
154
      if (code16)
11835
64
  size = 2;
11836
154
    }
11837
11838
  /* BND prefixed jump.  */
11839
164
  if (i.prefix[BND_PREFIX] != 0)
11840
0
    {
11841
0
      frag_opcode_byte (i.prefix[BND_PREFIX]);
11842
0
      i.prefixes -= 1;
11843
0
    }
11844
11845
164
  if (i.prefix[REX_PREFIX] != 0)
11846
4
    {
11847
4
      frag_opcode_byte (i.prefix[REX_PREFIX]);
11848
4
      i.prefixes -= 1;
11849
4
    }
11850
11851
164
  if (i.prefixes != 0)
11852
0
    as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
11853
11854
164
  if (now_seg == absolute_section)
11855
74
    {
11856
74
      abs_section_offset += i.opcode_length + size;
11857
74
      return;
11858
74
    }
11859
11860
90
  p = frag_more (i.opcode_length + size);
11861
90
  switch (i.opcode_length)
11862
90
    {
11863
0
    case 2:
11864
0
      *p++ = i.tm.base_opcode >> 8;
11865
      /* Fall through.  */
11866
90
    case 1:
11867
90
      *p++ = i.tm.base_opcode;
11868
90
      break;
11869
0
    default:
11870
0
      abort ();
11871
90
    }
11872
11873
90
#ifdef OBJ_ELF
11874
90
  if (flag_code == CODE_64BIT && size == 4
11875
55
      && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
11876
43
      && need_plt32_p (i.op[0].disps->X_add_symbol))
11877
43
    jump_reloc = BFD_RELOC_32_PLT_PCREL;
11878
90
#endif
11879
11880
90
  jump_reloc = reloc (size, 1, 1, jump_reloc);
11881
11882
90
  fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
11883
90
          i.op[0].disps, 1, jump_reloc);
11884
11885
  /* All jumps handled here are signed, but don't unconditionally use a
11886
     signed limit check for 32 and 16 bit jumps as we want to allow wrap
11887
     around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
11888
     respectively.  */
11889
90
  switch (size)
11890
90
    {
11891
10
    case 1:
11892
10
      fixP->fx_signed = 1;
11893
10
      break;
11894
11895
25
    case 2:
11896
25
      if (i.tm.mnem_off == MN_xbegin)
11897
0
  fixP->fx_signed = 1;
11898
25
      break;
11899
11900
55
    case 4:
11901
55
      if (flag_code == CODE_64BIT)
11902
55
  fixP->fx_signed = 1;
11903
55
      break;
11904
90
    }
11905
90
}
11906
11907
static void
11908
output_interseg_jump (void)
11909
8
{
11910
8
  char *p;
11911
8
  int size;
11912
8
  int prefix;
11913
8
  int code16;
11914
11915
8
  code16 = 0;
11916
8
  if (flag_code == CODE_16BIT)
11917
5
    code16 = CODE16;
11918
11919
8
  prefix = 0;
11920
8
  if (i.prefix[DATA_PREFIX] != 0)
11921
0
    {
11922
0
      prefix = 1;
11923
0
      i.prefixes -= 1;
11924
0
      code16 ^= CODE16;
11925
0
    }
11926
11927
8
  gas_assert (!i.prefix[REX_PREFIX]);
11928
11929
8
  size = 4;
11930
8
  if (code16)
11931
5
    size = 2;
11932
11933
8
  if (i.prefixes != 0)
11934
0
    as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
11935
11936
8
  if (now_seg == absolute_section)
11937
0
    {
11938
0
      abs_section_offset += prefix + 1 + 2 + size;
11939
0
      return;
11940
0
    }
11941
11942
  /* 1 opcode; 2 segment; offset  */
11943
8
  p = frag_more (prefix + 1 + 2 + size);
11944
11945
8
  if (i.prefix[DATA_PREFIX] != 0)
11946
0
    *p++ = DATA_PREFIX_OPCODE;
11947
11948
8
  if (i.prefix[REX_PREFIX] != 0)
11949
0
    *p++ = i.prefix[REX_PREFIX];
11950
11951
8
  *p++ = i.tm.base_opcode;
11952
8
  if (i.op[1].imms->X_op == O_constant)
11953
8
    {
11954
8
      offsetT n = i.op[1].imms->X_add_number;
11955
11956
8
      if (size == 2
11957
5
    && !fits_in_unsigned_word (n)
11958
5
    && !fits_in_signed_word (n))
11959
5
  {
11960
5
    as_bad (_("16-bit jump out of range"));
11961
5
    return;
11962
5
  }
11963
3
      md_number_to_chars (p, n, size);
11964
3
    }
11965
0
  else
11966
0
    fix_new_exp (frag_now, p - frag_now->fr_literal, size,
11967
0
     i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
11968
11969
3
  p += size;
11970
3
  if (i.op[0].imms->X_op == O_constant)
11971
3
    md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
11972
0
  else
11973
0
    fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
11974
0
     i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
11975
3
}
11976
11977
/* Hook used to reject pseudo-prefixes misplaced at the start of a line.  */
11978
11979
void i386_start_line (void)
11980
628k
{
11981
628k
  struct pseudo_prefixes last_pp;
11982
11983
628k
  memcpy (&last_pp, &pp, sizeof (pp));
11984
628k
  memset (&pp, 0, sizeof (pp));
11985
628k
  if (memcmp (&pp, &last_pp, sizeof (pp)))
11986
4
    as_bad_where (frag_now->fr_file, frag_now->fr_line,
11987
4
      _("pseudo prefix without instruction"));
11988
628k
}
11989
11990
/* Hook used to warn about pseudo-prefixes ahead of a label.  */
11991
11992
bool i386_check_label (void)
11993
5.25k
{
11994
5.25k
  struct pseudo_prefixes last_pp;
11995
11996
5.25k
  memcpy (&last_pp, &pp, sizeof (pp));
11997
5.25k
  memset (&pp, 0, sizeof (pp));
11998
5.25k
  if (memcmp (&pp, &last_pp, sizeof (pp)))
11999
0
    as_warn (_("pseudo prefix ahead of label; ignoring"));
12000
5.25k
  return true;
12001
5.25k
}
12002
12003
/* Hook used to parse pseudo-prefixes off of the start of a line.  */
12004
12005
int
12006
i386_unrecognized_line (int ch)
12007
64.6k
{
12008
64.6k
  char mnemonic[MAX_MNEM_SIZE];
12009
64.6k
  const char *end;
12010
12011
64.6k
  if (ch != '{')
12012
63.8k
    return 0;
12013
12014
796
  --input_line_pointer;
12015
796
  know (*input_line_pointer == ch);
12016
12017
796
  end = parse_insn (input_line_pointer, mnemonic, parse_pseudo_prefix);
12018
796
  if (end == NULL)
12019
6
    {
12020
      /* Diagnostic was already issued.  */
12021
6
      ignore_rest_of_line ();
12022
6
      memset (&pp, 0, sizeof (pp));
12023
6
      return 1;
12024
6
    }
12025
12026
790
  if (end == input_line_pointer)
12027
257
    {
12028
257
      ++input_line_pointer;
12029
257
      return 0;
12030
257
    }
12031
12032
533
  input_line_pointer += end - input_line_pointer;
12033
533
  return 1;
12034
790
}
12035
12036
#ifdef OBJ_ELF
12037
void
12038
x86_cleanup (void)
12039
478
{
12040
478
  char *p;
12041
478
  asection *seg = now_seg;
12042
478
  subsegT subseg = now_subseg;
12043
478
  asection *sec;
12044
478
  unsigned int alignment, align_size_1;
12045
478
  unsigned int isa_1_descsz, feature_2_descsz, descsz;
12046
478
  unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
12047
478
  unsigned int padding;
12048
12049
478
  if (!x86_used_note)
12050
0
    return;
12051
12052
478
  x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
12053
12054
  /* The .note.gnu.property section layout:
12055
12056
     Field  Length    Contents
12057
     ---- ----    ----
12058
     n_namsz  4   4
12059
     n_descsz 4   The note descriptor size
12060
     n_type 4   NT_GNU_PROPERTY_TYPE_0
12061
     n_name 4   "GNU"
12062
     n_desc n_descsz  The program property array
12063
     .... ....    ....
12064
   */
12065
12066
  /* Create the .note.gnu.property section.  */
12067
478
  sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
12068
478
  bfd_set_section_flags (sec,
12069
478
       (SEC_ALLOC
12070
478
        | SEC_LOAD
12071
478
        | SEC_DATA
12072
478
        | SEC_HAS_CONTENTS
12073
478
        | SEC_READONLY));
12074
12075
478
  if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
12076
478
    {
12077
478
      align_size_1 = 7;
12078
478
      alignment = 3;
12079
478
    }
12080
0
  else
12081
0
    {
12082
0
      align_size_1 = 3;
12083
0
      alignment = 2;
12084
0
    }
12085
12086
478
  bfd_set_section_alignment (sec, alignment);
12087
478
  elf_section_type (sec) = SHT_NOTE;
12088
12089
  /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
12090
          + 4-byte data  */
12091
478
  isa_1_descsz_raw = 4 + 4 + 4;
12092
  /* Align GNU_PROPERTY_X86_ISA_1_USED.  */
12093
478
  isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
12094
12095
478
  feature_2_descsz_raw = isa_1_descsz;
12096
  /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
12097
              + 4-byte data  */
12098
478
  feature_2_descsz_raw += 4 + 4 + 4;
12099
  /* Align GNU_PROPERTY_X86_FEATURE_2_USED.  */
12100
478
  feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
12101
478
          & ~align_size_1);
12102
12103
478
  descsz = feature_2_descsz;
12104
  /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz.  */
12105
478
  p = frag_more (4 + 4 + 4 + 4 + descsz);
12106
12107
  /* Write n_namsz.  */
12108
478
  md_number_to_chars (p, (valueT) 4, 4);
12109
12110
  /* Write n_descsz.  */
12111
478
  md_number_to_chars (p + 4, (valueT) descsz, 4);
12112
12113
  /* Write n_type.  */
12114
478
  md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
12115
12116
  /* Write n_name.  */
12117
478
  memcpy (p + 4 * 3, "GNU", 4);
12118
12119
  /* Write 4-byte type.  */
12120
478
  md_number_to_chars (p + 4 * 4,
12121
478
          (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
12122
12123
  /* Write 4-byte data size.  */
12124
478
  md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
12125
12126
  /* Write 4-byte data.  */
12127
478
  md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
12128
12129
  /* Zero out paddings.  */
12130
478
  padding = isa_1_descsz - isa_1_descsz_raw;
12131
478
  if (padding)
12132
478
    memset (p + 4 * 7, 0, padding);
12133
12134
  /* Write 4-byte type.  */
12135
478
  md_number_to_chars (p + isa_1_descsz + 4 * 4,
12136
478
          (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
12137
12138
  /* Write 4-byte data size.  */
12139
478
  md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
12140
12141
  /* Write 4-byte data.  */
12142
478
  md_number_to_chars (p + isa_1_descsz + 4 * 6,
12143
478
          (valueT) x86_feature_2_used, 4);
12144
12145
  /* Zero out paddings.  */
12146
478
  padding = feature_2_descsz - feature_2_descsz_raw;
12147
478
  if (padding)
12148
478
    memset (p + isa_1_descsz + 4 * 7, 0, padding);
12149
12150
  /* We probably can't restore the current segment, for there likely
12151
     isn't one yet...  */
12152
478
  if (seg && subseg)
12153
6
    subseg_set (seg, subseg);
12154
478
}
12155
12156
#include "tc-i386-ginsn.c"
12157
12158
/* Whether SFrame stack trace info is supported.  */
12159
bool
12160
x86_support_sframe_p (void)
12161
94
{
12162
  /* At this time, SFrame stack trace is supported for AMD64 ABI only.  */
12163
94
  return (x86_elf_abi == X86_64_ABI);
12164
94
}
12165
12166
/* The fixed offset from CFA for SFrame to recover the return address.
12167
   (useful only when SFrame RA tracking is not needed).  */
12168
offsetT
12169
x86_sframe_cfa_ra_offset (void)
12170
31
{
12171
31
  gas_assert (x86_elf_abi == X86_64_ABI);
12172
31
  return (offsetT) -8;
12173
31
}
12174
12175
/* The abi/arch identifier for SFrame.  */
12176
unsigned char
12177
x86_sframe_get_abi_arch (void)
12178
80
{
12179
80
  unsigned char sframe_abi_arch = 0;
12180
12181
80
  if (x86_support_sframe_p ())
12182
80
    {
12183
80
      gas_assert (!target_big_endian);
12184
80
      sframe_abi_arch = SFRAME_ABI_AMD64_ENDIAN_LITTLE;
12185
80
    }
12186
12187
80
  return sframe_abi_arch;
12188
80
}
12189
12190
#endif
12191
12192
static unsigned int
12193
encoding_length (const fragS *start_frag, offsetT start_off,
12194
     const char *frag_now_ptr)
12195
11.3k
{
12196
11.3k
  unsigned int len = 0;
12197
12198
11.3k
  if (start_frag != frag_now)
12199
16
    {
12200
16
      const fragS *fr = start_frag;
12201
12202
16
      do {
12203
16
  len += fr->fr_fix;
12204
16
  fr = fr->fr_next;
12205
16
      } while (fr && fr != frag_now);
12206
16
    }
12207
12208
11.3k
  return len - start_off + (frag_now_ptr - frag_now->fr_literal);
12209
11.3k
}
12210
12211
/* Return 1 for test, and, cmp, add, sub, inc and dec which may
12212
   be macro-fused with conditional jumps.
12213
   NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
12214
   or is one of the following format:
12215
12216
    cmp m, imm
12217
    add m, imm
12218
    sub m, imm
12219
   test m, imm
12220
    and m, imm
12221
    inc m
12222
    dec m
12223
12224
   it is unfusible.  */
12225
12226
static int
12227
maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
12228
0
{
12229
  /* No RIP address.  */
12230
0
  if (i.base_reg && i.base_reg->reg_num == RegIP)
12231
0
    return 0;
12232
12233
  /* No opcodes outside of base encoding space.  */
12234
0
  if (i.tm.opcode_space != SPACE_BASE)
12235
0
    return 0;
12236
12237
  /* add, sub without add/sub m, imm.  */
12238
0
  if (i.tm.base_opcode <= 5
12239
0
      || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
12240
0
      || ((i.tm.base_opcode | 3) == 0x83
12241
0
    && (i.tm.extension_opcode == 0x5
12242
0
        || i.tm.extension_opcode == 0x0)))
12243
0
    {
12244
0
      *mf_cmp_p = mf_cmp_alu_cmp;
12245
0
      return !(i.mem_operands && i.imm_operands);
12246
0
    }
12247
12248
  /* and without and m, imm.  */
12249
0
  if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
12250
0
      || ((i.tm.base_opcode | 3) == 0x83
12251
0
    && i.tm.extension_opcode == 0x4))
12252
0
    {
12253
0
      *mf_cmp_p = mf_cmp_test_and;
12254
0
      return !(i.mem_operands && i.imm_operands);
12255
0
    }
12256
12257
  /* test without test m imm.  */
12258
0
  if ((i.tm.base_opcode | 1) == 0x85
12259
0
      || (i.tm.base_opcode | 1) == 0xa9
12260
0
      || ((i.tm.base_opcode | 1) == 0xf7
12261
0
    && i.tm.extension_opcode == 0))
12262
0
    {
12263
0
      *mf_cmp_p = mf_cmp_test_and;
12264
0
      return !(i.mem_operands && i.imm_operands);
12265
0
    }
12266
12267
  /* cmp without cmp m, imm.  */
12268
0
  if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
12269
0
      || ((i.tm.base_opcode | 3) == 0x83
12270
0
    && (i.tm.extension_opcode == 0x7)))
12271
0
    {
12272
0
      *mf_cmp_p = mf_cmp_alu_cmp;
12273
0
      return !(i.mem_operands && i.imm_operands);
12274
0
    }
12275
12276
  /* inc, dec without inc/dec m.   */
12277
0
  if ((is_cpu (&i.tm, CpuNo64)
12278
0
       && (i.tm.base_opcode | 0xf) == 0x4f)
12279
0
      || ((i.tm.base_opcode | 1) == 0xff
12280
0
    && i.tm.extension_opcode <= 0x1))
12281
0
    {
12282
0
      *mf_cmp_p = mf_cmp_incdec;
12283
0
      return !i.mem_operands;
12284
0
    }
12285
12286
0
  return 0;
12287
0
}
12288
12289
/* Return 1 if a FUSED_JCC_PADDING frag should be generated.  */
12290
12291
static int
12292
add_fused_jcc_padding_frag_p (enum mf_cmp_kind *mf_cmp_p,
12293
            const struct last_insn *last_insn)
12294
11.6k
{
12295
  /* NB: Don't work with COND_JUMP86 without i386.  */
12296
11.6k
  if (!align_branch_power
12297
0
      || now_seg == absolute_section
12298
0
      || !cpu_arch_flags.bitfield.cpui386
12299
0
      || !(align_branch & align_branch_fused_bit))
12300
11.6k
    return 0;
12301
12302
0
  if (maybe_fused_with_jcc_p (mf_cmp_p))
12303
0
    {
12304
0
      if (last_insn->kind == last_insn_other)
12305
0
  return 1;
12306
0
      if (flag_debug)
12307
0
  as_warn_where (last_insn->file, last_insn->line,
12308
0
           _("`%s` skips -malign-branch-boundary on `%s`"),
12309
0
           last_insn->name, insn_name (&i.tm));
12310
0
    }
12311
12312
0
  return 0;
12313
0
}
12314
12315
/* Return 1 if a BRANCH_PREFIX frag should be generated.  */
12316
12317
static int
12318
add_branch_prefix_frag_p (const struct last_insn *last_insn)
12319
11.6k
{
12320
  /* NB: Don't work with COND_JUMP86 without i386.  Don't add prefix
12321
     to PadLock instructions since they include prefixes in opcode.  */
12322
11.6k
  if (!align_branch_power
12323
0
      || !align_branch_prefix_size
12324
0
      || now_seg == absolute_section
12325
0
      || is_padlock (&i.tm)
12326
0
      || !cpu_arch_flags.bitfield.cpui386)
12327
11.6k
    return 0;
12328
12329
  /* Don't add prefix if it is a prefix or there is no operand in case
12330
     that segment prefix is special.  */
12331
0
  if (!i.operands || i.tm.opcode_modifier.isprefix)
12332
0
    return 0;
12333
12334
0
  if (last_insn->kind == last_insn_other)
12335
0
    return 1;
12336
12337
0
  if (flag_debug)
12338
0
    as_warn_where (last_insn->file, last_insn->line,
12339
0
       _("`%s` skips -malign-branch-boundary on `%s`"),
12340
0
       last_insn->name, insn_name (&i.tm));
12341
12342
0
  return 0;
12343
0
}
12344
12345
/* Return 1 if a BRANCH_PADDING frag should be generated.  */
12346
12347
static int
12348
add_branch_padding_frag_p (enum align_branch_kind *branch_p,
12349
         enum mf_jcc_kind *mf_jcc_p,
12350
         const struct last_insn *last_insn)
12351
11.8k
{
12352
11.8k
  int add_padding;
12353
12354
  /* NB: Don't work with COND_JUMP86 without i386.  */
12355
11.8k
  if (!align_branch_power
12356
0
      || now_seg == absolute_section
12357
0
      || !cpu_arch_flags.bitfield.cpui386
12358
0
      || i.tm.opcode_space != SPACE_BASE)
12359
11.8k
    return 0;
12360
12361
0
  add_padding = 0;
12362
12363
  /* Check for jcc and direct jmp.  */
12364
0
  if (i.tm.opcode_modifier.jump == JUMP)
12365
0
    {
12366
0
      if (i.tm.base_opcode == JUMP_PC_RELATIVE)
12367
0
  {
12368
0
    *branch_p = align_branch_jmp;
12369
0
    add_padding = align_branch & align_branch_jmp_bit;
12370
0
  }
12371
0
      else
12372
0
  {
12373
    /* Because J<cc> and JN<cc> share same group in macro-fusible table,
12374
       igore the lowest bit.  */
12375
0
    *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
12376
0
    *branch_p = align_branch_jcc;
12377
0
    if ((align_branch & align_branch_jcc_bit))
12378
0
      add_padding = 1;
12379
0
  }
12380
0
    }
12381
0
  else if ((i.tm.base_opcode | 1) == 0xc3)
12382
0
    {
12383
      /* Near ret.  */
12384
0
      *branch_p = align_branch_ret;
12385
0
      if ((align_branch & align_branch_ret_bit))
12386
0
  add_padding = 1;
12387
0
    }
12388
0
  else
12389
0
    {
12390
      /* Check for indirect jmp, direct and indirect calls.  */
12391
0
      if (i.tm.base_opcode == 0xe8)
12392
0
  {
12393
    /* Direct call.  */
12394
0
    *branch_p = align_branch_call;
12395
0
    if ((align_branch & align_branch_call_bit))
12396
0
      add_padding = 1;
12397
0
  }
12398
0
      else if (i.tm.base_opcode == 0xff
12399
0
         && (i.tm.extension_opcode == 2
12400
0
       || i.tm.extension_opcode == 4))
12401
0
  {
12402
    /* Indirect call and jmp.  */
12403
0
    *branch_p = align_branch_indirect;
12404
0
    if ((align_branch & align_branch_indirect_bit))
12405
0
      add_padding = 1;
12406
0
  }
12407
12408
0
      if (add_padding
12409
0
    && i.disp_operands
12410
0
    && tls_get_addr
12411
0
    && (i.op[0].disps->X_op == O_symbol
12412
0
        || (i.op[0].disps->X_op == O_subtract
12413
0
      && i.op[0].disps->X_op_symbol == GOT_symbol)))
12414
0
  {
12415
0
    symbolS *s = i.op[0].disps->X_add_symbol;
12416
    /* No padding to call to global or undefined tls_get_addr.  */
12417
0
    if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
12418
0
        && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
12419
0
      return 0;
12420
0
  }
12421
0
    }
12422
12423
0
  if (add_padding
12424
0
      && last_insn->kind != last_insn_other)
12425
0
    {
12426
0
      if (flag_debug)
12427
0
  as_warn_where (last_insn->file, last_insn->line,
12428
0
           _("`%s` skips -malign-branch-boundary on `%s`"),
12429
0
           last_insn->name, insn_name (&i.tm));
12430
0
      return 0;
12431
0
    }
12432
12433
0
  return add_padding;
12434
0
}
12435
12436
static void
12437
output_insn (const struct last_insn *last_insn)
12438
11.8k
{
12439
11.8k
  fragS *insn_start_frag;
12440
11.8k
  offsetT insn_start_off;
12441
11.8k
  fragS *fragP = NULL;
12442
11.8k
  enum align_branch_kind branch = align_branch_none;
12443
  /* The initializer is arbitrary just to avoid uninitialized error.
12444
     it's actually either assigned in add_branch_padding_frag_p
12445
     or never be used.  */
12446
11.8k
  enum mf_jcc_kind mf_jcc = mf_jcc_jo;
12447
12448
11.8k
#ifdef OBJ_ELF
12449
11.8k
  if (x86_used_note && now_seg != absolute_section)
12450
11.4k
    {
12451
11.4k
      unsigned int feature_2_used = 0;
12452
12453
11.4k
      if ((i.xstate & xstate_tmm) == xstate_tmm
12454
11.4k
    || is_cpu (&i.tm, CpuAMX_TILE))
12455
2
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
12456
12457
11.4k
      if (is_cpu (&i.tm, Cpu8087)
12458
11.4k
    || is_cpu (&i.tm, Cpu287)
12459
11.4k
    || is_cpu (&i.tm, Cpu387)
12460
11.4k
    || is_cpu (&i.tm, Cpu687)
12461
11.4k
    || is_cpu (&i.tm, CpuFISTTP))
12462
1
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
12463
12464
11.4k
      if ((i.xstate & xstate_mmx)
12465
11.4k
    || i.tm.mnem_off == MN_emms
12466
11.4k
    || i.tm.mnem_off == MN_femms)
12467
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
12468
12469
11.4k
      if (i.index_reg)
12470
0
  {
12471
0
    if (i.index_reg->reg_type.bitfield.zmmword)
12472
0
      i.xstate |= xstate_zmm;
12473
0
    else if (i.index_reg->reg_type.bitfield.ymmword)
12474
0
      i.xstate |= xstate_ymm;
12475
0
    else if (i.index_reg->reg_type.bitfield.xmmword)
12476
0
      i.xstate |= xstate_xmm;
12477
0
  }
12478
12479
      /* vzeroall / vzeroupper */
12480
11.4k
      if (i.tm.base_opcode == 0x77 && is_cpu (&i.tm, CpuAVX))
12481
1
  i.xstate |= xstate_ymm;
12482
12483
11.4k
      if ((i.xstate & xstate_xmm)
12484
    /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
12485
11.4k
    || (i.tm.base_opcode == 0xae
12486
64
        && (is_cpu (&i.tm, CpuSSE)
12487
64
      || is_cpu (&i.tm, CpuAVX)))
12488
11.4k
    || is_cpu (&i.tm, CpuWideKL)
12489
11.4k
    || is_cpu (&i.tm, CpuKL))
12490
1
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
12491
12492
11.4k
      if ((i.xstate & xstate_ymm) == xstate_ymm)
12493
1
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
12494
11.4k
      if ((i.xstate & xstate_zmm) == xstate_zmm)
12495
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
12496
11.4k
      if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
12497
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
12498
11.4k
      if (is_cpu (&i.tm, CpuFXSR))
12499
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
12500
11.4k
      if (is_cpu (&i.tm, CpuXsave))
12501
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
12502
11.4k
      if (is_cpu (&i.tm, CpuXsaveopt))
12503
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
12504
11.4k
      if (is_cpu (&i.tm, CpuXSAVEC))
12505
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
12506
12507
11.4k
      x86_feature_2_used |= feature_2_used;
12508
12509
11.4k
      if (object_64bit
12510
0
    || (feature_2_used
12511
0
        & (GNU_PROPERTY_X86_FEATURE_2_XMM
12512
0
     | GNU_PROPERTY_X86_FEATURE_2_FXSR)) != 0
12513
0
    || is_cpu (&i.tm, CpuCMOV)
12514
0
    || is_cpu (&i.tm, CpuSYSCALL)
12515
0
    || i.tm.mnem_off == MN_cmpxchg8b)
12516
11.4k
  x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
12517
11.4k
      if (is_cpu (&i.tm, CpuSSE3)
12518
11.4k
    || is_cpu (&i.tm, CpuSSSE3)
12519
11.4k
    || is_cpu (&i.tm, CpuSSE4_1)
12520
11.4k
    || is_cpu (&i.tm, CpuSSE4_2)
12521
11.4k
    || is_cpu (&i.tm, CpuCX16)
12522
11.4k
    || is_cpu (&i.tm, CpuPOPCNT)
12523
    /* LAHF-SAHF insns in 64-bit mode.  */
12524
11.4k
    || (flag_code == CODE_64BIT
12525
10.9k
        && (i.tm.base_opcode | 1) == 0x9f
12526
1
        && i.tm.opcode_space == SPACE_BASE))
12527
2
  x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
12528
11.4k
      if (is_cpu (&i.tm, CpuAVX)
12529
11.4k
    || is_cpu (&i.tm, CpuAVX2)
12530
    /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
12531
       XOP, FMA4, LPW, TBM, and AMX.  */
12532
11.4k
    || (i.tm.opcode_modifier.vex
12533
192
        && !is_cpu (&i.tm, CpuAVX512F)
12534
192
        && !is_cpu (&i.tm, CpuAVX512BW)
12535
192
        && !is_cpu (&i.tm, CpuAVX512DQ)
12536
192
        && !is_cpu (&i.tm, CpuXOP)
12537
192
        && !is_cpu (&i.tm, CpuFMA4)
12538
192
        && !is_cpu (&i.tm, CpuLWP)
12539
192
        && !is_cpu (&i.tm, CpuTBM)
12540
192
        && !(feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
12541
11.3k
    || is_cpu (&i.tm, CpuLZCNT)
12542
11.3k
    || is_cpu (&i.tm, CpuMovbe)
12543
11.3k
    || is_cpu (&i.tm, CpuXSAVES)
12544
11.3k
    || (feature_2_used
12545
11.3k
        & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
12546
11.3k
     | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
12547
11.3k
     | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
12548
192
  x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
12549
11.4k
      if (is_cpu (&i.tm, CpuAVX512F)
12550
11.4k
    || is_cpu (&i.tm, CpuAVX512BW)
12551
11.4k
    || is_cpu (&i.tm, CpuAVX512DQ)
12552
11.4k
    || is_cpu (&i.tm, CpuAVX512VL)
12553
    /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
12554
       AVX512-4FMAPS, and AVX512-4VNNIW.  */
12555
11.4k
    || (i.tm.opcode_modifier.evex
12556
47
        && !is_cpu (&i.tm, CpuAVX512ER)
12557
47
        && !is_cpu (&i.tm, CpuAVX512PF)
12558
47
        && !is_cpu (&i.tm, CpuAVX512_4FMAPS)
12559
47
        && !is_cpu (&i.tm, CpuAVX512_4VNNIW)))
12560
47
  x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
12561
11.4k
    }
12562
11.8k
#endif
12563
12564
  /* Tie dwarf2 debug info to the address at the start of the insn.
12565
     We can't do this after the insn has been output as the current
12566
     frag may have been closed off.  eg. by frag_var.  */
12567
11.8k
  dwarf2_emit_insn (0);
12568
12569
11.8k
  insn_start_frag = frag_now;
12570
11.8k
  insn_start_off = frag_now_fix ();
12571
12572
11.8k
  if (add_branch_padding_frag_p (&branch, &mf_jcc, last_insn))
12573
0
    {
12574
0
      char *p;
12575
      /* Branch can be 8 bytes.  Leave some room for prefixes.  */
12576
0
      unsigned int max_branch_padding_size = 14;
12577
12578
      /* Align section to boundary.  */
12579
0
      record_alignment (now_seg, align_branch_power);
12580
12581
      /* Make room for padding.  */
12582
0
      frag_grow (max_branch_padding_size);
12583
12584
      /* Start of the padding.  */
12585
0
      p = frag_more (0);
12586
12587
0
      fragP = frag_now;
12588
12589
0
      frag_var (rs_machine_dependent, max_branch_padding_size, 0,
12590
0
    ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
12591
0
    NULL, 0, p);
12592
12593
0
      fragP->tc_frag_data.mf_type = mf_jcc;
12594
0
      fragP->tc_frag_data.branch_type = branch;
12595
0
      fragP->tc_frag_data.max_bytes = max_branch_padding_size;
12596
0
    }
12597
12598
11.8k
  if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
12599
0
      && !pre_386_16bit_warned)
12600
0
    {
12601
0
      as_warn (_("use .code16 to ensure correct addressing mode"));
12602
0
      pre_386_16bit_warned = true;
12603
0
    }
12604
12605
  /* Output jumps.  */
12606
11.8k
  if (i.tm.opcode_modifier.jump == JUMP)
12607
47
    output_branch ();
12608
11.7k
  else if (i.tm.opcode_modifier.jump == JUMP_BYTE
12609
11.7k
     || i.tm.opcode_modifier.jump == JUMP_DWORD)
12610
164
    output_jump ();
12611
11.6k
  else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
12612
8
    output_interseg_jump ();
12613
11.6k
  else
12614
11.6k
    {
12615
      /* Output normal instructions here.  */
12616
11.6k
      char *p;
12617
11.6k
      unsigned char *q;
12618
11.6k
      unsigned int j;
12619
11.6k
      enum mf_cmp_kind mf_cmp;
12620
12621
11.6k
      if (avoid_fence
12622
0
    && (i.tm.base_opcode == 0xaee8
12623
0
        || i.tm.base_opcode == 0xaef0
12624
0
        || i.tm.base_opcode == 0xaef8))
12625
0
  {
12626
    /* Encode lfence, mfence, and sfence as
12627
       f0 83 04 24 00   lock addl $0x0, (%{re}sp).  */
12628
0
    if (flag_code == CODE_16BIT)
12629
0
      as_bad (_("Cannot convert `%s' in 16-bit mode"), insn_name (&i.tm));
12630
0
    else if (omit_lock_prefix)
12631
0
      as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
12632
0
        insn_name (&i.tm));
12633
0
    else if (now_seg != absolute_section)
12634
0
      {
12635
0
        offsetT val = 0x240483f0ULL;
12636
12637
0
        p = frag_more (5);
12638
0
        md_number_to_chars (p, val, 5);
12639
0
      }
12640
0
    else
12641
0
      abs_section_offset += 5;
12642
0
    return;
12643
0
  }
12644
12645
      /* Some processors fail on LOCK prefix. This options makes
12646
   assembler ignore LOCK prefix and serves as a workaround.  */
12647
11.6k
      if (omit_lock_prefix)
12648
0
  {
12649
0
    if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
12650
0
        && i.tm.opcode_modifier.isprefix)
12651
0
      return;
12652
0
    i.prefix[LOCK_PREFIX] = 0;
12653
0
  }
12654
12655
11.6k
      if (branch)
12656
  /* Skip if this is a branch.  */
12657
0
  ;
12658
11.6k
      else if (add_fused_jcc_padding_frag_p (&mf_cmp, last_insn))
12659
0
  {
12660
    /* Make room for padding.  */
12661
0
    frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
12662
0
    p = frag_more (0);
12663
12664
0
    fragP = frag_now;
12665
12666
0
    frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
12667
0
        ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
12668
0
        NULL, 0, p);
12669
12670
0
    fragP->tc_frag_data.mf_type = mf_cmp;
12671
0
    fragP->tc_frag_data.branch_type = align_branch_fused;
12672
0
    fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
12673
0
  }
12674
11.6k
      else if (add_branch_prefix_frag_p (last_insn))
12675
0
  {
12676
0
    unsigned int max_prefix_size = align_branch_prefix_size;
12677
12678
    /* Make room for padding.  */
12679
0
    frag_grow (max_prefix_size);
12680
0
    p = frag_more (0);
12681
12682
0
    fragP = frag_now;
12683
12684
0
    frag_var (rs_machine_dependent, max_prefix_size, 0,
12685
0
        ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
12686
0
        NULL, 0, p);
12687
12688
0
    fragP->tc_frag_data.max_bytes = max_prefix_size;
12689
0
  }
12690
12691
      /* Since the VEX/EVEX prefix contains the implicit prefix, we
12692
   don't need the explicit prefix.  */
12693
11.6k
      if (!is_any_vex_encoding (&i.tm))
12694
11.2k
  {
12695
11.2k
    switch (i.tm.opcode_modifier.opcodeprefix)
12696
11.2k
      {
12697
14
      case PREFIX_0X66:
12698
14
        add_prefix (0x66);
12699
14
        break;
12700
0
      case PREFIX_0XF2:
12701
0
        add_prefix (0xf2);
12702
0
        break;
12703
5
      case PREFIX_0XF3:
12704
5
        if (!is_padlock (&i.tm)
12705
0
      || (i.prefix[REP_PREFIX] != 0xf3))
12706
5
    add_prefix (0xf3);
12707
5
        break;
12708
11.2k
      case PREFIX_NONE:
12709
11.2k
        switch (i.opcode_length)
12710
11.2k
    {
12711
6
    case 2:
12712
6
      break;
12713
11.2k
    case 1:
12714
      /* Check for pseudo prefixes.  */
12715
11.2k
      if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
12716
11.2k
        break;
12717
3
      as_bad_where (insn_start_frag->fr_file,
12718
3
        insn_start_frag->fr_line,
12719
3
        _("pseudo prefix without instruction"));
12720
3
      return;
12721
0
    default:
12722
0
      abort ();
12723
11.2k
    }
12724
11.2k
        break;
12725
11.2k
      default:
12726
0
        abort ();
12727
11.2k
      }
12728
12729
11.2k
#ifdef OBJ_ELF
12730
    /* For x32, add a dummy REX_OPCODE prefix for mov/add with
12731
       R_X86_64_GOTTPOFF relocation so that linker can safely
12732
       perform IE->LE optimization.  A dummy REX_OPCODE prefix
12733
       is also needed for lea with R_X86_64_GOTPC32_TLSDESC
12734
       relocation for GDesc -> IE/LE optimization.  */
12735
11.2k
    if (x86_elf_abi == X86_64_X32_ABI
12736
0
        && !is_apx_rex2_encoding ()
12737
0
        && (dot_insn () ? i.insn_opcode_space
12738
0
            : i.tm.opcode_space) == SPACE_BASE
12739
0
        && i.operands == 2
12740
0
        && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
12741
0
      || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
12742
0
        && i.prefix[REX_PREFIX] == 0)
12743
0
      add_prefix (REX_OPCODE);
12744
11.2k
#endif
12745
12746
    /* The prefix bytes.  */
12747
90.0k
    for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
12748
78.7k
      if (*q)
12749
348
        frag_opcode_byte (*q);
12750
12751
11.2k
    if (is_apx_rex2_encoding ())
12752
11
      {
12753
11
        frag_opcode_byte (i.vex.bytes[0]);
12754
11
        frag_opcode_byte (i.vex.bytes[1]);
12755
11
      }
12756
11.2k
  }
12757
366
      else
12758
366
  {
12759
2.92k
    for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
12760
2.56k
      if (*q)
12761
0
        switch (j)
12762
0
    {
12763
0
    case SEG_PREFIX:
12764
0
    case ADDR_PREFIX:
12765
0
      frag_opcode_byte (*q);
12766
0
      break;
12767
0
    default:
12768
      /* There should be no other prefixes for instructions
12769
         with VEX prefix.  */
12770
0
      abort ();
12771
0
    }
12772
12773
    /* For EVEX instructions i.vrex should become 0 after
12774
       build_evex_prefix.  For VEX instructions upper 16 registers
12775
       aren't available, so VREX should be 0.  */
12776
366
    if (i.vrex)
12777
0
      abort ();
12778
    /* Now the VEX prefix.  */
12779
366
    if (now_seg != absolute_section)
12780
240
      {
12781
240
        p = frag_more (i.vex.length);
12782
1.00k
        for (j = 0; j < i.vex.length; j++)
12783
766
    p[j] = i.vex.bytes[j];
12784
240
      }
12785
126
    else
12786
126
      abs_section_offset += i.vex.length;
12787
366
  }
12788
12789
      /* Now the opcode; be careful about word order here!  */
12790
11.6k
      j = i.opcode_length;
12791
11.6k
      if (!i.vex.length)
12792
11.2k
  switch (i.tm.opcode_space)
12793
11.2k
    {
12794
3.11k
    case SPACE_BASE:
12795
3.11k
      break;
12796
8.12k
    case SPACE_0F:
12797
8.12k
      ++j;
12798
8.12k
      break;
12799
0
    case SPACE_0F38:
12800
0
    case SPACE_0F3A:
12801
0
      j += 2;
12802
0
      break;
12803
0
    default:
12804
0
      abort ();
12805
11.2k
    }
12806
12807
11.6k
      if (now_seg == absolute_section)
12808
265
  abs_section_offset += j;
12809
11.3k
      else if (j == 1)
12810
3.18k
  {
12811
3.18k
    FRAG_APPEND_1_CHAR (i.tm.base_opcode);
12812
3.18k
  }
12813
8.16k
      else
12814
8.16k
  {
12815
8.16k
    p = frag_more (j);
12816
8.16k
    if (!i.vex.length
12817
8.13k
        && i.tm.opcode_space != SPACE_BASE)
12818
8.12k
      {
12819
8.12k
        *p++ = 0x0f;
12820
8.12k
        if (i.tm.opcode_space != SPACE_0F)
12821
0
    *p++ = i.tm.opcode_space == SPACE_0F38
12822
0
           ? 0x38 : 0x3a;
12823
8.12k
      }
12824
12825
8.16k
    switch (i.opcode_length)
12826
8.16k
      {
12827
49
      case 2:
12828
        /* Put out high byte first: can't use md_number_to_chars!  */
12829
49
        *p++ = (i.tm.base_opcode >> 8) & 0xff;
12830
        /* Fall through.  */
12831
8.16k
      case 1:
12832
8.16k
        *p = i.tm.base_opcode & 0xff;
12833
8.16k
        break;
12834
0
      default:
12835
0
        abort ();
12836
0
        break;
12837
8.16k
      }
12838
12839
8.16k
  }
12840
12841
      /* Now the modrm byte and sib byte (if present).  */
12842
11.6k
      if (i.tm.opcode_modifier.modrm)
12843
9.54k
  {
12844
9.54k
    frag_opcode_byte ((i.rm.regmem << 0)
12845
9.54k
           | (i.rm.reg << 3)
12846
9.54k
           | (i.rm.mode << 6));
12847
    /* If i.rm.regmem == ESP (4)
12848
       && i.rm.mode != (Register mode)
12849
       && not 16 bit
12850
       ==> need second modrm byte.  */
12851
9.54k
    if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
12852
8.82k
        && i.rm.mode != 3
12853
8.82k
        && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
12854
8.82k
      frag_opcode_byte ((i.sib.base << 0)
12855
8.82k
            | (i.sib.index << 3)
12856
8.82k
            | (i.sib.scale << 6));
12857
9.54k
  }
12858
12859
11.6k
      if (i.disp_operands)
12860
9.46k
  output_disp (insn_start_frag, insn_start_off);
12861
12862
11.6k
      if (i.imm_operands)
12863
1.08k
  output_imm (insn_start_frag, insn_start_off);
12864
12865
      /*
12866
       * frag_now_fix () returning plain abs_section_offset when we're in the
12867
       * absolute section, and abs_section_offset not getting updated as data
12868
       * gets added to the frag breaks the logic below.
12869
       */
12870
11.6k
      if (now_seg != absolute_section)
12871
11.3k
  {
12872
11.3k
    j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
12873
11.3k
    if (j > 15)
12874
0
      {
12875
0
        if (dot_insn ())
12876
0
    as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
12877
0
      j);
12878
0
        else
12879
0
    as_bad (_("instruction length of %u bytes exceeds the limit of 15"),
12880
0
      j);
12881
0
      }
12882
11.3k
    else if (fragP)
12883
0
      {
12884
        /* NB: Don't add prefix with GOTPC relocation since
12885
     output_disp() above depends on the fixed encoding
12886
     length.  Can't add prefix with TLS relocation since
12887
     it breaks TLS linker optimization.  */
12888
0
        unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
12889
        /* Prefix count on the current instruction.  */
12890
0
        unsigned int count = i.vex.length;
12891
0
        unsigned int k;
12892
0
        for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
12893
    /* REX byte is encoded in VEX/EVEX prefix.  */
12894
0
    if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
12895
0
      count++;
12896
12897
        /* Count prefixes for extended opcode maps.  */
12898
0
        if (!i.vex.length)
12899
0
    switch (i.tm.opcode_space)
12900
0
      {
12901
0
      case SPACE_BASE:
12902
0
        break;
12903
0
      case SPACE_0F:
12904
0
        count++;
12905
0
        break;
12906
0
      case SPACE_0F38:
12907
0
      case SPACE_0F3A:
12908
0
        count += 2;
12909
0
        break;
12910
0
      default:
12911
0
        abort ();
12912
0
      }
12913
12914
0
        if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12915
0
      == BRANCH_PREFIX)
12916
0
    {
12917
      /* Set the maximum prefix size in BRANCH_PREFIX
12918
         frag.  */
12919
0
      if (fragP->tc_frag_data.max_bytes > max)
12920
0
        fragP->tc_frag_data.max_bytes = max;
12921
0
      if (fragP->tc_frag_data.max_bytes > count)
12922
0
        fragP->tc_frag_data.max_bytes -= count;
12923
0
      else
12924
0
        fragP->tc_frag_data.max_bytes = 0;
12925
0
    }
12926
0
        else
12927
0
    {
12928
      /* Remember the maximum prefix size in FUSED_JCC_PADDING
12929
         frag.  */
12930
0
      unsigned int max_prefix_size;
12931
0
      if (align_branch_prefix_size > max)
12932
0
        max_prefix_size = max;
12933
0
      else
12934
0
        max_prefix_size = align_branch_prefix_size;
12935
0
      if (max_prefix_size > count)
12936
0
        fragP->tc_frag_data.max_prefix_length
12937
0
          = max_prefix_size - count;
12938
0
    }
12939
12940
        /* Use existing segment prefix if possible.  Use CS
12941
     segment prefix in 64-bit mode.  In 32-bit mode, use SS
12942
     segment prefix with ESP/EBP base register and use DS
12943
     segment prefix without ESP/EBP base register.  */
12944
0
        if (i.prefix[SEG_PREFIX])
12945
0
    fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
12946
0
        else if (flag_code == CODE_64BIT)
12947
0
    fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
12948
0
        else if (i.base_reg
12949
0
           && (i.base_reg->reg_num == 4
12950
0
         || i.base_reg->reg_num == 5))
12951
0
    fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
12952
0
        else
12953
0
    fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
12954
0
      }
12955
11.3k
  }
12956
11.6k
    }
12957
12958
  /* NB: Don't work with COND_JUMP86 without i386.  */
12959
11.8k
  if (align_branch_power
12960
0
      && now_seg != absolute_section
12961
0
      && cpu_arch_flags.bitfield.cpui386)
12962
0
    {
12963
      /* Terminate each frag so that we can add prefix and check for
12964
         fused jcc.  */
12965
0
      frag_wane (frag_now);
12966
0
      frag_new (0);
12967
0
    }
12968
12969
#ifdef DEBUG386
12970
  if (flag_debug)
12971
    {
12972
      pi ("" /*line*/, &i);
12973
    }
12974
#endif /* DEBUG386  */
12975
11.8k
}
12976
12977
/* Return the size of the displacement operand N.  */
12978
12979
static int
12980
disp_size (unsigned int n)
12981
9.46k
{
12982
9.46k
  int size = 4;
12983
12984
9.46k
  if (i.types[n].bitfield.disp64)
12985
0
    size = 8;
12986
9.46k
  else if (i.types[n].bitfield.disp8)
12987
2
    size = 1;
12988
9.45k
  else if (i.types[n].bitfield.disp16)
12989
630
    size = 2;
12990
9.46k
  return size;
12991
9.46k
}
12992
12993
/* Return the size of the immediate operand N.  */
12994
12995
static int
12996
imm_size (unsigned int n)
12997
1.08k
{
12998
1.08k
  int size = 4;
12999
1.08k
  if (i.types[n].bitfield.imm64)
13000
0
    size = 8;
13001
1.08k
  else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
13002
911
    size = 1;
13003
169
  else if (i.types[n].bitfield.imm16)
13004
141
    size = 2;
13005
1.08k
  return size;
13006
1.08k
}
13007
13008
static void
13009
output_disp (fragS *insn_start_frag, offsetT insn_start_off)
13010
9.46k
{
13011
9.46k
  char *p;
13012
9.46k
  unsigned int n;
13013
13014
20.1k
  for (n = 0; n < i.operands; n++)
13015
10.7k
    {
13016
10.7k
      if (operand_type_check (i.types[n], disp))
13017
9.46k
  {
13018
9.46k
    int size = disp_size (n);
13019
13020
9.46k
    if (now_seg == absolute_section)
13021
187
      abs_section_offset += size;
13022
9.27k
    else if (i.op[n].disps->X_op == O_constant)
13023
494
      {
13024
494
        offsetT val = i.op[n].disps->X_add_number;
13025
13026
494
        val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
13027
494
             size);
13028
494
        p = frag_more (size);
13029
494
        md_number_to_chars (p, val, size);
13030
494
      }
13031
8.77k
    else
13032
8.77k
      {
13033
8.77k
        enum bfd_reloc_code_real reloc_type;
13034
8.77k
        bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
13035
8.77k
        bool sign = (flag_code == CODE_64BIT && size == 4
13036
8.33k
         && (!want_disp32 (&i.tm)
13037
0
             || (i.tm.opcode_modifier.jump && !i.jumpabsolute
13038
0
           && !i.types[n].bitfield.baseindex)))
13039
442
        || pcrel;
13040
8.77k
        fixS *fixP;
13041
13042
        /* We can't have 8 bit displacement here.  */
13043
8.77k
        gas_assert (!i.types[n].bitfield.disp8);
13044
13045
        /* The PC relative address is computed relative
13046
     to the instruction boundary, so in case immediate
13047
     fields follows, we need to adjust the value.  */
13048
8.77k
        if (pcrel && i.imm_operands)
13049
0
    {
13050
0
      unsigned int n1;
13051
0
      int sz = 0;
13052
13053
0
      for (n1 = 0; n1 < i.operands; n1++)
13054
0
        if (operand_type_check (i.types[n1], imm))
13055
0
          {
13056
      /* Only one immediate is allowed for PC
13057
         relative address, except with .insn.  */
13058
0
      gas_assert (sz == 0 || dot_insn ());
13059
0
      sz += imm_size (n1);
13060
0
          }
13061
      /* We should find at least one immediate.  */
13062
0
      gas_assert (sz != 0);
13063
0
      i.op[n].disps->X_add_number -= sz;
13064
0
    }
13065
13066
8.77k
        p = frag_more (size);
13067
8.77k
        reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
13068
8.77k
        if (GOT_symbol
13069
8.09k
      && GOT_symbol == i.op[n].disps->X_add_symbol
13070
0
      && (((reloc_type == BFD_RELOC_32
13071
0
      || reloc_type == BFD_RELOC_X86_64_32S
13072
0
      || (reloc_type == BFD_RELOC_64
13073
0
          && object_64bit))
13074
0
           && (i.op[n].disps->X_op == O_symbol
13075
0
         || (i.op[n].disps->X_op == O_add
13076
0
             && ((symbol_get_value_expression
13077
0
            (i.op[n].disps->X_op_symbol)->X_op)
13078
0
           == O_subtract))))
13079
0
          || reloc_type == BFD_RELOC_32_PCREL))
13080
0
    {
13081
0
      if (!object_64bit)
13082
0
        {
13083
0
          reloc_type = BFD_RELOC_32_GOT_PCREL;
13084
0
          i.has_gotpc_tls_reloc = true;
13085
0
          i.op[n].disps->X_add_number +=
13086
0
      encoding_length (insn_start_frag, insn_start_off, p);
13087
0
        }
13088
0
      else if (reloc_type == BFD_RELOC_64)
13089
0
        reloc_type = BFD_RELOC_64_GOT_PCREL;
13090
0
      else
13091
        /* Don't do the adjustment for x86-64, as there
13092
           the pcrel addressing is relative to the _next_
13093
           insn, and that is taken care of in other code.  */
13094
0
        reloc_type = BFD_RELOC_X86_64_GOTPC32;
13095
0
    }
13096
8.77k
        else if (align_branch_power)
13097
0
    {
13098
0
      switch (reloc_type)
13099
0
        {
13100
0
        case BFD_RELOC_386_TLS_GD:
13101
0
        case BFD_RELOC_386_TLS_LDM:
13102
0
        case BFD_RELOC_386_TLS_IE:
13103
0
        case BFD_RELOC_386_TLS_IE_32:
13104
0
        case BFD_RELOC_386_TLS_GOTIE:
13105
0
        case BFD_RELOC_386_TLS_GOTDESC:
13106
0
        case BFD_RELOC_386_TLS_DESC_CALL:
13107
0
        case BFD_RELOC_X86_64_TLSGD:
13108
0
        case BFD_RELOC_X86_64_TLSLD:
13109
0
        case BFD_RELOC_X86_64_GOTTPOFF:
13110
0
        case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
13111
0
        case BFD_RELOC_X86_64_CODE_5_GOTTPOFF:
13112
0
        case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
13113
0
        case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
13114
0
        case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
13115
0
        case BFD_RELOC_X86_64_CODE_5_GOTPC32_TLSDESC:
13116
0
        case BFD_RELOC_X86_64_CODE_6_GOTPC32_TLSDESC:
13117
0
        case BFD_RELOC_X86_64_TLSDESC_CALL:
13118
0
          i.has_gotpc_tls_reloc = true;
13119
0
        default:
13120
0
          break;
13121
0
        }
13122
0
    }
13123
8.77k
        fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
13124
8.77k
          size, i.op[n].disps, pcrel,
13125
8.77k
          reloc_type);
13126
13127
8.77k
        if (flag_code == CODE_64BIT && size == 4 && pcrel
13128
0
      && !i.prefix[ADDR_PREFIX])
13129
0
    fixP->fx_signed = 1;
13130
13131
8.77k
        if (i.base_reg && i.base_reg->reg_num == RegIP)
13132
0
    {
13133
0
      if (reloc_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
13134
0
        {
13135
          /* Set fx_tcbit for REX2 prefix.  */
13136
0
          if (is_apx_rex2_encoding ())
13137
0
      fixP->fx_tcbit = 1;
13138
0
          continue;
13139
0
        }
13140
0
    }
13141
        /* In 64-bit, i386_validate_fix updates only (%rip)
13142
     relocations.  */
13143
8.77k
        else if (object_64bit)
13144
8.77k
    continue;
13145
13146
0
#ifdef OBJ_ELF
13147
        /* Check for "call/jmp *mem", "push mem", "mov mem, %reg",
13148
     "movrs mem, %reg", "test %reg, mem" and "binop mem, %reg" where
13149
     binop is one of adc, add, and, cmp, or, sbb, sub, xor, or imul
13150
     instructions without data prefix.  Always generate
13151
     R_386_GOT32X for "sym*GOT" operand in 32-bit mode.  */
13152
0
        unsigned int space = dot_insn () ? i.insn_opcode_space
13153
0
                 : i.tm.opcode_space;
13154
0
        if (i.prefix[DATA_PREFIX] == 0
13155
0
      && (i.rm.mode == 2
13156
0
          || (i.rm.mode == 0 && i.rm.regmem == 5))
13157
0
      && ((space == SPACE_BASE
13158
0
           && i.tm.base_opcode == 0xff
13159
0
           && (i.rm.reg == 2 || i.rm.reg == 4 || i.rm.reg == 6))
13160
0
          || ((space == SPACE_BASE
13161
0
         || space == SPACE_0F38
13162
0
         || space == SPACE_MAP4)
13163
0
        && i.tm.base_opcode == 0x8b)
13164
0
          || ((space == SPACE_BASE
13165
0
         || space == SPACE_MAP4)
13166
0
        && (i.tm.base_opcode == 0x85
13167
0
            || (i.tm.base_opcode
13168
0
          | (i.operands > 2 ? 0x3a : 0x38)) == 0x3b))
13169
0
          || (((space == SPACE_0F
13170
          /* Because of the 0F prefix, no suitable relocation
13171
             exists for this unless it's REX2-encoded.  */
13172
0
          && is_apx_rex2_encoding ())
13173
0
         || space == SPACE_MAP4)
13174
0
        && i.tm.base_opcode == 0xaf)))
13175
0
    {
13176
0
      if (object_64bit)
13177
0
        {
13178
0
          if (reloc_type == BFD_RELOC_X86_64_GOTTPOFF)
13179
0
      {
13180
0
        if (space == SPACE_MAP4)
13181
0
          fixP->fx_tcbit3 = 1;
13182
0
        else if (space == SPACE_0F38 && i.rex)
13183
0
          fixP->fx_tcbit2 = 1;
13184
0
        else if (space == SPACE_0F38 || is_apx_rex2_encoding ())
13185
0
          fixP->fx_tcbit = 1;
13186
0
      }
13187
0
          else if (generate_relax_relocations)
13188
0
      {
13189
0
        if (space == SPACE_MAP4)
13190
0
          {
13191
0
            fixP->fx_tcbit3 = 1;
13192
0
            fixP->fx_tcbit2 = 1;
13193
0
          }
13194
0
        else if (space == SPACE_0F38)
13195
0
          {
13196
0
            fixP->fx_tcbit3 = 1;
13197
0
            if (i.rex)
13198
0
        fixP->fx_tcbit = 1;
13199
0
          }
13200
0
        else if (is_apx_rex2_encoding ())
13201
0
          fixP->fx_tcbit3 = 1;
13202
0
        else if (i.rex)
13203
0
          fixP->fx_tcbit2 = 1;
13204
0
        else
13205
0
          fixP->fx_tcbit = 1;
13206
0
      }
13207
0
        }
13208
0
      else if (generate_relax_relocations
13209
0
         ? (!shared || i.rm.mode != 0 || i.rm.regmem != 5)
13210
0
         : (!shared && i.rm.mode == 0 && i.rm.regmem == 5))
13211
0
        fixP->fx_tcbit2 = 1;
13212
0
    }
13213
0
#endif
13214
0
      }
13215
9.46k
  }
13216
10.7k
    }
13217
9.46k
}
13218
13219
static void
13220
output_imm (fragS *insn_start_frag, offsetT insn_start_off)
13221
1.08k
{
13222
1.08k
  char *p;
13223
1.08k
  unsigned int n;
13224
13225
3.22k
  for (n = 0; n < i.operands; n++)
13226
2.14k
    {
13227
2.14k
      if (operand_type_check (i.types[n], imm))
13228
1.08k
  {
13229
1.08k
    int size = imm_size (n);
13230
13231
1.08k
    if (now_seg == absolute_section)
13232
128
      abs_section_offset += size;
13233
952
    else if (i.op[n].imms->X_op == O_constant)
13234
755
      {
13235
755
        offsetT val;
13236
13237
755
        val = offset_in_range (i.op[n].imms->X_add_number,
13238
755
             size);
13239
755
        p = frag_more (size);
13240
755
        md_number_to_chars (p, val, size);
13241
755
      }
13242
197
    else
13243
197
      {
13244
        /* Not absolute_section.
13245
     Need a 32-bit fixup (don't support 8bit
13246
     non-absolute imms).  Try to support other
13247
     sizes ...  */
13248
197
        enum bfd_reloc_code_real reloc_type;
13249
197
        int sign;
13250
13251
197
        if (i.types[n].bitfield.imm32s
13252
3
      && (i.suffix == QWORD_MNEM_SUFFIX
13253
3
          || (!i.suffix && i.tm.opcode_modifier.no_lsuf)
13254
0
          || (i.prefix[REX_PREFIX] & REX_W)
13255
0
          || dot_insn ()))
13256
3
    sign = 1;
13257
194
        else
13258
194
    sign = 0;
13259
13260
197
        p = frag_more (size);
13261
197
        reloc_type = reloc (size, 0, sign, i.reloc[n]);
13262
13263
        /*   This is tough to explain.  We end up with this one if we
13264
         * have operands that look like
13265
         * "_GLOBAL_OFFSET_TABLE_+[.-.L284]".  The goal here is to
13266
         * obtain the absolute address of the GOT, and it is strongly
13267
         * preferable from a performance point of view to avoid using
13268
         * a runtime relocation for this.  The actual sequence of
13269
         * instructions often look something like:
13270
         *
13271
         *  call  .L66
13272
         * .L66:
13273
         *  popl  %ebx
13274
         *  addl  $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
13275
         *
13276
         *   The call and pop essentially return the absolute address
13277
         * of the label .L66 and store it in %ebx.  The linker itself
13278
         * will ultimately change the first operand of the addl so
13279
         * that %ebx points to the GOT, but to keep things simple, the
13280
         * .o file must have this operand set so that it generates not
13281
         * the absolute address of .L66, but the absolute address of
13282
         * itself.  This allows the linker itself simply treat a GOTPC
13283
         * relocation as asking for a pcrel offset to the GOT to be
13284
         * added in, and the addend of the relocation is stored in the
13285
         * operand field for the instruction itself.
13286
         *
13287
         *   Our job here is to fix the operand so that it would add
13288
         * the correct offset so that %ebx would point to itself.  The
13289
         * thing that is tricky is that .-.L66 will point to the
13290
         * beginning of the instruction, so we need to further modify
13291
         * the operand so that it will point to itself.  There are
13292
         * other cases where you have something like:
13293
         *
13294
         *  .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
13295
         *
13296
         * and here no correction would be required.  Internally in
13297
         * the assembler we treat operands of this form as not being
13298
         * pcrel since the '.' is explicitly mentioned, and I wonder
13299
         * whether it would simplify matters to do it this way.  Who
13300
         * knows.  In earlier versions of the PIC patches, the
13301
         * pcrel_adjust field was used to store the correction, but
13302
         * since the expression is not pcrel, I felt it would be
13303
         * confusing to do it this way.  */
13304
13305
197
        if ((reloc_type == BFD_RELOC_32
13306
190
       || reloc_type == BFD_RELOC_X86_64_32S
13307
187
       || reloc_type == BFD_RELOC_64)
13308
10
      && GOT_symbol
13309
0
      && GOT_symbol == i.op[n].imms->X_add_symbol
13310
0
      && (i.op[n].imms->X_op == O_symbol
13311
0
          || (i.op[n].imms->X_op == O_add
13312
0
        && ((symbol_get_value_expression
13313
0
             (i.op[n].imms->X_op_symbol)->X_op)
13314
0
            == O_subtract))))
13315
0
    {
13316
0
      if (!object_64bit)
13317
0
        reloc_type = BFD_RELOC_32_GOT_PCREL;
13318
0
      else if (size == 4)
13319
0
        reloc_type = BFD_RELOC_X86_64_GOTPC32;
13320
0
      else if (size == 8)
13321
0
        reloc_type = BFD_RELOC_64_GOT_PCREL;
13322
0
      i.has_gotpc_tls_reloc = true;
13323
0
      i.op[n].imms->X_add_number +=
13324
0
        encoding_length (insn_start_frag, insn_start_off, p);
13325
0
    }
13326
197
        fix_new_exp (frag_now, p - frag_now->fr_literal, size,
13327
197
         i.op[n].imms, 0, reloc_type);
13328
197
      }
13329
1.08k
  }
13330
2.14k
    }
13331
1.08k
}
13332

13333
/* x86_cons_fix_new is called via the expression parsing code when a
13334
   reloc is needed.  We use this hook to get the correct .got reloc.  */
13335
static int cons_sign = -1;
13336
13337
void
13338
x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
13339
      expressionS *exp, bfd_reloc_code_real_type r)
13340
3.16k
{
13341
3.16k
  r = reloc (len, 0, cons_sign, r);
13342
13343
#ifdef TE_PE
13344
  if (exp->X_op == O_secrel)
13345
    {
13346
      exp->X_op = O_symbol;
13347
      r = BFD_RELOC_32_SECREL;
13348
    }
13349
  else if (exp->X_op == O_secidx)
13350
    r = BFD_RELOC_16_SECIDX;
13351
#endif
13352
13353
3.16k
  fix_new_exp (frag, off, len, exp, 0, r);
13354
3.16k
}
13355
13356
/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
13357
   purpose of the `.dc.a' internal pseudo-op.  */
13358
13359
int
13360
x86_address_bytes (void)
13361
3.28k
{
13362
3.28k
  if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
13363
0
    return 4;
13364
3.28k
  return stdoutput->arch_info->bits_per_address / 8;
13365
3.28k
}
13366
13367
#if (defined (OBJ_ELF) || defined (OBJ_MACH_O) || defined (TE_PE))
13368
/* Parse operands of the form
13369
   <symbol>@GOTOFF+<nnn>
13370
   and similar .plt or .got references.
13371
13372
   If we find one, set up the correct relocation in RELOC and copy the
13373
   input string, minus the `@GOTOFF' into a malloc'd buffer for
13374
   parsing by the calling routine.  Return this buffer, and if ADJUST
13375
   is non-null set it to the length of the string we removed from the
13376
   input line.  Otherwise return NULL.  */
13377
static char *
13378
lex_got (enum bfd_reloc_code_real *rel,
13379
   int *adjust,
13380
   i386_operand_type *types)
13381
14.4k
{
13382
  /* Some of the relocations depend on the size of what field is to
13383
     be relocated.  But in our callers i386_immediate and i386_displacement
13384
     we don't yet know the operand size (this will be set by insn
13385
     matching).  Hence we record the word32 relocation here,
13386
     and adjust the reloc according to the real size in reloc().  */
13387
14.4k
  char *cp;
13388
14.4k
  unsigned int j;
13389
13390
88.2k
  for (cp = input_line_pointer; *cp != '@'; cp++)
13391
80.0k
    if (is_end_of_stmt (*cp) || *cp == ',')
13392
6.22k
      return NULL;
13393
13394
50.7k
  for (j = 0; j < ARRAY_SIZE (gotrel); j++)
13395
50.6k
    {
13396
50.6k
      int len = gotrel[j].len;
13397
50.6k
      if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
13398
8.12k
  {
13399
8.12k
    if (gotrel[j].rel[object_64bit] != 0)
13400
8.12k
      {
13401
8.12k
        int first, second;
13402
8.12k
        char *tmpbuf, *past_reloc;
13403
13404
8.12k
        i.has_gotrel = true;
13405
8.12k
        *rel = gotrel[j].rel[object_64bit];
13406
13407
8.12k
        if (types)
13408
8.11k
    {
13409
8.11k
      if (flag_code != CODE_64BIT)
13410
0
        {
13411
0
          types->bitfield.imm32 = 1;
13412
0
          types->bitfield.disp32 = 1;
13413
0
        }
13414
8.11k
      else
13415
8.11k
        *types = gotrel[j].types64;
13416
8.11k
    }
13417
13418
8.12k
        if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
13419
14
    GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
13420
13421
        /* The length of the first part of our input line.  */
13422
8.12k
        first = cp - input_line_pointer;
13423
13424
        /* The second part goes from after the reloc token until
13425
     (and including) an end_of_line char or comma.  */
13426
8.12k
        past_reloc = cp + 1 + len;
13427
8.12k
        cp = past_reloc;
13428
259k
        while (!is_end_of_stmt (*cp) && *cp != ',')
13429
251k
    ++cp;
13430
8.12k
        second = cp + 1 - past_reloc;
13431
13432
        /* Allocate and copy string.  The trailing NUL shouldn't
13433
     be necessary, but be safe.  */
13434
8.12k
        tmpbuf = XNEWVEC (char, first + second + 2);
13435
8.12k
        memcpy (tmpbuf, input_line_pointer, first);
13436
8.12k
        if (second != 0 && !is_whitespace (*past_reloc))
13437
    /* Replace the relocation token with ' ', so that
13438
       errors like foo@GOTOFF1 will be detected.  */
13439
35
    tmpbuf[first++] = ' ';
13440
8.09k
        else
13441
    /* Increment length by 1 if the relocation token is
13442
       removed.  */
13443
8.09k
    len++;
13444
8.12k
        if (adjust)
13445
32
    *adjust = len;
13446
8.12k
        memcpy (tmpbuf + first, past_reloc, second);
13447
8.12k
        tmpbuf[first + second] = '\0';
13448
8.12k
        return tmpbuf;
13449
8.12k
      }
13450
13451
0
    as_bad (_("@%s reloc is not supported with %d-bit output format"),
13452
0
      gotrel[j].str, 1 << (5 + object_64bit));
13453
0
    return NULL;
13454
8.12k
  }
13455
50.6k
    }
13456
13457
  /* Might be a symbol version string.  Don't as_bad here.  */
13458
98
  return NULL;
13459
8.22k
}
13460
#else
13461
# define lex_got(reloc, adjust, types) NULL
13462
#endif
13463
13464
bfd_reloc_code_real_type
13465
x86_cons (expressionS *exp, int size)
13466
2.58k
{
13467
2.58k
  bfd_reloc_code_real_type got_reloc = NO_RELOC;
13468
13469
2.58k
  intel_syntax = -intel_syntax;
13470
2.58k
  exp->X_md = 0;
13471
2.58k
  expr_mode = expr_operator_none;
13472
13473
2.58k
#if defined (OBJ_ELF) || defined (TE_PE)
13474
2.58k
  if (size == 4
13475
# ifdef TE_PE
13476
      || (size == 2)
13477
# endif
13478
1.92k
      || (object_64bit && size == 8))
13479
825
    {
13480
      /* Handle @GOTOFF and the like in an expression.  */
13481
825
      char *save;
13482
825
      char *gotfree_input_line;
13483
825
      int adjust = 0;
13484
13485
825
      save = input_line_pointer;
13486
825
      gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
13487
825
      if (gotfree_input_line)
13488
9
  input_line_pointer = gotfree_input_line;
13489
13490
825
      expression (exp);
13491
13492
825
      if (gotfree_input_line)
13493
9
  {
13494
    /* expression () has merrily parsed up to the end of line,
13495
       or a comma - in the wrong buffer.  Transfer how far
13496
       input_line_pointer has moved to the right buffer.  */
13497
9
    input_line_pointer = (save
13498
9
        + (input_line_pointer - gotfree_input_line)
13499
9
        + adjust);
13500
9
    free (gotfree_input_line);
13501
9
    if (exp->X_op == O_constant
13502
3
        || exp->X_op == O_absent
13503
3
        || exp->X_op == O_illegal
13504
3
        || exp->X_op == O_register
13505
3
        || exp->X_op == O_big)
13506
7
      {
13507
7
        char c = *input_line_pointer;
13508
7
        *input_line_pointer = 0;
13509
7
        as_bad (_("missing or invalid expression `%s'"), save);
13510
7
        *input_line_pointer = c;
13511
7
      }
13512
2
    else if ((got_reloc == BFD_RELOC_386_PLT32
13513
2
        || got_reloc == BFD_RELOC_32_PLT_PCREL)
13514
0
       && exp->X_op != O_symbol)
13515
0
      {
13516
0
        char c = *input_line_pointer;
13517
0
        *input_line_pointer = 0;
13518
0
        as_bad (_("invalid PLT expression `%s'"), save);
13519
0
        *input_line_pointer = c;
13520
0
      }
13521
9
  }
13522
825
    }
13523
1.75k
  else
13524
1.75k
#endif
13525
1.75k
    expression (exp);
13526
13527
2.58k
  intel_syntax = -intel_syntax;
13528
13529
2.58k
  if (intel_syntax)
13530
2.10k
    i386_intel_simplify (exp, false);
13531
13532
  /* If not 64bit, massage value, to account for wraparound when !BFD64.  */
13533
2.58k
  if (size <= 4 && expr_mode == expr_operator_present
13534
1.13k
      && exp->X_op == O_constant && !object_64bit)
13535
0
    exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
13536
13537
2.58k
  return got_reloc;
13538
2.58k
}
13539
13540
static void
13541
signed_cons (int size)
13542
13
{
13543
13
  if (object_64bit)
13544
13
    cons_sign = 1;
13545
13
  cons (size);
13546
13
  cons_sign = -1;
13547
13
}
13548
13549
static void
13550
s_insn (int dummy ATTRIBUTE_UNUSED)
13551
3.27k
{
13552
3.27k
  char mnemonic[MAX_MNEM_SIZE], *line = input_line_pointer, *ptr;
13553
3.27k
  char *saved_ilp = find_end_of_line (line, false), saved_char;
13554
3.27k
  const char *end;
13555
3.27k
  unsigned int j;
13556
3.27k
  valueT val;
13557
3.27k
  bool vex = false, xop = false;
13558
3.27k
  enum { evex_none, evex_basic, evex_nd } evex = evex_none;
13559
3.27k
  struct last_insn *last_insn;
13560
13561
3.27k
  init_globals ();
13562
13563
3.27k
  saved_char = *saved_ilp;
13564
3.27k
  *saved_ilp = 0;
13565
13566
3.27k
  end = parse_insn (line, mnemonic, parse_prefix);
13567
3.27k
  if (end == NULL)
13568
2
    {
13569
469
  bad:
13570
469
      *saved_ilp = saved_char;
13571
469
      input_line_pointer = saved_ilp;
13572
469
      ignore_rest_of_line ();
13573
469
      i.tm.mnem_off = 0;
13574
469
      memset (&pp, 0, sizeof (pp));
13575
469
      return;
13576
2
    }
13577
3.27k
  line += end - line;
13578
13579
3.27k
  current_templates.start = &i.tm;
13580
3.27k
  current_templates.end = &i.tm + 1;
13581
3.27k
  i.tm.mnem_off = MN__insn;
13582
3.27k
  i.tm.extension_opcode = None;
13583
13584
3.27k
  if (startswith (line, "VEX")
13585
1.34k
      && (line[3] == '.' || is_whitespace (line[3])))
13586
1.18k
    {
13587
1.18k
      vex = true;
13588
1.18k
      line += 3;
13589
1.18k
    }
13590
2.08k
  else if (startswith (line, "XOP") && ISDIGIT (line[3]))
13591
2
    {
13592
2
      char *e;
13593
2
      unsigned long n = strtoul (line + 3, &e, 16);
13594
13595
2
      if (e == line + 5 && n >= 0x08 && n <= 0x1f
13596
0
    && (*e == '.' || is_whitespace (*e)))
13597
0
  {
13598
0
    xop = true;
13599
    /* Arrange for build_vex_prefix() to emit 0x8f.  */
13600
0
    i.tm.opcode_space = SPACE_XOP08;
13601
0
    i.insn_opcode_space = n;
13602
0
    line = e;
13603
0
  }
13604
2
    }
13605
2.08k
  else if (startswith (line, "EVEX")
13606
1.32k
     && (line[4] == '.' || is_whitespace (line[4])))
13607
1.32k
    {
13608
1.32k
      evex = evex_basic;
13609
1.32k
      line += 4;
13610
1.32k
    }
13611
13612
3.27k
  if (vex || xop
13613
3.27k
      ? pp.encoding == encoding_evex
13614
3.27k
      : evex
13615
2.08k
  ? pp.encoding == encoding_vex
13616
1.32k
    || pp.encoding == encoding_vex3
13617
2.08k
  : pp.encoding != encoding_default)
13618
441
    {
13619
441
      as_bad (_("pseudo-prefix conflicts with encoding specifier"));
13620
441
      goto bad;
13621
441
    }
13622
13623
2.82k
  if (line > end && pp.encoding == encoding_default)
13624
2.06k
    pp.encoding = evex ? encoding_evex : encoding_vex;
13625
13626
2.82k
  if (pp.encoding != encoding_default)
13627
2.06k
    {
13628
      /* Only address size and segment override prefixes are permitted with
13629
         VEX/XOP/EVEX encodings.  */
13630
2.06k
      const unsigned char *p = i.prefix;
13631
13632
16.5k
      for (j = 0; j < ARRAY_SIZE (i.prefix); ++j, ++p)
13633
14.4k
  {
13634
14.4k
    if (!*p)
13635
14.4k
      continue;
13636
13637
0
    switch (j)
13638
0
      {
13639
0
      case SEG_PREFIX:
13640
0
      case ADDR_PREFIX:
13641
0
        break;
13642
0
      default:
13643
0
      as_bad (_("illegal prefix used with VEX/XOP/EVEX"));
13644
0
      goto bad;
13645
0
      }
13646
0
  }
13647
2.06k
    }
13648
13649
2.82k
  if (line > end && *line == '.')
13650
1.59k
    {
13651
      /* Length specifier (VEX.L, XOP.L, EVEX.L'L).  */
13652
1.59k
      switch (line[1])
13653
1.59k
  {
13654
1.28k
  case 'L':
13655
1.28k
    switch (line[2])
13656
1.28k
      {
13657
0
      case '0':
13658
0
        if (evex)
13659
0
    i.tm.opcode_modifier.evex = EVEX128;
13660
0
        else
13661
0
    i.tm.opcode_modifier.vex = VEX128;
13662
0
        break;
13663
13664
3
      case '1':
13665
3
        if (evex)
13666
3
    i.tm.opcode_modifier.evex = EVEX256;
13667
0
        else
13668
0
    i.tm.opcode_modifier.vex = VEX256;
13669
3
        break;
13670
13671
1
      case '2':
13672
1
        if (evex)
13673
0
    i.tm.opcode_modifier.evex = EVEX512;
13674
1
        break;
13675
13676
0
      case '3':
13677
0
        if (evex)
13678
0
    i.tm.opcode_modifier.evex = EVEX_L3;
13679
0
        break;
13680
13681
0
      case 'I':
13682
0
        if (line[3] == 'G')
13683
0
    {
13684
0
      if (evex)
13685
0
        i.tm.opcode_modifier.evex = EVEXLIG;
13686
0
      else
13687
0
        i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
13688
0
      ++line;
13689
0
    }
13690
0
        break;
13691
1.28k
      }
13692
13693
1.28k
    if (i.tm.opcode_modifier.vex || i.tm.opcode_modifier.evex)
13694
3
      line += 3;
13695
1.28k
    break;
13696
13697
0
  case '1':
13698
0
    if (line[2] == '2' && line[3] == '8')
13699
0
      {
13700
0
        if (evex)
13701
0
    i.tm.opcode_modifier.evex = EVEX128;
13702
0
        else
13703
0
    i.tm.opcode_modifier.vex = VEX128;
13704
0
        line += 4;
13705
0
      }
13706
0
    break;
13707
13708
0
  case '2':
13709
0
    if (line[2] == '5' && line[3] == '6')
13710
0
      {
13711
0
        if (evex)
13712
0
    i.tm.opcode_modifier.evex = EVEX256;
13713
0
        else
13714
0
    i.tm.opcode_modifier.vex = VEX256;
13715
0
        line += 4;
13716
0
      }
13717
0
    break;
13718
13719
0
  case '5':
13720
0
    if (evex && line[2] == '1' && line[3] == '2')
13721
0
      {
13722
0
        i.tm.opcode_modifier.evex = EVEX512;
13723
0
        line += 4;
13724
0
      }
13725
0
    break;
13726
1.59k
  }
13727
1.59k
    }
13728
13729
2.82k
  if (line > end && *line == '.')
13730
1.59k
    {
13731
      /* embedded prefix (VEX.pp, XOP.pp, EVEX.pp).  */
13732
1.59k
      switch (line[1])
13733
1.59k
  {
13734
0
  case 'N':
13735
0
    if (line[2] == 'P')
13736
0
      line += 3;
13737
0
    break;
13738
13739
0
  case '6':
13740
0
    if (line[2] == '6')
13741
0
      {
13742
0
        i.tm.opcode_modifier.opcodeprefix = PREFIX_0X66;
13743
0
        line += 3;
13744
0
      }
13745
0
    break;
13746
13747
261
  case 'F': case 'f':
13748
261
    if (line[2] == '3')
13749
174
      {
13750
174
        i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
13751
174
        line += 3;
13752
174
      }
13753
87
    else if (line[2] == '2')
13754
0
      {
13755
0
        i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF2;
13756
0
        line += 3;
13757
0
      }
13758
261
    break;
13759
1.59k
  }
13760
1.59k
    }
13761
13762
2.82k
  if (line > end && !xop && *line == '.')
13763
1.41k
    {
13764
      /* Encoding space (VEX.mmmmm, EVEX.mmmm).  */
13765
1.41k
      switch (line[1])
13766
1.41k
  {
13767
0
  case '0':
13768
0
    if (TOUPPER (line[2]) != 'F')
13769
0
      break;
13770
0
    if (line[3] == '.' || is_whitespace (line[3]))
13771
0
      {
13772
0
        i.insn_opcode_space = SPACE_0F;
13773
0
        line += 3;
13774
0
      }
13775
0
    else if (line[3] == '3'
13776
0
       && (line[4] == '8' || TOUPPER (line[4]) == 'A')
13777
0
       && (line[5] == '.' || is_whitespace (line[5])))
13778
0
      {
13779
0
        i.insn_opcode_space = line[4] == '8' ? SPACE_0F38 : SPACE_0F3A;
13780
0
        line += 5;
13781
0
      }
13782
0
    break;
13783
13784
31
  case 'M':
13785
31
    if (ISDIGIT (line[2]) && line[2] != '0')
13786
31
      {
13787
31
        char *e;
13788
31
        unsigned long n = strtoul (line + 2, &e, 10);
13789
13790
31
        if (n <= (evex ? 15 : 31)
13791
31
      && (*e == '.' || is_whitespace (*e)))
13792
31
    {
13793
31
      i.insn_opcode_space = n;
13794
31
      line = e;
13795
31
    }
13796
31
      }
13797
31
    break;
13798
1.41k
  }
13799
1.41k
    }
13800
13801
2.82k
  if (line > end && *line == '.' && line[1] == 'W')
13802
16
    {
13803
      /* VEX.W, XOP.W, EVEX.W  */
13804
16
      switch (line[2])
13805
16
  {
13806
7
  case '0':
13807
7
    i.tm.opcode_modifier.vexw = VEXW0;
13808
7
    break;
13809
13810
1
  case '1':
13811
1
    i.tm.opcode_modifier.vexw = VEXW1;
13812
1
    break;
13813
13814
0
  case 'I':
13815
0
    if (line[3] == 'G')
13816
0
      {
13817
0
        i.tm.opcode_modifier.vexw = VEXWIG;
13818
0
        ++line;
13819
0
      }
13820
0
    break;
13821
16
  }
13822
13823
16
      if (i.tm.opcode_modifier.vexw)
13824
8
  line += 3;
13825
16
    }
13826
13827
2.82k
  if (line > end && evex && *line == '.')
13828
1.28k
    {
13829
1.28k
      if (line[1] == 'N' && line[2] == 'D')
13830
0
  {
13831
0
    evex = evex_nd;
13832
0
    line += 3;
13833
0
  }
13834
1.28k
      else if (line[1] == 'Z' && line[2] == 'U')
13835
0
  {
13836
0
    i.tm.opcode_modifier.operandconstraint = ZERO_UPPER;
13837
0
    line += 3;
13838
0
  }
13839
1.28k
    }
13840
13841
2.82k
  if (line > end && *line && !is_whitespace (*line))
13842
1.57k
    {
13843
      /* Improve diagnostic a little.  */
13844
1.57k
      if (*line == '.' && line[1] && !is_whitespace (line[1]))
13845
1.38k
  ++line;
13846
1.57k
      goto done;
13847
1.57k
    }
13848
13849
  /* Before processing the opcode expression, find trailing "+r" or
13850
     "/<digit>" specifiers.  */
13851
1.25k
  for (ptr = line; ; ++ptr)
13852
1.79k
    {
13853
1.79k
      unsigned long n;
13854
1.79k
      char *e;
13855
13856
1.79k
      ptr = strpbrk (ptr, "+/,");
13857
1.79k
      if (ptr == NULL || *ptr == ',')
13858
963
  break;
13859
13860
836
      if (*ptr == '+' && ptr[1] == 'r'
13861
26
    && (ptr[2] == ',' || (is_whitespace (ptr[2]) && ptr[3] == ',')))
13862
20
  {
13863
20
    *ptr = ' ';
13864
20
    ptr[1] = ' ';
13865
20
    i.short_form = true;
13866
20
    break;
13867
20
  }
13868
13869
816
      if (*ptr == '/' && ISDIGIT (ptr[1])
13870
413
    && (n = strtoul (ptr + 1, &e, 8)) < 8
13871
410
    && e == ptr + 2
13872
277
    && (ptr[2] == ',' || (is_whitespace (ptr[2]) && ptr[3] == ',')))
13873
276
  {
13874
276
    *ptr = ' ';
13875
276
    ptr[1] = ' ';
13876
276
    i.tm.extension_opcode = n;
13877
276
    i.tm.opcode_modifier.modrm = 1;
13878
276
    break;
13879
276
  }
13880
816
    }
13881
13882
1.25k
  input_line_pointer = line;
13883
1.25k
  val = get_absolute_expression ();
13884
1.25k
  line = input_line_pointer;
13885
13886
1.25k
  if (i.short_form && (val & 7))
13887
0
    as_warn ("`+r' assumes low three opcode bits to be clear");
13888
13889
2.85k
  for (j = 1; j < sizeof(val); ++j)
13890
2.65k
    if (!(val >> (j * 8)))
13891
1.05k
      break;
13892
13893
  /* Trim off a prefix if present.  */
13894
1.25k
  if (j > 1 && !vex && !xop && !evex)
13895
100
    {
13896
100
      uint8_t byte = val >> ((j - 1) * 8);
13897
13898
100
      switch (byte)
13899
100
  {
13900
0
  case DATA_PREFIX_OPCODE:
13901
8
  case REPE_PREFIX_OPCODE:
13902
8
  case REPNE_PREFIX_OPCODE:
13903
8
    if (!add_prefix (byte))
13904
0
      goto bad;
13905
8
    val &= ((uint64_t)1 << (--j * 8)) - 1;
13906
8
    break;
13907
100
  }
13908
100
    }
13909
13910
1.25k
  if (evex == evex_basic && *line == '{')
13911
18
    {
13912
18
      int length = check_Scc_OszcOperations (line);
13913
13914
18
      if (length > 0)
13915
0
  {
13916
0
    line += length;
13917
0
    if (is_whitespace (*line))
13918
0
      ++line;
13919
13920
0
    if (i.tm.opcode_modifier.operandconstraint)
13921
0
      {
13922
0
        as_bad (_("SCC/OSZC specifier cannot be used here"));
13923
0
        goto bad;
13924
0
      }
13925
0
    i.tm.opcode_modifier.operandconstraint = SCC;
13926
0
  }
13927
18
    }
13928
13929
  /* Parse operands, if any, before evaluating encoding space.  */
13930
1.25k
  if (*line == ',')
13931
172
    {
13932
172
      i.memshift = -1;
13933
13934
172
      ptr = parse_operands (line + 1, &i386_mnemonics[MN__insn]);
13935
172
      this_operand = -1;
13936
172
      if (!ptr)
13937
26
  goto bad;
13938
146
      line = ptr;
13939
13940
146
      if (!i.operands)
13941
2
  {
13942
2
    as_bad (_("expecting operand after ','; got nothing"));
13943
2
    goto done;
13944
2
  }
13945
13946
144
      if (i.mem_operands > 1)
13947
1
  {
13948
1
    as_bad (_("too many memory references for `%s'"),
13949
1
      &i386_mnemonics[MN__insn]);
13950
1
    goto done;
13951
1
  }
13952
13953
      /* No need to distinguish encoding_evex and encoding_evex512.  */
13954
143
      if (pp.encoding == encoding_evex512)
13955
0
  pp.encoding = encoding_evex;
13956
143
    }
13957
13958
  /* Trim off encoding space.  */
13959
1.23k
  if (j > 1 && !i.insn_opcode_space && (val >> ((j - 1) * 8)) == 0x0f)
13960
44
    {
13961
44
      uint8_t byte = val >> ((--j - 1) * 8);
13962
13963
44
      i.insn_opcode_space = SPACE_0F;
13964
44
      switch (byte & -(j > 1 && !pp.rex2_encoding
13965
44
           && (pp.encoding != encoding_egpr || evex)))
13966
44
  {
13967
0
  case 0x38:
13968
0
    i.insn_opcode_space = SPACE_0F38;
13969
0
    --j;
13970
0
    break;
13971
0
  case 0x3a:
13972
0
    i.insn_opcode_space = SPACE_0F3A;
13973
0
    --j;
13974
0
    break;
13975
44
  }
13976
44
      i.tm.opcode_space = i.insn_opcode_space;
13977
44
      val &= ((uint64_t)1 << (j * 8)) - 1;
13978
44
    }
13979
1.23k
  if (!i.tm.opcode_space && (vex || evex))
13980
    /* Arrange for build_vex_prefix() to properly emit 0xC4/0xC5.
13981
       Also avoid hitting abort() there or in build_evex_prefix().  */
13982
472
    i.tm.opcode_space = i.insn_opcode_space == SPACE_0F ? SPACE_0F
13983
472
               : SPACE_0F38;
13984
13985
1.23k
  if (j > 2)
13986
224
    {
13987
224
      as_bad (_("opcode residual (%#"PRIx64") too wide"), (uint64_t) val);
13988
224
      goto done;
13989
224
    }
13990
1.00k
  i.opcode_length = j;
13991
13992
  /* Handle operands, if any.  */
13993
1.00k
  if (i.operands)
13994
139
    {
13995
139
      i386_operand_type combined;
13996
139
      expressionS *disp_exp = NULL;
13997
139
      bool changed;
13998
13999
139
      if (pp.encoding == encoding_egpr)
14000
0
  {
14001
0
    if (vex || xop)
14002
0
      {
14003
0
        as_bad (_("eGPR use conflicts with encoding specifier"));
14004
0
        goto done;
14005
0
      }
14006
0
    if (evex)
14007
0
      pp.encoding = encoding_evex;
14008
0
    else
14009
0
      pp.encoding = encoding_default;
14010
0
  }
14011
14012
      /* Are we to emit ModR/M encoding?  */
14013
139
      if (!i.short_form
14014
121
    && (i.mem_operands
14015
12
        || i.reg_operands > (pp.encoding != encoding_default)
14016
12
        || i.tm.extension_opcode != None))
14017
109
  i.tm.opcode_modifier.modrm = 1;
14018
14019
139
      if (!i.tm.opcode_modifier.modrm
14020
30
    && (i.reg_operands
14021
30
        > i.short_form + 0U + (pp.encoding != encoding_default)
14022
30
        || i.mem_operands))
14023
0
  {
14024
0
    as_bad (_("too many register/memory operands"));
14025
0
    goto done;
14026
0
  }
14027
14028
      /* Enforce certain constraints on operands.  */
14029
139
      switch (i.reg_operands + i.mem_operands
14030
139
        + (i.tm.extension_opcode != None)
14031
139
        + (i.tm.opcode_modifier.operandconstraint == SCC))
14032
139
  {
14033
8
  case 0:
14034
8
    if (i.short_form)
14035
0
      {
14036
0
        as_bad (_("too few register/memory operands"));
14037
0
        goto done;
14038
0
      }
14039
    /* Fall through.  */
14040
61
  case 1:
14041
61
    if (i.tm.opcode_modifier.modrm)
14042
31
      {
14043
31
        as_bad (_("too few register/memory operands"));
14044
31
        goto done;
14045
31
      }
14046
    /* Fall through.  */
14047
107
  case 2:
14048
107
    if (evex == evex_nd)
14049
0
      {
14050
0
        as_bad (_("too few register/memory operands"));
14051
0
        goto done;
14052
0
      }
14053
107
    break;
14054
14055
107
  case 4:
14056
0
    if (i.imm_operands
14057
0
        && (i.op[0].imms->X_op != O_constant
14058
0
      || !fits_in_imm4 (i.op[0].imms->X_add_number)))
14059
0
      {
14060
0
        as_bad (_("constant doesn't fit in %d bits"), evex ? 3 : 4);
14061
0
        goto done;
14062
0
      }
14063
    /* Fall through.  */
14064
1
  case 3:
14065
1
    if (i.tm.opcode_modifier.operandconstraint == SCC)
14066
0
      break;
14067
1
    if (pp.encoding != encoding_default)
14068
0
      {
14069
0
        i.tm.opcode_modifier.vexvvvv = (i.tm.extension_opcode == None
14070
0
                && evex != evex_nd)
14071
0
               ? VexVVVV_SRC1 : VexVVVV_DST;
14072
0
        break;
14073
0
      }
14074
    /* Fall through.  */
14075
1
  default:
14076
1
    as_bad (_("too many register/memory operands"));
14077
1
    goto done;
14078
139
  }
14079
14080
      /* Bring operands into canonical order (imm, mem, reg).  */
14081
107
      do
14082
107
  {
14083
107
    changed = false;
14084
14085
110
    for (j = 1; j < i.operands; ++j)
14086
3
      {
14087
3
        if ((!operand_type_check (i.types[j - 1], imm)
14088
1
       && operand_type_check (i.types[j], imm))
14089
3
      || (i.types[j - 1].bitfield.class != ClassNone
14090
0
          && i.types[j].bitfield.class == ClassNone))
14091
0
    {
14092
0
      swap_2_operands (j - 1, j);
14093
0
      changed = true;
14094
0
    }
14095
3
      }
14096
107
  }
14097
107
      while (changed);
14098
14099
      /* For Intel syntax swap the order of register operands.  */
14100
107
      if (intel_syntax)
14101
20
  switch (i.reg_operands)
14102
20
    {
14103
0
    case 0:
14104
20
    case 1:
14105
20
      break;
14106
14107
0
    case 4:
14108
0
      swap_2_operands (i.imm_operands + i.mem_operands + 1, i.operands - 2);
14109
      /* Fall through.  */
14110
0
    case 3:
14111
0
    case 2:
14112
0
      swap_2_operands (i.imm_operands + i.mem_operands, i.operands - 1);
14113
0
      break;
14114
14115
0
    default:
14116
0
      abort ();
14117
20
    }
14118
14119
      /* Enforce constraints when using VSIB.  */
14120
107
      if (i.index_reg
14121
0
    && (i.index_reg->reg_type.bitfield.xmmword
14122
0
        || i.index_reg->reg_type.bitfield.ymmword
14123
0
        || i.index_reg->reg_type.bitfield.zmmword))
14124
0
  {
14125
0
    if (pp.encoding == encoding_default)
14126
0
      {
14127
0
        as_bad (_("VSIB unavailable with legacy encoding"));
14128
0
        goto done;
14129
0
      }
14130
14131
0
    if (pp.encoding == encoding_evex
14132
0
        && i.reg_operands > 1)
14133
0
      {
14134
        /* We could allow two register operands, encoding the 2nd one in
14135
     an 8-bit immediate like for 4-register-operand insns, but that
14136
     would require ugly fiddling with process_operands() and/or
14137
     build_modrm_byte().  */
14138
0
        as_bad (_("too many register operands with VSIB"));
14139
0
        goto done;
14140
0
      }
14141
14142
0
    i.tm.opcode_modifier.sib = 1;
14143
0
  }
14144
14145
      /* Establish operand size encoding.  */
14146
107
      operand_type_set (&combined, 0);
14147
14148
207
      for (j = i.imm_operands; j < i.operands; ++j)
14149
100
  {
14150
    /* Look for 8-bit operands that use old registers.  */
14151
100
    if (pp.encoding != encoding_default
14152
82
        && flag_code == CODE_64BIT
14153
6
        && i.types[j].bitfield.class == Reg
14154
5
        && i.types[j].bitfield.byte
14155
0
        && !(i.op[j].regs->reg_flags & (RegRex | RegRex2 | RegRex64))
14156
0
        && i.op[j].regs->reg_num > 3)
14157
0
      as_bad (_("can't encode register '%s%s' with VEX/XOP/EVEX"),
14158
0
        register_prefix, i.op[j].regs->reg_name);
14159
14160
100
    i.types[j].bitfield.instance = InstanceNone;
14161
14162
100
    if (operand_type_check (i.types[j], disp))
14163
77
      {
14164
77
        i.types[j].bitfield.baseindex = 1;
14165
77
        disp_exp = i.op[j].disps;
14166
77
      }
14167
14168
100
    if (evex && i.types[j].bitfield.baseindex)
14169
1
      {
14170
1
        unsigned int n = i.memshift;
14171
14172
1
        if (i.types[j].bitfield.byte)
14173
0
    n = 0;
14174
1
        else if (i.types[j].bitfield.word)
14175
0
    n = 1;
14176
1
        else if (i.types[j].bitfield.dword)
14177
0
    n = 2;
14178
1
        else if (i.types[j].bitfield.qword)
14179
0
    n = 3;
14180
1
        else if (i.types[j].bitfield.xmmword)
14181
0
    n = 4;
14182
1
        else if (i.types[j].bitfield.ymmword)
14183
0
    n = 5;
14184
1
        else if (i.types[j].bitfield.zmmword)
14185
0
    n = 6;
14186
14187
1
        if (i.memshift < 32 && n != i.memshift)
14188
0
    as_warn ("conflicting memory operand size specifiers");
14189
1
        i.memshift = n;
14190
1
      }
14191
14192
100
    if ((i.broadcast.type || i.broadcast.bytes)
14193
0
        && j == i.broadcast.operand)
14194
0
      continue;
14195
14196
100
    combined = operand_type_or (combined, i.types[j]);
14197
100
    combined.bitfield.class = ClassNone;
14198
100
  }
14199
14200
107
      switch ((i.broadcast.type ? i.broadcast.type : 1)
14201
107
        << (i.memshift < 32 ? i.memshift : 0))
14202
107
  {
14203
0
  case 64: combined.bitfield.zmmword = 1; break;
14204
0
  case 32: combined.bitfield.ymmword = 1; break;
14205
0
  case 16: combined.bitfield.xmmword = 1; break;
14206
0
  case  8: combined.bitfield.qword = 1; break;
14207
0
  case  4: combined.bitfield.dword = 1; break;
14208
107
  }
14209
14210
107
      if (pp.encoding == encoding_default)
14211
26
  {
14212
26
    if (flag_code == CODE_64BIT && combined.bitfield.qword)
14213
0
      i.rex |= REX_W;
14214
26
    else if ((flag_code == CODE_16BIT ? combined.bitfield.dword
14215
26
              : combined.bitfield.word)
14216
18
             && !add_prefix (DATA_PREFIX_OPCODE))
14217
0
      goto done;
14218
26
  }
14219
81
      else if (!i.tm.opcode_modifier.vexw)
14220
81
  {
14221
81
    if (flag_code == CODE_64BIT)
14222
5
      {
14223
5
        if (combined.bitfield.qword)
14224
3
          i.tm.opcode_modifier.vexw = VEXW1;
14225
2
        else if (combined.bitfield.dword)
14226
2
          i.tm.opcode_modifier.vexw = VEXW0;
14227
5
      }
14228
14229
81
    if (!i.tm.opcode_modifier.vexw)
14230
76
      i.tm.opcode_modifier.vexw = VEXWIG;
14231
81
  }
14232
14233
107
      if (vex || xop)
14234
78
  {
14235
78
    if (!i.tm.opcode_modifier.vex)
14236
78
      {
14237
78
        if (combined.bitfield.ymmword)
14238
0
          i.tm.opcode_modifier.vex = VEX256;
14239
78
        else if (combined.bitfield.xmmword)
14240
0
          i.tm.opcode_modifier.vex = VEX128;
14241
78
      }
14242
78
  }
14243
29
      else if (evex)
14244
3
  {
14245
3
    if (!i.tm.opcode_modifier.evex)
14246
0
      {
14247
        /* Do _not_ consider AVX512VL here.  */
14248
0
        if (combined.bitfield.zmmword)
14249
0
          i.tm.opcode_modifier.evex = EVEX512;
14250
0
        else if (combined.bitfield.ymmword)
14251
0
          i.tm.opcode_modifier.evex = EVEX256;
14252
0
        else if (combined.bitfield.xmmword)
14253
0
          i.tm.opcode_modifier.evex = EVEX128;
14254
0
      }
14255
14256
3
    if (i.memshift >= 32)
14257
3
      {
14258
3
        unsigned int n = 0;
14259
14260
3
        switch (i.tm.opcode_modifier.evex)
14261
3
    {
14262
0
    case EVEX512: n = 64; break;
14263
3
    case EVEX256: n = 32; break;
14264
0
    case EVEX128: n = 16; break;
14265
3
    }
14266
14267
3
        if (i.broadcast.type)
14268
0
    n /= i.broadcast.type;
14269
14270
3
        if (n > 0)
14271
18
    for (i.memshift = 0; !(n & 1); n >>= 1)
14272
15
      ++i.memshift;
14273
0
        else if (disp_exp != NULL && disp_exp->X_op == O_constant
14274
0
           && disp_exp->X_add_number != 0
14275
0
           && pp.disp_encoding != disp_encoding_32bit)
14276
0
    {
14277
0
      if (!quiet_warnings)
14278
0
        as_warn ("cannot determine memory operand size");
14279
0
      pp.disp_encoding = disp_encoding_32bit;
14280
0
    }
14281
3
      }
14282
3
  }
14283
14284
107
      if (i.memshift >= 32)
14285
104
  i.memshift = 0;
14286
3
      else if (!evex)
14287
0
  pp.encoding = encoding_error;
14288
14289
107
      if (i.disp_operands && !optimize_disp (&i.tm))
14290
0
  goto done;
14291
14292
      /* Establish size for immediate operands.  */
14293
117
      for (j = 0; j < i.imm_operands; ++j)
14294
10
  {
14295
10
    expressionS *expP = i.op[j].imms;
14296
14297
10
    gas_assert (operand_type_check (i.types[j], imm));
14298
10
    operand_type_set (&i.types[j], 0);
14299
14300
10
    if (i.imm_bits[j] > 32)
14301
0
      i.types[j].bitfield.imm64 = 1;
14302
10
    else if (i.imm_bits[j] > 16)
14303
0
      {
14304
0
        if (flag_code == CODE_64BIT && (i.flags[j] & Operand_Signed))
14305
0
    i.types[j].bitfield.imm32s = 1;
14306
0
        else
14307
0
    i.types[j].bitfield.imm32 = 1;
14308
0
      }
14309
10
    else if (i.imm_bits[j] > 8)
14310
0
      i.types[j].bitfield.imm16 = 1;
14311
10
    else if (i.imm_bits[j] > 0)
14312
0
      {
14313
0
        if (i.flags[j] & Operand_Signed)
14314
0
    i.types[j].bitfield.imm8s = 1;
14315
0
        else
14316
0
    i.types[j].bitfield.imm8 = 1;
14317
0
      }
14318
10
    else if (expP->X_op == O_constant)
14319
10
      {
14320
10
        i.types[j] = smallest_imm_type (expP->X_add_number);
14321
10
        i.types[j].bitfield.imm1 = 0;
14322
        /* Oddly enough imm_size() checks imm64 first, so the bit needs
14323
     zapping since smallest_imm_type() sets it unconditionally.  */
14324
10
        if (flag_code != CODE_64BIT)
14325
0
    {
14326
0
      i.types[j].bitfield.imm64 = 0;
14327
0
      i.types[j].bitfield.imm32s = 0;
14328
0
      i.types[j].bitfield.imm32 = 1;
14329
0
    }
14330
10
        else if (i.types[j].bitfield.imm32 || i.types[j].bitfield.imm32s)
14331
10
    i.types[j].bitfield.imm64 = 0;
14332
10
      }
14333
0
    else
14334
      /* Non-constant expressions are sized heuristically.  */
14335
0
      switch (flag_code)
14336
0
        {
14337
0
        case CODE_64BIT: i.types[j].bitfield.imm32s = 1; break;
14338
0
        case CODE_32BIT: i.types[j].bitfield.imm32 = 1; break;
14339
0
        case CODE_16BIT: i.types[j].bitfield.imm16 = 1; break;
14340
0
        }
14341
10
  }
14342
14343
217
      for (j = 0; j < i.operands; ++j)
14344
110
  i.tm.operand_types[j] = i.types[j];
14345
14346
107
      process_operands ();
14347
107
    }
14348
14349
  /* Don't set opcode until after processing operands, to avoid any
14350
     potential special casing there.  */
14351
974
  i.tm.base_opcode |= val;
14352
14353
974
  if (pp.encoding == encoding_error
14354
974
      || (pp.encoding != encoding_evex
14355
974
    ? i.broadcast.type || i.broadcast.bytes
14356
933
      || i.rounding.type != rc_none
14357
933
      || i.mask.reg
14358
974
    : (i.mem_operands && i.rounding.type != rc_none)
14359
41
      || ((i.broadcast.type || i.broadcast.bytes)
14360
0
    && !(i.flags[i.broadcast.operand] & Operand_Mem))))
14361
0
    {
14362
0
      as_bad (_("conflicting .insn operands"));
14363
0
      goto done;
14364
0
    }
14365
14366
974
  if (vex || xop)
14367
317
    {
14368
317
      if (is_apx_evex_encoding ())
14369
0
  {
14370
0
    as_bad (_("APX functionality cannot be used with %s encodings"),
14371
0
      vex ? "VEX" : "XOP");
14372
0
    goto done;
14373
0
  }
14374
14375
317
      if (!i.tm.opcode_modifier.vex)
14376
317
  i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
14377
14378
317
      build_vex_prefix (NULL);
14379
317
      i.rex &= REX_OPCODE;
14380
317
    }
14381
657
  else if (evex)
14382
41
    {
14383
41
      if (!i.tm.opcode_modifier.evex)
14384
38
  i.tm.opcode_modifier.evex = EVEXLIG;
14385
14386
      /* To keep earlier .insn uses working as far as possible, take the
14387
   legacy path when opcode space is 4 bits wide (impossible to encode in
14388
   extended EVEX), and when no "extended" syntax elements are used.  */
14389
41
      if ((!is_apx_evex_encoding () || i.insn_opcode_space > 7)
14390
38
    && evex == evex_basic
14391
38
    && !i.tm.opcode_modifier.operandconstraint)
14392
38
  build_evex_prefix ();
14393
3
      else if (i.insn_opcode_space > 7)
14394
0
  {
14395
0
    as_bad (_("opcode space cannot be larger than 7"));
14396
0
    goto done;
14397
0
  }
14398
3
      else if (evex == evex_nd && (i.broadcast.type || i.broadcast.bytes))
14399
0
  {
14400
0
    as_bad (_("ND and broadcast cannot be used at the same time"));
14401
0
    goto done;
14402
0
  }
14403
3
      else if (pp.has_nf && i.mask.reg)
14404
0
  {
14405
0
    as_bad (_("{nf} and masking cannot be used at the same time"));
14406
0
    goto done;
14407
0
  }
14408
3
      else if (i.tm.opcode_modifier.operandconstraint == SCC
14409
0
         && (pp.has_nf || i.mask.reg))
14410
0
  {
14411
0
    as_bad (_("SCC cannot be used at the same time {nf} / masking"));
14412
0
    goto done;
14413
0
  }
14414
3
      else if (!build_apx_evex_prefix (evex == evex_nd))
14415
0
  goto done;
14416
41
      i.rex &= REX_OPCODE;
14417
41
    }
14418
616
  else
14419
616
    establish_rex ();
14420
14421
974
  last_insn = &seg_info(now_seg)->tc_segment_info_data.last_insn;
14422
974
  output_insn (last_insn);
14423
974
  last_insn->kind = last_insn_directive;
14424
974
  last_insn->name = ".insn directive";
14425
974
  last_insn->file = as_where (&last_insn->line);
14426
14427
974
#ifdef OBJ_ELF
14428
  /* PS: SCFI is enabled only for System V AMD64 ABI.  The ABI check has been
14429
     performed in i386_target_format.  */
14430
974
  if (flag_synth_cfi)
14431
0
    as_bad (_("SCFI: hand-crafting instructions not supported"));
14432
974
#endif
14433
14434
2.80k
 done:
14435
2.80k
  *saved_ilp = saved_char;
14436
2.80k
  input_line_pointer = line;
14437
14438
2.80k
  demand_empty_rest_of_line ();
14439
14440
  /* Make sure dot_insn() won't yield "true" anymore.  */
14441
2.80k
  i.tm.mnem_off = 0;
14442
14443
2.80k
  current_templates.start = NULL;
14444
2.80k
  memset (&pp, 0, sizeof (pp));
14445
2.80k
}
14446
14447
#ifdef TE_PE
14448
static void
14449
pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
14450
{
14451
  expressionS exp;
14452
14453
  do
14454
    {
14455
      expression (&exp);
14456
      if (exp.X_op == O_symbol)
14457
  exp.X_op = O_secrel;
14458
14459
      emit_expr (&exp, 4);
14460
    }
14461
  while (*input_line_pointer++ == ',');
14462
14463
  input_line_pointer--;
14464
  demand_empty_rest_of_line ();
14465
}
14466
14467
static void
14468
pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
14469
{
14470
  expressionS exp;
14471
14472
  do
14473
    {
14474
      expression (&exp);
14475
      if (exp.X_op == O_symbol)
14476
  exp.X_op = O_secidx;
14477
14478
      emit_expr (&exp, 2);
14479
    }
14480
  while (*input_line_pointer++ == ',');
14481
14482
  input_line_pointer--;
14483
  demand_empty_rest_of_line ();
14484
}
14485
#endif
14486
14487
/* Handle Rounding Control / SAE specifiers.  */
14488
14489
static char *
14490
RC_SAE_specifier (const char *pstr)
14491
33
{
14492
33
  unsigned int j;
14493
14494
173
  for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
14495
145
    {
14496
145
      if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
14497
5
  {
14498
5
    if (i.rounding.type != rc_none)
14499
0
      {
14500
0
        as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
14501
0
        return NULL;
14502
0
      }
14503
14504
5
    switch (pp.encoding)
14505
5
      {
14506
5
      case encoding_default:
14507
5
      case encoding_egpr:
14508
5
        pp.encoding = encoding_evex512;
14509
5
        break;
14510
0
      case encoding_evex:
14511
0
      case encoding_evex512:
14512
0
        break;
14513
0
      default:
14514
0
        return NULL;
14515
5
      }
14516
14517
5
    i.rounding.type = RC_NamesTable[j].type;
14518
14519
5
    return (char *)(pstr + RC_NamesTable[j].len);
14520
5
  }
14521
145
    }
14522
14523
28
  return NULL;
14524
33
}
14525
14526
/* Handle Vector operations.  */
14527
14528
static char *
14529
check_VecOperations (char *op_string)
14530
41
{
14531
41
  const reg_entry *mask;
14532
41
  const char *saved;
14533
41
  char *end_op;
14534
14535
46
  while (*op_string)
14536
41
    {
14537
41
      saved = op_string;
14538
41
      if (*op_string == '{')
14539
41
  {
14540
41
    op_string++;
14541
41
    if (is_whitespace (*op_string))
14542
8
      op_string++;
14543
14544
    /* Check broadcasts.  */
14545
41
    if (startswith (op_string, "1to"))
14546
1
      {
14547
1
        unsigned int bcst_type;
14548
14549
1
        if (i.broadcast.type)
14550
0
    goto duplicated_vec_op;
14551
14552
1
        op_string += 3;
14553
1
        if (*op_string == '8')
14554
0
    bcst_type = 8;
14555
1
        else if (*op_string == '4')
14556
0
    bcst_type = 4;
14557
1
        else if (*op_string == '2')
14558
0
    bcst_type = 2;
14559
1
        else if (*op_string == '1'
14560
0
           && *(op_string+1) == '6')
14561
0
    {
14562
0
      bcst_type = 16;
14563
0
      op_string++;
14564
0
    }
14565
1
        else if (*op_string == '3'
14566
0
           && *(op_string+1) == '2')
14567
0
    {
14568
0
      bcst_type = 32;
14569
0
      op_string++;
14570
0
    }
14571
1
        else
14572
1
    {
14573
1
      as_bad (_("Unsupported broadcast: `%s'"), saved);
14574
1
      return NULL;
14575
1
    }
14576
0
        op_string++;
14577
14578
0
        switch (pp.encoding)
14579
0
    {
14580
0
    case encoding_default:
14581
0
    case encoding_egpr:
14582
0
      pp.encoding = encoding_evex;
14583
0
      break;
14584
0
    case encoding_evex:
14585
0
    case encoding_evex512:
14586
0
      break;
14587
0
    default:
14588
0
      goto unknown_vec_op;
14589
0
    }
14590
14591
0
        i.broadcast.type = bcst_type;
14592
0
        i.broadcast.operand = this_operand;
14593
14594
        /* For .insn a data size specifier may be appended.  */
14595
0
        if (dot_insn () && *op_string == ':')
14596
0
    goto dot_insn_modifier;
14597
0
      }
14598
    /* Check .insn special cases.  */
14599
40
    else if (dot_insn () && *op_string == ':')
14600
0
      {
14601
0
      dot_insn_modifier:
14602
0
        switch (op_string[1])
14603
0
    {
14604
0
      unsigned long n;
14605
14606
0
    case 'd':
14607
0
      if (i.memshift < 32)
14608
0
        goto duplicated_vec_op;
14609
14610
0
      n = strtoul (op_string + 2, &end_op, 0);
14611
0
      if (n)
14612
0
        for (i.memshift = 0; !(n & 1); n >>= 1)
14613
0
          ++i.memshift;
14614
0
      if (i.memshift < 32 && n == 1)
14615
0
        op_string = end_op;
14616
0
      break;
14617
14618
0
    case 's': case 'u':
14619
      /* This isn't really a "vector" operation, but a sign/size
14620
         specifier for immediate operands of .insn.  Note that AT&T
14621
         syntax handles the same in i386_immediate().  */
14622
0
      if (!intel_syntax)
14623
0
        break;
14624
14625
0
      if (i.imm_bits[this_operand])
14626
0
        goto duplicated_vec_op;
14627
14628
0
      n = strtoul (op_string + 2, &end_op, 0);
14629
0
      if (n && n <= (flag_code == CODE_64BIT ? 64 : 32))
14630
0
        {
14631
0
          i.imm_bits[this_operand] = n;
14632
0
          if (op_string[1] == 's')
14633
0
      i.flags[this_operand] |= Operand_Signed;
14634
0
          op_string = end_op;
14635
0
        }
14636
0
      break;
14637
0
    }
14638
0
      }
14639
    /* Check masking operation.  */
14640
40
    else if ((mask = parse_register (op_string, &end_op)) != NULL)
14641
4
      {
14642
4
        if (mask == &bad_reg)
14643
0
    return NULL;
14644
14645
        /* k0 can't be used for write mask.  */
14646
4
        if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
14647
4
    {
14648
4
      as_bad (_("`%s%s' can't be used for write mask"),
14649
4
        register_prefix, mask->reg_name);
14650
4
      return NULL;
14651
4
    }
14652
14653
0
        if (!i.mask.reg)
14654
0
    {
14655
0
      i.mask.reg = mask;
14656
0
      i.mask.operand = this_operand;
14657
0
    }
14658
0
        else if (i.mask.reg->reg_num)
14659
0
    goto duplicated_vec_op;
14660
0
        else
14661
0
    {
14662
0
      i.mask.reg = mask;
14663
14664
      /* Only "{z}" is allowed here.  No need to check
14665
         zeroing mask explicitly.  */
14666
0
      if (i.mask.operand != (unsigned int) this_operand)
14667
0
        {
14668
0
          as_bad (_("invalid write mask `%s'"), saved);
14669
0
          return NULL;
14670
0
        }
14671
0
    }
14672
14673
0
        op_string = end_op;
14674
0
      }
14675
    /* Check zeroing-flag for masking operation.  */
14676
36
    else if (*op_string == 'z')
14677
12
      {
14678
12
        if (!i.mask.reg)
14679
12
    {
14680
12
      i.mask.reg = reg_k0;
14681
12
      i.mask.zeroing = 1;
14682
12
      i.mask.operand = this_operand;
14683
12
    }
14684
0
        else
14685
0
    {
14686
0
      if (i.mask.zeroing)
14687
0
        {
14688
0
        duplicated_vec_op:
14689
0
          as_bad (_("duplicated `%s'"), saved);
14690
0
          return NULL;
14691
0
        }
14692
14693
0
      i.mask.zeroing = 1;
14694
14695
      /* Only "{%k}" is allowed here.  No need to check mask
14696
         register explicitly.  */
14697
0
      if (i.mask.operand != (unsigned int) this_operand)
14698
0
        {
14699
0
          as_bad (_("invalid zeroing-masking `%s'"),
14700
0
            saved);
14701
0
          return NULL;
14702
0
        }
14703
0
    }
14704
14705
12
        op_string++;
14706
12
      }
14707
24
    else if (intel_syntax
14708
23
       && (op_string = RC_SAE_specifier (op_string)) != NULL)
14709
5
      i.rounding.modifier = true;
14710
19
    else
14711
19
      goto unknown_vec_op;
14712
14713
17
    if (is_whitespace (*op_string))
14714
0
      op_string++;
14715
17
    if (*op_string != '}')
14716
12
      {
14717
12
        as_bad (_("missing `}' in `%s'"), saved);
14718
12
        return NULL;
14719
12
      }
14720
5
    op_string++;
14721
14722
5
    if (is_whitespace (*op_string))
14723
0
      ++op_string;
14724
14725
5
    continue;
14726
17
  }
14727
19
    unknown_vec_op:
14728
      /* We don't know this one.  */
14729
19
      as_bad (_("unknown vector operation: `%s'"), saved);
14730
19
      return NULL;
14731
41
    }
14732
14733
5
  if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
14734
0
    {
14735
0
      as_bad (_("zeroing-masking only allowed with write mask"));
14736
0
      return NULL;
14737
0
    }
14738
14739
5
  return op_string;
14740
5
}
14741
14742
static int
14743
i386_immediate (char *imm_start)
14744
2.91k
{
14745
2.91k
  char *save_input_line_pointer;
14746
2.91k
  char *gotfree_input_line;
14747
2.91k
  segT exp_seg = 0;
14748
2.91k
  expressionS *exp;
14749
2.91k
  i386_operand_type types;
14750
14751
2.91k
  operand_type_set (&types, ~0);
14752
14753
2.91k
  if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
14754
0
    {
14755
0
      as_bad (_("at most %d immediate operands are allowed"),
14756
0
        MAX_IMMEDIATE_OPERANDS);
14757
0
      return 0;
14758
0
    }
14759
14760
2.91k
  exp = &im_expressions[i.imm_operands++];
14761
2.91k
  i.op[this_operand].imms = exp;
14762
14763
2.91k
  if (is_whitespace (*imm_start))
14764
2
    ++imm_start;
14765
14766
2.91k
  save_input_line_pointer = input_line_pointer;
14767
2.91k
  input_line_pointer = imm_start;
14768
14769
2.91k
  gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
14770
2.91k
  if (gotfree_input_line)
14771
0
    input_line_pointer = gotfree_input_line;
14772
14773
2.91k
  expr_mode = expr_operator_none;
14774
2.91k
  exp_seg = expression (exp);
14775
14776
  /* For .insn immediates there may be a size specifier.  */
14777
2.91k
  if (dot_insn () && *input_line_pointer == '{' && input_line_pointer[1] == ':'
14778
0
      && (input_line_pointer[2] == 's' || input_line_pointer[2] == 'u'))
14779
0
    {
14780
0
      char *e;
14781
0
      unsigned long n = strtoul (input_line_pointer + 3, &e, 0);
14782
14783
0
      if (*e == '}' && n && n <= (flag_code == CODE_64BIT ? 64 : 32))
14784
0
  {
14785
0
    i.imm_bits[this_operand] = n;
14786
0
    if (input_line_pointer[2] == 's')
14787
0
      i.flags[this_operand] |= Operand_Signed;
14788
0
    input_line_pointer = e + 1;
14789
0
  }
14790
0
    }
14791
14792
2.91k
  SKIP_WHITESPACE ();
14793
2.91k
  if (*input_line_pointer)
14794
1.53k
    as_bad (_("junk `%s' after expression"), input_line_pointer);
14795
14796
2.91k
  input_line_pointer = save_input_line_pointer;
14797
2.91k
  if (gotfree_input_line)
14798
0
    {
14799
0
      free (gotfree_input_line);
14800
14801
0
      if (exp->X_op == O_constant)
14802
0
  exp->X_op = O_illegal;
14803
0
    }
14804
14805
2.91k
  if (exp_seg == reg_section)
14806
0
    {
14807
0
      as_bad (_("illegal immediate register operand %s"), imm_start);
14808
0
      return 0;
14809
0
    }
14810
14811
2.91k
  return i386_finalize_immediate (exp_seg, exp, types, imm_start);
14812
2.91k
}
14813
14814
static int
14815
i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
14816
       i386_operand_type types, const char *imm_start)
14817
4.30k
{
14818
4.30k
  if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
14819
6
    {
14820
6
      if (imm_start)
14821
6
  as_bad (_("missing or invalid immediate expression `%s'"),
14822
6
    imm_start);
14823
6
      return 0;
14824
6
    }
14825
4.29k
  else if (exp->X_op == O_constant)
14826
2.69k
    {
14827
      /* Size it properly later.  */
14828
2.69k
      i.types[this_operand].bitfield.imm64 = 1;
14829
14830
      /* If not 64bit, sign/zero extend val, to account for wraparound
14831
   when !BFD64.  */
14832
2.69k
      if (expr_mode == expr_operator_present
14833
737
    && flag_code != CODE_64BIT && !object_64bit)
14834
0
  exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
14835
2.69k
    }
14836
#ifdef OBJ_AOUT
14837
  else if (exp_seg != absolute_section
14838
     && exp_seg != text_section
14839
     && exp_seg != data_section
14840
     && exp_seg != bss_section
14841
     && exp_seg != undefined_section
14842
     && !bfd_is_com_section (exp_seg))
14843
    {
14844
      as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
14845
      return 0;
14846
    }
14847
#endif
14848
1.59k
  else
14849
1.59k
    {
14850
      /* This is an address.  The size of the address will be
14851
   determined later, depending on destination register,
14852
   suffix, or the default for the section.  */
14853
1.59k
      i.types[this_operand].bitfield.imm8 = 1;
14854
1.59k
      i.types[this_operand].bitfield.imm16 = 1;
14855
1.59k
      i.types[this_operand].bitfield.imm32 = 1;
14856
1.59k
      i.types[this_operand].bitfield.imm32s = 1;
14857
1.59k
      i.types[this_operand].bitfield.imm64 = 1;
14858
1.59k
      i.types[this_operand] = operand_type_and (i.types[this_operand],
14859
1.59k
            types);
14860
1.59k
    }
14861
14862
4.29k
  return 1;
14863
4.30k
}
14864
14865
static char *
14866
i386_scale (char *scale)
14867
1
{
14868
1
  offsetT val;
14869
1
  char *save = input_line_pointer;
14870
14871
1
  input_line_pointer = scale;
14872
1
  val = get_absolute_expression ();
14873
14874
1
  switch (val)
14875
1
    {
14876
0
    case 1:
14877
0
      i.log2_scale_factor = 0;
14878
0
      break;
14879
0
    case 2:
14880
0
      i.log2_scale_factor = 1;
14881
0
      break;
14882
0
    case 4:
14883
0
      i.log2_scale_factor = 2;
14884
0
      break;
14885
0
    case 8:
14886
0
      i.log2_scale_factor = 3;
14887
0
      break;
14888
1
    default:
14889
1
      {
14890
1
  char sep = *input_line_pointer;
14891
14892
1
  *input_line_pointer = '\0';
14893
1
  as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
14894
1
    scale);
14895
1
  *input_line_pointer = sep;
14896
1
  input_line_pointer = save;
14897
1
  return NULL;
14898
0
      }
14899
1
    }
14900
0
  if (i.log2_scale_factor != 0 && i.index_reg == 0)
14901
0
    {
14902
0
      as_warn (_("scale factor of %d without an index register"),
14903
0
         1 << i.log2_scale_factor);
14904
0
      i.log2_scale_factor = 0;
14905
0
    }
14906
0
  scale = input_line_pointer;
14907
0
  input_line_pointer = save;
14908
0
  return scale;
14909
1
}
14910
14911
static int
14912
i386_displacement (char *disp_start, char *disp_end)
14913
10.6k
{
14914
10.6k
  expressionS *exp;
14915
10.6k
  segT exp_seg = 0;
14916
10.6k
  char *save_input_line_pointer;
14917
10.6k
  char *gotfree_input_line;
14918
10.6k
  int override;
14919
10.6k
  i386_operand_type bigdisp, types = anydisp;
14920
10.6k
  int ret;
14921
14922
10.6k
  if (i.disp_operands == MAX_MEMORY_OPERANDS)
14923
0
    {
14924
0
      as_bad (_("at most %d displacement operands are allowed"),
14925
0
        MAX_MEMORY_OPERANDS);
14926
0
      return 0;
14927
0
    }
14928
14929
10.6k
  operand_type_set (&bigdisp, 0);
14930
10.6k
  if (i.jumpabsolute
14931
10.6k
      || i.types[this_operand].bitfield.baseindex
14932
10.6k
      || (current_templates.start->opcode_modifier.jump != JUMP
14933
10.6k
    && current_templates.start->opcode_modifier.jump != JUMP_DWORD))
14934
10.5k
    {
14935
10.5k
      i386_addressing_mode ();
14936
10.5k
      override = (i.prefix[ADDR_PREFIX] != 0);
14937
10.5k
      if (flag_code == CODE_64BIT)
14938
10.1k
  {
14939
10.1k
    bigdisp.bitfield.disp32 = 1;
14940
10.1k
    if (!override)
14941
10.1k
      bigdisp.bitfield.disp64 = 1;
14942
10.1k
  }
14943
435
      else if ((flag_code == CODE_16BIT) ^ override)
14944
407
    bigdisp.bitfield.disp16 = 1;
14945
28
      else
14946
28
    bigdisp.bitfield.disp32 = 1;
14947
10.5k
    }
14948
139
  else
14949
139
    {
14950
      /* For PC-relative branches, the width of the displacement may be
14951
   dependent upon data size, but is never dependent upon address size.
14952
   Also make sure to not unintentionally match against a non-PC-relative
14953
   branch template.  */
14954
139
      const insn_template *t = current_templates.start;
14955
139
      bool has_intel64 = false;
14956
14957
400
      while (++t < current_templates.end)
14958
392
  {
14959
392
    if (t->opcode_modifier.jump
14960
392
        != current_templates.start->opcode_modifier.jump)
14961
131
      break;
14962
261
    if ((t->opcode_modifier.isa64 >= INTEL64))
14963
132
      has_intel64 = true;
14964
261
  }
14965
139
      current_templates.end = t;
14966
14967
139
      override = (i.prefix[DATA_PREFIX] != 0);
14968
139
      if (flag_code == CODE_64BIT)
14969
133
  {
14970
133
    if ((override || i.suffix == WORD_MNEM_SUFFIX)
14971
65
        && (!intel64 || !has_intel64))
14972
1
      bigdisp.bitfield.disp16 = 1;
14973
132
    else
14974
132
      bigdisp.bitfield.disp32 = 1;
14975
133
  }
14976
6
      else
14977
6
  {
14978
6
    if (!override)
14979
4
      override = (i.suffix == (flag_code != CODE_16BIT
14980
4
             ? WORD_MNEM_SUFFIX
14981
4
             : LONG_MNEM_SUFFIX));
14982
6
    bigdisp.bitfield.disp32 = 1;
14983
6
    if ((flag_code == CODE_16BIT) ^ override)
14984
4
      {
14985
4
        bigdisp.bitfield.disp32 = 0;
14986
4
        bigdisp.bitfield.disp16 = 1;
14987
4
      }
14988
6
  }
14989
139
    }
14990
10.6k
  i.types[this_operand] = operand_type_or (i.types[this_operand],
14991
10.6k
             bigdisp);
14992
14993
10.6k
  exp = &disp_expressions[i.disp_operands];
14994
10.6k
  i.op[this_operand].disps = exp;
14995
10.6k
  i.disp_operands++;
14996
10.6k
  save_input_line_pointer = input_line_pointer;
14997
10.6k
  input_line_pointer = disp_start;
14998
10.6k
  END_STRING_AND_SAVE (disp_end);
14999
15000
10.6k
#ifndef GCC_ASM_O_HACK
15001
10.6k
#define GCC_ASM_O_HACK 0
15002
10.6k
#endif
15003
#if GCC_ASM_O_HACK
15004
  END_STRING_AND_SAVE (disp_end + 1);
15005
  if (i.types[this_operand].bitfield.baseIndex
15006
      && displacement_string_end[-1] == '+')
15007
    {
15008
      /* This hack is to avoid a warning when using the "o"
15009
   constraint within gcc asm statements.
15010
   For instance:
15011
15012
   #define _set_tssldt_desc(n,addr,limit,type) \
15013
   __asm__ __volatile__ ( \
15014
   "movw %w2,%0\n\t" \
15015
   "movw %w1,2+%0\n\t" \
15016
   "rorl $16,%1\n\t" \
15017
   "movb %b1,4+%0\n\t" \
15018
   "movb %4,5+%0\n\t" \
15019
   "movb $0,6+%0\n\t" \
15020
   "movb %h1,7+%0\n\t" \
15021
   "rorl $16,%1" \
15022
   : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
15023
15024
   This works great except that the output assembler ends
15025
   up looking a bit weird if it turns out that there is
15026
   no offset.  You end up producing code that looks like:
15027
15028
   #APP
15029
   movw $235,(%eax)
15030
   movw %dx,2+(%eax)
15031
   rorl $16,%edx
15032
   movb %dl,4+(%eax)
15033
   movb $137,5+(%eax)
15034
   movb $0,6+(%eax)
15035
   movb %dh,7+(%eax)
15036
   rorl $16,%edx
15037
   #NO_APP
15038
15039
   So here we provide the missing zero.  */
15040
15041
      *displacement_string_end = '0';
15042
    }
15043
#endif
15044
10.6k
  gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
15045
10.6k
  if (gotfree_input_line)
15046
8.09k
    input_line_pointer = gotfree_input_line;
15047
15048
10.6k
  expr_mode = expr_operator_none;
15049
10.6k
  exp_seg = expression (exp);
15050
15051
10.6k
  SKIP_WHITESPACE ();
15052
10.6k
  if (*input_line_pointer)
15053
9.12k
    as_bad (_("junk `%s' after expression"), input_line_pointer);
15054
#if GCC_ASM_O_HACK
15055
  RESTORE_END_STRING (disp_end + 1);
15056
#endif
15057
10.6k
  input_line_pointer = save_input_line_pointer;
15058
10.6k
  if (gotfree_input_line)
15059
8.09k
    {
15060
8.09k
      free (gotfree_input_line);
15061
15062
8.09k
      if (exp->X_op == O_constant || exp->X_op == O_register)
15063
0
  exp->X_op = O_illegal;
15064
8.09k
    }
15065
15066
10.6k
  ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
15067
15068
10.6k
  RESTORE_END_STRING (disp_end);
15069
15070
10.6k
  return ret;
15071
10.6k
}
15072
15073
static int
15074
i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
15075
          i386_operand_type types, const char *disp_start)
15076
12.9k
{
15077
12.9k
  int ret = 1;
15078
15079
  /* We do this to make sure that the section symbol is in
15080
     the symbol table.  We will ultimately change the relocation
15081
     to be relative to the beginning of the section.  */
15082
12.9k
  if (i.reloc[this_operand] == BFD_RELOC_32_GOTOFF
15083
12.9k
      || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
15084
4.90k
      || i.reloc[this_operand] == BFD_RELOC_64_GOTOFF)
15085
8.09k
    {
15086
8.09k
      if (exp->X_op != O_symbol
15087
4
    && exp->X_op != O_add
15088
4
    && exp->X_op != O_subtract)
15089
0
  goto inv_disp;
15090
15091
8.09k
      if (S_IS_LOCAL (exp->X_add_symbol)
15092
4
    && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
15093
4
    && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
15094
2
  section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
15095
15096
8.09k
      if (exp->X_op != O_symbol)
15097
4
  {
15098
4
    if (S_IS_LOCAL (exp->X_op_symbol)
15099
2
        && S_GET_SEGMENT (exp->X_op_symbol) != undefined_section
15100
2
        && S_GET_SEGMENT (exp->X_op_symbol) != expr_section)
15101
0
      section_symbol (S_GET_SEGMENT (exp->X_op_symbol));
15102
15103
4
    exp->X_add_symbol = make_expr_symbol (exp);
15104
4
  }
15105
15106
8.09k
      exp->X_op = O_subtract;
15107
8.09k
      exp->X_op_symbol = GOT_symbol;
15108
8.09k
      if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
15109
8.09k
  i.reloc[this_operand] = BFD_RELOC_32_PCREL;
15110
0
      else if (i.reloc[this_operand] == BFD_RELOC_64_GOTOFF)
15111
0
  i.reloc[this_operand] = BFD_RELOC_64;
15112
0
      else
15113
0
  i.reloc[this_operand] = BFD_RELOC_32;
15114
8.09k
    }
15115
15116
4.90k
  else if (exp->X_op == O_absent
15117
4.90k
     || exp->X_op == O_illegal
15118
4.90k
     || exp->X_op == O_big)
15119
1
    {
15120
1
    inv_disp:
15121
1
      as_bad (_("missing or invalid displacement expression `%s'"),
15122
1
        disp_start);
15123
1
      ret = 0;
15124
1
    }
15125
15126
4.90k
  else if (exp->X_op == O_constant)
15127
1.46k
    {
15128
      /* Sizing gets taken care of by optimize_disp().
15129
15130
   If not 64bit, sign/zero extend val, to account for wraparound
15131
   when !BFD64.  */
15132
1.46k
      if (expr_mode == expr_operator_present
15133
674
    && flag_code != CODE_64BIT && !object_64bit)
15134
0
  exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
15135
1.46k
    }
15136
15137
#ifdef OBJ_AOUT
15138
  else if (exp_seg != absolute_section
15139
     && exp_seg != text_section
15140
     && exp_seg != data_section
15141
     && exp_seg != bss_section
15142
     && exp_seg != undefined_section
15143
     && !bfd_is_com_section (exp_seg))
15144
    {
15145
      as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
15146
      ret = 0;
15147
    }
15148
#endif
15149
15150
3.43k
  else if (current_templates.start->opcode_modifier.jump == JUMP_BYTE)
15151
0
    i.types[this_operand].bitfield.disp8 = 1;
15152
15153
  /* Check if this is a displacement only operand.  */
15154
12.9k
  if (!i.types[this_operand].bitfield.baseindex)
15155
12.9k
    i.types[this_operand] =
15156
12.9k
      operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
15157
12.9k
           operand_type_and (i.types[this_operand], types));
15158
15159
12.9k
  return ret;
15160
12.9k
}
15161
15162
/* Return the active addressing mode, taking address override and
15163
   registers forming the address into consideration.  Update the
15164
   address override prefix if necessary.  */
15165
15166
static enum flag_code
15167
i386_addressing_mode (void)
15168
25.8k
{
15169
25.8k
  enum flag_code addr_mode;
15170
15171
25.8k
  if (i.prefix[ADDR_PREFIX])
15172
3
    addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
15173
25.8k
  else if (flag_code == CODE_16BIT
15174
3.82k
     && is_cpu (current_templates.start, CpuMPX)
15175
     /* Avoid replacing the "16-bit addressing not allowed" diagnostic
15176
        from md_assemble() by "is not a valid base/index expression"
15177
        when there is a base and/or index.  */
15178
0
     && !i.types[this_operand].bitfield.baseindex)
15179
0
    {
15180
      /* MPX insn memory operands with neither base nor index must be forced
15181
   to use 32-bit addressing in 16-bit mode.  */
15182
0
      addr_mode = CODE_32BIT;
15183
0
      i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
15184
0
      ++i.prefixes;
15185
0
      gas_assert (!i.types[this_operand].bitfield.disp16);
15186
0
      gas_assert (!i.types[this_operand].bitfield.disp32);
15187
0
    }
15188
25.8k
  else
15189
25.8k
    {
15190
25.8k
      addr_mode = flag_code;
15191
15192
25.8k
#if INFER_ADDR_PREFIX
15193
25.8k
      if (i.mem_operands == 0)
15194
25.5k
  {
15195
    /* Infer address prefix from the first memory operand.  */
15196
25.5k
    const reg_entry *addr_reg = i.base_reg;
15197
15198
25.5k
    if (addr_reg == NULL)
15199
25.5k
      addr_reg = i.index_reg;
15200
15201
25.5k
    if (addr_reg)
15202
8
      {
15203
8
        if (addr_reg->reg_type.bitfield.dword)
15204
5
    addr_mode = CODE_32BIT;
15205
3
        else if (flag_code != CODE_64BIT
15206
2
           && addr_reg->reg_type.bitfield.word)
15207
0
    addr_mode = CODE_16BIT;
15208
15209
8
        if (addr_mode != flag_code)
15210
5
    {
15211
5
      i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
15212
5
      i.prefixes += 1;
15213
      /* Change the size of any displacement too.  At most one
15214
         of Disp16 or Disp32 is set.
15215
         FIXME.  There doesn't seem to be any real need for
15216
         separate Disp16 and Disp32 flags.  The same goes for
15217
         Imm16 and Imm32.  Removing them would probably clean
15218
         up the code quite a lot.  */
15219
5
      if (flag_code != CODE_64BIT
15220
1
          && (i.types[this_operand].bitfield.disp16
15221
1
        || i.types[this_operand].bitfield.disp32))
15222
0
        {
15223
0
          static const i386_operand_type disp16_32 = {
15224
0
      .bitfield = { .disp16 = 1, .disp32 = 1 }
15225
0
          };
15226
15227
0
          i.types[this_operand]
15228
0
      = operand_type_xor (i.types[this_operand], disp16_32);
15229
0
        }
15230
5
    }
15231
8
      }
15232
25.5k
  }
15233
25.8k
#endif
15234
25.8k
    }
15235
15236
25.8k
  return addr_mode;
15237
25.8k
}
15238
15239
/* Make sure the memory operand we've been dealt is valid.
15240
   Return 1 on success, 0 on a failure.  */
15241
15242
static int
15243
i386_index_check (const char *operand_string)
15244
12.9k
{
15245
12.9k
  const char *kind = "base/index";
15246
12.9k
  enum flag_code addr_mode = i386_addressing_mode ();
15247
12.9k
  const insn_template *t = current_templates.end - 1;
15248
15249
12.9k
  if (t->opcode_modifier.isstring)
15250
55
    {
15251
      /* Memory operands of string insns are special in that they only allow
15252
   a single register (rDI or rSI) as their memory address.  */
15253
55
      const reg_entry *expected_reg;
15254
55
      static const char di_si[][2][4] =
15255
55
  {
15256
55
    { "esi", "edi" },
15257
55
    { "si", "di" },
15258
55
    { "rsi", "rdi" }
15259
55
  };
15260
      /* For a few other insns with fixed register addressing we (ab)use the
15261
   IsString attribute as well.  */
15262
55
      static const char loregs[][4][4] =
15263
55
  {
15264
55
    { "eax", "ecx", "edx", "ebx" },
15265
55
    {  "ax",  "cx",  "dx",  "bx" },
15266
55
    { "rax", "rcx", "rdx", "rbx" }
15267
55
  };
15268
15269
55
      kind = "string address";
15270
15271
55
      if (t->opcode_modifier.prefixok == PrefixRep)
15272
49
  {
15273
49
    int es_op = t->opcode_modifier.isstring - IS_STRING_ES_OP0;
15274
49
    int op = 0;
15275
15276
49
    if (!t->operand_types[0].bitfield.baseindex
15277
17
        || ((!i.mem_operands != !intel_syntax)
15278
3
      && t->operand_types[1].bitfield.baseindex))
15279
32
      op = 1;
15280
49
    expected_reg = str_hash_find (reg_hash,
15281
49
          di_si[addr_mode][op == es_op]);
15282
49
  }
15283
6
      else
15284
6
  {
15285
6
    unsigned int op = t->operand_types[0].bitfield.baseindex ? 0 : 1;
15286
15287
6
    if (!t->operand_types[op].bitfield.instance)
15288
0
      return 1; /* Operand mismatch will be detected elsewhere.  */
15289
6
    expected_reg
15290
6
      = str_hash_find (reg_hash,
15291
6
           loregs[addr_mode][t->operand_types[op]
15292
6
                 .bitfield.instance - 1]);
15293
6
  }
15294
15295
55
      if (i.base_reg != expected_reg
15296
0
    || i.index_reg
15297
0
    || operand_type_check (i.types[this_operand], disp))
15298
55
  {
15299
    /* The second memory operand must have the same size as
15300
       the first one.  */
15301
55
    if (i.mem_operands
15302
0
        && i.base_reg
15303
0
        && !((addr_mode == CODE_64BIT
15304
0
        && i.base_reg->reg_type.bitfield.qword)
15305
0
       || (addr_mode == CODE_32BIT
15306
0
           ? i.base_reg->reg_type.bitfield.dword
15307
0
           : i.base_reg->reg_type.bitfield.word)))
15308
0
      goto bad_address;
15309
15310
55
    as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
15311
55
       operand_string,
15312
55
       intel_syntax ? '[' : '(',
15313
55
       register_prefix,
15314
55
       expected_reg->reg_name,
15315
55
       intel_syntax ? ']' : ')');
15316
55
    return 1;
15317
55
  }
15318
0
      else
15319
0
  return 1;
15320
15321
3
    bad_address:
15322
3
      as_bad (_("`%s' is not a valid %s expression"),
15323
3
        operand_string, kind);
15324
3
      return 0;
15325
55
    }
15326
12.9k
  else
15327
12.9k
    {
15328
12.9k
      t = current_templates.start;
15329
15330
12.9k
      if (addr_mode != CODE_16BIT)
15331
10.9k
  {
15332
    /* 32-bit/64-bit checks.  */
15333
10.9k
    if (pp.disp_encoding == disp_encoding_16bit)
15334
1
      {
15335
1
      bad_disp:
15336
1
        as_bad (_("invalid `%s' prefix"),
15337
1
          addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
15338
1
        return 0;
15339
1
      }
15340
15341
10.9k
    if ((i.base_reg
15342
5
         && ((addr_mode == CODE_64BIT
15343
5
        ? !i.base_reg->reg_type.bitfield.qword
15344
5
        : !i.base_reg->reg_type.bitfield.dword)
15345
4
       || (i.index_reg && i.base_reg->reg_num == RegIP)
15346
4
       || i.base_reg->reg_num == RegIZ))
15347
10.9k
        || (i.index_reg
15348
1
      && !i.index_reg->reg_type.bitfield.xmmword
15349
1
      && !i.index_reg->reg_type.bitfield.ymmword
15350
1
      && !i.index_reg->reg_type.bitfield.zmmword
15351
1
      && ((addr_mode == CODE_64BIT
15352
1
           ? !i.index_reg->reg_type.bitfield.qword
15353
1
           : !i.index_reg->reg_type.bitfield.dword)
15354
1
          || !i.index_reg->reg_type.bitfield.baseindex)))
15355
2
      goto bad_address;
15356
15357
    /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
15358
10.9k
    if (t->mnem_off == MN_bndmk
15359
10.9k
        || t->mnem_off == MN_bndldx
15360
10.9k
        || t->mnem_off == MN_bndstx
15361
10.9k
        || t->opcode_modifier.sib == SIBMEM)
15362
0
      {
15363
        /* They cannot use RIP-relative addressing. */
15364
0
        if (i.base_reg && i.base_reg->reg_num == RegIP)
15365
0
    {
15366
0
      as_bad (_("`%s' cannot be used here"), operand_string);
15367
0
      return 0;
15368
0
    }
15369
15370
        /* bndldx and bndstx ignore their scale factor. */
15371
0
        if ((t->mnem_off == MN_bndldx || t->mnem_off == MN_bndstx)
15372
0
      && i.log2_scale_factor)
15373
0
    as_warn (_("register scaling is being ignored here"));
15374
0
      }
15375
10.9k
  }
15376
1.91k
      else
15377
1.91k
  {
15378
    /* 16-bit checks.  */
15379
1.91k
    if (pp.disp_encoding == disp_encoding_32bit)
15380
0
      goto bad_disp;
15381
15382
1.91k
    if ((i.base_reg
15383
1
         && (!i.base_reg->reg_type.bitfield.word
15384
0
       || !i.base_reg->reg_type.bitfield.baseindex))
15385
1.91k
        || (i.index_reg
15386
0
      && (!i.index_reg->reg_type.bitfield.word
15387
0
          || !i.index_reg->reg_type.bitfield.baseindex
15388
0
          || !(i.base_reg
15389
0
         && i.base_reg->reg_num < 6
15390
0
         && i.index_reg->reg_num >= 6
15391
0
         && i.log2_scale_factor == 0))))
15392
1
      goto bad_address;
15393
1.91k
  }
15394
12.9k
    }
15395
12.9k
  return 1;
15396
12.9k
}
15397
15398
/* Handle vector immediates.  */
15399
15400
static int
15401
RC_SAE_immediate (const char *imm_start)
15402
17.9k
{
15403
17.9k
  const char *pstr = imm_start;
15404
15405
17.9k
  if (*pstr != '{')
15406
17.9k
    return 0;
15407
15408
10
  pstr++;
15409
10
  if (is_whitespace (*pstr))
15410
0
    pstr++;
15411
15412
10
  pstr = RC_SAE_specifier (pstr);
15413
10
  if (pstr == NULL)
15414
10
    return 0;
15415
15416
0
  if (is_whitespace (*pstr))
15417
0
    pstr++;
15418
15419
0
  if (*pstr++ != '}')
15420
0
    {
15421
0
      as_bad (_("Missing '}': '%s'"), imm_start);
15422
0
      return 0;
15423
0
    }
15424
0
  /* RC/SAE immediate string should contain nothing more.  */;
15425
0
  if (*pstr != 0)
15426
0
    {
15427
0
      as_bad (_("Junk after '}': '%s'"), imm_start);
15428
0
      return 0;
15429
0
    }
15430
15431
  /* Internally this doesn't count as an operand.  */
15432
0
  --i.operands;
15433
15434
0
  return 1;
15435
0
}
15436
15437
static INLINE bool starts_memory_operand (char c)
15438
10.8k
{
15439
10.8k
  return ISDIGIT (c)
15440
9.60k
   || is_name_beginner (c)
15441
207
   || (c && strchr ("([\"+-!~", c));
15442
10.8k
}
15443
15444
/* Parse OPERAND_STRING into the i386_insn structure I.  Returns zero
15445
   on error.  */
15446
15447
static int
15448
i386_att_operand (char *operand_string)
15449
14.3k
{
15450
14.3k
  const reg_entry *r;
15451
14.3k
  char *end_op;
15452
14.3k
  char *op_string = operand_string;
15453
15454
14.3k
  if (is_whitespace (*op_string))
15455
0
    ++op_string;
15456
15457
  /* We check for an absolute prefix (differentiating,
15458
     for example, 'jmp pc_relative_label' from 'jmp *absolute_label'.  */
15459
14.3k
  if (*op_string == ABSOLUTE_PREFIX
15460
17
      && current_templates.start->opcode_modifier.jump)
15461
17
    {
15462
17
      ++op_string;
15463
17
      if (is_whitespace (*op_string))
15464
0
  ++op_string;
15465
17
      i.jumpabsolute = true;
15466
17
    }
15467
15468
  /* Check if operand is a register.  */
15469
14.3k
  if ((r = parse_register (op_string, &end_op)) != NULL)
15470
460
    {
15471
460
      i386_operand_type temp;
15472
15473
460
      if (r == &bad_reg)
15474
0
  return 0;
15475
15476
      /* Check for a segment override by searching for ':' after a
15477
   segment register.  */
15478
460
      op_string = end_op;
15479
460
      if (is_whitespace (*op_string))
15480
23
  ++op_string;
15481
460
      if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
15482
5
  {
15483
5
    i.seg[i.mem_operands] = r;
15484
15485
    /* Skip the ':' and whitespace.  */
15486
5
    ++op_string;
15487
5
    if (is_whitespace (*op_string))
15488
0
      ++op_string;
15489
15490
    /* Handle case of %es:*foo.  */
15491
5
    if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX
15492
0
        && current_templates.start->opcode_modifier.jump)
15493
0
      {
15494
0
        ++op_string;
15495
0
        if (is_whitespace (*op_string))
15496
0
    ++op_string;
15497
0
        i.jumpabsolute = true;
15498
0
      }
15499
15500
5
    if (!starts_memory_operand (*op_string))
15501
0
      {
15502
0
        as_bad (_("bad memory operand `%s'"), op_string);
15503
0
        return 0;
15504
0
      }
15505
5
    goto do_memory_reference;
15506
5
  }
15507
15508
      /* Handle vector operations.  */
15509
455
      if (*op_string == '{')
15510
4
  {
15511
4
    op_string = check_VecOperations (op_string);
15512
4
    if (op_string == NULL)
15513
4
      return 0;
15514
4
  }
15515
15516
451
      if (*op_string)
15517
75
  {
15518
75
    as_bad (_("junk `%s' after register"), op_string);
15519
75
    return 0;
15520
75
  }
15521
15522
       /* Reject pseudo registers for .insn.  */
15523
376
      if (dot_insn () && r->reg_type.bitfield.class == ClassNone)
15524
0
  {
15525
0
    as_bad (_("`%s%s' cannot be used here"),
15526
0
      register_prefix, r->reg_name);
15527
0
    return 0;
15528
0
  }
15529
15530
376
      temp = r->reg_type;
15531
376
      temp.bitfield.baseindex = 0;
15532
376
      i.types[this_operand] = operand_type_or (i.types[this_operand],
15533
376
                 temp);
15534
376
      i.types[this_operand].bitfield.unspecified = 0;
15535
376
      i.op[this_operand].regs = r;
15536
376
      i.reg_operands++;
15537
15538
      /* A GPR may follow an RC or SAE immediate only if a (vector) register
15539
         operand was also present earlier on.  */
15540
376
      if (i.rounding.type != rc_none && temp.bitfield.class == Reg
15541
0
          && i.reg_operands == 1)
15542
0
  {
15543
0
    unsigned int j;
15544
15545
0
    for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
15546
0
      if (i.rounding.type == RC_NamesTable[j].type)
15547
0
        break;
15548
0
    as_bad (_("`%s': misplaced `{%s}'"),
15549
0
      insn_name (current_templates.start), RC_NamesTable[j].name);
15550
0
    return 0;
15551
0
  }
15552
376
    }
15553
13.8k
  else if (*op_string == REGISTER_PREFIX)
15554
69
    {
15555
69
      as_bad (_("bad register name `%s'"), op_string);
15556
69
      return 0;
15557
69
    }
15558
13.7k
  else if (*op_string == IMMEDIATE_PREFIX)
15559
2.91k
    {
15560
2.91k
      ++op_string;
15561
2.91k
      if (i.jumpabsolute)
15562
0
  {
15563
0
    as_bad (_("immediate operand illegal with absolute jump"));
15564
0
    return 0;
15565
0
  }
15566
2.91k
      if (!i386_immediate (op_string))
15567
6
  return 0;
15568
2.91k
      if (i.rounding.type != rc_none)
15569
0
  {
15570
0
    as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
15571
0
      insn_name (current_templates.start));
15572
0
    return 0;
15573
0
  }
15574
2.91k
    }
15575
10.8k
  else if (RC_SAE_immediate (operand_string))
15576
0
    {
15577
      /* If it is a RC or SAE immediate, do the necessary placement check:
15578
   Only another immediate or a GPR may precede it.  */
15579
0
      if (i.mem_operands || i.reg_operands + i.imm_operands > 1
15580
0
    || (i.reg_operands == 1
15581
0
        && i.op[0].regs->reg_type.bitfield.class != Reg))
15582
0
  {
15583
0
    as_bad (_("`%s': misplaced `%s'"),
15584
0
      insn_name (current_templates.start), operand_string);
15585
0
    return 0;
15586
0
  }
15587
0
    }
15588
10.8k
  else if (starts_memory_operand (*op_string))
15589
10.8k
    {
15590
      /* This is a memory reference of some sort.  */
15591
10.8k
      char *base_string;
15592
15593
      /* Start and end of displacement string expression (if found).  */
15594
10.8k
      char *displacement_string_start;
15595
10.8k
      char *displacement_string_end;
15596
15597
10.8k
    do_memory_reference:
15598
      /* Check for base index form.  We detect the base index form by
15599
   looking for an ')' at the end of the operand, searching
15600
   for the '(' matching it, and finding a REGISTER_PREFIX or ','
15601
   after the '('.  */
15602
10.8k
      base_string = op_string + strlen (op_string);
15603
15604
      /* Handle vector operations.  */
15605
10.8k
      --base_string;
15606
10.8k
      if (is_whitespace (*base_string))
15607
2
  --base_string;
15608
15609
10.8k
      if (*base_string == '}')
15610
15
  {
15611
15
    char *vop_start = NULL;
15612
15613
302
    while (base_string-- > op_string)
15614
301
      {
15615
301
        if (*base_string == '"')
15616
0
    break;
15617
301
        if (*base_string != '{')
15618
287
    continue;
15619
15620
14
        vop_start = base_string;
15621
15622
14
        --base_string;
15623
14
        if (is_whitespace (*base_string))
15624
7
    --base_string;
15625
15626
14
        if (*base_string != '}')
15627
14
    break;
15628
15629
0
        vop_start = NULL;
15630
0
      }
15631
15632
15
    if (!vop_start)
15633
1
      {
15634
1
        as_bad (_("unbalanced figure braces"));
15635
1
        return 0;
15636
1
      }
15637
15638
14
    if (check_VecOperations (vop_start) == NULL)
15639
14
      return 0;
15640
14
  }
15641
15642
      /* If we only have a displacement, set-up for it to be parsed later.  */
15643
10.8k
      displacement_string_start = op_string;
15644
10.8k
      displacement_string_end = base_string + 1;
15645
15646
10.8k
      if (*base_string == ')')
15647
139
  {
15648
139
    char *temp_string;
15649
139
    unsigned int parens_not_balanced = 0;
15650
139
    bool in_quotes = false;
15651
15652
    /* We've already checked that the number of left & right ()'s are
15653
       equal, and that there's a matching set of double quotes.  */
15654
139
    end_op = base_string;
15655
1.52k
    for (temp_string = op_string; temp_string < end_op; temp_string++)
15656
1.38k
      {
15657
1.38k
        if (*temp_string == '\\' && temp_string[1] == '"')
15658
0
    ++temp_string;
15659
1.38k
        else if (*temp_string == '"')
15660
124
    in_quotes = !in_quotes;
15661
1.25k
        else if (!in_quotes)
15662
1.25k
    {
15663
1.25k
      if (*temp_string == '(' && !parens_not_balanced++)
15664
139
        base_string = temp_string;
15665
1.25k
      if (*temp_string == ')')
15666
0
        --parens_not_balanced;
15667
1.25k
    }
15668
1.38k
      }
15669
15670
139
    temp_string = base_string;
15671
15672
    /* Skip past '(' and whitespace.  */
15673
139
    gas_assert (*base_string == '(');
15674
139
    ++base_string;
15675
139
    if (is_whitespace (*base_string))
15676
0
      ++base_string;
15677
15678
139
    if (*base_string == ','
15679
138
        || ((i.base_reg = parse_register (base_string, &end_op))
15680
138
      != NULL))
15681
5
      {
15682
5
        displacement_string_end = temp_string;
15683
15684
5
        i.types[this_operand].bitfield.baseindex = 1;
15685
15686
5
        if (i.base_reg)
15687
4
    {
15688
4
      if (i.base_reg == &bad_reg)
15689
0
        return 0;
15690
4
      base_string = end_op;
15691
4
      if (is_whitespace (*base_string))
15692
0
        ++base_string;
15693
4
    }
15694
15695
        /* There may be an index reg or scale factor here.  */
15696
5
        if (*base_string == ',')
15697
1
    {
15698
1
      ++base_string;
15699
1
      if (is_whitespace (*base_string))
15700
1
        ++base_string;
15701
15702
1
      if ((i.index_reg = parse_register (base_string, &end_op))
15703
1
          != NULL)
15704
0
        {
15705
0
          if (i.index_reg == &bad_reg)
15706
0
      return 0;
15707
0
          base_string = end_op;
15708
0
          if (is_whitespace (*base_string))
15709
0
      ++base_string;
15710
0
          if (*base_string == ',')
15711
0
      {
15712
0
        ++base_string;
15713
0
        if (is_whitespace (*base_string))
15714
0
          ++base_string;
15715
0
      }
15716
0
          else if (*base_string != ')')
15717
0
      {
15718
0
        as_bad (_("expecting `,' or `)' "
15719
0
            "after index register in `%s'"),
15720
0
          operand_string);
15721
0
        return 0;
15722
0
      }
15723
0
        }
15724
1
      else if (*base_string == REGISTER_PREFIX)
15725
0
        {
15726
0
          end_op = strchr (base_string, ',');
15727
0
          if (end_op)
15728
0
      *end_op = '\0';
15729
0
          as_bad (_("bad register name `%s'"), base_string);
15730
0
          return 0;
15731
0
        }
15732
15733
      /* Check for scale factor.  */
15734
1
      if (*base_string != ')')
15735
1
        {
15736
1
          char *end_scale = i386_scale (base_string);
15737
15738
1
          if (!end_scale)
15739
1
      return 0;
15740
15741
0
          base_string = end_scale;
15742
0
          if (is_whitespace (*base_string))
15743
0
      ++base_string;
15744
0
          if (*base_string != ')')
15745
0
      {
15746
0
        as_bad (_("expecting `)' "
15747
0
            "after scale factor in `%s'"),
15748
0
          operand_string);
15749
0
        return 0;
15750
0
      }
15751
0
        }
15752
0
      else if (!i.index_reg)
15753
0
        {
15754
0
          as_bad (_("expecting index register or scale factor "
15755
0
        "after `,'; got '%c'"),
15756
0
            *base_string);
15757
0
          return 0;
15758
0
        }
15759
1
    }
15760
4
        else if (*base_string != ')')
15761
0
    {
15762
0
      as_bad (_("expecting `,' or `)' "
15763
0
          "after base register in `%s'"),
15764
0
        operand_string);
15765
0
      return 0;
15766
0
    }
15767
5
      }
15768
134
    else if (*base_string == REGISTER_PREFIX)
15769
134
      {
15770
134
        end_op = strchr (base_string, ',');
15771
134
        if (end_op)
15772
0
    *end_op = '\0';
15773
134
        as_bad (_("bad register name `%s'"), base_string);
15774
134
        return 0;
15775
134
      }
15776
139
  }
15777
15778
      /* If there's an expression beginning the operand, parse it,
15779
   assuming displacement_string_start and
15780
   displacement_string_end are meaningful.  */
15781
10.6k
      if (displacement_string_start != displacement_string_end)
15782
10.6k
  {
15783
10.6k
    if (!i386_displacement (displacement_string_start,
15784
10.6k
          displacement_string_end))
15785
1
      return 0;
15786
10.6k
  }
15787
15788
      /* Special case for (%dx) while doing input/output op.  */
15789
10.6k
      if (i.base_reg
15790
4
    && i.base_reg->reg_type.bitfield.instance == RegD
15791
0
    && i.base_reg->reg_type.bitfield.word
15792
0
    && i.index_reg == 0
15793
0
    && i.log2_scale_factor == 0
15794
0
    && i.seg[i.mem_operands] == 0
15795
0
    && !operand_type_check (i.types[this_operand], disp))
15796
0
  {
15797
0
    i.types[this_operand] = i.base_reg->reg_type;
15798
0
    i.op[this_operand].regs = i.base_reg;
15799
0
    i.base_reg = NULL;
15800
0
    i.input_output_operand = true;
15801
0
    return 1;
15802
0
  }
15803
15804
10.6k
      if (i386_index_check (operand_string) == 0)
15805
1
  return 0;
15806
10.6k
      i.flags[this_operand] |= Operand_Mem;
15807
10.6k
      i.mem_operands++;
15808
10.6k
    }
15809
32
  else
15810
32
    {
15811
      /* It's not a memory operand; argh!  */
15812
32
      as_bad (_("invalid char %s beginning operand %d `%s'"),
15813
32
        output_invalid (*op_string),
15814
32
        this_operand + 1,
15815
32
        op_string);
15816
32
      return 0;
15817
32
    }
15818
13.9k
  return 1;     /* Normal return.  */
15819
14.3k
}
15820

15821
/* Initialize the tc_frag_data field of a fragment.  */
15822
15823
void i386_frag_init (fragS *fragP, size_t max_bytes)
15824
7.22k
{
15825
7.22k
  memset (&fragP->tc_frag_data, 0, sizeof (fragP->tc_frag_data));
15826
7.22k
  fragP->tc_frag_data.isa = cpu_arch_isa;
15827
7.22k
  fragP->tc_frag_data.tune = cpu_arch_tune;
15828
7.22k
  fragP->tc_frag_data.cpunop = cpu_arch_flags.bitfield.cpunop;
15829
7.22k
  fragP->tc_frag_data.isanop = cpu_arch_isa_flags.bitfield.cpunop;
15830
7.22k
  fragP->tc_frag_data.code = i386_flag_code;
15831
7.22k
  fragP->tc_frag_data.max_bytes = max_bytes;
15832
7.22k
  fragP->tc_frag_data.last_insn_normal
15833
7.22k
    = (seg_info(now_seg)->tc_segment_info_data.last_insn.kind
15834
7.22k
       == last_insn_other);
15835
7.22k
  fragP->tc_frag_data.no_cond_jump_promotion = no_cond_jump_promotion;
15836
7.22k
}
15837
15838
/* Calculate the maximum variable size (i.e., excluding fr_fix)
15839
   that an rs_machine_dependent frag may reach.  */
15840
15841
unsigned int
15842
i386_frag_max_var (fragS *frag)
15843
3
{
15844
  /* The only relaxable frags are for jumps.
15845
     Unconditional jumps can grow by 4 bytes and others by 5 bytes.  */
15846
3
  gas_assert (frag->fr_type == rs_machine_dependent);
15847
3
  return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
15848
3
}
15849
15850
#ifdef OBJ_ELF
15851
static int
15852
elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
15853
0
{
15854
  /* STT_GNU_IFUNC symbol must go through PLT.  */
15855
0
  if ((symbol_get_bfdsym (fr_symbol)->flags
15856
0
       & BSF_GNU_INDIRECT_FUNCTION) != 0)
15857
0
    return 0;
15858
15859
0
  if (!S_IS_EXTERNAL (fr_symbol))
15860
    /* Symbol may be weak or local.  */
15861
0
    return !S_IS_WEAK (fr_symbol);
15862
15863
  /* Global symbols with non-default visibility can't be preempted. */
15864
0
  if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
15865
0
    return 1;
15866
15867
0
  if (fr_var != NO_RELOC)
15868
0
    switch ((enum bfd_reloc_code_real) fr_var)
15869
0
      {
15870
0
      case BFD_RELOC_386_PLT32:
15871
0
      case BFD_RELOC_32_PLT_PCREL:
15872
  /* Symbol with PLT relocation may be preempted. */
15873
0
  return 0;
15874
0
      default:
15875
0
  abort ();
15876
0
      }
15877
15878
  /* Global symbols with default visibility in a shared library may be
15879
     preempted by another definition.  */
15880
0
  return !shared;
15881
0
}
15882
#endif
15883
15884
/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
15885
   Note also work for Skylake and Cascadelake.
15886
---------------------------------------------------------------------
15887
|   JCC   | ADD/SUB/CMP | INC/DEC | TEST/AND |
15888
| ------  | ----------- | ------- | -------- |
15889
|   Jo    |      N      |    N    |     Y    |
15890
|   Jno   |      N      |    N    |     Y    |
15891
|  Jc/Jb  |      Y      |    N    |     Y    |
15892
| Jae/Jnb |      Y      |    N    |     Y    |
15893
|  Je/Jz  |      Y      |    Y    |     Y    |
15894
| Jne/Jnz |      Y      |    Y    |     Y    |
15895
| Jna/Jbe |      Y      |    N    |     Y    |
15896
| Ja/Jnbe |      Y      |    N    |     Y    |
15897
|   Js    |      N      |    N    |     Y    |
15898
|   Jns   |      N      |    N    |     Y    |
15899
|  Jp/Jpe |      N      |    N    |     Y    |
15900
| Jnp/Jpo |      N      |    N    |     Y    |
15901
| Jl/Jnge |      Y      |    Y    |     Y    |
15902
| Jge/Jnl |      Y      |    Y    |     Y    |
15903
| Jle/Jng |      Y      |    Y    |     Y    |
15904
| Jg/Jnle |      Y      |    Y    |     Y    |
15905
---------------------------------------------------------------------  */
15906
static int
15907
i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
15908
0
{
15909
0
  if (mf_cmp == mf_cmp_alu_cmp)
15910
0
    return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
15911
0
      || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
15912
0
  if (mf_cmp == mf_cmp_incdec)
15913
0
    return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
15914
0
      || mf_jcc == mf_jcc_jle);
15915
0
  if (mf_cmp == mf_cmp_test_and)
15916
0
    return 1;
15917
0
  return 0;
15918
0
}
15919
15920
/* Return the next non-empty frag.  */
15921
15922
static fragS *
15923
i386_next_non_empty_frag (fragS *fragP)
15924
0
{
15925
  /* There may be a frag with a ".fill 0" when there is no room in
15926
     the current frag for frag_grow in output_insn.  */
15927
0
  for (fragP = fragP->fr_next;
15928
0
       (fragP != NULL
15929
0
  && fragP->fr_type == rs_fill
15930
0
  && fragP->fr_fix == 0);
15931
0
       fragP = fragP->fr_next)
15932
0
    ;
15933
0
  return fragP;
15934
0
}
15935
15936
/* Return the next jcc frag after BRANCH_PADDING.  */
15937
15938
static fragS *
15939
i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
15940
0
{
15941
0
  fragS *branch_fragP;
15942
0
  if (!pad_fragP)
15943
0
    return NULL;
15944
15945
0
  if (pad_fragP->fr_type == rs_machine_dependent
15946
0
      && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
15947
0
    == BRANCH_PADDING))
15948
0
    {
15949
0
      branch_fragP = i386_next_non_empty_frag (pad_fragP);
15950
0
      if (branch_fragP->fr_type != rs_machine_dependent)
15951
0
  return NULL;
15952
0
      if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
15953
0
    && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
15954
0
           pad_fragP->tc_frag_data.mf_type))
15955
0
  return branch_fragP;
15956
0
    }
15957
15958
0
  return NULL;
15959
0
}
15960
15961
/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags.  */
15962
15963
static void
15964
i386_classify_machine_dependent_frag (fragS *fragP)
15965
0
{
15966
0
  fragS *cmp_fragP;
15967
0
  fragS *pad_fragP;
15968
0
  fragS *branch_fragP;
15969
0
  fragS *next_fragP;
15970
0
  unsigned int max_prefix_length;
15971
15972
0
  if (fragP->tc_frag_data.classified)
15973
0
    return;
15974
15975
  /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING.  Convert
15976
     FUSED_JCC_PADDING and merge BRANCH_PADDING.  */
15977
0
  for (next_fragP = fragP;
15978
0
       next_fragP != NULL;
15979
0
       next_fragP = next_fragP->fr_next)
15980
0
    {
15981
0
      next_fragP->tc_frag_data.classified = 1;
15982
0
      if (next_fragP->fr_type == rs_machine_dependent)
15983
0
  switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
15984
0
    {
15985
0
    case BRANCH_PADDING:
15986
      /* The BRANCH_PADDING frag must be followed by a branch
15987
         frag.  */
15988
0
      branch_fragP = i386_next_non_empty_frag (next_fragP);
15989
0
      next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
15990
0
      break;
15991
0
    case FUSED_JCC_PADDING:
15992
      /* Check if this is a fused jcc:
15993
         FUSED_JCC_PADDING
15994
         CMP like instruction
15995
         BRANCH_PADDING
15996
         COND_JUMP
15997
         */
15998
0
      cmp_fragP = i386_next_non_empty_frag (next_fragP);
15999
0
      pad_fragP = i386_next_non_empty_frag (cmp_fragP);
16000
0
      branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
16001
0
      if (branch_fragP)
16002
0
        {
16003
    /* The BRANCH_PADDING frag is merged with the
16004
       FUSED_JCC_PADDING frag.  */
16005
0
    next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
16006
    /* CMP like instruction size.  */
16007
0
    next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
16008
0
    frag_wane (pad_fragP);
16009
    /* Skip to branch_fragP.  */
16010
0
    next_fragP = branch_fragP;
16011
0
        }
16012
0
      else if (next_fragP->tc_frag_data.max_prefix_length)
16013
0
        {
16014
    /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
16015
       a fused jcc.  */
16016
0
    next_fragP->fr_subtype
16017
0
      = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
16018
0
    next_fragP->tc_frag_data.max_bytes
16019
0
      = next_fragP->tc_frag_data.max_prefix_length;
16020
    /* This will be updated in the BRANCH_PREFIX scan.  */
16021
0
    next_fragP->tc_frag_data.max_prefix_length = 0;
16022
0
        }
16023
0
      else
16024
0
        frag_wane (next_fragP);
16025
0
      break;
16026
0
    }
16027
0
    }
16028
16029
  /* Stop if there is no BRANCH_PREFIX.  */
16030
0
  if (!align_branch_prefix_size)
16031
0
    return;
16032
16033
  /* Scan for BRANCH_PREFIX.  */
16034
0
  for (; fragP != NULL; fragP = fragP->fr_next)
16035
0
    {
16036
0
      if (fragP->fr_type != rs_machine_dependent
16037
0
    || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
16038
0
        != BRANCH_PREFIX))
16039
0
  continue;
16040
16041
      /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
16042
   COND_JUMP_PREFIX.  */
16043
0
      max_prefix_length = 0;
16044
0
      for (next_fragP = fragP;
16045
0
     next_fragP != NULL;
16046
0
     next_fragP = next_fragP->fr_next)
16047
0
  {
16048
0
    if (next_fragP->fr_type == rs_fill)
16049
      /* Skip rs_fill frags.  */
16050
0
      continue;
16051
0
    else if (next_fragP->fr_type != rs_machine_dependent)
16052
      /* Stop for all other frags.  */
16053
0
      break;
16054
16055
    /* rs_machine_dependent frags.  */
16056
0
    if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
16057
0
        == BRANCH_PREFIX)
16058
0
      {
16059
        /* Count BRANCH_PREFIX frags.  */
16060
0
        if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
16061
0
    {
16062
0
      max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
16063
0
      frag_wane (next_fragP);
16064
0
    }
16065
0
        else
16066
0
    max_prefix_length
16067
0
      += next_fragP->tc_frag_data.max_bytes;
16068
0
      }
16069
0
    else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
16070
0
        == BRANCH_PADDING)
16071
0
       || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
16072
0
           == FUSED_JCC_PADDING))
16073
0
      {
16074
        /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING.  */
16075
0
        fragP->tc_frag_data.u.padding_fragP = next_fragP;
16076
0
        break;
16077
0
      }
16078
0
    else
16079
      /* Stop for other rs_machine_dependent frags.  */
16080
0
      break;
16081
0
  }
16082
16083
0
      fragP->tc_frag_data.max_prefix_length = max_prefix_length;
16084
16085
      /* Skip to the next frag.  */
16086
0
      fragP = next_fragP;
16087
0
    }
16088
0
}
16089
16090
/* Compute padding size for
16091
16092
  FUSED_JCC_PADDING
16093
  CMP like instruction
16094
  BRANCH_PADDING
16095
  COND_JUMP/UNCOND_JUMP
16096
16097
   or
16098
16099
  BRANCH_PADDING
16100
  COND_JUMP/UNCOND_JUMP
16101
 */
16102
16103
static int
16104
i386_branch_padding_size (fragS *fragP, offsetT address)
16105
0
{
16106
0
  unsigned int offset, size, padding_size;
16107
0
  fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
16108
16109
  /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag.  */
16110
0
  if (!address)
16111
0
    address = fragP->fr_address;
16112
0
  address += fragP->fr_fix;
16113
16114
  /* CMP like instrunction size.  */
16115
0
  size = fragP->tc_frag_data.cmp_size;
16116
16117
  /* The base size of the branch frag.  */
16118
0
  size += branch_fragP->fr_fix;
16119
16120
  /* Add opcode and displacement bytes for the rs_machine_dependent
16121
     branch frag.  */
16122
0
  if (branch_fragP->fr_type == rs_machine_dependent)
16123
0
    size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
16124
16125
  /* Check if branch is within boundary and doesn't end at the last
16126
     byte.  */
16127
0
  offset = address & ((1U << align_branch_power) - 1);
16128
0
  if ((offset + size) >= (1U << align_branch_power))
16129
    /* Padding needed to avoid crossing boundary.  */
16130
0
    padding_size = (1U << align_branch_power) - offset;
16131
0
  else
16132
    /* No padding needed.  */
16133
0
    padding_size = 0;
16134
16135
  /* The return value may be saved in tc_frag_data.length which is
16136
     unsigned byte.  */
16137
0
  if (!fits_in_unsigned_byte (padding_size))
16138
0
    abort ();
16139
16140
0
  return padding_size;
16141
0
}
16142
16143
/* i386_generic_table_relax_frag()
16144
16145
   Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
16146
   grow/shrink padding to align branch frags.  Hand others to
16147
   relax_frag().  */
16148
16149
long
16150
i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
16151
0
{
16152
0
  if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
16153
0
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
16154
0
    {
16155
0
      long padding_size = i386_branch_padding_size (fragP, 0);
16156
0
      long grow = padding_size - fragP->tc_frag_data.length;
16157
16158
      /* When the BRANCH_PREFIX frag is used, the computed address
16159
         must match the actual address and there should be no padding.  */
16160
0
      if (fragP->tc_frag_data.padding_address
16161
0
    && (fragP->tc_frag_data.padding_address != fragP->fr_address
16162
0
        || padding_size))
16163
0
  abort ();
16164
16165
      /* Update the padding size.  */
16166
0
      if (grow)
16167
0
  fragP->tc_frag_data.length = padding_size;
16168
16169
0
      return grow;
16170
0
    }
16171
0
  else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
16172
0
    {
16173
0
      fragS *padding_fragP, *next_fragP;
16174
0
      long padding_size, left_size, last_size;
16175
16176
0
      padding_fragP = fragP->tc_frag_data.u.padding_fragP;
16177
0
      if (!padding_fragP)
16178
  /* Use the padding set by the leading BRANCH_PREFIX frag.  */
16179
0
  return (fragP->tc_frag_data.length
16180
0
    - fragP->tc_frag_data.last_length);
16181
16182
      /* Compute the relative address of the padding frag in the very
16183
        first time where the BRANCH_PREFIX frag sizes are zero.  */
16184
0
      if (!fragP->tc_frag_data.padding_address)
16185
0
  fragP->tc_frag_data.padding_address
16186
0
    = padding_fragP->fr_address - (fragP->fr_address - stretch);
16187
16188
      /* First update the last length from the previous interation.  */
16189
0
      left_size = fragP->tc_frag_data.prefix_length;
16190
0
      for (next_fragP = fragP;
16191
0
     next_fragP != padding_fragP;
16192
0
     next_fragP = next_fragP->fr_next)
16193
0
  if (next_fragP->fr_type == rs_machine_dependent
16194
0
      && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
16195
0
    == BRANCH_PREFIX))
16196
0
    {
16197
0
      if (left_size)
16198
0
        {
16199
0
    int max = next_fragP->tc_frag_data.max_bytes;
16200
0
    if (max)
16201
0
      {
16202
0
        int size;
16203
0
        if (max > left_size)
16204
0
          size = left_size;
16205
0
        else
16206
0
          size = max;
16207
0
        left_size -= size;
16208
0
        next_fragP->tc_frag_data.last_length = size;
16209
0
      }
16210
0
        }
16211
0
      else
16212
0
        next_fragP->tc_frag_data.last_length = 0;
16213
0
    }
16214
16215
      /* Check the padding size for the padding frag.  */
16216
0
      padding_size = i386_branch_padding_size
16217
0
  (padding_fragP, (fragP->fr_address
16218
0
       + fragP->tc_frag_data.padding_address));
16219
16220
0
      last_size = fragP->tc_frag_data.prefix_length;
16221
      /* Check if there is change from the last interation.  */
16222
0
      if (padding_size == last_size)
16223
0
  {
16224
    /* Update the expected address of the padding frag.  */
16225
0
    padding_fragP->tc_frag_data.padding_address
16226
0
      = (fragP->fr_address + padding_size
16227
0
         + fragP->tc_frag_data.padding_address);
16228
0
    return 0;
16229
0
  }
16230
16231
0
      if (padding_size > fragP->tc_frag_data.max_prefix_length)
16232
0
  {
16233
    /* No padding if there is no sufficient room.  Clear the
16234
       expected address of the padding frag.  */
16235
0
    padding_fragP->tc_frag_data.padding_address = 0;
16236
0
    padding_size = 0;
16237
0
  }
16238
0
      else
16239
  /* Store the expected address of the padding frag.  */
16240
0
  padding_fragP->tc_frag_data.padding_address
16241
0
    = (fragP->fr_address + padding_size
16242
0
       + fragP->tc_frag_data.padding_address);
16243
16244
0
      fragP->tc_frag_data.prefix_length = padding_size;
16245
16246
      /* Update the length for the current interation.  */
16247
0
      left_size = padding_size;
16248
0
      for (next_fragP = fragP;
16249
0
     next_fragP != padding_fragP;
16250
0
     next_fragP = next_fragP->fr_next)
16251
0
  if (next_fragP->fr_type == rs_machine_dependent
16252
0
      && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
16253
0
    == BRANCH_PREFIX))
16254
0
    {
16255
0
      if (left_size)
16256
0
        {
16257
0
    int max = next_fragP->tc_frag_data.max_bytes;
16258
0
    if (max)
16259
0
      {
16260
0
        int size;
16261
0
        if (max > left_size)
16262
0
          size = left_size;
16263
0
        else
16264
0
          size = max;
16265
0
        left_size -= size;
16266
0
        next_fragP->tc_frag_data.length = size;
16267
0
      }
16268
0
        }
16269
0
      else
16270
0
        next_fragP->tc_frag_data.length = 0;
16271
0
    }
16272
16273
0
      return (fragP->tc_frag_data.length
16274
0
        - fragP->tc_frag_data.last_length);
16275
0
    }
16276
0
  return relax_frag (segment, fragP, stretch);
16277
0
}
16278
16279
/* md_estimate_size_before_relax()
16280
16281
   Called just before relax() for rs_machine_dependent frags.  The x86
16282
   assembler uses these frags to handle variable size jump
16283
   instructions.
16284
16285
   Any symbol that is now undefined will not become defined.
16286
   Return the correct fr_subtype in the frag.
16287
   Return the initial "guess for variable size of frag" to caller.
16288
   The guess is actually the growth beyond the fixed part.  Whatever
16289
   we do to grow the fixed or variable part contributes to our
16290
   returned value.  */
16291
16292
int
16293
md_estimate_size_before_relax (fragS *fragP, segT segment)
16294
0
{
16295
0
  if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
16296
0
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
16297
0
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
16298
0
    {
16299
0
      i386_classify_machine_dependent_frag (fragP);
16300
0
      return fragP->tc_frag_data.length;
16301
0
    }
16302
16303
  /* We've already got fragP->fr_subtype right;  all we have to do is
16304
     check for un-relaxable symbols.  On an ELF system, we can't relax
16305
     an externally visible symbol, because it may be overridden by a
16306
     shared library.  */
16307
0
  if (S_GET_SEGMENT (fragP->fr_symbol) != segment
16308
0
#ifdef OBJ_ELF
16309
0
      || !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
16310
0
              fragP->fr_var)
16311
0
#endif
16312
#if defined (OBJ_COFF) && defined (TE_PE)
16313
      || S_IS_WEAK (fragP->fr_symbol)
16314
#endif
16315
0
      )
16316
0
    {
16317
      /* Symbol is undefined in this segment, or we need to keep a
16318
   reloc so that weak symbols can be overridden.  */
16319
0
      int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
16320
0
      enum bfd_reloc_code_real reloc_type;
16321
0
      unsigned char *opcode;
16322
0
      int old_fr_fix;
16323
0
      fixS *fixP = NULL;
16324
16325
0
      reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
16326
0
#ifdef OBJ_ELF
16327
0
      if (reloc_type == NO_RELOC
16328
0
    && size != 2
16329
0
    && fragP->tc_frag_data.code == CODE_64BIT
16330
0
    && fragP->fr_offset == 0
16331
0
    && need_plt32_p (fragP->fr_symbol))
16332
0
  reloc_type = BFD_RELOC_32_PLT_PCREL;
16333
0
#endif
16334
16335
0
      old_fr_fix = fragP->fr_fix;
16336
0
      opcode = (unsigned char *) fragP->fr_opcode;
16337
16338
0
      switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
16339
0
  {
16340
0
  case UNCOND_JUMP:
16341
    /* Make jmp (0xeb) a (d)word displacement jump.  */
16342
0
    opcode[0] = 0xe9;
16343
0
    fragP->fr_fix += size;
16344
0
    fixP = fix_new (fragP, old_fr_fix, size,
16345
0
        fragP->fr_symbol,
16346
0
        fragP->fr_offset, 1,
16347
0
        _reloc (size, 1, 1, reloc_type,
16348
0
          fragP->tc_frag_data.code == CODE_64BIT,
16349
0
          fragP->fr_file, fragP->fr_line));
16350
0
    break;
16351
16352
0
  case COND_JUMP86:
16353
0
    if (fragP->tc_frag_data.no_cond_jump_promotion
16354
0
        && fragP->fr_var == NO_RELOC)
16355
0
      {
16356
0
        fragP->fr_fix += 1;
16357
0
        fixP = fix_new (fragP, old_fr_fix, 1,
16358
0
            fragP->fr_symbol,
16359
0
            fragP->fr_offset, 1,
16360
0
            BFD_RELOC_8_PCREL);
16361
0
        fixP->fx_signed = 1;
16362
0
        break;
16363
0
      }
16364
16365
0
    if (size == 2)
16366
0
      {
16367
        /* Negate the condition, and branch past an
16368
     unconditional jump.  */
16369
0
        opcode[0] ^= 1;
16370
0
        opcode[1] = 3;
16371
        /* Insert an unconditional jump.  */
16372
0
        opcode[2] = 0xe9;
16373
        /* We added two extra opcode bytes, and have a two byte
16374
     offset.  */
16375
0
        fragP->fr_fix += 2 + 2;
16376
0
        fix_new (fragP, old_fr_fix + 2, 2,
16377
0
           fragP->fr_symbol,
16378
0
           fragP->fr_offset, 1,
16379
0
           _reloc (size, 1, 1, reloc_type,
16380
0
             fragP->tc_frag_data.code == CODE_64BIT,
16381
0
             fragP->fr_file, fragP->fr_line));
16382
0
        break;
16383
0
      }
16384
    /* Fall through.  */
16385
16386
0
  case COND_JUMP:
16387
    /* This changes the byte-displacement jump 0x7N
16388
       to the (d)word-displacement jump 0x0f,0x8N.  */
16389
0
    opcode[1] = opcode[0] + 0x10;
16390
0
    opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
16391
    /* We've added an opcode byte.  */
16392
0
    fragP->fr_fix += 1 + size;
16393
0
    fixP = fix_new (fragP, old_fr_fix + 1, size,
16394
0
        fragP->fr_symbol,
16395
0
        fragP->fr_offset, 1,
16396
0
        _reloc (size, 1, 1, reloc_type,
16397
0
          fragP->tc_frag_data.code == CODE_64BIT,
16398
0
          fragP->fr_file, fragP->fr_line));
16399
0
    break;
16400
16401
0
  default:
16402
0
    BAD_CASE (fragP->fr_subtype);
16403
0
    break;
16404
0
  }
16405
16406
      /* All jumps handled here are signed, but don't unconditionally use a
16407
   signed limit check for 32 and 16 bit jumps as we want to allow wrap
16408
   around at 4G (outside of 64-bit mode) and 64k.  */
16409
0
      if (size == 4 && flag_code == CODE_64BIT)
16410
0
  fixP->fx_signed = 1;
16411
16412
0
      frag_wane (fragP);
16413
0
      return fragP->fr_fix - old_fr_fix;
16414
0
    }
16415
16416
  /* Guess size depending on current relax state.  Initially the relax
16417
     state will correspond to a short jump and we return 1, because
16418
     the variable part of the frag (the branch offset) is one byte
16419
     long.  However, we can relax a section more than once and in that
16420
     case we must either set fr_subtype back to the unrelaxed state,
16421
     or return the value for the appropriate branch.  */
16422
0
  return md_relax_table[fragP->fr_subtype].rlx_length;
16423
0
}
16424
16425
/* Called after relax() is finished.
16426
16427
   In:  Address of frag.
16428
  fr_type == rs_machine_dependent.
16429
  fr_subtype is what the address relaxed to.
16430
16431
   Out: Any fixSs and constants are set up.
16432
  Caller will turn frag into a ".space 0".  */
16433
16434
void
16435
md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
16436
                 fragS *fragP)
16437
0
{
16438
0
  unsigned char *opcode;
16439
0
  unsigned char *where_to_put_displacement = NULL;
16440
0
  offsetT target_address;
16441
0
  offsetT opcode_address;
16442
0
  unsigned int extension = 0;
16443
0
  offsetT displacement_from_opcode_start;
16444
16445
0
  if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
16446
0
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
16447
0
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
16448
0
    {
16449
      /* Generate nop padding.  */
16450
0
      unsigned int size = fragP->tc_frag_data.length;
16451
0
      if (size)
16452
0
  {
16453
0
    if (size > fragP->tc_frag_data.max_bytes)
16454
0
      abort ();
16455
16456
0
    if (flag_debug)
16457
0
      {
16458
0
        const char *msg;
16459
0
        const char *branch = "branch";
16460
0
        const char *prefix = "";
16461
0
        fragS *padding_fragP;
16462
0
        if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
16463
0
      == BRANCH_PREFIX)
16464
0
    {
16465
0
      padding_fragP = fragP->tc_frag_data.u.padding_fragP;
16466
0
      switch (fragP->tc_frag_data.default_prefix)
16467
0
        {
16468
0
        default:
16469
0
          abort ();
16470
0
          break;
16471
0
        case CS_PREFIX_OPCODE:
16472
0
          prefix = " cs";
16473
0
          break;
16474
0
        case DS_PREFIX_OPCODE:
16475
0
          prefix = " ds";
16476
0
          break;
16477
0
        case ES_PREFIX_OPCODE:
16478
0
          prefix = " es";
16479
0
          break;
16480
0
        case FS_PREFIX_OPCODE:
16481
0
          prefix = " fs";
16482
0
          break;
16483
0
        case GS_PREFIX_OPCODE:
16484
0
          prefix = " gs";
16485
0
          break;
16486
0
        case SS_PREFIX_OPCODE:
16487
0
          prefix = " ss";
16488
0
          break;
16489
0
        }
16490
0
      if (padding_fragP)
16491
0
        msg = _("%s:%u: add %d%s at 0x%llx to align "
16492
0
          "%s within %d-byte boundary\n");
16493
0
      else
16494
0
        msg = _("%s:%u: add additional %d%s at 0x%llx to "
16495
0
          "align %s within %d-byte boundary\n");
16496
0
    }
16497
0
        else
16498
0
    {
16499
0
      padding_fragP = fragP;
16500
0
      msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
16501
0
        "%s within %d-byte boundary\n");
16502
0
    }
16503
16504
0
        if (padding_fragP)
16505
0
    switch (padding_fragP->tc_frag_data.branch_type)
16506
0
      {
16507
0
      case align_branch_jcc:
16508
0
        branch = "jcc";
16509
0
        break;
16510
0
      case align_branch_fused:
16511
0
        branch = "fused jcc";
16512
0
        break;
16513
0
      case align_branch_jmp:
16514
0
        branch = "jmp";
16515
0
        break;
16516
0
      case align_branch_call:
16517
0
        branch = "call";
16518
0
        break;
16519
0
      case align_branch_indirect:
16520
0
        branch = "indiret branch";
16521
0
        break;
16522
0
      case align_branch_ret:
16523
0
        branch = "ret";
16524
0
        break;
16525
0
      default:
16526
0
        break;
16527
0
      }
16528
16529
0
        fprintf (stdout, msg,
16530
0
           fragP->fr_file, fragP->fr_line, size, prefix,
16531
0
           (long long) fragP->fr_address, branch,
16532
0
           1 << align_branch_power);
16533
0
      }
16534
0
    if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
16535
0
      memset (fragP->fr_opcode,
16536
0
        fragP->tc_frag_data.default_prefix, size);
16537
0
    else
16538
0
      i386_generate_nops (fragP, (char *) fragP->fr_opcode,
16539
0
        size, 0);
16540
0
    fragP->fr_fix += size;
16541
0
  }
16542
0
      return;
16543
0
    }
16544
16545
0
  opcode = (unsigned char *) fragP->fr_opcode;
16546
16547
  /* Address we want to reach in file space.  */
16548
0
  target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
16549
16550
  /* Address opcode resides at in file space.  */
16551
0
  opcode_address = fragP->fr_address + fragP->fr_fix;
16552
16553
  /* Displacement from opcode start to fill into instruction.  */
16554
0
  displacement_from_opcode_start = target_address - opcode_address;
16555
16556
0
  if ((fragP->fr_subtype & BIG) == 0)
16557
0
    {
16558
      /* Don't have to change opcode.  */
16559
0
      extension = 1;    /* 1 opcode + 1 displacement  */
16560
0
      where_to_put_displacement = &opcode[1];
16561
0
    }
16562
0
  else
16563
0
    {
16564
0
      if (fragP->tc_frag_data.no_cond_jump_promotion
16565
0
    && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
16566
0
  as_warn_where (fragP->fr_file, fragP->fr_line,
16567
0
           _("long jump required"));
16568
16569
0
      switch (fragP->fr_subtype)
16570
0
  {
16571
0
  case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
16572
0
    extension = 4;    /* 1 opcode + 4 displacement  */
16573
0
    opcode[0] = 0xe9;
16574
0
    where_to_put_displacement = &opcode[1];
16575
0
    break;
16576
16577
0
  case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
16578
0
    extension = 2;    /* 1 opcode + 2 displacement  */
16579
0
    opcode[0] = 0xe9;
16580
0
    where_to_put_displacement = &opcode[1];
16581
0
    break;
16582
16583
0
  case ENCODE_RELAX_STATE (COND_JUMP, BIG):
16584
0
  case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
16585
0
    extension = 5;    /* 2 opcode + 4 displacement  */
16586
0
    opcode[1] = opcode[0] + 0x10;
16587
0
    opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
16588
0
    where_to_put_displacement = &opcode[2];
16589
0
    break;
16590
16591
0
  case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
16592
0
    extension = 3;    /* 2 opcode + 2 displacement  */
16593
0
    opcode[1] = opcode[0] + 0x10;
16594
0
    opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
16595
0
    where_to_put_displacement = &opcode[2];
16596
0
    break;
16597
16598
0
  case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
16599
0
    extension = 4;
16600
0
    opcode[0] ^= 1;
16601
0
    opcode[1] = 3;
16602
0
    opcode[2] = 0xe9;
16603
0
    where_to_put_displacement = &opcode[3];
16604
0
    break;
16605
16606
0
  default:
16607
0
    BAD_CASE (fragP->fr_subtype);
16608
0
    break;
16609
0
  }
16610
0
    }
16611
16612
  /* If size if less then four we are sure that the operand fits,
16613
     but if it's 4, then it could be that the displacement is larger
16614
     then -/+ 2GB.  */
16615
0
  if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
16616
0
      && object_64bit
16617
0
      && ((addressT) (displacement_from_opcode_start - extension
16618
0
          + ((addressT) 1 << 31))
16619
0
    > (((addressT) 2 << 31) - 1)))
16620
0
    {
16621
0
      as_bad_where (fragP->fr_file, fragP->fr_line,
16622
0
        _("jump target out of range"));
16623
      /* Make us emit 0.  */
16624
0
      displacement_from_opcode_start = extension;
16625
0
    }
16626
  /* Now put displacement after opcode.  */
16627
0
  md_number_to_chars ((char *) where_to_put_displacement,
16628
0
          displacement_from_opcode_start - extension,
16629
0
          DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
16630
0
  fragP->fr_fix += extension;
16631
0
}
16632

16633
/* Apply a fixup (fixP) to segment data, once it has been determined
16634
   by our caller that we have all the info we need to fix it up.
16635
16636
   Parameter valP is the pointer to the value of the bits.
16637
16638
   On the 386, immediates, displacements, and data pointers are all in
16639
   the same (little-endian) format, so we don't need to care about which
16640
   we are handling.  */
16641
16642
void
16643
md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
16644
0
{
16645
0
  char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
16646
0
  valueT value = *valP;
16647
16648
0
#if !defined (TE_Mach)
16649
0
  if (fixP->fx_pcrel)
16650
0
    {
16651
0
      switch (fixP->fx_r_type)
16652
0
  {
16653
0
  default:
16654
0
    break;
16655
16656
0
  case BFD_RELOC_64:
16657
0
    fixP->fx_r_type = BFD_RELOC_64_PCREL;
16658
0
    break;
16659
0
  case BFD_RELOC_32:
16660
0
  case BFD_RELOC_X86_64_32S:
16661
0
    fixP->fx_r_type = BFD_RELOC_32_PCREL;
16662
0
    break;
16663
0
  case BFD_RELOC_16:
16664
0
    fixP->fx_r_type = BFD_RELOC_16_PCREL;
16665
0
    break;
16666
0
  case BFD_RELOC_8:
16667
0
    fixP->fx_r_type = BFD_RELOC_8_PCREL;
16668
0
    break;
16669
0
  }
16670
0
    }
16671
16672
0
  if (fixP->fx_addsy != NULL
16673
0
      && (fixP->fx_r_type == BFD_RELOC_32_PCREL
16674
0
    || fixP->fx_r_type == BFD_RELOC_64_PCREL
16675
0
    || fixP->fx_r_type == BFD_RELOC_16_PCREL
16676
0
    || fixP->fx_r_type == BFD_RELOC_8_PCREL)
16677
0
      && !use_rela_relocations)
16678
0
    {
16679
      /* This is a hack.  There should be a better way to handle this.
16680
   This covers for the fact that bfd_install_relocation will
16681
   subtract the current location (for partial_inplace, PC relative
16682
   relocations); see more below.  */
16683
0
#if defined (OBJ_ELF) || defined (TE_PE)
16684
0
      value += fixP->fx_where + fixP->fx_frag->fr_address;
16685
0
#endif
16686
0
#ifdef OBJ_ELF
16687
0
      segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
16688
16689
0
      if ((sym_seg == seg
16690
0
     || (symbol_section_p (fixP->fx_addsy)
16691
0
         && sym_seg != absolute_section))
16692
0
    && !generic_force_reloc (fixP))
16693
0
  {
16694
    /* Yes, we add the values in twice.  This is because
16695
       bfd_install_relocation subtracts them out again.  I think
16696
       bfd_install_relocation is broken, but I don't dare change
16697
       it.  FIXME.  */
16698
0
    value += fixP->fx_where + fixP->fx_frag->fr_address;
16699
0
  }
16700
0
#endif
16701
#if defined (OBJ_COFF) && defined (TE_PE)
16702
      /* For some reason, the PE format does not store a
16703
   section address offset for a PC relative symbol.  */
16704
      if (S_GET_SEGMENT (fixP->fx_addsy) != seg
16705
    || S_IS_WEAK (fixP->fx_addsy))
16706
  value += md_pcrel_from (fixP);
16707
#endif
16708
0
    }
16709
#if defined (OBJ_COFF) && defined (TE_PE)
16710
  if (fixP->fx_addsy != NULL
16711
      && S_IS_WEAK (fixP->fx_addsy)
16712
      /* PR 16858: Do not modify weak function references.  */
16713
      && ! fixP->fx_pcrel)
16714
    {
16715
#if !defined (TE_PEP)
16716
      /* For x86 PE weak function symbols are neither PC-relative
16717
   nor do they set S_IS_FUNCTION.  So the only reliable way
16718
   to detect them is to check the flags of their containing
16719
   section.  */
16720
      if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
16721
    && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
16722
  ;
16723
      else
16724
#endif
16725
      value -= S_GET_VALUE (fixP->fx_addsy);
16726
    }
16727
#endif
16728
16729
  /* Fix a few things - the dynamic linker expects certain values here,
16730
     and we must not disappoint it.  */
16731
0
#ifdef OBJ_ELF
16732
0
  if (fixP->fx_addsy)
16733
0
    switch (fixP->fx_r_type)
16734
0
      {
16735
0
      case BFD_RELOC_386_PLT32:
16736
0
      case BFD_RELOC_32_PLT_PCREL:
16737
  /* Make the jump instruction point to the address of the operand.
16738
     At runtime we merely add the offset to the actual PLT entry.
16739
     NB: Subtract the offset size only for jump instructions.  */
16740
0
  if (fixP->fx_pcrel)
16741
0
    value = -4;
16742
0
  break;
16743
16744
0
      case BFD_RELOC_386_TLS_GD:
16745
0
      case BFD_RELOC_386_TLS_LDM:
16746
0
      case BFD_RELOC_386_TLS_IE_32:
16747
0
      case BFD_RELOC_386_TLS_IE:
16748
0
      case BFD_RELOC_386_TLS_GOTIE:
16749
0
      case BFD_RELOC_386_TLS_GOTDESC:
16750
0
      case BFD_RELOC_X86_64_TLSGD:
16751
0
      case BFD_RELOC_X86_64_TLSLD:
16752
0
      case BFD_RELOC_X86_64_GOTTPOFF:
16753
0
      case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
16754
0
      case BFD_RELOC_X86_64_CODE_5_GOTTPOFF:
16755
0
      case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
16756
0
      case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
16757
0
      case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
16758
0
      case BFD_RELOC_X86_64_CODE_5_GOTPC32_TLSDESC:
16759
0
      case BFD_RELOC_X86_64_CODE_6_GOTPC32_TLSDESC:
16760
0
  value = 0; /* Fully resolved at runtime.  No addend.  */
16761
  /* Fallthrough */
16762
0
      case BFD_RELOC_386_TLS_LE:
16763
0
      case BFD_RELOC_386_TLS_LDO_32:
16764
0
      case BFD_RELOC_386_TLS_LE_32:
16765
0
      case BFD_RELOC_X86_64_DTPOFF32:
16766
0
      case BFD_RELOC_X86_64_DTPOFF64:
16767
0
      case BFD_RELOC_X86_64_TPOFF32:
16768
0
      case BFD_RELOC_X86_64_TPOFF64:
16769
0
  S_SET_THREAD_LOCAL (fixP->fx_addsy);
16770
0
  break;
16771
16772
0
      case BFD_RELOC_386_TLS_DESC_CALL:
16773
0
      case BFD_RELOC_X86_64_TLSDESC_CALL:
16774
0
  value = 0; /* Fully resolved at runtime.  No addend.  */
16775
0
  S_SET_THREAD_LOCAL (fixP->fx_addsy);
16776
0
  fixP->fx_done = 0;
16777
0
  return;
16778
16779
0
      case BFD_RELOC_VTABLE_INHERIT:
16780
0
      case BFD_RELOC_VTABLE_ENTRY:
16781
0
  fixP->fx_done = 0;
16782
0
  return;
16783
16784
0
      default:
16785
0
  break;
16786
0
      }
16787
0
#endif /* OBJ_ELF  */
16788
16789
  /* If not 64bit, massage value, to account for wraparound when !BFD64.  */
16790
0
  if (!object_64bit)
16791
0
    value = extend_to_32bit_address (value);
16792
16793
0
  *valP = value;
16794
0
#endif /* !defined (TE_Mach)  */
16795
16796
  /* Are we finished with this relocation now?  */
16797
0
  if (fixP->fx_addsy == NULL)
16798
0
    {
16799
0
      fixP->fx_done = 1;
16800
0
      switch (fixP->fx_r_type)
16801
0
  {
16802
0
  case BFD_RELOC_X86_64_32S:
16803
0
    fixP->fx_signed = 1;
16804
0
    break;
16805
16806
0
  default:
16807
0
    break;
16808
0
  }
16809
0
    }
16810
#if defined (OBJ_COFF) && defined (TE_PE)
16811
  else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
16812
    {
16813
      fixP->fx_done = 0;
16814
      /* Remember value for tc_gen_reloc.  */
16815
      fixP->fx_addnumber = value;
16816
      /* Clear out the frag for now.  */
16817
      value = 0;
16818
    }
16819
#endif
16820
0
  else if (use_rela_relocations)
16821
0
    {
16822
0
      if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
16823
0
  fixP->fx_no_overflow = 1;
16824
      /* Remember value for tc_gen_reloc.  */
16825
0
      fixP->fx_addnumber = value;
16826
0
      value = 0;
16827
0
    }
16828
16829
0
  md_number_to_chars (p, value, fixP->fx_size);
16830
0
}
16831

16832
const char *
16833
md_atof (int type, char *litP, int *sizeP)
16834
686
{
16835
  /* This outputs the LITTLENUMs in REVERSE order;
16836
     in accord with the bigendian 386.  */
16837
686
  return ieee_md_atof (type, litP, sizeP, false);
16838
686
}
16839

16840
static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
16841
16842
static char *
16843
output_invalid (int c)
16844
68.7k
{
16845
68.7k
  if (ISPRINT (c))
16846
11.7k
    snprintf (output_invalid_buf, sizeof (output_invalid_buf),
16847
11.7k
        "'%c'", c);
16848
57.0k
  else
16849
57.0k
    snprintf (output_invalid_buf, sizeof (output_invalid_buf),
16850
57.0k
        "(0x%x)", (unsigned char) c);
16851
68.7k
  return output_invalid_buf;
16852
68.7k
}
16853
16854
/* Verify that @r can be used in the current context.  */
16855
16856
static bool check_register (const reg_entry *r)
16857
2.64k
{
16858
2.64k
  if (allow_pseudo_reg)
16859
22
    return true;
16860
16861
2.62k
  if (operand_type_all_zero (&r->reg_type))
16862
0
    return false;
16863
16864
2.62k
  if ((r->reg_type.bitfield.dword
16865
2.39k
       || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
16866
2.38k
       || r->reg_type.bitfield.class == RegCR
16867
2.38k
       || r->reg_type.bitfield.class == RegDR)
16868
457
      && !cpu_arch_flags.bitfield.cpui386)
16869
0
    return false;
16870
16871
2.62k
  if (r->reg_type.bitfield.class == RegTR
16872
38
      && (flag_code == CODE_64BIT
16873
0
    || !cpu_arch_flags.bitfield.cpui386
16874
0
    || cpu_arch_isa_flags.bitfield.cpui586
16875
0
    || cpu_arch_isa_flags.bitfield.cpui686))
16876
38
    return false;
16877
16878
2.58k
  if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
16879
0
    return false;
16880
16881
2.58k
  if (!cpu_arch_flags.bitfield.cpuavx512f)
16882
1.49k
    {
16883
1.49k
      if (r->reg_type.bitfield.zmmword
16884
1.49k
    || r->reg_type.bitfield.class == RegMask)
16885
0
  return false;
16886
16887
1.49k
      if (!cpu_arch_flags.bitfield.cpuavx)
16888
0
  {
16889
0
    if (r->reg_type.bitfield.ymmword)
16890
0
      return false;
16891
16892
0
    if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
16893
0
      return false;
16894
0
  }
16895
1.49k
    }
16896
16897
2.58k
  if (r->reg_type.bitfield.zmmword)
16898
5
    {
16899
5
      if (vector_size < VSZ512)
16900
0
  return false;
16901
16902
      /* Don't update pp when not dealing with insn operands.  */
16903
5
      switch (current_templates.start ? pp.encoding : encoding_evex)
16904
5
  {
16905
3
  case encoding_default:
16906
3
  case encoding_egpr:
16907
3
    pp.encoding = encoding_evex512;
16908
3
    break;
16909
0
  case encoding_evex:
16910
2
  case encoding_evex512:
16911
2
    break;
16912
0
  default:
16913
0
    pp.encoding = encoding_error;
16914
0
    break;
16915
5
  }
16916
5
    }
16917
16918
2.58k
  if (vector_size < VSZ256 && r->reg_type.bitfield.ymmword)
16919
0
    return false;
16920
16921
2.58k
  if (r->reg_type.bitfield.tmmword
16922
4
      && (!cpu_arch_flags.bitfield.cpuamx_tile
16923
4
          || flag_code != CODE_64BIT))
16924
0
    return false;
16925
16926
2.58k
  if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
16927
0
    return false;
16928
16929
  /* Don't allow fake index register unless allow_index_reg isn't 0. */
16930
2.58k
  if (!allow_index_reg && r->reg_num == RegIZ)
16931
2
    return false;
16932
16933
  /* Upper 16 vector registers are only available with VREX in 64bit
16934
     mode, and require EVEX encoding.  */
16935
2.58k
  if (r->reg_flags & RegVRex)
16936
10
    {
16937
10
      if (!cpu_arch_flags.bitfield.cpuavx512f
16938
5
    || flag_code != CODE_64BIT)
16939
9
  return false;
16940
16941
      /* Don't update pp when not dealing with insn operands.  */
16942
1
      switch (current_templates.start ? pp.encoding : encoding_evex)
16943
1
  {
16944
0
    case encoding_default:
16945
0
    case encoding_egpr:
16946
0
    case encoding_evex512:
16947
0
      pp.encoding = encoding_evex;
16948
0
      break;
16949
1
    case encoding_evex:
16950
1
      break;
16951
0
    default:
16952
0
      pp.encoding = encoding_error;
16953
0
      break;
16954
1
  }
16955
1
    }
16956
16957
2.57k
  if (r->reg_flags & RegRex2)
16958
149
    {
16959
149
      if (!cpu_arch_flags.bitfield.cpuapx_f
16960
149
    || flag_code != CODE_64BIT)
16961
133
  return false;
16962
16963
      /* Don't update pp when not dealing with insn operands.  */
16964
16
      switch (current_templates.start ? pp.encoding : encoding_egpr)
16965
16
  {
16966
11
  case encoding_default:
16967
11
    pp.encoding = encoding_egpr;
16968
11
    break;
16969
2
  case encoding_egpr:
16970
5
  case encoding_evex:
16971
5
  case encoding_evex512:
16972
5
    break;
16973
0
  default:
16974
0
    pp.encoding = encoding_error;
16975
0
    break;
16976
16
  }
16977
16
    }
16978
16979
2.44k
  if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
16980
649
      && (!cpu_arch_flags.bitfield.cpu64
16981
649
    || r->reg_type.bitfield.class != RegCR
16982
0
    || dot_insn ())
16983
649
      && flag_code != CODE_64BIT)
16984
252
    return false;
16985
16986
2.18k
  if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
16987
2
      && !intel_syntax)
16988
0
    return false;
16989
16990
2.18k
  return true;
16991
2.18k
}
16992
16993
/* REG_STRING starts *before* REGISTER_PREFIX.  */
16994
16995
static const reg_entry *
16996
parse_real_register (const char *reg_string, char **end_op)
16997
2.95k
{
16998
2.95k
  const char *s = reg_string;
16999
2.95k
  char *p;
17000
2.95k
  char reg_name_given[MAX_REG_NAME_SIZE + 1];
17001
2.95k
  const reg_entry *r;
17002
17003
  /* Skip possible REGISTER_PREFIX and possible whitespace.  */
17004
2.95k
  if (*s == REGISTER_PREFIX)
17005
2.70k
    ++s;
17006
17007
2.95k
  if (is_whitespace (*s))
17008
0
    ++s;
17009
17010
2.95k
  p = reg_name_given;
17011
8.85k
  while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
17012
5.93k
    {
17013
5.93k
      if (p >= reg_name_given + MAX_REG_NAME_SIZE)
17014
31
  return NULL;
17015
5.90k
      s++;
17016
5.90k
    }
17017
17018
2.92k
  if (is_part_of_name (*s))
17019
163
    return NULL;
17020
17021
2.75k
  *end_op = (char *) s;
17022
17023
2.75k
  r = str_hash_find (reg_hash, reg_name_given);
17024
17025
  /* Handle floating point regs, allowing spaces in the (i) part.  */
17026
2.75k
  if (r == reg_st0)
17027
95
    {
17028
95
      if (!cpu_arch_flags.bitfield.cpu8087
17029
13
    && !cpu_arch_flags.bitfield.cpu287
17030
13
    && !cpu_arch_flags.bitfield.cpu387
17031
0
    && !allow_pseudo_reg)
17032
0
  return NULL;
17033
17034
95
      if (is_whitespace (*s))
17035
54
  ++s;
17036
95
      if (*s == '(')
17037
0
  {
17038
0
    ++s;
17039
0
    if (is_whitespace (*s))
17040
0
      ++s;
17041
0
    if (*s >= '0' && *s <= '7')
17042
0
      {
17043
0
        int fpr = *s - '0';
17044
0
        ++s;
17045
0
        if (is_whitespace (*s))
17046
0
    ++s;
17047
0
        if (*s == ')')
17048
0
    {
17049
0
      *end_op = (char *) s + 1;
17050
0
      know (r[fpr].reg_num == fpr);
17051
0
      return r + fpr;
17052
0
    }
17053
0
      }
17054
    /* We have "%st(" then garbage.  */
17055
0
    return NULL;
17056
0
  }
17057
95
    }
17058
17059
2.75k
  return r && check_register (r) ? r : NULL;
17060
2.75k
}
17061
17062
/* REG_STRING starts *before* REGISTER_PREFIX.  */
17063
17064
static const reg_entry *
17065
parse_register (const char *reg_string, char **end_op)
17066
14.4k
{
17067
14.4k
  const reg_entry *r;
17068
17069
14.4k
  if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
17070
534
    r = parse_real_register (reg_string, end_op);
17071
13.9k
  else
17072
13.9k
    r = NULL;
17073
14.4k
  if (!r)
17074
14.1k
    {
17075
14.1k
      char *save = input_line_pointer;
17076
14.1k
      char *buf = xstrdup (reg_string), *name;
17077
14.1k
      symbolS *symbolP;
17078
14.1k
      offsetT off;
17079
17080
14.1k
      input_line_pointer = buf;
17081
14.1k
      get_symbol_name (&name);
17082
14.1k
      symbolP = symbol_find (name);
17083
14.1k
      symbolP = symbol_equated_to (symbolP, &off);
17084
14.1k
      if (symbolP && off == 0 && S_GET_SEGMENT (symbolP) == reg_section)
17085
137
  {
17086
137
    const expressionS *e = symbol_get_value_expression (symbolP);
17087
17088
137
    if (e->X_op == O_register)
17089
137
      {
17090
137
        know ((valueT) e->X_add_number < i386_regtab_size);
17091
137
        r = i386_regtab + e->X_add_number;
17092
137
        *end_op = (char *) reg_string + (input_line_pointer - buf);
17093
137
      }
17094
137
    if (r && !check_register (r))
17095
0
      {
17096
0
        as_bad (_("register '%s%s' cannot be used here"),
17097
0
          register_prefix, r->reg_name);
17098
0
        r = &bad_reg;
17099
0
      }
17100
137
  }
17101
14.1k
      input_line_pointer = save;
17102
14.1k
      free (buf);
17103
14.1k
    }
17104
14.4k
  return r;
17105
14.4k
}
17106
17107
int
17108
i386_parse_name (char *name,
17109
     expressionS *e,
17110
     enum expr_mode mode,
17111
     char *nextcharP)
17112
58.7k
{
17113
58.7k
  const reg_entry *r = NULL;
17114
58.7k
  char *end = input_line_pointer;
17115
17116
  /* We only know the terminating character here.  It being double quote could
17117
     be the closing one of a quoted symbol name, or an opening one from a
17118
     following string (or another quoted symbol name).  Since the latter can't
17119
     be valid syntax for anything, bailing in either case is good enough.  */
17120
58.7k
  if (*nextcharP == '"')
17121
992
    return 0;
17122
17123
57.7k
  *end = *nextcharP;
17124
57.7k
  if (*name == REGISTER_PREFIX || allow_naked_reg)
17125
300
    r = parse_real_register (name, &input_line_pointer);
17126
57.7k
  if (r && end <= input_line_pointer)
17127
22
    {
17128
22
      *nextcharP = *input_line_pointer;
17129
22
      *input_line_pointer = 0;
17130
22
      e->X_op = O_register;
17131
22
      e->X_add_number = r - i386_regtab;
17132
22
      return 1;
17133
22
    }
17134
57.6k
  input_line_pointer = end;
17135
57.6k
  *end = 0;
17136
57.6k
  return intel_syntax ? i386_intel_parse_name (name, e, mode) : 0;
17137
57.7k
}
17138
17139
void
17140
md_operand (expressionS *e)
17141
31.6k
{
17142
31.6k
  char *end;
17143
31.6k
  const reg_entry *r;
17144
17145
31.6k
  switch (*input_line_pointer)
17146
31.6k
    {
17147
2.11k
    case REGISTER_PREFIX:
17148
2.11k
      r = parse_real_register (input_line_pointer, &end);
17149
2.11k
      if (r)
17150
1.12k
  {
17151
1.12k
    e->X_op = O_register;
17152
1.12k
    e->X_add_number = r - i386_regtab;
17153
1.12k
    input_line_pointer = end;
17154
1.12k
  }
17155
2.11k
      break;
17156
17157
366
    case '[':
17158
366
      gas_assert (intel_syntax);
17159
366
      end = input_line_pointer++;
17160
366
      expression (e);
17161
366
      if (*input_line_pointer == ']')
17162
261
  {
17163
261
    ++input_line_pointer;
17164
261
    e->X_op_symbol = make_expr_symbol (e);
17165
261
    e->X_add_symbol = NULL;
17166
261
    e->X_add_number = 0;
17167
261
    e->X_op = O_index;
17168
261
  }
17169
105
      else
17170
105
  {
17171
105
    e->X_op = O_absent;
17172
105
    input_line_pointer = end;
17173
105
  }
17174
366
      break;
17175
31.6k
    }
17176
31.6k
}
17177
17178
#ifdef BFD64
17179
/* To maintain consistency with !BFD64 builds of gas record, whether any
17180
   (binary) operator was involved in an expression.  As expressions are
17181
   evaluated in only 32 bits when !BFD64, we use this to decide whether to
17182
   truncate results.  */
17183
bool i386_record_operator (operatorT op,
17184
         const expressionS *left,
17185
         const expressionS *right)
17186
73.0k
{
17187
73.0k
  if (op == O_absent)
17188
2.02k
    return false;
17189
17190
71.0k
  if (!left)
17191
26.1k
    {
17192
      /* Since the expression parser applies unary operators fine to bignum
17193
   operands, we don't need to be concerned of respective operands not
17194
   fitting in 32 bits.  */
17195
26.1k
      if (right->X_op == O_constant && right->X_unsigned
17196
8.59k
    && !fits_in_unsigned_long (right->X_add_number))
17197
1.52k
  return false;
17198
26.1k
    }
17199
  /* This isn't entirely right: The pattern can also result when constant
17200
     expressions are folded (e.g. 0xffffffff + 1).  */
17201
44.8k
  else if ((left->X_op == O_constant && left->X_unsigned
17202
9.50k
      && !fits_in_unsigned_long (left->X_add_number))
17203
44.1k
     || (right->X_op == O_constant && right->X_unsigned
17204
20.2k
         && !fits_in_unsigned_long (right->X_add_number)))
17205
2.54k
    expr_mode = expr_large_value;
17206
17207
69.4k
  if (expr_mode != expr_large_value)
17208
64.6k
    expr_mode = expr_operator_present;
17209
17210
69.4k
  return false;
17211
71.0k
}
17212
#endif
17213

17214
const char md_shortopts[] =
17215
#ifdef OBJ_ELF
17216
  "kVQ:"
17217
# ifdef TE_SOLARIS
17218
  "s"
17219
# endif
17220
#endif
17221
  "qnO::";
17222
17223
0
#define OPTION_32 (OPTION_MD_BASE + 0)
17224
0
#define OPTION_64 (OPTION_MD_BASE + 1)
17225
0
#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
17226
0
#define OPTION_MARCH (OPTION_MD_BASE + 3)
17227
0
#define OPTION_MTUNE (OPTION_MD_BASE + 4)
17228
0
#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
17229
0
#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
17230
0
#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
17231
0
#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
17232
0
#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
17233
0
#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
17234
0
#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
17235
0
#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
17236
0
#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
17237
0
#define OPTION_X32 (OPTION_MD_BASE + 14)
17238
0
#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
17239
0
#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
17240
0
#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
17241
#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
17242
0
#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
17243
0
#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
17244
0
#define OPTION_MSHARED (OPTION_MD_BASE + 21)
17245
0
#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
17246
0
#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
17247
0
#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
17248
0
#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
17249
0
#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
17250
0
#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
17251
0
#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
17252
0
#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
17253
0
#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
17254
0
#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
17255
0
#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
17256
0
#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
17257
0
#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
17258
0
#define OPTION_MTLS_CHECK (OPTION_MD_BASE + 35)
17259
17260
const struct option md_longopts[] =
17261
{
17262
  {"32", no_argument, NULL, OPTION_32},
17263
#if (defined (OBJ_ELF) || defined (TE_PE) || defined (OBJ_MACH_O)) \
17264
    && defined (BFD64)
17265
  {"64", no_argument, NULL, OPTION_64},
17266
#endif
17267
#ifdef OBJ_ELF
17268
# ifdef BFD64
17269
  {"x32", no_argument, NULL, OPTION_X32},
17270
# endif
17271
  {"mshared", no_argument, NULL, OPTION_MSHARED},
17272
  {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
17273
#endif
17274
  {"divide", no_argument, NULL, OPTION_DIVIDE},
17275
  {"march", required_argument, NULL, OPTION_MARCH},
17276
  {"mtune", required_argument, NULL, OPTION_MTUNE},
17277
  {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
17278
  {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
17279
  {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
17280
  {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
17281
  {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
17282
  {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
17283
  {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
17284
  {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
17285
  {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
17286
  {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
17287
  {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
17288
  {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
17289
  {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
17290
# if defined (TE_PE) || defined (TE_PEP)
17291
  {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
17292
#endif
17293
  {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
17294
  {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
17295
  {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
17296
  {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
17297
  {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
17298
  {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
17299
  {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
17300
  {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
17301
  {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
17302
  {"mlfence-before-indirect-branch", required_argument, NULL,
17303
   OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
17304
  {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
17305
  {"mamd64", no_argument, NULL, OPTION_MAMD64},
17306
  {"mintel64", no_argument, NULL, OPTION_MINTEL64},
17307
  {"mtls-check", required_argument, NULL, OPTION_MTLS_CHECK},
17308
  {NULL, no_argument, NULL, 0}
17309
};
17310
const size_t md_longopts_size = sizeof (md_longopts);
17311
17312
int
17313
md_parse_option (int c, const char *arg)
17314
0
{
17315
0
  unsigned int j;
17316
0
  char *arch, *next, *saved, *type;
17317
17318
0
  switch (c)
17319
0
    {
17320
0
    case 'n':
17321
0
      optimize_align_code = 0;
17322
0
      break;
17323
17324
0
    case 'q':
17325
0
      quiet_warnings = 1;
17326
0
      break;
17327
17328
0
#ifdef OBJ_ELF
17329
      /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
17330
   should be emitted or not.  FIXME: Not implemented.  */
17331
0
    case 'Q':
17332
0
      if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
17333
0
  return 0;
17334
0
      break;
17335
17336
      /* -V: SVR4 argument to print version ID.  */
17337
0
    case 'V':
17338
0
      print_version_id ();
17339
0
      break;
17340
17341
      /* -k: Ignore for FreeBSD compatibility.  */
17342
0
    case 'k':
17343
0
      break;
17344
17345
# ifdef TE_SOLARIS
17346
    case 's':
17347
      /* -s: On i386 Solaris, this tells the native assembler to use
17348
   .stab instead of .stab.excl.  We always use .stab anyhow.  */
17349
      break;
17350
# endif
17351
17352
0
    case OPTION_MSHARED:
17353
0
      shared = 1;
17354
0
      break;
17355
17356
0
    case OPTION_X86_USED_NOTE:
17357
0
      if (strcasecmp (arg, "yes") == 0)
17358
0
        x86_used_note = 1;
17359
0
      else if (strcasecmp (arg, "no") == 0)
17360
0
        x86_used_note = 0;
17361
0
      else
17362
0
        as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
17363
0
      break;
17364
0
#endif
17365
17366
0
#ifdef BFD64
17367
17368
0
#if (defined (OBJ_ELF) || defined (TE_PE) || defined (OBJ_MACH_O))
17369
0
    case OPTION_64:
17370
0
      {
17371
0
  const char **list, **l;
17372
17373
0
  list = bfd_target_list ();
17374
0
  for (l = list; *l != NULL; l++)
17375
0
#if defined (OBJ_ELF)
17376
0
    if (strcmp (*l, ELF_TARGET_FORMAT64) == 0)
17377
#elif defined (TE_PE)
17378
    if (strcmp (*l, "pe-x86-64") == 0)
17379
#else
17380
    if (strcmp (*l, "mach-o-x86-64") == 0)
17381
#endif
17382
0
      {
17383
0
        default_arch = "x86_64";
17384
0
        break;
17385
0
      }
17386
0
  if (*l == NULL)
17387
0
    as_fatal (_("no compiled in support for x86_64"));
17388
0
  free (list);
17389
0
      }
17390
0
      break;
17391
0
#endif
17392
17393
0
#ifdef OBJ_ELF
17394
0
    case OPTION_X32:
17395
0
      {
17396
0
  const char **list, **l;
17397
17398
0
  list = bfd_target_list ();
17399
0
  for (l = list; *l != NULL; l++)
17400
0
    if (strcmp (*l, ELF_TARGET_FORMAT32) == 0)
17401
0
      {
17402
0
        default_arch = "x86_64:32";
17403
0
        break;
17404
0
      }
17405
0
  if (*l == NULL)
17406
0
    as_fatal (_("no compiled in support for 32bit x86_64"));
17407
0
  free (list);
17408
0
      }
17409
0
      break;
17410
0
#endif
17411
17412
0
#endif /* BFD64 */
17413
17414
0
    case OPTION_32:
17415
0
      {
17416
0
  const char **list, **l;
17417
17418
0
  list = bfd_target_list ();
17419
0
  for (l = list; *l != NULL; l++)
17420
0
    if (strstr (*l, "-i386")
17421
0
        || strstr (*l, "-go32"))
17422
0
      {
17423
0
        default_arch = "i386";
17424
0
        break;
17425
0
      }
17426
0
  if (*l == NULL)
17427
0
    as_fatal (_("no compiled in support for ix86"));
17428
0
  free (list);
17429
0
      }
17430
0
      break;
17431
17432
0
    case OPTION_DIVIDE:
17433
#ifdef SVR4_COMMENT_CHARS
17434
      {
17435
  char *n, *t;
17436
  const char *s;
17437
17438
  n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
17439
  t = n;
17440
  for (s = i386_comment_chars; *s != '\0'; s++)
17441
    if (*s != '/')
17442
      *t++ = *s;
17443
  *t = '\0';
17444
  i386_comment_chars = n;
17445
      }
17446
#endif
17447
0
      break;
17448
17449
0
    case OPTION_MARCH:
17450
0
      saved = xstrdup (arg);
17451
0
      arch = saved;
17452
      /* Allow -march=+nosse.  */
17453
0
      if (*arch == '+')
17454
0
  arch++;
17455
0
      do
17456
0
  {
17457
0
    char *vsz;
17458
17459
0
    if (*arch == '.')
17460
0
      as_fatal (_("invalid -march= option: `%s'"), arg);
17461
0
    next = strchr (arch, '+');
17462
0
    if (next)
17463
0
      *next++ = '\0';
17464
0
    vsz = strchr (arch, '/');
17465
0
    if (vsz)
17466
0
      *vsz++ = '\0';
17467
0
    for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
17468
0
      {
17469
0
        if (vsz && cpu_arch[j].vsz != vsz_set)
17470
0
    continue;
17471
17472
0
        if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
17473
0
            && strcmp (arch, cpu_arch[j].name) == 0)
17474
0
    {
17475
      /* Processor.  */
17476
0
      if (! cpu_arch[j].enable.bitfield.cpui386)
17477
0
        continue;
17478
17479
0
      cpu_arch_name = cpu_arch[j].name;
17480
0
      free (cpu_sub_arch_name);
17481
0
      cpu_sub_arch_name = NULL;
17482
0
      cpu_arch_flags = cpu_arch[j].enable;
17483
0
      cpu_arch_isa = cpu_arch[j].type;
17484
0
      cpu_arch_isa_flags = cpu_arch[j].enable;
17485
0
      if (!cpu_arch_tune_set)
17486
0
        cpu_arch_tune = cpu_arch_isa;
17487
0
      vector_size = VSZ_DEFAULT;
17488
0
      break;
17489
0
    }
17490
0
        else if (cpu_arch[j].type == PROCESSOR_NONE
17491
0
           && strcmp (arch, cpu_arch[j].name) == 0
17492
0
           && !cpu_flags_all_zero (&cpu_arch[j].enable))
17493
0
    {
17494
      /* ISA extension.  */
17495
0
      isa_enable (j);
17496
17497
0
      switch (cpu_arch[j].vsz)
17498
0
        {
17499
0
        default:
17500
0
          break;
17501
17502
0
        case vsz_set:
17503
0
          if (vsz)
17504
0
      {
17505
0
        char *end;
17506
0
        unsigned long val = strtoul (vsz, &end, 0);
17507
17508
0
        if (*end)
17509
0
          val = 0;
17510
0
        switch (val)
17511
0
          {
17512
0
          case 512: vector_size = VSZ512; break;
17513
0
          case 256: vector_size = VSZ256; break;
17514
0
          case 128: vector_size = VSZ128; break;
17515
0
          default:
17516
0
            as_warn (_("Unrecognized vector size specifier ignored"));
17517
0
            break;
17518
0
          }
17519
0
        break;
17520
0
      }
17521
      /* Fall through.  */
17522
0
        case vsz_reset:
17523
0
          vector_size = VSZ_DEFAULT;
17524
0
          break;
17525
0
        }
17526
17527
0
      break;
17528
0
    }
17529
0
      }
17530
17531
0
    if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
17532
0
      {
17533
        /* Disable an ISA extension.  */
17534
0
        for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
17535
0
    if (cpu_arch[j].type == PROCESSOR_NONE
17536
0
        && strcmp (arch + 2, cpu_arch[j].name) == 0)
17537
0
      {
17538
0
        isa_disable (j);
17539
0
        if (cpu_arch[j].vsz == vsz_set)
17540
0
          vector_size = VSZ_DEFAULT;
17541
0
        break;
17542
0
      }
17543
0
      }
17544
17545
0
    if (j >= ARRAY_SIZE (cpu_arch))
17546
0
      as_fatal (_("invalid -march= option: `%s'"), arg);
17547
17548
0
    arch = next;
17549
0
  }
17550
0
      while (next != NULL);
17551
0
      free (saved);
17552
0
      break;
17553
17554
0
    case OPTION_MTUNE:
17555
0
      if (*arg == '.')
17556
0
  as_fatal (_("invalid -mtune= option: `%s'"), arg);
17557
0
      for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
17558
0
  {
17559
0
    if (cpu_arch[j].type != PROCESSOR_NONE
17560
0
        && strcmp (arg, cpu_arch[j].name) == 0)
17561
0
      {
17562
0
        cpu_arch_tune_set = 1;
17563
0
        cpu_arch_tune = cpu_arch [j].type;
17564
0
        break;
17565
0
      }
17566
0
  }
17567
0
      if (j >= ARRAY_SIZE (cpu_arch))
17568
0
  as_fatal (_("invalid -mtune= option: `%s'"), arg);
17569
0
      break;
17570
17571
0
    case OPTION_MMNEMONIC:
17572
0
      if (strcasecmp (arg, "att") == 0)
17573
0
  intel_mnemonic = 0;
17574
0
      else if (strcasecmp (arg, "intel") == 0)
17575
0
  intel_mnemonic = 1;
17576
0
      else
17577
0
  as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
17578
0
      break;
17579
17580
0
    case OPTION_MSYNTAX:
17581
0
      if (strcasecmp (arg, "att") == 0)
17582
0
  _set_intel_syntax (0);
17583
0
      else if (strcasecmp (arg, "intel") == 0)
17584
0
  _set_intel_syntax (1);
17585
0
      else
17586
0
  as_fatal (_("invalid -msyntax= option: `%s'"), arg);
17587
0
      break;
17588
17589
0
    case OPTION_MINDEX_REG:
17590
0
      allow_index_reg = 1;
17591
0
      break;
17592
17593
0
    case OPTION_MNAKED_REG:
17594
0
      allow_naked_reg = 1;
17595
0
      register_prefix = "";
17596
0
      break;
17597
17598
0
    case OPTION_MSSE2AVX:
17599
0
      sse2avx = 1;
17600
0
      break;
17601
17602
0
    case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
17603
0
      use_unaligned_vector_move = 1;
17604
0
      break;
17605
17606
0
    case OPTION_MSSE_CHECK:
17607
0
      if (strcasecmp (arg, "error") == 0)
17608
0
  sse_check = check_error;
17609
0
      else if (strcasecmp (arg, "warning") == 0)
17610
0
  sse_check = check_warning;
17611
0
      else if (strcasecmp (arg, "none") == 0)
17612
0
  sse_check = check_none;
17613
0
      else
17614
0
  as_fatal (_("invalid -msse-check= option: `%s'"), arg);
17615
0
      break;
17616
17617
0
    case OPTION_MOPERAND_CHECK:
17618
0
      if (strcasecmp (arg, "error") == 0)
17619
0
  operand_check = check_error;
17620
0
      else if (strcasecmp (arg, "warning") == 0)
17621
0
  operand_check = check_warning;
17622
0
      else if (strcasecmp (arg, "none") == 0)
17623
0
  operand_check = check_none;
17624
0
      else
17625
0
  as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
17626
0
      break;
17627
17628
0
    case OPTION_MAVXSCALAR:
17629
0
      if (strcasecmp (arg, "128") == 0)
17630
0
  avxscalar = vex128;
17631
0
      else if (strcasecmp (arg, "256") == 0)
17632
0
  avxscalar = vex256;
17633
0
      else
17634
0
  as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
17635
0
      break;
17636
17637
0
    case OPTION_MVEXWIG:
17638
0
      if (strcmp (arg, "0") == 0)
17639
0
  vexwig = vexw0;
17640
0
      else if (strcmp (arg, "1") == 0)
17641
0
  vexwig = vexw1;
17642
0
      else
17643
0
  as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
17644
0
      break;
17645
17646
0
    case OPTION_MADD_BND_PREFIX:
17647
0
      add_bnd_prefix = 1;
17648
0
      break;
17649
17650
0
    case OPTION_MEVEXLIG:
17651
0
      if (strcmp (arg, "128") == 0)
17652
0
  evexlig = evexl128;
17653
0
      else if (strcmp (arg, "256") == 0)
17654
0
  evexlig = evexl256;
17655
0
      else  if (strcmp (arg, "512") == 0)
17656
0
  evexlig = evexl512;
17657
0
      else
17658
0
  as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
17659
0
      break;
17660
17661
0
    case OPTION_MEVEXRCIG:
17662
0
      if (strcmp (arg, "rne") == 0)
17663
0
  evexrcig = rne;
17664
0
      else if (strcmp (arg, "rd") == 0)
17665
0
  evexrcig = rd;
17666
0
      else if (strcmp (arg, "ru") == 0)
17667
0
  evexrcig = ru;
17668
0
      else if (strcmp (arg, "rz") == 0)
17669
0
  evexrcig = rz;
17670
0
      else
17671
0
  as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
17672
0
      break;
17673
17674
0
    case OPTION_MEVEXWIG:
17675
0
      if (strcmp (arg, "0") == 0)
17676
0
  evexwig = evexw0;
17677
0
      else if (strcmp (arg, "1") == 0)
17678
0
  evexwig = evexw1;
17679
0
      else
17680
0
  as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
17681
0
      break;
17682
17683
# if defined (TE_PE) || defined (TE_PEP)
17684
    case OPTION_MBIG_OBJ:
17685
      use_big_obj = 1;
17686
      break;
17687
#endif
17688
17689
0
    case OPTION_MOMIT_LOCK_PREFIX:
17690
0
      if (strcasecmp (arg, "yes") == 0)
17691
0
        omit_lock_prefix = 1;
17692
0
      else if (strcasecmp (arg, "no") == 0)
17693
0
        omit_lock_prefix = 0;
17694
0
      else
17695
0
        as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
17696
0
      break;
17697
17698
0
    case OPTION_MFENCE_AS_LOCK_ADD:
17699
0
      if (strcasecmp (arg, "yes") == 0)
17700
0
        avoid_fence = 1;
17701
0
      else if (strcasecmp (arg, "no") == 0)
17702
0
        avoid_fence = 0;
17703
0
      else
17704
0
        as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
17705
0
      break;
17706
17707
0
    case OPTION_MLFENCE_AFTER_LOAD:
17708
0
      if (strcasecmp (arg, "yes") == 0)
17709
0
  lfence_after_load = 1;
17710
0
      else if (strcasecmp (arg, "no") == 0)
17711
0
  lfence_after_load = 0;
17712
0
      else
17713
0
        as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
17714
0
      break;
17715
17716
0
    case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
17717
0
      if (strcasecmp (arg, "all") == 0)
17718
0
  {
17719
0
    lfence_before_indirect_branch = lfence_branch_all;
17720
0
    if (lfence_before_ret == lfence_before_ret_none)
17721
0
      lfence_before_ret = lfence_before_ret_shl;
17722
0
  }
17723
0
      else if (strcasecmp (arg, "memory") == 0)
17724
0
  lfence_before_indirect_branch = lfence_branch_memory;
17725
0
      else if (strcasecmp (arg, "register") == 0)
17726
0
  lfence_before_indirect_branch = lfence_branch_register;
17727
0
      else if (strcasecmp (arg, "none") == 0)
17728
0
  lfence_before_indirect_branch = lfence_branch_none;
17729
0
      else
17730
0
        as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
17731
0
      arg);
17732
0
      break;
17733
17734
0
    case OPTION_MLFENCE_BEFORE_RET:
17735
0
      if (strcasecmp (arg, "or") == 0)
17736
0
  lfence_before_ret = lfence_before_ret_or;
17737
0
      else if (strcasecmp (arg, "not") == 0)
17738
0
  lfence_before_ret = lfence_before_ret_not;
17739
0
      else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
17740
0
  lfence_before_ret = lfence_before_ret_shl;
17741
0
      else if (strcasecmp (arg, "none") == 0)
17742
0
  lfence_before_ret = lfence_before_ret_none;
17743
0
      else
17744
0
        as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
17745
0
      arg);
17746
0
      break;
17747
17748
0
    case OPTION_MRELAX_RELOCATIONS:
17749
0
      if (strcasecmp (arg, "yes") == 0)
17750
0
        generate_relax_relocations = 1;
17751
0
      else if (strcasecmp (arg, "no") == 0)
17752
0
        generate_relax_relocations = 0;
17753
0
      else
17754
0
        as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
17755
0
      break;
17756
17757
0
    case OPTION_MALIGN_BRANCH_BOUNDARY:
17758
0
      {
17759
0
  char *end;
17760
0
  long int align = strtoul (arg, &end, 0);
17761
0
  if (*end == '\0')
17762
0
    {
17763
0
      if (align == 0)
17764
0
        {
17765
0
    align_branch_power = 0;
17766
0
    break;
17767
0
        }
17768
0
      else if (align >= 16)
17769
0
        {
17770
0
    int align_power;
17771
0
    for (align_power = 0;
17772
0
         (align & 1) == 0;
17773
0
         align >>= 1, align_power++)
17774
0
      continue;
17775
    /* Limit alignment power to 31.  */
17776
0
    if (align == 1 && align_power < 32)
17777
0
      {
17778
0
        align_branch_power = align_power;
17779
0
        break;
17780
0
      }
17781
0
        }
17782
0
    }
17783
0
  as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
17784
0
      }
17785
0
      break;
17786
17787
0
    case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
17788
0
      {
17789
0
  char *end;
17790
0
  int align = strtoul (arg, &end, 0);
17791
  /* Some processors only support 5 prefixes.  */
17792
0
  if (*end == '\0' && align >= 0 && align < 6)
17793
0
    {
17794
0
      align_branch_prefix_size = align;
17795
0
      break;
17796
0
    }
17797
0
  as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
17798
0
      arg);
17799
0
      }
17800
0
      break;
17801
17802
0
    case OPTION_MALIGN_BRANCH:
17803
0
      align_branch = 0;
17804
0
      saved = xstrdup (arg);
17805
0
      type = saved;
17806
0
      do
17807
0
  {
17808
0
    next = strchr (type, '+');
17809
0
    if (next)
17810
0
      *next++ = '\0';
17811
0
    if (strcasecmp (type, "jcc") == 0)
17812
0
      align_branch |= align_branch_jcc_bit;
17813
0
    else if (strcasecmp (type, "fused") == 0)
17814
0
      align_branch |= align_branch_fused_bit;
17815
0
    else if (strcasecmp (type, "jmp") == 0)
17816
0
      align_branch |= align_branch_jmp_bit;
17817
0
    else if (strcasecmp (type, "call") == 0)
17818
0
      align_branch |= align_branch_call_bit;
17819
0
    else if (strcasecmp (type, "ret") == 0)
17820
0
      align_branch |= align_branch_ret_bit;
17821
0
    else if (strcasecmp (type, "indirect") == 0)
17822
0
      align_branch |= align_branch_indirect_bit;
17823
0
    else
17824
0
      as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
17825
0
    type = next;
17826
0
  }
17827
0
      while (next != NULL);
17828
0
      free (saved);
17829
0
      break;
17830
17831
0
    case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
17832
0
      align_branch_power = 5;
17833
0
      align_branch_prefix_size = 5;
17834
0
      align_branch = (align_branch_jcc_bit
17835
0
          | align_branch_fused_bit
17836
0
          | align_branch_jmp_bit);
17837
0
      break;
17838
17839
0
    case OPTION_MAMD64:
17840
0
      isa64 = amd64;
17841
0
      break;
17842
17843
0
    case OPTION_MINTEL64:
17844
0
      isa64 = intel64;
17845
0
      break;
17846
17847
0
    case 'O':
17848
0
      if (arg == NULL)
17849
0
  {
17850
0
    optimize = 1;
17851
    /* Turn off -Os.  */
17852
0
    optimize_for_space = 0;
17853
0
  }
17854
0
      else if (*arg == 's')
17855
0
  {
17856
0
    optimize_for_space = 1;
17857
    /* Turn on all encoding optimizations.  */
17858
0
    optimize = INT_MAX;
17859
0
  }
17860
0
      else
17861
0
  {
17862
0
    optimize = atoi (arg);
17863
    /* Turn off -Os.  */
17864
0
    optimize_for_space = 0;
17865
0
  }
17866
0
      break;
17867
0
    case OPTION_MTLS_CHECK:
17868
0
      if (strcasecmp (arg, "yes") == 0)
17869
0
  tls_check = true;
17870
0
      else if (strcasecmp (arg, "no") == 0)
17871
0
  tls_check = false;
17872
0
      else
17873
0
  as_fatal (_("invalid -mtls-check= option: `%s'"), arg);
17874
0
      break;
17875
17876
0
    default:
17877
0
      return 0;
17878
0
    }
17879
0
  return 1;
17880
0
}
17881
17882
0
#define MESSAGE_TEMPLATE \
17883
0
"                                                                                "
17884
17885
static char *
17886
output_message (FILE *stream, char *p, char *message, char *start,
17887
    int *left_p, const char *name, int len)
17888
0
{
17889
0
  int size = sizeof (MESSAGE_TEMPLATE);
17890
0
  int left = *left_p;
17891
17892
  /* Reserve 2 spaces for ", " or ",\0" */
17893
0
  left -= len + 2;
17894
17895
  /* Check if there is any room.  */
17896
0
  if (left >= 0)
17897
0
    {
17898
0
      if (p != start)
17899
0
  {
17900
0
    *p++ = ',';
17901
0
    *p++ = ' ';
17902
0
  }
17903
0
      p = mempcpy (p, name, len);
17904
0
    }
17905
0
  else
17906
0
    {
17907
      /* Output the current message now and start a new one.  */
17908
0
      *p++ = ',';
17909
0
      *p = '\0';
17910
0
      fprintf (stream, "%s\n", message);
17911
0
      p = start;
17912
0
      left = size - (start - message) - len - 2;
17913
17914
0
      gas_assert (left >= 0);
17915
17916
0
      p = mempcpy (p, name, len);
17917
0
    }
17918
17919
0
  *left_p = left;
17920
0
  return p;
17921
0
}
17922
17923
static void
17924
show_arch (FILE *stream, int ext, int check)
17925
0
{
17926
0
  static char message[] = MESSAGE_TEMPLATE;
17927
0
  char *start = message + 27;
17928
0
  char *p;
17929
0
  int size = sizeof (MESSAGE_TEMPLATE);
17930
0
  int left;
17931
0
  const char *name;
17932
0
  int len;
17933
0
  unsigned int j;
17934
17935
0
  p = start;
17936
0
  left = size - (start - message);
17937
17938
0
  if (!ext && check)
17939
0
    {
17940
0
      p = output_message (stream, p, message, start, &left,
17941
0
        STRING_COMMA_LEN ("default"));
17942
0
      p = output_message (stream, p, message, start, &left,
17943
0
        STRING_COMMA_LEN ("push"));
17944
0
      p = output_message (stream, p, message, start, &left,
17945
0
        STRING_COMMA_LEN ("pop"));
17946
0
    }
17947
17948
0
  for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
17949
0
    {
17950
      /* Should it be skipped?  */
17951
0
      if (cpu_arch [j].skip)
17952
0
  continue;
17953
17954
0
      name = cpu_arch [j].name;
17955
0
      len = cpu_arch [j].len;
17956
0
      if (cpu_arch[j].type == PROCESSOR_NONE)
17957
0
  {
17958
    /* It is an extension.  Skip if we aren't asked to show it.  */
17959
0
    if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
17960
0
      continue;
17961
0
  }
17962
0
      else if (ext)
17963
0
  {
17964
    /* It is an processor.  Skip if we show only extension.  */
17965
0
    continue;
17966
0
  }
17967
0
      else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
17968
0
  {
17969
    /* It is an impossible processor - skip.  */
17970
0
    continue;
17971
0
  }
17972
17973
0
      p = output_message (stream, p, message, start, &left, name, len);
17974
0
    }
17975
17976
  /* Display disabled extensions.  */
17977
0
  if (ext)
17978
0
    for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
17979
0
      {
17980
0
  char *str;
17981
17982
0
  if (cpu_arch[j].type != PROCESSOR_NONE
17983
0
      || !cpu_flags_all_zero (&cpu_arch[j].enable))
17984
0
    continue;
17985
0
  str = xasprintf ("no%s", cpu_arch[j].name);
17986
0
  p = output_message (stream, p, message, start, &left, str,
17987
0
          strlen (str));
17988
0
  free (str);
17989
0
      }
17990
17991
0
  *p = '\0';
17992
0
  fprintf (stream, "%s\n", message);
17993
0
}
17994
17995
void
17996
md_show_usage (FILE *stream)
17997
0
{
17998
0
#ifdef OBJ_ELF
17999
0
  fprintf (stream, _("\
18000
0
  -Qy, -Qn                ignored\n\
18001
0
  -V                      print assembler version number\n\
18002
0
  -k                      ignored\n"));
18003
0
#endif
18004
0
  fprintf (stream, _("\
18005
0
  -n                      do not optimize code alignment\n\
18006
0
  -O{012s}                attempt some code optimizations\n\
18007
0
  -q                      quieten some warnings\n"));
18008
0
#ifdef OBJ_ELF
18009
0
  fprintf (stream, _("\
18010
0
  -s                      ignored\n"));
18011
0
#endif
18012
0
#ifdef BFD64
18013
0
# ifdef OBJ_ELF
18014
0
  fprintf (stream, _("\
18015
0
  --32/--64/--x32         generate 32bit/64bit/x32 object\n"));
18016
# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
18017
  fprintf (stream, _("\
18018
  --32/--64               generate 32bit/64bit object\n"));
18019
# endif
18020
0
#endif
18021
#ifdef SVR4_COMMENT_CHARS
18022
  fprintf (stream, _("\
18023
  --divide                do not treat `/' as a comment character\n"));
18024
#else
18025
0
  fprintf (stream, _("\
18026
0
  --divide                ignored\n"));
18027
0
#endif
18028
0
  fprintf (stream, _("\
18029
0
  -march=CPU[,+EXTENSION...]\n\
18030
0
                          generate code for CPU and EXTENSION, CPU is one of:\n"));
18031
0
  show_arch (stream, 0, 1);
18032
0
  fprintf (stream, _("\
18033
0
                          EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
18034
0
  show_arch (stream, 1, 0);
18035
0
  fprintf (stream, _("\
18036
0
  -mtune=CPU              optimize for CPU, CPU is one of:\n"));
18037
0
  show_arch (stream, 0, 0);
18038
0
  fprintf (stream, _("\
18039
0
  -msse2avx               encode SSE instructions with VEX prefix\n"));
18040
0
  fprintf (stream, _("\
18041
0
  -muse-unaligned-vector-move\n\
18042
0
                          encode aligned vector move as unaligned vector move\n"));
18043
0
  fprintf (stream, _("\
18044
0
  -msse-check=[none|error|warning] (default: none)\n\
18045
0
                          check SSE instructions\n"));
18046
0
  fprintf (stream, _("\
18047
0
  -moperand-check=[none|error|warning] (default: warning)\n\
18048
0
                          check operand combinations for validity\n"));
18049
0
  fprintf (stream, _("\
18050
0
  -mavxscalar=[128|256] (default: 128)\n\
18051
0
                          encode scalar AVX instructions with specific vector\n\
18052
0
                           length\n"));
18053
0
  fprintf (stream, _("\
18054
0
  -mvexwig=[0|1] (default: 0)\n\
18055
0
                          encode VEX instructions with specific VEX.W value\n\
18056
0
                           for VEX.W bit ignored instructions\n"));
18057
0
  fprintf (stream, _("\
18058
0
  -mevexlig=[128|256|512] (default: 128)\n\
18059
0
                          encode scalar EVEX instructions with specific vector\n\
18060
0
                           length\n"));
18061
0
  fprintf (stream, _("\
18062
0
  -mevexwig=[0|1] (default: 0)\n\
18063
0
                          encode EVEX instructions with specific EVEX.W value\n\
18064
0
                           for EVEX.W bit ignored instructions\n"));
18065
0
  fprintf (stream, _("\
18066
0
  -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
18067
0
                          encode EVEX instructions with specific EVEX.RC value\n\
18068
0
                           for SAE-only ignored instructions\n"));
18069
0
  fprintf (stream, _("\
18070
0
  -mmnemonic=[att|intel] "));
18071
0
  if (SYSV386_COMPAT)
18072
0
    fprintf (stream, _("(default: att)\n"));
18073
0
  else
18074
0
    fprintf (stream, _("(default: intel)\n"));
18075
0
  fprintf (stream, _("\
18076
0
                          use AT&T/Intel mnemonic (AT&T syntax only)\n"));
18077
0
  fprintf (stream, _("\
18078
0
  -msyntax=[att|intel] (default: att)\n\
18079
0
                          use AT&T/Intel syntax\n"));
18080
0
  fprintf (stream, _("\
18081
0
  -mindex-reg             support pseudo index registers\n"));
18082
0
  fprintf (stream, _("\
18083
0
  -mnaked-reg             don't require `%%' prefix for registers\n"));
18084
0
  fprintf (stream, _("\
18085
0
  -madd-bnd-prefix        add BND prefix for all valid branches\n"));
18086
0
#ifdef OBJ_ELF
18087
0
  fprintf (stream, _("\
18088
0
  -mshared                disable branch optimization for shared code\n"));
18089
0
  fprintf (stream, _("\
18090
0
  -mx86-used-note=[no|yes] "));
18091
0
  if (DEFAULT_X86_USED_NOTE)
18092
0
    fprintf (stream, _("(default: yes)\n"));
18093
0
  else
18094
0
    fprintf (stream, _("(default: no)\n"));
18095
0
  fprintf (stream, _("\
18096
0
                          generate x86 used ISA and feature properties\n"));
18097
0
#endif
18098
#if defined (TE_PE) || defined (TE_PEP)
18099
  fprintf (stream, _("\
18100
  -mbig-obj               generate big object files\n"));
18101
#endif
18102
0
  fprintf (stream, _("\
18103
0
  -momit-lock-prefix=[no|yes] (default: no)\n\
18104
0
                          strip all lock prefixes\n"));
18105
0
  fprintf (stream, _("\
18106
0
  -mfence-as-lock-add=[no|yes] (default: no)\n\
18107
0
                          encode lfence, mfence and sfence as\n\
18108
0
                           lock addl $0x0, (%%{re}sp)\n"));
18109
0
  fprintf (stream, _("\
18110
0
  -mrelax-relocations=[no|yes] "));
18111
0
  if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
18112
0
    fprintf (stream, _("(default: yes)\n"));
18113
0
  else
18114
0
    fprintf (stream, _("(default: no)\n"));
18115
0
  fprintf (stream, _("\
18116
0
                          generate relax relocations\n"));
18117
0
#ifdef OBJ_ELF
18118
0
  fprintf (stream, _("\
18119
0
  -mtls-check=[no|yes] "));
18120
0
  if (DEFAULT_X86_TLS_CHECK)
18121
0
    fprintf (stream, _("(default: yes)\n"));
18122
0
  else
18123
0
    fprintf (stream, _("(default: no)\n"));
18124
0
  fprintf (stream, _("\
18125
0
                          check TLS relocation\n"));
18126
0
#endif
18127
0
  fprintf (stream, _("\
18128
0
  -malign-branch-boundary=NUM (default: 0)\n\
18129
0
                          align branches within NUM byte boundary\n"));
18130
0
  fprintf (stream, _("\
18131
0
  -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
18132
0
                          TYPE is combination of jcc, fused, jmp, call, ret,\n\
18133
0
                           indirect\n\
18134
0
                          specify types of branches to align\n"));
18135
0
  fprintf (stream, _("\
18136
0
  -malign-branch-prefix-size=NUM (default: 5)\n\
18137
0
                          align branches with NUM prefixes per instruction\n"));
18138
0
  fprintf (stream, _("\
18139
0
  -mbranches-within-32B-boundaries\n\
18140
0
                          align branches within 32 byte boundary\n"));
18141
0
  fprintf (stream, _("\
18142
0
  -mlfence-after-load=[no|yes] (default: no)\n\
18143
0
                          generate lfence after load\n"));
18144
0
  fprintf (stream, _("\
18145
0
  -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
18146
0
                          generate lfence before indirect near branch\n"));
18147
0
  fprintf (stream, _("\
18148
0
  -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
18149
0
                          generate lfence before ret\n"));
18150
0
  fprintf (stream, _("\
18151
0
  -mamd64                 accept only AMD64 ISA [default]\n"));
18152
0
  fprintf (stream, _("\
18153
0
  -mintel64               accept only Intel64 ISA\n"));
18154
0
}
18155
18156
#if (defined (OBJ_ELF) || defined (TE_PE) || defined (OBJ_MACH_O))
18157
18158
/* Pick the target format to use.  */
18159
18160
const char *
18161
i386_target_format (void)
18162
478
{
18163
478
  if (startswith (default_arch, "x86_64"))
18164
478
    {
18165
478
      update_code_flag (CODE_64BIT, 1);
18166
478
#ifdef OBJ_ELF
18167
478
      if (default_arch[6] == '\0')
18168
478
  x86_elf_abi = X86_64_ABI;
18169
0
      else
18170
0
  x86_elf_abi = X86_64_X32_ABI;
18171
478
#endif
18172
478
    }
18173
0
  else if (!strcmp (default_arch, "i386"))
18174
0
    update_code_flag (CODE_32BIT, 1);
18175
0
  else if (!strcmp (default_arch, "iamcu"))
18176
0
    {
18177
0
      update_code_flag (CODE_32BIT, 1);
18178
0
      if (cpu_arch_isa == PROCESSOR_UNKNOWN)
18179
0
  {
18180
0
    static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
18181
0
    cpu_arch_name = "iamcu";
18182
0
    free (cpu_sub_arch_name);
18183
0
    cpu_sub_arch_name = NULL;
18184
0
    cpu_arch_flags = iamcu_flags;
18185
0
    cpu_arch_isa = PROCESSOR_IAMCU;
18186
0
    cpu_arch_isa_flags = iamcu_flags;
18187
0
    if (!cpu_arch_tune_set)
18188
0
      cpu_arch_tune = PROCESSOR_IAMCU;
18189
0
  }
18190
0
      else if (cpu_arch_isa != PROCESSOR_IAMCU)
18191
0
  as_fatal (_("Intel MCU doesn't support `%s' architecture"),
18192
0
      cpu_arch_name);
18193
0
    }
18194
0
  else
18195
0
    as_fatal (_("unknown architecture"));
18196
18197
478
#ifdef OBJ_ELF
18198
478
  if (flag_synth_cfi && x86_elf_abi != X86_64_ABI)
18199
0
    as_fatal (_("SCFI is not supported for this ABI"));
18200
478
#endif
18201
18202
478
  if (cpu_flags_all_zero (&cpu_arch_isa_flags))
18203
1
    cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
18204
18205
478
  switch (OUTPUT_FLAVOR)
18206
478
    {
18207
#ifdef TE_PE
18208
    case bfd_target_coff_flavour:
18209
      if (flag_code == CODE_64BIT)
18210
  {
18211
    object_64bit = 1;
18212
    return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
18213
  }
18214
      return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
18215
#endif
18216
0
#ifdef OBJ_ELF
18217
478
    case bfd_target_elf_flavour:
18218
478
      {
18219
478
  const char *format;
18220
18221
478
  switch (x86_elf_abi)
18222
478
    {
18223
0
    default:
18224
0
      format = ELF_TARGET_FORMAT;
18225
0
#ifndef TE_SOLARIS
18226
0
      tls_get_addr = "___tls_get_addr";
18227
0
#endif
18228
0
      break;
18229
478
    case X86_64_ABI:
18230
478
      use_rela_relocations = 1;
18231
478
      object_64bit = 1;
18232
478
#ifndef TE_SOLARIS
18233
478
      tls_get_addr = "__tls_get_addr";
18234
478
#endif
18235
478
      format = ELF_TARGET_FORMAT64;
18236
478
      break;
18237
0
    case X86_64_X32_ABI:
18238
0
      use_rela_relocations = 1;
18239
0
      object_64bit = 1;
18240
0
#ifndef TE_SOLARIS
18241
0
      tls_get_addr = "__tls_get_addr";
18242
0
#endif
18243
0
      disallow_64bit_reloc = 1;
18244
0
      format = ELF_TARGET_FORMAT32;
18245
0
      break;
18246
478
    }
18247
478
  if (cpu_arch_isa == PROCESSOR_IAMCU)
18248
0
    {
18249
0
      if (x86_elf_abi != I386_ABI)
18250
0
        as_fatal (_("Intel MCU is 32bit only"));
18251
0
      return ELF_TARGET_IAMCU_FORMAT;
18252
0
    }
18253
478
  else
18254
478
    return format;
18255
478
      }
18256
0
#endif
18257
#if defined (OBJ_MACH_O)
18258
    case bfd_target_mach_o_flavour:
18259
      if (flag_code == CODE_64BIT)
18260
  {
18261
    use_rela_relocations = 1;
18262
    object_64bit = 1;
18263
    return "mach-o-x86-64";
18264
  }
18265
      else
18266
  return "mach-o-i386";
18267
#endif
18268
0
    default:
18269
0
      abort ();
18270
0
      return NULL;
18271
478
    }
18272
478
}
18273
18274
#endif /* ELF / PE / MACH_O  */
18275

18276
#ifdef OBJ_ELF
18277
symbolS *
18278
md_undefined_symbol (char *name)
18279
3.13k
{
18280
3.13k
  if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
18281
48
      && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
18282
14
      && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
18283
14
      && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
18284
14
    {
18285
14
      if (!GOT_symbol)
18286
14
  {
18287
14
    if (symbol_find (name))
18288
0
      as_bad (_("GOT already in symbol table"));
18289
14
    GOT_symbol = symbol_new (name, undefined_section,
18290
14
           &zero_address_frag, 0);
18291
14
  };
18292
14
      return GOT_symbol;
18293
14
    }
18294
3.11k
  return NULL;
18295
3.13k
}
18296
#endif
18297
18298
#ifdef OBJ_AOUT
18299
/* Round up a section size to the appropriate boundary.  */
18300
18301
valueT
18302
md_section_align (segT segment, valueT size)
18303
{
18304
  /* For a.out, force the section size to be aligned.  If we don't do
18305
     this, BFD will align it for us, but it will not write out the
18306
     final bytes of the section.  This may be a bug in BFD, but it is
18307
     easier to fix it here since that is how the other a.out targets
18308
     work.  */
18309
  int align = bfd_section_alignment (segment);
18310
18311
  return (size + ((valueT) 1 << align) - 1) & -((valueT) 1 << align);
18312
}
18313
#endif
18314
18315
/* On the i386, PC-relative offsets are relative to the start of the
18316
   next instruction.  That is, the address of the offset, plus its
18317
   size, since the offset is always the last part of the insn.  */
18318
18319
long
18320
md_pcrel_from (fixS *fixP)
18321
0
{
18322
0
  return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
18323
0
}
18324
18325
#ifdef OBJ_AOUT
18326
18327
static void
18328
s_bss (int ignore ATTRIBUTE_UNUSED)
18329
{
18330
  int temp;
18331
18332
  temp = get_absolute_expression ();
18333
  subseg_set (bss_section, temp);
18334
  demand_empty_rest_of_line ();
18335
}
18336
18337
#endif
18338
18339
/* Remember constant directive.  */
18340
18341
void
18342
i386_cons_align (int ignore ATTRIBUTE_UNUSED)
18343
10.4k
{
18344
10.4k
  struct last_insn *last_insn
18345
10.4k
    = &seg_info(now_seg)->tc_segment_info_data.last_insn;
18346
18347
10.4k
  if (bfd_section_flags (now_seg) & SEC_CODE)
18348
9.57k
    {
18349
9.57k
      last_insn->kind = last_insn_directive;
18350
9.57k
      last_insn->name = "constant directive";
18351
9.57k
      last_insn->file = as_where (&last_insn->line);
18352
9.57k
    }
18353
10.4k
}
18354
18355
int
18356
i386_validate_fix (fixS *fixp)
18357
0
{
18358
0
  if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
18359
0
    {
18360
0
      reloc_howto_type *howto;
18361
18362
0
      howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
18363
0
      as_bad_where (fixp->fx_file, fixp->fx_line,
18364
0
        _("invalid %s relocation against register"),
18365
0
        howto ? howto->name : "<unknown>");
18366
0
      return 0;
18367
0
    }
18368
18369
0
#ifdef OBJ_ELF
18370
0
  if (fixp->fx_r_type == BFD_RELOC_SIZE32
18371
0
      || fixp->fx_r_type == BFD_RELOC_SIZE64)
18372
0
    return fixp->fx_addsy
18373
0
     && (!S_IS_DEFINED (fixp->fx_addsy)
18374
0
         || S_IS_EXTERNAL (fixp->fx_addsy));
18375
18376
  /* BFD_RELOC_X86_64_GOTTPOFF:
18377
      1. fx_tcbit -> BFD_RELOC_X86_64_CODE_4_GOTTPOFF
18378
      2. fx_tcbit2 -> BFD_RELOC_X86_64_CODE_5_GOTTPOFF
18379
      3. fx_tcbit3 -> BFD_RELOC_X86_64_CODE_6_GOTTPOFF
18380
    BFD_RELOC_X86_64_GOTPC32_TLSDESC:
18381
      1. fx_tcbit -> BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC
18382
    BFD_RELOC_32_PCREL:
18383
      1. fx_tcbit && fx_tcbit3 -> BFD_RELOC_X86_64_CODE_5_GOTPCRELX
18384
      2. fx_tcbit -> BFD_RELOC_X86_64_GOTPCRELX
18385
      3. fx_tcbit2 && fx_tcbit3 -> BFD_RELOC_X86_64_CODE_6_GOTPCRELX
18386
      4. fx_tcbit2 -> BFD_RELOC_X86_64_REX_GOTPCRELX
18387
      5. fx_tcbit3 -> BFD_RELOC_X86_64_CODE_4_GOTPCRELX
18388
      6. else -> BFD_RELOC_X86_64_GOTPCREL
18389
   */
18390
0
  if (fixp->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF)
18391
0
    {
18392
0
      if (fixp->fx_tcbit)
18393
0
  fixp->fx_r_type = BFD_RELOC_X86_64_CODE_4_GOTTPOFF;
18394
0
      else if (fixp->fx_tcbit2)
18395
0
  fixp->fx_r_type = BFD_RELOC_X86_64_CODE_5_GOTTPOFF;
18396
0
      else if (fixp->fx_tcbit3)
18397
0
  fixp->fx_r_type = BFD_RELOC_X86_64_CODE_6_GOTTPOFF;
18398
0
    }
18399
0
  else if (fixp->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
18400
0
     && fixp->fx_tcbit)
18401
0
    fixp->fx_r_type = BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC;
18402
0
#endif
18403
18404
0
  if (fixp->fx_subsy)
18405
0
    {
18406
0
      if (fixp->fx_subsy == GOT_symbol)
18407
0
  {
18408
0
    if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
18409
0
      {
18410
0
        if (!object_64bit)
18411
0
    abort ();
18412
0
#ifdef OBJ_ELF
18413
0
        if (fixp->fx_tcbit)
18414
0
    fixp->fx_r_type = fixp->fx_tcbit3
18415
0
          ? BFD_RELOC_X86_64_CODE_5_GOTPCRELX
18416
0
          : BFD_RELOC_X86_64_GOTPCRELX;
18417
0
        else if (fixp->fx_tcbit2)
18418
0
    fixp->fx_r_type = fixp->fx_tcbit3
18419
0
          ? BFD_RELOC_X86_64_CODE_6_GOTPCRELX
18420
0
          : BFD_RELOC_X86_64_REX_GOTPCRELX;
18421
0
        else if (fixp->fx_tcbit3)
18422
0
    fixp->fx_r_type = BFD_RELOC_X86_64_CODE_4_GOTPCRELX;
18423
0
        else
18424
0
#endif
18425
0
    fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
18426
0
      }
18427
0
    else
18428
0
      {
18429
0
        if (!object_64bit)
18430
0
    fixp->fx_r_type = BFD_RELOC_32_GOTOFF;
18431
0
        else
18432
0
    fixp->fx_r_type = BFD_RELOC_64_GOTOFF;
18433
0
      }
18434
0
    fixp->fx_subsy = 0;
18435
0
  }
18436
0
    }
18437
0
#ifdef OBJ_ELF
18438
0
  else
18439
0
    {
18440
      /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
18441
   to section.  Since PLT32 relocation must be against symbols,
18442
   turn such PLT32 relocation into PC32 relocation.  NB: We can
18443
   turn PLT32 relocation into PC32 relocation only for PC-relative
18444
   relocations since non-PC-relative relocations need PLT entries.
18445
       */
18446
0
      if (fixp->fx_addsy
18447
0
    && fixp->fx_pcrel
18448
0
    && (fixp->fx_r_type == BFD_RELOC_386_PLT32
18449
0
        || fixp->fx_r_type == BFD_RELOC_32_PLT_PCREL)
18450
0
    && symbol_section_p (fixp->fx_addsy))
18451
0
  fixp->fx_r_type = BFD_RELOC_32_PCREL;
18452
0
      if (!object_64bit)
18453
0
  {
18454
0
    if (fixp->fx_r_type == BFD_RELOC_386_GOT32
18455
0
        && fixp->fx_tcbit2)
18456
0
      fixp->fx_r_type = BFD_RELOC_386_GOT32X;
18457
0
  }
18458
0
    }
18459
0
#endif
18460
18461
0
  return 1;
18462
0
}
18463
18464
arelent *
18465
tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
18466
0
{
18467
0
  arelent *rel;
18468
0
  bfd_reloc_code_real_type code;
18469
18470
0
  switch (fixp->fx_r_type)
18471
0
    {
18472
0
#ifdef OBJ_ELF
18473
0
      symbolS *sym;
18474
18475
0
    case BFD_RELOC_SIZE32:
18476
0
    case BFD_RELOC_SIZE64:
18477
0
      if (fixp->fx_addsy
18478
0
    && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
18479
0
    && (!fixp->fx_subsy
18480
0
        || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
18481
0
  sym = fixp->fx_addsy;
18482
0
      else if (fixp->fx_subsy
18483
0
         && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
18484
0
         && (!fixp->fx_addsy
18485
0
       || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
18486
0
  sym = fixp->fx_subsy;
18487
0
      else
18488
0
  sym = NULL;
18489
0
      if (sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
18490
0
  {
18491
    /* Resolve size relocation against local symbol to size of
18492
       the symbol plus addend.  */
18493
0
    valueT value = S_GET_SIZE (sym);
18494
18495
0
    if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
18496
0
      value = bfd_section_size (S_GET_SEGMENT (sym));
18497
0
    if (sym == fixp->fx_subsy)
18498
0
      {
18499
0
        value = -value;
18500
0
        if (fixp->fx_addsy)
18501
0
          value += S_GET_VALUE (fixp->fx_addsy);
18502
0
      }
18503
0
    else if (fixp->fx_subsy)
18504
0
      value -= S_GET_VALUE (fixp->fx_subsy);
18505
0
    value += fixp->fx_offset;
18506
0
    if (fixp->fx_r_type == BFD_RELOC_SIZE32
18507
0
        && object_64bit
18508
0
        && !fits_in_unsigned_long (value))
18509
0
      as_bad_where (fixp->fx_file, fixp->fx_line,
18510
0
        _("symbol size computation overflow"));
18511
0
    fixp->fx_addsy = NULL;
18512
0
    fixp->fx_subsy = NULL;
18513
0
    md_apply_fix (fixp, &value, NULL);
18514
0
    return NULL;
18515
0
  }
18516
0
      if (!fixp->fx_addsy || fixp->fx_subsy)
18517
0
  {
18518
0
    as_bad_where (fixp->fx_file, fixp->fx_line,
18519
0
      "unsupported expression involving @size");
18520
0
    return NULL;
18521
0
  }
18522
0
#endif
18523
      /* Fall through.  */
18524
18525
0
    case BFD_RELOC_32_PLT_PCREL:
18526
0
    case BFD_RELOC_X86_64_GOT32:
18527
0
    case BFD_RELOC_X86_64_GOTPCREL:
18528
0
    case BFD_RELOC_X86_64_GOTPCRELX:
18529
0
    case BFD_RELOC_X86_64_REX_GOTPCRELX:
18530
0
    case BFD_RELOC_X86_64_CODE_4_GOTPCRELX:
18531
0
    case BFD_RELOC_X86_64_CODE_5_GOTPCRELX:
18532
0
    case BFD_RELOC_X86_64_CODE_6_GOTPCRELX:
18533
0
    case BFD_RELOC_386_PLT32:
18534
0
    case BFD_RELOC_386_GOT32:
18535
0
    case BFD_RELOC_386_GOT32X:
18536
0
    case BFD_RELOC_32_GOTOFF:
18537
0
    case BFD_RELOC_32_GOT_PCREL:
18538
0
    case BFD_RELOC_386_TLS_GD:
18539
0
    case BFD_RELOC_386_TLS_LDM:
18540
0
    case BFD_RELOC_386_TLS_LDO_32:
18541
0
    case BFD_RELOC_386_TLS_IE_32:
18542
0
    case BFD_RELOC_386_TLS_IE:
18543
0
    case BFD_RELOC_386_TLS_GOTIE:
18544
0
    case BFD_RELOC_386_TLS_LE_32:
18545
0
    case BFD_RELOC_386_TLS_LE:
18546
0
    case BFD_RELOC_386_TLS_GOTDESC:
18547
0
    case BFD_RELOC_386_TLS_DESC_CALL:
18548
0
    case BFD_RELOC_X86_64_TLSGD:
18549
0
    case BFD_RELOC_X86_64_TLSLD:
18550
0
    case BFD_RELOC_X86_64_DTPOFF32:
18551
0
    case BFD_RELOC_X86_64_DTPOFF64:
18552
0
    case BFD_RELOC_X86_64_GOTTPOFF:
18553
0
    case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
18554
0
    case BFD_RELOC_X86_64_CODE_5_GOTTPOFF:
18555
0
    case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
18556
0
    case BFD_RELOC_X86_64_TPOFF32:
18557
0
    case BFD_RELOC_X86_64_TPOFF64:
18558
0
    case BFD_RELOC_64_GOTOFF:
18559
0
    case BFD_RELOC_X86_64_GOTPC32:
18560
0
    case BFD_RELOC_X86_64_GOT64:
18561
0
    case BFD_RELOC_X86_64_GOTPCREL64:
18562
0
    case BFD_RELOC_64_GOT_PCREL:
18563
0
    case BFD_RELOC_X86_64_GOTPLT64:
18564
0
    case BFD_RELOC_64_PLTOFF:
18565
0
    case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
18566
0
    case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
18567
0
    case BFD_RELOC_X86_64_CODE_5_GOTPC32_TLSDESC:
18568
0
    case BFD_RELOC_X86_64_CODE_6_GOTPC32_TLSDESC:
18569
0
    case BFD_RELOC_X86_64_TLSDESC_CALL:
18570
0
    case BFD_RELOC_RVA:
18571
0
    case BFD_RELOC_VTABLE_ENTRY:
18572
0
    case BFD_RELOC_VTABLE_INHERIT:
18573
#ifdef TE_PE
18574
    case BFD_RELOC_32_SECREL:
18575
    case BFD_RELOC_16_SECIDX:
18576
#endif
18577
0
      code = fixp->fx_r_type;
18578
0
      break;
18579
0
    case BFD_RELOC_X86_64_32S:
18580
0
      if (!fixp->fx_pcrel)
18581
0
  {
18582
    /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32.  */
18583
0
    code = fixp->fx_r_type;
18584
0
    break;
18585
0
  }
18586
      /* Fall through.  */
18587
0
    default:
18588
0
      if (fixp->fx_pcrel)
18589
0
  {
18590
0
    switch (fixp->fx_size)
18591
0
      {
18592
0
      default:
18593
0
        as_bad_where (fixp->fx_file, fixp->fx_line,
18594
0
          _("can not do %d byte pc-relative relocation"),
18595
0
          fixp->fx_size);
18596
0
        code = BFD_RELOC_32_PCREL;
18597
0
        break;
18598
0
      case 1: code = BFD_RELOC_8_PCREL;  break;
18599
0
      case 2: code = BFD_RELOC_16_PCREL; break;
18600
0
      case 4: code = BFD_RELOC_32_PCREL; break;
18601
0
#ifdef BFD64
18602
0
      case 8: code = BFD_RELOC_64_PCREL; break;
18603
0
#endif
18604
0
      }
18605
0
  }
18606
0
      else
18607
0
  {
18608
0
    switch (fixp->fx_size)
18609
0
      {
18610
0
      default:
18611
0
        as_bad_where (fixp->fx_file, fixp->fx_line,
18612
0
          _("can not do %d byte relocation"),
18613
0
          fixp->fx_size);
18614
0
        code = BFD_RELOC_32;
18615
0
        break;
18616
0
      case 1: code = BFD_RELOC_8;  break;
18617
0
      case 2: code = BFD_RELOC_16; break;
18618
0
      case 4: code = BFD_RELOC_32; break;
18619
0
#ifdef BFD64
18620
0
      case 8: code = BFD_RELOC_64; break;
18621
0
#endif
18622
0
      }
18623
0
  }
18624
0
      break;
18625
0
    }
18626
18627
0
  if ((code == BFD_RELOC_32
18628
0
       || code == BFD_RELOC_32_PCREL
18629
0
       || code == BFD_RELOC_X86_64_32S)
18630
0
      && GOT_symbol
18631
0
      && fixp->fx_addsy == GOT_symbol)
18632
0
    {
18633
0
      if (!object_64bit)
18634
0
  code = BFD_RELOC_32_GOT_PCREL;
18635
0
      else
18636
0
  code = BFD_RELOC_X86_64_GOTPC32;
18637
0
    }
18638
0
  if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
18639
0
      && GOT_symbol
18640
0
      && fixp->fx_addsy == GOT_symbol)
18641
0
    {
18642
0
      code = BFD_RELOC_64_GOT_PCREL;
18643
0
    }
18644
18645
0
  rel = notes_alloc (sizeof (arelent));
18646
0
  rel->sym_ptr_ptr = notes_alloc (sizeof (asymbol *));
18647
0
  *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
18648
18649
0
  rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
18650
18651
0
  if (!use_rela_relocations)
18652
0
    {
18653
      /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
18654
   vtable entry to be used in the relocation's section offset.  */
18655
0
      if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18656
0
  rel->address = fixp->fx_offset;
18657
#if defined (OBJ_COFF) && defined (TE_PE)
18658
      else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
18659
  rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
18660
      else
18661
#endif
18662
0
      rel->addend = 0;
18663
0
    }
18664
  /* Use the rela in 64bit mode.  */
18665
0
  else
18666
0
    {
18667
0
      if (disallow_64bit_reloc)
18668
0
  switch (code)
18669
0
    {
18670
0
    case BFD_RELOC_X86_64_DTPOFF64:
18671
0
    case BFD_RELOC_X86_64_TPOFF64:
18672
0
    case BFD_RELOC_64_PCREL:
18673
0
    case BFD_RELOC_64_GOTOFF:
18674
0
    case BFD_RELOC_X86_64_GOT64:
18675
0
    case BFD_RELOC_X86_64_GOTPCREL64:
18676
0
    case BFD_RELOC_64_GOT_PCREL:
18677
0
    case BFD_RELOC_X86_64_GOTPLT64:
18678
0
    case BFD_RELOC_64_PLTOFF:
18679
0
      as_bad_where (fixp->fx_file, fixp->fx_line,
18680
0
        _("cannot represent relocation type %s in x32 mode"),
18681
0
        bfd_get_reloc_code_name (code));
18682
0
      break;
18683
0
    default:
18684
0
      break;
18685
0
    }
18686
18687
0
      if (!fixp->fx_pcrel)
18688
0
  rel->addend = fixp->fx_offset;
18689
0
      else
18690
0
  switch (code)
18691
0
    {
18692
0
    case BFD_RELOC_32_PLT_PCREL:
18693
0
    case BFD_RELOC_X86_64_GOT32:
18694
0
    case BFD_RELOC_X86_64_GOTPCREL:
18695
0
    case BFD_RELOC_X86_64_GOTPCRELX:
18696
0
    case BFD_RELOC_X86_64_REX_GOTPCRELX:
18697
0
    case BFD_RELOC_X86_64_CODE_4_GOTPCRELX:
18698
0
    case BFD_RELOC_X86_64_CODE_5_GOTPCRELX:
18699
0
    case BFD_RELOC_X86_64_CODE_6_GOTPCRELX:
18700
0
    case BFD_RELOC_X86_64_TLSGD:
18701
0
    case BFD_RELOC_X86_64_TLSLD:
18702
0
    case BFD_RELOC_X86_64_GOTTPOFF:
18703
0
    case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
18704
0
    case BFD_RELOC_X86_64_CODE_5_GOTTPOFF:
18705
0
    case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
18706
0
    case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
18707
0
    case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
18708
0
    case BFD_RELOC_X86_64_CODE_5_GOTPC32_TLSDESC:
18709
0
    case BFD_RELOC_X86_64_CODE_6_GOTPC32_TLSDESC:
18710
0
    case BFD_RELOC_X86_64_TLSDESC_CALL:
18711
0
      rel->addend = fixp->fx_offset - fixp->fx_size;
18712
0
      break;
18713
0
    default:
18714
0
      rel->addend = (section->vma
18715
0
         - fixp->fx_size
18716
0
         + fixp->fx_addnumber
18717
0
         + md_pcrel_from (fixp));
18718
0
      break;
18719
0
    }
18720
0
    }
18721
18722
0
  rel->howto = bfd_reloc_type_lookup (stdoutput, code);
18723
0
  if (rel->howto == NULL)
18724
0
    {
18725
0
      as_bad_where (fixp->fx_file, fixp->fx_line,
18726
0
        _("cannot represent relocation type %s"),
18727
0
        bfd_get_reloc_code_name (code));
18728
      /* Set howto to a garbage value so that we can keep going.  */
18729
0
      rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
18730
0
      gas_assert (rel->howto != NULL);
18731
0
    }
18732
18733
0
  return rel;
18734
0
}
18735
18736
#include "tc-i386-intel.c"
18737
18738
void
18739
tc_x86_parse_to_dw2regnum (expressionS *exp)
18740
451
{
18741
451
  int saved_naked_reg;
18742
451
  char saved_register_dot;
18743
18744
451
  saved_naked_reg = allow_naked_reg;
18745
451
  allow_naked_reg = 1;
18746
451
  saved_register_dot = register_chars['.'];
18747
451
  register_chars['.'] = '.';
18748
451
  allow_pseudo_reg = 1;
18749
451
  expression_and_evaluate (exp);
18750
451
  allow_pseudo_reg = 0;
18751
451
  register_chars['.'] = saved_register_dot;
18752
451
  allow_naked_reg = saved_naked_reg;
18753
18754
451
  if (exp->X_op == O_register && exp->X_add_number >= 0)
18755
0
    {
18756
0
      exp->X_op = O_illegal;
18757
0
      if ((addressT) exp->X_add_number < i386_regtab_size)
18758
0
  {
18759
0
    exp->X_add_number = i386_regtab[exp->X_add_number]
18760
0
            .dw2_regnum[object_64bit];
18761
0
    if (exp->X_add_number != Dw2Inval)
18762
0
      exp->X_op = O_constant;
18763
0
  }
18764
0
    }
18765
451
}
18766
18767
void
18768
tc_x86_frame_initial_instructions (void)
18769
78
{
18770
78
  cfi_add_CFA_def_cfa (object_64bit ? REG_SP : 4, -x86_cie_data_alignment);
18771
78
  cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
18772
78
}
18773
18774
int
18775
x86_dwarf2_addr_size (void)
18776
49
{
18777
49
#ifdef OBJ_ELF
18778
49
  if (x86_elf_abi == X86_64_X32_ABI)
18779
0
    return 4;
18780
49
#endif
18781
49
  return bfd_arch_bits_per_address (stdoutput) / 8;
18782
49
}
18783
18784
#ifdef TE_PE
18785
void
18786
tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
18787
{
18788
  expressionS exp;
18789
18790
  exp.X_op = O_secrel;
18791
  exp.X_add_symbol = symbol;
18792
  exp.X_add_number = 0;
18793
  emit_expr (&exp, size);
18794
}
18795
#endif
18796
18797
#ifdef OBJ_ELF
18798
int
18799
i386_elf_section_type (const char *str, size_t len)
18800
131
{
18801
131
  if (flag_code == CODE_64BIT
18802
131
      && len == sizeof ("unwind") - 1
18803
0
      && startswith (str, "unwind"))
18804
0
    return SHT_X86_64_UNWIND;
18805
18806
131
  return -1;
18807
131
}
18808
18809
void
18810
i386_elf_section_change_hook (void)
18811
3.18k
{
18812
3.18k
  struct i386_segment_info *info = &seg_info(now_seg)->tc_segment_info_data;
18813
3.18k
  struct i386_segment_info *curr, *prev;
18814
18815
3.18k
  if (info->subseg == now_subseg)
18816
3.15k
    return;
18817
18818
  /* Find the (or make a) list entry to save state into.  */
18819
68
  for (prev = info; (curr = prev->next) != NULL; prev = curr)
18820
44
    if (curr->subseg == info->subseg)
18821
7
      break;
18822
31
  if (!curr)
18823
24
    {
18824
24
      curr = notes_alloc (sizeof (*curr));
18825
24
      curr->subseg = info->subseg;
18826
24
      curr->next = NULL;
18827
24
      prev->next = curr;
18828
24
    }
18829
31
  curr->last_insn = info->last_insn;
18830
18831
  /* Find the list entry to load state from.  */
18832
84
  for (curr = info->next; curr; curr = curr->next)
18833
63
    if (curr->subseg == now_subseg)
18834
10
      break;
18835
31
  if (curr)
18836
10
    info->last_insn = curr->last_insn;
18837
21
  else
18838
21
    memset (&info->last_insn, 0, sizeof (info->last_insn));
18839
31
  info->subseg = now_subseg;
18840
31
}
18841
18842
#ifdef TE_SOLARIS
18843
void
18844
i386_solaris_fix_up_eh_frame (segT sec)
18845
{
18846
  if (flag_code == CODE_64BIT)
18847
    elf_section_type (sec) = SHT_X86_64_UNWIND;
18848
}
18849
#endif
18850
18851
/* For ELF on x86-64, add support for SHF_X86_64_LARGE.  */
18852
18853
bfd_vma
18854
x86_64_section_letter (int letter, const char **extra)
18855
3.83k
{
18856
3.83k
  if (flag_code == CODE_64BIT)
18857
1.72k
    {
18858
1.72k
      if (letter == 'l')
18859
0
  return SHF_X86_64_LARGE;
18860
18861
1.72k
      *extra = "l";
18862
1.72k
    }
18863
3.83k
  return -1;
18864
3.83k
}
18865
18866
static void
18867
handle_large_common (int small ATTRIBUTE_UNUSED)
18868
26
{
18869
26
  if (flag_code != CODE_64BIT)
18870
25
    {
18871
25
      s_comm_internal (0, elf_common_parse);
18872
25
      as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
18873
25
    }
18874
1
  else
18875
1
    {
18876
1
      static segT lbss_section;
18877
1
      asection *saved_com_section_ptr = elf_com_section_ptr;
18878
1
      asection *saved_bss_section = bss_section;
18879
18880
1
      if (lbss_section == NULL)
18881
1
  {
18882
1
    flagword applicable;
18883
1
    segT seg = now_seg;
18884
1
    subsegT subseg = now_subseg;
18885
18886
    /* The .lbss section is for local .largecomm symbols.  */
18887
1
    lbss_section = subseg_new (".lbss", 0);
18888
1
    applicable = bfd_applicable_section_flags (stdoutput);
18889
1
    bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
18890
1
    seg_info (lbss_section)->bss = 1;
18891
18892
1
    subseg_set (seg, subseg);
18893
1
  }
18894
18895
1
      elf_com_section_ptr = &bfd_elf_large_com_section;
18896
1
      bss_section = lbss_section;
18897
18898
1
      s_comm_internal (0, elf_common_parse);
18899
18900
1
      elf_com_section_ptr = saved_com_section_ptr;
18901
1
      bss_section = saved_bss_section;
18902
1
    }
18903
26
}
18904
#endif /* OBJ_ELF */