Coverage Report

Created: 2026-04-04 08:16

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
1.49k
#define SEG_PREFIX  1
64
31.9k
#define ADDR_PREFIX 2
65
4.03k
#define DATA_PREFIX 3
66
349
#define REP_PREFIX  4
67
0
#define HLE_PREFIX  REP_PREFIX
68
335
#define BND_PREFIX  REP_PREFIX
69
2.69k
#define LOCK_PREFIX 5
70
6.03k
#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
112k
#define REGISTER_PREFIX '%'
75
5.86k
#define IMMEDIATE_PREFIX '$'
76
11.9k
#define ABSOLUTE_PREFIX '*'
77
78
/* these are the instruction mnemonic suffixes in AT&T syntax or
79
   memory operand size in Intel syntax.  */
80
6.23k
#define WORD_MNEM_SUFFIX  'w'
81
9.57k
#define BYTE_MNEM_SUFFIX  'b'
82
11.9k
#define SHORT_MNEM_SUFFIX 's'
83
21.0k
#define LONG_MNEM_SUFFIX  'l'
84
89.4k
#define QWORD_MNEM_SUFFIX  'q'
85
86
606k
#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
784
#define commutative staticrounding
92
93
/*
94
  'templates' is for grouping together 'template' structures for opcodes
95
  of the same name.  This is only used for storing the insns in the grand
96
  ole hash table of insns.
97
  The templates themselves start at START and range up to (but not including)
98
  END.
99
  */
100
typedef struct
101
{
102
  const insn_template *start;
103
  const insn_template *end;
104
}
105
templates;
106
107
/* 386 operand encoding bytes:  see 386 book for details of this.  */
108
typedef struct
109
{
110
  unsigned int regmem;  /* codes register or memory operand */
111
  unsigned int reg; /* codes register operand (or extended opcode) */
112
  unsigned int mode;  /* how to interpret regmem & reg */
113
}
114
modrm_byte;
115
116
/* x86-64 extension prefix.  */
117
typedef int rex_byte;
118
119
/* 386 opcode byte to code indirect addressing.  */
120
typedef struct
121
{
122
  unsigned base;
123
  unsigned index;
124
  unsigned scale;
125
}
126
sib_byte;
127
128
/* x86 arch names, types and features */
129
typedef struct
130
{
131
  const char *name;   /* arch name */
132
  unsigned int len:8;   /* arch string length */
133
  bool skip:1;      /* show_arch should skip this. */
134
  enum processor_type type; /* arch type */
135
  enum { vsz_none, vsz_set, vsz_reset } vsz; /* vector size control */
136
  i386_cpu_flags enable;    /* cpu feature enable flags */
137
  i386_cpu_flags disable; /* cpu feature disable flags */
138
}
139
arch_entry;
140
141
/* Modes for parse_insn() to operate in.  */
142
enum parse_mode {
143
  parse_all,
144
  parse_prefix,
145
  parse_pseudo_prefix,
146
};
147
148
static void update_code_flag (int, int);
149
static void s_insn (int);
150
static void s_noopt (int);
151
static void set_code_flag (int);
152
static void set_16bit_gcc_code_flag (int);
153
static void set_intel_syntax (int);
154
static void set_intel_mnemonic (int);
155
static void set_allow_index_reg (int);
156
static void set_check (int);
157
static void set_cpu_arch (int);
158
#ifdef TE_PE
159
static void pe_directive_secrel (int);
160
static void pe_directive_secidx (int);
161
#endif
162
static void signed_cons (int);
163
static char *output_invalid (int c);
164
static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
165
            const char *);
166
static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
167
               const char *);
168
static int i386_att_operand (char *);
169
static int i386_intel_operand (char *, int);
170
static int i386_intel_simplify (expressionS *);
171
static int i386_intel_parse_name (const char *, expressionS *, enum expr_mode);
172
static const reg_entry *parse_register (const char *, char **);
173
static const char *parse_insn (const char *, char *, enum parse_mode);
174
static char *parse_operands (char *, const char *);
175
static void copy_operand (unsigned int, unsigned int);
176
static void swap_operands (void);
177
static void swap_2_operands (unsigned int, unsigned int);
178
static enum i386_flag_code i386_addressing_mode (void);
179
static void optimize_imm (void);
180
static bool optimize_disp (const insn_template *t);
181
static const insn_template *match_template (char);
182
static int check_string (void);
183
static int process_suffix (const insn_template *);
184
static int check_byte_reg (void);
185
static int check_long_reg (void);
186
static int check_qword_reg (void);
187
static int check_word_reg (void);
188
static int finalize_imm (void);
189
static int process_operands (void);
190
static const reg_entry *build_modrm_byte (void);
191
static void output_insn (const struct last_insn *);
192
static void output_imm (fragS *, offsetT);
193
static void output_disp (fragS *, offsetT);
194
#ifdef OBJ_AOUT
195
static void s_bss (int);
196
#endif
197
#ifdef OBJ_ELF
198
static void handle_large_common (int small ATTRIBUTE_UNUSED);
199
200
/* GNU_PROPERTY_X86_ISA_1_USED.  */
201
static unsigned int x86_isa_1_used;
202
/* GNU_PROPERTY_X86_FEATURE_2_USED.  */
203
static unsigned int x86_feature_2_used;
204
/* Generate x86 used ISA and feature properties.  */
205
static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
206
#endif
207
208
static const char *default_arch = DEFAULT_ARCH;
209
210
/* parse_register() returns this when a register alias cannot be used.  */
211
static const reg_entry bad_reg = { "<bad>", OPERAND_TYPE_NONE, 0, 0,
212
           { Dw2Inval, Dw2Inval } };
213
214
static const reg_entry *reg_eax;
215
static const reg_entry *reg_ds;
216
static const reg_entry *reg_es;
217
static const reg_entry *reg_ss;
218
static const reg_entry *reg_st0;
219
static const reg_entry *reg_k0;
220
221
/* VEX prefix.  */
222
typedef struct
223
{
224
  /* VEX prefix is either 2 byte or 3 byte.  EVEX is 4 byte.  */
225
  unsigned char bytes[4];
226
  unsigned int length;
227
  /* Destination or source register specifier.  */
228
  const reg_entry *register_specifier;
229
} vex_prefix;
230
231
/* 'md_assemble ()' gathers together information and puts it into a
232
   i386_insn.  */
233
234
union i386_op
235
  {
236
    expressionS *disps;
237
    expressionS *imms;
238
    const reg_entry *regs;
239
  };
240
241
enum i386_error
242
  {
243
    no_error, /* Must be first.  */
244
    operand_size_mismatch,
245
    operand_type_mismatch,
246
    register_type_mismatch,
247
    number_of_operands_mismatch,
248
    invalid_instruction_suffix,
249
    bad_imm4,
250
    unsupported_with_intel_mnemonic,
251
    unsupported_syntax,
252
    unsupported_EGPR_for_addressing,
253
    unsupported_nf,
254
    unsupported,
255
    unsupported_on_arch,
256
    unsupported_64bit,
257
    no_vex_encoding,
258
    no_evex_encoding,
259
    invalid_sib_address,
260
    invalid_vsib_address,
261
    invalid_vector_register_set,
262
    invalid_tmm_register_set,
263
    invalid_dest_and_src_register_set,
264
    invalid_dest_register_set,
265
    invalid_pseudo_prefix,
266
    unsupported_vector_index_register,
267
    unsupported_broadcast,
268
    broadcast_needed,
269
    unsupported_masking,
270
    mask_not_on_destination,
271
    no_default_mask,
272
    unsupported_rc_sae,
273
    unsupported_vector_size,
274
    unsupported_rsp_register,
275
    internal_error,
276
  };
277
278
#ifdef OBJ_ELF
279
enum x86_tls_error_type
280
{
281
  x86_tls_error_continue,
282
  x86_tls_error_none,
283
  x86_tls_error_insn,
284
  x86_tls_error_opcode,
285
  x86_tls_error_sib,
286
  x86_tls_error_no_base_reg,
287
  x86_tls_error_require_no_base_index_reg,
288
  x86_tls_error_base_reg,
289
  x86_tls_error_index_ebx,
290
  x86_tls_error_eax,
291
  x86_tls_error_RegA,
292
  x86_tls_error_ebx,
293
  x86_tls_error_rip,
294
  x86_tls_error_dest_eax,
295
  x86_tls_error_dest_rdi,
296
  x86_tls_error_scale_factor,
297
  x86_tls_error_base_reg_size,
298
  x86_tls_error_dest_32bit_reg_size,
299
  x86_tls_error_dest_64bit_reg_size,
300
  x86_tls_error_dest_32bit_or_64bit_reg_size
301
};
302
#endif
303
304
struct _i386_insn
305
  {
306
    /* TM holds the template for the insn were currently assembling.  */
307
    insn_template tm;
308
309
    /* 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
395
#define Operand_PCrel 1
335
15.7k
#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
17.1k
#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
667k
#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
20.4k
  do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
632
#define RESTORE_END_STRING(s) \
633
20.4k
  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
6.00k
#define dot_insn() (i.tm.mnem_off == MN__insn)
650
651
static enum i386_flag_code i386_flag_code;
652
182k
#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
567
#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
9.36k
#define VSZ256 1
911
5.42k
#define VSZ512 2
912
26
#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
103
#define CODE16  1
975
210
#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
105
  ((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_386_GOTOFF,
1407
               BFD_RELOC_X86_64_GOTOFF64 },
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
11.1k
{
1818
11.1k
  switch (ARRAY_SIZE(x->array))
1819
11.1k
    {
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
11.1k
    case 1:
1829
11.1k
      return !x->array[0];
1830
0
    default:
1831
0
      abort ();
1832
11.1k
    }
1833
11.1k
}
1834
1835
static INLINE void
1836
operand_type_set (union i386_operand_type *x, unsigned int v)
1837
18.6k
{
1838
18.6k
  switch (ARRAY_SIZE(x->array))
1839
18.6k
    {
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
18.6k
    case 1:
1847
18.6k
      x->array[0] = v;
1848
      /* Fall through.  */
1849
18.6k
      break;
1850
0
    default:
1851
0
      abort ();
1852
18.6k
    }
1853
1854
18.6k
  x->bitfield.class = ClassNone;
1855
18.6k
  x->bitfield.instance = InstanceNone;
1856
18.6k
}
1857
1858
static INLINE int
1859
operand_type_equal (const union i386_operand_type *x,
1860
        const union i386_operand_type *y)
1861
18
{
1862
18
  switch (ARRAY_SIZE(x->array))
1863
18
    {
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
18
    case 1:
1873
18
      return x->array[0] == y->array[0];
1874
0
      break;
1875
0
    default:
1876
0
      abort ();
1877
18
    }
1878
18
}
1879
1880
static INLINE bool
1881
_is_cpu (const i386_cpu_attr *a, enum i386_cpu cpu)
1882
93.4k
{
1883
93.4k
  switch (cpu)
1884
93.4k
    {
1885
2.44k
    case Cpu287:      return a->bitfield.cpu287;
1886
2.44k
    case Cpu387:      return a->bitfield.cpu387;
1887
0
    case Cpu3dnow:    return a->bitfield.cpu3dnow;
1888
0
    case Cpu3dnowA:   return a->bitfield.cpu3dnowa;
1889
4.08k
    case CpuAVX:      return a->bitfield.cpuavx;
1890
6
    case CpuHLE:      return a->bitfield.cpuhle;
1891
2.98k
    case CpuAVX512F:  return a->bitfield.cpuavx512f;
1892
2.44k
    case CpuAVX512VL: return a->bitfield.cpuavx512vl;
1893
3.23k
    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
75.7k
    default:
1899
75.7k
      gas_assert (cpu < CpuAttrEnums);
1900
93.4k
    }
1901
75.7k
  return a->bitfield.isa == cpu + 1u;
1902
93.4k
}
1903
1904
static INLINE bool
1905
is_cpu (const insn_template *t, enum i386_cpu cpu)
1906
91.8k
{
1907
91.8k
  return _is_cpu(&t->cpu, cpu);
1908
91.8k
}
1909
1910
static INLINE bool
1911
maybe_cpu (const insn_template *t, enum i386_cpu cpu)
1912
1.55k
{
1913
1.55k
  return _is_cpu(&t->cpu_any, cpu);
1914
1.55k
}
1915
1916
static i386_cpu_flags cpu_flags_from_attr (i386_cpu_attr a)
1917
99.1k
{
1918
99.1k
  const unsigned int bps = sizeof (a.array[0]) * CHAR_BIT;
1919
99.1k
  i386_cpu_flags f = { .array[0] = 0 };
1920
1921
99.1k
  switch (ARRAY_SIZE (a.array))
1922
99.1k
    {
1923
99.1k
    case 1:
1924
99.1k
      f.array[CpuAttrEnums / bps]
1925
99.1k
#ifndef WORDS_BIGENDIAN
1926
99.1k
  |= (a.array[0] >> CpuIsaBits) << (CpuAttrEnums % bps);
1927
#else
1928
  |= (a.array[0] << CpuIsaBits) >> (CpuAttrEnums % bps);
1929
#endif
1930
99.1k
      if (CpuMax / bps > CpuAttrEnums / bps)
1931
99.1k
  f.array[CpuAttrEnums / bps + 1]
1932
99.1k
#ifndef WORDS_BIGENDIAN
1933
99.1k
    = (a.array[0] >> CpuIsaBits) >> (bps - CpuAttrEnums % bps);
1934
#else
1935
    = (a.array[0] << CpuIsaBits) << (bps - CpuAttrEnums % bps);
1936
#endif
1937
99.1k
      break;
1938
1939
0
    default:
1940
0
      abort ();
1941
99.1k
    }
1942
1943
99.1k
  if (a.bitfield.isa)
1944
19.2k
#ifndef WORDS_BIGENDIAN
1945
19.2k
    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
99.1k
  return f;
1951
99.1k
}
1952
1953
static INLINE int
1954
cpu_flags_all_zero (const union i386_cpu_flags *x)
1955
89.2k
{
1956
89.2k
  switch (ARRAY_SIZE(x->array))
1957
89.2k
    {
1958
89.2k
    case 6:
1959
89.2k
      if (x->array[5])
1960
43
  return 0;
1961
      /* Fall through.  */
1962
89.1k
    case 5:
1963
89.1k
      if (x->array[4])
1964
17.7k
  return 0;
1965
      /* Fall through.  */
1966
71.4k
    case 4:
1967
71.4k
      if (x->array[3])
1968
75
  return 0;
1969
      /* Fall through.  */
1970
71.3k
    case 3:
1971
71.3k
      if (x->array[2])
1972
124
  return 0;
1973
      /* Fall through.  */
1974
71.2k
    case 2:
1975
71.2k
      if (x->array[1])
1976
48
  return 0;
1977
      /* Fall through.  */
1978
71.1k
    case 1:
1979
71.1k
      return !x->array[0];
1980
0
    default:
1981
0
      abort ();
1982
89.2k
    }
1983
89.2k
}
1984
1985
static INLINE int
1986
cpu_flags_equal (const union i386_cpu_flags *x,
1987
     const union i386_cpu_flags *y)
1988
20.3k
{
1989
20.3k
  switch (ARRAY_SIZE(x->array))
1990
20.3k
    {
1991
20.3k
    case 6:
1992
20.3k
      if (x->array[5] != y->array[5])
1993
11
  return 0;
1994
      /* Fall through.  */
1995
20.3k
    case 5:
1996
20.3k
      if (x->array[4] != y->array[4])
1997
11.8k
  return 0;
1998
      /* Fall through.  */
1999
8.58k
    case 4:
2000
8.58k
      if (x->array[3] != y->array[3])
2001
36
  return 0;
2002
      /* Fall through.  */
2003
8.54k
    case 3:
2004
8.54k
      if (x->array[2] != y->array[2])
2005
97
  return 0;
2006
      /* Fall through.  */
2007
8.44k
    case 2:
2008
8.44k
      if (x->array[1] != y->array[1])
2009
15
  return 0;
2010
      /* Fall through.  */
2011
8.43k
    case 1:
2012
8.43k
      return x->array[0] == y->array[0];
2013
0
      break;
2014
0
    default:
2015
0
      abort ();
2016
20.3k
    }
2017
20.3k
}
2018
2019
static INLINE int
2020
cpu_flags_check_cpu64 (const insn_template *t)
2021
48.7k
{
2022
48.7k
  return flag_code == CODE_64BIT
2023
48.7k
   ? !t->cpu.bitfield.cpuno64
2024
48.7k
   : !t->cpu.bitfield.cpu64;
2025
48.7k
}
2026
2027
static INLINE i386_cpu_flags
2028
cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
2029
30.2k
{
2030
30.2k
  switch (ARRAY_SIZE (x.array))
2031
30.2k
    {
2032
30.2k
    case 6:
2033
30.2k
      x.array [5] &= y.array [5];
2034
      /* Fall through.  */
2035
30.2k
    case 5:
2036
30.2k
      x.array [4] &= y.array [4];
2037
      /* Fall through.  */
2038
30.2k
    case 4:
2039
30.2k
      x.array [3] &= y.array [3];
2040
      /* Fall through.  */
2041
30.2k
    case 3:
2042
30.2k
      x.array [2] &= y.array [2];
2043
      /* Fall through.  */
2044
30.2k
    case 2:
2045
30.2k
      x.array [1] &= y.array [1];
2046
      /* Fall through.  */
2047
30.2k
    case 1:
2048
30.2k
      x.array [0] &= y.array [0];
2049
30.2k
      break;
2050
0
    default:
2051
0
      abort ();
2052
30.2k
    }
2053
30.2k
  return x;
2054
30.2k
}
2055
2056
static INLINE i386_cpu_flags
2057
cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
2058
40
{
2059
40
  switch (ARRAY_SIZE (x.array))
2060
40
    {
2061
40
    case 6:
2062
40
      x.array [5] |= y.array [5];
2063
      /* Fall through.  */
2064
40
    case 5:
2065
40
      x.array [4] |= y.array [4];
2066
      /* Fall through.  */
2067
40
    case 4:
2068
40
      x.array [3] |= y.array [3];
2069
      /* Fall through.  */
2070
40
    case 3:
2071
40
      x.array [2] |= y.array [2];
2072
      /* Fall through.  */
2073
40
    case 2:
2074
40
      x.array [1] |= y.array [1];
2075
      /* Fall through.  */
2076
40
    case 1:
2077
40
      x.array [0] |= y.array [0];
2078
40
      break;
2079
0
    default:
2080
0
      abort ();
2081
40
    }
2082
40
  return x;
2083
40
}
2084
2085
static INLINE i386_cpu_flags
2086
cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
2087
6.15k
{
2088
6.15k
  switch (ARRAY_SIZE (x.array))
2089
6.15k
    {
2090
6.15k
    case 6:
2091
6.15k
      x.array [5] &= ~y.array [5];
2092
      /* Fall through.  */
2093
6.15k
    case 5:
2094
6.15k
      x.array [4] &= ~y.array [4];
2095
      /* Fall through.  */
2096
6.15k
    case 4:
2097
6.15k
      x.array [3] &= ~y.array [3];
2098
      /* Fall through.  */
2099
6.15k
    case 3:
2100
6.15k
      x.array [2] &= ~y.array [2];
2101
      /* Fall through.  */
2102
6.15k
    case 2:
2103
6.15k
      x.array [1] &= ~y.array [1];
2104
      /* Fall through.  */
2105
6.15k
    case 1:
2106
6.15k
      x.array [0] &= ~y.array [0];
2107
6.15k
      break;
2108
0
    default:
2109
0
      abort ();
2110
6.15k
    }
2111
6.15k
  return x;
2112
6.15k
}
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
457
{
2118
457
  return pp.encoding == encoding_evex
2119
453
  || pp.encoding == encoding_evex512
2120
453
  || pp.has_nf
2121
453
  || (t->opcode_modifier.vex && pp.encoding == encoding_egpr)
2122
453
  || i.mask.reg;
2123
457
}
2124
2125
85.6k
#define CPU_FLAGS_ARCH_MATCH    0x1
2126
101k
#define CPU_FLAGS_64BIT_MATCH   0x2
2127
2128
#define CPU_FLAGS_PERFECT_MATCH \
2129
48.7k
  (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
17
{
2147
17
  const char *suffix_string = l;
2148
2149
296
  while (is_whitespace (*suffix_string))
2150
279
    suffix_string++;
2151
2152
  /* If {oszc flags} is absent, just return.  */
2153
17
  if (*suffix_string != '{')
2154
2
    return 0;
2155
2156
  /* Skip '{'.  */
2157
15
  suffix_string++;
2158
2159
  /* For .insn require 'scc=' as the first element.  */
2160
15
  if (dot_insn ())
2161
0
    {
2162
0
      char *copy;
2163
0
      valueT val;
2164
2165
0
      while (is_whitespace (*suffix_string))
2166
0
  suffix_string++;
2167
2168
0
      if (strncasecmp (suffix_string, "scc", 3) == 0)
2169
0
  suffix_string += 3;
2170
0
      else
2171
0
  {
2172
0
    as_bad (_("unrecognized pseudo-suffix"));
2173
0
    return -1;
2174
0
  }
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
71
  while (is_whitespace (*suffix_string))
2214
56
    suffix_string++;
2215
2216
15
  if (strncasecmp (suffix_string, "dfv", 3) == 0)
2217
0
    suffix_string += 3;
2218
15
  else
2219
15
    {
2220
15
      as_bad (_("unrecognized pseudo-suffix"));
2221
15
      return -1;
2222
15
    }
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
48.7k
{
2294
48.7k
  i386_cpu_flags cpu, active, all = cpu_flags_from_attr (t->cpu);
2295
48.7k
  i386_cpu_flags any = cpu_flags_from_attr (t->cpu_any);
2296
48.7k
  int match = cpu_flags_check_cpu64 (t) ? CPU_FLAGS_64BIT_MATCH : 0;
2297
2298
48.7k
  all.bitfield.cpu64 = 0;
2299
48.7k
  all.bitfield.cpuno64 = 0;
2300
48.7k
  gas_assert (!any.bitfield.cpu64);
2301
48.7k
  gas_assert (!any.bitfield.cpuno64);
2302
2303
48.7k
  if (cpu_flags_all_zero (&all) && cpu_flags_all_zero (&any))
2304
28.4k
    {
2305
      /* This instruction is available on all archs.  */
2306
28.4k
      return match | CPU_FLAGS_ARCH_MATCH;
2307
28.4k
    }
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
20.3k
  if (t->opcode_modifier.vex && t->opcode_modifier.evex)
2313
456
    {
2314
      /* Dual AVX/AVX512 templates need to retain AVX512* only if we already
2315
   know that EVEX encoding will be needed.  */
2316
456
      if ((any.bitfield.cpuavx || any.bitfield.cpuavx2 || any.bitfield.cpufma)
2317
430
    && (any.bitfield.cpuavx512f || any.bitfield.cpuavx512vl))
2318
430
  {
2319
430
    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
426
    else if (i.operands)
2328
0
      {
2329
0
        any.bitfield.cpuavx512f = 0;
2330
0
        any.bitfield.cpuavx512vl = 0;
2331
0
      }
2332
430
  }
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
456
      if (any.bitfield.cpuapx_f
2340
26
    && (any.bitfield.cpubmi || any.bitfield.cpubmi2
2341
26
        || any.bitfield.cpuavx512f || any.bitfield.cpuavx512bw
2342
26
        || any.bitfield.cpuavx512dq || any.bitfield.cpuamx_tile
2343
24
        || any.bitfield.cpucmpccxadd || any.bitfield.cpuuser_msr
2344
24
        || any.bitfield.cpumsr_imm || any.bitfield.cpuamx_transpose
2345
0
        || any.bitfield.cpuamx_movrs))
2346
26
  {
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
26
    gas_assert (!cpu_flags_all_zero (&all));
2351
2352
26
    cpu = cpu_flags_and (all, any);
2353
26
    gas_assert (cpu_flags_equal (&cpu, &all));
2354
2355
26
    if (need_evex_encoding (t))
2356
0
      all = any;
2357
2358
26
    memset (&any, 0, sizeof (any));
2359
26
  }
2360
456
    }
2361
19.8k
  else if (t->opcode_modifier.evex
2362
     /* Implicitly !t->opcode_modifier.vex.  */
2363
14.3k
     && all.bitfield.cpuapx_f
2364
7
     && (t->opcode_modifier.nf
2365
7
         || (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
20.3k
  if (flag_code != CODE_64BIT)
2374
6.12k
    active = cpu_flags_and_not (cpu_arch_flags, cpu_64_flags);
2375
14.2k
  else
2376
14.2k
    active = cpu_arch_flags;
2377
20.3k
  cpu = cpu_flags_and (all, active);
2378
20.3k
  if (cpu_flags_equal (&cpu, &all))
2379
8.38k
    {
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
8.38k
      if (any.bitfield.cpuavx && any.bitfield.cpuavx2)
2384
0
  any.bitfield.cpuavx2 = 0;
2385
2386
8.38k
      cpu = cpu_flags_and (any, active);
2387
8.38k
      if (cpu_flags_all_zero (&any) || !cpu_flags_all_zero (&cpu))
2388
8.36k
  match |= CPU_FLAGS_ARCH_MATCH;
2389
8.38k
    }
2390
20.3k
  return match;
2391
20.3k
}
2392
2393
static INLINE i386_operand_type
2394
operand_type_and (i386_operand_type x, i386_operand_type y)
2395
37.8k
{
2396
37.8k
  if (x.bitfield.class != y.bitfield.class)
2397
13.6k
    x.bitfield.class = ClassNone;
2398
37.8k
  if (x.bitfield.instance != y.bitfield.instance)
2399
1.96k
    x.bitfield.instance = InstanceNone;
2400
2401
37.8k
  switch (ARRAY_SIZE (x.array))
2402
37.8k
    {
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
37.8k
    case 1:
2410
37.8k
      x.array [0] &= y.array [0];
2411
37.8k
      break;
2412
0
    default:
2413
0
      abort ();
2414
37.8k
    }
2415
37.8k
  return x;
2416
37.8k
}
2417
2418
static INLINE i386_operand_type
2419
operand_type_and_not (i386_operand_type x, i386_operand_type y)
2420
9.23k
{
2421
9.23k
  gas_assert (y.bitfield.class == ClassNone);
2422
9.23k
  gas_assert (y.bitfield.instance == InstanceNone);
2423
2424
9.23k
  switch (ARRAY_SIZE (x.array))
2425
9.23k
    {
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
9.23k
    case 1:
2433
9.23k
      x.array [0] &= ~y.array [0];
2434
9.23k
      break;
2435
0
    default:
2436
0
      abort ();
2437
9.23k
    }
2438
9.23k
  return x;
2439
9.23k
}
2440
2441
static INLINE i386_operand_type
2442
operand_type_or (i386_operand_type x, i386_operand_type y)
2443
23.2k
{
2444
23.2k
  gas_assert (x.bitfield.class == ClassNone ||
2445
23.2k
              y.bitfield.class == ClassNone ||
2446
23.2k
              x.bitfield.class == y.bitfield.class);
2447
23.2k
  gas_assert (x.bitfield.instance == InstanceNone ||
2448
23.2k
              y.bitfield.instance == InstanceNone ||
2449
23.2k
              x.bitfield.instance == y.bitfield.instance);
2450
2451
23.2k
  switch (ARRAY_SIZE (x.array))
2452
23.2k
    {
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
23.2k
    case 1:
2460
23.2k
      x.array [0] |= y.array [0];
2461
23.2k
      break;
2462
0
    default:
2463
0
      abort ();
2464
23.2k
    }
2465
23.2k
  return x;
2466
23.2k
}
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
36.6k
{
2506
36.6k
  switch (c)
2507
36.6k
    {
2508
0
    case reg:
2509
0
      return t.bitfield.class == Reg;
2510
2511
5.55k
    case imm:
2512
5.55k
      return (t.bitfield.imm8
2513
4.51k
        || t.bitfield.imm8s
2514
4.48k
        || t.bitfield.imm16
2515
4.34k
        || t.bitfield.imm32
2516
4.31k
        || t.bitfield.imm32s
2517
4.31k
        || t.bitfield.imm64);
2518
2519
25.6k
    case disp:
2520
25.6k
      return (t.bitfield.disp8
2521
22.3k
        || t.bitfield.disp16
2522
20.7k
        || t.bitfield.disp32
2523
12.3k
        || t.bitfield.disp64);
2524
2525
5.44k
    case anymem:
2526
5.44k
      return (t.bitfield.disp8
2527
2.25k
        || t.bitfield.disp16
2528
2.25k
        || t.bitfield.disp32
2529
2.25k
        || t.bitfield.disp64
2530
2.19k
        || t.bitfield.baseindex);
2531
2532
0
    default:
2533
0
      abort ();
2534
36.6k
    }
2535
2536
0
  return 0;
2537
36.6k
}
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
3.87k
{
2546
3.87k
  return !((i.types[given].bitfield.byte
2547
54
      && !t->operand_types[wanted].bitfield.byte)
2548
3.87k
     || (i.types[given].bitfield.word
2549
419
         && !t->operand_types[wanted].bitfield.word)
2550
3.83k
     || (i.types[given].bitfield.dword
2551
36
         && !t->operand_types[wanted].bitfield.dword)
2552
3.82k
     || (i.types[given].bitfield.qword
2553
116
         && (!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
113
       || (intel_syntax
2558
104
           && flag_code != CODE_64BIT
2559
0
           && (t->operand_types[wanted].bitfield.class == Reg
2560
0
         || t->opcode_modifier.isstring)))));
2561
3.87k
}
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
0
{
2570
0
  return !i.types[given].bitfield.tbyte
2571
0
   || t->operand_types[wanted].bitfield.tbyte;
2572
0
}
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
2.83k
{
2581
2.83k
  return !((i.types[given].bitfield.xmmword
2582
0
      && !t->operand_types[wanted].bitfield.xmmword)
2583
2.83k
     || (i.types[given].bitfield.ymmword
2584
0
         && !t->operand_types[wanted].bitfield.ymmword)
2585
2.83k
     || (i.types[given].bitfield.zmmword
2586
0
         && !t->operand_types[wanted].bitfield.zmmword)
2587
2.83k
     || (i.types[given].bitfield.tmmword
2588
2
         && !t->operand_types[wanted].bitfield.tmmword));
2589
2.83k
}
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
3.24k
{
2598
3.24k
  return (match_operand_size (t, wanted, given)
2599
3.24k
    && (!i.types[given].bitfield.tbyte
2600
0
        || t->operand_types[wanted].bitfield.tbyte)
2601
3.24k
    && !((i.types[given].bitfield.unspecified
2602
3.24k
    && !i.broadcast.type
2603
3.24k
    && !i.broadcast.bytes
2604
3.24k
    && !t->operand_types[wanted].bitfield.unspecified)
2605
3.23k
         || (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
3.23k
         || ((t->operand_types[wanted].bitfield.class == RegSIMD
2612
402
        && t->operand_types[wanted].bitfield.byte
2613
402
           + t->operand_types[wanted].bitfield.word
2614
402
           + t->operand_types[wanted].bitfield.dword
2615
402
           + t->operand_types[wanted].bitfield.qword
2616
402
           > !!t->opcode_modifier.broadcast)
2617
3.23k
       ? (i.types[given].bitfield.xmmword
2618
402
          || i.types[given].bitfield.ymmword
2619
402
          || i.types[given].bitfield.zmmword)
2620
3.23k
       : !match_simd_size(t, wanted, given))));
2621
3.24k
}
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
10.0k
#define MATCH_STRAIGHT 1
2629
2.93k
#define MATCH_REVERSE  2
2630
2631
static INLINE unsigned int
2632
operand_size_match (const insn_template *t)
2633
6.39k
{
2634
6.39k
  unsigned int j, match = MATCH_STRAIGHT;
2635
2636
  /* Don't check non-absolute jump instructions.  */
2637
6.39k
  if (t->opcode_modifier.jump
2638
328
      && t->opcode_modifier.jump != JUMP_ABSOLUTE)
2639
274
    return match;
2640
2641
8.98k
  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
2.86k
    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
11.8k
  for (; j < i.operands; j++)
2657
5.75k
    {
2658
5.75k
      if (i.types[j].bitfield.class == Reg
2659
469
    && (t->operand_types[j].bitfield.class == Reg
2660
122
        || (t->operand_types[j].bitfield.instance == Accum
2661
104
      && (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
451
    && !match_operand_size (t, j, j))
2666
51
  {
2667
51
    match = 0;
2668
51
    break;
2669
51
  }
2670
2671
5.70k
      if (i.types[j].bitfield.class == RegFP
2672
90
    && (t->operand_types[j].bitfield.class == RegFP
2673
90
        || (t->operand_types[j].bitfield.instance == Accum
2674
0
      && t->operand_types[j].bitfield.tbyte))
2675
0
    && !match_fp_size (t, j, j))
2676
0
  {
2677
0
    match = 0;
2678
0
    break;
2679
0
  }
2680
2681
5.70k
      if (i.types[j].bitfield.class == RegSIMD
2682
33
    && (t->operand_types[j].bitfield.class == RegSIMD
2683
32
        || (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
5.70k
      if ((i.flags[j] & Operand_Mem)
2693
5.13k
    && operand_type_check (t->operand_types[j], anymem)
2694
3.15k
    && t->opcode_modifier.operandconstraint != ANY_SIZE
2695
3.15k
    && !match_mem_size (t, j, j))
2696
12
  {
2697
12
    match = 0;
2698
12
    break;
2699
12
  }
2700
5.70k
    }
2701
2702
6.12k
  if (!t->opcode_modifier.d)
2703
4.60k
    return match;
2704
2705
  /* Check reverse.  */
2706
1.52k
  gas_assert (i.operands >= 2);
2707
2708
3.35k
  for (j = i.imm_operands; j < i.operands; j++)
2709
1.83k
    {
2710
1.83k
      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
1.83k
      if (is_cpu (t, CpuFMA4) || is_cpu (t, CpuXOP)
2716
1.82k
    || is_cpu (t, CpuAPX_F)|| is_cpu (t, CpuAPX_NDD))
2717
304
  given = j < 2 ? 1 - j : j;
2718
2719
1.83k
      if (i.types[given].bitfield.class == Reg
2720
267
    && (t->operand_types[j].bitfield.class == Reg
2721
93
        || (t->operand_types[j].bitfield.instance == Accum
2722
0
      && (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
174
    && !match_operand_size (t, j, given))
2728
0
  return match;
2729
2730
1.83k
      if (i.types[given].bitfield.class == RegFP
2731
45
    && (t->operand_types[j].bitfield.class == RegFP
2732
45
        || (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
1.83k
      if (i.types[given].bitfield.class == RegSIMD
2740
14
    && t->operand_types[j].bitfield.class == RegSIMD
2741
1
    && !match_simd_size (t, j, given))
2742
1
  return match;
2743
2744
1.83k
      if ((i.flags[given] & Operand_Mem)
2745
305
    && operand_type_check (t->operand_types[j], anymem)
2746
92
    && !match_mem_size (t, j, given))
2747
2
  return match;
2748
1.83k
    }
2749
2750
1.51k
  return match | MATCH_REVERSE;
2751
1.52k
}
2752
2753
static INLINE int
2754
operand_type_match (i386_operand_type overlap,
2755
        i386_operand_type given)
2756
8.10k
{
2757
8.10k
  i386_operand_type temp = overlap;
2758
2759
8.10k
  temp.bitfield.unspecified = 0;
2760
8.10k
  temp.bitfield.byte = 0;
2761
8.10k
  temp.bitfield.word = 0;
2762
8.10k
  temp.bitfield.dword = 0;
2763
8.10k
  temp.bitfield.fword = 0;
2764
8.10k
  temp.bitfield.qword = 0;
2765
8.10k
  temp.bitfield.tbyte = 0;
2766
8.10k
  temp.bitfield.xmmword = 0;
2767
8.10k
  temp.bitfield.ymmword = 0;
2768
8.10k
  temp.bitfield.zmmword = 0;
2769
8.10k
  temp.bitfield.tmmword = 0;
2770
8.10k
  if (operand_type_all_zero (&temp))
2771
5.02k
    goto mismatch;
2772
2773
  /* When a (register) instance is expected, operand size needs checking
2774
     to disambiguate.  */
2775
3.08k
  if (overlap.bitfield.instance != InstanceNone
2776
13
      && !overlap.bitfield.byte
2777
13
      && !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
3.07k
  if (given.bitfield.baseindex == overlap.bitfield.baseindex)
2791
3.07k
    return 1;
2792
2793
5.03k
 mismatch:
2794
5.03k
  i.error = operand_type_mismatch;
2795
5.03k
  return 0;
2796
3.07k
}
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
160
{
2808
160
  if (g0.bitfield.class != Reg
2809
58
      && g0.bitfield.class != RegSIMD
2810
58
      && (g0.bitfield.unspecified
2811
0
    || !operand_type_check (g0, anymem)))
2812
58
    return 1;
2813
2814
102
  if (g1.bitfield.class != Reg
2815
99
      && g1.bitfield.class != RegSIMD
2816
99
      && (g1.bitfield.unspecified
2817
0
    || !operand_type_check (g1, anymem)))
2818
99
    return 1;
2819
2820
3
  if (g0.bitfield.byte == g1.bitfield.byte
2821
3
      && g0.bitfield.word == g1.bitfield.word
2822
3
      && g0.bitfield.dword == g1.bitfield.dword
2823
3
      && g0.bitfield.qword == g1.bitfield.qword
2824
3
      && g0.bitfield.xmmword == g1.bitfield.xmmword
2825
3
      && g0.bitfield.ymmword == g1.bitfield.ymmword
2826
3
      && g0.bitfield.zmmword == g1.bitfield.zmmword)
2827
3
    return 1;
2828
2829
  /* If expectations overlap in no more than a single size, all is fine. */
2830
0
  g0 = operand_type_and (t0, t1);
2831
0
  if (g0.bitfield.byte
2832
0
      + g0.bitfield.word
2833
0
      + g0.bitfield.dword
2834
0
      + g0.bitfield.qword
2835
0
      + g0.bitfield.xmmword
2836
0
      + g0.bitfield.ymmword
2837
0
      + g0.bitfield.zmmword <= 1)
2838
0
    return 1;
2839
2840
0
  i.error = register_type_mismatch;
2841
2842
0
  return 0;
2843
0
}
2844
2845
static INLINE unsigned int
2846
register_number (const reg_entry *r)
2847
0
{
2848
0
  unsigned int nr = r->reg_num;
2849
2850
0
  if (r->reg_flags & RegRex)
2851
0
    nr += 8;
2852
2853
0
  if (r->reg_flags & (RegVRex | RegRex2))
2854
0
    nr += 16;
2855
2856
0
  return nr;
2857
0
}
2858
2859
static INLINE unsigned int
2860
mode_from_disp_size (i386_operand_type t)
2861
0
{
2862
0
  if (t.bitfield.disp8)
2863
0
    return 1;
2864
0
  else if (t.bitfield.disp16
2865
0
     || t.bitfield.disp32)
2866
0
    return 2;
2867
0
  else
2868
0
    return 0;
2869
0
}
2870
2871
static INLINE int
2872
fits_in_signed_byte (addressT num)
2873
1.68k
{
2874
1.68k
  return num + 0x80 <= 0xff;
2875
1.68k
}
2876
2877
static INLINE int
2878
fits_in_unsigned_byte (addressT num)
2879
1.14k
{
2880
1.14k
  return num <= 0xff;
2881
1.14k
}
2882
2883
static INLINE int
2884
fits_in_unsigned_word (addressT num)
2885
686
{
2886
686
  return num <= 0xffff;
2887
686
}
2888
2889
static INLINE int
2890
fits_in_signed_word (addressT num)
2891
285
{
2892
285
  return num + 0x8000 <= 0xffff;
2893
285
}
2894
2895
static INLINE int
2896
fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
2897
1.19k
{
2898
#ifndef BFD64
2899
  return 1;
2900
#else
2901
1.19k
  return num + 0x80000000 <= 0xffffffff;
2902
1.19k
#endif
2903
1.19k
}        /* fits_in_signed_long() */
2904
2905
static INLINE int
2906
fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
2907
55.5k
{
2908
#ifndef BFD64
2909
  return 1;
2910
#else
2911
55.5k
  return num <= 0xffffffff;
2912
55.5k
#endif
2913
55.5k
}        /* 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
542
{
2931
542
  int shift = i.memshift;
2932
542
  unsigned int mask;
2933
2934
542
  if (shift == -1)
2935
0
    abort ();
2936
2937
542
  mask = (1 << shift) - 1;
2938
2939
  /* Return 0 if NUM isn't properly aligned.  */
2940
542
  if ((num & mask))
2941
0
    return 0;
2942
2943
  /* Check if NUM will fit in 8bit after shift.  */
2944
542
  return fits_in_signed_byte (num >> shift);
2945
542
}
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
1.30k
{
2958
1.30k
  i386_operand_type t;
2959
2960
1.30k
  operand_type_set (&t, 0);
2961
1.30k
  t.bitfield.imm64 = 1;
2962
2963
1.30k
  if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
2964
163
    {
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
163
      t.bitfield.imm1 = 1;
2971
163
      t.bitfield.imm8 = 1;
2972
163
      t.bitfield.imm8s = 1;
2973
163
      t.bitfield.imm16 = 1;
2974
163
      t.bitfield.imm32 = 1;
2975
163
      t.bitfield.imm32s = 1;
2976
163
    }
2977
1.14k
  else if (fits_in_signed_byte (num))
2978
852
    {
2979
852
      if (fits_in_unsigned_byte (num))
2980
595
  t.bitfield.imm8 = 1;
2981
852
      t.bitfield.imm8s = 1;
2982
852
      t.bitfield.imm16 = 1;
2983
852
      if (flag_code != CODE_64BIT || fits_in_unsigned_long (num))
2984
847
  t.bitfield.imm32 = 1;
2985
852
      t.bitfield.imm32s = 1;
2986
852
    }
2987
292
  else if (fits_in_unsigned_byte (num))
2988
8
    {
2989
8
      t.bitfield.imm8 = 1;
2990
8
      t.bitfield.imm16 = 1;
2991
8
      t.bitfield.imm32 = 1;
2992
8
      t.bitfield.imm32s = 1;
2993
8
    }
2994
284
  else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2995
138
    {
2996
138
      t.bitfield.imm16 = 1;
2997
138
      if (flag_code != CODE_64BIT || fits_in_unsigned_long (num))
2998
135
  t.bitfield.imm32 = 1;
2999
138
      t.bitfield.imm32s = 1;
3000
138
    }
3001
146
  else if (fits_in_signed_long (num))
3002
81
    {
3003
81
      if (flag_code != CODE_64BIT || fits_in_unsigned_long (num))
3004
76
  t.bitfield.imm32 = 1;
3005
81
      t.bitfield.imm32s = 1;
3006
81
    }
3007
65
  else if (fits_in_unsigned_long (num))
3008
1
    t.bitfield.imm32 = 1;
3009
3010
1.30k
  return t;
3011
1.30k
}
3012
3013
static offsetT
3014
offset_in_range (offsetT val, int size)
3015
65
{
3016
65
  addressT mask;
3017
3018
65
  switch (size)
3019
65
    {
3020
50
    case 1: mask = ((addressT) 1 <<  8) - 1; break;
3021
0
    case 2: mask = ((addressT) 1 << 16) - 1; break;
3022
0
#ifdef BFD64
3023
15
    case 4: mask = ((addressT) 1 << 32) - 1; break;
3024
0
#endif
3025
0
    case sizeof (val): return val;
3026
0
    default: abort ();
3027
65
    }
3028
3029
65
  if ((val & ~mask) != 0 && (-(addressT) val & ~mask) != 0)
3030
10
    as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
3031
10
       (uint64_t) val, (uint64_t) (val & mask));
3032
3033
65
  return val & mask;
3034
65
}
3035
3036
static INLINE const char *insn_name (const insn_template *t)
3037
1.48M
{
3038
1.48M
  return &i386_mnemonics[t->mnem_off];
3039
1.48M
}
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
542
{
3062
542
  enum PREFIX_GROUP ret = PREFIX_OTHER;
3063
542
  unsigned int q;
3064
3065
542
  if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
3066
158
      && flag_code == CODE_64BIT)
3067
158
    {
3068
158
      if ((i.prefix[REX_PREFIX] & prefix & REX_W)
3069
156
    || (i.prefix[REX_PREFIX] & prefix & REX_R)
3070
156
    || (i.prefix[REX_PREFIX] & prefix & REX_X)
3071
156
    || (i.prefix[REX_PREFIX] & prefix & REX_B))
3072
2
  ret = PREFIX_EXIST;
3073
158
      q = REX_PREFIX;
3074
158
    }
3075
384
  else
3076
384
    {
3077
384
      switch (prefix)
3078
384
  {
3079
0
  default:
3080
0
    abort ();
3081
3082
1
  case DS_PREFIX_OPCODE:
3083
1
    ret = PREFIX_DS;
3084
    /* Fall through.  */
3085
50
  case CS_PREFIX_OPCODE:
3086
50
  case ES_PREFIX_OPCODE:
3087
55
  case FS_PREFIX_OPCODE:
3088
75
  case GS_PREFIX_OPCODE:
3089
91
  case SS_PREFIX_OPCODE:
3090
91
    q = SEG_PREFIX;
3091
91
    break;
3092
3093
0
  case REPNE_PREFIX_OPCODE:
3094
14
  case REPE_PREFIX_OPCODE:
3095
14
    q = REP_PREFIX;
3096
14
    ret = PREFIX_REP;
3097
14
    break;
3098
3099
37
  case LOCK_PREFIX_OPCODE:
3100
37
    q = LOCK_PREFIX;
3101
37
    ret = PREFIX_LOCK;
3102
37
    break;
3103
3104
0
  case FWAIT_OPCODE:
3105
0
    q = WAIT_PREFIX;
3106
0
    break;
3107
3108
15
  case ADDR_PREFIX_OPCODE:
3109
15
    q = ADDR_PREFIX;
3110
15
    break;
3111
3112
227
  case DATA_PREFIX_OPCODE:
3113
227
    q = DATA_PREFIX;
3114
227
    break;
3115
384
  }
3116
384
      if (i.prefix[q] != 0)
3117
0
  ret = PREFIX_EXIST;
3118
384
    }
3119
3120
542
  if (ret)
3121
540
    {
3122
540
      if (!i.prefix[q])
3123
505
  ++i.prefixes;
3124
540
      i.prefix[q] |= prefix;
3125
540
    }
3126
2
  else
3127
2
    as_bad (_("same type of prefix used twice"));
3128
3129
542
  return ret;
3130
542
}
3131
3132
static void
3133
update_code_flag (int value, int check)
3134
1.32k
{
3135
1.32k
  PRINTF_LIKE ((*as_error)) = check ? as_fatal : as_bad;
3136
3137
1.32k
  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.32k
  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.32k
  flag_code = (enum flag_code) value;
3152
3153
1.32k
  stackop_size = '\0';
3154
1.32k
}
3155
3156
static void
3157
set_code_flag (int value)
3158
761
{
3159
761
  update_code_flag (value, 0);
3160
761
}
3161
3162
static void
3163
set_16bit_gcc_code_flag (int new_code_flag)
3164
0
{
3165
0
  flag_code = (enum flag_code) new_code_flag;
3166
0
  if (flag_code != CODE_16BIT)
3167
0
    abort ();
3168
0
  stackop_size = LONG_MNEM_SUFFIX;
3169
0
}
3170
3171
static void
3172
_set_intel_syntax (int syntax_flag)
3173
40
{
3174
40
  intel_syntax = syntax_flag;
3175
3176
40
  expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
3177
3178
40
  register_prefix = allow_naked_reg ? "" : "%";
3179
40
}
3180
3181
static void
3182
set_intel_syntax (int syntax_flag)
3183
40
{
3184
  /* Find out if register prefixing is specified.  */
3185
40
  int ask_naked_reg = 0;
3186
3187
40
  SKIP_WHITESPACE ();
3188
40
  if (!is_end_of_stmt (*input_line_pointer))
3189
14
    {
3190
14
      char *string;
3191
14
      int e = get_symbol_name (&string);
3192
3193
14
      if (strcmp (string, "prefix") == 0)
3194
0
  ask_naked_reg = 1;
3195
14
      else if (strcmp (string, "noprefix") == 0)
3196
0
  ask_naked_reg = -1;
3197
14
      else
3198
14
  as_bad (_("bad argument to syntax directive."));
3199
14
      (void) restore_line_pointer (e);
3200
14
    }
3201
40
  demand_empty_rest_of_line ();
3202
3203
40
  if (ask_naked_reg == 0)
3204
40
    allow_naked_reg = (syntax_flag
3205
37
           && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
3206
0
  else
3207
0
    allow_naked_reg = (ask_naked_reg < 0);
3208
3209
40
  _set_intel_syntax (syntax_flag);
3210
40
}
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
18
{
3268
  /* Intel MCU is only supported on ELF.  */
3269
18
#ifdef OBJ_ELF
3270
18
  static const char *arch;
3271
3272
18
  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
18
  if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
3283
18
      == new_flag.bitfield.cpuiamcu)
3284
18
    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
13
{
3293
13
  if (cpu_sub_arch_name)
3294
2
    cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
3295
2
          pfx, name, (const char *) NULL);
3296
11
  else
3297
11
    cpu_sub_arch_name = concat (pfx, name, (const char *) NULL);
3298
13
}
3299
3300
static void isa_enable (unsigned int idx)
3301
20
{
3302
20
  i386_cpu_flags flags = cpu_flags_or (cpu_arch_flags, cpu_arch[idx].enable);
3303
3304
20
  if (!cpu_flags_equal (&flags, &cpu_arch_flags))
3305
2
    {
3306
2
      extend_cpu_sub_arch_name (".", cpu_arch[idx].name);
3307
2
      cpu_arch_flags = flags;
3308
2
    }
3309
3310
20
  cpu_arch_isa_flags = cpu_flags_or (cpu_arch_isa_flags, cpu_arch[idx].enable);
3311
20
}
3312
3313
static void isa_disable (unsigned int idx)
3314
18
{
3315
18
  i386_cpu_flags flags
3316
18
    = cpu_flags_and_not (cpu_arch_flags, cpu_arch[idx].disable);
3317
3318
18
  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
18
  cpu_arch_isa_flags
3325
18
    = cpu_flags_and_not (cpu_arch_isa_flags, cpu_arch[idx].disable);
3326
18
}
3327
3328
static void
3329
set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
3330
249
{
3331
249
  typedef struct arch_stack_entry
3332
249
  {
3333
249
    const struct arch_stack_entry *prev;
3334
249
    const char *name;
3335
249
    char *sub_name;
3336
249
    i386_cpu_flags flags;
3337
249
    i386_cpu_flags isa_flags;
3338
249
    enum processor_type isa;
3339
249
    enum flag_code flag_code;
3340
249
    unsigned int vector_size;
3341
249
    char stackop_size;
3342
249
    bool no_cond_jump_promotion;
3343
249
  } arch_stack_entry;
3344
249
  static const arch_stack_entry *arch_stack_top;
3345
249
  char *s;
3346
249
  int e;
3347
249
  const char *string;
3348
249
  unsigned int j = 0;
3349
3350
249
  SKIP_WHITESPACE ();
3351
3352
249
  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
248
  e = get_symbol_name (&s);
3360
248
  string = s;
3361
3362
248
  if (strcmp (string, "push") == 0)
3363
10
    {
3364
10
      arch_stack_entry *top = XNEW (arch_stack_entry);
3365
3366
10
      top->name = cpu_arch_name;
3367
10
      if (cpu_sub_arch_name)
3368
0
  top->sub_name = xstrdup (cpu_sub_arch_name);
3369
10
      else
3370
10
  top->sub_name = NULL;
3371
10
      top->flags = cpu_arch_flags;
3372
10
      top->isa = cpu_arch_isa;
3373
10
      top->isa_flags = cpu_arch_isa_flags;
3374
10
      top->flag_code = flag_code;
3375
10
      top->vector_size = vector_size;
3376
10
      top->stackop_size = stackop_size;
3377
10
      top->no_cond_jump_promotion = no_cond_jump_promotion;
3378
3379
10
      top->prev = arch_stack_top;
3380
10
      arch_stack_top = top;
3381
3382
10
      (void) restore_line_pointer (e);
3383
10
      demand_empty_rest_of_line ();
3384
10
      return;
3385
10
    }
3386
3387
238
  if (strcmp (string, "pop") == 0)
3388
3
    {
3389
3
      const arch_stack_entry *top = arch_stack_top;
3390
3391
3
      if (!top)
3392
0
  {
3393
0
    as_bad (_(".arch stack is empty"));
3394
187
  restore_bad:
3395
187
    (void) restore_line_pointer (e);
3396
187
    ignore_rest_of_line ();
3397
187
    return;
3398
0
  }
3399
3400
3
      if (top->flag_code != flag_code
3401
3
    || 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
3
      arch_stack_top = top->prev;
3416
3417
3
      cpu_arch_name = top->name;
3418
3
      free (cpu_sub_arch_name);
3419
3
      cpu_sub_arch_name = top->sub_name;
3420
3
      cpu_arch_flags = top->flags;
3421
3
      cpu_arch_isa = top->isa;
3422
3
      cpu_arch_isa_flags = top->isa_flags;
3423
3
      vector_size = top->vector_size;
3424
3
      no_cond_jump_promotion = top->no_cond_jump_promotion;
3425
3426
3
      XDELETE (top);
3427
3428
3
      (void) restore_line_pointer (e);
3429
3
      demand_empty_rest_of_line ();
3430
3
      return;
3431
3
    }
3432
3433
235
  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
41.5k
  for (; j < ARRAY_SIZE (cpu_arch); j++)
3457
41.3k
    {
3458
41.3k
      if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
3459
38
    && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
3460
38
  {
3461
38
    if (*string != '.')
3462
18
      {
3463
18
        check_cpu_arch_compatible (string, cpu_arch[j].enable);
3464
3465
18
        if (flag_code == CODE_64BIT && !cpu_arch[j].enable.bitfield.cpu64 )
3466
8
    {
3467
8
      as_bad (_("64bit mode not supported on `%s'."),
3468
8
        cpu_arch[j].name);
3469
8
      goto restore_bad;
3470
8
    }
3471
3472
10
        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
10
        cpu_arch_name = cpu_arch[j].name;
3480
10
        free (cpu_sub_arch_name);
3481
10
        cpu_sub_arch_name = NULL;
3482
10
        cpu_arch_flags = cpu_arch[j].enable;
3483
10
        cpu_arch_isa = cpu_arch[j].type;
3484
10
        cpu_arch_isa_flags = cpu_arch[j].enable;
3485
10
        if (!cpu_arch_tune_set)
3486
10
    cpu_arch_tune = cpu_arch_isa;
3487
3488
10
        vector_size = VSZ_DEFAULT;
3489
3490
10
        pre_386_16bit_warned = false;
3491
10
        break;
3492
10
      }
3493
3494
20
    if (cpu_flags_all_zero (&cpu_arch[j].enable))
3495
0
      continue;
3496
3497
20
    isa_enable (j);
3498
3499
20
    (void) restore_line_pointer (e);
3500
3501
20
    switch (cpu_arch[j].vsz)
3502
20
      {
3503
3
      default:
3504
3
        break;
3505
3506
3
      case vsz_set:
3507
#ifdef SVR4_COMMENT_CHARS
3508
        if (*input_line_pointer == ':' || *input_line_pointer == '/')
3509
#else
3510
1
        if (*input_line_pointer == '/')
3511
1
#endif
3512
1
    {
3513
1
      ++input_line_pointer;
3514
1
      switch (get_absolute_expression ())
3515
1
        {
3516
0
        case 512: vector_size = VSZ512; break;
3517
0
        case 256: vector_size = VSZ256; break;
3518
0
        case 128: vector_size = VSZ128; break;
3519
1
        default:
3520
1
          as_bad (_("Unrecognized vector size specifier"));
3521
1
          ignore_rest_of_line ();
3522
1
          return;
3523
1
        }
3524
0
      break;
3525
1
    }
3526
    /* Fall through.  */
3527
16
      case vsz_reset:
3528
16
        vector_size = VSZ_DEFAULT;
3529
16
        break;
3530
20
      }
3531
3532
19
    demand_empty_rest_of_line ();
3533
19
    return;
3534
20
  }
3535
41.3k
    }
3536
3537
207
  if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
3538
22
    {
3539
      /* Disable an ISA extension.  */
3540
1.84k
      for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
3541
1.84k
  if (cpu_arch[j].type == PROCESSOR_NONE
3542
938
      && strcmp (string + 3, cpu_arch[j].name) == 0)
3543
18
    {
3544
18
      isa_disable (j);
3545
3546
18
      if (cpu_arch[j].vsz == vsz_set)
3547
0
        vector_size = VSZ_DEFAULT;
3548
3549
18
      (void) restore_line_pointer (e);
3550
18
      demand_empty_rest_of_line ();
3551
18
      return;
3552
18
    }
3553
22
    }
3554
3555
189
  if (j == ARRAY_SIZE (cpu_arch))
3556
179
    {
3557
179
      as_bad (_("no such architecture: `%s'"), string);
3558
179
      goto restore_bad;
3559
179
    }
3560
3561
10
  no_cond_jump_promotion = 0;
3562
10
  if (restore_line_pointer (e) == ','
3563
0
      && !is_end_of_stmt (input_line_pointer[1]))
3564
0
    {
3565
0
      ++input_line_pointer;
3566
0
      e = get_symbol_name (&s);
3567
0
      string = s;
3568
3569
0
      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
0
      else if (strcmp (string, "jumps") != 0)
3577
0
  {
3578
0
    as_bad (_("no such architecture modifier: `%s'"), string);
3579
0
    goto restore_bad;
3580
0
  }
3581
3582
0
      (void) restore_line_pointer (e);
3583
0
    }
3584
3585
10
  demand_empty_rest_of_line ();
3586
10
}
3587
3588
enum bfd_architecture
3589
i386_arch (void)
3590
567
{
3591
567
  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
567
  else
3598
567
    return bfd_arch_i386;
3599
567
}
3600
3601
unsigned long
3602
i386_mach (void)
3603
567
{
3604
567
  if (startswith (default_arch, "x86_64"))
3605
567
    {
3606
567
      if (default_arch[6] == '\0')
3607
567
  return bfd_mach_x86_64;
3608
0
      else
3609
0
  return bfd_mach_x64_32;
3610
567
    }
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
567
}
3626

3627
#include "opcodes/i386-tbl.h"
3628
3629
static void
3630
op_lookup (const char *mnemonic)
3631
52.4k
{
3632
52.4k
   i386_op_off_t *pos = str_hash_find (op_hash, mnemonic);
3633
3634
52.4k
   if (pos != NULL)
3635
22.7k
     {
3636
22.7k
       current_templates.start = &i386_optab[pos[0]];
3637
22.7k
       current_templates.end = &i386_optab[pos[1]];
3638
22.7k
     }
3639
29.7k
   else
3640
29.7k
     current_templates.end = current_templates.start = NULL;
3641
52.4k
}
3642
3643
void
3644
md_begin (void)
3645
567
{
3646
  /* Make sure possible padding space is clear.  */
3647
567
  memset (&pp, 0, sizeof (pp));
3648
3649
  /* Initialize op_hash hash table.  */
3650
567
  op_hash = str_htab_create ();
3651
3652
567
  {
3653
567
    const i386_op_off_t *cur = i386_op_sets;
3654
567
    const i386_op_off_t *end = cur + ARRAY_SIZE (i386_op_sets) - 1;
3655
3656
1.48M
    for (; cur < end; ++cur)
3657
1.48M
      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
567
  }
3660
3661
  /* Initialize reg_hash hash table.  */
3662
567
  reg_hash = str_htab_create ();
3663
567
  {
3664
567
    const reg_entry *regtab;
3665
567
    unsigned int regtab_size = i386_regtab_size;
3666
3667
200k
    for (regtab = i386_regtab; regtab_size--; regtab++)
3668
199k
      {
3669
199k
  switch (regtab->reg_type.bitfield.class)
3670
199k
    {
3671
77.1k
    case Reg:
3672
77.1k
      if (regtab->reg_type.bitfield.dword)
3673
18.1k
        {
3674
18.1k
    if (regtab->reg_type.bitfield.instance == Accum)
3675
567
      reg_eax = regtab;
3676
18.1k
        }
3677
77.1k
      break;
3678
3679
4.53k
    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
4.53k
      if (regtab->reg_type.bitfield.instance != Accum)
3683
3.96k
        continue;
3684
567
      reg_st0 = regtab;
3685
567
      break;
3686
3687
3.96k
    case SReg:
3688
3.96k
      switch (regtab->reg_num)
3689
3.96k
        {
3690
567
        case 0: reg_es = regtab; break;
3691
567
        case 2: reg_ss = regtab; break;
3692
567
        case 3: reg_ds = regtab; break;
3693
3.96k
        }
3694
3.96k
      break;
3695
3696
4.53k
    case RegMask:
3697
4.53k
      if (!regtab->reg_num)
3698
567
        reg_k0 = regtab;
3699
4.53k
      break;
3700
199k
    }
3701
3702
195k
  if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3703
0
    as_fatal (_("duplicate %s"), regtab->reg_name);
3704
195k
      }
3705
567
  }
3706
3707
  /* Fill in lexical tables:  mnemonic_chars, operand_chars.  */
3708
567
  {
3709
567
    int c;
3710
567
    const char *p;
3711
3712
145k
    for (c = 0; c < 256; c++)
3713
145k
      {
3714
145k
  if (ISDIGIT (c) || ISLOWER (c))
3715
20.4k
    {
3716
20.4k
      mnemonic_chars[c] = c;
3717
20.4k
      register_chars[c] = c;
3718
20.4k
      operand_chars[c] = c;
3719
20.4k
    }
3720
124k
  else if (ISUPPER (c))
3721
14.7k
    {
3722
14.7k
      mnemonic_chars[c] = TOLOWER (c);
3723
14.7k
      register_chars[c] = mnemonic_chars[c];
3724
14.7k
      operand_chars[c] = c;
3725
14.7k
    }
3726
#ifdef SVR4_COMMENT_CHARS
3727
  else if (c == '\\' && strchr (i386_comment_chars, '/'))
3728
    operand_chars[c] = c;
3729
#endif
3730
3731
145k
  if (c >= 128)
3732
72.5k
    operand_chars[c] = c;
3733
145k
      }
3734
3735
567
    mnemonic_chars['_'] = '_';
3736
567
    mnemonic_chars['-'] = '-';
3737
567
    mnemonic_chars['.'] = '.';
3738
3739
3.40k
    for (p = extra_symbol_chars; *p != '\0'; p++)
3740
2.83k
      operand_chars[(unsigned char) *p] = *p;
3741
11.9k
    for (p = operand_special_chars; *p != '\0'; p++)
3742
11.3k
      operand_chars[(unsigned char) *p] = *p;
3743
567
  }
3744
3745
567
  if (object_64bit)
3746
567
    {
3747
#if defined (OBJ_COFF) && defined (TE_PE)
3748
      x86_dwarf2_return_column = 32;
3749
#else
3750
567
      x86_dwarf2_return_column = REG_RA;
3751
567
#endif
3752
567
      x86_cie_data_alignment = -8;
3753
567
    }
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
567
  if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3763
0
    abort ();
3764
567
}
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
567
{
3776
567
  if (!ENABLE_LEAK_CHECK)
3777
0
    return;
3778
567
  htab_delete (op_hash);
3779
567
  htab_delete (reg_hash);
3780
567
  GOT_symbol = NULL;
3781
567
}
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
2.40k
{
3963
2.40k
  if (other != NO_RELOC)
3964
131
    {
3965
131
      reloc_howto_type *rel;
3966
3967
131
      if (size == 8)
3968
0
  switch (other)
3969
0
    {
3970
0
    case BFD_RELOC_64_PLTOFF:
3971
0
    case BFD_RELOC_X86_64_GOTPLT64:
3972
0
      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_X86_64_GOTPC64;
3977
0
      break;
3978
0
    case BFD_RELOC_X86_64_GOTPCREL:
3979
0
      other = BFD_RELOC_X86_64_GOTPCREL64;
3980
0
      break;
3981
0
    case BFD_RELOC_X86_64_TPOFF32:
3982
0
      other = BFD_RELOC_X86_64_TPOFF64;
3983
0
      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
0
    }
3990
3991
131
#ifdef OBJ_ELF
3992
131
      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
131
#endif
4004
4005
      /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless.  */
4006
131
      if (size == 4 && (!code64 || disallow_64bit_reloc))
4007
2
  sign = -1;
4008
4009
131
      rel = bfd_reloc_type_lookup (stdoutput, other);
4010
131
      if (!rel)
4011
0
  as_bad_where (file, line, _("unknown relocation (%u)"), other);
4012
131
      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
129
      else if (pcrel && !rel->pc_relative)
4017
0
  as_bad_where (file, line,
4018
0
          _("non-pc-relative relocation for pc-relative field"));
4019
129
      else if ((rel->complain_on_overflow == complain_overflow_signed
4020
129
    && !sign)
4021
129
         || (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
129
      else
4026
129
  return other;
4027
2
      return NO_RELOC;
4028
131
    }
4029
4030
2.27k
  if (pcrel)
4031
32
    {
4032
32
      if (!sign)
4033
0
  as_bad_where (file, line,
4034
0
          _("there are no unsigned pc-relative relocations"));
4035
32
      switch (size)
4036
32
  {
4037
15
  case 1: return BFD_RELOC_8_PCREL;
4038
14
  case 2: return BFD_RELOC_16_PCREL;
4039
3
  case 4: return BFD_RELOC_32_PCREL;
4040
0
  case 8: return BFD_RELOC_64_PCREL;
4041
32
  }
4042
0
      as_bad_where (file, line,
4043
0
        _("cannot do %u byte pc-relative relocation"), size);
4044
0
    }
4045
2.24k
  else
4046
2.24k
    {
4047
2.24k
      if (sign > 0)
4048
236
  switch (size)
4049
236
    {
4050
236
    case 4: return BFD_RELOC_X86_64_32S;
4051
236
    }
4052
2.00k
      else
4053
2.00k
  switch (size)
4054
2.00k
    {
4055
366
    case 1: return BFD_RELOC_8;
4056
156
    case 2: return BFD_RELOC_16;
4057
1.29k
    case 4: return BFD_RELOC_32;
4058
171
    case 8: return BFD_RELOC_64;
4059
2.00k
    }
4060
15
      as_bad_where (file, line, _("cannot do %s %u byte relocation"),
4061
15
        sign > 0 ? "signed" : "unsigned", size);
4062
15
    }
4063
4064
15
  return NO_RELOC;
4065
2.27k
}
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
2.40k
{
4073
2.40k
  return _reloc (size, pcrel, sign, other, flag_code == CODE_64BIT, NULL, 0);
4074
2.40k
}
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_386_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_X86_64_GOTOFF64
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
5.93k
{
4154
5.93k
  return flag_code != CODE_64BIT
4155
3.65k
   || i.prefix[ADDR_PREFIX]
4156
3.65k
   || ((t->mnem_off == MN_lea
4157
3.65k
        || (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
5.93k
}
4161
4162
static INLINE bool is_padlock (const insn_template *t)
4163
2.66k
{
4164
  /* (Ab)use the PrefixRepe attribute of PadLock insns as long as no
4165
     others use it.  */
4166
2.66k
  return t->opcode_modifier.prefixok == PrefixRepe;
4167
2.66k
}
4168
4169
static int
4170
intel_float_operand (const char *mnemonic)
4171
10.5k
{
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
10.5k
  if (mnemonic[0] != 'f')
4177
10.3k
    return 0; /* non-math */
4178
4179
243
  switch (mnemonic[1])
4180
243
    {
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
15
    case 'i':
4185
15
      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
10
    case 'n':
4191
10
      if (mnemonic[2] != 'o' /* fnop */)
4192
10
  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
201
    case 's':
4199
201
      if (mnemonic[2] == 'a')
4200
5
  return 3; /* fsave */
4201
196
      if (mnemonic[2] == 't')
4202
176
  {
4203
176
    switch (mnemonic[3])
4204
176
      {
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
176
      }
4211
176
  }
4212
196
      break;
4213
196
    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
243
    }
4218
4219
213
  return 1;
4220
243
}
4221
4222
static INLINE void
4223
install_template (const insn_template *t)
4224
2.66k
{
4225
2.66k
  unsigned int l;
4226
4227
2.66k
  i.tm = *t;
4228
4229
  /* Dual VEX/EVEX templates need stripping one of the possible variants.  */
4230
2.66k
  if (t->opcode_modifier.vex && t->opcode_modifier.evex)
4231
1
    {
4232
1
      if ((maybe_cpu (t, CpuAVX) || maybe_cpu (t, CpuAVX2)
4233
1
     || 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
1
      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
1
    && maybe_cpu (t, CpuAPX_F))
4261
1
  {
4262
1
    if (need_evex_encoding (t))
4263
0
      i.tm.opcode_modifier.vex = 0;
4264
1
    else
4265
1
      i.tm.opcode_modifier.evex = 0;
4266
1
  }
4267
1
    }
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
2.66k
  if (i.tm.opcode_modifier.operandconstraint == SCC)
4272
0
    {
4273
      /* Get EVEX.SCC value from the lower 4 bits of base_opcode.  */
4274
0
      i.scc = i.tm.base_opcode & 0xf;
4275
0
      i.tm.base_opcode >>= 8;
4276
0
    }
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
2.66k
  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
2.69k
  for (l = 1; l < 4; ++l)
4286
2.69k
    if (!(i.tm.base_opcode >> (8 * l)))
4287
2.66k
      break;
4288
4289
2.66k
  i.opcode_length = l;
4290
2.66k
}
4291
4292
/* Build the VEX prefix.  */
4293
4294
static void
4295
build_vex_prefix (const insn_template *t)
4296
668
{
4297
668
  unsigned int register_specifier;
4298
668
  unsigned int vector_length;
4299
668
  bool w;
4300
4301
  /* Check register specifier.  */
4302
668
  if (i.vex.register_specifier)
4303
0
    {
4304
0
      register_specifier =
4305
0
  ~register_number (i.vex.register_specifier) & 0xf;
4306
0
      gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
4307
0
    }
4308
668
  else
4309
668
    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
668
  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
668
  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
668
  if (i.tm.opcode_modifier.vex == VEXScalar)
4374
666
    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
668
  if (i.tm.opcode_modifier.vexw == VEXWIG)
4398
192
    w = vexwig == vexw1 || (i.rex & REX_W);
4399
476
  else if (i.tm.opcode_modifier.vexw && !(i.rex & REX_W))
4400
1
    w = i.tm.opcode_modifier.vexw == VEXW1;
4401
475
  else
4402
475
    w = flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1;
4403
4404
  /* Use 2-byte VEX prefix if possible.  */
4405
668
  if (w == 0
4406
668
      && pp.encoding != encoding_vex3
4407
668
      && i.tm.opcode_space == SPACE_0F
4408
3
      && (i.rex & (REX_W | REX_X | REX_B)) == 0)
4409
3
    {
4410
      /* 2-byte VEX prefix.  */
4411
3
      bool r;
4412
4413
3
      i.vex.length = 2;
4414
3
      i.vex.bytes[0] = 0xc5;
4415
4416
      /* Check the REX.R bit.  */
4417
3
      r = !(i.rex & REX_R);
4418
3
      i.vex.bytes[1] = (r << 7
4419
3
      | register_specifier << 3
4420
3
      | vector_length << 2
4421
3
      | i.tm.opcode_modifier.opcodeprefix);
4422
3
    }
4423
665
  else
4424
665
    {
4425
      /* 3-byte VEX prefix.  */
4426
665
      i.vex.length = 3;
4427
4428
665
      switch (i.tm.opcode_space)
4429
665
  {
4430
0
  case SPACE_0F:
4431
657
  case SPACE_0F38:
4432
657
  case SPACE_0F3A:
4433
657
  case SPACE_MAP5:
4434
657
  case SPACE_MAP7:
4435
657
    i.vex.bytes[0] = 0xc4;
4436
657
    break;
4437
8
  case SPACE_XOP08:
4438
8
  case SPACE_XOP09:
4439
8
  case SPACE_XOP0A:
4440
8
    i.vex.bytes[0] = 0x8f;
4441
8
    break;
4442
0
  default:
4443
0
    abort ();
4444
665
  }
4445
4446
      /* The high 3 bits of the second VEX byte are 1's compliment
4447
   of RXB bits from REX.  */
4448
665
      i.vex.bytes[1] = ((~i.rex & 7) << 5)
4449
665
           | (!dot_insn () ? i.tm.opcode_space
4450
665
               : i.insn_opcode_space);
4451
4452
665
      i.vex.bytes[2] = (w << 7
4453
665
      | register_specifier << 3
4454
665
      | vector_length << 2
4455
665
      | i.tm.opcode_modifier.opcodeprefix);
4456
665
    }
4457
668
}
4458
4459
static INLINE bool
4460
is_any_vex_encoding (const insn_template *t)
4461
9.27k
{
4462
9.27k
  return t->opcode_modifier.vex || t->opcode_modifier.evex;
4463
9.27k
}
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
674
{
4469
674
  return i.rex2 || i.tm.opcode_space == SPACE_MAP4 || pp.has_nf
4470
674
    || (i.vex.register_specifier
4471
2
  && (i.vex.register_specifier->reg_flags & RegRex2));
4472
674
}
4473
4474
static INLINE bool
4475
is_apx_rex2_encoding (void)
4476
3.99k
{
4477
3.99k
  return i.rex2 || pp.rex2_encoding
4478
3.98k
  || i.tm.opcode_modifier.rex2;
4479
3.99k
}
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
6
{
4556
6
  unsigned int register_specifier;
4557
6
  bool w;
4558
6
  rex_byte vrex_used = 0;
4559
4560
  /* Check register specifier.  */
4561
6
  if (i.vex.register_specifier)
4562
2
    {
4563
2
      gas_assert ((i.vrex & REX_X) == 0);
4564
4565
2
      register_specifier = i.vex.register_specifier->reg_num;
4566
2
      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
2
      if (!(i.vex.register_specifier->reg_flags & RegVRex))
4571
2
  i.vex.bytes[3] = 0x8;
4572
2
      register_specifier = ~register_specifier & 0xf;
4573
2
    }
4574
4
  else
4575
4
    {
4576
4
      register_specifier = 0xf;
4577
4578
      /* Encode upper 16 vector index register in the fourth byte of
4579
   the EVEX prefix.  */
4580
4
      if (!(i.vrex & REX_X))
4581
4
  i.vex.bytes[3] = 0x8;
4582
0
      else
4583
0
  vrex_used |= REX_X;
4584
4
    }
4585
4586
  /* 4 byte EVEX prefix.  */
4587
6
  i.vex.length = 4;
4588
6
  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
6
  gas_assert (i.tm.opcode_space >= SPACE_0F);
4593
6
  gas_assert (i.tm.opcode_space <= SPACE_MAP7);
4594
6
  i.vex.bytes[1] = ((~i.rex & 7) << 5)
4595
6
       | (!dot_insn () ? i.tm.opcode_space
4596
6
           : 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
6
  if (!(i.vrex & REX_R))
4601
6
    i.vex.bytes[1] |= 0x10;
4602
0
  else
4603
0
    vrex_used |= REX_R;
4604
4605
6
  if ((i.reg_operands + i.imm_operands) == i.operands)
4606
6
    {
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
6
      if ((i.vrex & REX_B))
4612
0
  {
4613
0
    vrex_used |= REX_B;
4614
0
    i.vex.bytes[1] &= ~0x40;
4615
0
  }
4616
6
    }
4617
4618
  /* EVEX instructions shouldn't need the REX prefix.  */
4619
6
  i.vrex &= ~vrex_used;
4620
6
  gas_assert (i.vrex == 0);
4621
4622
  /* Check the REX.W bit and VEXW.  */
4623
6
  if (i.tm.opcode_modifier.vexw == VEXWIG)
4624
0
    w = evexwig == evexw1 || (i.rex & REX_W);
4625
6
  else if (i.tm.opcode_modifier.vexw && !(i.rex & REX_W))
4626
2
    w = i.tm.opcode_modifier.vexw == VEXW1;
4627
4
  else
4628
4
    w = flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1;
4629
4630
6
  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
6
  i.vex.bytes[2] = ((w << 7)
4682
6
        | (register_specifier << 3)
4683
6
        | 4 /* Encode the U bit.  */
4684
6
        | i.tm.opcode_modifier.opcodeprefix);
4685
4686
  /* The fourth byte of the EVEX prefix.  */
4687
  /* The zeroing-masking bit.  */
4688
6
  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
6
  if (i.rounding.type == rc_none)
4693
6
    {
4694
      /* Encode the vector length.  */
4695
6
      unsigned int vec_length;
4696
4697
6
      switch (i.tm.opcode_modifier.evex)
4698
6
  {
4699
1
  case EVEXLIG: /* LL' is ignored */
4700
1
    vec_length = evexlig << 5;
4701
1
    break;
4702
0
  case EVEX128:
4703
0
    vec_length = 0 << 5;
4704
0
    break;
4705
5
  case EVEX256:
4706
5
    vec_length = 1 << 5;
4707
5
    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
6
  }
4722
6
      i.vex.bytes[3] |= vec_length;
4723
      /* Encode the broadcast bit.  */
4724
6
      if (i.broadcast.type || i.broadcast.bytes)
4725
0
  i.vex.bytes[3] |= 0x10;
4726
6
    }
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
6
  if (i.mask.reg)
4733
0
    i.vex.bytes[3] |= i.mask.reg->reg_num;
4734
6
}
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
7
{
4745
7
  i.vex.length = 2;
4746
7
  i.vex.bytes[0] = 0xd5;
4747
  /* For the W R X B bits, the variables of rex prefix will be reused.  */
4748
7
  i.vex.bytes[1] = ((i.tm.opcode_space << 7)
4749
7
        | (i.rex2 << 4)
4750
7
        | ((i.rex | i.prefix[REX_PREFIX]) & 0xf));
4751
7
}
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
2
{
4762
  /* To mimic behavior for legacy insns, transform use of DATA16 and REX64 into
4763
     their embedded-prefix representations.  */
4764
2
  if (i.tm.opcode_space == SPACE_MAP4)
4765
0
    {
4766
0
      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
0
      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
0
    }
4789
4790
2
  build_evex_prefix ();
4791
2
  if (i.rex2 & REX_R)
4792
0
    i.vex.bytes[1] &= ~0x10;
4793
2
  if (i.rex2 & REX_B)
4794
0
    i.vex.bytes[1] |= 0x08;
4795
2
  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
2
  if (i.vex.register_specifier
4801
2
      && 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
2
  if ((i.vex.register_specifier && i.tm.opcode_space == SPACE_MAP4)
4807
2
      || i.tm.opcode_modifier.operandconstraint == ZERO_UPPER
4808
2
      || force_nd)
4809
0
    i.vex.bytes[3] |= 0x10;
4810
4811
  /* Encode SCC and oszc flags bits.  */
4812
2
  if (i.tm.opcode_modifier.operandconstraint == SCC)
4813
0
    {
4814
      /* The default value of vvvv is 1111 and needs to be cleared.  */
4815
0
      i.vex.bytes[2] &= ~0x78;
4816
0
      i.vex.bytes[2] |= (i.oszc_flags << 3);
4817
      /* ND and aaa bits shold be 0.  */
4818
0
      know (!(i.vex.bytes[3] & 0x17));
4819
      /* The default value of V' is 1 and needs to be cleared.  */
4820
0
      i.vex.bytes[3] = (i.vex.bytes[3] & ~0x08) | i.scc;
4821
0
    }
4822
4823
  /* Encode the NF bit.  */
4824
2
  if (pp.has_nf || i.tm.opcode_modifier.operandconstraint == EVEX_NF)
4825
0
    i.vex.bytes[3] |= 0x04;
4826
4827
2
  return true;
4828
2
}
4829
4830
static void establish_rex (void)
4831
2.11k
{
4832
  /* Note that legacy encodings have at most 2 non-immediate operands.  */
4833
2.11k
  unsigned int first = i.imm_operands;
4834
2.11k
  unsigned int last = i.operands > first ? i.operands - first - 1 : first;
4835
4836
  /* Respect a user-specified REX prefix.  */
4837
2.11k
  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
2.11k
  if (((i.types[first].bitfield.class == Reg
4841
157
  && (i.op[first].regs->reg_flags & RegRex64) != 0)
4842
2.10k
       || (i.types[last].bitfield.class == Reg
4843
74
     && (i.op[last].regs->reg_flags & RegRex64) != 0))
4844
13
      && !is_apx_rex2_encoding () && !is_any_vex_encoding (&i.tm))
4845
13
    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
2.11k
  if (i.rex || i.rex2 || i.tm.opcode_modifier.evex)
4851
119
    {
4852
279
      for (unsigned int x = first; x <= last; x++)
4853
160
  {
4854
    /* Look for 8 bit operand that uses old registers.  */
4855
160
    if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
4856
1
        && !(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
160
  }
4871
119
    }
4872
4873
2.11k
  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
2.11k
  if (is_apx_rex2_encoding ())
4896
7
    {
4897
      /* Most prefixes are not permitted with JMPABS.  */
4898
7
      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
7
      build_rex2_prefix ();
4916
      /* The individual REX.RXBW bits got consumed.  */
4917
7
      i.rex &= REX_OPCODE;
4918
7
      i.prefix[REX_PREFIX] = 0;
4919
7
    }
4920
2.10k
  else if (i.rex != 0)
4921
112
    add_prefix (REX_OPCODE | i.rex);
4922
2.11k
}
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
44
{
6503
44
  if (!is_it_end_of_statement ())
6504
28
    as_warn (_("`.noopt' arguments ignored"));
6505
6506
44
  optimize = 0;
6507
44
  optimize_for_space = 0;
6508
6509
44
  ignore_rest_of_line ();
6510
44
}
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
1.74k
{
6676
1.74k
  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
1.74k
}
6699
6700
/* Output lfence, 0xfaee8, before instruction.  */
6701
6702
static void
6703
insert_lfence_before (const struct last_insn *last_insn)
6704
1.74k
{
6705
1.74k
  char *p;
6706
6707
1.74k
  if (i.tm.opcode_space != SPACE_BASE)
6708
77
    return;
6709
6710
1.66k
  if (i.tm.base_opcode == 0xff
6711
34
      && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
6712
7
    {
6713
      /* Insert lfence before indirect branch if needed.  */
6714
6715
7
      if (lfence_before_indirect_branch == lfence_branch_none)
6716
7
  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
1.66k
  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
1.66k
}
6816
6817
/* Shared helper for md_assemble() and s_insn().  */
6818
static void init_globals (void)
6819
81.7k
{
6820
81.7k
  unsigned int j;
6821
6822
81.7k
  memset (&i, '\0', sizeof (i));
6823
81.7k
  i.rounding.type = rc_none;
6824
490k
  for (j = 0; j < MAX_OPERANDS; j++)
6825
408k
    i.reloc[j] = NO_RELOC;
6826
81.7k
  memset (disp_expressions, '\0', sizeof (disp_expressions));
6827
81.7k
  memset (im_expressions, '\0', sizeof (im_expressions));
6828
81.7k
  save_stack_p = save_stack;
6829
81.7k
}
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
17.8k
{
6838
17.8k
  return t->opcode_modifier.sse2avx
6839
   /* Note that all SSE2AVX templates have at least one operand.  */
6840
17.8k
   ? t->operand_types[t->operands - 1].bitfield.class == RegSIMD
6841
17.8k
   : (t->opcode_space == SPACE_0F
6842
1.35k
      && (t->base_opcode | 1) == 0xbf)
6843
16.6k
     || (t->opcode_space == SPACE_BASE
6844
3.49k
         && t->base_opcode == 0x63)
6845
16.4k
     || (intel_syntax /* shld / shrd may mean suffixed shl / shr.  */
6846
8.70k
         && t->opcode_space == SPACE_MAP4
6847
5.04k
         && (t->base_opcode | 8) == 0x2c);
6848
17.8k
}
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
33
{
6854
33
  switch (r_type)
6855
33
    {
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
22
    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
22
      if (i.tm.mnem_off != MN_lea)
6966
22
  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
11
    default:
7094
11
      break;
7095
33
    }
7096
7097
  /* This relocation is OK.  */
7098
11
  return x86_tls_error_none;
7099
33
}
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
22
{
7105
22
  unsigned int k;
7106
198
  for (k = 0; k < ARRAY_SIZE (gotrel); k++)
7107
198
    if (gotrel[k].rel[object_64bit] == r_type)
7108
22
      break;
7109
7110
22
  switch (tls_error)
7111
22
    {
7112
22
    case x86_tls_error_insn:
7113
22
      as_bad (_("@%s operator cannot be used with `%s'"),
7114
22
        gotrel[k].str, insn_name (&i.tm));
7115
22
      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
22
    }
7202
22
}
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
78.8k
{
7212
78.8k
  unsigned int j;
7213
78.8k
  char mnemonic[MAX_MNEM_SIZE], mnem_suffix = 0, *copy = NULL;
7214
78.8k
  char *xstrdup_copy = NULL;
7215
78.8k
  const char *end, *pass1_mnem = NULL;
7216
78.8k
  enum i386_error pass1_err = 0;
7217
78.8k
  struct pseudo_prefixes orig_pp = pp;
7218
78.8k
  const insn_template *t;
7219
78.8k
  struct last_insn *last_insn
7220
78.8k
    = &seg_info(now_seg)->tc_segment_info_data.last_insn;
7221
7222
  /* Initialize globals.  */
7223
78.8k
  current_templates.end = current_templates.start = NULL;
7224
79.9k
 retry:
7225
79.9k
  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
79.9k
  if (last_insn->kind != last_insn_other)
7230
19.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
79.9k
  end = parse_insn (line, mnemonic, parse_all);
7237
79.9k
  if (end == NULL)
7238
62.2k
    {
7239
62.2k
      if (pass1_mnem != NULL)
7240
1.07k
  goto match_error;
7241
61.1k
      if (i.error != no_error)
7242
171
  {
7243
171
    gas_assert (current_templates.start != NULL);
7244
171
    if (may_need_pass2 (current_templates.start) && !i.suffix)
7245
2
      goto no_match;
7246
    /* No point in trying a 2nd pass - it'll only find the same suffix
7247
       again.  */
7248
169
    mnem_suffix = i.suffix;
7249
169
    goto match_error;
7250
171
  }
7251
60.9k
      return;
7252
61.1k
    }
7253
17.7k
  t = current_templates.start;
7254
  /* NB: LINE may be change to be the same as XSTRDUP_COPY.  */
7255
17.7k
  if (xstrdup_copy != line && may_need_pass2 (t))
7256
1.40k
    {
7257
      /* Make a copy of the full line in case we need to retry.  */
7258
1.40k
      xstrdup_copy = xstrdup (line);
7259
1.40k
      copy = xstrdup_copy;
7260
1.40k
    }
7261
17.7k
  line += end - line;
7262
17.7k
  mnem_suffix = i.suffix;
7263
7264
17.7k
  line = parse_operands (line, mnemonic);
7265
17.7k
  this_operand = -1;
7266
17.7k
  if (line == NULL)
7267
9.46k
    {
7268
9.46k
      free (xstrdup_copy);
7269
9.46k
      return;
7270
9.46k
    }
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
8.26k
  if (intel_syntax
7282
4.07k
      && 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.02k
     && operand_type_check (i.types[0], imm)
7293
16
     && 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
8.26k
  if ((i.imm_operands == 2
7302
26
       && (t->mnem_off == MN_extrq || t->mnem_off == MN_insertq))
7303
8.26k
      || ((t->mnem_off == MN_out || t->mnem_off == MN_uwrmsr
7304
8.26k
     || t->mnem_off == MN_wrmsrns)
7305
16
    && i.imm_operands && i.operands > i.imm_operands))
7306
0
      swap_2_operands (0, 1);
7307
7308
8.26k
  if (i.imm_operands)
7309
2.25k
    {
7310
      /* For USER_MSR and MSR_IMM instructions, imm32 stands for the name of a
7311
   model specific register (MSR). That's an unsigned quantity, whereas all
7312
   other insns with 32-bit immediate and 64-bit operand size use
7313
   sign-extended immediates (imm32s). Therefore these insns are
7314
   special-cased, bypassing the normal handling of immediates here.  */
7315
2.25k
      if (is_cpu(current_templates.start, CpuUSER_MSR)
7316
2.25k
    || t->mnem_off == MN_rdmsr
7317
2.25k
    || t->mnem_off == MN_wrmsrns)
7318
2
  {
7319
4
    for (j = 0; j < i.imm_operands; j++)
7320
2
      i.types[j] = smallest_imm_type (i.op[j].imms->X_add_number);
7321
2
  }
7322
2.24k
      else
7323
2.24k
  optimize_imm ();
7324
2.25k
    }
7325
7326
8.26k
  if (i.disp_operands && !optimize_disp (t))
7327
0
    return;
7328
7329
  /* Next, we find a template that matches the given insn,
7330
     making sure the overlap of the given operands types is consistent
7331
     with the template operand types.  */
7332
7333
8.26k
  if (!(t = match_template (mnem_suffix)))
7334
5.60k
    {
7335
5.60k
      const char *err_msg;
7336
7337
5.60k
      if (copy && !mnem_suffix)
7338
1.09k
  {
7339
1.09k
    line = copy;
7340
1.09k
    copy = NULL;
7341
1.09k
  no_match:
7342
1.09k
    pass1_err = i.error;
7343
1.09k
    pass1_mnem = insn_name (current_templates.start);
7344
1.09k
    pp = orig_pp;
7345
1.09k
    goto retry;
7346
1.09k
  }
7347
7348
      /* If a non-/only-64bit template (group) was found in pass 1, and if
7349
   _some_ template (group) was found in pass 2, squash pass 1's
7350
   error.  */
7351
4.51k
      if (pass1_err == unsupported_64bit)
7352
0
  pass1_mnem = NULL;
7353
7354
5.75k
  match_error:
7355
5.75k
      free (xstrdup_copy);
7356
7357
5.75k
      switch (pass1_mnem ? pass1_err : i.error)
7358
5.75k
  {
7359
0
  default:
7360
0
    abort ();
7361
17
  case operand_size_mismatch:
7362
17
    err_msg = _("operand size mismatch");
7363
17
    break;
7364
1.05k
  case operand_type_mismatch:
7365
1.05k
    err_msg = _("operand type mismatch");
7366
1.05k
    break;
7367
0
  case register_type_mismatch:
7368
0
    err_msg = _("register type mismatch");
7369
0
    break;
7370
4.07k
  case number_of_operands_mismatch:
7371
4.07k
    err_msg = _("number of operands mismatch");
7372
4.07k
    break;
7373
391
  case invalid_instruction_suffix:
7374
391
    err_msg = _("invalid instruction suffix");
7375
391
    break;
7376
0
  case bad_imm4:
7377
0
    err_msg = _("constant doesn't fit in 4 bits");
7378
0
    break;
7379
0
  case unsupported_with_intel_mnemonic:
7380
0
    err_msg = _("unsupported with Intel mnemonic");
7381
0
    break;
7382
0
  case unsupported_syntax:
7383
0
    err_msg = _("unsupported syntax");
7384
0
    break;
7385
0
  case unsupported_EGPR_for_addressing:
7386
0
    err_msg = _("extended GPR cannot be used as base/index");
7387
0
    break;
7388
4
  case unsupported_nf:
7389
4
    err_msg = _("{nf} unsupported");
7390
4
    break;
7391
14
  case unsupported:
7392
14
    as_bad (_("unsupported instruction `%s'"),
7393
14
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
7394
14
    return;
7395
141
  case unsupported_on_arch:
7396
141
    as_bad (_("`%s' is not supported on `%s%s'"),
7397
141
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start),
7398
141
      cpu_arch_name ? cpu_arch_name : default_arch,
7399
141
      cpu_sub_arch_name ? cpu_sub_arch_name : "");
7400
141
    return;
7401
30
  case unsupported_64bit:
7402
30
    if (ISLOWER (mnem_suffix))
7403
19
      {
7404
19
        if (flag_code == CODE_64BIT)
7405
2
    as_bad (_("`%s%c' is not supported in 64-bit mode"),
7406
2
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start),
7407
2
      mnem_suffix);
7408
17
        else
7409
17
    as_bad (_("`%s%c' is only supported in 64-bit mode"),
7410
17
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start),
7411
17
      mnem_suffix);
7412
19
      }
7413
11
    else
7414
11
      {
7415
11
        if (flag_code == CODE_64BIT)
7416
7
    as_bad (_("`%s' is not supported in 64-bit mode"),
7417
7
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
7418
4
        else
7419
4
    as_bad (_("`%s' is only supported in 64-bit mode"),
7420
4
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
7421
11
      }
7422
30
    return;
7423
35
  case no_vex_encoding:
7424
35
    err_msg = _("no VEX/XOP encoding");
7425
35
    break;
7426
0
  case no_evex_encoding:
7427
0
    err_msg = _("no EVEX encoding");
7428
0
    break;
7429
0
  case invalid_sib_address:
7430
0
    err_msg = _("invalid SIB address");
7431
0
    break;
7432
0
  case invalid_vsib_address:
7433
0
    err_msg = _("invalid VSIB address");
7434
0
    break;
7435
0
  case invalid_vector_register_set:
7436
0
    err_msg = _("mask, index, and destination registers must be distinct");
7437
0
    break;
7438
0
  case invalid_tmm_register_set:
7439
0
    err_msg = _("all tmm registers must be distinct");
7440
0
    break;
7441
0
  case invalid_dest_and_src_register_set:
7442
0
    err_msg = _("destination and source registers must be distinct");
7443
0
    break;
7444
0
  case invalid_dest_register_set:
7445
0
    err_msg = _("two dest registers must be distinct");
7446
0
    break;
7447
0
  case invalid_pseudo_prefix:
7448
0
    err_msg = _("rex2 pseudo prefix cannot be used");
7449
0
    break;
7450
0
  case unsupported_vector_index_register:
7451
0
    err_msg = _("unsupported vector index register");
7452
0
    break;
7453
0
  case unsupported_broadcast:
7454
0
    err_msg = _("unsupported broadcast");
7455
0
    break;
7456
0
  case broadcast_needed:
7457
0
    err_msg = _("broadcast is needed for operand of such type");
7458
0
    break;
7459
0
  case unsupported_masking:
7460
0
    err_msg = _("unsupported masking");
7461
0
    break;
7462
0
  case mask_not_on_destination:
7463
0
    err_msg = _("mask not on destination operand");
7464
0
    break;
7465
0
  case no_default_mask:
7466
0
    err_msg = _("default mask isn't allowed");
7467
0
    break;
7468
0
  case unsupported_rc_sae:
7469
0
    err_msg = _("unsupported static rounding/sae");
7470
0
    break;
7471
0
  case unsupported_vector_size:
7472
0
    as_bad (_("vector size above %u required for `%s'"), 128u << vector_size,
7473
0
      pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
7474
0
    return;
7475
0
  case unsupported_rsp_register:
7476
0
    err_msg = _("'rsp' register cannot be used");
7477
0
    break;
7478
0
  case internal_error:
7479
0
    err_msg = _("internal error");
7480
0
    break;
7481
5.75k
  }
7482
5.57k
      as_bad (_("%s for `%s'"), err_msg,
7483
5.57k
        pass1_mnem ? pass1_mnem : insn_name (current_templates.start));
7484
5.57k
      return;
7485
5.75k
    }
7486
7487
2.66k
  free (xstrdup_copy);
7488
7489
2.66k
  if (sse_check != check_none
7490
      /* The opcode space check isn't strictly needed; it's there only to
7491
   bypass the logic below when easily possible.  */
7492
0
      && t->opcode_space >= SPACE_0F
7493
0
      && t->opcode_space <= SPACE_0F3A
7494
0
      && !is_cpu (&i.tm, CpuSSE4a)
7495
0
      && !is_any_vex_encoding (t))
7496
0
    {
7497
      /* Some KL and all WideKL insns have only implicit %xmm operands.  */
7498
0
      bool simd = is_cpu (t, CpuKL) || is_cpu (t, CpuWideKL);
7499
7500
0
      for (j = 0; j < t->operands; ++j)
7501
0
  {
7502
0
    if (t->operand_types[j].bitfield.class == RegMMX)
7503
0
      break;
7504
0
    if (t->operand_types[j].bitfield.class == RegSIMD)
7505
0
      simd = true;
7506
0
  }
7507
7508
0
      if (j >= t->operands && simd)
7509
0
  (sse_check == check_warning
7510
0
   ? as_warn
7511
0
   : as_bad) (_("SSE instruction `%s' is used"), insn_name (&i.tm));
7512
0
    }
7513
7514
2.66k
  if (i.tm.opcode_modifier.fwait)
7515
0
    if (!add_prefix (FWAIT_OPCODE))
7516
0
      return;
7517
7518
  /* Check if REP prefix is OK.  */
7519
2.66k
  if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep
7520
0
      && (i.prefix[REP_PREFIX] != REPE_PREFIX_OPCODE
7521
0
    || i.tm.opcode_modifier.prefixok != PrefixRepe))
7522
0
    {
7523
0
      as_bad (_("invalid instruction `%s' after `%s'"),
7524
0
    insn_name (&i.tm), i.rep_prefix);
7525
0
      return;
7526
0
    }
7527
7528
  /* Check for lock without a lockable instruction.  Destination operand
7529
     must be memory unless it is xchg (0x86).  */
7530
2.66k
  if (i.prefix[LOCK_PREFIX])
7531
1
    {
7532
1
      if (i.tm.opcode_modifier.prefixok < PrefixLock
7533
1
    || i.mem_operands == 0
7534
1
    || (i.tm.base_opcode != 0x86
7535
1
        && !(i.flags[i.operands - 1] & Operand_Mem)))
7536
0
  {
7537
0
    as_bad (_("expecting lockable instruction after `lock'"));
7538
0
    return;
7539
0
  }
7540
7541
      /* Zap the redundant prefix from XCHG when optimizing.  */
7542
1
      if (i.tm.base_opcode == 0x86 && optimize && !pp.no_optimize)
7543
0
  i.prefix[LOCK_PREFIX] = 0;
7544
1
    }
7545
7546
2.66k
#ifdef OBJ_ELF
7547
2.66k
  if (i.has_gotrel && tls_check)
7548
33
    {
7549
33
      enum x86_tls_error_type tls_error;
7550
33
      for (j = 0; j < i.operands; ++j)
7551
33
  {
7552
33
    tls_error = x86_check_tls_relocation (i.reloc[j]);
7553
33
    if (tls_error == x86_tls_error_continue)
7554
0
      continue;
7555
7556
33
    if (tls_error != x86_tls_error_none)
7557
22
      x86_report_tls_error (tls_error, i.reloc[j]);
7558
33
    break;
7559
33
  }
7560
33
    }
7561
2.66k
#endif
7562
7563
2.66k
  if ((is_any_vex_encoding (&i.tm) && i.tm.opcode_space != SPACE_MAP4)
7564
2.66k
      || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
7565
2.66k
      || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX
7566
2.66k
      || is_padlock(&i.tm))
7567
2
    {
7568
      /* Check for data size prefix on VEX/XOP/EVEX encoded, SIMD, and
7569
   PadLock insns.  */
7570
2
      if (i.prefix[DATA_PREFIX])
7571
0
  {
7572
0
    as_bad (_("data size prefix invalid with `%s'"), insn_name (&i.tm));
7573
0
    return;
7574
0
  }
7575
2
    }
7576
7577
  /* Check if HLE prefix is OK.  */
7578
2.66k
  if (i.hle_prefix && !check_hle ())
7579
0
    return;
7580
7581
  /* Check BND prefix.  */
7582
2.66k
  if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
7583
0
    as_bad (_("expecting valid branch instruction after `bnd'"));
7584
7585
  /* Check NOTRACK prefix.  */
7586
2.66k
  if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
7587
0
    as_bad (_("expecting indirect branch instruction after `notrack'"));
7588
7589
2.66k
  if (is_cpu (&i.tm, CpuMPX))
7590
0
    {
7591
0
      if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
7592
0
  as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
7593
0
      else if (flag_code != CODE_16BIT
7594
0
         ? i.prefix[ADDR_PREFIX]
7595
0
         : i.mem_operands && !i.prefix[ADDR_PREFIX])
7596
0
  as_bad (_("16-bit address isn't allowed in MPX instructions"));
7597
0
    }
7598
7599
  /* Insert BND prefix.  */
7600
2.66k
  if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
7601
0
    {
7602
0
      if (!i.prefix[BND_PREFIX])
7603
0
  add_prefix (BND_PREFIX_OPCODE);
7604
0
      else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
7605
0
  {
7606
0
    as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
7607
0
    i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
7608
0
  }
7609
0
    }
7610
7611
  /* Check string instruction segment overrides.  */
7612
2.66k
  if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
7613
32
    {
7614
32
      gas_assert (i.mem_operands);
7615
32
      if (!check_string ())
7616
0
  return;
7617
32
      i.disp_operands = 0;
7618
32
    }
7619
7620
  /* The memory operand of (%dx) should be only used with input/output
7621
     instructions (base opcodes: 0x6c, 0x6e, 0xec, 0xee).  */
7622
2.66k
  if (i.input_output_operand
7623
0
      && ((i.tm.base_opcode | 0x82) != 0xee
7624
0
    || i.tm.opcode_space != SPACE_BASE))
7625
0
    {
7626
0
      as_bad (_("input/output port address isn't allowed with `%s'"),
7627
0
        insn_name (&i.tm));
7628
0
      return;
7629
0
    }
7630
7631
2.66k
  if (optimize && !pp.no_optimize && i.tm.opcode_modifier.optimize)
7632
0
    {
7633
0
      if (pp.has_nf)
7634
0
  optimize_nf_encoding ();
7635
0
      optimize_encoding ();
7636
0
    }
7637
7638
  /* Past optimization there's no need to distinguish encoding_evex,
7639
     encoding_evex512, and encoding_egpr anymore.  */
7640
2.66k
  if (pp.encoding == encoding_evex512)
7641
0
    pp.encoding = encoding_evex;
7642
2.66k
  else if (pp.encoding == encoding_egpr)
7643
7
    pp.encoding = is_any_vex_encoding (&i.tm) ? encoding_evex
7644
7
               : encoding_default;
7645
7646
  /* Similarly {nf} can now be taken to imply {evex}.  */
7647
2.66k
  if (pp.has_nf && pp.encoding == encoding_default)
7648
0
    pp.encoding = encoding_evex;
7649
7650
2.66k
  if (use_unaligned_vector_move)
7651
0
    encode_with_unaligned_vector_move ();
7652
7653
2.66k
  if (!process_suffix (t))
7654
918
    return;
7655
7656
  /* Check if IP-relative addressing requirements can be satisfied.  */
7657
1.74k
  if (is_cpu (&i.tm, CpuPREFETCHI)
7658
0
      && !(i.base_reg && i.base_reg->reg_num == RegIP))
7659
0
    as_warn (_("'%s' only supports RIP-relative address"), insn_name (&i.tm));
7660
7661
  /* Update operand types and check extended states.  */
7662
2.87k
  for (j = 0; j < i.operands; j++)
7663
1.12k
    {
7664
1.12k
      enum operand_class class = i.types[j].bitfield.class;
7665
7666
1.12k
      i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
7667
1.12k
      switch (i.tm.operand_types[j].bitfield.class)
7668
1.12k
  {
7669
543
  default:
7670
543
    break;
7671
543
  case RegMMX:
7672
0
    i.xstate |= xstate_mmx;
7673
0
    break;
7674
0
  case RegMask:
7675
0
    i.xstate |= xstate_mask;
7676
0
    break;
7677
0
  case RegSIMD:
7678
0
    if (i.tm.operand_types[j].bitfield.tmmword)
7679
0
      i.xstate |= xstate_tmm;
7680
0
    else if (i.tm.operand_types[j].bitfield.zmmword
7681
0
       && !i.tm.opcode_modifier.vex
7682
0
       && vector_size >= VSZ512)
7683
0
      i.xstate |= xstate_zmm;
7684
0
    else if (i.tm.operand_types[j].bitfield.ymmword
7685
0
       && vector_size >= VSZ256)
7686
0
      i.xstate |= xstate_ymm;
7687
0
    else if (i.tm.operand_types[j].bitfield.xmmword)
7688
0
      i.xstate |= xstate_xmm;
7689
0
    break;
7690
583
  case ClassNone:
7691
583
    i.types[j].bitfield.class = class;
7692
583
    break;
7693
1.12k
  }
7694
1.12k
    }
7695
7696
  /* Make still unresolved immediate matches conform to size of immediate
7697
     given in i.suffix.  */
7698
1.74k
  if (!finalize_imm ())
7699
0
    return;
7700
7701
1.74k
  if (i.types[0].bitfield.imm1)
7702
0
    i.imm_operands = 0; /* kludge for shift insns.  */
7703
7704
  /* For insns with operands there are more diddles to do to the opcode.  */
7705
1.74k
  if (i.operands)
7706
746
    {
7707
746
      if (!process_operands ())
7708
0
  return;
7709
746
    }
7710
998
  else if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
7711
3
    {
7712
      /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc.  */
7713
3
      as_warn (_("translating to `%sp'"), insn_name (&i.tm));
7714
3
    }
7715
7716
1.74k
  if (is_any_vex_encoding (&i.tm))
7717
2
    {
7718
2
      if (!cpu_arch_flags.bitfield.cpui286)
7719
0
  {
7720
0
    as_bad (_("instruction `%s' isn't supported outside of protected mode."),
7721
0
      insn_name (&i.tm));
7722
0
    return;
7723
0
  }
7724
7725
      /* Check for explicit REX prefix.  */
7726
2
      if ((i.prefix[REX_PREFIX]
7727
0
     && (i.tm.opcode_space != SPACE_MAP4
7728
         /* To mimic behavior for legacy insns, permit use of REX64 for promoted
7729
      legacy instructions.  */
7730
0
         || i.prefix[REX_PREFIX] != (REX_OPCODE | REX_W)))
7731
2
    || pp.rex_encoding)
7732
0
  {
7733
0
    as_bad (_("REX prefix invalid with `%s'"), insn_name (&i.tm));
7734
0
    return;
7735
0
  }
7736
7737
      /* Check for explicit REX2 prefix.  */
7738
2
      if (pp.rex2_encoding)
7739
0
  {
7740
0
    as_bad (_("{rex2} prefix invalid with `%s'"), insn_name (&i.tm));
7741
0
    return;
7742
0
  }
7743
7744
2
      if (is_apx_evex_encoding ())
7745
0
  {
7746
0
    if (!build_apx_evex_prefix (false))
7747
0
      return;
7748
0
  }
7749
2
      else if (i.tm.opcode_modifier.vex)
7750
2
  build_vex_prefix (t);
7751
0
      else
7752
0
  build_evex_prefix ();
7753
7754
      /* The individual REX.RXBW bits got consumed.  */
7755
2
      i.rex &= REX_OPCODE;
7756
7757
      /* The rex2 bits got consumed.  */
7758
2
      i.rex2 = 0;
7759
2
    }
7760
7761
  /* Handle conversion of 'int $3' --> special int3 insn.  */
7762
1.74k
  if (i.tm.mnem_off == MN_int
7763
1
      && i.op[0].imms->X_add_number == 3)
7764
0
    {
7765
0
      i.tm.base_opcode = INT3_OPCODE;
7766
0
      i.imm_operands = 0;
7767
0
    }
7768
7769
1.74k
  if ((i.tm.opcode_modifier.jump == JUMP
7770
1.63k
       || i.tm.opcode_modifier.jump == JUMP_BYTE
7771
1.62k
       || i.tm.opcode_modifier.jump == JUMP_DWORD)
7772
232
      && i.op[0].disps->X_op == O_constant)
7773
61
    {
7774
      /* Convert "jmp constant" (and "call constant") to a jump (call) to
7775
   the absolute address given by the constant.  Since ix86 jumps and
7776
   calls are pc relative, we need to generate a reloc.  */
7777
61
      i.op[0].disps->X_add_symbol = &abs_symbol;
7778
61
      i.op[0].disps->X_op = O_symbol;
7779
61
    }
7780
7781
1.74k
  establish_rex ();
7782
7783
1.74k
  insert_lfence_before (last_insn);
7784
7785
  /* We are ready to output the insn.  */
7786
1.74k
  output_insn (last_insn);
7787
7788
1.74k
#ifdef OBJ_ELF
7789
  /* PS: SCFI is enabled only for System V AMD64 ABI.  The ABI check has been
7790
     performed in i386_target_format.  */
7791
1.74k
  if (flag_synth_cfi)
7792
0
    {
7793
0
      ginsnS *ginsn;
7794
0
      ginsn = x86_ginsn_new (symbol_temp_new_now (), frch_ginsn_gen_mode ());
7795
0
      frch_ginsn_data_append (ginsn);
7796
0
    }
7797
1.74k
#endif
7798
7799
1.74k
  insert_lfence_after ();
7800
7801
1.74k
  if (i.tm.opcode_modifier.isprefix)
7802
75
    {
7803
75
      last_insn->kind = last_insn_prefix;
7804
75
      last_insn->name = insn_name (&i.tm);
7805
75
      last_insn->file = as_where (&last_insn->line);
7806
75
    }
7807
1.66k
  else
7808
1.66k
    last_insn->kind = last_insn_other;
7809
1.74k
}
7810
7811
void
7812
md_assemble (char *line)
7813
78.8k
{
7814
78.8k
  i386_assemble (line);
7815
78.8k
  current_templates.start = NULL;
7816
78.8k
  memset (&pp, 0, sizeof (pp));
7817
78.8k
}
7818
7819
/* The Q suffix is generally valid only in 64-bit mode, with very few
7820
   exceptions: fild, fistp, fisttp, and cmpxchg8b.  Note that for fild
7821
   and fisttp only one of their two templates is matched below: That's
7822
   sufficient since other relevant attributes are the same between both
7823
   respective templates.  */
7824
static INLINE bool q_suffix_allowed(const insn_template *t)
7825
13.2k
{
7826
13.2k
  return flag_code == CODE_64BIT
7827
9.27k
   || (t->opcode_space == SPACE_BASE
7828
5.91k
       && t->base_opcode == 0xdf
7829
0
       && (t->extension_opcode & 1)) /* fild / fistp / fisttp */
7830
9.27k
   || t->mnem_off == MN_cmpxchg8b;
7831
13.2k
}
7832
7833
static const char *
7834
parse_insn (const char *line, char *mnemonic, enum parse_mode mode)
7835
82.6k
{
7836
82.6k
  const char *l = line, *token_start = l;
7837
82.6k
  char *mnem_p;
7838
82.6k
  bool pass1 = !current_templates.start;
7839
82.6k
  int supported;
7840
82.6k
  const insn_template *t;
7841
82.6k
  char *dot_p = NULL;
7842
7843
83.8k
  while (1)
7844
83.8k
    {
7845
83.8k
      const char *split;
7846
7847
83.8k
      mnem_p = mnemonic;
7848
      /* Pseudo-prefixes start with an opening figure brace.  */
7849
83.8k
      if ((*mnem_p = *l) == '{')
7850
1.04k
  {
7851
1.04k
    ++mnem_p;
7852
1.04k
    ++l;
7853
1.04k
    if (is_whitespace (*l))
7854
0
      ++l;
7855
1.04k
  }
7856
82.7k
      else if (mode == parse_pseudo_prefix)
7857
877
  break;
7858
299k
      while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
7859
217k
  {
7860
217k
    if (*mnem_p == '.')
7861
6.22k
      dot_p = mnem_p;
7862
217k
    mnem_p++;
7863
217k
    if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
7864
77
      {
7865
77
      too_long:
7866
77
        as_bad (_("no such instruction: `%s'"), token_start);
7867
77
        return NULL;
7868
77
      }
7869
217k
    l++;
7870
217k
  }
7871
82.8k
      split = l;
7872
82.8k
      if (is_whitespace (*l))
7873
30.5k
  ++l;
7874
      /* Pseudo-prefixes end with a closing figure brace.  */
7875
82.8k
      if (*mnemonic == '{' && *l == '}')
7876
927
  {
7877
927
    *mnem_p++ = *l++;
7878
927
    if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
7879
0
      goto too_long;
7880
927
    *mnem_p = '\0';
7881
7882
927
    if (is_whitespace (*l))
7883
750
      ++l;
7884
927
  }
7885
81.9k
      else if (l == split
7886
51.4k
         && *l != END_OF_INSN
7887
43.4k
         && (intel_syntax
7888
8.31k
       || (*l != PREFIX_SEPARATOR && *l != ',')))
7889
41.1k
  {
7890
41.1k
    if (mode != parse_all)
7891
495
      break;
7892
40.6k
    as_bad (_("invalid character %s in mnemonic"),
7893
40.6k
      output_invalid (*split));
7894
40.6k
    return NULL;
7895
41.1k
  }
7896
41.7k
      if (token_start == l)
7897
308
  {
7898
308
    if (!intel_syntax && *l == PREFIX_SEPARATOR)
7899
1
      as_bad (_("expecting prefix; got nothing"));
7900
307
    else
7901
307
      as_bad (_("expecting mnemonic; got nothing"));
7902
308
    return NULL;
7903
308
  }
7904
7905
      /* Look up instruction (or prefix) via hash table.  */
7906
41.4k
      op_lookup (mnemonic);
7907
7908
41.4k
      if (*l != END_OF_INSN
7909
33.4k
    && current_templates.start
7910
12.0k
    && current_templates.start->opcode_modifier.isprefix)
7911
1.16k
  {
7912
1.16k
    supported = cpu_flags_match (current_templates.start);
7913
1.16k
    if (!(supported & CPU_FLAGS_64BIT_MATCH))
7914
5
      {
7915
5
        as_bad ((flag_code != CODE_64BIT
7916
5
           ? _("`%s' is only supported in 64-bit mode")
7917
5
           : _("`%s' is not supported in 64-bit mode")),
7918
5
          insn_name (current_templates.start));
7919
5
        return NULL;
7920
5
      }
7921
1.16k
    if (supported != CPU_FLAGS_PERFECT_MATCH)
7922
5
      {
7923
5
        as_bad (_("`%s' is not supported on `%s%s'"),
7924
5
          insn_name (current_templates.start),
7925
5
          cpu_arch_name ? cpu_arch_name : default_arch,
7926
5
          cpu_sub_arch_name ? cpu_sub_arch_name : "");
7927
5
        return NULL;
7928
5
      }
7929
    /* If we are in 16-bit mode, do not allow addr16 or data16.
7930
       Similarly, in 32-bit mode, do not allow addr32 or data32.  */
7931
1.15k
    if ((current_templates.start->opcode_modifier.size == SIZE16
7932
1.09k
         || current_templates.start->opcode_modifier.size == SIZE32)
7933
66
        && flag_code != CODE_64BIT
7934
24
        && ((current_templates.start->opcode_modifier.size == SIZE32)
7935
24
      ^ (flag_code == CODE_16BIT)))
7936
3
      {
7937
3
        as_bad (_("redundant %s prefix"),
7938
3
          insn_name (current_templates.start));
7939
3
        return NULL;
7940
3
      }
7941
7942
1.15k
    if (current_templates.start->base_opcode == PSEUDO_PREFIX)
7943
918
      {
7944
        /* Handle pseudo prefixes.  */
7945
918
        switch (current_templates.start->extension_opcode)
7946
918
    {
7947
0
    case Prefix_Disp8:
7948
      /* {disp8} */
7949
0
      pp.disp_encoding = disp_encoding_8bit;
7950
0
      break;
7951
23
    case Prefix_Disp16:
7952
      /* {disp16} */
7953
23
      pp.disp_encoding = disp_encoding_16bit;
7954
23
      break;
7955
1
    case Prefix_Disp32:
7956
      /* {disp32} */
7957
1
      pp.disp_encoding = disp_encoding_32bit;
7958
1
      break;
7959
8
    case Prefix_Load:
7960
      /* {load} */
7961
8
      pp.dir_encoding = dir_encoding_load;
7962
8
      break;
7963
66
    case Prefix_Store:
7964
      /* {store} */
7965
66
      pp.dir_encoding = dir_encoding_store;
7966
66
      break;
7967
40
    case Prefix_VEX:
7968
      /* {vex} */
7969
40
      pp.encoding = encoding_vex;
7970
40
      break;
7971
0
    case Prefix_VEX3:
7972
      /* {vex3} */
7973
0
      pp.encoding = encoding_vex3;
7974
0
      break;
7975
733
    case Prefix_EVEX:
7976
      /* {evex} */
7977
733
      pp.encoding = encoding_evex;
7978
733
      break;
7979
41
    case Prefix_REX:
7980
      /* {rex} */
7981
41
      pp.rex_encoding = true;
7982
41
      break;
7983
0
    case Prefix_REX2:
7984
      /* {rex2} */
7985
0
      pp.rex2_encoding = true;
7986
0
      break;
7987
5
    case Prefix_NF:
7988
      /* {nf} */
7989
5
      pp.has_nf = true;
7990
5
      break;
7991
0
    case Prefix_NoOptimize:
7992
      /* {nooptimize} */
7993
0
      pp.no_optimize = true;
7994
0
      break;
7995
1
    case Prefix_NoImm8s:
7996
      /* {noimm8s} */
7997
1
      pp.no_imm8s = true;
7998
1
      break;
7999
0
    default:
8000
0
      abort ();
8001
918
    }
8002
918
        if (pp.has_nf
8003
5
      && pp.encoding != encoding_default
8004
0
      && pp.encoding != encoding_evex)
8005
0
    {
8006
0
      as_bad (_("{nf} cannot be combined with {vex}/{vex3}"));
8007
0
      return NULL;
8008
0
    }
8009
918
      }
8010
238
    else
8011
238
      {
8012
        /* Add prefix, checking for repeated prefixes.  */
8013
238
        switch (add_prefix (current_templates.start->base_opcode))
8014
238
    {
8015
2
    case PREFIX_EXIST:
8016
2
      return NULL;
8017
1
    case PREFIX_DS:
8018
1
      if (is_cpu (current_templates.start, CpuIBT))
8019
0
        i.notrack_prefix = insn_name (current_templates.start);
8020
1
      break;
8021
6
    case PREFIX_REP:
8022
6
      if (is_cpu (current_templates.start, CpuHLE))
8023
0
        i.hle_prefix = insn_name (current_templates.start);
8024
6
      else if (is_cpu (current_templates.start, CpuMPX))
8025
0
        i.bnd_prefix = insn_name (current_templates.start);
8026
6
      else
8027
6
        i.rep_prefix = insn_name (current_templates.start);
8028
6
      break;
8029
229
    default:
8030
229
      break;
8031
238
    }
8032
238
      }
8033
    /* Skip past PREFIX_SEPARATOR and reset token_start.  */
8034
1.15k
    l += (!intel_syntax && *l == PREFIX_SEPARATOR);
8035
1.15k
    if (is_whitespace (*l))
8036
5
      ++l;
8037
1.15k
    token_start = l;
8038
1.15k
  }
8039
40.2k
      else
8040
40.2k
  break;
8041
41.4k
    }
8042
8043
41.6k
  if (mode != parse_all)
8044
2.74k
    return token_start;
8045
8046
38.8k
  if (!current_templates.start)
8047
25.9k
    {
8048
#ifdef TE_SOLARIS
8049
      /* Sun specifies an alternative form for CMOVcc: Size suffix (if any)
8050
   first, then a dot, then the condition code mnemonic.  */
8051
      if ((mnemonic + 4 == dot_p && !memcmp (mnemonic, "cmov", 4))
8052
    /* While doc doesn't say so, gcc assumes it: Same for FCMOVcc,
8053
       except that there's no size suffix to care about.  */
8054
    || (mnemonic + 5 == dot_p && !memcmp (mnemonic, "fcmov", 5)))
8055
  {
8056
    /* Simply strip the dot.  */
8057
    memmove (dot_p, dot_p + 1, mnem_p - dot_p);
8058
    dot_p = mnem_p - 1;
8059
  }
8060
      else if (!intel_syntax
8061
         && mnemonic + 5 == dot_p
8062
         && !memcmp (mnemonic, "cmov", 4)
8063
         && strchr ("lqw", TOLOWER (dot_p[-1])))
8064
  {
8065
    /* Strip the dot, while moving the suffix.  */
8066
    char suffix = dot_p[-1];
8067
8068
    memmove (dot_p - 1, dot_p + 1, mnem_p - dot_p);
8069
    mnem_p[-2] = suffix;
8070
    dot_p = mnem_p - 1;
8071
  }
8072
      else
8073
#endif
8074
      /* Deprecated functionality (new code should use pseudo-prefixes instead):
8075
   Check if we should swap operand or force 32bit displacement in
8076
   encoding.  */
8077
25.9k
      if (mnem_p - 2 == dot_p && dot_p[1] == 's')
8078
251
  {
8079
251
    if (pp.dir_encoding == dir_encoding_default)
8080
251
      pp.dir_encoding = dir_encoding_swap;
8081
0
    else
8082
0
      as_warn (_("ignoring `.s' suffix due to earlier `{%s}'"),
8083
0
         pp.dir_encoding == dir_encoding_load ? "load" : "store");
8084
251
  }
8085
25.7k
      else if (mnem_p - 3 == dot_p
8086
20
         && dot_p[1] == 'd'
8087
6
         && dot_p[2] == '8')
8088
0
  {
8089
0
    if (pp.disp_encoding == disp_encoding_default)
8090
0
      pp.disp_encoding = disp_encoding_8bit;
8091
0
    else if (pp.disp_encoding != disp_encoding_8bit)
8092
0
      as_warn (_("ignoring `.d8' suffix due to earlier `{disp<N>}'"));
8093
0
  }
8094
25.7k
      else if (mnem_p - 4 == dot_p
8095
29
         && dot_p[1] == 'd'
8096
0
         && dot_p[2] == '3'
8097
0
         && dot_p[3] == '2')
8098
0
  {
8099
0
    if (pp.disp_encoding == disp_encoding_default)
8100
0
      pp.disp_encoding = disp_encoding_32bit;
8101
0
    else if (pp.disp_encoding != disp_encoding_32bit)
8102
0
      as_warn (_("ignoring `.d32' suffix due to earlier `{disp<N>}'"));
8103
0
  }
8104
25.7k
      else
8105
25.7k
  goto check_suffix;
8106
251
      mnem_p = dot_p;
8107
251
      *dot_p = '\0';
8108
251
      op_lookup (mnemonic);
8109
251
    }
8110
8111
13.1k
  if (!current_templates.start || !pass1)
8112
1.34k
    {
8113
1.34k
      current_templates.start = NULL;
8114
8115
27.0k
    check_suffix:
8116
27.0k
      if (mnem_p > mnemonic)
8117
27.0k
  {
8118
    /* See if we can get a match by trimming off a suffix.  */
8119
27.0k
    switch (mnem_p[-1])
8120
27.0k
      {
8121
2.80k
      case WORD_MNEM_SUFFIX:
8122
2.80k
        if (intel_syntax && (intel_float_operand (mnemonic) & 2))
8123
0
    i.suffix = SHORT_MNEM_SUFFIX;
8124
2.80k
        else
8125
    /* Fall through.  */
8126
5.80k
        case BYTE_MNEM_SUFFIX:
8127
8.74k
        case QWORD_MNEM_SUFFIX:
8128
8.74k
    i.suffix = mnem_p[-1];
8129
8.74k
        mnem_p[-1] = '\0';
8130
8.74k
        op_lookup (mnemonic);
8131
8.74k
        break;
8132
3.66k
      case SHORT_MNEM_SUFFIX:
8133
4.06k
      case LONG_MNEM_SUFFIX:
8134
4.06k
        if (!intel_syntax)
8135
1.82k
    {
8136
1.82k
      i.suffix = mnem_p[-1];
8137
1.82k
      mnem_p[-1] = '\0';
8138
1.82k
      op_lookup (mnemonic);
8139
1.82k
    }
8140
4.06k
        break;
8141
8142
        /* Intel Syntax.  */
8143
179
      case 'd':
8144
179
        if (intel_syntax)
8145
120
    {
8146
120
      if (intel_float_operand (mnemonic) == 1)
8147
4
        i.suffix = SHORT_MNEM_SUFFIX;
8148
116
      else
8149
116
        i.suffix = LONG_MNEM_SUFFIX;
8150
120
      mnem_p[-1] = '\0';
8151
120
      op_lookup (mnemonic);
8152
120
    }
8153
        /* For compatibility reasons accept MOVSD and CMPSD without
8154
           operands even in AT&T mode.  */
8155
59
        else if (*l == END_OF_INSN)
8156
54
    {
8157
54
      mnem_p[-1] = '\0';
8158
54
      op_lookup (mnemonic);
8159
54
      if (current_templates.start != NULL
8160
          /* MOVS or CMPS */
8161
14
          && (current_templates.start->base_opcode | 2) == 0xa6
8162
0
          && current_templates.start->opcode_space
8163
0
       == SPACE_BASE
8164
0
          && mnem_p[-2] == 's')
8165
0
        {
8166
0
          as_warn (_("found `%sd'; assuming `%sl' was meant"),
8167
0
             mnemonic, mnemonic);
8168
0
          i.suffix = LONG_MNEM_SUFFIX;
8169
0
        }
8170
54
      else
8171
54
        {
8172
54
          current_templates.start = NULL;
8173
54
          mnem_p[-1] = 'd';
8174
54
        }
8175
54
    }
8176
179
        break;
8177
27.0k
      }
8178
27.0k
  }
8179
8180
27.0k
      if (!current_templates.start)
8181
18.4k
  {
8182
18.4k
    if (pass1)
8183
18.0k
      as_bad (_("no such instruction: `%s'"), token_start);
8184
18.4k
    return NULL;
8185
18.4k
  }
8186
27.0k
    }
8187
8188
  /* Handle SCC OSZC flgs.  */
8189
20.4k
  if (current_templates.start->opcode_modifier.operandconstraint == SCC)
8190
17
    {
8191
17
      int length = check_Scc_OszcOperations (l);
8192
17
      if (length < 0)
8193
15
  return NULL;
8194
2
      l += length;
8195
2
    }
8196
8197
20.4k
  if ((current_templates.start->opcode_modifier.jump == JUMP
8198
19.7k
       || current_templates.start->opcode_modifier.jump == JUMP_BYTE)
8199
726
      && *l == ',')
8200
0
    {
8201
      /* Check for a branch hint.  We allow ",pt" and ",pn" for
8202
   predict taken and predict not taken respectively.
8203
   I'm not sure that branch hints actually do anything on loop
8204
   and jcxz insns (JumpByte) for current Pentium4 chips.  They
8205
   may work in the future and it doesn't hurt to accept them
8206
   now.  */
8207
0
      token_start = l++;
8208
0
      if (is_whitespace (*l))
8209
0
  ++l;
8210
0
      if (TOLOWER (*l) == 'p' && ISALPHA (l[1])
8211
0
    && (l[2] == END_OF_INSN || is_whitespace (l[2])))
8212
0
  {
8213
0
    if (TOLOWER (l[1]) == 't')
8214
0
      {
8215
0
        if (!add_prefix (DS_PREFIX_OPCODE))
8216
0
    return NULL;
8217
0
        l += 2;
8218
0
      }
8219
0
    else if (TOLOWER (l[1]) == 'n')
8220
0
      {
8221
0
        if (!add_prefix (CS_PREFIX_OPCODE))
8222
0
    return NULL;
8223
0
        l += 2;
8224
0
      }
8225
0
    else
8226
0
      l = token_start;
8227
0
  }
8228
0
      else
8229
0
  l = token_start;
8230
0
    }
8231
  /* Any other comma loses.  */
8232
20.4k
  if (*l == ',')
8233
1.85k
    {
8234
1.85k
      as_bad (_("invalid character %s in mnemonic"),
8235
1.85k
        output_invalid (*l));
8236
1.85k
      return NULL;
8237
1.85k
    }
8238
8239
  /* Check if instruction is supported on specified architecture.  */
8240
18.5k
  supported = 0;
8241
38.0k
  for (t = current_templates.start; t < current_templates.end; ++t)
8242
37.2k
    {
8243
37.2k
      supported |= cpu_flags_match (t);
8244
8245
37.2k
      if (i.suffix == QWORD_MNEM_SUFFIX && !q_suffix_allowed (t))
8246
9.27k
  supported &= ~CPU_FLAGS_64BIT_MATCH;
8247
8248
37.2k
      if (supported == CPU_FLAGS_PERFECT_MATCH)
8249
17.7k
  return l;
8250
37.2k
    }
8251
8252
821
  if (pass1)
8253
171
    {
8254
171
      if (supported & CPU_FLAGS_64BIT_MATCH)
8255
141
        i.error = unsupported_on_arch;
8256
30
      else
8257
30
        i.error = unsupported_64bit;
8258
171
    }
8259
8260
821
  return NULL;
8261
18.5k
}
8262
8263
static char *
8264
parse_operands (char *l, const char *mnemonic)
8265
17.9k
{
8266
17.9k
  char *token_start;
8267
8268
  /* 1 if operand is pending after ','.  */
8269
17.9k
  unsigned int expecting_operand = 0;
8270
8271
28.7k
  while (*l != END_OF_INSN)
8272
20.2k
    {
8273
      /* Non-zero if operand parens not balanced.  */
8274
20.2k
      unsigned int paren_not_balanced = 0;
8275
      /* True if inside double quotes.  */
8276
20.2k
      bool in_quotes = false;
8277
8278
      /* Skip optional white space before operand.  */
8279
20.2k
      if (is_whitespace (*l))
8280
35
  ++l;
8281
20.2k
      if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
8282
14
  {
8283
14
    as_bad (_("invalid character %s before operand %d"),
8284
14
      output_invalid (*l),
8285
14
      i.operands + 1);
8286
14
    return NULL;
8287
14
  }
8288
20.2k
      token_start = l;  /* After white space.  */
8289
343k
      while (in_quotes || paren_not_balanced || *l != ',')
8290
337k
  {
8291
337k
    if (*l == END_OF_INSN)
8292
12.2k
      {
8293
12.2k
        if (in_quotes)
8294
1.46k
    {
8295
1.46k
      as_bad (_("unbalanced double quotes in operand %d."),
8296
1.46k
        i.operands + 1);
8297
1.46k
      return NULL;
8298
1.46k
    }
8299
10.7k
        if (paren_not_balanced)
8300
569
    {
8301
569
      know (!intel_syntax);
8302
569
      as_bad (_("unbalanced parenthesis in operand %d."),
8303
569
        i.operands + 1);
8304
569
      return NULL;
8305
569
    }
8306
10.1k
        else
8307
10.1k
    break; /* we are done */
8308
10.7k
      }
8309
324k
    else if (*l == '\\' && l[1] == '"')
8310
0
      ++l;
8311
324k
    else if (*l == '"')
8312
5.13k
      in_quotes = !in_quotes;
8313
319k
    else if (!in_quotes && !is_operand_char (*l) && !is_whitespace (*l))
8314
1.55k
      {
8315
1.55k
        as_bad (_("invalid character %s in operand %d"),
8316
1.55k
          output_invalid (*l),
8317
1.55k
          i.operands + 1);
8318
1.55k
        return NULL;
8319
1.55k
      }
8320
323k
    if (!intel_syntax && !in_quotes)
8321
148k
      {
8322
148k
        if (*l == '(')
8323
307
    ++paren_not_balanced;
8324
148k
        if (*l == ')')
8325
858
    --paren_not_balanced;
8326
148k
      }
8327
323k
    l++;
8328
323k
  }
8329
16.6k
      if (l != token_start)
8330
16.1k
  {     /* Yes, we've read in another operand.  */
8331
16.1k
    unsigned int operand_ok;
8332
16.1k
    this_operand = i.operands++;
8333
16.1k
    if (i.operands > MAX_OPERANDS)
8334
8
      {
8335
8
        as_bad (_("spurious operands; (%d operands/instruction max)"),
8336
8
          MAX_OPERANDS);
8337
8
        return NULL;
8338
8
      }
8339
16.1k
    i.types[this_operand].bitfield.unspecified = 1;
8340
    /* Now parse operand adding info to 'i' as we go along.  */
8341
16.1k
    END_STRING_AND_SAVE (l);
8342
8343
16.1k
    if (i.mem_operands > 1)
8344
4
      {
8345
4
        as_bad (_("too many memory references for `%s'"),
8346
4
          mnemonic);
8347
4
        return 0;
8348
4
      }
8349
8350
16.1k
    if (intel_syntax)
8351
10.1k
      operand_ok =
8352
10.1k
        i386_intel_operand (token_start,
8353
10.1k
          intel_float_operand (mnemonic));
8354
5.98k
    else
8355
5.98k
      operand_ok = i386_att_operand (token_start);
8356
8357
16.1k
    RESTORE_END_STRING (l);
8358
16.1k
    if (!operand_ok)
8359
5.32k
      return NULL;
8360
16.1k
  }
8361
541
      else
8362
541
  {
8363
541
    if (expecting_operand)
8364
540
      {
8365
548
      expecting_operand_after_comma:
8366
548
        as_bad (_("expecting operand after ','; got nothing"));
8367
548
        return NULL;
8368
540
      }
8369
1
    if (*l == ',')
8370
0
      {
8371
0
        as_bad (_("expecting operand before ','; got nothing"));
8372
0
        return NULL;
8373
0
      }
8374
1
  }
8375
8376
      /* Now *l must be either ',' or END_OF_INSN.  */
8377
10.7k
      if (*l == ',')
8378
4.33k
  {
8379
4.33k
    if (*++l == END_OF_INSN)
8380
8
      {
8381
        /* Just skip it, if it's \n complain.  */
8382
8
        goto expecting_operand_after_comma;
8383
8
      }
8384
4.32k
    expecting_operand = 1;
8385
4.32k
  }
8386
10.7k
    }
8387
8.49k
  return l;
8388
17.9k
}
8389
8390
static void
8391
copy_operand (unsigned int to, unsigned int from)
8392
0
{
8393
0
  i.types[to] = i.types[from];
8394
0
  i.tm.operand_types[to] = i.tm.operand_types[from];
8395
0
  i.flags[to] = i.flags[from];
8396
0
  i.op[to] = i.op[from];
8397
0
  i.reloc[to] = i.reloc[from];
8398
0
  i.imm_bits[to] = i.imm_bits[from];
8399
  /* Note: i.mask and i.broadcast aren't handled here, as what (if
8400
     anything) to do there depends on context.  */
8401
0
}
8402
8403
static void
8404
swap_2_operands (unsigned int xchg1, unsigned int xchg2)
8405
1.01k
{
8406
1.01k
  union i386_op temp_op;
8407
1.01k
  i386_operand_type temp_type;
8408
1.01k
  unsigned int temp_flags;
8409
1.01k
  enum bfd_reloc_code_real temp_reloc;
8410
8411
1.01k
  temp_type = i.types[xchg2];
8412
1.01k
  i.types[xchg2] = i.types[xchg1];
8413
1.01k
  i.types[xchg1] = temp_type;
8414
8415
1.01k
  temp_flags = i.flags[xchg2];
8416
1.01k
  i.flags[xchg2] = i.flags[xchg1];
8417
1.01k
  i.flags[xchg1] = temp_flags;
8418
8419
1.01k
  temp_op = i.op[xchg2];
8420
1.01k
  i.op[xchg2] = i.op[xchg1];
8421
1.01k
  i.op[xchg1] = temp_op;
8422
8423
1.01k
  temp_reloc = i.reloc[xchg2];
8424
1.01k
  i.reloc[xchg2] = i.reloc[xchg1];
8425
1.01k
  i.reloc[xchg1] = temp_reloc;
8426
8427
1.01k
  temp_flags = i.imm_bits[xchg2];
8428
1.01k
  i.imm_bits[xchg2] = i.imm_bits[xchg1];
8429
1.01k
  i.imm_bits[xchg1] = temp_flags;
8430
8431
1.01k
  if (i.mask.reg)
8432
0
    {
8433
0
      if (i.mask.operand == xchg1)
8434
0
  i.mask.operand = xchg2;
8435
0
      else if (i.mask.operand == xchg2)
8436
0
  i.mask.operand = xchg1;
8437
0
    }
8438
1.01k
  if (i.broadcast.type || i.broadcast.bytes)
8439
0
    {
8440
0
      if (i.broadcast.operand == xchg1)
8441
0
  i.broadcast.operand = xchg2;
8442
0
      else if (i.broadcast.operand == xchg2)
8443
0
  i.broadcast.operand = xchg1;
8444
0
    }
8445
1.01k
}
8446
8447
static void
8448
swap_operands (void)
8449
1.01k
{
8450
1.01k
  switch (i.operands)
8451
1.01k
    {
8452
0
    case 5:
8453
0
    case 4:
8454
0
      swap_2_operands (1, i.operands - 2);
8455
      /* Fall through.  */
8456
4
    case 3:
8457
1.01k
    case 2:
8458
1.01k
      swap_2_operands (0, i.operands - 1);
8459
1.01k
      break;
8460
0
    default:
8461
0
      abort ();
8462
1.01k
    }
8463
8464
1.01k
  if (i.mem_operands == 2)
8465
4
    {
8466
4
      const reg_entry *temp_seg;
8467
4
      temp_seg = i.seg[0];
8468
4
      i.seg[0] = i.seg[1];
8469
4
      i.seg[1] = temp_seg;
8470
4
    }
8471
1.01k
}
8472
8473
/* Try to ensure constant immediates are represented in the smallest
8474
   opcode possible.  */
8475
static void
8476
optimize_imm (void)
8477
2.24k
{
8478
2.24k
  char guess_suffix = 0;
8479
2.24k
  int op;
8480
8481
2.24k
  if (i.suffix)
8482
1.04k
    guess_suffix = i.suffix;
8483
1.20k
  else if (i.reg_operands)
8484
48
    {
8485
      /* Figure out a suffix from the last register operand specified.
8486
   We can't do this properly yet, i.e. excluding special register
8487
   instances, but the following works for instructions with
8488
   immediates.  In any case, we can't set i.suffix yet.  */
8489
67
      for (op = i.operands; --op >= 0;)
8490
61
  if (i.types[op].bitfield.class != Reg)
8491
19
    continue;
8492
42
  else if (i.types[op].bitfield.byte)
8493
13
    {
8494
13
      guess_suffix = BYTE_MNEM_SUFFIX;
8495
13
      break;
8496
13
    }
8497
29
  else if (i.types[op].bitfield.word)
8498
22
    {
8499
22
      guess_suffix = WORD_MNEM_SUFFIX;
8500
22
      break;
8501
22
    }
8502
7
  else if (i.types[op].bitfield.dword)
8503
6
    {
8504
6
      guess_suffix = LONG_MNEM_SUFFIX;
8505
6
      break;
8506
6
    }
8507
1
  else if (i.types[op].bitfield.qword)
8508
1
    {
8509
1
      guess_suffix = QWORD_MNEM_SUFFIX;
8510
1
      break;
8511
1
    }
8512
48
    }
8513
1.15k
  else if ((flag_code == CODE_16BIT)
8514
1.15k
      ^ (i.prefix[DATA_PREFIX] != 0 && !(i.prefix[REX_PREFIX] & REX_W)))
8515
387
    guess_suffix = WORD_MNEM_SUFFIX;
8516
770
  else if (flag_code != CODE_64BIT
8517
504
     || (!(i.prefix[REX_PREFIX] & REX_W)
8518
         /* A more generic (but also more involved) way of dealing
8519
      with the special case(s) would be to go look for
8520
      DefaultSize attributes on any of the templates.  */
8521
504
         && current_templates.start->mnem_off != MN_push
8522
504
         && current_templates.start->mnem_off != MN_jmpabs))
8523
770
    guess_suffix = LONG_MNEM_SUFFIX;
8524
8525
5.55k
  for (op = i.operands; --op >= 0;)
8526
3.30k
    if (operand_type_check (i.types[op], imm))
8527
2.27k
      {
8528
2.27k
  switch (i.op[op].imms->X_op)
8529
2.27k
    {
8530
1.28k
    case O_constant:
8531
      /* If a suffix is given, this operand may be shortened.  */
8532
1.28k
      switch (guess_suffix)
8533
1.28k
        {
8534
749
        case LONG_MNEM_SUFFIX:
8535
749
    i.types[op].bitfield.imm32 = 1;
8536
749
    i.types[op].bitfield.imm64 = 1;
8537
749
    break;
8538
267
        case WORD_MNEM_SUFFIX:
8539
267
    i.types[op].bitfield.imm16 = 1;
8540
267
    i.types[op].bitfield.imm32 = 1;
8541
267
    i.types[op].bitfield.imm32s = 1;
8542
267
    i.types[op].bitfield.imm64 = 1;
8543
267
    break;
8544
270
        case BYTE_MNEM_SUFFIX:
8545
270
    i.types[op].bitfield.imm8 = 1;
8546
270
    i.types[op].bitfield.imm8s = 1;
8547
270
    i.types[op].bitfield.imm16 = 1;
8548
270
    i.types[op].bitfield.imm32 = 1;
8549
270
    i.types[op].bitfield.imm32s = 1;
8550
270
    i.types[op].bitfield.imm64 = 1;
8551
270
    break;
8552
1.28k
        }
8553
8554
      /* If this operand is at most 16 bits, convert it
8555
         to a signed 16 bit number before trying to see
8556
         whether it will fit in an even smaller size.
8557
         This allows a 16-bit operand such as $0xffe0 to
8558
         be recognised as within Imm8S range.  */
8559
1.28k
      if ((i.types[op].bitfield.imm16)
8560
537
    && fits_in_unsigned_word (i.op[op].imms->X_add_number))
8561
148
        {
8562
148
    i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
8563
148
            ^ 0x8000) - 0x8000);
8564
148
        }
8565
1.28k
#ifdef BFD64
8566
      /* Store 32-bit immediate in 64-bit for 64-bit BFD.  */
8567
1.28k
      if ((i.types[op].bitfield.imm32)
8568
1.28k
    && fits_in_unsigned_long (i.op[op].imms->X_add_number))
8569
955
        {
8570
955
    i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
8571
955
            ^ ((offsetT) 1 << 31))
8572
955
                 - ((offsetT) 1 << 31));
8573
955
        }
8574
1.28k
#endif
8575
1.28k
      i.types[op]
8576
1.28k
        = operand_type_or (i.types[op],
8577
1.28k
         smallest_imm_type (i.op[op].imms->X_add_number));
8578
8579
      /* We must avoid matching of Imm32 templates when 64bit
8580
         only immediate is available.  */
8581
1.28k
      if (guess_suffix == QWORD_MNEM_SUFFIX)
8582
0
        i.types[op].bitfield.imm32 = 0;
8583
1.28k
      break;
8584
8585
0
    case O_absent:
8586
0
    case O_register:
8587
0
      abort ();
8588
8589
      /* Symbols and expressions.  */
8590
989
    default:
8591
      /* Convert symbolic operand to proper sizes for matching, but don't
8592
         prevent matching a set of insns that only supports sizes other
8593
         than those matching the insn suffix.  */
8594
989
      {
8595
989
        i386_operand_type mask, allowed;
8596
989
        const insn_template *t = current_templates.start;
8597
8598
989
        operand_type_set (&mask, 0);
8599
989
        switch (guess_suffix)
8600
989
    {
8601
771
    case QWORD_MNEM_SUFFIX:
8602
771
      mask.bitfield.imm64 = 1;
8603
771
      mask.bitfield.imm32s = 1;
8604
771
      break;
8605
52
    case LONG_MNEM_SUFFIX:
8606
52
      mask.bitfield.imm32 = 1;
8607
52
      break;
8608
147
    case WORD_MNEM_SUFFIX:
8609
147
      mask.bitfield.imm16 = 1;
8610
147
      break;
8611
13
    case BYTE_MNEM_SUFFIX:
8612
13
      mask.bitfield.imm8 = 1;
8613
13
      break;
8614
6
    default:
8615
6
      break;
8616
989
    }
8617
8618
989
        allowed = operand_type_and (t->operand_types[op], mask);
8619
9.82k
        while (++t < current_templates.end)
8620
8.83k
    {
8621
8.83k
      allowed = operand_type_or (allowed, t->operand_types[op]);
8622
8.83k
      allowed = operand_type_and (allowed, mask);
8623
8.83k
    }
8624
8625
989
        if (!operand_type_all_zero (&allowed))
8626
955
    i.types[op] = operand_type_and (i.types[op], mask);
8627
989
      }
8628
0
      break;
8629
2.27k
    }
8630
2.27k
      }
8631
2.24k
}
8632
8633
/* Try to use the smallest displacement type too.  */
8634
static bool
8635
optimize_disp (const insn_template *t)
8636
5.10k
{
8637
5.10k
  unsigned int op;
8638
8639
5.10k
  if (!want_disp32 (t)
8640
2.83k
      && (!t->opcode_modifier.jump
8641
257
    || i.jumpabsolute || i.types[0].bitfield.baseindex))
8642
2.58k
    {
8643
6.15k
      for (op = i.imm_operands; op < i.operands; ++op)
8644
3.57k
  {
8645
3.57k
    const expressionS *exp = i.op[op].disps;
8646
8647
3.57k
    if (!operand_type_check (i.types[op], disp))
8648
205
      continue;
8649
8650
3.36k
    if (exp->X_op != O_constant)
8651
2.88k
      continue;
8652
8653
    /* Since displacement is signed extended to 64bit, don't allow
8654
       disp32 if it is out of range.  */
8655
483
    if (fits_in_signed_long (exp->X_add_number))
8656
428
      continue;
8657
8658
55
    i.types[op].bitfield.disp32 = 0;
8659
55
    if (i.types[op].bitfield.baseindex)
8660
0
      {
8661
0
        as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
8662
0
          (uint64_t) exp->X_add_number);
8663
0
        return false;
8664
0
      }
8665
55
  }
8666
2.58k
    }
8667
8668
  /* Don't optimize displacement for movabs / jmpabs since they only take
8669
     64-bit displacement.  */
8670
5.10k
  if (pp.disp_encoding > disp_encoding_8bit
8671
5.10k
      || t->mnem_off == MN_movabs || t->mnem_off == MN_jmpabs)
8672
0
    return true;
8673
8674
12.2k
  for (op = i.operands; op-- > 0;)
8675
7.10k
    if (operand_type_check (i.types[op], disp))
8676
5.93k
      {
8677
5.93k
  if (i.op[op].disps->X_op == O_constant)
8678
597
    {
8679
597
      offsetT op_disp = i.op[op].disps->X_add_number;
8680
8681
597
      if (!op_disp && i.types[op].bitfield.baseindex)
8682
0
        {
8683
0
    i.types[op] = operand_type_and_not (i.types[op], anydisp);
8684
0
    i.op[op].disps = NULL;
8685
0
    i.disp_operands--;
8686
0
    continue;
8687
0
        }
8688
8689
597
      if (i.types[op].bitfield.disp16
8690
1
    && fits_in_unsigned_word (op_disp))
8691
1
        {
8692
    /* If this operand is at most 16 bits, convert
8693
       to a signed 16 bit number and don't use 64bit
8694
       displacement.  */
8695
1
    op_disp = ((op_disp ^ 0x8000) - 0x8000);
8696
1
    i.types[op].bitfield.disp64 = 0;
8697
1
        }
8698
8699
597
#ifdef BFD64
8700
      /* Optimize 64-bit displacement to 32-bit for 64-bit BFD.  */
8701
597
      if ((flag_code != CODE_64BIT
8702
597
     ? i.types[op].bitfield.disp32
8703
597
     : want_disp32 (t)
8704
0
       && (!t->opcode_modifier.jump
8705
0
           || i.jumpabsolute || i.types[op].bitfield.baseindex))
8706
35
    && fits_in_unsigned_long (op_disp))
8707
35
        {
8708
    /* If this operand is at most 32 bits, convert
8709
       to a signed 32 bit number and don't use 64bit
8710
       displacement.  */
8711
35
    op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
8712
35
    i.types[op].bitfield.disp64 = 0;
8713
35
    i.types[op].bitfield.disp32 = 1;
8714
35
        }
8715
8716
597
      if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
8717
504
        {
8718
504
    i.types[op].bitfield.disp64 = 0;
8719
504
    i.types[op].bitfield.disp32 = 1;
8720
504
        }
8721
597
#endif
8722
597
      if ((i.types[op].bitfield.disp32
8723
55
     || i.types[op].bitfield.disp16)
8724
542
    && fits_in_disp8 (op_disp))
8725
487
        i.types[op].bitfield.disp8 = 1;
8726
8727
597
      i.op[op].disps->X_add_number = op_disp;
8728
597
    }
8729
5.33k
  else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8730
5.33k
     || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
8731
385
    {
8732
385
      fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
8733
385
       i.op[op].disps, 0, i.reloc[op]);
8734
385
      i.types[op] = operand_type_and_not (i.types[op], anydisp);
8735
385
    }
8736
4.94k
  else
8737
    /* We only support 64bit displacement on constants.  */
8738
4.94k
    i.types[op].bitfield.disp64 = 0;
8739
5.93k
      }
8740
8741
5.10k
  return true;
8742
5.10k
}
8743
8744
/* Return 1 if there is a match in broadcast bytes between operand
8745
   GIVEN and instruction template T.   */
8746
8747
static INLINE int
8748
match_broadcast_size (const insn_template *t, unsigned int given)
8749
0
{
8750
0
  return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
8751
0
     && i.types[given].bitfield.byte)
8752
0
    || (t->opcode_modifier.broadcast == WORD_BROADCAST
8753
0
        && i.types[given].bitfield.word)
8754
0
    || (t->opcode_modifier.broadcast == DWORD_BROADCAST
8755
0
        && i.types[given].bitfield.dword)
8756
0
    || (t->opcode_modifier.broadcast == QWORD_BROADCAST
8757
0
        && i.types[given].bitfield.qword));
8758
0
}
8759
8760
/* Check if operands are valid for the instruction.  */
8761
8762
static int
8763
check_VecOperands (const insn_template *t)
8764
1.55k
{
8765
1.55k
  unsigned int op;
8766
1.55k
  i386_cpu_flags cpu;
8767
8768
  /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
8769
     any one operand are implicity requiring AVX512VL support if the actual
8770
     operand size is YMMword or XMMword.  Since this function runs after
8771
     template matching, there's no need to check for YMMword/XMMword in
8772
     the template.  */
8773
1.55k
  cpu = cpu_flags_and (cpu_flags_from_attr (t->cpu), avx512);
8774
1.55k
  if (!cpu_flags_all_zero (&cpu)
8775
0
      && !is_cpu (t, CpuAVX512VL)
8776
0
      && !cpu_arch_flags.bitfield.cpuavx512vl
8777
0
      && (!t->opcode_modifier.vex || need_evex_encoding (t)))
8778
0
    {
8779
0
      for (op = 0; op < t->operands; ++op)
8780
0
  {
8781
0
    if (t->operand_types[op].bitfield.zmmword
8782
0
        && (i.types[op].bitfield.ymmword
8783
0
      || i.types[op].bitfield.xmmword))
8784
0
      {
8785
0
        i.error = operand_size_mismatch;
8786
0
        return 1;
8787
0
      }
8788
0
  }
8789
0
    }
8790
8791
  /* Somewhat similarly, templates specifying both AVX and AVX2 are
8792
     requiring AVX2 support if the actual operand size is YMMword.  */
8793
1.55k
  if (maybe_cpu (t, CpuAVX) && maybe_cpu (t, CpuAVX2)
8794
0
      && !cpu_arch_flags.bitfield.cpuavx2)
8795
0
    {
8796
0
      for (op = 0; op < t->operands; ++op)
8797
0
  {
8798
0
    if (t->operand_types[op].bitfield.xmmword
8799
0
        && i.types[op].bitfield.ymmword)
8800
0
      {
8801
0
        i.error = operand_size_mismatch;
8802
0
        return 1;
8803
0
      }
8804
0
  }
8805
0
    }
8806
8807
  /* Without VSIB byte, we can't have a vector register for index.  */
8808
1.55k
  if (!t->opcode_modifier.sib
8809
1.55k
      && i.index_reg
8810
0
      && (i.index_reg->reg_type.bitfield.xmmword
8811
0
    || i.index_reg->reg_type.bitfield.ymmword
8812
0
    || i.index_reg->reg_type.bitfield.zmmword))
8813
0
    {
8814
0
      i.error = unsupported_vector_index_register;
8815
0
      return 1;
8816
0
    }
8817
8818
  /* Check if default mask is allowed.  */
8819
1.55k
  if (t->opcode_modifier.operandconstraint == NO_DEFAULT_MASK
8820
0
      && (!i.mask.reg || i.mask.reg->reg_num == 0))
8821
0
    {
8822
0
      i.error = no_default_mask;
8823
0
      return 1;
8824
0
    }
8825
8826
  /* For VSIB byte, we need a vector register for index, and all vector
8827
     registers must be distinct.  */
8828
1.55k
  if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
8829
0
    {
8830
0
      if (!i.index_reg
8831
0
    || !((t->opcode_modifier.sib == VECSIB128
8832
0
    && i.index_reg->reg_type.bitfield.xmmword)
8833
0
         || (t->opcode_modifier.sib == VECSIB256
8834
0
       && i.index_reg->reg_type.bitfield.ymmword)
8835
0
         || (t->opcode_modifier.sib == VECSIB512
8836
0
       && i.index_reg->reg_type.bitfield.zmmword)))
8837
0
      {
8838
0
  i.error = invalid_vsib_address;
8839
0
  return 1;
8840
0
      }
8841
8842
0
      gas_assert (i.reg_operands == 2 || i.mask.reg);
8843
0
      if (i.reg_operands == 2 && !i.mask.reg)
8844
0
  {
8845
0
    gas_assert (i.types[0].bitfield.class == RegSIMD);
8846
0
    gas_assert (i.types[0].bitfield.xmmword
8847
0
          || i.types[0].bitfield.ymmword);
8848
0
    gas_assert (i.types[2].bitfield.class == RegSIMD);
8849
0
    gas_assert (i.types[2].bitfield.xmmword
8850
0
          || i.types[2].bitfield.ymmword);
8851
0
    if (operand_check == check_none)
8852
0
      return 0;
8853
0
    if (register_number (i.op[0].regs)
8854
0
        != register_number (i.index_reg)
8855
0
        && register_number (i.op[2].regs)
8856
0
     != register_number (i.index_reg)
8857
0
        && register_number (i.op[0].regs)
8858
0
     != register_number (i.op[2].regs))
8859
0
      return 0;
8860
0
    if (operand_check == check_error)
8861
0
      {
8862
0
        i.error = invalid_vector_register_set;
8863
0
        return 1;
8864
0
      }
8865
0
    as_warn (_("mask, index, and destination registers should be distinct"));
8866
0
  }
8867
0
      else if (i.reg_operands == 1 && i.mask.reg)
8868
0
  {
8869
0
    if (i.types[1].bitfield.class == RegSIMD
8870
0
        && (i.types[1].bitfield.xmmword
8871
0
            || i.types[1].bitfield.ymmword
8872
0
            || i.types[1].bitfield.zmmword)
8873
0
        && (register_number (i.op[1].regs)
8874
0
      == register_number (i.index_reg)))
8875
0
      {
8876
0
        if (operand_check == check_error)
8877
0
    {
8878
0
      i.error = invalid_vector_register_set;
8879
0
      return 1;
8880
0
    }
8881
0
        if (operand_check != check_none)
8882
0
    as_warn (_("index and destination registers should be distinct"));
8883
0
      }
8884
0
  }
8885
0
    }
8886
8887
  /* For AMX instructions with 3 TMM register operands, all operands
8888
      must be distinct.  */
8889
1.55k
  if (i.reg_operands == 3
8890
0
      && t->operand_types[0].bitfield.tmmword
8891
0
      && (i.op[0].regs == i.op[1].regs
8892
0
          || i.op[0].regs == i.op[2].regs
8893
0
          || i.op[1].regs == i.op[2].regs))
8894
0
    {
8895
0
      i.error = invalid_tmm_register_set;
8896
0
      return 1;
8897
0
    }
8898
8899
  /* For some special instructions require that destination must be distinct
8900
     from source registers.  */
8901
1.55k
  if (t->opcode_modifier.operandconstraint == DISTINCT_DEST)
8902
0
    {
8903
0
      unsigned int dest_reg = i.operands - 1;
8904
8905
0
      know (i.operands >= 3);
8906
8907
      /* #UD if dest_reg == src1_reg or dest_reg == src2_reg.  */
8908
0
      if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
8909
0
    || (i.reg_operands > 2
8910
0
        && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
8911
0
  {
8912
0
    i.error = invalid_dest_and_src_register_set;
8913
0
    return 1;
8914
0
  }
8915
0
    }
8916
8917
  /* Check if broadcast is supported by the instruction and is applied
8918
     to the memory operand.  */
8919
1.55k
  if (i.broadcast.type || i.broadcast.bytes)
8920
0
    {
8921
0
      i386_operand_type type, overlap;
8922
8923
      /* Check if specified broadcast is supported in this instruction,
8924
   and its broadcast bytes match the memory operand.  */
8925
0
      op = i.broadcast.operand;
8926
0
      if (!t->opcode_modifier.broadcast
8927
0
    || !(i.flags[op] & Operand_Mem)
8928
0
    || (!i.types[op].bitfield.unspecified
8929
0
        && !match_broadcast_size (t, op)))
8930
0
  {
8931
0
  bad_broadcast:
8932
0
    i.error = unsupported_broadcast;
8933
0
    return 1;
8934
0
  }
8935
8936
0
      operand_type_set (&type, 0);
8937
0
      switch (get_broadcast_bytes (t, false))
8938
0
  {
8939
0
  case 2:
8940
0
    type.bitfield.word = 1;
8941
0
    break;
8942
0
  case 4:
8943
0
    type.bitfield.dword = 1;
8944
0
    break;
8945
0
  case 8:
8946
0
    type.bitfield.qword = 1;
8947
0
    break;
8948
0
  case 16:
8949
0
    type.bitfield.xmmword = 1;
8950
0
    break;
8951
0
  case 32:
8952
0
    if (vector_size < VSZ256)
8953
0
      goto bad_broadcast;
8954
0
    type.bitfield.ymmword = 1;
8955
0
    break;
8956
0
  case 64:
8957
0
    if (vector_size < VSZ512)
8958
0
      goto bad_broadcast;
8959
0
    type.bitfield.zmmword = 1;
8960
0
    break;
8961
0
  default:
8962
0
    goto bad_broadcast;
8963
0
  }
8964
8965
0
      overlap = operand_type_and (type, t->operand_types[op]);
8966
0
      if (t->operand_types[op].bitfield.class == RegSIMD
8967
0
    && t->operand_types[op].bitfield.byte
8968
0
       + t->operand_types[op].bitfield.word
8969
0
       + t->operand_types[op].bitfield.dword
8970
0
       + t->operand_types[op].bitfield.qword > 1)
8971
0
  {
8972
0
    overlap.bitfield.xmmword = 0;
8973
0
    overlap.bitfield.ymmword = 0;
8974
0
    overlap.bitfield.zmmword = 0;
8975
0
  }
8976
0
      if (operand_type_all_zero (&overlap))
8977
0
    goto bad_broadcast;
8978
8979
0
      if (t->opcode_modifier.checkoperandsize)
8980
0
  {
8981
0
    unsigned int j;
8982
8983
0
    type.bitfield.baseindex = 1;
8984
0
    for (j = i.imm_operands; j < i.operands; ++j)
8985
0
      {
8986
0
        if (j != op
8987
0
      && !operand_type_register_match(i.types[j],
8988
0
              t->operand_types[j],
8989
0
              type,
8990
0
              t->operand_types[op]))
8991
0
    goto bad_broadcast;
8992
0
      }
8993
0
  }
8994
0
    }
8995
  /* If broadcast is supported in this instruction, we need to check if
8996
     operand of one-element size isn't specified without broadcast.  */
8997
1.55k
  else if (t->opcode_modifier.broadcast && i.mem_operands)
8998
0
    {
8999
      /* Find memory operand.  */
9000
0
      for (op = i.imm_operands; op < i.operands; op++)
9001
0
  if (i.flags[op] & Operand_Mem)
9002
0
    break;
9003
0
      gas_assert (op < i.operands);
9004
      /* Check size of the memory operand.  */
9005
0
      if (match_broadcast_size (t, op))
9006
0
  {
9007
0
    i.error = broadcast_needed;
9008
0
    return 1;
9009
0
  }
9010
0
    }
9011
1.55k
  else
9012
1.55k
    op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning.  */
9013
9014
  /* Check if requested masking is supported.  */
9015
1.55k
  if (i.mask.reg)
9016
0
    {
9017
0
      if (!t->opcode_modifier.masking)
9018
0
  {
9019
0
    i.error = unsupported_masking;
9020
0
    return 1;
9021
0
  }
9022
9023
      /* Common rules for masking:
9024
   - mask register destinations permit only zeroing-masking, without
9025
     that actually being expressed by a {z} operand suffix or EVEX.z,
9026
   - memory destinations allow only merging-masking,
9027
   - scatter/gather insns (i.e. ones using vSIB) only allow merging-
9028
     masking.  */
9029
0
      if (i.mask.zeroing
9030
0
    && (t->operand_types[t->operands - 1].bitfield.class == RegMask
9031
0
        || (i.flags[t->operands - 1] & Operand_Mem)
9032
0
        || t->opcode_modifier.sib))
9033
0
  {
9034
0
    i.error = unsupported_masking;
9035
0
    return 1;
9036
0
  }
9037
0
    }
9038
9039
  /* Check if masking is applied to dest operand.  */
9040
1.55k
  if (i.mask.reg && (i.mask.operand != i.operands - 1))
9041
0
    {
9042
0
      i.error = mask_not_on_destination;
9043
0
      return 1;
9044
0
    }
9045
9046
  /* Check RC/SAE.  */
9047
1.55k
  if (i.rounding.type != rc_none)
9048
0
    {
9049
0
      if (!t->opcode_modifier.sae
9050
0
    || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
9051
0
    || i.mem_operands)
9052
0
  {
9053
0
    i.error = unsupported_rc_sae;
9054
0
    return 1;
9055
0
  }
9056
9057
      /* Non-EVEX.{LIG,512} forms need to have a ZMM or YMM register as at
9058
   least one operand.  There's no need to check all operands, though:
9059
   Either of the last two operands will be of the right size in all
9060
   relevant templates.  */
9061
0
      if (t->opcode_modifier.evex != EVEXLIG
9062
0
    && t->opcode_modifier.evex != EVEX512
9063
0
    && !i.types[t->operands - 1].bitfield.zmmword
9064
0
    && !i.types[t->operands - 2].bitfield.zmmword)
9065
0
  {
9066
0
    i.error = operand_size_mismatch;
9067
0
    return 1;
9068
0
  }
9069
0
    }
9070
9071
  /* Check the special Imm4 cases; must be the first operand.  */
9072
1.55k
  if ((is_cpu (t, CpuXOP) && t->operands == 5)
9073
1.55k
      || (t->opcode_space == SPACE_0F3A
9074
0
    && (t->base_opcode | 3) == 0x0b
9075
0
    && (is_cpu (t, CpuAPX_F)
9076
0
     || (t->opcode_modifier.sse2avx && t->opcode_modifier.evex
9077
0
         && (!t->opcode_modifier.vex
9078
0
       || (pp.encoding != encoding_default
9079
0
           && pp.encoding != encoding_vex
9080
0
           && pp.encoding != encoding_vex3))))))
9081
0
    {
9082
0
      if (i.op[0].imms->X_op != O_constant
9083
0
    || !fits_in_imm4 (i.op[0].imms->X_add_number))
9084
0
  {
9085
0
    i.error = bad_imm4;
9086
0
    return 1;
9087
0
  }
9088
9089
      /* Turn off Imm<N> so that update_imm won't complain.  */
9090
0
      if (t->operands == 5)
9091
0
  operand_type_set (&i.types[0], 0);
9092
0
    }
9093
9094
  /* Check vector Disp8 operand.  */
9095
1.55k
  if (t->opcode_modifier.disp8memshift
9096
0
      && (!t->opcode_modifier.vex
9097
0
    || need_evex_encoding (t))
9098
0
      && pp.disp_encoding <= disp_encoding_8bit)
9099
0
    {
9100
0
      if (i.broadcast.type || i.broadcast.bytes)
9101
0
  i.memshift = t->opcode_modifier.broadcast - 1;
9102
0
      else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
9103
0
  i.memshift = t->opcode_modifier.disp8memshift;
9104
0
      else
9105
0
  {
9106
0
    const i386_operand_type *type = NULL, *fallback = NULL;
9107
9108
0
    i.memshift = 0;
9109
0
    for (op = i.imm_operands; op < i.operands; op++)
9110
0
      if (i.flags[op] & Operand_Mem)
9111
0
        {
9112
0
    if (t->opcode_modifier.evex == EVEXLIG)
9113
0
      i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
9114
0
    else if (t->operand_types[op].bitfield.xmmword
9115
0
       + t->operand_types[op].bitfield.ymmword
9116
0
       + t->operand_types[op].bitfield.zmmword <= 1)
9117
0
      type = &t->operand_types[op];
9118
0
    else if (!i.types[op].bitfield.unspecified)
9119
0
      type = &i.types[op];
9120
0
    else /* Ambiguities get resolved elsewhere.  */
9121
0
      fallback = &t->operand_types[op];
9122
0
        }
9123
0
      else if (i.types[op].bitfield.class == RegSIMD
9124
0
         && t->opcode_modifier.evex != EVEXLIG)
9125
0
        {
9126
0
    if (i.types[op].bitfield.zmmword)
9127
0
      i.memshift = 6;
9128
0
    else if (i.types[op].bitfield.ymmword && i.memshift < 5)
9129
0
      i.memshift = 5;
9130
0
    else if (i.types[op].bitfield.xmmword && i.memshift < 4)
9131
0
      i.memshift = 4;
9132
0
        }
9133
9134
0
    if (!type && !i.memshift)
9135
0
      type = fallback;
9136
0
    if (type)
9137
0
      {
9138
0
        if (type->bitfield.zmmword)
9139
0
    i.memshift = 6;
9140
0
        else if (type->bitfield.ymmword)
9141
0
    i.memshift = 5;
9142
0
        else if (type->bitfield.xmmword)
9143
0
    i.memshift = 4;
9144
0
      }
9145
9146
    /* For the check in fits_in_disp8().  */
9147
0
    if (i.memshift == 0)
9148
0
      i.memshift = -1;
9149
0
  }
9150
9151
0
      for (op = i.imm_operands; op < i.operands; op++)
9152
0
  if (operand_type_check (i.types[op], disp)
9153
0
      && i.op[op].disps->X_op == O_constant)
9154
0
    {
9155
      /* Make sure to leave i.types[op].bitfield.disp8 alone upon
9156
         secondary invocations of match_template().  */
9157
0
      if (fits_in_disp8 (i.op[op].disps->X_add_number))
9158
0
        {
9159
0
    if (!i.tm.mnem_off)
9160
0
      i.types[op].bitfield.disp8 = 1;
9161
0
    return 0;
9162
0
        }
9163
0
      if (!i.tm.mnem_off)
9164
0
        i.types[op].bitfield.disp8 = 0;
9165
0
    }
9166
0
    }
9167
9168
1.55k
  i.memshift = 0;
9169
9170
1.55k
  return 0;
9171
1.55k
}
9172
9173
/* Check if encoding requirements are met by the instruction.  */
9174
9175
static int
9176
VEX_check_encoding (const insn_template *t)
9177
2.69k
{
9178
2.69k
  if (pp.encoding == encoding_error)
9179
0
    {
9180
0
      i.error = unsupported;
9181
0
      return 1;
9182
0
    }
9183
9184
  /* Vector size restrictions.  */
9185
2.69k
  if ((vector_size < VSZ512
9186
0
       && t->opcode_modifier.evex == EVEX512)
9187
2.69k
      || (vector_size < VSZ256
9188
0
    && (t->opcode_modifier.evex == EVEX256
9189
0
        || t->opcode_modifier.vex == VEX256)))
9190
0
    {
9191
0
      i.error = unsupported_vector_size;
9192
0
      return 1;
9193
0
    }
9194
9195
2.69k
  switch (pp.encoding)
9196
2.69k
    {
9197
35
    case encoding_vex:
9198
35
    case encoding_vex3:
9199
      /* This instruction must be encoded with VEX prefix.  */
9200
35
      if (!t->opcode_modifier.vex)
9201
35
  {
9202
35
    i.error = no_vex_encoding;
9203
35
    return 1;
9204
35
  }
9205
0
      break;
9206
9207
2.65k
    case encoding_default:
9208
2.65k
      if (!pp.has_nf)
9209
2.65k
  break;
9210
      /* Fall through.  */
9211
0
    case encoding_evex:
9212
0
    case encoding_evex512:
9213
      /* This instruction must be encoded with EVEX prefix.  */
9214
0
      if (!t->opcode_modifier.evex)
9215
0
  {
9216
0
    i.error = no_evex_encoding;
9217
0
    return 1;
9218
0
  }
9219
0
      break;
9220
9221
7
    case encoding_egpr:
9222
      /* This instruction must be encoded with REX2 or EVEX prefix.  */
9223
7
      if (t->opcode_modifier.vex && !t->opcode_modifier.evex)
9224
0
  {
9225
0
    i.error = no_evex_encoding;
9226
0
    return 1;
9227
0
  }
9228
7
      break;
9229
9230
7
    default:
9231
0
      abort ();
9232
2.69k
    }
9233
9234
2.66k
  return 0;
9235
2.69k
}
9236
9237
/* Check if Egprs operands are valid for the instruction.  */
9238
9239
static bool
9240
check_EgprOperands (const insn_template *t)
9241
1.55k
{
9242
1.55k
  if (!t->opcode_modifier.noegpr)
9243
1.27k
    return false;
9244
9245
545
  for (unsigned int op = i.imm_operands; op < i.operands; op++)
9246
264
    {
9247
264
      if (i.types[op].bitfield.class != Reg)
9248
264
  continue;
9249
9250
0
      if (i.op[op].regs->reg_flags & RegRex2)
9251
0
  {
9252
0
    i.error = register_type_mismatch;
9253
0
    return true;
9254
0
  }
9255
0
    }
9256
9257
281
  if ((i.index_reg && (i.index_reg->reg_flags & RegRex2))
9258
281
      || (i.base_reg && (i.base_reg->reg_flags & RegRex2)))
9259
0
    {
9260
0
      i.error = unsupported_EGPR_for_addressing;
9261
0
      return true;
9262
0
    }
9263
9264
  /* Check if pseudo prefix {rex2} is valid.  */
9265
281
  if (pp.rex2_encoding && !t->opcode_modifier.sse2avx)
9266
0
    {
9267
0
      i.error = invalid_pseudo_prefix;
9268
0
      return true;
9269
0
    }
9270
9271
281
  return false;
9272
281
}
9273
9274
/* Check if APX operands are valid for the instruction.  */
9275
static bool
9276
check_APX_operands (const insn_template *t)
9277
1.55k
{
9278
  /* Push2* and Pop2* cannot use RSP and Pop2* cannot pop two same registers.
9279
   */
9280
1.55k
  switch (t->mnem_off)
9281
1.55k
    {
9282
0
    case MN_pop2:
9283
0
    case MN_pop2p:
9284
0
      if (register_number (i.op[0].regs) == register_number (i.op[1].regs))
9285
0
  {
9286
0
    i.error = invalid_dest_register_set;
9287
0
    return 1;
9288
0
  }
9289
    /* fall through */
9290
0
    case MN_push2:
9291
0
    case MN_push2p:
9292
0
      if (register_number (i.op[0].regs) == 4
9293
0
    || register_number (i.op[1].regs) == 4)
9294
0
  {
9295
0
    i.error = unsupported_rsp_register;
9296
0
    return 1;
9297
0
  }
9298
0
      break;
9299
1.55k
    }
9300
1.55k
  return 0;
9301
1.55k
}
9302
9303
/* Check if the instruction use the REX registers or REX prefix.  */
9304
static bool
9305
check_Rex_required (void)
9306
0
{
9307
0
  for (unsigned int op = i.imm_operands; op < i.operands; op++)
9308
0
    {
9309
0
      if (i.types[op].bitfield.class != Reg)
9310
0
  continue;
9311
9312
0
      if (i.op[op].regs->reg_flags & (RegRex | RegRex64))
9313
0
  return true;
9314
0
    }
9315
9316
0
  if ((i.index_reg && (i.index_reg->reg_flags & RegRex))
9317
0
      || (i.base_reg && (i.base_reg->reg_flags & RegRex)))
9318
0
    return true;
9319
9320
  /* Check pseudo prefix {rex} are valid.  */
9321
0
  return pp.rex_encoding;
9322
0
}
9323
9324
/* Optimize APX NDD insns to legacy insns.  */
9325
static unsigned int
9326
can_convert_NDD_to_legacy (const insn_template *t)
9327
0
{
9328
0
  unsigned int match_dest_op = ~0;
9329
9330
0
  if (!pp.has_nf && i.reg_operands >= 2)
9331
0
    {
9332
0
      unsigned int dest = i.operands - 1;
9333
0
      unsigned int src1 = i.operands - 2;
9334
0
      unsigned int src2 = (i.operands > 3) ? i.operands - 3 : 0;
9335
9336
0
      if (i.types[src1].bitfield.class == Reg
9337
0
    && i.op[src1].regs == i.op[dest].regs)
9338
0
  match_dest_op = src1;
9339
      /* If the first operand is the same as the third operand,
9340
   these instructions need to support the ability to commutative
9341
   the first two operands and still not change the semantics in order
9342
   to be optimized.  */
9343
0
      else if (optimize > 1
9344
0
         && t->opcode_modifier.commutative
9345
0
         && i.types[src2].bitfield.class == Reg
9346
0
         && i.op[src2].regs == i.op[dest].regs)
9347
0
  match_dest_op = src2;
9348
0
    }
9349
0
  return match_dest_op;
9350
0
}
9351
9352
/* Helper function for the progress() macro in match_template().  */
9353
static INLINE enum i386_error progress (enum i386_error new,
9354
          enum i386_error last,
9355
          unsigned int line, unsigned int *line_p)
9356
66.4k
{
9357
66.4k
  if (line <= *line_p)
9358
31.3k
    return last;
9359
35.1k
  *line_p = line;
9360
35.1k
  return new;
9361
66.4k
}
9362
9363
static const insn_template *
9364
match_template (char mnem_suffix)
9365
9.23k
{
9366
  /* Points to template once we've found it.  */
9367
9.23k
  const insn_template *t;
9368
9.23k
  i386_operand_type overlap0, overlap1, overlap2, overlap3;
9369
9.23k
  i386_operand_type overlap4;
9370
9.23k
  unsigned int found_reverse_match;
9371
9.23k
  i386_operand_type operand_types [MAX_OPERANDS];
9372
9.23k
  int addr_prefix_disp;
9373
9.23k
  unsigned int j, size_match, check_register, errline = __LINE__;
9374
9.23k
  enum i386_error specific_error = number_of_operands_mismatch;
9375
66.4k
#define progress(err) progress (err, specific_error, __LINE__, &errline)
9376
9377
#if MAX_OPERANDS != 5
9378
# error "MAX_OPERANDS must be 5."
9379
#endif
9380
9381
9.23k
  found_reverse_match = 0;
9382
9.23k
  addr_prefix_disp = -1;
9383
9384
62.8k
  for (t = current_templates.start; t < current_templates.end; t++)
9385
56.3k
    {
9386
56.3k
      addr_prefix_disp = -1;
9387
56.3k
      found_reverse_match = 0;
9388
9389
      /* Must have right number of operands.  */
9390
56.3k
      if (i.operands != t->operands)
9391
45.5k
  continue;
9392
9393
      /* Skip SSE2AVX templates when inapplicable.  */
9394
10.7k
      if (t->opcode_modifier.sse2avx
9395
406
    && (!sse2avx || i.prefix[DATA_PREFIX]))
9396
406
  {
9397
    /* Another non-SSE2AVX template has to follow.  */
9398
406
    gas_assert (t + 1 < current_templates.end);
9399
406
    continue;
9400
406
  }
9401
9402
      /* Check processor support.  */
9403
10.3k
      specific_error = progress (unsupported);
9404
10.3k
      if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
9405
2.38k
  continue;
9406
9407
      /* Check AT&T mnemonic.   */
9408
7.99k
      specific_error = progress (unsupported_with_intel_mnemonic);
9409
7.99k
      if (!intel_syntax && intel_mnemonic
9410
0
    && t->opcode_modifier.dialect == ATT_MNEMONIC)
9411
0
  continue;
9412
9413
      /* Check AT&T/Intel syntax.  */
9414
7.99k
      specific_error = progress (unsupported_syntax);
9415
7.99k
      if (intel_syntax
9416
7.99k
     ? t->opcode_modifier.dialect >= ATT_SYNTAX
9417
7.99k
     : t->opcode_modifier.dialect == INTEL_SYNTAX)
9418
0
  continue;
9419
9420
      /* Check NF support.  */
9421
7.99k
      specific_error = progress (unsupported_nf);
9422
7.99k
      if (pp.has_nf && !t->opcode_modifier.nf)
9423
4
  continue;
9424
9425
      /* Check Intel64/AMD64 ISA.   */
9426
7.98k
      switch (isa64)
9427
7.98k
  {
9428
7.98k
  default:
9429
    /* Default: Don't accept Intel64.  */
9430
7.98k
    if (t->opcode_modifier.isa64 == INTEL64)
9431
37
      continue;
9432
7.95k
    break;
9433
7.95k
  case amd64:
9434
    /* -mamd64: Don't accept Intel64 and Intel64 only.  */
9435
0
    if (t->opcode_modifier.isa64 >= INTEL64)
9436
0
      continue;
9437
0
    break;
9438
0
  case intel64:
9439
    /* -mintel64: Don't accept AMD64.  */
9440
0
    if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
9441
0
      continue;
9442
0
    break;
9443
7.98k
  }
9444
9445
      /* Check the suffix.  */
9446
7.95k
      specific_error = progress (invalid_instruction_suffix);
9447
7.95k
      if ((t->opcode_modifier.no_bsuf && mnem_suffix == BYTE_MNEM_SUFFIX)
9448
7.94k
    || (t->opcode_modifier.no_wsuf && mnem_suffix == WORD_MNEM_SUFFIX)
9449
7.94k
    || (t->opcode_modifier.no_lsuf && mnem_suffix == LONG_MNEM_SUFFIX)
9450
7.93k
    || (t->opcode_modifier.no_ssuf && mnem_suffix == SHORT_MNEM_SUFFIX)
9451
6.39k
    || (t->opcode_modifier.no_qsuf && mnem_suffix == QWORD_MNEM_SUFFIX))
9452
1.55k
  continue;
9453
9454
6.39k
      specific_error = progress (operand_size_mismatch);
9455
6.39k
      size_match = operand_size_match (t);
9456
6.39k
      if (!size_match)
9457
43
  continue;
9458
9459
      /* This is intentionally not
9460
9461
   if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
9462
9463
   as the case of a missing * on the operand is accepted (perhaps with
9464
   a warning, issued further down).  */
9465
6.35k
      specific_error = progress (operand_type_mismatch);
9466
6.35k
      if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
9467
17
  continue;
9468
9469
      /* In Intel syntax, normally we can check for memory operand size when
9470
   there is no mnemonic suffix.  But jmp and call have 2 different
9471
   encodings with Dword memory operand size.  Skip the "near" one
9472
   (permitting a register operand) when "far" was requested.  */
9473
6.33k
      if (i.far_branch
9474
0
    && t->opcode_modifier.jump == JUMP_ABSOLUTE
9475
0
    && t->operand_types[0].bitfield.class == Reg)
9476
0
  continue;
9477
9478
38.0k
      for (j = 0; j < MAX_OPERANDS; j++)
9479
31.6k
  operand_types[j] = t->operand_types[j];
9480
9481
      /* In general, don't allow 32-bit operands on pre-386.  */
9482
6.33k
      specific_error = progress (mnem_suffix ? invalid_instruction_suffix
9483
6.33k
               : operand_size_mismatch);
9484
6.33k
      j = i.imm_operands + (t->operands > i.imm_operands + 1);
9485
6.33k
      if (i.suffix == LONG_MNEM_SUFFIX
9486
21
    && !cpu_arch_flags.bitfield.cpui386
9487
0
    && (intel_syntax
9488
0
        ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
9489
0
     && !intel_float_operand (insn_name (t)))
9490
0
        : intel_float_operand (insn_name (t)) != 2)
9491
0
    && (t->operands == i.imm_operands
9492
0
        || (operand_types[i.imm_operands].bitfield.class != RegMMX
9493
0
         && operand_types[i.imm_operands].bitfield.class != RegSIMD
9494
0
         && operand_types[i.imm_operands].bitfield.class != RegMask)
9495
0
        || (operand_types[j].bitfield.class != RegMMX
9496
0
      && operand_types[j].bitfield.class != RegSIMD
9497
0
      && operand_types[j].bitfield.class != RegMask))
9498
0
    && !t->opcode_modifier.sib)
9499
0
  continue;
9500
9501
      /* Do not verify operands when there are none.  */
9502
6.33k
      if (!t->operands)
9503
1.14k
  {
9504
1.14k
    if (VEX_check_encoding (t))
9505
35
      {
9506
35
        specific_error = progress (i.error);
9507
35
        continue;
9508
35
      }
9509
9510
    /* Check if pseudo prefix {rex2} is valid.  */
9511
1.11k
    if (t->opcode_modifier.noegpr && pp.rex2_encoding)
9512
0
      {
9513
0
        specific_error = progress (invalid_pseudo_prefix);
9514
0
        continue;
9515
0
      }
9516
9517
    /* We've found a match; break out of loop.  */
9518
1.11k
    break;
9519
1.11k
  }
9520
9521
5.19k
      if (!t->opcode_modifier.jump
9522
301
    || t->opcode_modifier.jump == JUMP_ABSOLUTE)
9523
4.93k
  {
9524
    /* There should be only one Disp operand.  */
9525
15.4k
    for (j = 0; j < MAX_OPERANDS; j++)
9526
13.8k
      if (operand_type_check (operand_types[j], disp))
9527
3.37k
        break;
9528
4.93k
    if (j < MAX_OPERANDS)
9529
3.37k
      {
9530
3.37k
        bool override = (i.prefix[ADDR_PREFIX] != 0);
9531
9532
3.37k
        addr_prefix_disp = j;
9533
9534
        /* Address size prefix will turn Disp64 operand into Disp32 and
9535
     Disp32/Disp16 one into Disp16/Disp32 respectively.  */
9536
3.37k
        switch (flag_code)
9537
3.37k
    {
9538
792
    case CODE_16BIT:
9539
792
      override = !override;
9540
      /* Fall through.  */
9541
1.32k
    case CODE_32BIT:
9542
1.32k
      if (operand_types[j].bitfield.disp32
9543
1.32k
          && operand_types[j].bitfield.disp16)
9544
1.32k
        {
9545
1.32k
          operand_types[j].bitfield.disp16 = override;
9546
1.32k
          operand_types[j].bitfield.disp32 = !override;
9547
1.32k
        }
9548
1.32k
      gas_assert (!operand_types[j].bitfield.disp64);
9549
1.32k
      break;
9550
9551
2.05k
    case CODE_64BIT:
9552
2.05k
      if (operand_types[j].bitfield.disp64)
9553
88
        {
9554
88
          gas_assert (!operand_types[j].bitfield.disp32);
9555
88
          operand_types[j].bitfield.disp32 = override;
9556
88
          operand_types[j].bitfield.disp64 = !override;
9557
88
        }
9558
2.05k
      operand_types[j].bitfield.disp16 = 0;
9559
2.05k
      break;
9560
3.37k
    }
9561
3.37k
      }
9562
4.93k
  }
9563
9564
      /* We check register size if needed.  */
9565
5.19k
      if (t->opcode_modifier.checkoperandsize)
9566
1.48k
  {
9567
1.48k
    check_register = (1 << t->operands) - 1;
9568
1.48k
    if (i.broadcast.type || i.broadcast.bytes)
9569
0
      check_register &= ~(1 << i.broadcast.operand);
9570
1.48k
  }
9571
3.70k
      else
9572
3.70k
  check_register = 0;
9573
9574
5.19k
      overlap0 = operand_type_and (i.types[0], operand_types[0]);
9575
5.19k
      switch (t->operands)
9576
5.19k
  {
9577
1.55k
  case 1:
9578
1.55k
    if (!operand_type_match (overlap0, i.types[0]))
9579
1.16k
      {
9580
1.16k
        specific_error = progress (i.error);
9581
1.16k
        continue;
9582
1.16k
      }
9583
9584
    /* Allow the ModR/M encoding to be requested by using the {load} or
9585
       {store} pseudo prefix on an applicable insn.  */
9586
390
    if (!t->opcode_modifier.modrm
9587
286
        && i.reg_operands == 1
9588
6
        && ((pp.dir_encoding == dir_encoding_load
9589
0
       && t->mnem_off != MN_pop)
9590
6
      || (pp.dir_encoding == dir_encoding_store
9591
0
          && t->mnem_off != MN_push))
9592
        /* Avoid BSWAP.  */
9593
0
        && t->mnem_off != MN_bswap)
9594
0
      continue;
9595
390
    break;
9596
9597
3.62k
  case 2:
9598
    /* xchg %eax, %eax is a special case. It is an alias for nop
9599
       only in 32bit mode and we can use opcode 0x90.  In 64bit
9600
       mode, we can't use 0x90 for xchg %eax, %eax since it should
9601
       zero-extend %eax to %rax.  */
9602
3.62k
    if (t->base_opcode == 0x90
9603
13
        && t->opcode_space == SPACE_BASE)
9604
13
      {
9605
13
        if (flag_code == CODE_64BIT
9606
4
      && i.types[0].bitfield.instance == Accum
9607
0
      && i.types[0].bitfield.dword
9608
0
      && i.types[1].bitfield.instance == Accum)
9609
0
    continue;
9610
9611
        /* Allow the ModR/M encoding to be requested by using the
9612
     {load} or {store} pseudo prefix.  */
9613
13
        if (pp.dir_encoding == dir_encoding_load
9614
13
      || pp.dir_encoding == dir_encoding_store)
9615
0
    continue;
9616
13
      }
9617
9618
3.62k
    if (t->base_opcode == MOV_AX_DISP32
9619
99
        && t->opcode_space == SPACE_BASE
9620
99
        && t->mnem_off != MN_movabs)
9621
99
      {
9622
        /* Force 0x8b encoding for "mov foo@GOT, %eax".  */
9623
99
        if (i.reloc[0] == BFD_RELOC_386_GOT32)
9624
0
    continue;
9625
9626
        /* xrelease mov %eax, <disp> is another special case. It must not
9627
     match the accumulator-only encoding of mov.  */
9628
99
        if (i.hle_prefix)
9629
0
    continue;
9630
9631
        /* Allow the ModR/M encoding to be requested by using a suitable
9632
     {load} or {store} pseudo prefix.  */
9633
99
        if (pp.dir_encoding == (i.types[0].bitfield.instance == Accum
9634
99
             ? dir_encoding_store
9635
99
             : dir_encoding_load)
9636
0
      && !i.types[0].bitfield.disp64
9637
0
      && !i.types[1].bitfield.disp64)
9638
0
    continue;
9639
99
      }
9640
9641
    /* Allow the ModR/M encoding to be requested by using the {load} or
9642
       {store} pseudo prefix on an applicable insn.  */
9643
3.62k
    if (!t->opcode_modifier.modrm
9644
475
        && i.reg_operands == 1
9645
133
        && i.imm_operands == 1
9646
71
        && (pp.dir_encoding == dir_encoding_load
9647
71
      || pp.dir_encoding == dir_encoding_store)
9648
0
        && t->opcode_space == SPACE_BASE)
9649
0
      {
9650
0
        if (t->base_opcode == 0xb0 /* mov $imm, %reg */
9651
0
      && pp.dir_encoding == dir_encoding_store)
9652
0
    continue;
9653
9654
0
        if ((t->base_opcode | 0x38) == 0x3c /* <alu> $imm, %acc */
9655
0
      && (t->base_opcode != 0x3c /* cmp $imm, %acc */
9656
0
          || pp.dir_encoding == dir_encoding_load))
9657
0
    continue;
9658
9659
0
        if (t->base_opcode == 0xa8 /* test $imm, %acc */
9660
0
      && pp.dir_encoding == dir_encoding_load)
9661
0
    continue;
9662
0
      }
9663
    /* Fall through.  */
9664
9665
3.63k
  case 3:
9666
3.63k
    if (!(size_match & MATCH_STRAIGHT))
9667
21
      goto check_reverse;
9668
    /* Reverse direction of operands if swapping is possible in the first
9669
       place (operands need to be symmetric) and
9670
       - the load form is requested, and the template is a store form,
9671
       - the store form is requested, and the template is a load form,
9672
       - the non-default (swapped) form is requested.  */
9673
3.61k
    overlap1 = operand_type_and (operand_types[0], operand_types[1]);
9674
9675
3.61k
    j = i.operands - 1 - (t->opcode_space == SPACE_MAP4
9676
320
        && t->opcode_modifier.vexvvvv);
9677
9678
3.61k
    if (t->opcode_modifier.d && i.reg_operands == i.operands
9679
30
        && !operand_type_all_zero (&overlap1))
9680
21
      switch (pp.dir_encoding)
9681
21
        {
9682
0
        case dir_encoding_load:
9683
0
    if (operand_type_check (operand_types[j], anymem)
9684
0
        || t->opcode_modifier.regmem)
9685
0
      goto check_reverse;
9686
0
    break;
9687
9688
0
        case dir_encoding_store:
9689
0
    if (!operand_type_check (operand_types[j], anymem)
9690
0
        && !t->opcode_modifier.regmem)
9691
0
      goto check_reverse;
9692
0
    break;
9693
9694
0
        case dir_encoding_swap:
9695
0
    goto check_reverse;
9696
9697
21
        case dir_encoding_default:
9698
21
    break;
9699
21
        }
9700
9701
    /* If we want store form, we skip the current load.  */
9702
3.61k
    if ((pp.dir_encoding == dir_encoding_store
9703
3.61k
         || pp.dir_encoding == dir_encoding_swap)
9704
0
        && i.mem_operands == 0
9705
0
        && t->opcode_modifier.load)
9706
0
      continue;
9707
    /* Fall through.  */
9708
3.61k
  case 4:
9709
3.61k
  case 5:
9710
3.61k
    overlap1 = operand_type_and (i.types[1], operand_types[1]);
9711
3.61k
    if (!operand_type_match (overlap0, i.types[0])
9712
1.42k
        || !operand_type_match (overlap1, i.types[1])
9713
1.10k
        || ((check_register & 3) == 3
9714
102
      && !operand_type_register_match (i.types[0],
9715
102
               operand_types[0],
9716
102
               i.types[1],
9717
102
               operand_types[1])))
9718
2.50k
      {
9719
2.50k
        specific_error = progress (i.error);
9720
9721
        /* Check if other direction is valid ...  */
9722
2.50k
        if (!t->opcode_modifier.d)
9723
1.11k
    continue;
9724
9725
1.42k
      check_reverse:
9726
1.42k
        if (!(size_match & MATCH_REVERSE))
9727
3
    continue;
9728
        /* Try reversing direction of operands.  */
9729
1.41k
        j = is_cpu (t, CpuFMA4)
9730
1.41k
      || is_cpu (t, CpuXOP)
9731
1.41k
      || is_cpu (t, CpuAPX_F)
9732
1.41k
      || is_cpu (t, CpuAPX_NDD) ? 1 : i.operands - 1;
9733
1.41k
        overlap0 = operand_type_and (i.types[0], operand_types[j]);
9734
1.41k
        overlap1 = operand_type_and (i.types[j], operand_types[0]);
9735
1.41k
        overlap2 = operand_type_and (i.types[1], operand_types[1]);
9736
1.41k
        gas_assert (t->operands != 3 || !check_register
9737
1.41k
        || is_cpu (t, CpuAPX_F) || is_cpu (t, CpuAPX_NDD));
9738
1.41k
        if (!operand_type_match (overlap0, i.types[0])
9739
97
      || !operand_type_match (overlap1, i.types[j])
9740
58
      || (t->operands == 3
9741
0
          && !operand_type_match (overlap2, i.types[1]))
9742
58
      || (check_register
9743
58
          && !operand_type_register_match (i.types[0],
9744
58
                   operand_types[j],
9745
58
                   i.types[j],
9746
58
                   operand_types[0])))
9747
1.35k
    {
9748
      /* Does not match either direction.  */
9749
1.35k
      specific_error = progress (i.error);
9750
1.35k
      continue;
9751
1.35k
    }
9752
        /* found_reverse_match holds which variant of D
9753
     we've found.  */
9754
58
        if (!t->opcode_modifier.d)
9755
0
    found_reverse_match = 0;
9756
58
        else if (operand_types[0].bitfield.tbyte)
9757
0
    {
9758
0
      if (t->opcode_modifier.operandconstraint != UGH)
9759
0
        found_reverse_match = Opcode_FloatD;
9760
0
      else
9761
0
        found_reverse_match = ~0;
9762
      /* FSUB{,R} and FDIV{,R} may need a 2nd bit flipped.  */
9763
0
      if ((t->extension_opcode & 4)
9764
0
          && (intel_syntax || intel_mnemonic))
9765
0
        found_reverse_match |= Opcode_FloatR;
9766
0
    }
9767
58
        else if (is_cpu (t, CpuFMA4) || is_cpu (t, CpuXOP))
9768
0
    {
9769
0
      found_reverse_match = Opcode_VexW;
9770
0
      goto check_operands_345;
9771
0
    }
9772
58
        else if (t->opcode_space == SPACE_MAP4
9773
0
           && t->operands >= 3)
9774
0
    {
9775
0
      found_reverse_match = Opcode_D;
9776
0
      goto check_operands_345;
9777
0
    }
9778
58
        else if (t->opcode_modifier.commutative
9779
           /* CFCMOVcc also wants its major opcode unaltered.  */
9780
58
           || (t->opcode_space == SPACE_MAP4
9781
0
         && (t->base_opcode | 0xf) == 0x4f))
9782
0
    found_reverse_match = ~0;
9783
58
        else if (t->opcode_space != SPACE_BASE
9784
0
           && (t->opcode_space != SPACE_MAP4
9785
         /* MOVBE, originating from SPACE_0F38, also
9786
            belongs here.  */
9787
0
         || t->mnem_off == MN_movbe)
9788
0
           && (t->opcode_space != SPACE_0F
9789
         /* MOV to/from CR/DR/TR, as an exception, follow
9790
            the base opcode space encoding model.  */
9791
0
         || (t->base_opcode | 7) != 0x27))
9792
0
    found_reverse_match = (t->base_opcode & 0xee) != 0x6e
9793
0
              ? Opcode_ExtD : Opcode_SIMD_IntD;
9794
58
        else
9795
58
    found_reverse_match = Opcode_D;
9796
58
      }
9797
1.10k
    else
9798
1.10k
      {
9799
        /* Found a forward 2 operand match here.  */
9800
1.10k
      check_operands_345:
9801
1.10k
        switch (t->operands)
9802
1.10k
    {
9803
0
    case 5:
9804
0
      overlap4 = operand_type_and (i.types[4], operand_types[4]);
9805
0
      if (!operand_type_match (overlap4, i.types[4])
9806
0
          || !operand_type_register_match (i.types[3],
9807
0
                   operand_types[3],
9808
0
                   i.types[4],
9809
0
                   operand_types[4]))
9810
0
        {
9811
0
          specific_error = progress (i.error);
9812
0
          continue;
9813
0
        }
9814
      /* Fall through.  */
9815
0
    case 4:
9816
0
      overlap3 = operand_type_and (i.types[3], operand_types[3]);
9817
0
      if (!operand_type_match (overlap3, i.types[3])
9818
0
          || ((check_register & 0xa) == 0xa
9819
0
        && !operand_type_register_match (i.types[1],
9820
0
                  operand_types[1],
9821
0
                  i.types[3],
9822
0
                  operand_types[3]))
9823
0
          || ((check_register & 0xc) == 0xc
9824
0
        && !operand_type_register_match (i.types[2],
9825
0
                  operand_types[2],
9826
0
                  i.types[3],
9827
0
                  operand_types[3])))
9828
0
        {
9829
0
          specific_error = progress (i.error);
9830
0
          continue;
9831
0
        }
9832
      /* Fall through.  */
9833
0
    case 3:
9834
0
      overlap2 = operand_type_and (i.types[2], operand_types[2]);
9835
0
      if (!operand_type_match (overlap2, i.types[2])
9836
0
          || ((check_register & 5) == 5
9837
0
        && !operand_type_register_match (i.types[0],
9838
0
                  operand_types[0],
9839
0
                  i.types[2],
9840
0
                  operand_types[2]))
9841
0
          || ((check_register & 6) == 6
9842
0
        && !operand_type_register_match (i.types[1],
9843
0
                  operand_types[1],
9844
0
                  i.types[2],
9845
0
                  operand_types[2])))
9846
0
        {
9847
0
          specific_error = progress (i.error);
9848
0
          continue;
9849
0
        }
9850
0
      break;
9851
1.10k
    }
9852
1.10k
      }
9853
    /* Found either forward/reverse 2, 3 or 4 operand match here:
9854
       slip through to break.  */
9855
5.19k
  }
9856
9857
      /* Check if VEX/EVEX encoding requirements can be satisfied.  */
9858
1.55k
      if (VEX_check_encoding (t))
9859
0
  {
9860
0
    specific_error = progress (i.error);
9861
0
    continue;
9862
0
  }
9863
9864
      /* Check if EGPR operands(r16-r31) are valid.  */
9865
1.55k
      if (check_EgprOperands (t))
9866
0
  {
9867
0
    specific_error = progress (i.error);
9868
0
    continue;
9869
0
  }
9870
9871
      /* Check if vector operands are valid.  */
9872
1.55k
      if (check_VecOperands (t))
9873
0
  {
9874
0
    specific_error = progress (i.error);
9875
0
    continue;
9876
0
  }
9877
9878
      /* Check if APX operands are valid.  */
9879
1.55k
      if (check_APX_operands (t))
9880
0
  {
9881
0
    specific_error = progress (i.error);
9882
0
    continue;
9883
0
  }
9884
9885
      /* Check whether to use the shorter VEX encoding for certain insns where
9886
   the EVEX encoding comes first in the table.  This requires the respective
9887
   AVX-* feature to be explicitly enabled.
9888
9889
   Most of the respective insns have just a single EVEX and a single VEX
9890
   template.  The one that's presently different is generated using the
9891
   Vxy / Exy constructs: There are 3 suffix-less EVEX forms, the latter
9892
   two of which may fall back to their two corresponding VEX forms.  */
9893
1.55k
      j = t->mnem_off != MN_vcvtneps2bf16 ? 1 : 2;
9894
1.55k
      if ((t == current_templates.start || j > 1)
9895
149
    && t->opcode_modifier.disp8memshift
9896
0
    && !t->opcode_modifier.vex
9897
0
    && !need_evex_encoding (t)
9898
0
    && t + j < current_templates.end
9899
0
    && t[j].opcode_modifier.vex)
9900
0
  {
9901
0
    i386_cpu_flags cpu;
9902
0
    unsigned int memshift = i.memshift;
9903
9904
0
    i.memshift = 0;
9905
0
    cpu = cpu_flags_and (cpu_flags_from_attr (t[j].cpu),
9906
0
             cpu_arch_isa_flags);
9907
0
    if (!cpu_flags_all_zero (&cpu)
9908
0
        && (!i.types[0].bitfield.disp8
9909
0
      || !operand_type_check (i.types[0], disp)
9910
0
      || i.op[0].disps->X_op != O_constant
9911
0
      || fits_in_disp8 (i.op[0].disps->X_add_number)))
9912
0
      {
9913
0
        specific_error = progress (internal_error);
9914
0
        t += j - 1;
9915
0
        continue;
9916
0
      }
9917
0
    i.memshift = memshift;
9918
0
  }
9919
9920
      /* If we can optimize a NDD insn to legacy insn, like
9921
   add %r16, %r8, %r8 -> add %r16, %r8,
9922
   add  %r8, %r16, %r8 -> add %r16, %r8, then rematch template.
9923
   Note that the semantics have not been changed.  */
9924
1.55k
      if (optimize
9925
0
    && !pp.no_optimize
9926
0
    && pp.encoding != encoding_evex
9927
0
    && ((t + 1 < current_templates.end
9928
0
         && !t[1].opcode_modifier.evex
9929
0
         && t[1].opcode_space <= SPACE_0F38
9930
0
         && t->opcode_modifier.vexvvvv == VexVVVV_DST)
9931
0
        || t->mnem_off == MN_movbe)
9932
0
    && (i.types[i.operands - 1].bitfield.dword
9933
0
        || i.types[i.operands - 1].bitfield.qword))
9934
0
  {
9935
0
    unsigned int match_dest_op = can_convert_NDD_to_legacy (t);
9936
9937
0
    if (match_dest_op != (unsigned int) ~0)
9938
0
      {
9939
0
        size_match = true;
9940
        /* We ensure that the next template has the same input
9941
     operands as the original matching template by the first
9942
     opernd (ATT). To avoid someone support new NDD insns and
9943
     put it in the wrong position.  */
9944
0
        overlap0 = operand_type_and (i.types[0],
9945
0
             t[1].operand_types[0]);
9946
0
        if (t->opcode_modifier.d)
9947
0
    overlap1 = operand_type_and (i.types[0],
9948
0
               t[1].operand_types[1]);
9949
0
        if (!operand_type_match (overlap0, i.types[0])
9950
0
      && (!t->opcode_modifier.d
9951
0
          || !operand_type_match (overlap1, i.types[0])))
9952
0
    size_match = false;
9953
9954
0
        if (size_match
9955
0
      && (t[1].opcode_space <= SPACE_0F
9956
          /* Some non-legacy-map0/1 insns can be shorter when
9957
       legacy-encoded and when no REX prefix is required.  */
9958
0
          || (!check_EgprOperands (t + 1)
9959
0
        && !check_Rex_required ()
9960
0
        && !i.op[i.operands - 1].regs->reg_type.bitfield.qword)))
9961
0
    {
9962
0
      if (i.operands > 2 && match_dest_op == i.operands - 3)
9963
0
        {
9964
0
          swap_2_operands (match_dest_op, i.operands - 2);
9965
9966
          /* CMOVcc is marked commutative, but then also needs its
9967
       encoded condition inverted.  */
9968
0
          if ((t->base_opcode | 0xf) == 0x4f)
9969
0
      i.invert_cond = true;
9970
0
        }
9971
9972
0
      --i.operands;
9973
0
      --i.reg_operands;
9974
9975
0
      if (t->mnem_off == MN_movbe)
9976
0
        {
9977
0
          gas_assert (t[1].mnem_off == MN_bswap);
9978
0
          ++current_templates.end;
9979
0
        }
9980
9981
0
      specific_error = progress (internal_error);
9982
0
      continue;
9983
0
    }
9984
9985
0
      }
9986
0
  }
9987
9988
      /* We've found a match; break out of loop.  */
9989
1.55k
      break;
9990
1.55k
    }
9991
9992
9.23k
#undef progress
9993
9994
9.23k
  if (t == current_templates.end)
9995
6.57k
    {
9996
      /* We found no match.  */
9997
6.57k
      i.error = specific_error;
9998
6.57k
      return NULL;
9999
6.57k
    }
10000
10001
  /* Don't emit diagnostics or install the template when one was already
10002
     installed, i.e. when called from process_suffix().  */
10003
2.66k
  if (i.tm.mnem_off)
10004
2
    return t;
10005
10006
2.66k
  if (!quiet_warnings)
10007
2.66k
    {
10008
2.66k
      if (!intel_syntax
10009
337
    && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
10010
0
  as_warn (_("indirect %s without `*'"), insn_name (t));
10011
10012
2.66k
      if (t->opcode_modifier.isprefix
10013
75
    && t->opcode_modifier.mnemonicsize == IGNORESIZE)
10014
1
  {
10015
    /* Warn them that a data or address size prefix doesn't
10016
       affect assembly of the next line of code.  */
10017
1
    as_warn (_("stand-alone `%s' prefix"), insn_name (t));
10018
1
  }
10019
10020
2.66k
      if (intel_syntax && mnem_suffix && !t->opcode_modifier.intelsuffix)
10021
65
  {
10022
65
    static bool noticed;
10023
10024
65
    as_warn (_("mnemonic suffix used with `%s'"), insn_name (t));
10025
65
    if (!noticed)
10026
1
      {
10027
1
        noticed = true;
10028
1
        as_warn (_(
10029
1
"NOTE: Such forms are deprecated and will be rejected by a future version of the assembler"));
10030
1
      }
10031
65
  }
10032
2.66k
    }
10033
10034
  /* Copy the template we found.  */
10035
2.66k
  install_template (t);
10036
10037
2.66k
  if (addr_prefix_disp != -1)
10038
1.25k
    i.tm.operand_types[addr_prefix_disp]
10039
1.25k
      = operand_types[addr_prefix_disp];
10040
10041
  /* APX insns acting on byte operands are WIG, yet that can't be expressed
10042
     in the templates (they're also covering word/dword/qword operands).  */
10043
2.66k
  if (t->opcode_space == SPACE_MAP4 && !t->opcode_modifier.vexw &&
10044
0
      i.types[i.operands - 1].bitfield.byte)
10045
0
    {
10046
0
      gas_assert (t->opcode_modifier.w);
10047
0
      i.tm.opcode_modifier.vexw = VEXWIG;
10048
0
    }
10049
10050
2.66k
  switch (found_reverse_match)
10051
2.66k
    {
10052
2.60k
    case 0:
10053
2.60k
      break;
10054
10055
0
    case Opcode_FloatR:
10056
0
    case Opcode_FloatR | Opcode_FloatD:
10057
0
      i.tm.extension_opcode ^= Opcode_FloatR >> 3;
10058
0
      found_reverse_match &= Opcode_FloatD;
10059
10060
      /* Fall through.  */
10061
58
    default:
10062
      /* If we found a reverse match we must alter the opcode direction
10063
   bit and clear/flip the regmem modifier one.  found_reverse_match
10064
   holds bits to change (different for int & float insns).  */
10065
10066
58
      i.tm.base_opcode ^= found_reverse_match;
10067
10068
58
      if (i.tm.opcode_space == SPACE_MAP4)
10069
0
  goto swap_first_2;
10070
10071
      /* Certain SIMD insns have their load forms specified in the opcode
10072
   table, and hence we need to _set_ RegMem instead of clearing it.
10073
   We need to avoid setting the bit though on insns like KMOVW.  */
10074
58
      i.tm.opcode_modifier.regmem
10075
58
  = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
10076
58
    && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
10077
0
    && !i.tm.opcode_modifier.regmem;
10078
10079
      /* Fall through.  */
10080
58
    case ~0:
10081
58
      if (i.tm.opcode_space == SPACE_MAP4
10082
0
    && !t->opcode_modifier.commutative)
10083
0
  i.tm.opcode_modifier.operandconstraint = EVEX_NF;
10084
58
      i.tm.operand_types[0] = operand_types[i.operands - 1];
10085
58
      i.tm.operand_types[i.operands - 1] = operand_types[0];
10086
58
      break;
10087
10088
0
    case Opcode_VexW:
10089
      /* Only the first two register operands need reversing, alongside
10090
   flipping VEX.W.  */
10091
0
      i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
10092
10093
      /* In 3-operand insns XOP.W changes which operand goes into XOP.vvvv.  */
10094
0
      i.tm.opcode_modifier.vexvvvv = VexVVVV_SRC1;
10095
10096
0
    swap_first_2:
10097
0
      j = i.tm.operand_types[0].bitfield.imm8;
10098
0
      i.tm.operand_types[j] = operand_types[j + 1];
10099
0
      i.tm.operand_types[j + 1] = operand_types[j];
10100
0
      break;
10101
2.66k
    }
10102
10103
2.66k
  return t;
10104
2.66k
}
10105
10106
static int
10107
check_string (void)
10108
32
{
10109
32
  unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
10110
32
  unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
10111
10112
32
  if (i.seg[op] != NULL && i.seg[op] != reg_es)
10113
0
    {
10114
0
      as_bad (_("`%s' operand %u must use `%ses' segment"),
10115
0
        insn_name (&i.tm),
10116
0
        intel_syntax ? i.tm.operands - es_op : es_op + 1,
10117
0
        register_prefix);
10118
0
      return 0;
10119
0
    }
10120
10121
  /* There's only ever one segment override allowed per instruction.
10122
     This instruction possibly has a legal segment override on the
10123
     second operand, so copy the segment to where non-string
10124
     instructions store it, allowing common code.  */
10125
32
  i.seg[op] = i.seg[1];
10126
10127
32
  return 1;
10128
32
}
10129
10130
static int
10131
process_suffix (const insn_template *t)
10132
2.66k
{
10133
2.66k
  bool is_movx = false;
10134
10135
  /* If matched instruction specifies an explicit instruction mnemonic
10136
     suffix, use it.  */
10137
2.66k
  if (i.tm.opcode_modifier.size == SIZE16)
10138
1
    i.suffix = WORD_MNEM_SUFFIX;
10139
2.66k
  else if (i.tm.opcode_modifier.size == SIZE32)
10140
15
    i.suffix = LONG_MNEM_SUFFIX;
10141
2.64k
  else if (i.tm.opcode_modifier.size == SIZE64)
10142
12
    i.suffix = QWORD_MNEM_SUFFIX;
10143
2.63k
  else if (i.reg_operands
10144
212
     && (i.operands > 1 || i.types[0].bitfield.class == Reg)
10145
212
     && i.tm.opcode_modifier.operandconstraint != ADDR_PREFIX_OP_REG)
10146
212
    {
10147
212
      unsigned int numop = i.operands;
10148
10149
      /* MOVSX/MOVZX */
10150
212
      is_movx = (i.tm.opcode_space == SPACE_0F
10151
3
     && (i.tm.base_opcode | 8) == 0xbe)
10152
209
    || (i.tm.opcode_space == SPACE_BASE
10153
209
        && i.tm.base_opcode == 0x63
10154
0
        && is_cpu (&i.tm, Cpu64));
10155
10156
      /* movsx/movzx want only their source operand considered here, for the
10157
   ambiguity checking below.  The suffix will be replaced afterwards
10158
   to represent the destination (register).  */
10159
212
      if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
10160
0
  --i.operands;
10161
10162
      /* crc32 needs REX.W set regardless of suffix / source operand size.  */
10163
212
      if (i.tm.mnem_off == MN_crc32 && i.tm.operand_types[1].bitfield.qword)
10164
0
        i.rex |= REX_W;
10165
10166
      /* If there's no instruction mnemonic suffix we try to invent one
10167
   based on GPR operands.  */
10168
212
      if (!i.suffix)
10169
151
  {
10170
    /* We take i.suffix from the last register operand specified,
10171
       Destination register type is more significant than source
10172
       register type.  crc32 in SSE4.2 prefers source register
10173
       type. */
10174
151
    unsigned int op = i.tm.mnem_off == MN_crc32 ? 1 : i.operands;
10175
10176
249
    while (op--)
10177
249
      if (i.tm.operand_types[op].bitfield.instance == InstanceNone
10178
4
    || i.tm.operand_types[op].bitfield.instance == Accum)
10179
249
        {
10180
249
    if (i.types[op].bitfield.class != Reg)
10181
98
      continue;
10182
151
    if (i.types[op].bitfield.byte)
10183
17
      i.suffix = BYTE_MNEM_SUFFIX;
10184
134
    else if (i.types[op].bitfield.word)
10185
66
      i.suffix = WORD_MNEM_SUFFIX;
10186
68
    else if (i.types[op].bitfield.dword)
10187
9
      i.suffix = LONG_MNEM_SUFFIX;
10188
59
    else if (i.types[op].bitfield.qword)
10189
59
      i.suffix = QWORD_MNEM_SUFFIX;
10190
0
    else
10191
0
      continue;
10192
151
    break;
10193
151
        }
10194
10195
    /* As an exception, movsx/movzx silently default to a byte source
10196
       in AT&T mode.  */
10197
151
    if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
10198
0
      i.suffix = BYTE_MNEM_SUFFIX;
10199
151
  }
10200
61
      else if (i.suffix == BYTE_MNEM_SUFFIX)
10201
2
  {
10202
2
    if (!check_byte_reg ())
10203
0
      return 0;
10204
2
  }
10205
59
      else if (i.suffix == LONG_MNEM_SUFFIX)
10206
0
  {
10207
0
    if (!check_long_reg ())
10208
0
      return 0;
10209
0
  }
10210
59
      else if (i.suffix == QWORD_MNEM_SUFFIX)
10211
3
  {
10212
3
    if (!check_qword_reg ())
10213
3
      return 0;
10214
3
  }
10215
56
      else if (i.suffix == WORD_MNEM_SUFFIX)
10216
56
  {
10217
56
    if (!check_word_reg ())
10218
0
      return 0;
10219
56
  }
10220
0
      else if (intel_syntax
10221
0
         && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
10222
  /* Do nothing if the instruction is going to ignore the prefix.  */
10223
0
  ;
10224
0
      else
10225
0
  abort ();
10226
10227
      /* Undo the movsx/movzx change done above.  */
10228
209
      i.operands = numop;
10229
209
    }
10230
2.42k
  else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
10231
126
     && !i.suffix)
10232
86
    {
10233
86
      i.suffix = stackop_size;
10234
86
      if (stackop_size == LONG_MNEM_SUFFIX)
10235
0
  {
10236
    /* stackop_size is set to LONG_MNEM_SUFFIX for the
10237
       .code16gcc directive to support 16-bit mode with
10238
       32-bit address.  For IRET without a suffix, generate
10239
       16-bit IRET (opcode 0xcf) to return from an interrupt
10240
       handler.  */
10241
0
    if (i.tm.base_opcode == 0xcf)
10242
0
      {
10243
0
        i.suffix = WORD_MNEM_SUFFIX;
10244
0
        as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
10245
0
      }
10246
    /* Warn about changed behavior for segment register push/pop.  */
10247
0
    else if ((i.tm.base_opcode | 1) == 0x07)
10248
0
      as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
10249
0
         insn_name (&i.tm));
10250
0
  }
10251
86
    }
10252
2.33k
  else if (!i.suffix
10253
2.26k
     && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
10254
2.23k
         || i.tm.opcode_modifier.jump == JUMP_BYTE
10255
2.23k
         || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
10256
2.22k
         || (i.tm.opcode_space == SPACE_0F
10257
76
       && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
10258
36
       && i.tm.extension_opcode <= 3)))
10259
72
    {
10260
72
      switch (flag_code)
10261
72
  {
10262
70
  case CODE_64BIT:
10263
70
    if (!i.tm.opcode_modifier.no_qsuf)
10264
43
      {
10265
43
        if (i.tm.opcode_modifier.jump == JUMP_BYTE
10266
43
      || i.tm.opcode_modifier.no_lsuf)
10267
43
    i.suffix = QWORD_MNEM_SUFFIX;
10268
43
        break;
10269
43
      }
10270
    /* Fall through.  */
10271
27
  case CODE_32BIT:
10272
27
    if (!i.tm.opcode_modifier.no_lsuf)
10273
27
      i.suffix = LONG_MNEM_SUFFIX;
10274
27
    break;
10275
2
  case CODE_16BIT:
10276
2
    if (!i.tm.opcode_modifier.no_wsuf)
10277
2
      i.suffix = WORD_MNEM_SUFFIX;
10278
2
    break;
10279
72
  }
10280
72
    }
10281
10282
2.65k
  if (!i.suffix
10283
2.27k
      && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
10284
    /* Also cover lret/retf/iret in 64-bit mode.  */
10285
86
    || (flag_code == CODE_64BIT
10286
76
        && !i.tm.opcode_modifier.no_lsuf
10287
7
        && !i.tm.opcode_modifier.no_qsuf))
10288
2.20k
      && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
10289
      /* Explicit sizing prefixes are assumed to disambiguate insns.  */
10290
2.19k
      && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
10291
      /* Accept FLDENV et al without suffix.  */
10292
2.10k
      && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
10293
2.10k
    {
10294
2.10k
      unsigned int suffixes, evex = 0;
10295
10296
2.10k
      suffixes = !i.tm.opcode_modifier.no_bsuf;
10297
2.10k
      if (!i.tm.opcode_modifier.no_wsuf)
10298
1.06k
  suffixes |= 1 << 1;
10299
2.10k
      if (!i.tm.opcode_modifier.no_lsuf)
10300
1.09k
  suffixes |= 1 << 2;
10301
2.10k
      if (!i.tm.opcode_modifier.no_ssuf)
10302
24
  suffixes |= 1 << 4;
10303
2.10k
      if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
10304
437
  suffixes |= 1 << 5;
10305
10306
      /* Operand size may be ambiguous only across multiple templates.  Avoid
10307
   the extra effort though if we already know that multiple suffixes /
10308
   operand sizes are allowed.  Also limit this to non-SIMD operand sizes
10309
   (i.e. ones expressable via suffixes) for now.
10310
   There's one special case though that needs excluding: Insns taking
10311
   Disp<N> operands also match templates permitting BaseIndex.  JMP in
10312
   particular would thus wrongly trigger the check further down.  Cover
10313
   JUMP_DWORD insns here as well, just in case.  */
10314
2.10k
      if (i.tm.opcode_modifier.jump != JUMP
10315
2.05k
    && i.tm.opcode_modifier.jump != JUMP_DWORD)
10316
2.06k
  while (!(suffixes & (suffixes - 1)))
10317
969
    {
10318
      /* Sadly check_VecOperands(), running ahead of install_template(),
10319
         may update i.memshift.  Save and restore the value here.  */
10320
969
      unsigned int memshift = i.memshift;
10321
10322
969
      current_templates.start = t + 1;
10323
969
      t = match_template (0);
10324
969
      i.memshift = memshift;
10325
969
      if (t == NULL)
10326
967
        break;
10327
2
      if (!t->opcode_modifier.no_bsuf)
10328
2
        suffixes |= 1 << 0;
10329
2
      if (!t->opcode_modifier.no_wsuf)
10330
0
        suffixes |= 1 << 1;
10331
2
      if (!t->opcode_modifier.no_lsuf)
10332
0
        suffixes |= 1 << 2;
10333
2
      if (!t->opcode_modifier.no_ssuf)
10334
0
        suffixes |= 1 << 4;
10335
2
      if (flag_code == CODE_64BIT && !t->opcode_modifier.no_qsuf)
10336
0
        suffixes |= 1 << 5;
10337
2
    }
10338
10339
      /* For [XYZ]MMWORD operands inspect operand sizes.  While generally
10340
   also suitable for AT&T syntax mode, it was requested that this be
10341
   restricted to just Intel syntax.  */
10342
2.10k
      if (intel_syntax && is_any_vex_encoding (&i.tm)
10343
2
    && !i.broadcast.type && !i.broadcast.bytes)
10344
2
  {
10345
2
    unsigned int op;
10346
10347
3
    for (op = 0; op < i.tm.operands; ++op)
10348
1
      {
10349
1
        if (vector_size < VSZ512)
10350
0
    {
10351
0
      i.tm.operand_types[op].bitfield.zmmword = 0;
10352
0
      if (vector_size < VSZ256)
10353
0
        {
10354
0
          i.tm.operand_types[op].bitfield.ymmword = 0;
10355
0
          if (i.tm.operand_types[op].bitfield.xmmword
10356
0
        && i.tm.opcode_modifier.evex == EVEXDYN)
10357
0
      i.tm.opcode_modifier.evex = EVEX128;
10358
0
        }
10359
0
      else if (i.tm.operand_types[op].bitfield.ymmword
10360
0
         && !i.tm.operand_types[op].bitfield.xmmword
10361
0
         && i.tm.opcode_modifier.evex == EVEXDYN)
10362
0
        i.tm.opcode_modifier.evex = EVEX256;
10363
0
    }
10364
1
        else if (i.tm.opcode_modifier.evex
10365
0
           && !cpu_arch_flags.bitfield.cpuavx512vl)
10366
0
    {
10367
0
      if (i.tm.operand_types[op].bitfield.ymmword)
10368
0
        i.tm.operand_types[op].bitfield.xmmword = 0;
10369
0
      if (i.tm.operand_types[op].bitfield.zmmword)
10370
0
        i.tm.operand_types[op].bitfield.ymmword = 0;
10371
0
      if (i.tm.opcode_modifier.evex == EVEXDYN)
10372
0
        i.tm.opcode_modifier.evex = EVEX512;
10373
0
    }
10374
10375
1
        if (i.tm.operand_types[op].bitfield.xmmword
10376
1
      + i.tm.operand_types[op].bitfield.ymmword
10377
1
      + i.tm.operand_types[op].bitfield.zmmword < 2)
10378
1
    continue;
10379
10380
        /* Any properly sized operand disambiguates the insn.  */
10381
0
        if (i.types[op].bitfield.xmmword
10382
0
      || i.types[op].bitfield.ymmword
10383
0
      || i.types[op].bitfield.zmmword)
10384
0
    {
10385
0
      suffixes &= ~(7 << 6);
10386
0
      evex = 0;
10387
0
      break;
10388
0
    }
10389
10390
0
        if ((i.flags[op] & Operand_Mem)
10391
0
      && i.tm.operand_types[op].bitfield.unspecified)
10392
0
    {
10393
0
      if (i.tm.operand_types[op].bitfield.xmmword)
10394
0
        suffixes |= 1 << 6;
10395
0
      if (i.tm.operand_types[op].bitfield.ymmword)
10396
0
        suffixes |= 1 << 7;
10397
0
      if (i.tm.operand_types[op].bitfield.zmmword)
10398
0
        suffixes |= 1 << 8;
10399
0
      if (i.tm.opcode_modifier.evex)
10400
0
        evex = EVEX512;
10401
0
    }
10402
0
      }
10403
2
  }
10404
10405
      /* Are multiple suffixes / operand sizes allowed?  */
10406
2.10k
      if (suffixes & (suffixes - 1))
10407
1.09k
  {
10408
1.09k
    if (intel_syntax
10409
922
        && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
10410
7
      || operand_check == check_error))
10411
915
      {
10412
915
        as_bad (_("ambiguous operand size for `%s'"), insn_name (&i.tm));
10413
915
        return 0;
10414
915
      }
10415
177
    if (operand_check == check_error)
10416
0
      {
10417
0
        as_bad (_("no instruction mnemonic suffix given and "
10418
0
      "no register operands; can't size `%s'"), insn_name (&i.tm));
10419
0
        return 0;
10420
0
      }
10421
177
    if (operand_check == check_warning)
10422
177
      as_warn (_("%s; using default for `%s'"),
10423
177
           intel_syntax
10424
177
           ? _("ambiguous operand size")
10425
177
           : _("no instruction mnemonic suffix given and "
10426
177
         "no register operands"),
10427
177
           insn_name (&i.tm));
10428
10429
177
    if (i.tm.opcode_modifier.floatmf)
10430
6
      i.suffix = SHORT_MNEM_SUFFIX;
10431
171
    else if (is_movx)
10432
0
      /* handled below */;
10433
171
    else if (evex)
10434
0
      i.tm.opcode_modifier.evex = evex;
10435
171
    else if (flag_code == CODE_16BIT)
10436
125
      i.suffix = WORD_MNEM_SUFFIX;
10437
46
    else if (!i.tm.opcode_modifier.no_lsuf)
10438
46
      i.suffix = LONG_MNEM_SUFFIX;
10439
0
    else
10440
0
      i.suffix = QWORD_MNEM_SUFFIX;
10441
177
  }
10442
2.10k
    }
10443
10444
1.74k
  if (is_movx)
10445
0
    {
10446
      /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
10447
   In AT&T syntax, if there is no suffix (warned about above), the default
10448
   will be byte extension.  */
10449
0
      if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
10450
0
  i.tm.base_opcode |= 1;
10451
10452
      /* For further processing, the suffix should represent the destination
10453
   (register).  This is already the case when one was used with
10454
   mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
10455
   no suffix to begin with.  */
10456
0
      if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
10457
0
  {
10458
0
    if (i.types[1].bitfield.word)
10459
0
      i.suffix = WORD_MNEM_SUFFIX;
10460
0
    else if (i.types[1].bitfield.qword)
10461
0
      i.suffix = QWORD_MNEM_SUFFIX;
10462
0
    else
10463
0
      i.suffix = LONG_MNEM_SUFFIX;
10464
10465
0
    i.tm.opcode_modifier.w = 0;
10466
0
  }
10467
0
    }
10468
10469
1.74k
  if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
10470
42
    i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
10471
42
       != (i.tm.operand_types[1].bitfield.class == Reg);
10472
10473
  /* Change the opcode based on the operand size given by i.suffix.  */
10474
1.74k
  switch (i.suffix)
10475
1.74k
    {
10476
    /* Size floating point instruction.  */
10477
112
    case LONG_MNEM_SUFFIX:
10478
112
      if (i.tm.opcode_modifier.floatmf)
10479
0
  {
10480
0
    i.tm.base_opcode ^= 4;
10481
0
    break;
10482
0
  }
10483
    /* fall through */
10484
402
    case WORD_MNEM_SUFFIX:
10485
516
    case QWORD_MNEM_SUFFIX:
10486
      /* It's not a byte, select word/dword operation.  */
10487
516
      if (i.tm.opcode_modifier.w)
10488
339
  {
10489
339
    if (i.short_form)
10490
20
      i.tm.base_opcode |= 8;
10491
319
    else
10492
319
      i.tm.base_opcode |= 1;
10493
339
  }
10494
10495
      /* Set mode64 for an operand.  */
10496
516
      if (i.suffix == QWORD_MNEM_SUFFIX)
10497
114
  {
10498
114
    if (flag_code == CODE_64BIT
10499
114
        && !i.tm.opcode_modifier.norex64
10500
65
        && !i.tm.opcode_modifier.vexw
10501
        /* Special case for xchg %rax,%rax.  It is NOP and doesn't
10502
     need rex64. */
10503
65
        && ! (i.operands == 2
10504
52
        && i.tm.base_opcode == 0x90
10505
0
        && i.tm.opcode_space == SPACE_BASE
10506
0
        && i.types[0].bitfield.instance == Accum
10507
0
        && i.types[1].bitfield.instance == Accum))
10508
65
      i.rex |= REX_W;
10509
10510
114
    break;
10511
114
  }
10512
10513
    /* fall through */
10514
408
    case SHORT_MNEM_SUFFIX:
10515
      /* Now select between word & dword operations via the operand
10516
   size prefix, except for instructions that will ignore this
10517
   prefix anyway.  */
10518
408
      if (i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
10519
407
    && !i.tm.opcode_modifier.floatmf
10520
401
    && (!is_any_vex_encoding (&i.tm)
10521
0
        || i.tm.opcode_space == SPACE_MAP4)
10522
401
    && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
10523
251
        || (flag_code == CODE_64BIT
10524
112
      && i.tm.opcode_modifier.jump == JUMP_BYTE)))
10525
165
  {
10526
165
    unsigned int prefix = DATA_PREFIX_OPCODE;
10527
10528
165
    if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
10529
15
      prefix = ADDR_PREFIX_OPCODE;
10530
10531
    /* The DATA PREFIX of EVEX promoted from legacy APX instructions
10532
       needs to be adjusted.  */
10533
165
    if (i.tm.opcode_space == SPACE_MAP4)
10534
0
      {
10535
0
        gas_assert (!i.tm.opcode_modifier.opcodeprefix);
10536
0
        i.tm.opcode_modifier.opcodeprefix = PREFIX_0X66;
10537
0
      }
10538
165
    else if (!add_prefix (prefix))
10539
0
      return 0;
10540
165
  }
10541
10542
408
      break;
10543
10544
1.18k
    case 0:
10545
      /* Select word/dword/qword operation with explicit data sizing prefix
10546
   when there are no suitable register operands.  */
10547
1.18k
      if (i.tm.opcode_modifier.w
10548
32
    && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
10549
32
    && (!i.reg_operands
10550
0
        || (i.reg_operands == 1
10551
          /* ShiftCount */
10552
0
      && (i.tm.operand_types[0].bitfield.instance == RegC
10553
          /* InOutPortReg */
10554
0
          || i.tm.operand_types[0].bitfield.instance == RegD
10555
0
          || i.tm.operand_types[1].bitfield.instance == RegD
10556
0
          || i.tm.mnem_off == MN_crc32))))
10557
32
  i.tm.base_opcode |= 1;
10558
1.18k
      break;
10559
1.74k
    }
10560
10561
1.74k
  if (i.tm.opcode_modifier.operandconstraint == ADDR_PREFIX_OP_REG)
10562
0
    {
10563
0
      gas_assert (!i.suffix);
10564
0
      gas_assert (i.reg_operands);
10565
10566
0
      if (i.tm.operand_types[0].bitfield.instance == Accum
10567
0
    || i.operands == 1)
10568
0
  {
10569
    /* The address size override prefix changes the size of the
10570
       first operand.  */
10571
0
    if (flag_code == CODE_64BIT
10572
0
        && i.op[0].regs->reg_type.bitfield.word)
10573
0
      {
10574
0
        as_bad (_("16-bit addressing unavailable for `%s'"),
10575
0
          insn_name (&i.tm));
10576
0
        return 0;
10577
0
      }
10578
10579
0
    if ((flag_code == CODE_32BIT
10580
0
         ? i.op[0].regs->reg_type.bitfield.word
10581
0
         : i.op[0].regs->reg_type.bitfield.dword)
10582
0
        && !add_prefix (ADDR_PREFIX_OPCODE))
10583
0
      return 0;
10584
0
  }
10585
0
      else
10586
0
  {
10587
    /* Check invalid register operand when the address size override
10588
       prefix changes the size of register operands.  */
10589
0
    unsigned int op;
10590
0
    enum { need_word, need_dword, need_qword } need;
10591
10592
    /* Check the register operand for the address size prefix if
10593
       the memory operand has no real registers, like symbol, DISP
10594
       or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant.  */
10595
0
    if (i.mem_operands == 1
10596
0
        && i.reg_operands == 1
10597
0
        && i.operands == 2
10598
0
        && i.types[1].bitfield.class == Reg
10599
0
        && (flag_code == CODE_32BIT
10600
0
      ? i.op[1].regs->reg_type.bitfield.word
10601
0
      : i.op[1].regs->reg_type.bitfield.dword)
10602
0
        && ((i.base_reg == NULL && i.index_reg == NULL)
10603
0
#ifdef OBJ_ELF
10604
0
      || (x86_elf_abi == X86_64_X32_ABI
10605
0
          && i.base_reg
10606
0
          && i.base_reg->reg_num == RegIP
10607
0
          && i.base_reg->reg_type.bitfield.qword))
10608
#else
10609
      || 0)
10610
#endif
10611
0
        && !add_prefix (ADDR_PREFIX_OPCODE))
10612
0
      return 0;
10613
10614
0
    if (flag_code == CODE_32BIT)
10615
0
      need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
10616
0
    else if (i.prefix[ADDR_PREFIX])
10617
0
      need = need_dword;
10618
0
    else
10619
0
      need = flag_code == CODE_64BIT ? need_qword : need_word;
10620
10621
0
    for (op = i.imm_operands; op < i.operands; op++)
10622
0
      {
10623
0
        if (i.types[op].bitfield.class != Reg)
10624
0
    continue;
10625
10626
0
        switch (need)
10627
0
    {
10628
0
    case need_word:
10629
0
      if (i.op[op].regs->reg_type.bitfield.word)
10630
0
        continue;
10631
0
      break;
10632
0
    case need_dword:
10633
0
      if (i.op[op].regs->reg_type.bitfield.dword)
10634
0
        continue;
10635
0
      break;
10636
0
    case need_qword:
10637
0
      if (i.op[op].regs->reg_type.bitfield.qword)
10638
0
        continue;
10639
0
      break;
10640
0
    }
10641
10642
0
        as_bad (_("invalid register operand size for `%s'"),
10643
0
          insn_name (&i.tm));
10644
0
        return 0;
10645
0
      }
10646
0
  }
10647
0
    }
10648
10649
1.74k
  return 1;
10650
1.74k
}
10651
10652
static int
10653
check_byte_reg (void)
10654
2
{
10655
2
  int op;
10656
10657
5
  for (op = i.operands; --op >= 0;)
10658
4
    {
10659
      /* Skip non-register operands. */
10660
4
      if (i.types[op].bitfield.class != Reg)
10661
2
  continue;
10662
10663
      /* If this is an eight bit register, it's OK.  */
10664
2
      if (i.types[op].bitfield.byte)
10665
2
  {
10666
2
    if (i.tm.opcode_modifier.checkoperandsize)
10667
1
      break;
10668
1
    continue;
10669
2
  }
10670
10671
      /* I/O port address operands are OK too.  */
10672
0
      if (i.tm.operand_types[op].bitfield.instance == RegD
10673
0
    && i.tm.operand_types[op].bitfield.word)
10674
0
  continue;
10675
10676
      /* crc32 only wants its source operand checked here.  */
10677
0
      if (i.tm.mnem_off == MN_crc32 && op != 0)
10678
0
  continue;
10679
10680
      /* Any other register is bad.  */
10681
0
      as_bad (_("`%s%s' not allowed with `%s%c'"),
10682
0
        register_prefix, i.op[op].regs->reg_name,
10683
0
        insn_name (&i.tm), i.suffix);
10684
0
      return 0;
10685
0
    }
10686
2
  return 1;
10687
2
}
10688
10689
static int
10690
check_long_reg (void)
10691
0
{
10692
0
  int op;
10693
10694
0
  for (op = i.operands; --op >= 0;)
10695
    /* Skip non-register operands. */
10696
0
    if (i.types[op].bitfield.class != Reg)
10697
0
      continue;
10698
    /* Reject eight bit registers, except where the template requires
10699
       them. (eg. movzb)  */
10700
0
    else if (i.types[op].bitfield.byte
10701
0
       && (i.tm.operand_types[op].bitfield.word
10702
0
     || i.tm.operand_types[op].bitfield.dword
10703
0
     || i.tm.operand_types[op].bitfield.qword))
10704
0
      {
10705
0
  as_bad (_("`%s%s' not allowed with `%s%c'"),
10706
0
    register_prefix,
10707
0
    i.op[op].regs->reg_name,
10708
0
    insn_name (&i.tm),
10709
0
    i.suffix);
10710
0
  return 0;
10711
0
      }
10712
    /* Error if the e prefix on a general reg is missing, or if the r
10713
       prefix on a general reg is present.  */
10714
0
    else if ((i.types[op].bitfield.word
10715
0
        || i.types[op].bitfield.qword)
10716
0
       && i.tm.operand_types[op].bitfield.dword)
10717
0
      {
10718
0
  as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
10719
0
    register_prefix, i.op[op].regs->reg_name,
10720
0
    i.suffix);
10721
0
  return 0;
10722
0
      }
10723
0
    else if (i.tm.opcode_modifier.checkoperandsize)
10724
0
      break;
10725
10726
0
  return 1;
10727
0
}
10728
10729
static int
10730
check_qword_reg (void)
10731
3
{
10732
3
  int op;
10733
10734
3
  for (op = i.operands; --op >= 0; )
10735
    /* Skip non-register operands. */
10736
3
    if (i.types[op].bitfield.class != Reg)
10737
0
      continue;
10738
    /* Reject eight bit registers, except where the template requires
10739
       them. (eg. movzb)  */
10740
3
    else if (i.types[op].bitfield.byte
10741
0
       && (i.tm.operand_types[op].bitfield.word
10742
0
     || i.tm.operand_types[op].bitfield.dword
10743
0
     || i.tm.operand_types[op].bitfield.qword))
10744
0
      {
10745
0
  as_bad (_("`%s%s' not allowed with `%s%c'"),
10746
0
    register_prefix,
10747
0
    i.op[op].regs->reg_name,
10748
0
    insn_name (&i.tm),
10749
0
    i.suffix);
10750
0
  return 0;
10751
0
      }
10752
    /* Error if the r prefix on a general reg is missing.  */
10753
3
    else if ((i.types[op].bitfield.word
10754
0
        || i.types[op].bitfield.dword)
10755
3
       && i.tm.operand_types[op].bitfield.qword)
10756
3
      {
10757
3
  as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
10758
3
    register_prefix, i.op[op].regs->reg_name, i.suffix);
10759
3
  return 0;
10760
3
      }
10761
0
    else if (i.tm.opcode_modifier.checkoperandsize)
10762
0
      break;
10763
10764
0
  return 1;
10765
3
}
10766
10767
static int
10768
check_word_reg (void)
10769
56
{
10770
56
  int op;
10771
56
  for (op = i.operands; --op >= 0;)
10772
    /* Skip non-register operands. */
10773
56
    if (i.types[op].bitfield.class != Reg)
10774
0
      continue;
10775
    /* Reject eight bit registers, except where the template requires
10776
       them. (eg. movzb)  */
10777
56
    else if (i.types[op].bitfield.byte
10778
0
       && (i.tm.operand_types[op].bitfield.word
10779
0
     || i.tm.operand_types[op].bitfield.dword
10780
0
     || i.tm.operand_types[op].bitfield.qword))
10781
0
      {
10782
0
  as_bad (_("`%s%s' not allowed with `%s%c'"),
10783
0
    register_prefix,
10784
0
    i.op[op].regs->reg_name,
10785
0
    insn_name (&i.tm),
10786
0
    i.suffix);
10787
0
  return 0;
10788
0
      }
10789
    /* Error if the e or r prefix on a general reg is present.  */
10790
56
    else if ((i.types[op].bitfield.dword
10791
56
     || i.types[op].bitfield.qword)
10792
0
       && i.tm.operand_types[op].bitfield.word)
10793
0
      {
10794
0
  as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
10795
0
    register_prefix, i.op[op].regs->reg_name,
10796
0
    i.suffix);
10797
0
  return 0;
10798
0
      }
10799
56
    else if (i.tm.opcode_modifier.checkoperandsize)
10800
56
      break;
10801
10802
56
  return 1;
10803
56
}
10804
10805
static int
10806
update_imm (unsigned int j)
10807
1.12k
{
10808
1.12k
  i386_operand_type overlap = i.types[j];
10809
10810
1.12k
  if (i.tm.operand_types[j].bitfield.imm8
10811
197
      && i.tm.operand_types[j].bitfield.imm8s
10812
0
      && overlap.bitfield.imm8 && overlap.bitfield.imm8s)
10813
0
    {
10814
      /* This combination is used on 8-bit immediates where e.g. $~0 is
10815
   desirable to permit.  We're past operand type matching, so simply
10816
   put things back in the shape they were before introducing the
10817
   distinction between Imm8, Imm8S, and Imm8|Imm8S.  */
10818
0
      overlap.bitfield.imm8s = 0;
10819
0
    }
10820
10821
1.12k
  if (overlap.bitfield.imm8
10822
1.12k
      + overlap.bitfield.imm8s
10823
1.12k
      + overlap.bitfield.imm16
10824
1.12k
      + overlap.bitfield.imm32
10825
1.12k
      + overlap.bitfield.imm32s
10826
1.12k
      + overlap.bitfield.imm64 > 1)
10827
151
    {
10828
151
      static const i386_operand_type imm16 = { .bitfield = { .imm16 = 1 } };
10829
151
      static const i386_operand_type imm32 = { .bitfield = { .imm32 = 1 } };
10830
151
      static const i386_operand_type imm32s = { .bitfield = { .imm32s = 1 } };
10831
151
      static const i386_operand_type imm16_32 = { .bitfield =
10832
151
  { .imm16 = 1, .imm32 = 1 }
10833
151
      };
10834
151
      static const i386_operand_type imm16_32s =  { .bitfield =
10835
151
  { .imm16 = 1, .imm32s = 1 }
10836
151
      };
10837
151
      static const i386_operand_type imm16_32_32s = { .bitfield =
10838
151
  { .imm16 = 1, .imm32 = 1, .imm32s = 1 }
10839
151
      };
10840
10841
151
      if (i.suffix)
10842
142
  {
10843
142
    i386_operand_type temp;
10844
10845
142
    operand_type_set (&temp, 0);
10846
142
    if (i.suffix == BYTE_MNEM_SUFFIX)
10847
6
      {
10848
6
        temp.bitfield.imm8 = overlap.bitfield.imm8;
10849
6
        temp.bitfield.imm8s = overlap.bitfield.imm8s;
10850
6
      }
10851
136
    else if (i.suffix == WORD_MNEM_SUFFIX)
10852
126
      temp.bitfield.imm16 = overlap.bitfield.imm16;
10853
10
    else if (i.suffix == QWORD_MNEM_SUFFIX)
10854
0
      {
10855
0
        temp.bitfield.imm64 = overlap.bitfield.imm64;
10856
0
        temp.bitfield.imm32s = overlap.bitfield.imm32s;
10857
0
      }
10858
10
    else
10859
10
      temp.bitfield.imm32 = overlap.bitfield.imm32;
10860
142
    overlap = temp;
10861
142
  }
10862
9
      else if (operand_type_equal (&overlap, &imm16_32_32s)
10863
9
         || operand_type_equal (&overlap, &imm16_32)
10864
0
         || operand_type_equal (&overlap, &imm16_32s))
10865
9
  {
10866
9
    if ((flag_code == CODE_16BIT)
10867
9
        ^ (i.prefix[DATA_PREFIX] != 0 && !(i.prefix[REX_PREFIX] & REX_W)))
10868
0
      overlap = imm16;
10869
9
    else
10870
9
      overlap = imm32s;
10871
9
  }
10872
0
      else if (i.prefix[REX_PREFIX] & REX_W)
10873
0
  overlap = operand_type_and (overlap, imm32s);
10874
0
      else if (i.prefix[DATA_PREFIX])
10875
0
  overlap = operand_type_and (overlap,
10876
0
            flag_code != CODE_16BIT ? imm16 : imm32);
10877
151
      if (overlap.bitfield.imm8
10878
151
    + overlap.bitfield.imm8s
10879
151
    + overlap.bitfield.imm16
10880
151
    + overlap.bitfield.imm32
10881
151
    + overlap.bitfield.imm32s
10882
151
    + overlap.bitfield.imm64 != 1)
10883
0
  {
10884
0
    as_bad (_("no instruction mnemonic suffix given; "
10885
0
        "can't determine immediate size"));
10886
0
    return 0;
10887
0
  }
10888
151
    }
10889
1.12k
  i.types[j] = overlap;
10890
10891
1.12k
  return 1;
10892
1.12k
}
10893
10894
static int
10895
finalize_imm (void)
10896
1.74k
{
10897
1.74k
  unsigned int j, n;
10898
10899
  /* Update the first 2 immediate operands.  */
10900
1.74k
  n = i.operands > 2 ? 2 : i.operands;
10901
1.74k
  if (n)
10902
746
    {
10903
1.87k
      for (j = 0; j < n; j++)
10904
1.12k
  if (update_imm (j) == 0)
10905
0
    return 0;
10906
10907
      /* The 3rd operand can't be immediate operand.  */
10908
746
      gas_assert (operand_type_check (i.types[2], imm) == 0);
10909
746
    }
10910
10911
1.74k
  return 1;
10912
1.74k
}
10913
10914
static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
10915
         bool do_sse2avx)
10916
214
{
10917
214
  if (r->reg_flags & RegRex)
10918
10
    {
10919
10
      if (i.rex & rex_bit)
10920
0
  as_bad (_("same type of prefix used twice"));
10921
10
      i.rex |= rex_bit;
10922
10
    }
10923
204
  else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
10924
0
    {
10925
0
      gas_assert (i.vex.register_specifier == r);
10926
0
      i.vex.register_specifier += 8;
10927
0
    }
10928
10929
214
  if (r->reg_flags & RegVRex)
10930
0
    i.vrex |= rex_bit;
10931
10932
214
  if (r->reg_flags & RegRex2)
10933
7
    i.rex2 |= rex_bit;
10934
214
}
10935
10936
static INLINE void
10937
set_rex_rex2 (const reg_entry *r, unsigned int rex_bit)
10938
0
{
10939
0
  if ((r->reg_flags & RegRex) != 0)
10940
0
    i.rex |= rex_bit;
10941
0
  if ((r->reg_flags & RegRex2) != 0)
10942
0
    i.rex2 |= rex_bit;
10943
0
}
10944
10945
static int
10946
process_operands (void)
10947
964
{
10948
  /* Default segment register this instruction will use for memory
10949
     accesses.  0 means unknown.  This is only for optimizing out
10950
     unnecessary segment overrides.  */
10951
964
  const reg_entry *default_seg = NULL;
10952
10953
2.04k
  for (unsigned int j = i.imm_operands; j < i.operands; j++)
10954
1.08k
    if (i.types[j].bitfield.instance != InstanceNone)
10955
4
      i.reg_operands--;
10956
10957
964
  if (i.tm.opcode_modifier.sse2avx)
10958
0
    {
10959
      /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
10960
   need converting.  */
10961
0
      i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
10962
0
      i.prefix[REX_PREFIX] = 0;
10963
0
      pp.rex_encoding = 0;
10964
0
      pp.rex2_encoding = 0;
10965
0
    }
10966
  /* ImmExt should be processed after SSE2AVX.  */
10967
964
  else if (i.tm.opcode_modifier.immext)
10968
0
    process_immext ();
10969
10970
  /* TILEZERO is unusual in that it has a single operand encoded in ModR/M.reg,
10971
     not ModR/M.rm.  To avoid special casing this in build_modrm_byte(), fake a
10972
     new destination operand here, while converting the source one to register
10973
     number 0.  */
10974
964
  if (i.tm.mnem_off == MN_tilezero)
10975
0
    {
10976
0
      copy_operand (1, 0);
10977
0
      i.op[0].regs -= i.op[0].regs->reg_num;
10978
0
      i.operands++;
10979
0
      i.reg_operands++;
10980
0
      i.tm.operands++;
10981
0
    }
10982
10983
964
  if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
10984
0
    {
10985
0
      static const i386_operand_type regxmm = {
10986
0
        .bitfield = { .class = RegSIMD, .xmmword = 1 }
10987
0
      };
10988
0
      unsigned int dupl = i.operands;
10989
0
      unsigned int dest = dupl - 1;
10990
0
      unsigned int j;
10991
10992
      /* The destination must be an xmm register.  */
10993
0
      gas_assert (i.reg_operands
10994
0
      && MAX_OPERANDS > dupl
10995
0
      && operand_type_equal (&i.types[dest], &regxmm));
10996
10997
0
      if (i.tm.operand_types[0].bitfield.instance == Accum
10998
0
    && i.tm.operand_types[0].bitfield.xmmword)
10999
0
  {
11000
    /* Keep xmm0 for instructions with VEX prefix and 3
11001
       sources.  */
11002
0
    i.tm.operand_types[0].bitfield.instance = InstanceNone;
11003
0
    i.tm.operand_types[0].bitfield.class = RegSIMD;
11004
0
    i.reg_operands++;
11005
0
    goto duplicate;
11006
0
  }
11007
11008
0
      if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_1ST_XMM0)
11009
0
  {
11010
0
    gas_assert ((MAX_OPERANDS - 1) > dupl);
11011
11012
    /* Add the implicit xmm0 for instructions with VEX prefix
11013
       and 3 sources.  */
11014
0
    for (j = i.operands; j > 0; j--)
11015
0
      copy_operand (j, j - 1);
11016
0
    i.op[0].regs = str_hash_find (reg_hash, "xmm0");
11017
0
    i.types[0] = regxmm;
11018
0
    i.tm.operand_types[0] = regxmm;
11019
11020
0
    i.operands += 2;
11021
0
    i.reg_operands += 2;
11022
0
    i.tm.operands += 2;
11023
11024
0
    dupl++;
11025
0
    dest++;
11026
0
  }
11027
0
      else
11028
0
  {
11029
0
  duplicate:
11030
0
    i.operands++;
11031
0
    i.reg_operands++;
11032
0
    i.tm.operands++;
11033
0
  }
11034
11035
0
      copy_operand (dupl, dest);
11036
11037
0
      if (i.tm.opcode_modifier.immext)
11038
0
  process_immext ();
11039
0
    }
11040
964
  else if (i.tm.operand_types[0].bitfield.instance == Accum
11041
0
     && i.tm.opcode_modifier.modrm)
11042
0
    {
11043
0
      unsigned int j;
11044
11045
0
      for (j = 1; j < i.operands; j++)
11046
0
  copy_operand (j - 1, j);
11047
11048
      /* No adjustment to i.reg_operands: This was already done at the top
11049
   of the function.  */
11050
0
      i.operands--;
11051
0
      i.tm.operands--;
11052
0
    }
11053
964
  else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_GROUP)
11054
0
    {
11055
0
      unsigned int op, extra;
11056
0
      const reg_entry *first;
11057
11058
      /* The second operand must be {x,y,z,t}mmN.  */
11059
0
      gas_assert ((i.operands == 2 || i.operands == 3)
11060
0
      && i.types[1].bitfield.class == RegSIMD);
11061
11062
0
      switch (i.types[i.operands - 1].bitfield.class)
11063
0
  {
11064
0
  case RegSIMD:
11065
0
    op = 1;
11066
0
    if (i.operands == 2)
11067
0
      {
11068
        /* AMX-TRANSPOSE operand 2: N must be a multiple of 2. */
11069
0
        extra = 1;
11070
0
      }
11071
0
    else
11072
0
      {
11073
        /* AVX512-{4FMAPS,4VNNIW} operand 2: N must be a multiple of 4. */
11074
0
        extra = 3;
11075
0
      }
11076
0
    break;
11077
11078
0
  case RegMask:
11079
    /* AVX512-VP2INTERSECT operand 3: N must be a multiple of 2. */
11080
0
    op = 2;
11081
0
    extra = 1;
11082
0
    break;
11083
11084
0
  default:
11085
0
    abort ();
11086
0
  }
11087
11088
0
      first = i.op[op].regs - (register_number (i.op[op].regs) & extra);
11089
0
      if (i.op[op].regs != first)
11090
0
  as_warn (_("operand %u `%s%s' implicitly denotes"
11091
0
       " `%s%s' to `%s%s' group in `%s'"),
11092
0
     intel_syntax ? i.operands - op : op + 1,
11093
0
     register_prefix, i.op[op].regs->reg_name,
11094
0
     register_prefix, first[0].reg_name,
11095
0
     register_prefix, first[extra].reg_name,
11096
0
     insn_name (&i.tm));
11097
0
    }
11098
964
  else if (i.tm.opcode_modifier.operandconstraint == REG_KLUDGE)
11099
0
    {
11100
      /* The imul $imm, %reg instruction is converted into
11101
   imul $imm, %reg, %reg, and the clr %reg instruction
11102
   is converted into xor %reg, %reg.  */
11103
11104
0
      unsigned int first_reg_op;
11105
11106
0
      if (operand_type_check (i.types[0], reg))
11107
0
  first_reg_op = 0;
11108
0
      else
11109
0
  first_reg_op = 1;
11110
      /* Pretend we saw the extra register operand.  */
11111
0
      gas_assert (i.reg_operands == 1
11112
0
      && i.op[first_reg_op + 1].regs == 0);
11113
0
      i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
11114
0
      i.types[first_reg_op + 1] = i.types[first_reg_op];
11115
0
      i.operands++;
11116
0
      i.reg_operands++;
11117
11118
      /* For IMULZU switch around the constraint.  */
11119
0
      if (i.tm.mnem_off == MN_imulzu)
11120
0
  i.tm.opcode_modifier.operandconstraint = ZERO_UPPER;
11121
0
    }
11122
11123
964
  if (i.tm.opcode_modifier.modrm)
11124
608
    {
11125
      /* The opcode is completed (modulo i.tm.extension_opcode which
11126
   must be put into the modrm byte).  Now, we make the modrm and
11127
   index base bytes based on all the info we've collected.  */
11128
11129
608
      default_seg = build_modrm_byte ();
11130
11131
608
      if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
11132
0
  {
11133
    /* Warn about some common errors, but press on regardless.  */
11134
0
    if (i.operands == 2)
11135
0
      {
11136
        /* Reversed arguments on faddp or fmulp.  */
11137
0
        as_warn (_("translating to `%s %s%s,%s%s'"), insn_name (&i.tm),
11138
0
           register_prefix, i.op[!intel_syntax].regs->reg_name,
11139
0
           register_prefix, i.op[intel_syntax].regs->reg_name);
11140
0
      }
11141
0
    else if (i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
11142
0
      {
11143
        /* Extraneous `l' suffix on fp insn.  */
11144
0
        as_warn (_("translating to `%s %s%s'"), insn_name (&i.tm),
11145
0
           register_prefix, i.op[0].regs->reg_name);
11146
0
      }
11147
0
  }
11148
608
    }
11149
356
  else if (i.types[0].bitfield.class == SReg && !dot_insn ())
11150
0
    {
11151
0
      if (flag_code != CODE_64BIT
11152
0
    ? i.tm.base_opcode == POP_SEG_SHORT
11153
0
      && i.op[0].regs->reg_num == 1
11154
0
    : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
11155
0
      && i.op[0].regs->reg_num < 4)
11156
0
  {
11157
0
    as_bad (_("you can't `%s %s%s'"),
11158
0
      insn_name (&i.tm), register_prefix, i.op[0].regs->reg_name);
11159
0
    return 0;
11160
0
  }
11161
0
      if (i.op[0].regs->reg_num > 3
11162
0
    && i.tm.opcode_space == SPACE_BASE )
11163
0
  {
11164
0
    i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
11165
0
    i.tm.opcode_space = SPACE_0F;
11166
0
  }
11167
0
      i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
11168
0
    }
11169
356
  else if (i.tm.opcode_space == SPACE_BASE
11170
354
     && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
11171
0
    {
11172
0
      default_seg = reg_ds;
11173
0
    }
11174
356
  else if (i.tm.opcode_modifier.isstring)
11175
32
    {
11176
      /* For the string instructions that allow a segment override
11177
   on one of their operands, the default segment is ds.  */
11178
32
      default_seg = reg_ds;
11179
32
    }
11180
324
  else if (i.short_form)
11181
44
    {
11182
      /* The register operand is in the 1st or 2nd non-immediate operand.  */
11183
44
      const reg_entry *r = i.op[i.imm_operands].regs;
11184
11185
44
      if (!dot_insn ()
11186
38
    && r->reg_type.bitfield.instance == Accum
11187
23
    && i.op[i.imm_operands + 1].regs)
11188
0
  r = i.op[i.imm_operands + 1].regs;
11189
      /* Register goes in low 3 bits of opcode.  */
11190
44
      i.tm.base_opcode |= r->reg_num;
11191
44
      set_rex_vrex (r, REX_B, false);
11192
11193
44
      if (dot_insn () && i.reg_operands == 2)
11194
0
  {
11195
0
    gas_assert (is_any_vex_encoding (&i.tm)
11196
0
          || pp.encoding != encoding_default);
11197
0
    i.vex.register_specifier = i.op[i.operands - 1].regs;
11198
0
  }
11199
44
    }
11200
280
  else if (i.reg_operands == 1
11201
2
     && !i.flags[i.operands - 1]
11202
2
     && i.tm.operand_types[i.operands - 1].bitfield.instance
11203
2
        == InstanceNone)
11204
2
    {
11205
2
      gas_assert (is_any_vex_encoding (&i.tm)
11206
2
      || pp.encoding != encoding_default);
11207
2
      i.vex.register_specifier = i.op[i.operands - 1].regs;
11208
2
    }
11209
11210
964
  if ((i.seg[0] || i.prefix[SEG_PREFIX])
11211
27
      && i.tm.mnem_off == MN_lea)
11212
0
    {
11213
0
      if (!quiet_warnings)
11214
0
  as_warn (_("segment override on `%s' is ineffectual"), insn_name (&i.tm));
11215
0
      if (optimize && !pp.no_optimize)
11216
0
  {
11217
0
    i.seg[0] = NULL;
11218
0
    i.prefix[SEG_PREFIX] = 0;
11219
0
  }
11220
0
    }
11221
11222
  /* If a segment was explicitly specified, and the specified segment
11223
     is neither the default nor the one already recorded from a prefix,
11224
     use an opcode prefix to select it.  If we never figured out what
11225
     the default segment is, then default_seg will be zero at this
11226
     point, and the specified segment prefix will always be used.  */
11227
964
  if (i.seg[0]
11228
5
      && i.seg[0] != default_seg
11229
5
      && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
11230
5
    {
11231
5
      if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
11232
0
  return 0;
11233
5
    }
11234
964
  return 1;
11235
964
}
11236
11237
static const reg_entry *
11238
build_modrm_byte (void)
11239
608
{
11240
608
  const reg_entry *default_seg = NULL;
11241
608
  unsigned int source = i.imm_operands - i.tm.opcode_modifier.immext
11242
      /* Compensate for kludge in md_assemble().  */
11243
608
      + i.tm.operand_types[0].bitfield.imm1;
11244
608
  unsigned int dest = i.operands - 1 - i.tm.opcode_modifier.immext;
11245
608
  unsigned int v, op, reg_slot;
11246
11247
  /* Accumulator (in particular %st), shift count (%cl), and alike need
11248
     to be skipped just like immediate operands do.  */
11249
608
  if (i.tm.operand_types[source].bitfield.instance)
11250
0
    ++source;
11251
608
  while (i.tm.operand_types[dest].bitfield.instance)
11252
0
    --dest;
11253
11254
710
  for (op = source; op < i.operands; ++op)
11255
710
    if (i.tm.operand_types[op].bitfield.baseindex)
11256
608
      break;
11257
11258
608
  if (i.reg_operands + i.mem_operands + (i.tm.extension_opcode != None)
11259
608
      + (i.tm.opcode_modifier.operandconstraint == SCC) == 4)
11260
0
    {
11261
0
      expressionS *exp;
11262
11263
      /* There are 2 kinds of instructions:
11264
   1. 5 operands: 4 register operands or 3 register operands
11265
   plus 1 memory operand plus one Imm4 operand, VexXDS, and
11266
   VexW0 or VexW1.  The destination must be either XMM, YMM or
11267
   ZMM register.
11268
   2. 4 operands: 4 register operands or 3 register operands
11269
   plus 1 memory operand, with VexXDS.
11270
   3. Other equivalent combinations when coming from s_insn().  */
11271
0
      if (!dot_insn ())
11272
0
  {
11273
0
    gas_assert (i.tm.opcode_modifier.vexvvvv
11274
0
          && i.tm.opcode_modifier.vexw);
11275
0
    gas_assert (i.tm.operand_types[dest].bitfield.class == RegSIMD);
11276
0
  }
11277
11278
      /* Of the first two non-immediate operands the one with the template
11279
   not allowing for a memory one is encoded in the immediate operand.  */
11280
0
      if (source == op)
11281
0
  reg_slot = source + 1;
11282
0
      else
11283
0
  reg_slot = source++;
11284
11285
0
      if (!dot_insn ())
11286
0
  {
11287
0
    gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
11288
0
    gas_assert (!(i.op[reg_slot].regs->reg_flags & RegVRex));
11289
0
  }
11290
0
      else
11291
0
  gas_assert (i.tm.operand_types[reg_slot].bitfield.class != ClassNone);
11292
11293
0
      if (i.imm_operands == 0)
11294
0
  {
11295
    /* When there is no immediate operand, generate an 8bit
11296
       immediate operand to encode the first operand.  */
11297
0
    exp = &im_expressions[i.imm_operands++];
11298
0
    i.op[i.operands].imms = exp;
11299
0
    i.types[i.operands].bitfield.imm8 = 1;
11300
0
    i.operands++;
11301
11302
0
    exp->X_op = O_constant;
11303
0
  }
11304
0
      else
11305
0
  {
11306
0
    gas_assert (i.imm_operands == 1);
11307
0
    gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
11308
0
    gas_assert (!i.tm.opcode_modifier.immext);
11309
11310
    /* Turn on Imm8 again so that output_imm will generate it.  */
11311
0
    i.types[0].bitfield.imm8 = 1;
11312
11313
0
    exp = i.op[0].imms;
11314
0
  }
11315
0
      exp->X_add_number |= register_number (i.op[reg_slot].regs)
11316
0
         << (3 + !(i.tm.opcode_modifier.evex
11317
0
             || pp.encoding == encoding_evex));
11318
0
    }
11319
11320
608
  switch (i.tm.opcode_modifier.vexvvvv)
11321
608
    {
11322
    /* VEX.vvvv encodes the last source register operand.  */
11323
0
    case VexVVVV_SRC2:
11324
0
      v = source++;
11325
0
      break;
11326
    /* VEX.vvvv encodes the first source register operand.  */
11327
0
    case VexVVVV_SRC1:
11328
0
      v =  dest - 1;
11329
0
      break;
11330
    /* VEX.vvvv encodes the destination register operand.  */
11331
0
    case VexVVVV_DST:
11332
0
      v = dest--;
11333
0
      break;
11334
608
    default:
11335
608
      v = ~0;
11336
608
      break;
11337
608
     }
11338
11339
608
  if (dest == source)
11340
448
    dest = ~0;
11341
11342
608
  gas_assert (source < dest);
11343
11344
608
  if (v < MAX_OPERANDS)
11345
0
    {
11346
0
      gas_assert (i.tm.opcode_modifier.vexvvvv);
11347
0
      i.vex.register_specifier = i.op[v].regs;
11348
0
    }
11349
11350
608
  if (op < i.operands)
11351
608
    {
11352
608
      if (i.mem_operands)
11353
598
  {
11354
598
    unsigned int fake_zero_displacement = 0;
11355
11356
598
    gas_assert (i.flags[op] & Operand_Mem);
11357
11358
598
    if (i.tm.opcode_modifier.sib)
11359
0
      {
11360
        /* The index register of VSIB shouldn't be RegIZ.  */
11361
0
        if (i.tm.opcode_modifier.sib != SIBMEM
11362
0
      && i.index_reg->reg_num == RegIZ)
11363
0
    abort ();
11364
11365
0
        i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
11366
0
        if (!i.base_reg)
11367
0
    {
11368
0
      i.sib.base = NO_BASE_REGISTER;
11369
0
      i.sib.scale = i.log2_scale_factor;
11370
0
      i.types[op] = operand_type_and_not (i.types[op], anydisp);
11371
0
      i.types[op].bitfield.disp32 = 1;
11372
0
    }
11373
11374
        /* Since the mandatory SIB always has index register, so
11375
     the code logic remains unchanged. The non-mandatory SIB
11376
     without index register is allowed and will be handled
11377
     later.  */
11378
0
        if (i.index_reg)
11379
0
    {
11380
0
      if (i.index_reg->reg_num == RegIZ)
11381
0
        i.sib.index = NO_INDEX_REGISTER;
11382
0
      else
11383
0
        i.sib.index = i.index_reg->reg_num;
11384
0
      set_rex_vrex (i.index_reg, REX_X, false);
11385
0
    }
11386
0
      }
11387
11388
598
    default_seg = reg_ds;
11389
11390
598
    if (i.base_reg == 0)
11391
598
      {
11392
598
        i.rm.mode = 0;
11393
598
        if (!i.disp_operands)
11394
0
    fake_zero_displacement = 1;
11395
598
        if (i.index_reg == 0)
11396
598
    {
11397
      /* Both check for VSIB and mandatory non-vector SIB. */
11398
598
      gas_assert (!i.tm.opcode_modifier.sib
11399
598
            || i.tm.opcode_modifier.sib == SIBMEM);
11400
      /* Operand is just <disp>  */
11401
598
      i.types[op] = operand_type_and_not (i.types[op], anydisp);
11402
598
      if (flag_code == CODE_64BIT)
11403
280
        {
11404
          /* 64bit mode overwrites the 32bit absolute
11405
       addressing by RIP relative addressing and
11406
       absolute addressing is encoded by one of the
11407
       redundant SIB forms.  */
11408
280
          i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
11409
280
          i.sib.base = NO_BASE_REGISTER;
11410
280
          i.sib.index = NO_INDEX_REGISTER;
11411
280
          i.types[op].bitfield.disp32 = 1;
11412
280
        }
11413
318
      else if ((flag_code == CODE_16BIT)
11414
318
         ^ (i.prefix[ADDR_PREFIX] != 0))
11415
317
        {
11416
317
          i.rm.regmem = NO_BASE_REGISTER_16;
11417
317
          i.types[op].bitfield.disp16 = 1;
11418
317
        }
11419
1
      else
11420
1
        {
11421
1
          i.rm.regmem = NO_BASE_REGISTER;
11422
1
          i.types[op].bitfield.disp32 = 1;
11423
1
        }
11424
598
    }
11425
0
        else if (!i.tm.opcode_modifier.sib)
11426
0
    {
11427
      /* !i.base_reg && i.index_reg  */
11428
0
      if (i.index_reg->reg_num == RegIZ)
11429
0
        i.sib.index = NO_INDEX_REGISTER;
11430
0
      else
11431
0
        i.sib.index = i.index_reg->reg_num;
11432
0
      i.sib.base = NO_BASE_REGISTER;
11433
0
      i.sib.scale = i.log2_scale_factor;
11434
0
      i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
11435
0
      i.types[op] = operand_type_and_not (i.types[op], anydisp);
11436
0
      i.types[op].bitfield.disp32 = 1;
11437
0
      set_rex_rex2 (i.index_reg, REX_X);
11438
0
    }
11439
598
      }
11440
    /* RIP addressing for 64bit mode.  */
11441
0
    else if (i.base_reg->reg_num == RegIP)
11442
0
      {
11443
0
        gas_assert (!i.tm.opcode_modifier.sib);
11444
0
        i.rm.regmem = NO_BASE_REGISTER;
11445
0
        i.types[op].bitfield.disp8 = 0;
11446
0
        i.types[op].bitfield.disp16 = 0;
11447
0
        i.types[op].bitfield.disp32 = 1;
11448
0
        i.types[op].bitfield.disp64 = 0;
11449
0
        i.flags[op] |= Operand_PCrel;
11450
0
        if (! i.disp_operands)
11451
0
    fake_zero_displacement = 1;
11452
0
      }
11453
0
    else if (i.base_reg->reg_type.bitfield.word)
11454
0
      {
11455
0
        gas_assert (!i.tm.opcode_modifier.sib);
11456
0
        switch (i.base_reg->reg_num)
11457
0
    {
11458
0
    case 3: /* (%bx)  */
11459
0
      if (i.index_reg == 0)
11460
0
        i.rm.regmem = 7;
11461
0
      else /* (%bx,%si) -> 0, or (%bx,%di) -> 1  */
11462
0
        i.rm.regmem = i.index_reg->reg_num - 6;
11463
0
      break;
11464
0
    case 5: /* (%bp)  */
11465
0
      default_seg = reg_ss;
11466
0
      if (i.index_reg == 0)
11467
0
        {
11468
0
          i.rm.regmem = 6;
11469
0
          if (operand_type_check (i.types[op], disp) == 0)
11470
0
      {
11471
        /* fake (%bp) into 0(%bp)  */
11472
0
        if (pp.disp_encoding == disp_encoding_16bit)
11473
0
          i.types[op].bitfield.disp16 = 1;
11474
0
        else
11475
0
          i.types[op].bitfield.disp8 = 1;
11476
0
        fake_zero_displacement = 1;
11477
0
      }
11478
0
        }
11479
0
      else /* (%bp,%si) -> 2, or (%bp,%di) -> 3  */
11480
0
        i.rm.regmem = i.index_reg->reg_num - 6 + 2;
11481
0
      break;
11482
0
    default: /* (%si) -> 4 or (%di) -> 5  */
11483
0
      i.rm.regmem = i.base_reg->reg_num - 6 + 4;
11484
0
    }
11485
0
        if (!fake_zero_displacement
11486
0
      && !i.disp_operands
11487
0
      && pp.disp_encoding)
11488
0
    {
11489
0
      fake_zero_displacement = 1;
11490
0
      if (pp.disp_encoding == disp_encoding_8bit)
11491
0
        i.types[op].bitfield.disp8 = 1;
11492
0
      else
11493
0
        i.types[op].bitfield.disp16 = 1;
11494
0
    }
11495
0
        i.rm.mode = mode_from_disp_size (i.types[op]);
11496
0
      }
11497
0
    else /* i.base_reg and 32/64 bit mode  */
11498
0
      {
11499
0
        if (operand_type_check (i.types[op], disp))
11500
0
    {
11501
0
      i.types[op].bitfield.disp16 = 0;
11502
0
      i.types[op].bitfield.disp64 = 0;
11503
0
      i.types[op].bitfield.disp32 = 1;
11504
0
    }
11505
11506
0
        if (!i.tm.opcode_modifier.sib)
11507
0
    i.rm.regmem = i.base_reg->reg_num;
11508
0
        set_rex_rex2 (i.base_reg, REX_B);
11509
0
        i.sib.base = i.base_reg->reg_num;
11510
        /* x86-64 ignores REX prefix bit here to avoid decoder
11511
     complications.  */
11512
0
        if (!(i.base_reg->reg_flags & RegRex)
11513
0
      && (i.base_reg->reg_num == EBP_REG_NUM
11514
0
       || i.base_reg->reg_num == ESP_REG_NUM))
11515
0
      default_seg = reg_ss;
11516
0
        if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
11517
0
    {
11518
0
      fake_zero_displacement = 1;
11519
0
      if (pp.disp_encoding == disp_encoding_32bit)
11520
0
        i.types[op].bitfield.disp32 = 1;
11521
0
      else
11522
0
        i.types[op].bitfield.disp8 = 1;
11523
0
    }
11524
0
        i.sib.scale = i.log2_scale_factor;
11525
0
        if (i.index_reg == 0)
11526
0
    {
11527
      /* Only check for VSIB. */
11528
0
      gas_assert (i.tm.opcode_modifier.sib != VECSIB128
11529
0
            && i.tm.opcode_modifier.sib != VECSIB256
11530
0
            && i.tm.opcode_modifier.sib != VECSIB512);
11531
11532
      /* <disp>(%esp) becomes two byte modrm with no index
11533
         register.  We've already stored the code for esp
11534
         in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
11535
         Any base register besides %esp will not use the
11536
         extra modrm byte.  */
11537
0
      i.sib.index = NO_INDEX_REGISTER;
11538
0
    }
11539
0
        else if (!i.tm.opcode_modifier.sib)
11540
0
    {
11541
0
      if (i.index_reg->reg_num == RegIZ)
11542
0
        i.sib.index = NO_INDEX_REGISTER;
11543
0
      else
11544
0
        i.sib.index = i.index_reg->reg_num;
11545
0
      i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
11546
0
      set_rex_rex2 (i.index_reg, REX_X);
11547
0
    }
11548
11549
0
        if (i.disp_operands
11550
0
      && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
11551
0
          || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
11552
0
    i.rm.mode = 0;
11553
0
        else
11554
0
    {
11555
0
      if (!fake_zero_displacement
11556
0
          && !i.disp_operands
11557
0
          && pp.disp_encoding)
11558
0
        {
11559
0
          fake_zero_displacement = 1;
11560
0
          if (pp.disp_encoding == disp_encoding_8bit)
11561
0
      i.types[op].bitfield.disp8 = 1;
11562
0
          else
11563
0
      i.types[op].bitfield.disp32 = 1;
11564
0
        }
11565
0
      i.rm.mode = mode_from_disp_size (i.types[op]);
11566
0
    }
11567
0
      }
11568
11569
598
    if (fake_zero_displacement)
11570
0
      {
11571
        /* Fakes a zero displacement assuming that i.types[op]
11572
     holds the correct displacement size.  */
11573
0
        expressionS *exp;
11574
11575
0
        gas_assert (i.op[op].disps == 0);
11576
0
        exp = &disp_expressions[i.disp_operands++];
11577
0
        i.op[op].disps = exp;
11578
0
        exp->X_op = O_constant;
11579
0
        exp->X_add_number = 0;
11580
0
        exp->X_add_symbol = NULL;
11581
0
        exp->X_op_symbol = NULL;
11582
0
      }
11583
598
  }
11584
10
    else
11585
10
  {
11586
10
      i.rm.mode = 3;
11587
10
      i.rm.regmem = i.op[op].regs->reg_num;
11588
10
      set_rex_vrex (i.op[op].regs, REX_B, false);
11589
10
  }
11590
11591
608
      if (op == dest)
11592
102
  dest = ~0;
11593
608
      if (op == source)
11594
506
  source = ~0;
11595
608
    }
11596
0
  else
11597
0
    {
11598
0
      i.rm.mode = 3;
11599
0
      if (!i.tm.opcode_modifier.regmem)
11600
0
  {
11601
0
    gas_assert (source < MAX_OPERANDS);
11602
0
    i.rm.regmem = i.op[source].regs->reg_num;
11603
0
    set_rex_vrex (i.op[source].regs, REX_B,
11604
0
      dest >= MAX_OPERANDS && i.tm.opcode_modifier.sse2avx);
11605
0
    source = ~0;
11606
0
  }
11607
0
      else
11608
0
  {
11609
0
    gas_assert (dest < MAX_OPERANDS);
11610
0
    i.rm.regmem = i.op[dest].regs->reg_num;
11611
0
    set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
11612
0
    dest = ~0;
11613
0
  }
11614
0
    }
11615
11616
  /* Fill in i.rm.reg field with extension opcode (if any) or the
11617
     appropriate register.  */
11618
608
  if (i.tm.extension_opcode != None)
11619
448
    i.rm.reg = i.tm.extension_opcode;
11620
160
  else if (!i.tm.opcode_modifier.regmem && dest < MAX_OPERANDS)
11621
58
    {
11622
58
      i.rm.reg = i.op[dest].regs->reg_num;
11623
58
      set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
11624
58
    }
11625
102
  else
11626
102
    {
11627
102
      gas_assert (source < MAX_OPERANDS);
11628
102
      i.rm.reg = i.op[source].regs->reg_num;
11629
102
      set_rex_vrex (i.op[source].regs, REX_R, false);
11630
102
    }
11631
11632
608
  if (flag_code != CODE_64BIT && (i.rex & REX_R))
11633
0
    {
11634
0
      gas_assert (i.types[!i.tm.opcode_modifier.regmem].bitfield.class == RegCR);
11635
0
      i.rex &= ~REX_R;
11636
0
      add_prefix (LOCK_PREFIX_OPCODE);
11637
0
    }
11638
11639
608
  return default_seg;
11640
608
}
11641
11642
static INLINE void
11643
frag_opcode_byte (unsigned char byte)
11644
1.27k
{
11645
1.27k
  if (now_seg != absolute_section)
11646
964
    FRAG_APPEND_1_CHAR (byte);
11647
309
  else
11648
309
    ++abs_section_offset;
11649
1.27k
}
11650
11651
static unsigned int
11652
flip_code16 (unsigned int code16)
11653
96
{
11654
96
  gas_assert (i.tm.operands == 1);
11655
11656
96
  return !(i.prefix[REX_PREFIX] & REX_W)
11657
96
   && (code16 ? i.tm.operand_types[0].bitfield.disp32
11658
96
        : i.tm.operand_types[0].bitfield.disp16)
11659
96
   ? CODE16 : 0;
11660
96
}
11661
11662
static void
11663
output_branch (void)
11664
107
{
11665
107
  char *p;
11666
107
  int size;
11667
107
  int code16;
11668
107
  int prefix;
11669
107
  relax_substateT subtype;
11670
107
  symbolS *sym;
11671
107
  offsetT off;
11672
11673
107
  if (now_seg == absolute_section)
11674
2
    {
11675
2
      as_bad (_("relaxable branches not supported in absolute section"));
11676
2
      return;
11677
2
    }
11678
11679
105
  code16 = flag_code == CODE_16BIT ? CODE16 : 0;
11680
105
  size = pp.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
11681
11682
105
  prefix = 0;
11683
105
  if (i.prefix[DATA_PREFIX] != 0)
11684
56
    {
11685
56
      prefix = 1;
11686
56
      i.prefixes -= 1;
11687
56
      code16 ^= flip_code16(code16);
11688
56
    }
11689
  /* Pentium4 branch hints.  */
11690
105
  if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
11691
96
      || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
11692
9
    {
11693
9
      prefix++;
11694
9
      i.prefixes--;
11695
9
    }
11696
105
  if (i.prefix[REX_PREFIX] != 0)
11697
0
    {
11698
0
      prefix++;
11699
0
      i.prefixes--;
11700
0
    }
11701
11702
  /* BND prefixed jump.  */
11703
105
  if (i.prefix[BND_PREFIX] != 0)
11704
0
    {
11705
0
      prefix++;
11706
0
      i.prefixes--;
11707
0
    }
11708
11709
105
  if (i.prefixes != 0)
11710
3
    as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
11711
11712
  /* It's always a symbol;  End frag & setup for relax.
11713
     Make sure there is enough room in this frag for the largest
11714
     instruction we may generate in md_convert_frag.  This is 2
11715
     bytes for the opcode and room for the prefix and largest
11716
     displacement.  */
11717
105
  frag_grow (prefix + 2 + 4);
11718
  /* Prefix and 1 opcode byte go in fr_fix.  */
11719
105
  p = frag_more (prefix + 1);
11720
105
  if (i.prefix[DATA_PREFIX] != 0)
11721
56
    *p++ = DATA_PREFIX_OPCODE;
11722
105
  if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
11723
96
      || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
11724
9
    *p++ = i.prefix[SEG_PREFIX];
11725
105
  if (i.prefix[BND_PREFIX] != 0)
11726
0
    *p++ = BND_PREFIX_OPCODE;
11727
105
  if (i.prefix[REX_PREFIX] != 0)
11728
0
    *p++ = i.prefix[REX_PREFIX];
11729
105
  *p = i.tm.base_opcode;
11730
11731
105
  if ((unsigned char) *p == JUMP_PC_RELATIVE)
11732
67
    subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
11733
38
  else if (cpu_arch_flags.bitfield.cpui386)
11734
38
    subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
11735
0
  else
11736
0
    subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
11737
105
  subtype |= code16;
11738
11739
105
  sym = i.op[0].disps->X_add_symbol;
11740
105
  off = i.op[0].disps->X_add_number;
11741
11742
105
  if (i.op[0].disps->X_op != O_constant
11743
105
      && i.op[0].disps->X_op != O_symbol)
11744
10
    {
11745
      /* Handle complex expressions.  */
11746
10
      sym = make_expr_symbol (i.op[0].disps);
11747
10
      off = 0;
11748
10
    }
11749
11750
  /* 1 possible extra opcode + 4 byte displacement go in var part.
11751
     Pass reloc in fr_var.  */
11752
105
  frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
11753
105
}
11754
11755
/* PLT32 relocation is ELF only.  */
11756
#ifdef OBJ_ELF
11757
/* Return TRUE iff PLT32 relocation should be used for branching to
11758
   symbol S.  */
11759
11760
static bool
11761
need_plt32_p (symbolS *s)
11762
31
{
11763
#ifdef TE_SOLARIS
11764
  /* Don't emit PLT32 relocation on Solaris: neither native linker nor
11765
     krtld support it.  */
11766
  return false;
11767
#endif
11768
11769
  /* Since there is no need to prepare for PLT branch on x86-64, we
11770
     can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
11771
     be used as a marker for 32-bit PC-relative branches.  */
11772
31
  if (!object_64bit)
11773
0
    return false;
11774
11775
31
  if (s == NULL)
11776
0
    return false;
11777
11778
  /* Weak or undefined symbol need PLT32 relocation.  */
11779
31
  if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
11780
31
    return true;
11781
11782
  /* Non-global symbol doesn't need PLT32 relocation.  */
11783
0
  if (! S_IS_EXTERNAL (s))
11784
0
    return false;
11785
11786
  /* Other global symbols need PLT32 relocation.  NB: Symbol with
11787
     non-default visibilities are treated as normal global symbol
11788
     so that PLT32 relocation can be used as a marker for 32-bit
11789
     PC-relative branches.  It is useful for linker relaxation.  */
11790
0
  return true;
11791
0
}
11792
#endif
11793
11794
static void
11795
output_jump (void)
11796
125
{
11797
125
  char *p;
11798
125
  int size;
11799
125
  fixS *fixP;
11800
125
  bfd_reloc_code_real_type jump_reloc = i.reloc[0];
11801
11802
125
  if (i.tm.opcode_modifier.jump == JUMP_BYTE)
11803
15
    {
11804
      /* This is a loop or jecxz type instruction.  */
11805
15
      size = 1;
11806
15
      if (i.prefix[ADDR_PREFIX] != 0)
11807
15
  {
11808
15
    frag_opcode_byte (ADDR_PREFIX_OPCODE);
11809
15
    i.prefixes -= 1;
11810
15
  }
11811
      /* Pentium4 branch hints.  */
11812
15
      if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
11813
5
    || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
11814
10
  {
11815
10
    frag_opcode_byte (i.prefix[SEG_PREFIX]);
11816
10
    i.prefixes--;
11817
10
  }
11818
15
    }
11819
110
  else
11820
110
    {
11821
110
      int code16;
11822
11823
110
      code16 = 0;
11824
110
      if (flag_code == CODE_16BIT)
11825
1
  code16 = CODE16;
11826
11827
110
      if (i.prefix[DATA_PREFIX] != 0)
11828
40
  {
11829
40
    frag_opcode_byte (DATA_PREFIX_OPCODE);
11830
40
    i.prefixes -= 1;
11831
40
    code16 ^= flip_code16(code16);
11832
40
  }
11833
11834
110
      size = 4;
11835
110
      if (code16)
11836
41
  size = 2;
11837
110
    }
11838
11839
  /* BND prefixed jump.  */
11840
125
  if (i.prefix[BND_PREFIX] != 0)
11841
0
    {
11842
0
      frag_opcode_byte (i.prefix[BND_PREFIX]);
11843
0
      i.prefixes -= 1;
11844
0
    }
11845
11846
125
  if (i.prefix[REX_PREFIX] != 0)
11847
3
    {
11848
3
      frag_opcode_byte (i.prefix[REX_PREFIX]);
11849
3
      i.prefixes -= 1;
11850
3
    }
11851
11852
125
  if (i.prefixes != 0)
11853
0
    as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
11854
11855
125
  if (now_seg == absolute_section)
11856
62
    {
11857
62
      abs_section_offset += i.opcode_length + size;
11858
62
      return;
11859
62
    }
11860
11861
63
  p = frag_more (i.opcode_length + size);
11862
63
  switch (i.opcode_length)
11863
63
    {
11864
0
    case 2:
11865
0
      *p++ = i.tm.base_opcode >> 8;
11866
      /* Fall through.  */
11867
63
    case 1:
11868
63
      *p++ = i.tm.base_opcode;
11869
63
      break;
11870
0
    default:
11871
0
      abort ();
11872
63
    }
11873
11874
63
#ifdef OBJ_ELF
11875
63
  if (flag_code == CODE_64BIT && size == 4
11876
34
      && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
11877
31
      && need_plt32_p (i.op[0].disps->X_add_symbol))
11878
31
    jump_reloc = BFD_RELOC_32_PLT_PCREL;
11879
63
#endif
11880
11881
63
  jump_reloc = reloc (size, 1, 1, jump_reloc);
11882
11883
63
  fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
11884
63
          i.op[0].disps, 1, jump_reloc);
11885
11886
  /* All jumps handled here are signed, but don't unconditionally use a
11887
     signed limit check for 32 and 16 bit jumps as we want to allow wrap
11888
     around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
11889
     respectively.  */
11890
63
  switch (size)
11891
63
    {
11892
15
    case 1:
11893
15
      fixP->fx_signed = 1;
11894
15
      break;
11895
11896
14
    case 2:
11897
14
      if (i.tm.mnem_off == MN_xbegin)
11898
0
  fixP->fx_signed = 1;
11899
14
      break;
11900
11901
34
    case 4:
11902
34
      if (flag_code == CODE_64BIT)
11903
34
  fixP->fx_signed = 1;
11904
34
      break;
11905
63
    }
11906
63
}
11907
11908
static void
11909
output_interseg_jump (void)
11910
11
{
11911
11
  char *p;
11912
11
  int size;
11913
11
  int prefix;
11914
11
  int code16;
11915
11916
11
  code16 = 0;
11917
11
  if (flag_code == CODE_16BIT)
11918
2
    code16 = CODE16;
11919
11920
11
  prefix = 0;
11921
11
  if (i.prefix[DATA_PREFIX] != 0)
11922
1
    {
11923
1
      prefix = 1;
11924
1
      i.prefixes -= 1;
11925
1
      code16 ^= CODE16;
11926
1
    }
11927
11928
11
  gas_assert (!i.prefix[REX_PREFIX]);
11929
11930
11
  size = 4;
11931
11
  if (code16)
11932
1
    size = 2;
11933
11934
11
  if (i.prefixes != 0)
11935
0
    as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
11936
11937
11
  if (now_seg == absolute_section)
11938
2
    {
11939
2
      abs_section_offset += prefix + 1 + 2 + size;
11940
2
      return;
11941
2
    }
11942
11943
  /* 1 opcode; 2 segment; offset  */
11944
9
  p = frag_more (prefix + 1 + 2 + size);
11945
11946
9
  if (i.prefix[DATA_PREFIX] != 0)
11947
1
    *p++ = DATA_PREFIX_OPCODE;
11948
11949
9
  if (i.prefix[REX_PREFIX] != 0)
11950
0
    *p++ = i.prefix[REX_PREFIX];
11951
11952
9
  *p++ = i.tm.base_opcode;
11953
9
  if (i.op[1].imms->X_op == O_constant)
11954
8
    {
11955
8
      offsetT n = i.op[1].imms->X_add_number;
11956
11957
8
      if (size == 2
11958
1
    && !fits_in_unsigned_word (n)
11959
1
    && !fits_in_signed_word (n))
11960
1
  {
11961
1
    as_bad (_("16-bit jump out of range"));
11962
1
    return;
11963
1
  }
11964
7
      md_number_to_chars (p, n, size);
11965
7
    }
11966
1
  else
11967
1
    fix_new_exp (frag_now, p - frag_now->fr_literal, size,
11968
1
     i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
11969
11970
8
  p += size;
11971
8
  if (i.op[0].imms->X_op == O_constant)
11972
5
    md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
11973
3
  else
11974
3
    fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
11975
3
     i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
11976
8
}
11977
11978
/* Hook used to reject pseudo-prefixes misplaced at the start of a line.  */
11979
11980
void i386_start_line (void)
11981
496k
{
11982
496k
  struct pseudo_prefixes last_pp;
11983
11984
496k
  memcpy (&last_pp, &pp, sizeof (pp));
11985
496k
  memset (&pp, 0, sizeof (pp));
11986
496k
  if (memcmp (&pp, &last_pp, sizeof (pp)))
11987
2
    as_bad_where (frag_now->fr_file, frag_now->fr_line,
11988
2
      _("pseudo prefix without instruction"));
11989
496k
}
11990
11991
/* Hook used to warn about pseudo-prefixes ahead of a label.  */
11992
11993
bool i386_check_label (void)
11994
4.25k
{
11995
4.25k
  struct pseudo_prefixes last_pp;
11996
11997
4.25k
  memcpy (&last_pp, &pp, sizeof (pp));
11998
4.25k
  memset (&pp, 0, sizeof (pp));
11999
4.25k
  if (memcmp (&pp, &last_pp, sizeof (pp)))
12000
286
    as_warn (_("pseudo prefix ahead of label; ignoring"));
12001
4.25k
  return true;
12002
4.25k
}
12003
12004
/* Hook used to parse pseudo-prefixes off of the start of a line.  */
12005
12006
int
12007
i386_unrecognized_line (int ch)
12008
32.1k
{
12009
32.1k
  char mnemonic[MAX_MNEM_SIZE];
12010
32.1k
  const char *end;
12011
12012
32.1k
  if (ch != '{')
12013
31.1k
    return 0;
12014
12015
970
  --input_line_pointer;
12016
970
  know (*input_line_pointer == ch);
12017
12018
970
  end = parse_insn (input_line_pointer, mnemonic, parse_pseudo_prefix);
12019
970
  if (end == NULL)
12020
0
    {
12021
      /* Diagnostic was already issued.  */
12022
0
      ignore_rest_of_line ();
12023
0
      memset (&pp, 0, sizeof (pp));
12024
0
      return 1;
12025
0
    }
12026
12027
970
  if (end == input_line_pointer)
12028
93
    {
12029
93
      ++input_line_pointer;
12030
93
      return 0;
12031
93
    }
12032
12033
877
  input_line_pointer += end - input_line_pointer;
12034
877
  return 1;
12035
970
}
12036
12037
#ifdef OBJ_ELF
12038
void
12039
x86_cleanup (void)
12040
567
{
12041
567
  char *p;
12042
567
  asection *seg = now_seg;
12043
567
  subsegT subseg = now_subseg;
12044
567
  asection *sec;
12045
567
  unsigned int alignment, align_size_1;
12046
567
  unsigned int isa_1_descsz, feature_2_descsz, descsz;
12047
567
  unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
12048
567
  unsigned int padding;
12049
12050
567
  if (!x86_used_note)
12051
0
    return;
12052
12053
567
  x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
12054
12055
  /* The .note.gnu.property section layout:
12056
12057
     Field  Length    Contents
12058
     ---- ----    ----
12059
     n_namsz  4   4
12060
     n_descsz 4   The note descriptor size
12061
     n_type 4   NT_GNU_PROPERTY_TYPE_0
12062
     n_name 4   "GNU"
12063
     n_desc n_descsz  The program property array
12064
     .... ....    ....
12065
   */
12066
12067
  /* Create the .note.gnu.property section.  */
12068
567
  sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
12069
567
  bfd_set_section_flags (sec,
12070
567
       (SEC_ALLOC
12071
567
        | SEC_LOAD
12072
567
        | SEC_DATA
12073
567
        | SEC_HAS_CONTENTS
12074
567
        | SEC_READONLY));
12075
12076
567
  if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
12077
567
    {
12078
567
      align_size_1 = 7;
12079
567
      alignment = 3;
12080
567
    }
12081
0
  else
12082
0
    {
12083
0
      align_size_1 = 3;
12084
0
      alignment = 2;
12085
0
    }
12086
12087
567
  bfd_set_section_alignment (sec, alignment);
12088
567
  elf_section_type (sec) = SHT_NOTE;
12089
12090
  /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
12091
          + 4-byte data  */
12092
567
  isa_1_descsz_raw = 4 + 4 + 4;
12093
  /* Align GNU_PROPERTY_X86_ISA_1_USED.  */
12094
567
  isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
12095
12096
567
  feature_2_descsz_raw = isa_1_descsz;
12097
  /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
12098
              + 4-byte data  */
12099
567
  feature_2_descsz_raw += 4 + 4 + 4;
12100
  /* Align GNU_PROPERTY_X86_FEATURE_2_USED.  */
12101
567
  feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
12102
567
          & ~align_size_1);
12103
12104
567
  descsz = feature_2_descsz;
12105
  /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz.  */
12106
567
  p = frag_more (4 + 4 + 4 + 4 + descsz);
12107
12108
  /* Write n_namsz.  */
12109
567
  md_number_to_chars (p, (valueT) 4, 4);
12110
12111
  /* Write n_descsz.  */
12112
567
  md_number_to_chars (p + 4, (valueT) descsz, 4);
12113
12114
  /* Write n_type.  */
12115
567
  md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
12116
12117
  /* Write n_name.  */
12118
567
  memcpy (p + 4 * 3, "GNU", 4);
12119
12120
  /* Write 4-byte type.  */
12121
567
  md_number_to_chars (p + 4 * 4,
12122
567
          (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
12123
12124
  /* Write 4-byte data size.  */
12125
567
  md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
12126
12127
  /* Write 4-byte data.  */
12128
567
  md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
12129
12130
  /* Zero out paddings.  */
12131
567
  padding = isa_1_descsz - isa_1_descsz_raw;
12132
567
  if (padding)
12133
567
    memset (p + 4 * 7, 0, padding);
12134
12135
  /* Write 4-byte type.  */
12136
567
  md_number_to_chars (p + isa_1_descsz + 4 * 4,
12137
567
          (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
12138
12139
  /* Write 4-byte data size.  */
12140
567
  md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
12141
12142
  /* Write 4-byte data.  */
12143
567
  md_number_to_chars (p + isa_1_descsz + 4 * 6,
12144
567
          (valueT) x86_feature_2_used, 4);
12145
12146
  /* Zero out paddings.  */
12147
567
  padding = feature_2_descsz - feature_2_descsz_raw;
12148
567
  if (padding)
12149
567
    memset (p + isa_1_descsz + 4 * 7, 0, padding);
12150
12151
  /* We probably can't restore the current segment, for there likely
12152
     isn't one yet...  */
12153
567
  if (seg && subseg)
12154
11
    subseg_set (seg, subseg);
12155
567
}
12156
12157
#include "tc-i386-ginsn.c"
12158
12159
/* Whether SFrame stack trace info is supported.  */
12160
bool
12161
x86_support_sframe_p (void)
12162
68
{
12163
  /* At this time, SFrame stack trace is supported for AMD64 ABI only.  */
12164
68
  return (x86_elf_abi == X86_64_ABI);
12165
68
}
12166
12167
/* The fixed offset from CFA for SFrame to recover the return address.
12168
   (useful only when SFrame RA tracking is not needed).  */
12169
offsetT
12170
x86_sframe_cfa_ra_offset (void)
12171
25
{
12172
25
  gas_assert (x86_elf_abi == X86_64_ABI);
12173
25
  return (offsetT) -8;
12174
25
}
12175
12176
/* The abi/arch identifier for SFrame.  */
12177
unsigned char
12178
x86_sframe_get_abi_arch (void)
12179
57
{
12180
57
  unsigned char sframe_abi_arch = 0;
12181
12182
57
  if (x86_support_sframe_p ())
12183
57
    {
12184
57
      gas_assert (!target_big_endian);
12185
57
      sframe_abi_arch = SFRAME_ABI_AMD64_ENDIAN_LITTLE;
12186
57
    }
12187
12188
57
  return sframe_abi_arch;
12189
57
}
12190
12191
#endif
12192
12193
static unsigned int
12194
encoding_length (const fragS *start_frag, offsetT start_off,
12195
     const char *frag_now_ptr)
12196
2.26k
{
12197
2.26k
  unsigned int len = 0;
12198
12199
2.26k
  if (start_frag != frag_now)
12200
0
    {
12201
0
      const fragS *fr = start_frag;
12202
12203
0
      do {
12204
0
  len += fr->fr_fix;
12205
0
  fr = fr->fr_next;
12206
0
      } while (fr && fr != frag_now);
12207
0
    }
12208
12209
2.26k
  return len - start_off + (frag_now_ptr - frag_now->fr_literal);
12210
2.26k
}
12211
12212
/* Return 1 for test, and, cmp, add, sub, inc and dec which may
12213
   be macro-fused with conditional jumps.
12214
   NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
12215
   or is one of the following format:
12216
12217
    cmp m, imm
12218
    add m, imm
12219
    sub m, imm
12220
   test m, imm
12221
    and m, imm
12222
    inc m
12223
    dec m
12224
12225
   it is unfusible.  */
12226
12227
static int
12228
maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
12229
0
{
12230
  /* No RIP address.  */
12231
0
  if (i.base_reg && i.base_reg->reg_num == RegIP)
12232
0
    return 0;
12233
12234
  /* No opcodes outside of base encoding space.  */
12235
0
  if (i.tm.opcode_space != SPACE_BASE)
12236
0
    return 0;
12237
12238
  /* add, sub without add/sub m, imm.  */
12239
0
  if (i.tm.base_opcode <= 5
12240
0
      || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
12241
0
      || ((i.tm.base_opcode | 3) == 0x83
12242
0
    && (i.tm.extension_opcode == 0x5
12243
0
        || i.tm.extension_opcode == 0x0)))
12244
0
    {
12245
0
      *mf_cmp_p = mf_cmp_alu_cmp;
12246
0
      return !(i.mem_operands && i.imm_operands);
12247
0
    }
12248
12249
  /* and without and m, imm.  */
12250
0
  if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
12251
0
      || ((i.tm.base_opcode | 3) == 0x83
12252
0
    && i.tm.extension_opcode == 0x4))
12253
0
    {
12254
0
      *mf_cmp_p = mf_cmp_test_and;
12255
0
      return !(i.mem_operands && i.imm_operands);
12256
0
    }
12257
12258
  /* test without test m imm.  */
12259
0
  if ((i.tm.base_opcode | 1) == 0x85
12260
0
      || (i.tm.base_opcode | 1) == 0xa9
12261
0
      || ((i.tm.base_opcode | 1) == 0xf7
12262
0
    && i.tm.extension_opcode == 0))
12263
0
    {
12264
0
      *mf_cmp_p = mf_cmp_test_and;
12265
0
      return !(i.mem_operands && i.imm_operands);
12266
0
    }
12267
12268
  /* cmp without cmp m, imm.  */
12269
0
  if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
12270
0
      || ((i.tm.base_opcode | 3) == 0x83
12271
0
    && (i.tm.extension_opcode == 0x7)))
12272
0
    {
12273
0
      *mf_cmp_p = mf_cmp_alu_cmp;
12274
0
      return !(i.mem_operands && i.imm_operands);
12275
0
    }
12276
12277
  /* inc, dec without inc/dec m.   */
12278
0
  if ((is_cpu (&i.tm, CpuNo64)
12279
0
       && (i.tm.base_opcode | 0xf) == 0x4f)
12280
0
      || ((i.tm.base_opcode | 1) == 0xff
12281
0
    && i.tm.extension_opcode <= 0x1))
12282
0
    {
12283
0
      *mf_cmp_p = mf_cmp_incdec;
12284
0
      return !i.mem_operands;
12285
0
    }
12286
12287
0
  return 0;
12288
0
}
12289
12290
/* Return 1 if a FUSED_JCC_PADDING frag should be generated.  */
12291
12292
static int
12293
add_fused_jcc_padding_frag_p (enum mf_cmp_kind *mf_cmp_p,
12294
            const struct last_insn *last_insn)
12295
2.54k
{
12296
  /* NB: Don't work with COND_JUMP86 without i386.  */
12297
2.54k
  if (!align_branch_power
12298
0
      || now_seg == absolute_section
12299
0
      || !cpu_arch_flags.bitfield.cpui386
12300
0
      || !(align_branch & align_branch_fused_bit))
12301
2.54k
    return 0;
12302
12303
0
  if (maybe_fused_with_jcc_p (mf_cmp_p))
12304
0
    {
12305
0
      if (last_insn->kind == last_insn_other)
12306
0
  return 1;
12307
0
      if (flag_debug)
12308
0
  as_warn_where (last_insn->file, last_insn->line,
12309
0
           _("`%s` skips -malign-branch-boundary on `%s`"),
12310
0
           last_insn->name, insn_name (&i.tm));
12311
0
    }
12312
12313
0
  return 0;
12314
0
}
12315
12316
/* Return 1 if a BRANCH_PREFIX frag should be generated.  */
12317
12318
static int
12319
add_branch_prefix_frag_p (const struct last_insn *last_insn)
12320
2.54k
{
12321
  /* NB: Don't work with COND_JUMP86 without i386.  Don't add prefix
12322
     to PadLock instructions since they include prefixes in opcode.  */
12323
2.54k
  if (!align_branch_power
12324
0
      || !align_branch_prefix_size
12325
0
      || now_seg == absolute_section
12326
0
      || is_padlock (&i.tm)
12327
0
      || !cpu_arch_flags.bitfield.cpui386)
12328
2.54k
    return 0;
12329
12330
  /* Don't add prefix if it is a prefix or there is no operand in case
12331
     that segment prefix is special.  */
12332
0
  if (!i.operands || i.tm.opcode_modifier.isprefix)
12333
0
    return 0;
12334
12335
0
  if (last_insn->kind == last_insn_other)
12336
0
    return 1;
12337
12338
0
  if (flag_debug)
12339
0
    as_warn_where (last_insn->file, last_insn->line,
12340
0
       _("`%s` skips -malign-branch-boundary on `%s`"),
12341
0
       last_insn->name, insn_name (&i.tm));
12342
12343
0
  return 0;
12344
0
}
12345
12346
/* Return 1 if a BRANCH_PADDING frag should be generated.  */
12347
12348
static int
12349
add_branch_padding_frag_p (enum align_branch_kind *branch_p,
12350
         enum mf_jcc_kind *mf_jcc_p,
12351
         const struct last_insn *last_insn)
12352
2.78k
{
12353
2.78k
  int add_padding;
12354
12355
  /* NB: Don't work with COND_JUMP86 without i386.  */
12356
2.78k
  if (!align_branch_power
12357
0
      || now_seg == absolute_section
12358
0
      || !cpu_arch_flags.bitfield.cpui386
12359
0
      || i.tm.opcode_space != SPACE_BASE)
12360
2.78k
    return 0;
12361
12362
0
  add_padding = 0;
12363
12364
  /* Check for jcc and direct jmp.  */
12365
0
  if (i.tm.opcode_modifier.jump == JUMP)
12366
0
    {
12367
0
      if (i.tm.base_opcode == JUMP_PC_RELATIVE)
12368
0
  {
12369
0
    *branch_p = align_branch_jmp;
12370
0
    add_padding = align_branch & align_branch_jmp_bit;
12371
0
  }
12372
0
      else
12373
0
  {
12374
    /* Because J<cc> and JN<cc> share same group in macro-fusible table,
12375
       igore the lowest bit.  */
12376
0
    *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
12377
0
    *branch_p = align_branch_jcc;
12378
0
    if ((align_branch & align_branch_jcc_bit))
12379
0
      add_padding = 1;
12380
0
  }
12381
0
    }
12382
0
  else if ((i.tm.base_opcode | 1) == 0xc3)
12383
0
    {
12384
      /* Near ret.  */
12385
0
      *branch_p = align_branch_ret;
12386
0
      if ((align_branch & align_branch_ret_bit))
12387
0
  add_padding = 1;
12388
0
    }
12389
0
  else
12390
0
    {
12391
      /* Check for indirect jmp, direct and indirect calls.  */
12392
0
      if (i.tm.base_opcode == 0xe8)
12393
0
  {
12394
    /* Direct call.  */
12395
0
    *branch_p = align_branch_call;
12396
0
    if ((align_branch & align_branch_call_bit))
12397
0
      add_padding = 1;
12398
0
  }
12399
0
      else if (i.tm.base_opcode == 0xff
12400
0
         && (i.tm.extension_opcode == 2
12401
0
       || i.tm.extension_opcode == 4))
12402
0
  {
12403
    /* Indirect call and jmp.  */
12404
0
    *branch_p = align_branch_indirect;
12405
0
    if ((align_branch & align_branch_indirect_bit))
12406
0
      add_padding = 1;
12407
0
  }
12408
12409
0
      if (add_padding
12410
0
    && i.disp_operands
12411
0
    && tls_get_addr
12412
0
    && (i.op[0].disps->X_op == O_symbol
12413
0
        || (i.op[0].disps->X_op == O_subtract
12414
0
      && i.op[0].disps->X_op_symbol == GOT_symbol)))
12415
0
  {
12416
0
    symbolS *s = i.op[0].disps->X_add_symbol;
12417
    /* No padding to call to global or undefined tls_get_addr.  */
12418
0
    if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
12419
0
        && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
12420
0
      return 0;
12421
0
  }
12422
0
    }
12423
12424
0
  if (add_padding
12425
0
      && last_insn->kind != last_insn_other)
12426
0
    {
12427
0
      if (flag_debug)
12428
0
  as_warn_where (last_insn->file, last_insn->line,
12429
0
           _("`%s` skips -malign-branch-boundary on `%s`"),
12430
0
           last_insn->name, insn_name (&i.tm));
12431
0
      return 0;
12432
0
    }
12433
12434
0
  return add_padding;
12435
0
}
12436
12437
static void
12438
output_insn (const struct last_insn *last_insn)
12439
2.78k
{
12440
2.78k
  fragS *insn_start_frag;
12441
2.78k
  offsetT insn_start_off;
12442
2.78k
  fragS *fragP = NULL;
12443
2.78k
  enum align_branch_kind branch = align_branch_none;
12444
  /* The initializer is arbitrary just to avoid uninitialized error.
12445
     it's actually either assigned in add_branch_padding_frag_p
12446
     or never be used.  */
12447
2.78k
  enum mf_jcc_kind mf_jcc = mf_jcc_jo;
12448
12449
2.78k
#ifdef OBJ_ELF
12450
2.78k
  if (x86_used_note && now_seg != absolute_section)
12451
2.44k
    {
12452
2.44k
      unsigned int feature_2_used = 0;
12453
12454
2.44k
      if ((i.xstate & xstate_tmm) == xstate_tmm
12455
2.44k
    || is_cpu (&i.tm, CpuAMX_TILE))
12456
1
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
12457
12458
2.44k
      if (is_cpu (&i.tm, Cpu8087)
12459
2.44k
    || is_cpu (&i.tm, Cpu287)
12460
2.44k
    || is_cpu (&i.tm, Cpu387)
12461
2.44k
    || is_cpu (&i.tm, Cpu687)
12462
2.41k
    || is_cpu (&i.tm, CpuFISTTP))
12463
35
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
12464
12465
2.44k
      if ((i.xstate & xstate_mmx)
12466
2.44k
    || i.tm.mnem_off == MN_emms
12467
2.41k
    || i.tm.mnem_off == MN_femms)
12468
34
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
12469
12470
2.44k
      if (i.index_reg)
12471
0
  {
12472
0
    if (i.index_reg->reg_type.bitfield.zmmword)
12473
0
      i.xstate |= xstate_zmm;
12474
0
    else if (i.index_reg->reg_type.bitfield.ymmword)
12475
0
      i.xstate |= xstate_ymm;
12476
0
    else if (i.index_reg->reg_type.bitfield.xmmword)
12477
0
      i.xstate |= xstate_xmm;
12478
0
  }
12479
12480
      /* vzeroall / vzeroupper */
12481
2.44k
      if (i.tm.base_opcode == 0x77 && is_cpu (&i.tm, CpuAVX))
12482
1
  i.xstate |= xstate_ymm;
12483
12484
2.44k
      if ((i.xstate & xstate_xmm)
12485
    /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
12486
2.44k
    || (i.tm.base_opcode == 0xae
12487
50
        && (is_cpu (&i.tm, CpuSSE)
12488
50
      || is_cpu (&i.tm, CpuAVX)))
12489
2.44k
    || is_cpu (&i.tm, CpuWideKL)
12490
2.44k
    || is_cpu (&i.tm, CpuKL))
12491
1
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
12492
12493
2.44k
      if ((i.xstate & xstate_ymm) == xstate_ymm)
12494
1
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
12495
2.44k
      if ((i.xstate & xstate_zmm) == xstate_zmm)
12496
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
12497
2.44k
      if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
12498
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
12499
2.44k
      if (is_cpu (&i.tm, CpuFXSR))
12500
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
12501
2.44k
      if (is_cpu (&i.tm, CpuXsave))
12502
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
12503
2.44k
      if (is_cpu (&i.tm, CpuXsaveopt))
12504
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
12505
2.44k
      if (is_cpu (&i.tm, CpuXSAVEC))
12506
0
  feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
12507
12508
2.44k
      x86_feature_2_used |= feature_2_used;
12509
12510
2.44k
      if (object_64bit
12511
0
    || (feature_2_used
12512
0
        & (GNU_PROPERTY_X86_FEATURE_2_XMM
12513
0
     | GNU_PROPERTY_X86_FEATURE_2_FXSR)) != 0
12514
0
    || is_cpu (&i.tm, CpuCMOV)
12515
0
    || is_cpu (&i.tm, CpuSYSCALL)
12516
0
    || i.tm.mnem_off == MN_cmpxchg8b)
12517
2.44k
  x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
12518
2.44k
      if (is_cpu (&i.tm, CpuSSE3)
12519
2.44k
    || is_cpu (&i.tm, CpuSSSE3)
12520
2.44k
    || is_cpu (&i.tm, CpuSSE4_1)
12521
2.44k
    || is_cpu (&i.tm, CpuSSE4_2)
12522
2.44k
    || is_cpu (&i.tm, CpuCX16)
12523
2.44k
    || is_cpu (&i.tm, CpuPOPCNT)
12524
    /* LAHF-SAHF insns in 64-bit mode.  */
12525
2.44k
    || (flag_code == CODE_64BIT
12526
1.99k
        && (i.tm.base_opcode | 1) == 0x9f
12527
3
        && i.tm.opcode_space == SPACE_BASE))
12528
1
  x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
12529
2.44k
      if (is_cpu (&i.tm, CpuAVX)
12530
2.44k
    || is_cpu (&i.tm, CpuAVX2)
12531
    /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
12532
       XOP, FMA4, LPW, TBM, and AMX.  */
12533
2.44k
    || (i.tm.opcode_modifier.vex
12534
541
        && !is_cpu (&i.tm, CpuAVX512F)
12535
541
        && !is_cpu (&i.tm, CpuAVX512BW)
12536
541
        && !is_cpu (&i.tm, CpuAVX512DQ)
12537
541
        && !is_cpu (&i.tm, CpuXOP)
12538
541
        && !is_cpu (&i.tm, CpuFMA4)
12539
541
        && !is_cpu (&i.tm, CpuLWP)
12540
541
        && !is_cpu (&i.tm, CpuTBM)
12541
541
        && !(feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
12542
1.90k
    || is_cpu (&i.tm, CpuLZCNT)
12543
1.90k
    || is_cpu (&i.tm, CpuMovbe)
12544
1.90k
    || is_cpu (&i.tm, CpuXSAVES)
12545
1.90k
    || (feature_2_used
12546
1.90k
        & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
12547
1.90k
     | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
12548
1.90k
     | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
12549
541
  x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
12550
2.44k
      if (is_cpu (&i.tm, CpuAVX512F)
12551
2.44k
    || is_cpu (&i.tm, CpuAVX512BW)
12552
2.44k
    || is_cpu (&i.tm, CpuAVX512DQ)
12553
2.44k
    || is_cpu (&i.tm, CpuAVX512VL)
12554
    /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
12555
       AVX512-4FMAPS, and AVX512-4VNNIW.  */
12556
2.44k
    || (i.tm.opcode_modifier.evex
12557
6
        && !is_cpu (&i.tm, CpuAVX512ER)
12558
6
        && !is_cpu (&i.tm, CpuAVX512PF)
12559
6
        && !is_cpu (&i.tm, CpuAVX512_4FMAPS)
12560
6
        && !is_cpu (&i.tm, CpuAVX512_4VNNIW)))
12561
6
  x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
12562
2.44k
    }
12563
2.78k
#endif
12564
12565
  /* Tie dwarf2 debug info to the address at the start of the insn.
12566
     We can't do this after the insn has been output as the current
12567
     frag may have been closed off.  eg. by frag_var.  */
12568
2.78k
  dwarf2_emit_insn (0);
12569
12570
2.78k
  insn_start_frag = frag_now;
12571
2.78k
  insn_start_off = frag_now_fix ();
12572
12573
2.78k
  if (add_branch_padding_frag_p (&branch, &mf_jcc, last_insn))
12574
0
    {
12575
0
      char *p;
12576
      /* Branch can be 8 bytes.  Leave some room for prefixes.  */
12577
0
      unsigned int max_branch_padding_size = 14;
12578
12579
      /* Align section to boundary.  */
12580
0
      record_alignment (now_seg, align_branch_power);
12581
12582
      /* Make room for padding.  */
12583
0
      frag_grow (max_branch_padding_size);
12584
12585
      /* Start of the padding.  */
12586
0
      p = frag_more (0);
12587
12588
0
      fragP = frag_now;
12589
12590
0
      frag_var (rs_machine_dependent, max_branch_padding_size, 0,
12591
0
    ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
12592
0
    NULL, 0, p);
12593
12594
0
      fragP->tc_frag_data.mf_type = mf_jcc;
12595
0
      fragP->tc_frag_data.branch_type = branch;
12596
0
      fragP->tc_frag_data.max_bytes = max_branch_padding_size;
12597
0
    }
12598
12599
2.78k
  if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
12600
0
      && !pre_386_16bit_warned)
12601
0
    {
12602
0
      as_warn (_("use .code16 to ensure correct addressing mode"));
12603
0
      pre_386_16bit_warned = true;
12604
0
    }
12605
12606
  /* Output jumps.  */
12607
2.78k
  if (i.tm.opcode_modifier.jump == JUMP)
12608
107
    output_branch ();
12609
2.68k
  else if (i.tm.opcode_modifier.jump == JUMP_BYTE
12610
2.66k
     || i.tm.opcode_modifier.jump == JUMP_DWORD)
12611
125
    output_jump ();
12612
2.55k
  else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
12613
11
    output_interseg_jump ();
12614
2.54k
  else
12615
2.54k
    {
12616
      /* Output normal instructions here.  */
12617
2.54k
      char *p;
12618
2.54k
      unsigned char *q;
12619
2.54k
      unsigned int j;
12620
2.54k
      enum mf_cmp_kind mf_cmp;
12621
12622
2.54k
      if (avoid_fence
12623
0
    && (i.tm.base_opcode == 0xaee8
12624
0
        || i.tm.base_opcode == 0xaef0
12625
0
        || i.tm.base_opcode == 0xaef8))
12626
0
  {
12627
    /* Encode lfence, mfence, and sfence as
12628
       f0 83 04 24 00   lock addl $0x0, (%{re}sp).  */
12629
0
    if (flag_code == CODE_16BIT)
12630
0
      as_bad (_("Cannot convert `%s' in 16-bit mode"), insn_name (&i.tm));
12631
0
    else if (omit_lock_prefix)
12632
0
      as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
12633
0
        insn_name (&i.tm));
12634
0
    else if (now_seg != absolute_section)
12635
0
      {
12636
0
        offsetT val = 0x240483f0ULL;
12637
12638
0
        p = frag_more (5);
12639
0
        md_number_to_chars (p, val, 5);
12640
0
      }
12641
0
    else
12642
0
      abs_section_offset += 5;
12643
0
    return;
12644
0
  }
12645
12646
      /* Some processors fail on LOCK prefix. This options makes
12647
   assembler ignore LOCK prefix and serves as a workaround.  */
12648
2.54k
      if (omit_lock_prefix)
12649
0
  {
12650
0
    if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
12651
0
        && i.tm.opcode_modifier.isprefix)
12652
0
      return;
12653
0
    i.prefix[LOCK_PREFIX] = 0;
12654
0
  }
12655
12656
2.54k
      if (branch)
12657
  /* Skip if this is a branch.  */
12658
0
  ;
12659
2.54k
      else if (add_fused_jcc_padding_frag_p (&mf_cmp, last_insn))
12660
0
  {
12661
    /* Make room for padding.  */
12662
0
    frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
12663
0
    p = frag_more (0);
12664
12665
0
    fragP = frag_now;
12666
12667
0
    frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
12668
0
        ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
12669
0
        NULL, 0, p);
12670
12671
0
    fragP->tc_frag_data.mf_type = mf_cmp;
12672
0
    fragP->tc_frag_data.branch_type = align_branch_fused;
12673
0
    fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
12674
0
  }
12675
2.54k
      else if (add_branch_prefix_frag_p (last_insn))
12676
0
  {
12677
0
    unsigned int max_prefix_size = align_branch_prefix_size;
12678
12679
    /* Make room for padding.  */
12680
0
    frag_grow (max_prefix_size);
12681
0
    p = frag_more (0);
12682
12683
0
    fragP = frag_now;
12684
12685
0
    frag_var (rs_machine_dependent, max_prefix_size, 0,
12686
0
        ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
12687
0
        NULL, 0, p);
12688
12689
0
    fragP->tc_frag_data.max_bytes = max_prefix_size;
12690
0
  }
12691
12692
      /* Since the VEX/EVEX prefix contains the implicit prefix, we
12693
   don't need the explicit prefix.  */
12694
2.54k
      if (!is_any_vex_encoding (&i.tm))
12695
1.87k
  {
12696
1.87k
    switch (i.tm.opcode_modifier.opcodeprefix)
12697
1.87k
      {
12698
0
      case PREFIX_0X66:
12699
0
        add_prefix (0x66);
12700
0
        break;
12701
0
      case PREFIX_0XF2:
12702
0
        add_prefix (0xf2);
12703
0
        break;
12704
0
      case PREFIX_0XF3:
12705
0
        if (!is_padlock (&i.tm)
12706
0
      || (i.prefix[REP_PREFIX] != 0xf3))
12707
0
    add_prefix (0xf3);
12708
0
        break;
12709
1.87k
      case PREFIX_NONE:
12710
1.87k
        switch (i.opcode_length)
12711
1.87k
    {
12712
41
    case 2:
12713
41
      break;
12714
1.82k
    case 1:
12715
      /* Check for pseudo prefixes.  */
12716
1.82k
      if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
12717
1.82k
        break;
12718
0
      as_bad_where (insn_start_frag->fr_file,
12719
0
        insn_start_frag->fr_line,
12720
0
        _("pseudo prefix without instruction"));
12721
0
      return;
12722
0
    default:
12723
0
      abort ();
12724
1.87k
    }
12725
1.87k
        break;
12726
1.87k
      default:
12727
0
        abort ();
12728
1.87k
      }
12729
12730
1.87k
#ifdef OBJ_ELF
12731
    /* For x32, add a dummy REX_OPCODE prefix for mov/add with
12732
       R_X86_64_GOTTPOFF relocation so that linker can safely
12733
       perform IE->LE optimization.  A dummy REX_OPCODE prefix
12734
       is also needed for lea with R_X86_64_GOTPC32_TLSDESC
12735
       relocation for GDesc -> IE/LE optimization.  */
12736
1.87k
    if (x86_elf_abi == X86_64_X32_ABI
12737
0
        && !is_apx_rex2_encoding ()
12738
0
        && (dot_insn () ? i.insn_opcode_space
12739
0
            : i.tm.opcode_space) == SPACE_BASE
12740
0
        && i.operands == 2
12741
0
        && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
12742
0
      || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
12743
0
        && i.prefix[REX_PREFIX] == 0)
12744
0
      add_prefix (REX_OPCODE);
12745
1.87k
#endif
12746
12747
    /* The prefix bytes.  */
12748
14.9k
    for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
12749
13.0k
      if (*q)
12750
239
        frag_opcode_byte (*q);
12751
12752
1.87k
    if (is_apx_rex2_encoding ())
12753
7
      {
12754
7
        frag_opcode_byte (i.vex.bytes[0]);
12755
7
        frag_opcode_byte (i.vex.bytes[1]);
12756
7
      }
12757
1.87k
  }
12758
674
      else
12759
674
  {
12760
5.39k
    for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
12761
4.71k
      if (*q)
12762
0
        switch (j)
12763
0
    {
12764
0
    case SEG_PREFIX:
12765
0
    case ADDR_PREFIX:
12766
0
      frag_opcode_byte (*q);
12767
0
      break;
12768
0
    default:
12769
      /* There should be no other prefixes for instructions
12770
         with VEX prefix.  */
12771
0
      abort ();
12772
0
    }
12773
12774
    /* For EVEX instructions i.vrex should become 0 after
12775
       build_evex_prefix.  For VEX instructions upper 16 registers
12776
       aren't available, so VREX should be 0.  */
12777
674
    if (i.vrex)
12778
0
      abort ();
12779
    /* Now the VEX prefix.  */
12780
674
    if (now_seg != absolute_section)
12781
548
      {
12782
548
        p = frag_more (i.vex.length);
12783
2.19k
        for (j = 0; j < i.vex.length; j++)
12784
1.64k
    p[j] = i.vex.bytes[j];
12785
548
      }
12786
126
    else
12787
126
      abs_section_offset += i.vex.length;
12788
674
  }
12789
12790
      /* Now the opcode; be careful about word order here!  */
12791
2.54k
      j = i.opcode_length;
12792
2.54k
      if (!i.vex.length)
12793
1.86k
  switch (i.tm.opcode_space)
12794
1.86k
    {
12795
1.78k
    case SPACE_BASE:
12796
1.78k
      break;
12797
75
    case SPACE_0F:
12798
75
      ++j;
12799
75
      break;
12800
0
    case SPACE_0F38:
12801
0
    case SPACE_0F3A:
12802
0
      j += 2;
12803
0
      break;
12804
0
    default:
12805
0
      abort ();
12806
1.86k
    }
12807
12808
2.54k
      if (now_seg == absolute_section)
12809
276
  abs_section_offset += j;
12810
2.26k
      else if (j == 1)
12811
2.14k
  {
12812
2.14k
    FRAG_APPEND_1_CHAR (i.tm.base_opcode);
12813
2.14k
  }
12814
123
      else
12815
123
  {
12816
123
    p = frag_more (j);
12817
123
    if (!i.vex.length
12818
112
        && i.tm.opcode_space != SPACE_BASE)
12819
75
      {
12820
75
        *p++ = 0x0f;
12821
75
        if (i.tm.opcode_space != SPACE_0F)
12822
0
    *p++ = i.tm.opcode_space == SPACE_0F38
12823
0
           ? 0x38 : 0x3a;
12824
75
      }
12825
12826
123
    switch (i.opcode_length)
12827
123
      {
12828
48
      case 2:
12829
        /* Put out high byte first: can't use md_number_to_chars!  */
12830
48
        *p++ = (i.tm.base_opcode >> 8) & 0xff;
12831
        /* Fall through.  */
12832
123
      case 1:
12833
123
        *p = i.tm.base_opcode & 0xff;
12834
123
        break;
12835
0
      default:
12836
0
        abort ();
12837
0
        break;
12838
123
      }
12839
12840
123
  }
12841
12842
      /* Now the modrm byte and sib byte (if present).  */
12843
2.54k
      if (i.tm.opcode_modifier.modrm)
12844
672
  {
12845
672
    frag_opcode_byte ((i.rm.regmem << 0)
12846
672
           | (i.rm.reg << 3)
12847
672
           | (i.rm.mode << 6));
12848
    /* If i.rm.regmem == ESP (4)
12849
       && i.rm.mode != (Register mode)
12850
       && not 16 bit
12851
       ==> need second modrm byte.  */
12852
672
    if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
12853
282
        && i.rm.mode != 3
12854
280
        && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
12855
280
      frag_opcode_byte ((i.sib.base << 0)
12856
280
            | (i.sib.index << 3)
12857
280
            | (i.sib.scale << 6));
12858
672
  }
12859
12860
2.54k
      if (i.disp_operands)
12861
598
  output_disp (insn_start_frag, insn_start_off);
12862
12863
2.54k
      if (i.imm_operands)
12864
240
  output_imm (insn_start_frag, insn_start_off);
12865
12866
      /*
12867
       * frag_now_fix () returning plain abs_section_offset when we're in the
12868
       * absolute section, and abs_section_offset not getting updated as data
12869
       * gets added to the frag breaks the logic below.
12870
       */
12871
2.54k
      if (now_seg != absolute_section)
12872
2.26k
  {
12873
2.26k
    j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
12874
2.26k
    if (j > 15)
12875
0
      {
12876
0
        if (dot_insn ())
12877
0
    as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
12878
0
      j);
12879
0
        else
12880
0
    as_bad (_("instruction length of %u bytes exceeds the limit of 15"),
12881
0
      j);
12882
0
      }
12883
2.26k
    else if (fragP)
12884
0
      {
12885
        /* NB: Don't add prefix with GOTPC relocation since
12886
     output_disp() above depends on the fixed encoding
12887
     length.  Can't add prefix with TLS relocation since
12888
     it breaks TLS linker optimization.  */
12889
0
        unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
12890
        /* Prefix count on the current instruction.  */
12891
0
        unsigned int count = i.vex.length;
12892
0
        unsigned int k;
12893
0
        for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
12894
    /* REX byte is encoded in VEX/EVEX prefix.  */
12895
0
    if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
12896
0
      count++;
12897
12898
        /* Count prefixes for extended opcode maps.  */
12899
0
        if (!i.vex.length)
12900
0
    switch (i.tm.opcode_space)
12901
0
      {
12902
0
      case SPACE_BASE:
12903
0
        break;
12904
0
      case SPACE_0F:
12905
0
        count++;
12906
0
        break;
12907
0
      case SPACE_0F38:
12908
0
      case SPACE_0F3A:
12909
0
        count += 2;
12910
0
        break;
12911
0
      default:
12912
0
        abort ();
12913
0
      }
12914
12915
0
        if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12916
0
      == BRANCH_PREFIX)
12917
0
    {
12918
      /* Set the maximum prefix size in BRANCH_PREFIX
12919
         frag.  */
12920
0
      if (fragP->tc_frag_data.max_bytes > max)
12921
0
        fragP->tc_frag_data.max_bytes = max;
12922
0
      if (fragP->tc_frag_data.max_bytes > count)
12923
0
        fragP->tc_frag_data.max_bytes -= count;
12924
0
      else
12925
0
        fragP->tc_frag_data.max_bytes = 0;
12926
0
    }
12927
0
        else
12928
0
    {
12929
      /* Remember the maximum prefix size in FUSED_JCC_PADDING
12930
         frag.  */
12931
0
      unsigned int max_prefix_size;
12932
0
      if (align_branch_prefix_size > max)
12933
0
        max_prefix_size = max;
12934
0
      else
12935
0
        max_prefix_size = align_branch_prefix_size;
12936
0
      if (max_prefix_size > count)
12937
0
        fragP->tc_frag_data.max_prefix_length
12938
0
          = max_prefix_size - count;
12939
0
    }
12940
12941
        /* Use existing segment prefix if possible.  Use CS
12942
     segment prefix in 64-bit mode.  In 32-bit mode, use SS
12943
     segment prefix with ESP/EBP base register and use DS
12944
     segment prefix without ESP/EBP base register.  */
12945
0
        if (i.prefix[SEG_PREFIX])
12946
0
    fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
12947
0
        else if (flag_code == CODE_64BIT)
12948
0
    fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
12949
0
        else if (i.base_reg
12950
0
           && (i.base_reg->reg_num == 4
12951
0
         || i.base_reg->reg_num == 5))
12952
0
    fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
12953
0
        else
12954
0
    fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
12955
0
      }
12956
2.26k
  }
12957
2.54k
    }
12958
12959
  /* NB: Don't work with COND_JUMP86 without i386.  */
12960
2.78k
  if (align_branch_power
12961
0
      && now_seg != absolute_section
12962
0
      && cpu_arch_flags.bitfield.cpui386)
12963
0
    {
12964
      /* Terminate each frag so that we can add prefix and check for
12965
         fused jcc.  */
12966
0
      frag_wane (frag_now);
12967
0
      frag_new (0);
12968
0
    }
12969
12970
#ifdef DEBUG386
12971
  if (flag_debug)
12972
    {
12973
      pi ("" /*line*/, &i);
12974
    }
12975
#endif /* DEBUG386  */
12976
2.78k
}
12977
12978
/* Return the size of the displacement operand N.  */
12979
12980
static int
12981
disp_size (unsigned int n)
12982
598
{
12983
598
  int size = 4;
12984
12985
598
  if (i.types[n].bitfield.disp64)
12986
0
    size = 8;
12987
598
  else if (i.types[n].bitfield.disp8)
12988
0
    size = 1;
12989
598
  else if (i.types[n].bitfield.disp16)
12990
317
    size = 2;
12991
598
  return size;
12992
598
}
12993
12994
/* Return the size of the immediate operand N.  */
12995
12996
static int
12997
imm_size (unsigned int n)
12998
240
{
12999
240
  int size = 4;
13000
240
  if (i.types[n].bitfield.imm64)
13001
0
    size = 8;
13002
240
  else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
13003
72
    size = 1;
13004
168
  else if (i.types[n].bitfield.imm16)
13005
145
    size = 2;
13006
240
  return size;
13007
240
}
13008
13009
static void
13010
output_disp (fragS *insn_start_frag, offsetT insn_start_off)
13011
598
{
13012
598
  char *p;
13013
598
  unsigned int n;
13014
13015
1.52k
  for (n = 0; n < i.operands; n++)
13016
922
    {
13017
922
      if (operand_type_check (i.types[n], disp))
13018
598
  {
13019
598
    int size = disp_size (n);
13020
13021
598
    if (now_seg == absolute_section)
13022
199
      abs_section_offset += size;
13023
399
    else if (i.op[n].disps->X_op == O_constant)
13024
4
      {
13025
4
        offsetT val = i.op[n].disps->X_add_number;
13026
13027
4
        val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
13028
4
             size);
13029
4
        p = frag_more (size);
13030
4
        md_number_to_chars (p, val, size);
13031
4
      }
13032
395
    else
13033
395
      {
13034
395
        enum bfd_reloc_code_real reloc_type;
13035
395
        bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
13036
395
        bool sign = (flag_code == CODE_64BIT && size == 4
13037
264
         && (!want_disp32 (&i.tm)
13038
0
             || (i.tm.opcode_modifier.jump && !i.jumpabsolute
13039
0
           && !i.types[n].bitfield.baseindex)))
13040
131
        || pcrel;
13041
395
        fixS *fixP;
13042
13043
        /* We can't have 8 bit displacement here.  */
13044
395
        gas_assert (!i.types[n].bitfield.disp8);
13045
13046
        /* The PC relative address is computed relative
13047
     to the instruction boundary, so in case immediate
13048
     fields follows, we need to adjust the value.  */
13049
395
        if (pcrel && i.imm_operands)
13050
0
    {
13051
0
      unsigned int n1;
13052
0
      int sz = 0;
13053
13054
0
      for (n1 = 0; n1 < i.operands; n1++)
13055
0
        if (operand_type_check (i.types[n1], imm))
13056
0
          {
13057
      /* Only one immediate is allowed for PC
13058
         relative address, except with .insn.  */
13059
0
      gas_assert (sz == 0 || dot_insn ());
13060
0
      sz += imm_size (n1);
13061
0
          }
13062
      /* We should find at least one immediate.  */
13063
0
      gas_assert (sz != 0);
13064
0
      i.op[n].disps->X_add_number -= sz;
13065
0
    }
13066
13067
395
        p = frag_more (size);
13068
395
        reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
13069
395
        if (GOT_symbol
13070
35
      && GOT_symbol == i.op[n].disps->X_add_symbol
13071
0
      && (((reloc_type == BFD_RELOC_32
13072
0
      || reloc_type == BFD_RELOC_X86_64_32S
13073
0
      || (reloc_type == BFD_RELOC_64
13074
0
          && object_64bit))
13075
0
           && (i.op[n].disps->X_op == O_symbol
13076
0
         || (i.op[n].disps->X_op == O_add
13077
0
             && ((symbol_get_value_expression
13078
0
            (i.op[n].disps->X_op_symbol)->X_op)
13079
0
           == O_subtract))))
13080
0
          || reloc_type == BFD_RELOC_32_PCREL))
13081
0
    {
13082
0
      if (!object_64bit)
13083
0
        {
13084
0
          reloc_type = BFD_RELOC_386_GOTPC;
13085
0
          i.has_gotpc_tls_reloc = true;
13086
0
          i.op[n].disps->X_add_number +=
13087
0
      encoding_length (insn_start_frag, insn_start_off, p);
13088
0
        }
13089
0
      else if (reloc_type == BFD_RELOC_64)
13090
0
        reloc_type = BFD_RELOC_X86_64_GOTPC64;
13091
0
      else
13092
        /* Don't do the adjustment for x86-64, as there
13093
           the pcrel addressing is relative to the _next_
13094
           insn, and that is taken care of in other code.  */
13095
0
        reloc_type = BFD_RELOC_X86_64_GOTPC32;
13096
0
    }
13097
395
        else if (align_branch_power)
13098
0
    {
13099
0
      switch (reloc_type)
13100
0
        {
13101
0
        case BFD_RELOC_386_TLS_GD:
13102
0
        case BFD_RELOC_386_TLS_LDM:
13103
0
        case BFD_RELOC_386_TLS_IE:
13104
0
        case BFD_RELOC_386_TLS_IE_32:
13105
0
        case BFD_RELOC_386_TLS_GOTIE:
13106
0
        case BFD_RELOC_386_TLS_GOTDESC:
13107
0
        case BFD_RELOC_386_TLS_DESC_CALL:
13108
0
        case BFD_RELOC_X86_64_TLSGD:
13109
0
        case BFD_RELOC_X86_64_TLSLD:
13110
0
        case BFD_RELOC_X86_64_GOTTPOFF:
13111
0
        case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
13112
0
        case BFD_RELOC_X86_64_CODE_5_GOTTPOFF:
13113
0
        case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
13114
0
        case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
13115
0
        case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
13116
0
        case BFD_RELOC_X86_64_CODE_5_GOTPC32_TLSDESC:
13117
0
        case BFD_RELOC_X86_64_CODE_6_GOTPC32_TLSDESC:
13118
0
        case BFD_RELOC_X86_64_TLSDESC_CALL:
13119
0
          i.has_gotpc_tls_reloc = true;
13120
0
        default:
13121
0
          break;
13122
0
        }
13123
0
    }
13124
395
        fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
13125
395
          size, i.op[n].disps, pcrel,
13126
395
          reloc_type);
13127
13128
395
        if (flag_code == CODE_64BIT && size == 4 && pcrel
13129
0
      && !i.prefix[ADDR_PREFIX])
13130
0
    fixP->fx_signed = 1;
13131
13132
395
        if (i.base_reg && i.base_reg->reg_num == RegIP)
13133
0
    {
13134
0
      if (reloc_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
13135
0
        {
13136
          /* Set fx_tcbit for REX2 prefix.  */
13137
0
          if (is_apx_rex2_encoding ())
13138
0
      fixP->fx_tcbit = 1;
13139
0
          continue;
13140
0
        }
13141
0
    }
13142
        /* In 64-bit, i386_validate_fix updates only (%rip)
13143
     relocations.  */
13144
395
        else if (object_64bit)
13145
395
    continue;
13146
13147
0
#ifdef OBJ_ELF
13148
        /* Check for "call/jmp *mem", "push mem", "mov mem, %reg",
13149
     "movrs mem, %reg", "test %reg, mem" and "binop mem, %reg" where
13150
     binop is one of adc, add, and, cmp, or, sbb, sub, xor, or imul
13151
     instructions without data prefix.  Always generate
13152
     R_386_GOT32X for "sym*GOT" operand in 32-bit mode.  */
13153
0
        unsigned int space = dot_insn () ? i.insn_opcode_space
13154
0
                 : i.tm.opcode_space;
13155
0
        if (i.prefix[DATA_PREFIX] == 0
13156
0
      && (i.rm.mode == 2
13157
0
          || (i.rm.mode == 0 && i.rm.regmem == 5))
13158
0
      && ((space == SPACE_BASE
13159
0
           && i.tm.base_opcode == 0xff
13160
0
           && (i.rm.reg == 2 || i.rm.reg == 4 || i.rm.reg == 6))
13161
0
          || ((space == SPACE_BASE
13162
0
         || space == SPACE_0F38
13163
0
         || space == SPACE_MAP4)
13164
0
        && i.tm.base_opcode == 0x8b)
13165
0
          || ((space == SPACE_BASE
13166
0
         || space == SPACE_MAP4)
13167
0
        && (i.tm.base_opcode == 0x85
13168
0
            || (i.tm.base_opcode
13169
0
          | (i.operands > 2 ? 0x3a : 0x38)) == 0x3b))
13170
0
          || (((space == SPACE_0F
13171
          /* Because of the 0F prefix, no suitable relocation
13172
             exists for this unless it's REX2-encoded.  */
13173
0
          && is_apx_rex2_encoding ())
13174
0
         || space == SPACE_MAP4)
13175
0
        && i.tm.base_opcode == 0xaf)))
13176
0
    {
13177
0
      if (object_64bit)
13178
0
        {
13179
0
          if (reloc_type == BFD_RELOC_X86_64_GOTTPOFF)
13180
0
      {
13181
0
        if (space == SPACE_MAP4)
13182
0
          fixP->fx_tcbit3 = 1;
13183
0
        else if (space == SPACE_0F38 && i.rex)
13184
0
          fixP->fx_tcbit2 = 1;
13185
0
        else if (space == SPACE_0F38 || is_apx_rex2_encoding ())
13186
0
          fixP->fx_tcbit = 1;
13187
0
      }
13188
0
          else if (generate_relax_relocations)
13189
0
      {
13190
0
        if (space == SPACE_MAP4)
13191
0
          {
13192
0
            fixP->fx_tcbit3 = 1;
13193
0
            fixP->fx_tcbit2 = 1;
13194
0
          }
13195
0
        else if (space == SPACE_0F38)
13196
0
          {
13197
0
            fixP->fx_tcbit3 = 1;
13198
0
            if (i.rex)
13199
0
        fixP->fx_tcbit = 1;
13200
0
          }
13201
0
        else if (is_apx_rex2_encoding ())
13202
0
          fixP->fx_tcbit3 = 1;
13203
0
        else if (i.rex)
13204
0
          fixP->fx_tcbit2 = 1;
13205
0
        else
13206
0
          fixP->fx_tcbit = 1;
13207
0
      }
13208
0
        }
13209
0
      else if (generate_relax_relocations
13210
0
         ? (!shared || i.rm.mode != 0 || i.rm.regmem != 5)
13211
0
         : (!shared && i.rm.mode == 0 && i.rm.regmem == 5))
13212
0
        fixP->fx_tcbit2 = 1;
13213
0
    }
13214
0
#endif
13215
0
      }
13216
598
  }
13217
922
    }
13218
598
}
13219
13220
static void
13221
output_imm (fragS *insn_start_frag, offsetT insn_start_off)
13222
240
{
13223
240
  char *p;
13224
240
  unsigned int n;
13225
13226
689
  for (n = 0; n < i.operands; n++)
13227
449
    {
13228
449
      if (operand_type_check (i.types[n], imm))
13229
240
  {
13230
240
    int size = imm_size (n);
13231
13232
240
    if (now_seg == absolute_section)
13233
134
      abs_section_offset += size;
13234
106
    else if (i.op[n].imms->X_op == O_constant)
13235
61
      {
13236
61
        offsetT val;
13237
13238
61
        val = offset_in_range (i.op[n].imms->X_add_number,
13239
61
             size);
13240
61
        p = frag_more (size);
13241
61
        md_number_to_chars (p, val, size);
13242
61
      }
13243
45
    else
13244
45
      {
13245
        /* Not absolute_section.
13246
     Need a 32-bit fixup (don't support 8bit
13247
     non-absolute imms).  Try to support other
13248
     sizes ...  */
13249
45
        enum bfd_reloc_code_real reloc_type;
13250
45
        int sign;
13251
13252
45
        if (i.types[n].bitfield.imm32s
13253
0
      && (i.suffix == QWORD_MNEM_SUFFIX
13254
0
          || (!i.suffix && i.tm.opcode_modifier.no_lsuf)
13255
0
          || (i.prefix[REX_PREFIX] & REX_W)
13256
0
          || dot_insn ()))
13257
0
    sign = 1;
13258
45
        else
13259
45
    sign = 0;
13260
13261
45
        p = frag_more (size);
13262
45
        reloc_type = reloc (size, 0, sign, i.reloc[n]);
13263
13264
        /*   This is tough to explain.  We end up with this one if we
13265
         * have operands that look like
13266
         * "_GLOBAL_OFFSET_TABLE_+[.-.L284]".  The goal here is to
13267
         * obtain the absolute address of the GOT, and it is strongly
13268
         * preferable from a performance point of view to avoid using
13269
         * a runtime relocation for this.  The actual sequence of
13270
         * instructions often look something like:
13271
         *
13272
         *  call  .L66
13273
         * .L66:
13274
         *  popl  %ebx
13275
         *  addl  $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
13276
         *
13277
         *   The call and pop essentially return the absolute address
13278
         * of the label .L66 and store it in %ebx.  The linker itself
13279
         * will ultimately change the first operand of the addl so
13280
         * that %ebx points to the GOT, but to keep things simple, the
13281
         * .o file must have this operand set so that it generates not
13282
         * the absolute address of .L66, but the absolute address of
13283
         * itself.  This allows the linker itself simply treat a GOTPC
13284
         * relocation as asking for a pcrel offset to the GOT to be
13285
         * added in, and the addend of the relocation is stored in the
13286
         * operand field for the instruction itself.
13287
         *
13288
         *   Our job here is to fix the operand so that it would add
13289
         * the correct offset so that %ebx would point to itself.  The
13290
         * thing that is tricky is that .-.L66 will point to the
13291
         * beginning of the instruction, so we need to further modify
13292
         * the operand so that it will point to itself.  There are
13293
         * other cases where you have something like:
13294
         *
13295
         *  .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
13296
         *
13297
         * and here no correction would be required.  Internally in
13298
         * the assembler we treat operands of this form as not being
13299
         * pcrel since the '.' is explicitly mentioned, and I wonder
13300
         * whether it would simplify matters to do it this way.  Who
13301
         * knows.  In earlier versions of the PIC patches, the
13302
         * pcrel_adjust field was used to store the correction, but
13303
         * since the expression is not pcrel, I felt it would be
13304
         * confusing to do it this way.  */
13305
13306
45
        if ((reloc_type == BFD_RELOC_32
13307
33
       || reloc_type == BFD_RELOC_X86_64_32S
13308
33
       || reloc_type == BFD_RELOC_64)
13309
12
      && GOT_symbol
13310
0
      && GOT_symbol == i.op[n].imms->X_add_symbol
13311
0
      && (i.op[n].imms->X_op == O_symbol
13312
0
          || (i.op[n].imms->X_op == O_add
13313
0
        && ((symbol_get_value_expression
13314
0
             (i.op[n].imms->X_op_symbol)->X_op)
13315
0
            == O_subtract))))
13316
0
    {
13317
0
      if (!object_64bit)
13318
0
        reloc_type = BFD_RELOC_386_GOTPC;
13319
0
      else if (size == 4)
13320
0
        reloc_type = BFD_RELOC_X86_64_GOTPC32;
13321
0
      else if (size == 8)
13322
0
        reloc_type = BFD_RELOC_X86_64_GOTPC64;
13323
0
      i.has_gotpc_tls_reloc = true;
13324
0
      i.op[n].imms->X_add_number +=
13325
0
        encoding_length (insn_start_frag, insn_start_off, p);
13326
0
    }
13327
45
        fix_new_exp (frag_now, p - frag_now->fr_literal, size,
13328
45
         i.op[n].imms, 0, reloc_type);
13329
45
      }
13330
240
  }
13331
449
    }
13332
240
}
13333

13334
/* x86_cons_fix_new is called via the expression parsing code when a
13335
   reloc is needed.  We use this hook to get the correct .got reloc.  */
13336
static int cons_sign = -1;
13337
13338
void
13339
x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
13340
      expressionS *exp, bfd_reloc_code_real_type r)
13341
1.89k
{
13342
1.89k
  r = reloc (len, 0, cons_sign, r);
13343
13344
#ifdef TE_PE
13345
  if (exp->X_op == O_secrel)
13346
    {
13347
      exp->X_op = O_symbol;
13348
      r = BFD_RELOC_32_SECREL;
13349
    }
13350
  else if (exp->X_op == O_secidx)
13351
    r = BFD_RELOC_16_SECIDX;
13352
#endif
13353
13354
1.89k
  fix_new_exp (frag, off, len, exp, 0, r);
13355
1.89k
}
13356
13357
/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
13358
   purpose of the `.dc.a' internal pseudo-op.  */
13359
13360
int
13361
x86_address_bytes (void)
13362
820
{
13363
820
  if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
13364
0
    return 4;
13365
820
  return stdoutput->arch_info->bits_per_address / 8;
13366
820
}
13367
13368
#if (defined (OBJ_ELF) || defined (OBJ_MACH_O) || defined (TE_PE))
13369
/* Parse operands of the form
13370
   <symbol>@GOTOFF+<nnn>
13371
   and similar .plt or .got references.
13372
13373
   If we find one, set up the correct relocation in RELOC and copy the
13374
   input string, minus the `@GOTOFF' into a malloc'd buffer for
13375
   parsing by the calling routine.  Return this buffer, and if ADJUST
13376
   is non-null set it to the length of the string we removed from the
13377
   input line.  Otherwise return NULL.  */
13378
static char *
13379
lex_got (enum bfd_reloc_code_real *rel,
13380
   int *adjust,
13381
   i386_operand_type *types)
13382
6.63k
{
13383
  /* Some of the relocations depend on the size of what field is to
13384
     be relocated.  But in our callers i386_immediate and i386_displacement
13385
     we don't yet know the operand size (this will be set by insn
13386
     matching).  Hence we record the word32 relocation here,
13387
     and adjust the reloc according to the real size in reloc().  */
13388
6.63k
  char *cp;
13389
6.63k
  unsigned int j;
13390
13391
96.9k
  for (cp = input_line_pointer; *cp != '@'; cp++)
13392
96.2k
    if (is_end_of_stmt (*cp) || *cp == ',')
13393
5.93k
      return NULL;
13394
13395
10.8k
  for (j = 0; j < ARRAY_SIZE (gotrel); j++)
13396
10.7k
    {
13397
10.7k
      int len = gotrel[j].len;
13398
10.7k
      if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
13399
617
  {
13400
617
    if (gotrel[j].rel[object_64bit] != 0)
13401
549
      {
13402
549
        int first, second;
13403
549
        char *tmpbuf, *past_reloc;
13404
13405
549
        i.has_gotrel = true;
13406
549
        *rel = gotrel[j].rel[object_64bit];
13407
13408
549
        if (types)
13409
482
    {
13410
482
      if (flag_code != CODE_64BIT)
13411
35
        {
13412
35
          types->bitfield.imm32 = 1;
13413
35
          types->bitfield.disp32 = 1;
13414
35
        }
13415
447
      else
13416
447
        *types = gotrel[j].types64;
13417
482
    }
13418
13419
549
        if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
13420
15
    GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
13421
13422
        /* The length of the first part of our input line.  */
13423
549
        first = cp - input_line_pointer;
13424
13425
        /* The second part goes from after the reloc token until
13426
     (and including) an end_of_line char or comma.  */
13427
549
        past_reloc = cp + 1 + len;
13428
549
        cp = past_reloc;
13429
13.0k
        while (!is_end_of_stmt (*cp) && *cp != ',')
13430
12.5k
    ++cp;
13431
549
        second = cp + 1 - past_reloc;
13432
13433
        /* Allocate and copy string.  The trailing NUL shouldn't
13434
     be necessary, but be safe.  */
13435
549
        tmpbuf = XNEWVEC (char, first + second + 2);
13436
549
        memcpy (tmpbuf, input_line_pointer, first);
13437
549
        if (second != 0 && !is_whitespace (*past_reloc))
13438
    /* Replace the relocation token with ' ', so that
13439
       errors like foo@GOTOFF1 will be detected.  */
13440
484
    tmpbuf[first++] = ' ';
13441
65
        else
13442
    /* Increment length by 1 if the relocation token is
13443
       removed.  */
13444
65
    len++;
13445
549
        if (adjust)
13446
133
    *adjust = len;
13447
549
        memcpy (tmpbuf + first, past_reloc, second);
13448
549
        tmpbuf[first + second] = '\0';
13449
549
        return tmpbuf;
13450
549
      }
13451
13452
68
    as_bad (_("@%s reloc is not supported with %d-bit output format"),
13453
68
      gotrel[j].str, 1 << (5 + object_64bit));
13454
68
    return NULL;
13455
617
  }
13456
10.7k
    }
13457
13458
  /* Might be a symbol version string.  Don't as_bad here.  */
13459
82
  return NULL;
13460
699
}
13461
#else
13462
# define lex_got(reloc, adjust, types) NULL
13463
#endif
13464
13465
bfd_reloc_code_real_type
13466
x86_cons (expressionS *exp, int size)
13467
1.22k
{
13468
1.22k
  bfd_reloc_code_real_type got_reloc = NO_RELOC;
13469
13470
1.22k
  intel_syntax = -intel_syntax;
13471
1.22k
  exp->X_md = 0;
13472
1.22k
  expr_mode = expr_operator_none;
13473
13474
1.22k
#if defined (OBJ_ELF) || defined (TE_PE)
13475
1.22k
  if (size == 4
13476
# ifdef TE_PE
13477
      || (size == 2)
13478
# endif
13479
584
      || (object_64bit && size == 8))
13480
655
    {
13481
      /* Handle @GOTOFF and the like in an expression.  */
13482
655
      char *save;
13483
655
      char *gotfree_input_line;
13484
655
      int adjust = 0;
13485
13486
655
      save = input_line_pointer;
13487
655
      gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
13488
655
      if (gotfree_input_line)
13489
67
  input_line_pointer = gotfree_input_line;
13490
13491
655
      expression (exp);
13492
13493
655
      if (gotfree_input_line)
13494
67
  {
13495
    /* expression () has merrily parsed up to the end of line,
13496
       or a comma - in the wrong buffer.  Transfer how far
13497
       input_line_pointer has moved to the right buffer.  */
13498
67
    input_line_pointer = (save
13499
67
        + (input_line_pointer - gotfree_input_line)
13500
67
        + adjust);
13501
67
    free (gotfree_input_line);
13502
67
    if (exp->X_op == O_constant
13503
1
        || exp->X_op == O_absent
13504
1
        || exp->X_op == O_illegal
13505
1
        || exp->X_op == O_register
13506
1
        || exp->X_op == O_big)
13507
67
      {
13508
67
        char c = *input_line_pointer;
13509
67
        *input_line_pointer = 0;
13510
67
        as_bad (_("missing or invalid expression `%s'"), save);
13511
67
        *input_line_pointer = c;
13512
67
      }
13513
0
    else if ((got_reloc == BFD_RELOC_386_PLT32
13514
0
        || got_reloc == BFD_RELOC_32_PLT_PCREL)
13515
0
       && exp->X_op != O_symbol)
13516
0
      {
13517
0
        char c = *input_line_pointer;
13518
0
        *input_line_pointer = 0;
13519
0
        as_bad (_("invalid PLT expression `%s'"), save);
13520
0
        *input_line_pointer = c;
13521
0
      }
13522
67
  }
13523
655
    }
13524
565
  else
13525
565
#endif
13526
565
    expression (exp);
13527
13528
1.22k
  intel_syntax = -intel_syntax;
13529
13530
1.22k
  if (intel_syntax)
13531
976
    i386_intel_simplify (exp);
13532
13533
  /* If not 64bit, massage value, to account for wraparound when !BFD64.  */
13534
1.22k
  if (size <= 4 && expr_mode == expr_operator_present
13535
264
      && exp->X_op == O_constant && !object_64bit)
13536
0
    exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
13537
13538
1.22k
  return got_reloc;
13539
1.22k
}
13540
13541
static void
13542
signed_cons (int size)
13543
1
{
13544
1
  if (object_64bit)
13545
1
    cons_sign = 1;
13546
1
  cons (size);
13547
1
  cons_sign = -1;
13548
1
}
13549
13550
static void
13551
s_insn (int dummy ATTRIBUTE_UNUSED)
13552
1.77k
{
13553
1.77k
  char mnemonic[MAX_MNEM_SIZE], *line = input_line_pointer, *ptr;
13554
1.77k
  char *saved_ilp = find_end_of_line (line, false), saved_char;
13555
1.77k
  const char *end;
13556
1.77k
  unsigned int j;
13557
1.77k
  valueT val;
13558
1.77k
  bool vex = false, xop = false;
13559
1.77k
  enum { evex_none, evex_basic, evex_nd } evex = evex_none;
13560
1.77k
  struct last_insn *last_insn;
13561
13562
1.77k
  init_globals ();
13563
13564
1.77k
  saved_char = *saved_ilp;
13565
1.77k
  *saved_ilp = 0;
13566
13567
1.77k
  end = parse_insn (line, mnemonic, parse_prefix);
13568
1.77k
  if (end == NULL)
13569
0
    {
13570
464
  bad:
13571
464
      *saved_ilp = saved_char;
13572
464
      ignore_rest_of_line ();
13573
464
      i.tm.mnem_off = 0;
13574
464
      memset (&pp, 0, sizeof (pp));
13575
464
      return;
13576
0
    }
13577
1.77k
  line += end - line;
13578
13579
1.77k
  current_templates.start = &i.tm;
13580
1.77k
  current_templates.end = &i.tm + 1;
13581
1.77k
  i.tm.mnem_off = MN__insn;
13582
1.77k
  i.tm.extension_opcode = None;
13583
13584
1.77k
  if (startswith (line, "VEX")
13585
1.30k
      && (line[3] == '.' || is_whitespace (line[3])))
13586
1.30k
    {
13587
1.30k
      vex = true;
13588
1.30k
      line += 3;
13589
1.30k
    }
13590
473
  else if (startswith (line, "XOP") && ISDIGIT (line[3]))
13591
11
    {
13592
11
      char *e;
13593
11
      unsigned long n = strtoul (line + 3, &e, 16);
13594
13595
11
      if (e == line + 5 && n >= 0x08 && n <= 0x1f
13596
8
    && (*e == '.' || is_whitespace (*e)))
13597
8
  {
13598
8
    xop = true;
13599
    /* Arrange for build_vex_prefix() to emit 0x8f.  */
13600
8
    i.tm.opcode_space = SPACE_XOP08;
13601
8
    i.insn_opcode_space = n;
13602
8
    line = e;
13603
8
  }
13604
11
    }
13605
462
  else if (startswith (line, "EVEX")
13606
34
     && (line[4] == '.' || is_whitespace (line[4])))
13607
34
    {
13608
34
      evex = evex_basic;
13609
34
      line += 4;
13610
34
    }
13611
13612
1.77k
  if (vex || xop
13613
1.77k
      ? pp.encoding == encoding_evex
13614
1.77k
      : evex
13615
465
  ? pp.encoding == encoding_vex
13616
34
    || pp.encoding == encoding_vex3
13617
465
  : pp.encoding != encoding_default)
13618
441
    {
13619
441
      as_bad (_("pseudo-prefix conflicts with encoding specifier"));
13620
441
      goto bad;
13621
441
    }
13622
13623
1.33k
  if (line > end && pp.encoding == encoding_default)
13624
903
    pp.encoding = evex ? encoding_evex : encoding_vex;
13625
13626
1.33k
  if (pp.encoding != encoding_default)
13627
903
    {
13628
      /* Only address size and segment override prefixes are permitted with
13629
         VEX/XOP/EVEX encodings.  */
13630
903
      const unsigned char *p = i.prefix;
13631
13632
7.22k
      for (j = 0; j < ARRAY_SIZE (i.prefix); ++j, ++p)
13633
6.32k
  {
13634
6.32k
    if (!*p)
13635
6.32k
      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
903
    }
13648
13649
1.33k
  if (line > end && *line == '.')
13650
229
    {
13651
      /* Length specifier (VEX.L, XOP.L, EVEX.L'L).  */
13652
229
      switch (line[1])
13653
229
  {
13654
18
  case 'L':
13655
18
    switch (line[2])
13656
18
      {
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
2
      case '1':
13665
2
        if (evex)
13666
2
    i.tm.opcode_modifier.evex = EVEX256;
13667
0
        else
13668
0
    i.tm.opcode_modifier.vex = VEX256;
13669
2
        break;
13670
13671
0
      case '2':
13672
0
        if (evex)
13673
0
    i.tm.opcode_modifier.evex = EVEX512;
13674
0
        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
18
      }
13692
13693
18
    if (i.tm.opcode_modifier.vex || i.tm.opcode_modifier.evex)
13694
2
      line += 3;
13695
18
    break;
13696
13697
2
  case '1':
13698
2
    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
2
    break;
13707
13708
121
  case '2':
13709
121
    if (line[2] == '5' && line[3] == '6')
13710
6
      {
13711
6
        if (evex)
13712
6
    i.tm.opcode_modifier.evex = EVEX256;
13713
0
        else
13714
0
    i.tm.opcode_modifier.vex = VEX256;
13715
6
        line += 4;
13716
6
      }
13717
121
    break;
13718
13719
24
  case '5':
13720
24
    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
24
    break;
13726
229
  }
13727
229
    }
13728
13729
1.33k
  if (line > end && *line == '.')
13730
221
    {
13731
      /* embedded prefix (VEX.pp, XOP.pp, EVEX.pp).  */
13732
221
      switch (line[1])
13733
221
  {
13734
2
  case 'N':
13735
2
    if (line[2] == 'P')
13736
0
      line += 3;
13737
2
    break;
13738
13739
4
  case '6':
13740
4
    if (line[2] == '6')
13741
0
      {
13742
0
        i.tm.opcode_modifier.opcodeprefix = PREFIX_0X66;
13743
0
        line += 3;
13744
0
      }
13745
4
    break;
13746
13747
25
  case 'F': case 'f':
13748
25
    if (line[2] == '3')
13749
9
      {
13750
9
        i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
13751
9
        line += 3;
13752
9
      }
13753
16
    else if (line[2] == '2')
13754
0
      {
13755
0
        i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF2;
13756
0
        line += 3;
13757
0
      }
13758
25
    break;
13759
221
  }
13760
221
    }
13761
13762
1.33k
  if (line > end && !xop && *line == '.')
13763
212
    {
13764
      /* Encoding space (VEX.mmmmm, EVEX.mmmm).  */
13765
212
      switch (line[1])
13766
212
  {
13767
20
  case '0':
13768
20
    if (TOUPPER (line[2]) != 'F')
13769
0
      break;
13770
20
    if (line[3] == '.' || is_whitespace (line[3]))
13771
4
      {
13772
4
        i.insn_opcode_space = SPACE_0F;
13773
4
        line += 3;
13774
4
      }
13775
16
    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
20
    break;
13783
13784
3
  case 'M':
13785
3
    if (ISDIGIT (line[2]) && line[2] != '0')
13786
3
      {
13787
3
        char *e;
13788
3
        unsigned long n = strtoul (line + 2, &e, 10);
13789
13790
3
        if (n <= (evex ? 15 : 31)
13791
3
      && (*e == '.' || is_whitespace (*e)))
13792
3
    {
13793
3
      i.insn_opcode_space = n;
13794
3
      line = e;
13795
3
    }
13796
3
      }
13797
3
    break;
13798
212
  }
13799
212
    }
13800
13801
1.33k
  if (line > end && *line == '.' && line[1] == 'W')
13802
8
    {
13803
      /* VEX.W, XOP.W, EVEX.W  */
13804
8
      switch (line[2])
13805
8
  {
13806
0
  case '0':
13807
0
    i.tm.opcode_modifier.vexw = VEXW0;
13808
0
    break;
13809
13810
0
  case '1':
13811
0
    i.tm.opcode_modifier.vexw = VEXW1;
13812
0
    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
8
  }
13822
13823
8
      if (i.tm.opcode_modifier.vexw)
13824
0
  line += 3;
13825
8
    }
13826
13827
1.33k
  if (line > end && evex && *line == '.')
13828
25
    {
13829
25
      if (line[1] == 'N' && line[2] == 'D')
13830
0
  {
13831
0
    evex = evex_nd;
13832
0
    line += 3;
13833
0
  }
13834
25
      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
25
    }
13840
13841
1.33k
  if (line > end && *line && !is_whitespace (*line))
13842
222
    {
13843
      /* Improve diagnostic a little.  */
13844
222
      if (*line == '.' && line[1] && !is_whitespace (line[1]))
13845
210
  ++line;
13846
222
      goto done;
13847
222
    }
13848
13849
  /* Before processing the opcode expression, find trailing "+r" or
13850
     "/<digit>" specifiers.  */
13851
1.11k
  for (ptr = line; ; ++ptr)
13852
1.70k
    {
13853
1.70k
      unsigned long n;
13854
1.70k
      char *e;
13855
13856
1.70k
      ptr = strpbrk (ptr, "+/,");
13857
1.70k
      if (ptr == NULL || *ptr == ',')
13858
834
  break;
13859
13860
870
      if (*ptr == '+' && ptr[1] == 'r'
13861
34
    && (ptr[2] == ',' || (is_whitespace (ptr[2]) && ptr[3] == ',')))
13862
13
  {
13863
13
    *ptr = ' ';
13864
13
    ptr[1] = ' ';
13865
13
    i.short_form = true;
13866
13
    break;
13867
13
  }
13868
13869
857
      if (*ptr == '/' && ISDIGIT (ptr[1])
13870
266
    && (n = strtoul (ptr + 1, &e, 8)) < 8
13871
266
    && e == ptr + 2
13872
265
    && (ptr[2] == ',' || (is_whitespace (ptr[2]) && ptr[3] == ',')))
13873
265
  {
13874
265
    *ptr = ' ';
13875
265
    ptr[1] = ' ';
13876
265
    i.tm.extension_opcode = n;
13877
265
    i.tm.opcode_modifier.modrm = 1;
13878
265
    break;
13879
265
  }
13880
857
    }
13881
13882
1.11k
  input_line_pointer = line;
13883
1.11k
  val = get_absolute_expression ();
13884
1.11k
  line = input_line_pointer;
13885
13886
1.11k
  if (i.short_form && (val & 7))
13887
0
    as_warn ("`+r' assumes low three opcode bits to be clear");
13888
13889
1.31k
  for (j = 1; j < sizeof(val); ++j)
13890
1.31k
    if (!(val >> (j * 8)))
13891
1.10k
      break;
13892
13893
  /* Trim off a prefix if present.  */
13894
1.11k
  if (j > 1 && !vex && !xop && !evex)
13895
50
    {
13896
50
      uint8_t byte = val >> ((j - 1) * 8);
13897
13898
50
      switch (byte)
13899
50
  {
13900
8
  case DATA_PREFIX_OPCODE:
13901
16
  case REPE_PREFIX_OPCODE:
13902
16
  case REPNE_PREFIX_OPCODE:
13903
16
    if (!add_prefix (byte))
13904
0
      goto bad;
13905
16
    val &= ((uint64_t)1 << (--j * 8)) - 1;
13906
16
    break;
13907
50
  }
13908
50
    }
13909
13910
1.11k
  if (evex == evex_basic && *line == '{')
13911
0
    {
13912
0
      int length = check_Scc_OszcOperations (line);
13913
13914
0
      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
0
    }
13928
13929
  /* Parse operands, if any, before evaluating encoding space.  */
13930
1.11k
  if (*line == ',')
13931
249
    {
13932
249
      i.memshift = -1;
13933
13934
249
      ptr = parse_operands (line + 1, &i386_mnemonics[MN__insn]);
13935
249
      this_operand = -1;
13936
249
      if (!ptr)
13937
23
  goto bad;
13938
226
      line = ptr;
13939
13940
226
      if (!i.operands)
13941
2
  {
13942
2
    as_bad (_("expecting operand after ','; got nothing"));
13943
2
    goto done;
13944
2
  }
13945
13946
224
      if (i.mem_operands > 1)
13947
0
  {
13948
0
    as_bad (_("too many memory references for `%s'"),
13949
0
      &i386_mnemonics[MN__insn]);
13950
0
    goto done;
13951
0
  }
13952
13953
      /* No need to distinguish encoding_evex and encoding_evex512.  */
13954
224
      if (pp.encoding == encoding_evex512)
13955
0
  pp.encoding = encoding_evex;
13956
224
    }
13957
13958
  /* Trim off encoding space.  */
13959
1.08k
  if (j > 1 && !i.insn_opcode_space && (val >> ((j - 1) * 8)) == 0x0f)
13960
26
    {
13961
26
      uint8_t byte = val >> ((--j - 1) * 8);
13962
13963
26
      i.insn_opcode_space = SPACE_0F;
13964
26
      switch (byte & -(j > 1 && !pp.rex2_encoding
13965
26
           && (pp.encoding != encoding_egpr || evex)))
13966
26
  {
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
26
  }
13976
26
      i.tm.opcode_space = i.insn_opcode_space;
13977
26
      val &= ((uint64_t)1 << (j * 8)) - 1;
13978
26
    }
13979
1.08k
  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
664
    i.tm.opcode_space = i.insn_opcode_space == SPACE_0F ? SPACE_0F
13983
664
               : SPACE_0F38;
13984
13985
1.08k
  if (j > 2)
13986
38
    {
13987
38
      as_bad (_("opcode residual (%#"PRIx64") too wide"), (uint64_t) val);
13988
38
      goto done;
13989
38
    }
13990
1.04k
  i.opcode_length = j;
13991
13992
  /* Handle operands, if any.  */
13993
1.04k
  if (i.operands)
13994
224
    {
13995
224
      i386_operand_type combined;
13996
224
      expressionS *disp_exp = NULL;
13997
224
      bool changed;
13998
13999
224
      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
224
      if (!i.short_form
14014
213
    && (i.mem_operands
14015
21
        || i.reg_operands > (pp.encoding != encoding_default)
14016
21
        || i.tm.extension_opcode != None))
14017
192
  i.tm.opcode_modifier.modrm = 1;
14018
14019
224
      if (!i.tm.opcode_modifier.modrm
14020
32
    && (i.reg_operands
14021
32
        > i.short_form + 0U + (pp.encoding != encoding_default)
14022
32
        || 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
224
      switch (i.reg_operands + i.mem_operands
14030
224
        + (i.tm.extension_opcode != None)
14031
224
        + (i.tm.opcode_modifier.operandconstraint == SCC))
14032
224
  {
14033
24
  case 0:
14034
24
    if (i.short_form)
14035
5
      {
14036
5
        as_bad (_("too few register/memory operands"));
14037
5
        goto done;
14038
5
      }
14039
    /* Fall through.  */
14040
28
  case 1:
14041
28
    if (i.tm.opcode_modifier.modrm)
14042
1
      {
14043
1
        as_bad (_("too few register/memory operands"));
14044
1
        goto done;
14045
1
      }
14046
    /* Fall through.  */
14047
218
  case 2:
14048
218
    if (evex == evex_nd)
14049
0
      {
14050
0
        as_bad (_("too few register/memory operands"));
14051
0
        goto done;
14052
0
      }
14053
218
    break;
14054
14055
218
  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
0
  case 3:
14065
0
    if (i.tm.opcode_modifier.operandconstraint == SCC)
14066
0
      break;
14067
0
    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
0
  default:
14076
0
    as_bad (_("too many register/memory operands"));
14077
0
    goto done;
14078
224
  }
14079
14080
      /* Bring operands into canonical order (imm, mem, reg).  */
14081
218
      do
14082
218
  {
14083
218
    changed = false;
14084
14085
218
    for (j = 1; j < i.operands; ++j)
14086
0
      {
14087
0
        if ((!operand_type_check (i.types[j - 1], imm)
14088
0
       && operand_type_check (i.types[j], imm))
14089
0
      || (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
0
      }
14096
218
  }
14097
218
      while (changed);
14098
14099
      /* For Intel syntax swap the order of register operands.  */
14100
218
      if (intel_syntax)
14101
135
  switch (i.reg_operands)
14102
135
    {
14103
129
    case 0:
14104
135
    case 1:
14105
135
      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
135
    }
14118
14119
      /* Enforce constraints when using VSIB.  */
14120
218
      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
218
      operand_type_set (&combined, 0);
14147
14148
417
      for (j = i.imm_operands; j < i.operands; ++j)
14149
199
  {
14150
    /* Look for 8-bit operands that use old registers.  */
14151
199
    if (pp.encoding != encoding_default
14152
193
        && flag_code == CODE_64BIT
14153
2
        && i.types[j].bitfield.class == Reg
14154
2
        && 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
199
    i.types[j].bitfield.instance = InstanceNone;
14161
14162
199
    if (operand_type_check (i.types[j], disp))
14163
191
      {
14164
191
        i.types[j].bitfield.baseindex = 1;
14165
191
        disp_exp = i.op[j].disps;
14166
191
      }
14167
14168
199
    if (evex && i.types[j].bitfield.baseindex)
14169
0
      {
14170
0
        unsigned int n = i.memshift;
14171
14172
0
        if (i.types[j].bitfield.byte)
14173
0
    n = 0;
14174
0
        else if (i.types[j].bitfield.word)
14175
0
    n = 1;
14176
0
        else if (i.types[j].bitfield.dword)
14177
0
    n = 2;
14178
0
        else if (i.types[j].bitfield.qword)
14179
0
    n = 3;
14180
0
        else if (i.types[j].bitfield.xmmword)
14181
0
    n = 4;
14182
0
        else if (i.types[j].bitfield.ymmword)
14183
0
    n = 5;
14184
0
        else if (i.types[j].bitfield.zmmword)
14185
0
    n = 6;
14186
14187
0
        if (i.memshift < 32 && n != i.memshift)
14188
0
    as_warn ("conflicting memory operand size specifiers");
14189
0
        i.memshift = n;
14190
0
      }
14191
14192
199
    if ((i.broadcast.type || i.broadcast.bytes)
14193
0
        && j == i.broadcast.operand)
14194
0
      continue;
14195
14196
199
    combined = operand_type_or (combined, i.types[j]);
14197
199
    combined.bitfield.class = ClassNone;
14198
199
  }
14199
14200
218
      switch ((i.broadcast.type ? i.broadcast.type : 1)
14201
218
        << (i.memshift < 32 ? i.memshift : 0))
14202
218
  {
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
218
  }
14209
14210
218
      if (pp.encoding == encoding_default)
14211
25
  {
14212
25
    if (flag_code == CODE_64BIT && combined.bitfield.qword)
14213
0
      i.rex |= REX_W;
14214
25
    else if ((flag_code == CODE_16BIT ? combined.bitfield.dword
14215
25
              : combined.bitfield.word)
14216
6
             && !add_prefix (DATA_PREFIX_OPCODE))
14217
0
      goto done;
14218
25
  }
14219
193
      else if (!i.tm.opcode_modifier.vexw)
14220
193
  {
14221
193
    if (flag_code == CODE_64BIT)
14222
2
      {
14223
2
        if (combined.bitfield.qword)
14224
2
          i.tm.opcode_modifier.vexw = VEXW1;
14225
0
        else if (combined.bitfield.dword)
14226
0
          i.tm.opcode_modifier.vexw = VEXW0;
14227
2
      }
14228
14229
193
    if (!i.tm.opcode_modifier.vexw)
14230
191
      i.tm.opcode_modifier.vexw = VEXWIG;
14231
193
  }
14232
14233
218
      if (vex || xop)
14234
191
  {
14235
191
    if (!i.tm.opcode_modifier.vex)
14236
191
      {
14237
191
        if (combined.bitfield.ymmword)
14238
0
          i.tm.opcode_modifier.vex = VEX256;
14239
191
        else if (combined.bitfield.xmmword)
14240
0
          i.tm.opcode_modifier.vex = VEX128;
14241
191
      }
14242
191
  }
14243
27
      else if (evex)
14244
2
  {
14245
2
    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
2
    if (i.memshift >= 32)
14257
2
      {
14258
2
        unsigned int n = 0;
14259
14260
2
        switch (i.tm.opcode_modifier.evex)
14261
2
    {
14262
0
    case EVEX512: n = 64; break;
14263
2
    case EVEX256: n = 32; break;
14264
0
    case EVEX128: n = 16; break;
14265
2
    }
14266
14267
2
        if (i.broadcast.type)
14268
0
    n /= i.broadcast.type;
14269
14270
2
        if (n > 0)
14271
12
    for (i.memshift = 0; !(n & 1); n >>= 1)
14272
10
      ++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
2
      }
14282
2
  }
14283
14284
218
      if (i.memshift >= 32)
14285
216
  i.memshift = 0;
14286
2
      else if (!evex)
14287
0
  pp.encoding = encoding_error;
14288
14289
218
      if (i.disp_operands && !optimize_disp (&i.tm))
14290
0
  goto done;
14291
14292
      /* Establish size for immediate operands.  */
14293
237
      for (j = 0; j < i.imm_operands; ++j)
14294
19
  {
14295
19
    expressionS *expP = i.op[j].imms;
14296
14297
19
    gas_assert (operand_type_check (i.types[j], imm));
14298
19
    operand_type_set (&i.types[j], 0);
14299
14300
19
    if (i.imm_bits[j] > 32)
14301
0
      i.types[j].bitfield.imm64 = 1;
14302
19
    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
19
    else if (i.imm_bits[j] > 8)
14310
0
      i.types[j].bitfield.imm16 = 1;
14311
19
    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
19
    else if (expP->X_op == O_constant)
14319
19
      {
14320
19
        i.types[j] = smallest_imm_type (expP->X_add_number);
14321
19
        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
19
        if (flag_code != CODE_64BIT)
14325
17
    {
14326
17
      i.types[j].bitfield.imm64 = 0;
14327
17
      i.types[j].bitfield.imm32s = 0;
14328
17
      i.types[j].bitfield.imm32 = 1;
14329
17
    }
14330
2
        else if (i.types[j].bitfield.imm32 || i.types[j].bitfield.imm32s)
14331
2
    i.types[j].bitfield.imm64 = 0;
14332
19
      }
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
19
  }
14342
14343
436
      for (j = 0; j < i.operands; ++j)
14344
218
  i.tm.operand_types[j] = i.types[j];
14345
14346
218
      process_operands ();
14347
218
    }
14348
14349
  /* Don't set opcode until after processing operands, to avoid any
14350
     potential special casing there.  */
14351
1.04k
  i.tm.base_opcode |= val;
14352
14353
1.04k
  if (pp.encoding == encoding_error
14354
1.04k
      || (pp.encoding != encoding_evex
14355
1.04k
    ? i.broadcast.type || i.broadcast.bytes
14356
1.03k
      || i.rounding.type != rc_none
14357
1.03k
      || i.mask.reg
14358
1.04k
    : (i.mem_operands && i.rounding.type != rc_none)
14359
6
      || ((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
1.04k
  if (vex || xop)
14367
666
    {
14368
666
      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
666
      if (!i.tm.opcode_modifier.vex)
14376
666
  i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
14377
14378
666
      build_vex_prefix (NULL);
14379
666
      i.rex &= REX_OPCODE;
14380
666
    }
14381
377
  else if (evex)
14382
6
    {
14383
6
      if (!i.tm.opcode_modifier.evex)
14384
1
  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
6
      if ((!is_apx_evex_encoding () || i.insn_opcode_space > 7)
14390
4
    && evex == evex_basic
14391
4
    && !i.tm.opcode_modifier.operandconstraint)
14392
4
  build_evex_prefix ();
14393
2
      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
2
      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
2
      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
2
      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
2
      else if (!build_apx_evex_prefix (evex == evex_nd))
14415
0
  goto done;
14416
6
      i.rex &= REX_OPCODE;
14417
6
    }
14418
371
  else
14419
371
    establish_rex ();
14420
14421
1.04k
  last_insn = &seg_info(now_seg)->tc_segment_info_data.last_insn;
14422
1.04k
  output_insn (last_insn);
14423
1.04k
  last_insn->kind = last_insn_directive;
14424
1.04k
  last_insn->name = ".insn directive";
14425
1.04k
  last_insn->file = as_where (&last_insn->line);
14426
14427
1.04k
#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
1.04k
  if (flag_synth_cfi)
14431
0
    as_bad (_("SCFI: hand-crafting instructions not supported"));
14432
1.04k
#endif
14433
14434
1.31k
 done:
14435
1.31k
  *saved_ilp = saved_char;
14436
1.31k
  input_line_pointer = line;
14437
14438
1.31k
  demand_empty_rest_of_line ();
14439
14440
  /* Make sure dot_insn() won't yield "true" anymore.  */
14441
1.31k
  i.tm.mnem_off = 0;
14442
14443
1.31k
  current_templates.start = NULL;
14444
1.31k
  memset (&pp, 0, sizeof (pp));
14445
1.31k
}
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
544
{
14492
544
  unsigned int j;
14493
14494
3.26k
  for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
14495
2.72k
    {
14496
2.72k
      if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
14497
3
  {
14498
3
    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
3
    switch (pp.encoding)
14505
3
      {
14506
3
      case encoding_default:
14507
3
      case encoding_egpr:
14508
3
        pp.encoding = encoding_evex512;
14509
3
        break;
14510
0
      case encoding_evex:
14511
0
      case encoding_evex512:
14512
0
        break;
14513
0
      default:
14514
0
        return NULL;
14515
3
      }
14516
14517
3
    i.rounding.type = RC_NamesTable[j].type;
14518
14519
3
    return (char *)(pstr + RC_NamesTable[j].len);
14520
3
  }
14521
2.72k
    }
14522
14523
541
  return NULL;
14524
544
}
14525
14526
/* Handle Vector operations.  */
14527
14528
static char *
14529
check_VecOperations (char *op_string)
14530
554
{
14531
554
  const reg_entry *mask;
14532
554
  const char *saved;
14533
554
  char *end_op;
14534
14535
554
  while (*op_string)
14536
554
    {
14537
554
      saved = op_string;
14538
554
      if (*op_string == '{')
14539
554
  {
14540
554
    op_string++;
14541
554
    if (is_whitespace (*op_string))
14542
0
      op_string++;
14543
14544
    /* Check broadcasts.  */
14545
554
    if (startswith (op_string, "1to"))
14546
0
      {
14547
0
        unsigned int bcst_type;
14548
14549
0
        if (i.broadcast.type)
14550
0
    goto duplicated_vec_op;
14551
14552
0
        op_string += 3;
14553
0
        if (*op_string == '8')
14554
0
    bcst_type = 8;
14555
0
        else if (*op_string == '4')
14556
0
    bcst_type = 4;
14557
0
        else if (*op_string == '2')
14558
0
    bcst_type = 2;
14559
0
        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
0
        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
0
        else
14572
0
    {
14573
0
      as_bad (_("Unsupported broadcast: `%s'"), saved);
14574
0
      return NULL;
14575
0
    }
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
554
    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
554
    else if ((mask = parse_register (op_string, &end_op)) != NULL)
14641
7
      {
14642
7
        if (mask == &bad_reg)
14643
0
    return NULL;
14644
14645
        /* k0 can't be used for write mask.  */
14646
7
        if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
14647
7
    {
14648
7
      as_bad (_("`%s%s' can't be used for write mask"),
14649
7
        register_prefix, mask->reg_name);
14650
7
      return NULL;
14651
7
    }
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
547
    else if (*op_string == 'z')
14677
0
      {
14678
0
        if (!i.mask.reg)
14679
0
    {
14680
0
      i.mask.reg = reg_k0;
14681
0
      i.mask.zeroing = 1;
14682
0
      i.mask.operand = this_operand;
14683
0
    }
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
0
        op_string++;
14706
0
      }
14707
547
    else if (intel_syntax
14708
538
       && (op_string = RC_SAE_specifier (op_string)) != NULL)
14709
3
      i.rounding.modifier = true;
14710
544
    else
14711
544
      goto unknown_vec_op;
14712
14713
3
    if (is_whitespace (*op_string))
14714
1
      op_string++;
14715
3
    if (*op_string != '}')
14716
3
      {
14717
3
        as_bad (_("missing `}' in `%s'"), saved);
14718
3
        return NULL;
14719
3
      }
14720
0
    op_string++;
14721
14722
0
    if (is_whitespace (*op_string))
14723
0
      ++op_string;
14724
14725
0
    continue;
14726
3
  }
14727
544
    unknown_vec_op:
14728
      /* We don't know this one.  */
14729
544
      as_bad (_("unknown vector operation: `%s'"), saved);
14730
544
      return NULL;
14731
554
    }
14732
14733
0
  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
0
  return op_string;
14740
0
}
14741
14742
static int
14743
i386_immediate (char *imm_start)
14744
1.47k
{
14745
1.47k
  char *save_input_line_pointer;
14746
1.47k
  char *gotfree_input_line;
14747
1.47k
  segT exp_seg = 0;
14748
1.47k
  expressionS *exp;
14749
1.47k
  i386_operand_type types;
14750
14751
1.47k
  operand_type_set (&types, ~0);
14752
14753
1.47k
  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
1.47k
  exp = &im_expressions[i.imm_operands++];
14761
1.47k
  i.op[this_operand].imms = exp;
14762
14763
1.47k
  if (is_whitespace (*imm_start))
14764
0
    ++imm_start;
14765
14766
1.47k
  save_input_line_pointer = input_line_pointer;
14767
1.47k
  input_line_pointer = imm_start;
14768
14769
1.47k
  gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
14770
1.47k
  if (gotfree_input_line)
14771
0
    input_line_pointer = gotfree_input_line;
14772
14773
1.47k
  expr_mode = expr_operator_none;
14774
1.47k
  exp_seg = expression (exp);
14775
14776
  /* For .insn immediates there may be a size specifier.  */
14777
1.47k
  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
1.47k
  SKIP_WHITESPACE ();
14793
1.47k
  if (*input_line_pointer)
14794
994
    as_bad (_("junk `%s' after expression"), input_line_pointer);
14795
14796
1.47k
  input_line_pointer = save_input_line_pointer;
14797
1.47k
  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
1.47k
  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
1.47k
  return i386_finalize_immediate (exp_seg, exp, types, imm_start);
14812
1.47k
}
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
2.35k
{
14818
2.35k
  if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
14819
17
    {
14820
17
      if (imm_start)
14821
17
  as_bad (_("missing or invalid immediate expression `%s'"),
14822
17
    imm_start);
14823
17
      return 0;
14824
17
    }
14825
2.33k
  else if (exp->X_op == O_constant)
14826
1.30k
    {
14827
      /* Size it properly later.  */
14828
1.30k
      i.types[this_operand].bitfield.imm64 = 1;
14829
14830
      /* If not 64bit, sign/zero extend val, to account for wraparound
14831
   when !BFD64.  */
14832
1.30k
      if (expr_mode == expr_operator_present
14833
306
    && flag_code != CODE_64BIT && !object_64bit)
14834
0
  exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
14835
1.30k
    }
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.03k
  else
14849
1.03k
    {
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.03k
      i.types[this_operand].bitfield.imm8 = 1;
14854
1.03k
      i.types[this_operand].bitfield.imm16 = 1;
14855
1.03k
      i.types[this_operand].bitfield.imm32 = 1;
14856
1.03k
      i.types[this_operand].bitfield.imm32s = 1;
14857
1.03k
      i.types[this_operand].bitfield.imm64 = 1;
14858
1.03k
      i.types[this_operand] = operand_type_and (i.types[this_operand],
14859
1.03k
            types);
14860
1.03k
    }
14861
14862
2.33k
  return 1;
14863
2.35k
}
14864
14865
static char *
14866
i386_scale (char *scale)
14867
0
{
14868
0
  offsetT val;
14869
0
  char *save = input_line_pointer;
14870
14871
0
  input_line_pointer = scale;
14872
0
  val = get_absolute_expression ();
14873
14874
0
  switch (val)
14875
0
    {
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
0
    default:
14889
0
      {
14890
0
  char sep = *input_line_pointer;
14891
14892
0
  *input_line_pointer = '\0';
14893
0
  as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
14894
0
    scale);
14895
0
  *input_line_pointer = sep;
14896
0
  input_line_pointer = save;
14897
0
  return NULL;
14898
0
      }
14899
0
    }
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
0
}
14910
14911
static int
14912
i386_displacement (char *disp_start, char *disp_end)
14913
4.36k
{
14914
4.36k
  expressionS *exp;
14915
4.36k
  segT exp_seg = 0;
14916
4.36k
  char *save_input_line_pointer;
14917
4.36k
  char *gotfree_input_line;
14918
4.36k
  int override;
14919
4.36k
  i386_operand_type bigdisp, types = anydisp;
14920
4.36k
  int ret;
14921
14922
4.36k
  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
4.36k
  operand_type_set (&bigdisp, 0);
14930
4.36k
  if (i.jumpabsolute
14931
4.35k
      || i.types[this_operand].bitfield.baseindex
14932
4.35k
      || (current_templates.start->opcode_modifier.jump != JUMP
14933
4.33k
    && current_templates.start->opcode_modifier.jump != JUMP_DWORD))
14934
4.29k
    {
14935
4.29k
      i386_addressing_mode ();
14936
4.29k
      override = (i.prefix[ADDR_PREFIX] != 0);
14937
4.29k
      if (flag_code == CODE_64BIT)
14938
4.01k
  {
14939
4.01k
    bigdisp.bitfield.disp32 = 1;
14940
4.01k
    if (!override)
14941
4.01k
      bigdisp.bitfield.disp64 = 1;
14942
4.01k
  }
14943
278
      else if ((flag_code == CODE_16BIT) ^ override)
14944
212
    bigdisp.bitfield.disp16 = 1;
14945
66
      else
14946
66
    bigdisp.bitfield.disp32 = 1;
14947
4.29k
    }
14948
70
  else
14949
70
    {
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
70
      const insn_template *t = current_templates.start;
14955
70
      bool has_intel64 = false;
14956
14957
166
      while (++t < current_templates.end)
14958
145
  {
14959
145
    if (t->opcode_modifier.jump
14960
145
        != current_templates.start->opcode_modifier.jump)
14961
49
      break;
14962
96
    if ((t->opcode_modifier.isa64 >= INTEL64))
14963
49
      has_intel64 = true;
14964
96
  }
14965
70
      current_templates.end = t;
14966
14967
70
      override = (i.prefix[DATA_PREFIX] != 0);
14968
70
      if (flag_code == CODE_64BIT)
14969
68
  {
14970
68
    if ((override || i.suffix == WORD_MNEM_SUFFIX)
14971
41
        && (!intel64 || !has_intel64))
14972
1
      bigdisp.bitfield.disp16 = 1;
14973
67
    else
14974
67
      bigdisp.bitfield.disp32 = 1;
14975
68
  }
14976
2
      else
14977
2
  {
14978
2
    if (!override)
14979
0
      override = (i.suffix == (flag_code != CODE_16BIT
14980
0
             ? WORD_MNEM_SUFFIX
14981
0
             : LONG_MNEM_SUFFIX));
14982
2
    bigdisp.bitfield.disp32 = 1;
14983
2
    if ((flag_code == CODE_16BIT) ^ override)
14984
0
      {
14985
0
        bigdisp.bitfield.disp32 = 0;
14986
0
        bigdisp.bitfield.disp16 = 1;
14987
0
      }
14988
2
  }
14989
70
    }
14990
4.36k
  i.types[this_operand] = operand_type_or (i.types[this_operand],
14991
4.36k
             bigdisp);
14992
14993
4.36k
  exp = &disp_expressions[i.disp_operands];
14994
4.36k
  i.op[this_operand].disps = exp;
14995
4.36k
  i.disp_operands++;
14996
4.36k
  save_input_line_pointer = input_line_pointer;
14997
4.36k
  input_line_pointer = disp_start;
14998
4.36k
  END_STRING_AND_SAVE (disp_end);
14999
15000
4.36k
#ifndef GCC_ASM_O_HACK
15001
4.36k
#define GCC_ASM_O_HACK 0
15002
4.36k
#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
4.36k
  gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
15045
4.36k
  if (gotfree_input_line)
15046
416
    input_line_pointer = gotfree_input_line;
15047
15048
4.36k
  expr_mode = expr_operator_none;
15049
4.36k
  exp_seg = expression (exp);
15050
15051
4.36k
  SKIP_WHITESPACE ();
15052
4.36k
  if (*input_line_pointer)
15053
3.47k
    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
4.36k
  input_line_pointer = save_input_line_pointer;
15058
4.36k
  if (gotfree_input_line)
15059
416
    {
15060
416
      free (gotfree_input_line);
15061
15062
416
      if (exp->X_op == O_constant || exp->X_op == O_register)
15063
0
  exp->X_op = O_illegal;
15064
416
    }
15065
15066
4.36k
  ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
15067
15068
4.36k
  RESTORE_END_STRING (disp_end);
15069
15070
4.36k
  return ret;
15071
4.36k
}
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
8.25k
{
15077
8.25k
  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
8.25k
  if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
15083
8.25k
      || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
15084
8.24k
      || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
15085
11
    {
15086
11
      if (exp->X_op != O_symbol)
15087
0
  goto inv_disp;
15088
15089
11
      if (S_IS_LOCAL (exp->X_add_symbol)
15090
0
    && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
15091
0
    && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
15092
0
  section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
15093
11
      exp->X_op = O_subtract;
15094
11
      exp->X_op_symbol = GOT_symbol;
15095
11
      if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
15096
11
  i.reloc[this_operand] = BFD_RELOC_32_PCREL;
15097
0
      else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
15098
0
  i.reloc[this_operand] = BFD_RELOC_64;
15099
0
      else
15100
0
  i.reloc[this_operand] = BFD_RELOC_32;
15101
11
    }
15102
15103
8.24k
  else if (exp->X_op == O_absent
15104
8.24k
     || exp->X_op == O_illegal
15105
8.24k
     || exp->X_op == O_big)
15106
4
    {
15107
4
    inv_disp:
15108
4
      as_bad (_("missing or invalid displacement expression `%s'"),
15109
4
        disp_start);
15110
4
      ret = 0;
15111
4
    }
15112
15113
8.24k
  else if (exp->X_op == O_constant)
15114
1.44k
    {
15115
      /* Sizing gets taken care of by optimize_disp().
15116
15117
   If not 64bit, sign/zero extend val, to account for wraparound
15118
   when !BFD64.  */
15119
1.44k
      if (expr_mode == expr_operator_present
15120
98
    && flag_code != CODE_64BIT && !object_64bit)
15121
0
  exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
15122
1.44k
    }
15123
15124
#ifdef OBJ_AOUT
15125
  else if (exp_seg != absolute_section
15126
     && exp_seg != text_section
15127
     && exp_seg != data_section
15128
     && exp_seg != bss_section
15129
     && exp_seg != undefined_section
15130
     && !bfd_is_com_section (exp_seg))
15131
    {
15132
      as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
15133
      ret = 0;
15134
    }
15135
#endif
15136
15137
6.80k
  else if (current_templates.start->opcode_modifier.jump == JUMP_BYTE)
15138
8
    i.types[this_operand].bitfield.disp8 = 1;
15139
15140
  /* Check if this is a displacement only operand.  */
15141
8.25k
  if (!i.types[this_operand].bitfield.baseindex)
15142
8.25k
    i.types[this_operand] =
15143
8.25k
      operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
15144
8.25k
           operand_type_and (i.types[this_operand], types));
15145
15146
8.25k
  return ret;
15147
8.25k
}
15148
15149
/* Return the active addressing mode, taking address override and
15150
   registers forming the address into consideration.  Update the
15151
   address override prefix if necessary.  */
15152
15153
static enum flag_code
15154
i386_addressing_mode (void)
15155
16.3k
{
15156
16.3k
  enum flag_code addr_mode;
15157
15158
16.3k
  if (i.prefix[ADDR_PREFIX])
15159
1
    addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
15160
16.3k
  else if (flag_code == CODE_16BIT
15161
2.27k
     && is_cpu (current_templates.start, CpuMPX)
15162
     /* Avoid replacing the "16-bit addressing not allowed" diagnostic
15163
        from md_assemble() by "is not a valid base/index expression"
15164
        when there is a base and/or index.  */
15165
0
     && !i.types[this_operand].bitfield.baseindex)
15166
0
    {
15167
      /* MPX insn memory operands with neither base nor index must be forced
15168
   to use 32-bit addressing in 16-bit mode.  */
15169
0
      addr_mode = CODE_32BIT;
15170
0
      i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
15171
0
      ++i.prefixes;
15172
0
      gas_assert (!i.types[this_operand].bitfield.disp16);
15173
0
      gas_assert (!i.types[this_operand].bitfield.disp32);
15174
0
    }
15175
16.3k
  else
15176
16.3k
    {
15177
16.3k
      addr_mode = flag_code;
15178
15179
16.3k
#if INFER_ADDR_PREFIX
15180
16.3k
      if (i.mem_operands == 0)
15181
14.6k
  {
15182
    /* Infer address prefix from the first memory operand.  */
15183
14.6k
    const reg_entry *addr_reg = i.base_reg;
15184
15185
14.6k
    if (addr_reg == NULL)
15186
14.6k
      addr_reg = i.index_reg;
15187
15188
14.6k
    if (addr_reg)
15189
2
      {
15190
2
        if (addr_reg->reg_type.bitfield.dword)
15191
1
    addr_mode = CODE_32BIT;
15192
1
        else if (flag_code != CODE_64BIT
15193
0
           && addr_reg->reg_type.bitfield.word)
15194
0
    addr_mode = CODE_16BIT;
15195
15196
2
        if (addr_mode != flag_code)
15197
1
    {
15198
1
      i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
15199
1
      i.prefixes += 1;
15200
      /* Change the size of any displacement too.  At most one
15201
         of Disp16 or Disp32 is set.
15202
         FIXME.  There doesn't seem to be any real need for
15203
         separate Disp16 and Disp32 flags.  The same goes for
15204
         Imm16 and Imm32.  Removing them would probably clean
15205
         up the code quite a lot.  */
15206
1
      if (flag_code != CODE_64BIT
15207
1
          && (i.types[this_operand].bitfield.disp16
15208
1
        || i.types[this_operand].bitfield.disp32))
15209
0
        {
15210
0
          static const i386_operand_type disp16_32 = {
15211
0
      .bitfield = { .disp16 = 1, .disp32 = 1 }
15212
0
          };
15213
15214
0
          i.types[this_operand]
15215
0
      = operand_type_xor (i.types[this_operand], disp16_32);
15216
0
        }
15217
1
    }
15218
2
      }
15219
14.6k
  }
15220
16.3k
#endif
15221
16.3k
    }
15222
15223
16.3k
  return addr_mode;
15224
16.3k
}
15225
15226
/* Make sure the memory operand we've been dealt is valid.
15227
   Return 1 on success, 0 on a failure.  */
15228
15229
static int
15230
i386_index_check (const char *operand_string)
15231
8.16k
{
15232
8.16k
  const char *kind = "base/index";
15233
8.16k
  enum flag_code addr_mode = i386_addressing_mode ();
15234
8.16k
  const insn_template *t = current_templates.end - 1;
15235
15236
8.16k
  if (t->opcode_modifier.isstring)
15237
34
    {
15238
      /* Memory operands of string insns are special in that they only allow
15239
   a single register (rDI or rSI) as their memory address.  */
15240
34
      const reg_entry *expected_reg;
15241
34
      static const char di_si[][2][4] =
15242
34
  {
15243
34
    { "esi", "edi" },
15244
34
    { "si", "di" },
15245
34
    { "rsi", "rdi" }
15246
34
  };
15247
      /* For a few other insns with fixed register addressing we (ab)use the
15248
   IsString attribute as well.  */
15249
34
      static const char loregs[][4][4] =
15250
34
  {
15251
34
    { "eax", "ecx", "edx", "ebx" },
15252
34
    {  "ax",  "cx",  "dx",  "bx" },
15253
34
    { "rax", "rcx", "rdx", "rbx" }
15254
34
  };
15255
15256
34
      kind = "string address";
15257
15258
34
      if (t->opcode_modifier.prefixok == PrefixRep)
15259
34
  {
15260
34
    int es_op = t->opcode_modifier.isstring - IS_STRING_ES_OP0;
15261
34
    int op = 0;
15262
15263
34
    if (!t->operand_types[0].bitfield.baseindex
15264
2
        || ((!i.mem_operands != !intel_syntax)
15265
1
      && t->operand_types[1].bitfield.baseindex))
15266
32
      op = 1;
15267
34
    expected_reg = str_hash_find (reg_hash,
15268
34
          di_si[addr_mode][op == es_op]);
15269
34
  }
15270
0
      else
15271
0
  {
15272
0
    unsigned int op = t->operand_types[0].bitfield.baseindex ? 0 : 1;
15273
15274
0
    if (!t->operand_types[op].bitfield.instance)
15275
0
      return 1; /* Operand mismatch will be detected elsewhere.  */
15276
0
    expected_reg
15277
0
      = str_hash_find (reg_hash,
15278
0
           loregs[addr_mode][t->operand_types[op]
15279
0
                 .bitfield.instance - 1]);
15280
0
  }
15281
15282
34
      if (i.base_reg != expected_reg
15283
0
    || i.index_reg
15284
0
    || operand_type_check (i.types[this_operand], disp))
15285
34
  {
15286
    /* The second memory operand must have the same size as
15287
       the first one.  */
15288
34
    if (i.mem_operands
15289
0
        && i.base_reg
15290
0
        && !((addr_mode == CODE_64BIT
15291
0
        && i.base_reg->reg_type.bitfield.qword)
15292
0
       || (addr_mode == CODE_32BIT
15293
0
           ? i.base_reg->reg_type.bitfield.dword
15294
0
           : i.base_reg->reg_type.bitfield.word)))
15295
0
      goto bad_address;
15296
15297
34
    as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
15298
34
       operand_string,
15299
34
       intel_syntax ? '[' : '(',
15300
34
       register_prefix,
15301
34
       expected_reg->reg_name,
15302
34
       intel_syntax ? ']' : ')');
15303
34
    return 1;
15304
34
  }
15305
0
      else
15306
0
  return 1;
15307
15308
2
    bad_address:
15309
2
      as_bad (_("`%s' is not a valid %s expression"),
15310
2
        operand_string, kind);
15311
2
      return 0;
15312
34
    }
15313
8.13k
  else
15314
8.13k
    {
15315
8.13k
      t = current_templates.start;
15316
15317
8.13k
      if (addr_mode != CODE_16BIT)
15318
6.99k
  {
15319
    /* 32-bit/64-bit checks.  */
15320
6.99k
    if (pp.disp_encoding == disp_encoding_16bit)
15321
10
      {
15322
10
      bad_disp:
15323
10
        as_bad (_("invalid `%s' prefix"),
15324
10
          addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
15325
10
        return 0;
15326
10
      }
15327
15328
6.98k
    if ((i.base_reg
15329
1
         && ((addr_mode == CODE_64BIT
15330
1
        ? !i.base_reg->reg_type.bitfield.qword
15331
1
        : !i.base_reg->reg_type.bitfield.dword)
15332
0
       || (i.index_reg && i.base_reg->reg_num == RegIP)
15333
0
       || i.base_reg->reg_num == RegIZ))
15334
6.98k
        || (i.index_reg
15335
1
      && !i.index_reg->reg_type.bitfield.xmmword
15336
1
      && !i.index_reg->reg_type.bitfield.ymmword
15337
1
      && !i.index_reg->reg_type.bitfield.zmmword
15338
1
      && ((addr_mode == CODE_64BIT
15339
1
           ? !i.index_reg->reg_type.bitfield.qword
15340
1
           : !i.index_reg->reg_type.bitfield.dword)
15341
1
          || !i.index_reg->reg_type.bitfield.baseindex)))
15342
2
      goto bad_address;
15343
15344
    /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
15345
6.98k
    if (t->mnem_off == MN_bndmk
15346
6.98k
        || t->mnem_off == MN_bndldx
15347
6.98k
        || t->mnem_off == MN_bndstx
15348
6.98k
        || t->opcode_modifier.sib == SIBMEM)
15349
0
      {
15350
        /* They cannot use RIP-relative addressing. */
15351
0
        if (i.base_reg && i.base_reg->reg_num == RegIP)
15352
0
    {
15353
0
      as_bad (_("`%s' cannot be used here"), operand_string);
15354
0
      return 0;
15355
0
    }
15356
15357
        /* bndldx and bndstx ignore their scale factor. */
15358
0
        if ((t->mnem_off == MN_bndldx || t->mnem_off == MN_bndstx)
15359
0
      && i.log2_scale_factor)
15360
0
    as_warn (_("register scaling is being ignored here"));
15361
0
      }
15362
6.98k
  }
15363
1.13k
      else
15364
1.13k
  {
15365
    /* 16-bit checks.  */
15366
1.13k
    if (pp.disp_encoding == disp_encoding_32bit)
15367
0
      goto bad_disp;
15368
15369
1.13k
    if ((i.base_reg
15370
0
         && (!i.base_reg->reg_type.bitfield.word
15371
0
       || !i.base_reg->reg_type.bitfield.baseindex))
15372
1.13k
        || (i.index_reg
15373
0
      && (!i.index_reg->reg_type.bitfield.word
15374
0
          || !i.index_reg->reg_type.bitfield.baseindex
15375
0
          || !(i.base_reg
15376
0
         && i.base_reg->reg_num < 6
15377
0
         && i.index_reg->reg_num >= 6
15378
0
         && i.log2_scale_factor == 0))))
15379
0
      goto bad_address;
15380
1.13k
  }
15381
8.13k
    }
15382
8.12k
  return 1;
15383
8.16k
}
15384
15385
/* Handle vector immediates.  */
15386
15387
static int
15388
RC_SAE_immediate (const char *imm_start)
15389
14.5k
{
15390
14.5k
  const char *pstr = imm_start;
15391
15392
14.5k
  if (*pstr != '{')
15393
14.5k
    return 0;
15394
15395
6
  pstr++;
15396
6
  if (is_whitespace (*pstr))
15397
0
    pstr++;
15398
15399
6
  pstr = RC_SAE_specifier (pstr);
15400
6
  if (pstr == NULL)
15401
6
    return 0;
15402
15403
0
  if (is_whitespace (*pstr))
15404
0
    pstr++;
15405
15406
0
  if (*pstr++ != '}')
15407
0
    {
15408
0
      as_bad (_("Missing '}': '%s'"), imm_start);
15409
0
      return 0;
15410
0
    }
15411
0
  /* RC/SAE immediate string should contain nothing more.  */;
15412
0
  if (*pstr != 0)
15413
0
    {
15414
0
      as_bad (_("Junk after '}': '%s'"), imm_start);
15415
0
      return 0;
15416
0
    }
15417
15418
  /* Internally this doesn't count as an operand.  */
15419
0
  --i.operands;
15420
15421
0
  return 1;
15422
0
}
15423
15424
static INLINE bool starts_memory_operand (char c)
15425
4.39k
{
15426
4.39k
  return ISDIGIT (c)
15427
3.06k
   || is_name_beginner (c)
15428
54
   || strchr ("([\"+-!~", c);
15429
4.39k
}
15430
15431
/* Parse OPERAND_STRING into the i386_insn structure I.  Returns zero
15432
   on error.  */
15433
15434
static int
15435
i386_att_operand (char *operand_string)
15436
5.98k
{
15437
5.98k
  const reg_entry *r;
15438
5.98k
  char *end_op;
15439
5.98k
  char *op_string = operand_string;
15440
15441
5.98k
  if (is_whitespace (*op_string))
15442
0
    ++op_string;
15443
15444
  /* We check for an absolute prefix (differentiating,
15445
     for example, 'jmp pc_relative_label' from 'jmp *absolute_label'.  */
15446
5.98k
  if (*op_string == ABSOLUTE_PREFIX
15447
18
      && current_templates.start->opcode_modifier.jump)
15448
18
    {
15449
18
      ++op_string;
15450
18
      if (is_whitespace (*op_string))
15451
0
  ++op_string;
15452
18
      i.jumpabsolute = true;
15453
18
    }
15454
15455
  /* Check if operand is a register.  */
15456
5.98k
  if ((r = parse_register (op_string, &end_op)) != NULL)
15457
108
    {
15458
108
      i386_operand_type temp;
15459
15460
108
      if (r == &bad_reg)
15461
0
  return 0;
15462
15463
      /* Check for a segment override by searching for ':' after a
15464
   segment register.  */
15465
108
      op_string = end_op;
15466
108
      if (is_whitespace (*op_string))
15467
0
  ++op_string;
15468
108
      if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
15469
5
  {
15470
5
    i.seg[i.mem_operands] = r;
15471
15472
    /* Skip the ':' and whitespace.  */
15473
5
    ++op_string;
15474
5
    if (is_whitespace (*op_string))
15475
0
      ++op_string;
15476
15477
    /* Handle case of %es:*foo.  */
15478
5
    if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX
15479
0
        && current_templates.start->opcode_modifier.jump)
15480
0
      {
15481
0
        ++op_string;
15482
0
        if (is_whitespace (*op_string))
15483
0
    ++op_string;
15484
0
        i.jumpabsolute = true;
15485
0
      }
15486
15487
5
    if (!starts_memory_operand (*op_string))
15488
0
      {
15489
0
        as_bad (_("bad memory operand `%s'"), op_string);
15490
0
        return 0;
15491
0
      }
15492
5
    goto do_memory_reference;
15493
5
  }
15494
15495
      /* Handle vector operations.  */
15496
103
      if (*op_string == '{')
15497
16
  {
15498
16
    op_string = check_VecOperations (op_string);
15499
16
    if (op_string == NULL)
15500
16
      return 0;
15501
16
  }
15502
15503
87
      if (*op_string)
15504
28
  {
15505
28
    as_bad (_("junk `%s' after register"), op_string);
15506
28
    return 0;
15507
28
  }
15508
15509
       /* Reject pseudo registers for .insn.  */
15510
59
      if (dot_insn () && r->reg_type.bitfield.class == ClassNone)
15511
0
  {
15512
0
    as_bad (_("`%s%s' cannot be used here"),
15513
0
      register_prefix, r->reg_name);
15514
0
    return 0;
15515
0
  }
15516
15517
59
      temp = r->reg_type;
15518
59
      temp.bitfield.baseindex = 0;
15519
59
      i.types[this_operand] = operand_type_or (i.types[this_operand],
15520
59
                 temp);
15521
59
      i.types[this_operand].bitfield.unspecified = 0;
15522
59
      i.op[this_operand].regs = r;
15523
59
      i.reg_operands++;
15524
15525
      /* A GPR may follow an RC or SAE immediate only if a (vector) register
15526
         operand was also present earlier on.  */
15527
59
      if (i.rounding.type != rc_none && temp.bitfield.class == Reg
15528
0
          && i.reg_operands == 1)
15529
0
  {
15530
0
    unsigned int j;
15531
15532
0
    for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
15533
0
      if (i.rounding.type == RC_NamesTable[j].type)
15534
0
        break;
15535
0
    as_bad (_("`%s': misplaced `{%s}'"),
15536
0
      insn_name (current_templates.start), RC_NamesTable[j].name);
15537
0
    return 0;
15538
0
  }
15539
59
    }
15540
5.87k
  else if (*op_string == REGISTER_PREFIX)
15541
5
    {
15542
5
      as_bad (_("bad register name `%s'"), op_string);
15543
5
      return 0;
15544
5
    }
15545
5.86k
  else if (*op_string == IMMEDIATE_PREFIX)
15546
1.47k
    {
15547
1.47k
      ++op_string;
15548
1.47k
      if (i.jumpabsolute)
15549
0
  {
15550
0
    as_bad (_("immediate operand illegal with absolute jump"));
15551
0
    return 0;
15552
0
  }
15553
1.47k
      if (!i386_immediate (op_string))
15554
17
  return 0;
15555
1.45k
      if (i.rounding.type != rc_none)
15556
0
  {
15557
0
    as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
15558
0
      insn_name (current_templates.start));
15559
0
    return 0;
15560
0
  }
15561
1.45k
    }
15562
4.39k
  else if (RC_SAE_immediate (operand_string))
15563
0
    {
15564
      /* If it is a RC or SAE immediate, do the necessary placement check:
15565
   Only another immediate or a GPR may precede it.  */
15566
0
      if (i.mem_operands || i.reg_operands + i.imm_operands > 1
15567
0
    || (i.reg_operands == 1
15568
0
        && i.op[0].regs->reg_type.bitfield.class != Reg))
15569
0
  {
15570
0
    as_bad (_("`%s': misplaced `%s'"),
15571
0
      insn_name (current_templates.start), operand_string);
15572
0
    return 0;
15573
0
  }
15574
0
    }
15575
4.39k
  else if (starts_memory_operand (*op_string))
15576
4.38k
    {
15577
      /* This is a memory reference of some sort.  */
15578
4.38k
      char *base_string;
15579
15580
      /* Start and end of displacement string expression (if found).  */
15581
4.38k
      char *displacement_string_start;
15582
4.38k
      char *displacement_string_end;
15583
15584
4.39k
    do_memory_reference:
15585
      /* Check for base index form.  We detect the base index form by
15586
   looking for an ')' at the end of the operand, searching
15587
   for the '(' matching it, and finding a REGISTER_PREFIX or ','
15588
   after the '('.  */
15589
4.39k
      base_string = op_string + strlen (op_string);
15590
15591
      /* Handle vector operations.  */
15592
4.39k
      --base_string;
15593
4.39k
      if (is_whitespace (*base_string))
15594
8
  --base_string;
15595
15596
4.39k
      if (*base_string == '}')
15597
11
  {
15598
11
    char *vop_start = NULL;
15599
15600
1.01k
    while (base_string-- > op_string)
15601
1.00k
      {
15602
1.00k
        if (*base_string == '"')
15603
0
    break;
15604
1.00k
        if (*base_string != '{')
15605
1.00k
    continue;
15606
15607
0
        vop_start = base_string;
15608
15609
0
        --base_string;
15610
0
        if (is_whitespace (*base_string))
15611
0
    --base_string;
15612
15613
0
        if (*base_string != '}')
15614
0
    break;
15615
15616
0
        vop_start = NULL;
15617
0
      }
15618
15619
11
    if (!vop_start)
15620
11
      {
15621
11
        as_bad (_("unbalanced figure braces"));
15622
11
        return 0;
15623
11
      }
15624
15625
0
    if (check_VecOperations (vop_start) == NULL)
15626
0
      return 0;
15627
0
  }
15628
15629
      /* If we only have a displacement, set-up for it to be parsed later.  */
15630
4.38k
      displacement_string_start = op_string;
15631
4.38k
      displacement_string_end = base_string + 1;
15632
15633
4.38k
      if (*base_string == ')')
15634
83
  {
15635
83
    char *temp_string;
15636
83
    unsigned int parens_not_balanced = 0;
15637
83
    bool in_quotes = false;
15638
15639
    /* We've already checked that the number of left & right ()'s are
15640
       equal, and that there's a matching set of double quotes.  */
15641
83
    end_op = base_string;
15642
1.68k
    for (temp_string = op_string; temp_string < end_op; temp_string++)
15643
1.60k
      {
15644
1.60k
        if (*temp_string == '\\' && temp_string[1] == '"')
15645
0
    ++temp_string;
15646
1.60k
        else if (*temp_string == '"')
15647
26
    in_quotes = !in_quotes;
15648
1.57k
        else if (!in_quotes)
15649
1.52k
    {
15650
1.52k
      if (*temp_string == '(' && !parens_not_balanced++)
15651
83
        base_string = temp_string;
15652
1.52k
      if (*temp_string == ')')
15653
0
        --parens_not_balanced;
15654
1.52k
    }
15655
1.60k
      }
15656
15657
83
    temp_string = base_string;
15658
15659
    /* Skip past '(' and whitespace.  */
15660
83
    gas_assert (*base_string == '(');
15661
83
    ++base_string;
15662
83
    if (is_whitespace (*base_string))
15663
65
      ++base_string;
15664
15665
83
    if (*base_string == ','
15666
70
        || ((i.base_reg = parse_register (base_string, &end_op))
15667
70
      != NULL))
15668
13
      {
15669
13
        displacement_string_end = temp_string;
15670
15671
13
        i.types[this_operand].bitfield.baseindex = 1;
15672
15673
13
        if (i.base_reg)
15674
0
    {
15675
0
      if (i.base_reg == &bad_reg)
15676
0
        return 0;
15677
0
      base_string = end_op;
15678
0
      if (is_whitespace (*base_string))
15679
0
        ++base_string;
15680
0
    }
15681
15682
        /* There may be an index reg or scale factor here.  */
15683
13
        if (*base_string == ',')
15684
13
    {
15685
13
      ++base_string;
15686
13
      if (is_whitespace (*base_string))
15687
0
        ++base_string;
15688
15689
13
      if ((i.index_reg = parse_register (base_string, &end_op))
15690
13
          != NULL)
15691
0
        {
15692
0
          if (i.index_reg == &bad_reg)
15693
0
      return 0;
15694
0
          base_string = end_op;
15695
0
          if (is_whitespace (*base_string))
15696
0
      ++base_string;
15697
0
          if (*base_string == ',')
15698
0
      {
15699
0
        ++base_string;
15700
0
        if (is_whitespace (*base_string))
15701
0
          ++base_string;
15702
0
      }
15703
0
          else if (*base_string != ')')
15704
0
      {
15705
0
        as_bad (_("expecting `,' or `)' "
15706
0
            "after index register in `%s'"),
15707
0
          operand_string);
15708
0
        return 0;
15709
0
      }
15710
0
        }
15711
13
      else if (*base_string == REGISTER_PREFIX)
15712
13
        {
15713
13
          end_op = strchr (base_string, ',');
15714
13
          if (end_op)
15715
13
      *end_op = '\0';
15716
13
          as_bad (_("bad register name `%s'"), base_string);
15717
13
          return 0;
15718
13
        }
15719
15720
      /* Check for scale factor.  */
15721
0
      if (*base_string != ')')
15722
0
        {
15723
0
          char *end_scale = i386_scale (base_string);
15724
15725
0
          if (!end_scale)
15726
0
      return 0;
15727
15728
0
          base_string = end_scale;
15729
0
          if (is_whitespace (*base_string))
15730
0
      ++base_string;
15731
0
          if (*base_string != ')')
15732
0
      {
15733
0
        as_bad (_("expecting `)' "
15734
0
            "after scale factor in `%s'"),
15735
0
          operand_string);
15736
0
        return 0;
15737
0
      }
15738
0
        }
15739
0
      else if (!i.index_reg)
15740
0
        {
15741
0
          as_bad (_("expecting index register or scale factor "
15742
0
        "after `,'; got '%c'"),
15743
0
            *base_string);
15744
0
          return 0;
15745
0
        }
15746
0
    }
15747
0
        else if (*base_string != ')')
15748
0
    {
15749
0
      as_bad (_("expecting `,' or `)' "
15750
0
          "after base register in `%s'"),
15751
0
        operand_string);
15752
0
      return 0;
15753
0
    }
15754
13
      }
15755
70
    else if (*base_string == REGISTER_PREFIX)
15756
0
      {
15757
0
        end_op = strchr (base_string, ',');
15758
0
        if (end_op)
15759
0
    *end_op = '\0';
15760
0
        as_bad (_("bad register name `%s'"), base_string);
15761
0
        return 0;
15762
0
      }
15763
83
  }
15764
15765
      /* If there's an expression beginning the operand, parse it,
15766
   assuming displacement_string_start and
15767
   displacement_string_end are meaningful.  */
15768
4.37k
      if (displacement_string_start != displacement_string_end)
15769
4.36k
  {
15770
4.36k
    if (!i386_displacement (displacement_string_start,
15771
4.36k
          displacement_string_end))
15772
4
      return 0;
15773
4.36k
  }
15774
15775
      /* Special case for (%dx) while doing input/output op.  */
15776
4.36k
      if (i.base_reg
15777
0
    && i.base_reg->reg_type.bitfield.instance == RegD
15778
0
    && i.base_reg->reg_type.bitfield.word
15779
0
    && i.index_reg == 0
15780
0
    && i.log2_scale_factor == 0
15781
0
    && i.seg[i.mem_operands] == 0
15782
0
    && !operand_type_check (i.types[this_operand], disp))
15783
0
  {
15784
0
    i.types[this_operand] = i.base_reg->reg_type;
15785
0
    i.op[this_operand].regs = i.base_reg;
15786
0
    i.base_reg = NULL;
15787
0
    i.input_output_operand = true;
15788
0
    return 1;
15789
0
  }
15790
15791
4.36k
      if (i386_index_check (operand_string) == 0)
15792
10
  return 0;
15793
4.35k
      i.flags[this_operand] |= Operand_Mem;
15794
4.35k
      i.mem_operands++;
15795
4.35k
    }
15796
5
  else
15797
5
    {
15798
      /* It's not a memory operand; argh!  */
15799
5
      as_bad (_("invalid char %s beginning operand %d `%s'"),
15800
5
        output_invalid (*op_string),
15801
5
        this_operand + 1,
15802
5
        op_string);
15803
5
      return 0;
15804
5
    }
15805
5.87k
  return 1;     /* Normal return.  */
15806
5.98k
}
15807

15808
/* Initialize the tc_frag_data field of a fragment.  */
15809
15810
void i386_frag_init (fragS *fragP, size_t max_bytes)
15811
9.64k
{
15812
9.64k
  memset (&fragP->tc_frag_data, 0, sizeof (fragP->tc_frag_data));
15813
9.64k
  fragP->tc_frag_data.isa = cpu_arch_isa;
15814
9.64k
  fragP->tc_frag_data.tune = cpu_arch_tune;
15815
9.64k
  fragP->tc_frag_data.cpunop = cpu_arch_flags.bitfield.cpunop;
15816
9.64k
  fragP->tc_frag_data.isanop = cpu_arch_isa_flags.bitfield.cpunop;
15817
9.64k
  fragP->tc_frag_data.code = i386_flag_code;
15818
9.64k
  fragP->tc_frag_data.max_bytes = max_bytes;
15819
9.64k
  fragP->tc_frag_data.last_insn_normal
15820
9.64k
    = (seg_info(now_seg)->tc_segment_info_data.last_insn.kind
15821
9.64k
       == last_insn_other);
15822
9.64k
  fragP->tc_frag_data.no_cond_jump_promotion = no_cond_jump_promotion;
15823
9.64k
}
15824
15825
/* Calculate the maximum variable size (i.e., excluding fr_fix)
15826
   that an rs_machine_dependent frag may reach.  */
15827
15828
unsigned int
15829
i386_frag_max_var (fragS *frag)
15830
3
{
15831
  /* The only relaxable frags are for jumps.
15832
     Unconditional jumps can grow by 4 bytes and others by 5 bytes.  */
15833
3
  gas_assert (frag->fr_type == rs_machine_dependent);
15834
3
  return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
15835
3
}
15836
15837
#ifdef OBJ_ELF
15838
static int
15839
elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
15840
0
{
15841
  /* STT_GNU_IFUNC symbol must go through PLT.  */
15842
0
  if ((symbol_get_bfdsym (fr_symbol)->flags
15843
0
       & BSF_GNU_INDIRECT_FUNCTION) != 0)
15844
0
    return 0;
15845
15846
0
  if (!S_IS_EXTERNAL (fr_symbol))
15847
    /* Symbol may be weak or local.  */
15848
0
    return !S_IS_WEAK (fr_symbol);
15849
15850
  /* Global symbols with non-default visibility can't be preempted. */
15851
0
  if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
15852
0
    return 1;
15853
15854
0
  if (fr_var != NO_RELOC)
15855
0
    switch ((enum bfd_reloc_code_real) fr_var)
15856
0
      {
15857
0
      case BFD_RELOC_386_PLT32:
15858
0
      case BFD_RELOC_32_PLT_PCREL:
15859
  /* Symbol with PLT relocation may be preempted. */
15860
0
  return 0;
15861
0
      default:
15862
0
  abort ();
15863
0
      }
15864
15865
  /* Global symbols with default visibility in a shared library may be
15866
     preempted by another definition.  */
15867
0
  return !shared;
15868
0
}
15869
#endif
15870
15871
/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
15872
   Note also work for Skylake and Cascadelake.
15873
---------------------------------------------------------------------
15874
|   JCC   | ADD/SUB/CMP | INC/DEC | TEST/AND |
15875
| ------  | ----------- | ------- | -------- |
15876
|   Jo    |      N      |    N    |     Y    |
15877
|   Jno   |      N      |    N    |     Y    |
15878
|  Jc/Jb  |      Y      |    N    |     Y    |
15879
| Jae/Jnb |      Y      |    N    |     Y    |
15880
|  Je/Jz  |      Y      |    Y    |     Y    |
15881
| Jne/Jnz |      Y      |    Y    |     Y    |
15882
| Jna/Jbe |      Y      |    N    |     Y    |
15883
| Ja/Jnbe |      Y      |    N    |     Y    |
15884
|   Js    |      N      |    N    |     Y    |
15885
|   Jns   |      N      |    N    |     Y    |
15886
|  Jp/Jpe |      N      |    N    |     Y    |
15887
| Jnp/Jpo |      N      |    N    |     Y    |
15888
| Jl/Jnge |      Y      |    Y    |     Y    |
15889
| Jge/Jnl |      Y      |    Y    |     Y    |
15890
| Jle/Jng |      Y      |    Y    |     Y    |
15891
| Jg/Jnle |      Y      |    Y    |     Y    |
15892
---------------------------------------------------------------------  */
15893
static int
15894
i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
15895
0
{
15896
0
  if (mf_cmp == mf_cmp_alu_cmp)
15897
0
    return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
15898
0
      || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
15899
0
  if (mf_cmp == mf_cmp_incdec)
15900
0
    return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
15901
0
      || mf_jcc == mf_jcc_jle);
15902
0
  if (mf_cmp == mf_cmp_test_and)
15903
0
    return 1;
15904
0
  return 0;
15905
0
}
15906
15907
/* Return the next non-empty frag.  */
15908
15909
static fragS *
15910
i386_next_non_empty_frag (fragS *fragP)
15911
0
{
15912
  /* There may be a frag with a ".fill 0" when there is no room in
15913
     the current frag for frag_grow in output_insn.  */
15914
0
  for (fragP = fragP->fr_next;
15915
0
       (fragP != NULL
15916
0
  && fragP->fr_type == rs_fill
15917
0
  && fragP->fr_fix == 0);
15918
0
       fragP = fragP->fr_next)
15919
0
    ;
15920
0
  return fragP;
15921
0
}
15922
15923
/* Return the next jcc frag after BRANCH_PADDING.  */
15924
15925
static fragS *
15926
i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
15927
0
{
15928
0
  fragS *branch_fragP;
15929
0
  if (!pad_fragP)
15930
0
    return NULL;
15931
15932
0
  if (pad_fragP->fr_type == rs_machine_dependent
15933
0
      && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
15934
0
    == BRANCH_PADDING))
15935
0
    {
15936
0
      branch_fragP = i386_next_non_empty_frag (pad_fragP);
15937
0
      if (branch_fragP->fr_type != rs_machine_dependent)
15938
0
  return NULL;
15939
0
      if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
15940
0
    && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
15941
0
           pad_fragP->tc_frag_data.mf_type))
15942
0
  return branch_fragP;
15943
0
    }
15944
15945
0
  return NULL;
15946
0
}
15947
15948
/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags.  */
15949
15950
static void
15951
i386_classify_machine_dependent_frag (fragS *fragP)
15952
0
{
15953
0
  fragS *cmp_fragP;
15954
0
  fragS *pad_fragP;
15955
0
  fragS *branch_fragP;
15956
0
  fragS *next_fragP;
15957
0
  unsigned int max_prefix_length;
15958
15959
0
  if (fragP->tc_frag_data.classified)
15960
0
    return;
15961
15962
  /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING.  Convert
15963
     FUSED_JCC_PADDING and merge BRANCH_PADDING.  */
15964
0
  for (next_fragP = fragP;
15965
0
       next_fragP != NULL;
15966
0
       next_fragP = next_fragP->fr_next)
15967
0
    {
15968
0
      next_fragP->tc_frag_data.classified = 1;
15969
0
      if (next_fragP->fr_type == rs_machine_dependent)
15970
0
  switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
15971
0
    {
15972
0
    case BRANCH_PADDING:
15973
      /* The BRANCH_PADDING frag must be followed by a branch
15974
         frag.  */
15975
0
      branch_fragP = i386_next_non_empty_frag (next_fragP);
15976
0
      next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
15977
0
      break;
15978
0
    case FUSED_JCC_PADDING:
15979
      /* Check if this is a fused jcc:
15980
         FUSED_JCC_PADDING
15981
         CMP like instruction
15982
         BRANCH_PADDING
15983
         COND_JUMP
15984
         */
15985
0
      cmp_fragP = i386_next_non_empty_frag (next_fragP);
15986
0
      pad_fragP = i386_next_non_empty_frag (cmp_fragP);
15987
0
      branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
15988
0
      if (branch_fragP)
15989
0
        {
15990
    /* The BRANCH_PADDING frag is merged with the
15991
       FUSED_JCC_PADDING frag.  */
15992
0
    next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
15993
    /* CMP like instruction size.  */
15994
0
    next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
15995
0
    frag_wane (pad_fragP);
15996
    /* Skip to branch_fragP.  */
15997
0
    next_fragP = branch_fragP;
15998
0
        }
15999
0
      else if (next_fragP->tc_frag_data.max_prefix_length)
16000
0
        {
16001
    /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
16002
       a fused jcc.  */
16003
0
    next_fragP->fr_subtype
16004
0
      = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
16005
0
    next_fragP->tc_frag_data.max_bytes
16006
0
      = next_fragP->tc_frag_data.max_prefix_length;
16007
    /* This will be updated in the BRANCH_PREFIX scan.  */
16008
0
    next_fragP->tc_frag_data.max_prefix_length = 0;
16009
0
        }
16010
0
      else
16011
0
        frag_wane (next_fragP);
16012
0
      break;
16013
0
    }
16014
0
    }
16015
16016
  /* Stop if there is no BRANCH_PREFIX.  */
16017
0
  if (!align_branch_prefix_size)
16018
0
    return;
16019
16020
  /* Scan for BRANCH_PREFIX.  */
16021
0
  for (; fragP != NULL; fragP = fragP->fr_next)
16022
0
    {
16023
0
      if (fragP->fr_type != rs_machine_dependent
16024
0
    || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
16025
0
        != BRANCH_PREFIX))
16026
0
  continue;
16027
16028
      /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
16029
   COND_JUMP_PREFIX.  */
16030
0
      max_prefix_length = 0;
16031
0
      for (next_fragP = fragP;
16032
0
     next_fragP != NULL;
16033
0
     next_fragP = next_fragP->fr_next)
16034
0
  {
16035
0
    if (next_fragP->fr_type == rs_fill)
16036
      /* Skip rs_fill frags.  */
16037
0
      continue;
16038
0
    else if (next_fragP->fr_type != rs_machine_dependent)
16039
      /* Stop for all other frags.  */
16040
0
      break;
16041
16042
    /* rs_machine_dependent frags.  */
16043
0
    if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
16044
0
        == BRANCH_PREFIX)
16045
0
      {
16046
        /* Count BRANCH_PREFIX frags.  */
16047
0
        if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
16048
0
    {
16049
0
      max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
16050
0
      frag_wane (next_fragP);
16051
0
    }
16052
0
        else
16053
0
    max_prefix_length
16054
0
      += next_fragP->tc_frag_data.max_bytes;
16055
0
      }
16056
0
    else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
16057
0
        == BRANCH_PADDING)
16058
0
       || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
16059
0
           == FUSED_JCC_PADDING))
16060
0
      {
16061
        /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING.  */
16062
0
        fragP->tc_frag_data.u.padding_fragP = next_fragP;
16063
0
        break;
16064
0
      }
16065
0
    else
16066
      /* Stop for other rs_machine_dependent frags.  */
16067
0
      break;
16068
0
  }
16069
16070
0
      fragP->tc_frag_data.max_prefix_length = max_prefix_length;
16071
16072
      /* Skip to the next frag.  */
16073
0
      fragP = next_fragP;
16074
0
    }
16075
0
}
16076
16077
/* Compute padding size for
16078
16079
  FUSED_JCC_PADDING
16080
  CMP like instruction
16081
  BRANCH_PADDING
16082
  COND_JUMP/UNCOND_JUMP
16083
16084
   or
16085
16086
  BRANCH_PADDING
16087
  COND_JUMP/UNCOND_JUMP
16088
 */
16089
16090
static int
16091
i386_branch_padding_size (fragS *fragP, offsetT address)
16092
0
{
16093
0
  unsigned int offset, size, padding_size;
16094
0
  fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
16095
16096
  /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag.  */
16097
0
  if (!address)
16098
0
    address = fragP->fr_address;
16099
0
  address += fragP->fr_fix;
16100
16101
  /* CMP like instrunction size.  */
16102
0
  size = fragP->tc_frag_data.cmp_size;
16103
16104
  /* The base size of the branch frag.  */
16105
0
  size += branch_fragP->fr_fix;
16106
16107
  /* Add opcode and displacement bytes for the rs_machine_dependent
16108
     branch frag.  */
16109
0
  if (branch_fragP->fr_type == rs_machine_dependent)
16110
0
    size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
16111
16112
  /* Check if branch is within boundary and doesn't end at the last
16113
     byte.  */
16114
0
  offset = address & ((1U << align_branch_power) - 1);
16115
0
  if ((offset + size) >= (1U << align_branch_power))
16116
    /* Padding needed to avoid crossing boundary.  */
16117
0
    padding_size = (1U << align_branch_power) - offset;
16118
0
  else
16119
    /* No padding needed.  */
16120
0
    padding_size = 0;
16121
16122
  /* The return value may be saved in tc_frag_data.length which is
16123
     unsigned byte.  */
16124
0
  if (!fits_in_unsigned_byte (padding_size))
16125
0
    abort ();
16126
16127
0
  return padding_size;
16128
0
}
16129
16130
/* i386_generic_table_relax_frag()
16131
16132
   Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
16133
   grow/shrink padding to align branch frags.  Hand others to
16134
   relax_frag().  */
16135
16136
long
16137
i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
16138
0
{
16139
0
  if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
16140
0
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
16141
0
    {
16142
0
      long padding_size = i386_branch_padding_size (fragP, 0);
16143
0
      long grow = padding_size - fragP->tc_frag_data.length;
16144
16145
      /* When the BRANCH_PREFIX frag is used, the computed address
16146
         must match the actual address and there should be no padding.  */
16147
0
      if (fragP->tc_frag_data.padding_address
16148
0
    && (fragP->tc_frag_data.padding_address != fragP->fr_address
16149
0
        || padding_size))
16150
0
  abort ();
16151
16152
      /* Update the padding size.  */
16153
0
      if (grow)
16154
0
  fragP->tc_frag_data.length = padding_size;
16155
16156
0
      return grow;
16157
0
    }
16158
0
  else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
16159
0
    {
16160
0
      fragS *padding_fragP, *next_fragP;
16161
0
      long padding_size, left_size, last_size;
16162
16163
0
      padding_fragP = fragP->tc_frag_data.u.padding_fragP;
16164
0
      if (!padding_fragP)
16165
  /* Use the padding set by the leading BRANCH_PREFIX frag.  */
16166
0
  return (fragP->tc_frag_data.length
16167
0
    - fragP->tc_frag_data.last_length);
16168
16169
      /* Compute the relative address of the padding frag in the very
16170
        first time where the BRANCH_PREFIX frag sizes are zero.  */
16171
0
      if (!fragP->tc_frag_data.padding_address)
16172
0
  fragP->tc_frag_data.padding_address
16173
0
    = padding_fragP->fr_address - (fragP->fr_address - stretch);
16174
16175
      /* First update the last length from the previous interation.  */
16176
0
      left_size = fragP->tc_frag_data.prefix_length;
16177
0
      for (next_fragP = fragP;
16178
0
     next_fragP != padding_fragP;
16179
0
     next_fragP = next_fragP->fr_next)
16180
0
  if (next_fragP->fr_type == rs_machine_dependent
16181
0
      && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
16182
0
    == BRANCH_PREFIX))
16183
0
    {
16184
0
      if (left_size)
16185
0
        {
16186
0
    int max = next_fragP->tc_frag_data.max_bytes;
16187
0
    if (max)
16188
0
      {
16189
0
        int size;
16190
0
        if (max > left_size)
16191
0
          size = left_size;
16192
0
        else
16193
0
          size = max;
16194
0
        left_size -= size;
16195
0
        next_fragP->tc_frag_data.last_length = size;
16196
0
      }
16197
0
        }
16198
0
      else
16199
0
        next_fragP->tc_frag_data.last_length = 0;
16200
0
    }
16201
16202
      /* Check the padding size for the padding frag.  */
16203
0
      padding_size = i386_branch_padding_size
16204
0
  (padding_fragP, (fragP->fr_address
16205
0
       + fragP->tc_frag_data.padding_address));
16206
16207
0
      last_size = fragP->tc_frag_data.prefix_length;
16208
      /* Check if there is change from the last interation.  */
16209
0
      if (padding_size == last_size)
16210
0
  {
16211
    /* Update the expected address of the padding frag.  */
16212
0
    padding_fragP->tc_frag_data.padding_address
16213
0
      = (fragP->fr_address + padding_size
16214
0
         + fragP->tc_frag_data.padding_address);
16215
0
    return 0;
16216
0
  }
16217
16218
0
      if (padding_size > fragP->tc_frag_data.max_prefix_length)
16219
0
  {
16220
    /* No padding if there is no sufficient room.  Clear the
16221
       expected address of the padding frag.  */
16222
0
    padding_fragP->tc_frag_data.padding_address = 0;
16223
0
    padding_size = 0;
16224
0
  }
16225
0
      else
16226
  /* Store the expected address of the padding frag.  */
16227
0
  padding_fragP->tc_frag_data.padding_address
16228
0
    = (fragP->fr_address + padding_size
16229
0
       + fragP->tc_frag_data.padding_address);
16230
16231
0
      fragP->tc_frag_data.prefix_length = padding_size;
16232
16233
      /* Update the length for the current interation.  */
16234
0
      left_size = padding_size;
16235
0
      for (next_fragP = fragP;
16236
0
     next_fragP != padding_fragP;
16237
0
     next_fragP = next_fragP->fr_next)
16238
0
  if (next_fragP->fr_type == rs_machine_dependent
16239
0
      && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
16240
0
    == BRANCH_PREFIX))
16241
0
    {
16242
0
      if (left_size)
16243
0
        {
16244
0
    int max = next_fragP->tc_frag_data.max_bytes;
16245
0
    if (max)
16246
0
      {
16247
0
        int size;
16248
0
        if (max > left_size)
16249
0
          size = left_size;
16250
0
        else
16251
0
          size = max;
16252
0
        left_size -= size;
16253
0
        next_fragP->tc_frag_data.length = size;
16254
0
      }
16255
0
        }
16256
0
      else
16257
0
        next_fragP->tc_frag_data.length = 0;
16258
0
    }
16259
16260
0
      return (fragP->tc_frag_data.length
16261
0
        - fragP->tc_frag_data.last_length);
16262
0
    }
16263
0
  return relax_frag (segment, fragP, stretch);
16264
0
}
16265
16266
/* md_estimate_size_before_relax()
16267
16268
   Called just before relax() for rs_machine_dependent frags.  The x86
16269
   assembler uses these frags to handle variable size jump
16270
   instructions.
16271
16272
   Any symbol that is now undefined will not become defined.
16273
   Return the correct fr_subtype in the frag.
16274
   Return the initial "guess for variable size of frag" to caller.
16275
   The guess is actually the growth beyond the fixed part.  Whatever
16276
   we do to grow the fixed or variable part contributes to our
16277
   returned value.  */
16278
16279
int
16280
md_estimate_size_before_relax (fragS *fragP, segT segment)
16281
0
{
16282
0
  if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
16283
0
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
16284
0
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
16285
0
    {
16286
0
      i386_classify_machine_dependent_frag (fragP);
16287
0
      return fragP->tc_frag_data.length;
16288
0
    }
16289
16290
  /* We've already got fragP->fr_subtype right;  all we have to do is
16291
     check for un-relaxable symbols.  On an ELF system, we can't relax
16292
     an externally visible symbol, because it may be overridden by a
16293
     shared library.  */
16294
0
  if (S_GET_SEGMENT (fragP->fr_symbol) != segment
16295
0
#ifdef OBJ_ELF
16296
0
      || !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
16297
0
              fragP->fr_var)
16298
0
#endif
16299
#if defined (OBJ_COFF) && defined (TE_PE)
16300
      || S_IS_WEAK (fragP->fr_symbol)
16301
#endif
16302
0
      )
16303
0
    {
16304
      /* Symbol is undefined in this segment, or we need to keep a
16305
   reloc so that weak symbols can be overridden.  */
16306
0
      int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
16307
0
      enum bfd_reloc_code_real reloc_type;
16308
0
      unsigned char *opcode;
16309
0
      int old_fr_fix;
16310
0
      fixS *fixP = NULL;
16311
16312
0
      reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
16313
0
#ifdef OBJ_ELF
16314
0
      if (reloc_type == NO_RELOC
16315
0
    && size != 2
16316
0
    && fragP->tc_frag_data.code == CODE_64BIT
16317
0
    && fragP->fr_offset == 0
16318
0
    && need_plt32_p (fragP->fr_symbol))
16319
0
  reloc_type = BFD_RELOC_32_PLT_PCREL;
16320
0
#endif
16321
16322
0
      old_fr_fix = fragP->fr_fix;
16323
0
      opcode = (unsigned char *) fragP->fr_opcode;
16324
16325
0
      switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
16326
0
  {
16327
0
  case UNCOND_JUMP:
16328
    /* Make jmp (0xeb) a (d)word displacement jump.  */
16329
0
    opcode[0] = 0xe9;
16330
0
    fragP->fr_fix += size;
16331
0
    fixP = fix_new (fragP, old_fr_fix, size,
16332
0
        fragP->fr_symbol,
16333
0
        fragP->fr_offset, 1,
16334
0
        _reloc (size, 1, 1, reloc_type,
16335
0
          fragP->tc_frag_data.code == CODE_64BIT,
16336
0
          fragP->fr_file, fragP->fr_line));
16337
0
    break;
16338
16339
0
  case COND_JUMP86:
16340
0
    if (fragP->tc_frag_data.no_cond_jump_promotion
16341
0
        && fragP->fr_var == NO_RELOC)
16342
0
      {
16343
0
        fragP->fr_fix += 1;
16344
0
        fixP = fix_new (fragP, old_fr_fix, 1,
16345
0
            fragP->fr_symbol,
16346
0
            fragP->fr_offset, 1,
16347
0
            BFD_RELOC_8_PCREL);
16348
0
        fixP->fx_signed = 1;
16349
0
        break;
16350
0
      }
16351
16352
0
    if (size == 2)
16353
0
      {
16354
        /* Negate the condition, and branch past an
16355
     unconditional jump.  */
16356
0
        opcode[0] ^= 1;
16357
0
        opcode[1] = 3;
16358
        /* Insert an unconditional jump.  */
16359
0
        opcode[2] = 0xe9;
16360
        /* We added two extra opcode bytes, and have a two byte
16361
     offset.  */
16362
0
        fragP->fr_fix += 2 + 2;
16363
0
        fix_new (fragP, old_fr_fix + 2, 2,
16364
0
           fragP->fr_symbol,
16365
0
           fragP->fr_offset, 1,
16366
0
           _reloc (size, 1, 1, reloc_type,
16367
0
             fragP->tc_frag_data.code == CODE_64BIT,
16368
0
             fragP->fr_file, fragP->fr_line));
16369
0
        break;
16370
0
      }
16371
    /* Fall through.  */
16372
16373
0
  case COND_JUMP:
16374
    /* This changes the byte-displacement jump 0x7N
16375
       to the (d)word-displacement jump 0x0f,0x8N.  */
16376
0
    opcode[1] = opcode[0] + 0x10;
16377
0
    opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
16378
    /* We've added an opcode byte.  */
16379
0
    fragP->fr_fix += 1 + size;
16380
0
    fixP = fix_new (fragP, old_fr_fix + 1, size,
16381
0
        fragP->fr_symbol,
16382
0
        fragP->fr_offset, 1,
16383
0
        _reloc (size, 1, 1, reloc_type,
16384
0
          fragP->tc_frag_data.code == CODE_64BIT,
16385
0
          fragP->fr_file, fragP->fr_line));
16386
0
    break;
16387
16388
0
  default:
16389
0
    BAD_CASE (fragP->fr_subtype);
16390
0
    break;
16391
0
  }
16392
16393
      /* All jumps handled here are signed, but don't unconditionally use a
16394
   signed limit check for 32 and 16 bit jumps as we want to allow wrap
16395
   around at 4G (outside of 64-bit mode) and 64k.  */
16396
0
      if (size == 4 && flag_code == CODE_64BIT)
16397
0
  fixP->fx_signed = 1;
16398
16399
0
      frag_wane (fragP);
16400
0
      return fragP->fr_fix - old_fr_fix;
16401
0
    }
16402
16403
  /* Guess size depending on current relax state.  Initially the relax
16404
     state will correspond to a short jump and we return 1, because
16405
     the variable part of the frag (the branch offset) is one byte
16406
     long.  However, we can relax a section more than once and in that
16407
     case we must either set fr_subtype back to the unrelaxed state,
16408
     or return the value for the appropriate branch.  */
16409
0
  return md_relax_table[fragP->fr_subtype].rlx_length;
16410
0
}
16411
16412
/* Called after relax() is finished.
16413
16414
   In:  Address of frag.
16415
  fr_type == rs_machine_dependent.
16416
  fr_subtype is what the address relaxed to.
16417
16418
   Out: Any fixSs and constants are set up.
16419
  Caller will turn frag into a ".space 0".  */
16420
16421
void
16422
md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
16423
                 fragS *fragP)
16424
0
{
16425
0
  unsigned char *opcode;
16426
0
  unsigned char *where_to_put_displacement = NULL;
16427
0
  offsetT target_address;
16428
0
  offsetT opcode_address;
16429
0
  unsigned int extension = 0;
16430
0
  offsetT displacement_from_opcode_start;
16431
16432
0
  if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
16433
0
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
16434
0
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
16435
0
    {
16436
      /* Generate nop padding.  */
16437
0
      unsigned int size = fragP->tc_frag_data.length;
16438
0
      if (size)
16439
0
  {
16440
0
    if (size > fragP->tc_frag_data.max_bytes)
16441
0
      abort ();
16442
16443
0
    if (flag_debug)
16444
0
      {
16445
0
        const char *msg;
16446
0
        const char *branch = "branch";
16447
0
        const char *prefix = "";
16448
0
        fragS *padding_fragP;
16449
0
        if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
16450
0
      == BRANCH_PREFIX)
16451
0
    {
16452
0
      padding_fragP = fragP->tc_frag_data.u.padding_fragP;
16453
0
      switch (fragP->tc_frag_data.default_prefix)
16454
0
        {
16455
0
        default:
16456
0
          abort ();
16457
0
          break;
16458
0
        case CS_PREFIX_OPCODE:
16459
0
          prefix = " cs";
16460
0
          break;
16461
0
        case DS_PREFIX_OPCODE:
16462
0
          prefix = " ds";
16463
0
          break;
16464
0
        case ES_PREFIX_OPCODE:
16465
0
          prefix = " es";
16466
0
          break;
16467
0
        case FS_PREFIX_OPCODE:
16468
0
          prefix = " fs";
16469
0
          break;
16470
0
        case GS_PREFIX_OPCODE:
16471
0
          prefix = " gs";
16472
0
          break;
16473
0
        case SS_PREFIX_OPCODE:
16474
0
          prefix = " ss";
16475
0
          break;
16476
0
        }
16477
0
      if (padding_fragP)
16478
0
        msg = _("%s:%u: add %d%s at 0x%llx to align "
16479
0
          "%s within %d-byte boundary\n");
16480
0
      else
16481
0
        msg = _("%s:%u: add additional %d%s at 0x%llx to "
16482
0
          "align %s within %d-byte boundary\n");
16483
0
    }
16484
0
        else
16485
0
    {
16486
0
      padding_fragP = fragP;
16487
0
      msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
16488
0
        "%s within %d-byte boundary\n");
16489
0
    }
16490
16491
0
        if (padding_fragP)
16492
0
    switch (padding_fragP->tc_frag_data.branch_type)
16493
0
      {
16494
0
      case align_branch_jcc:
16495
0
        branch = "jcc";
16496
0
        break;
16497
0
      case align_branch_fused:
16498
0
        branch = "fused jcc";
16499
0
        break;
16500
0
      case align_branch_jmp:
16501
0
        branch = "jmp";
16502
0
        break;
16503
0
      case align_branch_call:
16504
0
        branch = "call";
16505
0
        break;
16506
0
      case align_branch_indirect:
16507
0
        branch = "indiret branch";
16508
0
        break;
16509
0
      case align_branch_ret:
16510
0
        branch = "ret";
16511
0
        break;
16512
0
      default:
16513
0
        break;
16514
0
      }
16515
16516
0
        fprintf (stdout, msg,
16517
0
           fragP->fr_file, fragP->fr_line, size, prefix,
16518
0
           (long long) fragP->fr_address, branch,
16519
0
           1 << align_branch_power);
16520
0
      }
16521
0
    if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
16522
0
      memset (fragP->fr_opcode,
16523
0
        fragP->tc_frag_data.default_prefix, size);
16524
0
    else
16525
0
      i386_generate_nops (fragP, (char *) fragP->fr_opcode,
16526
0
        size, 0);
16527
0
    fragP->fr_fix += size;
16528
0
  }
16529
0
      return;
16530
0
    }
16531
16532
0
  opcode = (unsigned char *) fragP->fr_opcode;
16533
16534
  /* Address we want to reach in file space.  */
16535
0
  target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
16536
16537
  /* Address opcode resides at in file space.  */
16538
0
  opcode_address = fragP->fr_address + fragP->fr_fix;
16539
16540
  /* Displacement from opcode start to fill into instruction.  */
16541
0
  displacement_from_opcode_start = target_address - opcode_address;
16542
16543
0
  if ((fragP->fr_subtype & BIG) == 0)
16544
0
    {
16545
      /* Don't have to change opcode.  */
16546
0
      extension = 1;    /* 1 opcode + 1 displacement  */
16547
0
      where_to_put_displacement = &opcode[1];
16548
0
    }
16549
0
  else
16550
0
    {
16551
0
      if (fragP->tc_frag_data.no_cond_jump_promotion
16552
0
    && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
16553
0
  as_warn_where (fragP->fr_file, fragP->fr_line,
16554
0
           _("long jump required"));
16555
16556
0
      switch (fragP->fr_subtype)
16557
0
  {
16558
0
  case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
16559
0
    extension = 4;    /* 1 opcode + 4 displacement  */
16560
0
    opcode[0] = 0xe9;
16561
0
    where_to_put_displacement = &opcode[1];
16562
0
    break;
16563
16564
0
  case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
16565
0
    extension = 2;    /* 1 opcode + 2 displacement  */
16566
0
    opcode[0] = 0xe9;
16567
0
    where_to_put_displacement = &opcode[1];
16568
0
    break;
16569
16570
0
  case ENCODE_RELAX_STATE (COND_JUMP, BIG):
16571
0
  case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
16572
0
    extension = 5;    /* 2 opcode + 4 displacement  */
16573
0
    opcode[1] = opcode[0] + 0x10;
16574
0
    opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
16575
0
    where_to_put_displacement = &opcode[2];
16576
0
    break;
16577
16578
0
  case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
16579
0
    extension = 3;    /* 2 opcode + 2 displacement  */
16580
0
    opcode[1] = opcode[0] + 0x10;
16581
0
    opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
16582
0
    where_to_put_displacement = &opcode[2];
16583
0
    break;
16584
16585
0
  case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
16586
0
    extension = 4;
16587
0
    opcode[0] ^= 1;
16588
0
    opcode[1] = 3;
16589
0
    opcode[2] = 0xe9;
16590
0
    where_to_put_displacement = &opcode[3];
16591
0
    break;
16592
16593
0
  default:
16594
0
    BAD_CASE (fragP->fr_subtype);
16595
0
    break;
16596
0
  }
16597
0
    }
16598
16599
  /* If size if less then four we are sure that the operand fits,
16600
     but if it's 4, then it could be that the displacement is larger
16601
     then -/+ 2GB.  */
16602
0
  if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
16603
0
      && object_64bit
16604
0
      && ((addressT) (displacement_from_opcode_start - extension
16605
0
          + ((addressT) 1 << 31))
16606
0
    > (((addressT) 2 << 31) - 1)))
16607
0
    {
16608
0
      as_bad_where (fragP->fr_file, fragP->fr_line,
16609
0
        _("jump target out of range"));
16610
      /* Make us emit 0.  */
16611
0
      displacement_from_opcode_start = extension;
16612
0
    }
16613
  /* Now put displacement after opcode.  */
16614
0
  md_number_to_chars ((char *) where_to_put_displacement,
16615
0
          displacement_from_opcode_start - extension,
16616
0
          DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
16617
0
  fragP->fr_fix += extension;
16618
0
}
16619

16620
/* Apply a fixup (fixP) to segment data, once it has been determined
16621
   by our caller that we have all the info we need to fix it up.
16622
16623
   Parameter valP is the pointer to the value of the bits.
16624
16625
   On the 386, immediates, displacements, and data pointers are all in
16626
   the same (little-endian) format, so we don't need to care about which
16627
   we are handling.  */
16628
16629
void
16630
md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
16631
0
{
16632
0
  char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
16633
0
  valueT value = *valP;
16634
16635
0
#if !defined (TE_Mach)
16636
0
  if (fixP->fx_pcrel)
16637
0
    {
16638
0
      switch (fixP->fx_r_type)
16639
0
  {
16640
0
  default:
16641
0
    break;
16642
16643
0
  case BFD_RELOC_64:
16644
0
    fixP->fx_r_type = BFD_RELOC_64_PCREL;
16645
0
    break;
16646
0
  case BFD_RELOC_32:
16647
0
  case BFD_RELOC_X86_64_32S:
16648
0
    fixP->fx_r_type = BFD_RELOC_32_PCREL;
16649
0
    break;
16650
0
  case BFD_RELOC_16:
16651
0
    fixP->fx_r_type = BFD_RELOC_16_PCREL;
16652
0
    break;
16653
0
  case BFD_RELOC_8:
16654
0
    fixP->fx_r_type = BFD_RELOC_8_PCREL;
16655
0
    break;
16656
0
  }
16657
0
    }
16658
16659
0
  if (fixP->fx_addsy != NULL
16660
0
      && (fixP->fx_r_type == BFD_RELOC_32_PCREL
16661
0
    || fixP->fx_r_type == BFD_RELOC_64_PCREL
16662
0
    || fixP->fx_r_type == BFD_RELOC_16_PCREL
16663
0
    || fixP->fx_r_type == BFD_RELOC_8_PCREL)
16664
0
      && !use_rela_relocations)
16665
0
    {
16666
      /* This is a hack.  There should be a better way to handle this.
16667
   This covers for the fact that bfd_install_relocation will
16668
   subtract the current location (for partial_inplace, PC relative
16669
   relocations); see more below.  */
16670
0
#if defined (OBJ_ELF) || defined (TE_PE)
16671
0
      value += fixP->fx_where + fixP->fx_frag->fr_address;
16672
0
#endif
16673
0
#ifdef OBJ_ELF
16674
0
      segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
16675
16676
0
      if ((sym_seg == seg
16677
0
     || (symbol_section_p (fixP->fx_addsy)
16678
0
         && sym_seg != absolute_section))
16679
0
    && !generic_force_reloc (fixP))
16680
0
  {
16681
    /* Yes, we add the values in twice.  This is because
16682
       bfd_install_relocation subtracts them out again.  I think
16683
       bfd_install_relocation is broken, but I don't dare change
16684
       it.  FIXME.  */
16685
0
    value += fixP->fx_where + fixP->fx_frag->fr_address;
16686
0
  }
16687
0
#endif
16688
#if defined (OBJ_COFF) && defined (TE_PE)
16689
      /* For some reason, the PE format does not store a
16690
   section address offset for a PC relative symbol.  */
16691
      if (S_GET_SEGMENT (fixP->fx_addsy) != seg
16692
    || S_IS_WEAK (fixP->fx_addsy))
16693
  value += md_pcrel_from (fixP);
16694
#endif
16695
0
    }
16696
#if defined (OBJ_COFF) && defined (TE_PE)
16697
  if (fixP->fx_addsy != NULL
16698
      && S_IS_WEAK (fixP->fx_addsy)
16699
      /* PR 16858: Do not modify weak function references.  */
16700
      && ! fixP->fx_pcrel)
16701
    {
16702
#if !defined (TE_PEP)
16703
      /* For x86 PE weak function symbols are neither PC-relative
16704
   nor do they set S_IS_FUNCTION.  So the only reliable way
16705
   to detect them is to check the flags of their containing
16706
   section.  */
16707
      if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
16708
    && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
16709
  ;
16710
      else
16711
#endif
16712
      value -= S_GET_VALUE (fixP->fx_addsy);
16713
    }
16714
#endif
16715
16716
  /* Fix a few things - the dynamic linker expects certain values here,
16717
     and we must not disappoint it.  */
16718
0
#ifdef OBJ_ELF
16719
0
  if (fixP->fx_addsy)
16720
0
    switch (fixP->fx_r_type)
16721
0
      {
16722
0
      case BFD_RELOC_386_PLT32:
16723
0
      case BFD_RELOC_32_PLT_PCREL:
16724
  /* Make the jump instruction point to the address of the operand.
16725
     At runtime we merely add the offset to the actual PLT entry.
16726
     NB: Subtract the offset size only for jump instructions.  */
16727
0
  if (fixP->fx_pcrel)
16728
0
    value = -4;
16729
0
  break;
16730
16731
0
      case BFD_RELOC_386_TLS_GD:
16732
0
      case BFD_RELOC_386_TLS_LDM:
16733
0
      case BFD_RELOC_386_TLS_IE_32:
16734
0
      case BFD_RELOC_386_TLS_IE:
16735
0
      case BFD_RELOC_386_TLS_GOTIE:
16736
0
      case BFD_RELOC_386_TLS_GOTDESC:
16737
0
      case BFD_RELOC_X86_64_TLSGD:
16738
0
      case BFD_RELOC_X86_64_TLSLD:
16739
0
      case BFD_RELOC_X86_64_GOTTPOFF:
16740
0
      case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
16741
0
      case BFD_RELOC_X86_64_CODE_5_GOTTPOFF:
16742
0
      case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
16743
0
      case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
16744
0
      case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
16745
0
      case BFD_RELOC_X86_64_CODE_5_GOTPC32_TLSDESC:
16746
0
      case BFD_RELOC_X86_64_CODE_6_GOTPC32_TLSDESC:
16747
0
  value = 0; /* Fully resolved at runtime.  No addend.  */
16748
  /* Fallthrough */
16749
0
      case BFD_RELOC_386_TLS_LE:
16750
0
      case BFD_RELOC_386_TLS_LDO_32:
16751
0
      case BFD_RELOC_386_TLS_LE_32:
16752
0
      case BFD_RELOC_X86_64_DTPOFF32:
16753
0
      case BFD_RELOC_X86_64_DTPOFF64:
16754
0
      case BFD_RELOC_X86_64_TPOFF32:
16755
0
      case BFD_RELOC_X86_64_TPOFF64:
16756
0
  S_SET_THREAD_LOCAL (fixP->fx_addsy);
16757
0
  break;
16758
16759
0
      case BFD_RELOC_386_TLS_DESC_CALL:
16760
0
      case BFD_RELOC_X86_64_TLSDESC_CALL:
16761
0
  value = 0; /* Fully resolved at runtime.  No addend.  */
16762
0
  S_SET_THREAD_LOCAL (fixP->fx_addsy);
16763
0
  fixP->fx_done = 0;
16764
0
  return;
16765
16766
0
      case BFD_RELOC_VTABLE_INHERIT:
16767
0
      case BFD_RELOC_VTABLE_ENTRY:
16768
0
  fixP->fx_done = 0;
16769
0
  return;
16770
16771
0
      default:
16772
0
  break;
16773
0
      }
16774
0
#endif /* OBJ_ELF  */
16775
16776
  /* If not 64bit, massage value, to account for wraparound when !BFD64.  */
16777
0
  if (!object_64bit)
16778
0
    value = extend_to_32bit_address (value);
16779
16780
0
  *valP = value;
16781
0
#endif /* !defined (TE_Mach)  */
16782
16783
  /* Are we finished with this relocation now?  */
16784
0
  if (fixP->fx_addsy == NULL)
16785
0
    {
16786
0
      fixP->fx_done = 1;
16787
0
      switch (fixP->fx_r_type)
16788
0
  {
16789
0
  case BFD_RELOC_X86_64_32S:
16790
0
    fixP->fx_signed = 1;
16791
0
    break;
16792
16793
0
  default:
16794
0
    break;
16795
0
  }
16796
0
    }
16797
#if defined (OBJ_COFF) && defined (TE_PE)
16798
  else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
16799
    {
16800
      fixP->fx_done = 0;
16801
      /* Remember value for tc_gen_reloc.  */
16802
      fixP->fx_addnumber = value;
16803
      /* Clear out the frag for now.  */
16804
      value = 0;
16805
    }
16806
#endif
16807
0
  else if (use_rela_relocations)
16808
0
    {
16809
0
      if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
16810
0
  fixP->fx_no_overflow = 1;
16811
      /* Remember value for tc_gen_reloc.  */
16812
0
      fixP->fx_addnumber = value;
16813
0
      value = 0;
16814
0
    }
16815
16816
0
  md_number_to_chars (p, value, fixP->fx_size);
16817
0
}
16818

16819
const char *
16820
md_atof (int type, char *litP, int *sizeP)
16821
1.59k
{
16822
  /* This outputs the LITTLENUMs in REVERSE order;
16823
     in accord with the bigendian 386.  */
16824
1.59k
  return ieee_md_atof (type, litP, sizeP, false);
16825
1.59k
}
16826

16827
static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
16828
16829
static char *
16830
output_invalid (int c)
16831
44.0k
{
16832
44.0k
  if (ISPRINT (c))
16833
15.9k
    snprintf (output_invalid_buf, sizeof (output_invalid_buf),
16834
15.9k
        "'%c'", c);
16835
28.1k
  else
16836
28.1k
    snprintf (output_invalid_buf, sizeof (output_invalid_buf),
16837
28.1k
        "(0x%x)", (unsigned char) c);
16838
44.0k
  return output_invalid_buf;
16839
44.0k
}
16840
16841
/* Verify that @r can be used in the current context.  */
16842
16843
static bool check_register (const reg_entry *r)
16844
2.02k
{
16845
2.02k
  if (allow_pseudo_reg)
16846
24
    return true;
16847
16848
2.00k
  if (operand_type_all_zero (&r->reg_type))
16849
0
    return false;
16850
16851
2.00k
  if ((r->reg_type.bitfield.dword
16852
1.97k
       || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
16853
1.97k
       || r->reg_type.bitfield.class == RegCR
16854
1.97k
       || r->reg_type.bitfield.class == RegDR)
16855
178
      && !cpu_arch_flags.bitfield.cpui386)
16856
0
    return false;
16857
16858
2.00k
  if (r->reg_type.bitfield.class == RegTR
16859
18
      && (flag_code == CODE_64BIT
16860
0
    || !cpu_arch_flags.bitfield.cpui386
16861
0
    || cpu_arch_isa_flags.bitfield.cpui586
16862
0
    || cpu_arch_isa_flags.bitfield.cpui686))
16863
18
    return false;
16864
16865
1.98k
  if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
16866
0
    return false;
16867
16868
1.98k
  if (!cpu_arch_flags.bitfield.cpuavx512f)
16869
1.30k
    {
16870
1.30k
      if (r->reg_type.bitfield.zmmword
16871
1.30k
    || r->reg_type.bitfield.class == RegMask)
16872
0
  return false;
16873
16874
1.30k
      if (!cpu_arch_flags.bitfield.cpuavx)
16875
1.12k
  {
16876
1.12k
    if (r->reg_type.bitfield.ymmword)
16877
0
      return false;
16878
16879
1.12k
    if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
16880
0
      return false;
16881
1.12k
  }
16882
1.30k
    }
16883
16884
1.98k
  if (r->reg_type.bitfield.zmmword)
16885
0
    {
16886
0
      if (vector_size < VSZ512)
16887
0
  return false;
16888
16889
      /* Don't update pp when not dealing with insn operands.  */
16890
0
      switch (current_templates.start ? pp.encoding : encoding_evex)
16891
0
  {
16892
0
  case encoding_default:
16893
0
  case encoding_egpr:
16894
0
    pp.encoding = encoding_evex512;
16895
0
    break;
16896
0
  case encoding_evex:
16897
0
  case encoding_evex512:
16898
0
    break;
16899
0
  default:
16900
0
    pp.encoding = encoding_error;
16901
0
    break;
16902
0
  }
16903
0
    }
16904
16905
1.98k
  if (vector_size < VSZ256 && r->reg_type.bitfield.ymmword)
16906
0
    return false;
16907
16908
1.98k
  if (r->reg_type.bitfield.tmmword
16909
19
      && (!cpu_arch_flags.bitfield.cpuamx_tile
16910
19
          || flag_code != CODE_64BIT))
16911
9
    return false;
16912
16913
1.97k
  if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
16914
6
    return false;
16915
16916
  /* Don't allow fake index register unless allow_index_reg isn't 0. */
16917
1.97k
  if (!allow_index_reg && r->reg_num == RegIZ)
16918
224
    return false;
16919
16920
  /* Upper 16 vector registers are only available with VREX in 64bit
16921
     mode, and require EVEX encoding.  */
16922
1.74k
  if (r->reg_flags & RegVRex)
16923
20
    {
16924
20
      if (!cpu_arch_flags.bitfield.cpuavx512f
16925
4
    || flag_code != CODE_64BIT)
16926
19
  return false;
16927
16928
      /* Don't update pp when not dealing with insn operands.  */
16929
1
      switch (current_templates.start ? pp.encoding : encoding_evex)
16930
1
  {
16931
0
    case encoding_default:
16932
0
    case encoding_egpr:
16933
0
    case encoding_evex512:
16934
0
      pp.encoding = encoding_evex;
16935
0
      break;
16936
1
    case encoding_evex:
16937
1
      break;
16938
0
    default:
16939
0
      pp.encoding = encoding_error;
16940
0
      break;
16941
1
  }
16942
1
    }
16943
16944
1.72k
  if (r->reg_flags & RegRex2)
16945
24
    {
16946
24
      if (!cpu_arch_flags.bitfield.cpuapx_f
16947
15
    || flag_code != CODE_64BIT)
16948
9
  return false;
16949
16950
      /* Don't update pp when not dealing with insn operands.  */
16951
15
      switch (current_templates.start ? pp.encoding : encoding_egpr)
16952
15
  {
16953
7
  case encoding_default:
16954
7
    pp.encoding = encoding_egpr;
16955
7
    break;
16956
6
  case encoding_egpr:
16957
8
  case encoding_evex:
16958
8
  case encoding_evex512:
16959
8
    break;
16960
0
  default:
16961
0
    pp.encoding = encoding_error;
16962
0
    break;
16963
15
  }
16964
15
    }
16965
16966
1.71k
  if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
16967
324
      && (!cpu_arch_flags.bitfield.cpu64
16968
324
    || r->reg_type.bitfield.class != RegCR
16969
0
    || dot_insn ())
16970
324
      && flag_code != CODE_64BIT)
16971
3
    return false;
16972
16973
1.71k
  if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
16974
0
      && !intel_syntax)
16975
0
    return false;
16976
16977
1.71k
  return true;
16978
1.71k
}
16979
16980
/* REG_STRING starts *before* REGISTER_PREFIX.  */
16981
16982
static const reg_entry *
16983
parse_real_register (const char *reg_string, char **end_op)
16984
6.49k
{
16985
6.49k
  const char *s = reg_string;
16986
6.49k
  char *p;
16987
6.49k
  char reg_name_given[MAX_REG_NAME_SIZE + 1];
16988
6.49k
  const reg_entry *r;
16989
16990
  /* Skip possible REGISTER_PREFIX and possible whitespace.  */
16991
6.49k
  if (*s == REGISTER_PREFIX)
16992
6.14k
    ++s;
16993
16994
6.49k
  if (is_whitespace (*s))
16995
0
    ++s;
16996
16997
6.49k
  p = reg_name_given;
16998
11.4k
  while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
16999
4.98k
    {
17000
4.98k
      if (p >= reg_name_given + MAX_REG_NAME_SIZE)
17001
43
  return NULL;
17002
4.94k
      s++;
17003
4.94k
    }
17004
17005
6.45k
  if (is_part_of_name (*s))
17006
200
    return NULL;
17007
17008
6.25k
  *end_op = (char *) s;
17009
17010
6.25k
  r = str_hash_find (reg_hash, reg_name_given);
17011
17012
  /* Handle floating point regs, allowing spaces in the (i) part.  */
17013
6.25k
  if (r == reg_st0)
17014
201
    {
17015
201
      if (!cpu_arch_flags.bitfield.cpu8087
17016
76
    && !cpu_arch_flags.bitfield.cpu287
17017
76
    && !cpu_arch_flags.bitfield.cpu387
17018
0
    && !allow_pseudo_reg)
17019
0
  return NULL;
17020
17021
201
      if (is_whitespace (*s))
17022
106
  ++s;
17023
201
      if (*s == '(')
17024
0
  {
17025
0
    ++s;
17026
0
    if (is_whitespace (*s))
17027
0
      ++s;
17028
0
    if (*s >= '0' && *s <= '7')
17029
0
      {
17030
0
        int fpr = *s - '0';
17031
0
        ++s;
17032
0
        if (is_whitespace (*s))
17033
0
    ++s;
17034
0
        if (*s == ')')
17035
0
    {
17036
0
      *end_op = (char *) s + 1;
17037
0
      know (r[fpr].reg_num == fpr);
17038
0
      return r + fpr;
17039
0
    }
17040
0
      }
17041
    /* We have "%st(" then garbage.  */
17042
0
    return NULL;
17043
0
  }
17044
201
    }
17045
17046
6.25k
  return r && check_register (r) ? r : NULL;
17047
6.25k
}
17048
17049
/* REG_STRING starts *before* REGISTER_PREFIX.  */
17050
17051
static const reg_entry *
17052
parse_register (const char *reg_string, char **end_op)
17053
6.61k
{
17054
6.61k
  const reg_entry *r;
17055
17056
6.61k
  if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
17057
81
    r = parse_real_register (reg_string, end_op);
17058
6.53k
  else
17059
6.53k
    r = NULL;
17060
6.61k
  if (!r)
17061
6.55k
    {
17062
6.55k
      char *save = input_line_pointer;
17063
6.55k
      char *buf = xstrdup (reg_string), *name;
17064
6.55k
      symbolS *symbolP;
17065
6.55k
      offsetT off;
17066
17067
6.55k
      input_line_pointer = buf;
17068
6.55k
      get_symbol_name (&name);
17069
6.55k
      symbolP = symbol_find (name);
17070
6.55k
      symbolP = symbol_equated_to (symbolP, &off);
17071
6.55k
      if (symbolP && off == 0 && S_GET_SEGMENT (symbolP) == reg_section)
17072
52
  {
17073
52
    const expressionS *e = symbol_get_value_expression (symbolP);
17074
17075
52
    if (e->X_op == O_register)
17076
52
      {
17077
52
        know ((valueT) e->X_add_number < i386_regtab_size);
17078
52
        r = i386_regtab + e->X_add_number;
17079
52
        *end_op = (char *) reg_string + (input_line_pointer - buf);
17080
52
      }
17081
52
    if (r && !check_register (r))
17082
0
      {
17083
0
        as_bad (_("register '%s%s' cannot be used here"),
17084
0
          register_prefix, r->reg_name);
17085
0
        r = &bad_reg;
17086
0
      }
17087
52
  }
17088
6.55k
      input_line_pointer = save;
17089
6.55k
      free (buf);
17090
6.55k
    }
17091
6.61k
  return r;
17092
6.61k
}
17093
17094
int
17095
i386_parse_name (char *name,
17096
     expressionS *e,
17097
     enum expr_mode mode,
17098
     char *nextcharP)
17099
41.9k
{
17100
41.9k
  const reg_entry *r = NULL;
17101
41.9k
  char *end = input_line_pointer;
17102
17103
  /* We only know the terminating character here.  It being double quote could
17104
     be the closing one of a quoted symbol name, or an opening one from a
17105
     following string (or another quoted symbol name).  Since the latter can't
17106
     be valid syntax for anything, bailing in either case is good enough.  */
17107
41.9k
  if (*nextcharP == '"')
17108
1.37k
    return 0;
17109
17110
40.6k
  *end = *nextcharP;
17111
40.6k
  if (*name == REGISTER_PREFIX || allow_naked_reg)
17112
406
    r = parse_real_register (name, &input_line_pointer);
17113
40.6k
  if (r && end <= input_line_pointer)
17114
24
    {
17115
24
      *nextcharP = *input_line_pointer;
17116
24
      *input_line_pointer = 0;
17117
24
      e->X_op = O_register;
17118
24
      e->X_add_number = r - i386_regtab;
17119
24
      return 1;
17120
24
    }
17121
40.5k
  input_line_pointer = end;
17122
40.5k
  *end = 0;
17123
40.5k
  return intel_syntax ? i386_intel_parse_name (name, e, mode) : 0;
17124
40.6k
}
17125
17126
void
17127
md_operand (expressionS *e)
17128
25.8k
{
17129
25.8k
  char *end;
17130
25.8k
  const reg_entry *r;
17131
17132
25.8k
  switch (*input_line_pointer)
17133
25.8k
    {
17134
6.01k
    case REGISTER_PREFIX:
17135
6.01k
      r = parse_real_register (input_line_pointer, &end);
17136
6.01k
      if (r)
17137
1.07k
  {
17138
1.07k
    e->X_op = O_register;
17139
1.07k
    e->X_add_number = r - i386_regtab;
17140
1.07k
    input_line_pointer = end;
17141
1.07k
  }
17142
6.01k
      break;
17143
17144
66
    case '[':
17145
66
      gas_assert (intel_syntax);
17146
66
      end = input_line_pointer++;
17147
66
      expression (e);
17148
66
      if (*input_line_pointer == ']')
17149
62
  {
17150
62
    ++input_line_pointer;
17151
62
    e->X_op_symbol = make_expr_symbol (e);
17152
62
    e->X_add_symbol = NULL;
17153
62
    e->X_add_number = 0;
17154
62
    e->X_op = O_index;
17155
62
  }
17156
4
      else
17157
4
  {
17158
4
    e->X_op = O_absent;
17159
4
    input_line_pointer = end;
17160
4
  }
17161
66
      break;
17162
25.8k
    }
17163
25.8k
}
17164
17165
#ifdef BFD64
17166
/* To maintain consistency with !BFD64 builds of gas record, whether any
17167
   (binary) operator was involved in an expression.  As expressions are
17168
   evaluated in only 32 bits when !BFD64, we use this to decide whether to
17169
   truncate results.  */
17170
bool i386_record_operator (operatorT op,
17171
         const expressionS *left,
17172
         const expressionS *right)
17173
71.3k
{
17174
71.3k
  if (op == O_absent)
17175
3.35k
    return false;
17176
17177
68.0k
  if (!left)
17178
17.6k
    {
17179
      /* Since the expression parser applies unary operators fine to bignum
17180
   operands, we don't need to be concerned of respective operands not
17181
   fitting in 32 bits.  */
17182
17.6k
      if (right->X_op == O_constant && right->X_unsigned
17183
10.5k
    && !fits_in_unsigned_long (right->X_add_number))
17184
1.56k
  return false;
17185
17.6k
    }
17186
  /* This isn't entirely right: The pattern can also result when constant
17187
     expressions are folded (e.g. 0xffffffff + 1).  */
17188
50.3k
  else if ((left->X_op == O_constant && left->X_unsigned
17189
10.8k
      && !fits_in_unsigned_long (left->X_add_number))
17190
49.7k
     || (right->X_op == O_constant && right->X_unsigned
17191
32.3k
         && !fits_in_unsigned_long (right->X_add_number)))
17192
2.53k
    expr_mode = expr_large_value;
17193
17194
66.4k
  if (expr_mode != expr_large_value)
17195
49.8k
    expr_mode = expr_operator_present;
17196
17197
66.4k
  return false;
17198
68.0k
}
17199
#endif
17200

17201
const char md_shortopts[] =
17202
#ifdef OBJ_ELF
17203
  "kVQ:"
17204
# ifdef TE_SOLARIS
17205
  "s"
17206
# endif
17207
#endif
17208
  "qnO::";
17209
17210
0
#define OPTION_32 (OPTION_MD_BASE + 0)
17211
0
#define OPTION_64 (OPTION_MD_BASE + 1)
17212
0
#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
17213
0
#define OPTION_MARCH (OPTION_MD_BASE + 3)
17214
0
#define OPTION_MTUNE (OPTION_MD_BASE + 4)
17215
0
#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
17216
0
#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
17217
0
#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
17218
0
#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
17219
0
#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
17220
0
#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
17221
0
#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
17222
0
#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
17223
0
#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
17224
0
#define OPTION_X32 (OPTION_MD_BASE + 14)
17225
0
#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
17226
0
#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
17227
0
#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
17228
#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
17229
0
#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
17230
0
#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
17231
0
#define OPTION_MSHARED (OPTION_MD_BASE + 21)
17232
0
#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
17233
0
#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
17234
0
#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
17235
0
#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
17236
0
#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
17237
0
#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
17238
0
#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
17239
0
#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
17240
0
#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
17241
0
#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
17242
0
#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
17243
0
#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
17244
0
#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
17245
0
#define OPTION_MTLS_CHECK (OPTION_MD_BASE + 35)
17246
17247
const struct option md_longopts[] =
17248
{
17249
  {"32", no_argument, NULL, OPTION_32},
17250
#if (defined (OBJ_ELF) || defined (TE_PE) || defined (OBJ_MACH_O)) \
17251
    && defined (BFD64)
17252
  {"64", no_argument, NULL, OPTION_64},
17253
#endif
17254
#ifdef OBJ_ELF
17255
# ifdef BFD64
17256
  {"x32", no_argument, NULL, OPTION_X32},
17257
# endif
17258
  {"mshared", no_argument, NULL, OPTION_MSHARED},
17259
  {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
17260
#endif
17261
  {"divide", no_argument, NULL, OPTION_DIVIDE},
17262
  {"march", required_argument, NULL, OPTION_MARCH},
17263
  {"mtune", required_argument, NULL, OPTION_MTUNE},
17264
  {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
17265
  {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
17266
  {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
17267
  {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
17268
  {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
17269
  {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
17270
  {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
17271
  {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
17272
  {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
17273
  {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
17274
  {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
17275
  {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
17276
  {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
17277
# if defined (TE_PE) || defined (TE_PEP)
17278
  {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
17279
#endif
17280
  {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
17281
  {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
17282
  {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
17283
  {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
17284
  {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
17285
  {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
17286
  {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
17287
  {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
17288
  {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
17289
  {"mlfence-before-indirect-branch", required_argument, NULL,
17290
   OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
17291
  {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
17292
  {"mamd64", no_argument, NULL, OPTION_MAMD64},
17293
  {"mintel64", no_argument, NULL, OPTION_MINTEL64},
17294
  {"mtls-check", required_argument, NULL, OPTION_MTLS_CHECK},
17295
  {NULL, no_argument, NULL, 0}
17296
};
17297
const size_t md_longopts_size = sizeof (md_longopts);
17298
17299
int
17300
md_parse_option (int c, const char *arg)
17301
0
{
17302
0
  unsigned int j;
17303
0
  char *arch, *next, *saved, *type;
17304
17305
0
  switch (c)
17306
0
    {
17307
0
    case 'n':
17308
0
      optimize_align_code = 0;
17309
0
      break;
17310
17311
0
    case 'q':
17312
0
      quiet_warnings = 1;
17313
0
      break;
17314
17315
0
#ifdef OBJ_ELF
17316
      /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
17317
   should be emitted or not.  FIXME: Not implemented.  */
17318
0
    case 'Q':
17319
0
      if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
17320
0
  return 0;
17321
0
      break;
17322
17323
      /* -V: SVR4 argument to print version ID.  */
17324
0
    case 'V':
17325
0
      print_version_id ();
17326
0
      break;
17327
17328
      /* -k: Ignore for FreeBSD compatibility.  */
17329
0
    case 'k':
17330
0
      break;
17331
17332
# ifdef TE_SOLARIS
17333
    case 's':
17334
      /* -s: On i386 Solaris, this tells the native assembler to use
17335
   .stab instead of .stab.excl.  We always use .stab anyhow.  */
17336
      break;
17337
# endif
17338
17339
0
    case OPTION_MSHARED:
17340
0
      shared = 1;
17341
0
      break;
17342
17343
0
    case OPTION_X86_USED_NOTE:
17344
0
      if (strcasecmp (arg, "yes") == 0)
17345
0
        x86_used_note = 1;
17346
0
      else if (strcasecmp (arg, "no") == 0)
17347
0
        x86_used_note = 0;
17348
0
      else
17349
0
        as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
17350
0
      break;
17351
0
#endif
17352
17353
0
#ifdef BFD64
17354
17355
0
#if (defined (OBJ_ELF) || defined (TE_PE) || defined (OBJ_MACH_O))
17356
0
    case OPTION_64:
17357
0
      {
17358
0
  const char **list, **l;
17359
17360
0
  list = bfd_target_list ();
17361
0
  for (l = list; *l != NULL; l++)
17362
0
#if defined (OBJ_ELF)
17363
0
    if (strcmp (*l, ELF_TARGET_FORMAT64) == 0)
17364
#elif defined (TE_PE)
17365
    if (strcmp (*l, "pe-x86-64") == 0)
17366
#else
17367
    if (strcmp (*l, "mach-o-x86-64") == 0)
17368
#endif
17369
0
      {
17370
0
        default_arch = "x86_64";
17371
0
        break;
17372
0
      }
17373
0
  if (*l == NULL)
17374
0
    as_fatal (_("no compiled in support for x86_64"));
17375
0
  free (list);
17376
0
      }
17377
0
      break;
17378
0
#endif
17379
17380
0
#ifdef OBJ_ELF
17381
0
    case OPTION_X32:
17382
0
      {
17383
0
  const char **list, **l;
17384
17385
0
  list = bfd_target_list ();
17386
0
  for (l = list; *l != NULL; l++)
17387
0
    if (strcmp (*l, ELF_TARGET_FORMAT32) == 0)
17388
0
      {
17389
0
        default_arch = "x86_64:32";
17390
0
        break;
17391
0
      }
17392
0
  if (*l == NULL)
17393
0
    as_fatal (_("no compiled in support for 32bit x86_64"));
17394
0
  free (list);
17395
0
      }
17396
0
      break;
17397
0
#endif
17398
17399
0
#endif /* BFD64 */
17400
17401
0
    case OPTION_32:
17402
0
      {
17403
0
  const char **list, **l;
17404
17405
0
  list = bfd_target_list ();
17406
0
  for (l = list; *l != NULL; l++)
17407
0
    if (strstr (*l, "-i386")
17408
0
        || strstr (*l, "-go32"))
17409
0
      {
17410
0
        default_arch = "i386";
17411
0
        break;
17412
0
      }
17413
0
  if (*l == NULL)
17414
0
    as_fatal (_("no compiled in support for ix86"));
17415
0
  free (list);
17416
0
      }
17417
0
      break;
17418
17419
0
    case OPTION_DIVIDE:
17420
#ifdef SVR4_COMMENT_CHARS
17421
      {
17422
  char *n, *t;
17423
  const char *s;
17424
17425
  n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
17426
  t = n;
17427
  for (s = i386_comment_chars; *s != '\0'; s++)
17428
    if (*s != '/')
17429
      *t++ = *s;
17430
  *t = '\0';
17431
  i386_comment_chars = n;
17432
      }
17433
#endif
17434
0
      break;
17435
17436
0
    case OPTION_MARCH:
17437
0
      saved = xstrdup (arg);
17438
0
      arch = saved;
17439
      /* Allow -march=+nosse.  */
17440
0
      if (*arch == '+')
17441
0
  arch++;
17442
0
      do
17443
0
  {
17444
0
    char *vsz;
17445
17446
0
    if (*arch == '.')
17447
0
      as_fatal (_("invalid -march= option: `%s'"), arg);
17448
0
    next = strchr (arch, '+');
17449
0
    if (next)
17450
0
      *next++ = '\0';
17451
0
    vsz = strchr (arch, '/');
17452
0
    if (vsz)
17453
0
      *vsz++ = '\0';
17454
0
    for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
17455
0
      {
17456
0
        if (vsz && cpu_arch[j].vsz != vsz_set)
17457
0
    continue;
17458
17459
0
        if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
17460
0
            && strcmp (arch, cpu_arch[j].name) == 0)
17461
0
    {
17462
      /* Processor.  */
17463
0
      if (! cpu_arch[j].enable.bitfield.cpui386)
17464
0
        continue;
17465
17466
0
      cpu_arch_name = cpu_arch[j].name;
17467
0
      free (cpu_sub_arch_name);
17468
0
      cpu_sub_arch_name = NULL;
17469
0
      cpu_arch_flags = cpu_arch[j].enable;
17470
0
      cpu_arch_isa = cpu_arch[j].type;
17471
0
      cpu_arch_isa_flags = cpu_arch[j].enable;
17472
0
      if (!cpu_arch_tune_set)
17473
0
        cpu_arch_tune = cpu_arch_isa;
17474
0
      vector_size = VSZ_DEFAULT;
17475
0
      break;
17476
0
    }
17477
0
        else if (cpu_arch[j].type == PROCESSOR_NONE
17478
0
           && strcmp (arch, cpu_arch[j].name) == 0
17479
0
           && !cpu_flags_all_zero (&cpu_arch[j].enable))
17480
0
    {
17481
      /* ISA extension.  */
17482
0
      isa_enable (j);
17483
17484
0
      switch (cpu_arch[j].vsz)
17485
0
        {
17486
0
        default:
17487
0
          break;
17488
17489
0
        case vsz_set:
17490
0
          if (vsz)
17491
0
      {
17492
0
        char *end;
17493
0
        unsigned long val = strtoul (vsz, &end, 0);
17494
17495
0
        if (*end)
17496
0
          val = 0;
17497
0
        switch (val)
17498
0
          {
17499
0
          case 512: vector_size = VSZ512; break;
17500
0
          case 256: vector_size = VSZ256; break;
17501
0
          case 128: vector_size = VSZ128; break;
17502
0
          default:
17503
0
            as_warn (_("Unrecognized vector size specifier ignored"));
17504
0
            break;
17505
0
          }
17506
0
        break;
17507
0
      }
17508
      /* Fall through.  */
17509
0
        case vsz_reset:
17510
0
          vector_size = VSZ_DEFAULT;
17511
0
          break;
17512
0
        }
17513
17514
0
      break;
17515
0
    }
17516
0
      }
17517
17518
0
    if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
17519
0
      {
17520
        /* Disable an ISA extension.  */
17521
0
        for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
17522
0
    if (cpu_arch[j].type == PROCESSOR_NONE
17523
0
        && strcmp (arch + 2, cpu_arch[j].name) == 0)
17524
0
      {
17525
0
        isa_disable (j);
17526
0
        if (cpu_arch[j].vsz == vsz_set)
17527
0
          vector_size = VSZ_DEFAULT;
17528
0
        break;
17529
0
      }
17530
0
      }
17531
17532
0
    if (j >= ARRAY_SIZE (cpu_arch))
17533
0
      as_fatal (_("invalid -march= option: `%s'"), arg);
17534
17535
0
    arch = next;
17536
0
  }
17537
0
      while (next != NULL);
17538
0
      free (saved);
17539
0
      break;
17540
17541
0
    case OPTION_MTUNE:
17542
0
      if (*arg == '.')
17543
0
  as_fatal (_("invalid -mtune= option: `%s'"), arg);
17544
0
      for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
17545
0
  {
17546
0
    if (cpu_arch[j].type != PROCESSOR_NONE
17547
0
        && strcmp (arg, cpu_arch[j].name) == 0)
17548
0
      {
17549
0
        cpu_arch_tune_set = 1;
17550
0
        cpu_arch_tune = cpu_arch [j].type;
17551
0
        break;
17552
0
      }
17553
0
  }
17554
0
      if (j >= ARRAY_SIZE (cpu_arch))
17555
0
  as_fatal (_("invalid -mtune= option: `%s'"), arg);
17556
0
      break;
17557
17558
0
    case OPTION_MMNEMONIC:
17559
0
      if (strcasecmp (arg, "att") == 0)
17560
0
  intel_mnemonic = 0;
17561
0
      else if (strcasecmp (arg, "intel") == 0)
17562
0
  intel_mnemonic = 1;
17563
0
      else
17564
0
  as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
17565
0
      break;
17566
17567
0
    case OPTION_MSYNTAX:
17568
0
      if (strcasecmp (arg, "att") == 0)
17569
0
  _set_intel_syntax (0);
17570
0
      else if (strcasecmp (arg, "intel") == 0)
17571
0
  _set_intel_syntax (1);
17572
0
      else
17573
0
  as_fatal (_("invalid -msyntax= option: `%s'"), arg);
17574
0
      break;
17575
17576
0
    case OPTION_MINDEX_REG:
17577
0
      allow_index_reg = 1;
17578
0
      break;
17579
17580
0
    case OPTION_MNAKED_REG:
17581
0
      allow_naked_reg = 1;
17582
0
      register_prefix = "";
17583
0
      break;
17584
17585
0
    case OPTION_MSSE2AVX:
17586
0
      sse2avx = 1;
17587
0
      break;
17588
17589
0
    case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
17590
0
      use_unaligned_vector_move = 1;
17591
0
      break;
17592
17593
0
    case OPTION_MSSE_CHECK:
17594
0
      if (strcasecmp (arg, "error") == 0)
17595
0
  sse_check = check_error;
17596
0
      else if (strcasecmp (arg, "warning") == 0)
17597
0
  sse_check = check_warning;
17598
0
      else if (strcasecmp (arg, "none") == 0)
17599
0
  sse_check = check_none;
17600
0
      else
17601
0
  as_fatal (_("invalid -msse-check= option: `%s'"), arg);
17602
0
      break;
17603
17604
0
    case OPTION_MOPERAND_CHECK:
17605
0
      if (strcasecmp (arg, "error") == 0)
17606
0
  operand_check = check_error;
17607
0
      else if (strcasecmp (arg, "warning") == 0)
17608
0
  operand_check = check_warning;
17609
0
      else if (strcasecmp (arg, "none") == 0)
17610
0
  operand_check = check_none;
17611
0
      else
17612
0
  as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
17613
0
      break;
17614
17615
0
    case OPTION_MAVXSCALAR:
17616
0
      if (strcasecmp (arg, "128") == 0)
17617
0
  avxscalar = vex128;
17618
0
      else if (strcasecmp (arg, "256") == 0)
17619
0
  avxscalar = vex256;
17620
0
      else
17621
0
  as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
17622
0
      break;
17623
17624
0
    case OPTION_MVEXWIG:
17625
0
      if (strcmp (arg, "0") == 0)
17626
0
  vexwig = vexw0;
17627
0
      else if (strcmp (arg, "1") == 0)
17628
0
  vexwig = vexw1;
17629
0
      else
17630
0
  as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
17631
0
      break;
17632
17633
0
    case OPTION_MADD_BND_PREFIX:
17634
0
      add_bnd_prefix = 1;
17635
0
      break;
17636
17637
0
    case OPTION_MEVEXLIG:
17638
0
      if (strcmp (arg, "128") == 0)
17639
0
  evexlig = evexl128;
17640
0
      else if (strcmp (arg, "256") == 0)
17641
0
  evexlig = evexl256;
17642
0
      else  if (strcmp (arg, "512") == 0)
17643
0
  evexlig = evexl512;
17644
0
      else
17645
0
  as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
17646
0
      break;
17647
17648
0
    case OPTION_MEVEXRCIG:
17649
0
      if (strcmp (arg, "rne") == 0)
17650
0
  evexrcig = rne;
17651
0
      else if (strcmp (arg, "rd") == 0)
17652
0
  evexrcig = rd;
17653
0
      else if (strcmp (arg, "ru") == 0)
17654
0
  evexrcig = ru;
17655
0
      else if (strcmp (arg, "rz") == 0)
17656
0
  evexrcig = rz;
17657
0
      else
17658
0
  as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
17659
0
      break;
17660
17661
0
    case OPTION_MEVEXWIG:
17662
0
      if (strcmp (arg, "0") == 0)
17663
0
  evexwig = evexw0;
17664
0
      else if (strcmp (arg, "1") == 0)
17665
0
  evexwig = evexw1;
17666
0
      else
17667
0
  as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
17668
0
      break;
17669
17670
# if defined (TE_PE) || defined (TE_PEP)
17671
    case OPTION_MBIG_OBJ:
17672
      use_big_obj = 1;
17673
      break;
17674
#endif
17675
17676
0
    case OPTION_MOMIT_LOCK_PREFIX:
17677
0
      if (strcasecmp (arg, "yes") == 0)
17678
0
        omit_lock_prefix = 1;
17679
0
      else if (strcasecmp (arg, "no") == 0)
17680
0
        omit_lock_prefix = 0;
17681
0
      else
17682
0
        as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
17683
0
      break;
17684
17685
0
    case OPTION_MFENCE_AS_LOCK_ADD:
17686
0
      if (strcasecmp (arg, "yes") == 0)
17687
0
        avoid_fence = 1;
17688
0
      else if (strcasecmp (arg, "no") == 0)
17689
0
        avoid_fence = 0;
17690
0
      else
17691
0
        as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
17692
0
      break;
17693
17694
0
    case OPTION_MLFENCE_AFTER_LOAD:
17695
0
      if (strcasecmp (arg, "yes") == 0)
17696
0
  lfence_after_load = 1;
17697
0
      else if (strcasecmp (arg, "no") == 0)
17698
0
  lfence_after_load = 0;
17699
0
      else
17700
0
        as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
17701
0
      break;
17702
17703
0
    case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
17704
0
      if (strcasecmp (arg, "all") == 0)
17705
0
  {
17706
0
    lfence_before_indirect_branch = lfence_branch_all;
17707
0
    if (lfence_before_ret == lfence_before_ret_none)
17708
0
      lfence_before_ret = lfence_before_ret_shl;
17709
0
  }
17710
0
      else if (strcasecmp (arg, "memory") == 0)
17711
0
  lfence_before_indirect_branch = lfence_branch_memory;
17712
0
      else if (strcasecmp (arg, "register") == 0)
17713
0
  lfence_before_indirect_branch = lfence_branch_register;
17714
0
      else if (strcasecmp (arg, "none") == 0)
17715
0
  lfence_before_indirect_branch = lfence_branch_none;
17716
0
      else
17717
0
        as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
17718
0
      arg);
17719
0
      break;
17720
17721
0
    case OPTION_MLFENCE_BEFORE_RET:
17722
0
      if (strcasecmp (arg, "or") == 0)
17723
0
  lfence_before_ret = lfence_before_ret_or;
17724
0
      else if (strcasecmp (arg, "not") == 0)
17725
0
  lfence_before_ret = lfence_before_ret_not;
17726
0
      else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
17727
0
  lfence_before_ret = lfence_before_ret_shl;
17728
0
      else if (strcasecmp (arg, "none") == 0)
17729
0
  lfence_before_ret = lfence_before_ret_none;
17730
0
      else
17731
0
        as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
17732
0
      arg);
17733
0
      break;
17734
17735
0
    case OPTION_MRELAX_RELOCATIONS:
17736
0
      if (strcasecmp (arg, "yes") == 0)
17737
0
        generate_relax_relocations = 1;
17738
0
      else if (strcasecmp (arg, "no") == 0)
17739
0
        generate_relax_relocations = 0;
17740
0
      else
17741
0
        as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
17742
0
      break;
17743
17744
0
    case OPTION_MALIGN_BRANCH_BOUNDARY:
17745
0
      {
17746
0
  char *end;
17747
0
  long int align = strtoul (arg, &end, 0);
17748
0
  if (*end == '\0')
17749
0
    {
17750
0
      if (align == 0)
17751
0
        {
17752
0
    align_branch_power = 0;
17753
0
    break;
17754
0
        }
17755
0
      else if (align >= 16)
17756
0
        {
17757
0
    int align_power;
17758
0
    for (align_power = 0;
17759
0
         (align & 1) == 0;
17760
0
         align >>= 1, align_power++)
17761
0
      continue;
17762
    /* Limit alignment power to 31.  */
17763
0
    if (align == 1 && align_power < 32)
17764
0
      {
17765
0
        align_branch_power = align_power;
17766
0
        break;
17767
0
      }
17768
0
        }
17769
0
    }
17770
0
  as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
17771
0
      }
17772
0
      break;
17773
17774
0
    case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
17775
0
      {
17776
0
  char *end;
17777
0
  int align = strtoul (arg, &end, 0);
17778
  /* Some processors only support 5 prefixes.  */
17779
0
  if (*end == '\0' && align >= 0 && align < 6)
17780
0
    {
17781
0
      align_branch_prefix_size = align;
17782
0
      break;
17783
0
    }
17784
0
  as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
17785
0
      arg);
17786
0
      }
17787
0
      break;
17788
17789
0
    case OPTION_MALIGN_BRANCH:
17790
0
      align_branch = 0;
17791
0
      saved = xstrdup (arg);
17792
0
      type = saved;
17793
0
      do
17794
0
  {
17795
0
    next = strchr (type, '+');
17796
0
    if (next)
17797
0
      *next++ = '\0';
17798
0
    if (strcasecmp (type, "jcc") == 0)
17799
0
      align_branch |= align_branch_jcc_bit;
17800
0
    else if (strcasecmp (type, "fused") == 0)
17801
0
      align_branch |= align_branch_fused_bit;
17802
0
    else if (strcasecmp (type, "jmp") == 0)
17803
0
      align_branch |= align_branch_jmp_bit;
17804
0
    else if (strcasecmp (type, "call") == 0)
17805
0
      align_branch |= align_branch_call_bit;
17806
0
    else if (strcasecmp (type, "ret") == 0)
17807
0
      align_branch |= align_branch_ret_bit;
17808
0
    else if (strcasecmp (type, "indirect") == 0)
17809
0
      align_branch |= align_branch_indirect_bit;
17810
0
    else
17811
0
      as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
17812
0
    type = next;
17813
0
  }
17814
0
      while (next != NULL);
17815
0
      free (saved);
17816
0
      break;
17817
17818
0
    case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
17819
0
      align_branch_power = 5;
17820
0
      align_branch_prefix_size = 5;
17821
0
      align_branch = (align_branch_jcc_bit
17822
0
          | align_branch_fused_bit
17823
0
          | align_branch_jmp_bit);
17824
0
      break;
17825
17826
0
    case OPTION_MAMD64:
17827
0
      isa64 = amd64;
17828
0
      break;
17829
17830
0
    case OPTION_MINTEL64:
17831
0
      isa64 = intel64;
17832
0
      break;
17833
17834
0
    case 'O':
17835
0
      if (arg == NULL)
17836
0
  {
17837
0
    optimize = 1;
17838
    /* Turn off -Os.  */
17839
0
    optimize_for_space = 0;
17840
0
  }
17841
0
      else if (*arg == 's')
17842
0
  {
17843
0
    optimize_for_space = 1;
17844
    /* Turn on all encoding optimizations.  */
17845
0
    optimize = INT_MAX;
17846
0
  }
17847
0
      else
17848
0
  {
17849
0
    optimize = atoi (arg);
17850
    /* Turn off -Os.  */
17851
0
    optimize_for_space = 0;
17852
0
  }
17853
0
      break;
17854
0
    case OPTION_MTLS_CHECK:
17855
0
      if (strcasecmp (arg, "yes") == 0)
17856
0
  tls_check = true;
17857
0
      else if (strcasecmp (arg, "no") == 0)
17858
0
  tls_check = false;
17859
0
      else
17860
0
  as_fatal (_("invalid -mtls-check= option: `%s'"), arg);
17861
0
      break;
17862
17863
0
    default:
17864
0
      return 0;
17865
0
    }
17866
0
  return 1;
17867
0
}
17868
17869
0
#define MESSAGE_TEMPLATE \
17870
0
"                                                                                "
17871
17872
static char *
17873
output_message (FILE *stream, char *p, char *message, char *start,
17874
    int *left_p, const char *name, int len)
17875
0
{
17876
0
  int size = sizeof (MESSAGE_TEMPLATE);
17877
0
  int left = *left_p;
17878
17879
  /* Reserve 2 spaces for ", " or ",\0" */
17880
0
  left -= len + 2;
17881
17882
  /* Check if there is any room.  */
17883
0
  if (left >= 0)
17884
0
    {
17885
0
      if (p != start)
17886
0
  {
17887
0
    *p++ = ',';
17888
0
    *p++ = ' ';
17889
0
  }
17890
0
      p = mempcpy (p, name, len);
17891
0
    }
17892
0
  else
17893
0
    {
17894
      /* Output the current message now and start a new one.  */
17895
0
      *p++ = ',';
17896
0
      *p = '\0';
17897
0
      fprintf (stream, "%s\n", message);
17898
0
      p = start;
17899
0
      left = size - (start - message) - len - 2;
17900
17901
0
      gas_assert (left >= 0);
17902
17903
0
      p = mempcpy (p, name, len);
17904
0
    }
17905
17906
0
  *left_p = left;
17907
0
  return p;
17908
0
}
17909
17910
static void
17911
show_arch (FILE *stream, int ext, int check)
17912
0
{
17913
0
  static char message[] = MESSAGE_TEMPLATE;
17914
0
  char *start = message + 27;
17915
0
  char *p;
17916
0
  int size = sizeof (MESSAGE_TEMPLATE);
17917
0
  int left;
17918
0
  const char *name;
17919
0
  int len;
17920
0
  unsigned int j;
17921
17922
0
  p = start;
17923
0
  left = size - (start - message);
17924
17925
0
  if (!ext && check)
17926
0
    {
17927
0
      p = output_message (stream, p, message, start, &left,
17928
0
        STRING_COMMA_LEN ("default"));
17929
0
      p = output_message (stream, p, message, start, &left,
17930
0
        STRING_COMMA_LEN ("push"));
17931
0
      p = output_message (stream, p, message, start, &left,
17932
0
        STRING_COMMA_LEN ("pop"));
17933
0
    }
17934
17935
0
  for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
17936
0
    {
17937
      /* Should it be skipped?  */
17938
0
      if (cpu_arch [j].skip)
17939
0
  continue;
17940
17941
0
      name = cpu_arch [j].name;
17942
0
      len = cpu_arch [j].len;
17943
0
      if (cpu_arch[j].type == PROCESSOR_NONE)
17944
0
  {
17945
    /* It is an extension.  Skip if we aren't asked to show it.  */
17946
0
    if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
17947
0
      continue;
17948
0
  }
17949
0
      else if (ext)
17950
0
  {
17951
    /* It is an processor.  Skip if we show only extension.  */
17952
0
    continue;
17953
0
  }
17954
0
      else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
17955
0
  {
17956
    /* It is an impossible processor - skip.  */
17957
0
    continue;
17958
0
  }
17959
17960
0
      p = output_message (stream, p, message, start, &left, name, len);
17961
0
    }
17962
17963
  /* Display disabled extensions.  */
17964
0
  if (ext)
17965
0
    for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
17966
0
      {
17967
0
  char *str;
17968
17969
0
  if (cpu_arch[j].type != PROCESSOR_NONE
17970
0
      || !cpu_flags_all_zero (&cpu_arch[j].enable))
17971
0
    continue;
17972
0
  str = xasprintf ("no%s", cpu_arch[j].name);
17973
0
  p = output_message (stream, p, message, start, &left, str,
17974
0
          strlen (str));
17975
0
  free (str);
17976
0
      }
17977
17978
0
  *p = '\0';
17979
0
  fprintf (stream, "%s\n", message);
17980
0
}
17981
17982
void
17983
md_show_usage (FILE *stream)
17984
0
{
17985
0
#ifdef OBJ_ELF
17986
0
  fprintf (stream, _("\
17987
0
  -Qy, -Qn                ignored\n\
17988
0
  -V                      print assembler version number\n\
17989
0
  -k                      ignored\n"));
17990
0
#endif
17991
0
  fprintf (stream, _("\
17992
0
  -n                      do not optimize code alignment\n\
17993
0
  -O{012s}                attempt some code optimizations\n\
17994
0
  -q                      quieten some warnings\n"));
17995
0
#ifdef OBJ_ELF
17996
0
  fprintf (stream, _("\
17997
0
  -s                      ignored\n"));
17998
0
#endif
17999
0
#ifdef BFD64
18000
0
# ifdef OBJ_ELF
18001
0
  fprintf (stream, _("\
18002
0
  --32/--64/--x32         generate 32bit/64bit/x32 object\n"));
18003
# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
18004
  fprintf (stream, _("\
18005
  --32/--64               generate 32bit/64bit object\n"));
18006
# endif
18007
0
#endif
18008
#ifdef SVR4_COMMENT_CHARS
18009
  fprintf (stream, _("\
18010
  --divide                do not treat `/' as a comment character\n"));
18011
#else
18012
0
  fprintf (stream, _("\
18013
0
  --divide                ignored\n"));
18014
0
#endif
18015
0
  fprintf (stream, _("\
18016
0
  -march=CPU[,+EXTENSION...]\n\
18017
0
                          generate code for CPU and EXTENSION, CPU is one of:\n"));
18018
0
  show_arch (stream, 0, 1);
18019
0
  fprintf (stream, _("\
18020
0
                          EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
18021
0
  show_arch (stream, 1, 0);
18022
0
  fprintf (stream, _("\
18023
0
  -mtune=CPU              optimize for CPU, CPU is one of:\n"));
18024
0
  show_arch (stream, 0, 0);
18025
0
  fprintf (stream, _("\
18026
0
  -msse2avx               encode SSE instructions with VEX prefix\n"));
18027
0
  fprintf (stream, _("\
18028
0
  -muse-unaligned-vector-move\n\
18029
0
                          encode aligned vector move as unaligned vector move\n"));
18030
0
  fprintf (stream, _("\
18031
0
  -msse-check=[none|error|warning] (default: none)\n\
18032
0
                          check SSE instructions\n"));
18033
0
  fprintf (stream, _("\
18034
0
  -moperand-check=[none|error|warning] (default: warning)\n\
18035
0
                          check operand combinations for validity\n"));
18036
0
  fprintf (stream, _("\
18037
0
  -mavxscalar=[128|256] (default: 128)\n\
18038
0
                          encode scalar AVX instructions with specific vector\n\
18039
0
                           length\n"));
18040
0
  fprintf (stream, _("\
18041
0
  -mvexwig=[0|1] (default: 0)\n\
18042
0
                          encode VEX instructions with specific VEX.W value\n\
18043
0
                           for VEX.W bit ignored instructions\n"));
18044
0
  fprintf (stream, _("\
18045
0
  -mevexlig=[128|256|512] (default: 128)\n\
18046
0
                          encode scalar EVEX instructions with specific vector\n\
18047
0
                           length\n"));
18048
0
  fprintf (stream, _("\
18049
0
  -mevexwig=[0|1] (default: 0)\n\
18050
0
                          encode EVEX instructions with specific EVEX.W value\n\
18051
0
                           for EVEX.W bit ignored instructions\n"));
18052
0
  fprintf (stream, _("\
18053
0
  -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
18054
0
                          encode EVEX instructions with specific EVEX.RC value\n\
18055
0
                           for SAE-only ignored instructions\n"));
18056
0
  fprintf (stream, _("\
18057
0
  -mmnemonic=[att|intel] "));
18058
0
  if (SYSV386_COMPAT)
18059
0
    fprintf (stream, _("(default: att)\n"));
18060
0
  else
18061
0
    fprintf (stream, _("(default: intel)\n"));
18062
0
  fprintf (stream, _("\
18063
0
                          use AT&T/Intel mnemonic (AT&T syntax only)\n"));
18064
0
  fprintf (stream, _("\
18065
0
  -msyntax=[att|intel] (default: att)\n\
18066
0
                          use AT&T/Intel syntax\n"));
18067
0
  fprintf (stream, _("\
18068
0
  -mindex-reg             support pseudo index registers\n"));
18069
0
  fprintf (stream, _("\
18070
0
  -mnaked-reg             don't require `%%' prefix for registers\n"));
18071
0
  fprintf (stream, _("\
18072
0
  -madd-bnd-prefix        add BND prefix for all valid branches\n"));
18073
0
#ifdef OBJ_ELF
18074
0
  fprintf (stream, _("\
18075
0
  -mshared                disable branch optimization for shared code\n"));
18076
0
  fprintf (stream, _("\
18077
0
  -mx86-used-note=[no|yes] "));
18078
0
  if (DEFAULT_X86_USED_NOTE)
18079
0
    fprintf (stream, _("(default: yes)\n"));
18080
0
  else
18081
0
    fprintf (stream, _("(default: no)\n"));
18082
0
  fprintf (stream, _("\
18083
0
                          generate x86 used ISA and feature properties\n"));
18084
0
#endif
18085
#if defined (TE_PE) || defined (TE_PEP)
18086
  fprintf (stream, _("\
18087
  -mbig-obj               generate big object files\n"));
18088
#endif
18089
0
  fprintf (stream, _("\
18090
0
  -momit-lock-prefix=[no|yes] (default: no)\n\
18091
0
                          strip all lock prefixes\n"));
18092
0
  fprintf (stream, _("\
18093
0
  -mfence-as-lock-add=[no|yes] (default: no)\n\
18094
0
                          encode lfence, mfence and sfence as\n\
18095
0
                           lock addl $0x0, (%%{re}sp)\n"));
18096
0
  fprintf (stream, _("\
18097
0
  -mrelax-relocations=[no|yes] "));
18098
0
  if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
18099
0
    fprintf (stream, _("(default: yes)\n"));
18100
0
  else
18101
0
    fprintf (stream, _("(default: no)\n"));
18102
0
  fprintf (stream, _("\
18103
0
                          generate relax relocations\n"));
18104
0
#ifdef OBJ_ELF
18105
0
  fprintf (stream, _("\
18106
0
  -mtls-check=[no|yes] "));
18107
0
  if (DEFAULT_X86_TLS_CHECK)
18108
0
    fprintf (stream, _("(default: yes)\n"));
18109
0
  else
18110
0
    fprintf (stream, _("(default: no)\n"));
18111
0
  fprintf (stream, _("\
18112
0
                          check TLS relocation\n"));
18113
0
#endif
18114
0
  fprintf (stream, _("\
18115
0
  -malign-branch-boundary=NUM (default: 0)\n\
18116
0
                          align branches within NUM byte boundary\n"));
18117
0
  fprintf (stream, _("\
18118
0
  -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
18119
0
                          TYPE is combination of jcc, fused, jmp, call, ret,\n\
18120
0
                           indirect\n\
18121
0
                          specify types of branches to align\n"));
18122
0
  fprintf (stream, _("\
18123
0
  -malign-branch-prefix-size=NUM (default: 5)\n\
18124
0
                          align branches with NUM prefixes per instruction\n"));
18125
0
  fprintf (stream, _("\
18126
0
  -mbranches-within-32B-boundaries\n\
18127
0
                          align branches within 32 byte boundary\n"));
18128
0
  fprintf (stream, _("\
18129
0
  -mlfence-after-load=[no|yes] (default: no)\n\
18130
0
                          generate lfence after load\n"));
18131
0
  fprintf (stream, _("\
18132
0
  -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
18133
0
                          generate lfence before indirect near branch\n"));
18134
0
  fprintf (stream, _("\
18135
0
  -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
18136
0
                          generate lfence before ret\n"));
18137
0
  fprintf (stream, _("\
18138
0
  -mamd64                 accept only AMD64 ISA [default]\n"));
18139
0
  fprintf (stream, _("\
18140
0
  -mintel64               accept only Intel64 ISA\n"));
18141
0
}
18142
18143
#if (defined (OBJ_ELF) || defined (TE_PE) || defined (OBJ_MACH_O))
18144
18145
/* Pick the target format to use.  */
18146
18147
const char *
18148
i386_target_format (void)
18149
567
{
18150
567
  if (startswith (default_arch, "x86_64"))
18151
567
    {
18152
567
      update_code_flag (CODE_64BIT, 1);
18153
567
#ifdef OBJ_ELF
18154
567
      if (default_arch[6] == '\0')
18155
567
  x86_elf_abi = X86_64_ABI;
18156
0
      else
18157
0
  x86_elf_abi = X86_64_X32_ABI;
18158
567
#endif
18159
567
    }
18160
0
  else if (!strcmp (default_arch, "i386"))
18161
0
    update_code_flag (CODE_32BIT, 1);
18162
0
  else if (!strcmp (default_arch, "iamcu"))
18163
0
    {
18164
0
      update_code_flag (CODE_32BIT, 1);
18165
0
      if (cpu_arch_isa == PROCESSOR_UNKNOWN)
18166
0
  {
18167
0
    static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
18168
0
    cpu_arch_name = "iamcu";
18169
0
    free (cpu_sub_arch_name);
18170
0
    cpu_sub_arch_name = NULL;
18171
0
    cpu_arch_flags = iamcu_flags;
18172
0
    cpu_arch_isa = PROCESSOR_IAMCU;
18173
0
    cpu_arch_isa_flags = iamcu_flags;
18174
0
    if (!cpu_arch_tune_set)
18175
0
      cpu_arch_tune = PROCESSOR_IAMCU;
18176
0
  }
18177
0
      else if (cpu_arch_isa != PROCESSOR_IAMCU)
18178
0
  as_fatal (_("Intel MCU doesn't support `%s' architecture"),
18179
0
      cpu_arch_name);
18180
0
    }
18181
0
  else
18182
0
    as_fatal (_("unknown architecture"));
18183
18184
567
#ifdef OBJ_ELF
18185
567
  if (flag_synth_cfi && x86_elf_abi != X86_64_ABI)
18186
0
    as_fatal (_("SCFI is not supported for this ABI"));
18187
567
#endif
18188
18189
567
  if (cpu_flags_all_zero (&cpu_arch_isa_flags))
18190
1
    cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
18191
18192
567
  switch (OUTPUT_FLAVOR)
18193
567
    {
18194
#ifdef TE_PE
18195
    case bfd_target_coff_flavour:
18196
      if (flag_code == CODE_64BIT)
18197
  {
18198
    object_64bit = 1;
18199
    return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
18200
  }
18201
      return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
18202
#endif
18203
0
#ifdef OBJ_ELF
18204
567
    case bfd_target_elf_flavour:
18205
567
      {
18206
567
  const char *format;
18207
18208
567
  switch (x86_elf_abi)
18209
567
    {
18210
0
    default:
18211
0
      format = ELF_TARGET_FORMAT;
18212
0
#ifndef TE_SOLARIS
18213
0
      tls_get_addr = "___tls_get_addr";
18214
0
#endif
18215
0
      break;
18216
567
    case X86_64_ABI:
18217
567
      use_rela_relocations = 1;
18218
567
      object_64bit = 1;
18219
567
#ifndef TE_SOLARIS
18220
567
      tls_get_addr = "__tls_get_addr";
18221
567
#endif
18222
567
      format = ELF_TARGET_FORMAT64;
18223
567
      break;
18224
0
    case X86_64_X32_ABI:
18225
0
      use_rela_relocations = 1;
18226
0
      object_64bit = 1;
18227
0
#ifndef TE_SOLARIS
18228
0
      tls_get_addr = "__tls_get_addr";
18229
0
#endif
18230
0
      disallow_64bit_reloc = 1;
18231
0
      format = ELF_TARGET_FORMAT32;
18232
0
      break;
18233
567
    }
18234
567
  if (cpu_arch_isa == PROCESSOR_IAMCU)
18235
0
    {
18236
0
      if (x86_elf_abi != I386_ABI)
18237
0
        as_fatal (_("Intel MCU is 32bit only"));
18238
0
      return ELF_TARGET_IAMCU_FORMAT;
18239
0
    }
18240
567
  else
18241
567
    return format;
18242
567
      }
18243
0
#endif
18244
#if defined (OBJ_MACH_O)
18245
    case bfd_target_mach_o_flavour:
18246
      if (flag_code == CODE_64BIT)
18247
  {
18248
    use_rela_relocations = 1;
18249
    object_64bit = 1;
18250
    return "mach-o-x86-64";
18251
  }
18252
      else
18253
  return "mach-o-i386";
18254
#endif
18255
0
    default:
18256
0
      abort ();
18257
0
      return NULL;
18258
567
    }
18259
567
}
18260
18261
#endif /* ELF / PE / MACH_O  */
18262

18263
#ifdef OBJ_ELF
18264
symbolS *
18265
md_undefined_symbol (char *name)
18266
3.71k
{
18267
3.71k
  if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
18268
57
      && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
18269
15
      && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
18270
15
      && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
18271
15
    {
18272
15
      if (!GOT_symbol)
18273
15
  {
18274
15
    if (symbol_find (name))
18275
0
      as_bad (_("GOT already in symbol table"));
18276
15
    GOT_symbol = symbol_new (name, undefined_section,
18277
15
           &zero_address_frag, 0);
18278
15
  };
18279
15
      return GOT_symbol;
18280
15
    }
18281
3.69k
  return NULL;
18282
3.71k
}
18283
#endif
18284
18285
#ifdef OBJ_AOUT
18286
/* Round up a section size to the appropriate boundary.  */
18287
18288
valueT
18289
md_section_align (segT segment, valueT size)
18290
{
18291
  /* For a.out, force the section size to be aligned.  If we don't do
18292
     this, BFD will align it for us, but it will not write out the
18293
     final bytes of the section.  This may be a bug in BFD, but it is
18294
     easier to fix it here since that is how the other a.out targets
18295
     work.  */
18296
  int align = bfd_section_alignment (segment);
18297
18298
  return (size + ((valueT) 1 << align) - 1) & -((valueT) 1 << align);
18299
}
18300
#endif
18301
18302
/* On the i386, PC-relative offsets are relative to the start of the
18303
   next instruction.  That is, the address of the offset, plus its
18304
   size, since the offset is always the last part of the insn.  */
18305
18306
long
18307
md_pcrel_from (fixS *fixP)
18308
0
{
18309
0
  return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
18310
0
}
18311
18312
#ifdef OBJ_AOUT
18313
18314
static void
18315
s_bss (int ignore ATTRIBUTE_UNUSED)
18316
{
18317
  int temp;
18318
18319
  temp = get_absolute_expression ();
18320
  subseg_set (bss_section, temp);
18321
  demand_empty_rest_of_line ();
18322
}
18323
18324
#endif
18325
18326
/* Remember constant directive.  */
18327
18328
void
18329
i386_cons_align (int ignore ATTRIBUTE_UNUSED)
18330
15.6k
{
18331
15.6k
  struct last_insn *last_insn
18332
15.6k
    = &seg_info(now_seg)->tc_segment_info_data.last_insn;
18333
18334
15.6k
  if (bfd_section_flags (now_seg) & SEC_CODE)
18335
8.02k
    {
18336
8.02k
      last_insn->kind = last_insn_directive;
18337
8.02k
      last_insn->name = "constant directive";
18338
8.02k
      last_insn->file = as_where (&last_insn->line);
18339
8.02k
    }
18340
15.6k
}
18341
18342
int
18343
i386_validate_fix (fixS *fixp)
18344
0
{
18345
0
  if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
18346
0
    {
18347
0
      reloc_howto_type *howto;
18348
18349
0
      howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
18350
0
      as_bad_where (fixp->fx_file, fixp->fx_line,
18351
0
        _("invalid %s relocation against register"),
18352
0
        howto ? howto->name : "<unknown>");
18353
0
      return 0;
18354
0
    }
18355
18356
0
#ifdef OBJ_ELF
18357
0
  if (fixp->fx_r_type == BFD_RELOC_SIZE32
18358
0
      || fixp->fx_r_type == BFD_RELOC_SIZE64)
18359
0
    return fixp->fx_addsy
18360
0
     && (!S_IS_DEFINED (fixp->fx_addsy)
18361
0
         || S_IS_EXTERNAL (fixp->fx_addsy));
18362
18363
  /* BFD_RELOC_X86_64_GOTTPOFF:
18364
      1. fx_tcbit -> BFD_RELOC_X86_64_CODE_4_GOTTPOFF
18365
      2. fx_tcbit2 -> BFD_RELOC_X86_64_CODE_5_GOTTPOFF
18366
      3. fx_tcbit3 -> BFD_RELOC_X86_64_CODE_6_GOTTPOFF
18367
    BFD_RELOC_X86_64_GOTPC32_TLSDESC:
18368
      1. fx_tcbit -> BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC
18369
    BFD_RELOC_32_PCREL:
18370
      1. fx_tcbit && fx_tcbit3 -> BFD_RELOC_X86_64_CODE_5_GOTPCRELX
18371
      2. fx_tcbit -> BFD_RELOC_X86_64_GOTPCRELX
18372
      3. fx_tcbit2 && fx_tcbit3 -> BFD_RELOC_X86_64_CODE_6_GOTPCRELX
18373
      4. fx_tcbit2 -> BFD_RELOC_X86_64_REX_GOTPCRELX
18374
      5. fx_tcbit3 -> BFD_RELOC_X86_64_CODE_4_GOTPCRELX
18375
      6. else -> BFD_RELOC_X86_64_GOTPCREL
18376
   */
18377
0
  if (fixp->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF)
18378
0
    {
18379
0
      if (fixp->fx_tcbit)
18380
0
  fixp->fx_r_type = BFD_RELOC_X86_64_CODE_4_GOTTPOFF;
18381
0
      else if (fixp->fx_tcbit2)
18382
0
  fixp->fx_r_type = BFD_RELOC_X86_64_CODE_5_GOTTPOFF;
18383
0
      else if (fixp->fx_tcbit3)
18384
0
  fixp->fx_r_type = BFD_RELOC_X86_64_CODE_6_GOTTPOFF;
18385
0
    }
18386
0
  else if (fixp->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
18387
0
     && fixp->fx_tcbit)
18388
0
    fixp->fx_r_type = BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC;
18389
0
#endif
18390
18391
0
  if (fixp->fx_subsy)
18392
0
    {
18393
0
      if (fixp->fx_subsy == GOT_symbol)
18394
0
  {
18395
0
    if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
18396
0
      {
18397
0
        if (!object_64bit)
18398
0
    abort ();
18399
0
#ifdef OBJ_ELF
18400
0
        if (fixp->fx_tcbit)
18401
0
    fixp->fx_r_type = fixp->fx_tcbit3
18402
0
          ? BFD_RELOC_X86_64_CODE_5_GOTPCRELX
18403
0
          : BFD_RELOC_X86_64_GOTPCRELX;
18404
0
        else if (fixp->fx_tcbit2)
18405
0
    fixp->fx_r_type = fixp->fx_tcbit3
18406
0
          ? BFD_RELOC_X86_64_CODE_6_GOTPCRELX
18407
0
          : BFD_RELOC_X86_64_REX_GOTPCRELX;
18408
0
        else if (fixp->fx_tcbit3)
18409
0
    fixp->fx_r_type = BFD_RELOC_X86_64_CODE_4_GOTPCRELX;
18410
0
        else
18411
0
#endif
18412
0
    fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
18413
0
      }
18414
0
    else
18415
0
      {
18416
0
        if (!object_64bit)
18417
0
    fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
18418
0
        else
18419
0
    fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
18420
0
      }
18421
0
    fixp->fx_subsy = 0;
18422
0
  }
18423
0
    }
18424
0
#ifdef OBJ_ELF
18425
0
  else
18426
0
    {
18427
      /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
18428
   to section.  Since PLT32 relocation must be against symbols,
18429
   turn such PLT32 relocation into PC32 relocation.  NB: We can
18430
   turn PLT32 relocation into PC32 relocation only for PC-relative
18431
   relocations since non-PC-relative relocations need PLT entries.
18432
       */
18433
0
      if (fixp->fx_addsy
18434
0
    && fixp->fx_pcrel
18435
0
    && (fixp->fx_r_type == BFD_RELOC_386_PLT32
18436
0
        || fixp->fx_r_type == BFD_RELOC_32_PLT_PCREL)
18437
0
    && symbol_section_p (fixp->fx_addsy))
18438
0
  fixp->fx_r_type = BFD_RELOC_32_PCREL;
18439
0
      if (!object_64bit)
18440
0
  {
18441
0
    if (fixp->fx_r_type == BFD_RELOC_386_GOT32
18442
0
        && fixp->fx_tcbit2)
18443
0
      fixp->fx_r_type = BFD_RELOC_386_GOT32X;
18444
0
  }
18445
0
    }
18446
0
#endif
18447
18448
0
  return 1;
18449
0
}
18450
18451
arelent *
18452
tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
18453
0
{
18454
0
  arelent *rel;
18455
0
  bfd_reloc_code_real_type code;
18456
18457
0
  switch (fixp->fx_r_type)
18458
0
    {
18459
0
#ifdef OBJ_ELF
18460
0
      symbolS *sym;
18461
18462
0
    case BFD_RELOC_SIZE32:
18463
0
    case BFD_RELOC_SIZE64:
18464
0
      if (fixp->fx_addsy
18465
0
    && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
18466
0
    && (!fixp->fx_subsy
18467
0
        || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
18468
0
  sym = fixp->fx_addsy;
18469
0
      else if (fixp->fx_subsy
18470
0
         && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
18471
0
         && (!fixp->fx_addsy
18472
0
       || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
18473
0
  sym = fixp->fx_subsy;
18474
0
      else
18475
0
  sym = NULL;
18476
0
      if (sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
18477
0
  {
18478
    /* Resolve size relocation against local symbol to size of
18479
       the symbol plus addend.  */
18480
0
    valueT value = S_GET_SIZE (sym);
18481
18482
0
    if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
18483
0
      value = bfd_section_size (S_GET_SEGMENT (sym));
18484
0
    if (sym == fixp->fx_subsy)
18485
0
      {
18486
0
        value = -value;
18487
0
        if (fixp->fx_addsy)
18488
0
          value += S_GET_VALUE (fixp->fx_addsy);
18489
0
      }
18490
0
    else if (fixp->fx_subsy)
18491
0
      value -= S_GET_VALUE (fixp->fx_subsy);
18492
0
    value += fixp->fx_offset;
18493
0
    if (fixp->fx_r_type == BFD_RELOC_SIZE32
18494
0
        && object_64bit
18495
0
        && !fits_in_unsigned_long (value))
18496
0
      as_bad_where (fixp->fx_file, fixp->fx_line,
18497
0
        _("symbol size computation overflow"));
18498
0
    fixp->fx_addsy = NULL;
18499
0
    fixp->fx_subsy = NULL;
18500
0
    md_apply_fix (fixp, &value, NULL);
18501
0
    return NULL;
18502
0
  }
18503
0
      if (!fixp->fx_addsy || fixp->fx_subsy)
18504
0
  {
18505
0
    as_bad_where (fixp->fx_file, fixp->fx_line,
18506
0
      "unsupported expression involving @size");
18507
0
    return NULL;
18508
0
  }
18509
0
#endif
18510
      /* Fall through.  */
18511
18512
0
    case BFD_RELOC_32_PLT_PCREL:
18513
0
    case BFD_RELOC_X86_64_GOT32:
18514
0
    case BFD_RELOC_X86_64_GOTPCREL:
18515
0
    case BFD_RELOC_X86_64_GOTPCRELX:
18516
0
    case BFD_RELOC_X86_64_REX_GOTPCRELX:
18517
0
    case BFD_RELOC_X86_64_CODE_4_GOTPCRELX:
18518
0
    case BFD_RELOC_X86_64_CODE_5_GOTPCRELX:
18519
0
    case BFD_RELOC_X86_64_CODE_6_GOTPCRELX:
18520
0
    case BFD_RELOC_386_PLT32:
18521
0
    case BFD_RELOC_386_GOT32:
18522
0
    case BFD_RELOC_386_GOT32X:
18523
0
    case BFD_RELOC_386_GOTOFF:
18524
0
    case BFD_RELOC_386_GOTPC:
18525
0
    case BFD_RELOC_386_TLS_GD:
18526
0
    case BFD_RELOC_386_TLS_LDM:
18527
0
    case BFD_RELOC_386_TLS_LDO_32:
18528
0
    case BFD_RELOC_386_TLS_IE_32:
18529
0
    case BFD_RELOC_386_TLS_IE:
18530
0
    case BFD_RELOC_386_TLS_GOTIE:
18531
0
    case BFD_RELOC_386_TLS_LE_32:
18532
0
    case BFD_RELOC_386_TLS_LE:
18533
0
    case BFD_RELOC_386_TLS_GOTDESC:
18534
0
    case BFD_RELOC_386_TLS_DESC_CALL:
18535
0
    case BFD_RELOC_X86_64_TLSGD:
18536
0
    case BFD_RELOC_X86_64_TLSLD:
18537
0
    case BFD_RELOC_X86_64_DTPOFF32:
18538
0
    case BFD_RELOC_X86_64_DTPOFF64:
18539
0
    case BFD_RELOC_X86_64_GOTTPOFF:
18540
0
    case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
18541
0
    case BFD_RELOC_X86_64_CODE_5_GOTTPOFF:
18542
0
    case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
18543
0
    case BFD_RELOC_X86_64_TPOFF32:
18544
0
    case BFD_RELOC_X86_64_TPOFF64:
18545
0
    case BFD_RELOC_X86_64_GOTOFF64:
18546
0
    case BFD_RELOC_X86_64_GOTPC32:
18547
0
    case BFD_RELOC_X86_64_GOT64:
18548
0
    case BFD_RELOC_X86_64_GOTPCREL64:
18549
0
    case BFD_RELOC_X86_64_GOTPC64:
18550
0
    case BFD_RELOC_X86_64_GOTPLT64:
18551
0
    case BFD_RELOC_64_PLTOFF:
18552
0
    case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
18553
0
    case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
18554
0
    case BFD_RELOC_X86_64_CODE_5_GOTPC32_TLSDESC:
18555
0
    case BFD_RELOC_X86_64_CODE_6_GOTPC32_TLSDESC:
18556
0
    case BFD_RELOC_X86_64_TLSDESC_CALL:
18557
0
    case BFD_RELOC_RVA:
18558
0
    case BFD_RELOC_VTABLE_ENTRY:
18559
0
    case BFD_RELOC_VTABLE_INHERIT:
18560
#ifdef TE_PE
18561
    case BFD_RELOC_32_SECREL:
18562
    case BFD_RELOC_16_SECIDX:
18563
#endif
18564
0
      code = fixp->fx_r_type;
18565
0
      break;
18566
0
    case BFD_RELOC_X86_64_32S:
18567
0
      if (!fixp->fx_pcrel)
18568
0
  {
18569
    /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32.  */
18570
0
    code = fixp->fx_r_type;
18571
0
    break;
18572
0
  }
18573
      /* Fall through.  */
18574
0
    default:
18575
0
      if (fixp->fx_pcrel)
18576
0
  {
18577
0
    switch (fixp->fx_size)
18578
0
      {
18579
0
      default:
18580
0
        as_bad_where (fixp->fx_file, fixp->fx_line,
18581
0
          _("can not do %d byte pc-relative relocation"),
18582
0
          fixp->fx_size);
18583
0
        code = BFD_RELOC_32_PCREL;
18584
0
        break;
18585
0
      case 1: code = BFD_RELOC_8_PCREL;  break;
18586
0
      case 2: code = BFD_RELOC_16_PCREL; break;
18587
0
      case 4: code = BFD_RELOC_32_PCREL; break;
18588
0
#ifdef BFD64
18589
0
      case 8: code = BFD_RELOC_64_PCREL; break;
18590
0
#endif
18591
0
      }
18592
0
  }
18593
0
      else
18594
0
  {
18595
0
    switch (fixp->fx_size)
18596
0
      {
18597
0
      default:
18598
0
        as_bad_where (fixp->fx_file, fixp->fx_line,
18599
0
          _("can not do %d byte relocation"),
18600
0
          fixp->fx_size);
18601
0
        code = BFD_RELOC_32;
18602
0
        break;
18603
0
      case 1: code = BFD_RELOC_8;  break;
18604
0
      case 2: code = BFD_RELOC_16; break;
18605
0
      case 4: code = BFD_RELOC_32; break;
18606
0
#ifdef BFD64
18607
0
      case 8: code = BFD_RELOC_64; break;
18608
0
#endif
18609
0
      }
18610
0
  }
18611
0
      break;
18612
0
    }
18613
18614
0
  if ((code == BFD_RELOC_32
18615
0
       || code == BFD_RELOC_32_PCREL
18616
0
       || code == BFD_RELOC_X86_64_32S)
18617
0
      && GOT_symbol
18618
0
      && fixp->fx_addsy == GOT_symbol)
18619
0
    {
18620
0
      if (!object_64bit)
18621
0
  code = BFD_RELOC_386_GOTPC;
18622
0
      else
18623
0
  code = BFD_RELOC_X86_64_GOTPC32;
18624
0
    }
18625
0
  if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
18626
0
      && GOT_symbol
18627
0
      && fixp->fx_addsy == GOT_symbol)
18628
0
    {
18629
0
      code = BFD_RELOC_X86_64_GOTPC64;
18630
0
    }
18631
18632
0
  rel = notes_alloc (sizeof (arelent));
18633
0
  rel->sym_ptr_ptr = notes_alloc (sizeof (asymbol *));
18634
0
  *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
18635
18636
0
  rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
18637
18638
0
  if (!use_rela_relocations)
18639
0
    {
18640
      /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
18641
   vtable entry to be used in the relocation's section offset.  */
18642
0
      if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
18643
0
  rel->address = fixp->fx_offset;
18644
#if defined (OBJ_COFF) && defined (TE_PE)
18645
      else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
18646
  rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
18647
      else
18648
#endif
18649
0
      rel->addend = 0;
18650
0
    }
18651
  /* Use the rela in 64bit mode.  */
18652
0
  else
18653
0
    {
18654
0
      if (disallow_64bit_reloc)
18655
0
  switch (code)
18656
0
    {
18657
0
    case BFD_RELOC_X86_64_DTPOFF64:
18658
0
    case BFD_RELOC_X86_64_TPOFF64:
18659
0
    case BFD_RELOC_64_PCREL:
18660
0
    case BFD_RELOC_X86_64_GOTOFF64:
18661
0
    case BFD_RELOC_X86_64_GOT64:
18662
0
    case BFD_RELOC_X86_64_GOTPCREL64:
18663
0
    case BFD_RELOC_X86_64_GOTPC64:
18664
0
    case BFD_RELOC_X86_64_GOTPLT64:
18665
0
    case BFD_RELOC_64_PLTOFF:
18666
0
      as_bad_where (fixp->fx_file, fixp->fx_line,
18667
0
        _("cannot represent relocation type %s in x32 mode"),
18668
0
        bfd_get_reloc_code_name (code));
18669
0
      break;
18670
0
    default:
18671
0
      break;
18672
0
    }
18673
18674
0
      if (!fixp->fx_pcrel)
18675
0
  rel->addend = fixp->fx_offset;
18676
0
      else
18677
0
  switch (code)
18678
0
    {
18679
0
    case BFD_RELOC_32_PLT_PCREL:
18680
0
    case BFD_RELOC_X86_64_GOT32:
18681
0
    case BFD_RELOC_X86_64_GOTPCREL:
18682
0
    case BFD_RELOC_X86_64_GOTPCRELX:
18683
0
    case BFD_RELOC_X86_64_REX_GOTPCRELX:
18684
0
    case BFD_RELOC_X86_64_CODE_4_GOTPCRELX:
18685
0
    case BFD_RELOC_X86_64_CODE_5_GOTPCRELX:
18686
0
    case BFD_RELOC_X86_64_CODE_6_GOTPCRELX:
18687
0
    case BFD_RELOC_X86_64_TLSGD:
18688
0
    case BFD_RELOC_X86_64_TLSLD:
18689
0
    case BFD_RELOC_X86_64_GOTTPOFF:
18690
0
    case BFD_RELOC_X86_64_CODE_4_GOTTPOFF:
18691
0
    case BFD_RELOC_X86_64_CODE_5_GOTTPOFF:
18692
0
    case BFD_RELOC_X86_64_CODE_6_GOTTPOFF:
18693
0
    case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
18694
0
    case BFD_RELOC_X86_64_CODE_4_GOTPC32_TLSDESC:
18695
0
    case BFD_RELOC_X86_64_CODE_5_GOTPC32_TLSDESC:
18696
0
    case BFD_RELOC_X86_64_CODE_6_GOTPC32_TLSDESC:
18697
0
    case BFD_RELOC_X86_64_TLSDESC_CALL:
18698
0
      rel->addend = fixp->fx_offset - fixp->fx_size;
18699
0
      break;
18700
0
    default:
18701
0
      rel->addend = (section->vma
18702
0
         - fixp->fx_size
18703
0
         + fixp->fx_addnumber
18704
0
         + md_pcrel_from (fixp));
18705
0
      break;
18706
0
    }
18707
0
    }
18708
18709
0
  rel->howto = bfd_reloc_type_lookup (stdoutput, code);
18710
0
  if (rel->howto == NULL)
18711
0
    {
18712
0
      as_bad_where (fixp->fx_file, fixp->fx_line,
18713
0
        _("cannot represent relocation type %s"),
18714
0
        bfd_get_reloc_code_name (code));
18715
      /* Set howto to a garbage value so that we can keep going.  */
18716
0
      rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
18717
0
      gas_assert (rel->howto != NULL);
18718
0
    }
18719
18720
0
  return rel;
18721
0
}
18722
18723
#include "tc-i386-intel.c"
18724
18725
void
18726
tc_x86_parse_to_dw2regnum (expressionS *exp)
18727
732
{
18728
732
  int saved_naked_reg;
18729
732
  char saved_register_dot;
18730
18731
732
  saved_naked_reg = allow_naked_reg;
18732
732
  allow_naked_reg = 1;
18733
732
  saved_register_dot = register_chars['.'];
18734
732
  register_chars['.'] = '.';
18735
732
  allow_pseudo_reg = 1;
18736
732
  expression_and_evaluate (exp);
18737
732
  allow_pseudo_reg = 0;
18738
732
  register_chars['.'] = saved_register_dot;
18739
732
  allow_naked_reg = saved_naked_reg;
18740
18741
732
  if (exp->X_op == O_register && exp->X_add_number >= 0)
18742
0
    {
18743
0
      exp->X_op = O_illegal;
18744
0
      if ((addressT) exp->X_add_number < i386_regtab_size)
18745
0
  {
18746
0
    exp->X_add_number = i386_regtab[exp->X_add_number]
18747
0
            .dw2_regnum[object_64bit];
18748
0
    if (exp->X_add_number != Dw2Inval)
18749
0
      exp->X_op = O_constant;
18750
0
  }
18751
0
    }
18752
732
}
18753
18754
void
18755
tc_x86_frame_initial_instructions (void)
18756
90
{
18757
90
  cfi_add_CFA_def_cfa (object_64bit ? REG_SP : 4, -x86_cie_data_alignment);
18758
90
  cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
18759
90
}
18760
18761
int
18762
x86_dwarf2_addr_size (void)
18763
57
{
18764
57
#ifdef OBJ_ELF
18765
57
  if (x86_elf_abi == X86_64_X32_ABI)
18766
0
    return 4;
18767
57
#endif
18768
57
  return bfd_arch_bits_per_address (stdoutput) / 8;
18769
57
}
18770
18771
#ifdef TE_PE
18772
void
18773
tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
18774
{
18775
  expressionS exp;
18776
18777
  exp.X_op = O_secrel;
18778
  exp.X_add_symbol = symbol;
18779
  exp.X_add_number = 0;
18780
  emit_expr (&exp, size);
18781
}
18782
#endif
18783
18784
#ifdef OBJ_ELF
18785
int
18786
i386_elf_section_type (const char *str, size_t len)
18787
250
{
18788
250
  if (flag_code == CODE_64BIT
18789
250
      && len == sizeof ("unwind") - 1
18790
16
      && startswith (str, "unwind"))
18791
6
    return SHT_X86_64_UNWIND;
18792
18793
244
  return -1;
18794
250
}
18795
18796
void
18797
i386_elf_section_change_hook (void)
18798
13.4k
{
18799
13.4k
  struct i386_segment_info *info = &seg_info(now_seg)->tc_segment_info_data;
18800
13.4k
  struct i386_segment_info *curr, *prev;
18801
18802
13.4k
  if (info->subseg == now_subseg)
18803
13.3k
    return;
18804
18805
  /* Find the (or make a) list entry to save state into.  */
18806
167
  for (prev = info; (curr = prev->next) != NULL; prev = curr)
18807
117
    if (curr->subseg == info->subseg)
18808
8
      break;
18809
58
  if (!curr)
18810
50
    {
18811
50
      curr = notes_alloc (sizeof (*curr));
18812
50
      curr->subseg = info->subseg;
18813
50
      curr->next = NULL;
18814
50
      prev->next = curr;
18815
50
    }
18816
58
  curr->last_insn = info->last_insn;
18817
18818
  /* Find the list entry to load state from.  */
18819
194
  for (curr = info->next; curr; curr = curr->next)
18820
151
    if (curr->subseg == now_subseg)
18821
15
      break;
18822
58
  if (curr)
18823
15
    info->last_insn = curr->last_insn;
18824
43
  else
18825
43
    memset (&info->last_insn, 0, sizeof (info->last_insn));
18826
58
  info->subseg = now_subseg;
18827
58
}
18828
18829
#ifdef TE_SOLARIS
18830
void
18831
i386_solaris_fix_up_eh_frame (segT sec)
18832
{
18833
  if (flag_code == CODE_64BIT)
18834
    elf_section_type (sec) = SHT_X86_64_UNWIND;
18835
}
18836
#endif
18837
18838
/* For ELF on x86-64, add support for SHF_X86_64_LARGE.  */
18839
18840
bfd_vma
18841
x86_64_section_letter (int letter, const char **extra)
18842
27.8k
{
18843
27.8k
  if (flag_code == CODE_64BIT)
18844
27.8k
    {
18845
27.8k
      if (letter == 'l')
18846
1
  return SHF_X86_64_LARGE;
18847
18848
27.8k
      *extra = "l";
18849
27.8k
    }
18850
27.8k
  return -1;
18851
27.8k
}
18852
18853
static void
18854
handle_large_common (int small ATTRIBUTE_UNUSED)
18855
1
{
18856
1
  if (flag_code != CODE_64BIT)
18857
0
    {
18858
0
      s_comm_internal (0, elf_common_parse);
18859
0
      as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
18860
0
    }
18861
1
  else
18862
1
    {
18863
1
      static segT lbss_section;
18864
1
      asection *saved_com_section_ptr = elf_com_section_ptr;
18865
1
      asection *saved_bss_section = bss_section;
18866
18867
1
      if (lbss_section == NULL)
18868
1
  {
18869
1
    flagword applicable;
18870
1
    segT seg = now_seg;
18871
1
    subsegT subseg = now_subseg;
18872
18873
    /* The .lbss section is for local .largecomm symbols.  */
18874
1
    lbss_section = subseg_new (".lbss", 0);
18875
1
    applicable = bfd_applicable_section_flags (stdoutput);
18876
1
    bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
18877
1
    seg_info (lbss_section)->bss = 1;
18878
18879
1
    subseg_set (seg, subseg);
18880
1
  }
18881
18882
1
      elf_com_section_ptr = &bfd_elf_large_com_section;
18883
1
      bss_section = lbss_section;
18884
18885
1
      s_comm_internal (0, elf_common_parse);
18886
18887
1
      elf_com_section_ptr = saved_com_section_ptr;
18888
1
      bss_section = saved_bss_section;
18889
1
    }
18890
1
}
18891
#endif /* OBJ_ELF */