Coverage Report

Created: 2023-06-29 07:09

/src/binutils-gdb/gas/config/tc-i386.c
Line
Count
Source (jump to first uncovered line)
1
/* tc-i386.c -- Assemble code for the Intel 80386
2
   Copyright (C) 1989-2023 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 "gen-sframe.h"
34
#include "sframe.h"
35
#include "elf/x86-64.h"
36
#include "opcodes/i386-init.h"
37
#include "opcodes/i386-mnem.h"
38
#include <limits.h>
39
40
#ifndef INFER_ADDR_PREFIX
41
#define INFER_ADDR_PREFIX 1
42
#endif
43
44
#ifndef DEFAULT_ARCH
45
#define DEFAULT_ARCH "i386"
46
#endif
47
48
#ifndef INLINE
49
#if __GNUC__ >= 2
50
#define INLINE __inline__
51
#else
52
#define INLINE
53
#endif
54
#endif
55
56
/* Prefixes will be emitted in the order defined below.
57
   WAIT_PREFIX must be the first prefix since FWAIT is really is an
58
   instruction, and so must come before any prefixes.
59
   The preferred prefix order is SEG_PREFIX, ADDR_PREFIX, DATA_PREFIX,
60
   REP_PREFIX/HLE_PREFIX, LOCK_PREFIX.  */
61
1.77k
#define WAIT_PREFIX 0
62
98.9k
#define SEG_PREFIX  1
63
614k
#define ADDR_PREFIX 2
64
282k
#define DATA_PREFIX 3
65
8.01k
#define REP_PREFIX  4
66
0
#define HLE_PREFIX  REP_PREFIX
67
7.44k
#define BND_PREFIX  REP_PREFIX
68
187k
#define LOCK_PREFIX 5
69
223k
#define REX_PREFIX  6       /* must come last.  */
70
#define MAX_PREFIXES  7 /* max prefixes per opcode */
71
72
/* we define the syntax here (modulo base,index,scale syntax) */
73
1.06M
#define REGISTER_PREFIX '%'
74
220k
#define IMMEDIATE_PREFIX '$'
75
441k
#define ABSOLUTE_PREFIX '*'
76
77
/* these are the instruction mnemonic suffixes in AT&T syntax or
78
   memory operand size in Intel syntax.  */
79
342k
#define WORD_MNEM_SUFFIX  'w'
80
219k
#define BYTE_MNEM_SUFFIX  'b'
81
418k
#define SHORT_MNEM_SUFFIX 's'
82
911k
#define LONG_MNEM_SUFFIX  'l'
83
1.13M
#define QWORD_MNEM_SUFFIX  'q'
84
85
5.18M
#define END_OF_INSN '\0'
86
87
918k
#define OPERAND_TYPE_NONE { .bitfield = { .class = ClassNone } }
88
89
/* This matches the C -> StaticRounding alias in the opcode table.  */
90
5
#define commutative staticrounding
91
92
/*
93
  'templates' is for grouping together 'template' structures for opcodes
94
  of the same name.  This is only used for storing the insns in the grand
95
  ole hash table of insns.
96
  The templates themselves start at START and range up to (but not including)
97
  END.
98
  */
99
typedef struct
100
{
101
  const insn_template *start;
102
  const insn_template *end;
103
}
104
templates;
105
106
/* 386 operand encoding bytes:  see 386 book for details of this.  */
107
typedef struct
108
{
109
  unsigned int regmem;  /* codes register or memory operand */
110
  unsigned int reg; /* codes register operand (or extended opcode) */
111
  unsigned int mode;  /* how to interpret regmem & reg */
112
}
113
modrm_byte;
114
115
/* x86-64 extension prefix.  */
116
typedef int rex_byte;
117
118
/* 386 opcode byte to code indirect addressing.  */
119
typedef struct
120
{
121
  unsigned base;
122
  unsigned index;
123
  unsigned scale;
124
}
125
sib_byte;
126
127
/* x86 arch names, types and features */
128
typedef struct
129
{
130
  const char *name;   /* arch name */
131
  unsigned int len:8;   /* arch string length */
132
  bool skip:1;      /* show_arch should skip this. */
133
  enum processor_type type; /* arch type */
134
  i386_cpu_flags enable;    /* cpu feature enable flags */
135
  i386_cpu_flags disable; /* cpu feature disable flags */
136
}
137
arch_entry;
138
139
static void update_code_flag (int, int);
140
static void s_insn (int);
141
static void set_code_flag (int);
142
static void set_16bit_gcc_code_flag (int);
143
static void set_intel_syntax (int);
144
static void set_intel_mnemonic (int);
145
static void set_allow_index_reg (int);
146
static void set_check (int);
147
static void set_cpu_arch (int);
148
#ifdef TE_PE
149
static void pe_directive_secrel (int);
150
static void pe_directive_secidx (int);
151
#endif
152
static void signed_cons (int);
153
static char *output_invalid (int c);
154
static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
155
            const char *);
156
static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
157
               const char *);
158
static int i386_att_operand (char *);
159
static int i386_intel_operand (char *, int);
160
static int i386_intel_simplify (expressionS *);
161
static int i386_intel_parse_name (const char *, expressionS *);
162
static const reg_entry *parse_register (const char *, char **);
163
static const char *parse_insn (const char *, char *, bool);
164
static char *parse_operands (char *, const char *);
165
static void swap_operands (void);
166
static void swap_2_operands (unsigned int, unsigned int);
167
static enum flag_code i386_addressing_mode (void);
168
static void optimize_imm (void);
169
static bool optimize_disp (const insn_template *t);
170
static const insn_template *match_template (char);
171
static int check_string (void);
172
static int process_suffix (void);
173
static int check_byte_reg (void);
174
static int check_long_reg (void);
175
static int check_qword_reg (void);
176
static int check_word_reg (void);
177
static int finalize_imm (void);
178
static int process_operands (void);
179
static const reg_entry *build_modrm_byte (void);
180
static void output_insn (void);
181
static void output_imm (fragS *, offsetT);
182
static void output_disp (fragS *, offsetT);
183
#ifndef I386COFF
184
static void s_bss (int);
185
#endif
186
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
187
static void handle_large_common (int small ATTRIBUTE_UNUSED);
188
189
/* GNU_PROPERTY_X86_ISA_1_USED.  */
190
static unsigned int x86_isa_1_used;
191
/* GNU_PROPERTY_X86_FEATURE_2_USED.  */
192
static unsigned int x86_feature_2_used;
193
/* Generate x86 used ISA and feature properties.  */
194
static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
195
#endif
196
197
static const char *default_arch = DEFAULT_ARCH;
198
199
/* parse_register() returns this when a register alias cannot be used.  */
200
static const reg_entry bad_reg = { "<bad>", OPERAND_TYPE_NONE, 0, 0,
201
           { Dw2Inval, Dw2Inval } };
202
203
static const reg_entry *reg_eax;
204
static const reg_entry *reg_ds;
205
static const reg_entry *reg_es;
206
static const reg_entry *reg_ss;
207
static const reg_entry *reg_st0;
208
static const reg_entry *reg_k0;
209
210
/* VEX prefix.  */
211
typedef struct
212
{
213
  /* VEX prefix is either 2 byte or 3 byte.  EVEX is 4 byte.  */
214
  unsigned char bytes[4];
215
  unsigned int length;
216
  /* Destination or source register specifier.  */
217
  const reg_entry *register_specifier;
218
} vex_prefix;
219
220
/* 'md_assemble ()' gathers together information and puts it into a
221
   i386_insn.  */
222
223
union i386_op
224
  {
225
    expressionS *disps;
226
    expressionS *imms;
227
    const reg_entry *regs;
228
  };
229
230
enum i386_error
231
  {
232
    no_error, /* Must be first.  */
233
    operand_size_mismatch,
234
    operand_type_mismatch,
235
    register_type_mismatch,
236
    number_of_operands_mismatch,
237
    invalid_instruction_suffix,
238
    bad_imm4,
239
    unsupported_with_intel_mnemonic,
240
    unsupported_syntax,
241
    unsupported,
242
    unsupported_on_arch,
243
    unsupported_64bit,
244
    invalid_sib_address,
245
    invalid_vsib_address,
246
    invalid_vector_register_set,
247
    invalid_tmm_register_set,
248
    invalid_dest_and_src_register_set,
249
    unsupported_vector_index_register,
250
    unsupported_broadcast,
251
    broadcast_needed,
252
    unsupported_masking,
253
    mask_not_on_destination,
254
    no_default_mask,
255
    unsupported_rc_sae,
256
    invalid_register_operand,
257
  };
258
259
struct _i386_insn
260
  {
261
    /* TM holds the template for the insn were currently assembling.  */
262
    insn_template tm;
263
264
    /* SUFFIX holds the instruction size suffix for byte, word, dword
265
       or qword, if given.  */
266
    char suffix;
267
268
    /* OPCODE_LENGTH holds the number of base opcode bytes.  */
269
    unsigned char opcode_length;
270
271
    /* OPERANDS gives the number of given operands.  */
272
    unsigned int operands;
273
274
    /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
275
       of given register, displacement, memory operands and immediate
276
       operands.  */
277
    unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
278
279
    /* TYPES [i] is the type (see above #defines) which tells us how to
280
       use OP[i] for the corresponding operand.  */
281
    i386_operand_type types[MAX_OPERANDS];
282
283
    /* Displacement expression, immediate expression, or register for each
284
       operand.  */
285
    union i386_op op[MAX_OPERANDS];
286
287
    /* Flags for operands.  */
288
    unsigned int flags[MAX_OPERANDS];
289
5.13k
#define Operand_PCrel 1
290
561k
#define Operand_Mem   2
291
0
#define Operand_Signed 4 /* .insn only */
292
293
    /* Relocation type for operand */
294
    enum bfd_reloc_code_real reloc[MAX_OPERANDS];
295
296
    /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
297
       the base index byte below.  */
298
    const reg_entry *base_reg;
299
    const reg_entry *index_reg;
300
    unsigned int log2_scale_factor;
301
302
    /* SEG gives the seg_entries of this insn.  They are zero unless
303
       explicit segment overrides are given.  */
304
    const reg_entry *seg[2];
305
306
    /* PREFIX holds all the given prefix opcodes (usually null).
307
       PREFIXES is the number of prefix opcodes.  */
308
    unsigned int prefixes;
309
    unsigned char prefix[MAX_PREFIXES];
310
311
    /* .insn allows for reserved opcode spaces.  */
312
    unsigned char insn_opcode_space;
313
314
    /* .insn also allows (requires) specifying immediate size.  */
315
    unsigned char imm_bits[MAX_OPERANDS];
316
317
    /* Register is in low 3 bits of opcode.  */
318
    bool short_form;
319
320
    /* The operand to a branch insn indicates an absolute branch.  */
321
    bool jumpabsolute;
322
323
    /* The operand to a branch insn indicates a far branch.  */
324
    bool far_branch;
325
326
    /* There is a memory operand of (%dx) which should be only used
327
       with input/output instructions.  */
328
    bool input_output_operand;
329
330
    /* Extended states.  */
331
    enum
332
      {
333
  /* Use MMX state.  */
334
  xstate_mmx = 1 << 0,
335
  /* Use XMM state.  */
336
  xstate_xmm = 1 << 1,
337
  /* Use YMM state.  */
338
  xstate_ymm = 1 << 2 | xstate_xmm,
339
  /* Use ZMM state.  */
340
  xstate_zmm = 1 << 3 | xstate_ymm,
341
  /* Use TMM state.  */
342
  xstate_tmm = 1 << 4,
343
  /* Use MASK state.  */
344
  xstate_mask = 1 << 5
345
      } xstate;
346
347
    /* Has GOTPC or TLS relocation.  */
348
    bool has_gotpc_tls_reloc;
349
350
    /* RM and SIB are the modrm byte and the sib byte where the
351
       addressing modes of this insn are encoded.  */
352
    modrm_byte rm;
353
    rex_byte rex;
354
    rex_byte vrex;
355
    sib_byte sib;
356
    vex_prefix vex;
357
358
    /* Masking attributes.
359
360
       The struct describes masking, applied to OPERAND in the instruction.
361
       REG is a pointer to the corresponding mask register.  ZEROING tells
362
       whether merging or zeroing mask is used.  */
363
    struct Mask_Operation
364
    {
365
      const reg_entry *reg;
366
      unsigned int zeroing;
367
      /* The operand where this operation is associated.  */
368
      unsigned int operand;
369
    } mask;
370
371
    /* Rounding control and SAE attributes.  */
372
    struct RC_Operation
373
    {
374
      enum rc_type
375
  {
376
    rc_none = -1,
377
    rne,
378
    rd,
379
    ru,
380
    rz,
381
    saeonly
382
  } type;
383
      /* In Intel syntax the operand modifier form is supposed to be used, but
384
   we continue to accept the immediate forms as well.  */
385
      bool modifier;
386
    } rounding;
387
388
    /* Broadcasting attributes.
389
390
       The struct describes broadcasting, applied to OPERAND.  TYPE is
391
       expresses the broadcast factor.  */
392
    struct Broadcast_Operation
393
    {
394
      /* Type of broadcast: {1to2}, {1to4}, {1to8}, {1to16} or {1to32}.  */
395
      unsigned int type;
396
397
      /* Index of broadcasted operand.  */
398
      unsigned int operand;
399
400
      /* Number of bytes to broadcast.  */
401
      unsigned int bytes;
402
    } broadcast;
403
404
    /* Compressed disp8*N attribute.  */
405
    unsigned int memshift;
406
407
    /* Prefer load or store in encoding.  */
408
    enum
409
      {
410
  dir_encoding_default = 0,
411
  dir_encoding_load,
412
  dir_encoding_store,
413
  dir_encoding_swap
414
      } dir_encoding;
415
416
    /* Prefer 8bit, 16bit, 32bit displacement in encoding.  */
417
    enum
418
      {
419
  disp_encoding_default = 0,
420
  disp_encoding_8bit,
421
  disp_encoding_16bit,
422
  disp_encoding_32bit
423
      } disp_encoding;
424
425
    /* Prefer the REX byte in encoding.  */
426
    bool rex_encoding;
427
428
    /* Disable instruction size optimization.  */
429
    bool no_optimize;
430
431
    /* How to encode vector instructions.  */
432
    enum
433
      {
434
  vex_encoding_default = 0,
435
  vex_encoding_vex,
436
  vex_encoding_vex3,
437
  vex_encoding_evex,
438
  vex_encoding_error
439
      } vec_encoding;
440
441
    /* REP prefix.  */
442
    const char *rep_prefix;
443
444
    /* HLE prefix.  */
445
    const char *hle_prefix;
446
447
    /* Have BND prefix.  */
448
    const char *bnd_prefix;
449
450
    /* Have NOTRACK prefix.  */
451
    const char *notrack_prefix;
452
453
    /* Error message.  */
454
    enum i386_error error;
455
  };
456
457
typedef struct _i386_insn i386_insn;
458
459
/* Link RC type with corresponding string, that'll be looked for in
460
   asm.  */
461
struct RC_name
462
{
463
  enum rc_type type;
464
  const char *name;
465
  unsigned int len;
466
};
467
468
static const struct RC_name RC_NamesTable[] =
469
{
470
  {  rne, STRING_COMMA_LEN ("rn-sae") },
471
  {  rd,  STRING_COMMA_LEN ("rd-sae") },
472
  {  ru,  STRING_COMMA_LEN ("ru-sae") },
473
  {  rz,  STRING_COMMA_LEN ("rz-sae") },
474
  {  saeonly,  STRING_COMMA_LEN ("sae") },
475
};
476
477
/* To be indexed by segment register number.  */
478
static const unsigned char i386_seg_prefixes[] = {
479
  ES_PREFIX_OPCODE,
480
  CS_PREFIX_OPCODE,
481
  SS_PREFIX_OPCODE,
482
  DS_PREFIX_OPCODE,
483
  FS_PREFIX_OPCODE,
484
  GS_PREFIX_OPCODE
485
};
486
487
/* List of chars besides those in app.c:symbol_chars that can start an
488
   operand.  Used to prevent the scrubber eating vital white-space.  */
489
const char extra_symbol_chars[] = "*%-([{}"
490
#ifdef LEX_AT
491
  "@"
492
#endif
493
#ifdef LEX_QM
494
  "?"
495
#endif
496
  ;
497
498
#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
499
     && !defined (TE_GNU)       \
500
     && !defined (TE_LINUX)       \
501
     && !defined (TE_Haiku)       \
502
     && !defined (TE_FreeBSD)       \
503
     && !defined (TE_DragonFly)       \
504
     && !defined (TE_NetBSD))
505
/* This array holds the chars that always start a comment.  If the
506
   pre-processor is disabled, these aren't very useful.  The option
507
   --divide will remove '/' from this list.  */
508
const char *i386_comment_chars = "#/";
509
#define SVR4_COMMENT_CHARS 1
510
#define PREFIX_SEPARATOR '\\'
511
512
#else
513
const char *i386_comment_chars = "#";
514
925k
#define PREFIX_SEPARATOR '/'
515
#endif
516
517
/* This array holds the chars that only start a comment at the beginning of
518
   a line.  If the line seems to have the form '# 123 filename'
519
   .line and .file directives will appear in the pre-processed output.
520
   Note that input_file.c hand checks for '#' at the beginning of the
521
   first line of the input file.  This is because the compiler outputs
522
   #NO_APP at the beginning of its output.
523
   Also note that comments started like this one will always work if
524
   '/' isn't otherwise defined.  */
525
const char line_comment_chars[] = "#/";
526
527
const char line_separator_chars[] = ";";
528
529
/* Chars that can be used to separate mant from exp in floating point
530
   nums.  */
531
const char EXP_CHARS[] = "eE";
532
533
/* Chars that mean this number is a floating point constant
534
   As in 0f12.456
535
   or    0d1.2345e12.  */
536
const char FLT_CHARS[] = "fFdDxXhHbB";
537
538
/* Tables for lexical analysis.  */
539
static char mnemonic_chars[256];
540
static char register_chars[256];
541
static char operand_chars[256];
542
543
/* Lexical macros.  */
544
2.77M
#define is_operand_char(x) (operand_chars[(unsigned char) x])
545
#define is_register_char(x) (register_chars[(unsigned char) x])
546
3.15M
#define is_space_char(x) ((x) == ' ')
547
548
/* All non-digit non-letter characters that may occur in an operand and
549
   which aren't already in extra_symbol_chars[].  */
550
static const char operand_special_chars[] = "$+,)._~/<>|&^!=:@]";
551
552
/* md_assemble() always leaves the strings it's passed unaltered.  To
553
   effect this we maintain a stack of saved characters that we've smashed
554
   with '\0's (indicating end of strings for various sub-fields of the
555
   assembler instruction).  */
556
static char save_stack[32];
557
static char *save_stack_p;
558
#define END_STRING_AND_SAVE(s) \
559
337k
  do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
560
#define RESTORE_END_STRING(s) \
561
336k
  do { *(s) = *--save_stack_p; } while (0)
562
563
/* The instruction we're assembling.  */
564
static i386_insn i;
565
566
/* Possible templates for current insn.  */
567
static const templates *current_templates;
568
569
/* Per instruction expressionS buffers: max displacements & immediates.  */
570
static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
571
static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
572
573
/* Current operand we are working on.  */
574
static int this_operand = -1;
575
576
/* Are we processing a .insn directive?  */
577
208k
#define dot_insn() (i.tm.mnem_off == MN__insn)
578
579
/* We support four different modes.  FLAG_CODE variable is used to distinguish
580
   these.  */
581
582
enum flag_code {
583
  CODE_32BIT,
584
  CODE_16BIT,
585
  CODE_64BIT };
586
587
static enum flag_code flag_code;
588
static unsigned int object_64bit;
589
static unsigned int disallow_64bit_reloc;
590
static int use_rela_relocations = 0;
591
/* __tls_get_addr/___tls_get_addr symbol for TLS.  */
592
static const char *tls_get_addr;
593
594
#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
595
     || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
596
     || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
597
598
/* The ELF ABI to use.  */
599
enum x86_elf_abi
600
{
601
  I386_ABI,
602
  X86_64_ABI,
603
  X86_64_X32_ABI
604
};
605
606
static enum x86_elf_abi x86_elf_abi = I386_ABI;
607
#endif
608
609
#if defined (TE_PE) || defined (TE_PEP)
610
/* Use big object file format.  */
611
static int use_big_obj = 0;
612
#endif
613
614
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
615
/* 1 if generating code for a shared library.  */
616
static int shared = 0;
617
618
unsigned int x86_sframe_cfa_sp_reg;
619
/* The other CFA base register for SFrame stack trace info.  */
620
unsigned int x86_sframe_cfa_fp_reg;
621
unsigned int x86_sframe_cfa_ra_reg;
622
623
#endif
624
625
/* 1 for intel syntax,
626
   0 if att syntax.  */
627
static int intel_syntax = 0;
628
629
static enum x86_64_isa
630
{
631
  amd64 = 1,  /* AMD64 ISA.  */
632
  intel64 /* Intel64 ISA.  */
633
} isa64;
634
635
/* 1 for intel mnemonic,
636
   0 if att mnemonic.  */
637
static int intel_mnemonic = !SYSV386_COMPAT;
638
639
/* 1 if pseudo registers are permitted.  */
640
static int allow_pseudo_reg = 0;
641
642
/* 1 if register prefix % not required.  */
643
static int allow_naked_reg = 0;
644
645
/* 1 if the assembler should add BND prefix for all control-transferring
646
   instructions supporting it, even if this prefix wasn't specified
647
   explicitly.  */
648
static int add_bnd_prefix = 0;
649
650
/* 1 if pseudo index register, eiz/riz, is allowed .  */
651
static int allow_index_reg = 0;
652
653
/* 1 if the assembler should ignore LOCK prefix, even if it was
654
   specified explicitly.  */
655
static int omit_lock_prefix = 0;
656
657
/* 1 if the assembler should encode lfence, mfence, and sfence as
658
   "lock addl $0, (%{re}sp)".  */
659
static int avoid_fence = 0;
660
661
/* 1 if lfence should be inserted after every load.  */
662
static int lfence_after_load = 0;
663
664
/* Non-zero if lfence should be inserted before indirect branch.  */
665
static enum lfence_before_indirect_branch_kind
666
  {
667
    lfence_branch_none = 0,
668
    lfence_branch_register,
669
    lfence_branch_memory,
670
    lfence_branch_all
671
  }
672
lfence_before_indirect_branch;
673
674
/* Non-zero if lfence should be inserted before ret.  */
675
static enum lfence_before_ret_kind
676
  {
677
    lfence_before_ret_none = 0,
678
    lfence_before_ret_not,
679
    lfence_before_ret_or,
680
    lfence_before_ret_shl
681
  }
682
lfence_before_ret;
683
684
/* Types of previous instruction is .byte or prefix.  */
685
static struct
686
  {
687
    segT seg;
688
    const char *file;
689
    const char *name;
690
    unsigned int line;
691
    enum last_insn_kind
692
      {
693
  last_insn_other = 0,
694
  last_insn_directive,
695
  last_insn_prefix
696
      } kind;
697
  } last_insn;
698
699
/* 1 if the assembler should generate relax relocations.  */
700
701
static int generate_relax_relocations
702
  = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
703
704
static enum check_kind
705
  {
706
    check_none = 0,
707
    check_warning,
708
    check_error
709
  }
710
sse_check, operand_check = check_warning;
711
712
/* Non-zero if branches should be aligned within power of 2 boundary.  */
713
static int align_branch_power = 0;
714
715
/* Types of branches to align.  */
716
enum align_branch_kind
717
  {
718
    align_branch_none = 0,
719
    align_branch_jcc = 1,
720
    align_branch_fused = 2,
721
    align_branch_jmp = 3,
722
    align_branch_call = 4,
723
    align_branch_indirect = 5,
724
    align_branch_ret = 6
725
  };
726
727
/* Type bits of branches to align.  */
728
enum align_branch_bit
729
  {
730
    align_branch_jcc_bit = 1 << align_branch_jcc,
731
    align_branch_fused_bit = 1 << align_branch_fused,
732
    align_branch_jmp_bit = 1 << align_branch_jmp,
733
    align_branch_call_bit = 1 << align_branch_call,
734
    align_branch_indirect_bit = 1 << align_branch_indirect,
735
    align_branch_ret_bit = 1 << align_branch_ret
736
  };
737
738
static unsigned int align_branch = (align_branch_jcc_bit
739
            | align_branch_fused_bit
740
            | align_branch_jmp_bit);
741
742
/* Types of condition jump used by macro-fusion.  */
743
enum mf_jcc_kind
744
  {
745
    mf_jcc_jo = 0,  /* base opcode 0x70  */
746
    mf_jcc_jc,      /* base opcode 0x72  */
747
    mf_jcc_je,      /* base opcode 0x74  */
748
    mf_jcc_jna,     /* base opcode 0x76  */
749
    mf_jcc_js,      /* base opcode 0x78  */
750
    mf_jcc_jp,      /* base opcode 0x7a  */
751
    mf_jcc_jl,      /* base opcode 0x7c  */
752
    mf_jcc_jle,     /* base opcode 0x7e  */
753
  };
754
755
/* Types of compare flag-modifying insntructions used by macro-fusion.  */
756
enum mf_cmp_kind
757
  {
758
    mf_cmp_test_and,  /* test/cmp */
759
    mf_cmp_alu_cmp,  /* add/sub/cmp */
760
    mf_cmp_incdec  /* inc/dec */
761
  };
762
763
/* The maximum padding size for fused jcc.  CMP like instruction can
764
   be 9 bytes and jcc can be 6 bytes.  Leave room just in case for
765
   prefixes.   */
766
633
#define MAX_FUSED_JCC_PADDING_SIZE 20
767
768
/* The maximum number of prefixes added for an instruction.  */
769
static unsigned int align_branch_prefix_size = 5;
770
771
/* Optimization:
772
   1. Clear the REX_W bit with register operand if possible.
773
   2. Above plus use 128bit vector instruction to clear the full vector
774
      register.
775
 */
776
static int optimize = 0;
777
778
/* Optimization:
779
   1. Clear the REX_W bit with register operand if possible.
780
   2. Above plus use 128bit vector instruction to clear the full vector
781
      register.
782
   3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
783
      "testb $imm7,%r8".
784
 */
785
static int optimize_for_space = 0;
786
787
/* Register prefix used for error message.  */
788
static const char *register_prefix = "%";
789
790
/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
791
   leave, push, and pop instructions so that gcc has the same stack
792
   frame as in 32 bit mode.  */
793
static char stackop_size = '\0';
794
795
/* Non-zero to optimize code alignment.  */
796
int optimize_align_code = 1;
797
798
/* Non-zero to quieten some warnings.  */
799
static int quiet_warnings = 0;
800
801
/* Guard to avoid repeated warnings about non-16-bit code on 16-bit CPUs.  */
802
static bool pre_386_16bit_warned;
803
804
/* CPU name.  */
805
static const char *cpu_arch_name = NULL;
806
static char *cpu_sub_arch_name = NULL;
807
808
/* CPU feature flags.  */
809
static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
810
811
/* If we have selected a cpu we are generating instructions for.  */
812
static int cpu_arch_tune_set = 0;
813
814
/* Cpu we are generating instructions for.  */
815
enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
816
817
/* CPU feature flags of cpu we are generating instructions for.  */
818
static i386_cpu_flags cpu_arch_tune_flags;
819
820
/* CPU instruction set architecture used.  */
821
enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
822
823
/* CPU feature flags of instruction set architecture used.  */
824
i386_cpu_flags cpu_arch_isa_flags;
825
826
/* If set, conditional jumps are not automatically promoted to handle
827
   larger than a byte offset.  */
828
static bool no_cond_jump_promotion = false;
829
830
/* This will be set from an expression parser hook if there's any
831
   applicable operator involved in an expression.  */
832
static enum {
833
  expr_operator_none,
834
  expr_operator_present,
835
  expr_large_value,
836
} expr_mode;
837
838
/* Encode SSE instructions with VEX prefix.  */
839
static unsigned int sse2avx;
840
841
/* Encode aligned vector move as unaligned vector move.  */
842
static unsigned int use_unaligned_vector_move;
843
844
/* Encode scalar AVX instructions with specific vector length.  */
845
static enum
846
  {
847
    vex128 = 0,
848
    vex256
849
  } avxscalar;
850
851
/* Encode VEX WIG instructions with specific vex.w.  */
852
static enum
853
  {
854
    vexw0 = 0,
855
    vexw1
856
  } vexwig;
857
858
/* Encode scalar EVEX LIG instructions with specific vector length.  */
859
static enum
860
  {
861
    evexl128 = 0,
862
    evexl256,
863
    evexl512
864
  } evexlig;
865
866
/* Encode EVEX WIG instructions with specific evex.w.  */
867
static enum
868
  {
869
    evexw0 = 0,
870
    evexw1
871
  } evexwig;
872
873
/* Value to encode in EVEX RC bits, for SAE-only instructions.  */
874
static enum rc_type evexrcig = rne;
875
876
/* Pre-defined "_GLOBAL_OFFSET_TABLE_".  */
877
static symbolS *GOT_symbol;
878
879
/* The dwarf2 return column, adjusted for 32 or 64 bit.  */
880
unsigned int x86_dwarf2_return_column;
881
882
/* The dwarf2 data alignment, adjusted for 32 or 64 bit.  */
883
int x86_cie_data_alignment;
884
885
/* Interface to relax_segment.
886
   There are 3 major relax states for 386 jump insns because the
887
   different types of jumps add different sizes to frags when we're
888
   figuring out what sort of jump to choose to reach a given label.
889
890
   BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
891
   branches which are handled by md_estimate_size_before_relax() and
892
   i386_generic_table_relax_frag().  */
893
894
/* Types.  */
895
0
#define UNCOND_JUMP 0
896
0
#define COND_JUMP 1
897
0
#define COND_JUMP86 2
898
0
#define BRANCH_PADDING 3
899
0
#define BRANCH_PREFIX 4
900
0
#define FUSED_JCC_PADDING 5
901
902
/* Sizes.  */
903
1.90k
#define CODE16  1
904
7.09k
#define SMALL 0
905
#define SMALL16 (SMALL | CODE16)
906
0
#define BIG 2
907
0
#define BIG16 (BIG | CODE16)
908
909
#ifndef INLINE
910
#ifdef __GNUC__
911
#define INLINE __inline__
912
#else
913
#define INLINE
914
#endif
915
#endif
916
917
#define ENCODE_RELAX_STATE(type, size) \
918
3.54k
  ((relax_substateT) (((type) << 2) | (size)))
919
#define TYPE_FROM_RELAX_STATE(s) \
920
0
  ((s) >> 2)
921
#define DISP_SIZE_FROM_RELAX_STATE(s) \
922
0
    ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
923
924
/* This table is used by relax_frag to promote short jumps to long
925
   ones where necessary.  SMALL (short) jumps may be promoted to BIG
926
   (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long).  We
927
   don't allow a short jump in a 32 bit code segment to be promoted to
928
   a 16 bit offset jump because it's slower (requires data size
929
   prefix), and doesn't work, unless the destination is in the bottom
930
   64k of the code segment (The top 16 bits of eip are zeroed).  */
931
932
const relax_typeS md_relax_table[] =
933
{
934
  /* The fields are:
935
     1) most positive reach of this state,
936
     2) most negative reach of this state,
937
     3) how many bytes this mode will have in the variable part of the frag
938
     4) which index into the table to try if we can't fit into this one.  */
939
940
  /* UNCOND_JUMP states.  */
941
  {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
942
  {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
943
  /* dword jmp adds 4 bytes to frag:
944
     0 extra opcode bytes, 4 displacement bytes.  */
945
  {0, 0, 4, 0},
946
  /* word jmp adds 2 byte2 to frag:
947
     0 extra opcode bytes, 2 displacement bytes.  */
948
  {0, 0, 2, 0},
949
950
  /* COND_JUMP states.  */
951
  {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
952
  {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
953
  /* dword conditionals adds 5 bytes to frag:
954
     1 extra opcode byte, 4 displacement bytes.  */
955
  {0, 0, 5, 0},
956
  /* word conditionals add 3 bytes to frag:
957
     1 extra opcode byte, 2 displacement bytes.  */
958
  {0, 0, 3, 0},
959
960
  /* COND_JUMP86 states.  */
961
  {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
962
  {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
963
  /* dword conditionals adds 5 bytes to frag:
964
     1 extra opcode byte, 4 displacement bytes.  */
965
  {0, 0, 5, 0},
966
  /* word conditionals add 4 bytes to frag:
967
     1 displacement byte and a 3 byte long branch insn.  */
968
  {0, 0, 4, 0}
969
};
970
971
#define ARCH(n, t, f, s) \
972
  { STRING_COMMA_LEN (#n), s, PROCESSOR_ ## t, CPU_ ## f ## _FLAGS, \
973
    CPU_NONE_FLAGS }
974
#define SUBARCH(n, e, d, s) \
975
  { STRING_COMMA_LEN (#n), s, PROCESSOR_NONE, CPU_ ## e ## _FLAGS, \
976
    CPU_ ## d ## _FLAGS }
977
978
static const arch_entry cpu_arch[] =
979
{
980
  /* Do not replace the first two entries - i386_target_format() and
981
     set_cpu_arch() rely on them being there in this order.  */
982
  ARCH (generic32, GENERIC32, GENERIC32, false),
983
  ARCH (generic64, GENERIC64, GENERIC64, false),
984
  ARCH (i8086, UNKNOWN, NONE, false),
985
  ARCH (i186, UNKNOWN, 186, false),
986
  ARCH (i286, UNKNOWN, 286, false),
987
  ARCH (i386, I386, 386, false),
988
  ARCH (i486, I486, 486, false),
989
  ARCH (i586, PENTIUM, 586, false),
990
  ARCH (i686, PENTIUMPRO, 686, false),
991
  ARCH (pentium, PENTIUM, 586, false),
992
  ARCH (pentiumpro, PENTIUMPRO, PENTIUMPRO, false),
993
  ARCH (pentiumii, PENTIUMPRO, P2, false),
994
  ARCH (pentiumiii, PENTIUMPRO, P3, false),
995
  ARCH (pentium4, PENTIUM4, P4, false),
996
  ARCH (prescott, NOCONA, CORE, false),
997
  ARCH (nocona, NOCONA, NOCONA, false),
998
  ARCH (yonah, CORE, CORE, true),
999
  ARCH (core, CORE, CORE, false),
1000
  ARCH (merom, CORE2, CORE2, true),
1001
  ARCH (core2, CORE2, CORE2, false),
1002
  ARCH (corei7, COREI7, COREI7, false),
1003
  ARCH (iamcu, IAMCU, IAMCU, false),
1004
  ARCH (k6, K6, K6, false),
1005
  ARCH (k6_2, K6, K6_2, false),
1006
  ARCH (athlon, ATHLON, ATHLON, false),
1007
  ARCH (sledgehammer, K8, K8, true),
1008
  ARCH (opteron, K8, K8, false),
1009
  ARCH (k8, K8, K8, false),
1010
  ARCH (amdfam10, AMDFAM10, AMDFAM10, false),
1011
  ARCH (bdver1, BD, BDVER1, false),
1012
  ARCH (bdver2, BD, BDVER2, false),
1013
  ARCH (bdver3, BD, BDVER3, false),
1014
  ARCH (bdver4, BD, BDVER4, false),
1015
  ARCH (znver1, ZNVER, ZNVER1, false),
1016
  ARCH (znver2, ZNVER, ZNVER2, false),
1017
  ARCH (znver3, ZNVER, ZNVER3, false),
1018
  ARCH (znver4, ZNVER, ZNVER4, false),
1019
  ARCH (btver1, BT, BTVER1, false),
1020
  ARCH (btver2, BT, BTVER2, false),
1021
1022
  SUBARCH (8087, 8087, ANY_8087, false),
1023
  SUBARCH (87, NONE, ANY_8087, false), /* Disable only!  */
1024
  SUBARCH (287, 287, ANY_287, false),
1025
  SUBARCH (387, 387, ANY_387, false),
1026
  SUBARCH (687, 687, ANY_687, false),
1027
  SUBARCH (cmov, CMOV, CMOV, false),
1028
  SUBARCH (fxsr, FXSR, ANY_FXSR, false),
1029
  SUBARCH (mmx, MMX, ANY_MMX, false),
1030
  SUBARCH (sse, SSE, ANY_SSE, false),
1031
  SUBARCH (sse2, SSE2, ANY_SSE2, false),
1032
  SUBARCH (sse3, SSE3, ANY_SSE3, false),
1033
  SUBARCH (sse4a, SSE4A, ANY_SSE4A, false),
1034
  SUBARCH (ssse3, SSSE3, ANY_SSSE3, false),
1035
  SUBARCH (sse4.1, SSE4_1, ANY_SSE4_1, false),
1036
  SUBARCH (sse4.2, SSE4_2, ANY_SSE4_2, false),
1037
  SUBARCH (sse4, SSE4_2, ANY_SSE4_1, false),
1038
  SUBARCH (avx, AVX, ANY_AVX, false),
1039
  SUBARCH (avx2, AVX2, ANY_AVX2, false),
1040
  SUBARCH (avx512f, AVX512F, ANY_AVX512F, false),
1041
  SUBARCH (avx512cd, AVX512CD, ANY_AVX512CD, false),
1042
  SUBARCH (avx512er, AVX512ER, ANY_AVX512ER, false),
1043
  SUBARCH (avx512pf, AVX512PF, ANY_AVX512PF, false),
1044
  SUBARCH (avx512dq, AVX512DQ, ANY_AVX512DQ, false),
1045
  SUBARCH (avx512bw, AVX512BW, ANY_AVX512BW, false),
1046
  SUBARCH (avx512vl, AVX512VL, ANY_AVX512VL, false),
1047
  SUBARCH (monitor, MONITOR, MONITOR, false),
1048
  SUBARCH (vmx, VMX, ANY_VMX, false),
1049
  SUBARCH (vmfunc, VMFUNC, ANY_VMFUNC, false),
1050
  SUBARCH (smx, SMX, SMX, false),
1051
  SUBARCH (xsave, XSAVE, ANY_XSAVE, false),
1052
  SUBARCH (xsaveopt, XSAVEOPT, ANY_XSAVEOPT, false),
1053
  SUBARCH (xsavec, XSAVEC, ANY_XSAVEC, false),
1054
  SUBARCH (xsaves, XSAVES, ANY_XSAVES, false),
1055
  SUBARCH (aes, AES, ANY_AES, false),
1056
  SUBARCH (pclmul, PCLMUL, ANY_PCLMUL, false),
1057
  SUBARCH (clmul, PCLMUL, ANY_PCLMUL, true),
1058
  SUBARCH (fsgsbase, FSGSBASE, FSGSBASE, false),
1059
  SUBARCH (rdrnd, RDRND, RDRND, false),
1060
  SUBARCH (f16c, F16C, ANY_F16C, false),
1061
  SUBARCH (bmi2, BMI2, BMI2, false),
1062
  SUBARCH (fma, FMA, ANY_FMA, false),
1063
  SUBARCH (fma4, FMA4, ANY_FMA4, false),
1064
  SUBARCH (xop, XOP, ANY_XOP, false),
1065
  SUBARCH (lwp, LWP, ANY_LWP, false),
1066
  SUBARCH (movbe, MOVBE, MOVBE, false),
1067
  SUBARCH (cx16, CX16, CX16, false),
1068
  SUBARCH (lahf_sahf, LAHF_SAHF, LAHF_SAHF, false),
1069
  SUBARCH (ept, EPT, ANY_EPT, false),
1070
  SUBARCH (lzcnt, LZCNT, LZCNT, false),
1071
  SUBARCH (popcnt, POPCNT, POPCNT, false),
1072
  SUBARCH (hle, HLE, HLE, false),
1073
  SUBARCH (rtm, RTM, ANY_RTM, false),
1074
  SUBARCH (tsx, TSX, TSX, false),
1075
  SUBARCH (invpcid, INVPCID, INVPCID, false),
1076
  SUBARCH (clflush, CLFLUSH, CLFLUSH, false),
1077
  SUBARCH (nop, NOP, NOP, false),
1078
  SUBARCH (syscall, SYSCALL, SYSCALL, false),
1079
  SUBARCH (rdtscp, RDTSCP, RDTSCP, false),
1080
  SUBARCH (3dnow, 3DNOW, ANY_3DNOW, false),
1081
  SUBARCH (3dnowa, 3DNOWA, ANY_3DNOWA, false),
1082
  SUBARCH (padlock, PADLOCK, PADLOCK, false),
1083
  SUBARCH (pacifica, SVME, ANY_SVME, true),
1084
  SUBARCH (svme, SVME, ANY_SVME, false),
1085
  SUBARCH (abm, ABM, ABM, false),
1086
  SUBARCH (bmi, BMI, BMI, false),
1087
  SUBARCH (tbm, TBM, TBM, false),
1088
  SUBARCH (adx, ADX, ADX, false),
1089
  SUBARCH (rdseed, RDSEED, RDSEED, false),
1090
  SUBARCH (prfchw, PRFCHW, PRFCHW, false),
1091
  SUBARCH (smap, SMAP, SMAP, false),
1092
  SUBARCH (mpx, MPX, ANY_MPX, false),
1093
  SUBARCH (sha, SHA, ANY_SHA, false),
1094
  SUBARCH (clflushopt, CLFLUSHOPT, CLFLUSHOPT, false),
1095
  SUBARCH (prefetchwt1, PREFETCHWT1, PREFETCHWT1, false),
1096
  SUBARCH (se1, SE1, SE1, false),
1097
  SUBARCH (clwb, CLWB, CLWB, false),
1098
  SUBARCH (avx512ifma, AVX512IFMA, ANY_AVX512IFMA, false),
1099
  SUBARCH (avx512vbmi, AVX512VBMI, ANY_AVX512VBMI, false),
1100
  SUBARCH (avx512_4fmaps, AVX512_4FMAPS, ANY_AVX512_4FMAPS, false),
1101
  SUBARCH (avx512_4vnniw, AVX512_4VNNIW, ANY_AVX512_4VNNIW, false),
1102
  SUBARCH (avx512_vpopcntdq, AVX512_VPOPCNTDQ, ANY_AVX512_VPOPCNTDQ, false),
1103
  SUBARCH (avx512_vbmi2, AVX512_VBMI2, ANY_AVX512_VBMI2, false),
1104
  SUBARCH (avx512_vnni, AVX512_VNNI, ANY_AVX512_VNNI, false),
1105
  SUBARCH (avx512_bitalg, AVX512_BITALG, ANY_AVX512_BITALG, false),
1106
  SUBARCH (avx_vnni, AVX_VNNI, ANY_AVX_VNNI, false),
1107
  SUBARCH (clzero, CLZERO, CLZERO, false),
1108
  SUBARCH (mwaitx, MWAITX, MWAITX, false),
1109
  SUBARCH (ospke, OSPKE, ANY_OSPKE, false),
1110
  SUBARCH (rdpid, RDPID, RDPID, false),
1111
  SUBARCH (ptwrite, PTWRITE, PTWRITE, false),
1112
  SUBARCH (ibt, IBT, IBT, false),
1113
  SUBARCH (shstk, SHSTK, SHSTK, false),
1114
  SUBARCH (gfni, GFNI, ANY_GFNI, false),
1115
  SUBARCH (vaes, VAES, ANY_VAES, false),
1116
  SUBARCH (vpclmulqdq, VPCLMULQDQ, ANY_VPCLMULQDQ, false),
1117
  SUBARCH (wbnoinvd, WBNOINVD, WBNOINVD, false),
1118
  SUBARCH (pconfig, PCONFIG, PCONFIG, false),
1119
  SUBARCH (waitpkg, WAITPKG, WAITPKG, false),
1120
  SUBARCH (cldemote, CLDEMOTE, CLDEMOTE, false),
1121
  SUBARCH (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1122
  SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
1123
  SUBARCH (amx_fp16, AMX_FP16, ANY_AMX_FP16, false),
1124
  SUBARCH (amx_complex, AMX_COMPLEX, ANY_AMX_COMPLEX, false),
1125
  SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
1126
  SUBARCH (movdiri, MOVDIRI, MOVDIRI, false),
1127
  SUBARCH (movdir64b, MOVDIR64B, MOVDIR64B, false),
1128
  SUBARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, false),
1129
  SUBARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1130
     ANY_AVX512_VP2INTERSECT, false),
1131
  SUBARCH (tdx, TDX, TDX, false),
1132
  SUBARCH (enqcmd, ENQCMD, ENQCMD, false),
1133
  SUBARCH (serialize, SERIALIZE, SERIALIZE, false),
1134
  SUBARCH (rdpru, RDPRU, RDPRU, false),
1135
  SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
1136
  SUBARCH (sev_es, SEV_ES, ANY_SEV_ES, false),
1137
  SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
1138
  SUBARCH (kl, KL, ANY_KL, false),
1139
  SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
1140
  SUBARCH (uintr, UINTR, UINTR, false),
1141
  SUBARCH (hreset, HRESET, HRESET, false),
1142
  SUBARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, false),
1143
  SUBARCH (prefetchi, PREFETCHI, PREFETCHI, false),
1144
  SUBARCH (avx_ifma, AVX_IFMA, ANY_AVX_IFMA, false),
1145
  SUBARCH (avx_vnni_int8, AVX_VNNI_INT8, ANY_AVX_VNNI_INT8, false),
1146
  SUBARCH (cmpccxadd, CMPCCXADD, CMPCCXADD, false),
1147
  SUBARCH (wrmsrns, WRMSRNS, WRMSRNS, false),
1148
  SUBARCH (msrlist, MSRLIST, MSRLIST, false),
1149
  SUBARCH (avx_ne_convert, AVX_NE_CONVERT, ANY_AVX_NE_CONVERT, false),
1150
  SUBARCH (rao_int, RAO_INT, RAO_INT, false),
1151
  SUBARCH (rmpquery, RMPQUERY, ANY_RMPQUERY, false),
1152
  SUBARCH (fred, FRED, ANY_FRED, false),
1153
  SUBARCH (lkgs, LKGS, ANY_LKGS, false),
1154
};
1155
1156
#undef SUBARCH
1157
#undef ARCH
1158
1159
#ifdef I386COFF
1160
/* Like s_lcomm_internal in gas/read.c but the alignment string
1161
   is allowed to be optional.  */
1162
1163
static symbolS *
1164
pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1165
{
1166
  addressT align = 0;
1167
1168
  SKIP_WHITESPACE ();
1169
1170
  if (needs_align
1171
      && *input_line_pointer == ',')
1172
    {
1173
      align = parse_align (needs_align - 1);
1174
1175
      if (align == (addressT) -1)
1176
  return NULL;
1177
    }
1178
  else
1179
    {
1180
      if (size >= 8)
1181
  align = 3;
1182
      else if (size >= 4)
1183
  align = 2;
1184
      else if (size >= 2)
1185
  align = 1;
1186
      else
1187
  align = 0;
1188
    }
1189
1190
  bss_alloc (symbolP, size, align);
1191
  return symbolP;
1192
}
1193
1194
static void
1195
pe_lcomm (int needs_align)
1196
{
1197
  s_comm_internal (needs_align * 2, pe_lcomm_internal);
1198
}
1199
#endif
1200
1201
const pseudo_typeS md_pseudo_table[] =
1202
{
1203
#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1204
  {"align", s_align_bytes, 0},
1205
#else
1206
  {"align", s_align_ptwo, 0},
1207
#endif
1208
  {"arch", set_cpu_arch, 0},
1209
#ifndef I386COFF
1210
  {"bss", s_bss, 0},
1211
#else
1212
  {"lcomm", pe_lcomm, 1},
1213
#endif
1214
  {"ffloat", float_cons, 'f'},
1215
  {"dfloat", float_cons, 'd'},
1216
  {"tfloat", float_cons, 'x'},
1217
  {"hfloat", float_cons, 'h'},
1218
  {"bfloat16", float_cons, 'b'},
1219
  {"value", cons, 2},
1220
  {"slong", signed_cons, 4},
1221
  {"insn", s_insn, 0},
1222
  {"noopt", s_ignore, 0},
1223
  {"optim", s_ignore, 0},
1224
  {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1225
  {"code16", set_code_flag, CODE_16BIT},
1226
  {"code32", set_code_flag, CODE_32BIT},
1227
#ifdef BFD64
1228
  {"code64", set_code_flag, CODE_64BIT},
1229
#endif
1230
  {"intel_syntax", set_intel_syntax, 1},
1231
  {"att_syntax", set_intel_syntax, 0},
1232
  {"intel_mnemonic", set_intel_mnemonic, 1},
1233
  {"att_mnemonic", set_intel_mnemonic, 0},
1234
  {"allow_index_reg", set_allow_index_reg, 1},
1235
  {"disallow_index_reg", set_allow_index_reg, 0},
1236
  {"sse_check", set_check, 0},
1237
  {"operand_check", set_check, 1},
1238
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1239
  {"largecomm", handle_large_common, 0},
1240
#else
1241
  {"file", dwarf2_directive_file, 0},
1242
  {"loc", dwarf2_directive_loc, 0},
1243
  {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
1244
#endif
1245
#ifdef TE_PE
1246
  {"secrel32", pe_directive_secrel, 0},
1247
  {"secidx", pe_directive_secidx, 0},
1248
#endif
1249
  {0, 0, 0}
1250
};
1251
1252
/* For interface with expression ().  */
1253
extern char *input_line_pointer;
1254
1255
/* Hash table for instruction mnemonic lookup.  */
1256
static htab_t op_hash;
1257
1258
/* Hash table for register lookup.  */
1259
static htab_t reg_hash;
1260

1261
  /* Various efficient no-op patterns for aligning code labels.
1262
     Note: Don't try to assemble the instructions in the comments.
1263
     0L and 0w are not legal.  */
1264
static const unsigned char f32_1[] =
1265
  {0x90};       /* nop      */
1266
static const unsigned char f32_2[] =
1267
  {0x66,0x90};        /* xchg %ax,%ax   */
1268
static const unsigned char f32_3[] =
1269
  {0x8d,0x76,0x00};     /* leal 0(%esi),%esi  */
1270
static const unsigned char f32_4[] =
1271
  {0x8d,0x74,0x26,0x00};    /* leal 0(%esi,1),%esi  */
1272
static const unsigned char f32_6[] =
1273
  {0x8d,0xb6,0x00,0x00,0x00,0x00};  /* leal 0L(%esi),%esi */
1274
static const unsigned char f32_7[] =
1275
  {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
1276
static const unsigned char f16_3[] =
1277
  {0x8d,0x74,0x00};     /* lea 0(%si),%si */
1278
static const unsigned char f16_4[] =
1279
  {0x8d,0xb4,0x00,0x00};    /* lea 0W(%si),%si  */
1280
static const unsigned char jump_disp8[] =
1281
  {0xeb};       /* jmp disp8         */
1282
static const unsigned char jump32_disp32[] =
1283
  {0xe9};       /* jmp disp32        */
1284
static const unsigned char jump16_disp32[] =
1285
  {0x66,0xe9};        /* jmp disp32        */
1286
/* 32-bit NOPs patterns.  */
1287
static const unsigned char *const f32_patt[] = {
1288
  f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
1289
};
1290
/* 16-bit NOPs patterns.  */
1291
static const unsigned char *const f16_patt[] = {
1292
  f32_1, f32_2, f16_3, f16_4
1293
};
1294
/* nopl (%[re]ax) */
1295
static const unsigned char alt_3[] =
1296
  {0x0f,0x1f,0x00};
1297
/* nopl 0(%[re]ax) */
1298
static const unsigned char alt_4[] =
1299
  {0x0f,0x1f,0x40,0x00};
1300
/* nopl 0(%[re]ax,%[re]ax,1) */
1301
static const unsigned char alt_5[] =
1302
  {0x0f,0x1f,0x44,0x00,0x00};
1303
/* nopw 0(%[re]ax,%[re]ax,1) */
1304
static const unsigned char alt_6[] =
1305
  {0x66,0x0f,0x1f,0x44,0x00,0x00};
1306
/* nopl 0L(%[re]ax) */
1307
static const unsigned char alt_7[] =
1308
  {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1309
/* nopl 0L(%[re]ax,%[re]ax,1) */
1310
static const unsigned char alt_8[] =
1311
  {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1312
/* nopw 0L(%[re]ax,%[re]ax,1) */
1313
static const unsigned char alt_9[] =
1314
  {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1315
/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1316
static const unsigned char alt_10[] =
1317
  {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1318
/* data16 nopw %cs:0L(%eax,%eax,1) */
1319
static const unsigned char alt_11[] =
1320
  {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1321
/* 32-bit and 64-bit NOPs patterns.  */
1322
static const unsigned char *const alt_patt[] = {
1323
  f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
1324
  alt_9, alt_10, alt_11
1325
};
1326
1327
/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1328
   size of a single NOP instruction MAX_SINGLE_NOP_SIZE.  */
1329
1330
static void
1331
i386_output_nops (char *where, const unsigned char *const *patt,
1332
      int count, int max_single_nop_size)
1333
1334
0
{
1335
  /* Place the longer NOP first.  */
1336
0
  int last;
1337
0
  int offset;
1338
0
  const unsigned char *nops;
1339
1340
0
  if (max_single_nop_size < 1)
1341
0
    {
1342
0
      as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1343
0
    max_single_nop_size);
1344
0
      return;
1345
0
    }
1346
1347
0
  nops = patt[max_single_nop_size - 1];
1348
1349
  /* Use the smaller one if the requsted one isn't available.  */
1350
0
  if (nops == NULL)
1351
0
    {
1352
0
      max_single_nop_size--;
1353
0
      nops = patt[max_single_nop_size - 1];
1354
0
    }
1355
1356
0
  last = count % max_single_nop_size;
1357
1358
0
  count -= last;
1359
0
  for (offset = 0; offset < count; offset += max_single_nop_size)
1360
0
    memcpy (where + offset, nops, max_single_nop_size);
1361
1362
0
  if (last)
1363
0
    {
1364
0
      nops = patt[last - 1];
1365
0
      if (nops == NULL)
1366
0
  {
1367
    /* Use the smaller one plus one-byte NOP if the needed one
1368
       isn't available.  */
1369
0
    last--;
1370
0
    nops = patt[last - 1];
1371
0
    memcpy (where + offset, nops, last);
1372
0
    where[offset + last] = *patt[0];
1373
0
  }
1374
0
      else
1375
0
  memcpy (where + offset, nops, last);
1376
0
    }
1377
0
}
1378
1379
static INLINE int
1380
fits_in_imm7 (offsetT num)
1381
0
{
1382
0
  return (num & 0x7f) == num;
1383
0
}
1384
1385
static INLINE int
1386
fits_in_imm31 (offsetT num)
1387
0
{
1388
0
  return (num & 0x7fffffff) == num;
1389
0
}
1390
1391
/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1392
   single NOP instruction LIMIT.  */
1393
1394
void
1395
i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
1396
0
{
1397
0
  const unsigned char *const *patt = NULL;
1398
0
  int max_single_nop_size;
1399
  /* Maximum number of NOPs before switching to jump over NOPs.  */
1400
0
  int max_number_of_nops;
1401
1402
0
  switch (fragP->fr_type)
1403
0
    {
1404
0
    case rs_fill_nop:
1405
0
    case rs_align_code:
1406
0
      break;
1407
0
    case rs_machine_dependent:
1408
      /* Allow NOP padding for jumps and calls.  */
1409
0
      if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1410
0
    || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1411
0
  break;
1412
      /* Fall through.  */
1413
0
    default:
1414
0
      return;
1415
0
    }
1416
1417
  /* We need to decide which NOP sequence to use for 32bit and
1418
     64bit. When -mtune= is used:
1419
1420
     1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1421
     PROCESSOR_GENERIC32, f32_patt will be used.
1422
     2. For the rest, alt_patt will be used.
1423
1424
     When -mtune= isn't used, alt_patt will be used if
1425
     cpu_arch_isa_flags has CpuNop.  Otherwise, f32_patt will
1426
     be used.
1427
1428
     When -march= or .arch is used, we can't use anything beyond
1429
     cpu_arch_isa_flags.   */
1430
1431
0
  if (flag_code == CODE_16BIT)
1432
0
    {
1433
0
      patt = f16_patt;
1434
0
      max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1435
      /* Limit number of NOPs to 2 in 16-bit mode.  */
1436
0
      max_number_of_nops = 2;
1437
0
    }
1438
0
  else
1439
0
    {
1440
0
      if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
1441
0
  {
1442
    /* PROCESSOR_UNKNOWN means that all ISAs may be used.  */
1443
0
    switch (cpu_arch_tune)
1444
0
      {
1445
0
      case PROCESSOR_UNKNOWN:
1446
        /* We use cpu_arch_isa_flags to check if we SHOULD
1447
     optimize with nops.  */
1448
0
        if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1449
0
    patt = alt_patt;
1450
0
        else
1451
0
    patt = f32_patt;
1452
0
        break;
1453
0
      case PROCESSOR_PENTIUM4:
1454
0
      case PROCESSOR_NOCONA:
1455
0
      case PROCESSOR_CORE:
1456
0
      case PROCESSOR_CORE2:
1457
0
      case PROCESSOR_COREI7:
1458
0
      case PROCESSOR_GENERIC64:
1459
0
      case PROCESSOR_K6:
1460
0
      case PROCESSOR_ATHLON:
1461
0
      case PROCESSOR_K8:
1462
0
      case PROCESSOR_AMDFAM10:
1463
0
      case PROCESSOR_BD:
1464
0
      case PROCESSOR_ZNVER:
1465
0
      case PROCESSOR_BT:
1466
0
        patt = alt_patt;
1467
0
        break;
1468
0
      case PROCESSOR_I386:
1469
0
      case PROCESSOR_I486:
1470
0
      case PROCESSOR_PENTIUM:
1471
0
      case PROCESSOR_PENTIUMPRO:
1472
0
      case PROCESSOR_IAMCU:
1473
0
      case PROCESSOR_GENERIC32:
1474
0
        patt = f32_patt;
1475
0
        break;
1476
0
      case PROCESSOR_NONE:
1477
0
        abort ();
1478
0
      }
1479
0
  }
1480
0
      else
1481
0
  {
1482
0
    switch (fragP->tc_frag_data.tune)
1483
0
      {
1484
0
      case PROCESSOR_UNKNOWN:
1485
        /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
1486
     PROCESSOR_UNKNOWN.  */
1487
0
        abort ();
1488
0
        break;
1489
1490
0
      case PROCESSOR_I386:
1491
0
      case PROCESSOR_I486:
1492
0
      case PROCESSOR_PENTIUM:
1493
0
      case PROCESSOR_IAMCU:
1494
0
      case PROCESSOR_K6:
1495
0
      case PROCESSOR_ATHLON:
1496
0
      case PROCESSOR_K8:
1497
0
      case PROCESSOR_AMDFAM10:
1498
0
      case PROCESSOR_BD:
1499
0
      case PROCESSOR_ZNVER:
1500
0
      case PROCESSOR_BT:
1501
0
      case PROCESSOR_GENERIC32:
1502
        /* We use cpu_arch_isa_flags to check if we CAN optimize
1503
     with nops.  */
1504
0
        if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1505
0
    patt = alt_patt;
1506
0
        else
1507
0
    patt = f32_patt;
1508
0
        break;
1509
0
      case PROCESSOR_PENTIUMPRO:
1510
0
      case PROCESSOR_PENTIUM4:
1511
0
      case PROCESSOR_NOCONA:
1512
0
      case PROCESSOR_CORE:
1513
0
      case PROCESSOR_CORE2:
1514
0
      case PROCESSOR_COREI7:
1515
0
        if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
1516
0
    patt = alt_patt;
1517
0
        else
1518
0
    patt = f32_patt;
1519
0
        break;
1520
0
      case PROCESSOR_GENERIC64:
1521
0
        patt = alt_patt;
1522
0
        break;
1523
0
      case PROCESSOR_NONE:
1524
0
        abort ();
1525
0
      }
1526
0
  }
1527
1528
0
      if (patt == f32_patt)
1529
0
  {
1530
0
    max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1531
    /* Limit number of NOPs to 2 for older processors.  */
1532
0
    max_number_of_nops = 2;
1533
0
  }
1534
0
      else
1535
0
  {
1536
0
    max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1537
    /* Limit number of NOPs to 7 for newer processors.  */
1538
0
    max_number_of_nops = 7;
1539
0
  }
1540
0
    }
1541
1542
0
  if (limit == 0)
1543
0
    limit = max_single_nop_size;
1544
1545
0
  if (fragP->fr_type == rs_fill_nop)
1546
0
    {
1547
      /* Output NOPs for .nop directive.  */
1548
0
      if (limit > max_single_nop_size)
1549
0
  {
1550
0
    as_bad_where (fragP->fr_file, fragP->fr_line,
1551
0
      _("invalid single nop size: %d "
1552
0
        "(expect within [0, %d])"),
1553
0
      limit, max_single_nop_size);
1554
0
    return;
1555
0
  }
1556
0
    }
1557
0
  else if (fragP->fr_type != rs_machine_dependent)
1558
0
    fragP->fr_var = count;
1559
1560
0
  if ((count / max_single_nop_size) > max_number_of_nops)
1561
0
    {
1562
      /* Generate jump over NOPs.  */
1563
0
      offsetT disp = count - 2;
1564
0
      if (fits_in_imm7 (disp))
1565
0
  {
1566
    /* Use "jmp disp8" if possible.  */
1567
0
    count = disp;
1568
0
    where[0] = jump_disp8[0];
1569
0
    where[1] = count;
1570
0
    where += 2;
1571
0
  }
1572
0
      else
1573
0
  {
1574
0
    unsigned int size_of_jump;
1575
1576
0
    if (flag_code == CODE_16BIT)
1577
0
      {
1578
0
        where[0] = jump16_disp32[0];
1579
0
        where[1] = jump16_disp32[1];
1580
0
        size_of_jump = 2;
1581
0
      }
1582
0
    else
1583
0
      {
1584
0
        where[0] = jump32_disp32[0];
1585
0
        size_of_jump = 1;
1586
0
      }
1587
1588
0
    count -= size_of_jump + 4;
1589
0
    if (!fits_in_imm31 (count))
1590
0
      {
1591
0
        as_bad_where (fragP->fr_file, fragP->fr_line,
1592
0
          _("jump over nop padding out of range"));
1593
0
        return;
1594
0
      }
1595
1596
0
    md_number_to_chars (where + size_of_jump, count, 4);
1597
0
    where += size_of_jump + 4;
1598
0
  }
1599
0
    }
1600
1601
  /* Generate multiple NOPs.  */
1602
0
  i386_output_nops (where, patt, count, limit);
1603
0
}
1604
1605
static INLINE int
1606
operand_type_all_zero (const union i386_operand_type *x)
1607
257k
{
1608
257k
  switch (ARRAY_SIZE(x->array))
1609
257k
    {
1610
0
    case 3:
1611
0
      if (x->array[2])
1612
0
  return 0;
1613
      /* Fall through.  */
1614
0
    case 2:
1615
0
      if (x->array[1])
1616
0
  return 0;
1617
      /* Fall through.  */
1618
257k
    case 1:
1619
257k
      return !x->array[0];
1620
0
    default:
1621
0
      abort ();
1622
257k
    }
1623
257k
}
1624
1625
static INLINE void
1626
operand_type_set (union i386_operand_type *x, unsigned int v)
1627
300k
{
1628
300k
  switch (ARRAY_SIZE(x->array))
1629
300k
    {
1630
0
    case 3:
1631
0
      x->array[2] = v;
1632
      /* Fall through.  */
1633
0
    case 2:
1634
0
      x->array[1] = v;
1635
      /* Fall through.  */
1636
300k
    case 1:
1637
300k
      x->array[0] = v;
1638
      /* Fall through.  */
1639
300k
      break;
1640
0
    default:
1641
0
      abort ();
1642
300k
    }
1643
1644
300k
  x->bitfield.class = ClassNone;
1645
300k
  x->bitfield.instance = InstanceNone;
1646
300k
}
1647
1648
static INLINE int
1649
operand_type_equal (const union i386_operand_type *x,
1650
        const union i386_operand_type *y)
1651
0
{
1652
0
  switch (ARRAY_SIZE(x->array))
1653
0
    {
1654
0
    case 3:
1655
0
      if (x->array[2] != y->array[2])
1656
0
  return 0;
1657
      /* Fall through.  */
1658
0
    case 2:
1659
0
      if (x->array[1] != y->array[1])
1660
0
  return 0;
1661
      /* Fall through.  */
1662
0
    case 1:
1663
0
      return x->array[0] == y->array[0];
1664
0
      break;
1665
0
    default:
1666
0
      abort ();
1667
0
    }
1668
0
}
1669
1670
static INLINE int
1671
cpu_flags_all_zero (const union i386_cpu_flags *x)
1672
798k
{
1673
798k
  switch (ARRAY_SIZE(x->array))
1674
798k
    {
1675
798k
    case 5:
1676
798k
      if (x->array[4])
1677
218
  return 0;
1678
      /* Fall through.  */
1679
798k
    case 4:
1680
798k
      if (x->array[3])
1681
1.94k
  return 0;
1682
      /* Fall through.  */
1683
796k
    case 3:
1684
796k
      if (x->array[2])
1685
1.52k
  return 0;
1686
      /* Fall through.  */
1687
795k
    case 2:
1688
795k
      if (x->array[1])
1689
16.9k
  return 0;
1690
      /* Fall through.  */
1691
778k
    case 1:
1692
778k
      return !x->array[0];
1693
0
    default:
1694
0
      abort ();
1695
798k
    }
1696
798k
}
1697
1698
static INLINE int
1699
cpu_flags_equal (const union i386_cpu_flags *x,
1700
     const union i386_cpu_flags *y)
1701
171
{
1702
171
  switch (ARRAY_SIZE(x->array))
1703
171
    {
1704
171
    case 5:
1705
171
      if (x->array[4] != y->array[4])
1706
0
  return 0;
1707
      /* Fall through.  */
1708
171
    case 4:
1709
171
      if (x->array[3] != y->array[3])
1710
2
  return 0;
1711
      /* Fall through.  */
1712
169
    case 3:
1713
169
      if (x->array[2] != y->array[2])
1714
0
  return 0;
1715
      /* Fall through.  */
1716
169
    case 2:
1717
169
      if (x->array[1] != y->array[1])
1718
0
  return 0;
1719
      /* Fall through.  */
1720
169
    case 1:
1721
169
      return x->array[0] == y->array[0];
1722
0
      break;
1723
0
    default:
1724
0
      abort ();
1725
171
    }
1726
171
}
1727
1728
static INLINE int
1729
cpu_flags_check_cpu64 (i386_cpu_flags f)
1730
588k
{
1731
588k
  return !((flag_code == CODE_64BIT && f.bitfield.cpuno64)
1732
588k
     || (flag_code != CODE_64BIT && f.bitfield.cpu64));
1733
588k
}
1734
1735
static INLINE i386_cpu_flags
1736
cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
1737
211k
{
1738
211k
  switch (ARRAY_SIZE (x.array))
1739
211k
    {
1740
211k
    case 5:
1741
211k
      x.array [4] &= y.array [4];
1742
      /* Fall through.  */
1743
211k
    case 4:
1744
211k
      x.array [3] &= y.array [3];
1745
      /* Fall through.  */
1746
211k
    case 3:
1747
211k
      x.array [2] &= y.array [2];
1748
      /* Fall through.  */
1749
211k
    case 2:
1750
211k
      x.array [1] &= y.array [1];
1751
      /* Fall through.  */
1752
211k
    case 1:
1753
211k
      x.array [0] &= y.array [0];
1754
211k
      break;
1755
0
    default:
1756
0
      abort ();
1757
211k
    }
1758
211k
  return x;
1759
211k
}
1760
1761
static INLINE i386_cpu_flags
1762
cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
1763
340
{
1764
340
  switch (ARRAY_SIZE (x.array))
1765
340
    {
1766
340
    case 5:
1767
340
      x.array [4] |= y.array [4];
1768
      /* Fall through.  */
1769
340
    case 4:
1770
340
      x.array [3] |= y.array [3];
1771
      /* Fall through.  */
1772
340
    case 3:
1773
340
      x.array [2] |= y.array [2];
1774
      /* Fall through.  */
1775
340
    case 2:
1776
340
      x.array [1] |= y.array [1];
1777
      /* Fall through.  */
1778
340
    case 1:
1779
340
      x.array [0] |= y.array [0];
1780
340
      break;
1781
0
    default:
1782
0
      abort ();
1783
340
    }
1784
340
  return x;
1785
340
}
1786
1787
static INLINE i386_cpu_flags
1788
cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1789
0
{
1790
0
  switch (ARRAY_SIZE (x.array))
1791
0
    {
1792
0
    case 5:
1793
0
      x.array [4] &= ~y.array [4];
1794
      /* Fall through.  */
1795
0
    case 4:
1796
0
      x.array [3] &= ~y.array [3];
1797
      /* Fall through.  */
1798
0
    case 3:
1799
0
      x.array [2] &= ~y.array [2];
1800
      /* Fall through.  */
1801
0
    case 2:
1802
0
      x.array [1] &= ~y.array [1];
1803
      /* Fall through.  */
1804
0
    case 1:
1805
0
      x.array [0] &= ~y.array [0];
1806
0
      break;
1807
0
    default:
1808
0
      abort ();
1809
0
    }
1810
0
  return x;
1811
0
}
1812
1813
static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1814
1815
1.16M
#define CPU_FLAGS_ARCH_MATCH    0x1
1816
1.16M
#define CPU_FLAGS_64BIT_MATCH   0x2
1817
1818
#define CPU_FLAGS_PERFECT_MATCH \
1819
585k
  (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
1820
1821
/* Return CPU flags match bits. */
1822
1823
static int
1824
cpu_flags_match (const insn_template *t)
1825
585k
{
1826
585k
  i386_cpu_flags x = t->cpu_flags;
1827
585k
  int match = cpu_flags_check_cpu64 (x) ? CPU_FLAGS_64BIT_MATCH : 0;
1828
1829
585k
  x.bitfield.cpu64 = 0;
1830
585k
  x.bitfield.cpuno64 = 0;
1831
1832
585k
  if (cpu_flags_all_zero (&x))
1833
456k
    {
1834
      /* This instruction is available on all archs.  */
1835
456k
      match |= CPU_FLAGS_ARCH_MATCH;
1836
456k
    }
1837
129k
  else
1838
129k
    {
1839
      /* This instruction is available only on some archs.  */
1840
129k
      i386_cpu_flags cpu = cpu_arch_flags;
1841
1842
      /* AVX512VL is no standalone feature - match it and then strip it.  */
1843
129k
      if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1844
1
  return match;
1845
129k
      x.bitfield.cpuavx512vl = 0;
1846
1847
      /* AVX and AVX2 present at the same time express an operand size
1848
   dependency - strip AVX2 for the purposes here.  The operand size
1849
   dependent check occurs in check_vecOperands().  */
1850
129k
      if (x.bitfield.cpuavx && x.bitfield.cpuavx2)
1851
348
  x.bitfield.cpuavx2 = 0;
1852
1853
129k
      cpu = cpu_flags_and (x, cpu);
1854
129k
      if (!cpu_flags_all_zero (&cpu))
1855
127k
  {
1856
127k
    if (x.bitfield.cpuavx)
1857
7.56k
      {
1858
        /* We need to check a few extra flags with AVX.  */
1859
7.56k
        if (cpu.bitfield.cpuavx
1860
7.56k
      && (!t->opcode_modifier.sse2avx
1861
7.56k
          || (sse2avx && !i.prefix[DATA_PREFIX]))
1862
7.56k
      && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
1863
7.56k
      && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1864
7.56k
      && (!x.bitfield.cpupclmul || cpu.bitfield.cpupclmul))
1865
1.53k
    match |= CPU_FLAGS_ARCH_MATCH;
1866
7.56k
      }
1867
120k
    else if (x.bitfield.cpuavx512f)
1868
717
      {
1869
        /* We need to check a few extra flags with AVX512F.  */
1870
717
        if (cpu.bitfield.cpuavx512f
1871
717
      && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1872
717
      && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1873
717
      && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1874
717
    match |= CPU_FLAGS_ARCH_MATCH;
1875
717
      }
1876
119k
    else
1877
119k
      match |= CPU_FLAGS_ARCH_MATCH;
1878
127k
  }
1879
129k
    }
1880
585k
  return match;
1881
585k
}
1882
1883
static INLINE i386_operand_type
1884
operand_type_and (i386_operand_type x, i386_operand_type y)
1885
828k
{
1886
828k
  if (x.bitfield.class != y.bitfield.class)
1887
403k
    x.bitfield.class = ClassNone;
1888
828k
  if (x.bitfield.instance != y.bitfield.instance)
1889
688
    x.bitfield.instance = InstanceNone;
1890
1891
828k
  switch (ARRAY_SIZE (x.array))
1892
828k
    {
1893
0
    case 3:
1894
0
      x.array [2] &= y.array [2];
1895
      /* Fall through.  */
1896
0
    case 2:
1897
0
      x.array [1] &= y.array [1];
1898
      /* Fall through.  */
1899
828k
    case 1:
1900
828k
      x.array [0] &= y.array [0];
1901
828k
      break;
1902
0
    default:
1903
0
      abort ();
1904
828k
    }
1905
828k
  return x;
1906
828k
}
1907
1908
static INLINE i386_operand_type
1909
operand_type_and_not (i386_operand_type x, i386_operand_type y)
1910
191k
{
1911
191k
  gas_assert (y.bitfield.class == ClassNone);
1912
191k
  gas_assert (y.bitfield.instance == InstanceNone);
1913
1914
191k
  switch (ARRAY_SIZE (x.array))
1915
191k
    {
1916
0
    case 3:
1917
0
      x.array [2] &= ~y.array [2];
1918
      /* Fall through.  */
1919
0
    case 2:
1920
0
      x.array [1] &= ~y.array [1];
1921
      /* Fall through.  */
1922
191k
    case 1:
1923
191k
      x.array [0] &= ~y.array [0];
1924
191k
      break;
1925
0
    default:
1926
0
      abort ();
1927
191k
    }
1928
191k
  return x;
1929
191k
}
1930
1931
static INLINE i386_operand_type
1932
operand_type_or (i386_operand_type x, i386_operand_type y)
1933
337k
{
1934
337k
  gas_assert (x.bitfield.class == ClassNone ||
1935
337k
              y.bitfield.class == ClassNone ||
1936
337k
              x.bitfield.class == y.bitfield.class);
1937
337k
  gas_assert (x.bitfield.instance == InstanceNone ||
1938
337k
              y.bitfield.instance == InstanceNone ||
1939
337k
              x.bitfield.instance == y.bitfield.instance);
1940
1941
337k
  switch (ARRAY_SIZE (x.array))
1942
337k
    {
1943
0
    case 3:
1944
0
      x.array [2] |= y.array [2];
1945
      /* Fall through.  */
1946
0
    case 2:
1947
0
      x.array [1] |= y.array [1];
1948
      /* Fall through.  */
1949
337k
    case 1:
1950
337k
      x.array [0] |= y.array [0];
1951
337k
      break;
1952
0
    default:
1953
0
      abort ();
1954
337k
    }
1955
337k
  return x;
1956
337k
}
1957
1958
static INLINE i386_operand_type
1959
operand_type_xor (i386_operand_type x, i386_operand_type y)
1960
0
{
1961
0
  gas_assert (y.bitfield.class == ClassNone);
1962
0
  gas_assert (y.bitfield.instance == InstanceNone);
1963
1964
0
  switch (ARRAY_SIZE (x.array))
1965
0
    {
1966
0
    case 3:
1967
0
      x.array [2] ^= y.array [2];
1968
      /* Fall through.  */
1969
0
    case 2:
1970
0
      x.array [1] ^= y.array [1];
1971
      /* Fall through.  */
1972
0
    case 1:
1973
0
      x.array [0] ^= y.array [0];
1974
0
      break;
1975
0
    default:
1976
0
      abort ();
1977
0
    }
1978
0
  return x;
1979
0
}
1980
1981
static const i386_operand_type anydisp = {
1982
  .bitfield = { .disp8 = 1, .disp16 = 1, .disp32 = 1, .disp64 = 1 }
1983
};
1984
1985
enum operand_type
1986
{
1987
  reg,
1988
  imm,
1989
  disp,
1990
  anymem
1991
};
1992
1993
static INLINE int
1994
operand_type_check (i386_operand_type t, enum operand_type c)
1995
1.11M
{
1996
1.11M
  switch (c)
1997
1.11M
    {
1998
0
    case reg:
1999
0
      return t.bitfield.class == Reg;
2000
2001
385k
    case imm:
2002
385k
      return (t.bitfield.imm8
2003
385k
        || t.bitfield.imm8s
2004
385k
        || t.bitfield.imm16
2005
385k
        || t.bitfield.imm32
2006
385k
        || t.bitfield.imm32s
2007
385k
        || t.bitfield.imm64);
2008
2009
727k
    case disp:
2010
727k
      return (t.bitfield.disp8
2011
727k
        || t.bitfield.disp16
2012
727k
        || t.bitfield.disp32
2013
727k
        || t.bitfield.disp64);
2014
2015
0
    case anymem:
2016
0
      return (t.bitfield.disp8
2017
0
        || t.bitfield.disp16
2018
0
        || t.bitfield.disp32
2019
0
        || t.bitfield.disp64
2020
0
        || t.bitfield.baseindex);
2021
2022
0
    default:
2023
0
      abort ();
2024
1.11M
    }
2025
2026
0
  return 0;
2027
1.11M
}
2028
2029
/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2030
   between operand GIVEN and opeand WANTED for instruction template T.  */
2031
2032
static INLINE int
2033
match_operand_size (const insn_template *t, unsigned int wanted,
2034
        unsigned int given)
2035
593k
{
2036
593k
  return !((i.types[given].bitfield.byte
2037
593k
      && !t->operand_types[wanted].bitfield.byte)
2038
593k
     || (i.types[given].bitfield.word
2039
593k
         && !t->operand_types[wanted].bitfield.word)
2040
593k
     || (i.types[given].bitfield.dword
2041
593k
         && !t->operand_types[wanted].bitfield.dword)
2042
593k
     || (i.types[given].bitfield.qword
2043
593k
         && (!t->operand_types[wanted].bitfield.qword
2044
       /* Don't allow 64-bit (memory) operands outside of 64-bit
2045
          mode, when they're used where a 64-bit GPR could also
2046
          be used.  Checking is needed for Intel Syntax only.  */
2047
0
       || (intel_syntax
2048
0
           && flag_code != CODE_64BIT
2049
0
           && (t->operand_types[wanted].bitfield.class == Reg
2050
0
         || t->operand_types[wanted].bitfield.class == Accum
2051
0
         || t->opcode_modifier.isstring))))
2052
593k
     || (i.types[given].bitfield.tbyte
2053
593k
         && !t->operand_types[wanted].bitfield.tbyte));
2054
593k
}
2055
2056
/* Return 1 if there is no conflict in SIMD register between operand
2057
   GIVEN and opeand WANTED for instruction template T.  */
2058
2059
static INLINE int
2060
match_simd_size (const insn_template *t, unsigned int wanted,
2061
     unsigned int given)
2062
179k
{
2063
179k
  return !((i.types[given].bitfield.xmmword
2064
179k
      && !t->operand_types[wanted].bitfield.xmmword)
2065
179k
     || (i.types[given].bitfield.ymmword
2066
179k
         && !t->operand_types[wanted].bitfield.ymmword)
2067
179k
     || (i.types[given].bitfield.zmmword
2068
179k
         && !t->operand_types[wanted].bitfield.zmmword)
2069
179k
     || (i.types[given].bitfield.tmmword
2070
179k
         && !t->operand_types[wanted].bitfield.tmmword));
2071
179k
}
2072
2073
/* Return 1 if there is no conflict in any size between operand GIVEN
2074
   and opeand WANTED for instruction template T.  */
2075
2076
static INLINE int
2077
match_mem_size (const insn_template *t, unsigned int wanted,
2078
    unsigned int given)
2079
273k
{
2080
273k
  return (match_operand_size (t, wanted, given)
2081
273k
    && !((i.types[given].bitfield.unspecified
2082
273k
    && !i.broadcast.type
2083
273k
    && !i.broadcast.bytes
2084
273k
    && !t->operand_types[wanted].bitfield.unspecified)
2085
273k
         || (i.types[given].bitfield.fword
2086
162k
       && !t->operand_types[wanted].bitfield.fword)
2087
         /* For scalar opcode templates to allow register and memory
2088
      operands at the same time, some special casing is needed
2089
      here.  Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2090
      down-conversion vpmov*.  */
2091
273k
         || ((t->operand_types[wanted].bitfield.class == RegSIMD
2092
162k
        && t->operand_types[wanted].bitfield.byte
2093
14
           + t->operand_types[wanted].bitfield.word
2094
14
           + t->operand_types[wanted].bitfield.dword
2095
14
           + t->operand_types[wanted].bitfield.qword
2096
14
           > !!t->opcode_modifier.broadcast)
2097
162k
       ? (i.types[given].bitfield.xmmword
2098
0
          || i.types[given].bitfield.ymmword
2099
0
          || i.types[given].bitfield.zmmword)
2100
162k
       : !match_simd_size(t, wanted, given))));
2101
273k
}
2102
2103
/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2104
   operands for instruction template T, and it has MATCH_REVERSE set if there
2105
   is no size conflict on any operands for the template with operands reversed
2106
   (and the template allows for reversing in the first place).  */
2107
2108
459k
#define MATCH_STRAIGHT 1
2109
72.5k
#define MATCH_REVERSE  2
2110
2111
static INLINE unsigned int
2112
operand_size_match (const insn_template *t)
2113
303k
{
2114
303k
  unsigned int j, match = MATCH_STRAIGHT;
2115
2116
  /* Don't check non-absolute jump instructions.  */
2117
303k
  if (t->opcode_modifier.jump
2118
303k
      && t->opcode_modifier.jump != JUMP_ABSOLUTE)
2119
5.27k
    return match;
2120
2121
  /* Check memory and accumulator operand size.  */
2122
630k
  for (j = 0; j < i.operands; j++)
2123
365k
    {
2124
365k
      if (i.types[j].bitfield.class != Reg
2125
365k
    && i.types[j].bitfield.class != RegSIMD
2126
365k
    && t->opcode_modifier.operandconstraint == ANY_SIZE)
2127
200
  continue;
2128
2129
364k
      if (t->operand_types[j].bitfield.class == Reg
2130
364k
    && !match_operand_size (t, j, j))
2131
0
  {
2132
0
    match = 0;
2133
0
    break;
2134
0
  }
2135
2136
364k
      if (t->operand_types[j].bitfield.class == RegSIMD
2137
364k
    && !match_simd_size (t, j, j))
2138
0
  {
2139
0
    match = 0;
2140
0
    break;
2141
0
  }
2142
2143
364k
      if (t->operand_types[j].bitfield.instance == Accum
2144
364k
    && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
2145
0
  {
2146
0
    match = 0;
2147
0
    break;
2148
0
  }
2149
2150
364k
      if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
2151
32.5k
  {
2152
32.5k
    match = 0;
2153
32.5k
    break;
2154
32.5k
  }
2155
364k
    }
2156
2157
297k
  if (!t->opcode_modifier.d)
2158
218k
    return match;
2159
2160
  /* Check reverse.  */
2161
79.2k
  gas_assert (i.operands >= 2);
2162
2163
81.0k
  for (j = 0; j < i.operands; j++)
2164
80.8k
    {
2165
80.8k
      unsigned int given = i.operands - j - 1;
2166
2167
      /* For FMA4 and XOP insns VEX.W controls just the first two
2168
   register operands.  */
2169
80.8k
      if (t->cpu_flags.bitfield.cpufma4 || t->cpu_flags.bitfield.cpuxop)
2170
0
  given = j < 2 ? 1 - j : j;
2171
2172
80.8k
      if (t->operand_types[j].bitfield.class == Reg
2173
80.8k
    && !match_operand_size (t, j, given))
2174
0
  return match;
2175
2176
80.8k
      if (t->operand_types[j].bitfield.class == RegSIMD
2177
80.8k
    && !match_simd_size (t, j, given))
2178
0
  return match;
2179
2180
80.8k
      if (t->operand_types[j].bitfield.instance == Accum
2181
80.8k
    && (!match_operand_size (t, j, given)
2182
1.16k
        || !match_simd_size (t, j, given)))
2183
0
  return match;
2184
2185
80.8k
      if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
2186
79.0k
  return match;
2187
80.8k
    }
2188
2189
202
  return match | MATCH_REVERSE;
2190
79.2k
}
2191
2192
static INLINE int
2193
operand_type_match (i386_operand_type overlap,
2194
        i386_operand_type given)
2195
239k
{
2196
239k
  i386_operand_type temp = overlap;
2197
2198
239k
  temp.bitfield.unspecified = 0;
2199
239k
  temp.bitfield.byte = 0;
2200
239k
  temp.bitfield.word = 0;
2201
239k
  temp.bitfield.dword = 0;
2202
239k
  temp.bitfield.fword = 0;
2203
239k
  temp.bitfield.qword = 0;
2204
239k
  temp.bitfield.tbyte = 0;
2205
239k
  temp.bitfield.xmmword = 0;
2206
239k
  temp.bitfield.ymmword = 0;
2207
239k
  temp.bitfield.zmmword = 0;
2208
239k
  temp.bitfield.tmmword = 0;
2209
239k
  if (operand_type_all_zero (&temp))
2210
83.5k
    goto mismatch;
2211
2212
155k
  if (given.bitfield.baseindex == overlap.bitfield.baseindex)
2213
155k
    return 1;
2214
2215
83.5k
 mismatch:
2216
83.5k
  i.error = operand_type_mismatch;
2217
83.5k
  return 0;
2218
155k
}
2219
2220
/* If given types g0 and g1 are registers they must be of the same type
2221
   unless the expected operand type register overlap is null.
2222
   Intel syntax sized memory operands are also checked here.  */
2223
2224
static INLINE int
2225
operand_type_register_match (i386_operand_type g0,
2226
           i386_operand_type t0,
2227
           i386_operand_type g1,
2228
           i386_operand_type t1)
2229
3
{
2230
3
  if (g0.bitfield.class != Reg
2231
3
      && g0.bitfield.class != RegSIMD
2232
3
      && (g0.bitfield.unspecified
2233
3
    || !operand_type_check (g0, anymem)))
2234
3
    return 1;
2235
2236
0
  if (g1.bitfield.class != Reg
2237
0
      && g1.bitfield.class != RegSIMD
2238
0
      && (g1.bitfield.unspecified
2239
0
    || !operand_type_check (g1, anymem)))
2240
0
    return 1;
2241
2242
0
  if (g0.bitfield.byte == g1.bitfield.byte
2243
0
      && g0.bitfield.word == g1.bitfield.word
2244
0
      && g0.bitfield.dword == g1.bitfield.dword
2245
0
      && g0.bitfield.qword == g1.bitfield.qword
2246
0
      && g0.bitfield.xmmword == g1.bitfield.xmmword
2247
0
      && g0.bitfield.ymmword == g1.bitfield.ymmword
2248
0
      && g0.bitfield.zmmword == g1.bitfield.zmmword)
2249
0
    return 1;
2250
2251
  /* If expectations overlap in no more than a single size, all is fine. */
2252
0
  g0 = operand_type_and (t0, t1);
2253
0
  if (g0.bitfield.byte
2254
0
      + g0.bitfield.word
2255
0
      + g0.bitfield.dword
2256
0
      + g0.bitfield.qword
2257
0
      + g0.bitfield.xmmword
2258
0
      + g0.bitfield.ymmword
2259
0
      + g0.bitfield.zmmword <= 1)
2260
0
    return 1;
2261
2262
0
  i.error = register_type_mismatch;
2263
2264
0
  return 0;
2265
0
}
2266
2267
static INLINE unsigned int
2268
register_number (const reg_entry *r)
2269
0
{
2270
0
  unsigned int nr = r->reg_num;
2271
2272
0
  if (r->reg_flags & RegRex)
2273
0
    nr += 8;
2274
2275
0
  if (r->reg_flags & RegVRex)
2276
0
    nr += 16;
2277
2278
0
  return nr;
2279
0
}
2280
2281
static INLINE unsigned int
2282
mode_from_disp_size (i386_operand_type t)
2283
0
{
2284
0
  if (t.bitfield.disp8)
2285
0
    return 1;
2286
0
  else if (t.bitfield.disp16
2287
0
     || t.bitfield.disp32)
2288
0
    return 2;
2289
0
  else
2290
0
    return 0;
2291
0
}
2292
2293
static INLINE int
2294
fits_in_signed_byte (addressT num)
2295
137k
{
2296
137k
  return num + 0x80 <= 0xff;
2297
137k
}
2298
2299
static INLINE int
2300
fits_in_unsigned_byte (addressT num)
2301
55.5k
{
2302
55.5k
  return num <= 0xff;
2303
55.5k
}
2304
2305
static INLINE int
2306
fits_in_unsigned_word (addressT num)
2307
89.8k
{
2308
89.8k
  return num <= 0xffff;
2309
89.8k
}
2310
2311
static INLINE int
2312
fits_in_signed_word (addressT num)
2313
2.36k
{
2314
2.36k
  return num + 0x8000 <= 0xffff;
2315
2.36k
}
2316
2317
static INLINE int
2318
fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
2319
61.2k
{
2320
#ifndef BFD64
2321
  return 1;
2322
#else
2323
61.2k
  return num + 0x80000000 <= 0xffffffff;
2324
61.2k
#endif
2325
61.2k
}        /* fits_in_signed_long() */
2326
2327
static INLINE int
2328
fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
2329
334k
{
2330
#ifndef BFD64
2331
  return 1;
2332
#else
2333
334k
  return num <= 0xffffffff;
2334
334k
#endif
2335
334k
}        /* fits_in_unsigned_long() */
2336
2337
static INLINE valueT extend_to_32bit_address (addressT num)
2338
0
{
2339
0
#ifdef BFD64
2340
0
  if (fits_in_unsigned_long(num))
2341
0
    return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2342
2343
0
  if (!fits_in_signed_long (num))
2344
0
    return num & 0xffffffff;
2345
0
#endif
2346
2347
0
  return num;
2348
0
}
2349
2350
static INLINE int
2351
fits_in_disp8 (offsetT num)
2352
81.7k
{
2353
81.7k
  int shift = i.memshift;
2354
81.7k
  unsigned int mask;
2355
2356
81.7k
  if (shift == -1)
2357
0
    abort ();
2358
2359
81.7k
  mask = (1 << shift) - 1;
2360
2361
  /* Return 0 if NUM isn't properly aligned.  */
2362
81.7k
  if ((num & mask))
2363
0
    return 0;
2364
2365
  /* Check if NUM will fit in 8bit after shift.  */
2366
81.7k
  return fits_in_signed_byte (num >> shift);
2367
81.7k
}
2368
2369
static INLINE int
2370
fits_in_imm4 (offsetT num)
2371
0
{
2372
  /* Despite the name, check for imm3 if we're dealing with EVEX.  */
2373
0
  return (num & (i.vec_encoding != vex_encoding_evex ? 0xf : 7)) == num;
2374
0
}
2375
2376
static i386_operand_type
2377
smallest_imm_type (offsetT num)
2378
66.1k
{
2379
66.1k
  i386_operand_type t;
2380
2381
66.1k
  operand_type_set (&t, 0);
2382
66.1k
  t.bitfield.imm64 = 1;
2383
2384
66.1k
  if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
2385
10.6k
    {
2386
      /* This code is disabled on the 486 because all the Imm1 forms
2387
   in the opcode table are slower on the i486.  They're the
2388
   versions with the implicitly specified single-position
2389
   displacement, which has another syntax if you really want to
2390
   use that form.  */
2391
10.6k
      t.bitfield.imm1 = 1;
2392
10.6k
      t.bitfield.imm8 = 1;
2393
10.6k
      t.bitfield.imm8s = 1;
2394
10.6k
      t.bitfield.imm16 = 1;
2395
10.6k
      t.bitfield.imm32 = 1;
2396
10.6k
      t.bitfield.imm32s = 1;
2397
10.6k
    }
2398
55.5k
  else if (fits_in_signed_byte (num))
2399
52.9k
    {
2400
52.9k
      if (fits_in_unsigned_byte (num))
2401
52.3k
  t.bitfield.imm8 = 1;
2402
52.9k
      t.bitfield.imm8s = 1;
2403
52.9k
      t.bitfield.imm16 = 1;
2404
52.9k
      t.bitfield.imm32 = 1;
2405
52.9k
      t.bitfield.imm32s = 1;
2406
52.9k
    }
2407
2.64k
  else if (fits_in_unsigned_byte (num))
2408
279
    {
2409
279
      t.bitfield.imm8 = 1;
2410
279
      t.bitfield.imm16 = 1;
2411
279
      t.bitfield.imm32 = 1;
2412
279
      t.bitfield.imm32s = 1;
2413
279
    }
2414
2.36k
  else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2415
705
    {
2416
705
      t.bitfield.imm16 = 1;
2417
705
      t.bitfield.imm32 = 1;
2418
705
      t.bitfield.imm32s = 1;
2419
705
    }
2420
1.66k
  else if (fits_in_signed_long (num))
2421
1.36k
    {
2422
1.36k
      t.bitfield.imm32 = 1;
2423
1.36k
      t.bitfield.imm32s = 1;
2424
1.36k
    }
2425
299
  else if (fits_in_unsigned_long (num))
2426
0
    t.bitfield.imm32 = 1;
2427
2428
66.1k
  return t;
2429
66.1k
}
2430
2431
static offsetT
2432
offset_in_range (offsetT val, int size)
2433
130k
{
2434
130k
  addressT mask;
2435
2436
130k
  switch (size)
2437
130k
    {
2438
61.2k
    case 1: mask = ((addressT) 1 <<  8) - 1; break;
2439
41.4k
    case 2: mask = ((addressT) 1 << 16) - 1; break;
2440
0
#ifdef BFD64
2441
27.7k
    case 4: mask = ((addressT) 1 << 32) - 1; break;
2442
0
#endif
2443
0
    case sizeof (val): return val;
2444
0
    default: abort ();
2445
130k
    }
2446
2447
130k
  if ((val & ~mask) != 0 && (-val & ~mask) != 0)
2448
975
    as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
2449
975
       (uint64_t) val, (uint64_t) (val & mask));
2450
2451
130k
  return val & mask;
2452
130k
}
2453
2454
static INLINE const char *insn_name (const insn_template *t)
2455
1.59M
{
2456
1.59M
  return &i386_mnemonics[t->mnem_off];
2457
1.59M
}
2458
2459
enum PREFIX_GROUP
2460
{
2461
  PREFIX_EXIST = 0,
2462
  PREFIX_LOCK,
2463
  PREFIX_REP,
2464
  PREFIX_DS,
2465
  PREFIX_OTHER
2466
};
2467
2468
/* Returns
2469
   a. PREFIX_EXIST if attempting to add a prefix where one from the
2470
   same class already exists.
2471
   b. PREFIX_LOCK if lock prefix is added.
2472
   c. PREFIX_REP if rep/repne prefix is added.
2473
   d. PREFIX_DS if ds prefix is added.
2474
   e. PREFIX_OTHER if other prefix is added.
2475
 */
2476
2477
static enum PREFIX_GROUP
2478
add_prefix (unsigned int prefix)
2479
4.00k
{
2480
4.00k
  enum PREFIX_GROUP ret = PREFIX_OTHER;
2481
4.00k
  unsigned int q;
2482
2483
4.00k
  if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2484
4.00k
      && flag_code == CODE_64BIT)
2485
154
    {
2486
154
      if ((i.prefix[REX_PREFIX] & prefix & REX_W)
2487
154
    || (i.prefix[REX_PREFIX] & prefix & REX_R)
2488
154
    || (i.prefix[REX_PREFIX] & prefix & REX_X)
2489
154
    || (i.prefix[REX_PREFIX] & prefix & REX_B))
2490
0
  ret = PREFIX_EXIST;
2491
154
      q = REX_PREFIX;
2492
154
    }
2493
3.84k
  else
2494
3.84k
    {
2495
3.84k
      switch (prefix)
2496
3.84k
  {
2497
0
  default:
2498
0
    abort ();
2499
2500
17
  case DS_PREFIX_OPCODE:
2501
17
    ret = PREFIX_DS;
2502
    /* Fall through.  */
2503
1.18k
  case CS_PREFIX_OPCODE:
2504
1.20k
  case ES_PREFIX_OPCODE:
2505
1.47k
  case FS_PREFIX_OPCODE:
2506
1.47k
  case GS_PREFIX_OPCODE:
2507
1.49k
  case SS_PREFIX_OPCODE:
2508
1.49k
    q = SEG_PREFIX;
2509
1.49k
    break;
2510
2511
280
  case REPNE_PREFIX_OPCODE:
2512
571
  case REPE_PREFIX_OPCODE:
2513
571
    q = REP_PREFIX;
2514
571
    ret = PREFIX_REP;
2515
571
    break;
2516
2517
0
  case LOCK_PREFIX_OPCODE:
2518
0
    q = LOCK_PREFIX;
2519
0
    ret = PREFIX_LOCK;
2520
0
    break;
2521
2522
1.77k
  case FWAIT_OPCODE:
2523
1.77k
    q = WAIT_PREFIX;
2524
1.77k
    break;
2525
2526
0
  case ADDR_PREFIX_OPCODE:
2527
0
    q = ADDR_PREFIX;
2528
0
    break;
2529
2530
2
  case DATA_PREFIX_OPCODE:
2531
2
    q = DATA_PREFIX;
2532
2
    break;
2533
3.84k
  }
2534
3.84k
      if (i.prefix[q] != 0)
2535
64
  ret = PREFIX_EXIST;
2536
3.84k
    }
2537
2538
4.00k
  if (ret)
2539
3.93k
    {
2540
3.93k
      if (!i.prefix[q])
2541
3.93k
  ++i.prefixes;
2542
3.93k
      i.prefix[q] |= prefix;
2543
3.93k
    }
2544
64
  else
2545
64
    as_bad (_("same type of prefix used twice"));
2546
2547
4.00k
  return ret;
2548
4.00k
}
2549
2550
static void
2551
update_code_flag (int value, int check)
2552
4.83k
{
2553
4.83k
  PRINTF_LIKE ((*as_error));
2554
2555
4.83k
  flag_code = (enum flag_code) value;
2556
4.83k
  if (flag_code == CODE_64BIT)
2557
636
    {
2558
636
      cpu_arch_flags.bitfield.cpu64 = 1;
2559
636
      cpu_arch_flags.bitfield.cpuno64 = 0;
2560
636
    }
2561
4.19k
  else
2562
4.19k
    {
2563
4.19k
      cpu_arch_flags.bitfield.cpu64 = 0;
2564
4.19k
      cpu_arch_flags.bitfield.cpuno64 = 1;
2565
4.19k
    }
2566
4.83k
  if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
2567
0
    {
2568
0
      if (check)
2569
0
  as_error = as_fatal;
2570
0
      else
2571
0
  as_error = as_bad;
2572
0
      (*as_error) (_("64bit mode not supported on `%s'."),
2573
0
       cpu_arch_name ? cpu_arch_name : default_arch);
2574
0
    }
2575
4.83k
  if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
2576
0
    {
2577
0
      if (check)
2578
0
  as_error = as_fatal;
2579
0
      else
2580
0
  as_error = as_bad;
2581
0
      (*as_error) (_("32bit mode not supported on `%s'."),
2582
0
       cpu_arch_name ? cpu_arch_name : default_arch);
2583
0
    }
2584
4.83k
  stackop_size = '\0';
2585
4.83k
}
2586
2587
static void
2588
set_code_flag (int value)
2589
4.19k
{
2590
4.19k
  update_code_flag (value, 0);
2591
4.19k
}
2592
2593
static void
2594
set_16bit_gcc_code_flag (int new_code_flag)
2595
0
{
2596
0
  flag_code = (enum flag_code) new_code_flag;
2597
0
  if (flag_code != CODE_16BIT)
2598
0
    abort ();
2599
0
  cpu_arch_flags.bitfield.cpu64 = 0;
2600
0
  cpu_arch_flags.bitfield.cpuno64 = 1;
2601
0
  stackop_size = LONG_MNEM_SUFFIX;
2602
0
}
2603
2604
static void
2605
set_intel_syntax (int syntax_flag)
2606
0
{
2607
  /* Find out if register prefixing is specified.  */
2608
0
  int ask_naked_reg = 0;
2609
2610
0
  SKIP_WHITESPACE ();
2611
0
  if (!is_end_of_line[(unsigned char) *input_line_pointer])
2612
0
    {
2613
0
      char *string;
2614
0
      int e = get_symbol_name (&string);
2615
2616
0
      if (strcmp (string, "prefix") == 0)
2617
0
  ask_naked_reg = 1;
2618
0
      else if (strcmp (string, "noprefix") == 0)
2619
0
  ask_naked_reg = -1;
2620
0
      else
2621
0
  as_bad (_("bad argument to syntax directive."));
2622
0
      (void) restore_line_pointer (e);
2623
0
    }
2624
0
  demand_empty_rest_of_line ();
2625
2626
0
  intel_syntax = syntax_flag;
2627
2628
0
  if (ask_naked_reg == 0)
2629
0
    allow_naked_reg = (intel_syntax
2630
0
           && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
2631
0
  else
2632
0
    allow_naked_reg = (ask_naked_reg < 0);
2633
2634
0
  expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
2635
2636
0
  register_prefix = allow_naked_reg ? "" : "%";
2637
0
}
2638
2639
static void
2640
set_intel_mnemonic (int mnemonic_flag)
2641
0
{
2642
0
  intel_mnemonic = mnemonic_flag;
2643
0
}
2644
2645
static void
2646
set_allow_index_reg (int flag)
2647
0
{
2648
0
  allow_index_reg = flag;
2649
0
}
2650
2651
static void
2652
set_check (int what)
2653
0
{
2654
0
  enum check_kind *kind;
2655
0
  const char *str;
2656
2657
0
  if (what)
2658
0
    {
2659
0
      kind = &operand_check;
2660
0
      str = "operand";
2661
0
    }
2662
0
  else
2663
0
    {
2664
0
      kind = &sse_check;
2665
0
      str = "sse";
2666
0
    }
2667
2668
0
  SKIP_WHITESPACE ();
2669
2670
0
  if (!is_end_of_line[(unsigned char) *input_line_pointer])
2671
0
    {
2672
0
      char *string;
2673
0
      int e = get_symbol_name (&string);
2674
2675
0
      if (strcmp (string, "none") == 0)
2676
0
  *kind = check_none;
2677
0
      else if (strcmp (string, "warning") == 0)
2678
0
  *kind = check_warning;
2679
0
      else if (strcmp (string, "error") == 0)
2680
0
  *kind = check_error;
2681
0
      else
2682
0
  as_bad (_("bad argument to %s_check directive."), str);
2683
0
      (void) restore_line_pointer (e);
2684
0
    }
2685
0
  else
2686
0
    as_bad (_("missing argument for %s_check directive"), str);
2687
2688
0
  demand_empty_rest_of_line ();
2689
0
}
2690
2691
static void
2692
check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
2693
         i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
2694
10
{
2695
10
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2696
10
  static const char *arch;
2697
2698
  /* Intel MCU is only supported on ELF.  */
2699
10
  if (!IS_ELF)
2700
0
    return;
2701
2702
10
  if (!arch)
2703
1
    {
2704
      /* Use cpu_arch_name if it is set in md_parse_option.  Otherwise
2705
   use default_arch.  */
2706
1
      arch = cpu_arch_name;
2707
1
      if (!arch)
2708
1
  arch = default_arch;
2709
1
    }
2710
2711
  /* If we are targeting Intel MCU, we must enable it.  */
2712
10
  if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2713
10
      == new_flag.bitfield.cpuiamcu)
2714
10
    return;
2715
2716
0
  as_bad (_("`%s' is not supported on `%s'"), name, arch);
2717
0
#endif
2718
0
}
2719
2720
static void
2721
extend_cpu_sub_arch_name (const char *name)
2722
2
{
2723
2
  if (cpu_sub_arch_name)
2724
0
    cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
2725
0
          ".", name, (const char *) NULL);
2726
2
  else
2727
2
    cpu_sub_arch_name = concat (".", name, (const char *) NULL);
2728
2
}
2729
2730
static void
2731
set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
2732
828
{
2733
828
  typedef struct arch_stack_entry
2734
828
  {
2735
828
    const struct arch_stack_entry *prev;
2736
828
    const char *name;
2737
828
    char *sub_name;
2738
828
    i386_cpu_flags flags;
2739
828
    i386_cpu_flags isa_flags;
2740
828
    enum processor_type isa;
2741
828
    enum flag_code flag_code;
2742
828
    char stackop_size;
2743
828
    bool no_cond_jump_promotion;
2744
828
  } arch_stack_entry;
2745
828
  static const arch_stack_entry *arch_stack_top;
2746
2747
828
  SKIP_WHITESPACE ();
2748
2749
828
  if (!is_end_of_line[(unsigned char) *input_line_pointer])
2750
716
    {
2751
716
      char *s;
2752
716
      int e = get_symbol_name (&s);
2753
716
      const char *string = s;
2754
716
      unsigned int j = 0;
2755
716
      i386_cpu_flags flags;
2756
2757
716
      if (strcmp (string, "default") == 0)
2758
0
  {
2759
0
    if (strcmp (default_arch, "iamcu") == 0)
2760
0
      string = default_arch;
2761
0
    else
2762
0
      {
2763
0
        static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
2764
2765
0
        cpu_arch_name = NULL;
2766
0
        free (cpu_sub_arch_name);
2767
0
        cpu_sub_arch_name = NULL;
2768
0
        cpu_arch_flags = cpu_unknown_flags;
2769
0
        if (flag_code == CODE_64BIT)
2770
0
    {
2771
0
      cpu_arch_flags.bitfield.cpu64 = 1;
2772
0
      cpu_arch_flags.bitfield.cpuno64 = 0;
2773
0
    }
2774
0
        else
2775
0
    {
2776
0
      cpu_arch_flags.bitfield.cpu64 = 0;
2777
0
      cpu_arch_flags.bitfield.cpuno64 = 1;
2778
0
    }
2779
0
        cpu_arch_isa = PROCESSOR_UNKNOWN;
2780
0
        cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
2781
0
        if (!cpu_arch_tune_set)
2782
0
    {
2783
0
      cpu_arch_tune = cpu_arch_isa;
2784
0
      cpu_arch_tune_flags = cpu_arch_isa_flags;
2785
0
    }
2786
2787
0
        j = ARRAY_SIZE (cpu_arch) + 1;
2788
0
      }
2789
0
  }
2790
716
      else if (strcmp (string, "push") == 0)
2791
0
  {
2792
0
    arch_stack_entry *top = XNEW (arch_stack_entry);
2793
2794
0
    top->name = cpu_arch_name;
2795
0
    if (cpu_sub_arch_name)
2796
0
      top->sub_name = xstrdup (cpu_sub_arch_name);
2797
0
    else
2798
0
      top->sub_name = NULL;
2799
0
    top->flags = cpu_arch_flags;
2800
0
    top->isa = cpu_arch_isa;
2801
0
    top->isa_flags = cpu_arch_isa_flags;
2802
0
    top->flag_code = flag_code;
2803
0
    top->stackop_size = stackop_size;
2804
0
    top->no_cond_jump_promotion = no_cond_jump_promotion;
2805
2806
0
    top->prev = arch_stack_top;
2807
0
    arch_stack_top = top;
2808
2809
0
    (void) restore_line_pointer (e);
2810
0
    demand_empty_rest_of_line ();
2811
0
    return;
2812
0
  }
2813
716
      else if (strcmp (string, "pop") == 0)
2814
0
  {
2815
0
    const arch_stack_entry *top = arch_stack_top;
2816
2817
0
    if (!top)
2818
0
      as_bad (_(".arch stack is empty"));
2819
0
    else if (top->flag_code != flag_code
2820
0
       || top->stackop_size != stackop_size)
2821
0
      {
2822
0
        static const unsigned int bits[] = {
2823
0
          [CODE_16BIT] = 16,
2824
0
          [CODE_32BIT] = 32,
2825
0
          [CODE_64BIT] = 64,
2826
0
        };
2827
2828
0
        as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
2829
0
          bits[top->flag_code],
2830
0
          top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
2831
0
      }
2832
0
    else
2833
0
      {
2834
0
        arch_stack_top = top->prev;
2835
2836
0
        cpu_arch_name = top->name;
2837
0
        free (cpu_sub_arch_name);
2838
0
        cpu_sub_arch_name = top->sub_name;
2839
0
        cpu_arch_flags = top->flags;
2840
0
        cpu_arch_isa = top->isa;
2841
0
        cpu_arch_isa_flags = top->isa_flags;
2842
0
        no_cond_jump_promotion = top->no_cond_jump_promotion;
2843
2844
0
        XDELETE (top);
2845
0
      }
2846
2847
0
    (void) restore_line_pointer (e);
2848
0
    demand_empty_rest_of_line ();
2849
0
    return;
2850
0
  }
2851
2852
115k
      for (; j < ARRAY_SIZE (cpu_arch); j++)
2853
114k
  {
2854
114k
    if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
2855
114k
       && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
2856
181
      {
2857
181
        if (*string != '.')
2858
10
    {
2859
10
      check_cpu_arch_compatible (string, cpu_arch[j].enable);
2860
2861
10
      cpu_arch_name = cpu_arch[j].name;
2862
10
      free (cpu_sub_arch_name);
2863
10
      cpu_sub_arch_name = NULL;
2864
10
      cpu_arch_flags = cpu_arch[j].enable;
2865
10
      if (flag_code == CODE_64BIT)
2866
10
        {
2867
10
          cpu_arch_flags.bitfield.cpu64 = 1;
2868
10
          cpu_arch_flags.bitfield.cpuno64 = 0;
2869
10
        }
2870
0
      else
2871
0
        {
2872
0
          cpu_arch_flags.bitfield.cpu64 = 0;
2873
0
          cpu_arch_flags.bitfield.cpuno64 = 1;
2874
0
        }
2875
10
      cpu_arch_isa = cpu_arch[j].type;
2876
10
      cpu_arch_isa_flags = cpu_arch[j].enable;
2877
10
      if (!cpu_arch_tune_set)
2878
10
        {
2879
10
          cpu_arch_tune = cpu_arch_isa;
2880
10
          cpu_arch_tune_flags = cpu_arch_isa_flags;
2881
10
        }
2882
10
      pre_386_16bit_warned = false;
2883
10
      break;
2884
10
    }
2885
2886
171
        if (cpu_flags_all_zero (&cpu_arch[j].enable))
2887
0
          continue;
2888
2889
171
        flags = cpu_flags_or (cpu_arch_flags,
2890
171
            cpu_arch[j].enable);
2891
2892
171
        if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2893
2
    {
2894
2
      extend_cpu_sub_arch_name (string + 1);
2895
2
      cpu_arch_flags = flags;
2896
2
      cpu_arch_isa_flags = flags;
2897
2
    }
2898
169
        else
2899
169
    cpu_arch_isa_flags
2900
169
      = cpu_flags_or (cpu_arch_isa_flags,
2901
169
          cpu_arch[j].enable);
2902
171
        (void) restore_line_pointer (e);
2903
171
        demand_empty_rest_of_line ();
2904
171
        return;
2905
171
      }
2906
114k
  }
2907
2908
545
      if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
2909
348
  {
2910
    /* Disable an ISA extension.  */
2911
59.5k
    for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
2912
59.1k
      if (cpu_arch[j].type == PROCESSOR_NONE
2913
59.1k
          && strcmp (string + 3, cpu_arch[j].name) == 0)
2914
0
        {
2915
0
    flags = cpu_flags_and_not (cpu_arch_flags,
2916
0
             cpu_arch[j].disable);
2917
0
    if (!cpu_flags_equal (&flags, &cpu_arch_flags))
2918
0
      {
2919
0
        extend_cpu_sub_arch_name (string + 1);
2920
0
        cpu_arch_flags = flags;
2921
0
        cpu_arch_isa_flags = flags;
2922
0
      }
2923
0
    (void) restore_line_pointer (e);
2924
0
    demand_empty_rest_of_line ();
2925
0
    return;
2926
0
        }
2927
348
  }
2928
2929
545
      if (j == ARRAY_SIZE (cpu_arch))
2930
535
  as_bad (_("no such architecture: `%s'"), string);
2931
2932
545
      *input_line_pointer = e;
2933
545
    }
2934
112
  else
2935
112
    as_bad (_("missing cpu architecture"));
2936
2937
657
  no_cond_jump_promotion = 0;
2938
657
  if (*input_line_pointer == ','
2939
657
      && !is_end_of_line[(unsigned char) input_line_pointer[1]])
2940
0
    {
2941
0
      char *string;
2942
0
      char e;
2943
2944
0
      ++input_line_pointer;
2945
0
      e = get_symbol_name (&string);
2946
2947
0
      if (strcmp (string, "nojumps") == 0)
2948
0
  no_cond_jump_promotion = 1;
2949
0
      else if (strcmp (string, "jumps") == 0)
2950
0
  ;
2951
0
      else
2952
0
  as_bad (_("no such architecture modifier: `%s'"), string);
2953
2954
0
      (void) restore_line_pointer (e);
2955
0
    }
2956
2957
657
  demand_empty_rest_of_line ();
2958
657
}
2959
2960
enum bfd_architecture
2961
i386_arch (void)
2962
633
{
2963
633
  if (cpu_arch_isa == PROCESSOR_IAMCU)
2964
0
    {
2965
0
      if (OUTPUT_FLAVOR != bfd_target_elf_flavour
2966
0
    || flag_code == CODE_64BIT)
2967
0
  as_fatal (_("Intel MCU is 32bit ELF only"));
2968
0
      return bfd_arch_iamcu;
2969
0
    }
2970
633
  else
2971
633
    return bfd_arch_i386;
2972
633
}
2973
2974
unsigned long
2975
i386_mach (void)
2976
633
{
2977
633
  if (startswith (default_arch, "x86_64"))
2978
633
    {
2979
633
      if (default_arch[6] == '\0')
2980
633
  return bfd_mach_x86_64;
2981
0
      else
2982
0
  return bfd_mach_x64_32;
2983
633
    }
2984
0
  else if (!strcmp (default_arch, "i386")
2985
0
     || !strcmp (default_arch, "iamcu"))
2986
0
    {
2987
0
      if (cpu_arch_isa == PROCESSOR_IAMCU)
2988
0
  {
2989
0
    if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
2990
0
      as_fatal (_("Intel MCU is 32bit ELF only"));
2991
0
    return bfd_mach_i386_iamcu;
2992
0
  }
2993
0
      else
2994
0
  return bfd_mach_i386_i386;
2995
0
    }
2996
0
  else
2997
0
    as_fatal (_("unknown architecture"));
2998
633
}
2999

3000
#include "opcodes/i386-tbl.h"
3001
3002
void
3003
md_begin (void)
3004
633
{
3005
  /* Support pseudo prefixes like {disp32}.  */
3006
633
  lex_type ['{'] = LEX_BEGIN_NAME;
3007
3008
  /* Initialize op_hash hash table.  */
3009
633
  op_hash = str_htab_create ();
3010
3011
633
  {
3012
633
    const insn_template *const *sets = i386_op_sets;
3013
633
    const insn_template *const *end = sets + ARRAY_SIZE (i386_op_sets) - 1;
3014
3015
    /* Type checks to compensate for the conversion through void * which
3016
       occurs during hash table insertion / lookup.  */
3017
633
    (void) sizeof (sets == &current_templates->start);
3018
633
    (void) sizeof (end == &current_templates->end);
3019
1.47M
    for (; sets < end; ++sets)
3020
1.47M
      if (str_hash_insert (op_hash, insn_name (*sets), sets, 0))
3021
0
  as_fatal (_("duplicate %s"), insn_name (*sets));
3022
633
  }
3023
3024
  /* Initialize reg_hash hash table.  */
3025
633
  reg_hash = str_htab_create ();
3026
633
  {
3027
633
    const reg_entry *regtab;
3028
633
    unsigned int regtab_size = i386_regtab_size;
3029
3030
182k
    for (regtab = i386_regtab; regtab_size--; regtab++)
3031
182k
      {
3032
182k
  switch (regtab->reg_type.bitfield.class)
3033
182k
    {
3034
50.6k
    case Reg:
3035
50.6k
      if (regtab->reg_type.bitfield.dword)
3036
10.1k
        {
3037
10.1k
    if (regtab->reg_type.bitfield.instance == Accum)
3038
633
      reg_eax = regtab;
3039
10.1k
        }
3040
40.5k
      else if (regtab->reg_type.bitfield.tbyte)
3041
5.06k
        {
3042
    /* There's no point inserting st(<N>) in the hash table, as
3043
       parentheses aren't included in register_chars[] anyway.  */
3044
5.06k
    if (regtab->reg_type.bitfield.instance != Accum)
3045
4.43k
      continue;
3046
633
    reg_st0 = regtab;
3047
633
        }
3048
46.2k
      break;
3049
3050
46.2k
    case SReg:
3051
4.43k
      switch (regtab->reg_num)
3052
4.43k
        {
3053
633
        case 0: reg_es = regtab; break;
3054
633
        case 2: reg_ss = regtab; break;
3055
633
        case 3: reg_ds = regtab; break;
3056
4.43k
        }
3057
4.43k
      break;
3058
3059
5.06k
    case RegMask:
3060
5.06k
      if (!regtab->reg_num)
3061
633
        reg_k0 = regtab;
3062
5.06k
      break;
3063
182k
    }
3064
3065
177k
  if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3066
0
    as_fatal (_("duplicate %s"), regtab->reg_name);
3067
177k
      }
3068
633
  }
3069
3070
  /* Fill in lexical tables:  mnemonic_chars, operand_chars.  */
3071
633
  {
3072
633
    int c;
3073
633
    const char *p;
3074
3075
162k
    for (c = 0; c < 256; c++)
3076
162k
      {
3077
162k
  if (ISDIGIT (c) || ISLOWER (c))
3078
22.7k
    {
3079
22.7k
      mnemonic_chars[c] = c;
3080
22.7k
      register_chars[c] = c;
3081
22.7k
      operand_chars[c] = c;
3082
22.7k
    }
3083
139k
  else if (ISUPPER (c))
3084
16.4k
    {
3085
16.4k
      mnemonic_chars[c] = TOLOWER (c);
3086
16.4k
      register_chars[c] = mnemonic_chars[c];
3087
16.4k
      operand_chars[c] = c;
3088
16.4k
    }
3089
#ifdef SVR4_COMMENT_CHARS
3090
  else if (c == '\\' && strchr (i386_comment_chars, '/'))
3091
    operand_chars[c] = c;
3092
#endif
3093
3094
162k
  if (c >= 128)
3095
81.0k
    operand_chars[c] = c;
3096
162k
      }
3097
3098
633
    mnemonic_chars['_'] = '_';
3099
633
    mnemonic_chars['-'] = '-';
3100
633
    mnemonic_chars['.'] = '.';
3101
3102
5.06k
    for (p = extra_symbol_chars; *p != '\0'; p++)
3103
4.43k
      operand_chars[(unsigned char) *p] = *p;
3104
12.0k
    for (p = operand_special_chars; *p != '\0'; p++)
3105
11.3k
      operand_chars[(unsigned char) *p] = *p;
3106
633
  }
3107
3108
633
  if (flag_code == CODE_64BIT)
3109
633
    {
3110
#if defined (OBJ_COFF) && defined (TE_PE)
3111
      x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3112
          ? 32 : 16);
3113
#else
3114
633
      x86_dwarf2_return_column = 16;
3115
633
#endif
3116
633
      x86_cie_data_alignment = -8;
3117
633
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3118
633
      x86_sframe_cfa_sp_reg = 7;
3119
633
      x86_sframe_cfa_fp_reg = 6;
3120
633
#endif
3121
633
    }
3122
0
  else
3123
0
    {
3124
0
      x86_dwarf2_return_column = 8;
3125
0
      x86_cie_data_alignment = -4;
3126
0
    }
3127
3128
  /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3129
     can be turned into BRANCH_PREFIX frag.  */
3130
633
  if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3131
0
    abort ();
3132
633
}
3133
3134
void
3135
i386_print_statistics (FILE *file)
3136
0
{
3137
0
  htab_print_statistics (file, "i386 opcode", op_hash);
3138
0
  htab_print_statistics (file, "i386 register", reg_hash);
3139
0
}
3140
3141
void
3142
i386_md_end (void)
3143
633
{
3144
633
  htab_delete (op_hash);
3145
633
  htab_delete (reg_hash);
3146
633
}
3147

3148
#ifdef DEBUG386
3149
3150
/* Debugging routines for md_assemble.  */
3151
static void pte (insn_template *);
3152
static void pt (i386_operand_type);
3153
static void pe (expressionS *);
3154
static void ps (symbolS *);
3155
3156
static void
3157
pi (const char *line, i386_insn *x)
3158
{
3159
  unsigned int j;
3160
3161
  fprintf (stdout, "%s: template ", line);
3162
  pte (&x->tm);
3163
  fprintf (stdout, "  address: base %s  index %s  scale %x\n",
3164
     x->base_reg ? x->base_reg->reg_name : "none",
3165
     x->index_reg ? x->index_reg->reg_name : "none",
3166
     x->log2_scale_factor);
3167
  fprintf (stdout, "  modrm:  mode %x  reg %x  reg/mem %x\n",
3168
     x->rm.mode, x->rm.reg, x->rm.regmem);
3169
  fprintf (stdout, "  sib:  base %x  index %x  scale %x\n",
3170
     x->sib.base, x->sib.index, x->sib.scale);
3171
  fprintf (stdout, "  rex: 64bit %x  extX %x  extY %x  extZ %x\n",
3172
     (x->rex & REX_W) != 0,
3173
     (x->rex & REX_R) != 0,
3174
     (x->rex & REX_X) != 0,
3175
     (x->rex & REX_B) != 0);
3176
  for (j = 0; j < x->operands; j++)
3177
    {
3178
      fprintf (stdout, "    #%d:  ", j + 1);
3179
      pt (x->types[j]);
3180
      fprintf (stdout, "\n");
3181
      if (x->types[j].bitfield.class == Reg
3182
    || x->types[j].bitfield.class == RegMMX
3183
    || x->types[j].bitfield.class == RegSIMD
3184
    || x->types[j].bitfield.class == RegMask
3185
    || x->types[j].bitfield.class == SReg
3186
    || x->types[j].bitfield.class == RegCR
3187
    || x->types[j].bitfield.class == RegDR
3188
    || x->types[j].bitfield.class == RegTR
3189
    || x->types[j].bitfield.class == RegBND)
3190
  fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3191
      if (operand_type_check (x->types[j], imm))
3192
  pe (x->op[j].imms);
3193
      if (operand_type_check (x->types[j], disp))
3194
  pe (x->op[j].disps);
3195
    }
3196
}
3197
3198
static void
3199
pte (insn_template *t)
3200
{
3201
  static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
3202
  static const char *const opc_spc[] = {
3203
    NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
3204
    "XOP08", "XOP09", "XOP0A",
3205
  };
3206
  unsigned int j;
3207
3208
  fprintf (stdout, " %d operands ", t->operands);
3209
  if (opc_pfx[t->opcode_modifier.opcodeprefix])
3210
    fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
3211
  if (opc_spc[t->opcode_space])
3212
    fprintf (stdout, "space %s ", opc_spc[t->opcode_space]);
3213
  fprintf (stdout, "opcode %x ", t->base_opcode);
3214
  if (t->extension_opcode != None)
3215
    fprintf (stdout, "ext %x ", t->extension_opcode);
3216
  if (t->opcode_modifier.d)
3217
    fprintf (stdout, "D");
3218
  if (t->opcode_modifier.w)
3219
    fprintf (stdout, "W");
3220
  fprintf (stdout, "\n");
3221
  for (j = 0; j < t->operands; j++)
3222
    {
3223
      fprintf (stdout, "    #%d type ", j + 1);
3224
      pt (t->operand_types[j]);
3225
      fprintf (stdout, "\n");
3226
    }
3227
}
3228
3229
static void
3230
pe (expressionS *e)
3231
{
3232
  fprintf (stdout, "    operation     %d\n", e->X_op);
3233
  fprintf (stdout, "    add_number    %" PRId64 " (%" PRIx64 ")\n",
3234
     (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
3235
  if (e->X_add_symbol)
3236
    {
3237
      fprintf (stdout, "    add_symbol    ");
3238
      ps (e->X_add_symbol);
3239
      fprintf (stdout, "\n");
3240
    }
3241
  if (e->X_op_symbol)
3242
    {
3243
      fprintf (stdout, "    op_symbol    ");
3244
      ps (e->X_op_symbol);
3245
      fprintf (stdout, "\n");
3246
    }
3247
}
3248
3249
static void
3250
ps (symbolS *s)
3251
{
3252
  fprintf (stdout, "%s type %s%s",
3253
     S_GET_NAME (s),
3254
     S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3255
     segment_name (S_GET_SEGMENT (s)));
3256
}
3257
3258
static struct type_name
3259
  {
3260
    i386_operand_type mask;
3261
    const char *name;
3262
  }
3263
const type_names[] =
3264
{
3265
  { { .bitfield = { .class = Reg, .byte = 1 } }, "r8" },
3266
  { { .bitfield = { .class = Reg, .word = 1 } }, "r16" },
3267
  { { .bitfield = { .class = Reg, .dword = 1 } }, "r32" },
3268
  { { .bitfield = { .class = Reg, .qword = 1 } }, "r64" },
3269
  { { .bitfield = { .instance = Accum, .byte = 1 } }, "acc8" },
3270
  { { .bitfield = { .instance = Accum, .word = 1 } }, "acc16" },
3271
  { { .bitfield = { .instance = Accum, .dword = 1 } }, "acc32" },
3272
  { { .bitfield = { .instance = Accum, .qword = 1 } }, "acc64" },
3273
  { { .bitfield = { .imm8 = 1 } }, "i8" },
3274
  { { .bitfield = { .imm8s = 1 } }, "i8s" },
3275
  { { .bitfield = { .imm16 = 1 } }, "i16" },
3276
  { { .bitfield = { .imm32 = 1 } }, "i32" },
3277
  { { .bitfield = { .imm32s = 1 } }, "i32s" },
3278
  { { .bitfield = { .imm64 = 1 } }, "i64" },
3279
  { { .bitfield = { .imm1 = 1 } }, "i1" },
3280
  { { .bitfield = { .baseindex = 1 } }, "BaseIndex" },
3281
  { { .bitfield = { .disp8 = 1 } }, "d8" },
3282
  { { .bitfield = { .disp16 = 1 } }, "d16" },
3283
  { { .bitfield = { .disp32 = 1 } }, "d32" },
3284
  { { .bitfield = { .disp64 = 1 } }, "d64" },
3285
  { { .bitfield = { .instance = RegD, .word = 1 } }, "InOutPortReg" },
3286
  { { .bitfield = { .instance = RegC, .byte = 1 } }, "ShiftCount" },
3287
  { { .bitfield = { .class = RegCR } }, "control reg" },
3288
  { { .bitfield = { .class = RegTR } }, "test reg" },
3289
  { { .bitfield = { .class = RegDR } }, "debug reg" },
3290
  { { .bitfield = { .class = Reg, .tbyte = 1 } }, "FReg" },
3291
  { { .bitfield = { .instance = Accum, .tbyte = 1 } }, "FAcc" },
3292
  { { .bitfield = { .class = SReg } }, "SReg" },
3293
  { { .bitfield = { .class = RegMMX } }, "rMMX" },
3294
  { { .bitfield = { .class = RegSIMD, .xmmword = 1 } }, "rXMM" },
3295
  { { .bitfield = { .class = RegSIMD, .ymmword = 1 } }, "rYMM" },
3296
  { { .bitfield = { .class = RegSIMD, .zmmword = 1 } }, "rZMM" },
3297
  { { .bitfield = { .class = RegSIMD, .tmmword = 1 } }, "rTMM" },
3298
  { { .bitfield = { .class = RegMask } }, "Mask reg" },
3299
};
3300
3301
static void
3302
pt (i386_operand_type t)
3303
{
3304
  unsigned int j;
3305
  i386_operand_type a;
3306
3307
  for (j = 0; j < ARRAY_SIZE (type_names); j++)
3308
    {
3309
      a = operand_type_and (t, type_names[j].mask);
3310
      if (operand_type_equal (&a, &type_names[j].mask))
3311
  fprintf (stdout, "%s, ",  type_names[j].name);
3312
    }
3313
  fflush (stdout);
3314
}
3315
3316
#endif /* DEBUG386 */
3317

3318
static bfd_reloc_code_real_type
3319
reloc (unsigned int size,
3320
       int pcrel,
3321
       int sign,
3322
       bfd_reloc_code_real_type other)
3323
82.7k
{
3324
82.7k
  if (other != NO_RELOC)
3325
160
    {
3326
160
      reloc_howto_type *rel;
3327
3328
160
      if (size == 8)
3329
0
  switch (other)
3330
0
    {
3331
0
    case BFD_RELOC_X86_64_GOT32:
3332
0
      return BFD_RELOC_X86_64_GOT64;
3333
0
      break;
3334
0
    case BFD_RELOC_X86_64_GOTPLT64:
3335
0
      return BFD_RELOC_X86_64_GOTPLT64;
3336
0
      break;
3337
0
    case BFD_RELOC_X86_64_PLTOFF64:
3338
0
      return BFD_RELOC_X86_64_PLTOFF64;
3339
0
      break;
3340
0
    case BFD_RELOC_X86_64_GOTPC32:
3341
0
      other = BFD_RELOC_X86_64_GOTPC64;
3342
0
      break;
3343
0
    case BFD_RELOC_X86_64_GOTPCREL:
3344
0
      other = BFD_RELOC_X86_64_GOTPCREL64;
3345
0
      break;
3346
0
    case BFD_RELOC_X86_64_TPOFF32:
3347
0
      other = BFD_RELOC_X86_64_TPOFF64;
3348
0
      break;
3349
0
    case BFD_RELOC_X86_64_DTPOFF32:
3350
0
      other = BFD_RELOC_X86_64_DTPOFF64;
3351
0
      break;
3352
0
    default:
3353
0
      break;
3354
0
    }
3355
3356
160
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3357
160
      if (other == BFD_RELOC_SIZE32)
3358
11
  {
3359
11
    if (size == 8)
3360
0
      other = BFD_RELOC_SIZE64;
3361
11
    if (pcrel)
3362
0
      {
3363
0
        as_bad (_("there are no pc-relative size relocations"));
3364
0
        return NO_RELOC;
3365
0
      }
3366
11
  }
3367
160
#endif
3368
3369
      /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless.  */
3370
160
      if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
3371
73
  sign = -1;
3372
3373
160
      rel = bfd_reloc_type_lookup (stdoutput, other);
3374
160
      if (!rel)
3375
0
  as_bad (_("unknown relocation (%u)"), other);
3376
160
      else if (size != bfd_get_reloc_size (rel))
3377
142
  as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
3378
142
    bfd_get_reloc_size (rel),
3379
142
    size);
3380
18
      else if (pcrel && !rel->pc_relative)
3381
0
  as_bad (_("non-pc-relative relocation for pc-relative field"));
3382
18
      else if ((rel->complain_on_overflow == complain_overflow_signed
3383
18
    && !sign)
3384
18
         || (rel->complain_on_overflow == complain_overflow_unsigned
3385
18
       && sign > 0))
3386
0
  as_bad (_("relocated field and relocation type differ in signedness"));
3387
18
      else
3388
18
  return other;
3389
142
      return NO_RELOC;
3390
160
    }
3391
3392
82.6k
  if (pcrel)
3393
56
    {
3394
56
      if (!sign)
3395
0
  as_bad (_("there are no unsigned pc-relative relocations"));
3396
56
      switch (size)
3397
56
  {
3398
35
  case 1: return BFD_RELOC_8_PCREL;
3399
0
  case 2: return BFD_RELOC_16_PCREL;
3400
21
  case 4: return BFD_RELOC_32_PCREL;
3401
0
  case 8: return BFD_RELOC_64_PCREL;
3402
56
  }
3403
0
      as_bad (_("cannot do %u byte pc-relative relocation"), size);
3404
0
    }
3405
82.5k
  else
3406
82.5k
    {
3407
82.5k
      if (sign > 0)
3408
1.33k
  switch (size)
3409
1.33k
    {
3410
1.33k
    case 4: return BFD_RELOC_X86_64_32S;
3411
1.33k
    }
3412
81.2k
      else
3413
81.2k
  switch (size)
3414
81.2k
    {
3415
7.64k
    case 1: return BFD_RELOC_8;
3416
9.25k
    case 2: return BFD_RELOC_16;
3417
57.7k
    case 4: return BFD_RELOC_32;
3418
788
    case 8: return BFD_RELOC_64;
3419
81.2k
    }
3420
5.75k
      as_bad (_("cannot do %s %u byte relocation"),
3421
5.75k
        sign > 0 ? "signed" : "unsigned", size);
3422
5.75k
    }
3423
3424
5.75k
  return NO_RELOC;
3425
82.6k
}
3426
3427
/* Here we decide which fixups can be adjusted to make them relative to
3428
   the beginning of the section instead of the symbol.  Basically we need
3429
   to make sure that the dynamic relocations are done correctly, so in
3430
   some cases we force the original symbol to be used.  */
3431
3432
int
3433
tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
3434
0
{
3435
0
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3436
0
  if (!IS_ELF)
3437
0
    return 1;
3438
3439
  /* Don't adjust pc-relative references to merge sections in 64-bit
3440
     mode.  */
3441
0
  if (use_rela_relocations
3442
0
      && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3443
0
      && fixP->fx_pcrel)
3444
0
    return 0;
3445
3446
  /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3447
     and changed later by validate_fix.  */
3448
0
  if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3449
0
      && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3450
0
    return 0;
3451
3452
  /* Adjust_reloc_syms doesn't know about the GOT.  Need to keep symbol
3453
     for size relocations.  */
3454
0
  if (fixP->fx_r_type == BFD_RELOC_SIZE32
3455
0
      || fixP->fx_r_type == BFD_RELOC_SIZE64
3456
0
      || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
3457
0
      || fixP->fx_r_type == BFD_RELOC_386_GOT32
3458
0
      || fixP->fx_r_type == BFD_RELOC_386_GOT32X
3459
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3460
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3461
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3462
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
3463
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3464
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
3465
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3466
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
3467
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3468
0
      || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3469
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
3470
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
3471
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3472
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
3473
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3474
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3475
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
3476
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
3477
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3478
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
3479
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3480
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
3481
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3482
0
      || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
3483
0
      || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3484
0
      || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3485
0
    return 0;
3486
0
#endif
3487
0
  return 1;
3488
0
}
3489
3490
static INLINE bool
3491
want_disp32 (const insn_template *t)
3492
137k
{
3493
137k
  return flag_code != CODE_64BIT
3494
137k
   || i.prefix[ADDR_PREFIX]
3495
137k
   || (t->mnem_off == MN_lea
3496
73.5k
       && (!i.types[1].bitfield.qword
3497
161
    || t->opcode_modifier.size == SIZE32));
3498
137k
}
3499
3500
static int
3501
intel_float_operand (const char *mnemonic)
3502
0
{
3503
  /* Note that the value returned is meaningful only for opcodes with (memory)
3504
     operands, hence the code here is free to improperly handle opcodes that
3505
     have no operands (for better performance and smaller code). */
3506
3507
0
  if (mnemonic[0] != 'f')
3508
0
    return 0; /* non-math */
3509
3510
0
  switch (mnemonic[1])
3511
0
    {
3512
    /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3513
       the fs segment override prefix not currently handled because no
3514
       call path can make opcodes without operands get here */
3515
0
    case 'i':
3516
0
      return 2 /* integer op */;
3517
0
    case 'l':
3518
0
      if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3519
0
  return 3; /* fldcw/fldenv */
3520
0
      break;
3521
0
    case 'n':
3522
0
      if (mnemonic[2] != 'o' /* fnop */)
3523
0
  return 3; /* non-waiting control op */
3524
0
      break;
3525
0
    case 'r':
3526
0
      if (mnemonic[2] == 's')
3527
0
  return 3; /* frstor/frstpm */
3528
0
      break;
3529
0
    case 's':
3530
0
      if (mnemonic[2] == 'a')
3531
0
  return 3; /* fsave */
3532
0
      if (mnemonic[2] == 't')
3533
0
  {
3534
0
    switch (mnemonic[3])
3535
0
      {
3536
0
      case 'c': /* fstcw */
3537
0
      case 'd': /* fstdw */
3538
0
      case 'e': /* fstenv */
3539
0
      case 's': /* fsts[gw] */
3540
0
        return 3;
3541
0
      }
3542
0
  }
3543
0
      break;
3544
0
    case 'x':
3545
0
      if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3546
0
  return 0; /* fxsave/fxrstor are not really math ops */
3547
0
      break;
3548
0
    }
3549
3550
0
  return 1;
3551
0
}
3552
3553
static INLINE void
3554
install_template (const insn_template *t)
3555
187k
{
3556
187k
  unsigned int l;
3557
3558
187k
  i.tm = *t;
3559
3560
  /* Note that for pseudo prefixes this produces a length of 1. But for them
3561
     the length isn't interesting at all.  */
3562
187k
  for (l = 1; l < 4; ++l)
3563
187k
    if (!(t->base_opcode >> (8 * l)))
3564
187k
      break;
3565
3566
187k
  i.opcode_length = l;
3567
187k
}
3568
3569
/* Build the VEX prefix.  */
3570
3571
static void
3572
build_vex_prefix (const insn_template *t)
3573
2
{
3574
2
  unsigned int register_specifier;
3575
2
  unsigned int vector_length;
3576
2
  unsigned int w;
3577
3578
  /* Check register specifier.  */
3579
2
  if (i.vex.register_specifier)
3580
0
    {
3581
0
      register_specifier =
3582
0
  ~register_number (i.vex.register_specifier) & 0xf;
3583
0
      gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3584
0
    }
3585
2
  else
3586
2
    register_specifier = 0xf;
3587
3588
  /* Use 2-byte VEX prefix by swapping destination and source operand
3589
     if there are more than 1 register operand.  */
3590
2
  if (i.reg_operands > 1
3591
2
      && i.vec_encoding != vex_encoding_vex3
3592
2
      && i.dir_encoding == dir_encoding_default
3593
2
      && i.operands == i.reg_operands
3594
2
      && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
3595
2
      && i.tm.opcode_space == SPACE_0F
3596
2
      && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
3597
2
      && i.rex == REX_B)
3598
0
    {
3599
0
      unsigned int xchg;
3600
3601
0
      swap_2_operands (0, i.operands - 1);
3602
3603
0
      gas_assert (i.rm.mode == 3);
3604
3605
0
      i.rex = REX_R;
3606
0
      xchg = i.rm.regmem;
3607
0
      i.rm.regmem = i.rm.reg;
3608
0
      i.rm.reg = xchg;
3609
3610
0
      if (i.tm.opcode_modifier.d)
3611
0
  i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
3612
0
          ? Opcode_ExtD : Opcode_SIMD_IntD;
3613
0
      else /* Use the next insn.  */
3614
0
  install_template (&t[1]);
3615
0
    }
3616
3617
  /* Use 2-byte VEX prefix by swapping commutative source operands if there
3618
     are no memory operands and at least 3 register ones.  */
3619
2
  if (i.reg_operands >= 3
3620
2
      && i.vec_encoding != vex_encoding_vex3
3621
2
      && i.reg_operands == i.operands - i.imm_operands
3622
2
      && i.tm.opcode_modifier.vex
3623
2
      && i.tm.opcode_modifier.commutative
3624
2
      && (i.tm.opcode_modifier.sse2avx
3625
0
    || (optimize > 1 && !i.no_optimize))
3626
2
      && i.rex == REX_B
3627
2
      && i.vex.register_specifier
3628
2
      && !(i.vex.register_specifier->reg_flags & RegRex))
3629
0
    {
3630
0
      unsigned int xchg = i.operands - i.reg_operands;
3631
3632
0
      gas_assert (i.tm.opcode_space == SPACE_0F);
3633
0
      gas_assert (!i.tm.opcode_modifier.sae);
3634
0
      gas_assert (operand_type_equal (&i.types[i.operands - 2],
3635
0
                                      &i.types[i.operands - 3]));
3636
0
      gas_assert (i.rm.mode == 3);
3637
3638
0
      swap_2_operands (xchg, xchg + 1);
3639
3640
0
      i.rex = 0;
3641
0
      xchg = i.rm.regmem | 8;
3642
0
      i.rm.regmem = ~register_specifier & 0xf;
3643
0
      gas_assert (!(i.rm.regmem & 8));
3644
0
      i.vex.register_specifier += xchg - i.rm.regmem;
3645
0
      register_specifier = ~xchg & 0xf;
3646
0
    }
3647
3648
2
  if (i.tm.opcode_modifier.vex == VEXScalar)
3649
0
    vector_length = avxscalar;
3650
2
  else if (i.tm.opcode_modifier.vex == VEX256)
3651
0
    vector_length = 1;
3652
2
  else if (dot_insn () && i.tm.opcode_modifier.vex == VEX128)
3653
0
    vector_length = 0;
3654
2
  else
3655
2
    {
3656
2
      unsigned int op;
3657
3658
      /* Determine vector length from the last multi-length vector
3659
   operand.  */
3660
2
      vector_length = 0;
3661
2
      for (op = t->operands; op--;)
3662
0
  if (t->operand_types[op].bitfield.xmmword
3663
0
      && t->operand_types[op].bitfield.ymmword
3664
0
      && i.types[op].bitfield.ymmword)
3665
0
    {
3666
0
      vector_length = 1;
3667
0
      break;
3668
0
    }
3669
2
    }
3670
3671
  /* Check the REX.W bit and VEXW.  */
3672
2
  if (i.tm.opcode_modifier.vexw == VEXWIG)
3673
0
    w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3674
2
  else if (i.tm.opcode_modifier.vexw)
3675
2
    w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3676
0
  else
3677
0
    w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
3678
3679
  /* Use 2-byte VEX prefix if possible.  */
3680
2
  if (w == 0
3681
2
      && i.vec_encoding != vex_encoding_vex3
3682
2
      && i.tm.opcode_space == SPACE_0F
3683
2
      && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3684
0
    {
3685
      /* 2-byte VEX prefix.  */
3686
0
      unsigned int r;
3687
3688
0
      i.vex.length = 2;
3689
0
      i.vex.bytes[0] = 0xc5;
3690
3691
      /* Check the REX.R bit.  */
3692
0
      r = (i.rex & REX_R) ? 0 : 1;
3693
0
      i.vex.bytes[1] = (r << 7
3694
0
      | register_specifier << 3
3695
0
      | vector_length << 2
3696
0
      | i.tm.opcode_modifier.opcodeprefix);
3697
0
    }
3698
2
  else
3699
2
    {
3700
      /* 3-byte VEX prefix.  */
3701
2
      i.vex.length = 3;
3702
3703
2
      switch (i.tm.opcode_space)
3704
2
  {
3705
0
  case SPACE_0F:
3706
2
  case SPACE_0F38:
3707
2
  case SPACE_0F3A:
3708
2
    i.vex.bytes[0] = 0xc4;
3709
2
    break;
3710
0
  case SPACE_XOP08:
3711
0
  case SPACE_XOP09:
3712
0
  case SPACE_XOP0A:
3713
0
    i.vex.bytes[0] = 0x8f;
3714
0
    break;
3715
0
  default:
3716
0
    abort ();
3717
2
  }
3718
3719
      /* The high 3 bits of the second VEX byte are 1's compliment
3720
   of RXB bits from REX.  */
3721
2
      i.vex.bytes[1] = ((~i.rex & 7) << 5)
3722
2
           | (!dot_insn () ? i.tm.opcode_space
3723
2
               : i.insn_opcode_space);
3724
3725
2
      i.vex.bytes[2] = (w << 7
3726
2
      | register_specifier << 3
3727
2
      | vector_length << 2
3728
2
      | i.tm.opcode_modifier.opcodeprefix);
3729
2
    }
3730
2
}
3731
3732
static INLINE bool
3733
is_evex_encoding (const insn_template *t)
3734
630k
{
3735
630k
  return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
3736
630k
   || t->opcode_modifier.broadcast || t->opcode_modifier.masking
3737
630k
   || t->opcode_modifier.sae;
3738
630k
}
3739
3740
static INLINE bool
3741
is_any_vex_encoding (const insn_template *t)
3742
630k
{
3743
630k
  return t->opcode_modifier.vex || is_evex_encoding (t);
3744
630k
}
3745
3746
static unsigned int
3747
get_broadcast_bytes (const insn_template *t, bool diag)
3748
0
{
3749
0
  unsigned int op, bytes;
3750
0
  const i386_operand_type *types;
3751
3752
0
  if (i.broadcast.type)
3753
0
    return (1 << (t->opcode_modifier.broadcast - 1)) * i.broadcast.type;
3754
3755
0
  gas_assert (intel_syntax);
3756
3757
0
  for (op = 0; op < t->operands; ++op)
3758
0
    if (t->operand_types[op].bitfield.baseindex)
3759
0
      break;
3760
3761
0
  gas_assert (op < t->operands);
3762
3763
0
  if (t->opcode_modifier.evex
3764
0
      && t->opcode_modifier.evex != EVEXDYN)
3765
0
    switch (i.broadcast.bytes)
3766
0
      {
3767
0
      case 1:
3768
0
  if (t->operand_types[op].bitfield.word)
3769
0
    return 2;
3770
      /* Fall through.  */
3771
0
      case 2:
3772
0
  if (t->operand_types[op].bitfield.dword)
3773
0
    return 4;
3774
      /* Fall through.  */
3775
0
      case 4:
3776
0
  if (t->operand_types[op].bitfield.qword)
3777
0
    return 8;
3778
      /* Fall through.  */
3779
0
      case 8:
3780
0
  if (t->operand_types[op].bitfield.xmmword)
3781
0
    return 16;
3782
0
  if (t->operand_types[op].bitfield.ymmword)
3783
0
    return 32;
3784
0
  if (t->operand_types[op].bitfield.zmmword)
3785
0
    return 64;
3786
      /* Fall through.  */
3787
0
      default:
3788
0
        abort ();
3789
0
      }
3790
3791
0
  gas_assert (op + 1 < t->operands);
3792
3793
0
  if (t->operand_types[op + 1].bitfield.xmmword
3794
0
      + t->operand_types[op + 1].bitfield.ymmword
3795
0
      + t->operand_types[op + 1].bitfield.zmmword > 1)
3796
0
    {
3797
0
      types = &i.types[op + 1];
3798
0
      diag = false;
3799
0
    }
3800
0
  else /* Ambiguous - guess with a preference to non-AVX512VL forms.  */
3801
0
    types = &t->operand_types[op];
3802
3803
0
  if (types->bitfield.zmmword)
3804
0
    bytes = 64;
3805
0
  else if (types->bitfield.ymmword)
3806
0
    bytes = 32;
3807
0
  else
3808
0
    bytes = 16;
3809
3810
0
  if (diag)
3811
0
    as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
3812
0
       insn_name (t), bytes * 8);
3813
3814
0
  return bytes;
3815
0
}
3816
3817
/* Build the EVEX prefix.  */
3818
3819
static void
3820
build_evex_prefix (void)
3821
0
{
3822
0
  unsigned int register_specifier, w;
3823
0
  rex_byte vrex_used = 0;
3824
3825
  /* Check register specifier.  */
3826
0
  if (i.vex.register_specifier)
3827
0
    {
3828
0
      gas_assert ((i.vrex & REX_X) == 0);
3829
3830
0
      register_specifier = i.vex.register_specifier->reg_num;
3831
0
      if ((i.vex.register_specifier->reg_flags & RegRex))
3832
0
  register_specifier += 8;
3833
      /* The upper 16 registers are encoded in the fourth byte of the
3834
   EVEX prefix.  */
3835
0
      if (!(i.vex.register_specifier->reg_flags & RegVRex))
3836
0
  i.vex.bytes[3] = 0x8;
3837
0
      register_specifier = ~register_specifier & 0xf;
3838
0
    }
3839
0
  else
3840
0
    {
3841
0
      register_specifier = 0xf;
3842
3843
      /* Encode upper 16 vector index register in the fourth byte of
3844
   the EVEX prefix.  */
3845
0
      if (!(i.vrex & REX_X))
3846
0
  i.vex.bytes[3] = 0x8;
3847
0
      else
3848
0
  vrex_used |= REX_X;
3849
0
    }
3850
3851
  /* 4 byte EVEX prefix.  */
3852
0
  i.vex.length = 4;
3853
0
  i.vex.bytes[0] = 0x62;
3854
3855
  /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3856
     bits from REX.  */
3857
0
  gas_assert (i.tm.opcode_space >= SPACE_0F);
3858
0
  gas_assert (i.tm.opcode_space <= SPACE_EVEXMAP6);
3859
0
  i.vex.bytes[1] = ((~i.rex & 7) << 5)
3860
0
       | (!dot_insn () ? i.tm.opcode_space
3861
0
           : i.insn_opcode_space);
3862
3863
  /* The fifth bit of the second EVEX byte is 1's compliment of the
3864
     REX_R bit in VREX.  */
3865
0
  if (!(i.vrex & REX_R))
3866
0
    i.vex.bytes[1] |= 0x10;
3867
0
  else
3868
0
    vrex_used |= REX_R;
3869
3870
0
  if ((i.reg_operands + i.imm_operands) == i.operands)
3871
0
    {
3872
      /* When all operands are registers, the REX_X bit in REX is not
3873
   used.  We reuse it to encode the upper 16 registers, which is
3874
   indicated by the REX_B bit in VREX.  The REX_X bit is encoded
3875
   as 1's compliment.  */
3876
0
      if ((i.vrex & REX_B))
3877
0
  {
3878
0
    vrex_used |= REX_B;
3879
0
    i.vex.bytes[1] &= ~0x40;
3880
0
  }
3881
0
    }
3882
3883
  /* EVEX instructions shouldn't need the REX prefix.  */
3884
0
  i.vrex &= ~vrex_used;
3885
0
  gas_assert (i.vrex == 0);
3886
3887
  /* Check the REX.W bit and VEXW.  */
3888
0
  if (i.tm.opcode_modifier.vexw == VEXWIG)
3889
0
    w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3890
0
  else if (i.tm.opcode_modifier.vexw)
3891
0
    w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3892
0
  else
3893
0
    w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
3894
3895
  /* The third byte of the EVEX prefix.  */
3896
0
  i.vex.bytes[2] = ((w << 7)
3897
0
        | (register_specifier << 3)
3898
0
        | 4 /* Encode the U bit.  */
3899
0
        | i.tm.opcode_modifier.opcodeprefix);
3900
3901
  /* The fourth byte of the EVEX prefix.  */
3902
  /* The zeroing-masking bit.  */
3903
0
  if (i.mask.reg && i.mask.zeroing)
3904
0
    i.vex.bytes[3] |= 0x80;
3905
3906
  /* Don't always set the broadcast bit if there is no RC.  */
3907
0
  if (i.rounding.type == rc_none)
3908
0
    {
3909
      /* Encode the vector length.  */
3910
0
      unsigned int vec_length;
3911
3912
0
      if (!i.tm.opcode_modifier.evex
3913
0
    || i.tm.opcode_modifier.evex == EVEXDYN)
3914
0
  {
3915
0
    unsigned int op;
3916
3917
    /* Determine vector length from the last multi-length vector
3918
       operand.  */
3919
0
    for (op = i.operands; op--;)
3920
0
      if (i.tm.operand_types[op].bitfield.xmmword
3921
0
    + i.tm.operand_types[op].bitfield.ymmword
3922
0
    + i.tm.operand_types[op].bitfield.zmmword > 1)
3923
0
        {
3924
0
    if (i.types[op].bitfield.zmmword)
3925
0
      {
3926
0
        i.tm.opcode_modifier.evex = EVEX512;
3927
0
        break;
3928
0
      }
3929
0
    else if (i.types[op].bitfield.ymmword)
3930
0
      {
3931
0
        i.tm.opcode_modifier.evex = EVEX256;
3932
0
        break;
3933
0
      }
3934
0
    else if (i.types[op].bitfield.xmmword)
3935
0
      {
3936
0
        i.tm.opcode_modifier.evex = EVEX128;
3937
0
        break;
3938
0
      }
3939
0
    else if ((i.broadcast.type || i.broadcast.bytes)
3940
0
       && op == i.broadcast.operand)
3941
0
      {
3942
0
        switch (get_broadcast_bytes (&i.tm, true))
3943
0
          {
3944
0
      case 64:
3945
0
        i.tm.opcode_modifier.evex = EVEX512;
3946
0
        break;
3947
0
      case 32:
3948
0
        i.tm.opcode_modifier.evex = EVEX256;
3949
0
        break;
3950
0
      case 16:
3951
0
        i.tm.opcode_modifier.evex = EVEX128;
3952
0
        break;
3953
0
      default:
3954
0
        abort ();
3955
0
          }
3956
0
        break;
3957
0
      }
3958
0
        }
3959
3960
0
    if (op >= MAX_OPERANDS)
3961
0
      abort ();
3962
0
  }
3963
3964
0
      switch (i.tm.opcode_modifier.evex)
3965
0
  {
3966
0
  case EVEXLIG: /* LL' is ignored */
3967
0
    vec_length = evexlig << 5;
3968
0
    break;
3969
0
  case EVEX128:
3970
0
    vec_length = 0 << 5;
3971
0
    break;
3972
0
  case EVEX256:
3973
0
    vec_length = 1 << 5;
3974
0
    break;
3975
0
  case EVEX512:
3976
0
    vec_length = 2 << 5;
3977
0
    break;
3978
0
  case EVEX_L3:
3979
0
    if (dot_insn ())
3980
0
      {
3981
0
        vec_length = 3 << 5;
3982
0
        break;
3983
0
      }
3984
    /* Fall through.  */
3985
0
  default:
3986
0
    abort ();
3987
0
    break;
3988
0
  }
3989
0
      i.vex.bytes[3] |= vec_length;
3990
      /* Encode the broadcast bit.  */
3991
0
      if (i.broadcast.type || i.broadcast.bytes)
3992
0
  i.vex.bytes[3] |= 0x10;
3993
0
    }
3994
0
  else if (i.rounding.type != saeonly)
3995
0
    i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
3996
0
  else
3997
0
    i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
3998
3999
0
  if (i.mask.reg)
4000
0
    i.vex.bytes[3] |= i.mask.reg->reg_num;
4001
0
}
4002
4003
static void
4004
process_immext (void)
4005
0
{
4006
0
  expressionS *exp;
4007
4008
  /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
4009
     which is coded in the same place as an 8-bit immediate field
4010
     would be.  Here we fake an 8-bit immediate operand from the
4011
     opcode suffix stored in tm.extension_opcode.
4012
4013
     AVX instructions also use this encoding, for some of
4014
     3 argument instructions.  */
4015
4016
0
  gas_assert (i.imm_operands <= 1
4017
0
        && (i.operands <= 2
4018
0
      || (is_any_vex_encoding (&i.tm)
4019
0
          && i.operands <= 4)));
4020
4021
0
  exp = &im_expressions[i.imm_operands++];
4022
0
  i.op[i.operands].imms = exp;
4023
0
  i.types[i.operands].bitfield.imm8 = 1;
4024
0
  i.operands++;
4025
0
  exp->X_op = O_constant;
4026
0
  exp->X_add_number = i.tm.extension_opcode;
4027
0
  i.tm.extension_opcode = None;
4028
0
}
4029
4030
4031
static int
4032
check_hle (void)
4033
0
{
4034
0
  switch (i.tm.opcode_modifier.prefixok)
4035
0
    {
4036
0
    default:
4037
0
      abort ();
4038
0
    case PrefixLock:
4039
0
    case PrefixNone:
4040
0
    case PrefixNoTrack:
4041
0
    case PrefixRep:
4042
0
      as_bad (_("invalid instruction `%s' after `%s'"),
4043
0
        insn_name (&i.tm), i.hle_prefix);
4044
0
      return 0;
4045
0
    case PrefixHLELock:
4046
0
      if (i.prefix[LOCK_PREFIX])
4047
0
  return 1;
4048
0
      as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
4049
0
      return 0;
4050
0
    case PrefixHLEAny:
4051
0
      return 1;
4052
0
    case PrefixHLERelease:
4053
0
      if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4054
0
  {
4055
0
    as_bad (_("instruction `%s' after `xacquire' not allowed"),
4056
0
      insn_name (&i.tm));
4057
0
    return 0;
4058
0
  }
4059
0
      if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
4060
0
  {
4061
0
    as_bad (_("memory destination needed for instruction `%s'"
4062
0
        " after `xrelease'"), insn_name (&i.tm));
4063
0
    return 0;
4064
0
  }
4065
0
      return 1;
4066
0
    }
4067
0
}
4068
4069
/* Encode aligned vector move as unaligned vector move.  */
4070
4071
static void
4072
encode_with_unaligned_vector_move (void)
4073
0
{
4074
0
  switch (i.tm.base_opcode)
4075
0
    {
4076
0
    case 0x28:  /* Load instructions.  */
4077
0
    case 0x29:  /* Store instructions.  */
4078
      /* movaps/movapd/vmovaps/vmovapd.  */
4079
0
      if (i.tm.opcode_space == SPACE_0F
4080
0
    && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
4081
0
  i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
4082
0
      break;
4083
0
    case 0x6f:  /* Load instructions.  */
4084
0
    case 0x7f:  /* Store instructions.  */
4085
      /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
4086
0
      if (i.tm.opcode_space == SPACE_0F
4087
0
    && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4088
0
  i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4089
0
      break;
4090
0
    default:
4091
0
      break;
4092
0
    }
4093
0
}
4094
4095
/* Try the shortest encoding by shortening operand size.  */
4096
4097
static void
4098
optimize_encoding (void)
4099
0
{
4100
0
  unsigned int j;
4101
4102
0
  if (i.tm.mnem_off == MN_lea)
4103
0
    {
4104
      /* Optimize: -O:
4105
     lea symbol, %rN    -> mov $symbol, %rN
4106
     lea (%rM), %rN     -> mov %rM, %rN
4107
     lea (,%rM,1), %rN  -> mov %rM, %rN
4108
4109
     and in 32-bit mode for 16-bit addressing
4110
4111
     lea (%rM), %rN     -> movzx %rM, %rN
4112
4113
     and in 64-bit mode zap 32-bit addressing in favor of using a
4114
     32-bit (or less) destination.
4115
       */
4116
0
      if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4117
0
  {
4118
0
    if (!i.op[1].regs->reg_type.bitfield.word)
4119
0
      i.tm.opcode_modifier.size = SIZE32;
4120
0
    i.prefix[ADDR_PREFIX] = 0;
4121
0
  }
4122
4123
0
      if (!i.index_reg && !i.base_reg)
4124
0
  {
4125
    /* Handle:
4126
         lea symbol, %rN    -> mov $symbol, %rN
4127
     */
4128
0
    if (flag_code == CODE_64BIT)
4129
0
      {
4130
        /* Don't transform a relocation to a 16-bit one.  */
4131
0
        if (i.op[0].disps
4132
0
      && i.op[0].disps->X_op != O_constant
4133
0
      && i.op[1].regs->reg_type.bitfield.word)
4134
0
    return;
4135
4136
0
        if (!i.op[1].regs->reg_type.bitfield.qword
4137
0
      || i.tm.opcode_modifier.size == SIZE32)
4138
0
    {
4139
0
      i.tm.base_opcode = 0xb8;
4140
0
      i.tm.opcode_modifier.modrm = 0;
4141
0
      if (!i.op[1].regs->reg_type.bitfield.word)
4142
0
        i.types[0].bitfield.imm32 = 1;
4143
0
      else
4144
0
        {
4145
0
          i.tm.opcode_modifier.size = SIZE16;
4146
0
          i.types[0].bitfield.imm16 = 1;
4147
0
        }
4148
0
    }
4149
0
        else
4150
0
    {
4151
      /* Subject to further optimization below.  */
4152
0
      i.tm.base_opcode = 0xc7;
4153
0
      i.tm.extension_opcode = 0;
4154
0
      i.types[0].bitfield.imm32s = 1;
4155
0
      i.types[0].bitfield.baseindex = 0;
4156
0
    }
4157
0
      }
4158
    /* Outside of 64-bit mode address and operand sizes have to match if
4159
       a relocation is involved, as otherwise we wouldn't (currently) or
4160
       even couldn't express the relocation correctly.  */
4161
0
    else if (i.op[0].disps
4162
0
       && i.op[0].disps->X_op != O_constant
4163
0
       && ((!i.prefix[ADDR_PREFIX])
4164
0
           != (flag_code == CODE_32BIT
4165
0
         ? i.op[1].regs->reg_type.bitfield.dword
4166
0
         : i.op[1].regs->reg_type.bitfield.word)))
4167
0
      return;
4168
    /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4169
       destination is going to grow encoding size.  */
4170
0
    else if (flag_code == CODE_16BIT
4171
0
       && (optimize <= 1 || optimize_for_space)
4172
0
       && !i.prefix[ADDR_PREFIX]
4173
0
       && i.op[1].regs->reg_type.bitfield.dword)
4174
0
      return;
4175
0
    else
4176
0
      {
4177
0
        i.tm.base_opcode = 0xb8;
4178
0
        i.tm.opcode_modifier.modrm = 0;
4179
0
        if (i.op[1].regs->reg_type.bitfield.dword)
4180
0
    i.types[0].bitfield.imm32 = 1;
4181
0
        else
4182
0
    i.types[0].bitfield.imm16 = 1;
4183
4184
0
        if (i.op[0].disps
4185
0
      && i.op[0].disps->X_op == O_constant
4186
0
      && i.op[1].regs->reg_type.bitfield.dword
4187
      /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4188
         GCC 5. */
4189
0
      && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
4190
0
    i.op[0].disps->X_add_number &= 0xffff;
4191
0
      }
4192
4193
0
    i.tm.operand_types[0] = i.types[0];
4194
0
    i.imm_operands = 1;
4195
0
    if (!i.op[0].imms)
4196
0
      {
4197
0
        i.op[0].imms = &im_expressions[0];
4198
0
        i.op[0].imms->X_op = O_absent;
4199
0
      }
4200
0
  }
4201
0
      else if (i.op[0].disps
4202
0
      && (i.op[0].disps->X_op != O_constant
4203
0
          || i.op[0].disps->X_add_number))
4204
0
  return;
4205
0
      else
4206
0
  {
4207
    /* Handle:
4208
         lea (%rM), %rN     -> mov %rM, %rN
4209
         lea (,%rM,1), %rN  -> mov %rM, %rN
4210
         lea (%rM), %rN     -> movzx %rM, %rN
4211
     */
4212
0
    const reg_entry *addr_reg;
4213
4214
0
    if (!i.index_reg && i.base_reg->reg_num != RegIP)
4215
0
      addr_reg = i.base_reg;
4216
0
    else if (!i.base_reg
4217
0
       && i.index_reg->reg_num != RegIZ
4218
0
       && !i.log2_scale_factor)
4219
0
      addr_reg = i.index_reg;
4220
0
    else
4221
0
      return;
4222
4223
0
    if (addr_reg->reg_type.bitfield.word
4224
0
        && i.op[1].regs->reg_type.bitfield.dword)
4225
0
      {
4226
0
        if (flag_code != CODE_32BIT)
4227
0
    return;
4228
0
        i.tm.opcode_space = SPACE_0F;
4229
0
        i.tm.base_opcode = 0xb7;
4230
0
      }
4231
0
    else
4232
0
      i.tm.base_opcode = 0x8b;
4233
4234
0
    if (addr_reg->reg_type.bitfield.dword
4235
0
        && i.op[1].regs->reg_type.bitfield.qword)
4236
0
      i.tm.opcode_modifier.size = SIZE32;
4237
4238
0
    i.op[0].regs = addr_reg;
4239
0
    i.reg_operands = 2;
4240
0
  }
4241
4242
0
      i.mem_operands = 0;
4243
0
      i.disp_operands = 0;
4244
0
      i.prefix[ADDR_PREFIX] = 0;
4245
0
      i.prefix[SEG_PREFIX] = 0;
4246
0
      i.seg[0] = NULL;
4247
0
    }
4248
4249
0
  if (optimize_for_space
4250
0
      && i.tm.mnem_off == MN_test
4251
0
      && i.reg_operands == 1
4252
0
      && i.imm_operands == 1
4253
0
      && !i.types[1].bitfield.byte
4254
0
      && i.op[0].imms->X_op == O_constant
4255
0
      && fits_in_imm7 (i.op[0].imms->X_add_number))
4256
0
    {
4257
      /* Optimize: -Os:
4258
     test $imm7, %r64/%r32/%r16  -> test $imm7, %r8
4259
       */
4260
0
      unsigned int base_regnum = i.op[1].regs->reg_num;
4261
0
      if (flag_code == CODE_64BIT || base_regnum < 4)
4262
0
  {
4263
0
    i.types[1].bitfield.byte = 1;
4264
    /* Ignore the suffix.  */
4265
0
    i.suffix = 0;
4266
    /* Convert to byte registers.  */
4267
0
    if (i.types[1].bitfield.word)
4268
0
      j = 16;
4269
0
    else if (i.types[1].bitfield.dword)
4270
0
      j = 32;
4271
0
    else
4272
0
      j = 48;
4273
0
    if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4274
0
      j += 8;
4275
0
    i.op[1].regs -= j;
4276
0
  }
4277
0
    }
4278
0
  else if (flag_code == CODE_64BIT
4279
0
     && i.tm.opcode_space == SPACE_BASE
4280
0
     && ((i.types[1].bitfield.qword
4281
0
    && i.reg_operands == 1
4282
0
    && i.imm_operands == 1
4283
0
    && i.op[0].imms->X_op == O_constant
4284
0
    && ((i.tm.base_opcode == 0xb8
4285
0
         && i.tm.extension_opcode == None
4286
0
         && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4287
0
        || (fits_in_imm31 (i.op[0].imms->X_add_number)
4288
0
      && (i.tm.base_opcode == 0x24
4289
0
          || (i.tm.base_opcode == 0x80
4290
0
        && i.tm.extension_opcode == 0x4)
4291
0
          || i.tm.mnem_off == MN_test
4292
0
          || ((i.tm.base_opcode | 1) == 0xc7
4293
0
        && i.tm.extension_opcode == 0x0)))
4294
0
        || (fits_in_imm7 (i.op[0].imms->X_add_number)
4295
0
      && i.tm.base_opcode == 0x83
4296
0
      && i.tm.extension_opcode == 0x4)))
4297
0
         || (i.types[0].bitfield.qword
4298
0
       && ((i.reg_operands == 2
4299
0
      && i.op[0].regs == i.op[1].regs
4300
0
      && (i.tm.mnem_off == MN_xor
4301
0
          || i.tm.mnem_off == MN_sub))
4302
0
           || i.tm.mnem_off == MN_clr))))
4303
0
    {
4304
      /* Optimize: -O:
4305
     andq $imm31, %r64   -> andl $imm31, %r32
4306
     andq $imm7, %r64    -> andl $imm7, %r32
4307
     testq $imm31, %r64  -> testl $imm31, %r32
4308
     xorq %r64, %r64     -> xorl %r32, %r32
4309
     subq %r64, %r64     -> subl %r32, %r32
4310
     movq $imm31, %r64   -> movl $imm31, %r32
4311
     movq $imm32, %r64   -> movl $imm32, %r32
4312
        */
4313
0
      i.tm.opcode_modifier.size = SIZE32;
4314
0
      if (i.imm_operands)
4315
0
  {
4316
0
    i.types[0].bitfield.imm32 = 1;
4317
0
    i.types[0].bitfield.imm32s = 0;
4318
0
    i.types[0].bitfield.imm64 = 0;
4319
0
  }
4320
0
      else
4321
0
  {
4322
0
    i.types[0].bitfield.dword = 1;
4323
0
    i.types[0].bitfield.qword = 0;
4324
0
  }
4325
0
      i.types[1].bitfield.dword = 1;
4326
0
      i.types[1].bitfield.qword = 0;
4327
0
      if (i.tm.mnem_off == MN_mov || i.tm.mnem_off == MN_lea)
4328
0
  {
4329
    /* Handle
4330
         movq $imm31, %r64   -> movl $imm31, %r32
4331
         movq $imm32, %r64   -> movl $imm32, %r32
4332
     */
4333
0
    i.tm.operand_types[0].bitfield.imm32 = 1;
4334
0
    i.tm.operand_types[0].bitfield.imm32s = 0;
4335
0
    i.tm.operand_types[0].bitfield.imm64 = 0;
4336
0
    if ((i.tm.base_opcode | 1) == 0xc7)
4337
0
      {
4338
        /* Handle
4339
       movq $imm31, %r64   -> movl $imm31, %r32
4340
         */
4341
0
        i.tm.base_opcode = 0xb8;
4342
0
        i.tm.extension_opcode = None;
4343
0
        i.tm.opcode_modifier.w = 0;
4344
0
        i.tm.opcode_modifier.modrm = 0;
4345
0
      }
4346
0
  }
4347
0
    }
4348
0
  else if (optimize > 1
4349
0
     && !optimize_for_space
4350
0
     && i.reg_operands == 2
4351
0
     && i.op[0].regs == i.op[1].regs
4352
0
     && (i.tm.mnem_off == MN_and || i.tm.mnem_off == MN_or)
4353
0
     && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4354
0
    {
4355
      /* Optimize: -O2:
4356
     andb %rN, %rN  -> testb %rN, %rN
4357
     andw %rN, %rN  -> testw %rN, %rN
4358
     andq %rN, %rN  -> testq %rN, %rN
4359
     orb %rN, %rN   -> testb %rN, %rN
4360
     orw %rN, %rN   -> testw %rN, %rN
4361
     orq %rN, %rN   -> testq %rN, %rN
4362
4363
     and outside of 64-bit mode
4364
4365
     andl %rN, %rN  -> testl %rN, %rN
4366
     orl %rN, %rN   -> testl %rN, %rN
4367
       */
4368
0
      i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4369
0
    }
4370
0
  else if (i.tm.base_opcode == 0xba
4371
0
     && i.tm.opcode_space == SPACE_0F
4372
0
     && i.reg_operands == 1
4373
0
     && i.op[0].imms->X_op == O_constant
4374
0
     && i.op[0].imms->X_add_number >= 0)
4375
0
    {
4376
      /* Optimize: -O:
4377
     btw $n, %rN -> btl $n, %rN (outside of 16-bit mode, n < 16)
4378
     btq $n, %rN -> btl $n, %rN (in 64-bit mode, n < 32, N < 8)
4379
     btl $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
4380
4381
     With <BT> one of bts, btr, and bts also:
4382
     <BT>w $n, %rN -> btl $n, %rN (in 32-bit mode, n < 16)
4383
     <BT>l $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
4384
       */
4385
0
      switch (flag_code)
4386
0
  {
4387
0
  case CODE_64BIT:
4388
0
    if (i.tm.extension_opcode != 4)
4389
0
      break;
4390
0
    if (i.types[1].bitfield.qword
4391
0
        && i.op[0].imms->X_add_number < 32
4392
0
        && !(i.op[1].regs->reg_flags & RegRex))
4393
0
      i.tm.opcode_modifier.size = SIZE32;
4394
    /* Fall through.  */
4395
0
  case CODE_32BIT:
4396
0
    if (i.types[1].bitfield.word
4397
0
        && i.op[0].imms->X_add_number < 16)
4398
0
      i.tm.opcode_modifier.size = SIZE32;
4399
0
    break;
4400
0
  case CODE_16BIT:
4401
0
    if (i.op[0].imms->X_add_number < 16)
4402
0
      i.tm.opcode_modifier.size = SIZE16;
4403
0
    break;
4404
0
  }
4405
0
    }
4406
0
  else if (i.reg_operands == 3
4407
0
     && i.op[0].regs == i.op[1].regs
4408
0
     && !i.types[2].bitfield.xmmword
4409
0
     && (i.tm.opcode_modifier.vex
4410
0
         || ((!i.mask.reg || i.mask.zeroing)
4411
0
       && is_evex_encoding (&i.tm)
4412
0
       && (i.vec_encoding != vex_encoding_evex
4413
0
           || cpu_arch_isa_flags.bitfield.cpuavx512vl
4414
0
           || i.tm.cpu_flags.bitfield.cpuavx512vl
4415
0
           || (i.tm.operand_types[2].bitfield.zmmword
4416
0
         && i.types[2].bitfield.ymmword))))
4417
0
     && i.tm.opcode_space == SPACE_0F
4418
0
     && ((i.tm.base_opcode | 2) == 0x57
4419
0
         || i.tm.base_opcode == 0xdf
4420
0
         || i.tm.base_opcode == 0xef
4421
0
         || (i.tm.base_opcode | 3) == 0xfb
4422
0
         || i.tm.base_opcode == 0x42
4423
0
         || i.tm.base_opcode == 0x47))
4424
0
    {
4425
      /* Optimize: -O1:
4426
     VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4427
     vpsubq and vpsubw:
4428
       EVEX VOP %zmmM, %zmmM, %zmmN
4429
         -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
4430
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4431
       EVEX VOP %ymmM, %ymmM, %ymmN
4432
         -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
4433
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4434
       VEX VOP %ymmM, %ymmM, %ymmN
4435
         -> VEX VOP %xmmM, %xmmM, %xmmN
4436
     VOP, one of vpandn and vpxor:
4437
       VEX VOP %ymmM, %ymmM, %ymmN
4438
         -> VEX VOP %xmmM, %xmmM, %xmmN
4439
     VOP, one of vpandnd and vpandnq:
4440
       EVEX VOP %zmmM, %zmmM, %zmmN
4441
         -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
4442
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4443
       EVEX VOP %ymmM, %ymmM, %ymmN
4444
         -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
4445
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4446
     VOP, one of vpxord and vpxorq:
4447
       EVEX VOP %zmmM, %zmmM, %zmmN
4448
         -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
4449
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4450
       EVEX VOP %ymmM, %ymmM, %ymmN
4451
         -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
4452
         -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
4453
     VOP, one of kxord and kxorq:
4454
       VEX VOP %kM, %kM, %kN
4455
         -> VEX kxorw %kM, %kM, %kN
4456
     VOP, one of kandnd and kandnq:
4457
       VEX VOP %kM, %kM, %kN
4458
         -> VEX kandnw %kM, %kM, %kN
4459
       */
4460
0
      if (is_evex_encoding (&i.tm))
4461
0
  {
4462
0
    if (i.vec_encoding != vex_encoding_evex)
4463
0
      {
4464
0
        i.tm.opcode_modifier.vex = VEX128;
4465
0
        i.tm.opcode_modifier.vexw = VEXW0;
4466
0
        i.tm.opcode_modifier.evex = 0;
4467
0
      }
4468
0
    else if (optimize > 1)
4469
0
      i.tm.opcode_modifier.evex = EVEX128;
4470
0
    else
4471
0
      return;
4472
0
  }
4473
0
      else if (i.tm.operand_types[0].bitfield.class == RegMask)
4474
0
  {
4475
0
    i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
4476
0
    i.tm.opcode_modifier.vexw = VEXW0;
4477
0
  }
4478
0
      else
4479
0
  i.tm.opcode_modifier.vex = VEX128;
4480
4481
0
      if (i.tm.opcode_modifier.vex)
4482
0
  for (j = 0; j < 3; j++)
4483
0
    {
4484
0
      i.types[j].bitfield.xmmword = 1;
4485
0
      i.types[j].bitfield.ymmword = 0;
4486
0
    }
4487
0
    }
4488
0
  else if (i.vec_encoding != vex_encoding_evex
4489
0
     && !i.types[0].bitfield.zmmword
4490
0
     && !i.types[1].bitfield.zmmword
4491
0
     && !i.mask.reg
4492
0
     && !i.broadcast.type
4493
0
     && !i.broadcast.bytes
4494
0
     && is_evex_encoding (&i.tm)
4495
0
     && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4496
0
         || (i.tm.base_opcode & ~4) == 0xdb
4497
0
         || (i.tm.base_opcode & ~4) == 0xeb)
4498
0
     && i.tm.extension_opcode == None)
4499
0
    {
4500
      /* Optimize: -O1:
4501
     VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4502
     vmovdqu32 and vmovdqu64:
4503
       EVEX VOP %xmmM, %xmmN
4504
         -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4505
       EVEX VOP %ymmM, %ymmN
4506
         -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4507
       EVEX VOP %xmmM, mem
4508
         -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4509
       EVEX VOP %ymmM, mem
4510
         -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4511
       EVEX VOP mem, %xmmN
4512
         -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4513
       EVEX VOP mem, %ymmN
4514
         -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
4515
     VOP, one of vpand, vpandn, vpor, vpxor:
4516
       EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4517
         -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4518
       EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4519
         -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4520
       EVEX VOP{d,q} mem, %xmmM, %xmmN
4521
         -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4522
       EVEX VOP{d,q} mem, %ymmM, %ymmN
4523
         -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
4524
       */
4525
0
      for (j = 0; j < i.operands; j++)
4526
0
  if (operand_type_check (i.types[j], disp)
4527
0
      && i.op[j].disps->X_op == O_constant)
4528
0
    {
4529
      /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4530
         has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4531
         bytes, we choose EVEX Disp8 over VEX Disp32.  */
4532
0
      int evex_disp8, vex_disp8;
4533
0
      unsigned int memshift = i.memshift;
4534
0
      offsetT n = i.op[j].disps->X_add_number;
4535
4536
0
      evex_disp8 = fits_in_disp8 (n);
4537
0
      i.memshift = 0;
4538
0
      vex_disp8 = fits_in_disp8 (n);
4539
0
      if (evex_disp8 != vex_disp8)
4540
0
        {
4541
0
    i.memshift = memshift;
4542
0
    return;
4543
0
        }
4544
4545
0
      i.types[j].bitfield.disp8 = vex_disp8;
4546
0
      break;
4547
0
    }
4548
0
      if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4549
0
    && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4550
0
  i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4551
0
      i.tm.opcode_modifier.vex
4552
0
  = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4553
0
      i.tm.opcode_modifier.vexw = VEXW0;
4554
      /* VPAND, VPOR, and VPXOR are commutative.  */
4555
0
      if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
4556
0
  i.tm.opcode_modifier.commutative = 1;
4557
0
      i.tm.opcode_modifier.evex = 0;
4558
0
      i.tm.opcode_modifier.masking = 0;
4559
0
      i.tm.opcode_modifier.broadcast = 0;
4560
0
      i.tm.opcode_modifier.disp8memshift = 0;
4561
0
      i.memshift = 0;
4562
0
      if (j < i.operands)
4563
0
  i.types[j].bitfield.disp8
4564
0
    = fits_in_disp8 (i.op[j].disps->X_add_number);
4565
0
    }
4566
0
}
4567
4568
/* Return non-zero for load instruction.  */
4569
4570
static int
4571
load_insn_p (void)
4572
0
{
4573
0
  unsigned int dest;
4574
0
  int any_vex_p = is_any_vex_encoding (&i.tm);
4575
0
  unsigned int base_opcode = i.tm.base_opcode | 1;
4576
4577
0
  if (!any_vex_p)
4578
0
    {
4579
      /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
4580
   bndcn, bndstx, bndldx, clflushopt, clwb, cldemote.  */
4581
0
      if (i.tm.opcode_modifier.operandconstraint == ANY_SIZE)
4582
0
  return 0;
4583
4584
      /* pop.   */
4585
0
      if (i.tm.mnem_off == MN_pop)
4586
0
  return 1;
4587
0
    }
4588
4589
0
  if (i.tm.opcode_space == SPACE_BASE)
4590
0
    {
4591
      /* popf, popa.   */
4592
0
      if (i.tm.base_opcode == 0x9d
4593
0
    || i.tm.base_opcode == 0x61)
4594
0
  return 1;
4595
4596
      /* movs, cmps, lods, scas.  */
4597
0
      if ((i.tm.base_opcode | 0xb) == 0xaf)
4598
0
  return 1;
4599
4600
      /* outs, xlatb.  */
4601
0
      if (base_opcode == 0x6f
4602
0
    || i.tm.base_opcode == 0xd7)
4603
0
  return 1;
4604
      /* NB: For AMD-specific insns with implicit memory operands,
4605
   they're intentionally not covered.  */
4606
0
    }
4607
4608
  /* No memory operand.  */
4609
0
  if (!i.mem_operands)
4610
0
    return 0;
4611
4612
0
  if (any_vex_p)
4613
0
    {
4614
0
      if (i.tm.mnem_off == MN_vldmxcsr)
4615
0
  return 1;
4616
0
    }
4617
0
  else if (i.tm.opcode_space == SPACE_BASE)
4618
0
    {
4619
      /* test, not, neg, mul, imul, div, idiv.  */
4620
0
      if (base_opcode == 0xf7 && i.tm.extension_opcode != 1)
4621
0
  return 1;
4622
4623
      /* inc, dec.  */
4624
0
      if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4625
0
  return 1;
4626
4627
      /* add, or, adc, sbb, and, sub, xor, cmp.  */
4628
0
      if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4629
0
  return 1;
4630
4631
      /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
4632
0
      if ((base_opcode == 0xc1 || (base_opcode | 2) == 0xd3)
4633
0
    && i.tm.extension_opcode != 6)
4634
0
  return 1;
4635
4636
      /* Check for x87 instructions.  */
4637
0
      if ((base_opcode | 6) == 0xdf)
4638
0
  {
4639
    /* Skip fst, fstp, fstenv, fstcw.  */
4640
0
    if (i.tm.base_opcode == 0xd9
4641
0
        && (i.tm.extension_opcode == 2
4642
0
      || i.tm.extension_opcode == 3
4643
0
      || i.tm.extension_opcode == 6
4644
0
      || i.tm.extension_opcode == 7))
4645
0
      return 0;
4646
4647
    /* Skip fisttp, fist, fistp, fstp.  */
4648
0
    if (i.tm.base_opcode == 0xdb
4649
0
        && (i.tm.extension_opcode == 1
4650
0
      || i.tm.extension_opcode == 2
4651
0
      || i.tm.extension_opcode == 3
4652
0
      || i.tm.extension_opcode == 7))
4653
0
      return 0;
4654
4655
    /* Skip fisttp, fst, fstp, fsave, fstsw.  */
4656
0
    if (i.tm.base_opcode == 0xdd
4657
0
        && (i.tm.extension_opcode == 1
4658
0
      || i.tm.extension_opcode == 2
4659
0
      || i.tm.extension_opcode == 3
4660
0
      || i.tm.extension_opcode == 6
4661
0
      || i.tm.extension_opcode == 7))
4662
0
      return 0;
4663
4664
    /* Skip fisttp, fist, fistp, fbstp, fistp.  */
4665
0
    if (i.tm.base_opcode == 0xdf
4666
0
        && (i.tm.extension_opcode == 1
4667
0
      || i.tm.extension_opcode == 2
4668
0
      || i.tm.extension_opcode == 3
4669
0
      || i.tm.extension_opcode == 6
4670
0
      || i.tm.extension_opcode == 7))
4671
0
      return 0;
4672
4673
0
    return 1;
4674
0
  }
4675
0
    }
4676
0
  else if (i.tm.opcode_space == SPACE_0F)
4677
0
    {
4678
      /* bt, bts, btr, btc.  */
4679
0
      if (i.tm.base_opcode == 0xba
4680
0
    && (i.tm.extension_opcode | 3) == 7)
4681
0
  return 1;
4682
4683
      /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld.  */
4684
0
      if (i.tm.base_opcode == 0xc7
4685
0
    && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4686
0
    && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4687
0
        || i.tm.extension_opcode == 6))
4688
0
  return 1;
4689
4690
      /* fxrstor, ldmxcsr, xrstor.  */
4691
0
      if (i.tm.base_opcode == 0xae
4692
0
    && (i.tm.extension_opcode == 1
4693
0
        || i.tm.extension_opcode == 2
4694
0
        || i.tm.extension_opcode == 5))
4695
0
  return 1;
4696
4697
      /* lgdt, lidt, lmsw.  */
4698
0
      if (i.tm.base_opcode == 0x01
4699
0
    && (i.tm.extension_opcode == 2
4700
0
        || i.tm.extension_opcode == 3
4701
0
        || i.tm.extension_opcode == 6))
4702
0
  return 1;
4703
0
    }
4704
4705
0
  dest = i.operands - 1;
4706
4707
  /* Check fake imm8 operand and 3 source operands.  */
4708
0
  if ((i.tm.opcode_modifier.immext
4709
0
       || i.reg_operands + i.mem_operands == 4)
4710
0
      && i.types[dest].bitfield.imm8)
4711
0
    dest--;
4712
4713
  /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg.  */
4714
0
  if (i.tm.opcode_space == SPACE_BASE
4715
0
      && ((base_opcode | 0x38) == 0x39
4716
0
    || (base_opcode | 2) == 0x87))
4717
0
    return 1;
4718
4719
0
  if (i.tm.mnem_off == MN_xadd)
4720
0
    return 1;
4721
4722
  /* Check for load instruction.  */
4723
0
  return (i.types[dest].bitfield.class != ClassNone
4724
0
    || i.types[dest].bitfield.instance == Accum);
4725
0
}
4726
4727
/* Output lfence, 0xfaee8, after instruction.  */
4728
4729
static void
4730
insert_lfence_after (void)
4731
187k
{
4732
187k
  if (lfence_after_load && load_insn_p ())
4733
0
    {
4734
      /* There are also two REP string instructions that require
4735
   special treatment. Specifically, the compare string (CMPS)
4736
   and scan string (SCAS) instructions set EFLAGS in a manner
4737
   that depends on the data being compared/scanned. When used
4738
   with a REP prefix, the number of iterations may therefore
4739
   vary depending on this data. If the data is a program secret
4740
   chosen by the adversary using an LVI method,
4741
   then this data-dependent behavior may leak some aspect
4742
   of the secret.  */
4743
0
      if (((i.tm.base_opcode | 0x9) == 0xaf)
4744
0
    && i.prefix[REP_PREFIX])
4745
0
  {
4746
0
      as_warn (_("`%s` changes flags which would affect control flow behavior"),
4747
0
         insn_name (&i.tm));
4748
0
  }
4749
0
      char *p = frag_more (3);
4750
0
      *p++ = 0xf;
4751
0
      *p++ = 0xae;
4752
0
      *p = 0xe8;
4753
0
    }
4754
187k
}
4755
4756
/* Output lfence, 0xfaee8, before instruction.  */
4757
4758
static void
4759
insert_lfence_before (void)
4760
187k
{
4761
187k
  char *p;
4762
4763
187k
  if (i.tm.opcode_space != SPACE_BASE)
4764
3.20k
    return;
4765
4766
183k
  if (i.tm.base_opcode == 0xff
4767
183k
      && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4768
6
    {
4769
      /* Insert lfence before indirect branch if needed.  */
4770
4771
6
      if (lfence_before_indirect_branch == lfence_branch_none)
4772
6
  return;
4773
4774
0
      if (i.operands != 1)
4775
0
  abort ();
4776
4777
0
      if (i.reg_operands == 1)
4778
0
  {
4779
    /* Indirect branch via register.  Don't insert lfence with
4780
       -mlfence-after-load=yes.  */
4781
0
    if (lfence_after_load
4782
0
        || lfence_before_indirect_branch == lfence_branch_memory)
4783
0
      return;
4784
0
  }
4785
0
      else if (i.mem_operands == 1
4786
0
         && lfence_before_indirect_branch != lfence_branch_register)
4787
0
  {
4788
0
    as_warn (_("indirect `%s` with memory operand should be avoided"),
4789
0
       insn_name (&i.tm));
4790
0
    return;
4791
0
  }
4792
0
      else
4793
0
  return;
4794
4795
0
      if (last_insn.kind != last_insn_other
4796
0
    && last_insn.seg == now_seg)
4797
0
  {
4798
0
    as_warn_where (last_insn.file, last_insn.line,
4799
0
       _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
4800
0
       last_insn.name, insn_name (&i.tm));
4801
0
    return;
4802
0
  }
4803
4804
0
      p = frag_more (3);
4805
0
      *p++ = 0xf;
4806
0
      *p++ = 0xae;
4807
0
      *p = 0xe8;
4808
0
      return;
4809
0
    }
4810
4811
  /* Output or/not/shl and lfence before near ret.  */
4812
183k
  if (lfence_before_ret != lfence_before_ret_none
4813
183k
      && (i.tm.base_opcode | 1) == 0xc3)
4814
0
    {
4815
0
      if (last_insn.kind != last_insn_other
4816
0
    && last_insn.seg == now_seg)
4817
0
  {
4818
0
    as_warn_where (last_insn.file, last_insn.line,
4819
0
       _("`%s` skips -mlfence-before-ret on `%s`"),
4820
0
       last_insn.name, insn_name (&i.tm));
4821
0
    return;
4822
0
  }
4823
4824
      /* Near ret ingore operand size override under CPU64.  */
4825
0
      char prefix = flag_code == CODE_64BIT
4826
0
        ? 0x48
4827
0
        : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
4828
4829
0
      if (lfence_before_ret == lfence_before_ret_not)
4830
0
  {
4831
    /* not: 0xf71424, may add prefix
4832
       for operand size override or 64-bit code.  */
4833
0
    p = frag_more ((prefix ? 2 : 0) + 6 + 3);
4834
0
    if (prefix)
4835
0
      *p++ = prefix;
4836
0
    *p++ = 0xf7;
4837
0
    *p++ = 0x14;
4838
0
    *p++ = 0x24;
4839
0
    if (prefix)
4840
0
      *p++ = prefix;
4841
0
    *p++ = 0xf7;
4842
0
    *p++ = 0x14;
4843
0
    *p++ = 0x24;
4844
0
  }
4845
0
      else
4846
0
  {
4847
0
    p = frag_more ((prefix ? 1 : 0) + 4 + 3);
4848
0
    if (prefix)
4849
0
      *p++ = prefix;
4850
0
    if (lfence_before_ret == lfence_before_ret_or)
4851
0
      {
4852
        /* or: 0x830c2400, may add prefix
4853
     for operand size override or 64-bit code.  */
4854
0
        *p++ = 0x83;
4855
0
        *p++ = 0x0c;
4856
0
      }
4857
0
    else
4858
0
      {
4859
        /* shl: 0xc1242400, may add prefix
4860
     for operand size override or 64-bit code.  */
4861
0
        *p++ = 0xc1;
4862
0
        *p++ = 0x24;
4863
0
      }
4864
4865
0
    *p++ = 0x24;
4866
0
    *p++ = 0x0;
4867
0
  }
4868
4869
0
      *p++ = 0xf;
4870
0
      *p++ = 0xae;
4871
0
      *p = 0xe8;
4872
0
    }
4873
183k
}
4874
4875
/* Shared helper for md_assemble() and s_insn().  */
4876
static void init_globals (void)
4877
969k
{
4878
969k
  unsigned int j;
4879
4880
969k
  memset (&i, '\0', sizeof (i));
4881
969k
  i.rounding.type = rc_none;
4882
5.81M
  for (j = 0; j < MAX_OPERANDS; j++)
4883
4.84M
    i.reloc[j] = NO_RELOC;
4884
969k
  memset (disp_expressions, '\0', sizeof (disp_expressions));
4885
969k
  memset (im_expressions, '\0', sizeof (im_expressions));
4886
969k
  save_stack_p = save_stack;
4887
969k
}
4888
4889
/* Helper for md_assemble() to decide whether to prepare for a possible 2nd
4890
   parsing pass. Instead of introducing a rarely use new insn attribute this
4891
   utilizes a common pattern between affected templates. It is deemed
4892
   acceptable that this will lead to unnecessary pass 2 preparations in a
4893
   limited set of cases.  */
4894
static INLINE bool may_need_pass2 (const insn_template *t)
4895
263k
{
4896
263k
  return t->opcode_modifier.sse2avx
4897
   /* Note that all SSE2AVX templates have at least one operand.  */
4898
263k
   ? t->operand_types[t->operands - 1].bitfield.class == RegSIMD
4899
263k
   : (t->opcode_space == SPACE_0F
4900
259k
      && (t->base_opcode | 1) == 0xbf)
4901
259k
     || (t->opcode_space == SPACE_BASE
4902
259k
         && t->base_opcode == 0x63);
4903
263k
}
4904
4905
/* This is the guts of the machine-dependent assembler.  LINE points to a
4906
   machine dependent instruction.  This function is supposed to emit
4907
   the frags/bytes it assembles to.  */
4908
4909
void
4910
md_assemble (char *line)
4911
966k
{
4912
966k
  unsigned int j;
4913
966k
  char mnemonic[MAX_MNEM_SIZE], mnem_suffix = 0, *copy = NULL;
4914
966k
  const char *end, *pass1_mnem = NULL;
4915
966k
  enum i386_error pass1_err = 0;
4916
966k
  const insn_template *t;
4917
4918
  /* Initialize globals.  */
4919
966k
  current_templates = NULL;
4920
969k
 retry:
4921
969k
  init_globals ();
4922
4923
  /* First parse an instruction mnemonic & call i386_operand for the operands.
4924
     We assume that the scrubber has arranged it so that line[0] is the valid
4925
     start of a (possibly prefixed) mnemonic.  */
4926
4927
969k
  end = parse_insn (line, mnemonic, false);
4928
969k
  if (end == NULL)
4929
710k
    {
4930
710k
      if (pass1_mnem != NULL)
4931
3.38k
  goto match_error;
4932
706k
      if (i.error != no_error)
4933
4.02k
  {
4934
4.02k
    gas_assert (current_templates != NULL);
4935
4.02k
    if (may_need_pass2 (current_templates->start) && !i.suffix)
4936
273
      goto no_match;
4937
    /* No point in trying a 2nd pass - it'll only find the same suffix
4938
       again.  */
4939
3.75k
    mnem_suffix = i.suffix;
4940
3.75k
    goto match_error;
4941
4.02k
  }
4942
702k
      return;
4943
706k
    }
4944
259k
  t = current_templates->start;
4945
259k
  if (may_need_pass2 (t))
4946
3.43k
    {
4947
      /* Make a copy of the full line in case we need to retry.  */
4948
3.43k
      copy = xstrdup (line);
4949
3.43k
    }
4950
259k
  line += end - line;
4951
259k
  mnem_suffix = i.suffix;
4952
4953
259k
  line = parse_operands (line, mnemonic);
4954
259k
  this_operand = -1;
4955
259k
  if (line == NULL)
4956
35.1k
    {
4957
35.1k
      free (copy);
4958
35.1k
      return;
4959
35.1k
    }
4960
4961
  /* Now we've parsed the mnemonic into a set of templates, and have the
4962
     operands at hand.  */
4963
4964
  /* All Intel opcodes have reversed operands except for "bound", "enter",
4965
     "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
4966
     "rmpadjust", "rmpupdate", and "rmpquery".  We also don't reverse
4967
     intersegment "jmp" and "call" instructions with 2 immediate operands so
4968
     that the immediate segment precedes the offset consistently in Intel and
4969
     AT&T modes.  */
4970
224k
  if (intel_syntax
4971
224k
      && i.operands > 1
4972
224k
      && (t->mnem_off != MN_bound)
4973
224k
      && !startswith (mnemonic, "invlpg")
4974
224k
      && !startswith (mnemonic, "monitor")
4975
224k
      && !startswith (mnemonic, "mwait")
4976
224k
      && (t->mnem_off != MN_pvalidate)
4977
224k
      && !startswith (mnemonic, "rmp")
4978
224k
      && (t->mnem_off != MN_tpause)
4979
224k
      && (t->mnem_off != MN_umwait)
4980
224k
      && !(i.operands == 2
4981
0
     && operand_type_check (i.types[0], imm)
4982
0
     && operand_type_check (i.types[1], imm)))
4983
0
    swap_operands ();
4984
4985
  /* The order of the immediates should be reversed
4986
     for 2 immediates extrq and insertq instructions */
4987
224k
  if (i.imm_operands == 2
4988
224k
      && (t->mnem_off == MN_extrq || t->mnem_off == MN_insertq))
4989
0
      swap_2_operands (0, 1);
4990
4991
224k
  if (i.imm_operands)
4992
80.0k
    optimize_imm ();
4993
4994
224k
  if (i.disp_operands && !optimize_disp (t))
4995
0
    return;
4996
4997
  /* Next, we find a template that matches the given insn,
4998
     making sure the overlap of the given operands types is consistent
4999
     with the template operand types.  */
5000
5001
224k
  if (!(t = match_template (mnem_suffix)))
5002
37.0k
    {
5003
37.0k
      const char *err_msg;
5004
5005
37.0k
      if (copy && !mnem_suffix)
5006
3.11k
  {
5007
3.11k
    line = copy;
5008
3.11k
    copy = NULL;
5009
3.38k
  no_match:
5010
3.38k
    pass1_err = i.error;
5011
3.38k
    pass1_mnem = insn_name (current_templates->start);
5012
3.38k
    goto retry;
5013
3.11k
  }
5014
5015
      /* If a non-/only-64bit template (group) was found in pass 1, and if
5016
   _some_ template (group) was found in pass 2, squash pass 1's
5017
   error.  */
5018
33.9k
      if (pass1_err == unsupported_64bit)
5019
0
  pass1_mnem = NULL;
5020
5021
41.0k
  match_error:
5022
41.0k
      free (copy);
5023
5024
41.0k
      switch (pass1_mnem ? pass1_err : i.error)
5025
41.0k
  {
5026
0
  default:
5027
0
    abort ();
5028
6.07k
  case operand_size_mismatch:
5029
6.07k
    err_msg = _("operand size mismatch");
5030
6.07k
    break;
5031
346
  case operand_type_mismatch:
5032
346
    err_msg = _("operand type mismatch");
5033
346
    break;
5034
0
  case register_type_mismatch:
5035
0
    err_msg = _("register type mismatch");
5036
0
    break;
5037
29.0k
  case number_of_operands_mismatch:
5038
29.0k
    err_msg = _("number of operands mismatch");
5039
29.0k
    break;
5040
1.45k
  case invalid_instruction_suffix:
5041
1.45k
    err_msg = _("invalid instruction suffix");
5042
1.45k
    break;
5043
0
  case bad_imm4:
5044
0
    err_msg = _("constant doesn't fit in 4 bits");
5045
0
    break;
5046
0
  case unsupported_with_intel_mnemonic:
5047
0
    err_msg = _("unsupported with Intel mnemonic");
5048
0
    break;
5049
0
  case unsupported_syntax:
5050
0
    err_msg = _("unsupported syntax");
5051
0
    break;
5052
100
  case unsupported:
5053
100
    as_bad (_("unsupported instruction `%s'"),
5054
100
      pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
5055
100
    return;
5056
1.34k
  case unsupported_on_arch:
5057
1.34k
    as_bad (_("`%s' is not supported on `%s%s'"),
5058
1.34k
      pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
5059
1.34k
      cpu_arch_name ? cpu_arch_name : default_arch,
5060
1.34k
      cpu_sub_arch_name ? cpu_sub_arch_name : "");
5061
1.34k
    return;
5062
2.68k
  case unsupported_64bit:
5063
2.68k
    if (ISLOWER (mnem_suffix))
5064
1.27k
      {
5065
1.27k
        if (flag_code == CODE_64BIT)
5066
213
    as_bad (_("`%s%c' is not supported in 64-bit mode"),
5067
213
      pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
5068
213
      mnem_suffix);
5069
1.06k
        else
5070
1.06k
    as_bad (_("`%s%c' is only supported in 64-bit mode"),
5071
1.06k
      pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
5072
1.06k
      mnem_suffix);
5073
1.27k
      }
5074
1.40k
    else
5075
1.40k
      {
5076
1.40k
        if (flag_code == CODE_64BIT)
5077
298
    as_bad (_("`%s' is not supported in 64-bit mode"),
5078
298
      pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
5079
1.10k
        else
5080
1.10k
    as_bad (_("`%s' is only supported in 64-bit mode"),
5081
1.10k
      pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
5082
1.40k
      }
5083
2.68k
    return;
5084
0
  case invalid_sib_address:
5085
0
    err_msg = _("invalid SIB address");
5086
0
    break;
5087
0
  case invalid_vsib_address:
5088
0
    err_msg = _("invalid VSIB address");
5089
0
    break;
5090
0
  case invalid_vector_register_set:
5091
0
    err_msg = _("mask, index, and destination registers must be distinct");
5092
0
    break;
5093
0
  case invalid_tmm_register_set:
5094
0
    err_msg = _("all tmm registers must be distinct");
5095
0
    break;
5096
0
  case invalid_dest_and_src_register_set:
5097
0
    err_msg = _("destination and source registers must be distinct");
5098
0
    break;
5099
0
  case unsupported_vector_index_register:
5100
0
    err_msg = _("unsupported vector index register");
5101
0
    break;
5102
0
  case unsupported_broadcast:
5103
0
    err_msg = _("unsupported broadcast");
5104
0
    break;
5105
0
  case broadcast_needed:
5106
0
    err_msg = _("broadcast is needed for operand of such type");
5107
0
    break;
5108
0
  case unsupported_masking:
5109
0
    err_msg = _("unsupported masking");
5110
0
    break;
5111
0
  case mask_not_on_destination:
5112
0
    err_msg = _("mask not on destination operand");
5113
0
    break;
5114
0
  case no_default_mask:
5115
0
    err_msg = _("default mask isn't allowed");
5116
0
    break;
5117
0
  case unsupported_rc_sae:
5118
0
    err_msg = _("unsupported static rounding/sae");
5119
0
    break;
5120
0
  case invalid_register_operand:
5121
0
    err_msg = _("invalid register operand");
5122
0
    break;
5123
41.0k
  }
5124
36.9k
      as_bad (_("%s for `%s'"), err_msg,
5125
36.9k
        pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
5126
36.9k
      return;
5127
41.0k
    }
5128
5129
187k
  free (copy);
5130
5131
187k
  if (sse_check != check_none
5132
      /* The opcode space check isn't strictly needed; it's there only to
5133
   bypass the logic below when easily possible.  */
5134
187k
      && t->opcode_space >= SPACE_0F
5135
187k
      && t->opcode_space <= SPACE_0F3A
5136
187k
      && !i.tm.cpu_flags.bitfield.cpusse4a
5137
187k
      && !is_any_vex_encoding (t))
5138
0
    {
5139
0
      bool simd = false;
5140
5141
0
      for (j = 0; j < t->operands; ++j)
5142
0
  {
5143
0
    if (t->operand_types[j].bitfield.class == RegMMX)
5144
0
      break;
5145
0
    if (t->operand_types[j].bitfield.class == RegSIMD)
5146
0
      simd = true;
5147
0
  }
5148
5149
0
      if (j >= t->operands && simd)
5150
0
  (sse_check == check_warning
5151
0
   ? as_warn
5152
0
   : as_bad) (_("SSE instruction `%s' is used"), insn_name (&i.tm));
5153
0
    }
5154
5155
187k
  if (i.tm.opcode_modifier.fwait)
5156
1.77k
    if (!add_prefix (FWAIT_OPCODE))
5157
0
      return;
5158
5159
  /* Check if REP prefix is OK.  */
5160
187k
  if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
5161
0
    {
5162
0
      as_bad (_("invalid instruction `%s' after `%s'"),
5163
0
    insn_name (&i.tm), i.rep_prefix);
5164
0
      return;
5165
0
    }
5166
5167
  /* Check for lock without a lockable instruction.  Destination operand
5168
     must be memory unless it is xchg (0x86).  */
5169
187k
  if (i.prefix[LOCK_PREFIX])
5170
0
    {
5171
0
      if (i.tm.opcode_modifier.prefixok < PrefixLock
5172
0
    || i.mem_operands == 0
5173
0
    || (i.tm.base_opcode != 0x86
5174
0
        && !(i.flags[i.operands - 1] & Operand_Mem)))
5175
0
  {
5176
0
    as_bad (_("expecting lockable instruction after `lock'"));
5177
0
    return;
5178
0
  }
5179
5180
      /* Zap the redundant prefix from XCHG when optimizing.  */
5181
0
      if (i.tm.base_opcode == 0x86 && optimize && !i.no_optimize)
5182
0
  i.prefix[LOCK_PREFIX] = 0;
5183
0
    }
5184
5185
187k
  if (is_any_vex_encoding (&i.tm)
5186
187k
      || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
5187
187k
      || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX)
5188
2
    {
5189
      /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns.  */
5190
2
      if (i.prefix[DATA_PREFIX])
5191
0
  {
5192
0
    as_bad (_("data size prefix invalid with `%s'"), insn_name (&i.tm));
5193
0
    return;
5194
0
  }
5195
5196
      /* Don't allow e.g. KMOV in TLS code sequences.  */
5197
2
      for (j = i.imm_operands; j < i.operands; ++j)
5198
0
  switch (i.reloc[j])
5199
0
    {
5200
0
    case BFD_RELOC_386_TLS_GOTIE:
5201
0
    case BFD_RELOC_386_TLS_LE_32:
5202
0
    case BFD_RELOC_X86_64_GOTTPOFF:
5203
0
    case BFD_RELOC_X86_64_TLSLD:
5204
0
      as_bad (_("TLS relocation cannot be used with `%s'"), insn_name (&i.tm));
5205
0
      return;
5206
0
    default:
5207
0
      break;
5208
0
    }
5209
2
    }
5210
5211
  /* Check if HLE prefix is OK.  */
5212
187k
  if (i.hle_prefix && !check_hle ())
5213
0
    return;
5214
5215
  /* Check BND prefix.  */
5216
187k
  if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
5217
1
    as_bad (_("expecting valid branch instruction after `bnd'"));
5218
5219
  /* Check NOTRACK prefix.  */
5220
187k
  if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
5221
0
    as_bad (_("expecting indirect branch instruction after `notrack'"));
5222
5223
187k
  if (i.tm.cpu_flags.bitfield.cpumpx)
5224
99
    {
5225
99
      if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5226
0
  as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5227
99
      else if (flag_code != CODE_16BIT
5228
99
         ? i.prefix[ADDR_PREFIX]
5229
99
         : i.mem_operands && !i.prefix[ADDR_PREFIX])
5230
0
  as_bad (_("16-bit address isn't allowed in MPX instructions"));
5231
99
    }
5232
5233
  /* Insert BND prefix.  */
5234
187k
  if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5235
0
    {
5236
0
      if (!i.prefix[BND_PREFIX])
5237
0
  add_prefix (BND_PREFIX_OPCODE);
5238
0
      else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5239
0
  {
5240
0
    as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5241
0
    i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5242
0
  }
5243
0
    }
5244
5245
  /* Check string instruction segment overrides.  */
5246
187k
  if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
5247
839
    {
5248
839
      gas_assert (i.mem_operands);
5249
839
      if (!check_string ())
5250
0
  return;
5251
839
      i.disp_operands = 0;
5252
839
    }
5253
5254
  /* The memory operand of (%dx) should be only used with input/output
5255
     instructions (base opcodes: 0x6c, 0x6e, 0xec, 0xee).  */
5256
187k
  if (i.input_output_operand
5257
187k
      && ((i.tm.base_opcode | 0x82) != 0xee
5258
0
    || i.tm.opcode_space != SPACE_BASE))
5259
0
    {
5260
0
      as_bad (_("input/output port address isn't allowed with `%s'"),
5261
0
        insn_name (&i.tm));
5262
0
      return;
5263
0
    }
5264
5265
187k
  if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5266
0
    optimize_encoding ();
5267
5268
187k
  if (use_unaligned_vector_move)
5269
0
    encode_with_unaligned_vector_move ();
5270
5271
187k
  if (!process_suffix ())
5272
0
    return;
5273
5274
  /* Check if IP-relative addressing requirements can be satisfied.  */
5275
187k
  if (i.tm.cpu_flags.bitfield.cpuprefetchi
5276
187k
      && !(i.base_reg && i.base_reg->reg_num == RegIP))
5277
0
    as_warn (_("'%s' only supports RIP-relative address"), insn_name (&i.tm));
5278
5279
  /* Update operand types and check extended states.  */
5280
342k
  for (j = 0; j < i.operands; j++)
5281
155k
    {
5282
155k
      i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
5283
155k
      switch (i.tm.operand_types[j].bitfield.class)
5284
155k
  {
5285
155k
  default:
5286
155k
    break;
5287
155k
  case RegMMX:
5288
0
    i.xstate |= xstate_mmx;
5289
0
    break;
5290
0
  case RegMask:
5291
0
    i.xstate |= xstate_mask;
5292
0
    break;
5293
0
  case RegSIMD:
5294
0
    if (i.tm.operand_types[j].bitfield.tmmword)
5295
0
      i.xstate |= xstate_tmm;
5296
0
    else if (i.tm.operand_types[j].bitfield.zmmword)
5297
0
      i.xstate |= xstate_zmm;
5298
0
    else if (i.tm.operand_types[j].bitfield.ymmword)
5299
0
      i.xstate |= xstate_ymm;
5300
0
    else if (i.tm.operand_types[j].bitfield.xmmword)
5301
0
      i.xstate |= xstate_xmm;
5302
0
    break;
5303
155k
  }
5304
155k
    }
5305
5306
  /* Make still unresolved immediate matches conform to size of immediate
5307
     given in i.suffix.  */
5308
187k
  if (!finalize_imm ())
5309
0
    return;
5310
5311
187k
  if (i.types[0].bitfield.imm1)
5312
12
    i.imm_operands = 0; /* kludge for shift insns.  */
5313
5314
  /* For insns with operands there are more diddles to do to the opcode.  */
5315
187k
  if (i.operands)
5316
82.6k
    {
5317
82.6k
      if (!process_operands ())
5318
0
  return;
5319
82.6k
    }
5320
104k
  else if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
5321
4
    {
5322
      /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc.  */
5323
4
      as_warn (_("translating to `%sp'"), insn_name (&i.tm));
5324
4
    }
5325
5326
187k
  if (is_any_vex_encoding (&i.tm))
5327
2
    {
5328
2
      if (!cpu_arch_flags.bitfield.cpui286)
5329
0
  {
5330
0
    as_bad (_("instruction `%s' isn't supported outside of protected mode."),
5331
0
      insn_name (&i.tm));
5332
0
    return;
5333
0
  }
5334
5335
      /* Check for explicit REX prefix.  */
5336
2
      if (i.prefix[REX_PREFIX] || i.rex_encoding)
5337
0
  {
5338
0
    as_bad (_("REX prefix invalid with `%s'"), insn_name (&i.tm));
5339
0
    return;
5340
0
  }
5341
5342
2
      if (i.tm.opcode_modifier.vex)
5343
2
  build_vex_prefix (t);
5344
0
      else
5345
0
  build_evex_prefix ();
5346
5347
      /* The individual REX.RXBW bits got consumed.  */
5348
2
      i.rex &= REX_OPCODE;
5349
2
    }
5350
5351
  /* Handle conversion of 'int $3' --> special int3 insn.  */
5352
187k
  if (i.tm.mnem_off == MN_int
5353
187k
      && i.op[0].imms->X_add_number == 3)
5354
0
    {
5355
0
      i.tm.base_opcode = INT3_OPCODE;
5356
0
      i.imm_operands = 0;
5357
0
    }
5358
5359
187k
  if ((i.tm.opcode_modifier.jump == JUMP
5360
187k
       || i.tm.opcode_modifier.jump == JUMP_BYTE
5361
187k
       || i.tm.opcode_modifier.jump == JUMP_DWORD)
5362
187k
      && i.op[0].disps->X_op == O_constant)
5363
3.41k
    {
5364
      /* Convert "jmp constant" (and "call constant") to a jump (call) to
5365
   the absolute address given by the constant.  Since ix86 jumps and
5366
   calls are pc relative, we need to generate a reloc.  */
5367
3.41k
      i.op[0].disps->X_add_symbol = &abs_symbol;
5368
3.41k
      i.op[0].disps->X_op = O_symbol;
5369
3.41k
    }
5370
5371
  /* For 8 bit registers we need an empty rex prefix.  Also if the
5372
     instruction already has a prefix, we need to convert old
5373
     registers to new ones.  */
5374
5375
187k
  if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
5376
187k
       && (i.op[0].regs->reg_flags & RegRex64) != 0)
5377
187k
      || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
5378
187k
    && (i.op[1].regs->reg_flags & RegRex64) != 0)
5379
187k
      || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5380
187k
     || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
5381
187k
    && i.rex != 0))
5382
0
    {
5383
0
      int x;
5384
5385
0
      i.rex |= REX_OPCODE;
5386
0
      for (x = 0; x < 2; x++)
5387
0
  {
5388
    /* Look for 8 bit operand that uses old registers.  */
5389
0
    if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
5390
0
        && (i.op[x].regs->reg_flags & RegRex64) == 0)
5391
0
      {
5392
0
        gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5393
        /* In case it is "hi" register, give up.  */
5394
0
        if (i.op[x].regs->reg_num > 3)
5395
0
    as_bad (_("can't encode register '%s%s' in an "
5396
0
        "instruction requiring REX prefix."),
5397
0
      register_prefix, i.op[x].regs->reg_name);
5398
5399
        /* Otherwise it is equivalent to the extended register.
5400
     Since the encoding doesn't change this is merely
5401
     cosmetic cleanup for debug output.  */
5402
5403
0
        i.op[x].regs = i.op[x].regs + 8;
5404
0
      }
5405
0
  }
5406
0
    }
5407
5408
187k
  if (i.rex == 0 && i.rex_encoding)
5409
0
    {
5410
      /* Check if we can add a REX_OPCODE byte.  Look for 8 bit operand
5411
   that uses legacy register.  If it is "hi" register, don't add
5412
   the REX_OPCODE byte.  */
5413
0
      int x;
5414
0
      for (x = 0; x < 2; x++)
5415
0
  if (i.types[x].bitfield.class == Reg
5416
0
      && i.types[x].bitfield.byte
5417
0
      && (i.op[x].regs->reg_flags & RegRex64) == 0
5418
0
      && i.op[x].regs->reg_num > 3)
5419
0
    {
5420
0
      gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5421
0
      i.rex_encoding = false;
5422
0
      break;
5423
0
    }
5424
5425
0
      if (i.rex_encoding)
5426
0
  i.rex = REX_OPCODE;
5427
0
    }
5428
5429
187k
  if (i.rex != 0)
5430
154
    add_prefix (REX_OPCODE | i.rex);
5431
5432
187k
  insert_lfence_before ();
5433
5434
  /* We are ready to output the insn.  */
5435
187k
  output_insn ();
5436
5437
187k
  insert_lfence_after ();
5438
5439
187k
  last_insn.seg = now_seg;
5440
5441
187k
  if (i.tm.opcode_modifier.isprefix)
5442
9.90k
    {
5443
9.90k
      last_insn.kind = last_insn_prefix;
5444
9.90k
      last_insn.name = insn_name (&i.tm);
5445
9.90k
      last_insn.file = as_where (&last_insn.line);
5446
9.90k
    }
5447
177k
  else
5448
177k
    last_insn.kind = last_insn_other;
5449
187k
}
5450
5451
/* The Q suffix is generally valid only in 64-bit mode, with very few
5452
   exceptions: fild, fistp, fisttp, and cmpxchg8b.  Note that for fild
5453
   and fisttp only one of their two templates is matched below: That's
5454
   sufficient since other relevant attributes are the same between both
5455
   respective templates.  */
5456
static INLINE bool q_suffix_allowed(const insn_template *t)
5457
1.13k
{
5458
1.13k
  return flag_code == CODE_64BIT
5459
1.13k
   || (t->opcode_space == SPACE_BASE
5460
1.07k
       && t->base_opcode == 0xdf
5461
1.07k
       && (t->extension_opcode & 1)) /* fild / fistp / fisttp */
5462
1.13k
   || t->mnem_off == MN_cmpxchg8b;
5463
1.13k
}
5464
5465
static const char *
5466
parse_insn (const char *line, char *mnemonic, bool prefix_only)
5467
969k
{
5468
969k
  const char *l = line, *token_start = l;
5469
969k
  char *mnem_p;
5470
969k
  bool pass1 = !current_templates;
5471
969k
  int supported;
5472
969k
  const insn_template *t;
5473
969k
  char *dot_p = NULL;
5474
5475
972k
  while (1)
5476
972k
    {
5477
972k
      mnem_p = mnemonic;
5478
      /* Pseudo-prefixes start with an opening figure brace.  */
5479
972k
      if ((*mnem_p = *l) == '{')
5480
3.77k
  {
5481
3.77k
    ++mnem_p;
5482
3.77k
    ++l;
5483
3.77k
  }
5484
2.95M
      while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5485
1.98M
  {
5486
1.98M
    if (*mnem_p == '.')
5487
35.0k
      dot_p = mnem_p;
5488
1.98M
    mnem_p++;
5489
1.98M
    if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
5490
3.14k
      {
5491
3.14k
      too_long:
5492
3.14k
        as_bad (_("no such instruction: `%s'"), token_start);
5493
3.14k
        return NULL;
5494
3.14k
      }
5495
1.98M
    l++;
5496
1.98M
  }
5497
      /* Pseudo-prefixes end with a closing figure brace.  */
5498
969k
      if (*mnemonic == '{' && *l == '}')
5499
1.21k
  {
5500
1.21k
    *mnem_p++ = *l++;
5501
1.21k
    if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
5502
0
      goto too_long;
5503
1.21k
    *mnem_p = '\0';
5504
5505
    /* Point l at the closing brace if there's no other separator.  */
5506
1.21k
    if (*l != END_OF_INSN && !is_space_char (*l)
5507
1.21k
        && *l != PREFIX_SEPARATOR)
5508
106
      --l;
5509
1.21k
  }
5510
968k
      else if (!is_space_char (*l)
5511
968k
         && *l != END_OF_INSN
5512
968k
         && (intel_syntax
5513
461k
       || (*l != PREFIX_SEPARATOR && *l != ',')))
5514
453k
  {
5515
453k
    if (prefix_only)
5516
39
      break;
5517
453k
    as_bad (_("invalid character %s in mnemonic"),
5518
453k
      output_invalid (*l));
5519
453k
    return NULL;
5520
453k
  }
5521
515k
      if (token_start == l)
5522
1.55k
  {
5523
1.55k
    if (!intel_syntax && *l == PREFIX_SEPARATOR)
5524
19
      as_bad (_("expecting prefix; got nothing"));
5525
1.53k
    else
5526
1.53k
      as_bad (_("expecting mnemonic; got nothing"));
5527
1.55k
    return NULL;
5528
1.55k
  }
5529
5530
      /* Look up instruction (or prefix) via hash table.  */
5531
514k
      current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
5532
5533
514k
      if (*l != END_OF_INSN
5534
514k
    && (!is_space_char (*l) || l[1] != END_OF_INSN)
5535
514k
    && current_templates
5536
514k
    && current_templates->start->opcode_modifier.isprefix)
5537
3.28k
  {
5538
3.28k
    if (!cpu_flags_check_cpu64 (current_templates->start->cpu_flags))
5539
15
      {
5540
15
        as_bad ((flag_code != CODE_64BIT
5541
15
           ? _("`%s' is only supported in 64-bit mode")
5542
15
           : _("`%s' is not supported in 64-bit mode")),
5543
15
          insn_name (current_templates->start));
5544
15
        return NULL;
5545
15
      }
5546
    /* If we are in 16-bit mode, do not allow addr16 or data16.
5547
       Similarly, in 32-bit mode, do not allow addr32 or data32.  */
5548
3.26k
    if ((current_templates->start->opcode_modifier.size == SIZE16
5549
3.26k
         || current_templates->start->opcode_modifier.size == SIZE32)
5550
3.26k
        && flag_code != CODE_64BIT
5551
3.26k
        && ((current_templates->start->opcode_modifier.size == SIZE32)
5552
0
      ^ (flag_code == CODE_16BIT)))
5553
0
      {
5554
0
        as_bad (_("redundant %s prefix"),
5555
0
          insn_name (current_templates->start));
5556
0
        return NULL;
5557
0
      }
5558
5559
3.26k
    if (current_templates->start->base_opcode == PSEUDO_PREFIX)
5560
1.19k
      {
5561
        /* Handle pseudo prefixes.  */
5562
1.19k
        switch (current_templates->start->extension_opcode)
5563
1.19k
    {
5564
0
    case Prefix_Disp8:
5565
      /* {disp8} */
5566
0
      i.disp_encoding = disp_encoding_8bit;
5567
0
      break;
5568
483
    case Prefix_Disp16:
5569
      /* {disp16} */
5570
483
      i.disp_encoding = disp_encoding_16bit;
5571
483
      break;
5572
472
    case Prefix_Disp32:
5573
      /* {disp32} */
5574
472
      i.disp_encoding = disp_encoding_32bit;
5575
472
      break;
5576
3
    case Prefix_Load:
5577
      /* {load} */
5578
3
      i.dir_encoding = dir_encoding_load;
5579
3
      break;
5580
205
    case Prefix_Store:
5581
      /* {store} */
5582
205
      i.dir_encoding = dir_encoding_store;
5583
205
      break;
5584
0
    case Prefix_VEX:
5585
      /* {vex} */
5586
0
      i.vec_encoding = vex_encoding_vex;
5587
0
      break;
5588
34
    case Prefix_VEX3:
5589
      /* {vex3} */
5590
34
      i.vec_encoding = vex_encoding_vex3;
5591
34
      break;
5592
0
    case Prefix_EVEX:
5593
      /* {evex} */
5594
0
      i.vec_encoding = vex_encoding_evex;
5595
0
      break;
5596
2
    case Prefix_REX:
5597
      /* {rex} */
5598
2
      i.rex_encoding = true;
5599
2
      break;
5600
0
    case Prefix_NoOptimize:
5601
      /* {nooptimize} */
5602
0
      i.no_optimize = true;
5603
0
      break;
5604
0
    default:
5605
0
      abort ();
5606
1.19k
    }
5607
1.19k
      }
5608
2.06k
    else
5609
2.06k
      {
5610
        /* Add prefix, checking for repeated prefixes.  */
5611
2.06k
        switch (add_prefix (current_templates->start->base_opcode))
5612
2.06k
    {
5613
64
    case PREFIX_EXIST:
5614
64
      return NULL;
5615
17
    case PREFIX_DS:
5616
17
      if (current_templates->start->cpu_flags.bitfield.cpuibt)
5617
0
        i.notrack_prefix = insn_name (current_templates->start);
5618
17
      break;
5619
571
    case PREFIX_REP:
5620
571
      if (current_templates->start->cpu_flags.bitfield.cpuhle)
5621
0
        i.hle_prefix = insn_name (current_templates->start);
5622
571
      else if (current_templates->start->cpu_flags.bitfield.cpumpx)
5623
280
        i.bnd_prefix = insn_name (current_templates->start);
5624
291
      else
5625
291
        i.rep_prefix = insn_name (current_templates->start);
5626
571
      break;
5627
1.41k
    default:
5628
1.41k
      break;
5629
2.06k
    }
5630
2.06k
      }
5631
    /* Skip past PREFIX_SEPARATOR and reset token_start.  */
5632
3.20k
    token_start = ++l;
5633
3.20k
  }
5634
510k
      else
5635
510k
  break;
5636
514k
    }
5637
5638
511k
  if (prefix_only)
5639
40
    return token_start;
5640
5641
510k
  if (!current_templates)
5642
250k
    {
5643
      /* Deprecated functionality (new code should use pseudo-prefixes instead):
5644
   Check if we should swap operand or force 32bit displacement in
5645
   encoding.  */
5646
250k
      if (mnem_p - 2 == dot_p && dot_p[1] == 's')
5647
1.13k
  i.dir_encoding = dir_encoding_swap;
5648
249k
      else if (mnem_p - 3 == dot_p
5649
249k
         && dot_p[1] == 'd'
5650
249k
         && dot_p[2] == '8')
5651
0
  i.disp_encoding = disp_encoding_8bit;
5652
249k
      else if (mnem_p - 4 == dot_p
5653
249k
         && dot_p[1] == 'd'
5654
249k
         && dot_p[2] == '3'
5655
249k
         && dot_p[3] == '2')
5656
0
  i.disp_encoding = disp_encoding_32bit;
5657
249k
      else
5658
249k
  goto check_suffix;
5659
1.13k
      mnem_p = dot_p;
5660
1.13k
      *dot_p = '\0';
5661
1.13k
      current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
5662
1.13k
    }
5663
5664
261k
  if (!current_templates || !pass1)
5665
4.49k
    {
5666
4.49k
      current_templates = NULL;
5667
5668
254k
    check_suffix:
5669
254k
      if (mnem_p > mnemonic)
5670
254k
  {
5671
    /* See if we can get a match by trimming off a suffix.  */
5672
254k
    switch (mnem_p[-1])
5673
254k
      {
5674
5.52k
      case WORD_MNEM_SUFFIX:
5675
5.52k
        if (intel_syntax && (intel_float_operand (mnemonic) & 2))
5676
0
    i.suffix = SHORT_MNEM_SUFFIX;
5677
5.52k
        else
5678
    /* Fall through.  */
5679
18.3k
        case BYTE_MNEM_SUFFIX:
5680
20.6k
        case QWORD_MNEM_SUFFIX:
5681
20.6k
    i.suffix = mnem_p[-1];
5682
20.6k
        mnem_p[-1] = '\0';
5683
20.6k
        current_templates
5684
20.6k
    = (const templates *) str_hash_find (op_hash, mnemonic);
5685
20.6k
        break;
5686
42.8k
      case SHORT_MNEM_SUFFIX:
5687
51.3k
      case LONG_MNEM_SUFFIX:
5688
51.3k
        if (!intel_syntax)
5689
51.3k
    {
5690
51.3k
      i.suffix = mnem_p[-1];
5691
51.3k
      mnem_p[-1] = '\0';
5692
51.3k
      current_templates
5693
51.3k
        = (const templates *) str_hash_find (op_hash, mnemonic);
5694
51.3k
    }
5695
51.3k
        break;
5696
5697
        /* Intel Syntax.  */
5698
7.99k
      case 'd':
5699
7.99k
        if (intel_syntax)
5700
0
    {
5701
0
      if (intel_float_operand (mnemonic) == 1)
5702
0
        i.suffix = SHORT_MNEM_SUFFIX;
5703
0
      else
5704
0
        i.suffix = LONG_MNEM_SUFFIX;
5705
0
      mnem_p[-1] = '\0';
5706
0
      current_templates
5707
0
        = (const templates *) str_hash_find (op_hash, mnemonic);
5708
0
    }
5709
        /* For compatibility reasons accept MOVSD and CMPSD without
5710
           operands even in AT&T mode.  */
5711
7.99k
        else if (*l == END_OF_INSN
5712
7.99k
           || (is_space_char (*l) && l[1] == END_OF_INSN))
5713
6.40k
    {
5714
6.40k
      mnem_p[-1] = '\0';
5715
6.40k
      current_templates
5716
6.40k
        = (const templates *) str_hash_find (op_hash, mnemonic);
5717
6.40k
      if (current_templates != NULL
5718
          /* MOVS or CMPS */
5719
6.40k
          && (current_templates->start->base_opcode | 2) == 0xa6
5720
6.40k
          && current_templates->start->opcode_space
5721
0
       == SPACE_BASE
5722
6.40k
          && mnem_p[-2] == 's')
5723
0
        {
5724
0
          as_warn (_("found `%sd'; assuming `%sl' was meant"),
5725
0
             mnemonic, mnemonic);
5726
0
          i.suffix = LONG_MNEM_SUFFIX;
5727
0
        }
5728
6.40k
      else
5729
6.40k
        {
5730
6.40k
          current_templates = NULL;
5731
6.40k
          mnem_p[-1] = 'd';
5732
6.40k
        }
5733
6.40k
    }
5734
7.99k
        break;
5735
254k
      }
5736
254k
  }
5737
5738
254k
      if (!current_templates)
5739
247k
  {
5740
247k
    if (pass1)
5741
244k
      as_bad (_("no such instruction: `%s'"), token_start);
5742
247k
    return NULL;
5743
247k
  }
5744
254k
    }
5745
5746
263k
  if (current_templates->start->opcode_modifier.jump == JUMP
5747
263k
      || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
5748
7.66k
    {
5749
      /* Check for a branch hint.  We allow ",pt" and ",pn" for
5750
   predict taken and predict not taken respectively.
5751
   I'm not sure that branch hints actually do anything on loop
5752
   and jcxz insns (JumpByte) for current Pentium4 chips.  They
5753
   may work in the future and it doesn't hurt to accept them
5754
   now.  */
5755
7.66k
      if (l[0] == ',' && l[1] == 'p')
5756
0
  {
5757
0
    if (l[2] == 't')
5758
0
      {
5759
0
        if (!add_prefix (DS_PREFIX_OPCODE))
5760
0
    return NULL;
5761
0
        l += 3;
5762
0
      }
5763
0
    else if (l[2] == 'n')
5764
0
      {
5765
0
        if (!add_prefix (CS_PREFIX_OPCODE))
5766
0
    return NULL;
5767
0
        l += 3;
5768
0
      }
5769
0
  }
5770
7.66k
    }
5771
  /* Any other comma loses.  */
5772
263k
  if (*l == ',')
5773
110
    {
5774
110
      as_bad (_("invalid character %s in mnemonic"),
5775
110
        output_invalid (*l));
5776
110
      return NULL;
5777
110
    }
5778
5779
  /* Check if instruction is supported on specified architecture.  */
5780
263k
  supported = 0;
5781
277k
  for (t = current_templates->start; t < current_templates->end; ++t)
5782
273k
    {
5783
273k
      supported |= cpu_flags_match (t);
5784
5785
273k
      if (i.suffix == QWORD_MNEM_SUFFIX && !q_suffix_allowed (t))
5786
1.07k
  supported &= ~CPU_FLAGS_64BIT_MATCH;
5787
5788
273k
      if (supported == CPU_FLAGS_PERFECT_MATCH)
5789
259k
  return l;
5790
273k
    }
5791
5792
4.02k
  if (pass1)
5793
4.02k
    {
5794
4.02k
      if (supported & CPU_FLAGS_64BIT_MATCH)
5795
1.34k
        i.error = unsupported_on_arch;
5796
2.68k
      else
5797
2.68k
        i.error = unsupported_64bit;
5798
4.02k
    }
5799
5800
4.02k
  return NULL;
5801
263k
}
5802
5803
static char *
5804
parse_operands (char *l, const char *mnemonic)
5805
259k
{
5806
259k
  char *token_start;
5807
5808
  /* 1 if operand is pending after ','.  */
5809
259k
  unsigned int expecting_operand = 0;
5810
5811
469k
  while (*l != END_OF_INSN)
5812
245k
    {
5813
      /* Non-zero if operand parens not balanced.  */
5814
245k
      unsigned int paren_not_balanced = 0;
5815
      /* True if inside double quotes.  */
5816
245k
      bool in_quotes = false;
5817
5818
      /* Skip optional white space before operand.  */
5819
245k
      if (is_space_char (*l))
5820
151k
  ++l;
5821
245k
      if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
5822
3.19k
  {
5823
3.19k
    as_bad (_("invalid character %s before operand %d"),
5824
3.19k
      output_invalid (*l),
5825
3.19k
      i.operands + 1);
5826
3.19k
    return NULL;
5827
3.19k
  }
5828
242k
      token_start = l;  /* After white space.  */
5829
1.49M
      while (in_quotes || paren_not_balanced || *l != ',')
5830
1.38M
  {
5831
1.38M
    if (*l == END_OF_INSN)
5832
123k
      {
5833
123k
        if (in_quotes)
5834
2.44k
    {
5835
2.44k
      as_bad (_("unbalanced double quotes in operand %d."),
5836
2.44k
        i.operands + 1);
5837
2.44k
      return NULL;
5838
2.44k
    }
5839
121k
        if (paren_not_balanced)
5840
685
    {
5841
685
      know (!intel_syntax);
5842
685
      as_bad (_("unbalanced parenthesis in operand %d."),
5843
685
        i.operands + 1);
5844
685
      return NULL;
5845
685
    }
5846
120k
        else
5847
120k
    break; /* we are done */
5848
121k
      }
5849
1.26M
    else if (*l == '\\' && l[1] == '"')
5850
0
      ++l;
5851
1.26M
    else if (*l == '"')
5852
8.53k
      in_quotes = !in_quotes;
5853
1.25M
    else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
5854
11.7k
      {
5855
11.7k
        as_bad (_("invalid character %s in operand %d"),
5856
11.7k
          output_invalid (*l),
5857
11.7k
          i.operands + 1);
5858
11.7k
        return NULL;
5859
11.7k
      }
5860
1.24M
    if (!intel_syntax && !in_quotes)
5861
1.02M
      {
5862
1.02M
        if (*l == '(')
5863
2.02k
    ++paren_not_balanced;
5864
1.02M
        if (*l == ')')
5865
1.33k
    --paren_not_balanced;
5866
1.02M
      }
5867
1.24M
    l++;
5868
1.24M
  }
5869
227k
      if (l != token_start)
5870
221k
  {     /* Yes, we've read in another operand.  */
5871
221k
    unsigned int operand_ok;
5872
221k
    this_operand = i.operands++;
5873
221k
    if (i.operands > MAX_OPERANDS)
5874
0
      {
5875
0
        as_bad (_("spurious operands; (%d operands/instruction max)"),
5876
0
          MAX_OPERANDS);
5877
0
        return NULL;
5878
0
      }
5879
221k
    i.types[this_operand].bitfield.unspecified = 1;
5880
    /* Now parse operand adding info to 'i' as we go along.  */
5881
221k
    END_STRING_AND_SAVE (l);
5882
5883
221k
    if (i.mem_operands > 1)
5884
511
      {
5885
511
        as_bad (_("too many memory references for `%s'"),
5886
511
          mnemonic);
5887
511
        return 0;
5888
511
      }
5889
5890
220k
    if (intel_syntax)
5891
0
      operand_ok =
5892
0
        i386_intel_operand (token_start,
5893
0
          intel_float_operand (mnemonic));
5894
220k
    else
5895
220k
      operand_ok = i386_att_operand (token_start);
5896
5897
220k
    RESTORE_END_STRING (l);
5898
220k
    if (!operand_ok)
5899
15.2k
      return NULL;
5900
220k
  }
5901
6.47k
      else
5902
6.47k
  {
5903
6.47k
    if (expecting_operand)
5904
95
      {
5905
1.29k
      expecting_operand_after_comma:
5906
1.29k
        as_bad (_("expecting operand after ','; got nothing"));
5907
1.29k
        return NULL;
5908
95
      }
5909
6.37k
    if (*l == ',')
5910
67
      {
5911
67
        as_bad (_("expecting operand before ','; got nothing"));
5912
67
        return NULL;
5913
67
      }
5914
6.37k
  }
5915
5916
      /* Now *l must be either ',' or END_OF_INSN.  */
5917
211k
      if (*l == ',')
5918
95.4k
  {
5919
95.4k
    if (*++l == END_OF_INSN)
5920
1.20k
      {
5921
        /* Just skip it, if it's \n complain.  */
5922
1.20k
        goto expecting_operand_after_comma;
5923
1.20k
      }
5924
94.2k
    expecting_operand = 1;
5925
94.2k
  }
5926
211k
    }
5927
224k
  return l;
5928
259k
}
5929
5930
static void
5931
swap_2_operands (unsigned int xchg1, unsigned int xchg2)
5932
0
{
5933
0
  union i386_op temp_op;
5934
0
  i386_operand_type temp_type;
5935
0
  unsigned int temp_flags;
5936
0
  enum bfd_reloc_code_real temp_reloc;
5937
5938
0
  temp_type = i.types[xchg2];
5939
0
  i.types[xchg2] = i.types[xchg1];
5940
0
  i.types[xchg1] = temp_type;
5941
5942
0
  temp_flags = i.flags[xchg2];
5943
0
  i.flags[xchg2] = i.flags[xchg1];
5944
0
  i.flags[xchg1] = temp_flags;
5945
5946
0
  temp_op = i.op[xchg2];
5947
0
  i.op[xchg2] = i.op[xchg1];
5948
0
  i.op[xchg1] = temp_op;
5949
5950
0
  temp_reloc = i.reloc[xchg2];
5951
0
  i.reloc[xchg2] = i.reloc[xchg1];
5952
0
  i.reloc[xchg1] = temp_reloc;
5953
5954
0
  temp_flags = i.imm_bits[xchg2];
5955
0
  i.imm_bits[xchg2] = i.imm_bits[xchg1];
5956
0
  i.imm_bits[xchg1] = temp_flags;
5957
5958
0
  if (i.mask.reg)
5959
0
    {
5960
0
      if (i.mask.operand == xchg1)
5961
0
  i.mask.operand = xchg2;
5962
0
      else if (i.mask.operand == xchg2)
5963
0
  i.mask.operand = xchg1;
5964
0
    }
5965
0
  if (i.broadcast.type || i.broadcast.bytes)
5966
0
    {
5967
0
      if (i.broadcast.operand == xchg1)
5968
0
  i.broadcast.operand = xchg2;
5969
0
      else if (i.broadcast.operand == xchg2)
5970
0
  i.broadcast.operand = xchg1;
5971
0
    }
5972
0
}
5973
5974
static void
5975
swap_operands (void)
5976
0
{
5977
0
  switch (i.operands)
5978
0
    {
5979
0
    case 5:
5980
0
    case 4:
5981
0
      swap_2_operands (1, i.operands - 2);
5982
      /* Fall through.  */
5983
0
    case 3:
5984
0
    case 2:
5985
0
      swap_2_operands (0, i.operands - 1);
5986
0
      break;
5987
0
    default:
5988
0
      abort ();
5989
0
    }
5990
5991
0
  if (i.mem_operands == 2)
5992
0
    {
5993
0
      const reg_entry *temp_seg;
5994
0
      temp_seg = i.seg[0];
5995
0
      i.seg[0] = i.seg[1];
5996
0
      i.seg[1] = temp_seg;
5997
0
    }
5998
0
}
5999
6000
/* Try to ensure constant immediates are represented in the smallest
6001
   opcode possible.  */
6002
static void
6003
optimize_imm (void)
6004
80.0k
{
6005
80.0k
  char guess_suffix = 0;
6006
80.0k
  int op;
6007
6008
80.0k
  if (i.suffix)
6009
1.49k
    guess_suffix = i.suffix;
6010
78.5k
  else if (i.reg_operands)
6011
1
    {
6012
      /* Figure out a suffix from the last register operand specified.
6013
   We can't do this properly yet, i.e. excluding special register
6014
   instances, but the following works for instructions with
6015
   immediates.  In any case, we can't set i.suffix yet.  */
6016
1
      for (op = i.operands; --op >= 0;)
6017
1
  if (i.types[op].bitfield.class != Reg)
6018
0
    continue;
6019
1
  else if (i.types[op].bitfield.byte)
6020
0
    {
6021
0
      guess_suffix = BYTE_MNEM_SUFFIX;
6022
0
      break;
6023
0
    }
6024
1
  else if (i.types[op].bitfield.word)
6025
1
    {
6026
1
      guess_suffix = WORD_MNEM_SUFFIX;
6027
1
      break;
6028
1
    }
6029
0
  else if (i.types[op].bitfield.dword)
6030
0
    {
6031
0
      guess_suffix = LONG_MNEM_SUFFIX;
6032
0
      break;
6033
0
    }
6034
0
  else if (i.types[op].bitfield.qword)
6035
0
    {
6036
0
      guess_suffix = QWORD_MNEM_SUFFIX;
6037
0
      break;
6038
0
    }
6039
1
    }
6040
78.5k
  else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
6041
46.4k
    guess_suffix = WORD_MNEM_SUFFIX;
6042
32.0k
  else if (flag_code != CODE_64BIT || !(i.prefix[REX_PREFIX] & REX_W))
6043
32.0k
    guess_suffix = LONG_MNEM_SUFFIX;
6044
6045
237k
  for (op = i.operands; --op >= 0;)
6046
157k
    if (operand_type_check (i.types[op], imm))
6047
80.0k
      {
6048
80.0k
  switch (i.op[op].imms->X_op)
6049
80.0k
    {
6050
66.1k
    case O_constant:
6051
      /* If a suffix is given, this operand may be shortened.  */
6052
66.1k
      switch (guess_suffix)
6053
66.1k
        {
6054
27.8k
        case LONG_MNEM_SUFFIX:
6055
27.8k
    i.types[op].bitfield.imm32 = 1;
6056
27.8k
    i.types[op].bitfield.imm64 = 1;
6057
27.8k
    break;
6058
37.1k
        case WORD_MNEM_SUFFIX:
6059
37.1k
    i.types[op].bitfield.imm16 = 1;
6060
37.1k
    i.types[op].bitfield.imm32 = 1;
6061
37.1k
    i.types[op].bitfield.imm32s = 1;
6062
37.1k
    i.types[op].bitfield.imm64 = 1;
6063
37.1k
    break;
6064
1.17k
        case BYTE_MNEM_SUFFIX:
6065
1.17k
    i.types[op].bitfield.imm8 = 1;
6066
1.17k
    i.types[op].bitfield.imm8s = 1;
6067
1.17k
    i.types[op].bitfield.imm16 = 1;
6068
1.17k
    i.types[op].bitfield.imm32 = 1;
6069
1.17k
    i.types[op].bitfield.imm32s = 1;
6070
1.17k
    i.types[op].bitfield.imm64 = 1;
6071
1.17k
    break;
6072
66.1k
        }
6073
6074
      /* If this operand is at most 16 bits, convert it
6075
         to a signed 16 bit number before trying to see
6076
         whether it will fit in an even smaller size.
6077
         This allows a 16-bit operand such as $0xffe0 to
6078
         be recognised as within Imm8S range.  */
6079
66.1k
      if ((i.types[op].bitfield.imm16)
6080
66.1k
    && fits_in_unsigned_word (i.op[op].imms->X_add_number))
6081
36.6k
        {
6082
36.6k
    i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
6083
36.6k
            ^ 0x8000) - 0x8000);
6084
36.6k
        }
6085
66.1k
#ifdef BFD64
6086
      /* Store 32-bit immediate in 64-bit for 64-bit BFD.  */
6087
66.1k
      if ((i.types[op].bitfield.imm32)
6088
66.1k
    && fits_in_unsigned_long (i.op[op].imms->X_add_number))
6089
63.8k
        {
6090
63.8k
    i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
6091
63.8k
            ^ ((offsetT) 1 << 31))
6092
63.8k
                 - ((offsetT) 1 << 31));
6093
63.8k
        }
6094
66.1k
#endif
6095
66.1k
      i.types[op]
6096
66.1k
        = operand_type_or (i.types[op],
6097
66.1k
         smallest_imm_type (i.op[op].imms->X_add_number));
6098
6099
      /* We must avoid matching of Imm32 templates when 64bit
6100
         only immediate is available.  */
6101
66.1k
      if (guess_suffix == QWORD_MNEM_SUFFIX)
6102
0
        i.types[op].bitfield.imm32 = 0;
6103
66.1k
      break;
6104
6105
0
    case O_absent:
6106
0
    case O_register:
6107
0
      abort ();
6108
6109
      /* Symbols and expressions.  */
6110
13.8k
    default:
6111
      /* Convert symbolic operand to proper sizes for matching, but don't
6112
         prevent matching a set of insns that only supports sizes other
6113
         than those matching the insn suffix.  */
6114
13.8k
      {
6115
13.8k
        i386_operand_type mask, allowed;
6116
13.8k
        const insn_template *t = current_templates->start;
6117
6118
13.8k
        operand_type_set (&mask, 0);
6119
13.8k
        switch (guess_suffix)
6120
13.8k
    {
6121
0
    case QWORD_MNEM_SUFFIX:
6122
0
      mask.bitfield.imm64 = 1;
6123
0
      mask.bitfield.imm32s = 1;
6124
0
      break;
6125
4.34k
    case LONG_MNEM_SUFFIX:
6126
4.34k
      mask.bitfield.imm32 = 1;
6127
4.34k
      break;
6128
9.31k
    case WORD_MNEM_SUFFIX:
6129
9.31k
      mask.bitfield.imm16 = 1;
6130
9.31k
      break;
6131
175
    case BYTE_MNEM_SUFFIX:
6132
175
      mask.bitfield.imm8 = 1;
6133
175
      break;
6134
0
    default:
6135
0
      break;
6136
13.8k
    }
6137
6138
13.8k
        allowed = operand_type_and (t->operand_types[op], mask);
6139
52.8k
        while (++t < current_templates->end)
6140
38.9k
    {
6141
38.9k
      allowed = operand_type_or (allowed, t->operand_types[op]);
6142
38.9k
      allowed = operand_type_and (allowed, mask);
6143
38.9k
    }
6144
6145
13.8k
        if (!operand_type_all_zero (&allowed))
6146
11.3k
    i.types[op] = operand_type_and (i.types[op], mask);
6147
13.8k
      }
6148
0
      break;
6149
80.0k
    }
6150
80.0k
      }
6151
80.0k
}
6152
6153
/* Try to use the smallest displacement type too.  */
6154
static bool
6155
optimize_disp (const insn_template *t)
6156
105k
{
6157
105k
  unsigned int op;
6158
6159
105k
  if (!want_disp32 (t)
6160
105k
      && (!t->opcode_modifier.jump
6161
41.5k
    || i.jumpabsolute || i.types[0].bitfield.baseindex))
6162
39.4k
    {
6163
109k
      for (op = 0; op < i.operands; ++op)
6164
70.4k
  {
6165
70.4k
    const expressionS *exp = i.op[op].disps;
6166
6167
70.4k
    if (!operand_type_check (i.types[op], disp))
6168
29.1k
      continue;
6169
6170
41.3k
    if (exp->X_op != O_constant)
6171
12.2k
      continue;
6172
6173
    /* Since displacement is signed extended to 64bit, don't allow
6174
       disp32 if it is out of range.  */
6175
29.0k
    if (fits_in_signed_long (exp->X_add_number))
6176
28.9k
      continue;
6177
6178
115
    i.types[op].bitfield.disp32 = 0;
6179
115
    if (i.types[op].bitfield.baseindex)
6180
0
      {
6181
0
        as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
6182
0
          (uint64_t) exp->X_add_number);
6183
0
        return false;
6184
0
      }
6185
115
  }
6186
39.4k
    }
6187
6188
  /* Don't optimize displacement for movabs since it only takes 64bit
6189
     displacement.  */
6190
105k
  if (i.disp_encoding > disp_encoding_8bit
6191
105k
      || (flag_code == CODE_64BIT && t->mnem_off == MN_movabs))
6192
116
    return true;
6193
6194
292k
  for (op = i.operands; op-- > 0;)
6195
186k
    if (operand_type_check (i.types[op], disp))
6196
110k
      {
6197
110k
  if (i.op[op].disps->X_op == O_constant)
6198
81.8k
    {
6199
81.8k
      offsetT op_disp = i.op[op].disps->X_add_number;
6200
6201
81.8k
      if (!op_disp && i.types[op].bitfield.baseindex)
6202
0
        {
6203
0
    i.types[op] = operand_type_and_not (i.types[op], anydisp);
6204
0
    i.op[op].disps = NULL;
6205
0
    i.disp_operands--;
6206
0
    continue;
6207
0
        }
6208
6209
81.8k
      if (i.types[op].bitfield.disp16
6210
81.8k
    && fits_in_unsigned_word (op_disp))
6211
23.0k
        {
6212
    /* If this operand is at most 16 bits, convert
6213
       to a signed 16 bit number and don't use 64bit
6214
       displacement.  */
6215
23.0k
    op_disp = ((op_disp ^ 0x8000) - 0x8000);
6216
23.0k
    i.types[op].bitfield.disp64 = 0;
6217
23.0k
        }
6218
6219
81.8k
#ifdef BFD64
6220
      /* Optimize 64-bit displacement to 32-bit for 64-bit BFD.  */
6221
81.8k
      if ((flag_code != CODE_64BIT
6222
81.8k
     ? i.types[op].bitfield.disp32
6223
81.8k
     : want_disp32 (t)
6224
30.5k
       && (!t->opcode_modifier.jump
6225
76
           || i.jumpabsolute || i.types[op].bitfield.baseindex))
6226
81.8k
    && fits_in_unsigned_long (op_disp))
6227
742
        {
6228
    /* If this operand is at most 32 bits, convert
6229
       to a signed 32 bit number and don't use 64bit
6230
       displacement.  */
6231
742
    op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
6232
742
    i.types[op].bitfield.disp64 = 0;
6233
742
    i.types[op].bitfield.disp32 = 1;
6234
742
        }
6235
6236
81.8k
      if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
6237
30.2k
        {
6238
30.2k
    i.types[op].bitfield.disp64 = 0;
6239
30.2k
    i.types[op].bitfield.disp32 = 1;
6240
30.2k
        }
6241
81.8k
#endif
6242
81.8k
      if ((i.types[op].bitfield.disp32
6243
81.8k
     || i.types[op].bitfield.disp16)
6244
81.8k
    && fits_in_disp8 (op_disp))
6245
72.8k
        i.types[op].bitfield.disp8 = 1;
6246
6247
81.8k
      i.op[op].disps->X_add_number = op_disp;
6248
81.8k
    }
6249
29.1k
  else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
6250
29.1k
     || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
6251
0
    {
6252
0
      fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
6253
0
       i.op[op].disps, 0, i.reloc[op]);
6254
0
      i.types[op] = operand_type_and_not (i.types[op], anydisp);
6255
0
    }
6256
29.1k
  else
6257
    /* We only support 64bit displacement on constants.  */
6258
29.1k
    i.types[op].bitfield.disp64 = 0;
6259
110k
      }
6260
6261
105k
  return true;
6262
105k
}
6263
6264
/* Return 1 if there is a match in broadcast bytes between operand
6265
   GIVEN and instruction template T.   */
6266
6267
static INLINE int
6268
match_broadcast_size (const insn_template *t, unsigned int given)
6269
0
{
6270
0
  return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
6271
0
     && i.types[given].bitfield.byte)
6272
0
    || (t->opcode_modifier.broadcast == WORD_BROADCAST
6273
0
        && i.types[given].bitfield.word)
6274
0
    || (t->opcode_modifier.broadcast == DWORD_BROADCAST
6275
0
        && i.types[given].bitfield.dword)
6276
0
    || (t->opcode_modifier.broadcast == QWORD_BROADCAST
6277
0
        && i.types[given].bitfield.qword));
6278
0
}
6279
6280
/* Check if operands are valid for the instruction.  */
6281
6282
static int
6283
check_VecOperands (const insn_template *t)
6284
82.6k
{
6285
82.6k
  unsigned int op;
6286
82.6k
  i386_cpu_flags cpu;
6287
6288
  /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
6289
     any one operand are implicity requiring AVX512VL support if the actual
6290
     operand size is YMMword or XMMword.  Since this function runs after
6291
     template matching, there's no need to check for YMMword/XMMword in
6292
     the template.  */
6293
82.6k
  cpu = cpu_flags_and (t->cpu_flags, avx512);
6294
82.6k
  if (!cpu_flags_all_zero (&cpu)
6295
82.6k
      && !t->cpu_flags.bitfield.cpuavx512vl
6296
82.6k
      && !cpu_arch_flags.bitfield.cpuavx512vl)
6297
0
    {
6298
0
      for (op = 0; op < t->operands; ++op)
6299
0
  {
6300
0
    if (t->operand_types[op].bitfield.zmmword
6301
0
        && (i.types[op].bitfield.ymmword
6302
0
      || i.types[op].bitfield.xmmword))
6303
0
      {
6304
0
        i.error = unsupported;
6305
0
        return 1;
6306
0
      }
6307
0
  }
6308
0
    }
6309
6310
  /* Somewhat similarly, templates specifying both AVX and AVX2 are
6311
     requiring AVX2 support if the actual operand size is YMMword.  */
6312
82.6k
  if (t->cpu_flags.bitfield.cpuavx
6313
82.6k
      && t->cpu_flags.bitfield.cpuavx2
6314
82.6k
      && !cpu_arch_flags.bitfield.cpuavx2)
6315
0
    {
6316
0
      for (op = 0; op < t->operands; ++op)
6317
0
  {
6318
0
    if (t->operand_types[op].bitfield.xmmword
6319
0
        && i.types[op].bitfield.ymmword)
6320
0
      {
6321
0
        i.error = unsupported;
6322
0
        return 1;
6323
0
      }
6324
0
  }
6325
0
    }
6326
6327
  /* Without VSIB byte, we can't have a vector register for index.  */
6328
82.6k
  if (!t->opcode_modifier.sib
6329
82.6k
      && i.index_reg
6330
82.6k
      && (i.index_reg->reg_type.bitfield.xmmword
6331
0
    || i.index_reg->reg_type.bitfield.ymmword
6332
0
    || i.index_reg->reg_type.bitfield.zmmword))
6333
0
    {
6334
0
      i.error = unsupported_vector_index_register;
6335
0
      return 1;
6336
0
    }
6337
6338
  /* Check if default mask is allowed.  */
6339
82.6k
  if (t->opcode_modifier.operandconstraint == NO_DEFAULT_MASK
6340
82.6k
      && (!i.mask.reg || i.mask.reg->reg_num == 0))
6341
0
    {
6342
0
      i.error = no_default_mask;
6343
0
      return 1;
6344
0
    }
6345
6346
  /* For VSIB byte, we need a vector register for index, and all vector
6347
     registers must be distinct.  */
6348
82.6k
  if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
6349
0
    {
6350
0
      if (!i.index_reg
6351
0
    || !((t->opcode_modifier.sib == VECSIB128
6352
0
    && i.index_reg->reg_type.bitfield.xmmword)
6353
0
         || (t->opcode_modifier.sib == VECSIB256
6354
0
       && i.index_reg->reg_type.bitfield.ymmword)
6355
0
         || (t->opcode_modifier.sib == VECSIB512
6356
0
       && i.index_reg->reg_type.bitfield.zmmword)))
6357
0
      {
6358
0
  i.error = invalid_vsib_address;
6359
0
  return 1;
6360
0
      }
6361
6362
0
      gas_assert (i.reg_operands == 2 || i.mask.reg);
6363
0
      if (i.reg_operands == 2 && !i.mask.reg)
6364
0
  {
6365
0
    gas_assert (i.types[0].bitfield.class == RegSIMD);
6366
0
    gas_assert (i.types[0].bitfield.xmmword
6367
0
          || i.types[0].bitfield.ymmword);
6368
0
    gas_assert (i.types[2].bitfield.class == RegSIMD);
6369
0
    gas_assert (i.types[2].bitfield.xmmword
6370
0
          || i.types[2].bitfield.ymmword);
6371
0
    if (operand_check == check_none)
6372
0
      return 0;
6373
0
    if (register_number (i.op[0].regs)
6374
0
        != register_number (i.index_reg)
6375
0
        && register_number (i.op[2].regs)
6376
0
     != register_number (i.index_reg)
6377
0
        && register_number (i.op[0].regs)
6378
0
     != register_number (i.op[2].regs))
6379
0
      return 0;
6380
0
    if (operand_check == check_error)
6381
0
      {
6382
0
        i.error = invalid_vector_register_set;
6383
0
        return 1;
6384
0
      }
6385
0
    as_warn (_("mask, index, and destination registers should be distinct"));
6386
0
  }
6387
0
      else if (i.reg_operands == 1 && i.mask.reg)
6388
0
  {
6389
0
    if (i.types[1].bitfield.class == RegSIMD
6390
0
        && (i.types[1].bitfield.xmmword
6391
0
            || i.types[1].bitfield.ymmword
6392
0
            || i.types[1].bitfield.zmmword)
6393
0
        && (register_number (i.op[1].regs)
6394
0
      == register_number (i.index_reg)))
6395
0
      {
6396
0
        if (operand_check == check_error)
6397
0
    {
6398
0
      i.error = invalid_vector_register_set;
6399
0
      return 1;
6400
0
    }
6401
0
        if (operand_check != check_none)
6402
0
    as_warn (_("index and destination registers should be distinct"));
6403
0
      }
6404
0
  }
6405
0
    }
6406
6407
  /* For AMX instructions with 3 TMM register operands, all operands
6408
      must be distinct.  */
6409
82.6k
  if (i.reg_operands == 3
6410
82.6k
      && t->operand_types[0].bitfield.tmmword
6411
82.6k
      && (i.op[0].regs == i.op[1].regs
6412
0
          || i.op[0].regs == i.op[2].regs
6413
0
          || i.op[1].regs == i.op[2].regs))
6414
0
    {
6415
0
      i.error = invalid_tmm_register_set;
6416
0
      return 1;
6417
0
    }
6418
6419
  /* For some special instructions require that destination must be distinct
6420
     from source registers.  */
6421
82.6k
  if (t->opcode_modifier.operandconstraint == DISTINCT_DEST)
6422
0
    {
6423
0
      unsigned int dest_reg = i.operands - 1;
6424
6425
0
      know (i.operands >= 3);
6426
6427
      /* #UD if dest_reg == src1_reg or dest_reg == src2_reg.  */
6428
0
      if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6429
0
    || (i.reg_operands > 2
6430
0
        && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6431
0
  {
6432
0
    i.error = invalid_dest_and_src_register_set;
6433
0
    return 1;
6434
0
  }
6435
0
    }
6436
6437
  /* Check if broadcast is supported by the instruction and is applied
6438
     to the memory operand.  */
6439
82.6k
  if (i.broadcast.type || i.broadcast.bytes)
6440
0
    {
6441
0
      i386_operand_type type, overlap;
6442
6443
      /* Check if specified broadcast is supported in this instruction,
6444
   and its broadcast bytes match the memory operand.  */
6445
0
      op = i.broadcast.operand;
6446
0
      if (!t->opcode_modifier.broadcast
6447
0
    || !(i.flags[op] & Operand_Mem)
6448
0
    || (!i.types[op].bitfield.unspecified
6449
0
        && !match_broadcast_size (t, op)))
6450
0
  {
6451
0
  bad_broadcast:
6452
0
    i.error = unsupported_broadcast;
6453
0
    return 1;
6454
0
  }
6455
6456
0
      operand_type_set (&type, 0);
6457
0
      switch (get_broadcast_bytes (t, false))
6458
0
  {
6459
0
  case 2:
6460
0
    type.bitfield.word = 1;
6461
0
    break;
6462
0
  case 4:
6463
0
    type.bitfield.dword = 1;
6464
0
    break;
6465
0
  case 8:
6466
0
    type.bitfield.qword = 1;
6467
0
    break;
6468
0
  case 16:
6469
0
    type.bitfield.xmmword = 1;
6470
0
    break;
6471
0
  case 32:
6472
0
    type.bitfield.ymmword = 1;
6473
0
    break;
6474
0
  case 64:
6475
0
    type.bitfield.zmmword = 1;
6476
0
    break;
6477
0
  default:
6478
0
    goto bad_broadcast;
6479
0
  }
6480
6481
0
      overlap = operand_type_and (type, t->operand_types[op]);
6482
0
      if (t->operand_types[op].bitfield.class == RegSIMD
6483
0
    && t->operand_types[op].bitfield.byte
6484
0
       + t->operand_types[op].bitfield.word
6485
0
       + t->operand_types[op].bitfield.dword
6486
0
       + t->operand_types[op].bitfield.qword > 1)
6487
0
  {
6488
0
    overlap.bitfield.xmmword = 0;
6489
0
    overlap.bitfield.ymmword = 0;
6490
0
    overlap.bitfield.zmmword = 0;
6491
0
  }
6492
0
      if (operand_type_all_zero (&overlap))
6493
0
    goto bad_broadcast;
6494
6495
0
      if (t->opcode_modifier.checkoperandsize)
6496
0
  {
6497
0
    unsigned int j;
6498
6499
0
    type.bitfield.baseindex = 1;
6500
0
    for (j = 0; j < i.operands; ++j)
6501
0
      {
6502
0
        if (j != op
6503
0
      && !operand_type_register_match(i.types[j],
6504
0
              t->operand_types[j],
6505
0
              type,
6506
0
              t->operand_types[op]))
6507
0
    goto bad_broadcast;
6508
0
      }
6509
0
  }
6510
0
    }
6511
  /* If broadcast is supported in this instruction, we need to check if
6512
     operand of one-element size isn't specified without broadcast.  */
6513
82.6k
  else if (t->opcode_modifier.broadcast && i.mem_operands)
6514
0
    {
6515
      /* Find memory operand.  */
6516
0
      for (op = 0; op < i.operands; op++)
6517
0
  if (i.flags[op] & Operand_Mem)
6518
0
    break;
6519
0
      gas_assert (op < i.operands);
6520
      /* Check size of the memory operand.  */
6521
0
      if (match_broadcast_size (t, op))
6522
0
  {
6523
0
    i.error = broadcast_needed;
6524
0
    return 1;
6525
0
  }
6526
0
    }
6527
82.6k
  else
6528
82.6k
    op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning.  */
6529
6530
  /* Check if requested masking is supported.  */
6531
82.6k
  if (i.mask.reg)
6532
0
    {
6533
0
      if (!t->opcode_modifier.masking)
6534
0
  {
6535
0
    i.error = unsupported_masking;
6536
0
    return 1;
6537
0
  }
6538
6539
      /* Common rules for masking:
6540
   - mask register destinations permit only zeroing-masking, without
6541
     that actually being expressed by a {z} operand suffix or EVEX.z,
6542
   - memory destinations allow only merging-masking,
6543
   - scatter/gather insns (i.e. ones using vSIB) only allow merging-
6544
     masking.  */
6545
0
      if (i.mask.zeroing
6546
0
    && (t->operand_types[t->operands - 1].bitfield.class == RegMask
6547
0
        || (i.flags[t->operands - 1] & Operand_Mem)
6548
0
        || t->opcode_modifier.sib))
6549
0
  {
6550
0
    i.error = unsupported_masking;
6551
0
    return 1;
6552
0
  }
6553
0
    }
6554
6555
  /* Check if masking is applied to dest operand.  */
6556
82.6k
  if (i.mask.reg && (i.mask.operand != i.operands - 1))
6557
0
    {
6558
0
      i.error = mask_not_on_destination;
6559
0
      return 1;
6560
0
    }
6561
6562
  /* Check RC/SAE.  */
6563
82.6k
  if (i.rounding.type != rc_none)
6564
0
    {
6565
0
      if (!t->opcode_modifier.sae
6566
0
    || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
6567
0
    || i.mem_operands)
6568
0
  {
6569
0
    i.error = unsupported_rc_sae;
6570
0
    return 1;
6571
0
  }
6572
6573
      /* Non-EVEX.LIG forms need to have a ZMM register as at least one
6574
   operand.  */
6575
0
      if (t->opcode_modifier.evex != EVEXLIG)
6576
0
  {
6577
0
    for (op = 0; op < t->operands; ++op)
6578
0
      if (i.types[op].bitfield.zmmword)
6579
0
        break;
6580
0
    if (op >= t->operands)
6581
0
      {
6582
0
        i.error = operand_size_mismatch;
6583
0
        return 1;
6584
0
      }
6585
0
  }
6586
0
    }
6587
6588
  /* Check the special Imm4 cases; must be the first operand.  */
6589
82.6k
  if (t->cpu_flags.bitfield.cpuxop && t->operands == 5)
6590
0
    {
6591
0
      if (i.op[0].imms->X_op != O_constant
6592
0
    || !fits_in_imm4 (i.op[0].imms->X_add_number))
6593
0
  {
6594
0
    i.error = bad_imm4;
6595
0
    return 1;
6596
0
  }
6597
6598
      /* Turn off Imm<N> so that update_imm won't complain.  */
6599
0
      operand_type_set (&i.types[0], 0);
6600
0
    }
6601
6602
  /* Check vector Disp8 operand.  */
6603
82.6k
  if (t->opcode_modifier.disp8memshift
6604
82.6k
      && i.disp_encoding <= disp_encoding_8bit)
6605
0
    {
6606
0
      if (i.broadcast.type || i.broadcast.bytes)
6607
0
  i.memshift = t->opcode_modifier.broadcast - 1;
6608
0
      else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
6609
0
  i.memshift = t->opcode_modifier.disp8memshift;
6610
0
      else
6611
0
  {
6612
0
    const i386_operand_type *type = NULL, *fallback = NULL;
6613
6614
0
    i.memshift = 0;
6615
0
    for (op = 0; op < i.operands; op++)
6616
0
      if (i.flags[op] & Operand_Mem)
6617
0
        {
6618
0
    if (t->opcode_modifier.evex == EVEXLIG)
6619
0
      i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6620
0
    else if (t->operand_types[op].bitfield.xmmword
6621
0
       + t->operand_types[op].bitfield.ymmword
6622
0
       + t->operand_types[op].bitfield.zmmword <= 1)
6623
0
      type = &t->operand_types[op];
6624
0
    else if (!i.types[op].bitfield.unspecified)
6625
0
      type = &i.types[op];
6626
0
    else /* Ambiguities get resolved elsewhere.  */
6627
0
      fallback = &t->operand_types[op];
6628
0
        }
6629
0
      else if (i.types[op].bitfield.class == RegSIMD
6630
0
         && t->opcode_modifier.evex != EVEXLIG)
6631
0
        {
6632
0
    if (i.types[op].bitfield.zmmword)
6633
0
      i.memshift = 6;
6634
0
    else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6635
0
      i.memshift = 5;
6636
0
    else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6637
0
      i.memshift = 4;
6638
0
        }
6639
6640
0
    if (!type && !i.memshift)
6641
0
      type = fallback;
6642
0
    if (type)
6643
0
      {
6644
0
        if (type->bitfield.zmmword)
6645
0
    i.memshift = 6;
6646
0
        else if (type->bitfield.ymmword)
6647
0
    i.memshift = 5;
6648
0
        else if (type->bitfield.xmmword)
6649
0
    i.memshift = 4;
6650
0
      }
6651
6652
    /* For the check in fits_in_disp8().  */
6653
0
    if (i.memshift == 0)
6654
0
      i.memshift = -1;
6655
0
  }
6656
6657
0
      for (op = 0; op < i.operands; op++)
6658
0
  if (operand_type_check (i.types[op], disp)
6659
0
      && i.op[op].disps->X_op == O_constant)
6660
0
    {
6661
0
      if (fits_in_disp8 (i.op[op].disps->X_add_number))
6662
0
        {
6663
0
    i.types[op].bitfield.disp8 = 1;
6664
0
    return 0;
6665
0
        }
6666
0
      i.types[op].bitfield.disp8 = 0;
6667
0
    }
6668
0
    }
6669
6670
82.6k
  i.memshift = 0;
6671
6672
82.6k
  return 0;
6673
82.6k
}
6674
6675
/* Check if encoding requirements are met by the instruction.  */
6676
6677
static int
6678
VEX_check_encoding (const insn_template *t)
6679
187k
{
6680
187k
  if (i.vec_encoding == vex_encoding_error)
6681
0
    {
6682
0
      i.error = unsupported;
6683
0
      return 1;
6684
0
    }
6685
6686
187k
  if (i.vec_encoding == vex_encoding_evex)
6687
0
    {
6688
      /* This instruction must be encoded with EVEX prefix.  */
6689
0
      if (!is_evex_encoding (t))
6690
0
  {
6691
0
    i.error = unsupported;
6692
0
    return 1;
6693
0
  }
6694
0
      return 0;
6695
0
    }
6696
6697
187k
  if (!t->opcode_modifier.vex)
6698
187k
    {
6699
      /* This instruction template doesn't have VEX prefix.  */
6700
187k
      if (i.vec_encoding != vex_encoding_default)
6701
0
  {
6702
0
    i.error = unsupported;
6703
0
    return 1;
6704
0
  }
6705
187k
      return 0;
6706
187k
    }
6707
6708
2
  return 0;
6709
187k
}
6710
6711
/* Helper function for the progress() macro in match_template().  */
6712
static INLINE enum i386_error progress (enum i386_error new,
6713
          enum i386_error last,
6714
          unsigned int line, unsigned int *line_p)
6715
2.15M
{
6716
2.15M
  if (line <= *line_p)
6717
731k
    return last;
6718
1.42M
  *line_p = line;
6719
1.42M
  return new;
6720
2.15M
}
6721
6722
static const insn_template *
6723
match_template (char mnem_suffix)
6724
224k
{
6725
  /* Points to template once we've found it.  */
6726
224k
  const insn_template *t;
6727
224k
  i386_operand_type overlap0, overlap1, overlap2, overlap3;
6728
224k
  i386_operand_type overlap4;
6729
224k
  unsigned int found_reverse_match;
6730
224k
  i386_operand_type operand_types [MAX_OPERANDS];
6731
224k
  int addr_prefix_disp;
6732
224k
  unsigned int j, size_match, check_register, errline = __LINE__;
6733
224k
  enum i386_error specific_error = number_of_operands_mismatch;
6734
2.15M
#define progress(err) progress (err, specific_error, __LINE__, &errline)
6735
6736
#if MAX_OPERANDS != 5
6737
# error "MAX_OPERANDS must be 5."
6738
#endif
6739
6740
224k
  found_reverse_match = 0;
6741
224k
  addr_prefix_disp = -1;
6742
6743
623k
  for (t = current_templates->start; t < current_templates->end; t++)
6744
586k
    {
6745
586k
      addr_prefix_disp = -1;
6746
586k
      found_reverse_match = 0;
6747
6748
      /* Must have right number of operands.  */
6749
586k
      if (i.operands != t->operands)
6750
274k
  continue;
6751
6752
      /* Check processor support.  */
6753
311k
      specific_error = progress (unsupported);
6754
311k
      if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
6755
7.46k
  continue;
6756
6757
      /* Check AT&T mnemonic.   */
6758
304k
      specific_error = progress (unsupported_with_intel_mnemonic);
6759
304k
      if (intel_mnemonic && t->opcode_modifier.attmnemonic)
6760
0
  continue;
6761
6762
      /* Check AT&T/Intel syntax.  */
6763
304k
      specific_error = progress (unsupported_syntax);
6764
304k
      if ((intel_syntax && t->opcode_modifier.attsyntax)
6765
304k
    || (!intel_syntax && t->opcode_modifier.intelsyntax))
6766
0
  continue;
6767
6768
      /* Check Intel64/AMD64 ISA.   */
6769
304k
      switch (isa64)
6770
304k
  {
6771
304k
  default:
6772
    /* Default: Don't accept Intel64.  */
6773
304k
    if (t->opcode_modifier.isa64 == INTEL64)
6774
39
      continue;
6775
304k
    break;
6776
304k
  case amd64:
6777
    /* -mamd64: Don't accept Intel64 and Intel64 only.  */
6778
0
    if (t->opcode_modifier.isa64 >= INTEL64)
6779
0
      continue;
6780
0
    break;
6781
0
  case intel64:
6782
    /* -mintel64: Don't accept AMD64.  */
6783
0
    if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
6784
0
      continue;
6785
0
    break;
6786
304k
  }
6787
6788
      /* Check the suffix.  */
6789
304k
      specific_error = progress (invalid_instruction_suffix);
6790
304k
      if ((t->opcode_modifier.no_bsuf && mnem_suffix == BYTE_MNEM_SUFFIX)
6791
304k
    || (t->opcode_modifier.no_wsuf && mnem_suffix == WORD_MNEM_SUFFIX)
6792
304k
    || (t->opcode_modifier.no_lsuf && mnem_suffix == LONG_MNEM_SUFFIX)
6793
304k
    || (t->opcode_modifier.no_ssuf && mnem_suffix == SHORT_MNEM_SUFFIX)
6794
304k
    || (t->opcode_modifier.no_qsuf && mnem_suffix == QWORD_MNEM_SUFFIX))
6795
1.32k
  continue;
6796
6797
303k
      specific_error = progress (operand_size_mismatch);
6798
303k
      size_match = operand_size_match (t);
6799
303k
      if (!size_match)
6800
32.3k
  continue;
6801
6802
      /* This is intentionally not
6803
6804
   if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6805
6806
   as the case of a missing * on the operand is accepted (perhaps with
6807
   a warning, issued further down).  */
6808
270k
      specific_error = progress (operand_type_mismatch);
6809
270k
      if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
6810
92
  continue;
6811
6812
      /* In Intel syntax, normally we can check for memory operand size when
6813
   there is no mnemonic suffix.  But jmp and call have 2 different
6814
   encodings with Dword memory operand size.  Skip the "near" one
6815
   (permitting a register operand) when "far" was requested.  */
6816
270k
      if (i.far_branch
6817
270k
    && t->opcode_modifier.jump == JUMP_ABSOLUTE
6818
270k
    && t->operand_types[0].bitfield.class == Reg)
6819
0
  continue;
6820
6821
1.62M
      for (j = 0; j < MAX_OPERANDS; j++)
6822
1.35M
  operand_types[j] = t->operand_types[j];
6823
6824
      /* In general, don't allow 32-bit operands on pre-386.  */
6825
270k
      specific_error = progress (mnem_suffix ? invalid_instruction_suffix
6826
270k
               : operand_size_mismatch);
6827
270k
      j = i.imm_operands + (t->operands > i.imm_operands + 1);
6828
270k
      if (i.suffix == LONG_MNEM_SUFFIX
6829
270k
    && !cpu_arch_flags.bitfield.cpui386
6830
270k
    && (intel_syntax
6831
0
        ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
6832
0
     && !intel_float_operand (insn_name (t)))
6833
0
        : intel_float_operand (insn_name (t)) != 2)
6834
270k
    && (t->operands == i.imm_operands
6835
0
        || (operand_types[i.imm_operands].bitfield.class != RegMMX
6836
0
         && operand_types[i.imm_operands].bitfield.class != RegSIMD
6837
0
         && operand_types[i.imm_operands].bitfield.class != RegMask)
6838
0
        || (operand_types[j].bitfield.class != RegMMX
6839
0
      && operand_types[j].bitfield.class != RegSIMD
6840
0
      && operand_types[j].bitfield.class != RegMask))
6841
270k
    && !t->opcode_modifier.sib)
6842
0
  continue;
6843
6844
      /* Do not verify operands when there are none.  */
6845
270k
      if (!t->operands)
6846
104k
  {
6847
104k
    if (VEX_check_encoding (t))
6848
0
      {
6849
0
        specific_error = progress (i.error);
6850
0
        continue;
6851
0
      }
6852
6853
    /* We've found a match; break out of loop.  */
6854
104k
    break;
6855
104k
  }
6856
6857
166k
      if (!t->opcode_modifier.jump
6858
166k
    || t->opcode_modifier.jump == JUMP_ABSOLUTE)
6859
161k
  {
6860
    /* There should be only one Disp operand.  */
6861
324k
    for (j = 0; j < MAX_OPERANDS; j++)
6862
322k
      if (operand_type_check (operand_types[j], disp))
6863
159k
        break;
6864
161k
    if (j < MAX_OPERANDS)
6865
159k
      {
6866
159k
        bool override = (i.prefix[ADDR_PREFIX] != 0);
6867
6868
159k
        addr_prefix_disp = j;
6869
6870
        /* Address size prefix will turn Disp64 operand into Disp32 and
6871
     Disp32/Disp16 one into Disp16/Disp32 respectively.  */
6872
159k
        switch (flag_code)
6873
159k
    {
6874
95.4k
    case CODE_16BIT:
6875
95.4k
      override = !override;
6876
      /* Fall through.  */
6877
98.9k
    case CODE_32BIT:
6878
98.9k
      if (operand_types[j].bitfield.disp32
6879
98.9k
          && operand_types[j].bitfield.disp16)
6880
98.9k
        {
6881
98.9k
          operand_types[j].bitfield.disp16 = override;
6882
98.9k
          operand_types[j].bitfield.disp32 = !override;
6883
98.9k
        }
6884
98.9k
      gas_assert (!operand_types[j].bitfield.disp64);
6885
0
      break;
6886
6887
60.4k
    case CODE_64BIT:
6888
60.4k
      if (operand_types[j].bitfield.disp64)
6889
83
        {
6890
83
          gas_assert (!operand_types[j].bitfield.disp32);
6891
0
          operand_types[j].bitfield.disp32 = override;
6892
83
          operand_types[j].bitfield.disp64 = !override;
6893
83
        }
6894
0
      operand_types[j].bitfield.disp16 = 0;
6895
60.4k
      break;
6896
159k
    }
6897
159k
      }
6898
161k
  }
6899
6900
      /* We check register size if needed.  */
6901
166k
      if (t->opcode_modifier.checkoperandsize)
6902
72.2k
  {
6903
72.2k
    check_register = (1 << t->operands) - 1;
6904
72.2k
    if (i.broadcast.type || i.broadcast.bytes)
6905
0
      check_register &= ~(1 << i.broadcast.operand);
6906
72.2k
  }
6907
93.9k
      else
6908
93.9k
  check_register = 0;
6909
6910
166k
      overlap0 = operand_type_and (i.types[0], operand_types[0]);
6911
166k
      switch (t->operands)
6912
166k
  {
6913
10.1k
  case 1:
6914
10.1k
    if (!operand_type_match (overlap0, i.types[0]))
6915
366
      continue;
6916
6917
    /* Allow the ModR/M encoding to be requested by using the {load} or
6918
       {store} pseudo prefix on an applicable insn.  */
6919
9.77k
    if (!t->opcode_modifier.modrm
6920
9.77k
        && i.reg_operands == 1
6921
9.77k
        && ((i.dir_encoding == dir_encoding_load
6922
0
       && t->mnem_off != MN_pop)
6923
0
      || (i.dir_encoding == dir_encoding_store
6924
0
          && t->mnem_off != MN_push))
6925
        /* Avoid BSWAP.  */
6926
9.77k
        && t->mnem_off != MN_bswap)
6927
0
      continue;
6928
9.77k
    break;
6929
6930
156k
  case 2:
6931
    /* xchg %eax, %eax is a special case. It is an alias for nop
6932
       only in 32bit mode and we can use opcode 0x90.  In 64bit
6933
       mode, we can't use 0x90 for xchg %eax, %eax since it should
6934
       zero-extend %eax to %rax.  */
6935
156k
    if (t->base_opcode == 0x90
6936
156k
        && t->opcode_space == SPACE_BASE)
6937
0
      {
6938
0
        if (flag_code == CODE_64BIT
6939
0
      && i.types[0].bitfield.instance == Accum
6940
0
      && i.types[0].bitfield.dword
6941
0
      && i.types[1].bitfield.instance == Accum)
6942
0
    continue;
6943
6944
        /* Allow the ModR/M encoding to be requested by using the
6945
     {load} or {store} pseudo prefix.  */
6946
0
        if (i.dir_encoding == dir_encoding_load
6947
0
      || i.dir_encoding == dir_encoding_store)
6948
0
    continue;
6949
0
      }
6950
6951
156k
    if (t->base_opcode == MOV_AX_DISP32
6952
156k
        && t->opcode_space == SPACE_BASE
6953
156k
        && t->mnem_off != MN_movabs)
6954
102
      {
6955
        /* Force 0x8b encoding for "mov foo@GOT, %eax".  */
6956
102
        if (i.reloc[0] == BFD_RELOC_386_GOT32)
6957
0
    continue;
6958
6959
        /* xrelease mov %eax, <disp> is another special case. It must not
6960
     match the accumulator-only encoding of mov.  */
6961
102
        if (i.hle_prefix)
6962
0
    continue;
6963
6964
        /* Allow the ModR/M encoding to be requested by using a suitable
6965
     {load} or {store} pseudo prefix.  */
6966
102
        if (i.dir_encoding == (i.types[0].bitfield.instance == Accum
6967
102
             ? dir_encoding_store
6968
102
             : dir_encoding_load)
6969
102
      && !i.types[0].bitfield.disp64
6970
102
      && !i.types[1].bitfield.disp64)
6971
0
    continue;
6972
102
      }
6973
6974
    /* Allow the ModR/M encoding to be requested by using the {load} or
6975
       {store} pseudo prefix on an applicable insn.  */
6976
156k
    if (!t->opcode_modifier.modrm
6977
156k
        && i.reg_operands == 1
6978
156k
        && i.imm_operands == 1
6979
156k
        && (i.dir_encoding == dir_encoding_load
6980
0
      || i.dir_encoding == dir_encoding_store)
6981
156k
        && t->opcode_space == SPACE_BASE)
6982
0
      {
6983
0
        if (t->base_opcode == 0xb0 /* mov $imm, %reg */
6984
0
      && i.dir_encoding == dir_encoding_store)
6985
0
    continue;
6986
6987
0
        if ((t->base_opcode | 0x38) == 0x3c /* <alu> $imm, %acc */
6988
0
      && (t->base_opcode != 0x3c /* cmp $imm, %acc */
6989
0
          || i.dir_encoding == dir_encoding_load))
6990
0
    continue;
6991
6992
0
        if (t->base_opcode == 0xa8 /* test $imm, %acc */
6993
0
      && i.dir_encoding == dir_encoding_load)
6994
0
    continue;
6995
0
      }
6996
    /* Fall through.  */
6997
6998
156k
  case 3:
6999
156k
    if (!(size_match & MATCH_STRAIGHT))
7000
201
      goto check_reverse;
7001
    /* Reverse direction of operands if swapping is possible in the first
7002
       place (operands need to be symmetric) and
7003
       - the load form is requested, and the template is a store form,
7004
       - the store form is requested, and the template is a load form,
7005
       - the non-default (swapped) form is requested.  */
7006
155k
    overlap1 = operand_type_and (operand_types[0], operand_types[1]);
7007
155k
    if (t->opcode_modifier.d && i.reg_operands == i.operands
7008
155k
        && !operand_type_all_zero (&overlap1))
7009
0
      switch (i.dir_encoding)
7010
0
        {
7011
0
        case dir_encoding_load:
7012
0
    if (operand_type_check (operand_types[i.operands - 1], anymem)
7013
0
        || t->opcode_modifier.regmem)
7014
0
      goto check_reverse;
7015
0
    break;
7016
7017
0
        case dir_encoding_store:
7018
0
    if (!operand_type_check (operand_types[i.operands - 1], anymem)
7019
0
        && !t->opcode_modifier.regmem)
7020
0
      goto check_reverse;
7021
0
    break;
7022
7023
0
        case dir_encoding_swap:
7024
0
    goto check_reverse;
7025
7026
0
        case dir_encoding_default:
7027
0
    break;
7028
0
        }
7029
    /* If we want store form, we skip the current load.  */
7030
155k
    if ((i.dir_encoding == dir_encoding_store
7031
155k
         || i.dir_encoding == dir_encoding_swap)
7032
155k
        && i.mem_operands == 0
7033
155k
        && t->opcode_modifier.load)
7034
0
      continue;
7035
    /* Fall through.  */
7036
155k
  case 4:
7037
155k
  case 5:
7038
155k
    overlap1 = operand_type_and (i.types[1], operand_types[1]);
7039
155k
    if (!operand_type_match (overlap0, i.types[0])
7040
155k
        || !operand_type_match (overlap1, i.types[1])
7041
155k
        || ((check_register & 3) == 3
7042
72.8k
      && !operand_type_register_match (i.types[0],
7043
0
               operand_types[0],
7044
0
               i.types[1],
7045
0
               operand_types[1])))
7046
83.0k
      {
7047
83.0k
        specific_error = progress (i.error);
7048
7049
        /* Check if other direction is valid ...  */
7050
83.0k
        if (!t->opcode_modifier.d)
7051
10.8k
    continue;
7052
7053
72.3k
      check_reverse:
7054
72.3k
        if (!(size_match & MATCH_REVERSE))
7055
72.1k
    continue;
7056
        /* Try reversing direction of operands.  */
7057
202
        j = t->cpu_flags.bitfield.cpufma4
7058
202
      || t->cpu_flags.bitfield.cpuxop ? 1 : i.operands - 1;
7059
202
        overlap0 = operand_type_and (i.types[0], operand_types[j]);
7060
202
        overlap1 = operand_type_and (i.types[j], operand_types[0]);
7061
202
        overlap2 = operand_type_and (i.types[1], operand_types[1]);
7062
202
        gas_assert (t->operands != 3 || !check_register);
7063
202
        if (!operand_type_match (overlap0, i.types[0])
7064
202
      || !operand_type_match (overlap1, i.types[j])
7065
202
      || (t->operands == 3
7066
3
          && !operand_type_match (overlap2, i.types[1]))
7067
202
      || (check_register
7068
3
          && !operand_type_register_match (i.types[0],
7069
3
                   operand_types[j],
7070
3
                   i.types[j],
7071
3
                   operand_types[0])))
7072
199
    {
7073
      /* Does not match either direction.  */
7074
199
      specific_error = progress (i.error);
7075
199
      continue;
7076
199
    }
7077
        /* found_reverse_match holds which variant of D
7078
     we've found.  */
7079
3
        if (!t->opcode_modifier.d)
7080
0
    found_reverse_match = 0;
7081
3
        else if (operand_types[0].bitfield.tbyte)
7082
0
    {
7083
0
      if (t->opcode_modifier.operandconstraint != UGH)
7084
0
        found_reverse_match = Opcode_FloatD;
7085
0
      else
7086
0
        found_reverse_match = ~0;
7087
      /* FSUB{,R} and FDIV{,R} may need a 2nd bit flipped.  */
7088
0
      if ((t->extension_opcode & 4)
7089
0
          && (intel_syntax || intel_mnemonic))
7090
0
        found_reverse_match |= Opcode_FloatR;
7091
0
    }
7092
3
        else if (t->cpu_flags.bitfield.cpufma4
7093
3
           || t->cpu_flags.bitfield.cpuxop)
7094
0
    {
7095
0
      found_reverse_match = Opcode_VexW;
7096
0
      goto check_operands_345;
7097
0
    }
7098
3
        else if (t->opcode_space != SPACE_BASE
7099
3
           && (t->opcode_space != SPACE_0F
7100
         /* MOV to/from CR/DR/TR, as an exception, follow
7101
            the base opcode space encoding model.  */
7102
0
         || (t->base_opcode | 7) != 0x27))
7103
0
    found_reverse_match = (t->base_opcode & 0xee) != 0x6e
7104
0
              ? Opcode_ExtD : Opcode_SIMD_IntD;
7105
3
        else if (!t->opcode_modifier.commutative)
7106
3
    found_reverse_match = Opcode_D;
7107
0
        else
7108
0
    found_reverse_match = ~0;
7109
3
      }
7110
72.8k
    else
7111
72.8k
      {
7112
        /* Found a forward 2 operand match here.  */
7113
72.8k
      check_operands_345:
7114
72.8k
        switch (t->operands)
7115
72.8k
    {
7116
0
    case 5:
7117
0
      overlap4 = operand_type_and (i.types[4], operand_types[4]);
7118
0
      if (!operand_type_match (overlap4, i.types[4])
7119
0
          || !operand_type_register_match (i.types[3],
7120
0
                   operand_types[3],
7121
0
                   i.types[4],
7122
0
                   operand_types[4]))
7123
0
        {
7124
0
          specific_error = progress (i.error);
7125
0
          continue;
7126
0
        }
7127
      /* Fall through.  */
7128
0
    case 4:
7129
0
      overlap3 = operand_type_and (i.types[3], operand_types[3]);
7130
0
      if (!operand_type_match (overlap3, i.types[3])
7131
0
          || ((check_register & 0xa) == 0xa
7132
0
        && !operand_type_register_match (i.types[1],
7133
0
                  operand_types[1],
7134
0
                  i.types[3],
7135
0
                  operand_types[3]))
7136
0
          || ((check_register & 0xc) == 0xc
7137
0
        && !operand_type_register_match (i.types[2],
7138
0
                  operand_types[2],
7139
0
                  i.types[3],
7140
0
                  operand_types[3])))
7141
0
        {
7142
0
          specific_error = progress (i.error);
7143
0
          continue;
7144
0
        }
7145
      /* Fall through.  */
7146
0
    case 3:
7147
0
      overlap2 = operand_type_and (i.types[2], operand_types[2]);
7148
0
      if (!operand_type_match (overlap2, i.types[2])
7149
0
          || ((check_register & 5) == 5
7150
0
        && !operand_type_register_match (i.types[0],
7151
0
                  operand_types[0],
7152
0
                  i.types[2],
7153
0
                  operand_types[2]))
7154
0
          || ((check_register & 6) == 6
7155
0
        && !operand_type_register_match (i.types[1],
7156
0
                  operand_types[1],
7157
0
                  i.types[2],
7158
0
                  operand_types[2])))
7159
0
        {
7160
0
          specific_error = progress (i.error);
7161
0
          continue;
7162
0
        }
7163
0
      break;
7164
72.8k
    }
7165
72.8k
      }
7166
    /* Found either forward/reverse 2, 3 or 4 operand match here:
7167
       slip through to break.  */
7168
166k
  }
7169
7170
      /* Check if VEX/EVEX encoding requirements can be satisfied.  */
7171
82.6k
      if (VEX_check_encoding (t))
7172
0
  {
7173
0
    specific_error = progress (i.error);
7174
0
    continue;
7175
0
  }
7176
7177
      /* Check if vector operands are valid.  */
7178
82.6k
      if (check_VecOperands (t))
7179
0
  {
7180
0
    specific_error = progress (i.error);
7181
0
    continue;
7182
0
  }
7183
7184
      /* We've found a match; break out of loop.  */
7185
82.6k
      break;
7186
82.6k
    }
7187
7188
224k
#undef progress
7189
7190
224k
  if (t == current_templates->end)
7191
37.0k
    {
7192
      /* We found no match.  */
7193
37.0k
      i.error = specific_error;
7194
37.0k
      return NULL;
7195
37.0k
    }
7196
7197
187k
  if (!quiet_warnings)
7198
187k
    {
7199
187k
      if (!intel_syntax
7200
187k
    && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
7201
0
  as_warn (_("indirect %s without `*'"), insn_name (t));
7202
7203
187k
      if (t->opcode_modifier.isprefix
7204
187k
    && t->opcode_modifier.mnemonicsize == IGNORESIZE)
7205
0
  {
7206
    /* Warn them that a data or address size prefix doesn't
7207
       affect assembly of the next line of code.  */
7208
0
    as_warn (_("stand-alone `%s' prefix"), insn_name (t));
7209
0
  }
7210
187k
    }
7211
7212
  /* Copy the template we found.  */
7213
187k
  install_template (t);
7214
7215
187k
  if (addr_prefix_disp != -1)
7216
76.1k
    i.tm.operand_types[addr_prefix_disp]
7217
76.1k
      = operand_types[addr_prefix_disp];
7218
7219
187k
  switch (found_reverse_match)
7220
187k
    {
7221
187k
    case 0:
7222
187k
      break;
7223
7224
0
    case Opcode_FloatR:
7225
0
    case Opcode_FloatR | Opcode_FloatD:
7226
0
      i.tm.extension_opcode ^= Opcode_FloatR >> 3;
7227
0
      found_reverse_match &= Opcode_FloatD;
7228
7229
      /* Fall through.  */
7230
3
    default:
7231
      /* If we found a reverse match we must alter the opcode direction
7232
   bit and clear/flip the regmem modifier one.  found_reverse_match
7233
   holds bits to change (different for int & float insns).  */
7234
7235
3
      i.tm.base_opcode ^= found_reverse_match;
7236
7237
      /* Certain SIMD insns have their load forms specified in the opcode
7238
   table, and hence we need to _set_ RegMem instead of clearing it.
7239
   We need to avoid setting the bit though on insns like KMOVW.  */
7240
3
      i.tm.opcode_modifier.regmem
7241
3
  = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
7242
3
    && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
7243
3
    && !i.tm.opcode_modifier.regmem;
7244
7245
      /* Fall through.  */
7246
3
    case ~0:
7247
3
      i.tm.operand_types[0] = operand_types[i.operands - 1];
7248
3
      i.tm.operand_types[i.operands - 1] = operand_types[0];
7249
3
      break;
7250
7251
0
    case Opcode_VexW:
7252
      /* Only the first two register operands need reversing, alongside
7253
   flipping VEX.W.  */
7254
0
      i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
7255
7256
0
      j = i.tm.operand_types[0].bitfield.imm8;
7257
0
      i.tm.operand_types[j] = operand_types[j + 1];
7258
0
      i.tm.operand_types[j + 1] = operand_types[j];
7259
0
      break;
7260
187k
    }
7261
7262
187k
  return t;
7263
187k
}
7264
7265
static int
7266
check_string (void)
7267
839
{
7268
839
  unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
7269
839
  unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
7270
7271
839
  if (i.seg[op] != NULL && i.seg[op] != reg_es)
7272
0
    {
7273
0
      as_bad (_("`%s' operand %u must use `%ses' segment"),
7274
0
        insn_name (&i.tm),
7275
0
        intel_syntax ? i.tm.operands - es_op : es_op + 1,
7276
0
        register_prefix);
7277
0
      return 0;
7278
0
    }
7279
7280
  /* There's only ever one segment override allowed per instruction.
7281
     This instruction possibly has a legal segment override on the
7282
     second operand, so copy the segment to where non-string
7283
     instructions store it, allowing common code.  */
7284
839
  i.seg[op] = i.seg[1];
7285
7286
839
  return 1;
7287
839
}
7288
7289
static int
7290
process_suffix (void)
7291
187k
{
7292
187k
  bool is_movx = false;
7293
7294
  /* If matched instruction specifies an explicit instruction mnemonic
7295
     suffix, use it.  */
7296
187k
  if (i.tm.opcode_modifier.size == SIZE16)
7297
0
    i.suffix = WORD_MNEM_SUFFIX;
7298
187k
  else if (i.tm.opcode_modifier.size == SIZE32)
7299
0
    i.suffix = LONG_MNEM_SUFFIX;
7300
187k
  else if (i.tm.opcode_modifier.size == SIZE64)
7301
145
    i.suffix = QWORD_MNEM_SUFFIX;
7302
186k
  else if (i.reg_operands
7303
186k
     && (i.operands > 1 || i.types[0].bitfield.class == Reg)
7304
186k
     && i.tm.opcode_modifier.operandconstraint != ADDR_PREFIX_OP_REG)
7305
4
    {
7306
4
      unsigned int numop = i.operands;
7307
7308
      /* MOVSX/MOVZX */
7309
4
      is_movx = (i.tm.opcode_space == SPACE_0F
7310
4
     && (i.tm.base_opcode | 8) == 0xbe)
7311
4
    || (i.tm.opcode_space == SPACE_BASE
7312
4
        && i.tm.base_opcode == 0x63
7313
4
        && i.tm.cpu_flags.bitfield.cpu64);
7314
7315
      /* movsx/movzx want only their source operand considered here, for the
7316
   ambiguity checking below.  The suffix will be replaced afterwards
7317
   to represent the destination (register).  */
7318
4
      if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
7319
0
  --i.operands;
7320
7321
      /* crc32 needs REX.W set regardless of suffix / source operand size.  */
7322
4
      if (i.tm.mnem_off == MN_crc32 && i.tm.operand_types[1].bitfield.qword)
7323
0
        i.rex |= REX_W;
7324
7325
      /* If there's no instruction mnemonic suffix we try to invent one
7326
   based on GPR operands.  */
7327
4
      if (!i.suffix)
7328
4
  {
7329
    /* We take i.suffix from the last register operand specified,
7330
       Destination register type is more significant than source
7331
       register type.  crc32 in SSE4.2 prefers source register
7332
       type. */
7333
4
    unsigned int op = i.tm.mnem_off == MN_crc32 ? 1 : i.operands;
7334
7335
4
    while (op--)
7336
4
      if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7337
4
    || i.tm.operand_types[op].bitfield.instance == Accum)
7338
4
        {
7339
4
    if (i.types[op].bitfield.class != Reg)
7340
0
      continue;
7341
4
    if (i.types[op].bitfield.byte)
7342
0
      i.suffix = BYTE_MNEM_SUFFIX;
7343
4
    else if (i.types[op].bitfield.word)
7344
4
      i.suffix = WORD_MNEM_SUFFIX;
7345
0
    else if (i.types[op].bitfield.dword)
7346
0
      i.suffix = LONG_MNEM_SUFFIX;
7347
0
    else if (i.types[op].bitfield.qword)
7348
0
      i.suffix = QWORD_MNEM_SUFFIX;
7349
0
    else
7350
0
      continue;
7351
4
    break;
7352
4
        }
7353
7354
    /* As an exception, movsx/movzx silently default to a byte source
7355
       in AT&T mode.  */
7356
4
    if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
7357
0
      i.suffix = BYTE_MNEM_SUFFIX;
7358
4
  }
7359
0
      else if (i.suffix == BYTE_MNEM_SUFFIX)
7360
0
  {
7361
0
    if (!check_byte_reg ())
7362
0
      return 0;
7363
0
  }
7364
0
      else if (i.suffix == LONG_MNEM_SUFFIX)
7365
0
  {
7366
0
    if (!check_long_reg ())
7367
0
      return 0;
7368
0
  }
7369
0
      else if (i.suffix == QWORD_MNEM_SUFFIX)
7370
0
  {
7371
0
    if (!check_qword_reg ())
7372
0
      return 0;
7373
0
  }
7374
0
      else if (i.suffix == WORD_MNEM_SUFFIX)
7375
0
  {
7376
0
    if (!check_word_reg ())
7377
0
      return 0;
7378
0
  }
7379
0
      else if (intel_syntax
7380
0
         && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
7381
  /* Do nothing if the instruction is going to ignore the prefix.  */
7382
0
  ;
7383
0
      else
7384
0
  abort ();
7385
7386
      /* Undo the movsx/movzx change done above.  */
7387
4
      i.operands = numop;
7388
4
    }
7389
186k
  else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7390
186k
     && !i.suffix)
7391
66
    {
7392
66
      i.suffix = stackop_size;
7393
66
      if (stackop_size == LONG_MNEM_SUFFIX)
7394
0
  {
7395
    /* stackop_size is set to LONG_MNEM_SUFFIX for the
7396
       .code16gcc directive to support 16-bit mode with
7397
       32-bit address.  For IRET without a suffix, generate
7398
       16-bit IRET (opcode 0xcf) to return from an interrupt
7399
       handler.  */
7400
0
    if (i.tm.base_opcode == 0xcf)
7401
0
      {
7402
0
        i.suffix = WORD_MNEM_SUFFIX;
7403
0
        as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7404
0
      }
7405
    /* Warn about changed behavior for segment register push/pop.  */
7406
0
    else if ((i.tm.base_opcode | 1) == 0x07)
7407
0
      as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
7408
0
         insn_name (&i.tm));
7409
0
  }
7410
66
    }
7411
186k
  else if (!i.suffix
7412
186k
     && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7413
185k
         || i.tm.opcode_modifier.jump == JUMP_BYTE
7414
185k
         || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
7415
185k
         || (i.tm.opcode_space == SPACE_0F
7416
185k
       && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
7417
185k
       && i.tm.extension_opcode <= 3)))
7418
329
    {
7419
329
      switch (flag_code)
7420
329
  {
7421
257
  case CODE_64BIT:
7422
257
    if (!i.tm.opcode_modifier.no_qsuf)
7423
257
      {
7424
257
        if (i.tm.opcode_modifier.jump == JUMP_BYTE
7425
257
      || i.tm.opcode_modifier.no_lsuf)
7426
257
    i.suffix = QWORD_MNEM_SUFFIX;
7427
257
        break;
7428
257
      }
7429
    /* Fall through.  */
7430
14
  case CODE_32BIT:
7431
14
    if (!i.tm.opcode_modifier.no_lsuf)
7432
14
      i.suffix = LONG_MNEM_SUFFIX;
7433
14
    break;
7434
58
  case CODE_16BIT:
7435
58
    if (!i.tm.opcode_modifier.no_wsuf)
7436
58
      i.suffix = WORD_MNEM_SUFFIX;
7437
58
    break;
7438
329
  }
7439
329
    }
7440
7441
187k
  if (!i.suffix
7442
187k
      && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
7443
    /* Also cover lret/retf/iret in 64-bit mode.  */
7444
185k
    || (flag_code == CODE_64BIT
7445
66
        && !i.tm.opcode_modifier.no_lsuf
7446
66
        && !i.tm.opcode_modifier.no_qsuf))
7447
187k
      && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
7448
      /* Explicit sizing prefixes are assumed to disambiguate insns.  */
7449
187k
      && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
7450
      /* Accept FLDENV et al without suffix.  */
7451
187k
      && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
7452
183k
    {
7453
183k
      unsigned int suffixes, evex = 0;
7454
7455
183k
      suffixes = !i.tm.opcode_modifier.no_bsuf;
7456
183k
      if (!i.tm.opcode_modifier.no_wsuf)
7457
73.9k
  suffixes |= 1 << 1;
7458
183k
      if (!i.tm.opcode_modifier.no_lsuf)
7459
74.1k
  suffixes |= 1 << 2;
7460
183k
      if (!i.tm.opcode_modifier.no_ssuf)
7461
272
  suffixes |= 1 << 4;
7462
183k
      if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7463
28.5k
  suffixes |= 1 << 5;
7464
7465
      /* For [XYZ]MMWORD operands inspect operand sizes.  While generally
7466
   also suitable for AT&T syntax mode, it was requested that this be
7467
   restricted to just Intel syntax.  */
7468
183k
      if (intel_syntax && is_any_vex_encoding (&i.tm)
7469
183k
    && !i.broadcast.type && !i.broadcast.bytes)
7470
0
  {
7471
0
    unsigned int op;
7472
7473
0
    for (op = 0; op < i.tm.operands; ++op)
7474
0
      {
7475
0
        if (is_evex_encoding (&i.tm)
7476
0
      && !cpu_arch_flags.bitfield.cpuavx512vl)
7477
0
    {
7478
0
      if (i.tm.operand_types[op].bitfield.ymmword)
7479
0
        i.tm.operand_types[op].bitfield.xmmword = 0;
7480
0
      if (i.tm.operand_types[op].bitfield.zmmword)
7481
0
        i.tm.operand_types[op].bitfield.ymmword = 0;
7482
0
      if (!i.tm.opcode_modifier.evex
7483
0
          || i.tm.opcode_modifier.evex == EVEXDYN)
7484
0
        i.tm.opcode_modifier.evex = EVEX512;
7485
0
    }
7486
7487
0
        if (i.tm.operand_types[op].bitfield.xmmword
7488
0
      + i.tm.operand_types[op].bitfield.ymmword
7489
0
      + i.tm.operand_types[op].bitfield.zmmword < 2)
7490
0
    continue;
7491
7492
        /* Any properly sized operand disambiguates the insn.  */
7493
0
        if (i.types[op].bitfield.xmmword
7494
0
      || i.types[op].bitfield.ymmword
7495
0
      || i.types[op].bitfield.zmmword)
7496
0
    {
7497
0
      suffixes &= ~(7 << 6);
7498
0
      evex = 0;
7499
0
      break;
7500
0
    }
7501
7502
0
        if ((i.flags[op] & Operand_Mem)
7503
0
      && i.tm.operand_types[op].bitfield.unspecified)
7504
0
    {
7505
0
      if (i.tm.operand_types[op].bitfield.xmmword)
7506
0
        suffixes |= 1 << 6;
7507
0
      if (i.tm.operand_types[op].bitfield.ymmword)
7508
0
        suffixes |= 1 << 7;
7509
0
      if (i.tm.operand_types[op].bitfield.zmmword)
7510
0
        suffixes |= 1 << 8;
7511
0
      if (is_evex_encoding (&i.tm))
7512
0
        evex = EVEX512;
7513
0
    }
7514
0
      }
7515
0
  }
7516
7517
      /* Are multiple suffixes / operand sizes allowed?  */
7518
183k
      if (suffixes & (suffixes - 1))
7519
74.1k
  {
7520
74.1k
    if (intel_syntax
7521
74.1k
        && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
7522
0
      || operand_check == check_error))
7523
0
      {
7524
0
        as_bad (_("ambiguous operand size for `%s'"), insn_name (&i.tm));
7525
0
        return 0;
7526
0
      }
7527
74.1k
    if (operand_check == check_error)
7528
0
      {
7529
0
        as_bad (_("no instruction mnemonic suffix given and "
7530
0
      "no register operands; can't size `%s'"), insn_name (&i.tm));
7531
0
        return 0;
7532
0
      }
7533
74.1k
    if (operand_check == check_warning)
7534
74.1k
      as_warn (_("%s; using default for `%s'"),
7535
74.1k
           intel_syntax
7536
74.1k
           ? _("ambiguous operand size")
7537
74.1k
           : _("no instruction mnemonic suffix given and "
7538
74.1k
         "no register operands"),
7539
74.1k
           insn_name (&i.tm));
7540
7541
74.1k
    if (i.tm.opcode_modifier.floatmf)
7542
272
      i.suffix = SHORT_MNEM_SUFFIX;
7543
73.9k
    else if (is_movx)
7544
0
      /* handled below */;
7545
73.9k
    else if (evex)
7546
0
      i.tm.opcode_modifier.evex = evex;
7547
73.9k
    else if (flag_code == CODE_16BIT)
7548
44.3k
      i.suffix = WORD_MNEM_SUFFIX;
7549
29.5k
    else if (!i.tm.opcode_modifier.no_lsuf)
7550
29.5k
      i.suffix = LONG_MNEM_SUFFIX;
7551
0
    else
7552
0
      i.suffix = QWORD_MNEM_SUFFIX;
7553
74.1k
  }
7554
183k
    }
7555
7556
187k
  if (is_movx)
7557
0
    {
7558
      /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7559
   In AT&T syntax, if there is no suffix (warned about above), the default
7560
   will be byte extension.  */
7561
0
      if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7562
0
  i.tm.base_opcode |= 1;
7563
7564
      /* For further processing, the suffix should represent the destination
7565
   (register).  This is already the case when one was used with
7566
   mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7567
   no suffix to begin with.  */
7568
0
      if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7569
0
  {
7570
0
    if (i.types[1].bitfield.word)
7571
0
      i.suffix = WORD_MNEM_SUFFIX;
7572
0
    else if (i.types[1].bitfield.qword)
7573
0
      i.suffix = QWORD_MNEM_SUFFIX;
7574
0
    else
7575
0
      i.suffix = LONG_MNEM_SUFFIX;
7576
7577
0
    i.tm.opcode_modifier.w = 0;
7578
0
  }
7579
0
    }
7580
7581
187k
  if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7582
0
    i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7583
0
       != (i.tm.operand_types[1].bitfield.class == Reg);
7584
7585
  /* Change the opcode based on the operand size given by i.suffix.  */
7586
187k
  switch (i.suffix)
7587
187k
    {
7588
    /* Size floating point instruction.  */
7589
29.5k
    case LONG_MNEM_SUFFIX:
7590
29.5k
      if (i.tm.opcode_modifier.floatmf)
7591
0
  {
7592
0
    i.tm.base_opcode ^= 4;
7593
0
    break;
7594
0
  }
7595
    /* fall through */
7596
73.9k
    case WORD_MNEM_SUFFIX:
7597
74.4k
    case QWORD_MNEM_SUFFIX:
7598
      /* It's not a byte, select word/dword operation.  */
7599
74.4k
      if (i.tm.opcode_modifier.w)
7600
12.5k
  {
7601
12.5k
    if (i.short_form)
7602
0
      i.tm.base_opcode |= 8;
7603
12.5k
    else
7604
12.5k
      i.tm.base_opcode |= 1;
7605
12.5k
  }
7606
    /* fall through */
7607
74.6k
    case SHORT_MNEM_SUFFIX:
7608
      /* Now select between word & dword operations via the operand
7609
   size prefix, except for instructions that will ignore this
7610
   prefix anyway.  */
7611
74.6k
      if (i.suffix != QWORD_MNEM_SUFFIX
7612
74.6k
    && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
7613
74.6k
    && !i.tm.opcode_modifier.floatmf
7614
74.6k
    && !is_any_vex_encoding (&i.tm)
7615
74.6k
    && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7616
73.9k
        || (flag_code == CODE_64BIT
7617
73.9k
      && i.tm.opcode_modifier.jump == JUMP_BYTE)))
7618
2
  {
7619
2
    unsigned int prefix = DATA_PREFIX_OPCODE;
7620
7621
2
    if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
7622
0
      prefix = ADDR_PREFIX_OPCODE;
7623
7624
2
    if (!add_prefix (prefix))
7625
0
      return 0;
7626
2
  }
7627
7628
      /* Set mode64 for an operand.  */
7629
74.6k
      if (i.suffix == QWORD_MNEM_SUFFIX
7630
74.6k
    && flag_code == CODE_64BIT
7631
74.6k
    && !i.tm.opcode_modifier.norex64
7632
74.6k
    && !i.tm.opcode_modifier.vexw
7633
    /* Special case for xchg %rax,%rax.  It is NOP and doesn't
7634
       need rex64. */
7635
74.6k
    && ! (i.operands == 2
7636
154
    && i.tm.base_opcode == 0x90
7637
154
    && i.tm.opcode_space == SPACE_BASE
7638
154
    && i.types[0].bitfield.instance == Accum
7639
154
    && i.types[0].bitfield.qword
7640
154
    && i.types[1].bitfield.instance == Accum))
7641
154
  i.rex |= REX_W;
7642
7643
74.6k
      break;
7644
7645
111k
    case 0:
7646
      /* Select word/dword/qword operation with explicit data sizing prefix
7647
   when there are no suitable register operands.  */
7648
111k
      if (i.tm.opcode_modifier.w
7649
111k
    && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7650
111k
    && (!i.reg_operands
7651
0
        || (i.reg_operands == 1
7652
          /* ShiftCount */
7653
0
      && (i.tm.operand_types[0].bitfield.instance == RegC
7654
          /* InOutPortReg */
7655
0
          || i.tm.operand_types[0].bitfield.instance == RegD
7656
0
          || i.tm.operand_types[1].bitfield.instance == RegD
7657
0
          || i.tm.mnem_off == MN_crc32))))
7658
0
  i.tm.base_opcode |= 1;
7659
111k
      break;
7660
187k
    }
7661
7662
187k
  if (i.tm.opcode_modifier.operandconstraint == ADDR_PREFIX_OP_REG)
7663
0
    {
7664
0
      gas_assert (!i.suffix);
7665
0
      gas_assert (i.reg_operands);
7666
7667
0
      if (i.tm.operand_types[0].bitfield.instance == Accum
7668
0
    || i.operands == 1)
7669
0
  {
7670
    /* The address size override prefix changes the size of the
7671
       first operand.  */
7672
0
    if (flag_code == CODE_64BIT
7673
0
        && i.op[0].regs->reg_type.bitfield.word)
7674
0
      {
7675
0
        as_bad (_("16-bit addressing unavailable for `%s'"),
7676
0
          insn_name (&i.tm));
7677
0
        return 0;
7678
0
      }
7679
7680
0
    if ((flag_code == CODE_32BIT
7681
0
         ? i.op[0].regs->reg_type.bitfield.word
7682
0
         : i.op[0].regs->reg_type.bitfield.dword)
7683
0
        && !add_prefix (ADDR_PREFIX_OPCODE))
7684
0
      return 0;
7685
0
  }
7686
0
      else
7687
0
  {
7688
    /* Check invalid register operand when the address size override
7689
       prefix changes the size of register operands.  */
7690
0
    unsigned int op;
7691
0
    enum { need_word, need_dword, need_qword } need;
7692
7693
    /* Check the register operand for the address size prefix if
7694
       the memory operand has no real registers, like symbol, DISP
7695
       or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant.  */
7696
0
    if (i.mem_operands == 1
7697
0
        && i.reg_operands == 1
7698
0
        && i.operands == 2
7699
0
        && i.types[1].bitfield.class == Reg
7700
0
        && (flag_code == CODE_32BIT
7701
0
      ? i.op[1].regs->reg_type.bitfield.word
7702
0
      : i.op[1].regs->reg_type.bitfield.dword)
7703
0
        && ((i.base_reg == NULL && i.index_reg == NULL)
7704
0
#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7705
0
      || (x86_elf_abi == X86_64_X32_ABI
7706
0
          && i.base_reg
7707
0
          && i.base_reg->reg_num == RegIP
7708
0
          && i.base_reg->reg_type.bitfield.qword))
7709
#else
7710
      || 0)
7711
#endif
7712
0
        && !add_prefix (ADDR_PREFIX_OPCODE))
7713
0
      return 0;
7714
7715
0
    if (flag_code == CODE_32BIT)
7716
0
      need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7717
0
    else if (i.prefix[ADDR_PREFIX])
7718
0
      need = need_dword;
7719
0
    else
7720
0
      need = flag_code == CODE_64BIT ? need_qword : need_word;
7721
7722
0
    for (op = 0; op < i.operands; op++)
7723
0
      {
7724
0
        if (i.types[op].bitfield.class != Reg)
7725
0
    continue;
7726
7727
0
        switch (need)
7728
0
    {
7729
0
    case need_word:
7730
0
      if (i.op[op].regs->reg_type.bitfield.word)
7731
0
        continue;
7732
0
      break;
7733
0
    case need_dword:
7734
0
      if (i.op[op].regs->reg_type.bitfield.dword)
7735
0
        continue;
7736
0
      break;
7737
0
    case need_qword:
7738
0
      if (i.op[op].regs->reg_type.bitfield.qword)
7739
0
        continue;
7740
0
      break;
7741
0
    }
7742
7743
0
        as_bad (_("invalid register operand size for `%s'"),
7744
0
          insn_name (&i.tm));
7745
0
        return 0;
7746
0
      }
7747
0
  }
7748
0
    }
7749
7750
187k
  return 1;
7751
187k
}
7752
7753
static int
7754
check_byte_reg (void)
7755
0
{
7756
0
  int op;
7757
7758
0
  for (op = i.operands; --op >= 0;)
7759
0
    {
7760
      /* Skip non-register operands. */
7761
0
      if (i.types[op].bitfield.class != Reg)
7762
0
  continue;
7763
7764
      /* If this is an eight bit register, it's OK.  If it's the 16 or
7765
   32 bit version of an eight bit register, we will just use the
7766
   low portion, and that's OK too.  */
7767
0
      if (i.types[op].bitfield.byte)
7768
0
  continue;
7769
7770
      /* I/O port address operands are OK too.  */
7771
0
      if (i.tm.operand_types[op].bitfield.instance == RegD
7772
0
    && i.tm.operand_types[op].bitfield.word)
7773
0
  continue;
7774
7775
      /* crc32 only wants its source operand checked here.  */
7776
0
      if (i.tm.mnem_off == MN_crc32 && op != 0)
7777
0
  continue;
7778
7779
      /* Any other register is bad.  */
7780
0
      as_bad (_("`%s%s' not allowed with `%s%c'"),
7781
0
        register_prefix, i.op[op].regs->reg_name,
7782
0
        insn_name (&i.tm), i.suffix);
7783
0
      return 0;
7784
0
    }
7785
0
  return 1;
7786
0
}
7787
7788
static int
7789
check_long_reg (void)
7790
0
{
7791
0
  int op;
7792
7793
0
  for (op = i.operands; --op >= 0;)
7794
    /* Skip non-register operands. */
7795
0
    if (i.types[op].bitfield.class != Reg)
7796
0
      continue;
7797
    /* Reject eight bit registers, except where the template requires
7798
       them. (eg. movzb)  */
7799
0
    else if (i.types[op].bitfield.byte
7800
0
       && (i.tm.operand_types[op].bitfield.class == Reg
7801
0
     || i.tm.operand_types[op].bitfield.instance == Accum)
7802
0
       && (i.tm.operand_types[op].bitfield.word
7803
0
     || i.tm.operand_types[op].bitfield.dword))
7804
0
      {
7805
0
  as_bad (_("`%s%s' not allowed with `%s%c'"),
7806
0
    register_prefix,
7807
0
    i.op[op].regs->reg_name,
7808
0
    insn_name (&i.tm),
7809
0
    i.suffix);
7810
0
  return 0;
7811
0
      }
7812
    /* Error if the e prefix on a general reg is missing.  */
7813
0
    else if (i.types[op].bitfield.word
7814
0
       && (i.tm.operand_types[op].bitfield.class == Reg
7815
0
     || i.tm.operand_types[op].bitfield.instance == Accum)
7816
0
       && i.tm.operand_types[op].bitfield.dword)
7817
0
      {
7818
0
  as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7819
0
    register_prefix, i.op[op].regs->reg_name,
7820
0
    i.suffix);
7821
0
  return 0;
7822
0
      }
7823
    /* Warn if the r prefix on a general reg is present.  */
7824
0
    else if (i.types[op].bitfield.qword
7825
0
       && (i.tm.operand_types[op].bitfield.class == Reg
7826
0
     || i.tm.operand_types[op].bitfield.instance == Accum)
7827
0
       && i.tm.operand_types[op].bitfield.dword)
7828
0
      {
7829
0
  as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7830
0
    register_prefix, i.op[op].regs->reg_name, i.suffix);
7831
0
  return 0;
7832
0
      }
7833
0
  return 1;
7834
0
}
7835
7836
static int
7837
check_qword_reg (void)
7838
0
{
7839
0
  int op;
7840
7841
0
  for (op = i.operands; --op >= 0; )
7842
    /* Skip non-register operands. */
7843
0
    if (i.types[op].bitfield.class != Reg)
7844
0
      continue;
7845
    /* Reject eight bit registers, except where the template requires
7846
       them. (eg. movzb)  */
7847
0
    else if (i.types[op].bitfield.byte
7848
0
       && (i.tm.operand_types[op].bitfield.class == Reg
7849
0
     || i.tm.operand_types[op].bitfield.instance == Accum)
7850
0
       && (i.tm.operand_types[op].bitfield.word
7851
0
     || i.tm.operand_types[op].bitfield.dword))
7852
0
      {
7853
0
  as_bad (_("`%s%s' not allowed with `%s%c'"),
7854
0
    register_prefix,
7855
0
    i.op[op].regs->reg_name,
7856
0
    insn_name (&i.tm),
7857
0
    i.suffix);
7858
0
  return 0;
7859
0
      }
7860
    /* Warn if the r prefix on a general reg is missing.  */
7861
0
    else if ((i.types[op].bitfield.word
7862
0
        || i.types[op].bitfield.dword)
7863
0
       && (i.tm.operand_types[op].bitfield.class == Reg
7864
0
     || i.tm.operand_types[op].bitfield.instance == Accum)
7865
0
       && i.tm.operand_types[op].bitfield.qword)
7866
0
      {
7867
  /* Prohibit these changes in the 64bit mode, since the
7868
     lowering is more complicated.  */
7869
0
  as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7870
0
    register_prefix, i.op[op].regs->reg_name, i.suffix);
7871
0
  return 0;
7872
0
      }
7873
0
  return 1;
7874
0
}
7875
7876
static int
7877
check_word_reg (void)
7878
0
{
7879
0
  int op;
7880
0
  for (op = i.operands; --op >= 0;)
7881
    /* Skip non-register operands. */
7882
0
    if (i.types[op].bitfield.class != Reg)
7883
0
      continue;
7884
    /* Reject eight bit registers, except where the template requires
7885
       them. (eg. movzb)  */
7886
0
    else if (i.types[op].bitfield.byte
7887
0
       && (i.tm.operand_types[op].bitfield.class == Reg
7888
0
     || i.tm.operand_types[op].bitfield.instance == Accum)
7889
0
       && (i.tm.operand_types[op].bitfield.word
7890
0
     || i.tm.operand_types[op].bitfield.dword))
7891
0
      {
7892
0
  as_bad (_("`%s%s' not allowed with `%s%c'"),
7893
0
    register_prefix,
7894
0
    i.op[op].regs->reg_name,
7895
0
    insn_name (&i.tm),
7896
0
    i.suffix);
7897
0
  return 0;
7898
0
      }
7899
    /* Error if the e or r prefix on a general reg is present.  */
7900
0
    else if ((i.types[op].bitfield.dword
7901
0
     || i.types[op].bitfield.qword)
7902
0
       && (i.tm.operand_types[op].bitfield.class == Reg
7903
0
     || i.tm.operand_types[op].bitfield.instance == Accum)
7904
0
       && i.tm.operand_types[op].bitfield.word)
7905
0
      {
7906
0
  as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
7907
0
    register_prefix, i.op[op].regs->reg_name,
7908
0
    i.suffix);
7909
0
  return 0;
7910
0
      }
7911
0
  return 1;
7912
0
}
7913
7914
static int
7915
update_imm (unsigned int j)
7916
155k
{
7917
155k
  i386_operand_type overlap = i.types[j];
7918
7919
155k
  if (i.tm.operand_types[j].bitfield.imm8
7920
155k
      && i.tm.operand_types[j].bitfield.imm8s
7921
155k
      && overlap.bitfield.imm8 && overlap.bitfield.imm8s)
7922
0
    {
7923
      /* This combination is used on 8-bit immediates where e.g. $~0 is
7924
   desirable to permit.  We're past operand type matching, so simply
7925
   put things back in the shape they were before introducing the
7926
   distinction between Imm8, Imm8S, and Imm8|Imm8S.  */
7927
0
      overlap.bitfield.imm8s = 0;
7928
0
    }
7929
7930
155k
  if (overlap.bitfield.imm8
7931
155k
      + overlap.bitfield.imm8s
7932
155k
      + overlap.bitfield.imm16
7933
155k
      + overlap.bitfield.imm32
7934
155k
      + overlap.bitfield.imm32s
7935
155k
      + overlap.bitfield.imm64 > 1)
7936
177
    {
7937
177
      static const i386_operand_type imm16 = { .bitfield = { .imm16 = 1 } };
7938
177
      static const i386_operand_type imm32 = { .bitfield = { .imm32 = 1 } };
7939
177
      static const i386_operand_type imm32s = { .bitfield = { .imm32s = 1 } };
7940
177
      static const i386_operand_type imm16_32 = { .bitfield =
7941
177
  { .imm16 = 1, .imm32 = 1 }
7942
177
      };
7943
177
      static const i386_operand_type imm16_32s =  { .bitfield =
7944
177
  { .imm16 = 1, .imm32s = 1 }
7945
177
      };
7946
177
      static const i386_operand_type imm16_32_32s = { .bitfield =
7947
177
  { .imm16 = 1, .imm32 = 1, .imm32s = 1 }
7948
177
      };
7949
7950
177
      if (i.suffix)
7951
177
  {
7952
177
    i386_operand_type temp;
7953
7954
177
    operand_type_set (&temp, 0);
7955
177
    if (i.suffix == BYTE_MNEM_SUFFIX)
7956
0
      {
7957
0
        temp.bitfield.imm8 = overlap.bitfield.imm8;
7958
0
        temp.bitfield.imm8s = overlap.bitfield.imm8s;
7959
0
      }
7960
177
    else if (i.suffix == WORD_MNEM_SUFFIX)
7961
100
      temp.bitfield.imm16 = overlap.bitfield.imm16;
7962
77
    else if (i.suffix == QWORD_MNEM_SUFFIX)
7963
0
      {
7964
0
        temp.bitfield.imm64 = overlap.bitfield.imm64;
7965
0
        temp.bitfield.imm32s = overlap.bitfield.imm32s;
7966
0
      }
7967
77
    else
7968
77
      temp.bitfield.imm32 = overlap.bitfield.imm32;
7969
177
    overlap = temp;
7970
177
  }
7971
0
      else if (operand_type_equal (&overlap, &imm16_32_32s)
7972
0
         || operand_type_equal (&overlap, &imm16_32)
7973
0
         || operand_type_equal (&overlap, &imm16_32s))
7974
0
  {
7975
0
    if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
7976
0
      overlap = imm16;
7977
0
    else
7978
0
      overlap = imm32s;
7979
0
  }
7980
0
      else if (i.prefix[REX_PREFIX] & REX_W)
7981
0
  overlap = operand_type_and (overlap, imm32s);
7982
0
      else if (i.prefix[DATA_PREFIX])
7983
0
  overlap = operand_type_and (overlap,
7984
0
            flag_code != CODE_16BIT ? imm16 : imm32);
7985
177
      if (overlap.bitfield.imm8
7986
177
    + overlap.bitfield.imm8s
7987
177
    + overlap.bitfield.imm16
7988
177
    + overlap.bitfield.imm32
7989
177
    + overlap.bitfield.imm32s
7990
177
    + overlap.bitfield.imm64 != 1)
7991
0
  {
7992
0
    as_bad (_("no instruction mnemonic suffix given; "
7993
0
        "can't determine immediate size"));
7994
0
    return 0;
7995
0
  }
7996
177
    }
7997
155k
  i.types[j] = overlap;
7998
7999
155k
  return 1;
8000
155k
}
8001
8002
static int
8003
finalize_imm (void)
8004
187k
{
8005
187k
  unsigned int j, n;
8006
8007
  /* Update the first 2 immediate operands.  */
8008
187k
  n = i.operands > 2 ? 2 : i.operands;
8009
187k
  if (n)
8010
82.6k
    {
8011
238k
      for (j = 0; j < n; j++)
8012
155k
  if (update_imm (j) == 0)
8013
0
    return 0;
8014
8015
      /* The 3rd operand can't be immediate operand.  */
8016
82.6k
      gas_assert (operand_type_check (i.types[2], imm) == 0);
8017
82.6k
    }
8018
8019
187k
  return 1;
8020
187k
}
8021
8022
static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
8023
         bool do_sse2avx)
8024
4
{
8025
4
  if (r->reg_flags & RegRex)
8026
0
    {
8027
0
      if (i.rex & rex_bit)
8028
0
  as_bad (_("same type of prefix used twice"));
8029
0
      i.rex |= rex_bit;
8030
0
    }
8031
4
  else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
8032
0
    {
8033
0
      gas_assert (i.vex.register_specifier == r);
8034
0
      i.vex.register_specifier += 8;
8035
0
    }
8036
8037
4
  if (r->reg_flags & RegVRex)
8038
0
    i.vrex |= rex_bit;
8039
4
}
8040
8041
static int
8042
process_operands (void)
8043
82.6k
{
8044
  /* Default segment register this instruction will use for memory
8045
     accesses.  0 means unknown.  This is only for optimizing out
8046
     unnecessary segment overrides.  */
8047
82.6k
  const reg_entry *default_seg = NULL;
8048
8049
  /* We only need to check those implicit registers for instructions
8050
     with 3 operands or less.  */
8051
82.6k
  if (i.operands <= 3)
8052
238k
    for (unsigned int j = 0; j < i.operands; j++)
8053
155k
      if (i.types[j].bitfield.instance != InstanceNone)
8054
0
  i.reg_operands--;
8055
8056
82.6k
  if (i.tm.opcode_modifier.sse2avx)
8057
0
    {
8058
      /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
8059
   need converting.  */
8060
0
      i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
8061
0
      i.prefix[REX_PREFIX] = 0;
8062
0
      i.rex_encoding = 0;
8063
0
    }
8064
  /* ImmExt should be processed after SSE2AVX.  */
8065
82.6k
  else if (i.tm.opcode_modifier.immext)
8066
0
    process_immext ();
8067
8068
  /* TILEZERO is unusual in that it has a single operand encoded in ModR/M.reg,
8069
     not ModR/M.rm.  To avoid special casing this in build_modrm_byte(), fake a
8070
     new destination operand here, while converting the source one to register
8071
     number 0.  */
8072
82.6k
  if (i.tm.mnem_off == MN_tilezero)
8073
0
    {
8074
0
      i.op[1].regs = i.op[0].regs;
8075
0
      i.op[0].regs -= i.op[0].regs->reg_num;
8076
0
      i.types[1] = i.types[0];
8077
0
      i.tm.operand_types[1] = i.tm.operand_types[0];
8078
0
      i.flags[1] = i.flags[0];
8079
0
      i.operands++;
8080
0
      i.reg_operands++;
8081
0
      i.tm.operands++;
8082
0
    }
8083
8084
82.6k
  if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
8085
0
    {
8086
0
      static const i386_operand_type regxmm = {
8087
0
        .bitfield = { .class = RegSIMD, .xmmword = 1 }
8088
0
      };
8089
0
      unsigned int dupl = i.operands;
8090
0
      unsigned int dest = dupl - 1;
8091
0
      unsigned int j;
8092
8093
      /* The destination must be an xmm register.  */
8094
0
      gas_assert (i.reg_operands
8095
0
      && MAX_OPERANDS > dupl
8096
0
      && operand_type_equal (&i.types[dest], &regxmm));
8097
8098
0
      if (i.tm.operand_types[0].bitfield.instance == Accum
8099
0
    && i.tm.operand_types[0].bitfield.xmmword)
8100
0
  {
8101
    /* Keep xmm0 for instructions with VEX prefix and 3
8102
       sources.  */
8103
0
    i.tm.operand_types[0].bitfield.instance = InstanceNone;
8104
0
    i.tm.operand_types[0].bitfield.class = RegSIMD;
8105
0
    i.reg_operands++;
8106
0
    goto duplicate;
8107
0
  }
8108
8109
0
      if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_1ST_XMM0)
8110
0
  {
8111
0
    gas_assert ((MAX_OPERANDS - 1) > dupl);
8112
8113
    /* Add the implicit xmm0 for instructions with VEX prefix
8114
       and 3 sources.  */
8115
0
    for (j = i.operands; j > 0; j--)
8116
0
      {
8117
0
        i.op[j] = i.op[j - 1];
8118
0
        i.types[j] = i.types[j - 1];
8119
0
        i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8120
0
        i.flags[j] = i.flags[j - 1];
8121
0
      }
8122
0
    i.op[0].regs
8123
0
      = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
8124
0
    i.types[0] = regxmm;
8125
0
    i.tm.operand_types[0] = regxmm;
8126
8127
0
    i.operands += 2;
8128
0
    i.reg_operands += 2;
8129
0
    i.tm.operands += 2;
8130
8131
0
    dupl++;
8132
0
    dest++;
8133
0
    i.op[dupl] = i.op[dest];
8134
0
    i.types[dupl] = i.types[dest];
8135
0
    i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8136
0
    i.flags[dupl] = i.flags[dest];
8137
0
  }
8138
0
      else
8139
0
  {
8140
0
  duplicate:
8141
0
    i.operands++;
8142
0
    i.reg_operands++;
8143
0
    i.tm.operands++;
8144
8145
0
    i.op[dupl] = i.op[dest];
8146
0
    i.types[dupl] = i.types[dest];
8147
0
    i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8148
0
    i.flags[dupl] = i.flags[dest];
8149
0
  }
8150
8151
0
       if (i.tm.opcode_modifier.immext)
8152
0
   process_immext ();
8153
0
    }
8154
82.6k
  else if (i.tm.operand_types[0].bitfield.instance == Accum
8155
82.6k
     && i.tm.opcode_modifier.modrm)
8156
0
    {
8157
0
      unsigned int j;
8158
8159
0
      for (j = 1; j < i.operands; j++)
8160
0
  {
8161
0
    i.op[j - 1] = i.op[j];
8162
0
    i.types[j - 1] = i.types[j];
8163
8164
    /* We need to adjust fields in i.tm since they are used by
8165
       build_modrm_byte.  */
8166
0
    i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8167
8168
0
    i.flags[j - 1] = i.flags[j];
8169
0
  }
8170
8171
      /* No adjustment to i.reg_operands: This was already done at the top
8172
   of the function.  */
8173
0
      i.operands--;
8174
0
      i.tm.operands--;
8175
0
    }
8176
82.6k
  else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_QUAD_GROUP)
8177
0
    {
8178
0
      unsigned int regnum, first_reg_in_group, last_reg_in_group;
8179
8180
      /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
8181
0
      gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
8182
0
      regnum = register_number (i.op[1].regs);
8183
0
      first_reg_in_group = regnum & ~3;
8184
0
      last_reg_in_group = first_reg_in_group + 3;
8185
0
      if (regnum != first_reg_in_group)
8186
0
  as_warn (_("source register `%s%s' implicitly denotes"
8187
0
       " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
8188
0
     register_prefix, i.op[1].regs->reg_name,
8189
0
     register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
8190
0
     register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
8191
0
     insn_name (&i.tm));
8192
0
    }
8193
82.6k
  else if (i.tm.opcode_modifier.operandconstraint == REG_KLUDGE)
8194
0
    {
8195
      /* The imul $imm, %reg instruction is converted into
8196
   imul $imm, %reg, %reg, and the clr %reg instruction
8197
   is converted into xor %reg, %reg.  */
8198
8199
0
      unsigned int first_reg_op;
8200
8201
0
      if (operand_type_check (i.types[0], reg))
8202
0
  first_reg_op = 0;
8203
0
      else
8204
0
  first_reg_op = 1;
8205
      /* Pretend we saw the extra register operand.  */
8206
0
      gas_assert (i.reg_operands == 1
8207
0
      && i.op[first_reg_op + 1].regs == 0);
8208
0
      i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
8209
0
      i.types[first_reg_op + 1] = i.types[first_reg_op];
8210
0
      i.operands++;
8211
0
      i.reg_operands++;
8212
0
    }
8213
8214
82.6k
  if (i.tm.opcode_modifier.modrm)
8215
75.2k
    {
8216
      /* The opcode is completed (modulo i.tm.extension_opcode which
8217
   must be put into the modrm byte).  Now, we make the modrm and
8218
   index base bytes based on all the info we've collected.  */
8219
8220
75.2k
      default_seg = build_modrm_byte ();
8221
8222
75.2k
      if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
8223
0
  {
8224
    /* Warn about some common errors, but press on regardless.  */
8225
0
    if (i.operands == 2)
8226
0
      {
8227
        /* Reversed arguments on faddp or fmulp.  */
8228
0
        as_warn (_("translating to `%s %s%s,%s%s'"), insn_name (&i.tm),
8229
0
           register_prefix, i.op[!intel_syntax].regs->reg_name,
8230
0
           register_prefix, i.op[intel_syntax].regs->reg_name);
8231
0
      }
8232
0
    else if (i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
8233
0
      {
8234
        /* Extraneous `l' suffix on fp insn.  */
8235
0
        as_warn (_("translating to `%s %s%s'"), insn_name (&i.tm),
8236
0
           register_prefix, i.op[0].regs->reg_name);
8237
0
      }
8238
0
  }
8239
75.2k
    }
8240
7.34k
  else if (i.types[0].bitfield.class == SReg && !dot_insn ())
8241
0
    {
8242
0
      if (flag_code != CODE_64BIT
8243
0
    ? i.tm.base_opcode == POP_SEG_SHORT
8244
0
      && i.op[0].regs->reg_num == 1
8245
0
    : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
8246
0
      && i.op[0].regs->reg_num < 4)
8247
0
  {
8248
0
    as_bad (_("you can't `%s %s%s'"),
8249
0
      insn_name (&i.tm), register_prefix, i.op[0].regs->reg_name);
8250
0
    return 0;
8251
0
  }
8252
0
      if (i.op[0].regs->reg_num > 3
8253
0
    && i.tm.opcode_space == SPACE_BASE )
8254
0
  {
8255
0
    i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
8256
0
    i.tm.opcode_space = SPACE_0F;
8257
0
  }
8258
0
      i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
8259
0
    }
8260
7.34k
  else if (i.tm.opcode_space == SPACE_BASE
8261
7.34k
     && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
8262
0
    {
8263
0
      default_seg = reg_ds;
8264
0
    }
8265
7.34k
  else if (i.tm.opcode_modifier.isstring)
8266
839
    {
8267
      /* For the string instructions that allow a segment override
8268
   on one of their operands, the default segment is ds.  */
8269
839
      default_seg = reg_ds;
8270
839
    }
8271
6.50k
  else if (i.short_form)
8272
0
    {
8273
      /* The register operand is in the 1st or 2nd non-immediate operand.  */
8274
0
      const reg_entry *r = i.op[i.imm_operands].regs;
8275
8276
0
      if (!dot_insn ()
8277
0
    && r->reg_type.bitfield.instance == Accum
8278
0
    && i.op[i.imm_operands + 1].regs)
8279
0
  r = i.op[i.imm_operands + 1].regs;
8280
      /* Register goes in low 3 bits of opcode.  */
8281
0
      i.tm.base_opcode |= r->reg_num;
8282
0
      set_rex_vrex (r, REX_B, false);
8283
8284
0
      if (dot_insn () && i.reg_operands == 2)
8285
0
  {
8286
0
    gas_assert (is_any_vex_encoding (&i.tm)
8287
0
          || i.vec_encoding != vex_encoding_default);
8288
0
    i.vex.register_specifier = i.op[i.operands - 1].regs;
8289
0
  }
8290
0
    }
8291
6.50k
  else if (i.reg_operands == 1
8292
6.50k
     && !i.flags[i.operands - 1]
8293
6.50k
     && i.tm.operand_types[i.operands - 1].bitfield.instance
8294
0
        == InstanceNone)
8295
0
    {
8296
0
      gas_assert (is_any_vex_encoding (&i.tm)
8297
0
      || i.vec_encoding != vex_encoding_default);
8298
0
      i.vex.register_specifier = i.op[i.operands - 1].regs;
8299
0
    }
8300
8301
82.6k
  if ((i.seg[0] || i.prefix[SEG_PREFIX])
8302
82.6k
      && i.tm.mnem_off == MN_lea)
8303
0
    {
8304
0
      if (!quiet_warnings)
8305
0
  as_warn (_("segment override on `%s' is ineffectual"), insn_name (&i.tm));
8306
0
      if (optimize && !i.no_optimize)
8307
0
  {
8308
0
    i.seg[0] = NULL;
8309
0
    i.prefix[SEG_PREFIX] = 0;
8310
0
  }
8311
0
    }
8312
8313
  /* If a segment was explicitly specified, and the specified segment
8314
     is neither the default nor the one already recorded from a prefix,
8315
     use an opcode prefix to select it.  If we never figured out what
8316
     the default segment is, then default_seg will be zero at this
8317
     point, and the specified segment prefix will always be used.  */
8318
82.6k
  if (i.seg[0]
8319
82.6k
      && i.seg[0] != default_seg
8320
82.6k
      && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
8321
0
    {
8322
0
      if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
8323
0
  return 0;
8324
0
    }
8325
82.6k
  return 1;
8326
82.6k
}
8327
8328
static const reg_entry *
8329
build_modrm_byte (void)
8330
75.2k
{
8331
75.2k
  const reg_entry *default_seg = NULL;
8332
75.2k
  unsigned int source = i.imm_operands - i.tm.opcode_modifier.immext
8333
      /* Compensate for kludge in md_assemble().  */
8334
75.2k
      + i.tm.operand_types[0].bitfield.imm1;
8335
75.2k
  unsigned int dest = i.operands - 1 - i.tm.opcode_modifier.immext;
8336
75.2k
  unsigned int v, op, reg_slot = ~0;
8337
8338
  /* Accumulator (in particular %st), shift count (%cl), and alike need
8339
     to be skipped just like immediate operands do.  */
8340
75.2k
  if (i.tm.operand_types[source].bitfield.instance)
8341
0
    ++source;
8342
75.2k
  while (i.tm.operand_types[dest].bitfield.instance)
8343
0
    --dest;
8344
8345
75.2k
  for (op = source; op < i.operands; ++op)
8346
75.2k
    if (i.tm.operand_types[op].bitfield.baseindex)
8347
75.2k
      break;
8348
8349
75.2k
  if (i.reg_operands + i.mem_operands + (i.tm.extension_opcode != None) == 4)
8350
0
    {
8351
0
      expressionS *exp;
8352
8353
      /* There are 2 kinds of instructions:
8354
   1. 5 operands: 4 register operands or 3 register operands
8355
   plus 1 memory operand plus one Imm4 operand, VexXDS, and
8356
   VexW0 or VexW1.  The destination must be either XMM, YMM or
8357
   ZMM register.
8358
   2. 4 operands: 4 register operands or 3 register operands
8359
   plus 1 memory operand, with VexXDS.
8360
   3. Other equivalent combinations when coming from s_insn().  */
8361
0
      gas_assert (i.tm.opcode_modifier.vexvvvv
8362
0
      && i.tm.opcode_modifier.vexw);
8363
0
      gas_assert (dot_insn ()
8364
0
      || i.tm.operand_types[dest].bitfield.class == RegSIMD);
8365
8366
      /* Of the first two non-immediate operands the one with the template
8367
   not allowing for a memory one is encoded in the immediate operand.  */
8368
0
      if (source == op)
8369
0
  reg_slot = source + 1;
8370
0
      else
8371
0
  reg_slot = source++;
8372
8373
0
      if (!dot_insn ())
8374
0
  {
8375
0
    gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
8376
0
    gas_assert (!(i.op[reg_slot].regs->reg_flags & RegVRex));
8377
0
  }
8378
0
      else
8379
0
  gas_assert (i.tm.operand_types[reg_slot].bitfield.class != ClassNone);
8380
8381
0
      if (i.imm_operands == 0)
8382
0
  {
8383
    /* When there is no immediate operand, generate an 8bit
8384
       immediate operand to encode the first operand.  */
8385
0
    exp = &im_expressions[i.imm_operands++];
8386
0
    i.op[i.operands].imms = exp;
8387
0
    i.types[i.operands].bitfield.imm8 = 1;
8388
0
    i.operands++;
8389
8390
0
    exp->X_op = O_constant;
8391
0
  }
8392
0
      else
8393
0
  {
8394
0
    gas_assert (i.imm_operands == 1);
8395
0
    gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8396
0
    gas_assert (!i.tm.opcode_modifier.immext);
8397
8398
    /* Turn on Imm8 again so that output_imm will generate it.  */
8399
0
    i.types[0].bitfield.imm8 = 1;
8400
8401
0
    exp = i.op[0].imms;
8402
0
  }
8403
0
      exp->X_add_number |= register_number (i.op[reg_slot].regs)
8404
0
         << (3 + !(is_evex_encoding (&i.tm)
8405
0
             || i.vec_encoding == vex_encoding_evex));
8406
0
    }
8407
8408
75.2k
  for (v = source + 1; v < dest; ++v)
8409
0
    if (v != reg_slot)
8410
0
      break;
8411
75.2k
  if (v >= dest)
8412
75.2k
    v = ~0;
8413
75.2k
  if (i.tm.extension_opcode != None)
8414
75.2k
    {
8415
75.2k
      if (dest != source)
8416
0
  v = dest;
8417
75.2k
      dest = ~0;
8418
75.2k
    }
8419
75.2k
  gas_assert (source < dest);
8420
75.2k
  if (i.tm.opcode_modifier.operandconstraint == SWAP_SOURCES
8421
75.2k
      && source != op)
8422
0
    {
8423
0
      unsigned int tmp = source;
8424
8425
0
      source = v;
8426
0
      v = tmp;
8427
0
    }
8428
8429
75.2k
  if (v < MAX_OPERANDS)
8430
0
    {
8431
0
      gas_assert (i.tm.opcode_modifier.vexvvvv);
8432
0
      i.vex.register_specifier = i.op[v].regs;
8433
0
    }
8434
8435
75.2k
  if (op < i.operands)
8436
75.2k
    {
8437
75.2k
      if (i.mem_operands)
8438
75.2k
  {
8439
75.2k
    unsigned int fake_zero_displacement = 0;
8440
8441
75.2k
    gas_assert (i.flags[op] & Operand_Mem);
8442
8443
75.2k
    if (i.tm.opcode_modifier.sib)
8444
0
      {
8445
        /* The index register of VSIB shouldn't be RegIZ.  */
8446
0
        if (i.tm.opcode_modifier.sib != SIBMEM
8447
0
      && i.index_reg->reg_num == RegIZ)
8448
0
    abort ();
8449
8450
0
        i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8451
0
        if (!i.base_reg)
8452
0
    {
8453
0
      i.sib.base = NO_BASE_REGISTER;
8454
0
      i.sib.scale = i.log2_scale_factor;
8455
0
      i.types[op] = operand_type_and_not (i.types[op], anydisp);
8456
0
      i.types[op].bitfield.disp32 = 1;
8457
0
    }
8458
8459
        /* Since the mandatory SIB always has index register, so
8460
     the code logic remains unchanged. The non-mandatory SIB
8461
     without index register is allowed and will be handled
8462
     later.  */
8463
0
        if (i.index_reg)
8464
0
    {
8465
0
      if (i.index_reg->reg_num == RegIZ)
8466
0
        i.sib.index = NO_INDEX_REGISTER;
8467
0
      else
8468
0
        i.sib.index = i.index_reg->reg_num;
8469
0
      set_rex_vrex (i.index_reg, REX_X, false);
8470
0
    }
8471
0
      }
8472
8473
75.2k
    default_seg = reg_ds;
8474
8475
75.2k
    if (i.base_reg == 0)
8476
75.2k
      {
8477
75.2k
        i.rm.mode = 0;
8478
75.2k
        if (!i.disp_operands)
8479
0
    fake_zero_displacement = 1;
8480
75.2k
        if (i.index_reg == 0)
8481
75.2k
    {
8482
      /* Both check for VSIB and mandatory non-vector SIB. */
8483
75.2k
      gas_assert (!i.tm.opcode_modifier.sib
8484
75.2k
            || i.tm.opcode_modifier.sib == SIBMEM);
8485
      /* Operand is just <disp>  */
8486
0
      i.types[op] = operand_type_and_not (i.types[op], anydisp);
8487
75.2k
      if (flag_code == CODE_64BIT)
8488
28.8k
        {
8489
          /* 64bit mode overwrites the 32bit absolute
8490
       addressing by RIP relative addressing and
8491
       absolute addressing is encoded by one of the
8492
       redundant SIB forms.  */
8493
28.8k
          i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8494
28.8k
          i.sib.base = NO_BASE_REGISTER;
8495
28.8k
          i.sib.index = NO_INDEX_REGISTER;
8496
28.8k
          i.types[op].bitfield.disp32 = 1;
8497
28.8k
        }
8498
46.4k
      else if ((flag_code == CODE_16BIT)
8499
46.4k
         ^ (i.prefix[ADDR_PREFIX] != 0))
8500
44.0k
        {
8501
44.0k
          i.rm.regmem = NO_BASE_REGISTER_16;
8502
44.0k
          i.types[op].bitfield.disp16 = 1;
8503
44.0k
        }
8504
2.44k
      else
8505
2.44k
        {
8506
2.44k
          i.rm.regmem = NO_BASE_REGISTER;
8507
2.44k
          i.types[op].bitfield.disp32 = 1;
8508
2.44k
        }
8509
75.2k
    }
8510
0
        else if (!i.tm.opcode_modifier.sib)
8511
0
    {
8512
      /* !i.base_reg && i.index_reg  */
8513
0
      if (i.index_reg->reg_num == RegIZ)
8514
0
        i.sib.index = NO_INDEX_REGISTER;
8515
0
      else
8516
0
        i.sib.index = i.index_reg->reg_num;
8517
0
      i.sib.base = NO_BASE_REGISTER;
8518
0
      i.sib.scale = i.log2_scale_factor;
8519
0
      i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8520
0
      i.types[op] = operand_type_and_not (i.types[op], anydisp);
8521
0
      i.types[op].bitfield.disp32 = 1;
8522
0
      if ((i.index_reg->reg_flags & RegRex) != 0)
8523
0
        i.rex |= REX_X;
8524
0
    }
8525
75.2k
      }
8526
    /* RIP addressing for 64bit mode.  */
8527
0
    else if (i.base_reg->reg_num == RegIP)
8528
0
      {
8529
0
        gas_assert (!i.tm.opcode_modifier.sib);
8530
0
        i.rm.regmem = NO_BASE_REGISTER;
8531
0
        i.types[op].bitfield.disp8 = 0;
8532
0
        i.types[op].bitfield.disp16 = 0;
8533
0
        i.types[op].bitfield.disp32 = 1;
8534
0
        i.types[op].bitfield.disp64 = 0;
8535
0
        i.flags[op] |= Operand_PCrel;
8536
0
        if (! i.disp_operands)
8537
0
    fake_zero_displacement = 1;
8538
0
      }
8539
0
    else if (i.base_reg->reg_type.bitfield.word)
8540
0
      {
8541
0
        gas_assert (!i.tm.opcode_modifier.sib);
8542
0
        switch (i.base_reg->reg_num)
8543
0
    {
8544
0
    case 3: /* (%bx)  */
8545
0
      if (i.index_reg == 0)
8546
0
        i.rm.regmem = 7;
8547
0
      else /* (%bx,%si) -> 0, or (%bx,%di) -> 1  */
8548
0
        i.rm.regmem = i.index_reg->reg_num - 6;
8549
0
      break;
8550
0
    case 5: /* (%bp)  */
8551
0
      default_seg = reg_ss;
8552
0
      if (i.index_reg == 0)
8553
0
        {
8554
0
          i.rm.regmem = 6;
8555
0
          if (operand_type_check (i.types[op], disp) == 0)
8556
0
      {
8557
        /* fake (%bp) into 0(%bp)  */
8558
0
        if (i.disp_encoding == disp_encoding_16bit)
8559
0
          i.types[op].bitfield.disp16 = 1;
8560
0
        else
8561
0
          i.types[op].bitfield.disp8 = 1;
8562
0
        fake_zero_displacement = 1;
8563
0
      }
8564
0
        }
8565
0
      else /* (%bp,%si) -> 2, or (%bp,%di) -> 3  */
8566
0
        i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8567
0
      break;
8568
0
    default: /* (%si) -> 4 or (%di) -> 5  */
8569
0
      i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8570
0
    }
8571
0
        if (!fake_zero_displacement
8572
0
      && !i.disp_operands
8573
0
      && i.disp_encoding)
8574
0
    {
8575
0
      fake_zero_displacement = 1;
8576
0
      if (i.disp_encoding == disp_encoding_8bit)
8577
0
        i.types[op].bitfield.disp8 = 1;
8578
0
      else
8579
0
        i.types[op].bitfield.disp16 = 1;
8580
0
    }
8581
0
        i.rm.mode = mode_from_disp_size (i.types[op]);
8582
0
      }
8583
0
    else /* i.base_reg and 32/64 bit mode  */
8584
0
      {
8585
0
        if (operand_type_check (i.types[op], disp))
8586
0
    {
8587
0
      i.types[op].bitfield.disp16 = 0;
8588
0
      i.types[op].bitfield.disp64 = 0;
8589
0
      i.types[op].bitfield.disp32 = 1;
8590
0
    }
8591
8592
0
        if (!i.tm.opcode_modifier.sib)
8593
0
    i.rm.regmem = i.base_reg->reg_num;
8594
0
        if ((i.base_reg->reg_flags & RegRex) != 0)
8595
0
    i.rex |= REX_B;
8596
0
        i.sib.base = i.base_reg->reg_num;
8597
        /* x86-64 ignores REX prefix bit here to avoid decoder
8598
     complications.  */
8599
0
        if (!(i.base_reg->reg_flags & RegRex)
8600
0
      && (i.base_reg->reg_num == EBP_REG_NUM
8601
0
       || i.base_reg->reg_num == ESP_REG_NUM))
8602
0
      default_seg = reg_ss;
8603
0
        if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
8604
0
    {
8605
0
      fake_zero_displacement = 1;
8606
0
      if (i.disp_encoding == disp_encoding_32bit)
8607
0
        i.types[op].bitfield.disp32 = 1;
8608
0
      else
8609
0
        i.types[op].bitfield.disp8 = 1;
8610
0
    }
8611
0
        i.sib.scale = i.log2_scale_factor;
8612
0
        if (i.index_reg == 0)
8613
0
    {
8614
      /* Only check for VSIB. */
8615
0
      gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8616
0
            && i.tm.opcode_modifier.sib != VECSIB256
8617
0
            && i.tm.opcode_modifier.sib != VECSIB512);
8618
8619
      /* <disp>(%esp) becomes two byte modrm with no index
8620
         register.  We've already stored the code for esp
8621
         in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8622
         Any base register besides %esp will not use the
8623
         extra modrm byte.  */
8624
0
      i.sib.index = NO_INDEX_REGISTER;
8625
0
    }
8626
0
        else if (!i.tm.opcode_modifier.sib)
8627
0
    {
8628
0
      if (i.index_reg->reg_num == RegIZ)
8629
0
        i.sib.index = NO_INDEX_REGISTER;
8630
0
      else
8631
0
        i.sib.index = i.index_reg->reg_num;
8632
0
      i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8633
0
      if ((i.index_reg->reg_flags & RegRex) != 0)
8634
0
        i.rex |= REX_X;
8635
0
    }
8636
8637
0
        if (i.disp_operands
8638
0
      && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8639
0
          || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8640
0
    i.rm.mode = 0;
8641
0
        else
8642
0
    {
8643
0
      if (!fake_zero_displacement
8644
0
          && !i.disp_operands
8645
0
          && i.disp_encoding)
8646
0
        {
8647
0
          fake_zero_displacement = 1;
8648
0
          if (i.disp_encoding == disp_encoding_8bit)
8649
0
      i.types[op].bitfield.disp8 = 1;
8650
0
          else
8651
0
      i.types[op].bitfield.disp32 = 1;
8652
0
        }
8653
0
      i.rm.mode = mode_from_disp_size (i.types[op]);
8654
0
    }
8655
0
      }
8656
8657
75.2k
    if (fake_zero_displacement)
8658
0
      {
8659
        /* Fakes a zero displacement assuming that i.types[op]
8660
     holds the correct displacement size.  */
8661
0
        expressionS *exp;
8662
8663
0
        gas_assert (i.op[op].disps == 0);
8664
0
        exp = &disp_expressions[i.disp_operands++];
8665
0
        i.op[op].disps = exp;
8666
0
        exp->X_op = O_constant;
8667
0
        exp->X_add_number = 0;
8668
0
        exp->X_add_symbol = (symbolS *) 0;
8669
0
        exp->X_op_symbol = (symbolS *) 0;
8670
0
      }
8671
75.2k
  }
8672
1
    else
8673
1
  {
8674
1
      i.rm.mode = 3;
8675
1
      i.rm.regmem = i.op[op].regs->reg_num;
8676
1
      set_rex_vrex (i.op[op].regs, REX_B, false);
8677
1
  }
8678
8679
75.2k
      if (op == dest)
8680
0
  dest = ~0;
8681
75.2k
      if (op == source)
8682
75.2k
  source = ~0;
8683
75.2k
    }
8684
0
  else
8685
0
    {
8686
0
      i.rm.mode = 3;
8687
0
      if (!i.tm.opcode_modifier.regmem)
8688
0
  {
8689
0
    gas_assert (source < MAX_OPERANDS);
8690
0
    i.rm.regmem = i.op[source].regs->reg_num;
8691
0
    set_rex_vrex (i.op[source].regs, REX_B,
8692
0
      dest >= MAX_OPERANDS && i.tm.opcode_modifier.sse2avx);
8693
0
    source = ~0;
8694
0
  }
8695
0
      else
8696
0
  {
8697
0
    gas_assert (dest < MAX_OPERANDS);
8698
0
    i.rm.regmem = i.op[dest].regs->reg_num;
8699
0
    set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
8700
0
    dest = ~0;
8701
0
  }
8702
0
    }
8703
8704
  /* Fill in i.rm.reg field with extension opcode (if any) or the
8705
     appropriate register.  */
8706
75.2k
  if (i.tm.extension_opcode != None)
8707
75.2k
    i.rm.reg = i.tm.extension_opcode;
8708
3
  else if (!i.tm.opcode_modifier.regmem && dest < MAX_OPERANDS)
8709
3
    {
8710
3
      i.rm.reg = i.op[dest].regs->reg_num;
8711
3
      set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
8712
3
    }
8713
0
  else
8714
0
    {
8715
0
      gas_assert (source < MAX_OPERANDS);
8716
0
      i.rm.reg = i.op[source].regs->reg_num;
8717
0
      set_rex_vrex (i.op[source].regs, REX_R, false);
8718
0
    }
8719
8720
75.2k
  if (flag_code != CODE_64BIT && (i.rex & REX_R))
8721
0
    {
8722
0
      gas_assert (i.types[!i.tm.opcode_modifier.regmem].bitfield.class == RegCR);
8723
0
      i.rex &= ~REX_R;
8724
0
      add_prefix (LOCK_PREFIX_OPCODE);
8725
0
    }
8726
8727
0
  return default_seg;
8728
75.2k
}
8729
8730
static INLINE void
8731
frag_opcode_byte (unsigned char byte)
8732
106k
{
8733
106k
  if (now_seg != absolute_section)
8734
104k
    FRAG_APPEND_1_CHAR (byte);
8735
1.61k
  else
8736
1.61k
    ++abs_section_offset;
8737
106k
}
8738
8739
static unsigned int
8740
flip_code16 (unsigned int code16)
8741
0
{
8742
0
  gas_assert (i.tm.operands == 1);
8743
8744
0
  return !(i.prefix[REX_PREFIX] & REX_W)
8745
0
   && (code16 ? i.tm.operand_types[0].bitfield.disp32
8746
0
        : i.tm.operand_types[0].bitfield.disp16)
8747
0
   ? CODE16 : 0;
8748
0
}
8749
8750
static void
8751
output_branch (void)
8752
4.82k
{
8753
4.82k
  char *p;
8754
4.82k
  int size;
8755
4.82k
  int code16;
8756
4.82k
  int prefix;
8757
4.82k
  relax_substateT subtype;
8758
4.82k
  symbolS *sym;
8759
4.82k
  offsetT off;
8760
8761
4.82k
  if (now_seg == absolute_section)
8762
1.27k
    {
8763
1.27k
      as_bad (_("relaxable branches not supported in absolute section"));
8764
1.27k
      return;
8765
1.27k
    }
8766
8767
3.54k
  code16 = flag_code == CODE_16BIT ? CODE16 : 0;
8768
3.54k
  size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
8769
8770
3.54k
  prefix = 0;
8771
3.54k
  if (i.prefix[DATA_PREFIX] != 0)
8772
0
    {
8773
0
      prefix = 1;
8774
0
      i.prefixes -= 1;
8775
0
      code16 ^= flip_code16(code16);
8776
0
    }
8777
  /* Pentium4 branch hints.  */
8778
  if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8779
3.54k
      || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8780
2
    {
8781
2
      prefix++;
8782
2
      i.prefixes--;
8783
2
    }
8784
3.54k
  if (i.prefix[REX_PREFIX] != 0)
8785
0
    {
8786
0
      prefix++;
8787
0
      i.prefixes--;
8788
0
    }
8789
8790
  /* BND prefixed jump.  */
8791
3.54k
  if (i.prefix[BND_PREFIX] != 0)
8792
0
    {
8793
0
      prefix++;
8794
0
      i.prefixes--;
8795
0
    }
8796
8797
3.54k
  if (i.prefixes != 0)
8798
0
    as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
8799
8800
  /* It's always a symbol;  End frag & setup for relax.
8801
     Make sure there is enough room in this frag for the largest
8802
     instruction we may generate in md_convert_frag.  This is 2
8803
     bytes for the opcode and room for the prefix and largest
8804
     displacement.  */
8805
3.54k
  frag_grow (prefix + 2 + 4);
8806
  /* Prefix and 1 opcode byte go in fr_fix.  */
8807
3.54k
  p = frag_more (prefix + 1);
8808
3.54k
  if (i.prefix[DATA_PREFIX] != 0)
8809
0
    *p++ = DATA_PREFIX_OPCODE;
8810
3.54k
  if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
8811
3.54k
      || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
8812
2
    *p++ = i.prefix[SEG_PREFIX];
8813
3.54k
  if (i.prefix[BND_PREFIX] != 0)
8814
0
    *p++ = BND_PREFIX_OPCODE;
8815
3.54k
  if (i.prefix[REX_PREFIX] != 0)
8816
0
    *p++ = i.prefix[REX_PREFIX];
8817
3.54k
  *p = i.tm.base_opcode;
8818
8819
3.54k
  if ((unsigned char) *p == JUMP_PC_RELATIVE)
8820
0
    subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
8821
3.54k
  else if (cpu_arch_flags.bitfield.cpui386)
8822
3.54k
    subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
8823
0
  else
8824
0
    subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
8825
3.54k
  subtype |= code16;
8826
8827
3.54k
  sym = i.op[0].disps->X_add_symbol;
8828
3.54k
  off = i.op[0].disps->X_add_number;
8829
8830
3.54k
  if (i.op[0].disps->X_op != O_constant
8831
3.54k
      && i.op[0].disps->X_op != O_symbol)
8832
770
    {
8833
      /* Handle complex expressions.  */
8834
770
      sym = make_expr_symbol (i.op[0].disps);
8835
770
      off = 0;
8836
770
    }
8837
8838
3.54k
  frag_now->tc_frag_data.code64 = flag_code == CODE_64BIT;
8839
8840
  /* 1 possible extra opcode + 4 byte displacement go in var part.
8841
     Pass reloc in fr_var.  */
8842
3.54k
  frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
8843
3.54k
}
8844
8845
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8846
/* Return TRUE iff PLT32 relocation should be used for branching to
8847
   symbol S.  */
8848
8849
static bool
8850
need_plt32_p (symbolS *s)
8851
22
{
8852
  /* PLT32 relocation is ELF only.  */
8853
22
  if (!IS_ELF)
8854
0
    return false;
8855
8856
#ifdef TE_SOLARIS
8857
  /* Don't emit PLT32 relocation on Solaris: neither native linker nor
8858
     krtld support it.  */
8859
  return false;
8860
#endif
8861
8862
  /* Since there is no need to prepare for PLT branch on x86-64, we
8863
     can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
8864
     be used as a marker for 32-bit PC-relative branches.  */
8865
22
  if (!object_64bit)
8866
0
    return false;
8867
8868
22
  if (s == NULL)
8869
0
    return false;
8870
8871
  /* Weak or undefined symbol need PLT32 relocation.  */
8872
22
  if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
8873
7
    return true;
8874
8875
  /* Non-global symbol doesn't need PLT32 relocation.  */
8876
15
  if (! S_IS_EXTERNAL (s))
8877
15
    return false;
8878
8879
  /* Other global symbols need PLT32 relocation.  NB: Symbol with
8880
     non-default visibilities are treated as normal global symbol
8881
     so that PLT32 relocation can be used as a marker for 32-bit
8882
     PC-relative branches.  It is useful for linker relaxation.  */
8883
0
  return true;
8884
15
}
8885
#endif
8886
8887
static void
8888
output_jump (void)
8889
351
{
8890
351
  char *p;
8891
351
  int size;
8892
351
  fixS *fixP;
8893
351
  bfd_reloc_code_real_type jump_reloc = i.reloc[0];
8894
8895
351
  if (i.tm.opcode_modifier.jump == JUMP_BYTE)
8896
323
    {
8897
      /* This is a loop or jecxz type instruction.  */
8898
323
      size = 1;
8899
323
      if (i.prefix[ADDR_PREFIX] != 0)
8900
0
  {
8901
0
    frag_opcode_byte (ADDR_PREFIX_OPCODE);
8902
0
    i.prefixes -= 1;
8903
0
  }
8904
      /* Pentium4 branch hints.  */
8905
      if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
8906
323
    || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
8907
0
  {
8908
0
    frag_opcode_byte (i.prefix[SEG_PREFIX]);
8909
0
    i.prefixes--;
8910
0
  }
8911
323
    }
8912
28
  else
8913
28
    {
8914
28
      int code16;
8915
8916
28
      code16 = 0;
8917
28
      if (flag_code == CODE_16BIT)
8918
0
  code16 = CODE16;
8919
8920
28
      if (i.prefix[DATA_PREFIX] != 0)
8921
0
  {
8922
0
    frag_opcode_byte (DATA_PREFIX_OPCODE);
8923
0
    i.prefixes -= 1;
8924
0
    code16 ^= flip_code16(code16);
8925
0
  }
8926
8927
28
      size = 4;
8928
28
      if (code16)
8929
0
  size = 2;
8930
28
    }
8931
8932
  /* BND prefixed jump.  */
8933
351
  if (i.prefix[BND_PREFIX] != 0)
8934
0
    {
8935
0
      frag_opcode_byte (i.prefix[BND_PREFIX]);
8936
0
      i.prefixes -= 1;
8937
0
    }
8938
8939
351
  if (i.prefix[REX_PREFIX] != 0)
8940
0
    {
8941
0
      frag_opcode_byte (i.prefix[REX_PREFIX]);
8942
0
      i.prefixes -= 1;
8943
0
    }
8944
8945
351
  if (i.prefixes != 0)
8946
0
    as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
8947
8948
351
  if (now_seg == absolute_section)
8949
288
    {
8950
288
      abs_section_offset += i.opcode_length + size;
8951
288
      return;
8952
288
    }
8953
8954
63
  p = frag_more (i.opcode_length + size);
8955
63
  switch (i.opcode_length)
8956
63
    {
8957
0
    case 2:
8958
0
      *p++ = i.tm.base_opcode >> 8;
8959
      /* Fall through.  */
8960
63
    case 1:
8961
63
      *p++ = i.tm.base_opcode;
8962
63
      break;
8963
0
    default:
8964
0
      abort ();
8965
63
    }
8966
8967
63
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8968
63
  if (flag_code == CODE_64BIT && size == 4
8969
63
      && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
8970
63
      && need_plt32_p (i.op[0].disps->X_add_symbol))
8971
7
    jump_reloc = BFD_RELOC_X86_64_PLT32;
8972
63
#endif
8973
8974
63
  jump_reloc = reloc (size, 1, 1, jump_reloc);
8975
8976
63
  fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
8977
63
          i.op[0].disps, 1, jump_reloc);
8978
8979
  /* All jumps handled here are signed, but don't unconditionally use a
8980
     signed limit check for 32 and 16 bit jumps as we want to allow wrap
8981
     around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
8982
     respectively.  */
8983
63
  switch (size)
8984
63
    {
8985
35
    case 1:
8986
35
      fixP->fx_signed = 1;
8987
35
      break;
8988
8989
0
    case 2:
8990
0
      if (i.tm.mnem_off == MN_xbegin)
8991
0
  fixP->fx_signed = 1;
8992
0
      break;
8993
8994
28
    case 4:
8995
28
      if (flag_code == CODE_64BIT)
8996
28
  fixP->fx_signed = 1;
8997
28
      break;
8998
63
    }
8999
63
}
9000
9001
static void
9002
output_interseg_jump (void)
9003
0
{
9004
0
  char *p;
9005
0
  int size;
9006
0
  int prefix;
9007
0
  int code16;
9008
9009
0
  code16 = 0;
9010
0
  if (flag_code == CODE_16BIT)
9011
0
    code16 = CODE16;
9012
9013
0
  prefix = 0;
9014
0
  if (i.prefix[DATA_PREFIX] != 0)
9015
0
    {
9016
0
      prefix = 1;
9017
0
      i.prefixes -= 1;
9018
0
      code16 ^= CODE16;
9019
0
    }
9020
9021
0
  gas_assert (!i.prefix[REX_PREFIX]);
9022
9023
0
  size = 4;
9024
0
  if (code16)
9025
0
    size = 2;
9026
9027
0
  if (i.prefixes != 0)
9028
0
    as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
9029
9030
0
  if (now_seg == absolute_section)
9031
0
    {
9032
0
      abs_section_offset += prefix + 1 + 2 + size;
9033
0
      return;
9034
0
    }
9035
9036
  /* 1 opcode; 2 segment; offset  */
9037
0
  p = frag_more (prefix + 1 + 2 + size);
9038
9039
0
  if (i.prefix[DATA_PREFIX] != 0)
9040
0
    *p++ = DATA_PREFIX_OPCODE;
9041
9042
0
  if (i.prefix[REX_PREFIX] != 0)
9043
0
    *p++ = i.prefix[REX_PREFIX];
9044
9045
0
  *p++ = i.tm.base_opcode;
9046
0
  if (i.op[1].imms->X_op == O_constant)
9047
0
    {
9048
0
      offsetT n = i.op[1].imms->X_add_number;
9049
9050
0
      if (size == 2
9051
0
    && !fits_in_unsigned_word (n)
9052
0
    && !fits_in_signed_word (n))
9053
0
  {
9054
0
    as_bad (_("16-bit jump out of range"));
9055
0
    return;
9056
0
  }
9057
0
      md_number_to_chars (p, n, size);
9058
0
    }
9059
0
  else
9060
0
    fix_new_exp (frag_now, p - frag_now->fr_literal, size,
9061
0
     i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
9062
9063
0
  p += size;
9064
0
  if (i.op[0].imms->X_op == O_constant)
9065
0
    md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
9066
0
  else
9067
0
    fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
9068
0
     i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
9069
0
}
9070
9071
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9072
void
9073
x86_cleanup (void)
9074
633
{
9075
633
  char *p;
9076
633
  asection *seg = now_seg;
9077
633
  subsegT subseg = now_subseg;
9078
633
  asection *sec;
9079
633
  unsigned int alignment, align_size_1;
9080
633
  unsigned int isa_1_descsz, feature_2_descsz, descsz;
9081
633
  unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9082
633
  unsigned int padding;
9083
9084
633
  if (!IS_ELF || !x86_used_note)
9085
0
    return;
9086
9087
633
  x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9088
9089
  /* The .note.gnu.property section layout:
9090
9091
     Field  Length    Contents
9092
     ---- ----    ----
9093
     n_namsz  4   4
9094
     n_descsz 4   The note descriptor size
9095
     n_type 4   NT_GNU_PROPERTY_TYPE_0
9096
     n_name 4   "GNU"
9097
     n_desc n_descsz  The program property array
9098
     .... ....    ....
9099
   */
9100
9101
  /* Create the .note.gnu.property section.  */
9102
633
  sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
9103
633
  bfd_set_section_flags (sec,
9104
633
       (SEC_ALLOC
9105
633
        | SEC_LOAD
9106
633
        | SEC_DATA
9107
633
        | SEC_HAS_CONTENTS
9108
633
        | SEC_READONLY));
9109
9110
633
  if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9111
633
    {
9112
633
      align_size_1 = 7;
9113
633
      alignment = 3;
9114
633
    }
9115
0
  else
9116
0
    {
9117
0
      align_size_1 = 3;
9118
0
      alignment = 2;
9119
0
    }
9120
9121
633
  bfd_set_section_alignment (sec, alignment);
9122
633
  elf_section_type (sec) = SHT_NOTE;
9123
9124
  /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9125
          + 4-byte data  */
9126
633
  isa_1_descsz_raw = 4 + 4 + 4;
9127
  /* Align GNU_PROPERTY_X86_ISA_1_USED.  */
9128
633
  isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
9129
9130
633
  feature_2_descsz_raw = isa_1_descsz;
9131
  /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
9132
              + 4-byte data  */
9133
633
  feature_2_descsz_raw += 4 + 4 + 4;
9134
  /* Align GNU_PROPERTY_X86_FEATURE_2_USED.  */
9135
633
  feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9136
633
          & ~align_size_1);
9137
9138
633
  descsz = feature_2_descsz;
9139
  /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz.  */
9140
633
  p = frag_more (4 + 4 + 4 + 4 + descsz);
9141
9142
  /* Write n_namsz.  */
9143
633
  md_number_to_chars (p, (valueT) 4, 4);
9144
9145
  /* Write n_descsz.  */
9146
633
  md_number_to_chars (p + 4, (valueT) descsz, 4);
9147
9148
  /* Write n_type.  */
9149
633
  md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9150
9151
  /* Write n_name.  */
9152
633
  memcpy (p + 4 * 3, "GNU", 4);
9153
9154
  /* Write 4-byte type.  */
9155
633
  md_number_to_chars (p + 4 * 4,
9156
633
          (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
9157
9158
  /* Write 4-byte data size.  */
9159
633
  md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
9160
9161
  /* Write 4-byte data.  */
9162
633
  md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
9163
9164
  /* Zero out paddings.  */
9165
633
  padding = isa_1_descsz - isa_1_descsz_raw;
9166
633
  if (padding)
9167
633
    memset (p + 4 * 7, 0, padding);
9168
9169
  /* Write 4-byte type.  */
9170
633
  md_number_to_chars (p + isa_1_descsz + 4 * 4,
9171
633
          (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9172
9173
  /* Write 4-byte data size.  */
9174
633
  md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9175
9176
  /* Write 4-byte data.  */
9177
633
  md_number_to_chars (p + isa_1_descsz + 4 * 6,
9178
633
          (valueT) x86_feature_2_used, 4);
9179
9180
  /* Zero out paddings.  */
9181
633
  padding = feature_2_descsz - feature_2_descsz_raw;
9182
633
  if (padding)
9183
633
    memset (p + isa_1_descsz + 4 * 7, 0, padding);
9184
9185
  /* We probably can't restore the current segment, for there likely
9186
     isn't one yet...  */
9187
633
  if (seg && subseg)
9188
97
    subseg_set (seg, subseg);
9189
633
}
9190
9191
bool
9192
x86_support_sframe_p (void)
9193
0
{
9194
  /* At this time, SFrame stack trace is supported for AMD64 ABI only.  */
9195
0
  return (x86_elf_abi == X86_64_ABI);
9196
0
}
9197
9198
bool
9199
x86_sframe_ra_tracking_p (void)
9200
0
{
9201
  /* In AMD64, return address is always stored on the stack at a fixed offset
9202
     from the CFA (provided via x86_sframe_cfa_ra_offset ()).
9203
     Do not track explicitly via an SFrame Frame Row Entry.  */
9204
0
  return false;
9205
0
}
9206
9207
offsetT
9208
x86_sframe_cfa_ra_offset (void)
9209
0
{
9210
0
  gas_assert (x86_elf_abi == X86_64_ABI);
9211
0
  return (offsetT) -8;
9212
0
}
9213
9214
unsigned char
9215
x86_sframe_get_abi_arch (void)
9216
0
{
9217
0
  unsigned char sframe_abi_arch = 0;
9218
9219
0
  if (x86_support_sframe_p ())
9220
0
    {
9221
0
      gas_assert (!target_big_endian);
9222
0
      sframe_abi_arch = SFRAME_ABI_AMD64_ENDIAN_LITTLE;
9223
0
    }
9224
9225
0
  return sframe_abi_arch;
9226
0
}
9227
9228
#endif
9229
9230
static unsigned int
9231
encoding_length (const fragS *start_frag, offsetT start_off,
9232
     const char *frag_now_ptr)
9233
178k
{
9234
178k
  unsigned int len = 0;
9235
9236
178k
  if (start_frag != frag_now)
9237
122
    {
9238
122
      const fragS *fr = start_frag;
9239
9240
122
      do {
9241
122
  len += fr->fr_fix;
9242
122
  fr = fr->fr_next;
9243
122
      } while (fr && fr != frag_now);
9244
122
    }
9245
9246
178k
  return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9247
178k
}
9248
9249
/* Return 1 for test, and, cmp, add, sub, inc and dec which may
9250
   be macro-fused with conditional jumps.
9251
   NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9252
   or is one of the following format:
9253
9254
    cmp m, imm
9255
    add m, imm
9256
    sub m, imm
9257
   test m, imm
9258
    and m, imm
9259
    inc m
9260
    dec m
9261
9262
   it is unfusible.  */
9263
9264
static int
9265
maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
9266
0
{
9267
  /* No RIP address.  */
9268
0
  if (i.base_reg && i.base_reg->reg_num == RegIP)
9269
0
    return 0;
9270
9271
  /* No opcodes outside of base encoding space.  */
9272
0
  if (i.tm.opcode_space != SPACE_BASE)
9273
0
    return 0;
9274
9275
  /* add, sub without add/sub m, imm.  */
9276
0
  if (i.tm.base_opcode <= 5
9277
0
      || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9278
0
      || ((i.tm.base_opcode | 3) == 0x83
9279
0
    && (i.tm.extension_opcode == 0x5
9280
0
        || i.tm.extension_opcode == 0x0)))
9281
0
    {
9282
0
      *mf_cmp_p = mf_cmp_alu_cmp;
9283
0
      return !(i.mem_operands && i.imm_operands);
9284
0
    }
9285
9286
  /* and without and m, imm.  */
9287
0
  if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9288
0
      || ((i.tm.base_opcode | 3) == 0x83
9289
0
    && i.tm.extension_opcode == 0x4))
9290
0
    {
9291
0
      *mf_cmp_p = mf_cmp_test_and;
9292
0
      return !(i.mem_operands && i.imm_operands);
9293
0
    }
9294
9295
  /* test without test m imm.  */
9296
0
  if ((i.tm.base_opcode | 1) == 0x85
9297
0
      || (i.tm.base_opcode | 1) == 0xa9
9298
0
      || ((i.tm.base_opcode | 1) == 0xf7
9299
0
    && i.tm.extension_opcode == 0))
9300
0
    {
9301
0
      *mf_cmp_p = mf_cmp_test_and;
9302
0
      return !(i.mem_operands && i.imm_operands);
9303
0
    }
9304
9305
  /* cmp without cmp m, imm.  */
9306
0
  if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
9307
0
      || ((i.tm.base_opcode | 3) == 0x83
9308
0
    && (i.tm.extension_opcode == 0x7)))
9309
0
    {
9310
0
      *mf_cmp_p = mf_cmp_alu_cmp;
9311
0
      return !(i.mem_operands && i.imm_operands);
9312
0
    }
9313
9314
  /* inc, dec without inc/dec m.   */
9315
0
  if ((i.tm.cpu_flags.bitfield.cpuno64
9316
0
       && (i.tm.base_opcode | 0xf) == 0x4f)
9317
0
      || ((i.tm.base_opcode | 1) == 0xff
9318
0
    && i.tm.extension_opcode <= 0x1))
9319
0
    {
9320
0
      *mf_cmp_p = mf_cmp_incdec;
9321
0
      return !i.mem_operands;
9322
0
    }
9323
9324
0
  return 0;
9325
0
}
9326
9327
/* Return 1 if a FUSED_JCC_PADDING frag should be generated.  */
9328
9329
static int
9330
add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
9331
181k
{
9332
  /* NB: Don't work with COND_JUMP86 without i386.  */
9333
181k
  if (!align_branch_power
9334
181k
      || now_seg == absolute_section
9335
181k
      || !cpu_arch_flags.bitfield.cpui386
9336
181k
      || !(align_branch & align_branch_fused_bit))
9337
181k
    return 0;
9338
9339
0
  if (maybe_fused_with_jcc_p (mf_cmp_p))
9340
0
    {
9341
0
      if (last_insn.kind == last_insn_other
9342
0
    || last_insn.seg != now_seg)
9343
0
  return 1;
9344
0
      if (flag_debug)
9345
0
  as_warn_where (last_insn.file, last_insn.line,
9346
0
           _("`%s` skips -malign-branch-boundary on `%s`"),
9347
0
           last_insn.name, insn_name (&i.tm));
9348
0
    }
9349
9350
0
  return 0;
9351
0
}
9352
9353
/* Return 1 if a BRANCH_PREFIX frag should be generated.  */
9354
9355
static int
9356
add_branch_prefix_frag_p (void)
9357
181k
{
9358
  /* NB: Don't work with COND_JUMP86 without i386.  Don't add prefix
9359
     to PadLock instructions since they include prefixes in opcode.  */
9360
181k
  if (!align_branch_power
9361
181k
      || !align_branch_prefix_size
9362
181k
      || now_seg == absolute_section
9363
181k
      || i.tm.cpu_flags.bitfield.cpupadlock
9364
181k
      || !cpu_arch_flags.bitfield.cpui386)
9365
181k
    return 0;
9366
9367
  /* Don't add prefix if it is a prefix or there is no operand in case
9368
     that segment prefix is special.  */
9369
0
  if (!i.operands || i.tm.opcode_modifier.isprefix)
9370
0
    return 0;
9371
9372
0
  if (last_insn.kind == last_insn_other
9373
0
      || last_insn.seg != now_seg)
9374
0
    return 1;
9375
9376
0
  if (flag_debug)
9377
0
    as_warn_where (last_insn.file, last_insn.line,
9378
0
       _("`%s` skips -malign-branch-boundary on `%s`"),
9379
0
       last_insn.name, insn_name (&i.tm));
9380
9381
0
  return 0;
9382
0
}
9383
9384
/* Return 1 if a BRANCH_PADDING frag should be generated.  */
9385
9386
static int
9387
add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9388
         enum mf_jcc_kind *mf_jcc_p)
9389
187k
{
9390
187k
  int add_padding;
9391
9392
  /* NB: Don't work with COND_JUMP86 without i386.  */
9393
187k
  if (!align_branch_power
9394
187k
      || now_seg == absolute_section
9395
187k
      || !cpu_arch_flags.bitfield.cpui386
9396
187k
      || i.tm.opcode_space != SPACE_BASE)
9397
187k
    return 0;
9398
9399
0
  add_padding = 0;
9400
9401
  /* Check for jcc and direct jmp.  */
9402
0
  if (i.tm.opcode_modifier.jump == JUMP)
9403
0
    {
9404
0
      if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9405
0
  {
9406
0
    *branch_p = align_branch_jmp;
9407
0
    add_padding = align_branch & align_branch_jmp_bit;
9408
0
  }
9409
0
      else
9410
0
  {
9411
    /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9412
       igore the lowest bit.  */
9413
0
    *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
9414
0
    *branch_p = align_branch_jcc;
9415
0
    if ((align_branch & align_branch_jcc_bit))
9416
0
      add_padding = 1;
9417
0
  }
9418
0
    }
9419
0
  else if ((i.tm.base_opcode | 1) == 0xc3)
9420
0
    {
9421
      /* Near ret.  */
9422
0
      *branch_p = align_branch_ret;
9423
0
      if ((align_branch & align_branch_ret_bit))
9424
0
  add_padding = 1;
9425
0
    }
9426
0
  else
9427
0
    {
9428
      /* Check for indirect jmp, direct and indirect calls.  */
9429
0
      if (i.tm.base_opcode == 0xe8)
9430
0
  {
9431
    /* Direct call.  */
9432
0
    *branch_p = align_branch_call;
9433
0
    if ((align_branch & align_branch_call_bit))
9434
0
      add_padding = 1;
9435
0
  }
9436
0
      else if (i.tm.base_opcode == 0xff
9437
0
         && (i.tm.extension_opcode == 2
9438
0
       || i.tm.extension_opcode == 4))
9439
0
  {
9440
    /* Indirect call and jmp.  */
9441
0
    *branch_p = align_branch_indirect;
9442
0
    if ((align_branch & align_branch_indirect_bit))
9443
0
      add_padding = 1;
9444
0
  }
9445
9446
0
      if (add_padding
9447
0
    && i.disp_operands
9448
0
    && tls_get_addr
9449
0
    && (i.op[0].disps->X_op == O_symbol
9450
0
        || (i.op[0].disps->X_op == O_subtract
9451
0
      && i.op[0].disps->X_op_symbol == GOT_symbol)))
9452
0
  {
9453
0
    symbolS *s = i.op[0].disps->X_add_symbol;
9454
    /* No padding to call to global or undefined tls_get_addr.  */
9455
0
    if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9456
0
        && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9457
0
      return 0;
9458
0
  }
9459
0
    }
9460
9461
0
  if (add_padding
9462
0
      && last_insn.kind != last_insn_other
9463
0
      && last_insn.seg == now_seg)
9464
0
    {
9465
0
      if (flag_debug)
9466
0
  as_warn_where (last_insn.file, last_insn.line,
9467
0
           _("`%s` skips -malign-branch-boundary on `%s`"),
9468
0
           last_insn.name, insn_name (&i.tm));
9469
0
      return 0;
9470
0
    }
9471
9472
0
  return add_padding;
9473
0
}
9474
9475
static void
9476
output_insn (void)
9477
187k
{
9478
187k
  fragS *insn_start_frag;
9479
187k
  offsetT insn_start_off;
9480
187k
  fragS *fragP = NULL;
9481
187k
  enum align_branch_kind branch = align_branch_none;
9482
  /* The initializer is arbitrary just to avoid uninitialized error.
9483
     it's actually either assigned in add_branch_padding_frag_p
9484
     or never be used.  */
9485
187k
  enum mf_jcc_kind mf_jcc = mf_jcc_jo;
9486
9487
187k
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9488
187k
  if (IS_ELF && x86_used_note && now_seg != absolute_section)
9489
181k
    {
9490
181k
      if ((i.xstate & xstate_tmm) == xstate_tmm
9491
181k
    || i.tm.cpu_flags.bitfield.cpuamx_tile)
9492
2
  x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9493
9494
181k
      if (i.tm.cpu_flags.bitfield.cpu8087
9495
181k
    || i.tm.cpu_flags.bitfield.cpu287
9496
181k
    || i.tm.cpu_flags.bitfield.cpu387
9497
181k
    || i.tm.cpu_flags.bitfield.cpu687
9498
181k
    || i.tm.cpu_flags.bitfield.cpufisttp)
9499
2.04k
  x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
9500
9501
181k
      if ((i.xstate & xstate_mmx)
9502
181k
    || i.tm.mnem_off == MN_emms
9503
181k
    || i.tm.mnem_off == MN_femms)
9504
0
  x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
9505
9506
181k
      if (i.index_reg)
9507
0
  {
9508
0
    if (i.index_reg->reg_type.bitfield.zmmword)
9509
0
      i.xstate |= xstate_zmm;
9510
0
    else if (i.index_reg->reg_type.bitfield.ymmword)
9511
0
      i.xstate |= xstate_ymm;
9512
0
    else if (i.index_reg->reg_type.bitfield.xmmword)
9513
0
      i.xstate |= xstate_xmm;
9514
0
  }
9515
9516
      /* vzeroall / vzeroupper */
9517
181k
      if (i.tm.base_opcode == 0x77 && i.tm.cpu_flags.bitfield.cpuavx)
9518
0
  i.xstate |= xstate_ymm;
9519
9520
181k
      if ((i.xstate & xstate_xmm)
9521
    /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9522
181k
    || (i.tm.base_opcode == 0xae
9523
181k
        && (i.tm.cpu_flags.bitfield.cpusse
9524
0
      || i.tm.cpu_flags.bitfield.cpuavx))
9525
181k
    || i.tm.cpu_flags.bitfield.cpuwidekl
9526
181k
    || i.tm.cpu_flags.bitfield.cpukl)
9527
0
  x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
9528
9529
181k
      if ((i.xstate & xstate_ymm) == xstate_ymm)
9530
0
  x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
9531
181k
      if ((i.xstate & xstate_zmm) == xstate_zmm)
9532
0
  x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
9533
181k
      if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
9534
0
  x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
9535
181k
      if (i.tm.cpu_flags.bitfield.cpufxsr)
9536
0
  x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
9537
181k
      if (i.tm.cpu_flags.bitfield.cpuxsave)
9538
5
  x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
9539
181k
      if (i.tm.cpu_flags.bitfield.cpuxsaveopt)
9540
0
  x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
9541
181k
      if (i.tm.cpu_flags.bitfield.cpuxsavec)
9542
0
  x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
9543
9544
181k
      if (x86_feature_2_used
9545
181k
    || i.tm.cpu_flags.bitfield.cpucmov
9546
181k
    || i.tm.cpu_flags.bitfield.cpusyscall
9547
181k
    || i.tm.mnem_off == MN_cmpxchg8b)
9548
181k
  x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
9549
181k
      if (i.tm.cpu_flags.bitfield.cpusse3
9550
181k
    || i.tm.cpu_flags.bitfield.cpussse3
9551
181k
    || i.tm.cpu_flags.bitfield.cpusse4_1
9552
181k
    || i.tm.cpu_flags.bitfield.cpusse4_2
9553
181k
    || i.tm.cpu_flags.bitfield.cpucx16
9554
181k
    || i.tm.cpu_flags.bitfield.cpupopcnt
9555
    /* LAHF-SAHF insns in 64-bit mode.  */
9556
181k
    || (flag_code == CODE_64BIT
9557
181k
        && (i.tm.base_opcode | 1) == 0x9f
9558
181k
        && i.tm.opcode_space == SPACE_BASE))
9559
0
  x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
9560
181k
      if (i.tm.cpu_flags.bitfield.cpuavx
9561
181k
    || i.tm.cpu_flags.bitfield.cpuavx2
9562
    /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
9563
       XOP, FMA4, LPW, TBM, and AMX.  */
9564
181k
    || (i.tm.opcode_modifier.vex
9565
181k
        && !i.tm.cpu_flags.bitfield.cpuavx512f
9566
181k
        && !i.tm.cpu_flags.bitfield.cpuavx512bw
9567
181k
        && !i.tm.cpu_flags.bitfield.cpuavx512dq
9568
181k
        && !i.tm.cpu_flags.bitfield.cpuxop
9569
181k
        && !i.tm.cpu_flags.bitfield.cpufma4
9570
181k
        && !i.tm.cpu_flags.bitfield.cpulwp
9571
181k
        && !i.tm.cpu_flags.bitfield.cputbm
9572
181k
        && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
9573
181k
    || i.tm.cpu_flags.bitfield.cpuf16c
9574
181k
    || i.tm.cpu_flags.bitfield.cpufma
9575
181k
    || i.tm.cpu_flags.bitfield.cpulzcnt
9576
181k
    || i.tm.cpu_flags.bitfield.cpumovbe
9577
181k
    || i.tm.cpu_flags.bitfield.cpuxsaves
9578
181k
    || (x86_feature_2_used
9579
181k
        & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9580
181k
     | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9581
181k
     | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9582
143k
  x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
9583
181k
      if (i.tm.cpu_flags.bitfield.cpuavx512f
9584
181k
    || i.tm.cpu_flags.bitfield.cpuavx512bw
9585
181k
    || i.tm.cpu_flags.bitfield.cpuavx512dq
9586
181k
    || i.tm.cpu_flags.bitfield.cpuavx512vl
9587
    /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
9588
       AVX512-4FMAPS, and AVX512-4VNNIW.  */
9589
181k
    || (i.tm.opcode_modifier.evex
9590
181k
        && !i.tm.cpu_flags.bitfield.cpuavx512er
9591
181k
        && !i.tm.cpu_flags.bitfield.cpuavx512pf
9592
181k
        && !i.tm.cpu_flags.bitfield.cpuavx512_4fmaps
9593
181k
        && !i.tm.cpu_flags.bitfield.cpuavx512_4vnniw))
9594
0
  x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
9595
181k
    }
9596
187k
#endif
9597
9598
  /* Tie dwarf2 debug info to the address at the start of the insn.
9599
     We can't do this after the insn has been output as the current
9600
     frag may have been closed off.  eg. by frag_var.  */
9601
187k
  dwarf2_emit_insn (0);
9602
9603
187k
  insn_start_frag = frag_now;
9604
187k
  insn_start_off = frag_now_fix ();
9605
9606
187k
  if (add_branch_padding_frag_p (&branch, &mf_jcc))
9607
0
    {
9608
0
      char *p;
9609
      /* Branch can be 8 bytes.  Leave some room for prefixes.  */
9610
0
      unsigned int max_branch_padding_size = 14;
9611
9612
      /* Align section to boundary.  */
9613
0
      record_alignment (now_seg, align_branch_power);
9614
9615
      /* Make room for padding.  */
9616
0
      frag_grow (max_branch_padding_size);
9617
9618
      /* Start of the padding.  */
9619
0
      p = frag_more (0);
9620
9621
0
      fragP = frag_now;
9622
9623
0
      frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9624
0
    ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9625
0
    NULL, 0, p);
9626
9627
0
      fragP->tc_frag_data.mf_type = mf_jcc;
9628
0
      fragP->tc_frag_data.branch_type = branch;
9629
0
      fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9630
0
    }
9631
9632
187k
  if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
9633
187k
      && !pre_386_16bit_warned)
9634
0
    {
9635
0
      as_warn (_("use .code16 to ensure correct addressing mode"));
9636
0
      pre_386_16bit_warned = true;
9637
0
    }
9638
9639
  /* Output jumps.  */
9640
187k
  if (i.tm.opcode_modifier.jump == JUMP)
9641
4.82k
    output_branch ();
9642
182k
  else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9643
182k
     || i.tm.opcode_modifier.jump == JUMP_DWORD)
9644
351
    output_jump ();
9645
181k
  else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
9646
0
    output_interseg_jump ();
9647
181k
  else
9648
181k
    {
9649
      /* Output normal instructions here.  */
9650
181k
      char *p;
9651
181k
      unsigned char *q;
9652
181k
      unsigned int j;
9653
181k
      enum mf_cmp_kind mf_cmp;
9654
9655
181k
      if (avoid_fence
9656
181k
    && (i.tm.base_opcode == 0xaee8
9657
0
        || i.tm.base_opcode == 0xaef0
9658
0
        || i.tm.base_opcode == 0xaef8))
9659
0
  {
9660
    /* Encode lfence, mfence, and sfence as
9661
       f0 83 04 24 00   lock addl $0x0, (%{re}sp).  */
9662
0
    if (flag_code == CODE_16BIT)
9663
0
      as_bad (_("Cannot convert `%s' in 16-bit mode"), insn_name (&i.tm));
9664
0
    else if (omit_lock_prefix)
9665
0
      as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
9666
0
        insn_name (&i.tm));
9667
0
    else if (now_seg != absolute_section)
9668
0
      {
9669
0
        offsetT val = 0x240483f0ULL;
9670
9671
0
        p = frag_more (5);
9672
0
        md_number_to_chars (p, val, 5);
9673
0
      }
9674
0
    else
9675
0
      abs_section_offset += 5;
9676
0
    return;
9677
0
  }
9678
9679
      /* Some processors fail on LOCK prefix. This options makes
9680
   assembler ignore LOCK prefix and serves as a workaround.  */
9681
181k
      if (omit_lock_prefix)
9682
0
  {
9683
0
    if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9684
0
        && i.tm.opcode_modifier.isprefix)
9685
0
      return;
9686
0
    i.prefix[LOCK_PREFIX] = 0;
9687
0
  }
9688
9689
181k
      if (branch)
9690
  /* Skip if this is a branch.  */
9691
0
  ;
9692
181k
      else if (add_fused_jcc_padding_frag_p (&mf_cmp))
9693
0
  {
9694
    /* Make room for padding.  */
9695
0
    frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9696
0
    p = frag_more (0);
9697
9698
0
    fragP = frag_now;
9699
9700
0
    frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9701
0
        ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9702
0
        NULL, 0, p);
9703
9704
0
    fragP->tc_frag_data.mf_type = mf_cmp;
9705
0
    fragP->tc_frag_data.branch_type = align_branch_fused;
9706
0
    fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9707
0
  }
9708
181k
      else if (add_branch_prefix_frag_p ())
9709
0
  {
9710
0
    unsigned int max_prefix_size = align_branch_prefix_size;
9711
9712
    /* Make room for padding.  */
9713
0
    frag_grow (max_prefix_size);
9714
0
    p = frag_more (0);
9715
9716
0
    fragP = frag_now;
9717
9718
0
    frag_var (rs_machine_dependent, max_prefix_size, 0,
9719
0
        ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9720
0
        NULL, 0, p);
9721
9722
0
    fragP->tc_frag_data.max_bytes = max_prefix_size;
9723
0
  }
9724
9725
      /* Since the VEX/EVEX prefix contains the implicit prefix, we
9726
   don't need the explicit prefix.  */
9727
181k
      if (!is_any_vex_encoding (&i.tm))
9728
181k
  {
9729
181k
    switch (i.tm.opcode_modifier.opcodeprefix)
9730
181k
      {
9731
0
      case PREFIX_0X66:
9732
0
        add_prefix (0x66);
9733
0
        break;
9734
0
      case PREFIX_0XF2:
9735
0
        add_prefix (0xf2);
9736
0
        break;
9737
0
      case PREFIX_0XF3:
9738
0
        if (!i.tm.cpu_flags.bitfield.cpupadlock
9739
0
      || (i.prefix[REP_PREFIX] != 0xf3))
9740
0
    add_prefix (0xf3);
9741
0
        break;
9742
181k
      case PREFIX_NONE:
9743
181k
        switch (i.opcode_length)
9744
181k
    {
9745
15
    case 2:
9746
15
      break;
9747
181k
    case 1:
9748
      /* Check for pseudo prefixes.  */
9749
181k
      if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9750
181k
        break;
9751
9
      as_bad_where (insn_start_frag->fr_file,
9752
9
        insn_start_frag->fr_line,
9753
9
        _("pseudo prefix without instruction"));
9754
9
      return;
9755
0
    default:
9756
0
      abort ();
9757
181k
    }
9758
181k
        break;
9759
181k
      default:
9760
0
        abort ();
9761
181k
      }
9762
9763
181k
#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
9764
    /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9765
       R_X86_64_GOTTPOFF relocation so that linker can safely
9766
       perform IE->LE optimization.  A dummy REX_OPCODE prefix
9767
       is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9768
       relocation for GDesc -> IE/LE optimization.  */
9769
181k
    if (x86_elf_abi == X86_64_X32_ABI
9770
181k
        && i.operands == 2
9771
181k
        && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9772
0
      || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
9773
181k
        && i.prefix[REX_PREFIX] == 0)
9774
0
      add_prefix (REX_OPCODE);
9775
181k
#endif
9776
9777
    /* The prefix bytes.  */
9778
1.45M
    for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
9779
1.27M
      if (*q)
9780
1.97k
        frag_opcode_byte (*q);
9781
181k
  }
9782
2
      else
9783
2
  {
9784
16
    for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
9785
14
      if (*q)
9786
0
        switch (j)
9787
0
    {
9788
0
    case SEG_PREFIX:
9789
0
    case ADDR_PREFIX:
9790
0
      frag_opcode_byte (*q);
9791
0
      break;
9792
0
    default:
9793
      /* There should be no other prefixes for instructions
9794
         with VEX prefix.  */
9795
0
      abort ();
9796
0
    }
9797
9798
    /* For EVEX instructions i.vrex should become 0 after
9799
       build_evex_prefix.  For VEX instructions upper 16 registers
9800
       aren't available, so VREX should be 0.  */
9801
2
    if (i.vrex)
9802
0
      abort ();
9803
    /* Now the VEX prefix.  */
9804
2
    if (now_seg != absolute_section)
9805
2
      {
9806
2
        p = frag_more (i.vex.length);
9807
8
        for (j = 0; j < i.vex.length; j++)
9808
6
    p[j] = i.vex.bytes[j];
9809
2
      }
9810
0
    else
9811
0
      abs_section_offset += i.vex.length;
9812
2
  }
9813
9814
      /* Now the opcode; be careful about word order here!  */
9815
181k
      j = i.opcode_length;
9816
181k
      if (!i.vex.length)
9817
181k
  switch (i.tm.opcode_space)
9818
181k
    {
9819
178k
    case SPACE_BASE:
9820
178k
      break;
9821
3.20k
    case SPACE_0F:
9822
3.20k
      ++j;
9823
3.20k
      break;
9824
0
    case SPACE_0F38:
9825
0
    case SPACE_0F3A:
9826
0
      j += 2;
9827
0
      break;
9828
0
    default:
9829
0
      abort ();
9830
181k
    }
9831
9832
181k
      if (now_seg == absolute_section)
9833
3.63k
  abs_section_offset += j;
9834
178k
      else if (j == 1)
9835
175k
  {
9836
175k
    FRAG_APPEND_1_CHAR (i.tm.base_opcode);
9837
175k
  }
9838
3.20k
      else
9839
3.20k
  {
9840
3.20k
    p = frag_more (j);
9841
3.20k
    if (!i.vex.length
9842
3.20k
        && i.tm.opcode_space != SPACE_BASE)
9843
3.20k
      {
9844
3.20k
        *p++ = 0x0f;
9845
3.20k
        if (i.tm.opcode_space != SPACE_0F)
9846
0
    *p++ = i.tm.opcode_space == SPACE_0F38
9847
0
           ? 0x38 : 0x3a;
9848
3.20k
      }
9849
9850
3.20k
    switch (i.opcode_length)
9851
3.20k
      {
9852
17
      case 2:
9853
        /* Put out high byte first: can't use md_number_to_chars!  */
9854
17
        *p++ = (i.tm.base_opcode >> 8) & 0xff;
9855
        /* Fall through.  */
9856
3.20k
      case 1:
9857
3.20k
        *p = i.tm.base_opcode & 0xff;
9858
3.20k
        break;
9859
0
      default:
9860
0
        abort ();
9861
0
        break;
9862
3.20k
      }
9863
9864
3.20k
  }
9865
9866
      /* Now the modrm byte and sib byte (if present).  */
9867
181k
      if (i.tm.opcode_modifier.modrm)
9868
75.2k
  {
9869
75.2k
    frag_opcode_byte ((i.rm.regmem << 0)
9870
75.2k
           | (i.rm.reg << 3)
9871
75.2k
           | (i.rm.mode << 6));
9872
    /* If i.rm.regmem == ESP (4)
9873
       && i.rm.mode != (Register mode)
9874
       && not 16 bit
9875
       ==> need second modrm byte.  */
9876
75.2k
    if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
9877
75.2k
        && i.rm.mode != 3
9878
75.2k
        && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
9879
28.8k
      frag_opcode_byte ((i.sib.base << 0)
9880
28.8k
            | (i.sib.index << 3)
9881
28.8k
            | (i.sib.scale << 6));
9882
75.2k
  }
9883
9884
181k
      if (i.disp_operands)
9885
75.2k
  output_disp (insn_start_frag, insn_start_off);
9886
9887
181k
      if (i.imm_operands)
9888
73.3k
  output_imm (insn_start_frag, insn_start_off);
9889
9890
      /*
9891
       * frag_now_fix () returning plain abs_section_offset when we're in the
9892
       * absolute section, and abs_section_offset not getting updated as data
9893
       * gets added to the frag breaks the logic below.
9894
       */
9895
181k
      if (now_seg != absolute_section)
9896
178k
  {
9897
178k
    j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
9898
178k
    if (j > 15)
9899
0
      as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
9900
0
         j);
9901
178k
    else if (fragP)
9902
0
      {
9903
        /* NB: Don't add prefix with GOTPC relocation since
9904
     output_disp() above depends on the fixed encoding
9905
     length.  Can't add prefix with TLS relocation since
9906
     it breaks TLS linker optimization.  */
9907
0
        unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
9908
        /* Prefix count on the current instruction.  */
9909
0
        unsigned int count = i.vex.length;
9910
0
        unsigned int k;
9911
0
        for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
9912
    /* REX byte is encoded in VEX/EVEX prefix.  */
9913
0
    if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
9914
0
      count++;
9915
9916
        /* Count prefixes for extended opcode maps.  */
9917
0
        if (!i.vex.length)
9918
0
    switch (i.tm.opcode_space)
9919
0
      {
9920
0
      case SPACE_BASE:
9921
0
        break;
9922
0
      case SPACE_0F:
9923
0
        count++;
9924
0
        break;
9925
0
      case SPACE_0F38:
9926
0
      case SPACE_0F3A:
9927
0
        count += 2;
9928
0
        break;
9929
0
      default:
9930
0
        abort ();
9931
0
      }
9932
9933
0
        if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
9934
0
      == BRANCH_PREFIX)
9935
0
    {
9936
      /* Set the maximum prefix size in BRANCH_PREFIX
9937
         frag.  */
9938
0
      if (fragP->tc_frag_data.max_bytes > max)
9939
0
        fragP->tc_frag_data.max_bytes = max;
9940
0
      if (fragP->tc_frag_data.max_bytes > count)
9941
0
        fragP->tc_frag_data.max_bytes -= count;
9942
0
      else
9943
0
        fragP->tc_frag_data.max_bytes = 0;
9944
0
    }
9945
0
        else
9946
0
    {
9947
      /* Remember the maximum prefix size in FUSED_JCC_PADDING
9948
         frag.  */
9949
0
      unsigned int max_prefix_size;
9950
0
      if (align_branch_prefix_size > max)
9951
0
        max_prefix_size = max;
9952
0
      else
9953
0
        max_prefix_size = align_branch_prefix_size;
9954
0
      if (max_prefix_size > count)
9955
0
        fragP->tc_frag_data.max_prefix_length
9956
0
          = max_prefix_size - count;
9957
0
    }
9958
9959
        /* Use existing segment prefix if possible.  Use CS
9960
     segment prefix in 64-bit mode.  In 32-bit mode, use SS
9961
     segment prefix with ESP/EBP base register and use DS
9962
     segment prefix without ESP/EBP base register.  */
9963
0
        if (i.prefix[SEG_PREFIX])
9964
0
    fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
9965
0
        else if (flag_code == CODE_64BIT)
9966
0
    fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
9967
0
        else if (i.base_reg
9968
0
           && (i.base_reg->reg_num == 4
9969
0
         || i.base_reg->reg_num == 5))
9970
0
    fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
9971
0
        else
9972
0
    fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
9973
0
      }
9974
178k
  }
9975
181k
    }
9976
9977
  /* NB: Don't work with COND_JUMP86 without i386.  */
9978
187k
  if (align_branch_power
9979
187k
      && now_seg != absolute_section
9980
187k
      && cpu_arch_flags.bitfield.cpui386)
9981
0
    {
9982
      /* Terminate each frag so that we can add prefix and check for
9983
         fused jcc.  */
9984
0
      frag_wane (frag_now);
9985
0
      frag_new (0);
9986
0
    }
9987
9988
#ifdef DEBUG386
9989
  if (flag_debug)
9990
    {
9991
      pi ("" /*line*/, &i);
9992
    }
9993
#endif /* DEBUG386  */
9994
187k
}
9995
9996
/* Return the size of the displacement operand N.  */
9997
9998
static int
9999
disp_size (unsigned int n)
10000
75.2k
{
10001
75.2k
  int size = 4;
10002
10003
75.2k
  if (i.types[n].bitfield.disp64)
10004
0
    size = 8;
10005
75.2k
  else if (i.types[n].bitfield.disp8)
10006
0
    size = 1;
10007
75.2k
  else if (i.types[n].bitfield.disp16)
10008
44.0k
    size = 2;
10009
75.2k
  return size;
10010
75.2k
}
10011
10012
/* Return the size of the immediate operand N.  */
10013
10014
static int
10015
imm_size (unsigned int n)
10016
73.3k
{
10017
73.3k
  int size = 4;
10018
73.3k
  if (i.types[n].bitfield.imm64)
10019
0
    size = 8;
10020
73.3k
  else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
10021
62.6k
    size = 1;
10022
10.6k
  else if (i.types[n].bitfield.imm16)
10023
7.16k
    size = 2;
10024
73.3k
  return size;
10025
73.3k
}
10026
10027
static void
10028
output_disp (fragS *insn_start_frag, offsetT insn_start_off)
10029
75.2k
{
10030
75.2k
  char *p;
10031
75.2k
  unsigned int n;
10032
10033
222k
  for (n = 0; n < i.operands; n++)
10034
147k
    {
10035
147k
      if (operand_type_check (i.types[n], disp))
10036
75.2k
  {
10037
75.2k
    int size = disp_size (n);
10038
10039
75.2k
    if (now_seg == absolute_section)
10040
1.16k
      abs_section_offset += size;
10041
74.1k
    else if (i.op[n].disps->X_op == O_constant)
10042
68.9k
      {
10043
68.9k
        offsetT val = i.op[n].disps->X_add_number;
10044
10045
68.9k
        val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
10046
68.9k
             size);
10047
68.9k
        p = frag_more (size);
10048
68.9k
        md_number_to_chars (p, val, size);
10049
68.9k
      }
10050
5.13k
    else
10051
5.13k
      {
10052
5.13k
        enum bfd_reloc_code_real reloc_type;
10053
5.13k
        bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
10054
5.13k
        bool sign = (flag_code == CODE_64BIT && size == 4
10055
5.13k
         && (!want_disp32 (&i.tm)
10056
1.33k
             || (i.tm.opcode_modifier.jump && !i.jumpabsolute
10057
0
           && !i.types[n].bitfield.baseindex)))
10058
5.13k
        || pcrel;
10059
5.13k
        fixS *fixP;
10060
10061
        /* We can't have 8 bit displacement here.  */
10062
5.13k
        gas_assert (!i.types[n].bitfield.disp8);
10063
10064
        /* The PC relative address is computed relative
10065
     to the instruction boundary, so in case immediate
10066
     fields follows, we need to adjust the value.  */
10067
5.13k
        if (pcrel && i.imm_operands)
10068
0
    {
10069
0
      unsigned int n1;
10070
0
      int sz = 0;
10071
10072
0
      for (n1 = 0; n1 < i.operands; n1++)
10073
0
        if (operand_type_check (i.types[n1], imm))
10074
0
          {
10075
      /* Only one immediate is allowed for PC
10076
         relative address, except with .insn.  */
10077
0
      gas_assert (sz == 0 || dot_insn ());
10078
0
      sz += imm_size (n1);
10079
0
          }
10080
      /* We should find at least one immediate.  */
10081
0
      gas_assert (sz != 0);
10082
0
      i.op[n].disps->X_add_number -= sz;
10083
0
    }
10084
10085
0
        p = frag_more (size);
10086
5.13k
        reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
10087
5.13k
        if (GOT_symbol
10088
5.13k
      && GOT_symbol == i.op[n].disps->X_add_symbol
10089
5.13k
      && (((reloc_type == BFD_RELOC_32
10090
0
      || reloc_type == BFD_RELOC_X86_64_32S
10091
0
      || (reloc_type == BFD_RELOC_64
10092
0
          && object_64bit))
10093
0
           && (i.op[n].disps->X_op == O_symbol
10094
0
         || (i.op[n].disps->X_op == O_add
10095
0
             && ((symbol_get_value_expression
10096
0
            (i.op[n].disps->X_op_symbol)->X_op)
10097
0
           == O_subtract))))
10098
0
          || reloc_type == BFD_RELOC_32_PCREL))
10099
0
    {
10100
0
      if (!object_64bit)
10101
0
        {
10102
0
          reloc_type = BFD_RELOC_386_GOTPC;
10103
0
          i.has_gotpc_tls_reloc = true;
10104
0
          i.op[n].disps->X_add_number +=
10105
0
      encoding_length (insn_start_frag, insn_start_off, p);
10106
0
        }
10107
0
      else if (reloc_type == BFD_RELOC_64)
10108
0
        reloc_type = BFD_RELOC_X86_64_GOTPC64;
10109
0
      else
10110
        /* Don't do the adjustment for x86-64, as there
10111
           the pcrel addressing is relative to the _next_
10112
           insn, and that is taken care of in other code.  */
10113
0
        reloc_type = BFD_RELOC_X86_64_GOTPC32;
10114
0
    }
10115
5.13k
        else if (align_branch_power)
10116
0
    {
10117
0
      switch (reloc_type)
10118
0
        {
10119
0
        case BFD_RELOC_386_TLS_GD:
10120
0
        case BFD_RELOC_386_TLS_LDM:
10121
0
        case BFD_RELOC_386_TLS_IE:
10122
0
        case BFD_RELOC_386_TLS_IE_32:
10123
0
        case BFD_RELOC_386_TLS_GOTIE:
10124
0
        case BFD_RELOC_386_TLS_GOTDESC:
10125
0
        case BFD_RELOC_386_TLS_DESC_CALL:
10126
0
        case BFD_RELOC_X86_64_TLSGD:
10127
0
        case BFD_RELOC_X86_64_TLSLD:
10128
0
        case BFD_RELOC_X86_64_GOTTPOFF:
10129
0
        case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10130
0
        case BFD_RELOC_X86_64_TLSDESC_CALL:
10131
0
          i.has_gotpc_tls_reloc = true;
10132
0
        default:
10133
0
          break;
10134
0
        }
10135
0
    }
10136
5.13k
        fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10137
5.13k
          size, i.op[n].disps, pcrel,
10138
5.13k
          reloc_type);
10139
10140
5.13k
        if (flag_code == CODE_64BIT && size == 4 && pcrel
10141
5.13k
      && !i.prefix[ADDR_PREFIX])
10142
0
    fixP->fx_signed = 1;
10143
10144
        /* Check for "call/jmp *mem", "mov mem, %reg",
10145
     "test %reg, mem" and "binop mem, %reg" where binop
10146
     is one of adc, add, and, cmp, or, sbb, sub, xor
10147
     instructions without data prefix.  Always generate
10148
     R_386_GOT32X for "sym*GOT" operand in 32-bit mode.  */
10149
5.13k
        if (i.prefix[DATA_PREFIX] == 0
10150
5.13k
      && (generate_relax_relocations
10151
5.13k
          || (!object_64bit
10152
0
        && i.rm.mode == 0
10153
0
        && i.rm.regmem == 5))
10154
5.13k
      && (i.rm.mode == 2
10155
5.13k
          || (i.rm.mode == 0 && i.rm.regmem == 5))
10156
5.13k
      && i.tm.opcode_space == SPACE_BASE
10157
5.13k
      && ((i.operands == 1
10158
1.65k
           && i.tm.base_opcode == 0xff
10159
1.65k
           && (i.rm.reg == 2 || i.rm.reg == 4))
10160
1.65k
          || (i.operands == 2
10161
1.65k
        && (i.tm.base_opcode == 0x8b
10162
202
            || i.tm.base_opcode == 0x85
10163
202
            || (i.tm.base_opcode & ~0x38) == 0x03))))
10164
0
    {
10165
0
      if (object_64bit)
10166
0
        {
10167
0
          fixP->fx_tcbit = i.rex != 0;
10168
0
          if (i.base_reg
10169
0
        && (i.base_reg->reg_num == RegIP))
10170
0
          fixP->fx_tcbit2 = 1;
10171
0
        }
10172
0
      else
10173
0
        fixP->fx_tcbit2 = 1;
10174
0
    }
10175
5.13k
      }
10176
75.2k
  }
10177
147k
    }
10178
75.2k
}
10179
10180
static void
10181
output_imm (fragS *insn_start_frag, offsetT insn_start_off)
10182
73.3k
{
10183
73.3k
  char *p;
10184
73.3k
  unsigned int n;
10185
10186
218k
  for (n = 0; n < i.operands; n++)
10187
145k
    {
10188
145k
      if (operand_type_check (i.types[n], imm))
10189
73.3k
  {
10190
73.3k
    int size = imm_size (n);
10191
10192
73.3k
    if (now_seg == absolute_section)
10193
1.30k
      abs_section_offset += size;
10194
72.0k
    else if (i.op[n].imms->X_op == O_constant)
10195
61.4k
      {
10196
61.4k
        offsetT val;
10197
10198
61.4k
        val = offset_in_range (i.op[n].imms->X_add_number,
10199
61.4k
             size);
10200
61.4k
        p = frag_more (size);
10201
61.4k
        md_number_to_chars (p, val, size);
10202
61.4k
      }
10203
10.5k
    else
10204
10.5k
      {
10205
        /* Not absolute_section.
10206
     Need a 32-bit fixup (don't support 8bit
10207
     non-absolute imms).  Try to support other
10208
     sizes ...  */
10209
10.5k
        enum bfd_reloc_code_real reloc_type;
10210
10.5k
        int sign;
10211
10212
10.5k
        if (i.types[n].bitfield.imm32s
10213
10.5k
      && (i.suffix == QWORD_MNEM_SUFFIX
10214
0
          || (!i.suffix && i.tm.opcode_modifier.no_lsuf)
10215
0
          || dot_insn ()))
10216
0
    sign = 1;
10217
10.5k
        else
10218
10.5k
    sign = 0;
10219
10220
10.5k
        p = frag_more (size);
10221
10.5k
        reloc_type = reloc (size, 0, sign, i.reloc[n]);
10222
10223
        /*   This is tough to explain.  We end up with this one if we
10224
         * have operands that look like
10225
         * "_GLOBAL_OFFSET_TABLE_+[.-.L284]".  The goal here is to
10226
         * obtain the absolute address of the GOT, and it is strongly
10227
         * preferable from a performance point of view to avoid using
10228
         * a runtime relocation for this.  The actual sequence of
10229
         * instructions often look something like:
10230
         *
10231
         *  call  .L66
10232
         * .L66:
10233
         *  popl  %ebx
10234
         *  addl  $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10235
         *
10236
         *   The call and pop essentially return the absolute address
10237
         * of the label .L66 and store it in %ebx.  The linker itself
10238
         * will ultimately change the first operand of the addl so
10239
         * that %ebx points to the GOT, but to keep things simple, the
10240
         * .o file must have this operand set so that it generates not
10241
         * the absolute address of .L66, but the absolute address of
10242
         * itself.  This allows the linker itself simply treat a GOTPC
10243
         * relocation as asking for a pcrel offset to the GOT to be
10244
         * added in, and the addend of the relocation is stored in the
10245
         * operand field for the instruction itself.
10246
         *
10247
         *   Our job here is to fix the operand so that it would add
10248
         * the correct offset so that %ebx would point to itself.  The
10249
         * thing that is tricky is that .-.L66 will point to the
10250
         * beginning of the instruction, so we need to further modify
10251
         * the operand so that it will point to itself.  There are
10252
         * other cases where you have something like:
10253
         *
10254
         *  .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10255
         *
10256
         * and here no correction would be required.  Internally in
10257
         * the assembler we treat operands of this form as not being
10258
         * pcrel since the '.' is explicitly mentioned, and I wonder
10259
         * whether it would simplify matters to do it this way.  Who
10260
         * knows.  In earlier versions of the PIC patches, the
10261
         * pcrel_adjust field was used to store the correction, but
10262
         * since the expression is not pcrel, I felt it would be
10263
         * confusing to do it this way.  */
10264
10265
10.5k
        if ((reloc_type == BFD_RELOC_32
10266
10.5k
       || reloc_type == BFD_RELOC_X86_64_32S
10267
10.5k
       || reloc_type == BFD_RELOC_64)
10268
10.5k
      && GOT_symbol
10269
10.5k
      && GOT_symbol == i.op[n].imms->X_add_symbol
10270
10.5k
      && (i.op[n].imms->X_op == O_symbol
10271
0
          || (i.op[n].imms->X_op == O_add
10272
0
        && ((symbol_get_value_expression
10273
0
             (i.op[n].imms->X_op_symbol)->X_op)
10274
0
            == O_subtract))))
10275
0
    {
10276
0
      if (!object_64bit)
10277
0
        reloc_type = BFD_RELOC_386_GOTPC;
10278
0
      else if (size == 4)
10279
0
        reloc_type = BFD_RELOC_X86_64_GOTPC32;
10280
0
      else if (size == 8)
10281
0
        reloc_type = BFD_RELOC_X86_64_GOTPC64;
10282
0
      i.has_gotpc_tls_reloc = true;
10283
0
      i.op[n].imms->X_add_number +=
10284
0
        encoding_length (insn_start_frag, insn_start_off, p);
10285
0
    }
10286
10.5k
        fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10287
10.5k
         i.op[n].imms, 0, reloc_type);
10288
10.5k
      }
10289
73.3k
  }
10290
145k
    }
10291
73.3k
}
10292

10293
/* x86_cons_fix_new is called via the expression parsing code when a
10294
   reloc is needed.  We use this hook to get the correct .got reloc.  */
10295
static int cons_sign = -1;
10296
10297
void
10298
x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
10299
      expressionS *exp, bfd_reloc_code_real_type r)
10300
67.0k
{
10301
67.0k
  r = reloc (len, 0, cons_sign, r);
10302
10303
#ifdef TE_PE
10304
  if (exp->X_op == O_secrel)
10305
    {
10306
      exp->X_op = O_symbol;
10307
      r = BFD_RELOC_32_SECREL;
10308
    }
10309
  else if (exp->X_op == O_secidx)
10310
    r = BFD_RELOC_16_SECIDX;
10311
#endif
10312
10313
67.0k
  fix_new_exp (frag, off, len, exp, 0, r);
10314
67.0k
}
10315
10316
/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10317
   purpose of the `.dc.a' internal pseudo-op.  */
10318
10319
int
10320
x86_address_bytes (void)
10321
0
{
10322
0
  if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10323
0
    return 4;
10324
0
  return stdoutput->arch_info->bits_per_address / 8;
10325
0
}
10326
10327
#if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10328
     || defined (LEX_AT)) && !defined (TE_PE)
10329
# define lex_got(reloc, adjust, types) NULL
10330
#else
10331
/* Parse operands of the form
10332
   <symbol>@GOTOFF+<nnn>
10333
   and similar .plt or .got references.
10334
10335
   If we find one, set up the correct relocation in RELOC and copy the
10336
   input string, minus the `@GOTOFF' into a malloc'd buffer for
10337
   parsing by the calling routine.  Return this buffer, and if ADJUST
10338
   is non-null set it to the length of the string we removed from the
10339
   input line.  Otherwise return NULL.  */
10340
static char *
10341
lex_got (enum bfd_reloc_code_real *rel,
10342
   int *adjust,
10343
   i386_operand_type *types)
10344
229k
{
10345
  /* Some of the relocations depend on the size of what field is to
10346
     be relocated.  But in our callers i386_immediate and i386_displacement
10347
     we don't yet know the operand size (this will be set by insn
10348
     matching).  Hence we record the word32 relocation here,
10349
     and adjust the reloc according to the real size in reloc().  */
10350
229k
  static const struct
10351
229k
  {
10352
229k
    const char *str;
10353
229k
    int len;
10354
229k
    const enum bfd_reloc_code_real rel[2];
10355
229k
    const i386_operand_type types64;
10356
229k
    bool need_GOT_symbol;
10357
229k
  }
10358
229k
    gotrel[] =
10359
229k
  {
10360
10361
1.60M
#define OPERAND_TYPE_IMM32_32S_DISP32 { .bitfield = \
10362
1.60M
  { .imm32 = 1, .imm32s = 1, .disp32 = 1 } }
10363
229k
#define OPERAND_TYPE_IMM32_32S_64_DISP32 { .bitfield = \
10364
229k
  { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1 } }
10365
459k
#define OPERAND_TYPE_IMM32_32S_64_DISP32_64 { .bitfield = \
10366
459k
  { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1, .disp64 = 1 } }
10367
459k
#define OPERAND_TYPE_IMM64_DISP64 { .bitfield = \
10368
459k
  { .imm64 = 1, .disp64 = 1 } }
10369
10370
229k
#ifndef TE_PE
10371
229k
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
10372
229k
    { STRING_COMMA_LEN ("SIZE"),      { BFD_RELOC_SIZE32,
10373
229k
          BFD_RELOC_SIZE32 },
10374
229k
      { .bitfield = { .imm32 = 1, .imm64 = 1 } }, false },
10375
229k
#endif
10376
229k
    { STRING_COMMA_LEN ("PLTOFF"),   { _dummy_first_bfd_reloc_code_real,
10377
229k
               BFD_RELOC_X86_64_PLTOFF64 },
10378
229k
      { .bitfield = { .imm64 = 1 } }, true },
10379
229k
    { STRING_COMMA_LEN ("PLT"),      { BFD_RELOC_386_PLT32,
10380
229k
               BFD_RELOC_X86_64_PLT32    },
10381
229k
      OPERAND_TYPE_IMM32_32S_DISP32, false },
10382
229k
    { STRING_COMMA_LEN ("GOTPLT"),   { _dummy_first_bfd_reloc_code_real,
10383
229k
               BFD_RELOC_X86_64_GOTPLT64 },
10384
229k
      OPERAND_TYPE_IMM64_DISP64, true },
10385
229k
    { STRING_COMMA_LEN ("GOTOFF"),   { BFD_RELOC_386_GOTOFF,
10386
229k
               BFD_RELOC_X86_64_GOTOFF64 },
10387
229k
      OPERAND_TYPE_IMM64_DISP64, true },
10388
229k
    { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10389
229k
               BFD_RELOC_X86_64_GOTPCREL },
10390
229k
      OPERAND_TYPE_IMM32_32S_DISP32, true },
10391
229k
    { STRING_COMMA_LEN ("TLSGD"),    { BFD_RELOC_386_TLS_GD,
10392
229k
               BFD_RELOC_X86_64_TLSGD    },
10393
229k
      OPERAND_TYPE_IMM32_32S_DISP32, true },
10394
229k
    { STRING_COMMA_LEN ("TLSLDM"),   { BFD_RELOC_386_TLS_LDM,
10395
229k
               _dummy_first_bfd_reloc_code_real },
10396
229k
      OPERAND_TYPE_NONE, true },
10397
229k
    { STRING_COMMA_LEN ("TLSLD"),    { _dummy_first_bfd_reloc_code_real,
10398
229k
               BFD_RELOC_X86_64_TLSLD    },
10399
229k
      OPERAND_TYPE_IMM32_32S_DISP32, true },
10400
229k
    { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10401
229k
               BFD_RELOC_X86_64_GOTTPOFF },
10402
229k
      OPERAND_TYPE_IMM32_32S_DISP32, true },
10403
229k
    { STRING_COMMA_LEN ("TPOFF"),    { BFD_RELOC_386_TLS_LE_32,
10404
229k
               BFD_RELOC_X86_64_TPOFF32  },
10405
229k
      OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
10406
229k
    { STRING_COMMA_LEN ("NTPOFF"),   { BFD_RELOC_386_TLS_LE,
10407
229k
               _dummy_first_bfd_reloc_code_real },
10408
229k
      OPERAND_TYPE_NONE, true },
10409
229k
    { STRING_COMMA_LEN ("DTPOFF"),   { BFD_RELOC_386_TLS_LDO_32,
10410
229k
               BFD_RELOC_X86_64_DTPOFF32 },
10411
229k
      OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
10412
229k
    { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10413
229k
               _dummy_first_bfd_reloc_code_real },
10414
229k
      OPERAND_TYPE_NONE, true },
10415
229k
    { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10416
229k
               _dummy_first_bfd_reloc_code_real },
10417
229k
      OPERAND_TYPE_NONE, true },
10418
229k
    { STRING_COMMA_LEN ("GOT"),      { BFD_RELOC_386_GOT32,
10419
229k
               BFD_RELOC_X86_64_GOT32    },
10420
229k
      OPERAND_TYPE_IMM32_32S_64_DISP32, true },
10421
229k
    { STRING_COMMA_LEN ("TLSDESC"),  { BFD_RELOC_386_TLS_GOTDESC,
10422
229k
               BFD_RELOC_X86_64_GOTPC32_TLSDESC },
10423
229k
      OPERAND_TYPE_IMM32_32S_DISP32, true },
10424
229k
    { STRING_COMMA_LEN ("TLSCALL"),  { BFD_RELOC_386_TLS_DESC_CALL,
10425
229k
               BFD_RELOC_X86_64_TLSDESC_CALL },
10426
229k
      OPERAND_TYPE_IMM32_32S_DISP32, true },
10427
#else /* TE_PE */
10428
    { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10429
               BFD_RELOC_32_SECREL },
10430
      OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
10431
#endif
10432
10433
229k
#undef OPERAND_TYPE_IMM32_32S_DISP32
10434
229k
#undef OPERAND_TYPE_IMM32_32S_64_DISP32
10435
229k
#undef OPERAND_TYPE_IMM32_32S_64_DISP32_64
10436
229k
#undef OPERAND_TYPE_IMM64_DISP64
10437
10438
229k
  };
10439
229k
  char *cp;
10440
229k
  unsigned int j;
10441
10442
#if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
10443
  if (!IS_ELF)
10444
    return NULL;
10445
#endif
10446
10447
1.05M
  for (cp = input_line_pointer; *cp != '@'; cp++)
10448
1.05M
    if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
10449
226k
      return NULL;
10450
10451
43.9k
  for (j = 0; j < ARRAY_SIZE (gotrel); j++)
10452
42.0k
    {
10453
42.0k
      int len = gotrel[j].len;
10454
42.0k
      if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
10455
1.16k
  {
10456
1.16k
    if (gotrel[j].rel[object_64bit] != 0)
10457
1.16k
      {
10458
1.16k
        int first, second;
10459
1.16k
        char *tmpbuf, *past_reloc;
10460
10461
1.16k
        *rel = gotrel[j].rel[object_64bit];
10462
10463
1.16k
        if (types)
10464
997
    {
10465
997
      if (flag_code != CODE_64BIT)
10466
747
        {
10467
747
          types->bitfield.imm32 = 1;
10468
747
          types->bitfield.disp32 = 1;
10469
747
        }
10470
250
      else
10471
250
        *types = gotrel[j].types64;
10472
997
    }
10473
10474
1.16k
        if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
10475
1
    GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10476
10477
        /* The length of the first part of our input line.  */
10478
1.16k
        first = cp - input_line_pointer;
10479
10480
        /* The second part goes from after the reloc token until
10481
     (and including) an end_of_line char or comma.  */
10482
1.16k
        past_reloc = cp + 1 + len;
10483
1.16k
        cp = past_reloc;
10484
7.77k
        while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10485
6.60k
    ++cp;
10486
1.16k
        second = cp + 1 - past_reloc;
10487
10488
        /* Allocate and copy string.  The trailing NUL shouldn't
10489
     be necessary, but be safe.  */
10490
1.16k
        tmpbuf = XNEWVEC (char, first + second + 2);
10491
1.16k
        memcpy (tmpbuf, input_line_pointer, first);
10492
1.16k
        if (second != 0 && *past_reloc != ' ')
10493
    /* Replace the relocation token with ' ', so that
10494
       errors like foo@GOTOFF1 will be detected.  */
10495
1.15k
    tmpbuf[first++] = ' ';
10496
11
        else
10497
    /* Increment length by 1 if the relocation token is
10498
       removed.  */
10499
11
    len++;
10500
1.16k
        if (adjust)
10501
170
    *adjust = len;
10502
1.16k
        memcpy (tmpbuf + first, past_reloc, second);
10503
1.16k
        tmpbuf[first + second] = '\0';
10504
1.16k
        return tmpbuf;
10505
1.16k
      }
10506
10507
0
    as_bad (_("@%s reloc is not supported with %d-bit output format"),
10508
0
      gotrel[j].str, 1 << (5 + object_64bit));
10509
0
    return NULL;
10510
1.16k
  }
10511
42.0k
    }
10512
10513
  /* Might be a symbol version string.  Don't as_bad here.  */
10514
1.91k
  return NULL;
10515
3.08k
}
10516
#endif
10517
10518
bfd_reloc_code_real_type
10519
x86_cons (expressionS *exp, int size)
10520
9.85k
{
10521
9.85k
  bfd_reloc_code_real_type got_reloc = NO_RELOC;
10522
10523
9.85k
  intel_syntax = -intel_syntax;
10524
9.85k
  exp->X_md = 0;
10525
9.85k
  expr_mode = expr_operator_none;
10526
10527
9.85k
#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10528
9.85k
      && !defined (LEX_AT)) \
10529
9.85k
    || defined (TE_PE)
10530
9.85k
  if (size == 4 || (object_64bit && size == 8))
10531
9.34k
    {
10532
      /* Handle @GOTOFF and the like in an expression.  */
10533
9.34k
      char *save;
10534
9.34k
      char *gotfree_input_line;
10535
9.34k
      int adjust = 0;
10536
10537
9.34k
      save = input_line_pointer;
10538
9.34k
      gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
10539
9.34k
      if (gotfree_input_line)
10540
170
  input_line_pointer = gotfree_input_line;
10541
10542
9.34k
      expression (exp);
10543
10544
9.34k
      if (gotfree_input_line)
10545
170
  {
10546
    /* expression () has merrily parsed up to the end of line,
10547
       or a comma - in the wrong buffer.  Transfer how far
10548
       input_line_pointer has moved to the right buffer.  */
10549
170
    input_line_pointer = (save
10550
170
        + (input_line_pointer - gotfree_input_line)
10551
170
        + adjust);
10552
170
    free (gotfree_input_line);
10553
170
    if (exp->X_op == O_constant
10554
170
        || exp->X_op == O_absent
10555
170
        || exp->X_op == O_illegal
10556
170
        || exp->X_op == O_register
10557
170
        || exp->X_op == O_big)
10558
17
      {
10559
17
        char c = *input_line_pointer;
10560
17
        *input_line_pointer = 0;
10561
17
        as_bad (_("missing or invalid expression `%s'"), save);
10562
17
        *input_line_pointer = c;
10563
17
      }
10564
153
    else if ((got_reloc == BFD_RELOC_386_PLT32
10565
153
        || got_reloc == BFD_RELOC_X86_64_PLT32)
10566
153
       && exp->X_op != O_symbol)
10567
0
      {
10568
0
        char c = *input_line_pointer;
10569
0
        *input_line_pointer = 0;
10570
0
        as_bad (_("invalid PLT expression `%s'"), save);
10571
0
        *input_line_pointer = c;
10572
0
      }
10573
170
  }
10574
9.34k
    }
10575
512
  else
10576
512
#endif
10577
512
    expression (exp);
10578
10579
9.85k
  intel_syntax = -intel_syntax;
10580
10581
9.85k
  if (intel_syntax)
10582
0
    i386_intel_simplify (exp);
10583
10584
  /* If not 64bit, massage value, to account for wraparound when !BFD64.  */
10585
9.85k
  if (size <= 4 && expr_mode == expr_operator_present
10586
9.85k
      && exp->X_op == O_constant && !object_64bit)
10587
0
    exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10588
10589
9.85k
  return got_reloc;
10590
9.85k
}
10591
10592
static void
10593
signed_cons (int size)
10594
0
{
10595
0
  if (object_64bit)
10596
0
    cons_sign = 1;
10597
0
  cons (size);
10598
0
  cons_sign = -1;
10599
0
}
10600
10601
static void
10602
s_insn (int dummy ATTRIBUTE_UNUSED)
10603
101
{
10604
101
  char mnemonic[MAX_MNEM_SIZE], *line = input_line_pointer, *ptr;
10605
101
  char *saved_ilp = find_end_of_line (line, false), saved_char;
10606
101
  const char *end;
10607
101
  unsigned int j;
10608
101
  valueT val;
10609
101
  bool vex = false, xop = false, evex = false;
10610
101
  static const templates tt = { &i.tm, &i.tm + 1 };
10611
10612
101
  init_globals ();
10613
10614
101
  saved_char = *saved_ilp;
10615
101
  *saved_ilp = 0;
10616
10617
101
  end = parse_insn (line, mnemonic, true);
10618
101
  if (end == NULL)
10619
61
    {
10620
61
  bad:
10621
61
      *saved_ilp = saved_char;
10622
61
      ignore_rest_of_line ();
10623
61
      i.tm.mnem_off = 0;
10624
61
      return;
10625
61
    }
10626
40
  line += end - line;
10627
10628
40
  current_templates = &tt;
10629
40
  i.tm.mnem_off = MN__insn;
10630
40
  i.tm.extension_opcode = None;
10631
10632
40
  if (startswith (line, "VEX")
10633
40
      && (line[3] == '.' || is_space_char (line[3])))
10634
0
    {
10635
0
      vex = true;
10636
0
      line += 3;
10637
0
    }
10638
40
  else if (startswith (line, "XOP") && ISDIGIT (line[3]))
10639
0
    {
10640
0
      char *e;
10641
0
      unsigned long n = strtoul (line + 3, &e, 16);
10642
10643
0
      if (e == line + 5 && n >= 0x08 && n <= 0x1f
10644
0
    && (*e == '.' || is_space_char (*e)))
10645
0
  {
10646
0
    xop = true;
10647
    /* Arrange for build_vex_prefix() to emit 0x8f.  */
10648
0
    i.tm.opcode_space = SPACE_XOP08;
10649
0
    i.insn_opcode_space = n;
10650
0
    line = e;
10651
0
  }
10652
0
    }
10653
40
  else if (startswith (line, "EVEX")
10654
40
     && (line[4] == '.' || is_space_char (line[4])))
10655
0
    {
10656
0
      evex = true;
10657
0
      line += 4;
10658
0
    }
10659
10660
40
  if (vex || xop
10661
40
      ? i.vec_encoding == vex_encoding_evex
10662
40
      : evex
10663
40
  ? i.vec_encoding == vex_encoding_vex
10664
0
    || i.vec_encoding == vex_encoding_vex3
10665
40
  : i.vec_encoding != vex_encoding_default)
10666
0
    {
10667
0
      as_bad (_("pseudo-prefix conflicts with encoding specifier"));
10668
0
      goto bad;
10669
0
    }
10670
10671
40
  if (line > end && i.vec_encoding == vex_encoding_default)
10672
0
    i.vec_encoding = evex ? vex_encoding_evex : vex_encoding_vex;
10673
10674
40
  if (line > end && *line == '.')
10675
0
    {
10676
      /* Length specifier (VEX.L, XOP.L, EVEX.L'L).  */
10677
0
      switch (line[1])
10678
0
  {
10679
0
  case 'L':
10680
0
    switch (line[2])
10681
0
      {
10682
0
      case '0':
10683
0
        if (evex)
10684
0
    i.tm.opcode_modifier.evex = EVEX128;
10685
0
        else
10686
0
    i.tm.opcode_modifier.vex = VEX128;
10687
0
        break;
10688
10689
0
      case '1':
10690
0
        if (evex)
10691
0
    i.tm.opcode_modifier.evex = EVEX256;
10692
0
        else
10693
0
    i.tm.opcode_modifier.vex = VEX256;
10694
0
        break;
10695
10696
0
      case '2':
10697
0
        if (evex)
10698
0
    i.tm.opcode_modifier.evex = EVEX512;
10699
0
        break;
10700
10701
0
      case '3':
10702
0
        if (evex)
10703
0
    i.tm.opcode_modifier.evex = EVEX_L3;
10704
0
        break;
10705
10706
0
      case 'I':
10707
0
        if (line[3] == 'G')
10708
0
    {
10709
0
      if (evex)
10710
0
        i.tm.opcode_modifier.evex = EVEXLIG;
10711
0
      else
10712
0
        i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
10713
0
      ++line;
10714
0
    }
10715
0
        break;
10716
0
      }
10717
10718
0
    if (i.tm.opcode_modifier.vex || i.tm.opcode_modifier.evex)
10719
0
      line += 3;
10720
0
    break;
10721
10722
0
  case '1':
10723
0
    if (line[2] == '2' && line[3] == '8')
10724
0
      {
10725
0
        if (evex)
10726
0
    i.tm.opcode_modifier.evex = EVEX128;
10727
0
        else
10728
0
    i.tm.opcode_modifier.vex = VEX128;
10729
0
        line += 4;
10730
0
      }
10731
0
    break;
10732
10733
0
  case '2':
10734
0
    if (line[2] == '5' && line[3] == '6')
10735
0
      {
10736
0
        if (evex)
10737
0
    i.tm.opcode_modifier.evex = EVEX256;
10738
0
        else
10739
0
    i.tm.opcode_modifier.vex = VEX256;
10740
0
        line += 4;
10741
0
      }
10742
0
    break;
10743
10744
0
  case '5':
10745
0
    if (evex && line[2] == '1' && line[3] == '2')
10746
0
      {
10747
0
        i.tm.opcode_modifier.evex = EVEX512;
10748
0
        line += 4;
10749
0
      }
10750
0
    break;
10751
0
  }
10752
0
    }
10753
10754
40
  if (line > end && *line == '.')
10755
0
    {
10756
      /* embedded prefix (VEX.pp, XOP.pp, EVEX.pp).  */
10757
0
      switch (line[1])
10758
0
  {
10759
0
  case 'N':
10760
0
    if (line[2] == 'P')
10761
0
      line += 3;
10762
0
    break;
10763
10764
0
  case '6':
10765
0
    if (line[2] == '6')
10766
0
      {
10767
0
        i.tm.opcode_modifier.opcodeprefix = PREFIX_0X66;
10768
0
        line += 3;
10769
0
      }
10770
0
    break;
10771
10772
0
  case 'F': case 'f':
10773
0
    if (line[2] == '3')
10774
0
      {
10775
0
        i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
10776
0
        line += 3;
10777
0
      }
10778
0
    else if (line[2] == '2')
10779
0
      {
10780
0
        i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF2;
10781
0
        line += 3;
10782
0
      }
10783
0
    break;
10784
0
  }
10785
0
    }
10786
10787
40
  if (line > end && !xop && *line == '.')
10788
0
    {
10789
      /* Encoding space (VEX.mmmmm, EVEX.mmmm).  */
10790
0
      switch (line[1])
10791
0
  {
10792
0
  case '0':
10793
0
    if (TOUPPER (line[2]) != 'F')
10794
0
      break;
10795
0
    if (line[3] == '.' || is_space_char (line[3]))
10796
0
      {
10797
0
        i.insn_opcode_space = SPACE_0F;
10798
0
        line += 3;
10799
0
      }
10800
0
    else if (line[3] == '3'
10801
0
       && (line[4] == '8' || TOUPPER (line[4]) == 'A')
10802
0
       && (line[5] == '.' || is_space_char (line[5])))
10803
0
      {
10804
0
        i.insn_opcode_space = line[4] == '8' ? SPACE_0F38 : SPACE_0F3A;
10805
0
        line += 5;
10806
0
      }
10807
0
    break;
10808
10809
0
  case 'M':
10810
0
    if (ISDIGIT (line[2]) && line[2] != '0')
10811
0
      {
10812
0
        char *e;
10813
0
        unsigned long n = strtoul (line + 2, &e, 10);
10814
10815
0
        if (n <= (evex ? 15 : 31)
10816
0
      && (*e == '.' || is_space_char (*e)))
10817
0
    {
10818
0
      i.insn_opcode_space = n;
10819
0
      line = e;
10820
0
    }
10821
0
      }
10822
0
    break;
10823
0
  }
10824
0
    }
10825
10826
40
  if (line > end && *line == '.' && line[1] == 'W')
10827
0
    {
10828
      /* VEX.W, XOP.W, EVEX.W  */
10829
0
      switch (line[2])
10830
0
  {
10831
0
  case '0':
10832
0
    i.tm.opcode_modifier.vexw = VEXW0;
10833
0
    break;
10834
10835
0
  case '1':
10836
0
    i.tm.opcode_modifier.vexw = VEXW1;
10837
0
    break;
10838
10839
0
  case 'I':
10840
0
    if (line[3] == 'G')
10841
0
      {
10842
0
        i.tm.opcode_modifier.vexw = VEXWIG;
10843
0
        ++line;
10844
0
      }
10845
0
    break;
10846
0
  }
10847
10848
0
      if (i.tm.opcode_modifier.vexw)
10849
0
  line += 3;
10850
0
    }
10851
10852
40
  if (line > end && *line && !is_space_char (*line))
10853
0
    {
10854
      /* Improve diagnostic a little.  */
10855
0
      if (*line == '.' && line[1] && !is_space_char (line[1]))
10856
0
  ++line;
10857
0
      goto done;
10858
0
    }
10859
10860
  /* Before processing the opcode expression, find trailing "+r" or
10861
     "/<digit>" specifiers.  */
10862
40
  for (ptr = line; ; ++ptr)
10863
70
    {
10864
70
      unsigned long n;
10865
70
      char *e;
10866
10867
70
      ptr = strpbrk (ptr, "+/,");
10868
70
      if (ptr == NULL || *ptr == ',')
10869
40
  break;
10870
10871
30
      if (*ptr == '+' && ptr[1] == 'r'
10872
30
    && (ptr[2] == ',' || (is_space_char (ptr[2]) && ptr[3] == ',')))
10873
0
  {
10874
0
    *ptr = ' ';
10875
0
    ptr[1] = ' ';
10876
0
    i.short_form = true;
10877
0
    break;
10878
0
  }
10879
10880
30
      if (*ptr == '/' && ISDIGIT (ptr[1])
10881
30
    && (n = strtoul (ptr + 1, &e, 8)) < 8
10882
30
    && e == ptr + 2
10883
30
    && (ptr[2] == ',' || (is_space_char (ptr[2]) && ptr[3] == ',')))
10884
0
  {
10885
0
    *ptr = ' ';
10886
0
    ptr[1] = ' ';
10887
0
    i.tm.extension_opcode = n;
10888
0
    i.tm.opcode_modifier.modrm = 1;
10889
0
    break;
10890
0
  }
10891
30
    }
10892
10893
40
  input_line_pointer = line;
10894
40
  val = get_absolute_expression ();
10895
40
  line = input_line_pointer;
10896
10897
40
  if (i.short_form && (val & 7))
10898
0
    as_warn ("`+r' assumes low three opcode bits to be clear");
10899
10900
40
  for (j = 1; j < sizeof(val); ++j)
10901
40
    if (!(val >> (j * 8)))
10902
40
      break;
10903
10904
  /* Trim off a prefix if present.  */
10905
40
  if (j > 1 && !vex && !xop && !evex)
10906
0
    {
10907
0
      uint8_t byte = val >> ((j - 1) * 8);
10908
10909
0
      switch (byte)
10910
0
  {
10911
0
  case DATA_PREFIX_OPCODE:
10912
0
  case REPE_PREFIX_OPCODE:
10913
0
  case REPNE_PREFIX_OPCODE:
10914
0
    if (!add_prefix (byte))
10915
0
      goto bad;
10916
0
    val &= ((uint64_t)1 << (--j * 8)) - 1;
10917
0
    break;
10918
0
  }
10919
0
    }
10920
10921
  /* Trim off encoding space.  */
10922
40
  if (j > 1 && !i.insn_opcode_space && (val >> ((j - 1) * 8)) == 0x0f)
10923
0
    {
10924
0
      uint8_t byte = val >> ((--j - 1) * 8);
10925
10926
0
      i.insn_opcode_space = SPACE_0F;
10927
0
      switch (byte & -(j > 1))
10928
0
  {
10929
0
  case 0x38:
10930
0
    i.insn_opcode_space = SPACE_0F38;
10931
0
    --j;
10932
0
    break;
10933
0
  case 0x3a:
10934
0
    i.insn_opcode_space = SPACE_0F3A;
10935
0
    --j;
10936
0
    break;
10937
0
  }
10938
0
      i.tm.opcode_space = i.insn_opcode_space;
10939
0
      val &= ((uint64_t)1 << (j * 8)) - 1;
10940
0
    }
10941
40
  if (!i.tm.opcode_space && (vex || evex))
10942
    /* Arrange for build_vex_prefix() to properly emit 0xC4/0xC5.
10943
       Also avoid hitting abort() there or in build_evex_prefix().  */
10944
0
    i.tm.opcode_space = i.insn_opcode_space == SPACE_0F ? SPACE_0F
10945
0
               : SPACE_0F38;
10946
10947
40
  if (j > 2)
10948
0
    {
10949
0
      as_bad (_("opcode residual (%#"PRIx64") too wide"), (uint64_t) val);
10950
0
      goto bad;
10951
0
    }
10952
40
  i.opcode_length = j;
10953
10954
  /* Handle operands, if any.  */
10955
40
  if (*line == ',')
10956
0
    {
10957
0
      i386_operand_type combined;
10958
0
      expressionS *disp_exp = NULL;
10959
0
      bool changed;
10960
10961
0
      i.memshift = -1;
10962
10963
0
      ptr = parse_operands (line + 1, &i386_mnemonics[MN__insn]);
10964
0
      this_operand = -1;
10965
0
      if (!ptr)
10966
0
  goto bad;
10967
0
      line = ptr;
10968
10969
0
      if (!i.operands)
10970
0
  {
10971
0
    as_bad (_("expecting operand after ','; got nothing"));
10972
0
    goto done;
10973
0
  }
10974
10975
0
      if (i.mem_operands > 1)
10976
0
  {
10977
0
    as_bad (_("too many memory references for `%s'"),
10978
0
      &i386_mnemonics[MN__insn]);
10979
0
    goto done;
10980
0
  }
10981
10982
      /* Are we to emit ModR/M encoding?  */
10983
0
      if (!i.short_form
10984
0
    && (i.mem_operands
10985
0
        || i.reg_operands > (i.vec_encoding != vex_encoding_default)
10986
0
        || i.tm.extension_opcode != None))
10987
0
  i.tm.opcode_modifier.modrm = 1;
10988
10989
0
      if (!i.tm.opcode_modifier.modrm
10990
0
    && (i.reg_operands
10991
0
        > i.short_form + 0U + (i.vec_encoding != vex_encoding_default)
10992
0
        || i.mem_operands))
10993
0
  {
10994
0
    as_bad (_("too many register/memory operands"));
10995
0
    goto done;
10996
0
  }
10997
10998
      /* Enforce certain constraints on operands.  */
10999
0
      switch (i.reg_operands + i.mem_operands
11000
0
        + (i.tm.extension_opcode != None))
11001
0
  {
11002
0
  case 0:
11003
0
    if (i.short_form)
11004
0
      {
11005
0
        as_bad (_("too few register/memory operands"));
11006
0
        goto done;
11007
0
      }
11008
    /* Fall through.  */
11009
0
  case 1:
11010
0
    if (i.tm.opcode_modifier.modrm)
11011
0
      {
11012
0
        as_bad (_("too few register/memory operands"));
11013
0
        goto done;
11014
0
      }
11015
0
    break;
11016
11017
0
  case 2:
11018
0
    break;
11019
11020
0
  case 4:
11021
0
    if (i.imm_operands
11022
0
        && (i.op[0].imms->X_op != O_constant
11023
0
      || !fits_in_imm4 (i.op[0].imms->X_add_number)))
11024
0
      {
11025
0
        as_bad (_("constant doesn't fit in %d bits"), evex ? 3 : 4);
11026
0
        goto done;
11027
0
      }
11028
    /* Fall through.  */
11029
0
  case 3:
11030
0
    if (i.vec_encoding != vex_encoding_default)
11031
0
      {
11032
0
        i.tm.opcode_modifier.vexvvvv = 1;
11033
0
        break;
11034
0
      }
11035
    /* Fall through.  */
11036
0
  default:
11037
0
    as_bad (_("too many register/memory operands"));
11038
0
    goto done;
11039
0
  }
11040
11041
      /* Bring operands into canonical order (imm, mem, reg).  */
11042
0
      do
11043
0
  {
11044
0
    changed = false;
11045
11046
0
    for (j = 1; j < i.operands; ++j)
11047
0
      {
11048
0
        if ((!operand_type_check (i.types[j - 1], imm)
11049
0
       && operand_type_check (i.types[j], imm))
11050
0
      || (i.types[j - 1].bitfield.class != ClassNone
11051
0
          && i.types[j].bitfield.class == ClassNone))
11052
0
    {
11053
0
      swap_2_operands (j - 1, j);
11054
0
      changed = true;
11055
0
    }
11056
0
      }
11057
0
  }
11058
0
      while (changed);
11059
11060
      /* For Intel syntax swap the order of register operands.  */
11061
0
      if (intel_syntax)
11062
0
  switch (i.reg_operands)
11063
0
    {
11064
0
    case 0:
11065
0
    case 1:
11066
0
      break;
11067
11068
0
    case 4:
11069
0
      swap_2_operands (i.imm_operands + i.mem_operands + 1, i.operands - 2);
11070
      /* Fall through.  */
11071
0
    case 3:
11072
0
    case 2:
11073
0
      swap_2_operands (i.imm_operands + i.mem_operands, i.operands - 1);
11074
0
      break;
11075
11076
0
    default:
11077
0
      abort ();
11078
0
    }
11079
11080
      /* Enforce constraints when using VSIB.  */
11081
0
      if (i.index_reg
11082
0
    && (i.index_reg->reg_type.bitfield.xmmword
11083
0
        || i.index_reg->reg_type.bitfield.ymmword
11084
0
        || i.index_reg->reg_type.bitfield.zmmword))
11085
0
  {
11086
0
    if (i.vec_encoding == vex_encoding_default)
11087
0
      {
11088
0
        as_bad (_("VSIB unavailable with legacy encoding"));
11089
0
        goto done;
11090
0
      }
11091
11092
0
    if (i.vec_encoding == vex_encoding_evex
11093
0
        && i.reg_operands > 1)
11094
0
      {
11095
        /* We could allow two register operands, encoding the 2nd one in
11096
     an 8-bit immediate like for 4-register-operand insns, but that
11097
     would require ugly fiddling with process_operands() and/or
11098
     build_modrm_byte().  */
11099
0
        as_bad (_("too many register operands with VSIB"));
11100
0
        goto done;
11101
0
      }
11102
11103
0
    i.tm.opcode_modifier.sib = 1;
11104
0
  }
11105
11106
      /* Establish operand size encoding.  */
11107
0
      operand_type_set (&combined, 0);
11108
11109
0
      for (j = i.imm_operands; j < i.operands; ++j)
11110
0
  {
11111
0
    i.types[j].bitfield.instance = InstanceNone;
11112
11113
0
    if (operand_type_check (i.types[j], disp))
11114
0
      {
11115
0
        i.types[j].bitfield.baseindex = 1;
11116
0
        disp_exp = i.op[j].disps;
11117
0
      }
11118
11119
0
    if (evex && i.types[j].bitfield.baseindex)
11120
0
      {
11121
0
        unsigned int n = i.memshift;
11122
11123
0
        if (i.types[j].bitfield.byte)
11124
0
    n = 0;
11125
0
        else if (i.types[j].bitfield.word)
11126
0
    n = 1;
11127
0
        else if (i.types[j].bitfield.dword)
11128
0
    n = 2;
11129
0
        else if (i.types[j].bitfield.qword)
11130
0
    n = 3;
11131
0
        else if (i.types[j].bitfield.xmmword)
11132
0
    n = 4;
11133
0
        else if (i.types[j].bitfield.ymmword)
11134
0
    n = 5;
11135
0
        else if (i.types[j].bitfield.zmmword)
11136
0
    n = 6;
11137
11138
0
        if (i.memshift < 32 && n != i.memshift)
11139
0
    as_warn ("conflicting memory operand size specifiers");
11140
0
        i.memshift = n;
11141
0
      }
11142
11143
0
    if ((i.broadcast.type || i.broadcast.bytes)
11144
0
        && j == i.broadcast.operand)
11145
0
      continue;
11146
11147
0
    combined = operand_type_or (combined, i.types[j]);
11148
0
    combined.bitfield.class = ClassNone;
11149
0
  }
11150
11151
0
      switch ((i.broadcast.type ? i.broadcast.type : 1)
11152
0
        << (i.memshift < 32 ? i.memshift : 0))
11153
0
  {
11154
0
  case 64: combined.bitfield.zmmword = 1; break;
11155
0
  case 32: combined.bitfield.ymmword = 1; break;
11156
0
  case 16: combined.bitfield.xmmword = 1; break;
11157
0
  case  8: combined.bitfield.qword = 1; break;
11158
0
  case  4: combined.bitfield.dword = 1; break;
11159
0
  }
11160
11161
0
      if (i.vec_encoding == vex_encoding_default)
11162
0
  {
11163
0
    if (flag_code == CODE_64BIT && combined.bitfield.qword)
11164
0
      i.rex |= REX_W;
11165
0
    else if ((flag_code == CODE_16BIT ? combined.bitfield.dword
11166
0
              : combined.bitfield.word)
11167
0
             && !add_prefix (DATA_PREFIX_OPCODE))
11168
0
      goto done;
11169
0
  }
11170
0
      else if (!i.tm.opcode_modifier.vexw)
11171
0
  {
11172
0
    if (flag_code == CODE_64BIT)
11173
0
      {
11174
0
        if (combined.bitfield.qword)
11175
0
          i.tm.opcode_modifier.vexw = VEXW1;
11176
0
        else if (combined.bitfield.dword)
11177
0
          i.tm.opcode_modifier.vexw = VEXW0;
11178
0
      }
11179
11180
0
    if (!i.tm.opcode_modifier.vexw)
11181
0
      i.tm.opcode_modifier.vexw = VEXWIG;
11182
0
  }
11183
11184
0
      if (vex || xop)
11185
0
  {
11186
0
    if (!i.tm.opcode_modifier.vex)
11187
0
      {
11188
0
        if (combined.bitfield.ymmword)
11189
0
          i.tm.opcode_modifier.vex = VEX256;
11190
0
        else if (combined.bitfield.xmmword)
11191
0
          i.tm.opcode_modifier.vex = VEX128;
11192
0
      }
11193
0
  }
11194
0
      else if (evex)
11195
0
  {
11196
0
    if (!i.tm.opcode_modifier.evex)
11197
0
      {
11198
        /* Do _not_ consider AVX512VL here.  */
11199
0
        if (i.rounding.type != rc_none || combined.bitfield.zmmword)
11200
0
          i.tm.opcode_modifier.evex = EVEX512;
11201
0
        else if (combined.bitfield.ymmword)
11202
0
          i.tm.opcode_modifier.evex = EVEX256;
11203
0
        else if (combined.bitfield.xmmword)
11204
0
          i.tm.opcode_modifier.evex = EVEX128;
11205
0
      }
11206
11207
0
    if (i.memshift >= 32)
11208
0
      {
11209
0
        unsigned int n = 0;
11210
11211
0
        switch (i.tm.opcode_modifier.evex)
11212
0
    {
11213
0
    case EVEX512: n = 64; break;
11214
0
    case EVEX256: n = 32; break;
11215
0
    case EVEX128: n = 16; break;
11216
0
    }
11217
11218
0
        if (i.broadcast.type)
11219
0
    n /= i.broadcast.type;
11220
11221
0
        if (n > 0)
11222
0
    for (i.memshift = 0; !(n & 1); n >>= 1)
11223
0
      ++i.memshift;
11224
0
        else if (disp_exp != NULL && disp_exp->X_op == O_constant
11225
0
           && disp_exp->X_add_number != 0
11226
0
           && i.disp_encoding != disp_encoding_32bit)
11227
0
    {
11228
0
      if (!quiet_warnings)
11229
0
        as_warn ("cannot determine memory operand size");
11230
0
      i.disp_encoding = disp_encoding_32bit;
11231
0
    }
11232
0
      }
11233
0
  }
11234
11235
0
      if (i.memshift >= 32)
11236
0
  i.memshift = 0;
11237
0
      else if (!evex)
11238
0
  i.vec_encoding = vex_encoding_error;
11239
11240
0
      if (i.disp_operands && !optimize_disp (&i.tm))
11241
0
  goto done;
11242
11243
      /* Establish size for immediate operands.  */
11244
0
      for (j = 0; j < i.imm_operands; ++j)
11245
0
  {
11246
0
    expressionS *expP = i.op[j].imms;
11247
11248
0
    gas_assert (operand_type_check (i.types[j], imm));
11249
0
    operand_type_set (&i.types[j], 0);
11250
11251
0
    if (i.imm_bits[j] > 32)
11252
0
      i.types[j].bitfield.imm64 = 1;
11253
0
    else if (i.imm_bits[j] > 16)
11254
0
      {
11255
0
        if (flag_code == CODE_64BIT && (i.flags[j] & Operand_Signed))
11256
0
    i.types[j].bitfield.imm32s = 1;
11257
0
        else
11258
0
    i.types[j].bitfield.imm32 = 1;
11259
0
      }
11260
0
    else if (i.imm_bits[j] > 8)
11261
0
      i.types[j].bitfield.imm16 = 1;
11262
0
    else if (i.imm_bits[j] > 0)
11263
0
      {
11264
0
        if (i.flags[j] & Operand_Signed)
11265
0
    i.types[j].bitfield.imm8s = 1;
11266
0
        else
11267
0
    i.types[j].bitfield.imm8 = 1;
11268
0
      }
11269
0
    else if (expP->X_op == O_constant)
11270
0
      {
11271
0
        i.types[j] = smallest_imm_type (expP->X_add_number);
11272
0
        i.types[j].bitfield.imm1 = 0;
11273
        /* Oddly enough imm_size() checks imm64 first, so the bit needs
11274
     zapping since smallest_imm_type() sets it unconditionally.  */
11275
0
        if (flag_code != CODE_64BIT)
11276
0
    {
11277
0
      i.types[j].bitfield.imm64 = 0;
11278
0
      i.types[j].bitfield.imm32s = 0;
11279
0
      i.types[j].bitfield.imm32 = 1;
11280
0
    }
11281
0
        else if (i.types[j].bitfield.imm32 || i.types[j].bitfield.imm32s)
11282
0
    i.types[j].bitfield.imm64 = 0;
11283
0
      }
11284
0
    else
11285
      /* Non-constant expressions are sized heuristically.  */
11286
0
      switch (flag_code)
11287
0
        {
11288
0
        case CODE_64BIT: i.types[j].bitfield.imm32s = 1; break;
11289
0
        case CODE_32BIT: i.types[j].bitfield.imm32 = 1; break;
11290
0
        case CODE_16BIT: i.types[j].bitfield.imm16 = 1; break;
11291
0
        }
11292
0
  }
11293
11294
0
      for (j = 0; j < i.operands; ++j)
11295
0
  i.tm.operand_types[j] = i.types[j];
11296
11297
0
      process_operands ();
11298
0
    }
11299
11300
  /* Don't set opcode until after processing operands, to avoid any
11301
     potential special casing there.  */
11302
40
  i.tm.base_opcode |= val;
11303
11304
40
  if (i.vec_encoding == vex_encoding_error
11305
40
      || (i.vec_encoding != vex_encoding_evex
11306
40
    ? i.broadcast.type || i.broadcast.bytes
11307
40
      || i.rounding.type != rc_none
11308
40
      || i.mask.reg
11309
40
    : (i.broadcast.type || i.broadcast.bytes)
11310
0
      && i.rounding.type != rc_none))
11311
0
    {
11312
0
      as_bad (_("conflicting .insn operands"));
11313
0
      goto done;
11314
0
    }
11315
11316
40
  if (vex || xop)
11317
0
    {
11318
0
      if (!i.tm.opcode_modifier.vex)
11319
0
  i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
11320
11321
0
      build_vex_prefix (NULL);
11322
0
      i.rex &= REX_OPCODE;
11323
0
    }
11324
40
  else if (evex)
11325
0
    {
11326
0
      if (!i.tm.opcode_modifier.evex)
11327
0
  i.tm.opcode_modifier.evex = EVEXLIG;
11328
11329
0
      build_evex_prefix ();
11330
0
      i.rex &= REX_OPCODE;
11331
0
    }
11332
40
  else if (i.rex != 0)
11333
0
    add_prefix (REX_OPCODE | i.rex);
11334
11335
40
  output_insn ();
11336
11337
40
 done:
11338
40
  *saved_ilp = saved_char;
11339
40
  input_line_pointer = line;
11340
11341
40
  demand_empty_rest_of_line ();
11342
11343
  /* Make sure dot_insn() won't yield "true" anymore.  */
11344
40
  i.tm.mnem_off = 0;
11345
40
}
11346
11347
#ifdef TE_PE
11348
static void
11349
pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
11350
{
11351
  expressionS exp;
11352
11353
  do
11354
    {
11355
      expression (&exp);
11356
      if (exp.X_op == O_symbol)
11357
  exp.X_op = O_secrel;
11358
11359
      emit_expr (&exp, 4);
11360
    }
11361
  while (*input_line_pointer++ == ',');
11362
11363
  input_line_pointer--;
11364
  demand_empty_rest_of_line ();
11365
}
11366
11367
static void
11368
pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
11369
{
11370
  expressionS exp;
11371
11372
  do
11373
    {
11374
      expression (&exp);
11375
      if (exp.X_op == O_symbol)
11376
  exp.X_op = O_secidx;
11377
11378
      emit_expr (&exp, 2);
11379
    }
11380
  while (*input_line_pointer++ == ',');
11381
11382
  input_line_pointer--;
11383
  demand_empty_rest_of_line ();
11384
}
11385
#endif
11386
11387
/* Handle Rounding Control / SAE specifiers.  */
11388
11389
static char *
11390
RC_SAE_specifier (const char *pstr)
11391
1
{
11392
1
  unsigned int j;
11393
11394
5
  for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
11395
5
    {
11396
5
      if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
11397
1
  {
11398
1
    if (i.rounding.type != rc_none)
11399
0
      {
11400
0
        as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
11401
0
        return NULL;
11402
0
      }
11403
11404
1
    i.rounding.type = RC_NamesTable[j].type;
11405
11406
1
    return (char *)(pstr + RC_NamesTable[j].len);
11407
1
  }
11408
5
    }
11409
11410
0
  return NULL;
11411
1
}
11412
11413
/* Handle Vector operations.  */
11414
11415
static char *
11416
check_VecOperations (char *op_string)
11417
0
{
11418
0
  const reg_entry *mask;
11419
0
  const char *saved;
11420
0
  char *end_op;
11421
11422
0
  while (*op_string)
11423
0
    {
11424
0
      saved = op_string;
11425
0
      if (*op_string == '{')
11426
0
  {
11427
0
    op_string++;
11428
11429
    /* Check broadcasts.  */
11430
0
    if (startswith (op_string, "1to"))
11431
0
      {
11432
0
        unsigned int bcst_type;
11433
11434
0
        if (i.broadcast.type)
11435
0
    goto duplicated_vec_op;
11436
11437
0
        op_string += 3;
11438
0
        if (*op_string == '8')
11439
0
    bcst_type = 8;
11440
0
        else if (*op_string == '4')
11441
0
    bcst_type = 4;
11442
0
        else if (*op_string == '2')
11443
0
    bcst_type = 2;
11444
0
        else if (*op_string == '1'
11445
0
           && *(op_string+1) == '6')
11446
0
    {
11447
0
      bcst_type = 16;
11448
0
      op_string++;
11449
0
    }
11450
0
        else if (*op_string == '3'
11451
0
           && *(op_string+1) == '2')
11452
0
    {
11453
0
      bcst_type = 32;
11454
0
      op_string++;
11455
0
    }
11456
0
        else
11457
0
    {
11458
0
      as_bad (_("Unsupported broadcast: `%s'"), saved);
11459
0
      return NULL;
11460
0
    }
11461
0
        op_string++;
11462
11463
0
        i.broadcast.type = bcst_type;
11464
0
        i.broadcast.operand = this_operand;
11465
11466
        /* For .insn a data size specifier may be appended.  */
11467
0
        if (dot_insn () && *op_string == ':')
11468
0
    goto dot_insn_modifier;
11469
0
      }
11470
    /* Check .insn special cases.  */
11471
0
    else if (dot_insn () && *op_string == ':')
11472
0
      {
11473
0
      dot_insn_modifier:
11474
0
        switch (op_string[1])
11475
0
    {
11476
0
      unsigned long n;
11477
11478
0
    case 'd':
11479
0
      if (i.memshift < 32)
11480
0
        goto duplicated_vec_op;
11481
11482
0
      n = strtoul (op_string + 2, &end_op, 0);
11483
0
      if (n)
11484
0
        for (i.memshift = 0; !(n & 1); n >>= 1)
11485
0
          ++i.memshift;
11486
0
      if (i.memshift < 32 && n == 1)
11487
0
        op_string = end_op;
11488
0
      break;
11489
11490
0
    case 's': case 'u':
11491
      /* This isn't really a "vector" operation, but a sign/size
11492
         specifier for immediate operands of .insn.  Note that AT&T
11493
         syntax handles the same in i386_immediate().  */
11494
0
      if (!intel_syntax)
11495
0
        break;
11496
11497
0
      if (i.imm_bits[this_operand])
11498
0
        goto duplicated_vec_op;
11499
11500
0
      n = strtoul (op_string + 2, &end_op, 0);
11501
0
      if (n && n <= (flag_code == CODE_64BIT ? 64 : 32))
11502
0
        {
11503
0
          i.imm_bits[this_operand] = n;
11504
0
          if (op_string[1] == 's')
11505
0
      i.flags[this_operand] |= Operand_Signed;
11506
0
          op_string = end_op;
11507
0
        }
11508
0
      break;
11509
0
    }
11510
0
      }
11511
    /* Check masking operation.  */
11512
0
    else if ((mask = parse_register (op_string, &end_op)) != NULL)
11513
0
      {
11514
0
        if (mask == &bad_reg)
11515
0
    return NULL;
11516
11517
        /* k0 can't be used for write mask.  */
11518
0
        if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
11519
0
    {
11520
0
      as_bad (_("`%s%s' can't be used for write mask"),
11521
0
        register_prefix, mask->reg_name);
11522
0
      return NULL;
11523
0
    }
11524
11525
0
        if (!i.mask.reg)
11526
0
    {
11527
0
      i.mask.reg = mask;
11528
0
      i.mask.operand = this_operand;
11529
0
    }
11530
0
        else if (i.mask.reg->reg_num)
11531
0
    goto duplicated_vec_op;
11532
0
        else
11533
0
    {
11534
0
      i.mask.reg = mask;
11535
11536
      /* Only "{z}" is allowed here.  No need to check
11537
         zeroing mask explicitly.  */
11538
0
      if (i.mask.operand != (unsigned int) this_operand)
11539
0
        {
11540
0
          as_bad (_("invalid write mask `%s'"), saved);
11541
0
          return NULL;
11542
0
        }
11543
0
    }
11544
11545
0
        op_string = end_op;
11546
0
      }
11547
    /* Check zeroing-flag for masking operation.  */
11548
0
    else if (*op_string == 'z')
11549
0
      {
11550
0
        if (!i.mask.reg)
11551
0
    {
11552
0
      i.mask.reg = reg_k0;
11553
0
      i.mask.zeroing = 1;
11554
0
      i.mask.operand = this_operand;
11555
0
    }
11556
0
        else
11557
0
    {
11558
0
      if (i.mask.zeroing)
11559
0
        {
11560
0
        duplicated_vec_op:
11561
0
          as_bad (_("duplicated `%s'"), saved);
11562
0
          return NULL;
11563
0
        }
11564
11565
0
      i.mask.zeroing = 1;
11566
11567
      /* Only "{%k}" is allowed here.  No need to check mask
11568
         register explicitly.  */
11569
0
      if (i.mask.operand != (unsigned int) this_operand)
11570
0
        {
11571
0
          as_bad (_("invalid zeroing-masking `%s'"),
11572
0
            saved);
11573
0
          return NULL;
11574
0
        }
11575
0
    }
11576
11577
0
        op_string++;
11578
0
      }
11579
0
    else if (intel_syntax
11580
0
       && (op_string = RC_SAE_specifier (op_string)) != NULL)
11581
0
      i.rounding.modifier = true;
11582
0
    else
11583
0
      goto unknown_vec_op;
11584
11585
0
    if (*op_string != '}')
11586
0
      {
11587
0
        as_bad (_("missing `}' in `%s'"), saved);
11588
0
        return NULL;
11589
0
      }
11590
0
    op_string++;
11591
11592
    /* Strip whitespace since the addition of pseudo prefixes
11593
       changed how the scrubber treats '{'.  */
11594
0
    if (is_space_char (*op_string))
11595
0
      ++op_string;
11596
11597
0
    continue;
11598
0
  }
11599
0
    unknown_vec_op:
11600
      /* We don't know this one.  */
11601
0
      as_bad (_("unknown vector operation: `%s'"), saved);
11602
0
      return NULL;
11603
0
    }
11604
11605
0
  if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
11606
0
    {
11607
0
      as_bad (_("zeroing-masking only allowed with write mask"));
11608
0
      return NULL;
11609
0
    }
11610
11611
0
  return op_string;
11612
0
}
11613
11614
static int
11615
i386_immediate (char *imm_start)
11616
104k
{
11617
104k
  char *save_input_line_pointer;
11618
104k
  char *gotfree_input_line;
11619
104k
  segT exp_seg = 0;
11620
104k
  expressionS *exp;
11621
104k
  i386_operand_type types;
11622
11623
104k
  operand_type_set (&types, ~0);
11624
11625
104k
  if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
11626
0
    {
11627
0
      as_bad (_("at most %d immediate operands are allowed"),
11628
0
        MAX_IMMEDIATE_OPERANDS);
11629
0
      return 0;
11630
0
    }
11631
11632
104k
  exp = &im_expressions[i.imm_operands++];
11633
104k
  i.op[this_operand].imms = exp;
11634
11635
104k
  if (is_space_char (*imm_start))
11636
414
    ++imm_start;
11637
11638
104k
  save_input_line_pointer = input_line_pointer;
11639
104k
  input_line_pointer = imm_start;
11640
11641
104k
  gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
11642
104k
  if (gotfree_input_line)
11643
196
    input_line_pointer = gotfree_input_line;
11644
11645
104k
  expr_mode = expr_operator_none;
11646
104k
  exp_seg = expression (exp);
11647
11648
  /* For .insn immediates there may be a size specifier.  */
11649
104k
  if (dot_insn () && *input_line_pointer == '{' && input_line_pointer[1] == ':'
11650
104k
      && (input_line_pointer[2] == 's' || input_line_pointer[2] == 'u'))
11651
0
    {
11652
0
      char *e;
11653
0
      unsigned long n = strtoul (input_line_pointer + 3, &e, 0);
11654
11655
0
      if (*e == '}' && n && n <= (flag_code == CODE_64BIT ? 64 : 32))
11656
0
  {
11657
0
    i.imm_bits[this_operand] = n;
11658
0
    if (input_line_pointer[2] == 's')
11659
0
      i.flags[this_operand] |= Operand_Signed;
11660
0
    input_line_pointer = e + 1;
11661
0
  }
11662
0
    }
11663
11664
104k
  SKIP_WHITESPACE ();
11665
104k
  if (*input_line_pointer)
11666
4.46k
    as_bad (_("junk `%s' after expression"), input_line_pointer);
11667
11668
104k
  input_line_pointer = save_input_line_pointer;
11669
104k
  if (gotfree_input_line)
11670
196
    {
11671
196
      free (gotfree_input_line);
11672
11673
196
      if (exp->X_op == O_constant)
11674
60
  exp->X_op = O_illegal;
11675
196
    }
11676
11677
104k
  if (exp_seg == reg_section)
11678
39
    {
11679
39
      as_bad (_("illegal immediate register operand %s"), imm_start);
11680
39
      return 0;
11681
39
    }
11682
11683
104k
  return i386_finalize_immediate (exp_seg, exp, types, imm_start);
11684
104k
}
11685
11686
static int
11687
i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11688
       i386_operand_type types, const char *imm_start)
11689
104k
{
11690
104k
  if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
11691
14.4k
    {
11692
14.4k
      if (imm_start)
11693
14.4k
  as_bad (_("missing or invalid immediate expression `%s'"),
11694
14.4k
    imm_start);
11695
14.4k
      return 0;
11696
14.4k
    }
11697
89.8k
  else if (exp->X_op == O_constant)
11698
75.4k
    {
11699
      /* Size it properly later.  */
11700
75.4k
      i.types[this_operand].bitfield.imm64 = 1;
11701
11702
      /* If not 64bit, sign/zero extend val, to account for wraparound
11703
   when !BFD64.  */
11704
75.4k
      if (expr_mode == expr_operator_present
11705
75.4k
    && flag_code != CODE_64BIT && !object_64bit)
11706
0
  exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
11707
75.4k
    }
11708
#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
11709
  else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
11710
     && exp_seg != absolute_section
11711
     && exp_seg != text_section
11712
     && exp_seg != data_section
11713
     && exp_seg != bss_section
11714
     && exp_seg != undefined_section
11715
     && !bfd_is_com_section (exp_seg))
11716
    {
11717
      as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
11718
      return 0;
11719
    }
11720
#endif
11721
14.3k
  else
11722
14.3k
    {
11723
      /* This is an address.  The size of the address will be
11724
   determined later, depending on destination register,
11725
   suffix, or the default for the section.  */
11726
14.3k
      i.types[this_operand].bitfield.imm8 = 1;
11727
14.3k
      i.types[this_operand].bitfield.imm16 = 1;
11728
14.3k
      i.types[this_operand].bitfield.imm32 = 1;
11729
14.3k
      i.types[this_operand].bitfield.imm32s = 1;
11730
14.3k
      i.types[this_operand].bitfield.imm64 = 1;
11731
14.3k
      i.types[this_operand] = operand_type_and (i.types[this_operand],
11732
14.3k
            types);
11733
14.3k
    }
11734
11735
89.8k
  return 1;
11736
104k
}
11737
11738
static char *
11739
i386_scale (char *scale)
11740
21
{
11741
21
  offsetT val;
11742
21
  char *save = input_line_pointer;
11743
11744
21
  input_line_pointer = scale;
11745
21
  val = get_absolute_expression ();
11746
11747
21
  switch (val)
11748
21
    {
11749
0
    case 1:
11750
0
      i.log2_scale_factor = 0;
11751
0
      break;
11752
8
    case 2:
11753
8
      i.log2_scale_factor = 1;
11754
8
      break;
11755
0
    case 4:
11756
0
      i.log2_scale_factor = 2;
11757
0
      break;
11758
0
    case 8:
11759
0
      i.log2_scale_factor = 3;
11760
0
      break;
11761
13
    default:
11762
13
      {
11763
13
  char sep = *input_line_pointer;
11764
11765
13
  *input_line_pointer = '\0';
11766
13
  as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
11767
13
    scale);
11768
13
  *input_line_pointer = sep;
11769
13
  input_line_pointer = save;
11770
13
  return NULL;
11771
0
      }
11772
21
    }
11773
8
  if (i.log2_scale_factor != 0 && i.index_reg == 0)
11774
8
    {
11775
8
      as_warn (_("scale factor of %d without an index register"),
11776
8
         1 << i.log2_scale_factor);
11777
8
      i.log2_scale_factor = 0;
11778
8
    }
11779
8
  scale = input_line_pointer;
11780
8
  input_line_pointer = save;
11781
8
  return scale;
11782
21
}
11783
11784
static int
11785
i386_displacement (char *disp_start, char *disp_end)
11786
116k
{
11787
116k
  expressionS *exp;
11788
116k
  segT exp_seg = 0;
11789
116k
  char *save_input_line_pointer;
11790
116k
  char *gotfree_input_line;
11791
116k
  int override;
11792
116k
  i386_operand_type bigdisp, types = anydisp;
11793
116k
  int ret;
11794
11795
116k
  if (i.disp_operands == MAX_MEMORY_OPERANDS)
11796
0
    {
11797
0
      as_bad (_("at most %d displacement operands are allowed"),
11798
0
        MAX_MEMORY_OPERANDS);
11799
0
      return 0;
11800
0
    }
11801
11802
116k
  operand_type_set (&bigdisp, 0);
11803
116k
  if (i.jumpabsolute
11804
116k
      || i.types[this_operand].bitfield.baseindex
11805
116k
      || (current_templates->start->opcode_modifier.jump != JUMP
11806
115k
    && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
11807
109k
    {
11808
109k
      i386_addressing_mode ();
11809
109k
      override = (i.prefix[ADDR_PREFIX] != 0);
11810
109k
      if (flag_code == CODE_64BIT)
11811
45.1k
  {
11812
45.1k
    bigdisp.bitfield.disp32 = 1;
11813
45.1k
    if (!override)
11814
45.1k
      bigdisp.bitfield.disp64 = 1;
11815
45.1k
  }
11816
64.4k
      else if ((flag_code == CODE_16BIT) ^ override)
11817
60.4k
    bigdisp.bitfield.disp16 = 1;
11818
4.04k
      else
11819
4.04k
    bigdisp.bitfield.disp32 = 1;
11820
109k
    }
11821
6.43k
  else
11822
6.43k
    {
11823
      /* For PC-relative branches, the width of the displacement may be
11824
   dependent upon data size, but is never dependent upon address size.
11825
   Also make sure to not unintentionally match against a non-PC-relative
11826
   branch template.  */
11827
6.43k
      static templates aux_templates;
11828
6.43k
      const insn_template *t = current_templates->start;
11829
6.43k
      bool has_intel64 = false;
11830
11831
6.43k
      aux_templates.start = t;
11832
6.50k
      while (++t < current_templates->end)
11833
117
  {
11834
117
    if (t->opcode_modifier.jump
11835
117
        != current_templates->start->opcode_modifier.jump)
11836
43
      break;
11837
74
    if ((t->opcode_modifier.isa64 >= INTEL64))
11838
46
      has_intel64 = true;
11839
74
  }
11840
6.43k
      if (t < current_templates->end)
11841
43
  {
11842
43
    aux_templates.end = t;
11843
43
    current_templates = &aux_templates;
11844
43
  }
11845
11846
6.43k
      override = (i.prefix[DATA_PREFIX] != 0);
11847
6.43k
      if (flag_code == CODE_64BIT)
11848
2.19k
  {
11849
2.19k
    if ((override || i.suffix == WORD_MNEM_SUFFIX)
11850
2.19k
        && (!intel64 || !has_intel64))
11851
55
      bigdisp.bitfield.disp16 = 1;
11852
2.14k
    else
11853
2.14k
      bigdisp.bitfield.disp32 = 1;
11854
2.19k
  }
11855
4.23k
      else
11856
4.23k
  {
11857
4.23k
    if (!override)
11858
4.23k
      override = (i.suffix == (flag_code != CODE_16BIT
11859
4.23k
             ? WORD_MNEM_SUFFIX
11860
4.23k
             : LONG_MNEM_SUFFIX));
11861
4.23k
    bigdisp.bitfield.disp32 = 1;
11862
4.23k
    if ((flag_code == CODE_16BIT) ^ override)
11863
3.35k
      {
11864
3.35k
        bigdisp.bitfield.disp32 = 0;
11865
3.35k
        bigdisp.bitfield.disp16 = 1;
11866
3.35k
      }
11867
4.23k
  }
11868
6.43k
    }
11869
116k
  i.types[this_operand] = operand_type_or (i.types[this_operand],
11870
116k
             bigdisp);
11871
11872
116k
  exp = &disp_expressions[i.disp_operands];
11873
116k
  i.op[this_operand].disps = exp;
11874
116k
  i.disp_operands++;
11875
116k
  save_input_line_pointer = input_line_pointer;
11876
116k
  input_line_pointer = disp_start;
11877
116k
  END_STRING_AND_SAVE (disp_end);
11878
11879
116k
#ifndef GCC_ASM_O_HACK
11880
116k
#define GCC_ASM_O_HACK 0
11881
116k
#endif
11882
#if GCC_ASM_O_HACK
11883
  END_STRING_AND_SAVE (disp_end + 1);
11884
  if (i.types[this_operand].bitfield.baseIndex
11885
      && displacement_string_end[-1] == '+')
11886
    {
11887
      /* This hack is to avoid a warning when using the "o"
11888
   constraint within gcc asm statements.
11889
   For instance:
11890
11891
   #define _set_tssldt_desc(n,addr,limit,type) \
11892
   __asm__ __volatile__ ( \
11893
   "movw %w2,%0\n\t" \
11894
   "movw %w1,2+%0\n\t" \
11895
   "rorl $16,%1\n\t" \
11896
   "movb %b1,4+%0\n\t" \
11897
   "movb %4,5+%0\n\t" \
11898
   "movb $0,6+%0\n\t" \
11899
   "movb %h1,7+%0\n\t" \
11900
   "rorl $16,%1" \
11901
   : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
11902
11903
   This works great except that the output assembler ends
11904
   up looking a bit weird if it turns out that there is
11905
   no offset.  You end up producing code that looks like:
11906
11907
   #APP
11908
   movw $235,(%eax)
11909
   movw %dx,2+(%eax)
11910
   rorl $16,%edx
11911
   movb %dl,4+(%eax)
11912
   movb $137,5+(%eax)
11913
   movb $0,6+(%eax)
11914
   movb %dh,7+(%eax)
11915
   rorl $16,%edx
11916
   #NO_APP
11917
11918
   So here we provide the missing zero.  */
11919
11920
      *displacement_string_end = '0';
11921
    }
11922
#endif
11923
116k
  gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
11924
116k
  if (gotfree_input_line)
11925
801
    input_line_pointer = gotfree_input_line;
11926
11927
116k
  expr_mode = expr_operator_none;
11928
116k
  exp_seg = expression (exp);
11929
11930
116k
  SKIP_WHITESPACE ();
11931
116k
  if (*input_line_pointer)
11932
15.3k
    as_bad (_("junk `%s' after expression"), input_line_pointer);
11933
#if GCC_ASM_O_HACK
11934
  RESTORE_END_STRING (disp_end + 1);
11935
#endif
11936
116k
  input_line_pointer = save_input_line_pointer;
11937
116k
  if (gotfree_input_line)
11938
801
    {
11939
801
      free (gotfree_input_line);
11940
11941
801
      if (exp->X_op == O_constant || exp->X_op == O_register)
11942
43
  exp->X_op = O_illegal;
11943
801
    }
11944
11945
116k
  ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
11946
11947
116k
  RESTORE_END_STRING (disp_end);
11948
11949
116k
  return ret;
11950
116k
}
11951
11952
static int
11953
i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11954
          i386_operand_type types, const char *disp_start)
11955
116k
{
11956
116k
  int ret = 1;
11957
11958
  /* We do this to make sure that the section symbol is in
11959
     the symbol table.  We will ultimately change the relocation
11960
     to be relative to the beginning of the section.  */
11961
116k
  if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
11962
116k
      || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
11963
116k
      || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11964
373
    {
11965
373
      if (exp->X_op != O_symbol)
11966
17
  goto inv_disp;
11967
11968
356
      if (S_IS_LOCAL (exp->X_add_symbol)
11969
356
    && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
11970
356
    && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
11971
0
  section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
11972
356
      exp->X_op = O_subtract;
11973
356
      exp->X_op_symbol = GOT_symbol;
11974
356
      if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
11975
0
  i.reloc[this_operand] = BFD_RELOC_32_PCREL;
11976
356
      else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
11977
356
  i.reloc[this_operand] = BFD_RELOC_64;
11978
0
      else
11979
0
  i.reloc[this_operand] = BFD_RELOC_32;
11980
356
    }
11981
11982
115k
  else if (exp->X_op == O_absent
11983
115k
     || exp->X_op == O_illegal
11984
115k
     || exp->X_op == O_big)
11985
363
    {
11986
380
    inv_disp:
11987
380
      as_bad (_("missing or invalid displacement expression `%s'"),
11988
380
        disp_start);
11989
380
      ret = 0;
11990
380
    }
11991
11992
115k
  else if (exp->X_op == O_constant)
11993
82.7k
    {
11994
      /* Sizing gets taken care of by optimize_disp().
11995
11996
   If not 64bit, sign/zero extend val, to account for wraparound
11997
   when !BFD64.  */
11998
82.7k
      if (expr_mode == expr_operator_present
11999
82.7k
    && flag_code != CODE_64BIT && !object_64bit)
12000
0
  exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
12001
82.7k
    }
12002
12003
#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
12004
  else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
12005
     && exp_seg != absolute_section
12006
     && exp_seg != text_section
12007
     && exp_seg != data_section
12008
     && exp_seg != bss_section
12009
     && exp_seg != undefined_section
12010
     && !bfd_is_com_section (exp_seg))
12011
    {
12012
      as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
12013
      ret = 0;
12014
    }
12015
#endif
12016
12017
32.5k
  else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
12018
323
    i.types[this_operand].bitfield.disp8 = 1;
12019
12020
  /* Check if this is a displacement only operand.  */
12021
116k
  if (!i.types[this_operand].bitfield.baseindex)
12022
116k
    i.types[this_operand] =
12023
116k
      operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
12024
116k
           operand_type_and (i.types[this_operand], types));
12025
12026
116k
  return ret;
12027
116k
}
12028
12029
/* Return the active addressing mode, taking address override and
12030
   registers forming the address into consideration.  Update the
12031
   address override prefix if necessary.  */
12032
12033
static enum flag_code
12034
i386_addressing_mode (void)
12035
225k
{
12036
225k
  enum flag_code addr_mode;
12037
12038
225k
  if (i.prefix[ADDR_PREFIX])
12039
0
    addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
12040
225k
  else if (flag_code == CODE_16BIT
12041
225k
     && current_templates->start->cpu_flags.bitfield.cpumpx
12042
     /* Avoid replacing the "16-bit addressing not allowed" diagnostic
12043
        from md_assemble() by "is not a valid base/index expression"
12044
        when there is a base and/or index.  */
12045
225k
     && !i.types[this_operand].bitfield.baseindex)
12046
0
    {
12047
      /* MPX insn memory operands with neither base nor index must be forced
12048
   to use 32-bit addressing in 16-bit mode.  */
12049
0
      addr_mode = CODE_32BIT;
12050
0
      i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
12051
0
      ++i.prefixes;
12052
0
      gas_assert (!i.types[this_operand].bitfield.disp16);
12053
0
      gas_assert (!i.types[this_operand].bitfield.disp32);
12054
0
    }
12055
225k
  else
12056
225k
    {
12057
225k
      addr_mode = flag_code;
12058
12059
225k
#if INFER_ADDR_PREFIX
12060
225k
      if (i.mem_operands == 0)
12061
213k
  {
12062
    /* Infer address prefix from the first memory operand.  */
12063
213k
    const reg_entry *addr_reg = i.base_reg;
12064
12065
213k
    if (addr_reg == NULL)
12066
213k
      addr_reg = i.index_reg;
12067
12068
213k
    if (addr_reg)
12069
0
      {
12070
0
        if (addr_reg->reg_type.bitfield.dword)
12071
0
    addr_mode = CODE_32BIT;
12072
0
        else if (flag_code != CODE_64BIT
12073
0
           && addr_reg->reg_type.bitfield.word)
12074
0
    addr_mode = CODE_16BIT;
12075
12076
0
        if (addr_mode != flag_code)
12077
0
    {
12078
0
      i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
12079
0
      i.prefixes += 1;
12080
      /* Change the size of any displacement too.  At most one
12081
         of Disp16 or Disp32 is set.
12082
         FIXME.  There doesn't seem to be any real need for
12083
         separate Disp16 and Disp32 flags.  The same goes for
12084
         Imm16 and Imm32.  Removing them would probably clean
12085
         up the code quite a lot.  */
12086
0
      if (flag_code != CODE_64BIT
12087
0
          && (i.types[this_operand].bitfield.disp16
12088
0
        || i.types[this_operand].bitfield.disp32))
12089
0
        {
12090
0
          static const i386_operand_type disp16_32 = {
12091
0
      .bitfield = { .disp16 = 1, .disp32 = 1 }
12092
0
          };
12093
12094
0
          i.types[this_operand]
12095
0
      = operand_type_xor (i.types[this_operand], disp16_32);
12096
0
        }
12097
0
    }
12098
0
      }
12099
213k
  }
12100
225k
#endif
12101
225k
    }
12102
12103
0
  return addr_mode;
12104
225k
}
12105
12106
/* Make sure the memory operand we've been dealt is valid.
12107
   Return 1 on success, 0 on a failure.  */
12108
12109
static int
12110
i386_index_check (const char *operand_string)
12111
115k
{
12112
115k
  const char *kind = "base/index";
12113
115k
  enum flag_code addr_mode = i386_addressing_mode ();
12114
115k
  const insn_template *t = current_templates->end - 1;
12115
12116
115k
  if (t->opcode_modifier.isstring)
12117
3.33k
    {
12118
      /* Memory operands of string insns are special in that they only allow
12119
   a single register (rDI, rSI, or rBX) as their memory address.  */
12120
3.33k
      const reg_entry *expected_reg;
12121
3.33k
      static const char *di_si[][2] =
12122
3.33k
  {
12123
3.33k
    { "esi", "edi" },
12124
3.33k
    { "si", "di" },
12125
3.33k
    { "rsi", "rdi" }
12126
3.33k
  };
12127
3.33k
      static const char *bx[] = { "ebx", "bx", "rbx" };
12128
12129
3.33k
      kind = "string address";
12130
12131
3.33k
      if (t->opcode_modifier.prefixok == PrefixRep)
12132
3.33k
  {
12133
3.33k
    int es_op = t->opcode_modifier.isstring - IS_STRING_ES_OP0;
12134
3.33k
    int op = 0;
12135
12136
3.33k
    if (!t->operand_types[0].bitfield.baseindex
12137
3.33k
        || ((!i.mem_operands != !intel_syntax)
12138
3.25k
      && t->operand_types[1].bitfield.baseindex))
12139
922
      op = 1;
12140
3.33k
    expected_reg
12141
3.33k
      = (const reg_entry *) str_hash_find (reg_hash,
12142
3.33k
             di_si[addr_mode][op == es_op]);
12143
3.33k
  }
12144
0
      else
12145
0
  expected_reg
12146
0
    = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
12147
12148
3.33k
      if (i.base_reg != expected_reg
12149
3.33k
    || i.index_reg
12150
3.33k
    || operand_type_check (i.types[this_operand], disp))
12151
3.33k
  {
12152
    /* The second memory operand must have the same size as
12153
       the first one.  */
12154
3.33k
    if (i.mem_operands
12155
3.33k
        && i.base_reg
12156
3.33k
        && !((addr_mode == CODE_64BIT
12157
0
        && i.base_reg->reg_type.bitfield.qword)
12158
0
       || (addr_mode == CODE_32BIT
12159
0
           ? i.base_reg->reg_type.bitfield.dword
12160
0
           : i.base_reg->reg_type.bitfield.word)))
12161
0
      goto bad_address;
12162
12163
3.33k
    as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
12164
3.33k
       operand_string,
12165
3.33k
       intel_syntax ? '[' : '(',
12166
3.33k
       register_prefix,
12167
3.33k
       expected_reg->reg_name,
12168
3.33k
       intel_syntax ? ']' : ')');
12169
3.33k
    return 1;
12170
3.33k
  }
12171
0
      else
12172
0
  return 1;
12173
12174
0
    bad_address:
12175
0
      as_bad (_("`%s' is not a valid %s expression"),
12176
0
        operand_string, kind);
12177
0
      return 0;
12178
3.33k
    }
12179
112k
  else
12180
112k
    {
12181
112k
      t = current_templates->start;
12182
12183
112k
      if (addr_mode != CODE_16BIT)
12184
51.4k
  {
12185
    /* 32-bit/64-bit checks.  */
12186
51.4k
    if (i.disp_encoding == disp_encoding_16bit)
12187
0
      {
12188
16
      bad_disp:
12189
16
        as_bad (_("invalid `%s' prefix"),
12190
16
          addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
12191
16
        return 0;
12192
0
      }
12193
12194
51.4k
    if ((i.base_reg
12195
51.4k
         && ((addr_mode == CODE_64BIT
12196
0
        ? !i.base_reg->reg_type.bitfield.qword
12197
0
        : !i.base_reg->reg_type.bitfield.dword)
12198
0
       || (i.index_reg && i.base_reg->reg_num == RegIP)
12199
0
       || i.base_reg->reg_num == RegIZ))
12200
51.4k
        || (i.index_reg
12201
51.4k
      && !i.index_reg->reg_type.bitfield.xmmword
12202
51.4k
      && !i.index_reg->reg_type.bitfield.ymmword
12203
51.4k
      && !i.index_reg->reg_type.bitfield.zmmword
12204
51.4k
      && ((addr_mode == CODE_64BIT
12205
0
           ? !i.index_reg->reg_type.bitfield.qword
12206
0
           : !i.index_reg->reg_type.bitfield.dword)
12207
0
          || !i.index_reg->reg_type.bitfield.baseindex)))
12208
0
      goto bad_address;
12209
12210
    /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
12211
51.4k
    if (t->mnem_off == MN_bndmk
12212
51.4k
        || t->mnem_off == MN_bndldx
12213
51.4k
        || t->mnem_off == MN_bndstx
12214
51.4k
        || t->opcode_modifier.sib == SIBMEM)
12215
0
      {
12216
        /* They cannot use RIP-relative addressing. */
12217
0
        if (i.base_reg && i.base_reg->reg_num == RegIP)
12218
0
    {
12219
0
      as_bad (_("`%s' cannot be used here"), operand_string);
12220
0
      return 0;
12221
0
    }
12222
12223
        /* bndldx and bndstx ignore their scale factor. */
12224
0
        if ((t->mnem_off == MN_bndldx || t->mnem_off == MN_bndstx)
12225
0
      && i.log2_scale_factor)
12226
0
    as_warn (_("register scaling is being ignored here"));
12227
0
      }
12228
51.4k
  }
12229
60.9k
      else
12230
60.9k
  {
12231
    /* 16-bit checks.  */
12232
60.9k
    if (i.disp_encoding == disp_encoding_32bit)
12233
16
      goto bad_disp;
12234
12235
60.8k
    if ((i.base_reg
12236
60.8k
         && (!i.base_reg->reg_type.bitfield.word
12237
0
       || !i.base_reg->reg_type.bitfield.baseindex))
12238
60.8k
        || (i.index_reg
12239
60.8k
      && (!i.index_reg->reg_type.bitfield.word
12240
0
          || !i.index_reg->reg_type.bitfield.baseindex
12241
0
          || !(i.base_reg
12242
0
         && i.base_reg->reg_num < 6
12243
0
         && i.index_reg->reg_num >= 6
12244
0
         && i.log2_scale_factor == 0))))
12245
0
      goto bad_address;
12246
60.8k
  }
12247
112k
    }
12248
112k
  return 1;
12249
115k
}
12250
12251
/* Handle vector immediates.  */
12252
12253
static int
12254
RC_SAE_immediate (const char *imm_start)
12255
116k
{
12256
116k
  const char *pstr = imm_start;
12257
12258
116k
  if (*pstr != '{')
12259
116k
    return 0;
12260
12261
1
  pstr = RC_SAE_specifier (pstr + 1);
12262
1
  if (pstr == NULL)
12263
0
    return 0;
12264
12265
1
  if (*pstr++ != '}')
12266
1
    {
12267
1
      as_bad (_("Missing '}': '%s'"), imm_start);
12268
1
      return 0;
12269
1
    }
12270
0
  /* RC/SAE immediate string should contain nothing more.  */;
12271
0
  if (*pstr != 0)
12272
0
    {
12273
0
      as_bad (_("Junk after '}': '%s'"), imm_start);
12274
0
      return 0;
12275
0
    }
12276
12277
  /* Internally this doesn't count as an operand.  */
12278
0
  --i.operands;
12279
12280
0
  return 1;
12281
0
}
12282
12283
static INLINE bool starts_memory_operand (char c)
12284
116k
{
12285
116k
  return ISDIGIT (c)
12286
116k
   || is_name_beginner (c)
12287
116k
   || strchr ("([\"+-!~", c);
12288
116k
}
12289
12290
/* Parse OPERAND_STRING into the i386_insn structure I.  Returns zero
12291
   on error.  */
12292
12293
static int
12294
i386_att_operand (char *operand_string)
12295
220k
{
12296
220k
  const reg_entry *r;
12297
220k
  char *end_op;
12298
220k
  char *op_string = operand_string;
12299
12300
220k
  if (is_space_char (*op_string))
12301
0
    ++op_string;
12302
12303
  /* We check for an absolute prefix (differentiating,
12304
     for example, 'jmp pc_relative_label' from 'jmp *absolute_label'.  */
12305
220k
  if (*op_string == ABSOLUTE_PREFIX
12306
220k
      && current_templates->start->opcode_modifier.jump)
12307
193
    {
12308
193
      ++op_string;
12309
193
      if (is_space_char (*op_string))
12310
0
  ++op_string;
12311
193
      i.jumpabsolute = true;
12312
193
    }
12313
12314
  /* Check if operand is a register.  */
12315
220k
  if ((r = parse_register (op_string, &end_op)) != NULL)
12316
72
    {
12317
72
      i386_operand_type temp;
12318
12319
72
      if (r == &bad_reg)
12320
0
  return 0;
12321
12322
      /* Check for a segment override by searching for ':' after a
12323
   segment register.  */
12324
72
      op_string = end_op;
12325
72
      if (is_space_char (*op_string))
12326
0
  ++op_string;
12327
72
      if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
12328
60
  {
12329
60
    i.seg[i.mem_operands] = r;
12330
12331
    /* Skip the ':' and whitespace.  */
12332
60
    ++op_string;
12333
60
    if (is_space_char (*op_string))
12334
0
      ++op_string;
12335
12336
    /* Handle case of %es:*foo.  */
12337
60
    if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX
12338
60
        && current_templates->start->opcode_modifier.jump)
12339
0
      {
12340
0
        ++op_string;
12341
0
        if (is_space_char (*op_string))
12342
0
    ++op_string;
12343
0
        i.jumpabsolute = true;
12344
0
      }
12345
12346
60
    if (!starts_memory_operand (*op_string))
12347
0
      {
12348
0
        as_bad (_("bad memory operand `%s'"), op_string);
12349
0
        return 0;
12350
0
      }
12351
60
    goto do_memory_reference;
12352
60
  }
12353
12354
      /* Handle vector operations.  */
12355
12
      if (*op_string == '{')
12356
0
  {
12357
0
    op_string = check_VecOperations (op_string);
12358
0
    if (op_string == NULL)
12359
0
      return 0;
12360
0
  }
12361
12362
12
      if (*op_string)
12363
8
  {
12364
8
    as_bad (_("junk `%s' after register"), op_string);
12365
8
    return 0;
12366
8
  }
12367
12368
       /* Reject pseudo registers for .insn.  */
12369
4
      if (dot_insn () && r->reg_type.bitfield.class == ClassNone)
12370
0
  {
12371
0
    as_bad (_("`%s%s' cannot be used here"),
12372
0
      register_prefix, r->reg_name);
12373
0
    return 0;
12374
0
  }
12375
12376
4
      temp = r->reg_type;
12377
4
      temp.bitfield.baseindex = 0;
12378
4
      i.types[this_operand] = operand_type_or (i.types[this_operand],
12379
4
                 temp);
12380
4
      i.types[this_operand].bitfield.unspecified = 0;
12381
4
      i.op[this_operand].regs = r;
12382
4
      i.reg_operands++;
12383
12384
      /* A GPR may follow an RC or SAE immediate only if a (vector) register
12385
         operand was also present earlier on.  */
12386
4
      if (i.rounding.type != rc_none && temp.bitfield.class == Reg
12387
4
          && i.reg_operands == 1)
12388
0
  {
12389
0
    unsigned int j;
12390
12391
0
    for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
12392
0
      if (i.rounding.type == RC_NamesTable[j].type)
12393
0
        break;
12394
0
    as_bad (_("`%s': misplaced `{%s}'"),
12395
0
      insn_name (current_templates->start), RC_NamesTable[j].name);
12396
0
    return 0;
12397
0
  }
12398
4
    }
12399
220k
  else if (*op_string == REGISTER_PREFIX)
12400
13
    {
12401
13
      as_bad (_("bad register name `%s'"), op_string);
12402
13
      return 0;
12403
13
    }
12404
220k
  else if (*op_string == IMMEDIATE_PREFIX)
12405
104k
    {
12406
104k
      ++op_string;
12407
104k
      if (i.jumpabsolute)
12408
0
  {
12409
0
    as_bad (_("immediate operand illegal with absolute jump"));
12410
0
    return 0;
12411
0
  }
12412
104k
      if (!i386_immediate (op_string))
12413
14.5k
  return 0;
12414
89.8k
      if (i.rounding.type != rc_none)
12415
0
  {
12416
0
    as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
12417
0
      insn_name (current_templates->start));
12418
0
    return 0;
12419
0
  }
12420
89.8k
    }
12421
116k
  else if (RC_SAE_immediate (operand_string))
12422
0
    {
12423
      /* If it is a RC or SAE immediate, do the necessary placement check:
12424
   Only another immediate or a GPR may precede it.  */
12425
0
      if (i.mem_operands || i.reg_operands + i.imm_operands > 1
12426
0
    || (i.reg_operands == 1
12427
0
        && i.op[0].regs->reg_type.bitfield.class != Reg))
12428
0
  {
12429
0
    as_bad (_("`%s': misplaced `%s'"),
12430
0
      insn_name (current_templates->start), operand_string);
12431
0
    return 0;
12432
0
  }
12433
0
    }
12434
116k
  else if (starts_memory_operand (*op_string))
12435
116k
    {
12436
      /* This is a memory reference of some sort.  */
12437
116k
      char *base_string;
12438
12439
      /* Start and end of displacement string expression (if found).  */
12440
116k
      char *displacement_string_start;
12441
116k
      char *displacement_string_end;
12442
12443
116k
    do_memory_reference:
12444
      /* Check for base index form.  We detect the base index form by
12445
   looking for an ')' at the end of the operand, searching
12446
   for the '(' matching it, and finding a REGISTER_PREFIX or ','
12447
   after the '('.  */
12448
116k
      base_string = op_string + strlen (op_string);
12449
12450
      /* Handle vector operations.  */
12451
116k
      --base_string;
12452
116k
      if (is_space_char (*base_string))
12453
190
  --base_string;
12454
12455
116k
      if (*base_string == '}')
12456
8
  {
12457
8
    char *vop_start = NULL;
12458
12459
101
    while (base_string-- > op_string)
12460
93
      {
12461
93
        if (*base_string == '"')
12462
0
    break;
12463
93
        if (*base_string != '{')
12464
93
    continue;
12465
12466
0
        vop_start = base_string;
12467
12468
0
        --base_string;
12469
0
        if (is_space_char (*base_string))
12470
0
    --base_string;
12471
12472
0
        if (*base_string != '}')
12473
0
    break;
12474
12475
0
        vop_start = NULL;
12476
0
      }
12477
12478
8
    if (!vop_start)
12479
8
      {
12480
8
        as_bad (_("unbalanced figure braces"));
12481
8
        return 0;
12482
8
      }
12483
12484
0
    if (check_VecOperations (vop_start) == NULL)
12485
0
      return 0;
12486
0
  }
12487
12488
      /* If we only have a displacement, set-up for it to be parsed later.  */
12489
116k
      displacement_string_start = op_string;
12490
116k
      displacement_string_end = base_string + 1;
12491
12492
116k
      if (*base_string == ')')
12493
966
  {
12494
966
    char *temp_string;
12495
966
    unsigned int parens_not_balanced = 0;
12496
966
    bool in_quotes = false;
12497
12498
    /* We've already checked that the number of left & right ()'s are
12499
       equal, and that there's a matching set of double quotes.  */
12500
966
    end_op = base_string;
12501
6.55k
    for (temp_string = op_string; temp_string < end_op; temp_string++)
12502
5.59k
      {
12503
5.59k
        if (*temp_string == '\\' && temp_string[1] == '"')
12504
0
    ++temp_string;
12505
5.59k
        else if (*temp_string == '"')
12506
0
    in_quotes = !in_quotes;
12507
5.59k
        else if (!in_quotes)
12508
5.59k
    {
12509
5.59k
      if (*temp_string == '(' && !parens_not_balanced++)
12510
1.06k
        base_string = temp_string;
12511
5.59k
      if (*temp_string == ')')
12512
97
        --parens_not_balanced;
12513
5.59k
    }
12514
5.59k
      }
12515
12516
966
    temp_string = base_string;
12517
12518
    /* Skip past '(' and whitespace.  */
12519
966
    gas_assert (*base_string == '(');
12520
0
    ++base_string;
12521
966
    if (is_space_char (*base_string))
12522
134
      ++base_string;
12523
12524
966
    if (*base_string == ','
12525
966
        || ((i.base_reg = parse_register (base_string, &end_op))
12526
945
      != NULL))
12527
21
      {
12528
21
        displacement_string_end = temp_string;
12529
12530
21
        i.types[this_operand].bitfield.baseindex = 1;
12531
12532
21
        if (i.base_reg)
12533
0
    {
12534
0
      if (i.base_reg == &bad_reg)
12535
0
        return 0;
12536
0
      base_string = end_op;
12537
0
      if (is_space_char (*base_string))
12538
0
        ++base_string;
12539
0
    }
12540
12541
        /* There may be an index reg or scale factor here.  */
12542
21
        if (*base_string == ',')
12543
21
    {
12544
21
      ++base_string;
12545
21
      if (is_space_char (*base_string))
12546
0
        ++base_string;
12547
12548
21
      if ((i.index_reg = parse_register (base_string, &end_op))
12549
21
          != NULL)
12550
0
        {
12551
0
          if (i.index_reg == &bad_reg)
12552
0
      return 0;
12553
0
          base_string = end_op;
12554
0
          if (is_space_char (*base_string))
12555
0
      ++base_string;
12556
0
          if (*base_string == ',')
12557
0
      {
12558
0
        ++base_string;
12559
0
        if (is_space_char (*base_string))
12560
0
          ++base_string;
12561
0
      }
12562
0
          else if (*base_string != ')')
12563
0
      {
12564
0
        as_bad (_("expecting `,' or `)' "
12565
0
            "after index register in `%s'"),
12566
0
          operand_string);
12567
0
        return 0;
12568
0
      }
12569
0
        }
12570
21
      else if (*base_string == REGISTER_PREFIX)
12571
0
        {
12572
0
          end_op = strchr (base_string, ',');
12573
0
          if (end_op)
12574
0
      *end_op = '\0';
12575
0
          as_bad (_("bad register name `%s'"), base_string);
12576
0
          return 0;
12577
0
        }
12578
12579
      /* Check for scale factor.  */
12580
21
      if (*base_string != ')')
12581
21
        {
12582
21
          char *end_scale = i386_scale (base_string);
12583
12584
21
          if (!end_scale)
12585
13
      return 0;
12586
12587
8
          base_string = end_scale;
12588
8
          if (is_space_char (*base_string))
12589
0
      ++base_string;
12590
8
          if (*base_string != ')')
12591
8
      {
12592
8
        as_bad (_("expecting `)' "
12593
8
            "after scale factor in `%s'"),
12594
8
          operand_string);
12595
8
        return 0;
12596
8
      }
12597
8
        }
12598
0
      else if (!i.index_reg)
12599
0
        {
12600
0
          as_bad (_("expecting index register or scale factor "
12601
0
        "after `,'; got '%c'"),
12602
0
            *base_string);
12603
0
          return 0;
12604
0
        }
12605
21
    }
12606
0
        else if (*base_string != ')')
12607
0
    {
12608
0
      as_bad (_("expecting `,' or `)' "
12609
0
          "after base register in `%s'"),
12610
0
        operand_string);
12611
0
      return 0;
12612
0
    }
12613
21
      }
12614
945
    else if (*base_string == REGISTER_PREFIX)
12615
3
      {
12616
3
        end_op = strchr (base_string, ',');
12617
3
        if (end_op)
12618
0
    *end_op = '\0';
12619
3
        as_bad (_("bad register name `%s'"), base_string);
12620
3
        return 0;
12621
3
      }
12622
966
  }
12623
12624
      /* If there's an expression beginning the operand, parse it,
12625
   assuming displacement_string_start and
12626
   displacement_string_end are meaningful.  */
12627
116k
      if (displacement_string_start != displacement_string_end)
12628
116k
  {
12629
116k
    if (!i386_displacement (displacement_string_start,
12630
116k
          displacement_string_end))
12631
380
      return 0;
12632
116k
  }
12633
12634
      /* Special case for (%dx) while doing input/output op.  */
12635
115k
      if (i.base_reg
12636
115k
    && i.base_reg->reg_type.bitfield.instance == RegD
12637
115k
    && i.base_reg->reg_type.bitfield.word
12638
115k
    && i.index_reg == 0
12639
115k
    && i.log2_scale_factor == 0
12640
115k
    && i.seg[i.mem_operands] == 0
12641
115k
    && !operand_type_check (i.types[this_operand], disp))
12642
0
  {
12643
0
    i.types[this_operand] = i.base_reg->reg_type;
12644
0
    i.input_output_operand = true;
12645
0
    return 1;
12646
0
  }
12647
12648
115k
      if (i386_index_check (operand_string) == 0)
12649
16
  return 0;
12650
115k
      i.flags[this_operand] |= Operand_Mem;
12651
115k
      i.mem_operands++;
12652
115k
    }
12653
253
  else
12654
253
    {
12655
      /* It's not a memory operand; argh!  */
12656
253
      as_bad (_("invalid char %s beginning operand %d `%s'"),
12657
253
        output_invalid (*op_string),
12658
253
        this_operand + 1,
12659
253
        op_string);
12660
253
      return 0;
12661
253
    }
12662
205k
  return 1;     /* Normal return.  */
12663
220k
}
12664

12665
/* Calculate the maximum variable size (i.e., excluding fr_fix)
12666
   that an rs_machine_dependent frag may reach.  */
12667
12668
unsigned int
12669
i386_frag_max_var (fragS *frag)
12670
0
{
12671
  /* The only relaxable frags are for jumps.
12672
     Unconditional jumps can grow by 4 bytes and others by 5 bytes.  */
12673
0
  gas_assert (frag->fr_type == rs_machine_dependent);
12674
0
  return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
12675
0
}
12676
12677
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12678
static int
12679
elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
12680
0
{
12681
  /* STT_GNU_IFUNC symbol must go through PLT.  */
12682
0
  if ((symbol_get_bfdsym (fr_symbol)->flags
12683
0
       & BSF_GNU_INDIRECT_FUNCTION) != 0)
12684
0
    return 0;
12685
12686
0
  if (!S_IS_EXTERNAL (fr_symbol))
12687
    /* Symbol may be weak or local.  */
12688
0
    return !S_IS_WEAK (fr_symbol);
12689
12690
  /* Global symbols with non-default visibility can't be preempted. */
12691
0
  if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
12692
0
    return 1;
12693
12694
0
  if (fr_var != NO_RELOC)
12695
0
    switch ((enum bfd_reloc_code_real) fr_var)
12696
0
      {
12697
0
      case BFD_RELOC_386_PLT32:
12698
0
      case BFD_RELOC_X86_64_PLT32:
12699
  /* Symbol with PLT relocation may be preempted. */
12700
0
  return 0;
12701
0
      default:
12702
0
  abort ();
12703
0
      }
12704
12705
  /* Global symbols with default visibility in a shared library may be
12706
     preempted by another definition.  */
12707
0
  return !shared;
12708
0
}
12709
#endif
12710
12711
/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
12712
   Note also work for Skylake and Cascadelake.
12713
---------------------------------------------------------------------
12714
|   JCC   | ADD/SUB/CMP | INC/DEC | TEST/AND |
12715
| ------  | ----------- | ------- | -------- |
12716
|   Jo    |      N      |    N    |     Y    |
12717
|   Jno   |      N      |    N    |     Y    |
12718
|  Jc/Jb  |      Y      |    N    |     Y    |
12719
| Jae/Jnb |      Y      |    N    |     Y    |
12720
|  Je/Jz  |      Y      |    Y    |     Y    |
12721
| Jne/Jnz |      Y      |    Y    |     Y    |
12722
| Jna/Jbe |      Y      |    N    |     Y    |
12723
| Ja/Jnbe |      Y      |    N    |     Y    |
12724
|   Js    |      N      |    N    |     Y    |
12725
|   Jns   |      N      |    N    |     Y    |
12726
|  Jp/Jpe |      N      |    N    |     Y    |
12727
| Jnp/Jpo |      N      |    N    |     Y    |
12728
| Jl/Jnge |      Y      |    Y    |     Y    |
12729
| Jge/Jnl |      Y      |    Y    |     Y    |
12730
| Jle/Jng |      Y      |    Y    |     Y    |
12731
| Jg/Jnle |      Y      |    Y    |     Y    |
12732
---------------------------------------------------------------------  */
12733
static int
12734
i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
12735
0
{
12736
0
  if (mf_cmp == mf_cmp_alu_cmp)
12737
0
    return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
12738
0
      || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
12739
0
  if (mf_cmp == mf_cmp_incdec)
12740
0
    return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
12741
0
      || mf_jcc == mf_jcc_jle);
12742
0
  if (mf_cmp == mf_cmp_test_and)
12743
0
    return 1;
12744
0
  return 0;
12745
0
}
12746
12747
/* Return the next non-empty frag.  */
12748
12749
static fragS *
12750
i386_next_non_empty_frag (fragS *fragP)
12751
0
{
12752
  /* There may be a frag with a ".fill 0" when there is no room in
12753
     the current frag for frag_grow in output_insn.  */
12754
0
  for (fragP = fragP->fr_next;
12755
0
       (fragP != NULL
12756
0
  && fragP->fr_type == rs_fill
12757
0
  && fragP->fr_fix == 0);
12758
0
       fragP = fragP->fr_next)
12759
0
    ;
12760
0
  return fragP;
12761
0
}
12762
12763
/* Return the next jcc frag after BRANCH_PADDING.  */
12764
12765
static fragS *
12766
i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
12767
0
{
12768
0
  fragS *branch_fragP;
12769
0
  if (!pad_fragP)
12770
0
    return NULL;
12771
12772
0
  if (pad_fragP->fr_type == rs_machine_dependent
12773
0
      && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
12774
0
    == BRANCH_PADDING))
12775
0
    {
12776
0
      branch_fragP = i386_next_non_empty_frag (pad_fragP);
12777
0
      if (branch_fragP->fr_type != rs_machine_dependent)
12778
0
  return NULL;
12779
0
      if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
12780
0
    && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
12781
0
           pad_fragP->tc_frag_data.mf_type))
12782
0
  return branch_fragP;
12783
0
    }
12784
12785
0
  return NULL;
12786
0
}
12787
12788
/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags.  */
12789
12790
static void
12791
i386_classify_machine_dependent_frag (fragS *fragP)
12792
0
{
12793
0
  fragS *cmp_fragP;
12794
0
  fragS *pad_fragP;
12795
0
  fragS *branch_fragP;
12796
0
  fragS *next_fragP;
12797
0
  unsigned int max_prefix_length;
12798
12799
0
  if (fragP->tc_frag_data.classified)
12800
0
    return;
12801
12802
  /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING.  Convert
12803
     FUSED_JCC_PADDING and merge BRANCH_PADDING.  */
12804
0
  for (next_fragP = fragP;
12805
0
       next_fragP != NULL;
12806
0
       next_fragP = next_fragP->fr_next)
12807
0
    {
12808
0
      next_fragP->tc_frag_data.classified = 1;
12809
0
      if (next_fragP->fr_type == rs_machine_dependent)
12810
0
  switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
12811
0
    {
12812
0
    case BRANCH_PADDING:
12813
      /* The BRANCH_PADDING frag must be followed by a branch
12814
         frag.  */
12815
0
      branch_fragP = i386_next_non_empty_frag (next_fragP);
12816
0
      next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
12817
0
      break;
12818
0
    case FUSED_JCC_PADDING:
12819
      /* Check if this is a fused jcc:
12820
         FUSED_JCC_PADDING
12821
         CMP like instruction
12822
         BRANCH_PADDING
12823
         COND_JUMP
12824
         */
12825
0
      cmp_fragP = i386_next_non_empty_frag (next_fragP);
12826
0
      pad_fragP = i386_next_non_empty_frag (cmp_fragP);
12827
0
      branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
12828
0
      if (branch_fragP)
12829
0
        {
12830
    /* The BRANCH_PADDING frag is merged with the
12831
       FUSED_JCC_PADDING frag.  */
12832
0
    next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
12833
    /* CMP like instruction size.  */
12834
0
    next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
12835
0
    frag_wane (pad_fragP);
12836
    /* Skip to branch_fragP.  */
12837
0
    next_fragP = branch_fragP;
12838
0
        }
12839
0
      else if (next_fragP->tc_frag_data.max_prefix_length)
12840
0
        {
12841
    /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
12842
       a fused jcc.  */
12843
0
    next_fragP->fr_subtype
12844
0
      = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
12845
0
    next_fragP->tc_frag_data.max_bytes
12846
0
      = next_fragP->tc_frag_data.max_prefix_length;
12847
    /* This will be updated in the BRANCH_PREFIX scan.  */
12848
0
    next_fragP->tc_frag_data.max_prefix_length = 0;
12849
0
        }
12850
0
      else
12851
0
        frag_wane (next_fragP);
12852
0
      break;
12853
0
    }
12854
0
    }
12855
12856
  /* Stop if there is no BRANCH_PREFIX.  */
12857
0
  if (!align_branch_prefix_size)
12858
0
    return;
12859
12860
  /* Scan for BRANCH_PREFIX.  */
12861
0
  for (; fragP != NULL; fragP = fragP->fr_next)
12862
0
    {
12863
0
      if (fragP->fr_type != rs_machine_dependent
12864
0
    || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
12865
0
        != BRANCH_PREFIX))
12866
0
  continue;
12867
12868
      /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
12869
   COND_JUMP_PREFIX.  */
12870
0
      max_prefix_length = 0;
12871
0
      for (next_fragP = fragP;
12872
0
     next_fragP != NULL;
12873
0
     next_fragP = next_fragP->fr_next)
12874
0
  {
12875
0
    if (next_fragP->fr_type == rs_fill)
12876
      /* Skip rs_fill frags.  */
12877
0
      continue;
12878
0
    else if (next_fragP->fr_type != rs_machine_dependent)
12879
      /* Stop for all other frags.  */
12880
0
      break;
12881
12882
    /* rs_machine_dependent frags.  */
12883
0
    if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12884
0
        == BRANCH_PREFIX)
12885
0
      {
12886
        /* Count BRANCH_PREFIX frags.  */
12887
0
        if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
12888
0
    {
12889
0
      max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
12890
0
      frag_wane (next_fragP);
12891
0
    }
12892
0
        else
12893
0
    max_prefix_length
12894
0
      += next_fragP->tc_frag_data.max_bytes;
12895
0
      }
12896
0
    else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12897
0
        == BRANCH_PADDING)
12898
0
       || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
12899
0
           == FUSED_JCC_PADDING))
12900
0
      {
12901
        /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING.  */
12902
0
        fragP->tc_frag_data.u.padding_fragP = next_fragP;
12903
0
        break;
12904
0
      }
12905
0
    else
12906
      /* Stop for other rs_machine_dependent frags.  */
12907
0
      break;
12908
0
  }
12909
12910
0
      fragP->tc_frag_data.max_prefix_length = max_prefix_length;
12911
12912
      /* Skip to the next frag.  */
12913
0
      fragP = next_fragP;
12914
0
    }
12915
0
}
12916
12917
/* Compute padding size for
12918
12919
  FUSED_JCC_PADDING
12920
  CMP like instruction
12921
  BRANCH_PADDING
12922
  COND_JUMP/UNCOND_JUMP
12923
12924
   or
12925
12926
  BRANCH_PADDING
12927
  COND_JUMP/UNCOND_JUMP
12928
 */
12929
12930
static int
12931
i386_branch_padding_size (fragS *fragP, offsetT address)
12932
0
{
12933
0
  unsigned int offset, size, padding_size;
12934
0
  fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
12935
12936
  /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag.  */
12937
0
  if (!address)
12938
0
    address = fragP->fr_address;
12939
0
  address += fragP->fr_fix;
12940
12941
  /* CMP like instrunction size.  */
12942
0
  size = fragP->tc_frag_data.cmp_size;
12943
12944
  /* The base size of the branch frag.  */
12945
0
  size += branch_fragP->fr_fix;
12946
12947
  /* Add opcode and displacement bytes for the rs_machine_dependent
12948
     branch frag.  */
12949
0
  if (branch_fragP->fr_type == rs_machine_dependent)
12950
0
    size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
12951
12952
  /* Check if branch is within boundary and doesn't end at the last
12953
     byte.  */
12954
0
  offset = address & ((1U << align_branch_power) - 1);
12955
0
  if ((offset + size) >= (1U << align_branch_power))
12956
    /* Padding needed to avoid crossing boundary.  */
12957
0
    padding_size = (1U << align_branch_power) - offset;
12958
0
  else
12959
    /* No padding needed.  */
12960
0
    padding_size = 0;
12961
12962
  /* The return value may be saved in tc_frag_data.length which is
12963
     unsigned byte.  */
12964
0
  if (!fits_in_unsigned_byte (padding_size))
12965
0
    abort ();
12966
12967
0
  return padding_size;
12968
0
}
12969
12970
/* i386_generic_table_relax_frag()
12971
12972
   Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
12973
   grow/shrink padding to align branch frags.  Hand others to
12974
   relax_frag().  */
12975
12976
long
12977
i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
12978
0
{
12979
0
  if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
12980
0
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
12981
0
    {
12982
0
      long padding_size = i386_branch_padding_size (fragP, 0);
12983
0
      long grow = padding_size - fragP->tc_frag_data.length;
12984
12985
      /* When the BRANCH_PREFIX frag is used, the computed address
12986
         must match the actual address and there should be no padding.  */
12987
0
      if (fragP->tc_frag_data.padding_address
12988
0
    && (fragP->tc_frag_data.padding_address != fragP->fr_address
12989
0
        || padding_size))
12990
0
  abort ();
12991
12992
      /* Update the padding size.  */
12993
0
      if (grow)
12994
0
  fragP->tc_frag_data.length = padding_size;
12995
12996
0
      return grow;
12997
0
    }
12998
0
  else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
12999
0
    {
13000
0
      fragS *padding_fragP, *next_fragP;
13001
0
      long padding_size, left_size, last_size;
13002
13003
0
      padding_fragP = fragP->tc_frag_data.u.padding_fragP;
13004
0
      if (!padding_fragP)
13005
  /* Use the padding set by the leading BRANCH_PREFIX frag.  */
13006
0
  return (fragP->tc_frag_data.length
13007
0
    - fragP->tc_frag_data.last_length);
13008
13009
      /* Compute the relative address of the padding frag in the very
13010
        first time where the BRANCH_PREFIX frag sizes are zero.  */
13011
0
      if (!fragP->tc_frag_data.padding_address)
13012
0
  fragP->tc_frag_data.padding_address
13013
0
    = padding_fragP->fr_address - (fragP->fr_address - stretch);
13014
13015
      /* First update the last length from the previous interation.  */
13016
0
      left_size = fragP->tc_frag_data.prefix_length;
13017
0
      for (next_fragP = fragP;
13018
0
     next_fragP != padding_fragP;
13019
0
     next_fragP = next_fragP->fr_next)
13020
0
  if (next_fragP->fr_type == rs_machine_dependent
13021
0
      && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13022
0
    == BRANCH_PREFIX))
13023
0
    {
13024
0
      if (left_size)
13025
0
        {
13026
0
    int max = next_fragP->tc_frag_data.max_bytes;
13027
0
    if (max)
13028
0
      {
13029
0
        int size;
13030
0
        if (max > left_size)
13031
0
          size = left_size;
13032
0
        else
13033
0
          size = max;
13034
0
        left_size -= size;
13035
0
        next_fragP->tc_frag_data.last_length = size;
13036
0
      }
13037
0
        }
13038
0
      else
13039
0
        next_fragP->tc_frag_data.last_length = 0;
13040
0
    }
13041
13042
      /* Check the padding size for the padding frag.  */
13043
0
      padding_size = i386_branch_padding_size
13044
0
  (padding_fragP, (fragP->fr_address
13045
0
       + fragP->tc_frag_data.padding_address));
13046
13047
0
      last_size = fragP->tc_frag_data.prefix_length;
13048
      /* Check if there is change from the last interation.  */
13049
0
      if (padding_size == last_size)
13050
0
  {
13051
    /* Update the expected address of the padding frag.  */
13052
0
    padding_fragP->tc_frag_data.padding_address
13053
0
      = (fragP->fr_address + padding_size
13054
0
         + fragP->tc_frag_data.padding_address);
13055
0
    return 0;
13056
0
  }
13057
13058
0
      if (padding_size > fragP->tc_frag_data.max_prefix_length)
13059
0
  {
13060
    /* No padding if there is no sufficient room.  Clear the
13061
       expected address of the padding frag.  */
13062
0
    padding_fragP->tc_frag_data.padding_address = 0;
13063
0
    padding_size = 0;
13064
0
  }
13065
0
      else
13066
  /* Store the expected address of the padding frag.  */
13067
0
  padding_fragP->tc_frag_data.padding_address
13068
0
    = (fragP->fr_address + padding_size
13069
0
       + fragP->tc_frag_data.padding_address);
13070
13071
0
      fragP->tc_frag_data.prefix_length = padding_size;
13072
13073
      /* Update the length for the current interation.  */
13074
0
      left_size = padding_size;
13075
0
      for (next_fragP = fragP;
13076
0
     next_fragP != padding_fragP;
13077
0
     next_fragP = next_fragP->fr_next)
13078
0
  if (next_fragP->fr_type == rs_machine_dependent
13079
0
      && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13080
0
    == BRANCH_PREFIX))
13081
0
    {
13082
0
      if (left_size)
13083
0
        {
13084
0
    int max = next_fragP->tc_frag_data.max_bytes;
13085
0
    if (max)
13086
0
      {
13087
0
        int size;
13088
0
        if (max > left_size)
13089
0
          size = left_size;
13090
0
        else
13091
0
          size = max;
13092
0
        left_size -= size;
13093
0
        next_fragP->tc_frag_data.length = size;
13094
0
      }
13095
0
        }
13096
0
      else
13097
0
        next_fragP->tc_frag_data.length = 0;
13098
0
    }
13099
13100
0
      return (fragP->tc_frag_data.length
13101
0
        - fragP->tc_frag_data.last_length);
13102
0
    }
13103
0
  return relax_frag (segment, fragP, stretch);
13104
0
}
13105
13106
/* md_estimate_size_before_relax()
13107
13108
   Called just before relax() for rs_machine_dependent frags.  The x86
13109
   assembler uses these frags to handle variable size jump
13110
   instructions.
13111
13112
   Any symbol that is now undefined will not become defined.
13113
   Return the correct fr_subtype in the frag.
13114
   Return the initial "guess for variable size of frag" to caller.
13115
   The guess is actually the growth beyond the fixed part.  Whatever
13116
   we do to grow the fixed or variable part contributes to our
13117
   returned value.  */
13118
13119
int
13120
md_estimate_size_before_relax (fragS *fragP, segT segment)
13121
0
{
13122
0
  if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
13123
0
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
13124
0
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
13125
0
    {
13126
0
      i386_classify_machine_dependent_frag (fragP);
13127
0
      return fragP->tc_frag_data.length;
13128
0
    }
13129
13130
  /* We've already got fragP->fr_subtype right;  all we have to do is
13131
     check for un-relaxable symbols.  On an ELF system, we can't relax
13132
     an externally visible symbol, because it may be overridden by a
13133
     shared library.  */
13134
0
  if (S_GET_SEGMENT (fragP->fr_symbol) != segment
13135
0
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13136
0
      || (IS_ELF
13137
0
    && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
13138
0
            fragP->fr_var))
13139
0
#endif
13140
#if defined (OBJ_COFF) && defined (TE_PE)
13141
      || (OUTPUT_FLAVOR == bfd_target_coff_flavour
13142
    && S_IS_WEAK (fragP->fr_symbol))
13143
#endif
13144
0
      )
13145
0
    {
13146
      /* Symbol is undefined in this segment, or we need to keep a
13147
   reloc so that weak symbols can be overridden.  */
13148
0
      int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
13149
0
      enum bfd_reloc_code_real reloc_type;
13150
0
      unsigned char *opcode;
13151
0
      int old_fr_fix;
13152
0
      fixS *fixP = NULL;
13153
13154
0
      if (fragP->fr_var != NO_RELOC)
13155
0
  reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
13156
0
      else if (size == 2)
13157
0
  reloc_type = BFD_RELOC_16_PCREL;
13158
0
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13159
0
      else if (fragP->tc_frag_data.code64 && fragP->fr_offset == 0
13160
0
         && need_plt32_p (fragP->fr_symbol))
13161
0
  reloc_type = BFD_RELOC_X86_64_PLT32;
13162
0
#endif
13163
0
      else
13164
0
  reloc_type = BFD_RELOC_32_PCREL;
13165
13166
0
      old_fr_fix = fragP->fr_fix;
13167
0
      opcode = (unsigned char *) fragP->fr_opcode;
13168
13169
0
      switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
13170
0
  {
13171
0
  case UNCOND_JUMP:
13172
    /* Make jmp (0xeb) a (d)word displacement jump.  */
13173
0
    opcode[0] = 0xe9;
13174
0
    fragP->fr_fix += size;
13175
0
    fixP = fix_new (fragP, old_fr_fix, size,
13176
0
        fragP->fr_symbol,
13177
0
        fragP->fr_offset, 1,
13178
0
        reloc_type);
13179
0
    break;
13180
13181
0
  case COND_JUMP86:
13182
0
    if (size == 2
13183
0
        && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
13184
0
      {
13185
        /* Negate the condition, and branch past an
13186
     unconditional jump.  */
13187
0
        opcode[0] ^= 1;
13188
0
        opcode[1] = 3;
13189
        /* Insert an unconditional jump.  */
13190
0
        opcode[2] = 0xe9;
13191
        /* We added two extra opcode bytes, and have a two byte
13192
     offset.  */
13193
0
        fragP->fr_fix += 2 + 2;
13194
0
        fix_new (fragP, old_fr_fix + 2, 2,
13195
0
           fragP->fr_symbol,
13196
0
           fragP->fr_offset, 1,
13197
0
           reloc_type);
13198
0
        break;
13199
0
      }
13200
    /* Fall through.  */
13201
13202
0
  case COND_JUMP:
13203
0
    if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
13204
0
      {
13205
0
        fragP->fr_fix += 1;
13206
0
        fixP = fix_new (fragP, old_fr_fix, 1,
13207
0
            fragP->fr_symbol,
13208
0
            fragP->fr_offset, 1,
13209
0
            BFD_RELOC_8_PCREL);
13210
0
        fixP->fx_signed = 1;
13211
0
        break;
13212
0
      }
13213
13214
    /* This changes the byte-displacement jump 0x7N
13215
       to the (d)word-displacement jump 0x0f,0x8N.  */
13216
0
    opcode[1] = opcode[0] + 0x10;
13217
0
    opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
13218
    /* We've added an opcode byte.  */
13219
0
    fragP->fr_fix += 1 + size;
13220
0
    fixP = fix_new (fragP, old_fr_fix + 1, size,
13221
0
        fragP->fr_symbol,
13222
0
        fragP->fr_offset, 1,
13223
0
        reloc_type);
13224
0
    break;
13225
13226
0
  default:
13227
0
    BAD_CASE (fragP->fr_subtype);
13228
0
    break;
13229
0
  }
13230
13231
      /* All jumps handled here are signed, but don't unconditionally use a
13232
   signed limit check for 32 and 16 bit jumps as we want to allow wrap
13233
   around at 4G (outside of 64-bit mode) and 64k.  */
13234
0
      if (size == 4 && flag_code == CODE_64BIT)
13235
0
  fixP->fx_signed = 1;
13236
13237
0
      frag_wane (fragP);
13238
0
      return fragP->fr_fix - old_fr_fix;
13239
0
    }
13240
13241
  /* Guess size depending on current relax state.  Initially the relax
13242
     state will correspond to a short jump and we return 1, because
13243
     the variable part of the frag (the branch offset) is one byte
13244
     long.  However, we can relax a section more than once and in that
13245
     case we must either set fr_subtype back to the unrelaxed state,
13246
     or return the value for the appropriate branch.  */
13247
0
  return md_relax_table[fragP->fr_subtype].rlx_length;
13248
0
}
13249
13250
/* Called after relax() is finished.
13251
13252
   In:  Address of frag.
13253
  fr_type == rs_machine_dependent.
13254
  fr_subtype is what the address relaxed to.
13255
13256
   Out: Any fixSs and constants are set up.
13257
  Caller will turn frag into a ".space 0".  */
13258
13259
void
13260
md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
13261
                 fragS *fragP)
13262
0
{
13263
0
  unsigned char *opcode;
13264
0
  unsigned char *where_to_put_displacement = NULL;
13265
0
  offsetT target_address;
13266
0
  offsetT opcode_address;
13267
0
  unsigned int extension = 0;
13268
0
  offsetT displacement_from_opcode_start;
13269
13270
0
  if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
13271
0
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
13272
0
      || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
13273
0
    {
13274
      /* Generate nop padding.  */
13275
0
      unsigned int size = fragP->tc_frag_data.length;
13276
0
      if (size)
13277
0
  {
13278
0
    if (size > fragP->tc_frag_data.max_bytes)
13279
0
      abort ();
13280
13281
0
    if (flag_debug)
13282
0
      {
13283
0
        const char *msg;
13284
0
        const char *branch = "branch";
13285
0
        const char *prefix = "";
13286
0
        fragS *padding_fragP;
13287
0
        if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
13288
0
      == BRANCH_PREFIX)
13289
0
    {
13290
0
      padding_fragP = fragP->tc_frag_data.u.padding_fragP;
13291
0
      switch (fragP->tc_frag_data.default_prefix)
13292
0
        {
13293
0
        default:
13294
0
          abort ();
13295
0
          break;
13296
0
        case CS_PREFIX_OPCODE:
13297
0
          prefix = " cs";
13298
0
          break;
13299
0
        case DS_PREFIX_OPCODE:
13300
0
          prefix = " ds";
13301
0
          break;
13302
0
        case ES_PREFIX_OPCODE:
13303
0
          prefix = " es";
13304
0
          break;
13305
0
        case FS_PREFIX_OPCODE:
13306
0
          prefix = " fs";
13307
0
          break;
13308
0
        case GS_PREFIX_OPCODE:
13309
0
          prefix = " gs";
13310
0
          break;
13311
0
        case SS_PREFIX_OPCODE:
13312
0
          prefix = " ss";
13313
0
          break;
13314
0
        }
13315
0
      if (padding_fragP)
13316
0
        msg = _("%s:%u: add %d%s at 0x%llx to align "
13317
0
          "%s within %d-byte boundary\n");
13318
0
      else
13319
0
        msg = _("%s:%u: add additional %d%s at 0x%llx to "
13320
0
          "align %s within %d-byte boundary\n");
13321
0
    }
13322
0
        else
13323
0
    {
13324
0
      padding_fragP = fragP;
13325
0
      msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
13326
0
        "%s within %d-byte boundary\n");
13327
0
    }
13328
13329
0
        if (padding_fragP)
13330
0
    switch (padding_fragP->tc_frag_data.branch_type)
13331
0
      {
13332
0
      case align_branch_jcc:
13333
0
        branch = "jcc";
13334
0
        break;
13335
0
      case align_branch_fused:
13336
0
        branch = "fused jcc";
13337
0
        break;
13338
0
      case align_branch_jmp:
13339
0
        branch = "jmp";
13340
0
        break;
13341
0
      case align_branch_call:
13342
0
        branch = "call";
13343
0
        break;
13344
0
      case align_branch_indirect:
13345
0
        branch = "indiret branch";
13346
0
        break;
13347
0
      case align_branch_ret:
13348
0
        branch = "ret";
13349
0
        break;
13350
0
      default:
13351
0
        break;
13352
0
      }
13353
13354
0
        fprintf (stdout, msg,
13355
0
           fragP->fr_file, fragP->fr_line, size, prefix,
13356
0
           (long long) fragP->fr_address, branch,
13357
0
           1 << align_branch_power);
13358
0
      }
13359
0
    if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
13360
0
      memset (fragP->fr_opcode,
13361
0
        fragP->tc_frag_data.default_prefix, size);
13362
0
    else
13363
0
      i386_generate_nops (fragP, (char *) fragP->fr_opcode,
13364
0
        size, 0);
13365
0
    fragP->fr_fix += size;
13366
0
  }
13367
0
      return;
13368
0
    }
13369
13370
0
  opcode = (unsigned char *) fragP->fr_opcode;
13371
13372
  /* Address we want to reach in file space.  */
13373
0
  target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
13374
13375
  /* Address opcode resides at in file space.  */
13376
0
  opcode_address = fragP->fr_address + fragP->fr_fix;
13377
13378
  /* Displacement from opcode start to fill into instruction.  */
13379
0
  displacement_from_opcode_start = target_address - opcode_address;
13380
13381
0
  if ((fragP->fr_subtype & BIG) == 0)
13382
0
    {
13383
      /* Don't have to change opcode.  */
13384
0
      extension = 1;    /* 1 opcode + 1 displacement  */
13385
0
      where_to_put_displacement = &opcode[1];
13386
0
    }
13387
0
  else
13388
0
    {
13389
0
      if (no_cond_jump_promotion
13390
0
    && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
13391
0
  as_warn_where (fragP->fr_file, fragP->fr_line,
13392
0
           _("long jump required"));
13393
13394
0
      switch (fragP->fr_subtype)
13395
0
  {
13396
0
  case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
13397
0
    extension = 4;    /* 1 opcode + 4 displacement  */
13398
0
    opcode[0] = 0xe9;
13399
0
    where_to_put_displacement = &opcode[1];
13400
0
    break;
13401
13402
0
  case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
13403
0
    extension = 2;    /* 1 opcode + 2 displacement  */
13404
0
    opcode[0] = 0xe9;
13405
0
    where_to_put_displacement = &opcode[1];
13406
0
    break;
13407
13408
0
  case ENCODE_RELAX_STATE (COND_JUMP, BIG):
13409
0
  case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
13410
0
    extension = 5;    /* 2 opcode + 4 displacement  */
13411
0
    opcode[1] = opcode[0] + 0x10;
13412
0
    opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
13413
0
    where_to_put_displacement = &opcode[2];
13414
0
    break;
13415
13416
0
  case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
13417
0
    extension = 3;    /* 2 opcode + 2 displacement  */
13418
0
    opcode[1] = opcode[0] + 0x10;
13419
0
    opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
13420
0
    where_to_put_displacement = &opcode[2];
13421
0
    break;
13422
13423
0
  case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
13424
0
    extension = 4;
13425
0
    opcode[0] ^= 1;
13426
0
    opcode[1] = 3;
13427
0
    opcode[2] = 0xe9;
13428
0
    where_to_put_displacement = &opcode[3];
13429
0
    break;
13430
13431
0
  default:
13432
0
    BAD_CASE (fragP->fr_subtype);
13433
0
    break;
13434
0
  }
13435
0
    }
13436
13437
  /* If size if less then four we are sure that the operand fits,
13438
     but if it's 4, then it could be that the displacement is larger
13439
     then -/+ 2GB.  */
13440
0
  if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
13441
0
      && object_64bit
13442
0
      && ((addressT) (displacement_from_opcode_start - extension
13443
0
          + ((addressT) 1 << 31))
13444
0
    > (((addressT) 2 << 31) - 1)))
13445
0
    {
13446
0
      as_bad_where (fragP->fr_file, fragP->fr_line,
13447
0
        _("jump target out of range"));
13448
      /* Make us emit 0.  */
13449
0
      displacement_from_opcode_start = extension;
13450
0
    }
13451
  /* Now put displacement after opcode.  */
13452
0
  md_number_to_chars ((char *) where_to_put_displacement,
13453
0
          (valueT) (displacement_from_opcode_start - extension),
13454
0
          DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
13455
0
  fragP->fr_fix += extension;
13456
0
}
13457

13458
/* Apply a fixup (fixP) to segment data, once it has been determined
13459
   by our caller that we have all the info we need to fix it up.
13460
13461
   Parameter valP is the pointer to the value of the bits.
13462
13463
   On the 386, immediates, displacements, and data pointers are all in
13464
   the same (little-endian) format, so we don't need to care about which
13465
   we are handling.  */
13466
13467
void
13468
md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
13469
0
{
13470
0
  char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
13471
0
  valueT value = *valP;
13472
13473
0
#if !defined (TE_Mach)
13474
0
  if (fixP->fx_pcrel)
13475
0
    {
13476
0
      switch (fixP->fx_r_type)
13477
0
  {
13478
0
  default:
13479
0
    break;
13480
13481
0
  case BFD_RELOC_64:
13482
0
    fixP->fx_r_type = BFD_RELOC_64_PCREL;
13483
0
    break;
13484
0
  case BFD_RELOC_32:
13485
0
  case BFD_RELOC_X86_64_32S:
13486
0
    fixP->fx_r_type = BFD_RELOC_32_PCREL;
13487
0
    break;
13488
0
  case BFD_RELOC_16:
13489
0
    fixP->fx_r_type = BFD_RELOC_16_PCREL;
13490
0
    break;
13491
0
  case BFD_RELOC_8:
13492
0
    fixP->fx_r_type = BFD_RELOC_8_PCREL;
13493
0
    break;
13494
0
  }
13495
0
    }
13496
13497
0
  if (fixP->fx_addsy != NULL
13498
0
      && (fixP->fx_r_type == BFD_RELOC_32_PCREL
13499
0
    || fixP->fx_r_type == BFD_RELOC_64_PCREL
13500
0
    || fixP->fx_r_type == BFD_RELOC_16_PCREL
13501
0
    || fixP->fx_r_type == BFD_RELOC_8_PCREL)
13502
0
      && !use_rela_relocations)
13503
0
    {
13504
      /* This is a hack.  There should be a better way to handle this.
13505
   This covers for the fact that bfd_install_relocation will
13506
   subtract the current location (for partial_inplace, PC relative
13507
   relocations); see more below.  */
13508
0
#ifndef OBJ_AOUT
13509
0
      if (IS_ELF
13510
#ifdef TE_PE
13511
    || OUTPUT_FLAVOR == bfd_target_coff_flavour
13512
#endif
13513
0
    )
13514
0
  value += fixP->fx_where + fixP->fx_frag->fr_address;
13515
0
#endif
13516
0
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13517
0
      if (IS_ELF)
13518
0
  {
13519
0
    segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
13520
13521
0
    if ((sym_seg == seg
13522
0
         || (symbol_section_p (fixP->fx_addsy)
13523
0
       && sym_seg != absolute_section))
13524
0
        && !generic_force_reloc (fixP))
13525
0
      {
13526
        /* Yes, we add the values in twice.  This is because
13527
     bfd_install_relocation subtracts them out again.  I think
13528
     bfd_install_relocation is broken, but I don't dare change
13529
     it.  FIXME.  */
13530
0
        value += fixP->fx_where + fixP->fx_frag->fr_address;
13531
0
      }
13532
0
  }
13533
0
#endif
13534
#if defined (OBJ_COFF) && defined (TE_PE)
13535
      /* For some reason, the PE format does not store a
13536
   section address offset for a PC relative symbol.  */
13537
      if (S_GET_SEGMENT (fixP->fx_addsy) != seg
13538
    || S_IS_WEAK (fixP->fx_addsy))
13539
  value += md_pcrel_from (fixP);
13540
#endif
13541
0
    }
13542
#if defined (OBJ_COFF) && defined (TE_PE)
13543
  if (fixP->fx_addsy != NULL
13544
      && S_IS_WEAK (fixP->fx_addsy)
13545
      /* PR 16858: Do not modify weak function references.  */
13546
      && ! fixP->fx_pcrel)
13547
    {
13548
#if !defined (TE_PEP)
13549
      /* For x86 PE weak function symbols are neither PC-relative
13550
   nor do they set S_IS_FUNCTION.  So the only reliable way
13551
   to detect them is to check the flags of their containing
13552
   section.  */
13553
      if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
13554
    && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
13555
  ;
13556
      else
13557
#endif
13558
      value -= S_GET_VALUE (fixP->fx_addsy);
13559
    }
13560
#endif
13561
13562
  /* Fix a few things - the dynamic linker expects certain values here,
13563
     and we must not disappoint it.  */
13564
0
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13565
0
  if (IS_ELF && fixP->fx_addsy)
13566
0
    switch (fixP->fx_r_type)
13567
0
      {
13568
0
      case BFD_RELOC_386_PLT32:
13569
0
      case BFD_RELOC_X86_64_PLT32:
13570
  /* Make the jump instruction point to the address of the operand.
13571
     At runtime we merely add the offset to the actual PLT entry.
13572
     NB: Subtract the offset size only for jump instructions.  */
13573
0
  if (fixP->fx_pcrel)
13574
0
    value = -4;
13575
0
  break;
13576
13577
0
      case BFD_RELOC_386_TLS_GD:
13578
0
      case BFD_RELOC_386_TLS_LDM:
13579
0
      case BFD_RELOC_386_TLS_IE_32:
13580
0
      case BFD_RELOC_386_TLS_IE:
13581
0
      case BFD_RELOC_386_TLS_GOTIE:
13582
0
      case BFD_RELOC_386_TLS_GOTDESC:
13583
0
      case BFD_RELOC_X86_64_TLSGD:
13584
0
      case BFD_RELOC_X86_64_TLSLD:
13585
0
      case BFD_RELOC_X86_64_GOTTPOFF:
13586
0
      case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
13587
0
  value = 0; /* Fully resolved at runtime.  No addend.  */
13588
  /* Fallthrough */
13589
0
      case BFD_RELOC_386_TLS_LE:
13590
0
      case BFD_RELOC_386_TLS_LDO_32:
13591
0
      case BFD_RELOC_386_TLS_LE_32:
13592
0
      case BFD_RELOC_X86_64_DTPOFF32:
13593
0
      case BFD_RELOC_X86_64_DTPOFF64:
13594
0
      case BFD_RELOC_X86_64_TPOFF32:
13595
0
      case BFD_RELOC_X86_64_TPOFF64:
13596
0
  S_SET_THREAD_LOCAL (fixP->fx_addsy);
13597
0
  break;
13598
13599
0
      case BFD_RELOC_386_TLS_DESC_CALL:
13600
0
      case BFD_RELOC_X86_64_TLSDESC_CALL:
13601
0
  value = 0; /* Fully resolved at runtime.  No addend.  */
13602
0
  S_SET_THREAD_LOCAL (fixP->fx_addsy);
13603
0
  fixP->fx_done = 0;
13604
0
  return;
13605
13606
0
      case BFD_RELOC_VTABLE_INHERIT:
13607
0
      case BFD_RELOC_VTABLE_ENTRY:
13608
0
  fixP->fx_done = 0;
13609
0
  return;
13610
13611
0
      default:
13612
0
  break;
13613
0
      }
13614
0
#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)  */
13615
13616
  /* If not 64bit, massage value, to account for wraparound when !BFD64.  */
13617
0
  if (!object_64bit)
13618
0
    value = extend_to_32bit_address (value);
13619
13620
0
  *valP = value;
13621
0
#endif /* !defined (TE_Mach)  */
13622
13623
  /* Are we finished with this relocation now?  */
13624
0
  if (fixP->fx_addsy == NULL)
13625
0
    {
13626
0
      fixP->fx_done = 1;
13627
0
      switch (fixP->fx_r_type)
13628
0
  {
13629
0
  case BFD_RELOC_X86_64_32S:
13630
0
    fixP->fx_signed = 1;
13631
0
    break;
13632
13633
0
  default:
13634
0
    break;
13635
0
  }
13636
0
    }
13637
#if defined (OBJ_COFF) && defined (TE_PE)
13638
  else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
13639
    {
13640
      fixP->fx_done = 0;
13641
      /* Remember value for tc_gen_reloc.  */
13642
      fixP->fx_addnumber = value;
13643
      /* Clear out the frag for now.  */
13644
      value = 0;
13645
    }
13646
#endif
13647
0
  else if (use_rela_relocations)
13648
0
    {
13649
0
      if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
13650
0
  fixP->fx_no_overflow = 1;
13651
      /* Remember value for tc_gen_reloc.  */
13652
0
      fixP->fx_addnumber = value;
13653
0
      value = 0;
13654
0
    }
13655
13656
0
  md_number_to_chars (p, value, fixP->fx_size);
13657
0
}
13658

13659
const char *
13660
md_atof (int type, char *litP, int *sizeP)
13661
35.3k
{
13662
  /* This outputs the LITTLENUMs in REVERSE order;
13663
     in accord with the bigendian 386.  */
13664
35.3k
  return ieee_md_atof (type, litP, sizeP, false);
13665
35.3k
}
13666

13667
static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
13668
13669
static char *
13670
output_invalid (int c)
13671
468k
{
13672
468k
  if (ISPRINT (c))
13673
108k
    snprintf (output_invalid_buf, sizeof (output_invalid_buf),
13674
108k
        "'%c'", c);
13675
360k
  else
13676
360k
    snprintf (output_invalid_buf, sizeof (output_invalid_buf),
13677
360k
        "(0x%x)", (unsigned char) c);
13678
468k
  return output_invalid_buf;
13679
468k
}
13680
13681
/* Verify that @r can be used in the current context.  */
13682
13683
static bool check_register (const reg_entry *r)
13684
4.23k
{
13685
4.23k
  if (allow_pseudo_reg)
13686
2
    return true;
13687
13688
4.23k
  if (operand_type_all_zero (&r->reg_type))
13689
3
    return false;
13690
13691
4.23k
  if ((r->reg_type.bitfield.dword
13692
4.23k
       || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
13693
4.23k
       || r->reg_type.bitfield.class == RegCR
13694
4.23k
       || r->reg_type.bitfield.class == RegDR)
13695
4.23k
      && !cpu_arch_flags.bitfield.cpui386)
13696
0
    return false;
13697
13698
4.23k
  if (r->reg_type.bitfield.class == RegTR
13699
4.23k
      && (flag_code == CODE_64BIT
13700
115
    || !cpu_arch_flags.bitfield.cpui386
13701
115
    || cpu_arch_isa_flags.bitfield.cpui586
13702
115
    || cpu_arch_isa_flags.bitfield.cpui686))
13703
115
    return false;
13704
13705
4.11k
  if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
13706
0
    return false;
13707
13708
4.11k
  if (!cpu_arch_flags.bitfield.cpuavx512f)
13709
3.69k
    {
13710
3.69k
      if (r->reg_type.bitfield.zmmword
13711
3.69k
    || r->reg_type.bitfield.class == RegMask)
13712
2
  return false;
13713
13714
3.69k
      if (!cpu_arch_flags.bitfield.cpuavx)
13715
0
  {
13716
0
    if (r->reg_type.bitfield.ymmword)
13717
0
      return false;
13718
13719
0
    if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
13720
0
      return false;
13721
0
  }
13722
3.69k
    }
13723
13724
4.11k
  if (r->reg_type.bitfield.tmmword
13725
4.11k
      && (!cpu_arch_flags.bitfield.cpuamx_tile
13726
218
          || flag_code != CODE_64BIT))
13727
192
    return false;
13728
13729
3.92k
  if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
13730
0
    return false;
13731
13732
  /* Don't allow fake index register unless allow_index_reg isn't 0. */
13733
3.92k
  if (!allow_index_reg && r->reg_num == RegIZ)
13734
0
    return false;
13735
13736
  /* Upper 16 vector registers are only available with VREX in 64bit
13737
     mode, and require EVEX encoding.  */
13738
3.92k
  if (r->reg_flags & RegVRex)
13739
0
    {
13740
0
      if (!cpu_arch_flags.bitfield.cpuavx512f
13741
0
    || flag_code != CODE_64BIT)
13742
0
  return false;
13743
13744
0
      if (i.vec_encoding == vex_encoding_default)
13745
0
  i.vec_encoding = vex_encoding_evex;
13746
0
      else if (i.vec_encoding != vex_encoding_evex)
13747
0
  i.vec_encoding = vex_encoding_error;
13748
0
    }
13749
13750
3.92k
  if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
13751
3.92k
      && (!cpu_arch_flags.bitfield.cpulm
13752
1.71k
    || r->reg_type.bitfield.class != RegCR
13753
1.71k
    || dot_insn ())
13754
3.92k
      && flag_code != CODE_64BIT)
13755
1.45k
    return false;
13756
13757
2.46k
  if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
13758
2.46k
      && !intel_syntax)
13759
0
    return false;
13760
13761
2.46k
  return true;
13762
2.46k
}
13763
13764
/* REG_STRING starts *before* REGISTER_PREFIX.  */
13765
13766
static const reg_entry *
13767
parse_real_register (const char *reg_string, char **end_op)
13768
34.5k
{
13769
34.5k
  const char *s = reg_string;
13770
34.5k
  char *p;
13771
34.5k
  char reg_name_given[MAX_REG_NAME_SIZE + 1];
13772
34.5k
  const reg_entry *r;
13773
13774
  /* Skip possible REGISTER_PREFIX and possible whitespace.  */
13775
34.5k
  if (*s == REGISTER_PREFIX)
13776
34.5k
    ++s;
13777
13778
34.5k
  if (is_space_char (*s))
13779
4
    ++s;
13780
13781
34.5k
  p = reg_name_given;
13782
68.5k
  while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
13783
34.1k
    {
13784
34.1k
      if (p >= reg_name_given + MAX_REG_NAME_SIZE)
13785
223
  return (const reg_entry *) NULL;
13786
33.9k
      s++;
13787
33.9k
    }
13788
13789
34.3k
  if (is_part_of_name (*s))
13790
799
    return (const reg_entry *) NULL;
13791
13792
33.5k
  *end_op = (char *) s;
13793
13794
33.5k
  r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
13795
13796
  /* Handle floating point regs, allowing spaces in the (i) part.  */
13797
33.5k
  if (r == reg_st0)
13798
0
    {
13799
0
      if (!cpu_arch_flags.bitfield.cpu8087
13800
0
    && !cpu_arch_flags.bitfield.cpu287
13801
0
    && !cpu_arch_flags.bitfield.cpu387
13802
0
    && !allow_pseudo_reg)
13803
0
  return (const reg_entry *) NULL;
13804
13805
0
      if (is_space_char (*s))
13806
0
  ++s;
13807
0
      if (*s == '(')
13808
0
  {
13809
0
    ++s;
13810
0
    if (is_space_char (*s))
13811
0
      ++s;
13812
0
    if (*s >= '0' && *s <= '7')
13813
0
      {
13814
0
        int fpr = *s - '0';
13815
0
        ++s;
13816
0
        if (is_space_char (*s))
13817
0
    ++s;
13818
0
        if (*s == ')')
13819
0
    {
13820
0
      *end_op = (char *) s + 1;
13821
0
      know (r[fpr].reg_num == fpr);
13822
0
      return r + fpr;
13823
0
    }
13824
0
      }
13825
    /* We have "%st(" then garbage.  */
13826
0
    return (const reg_entry *) NULL;
13827
0
  }
13828
0
    }
13829
13830
33.5k
  return r && check_register (r) ? r : NULL;
13831
33.5k
}
13832
13833
/* REG_STRING starts *before* REGISTER_PREFIX.  */
13834
13835
static const reg_entry *
13836
parse_register (const char *reg_string, char **end_op)
13837
221k
{
13838
221k
  const reg_entry *r;
13839
13840
221k
  if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
13841
88
    r = parse_real_register (reg_string, end_op);
13842
221k
  else
13843
221k
    r = NULL;
13844
221k
  if (!r)
13845
221k
    {
13846
221k
      char *save = input_line_pointer;
13847
221k
      char *buf = xstrdup (reg_string), *name;
13848
221k
      symbolS *symbolP;
13849
13850
221k
      input_line_pointer = buf;
13851
221k
      get_symbol_name (&name);
13852
221k
      symbolP = symbol_find (name);
13853
221k
      while (symbolP && symbol_equated_p (symbolP))
13854
67
  {
13855
67
    const expressionS *e = symbol_get_value_expression(symbolP);
13856
13857
67
    if (e->X_add_number)
13858
0
      break;
13859
67
    symbolP = e->X_add_symbol;
13860
67
  }
13861
221k
      if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
13862
51
  {
13863
51
    const expressionS *e = symbol_get_value_expression (symbolP);
13864
13865
51
    if (e->X_op == O_register)
13866
0
      {
13867
0
        know (e->X_add_number >= 0
13868
0
        && (valueT) e->X_add_number < i386_regtab_size);
13869
0
        r = i386_regtab + e->X_add_number;
13870
0
        *end_op = (char *) reg_string + (input_line_pointer - buf);
13871
0
      }
13872
51
    if (r && !check_register (r))
13873
0
      {
13874
0
        as_bad (_("register '%s%s' cannot be used here"),
13875
0
          register_prefix, r->reg_name);
13876
0
        r = &bad_reg;
13877
0
      }
13878
51
  }
13879
0
      input_line_pointer = save;
13880
221k
      free (buf);
13881
221k
    }
13882
0
  return r;
13883
221k
}
13884
13885
int
13886
i386_parse_name (char *name, expressionS *e, char *nextcharP)
13887
176k
{
13888
176k
  const reg_entry *r = NULL;
13889
176k
  char *end = input_line_pointer;
13890
13891
  /* We only know the terminating character here.  It being double quote could
13892
     be the closing one of a quoted symbol name, or an opening one from a
13893
     following string (or another quoted symbol name).  Since the latter can't
13894
     be valid syntax for anything, bailing in either case is good enough.  */
13895
176k
  if (*nextcharP == '"')
13896
12.9k
    return 0;
13897
13898
163k
  *end = *nextcharP;
13899
163k
  if (*name == REGISTER_PREFIX || allow_naked_reg)
13900
2
    r = parse_real_register (name, &input_line_pointer);
13901
163k
  if (r && end <= input_line_pointer)
13902
2
    {
13903
2
      *nextcharP = *input_line_pointer;
13904
2
      *input_line_pointer = 0;
13905
2
      e->X_op = O_register;
13906
2
      e->X_add_number = r - i386_regtab;
13907
2
      return 1;
13908
2
    }
13909
163k
  input_line_pointer = end;
13910
163k
  *end = 0;
13911
163k
  return intel_syntax ? i386_intel_parse_name (name, e) : 0;
13912
163k
}
13913
13914
void
13915
md_operand (expressionS *e)
13916
101k
{
13917
101k
  char *end;
13918
101k
  const reg_entry *r;
13919
13920
101k
  switch (*input_line_pointer)
13921
101k
    {
13922
34.5k
    case REGISTER_PREFIX:
13923
34.5k
      r = parse_real_register (input_line_pointer, &end);
13924
34.5k
      if (r)
13925
2.39k
  {
13926
2.39k
    e->X_op = O_register;
13927
2.39k
    e->X_add_number = r - i386_regtab;
13928
2.39k
    input_line_pointer = end;
13929
2.39k
  }
13930
34.5k
      break;
13931
13932
0
    case '[':
13933
0
      gas_assert (intel_syntax);
13934
0
      end = input_line_pointer++;
13935
0
      expression (e);
13936
0
      if (*input_line_pointer == ']')
13937
0
  {
13938
0
    ++input_line_pointer;
13939
0
    e->X_op_symbol = make_expr_symbol (e);
13940
0
    e->X_add_symbol = NULL;
13941
0
    e->X_add_number = 0;
13942
0
    e->X_op = O_index;
13943
0
  }
13944
0
      else
13945
0
  {
13946
0
    e->X_op = O_absent;
13947
0
    input_line_pointer = end;
13948
0
  }
13949
0
      break;
13950
101k
    }
13951
101k
}
13952
13953
#ifdef BFD64
13954
/* To maintain consistency with !BFD64 builds of gas record, whether any
13955
   (binary) operator was involved in an expression.  As expressions are
13956
   evaluated in only 32 bits when !BFD64, we use this to decide whether to
13957
   truncate results.  */
13958
bool i386_record_operator (operatorT op,
13959
         const expressionS *left,
13960
         const expressionS *right)
13961
382k
{
13962
382k
  if (op == O_absent)
13963
57.3k
    return false;
13964
13965
325k
  if (!left)
13966
155k
    {
13967
      /* Since the expression parser applies unary operators fine to bignum
13968
   operands, we don't need to be concerned of respective operands not
13969
   fitting in 32 bits.  */
13970
155k
      if (right->X_op == O_constant && right->X_unsigned
13971
155k
    && !fits_in_unsigned_long (right->X_add_number))
13972
2.45k
  return false;
13973
155k
    }
13974
  /* This isn't entirely right: The pattern can also result when constant
13975
     expressions are folded (e.g. 0xffffffff + 1).  */
13976
169k
  else if ((left->X_op == O_constant && left->X_unsigned
13977
169k
      && !fits_in_unsigned_long (left->X_add_number))
13978
169k
     || (right->X_op == O_constant && right->X_unsigned
13979
159k
         && !fits_in_unsigned_long (right->X_add_number)))
13980
22.7k
    expr_mode = expr_large_value;
13981
13982
322k
  if (expr_mode != expr_large_value)
13983
240k
    expr_mode = expr_operator_present;
13984
13985
322k
  return false;
13986
325k
}
13987
#endif
13988

13989
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
13990
const char *md_shortopts = "kVQ:sqnO::";
13991
#else
13992
const char *md_shortopts = "qnO::";
13993
#endif
13994
13995
0
#define OPTION_32 (OPTION_MD_BASE + 0)
13996
0
#define OPTION_64 (OPTION_MD_BASE + 1)
13997
0
#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
13998
0
#define OPTION_MARCH (OPTION_MD_BASE + 3)
13999
0
#define OPTION_MTUNE (OPTION_MD_BASE + 4)
14000
0
#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
14001
0
#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
14002
0
#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
14003
0
#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
14004
0
#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
14005
0
#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
14006
0
#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
14007
0
#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
14008
0
#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
14009
0
#define OPTION_X32 (OPTION_MD_BASE + 14)
14010
0
#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
14011
0
#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
14012
0
#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
14013
#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
14014
0
#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
14015
0
#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
14016
0
#define OPTION_MSHARED (OPTION_MD_BASE + 21)
14017
0
#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
14018
0
#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
14019
0
#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
14020
0
#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
14021
0
#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
14022
0
#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
14023
0
#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
14024
0
#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
14025
0
#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
14026
0
#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
14027
0
#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
14028
0
#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
14029
0
#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
14030
14031
struct option md_longopts[] =
14032
{
14033
  {"32", no_argument, NULL, OPTION_32},
14034
#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
14035
     || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
14036
  {"64", no_argument, NULL, OPTION_64},
14037
#endif
14038
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14039
  {"x32", no_argument, NULL, OPTION_X32},
14040
  {"mshared", no_argument, NULL, OPTION_MSHARED},
14041
  {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
14042
#endif
14043
  {"divide", no_argument, NULL, OPTION_DIVIDE},
14044
  {"march", required_argument, NULL, OPTION_MARCH},
14045
  {"mtune", required_argument, NULL, OPTION_MTUNE},
14046
  {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
14047
  {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
14048
  {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
14049
  {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
14050
  {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
14051
  {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
14052
  {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
14053
  {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
14054
  {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
14055
  {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
14056
  {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
14057
  {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
14058
  {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
14059
# if defined (TE_PE) || defined (TE_PEP)
14060
  {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
14061
#endif
14062
  {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
14063
  {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
14064
  {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
14065
  {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
14066
  {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
14067
  {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
14068
  {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
14069
  {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
14070
  {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
14071
  {"mlfence-before-indirect-branch", required_argument, NULL,
14072
   OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
14073
  {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
14074
  {"mamd64", no_argument, NULL, OPTION_MAMD64},
14075
  {"mintel64", no_argument, NULL, OPTION_MINTEL64},
14076
  {NULL, no_argument, NULL, 0}
14077
};
14078
size_t md_longopts_size = sizeof (md_longopts);
14079
14080
int
14081
md_parse_option (int c, const char *arg)
14082
0
{
14083
0
  unsigned int j;
14084
0
  char *arch, *next, *saved, *type;
14085
14086
0
  switch (c)
14087
0
    {
14088
0
    case 'n':
14089
0
      optimize_align_code = 0;
14090
0
      break;
14091
14092
0
    case 'q':
14093
0
      quiet_warnings = 1;
14094
0
      break;
14095
14096
0
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14097
      /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
14098
   should be emitted or not.  FIXME: Not implemented.  */
14099
0
    case 'Q':
14100
0
      if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
14101
0
  return 0;
14102
0
      break;
14103
14104
      /* -V: SVR4 argument to print version ID.  */
14105
0
    case 'V':
14106
0
      print_version_id ();
14107
0
      break;
14108
14109
      /* -k: Ignore for FreeBSD compatibility.  */
14110
0
    case 'k':
14111
0
      break;
14112
14113
0
    case 's':
14114
      /* -s: On i386 Solaris, this tells the native assembler to use
14115
   .stab instead of .stab.excl.  We always use .stab anyhow.  */
14116
0
      break;
14117
14118
0
    case OPTION_MSHARED:
14119
0
      shared = 1;
14120
0
      break;
14121
14122
0
    case OPTION_X86_USED_NOTE:
14123
0
      if (strcasecmp (arg, "yes") == 0)
14124
0
        x86_used_note = 1;
14125
0
      else if (strcasecmp (arg, "no") == 0)
14126
0
        x86_used_note = 0;
14127
0
      else
14128
0
        as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
14129
0
      break;
14130
14131
14132
0
#endif
14133
0
#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
14134
0
     || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
14135
0
    case OPTION_64:
14136
0
      {
14137
0
  const char **list, **l;
14138
14139
0
  list = bfd_target_list ();
14140
0
  for (l = list; *l != NULL; l++)
14141
0
    if (startswith (*l, "elf64-x86-64")
14142
0
        || strcmp (*l, "coff-x86-64") == 0
14143
0
        || strcmp (*l, "pe-x86-64") == 0
14144
0
        || strcmp (*l, "pei-x86-64") == 0
14145
0
        || strcmp (*l, "mach-o-x86-64") == 0)
14146
0
      {
14147
0
        default_arch = "x86_64";
14148
0
        break;
14149
0
      }
14150
0
  if (*l == NULL)
14151
0
    as_fatal (_("no compiled in support for x86_64"));
14152
0
  free (list);
14153
0
      }
14154
0
      break;
14155
0
#endif
14156
14157
0
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14158
0
    case OPTION_X32:
14159
0
      if (IS_ELF)
14160
0
  {
14161
0
    const char **list, **l;
14162
14163
0
    list = bfd_target_list ();
14164
0
    for (l = list; *l != NULL; l++)
14165
0
      if (startswith (*l, "elf32-x86-64"))
14166
0
        {
14167
0
    default_arch = "x86_64:32";
14168
0
    break;
14169
0
        }
14170
0
    if (*l == NULL)
14171
0
      as_fatal (_("no compiled in support for 32bit x86_64"));
14172
0
    free (list);
14173
0
  }
14174
0
      else
14175
0
  as_fatal (_("32bit x86_64 is only supported for ELF"));
14176
0
      break;
14177
0
#endif
14178
14179
0
    case OPTION_32:
14180
0
      {
14181
0
  const char **list, **l;
14182
14183
0
  list = bfd_target_list ();
14184
0
  for (l = list; *l != NULL; l++)
14185
0
    if (strstr (*l, "-i386")
14186
0
        || strstr (*l, "-go32"))
14187
0
      {
14188
0
        default_arch = "i386";
14189
0
        break;
14190
0
      }
14191
0
  if (*l == NULL)
14192
0
    as_fatal (_("no compiled in support for ix86"));
14193
0
  free (list);
14194
0
      }
14195
0
      break;
14196
14197
0
    case OPTION_DIVIDE:
14198
#ifdef SVR4_COMMENT_CHARS
14199
      {
14200
  char *n, *t;
14201
  const char *s;
14202
14203
  n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
14204
  t = n;
14205
  for (s = i386_comment_chars; *s != '\0'; s++)
14206
    if (*s != '/')
14207
      *t++ = *s;
14208
  *t = '\0';
14209
  i386_comment_chars = n;
14210
      }
14211
#endif
14212
0
      break;
14213
14214
0
    case OPTION_MARCH:
14215
0
      saved = xstrdup (arg);
14216
0
      arch = saved;
14217
      /* Allow -march=+nosse.  */
14218
0
      if (*arch == '+')
14219
0
  arch++;
14220
0
      do
14221
0
  {
14222
0
    if (*arch == '.')
14223
0
      as_fatal (_("invalid -march= option: `%s'"), arg);
14224
0
    next = strchr (arch, '+');
14225
0
    if (next)
14226
0
      *next++ = '\0';
14227
0
    for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
14228
0
      {
14229
0
        if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
14230
0
            && strcmp (arch, cpu_arch[j].name) == 0)
14231
0
    {
14232
      /* Processor.  */
14233
0
      if (! cpu_arch[j].enable.bitfield.cpui386)
14234
0
        continue;
14235
14236
0
      cpu_arch_name = cpu_arch[j].name;
14237
0
      free (cpu_sub_arch_name);
14238
0
      cpu_sub_arch_name = NULL;
14239
0
      cpu_arch_flags = cpu_arch[j].enable;
14240
0
      cpu_arch_isa = cpu_arch[j].type;
14241
0
      cpu_arch_isa_flags = cpu_arch[j].enable;
14242
0
      if (!cpu_arch_tune_set)
14243
0
        {
14244
0
          cpu_arch_tune = cpu_arch_isa;
14245
0
          cpu_arch_tune_flags = cpu_arch_isa_flags;
14246
0
        }
14247
0
      break;
14248
0
    }
14249
0
        else if (cpu_arch[j].type == PROCESSOR_NONE
14250
0
           && strcmp (arch, cpu_arch[j].name) == 0
14251
0
           && !cpu_flags_all_zero (&cpu_arch[j].enable))
14252
0
    {
14253
      /* ISA extension.  */
14254
0
      i386_cpu_flags flags;
14255
14256
0
      flags = cpu_flags_or (cpu_arch_flags,
14257
0
          cpu_arch[j].enable);
14258
14259
0
      if (!cpu_flags_equal (&flags, &cpu_arch_flags))
14260
0
        {
14261
0
          extend_cpu_sub_arch_name (arch);
14262
0
          cpu_arch_flags = flags;
14263
0
          cpu_arch_isa_flags = flags;
14264
0
        }
14265
0
      else
14266
0
        cpu_arch_isa_flags
14267
0
          = cpu_flags_or (cpu_arch_isa_flags,
14268
0
              cpu_arch[j].enable);
14269
0
      break;
14270
0
    }
14271
0
      }
14272
14273
0
    if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
14274
0
      {
14275
        /* Disable an ISA extension.  */
14276
0
        for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
14277
0
    if (cpu_arch[j].type == PROCESSOR_NONE
14278
0
        && strcmp (arch + 2, cpu_arch[j].name) == 0)
14279
0
      {
14280
0
        i386_cpu_flags flags;
14281
14282
0
        flags = cpu_flags_and_not (cpu_arch_flags,
14283
0
                 cpu_arch[j].disable);
14284
0
        if (!cpu_flags_equal (&flags, &cpu_arch_flags))
14285
0
          {
14286
0
      extend_cpu_sub_arch_name (arch);
14287
0
      cpu_arch_flags = flags;
14288
0
      cpu_arch_isa_flags = flags;
14289
0
          }
14290
0
        break;
14291
0
      }
14292
0
      }
14293
14294
0
    if (j >= ARRAY_SIZE (cpu_arch))
14295
0
      as_fatal (_("invalid -march= option: `%s'"), arg);
14296
14297
0
    arch = next;
14298
0
  }
14299
0
      while (next != NULL);
14300
0
      free (saved);
14301
0
      break;
14302
14303
0
    case OPTION_MTUNE:
14304
0
      if (*arg == '.')
14305
0
  as_fatal (_("invalid -mtune= option: `%s'"), arg);
14306
0
      for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
14307
0
  {
14308
0
    if (cpu_arch[j].type != PROCESSOR_NONE
14309
0
        && strcmp (arg, cpu_arch[j].name) == 0)
14310
0
      {
14311
0
        cpu_arch_tune_set = 1;
14312
0
        cpu_arch_tune = cpu_arch [j].type;
14313
0
        cpu_arch_tune_flags = cpu_arch[j].enable;
14314
0
        break;
14315
0
      }
14316
0
  }
14317
0
      if (j >= ARRAY_SIZE (cpu_arch))
14318
0
  as_fatal (_("invalid -mtune= option: `%s'"), arg);
14319
0
      break;
14320
14321
0
    case OPTION_MMNEMONIC:
14322
0
      if (strcasecmp (arg, "att") == 0)
14323
0
  intel_mnemonic = 0;
14324
0
      else if (strcasecmp (arg, "intel") == 0)
14325
0
  intel_mnemonic = 1;
14326
0
      else
14327
0
  as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
14328
0
      break;
14329
14330
0
    case OPTION_MSYNTAX:
14331
0
      if (strcasecmp (arg, "att") == 0)
14332
0
  intel_syntax = 0;
14333
0
      else if (strcasecmp (arg, "intel") == 0)
14334
0
  intel_syntax = 1;
14335
0
      else
14336
0
  as_fatal (_("invalid -msyntax= option: `%s'"), arg);
14337
0
      break;
14338
14339
0
    case OPTION_MINDEX_REG:
14340
0
      allow_index_reg = 1;
14341
0
      break;
14342
14343
0
    case OPTION_MNAKED_REG:
14344
0
      allow_naked_reg = 1;
14345
0
      break;
14346
14347
0
    case OPTION_MSSE2AVX:
14348
0
      sse2avx = 1;
14349
0
      break;
14350
14351
0
    case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
14352
0
      use_unaligned_vector_move = 1;
14353
0
      break;
14354
14355
0
    case OPTION_MSSE_CHECK:
14356
0
      if (strcasecmp (arg, "error") == 0)
14357
0
  sse_check = check_error;
14358
0
      else if (strcasecmp (arg, "warning") == 0)
14359
0
  sse_check = check_warning;
14360
0
      else if (strcasecmp (arg, "none") == 0)
14361
0
  sse_check = check_none;
14362
0
      else
14363
0
  as_fatal (_("invalid -msse-check= option: `%s'"), arg);
14364
0
      break;
14365
14366
0
    case OPTION_MOPERAND_CHECK:
14367
0
      if (strcasecmp (arg, "error") == 0)
14368
0
  operand_check = check_error;
14369
0
      else if (strcasecmp (arg, "warning") == 0)
14370
0
  operand_check = check_warning;
14371
0
      else if (strcasecmp (arg, "none") == 0)
14372
0
  operand_check = check_none;
14373
0
      else
14374
0
  as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
14375
0
      break;
14376
14377
0
    case OPTION_MAVXSCALAR:
14378
0
      if (strcasecmp (arg, "128") == 0)
14379
0
  avxscalar = vex128;
14380
0
      else if (strcasecmp (arg, "256") == 0)
14381
0
  avxscalar = vex256;
14382
0
      else
14383
0
  as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
14384
0
      break;
14385
14386
0
    case OPTION_MVEXWIG:
14387
0
      if (strcmp (arg, "0") == 0)
14388
0
  vexwig = vexw0;
14389
0
      else if (strcmp (arg, "1") == 0)
14390
0
  vexwig = vexw1;
14391
0
      else
14392
0
  as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
14393
0
      break;
14394
14395
0
    case OPTION_MADD_BND_PREFIX:
14396
0
      add_bnd_prefix = 1;
14397
0
      break;
14398
14399
0
    case OPTION_MEVEXLIG:
14400
0
      if (strcmp (arg, "128") == 0)
14401
0
  evexlig = evexl128;
14402
0
      else if (strcmp (arg, "256") == 0)
14403
0
  evexlig = evexl256;
14404
0
      else  if (strcmp (arg, "512") == 0)
14405
0
  evexlig = evexl512;
14406
0
      else
14407
0
  as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
14408
0
      break;
14409
14410
0
    case OPTION_MEVEXRCIG:
14411
0
      if (strcmp (arg, "rne") == 0)
14412
0
  evexrcig = rne;
14413
0
      else if (strcmp (arg, "rd") == 0)
14414
0
  evexrcig = rd;
14415
0
      else if (strcmp (arg, "ru") == 0)
14416
0
  evexrcig = ru;
14417
0
      else if (strcmp (arg, "rz") == 0)
14418
0
  evexrcig = rz;
14419
0
      else
14420
0
  as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
14421
0
      break;
14422
14423
0
    case OPTION_MEVEXWIG:
14424
0
      if (strcmp (arg, "0") == 0)
14425
0
  evexwig = evexw0;
14426
0
      else if (strcmp (arg, "1") == 0)
14427
0
  evexwig = evexw1;
14428
0
      else
14429
0
  as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
14430
0
      break;
14431
14432
# if defined (TE_PE) || defined (TE_PEP)
14433
    case OPTION_MBIG_OBJ:
14434
      use_big_obj = 1;
14435
      break;
14436
#endif
14437
14438
0
    case OPTION_MOMIT_LOCK_PREFIX:
14439
0
      if (strcasecmp (arg, "yes") == 0)
14440
0
        omit_lock_prefix = 1;
14441
0
      else if (strcasecmp (arg, "no") == 0)
14442
0
        omit_lock_prefix = 0;
14443
0
      else
14444
0
        as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
14445
0
      break;
14446
14447
0
    case OPTION_MFENCE_AS_LOCK_ADD:
14448
0
      if (strcasecmp (arg, "yes") == 0)
14449
0
        avoid_fence = 1;
14450
0
      else if (strcasecmp (arg, "no") == 0)
14451
0
        avoid_fence = 0;
14452
0
      else
14453
0
        as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
14454
0
      break;
14455
14456
0
    case OPTION_MLFENCE_AFTER_LOAD:
14457
0
      if (strcasecmp (arg, "yes") == 0)
14458
0
  lfence_after_load = 1;
14459
0
      else if (strcasecmp (arg, "no") == 0)
14460
0
  lfence_after_load = 0;
14461
0
      else
14462
0
        as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
14463
0
      break;
14464
14465
0
    case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
14466
0
      if (strcasecmp (arg, "all") == 0)
14467
0
  {
14468
0
    lfence_before_indirect_branch = lfence_branch_all;
14469
0
    if (lfence_before_ret == lfence_before_ret_none)
14470
0
      lfence_before_ret = lfence_before_ret_shl;
14471
0
  }
14472
0
      else if (strcasecmp (arg, "memory") == 0)
14473
0
  lfence_before_indirect_branch = lfence_branch_memory;
14474
0
      else if (strcasecmp (arg, "register") == 0)
14475
0
  lfence_before_indirect_branch = lfence_branch_register;
14476
0
      else if (strcasecmp (arg, "none") == 0)
14477
0
  lfence_before_indirect_branch = lfence_branch_none;
14478
0
      else
14479
0
        as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
14480
0
      arg);
14481
0
      break;
14482
14483
0
    case OPTION_MLFENCE_BEFORE_RET:
14484
0
      if (strcasecmp (arg, "or") == 0)
14485
0
  lfence_before_ret = lfence_before_ret_or;
14486
0
      else if (strcasecmp (arg, "not") == 0)
14487
0
  lfence_before_ret = lfence_before_ret_not;
14488
0
      else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
14489
0
  lfence_before_ret = lfence_before_ret_shl;
14490
0
      else if (strcasecmp (arg, "none") == 0)
14491
0
  lfence_before_ret = lfence_before_ret_none;
14492
0
      else
14493
0
        as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
14494
0
      arg);
14495
0
      break;
14496
14497
0
    case OPTION_MRELAX_RELOCATIONS:
14498
0
      if (strcasecmp (arg, "yes") == 0)
14499
0
        generate_relax_relocations = 1;
14500
0
      else if (strcasecmp (arg, "no") == 0)
14501
0
        generate_relax_relocations = 0;
14502
0
      else
14503
0
        as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
14504
0
      break;
14505
14506
0
    case OPTION_MALIGN_BRANCH_BOUNDARY:
14507
0
      {
14508
0
  char *end;
14509
0
  long int align = strtoul (arg, &end, 0);
14510
0
  if (*end == '\0')
14511
0
    {
14512
0
      if (align == 0)
14513
0
        {
14514
0
    align_branch_power = 0;
14515
0
    break;
14516
0
        }
14517
0
      else if (align >= 16)
14518
0
        {
14519
0
    int align_power;
14520
0
    for (align_power = 0;
14521
0
         (align & 1) == 0;
14522
0
         align >>= 1, align_power++)
14523
0
      continue;
14524
    /* Limit alignment power to 31.  */
14525
0
    if (align == 1 && align_power < 32)
14526
0
      {
14527
0
        align_branch_power = align_power;
14528
0
        break;
14529
0
      }
14530
0
        }
14531
0
    }
14532
0
  as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
14533
0
      }
14534
0
      break;
14535
14536
0
    case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
14537
0
      {
14538
0
  char *end;
14539
0
  int align = strtoul (arg, &end, 0);
14540
  /* Some processors only support 5 prefixes.  */
14541
0
  if (*end == '\0' && align >= 0 && align < 6)
14542
0
    {
14543
0
      align_branch_prefix_size = align;
14544
0
      break;
14545
0
    }
14546
0
  as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
14547
0
      arg);
14548
0
      }
14549
0
      break;
14550
14551
0
    case OPTION_MALIGN_BRANCH:
14552
0
      align_branch = 0;
14553
0
      saved = xstrdup (arg);
14554
0
      type = saved;
14555
0
      do
14556
0
  {
14557
0
    next = strchr (type, '+');
14558
0
    if (next)
14559
0
      *next++ = '\0';
14560
0
    if (strcasecmp (type, "jcc") == 0)
14561
0
      align_branch |= align_branch_jcc_bit;
14562
0
    else if (strcasecmp (type, "fused") == 0)
14563
0
      align_branch |= align_branch_fused_bit;
14564
0
    else if (strcasecmp (type, "jmp") == 0)
14565
0
      align_branch |= align_branch_jmp_bit;
14566
0
    else if (strcasecmp (type, "call") == 0)
14567
0
      align_branch |= align_branch_call_bit;
14568
0
    else if (strcasecmp (type, "ret") == 0)
14569
0
      align_branch |= align_branch_ret_bit;
14570
0
    else if (strcasecmp (type, "indirect") == 0)
14571
0
      align_branch |= align_branch_indirect_bit;
14572
0
    else
14573
0
      as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
14574
0
    type = next;
14575
0
  }
14576
0
      while (next != NULL);
14577
0
      free (saved);
14578
0
      break;
14579
14580
0
    case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
14581
0
      align_branch_power = 5;
14582
0
      align_branch_prefix_size = 5;
14583
0
      align_branch = (align_branch_jcc_bit
14584
0
          | align_branch_fused_bit
14585
0
          | align_branch_jmp_bit);
14586
0
      break;
14587
14588
0
    case OPTION_MAMD64:
14589
0
      isa64 = amd64;
14590
0
      break;
14591
14592
0
    case OPTION_MINTEL64:
14593
0
      isa64 = intel64;
14594
0
      break;
14595
14596
0
    case 'O':
14597
0
      if (arg == NULL)
14598
0
  {
14599
0
    optimize = 1;
14600
    /* Turn off -Os.  */
14601
0
    optimize_for_space = 0;
14602
0
  }
14603
0
      else if (*arg == 's')
14604
0
  {
14605
0
    optimize_for_space = 1;
14606
    /* Turn on all encoding optimizations.  */
14607
0
    optimize = INT_MAX;
14608
0
  }
14609
0
      else
14610
0
  {
14611
0
    optimize = atoi (arg);
14612
    /* Turn off -Os.  */
14613
0
    optimize_for_space = 0;
14614
0
  }
14615
0
      break;
14616
14617
0
    default:
14618
0
      return 0;
14619
0
    }
14620
0
  return 1;
14621
0
}
14622
14623
0
#define MESSAGE_TEMPLATE \
14624
0
"                                                                                "
14625
14626
static char *
14627
output_message (FILE *stream, char *p, char *message, char *start,
14628
    int *left_p, const char *name, int len)
14629
0
{
14630
0
  int size = sizeof (MESSAGE_TEMPLATE);
14631
0
  int left = *left_p;
14632
14633
  /* Reserve 2 spaces for ", " or ",\0" */
14634
0
  left -= len + 2;
14635
14636
  /* Check if there is any room.  */
14637
0
  if (left >= 0)
14638
0
    {
14639
0
      if (p != start)
14640
0
  {
14641
0
    *p++ = ',';
14642
0
    *p++ = ' ';
14643
0
  }
14644
0
      p = mempcpy (p, name, len);
14645
0
    }
14646
0
  else
14647
0
    {
14648
      /* Output the current message now and start a new one.  */
14649
0
      *p++ = ',';
14650
0
      *p = '\0';
14651
0
      fprintf (stream, "%s\n", message);
14652
0
      p = start;
14653
0
      left = size - (start - message) - len - 2;
14654
14655
0
      gas_assert (left >= 0);
14656
14657
0
      p = mempcpy (p, name, len);
14658
0
    }
14659
14660
0
  *left_p = left;
14661
0
  return p;
14662
0
}
14663
14664
static void
14665
show_arch (FILE *stream, int ext, int check)
14666
0
{
14667
0
  static char message[] = MESSAGE_TEMPLATE;
14668
0
  char *start = message + 27;
14669
0
  char *p;
14670
0
  int size = sizeof (MESSAGE_TEMPLATE);
14671
0
  int left;
14672
0
  const char *name;
14673
0
  int len;
14674
0
  unsigned int j;
14675
14676
0
  p = start;
14677
0
  left = size - (start - message);
14678
14679
0
  if (!ext && check)
14680
0
    {
14681
0
      p = output_message (stream, p, message, start, &left,
14682
0
        STRING_COMMA_LEN ("default"));
14683
0
      p = output_message (stream, p, message, start, &left,
14684
0
        STRING_COMMA_LEN ("push"));
14685
0
      p = output_message (stream, p, message, start, &left,
14686
0
        STRING_COMMA_LEN ("pop"));
14687
0
    }
14688
14689
0
  for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
14690
0
    {
14691
      /* Should it be skipped?  */
14692
0
      if (cpu_arch [j].skip)
14693
0
  continue;
14694
14695
0
      name = cpu_arch [j].name;
14696
0
      len = cpu_arch [j].len;
14697
0
      if (cpu_arch[j].type == PROCESSOR_NONE)
14698
0
  {
14699
    /* It is an extension.  Skip if we aren't asked to show it.  */
14700
0
    if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
14701
0
      continue;
14702
0
  }
14703
0
      else if (ext)
14704
0
  {
14705
    /* It is an processor.  Skip if we show only extension.  */
14706
0
    continue;
14707
0
  }
14708
0
      else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
14709
0
  {
14710
    /* It is an impossible processor - skip.  */
14711
0
    continue;
14712
0
  }
14713
14714
0
      p = output_message (stream, p, message, start, &left, name, len);
14715
0
    }
14716
14717
  /* Display disabled extensions.  */
14718
0
  if (ext)
14719
0
    for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
14720
0
      {
14721
0
  char *str;
14722
14723
0
  if (cpu_arch[j].type != PROCESSOR_NONE
14724
0
      || !cpu_flags_all_zero (&cpu_arch[j].enable))
14725
0
    continue;
14726
0
  str = xasprintf ("no%s", cpu_arch[j].name);
14727
0
  p = output_message (stream, p, message, start, &left, str,
14728
0
          strlen (str));
14729
0
  free (str);
14730
0
      }
14731
14732
0
  *p = '\0';
14733
0
  fprintf (stream, "%s\n", message);
14734
0
}
14735
14736
void
14737
md_show_usage (FILE *stream)
14738
0
{
14739
0
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14740
0
  fprintf (stream, _("\
14741
0
  -Qy, -Qn                ignored\n\
14742
0
  -V                      print assembler version number\n\
14743
0
  -k                      ignored\n"));
14744
0
#endif
14745
0
  fprintf (stream, _("\
14746
0
  -n                      do not optimize code alignment\n\
14747
0
  -O{012s}                attempt some code optimizations\n\
14748
0
  -q                      quieten some warnings\n"));
14749
0
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14750
0
  fprintf (stream, _("\
14751
0
  -s                      ignored\n"));
14752
0
#endif
14753
0
#ifdef BFD64
14754
0
# if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14755
0
  fprintf (stream, _("\
14756
0
  --32/--64/--x32         generate 32bit/64bit/x32 object\n"));
14757
# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
14758
  fprintf (stream, _("\
14759
  --32/--64               generate 32bit/64bit object\n"));
14760
# endif
14761
0
#endif
14762
#ifdef SVR4_COMMENT_CHARS
14763
  fprintf (stream, _("\
14764
  --divide                do not treat `/' as a comment character\n"));
14765
#else
14766
0
  fprintf (stream, _("\
14767
0
  --divide                ignored\n"));
14768
0
#endif
14769
0
  fprintf (stream, _("\
14770
0
  -march=CPU[,+EXTENSION...]\n\
14771
0
                          generate code for CPU and EXTENSION, CPU is one of:\n"));
14772
0
  show_arch (stream, 0, 1);
14773
0
  fprintf (stream, _("\
14774
0
                          EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
14775
0
  show_arch (stream, 1, 0);
14776
0
  fprintf (stream, _("\
14777
0
  -mtune=CPU              optimize for CPU, CPU is one of:\n"));
14778
0
  show_arch (stream, 0, 0);
14779
0
  fprintf (stream, _("\
14780
0
  -msse2avx               encode SSE instructions with VEX prefix\n"));
14781
0
  fprintf (stream, _("\
14782
0
  -muse-unaligned-vector-move\n\
14783
0
                          encode aligned vector move as unaligned vector move\n"));
14784
0
  fprintf (stream, _("\
14785
0
  -msse-check=[none|error|warning] (default: warning)\n\
14786
0
                          check SSE instructions\n"));
14787
0
  fprintf (stream, _("\
14788
0
  -moperand-check=[none|error|warning] (default: warning)\n\
14789
0
                          check operand combinations for validity\n"));
14790
0
  fprintf (stream, _("\
14791
0
  -mavxscalar=[128|256] (default: 128)\n\
14792
0
                          encode scalar AVX instructions with specific vector\n\
14793
0
                           length\n"));
14794
0
  fprintf (stream, _("\
14795
0
  -mvexwig=[0|1] (default: 0)\n\
14796
0
                          encode VEX instructions with specific VEX.W value\n\
14797
0
                           for VEX.W bit ignored instructions\n"));
14798
0
  fprintf (stream, _("\
14799
0
  -mevexlig=[128|256|512] (default: 128)\n\
14800
0
                          encode scalar EVEX instructions with specific vector\n\
14801
0
                           length\n"));
14802
0
  fprintf (stream, _("\
14803
0
  -mevexwig=[0|1] (default: 0)\n\
14804
0
                          encode EVEX instructions with specific EVEX.W value\n\
14805
0
                           for EVEX.W bit ignored instructions\n"));
14806
0
  fprintf (stream, _("\
14807
0
  -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
14808
0
                          encode EVEX instructions with specific EVEX.RC value\n\
14809
0
                           for SAE-only ignored instructions\n"));
14810
0
  fprintf (stream, _("\
14811
0
  -mmnemonic=[att|intel] "));
14812
0
  if (SYSV386_COMPAT)
14813
0
    fprintf (stream, _("(default: att)\n"));
14814
0
  else
14815
0
    fprintf (stream, _("(default: intel)\n"));
14816
0
  fprintf (stream, _("\
14817
0
                          use AT&T/Intel mnemonic\n"));
14818
0
  fprintf (stream, _("\
14819
0
  -msyntax=[att|intel] (default: att)\n\
14820
0
                          use AT&T/Intel syntax\n"));
14821
0
  fprintf (stream, _("\
14822
0
  -mindex-reg             support pseudo index registers\n"));
14823
0
  fprintf (stream, _("\
14824
0
  -mnaked-reg             don't require `%%' prefix for registers\n"));
14825
0
  fprintf (stream, _("\
14826
0
  -madd-bnd-prefix        add BND prefix for all valid branches\n"));
14827
0
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
14828
0
  fprintf (stream, _("\
14829
0
  -mshared                disable branch optimization for shared code\n"));
14830
0
  fprintf (stream, _("\
14831
0
  -mx86-used-note=[no|yes] "));
14832
0
  if (DEFAULT_X86_USED_NOTE)
14833
0
    fprintf (stream, _("(default: yes)\n"));
14834
0
  else
14835
0
    fprintf (stream, _("(default: no)\n"));
14836
0
  fprintf (stream, _("\
14837
0
                          generate x86 used ISA and feature properties\n"));
14838
0
#endif
14839
#if defined (TE_PE) || defined (TE_PEP)
14840
  fprintf (stream, _("\
14841
  -mbig-obj               generate big object files\n"));
14842
#endif
14843
0
  fprintf (stream, _("\
14844
0
  -momit-lock-prefix=[no|yes] (default: no)\n\
14845
0
                          strip all lock prefixes\n"));
14846
0
  fprintf (stream, _("\
14847
0
  -mfence-as-lock-add=[no|yes] (default: no)\n\
14848
0
                          encode lfence, mfence and sfence as\n\
14849
0
                           lock addl $0x0, (%%{re}sp)\n"));
14850
0
  fprintf (stream, _("\
14851
0
  -mrelax-relocations=[no|yes] "));
14852
0
  if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
14853
0
    fprintf (stream, _("(default: yes)\n"));
14854
0
  else
14855
0
    fprintf (stream, _("(default: no)\n"));
14856
0
  fprintf (stream, _("\
14857
0
                          generate relax relocations\n"));
14858
0
  fprintf (stream, _("\
14859
0
  -malign-branch-boundary=NUM (default: 0)\n\
14860
0
                          align branches within NUM byte boundary\n"));
14861
0
  fprintf (stream, _("\
14862
0
  -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
14863
0
                          TYPE is combination of jcc, fused, jmp, call, ret,\n\
14864
0
                           indirect\n\
14865
0
                          specify types of branches to align\n"));
14866
0
  fprintf (stream, _("\
14867
0
  -malign-branch-prefix-size=NUM (default: 5)\n\
14868
0
                          align branches with NUM prefixes per instruction\n"));
14869
0
  fprintf (stream, _("\
14870
0
  -mbranches-within-32B-boundaries\n\
14871
0
                          align branches within 32 byte boundary\n"));
14872
0
  fprintf (stream, _("\
14873
0
  -mlfence-after-load=[no|yes] (default: no)\n\
14874
0
                          generate lfence after load\n"));
14875
0
  fprintf (stream, _("\
14876
0
  -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
14877
0
                          generate lfence before indirect near branch\n"));
14878
0
  fprintf (stream, _("\
14879
0
  -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
14880
0
                          generate lfence before ret\n"));
14881
0
  fprintf (stream, _("\
14882
0
  -mamd64                 accept only AMD64 ISA [default]\n"));
14883
0
  fprintf (stream, _("\
14884
0
  -mintel64               accept only Intel64 ISA\n"));
14885
0
}
14886
14887
#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
14888
     || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
14889
     || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
14890
14891
/* Pick the target format to use.  */
14892
14893
const char *
14894
i386_target_format (void)
14895
633
{
14896
633
  if (startswith (default_arch, "x86_64"))
14897
633
    {
14898
633
      update_code_flag (CODE_64BIT, 1);
14899
633
      if (default_arch[6] == '\0')
14900
633
  x86_elf_abi = X86_64_ABI;
14901
0
      else
14902
0
  x86_elf_abi = X86_64_X32_ABI;
14903
633
    }
14904
0
  else if (!strcmp (default_arch, "i386"))
14905
0
    update_code_flag (CODE_32BIT, 1);
14906
0
  else if (!strcmp (default_arch, "iamcu"))
14907
0
    {
14908
0
      update_code_flag (CODE_32BIT, 1);
14909
0
      if (cpu_arch_isa == PROCESSOR_UNKNOWN)
14910
0
  {
14911
0
    static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
14912
0
    cpu_arch_name = "iamcu";
14913
0
    free (cpu_sub_arch_name);
14914
0
    cpu_sub_arch_name = NULL;
14915
0
    cpu_arch_flags = iamcu_flags;
14916
0
    cpu_arch_isa = PROCESSOR_IAMCU;
14917
0
    cpu_arch_isa_flags = iamcu_flags;
14918
0
    if (!cpu_arch_tune_set)
14919
0
      {
14920
0
        cpu_arch_tune = cpu_arch_isa;
14921
0
        cpu_arch_tune_flags = cpu_arch_isa_flags;
14922
0
      }
14923
0
  }
14924
0
      else if (cpu_arch_isa != PROCESSOR_IAMCU)
14925
0
  as_fatal (_("Intel MCU doesn't support `%s' architecture"),
14926
0
      cpu_arch_name);
14927
0
    }
14928
0
  else
14929
0
    as_fatal (_("unknown architecture"));
14930
14931
633
  if (cpu_flags_all_zero (&cpu_arch_isa_flags))
14932
1
    cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
14933
633
  if (cpu_flags_all_zero (&cpu_arch_tune_flags))
14934
1
    cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].enable;
14935
14936
633
  switch (OUTPUT_FLAVOR)
14937
633
    {
14938
#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
14939
    case bfd_target_aout_flavour:
14940
      return AOUT_TARGET_FORMAT;
14941
#endif
14942
#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
14943
# if defined (TE_PE) || defined (TE_PEP)
14944
    case bfd_target_coff_flavour:
14945
      if (flag_code == CODE_64BIT)
14946
  {
14947
    object_64bit = 1;
14948
    return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
14949
  }
14950
      return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
14951
# elif defined (TE_GO32)
14952
    case bfd_target_coff_flavour:
14953
      return "coff-go32";
14954
# else
14955
    case bfd_target_coff_flavour:
14956
      return "coff-i386";
14957
# endif
14958
#endif
14959
0
#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
14960
633
    case bfd_target_elf_flavour:
14961
633
      {
14962
633
  const char *format;
14963
14964
633
  switch (x86_elf_abi)
14965
633
    {
14966
0
    default:
14967
0
      format = ELF_TARGET_FORMAT;
14968
0
#ifndef TE_SOLARIS
14969
0
      tls_get_addr = "___tls_get_addr";
14970
0
#endif
14971
0
      break;
14972
633
    case X86_64_ABI:
14973
633
      use_rela_relocations = 1;
14974
633
      object_64bit = 1;
14975
633
#ifndef TE_SOLARIS
14976
633
      tls_get_addr = "__tls_get_addr";
14977
633
#endif
14978
633
      format = ELF_TARGET_FORMAT64;
14979
633
      break;
14980
0
    case X86_64_X32_ABI:
14981
0
      use_rela_relocations = 1;
14982
0
      object_64bit = 1;
14983
0
#ifndef TE_SOLARIS
14984
0
      tls_get_addr = "__tls_get_addr";
14985
0
#endif
14986
0
      disallow_64bit_reloc = 1;
14987
0
      format = ELF_TARGET_FORMAT32;
14988
0
      break;
14989
633
    }
14990
633
  if (cpu_arch_isa == PROCESSOR_IAMCU)
14991
0
    {
14992
0
      if (x86_elf_abi != I386_ABI)
14993
0
        as_fatal (_("Intel MCU is 32bit only"));
14994
0
      return ELF_TARGET_IAMCU_FORMAT;
14995
0
    }
14996
633
  else
14997
633
    return format;
14998
633
      }
14999
0
#endif
15000
#if defined (OBJ_MACH_O)
15001
    case bfd_target_mach_o_flavour:
15002
      if (flag_code == CODE_64BIT)
15003
  {
15004
    use_rela_relocations = 1;
15005
    object_64bit = 1;
15006
    return "mach-o-x86-64";
15007
  }
15008
      else
15009
  return "mach-o-i386";
15010
#endif
15011
0
    default:
15012
0
      abort ();
15013
0
      return NULL;
15014
633
    }
15015
633
}
15016
15017
#endif /* OBJ_MAYBE_ more than one  */
15018

15019
symbolS *
15020
md_undefined_symbol (char *name)
15021
52.5k
{
15022
52.5k
  if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
15023
52.5k
      && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
15024
52.5k
      && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
15025
52.5k
      && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
15026
1
    {
15027
1
      if (!GOT_symbol)
15028
1
  {
15029
1
    if (symbol_find (name))
15030
0
      as_bad (_("GOT already in symbol table"));
15031
1
    GOT_symbol = symbol_new (name, undefined_section,
15032
1
           &zero_address_frag, 0);
15033
1
  };
15034
1
      return GOT_symbol;
15035
1
    }
15036
52.5k
  return 0;
15037
52.5k
}
15038
15039
/* Round up a section size to the appropriate boundary.  */
15040
15041
valueT
15042
md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
15043
0
{
15044
#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
15045
  if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
15046
    {
15047
      /* For a.out, force the section size to be aligned.  If we don't do
15048
   this, BFD will align it for us, but it will not write out the
15049
   final bytes of the section.  This may be a bug in BFD, but it is
15050
   easier to fix it here since that is how the other a.out targets
15051
   work.  */
15052
      int align;
15053
15054
      align = bfd_section_alignment (segment);
15055
      size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
15056
    }
15057
#endif
15058
15059
0
  return size;
15060
0
}
15061
15062
/* On the i386, PC-relative offsets are relative to the start of the
15063
   next instruction.  That is, the address of the offset, plus its
15064
   size, since the offset is always the last part of the insn.  */
15065
15066
long
15067
md_pcrel_from (fixS *fixP)
15068
0
{
15069
0
  return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
15070
0
}
15071
15072
#ifndef I386COFF
15073
15074
static void
15075
s_bss (int ignore ATTRIBUTE_UNUSED)
15076
2.98k
{
15077
2.98k
  int temp;
15078
15079
2.98k
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15080
2.98k
  if (IS_ELF)
15081
2.98k
    obj_elf_section_change_hook ();
15082
2.98k
#endif
15083
2.98k
  temp = get_absolute_expression ();
15084
2.98k
  subseg_set (bss_section, (subsegT) temp);
15085
2.98k
  demand_empty_rest_of_line ();
15086
2.98k
}
15087
15088
#endif
15089
15090
/* Remember constant directive.  */
15091
15092
void
15093
i386_cons_align (int ignore ATTRIBUTE_UNUSED)
15094
55.2k
{
15095
55.2k
  if (last_insn.kind != last_insn_directive
15096
55.2k
      && (bfd_section_flags (now_seg) & SEC_CODE))
15097
4.46k
    {
15098
4.46k
      last_insn.seg = now_seg;
15099
4.46k
      last_insn.kind = last_insn_directive;
15100
4.46k
      last_insn.name = "constant directive";
15101
4.46k
      last_insn.file = as_where (&last_insn.line);
15102
4.46k
      if (lfence_before_ret != lfence_before_ret_none)
15103
0
  {
15104
0
    if (lfence_before_indirect_branch != lfence_branch_none)
15105
0
      as_warn (_("constant directive skips -mlfence-before-ret "
15106
0
           "and -mlfence-before-indirect-branch"));
15107
0
    else
15108
0
      as_warn (_("constant directive skips -mlfence-before-ret"));
15109
0
  }
15110
4.46k
      else if (lfence_before_indirect_branch != lfence_branch_none)
15111
0
  as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
15112
4.46k
    }
15113
55.2k
}
15114
15115
int
15116
i386_validate_fix (fixS *fixp)
15117
0
{
15118
0
  if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
15119
0
    {
15120
0
      reloc_howto_type *howto;
15121
15122
0
      howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
15123
0
      as_bad_where (fixp->fx_file, fixp->fx_line,
15124
0
        _("invalid %s relocation against register"),
15125
0
        howto ? howto->name : "<unknown>");
15126
0
      return 0;
15127
0
    }
15128
15129
0
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15130
0
  if (fixp->fx_r_type == BFD_RELOC_SIZE32
15131
0
      || fixp->fx_r_type == BFD_RELOC_SIZE64)
15132
0
    return IS_ELF && fixp->fx_addsy
15133
0
     && (!S_IS_DEFINED (fixp->fx_addsy)
15134
0
         || S_IS_EXTERNAL (fixp->fx_addsy));
15135
0
#endif
15136
15137
0
  if (fixp->fx_subsy)
15138
0
    {
15139
0
      if (fixp->fx_subsy == GOT_symbol)
15140
0
  {
15141
0
    if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
15142
0
      {
15143
0
        if (!object_64bit)
15144
0
    abort ();
15145
0
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15146
0
        if (fixp->fx_tcbit2)
15147
0
    fixp->fx_r_type = (fixp->fx_tcbit
15148
0
           ? BFD_RELOC_X86_64_REX_GOTPCRELX
15149
0
           : BFD_RELOC_X86_64_GOTPCRELX);
15150
0
        else
15151
0
#endif
15152
0
    fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
15153
0
      }
15154
0
    else
15155
0
      {
15156
0
        if (!object_64bit)
15157
0
    fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
15158
0
        else
15159
0
    fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
15160
0
      }
15161
0
    fixp->fx_subsy = 0;
15162
0
  }
15163
0
    }
15164
0
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15165
0
  else
15166
0
    {
15167
      /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
15168
   to section.  Since PLT32 relocation must be against symbols,
15169
   turn such PLT32 relocation into PC32 relocation.  */
15170
0
      if (fixp->fx_addsy
15171
0
    && (fixp->fx_r_type == BFD_RELOC_386_PLT32
15172
0
        || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
15173
0
    && symbol_section_p (fixp->fx_addsy))
15174
0
  fixp->fx_r_type = BFD_RELOC_32_PCREL;
15175
0
      if (!object_64bit)
15176
0
  {
15177
0
    if (fixp->fx_r_type == BFD_RELOC_386_GOT32
15178
0
        && fixp->fx_tcbit2)
15179
0
      fixp->fx_r_type = BFD_RELOC_386_GOT32X;
15180
0
  }
15181
0
    }
15182
0
#endif
15183
15184
0
  return 1;
15185
0
}
15186
15187
arelent *
15188
tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
15189
0
{
15190
0
  arelent *rel;
15191
0
  bfd_reloc_code_real_type code;
15192
15193
0
  switch (fixp->fx_r_type)
15194
0
    {
15195
0
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15196
0
      symbolS *sym;
15197
15198
0
    case BFD_RELOC_SIZE32:
15199
0
    case BFD_RELOC_SIZE64:
15200
0
      if (fixp->fx_addsy
15201
0
    && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
15202
0
    && (!fixp->fx_subsy
15203
0
        || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
15204
0
  sym = fixp->fx_addsy;
15205
0
      else if (fixp->fx_subsy
15206
0
         && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
15207
0
         && (!fixp->fx_addsy
15208
0
       || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
15209
0
  sym = fixp->fx_subsy;
15210
0
      else
15211
0
  sym = NULL;
15212
0
      if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
15213
0
  {
15214
    /* Resolve size relocation against local symbol to size of
15215
       the symbol plus addend.  */
15216
0
    valueT value = S_GET_SIZE (sym);
15217
15218
0
    if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
15219
0
      value = bfd_section_size (S_GET_SEGMENT (sym));
15220
0
    if (sym == fixp->fx_subsy)
15221
0
      {
15222
0
        value = -value;
15223
0
        if (fixp->fx_addsy)
15224
0
          value += S_GET_VALUE (fixp->fx_addsy);
15225
0
      }
15226
0
    else if (fixp->fx_subsy)
15227
0
      value -= S_GET_VALUE (fixp->fx_subsy);
15228
0
    value += fixp->fx_offset;
15229
0
    if (fixp->fx_r_type == BFD_RELOC_SIZE32
15230
0
        && object_64bit
15231
0
        && !fits_in_unsigned_long (value))
15232
0
      as_bad_where (fixp->fx_file, fixp->fx_line,
15233
0
        _("symbol size computation overflow"));
15234
0
    fixp->fx_addsy = NULL;
15235
0
    fixp->fx_subsy = NULL;
15236
0
    md_apply_fix (fixp, (valueT *) &value, NULL);
15237
0
    return NULL;
15238
0
  }
15239
0
      if (!fixp->fx_addsy || fixp->fx_subsy)
15240
0
  {
15241
0
    as_bad_where (fixp->fx_file, fixp->fx_line,
15242
0
      "unsupported expression involving @size");
15243
0
    return NULL;
15244
0
  }
15245
0
#endif
15246
      /* Fall through.  */
15247
15248
0
    case BFD_RELOC_X86_64_PLT32:
15249
0
    case BFD_RELOC_X86_64_GOT32:
15250
0
    case BFD_RELOC_X86_64_GOTPCREL:
15251
0
    case BFD_RELOC_X86_64_GOTPCRELX:
15252
0
    case BFD_RELOC_X86_64_REX_GOTPCRELX:
15253
0
    case BFD_RELOC_386_PLT32:
15254
0
    case BFD_RELOC_386_GOT32:
15255
0
    case BFD_RELOC_386_GOT32X:
15256
0
    case BFD_RELOC_386_GOTOFF:
15257
0
    case BFD_RELOC_386_GOTPC:
15258
0
    case BFD_RELOC_386_TLS_GD:
15259
0
    case BFD_RELOC_386_TLS_LDM:
15260
0
    case BFD_RELOC_386_TLS_LDO_32:
15261
0
    case BFD_RELOC_386_TLS_IE_32:
15262
0
    case BFD_RELOC_386_TLS_IE:
15263
0
    case BFD_RELOC_386_TLS_GOTIE:
15264
0
    case BFD_RELOC_386_TLS_LE_32:
15265
0
    case BFD_RELOC_386_TLS_LE:
15266
0
    case BFD_RELOC_386_TLS_GOTDESC:
15267
0
    case BFD_RELOC_386_TLS_DESC_CALL:
15268
0
    case BFD_RELOC_X86_64_TLSGD:
15269
0
    case BFD_RELOC_X86_64_TLSLD:
15270
0
    case BFD_RELOC_X86_64_DTPOFF32:
15271
0
    case BFD_RELOC_X86_64_DTPOFF64:
15272
0
    case BFD_RELOC_X86_64_GOTTPOFF:
15273
0
    case BFD_RELOC_X86_64_TPOFF32:
15274
0
    case BFD_RELOC_X86_64_TPOFF64:
15275
0
    case BFD_RELOC_X86_64_GOTOFF64:
15276
0
    case BFD_RELOC_X86_64_GOTPC32:
15277
0
    case BFD_RELOC_X86_64_GOT64:
15278
0
    case BFD_RELOC_X86_64_GOTPCREL64:
15279
0
    case BFD_RELOC_X86_64_GOTPC64:
15280
0
    case BFD_RELOC_X86_64_GOTPLT64:
15281
0
    case BFD_RELOC_X86_64_PLTOFF64:
15282
0
    case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
15283
0
    case BFD_RELOC_X86_64_TLSDESC_CALL:
15284
0
    case BFD_RELOC_RVA:
15285
0
    case BFD_RELOC_VTABLE_ENTRY:
15286
0
    case BFD_RELOC_VTABLE_INHERIT:
15287
#ifdef TE_PE
15288
    case BFD_RELOC_32_SECREL:
15289
    case BFD_RELOC_16_SECIDX:
15290
#endif
15291
0
      code = fixp->fx_r_type;
15292
0
      break;
15293
0
    case BFD_RELOC_X86_64_32S:
15294
0
      if (!fixp->fx_pcrel)
15295
0
  {
15296
    /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32.  */
15297
0
    code = fixp->fx_r_type;
15298
0
    break;
15299
0
  }
15300
      /* Fall through.  */
15301
0
    default:
15302
0
      if (fixp->fx_pcrel)
15303
0
  {
15304
0
    switch (fixp->fx_size)
15305
0
      {
15306
0
      default:
15307
0
        as_bad_where (fixp->fx_file, fixp->fx_line,
15308
0
          _("can not do %d byte pc-relative relocation"),
15309
0
          fixp->fx_size);
15310
0
        code = BFD_RELOC_32_PCREL;
15311
0
        break;
15312
0
      case 1: code = BFD_RELOC_8_PCREL;  break;
15313
0
      case 2: code = BFD_RELOC_16_PCREL; break;
15314
0
      case 4: code = BFD_RELOC_32_PCREL; break;
15315
0
#ifdef BFD64
15316
0
      case 8: code = BFD_RELOC_64_PCREL; break;
15317
0
#endif
15318
0
      }
15319
0
  }
15320
0
      else
15321
0
  {
15322
0
    switch (fixp->fx_size)
15323
0
      {
15324
0
      default:
15325
0
        as_bad_where (fixp->fx_file, fixp->fx_line,
15326
0
          _("can not do %d byte relocation"),
15327
0
          fixp->fx_size);
15328
0
        code = BFD_RELOC_32;
15329
0
        break;
15330
0
      case 1: code = BFD_RELOC_8;  break;
15331
0
      case 2: code = BFD_RELOC_16; break;
15332
0
      case 4: code = BFD_RELOC_32; break;
15333
0
#ifdef BFD64
15334
0
      case 8: code = BFD_RELOC_64; break;
15335
0
#endif
15336
0
      }
15337
0
  }
15338
0
      break;
15339
0
    }
15340
15341
0
  if ((code == BFD_RELOC_32
15342
0
       || code == BFD_RELOC_32_PCREL
15343
0
       || code == BFD_RELOC_X86_64_32S)
15344
0
      && GOT_symbol
15345
0
      && fixp->fx_addsy == GOT_symbol)
15346
0
    {
15347
0
      if (!object_64bit)
15348
0
  code = BFD_RELOC_386_GOTPC;
15349
0
      else
15350
0
  code = BFD_RELOC_X86_64_GOTPC32;
15351
0
    }
15352
0
  if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
15353
0
      && GOT_symbol
15354
0
      && fixp->fx_addsy == GOT_symbol)
15355
0
    {
15356
0
      code = BFD_RELOC_X86_64_GOTPC64;
15357
0
    }
15358
15359
0
  rel = XNEW (arelent);
15360
0
  rel->sym_ptr_ptr = XNEW (asymbol *);
15361
0
  *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
15362
15363
0
  rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
15364
15365
0
  if (!use_rela_relocations)
15366
0
    {
15367
      /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
15368
   vtable entry to be used in the relocation's section offset.  */
15369
0
      if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
15370
0
  rel->address = fixp->fx_offset;
15371
#if defined (OBJ_COFF) && defined (TE_PE)
15372
      else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
15373
  rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
15374
      else
15375
#endif
15376
0
      rel->addend = 0;
15377
0
    }
15378
  /* Use the rela in 64bit mode.  */
15379
0
  else
15380
0
    {
15381
0
      if (disallow_64bit_reloc)
15382
0
  switch (code)
15383
0
    {
15384
0
    case BFD_RELOC_X86_64_DTPOFF64:
15385
0
    case BFD_RELOC_X86_64_TPOFF64:
15386
0
    case BFD_RELOC_64_PCREL:
15387
0
    case BFD_RELOC_X86_64_GOTOFF64:
15388
0
    case BFD_RELOC_X86_64_GOT64:
15389
0
    case BFD_RELOC_X86_64_GOTPCREL64:
15390
0
    case BFD_RELOC_X86_64_GOTPC64:
15391
0
    case BFD_RELOC_X86_64_GOTPLT64:
15392
0
    case BFD_RELOC_X86_64_PLTOFF64:
15393
0
      as_bad_where (fixp->fx_file, fixp->fx_line,
15394
0
        _("cannot represent relocation type %s in x32 mode"),
15395
0
        bfd_get_reloc_code_name (code));
15396
0
      break;
15397
0
    default:
15398
0
      break;
15399
0
    }
15400
15401
0
      if (!fixp->fx_pcrel)
15402
0
  rel->addend = fixp->fx_offset;
15403
0
      else
15404
0
  switch (code)
15405
0
    {
15406
0
    case BFD_RELOC_X86_64_PLT32:
15407
0
    case BFD_RELOC_X86_64_GOT32:
15408
0
    case BFD_RELOC_X86_64_GOTPCREL:
15409
0
    case BFD_RELOC_X86_64_GOTPCRELX:
15410
0
    case BFD_RELOC_X86_64_REX_GOTPCRELX:
15411
0
    case BFD_RELOC_X86_64_TLSGD:
15412
0
    case BFD_RELOC_X86_64_TLSLD:
15413
0
    case BFD_RELOC_X86_64_GOTTPOFF:
15414
0
    case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
15415
0
    case BFD_RELOC_X86_64_TLSDESC_CALL:
15416
0
      rel->addend = fixp->fx_offset - fixp->fx_size;
15417
0
      break;
15418
0
    default:
15419
0
      rel->addend = (section->vma
15420
0
         - fixp->fx_size
15421
0
         + fixp->fx_addnumber
15422
0
         + md_pcrel_from (fixp));
15423
0
      break;
15424
0
    }
15425
0
    }
15426
15427
0
  rel->howto = bfd_reloc_type_lookup (stdoutput, code);
15428
0
  if (rel->howto == NULL)
15429
0
    {
15430
0
      as_bad_where (fixp->fx_file, fixp->fx_line,
15431
0
        _("cannot represent relocation type %s"),
15432
0
        bfd_get_reloc_code_name (code));
15433
      /* Set howto to a garbage value so that we can keep going.  */
15434
0
      rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
15435
0
      gas_assert (rel->howto != NULL);
15436
0
    }
15437
15438
0
  return rel;
15439
0
}
15440
15441
#include "tc-i386-intel.c"
15442
15443
void
15444
tc_x86_parse_to_dw2regnum (expressionS *exp)
15445
2
{
15446
2
  int saved_naked_reg;
15447
2
  char saved_register_dot;
15448
15449
2
  saved_naked_reg = allow_naked_reg;
15450
2
  allow_naked_reg = 1;
15451
2
  saved_register_dot = register_chars['.'];
15452
2
  register_chars['.'] = '.';
15453
2
  allow_pseudo_reg = 1;
15454
2
  expression_and_evaluate (exp);
15455
2
  allow_pseudo_reg = 0;
15456
2
  register_chars['.'] = saved_register_dot;
15457
2
  allow_naked_reg = saved_naked_reg;
15458
15459
2
  if (exp->X_op == O_register && exp->X_add_number >= 0)
15460
2
    {
15461
2
      if ((addressT) exp->X_add_number < i386_regtab_size)
15462
2
  {
15463
2
    exp->X_op = O_constant;
15464
2
    exp->X_add_number = i386_regtab[exp->X_add_number]
15465
2
            .dw2_regnum[flag_code >> 1];
15466
2
  }
15467
0
      else
15468
0
  exp->X_op = O_illegal;
15469
2
    }
15470
2
}
15471
15472
void
15473
tc_x86_frame_initial_instructions (void)
15474
38
{
15475
38
  static unsigned int sp_regno[2];
15476
15477
38
  if (!sp_regno[flag_code >> 1])
15478
2
    {
15479
2
      char *saved_input = input_line_pointer;
15480
2
      char sp[][4] = {"esp", "rsp"};
15481
2
      expressionS exp;
15482
15483
2
      input_line_pointer = sp[flag_code >> 1];
15484
2
      tc_x86_parse_to_dw2regnum (&exp);
15485
2
      gas_assert (exp.X_op == O_constant);
15486
0
      sp_regno[flag_code >> 1] = exp.X_add_number;
15487
2
      input_line_pointer = saved_input;
15488
2
    }
15489
15490
0
  cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
15491
38
  cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
15492
38
}
15493
15494
int
15495
x86_dwarf2_addr_size (void)
15496
200
{
15497
200
#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
15498
200
  if (x86_elf_abi == X86_64_X32_ABI)
15499
0
    return 4;
15500
200
#endif
15501
200
  return bfd_arch_bits_per_address (stdoutput) / 8;
15502
200
}
15503
15504
int
15505
i386_elf_section_type (const char *str, size_t len)
15506
0
{
15507
0
  if (flag_code == CODE_64BIT
15508
0
      && len == sizeof ("unwind") - 1
15509
0
      && startswith (str, "unwind"))
15510
0
    return SHT_X86_64_UNWIND;
15511
15512
0
  return -1;
15513
0
}
15514
15515
#ifdef TE_SOLARIS
15516
void
15517
i386_solaris_fix_up_eh_frame (segT sec)
15518
{
15519
  if (flag_code == CODE_64BIT)
15520
    elf_section_type (sec) = SHT_X86_64_UNWIND;
15521
}
15522
#endif
15523
15524
#ifdef TE_PE
15525
void
15526
tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
15527
{
15528
  expressionS exp;
15529
15530
  exp.X_op = O_secrel;
15531
  exp.X_add_symbol = symbol;
15532
  exp.X_add_number = 0;
15533
  emit_expr (&exp, size);
15534
}
15535
#endif
15536
15537
#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15538
/* For ELF on x86-64, add support for SHF_X86_64_LARGE.  */
15539
15540
bfd_vma
15541
x86_64_section_letter (int letter, const char **ptr_msg)
15542
0
{
15543
0
  if (flag_code == CODE_64BIT)
15544
0
    {
15545
0
      if (letter == 'l')
15546
0
  return SHF_X86_64_LARGE;
15547
15548
0
      *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
15549
0
    }
15550
0
  else
15551
0
    *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
15552
0
  return -1;
15553
0
}
15554
15555
bfd_vma
15556
x86_64_section_word (char *str, size_t len)
15557
0
{
15558
0
  if (len == 5 && flag_code == CODE_64BIT && startswith (str, "large"))
15559
0
    return SHF_X86_64_LARGE;
15560
15561
0
  return -1;
15562
0
}
15563
15564
static void
15565
handle_large_common (int small ATTRIBUTE_UNUSED)
15566
0
{
15567
0
  if (flag_code != CODE_64BIT)
15568
0
    {
15569
0
      s_comm_internal (0, elf_common_parse);
15570
0
      as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
15571
0
    }
15572
0
  else
15573
0
    {
15574
0
      static segT lbss_section;
15575
0
      asection *saved_com_section_ptr = elf_com_section_ptr;
15576
0
      asection *saved_bss_section = bss_section;
15577
15578
0
      if (lbss_section == NULL)
15579
0
  {
15580
0
    flagword applicable;
15581
0
    segT seg = now_seg;
15582
0
    subsegT subseg = now_subseg;
15583
15584
    /* The .lbss section is for local .largecomm symbols.  */
15585
0
    lbss_section = subseg_new (".lbss", 0);
15586
0
    applicable = bfd_applicable_section_flags (stdoutput);
15587
0
    bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
15588
0
    seg_info (lbss_section)->bss = 1;
15589
15590
0
    subseg_set (seg, subseg);
15591
0
  }
15592
15593
0
      elf_com_section_ptr = &_bfd_elf_large_com_section;
15594
0
      bss_section = lbss_section;
15595
15596
0
      s_comm_internal (0, elf_common_parse);
15597
15598
0
      elf_com_section_ptr = saved_com_section_ptr;
15599
0
      bss_section = saved_bss_section;
15600
0
    }
15601
0
}
15602
#endif /* OBJ_ELF || OBJ_MAYBE_ELF */