]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gas/config/tc-i386.c
x86: re-order update_code_flag()
[thirdparty/binutils-gdb.git] / gas / config / tc-i386.c
CommitLineData
b534c6d3 1/* tc-i386.c -- Assemble code for the Intel 80386
d87bef3a 2 Copyright (C) 1989-2023 Free Software Foundation, Inc.
252b5132
RH
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
ec2655a6 8 the Free Software Foundation; either version 3, or (at your option)
252b5132
RH
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
4b4da160
NC
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
252b5132 20
47926f60
KH
21/* Intel 80386 machine specific gas.
22 Written by Eliot Dresselhaus (eliot@mgm.mit.edu).
3e73aa7c 23 x86_64 support by Jan Hubicka (jh@suse.cz)
0f10071e 24 VIA PadLock support by Michal Ludvig (mludvig@suse.cz)
47926f60
KH
25 Bugs & suggestions are completely welcome. This is free software.
26 Please help us make it better. */
252b5132 27
252b5132 28#include "as.h"
3882b010 29#include "safe-ctype.h"
252b5132 30#include "subsegs.h"
316e2c05 31#include "dwarf2dbg.h"
54cfded0 32#include "dw2gencfi.h"
b52c4ee4
IB
33#include "gen-sframe.h"
34#include "sframe.h"
d2b2c203 35#include "elf/x86-64.h"
40fb9820 36#include "opcodes/i386-init.h"
5c139202 37#include "opcodes/i386-mnem.h"
41fd2579 38#include <limits.h>
41fd2579 39
c3332e24 40#ifndef INFER_ADDR_PREFIX
eecb386c 41#define INFER_ADDR_PREFIX 1
c3332e24
AM
42#endif
43
29b0f896
AM
44#ifndef DEFAULT_ARCH
45#define DEFAULT_ARCH "i386"
246fcdee 46#endif
252b5132 47
edde18a5
AM
48#ifndef INLINE
49#if __GNUC__ >= 2
50#define INLINE __inline__
51#else
52#define INLINE
53#endif
54#endif
55
6305a203
L
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,
42164a71 60 REP_PREFIX/HLE_PREFIX, LOCK_PREFIX. */
6305a203
L
61#define WAIT_PREFIX 0
62#define SEG_PREFIX 1
63#define ADDR_PREFIX 2
64#define DATA_PREFIX 3
c32fa91d 65#define REP_PREFIX 4
42164a71 66#define HLE_PREFIX REP_PREFIX
7e8b059b 67#define BND_PREFIX REP_PREFIX
c32fa91d 68#define LOCK_PREFIX 5
4e9ac44a
L
69#define REX_PREFIX 6 /* must come last. */
70#define MAX_PREFIXES 7 /* max prefixes per opcode */
6305a203
L
71
72/* we define the syntax here (modulo base,index,scale syntax) */
73#define REGISTER_PREFIX '%'
74#define IMMEDIATE_PREFIX '$'
75#define ABSOLUTE_PREFIX '*'
76
77/* these are the instruction mnemonic suffixes in AT&T syntax or
78 memory operand size in Intel syntax. */
79#define WORD_MNEM_SUFFIX 'w'
80#define BYTE_MNEM_SUFFIX 'b'
81#define SHORT_MNEM_SUFFIX 's'
82#define LONG_MNEM_SUFFIX 'l'
83#define QWORD_MNEM_SUFFIX 'q'
6305a203
L
84
85#define END_OF_INSN '\0'
86
05909f23
JB
87#define OPERAND_TYPE_NONE { .bitfield = { .class = ClassNone } }
88
79dec6b7
JB
89/* This matches the C -> StaticRounding alias in the opcode table. */
90#define commutative staticrounding
91
6305a203
L
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 */
99typedef struct
100{
d3ce72d0
NC
101 const insn_template *start;
102 const insn_template *end;
6305a203
L
103}
104templates;
105
106/* 386 operand encoding bytes: see 386 book for details of this. */
107typedef 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}
113modrm_byte;
114
115/* x86-64 extension prefix. */
116typedef int rex_byte;
117
6305a203
L
118/* 386 opcode byte to code indirect addressing. */
119typedef struct
120{
121 unsigned base;
122 unsigned index;
123 unsigned scale;
124}
125sib_byte;
126
6305a203
L
127/* x86 arch names, types and features */
128typedef struct
129{
130 const char *name; /* arch name */
6ceeed25
JB
131 unsigned int len:8; /* arch string length */
132 bool skip:1; /* show_arch should skip this. */
6305a203 133 enum processor_type type; /* arch type */
4fc85f37 134 enum { vsz_none, vsz_set, vsz_reset } vsz; /* vector size control */
ae89daec
JB
135 i386_cpu_flags enable; /* cpu feature enable flags */
136 i386_cpu_flags disable; /* cpu feature disable flags */
6305a203
L
137}
138arch_entry;
139
78f12dd3 140static void update_code_flag (int, int);
edd67638 141static void s_insn (int);
e3bb37b5
L
142static void set_code_flag (int);
143static void set_16bit_gcc_code_flag (int);
144static void set_intel_syntax (int);
1efbbeb4 145static void set_intel_mnemonic (int);
db51cc60 146static void set_allow_index_reg (int);
7bab8ab5 147static void set_check (int);
e3bb37b5 148static void set_cpu_arch (int);
6482c264 149#ifdef TE_PE
e3bb37b5 150static void pe_directive_secrel (int);
145667f8 151static void pe_directive_secidx (int);
6482c264 152#endif
e3bb37b5
L
153static void signed_cons (int);
154static char *output_invalid (int c);
ee86248c
JB
155static int i386_finalize_immediate (segT, expressionS *, i386_operand_type,
156 const char *);
157static int i386_finalize_displacement (segT, expressionS *, i386_operand_type,
158 const char *);
a7619375 159static int i386_att_operand (char *);
e3bb37b5 160static int i386_intel_operand (char *, int);
ee86248c
JB
161static int i386_intel_simplify (expressionS *);
162static int i386_intel_parse_name (const char *, expressionS *);
4f081312 163static const reg_entry *parse_register (const char *, char **);
edd67638 164static const char *parse_insn (const char *, char *, bool);
e3bb37b5
L
165static char *parse_operands (char *, const char *);
166static void swap_operands (void);
783c187b 167static void swap_2_operands (unsigned int, unsigned int);
48bcea9f 168static enum flag_code i386_addressing_mode (void);
e3bb37b5 169static void optimize_imm (void);
0de704b9 170static bool optimize_disp (const insn_template *t);
83b16ac6 171static const insn_template *match_template (char);
e3bb37b5
L
172static int check_string (void);
173static int process_suffix (void);
174static int check_byte_reg (void);
175static int check_long_reg (void);
176static int check_qword_reg (void);
177static int check_word_reg (void);
178static int finalize_imm (void);
179static int process_operands (void);
5e042380 180static const reg_entry *build_modrm_byte (void);
e3bb37b5
L
181static void output_insn (void);
182static void output_imm (fragS *, offsetT);
183static void output_disp (fragS *, offsetT);
29b0f896 184#ifndef I386COFF
e3bb37b5 185static void s_bss (int);
252b5132 186#endif
17d4e2a2
L
187#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
188static void handle_large_common (int small ATTRIBUTE_UNUSED);
b4a3a7b4
L
189
190/* GNU_PROPERTY_X86_ISA_1_USED. */
191static unsigned int x86_isa_1_used;
192/* GNU_PROPERTY_X86_FEATURE_2_USED. */
193static unsigned int x86_feature_2_used;
194/* Generate x86 used ISA and feature properties. */
195static unsigned int x86_used_note = DEFAULT_X86_USED_NOTE;
17d4e2a2 196#endif
252b5132 197
a847613f 198static const char *default_arch = DEFAULT_ARCH;
3e73aa7c 199
8a6fb3f9
JB
200/* parse_register() returns this when a register alias cannot be used. */
201static const reg_entry bad_reg = { "<bad>", OPERAND_TYPE_NONE, 0, 0,
202 { Dw2Inval, Dw2Inval } };
203
34684862 204static const reg_entry *reg_eax;
5e042380
JB
205static const reg_entry *reg_ds;
206static const reg_entry *reg_es;
207static const reg_entry *reg_ss;
6288d05f 208static const reg_entry *reg_st0;
6225c532
JB
209static const reg_entry *reg_k0;
210
c0f3af97
L
211/* VEX prefix. */
212typedef struct
213{
43234a1e
L
214 /* VEX prefix is either 2 byte or 3 byte. EVEX is 4 byte. */
215 unsigned char bytes[4];
c0f3af97
L
216 unsigned int length;
217 /* Destination or source register specifier. */
218 const reg_entry *register_specifier;
219} vex_prefix;
220
252b5132 221/* 'md_assemble ()' gathers together information and puts it into a
47926f60 222 i386_insn. */
252b5132 223
520dc8e8
AM
224union i386_op
225 {
226 expressionS *disps;
227 expressionS *imms;
228 const reg_entry *regs;
229 };
230
a65babc9
L
231enum i386_error
232 {
b4d65f2d 233 no_error, /* Must be first. */
86e026a4 234 operand_size_mismatch,
a65babc9
L
235 operand_type_mismatch,
236 register_type_mismatch,
237 number_of_operands_mismatch,
238 invalid_instruction_suffix,
239 bad_imm4,
a65babc9
L
240 unsupported_with_intel_mnemonic,
241 unsupported_syntax,
6c30d220 242 unsupported,
9db83a32
JB
243 unsupported_on_arch,
244 unsupported_64bit,
260cd341 245 invalid_sib_address,
6c30d220 246 invalid_vsib_address,
7bab8ab5 247 invalid_vector_register_set,
260cd341 248 invalid_tmm_register_set,
0cc78721 249 invalid_dest_and_src_register_set,
43234a1e
L
250 unsupported_vector_index_register,
251 unsupported_broadcast,
43234a1e
L
252 broadcast_needed,
253 unsupported_masking,
254 mask_not_on_destination,
255 no_default_mask,
256 unsupported_rc_sae,
43234a1e 257 invalid_register_operand,
a65babc9
L
258 };
259
252b5132
RH
260struct _i386_insn
261 {
47926f60 262 /* TM holds the template for the insn were currently assembling. */
d3ce72d0 263 insn_template tm;
252b5132 264
7d5e4556
L
265 /* SUFFIX holds the instruction size suffix for byte, word, dword
266 or qword, if given. */
252b5132
RH
267 char suffix;
268
9a182d04
JB
269 /* OPCODE_LENGTH holds the number of base opcode bytes. */
270 unsigned char opcode_length;
271
47926f60 272 /* OPERANDS gives the number of given operands. */
252b5132
RH
273 unsigned int operands;
274
275 /* REG_OPERANDS, DISP_OPERANDS, MEM_OPERANDS, IMM_OPERANDS give the number
276 of given register, displacement, memory operands and immediate
47926f60 277 operands. */
252b5132
RH
278 unsigned int reg_operands, disp_operands, mem_operands, imm_operands;
279
280 /* TYPES [i] is the type (see above #defines) which tells us how to
520dc8e8 281 use OP[i] for the corresponding operand. */
40fb9820 282 i386_operand_type types[MAX_OPERANDS];
252b5132 283
520dc8e8
AM
284 /* Displacement expression, immediate expression, or register for each
285 operand. */
286 union i386_op op[MAX_OPERANDS];
252b5132 287
3e73aa7c
JH
288 /* Flags for operands. */
289 unsigned int flags[MAX_OPERANDS];
290#define Operand_PCrel 1
c48dadc9 291#define Operand_Mem 2
c032bc4f 292#define Operand_Signed 4 /* .insn only */
3e73aa7c 293
252b5132 294 /* Relocation type for operand */
f86103b7 295 enum bfd_reloc_code_real reloc[MAX_OPERANDS];
252b5132 296
252b5132
RH
297 /* BASE_REG, INDEX_REG, and LOG2_SCALE_FACTOR are used to encode
298 the base index byte below. */
299 const reg_entry *base_reg;
300 const reg_entry *index_reg;
301 unsigned int log2_scale_factor;
302
303 /* SEG gives the seg_entries of this insn. They are zero unless
47926f60 304 explicit segment overrides are given. */
5e042380 305 const reg_entry *seg[2];
252b5132
RH
306
307 /* PREFIX holds all the given prefix opcodes (usually null).
308 PREFIXES is the number of prefix opcodes. */
309 unsigned int prefixes;
310 unsigned char prefix[MAX_PREFIXES];
311
d0c2e3ec
JB
312 /* .insn allows for reserved opcode spaces. */
313 unsigned char insn_opcode_space;
314
c032bc4f
JB
315 /* .insn also allows (requires) specifying immediate size. */
316 unsigned char imm_bits[MAX_OPERANDS];
317
50128d0c 318 /* Register is in low 3 bits of opcode. */
5b7c81bd 319 bool short_form;
50128d0c 320
6f2f06be 321 /* The operand to a branch insn indicates an absolute branch. */
5b7c81bd 322 bool jumpabsolute;
6f2f06be 323
a4d3acd2
JB
324 /* The operand to a branch insn indicates a far branch. */
325 bool far_branch;
326
9373f275
L
327 /* There is a memory operand of (%dx) which should be only used
328 with input/output instructions. */
329 bool input_output_operand;
330
921eafea
L
331 /* Extended states. */
332 enum
333 {
334 /* Use MMX state. */
335 xstate_mmx = 1 << 0,
336 /* Use XMM state. */
337 xstate_xmm = 1 << 1,
338 /* Use YMM state. */
339 xstate_ymm = 1 << 2 | xstate_xmm,
340 /* Use ZMM state. */
341 xstate_zmm = 1 << 3 | xstate_ymm,
342 /* Use TMM state. */
32930e4e
L
343 xstate_tmm = 1 << 4,
344 /* Use MASK state. */
345 xstate_mask = 1 << 5
921eafea 346 } xstate;
260cd341 347
e379e5f3 348 /* Has GOTPC or TLS relocation. */
5b7c81bd 349 bool has_gotpc_tls_reloc;
e379e5f3 350
252b5132 351 /* RM and SIB are the modrm byte and the sib byte where the
c1e679ec 352 addressing modes of this insn are encoded. */
252b5132 353 modrm_byte rm;
3e73aa7c 354 rex_byte rex;
43234a1e 355 rex_byte vrex;
252b5132 356 sib_byte sib;
c0f3af97 357 vex_prefix vex;
b6169b20 358
6225c532
JB
359 /* Masking attributes.
360
361 The struct describes masking, applied to OPERAND in the instruction.
362 REG is a pointer to the corresponding mask register. ZEROING tells
363 whether merging or zeroing mask is used. */
364 struct Mask_Operation
365 {
366 const reg_entry *reg;
367 unsigned int zeroing;
368 /* The operand where this operation is associated. */
369 unsigned int operand;
370 } mask;
43234a1e
L
371
372 /* Rounding control and SAE attributes. */
ca5312a2
JB
373 struct RC_Operation
374 {
375 enum rc_type
376 {
377 rc_none = -1,
378 rne,
379 rd,
380 ru,
381 rz,
382 saeonly
383 } type;
7063667e
JB
384 /* In Intel syntax the operand modifier form is supposed to be used, but
385 we continue to accept the immediate forms as well. */
386 bool modifier;
ca5312a2 387 } rounding;
43234a1e 388
5273a3cd
JB
389 /* Broadcasting attributes.
390
391 The struct describes broadcasting, applied to OPERAND. TYPE is
392 expresses the broadcast factor. */
393 struct Broadcast_Operation
394 {
0cc78721 395 /* Type of broadcast: {1to2}, {1to4}, {1to8}, {1to16} or {1to32}. */
5273a3cd
JB
396 unsigned int type;
397
398 /* Index of broadcasted operand. */
399 unsigned int operand;
400
401 /* Number of bytes to broadcast. */
402 unsigned int bytes;
403 } broadcast;
43234a1e
L
404
405 /* Compressed disp8*N attribute. */
406 unsigned int memshift;
407
86fa6981
L
408 /* Prefer load or store in encoding. */
409 enum
410 {
411 dir_encoding_default = 0,
412 dir_encoding_load,
64c49ab3
JB
413 dir_encoding_store,
414 dir_encoding_swap
86fa6981 415 } dir_encoding;
891edac4 416
41eb8e88 417 /* Prefer 8bit, 16bit, 32bit displacement in encoding. */
a501d77e
L
418 enum
419 {
420 disp_encoding_default = 0,
421 disp_encoding_8bit,
41eb8e88 422 disp_encoding_16bit,
a501d77e
L
423 disp_encoding_32bit
424 } disp_encoding;
f8a5c266 425
6b6b6807 426 /* Prefer the REX byte in encoding. */
5b7c81bd 427 bool rex_encoding;
6b6b6807 428
b6f8c7c4 429 /* Disable instruction size optimization. */
5b7c81bd 430 bool no_optimize;
b6f8c7c4 431
86fa6981
L
432 /* How to encode vector instructions. */
433 enum
434 {
435 vex_encoding_default = 0,
42e04b36 436 vex_encoding_vex,
86fa6981 437 vex_encoding_vex3,
da4977e0
JB
438 vex_encoding_evex,
439 vex_encoding_error
86fa6981
L
440 } vec_encoding;
441
d5de92cf
L
442 /* REP prefix. */
443 const char *rep_prefix;
444
165de32a
L
445 /* HLE prefix. */
446 const char *hle_prefix;
42164a71 447
7e8b059b
L
448 /* Have BND prefix. */
449 const char *bnd_prefix;
450
04ef582a
L
451 /* Have NOTRACK prefix. */
452 const char *notrack_prefix;
453
891edac4 454 /* Error message. */
a65babc9 455 enum i386_error error;
252b5132
RH
456 };
457
458typedef struct _i386_insn i386_insn;
459
43234a1e
L
460/* Link RC type with corresponding string, that'll be looked for in
461 asm. */
462struct RC_name
463{
464 enum rc_type type;
465 const char *name;
466 unsigned int len;
467};
468
469static const struct RC_name RC_NamesTable[] =
470{
471 { rne, STRING_COMMA_LEN ("rn-sae") },
472 { rd, STRING_COMMA_LEN ("rd-sae") },
473 { ru, STRING_COMMA_LEN ("ru-sae") },
474 { rz, STRING_COMMA_LEN ("rz-sae") },
475 { saeonly, STRING_COMMA_LEN ("sae") },
476};
477
3bfea8ba
L
478/* To be indexed by segment register number. */
479static const unsigned char i386_seg_prefixes[] = {
480 ES_PREFIX_OPCODE,
481 CS_PREFIX_OPCODE,
482 SS_PREFIX_OPCODE,
483 DS_PREFIX_OPCODE,
484 FS_PREFIX_OPCODE,
485 GS_PREFIX_OPCODE
486};
487
252b5132
RH
488/* List of chars besides those in app.c:symbol_chars that can start an
489 operand. Used to prevent the scrubber eating vital white-space. */
86fa6981 490const char extra_symbol_chars[] = "*%-([{}"
252b5132 491#ifdef LEX_AT
32137342
NC
492 "@"
493#endif
494#ifdef LEX_QM
495 "?"
252b5132 496#endif
32137342 497 ;
252b5132 498
b3983e5f
JB
499#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
500 && !defined (TE_GNU) \
501 && !defined (TE_LINUX) \
d85e70a3 502 && !defined (TE_Haiku) \
b3983e5f
JB
503 && !defined (TE_FreeBSD) \
504 && !defined (TE_DragonFly) \
505 && !defined (TE_NetBSD))
252b5132 506/* This array holds the chars that always start a comment. If the
b3b91714
AM
507 pre-processor is disabled, these aren't very useful. The option
508 --divide will remove '/' from this list. */
509const char *i386_comment_chars = "#/";
510#define SVR4_COMMENT_CHARS 1
252b5132 511#define PREFIX_SEPARATOR '\\'
252b5132 512
b3b91714
AM
513#else
514const char *i386_comment_chars = "#";
515#define PREFIX_SEPARATOR '/'
516#endif
517
252b5132
RH
518/* This array holds the chars that only start a comment at the beginning of
519 a line. If the line seems to have the form '# 123 filename'
ce8a8b2f
AM
520 .line and .file directives will appear in the pre-processed output.
521 Note that input_file.c hand checks for '#' at the beginning of the
252b5132 522 first line of the input file. This is because the compiler outputs
ce8a8b2f
AM
523 #NO_APP at the beginning of its output.
524 Also note that comments started like this one will always work if
252b5132 525 '/' isn't otherwise defined. */
b3b91714 526const char line_comment_chars[] = "#/";
252b5132 527
63a0b638 528const char line_separator_chars[] = ";";
252b5132 529
ce8a8b2f
AM
530/* Chars that can be used to separate mant from exp in floating point
531 nums. */
252b5132
RH
532const char EXP_CHARS[] = "eE";
533
ce8a8b2f
AM
534/* Chars that mean this number is a floating point constant
535 As in 0f12.456
536 or 0d1.2345e12. */
de133cf9 537const char FLT_CHARS[] = "fFdDxXhHbB";
252b5132 538
ce8a8b2f 539/* Tables for lexical analysis. */
252b5132
RH
540static char mnemonic_chars[256];
541static char register_chars[256];
542static char operand_chars[256];
252b5132 543
ce8a8b2f 544/* Lexical macros. */
252b5132
RH
545#define is_operand_char(x) (operand_chars[(unsigned char) x])
546#define is_register_char(x) (register_chars[(unsigned char) x])
547#define is_space_char(x) ((x) == ' ')
252b5132 548
d2b1a14d
JB
549/* All non-digit non-letter characters that may occur in an operand and
550 which aren't already in extra_symbol_chars[]. */
551static const char operand_special_chars[] = "$+,)._~/<>|&^!=:@]";
252b5132
RH
552
553/* md_assemble() always leaves the strings it's passed unaltered. To
554 effect this we maintain a stack of saved characters that we've smashed
555 with '\0's (indicating end of strings for various sub-fields of the
47926f60 556 assembler instruction). */
252b5132 557static char save_stack[32];
ce8a8b2f 558static char *save_stack_p;
252b5132
RH
559#define END_STRING_AND_SAVE(s) \
560 do { *save_stack_p++ = *(s); *(s) = '\0'; } while (0)
561#define RESTORE_END_STRING(s) \
562 do { *(s) = *--save_stack_p; } while (0)
563
47926f60 564/* The instruction we're assembling. */
252b5132
RH
565static i386_insn i;
566
567/* Possible templates for current insn. */
568static const templates *current_templates;
569
31b2323c
L
570/* Per instruction expressionS buffers: max displacements & immediates. */
571static expressionS disp_expressions[MAX_MEMORY_OPERANDS];
572static expressionS im_expressions[MAX_IMMEDIATE_OPERANDS];
252b5132 573
47926f60 574/* Current operand we are working on. */
ee86248c 575static int this_operand = -1;
252b5132 576
d0c2e3ec
JB
577/* Are we processing a .insn directive? */
578#define dot_insn() (i.tm.mnem_off == MN__insn)
579
3e73aa7c
JH
580/* We support four different modes. FLAG_CODE variable is used to distinguish
581 these. */
582
583enum flag_code {
584 CODE_32BIT,
585 CODE_16BIT,
586 CODE_64BIT };
587
588static enum flag_code flag_code;
4fa24527 589static unsigned int object_64bit;
862be3fb 590static unsigned int disallow_64bit_reloc;
3e73aa7c 591static int use_rela_relocations = 0;
e379e5f3
L
592/* __tls_get_addr/___tls_get_addr symbol for TLS. */
593static const char *tls_get_addr;
3e73aa7c 594
7af8ed2d
NC
595#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
596 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
597 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
598
351f65ca
L
599/* The ELF ABI to use. */
600enum x86_elf_abi
601{
602 I386_ABI,
7f56bc95
L
603 X86_64_ABI,
604 X86_64_X32_ABI
351f65ca
L
605};
606
607static enum x86_elf_abi x86_elf_abi = I386_ABI;
7af8ed2d 608#endif
351f65ca 609
167ad85b
TG
610#if defined (TE_PE) || defined (TE_PEP)
611/* Use big object file format. */
612static int use_big_obj = 0;
613#endif
614
8dcea932
L
615#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
616/* 1 if generating code for a shared library. */
617static int shared = 0;
b52c4ee4
IB
618
619unsigned int x86_sframe_cfa_sp_reg;
3e3e792a 620/* The other CFA base register for SFrame stack trace info. */
b52c4ee4
IB
621unsigned int x86_sframe_cfa_fp_reg;
622unsigned int x86_sframe_cfa_ra_reg;
623
8dcea932
L
624#endif
625
47926f60
KH
626/* 1 for intel syntax,
627 0 if att syntax. */
628static int intel_syntax = 0;
252b5132 629
4b5aaf5f
L
630static enum x86_64_isa
631{
632 amd64 = 1, /* AMD64 ISA. */
633 intel64 /* Intel64 ISA. */
634} isa64;
e89c5eaa 635
1efbbeb4
L
636/* 1 for intel mnemonic,
637 0 if att mnemonic. */
638static int intel_mnemonic = !SYSV386_COMPAT;
639
a60de03c
JB
640/* 1 if pseudo registers are permitted. */
641static int allow_pseudo_reg = 0;
642
47926f60
KH
643/* 1 if register prefix % not required. */
644static int allow_naked_reg = 0;
252b5132 645
33eaf5de 646/* 1 if the assembler should add BND prefix for all control-transferring
7e8b059b
L
647 instructions supporting it, even if this prefix wasn't specified
648 explicitly. */
649static int add_bnd_prefix = 0;
650
ba104c83 651/* 1 if pseudo index register, eiz/riz, is allowed . */
db51cc60
L
652static int allow_index_reg = 0;
653
d022bddd
IT
654/* 1 if the assembler should ignore LOCK prefix, even if it was
655 specified explicitly. */
656static int omit_lock_prefix = 0;
657
e4e00185
AS
658/* 1 if the assembler should encode lfence, mfence, and sfence as
659 "lock addl $0, (%{re}sp)". */
660static int avoid_fence = 0;
661
ae531041
L
662/* 1 if lfence should be inserted after every load. */
663static int lfence_after_load = 0;
664
665/* Non-zero if lfence should be inserted before indirect branch. */
666static enum lfence_before_indirect_branch_kind
667 {
668 lfence_branch_none = 0,
669 lfence_branch_register,
670 lfence_branch_memory,
671 lfence_branch_all
672 }
673lfence_before_indirect_branch;
674
675/* Non-zero if lfence should be inserted before ret. */
676static enum lfence_before_ret_kind
677 {
678 lfence_before_ret_none = 0,
679 lfence_before_ret_not,
a09f656b 680 lfence_before_ret_or,
681 lfence_before_ret_shl
ae531041
L
682 }
683lfence_before_ret;
684
685/* Types of previous instruction is .byte or prefix. */
e379e5f3
L
686static struct
687 {
688 segT seg;
689 const char *file;
690 const char *name;
691 unsigned int line;
692 enum last_insn_kind
693 {
694 last_insn_other = 0,
695 last_insn_directive,
696 last_insn_prefix
697 } kind;
698 } last_insn;
699
0cb4071e
L
700/* 1 if the assembler should generate relax relocations. */
701
702static int generate_relax_relocations
703 = DEFAULT_GENERATE_X86_RELAX_RELOCATIONS;
704
7bab8ab5 705static enum check_kind
daf50ae7 706 {
7bab8ab5
JB
707 check_none = 0,
708 check_warning,
709 check_error
daf50ae7 710 }
7bab8ab5 711sse_check, operand_check = check_warning;
daf50ae7 712
e379e5f3
L
713/* Non-zero if branches should be aligned within power of 2 boundary. */
714static int align_branch_power = 0;
715
716/* Types of branches to align. */
717enum align_branch_kind
718 {
719 align_branch_none = 0,
720 align_branch_jcc = 1,
721 align_branch_fused = 2,
722 align_branch_jmp = 3,
723 align_branch_call = 4,
724 align_branch_indirect = 5,
725 align_branch_ret = 6
726 };
727
728/* Type bits of branches to align. */
729enum align_branch_bit
730 {
731 align_branch_jcc_bit = 1 << align_branch_jcc,
732 align_branch_fused_bit = 1 << align_branch_fused,
733 align_branch_jmp_bit = 1 << align_branch_jmp,
734 align_branch_call_bit = 1 << align_branch_call,
735 align_branch_indirect_bit = 1 << align_branch_indirect,
736 align_branch_ret_bit = 1 << align_branch_ret
737 };
738
739static unsigned int align_branch = (align_branch_jcc_bit
740 | align_branch_fused_bit
741 | align_branch_jmp_bit);
742
79d72f45
HL
743/* Types of condition jump used by macro-fusion. */
744enum mf_jcc_kind
745 {
746 mf_jcc_jo = 0, /* base opcode 0x70 */
747 mf_jcc_jc, /* base opcode 0x72 */
748 mf_jcc_je, /* base opcode 0x74 */
749 mf_jcc_jna, /* base opcode 0x76 */
750 mf_jcc_js, /* base opcode 0x78 */
751 mf_jcc_jp, /* base opcode 0x7a */
752 mf_jcc_jl, /* base opcode 0x7c */
753 mf_jcc_jle, /* base opcode 0x7e */
754 };
755
756/* Types of compare flag-modifying insntructions used by macro-fusion. */
757enum mf_cmp_kind
758 {
759 mf_cmp_test_and, /* test/cmp */
760 mf_cmp_alu_cmp, /* add/sub/cmp */
761 mf_cmp_incdec /* inc/dec */
762 };
763
e379e5f3
L
764/* The maximum padding size for fused jcc. CMP like instruction can
765 be 9 bytes and jcc can be 6 bytes. Leave room just in case for
766 prefixes. */
767#define MAX_FUSED_JCC_PADDING_SIZE 20
768
769/* The maximum number of prefixes added for an instruction. */
770static unsigned int align_branch_prefix_size = 5;
771
b6f8c7c4
L
772/* Optimization:
773 1. Clear the REX_W bit with register operand if possible.
774 2. Above plus use 128bit vector instruction to clear the full vector
775 register.
776 */
777static int optimize = 0;
778
779/* Optimization:
780 1. Clear the REX_W bit with register operand if possible.
781 2. Above plus use 128bit vector instruction to clear the full vector
782 register.
783 3. Above plus optimize "test{q,l,w} $imm8,%r{64,32,16}" to
784 "testb $imm7,%r8".
785 */
786static int optimize_for_space = 0;
787
2ca3ace5
L
788/* Register prefix used for error message. */
789static const char *register_prefix = "%";
790
47926f60
KH
791/* Used in 16 bit gcc mode to add an l suffix to call, ret, enter,
792 leave, push, and pop instructions so that gcc has the same stack
793 frame as in 32 bit mode. */
794static char stackop_size = '\0';
eecb386c 795
12b55ccc
L
796/* Non-zero to optimize code alignment. */
797int optimize_align_code = 1;
798
47926f60
KH
799/* Non-zero to quieten some warnings. */
800static int quiet_warnings = 0;
a38cf1db 801
d59a54c2
JB
802/* Guard to avoid repeated warnings about non-16-bit code on 16-bit CPUs. */
803static bool pre_386_16bit_warned;
804
47926f60
KH
805/* CPU name. */
806static const char *cpu_arch_name = NULL;
6305a203 807static char *cpu_sub_arch_name = NULL;
a38cf1db 808
47926f60 809/* CPU feature flags. */
40fb9820
L
810static i386_cpu_flags cpu_arch_flags = CPU_UNKNOWN_FLAGS;
811
ccc9c027
L
812/* If we have selected a cpu we are generating instructions for. */
813static int cpu_arch_tune_set = 0;
814
9103f4f4 815/* Cpu we are generating instructions for. */
fbf3f584 816enum processor_type cpu_arch_tune = PROCESSOR_UNKNOWN;
9103f4f4
L
817
818/* CPU feature flags of cpu we are generating instructions for. */
40fb9820 819static i386_cpu_flags cpu_arch_tune_flags;
9103f4f4 820
ccc9c027 821/* CPU instruction set architecture used. */
fbf3f584 822enum processor_type cpu_arch_isa = PROCESSOR_UNKNOWN;
ccc9c027 823
9103f4f4 824/* CPU feature flags of instruction set architecture used. */
fbf3f584 825i386_cpu_flags cpu_arch_isa_flags;
9103f4f4 826
fddf5b5b
AM
827/* If set, conditional jumps are not automatically promoted to handle
828 larger than a byte offset. */
f68697e8 829static bool no_cond_jump_promotion = false;
fddf5b5b 830
5cc00775
JB
831/* This will be set from an expression parser hook if there's any
832 applicable operator involved in an expression. */
833static enum {
834 expr_operator_none,
835 expr_operator_present,
836 expr_large_value,
837} expr_mode;
838
c0f3af97
L
839/* Encode SSE instructions with VEX prefix. */
840static unsigned int sse2avx;
841
c8480b58
L
842/* Encode aligned vector move as unaligned vector move. */
843static unsigned int use_unaligned_vector_move;
844
4fc85f37
JB
845/* Maximum permitted vector size. */
846#define VSZ_DEFAULT VSZ512
847static unsigned int vector_size = VSZ_DEFAULT;
848
539f890d
L
849/* Encode scalar AVX instructions with specific vector length. */
850static enum
851 {
852 vex128 = 0,
853 vex256
854 } avxscalar;
855
03751133
L
856/* Encode VEX WIG instructions with specific vex.w. */
857static enum
858 {
859 vexw0 = 0,
860 vexw1
861 } vexwig;
862
43234a1e
L
863/* Encode scalar EVEX LIG instructions with specific vector length. */
864static enum
865 {
866 evexl128 = 0,
867 evexl256,
868 evexl512
869 } evexlig;
870
871/* Encode EVEX WIG instructions with specific evex.w. */
872static enum
873 {
874 evexw0 = 0,
875 evexw1
876 } evexwig;
877
d3d3c6db
IT
878/* Value to encode in EVEX RC bits, for SAE-only instructions. */
879static enum rc_type evexrcig = rne;
880
29b0f896 881/* Pre-defined "_GLOBAL_OFFSET_TABLE_". */
87c245cc 882static symbolS *GOT_symbol;
29b0f896 883
a4447b93
RH
884/* The dwarf2 return column, adjusted for 32 or 64 bit. */
885unsigned int x86_dwarf2_return_column;
886
887/* The dwarf2 data alignment, adjusted for 32 or 64 bit. */
888int x86_cie_data_alignment;
889
252b5132 890/* Interface to relax_segment.
fddf5b5b
AM
891 There are 3 major relax states for 386 jump insns because the
892 different types of jumps add different sizes to frags when we're
e379e5f3
L
893 figuring out what sort of jump to choose to reach a given label.
894
895 BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING are used to align
896 branches which are handled by md_estimate_size_before_relax() and
897 i386_generic_table_relax_frag(). */
252b5132 898
47926f60 899/* Types. */
93c2a809
AM
900#define UNCOND_JUMP 0
901#define COND_JUMP 1
902#define COND_JUMP86 2
e379e5f3
L
903#define BRANCH_PADDING 3
904#define BRANCH_PREFIX 4
905#define FUSED_JCC_PADDING 5
fddf5b5b 906
47926f60 907/* Sizes. */
252b5132
RH
908#define CODE16 1
909#define SMALL 0
29b0f896 910#define SMALL16 (SMALL | CODE16)
252b5132 911#define BIG 2
29b0f896 912#define BIG16 (BIG | CODE16)
252b5132
RH
913
914#ifndef INLINE
915#ifdef __GNUC__
916#define INLINE __inline__
917#else
918#define INLINE
919#endif
920#endif
921
fddf5b5b
AM
922#define ENCODE_RELAX_STATE(type, size) \
923 ((relax_substateT) (((type) << 2) | (size)))
924#define TYPE_FROM_RELAX_STATE(s) \
925 ((s) >> 2)
926#define DISP_SIZE_FROM_RELAX_STATE(s) \
927 ((((s) & 3) == BIG ? 4 : (((s) & 3) == BIG16 ? 2 : 1)))
252b5132
RH
928
929/* This table is used by relax_frag to promote short jumps to long
930 ones where necessary. SMALL (short) jumps may be promoted to BIG
931 (32 bit long) ones, and SMALL16 jumps to BIG16 (16 bit long). We
932 don't allow a short jump in a 32 bit code segment to be promoted to
933 a 16 bit offset jump because it's slower (requires data size
934 prefix), and doesn't work, unless the destination is in the bottom
935 64k of the code segment (The top 16 bits of eip are zeroed). */
936
937const relax_typeS md_relax_table[] =
938{
24eab124
AM
939 /* The fields are:
940 1) most positive reach of this state,
941 2) most negative reach of this state,
93c2a809 942 3) how many bytes this mode will have in the variable part of the frag
ce8a8b2f 943 4) which index into the table to try if we can't fit into this one. */
252b5132 944
fddf5b5b 945 /* UNCOND_JUMP states. */
93c2a809
AM
946 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG)},
947 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16)},
948 /* dword jmp adds 4 bytes to frag:
949 0 extra opcode bytes, 4 displacement bytes. */
252b5132 950 {0, 0, 4, 0},
93c2a809
AM
951 /* word jmp adds 2 byte2 to frag:
952 0 extra opcode bytes, 2 displacement bytes. */
252b5132
RH
953 {0, 0, 2, 0},
954
93c2a809
AM
955 /* COND_JUMP states. */
956 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG)},
957 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP, BIG16)},
958 /* dword conditionals adds 5 bytes to frag:
959 1 extra opcode byte, 4 displacement bytes. */
960 {0, 0, 5, 0},
fddf5b5b 961 /* word conditionals add 3 bytes to frag:
93c2a809
AM
962 1 extra opcode byte, 2 displacement bytes. */
963 {0, 0, 3, 0},
964
965 /* COND_JUMP86 states. */
966 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG)},
967 {127 + 1, -128 + 1, 1, ENCODE_RELAX_STATE (COND_JUMP86, BIG16)},
968 /* dword conditionals adds 5 bytes to frag:
969 1 extra opcode byte, 4 displacement bytes. */
970 {0, 0, 5, 0},
971 /* word conditionals add 4 bytes to frag:
972 1 displacement byte and a 3 byte long branch insn. */
973 {0, 0, 4, 0}
252b5132
RH
974};
975
6ceeed25 976#define ARCH(n, t, f, s) \
4fc85f37 977 { STRING_COMMA_LEN (#n), s, PROCESSOR_ ## t, vsz_none, CPU_ ## f ## _FLAGS, \
ae89daec
JB
978 CPU_NONE_FLAGS }
979#define SUBARCH(n, e, d, s) \
4fc85f37 980 { STRING_COMMA_LEN (#n), s, PROCESSOR_NONE, vsz_none, CPU_ ## e ## _FLAGS, \
ae89daec 981 CPU_ ## d ## _FLAGS }
4fc85f37
JB
982#define VECARCH(n, e, d, v) \
983 { STRING_COMMA_LEN (#n), false, PROCESSOR_NONE, vsz_ ## v, \
984 CPU_ ## e ## _FLAGS, CPU_ ## d ## _FLAGS }
6ceeed25 985
9103f4f4
L
986static const arch_entry cpu_arch[] =
987{
3ce2ebcf
JB
988 /* Do not replace the first two entries - i386_target_format() and
989 set_cpu_arch() rely on them being there in this order. */
6ceeed25
JB
990 ARCH (generic32, GENERIC32, GENERIC32, false),
991 ARCH (generic64, GENERIC64, GENERIC64, false),
992 ARCH (i8086, UNKNOWN, NONE, false),
4d97c5c8
JB
993 ARCH (i186, UNKNOWN, 186, false),
994 ARCH (i286, UNKNOWN, 286, false),
995 ARCH (i386, I386, 386, false),
996 ARCH (i486, I486, 486, false),
997 ARCH (i586, PENTIUM, 586, false),
998 ARCH (i686, PENTIUMPRO, 686, false),
999 ARCH (pentium, PENTIUM, 586, false),
6ceeed25
JB
1000 ARCH (pentiumpro, PENTIUMPRO, PENTIUMPRO, false),
1001 ARCH (pentiumii, PENTIUMPRO, P2, false),
1002 ARCH (pentiumiii, PENTIUMPRO, P3, false),
1003 ARCH (pentium4, PENTIUM4, P4, false),
1004 ARCH (prescott, NOCONA, CORE, false),
1005 ARCH (nocona, NOCONA, NOCONA, false),
1006 ARCH (yonah, CORE, CORE, true),
1007 ARCH (core, CORE, CORE, false),
1008 ARCH (merom, CORE2, CORE2, true),
1009 ARCH (core2, CORE2, CORE2, false),
1010 ARCH (corei7, COREI7, COREI7, false),
1011 ARCH (iamcu, IAMCU, IAMCU, false),
1012 ARCH (k6, K6, K6, false),
1013 ARCH (k6_2, K6, K6_2, false),
1014 ARCH (athlon, ATHLON, ATHLON, false),
1015 ARCH (sledgehammer, K8, K8, true),
1016 ARCH (opteron, K8, K8, false),
1017 ARCH (k8, K8, K8, false),
1018 ARCH (amdfam10, AMDFAM10, AMDFAM10, false),
1019 ARCH (bdver1, BD, BDVER1, false),
1020 ARCH (bdver2, BD, BDVER2, false),
1021 ARCH (bdver3, BD, BDVER3, false),
1022 ARCH (bdver4, BD, BDVER4, false),
1023 ARCH (znver1, ZNVER, ZNVER1, false),
1024 ARCH (znver2, ZNVER, ZNVER2, false),
1025 ARCH (znver3, ZNVER, ZNVER3, false),
b0e8fa7f 1026 ARCH (znver4, ZNVER, ZNVER4, false),
6ceeed25
JB
1027 ARCH (btver1, BT, BTVER1, false),
1028 ARCH (btver2, BT, BTVER2, false),
1029
4d97c5c8
JB
1030 SUBARCH (8087, 8087, ANY_8087, false),
1031 SUBARCH (87, NONE, ANY_8087, false), /* Disable only! */
ae89daec
JB
1032 SUBARCH (287, 287, ANY_287, false),
1033 SUBARCH (387, 387, ANY_387, false),
1034 SUBARCH (687, 687, ANY_687, false),
4d97c5c8 1035 SUBARCH (cmov, CMOV, CMOV, false),
88bd2203 1036 SUBARCH (fxsr, FXSR, ANY_FXSR, false),
ae89daec
JB
1037 SUBARCH (mmx, MMX, ANY_MMX, false),
1038 SUBARCH (sse, SSE, ANY_SSE, false),
1039 SUBARCH (sse2, SSE2, ANY_SSE2, false),
1040 SUBARCH (sse3, SSE3, ANY_SSE3, false),
1041 SUBARCH (sse4a, SSE4A, ANY_SSE4A, false),
1042 SUBARCH (ssse3, SSSE3, ANY_SSSE3, false),
1043 SUBARCH (sse4.1, SSE4_1, ANY_SSE4_1, false),
1044 SUBARCH (sse4.2, SSE4_2, ANY_SSE4_2, false),
1045 SUBARCH (sse4, SSE4_2, ANY_SSE4_1, false),
4fc85f37
JB
1046 VECARCH (avx, AVX, ANY_AVX, reset),
1047 VECARCH (avx2, AVX2, ANY_AVX2, reset),
1048 VECARCH (avx512f, AVX512F, ANY_AVX512F, reset),
1049 VECARCH (avx512cd, AVX512CD, ANY_AVX512CD, reset),
1050 VECARCH (avx512er, AVX512ER, ANY_AVX512ER, reset),
1051 VECARCH (avx512pf, AVX512PF, ANY_AVX512PF, reset),
1052 VECARCH (avx512dq, AVX512DQ, ANY_AVX512DQ, reset),
1053 VECARCH (avx512bw, AVX512BW, ANY_AVX512BW, reset),
1054 VECARCH (avx512vl, AVX512VL, ANY_AVX512VL, reset),
cafa5ef7 1055 SUBARCH (monitor, MONITOR, MONITOR, false),
25626f79
JB
1056 SUBARCH (vmx, VMX, ANY_VMX, false),
1057 SUBARCH (vmfunc, VMFUNC, ANY_VMFUNC, false),
ae89daec 1058 SUBARCH (smx, SMX, SMX, false),
5091b9ee
JB
1059 SUBARCH (xsave, XSAVE, ANY_XSAVE, false),
1060 SUBARCH (xsaveopt, XSAVEOPT, ANY_XSAVEOPT, false),
1061 SUBARCH (xsavec, XSAVEC, ANY_XSAVEC, false),
1062 SUBARCH (xsaves, XSAVES, ANY_XSAVES, false),
1063 SUBARCH (aes, AES, ANY_AES, false),
d54678eb
JB
1064 SUBARCH (pclmul, PCLMULQDQ, ANY_PCLMULQDQ, false),
1065 SUBARCH (clmul, PCLMULQDQ, ANY_PCLMULQDQ, true),
ae89daec
JB
1066 SUBARCH (fsgsbase, FSGSBASE, FSGSBASE, false),
1067 SUBARCH (rdrnd, RDRND, RDRND, false),
5091b9ee 1068 SUBARCH (f16c, F16C, ANY_F16C, false),
ae89daec 1069 SUBARCH (bmi2, BMI2, BMI2, false),
5091b9ee
JB
1070 SUBARCH (fma, FMA, ANY_FMA, false),
1071 SUBARCH (fma4, FMA4, ANY_FMA4, false),
1072 SUBARCH (xop, XOP, ANY_XOP, false),
1073 SUBARCH (lwp, LWP, ANY_LWP, false),
ae89daec
JB
1074 SUBARCH (movbe, MOVBE, MOVBE, false),
1075 SUBARCH (cx16, CX16, CX16, false),
c3bb24f5 1076 SUBARCH (lahf_sahf, LAHF_SAHF, LAHF_SAHF, false),
25626f79 1077 SUBARCH (ept, EPT, ANY_EPT, false),
ae89daec
JB
1078 SUBARCH (lzcnt, LZCNT, LZCNT, false),
1079 SUBARCH (popcnt, POPCNT, POPCNT, false),
1080 SUBARCH (hle, HLE, HLE, false),
760ab3d0
JB
1081 SUBARCH (rtm, RTM, ANY_RTM, false),
1082 SUBARCH (tsx, TSX, TSX, false),
ae89daec
JB
1083 SUBARCH (invpcid, INVPCID, INVPCID, false),
1084 SUBARCH (clflush, CLFLUSH, CLFLUSH, false),
1085 SUBARCH (nop, NOP, NOP, false),
1086 SUBARCH (syscall, SYSCALL, SYSCALL, false),
1087 SUBARCH (rdtscp, RDTSCP, RDTSCP, false),
5091b9ee
JB
1088 SUBARCH (3dnow, 3DNOW, ANY_3DNOW, false),
1089 SUBARCH (3dnowa, 3DNOWA, ANY_3DNOWA, false),
ae89daec 1090 SUBARCH (padlock, PADLOCK, PADLOCK, false),
0919e770
JB
1091 SUBARCH (pacifica, SVME, ANY_SVME, true),
1092 SUBARCH (svme, SVME, ANY_SVME, false),
ae89daec
JB
1093 SUBARCH (abm, ABM, ABM, false),
1094 SUBARCH (bmi, BMI, BMI, false),
1095 SUBARCH (tbm, TBM, TBM, false),
1096 SUBARCH (adx, ADX, ADX, false),
1097 SUBARCH (rdseed, RDSEED, RDSEED, false),
1098 SUBARCH (prfchw, PRFCHW, PRFCHW, false),
1099 SUBARCH (smap, SMAP, SMAP, false),
5091b9ee
JB
1100 SUBARCH (mpx, MPX, ANY_MPX, false),
1101 SUBARCH (sha, SHA, ANY_SHA, false),
ae89daec
JB
1102 SUBARCH (clflushopt, CLFLUSHOPT, CLFLUSHOPT, false),
1103 SUBARCH (prefetchwt1, PREFETCHWT1, PREFETCHWT1, false),
1104 SUBARCH (se1, SE1, SE1, false),
1105 SUBARCH (clwb, CLWB, CLWB, false),
4fc85f37
JB
1106 VECARCH (avx512ifma, AVX512IFMA, ANY_AVX512IFMA, reset),
1107 VECARCH (avx512vbmi, AVX512VBMI, ANY_AVX512VBMI, reset),
1108 VECARCH (avx512_4fmaps, AVX512_4FMAPS, ANY_AVX512_4FMAPS, reset),
1109 VECARCH (avx512_4vnniw, AVX512_4VNNIW, ANY_AVX512_4VNNIW, reset),
1110 VECARCH (avx512_vpopcntdq, AVX512_VPOPCNTDQ, ANY_AVX512_VPOPCNTDQ, reset),
1111 VECARCH (avx512_vbmi2, AVX512_VBMI2, ANY_AVX512_VBMI2, reset),
1112 VECARCH (avx512_vnni, AVX512_VNNI, ANY_AVX512_VNNI, reset),
1113 VECARCH (avx512_bitalg, AVX512_BITALG, ANY_AVX512_BITALG, reset),
1114 VECARCH (avx_vnni, AVX_VNNI, ANY_AVX_VNNI, reset),
ae89daec
JB
1115 SUBARCH (clzero, CLZERO, CLZERO, false),
1116 SUBARCH (mwaitx, MWAITX, MWAITX, false),
5091b9ee 1117 SUBARCH (ospke, OSPKE, ANY_OSPKE, false),
ae89daec
JB
1118 SUBARCH (rdpid, RDPID, RDPID, false),
1119 SUBARCH (ptwrite, PTWRITE, PTWRITE, false),
4d97c5c8
JB
1120 SUBARCH (ibt, IBT, IBT, false),
1121 SUBARCH (shstk, SHSTK, SHSTK, false),
88bd2203 1122 SUBARCH (gfni, GFNI, ANY_GFNI, false),
4fc85f37
JB
1123 VECARCH (vaes, VAES, ANY_VAES, reset),
1124 VECARCH (vpclmulqdq, VPCLMULQDQ, ANY_VPCLMULQDQ, reset),
ae89daec
JB
1125 SUBARCH (wbnoinvd, WBNOINVD, WBNOINVD, false),
1126 SUBARCH (pconfig, PCONFIG, PCONFIG, false),
1127 SUBARCH (waitpkg, WAITPKG, WAITPKG, false),
1128 SUBARCH (cldemote, CLDEMOTE, CLDEMOTE, false),
1129 SUBARCH (amx_int8, AMX_INT8, ANY_AMX_INT8, false),
1130 SUBARCH (amx_bf16, AMX_BF16, ANY_AMX_BF16, false),
5091b9ee 1131 SUBARCH (amx_fp16, AMX_FP16, ANY_AMX_FP16, false),
d100d8c1 1132 SUBARCH (amx_complex, AMX_COMPLEX, ANY_AMX_COMPLEX, false),
ae89daec 1133 SUBARCH (amx_tile, AMX_TILE, ANY_AMX_TILE, false),
4d97c5c8
JB
1134 SUBARCH (movdiri, MOVDIRI, MOVDIRI, false),
1135 SUBARCH (movdir64b, MOVDIR64B, MOVDIR64B, false),
4fc85f37
JB
1136 VECARCH (avx512_bf16, AVX512_BF16, ANY_AVX512_BF16, reset),
1137 VECARCH (avx512_vp2intersect, AVX512_VP2INTERSECT,
1138 ANY_AVX512_VP2INTERSECT, reset),
4d97c5c8
JB
1139 SUBARCH (tdx, TDX, TDX, false),
1140 SUBARCH (enqcmd, ENQCMD, ENQCMD, false),
1141 SUBARCH (serialize, SERIALIZE, SERIALIZE, false),
ae89daec
JB
1142 SUBARCH (rdpru, RDPRU, RDPRU, false),
1143 SUBARCH (mcommit, MCOMMIT, MCOMMIT, false),
0919e770 1144 SUBARCH (sev_es, SEV_ES, ANY_SEV_ES, false),
760ab3d0 1145 SUBARCH (tsxldtrk, TSXLDTRK, ANY_TSXLDTRK, false),
88bd2203
JB
1146 SUBARCH (kl, KL, ANY_KL, false),
1147 SUBARCH (widekl, WIDEKL, ANY_WIDEKL, false),
4d97c5c8
JB
1148 SUBARCH (uintr, UINTR, UINTR, false),
1149 SUBARCH (hreset, HRESET, HRESET, false),
4fc85f37 1150 VECARCH (avx512_fp16, AVX512_FP16, ANY_AVX512_FP16, reset),
ef07be45 1151 SUBARCH (prefetchi, PREFETCHI, PREFETCHI, false),
4fc85f37
JB
1152 VECARCH (avx_ifma, AVX_IFMA, ANY_AVX_IFMA, reset),
1153 VECARCH (avx_vnni_int8, AVX_VNNI_INT8, ANY_AVX_VNNI_INT8, reset),
4d97c5c8
JB
1154 SUBARCH (cmpccxadd, CMPCCXADD, CMPCCXADD, false),
1155 SUBARCH (wrmsrns, WRMSRNS, WRMSRNS, false),
1156 SUBARCH (msrlist, MSRLIST, MSRLIST, false),
4fc85f37 1157 VECARCH (avx_ne_convert, AVX_NE_CONVERT, ANY_AVX_NE_CONVERT, reset),
4d97c5c8 1158 SUBARCH (rao_int, RAO_INT, RAO_INT, false),
0919e770 1159 SUBARCH (rmpquery, RMPQUERY, ANY_RMPQUERY, false),
c88ed92f
ZJ
1160 SUBARCH (fred, FRED, ANY_FRED, false),
1161 SUBARCH (lkgs, LKGS, ANY_LKGS, false),
4fc85f37
JB
1162 VECARCH (avx_vnni_int16, AVX_VNNI_INT16, ANY_AVX_VNNI_INT16, reset),
1163 VECARCH (sha512, SHA512, ANY_SHA512, reset),
1164 VECARCH (sm3, SM3, ANY_SM3, reset),
1165 VECARCH (sm4, SM4, ANY_SM4, reset),
b5c37946 1166 SUBARCH (pbndkb, PBNDKB, PBNDKB, false),
4fc85f37 1167 VECARCH (avx10.1, AVX10_1, ANY_AVX512F, set),
293f5f65
L
1168};
1169
6ceeed25
JB
1170#undef SUBARCH
1171#undef ARCH
1172
704209c0 1173#ifdef I386COFF
a6c24e68
NC
1174/* Like s_lcomm_internal in gas/read.c but the alignment string
1175 is allowed to be optional. */
1176
1177static symbolS *
1178pe_lcomm_internal (int needs_align, symbolS *symbolP, addressT size)
1179{
1180 addressT align = 0;
1181
1182 SKIP_WHITESPACE ();
1183
7ab9ffdd 1184 if (needs_align
a6c24e68
NC
1185 && *input_line_pointer == ',')
1186 {
1187 align = parse_align (needs_align - 1);
7ab9ffdd 1188
a6c24e68
NC
1189 if (align == (addressT) -1)
1190 return NULL;
1191 }
1192 else
1193 {
1194 if (size >= 8)
1195 align = 3;
1196 else if (size >= 4)
1197 align = 2;
1198 else if (size >= 2)
1199 align = 1;
1200 else
1201 align = 0;
1202 }
1203
1204 bss_alloc (symbolP, size, align);
1205 return symbolP;
1206}
1207
704209c0 1208static void
a6c24e68
NC
1209pe_lcomm (int needs_align)
1210{
1211 s_comm_internal (needs_align * 2, pe_lcomm_internal);
1212}
704209c0 1213#endif
a6c24e68 1214
29b0f896
AM
1215const pseudo_typeS md_pseudo_table[] =
1216{
1217#if !defined(OBJ_AOUT) && !defined(USE_ALIGN_PTWO)
1218 {"align", s_align_bytes, 0},
1219#else
1220 {"align", s_align_ptwo, 0},
1221#endif
1222 {"arch", set_cpu_arch, 0},
1223#ifndef I386COFF
1224 {"bss", s_bss, 0},
a6c24e68
NC
1225#else
1226 {"lcomm", pe_lcomm, 1},
29b0f896
AM
1227#endif
1228 {"ffloat", float_cons, 'f'},
1229 {"dfloat", float_cons, 'd'},
1230 {"tfloat", float_cons, 'x'},
7d19d096 1231 {"hfloat", float_cons, 'h'},
de133cf9 1232 {"bfloat16", float_cons, 'b'},
29b0f896 1233 {"value", cons, 2},
d182319b 1234 {"slong", signed_cons, 4},
edd67638 1235 {"insn", s_insn, 0},
29b0f896
AM
1236 {"noopt", s_ignore, 0},
1237 {"optim", s_ignore, 0},
1238 {"code16gcc", set_16bit_gcc_code_flag, CODE_16BIT},
1239 {"code16", set_code_flag, CODE_16BIT},
1240 {"code32", set_code_flag, CODE_32BIT},
da5f19a2 1241#ifdef BFD64
29b0f896 1242 {"code64", set_code_flag, CODE_64BIT},
da5f19a2 1243#endif
29b0f896
AM
1244 {"intel_syntax", set_intel_syntax, 1},
1245 {"att_syntax", set_intel_syntax, 0},
1efbbeb4
L
1246 {"intel_mnemonic", set_intel_mnemonic, 1},
1247 {"att_mnemonic", set_intel_mnemonic, 0},
db51cc60
L
1248 {"allow_index_reg", set_allow_index_reg, 1},
1249 {"disallow_index_reg", set_allow_index_reg, 0},
7bab8ab5
JB
1250 {"sse_check", set_check, 0},
1251 {"operand_check", set_check, 1},
3b22753a
L
1252#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1253 {"largecomm", handle_large_common, 0},
07a53e5c 1254#else
68d20676 1255 {"file", dwarf2_directive_file, 0},
07a53e5c
RH
1256 {"loc", dwarf2_directive_loc, 0},
1257 {"loc_mark_labels", dwarf2_directive_loc_mark_labels, 0},
3b22753a 1258#endif
6482c264
NC
1259#ifdef TE_PE
1260 {"secrel32", pe_directive_secrel, 0},
145667f8 1261 {"secidx", pe_directive_secidx, 0},
6482c264 1262#endif
29b0f896
AM
1263 {0, 0, 0}
1264};
1265
1266/* For interface with expression (). */
1267extern char *input_line_pointer;
1268
1269/* Hash table for instruction mnemonic lookup. */
629310ab 1270static htab_t op_hash;
29b0f896
AM
1271
1272/* Hash table for register lookup. */
629310ab 1273static htab_t reg_hash;
29b0f896 1274\f
ce8a8b2f
AM
1275 /* Various efficient no-op patterns for aligning code labels.
1276 Note: Don't try to assemble the instructions in the comments.
1277 0L and 0w are not legal. */
62a02d25
L
1278static const unsigned char f32_1[] =
1279 {0x90}; /* nop */
1280static const unsigned char f32_2[] =
1281 {0x66,0x90}; /* xchg %ax,%ax */
1282static const unsigned char f32_3[] =
1283 {0x8d,0x76,0x00}; /* leal 0(%esi),%esi */
1284static const unsigned char f32_4[] =
1285 {0x8d,0x74,0x26,0x00}; /* leal 0(%esi,1),%esi */
62a02d25
L
1286static const unsigned char f32_6[] =
1287 {0x8d,0xb6,0x00,0x00,0x00,0x00}; /* leal 0L(%esi),%esi */
1288static const unsigned char f32_7[] =
1289 {0x8d,0xb4,0x26,0x00,0x00,0x00,0x00}; /* leal 0L(%esi,1),%esi */
62a02d25 1290static const unsigned char f16_3[] =
3ae729d5 1291 {0x8d,0x74,0x00}; /* lea 0(%si),%si */
62a02d25 1292static const unsigned char f16_4[] =
3ae729d5
L
1293 {0x8d,0xb4,0x00,0x00}; /* lea 0W(%si),%si */
1294static const unsigned char jump_disp8[] =
1295 {0xeb}; /* jmp disp8 */
1296static const unsigned char jump32_disp32[] =
1297 {0xe9}; /* jmp disp32 */
1298static const unsigned char jump16_disp32[] =
1299 {0x66,0xe9}; /* jmp disp32 */
62a02d25
L
1300/* 32-bit NOPs patterns. */
1301static const unsigned char *const f32_patt[] = {
3ae729d5 1302 f32_1, f32_2, f32_3, f32_4, NULL, f32_6, f32_7
62a02d25
L
1303};
1304/* 16-bit NOPs patterns. */
1305static const unsigned char *const f16_patt[] = {
3ae729d5 1306 f32_1, f32_2, f16_3, f16_4
62a02d25
L
1307};
1308/* nopl (%[re]ax) */
1309static const unsigned char alt_3[] =
1310 {0x0f,0x1f,0x00};
1311/* nopl 0(%[re]ax) */
1312static const unsigned char alt_4[] =
1313 {0x0f,0x1f,0x40,0x00};
1314/* nopl 0(%[re]ax,%[re]ax,1) */
1315static const unsigned char alt_5[] =
1316 {0x0f,0x1f,0x44,0x00,0x00};
1317/* nopw 0(%[re]ax,%[re]ax,1) */
1318static const unsigned char alt_6[] =
1319 {0x66,0x0f,0x1f,0x44,0x00,0x00};
1320/* nopl 0L(%[re]ax) */
1321static const unsigned char alt_7[] =
1322 {0x0f,0x1f,0x80,0x00,0x00,0x00,0x00};
1323/* nopl 0L(%[re]ax,%[re]ax,1) */
1324static const unsigned char alt_8[] =
1325 {0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1326/* nopw 0L(%[re]ax,%[re]ax,1) */
1327static const unsigned char alt_9[] =
1328 {0x66,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
1329/* nopw %cs:0L(%[re]ax,%[re]ax,1) */
1330static const unsigned char alt_10[] =
1331 {0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
3ae729d5
L
1332/* data16 nopw %cs:0L(%eax,%eax,1) */
1333static const unsigned char alt_11[] =
1334 {0x66,0x66,0x2e,0x0f,0x1f,0x84,0x00,0x00,0x00,0x00,0x00};
62a02d25
L
1335/* 32-bit and 64-bit NOPs patterns. */
1336static const unsigned char *const alt_patt[] = {
1337 f32_1, f32_2, alt_3, alt_4, alt_5, alt_6, alt_7, alt_8,
3ae729d5 1338 alt_9, alt_10, alt_11
62a02d25
L
1339};
1340
1341/* Genenerate COUNT bytes of NOPs to WHERE from PATT with the maximum
1342 size of a single NOP instruction MAX_SINGLE_NOP_SIZE. */
1343
1344static void
1345i386_output_nops (char *where, const unsigned char *const *patt,
1346 int count, int max_single_nop_size)
1347
1348{
3ae729d5
L
1349 /* Place the longer NOP first. */
1350 int last;
1351 int offset;
3076e594
NC
1352 const unsigned char *nops;
1353
1354 if (max_single_nop_size < 1)
1355 {
1356 as_fatal (_("i386_output_nops called to generate nops of at most %d bytes!"),
1357 max_single_nop_size);
1358 return;
1359 }
1360
1361 nops = patt[max_single_nop_size - 1];
3ae729d5
L
1362
1363 /* Use the smaller one if the requsted one isn't available. */
1364 if (nops == NULL)
62a02d25 1365 {
3ae729d5
L
1366 max_single_nop_size--;
1367 nops = patt[max_single_nop_size - 1];
62a02d25
L
1368 }
1369
3ae729d5
L
1370 last = count % max_single_nop_size;
1371
1372 count -= last;
1373 for (offset = 0; offset < count; offset += max_single_nop_size)
1374 memcpy (where + offset, nops, max_single_nop_size);
1375
1376 if (last)
1377 {
1378 nops = patt[last - 1];
1379 if (nops == NULL)
1380 {
1381 /* Use the smaller one plus one-byte NOP if the needed one
1382 isn't available. */
1383 last--;
1384 nops = patt[last - 1];
1385 memcpy (where + offset, nops, last);
1386 where[offset + last] = *patt[0];
1387 }
1388 else
1389 memcpy (where + offset, nops, last);
1390 }
62a02d25
L
1391}
1392
3ae729d5
L
1393static INLINE int
1394fits_in_imm7 (offsetT num)
1395{
1396 return (num & 0x7f) == num;
1397}
1398
1399static INLINE int
1400fits_in_imm31 (offsetT num)
1401{
1402 return (num & 0x7fffffff) == num;
1403}
62a02d25
L
1404
1405/* Genenerate COUNT bytes of NOPs to WHERE with the maximum size of a
1406 single NOP instruction LIMIT. */
1407
1408void
3ae729d5 1409i386_generate_nops (fragS *fragP, char *where, offsetT count, int limit)
62a02d25 1410{
3ae729d5 1411 const unsigned char *const *patt = NULL;
62a02d25 1412 int max_single_nop_size;
3ae729d5
L
1413 /* Maximum number of NOPs before switching to jump over NOPs. */
1414 int max_number_of_nops;
62a02d25 1415
3ae729d5 1416 switch (fragP->fr_type)
62a02d25 1417 {
3ae729d5
L
1418 case rs_fill_nop:
1419 case rs_align_code:
1420 break;
e379e5f3
L
1421 case rs_machine_dependent:
1422 /* Allow NOP padding for jumps and calls. */
1423 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
1424 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
1425 break;
1426 /* Fall through. */
3ae729d5 1427 default:
62a02d25
L
1428 return;
1429 }
1430
ccc9c027
L
1431 /* We need to decide which NOP sequence to use for 32bit and
1432 64bit. When -mtune= is used:
4eed87de 1433
76bc74dc
L
1434 1. For PROCESSOR_I386, PROCESSOR_I486, PROCESSOR_PENTIUM and
1435 PROCESSOR_GENERIC32, f32_patt will be used.
80b8656c
L
1436 2. For the rest, alt_patt will be used.
1437
1438 When -mtune= isn't used, alt_patt will be used if
22109423 1439 cpu_arch_isa_flags has CpuNop. Otherwise, f32_patt will
76bc74dc 1440 be used.
ccc9c027
L
1441
1442 When -march= or .arch is used, we can't use anything beyond
1443 cpu_arch_isa_flags. */
1444
1445 if (flag_code == CODE_16BIT)
1446 {
3ae729d5
L
1447 patt = f16_patt;
1448 max_single_nop_size = sizeof (f16_patt) / sizeof (f16_patt[0]);
1449 /* Limit number of NOPs to 2 in 16-bit mode. */
1450 max_number_of_nops = 2;
252b5132 1451 }
33fef721 1452 else
ccc9c027 1453 {
fbf3f584 1454 if (fragP->tc_frag_data.isa == PROCESSOR_UNKNOWN)
ccc9c027
L
1455 {
1456 /* PROCESSOR_UNKNOWN means that all ISAs may be used. */
1457 switch (cpu_arch_tune)
1458 {
1459 case PROCESSOR_UNKNOWN:
1460 /* We use cpu_arch_isa_flags to check if we SHOULD
22109423
L
1461 optimize with nops. */
1462 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1463 patt = alt_patt;
ccc9c027
L
1464 else
1465 patt = f32_patt;
1466 break;
ccc9c027
L
1467 case PROCESSOR_PENTIUM4:
1468 case PROCESSOR_NOCONA:
ef05d495 1469 case PROCESSOR_CORE:
76bc74dc 1470 case PROCESSOR_CORE2:
bd5295b2 1471 case PROCESSOR_COREI7:
76bc74dc 1472 case PROCESSOR_GENERIC64:
ccc9c027
L
1473 case PROCESSOR_K6:
1474 case PROCESSOR_ATHLON:
1475 case PROCESSOR_K8:
4eed87de 1476 case PROCESSOR_AMDFAM10:
8aedb9fe 1477 case PROCESSOR_BD:
029f3522 1478 case PROCESSOR_ZNVER:
7b458c12 1479 case PROCESSOR_BT:
80b8656c 1480 patt = alt_patt;
ccc9c027 1481 break;
76bc74dc 1482 case PROCESSOR_I386:
ccc9c027
L
1483 case PROCESSOR_I486:
1484 case PROCESSOR_PENTIUM:
2dde1948 1485 case PROCESSOR_PENTIUMPRO:
81486035 1486 case PROCESSOR_IAMCU:
ccc9c027
L
1487 case PROCESSOR_GENERIC32:
1488 patt = f32_patt;
1489 break;
c368d2a8
JB
1490 case PROCESSOR_NONE:
1491 abort ();
4eed87de 1492 }
ccc9c027
L
1493 }
1494 else
1495 {
fbf3f584 1496 switch (fragP->tc_frag_data.tune)
ccc9c027
L
1497 {
1498 case PROCESSOR_UNKNOWN:
e6a14101 1499 /* When cpu_arch_isa is set, cpu_arch_tune shouldn't be
ccc9c027
L
1500 PROCESSOR_UNKNOWN. */
1501 abort ();
1502 break;
1503
76bc74dc 1504 case PROCESSOR_I386:
ccc9c027
L
1505 case PROCESSOR_I486:
1506 case PROCESSOR_PENTIUM:
81486035 1507 case PROCESSOR_IAMCU:
ccc9c027
L
1508 case PROCESSOR_K6:
1509 case PROCESSOR_ATHLON:
1510 case PROCESSOR_K8:
4eed87de 1511 case PROCESSOR_AMDFAM10:
8aedb9fe 1512 case PROCESSOR_BD:
029f3522 1513 case PROCESSOR_ZNVER:
7b458c12 1514 case PROCESSOR_BT:
ccc9c027
L
1515 case PROCESSOR_GENERIC32:
1516 /* We use cpu_arch_isa_flags to check if we CAN optimize
22109423
L
1517 with nops. */
1518 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1519 patt = alt_patt;
ccc9c027
L
1520 else
1521 patt = f32_patt;
1522 break;
76bc74dc
L
1523 case PROCESSOR_PENTIUMPRO:
1524 case PROCESSOR_PENTIUM4:
1525 case PROCESSOR_NOCONA:
1526 case PROCESSOR_CORE:
ef05d495 1527 case PROCESSOR_CORE2:
bd5295b2 1528 case PROCESSOR_COREI7:
22109423 1529 if (fragP->tc_frag_data.isa_flags.bitfield.cpunop)
80b8656c 1530 patt = alt_patt;
ccc9c027
L
1531 else
1532 patt = f32_patt;
1533 break;
1534 case PROCESSOR_GENERIC64:
80b8656c 1535 patt = alt_patt;
ccc9c027 1536 break;
c368d2a8
JB
1537 case PROCESSOR_NONE:
1538 abort ();
4eed87de 1539 }
ccc9c027
L
1540 }
1541
76bc74dc
L
1542 if (patt == f32_patt)
1543 {
3ae729d5
L
1544 max_single_nop_size = sizeof (f32_patt) / sizeof (f32_patt[0]);
1545 /* Limit number of NOPs to 2 for older processors. */
1546 max_number_of_nops = 2;
76bc74dc
L
1547 }
1548 else
1549 {
3ae729d5
L
1550 max_single_nop_size = sizeof (alt_patt) / sizeof (alt_patt[0]);
1551 /* Limit number of NOPs to 7 for newer processors. */
1552 max_number_of_nops = 7;
1553 }
1554 }
1555
1556 if (limit == 0)
1557 limit = max_single_nop_size;
1558
1559 if (fragP->fr_type == rs_fill_nop)
1560 {
1561 /* Output NOPs for .nop directive. */
1562 if (limit > max_single_nop_size)
1563 {
1564 as_bad_where (fragP->fr_file, fragP->fr_line,
1565 _("invalid single nop size: %d "
1566 "(expect within [0, %d])"),
1567 limit, max_single_nop_size);
1568 return;
1569 }
1570 }
e379e5f3 1571 else if (fragP->fr_type != rs_machine_dependent)
3ae729d5
L
1572 fragP->fr_var = count;
1573
1574 if ((count / max_single_nop_size) > max_number_of_nops)
1575 {
1576 /* Generate jump over NOPs. */
1577 offsetT disp = count - 2;
1578 if (fits_in_imm7 (disp))
1579 {
1580 /* Use "jmp disp8" if possible. */
1581 count = disp;
1582 where[0] = jump_disp8[0];
1583 where[1] = count;
1584 where += 2;
1585 }
1586 else
1587 {
1588 unsigned int size_of_jump;
1589
1590 if (flag_code == CODE_16BIT)
1591 {
1592 where[0] = jump16_disp32[0];
1593 where[1] = jump16_disp32[1];
1594 size_of_jump = 2;
1595 }
1596 else
1597 {
1598 where[0] = jump32_disp32[0];
1599 size_of_jump = 1;
1600 }
1601
1602 count -= size_of_jump + 4;
1603 if (!fits_in_imm31 (count))
1604 {
1605 as_bad_where (fragP->fr_file, fragP->fr_line,
1606 _("jump over nop padding out of range"));
1607 return;
1608 }
1609
1610 md_number_to_chars (where + size_of_jump, count, 4);
1611 where += size_of_jump + 4;
76bc74dc 1612 }
ccc9c027 1613 }
3ae729d5
L
1614
1615 /* Generate multiple NOPs. */
1616 i386_output_nops (where, patt, count, limit);
252b5132
RH
1617}
1618
c6fb90c8 1619static INLINE int
0dfbf9d7 1620operand_type_all_zero (const union i386_operand_type *x)
40fb9820 1621{
0dfbf9d7 1622 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1623 {
1624 case 3:
0dfbf9d7 1625 if (x->array[2])
c6fb90c8 1626 return 0;
1a0670f3 1627 /* Fall through. */
c6fb90c8 1628 case 2:
0dfbf9d7 1629 if (x->array[1])
c6fb90c8 1630 return 0;
1a0670f3 1631 /* Fall through. */
c6fb90c8 1632 case 1:
0dfbf9d7 1633 return !x->array[0];
c6fb90c8
L
1634 default:
1635 abort ();
1636 }
40fb9820
L
1637}
1638
c6fb90c8 1639static INLINE void
0dfbf9d7 1640operand_type_set (union i386_operand_type *x, unsigned int v)
40fb9820 1641{
0dfbf9d7 1642 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1643 {
1644 case 3:
0dfbf9d7 1645 x->array[2] = v;
1a0670f3 1646 /* Fall through. */
c6fb90c8 1647 case 2:
0dfbf9d7 1648 x->array[1] = v;
1a0670f3 1649 /* Fall through. */
c6fb90c8 1650 case 1:
0dfbf9d7 1651 x->array[0] = v;
1a0670f3 1652 /* Fall through. */
c6fb90c8
L
1653 break;
1654 default:
1655 abort ();
1656 }
bab6aec1
JB
1657
1658 x->bitfield.class = ClassNone;
75e5731b 1659 x->bitfield.instance = InstanceNone;
c6fb90c8 1660}
40fb9820 1661
c6fb90c8 1662static INLINE int
0dfbf9d7
L
1663operand_type_equal (const union i386_operand_type *x,
1664 const union i386_operand_type *y)
c6fb90c8 1665{
0dfbf9d7 1666 switch (ARRAY_SIZE(x->array))
c6fb90c8
L
1667 {
1668 case 3:
0dfbf9d7 1669 if (x->array[2] != y->array[2])
c6fb90c8 1670 return 0;
1a0670f3 1671 /* Fall through. */
c6fb90c8 1672 case 2:
0dfbf9d7 1673 if (x->array[1] != y->array[1])
c6fb90c8 1674 return 0;
1a0670f3 1675 /* Fall through. */
c6fb90c8 1676 case 1:
0dfbf9d7 1677 return x->array[0] == y->array[0];
c6fb90c8
L
1678 break;
1679 default:
1680 abort ();
1681 }
1682}
40fb9820 1683
734dfd1c
JB
1684static INLINE bool
1685is_cpu (const insn_template *t, enum i386_cpu cpu)
1686{
1687 switch (cpu)
1688 {
1689 case Cpu287: return t->cpu.bitfield.cpu287;
1690 case Cpu387: return t->cpu.bitfield.cpu387;
1691 case Cpu3dnow: return t->cpu.bitfield.cpu3dnow;
1692 case Cpu3dnowA: return t->cpu.bitfield.cpu3dnowa;
1693 case CpuAVX: return t->cpu.bitfield.cpuavx;
1694 case CpuHLE: return t->cpu.bitfield.cpuhle;
1695 case CpuAVX512F: return t->cpu.bitfield.cpuavx512f;
1696 case CpuAVX512VL: return t->cpu.bitfield.cpuavx512vl;
1697 case Cpu64: return t->cpu.bitfield.cpu64;
1698 case CpuNo64: return t->cpu.bitfield.cpuno64;
1699 default:
1700 gas_assert (cpu < CpuAttrEnums);
1701 }
1702 return t->cpu.bitfield.isa == cpu + 1u;
1703}
1704
1705static i386_cpu_flags cpu_flags_from_attr (i386_cpu_attr a)
1706{
1707 const unsigned int bps = sizeof (a.array[0]) * CHAR_BIT;
1708 i386_cpu_flags f = { .array[0] = 0 };
1709
1710 switch (ARRAY_SIZE(a.array))
1711 {
1712 case 1:
1713 f.array[CpuAttrEnums / bps]
1714 |= (a.array[0] >> CpuIsaBits) << (CpuAttrEnums % bps);
1715 if (CpuAttrEnums % bps > CpuIsaBits)
1716 f.array[CpuAttrEnums / bps + 1]
1717 = (a.array[0] >> CpuIsaBits) >> (bps - CpuAttrEnums % bps);
1718 break;
1719 default:
1720 abort ();
1721 }
1722
1723 if (a.bitfield.isa)
1724 f.array[(a.bitfield.isa - 1) / bps] |= 1u << ((a.bitfield.isa - 1) % bps);
1725
1726 return f;
1727}
1728
0dfbf9d7
L
1729static INLINE int
1730cpu_flags_all_zero (const union i386_cpu_flags *x)
1731{
1732 switch (ARRAY_SIZE(x->array))
1733 {
75f8266a
KL
1734 case 5:
1735 if (x->array[4])
1736 return 0;
1737 /* Fall through. */
53467f57
IT
1738 case 4:
1739 if (x->array[3])
1740 return 0;
1741 /* Fall through. */
0dfbf9d7
L
1742 case 3:
1743 if (x->array[2])
1744 return 0;
1a0670f3 1745 /* Fall through. */
0dfbf9d7
L
1746 case 2:
1747 if (x->array[1])
1748 return 0;
1a0670f3 1749 /* Fall through. */
0dfbf9d7
L
1750 case 1:
1751 return !x->array[0];
1752 default:
1753 abort ();
1754 }
1755}
1756
0dfbf9d7
L
1757static INLINE int
1758cpu_flags_equal (const union i386_cpu_flags *x,
1759 const union i386_cpu_flags *y)
1760{
1761 switch (ARRAY_SIZE(x->array))
1762 {
75f8266a
KL
1763 case 5:
1764 if (x->array[4] != y->array[4])
1765 return 0;
1766 /* Fall through. */
53467f57
IT
1767 case 4:
1768 if (x->array[3] != y->array[3])
1769 return 0;
1770 /* Fall through. */
0dfbf9d7
L
1771 case 3:
1772 if (x->array[2] != y->array[2])
1773 return 0;
1a0670f3 1774 /* Fall through. */
0dfbf9d7
L
1775 case 2:
1776 if (x->array[1] != y->array[1])
1777 return 0;
1a0670f3 1778 /* Fall through. */
0dfbf9d7
L
1779 case 1:
1780 return x->array[0] == y->array[0];
1781 break;
1782 default:
1783 abort ();
1784 }
1785}
c6fb90c8
L
1786
1787static INLINE int
734dfd1c 1788cpu_flags_check_cpu64 (i386_cpu_attr a)
c6fb90c8 1789{
734dfd1c
JB
1790 return !((flag_code == CODE_64BIT && a.bitfield.cpuno64)
1791 || (flag_code != CODE_64BIT && a.bitfield.cpu64));
40fb9820
L
1792}
1793
c6fb90c8
L
1794static INLINE i386_cpu_flags
1795cpu_flags_and (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1796{
c6fb90c8
L
1797 switch (ARRAY_SIZE (x.array))
1798 {
75f8266a
KL
1799 case 5:
1800 x.array [4] &= y.array [4];
1801 /* Fall through. */
53467f57
IT
1802 case 4:
1803 x.array [3] &= y.array [3];
1804 /* Fall through. */
c6fb90c8
L
1805 case 3:
1806 x.array [2] &= y.array [2];
1a0670f3 1807 /* Fall through. */
c6fb90c8
L
1808 case 2:
1809 x.array [1] &= y.array [1];
1a0670f3 1810 /* Fall through. */
c6fb90c8
L
1811 case 1:
1812 x.array [0] &= y.array [0];
1813 break;
1814 default:
1815 abort ();
1816 }
1817 return x;
1818}
40fb9820 1819
c6fb90c8
L
1820static INLINE i386_cpu_flags
1821cpu_flags_or (i386_cpu_flags x, i386_cpu_flags y)
40fb9820 1822{
c6fb90c8 1823 switch (ARRAY_SIZE (x.array))
40fb9820 1824 {
75f8266a
KL
1825 case 5:
1826 x.array [4] |= y.array [4];
1827 /* Fall through. */
53467f57
IT
1828 case 4:
1829 x.array [3] |= y.array [3];
1830 /* Fall through. */
c6fb90c8
L
1831 case 3:
1832 x.array [2] |= y.array [2];
1a0670f3 1833 /* Fall through. */
c6fb90c8
L
1834 case 2:
1835 x.array [1] |= y.array [1];
1a0670f3 1836 /* Fall through. */
c6fb90c8
L
1837 case 1:
1838 x.array [0] |= y.array [0];
40fb9820
L
1839 break;
1840 default:
1841 abort ();
1842 }
40fb9820
L
1843 return x;
1844}
1845
309d3373
JB
1846static INLINE i386_cpu_flags
1847cpu_flags_and_not (i386_cpu_flags x, i386_cpu_flags y)
1848{
1849 switch (ARRAY_SIZE (x.array))
1850 {
75f8266a
KL
1851 case 5:
1852 x.array [4] &= ~y.array [4];
1853 /* Fall through. */
53467f57
IT
1854 case 4:
1855 x.array [3] &= ~y.array [3];
1856 /* Fall through. */
309d3373
JB
1857 case 3:
1858 x.array [2] &= ~y.array [2];
1a0670f3 1859 /* Fall through. */
309d3373
JB
1860 case 2:
1861 x.array [1] &= ~y.array [1];
1a0670f3 1862 /* Fall through. */
309d3373
JB
1863 case 1:
1864 x.array [0] &= ~y.array [0];
1865 break;
1866 default:
1867 abort ();
1868 }
1869 return x;
1870}
1871
6c0946d0
JB
1872static const i386_cpu_flags avx512 = CPU_ANY_AVX512F_FLAGS;
1873
c0f3af97
L
1874#define CPU_FLAGS_ARCH_MATCH 0x1
1875#define CPU_FLAGS_64BIT_MATCH 0x2
1876
c0f3af97 1877#define CPU_FLAGS_PERFECT_MATCH \
db12e14e 1878 (CPU_FLAGS_ARCH_MATCH | CPU_FLAGS_64BIT_MATCH)
c0f3af97
L
1879
1880/* Return CPU flags match bits. */
3629bb00 1881
40fb9820 1882static int
d3ce72d0 1883cpu_flags_match (const insn_template *t)
40fb9820 1884{
734dfd1c
JB
1885 i386_cpu_flags x = cpu_flags_from_attr (t->cpu);
1886 int match = cpu_flags_check_cpu64 (t->cpu) ? CPU_FLAGS_64BIT_MATCH : 0;
40fb9820
L
1887
1888 x.bitfield.cpu64 = 0;
1889 x.bitfield.cpuno64 = 0;
1890
0dfbf9d7 1891 if (cpu_flags_all_zero (&x))
c0f3af97
L
1892 {
1893 /* This instruction is available on all archs. */
db12e14e 1894 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1895 }
3629bb00
L
1896 else
1897 {
c0f3af97 1898 /* This instruction is available only on some archs. */
3629bb00
L
1899 i386_cpu_flags cpu = cpu_arch_flags;
1900
ab592e75
JB
1901 /* AVX512VL is no standalone feature - match it and then strip it. */
1902 if (x.bitfield.cpuavx512vl && !cpu.bitfield.cpuavx512vl)
1903 return match;
1904 x.bitfield.cpuavx512vl = 0;
1905
22c36940
JB
1906 /* AVX and AVX2 present at the same time express an operand size
1907 dependency - strip AVX2 for the purposes here. The operand size
1908 dependent check occurs in check_vecOperands(). */
1909 if (x.bitfield.cpuavx && x.bitfield.cpuavx2)
1910 x.bitfield.cpuavx2 = 0;
1911
3629bb00 1912 cpu = cpu_flags_and (x, cpu);
c0f3af97
L
1913 if (!cpu_flags_all_zero (&cpu))
1914 {
57392598 1915 if (x.bitfield.cpuavx)
a5ff0eb2 1916 {
929f69fa 1917 /* We need to check a few extra flags with AVX. */
b9d49817 1918 if (cpu.bitfield.cpuavx
40d231b4
JB
1919 && (!t->opcode_modifier.sse2avx
1920 || (sse2avx && !i.prefix[DATA_PREFIX]))
b9d49817 1921 && (!x.bitfield.cpuaes || cpu.bitfield.cpuaes)
929f69fa 1922 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
d54678eb 1923 && (!x.bitfield.cpupclmulqdq || cpu.bitfield.cpupclmulqdq))
b9d49817 1924 match |= CPU_FLAGS_ARCH_MATCH;
a5ff0eb2 1925 }
929f69fa
JB
1926 else if (x.bitfield.cpuavx512f)
1927 {
1928 /* We need to check a few extra flags with AVX512F. */
1929 if (cpu.bitfield.cpuavx512f
1930 && (!x.bitfield.cpugfni || cpu.bitfield.cpugfni)
1931 && (!x.bitfield.cpuvaes || cpu.bitfield.cpuvaes)
1932 && (!x.bitfield.cpuvpclmulqdq || cpu.bitfield.cpuvpclmulqdq))
1933 match |= CPU_FLAGS_ARCH_MATCH;
1934 }
a5ff0eb2 1935 else
db12e14e 1936 match |= CPU_FLAGS_ARCH_MATCH;
c0f3af97 1937 }
3629bb00 1938 }
c0f3af97 1939 return match;
40fb9820
L
1940}
1941
c6fb90c8
L
1942static INLINE i386_operand_type
1943operand_type_and (i386_operand_type x, i386_operand_type y)
40fb9820 1944{
bab6aec1
JB
1945 if (x.bitfield.class != y.bitfield.class)
1946 x.bitfield.class = ClassNone;
75e5731b
JB
1947 if (x.bitfield.instance != y.bitfield.instance)
1948 x.bitfield.instance = InstanceNone;
bab6aec1 1949
c6fb90c8
L
1950 switch (ARRAY_SIZE (x.array))
1951 {
1952 case 3:
1953 x.array [2] &= y.array [2];
1a0670f3 1954 /* Fall through. */
c6fb90c8
L
1955 case 2:
1956 x.array [1] &= y.array [1];
1a0670f3 1957 /* Fall through. */
c6fb90c8
L
1958 case 1:
1959 x.array [0] &= y.array [0];
1960 break;
1961 default:
1962 abort ();
1963 }
1964 return x;
40fb9820
L
1965}
1966
73053c1f
JB
1967static INLINE i386_operand_type
1968operand_type_and_not (i386_operand_type x, i386_operand_type y)
1969{
bab6aec1 1970 gas_assert (y.bitfield.class == ClassNone);
75e5731b 1971 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 1972
73053c1f
JB
1973 switch (ARRAY_SIZE (x.array))
1974 {
1975 case 3:
1976 x.array [2] &= ~y.array [2];
1977 /* Fall through. */
1978 case 2:
1979 x.array [1] &= ~y.array [1];
1980 /* Fall through. */
1981 case 1:
1982 x.array [0] &= ~y.array [0];
1983 break;
1984 default:
1985 abort ();
1986 }
1987 return x;
1988}
1989
c6fb90c8
L
1990static INLINE i386_operand_type
1991operand_type_or (i386_operand_type x, i386_operand_type y)
40fb9820 1992{
bab6aec1
JB
1993 gas_assert (x.bitfield.class == ClassNone ||
1994 y.bitfield.class == ClassNone ||
1995 x.bitfield.class == y.bitfield.class);
75e5731b
JB
1996 gas_assert (x.bitfield.instance == InstanceNone ||
1997 y.bitfield.instance == InstanceNone ||
1998 x.bitfield.instance == y.bitfield.instance);
bab6aec1 1999
c6fb90c8 2000 switch (ARRAY_SIZE (x.array))
40fb9820 2001 {
c6fb90c8
L
2002 case 3:
2003 x.array [2] |= y.array [2];
1a0670f3 2004 /* Fall through. */
c6fb90c8
L
2005 case 2:
2006 x.array [1] |= y.array [1];
1a0670f3 2007 /* Fall through. */
c6fb90c8
L
2008 case 1:
2009 x.array [0] |= y.array [0];
40fb9820
L
2010 break;
2011 default:
2012 abort ();
2013 }
c6fb90c8
L
2014 return x;
2015}
40fb9820 2016
c6fb90c8
L
2017static INLINE i386_operand_type
2018operand_type_xor (i386_operand_type x, i386_operand_type y)
2019{
bab6aec1 2020 gas_assert (y.bitfield.class == ClassNone);
75e5731b 2021 gas_assert (y.bitfield.instance == InstanceNone);
bab6aec1 2022
c6fb90c8
L
2023 switch (ARRAY_SIZE (x.array))
2024 {
2025 case 3:
2026 x.array [2] ^= y.array [2];
1a0670f3 2027 /* Fall through. */
c6fb90c8
L
2028 case 2:
2029 x.array [1] ^= y.array [1];
1a0670f3 2030 /* Fall through. */
c6fb90c8
L
2031 case 1:
2032 x.array [0] ^= y.array [0];
2033 break;
2034 default:
2035 abort ();
2036 }
40fb9820
L
2037 return x;
2038}
2039
05909f23
JB
2040static const i386_operand_type anydisp = {
2041 .bitfield = { .disp8 = 1, .disp16 = 1, .disp32 = 1, .disp64 = 1 }
2042};
40fb9820
L
2043
2044enum operand_type
2045{
2046 reg,
40fb9820
L
2047 imm,
2048 disp,
2049 anymem
2050};
2051
c6fb90c8 2052static INLINE int
40fb9820
L
2053operand_type_check (i386_operand_type t, enum operand_type c)
2054{
2055 switch (c)
2056 {
2057 case reg:
bab6aec1 2058 return t.bitfield.class == Reg;
40fb9820 2059
40fb9820
L
2060 case imm:
2061 return (t.bitfield.imm8
2062 || t.bitfield.imm8s
2063 || t.bitfield.imm16
2064 || t.bitfield.imm32
2065 || t.bitfield.imm32s
2066 || t.bitfield.imm64);
2067
2068 case disp:
2069 return (t.bitfield.disp8
2070 || t.bitfield.disp16
2071 || t.bitfield.disp32
40fb9820
L
2072 || t.bitfield.disp64);
2073
2074 case anymem:
2075 return (t.bitfield.disp8
2076 || t.bitfield.disp16
2077 || t.bitfield.disp32
40fb9820
L
2078 || t.bitfield.disp64
2079 || t.bitfield.baseindex);
2080
2081 default:
2082 abort ();
2083 }
2cfe26b6
AM
2084
2085 return 0;
40fb9820
L
2086}
2087
7a54636a
L
2088/* Return 1 if there is no conflict in 8bit/16bit/32bit/64bit/80bit size
2089 between operand GIVEN and opeand WANTED for instruction template T. */
5c07affc
L
2090
2091static INLINE int
7a54636a
L
2092match_operand_size (const insn_template *t, unsigned int wanted,
2093 unsigned int given)
5c07affc 2094{
3ac21baa
JB
2095 return !((i.types[given].bitfield.byte
2096 && !t->operand_types[wanted].bitfield.byte)
2097 || (i.types[given].bitfield.word
2098 && !t->operand_types[wanted].bitfield.word)
2099 || (i.types[given].bitfield.dword
2100 && !t->operand_types[wanted].bitfield.dword)
2101 || (i.types[given].bitfield.qword
9db83a32
JB
2102 && (!t->operand_types[wanted].bitfield.qword
2103 /* Don't allow 64-bit (memory) operands outside of 64-bit
2104 mode, when they're used where a 64-bit GPR could also
2105 be used. Checking is needed for Intel Syntax only. */
2106 || (intel_syntax
2107 && flag_code != CODE_64BIT
2108 && (t->operand_types[wanted].bitfield.class == Reg
2109 || t->operand_types[wanted].bitfield.class == Accum
2110 || t->opcode_modifier.isstring))))
3ac21baa
JB
2111 || (i.types[given].bitfield.tbyte
2112 && !t->operand_types[wanted].bitfield.tbyte));
5c07affc
L
2113}
2114
dd40ce22
L
2115/* Return 1 if there is no conflict in SIMD register between operand
2116 GIVEN and opeand WANTED for instruction template T. */
1b54b8d7
JB
2117
2118static INLINE int
dd40ce22
L
2119match_simd_size (const insn_template *t, unsigned int wanted,
2120 unsigned int given)
1b54b8d7 2121{
3ac21baa
JB
2122 return !((i.types[given].bitfield.xmmword
2123 && !t->operand_types[wanted].bitfield.xmmword)
2124 || (i.types[given].bitfield.ymmword
2125 && !t->operand_types[wanted].bitfield.ymmword)
2126 || (i.types[given].bitfield.zmmword
260cd341
LC
2127 && !t->operand_types[wanted].bitfield.zmmword)
2128 || (i.types[given].bitfield.tmmword
2129 && !t->operand_types[wanted].bitfield.tmmword));
1b54b8d7
JB
2130}
2131
7a54636a
L
2132/* Return 1 if there is no conflict in any size between operand GIVEN
2133 and opeand WANTED for instruction template T. */
5c07affc
L
2134
2135static INLINE int
dd40ce22
L
2136match_mem_size (const insn_template *t, unsigned int wanted,
2137 unsigned int given)
5c07affc 2138{
7a54636a 2139 return (match_operand_size (t, wanted, given)
3ac21baa 2140 && !((i.types[given].bitfield.unspecified
5273a3cd 2141 && !i.broadcast.type
a5748e0d 2142 && !i.broadcast.bytes
3ac21baa
JB
2143 && !t->operand_types[wanted].bitfield.unspecified)
2144 || (i.types[given].bitfield.fword
2145 && !t->operand_types[wanted].bitfield.fword)
1b54b8d7
JB
2146 /* For scalar opcode templates to allow register and memory
2147 operands at the same time, some special casing is needed
d6793fa1
JB
2148 here. Also for v{,p}broadcast*, {,v}pmov{s,z}*, and
2149 down-conversion vpmov*. */
3528c362 2150 || ((t->operand_types[wanted].bitfield.class == RegSIMD
bc49bfd8
JB
2151 && t->operand_types[wanted].bitfield.byte
2152 + t->operand_types[wanted].bitfield.word
2153 + t->operand_types[wanted].bitfield.dword
2154 + t->operand_types[wanted].bitfield.qword
2155 > !!t->opcode_modifier.broadcast)
3ac21baa
JB
2156 ? (i.types[given].bitfield.xmmword
2157 || i.types[given].bitfield.ymmword
2158 || i.types[given].bitfield.zmmword)
2159 : !match_simd_size(t, wanted, given))));
5c07affc
L
2160}
2161
3ac21baa
JB
2162/* Return value has MATCH_STRAIGHT set if there is no size conflict on any
2163 operands for instruction template T, and it has MATCH_REVERSE set if there
2164 is no size conflict on any operands for the template with operands reversed
2165 (and the template allows for reversing in the first place). */
5c07affc 2166
3ac21baa
JB
2167#define MATCH_STRAIGHT 1
2168#define MATCH_REVERSE 2
2169
2170static INLINE unsigned int
d3ce72d0 2171operand_size_match (const insn_template *t)
5c07affc 2172{
3ac21baa 2173 unsigned int j, match = MATCH_STRAIGHT;
5c07affc 2174
0cfa3eb3 2175 /* Don't check non-absolute jump instructions. */
5c07affc 2176 if (t->opcode_modifier.jump
0cfa3eb3 2177 && t->opcode_modifier.jump != JUMP_ABSOLUTE)
5c07affc
L
2178 return match;
2179
2180 /* Check memory and accumulator operand size. */
2181 for (j = 0; j < i.operands; j++)
2182 {
3528c362
JB
2183 if (i.types[j].bitfield.class != Reg
2184 && i.types[j].bitfield.class != RegSIMD
255571cd 2185 && t->opcode_modifier.operandconstraint == ANY_SIZE)
5c07affc
L
2186 continue;
2187
bab6aec1 2188 if (t->operand_types[j].bitfield.class == Reg
7a54636a 2189 && !match_operand_size (t, j, j))
5c07affc
L
2190 {
2191 match = 0;
2192 break;
2193 }
2194
3528c362 2195 if (t->operand_types[j].bitfield.class == RegSIMD
3ac21baa 2196 && !match_simd_size (t, j, j))
1b54b8d7
JB
2197 {
2198 match = 0;
2199 break;
2200 }
2201
75e5731b 2202 if (t->operand_types[j].bitfield.instance == Accum
7a54636a 2203 && (!match_operand_size (t, j, j) || !match_simd_size (t, j, j)))
1b54b8d7
JB
2204 {
2205 match = 0;
2206 break;
2207 }
2208
c48dadc9 2209 if ((i.flags[j] & Operand_Mem) && !match_mem_size (t, j, j))
5c07affc
L
2210 {
2211 match = 0;
2212 break;
2213 }
2214 }
2215
3ac21baa 2216 if (!t->opcode_modifier.d)
7b94647a 2217 return match;
5c07affc
L
2218
2219 /* Check reverse. */
aa180741 2220 gas_assert (i.operands >= 2);
5c07affc 2221
f5eb1d70 2222 for (j = 0; j < i.operands; j++)
5c07affc 2223 {
f5eb1d70
JB
2224 unsigned int given = i.operands - j - 1;
2225
aa180741 2226 /* For FMA4 and XOP insns VEX.W controls just the first two
8bd915b7 2227 register operands. */
734dfd1c 2228 if (is_cpu (t, CpuFMA4) || is_cpu (t, CpuXOP))
8bd915b7
JB
2229 given = j < 2 ? 1 - j : j;
2230
bab6aec1 2231 if (t->operand_types[j].bitfield.class == Reg
f5eb1d70 2232 && !match_operand_size (t, j, given))
7b94647a 2233 return match;
5c07affc 2234
3528c362 2235 if (t->operand_types[j].bitfield.class == RegSIMD
f5eb1d70 2236 && !match_simd_size (t, j, given))
7b94647a 2237 return match;
dbbc8b7e 2238
75e5731b 2239 if (t->operand_types[j].bitfield.instance == Accum
f5eb1d70
JB
2240 && (!match_operand_size (t, j, given)
2241 || !match_simd_size (t, j, given)))
7b94647a 2242 return match;
dbbc8b7e 2243
f5eb1d70 2244 if ((i.flags[given] & Operand_Mem) && !match_mem_size (t, j, given))
7b94647a 2245 return match;
5c07affc
L
2246 }
2247
3ac21baa 2248 return match | MATCH_REVERSE;
5c07affc
L
2249}
2250
c6fb90c8 2251static INLINE int
40fb9820
L
2252operand_type_match (i386_operand_type overlap,
2253 i386_operand_type given)
2254{
2255 i386_operand_type temp = overlap;
2256
7d5e4556 2257 temp.bitfield.unspecified = 0;
5c07affc
L
2258 temp.bitfield.byte = 0;
2259 temp.bitfield.word = 0;
2260 temp.bitfield.dword = 0;
2261 temp.bitfield.fword = 0;
2262 temp.bitfield.qword = 0;
2263 temp.bitfield.tbyte = 0;
2264 temp.bitfield.xmmword = 0;
c0f3af97 2265 temp.bitfield.ymmword = 0;
43234a1e 2266 temp.bitfield.zmmword = 0;
260cd341 2267 temp.bitfield.tmmword = 0;
0dfbf9d7 2268 if (operand_type_all_zero (&temp))
891edac4 2269 goto mismatch;
40fb9820 2270
6f2f06be 2271 if (given.bitfield.baseindex == overlap.bitfield.baseindex)
891edac4
L
2272 return 1;
2273
dc1e8a47 2274 mismatch:
a65babc9 2275 i.error = operand_type_mismatch;
891edac4 2276 return 0;
40fb9820
L
2277}
2278
7d5e4556 2279/* If given types g0 and g1 are registers they must be of the same type
10c17abd 2280 unless the expected operand type register overlap is null.
8ee52bcf 2281 Intel syntax sized memory operands are also checked here. */
40fb9820 2282
c6fb90c8 2283static INLINE int
dc821c5f 2284operand_type_register_match (i386_operand_type g0,
40fb9820 2285 i386_operand_type t0,
40fb9820
L
2286 i386_operand_type g1,
2287 i386_operand_type t1)
2288{
bab6aec1 2289 if (g0.bitfield.class != Reg
3528c362 2290 && g0.bitfield.class != RegSIMD
8ee52bcf
JB
2291 && (g0.bitfield.unspecified
2292 || !operand_type_check (g0, anymem)))
40fb9820
L
2293 return 1;
2294
bab6aec1 2295 if (g1.bitfield.class != Reg
3528c362 2296 && g1.bitfield.class != RegSIMD
8ee52bcf
JB
2297 && (g1.bitfield.unspecified
2298 || !operand_type_check (g1, anymem)))
40fb9820
L
2299 return 1;
2300
dc821c5f
JB
2301 if (g0.bitfield.byte == g1.bitfield.byte
2302 && g0.bitfield.word == g1.bitfield.word
2303 && g0.bitfield.dword == g1.bitfield.dword
10c17abd
JB
2304 && g0.bitfield.qword == g1.bitfield.qword
2305 && g0.bitfield.xmmword == g1.bitfield.xmmword
2306 && g0.bitfield.ymmword == g1.bitfield.ymmword
2307 && g0.bitfield.zmmword == g1.bitfield.zmmword)
40fb9820
L
2308 return 1;
2309
c4d09633
JB
2310 /* If expectations overlap in no more than a single size, all is fine. */
2311 g0 = operand_type_and (t0, t1);
2312 if (g0.bitfield.byte
2313 + g0.bitfield.word
2314 + g0.bitfield.dword
2315 + g0.bitfield.qword
2316 + g0.bitfield.xmmword
2317 + g0.bitfield.ymmword
2318 + g0.bitfield.zmmword <= 1)
891edac4
L
2319 return 1;
2320
a65babc9 2321 i.error = register_type_mismatch;
891edac4
L
2322
2323 return 0;
40fb9820
L
2324}
2325
4c692bc7
JB
2326static INLINE unsigned int
2327register_number (const reg_entry *r)
2328{
2329 unsigned int nr = r->reg_num;
2330
2331 if (r->reg_flags & RegRex)
2332 nr += 8;
2333
200cbe0f
L
2334 if (r->reg_flags & RegVRex)
2335 nr += 16;
2336
4c692bc7
JB
2337 return nr;
2338}
2339
252b5132 2340static INLINE unsigned int
40fb9820 2341mode_from_disp_size (i386_operand_type t)
252b5132 2342{
b5014f7a 2343 if (t.bitfield.disp8)
40fb9820
L
2344 return 1;
2345 else if (t.bitfield.disp16
a775efc8 2346 || t.bitfield.disp32)
40fb9820
L
2347 return 2;
2348 else
2349 return 0;
252b5132
RH
2350}
2351
2352static INLINE int
65879393 2353fits_in_signed_byte (addressT num)
252b5132 2354{
65879393 2355 return num + 0x80 <= 0xff;
47926f60 2356}
252b5132
RH
2357
2358static INLINE int
65879393 2359fits_in_unsigned_byte (addressT num)
252b5132 2360{
65879393 2361 return num <= 0xff;
47926f60 2362}
252b5132
RH
2363
2364static INLINE int
65879393 2365fits_in_unsigned_word (addressT num)
252b5132 2366{
65879393 2367 return num <= 0xffff;
47926f60 2368}
252b5132
RH
2369
2370static INLINE int
65879393 2371fits_in_signed_word (addressT num)
252b5132 2372{
65879393 2373 return num + 0x8000 <= 0xffff;
47926f60 2374}
2a962e6d 2375
3e73aa7c 2376static INLINE int
65879393 2377fits_in_signed_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2378{
2379#ifndef BFD64
2380 return 1;
2381#else
65879393 2382 return num + 0x80000000 <= 0xffffffff;
3e73aa7c
JH
2383#endif
2384} /* fits_in_signed_long() */
2a962e6d 2385
3e73aa7c 2386static INLINE int
65879393 2387fits_in_unsigned_long (addressT num ATTRIBUTE_UNUSED)
3e73aa7c
JH
2388{
2389#ifndef BFD64
2390 return 1;
2391#else
65879393 2392 return num <= 0xffffffff;
3e73aa7c
JH
2393#endif
2394} /* fits_in_unsigned_long() */
252b5132 2395
a442cac5
JB
2396static INLINE valueT extend_to_32bit_address (addressT num)
2397{
2398#ifdef BFD64
2399 if (fits_in_unsigned_long(num))
2400 return (num ^ ((addressT) 1 << 31)) - ((addressT) 1 << 31);
2401
2402 if (!fits_in_signed_long (num))
2403 return num & 0xffffffff;
2404#endif
2405
2406 return num;
2407}
2408
43234a1e 2409static INLINE int
b5014f7a 2410fits_in_disp8 (offsetT num)
43234a1e
L
2411{
2412 int shift = i.memshift;
2413 unsigned int mask;
2414
2415 if (shift == -1)
2416 abort ();
2417
2418 mask = (1 << shift) - 1;
2419
2420 /* Return 0 if NUM isn't properly aligned. */
2421 if ((num & mask))
2422 return 0;
2423
2424 /* Check if NUM will fit in 8bit after shift. */
2425 return fits_in_signed_byte (num >> shift);
2426}
2427
a683cc34
SP
2428static INLINE int
2429fits_in_imm4 (offsetT num)
2430{
0ff3b7d0
JB
2431 /* Despite the name, check for imm3 if we're dealing with EVEX. */
2432 return (num & (i.vec_encoding != vex_encoding_evex ? 0xf : 7)) == num;
a683cc34
SP
2433}
2434
40fb9820 2435static i386_operand_type
e3bb37b5 2436smallest_imm_type (offsetT num)
252b5132 2437{
40fb9820 2438 i386_operand_type t;
7ab9ffdd 2439
0dfbf9d7 2440 operand_type_set (&t, 0);
40fb9820
L
2441 t.bitfield.imm64 = 1;
2442
2443 if (cpu_arch_tune != PROCESSOR_I486 && num == 1)
e413e4e9
AM
2444 {
2445 /* This code is disabled on the 486 because all the Imm1 forms
2446 in the opcode table are slower on the i486. They're the
2447 versions with the implicitly specified single-position
2448 displacement, which has another syntax if you really want to
2449 use that form. */
40fb9820
L
2450 t.bitfield.imm1 = 1;
2451 t.bitfield.imm8 = 1;
2452 t.bitfield.imm8s = 1;
2453 t.bitfield.imm16 = 1;
2454 t.bitfield.imm32 = 1;
2455 t.bitfield.imm32s = 1;
2456 }
2457 else if (fits_in_signed_byte (num))
2458 {
c34d1cc9
JB
2459 if (fits_in_unsigned_byte (num))
2460 t.bitfield.imm8 = 1;
40fb9820
L
2461 t.bitfield.imm8s = 1;
2462 t.bitfield.imm16 = 1;
2463 t.bitfield.imm32 = 1;
2464 t.bitfield.imm32s = 1;
2465 }
2466 else if (fits_in_unsigned_byte (num))
2467 {
2468 t.bitfield.imm8 = 1;
2469 t.bitfield.imm16 = 1;
2470 t.bitfield.imm32 = 1;
2471 t.bitfield.imm32s = 1;
2472 }
2473 else if (fits_in_signed_word (num) || fits_in_unsigned_word (num))
2474 {
2475 t.bitfield.imm16 = 1;
2476 t.bitfield.imm32 = 1;
2477 t.bitfield.imm32s = 1;
2478 }
2479 else if (fits_in_signed_long (num))
2480 {
2481 t.bitfield.imm32 = 1;
2482 t.bitfield.imm32s = 1;
2483 }
2484 else if (fits_in_unsigned_long (num))
2485 t.bitfield.imm32 = 1;
2486
2487 return t;
47926f60 2488}
252b5132 2489
847f7ad4 2490static offsetT
e3bb37b5 2491offset_in_range (offsetT val, int size)
847f7ad4 2492{
508866be 2493 addressT mask;
ba2adb93 2494
847f7ad4
AM
2495 switch (size)
2496 {
508866be
L
2497 case 1: mask = ((addressT) 1 << 8) - 1; break;
2498 case 2: mask = ((addressT) 1 << 16) - 1; break;
3e73aa7c 2499#ifdef BFD64
64965897 2500 case 4: mask = ((addressT) 1 << 32) - 1; break;
3e73aa7c 2501#endif
64965897 2502 case sizeof (val): return val;
47926f60 2503 default: abort ();
847f7ad4
AM
2504 }
2505
4fe51f7d 2506 if ((val & ~mask) != 0 && (-val & ~mask) != 0)
f493c217
AM
2507 as_warn (_("0x%" PRIx64 " shortened to 0x%" PRIx64),
2508 (uint64_t) val, (uint64_t) (val & mask));
847f7ad4 2509
847f7ad4
AM
2510 return val & mask;
2511}
2512
76d3f746
JB
2513static INLINE const char *insn_name (const insn_template *t)
2514{
5c139202 2515 return &i386_mnemonics[t->mnem_off];
76d3f746
JB
2516}
2517
c32fa91d
L
2518enum PREFIX_GROUP
2519{
2520 PREFIX_EXIST = 0,
2521 PREFIX_LOCK,
2522 PREFIX_REP,
04ef582a 2523 PREFIX_DS,
c32fa91d
L
2524 PREFIX_OTHER
2525};
2526
2527/* Returns
2528 a. PREFIX_EXIST if attempting to add a prefix where one from the
2529 same class already exists.
2530 b. PREFIX_LOCK if lock prefix is added.
2531 c. PREFIX_REP if rep/repne prefix is added.
04ef582a
L
2532 d. PREFIX_DS if ds prefix is added.
2533 e. PREFIX_OTHER if other prefix is added.
c32fa91d
L
2534 */
2535
2536static enum PREFIX_GROUP
e3bb37b5 2537add_prefix (unsigned int prefix)
252b5132 2538{
c32fa91d 2539 enum PREFIX_GROUP ret = PREFIX_OTHER;
b1905489 2540 unsigned int q;
252b5132 2541
29b0f896
AM
2542 if (prefix >= REX_OPCODE && prefix < REX_OPCODE + 16
2543 && flag_code == CODE_64BIT)
b1905489 2544 {
161a04f6 2545 if ((i.prefix[REX_PREFIX] & prefix & REX_W)
44846f29
JB
2546 || (i.prefix[REX_PREFIX] & prefix & REX_R)
2547 || (i.prefix[REX_PREFIX] & prefix & REX_X)
2548 || (i.prefix[REX_PREFIX] & prefix & REX_B))
c32fa91d 2549 ret = PREFIX_EXIST;
b1905489
JB
2550 q = REX_PREFIX;
2551 }
3e73aa7c 2552 else
b1905489
JB
2553 {
2554 switch (prefix)
2555 {
2556 default:
2557 abort ();
2558
b1905489 2559 case DS_PREFIX_OPCODE:
04ef582a
L
2560 ret = PREFIX_DS;
2561 /* Fall through. */
2562 case CS_PREFIX_OPCODE:
b1905489
JB
2563 case ES_PREFIX_OPCODE:
2564 case FS_PREFIX_OPCODE:
2565 case GS_PREFIX_OPCODE:
2566 case SS_PREFIX_OPCODE:
2567 q = SEG_PREFIX;
2568 break;
2569
2570 case REPNE_PREFIX_OPCODE:
2571 case REPE_PREFIX_OPCODE:
c32fa91d
L
2572 q = REP_PREFIX;
2573 ret = PREFIX_REP;
2574 break;
2575
b1905489 2576 case LOCK_PREFIX_OPCODE:
c32fa91d
L
2577 q = LOCK_PREFIX;
2578 ret = PREFIX_LOCK;
b1905489
JB
2579 break;
2580
2581 case FWAIT_OPCODE:
2582 q = WAIT_PREFIX;
2583 break;
2584
2585 case ADDR_PREFIX_OPCODE:
2586 q = ADDR_PREFIX;
2587 break;
2588
2589 case DATA_PREFIX_OPCODE:
2590 q = DATA_PREFIX;
2591 break;
2592 }
2593 if (i.prefix[q] != 0)
c32fa91d 2594 ret = PREFIX_EXIST;
b1905489 2595 }
252b5132 2596
b1905489 2597 if (ret)
252b5132 2598 {
b1905489
JB
2599 if (!i.prefix[q])
2600 ++i.prefixes;
2601 i.prefix[q] |= prefix;
252b5132 2602 }
b1905489
JB
2603 else
2604 as_bad (_("same type of prefix used twice"));
252b5132 2605
252b5132
RH
2606 return ret;
2607}
2608
2609static void
78f12dd3 2610update_code_flag (int value, int check)
eecb386c 2611{
b44fef84
JB
2612 PRINTF_LIKE ((*as_error)) = check ? as_fatal : as_bad;
2613
2614 if (value == CODE_64BIT && !cpu_arch_flags.bitfield.cpulm )
2615 {
2616 as_error (_("64bit mode not supported on `%s'."),
2617 cpu_arch_name ? cpu_arch_name : default_arch);
2618 return;
2619 }
2620
2621 if (value == CODE_32BIT && !cpu_arch_flags.bitfield.cpui386)
2622 {
2623 as_error (_("32bit mode not supported on `%s'."),
2624 cpu_arch_name ? cpu_arch_name : default_arch);
2625 return;
2626 }
78f12dd3 2627
1e9cc1c2 2628 flag_code = (enum flag_code) value;
40fb9820
L
2629 if (flag_code == CODE_64BIT)
2630 {
2631 cpu_arch_flags.bitfield.cpu64 = 1;
2632 cpu_arch_flags.bitfield.cpuno64 = 0;
40fb9820
L
2633 }
2634 else
2635 {
2636 cpu_arch_flags.bitfield.cpu64 = 0;
2637 cpu_arch_flags.bitfield.cpuno64 = 1;
40fb9820 2638 }
b44fef84 2639
eecb386c
AM
2640 stackop_size = '\0';
2641}
2642
78f12dd3
L
2643static void
2644set_code_flag (int value)
2645{
2646 update_code_flag (value, 0);
2647}
2648
eecb386c 2649static void
e3bb37b5 2650set_16bit_gcc_code_flag (int new_code_flag)
252b5132 2651{
1e9cc1c2 2652 flag_code = (enum flag_code) new_code_flag;
40fb9820
L
2653 if (flag_code != CODE_16BIT)
2654 abort ();
2655 cpu_arch_flags.bitfield.cpu64 = 0;
2656 cpu_arch_flags.bitfield.cpuno64 = 1;
9306ca4a 2657 stackop_size = LONG_MNEM_SUFFIX;
252b5132
RH
2658}
2659
2660static void
e3bb37b5 2661set_intel_syntax (int syntax_flag)
252b5132
RH
2662{
2663 /* Find out if register prefixing is specified. */
2664 int ask_naked_reg = 0;
2665
2666 SKIP_WHITESPACE ();
29b0f896 2667 if (!is_end_of_line[(unsigned char) *input_line_pointer])
252b5132 2668 {
d02603dc
NC
2669 char *string;
2670 int e = get_symbol_name (&string);
252b5132 2671
47926f60 2672 if (strcmp (string, "prefix") == 0)
252b5132 2673 ask_naked_reg = 1;
47926f60 2674 else if (strcmp (string, "noprefix") == 0)
252b5132
RH
2675 ask_naked_reg = -1;
2676 else
d0b47220 2677 as_bad (_("bad argument to syntax directive."));
d02603dc 2678 (void) restore_line_pointer (e);
252b5132
RH
2679 }
2680 demand_empty_rest_of_line ();
c3332e24 2681
252b5132
RH
2682 intel_syntax = syntax_flag;
2683
2684 if (ask_naked_reg == 0)
f86103b7
AM
2685 allow_naked_reg = (intel_syntax
2686 && (bfd_get_symbol_leading_char (stdoutput) != '\0'));
252b5132
RH
2687 else
2688 allow_naked_reg = (ask_naked_reg < 0);
9306ca4a 2689
ee86248c 2690 expr_set_rank (O_full_ptr, syntax_flag ? 10 : 0);
7ab9ffdd 2691
e4a3b5a4 2692 register_prefix = allow_naked_reg ? "" : "%";
252b5132
RH
2693}
2694
1efbbeb4
L
2695static void
2696set_intel_mnemonic (int mnemonic_flag)
2697{
e1d4d893 2698 intel_mnemonic = mnemonic_flag;
1efbbeb4
L
2699}
2700
db51cc60
L
2701static void
2702set_allow_index_reg (int flag)
2703{
2704 allow_index_reg = flag;
2705}
2706
cb19c032 2707static void
7bab8ab5 2708set_check (int what)
cb19c032 2709{
7bab8ab5
JB
2710 enum check_kind *kind;
2711 const char *str;
2712
2713 if (what)
2714 {
2715 kind = &operand_check;
2716 str = "operand";
2717 }
2718 else
2719 {
2720 kind = &sse_check;
2721 str = "sse";
2722 }
2723
cb19c032
L
2724 SKIP_WHITESPACE ();
2725
2726 if (!is_end_of_line[(unsigned char) *input_line_pointer])
2727 {
d02603dc
NC
2728 char *string;
2729 int e = get_symbol_name (&string);
cb19c032
L
2730
2731 if (strcmp (string, "none") == 0)
7bab8ab5 2732 *kind = check_none;
cb19c032 2733 else if (strcmp (string, "warning") == 0)
7bab8ab5 2734 *kind = check_warning;
cb19c032 2735 else if (strcmp (string, "error") == 0)
7bab8ab5 2736 *kind = check_error;
cb19c032 2737 else
7bab8ab5 2738 as_bad (_("bad argument to %s_check directive."), str);
d02603dc 2739 (void) restore_line_pointer (e);
cb19c032
L
2740 }
2741 else
7bab8ab5 2742 as_bad (_("missing argument for %s_check directive"), str);
cb19c032
L
2743
2744 demand_empty_rest_of_line ();
2745}
2746
8a9036a4
L
2747static void
2748check_cpu_arch_compatible (const char *name ATTRIBUTE_UNUSED,
1e9cc1c2 2749 i386_cpu_flags new_flag ATTRIBUTE_UNUSED)
8a9036a4
L
2750{
2751#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2752 static const char *arch;
2753
c085ab00 2754 /* Intel MCU is only supported on ELF. */
8a9036a4
L
2755 if (!IS_ELF)
2756 return;
2757
2758 if (!arch)
2759 {
2760 /* Use cpu_arch_name if it is set in md_parse_option. Otherwise
2761 use default_arch. */
2762 arch = cpu_arch_name;
2763 if (!arch)
2764 arch = default_arch;
2765 }
2766
81486035 2767 /* If we are targeting Intel MCU, we must enable it. */
648d04db
JB
2768 if ((get_elf_backend_data (stdoutput)->elf_machine_code == EM_IAMCU)
2769 == new_flag.bitfield.cpuiamcu)
81486035
L
2770 return;
2771
8a9036a4
L
2772 as_bad (_("`%s' is not supported on `%s'"), name, arch);
2773#endif
2774}
2775
8180707f
JB
2776static void
2777extend_cpu_sub_arch_name (const char *name)
2778{
2779 if (cpu_sub_arch_name)
2780 cpu_sub_arch_name = reconcat (cpu_sub_arch_name, cpu_sub_arch_name,
ae89daec 2781 ".", name, (const char *) NULL);
8180707f 2782 else
ae89daec 2783 cpu_sub_arch_name = concat (".", name, (const char *) NULL);
8180707f
JB
2784}
2785
e413e4e9 2786static void
e3bb37b5 2787set_cpu_arch (int dummy ATTRIBUTE_UNUSED)
e413e4e9 2788{
f68697e8
JB
2789 typedef struct arch_stack_entry
2790 {
2791 const struct arch_stack_entry *prev;
2792 const char *name;
2793 char *sub_name;
2794 i386_cpu_flags flags;
2795 i386_cpu_flags isa_flags;
2796 enum processor_type isa;
2797 enum flag_code flag_code;
4fc85f37 2798 unsigned int vector_size;
f68697e8
JB
2799 char stackop_size;
2800 bool no_cond_jump_promotion;
2801 } arch_stack_entry;
2802 static const arch_stack_entry *arch_stack_top;
dfab07b9
JB
2803 char *s;
2804 int e;
2805 const char *string;
2806 unsigned int j = 0;
2807 i386_cpu_flags flags;
f68697e8 2808
47926f60 2809 SKIP_WHITESPACE ();
e413e4e9 2810
dfab07b9
JB
2811 if (is_end_of_line[(unsigned char) *input_line_pointer])
2812 {
2813 as_bad (_("missing cpu architecture"));
2814 input_line_pointer++;
2815 return;
2816 }
2817
2818 e = get_symbol_name (&s);
2819 string = s;
2820
2821 if (strcmp (string, "push") == 0)
e413e4e9 2822 {
dfab07b9 2823 arch_stack_entry *top = XNEW (arch_stack_entry);
e413e4e9 2824
dfab07b9
JB
2825 top->name = cpu_arch_name;
2826 if (cpu_sub_arch_name)
2827 top->sub_name = xstrdup (cpu_sub_arch_name);
2828 else
2829 top->sub_name = NULL;
2830 top->flags = cpu_arch_flags;
2831 top->isa = cpu_arch_isa;
2832 top->isa_flags = cpu_arch_isa_flags;
2833 top->flag_code = flag_code;
4fc85f37 2834 top->vector_size = vector_size;
dfab07b9
JB
2835 top->stackop_size = stackop_size;
2836 top->no_cond_jump_promotion = no_cond_jump_promotion;
2837
2838 top->prev = arch_stack_top;
2839 arch_stack_top = top;
2840
2841 (void) restore_line_pointer (e);
2842 demand_empty_rest_of_line ();
2843 return;
2844 }
2845
2846 if (strcmp (string, "pop") == 0)
2847 {
2848 const arch_stack_entry *top = arch_stack_top;
2849
2850 if (!top)
2851 as_bad (_(".arch stack is empty"));
2852 else if (top->flag_code != flag_code
2853 || top->stackop_size != stackop_size)
2854 {
2855 static const unsigned int bits[] = {
2856 [CODE_16BIT] = 16,
2857 [CODE_32BIT] = 32,
2858 [CODE_64BIT] = 64,
2859 };
2860
2861 as_bad (_("this `.arch pop' requires `.code%u%s' to be in effect"),
2862 bits[top->flag_code],
2863 top->stackop_size == LONG_MNEM_SUFFIX ? "gcc" : "");
2864 }
2865 else
3ce2ebcf 2866 {
dfab07b9
JB
2867 arch_stack_top = top->prev;
2868
2869 cpu_arch_name = top->name;
2870 free (cpu_sub_arch_name);
2871 cpu_sub_arch_name = top->sub_name;
2872 cpu_arch_flags = top->flags;
2873 cpu_arch_isa = top->isa;
2874 cpu_arch_isa_flags = top->isa_flags;
4fc85f37 2875 vector_size = top->vector_size;
dfab07b9
JB
2876 no_cond_jump_promotion = top->no_cond_jump_promotion;
2877
2878 XDELETE (top);
2879 }
2880
2881 (void) restore_line_pointer (e);
2882 demand_empty_rest_of_line ();
2883 return;
2884 }
2885
2886 if (strcmp (string, "default") == 0)
2887 {
2888 if (strcmp (default_arch, "iamcu") == 0)
2889 string = default_arch;
2890 else
2891 {
2892 static const i386_cpu_flags cpu_unknown_flags = CPU_UNKNOWN_FLAGS;
2893
2894 cpu_arch_name = NULL;
2895 free (cpu_sub_arch_name);
2896 cpu_sub_arch_name = NULL;
2897 cpu_arch_flags = cpu_unknown_flags;
2898 if (flag_code == CODE_64BIT)
2899 {
2900 cpu_arch_flags.bitfield.cpu64 = 1;
2901 cpu_arch_flags.bitfield.cpuno64 = 0;
2902 }
3ce2ebcf
JB
2903 else
2904 {
dfab07b9
JB
2905 cpu_arch_flags.bitfield.cpu64 = 0;
2906 cpu_arch_flags.bitfield.cpuno64 = 1;
2907 }
2908 cpu_arch_isa = PROCESSOR_UNKNOWN;
2909 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
2910 if (!cpu_arch_tune_set)
2911 {
2912 cpu_arch_tune = cpu_arch_isa;
2913 cpu_arch_tune_flags = cpu_arch_isa_flags;
2914 }
2915
4fc85f37
JB
2916 vector_size = VSZ_DEFAULT;
2917
dfab07b9
JB
2918 j = ARRAY_SIZE (cpu_arch) + 1;
2919 }
2920 }
2921
2922 for (; j < ARRAY_SIZE (cpu_arch); j++)
2923 {
2924 if (strcmp (string + (*string == '.'), cpu_arch[j].name) == 0
2925 && (*string == '.') == (cpu_arch[j].type == PROCESSOR_NONE))
2926 {
2927 if (*string != '.')
2928 {
2929 check_cpu_arch_compatible (string, cpu_arch[j].enable);
3ce2ebcf 2930
dfab07b9 2931 cpu_arch_name = cpu_arch[j].name;
3ce2ebcf
JB
2932 free (cpu_sub_arch_name);
2933 cpu_sub_arch_name = NULL;
dfab07b9 2934 cpu_arch_flags = cpu_arch[j].enable;
3ce2ebcf
JB
2935 if (flag_code == CODE_64BIT)
2936 {
2937 cpu_arch_flags.bitfield.cpu64 = 1;
2938 cpu_arch_flags.bitfield.cpuno64 = 0;
2939 }
2940 else
2941 {
2942 cpu_arch_flags.bitfield.cpu64 = 0;
2943 cpu_arch_flags.bitfield.cpuno64 = 1;
2944 }
dfab07b9
JB
2945 cpu_arch_isa = cpu_arch[j].type;
2946 cpu_arch_isa_flags = cpu_arch[j].enable;
3ce2ebcf
JB
2947 if (!cpu_arch_tune_set)
2948 {
2949 cpu_arch_tune = cpu_arch_isa;
2950 cpu_arch_tune_flags = cpu_arch_isa_flags;
2951 }
4fc85f37
JB
2952
2953 vector_size = VSZ_DEFAULT;
2954
dfab07b9
JB
2955 pre_386_16bit_warned = false;
2956 break;
3ce2ebcf 2957 }
f68697e8 2958
dfab07b9
JB
2959 if (cpu_flags_all_zero (&cpu_arch[j].enable))
2960 continue;
f68697e8 2961
dfab07b9 2962 flags = cpu_flags_or (cpu_arch_flags, cpu_arch[j].enable);
f68697e8 2963
dfab07b9 2964 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
f68697e8 2965 {
dfab07b9
JB
2966 extend_cpu_sub_arch_name (string + 1);
2967 cpu_arch_flags = flags;
2968 cpu_arch_isa_flags = flags;
f68697e8
JB
2969 }
2970 else
dfab07b9
JB
2971 cpu_arch_isa_flags
2972 = cpu_flags_or (cpu_arch_isa_flags, cpu_arch[j].enable);
f68697e8
JB
2973
2974 (void) restore_line_pointer (e);
4fc85f37
JB
2975
2976 switch (cpu_arch[j].vsz)
2977 {
2978 default:
2979 break;
2980
2981 case vsz_set:
2982#ifdef SVR4_COMMENT_CHARS
2983 if (*input_line_pointer == ':' || *input_line_pointer == '/')
2984#else
2985 if (*input_line_pointer == '/')
2986#endif
2987 {
2988 ++input_line_pointer;
2989 switch (get_absolute_expression ())
2990 {
2991 case 512: vector_size = VSZ512; break;
2992 case 256: vector_size = VSZ256; break;
2993 case 128: vector_size = VSZ128; break;
2994 default:
2995 as_bad (_("Unrecognized vector size specifier"));
2996 ignore_rest_of_line ();
2997 return;
2998 }
2999 break;
3000 }
3001 /* Fall through. */
3002 case vsz_reset:
3003 vector_size = VSZ_DEFAULT;
3004 break;
3005 }
3006
f68697e8
JB
3007 demand_empty_rest_of_line ();
3008 return;
3009 }
dfab07b9 3010 }
3ce2ebcf 3011
dfab07b9
JB
3012 if (startswith (string, ".no") && j >= ARRAY_SIZE (cpu_arch))
3013 {
3014 /* Disable an ISA extension. */
3015 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
3016 if (cpu_arch[j].type == PROCESSOR_NONE
3017 && strcmp (string + 3, cpu_arch[j].name) == 0)
3018 {
3019 flags = cpu_flags_and_not (cpu_arch_flags, cpu_arch[j].disable);
3020 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
293f5f65 3021 {
dfab07b9
JB
3022 extend_cpu_sub_arch_name (string + 1);
3023 cpu_arch_flags = flags;
3024 cpu_arch_isa_flags = flags;
293f5f65 3025 }
e413e4e9 3026
4fc85f37
JB
3027 if (cpu_arch[j].vsz == vsz_set)
3028 vector_size = VSZ_DEFAULT;
3029
dfab07b9
JB
3030 (void) restore_line_pointer (e);
3031 demand_empty_rest_of_line ();
3032 return;
3033 }
e413e4e9 3034 }
dfab07b9
JB
3035
3036 if (j == ARRAY_SIZE (cpu_arch))
3037 as_bad (_("no such architecture: `%s'"), string);
3038
3039 *input_line_pointer = e;
e413e4e9 3040
fddf5b5b
AM
3041 no_cond_jump_promotion = 0;
3042 if (*input_line_pointer == ','
29b0f896 3043 && !is_end_of_line[(unsigned char) input_line_pointer[1]])
fddf5b5b 3044 {
d02603dc 3045 ++input_line_pointer;
dfab07b9
JB
3046 e = get_symbol_name (&s);
3047 string = s;
fddf5b5b
AM
3048
3049 if (strcmp (string, "nojumps") == 0)
3050 no_cond_jump_promotion = 1;
3051 else if (strcmp (string, "jumps") == 0)
3052 ;
3053 else
3054 as_bad (_("no such architecture modifier: `%s'"), string);
3055
d02603dc 3056 (void) restore_line_pointer (e);
fddf5b5b
AM
3057 }
3058
e413e4e9
AM
3059 demand_empty_rest_of_line ();
3060}
3061
8a9036a4
L
3062enum bfd_architecture
3063i386_arch (void)
3064{
c085ab00 3065 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
3066 {
3067 if (OUTPUT_FLAVOR != bfd_target_elf_flavour
3068 || flag_code == CODE_64BIT)
3069 as_fatal (_("Intel MCU is 32bit ELF only"));
3070 return bfd_arch_iamcu;
3071 }
8a9036a4
L
3072 else
3073 return bfd_arch_i386;
3074}
3075
b9d79e03 3076unsigned long
7016a5d5 3077i386_mach (void)
b9d79e03 3078{
d34049e8 3079 if (startswith (default_arch, "x86_64"))
8a9036a4 3080 {
c085ab00 3081 if (default_arch[6] == '\0')
8a9036a4 3082 return bfd_mach_x86_64;
351f65ca
L
3083 else
3084 return bfd_mach_x64_32;
8a9036a4 3085 }
5197d474
L
3086 else if (!strcmp (default_arch, "i386")
3087 || !strcmp (default_arch, "iamcu"))
81486035
L
3088 {
3089 if (cpu_arch_isa == PROCESSOR_IAMCU)
3090 {
3091 if (OUTPUT_FLAVOR != bfd_target_elf_flavour)
3092 as_fatal (_("Intel MCU is 32bit ELF only"));
3093 return bfd_mach_i386_iamcu;
3094 }
3095 else
3096 return bfd_mach_i386_i386;
3097 }
b9d79e03 3098 else
2b5d6a91 3099 as_fatal (_("unknown architecture"));
b9d79e03 3100}
b9d79e03 3101\f
99f0fb12
JB
3102#include "opcodes/i386-tbl.h"
3103
252b5132 3104void
7016a5d5 3105md_begin (void)
252b5132 3106{
86fa6981
L
3107 /* Support pseudo prefixes like {disp32}. */
3108 lex_type ['{'] = LEX_BEGIN_NAME;
3109
47926f60 3110 /* Initialize op_hash hash table. */
629310ab 3111 op_hash = str_htab_create ();
252b5132
RH
3112
3113 {
65f440c8
JB
3114 const insn_template *const *sets = i386_op_sets;
3115 const insn_template *const *end = sets + ARRAY_SIZE (i386_op_sets) - 1;
3116
3117 /* Type checks to compensate for the conversion through void * which
3118 occurs during hash table insertion / lookup. */
eb993861
JB
3119 (void) sizeof (sets == &current_templates->start);
3120 (void) sizeof (end == &current_templates->end);
65f440c8 3121 for (; sets < end; ++sets)
76d3f746
JB
3122 if (str_hash_insert (op_hash, insn_name (*sets), sets, 0))
3123 as_fatal (_("duplicate %s"), insn_name (*sets));
252b5132
RH
3124 }
3125
47926f60 3126 /* Initialize reg_hash hash table. */
629310ab 3127 reg_hash = str_htab_create ();
252b5132 3128 {
29b0f896 3129 const reg_entry *regtab;
c3fe08fa 3130 unsigned int regtab_size = i386_regtab_size;
252b5132 3131
c3fe08fa 3132 for (regtab = i386_regtab; regtab_size--; regtab++)
6225c532 3133 {
6288d05f
JB
3134 switch (regtab->reg_type.bitfield.class)
3135 {
3136 case Reg:
34684862
JB
3137 if (regtab->reg_type.bitfield.dword)
3138 {
3139 if (regtab->reg_type.bitfield.instance == Accum)
3140 reg_eax = regtab;
3141 }
3142 else if (regtab->reg_type.bitfield.tbyte)
6288d05f
JB
3143 {
3144 /* There's no point inserting st(<N>) in the hash table, as
3145 parentheses aren't included in register_chars[] anyway. */
3146 if (regtab->reg_type.bitfield.instance != Accum)
3147 continue;
3148 reg_st0 = regtab;
3149 }
3150 break;
3151
5e042380
JB
3152 case SReg:
3153 switch (regtab->reg_num)
3154 {
3155 case 0: reg_es = regtab; break;
3156 case 2: reg_ss = regtab; break;
3157 case 3: reg_ds = regtab; break;
3158 }
3159 break;
3160
6288d05f
JB
3161 case RegMask:
3162 if (!regtab->reg_num)
3163 reg_k0 = regtab;
3164 break;
3165 }
3166
6225c532
JB
3167 if (str_hash_insert (reg_hash, regtab->reg_name, regtab, 0) != NULL)
3168 as_fatal (_("duplicate %s"), regtab->reg_name);
6225c532 3169 }
252b5132
RH
3170 }
3171
47926f60 3172 /* Fill in lexical tables: mnemonic_chars, operand_chars. */
252b5132 3173 {
29b0f896 3174 int c;
d2b1a14d 3175 const char *p;
252b5132
RH
3176
3177 for (c = 0; c < 256; c++)
3178 {
014fbcda 3179 if (ISDIGIT (c) || ISLOWER (c))
252b5132
RH
3180 {
3181 mnemonic_chars[c] = c;
3182 register_chars[c] = c;
3183 operand_chars[c] = c;
3184 }
3882b010 3185 else if (ISUPPER (c))
252b5132 3186 {
3882b010 3187 mnemonic_chars[c] = TOLOWER (c);
252b5132
RH
3188 register_chars[c] = mnemonic_chars[c];
3189 operand_chars[c] = c;
3190 }
b3983e5f
JB
3191#ifdef SVR4_COMMENT_CHARS
3192 else if (c == '\\' && strchr (i386_comment_chars, '/'))
3193 operand_chars[c] = c;
3194#endif
252b5132 3195
4795cd4a
JB
3196 if (c >= 128)
3197 operand_chars[c] = c;
252b5132
RH
3198 }
3199
c0f3af97 3200 mnemonic_chars['_'] = '_';
791fe849 3201 mnemonic_chars['-'] = '-';
0003779b 3202 mnemonic_chars['.'] = '.';
252b5132 3203
d2b1a14d
JB
3204 for (p = extra_symbol_chars; *p != '\0'; p++)
3205 operand_chars[(unsigned char) *p] = *p;
252b5132
RH
3206 for (p = operand_special_chars; *p != '\0'; p++)
3207 operand_chars[(unsigned char) *p] = *p;
3208 }
3209
a4447b93
RH
3210 if (flag_code == CODE_64BIT)
3211 {
ca19b261
KT
3212#if defined (OBJ_COFF) && defined (TE_PE)
3213 x86_dwarf2_return_column = (OUTPUT_FLAVOR == bfd_target_coff_flavour
3214 ? 32 : 16);
3215#else
a4447b93 3216 x86_dwarf2_return_column = 16;
ca19b261 3217#endif
61ff971f 3218 x86_cie_data_alignment = -8;
b52c4ee4
IB
3219#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3220 x86_sframe_cfa_sp_reg = 7;
3221 x86_sframe_cfa_fp_reg = 6;
3222#endif
a4447b93
RH
3223 }
3224 else
3225 {
3226 x86_dwarf2_return_column = 8;
3227 x86_cie_data_alignment = -4;
3228 }
e379e5f3
L
3229
3230 /* NB: FUSED_JCC_PADDING frag must have sufficient room so that it
3231 can be turned into BRANCH_PREFIX frag. */
3232 if (align_branch_prefix_size > MAX_FUSED_JCC_PADDING_SIZE)
3233 abort ();
252b5132
RH
3234}
3235
3236void
e3bb37b5 3237i386_print_statistics (FILE *file)
252b5132 3238{
629310ab
ML
3239 htab_print_statistics (file, "i386 opcode", op_hash);
3240 htab_print_statistics (file, "i386 register", reg_hash);
252b5132 3241}
654d6f31
AM
3242
3243void
3244i386_md_end (void)
3245{
3246 htab_delete (op_hash);
3247 htab_delete (reg_hash);
3248}
252b5132 3249\f
252b5132
RH
3250#ifdef DEBUG386
3251
ce8a8b2f 3252/* Debugging routines for md_assemble. */
d3ce72d0 3253static void pte (insn_template *);
40fb9820 3254static void pt (i386_operand_type);
e3bb37b5
L
3255static void pe (expressionS *);
3256static void ps (symbolS *);
252b5132
RH
3257
3258static void
2c703856 3259pi (const char *line, i386_insn *x)
252b5132 3260{
09137c09 3261 unsigned int j;
252b5132
RH
3262
3263 fprintf (stdout, "%s: template ", line);
3264 pte (&x->tm);
09f131f2
JH
3265 fprintf (stdout, " address: base %s index %s scale %x\n",
3266 x->base_reg ? x->base_reg->reg_name : "none",
3267 x->index_reg ? x->index_reg->reg_name : "none",
3268 x->log2_scale_factor);
3269 fprintf (stdout, " modrm: mode %x reg %x reg/mem %x\n",
252b5132 3270 x->rm.mode, x->rm.reg, x->rm.regmem);
09f131f2
JH
3271 fprintf (stdout, " sib: base %x index %x scale %x\n",
3272 x->sib.base, x->sib.index, x->sib.scale);
3273 fprintf (stdout, " rex: 64bit %x extX %x extY %x extZ %x\n",
161a04f6
L
3274 (x->rex & REX_W) != 0,
3275 (x->rex & REX_R) != 0,
3276 (x->rex & REX_X) != 0,
3277 (x->rex & REX_B) != 0);
09137c09 3278 for (j = 0; j < x->operands; j++)
252b5132 3279 {
09137c09
SP
3280 fprintf (stdout, " #%d: ", j + 1);
3281 pt (x->types[j]);
252b5132 3282 fprintf (stdout, "\n");
bab6aec1 3283 if (x->types[j].bitfield.class == Reg
3528c362
JB
3284 || x->types[j].bitfield.class == RegMMX
3285 || x->types[j].bitfield.class == RegSIMD
dd6b8a0b 3286 || x->types[j].bitfield.class == RegMask
00cee14f 3287 || x->types[j].bitfield.class == SReg
4a5c67ed
JB
3288 || x->types[j].bitfield.class == RegCR
3289 || x->types[j].bitfield.class == RegDR
dd6b8a0b
JB
3290 || x->types[j].bitfield.class == RegTR
3291 || x->types[j].bitfield.class == RegBND)
09137c09
SP
3292 fprintf (stdout, "%s\n", x->op[j].regs->reg_name);
3293 if (operand_type_check (x->types[j], imm))
3294 pe (x->op[j].imms);
3295 if (operand_type_check (x->types[j], disp))
3296 pe (x->op[j].disps);
252b5132
RH
3297 }
3298}
3299
3300static void
d3ce72d0 3301pte (insn_template *t)
252b5132 3302{
b933fa4b 3303 static const unsigned char opc_pfx[] = { 0, 0x66, 0xf3, 0xf2 };
441f6aca 3304 static const char *const opc_spc[] = {
0cc78721 3305 NULL, "0f", "0f38", "0f3a", NULL, "evexmap5", "evexmap6", NULL,
441f6aca
JB
3306 "XOP08", "XOP09", "XOP0A",
3307 };
09137c09 3308 unsigned int j;
441f6aca 3309
252b5132 3310 fprintf (stdout, " %d operands ", t->operands);
441f6aca
JB
3311 if (opc_pfx[t->opcode_modifier.opcodeprefix])
3312 fprintf (stdout, "pfx %x ", opc_pfx[t->opcode_modifier.opcodeprefix]);
ddb62495
JB
3313 if (opc_spc[t->opcode_space])
3314 fprintf (stdout, "space %s ", opc_spc[t->opcode_space]);
47926f60 3315 fprintf (stdout, "opcode %x ", t->base_opcode);
252b5132
RH
3316 if (t->extension_opcode != None)
3317 fprintf (stdout, "ext %x ", t->extension_opcode);
40fb9820 3318 if (t->opcode_modifier.d)
252b5132 3319 fprintf (stdout, "D");
40fb9820 3320 if (t->opcode_modifier.w)
252b5132
RH
3321 fprintf (stdout, "W");
3322 fprintf (stdout, "\n");
09137c09 3323 for (j = 0; j < t->operands; j++)
252b5132 3324 {
09137c09
SP
3325 fprintf (stdout, " #%d type ", j + 1);
3326 pt (t->operand_types[j]);
252b5132
RH
3327 fprintf (stdout, "\n");
3328 }
3329}
3330
3331static void
e3bb37b5 3332pe (expressionS *e)
252b5132 3333{
24eab124 3334 fprintf (stdout, " operation %d\n", e->X_op);
b8281767
AM
3335 fprintf (stdout, " add_number %" PRId64 " (%" PRIx64 ")\n",
3336 (int64_t) e->X_add_number, (uint64_t) (valueT) e->X_add_number);
252b5132
RH
3337 if (e->X_add_symbol)
3338 {
3339 fprintf (stdout, " add_symbol ");
3340 ps (e->X_add_symbol);
3341 fprintf (stdout, "\n");
3342 }
3343 if (e->X_op_symbol)
3344 {
3345 fprintf (stdout, " op_symbol ");
3346 ps (e->X_op_symbol);
3347 fprintf (stdout, "\n");
3348 }
3349}
3350
3351static void
e3bb37b5 3352ps (symbolS *s)
252b5132
RH
3353{
3354 fprintf (stdout, "%s type %s%s",
3355 S_GET_NAME (s),
3356 S_IS_EXTERNAL (s) ? "EXTERNAL " : "",
3357 segment_name (S_GET_SEGMENT (s)));
3358}
3359
7b81dfbb 3360static struct type_name
252b5132 3361 {
40fb9820
L
3362 i386_operand_type mask;
3363 const char *name;
252b5132 3364 }
7b81dfbb 3365const type_names[] =
252b5132 3366{
05909f23
JB
3367 { { .bitfield = { .class = Reg, .byte = 1 } }, "r8" },
3368 { { .bitfield = { .class = Reg, .word = 1 } }, "r16" },
3369 { { .bitfield = { .class = Reg, .dword = 1 } }, "r32" },
3370 { { .bitfield = { .class = Reg, .qword = 1 } }, "r64" },
3371 { { .bitfield = { .instance = Accum, .byte = 1 } }, "acc8" },
3372 { { .bitfield = { .instance = Accum, .word = 1 } }, "acc16" },
3373 { { .bitfield = { .instance = Accum, .dword = 1 } }, "acc32" },
3374 { { .bitfield = { .instance = Accum, .qword = 1 } }, "acc64" },
3375 { { .bitfield = { .imm8 = 1 } }, "i8" },
3376 { { .bitfield = { .imm8s = 1 } }, "i8s" },
3377 { { .bitfield = { .imm16 = 1 } }, "i16" },
3378 { { .bitfield = { .imm32 = 1 } }, "i32" },
3379 { { .bitfield = { .imm32s = 1 } }, "i32s" },
3380 { { .bitfield = { .imm64 = 1 } }, "i64" },
3381 { { .bitfield = { .imm1 = 1 } }, "i1" },
3382 { { .bitfield = { .baseindex = 1 } }, "BaseIndex" },
3383 { { .bitfield = { .disp8 = 1 } }, "d8" },
3384 { { .bitfield = { .disp16 = 1 } }, "d16" },
3385 { { .bitfield = { .disp32 = 1 } }, "d32" },
3386 { { .bitfield = { .disp64 = 1 } }, "d64" },
3387 { { .bitfield = { .instance = RegD, .word = 1 } }, "InOutPortReg" },
3388 { { .bitfield = { .instance = RegC, .byte = 1 } }, "ShiftCount" },
3389 { { .bitfield = { .class = RegCR } }, "control reg" },
3390 { { .bitfield = { .class = RegTR } }, "test reg" },
3391 { { .bitfield = { .class = RegDR } }, "debug reg" },
3392 { { .bitfield = { .class = Reg, .tbyte = 1 } }, "FReg" },
3393 { { .bitfield = { .instance = Accum, .tbyte = 1 } }, "FAcc" },
3394 { { .bitfield = { .class = SReg } }, "SReg" },
3395 { { .bitfield = { .class = RegMMX } }, "rMMX" },
3396 { { .bitfield = { .class = RegSIMD, .xmmword = 1 } }, "rXMM" },
3397 { { .bitfield = { .class = RegSIMD, .ymmword = 1 } }, "rYMM" },
3398 { { .bitfield = { .class = RegSIMD, .zmmword = 1 } }, "rZMM" },
3399 { { .bitfield = { .class = RegSIMD, .tmmword = 1 } }, "rTMM" },
3400 { { .bitfield = { .class = RegMask } }, "Mask reg" },
252b5132
RH
3401};
3402
3403static void
40fb9820 3404pt (i386_operand_type t)
252b5132 3405{
40fb9820 3406 unsigned int j;
c6fb90c8 3407 i386_operand_type a;
252b5132 3408
40fb9820 3409 for (j = 0; j < ARRAY_SIZE (type_names); j++)
c6fb90c8
L
3410 {
3411 a = operand_type_and (t, type_names[j].mask);
2c703856 3412 if (operand_type_equal (&a, &type_names[j].mask))
c6fb90c8
L
3413 fprintf (stdout, "%s, ", type_names[j].name);
3414 }
252b5132
RH
3415 fflush (stdout);
3416}
3417
3418#endif /* DEBUG386 */
3419\f
252b5132 3420static bfd_reloc_code_real_type
3956db08 3421reloc (unsigned int size,
64e74474
AM
3422 int pcrel,
3423 int sign,
3424 bfd_reloc_code_real_type other)
252b5132 3425{
47926f60 3426 if (other != NO_RELOC)
3956db08 3427 {
91d6fa6a 3428 reloc_howto_type *rel;
3956db08
JB
3429
3430 if (size == 8)
3431 switch (other)
3432 {
64e74474
AM
3433 case BFD_RELOC_X86_64_GOT32:
3434 return BFD_RELOC_X86_64_GOT64;
3435 break;
553d1284
L
3436 case BFD_RELOC_X86_64_GOTPLT64:
3437 return BFD_RELOC_X86_64_GOTPLT64;
3438 break;
64e74474
AM
3439 case BFD_RELOC_X86_64_PLTOFF64:
3440 return BFD_RELOC_X86_64_PLTOFF64;
3441 break;
3442 case BFD_RELOC_X86_64_GOTPC32:
3443 other = BFD_RELOC_X86_64_GOTPC64;
3444 break;
3445 case BFD_RELOC_X86_64_GOTPCREL:
3446 other = BFD_RELOC_X86_64_GOTPCREL64;
3447 break;
3448 case BFD_RELOC_X86_64_TPOFF32:
3449 other = BFD_RELOC_X86_64_TPOFF64;
3450 break;
3451 case BFD_RELOC_X86_64_DTPOFF32:
3452 other = BFD_RELOC_X86_64_DTPOFF64;
3453 break;
3454 default:
3455 break;
3956db08 3456 }
e05278af 3457
8ce3d284 3458#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
3459 if (other == BFD_RELOC_SIZE32)
3460 {
3461 if (size == 8)
1ab668bf 3462 other = BFD_RELOC_SIZE64;
8fd4256d 3463 if (pcrel)
1ab668bf
AM
3464 {
3465 as_bad (_("there are no pc-relative size relocations"));
3466 return NO_RELOC;
3467 }
8fd4256d 3468 }
8ce3d284 3469#endif
8fd4256d 3470
e05278af 3471 /* Sign-checking 4-byte relocations in 16-/32-bit code is pointless. */
f2d8a97c 3472 if (size == 4 && (flag_code != CODE_64BIT || disallow_64bit_reloc))
e05278af
JB
3473 sign = -1;
3474
91d6fa6a
NC
3475 rel = bfd_reloc_type_lookup (stdoutput, other);
3476 if (!rel)
3956db08 3477 as_bad (_("unknown relocation (%u)"), other);
91d6fa6a 3478 else if (size != bfd_get_reloc_size (rel))
3956db08 3479 as_bad (_("%u-byte relocation cannot be applied to %u-byte field"),
91d6fa6a 3480 bfd_get_reloc_size (rel),
3956db08 3481 size);
91d6fa6a 3482 else if (pcrel && !rel->pc_relative)
3956db08 3483 as_bad (_("non-pc-relative relocation for pc-relative field"));
91d6fa6a 3484 else if ((rel->complain_on_overflow == complain_overflow_signed
3956db08 3485 && !sign)
91d6fa6a 3486 || (rel->complain_on_overflow == complain_overflow_unsigned
64e74474 3487 && sign > 0))
3956db08
JB
3488 as_bad (_("relocated field and relocation type differ in signedness"));
3489 else
3490 return other;
3491 return NO_RELOC;
3492 }
252b5132
RH
3493
3494 if (pcrel)
3495 {
3e73aa7c 3496 if (!sign)
3956db08 3497 as_bad (_("there are no unsigned pc-relative relocations"));
252b5132
RH
3498 switch (size)
3499 {
3500 case 1: return BFD_RELOC_8_PCREL;
3501 case 2: return BFD_RELOC_16_PCREL;
d258b828 3502 case 4: return BFD_RELOC_32_PCREL;
d6ab8113 3503 case 8: return BFD_RELOC_64_PCREL;
252b5132 3504 }
3956db08 3505 as_bad (_("cannot do %u byte pc-relative relocation"), size);
252b5132
RH
3506 }
3507 else
3508 {
3956db08 3509 if (sign > 0)
e5cb08ac 3510 switch (size)
3e73aa7c
JH
3511 {
3512 case 4: return BFD_RELOC_X86_64_32S;
3513 }
3514 else
3515 switch (size)
3516 {
3517 case 1: return BFD_RELOC_8;
3518 case 2: return BFD_RELOC_16;
3519 case 4: return BFD_RELOC_32;
3520 case 8: return BFD_RELOC_64;
3521 }
3956db08
JB
3522 as_bad (_("cannot do %s %u byte relocation"),
3523 sign > 0 ? "signed" : "unsigned", size);
252b5132
RH
3524 }
3525
0cc9e1d3 3526 return NO_RELOC;
252b5132
RH
3527}
3528
47926f60
KH
3529/* Here we decide which fixups can be adjusted to make them relative to
3530 the beginning of the section instead of the symbol. Basically we need
3531 to make sure that the dynamic relocations are done correctly, so in
3532 some cases we force the original symbol to be used. */
3533
252b5132 3534int
e3bb37b5 3535tc_i386_fix_adjustable (fixS *fixP ATTRIBUTE_UNUSED)
252b5132 3536{
6d249963 3537#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 3538 if (!IS_ELF)
31312f95
AM
3539 return 1;
3540
a161fe53
AM
3541 /* Don't adjust pc-relative references to merge sections in 64-bit
3542 mode. */
3543 if (use_rela_relocations
3544 && (S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_MERGE) != 0
3545 && fixP->fx_pcrel)
252b5132 3546 return 0;
31312f95 3547
8d01d9a9
AJ
3548 /* The x86_64 GOTPCREL are represented as 32bit PCrel relocations
3549 and changed later by validate_fix. */
3550 if (GOT_symbol && fixP->fx_subsy == GOT_symbol
3551 && fixP->fx_r_type == BFD_RELOC_32_PCREL)
3552 return 0;
3553
8fd4256d
L
3554 /* Adjust_reloc_syms doesn't know about the GOT. Need to keep symbol
3555 for size relocations. */
3556 if (fixP->fx_r_type == BFD_RELOC_SIZE32
3557 || fixP->fx_r_type == BFD_RELOC_SIZE64
3558 || fixP->fx_r_type == BFD_RELOC_386_GOTOFF
252b5132 3559 || fixP->fx_r_type == BFD_RELOC_386_GOT32
02a86693 3560 || fixP->fx_r_type == BFD_RELOC_386_GOT32X
13ae64f3
JJ
3561 || fixP->fx_r_type == BFD_RELOC_386_TLS_GD
3562 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDM
3563 || fixP->fx_r_type == BFD_RELOC_386_TLS_LDO_32
3564 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE_32
37e55690
JJ
3565 || fixP->fx_r_type == BFD_RELOC_386_TLS_IE
3566 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTIE
13ae64f3
JJ
3567 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE_32
3568 || fixP->fx_r_type == BFD_RELOC_386_TLS_LE
67a4f2b7
AO
3569 || fixP->fx_r_type == BFD_RELOC_386_TLS_GOTDESC
3570 || fixP->fx_r_type == BFD_RELOC_386_TLS_DESC_CALL
3e73aa7c 3571 || fixP->fx_r_type == BFD_RELOC_X86_64_GOT32
80b3ee89 3572 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCREL
56ceb5b5
L
3573 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPCRELX
3574 || fixP->fx_r_type == BFD_RELOC_X86_64_REX_GOTPCRELX
bffbf940
JJ
3575 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSGD
3576 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSLD
3577 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF32
d6ab8113 3578 || fixP->fx_r_type == BFD_RELOC_X86_64_DTPOFF64
bffbf940
JJ
3579 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTTPOFF
3580 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF32
d6ab8113
JB
3581 || fixP->fx_r_type == BFD_RELOC_X86_64_TPOFF64
3582 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTOFF64
67a4f2b7
AO
3583 || fixP->fx_r_type == BFD_RELOC_X86_64_GOTPC32_TLSDESC
3584 || fixP->fx_r_type == BFD_RELOC_X86_64_TLSDESC_CALL
252b5132
RH
3585 || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
3586 || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
3587 return 0;
31312f95 3588#endif
252b5132
RH
3589 return 1;
3590}
252b5132 3591
a9aabc23
JB
3592static INLINE bool
3593want_disp32 (const insn_template *t)
3594{
3595 return flag_code != CODE_64BIT
3596 || i.prefix[ADDR_PREFIX]
7fc69528 3597 || (t->mnem_off == MN_lea
fe134c65
JB
3598 && (!i.types[1].bitfield.qword
3599 || t->opcode_modifier.size == SIZE32));
a9aabc23
JB
3600}
3601
b4cac588 3602static int
e3bb37b5 3603intel_float_operand (const char *mnemonic)
252b5132 3604{
9306ca4a
JB
3605 /* Note that the value returned is meaningful only for opcodes with (memory)
3606 operands, hence the code here is free to improperly handle opcodes that
3607 have no operands (for better performance and smaller code). */
3608
3609 if (mnemonic[0] != 'f')
3610 return 0; /* non-math */
3611
3612 switch (mnemonic[1])
3613 {
3614 /* fclex, fdecstp, fdisi, femms, feni, fincstp, finit, fsetpm, and
3615 the fs segment override prefix not currently handled because no
3616 call path can make opcodes without operands get here */
3617 case 'i':
3618 return 2 /* integer op */;
3619 case 'l':
3620 if (mnemonic[2] == 'd' && (mnemonic[3] == 'c' || mnemonic[3] == 'e'))
3621 return 3; /* fldcw/fldenv */
3622 break;
3623 case 'n':
3624 if (mnemonic[2] != 'o' /* fnop */)
3625 return 3; /* non-waiting control op */
3626 break;
3627 case 'r':
3628 if (mnemonic[2] == 's')
3629 return 3; /* frstor/frstpm */
3630 break;
3631 case 's':
3632 if (mnemonic[2] == 'a')
3633 return 3; /* fsave */
3634 if (mnemonic[2] == 't')
3635 {
3636 switch (mnemonic[3])
3637 {
3638 case 'c': /* fstcw */
3639 case 'd': /* fstdw */
3640 case 'e': /* fstenv */
3641 case 's': /* fsts[gw] */
3642 return 3;
3643 }
3644 }
3645 break;
3646 case 'x':
3647 if (mnemonic[2] == 'r' || mnemonic[2] == 's')
3648 return 0; /* fxsave/fxrstor are not really math ops */
3649 break;
3650 }
252b5132 3651
9306ca4a 3652 return 1;
252b5132
RH
3653}
3654
9a182d04
JB
3655static INLINE void
3656install_template (const insn_template *t)
3657{
3658 unsigned int l;
3659
3660 i.tm = *t;
3661
3662 /* Note that for pseudo prefixes this produces a length of 1. But for them
3663 the length isn't interesting at all. */
3664 for (l = 1; l < 4; ++l)
3665 if (!(t->base_opcode >> (8 * l)))
3666 break;
3667
3668 i.opcode_length = l;
3669}
3670
c0f3af97
L
3671/* Build the VEX prefix. */
3672
3673static void
d3ce72d0 3674build_vex_prefix (const insn_template *t)
c0f3af97
L
3675{
3676 unsigned int register_specifier;
c0f3af97 3677 unsigned int vector_length;
03751133 3678 unsigned int w;
c0f3af97
L
3679
3680 /* Check register specifier. */
3681 if (i.vex.register_specifier)
43234a1e
L
3682 {
3683 register_specifier =
3684 ~register_number (i.vex.register_specifier) & 0xf;
3685 gas_assert ((i.vex.register_specifier->reg_flags & RegVRex) == 0);
3686 }
c0f3af97
L
3687 else
3688 register_specifier = 0xf;
3689
79f0fa25
L
3690 /* Use 2-byte VEX prefix by swapping destination and source operand
3691 if there are more than 1 register operand. */
3692 if (i.reg_operands > 1
3693 && i.vec_encoding != vex_encoding_vex3
86fa6981 3694 && i.dir_encoding == dir_encoding_default
fa99fab2 3695 && i.operands == i.reg_operands
dbbc8b7e 3696 && operand_type_equal (&i.types[0], &i.types[i.operands - 1])
ddb62495 3697 && i.tm.opcode_space == SPACE_0F
dbbc8b7e 3698 && (i.tm.opcode_modifier.load || i.tm.opcode_modifier.d)
fa99fab2
L
3699 && i.rex == REX_B)
3700 {
67748abf 3701 unsigned int xchg;
fa99fab2 3702
67748abf 3703 swap_2_operands (0, i.operands - 1);
fa99fab2 3704
9c2799c2 3705 gas_assert (i.rm.mode == 3);
fa99fab2
L
3706
3707 i.rex = REX_R;
3708 xchg = i.rm.regmem;
3709 i.rm.regmem = i.rm.reg;
3710 i.rm.reg = xchg;
3711
dbbc8b7e
JB
3712 if (i.tm.opcode_modifier.d)
3713 i.tm.base_opcode ^= (i.tm.base_opcode & 0xee) != 0x6e
2c735193 3714 ? Opcode_ExtD : Opcode_SIMD_IntD;
dbbc8b7e 3715 else /* Use the next insn. */
9a182d04 3716 install_template (&t[1]);
fa99fab2
L
3717 }
3718
79dec6b7
JB
3719 /* Use 2-byte VEX prefix by swapping commutative source operands if there
3720 are no memory operands and at least 3 register ones. */
3721 if (i.reg_operands >= 3
3722 && i.vec_encoding != vex_encoding_vex3
3723 && i.reg_operands == i.operands - i.imm_operands
3724 && i.tm.opcode_modifier.vex
3725 && i.tm.opcode_modifier.commutative
33740f21
JB
3726 && (i.tm.opcode_modifier.sse2avx
3727 || (optimize > 1 && !i.no_optimize))
79dec6b7
JB
3728 && i.rex == REX_B
3729 && i.vex.register_specifier
3730 && !(i.vex.register_specifier->reg_flags & RegRex))
3731 {
3732 unsigned int xchg = i.operands - i.reg_operands;
79dec6b7 3733
ddb62495 3734 gas_assert (i.tm.opcode_space == SPACE_0F);
79dec6b7
JB
3735 gas_assert (!i.tm.opcode_modifier.sae);
3736 gas_assert (operand_type_equal (&i.types[i.operands - 2],
3737 &i.types[i.operands - 3]));
3738 gas_assert (i.rm.mode == 3);
3739
67748abf 3740 swap_2_operands (xchg, xchg + 1);
79dec6b7
JB
3741
3742 i.rex = 0;
3743 xchg = i.rm.regmem | 8;
3744 i.rm.regmem = ~register_specifier & 0xf;
3745 gas_assert (!(i.rm.regmem & 8));
3746 i.vex.register_specifier += xchg - i.rm.regmem;
3747 register_specifier = ~xchg & 0xf;
3748 }
3749
539f890d
L
3750 if (i.tm.opcode_modifier.vex == VEXScalar)
3751 vector_length = avxscalar;
10c17abd
JB
3752 else if (i.tm.opcode_modifier.vex == VEX256)
3753 vector_length = 1;
d0c2e3ec
JB
3754 else if (dot_insn () && i.tm.opcode_modifier.vex == VEX128)
3755 vector_length = 0;
539f890d 3756 else
10c17abd 3757 {
56522fc5 3758 unsigned int op;
10c17abd 3759
c7213af9
L
3760 /* Determine vector length from the last multi-length vector
3761 operand. */
10c17abd 3762 vector_length = 0;
56522fc5 3763 for (op = t->operands; op--;)
10c17abd
JB
3764 if (t->operand_types[op].bitfield.xmmword
3765 && t->operand_types[op].bitfield.ymmword
3766 && i.types[op].bitfield.ymmword)
3767 {
3768 vector_length = 1;
3769 break;
3770 }
3771 }
c0f3af97 3772
03751133
L
3773 /* Check the REX.W bit and VEXW. */
3774 if (i.tm.opcode_modifier.vexw == VEXWIG)
3775 w = (vexwig == vexw1 || (i.rex & REX_W)) ? 1 : 0;
3776 else if (i.tm.opcode_modifier.vexw)
3777 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3778 else
931d03b7 3779 w = (flag_code == CODE_64BIT ? i.rex & REX_W : vexwig == vexw1) ? 1 : 0;
03751133 3780
c0f3af97 3781 /* Use 2-byte VEX prefix if possible. */
03751133
L
3782 if (w == 0
3783 && i.vec_encoding != vex_encoding_vex3
ddb62495 3784 && i.tm.opcode_space == SPACE_0F
c0f3af97
L
3785 && (i.rex & (REX_W | REX_X | REX_B)) == 0)
3786 {
3787 /* 2-byte VEX prefix. */
3788 unsigned int r;
3789
3790 i.vex.length = 2;
3791 i.vex.bytes[0] = 0xc5;
3792
3793 /* Check the REX.R bit. */
3794 r = (i.rex & REX_R) ? 0 : 1;
3795 i.vex.bytes[1] = (r << 7
3796 | register_specifier << 3
3797 | vector_length << 2
35648716 3798 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3799 }
3800 else
3801 {
3802 /* 3-byte VEX prefix. */
f88c9eb0 3803 i.vex.length = 3;
f88c9eb0 3804
ddb62495 3805 switch (i.tm.opcode_space)
5dd85c99 3806 {
441f6aca
JB
3807 case SPACE_0F:
3808 case SPACE_0F38:
3809 case SPACE_0F3A:
80de6e00 3810 i.vex.bytes[0] = 0xc4;
7f399153 3811 break;
441f6aca
JB
3812 case SPACE_XOP08:
3813 case SPACE_XOP09:
3814 case SPACE_XOP0A:
f88c9eb0 3815 i.vex.bytes[0] = 0x8f;
7f399153
L
3816 break;
3817 default:
3818 abort ();
f88c9eb0 3819 }
c0f3af97 3820
c0f3af97
L
3821 /* The high 3 bits of the second VEX byte are 1's compliment
3822 of RXB bits from REX. */
d0c2e3ec
JB
3823 i.vex.bytes[1] = ((~i.rex & 7) << 5)
3824 | (!dot_insn () ? i.tm.opcode_space
3825 : i.insn_opcode_space);
c0f3af97 3826
c0f3af97
L
3827 i.vex.bytes[2] = (w << 7
3828 | register_specifier << 3
3829 | vector_length << 2
35648716 3830 | i.tm.opcode_modifier.opcodeprefix);
c0f3af97
L
3831 }
3832}
3833
5b7c81bd 3834static INLINE bool
e771e7c9
JB
3835is_evex_encoding (const insn_template *t)
3836{
7091c612 3837 return t->opcode_modifier.evex || t->opcode_modifier.disp8memshift
e771e7c9 3838 || t->opcode_modifier.broadcast || t->opcode_modifier.masking
a80195f1 3839 || t->opcode_modifier.sae;
e771e7c9
JB
3840}
3841
5b7c81bd 3842static INLINE bool
7a8655d2
JB
3843is_any_vex_encoding (const insn_template *t)
3844{
7b47a312 3845 return t->opcode_modifier.vex || is_evex_encoding (t);
7a8655d2
JB
3846}
3847
a5748e0d
JB
3848static unsigned int
3849get_broadcast_bytes (const insn_template *t, bool diag)
3850{
3851 unsigned int op, bytes;
3852 const i386_operand_type *types;
3853
3854 if (i.broadcast.type)
9b345ce8 3855 return (1 << (t->opcode_modifier.broadcast - 1)) * i.broadcast.type;
a5748e0d
JB
3856
3857 gas_assert (intel_syntax);
3858
3859 for (op = 0; op < t->operands; ++op)
3860 if (t->operand_types[op].bitfield.baseindex)
3861 break;
3862
3863 gas_assert (op < t->operands);
3864
3865 if (t->opcode_modifier.evex
3866 && t->opcode_modifier.evex != EVEXDYN)
3867 switch (i.broadcast.bytes)
3868 {
3869 case 1:
3870 if (t->operand_types[op].bitfield.word)
3871 return 2;
3872 /* Fall through. */
3873 case 2:
3874 if (t->operand_types[op].bitfield.dword)
3875 return 4;
3876 /* Fall through. */
3877 case 4:
3878 if (t->operand_types[op].bitfield.qword)
3879 return 8;
3880 /* Fall through. */
3881 case 8:
3882 if (t->operand_types[op].bitfield.xmmword)
3883 return 16;
3884 if (t->operand_types[op].bitfield.ymmword)
3885 return 32;
3886 if (t->operand_types[op].bitfield.zmmword)
3887 return 64;
3888 /* Fall through. */
3889 default:
3890 abort ();
3891 }
3892
3893 gas_assert (op + 1 < t->operands);
3894
3895 if (t->operand_types[op + 1].bitfield.xmmword
3896 + t->operand_types[op + 1].bitfield.ymmword
3897 + t->operand_types[op + 1].bitfield.zmmword > 1)
3898 {
3899 types = &i.types[op + 1];
3900 diag = false;
3901 }
3902 else /* Ambiguous - guess with a preference to non-AVX512VL forms. */
3903 types = &t->operand_types[op];
3904
3905 if (types->bitfield.zmmword)
3906 bytes = 64;
3907 else if (types->bitfield.ymmword)
3908 bytes = 32;
3909 else
3910 bytes = 16;
3911
3912 if (diag)
3913 as_warn (_("ambiguous broadcast for `%s', using %u-bit form"),
76d3f746 3914 insn_name (t), bytes * 8);
a5748e0d
JB
3915
3916 return bytes;
3917}
3918
43234a1e
L
3919/* Build the EVEX prefix. */
3920
3921static void
3922build_evex_prefix (void)
3923{
35648716 3924 unsigned int register_specifier, w;
43234a1e
L
3925 rex_byte vrex_used = 0;
3926
3927 /* Check register specifier. */
3928 if (i.vex.register_specifier)
3929 {
3930 gas_assert ((i.vrex & REX_X) == 0);
3931
3932 register_specifier = i.vex.register_specifier->reg_num;
3933 if ((i.vex.register_specifier->reg_flags & RegRex))
3934 register_specifier += 8;
3935 /* The upper 16 registers are encoded in the fourth byte of the
3936 EVEX prefix. */
3937 if (!(i.vex.register_specifier->reg_flags & RegVRex))
3938 i.vex.bytes[3] = 0x8;
3939 register_specifier = ~register_specifier & 0xf;
3940 }
3941 else
3942 {
3943 register_specifier = 0xf;
3944
3945 /* Encode upper 16 vector index register in the fourth byte of
3946 the EVEX prefix. */
3947 if (!(i.vrex & REX_X))
3948 i.vex.bytes[3] = 0x8;
3949 else
3950 vrex_used |= REX_X;
3951 }
3952
43234a1e
L
3953 /* 4 byte EVEX prefix. */
3954 i.vex.length = 4;
3955 i.vex.bytes[0] = 0x62;
3956
43234a1e
L
3957 /* The high 3 bits of the second EVEX byte are 1's compliment of RXB
3958 bits from REX. */
ddb62495
JB
3959 gas_assert (i.tm.opcode_space >= SPACE_0F);
3960 gas_assert (i.tm.opcode_space <= SPACE_EVEXMAP6);
d0c2e3ec
JB
3961 i.vex.bytes[1] = ((~i.rex & 7) << 5)
3962 | (!dot_insn () ? i.tm.opcode_space
3963 : i.insn_opcode_space);
43234a1e
L
3964
3965 /* The fifth bit of the second EVEX byte is 1's compliment of the
3966 REX_R bit in VREX. */
3967 if (!(i.vrex & REX_R))
3968 i.vex.bytes[1] |= 0x10;
3969 else
3970 vrex_used |= REX_R;
3971
3972 if ((i.reg_operands + i.imm_operands) == i.operands)
3973 {
3974 /* When all operands are registers, the REX_X bit in REX is not
3975 used. We reuse it to encode the upper 16 registers, which is
3976 indicated by the REX_B bit in VREX. The REX_X bit is encoded
3977 as 1's compliment. */
3978 if ((i.vrex & REX_B))
3979 {
3980 vrex_used |= REX_B;
3981 i.vex.bytes[1] &= ~0x40;
3982 }
3983 }
3984
3985 /* EVEX instructions shouldn't need the REX prefix. */
3986 i.vrex &= ~vrex_used;
3987 gas_assert (i.vrex == 0);
3988
6865c043
L
3989 /* Check the REX.W bit and VEXW. */
3990 if (i.tm.opcode_modifier.vexw == VEXWIG)
3991 w = (evexwig == evexw1 || (i.rex & REX_W)) ? 1 : 0;
3992 else if (i.tm.opcode_modifier.vexw)
3993 w = i.tm.opcode_modifier.vexw == VEXW1 ? 1 : 0;
3994 else
931d03b7 3995 w = (flag_code == CODE_64BIT ? i.rex & REX_W : evexwig == evexw1) ? 1 : 0;
43234a1e 3996
43234a1e 3997 /* The third byte of the EVEX prefix. */
35648716
JB
3998 i.vex.bytes[2] = ((w << 7)
3999 | (register_specifier << 3)
4000 | 4 /* Encode the U bit. */
4001 | i.tm.opcode_modifier.opcodeprefix);
43234a1e
L
4002
4003 /* The fourth byte of the EVEX prefix. */
4004 /* The zeroing-masking bit. */
6225c532 4005 if (i.mask.reg && i.mask.zeroing)
43234a1e
L
4006 i.vex.bytes[3] |= 0x80;
4007
4008 /* Don't always set the broadcast bit if there is no RC. */
ca5312a2 4009 if (i.rounding.type == rc_none)
43234a1e
L
4010 {
4011 /* Encode the vector length. */
4012 unsigned int vec_length;
4013
e771e7c9
JB
4014 if (!i.tm.opcode_modifier.evex
4015 || i.tm.opcode_modifier.evex == EVEXDYN)
4016 {
56522fc5 4017 unsigned int op;
e771e7c9 4018
c7213af9
L
4019 /* Determine vector length from the last multi-length vector
4020 operand. */
56522fc5 4021 for (op = i.operands; op--;)
e771e7c9
JB
4022 if (i.tm.operand_types[op].bitfield.xmmword
4023 + i.tm.operand_types[op].bitfield.ymmword
4024 + i.tm.operand_types[op].bitfield.zmmword > 1)
4025 {
4026 if (i.types[op].bitfield.zmmword)
c7213af9
L
4027 {
4028 i.tm.opcode_modifier.evex = EVEX512;
4029 break;
4030 }
e771e7c9 4031 else if (i.types[op].bitfield.ymmword)
c7213af9
L
4032 {
4033 i.tm.opcode_modifier.evex = EVEX256;
4034 break;
4035 }
e771e7c9 4036 else if (i.types[op].bitfield.xmmword)
c7213af9
L
4037 {
4038 i.tm.opcode_modifier.evex = EVEX128;
4039 break;
4040 }
9b345ce8
JB
4041 else if ((i.broadcast.type || i.broadcast.bytes)
4042 && op == i.broadcast.operand)
625cbd7a 4043 {
a5748e0d 4044 switch (get_broadcast_bytes (&i.tm, true))
625cbd7a
JB
4045 {
4046 case 64:
4047 i.tm.opcode_modifier.evex = EVEX512;
4048 break;
4049 case 32:
4050 i.tm.opcode_modifier.evex = EVEX256;
4051 break;
4052 case 16:
4053 i.tm.opcode_modifier.evex = EVEX128;
4054 break;
4055 default:
c7213af9 4056 abort ();
625cbd7a 4057 }
c7213af9 4058 break;
625cbd7a 4059 }
e771e7c9 4060 }
c7213af9 4061
56522fc5 4062 if (op >= MAX_OPERANDS)
c7213af9 4063 abort ();
e771e7c9
JB
4064 }
4065
43234a1e
L
4066 switch (i.tm.opcode_modifier.evex)
4067 {
4068 case EVEXLIG: /* LL' is ignored */
4069 vec_length = evexlig << 5;
4070 break;
4071 case EVEX128:
4072 vec_length = 0 << 5;
4073 break;
4074 case EVEX256:
4075 vec_length = 1 << 5;
4076 break;
4077 case EVEX512:
4078 vec_length = 2 << 5;
4079 break;
d0c2e3ec
JB
4080 case EVEX_L3:
4081 if (dot_insn ())
4082 {
4083 vec_length = 3 << 5;
4084 break;
4085 }
4086 /* Fall through. */
43234a1e
L
4087 default:
4088 abort ();
4089 break;
4090 }
4091 i.vex.bytes[3] |= vec_length;
4092 /* Encode the broadcast bit. */
9b345ce8 4093 if (i.broadcast.type || i.broadcast.bytes)
43234a1e
L
4094 i.vex.bytes[3] |= 0x10;
4095 }
ca5312a2
JB
4096 else if (i.rounding.type != saeonly)
4097 i.vex.bytes[3] |= 0x10 | (i.rounding.type << 5);
43234a1e 4098 else
ca5312a2 4099 i.vex.bytes[3] |= 0x10 | (evexrcig << 5);
43234a1e 4100
6225c532
JB
4101 if (i.mask.reg)
4102 i.vex.bytes[3] |= i.mask.reg->reg_num;
43234a1e
L
4103}
4104
65da13b5
L
4105static void
4106process_immext (void)
4107{
4108 expressionS *exp;
4109
c0f3af97 4110 /* These AMD 3DNow! and SSE2 instructions have an opcode suffix
65da13b5
L
4111 which is coded in the same place as an 8-bit immediate field
4112 would be. Here we fake an 8-bit immediate operand from the
4113 opcode suffix stored in tm.extension_opcode.
4114
c1e679ec 4115 AVX instructions also use this encoding, for some of
c0f3af97 4116 3 argument instructions. */
65da13b5 4117
43234a1e 4118 gas_assert (i.imm_operands <= 1
7ab9ffdd 4119 && (i.operands <= 2
7a8655d2 4120 || (is_any_vex_encoding (&i.tm)
7ab9ffdd 4121 && i.operands <= 4)));
65da13b5
L
4122
4123 exp = &im_expressions[i.imm_operands++];
4124 i.op[i.operands].imms = exp;
be1643ff 4125 i.types[i.operands].bitfield.imm8 = 1;
65da13b5
L
4126 i.operands++;
4127 exp->X_op = O_constant;
4128 exp->X_add_number = i.tm.extension_opcode;
4129 i.tm.extension_opcode = None;
4130}
4131
42164a71
L
4132
4133static int
4134check_hle (void)
4135{
742732c7 4136 switch (i.tm.opcode_modifier.prefixok)
42164a71
L
4137 {
4138 default:
4139 abort ();
742732c7
JB
4140 case PrefixLock:
4141 case PrefixNone:
4142 case PrefixNoTrack:
4143 case PrefixRep:
165de32a 4144 as_bad (_("invalid instruction `%s' after `%s'"),
76d3f746 4145 insn_name (&i.tm), i.hle_prefix);
42164a71 4146 return 0;
742732c7 4147 case PrefixHLELock:
42164a71
L
4148 if (i.prefix[LOCK_PREFIX])
4149 return 1;
165de32a 4150 as_bad (_("missing `lock' with `%s'"), i.hle_prefix);
42164a71 4151 return 0;
742732c7 4152 case PrefixHLEAny:
42164a71 4153 return 1;
742732c7 4154 case PrefixHLERelease:
42164a71
L
4155 if (i.prefix[HLE_PREFIX] != XRELEASE_PREFIX_OPCODE)
4156 {
4157 as_bad (_("instruction `%s' after `xacquire' not allowed"),
76d3f746 4158 insn_name (&i.tm));
42164a71
L
4159 return 0;
4160 }
8dc0818e 4161 if (i.mem_operands == 0 || !(i.flags[i.operands - 1] & Operand_Mem))
42164a71
L
4162 {
4163 as_bad (_("memory destination needed for instruction `%s'"
76d3f746 4164 " after `xrelease'"), insn_name (&i.tm));
42164a71
L
4165 return 0;
4166 }
4167 return 1;
4168 }
4169}
4170
c8480b58
L
4171/* Encode aligned vector move as unaligned vector move. */
4172
4173static void
4174encode_with_unaligned_vector_move (void)
4175{
4176 switch (i.tm.base_opcode)
4177 {
b3a9fe6f
L
4178 case 0x28: /* Load instructions. */
4179 case 0x29: /* Store instructions. */
c8480b58 4180 /* movaps/movapd/vmovaps/vmovapd. */
ddb62495 4181 if (i.tm.opcode_space == SPACE_0F
c8480b58 4182 && i.tm.opcode_modifier.opcodeprefix <= PREFIX_0X66)
b3a9fe6f 4183 i.tm.base_opcode = 0x10 | (i.tm.base_opcode & 1);
c8480b58 4184 break;
b3a9fe6f
L
4185 case 0x6f: /* Load instructions. */
4186 case 0x7f: /* Store instructions. */
c8480b58 4187 /* movdqa/vmovdqa/vmovdqa64/vmovdqa32. */
ddb62495 4188 if (i.tm.opcode_space == SPACE_0F
c8480b58
L
4189 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0X66)
4190 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
4191 break;
4192 default:
4193 break;
4194 }
4195}
4196
b6f8c7c4
L
4197/* Try the shortest encoding by shortening operand size. */
4198
4199static void
4200optimize_encoding (void)
4201{
a0a1771e 4202 unsigned int j;
b6f8c7c4 4203
7fc69528 4204 if (i.tm.mnem_off == MN_lea)
fe134c65
JB
4205 {
4206 /* Optimize: -O:
4207 lea symbol, %rN -> mov $symbol, %rN
4208 lea (%rM), %rN -> mov %rM, %rN
4209 lea (,%rM,1), %rN -> mov %rM, %rN
4210
4211 and in 32-bit mode for 16-bit addressing
4212
4213 lea (%rM), %rN -> movzx %rM, %rN
4214
4215 and in 64-bit mode zap 32-bit addressing in favor of using a
4216 32-bit (or less) destination.
4217 */
4218 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
4219 {
4220 if (!i.op[1].regs->reg_type.bitfield.word)
4221 i.tm.opcode_modifier.size = SIZE32;
4222 i.prefix[ADDR_PREFIX] = 0;
4223 }
4224
4225 if (!i.index_reg && !i.base_reg)
4226 {
4227 /* Handle:
4228 lea symbol, %rN -> mov $symbol, %rN
4229 */
4230 if (flag_code == CODE_64BIT)
4231 {
4232 /* Don't transform a relocation to a 16-bit one. */
4233 if (i.op[0].disps
4234 && i.op[0].disps->X_op != O_constant
4235 && i.op[1].regs->reg_type.bitfield.word)
4236 return;
4237
4238 if (!i.op[1].regs->reg_type.bitfield.qword
4239 || i.tm.opcode_modifier.size == SIZE32)
4240 {
4241 i.tm.base_opcode = 0xb8;
4242 i.tm.opcode_modifier.modrm = 0;
4243 if (!i.op[1].regs->reg_type.bitfield.word)
4244 i.types[0].bitfield.imm32 = 1;
4245 else
4246 {
4247 i.tm.opcode_modifier.size = SIZE16;
4248 i.types[0].bitfield.imm16 = 1;
4249 }
4250 }
4251 else
4252 {
4253 /* Subject to further optimization below. */
4254 i.tm.base_opcode = 0xc7;
4255 i.tm.extension_opcode = 0;
4256 i.types[0].bitfield.imm32s = 1;
4257 i.types[0].bitfield.baseindex = 0;
4258 }
4259 }
4260 /* Outside of 64-bit mode address and operand sizes have to match if
4261 a relocation is involved, as otherwise we wouldn't (currently) or
4262 even couldn't express the relocation correctly. */
4263 else if (i.op[0].disps
4264 && i.op[0].disps->X_op != O_constant
4265 && ((!i.prefix[ADDR_PREFIX])
4266 != (flag_code == CODE_32BIT
4267 ? i.op[1].regs->reg_type.bitfield.dword
4268 : i.op[1].regs->reg_type.bitfield.word)))
4269 return;
7772f168
JB
4270 /* In 16-bit mode converting LEA with 16-bit addressing and a 32-bit
4271 destination is going to grow encoding size. */
4272 else if (flag_code == CODE_16BIT
4273 && (optimize <= 1 || optimize_for_space)
4274 && !i.prefix[ADDR_PREFIX]
4275 && i.op[1].regs->reg_type.bitfield.dword)
4276 return;
fe134c65
JB
4277 else
4278 {
4279 i.tm.base_opcode = 0xb8;
4280 i.tm.opcode_modifier.modrm = 0;
4281 if (i.op[1].regs->reg_type.bitfield.dword)
4282 i.types[0].bitfield.imm32 = 1;
4283 else
4284 i.types[0].bitfield.imm16 = 1;
4285
4286 if (i.op[0].disps
4287 && i.op[0].disps->X_op == O_constant
4288 && i.op[1].regs->reg_type.bitfield.dword
60cfa10c
L
4289 /* NB: Add () to !i.prefix[ADDR_PREFIX] to silence
4290 GCC 5. */
4291 && (!i.prefix[ADDR_PREFIX]) != (flag_code == CODE_32BIT))
fe134c65
JB
4292 i.op[0].disps->X_add_number &= 0xffff;
4293 }
4294
4295 i.tm.operand_types[0] = i.types[0];
4296 i.imm_operands = 1;
4297 if (!i.op[0].imms)
4298 {
4299 i.op[0].imms = &im_expressions[0];
4300 i.op[0].imms->X_op = O_absent;
4301 }
4302 }
4303 else if (i.op[0].disps
4304 && (i.op[0].disps->X_op != O_constant
4305 || i.op[0].disps->X_add_number))
4306 return;
4307 else
4308 {
4309 /* Handle:
4310 lea (%rM), %rN -> mov %rM, %rN
4311 lea (,%rM,1), %rN -> mov %rM, %rN
4312 lea (%rM), %rN -> movzx %rM, %rN
4313 */
4314 const reg_entry *addr_reg;
4315
4316 if (!i.index_reg && i.base_reg->reg_num != RegIP)
4317 addr_reg = i.base_reg;
4318 else if (!i.base_reg
4319 && i.index_reg->reg_num != RegIZ
4320 && !i.log2_scale_factor)
4321 addr_reg = i.index_reg;
4322 else
4323 return;
4324
4325 if (addr_reg->reg_type.bitfield.word
4326 && i.op[1].regs->reg_type.bitfield.dword)
4327 {
4328 if (flag_code != CODE_32BIT)
4329 return;
ddb62495 4330 i.tm.opcode_space = SPACE_0F;
fe134c65
JB
4331 i.tm.base_opcode = 0xb7;
4332 }
4333 else
4334 i.tm.base_opcode = 0x8b;
4335
4336 if (addr_reg->reg_type.bitfield.dword
4337 && i.op[1].regs->reg_type.bitfield.qword)
4338 i.tm.opcode_modifier.size = SIZE32;
4339
4340 i.op[0].regs = addr_reg;
4341 i.reg_operands = 2;
4342 }
4343
4344 i.mem_operands = 0;
4345 i.disp_operands = 0;
4346 i.prefix[ADDR_PREFIX] = 0;
4347 i.prefix[SEG_PREFIX] = 0;
4348 i.seg[0] = NULL;
4349 }
4350
b6f8c7c4 4351 if (optimize_for_space
7fc69528 4352 && i.tm.mnem_off == MN_test
b6f8c7c4
L
4353 && i.reg_operands == 1
4354 && i.imm_operands == 1
4355 && !i.types[1].bitfield.byte
4356 && i.op[0].imms->X_op == O_constant
7fc69528 4357 && fits_in_imm7 (i.op[0].imms->X_add_number))
b6f8c7c4
L
4358 {
4359 /* Optimize: -Os:
4360 test $imm7, %r64/%r32/%r16 -> test $imm7, %r8
4361 */
4362 unsigned int base_regnum = i.op[1].regs->reg_num;
4363 if (flag_code == CODE_64BIT || base_regnum < 4)
4364 {
4365 i.types[1].bitfield.byte = 1;
4366 /* Ignore the suffix. */
4367 i.suffix = 0;
7697afb6
JB
4368 /* Convert to byte registers. */
4369 if (i.types[1].bitfield.word)
4370 j = 16;
4371 else if (i.types[1].bitfield.dword)
4372 j = 32;
4373 else
4374 j = 48;
4375 if (!(i.op[1].regs->reg_flags & RegRex) && base_regnum < 4)
4376 j += 8;
4377 i.op[1].regs -= j;
b6f8c7c4
L
4378 }
4379 }
4380 else if (flag_code == CODE_64BIT
ddb62495 4381 && i.tm.opcode_space == SPACE_BASE
d3d50934
L
4382 && ((i.types[1].bitfield.qword
4383 && i.reg_operands == 1
b6f8c7c4
L
4384 && i.imm_operands == 1
4385 && i.op[0].imms->X_op == O_constant
507916b8 4386 && ((i.tm.base_opcode == 0xb8
b6f8c7c4
L
4387 && i.tm.extension_opcode == None
4388 && fits_in_unsigned_long (i.op[0].imms->X_add_number))
4389 || (fits_in_imm31 (i.op[0].imms->X_add_number)
7fc69528 4390 && (i.tm.base_opcode == 0x24
b6f8c7c4
L
4391 || (i.tm.base_opcode == 0x80
4392 && i.tm.extension_opcode == 0x4)
7fc69528
JB
4393 || i.tm.mnem_off == MN_test
4394 || ((i.tm.base_opcode | 1) == 0xc7
b8364fa7
JB
4395 && i.tm.extension_opcode == 0x0)))
4396 || (fits_in_imm7 (i.op[0].imms->X_add_number)
4397 && i.tm.base_opcode == 0x83
4398 && i.tm.extension_opcode == 0x4)))
d3d50934
L
4399 || (i.types[0].bitfield.qword
4400 && ((i.reg_operands == 2
4401 && i.op[0].regs == i.op[1].regs
7fc69528
JB
4402 && (i.tm.mnem_off == MN_xor
4403 || i.tm.mnem_off == MN_sub))
4404 || i.tm.mnem_off == MN_clr))))
b6f8c7c4
L
4405 {
4406 /* Optimize: -O:
4407 andq $imm31, %r64 -> andl $imm31, %r32
b8364fa7 4408 andq $imm7, %r64 -> andl $imm7, %r32
b6f8c7c4
L
4409 testq $imm31, %r64 -> testl $imm31, %r32
4410 xorq %r64, %r64 -> xorl %r32, %r32
4411 subq %r64, %r64 -> subl %r32, %r32
4412 movq $imm31, %r64 -> movl $imm31, %r32
4413 movq $imm32, %r64 -> movl $imm32, %r32
4414 */
04784e33
JB
4415 i.tm.opcode_modifier.size = SIZE32;
4416 if (i.imm_operands)
4417 {
4418 i.types[0].bitfield.imm32 = 1;
4419 i.types[0].bitfield.imm32s = 0;
4420 i.types[0].bitfield.imm64 = 0;
4421 }
4422 else
4423 {
4424 i.types[0].bitfield.dword = 1;
4425 i.types[0].bitfield.qword = 0;
4426 }
4427 i.types[1].bitfield.dword = 1;
4428 i.types[1].bitfield.qword = 0;
7fc69528 4429 if (i.tm.mnem_off == MN_mov || i.tm.mnem_off == MN_lea)
b6f8c7c4
L
4430 {
4431 /* Handle
4432 movq $imm31, %r64 -> movl $imm31, %r32
4433 movq $imm32, %r64 -> movl $imm32, %r32
4434 */
4435 i.tm.operand_types[0].bitfield.imm32 = 1;
4436 i.tm.operand_types[0].bitfield.imm32s = 0;
4437 i.tm.operand_types[0].bitfield.imm64 = 0;
507916b8 4438 if ((i.tm.base_opcode | 1) == 0xc7)
b6f8c7c4
L
4439 {
4440 /* Handle
4441 movq $imm31, %r64 -> movl $imm31, %r32
4442 */
507916b8 4443 i.tm.base_opcode = 0xb8;
b6f8c7c4 4444 i.tm.extension_opcode = None;
507916b8 4445 i.tm.opcode_modifier.w = 0;
b6f8c7c4
L
4446 i.tm.opcode_modifier.modrm = 0;
4447 }
4448 }
4449 }
5641ec01
JB
4450 else if (optimize > 1
4451 && !optimize_for_space
4452 && i.reg_operands == 2
4453 && i.op[0].regs == i.op[1].regs
7fc69528 4454 && (i.tm.mnem_off == MN_and || i.tm.mnem_off == MN_or)
5641ec01
JB
4455 && (flag_code != CODE_64BIT || !i.types[0].bitfield.dword))
4456 {
4457 /* Optimize: -O2:
4458 andb %rN, %rN -> testb %rN, %rN
4459 andw %rN, %rN -> testw %rN, %rN
4460 andq %rN, %rN -> testq %rN, %rN
4461 orb %rN, %rN -> testb %rN, %rN
4462 orw %rN, %rN -> testw %rN, %rN
4463 orq %rN, %rN -> testq %rN, %rN
4464
4465 and outside of 64-bit mode
4466
4467 andl %rN, %rN -> testl %rN, %rN
4468 orl %rN, %rN -> testl %rN, %rN
4469 */
4470 i.tm.base_opcode = 0x84 | (i.tm.base_opcode & 1);
4471 }
ad2f4436
JB
4472 else if (i.tm.base_opcode == 0xba
4473 && i.tm.opcode_space == SPACE_0F
4474 && i.reg_operands == 1
4475 && i.op[0].imms->X_op == O_constant
4476 && i.op[0].imms->X_add_number >= 0)
4477 {
4478 /* Optimize: -O:
4479 btw $n, %rN -> btl $n, %rN (outside of 16-bit mode, n < 16)
4480 btq $n, %rN -> btl $n, %rN (in 64-bit mode, n < 32, N < 8)
4481 btl $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
4482
4483 With <BT> one of bts, btr, and bts also:
4484 <BT>w $n, %rN -> btl $n, %rN (in 32-bit mode, n < 16)
4485 <BT>l $n, %rN -> btw $n, %rN (in 16-bit mode, n < 16)
4486 */
4487 switch (flag_code)
4488 {
4489 case CODE_64BIT:
4490 if (i.tm.extension_opcode != 4)
4491 break;
4492 if (i.types[1].bitfield.qword
4493 && i.op[0].imms->X_add_number < 32
4494 && !(i.op[1].regs->reg_flags & RegRex))
4495 i.tm.opcode_modifier.size = SIZE32;
4496 /* Fall through. */
4497 case CODE_32BIT:
4498 if (i.types[1].bitfield.word
4499 && i.op[0].imms->X_add_number < 16)
4500 i.tm.opcode_modifier.size = SIZE32;
4501 break;
4502 case CODE_16BIT:
4503 if (i.op[0].imms->X_add_number < 16)
4504 i.tm.opcode_modifier.size = SIZE16;
4505 break;
4506 }
4507 }
99112332 4508 else if (i.reg_operands == 3
b6f8c7c4
L
4509 && i.op[0].regs == i.op[1].regs
4510 && !i.types[2].bitfield.xmmword
4511 && (i.tm.opcode_modifier.vex
6225c532 4512 || ((!i.mask.reg || i.mask.zeroing)
e771e7c9 4513 && is_evex_encoding (&i.tm)
80c34c38 4514 && (i.vec_encoding != vex_encoding_evex
dd22218c 4515 || cpu_arch_isa_flags.bitfield.cpuavx512vl
734dfd1c 4516 || is_cpu (&i.tm, CpuAVX512VL)
7091c612 4517 || (i.tm.operand_types[2].bitfield.zmmword
dd22218c 4518 && i.types[2].bitfield.ymmword))))
ddb62495 4519 && i.tm.opcode_space == SPACE_0F
5844ccaa
JB
4520 && ((i.tm.base_opcode | 2) == 0x57
4521 || i.tm.base_opcode == 0xdf
4522 || i.tm.base_opcode == 0xef
4523 || (i.tm.base_opcode | 3) == 0xfb
4524 || i.tm.base_opcode == 0x42
4525 || i.tm.base_opcode == 0x47))
b6f8c7c4 4526 {
99112332 4527 /* Optimize: -O1:
8305403a
L
4528 VOP, one of vandnps, vandnpd, vxorps, vxorpd, vpsubb, vpsubd,
4529 vpsubq and vpsubw:
b6f8c7c4
L
4530 EVEX VOP %zmmM, %zmmM, %zmmN
4531 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4532 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4533 EVEX VOP %ymmM, %ymmM, %ymmN
4534 -> VEX VOP %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4535 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4536 VEX VOP %ymmM, %ymmM, %ymmN
4537 -> VEX VOP %xmmM, %xmmM, %xmmN
4538 VOP, one of vpandn and vpxor:
4539 VEX VOP %ymmM, %ymmM, %ymmN
4540 -> VEX VOP %xmmM, %xmmM, %xmmN
4541 VOP, one of vpandnd and vpandnq:
4542 EVEX VOP %zmmM, %zmmM, %zmmN
4543 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4544 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4545 EVEX VOP %ymmM, %ymmM, %ymmN
4546 -> VEX vpandn %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4547 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4548 VOP, one of vpxord and vpxorq:
4549 EVEX VOP %zmmM, %zmmM, %zmmN
4550 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4551 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
b6f8c7c4
L
4552 EVEX VOP %ymmM, %ymmM, %ymmN
4553 -> VEX vpxor %xmmM, %xmmM, %xmmN (M and N < 16)
99112332 4554 -> EVEX VOP %xmmM, %xmmM, %xmmN (M || N >= 16) (-O2)
1424ad86
JB
4555 VOP, one of kxord and kxorq:
4556 VEX VOP %kM, %kM, %kN
4557 -> VEX kxorw %kM, %kM, %kN
4558 VOP, one of kandnd and kandnq:
4559 VEX VOP %kM, %kM, %kN
4560 -> VEX kandnw %kM, %kM, %kN
b6f8c7c4 4561 */
e771e7c9 4562 if (is_evex_encoding (&i.tm))
b6f8c7c4 4563 {
7b1d7ca1 4564 if (i.vec_encoding != vex_encoding_evex)
b6f8c7c4
L
4565 {
4566 i.tm.opcode_modifier.vex = VEX128;
4567 i.tm.opcode_modifier.vexw = VEXW0;
4568 i.tm.opcode_modifier.evex = 0;
4569 }
7b1d7ca1 4570 else if (optimize > 1)
dd22218c
L
4571 i.tm.opcode_modifier.evex = EVEX128;
4572 else
4573 return;
b6f8c7c4 4574 }
f74a6307 4575 else if (i.tm.operand_types[0].bitfield.class == RegMask)
1424ad86 4576 {
35648716 4577 i.tm.opcode_modifier.opcodeprefix = PREFIX_NONE;
1424ad86
JB
4578 i.tm.opcode_modifier.vexw = VEXW0;
4579 }
b6f8c7c4
L
4580 else
4581 i.tm.opcode_modifier.vex = VEX128;
4582
4583 if (i.tm.opcode_modifier.vex)
4584 for (j = 0; j < 3; j++)
4585 {
4586 i.types[j].bitfield.xmmword = 1;
4587 i.types[j].bitfield.ymmword = 0;
4588 }
4589 }
392a5972 4590 else if (i.vec_encoding != vex_encoding_evex
97ed31ae 4591 && !i.types[0].bitfield.zmmword
392a5972 4592 && !i.types[1].bitfield.zmmword
6225c532 4593 && !i.mask.reg
9b345ce8 4594 && !i.broadcast.type
a5748e0d 4595 && !i.broadcast.bytes
97ed31ae 4596 && is_evex_encoding (&i.tm)
35648716
JB
4597 && ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4598 || (i.tm.base_opcode & ~4) == 0xdb
4599 || (i.tm.base_opcode & ~4) == 0xeb)
97ed31ae
L
4600 && i.tm.extension_opcode == None)
4601 {
4602 /* Optimize: -O1:
4603 VOP, one of vmovdqa32, vmovdqa64, vmovdqu8, vmovdqu16,
4604 vmovdqu32 and vmovdqu64:
4605 EVEX VOP %xmmM, %xmmN
4606 -> VEX vmovdqa|vmovdqu %xmmM, %xmmN (M and N < 16)
4607 EVEX VOP %ymmM, %ymmN
4608 -> VEX vmovdqa|vmovdqu %ymmM, %ymmN (M and N < 16)
4609 EVEX VOP %xmmM, mem
4610 -> VEX vmovdqa|vmovdqu %xmmM, mem (M < 16)
4611 EVEX VOP %ymmM, mem
4612 -> VEX vmovdqa|vmovdqu %ymmM, mem (M < 16)
4613 EVEX VOP mem, %xmmN
4614 -> VEX mvmovdqa|vmovdquem, %xmmN (N < 16)
4615 EVEX VOP mem, %ymmN
4616 -> VEX vmovdqa|vmovdqu mem, %ymmN (N < 16)
a0a1771e
JB
4617 VOP, one of vpand, vpandn, vpor, vpxor:
4618 EVEX VOP{d,q} %xmmL, %xmmM, %xmmN
4619 -> VEX VOP %xmmL, %xmmM, %xmmN (L, M, and N < 16)
4620 EVEX VOP{d,q} %ymmL, %ymmM, %ymmN
4621 -> VEX VOP %ymmL, %ymmM, %ymmN (L, M, and N < 16)
4622 EVEX VOP{d,q} mem, %xmmM, %xmmN
4623 -> VEX VOP mem, %xmmM, %xmmN (M and N < 16)
4624 EVEX VOP{d,q} mem, %ymmM, %ymmN
4625 -> VEX VOP mem, %ymmM, %ymmN (M and N < 16)
97ed31ae 4626 */
a0a1771e 4627 for (j = 0; j < i.operands; j++)
392a5972
L
4628 if (operand_type_check (i.types[j], disp)
4629 && i.op[j].disps->X_op == O_constant)
4630 {
4631 /* Since the VEX prefix has 2 or 3 bytes, the EVEX prefix
4632 has 4 bytes, EVEX Disp8 has 1 byte and VEX Disp32 has 4
4633 bytes, we choose EVEX Disp8 over VEX Disp32. */
4634 int evex_disp8, vex_disp8;
4635 unsigned int memshift = i.memshift;
4636 offsetT n = i.op[j].disps->X_add_number;
4637
4638 evex_disp8 = fits_in_disp8 (n);
4639 i.memshift = 0;
4640 vex_disp8 = fits_in_disp8 (n);
4641 if (evex_disp8 != vex_disp8)
4642 {
4643 i.memshift = memshift;
4644 return;
4645 }
4646
4647 i.types[j].bitfield.disp8 = vex_disp8;
4648 break;
4649 }
35648716
JB
4650 if ((i.tm.base_opcode & ~Opcode_SIMD_IntD) == 0x6f
4651 && i.tm.opcode_modifier.opcodeprefix == PREFIX_0XF2)
4652 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
97ed31ae
L
4653 i.tm.opcode_modifier.vex
4654 = i.types[0].bitfield.ymmword ? VEX256 : VEX128;
4655 i.tm.opcode_modifier.vexw = VEXW0;
79dec6b7 4656 /* VPAND, VPOR, and VPXOR are commutative. */
35648716 4657 if (i.reg_operands == 3 && i.tm.base_opcode != 0xdf)
79dec6b7 4658 i.tm.opcode_modifier.commutative = 1;
97ed31ae
L
4659 i.tm.opcode_modifier.evex = 0;
4660 i.tm.opcode_modifier.masking = 0;
a0a1771e 4661 i.tm.opcode_modifier.broadcast = 0;
97ed31ae
L
4662 i.tm.opcode_modifier.disp8memshift = 0;
4663 i.memshift = 0;
a0a1771e
JB
4664 if (j < i.operands)
4665 i.types[j].bitfield.disp8
4666 = fits_in_disp8 (i.op[j].disps->X_add_number);
97ed31ae 4667 }
b5c37946
SJ
4668 else if (optimize_for_space
4669 && i.tm.base_opcode == 0x29
4670 && i.tm.opcode_space == SPACE_0F38
4671 && i.operands == i.reg_operands
4672 && i.op[0].regs == i.op[1].regs
4673 && (!i.tm.opcode_modifier.vex
4674 || !(i.op[0].regs->reg_flags & RegRex))
4675 && !is_evex_encoding (&i.tm))
4676 {
4677 /* Optimize: -Os:
4678 pcmpeqq %xmmN, %xmmN -> pcmpeqd %xmmN, %xmmN
4679 vpcmpeqq %xmmN, %xmmN, %xmmM -> vpcmpeqd %xmmN, %xmmN, %xmmM (N < 8)
4680 vpcmpeqq %ymmN, %ymmN, %ymmM -> vpcmpeqd %ymmN, %ymmN, %ymmM (N < 8)
4681 */
4682 i.tm.opcode_space = SPACE_0F;
4683 i.tm.base_opcode = 0x76;
4684 }
4685 else if (((i.tm.base_opcode >= 0x64
4686 && i.tm.base_opcode <= 0x66
4687 && i.tm.opcode_space == SPACE_0F)
4688 || (i.tm.base_opcode == 0x37
4689 && i.tm.opcode_space == SPACE_0F38))
4690 && i.operands == i.reg_operands
4691 && i.op[0].regs == i.op[1].regs
4692 && !is_evex_encoding (&i.tm))
4693 {
4694 /* Optimize: -O:
4695 pcmpgt[bwd] %mmN, %mmN -> pxor %mmN, %mmN
4696 pcmpgt[bwdq] %xmmN, %xmmN -> pxor %xmmN, %xmmN
4697 vpcmpgt[bwdq] %xmmN, %xmmN, %xmmM -> vpxor %xmmN, %xmmN, %xmmM (N < 8)
4698 vpcmpgt[bwdq] %xmmN, %xmmN, %xmmM -> vpxor %xmm0, %xmm0, %xmmM (N > 7)
4699 vpcmpgt[bwdq] %ymmN, %ymmN, %ymmM -> vpxor %ymmN, %ymmN, %ymmM (N < 8)
4700 vpcmpgt[bwdq] %ymmN, %ymmN, %ymmM -> vpxor %ymm0, %ymm0, %ymmM (N > 7)
4701 */
4702 i.tm.opcode_space = SPACE_0F;
4703 i.tm.base_opcode = 0xef;
4704 if (i.tm.opcode_modifier.vex && (i.op[0].regs->reg_flags & RegRex))
4705 {
4706 if (i.operands == 2)
4707 {
4708 gas_assert (i.tm.opcode_modifier.sse2avx);
4709
4710 i.operands = 3;
4711 i.reg_operands = 3;
4712 i.tm.operands = 3;
4713
4714 i.op[2].regs = i.op[0].regs;
4715 i.types[2] = i.types[0];
4716 i.flags[2] = i.flags[0];
4717 i.tm.operand_types[2] = i.tm.operand_types[0];
4718
4719 i.tm.opcode_modifier.sse2avx = 0;
4720 }
4721 i.op[0].regs -= i.op[0].regs->reg_num + 8;
4722 i.op[1].regs = i.op[0].regs;
4723 }
4724 }
4725 else if (optimize_for_space
4726 && i.tm.base_opcode == 0x59
4727 && i.tm.opcode_space == SPACE_0F38
4728 && i.operands == i.reg_operands
4729 && i.tm.opcode_modifier.vex
4730 && !(i.op[0].regs->reg_flags & RegRex)
4731 && i.op[0].regs->reg_type.bitfield.xmmword
4732 && i.vec_encoding != vex_encoding_vex3)
4733 {
4734 /* Optimize: -Os:
4735 vpbroadcastq %xmmN, %xmmM -> vpunpcklqdq %xmmN, %xmmN, %xmmM (N < 8)
4736 */
4737 i.tm.opcode_space = SPACE_0F;
4738 i.tm.base_opcode = 0x6c;
4739 i.tm.opcode_modifier.vexvvvv = 1;
4740
4741 ++i.operands;
4742 ++i.reg_operands;
4743 ++i.tm.operands;
4744
4745 i.op[2].regs = i.op[0].regs;
4746 i.types[2] = i.types[0];
4747 i.flags[2] = i.flags[0];
4748 i.tm.operand_types[2] = i.tm.operand_types[0];
4749
4750 swap_2_operands (1, 2);
4751 }
b6f8c7c4
L
4752}
4753
ae531041
L
4754/* Return non-zero for load instruction. */
4755
4756static int
4757load_insn_p (void)
4758{
4759 unsigned int dest;
4760 int any_vex_p = is_any_vex_encoding (&i.tm);
4761 unsigned int base_opcode = i.tm.base_opcode | 1;
4762
4763 if (!any_vex_p)
4764 {
ef07be45
CL
4765 /* Anysize insns: lea, invlpg, clflush, prefetch*, bndmk, bndcl, bndcu,
4766 bndcn, bndstx, bndldx, clflushopt, clwb, cldemote. */
255571cd 4767 if (i.tm.opcode_modifier.operandconstraint == ANY_SIZE)
ae531041
L
4768 return 0;
4769
389d00a5 4770 /* pop. */
6d86a545 4771 if (i.tm.mnem_off == MN_pop)
389d00a5
JB
4772 return 1;
4773 }
4774
ddb62495 4775 if (i.tm.opcode_space == SPACE_BASE)
389d00a5
JB
4776 {
4777 /* popf, popa. */
4778 if (i.tm.base_opcode == 0x9d
a09f656b 4779 || i.tm.base_opcode == 0x61)
ae531041
L
4780 return 1;
4781
4782 /* movs, cmps, lods, scas. */
4783 if ((i.tm.base_opcode | 0xb) == 0xaf)
4784 return 1;
4785
a09f656b 4786 /* outs, xlatb. */
4787 if (base_opcode == 0x6f
4788 || i.tm.base_opcode == 0xd7)
ae531041 4789 return 1;
a09f656b 4790 /* NB: For AMD-specific insns with implicit memory operands,
4791 they're intentionally not covered. */
ae531041
L
4792 }
4793
4794 /* No memory operand. */
4795 if (!i.mem_operands)
4796 return 0;
4797
4798 if (any_vex_p)
4799 {
7fc69528 4800 if (i.tm.mnem_off == MN_vldmxcsr)
ae531041
L
4801 return 1;
4802 }
ddb62495 4803 else if (i.tm.opcode_space == SPACE_BASE)
ae531041
L
4804 {
4805 /* test, not, neg, mul, imul, div, idiv. */
aa4c197d 4806 if (base_opcode == 0xf7 && i.tm.extension_opcode != 1)
ae531041
L
4807 return 1;
4808
4809 /* inc, dec. */
4810 if (base_opcode == 0xff && i.tm.extension_opcode <= 1)
4811 return 1;
4812
4813 /* add, or, adc, sbb, and, sub, xor, cmp. */
4814 if (i.tm.base_opcode >= 0x80 && i.tm.base_opcode <= 0x83)
4815 return 1;
4816
ae531041 4817 /* rol, ror, rcl, rcr, shl/sal, shr, sar. */
aa4c197d 4818 if ((base_opcode == 0xc1 || (base_opcode | 2) == 0xd3)
ae531041
L
4819 && i.tm.extension_opcode != 6)
4820 return 1;
4821
ae531041 4822 /* Check for x87 instructions. */
aa4c197d 4823 if ((base_opcode | 6) == 0xdf)
ae531041
L
4824 {
4825 /* Skip fst, fstp, fstenv, fstcw. */
4826 if (i.tm.base_opcode == 0xd9
4827 && (i.tm.extension_opcode == 2
4828 || i.tm.extension_opcode == 3
4829 || i.tm.extension_opcode == 6
4830 || i.tm.extension_opcode == 7))
4831 return 0;
4832
4833 /* Skip fisttp, fist, fistp, fstp. */
4834 if (i.tm.base_opcode == 0xdb
4835 && (i.tm.extension_opcode == 1
4836 || i.tm.extension_opcode == 2
4837 || i.tm.extension_opcode == 3
4838 || i.tm.extension_opcode == 7))
4839 return 0;
4840
4841 /* Skip fisttp, fst, fstp, fsave, fstsw. */
4842 if (i.tm.base_opcode == 0xdd
4843 && (i.tm.extension_opcode == 1
4844 || i.tm.extension_opcode == 2
4845 || i.tm.extension_opcode == 3
4846 || i.tm.extension_opcode == 6
4847 || i.tm.extension_opcode == 7))
4848 return 0;
4849
4850 /* Skip fisttp, fist, fistp, fbstp, fistp. */
4851 if (i.tm.base_opcode == 0xdf
4852 && (i.tm.extension_opcode == 1
4853 || i.tm.extension_opcode == 2
4854 || i.tm.extension_opcode == 3
4855 || i.tm.extension_opcode == 6
4856 || i.tm.extension_opcode == 7))
4857 return 0;
4858
4859 return 1;
4860 }
4861 }
ddb62495 4862 else if (i.tm.opcode_space == SPACE_0F)
389d00a5
JB
4863 {
4864 /* bt, bts, btr, btc. */
4865 if (i.tm.base_opcode == 0xba
aa4c197d 4866 && (i.tm.extension_opcode | 3) == 7)
389d00a5
JB
4867 return 1;
4868
4869 /* cmpxchg8b, cmpxchg16b, xrstors, vmptrld. */
4870 if (i.tm.base_opcode == 0xc7
4871 && i.tm.opcode_modifier.opcodeprefix == PREFIX_NONE
4872 && (i.tm.extension_opcode == 1 || i.tm.extension_opcode == 3
4873 || i.tm.extension_opcode == 6))
4874 return 1;
4875
4876 /* fxrstor, ldmxcsr, xrstor. */
4877 if (i.tm.base_opcode == 0xae
4878 && (i.tm.extension_opcode == 1
4879 || i.tm.extension_opcode == 2
4880 || i.tm.extension_opcode == 5))
4881 return 1;
4882
4883 /* lgdt, lidt, lmsw. */
4884 if (i.tm.base_opcode == 0x01
4885 && (i.tm.extension_opcode == 2
4886 || i.tm.extension_opcode == 3
4887 || i.tm.extension_opcode == 6))
4888 return 1;
4889 }
ae531041
L
4890
4891 dest = i.operands - 1;
4892
4893 /* Check fake imm8 operand and 3 source operands. */
4894 if ((i.tm.opcode_modifier.immext
aa180741 4895 || i.reg_operands + i.mem_operands == 4)
ae531041
L
4896 && i.types[dest].bitfield.imm8)
4897 dest--;
4898
389d00a5 4899 /* add, or, adc, sbb, and, sub, xor, cmp, test, xchg. */
ddb62495 4900 if (i.tm.opcode_space == SPACE_BASE
aa4c197d 4901 && ((base_opcode | 0x38) == 0x39
389d00a5
JB
4902 || (base_opcode | 2) == 0x87))
4903 return 1;
4904
7fc69528 4905 if (i.tm.mnem_off == MN_xadd)
ae531041
L
4906 return 1;
4907
4908 /* Check for load instruction. */
4909 return (i.types[dest].bitfield.class != ClassNone
4910 || i.types[dest].bitfield.instance == Accum);
4911}
4912
4913/* Output lfence, 0xfaee8, after instruction. */
4914
4915static void
4916insert_lfence_after (void)
4917{
4918 if (lfence_after_load && load_insn_p ())
4919 {
a09f656b 4920 /* There are also two REP string instructions that require
4921 special treatment. Specifically, the compare string (CMPS)
4922 and scan string (SCAS) instructions set EFLAGS in a manner
4923 that depends on the data being compared/scanned. When used
4924 with a REP prefix, the number of iterations may therefore
4925 vary depending on this data. If the data is a program secret
4926 chosen by the adversary using an LVI method,
4927 then this data-dependent behavior may leak some aspect
4928 of the secret. */
aa4c197d 4929 if (((i.tm.base_opcode | 0x9) == 0xaf)
a09f656b 4930 && i.prefix[REP_PREFIX])
4931 {
4932 as_warn (_("`%s` changes flags which would affect control flow behavior"),
76d3f746 4933 insn_name (&i.tm));
a09f656b 4934 }
ae531041
L
4935 char *p = frag_more (3);
4936 *p++ = 0xf;
4937 *p++ = 0xae;
4938 *p = 0xe8;
4939 }
4940}
4941
4942/* Output lfence, 0xfaee8, before instruction. */
4943
4944static void
4945insert_lfence_before (void)
4946{
4947 char *p;
4948
ddb62495 4949 if (i.tm.opcode_space != SPACE_BASE)
ae531041
L
4950 return;
4951
4952 if (i.tm.base_opcode == 0xff
4953 && (i.tm.extension_opcode == 2 || i.tm.extension_opcode == 4))
4954 {
4955 /* Insert lfence before indirect branch if needed. */
4956
4957 if (lfence_before_indirect_branch == lfence_branch_none)
4958 return;
4959
4960 if (i.operands != 1)
4961 abort ();
4962
4963 if (i.reg_operands == 1)
4964 {
4965 /* Indirect branch via register. Don't insert lfence with
4966 -mlfence-after-load=yes. */
4967 if (lfence_after_load
4968 || lfence_before_indirect_branch == lfence_branch_memory)
4969 return;
4970 }
4971 else if (i.mem_operands == 1
4972 && lfence_before_indirect_branch != lfence_branch_register)
4973 {
4974 as_warn (_("indirect `%s` with memory operand should be avoided"),
76d3f746 4975 insn_name (&i.tm));
ae531041
L
4976 return;
4977 }
4978 else
4979 return;
4980
4981 if (last_insn.kind != last_insn_other
4982 && last_insn.seg == now_seg)
4983 {
4984 as_warn_where (last_insn.file, last_insn.line,
4985 _("`%s` skips -mlfence-before-indirect-branch on `%s`"),
76d3f746 4986 last_insn.name, insn_name (&i.tm));
ae531041
L
4987 return;
4988 }
4989
4990 p = frag_more (3);
4991 *p++ = 0xf;
4992 *p++ = 0xae;
4993 *p = 0xe8;
4994 return;
4995 }
4996
503648e4 4997 /* Output or/not/shl and lfence before near ret. */
ae531041 4998 if (lfence_before_ret != lfence_before_ret_none
aa4c197d 4999 && (i.tm.base_opcode | 1) == 0xc3)
ae531041
L
5000 {
5001 if (last_insn.kind != last_insn_other
5002 && last_insn.seg == now_seg)
5003 {
5004 as_warn_where (last_insn.file, last_insn.line,
5005 _("`%s` skips -mlfence-before-ret on `%s`"),
76d3f746 5006 last_insn.name, insn_name (&i.tm));
ae531041
L
5007 return;
5008 }
a09f656b 5009
a09f656b 5010 /* Near ret ingore operand size override under CPU64. */
503648e4 5011 char prefix = flag_code == CODE_64BIT
5012 ? 0x48
5013 : i.prefix[DATA_PREFIX] ? 0x66 : 0x0;
a09f656b 5014
5015 if (lfence_before_ret == lfence_before_ret_not)
5016 {
5017 /* not: 0xf71424, may add prefix
5018 for operand size override or 64-bit code. */
5019 p = frag_more ((prefix ? 2 : 0) + 6 + 3);
5020 if (prefix)
5021 *p++ = prefix;
ae531041
L
5022 *p++ = 0xf7;
5023 *p++ = 0x14;
5024 *p++ = 0x24;
a09f656b 5025 if (prefix)
5026 *p++ = prefix;
ae531041
L
5027 *p++ = 0xf7;
5028 *p++ = 0x14;
5029 *p++ = 0x24;
5030 }
a09f656b 5031 else
5032 {
5033 p = frag_more ((prefix ? 1 : 0) + 4 + 3);
5034 if (prefix)
5035 *p++ = prefix;
5036 if (lfence_before_ret == lfence_before_ret_or)
5037 {
5038 /* or: 0x830c2400, may add prefix
5039 for operand size override or 64-bit code. */
5040 *p++ = 0x83;
5041 *p++ = 0x0c;
5042 }
5043 else
5044 {
5045 /* shl: 0xc1242400, may add prefix
5046 for operand size override or 64-bit code. */
5047 *p++ = 0xc1;
5048 *p++ = 0x24;
5049 }
5050
5051 *p++ = 0x24;
5052 *p++ = 0x0;
5053 }
5054
ae531041
L
5055 *p++ = 0xf;
5056 *p++ = 0xae;
5057 *p = 0xe8;
5058 }
5059}
5060
edd67638
JB
5061/* Shared helper for md_assemble() and s_insn(). */
5062static void init_globals (void)
5063{
5064 unsigned int j;
5065
5066 memset (&i, '\0', sizeof (i));
5067 i.rounding.type = rc_none;
5068 for (j = 0; j < MAX_OPERANDS; j++)
5069 i.reloc[j] = NO_RELOC;
5070 memset (disp_expressions, '\0', sizeof (disp_expressions));
5071 memset (im_expressions, '\0', sizeof (im_expressions));
5072 save_stack_p = save_stack;
5073}
5074
04784e33
JB
5075/* Helper for md_assemble() to decide whether to prepare for a possible 2nd
5076 parsing pass. Instead of introducing a rarely use new insn attribute this
5077 utilizes a common pattern between affected templates. It is deemed
5078 acceptable that this will lead to unnecessary pass 2 preparations in a
5079 limited set of cases. */
5080static INLINE bool may_need_pass2 (const insn_template *t)
5081{
5082 return t->opcode_modifier.sse2avx
5083 /* Note that all SSE2AVX templates have at least one operand. */
a28fedbc 5084 ? t->operand_types[t->operands - 1].bitfield.class == RegSIMD
ddb62495 5085 : (t->opcode_space == SPACE_0F
a28fedbc 5086 && (t->base_opcode | 1) == 0xbf)
ddb62495 5087 || (t->opcode_space == SPACE_BASE
a28fedbc 5088 && t->base_opcode == 0x63);
04784e33
JB
5089}
5090
252b5132
RH
5091/* This is the guts of the machine-dependent assembler. LINE points to a
5092 machine dependent instruction. This function is supposed to emit
5093 the frags/bytes it assembles to. */
5094
5095void
65da13b5 5096md_assemble (char *line)
252b5132 5097{
40fb9820 5098 unsigned int j;
9db83a32 5099 char mnemonic[MAX_MNEM_SIZE], mnem_suffix = 0, *copy = NULL;
04784e33
JB
5100 const char *end, *pass1_mnem = NULL;
5101 enum i386_error pass1_err = 0;
d3ce72d0 5102 const insn_template *t;
252b5132 5103
47926f60 5104 /* Initialize globals. */
04784e33
JB
5105 current_templates = NULL;
5106 retry:
edd67638 5107 init_globals ();
252b5132
RH
5108
5109 /* First parse an instruction mnemonic & call i386_operand for the operands.
5110 We assume that the scrubber has arranged it so that line[0] is the valid
47926f60 5111 start of a (possibly prefixed) mnemonic. */
252b5132 5112
edd67638 5113 end = parse_insn (line, mnemonic, false);
5317ad2c 5114 if (end == NULL)
04784e33
JB
5115 {
5116 if (pass1_mnem != NULL)
5117 goto match_error;
9db83a32
JB
5118 if (i.error != no_error)
5119 {
5120 gas_assert (current_templates != NULL);
5121 if (may_need_pass2 (current_templates->start) && !i.suffix)
5122 goto no_match;
5123 /* No point in trying a 2nd pass - it'll only find the same suffix
5124 again. */
5125 mnem_suffix = i.suffix;
5126 goto match_error;
5127 }
04784e33
JB
5128 return;
5129 }
6d86a545
JB
5130 t = current_templates->start;
5131 if (may_need_pass2 (t))
04784e33
JB
5132 {
5133 /* Make a copy of the full line in case we need to retry. */
5134 copy = xstrdup (line);
5135 }
5317ad2c 5136 line += end - line;
83b16ac6 5137 mnem_suffix = i.suffix;
252b5132 5138
29b0f896 5139 line = parse_operands (line, mnemonic);
ee86248c 5140 this_operand = -1;
29b0f896 5141 if (line == NULL)
04784e33
JB
5142 {
5143 free (copy);
5144 return;
5145 }
252b5132 5146
29b0f896
AM
5147 /* Now we've parsed the mnemonic into a set of templates, and have the
5148 operands at hand. */
5149
b630c145 5150 /* All Intel opcodes have reversed operands except for "bound", "enter",
c0e54661 5151 "invlpg*", "monitor*", "mwait*", "tpause", "umwait", "pvalidate",
b0e8fa7f
TJ
5152 "rmpadjust", "rmpupdate", and "rmpquery". We also don't reverse
5153 intersegment "jmp" and "call" instructions with 2 immediate operands so
5154 that the immediate segment precedes the offset consistently in Intel and
5155 AT&T modes. */
4d456e3d
L
5156 if (intel_syntax
5157 && i.operands > 1
6d86a545
JB
5158 && (t->mnem_off != MN_bound)
5159 && !startswith (mnemonic, "invlpg")
d34049e8
ML
5160 && !startswith (mnemonic, "monitor")
5161 && !startswith (mnemonic, "mwait")
6d86a545 5162 && (t->mnem_off != MN_pvalidate)
d34049e8 5163 && !startswith (mnemonic, "rmp")
6d86a545
JB
5164 && (t->mnem_off != MN_tpause)
5165 && (t->mnem_off != MN_umwait)
47c0279b
JB
5166 && !(i.operands == 2
5167 && operand_type_check (i.types[0], imm)
40fb9820 5168 && operand_type_check (i.types[1], imm)))
29b0f896
AM
5169 swap_operands ();
5170
ec56d5c0
JB
5171 /* The order of the immediates should be reversed
5172 for 2 immediates extrq and insertq instructions */
5173 if (i.imm_operands == 2
6d86a545 5174 && (t->mnem_off == MN_extrq || t->mnem_off == MN_insertq))
ec56d5c0
JB
5175 swap_2_operands (0, 1);
5176
29b0f896
AM
5177 if (i.imm_operands)
5178 optimize_imm ();
5179
0de704b9
JB
5180 if (i.disp_operands && !optimize_disp (t))
5181 return;
29b0f896
AM
5182
5183 /* Next, we find a template that matches the given insn,
5184 making sure the overlap of the given operands types is consistent
5185 with the template operand types. */
252b5132 5186
83b16ac6 5187 if (!(t = match_template (mnem_suffix)))
04784e33
JB
5188 {
5189 const char *err_msg;
5190
5191 if (copy && !mnem_suffix)
5192 {
5193 line = copy;
5194 copy = NULL;
9db83a32 5195 no_match:
04784e33 5196 pass1_err = i.error;
76d3f746 5197 pass1_mnem = insn_name (current_templates->start);
04784e33
JB
5198 goto retry;
5199 }
9db83a32
JB
5200
5201 /* If a non-/only-64bit template (group) was found in pass 1, and if
5202 _some_ template (group) was found in pass 2, squash pass 1's
5203 error. */
5204 if (pass1_err == unsupported_64bit)
5205 pass1_mnem = NULL;
5206
04784e33 5207 match_error:
9db83a32
JB
5208 free (copy);
5209
04784e33
JB
5210 switch (pass1_mnem ? pass1_err : i.error)
5211 {
5212 default:
5213 abort ();
5214 case operand_size_mismatch:
5215 err_msg = _("operand size mismatch");
5216 break;
5217 case operand_type_mismatch:
5218 err_msg = _("operand type mismatch");
5219 break;
5220 case register_type_mismatch:
5221 err_msg = _("register type mismatch");
5222 break;
5223 case number_of_operands_mismatch:
5224 err_msg = _("number of operands mismatch");
5225 break;
5226 case invalid_instruction_suffix:
5227 err_msg = _("invalid instruction suffix");
5228 break;
5229 case bad_imm4:
5230 err_msg = _("constant doesn't fit in 4 bits");
5231 break;
5232 case unsupported_with_intel_mnemonic:
5233 err_msg = _("unsupported with Intel mnemonic");
5234 break;
5235 case unsupported_syntax:
5236 err_msg = _("unsupported syntax");
5237 break;
5238 case unsupported:
5239 as_bad (_("unsupported instruction `%s'"),
76d3f746 5240 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
04784e33 5241 return;
9db83a32
JB
5242 case unsupported_on_arch:
5243 as_bad (_("`%s' is not supported on `%s%s'"),
76d3f746 5244 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
9db83a32
JB
5245 cpu_arch_name ? cpu_arch_name : default_arch,
5246 cpu_sub_arch_name ? cpu_sub_arch_name : "");
5247 return;
5248 case unsupported_64bit:
5249 if (ISLOWER (mnem_suffix))
e8b4b7b2
NC
5250 {
5251 if (flag_code == CODE_64BIT)
5252 as_bad (_("`%s%c' is not supported in 64-bit mode"),
76d3f746 5253 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
e8b4b7b2
NC
5254 mnem_suffix);
5255 else
5256 as_bad (_("`%s%c' is only supported in 64-bit mode"),
76d3f746 5257 pass1_mnem ? pass1_mnem : insn_name (current_templates->start),
e8b4b7b2
NC
5258 mnem_suffix);
5259 }
9db83a32 5260 else
e8b4b7b2
NC
5261 {
5262 if (flag_code == CODE_64BIT)
5263 as_bad (_("`%s' is not supported in 64-bit mode"),
76d3f746 5264 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
e8b4b7b2
NC
5265 else
5266 as_bad (_("`%s' is only supported in 64-bit mode"),
76d3f746 5267 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
e8b4b7b2 5268 }
9db83a32 5269 return;
04784e33
JB
5270 case invalid_sib_address:
5271 err_msg = _("invalid SIB address");
5272 break;
5273 case invalid_vsib_address:
5274 err_msg = _("invalid VSIB address");
5275 break;
5276 case invalid_vector_register_set:
5277 err_msg = _("mask, index, and destination registers must be distinct");
5278 break;
5279 case invalid_tmm_register_set:
5280 err_msg = _("all tmm registers must be distinct");
5281 break;
5282 case invalid_dest_and_src_register_set:
5283 err_msg = _("destination and source registers must be distinct");
5284 break;
5285 case unsupported_vector_index_register:
5286 err_msg = _("unsupported vector index register");
5287 break;
5288 case unsupported_broadcast:
5289 err_msg = _("unsupported broadcast");
5290 break;
5291 case broadcast_needed:
5292 err_msg = _("broadcast is needed for operand of such type");
5293 break;
5294 case unsupported_masking:
5295 err_msg = _("unsupported masking");
5296 break;
5297 case mask_not_on_destination:
5298 err_msg = _("mask not on destination operand");
5299 break;
5300 case no_default_mask:
5301 err_msg = _("default mask isn't allowed");
5302 break;
5303 case unsupported_rc_sae:
5304 err_msg = _("unsupported static rounding/sae");
5305 break;
5306 case invalid_register_operand:
5307 err_msg = _("invalid register operand");
5308 break;
5309 }
5310 as_bad (_("%s for `%s'"), err_msg,
76d3f746 5311 pass1_mnem ? pass1_mnem : insn_name (current_templates->start));
04784e33
JB
5312 return;
5313 }
5314
5315 free (copy);
252b5132 5316
7bab8ab5 5317 if (sse_check != check_none
ffb86450
JB
5318 /* The opcode space check isn't strictly needed; it's there only to
5319 bypass the logic below when easily possible. */
ddb62495
JB
5320 && t->opcode_space >= SPACE_0F
5321 && t->opcode_space <= SPACE_0F3A
734dfd1c 5322 && !is_cpu (&i.tm, CpuSSE4a)
ffb86450 5323 && !is_any_vex_encoding (t))
daf50ae7 5324 {
ffb86450
JB
5325 bool simd = false;
5326
5327 for (j = 0; j < t->operands; ++j)
5328 {
5329 if (t->operand_types[j].bitfield.class == RegMMX)
5330 break;
5331 if (t->operand_types[j].bitfield.class == RegSIMD)
5332 simd = true;
5333 }
5334
5335 if (j >= t->operands && simd)
5336 (sse_check == check_warning
5337 ? as_warn
76d3f746 5338 : as_bad) (_("SSE instruction `%s' is used"), insn_name (&i.tm));
daf50ae7
L
5339 }
5340
40fb9820 5341 if (i.tm.opcode_modifier.fwait)
29b0f896
AM
5342 if (!add_prefix (FWAIT_OPCODE))
5343 return;
252b5132 5344
d5de92cf 5345 /* Check if REP prefix is OK. */
742732c7 5346 if (i.rep_prefix && i.tm.opcode_modifier.prefixok != PrefixRep)
d5de92cf
L
5347 {
5348 as_bad (_("invalid instruction `%s' after `%s'"),
76d3f746 5349 insn_name (&i.tm), i.rep_prefix);
d5de92cf
L
5350 return;
5351 }
5352
c1ba0266
L
5353 /* Check for lock without a lockable instruction. Destination operand
5354 must be memory unless it is xchg (0x86). */
9a4a4499
JB
5355 if (i.prefix[LOCK_PREFIX])
5356 {
5357 if (i.tm.opcode_modifier.prefixok < PrefixLock
c1ba0266
L
5358 || i.mem_operands == 0
5359 || (i.tm.base_opcode != 0x86
9a4a4499
JB
5360 && !(i.flags[i.operands - 1] & Operand_Mem)))
5361 {
5362 as_bad (_("expecting lockable instruction after `lock'"));
5363 return;
5364 }
5365
5366 /* Zap the redundant prefix from XCHG when optimizing. */
5367 if (i.tm.base_opcode == 0x86 && optimize && !i.no_optimize)
5368 i.prefix[LOCK_PREFIX] = 0;
c32fa91d
L
5369 }
5370
e3669c7f
JB
5371 if (is_any_vex_encoding (&i.tm)
5372 || i.tm.operand_types[i.imm_operands].bitfield.class >= RegMMX
5373 || i.tm.operand_types[i.imm_operands + 1].bitfield.class >= RegMMX)
7a8655d2 5374 {
e3669c7f
JB
5375 /* Check for data size prefix on VEX/XOP/EVEX encoded and SIMD insns. */
5376 if (i.prefix[DATA_PREFIX])
5377 {
76d3f746 5378 as_bad (_("data size prefix invalid with `%s'"), insn_name (&i.tm));
e3669c7f
JB
5379 return;
5380 }
5381
5382 /* Don't allow e.g. KMOV in TLS code sequences. */
5383 for (j = i.imm_operands; j < i.operands; ++j)
5384 switch (i.reloc[j])
5385 {
5386 case BFD_RELOC_386_TLS_GOTIE:
5387 case BFD_RELOC_386_TLS_LE_32:
5388 case BFD_RELOC_X86_64_GOTTPOFF:
5389 case BFD_RELOC_X86_64_TLSLD:
76d3f746 5390 as_bad (_("TLS relocation cannot be used with `%s'"), insn_name (&i.tm));
e3669c7f
JB
5391 return;
5392 default:
5393 break;
5394 }
7a8655d2
JB
5395 }
5396
42164a71 5397 /* Check if HLE prefix is OK. */
165de32a 5398 if (i.hle_prefix && !check_hle ())
42164a71
L
5399 return;
5400
7e8b059b
L
5401 /* Check BND prefix. */
5402 if (i.bnd_prefix && !i.tm.opcode_modifier.bndprefixok)
5403 as_bad (_("expecting valid branch instruction after `bnd'"));
5404
04ef582a 5405 /* Check NOTRACK prefix. */
742732c7 5406 if (i.notrack_prefix && i.tm.opcode_modifier.prefixok != PrefixNoTrack)
9fef80d6 5407 as_bad (_("expecting indirect branch instruction after `notrack'"));
04ef582a 5408
734dfd1c 5409 if (is_cpu (&i.tm, CpuMPX))
327e8c42
JB
5410 {
5411 if (flag_code == CODE_64BIT && i.prefix[ADDR_PREFIX])
5412 as_bad (_("32-bit address isn't allowed in 64-bit MPX instructions."));
5413 else if (flag_code != CODE_16BIT
5414 ? i.prefix[ADDR_PREFIX]
5415 : i.mem_operands && !i.prefix[ADDR_PREFIX])
5416 as_bad (_("16-bit address isn't allowed in MPX instructions"));
5417 }
7e8b059b
L
5418
5419 /* Insert BND prefix. */
76d3a78a
JB
5420 if (add_bnd_prefix && i.tm.opcode_modifier.bndprefixok)
5421 {
5422 if (!i.prefix[BND_PREFIX])
5423 add_prefix (BND_PREFIX_OPCODE);
5424 else if (i.prefix[BND_PREFIX] != BND_PREFIX_OPCODE)
5425 {
5426 as_warn (_("replacing `rep'/`repe' prefix by `bnd'"));
5427 i.prefix[BND_PREFIX] = BND_PREFIX_OPCODE;
5428 }
5429 }
7e8b059b 5430
29b0f896 5431 /* Check string instruction segment overrides. */
51c8edf6 5432 if (i.tm.opcode_modifier.isstring >= IS_STRING_ES_OP0)
29b0f896 5433 {
51c8edf6 5434 gas_assert (i.mem_operands);
29b0f896 5435 if (!check_string ())
5dd0794d 5436 return;
fc0763e6 5437 i.disp_operands = 0;
29b0f896 5438 }
5dd0794d 5439
9373f275
L
5440 /* The memory operand of (%dx) should be only used with input/output
5441 instructions (base opcodes: 0x6c, 0x6e, 0xec, 0xee). */
5442 if (i.input_output_operand
5443 && ((i.tm.base_opcode | 0x82) != 0xee
ddb62495 5444 || i.tm.opcode_space != SPACE_BASE))
9373f275
L
5445 {
5446 as_bad (_("input/output port address isn't allowed with `%s'"),
76d3f746 5447 insn_name (&i.tm));
9373f275
L
5448 return;
5449 }
5450
b6f8c7c4
L
5451 if (optimize && !i.no_optimize && i.tm.opcode_modifier.optimize)
5452 optimize_encoding ();
5453
c8480b58
L
5454 if (use_unaligned_vector_move)
5455 encode_with_unaligned_vector_move ();
5456
29b0f896
AM
5457 if (!process_suffix ())
5458 return;
e413e4e9 5459
ef07be45 5460 /* Check if IP-relative addressing requirements can be satisfied. */
734dfd1c 5461 if (is_cpu (&i.tm, CpuPREFETCHI)
ef07be45 5462 && !(i.base_reg && i.base_reg->reg_num == RegIP))
76d3f746 5463 as_warn (_("'%s' only supports RIP-relative address"), insn_name (&i.tm));
ef07be45 5464
921eafea 5465 /* Update operand types and check extended states. */
bc0844ae 5466 for (j = 0; j < i.operands; j++)
921eafea
L
5467 {
5468 i.types[j] = operand_type_and (i.types[j], i.tm.operand_types[j]);
3d70986f 5469 switch (i.tm.operand_types[j].bitfield.class)
921eafea
L
5470 {
5471 default:
5472 break;
5473 case RegMMX:
5474 i.xstate |= xstate_mmx;
5475 break;
5476 case RegMask:
32930e4e 5477 i.xstate |= xstate_mask;
921eafea
L
5478 break;
5479 case RegSIMD:
3d70986f 5480 if (i.tm.operand_types[j].bitfield.tmmword)
921eafea 5481 i.xstate |= xstate_tmm;
4fc85f37
JB
5482 else if (i.tm.operand_types[j].bitfield.zmmword
5483 && vector_size >= VSZ512)
921eafea 5484 i.xstate |= xstate_zmm;
4fc85f37
JB
5485 else if (i.tm.operand_types[j].bitfield.ymmword
5486 && vector_size >= VSZ256)
921eafea 5487 i.xstate |= xstate_ymm;
3d70986f 5488 else if (i.tm.operand_types[j].bitfield.xmmword)
921eafea
L
5489 i.xstate |= xstate_xmm;
5490 break;
5491 }
5492 }
bc0844ae 5493
29b0f896
AM
5494 /* Make still unresolved immediate matches conform to size of immediate
5495 given in i.suffix. */
5496 if (!finalize_imm ())
5497 return;
252b5132 5498
40fb9820 5499 if (i.types[0].bitfield.imm1)
29b0f896 5500 i.imm_operands = 0; /* kludge for shift insns. */
252b5132 5501
29b0f896
AM
5502 /* For insns with operands there are more diddles to do to the opcode. */
5503 if (i.operands)
5504 {
5505 if (!process_operands ())
5506 return;
5507 }
255571cd 5508 else if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
29b0f896
AM
5509 {
5510 /* UnixWare fsub no args is alias for fsubp, fadd -> faddp, etc. */
76d3f746 5511 as_warn (_("translating to `%sp'"), insn_name (&i.tm));
29b0f896 5512 }
252b5132 5513
7a8655d2 5514 if (is_any_vex_encoding (&i.tm))
9e5e5283 5515 {
c1dc7af5 5516 if (!cpu_arch_flags.bitfield.cpui286)
9e5e5283 5517 {
c1dc7af5 5518 as_bad (_("instruction `%s' isn't supported outside of protected mode."),
76d3f746 5519 insn_name (&i.tm));
9e5e5283
L
5520 return;
5521 }
c0f3af97 5522
0b9404fd
JB
5523 /* Check for explicit REX prefix. */
5524 if (i.prefix[REX_PREFIX] || i.rex_encoding)
5525 {
76d3f746 5526 as_bad (_("REX prefix invalid with `%s'"), insn_name (&i.tm));
0b9404fd
JB
5527 return;
5528 }
5529
9e5e5283
L
5530 if (i.tm.opcode_modifier.vex)
5531 build_vex_prefix (t);
5532 else
5533 build_evex_prefix ();
0b9404fd
JB
5534
5535 /* The individual REX.RXBW bits got consumed. */
5536 i.rex &= REX_OPCODE;
9e5e5283 5537 }
43234a1e 5538
7fc69528
JB
5539 /* Handle conversion of 'int $3' --> special int3 insn. */
5540 if (i.tm.mnem_off == MN_int
a6461c02 5541 && i.op[0].imms->X_add_number == 3)
29b0f896
AM
5542 {
5543 i.tm.base_opcode = INT3_OPCODE;
5544 i.imm_operands = 0;
5545 }
252b5132 5546
0cfa3eb3
JB
5547 if ((i.tm.opcode_modifier.jump == JUMP
5548 || i.tm.opcode_modifier.jump == JUMP_BYTE
5549 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896
AM
5550 && i.op[0].disps->X_op == O_constant)
5551 {
5552 /* Convert "jmp constant" (and "call constant") to a jump (call) to
5553 the absolute address given by the constant. Since ix86 jumps and
5554 calls are pc relative, we need to generate a reloc. */
5555 i.op[0].disps->X_add_symbol = &abs_symbol;
5556 i.op[0].disps->X_op = O_symbol;
5557 }
252b5132 5558
29b0f896
AM
5559 /* For 8 bit registers we need an empty rex prefix. Also if the
5560 instruction already has a prefix, we need to convert old
5561 registers to new ones. */
773f551c 5562
bab6aec1 5563 if ((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte
29b0f896 5564 && (i.op[0].regs->reg_flags & RegRex64) != 0)
bab6aec1 5565 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte
29b0f896 5566 && (i.op[1].regs->reg_flags & RegRex64) != 0)
bab6aec1
JB
5567 || (((i.types[0].bitfield.class == Reg && i.types[0].bitfield.byte)
5568 || (i.types[1].bitfield.class == Reg && i.types[1].bitfield.byte))
29b0f896
AM
5569 && i.rex != 0))
5570 {
5571 int x;
726c5dcd 5572
29b0f896
AM
5573 i.rex |= REX_OPCODE;
5574 for (x = 0; x < 2; x++)
5575 {
5576 /* Look for 8 bit operand that uses old registers. */
bab6aec1 5577 if (i.types[x].bitfield.class == Reg && i.types[x].bitfield.byte
29b0f896 5578 && (i.op[x].regs->reg_flags & RegRex64) == 0)
773f551c 5579 {
3f93af61 5580 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
29b0f896
AM
5581 /* In case it is "hi" register, give up. */
5582 if (i.op[x].regs->reg_num > 3)
a540244d 5583 as_bad (_("can't encode register '%s%s' in an "
4eed87de 5584 "instruction requiring REX prefix."),
a540244d 5585 register_prefix, i.op[x].regs->reg_name);
773f551c 5586
29b0f896
AM
5587 /* Otherwise it is equivalent to the extended register.
5588 Since the encoding doesn't change this is merely
5589 cosmetic cleanup for debug output. */
5590
5591 i.op[x].regs = i.op[x].regs + 8;
773f551c 5592 }
29b0f896
AM
5593 }
5594 }
773f551c 5595
6b6b6807
L
5596 if (i.rex == 0 && i.rex_encoding)
5597 {
5598 /* Check if we can add a REX_OPCODE byte. Look for 8 bit operand
3f93af61 5599 that uses legacy register. If it is "hi" register, don't add
6b6b6807
L
5600 the REX_OPCODE byte. */
5601 int x;
5602 for (x = 0; x < 2; x++)
bab6aec1 5603 if (i.types[x].bitfield.class == Reg
6b6b6807
L
5604 && i.types[x].bitfield.byte
5605 && (i.op[x].regs->reg_flags & RegRex64) == 0
5606 && i.op[x].regs->reg_num > 3)
5607 {
3f93af61 5608 gas_assert (!(i.op[x].regs->reg_flags & RegRex));
5b7c81bd 5609 i.rex_encoding = false;
6b6b6807
L
5610 break;
5611 }
5612
5613 if (i.rex_encoding)
5614 i.rex = REX_OPCODE;
5615 }
5616
7ab9ffdd 5617 if (i.rex != 0)
29b0f896
AM
5618 add_prefix (REX_OPCODE | i.rex);
5619
ae531041
L
5620 insert_lfence_before ();
5621
29b0f896
AM
5622 /* We are ready to output the insn. */
5623 output_insn ();
e379e5f3 5624
ae531041
L
5625 insert_lfence_after ();
5626
e379e5f3
L
5627 last_insn.seg = now_seg;
5628
5629 if (i.tm.opcode_modifier.isprefix)
5630 {
5631 last_insn.kind = last_insn_prefix;
76d3f746 5632 last_insn.name = insn_name (&i.tm);
e379e5f3
L
5633 last_insn.file = as_where (&last_insn.line);
5634 }
5635 else
5636 last_insn.kind = last_insn_other;
29b0f896
AM
5637}
5638
9db83a32
JB
5639/* The Q suffix is generally valid only in 64-bit mode, with very few
5640 exceptions: fild, fistp, fisttp, and cmpxchg8b. Note that for fild
5641 and fisttp only one of their two templates is matched below: That's
5642 sufficient since other relevant attributes are the same between both
5643 respective templates. */
5644static INLINE bool q_suffix_allowed(const insn_template *t)
5645{
5646 return flag_code == CODE_64BIT
ddb62495 5647 || (t->opcode_space == SPACE_BASE
9db83a32
JB
5648 && t->base_opcode == 0xdf
5649 && (t->extension_opcode & 1)) /* fild / fistp / fisttp */
7fc69528 5650 || t->mnem_off == MN_cmpxchg8b;
9db83a32
JB
5651}
5652
5317ad2c 5653static const char *
edd67638 5654parse_insn (const char *line, char *mnemonic, bool prefix_only)
29b0f896 5655{
5317ad2c 5656 const char *l = line, *token_start = l;
29b0f896 5657 char *mnem_p;
04784e33 5658 bool pass1 = !current_templates;
5c6af06e 5659 int supported;
d3ce72d0 5660 const insn_template *t;
b6169b20 5661 char *dot_p = NULL;
29b0f896 5662
29b0f896
AM
5663 while (1)
5664 {
5665 mnem_p = mnemonic;
778415f5
JB
5666 /* Pseudo-prefixes start with an opening figure brace. */
5667 if ((*mnem_p = *l) == '{')
5668 {
5669 ++mnem_p;
5670 ++l;
5671 }
29b0f896
AM
5672 while ((*mnem_p = mnemonic_chars[(unsigned char) *l]) != 0)
5673 {
b6169b20
L
5674 if (*mnem_p == '.')
5675 dot_p = mnem_p;
29b0f896
AM
5676 mnem_p++;
5677 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
45288df1 5678 {
778415f5 5679 too_long:
29b0f896
AM
5680 as_bad (_("no such instruction: `%s'"), token_start);
5681 return NULL;
5682 }
5683 l++;
5684 }
778415f5
JB
5685 /* Pseudo-prefixes end with a closing figure brace. */
5686 if (*mnemonic == '{' && *l == '}')
5687 {
5688 *mnem_p++ = *l++;
5689 if (mnem_p >= mnemonic + MAX_MNEM_SIZE)
5690 goto too_long;
5691 *mnem_p = '\0';
5692
5693 /* Point l at the closing brace if there's no other separator. */
5694 if (*l != END_OF_INSN && !is_space_char (*l)
5695 && *l != PREFIX_SEPARATOR)
5696 --l;
5697 }
5698 else if (!is_space_char (*l)
5699 && *l != END_OF_INSN
5700 && (intel_syntax
5701 || (*l != PREFIX_SEPARATOR && *l != ',')))
29b0f896 5702 {
edd67638
JB
5703 if (prefix_only)
5704 break;
29b0f896
AM
5705 as_bad (_("invalid character %s in mnemonic"),
5706 output_invalid (*l));
5707 return NULL;
5708 }
5709 if (token_start == l)
5710 {
e44823cf 5711 if (!intel_syntax && *l == PREFIX_SEPARATOR)
29b0f896
AM
5712 as_bad (_("expecting prefix; got nothing"));
5713 else
5714 as_bad (_("expecting mnemonic; got nothing"));
5715 return NULL;
5716 }
45288df1 5717
29b0f896 5718 /* Look up instruction (or prefix) via hash table. */
629310ab 5719 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
47926f60 5720
29b0f896
AM
5721 if (*l != END_OF_INSN
5722 && (!is_space_char (*l) || l[1] != END_OF_INSN)
5723 && current_templates
40fb9820 5724 && current_templates->start->opcode_modifier.isprefix)
29b0f896 5725 {
734dfd1c 5726 if (!cpu_flags_check_cpu64 (current_templates->start->cpu))
2dd88dca
JB
5727 {
5728 as_bad ((flag_code != CODE_64BIT
5729 ? _("`%s' is only supported in 64-bit mode")
5730 : _("`%s' is not supported in 64-bit mode")),
76d3f746 5731 insn_name (current_templates->start));
2dd88dca
JB
5732 return NULL;
5733 }
29b0f896
AM
5734 /* If we are in 16-bit mode, do not allow addr16 or data16.
5735 Similarly, in 32-bit mode, do not allow addr32 or data32. */
673fe0f0
JB
5736 if ((current_templates->start->opcode_modifier.size == SIZE16
5737 || current_templates->start->opcode_modifier.size == SIZE32)
29b0f896 5738 && flag_code != CODE_64BIT
673fe0f0 5739 && ((current_templates->start->opcode_modifier.size == SIZE32)
29b0f896
AM
5740 ^ (flag_code == CODE_16BIT)))
5741 {
5742 as_bad (_("redundant %s prefix"),
76d3f746 5743 insn_name (current_templates->start));
29b0f896 5744 return NULL;
45288df1 5745 }
31184569
JB
5746
5747 if (current_templates->start->base_opcode == PSEUDO_PREFIX)
29b0f896 5748 {
86fa6981 5749 /* Handle pseudo prefixes. */
31184569 5750 switch (current_templates->start->extension_opcode)
86fa6981 5751 {
41eb8e88 5752 case Prefix_Disp8:
86fa6981
L
5753 /* {disp8} */
5754 i.disp_encoding = disp_encoding_8bit;
5755 break;
41eb8e88
L
5756 case Prefix_Disp16:
5757 /* {disp16} */
5758 i.disp_encoding = disp_encoding_16bit;
5759 break;
5760 case Prefix_Disp32:
86fa6981
L
5761 /* {disp32} */
5762 i.disp_encoding = disp_encoding_32bit;
5763 break;
41eb8e88 5764 case Prefix_Load:
86fa6981
L
5765 /* {load} */
5766 i.dir_encoding = dir_encoding_load;
5767 break;
41eb8e88 5768 case Prefix_Store:
86fa6981
L
5769 /* {store} */
5770 i.dir_encoding = dir_encoding_store;
5771 break;
41eb8e88 5772 case Prefix_VEX:
42e04b36
L
5773 /* {vex} */
5774 i.vec_encoding = vex_encoding_vex;
86fa6981 5775 break;
41eb8e88 5776 case Prefix_VEX3:
86fa6981
L
5777 /* {vex3} */
5778 i.vec_encoding = vex_encoding_vex3;
5779 break;
41eb8e88 5780 case Prefix_EVEX:
86fa6981
L
5781 /* {evex} */
5782 i.vec_encoding = vex_encoding_evex;
5783 break;
41eb8e88 5784 case Prefix_REX:
6b6b6807 5785 /* {rex} */
5b7c81bd 5786 i.rex_encoding = true;
6b6b6807 5787 break;
41eb8e88 5788 case Prefix_NoOptimize:
b6f8c7c4 5789 /* {nooptimize} */
5b7c81bd 5790 i.no_optimize = true;
b6f8c7c4 5791 break;
86fa6981
L
5792 default:
5793 abort ();
5794 }
5795 }
5796 else
5797 {
5798 /* Add prefix, checking for repeated prefixes. */
4e9ac44a 5799 switch (add_prefix (current_templates->start->base_opcode))
86fa6981 5800 {
4e9ac44a
L
5801 case PREFIX_EXIST:
5802 return NULL;
5803 case PREFIX_DS:
734dfd1c 5804 if (is_cpu (current_templates->start, CpuIBT))
76d3f746 5805 i.notrack_prefix = insn_name (current_templates->start);
4e9ac44a
L
5806 break;
5807 case PREFIX_REP:
734dfd1c 5808 if (is_cpu (current_templates->start, CpuHLE))
76d3f746 5809 i.hle_prefix = insn_name (current_templates->start);
734dfd1c 5810 else if (is_cpu (current_templates->start, CpuMPX))
76d3f746 5811 i.bnd_prefix = insn_name (current_templates->start);
4e9ac44a 5812 else
76d3f746 5813 i.rep_prefix = insn_name (current_templates->start);
4e9ac44a
L
5814 break;
5815 default:
5816 break;
86fa6981 5817 }
29b0f896
AM
5818 }
5819 /* Skip past PREFIX_SEPARATOR and reset token_start. */
5820 token_start = ++l;
5821 }
5822 else
5823 break;
5824 }
45288df1 5825
edd67638
JB
5826 if (prefix_only)
5827 return token_start;
5828
30a55f88 5829 if (!current_templates)
b6169b20 5830 {
07d5e953
JB
5831 /* Deprecated functionality (new code should use pseudo-prefixes instead):
5832 Check if we should swap operand or force 32bit displacement in
f8a5c266 5833 encoding. */
30a55f88 5834 if (mnem_p - 2 == dot_p && dot_p[1] == 's')
64c49ab3 5835 i.dir_encoding = dir_encoding_swap;
8d63c93e 5836 else if (mnem_p - 3 == dot_p
a501d77e
L
5837 && dot_p[1] == 'd'
5838 && dot_p[2] == '8')
5839 i.disp_encoding = disp_encoding_8bit;
8d63c93e 5840 else if (mnem_p - 4 == dot_p
f8a5c266
L
5841 && dot_p[1] == 'd'
5842 && dot_p[2] == '3'
5843 && dot_p[3] == '2')
a501d77e 5844 i.disp_encoding = disp_encoding_32bit;
30a55f88
L
5845 else
5846 goto check_suffix;
5847 mnem_p = dot_p;
5848 *dot_p = '\0';
629310ab 5849 current_templates = (const templates *) str_hash_find (op_hash, mnemonic);
b6169b20
L
5850 }
5851
04784e33 5852 if (!current_templates || !pass1)
29b0f896 5853 {
04784e33
JB
5854 current_templates = NULL;
5855
dc1e8a47 5856 check_suffix:
1c529385 5857 if (mnem_p > mnemonic)
29b0f896 5858 {
1c529385
LH
5859 /* See if we can get a match by trimming off a suffix. */
5860 switch (mnem_p[-1])
29b0f896 5861 {
1c529385
LH
5862 case WORD_MNEM_SUFFIX:
5863 if (intel_syntax && (intel_float_operand (mnemonic) & 2))
29b0f896
AM
5864 i.suffix = SHORT_MNEM_SUFFIX;
5865 else
1c529385
LH
5866 /* Fall through. */
5867 case BYTE_MNEM_SUFFIX:
5868 case QWORD_MNEM_SUFFIX:
5869 i.suffix = mnem_p[-1];
29b0f896 5870 mnem_p[-1] = '\0';
fe0e921f
AM
5871 current_templates
5872 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5873 break;
5874 case SHORT_MNEM_SUFFIX:
5875 case LONG_MNEM_SUFFIX:
5876 if (!intel_syntax)
5877 {
5878 i.suffix = mnem_p[-1];
5879 mnem_p[-1] = '\0';
fe0e921f
AM
5880 current_templates
5881 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385
LH
5882 }
5883 break;
5884
5885 /* Intel Syntax. */
5886 case 'd':
5887 if (intel_syntax)
5888 {
5889 if (intel_float_operand (mnemonic) == 1)
5890 i.suffix = SHORT_MNEM_SUFFIX;
5891 else
5892 i.suffix = LONG_MNEM_SUFFIX;
5893 mnem_p[-1] = '\0';
fe0e921f
AM
5894 current_templates
5895 = (const templates *) str_hash_find (op_hash, mnemonic);
1c529385 5896 }
04784e33
JB
5897 /* For compatibility reasons accept MOVSD and CMPSD without
5898 operands even in AT&T mode. */
5899 else if (*l == END_OF_INSN
5900 || (is_space_char (*l) && l[1] == END_OF_INSN))
5901 {
5902 mnem_p[-1] = '\0';
5903 current_templates
5904 = (const templates *) str_hash_find (op_hash, mnemonic);
5905 if (current_templates != NULL
5906 /* MOVS or CMPS */
5907 && (current_templates->start->base_opcode | 2) == 0xa6
ddb62495 5908 && current_templates->start->opcode_space
04784e33
JB
5909 == SPACE_BASE
5910 && mnem_p[-2] == 's')
5911 {
5912 as_warn (_("found `%sd'; assuming `%sl' was meant"),
5913 mnemonic, mnemonic);
5914 i.suffix = LONG_MNEM_SUFFIX;
5915 }
5916 else
5917 {
5918 current_templates = NULL;
5919 mnem_p[-1] = 'd';
5920 }
5921 }
1c529385 5922 break;
29b0f896 5923 }
29b0f896 5924 }
1c529385 5925
29b0f896
AM
5926 if (!current_templates)
5927 {
04784e33
JB
5928 if (pass1)
5929 as_bad (_("no such instruction: `%s'"), token_start);
29b0f896
AM
5930 return NULL;
5931 }
5932 }
252b5132 5933
0cfa3eb3
JB
5934 if (current_templates->start->opcode_modifier.jump == JUMP
5935 || current_templates->start->opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
5936 {
5937 /* Check for a branch hint. We allow ",pt" and ",pn" for
5938 predict taken and predict not taken respectively.
5939 I'm not sure that branch hints actually do anything on loop
5940 and jcxz insns (JumpByte) for current Pentium4 chips. They
5941 may work in the future and it doesn't hurt to accept them
5942 now. */
5943 if (l[0] == ',' && l[1] == 'p')
5944 {
5945 if (l[2] == 't')
5946 {
5947 if (!add_prefix (DS_PREFIX_OPCODE))
5948 return NULL;
5949 l += 3;
5950 }
5951 else if (l[2] == 'n')
5952 {
5953 if (!add_prefix (CS_PREFIX_OPCODE))
5954 return NULL;
5955 l += 3;
5956 }
5957 }
5958 }
5959 /* Any other comma loses. */
5960 if (*l == ',')
5961 {
5962 as_bad (_("invalid character %s in mnemonic"),
5963 output_invalid (*l));
5964 return NULL;
5965 }
252b5132 5966
29b0f896 5967 /* Check if instruction is supported on specified architecture. */
5c6af06e
JB
5968 supported = 0;
5969 for (t = current_templates->start; t < current_templates->end; ++t)
5970 {
c0f3af97 5971 supported |= cpu_flags_match (t);
9db83a32
JB
5972
5973 if (i.suffix == QWORD_MNEM_SUFFIX && !q_suffix_allowed (t))
5974 supported &= ~CPU_FLAGS_64BIT_MATCH;
5975
c0f3af97 5976 if (supported == CPU_FLAGS_PERFECT_MATCH)
d59a54c2 5977 return l;
29b0f896 5978 }
3629bb00 5979
9db83a32
JB
5980 if (pass1)
5981 {
5982 if (supported & CPU_FLAGS_64BIT_MATCH)
5983 i.error = unsupported_on_arch;
5984 else
5985 i.error = unsupported_64bit;
5986 }
252b5132 5987
548d0ee6 5988 return NULL;
29b0f896 5989}
252b5132 5990
29b0f896 5991static char *
e3bb37b5 5992parse_operands (char *l, const char *mnemonic)
29b0f896
AM
5993{
5994 char *token_start;
3138f287 5995
29b0f896
AM
5996 /* 1 if operand is pending after ','. */
5997 unsigned int expecting_operand = 0;
252b5132 5998
29b0f896
AM
5999 while (*l != END_OF_INSN)
6000 {
e68c3d59
JB
6001 /* Non-zero if operand parens not balanced. */
6002 unsigned int paren_not_balanced = 0;
6003 /* True if inside double quotes. */
6004 bool in_quotes = false;
6005
29b0f896
AM
6006 /* Skip optional white space before operand. */
6007 if (is_space_char (*l))
6008 ++l;
d02603dc 6009 if (!is_operand_char (*l) && *l != END_OF_INSN && *l != '"')
29b0f896
AM
6010 {
6011 as_bad (_("invalid character %s before operand %d"),
6012 output_invalid (*l),
6013 i.operands + 1);
6014 return NULL;
6015 }
d02603dc 6016 token_start = l; /* After white space. */
e68c3d59 6017 while (in_quotes || paren_not_balanced || *l != ',')
29b0f896
AM
6018 {
6019 if (*l == END_OF_INSN)
6020 {
e68c3d59
JB
6021 if (in_quotes)
6022 {
6023 as_bad (_("unbalanced double quotes in operand %d."),
6024 i.operands + 1);
6025 return NULL;
6026 }
29b0f896
AM
6027 if (paren_not_balanced)
6028 {
98ff9f1c
JB
6029 know (!intel_syntax);
6030 as_bad (_("unbalanced parenthesis in operand %d."),
6031 i.operands + 1);
29b0f896
AM
6032 return NULL;
6033 }
6034 else
6035 break; /* we are done */
6036 }
e68c3d59
JB
6037 else if (*l == '\\' && l[1] == '"')
6038 ++l;
6039 else if (*l == '"')
6040 in_quotes = !in_quotes;
6041 else if (!in_quotes && !is_operand_char (*l) && !is_space_char (*l))
29b0f896
AM
6042 {
6043 as_bad (_("invalid character %s in operand %d"),
6044 output_invalid (*l),
6045 i.operands + 1);
6046 return NULL;
6047 }
e68c3d59 6048 if (!intel_syntax && !in_quotes)
29b0f896
AM
6049 {
6050 if (*l == '(')
6051 ++paren_not_balanced;
6052 if (*l == ')')
6053 --paren_not_balanced;
6054 }
29b0f896
AM
6055 l++;
6056 }
6057 if (l != token_start)
6058 { /* Yes, we've read in another operand. */
6059 unsigned int operand_ok;
6060 this_operand = i.operands++;
6061 if (i.operands > MAX_OPERANDS)
6062 {
6063 as_bad (_("spurious operands; (%d operands/instruction max)"),
6064 MAX_OPERANDS);
6065 return NULL;
6066 }
9d46ce34 6067 i.types[this_operand].bitfield.unspecified = 1;
29b0f896
AM
6068 /* Now parse operand adding info to 'i' as we go along. */
6069 END_STRING_AND_SAVE (l);
6070
1286ab78
L
6071 if (i.mem_operands > 1)
6072 {
6073 as_bad (_("too many memory references for `%s'"),
6074 mnemonic);
6075 return 0;
6076 }
6077
29b0f896
AM
6078 if (intel_syntax)
6079 operand_ok =
6080 i386_intel_operand (token_start,
6081 intel_float_operand (mnemonic));
6082 else
a7619375 6083 operand_ok = i386_att_operand (token_start);
29b0f896
AM
6084
6085 RESTORE_END_STRING (l);
6086 if (!operand_ok)
6087 return NULL;
6088 }
6089 else
6090 {
6091 if (expecting_operand)
6092 {
6093 expecting_operand_after_comma:
6094 as_bad (_("expecting operand after ','; got nothing"));
6095 return NULL;
6096 }
6097 if (*l == ',')
6098 {
6099 as_bad (_("expecting operand before ','; got nothing"));
6100 return NULL;
6101 }
6102 }
7f3f1ea2 6103
29b0f896
AM
6104 /* Now *l must be either ',' or END_OF_INSN. */
6105 if (*l == ',')
6106 {
6107 if (*++l == END_OF_INSN)
6108 {
6109 /* Just skip it, if it's \n complain. */
6110 goto expecting_operand_after_comma;
6111 }
6112 expecting_operand = 1;
6113 }
6114 }
6115 return l;
6116}
7f3f1ea2 6117
050dfa73 6118static void
783c187b 6119swap_2_operands (unsigned int xchg1, unsigned int xchg2)
050dfa73
MM
6120{
6121 union i386_op temp_op;
40fb9820 6122 i386_operand_type temp_type;
c48dadc9 6123 unsigned int temp_flags;
050dfa73 6124 enum bfd_reloc_code_real temp_reloc;
4eed87de 6125
050dfa73
MM
6126 temp_type = i.types[xchg2];
6127 i.types[xchg2] = i.types[xchg1];
6128 i.types[xchg1] = temp_type;
c48dadc9
JB
6129
6130 temp_flags = i.flags[xchg2];
6131 i.flags[xchg2] = i.flags[xchg1];
6132 i.flags[xchg1] = temp_flags;
6133
050dfa73
MM
6134 temp_op = i.op[xchg2];
6135 i.op[xchg2] = i.op[xchg1];
6136 i.op[xchg1] = temp_op;
c48dadc9 6137
050dfa73
MM
6138 temp_reloc = i.reloc[xchg2];
6139 i.reloc[xchg2] = i.reloc[xchg1];
6140 i.reloc[xchg1] = temp_reloc;
43234a1e 6141
c032bc4f
JB
6142 temp_flags = i.imm_bits[xchg2];
6143 i.imm_bits[xchg2] = i.imm_bits[xchg1];
6144 i.imm_bits[xchg1] = temp_flags;
6145
6225c532 6146 if (i.mask.reg)
43234a1e 6147 {
6225c532
JB
6148 if (i.mask.operand == xchg1)
6149 i.mask.operand = xchg2;
6150 else if (i.mask.operand == xchg2)
6151 i.mask.operand = xchg1;
43234a1e 6152 }
a5748e0d 6153 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 6154 {
5273a3cd
JB
6155 if (i.broadcast.operand == xchg1)
6156 i.broadcast.operand = xchg2;
6157 else if (i.broadcast.operand == xchg2)
6158 i.broadcast.operand = xchg1;
43234a1e 6159 }
050dfa73
MM
6160}
6161
29b0f896 6162static void
e3bb37b5 6163swap_operands (void)
29b0f896 6164{
b7c61d9a 6165 switch (i.operands)
050dfa73 6166 {
c0f3af97 6167 case 5:
b7c61d9a 6168 case 4:
4d456e3d 6169 swap_2_operands (1, i.operands - 2);
1a0670f3 6170 /* Fall through. */
b7c61d9a
L
6171 case 3:
6172 case 2:
4d456e3d 6173 swap_2_operands (0, i.operands - 1);
b7c61d9a
L
6174 break;
6175 default:
6176 abort ();
29b0f896 6177 }
29b0f896
AM
6178
6179 if (i.mem_operands == 2)
6180 {
5e042380 6181 const reg_entry *temp_seg;
29b0f896
AM
6182 temp_seg = i.seg[0];
6183 i.seg[0] = i.seg[1];
6184 i.seg[1] = temp_seg;
6185 }
6186}
252b5132 6187
29b0f896
AM
6188/* Try to ensure constant immediates are represented in the smallest
6189 opcode possible. */
6190static void
e3bb37b5 6191optimize_imm (void)
29b0f896
AM
6192{
6193 char guess_suffix = 0;
6194 int op;
252b5132 6195
29b0f896
AM
6196 if (i.suffix)
6197 guess_suffix = i.suffix;
6198 else if (i.reg_operands)
6199 {
6200 /* Figure out a suffix from the last register operand specified.
75e5731b
JB
6201 We can't do this properly yet, i.e. excluding special register
6202 instances, but the following works for instructions with
6203 immediates. In any case, we can't set i.suffix yet. */
29b0f896 6204 for (op = i.operands; --op >= 0;)
bab6aec1
JB
6205 if (i.types[op].bitfield.class != Reg)
6206 continue;
6207 else if (i.types[op].bitfield.byte)
7ab9ffdd 6208 {
40fb9820
L
6209 guess_suffix = BYTE_MNEM_SUFFIX;
6210 break;
6211 }
bab6aec1 6212 else if (i.types[op].bitfield.word)
252b5132 6213 {
40fb9820
L
6214 guess_suffix = WORD_MNEM_SUFFIX;
6215 break;
6216 }
bab6aec1 6217 else if (i.types[op].bitfield.dword)
40fb9820
L
6218 {
6219 guess_suffix = LONG_MNEM_SUFFIX;
6220 break;
6221 }
bab6aec1 6222 else if (i.types[op].bitfield.qword)
40fb9820
L
6223 {
6224 guess_suffix = QWORD_MNEM_SUFFIX;
29b0f896 6225 break;
252b5132 6226 }
29b0f896
AM
6227 }
6228 else if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
6229 guess_suffix = WORD_MNEM_SUFFIX;
5cc00775
JB
6230 else if (flag_code != CODE_64BIT || !(i.prefix[REX_PREFIX] & REX_W))
6231 guess_suffix = LONG_MNEM_SUFFIX;
29b0f896
AM
6232
6233 for (op = i.operands; --op >= 0;)
40fb9820 6234 if (operand_type_check (i.types[op], imm))
29b0f896
AM
6235 {
6236 switch (i.op[op].imms->X_op)
252b5132 6237 {
29b0f896
AM
6238 case O_constant:
6239 /* If a suffix is given, this operand may be shortened. */
6240 switch (guess_suffix)
252b5132 6241 {
29b0f896 6242 case LONG_MNEM_SUFFIX:
40fb9820
L
6243 i.types[op].bitfield.imm32 = 1;
6244 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
6245 break;
6246 case WORD_MNEM_SUFFIX:
40fb9820
L
6247 i.types[op].bitfield.imm16 = 1;
6248 i.types[op].bitfield.imm32 = 1;
6249 i.types[op].bitfield.imm32s = 1;
6250 i.types[op].bitfield.imm64 = 1;
29b0f896
AM
6251 break;
6252 case BYTE_MNEM_SUFFIX:
40fb9820
L
6253 i.types[op].bitfield.imm8 = 1;
6254 i.types[op].bitfield.imm8s = 1;
6255 i.types[op].bitfield.imm16 = 1;
6256 i.types[op].bitfield.imm32 = 1;
6257 i.types[op].bitfield.imm32s = 1;
6258 i.types[op].bitfield.imm64 = 1;
29b0f896 6259 break;
252b5132 6260 }
252b5132 6261
29b0f896
AM
6262 /* If this operand is at most 16 bits, convert it
6263 to a signed 16 bit number before trying to see
6264 whether it will fit in an even smaller size.
6265 This allows a 16-bit operand such as $0xffe0 to
6266 be recognised as within Imm8S range. */
40fb9820 6267 if ((i.types[op].bitfield.imm16)
7e96fb68 6268 && fits_in_unsigned_word (i.op[op].imms->X_add_number))
252b5132 6269 {
87ed972d
JB
6270 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
6271 ^ 0x8000) - 0x8000);
29b0f896 6272 }
a28def75
L
6273#ifdef BFD64
6274 /* Store 32-bit immediate in 64-bit for 64-bit BFD. */
40fb9820 6275 if ((i.types[op].bitfield.imm32)
7e96fb68 6276 && fits_in_unsigned_long (i.op[op].imms->X_add_number))
29b0f896
AM
6277 {
6278 i.op[op].imms->X_add_number = ((i.op[op].imms->X_add_number
6279 ^ ((offsetT) 1 << 31))
6280 - ((offsetT) 1 << 31));
6281 }
a28def75 6282#endif
40fb9820 6283 i.types[op]
c6fb90c8
L
6284 = operand_type_or (i.types[op],
6285 smallest_imm_type (i.op[op].imms->X_add_number));
252b5132 6286
29b0f896
AM
6287 /* We must avoid matching of Imm32 templates when 64bit
6288 only immediate is available. */
6289 if (guess_suffix == QWORD_MNEM_SUFFIX)
40fb9820 6290 i.types[op].bitfield.imm32 = 0;
29b0f896 6291 break;
252b5132 6292
29b0f896
AM
6293 case O_absent:
6294 case O_register:
6295 abort ();
6296
6297 /* Symbols and expressions. */
6298 default:
9cd96992
JB
6299 /* Convert symbolic operand to proper sizes for matching, but don't
6300 prevent matching a set of insns that only supports sizes other
6301 than those matching the insn suffix. */
6302 {
40fb9820 6303 i386_operand_type mask, allowed;
87ed972d 6304 const insn_template *t = current_templates->start;
9cd96992 6305
0dfbf9d7 6306 operand_type_set (&mask, 0);
9cd96992
JB
6307 switch (guess_suffix)
6308 {
6309 case QWORD_MNEM_SUFFIX:
40fb9820
L
6310 mask.bitfield.imm64 = 1;
6311 mask.bitfield.imm32s = 1;
9cd96992
JB
6312 break;
6313 case LONG_MNEM_SUFFIX:
40fb9820 6314 mask.bitfield.imm32 = 1;
9cd96992
JB
6315 break;
6316 case WORD_MNEM_SUFFIX:
40fb9820 6317 mask.bitfield.imm16 = 1;
9cd96992
JB
6318 break;
6319 case BYTE_MNEM_SUFFIX:
40fb9820 6320 mask.bitfield.imm8 = 1;
9cd96992
JB
6321 break;
6322 default:
9cd96992
JB
6323 break;
6324 }
8f0212ac
JB
6325
6326 allowed = operand_type_and (t->operand_types[op], mask);
6327 while (++t < current_templates->end)
6328 {
6329 allowed = operand_type_or (allowed, t->operand_types[op]);
6330 allowed = operand_type_and (allowed, mask);
6331 }
6332
0dfbf9d7 6333 if (!operand_type_all_zero (&allowed))
c6fb90c8 6334 i.types[op] = operand_type_and (i.types[op], mask);
9cd96992 6335 }
29b0f896 6336 break;
252b5132 6337 }
29b0f896
AM
6338 }
6339}
47926f60 6340
29b0f896 6341/* Try to use the smallest displacement type too. */
0de704b9
JB
6342static bool
6343optimize_disp (const insn_template *t)
29b0f896 6344{
0de704b9 6345 unsigned int op;
3e73aa7c 6346
0de704b9
JB
6347 if (!want_disp32 (t)
6348 && (!t->opcode_modifier.jump
6349 || i.jumpabsolute || i.types[0].bitfield.baseindex))
6350 {
6351 for (op = 0; op < i.operands; ++op)
6352 {
6353 const expressionS *exp = i.op[op].disps;
6354
6355 if (!operand_type_check (i.types[op], disp))
6356 continue;
6357
6358 if (exp->X_op != O_constant)
6359 continue;
6360
6361 /* Since displacement is signed extended to 64bit, don't allow
6362 disp32 if it is out of range. */
6363 if (fits_in_signed_long (exp->X_add_number))
6364 continue;
6365
6366 i.types[op].bitfield.disp32 = 0;
6367 if (i.types[op].bitfield.baseindex)
6368 {
6369 as_bad (_("0x%" PRIx64 " out of range of signed 32bit displacement"),
6370 (uint64_t) exp->X_add_number);
6371 return false;
6372 }
6373 }
6374 }
6375
6376 /* Don't optimize displacement for movabs since it only takes 64bit
6377 displacement. */
6378 if (i.disp_encoding > disp_encoding_8bit
6379 || (flag_code == CODE_64BIT && t->mnem_off == MN_movabs))
6380 return true;
6381
6382 for (op = i.operands; op-- > 0;)
40fb9820 6383 if (operand_type_check (i.types[op], disp))
252b5132 6384 {
b300c311 6385 if (i.op[op].disps->X_op == O_constant)
252b5132 6386 {
91d6fa6a 6387 offsetT op_disp = i.op[op].disps->X_add_number;
29b0f896 6388
91d6fa6a 6389 if (!op_disp && i.types[op].bitfield.baseindex)
b300c311 6390 {
2f2be86b
JB
6391 i.types[op] = operand_type_and_not (i.types[op], anydisp);
6392 i.op[op].disps = NULL;
b300c311 6393 i.disp_operands--;
f185acdd
JB
6394 continue;
6395 }
6396
6397 if (i.types[op].bitfield.disp16
cd613c1f 6398 && fits_in_unsigned_word (op_disp))
f185acdd
JB
6399 {
6400 /* If this operand is at most 16 bits, convert
6401 to a signed 16 bit number and don't use 64bit
6402 displacement. */
6403 op_disp = ((op_disp ^ 0x8000) - 0x8000);
6404 i.types[op].bitfield.disp64 = 0;
b300c311 6405 }
f185acdd 6406
28a167a4 6407#ifdef BFD64
a50187b2 6408 /* Optimize 64-bit displacement to 32-bit for 64-bit BFD. */
a775efc8
JB
6409 if ((flag_code != CODE_64BIT
6410 ? i.types[op].bitfield.disp32
0de704b9
JB
6411 : want_disp32 (t)
6412 && (!t->opcode_modifier.jump
a775efc8 6413 || i.jumpabsolute || i.types[op].bitfield.baseindex))
a50187b2 6414 && fits_in_unsigned_long (op_disp))
b300c311 6415 {
a50187b2
JB
6416 /* If this operand is at most 32 bits, convert
6417 to a signed 32 bit number and don't use 64bit
6418 displacement. */
6419 op_disp = (op_disp ^ ((offsetT) 1 << 31)) - ((addressT) 1 << 31);
6420 i.types[op].bitfield.disp64 = 0;
6421 i.types[op].bitfield.disp32 = 1;
6422 }
28a167a4 6423
a50187b2
JB
6424 if (flag_code == CODE_64BIT && fits_in_signed_long (op_disp))
6425 {
6426 i.types[op].bitfield.disp64 = 0;
a775efc8 6427 i.types[op].bitfield.disp32 = 1;
b300c311 6428 }
28a167a4 6429#endif
40fb9820 6430 if ((i.types[op].bitfield.disp32
40fb9820 6431 || i.types[op].bitfield.disp16)
b5014f7a 6432 && fits_in_disp8 (op_disp))
40fb9820 6433 i.types[op].bitfield.disp8 = 1;
77c59789
JB
6434
6435 i.op[op].disps->X_add_number = op_disp;
252b5132 6436 }
67a4f2b7
AO
6437 else if (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
6438 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL)
6439 {
6440 fix_new_exp (frag_now, frag_more (0) - frag_now->fr_literal, 0,
6441 i.op[op].disps, 0, i.reloc[op]);
2f2be86b 6442 i.types[op] = operand_type_and_not (i.types[op], anydisp);
67a4f2b7
AO
6443 }
6444 else
b300c311 6445 /* We only support 64bit displacement on constants. */
40fb9820 6446 i.types[op].bitfield.disp64 = 0;
252b5132 6447 }
0de704b9
JB
6448
6449 return true;
29b0f896
AM
6450}
6451
4a1b91ea
L
6452/* Return 1 if there is a match in broadcast bytes between operand
6453 GIVEN and instruction template T. */
6454
6455static INLINE int
6456match_broadcast_size (const insn_template *t, unsigned int given)
6457{
6458 return ((t->opcode_modifier.broadcast == BYTE_BROADCAST
6459 && i.types[given].bitfield.byte)
6460 || (t->opcode_modifier.broadcast == WORD_BROADCAST
6461 && i.types[given].bitfield.word)
6462 || (t->opcode_modifier.broadcast == DWORD_BROADCAST
6463 && i.types[given].bitfield.dword)
6464 || (t->opcode_modifier.broadcast == QWORD_BROADCAST
6465 && i.types[given].bitfield.qword));
6466}
6467
6c30d220
L
6468/* Check if operands are valid for the instruction. */
6469
6470static int
6471check_VecOperands (const insn_template *t)
6472{
43234a1e 6473 unsigned int op;
e2195274 6474 i386_cpu_flags cpu;
e2195274
JB
6475
6476 /* Templates allowing for ZMMword as well as YMMword and/or XMMword for
6477 any one operand are implicity requiring AVX512VL support if the actual
6478 operand size is YMMword or XMMword. Since this function runs after
a61cb9db
L
6479 template matching, there's no need to check for YMMword/XMMword in
6480 the template. */
734dfd1c 6481 cpu = cpu_flags_and (cpu_flags_from_attr (t->cpu), avx512);
e2195274 6482 if (!cpu_flags_all_zero (&cpu)
734dfd1c 6483 && !is_cpu (t, CpuAVX512VL)
e2195274
JB
6484 && !cpu_arch_flags.bitfield.cpuavx512vl)
6485 {
6486 for (op = 0; op < t->operands; ++op)
6487 {
6488 if (t->operand_types[op].bitfield.zmmword
6489 && (i.types[op].bitfield.ymmword
6490 || i.types[op].bitfield.xmmword))
6491 {
6492 i.error = unsupported;
6493 return 1;
6494 }
6495 }
6496 }
43234a1e 6497
22c36940
JB
6498 /* Somewhat similarly, templates specifying both AVX and AVX2 are
6499 requiring AVX2 support if the actual operand size is YMMword. */
734dfd1c 6500 if (is_cpu (t, CpuAVX) && is_cpu (t, CpuAVX2)
22c36940
JB
6501 && !cpu_arch_flags.bitfield.cpuavx2)
6502 {
6503 for (op = 0; op < t->operands; ++op)
6504 {
6505 if (t->operand_types[op].bitfield.xmmword
6506 && i.types[op].bitfield.ymmword)
6507 {
6508 i.error = unsupported;
6509 return 1;
6510 }
6511 }
6512 }
6513
6c30d220 6514 /* Without VSIB byte, we can't have a vector register for index. */
63112cd6 6515 if (!t->opcode_modifier.sib
6c30d220 6516 && i.index_reg
1b54b8d7
JB
6517 && (i.index_reg->reg_type.bitfield.xmmword
6518 || i.index_reg->reg_type.bitfield.ymmword
6519 || i.index_reg->reg_type.bitfield.zmmword))
6c30d220
L
6520 {
6521 i.error = unsupported_vector_index_register;
6522 return 1;
6523 }
6524
ad8ecc81 6525 /* Check if default mask is allowed. */
255571cd 6526 if (t->opcode_modifier.operandconstraint == NO_DEFAULT_MASK
6225c532 6527 && (!i.mask.reg || i.mask.reg->reg_num == 0))
ad8ecc81
MZ
6528 {
6529 i.error = no_default_mask;
6530 return 1;
6531 }
6532
7bab8ab5
JB
6533 /* For VSIB byte, we need a vector register for index, and all vector
6534 registers must be distinct. */
260cd341 6535 if (t->opcode_modifier.sib && t->opcode_modifier.sib != SIBMEM)
7bab8ab5
JB
6536 {
6537 if (!i.index_reg
63112cd6 6538 || !((t->opcode_modifier.sib == VECSIB128
1b54b8d7 6539 && i.index_reg->reg_type.bitfield.xmmword)
63112cd6 6540 || (t->opcode_modifier.sib == VECSIB256
1b54b8d7 6541 && i.index_reg->reg_type.bitfield.ymmword)
63112cd6 6542 || (t->opcode_modifier.sib == VECSIB512
1b54b8d7 6543 && i.index_reg->reg_type.bitfield.zmmword)))
7bab8ab5
JB
6544 {
6545 i.error = invalid_vsib_address;
6546 return 1;
6547 }
6548
6225c532
JB
6549 gas_assert (i.reg_operands == 2 || i.mask.reg);
6550 if (i.reg_operands == 2 && !i.mask.reg)
43234a1e 6551 {
3528c362 6552 gas_assert (i.types[0].bitfield.class == RegSIMD);
1b54b8d7
JB
6553 gas_assert (i.types[0].bitfield.xmmword
6554 || i.types[0].bitfield.ymmword);
3528c362 6555 gas_assert (i.types[2].bitfield.class == RegSIMD);
1b54b8d7
JB
6556 gas_assert (i.types[2].bitfield.xmmword
6557 || i.types[2].bitfield.ymmword);
43234a1e
L
6558 if (operand_check == check_none)
6559 return 0;
6560 if (register_number (i.op[0].regs)
6561 != register_number (i.index_reg)
6562 && register_number (i.op[2].regs)
6563 != register_number (i.index_reg)
6564 && register_number (i.op[0].regs)
6565 != register_number (i.op[2].regs))
6566 return 0;
6567 if (operand_check == check_error)
6568 {
6569 i.error = invalid_vector_register_set;
6570 return 1;
6571 }
6572 as_warn (_("mask, index, and destination registers should be distinct"));
6573 }
6225c532 6574 else if (i.reg_operands == 1 && i.mask.reg)
8444f82a 6575 {
3528c362 6576 if (i.types[1].bitfield.class == RegSIMD
1b54b8d7
JB
6577 && (i.types[1].bitfield.xmmword
6578 || i.types[1].bitfield.ymmword
6579 || i.types[1].bitfield.zmmword)
8444f82a
MZ
6580 && (register_number (i.op[1].regs)
6581 == register_number (i.index_reg)))
6582 {
6583 if (operand_check == check_error)
6584 {
6585 i.error = invalid_vector_register_set;
6586 return 1;
6587 }
6588 if (operand_check != check_none)
6589 as_warn (_("index and destination registers should be distinct"));
6590 }
6591 }
43234a1e 6592 }
7bab8ab5 6593
fc141319
L
6594 /* For AMX instructions with 3 TMM register operands, all operands
6595 must be distinct. */
6596 if (i.reg_operands == 3
6597 && t->operand_types[0].bitfield.tmmword
6598 && (i.op[0].regs == i.op[1].regs
6599 || i.op[0].regs == i.op[2].regs
6600 || i.op[1].regs == i.op[2].regs))
6601 {
6602 i.error = invalid_tmm_register_set;
6603 return 1;
260cd341
LC
6604 }
6605
0cc78721
CL
6606 /* For some special instructions require that destination must be distinct
6607 from source registers. */
255571cd 6608 if (t->opcode_modifier.operandconstraint == DISTINCT_DEST)
0cc78721
CL
6609 {
6610 unsigned int dest_reg = i.operands - 1;
6611
6612 know (i.operands >= 3);
6613
6614 /* #UD if dest_reg == src1_reg or dest_reg == src2_reg. */
6615 if (i.op[dest_reg - 1].regs == i.op[dest_reg].regs
6616 || (i.reg_operands > 2
6617 && i.op[dest_reg - 2].regs == i.op[dest_reg].regs))
6618 {
6619 i.error = invalid_dest_and_src_register_set;
6620 return 1;
6621 }
6622 }
6623
43234a1e
L
6624 /* Check if broadcast is supported by the instruction and is applied
6625 to the memory operand. */
a5748e0d 6626 if (i.broadcast.type || i.broadcast.bytes)
43234a1e 6627 {
8e6e0792 6628 i386_operand_type type, overlap;
43234a1e
L
6629
6630 /* Check if specified broadcast is supported in this instruction,
4a1b91ea 6631 and its broadcast bytes match the memory operand. */
5273a3cd 6632 op = i.broadcast.operand;
8e6e0792 6633 if (!t->opcode_modifier.broadcast
c48dadc9 6634 || !(i.flags[op] & Operand_Mem)
c39e5b26 6635 || (!i.types[op].bitfield.unspecified
4a1b91ea 6636 && !match_broadcast_size (t, op)))
43234a1e
L
6637 {
6638 bad_broadcast:
6639 i.error = unsupported_broadcast;
6640 return 1;
6641 }
8e6e0792
JB
6642
6643 operand_type_set (&type, 0);
a5748e0d 6644 switch (get_broadcast_bytes (t, false))
8e6e0792 6645 {
4a1b91ea
L
6646 case 2:
6647 type.bitfield.word = 1;
6648 break;
6649 case 4:
6650 type.bitfield.dword = 1;
6651 break;
8e6e0792
JB
6652 case 8:
6653 type.bitfield.qword = 1;
6654 break;
6655 case 16:
6656 type.bitfield.xmmword = 1;
6657 break;
6658 case 32:
4fc85f37
JB
6659 if (vector_size < VSZ256)
6660 goto bad_broadcast;
8e6e0792
JB
6661 type.bitfield.ymmword = 1;
6662 break;
6663 case 64:
4fc85f37
JB
6664 if (vector_size < VSZ512)
6665 goto bad_broadcast;
8e6e0792
JB
6666 type.bitfield.zmmword = 1;
6667 break;
6668 default:
6669 goto bad_broadcast;
6670 }
6671
6672 overlap = operand_type_and (type, t->operand_types[op]);
bc49bfd8
JB
6673 if (t->operand_types[op].bitfield.class == RegSIMD
6674 && t->operand_types[op].bitfield.byte
6675 + t->operand_types[op].bitfield.word
6676 + t->operand_types[op].bitfield.dword
6677 + t->operand_types[op].bitfield.qword > 1)
6678 {
6679 overlap.bitfield.xmmword = 0;
6680 overlap.bitfield.ymmword = 0;
6681 overlap.bitfield.zmmword = 0;
6682 }
8e6e0792
JB
6683 if (operand_type_all_zero (&overlap))
6684 goto bad_broadcast;
6685
9c19e9ec 6686 if (t->opcode_modifier.checkoperandsize)
8e6e0792
JB
6687 {
6688 unsigned int j;
6689
e2195274 6690 type.bitfield.baseindex = 1;
8e6e0792
JB
6691 for (j = 0; j < i.operands; ++j)
6692 {
6693 if (j != op
6694 && !operand_type_register_match(i.types[j],
6695 t->operand_types[j],
6696 type,
6697 t->operand_types[op]))
6698 goto bad_broadcast;
6699 }
6700 }
43234a1e
L
6701 }
6702 /* If broadcast is supported in this instruction, we need to check if
6703 operand of one-element size isn't specified without broadcast. */
6704 else if (t->opcode_modifier.broadcast && i.mem_operands)
6705 {
6706 /* Find memory operand. */
6707 for (op = 0; op < i.operands; op++)
8dc0818e 6708 if (i.flags[op] & Operand_Mem)
43234a1e
L
6709 break;
6710 gas_assert (op < i.operands);
6711 /* Check size of the memory operand. */
4a1b91ea 6712 if (match_broadcast_size (t, op))
43234a1e
L
6713 {
6714 i.error = broadcast_needed;
6715 return 1;
6716 }
6717 }
c39e5b26
JB
6718 else
6719 op = MAX_OPERANDS - 1; /* Avoid uninitialized variable warning. */
43234a1e
L
6720
6721 /* Check if requested masking is supported. */
6225c532 6722 if (i.mask.reg)
43234a1e 6723 {
b1c79256 6724 if (!t->opcode_modifier.masking)
ae2387fe 6725 {
b1c79256
JB
6726 i.error = unsupported_masking;
6727 return 1;
6728 }
6729
6730 /* Common rules for masking:
6731 - mask register destinations permit only zeroing-masking, without
6732 that actually being expressed by a {z} operand suffix or EVEX.z,
6733 - memory destinations allow only merging-masking,
6734 - scatter/gather insns (i.e. ones using vSIB) only allow merging-
6735 masking. */
6736 if (i.mask.zeroing
6737 && (t->operand_types[t->operands - 1].bitfield.class == RegMask
6738 || (i.flags[t->operands - 1] & Operand_Mem)
6739 || t->opcode_modifier.sib))
6740 {
6741 i.error = unsupported_masking;
6742 return 1;
ae2387fe 6743 }
43234a1e
L
6744 }
6745
6746 /* Check if masking is applied to dest operand. */
6225c532 6747 if (i.mask.reg && (i.mask.operand != i.operands - 1))
43234a1e
L
6748 {
6749 i.error = mask_not_on_destination;
6750 return 1;
6751 }
6752
43234a1e 6753 /* Check RC/SAE. */
ca5312a2 6754 if (i.rounding.type != rc_none)
43234a1e 6755 {
a80195f1 6756 if (!t->opcode_modifier.sae
cf665fee
JB
6757 || ((i.rounding.type != saeonly) != t->opcode_modifier.staticrounding)
6758 || i.mem_operands)
43234a1e
L
6759 {
6760 i.error = unsupported_rc_sae;
6761 return 1;
6762 }
cf665fee
JB
6763
6764 /* Non-EVEX.LIG forms need to have a ZMM register as at least one
6765 operand. */
6766 if (t->opcode_modifier.evex != EVEXLIG)
7bab8ab5 6767 {
cf665fee
JB
6768 for (op = 0; op < t->operands; ++op)
6769 if (i.types[op].bitfield.zmmword)
6770 break;
6771 if (op >= t->operands)
6772 {
6773 i.error = operand_size_mismatch;
6774 return 1;
6775 }
7bab8ab5 6776 }
6c30d220
L
6777 }
6778
da4977e0 6779 /* Check the special Imm4 cases; must be the first operand. */
734dfd1c 6780 if (is_cpu (t, CpuXOP) && t->operands == 5)
da4977e0
JB
6781 {
6782 if (i.op[0].imms->X_op != O_constant
6783 || !fits_in_imm4 (i.op[0].imms->X_add_number))
6784 {
6785 i.error = bad_imm4;
6786 return 1;
6787 }
6788
6789 /* Turn off Imm<N> so that update_imm won't complain. */
6790 operand_type_set (&i.types[0], 0);
6791 }
6792
43234a1e 6793 /* Check vector Disp8 operand. */
b5014f7a 6794 if (t->opcode_modifier.disp8memshift
1a42a9fe 6795 && i.disp_encoding <= disp_encoding_8bit)
43234a1e 6796 {
9b345ce8 6797 if (i.broadcast.type || i.broadcast.bytes)
4a1b91ea 6798 i.memshift = t->opcode_modifier.broadcast - 1;
7091c612 6799 else if (t->opcode_modifier.disp8memshift != DISP8_SHIFT_VL)
43234a1e 6800 i.memshift = t->opcode_modifier.disp8memshift;
7091c612
JB
6801 else
6802 {
125ff819 6803 const i386_operand_type *type = NULL, *fallback = NULL;
7091c612
JB
6804
6805 i.memshift = 0;
6806 for (op = 0; op < i.operands; op++)
8dc0818e 6807 if (i.flags[op] & Operand_Mem)
7091c612 6808 {
4174bfff
JB
6809 if (t->opcode_modifier.evex == EVEXLIG)
6810 i.memshift = 2 + (i.suffix == QWORD_MNEM_SUFFIX);
6811 else if (t->operand_types[op].bitfield.xmmword
6812 + t->operand_types[op].bitfield.ymmword
6813 + t->operand_types[op].bitfield.zmmword <= 1)
7091c612
JB
6814 type = &t->operand_types[op];
6815 else if (!i.types[op].bitfield.unspecified)
6816 type = &i.types[op];
125ff819
JB
6817 else /* Ambiguities get resolved elsewhere. */
6818 fallback = &t->operand_types[op];
7091c612 6819 }
3528c362 6820 else if (i.types[op].bitfield.class == RegSIMD
4174bfff 6821 && t->opcode_modifier.evex != EVEXLIG)
7091c612
JB
6822 {
6823 if (i.types[op].bitfield.zmmword)
6824 i.memshift = 6;
6825 else if (i.types[op].bitfield.ymmword && i.memshift < 5)
6826 i.memshift = 5;
6827 else if (i.types[op].bitfield.xmmword && i.memshift < 4)
6828 i.memshift = 4;
6829 }
6830
125ff819
JB
6831 if (!type && !i.memshift)
6832 type = fallback;
7091c612
JB
6833 if (type)
6834 {
6835 if (type->bitfield.zmmword)
6836 i.memshift = 6;
6837 else if (type->bitfield.ymmword)
6838 i.memshift = 5;
6839 else if (type->bitfield.xmmword)
6840 i.memshift = 4;
6841 }
6842
6843 /* For the check in fits_in_disp8(). */
6844 if (i.memshift == 0)
6845 i.memshift = -1;
6846 }
43234a1e
L
6847
6848 for (op = 0; op < i.operands; op++)
6849 if (operand_type_check (i.types[op], disp)
6850 && i.op[op].disps->X_op == O_constant)
6851 {
b5014f7a 6852 if (fits_in_disp8 (i.op[op].disps->X_add_number))
43234a1e 6853 {
b5014f7a
JB
6854 i.types[op].bitfield.disp8 = 1;
6855 return 0;
43234a1e 6856 }
b5014f7a 6857 i.types[op].bitfield.disp8 = 0;
43234a1e
L
6858 }
6859 }
b5014f7a
JB
6860
6861 i.memshift = 0;
43234a1e 6862
6c30d220
L
6863 return 0;
6864}
6865
da4977e0 6866/* Check if encoding requirements are met by the instruction. */
a683cc34
SP
6867
6868static int
da4977e0 6869VEX_check_encoding (const insn_template *t)
a683cc34 6870{
da4977e0
JB
6871 if (i.vec_encoding == vex_encoding_error)
6872 {
6873 i.error = unsupported;
6874 return 1;
6875 }
6876
4fc85f37
JB
6877 /* Vector size restrictions. */
6878 if ((vector_size < VSZ512
6879 && (t->opcode_modifier.evex == EVEX512
6880 || t->opcode_modifier.vsz >= VSZ512))
6881 || (vector_size < VSZ256
6882 && (t->opcode_modifier.evex == EVEX256
6883 || t->opcode_modifier.vex == VEX256
6884 || t->opcode_modifier.vsz >= VSZ256)))
6885 {
6886 i.error = unsupported;
6887 return 1;
6888 }
6889
86fa6981 6890 if (i.vec_encoding == vex_encoding_evex)
43234a1e 6891 {
86fa6981 6892 /* This instruction must be encoded with EVEX prefix. */
e771e7c9 6893 if (!is_evex_encoding (t))
86fa6981
L
6894 {
6895 i.error = unsupported;
6896 return 1;
6897 }
6898 return 0;
43234a1e
L
6899 }
6900
a683cc34 6901 if (!t->opcode_modifier.vex)
86fa6981
L
6902 {
6903 /* This instruction template doesn't have VEX prefix. */
6904 if (i.vec_encoding != vex_encoding_default)
6905 {
6906 i.error = unsupported;
6907 return 1;
6908 }
6909 return 0;
6910 }
a683cc34 6911
a683cc34
SP
6912 return 0;
6913}
6914
7b94647a
JB
6915/* Helper function for the progress() macro in match_template(). */
6916static INLINE enum i386_error progress (enum i386_error new,
6917 enum i386_error last,
6918 unsigned int line, unsigned int *line_p)
6919{
6920 if (line <= *line_p)
6921 return last;
6922 *line_p = line;
6923 return new;
6924}
6925
d3ce72d0 6926static const insn_template *
83b16ac6 6927match_template (char mnem_suffix)
29b0f896
AM
6928{
6929 /* Points to template once we've found it. */
d3ce72d0 6930 const insn_template *t;
40fb9820 6931 i386_operand_type overlap0, overlap1, overlap2, overlap3;
c0f3af97 6932 i386_operand_type overlap4;
29b0f896 6933 unsigned int found_reverse_match;
40fb9820 6934 i386_operand_type operand_types [MAX_OPERANDS];
539e75ad 6935 int addr_prefix_disp;
7b94647a
JB
6936 unsigned int j, size_match, check_register, errline = __LINE__;
6937 enum i386_error specific_error = number_of_operands_mismatch;
6938#define progress(err) progress (err, specific_error, __LINE__, &errline)
29b0f896 6939
c0f3af97
L
6940#if MAX_OPERANDS != 5
6941# error "MAX_OPERANDS must be 5."
f48ff2ae
L
6942#endif
6943
29b0f896 6944 found_reverse_match = 0;
539e75ad 6945 addr_prefix_disp = -1;
40fb9820 6946
45aa61fe 6947 for (t = current_templates->start; t < current_templates->end; t++)
29b0f896 6948 {
539e75ad 6949 addr_prefix_disp = -1;
dbbc8b7e 6950 found_reverse_match = 0;
539e75ad 6951
7b94647a 6952 /* Must have right number of operands. */
29b0f896
AM
6953 if (i.operands != t->operands)
6954 continue;
6955
50aecf8c 6956 /* Check processor support. */
7b94647a 6957 specific_error = progress (unsupported);
45a4bb20 6958 if (cpu_flags_match (t) != CPU_FLAGS_PERFECT_MATCH)
50aecf8c
L
6959 continue;
6960
e1d4d893 6961 /* Check AT&T mnemonic. */
7b94647a 6962 specific_error = progress (unsupported_with_intel_mnemonic);
e1d4d893 6963 if (intel_mnemonic && t->opcode_modifier.attmnemonic)
1efbbeb4
L
6964 continue;
6965
4b5aaf5f 6966 /* Check AT&T/Intel syntax. */
7b94647a 6967 specific_error = progress (unsupported_syntax);
5c07affc 6968 if ((intel_syntax && t->opcode_modifier.attsyntax)
4b5aaf5f 6969 || (!intel_syntax && t->opcode_modifier.intelsyntax))
1efbbeb4
L
6970 continue;
6971
4b5aaf5f
L
6972 /* Check Intel64/AMD64 ISA. */
6973 switch (isa64)
6974 {
6975 default:
6976 /* Default: Don't accept Intel64. */
6977 if (t->opcode_modifier.isa64 == INTEL64)
6978 continue;
6979 break;
6980 case amd64:
6981 /* -mamd64: Don't accept Intel64 and Intel64 only. */
6982 if (t->opcode_modifier.isa64 >= INTEL64)
6983 continue;
6984 break;
6985 case intel64:
6986 /* -mintel64: Don't accept AMD64. */
5990e377 6987 if (t->opcode_modifier.isa64 == AMD64 && flag_code == CODE_64BIT)
4b5aaf5f
L
6988 continue;
6989 break;
6990 }
6991
dc2be329 6992 /* Check the suffix. */
7b94647a 6993 specific_error = progress (invalid_instruction_suffix);
7505bb03
JB
6994 if ((t->opcode_modifier.no_bsuf && mnem_suffix == BYTE_MNEM_SUFFIX)
6995 || (t->opcode_modifier.no_wsuf && mnem_suffix == WORD_MNEM_SUFFIX)
6996 || (t->opcode_modifier.no_lsuf && mnem_suffix == LONG_MNEM_SUFFIX)
6997 || (t->opcode_modifier.no_ssuf && mnem_suffix == SHORT_MNEM_SUFFIX)
6998 || (t->opcode_modifier.no_qsuf && mnem_suffix == QWORD_MNEM_SUFFIX))
83b16ac6 6999 continue;
29b0f896 7000
7b94647a 7001 specific_error = progress (operand_size_mismatch);
3ac21baa
JB
7002 size_match = operand_size_match (t);
7003 if (!size_match)
7d5e4556 7004 continue;
539e75ad 7005
6f2f06be
JB
7006 /* This is intentionally not
7007
0cfa3eb3 7008 if (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE))
6f2f06be
JB
7009
7010 as the case of a missing * on the operand is accepted (perhaps with
7011 a warning, issued further down). */
7b94647a 7012 specific_error = progress (operand_type_mismatch);
0cfa3eb3 7013 if (i.jumpabsolute && t->opcode_modifier.jump != JUMP_ABSOLUTE)
7b94647a 7014 continue;
6f2f06be 7015
a4d3acd2
JB
7016 /* In Intel syntax, normally we can check for memory operand size when
7017 there is no mnemonic suffix. But jmp and call have 2 different
7018 encodings with Dword memory operand size. Skip the "near" one
7019 (permitting a register operand) when "far" was requested. */
7020 if (i.far_branch
7021 && t->opcode_modifier.jump == JUMP_ABSOLUTE
7022 && t->operand_types[0].bitfield.class == Reg)
7023 continue;
7024
5c07affc
L
7025 for (j = 0; j < MAX_OPERANDS; j++)
7026 operand_types[j] = t->operand_types[j];
7027
9db83a32 7028 /* In general, don't allow 32-bit operands on pre-386. */
7b94647a
JB
7029 specific_error = progress (mnem_suffix ? invalid_instruction_suffix
7030 : operand_size_mismatch);
4873e243 7031 j = i.imm_operands + (t->operands > i.imm_operands + 1);
9db83a32
JB
7032 if (i.suffix == LONG_MNEM_SUFFIX
7033 && !cpu_arch_flags.bitfield.cpui386
45aa61fe 7034 && (intel_syntax
3cd7f3e3 7035 ? (t->opcode_modifier.mnemonicsize != IGNORESIZE
76d3f746
JB
7036 && !intel_float_operand (insn_name (t)))
7037 : intel_float_operand (insn_name (t)) != 2)
4873e243
JB
7038 && (t->operands == i.imm_operands
7039 || (operand_types[i.imm_operands].bitfield.class != RegMMX
7040 && operand_types[i.imm_operands].bitfield.class != RegSIMD
7041 && operand_types[i.imm_operands].bitfield.class != RegMask)
7042 || (operand_types[j].bitfield.class != RegMMX
7043 && operand_types[j].bitfield.class != RegSIMD
7044 && operand_types[j].bitfield.class != RegMask))
63112cd6 7045 && !t->opcode_modifier.sib)
192dc9c6
JB
7046 continue;
7047
29b0f896 7048 /* Do not verify operands when there are none. */
e365e234 7049 if (!t->operands)
da4977e0
JB
7050 {
7051 if (VEX_check_encoding (t))
7052 {
7b94647a 7053 specific_error = progress (i.error);
da4977e0
JB
7054 continue;
7055 }
7056
7057 /* We've found a match; break out of loop. */
7058 break;
7059 }
252b5132 7060
48bcea9f
JB
7061 if (!t->opcode_modifier.jump
7062 || t->opcode_modifier.jump == JUMP_ABSOLUTE)
7063 {
7064 /* There should be only one Disp operand. */
7065 for (j = 0; j < MAX_OPERANDS; j++)
7066 if (operand_type_check (operand_types[j], disp))
539e75ad 7067 break;
48bcea9f
JB
7068 if (j < MAX_OPERANDS)
7069 {
5b7c81bd 7070 bool override = (i.prefix[ADDR_PREFIX] != 0);
48bcea9f
JB
7071
7072 addr_prefix_disp = j;
7073
a775efc8
JB
7074 /* Address size prefix will turn Disp64 operand into Disp32 and
7075 Disp32/Disp16 one into Disp16/Disp32 respectively. */
48bcea9f 7076 switch (flag_code)
40fb9820 7077 {
48bcea9f
JB
7078 case CODE_16BIT:
7079 override = !override;
7080 /* Fall through. */
7081 case CODE_32BIT:
7082 if (operand_types[j].bitfield.disp32
7083 && operand_types[j].bitfield.disp16)
40fb9820 7084 {
48bcea9f
JB
7085 operand_types[j].bitfield.disp16 = override;
7086 operand_types[j].bitfield.disp32 = !override;
40fb9820 7087 }
a775efc8 7088 gas_assert (!operand_types[j].bitfield.disp64);
48bcea9f
JB
7089 break;
7090
7091 case CODE_64BIT:
a775efc8 7092 if (operand_types[j].bitfield.disp64)
40fb9820 7093 {
a775efc8 7094 gas_assert (!operand_types[j].bitfield.disp32);
48bcea9f 7095 operand_types[j].bitfield.disp32 = override;
a775efc8 7096 operand_types[j].bitfield.disp64 = !override;
40fb9820 7097 }
48bcea9f
JB
7098 operand_types[j].bitfield.disp16 = 0;
7099 break;
40fb9820 7100 }
539e75ad 7101 }
48bcea9f 7102 }
539e75ad 7103
56ffb741 7104 /* We check register size if needed. */
9c19e9ec 7105 if (t->opcode_modifier.checkoperandsize)
e2195274
JB
7106 {
7107 check_register = (1 << t->operands) - 1;
a5748e0d 7108 if (i.broadcast.type || i.broadcast.bytes)
5273a3cd 7109 check_register &= ~(1 << i.broadcast.operand);
e2195274
JB
7110 }
7111 else
7112 check_register = 0;
7113
c6fb90c8 7114 overlap0 = operand_type_and (i.types[0], operand_types[0]);
29b0f896
AM
7115 switch (t->operands)
7116 {
7117 case 1:
40fb9820 7118 if (!operand_type_match (overlap0, i.types[0]))
29b0f896 7119 continue;
ae9a0a51
JB
7120
7121 /* Allow the ModR/M encoding to be requested by using the {load} or
7122 {store} pseudo prefix on an applicable insn. */
7123 if (!t->opcode_modifier.modrm
7124 && i.reg_operands == 1
7125 && ((i.dir_encoding == dir_encoding_load
7126 && t->mnem_off != MN_pop)
7127 || (i.dir_encoding == dir_encoding_store
7128 && t->mnem_off != MN_push))
7129 /* Avoid BSWAP. */
7130 && t->mnem_off != MN_bswap)
7131 continue;
29b0f896 7132 break;
ae9a0a51 7133
29b0f896 7134 case 2:
33eaf5de 7135 /* xchg %eax, %eax is a special case. It is an alias for nop
8b38ad71
L
7136 only in 32bit mode and we can use opcode 0x90. In 64bit
7137 mode, we can't use 0x90 for xchg %eax, %eax since it should
7138 zero-extend %eax to %rax. */
ae9a0a51
JB
7139 if (t->base_opcode == 0x90
7140 && t->opcode_space == SPACE_BASE)
7141 {
7142 if (flag_code == CODE_64BIT
7143 && i.types[0].bitfield.instance == Accum
7144 && i.types[0].bitfield.dword
7145 && i.types[1].bitfield.instance == Accum)
7146 continue;
7147
7148 /* Allow the ModR/M encoding to be requested by using the
7149 {load} or {store} pseudo prefix. */
7150 if (i.dir_encoding == dir_encoding_load
7151 || i.dir_encoding == dir_encoding_store)
7152 continue;
7153 }
e3669c7f
JB
7154
7155 if (t->base_opcode == MOV_AX_DISP32
ddb62495 7156 && t->opcode_space == SPACE_BASE
69196391 7157 && t->mnem_off != MN_movabs)
e3669c7f
JB
7158 {
7159 /* Force 0x8b encoding for "mov foo@GOT, %eax". */
7160 if (i.reloc[0] == BFD_RELOC_386_GOT32)
7161 continue;
7162
7163 /* xrelease mov %eax, <disp> is another special case. It must not
7164 match the accumulator-only encoding of mov. */
7165 if (i.hle_prefix)
7166 continue;
ae9a0a51
JB
7167
7168 /* Allow the ModR/M encoding to be requested by using a suitable
7169 {load} or {store} pseudo prefix. */
7170 if (i.dir_encoding == (i.types[0].bitfield.instance == Accum
7171 ? dir_encoding_store
7172 : dir_encoding_load)
7173 && !i.types[0].bitfield.disp64
7174 && !i.types[1].bitfield.disp64)
7175 continue;
7176 }
7177
7178 /* Allow the ModR/M encoding to be requested by using the {load} or
7179 {store} pseudo prefix on an applicable insn. */
7180 if (!t->opcode_modifier.modrm
7181 && i.reg_operands == 1
7182 && i.imm_operands == 1
7183 && (i.dir_encoding == dir_encoding_load
7184 || i.dir_encoding == dir_encoding_store)
7185 && t->opcode_space == SPACE_BASE)
7186 {
7187 if (t->base_opcode == 0xb0 /* mov $imm, %reg */
7188 && i.dir_encoding == dir_encoding_store)
7189 continue;
7190
7191 if ((t->base_opcode | 0x38) == 0x3c /* <alu> $imm, %acc */
7192 && (t->base_opcode != 0x3c /* cmp $imm, %acc */
7193 || i.dir_encoding == dir_encoding_load))
7194 continue;
7195
7196 if (t->base_opcode == 0xa8 /* test $imm, %acc */
7197 && i.dir_encoding == dir_encoding_load)
7198 continue;
e3669c7f 7199 }
f5eb1d70
JB
7200 /* Fall through. */
7201
7202 case 3:
3ac21baa
JB
7203 if (!(size_match & MATCH_STRAIGHT))
7204 goto check_reverse;
64c49ab3
JB
7205 /* Reverse direction of operands if swapping is possible in the first
7206 place (operands need to be symmetric) and
7207 - the load form is requested, and the template is a store form,
7208 - the store form is requested, and the template is a load form,
7209 - the non-default (swapped) form is requested. */
7210 overlap1 = operand_type_and (operand_types[0], operand_types[1]);
f5eb1d70 7211 if (t->opcode_modifier.d && i.reg_operands == i.operands
64c49ab3
JB
7212 && !operand_type_all_zero (&overlap1))
7213 switch (i.dir_encoding)
7214 {
7215 case dir_encoding_load:
7216 if (operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 7217 || t->opcode_modifier.regmem)
64c49ab3
JB
7218 goto check_reverse;
7219 break;
7220
7221 case dir_encoding_store:
7222 if (!operand_type_check (operand_types[i.operands - 1], anymem)
dfd69174 7223 && !t->opcode_modifier.regmem)
64c49ab3
JB
7224 goto check_reverse;
7225 break;
7226
7227 case dir_encoding_swap:
7228 goto check_reverse;
7229
7230 case dir_encoding_default:
7231 break;
7232 }
86fa6981 7233 /* If we want store form, we skip the current load. */
64c49ab3
JB
7234 if ((i.dir_encoding == dir_encoding_store
7235 || i.dir_encoding == dir_encoding_swap)
86fa6981
L
7236 && i.mem_operands == 0
7237 && t->opcode_modifier.load)
fa99fab2 7238 continue;
1a0670f3 7239 /* Fall through. */
f48ff2ae 7240 case 4:
c0f3af97 7241 case 5:
c6fb90c8 7242 overlap1 = operand_type_and (i.types[1], operand_types[1]);
40fb9820
L
7243 if (!operand_type_match (overlap0, i.types[0])
7244 || !operand_type_match (overlap1, i.types[1])
e2195274 7245 || ((check_register & 3) == 3
dc821c5f 7246 && !operand_type_register_match (i.types[0],
40fb9820 7247 operand_types[0],
dc821c5f 7248 i.types[1],
40fb9820 7249 operand_types[1])))
29b0f896 7250 {
7b94647a
JB
7251 specific_error = progress (i.error);
7252
29b0f896 7253 /* Check if other direction is valid ... */
38e314eb 7254 if (!t->opcode_modifier.d)
29b0f896
AM
7255 continue;
7256
dc1e8a47 7257 check_reverse:
3ac21baa
JB
7258 if (!(size_match & MATCH_REVERSE))
7259 continue;
29b0f896 7260 /* Try reversing direction of operands. */
734dfd1c
JB
7261 j = is_cpu (t, CpuFMA4)
7262 || is_cpu (t, CpuXOP) ? 1 : i.operands - 1;
8bd915b7
JB
7263 overlap0 = operand_type_and (i.types[0], operand_types[j]);
7264 overlap1 = operand_type_and (i.types[j], operand_types[0]);
c975cec5
JB
7265 overlap2 = operand_type_and (i.types[1], operand_types[1]);
7266 gas_assert (t->operands != 3 || !check_register);
40fb9820 7267 if (!operand_type_match (overlap0, i.types[0])
8bd915b7 7268 || !operand_type_match (overlap1, i.types[j])
c975cec5
JB
7269 || (t->operands == 3
7270 && !operand_type_match (overlap2, i.types[1]))
45664ddb 7271 || (check_register
dc821c5f 7272 && !operand_type_register_match (i.types[0],
8bd915b7
JB
7273 operand_types[j],
7274 i.types[j],
45664ddb 7275 operand_types[0])))
29b0f896
AM
7276 {
7277 /* Does not match either direction. */
7b94647a 7278 specific_error = progress (i.error);
29b0f896
AM
7279 continue;
7280 }
ac9226cf 7281 /* found_reverse_match holds which variant of D
29b0f896 7282 we've found. */
38e314eb
JB
7283 if (!t->opcode_modifier.d)
7284 found_reverse_match = 0;
7285 else if (operand_types[0].bitfield.tbyte)
ac9226cf 7286 {
4943d587
JB
7287 if (t->opcode_modifier.operandconstraint != UGH)
7288 found_reverse_match = Opcode_FloatD;
bd782808
JB
7289 else
7290 found_reverse_match = ~0;
ac9226cf 7291 /* FSUB{,R} and FDIV{,R} may need a 2nd bit flipped. */
bd782808 7292 if ((t->extension_opcode & 4)
ac9226cf
JB
7293 && (intel_syntax || intel_mnemonic))
7294 found_reverse_match |= Opcode_FloatR;
7295 }
734dfd1c 7296 else if (is_cpu (t, CpuFMA4) || is_cpu (t, CpuXOP))
8bd915b7
JB
7297 {
7298 found_reverse_match = Opcode_VexW;
7299 goto check_operands_345;
7300 }
ddb62495
JB
7301 else if (t->opcode_space != SPACE_BASE
7302 && (t->opcode_space != SPACE_0F
2c735193
JB
7303 /* MOV to/from CR/DR/TR, as an exception, follow
7304 the base opcode space encoding model. */
7305 || (t->base_opcode | 7) != 0x27))
dbbc8b7e 7306 found_reverse_match = (t->base_opcode & 0xee) != 0x6e
2c735193 7307 ? Opcode_ExtD : Opcode_SIMD_IntD;
a33ef3c2 7308 else if (!t->opcode_modifier.commutative)
38e314eb 7309 found_reverse_match = Opcode_D;
a33ef3c2
JB
7310 else
7311 found_reverse_match = ~0;
29b0f896 7312 }
f48ff2ae 7313 else
29b0f896 7314 {
f48ff2ae 7315 /* Found a forward 2 operand match here. */
8bd915b7 7316 check_operands_345:
d1cbb4db
L
7317 switch (t->operands)
7318 {
c0f3af97 7319 case 5:
3d0738af 7320 overlap4 = operand_type_and (i.types[4], operand_types[4]);
c0f3af97 7321 if (!operand_type_match (overlap4, i.types[4])
dc821c5f 7322 || !operand_type_register_match (i.types[3],
c0f3af97 7323 operand_types[3],
c0f3af97
L
7324 i.types[4],
7325 operand_types[4]))
7b94647a
JB
7326 {
7327 specific_error = progress (i.error);
7328 continue;
7329 }
1a0670f3 7330 /* Fall through. */
f48ff2ae 7331 case 4:
3d0738af 7332 overlap3 = operand_type_and (i.types[3], operand_types[3]);
40fb9820 7333 if (!operand_type_match (overlap3, i.types[3])
e2195274
JB
7334 || ((check_register & 0xa) == 0xa
7335 && !operand_type_register_match (i.types[1],
f7768225
JB
7336 operand_types[1],
7337 i.types[3],
e2195274
JB
7338 operand_types[3]))
7339 || ((check_register & 0xc) == 0xc
7340 && !operand_type_register_match (i.types[2],
7341 operand_types[2],
7342 i.types[3],
7343 operand_types[3])))
7b94647a
JB
7344 {
7345 specific_error = progress (i.error);
7346 continue;
7347 }
1a0670f3 7348 /* Fall through. */
f48ff2ae 7349 case 3:
3d0738af 7350 overlap2 = operand_type_and (i.types[2], operand_types[2]);
40fb9820 7351 if (!operand_type_match (overlap2, i.types[2])
e2195274
JB
7352 || ((check_register & 5) == 5
7353 && !operand_type_register_match (i.types[0],
23e42951
JB
7354 operand_types[0],
7355 i.types[2],
e2195274
JB
7356 operand_types[2]))
7357 || ((check_register & 6) == 6
7358 && !operand_type_register_match (i.types[1],
7359 operand_types[1],
7360 i.types[2],
7361 operand_types[2])))
7b94647a
JB
7362 {
7363 specific_error = progress (i.error);
7364 continue;
7365 }
f48ff2ae
L
7366 break;
7367 }
29b0f896 7368 }
f48ff2ae 7369 /* Found either forward/reverse 2, 3 or 4 operand match here:
29b0f896
AM
7370 slip through to break. */
7371 }
c0f3af97 7372
9bb4d860
L
7373 /* Check if VEX/EVEX encoding requirements can be satisfied. */
7374 if (VEX_check_encoding (t))
da4977e0 7375 {
7b94647a 7376 specific_error = progress (i.error);
da4977e0
JB
7377 continue;
7378 }
7379
9bb4d860
L
7380 /* Check if vector operands are valid. */
7381 if (check_VecOperands (t))
5614d22c 7382 {
7b94647a 7383 specific_error = progress (i.error);
5614d22c
JB
7384 continue;
7385 }
a683cc34 7386
29b0f896
AM
7387 /* We've found a match; break out of loop. */
7388 break;
7389 }
7390
7b94647a
JB
7391#undef progress
7392
29b0f896
AM
7393 if (t == current_templates->end)
7394 {
7395 /* We found no match. */
04784e33 7396 i.error = specific_error;
fa99fab2 7397 return NULL;
29b0f896 7398 }
252b5132 7399
29b0f896
AM
7400 if (!quiet_warnings)
7401 {
7402 if (!intel_syntax
0cfa3eb3 7403 && (i.jumpabsolute != (t->opcode_modifier.jump == JUMP_ABSOLUTE)))
76d3f746 7404 as_warn (_("indirect %s without `*'"), insn_name (t));
29b0f896 7405
40fb9820 7406 if (t->opcode_modifier.isprefix
3cd7f3e3 7407 && t->opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7408 {
7409 /* Warn them that a data or address size prefix doesn't
7410 affect assembly of the next line of code. */
76d3f746 7411 as_warn (_("stand-alone `%s' prefix"), insn_name (t));
29b0f896
AM
7412 }
7413 }
7414
7415 /* Copy the template we found. */
9a182d04 7416 install_template (t);
539e75ad
L
7417
7418 if (addr_prefix_disp != -1)
7419 i.tm.operand_types[addr_prefix_disp]
7420 = operand_types[addr_prefix_disp];
7421
8bd915b7 7422 switch (found_reverse_match)
29b0f896 7423 {
8bd915b7
JB
7424 case 0:
7425 break;
7426
bd782808
JB
7427 case Opcode_FloatR:
7428 case Opcode_FloatR | Opcode_FloatD:
7429 i.tm.extension_opcode ^= Opcode_FloatR >> 3;
7430 found_reverse_match &= Opcode_FloatD;
7431
7432 /* Fall through. */
8bd915b7 7433 default:
dfd69174
JB
7434 /* If we found a reverse match we must alter the opcode direction
7435 bit and clear/flip the regmem modifier one. found_reverse_match
7436 holds bits to change (different for int & float insns). */
29b0f896
AM
7437
7438 i.tm.base_opcode ^= found_reverse_match;
7439
dfd69174
JB
7440 /* Certain SIMD insns have their load forms specified in the opcode
7441 table, and hence we need to _set_ RegMem instead of clearing it.
7442 We need to avoid setting the bit though on insns like KMOVW. */
7443 i.tm.opcode_modifier.regmem
7444 = i.tm.opcode_modifier.modrm && i.tm.opcode_modifier.d
7445 && i.tm.operands > 2U - i.tm.opcode_modifier.sse2avx
7446 && !i.tm.opcode_modifier.regmem;
a33ef3c2
JB
7447
7448 /* Fall through. */
7449 case ~0:
7450 i.tm.operand_types[0] = operand_types[i.operands - 1];
7451 i.tm.operand_types[i.operands - 1] = operand_types[0];
8bd915b7
JB
7452 break;
7453
7454 case Opcode_VexW:
7455 /* Only the first two register operands need reversing, alongside
7456 flipping VEX.W. */
7457 i.tm.opcode_modifier.vexw ^= VEXW0 ^ VEXW1;
7458
7459 j = i.tm.operand_types[0].bitfield.imm8;
7460 i.tm.operand_types[j] = operand_types[j + 1];
7461 i.tm.operand_types[j + 1] = operand_types[j];
7462 break;
29b0f896
AM
7463 }
7464
fa99fab2 7465 return t;
29b0f896
AM
7466}
7467
7468static int
e3bb37b5 7469check_string (void)
29b0f896 7470{
51c8edf6
JB
7471 unsigned int es_op = i.tm.opcode_modifier.isstring - IS_STRING_ES_OP0;
7472 unsigned int op = i.tm.operand_types[0].bitfield.baseindex ? es_op : 0;
8dc0818e 7473
5e042380 7474 if (i.seg[op] != NULL && i.seg[op] != reg_es)
29b0f896 7475 {
51c8edf6 7476 as_bad (_("`%s' operand %u must use `%ses' segment"),
76d3f746 7477 insn_name (&i.tm),
51c8edf6
JB
7478 intel_syntax ? i.tm.operands - es_op : es_op + 1,
7479 register_prefix);
7480 return 0;
29b0f896 7481 }
51c8edf6
JB
7482
7483 /* There's only ever one segment override allowed per instruction.
7484 This instruction possibly has a legal segment override on the
7485 second operand, so copy the segment to where non-string
7486 instructions store it, allowing common code. */
7487 i.seg[op] = i.seg[1];
7488
29b0f896
AM
7489 return 1;
7490}
7491
7492static int
543613e9 7493process_suffix (void)
29b0f896 7494{
7fc69528 7495 bool is_movx = false;
8b65b895 7496
29b0f896
AM
7497 /* If matched instruction specifies an explicit instruction mnemonic
7498 suffix, use it. */
673fe0f0 7499 if (i.tm.opcode_modifier.size == SIZE16)
40fb9820 7500 i.suffix = WORD_MNEM_SUFFIX;
673fe0f0 7501 else if (i.tm.opcode_modifier.size == SIZE32)
40fb9820 7502 i.suffix = LONG_MNEM_SUFFIX;
673fe0f0 7503 else if (i.tm.opcode_modifier.size == SIZE64)
40fb9820 7504 i.suffix = QWORD_MNEM_SUFFIX;
13e600d0 7505 else if (i.reg_operands
c8f8eebc 7506 && (i.operands > 1 || i.types[0].bitfield.class == Reg)
255571cd 7507 && i.tm.opcode_modifier.operandconstraint != ADDR_PREFIX_OP_REG)
29b0f896 7508 {
65fca059 7509 unsigned int numop = i.operands;
389d00a5
JB
7510
7511 /* MOVSX/MOVZX */
ddb62495 7512 is_movx = (i.tm.opcode_space == SPACE_0F
389d00a5 7513 && (i.tm.base_opcode | 8) == 0xbe)
ddb62495 7514 || (i.tm.opcode_space == SPACE_BASE
389d00a5 7515 && i.tm.base_opcode == 0x63
734dfd1c 7516 && is_cpu (&i.tm, Cpu64));
389d00a5 7517
65fca059
JB
7518 /* movsx/movzx want only their source operand considered here, for the
7519 ambiguity checking below. The suffix will be replaced afterwards
7520 to represent the destination (register). */
389d00a5 7521 if (is_movx && (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63))
65fca059
JB
7522 --i.operands;
7523
643bb870 7524 /* crc32 needs REX.W set regardless of suffix / source operand size. */
7fc69528 7525 if (i.tm.mnem_off == MN_crc32 && i.tm.operand_types[1].bitfield.qword)
643bb870
JB
7526 i.rex |= REX_W;
7527
29b0f896 7528 /* If there's no instruction mnemonic suffix we try to invent one
13e600d0 7529 based on GPR operands. */
29b0f896
AM
7530 if (!i.suffix)
7531 {
7532 /* We take i.suffix from the last register operand specified,
7533 Destination register type is more significant than source
381d071f
L
7534 register type. crc32 in SSE4.2 prefers source register
7535 type. */
7fc69528 7536 unsigned int op = i.tm.mnem_off == MN_crc32 ? 1 : i.operands;
20592a94 7537
1a035124
JB
7538 while (op--)
7539 if (i.tm.operand_types[op].bitfield.instance == InstanceNone
7540 || i.tm.operand_types[op].bitfield.instance == Accum)
7541 {
7542 if (i.types[op].bitfield.class != Reg)
7543 continue;
7544 if (i.types[op].bitfield.byte)
7545 i.suffix = BYTE_MNEM_SUFFIX;
7546 else if (i.types[op].bitfield.word)
7547 i.suffix = WORD_MNEM_SUFFIX;
7548 else if (i.types[op].bitfield.dword)
7549 i.suffix = LONG_MNEM_SUFFIX;
7550 else if (i.types[op].bitfield.qword)
7551 i.suffix = QWORD_MNEM_SUFFIX;
7552 else
7553 continue;
7554 break;
7555 }
65fca059
JB
7556
7557 /* As an exception, movsx/movzx silently default to a byte source
7558 in AT&T mode. */
389d00a5 7559 if (is_movx && i.tm.opcode_modifier.w && !i.suffix && !intel_syntax)
65fca059 7560 i.suffix = BYTE_MNEM_SUFFIX;
29b0f896
AM
7561 }
7562 else if (i.suffix == BYTE_MNEM_SUFFIX)
7563 {
1cb0ab18 7564 if (!check_byte_reg ())
29b0f896
AM
7565 return 0;
7566 }
7567 else if (i.suffix == LONG_MNEM_SUFFIX)
7568 {
1cb0ab18 7569 if (!check_long_reg ())
29b0f896
AM
7570 return 0;
7571 }
7572 else if (i.suffix == QWORD_MNEM_SUFFIX)
7573 {
1cb0ab18 7574 if (!check_qword_reg ())
29b0f896
AM
7575 return 0;
7576 }
7577 else if (i.suffix == WORD_MNEM_SUFFIX)
7578 {
1cb0ab18 7579 if (!check_word_reg ())
29b0f896
AM
7580 return 0;
7581 }
3cd7f3e3
L
7582 else if (intel_syntax
7583 && i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
29b0f896
AM
7584 /* Do nothing if the instruction is going to ignore the prefix. */
7585 ;
7586 else
7587 abort ();
65fca059
JB
7588
7589 /* Undo the movsx/movzx change done above. */
7590 i.operands = numop;
29b0f896 7591 }
3cd7f3e3
L
7592 else if (i.tm.opcode_modifier.mnemonicsize == DEFAULTSIZE
7593 && !i.suffix)
29b0f896 7594 {
13e600d0
JB
7595 i.suffix = stackop_size;
7596 if (stackop_size == LONG_MNEM_SUFFIX)
06f74c5c
L
7597 {
7598 /* stackop_size is set to LONG_MNEM_SUFFIX for the
7599 .code16gcc directive to support 16-bit mode with
7600 32-bit address. For IRET without a suffix, generate
7601 16-bit IRET (opcode 0xcf) to return from an interrupt
7602 handler. */
13e600d0
JB
7603 if (i.tm.base_opcode == 0xcf)
7604 {
7605 i.suffix = WORD_MNEM_SUFFIX;
7606 as_warn (_("generating 16-bit `iret' for .code16gcc directive"));
7607 }
7608 /* Warn about changed behavior for segment register push/pop. */
7609 else if ((i.tm.base_opcode | 1) == 0x07)
7610 as_warn (_("generating 32-bit `%s', unlike earlier gas versions"),
76d3f746 7611 insn_name (&i.tm));
06f74c5c 7612 }
29b0f896 7613 }
c006a730 7614 else if (!i.suffix
0cfa3eb3
JB
7615 && (i.tm.opcode_modifier.jump == JUMP_ABSOLUTE
7616 || i.tm.opcode_modifier.jump == JUMP_BYTE
7617 || i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT
ddb62495 7618 || (i.tm.opcode_space == SPACE_0F
389d00a5 7619 && i.tm.base_opcode == 0x01 /* [ls][gi]dt */
64e74474 7620 && i.tm.extension_opcode <= 3)))
9306ca4a
JB
7621 {
7622 switch (flag_code)
7623 {
7624 case CODE_64BIT:
40fb9820 7625 if (!i.tm.opcode_modifier.no_qsuf)
9306ca4a 7626 {
828c2a25
JB
7627 if (i.tm.opcode_modifier.jump == JUMP_BYTE
7628 || i.tm.opcode_modifier.no_lsuf)
7629 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a
JB
7630 break;
7631 }
1a0670f3 7632 /* Fall through. */
9306ca4a 7633 case CODE_32BIT:
40fb9820 7634 if (!i.tm.opcode_modifier.no_lsuf)
9306ca4a
JB
7635 i.suffix = LONG_MNEM_SUFFIX;
7636 break;
7637 case CODE_16BIT:
40fb9820 7638 if (!i.tm.opcode_modifier.no_wsuf)
9306ca4a
JB
7639 i.suffix = WORD_MNEM_SUFFIX;
7640 break;
7641 }
7642 }
252b5132 7643
c006a730 7644 if (!i.suffix
3cd7f3e3 7645 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8
JB
7646 /* Also cover lret/retf/iret in 64-bit mode. */
7647 || (flag_code == CODE_64BIT
7648 && !i.tm.opcode_modifier.no_lsuf
7649 && !i.tm.opcode_modifier.no_qsuf))
3cd7f3e3 7650 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
8bbb3ad8
JB
7651 /* Explicit sizing prefixes are assumed to disambiguate insns. */
7652 && !i.prefix[DATA_PREFIX] && !(i.prefix[REX_PREFIX] & REX_W)
62b3f548
JB
7653 /* Accept FLDENV et al without suffix. */
7654 && (i.tm.opcode_modifier.no_ssuf || i.tm.opcode_modifier.floatmf))
29b0f896 7655 {
6c0946d0 7656 unsigned int suffixes, evex = 0;
c006a730
JB
7657
7658 suffixes = !i.tm.opcode_modifier.no_bsuf;
7659 if (!i.tm.opcode_modifier.no_wsuf)
7660 suffixes |= 1 << 1;
7661 if (!i.tm.opcode_modifier.no_lsuf)
7662 suffixes |= 1 << 2;
c006a730
JB
7663 if (!i.tm.opcode_modifier.no_ssuf)
7664 suffixes |= 1 << 4;
7665 if (flag_code == CODE_64BIT && !i.tm.opcode_modifier.no_qsuf)
7666 suffixes |= 1 << 5;
7667
6c0946d0
JB
7668 /* For [XYZ]MMWORD operands inspect operand sizes. While generally
7669 also suitable for AT&T syntax mode, it was requested that this be
7670 restricted to just Intel syntax. */
a5748e0d
JB
7671 if (intel_syntax && is_any_vex_encoding (&i.tm)
7672 && !i.broadcast.type && !i.broadcast.bytes)
6c0946d0 7673 {
b9915cbc 7674 unsigned int op;
6c0946d0 7675
b9915cbc 7676 for (op = 0; op < i.tm.operands; ++op)
6c0946d0 7677 {
4fc85f37
JB
7678 if (vector_size < VSZ512)
7679 {
7680 i.tm.operand_types[op].bitfield.zmmword = 0;
7681 if (vector_size < VSZ256)
7682 {
7683 i.tm.operand_types[op].bitfield.ymmword = 0;
7684 if (i.tm.operand_types[op].bitfield.xmmword
7685 && (i.tm.opcode_modifier.evex == EVEXDYN
7686 || (!i.tm.opcode_modifier.evex
7687 && is_evex_encoding (&i.tm))))
7688 i.tm.opcode_modifier.evex = EVEX128;
7689 }
7690 else if (i.tm.operand_types[op].bitfield.ymmword
7691 && !i.tm.operand_types[op].bitfield.xmmword
7692 && (i.tm.opcode_modifier.evex == EVEXDYN
7693 || (!i.tm.opcode_modifier.evex
7694 && is_evex_encoding (&i.tm))))
7695 i.tm.opcode_modifier.evex = EVEX256;
7696 }
7697 else if (is_evex_encoding (&i.tm)
7698 && !cpu_arch_flags.bitfield.cpuavx512vl)
6c0946d0 7699 {
b9915cbc
JB
7700 if (i.tm.operand_types[op].bitfield.ymmword)
7701 i.tm.operand_types[op].bitfield.xmmword = 0;
7702 if (i.tm.operand_types[op].bitfield.zmmword)
7703 i.tm.operand_types[op].bitfield.ymmword = 0;
7704 if (!i.tm.opcode_modifier.evex
7705 || i.tm.opcode_modifier.evex == EVEXDYN)
7706 i.tm.opcode_modifier.evex = EVEX512;
7707 }
6c0946d0 7708
b9915cbc
JB
7709 if (i.tm.operand_types[op].bitfield.xmmword
7710 + i.tm.operand_types[op].bitfield.ymmword
7711 + i.tm.operand_types[op].bitfield.zmmword < 2)
7712 continue;
6c0946d0 7713
b9915cbc
JB
7714 /* Any properly sized operand disambiguates the insn. */
7715 if (i.types[op].bitfield.xmmword
7716 || i.types[op].bitfield.ymmword
7717 || i.types[op].bitfield.zmmword)
7718 {
7719 suffixes &= ~(7 << 6);
7720 evex = 0;
7721 break;
7722 }
6c0946d0 7723
b9915cbc
JB
7724 if ((i.flags[op] & Operand_Mem)
7725 && i.tm.operand_types[op].bitfield.unspecified)
7726 {
7727 if (i.tm.operand_types[op].bitfield.xmmword)
7728 suffixes |= 1 << 6;
7729 if (i.tm.operand_types[op].bitfield.ymmword)
7730 suffixes |= 1 << 7;
7731 if (i.tm.operand_types[op].bitfield.zmmword)
7732 suffixes |= 1 << 8;
7733 if (is_evex_encoding (&i.tm))
7734 evex = EVEX512;
6c0946d0
JB
7735 }
7736 }
7737 }
7738
7739 /* Are multiple suffixes / operand sizes allowed? */
c006a730 7740 if (suffixes & (suffixes - 1))
9306ca4a 7741 {
873494c8 7742 if (intel_syntax
3cd7f3e3 7743 && (i.tm.opcode_modifier.mnemonicsize != DEFAULTSIZE
873494c8 7744 || operand_check == check_error))
9306ca4a 7745 {
76d3f746 7746 as_bad (_("ambiguous operand size for `%s'"), insn_name (&i.tm));
9306ca4a
JB
7747 return 0;
7748 }
c006a730 7749 if (operand_check == check_error)
9306ca4a 7750 {
c006a730 7751 as_bad (_("no instruction mnemonic suffix given and "
76d3f746 7752 "no register operands; can't size `%s'"), insn_name (&i.tm));
9306ca4a
JB
7753 return 0;
7754 }
c006a730 7755 if (operand_check == check_warning)
873494c8
JB
7756 as_warn (_("%s; using default for `%s'"),
7757 intel_syntax
7758 ? _("ambiguous operand size")
7759 : _("no instruction mnemonic suffix given and "
7760 "no register operands"),
76d3f746 7761 insn_name (&i.tm));
c006a730
JB
7762
7763 if (i.tm.opcode_modifier.floatmf)
7764 i.suffix = SHORT_MNEM_SUFFIX;
389d00a5 7765 else if (is_movx)
65fca059 7766 /* handled below */;
6c0946d0
JB
7767 else if (evex)
7768 i.tm.opcode_modifier.evex = evex;
c006a730
JB
7769 else if (flag_code == CODE_16BIT)
7770 i.suffix = WORD_MNEM_SUFFIX;
1a035124 7771 else if (!i.tm.opcode_modifier.no_lsuf)
c006a730 7772 i.suffix = LONG_MNEM_SUFFIX;
1a035124
JB
7773 else
7774 i.suffix = QWORD_MNEM_SUFFIX;
9306ca4a 7775 }
29b0f896 7776 }
252b5132 7777
389d00a5 7778 if (is_movx)
65fca059
JB
7779 {
7780 /* In Intel syntax, movsx/movzx must have a "suffix" (checked above).
7781 In AT&T syntax, if there is no suffix (warned about above), the default
7782 will be byte extension. */
7783 if (i.tm.opcode_modifier.w && i.suffix && i.suffix != BYTE_MNEM_SUFFIX)
7784 i.tm.base_opcode |= 1;
7785
7786 /* For further processing, the suffix should represent the destination
7787 (register). This is already the case when one was used with
7788 mov[sz][bw]*, but we need to replace it for mov[sz]x, or if there was
7789 no suffix to begin with. */
7790 if (i.tm.opcode_modifier.w || i.tm.base_opcode == 0x63 || !i.suffix)
7791 {
7792 if (i.types[1].bitfield.word)
7793 i.suffix = WORD_MNEM_SUFFIX;
7794 else if (i.types[1].bitfield.qword)
7795 i.suffix = QWORD_MNEM_SUFFIX;
7796 else
7797 i.suffix = LONG_MNEM_SUFFIX;
7798
7799 i.tm.opcode_modifier.w = 0;
7800 }
7801 }
7802
50128d0c
JB
7803 if (!i.tm.opcode_modifier.modrm && i.reg_operands && i.tm.operands < 3)
7804 i.short_form = (i.tm.operand_types[0].bitfield.class == Reg)
7805 != (i.tm.operand_types[1].bitfield.class == Reg);
7806
d2224064
JB
7807 /* Change the opcode based on the operand size given by i.suffix. */
7808 switch (i.suffix)
29b0f896 7809 {
d2224064
JB
7810 /* Size floating point instruction. */
7811 case LONG_MNEM_SUFFIX:
7812 if (i.tm.opcode_modifier.floatmf)
7813 {
7814 i.tm.base_opcode ^= 4;
7815 break;
7816 }
7817 /* fall through */
7818 case WORD_MNEM_SUFFIX:
7819 case QWORD_MNEM_SUFFIX:
29b0f896 7820 /* It's not a byte, select word/dword operation. */
40fb9820 7821 if (i.tm.opcode_modifier.w)
29b0f896 7822 {
50128d0c 7823 if (i.short_form)
29b0f896
AM
7824 i.tm.base_opcode |= 8;
7825 else
7826 i.tm.base_opcode |= 1;
7827 }
d2224064
JB
7828 /* fall through */
7829 case SHORT_MNEM_SUFFIX:
29b0f896
AM
7830 /* Now select between word & dword operations via the operand
7831 size prefix, except for instructions that will ignore this
7832 prefix anyway. */
c8f8eebc 7833 if (i.suffix != QWORD_MNEM_SUFFIX
3cd7f3e3 7834 && i.tm.opcode_modifier.mnemonicsize != IGNORESIZE
c8f8eebc
JB
7835 && !i.tm.opcode_modifier.floatmf
7836 && !is_any_vex_encoding (&i.tm)
7837 && ((i.suffix == LONG_MNEM_SUFFIX) == (flag_code == CODE_16BIT)
7838 || (flag_code == CODE_64BIT
7839 && i.tm.opcode_modifier.jump == JUMP_BYTE)))
24eab124
AM
7840 {
7841 unsigned int prefix = DATA_PREFIX_OPCODE;
543613e9 7842
0cfa3eb3 7843 if (i.tm.opcode_modifier.jump == JUMP_BYTE) /* jcxz, loop */
29b0f896 7844 prefix = ADDR_PREFIX_OPCODE;
252b5132 7845
29b0f896
AM
7846 if (!add_prefix (prefix))
7847 return 0;
24eab124 7848 }
252b5132 7849
29b0f896
AM
7850 /* Set mode64 for an operand. */
7851 if (i.suffix == QWORD_MNEM_SUFFIX
9146926a 7852 && flag_code == CODE_64BIT
d2224064 7853 && !i.tm.opcode_modifier.norex64
4ed21b58 7854 && !i.tm.opcode_modifier.vexw
46e883c5 7855 /* Special case for xchg %rax,%rax. It is NOP and doesn't
d2224064
JB
7856 need rex64. */
7857 && ! (i.operands == 2
7858 && i.tm.base_opcode == 0x90
ddb62495 7859 && i.tm.opcode_space == SPACE_BASE
75e5731b
JB
7860 && i.types[0].bitfield.instance == Accum
7861 && i.types[0].bitfield.qword
44732014 7862 && i.types[1].bitfield.instance == Accum))
d2224064 7863 i.rex |= REX_W;
3e73aa7c 7864
d2224064 7865 break;
8bbb3ad8
JB
7866
7867 case 0:
f9a6a8f0 7868 /* Select word/dword/qword operation with explicit data sizing prefix
8bbb3ad8
JB
7869 when there are no suitable register operands. */
7870 if (i.tm.opcode_modifier.w
7871 && (i.prefix[DATA_PREFIX] || (i.prefix[REX_PREFIX] & REX_W))
7872 && (!i.reg_operands
7873 || (i.reg_operands == 1
7874 /* ShiftCount */
7875 && (i.tm.operand_types[0].bitfield.instance == RegC
7876 /* InOutPortReg */
7877 || i.tm.operand_types[0].bitfield.instance == RegD
7878 || i.tm.operand_types[1].bitfield.instance == RegD
7fc69528 7879 || i.tm.mnem_off == MN_crc32))))
8bbb3ad8
JB
7880 i.tm.base_opcode |= 1;
7881 break;
29b0f896 7882 }
7ecd2f8b 7883
255571cd 7884 if (i.tm.opcode_modifier.operandconstraint == ADDR_PREFIX_OP_REG)
c0a30a9f 7885 {
c8f8eebc
JB
7886 gas_assert (!i.suffix);
7887 gas_assert (i.reg_operands);
c0a30a9f 7888
c8f8eebc
JB
7889 if (i.tm.operand_types[0].bitfield.instance == Accum
7890 || i.operands == 1)
7891 {
7892 /* The address size override prefix changes the size of the
7893 first operand. */
7894 if (flag_code == CODE_64BIT
7895 && i.op[0].regs->reg_type.bitfield.word)
7896 {
7897 as_bad (_("16-bit addressing unavailable for `%s'"),
76d3f746 7898 insn_name (&i.tm));
c8f8eebc
JB
7899 return 0;
7900 }
7901
7902 if ((flag_code == CODE_32BIT
7903 ? i.op[0].regs->reg_type.bitfield.word
7904 : i.op[0].regs->reg_type.bitfield.dword)
7905 && !add_prefix (ADDR_PREFIX_OPCODE))
7906 return 0;
7907 }
c0a30a9f
L
7908 else
7909 {
c8f8eebc
JB
7910 /* Check invalid register operand when the address size override
7911 prefix changes the size of register operands. */
7912 unsigned int op;
7913 enum { need_word, need_dword, need_qword } need;
7914
27f13469 7915 /* Check the register operand for the address size prefix if
b3a3496f 7916 the memory operand has no real registers, like symbol, DISP
829f3fe1 7917 or bogus (x32-only) symbol(%rip) when symbol(%eip) is meant. */
27f13469
L
7918 if (i.mem_operands == 1
7919 && i.reg_operands == 1
7920 && i.operands == 2
27f13469 7921 && i.types[1].bitfield.class == Reg
b3a3496f
L
7922 && (flag_code == CODE_32BIT
7923 ? i.op[1].regs->reg_type.bitfield.word
7924 : i.op[1].regs->reg_type.bitfield.dword)
7925 && ((i.base_reg == NULL && i.index_reg == NULL)
829f3fe1
JB
7926#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
7927 || (x86_elf_abi == X86_64_X32_ABI
7928 && i.base_reg
b3a3496f
L
7929 && i.base_reg->reg_num == RegIP
7930 && i.base_reg->reg_type.bitfield.qword))
829f3fe1
JB
7931#else
7932 || 0)
7933#endif
27f13469
L
7934 && !add_prefix (ADDR_PREFIX_OPCODE))
7935 return 0;
7936
c8f8eebc
JB
7937 if (flag_code == CODE_32BIT)
7938 need = i.prefix[ADDR_PREFIX] ? need_word : need_dword;
7939 else if (i.prefix[ADDR_PREFIX])
c0a30a9f
L
7940 need = need_dword;
7941 else
7942 need = flag_code == CODE_64BIT ? need_qword : need_word;
c0a30a9f 7943
c8f8eebc
JB
7944 for (op = 0; op < i.operands; op++)
7945 {
7946 if (i.types[op].bitfield.class != Reg)
7947 continue;
7948
7949 switch (need)
7950 {
7951 case need_word:
7952 if (i.op[op].regs->reg_type.bitfield.word)
7953 continue;
7954 break;
7955 case need_dword:
7956 if (i.op[op].regs->reg_type.bitfield.dword)
7957 continue;
7958 break;
7959 case need_qword:
7960 if (i.op[op].regs->reg_type.bitfield.qword)
7961 continue;
7962 break;
7963 }
7964
7965 as_bad (_("invalid register operand size for `%s'"),
76d3f746 7966 insn_name (&i.tm));
c8f8eebc
JB
7967 return 0;
7968 }
7969 }
c0a30a9f
L
7970 }
7971
29b0f896
AM
7972 return 1;
7973}
3e73aa7c 7974
29b0f896 7975static int
543613e9 7976check_byte_reg (void)
29b0f896
AM
7977{
7978 int op;
543613e9 7979
29b0f896
AM
7980 for (op = i.operands; --op >= 0;)
7981 {
dc821c5f 7982 /* Skip non-register operands. */
bab6aec1 7983 if (i.types[op].bitfield.class != Reg)
dc821c5f
JB
7984 continue;
7985
29b0f896
AM
7986 /* If this is an eight bit register, it's OK. If it's the 16 or
7987 32 bit version of an eight bit register, we will just use the
7988 low portion, and that's OK too. */
dc821c5f 7989 if (i.types[op].bitfield.byte)
29b0f896
AM
7990 continue;
7991
5a819eb9 7992 /* I/O port address operands are OK too. */
75e5731b
JB
7993 if (i.tm.operand_types[op].bitfield.instance == RegD
7994 && i.tm.operand_types[op].bitfield.word)
5a819eb9
JB
7995 continue;
7996
9706160a 7997 /* crc32 only wants its source operand checked here. */
7fc69528 7998 if (i.tm.mnem_off == MN_crc32 && op != 0)
9344ff29
L
7999 continue;
8000
29b0f896 8001 /* Any other register is bad. */
73c76375
JB
8002 as_bad (_("`%s%s' not allowed with `%s%c'"),
8003 register_prefix, i.op[op].regs->reg_name,
76d3f746 8004 insn_name (&i.tm), i.suffix);
73c76375 8005 return 0;
29b0f896
AM
8006 }
8007 return 1;
8008}
8009
8010static int
e3bb37b5 8011check_long_reg (void)
29b0f896
AM
8012{
8013 int op;
8014
8015 for (op = i.operands; --op >= 0;)
dc821c5f 8016 /* Skip non-register operands. */
bab6aec1 8017 if (i.types[op].bitfield.class != Reg)
dc821c5f 8018 continue;
29b0f896
AM
8019 /* Reject eight bit registers, except where the template requires
8020 them. (eg. movzb) */
dc821c5f 8021 else if (i.types[op].bitfield.byte
bab6aec1 8022 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 8023 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
8024 && (i.tm.operand_types[op].bitfield.word
8025 || i.tm.operand_types[op].bitfield.dword))
29b0f896 8026 {
a540244d
L
8027 as_bad (_("`%s%s' not allowed with `%s%c'"),
8028 register_prefix,
29b0f896 8029 i.op[op].regs->reg_name,
76d3f746 8030 insn_name (&i.tm),
29b0f896
AM
8031 i.suffix);
8032 return 0;
8033 }
be4c5e58
L
8034 /* Error if the e prefix on a general reg is missing. */
8035 else if (i.types[op].bitfield.word
bab6aec1 8036 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 8037 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 8038 && i.tm.operand_types[op].bitfield.dword)
29b0f896 8039 {
be4c5e58
L
8040 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
8041 register_prefix, i.op[op].regs->reg_name,
8042 i.suffix);
8043 return 0;
252b5132 8044 }
e4630f71 8045 /* Warn if the r prefix on a general reg is present. */
dc821c5f 8046 else if (i.types[op].bitfield.qword
bab6aec1 8047 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 8048 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 8049 && i.tm.operand_types[op].bitfield.dword)
252b5132 8050 {
1cb0ab18
JB
8051 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
8052 register_prefix, i.op[op].regs->reg_name, i.suffix);
8053 return 0;
29b0f896
AM
8054 }
8055 return 1;
8056}
252b5132 8057
29b0f896 8058static int
e3bb37b5 8059check_qword_reg (void)
29b0f896
AM
8060{
8061 int op;
252b5132 8062
29b0f896 8063 for (op = i.operands; --op >= 0; )
dc821c5f 8064 /* Skip non-register operands. */
bab6aec1 8065 if (i.types[op].bitfield.class != Reg)
dc821c5f 8066 continue;
29b0f896
AM
8067 /* Reject eight bit registers, except where the template requires
8068 them. (eg. movzb) */
dc821c5f 8069 else if (i.types[op].bitfield.byte
bab6aec1 8070 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 8071 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
8072 && (i.tm.operand_types[op].bitfield.word
8073 || i.tm.operand_types[op].bitfield.dword))
29b0f896 8074 {
a540244d
L
8075 as_bad (_("`%s%s' not allowed with `%s%c'"),
8076 register_prefix,
29b0f896 8077 i.op[op].regs->reg_name,
76d3f746 8078 insn_name (&i.tm),
29b0f896
AM
8079 i.suffix);
8080 return 0;
8081 }
e4630f71 8082 /* Warn if the r prefix on a general reg is missing. */
dc821c5f
JB
8083 else if ((i.types[op].bitfield.word
8084 || i.types[op].bitfield.dword)
bab6aec1 8085 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 8086 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 8087 && i.tm.operand_types[op].bitfield.qword)
29b0f896
AM
8088 {
8089 /* Prohibit these changes in the 64bit mode, since the
8090 lowering is more complicated. */
1cb0ab18
JB
8091 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
8092 register_prefix, i.op[op].regs->reg_name, i.suffix);
8093 return 0;
252b5132 8094 }
29b0f896
AM
8095 return 1;
8096}
252b5132 8097
29b0f896 8098static int
e3bb37b5 8099check_word_reg (void)
29b0f896
AM
8100{
8101 int op;
8102 for (op = i.operands; --op >= 0;)
dc821c5f 8103 /* Skip non-register operands. */
bab6aec1 8104 if (i.types[op].bitfield.class != Reg)
dc821c5f 8105 continue;
29b0f896
AM
8106 /* Reject eight bit registers, except where the template requires
8107 them. (eg. movzb) */
dc821c5f 8108 else if (i.types[op].bitfield.byte
bab6aec1 8109 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 8110 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f
JB
8111 && (i.tm.operand_types[op].bitfield.word
8112 || i.tm.operand_types[op].bitfield.dword))
29b0f896 8113 {
a540244d
L
8114 as_bad (_("`%s%s' not allowed with `%s%c'"),
8115 register_prefix,
29b0f896 8116 i.op[op].regs->reg_name,
76d3f746 8117 insn_name (&i.tm),
29b0f896
AM
8118 i.suffix);
8119 return 0;
8120 }
9706160a
JB
8121 /* Error if the e or r prefix on a general reg is present. */
8122 else if ((i.types[op].bitfield.dword
dc821c5f 8123 || i.types[op].bitfield.qword)
bab6aec1 8124 && (i.tm.operand_types[op].bitfield.class == Reg
75e5731b 8125 || i.tm.operand_types[op].bitfield.instance == Accum)
dc821c5f 8126 && i.tm.operand_types[op].bitfield.word)
252b5132 8127 {
9706160a
JB
8128 as_bad (_("incorrect register `%s%s' used with `%c' suffix"),
8129 register_prefix, i.op[op].regs->reg_name,
8130 i.suffix);
8131 return 0;
29b0f896
AM
8132 }
8133 return 1;
8134}
252b5132 8135
29b0f896 8136static int
40fb9820 8137update_imm (unsigned int j)
29b0f896 8138{
bc0844ae 8139 i386_operand_type overlap = i.types[j];
c34d1cc9
JB
8140
8141 if (i.tm.operand_types[j].bitfield.imm8
8142 && i.tm.operand_types[j].bitfield.imm8s
8143 && overlap.bitfield.imm8 && overlap.bitfield.imm8s)
8144 {
8145 /* This combination is used on 8-bit immediates where e.g. $~0 is
8146 desirable to permit. We're past operand type matching, so simply
8147 put things back in the shape they were before introducing the
8148 distinction between Imm8, Imm8S, and Imm8|Imm8S. */
8149 overlap.bitfield.imm8s = 0;
8150 }
8151
be1643ff
JB
8152 if (overlap.bitfield.imm8
8153 + overlap.bitfield.imm8s
8154 + overlap.bitfield.imm16
8155 + overlap.bitfield.imm32
8156 + overlap.bitfield.imm32s
8157 + overlap.bitfield.imm64 > 1)
29b0f896 8158 {
05909f23
JB
8159 static const i386_operand_type imm16 = { .bitfield = { .imm16 = 1 } };
8160 static const i386_operand_type imm32 = { .bitfield = { .imm32 = 1 } };
8161 static const i386_operand_type imm32s = { .bitfield = { .imm32s = 1 } };
8162 static const i386_operand_type imm16_32 = { .bitfield =
8163 { .imm16 = 1, .imm32 = 1 }
8164 };
8165 static const i386_operand_type imm16_32s = { .bitfield =
8166 { .imm16 = 1, .imm32s = 1 }
8167 };
8168 static const i386_operand_type imm16_32_32s = { .bitfield =
8169 { .imm16 = 1, .imm32 = 1, .imm32s = 1 }
8170 };
8171
29b0f896
AM
8172 if (i.suffix)
8173 {
40fb9820
L
8174 i386_operand_type temp;
8175
0dfbf9d7 8176 operand_type_set (&temp, 0);
7ab9ffdd 8177 if (i.suffix == BYTE_MNEM_SUFFIX)
40fb9820
L
8178 {
8179 temp.bitfield.imm8 = overlap.bitfield.imm8;
8180 temp.bitfield.imm8s = overlap.bitfield.imm8s;
8181 }
8182 else if (i.suffix == WORD_MNEM_SUFFIX)
8183 temp.bitfield.imm16 = overlap.bitfield.imm16;
8184 else if (i.suffix == QWORD_MNEM_SUFFIX)
8185 {
8186 temp.bitfield.imm64 = overlap.bitfield.imm64;
8187 temp.bitfield.imm32s = overlap.bitfield.imm32s;
8188 }
8189 else
8190 temp.bitfield.imm32 = overlap.bitfield.imm32;
8191 overlap = temp;
29b0f896 8192 }
0dfbf9d7
L
8193 else if (operand_type_equal (&overlap, &imm16_32_32s)
8194 || operand_type_equal (&overlap, &imm16_32)
8195 || operand_type_equal (&overlap, &imm16_32s))
29b0f896 8196 {
40fb9820 8197 if ((flag_code == CODE_16BIT) ^ (i.prefix[DATA_PREFIX] != 0))
65da13b5 8198 overlap = imm16;
40fb9820 8199 else
65da13b5 8200 overlap = imm32s;
29b0f896 8201 }
8bbb3ad8
JB
8202 else if (i.prefix[REX_PREFIX] & REX_W)
8203 overlap = operand_type_and (overlap, imm32s);
8204 else if (i.prefix[DATA_PREFIX])
8205 overlap = operand_type_and (overlap,
8206 flag_code != CODE_16BIT ? imm16 : imm32);
be1643ff
JB
8207 if (overlap.bitfield.imm8
8208 + overlap.bitfield.imm8s
8209 + overlap.bitfield.imm16
8210 + overlap.bitfield.imm32
8211 + overlap.bitfield.imm32s
8212 + overlap.bitfield.imm64 != 1)
29b0f896 8213 {
4eed87de
AM
8214 as_bad (_("no instruction mnemonic suffix given; "
8215 "can't determine immediate size"));
29b0f896
AM
8216 return 0;
8217 }
8218 }
40fb9820 8219 i.types[j] = overlap;
29b0f896 8220
40fb9820
L
8221 return 1;
8222}
8223
8224static int
8225finalize_imm (void)
8226{
bc0844ae 8227 unsigned int j, n;
29b0f896 8228
bc0844ae
L
8229 /* Update the first 2 immediate operands. */
8230 n = i.operands > 2 ? 2 : i.operands;
8231 if (n)
8232 {
8233 for (j = 0; j < n; j++)
8234 if (update_imm (j) == 0)
8235 return 0;
40fb9820 8236
bc0844ae
L
8237 /* The 3rd operand can't be immediate operand. */
8238 gas_assert (operand_type_check (i.types[2], imm) == 0);
8239 }
29b0f896
AM
8240
8241 return 1;
8242}
8243
0a3eba42
JB
8244static INLINE void set_rex_vrex (const reg_entry *r, unsigned int rex_bit,
8245 bool do_sse2avx)
8246{
8247 if (r->reg_flags & RegRex)
8248 {
8249 if (i.rex & rex_bit)
8250 as_bad (_("same type of prefix used twice"));
8251 i.rex |= rex_bit;
8252 }
8253 else if (do_sse2avx && (i.rex & rex_bit) && i.vex.register_specifier)
8254 {
8255 gas_assert (i.vex.register_specifier == r);
8256 i.vex.register_specifier += 8;
8257 }
8258
8259 if (r->reg_flags & RegVRex)
8260 i.vrex |= rex_bit;
8261}
8262
29b0f896 8263static int
e3bb37b5 8264process_operands (void)
29b0f896
AM
8265{
8266 /* Default segment register this instruction will use for memory
8267 accesses. 0 means unknown. This is only for optimizing out
8268 unnecessary segment overrides. */
5e042380 8269 const reg_entry *default_seg = NULL;
29b0f896 8270
31b4cda3
JB
8271 /* We only need to check those implicit registers for instructions
8272 with 3 operands or less. */
8273 if (i.operands <= 3)
8274 for (unsigned int j = 0; j < i.operands; j++)
8275 if (i.types[j].bitfield.instance != InstanceNone)
8276 i.reg_operands--;
8277
a5aeccd9
JB
8278 if (i.tm.opcode_modifier.sse2avx)
8279 {
8280 /* Legacy encoded insns allow explicit REX prefixes, so these prefixes
8281 need converting. */
8282 i.rex |= i.prefix[REX_PREFIX] & (REX_W | REX_R | REX_X | REX_B);
8283 i.prefix[REX_PREFIX] = 0;
8284 i.rex_encoding = 0;
8285 }
c423d21a
JB
8286 /* ImmExt should be processed after SSE2AVX. */
8287 else if (i.tm.opcode_modifier.immext)
8288 process_immext ();
a5aeccd9 8289
ecb96e55
JB
8290 /* TILEZERO is unusual in that it has a single operand encoded in ModR/M.reg,
8291 not ModR/M.rm. To avoid special casing this in build_modrm_byte(), fake a
8292 new destination operand here, while converting the source one to register
8293 number 0. */
8294 if (i.tm.mnem_off == MN_tilezero)
8295 {
8296 i.op[1].regs = i.op[0].regs;
8297 i.op[0].regs -= i.op[0].regs->reg_num;
8298 i.types[1] = i.types[0];
8299 i.tm.operand_types[1] = i.tm.operand_types[0];
8300 i.flags[1] = i.flags[0];
8301 i.operands++;
8302 i.reg_operands++;
8303 i.tm.operands++;
8304 }
8305
2426c15f 8306 if (i.tm.opcode_modifier.sse2avx && i.tm.opcode_modifier.vexvvvv)
29b0f896 8307 {
05909f23
JB
8308 static const i386_operand_type regxmm = {
8309 .bitfield = { .class = RegSIMD, .xmmword = 1 }
8310 };
91d6fa6a
NC
8311 unsigned int dupl = i.operands;
8312 unsigned int dest = dupl - 1;
9fcfb3d7
L
8313 unsigned int j;
8314
c0f3af97 8315 /* The destination must be an xmm register. */
9c2799c2 8316 gas_assert (i.reg_operands
91d6fa6a 8317 && MAX_OPERANDS > dupl
7ab9ffdd 8318 && operand_type_equal (&i.types[dest], &regxmm));
c0f3af97 8319
75e5731b 8320 if (i.tm.operand_types[0].bitfield.instance == Accum
1b54b8d7 8321 && i.tm.operand_types[0].bitfield.xmmword)
e2ec9d29 8322 {
95dfdd85
JB
8323 /* Keep xmm0 for instructions with VEX prefix and 3
8324 sources. */
8325 i.tm.operand_types[0].bitfield.instance = InstanceNone;
8326 i.tm.operand_types[0].bitfield.class = RegSIMD;
31b4cda3 8327 i.reg_operands++;
95dfdd85 8328 goto duplicate;
c0f3af97 8329 }
95dfdd85
JB
8330
8331 if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_1ST_XMM0)
7ab9ffdd 8332 {
aa180741 8333 gas_assert ((MAX_OPERANDS - 1) > dupl);
c0f3af97
L
8334
8335 /* Add the implicit xmm0 for instructions with VEX prefix
8336 and 3 sources. */
8337 for (j = i.operands; j > 0; j--)
8338 {
8339 i.op[j] = i.op[j - 1];
8340 i.types[j] = i.types[j - 1];
8341 i.tm.operand_types[j] = i.tm.operand_types[j - 1];
8dc0818e 8342 i.flags[j] = i.flags[j - 1];
c0f3af97
L
8343 }
8344 i.op[0].regs
629310ab 8345 = (const reg_entry *) str_hash_find (reg_hash, "xmm0");
7ab9ffdd 8346 i.types[0] = regxmm;
c0f3af97
L
8347 i.tm.operand_types[0] = regxmm;
8348
8349 i.operands += 2;
8350 i.reg_operands += 2;
8351 i.tm.operands += 2;
8352
91d6fa6a 8353 dupl++;
c0f3af97 8354 dest++;
91d6fa6a
NC
8355 i.op[dupl] = i.op[dest];
8356 i.types[dupl] = i.types[dest];
8357 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 8358 i.flags[dupl] = i.flags[dest];
e2ec9d29 8359 }
c0f3af97
L
8360 else
8361 {
dc1e8a47 8362 duplicate:
c0f3af97
L
8363 i.operands++;
8364 i.reg_operands++;
8365 i.tm.operands++;
8366
91d6fa6a
NC
8367 i.op[dupl] = i.op[dest];
8368 i.types[dupl] = i.types[dest];
8369 i.tm.operand_types[dupl] = i.tm.operand_types[dest];
8dc0818e 8370 i.flags[dupl] = i.flags[dest];
c0f3af97
L
8371 }
8372
8373 if (i.tm.opcode_modifier.immext)
8374 process_immext ();
8375 }
75e5731b 8376 else if (i.tm.operand_types[0].bitfield.instance == Accum
bd782808 8377 && i.tm.opcode_modifier.modrm)
c0f3af97
L
8378 {
8379 unsigned int j;
8380
9fcfb3d7
L
8381 for (j = 1; j < i.operands; j++)
8382 {
8383 i.op[j - 1] = i.op[j];
8384 i.types[j - 1] = i.types[j];
8385
8386 /* We need to adjust fields in i.tm since they are used by
8387 build_modrm_byte. */
8388 i.tm.operand_types [j - 1] = i.tm.operand_types [j];
8dc0818e
JB
8389
8390 i.flags[j - 1] = i.flags[j];
9fcfb3d7
L
8391 }
8392
31b4cda3
JB
8393 /* No adjustment to i.reg_operands: This was already done at the top
8394 of the function. */
e2ec9d29 8395 i.operands--;
e2ec9d29
L
8396 i.tm.operands--;
8397 }
255571cd 8398 else if (i.tm.opcode_modifier.operandconstraint == IMPLICIT_QUAD_GROUP)
920d2ddc 8399 {
a477a8c4
JB
8400 unsigned int regnum, first_reg_in_group, last_reg_in_group;
8401
920d2ddc 8402 /* The second operand must be {x,y,z}mmN, where N is a multiple of 4. */
3528c362 8403 gas_assert (i.operands >= 2 && i.types[1].bitfield.class == RegSIMD);
a477a8c4
JB
8404 regnum = register_number (i.op[1].regs);
8405 first_reg_in_group = regnum & ~3;
8406 last_reg_in_group = first_reg_in_group + 3;
8407 if (regnum != first_reg_in_group)
8408 as_warn (_("source register `%s%s' implicitly denotes"
8409 " `%s%.3s%u' to `%s%.3s%u' source group in `%s'"),
8410 register_prefix, i.op[1].regs->reg_name,
8411 register_prefix, i.op[1].regs->reg_name, first_reg_in_group,
8412 register_prefix, i.op[1].regs->reg_name, last_reg_in_group,
76d3f746 8413 insn_name (&i.tm));
a477a8c4 8414 }
255571cd 8415 else if (i.tm.opcode_modifier.operandconstraint == REG_KLUDGE)
e2ec9d29
L
8416 {
8417 /* The imul $imm, %reg instruction is converted into
8418 imul $imm, %reg, %reg, and the clr %reg instruction
8419 is converted into xor %reg, %reg. */
8420
8421 unsigned int first_reg_op;
8422
8423 if (operand_type_check (i.types[0], reg))
8424 first_reg_op = 0;
8425 else
8426 first_reg_op = 1;
8427 /* Pretend we saw the extra register operand. */
9c2799c2 8428 gas_assert (i.reg_operands == 1
7ab9ffdd 8429 && i.op[first_reg_op + 1].regs == 0);
e2ec9d29
L
8430 i.op[first_reg_op + 1].regs = i.op[first_reg_op].regs;
8431 i.types[first_reg_op + 1] = i.types[first_reg_op];
8432 i.operands++;
8433 i.reg_operands++;
29b0f896
AM
8434 }
8435
85b80b0f 8436 if (i.tm.opcode_modifier.modrm)
29b0f896
AM
8437 {
8438 /* The opcode is completed (modulo i.tm.extension_opcode which
52271982
AM
8439 must be put into the modrm byte). Now, we make the modrm and
8440 index base bytes based on all the info we've collected. */
29b0f896
AM
8441
8442 default_seg = build_modrm_byte ();
bd782808
JB
8443
8444 if (!quiet_warnings && i.tm.opcode_modifier.operandconstraint == UGH)
8445 {
8446 /* Warn about some common errors, but press on regardless. */
8447 if (i.operands == 2)
8448 {
8449 /* Reversed arguments on faddp or fmulp. */
8450 as_warn (_("translating to `%s %s%s,%s%s'"), insn_name (&i.tm),
8451 register_prefix, i.op[!intel_syntax].regs->reg_name,
8452 register_prefix, i.op[intel_syntax].regs->reg_name);
8453 }
8454 else if (i.tm.opcode_modifier.mnemonicsize == IGNORESIZE)
8455 {
8456 /* Extraneous `l' suffix on fp insn. */
8457 as_warn (_("translating to `%s %s%s'"), insn_name (&i.tm),
8458 register_prefix, i.op[0].regs->reg_name);
8459 }
8460 }
29b0f896 8461 }
0ff3b7d0 8462 else if (i.types[0].bitfield.class == SReg && !dot_insn ())
85b80b0f
JB
8463 {
8464 if (flag_code != CODE_64BIT
8465 ? i.tm.base_opcode == POP_SEG_SHORT
8466 && i.op[0].regs->reg_num == 1
389d00a5 8467 : (i.tm.base_opcode | 1) == (POP_SEG386_SHORT & 0xff)
85b80b0f
JB
8468 && i.op[0].regs->reg_num < 4)
8469 {
8470 as_bad (_("you can't `%s %s%s'"),
76d3f746 8471 insn_name (&i.tm), register_prefix, i.op[0].regs->reg_name);
85b80b0f
JB
8472 return 0;
8473 }
389d00a5 8474 if (i.op[0].regs->reg_num > 3
ddb62495 8475 && i.tm.opcode_space == SPACE_BASE )
85b80b0f 8476 {
389d00a5 8477 i.tm.base_opcode ^= (POP_SEG_SHORT ^ POP_SEG386_SHORT) & 0xff;
ddb62495 8478 i.tm.opcode_space = SPACE_0F;
85b80b0f
JB
8479 }
8480 i.tm.base_opcode |= (i.op[0].regs->reg_num << 3);
8481 }
ddb62495 8482 else if (i.tm.opcode_space == SPACE_BASE
389d00a5 8483 && (i.tm.base_opcode & ~3) == MOV_AX_DISP32)
29b0f896 8484 {
5e042380 8485 default_seg = reg_ds;
29b0f896 8486 }
40fb9820 8487 else if (i.tm.opcode_modifier.isstring)
29b0f896
AM
8488 {
8489 /* For the string instructions that allow a segment override
8490 on one of their operands, the default segment is ds. */
5e042380 8491 default_seg = reg_ds;
29b0f896 8492 }
50128d0c 8493 else if (i.short_form)
85b80b0f 8494 {
0ff3b7d0
JB
8495 /* The register operand is in the 1st or 2nd non-immediate operand. */
8496 const reg_entry *r = i.op[i.imm_operands].regs;
85b80b0f 8497
0ff3b7d0
JB
8498 if (!dot_insn ()
8499 && r->reg_type.bitfield.instance == Accum
8500 && i.op[i.imm_operands + 1].regs)
8501 r = i.op[i.imm_operands + 1].regs;
85b80b0f 8502 /* Register goes in low 3 bits of opcode. */
4943d587 8503 i.tm.base_opcode |= r->reg_num;
0a3eba42 8504 set_rex_vrex (r, REX_B, false);
0ff3b7d0
JB
8505
8506 if (dot_insn () && i.reg_operands == 2)
8507 {
8508 gas_assert (is_any_vex_encoding (&i.tm)
8509 || i.vec_encoding != vex_encoding_default);
8510 i.vex.register_specifier = i.op[i.operands - 1].regs;
8511 }
8512 }
8513 else if (i.reg_operands == 1
8514 && !i.flags[i.operands - 1]
8515 && i.tm.operand_types[i.operands - 1].bitfield.instance
8516 == InstanceNone)
8517 {
8518 gas_assert (is_any_vex_encoding (&i.tm)
8519 || i.vec_encoding != vex_encoding_default);
8520 i.vex.register_specifier = i.op[i.operands - 1].regs;
85b80b0f 8521 }
29b0f896 8522
514a8bb0 8523 if ((i.seg[0] || i.prefix[SEG_PREFIX])
7fc69528 8524 && i.tm.mnem_off == MN_lea)
92334ad2
JB
8525 {
8526 if (!quiet_warnings)
76d3f746 8527 as_warn (_("segment override on `%s' is ineffectual"), insn_name (&i.tm));
739d7649 8528 if (optimize && !i.no_optimize)
92334ad2
JB
8529 {
8530 i.seg[0] = NULL;
8531 i.prefix[SEG_PREFIX] = 0;
8532 }
8533 }
52271982
AM
8534
8535 /* If a segment was explicitly specified, and the specified segment
b6773884
JB
8536 is neither the default nor the one already recorded from a prefix,
8537 use an opcode prefix to select it. If we never figured out what
8538 the default segment is, then default_seg will be zero at this
8539 point, and the specified segment prefix will always be used. */
8540 if (i.seg[0]
8541 && i.seg[0] != default_seg
5e042380 8542 && i386_seg_prefixes[i.seg[0]->reg_num] != i.prefix[SEG_PREFIX])
29b0f896 8543 {
5e042380 8544 if (!add_prefix (i386_seg_prefixes[i.seg[0]->reg_num]))
29b0f896
AM
8545 return 0;
8546 }
8547 return 1;
8548}
8549
5e042380 8550static const reg_entry *
e3bb37b5 8551build_modrm_byte (void)
29b0f896 8552{
5e042380 8553 const reg_entry *default_seg = NULL;
ecb96e55
JB
8554 unsigned int source = i.imm_operands - i.tm.opcode_modifier.immext
8555 /* Compensate for kludge in md_assemble(). */
8556 + i.tm.operand_types[0].bitfield.imm1;
8557 unsigned int dest = i.operands - 1 - i.tm.opcode_modifier.immext;
8558 unsigned int v, op, reg_slot = ~0;
8559
8560 /* Accumulator (in particular %st), shift count (%cl), and alike need
8561 to be skipped just like immediate operands do. */
8562 if (i.tm.operand_types[source].bitfield.instance)
8563 ++source;
8564 while (i.tm.operand_types[dest].bitfield.instance)
8565 --dest;
8566
8567 for (op = source; op < i.operands; ++op)
8568 if (i.tm.operand_types[op].bitfield.baseindex)
8569 break;
c0f3af97 8570
ecb96e55 8571 if (i.reg_operands + i.mem_operands + (i.tm.extension_opcode != None) == 4)
c0f3af97 8572 {
4c2c6516 8573 expressionS *exp;
c0f3af97 8574
a683cc34 8575 /* There are 2 kinds of instructions:
bed3d976 8576 1. 5 operands: 4 register operands or 3 register operands
9d3bf266 8577 plus 1 memory operand plus one Imm4 operand, VexXDS, and
bed3d976 8578 VexW0 or VexW1. The destination must be either XMM, YMM or
43234a1e 8579 ZMM register.
bed3d976 8580 2. 4 operands: 4 register operands or 3 register operands
0ff3b7d0
JB
8581 plus 1 memory operand, with VexXDS.
8582 3. Other equivalent combinations when coming from s_insn(). */
eea96d3f 8583 gas_assert (i.tm.opcode_modifier.vexvvvv
0ff3b7d0
JB
8584 && i.tm.opcode_modifier.vexw);
8585 gas_assert (dot_insn ()
8586 || i.tm.operand_types[dest].bitfield.class == RegSIMD);
a683cc34 8587
ecb96e55
JB
8588 /* Of the first two non-immediate operands the one with the template
8589 not allowing for a memory one is encoded in the immediate operand. */
8590 if (source == op)
8591 reg_slot = source + 1;
48db9223 8592 else
ecb96e55 8593 reg_slot = source++;
48db9223 8594
0ff3b7d0
JB
8595 if (!dot_insn ())
8596 {
8597 gas_assert (i.tm.operand_types[reg_slot].bitfield.class == RegSIMD);
8598 gas_assert (!(i.op[reg_slot].regs->reg_flags & RegVRex));
8599 }
8600 else
8601 gas_assert (i.tm.operand_types[reg_slot].bitfield.class != ClassNone);
8602
a683cc34 8603 if (i.imm_operands == 0)
bed3d976
JB
8604 {
8605 /* When there is no immediate operand, generate an 8bit
8606 immediate operand to encode the first operand. */
8607 exp = &im_expressions[i.imm_operands++];
8608 i.op[i.operands].imms = exp;
be1643ff 8609 i.types[i.operands].bitfield.imm8 = 1;
bed3d976
JB
8610 i.operands++;
8611
bed3d976 8612 exp->X_op = O_constant;
43234a1e 8613 }
922d8de8 8614 else
bed3d976 8615 {
9d3bf266
JB
8616 gas_assert (i.imm_operands == 1);
8617 gas_assert (fits_in_imm4 (i.op[0].imms->X_add_number));
8618 gas_assert (!i.tm.opcode_modifier.immext);
a683cc34 8619
9d3bf266
JB
8620 /* Turn on Imm8 again so that output_imm will generate it. */
8621 i.types[0].bitfield.imm8 = 1;
bed3d976 8622
0ff3b7d0 8623 exp = i.op[0].imms;
bed3d976 8624 }
0ff3b7d0
JB
8625 exp->X_add_number |= register_number (i.op[reg_slot].regs)
8626 << (3 + !(is_evex_encoding (&i.tm)
8627 || i.vec_encoding == vex_encoding_evex));
c0f3af97 8628 }
f12dc422 8629
ecb96e55
JB
8630 for (v = source + 1; v < dest; ++v)
8631 if (v != reg_slot)
8632 break;
8633 if (v >= dest)
8634 v = ~0;
8635 if (i.tm.extension_opcode != None)
8636 {
8637 if (dest != source)
8638 v = dest;
8639 dest = ~0;
8640 }
8641 gas_assert (source < dest);
8642 if (i.tm.opcode_modifier.operandconstraint == SWAP_SOURCES
8643 && source != op)
8644 {
8645 unsigned int tmp = source;
f12dc422 8646
ecb96e55
JB
8647 source = v;
8648 v = tmp;
8649 }
29b0f896 8650
ecb96e55
JB
8651 if (v < MAX_OPERANDS)
8652 {
8653 gas_assert (i.tm.opcode_modifier.vexvvvv);
8654 i.vex.register_specifier = i.op[v].regs;
29b0f896 8655 }
c0f3af97 8656
ecb96e55
JB
8657 if (op < i.operands)
8658 {
29b0f896
AM
8659 if (i.mem_operands)
8660 {
8661 unsigned int fake_zero_displacement = 0;
4eed87de 8662
ecb96e55 8663 gas_assert (i.flags[op] & Operand_Mem);
29b0f896 8664
63112cd6 8665 if (i.tm.opcode_modifier.sib)
6c30d220 8666 {
260cd341
LC
8667 /* The index register of VSIB shouldn't be RegIZ. */
8668 if (i.tm.opcode_modifier.sib != SIBMEM
8669 && i.index_reg->reg_num == RegIZ)
6c30d220
L
8670 abort ();
8671
8672 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8673 if (!i.base_reg)
8674 {
8675 i.sib.base = NO_BASE_REGISTER;
8676 i.sib.scale = i.log2_scale_factor;
2f2be86b 8677 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8678 i.types[op].bitfield.disp32 = 1;
6c30d220 8679 }
260cd341
LC
8680
8681 /* Since the mandatory SIB always has index register, so
8682 the code logic remains unchanged. The non-mandatory SIB
8683 without index register is allowed and will be handled
8684 later. */
8685 if (i.index_reg)
8686 {
8687 if (i.index_reg->reg_num == RegIZ)
8688 i.sib.index = NO_INDEX_REGISTER;
8689 else
8690 i.sib.index = i.index_reg->reg_num;
5b7c81bd 8691 set_rex_vrex (i.index_reg, REX_X, false);
260cd341 8692 }
6c30d220
L
8693 }
8694
5e042380 8695 default_seg = reg_ds;
29b0f896
AM
8696
8697 if (i.base_reg == 0)
8698 {
8699 i.rm.mode = 0;
8700 if (!i.disp_operands)
9bb129e8 8701 fake_zero_displacement = 1;
29b0f896
AM
8702 if (i.index_reg == 0)
8703 {
260cd341
LC
8704 /* Both check for VSIB and mandatory non-vector SIB. */
8705 gas_assert (!i.tm.opcode_modifier.sib
8706 || i.tm.opcode_modifier.sib == SIBMEM);
29b0f896 8707 /* Operand is just <disp> */
2f2be86b 8708 i.types[op] = operand_type_and_not (i.types[op], anydisp);
20f0a1fc 8709 if (flag_code == CODE_64BIT)
29b0f896
AM
8710 {
8711 /* 64bit mode overwrites the 32bit absolute
8712 addressing by RIP relative addressing and
8713 absolute addressing is encoded by one of the
8714 redundant SIB forms. */
8715 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8716 i.sib.base = NO_BASE_REGISTER;
8717 i.sib.index = NO_INDEX_REGISTER;
a775efc8 8718 i.types[op].bitfield.disp32 = 1;
20f0a1fc 8719 }
fc225355
L
8720 else if ((flag_code == CODE_16BIT)
8721 ^ (i.prefix[ADDR_PREFIX] != 0))
20f0a1fc
NC
8722 {
8723 i.rm.regmem = NO_BASE_REGISTER_16;
2f2be86b 8724 i.types[op].bitfield.disp16 = 1;
20f0a1fc
NC
8725 }
8726 else
8727 {
8728 i.rm.regmem = NO_BASE_REGISTER;
2f2be86b 8729 i.types[op].bitfield.disp32 = 1;
29b0f896
AM
8730 }
8731 }
63112cd6 8732 else if (!i.tm.opcode_modifier.sib)
29b0f896 8733 {
6c30d220 8734 /* !i.base_reg && i.index_reg */
e968fc9b 8735 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8736 i.sib.index = NO_INDEX_REGISTER;
8737 else
8738 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8739 i.sib.base = NO_BASE_REGISTER;
8740 i.sib.scale = i.log2_scale_factor;
8741 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
2f2be86b 8742 i.types[op] = operand_type_and_not (i.types[op], anydisp);
a775efc8 8743 i.types[op].bitfield.disp32 = 1;
29b0f896 8744 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8745 i.rex |= REX_X;
29b0f896
AM
8746 }
8747 }
8748 /* RIP addressing for 64bit mode. */
e968fc9b 8749 else if (i.base_reg->reg_num == RegIP)
29b0f896 8750 {
63112cd6 8751 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896 8752 i.rm.regmem = NO_BASE_REGISTER;
40fb9820
L
8753 i.types[op].bitfield.disp8 = 0;
8754 i.types[op].bitfield.disp16 = 0;
a775efc8 8755 i.types[op].bitfield.disp32 = 1;
40fb9820 8756 i.types[op].bitfield.disp64 = 0;
71903a11 8757 i.flags[op] |= Operand_PCrel;
20f0a1fc
NC
8758 if (! i.disp_operands)
8759 fake_zero_displacement = 1;
29b0f896 8760 }
dc821c5f 8761 else if (i.base_reg->reg_type.bitfield.word)
29b0f896 8762 {
63112cd6 8763 gas_assert (!i.tm.opcode_modifier.sib);
29b0f896
AM
8764 switch (i.base_reg->reg_num)
8765 {
8766 case 3: /* (%bx) */
8767 if (i.index_reg == 0)
8768 i.rm.regmem = 7;
8769 else /* (%bx,%si) -> 0, or (%bx,%di) -> 1 */
8770 i.rm.regmem = i.index_reg->reg_num - 6;
8771 break;
8772 case 5: /* (%bp) */
5e042380 8773 default_seg = reg_ss;
29b0f896
AM
8774 if (i.index_reg == 0)
8775 {
8776 i.rm.regmem = 6;
40fb9820 8777 if (operand_type_check (i.types[op], disp) == 0)
29b0f896
AM
8778 {
8779 /* fake (%bp) into 0(%bp) */
41eb8e88 8780 if (i.disp_encoding == disp_encoding_16bit)
1a02d6b0
L
8781 i.types[op].bitfield.disp16 = 1;
8782 else
8783 i.types[op].bitfield.disp8 = 1;
252b5132 8784 fake_zero_displacement = 1;
29b0f896
AM
8785 }
8786 }
8787 else /* (%bp,%si) -> 2, or (%bp,%di) -> 3 */
8788 i.rm.regmem = i.index_reg->reg_num - 6 + 2;
8789 break;
8790 default: /* (%si) -> 4 or (%di) -> 5 */
8791 i.rm.regmem = i.base_reg->reg_num - 6 + 4;
8792 }
41eb8e88
L
8793 if (!fake_zero_displacement
8794 && !i.disp_operands
8795 && i.disp_encoding)
8796 {
8797 fake_zero_displacement = 1;
8798 if (i.disp_encoding == disp_encoding_8bit)
8799 i.types[op].bitfield.disp8 = 1;
8800 else
8801 i.types[op].bitfield.disp16 = 1;
8802 }
29b0f896
AM
8803 i.rm.mode = mode_from_disp_size (i.types[op]);
8804 }
8805 else /* i.base_reg and 32/64 bit mode */
8806 {
a9aabc23 8807 if (operand_type_check (i.types[op], disp))
40fb9820 8808 {
73053c1f
JB
8809 i.types[op].bitfield.disp16 = 0;
8810 i.types[op].bitfield.disp64 = 0;
a775efc8 8811 i.types[op].bitfield.disp32 = 1;
40fb9820 8812 }
20f0a1fc 8813
63112cd6 8814 if (!i.tm.opcode_modifier.sib)
6c30d220 8815 i.rm.regmem = i.base_reg->reg_num;
29b0f896 8816 if ((i.base_reg->reg_flags & RegRex) != 0)
161a04f6 8817 i.rex |= REX_B;
29b0f896
AM
8818 i.sib.base = i.base_reg->reg_num;
8819 /* x86-64 ignores REX prefix bit here to avoid decoder
8820 complications. */
848930b2
JB
8821 if (!(i.base_reg->reg_flags & RegRex)
8822 && (i.base_reg->reg_num == EBP_REG_NUM
8823 || i.base_reg->reg_num == ESP_REG_NUM))
5e042380 8824 default_seg = reg_ss;
848930b2 8825 if (i.base_reg->reg_num == 5 && i.disp_operands == 0)
29b0f896 8826 {
848930b2 8827 fake_zero_displacement = 1;
1a02d6b0
L
8828 if (i.disp_encoding == disp_encoding_32bit)
8829 i.types[op].bitfield.disp32 = 1;
8830 else
8831 i.types[op].bitfield.disp8 = 1;
29b0f896
AM
8832 }
8833 i.sib.scale = i.log2_scale_factor;
8834 if (i.index_reg == 0)
8835 {
260cd341
LC
8836 /* Only check for VSIB. */
8837 gas_assert (i.tm.opcode_modifier.sib != VECSIB128
8838 && i.tm.opcode_modifier.sib != VECSIB256
8839 && i.tm.opcode_modifier.sib != VECSIB512);
8840
29b0f896
AM
8841 /* <disp>(%esp) becomes two byte modrm with no index
8842 register. We've already stored the code for esp
8843 in i.rm.regmem ie. ESCAPE_TO_TWO_BYTE_ADDRESSING.
8844 Any base register besides %esp will not use the
8845 extra modrm byte. */
8846 i.sib.index = NO_INDEX_REGISTER;
29b0f896 8847 }
63112cd6 8848 else if (!i.tm.opcode_modifier.sib)
29b0f896 8849 {
e968fc9b 8850 if (i.index_reg->reg_num == RegIZ)
db51cc60
L
8851 i.sib.index = NO_INDEX_REGISTER;
8852 else
8853 i.sib.index = i.index_reg->reg_num;
29b0f896
AM
8854 i.rm.regmem = ESCAPE_TO_TWO_BYTE_ADDRESSING;
8855 if ((i.index_reg->reg_flags & RegRex) != 0)
161a04f6 8856 i.rex |= REX_X;
29b0f896 8857 }
67a4f2b7
AO
8858
8859 if (i.disp_operands
8860 && (i.reloc[op] == BFD_RELOC_386_TLS_DESC_CALL
8861 || i.reloc[op] == BFD_RELOC_X86_64_TLSDESC_CALL))
8862 i.rm.mode = 0;
8863 else
a501d77e
L
8864 {
8865 if (!fake_zero_displacement
8866 && !i.disp_operands
8867 && i.disp_encoding)
8868 {
8869 fake_zero_displacement = 1;
8870 if (i.disp_encoding == disp_encoding_8bit)
8871 i.types[op].bitfield.disp8 = 1;
8872 else
8873 i.types[op].bitfield.disp32 = 1;
8874 }
8875 i.rm.mode = mode_from_disp_size (i.types[op]);
8876 }
29b0f896 8877 }
252b5132 8878
29b0f896
AM
8879 if (fake_zero_displacement)
8880 {
8881 /* Fakes a zero displacement assuming that i.types[op]
8882 holds the correct displacement size. */
8883 expressionS *exp;
8884
9c2799c2 8885 gas_assert (i.op[op].disps == 0);
29b0f896
AM
8886 exp = &disp_expressions[i.disp_operands++];
8887 i.op[op].disps = exp;
8888 exp->X_op = O_constant;
8889 exp->X_add_number = 0;
8890 exp->X_add_symbol = (symbolS *) 0;
8891 exp->X_op_symbol = (symbolS *) 0;
8892 }
8893 }
ecb96e55
JB
8894 else
8895 {
8896 i.rm.mode = 3;
8897 i.rm.regmem = i.op[op].regs->reg_num;
8898 set_rex_vrex (i.op[op].regs, REX_B, false);
8899 }
252b5132 8900
ecb96e55
JB
8901 if (op == dest)
8902 dest = ~0;
8903 if (op == source)
8904 source = ~0;
8905 }
8906 else
8907 {
8908 i.rm.mode = 3;
8909 if (!i.tm.opcode_modifier.regmem)
f88c9eb0 8910 {
ecb96e55
JB
8911 gas_assert (source < MAX_OPERANDS);
8912 i.rm.regmem = i.op[source].regs->reg_num;
8913 set_rex_vrex (i.op[source].regs, REX_B,
8914 dest >= MAX_OPERANDS && i.tm.opcode_modifier.sse2avx);
8915 source = ~0;
f88c9eb0 8916 }
ecb96e55 8917 else
29b0f896 8918 {
ecb96e55
JB
8919 gas_assert (dest < MAX_OPERANDS);
8920 i.rm.regmem = i.op[dest].regs->reg_num;
8921 set_rex_vrex (i.op[dest].regs, REX_B, i.tm.opcode_modifier.sse2avx);
8922 dest = ~0;
29b0f896 8923 }
ecb96e55 8924 }
252b5132 8925
ecb96e55
JB
8926 /* Fill in i.rm.reg field with extension opcode (if any) or the
8927 appropriate register. */
8928 if (i.tm.extension_opcode != None)
8929 i.rm.reg = i.tm.extension_opcode;
8930 else if (!i.tm.opcode_modifier.regmem && dest < MAX_OPERANDS)
8931 {
8932 i.rm.reg = i.op[dest].regs->reg_num;
8933 set_rex_vrex (i.op[dest].regs, REX_R, i.tm.opcode_modifier.sse2avx);
8934 }
8935 else
8936 {
8937 gas_assert (source < MAX_OPERANDS);
8938 i.rm.reg = i.op[source].regs->reg_num;
8939 set_rex_vrex (i.op[source].regs, REX_R, false);
29b0f896 8940 }
ecb96e55
JB
8941
8942 if (flag_code != CODE_64BIT && (i.rex & REX_R))
8943 {
8944 gas_assert (i.types[!i.tm.opcode_modifier.regmem].bitfield.class == RegCR);
8945 i.rex &= ~REX_R;
8946 add_prefix (LOCK_PREFIX_OPCODE);
8947 }
8948
29b0f896
AM
8949 return default_seg;
8950}
252b5132 8951
48ef937e
JB
8952static INLINE void
8953frag_opcode_byte (unsigned char byte)
8954{
8955 if (now_seg != absolute_section)
8956 FRAG_APPEND_1_CHAR (byte);
8957 else
8958 ++abs_section_offset;
8959}
8960
376cd056
JB
8961static unsigned int
8962flip_code16 (unsigned int code16)
8963{
8964 gas_assert (i.tm.operands == 1);
8965
8966 return !(i.prefix[REX_PREFIX] & REX_W)
8967 && (code16 ? i.tm.operand_types[0].bitfield.disp32
376cd056
JB
8968 : i.tm.operand_types[0].bitfield.disp16)
8969 ? CODE16 : 0;
8970}
8971
29b0f896 8972static void
e3bb37b5 8973output_branch (void)
29b0f896
AM
8974{
8975 char *p;
f8a5c266 8976 int size;
29b0f896
AM
8977 int code16;
8978 int prefix;
8979 relax_substateT subtype;
8980 symbolS *sym;
8981 offsetT off;
8982
48ef937e
JB
8983 if (now_seg == absolute_section)
8984 {
8985 as_bad (_("relaxable branches not supported in absolute section"));
8986 return;
8987 }
8988
f8a5c266 8989 code16 = flag_code == CODE_16BIT ? CODE16 : 0;
1a42a9fe 8990 size = i.disp_encoding > disp_encoding_8bit ? BIG : SMALL;
29b0f896
AM
8991
8992 prefix = 0;
8993 if (i.prefix[DATA_PREFIX] != 0)
252b5132 8994 {
29b0f896
AM
8995 prefix = 1;
8996 i.prefixes -= 1;
376cd056 8997 code16 ^= flip_code16(code16);
252b5132 8998 }
29b0f896
AM
8999 /* Pentium4 branch hints. */
9000 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
9001 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
2f66722d 9002 {
29b0f896
AM
9003 prefix++;
9004 i.prefixes--;
9005 }
9006 if (i.prefix[REX_PREFIX] != 0)
9007 {
9008 prefix++;
9009 i.prefixes--;
2f66722d
AM
9010 }
9011
7e8b059b
L
9012 /* BND prefixed jump. */
9013 if (i.prefix[BND_PREFIX] != 0)
9014 {
6cb0a70e
JB
9015 prefix++;
9016 i.prefixes--;
7e8b059b
L
9017 }
9018
f2810fe0 9019 if (i.prefixes != 0)
76d3f746 9020 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
29b0f896
AM
9021
9022 /* It's always a symbol; End frag & setup for relax.
9023 Make sure there is enough room in this frag for the largest
9024 instruction we may generate in md_convert_frag. This is 2
9025 bytes for the opcode and room for the prefix and largest
9026 displacement. */
9027 frag_grow (prefix + 2 + 4);
9028 /* Prefix and 1 opcode byte go in fr_fix. */
9029 p = frag_more (prefix + 1);
9030 if (i.prefix[DATA_PREFIX] != 0)
9031 *p++ = DATA_PREFIX_OPCODE;
9032 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE
9033 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE)
9034 *p++ = i.prefix[SEG_PREFIX];
6cb0a70e
JB
9035 if (i.prefix[BND_PREFIX] != 0)
9036 *p++ = BND_PREFIX_OPCODE;
29b0f896
AM
9037 if (i.prefix[REX_PREFIX] != 0)
9038 *p++ = i.prefix[REX_PREFIX];
9039 *p = i.tm.base_opcode;
9040
9041 if ((unsigned char) *p == JUMP_PC_RELATIVE)
f8a5c266 9042 subtype = ENCODE_RELAX_STATE (UNCOND_JUMP, size);
40fb9820 9043 else if (cpu_arch_flags.bitfield.cpui386)
f8a5c266 9044 subtype = ENCODE_RELAX_STATE (COND_JUMP, size);
29b0f896 9045 else
f8a5c266 9046 subtype = ENCODE_RELAX_STATE (COND_JUMP86, size);
29b0f896 9047 subtype |= code16;
3e73aa7c 9048
29b0f896
AM
9049 sym = i.op[0].disps->X_add_symbol;
9050 off = i.op[0].disps->X_add_number;
3e73aa7c 9051
29b0f896
AM
9052 if (i.op[0].disps->X_op != O_constant
9053 && i.op[0].disps->X_op != O_symbol)
3e73aa7c 9054 {
29b0f896
AM
9055 /* Handle complex expressions. */
9056 sym = make_expr_symbol (i.op[0].disps);
9057 off = 0;
9058 }
3e73aa7c 9059
1ef3994a
JB
9060 frag_now->tc_frag_data.code64 = flag_code == CODE_64BIT;
9061
29b0f896
AM
9062 /* 1 possible extra opcode + 4 byte displacement go in var part.
9063 Pass reloc in fr_var. */
d258b828 9064 frag_var (rs_machine_dependent, 5, i.reloc[0], subtype, sym, off, p);
29b0f896 9065}
3e73aa7c 9066
bd7ab16b
L
9067#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9068/* Return TRUE iff PLT32 relocation should be used for branching to
9069 symbol S. */
9070
5b7c81bd 9071static bool
bd7ab16b
L
9072need_plt32_p (symbolS *s)
9073{
9074 /* PLT32 relocation is ELF only. */
9075 if (!IS_ELF)
5b7c81bd 9076 return false;
bd7ab16b 9077
a5def729
RO
9078#ifdef TE_SOLARIS
9079 /* Don't emit PLT32 relocation on Solaris: neither native linker nor
9080 krtld support it. */
5b7c81bd 9081 return false;
a5def729
RO
9082#endif
9083
bd7ab16b
L
9084 /* Since there is no need to prepare for PLT branch on x86-64, we
9085 can generate R_X86_64_PLT32, instead of R_X86_64_PC32, which can
9086 be used as a marker for 32-bit PC-relative branches. */
9087 if (!object_64bit)
5b7c81bd 9088 return false;
bd7ab16b 9089
44365e88 9090 if (s == NULL)
5b7c81bd 9091 return false;
44365e88 9092
bd7ab16b
L
9093 /* Weak or undefined symbol need PLT32 relocation. */
9094 if (S_IS_WEAK (s) || !S_IS_DEFINED (s))
5b7c81bd 9095 return true;
bd7ab16b
L
9096
9097 /* Non-global symbol doesn't need PLT32 relocation. */
9098 if (! S_IS_EXTERNAL (s))
5b7c81bd 9099 return false;
bd7ab16b
L
9100
9101 /* Other global symbols need PLT32 relocation. NB: Symbol with
9102 non-default visibilities are treated as normal global symbol
9103 so that PLT32 relocation can be used as a marker for 32-bit
9104 PC-relative branches. It is useful for linker relaxation. */
5b7c81bd 9105 return true;
bd7ab16b
L
9106}
9107#endif
9108
29b0f896 9109static void
e3bb37b5 9110output_jump (void)
29b0f896
AM
9111{
9112 char *p;
9113 int size;
3e02c1cc 9114 fixS *fixP;
bd7ab16b 9115 bfd_reloc_code_real_type jump_reloc = i.reloc[0];
29b0f896 9116
0cfa3eb3 9117 if (i.tm.opcode_modifier.jump == JUMP_BYTE)
29b0f896
AM
9118 {
9119 /* This is a loop or jecxz type instruction. */
9120 size = 1;
9121 if (i.prefix[ADDR_PREFIX] != 0)
9122 {
48ef937e 9123 frag_opcode_byte (ADDR_PREFIX_OPCODE);
29b0f896
AM
9124 i.prefixes -= 1;
9125 }
9126 /* Pentium4 branch hints. */
9127 if (i.prefix[SEG_PREFIX] == CS_PREFIX_OPCODE /* not taken */
9128 || i.prefix[SEG_PREFIX] == DS_PREFIX_OPCODE /* taken */)
9129 {
48ef937e 9130 frag_opcode_byte (i.prefix[SEG_PREFIX]);
29b0f896 9131 i.prefixes--;
3e73aa7c
JH
9132 }
9133 }
29b0f896
AM
9134 else
9135 {
9136 int code16;
3e73aa7c 9137
29b0f896
AM
9138 code16 = 0;
9139 if (flag_code == CODE_16BIT)
9140 code16 = CODE16;
3e73aa7c 9141
29b0f896
AM
9142 if (i.prefix[DATA_PREFIX] != 0)
9143 {
48ef937e 9144 frag_opcode_byte (DATA_PREFIX_OPCODE);
29b0f896 9145 i.prefixes -= 1;
376cd056 9146 code16 ^= flip_code16(code16);
29b0f896 9147 }
252b5132 9148
29b0f896
AM
9149 size = 4;
9150 if (code16)
9151 size = 2;
9152 }
9fcc94b6 9153
6cb0a70e
JB
9154 /* BND prefixed jump. */
9155 if (i.prefix[BND_PREFIX] != 0)
29b0f896 9156 {
48ef937e 9157 frag_opcode_byte (i.prefix[BND_PREFIX]);
29b0f896
AM
9158 i.prefixes -= 1;
9159 }
252b5132 9160
6cb0a70e 9161 if (i.prefix[REX_PREFIX] != 0)
7e8b059b 9162 {
48ef937e 9163 frag_opcode_byte (i.prefix[REX_PREFIX]);
7e8b059b
L
9164 i.prefixes -= 1;
9165 }
9166
f2810fe0 9167 if (i.prefixes != 0)
76d3f746 9168 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
e0890092 9169
48ef937e
JB
9170 if (now_seg == absolute_section)
9171 {
9a182d04 9172 abs_section_offset += i.opcode_length + size;
48ef937e
JB
9173 return;
9174 }
9175
9a182d04
JB
9176 p = frag_more (i.opcode_length + size);
9177 switch (i.opcode_length)
42164a71
L
9178 {
9179 case 2:
9180 *p++ = i.tm.base_opcode >> 8;
1a0670f3 9181 /* Fall through. */
42164a71
L
9182 case 1:
9183 *p++ = i.tm.base_opcode;
9184 break;
9185 default:
9186 abort ();
9187 }
e0890092 9188
bd7ab16b 9189#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
9190 if (flag_code == CODE_64BIT && size == 4
9191 && jump_reloc == NO_RELOC && i.op[0].disps->X_add_number == 0
bd7ab16b
L
9192 && need_plt32_p (i.op[0].disps->X_add_symbol))
9193 jump_reloc = BFD_RELOC_X86_64_PLT32;
9194#endif
9195
9196 jump_reloc = reloc (size, 1, 1, jump_reloc);
9197
3e02c1cc 9198 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal, size,
bd7ab16b 9199 i.op[0].disps, 1, jump_reloc);
3e02c1cc 9200
eb19308f
JB
9201 /* All jumps handled here are signed, but don't unconditionally use a
9202 signed limit check for 32 and 16 bit jumps as we want to allow wrap
9203 around at 4G (outside of 64-bit mode) and 64k (except for XBEGIN)
9204 respectively. */
9205 switch (size)
9206 {
9207 case 1:
9208 fixP->fx_signed = 1;
9209 break;
9210
9211 case 2:
7fc69528 9212 if (i.tm.mnem_off == MN_xbegin)
eb19308f
JB
9213 fixP->fx_signed = 1;
9214 break;
9215
9216 case 4:
9217 if (flag_code == CODE_64BIT)
9218 fixP->fx_signed = 1;
9219 break;
9220 }
29b0f896 9221}
e0890092 9222
29b0f896 9223static void
e3bb37b5 9224output_interseg_jump (void)
29b0f896
AM
9225{
9226 char *p;
9227 int size;
9228 int prefix;
9229 int code16;
252b5132 9230
29b0f896
AM
9231 code16 = 0;
9232 if (flag_code == CODE_16BIT)
9233 code16 = CODE16;
a217f122 9234
29b0f896
AM
9235 prefix = 0;
9236 if (i.prefix[DATA_PREFIX] != 0)
9237 {
9238 prefix = 1;
9239 i.prefixes -= 1;
9240 code16 ^= CODE16;
9241 }
6cb0a70e
JB
9242
9243 gas_assert (!i.prefix[REX_PREFIX]);
252b5132 9244
29b0f896
AM
9245 size = 4;
9246 if (code16)
9247 size = 2;
252b5132 9248
f2810fe0 9249 if (i.prefixes != 0)
76d3f746 9250 as_warn (_("skipping prefixes on `%s'"), insn_name (&i.tm));
252b5132 9251
48ef937e
JB
9252 if (now_seg == absolute_section)
9253 {
9254 abs_section_offset += prefix + 1 + 2 + size;
9255 return;
9256 }
9257
29b0f896
AM
9258 /* 1 opcode; 2 segment; offset */
9259 p = frag_more (prefix + 1 + 2 + size);
3e73aa7c 9260
29b0f896
AM
9261 if (i.prefix[DATA_PREFIX] != 0)
9262 *p++ = DATA_PREFIX_OPCODE;
252b5132 9263
29b0f896
AM
9264 if (i.prefix[REX_PREFIX] != 0)
9265 *p++ = i.prefix[REX_PREFIX];
252b5132 9266
29b0f896
AM
9267 *p++ = i.tm.base_opcode;
9268 if (i.op[1].imms->X_op == O_constant)
9269 {
9270 offsetT n = i.op[1].imms->X_add_number;
252b5132 9271
29b0f896
AM
9272 if (size == 2
9273 && !fits_in_unsigned_word (n)
9274 && !fits_in_signed_word (n))
9275 {
9276 as_bad (_("16-bit jump out of range"));
9277 return;
9278 }
9279 md_number_to_chars (p, n, size);
9280 }
9281 else
9282 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
d258b828 9283 i.op[1].imms, 0, reloc (size, 0, 0, i.reloc[1]));
6d96a594
C
9284
9285 p += size;
9286 if (i.op[0].imms->X_op == O_constant)
9287 md_number_to_chars (p, (valueT) i.op[0].imms->X_add_number, 2);
9288 else
9289 fix_new_exp (frag_now, p - frag_now->fr_literal, 2,
9290 i.op[0].imms, 0, reloc (2, 0, 0, i.reloc[0]));
29b0f896 9291}
a217f122 9292
b4a3a7b4
L
9293#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
9294void
9295x86_cleanup (void)
9296{
9297 char *p;
9298 asection *seg = now_seg;
9299 subsegT subseg = now_subseg;
9300 asection *sec;
9301 unsigned int alignment, align_size_1;
9302 unsigned int isa_1_descsz, feature_2_descsz, descsz;
9303 unsigned int isa_1_descsz_raw, feature_2_descsz_raw;
9304 unsigned int padding;
9305
1273b2f8 9306 if (!IS_ELF || !x86_used_note)
b4a3a7b4
L
9307 return;
9308
b4a3a7b4
L
9309 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X86;
9310
9311 /* The .note.gnu.property section layout:
9312
9313 Field Length Contents
9314 ---- ---- ----
9315 n_namsz 4 4
9316 n_descsz 4 The note descriptor size
9317 n_type 4 NT_GNU_PROPERTY_TYPE_0
9318 n_name 4 "GNU"
9319 n_desc n_descsz The program property array
9320 .... .... ....
9321 */
9322
9323 /* Create the .note.gnu.property section. */
9324 sec = subseg_new (NOTE_GNU_PROPERTY_SECTION_NAME, 0);
fd361982 9325 bfd_set_section_flags (sec,
b4a3a7b4
L
9326 (SEC_ALLOC
9327 | SEC_LOAD
9328 | SEC_DATA
9329 | SEC_HAS_CONTENTS
9330 | SEC_READONLY));
9331
9332 if (get_elf_backend_data (stdoutput)->s->elfclass == ELFCLASS64)
9333 {
9334 align_size_1 = 7;
9335 alignment = 3;
9336 }
9337 else
9338 {
9339 align_size_1 = 3;
9340 alignment = 2;
9341 }
9342
fd361982 9343 bfd_set_section_alignment (sec, alignment);
b4a3a7b4
L
9344 elf_section_type (sec) = SHT_NOTE;
9345
1273b2f8
L
9346 /* GNU_PROPERTY_X86_ISA_1_USED: 4-byte type + 4-byte data size
9347 + 4-byte data */
9348 isa_1_descsz_raw = 4 + 4 + 4;
9349 /* Align GNU_PROPERTY_X86_ISA_1_USED. */
9350 isa_1_descsz = (isa_1_descsz_raw + align_size_1) & ~align_size_1;
b4a3a7b4
L
9351
9352 feature_2_descsz_raw = isa_1_descsz;
9353 /* GNU_PROPERTY_X86_FEATURE_2_USED: 4-byte type + 4-byte data size
1273b2f8 9354 + 4-byte data */
b4a3a7b4
L
9355 feature_2_descsz_raw += 4 + 4 + 4;
9356 /* Align GNU_PROPERTY_X86_FEATURE_2_USED. */
9357 feature_2_descsz = ((feature_2_descsz_raw + align_size_1)
9358 & ~align_size_1);
9359
9360 descsz = feature_2_descsz;
9361 /* Section size: n_namsz + n_descsz + n_type + n_name + n_descsz. */
9362 p = frag_more (4 + 4 + 4 + 4 + descsz);
9363
9364 /* Write n_namsz. */
9365 md_number_to_chars (p, (valueT) 4, 4);
9366
9367 /* Write n_descsz. */
9368 md_number_to_chars (p + 4, (valueT) descsz, 4);
9369
9370 /* Write n_type. */
9371 md_number_to_chars (p + 4 * 2, (valueT) NT_GNU_PROPERTY_TYPE_0, 4);
9372
9373 /* Write n_name. */
9374 memcpy (p + 4 * 3, "GNU", 4);
9375
1273b2f8
L
9376 /* Write 4-byte type. */
9377 md_number_to_chars (p + 4 * 4,
9378 (valueT) GNU_PROPERTY_X86_ISA_1_USED, 4);
b4a3a7b4 9379
1273b2f8
L
9380 /* Write 4-byte data size. */
9381 md_number_to_chars (p + 4 * 5, (valueT) 4, 4);
b4a3a7b4 9382
1273b2f8
L
9383 /* Write 4-byte data. */
9384 md_number_to_chars (p + 4 * 6, (valueT) x86_isa_1_used, 4);
b4a3a7b4 9385
1273b2f8
L
9386 /* Zero out paddings. */
9387 padding = isa_1_descsz - isa_1_descsz_raw;
9388 if (padding)
9389 memset (p + 4 * 7, 0, padding);
b4a3a7b4
L
9390
9391 /* Write 4-byte type. */
9392 md_number_to_chars (p + isa_1_descsz + 4 * 4,
9393 (valueT) GNU_PROPERTY_X86_FEATURE_2_USED, 4);
9394
9395 /* Write 4-byte data size. */
9396 md_number_to_chars (p + isa_1_descsz + 4 * 5, (valueT) 4, 4);
9397
9398 /* Write 4-byte data. */
9399 md_number_to_chars (p + isa_1_descsz + 4 * 6,
9400 (valueT) x86_feature_2_used, 4);
9401
9402 /* Zero out paddings. */
9403 padding = feature_2_descsz - feature_2_descsz_raw;
9404 if (padding)
9405 memset (p + isa_1_descsz + 4 * 7, 0, padding);
9406
9407 /* We probably can't restore the current segment, for there likely
9408 isn't one yet... */
9409 if (seg && subseg)
9410 subseg_set (seg, subseg);
9411}
b52c4ee4
IB
9412
9413bool
9414x86_support_sframe_p (void)
9415{
3e3e792a 9416 /* At this time, SFrame stack trace is supported for AMD64 ABI only. */
b52c4ee4
IB
9417 return (x86_elf_abi == X86_64_ABI);
9418}
9419
9420bool
9421x86_sframe_ra_tracking_p (void)
9422{
9423 /* In AMD64, return address is always stored on the stack at a fixed offset
9424 from the CFA (provided via x86_sframe_cfa_ra_offset ()).
9425 Do not track explicitly via an SFrame Frame Row Entry. */
9426 return false;
9427}
9428
9429offsetT
9430x86_sframe_cfa_ra_offset (void)
9431{
9432 gas_assert (x86_elf_abi == X86_64_ABI);
9433 return (offsetT) -8;
9434}
9435
9436unsigned char
9437x86_sframe_get_abi_arch (void)
9438{
9439 unsigned char sframe_abi_arch = 0;
9440
9441 if (x86_support_sframe_p ())
9442 {
9443 gas_assert (!target_big_endian);
9444 sframe_abi_arch = SFRAME_ABI_AMD64_ENDIAN_LITTLE;
9445 }
9446
9447 return sframe_abi_arch;
9448}
9449
b4a3a7b4
L
9450#endif
9451
9c33702b
JB
9452static unsigned int
9453encoding_length (const fragS *start_frag, offsetT start_off,
9454 const char *frag_now_ptr)
9455{
9456 unsigned int len = 0;
9457
9458 if (start_frag != frag_now)
9459 {
9460 const fragS *fr = start_frag;
9461
9462 do {
9463 len += fr->fr_fix;
9464 fr = fr->fr_next;
9465 } while (fr && fr != frag_now);
9466 }
9467
9468 return len - start_off + (frag_now_ptr - frag_now->fr_literal);
9469}
9470
e379e5f3 9471/* Return 1 for test, and, cmp, add, sub, inc and dec which may
79d72f45
HL
9472 be macro-fused with conditional jumps.
9473 NB: If TEST/AND/CMP/ADD/SUB/INC/DEC is of RIP relative address,
9474 or is one of the following format:
9475
9476 cmp m, imm
9477 add m, imm
9478 sub m, imm
9479 test m, imm
9480 and m, imm
9481 inc m
9482 dec m
9483
9484 it is unfusible. */
e379e5f3
L
9485
9486static int
79d72f45 9487maybe_fused_with_jcc_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9488{
9489 /* No RIP address. */
9490 if (i.base_reg && i.base_reg->reg_num == RegIP)
9491 return 0;
9492
389d00a5 9493 /* No opcodes outside of base encoding space. */
ddb62495 9494 if (i.tm.opcode_space != SPACE_BASE)
e379e5f3
L
9495 return 0;
9496
79d72f45
HL
9497 /* add, sub without add/sub m, imm. */
9498 if (i.tm.base_opcode <= 5
e379e5f3
L
9499 || (i.tm.base_opcode >= 0x28 && i.tm.base_opcode <= 0x2d)
9500 || ((i.tm.base_opcode | 3) == 0x83
79d72f45 9501 && (i.tm.extension_opcode == 0x5
e379e5f3 9502 || i.tm.extension_opcode == 0x0)))
79d72f45
HL
9503 {
9504 *mf_cmp_p = mf_cmp_alu_cmp;
9505 return !(i.mem_operands && i.imm_operands);
9506 }
e379e5f3 9507
79d72f45
HL
9508 /* and without and m, imm. */
9509 if ((i.tm.base_opcode >= 0x20 && i.tm.base_opcode <= 0x25)
9510 || ((i.tm.base_opcode | 3) == 0x83
9511 && i.tm.extension_opcode == 0x4))
9512 {
9513 *mf_cmp_p = mf_cmp_test_and;
9514 return !(i.mem_operands && i.imm_operands);
9515 }
9516
9517 /* test without test m imm. */
e379e5f3
L
9518 if ((i.tm.base_opcode | 1) == 0x85
9519 || (i.tm.base_opcode | 1) == 0xa9
9520 || ((i.tm.base_opcode | 1) == 0xf7
79d72f45
HL
9521 && i.tm.extension_opcode == 0))
9522 {
9523 *mf_cmp_p = mf_cmp_test_and;
9524 return !(i.mem_operands && i.imm_operands);
9525 }
9526
9527 /* cmp without cmp m, imm. */
9528 if ((i.tm.base_opcode >= 0x38 && i.tm.base_opcode <= 0x3d)
e379e5f3
L
9529 || ((i.tm.base_opcode | 3) == 0x83
9530 && (i.tm.extension_opcode == 0x7)))
79d72f45
HL
9531 {
9532 *mf_cmp_p = mf_cmp_alu_cmp;
9533 return !(i.mem_operands && i.imm_operands);
9534 }
e379e5f3 9535
79d72f45 9536 /* inc, dec without inc/dec m. */
734dfd1c 9537 if ((is_cpu (&i.tm, CpuNo64)
e379e5f3
L
9538 && (i.tm.base_opcode | 0xf) == 0x4f)
9539 || ((i.tm.base_opcode | 1) == 0xff
9540 && i.tm.extension_opcode <= 0x1))
79d72f45
HL
9541 {
9542 *mf_cmp_p = mf_cmp_incdec;
9543 return !i.mem_operands;
9544 }
e379e5f3
L
9545
9546 return 0;
9547}
9548
9549/* Return 1 if a FUSED_JCC_PADDING frag should be generated. */
9550
9551static int
79d72f45 9552add_fused_jcc_padding_frag_p (enum mf_cmp_kind* mf_cmp_p)
e379e5f3
L
9553{
9554 /* NB: Don't work with COND_JUMP86 without i386. */
9555 if (!align_branch_power
9556 || now_seg == absolute_section
9557 || !cpu_arch_flags.bitfield.cpui386
9558 || !(align_branch & align_branch_fused_bit))
9559 return 0;
9560
79d72f45 9561 if (maybe_fused_with_jcc_p (mf_cmp_p))
e379e5f3
L
9562 {
9563 if (last_insn.kind == last_insn_other
9564 || last_insn.seg != now_seg)
9565 return 1;
9566 if (flag_debug)
9567 as_warn_where (last_insn.file, last_insn.line,
9568 _("`%s` skips -malign-branch-boundary on `%s`"),
76d3f746 9569 last_insn.name, insn_name (&i.tm));
e379e5f3
L
9570 }
9571
9572 return 0;
9573}
9574
9575/* Return 1 if a BRANCH_PREFIX frag should be generated. */
9576
9577static int
9578add_branch_prefix_frag_p (void)
9579{
9580 /* NB: Don't work with COND_JUMP86 without i386. Don't add prefix
9581 to PadLock instructions since they include prefixes in opcode. */
9582 if (!align_branch_power
9583 || !align_branch_prefix_size
9584 || now_seg == absolute_section
734dfd1c 9585 || is_cpu (&i.tm, CpuPadLock)
e379e5f3
L
9586 || !cpu_arch_flags.bitfield.cpui386)
9587 return 0;
9588
9589 /* Don't add prefix if it is a prefix or there is no operand in case
9590 that segment prefix is special. */
9591 if (!i.operands || i.tm.opcode_modifier.isprefix)
9592 return 0;
9593
9594 if (last_insn.kind == last_insn_other
9595 || last_insn.seg != now_seg)
9596 return 1;
9597
9598 if (flag_debug)
9599 as_warn_where (last_insn.file, last_insn.line,
9600 _("`%s` skips -malign-branch-boundary on `%s`"),
76d3f746 9601 last_insn.name, insn_name (&i.tm));
e379e5f3
L
9602
9603 return 0;
9604}
9605
9606/* Return 1 if a BRANCH_PADDING frag should be generated. */
9607
9608static int
79d72f45
HL
9609add_branch_padding_frag_p (enum align_branch_kind *branch_p,
9610 enum mf_jcc_kind *mf_jcc_p)
e379e5f3
L
9611{
9612 int add_padding;
9613
9614 /* NB: Don't work with COND_JUMP86 without i386. */
9615 if (!align_branch_power
9616 || now_seg == absolute_section
389d00a5 9617 || !cpu_arch_flags.bitfield.cpui386
ddb62495 9618 || i.tm.opcode_space != SPACE_BASE)
e379e5f3
L
9619 return 0;
9620
9621 add_padding = 0;
9622
9623 /* Check for jcc and direct jmp. */
9624 if (i.tm.opcode_modifier.jump == JUMP)
9625 {
9626 if (i.tm.base_opcode == JUMP_PC_RELATIVE)
9627 {
9628 *branch_p = align_branch_jmp;
9629 add_padding = align_branch & align_branch_jmp_bit;
9630 }
9631 else
9632 {
79d72f45
HL
9633 /* Because J<cc> and JN<cc> share same group in macro-fusible table,
9634 igore the lowest bit. */
9635 *mf_jcc_p = (i.tm.base_opcode & 0x0e) >> 1;
e379e5f3
L
9636 *branch_p = align_branch_jcc;
9637 if ((align_branch & align_branch_jcc_bit))
9638 add_padding = 1;
9639 }
9640 }
e379e5f3
L
9641 else if ((i.tm.base_opcode | 1) == 0xc3)
9642 {
9643 /* Near ret. */
9644 *branch_p = align_branch_ret;
9645 if ((align_branch & align_branch_ret_bit))
9646 add_padding = 1;
9647 }
9648 else
9649 {
9650 /* Check for indirect jmp, direct and indirect calls. */
9651 if (i.tm.base_opcode == 0xe8)
9652 {
9653 /* Direct call. */
9654 *branch_p = align_branch_call;
9655 if ((align_branch & align_branch_call_bit))
9656 add_padding = 1;
9657 }
9658 else if (i.tm.base_opcode == 0xff
9659 && (i.tm.extension_opcode == 2
9660 || i.tm.extension_opcode == 4))
9661 {
9662 /* Indirect call and jmp. */
9663 *branch_p = align_branch_indirect;
9664 if ((align_branch & align_branch_indirect_bit))
9665 add_padding = 1;
9666 }
9667
9668 if (add_padding
9669 && i.disp_operands
9670 && tls_get_addr
9671 && (i.op[0].disps->X_op == O_symbol
9672 || (i.op[0].disps->X_op == O_subtract
9673 && i.op[0].disps->X_op_symbol == GOT_symbol)))
9674 {
9675 symbolS *s = i.op[0].disps->X_add_symbol;
9676 /* No padding to call to global or undefined tls_get_addr. */
9677 if ((S_IS_EXTERNAL (s) || !S_IS_DEFINED (s))
9678 && strcmp (S_GET_NAME (s), tls_get_addr) == 0)
9679 return 0;
9680 }
9681 }
9682
9683 if (add_padding
9684 && last_insn.kind != last_insn_other
9685 && last_insn.seg == now_seg)
9686 {
9687 if (flag_debug)
9688 as_warn_where (last_insn.file, last_insn.line,
9689 _("`%s` skips -malign-branch-boundary on `%s`"),
76d3f746 9690 last_insn.name, insn_name (&i.tm));
e379e5f3
L
9691 return 0;
9692 }
9693
9694 return add_padding;
9695}
9696
29b0f896 9697static void
e3bb37b5 9698output_insn (void)
29b0f896 9699{
2bbd9c25
JJ
9700 fragS *insn_start_frag;
9701 offsetT insn_start_off;
e379e5f3
L
9702 fragS *fragP = NULL;
9703 enum align_branch_kind branch = align_branch_none;
79d72f45
HL
9704 /* The initializer is arbitrary just to avoid uninitialized error.
9705 it's actually either assigned in add_branch_padding_frag_p
9706 or never be used. */
9707 enum mf_jcc_kind mf_jcc = mf_jcc_jo;
2bbd9c25 9708
b4a3a7b4 9709#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
48ef937e 9710 if (IS_ELF && x86_used_note && now_seg != absolute_section)
b4a3a7b4 9711 {
32930e4e 9712 if ((i.xstate & xstate_tmm) == xstate_tmm
734dfd1c 9713 || is_cpu (&i.tm, CpuAMX_TILE))
32930e4e
L
9714 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_TMM;
9715
734dfd1c
JB
9716 if (is_cpu (&i.tm, Cpu8087)
9717 || is_cpu (&i.tm, Cpu287)
9718 || is_cpu (&i.tm, Cpu387)
9719 || is_cpu (&i.tm, Cpu687)
9720 || is_cpu (&i.tm, CpuFISTTP))
b4a3a7b4 9721 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_X87;
014d61ea 9722
921eafea 9723 if ((i.xstate & xstate_mmx)
7fc69528
JB
9724 || i.tm.mnem_off == MN_emms
9725 || i.tm.mnem_off == MN_femms)
b4a3a7b4 9726 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MMX;
014d61ea 9727
32930e4e
L
9728 if (i.index_reg)
9729 {
9730 if (i.index_reg->reg_type.bitfield.zmmword)
9731 i.xstate |= xstate_zmm;
9732 else if (i.index_reg->reg_type.bitfield.ymmword)
9733 i.xstate |= xstate_ymm;
9734 else if (i.index_reg->reg_type.bitfield.xmmword)
9735 i.xstate |= xstate_xmm;
9736 }
014d61ea
JB
9737
9738 /* vzeroall / vzeroupper */
734dfd1c 9739 if (i.tm.base_opcode == 0x77 && is_cpu (&i.tm, CpuAVX))
014d61ea
JB
9740 i.xstate |= xstate_ymm;
9741
c4694f17 9742 if ((i.xstate & xstate_xmm)
389d00a5
JB
9743 /* ldmxcsr / stmxcsr / vldmxcsr / vstmxcsr */
9744 || (i.tm.base_opcode == 0xae
734dfd1c
JB
9745 && (is_cpu (&i.tm, CpuSSE)
9746 || is_cpu (&i.tm, CpuAVX)))
9747 || is_cpu (&i.tm, CpuWideKL)
9748 || is_cpu (&i.tm, CpuKL))
b4a3a7b4 9749 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XMM;
014d61ea 9750
921eafea 9751 if ((i.xstate & xstate_ymm) == xstate_ymm)
b4a3a7b4 9752 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_YMM;
921eafea 9753 if ((i.xstate & xstate_zmm) == xstate_zmm)
b4a3a7b4 9754 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_ZMM;
6225c532 9755 if (i.mask.reg || (i.xstate & xstate_mask) == xstate_mask)
32930e4e 9756 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_MASK;
734dfd1c 9757 if (is_cpu (&i.tm, CpuFXSR))
b4a3a7b4 9758 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_FXSR;
734dfd1c 9759 if (is_cpu (&i.tm, CpuXsave))
b4a3a7b4 9760 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVE;
734dfd1c 9761 if (is_cpu (&i.tm, CpuXsaveopt))
b4a3a7b4 9762 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT;
734dfd1c 9763 if (is_cpu (&i.tm, CpuXSAVEC))
b4a3a7b4 9764 x86_feature_2_used |= GNU_PROPERTY_X86_FEATURE_2_XSAVEC;
b0ab0693
L
9765
9766 if (x86_feature_2_used
734dfd1c
JB
9767 || is_cpu (&i.tm, CpuCMOV)
9768 || is_cpu (&i.tm, CpuSYSCALL)
7fc69528 9769 || i.tm.mnem_off == MN_cmpxchg8b)
b0ab0693 9770 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_BASELINE;
734dfd1c
JB
9771 if (is_cpu (&i.tm, CpuSSE3)
9772 || is_cpu (&i.tm, CpuSSSE3)
9773 || is_cpu (&i.tm, CpuSSE4_1)
9774 || is_cpu (&i.tm, CpuSSE4_2)
9775 || is_cpu (&i.tm, CpuCX16)
9776 || is_cpu (&i.tm, CpuPOPCNT)
b0ab0693
L
9777 /* LAHF-SAHF insns in 64-bit mode. */
9778 || (flag_code == CODE_64BIT
35648716 9779 && (i.tm.base_opcode | 1) == 0x9f
ddb62495 9780 && i.tm.opcode_space == SPACE_BASE))
b0ab0693 9781 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V2;
734dfd1c
JB
9782 if (is_cpu (&i.tm, CpuAVX)
9783 || is_cpu (&i.tm, CpuAVX2)
a9860005
JB
9784 /* Any VEX encoded insns execpt for AVX512F, AVX512BW, AVX512DQ,
9785 XOP, FMA4, LPW, TBM, and AMX. */
b0ab0693 9786 || (i.tm.opcode_modifier.vex
734dfd1c
JB
9787 && !is_cpu (&i.tm, CpuAVX512F)
9788 && !is_cpu (&i.tm, CpuAVX512BW)
9789 && !is_cpu (&i.tm, CpuAVX512DQ)
9790 && !is_cpu (&i.tm, CpuXOP)
9791 && !is_cpu (&i.tm, CpuFMA4)
9792 && !is_cpu (&i.tm, CpuLWP)
9793 && !is_cpu (&i.tm, CpuTBM)
b0ab0693 9794 && !(x86_feature_2_used & GNU_PROPERTY_X86_FEATURE_2_TMM))
734dfd1c
JB
9795 || is_cpu (&i.tm, CpuF16C)
9796 || is_cpu (&i.tm, CpuFMA)
9797 || is_cpu (&i.tm, CpuLZCNT)
9798 || is_cpu (&i.tm, CpuMovbe)
9799 || is_cpu (&i.tm, CpuXSAVES)
b0ab0693
L
9800 || (x86_feature_2_used
9801 & (GNU_PROPERTY_X86_FEATURE_2_XSAVE
9802 | GNU_PROPERTY_X86_FEATURE_2_XSAVEOPT
9803 | GNU_PROPERTY_X86_FEATURE_2_XSAVEC)) != 0)
9804 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V3;
734dfd1c
JB
9805 if (is_cpu (&i.tm, CpuAVX512F)
9806 || is_cpu (&i.tm, CpuAVX512BW)
9807 || is_cpu (&i.tm, CpuAVX512DQ)
9808 || is_cpu (&i.tm, CpuAVX512VL)
a9860005
JB
9809 /* Any EVEX encoded insns except for AVX512ER, AVX512PF,
9810 AVX512-4FMAPS, and AVX512-4VNNIW. */
b0ab0693 9811 || (i.tm.opcode_modifier.evex
734dfd1c
JB
9812 && !is_cpu (&i.tm, CpuAVX512ER)
9813 && !is_cpu (&i.tm, CpuAVX512PF)
9814 && !is_cpu (&i.tm, CpuAVX512_4FMAPS)
9815 && !is_cpu (&i.tm, CpuAVX512_4VNNIW)))
b0ab0693 9816 x86_isa_1_used |= GNU_PROPERTY_X86_ISA_1_V4;
b4a3a7b4
L
9817 }
9818#endif
9819
29b0f896
AM
9820 /* Tie dwarf2 debug info to the address at the start of the insn.
9821 We can't do this after the insn has been output as the current
9822 frag may have been closed off. eg. by frag_var. */
9823 dwarf2_emit_insn (0);
9824
2bbd9c25
JJ
9825 insn_start_frag = frag_now;
9826 insn_start_off = frag_now_fix ();
9827
79d72f45 9828 if (add_branch_padding_frag_p (&branch, &mf_jcc))
e379e5f3
L
9829 {
9830 char *p;
9831 /* Branch can be 8 bytes. Leave some room for prefixes. */
9832 unsigned int max_branch_padding_size = 14;
9833
9834 /* Align section to boundary. */
9835 record_alignment (now_seg, align_branch_power);
9836
9837 /* Make room for padding. */
9838 frag_grow (max_branch_padding_size);
9839
9840 /* Start of the padding. */
9841 p = frag_more (0);
9842
9843 fragP = frag_now;
9844
9845 frag_var (rs_machine_dependent, max_branch_padding_size, 0,
9846 ENCODE_RELAX_STATE (BRANCH_PADDING, 0),
9847 NULL, 0, p);
9848
79d72f45 9849 fragP->tc_frag_data.mf_type = mf_jcc;
e379e5f3
L
9850 fragP->tc_frag_data.branch_type = branch;
9851 fragP->tc_frag_data.max_bytes = max_branch_padding_size;
9852 }
9853
d59a54c2
JB
9854 if (!cpu_arch_flags.bitfield.cpui386 && (flag_code != CODE_16BIT)
9855 && !pre_386_16bit_warned)
9856 {
9857 as_warn (_("use .code16 to ensure correct addressing mode"));
9858 pre_386_16bit_warned = true;
9859 }
9860
29b0f896 9861 /* Output jumps. */
0cfa3eb3 9862 if (i.tm.opcode_modifier.jump == JUMP)
29b0f896 9863 output_branch ();
0cfa3eb3
JB
9864 else if (i.tm.opcode_modifier.jump == JUMP_BYTE
9865 || i.tm.opcode_modifier.jump == JUMP_DWORD)
29b0f896 9866 output_jump ();
0cfa3eb3 9867 else if (i.tm.opcode_modifier.jump == JUMP_INTERSEGMENT)
29b0f896
AM
9868 output_interseg_jump ();
9869 else
9870 {
9871 /* Output normal instructions here. */
9872 char *p;
9873 unsigned char *q;
47465058 9874 unsigned int j;
79d72f45 9875 enum mf_cmp_kind mf_cmp;
4dffcebc 9876
e4e00185 9877 if (avoid_fence
389d00a5
JB
9878 && (i.tm.base_opcode == 0xaee8
9879 || i.tm.base_opcode == 0xaef0
9880 || i.tm.base_opcode == 0xaef8))
48ef937e
JB
9881 {
9882 /* Encode lfence, mfence, and sfence as
9883 f0 83 04 24 00 lock addl $0x0, (%{re}sp). */
47f4115a 9884 if (flag_code == CODE_16BIT)
76d3f746 9885 as_bad (_("Cannot convert `%s' in 16-bit mode"), insn_name (&i.tm));
47f4115a
JB
9886 else if (omit_lock_prefix)
9887 as_bad (_("Cannot convert `%s' with `-momit-lock-prefix=yes' in effect"),
76d3f746 9888 insn_name (&i.tm));
47f4115a 9889 else if (now_seg != absolute_section)
48ef937e
JB
9890 {
9891 offsetT val = 0x240483f0ULL;
9892
9893 p = frag_more (5);
9894 md_number_to_chars (p, val, 5);
9895 }
9896 else
9897 abs_section_offset += 5;
9898 return;
9899 }
e4e00185 9900
d022bddd
IT
9901 /* Some processors fail on LOCK prefix. This options makes
9902 assembler ignore LOCK prefix and serves as a workaround. */
9903 if (omit_lock_prefix)
9904 {
35648716
JB
9905 if (i.tm.base_opcode == LOCK_PREFIX_OPCODE
9906 && i.tm.opcode_modifier.isprefix)
d022bddd
IT
9907 return;
9908 i.prefix[LOCK_PREFIX] = 0;
9909 }
9910
e379e5f3
L
9911 if (branch)
9912 /* Skip if this is a branch. */
9913 ;
79d72f45 9914 else if (add_fused_jcc_padding_frag_p (&mf_cmp))
e379e5f3
L
9915 {
9916 /* Make room for padding. */
9917 frag_grow (MAX_FUSED_JCC_PADDING_SIZE);
9918 p = frag_more (0);
9919
9920 fragP = frag_now;
9921
9922 frag_var (rs_machine_dependent, MAX_FUSED_JCC_PADDING_SIZE, 0,
9923 ENCODE_RELAX_STATE (FUSED_JCC_PADDING, 0),
9924 NULL, 0, p);
9925
79d72f45 9926 fragP->tc_frag_data.mf_type = mf_cmp;
e379e5f3
L
9927 fragP->tc_frag_data.branch_type = align_branch_fused;
9928 fragP->tc_frag_data.max_bytes = MAX_FUSED_JCC_PADDING_SIZE;
9929 }
9930 else if (add_branch_prefix_frag_p ())
9931 {
9932 unsigned int max_prefix_size = align_branch_prefix_size;
9933
9934 /* Make room for padding. */
9935 frag_grow (max_prefix_size);
9936 p = frag_more (0);
9937
9938 fragP = frag_now;
9939
9940 frag_var (rs_machine_dependent, max_prefix_size, 0,
9941 ENCODE_RELAX_STATE (BRANCH_PREFIX, 0),
9942 NULL, 0, p);
9943
9944 fragP->tc_frag_data.max_bytes = max_prefix_size;
9945 }
9946
43234a1e
L
9947 /* Since the VEX/EVEX prefix contains the implicit prefix, we
9948 don't need the explicit prefix. */
cf665fee 9949 if (!is_any_vex_encoding (&i.tm))
bc4bd9ab 9950 {
7b47a312 9951 switch (i.tm.opcode_modifier.opcodeprefix)
bc4bd9ab 9952 {
7b47a312
L
9953 case PREFIX_0X66:
9954 add_prefix (0x66);
9955 break;
9956 case PREFIX_0XF2:
9957 add_prefix (0xf2);
9958 break;
9959 case PREFIX_0XF3:
734dfd1c 9960 if (!is_cpu (&i.tm, CpuPadLock)
8b65b895
L
9961 || (i.prefix[REP_PREFIX] != 0xf3))
9962 add_prefix (0xf3);
c0f3af97 9963 break;
7b47a312 9964 case PREFIX_NONE:
9a182d04 9965 switch (i.opcode_length)
c0f3af97 9966 {
7b47a312 9967 case 2:
7b47a312 9968 break;
9a182d04 9969 case 1:
7b47a312 9970 /* Check for pseudo prefixes. */
9a182d04
JB
9971 if (!i.tm.opcode_modifier.isprefix || i.tm.base_opcode)
9972 break;
7b47a312
L
9973 as_bad_where (insn_start_frag->fr_file,
9974 insn_start_frag->fr_line,
9975 _("pseudo prefix without instruction"));
9976 return;
9977 default:
9978 abort ();
4dffcebc 9979 }
c0f3af97 9980 break;
c0f3af97
L
9981 default:
9982 abort ();
bc4bd9ab 9983 }
c0f3af97 9984
6d19a37a 9985#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
cf61b747
L
9986 /* For x32, add a dummy REX_OPCODE prefix for mov/add with
9987 R_X86_64_GOTTPOFF relocation so that linker can safely
14470f07
L
9988 perform IE->LE optimization. A dummy REX_OPCODE prefix
9989 is also needed for lea with R_X86_64_GOTPC32_TLSDESC
9990 relocation for GDesc -> IE/LE optimization. */
cf61b747
L
9991 if (x86_elf_abi == X86_64_X32_ABI
9992 && i.operands == 2
14470f07
L
9993 && (i.reloc[0] == BFD_RELOC_X86_64_GOTTPOFF
9994 || i.reloc[0] == BFD_RELOC_X86_64_GOTPC32_TLSDESC)
cf61b747
L
9995 && i.prefix[REX_PREFIX] == 0)
9996 add_prefix (REX_OPCODE);
6d19a37a 9997#endif
cf61b747 9998
c0f3af97
L
9999 /* The prefix bytes. */
10000 for (j = ARRAY_SIZE (i.prefix), q = i.prefix; j > 0; j--, q++)
10001 if (*q)
48ef937e 10002 frag_opcode_byte (*q);
0f10071e 10003 }
ae5c1c7b 10004 else
c0f3af97
L
10005 {
10006 for (j = 0, q = i.prefix; j < ARRAY_SIZE (i.prefix); j++, q++)
10007 if (*q)
10008 switch (j)
10009 {
c0f3af97
L
10010 case SEG_PREFIX:
10011 case ADDR_PREFIX:
48ef937e 10012 frag_opcode_byte (*q);
c0f3af97
L
10013 break;
10014 default:
10015 /* There should be no other prefixes for instructions
10016 with VEX prefix. */
10017 abort ();
10018 }
10019
43234a1e
L
10020 /* For EVEX instructions i.vrex should become 0 after
10021 build_evex_prefix. For VEX instructions upper 16 registers
10022 aren't available, so VREX should be 0. */
10023 if (i.vrex)
10024 abort ();
c0f3af97 10025 /* Now the VEX prefix. */
48ef937e
JB
10026 if (now_seg != absolute_section)
10027 {
10028 p = frag_more (i.vex.length);
10029 for (j = 0; j < i.vex.length; j++)
10030 p[j] = i.vex.bytes[j];
10031 }
10032 else
10033 abs_section_offset += i.vex.length;
c0f3af97 10034 }
252b5132 10035
29b0f896 10036 /* Now the opcode; be careful about word order here! */
389d00a5
JB
10037 j = i.opcode_length;
10038 if (!i.vex.length)
ddb62495 10039 switch (i.tm.opcode_space)
389d00a5
JB
10040 {
10041 case SPACE_BASE:
10042 break;
10043 case SPACE_0F:
10044 ++j;
10045 break;
10046 case SPACE_0F38:
10047 case SPACE_0F3A:
10048 j += 2;
10049 break;
10050 default:
10051 abort ();
10052 }
10053
48ef937e 10054 if (now_seg == absolute_section)
389d00a5
JB
10055 abs_section_offset += j;
10056 else if (j == 1)
29b0f896
AM
10057 {
10058 FRAG_APPEND_1_CHAR (i.tm.base_opcode);
10059 }
10060 else
10061 {
389d00a5
JB
10062 p = frag_more (j);
10063 if (!i.vex.length
ddb62495 10064 && i.tm.opcode_space != SPACE_BASE)
389d00a5
JB
10065 {
10066 *p++ = 0x0f;
ddb62495
JB
10067 if (i.tm.opcode_space != SPACE_0F)
10068 *p++ = i.tm.opcode_space == SPACE_0F38
389d00a5
JB
10069 ? 0x38 : 0x3a;
10070 }
10071
9a182d04 10072 switch (i.opcode_length)
331d2d0d 10073 {
4dffcebc 10074 case 2:
389d00a5
JB
10075 /* Put out high byte first: can't use md_number_to_chars! */
10076 *p++ = (i.tm.base_opcode >> 8) & 0xff;
10077 /* Fall through. */
10078 case 1:
10079 *p = i.tm.base_opcode & 0xff;
4dffcebc
L
10080 break;
10081 default:
10082 abort ();
10083 break;
331d2d0d 10084 }
0f10071e 10085
29b0f896 10086 }
3e73aa7c 10087
29b0f896 10088 /* Now the modrm byte and sib byte (if present). */
40fb9820 10089 if (i.tm.opcode_modifier.modrm)
29b0f896 10090 {
48ef937e
JB
10091 frag_opcode_byte ((i.rm.regmem << 0)
10092 | (i.rm.reg << 3)
10093 | (i.rm.mode << 6));
29b0f896
AM
10094 /* If i.rm.regmem == ESP (4)
10095 && i.rm.mode != (Register mode)
10096 && not 16 bit
10097 ==> need second modrm byte. */
10098 if (i.rm.regmem == ESCAPE_TO_TWO_BYTE_ADDRESSING
10099 && i.rm.mode != 3
dc821c5f 10100 && !(i.base_reg && i.base_reg->reg_type.bitfield.word))
48ef937e
JB
10101 frag_opcode_byte ((i.sib.base << 0)
10102 | (i.sib.index << 3)
10103 | (i.sib.scale << 6));
29b0f896 10104 }
3e73aa7c 10105
29b0f896 10106 if (i.disp_operands)
2bbd9c25 10107 output_disp (insn_start_frag, insn_start_off);
3e73aa7c 10108
29b0f896 10109 if (i.imm_operands)
2bbd9c25 10110 output_imm (insn_start_frag, insn_start_off);
9c33702b
JB
10111
10112 /*
10113 * frag_now_fix () returning plain abs_section_offset when we're in the
10114 * absolute section, and abs_section_offset not getting updated as data
10115 * gets added to the frag breaks the logic below.
10116 */
10117 if (now_seg != absolute_section)
10118 {
10119 j = encoding_length (insn_start_frag, insn_start_off, frag_more (0));
10120 if (j > 15)
10121 as_warn (_("instruction length of %u bytes exceeds the limit of 15"),
10122 j);
e379e5f3
L
10123 else if (fragP)
10124 {
10125 /* NB: Don't add prefix with GOTPC relocation since
10126 output_disp() above depends on the fixed encoding
10127 length. Can't add prefix with TLS relocation since
10128 it breaks TLS linker optimization. */
10129 unsigned int max = i.has_gotpc_tls_reloc ? 0 : 15 - j;
10130 /* Prefix count on the current instruction. */
10131 unsigned int count = i.vex.length;
10132 unsigned int k;
10133 for (k = 0; k < ARRAY_SIZE (i.prefix); k++)
10134 /* REX byte is encoded in VEX/EVEX prefix. */
10135 if (i.prefix[k] && (k != REX_PREFIX || !i.vex.length))
10136 count++;
10137
10138 /* Count prefixes for extended opcode maps. */
10139 if (!i.vex.length)
ddb62495 10140 switch (i.tm.opcode_space)
e379e5f3 10141 {
389d00a5 10142 case SPACE_BASE:
e379e5f3 10143 break;
389d00a5
JB
10144 case SPACE_0F:
10145 count++;
e379e5f3 10146 break;
389d00a5
JB
10147 case SPACE_0F38:
10148 case SPACE_0F3A:
10149 count += 2;
e379e5f3
L
10150 break;
10151 default:
10152 abort ();
10153 }
10154
10155 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
10156 == BRANCH_PREFIX)
10157 {
10158 /* Set the maximum prefix size in BRANCH_PREFIX
10159 frag. */
10160 if (fragP->tc_frag_data.max_bytes > max)
10161 fragP->tc_frag_data.max_bytes = max;
10162 if (fragP->tc_frag_data.max_bytes > count)
10163 fragP->tc_frag_data.max_bytes -= count;
10164 else
10165 fragP->tc_frag_data.max_bytes = 0;
10166 }
10167 else
10168 {
10169 /* Remember the maximum prefix size in FUSED_JCC_PADDING
10170 frag. */
10171 unsigned int max_prefix_size;
10172 if (align_branch_prefix_size > max)
10173 max_prefix_size = max;
10174 else
10175 max_prefix_size = align_branch_prefix_size;
10176 if (max_prefix_size > count)
10177 fragP->tc_frag_data.max_prefix_length
10178 = max_prefix_size - count;
10179 }
10180
10181 /* Use existing segment prefix if possible. Use CS
10182 segment prefix in 64-bit mode. In 32-bit mode, use SS
10183 segment prefix with ESP/EBP base register and use DS
10184 segment prefix without ESP/EBP base register. */
10185 if (i.prefix[SEG_PREFIX])
10186 fragP->tc_frag_data.default_prefix = i.prefix[SEG_PREFIX];
10187 else if (flag_code == CODE_64BIT)
10188 fragP->tc_frag_data.default_prefix = CS_PREFIX_OPCODE;
10189 else if (i.base_reg
10190 && (i.base_reg->reg_num == 4
10191 || i.base_reg->reg_num == 5))
10192 fragP->tc_frag_data.default_prefix = SS_PREFIX_OPCODE;
10193 else
10194 fragP->tc_frag_data.default_prefix = DS_PREFIX_OPCODE;
10195 }
9c33702b 10196 }
29b0f896 10197 }
252b5132 10198
e379e5f3
L
10199 /* NB: Don't work with COND_JUMP86 without i386. */
10200 if (align_branch_power
10201 && now_seg != absolute_section
10202 && cpu_arch_flags.bitfield.cpui386)
10203 {
10204 /* Terminate each frag so that we can add prefix and check for
10205 fused jcc. */
10206 frag_wane (frag_now);
10207 frag_new (0);
10208 }
10209
29b0f896
AM
10210#ifdef DEBUG386
10211 if (flag_debug)
10212 {
7b81dfbb 10213 pi ("" /*line*/, &i);
29b0f896
AM
10214 }
10215#endif /* DEBUG386 */
10216}
252b5132 10217
e205caa7
L
10218/* Return the size of the displacement operand N. */
10219
10220static int
10221disp_size (unsigned int n)
10222{
10223 int size = 4;
43234a1e 10224
b5014f7a 10225 if (i.types[n].bitfield.disp64)
40fb9820
L
10226 size = 8;
10227 else if (i.types[n].bitfield.disp8)
10228 size = 1;
10229 else if (i.types[n].bitfield.disp16)
10230 size = 2;
e205caa7
L
10231 return size;
10232}
10233
10234/* Return the size of the immediate operand N. */
10235
10236static int
10237imm_size (unsigned int n)
10238{
10239 int size = 4;
40fb9820
L
10240 if (i.types[n].bitfield.imm64)
10241 size = 8;
10242 else if (i.types[n].bitfield.imm8 || i.types[n].bitfield.imm8s)
10243 size = 1;
10244 else if (i.types[n].bitfield.imm16)
10245 size = 2;
e205caa7
L
10246 return size;
10247}
10248
29b0f896 10249static void
64e74474 10250output_disp (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10251{
10252 char *p;
10253 unsigned int n;
252b5132 10254
29b0f896
AM
10255 for (n = 0; n < i.operands; n++)
10256 {
b5014f7a 10257 if (operand_type_check (i.types[n], disp))
29b0f896 10258 {
48ef937e
JB
10259 int size = disp_size (n);
10260
10261 if (now_seg == absolute_section)
10262 abs_section_offset += size;
10263 else if (i.op[n].disps->X_op == O_constant)
29b0f896 10264 {
43234a1e 10265 offsetT val = i.op[n].disps->X_add_number;
252b5132 10266
629cfaf1
JB
10267 val = offset_in_range (val >> (size == 1 ? i.memshift : 0),
10268 size);
29b0f896
AM
10269 p = frag_more (size);
10270 md_number_to_chars (p, val, size);
10271 }
10272 else
10273 {
f86103b7 10274 enum bfd_reloc_code_real reloc_type;
a775efc8
JB
10275 bool pcrel = (i.flags[n] & Operand_PCrel) != 0;
10276 bool sign = (flag_code == CODE_64BIT && size == 4
10277 && (!want_disp32 (&i.tm)
10278 || (i.tm.opcode_modifier.jump && !i.jumpabsolute
10279 && !i.types[n].bitfield.baseindex)))
10280 || pcrel;
02a86693 10281 fixS *fixP;
29b0f896 10282
e205caa7 10283 /* We can't have 8 bit displacement here. */
9c2799c2 10284 gas_assert (!i.types[n].bitfield.disp8);
e205caa7 10285
29b0f896
AM
10286 /* The PC relative address is computed relative
10287 to the instruction boundary, so in case immediate
10288 fields follows, we need to adjust the value. */
10289 if (pcrel && i.imm_operands)
10290 {
29b0f896 10291 unsigned int n1;
e205caa7 10292 int sz = 0;
252b5132 10293
29b0f896 10294 for (n1 = 0; n1 < i.operands; n1++)
40fb9820 10295 if (operand_type_check (i.types[n1], imm))
252b5132 10296 {
e205caa7 10297 /* Only one immediate is allowed for PC
e3bf0aad
JB
10298 relative address, except with .insn. */
10299 gas_assert (sz == 0 || dot_insn ());
10300 sz += imm_size (n1);
252b5132 10301 }
e3bf0aad 10302 /* We should find at least one immediate. */
9c2799c2 10303 gas_assert (sz != 0);
e3bf0aad 10304 i.op[n].disps->X_add_number -= sz;
29b0f896 10305 }
520dc8e8 10306
29b0f896 10307 p = frag_more (size);
d258b828 10308 reloc_type = reloc (size, pcrel, sign, i.reloc[n]);
d6ab8113 10309 if (GOT_symbol
2bbd9c25 10310 && GOT_symbol == i.op[n].disps->X_add_symbol
d6ab8113 10311 && (((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10312 || reloc_type == BFD_RELOC_X86_64_32S
10313 || (reloc_type == BFD_RELOC_64
10314 && object_64bit))
d6ab8113
JB
10315 && (i.op[n].disps->X_op == O_symbol
10316 || (i.op[n].disps->X_op == O_add
10317 && ((symbol_get_value_expression
10318 (i.op[n].disps->X_op_symbol)->X_op)
10319 == O_subtract))))
10320 || reloc_type == BFD_RELOC_32_PCREL))
2bbd9c25 10321 {
4fa24527 10322 if (!object_64bit)
7b81dfbb
AJ
10323 {
10324 reloc_type = BFD_RELOC_386_GOTPC;
5b7c81bd 10325 i.has_gotpc_tls_reloc = true;
98da05bf 10326 i.op[n].disps->X_add_number +=
d583596c 10327 encoding_length (insn_start_frag, insn_start_off, p);
7b81dfbb
AJ
10328 }
10329 else if (reloc_type == BFD_RELOC_64)
10330 reloc_type = BFD_RELOC_X86_64_GOTPC64;
d6ab8113 10331 else
7b81dfbb
AJ
10332 /* Don't do the adjustment for x86-64, as there
10333 the pcrel addressing is relative to the _next_
10334 insn, and that is taken care of in other code. */
d6ab8113 10335 reloc_type = BFD_RELOC_X86_64_GOTPC32;
2bbd9c25 10336 }
e379e5f3
L
10337 else if (align_branch_power)
10338 {
10339 switch (reloc_type)
10340 {
10341 case BFD_RELOC_386_TLS_GD:
10342 case BFD_RELOC_386_TLS_LDM:
10343 case BFD_RELOC_386_TLS_IE:
10344 case BFD_RELOC_386_TLS_IE_32:
10345 case BFD_RELOC_386_TLS_GOTIE:
10346 case BFD_RELOC_386_TLS_GOTDESC:
10347 case BFD_RELOC_386_TLS_DESC_CALL:
10348 case BFD_RELOC_X86_64_TLSGD:
10349 case BFD_RELOC_X86_64_TLSLD:
10350 case BFD_RELOC_X86_64_GOTTPOFF:
10351 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
10352 case BFD_RELOC_X86_64_TLSDESC_CALL:
5b7c81bd 10353 i.has_gotpc_tls_reloc = true;
e379e5f3
L
10354 default:
10355 break;
10356 }
10357 }
02a86693
L
10358 fixP = fix_new_exp (frag_now, p - frag_now->fr_literal,
10359 size, i.op[n].disps, pcrel,
10360 reloc_type);
eb19308f
JB
10361
10362 if (flag_code == CODE_64BIT && size == 4 && pcrel
10363 && !i.prefix[ADDR_PREFIX])
10364 fixP->fx_signed = 1;
10365
02a86693
L
10366 /* Check for "call/jmp *mem", "mov mem, %reg",
10367 "test %reg, mem" and "binop mem, %reg" where binop
10368 is one of adc, add, and, cmp, or, sbb, sub, xor
e60f4d3b
L
10369 instructions without data prefix. Always generate
10370 R_386_GOT32X for "sym*GOT" operand in 32-bit mode. */
10371 if (i.prefix[DATA_PREFIX] == 0
10372 && (generate_relax_relocations
10373 || (!object_64bit
10374 && i.rm.mode == 0
10375 && i.rm.regmem == 5))
0cb4071e
L
10376 && (i.rm.mode == 2
10377 || (i.rm.mode == 0 && i.rm.regmem == 5))
ddb62495 10378 && i.tm.opcode_space == SPACE_BASE
02a86693
L
10379 && ((i.operands == 1
10380 && i.tm.base_opcode == 0xff
10381 && (i.rm.reg == 2 || i.rm.reg == 4))
10382 || (i.operands == 2
10383 && (i.tm.base_opcode == 0x8b
10384 || i.tm.base_opcode == 0x85
2ae4c703 10385 || (i.tm.base_opcode & ~0x38) == 0x03))))
02a86693
L
10386 {
10387 if (object_64bit)
10388 {
10389 fixP->fx_tcbit = i.rex != 0;
10390 if (i.base_reg
e968fc9b 10391 && (i.base_reg->reg_num == RegIP))
02a86693
L
10392 fixP->fx_tcbit2 = 1;
10393 }
10394 else
10395 fixP->fx_tcbit2 = 1;
10396 }
29b0f896
AM
10397 }
10398 }
10399 }
10400}
252b5132 10401
29b0f896 10402static void
64e74474 10403output_imm (fragS *insn_start_frag, offsetT insn_start_off)
29b0f896
AM
10404{
10405 char *p;
10406 unsigned int n;
252b5132 10407
29b0f896
AM
10408 for (n = 0; n < i.operands; n++)
10409 {
40fb9820 10410 if (operand_type_check (i.types[n], imm))
29b0f896 10411 {
48ef937e
JB
10412 int size = imm_size (n);
10413
10414 if (now_seg == absolute_section)
10415 abs_section_offset += size;
10416 else if (i.op[n].imms->X_op == O_constant)
29b0f896 10417 {
29b0f896 10418 offsetT val;
b4cac588 10419
29b0f896
AM
10420 val = offset_in_range (i.op[n].imms->X_add_number,
10421 size);
10422 p = frag_more (size);
10423 md_number_to_chars (p, val, size);
10424 }
10425 else
10426 {
10427 /* Not absolute_section.
10428 Need a 32-bit fixup (don't support 8bit
10429 non-absolute imms). Try to support other
10430 sizes ... */
f86103b7 10431 enum bfd_reloc_code_real reloc_type;
e205caa7 10432 int sign;
29b0f896 10433
40fb9820 10434 if (i.types[n].bitfield.imm32s
a7d61044 10435 && (i.suffix == QWORD_MNEM_SUFFIX
c032bc4f
JB
10436 || (!i.suffix && i.tm.opcode_modifier.no_lsuf)
10437 || dot_insn ()))
29b0f896 10438 sign = 1;
e205caa7
L
10439 else
10440 sign = 0;
520dc8e8 10441
29b0f896 10442 p = frag_more (size);
d258b828 10443 reloc_type = reloc (size, 0, sign, i.reloc[n]);
f86103b7 10444
2bbd9c25
JJ
10445 /* This is tough to explain. We end up with this one if we
10446 * have operands that look like
10447 * "_GLOBAL_OFFSET_TABLE_+[.-.L284]". The goal here is to
10448 * obtain the absolute address of the GOT, and it is strongly
10449 * preferable from a performance point of view to avoid using
10450 * a runtime relocation for this. The actual sequence of
10451 * instructions often look something like:
10452 *
10453 * call .L66
10454 * .L66:
10455 * popl %ebx
10456 * addl $_GLOBAL_OFFSET_TABLE_+[.-.L66],%ebx
10457 *
10458 * The call and pop essentially return the absolute address
10459 * of the label .L66 and store it in %ebx. The linker itself
10460 * will ultimately change the first operand of the addl so
10461 * that %ebx points to the GOT, but to keep things simple, the
10462 * .o file must have this operand set so that it generates not
10463 * the absolute address of .L66, but the absolute address of
10464 * itself. This allows the linker itself simply treat a GOTPC
10465 * relocation as asking for a pcrel offset to the GOT to be
10466 * added in, and the addend of the relocation is stored in the
10467 * operand field for the instruction itself.
10468 *
10469 * Our job here is to fix the operand so that it would add
10470 * the correct offset so that %ebx would point to itself. The
10471 * thing that is tricky is that .-.L66 will point to the
10472 * beginning of the instruction, so we need to further modify
10473 * the operand so that it will point to itself. There are
10474 * other cases where you have something like:
10475 *
10476 * .long $_GLOBAL_OFFSET_TABLE_+[.-.L66]
10477 *
10478 * and here no correction would be required. Internally in
10479 * the assembler we treat operands of this form as not being
10480 * pcrel since the '.' is explicitly mentioned, and I wonder
10481 * whether it would simplify matters to do it this way. Who
10482 * knows. In earlier versions of the PIC patches, the
10483 * pcrel_adjust field was used to store the correction, but
10484 * since the expression is not pcrel, I felt it would be
10485 * confusing to do it this way. */
10486
d6ab8113 10487 if ((reloc_type == BFD_RELOC_32
7b81dfbb
AJ
10488 || reloc_type == BFD_RELOC_X86_64_32S
10489 || reloc_type == BFD_RELOC_64)
29b0f896
AM
10490 && GOT_symbol
10491 && GOT_symbol == i.op[n].imms->X_add_symbol
10492 && (i.op[n].imms->X_op == O_symbol
10493 || (i.op[n].imms->X_op == O_add
10494 && ((symbol_get_value_expression
10495 (i.op[n].imms->X_op_symbol)->X_op)
10496 == O_subtract))))
10497 {
4fa24527 10498 if (!object_64bit)
d6ab8113 10499 reloc_type = BFD_RELOC_386_GOTPC;
7b81dfbb 10500 else if (size == 4)
d6ab8113 10501 reloc_type = BFD_RELOC_X86_64_GOTPC32;
7b81dfbb
AJ
10502 else if (size == 8)
10503 reloc_type = BFD_RELOC_X86_64_GOTPC64;
5b7c81bd 10504 i.has_gotpc_tls_reloc = true;
d583596c
JB
10505 i.op[n].imms->X_add_number +=
10506 encoding_length (insn_start_frag, insn_start_off, p);
29b0f896 10507 }
29b0f896
AM
10508 fix_new_exp (frag_now, p - frag_now->fr_literal, size,
10509 i.op[n].imms, 0, reloc_type);
10510 }
10511 }
10512 }
252b5132
RH
10513}
10514\f
d182319b
JB
10515/* x86_cons_fix_new is called via the expression parsing code when a
10516 reloc is needed. We use this hook to get the correct .got reloc. */
d182319b
JB
10517static int cons_sign = -1;
10518
10519void
e3bb37b5 10520x86_cons_fix_new (fragS *frag, unsigned int off, unsigned int len,
62ebcb5c 10521 expressionS *exp, bfd_reloc_code_real_type r)
d182319b 10522{
d258b828 10523 r = reloc (len, 0, cons_sign, r);
d182319b
JB
10524
10525#ifdef TE_PE
10526 if (exp->X_op == O_secrel)
10527 {
10528 exp->X_op = O_symbol;
10529 r = BFD_RELOC_32_SECREL;
10530 }
145667f8
MH
10531 else if (exp->X_op == O_secidx)
10532 r = BFD_RELOC_16_SECIDX;
d182319b
JB
10533#endif
10534
10535 fix_new_exp (frag, off, len, exp, 0, r);
10536}
10537
357d1bd8
L
10538/* Export the ABI address size for use by TC_ADDRESS_BYTES for the
10539 purpose of the `.dc.a' internal pseudo-op. */
10540
10541int
10542x86_address_bytes (void)
10543{
10544 if ((stdoutput->arch_info->mach & bfd_mach_x64_32))
10545 return 4;
10546 return stdoutput->arch_info->bits_per_address / 8;
10547}
10548
deea4973
JB
10549#if (!(defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) || defined (OBJ_MACH_O)) \
10550 || defined (LEX_AT)) && !defined (TE_PE)
d258b828 10551# define lex_got(reloc, adjust, types) NULL
718ddfc0 10552#else
f3c180ae
AM
10553/* Parse operands of the form
10554 <symbol>@GOTOFF+<nnn>
10555 and similar .plt or .got references.
10556
10557 If we find one, set up the correct relocation in RELOC and copy the
10558 input string, minus the `@GOTOFF' into a malloc'd buffer for
10559 parsing by the calling routine. Return this buffer, and if ADJUST
10560 is non-null set it to the length of the string we removed from the
10561 input line. Otherwise return NULL. */
10562static char *
91d6fa6a 10563lex_got (enum bfd_reloc_code_real *rel,
64e74474 10564 int *adjust,
d258b828 10565 i386_operand_type *types)
f3c180ae 10566{
7b81dfbb
AJ
10567 /* Some of the relocations depend on the size of what field is to
10568 be relocated. But in our callers i386_immediate and i386_displacement
10569 we don't yet know the operand size (this will be set by insn
10570 matching). Hence we record the word32 relocation here,
10571 and adjust the reloc according to the real size in reloc(). */
145667f8
MH
10572 static const struct
10573 {
f3c180ae 10574 const char *str;
cff8d58a 10575 int len;
4fa24527 10576 const enum bfd_reloc_code_real rel[2];
40fb9820 10577 const i386_operand_type types64;
5b7c81bd 10578 bool need_GOT_symbol;
145667f8
MH
10579 }
10580 gotrel[] =
10581 {
05909f23
JB
10582
10583#define OPERAND_TYPE_IMM32_32S_DISP32 { .bitfield = \
10584 { .imm32 = 1, .imm32s = 1, .disp32 = 1 } }
10585#define OPERAND_TYPE_IMM32_32S_64_DISP32 { .bitfield = \
10586 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1 } }
10587#define OPERAND_TYPE_IMM32_32S_64_DISP32_64 { .bitfield = \
10588 { .imm32 = 1, .imm32s = 1, .imm64 = 1, .disp32 = 1, .disp64 = 1 } }
10589#define OPERAND_TYPE_IMM64_DISP64 { .bitfield = \
10590 { .imm64 = 1, .disp64 = 1 } }
10591
deea4973 10592#ifndef TE_PE
8ce3d284 10593#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8fd4256d
L
10594 { STRING_COMMA_LEN ("SIZE"), { BFD_RELOC_SIZE32,
10595 BFD_RELOC_SIZE32 },
05909f23 10596 { .bitfield = { .imm32 = 1, .imm64 = 1 } }, false },
8ce3d284 10597#endif
cff8d58a
L
10598 { STRING_COMMA_LEN ("PLTOFF"), { _dummy_first_bfd_reloc_code_real,
10599 BFD_RELOC_X86_64_PLTOFF64 },
05909f23 10600 { .bitfield = { .imm64 = 1 } }, true },
cff8d58a
L
10601 { STRING_COMMA_LEN ("PLT"), { BFD_RELOC_386_PLT32,
10602 BFD_RELOC_X86_64_PLT32 },
a775efc8 10603 OPERAND_TYPE_IMM32_32S_DISP32, false },
cff8d58a
L
10604 { STRING_COMMA_LEN ("GOTPLT"), { _dummy_first_bfd_reloc_code_real,
10605 BFD_RELOC_X86_64_GOTPLT64 },
5b7c81bd 10606 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10607 { STRING_COMMA_LEN ("GOTOFF"), { BFD_RELOC_386_GOTOFF,
10608 BFD_RELOC_X86_64_GOTOFF64 },
5b7c81bd 10609 OPERAND_TYPE_IMM64_DISP64, true },
cff8d58a
L
10610 { STRING_COMMA_LEN ("GOTPCREL"), { _dummy_first_bfd_reloc_code_real,
10611 BFD_RELOC_X86_64_GOTPCREL },
a775efc8 10612 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10613 { STRING_COMMA_LEN ("TLSGD"), { BFD_RELOC_386_TLS_GD,
10614 BFD_RELOC_X86_64_TLSGD },
a775efc8 10615 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10616 { STRING_COMMA_LEN ("TLSLDM"), { BFD_RELOC_386_TLS_LDM,
10617 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10618 OPERAND_TYPE_NONE, true },
cff8d58a
L
10619 { STRING_COMMA_LEN ("TLSLD"), { _dummy_first_bfd_reloc_code_real,
10620 BFD_RELOC_X86_64_TLSLD },
a775efc8 10621 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10622 { STRING_COMMA_LEN ("GOTTPOFF"), { BFD_RELOC_386_TLS_IE_32,
10623 BFD_RELOC_X86_64_GOTTPOFF },
a775efc8 10624 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10625 { STRING_COMMA_LEN ("TPOFF"), { BFD_RELOC_386_TLS_LE_32,
10626 BFD_RELOC_X86_64_TPOFF32 },
a775efc8 10627 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10628 { STRING_COMMA_LEN ("NTPOFF"), { BFD_RELOC_386_TLS_LE,
10629 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10630 OPERAND_TYPE_NONE, true },
cff8d58a
L
10631 { STRING_COMMA_LEN ("DTPOFF"), { BFD_RELOC_386_TLS_LDO_32,
10632 BFD_RELOC_X86_64_DTPOFF32 },
a775efc8 10633 OPERAND_TYPE_IMM32_32S_64_DISP32_64, true },
cff8d58a
L
10634 { STRING_COMMA_LEN ("GOTNTPOFF"),{ BFD_RELOC_386_TLS_GOTIE,
10635 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10636 OPERAND_TYPE_NONE, true },
cff8d58a
L
10637 { STRING_COMMA_LEN ("INDNTPOFF"),{ BFD_RELOC_386_TLS_IE,
10638 _dummy_first_bfd_reloc_code_real },
5b7c81bd 10639 OPERAND_TYPE_NONE, true },
cff8d58a
L
10640 { STRING_COMMA_LEN ("GOT"), { BFD_RELOC_386_GOT32,
10641 BFD_RELOC_X86_64_GOT32 },
a775efc8 10642 OPERAND_TYPE_IMM32_32S_64_DISP32, true },
cff8d58a
L
10643 { STRING_COMMA_LEN ("TLSDESC"), { BFD_RELOC_386_TLS_GOTDESC,
10644 BFD_RELOC_X86_64_GOTPC32_TLSDESC },
a775efc8 10645 OPERAND_TYPE_IMM32_32S_DISP32, true },
cff8d58a
L
10646 { STRING_COMMA_LEN ("TLSCALL"), { BFD_RELOC_386_TLS_DESC_CALL,
10647 BFD_RELOC_X86_64_TLSDESC_CALL },
a775efc8 10648 OPERAND_TYPE_IMM32_32S_DISP32, true },
deea4973
JB
10649#else /* TE_PE */
10650 { STRING_COMMA_LEN ("SECREL32"), { BFD_RELOC_32_SECREL,
10651 BFD_RELOC_32_SECREL },
a775efc8 10652 OPERAND_TYPE_IMM32_32S_64_DISP32_64, false },
deea4973 10653#endif
05909f23
JB
10654
10655#undef OPERAND_TYPE_IMM32_32S_DISP32
10656#undef OPERAND_TYPE_IMM32_32S_64_DISP32
10657#undef OPERAND_TYPE_IMM32_32S_64_DISP32_64
10658#undef OPERAND_TYPE_IMM64_DISP64
10659
f3c180ae
AM
10660 };
10661 char *cp;
10662 unsigned int j;
10663
deea4973 10664#if defined (OBJ_MAYBE_ELF) && !defined (TE_PE)
718ddfc0
JB
10665 if (!IS_ELF)
10666 return NULL;
d382c579 10667#endif
718ddfc0 10668
f3c180ae 10669 for (cp = input_line_pointer; *cp != '@'; cp++)
67c11a9b 10670 if (is_end_of_line[(unsigned char) *cp] || *cp == ',')
f3c180ae
AM
10671 return NULL;
10672
47465058 10673 for (j = 0; j < ARRAY_SIZE (gotrel); j++)
f3c180ae 10674 {
cff8d58a 10675 int len = gotrel[j].len;
28f81592 10676 if (strncasecmp (cp + 1, gotrel[j].str, len) == 0)
f3c180ae 10677 {
4fa24527 10678 if (gotrel[j].rel[object_64bit] != 0)
f3c180ae 10679 {
28f81592
AM
10680 int first, second;
10681 char *tmpbuf, *past_reloc;
f3c180ae 10682
91d6fa6a 10683 *rel = gotrel[j].rel[object_64bit];
f3c180ae 10684
3956db08
JB
10685 if (types)
10686 {
10687 if (flag_code != CODE_64BIT)
40fb9820
L
10688 {
10689 types->bitfield.imm32 = 1;
10690 types->bitfield.disp32 = 1;
10691 }
3956db08
JB
10692 else
10693 *types = gotrel[j].types64;
10694 }
10695
844bf810 10696 if (gotrel[j].need_GOT_symbol && GOT_symbol == NULL)
f3c180ae
AM
10697 GOT_symbol = symbol_find_or_make (GLOBAL_OFFSET_TABLE_NAME);
10698
28f81592 10699 /* The length of the first part of our input line. */
f3c180ae 10700 first = cp - input_line_pointer;
28f81592
AM
10701
10702 /* The second part goes from after the reloc token until
67c11a9b 10703 (and including) an end_of_line char or comma. */
28f81592 10704 past_reloc = cp + 1 + len;
67c11a9b
AM
10705 cp = past_reloc;
10706 while (!is_end_of_line[(unsigned char) *cp] && *cp != ',')
10707 ++cp;
10708 second = cp + 1 - past_reloc;
28f81592
AM
10709
10710 /* Allocate and copy string. The trailing NUL shouldn't
10711 be necessary, but be safe. */
add39d23 10712 tmpbuf = XNEWVEC (char, first + second + 2);
f3c180ae 10713 memcpy (tmpbuf, input_line_pointer, first);
0787a12d
AM
10714 if (second != 0 && *past_reloc != ' ')
10715 /* Replace the relocation token with ' ', so that
10716 errors like foo@GOTOFF1 will be detected. */
10717 tmpbuf[first++] = ' ';
af89796a
L
10718 else
10719 /* Increment length by 1 if the relocation token is
10720 removed. */
10721 len++;
10722 if (adjust)
10723 *adjust = len;
0787a12d
AM
10724 memcpy (tmpbuf + first, past_reloc, second);
10725 tmpbuf[first + second] = '\0';
f3c180ae
AM
10726 return tmpbuf;
10727 }
10728
4fa24527
JB
10729 as_bad (_("@%s reloc is not supported with %d-bit output format"),
10730 gotrel[j].str, 1 << (5 + object_64bit));
f3c180ae
AM
10731 return NULL;
10732 }
10733 }
10734
10735 /* Might be a symbol version string. Don't as_bad here. */
10736 return NULL;
10737}
4e4f7c87 10738#endif
f3c180ae 10739
62ebcb5c 10740bfd_reloc_code_real_type
e3bb37b5 10741x86_cons (expressionS *exp, int size)
f3c180ae 10742{
62ebcb5c
AM
10743 bfd_reloc_code_real_type got_reloc = NO_RELOC;
10744
6b50f5f4
JB
10745 intel_syntax = -intel_syntax;
10746 exp->X_md = 0;
5cc00775 10747 expr_mode = expr_operator_none;
6b50f5f4 10748
2748c1b1
L
10749#if ((defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)) \
10750 && !defined (LEX_AT)) \
10751 || defined (TE_PE)
4fa24527 10752 if (size == 4 || (object_64bit && size == 8))
f3c180ae
AM
10753 {
10754 /* Handle @GOTOFF and the like in an expression. */
10755 char *save;
10756 char *gotfree_input_line;
4a57f2cf 10757 int adjust = 0;
f3c180ae
AM
10758
10759 save = input_line_pointer;
d258b828 10760 gotfree_input_line = lex_got (&got_reloc, &adjust, NULL);
f3c180ae
AM
10761 if (gotfree_input_line)
10762 input_line_pointer = gotfree_input_line;
10763
10764 expression (exp);
10765
10766 if (gotfree_input_line)
10767 {
10768 /* expression () has merrily parsed up to the end of line,
10769 or a comma - in the wrong buffer. Transfer how far
10770 input_line_pointer has moved to the right buffer. */
10771 input_line_pointer = (save
10772 + (input_line_pointer - gotfree_input_line)
10773 + adjust);
10774 free (gotfree_input_line);
3992d3b7
AM
10775 if (exp->X_op == O_constant
10776 || exp->X_op == O_absent
10777 || exp->X_op == O_illegal
0398aac5 10778 || exp->X_op == O_register
3992d3b7
AM
10779 || exp->X_op == O_big)
10780 {
10781 char c = *input_line_pointer;
10782 *input_line_pointer = 0;
10783 as_bad (_("missing or invalid expression `%s'"), save);
10784 *input_line_pointer = c;
10785 }
b9519cfe
L
10786 else if ((got_reloc == BFD_RELOC_386_PLT32
10787 || got_reloc == BFD_RELOC_X86_64_PLT32)
10788 && exp->X_op != O_symbol)
10789 {
10790 char c = *input_line_pointer;
10791 *input_line_pointer = 0;
10792 as_bad (_("invalid PLT expression `%s'"), save);
10793 *input_line_pointer = c;
10794 }
f3c180ae
AM
10795 }
10796 }
10797 else
6b50f5f4 10798#endif
f3c180ae 10799 expression (exp);
ee86248c
JB
10800
10801 intel_syntax = -intel_syntax;
10802
10803 if (intel_syntax)
10804 i386_intel_simplify (exp);
62ebcb5c 10805
a442cac5 10806 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
5cc00775
JB
10807 if (size <= 4 && expr_mode == expr_operator_present
10808 && exp->X_op == O_constant && !object_64bit)
a442cac5
JB
10809 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
10810
62ebcb5c 10811 return got_reloc;
f3c180ae 10812}
f3c180ae 10813
9f32dd5b
L
10814static void
10815signed_cons (int size)
6482c264 10816{
a442cac5 10817 if (object_64bit)
d182319b
JB
10818 cons_sign = 1;
10819 cons (size);
10820 cons_sign = -1;
6482c264
NC
10821}
10822
edd67638
JB
10823static void
10824s_insn (int dummy ATTRIBUTE_UNUSED)
10825{
393fbe8d 10826 char mnemonic[MAX_MNEM_SIZE], *line = input_line_pointer, *ptr;
edd67638
JB
10827 char *saved_ilp = find_end_of_line (line, false), saved_char;
10828 const char *end;
10829 unsigned int j;
10830 valueT val;
10831 bool vex = false, xop = false, evex = false;
10832 static const templates tt = { &i.tm, &i.tm + 1 };
10833
10834 init_globals ();
10835
10836 saved_char = *saved_ilp;
10837 *saved_ilp = 0;
10838
10839 end = parse_insn (line, mnemonic, true);
10840 if (end == NULL)
10841 {
10842 bad:
10843 *saved_ilp = saved_char;
10844 ignore_rest_of_line ();
d0c2e3ec 10845 i.tm.mnem_off = 0;
edd67638
JB
10846 return;
10847 }
10848 line += end - line;
10849
10850 current_templates = &tt;
10851 i.tm.mnem_off = MN__insn;
393fbe8d 10852 i.tm.extension_opcode = None;
edd67638
JB
10853
10854 if (startswith (line, "VEX")
10855 && (line[3] == '.' || is_space_char (line[3])))
10856 {
10857 vex = true;
10858 line += 3;
10859 }
10860 else if (startswith (line, "XOP") && ISDIGIT (line[3]))
10861 {
10862 char *e;
10863 unsigned long n = strtoul (line + 3, &e, 16);
10864
10865 if (e == line + 5 && n >= 0x08 && n <= 0x1f
10866 && (*e == '.' || is_space_char (*e)))
10867 {
10868 xop = true;
d0c2e3ec
JB
10869 /* Arrange for build_vex_prefix() to emit 0x8f. */
10870 i.tm.opcode_space = SPACE_XOP08;
10871 i.insn_opcode_space = n;
edd67638
JB
10872 line = e;
10873 }
10874 }
10875 else if (startswith (line, "EVEX")
10876 && (line[4] == '.' || is_space_char (line[4])))
10877 {
10878 evex = true;
10879 line += 4;
10880 }
10881
10882 if (vex || xop
10883 ? i.vec_encoding == vex_encoding_evex
10884 : evex
10885 ? i.vec_encoding == vex_encoding_vex
10886 || i.vec_encoding == vex_encoding_vex3
10887 : i.vec_encoding != vex_encoding_default)
10888 {
10889 as_bad (_("pseudo-prefix conflicts with encoding specifier"));
10890 goto bad;
10891 }
10892
0ff3b7d0
JB
10893 if (line > end && i.vec_encoding == vex_encoding_default)
10894 i.vec_encoding = evex ? vex_encoding_evex : vex_encoding_vex;
10895
1adecddd
JB
10896 if (i.vec_encoding != vex_encoding_default)
10897 {
10898 /* Only address size and segment override prefixes are permitted with
10899 VEX/XOP/EVEX encodings. */
10900 const unsigned char *p = i.prefix;
10901
10902 for (j = 0; j < ARRAY_SIZE (i.prefix); ++j, ++p)
10903 {
10904 if (!*p)
10905 continue;
10906
10907 switch (j)
10908 {
10909 case SEG_PREFIX:
10910 case ADDR_PREFIX:
10911 break;
10912 default:
10913 as_bad (_("illegal prefix used with VEX/XOP/EVEX"));
10914 goto bad;
10915 }
10916 }
10917 }
10918
edd67638
JB
10919 if (line > end && *line == '.')
10920 {
d0c2e3ec
JB
10921 /* Length specifier (VEX.L, XOP.L, EVEX.L'L). */
10922 switch (line[1])
10923 {
10924 case 'L':
10925 switch (line[2])
10926 {
10927 case '0':
10928 if (evex)
10929 i.tm.opcode_modifier.evex = EVEX128;
10930 else
10931 i.tm.opcode_modifier.vex = VEX128;
10932 break;
10933
10934 case '1':
10935 if (evex)
10936 i.tm.opcode_modifier.evex = EVEX256;
10937 else
10938 i.tm.opcode_modifier.vex = VEX256;
10939 break;
10940
10941 case '2':
10942 if (evex)
10943 i.tm.opcode_modifier.evex = EVEX512;
10944 break;
10945
10946 case '3':
10947 if (evex)
10948 i.tm.opcode_modifier.evex = EVEX_L3;
10949 break;
10950
10951 case 'I':
10952 if (line[3] == 'G')
10953 {
10954 if (evex)
10955 i.tm.opcode_modifier.evex = EVEXLIG;
10956 else
10957 i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
10958 ++line;
10959 }
10960 break;
10961 }
10962
10963 if (i.tm.opcode_modifier.vex || i.tm.opcode_modifier.evex)
10964 line += 3;
10965 break;
10966
10967 case '1':
10968 if (line[2] == '2' && line[3] == '8')
10969 {
10970 if (evex)
10971 i.tm.opcode_modifier.evex = EVEX128;
10972 else
10973 i.tm.opcode_modifier.vex = VEX128;
10974 line += 4;
10975 }
10976 break;
10977
10978 case '2':
10979 if (line[2] == '5' && line[3] == '6')
10980 {
10981 if (evex)
10982 i.tm.opcode_modifier.evex = EVEX256;
10983 else
10984 i.tm.opcode_modifier.vex = VEX256;
10985 line += 4;
10986 }
10987 break;
10988
10989 case '5':
10990 if (evex && line[2] == '1' && line[3] == '2')
10991 {
10992 i.tm.opcode_modifier.evex = EVEX512;
10993 line += 4;
10994 }
10995 break;
10996 }
10997 }
10998
10999 if (line > end && *line == '.')
11000 {
11001 /* embedded prefix (VEX.pp, XOP.pp, EVEX.pp). */
11002 switch (line[1])
11003 {
11004 case 'N':
11005 if (line[2] == 'P')
11006 line += 3;
11007 break;
11008
11009 case '6':
11010 if (line[2] == '6')
11011 {
11012 i.tm.opcode_modifier.opcodeprefix = PREFIX_0X66;
11013 line += 3;
11014 }
11015 break;
11016
11017 case 'F': case 'f':
11018 if (line[2] == '3')
11019 {
11020 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF3;
11021 line += 3;
11022 }
11023 else if (line[2] == '2')
11024 {
11025 i.tm.opcode_modifier.opcodeprefix = PREFIX_0XF2;
11026 line += 3;
11027 }
11028 break;
11029 }
11030 }
11031
11032 if (line > end && !xop && *line == '.')
11033 {
11034 /* Encoding space (VEX.mmmmm, EVEX.mmmm). */
11035 switch (line[1])
11036 {
11037 case '0':
11038 if (TOUPPER (line[2]) != 'F')
11039 break;
11040 if (line[3] == '.' || is_space_char (line[3]))
11041 {
11042 i.insn_opcode_space = SPACE_0F;
11043 line += 3;
11044 }
11045 else if (line[3] == '3'
11046 && (line[4] == '8' || TOUPPER (line[4]) == 'A')
11047 && (line[5] == '.' || is_space_char (line[5])))
11048 {
11049 i.insn_opcode_space = line[4] == '8' ? SPACE_0F38 : SPACE_0F3A;
11050 line += 5;
11051 }
11052 break;
11053
11054 case 'M':
11055 if (ISDIGIT (line[2]) && line[2] != '0')
11056 {
11057 char *e;
11058 unsigned long n = strtoul (line + 2, &e, 10);
11059
11060 if (n <= (evex ? 15 : 31)
11061 && (*e == '.' || is_space_char (*e)))
11062 {
11063 i.insn_opcode_space = n;
11064 line = e;
11065 }
11066 }
11067 break;
11068 }
11069 }
11070
11071 if (line > end && *line == '.' && line[1] == 'W')
11072 {
11073 /* VEX.W, XOP.W, EVEX.W */
11074 switch (line[2])
11075 {
11076 case '0':
11077 i.tm.opcode_modifier.vexw = VEXW0;
11078 break;
11079
11080 case '1':
11081 i.tm.opcode_modifier.vexw = VEXW1;
11082 break;
11083
11084 case 'I':
11085 if (line[3] == 'G')
11086 {
11087 i.tm.opcode_modifier.vexw = VEXWIG;
11088 ++line;
11089 }
11090 break;
11091 }
11092
11093 if (i.tm.opcode_modifier.vexw)
11094 line += 3;
11095 }
11096
11097 if (line > end && *line && !is_space_char (*line))
11098 {
11099 /* Improve diagnostic a little. */
11100 if (*line == '.' && line[1] && !is_space_char (line[1]))
11101 ++line;
11102 goto done;
edd67638
JB
11103 }
11104
393fbe8d
JB
11105 /* Before processing the opcode expression, find trailing "+r" or
11106 "/<digit>" specifiers. */
11107 for (ptr = line; ; ++ptr)
11108 {
11109 unsigned long n;
11110 char *e;
11111
11112 ptr = strpbrk (ptr, "+/,");
11113 if (ptr == NULL || *ptr == ',')
11114 break;
11115
11116 if (*ptr == '+' && ptr[1] == 'r'
11117 && (ptr[2] == ',' || (is_space_char (ptr[2]) && ptr[3] == ',')))
11118 {
11119 *ptr = ' ';
11120 ptr[1] = ' ';
11121 i.short_form = true;
11122 break;
11123 }
11124
11125 if (*ptr == '/' && ISDIGIT (ptr[1])
11126 && (n = strtoul (ptr + 1, &e, 8)) < 8
11127 && e == ptr + 2
11128 && (ptr[2] == ',' || (is_space_char (ptr[2]) && ptr[3] == ',')))
11129 {
11130 *ptr = ' ';
11131 ptr[1] = ' ';
11132 i.tm.extension_opcode = n;
11133 i.tm.opcode_modifier.modrm = 1;
11134 break;
11135 }
11136 }
11137
edd67638
JB
11138 input_line_pointer = line;
11139 val = get_absolute_expression ();
11140 line = input_line_pointer;
11141
393fbe8d
JB
11142 if (i.short_form && (val & 7))
11143 as_warn ("`+r' assumes low three opcode bits to be clear");
11144
edd67638
JB
11145 for (j = 1; j < sizeof(val); ++j)
11146 if (!(val >> (j * 8)))
11147 break;
11148
11149 /* Trim off a prefix if present. */
11150 if (j > 1 && !vex && !xop && !evex)
11151 {
11152 uint8_t byte = val >> ((j - 1) * 8);
11153
11154 switch (byte)
11155 {
11156 case DATA_PREFIX_OPCODE:
11157 case REPE_PREFIX_OPCODE:
11158 case REPNE_PREFIX_OPCODE:
11159 if (!add_prefix (byte))
11160 goto bad;
11161 val &= ((uint64_t)1 << (--j * 8)) - 1;
11162 break;
11163 }
11164 }
11165
11166 /* Trim off encoding space. */
d0c2e3ec 11167 if (j > 1 && !i.insn_opcode_space && (val >> ((j - 1) * 8)) == 0x0f)
edd67638
JB
11168 {
11169 uint8_t byte = val >> ((--j - 1) * 8);
11170
d0c2e3ec 11171 i.insn_opcode_space = SPACE_0F;
edd67638
JB
11172 switch (byte & -(j > 1))
11173 {
11174 case 0x38:
d0c2e3ec 11175 i.insn_opcode_space = SPACE_0F38;
edd67638
JB
11176 --j;
11177 break;
11178 case 0x3a:
d0c2e3ec 11179 i.insn_opcode_space = SPACE_0F3A;
edd67638
JB
11180 --j;
11181 break;
11182 }
d0c2e3ec 11183 i.tm.opcode_space = i.insn_opcode_space;
edd67638
JB
11184 val &= ((uint64_t)1 << (j * 8)) - 1;
11185 }
d0c2e3ec
JB
11186 if (!i.tm.opcode_space && (vex || evex))
11187 /* Arrange for build_vex_prefix() to properly emit 0xC4/0xC5.
11188 Also avoid hitting abort() there or in build_evex_prefix(). */
11189 i.tm.opcode_space = i.insn_opcode_space == SPACE_0F ? SPACE_0F
11190 : SPACE_0F38;
edd67638
JB
11191
11192 if (j > 2)
11193 {
11194 as_bad (_("opcode residual (%#"PRIx64") too wide"), (uint64_t) val);
11195 goto bad;
11196 }
11197 i.opcode_length = j;
0ff3b7d0
JB
11198
11199 /* Handle operands, if any. */
11200 if (*line == ',')
11201 {
11202 i386_operand_type combined;
f7377a91 11203 expressionS *disp_exp = NULL;
0ff3b7d0
JB
11204 bool changed;
11205
f7377a91
JB
11206 i.memshift = -1;
11207
0ff3b7d0
JB
11208 ptr = parse_operands (line + 1, &i386_mnemonics[MN__insn]);
11209 this_operand = -1;
11210 if (!ptr)
11211 goto bad;
11212 line = ptr;
11213
11214 if (!i.operands)
11215 {
11216 as_bad (_("expecting operand after ','; got nothing"));
11217 goto done;
11218 }
11219
11220 if (i.mem_operands > 1)
11221 {
11222 as_bad (_("too many memory references for `%s'"),
11223 &i386_mnemonics[MN__insn]);
11224 goto done;
11225 }
11226
11227 /* Are we to emit ModR/M encoding? */
11228 if (!i.short_form
11229 && (i.mem_operands
11230 || i.reg_operands > (i.vec_encoding != vex_encoding_default)
11231 || i.tm.extension_opcode != None))
11232 i.tm.opcode_modifier.modrm = 1;
11233
11234 if (!i.tm.opcode_modifier.modrm
11235 && (i.reg_operands
11236 > i.short_form + 0U + (i.vec_encoding != vex_encoding_default)
11237 || i.mem_operands))
11238 {
11239 as_bad (_("too many register/memory operands"));
11240 goto done;
11241 }
11242
11243 /* Enforce certain constraints on operands. */
11244 switch (i.reg_operands + i.mem_operands
11245 + (i.tm.extension_opcode != None))
11246 {
11247 case 0:
11248 if (i.short_form)
11249 {
11250 as_bad (_("too few register/memory operands"));
11251 goto done;
11252 }
11253 /* Fall through. */
11254 case 1:
11255 if (i.tm.opcode_modifier.modrm)
11256 {
11257 as_bad (_("too few register/memory operands"));
11258 goto done;
11259 }
11260 break;
11261
11262 case 2:
11263 break;
11264
11265 case 4:
11266 if (i.imm_operands
11267 && (i.op[0].imms->X_op != O_constant
11268 || !fits_in_imm4 (i.op[0].imms->X_add_number)))
11269 {
11270 as_bad (_("constant doesn't fit in %d bits"), evex ? 3 : 4);
11271 goto done;
11272 }
11273 /* Fall through. */
11274 case 3:
11275 if (i.vec_encoding != vex_encoding_default)
11276 {
11277 i.tm.opcode_modifier.vexvvvv = 1;
11278 break;
11279 }
11280 /* Fall through. */
11281 default:
11282 as_bad (_("too many register/memory operands"));
11283 goto done;
11284 }
11285
11286 /* Bring operands into canonical order (imm, mem, reg). */
11287 do
11288 {
11289 changed = false;
11290
11291 for (j = 1; j < i.operands; ++j)
11292 {
11293 if ((!operand_type_check (i.types[j - 1], imm)
11294 && operand_type_check (i.types[j], imm))
11295 || (i.types[j - 1].bitfield.class != ClassNone
11296 && i.types[j].bitfield.class == ClassNone))
11297 {
11298 swap_2_operands (j - 1, j);
11299 changed = true;
11300 }
11301 }
11302 }
11303 while (changed);
11304
11305 /* For Intel syntax swap the order of register operands. */
11306 if (intel_syntax)
11307 switch (i.reg_operands)
11308 {
11309 case 0:
11310 case 1:
11311 break;
11312
11313 case 4:
11314 swap_2_operands (i.imm_operands + i.mem_operands + 1, i.operands - 2);
11315 /* Fall through. */
11316 case 3:
11317 case 2:
11318 swap_2_operands (i.imm_operands + i.mem_operands, i.operands - 1);
11319 break;
11320
11321 default:
11322 abort ();
11323 }
11324
11325 /* Enforce constraints when using VSIB. */
11326 if (i.index_reg
11327 && (i.index_reg->reg_type.bitfield.xmmword
11328 || i.index_reg->reg_type.bitfield.ymmword
11329 || i.index_reg->reg_type.bitfield.zmmword))
11330 {
11331 if (i.vec_encoding == vex_encoding_default)
11332 {
11333 as_bad (_("VSIB unavailable with legacy encoding"));
11334 goto done;
11335 }
11336
11337 if (i.vec_encoding == vex_encoding_evex
11338 && i.reg_operands > 1)
11339 {
11340 /* We could allow two register operands, encoding the 2nd one in
11341 an 8-bit immediate like for 4-register-operand insns, but that
11342 would require ugly fiddling with process_operands() and/or
11343 build_modrm_byte(). */
11344 as_bad (_("too many register operands with VSIB"));
11345 goto done;
11346 }
11347
11348 i.tm.opcode_modifier.sib = 1;
11349 }
11350
11351 /* Establish operand size encoding. */
11352 operand_type_set (&combined, 0);
f7377a91 11353
0ff3b7d0
JB
11354 for (j = i.imm_operands; j < i.operands; ++j)
11355 {
11356 i.types[j].bitfield.instance = InstanceNone;
11357
11358 if (operand_type_check (i.types[j], disp))
f7377a91
JB
11359 {
11360 i.types[j].bitfield.baseindex = 1;
11361 disp_exp = i.op[j].disps;
11362 }
11363
11364 if (evex && i.types[j].bitfield.baseindex)
11365 {
11366 unsigned int n = i.memshift;
11367
11368 if (i.types[j].bitfield.byte)
11369 n = 0;
11370 else if (i.types[j].bitfield.word)
11371 n = 1;
11372 else if (i.types[j].bitfield.dword)
11373 n = 2;
11374 else if (i.types[j].bitfield.qword)
11375 n = 3;
11376 else if (i.types[j].bitfield.xmmword)
11377 n = 4;
11378 else if (i.types[j].bitfield.ymmword)
11379 n = 5;
11380 else if (i.types[j].bitfield.zmmword)
11381 n = 6;
11382
11383 if (i.memshift < 32 && n != i.memshift)
11384 as_warn ("conflicting memory operand size specifiers");
11385 i.memshift = n;
11386 }
0ff3b7d0
JB
11387
11388 if ((i.broadcast.type || i.broadcast.bytes)
11389 && j == i.broadcast.operand)
11390 continue;
11391
11392 combined = operand_type_or (combined, i.types[j]);
11393 combined.bitfield.class = ClassNone;
11394 }
11395
f7377a91
JB
11396 switch ((i.broadcast.type ? i.broadcast.type : 1)
11397 << (i.memshift < 32 ? i.memshift : 0))
11398 {
11399 case 64: combined.bitfield.zmmword = 1; break;
11400 case 32: combined.bitfield.ymmword = 1; break;
11401 case 16: combined.bitfield.xmmword = 1; break;
11402 case 8: combined.bitfield.qword = 1; break;
11403 case 4: combined.bitfield.dword = 1; break;
11404 }
11405
0ff3b7d0
JB
11406 if (i.vec_encoding == vex_encoding_default)
11407 {
11408 if (flag_code == CODE_64BIT && combined.bitfield.qword)
11409 i.rex |= REX_W;
11410 else if ((flag_code == CODE_16BIT ? combined.bitfield.dword
11411 : combined.bitfield.word)
11412 && !add_prefix (DATA_PREFIX_OPCODE))
11413 goto done;
11414 }
11415 else if (!i.tm.opcode_modifier.vexw)
11416 {
11417 if (flag_code == CODE_64BIT)
11418 {
11419 if (combined.bitfield.qword)
11420 i.tm.opcode_modifier.vexw = VEXW1;
11421 else if (combined.bitfield.dword)
11422 i.tm.opcode_modifier.vexw = VEXW0;
11423 }
11424
11425 if (!i.tm.opcode_modifier.vexw)
11426 i.tm.opcode_modifier.vexw = VEXWIG;
11427 }
11428
11429 if (vex || xop)
11430 {
11431 if (!i.tm.opcode_modifier.vex)
11432 {
11433 if (combined.bitfield.ymmword)
11434 i.tm.opcode_modifier.vex = VEX256;
11435 else if (combined.bitfield.xmmword)
11436 i.tm.opcode_modifier.vex = VEX128;
11437 }
11438 }
11439 else if (evex)
11440 {
11441 if (!i.tm.opcode_modifier.evex)
11442 {
11443 /* Do _not_ consider AVX512VL here. */
11444 if (i.rounding.type != rc_none || combined.bitfield.zmmword)
11445 i.tm.opcode_modifier.evex = EVEX512;
11446 else if (combined.bitfield.ymmword)
11447 i.tm.opcode_modifier.evex = EVEX256;
11448 else if (combined.bitfield.xmmword)
11449 i.tm.opcode_modifier.evex = EVEX128;
11450 }
f7377a91
JB
11451
11452 if (i.memshift >= 32)
11453 {
11454 unsigned int n = 0;
11455
11456 switch (i.tm.opcode_modifier.evex)
11457 {
11458 case EVEX512: n = 64; break;
11459 case EVEX256: n = 32; break;
11460 case EVEX128: n = 16; break;
11461 }
11462
11463 if (i.broadcast.type)
11464 n /= i.broadcast.type;
11465
11466 if (n > 0)
11467 for (i.memshift = 0; !(n & 1); n >>= 1)
11468 ++i.memshift;
11469 else if (disp_exp != NULL && disp_exp->X_op == O_constant
11470 && disp_exp->X_add_number != 0
11471 && i.disp_encoding != disp_encoding_32bit)
11472 {
11473 if (!quiet_warnings)
11474 as_warn ("cannot determine memory operand size");
11475 i.disp_encoding = disp_encoding_32bit;
11476 }
11477 }
0ff3b7d0
JB
11478 }
11479
f7377a91
JB
11480 if (i.memshift >= 32)
11481 i.memshift = 0;
11482 else if (!evex)
11483 i.vec_encoding = vex_encoding_error;
11484
0ff3b7d0
JB
11485 if (i.disp_operands && !optimize_disp (&i.tm))
11486 goto done;
11487
c032bc4f
JB
11488 /* Establish size for immediate operands. */
11489 for (j = 0; j < i.imm_operands; ++j)
11490 {
11491 expressionS *expP = i.op[j].imms;
11492
11493 gas_assert (operand_type_check (i.types[j], imm));
11494 operand_type_set (&i.types[j], 0);
11495
11496 if (i.imm_bits[j] > 32)
11497 i.types[j].bitfield.imm64 = 1;
11498 else if (i.imm_bits[j] > 16)
11499 {
11500 if (flag_code == CODE_64BIT && (i.flags[j] & Operand_Signed))
11501 i.types[j].bitfield.imm32s = 1;
11502 else
11503 i.types[j].bitfield.imm32 = 1;
11504 }
11505 else if (i.imm_bits[j] > 8)
11506 i.types[j].bitfield.imm16 = 1;
11507 else if (i.imm_bits[j] > 0)
11508 {
11509 if (i.flags[j] & Operand_Signed)
11510 i.types[j].bitfield.imm8s = 1;
11511 else
11512 i.types[j].bitfield.imm8 = 1;
11513 }
11514 else if (expP->X_op == O_constant)
11515 {
11516 i.types[j] = smallest_imm_type (expP->X_add_number);
11517 i.types[j].bitfield.imm1 = 0;
11518 /* Oddly enough imm_size() checks imm64 first, so the bit needs
11519 zapping since smallest_imm_type() sets it unconditionally. */
11520 if (flag_code != CODE_64BIT)
11521 {
11522 i.types[j].bitfield.imm64 = 0;
11523 i.types[j].bitfield.imm32s = 0;
11524 i.types[j].bitfield.imm32 = 1;
11525 }
11526 else if (i.types[j].bitfield.imm32 || i.types[j].bitfield.imm32s)
11527 i.types[j].bitfield.imm64 = 0;
11528 }
11529 else
11530 /* Non-constant expressions are sized heuristically. */
11531 switch (flag_code)
11532 {
11533 case CODE_64BIT: i.types[j].bitfield.imm32s = 1; break;
11534 case CODE_32BIT: i.types[j].bitfield.imm32 = 1; break;
11535 case CODE_16BIT: i.types[j].bitfield.imm16 = 1; break;
11536 }
11537 }
11538
0ff3b7d0
JB
11539 for (j = 0; j < i.operands; ++j)
11540 i.tm.operand_types[j] = i.types[j];
11541
11542 process_operands ();
11543 }
11544
11545 /* Don't set opcode until after processing operands, to avoid any
11546 potential special casing there. */
11547 i.tm.base_opcode |= val;
11548
11549 if (i.vec_encoding == vex_encoding_error
11550 || (i.vec_encoding != vex_encoding_evex
11551 ? i.broadcast.type || i.broadcast.bytes
11552 || i.rounding.type != rc_none
11553 || i.mask.reg
11554 : (i.broadcast.type || i.broadcast.bytes)
11555 && i.rounding.type != rc_none))
11556 {
11557 as_bad (_("conflicting .insn operands"));
11558 goto done;
11559 }
edd67638 11560
d0c2e3ec
JB
11561 if (vex || xop)
11562 {
11563 if (!i.tm.opcode_modifier.vex)
11564 i.tm.opcode_modifier.vex = VEXScalar; /* LIG */
11565
11566 build_vex_prefix (NULL);
11567 i.rex &= REX_OPCODE;
11568 }
11569 else if (evex)
11570 {
11571 if (!i.tm.opcode_modifier.evex)
11572 i.tm.opcode_modifier.evex = EVEXLIG;
11573
11574 build_evex_prefix ();
11575 i.rex &= REX_OPCODE;
11576 }
0ff3b7d0
JB
11577 else if (i.rex != 0)
11578 add_prefix (REX_OPCODE | i.rex);
d0c2e3ec 11579
edd67638
JB
11580 output_insn ();
11581
d0c2e3ec 11582 done:
edd67638
JB
11583 *saved_ilp = saved_char;
11584 input_line_pointer = line;
11585
11586 demand_empty_rest_of_line ();
d0c2e3ec
JB
11587
11588 /* Make sure dot_insn() won't yield "true" anymore. */
11589 i.tm.mnem_off = 0;
edd67638
JB
11590}
11591
d182319b 11592#ifdef TE_PE
6482c264 11593static void
7016a5d5 11594pe_directive_secrel (int dummy ATTRIBUTE_UNUSED)
6482c264
NC
11595{
11596 expressionS exp;
11597
11598 do
11599 {
11600 expression (&exp);
11601 if (exp.X_op == O_symbol)
11602 exp.X_op = O_secrel;
11603
11604 emit_expr (&exp, 4);
11605 }
11606 while (*input_line_pointer++ == ',');
11607
11608 input_line_pointer--;
11609 demand_empty_rest_of_line ();
11610}
145667f8
MH
11611
11612static void
11613pe_directive_secidx (int dummy ATTRIBUTE_UNUSED)
11614{
11615 expressionS exp;
11616
11617 do
11618 {
11619 expression (&exp);
11620 if (exp.X_op == O_symbol)
11621 exp.X_op = O_secidx;
11622
11623 emit_expr (&exp, 2);
11624 }
11625 while (*input_line_pointer++ == ',');
11626
11627 input_line_pointer--;
11628 demand_empty_rest_of_line ();
11629}
6482c264
NC
11630#endif
11631
7063667e
JB
11632/* Handle Rounding Control / SAE specifiers. */
11633
11634static char *
11635RC_SAE_specifier (const char *pstr)
11636{
11637 unsigned int j;
11638
11639 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); j++)
11640 {
11641 if (!strncmp (pstr, RC_NamesTable[j].name, RC_NamesTable[j].len))
11642 {
11643 if (i.rounding.type != rc_none)
11644 {
11645 as_bad (_("duplicated `{%s}'"), RC_NamesTable[j].name);
11646 return NULL;
11647 }
11648
11649 i.rounding.type = RC_NamesTable[j].type;
11650
11651 return (char *)(pstr + RC_NamesTable[j].len);
11652 }
11653 }
11654
11655 return NULL;
11656}
11657
43234a1e
L
11658/* Handle Vector operations. */
11659
11660static char *
f70c6814 11661check_VecOperations (char *op_string)
43234a1e
L
11662{
11663 const reg_entry *mask;
11664 const char *saved;
11665 char *end_op;
11666
f70c6814 11667 while (*op_string)
43234a1e
L
11668 {
11669 saved = op_string;
11670 if (*op_string == '{')
11671 {
11672 op_string++;
11673
11674 /* Check broadcasts. */
d34049e8 11675 if (startswith (op_string, "1to"))
43234a1e 11676 {
5273a3cd 11677 unsigned int bcst_type;
43234a1e 11678
5273a3cd 11679 if (i.broadcast.type)
43234a1e
L
11680 goto duplicated_vec_op;
11681
11682 op_string += 3;
11683 if (*op_string == '8')
8e6e0792 11684 bcst_type = 8;
b28d1bda 11685 else if (*op_string == '4')
8e6e0792 11686 bcst_type = 4;
b28d1bda 11687 else if (*op_string == '2')
8e6e0792 11688 bcst_type = 2;
43234a1e
L
11689 else if (*op_string == '1'
11690 && *(op_string+1) == '6')
11691 {
8e6e0792 11692 bcst_type = 16;
43234a1e
L
11693 op_string++;
11694 }
0cc78721
CL
11695 else if (*op_string == '3'
11696 && *(op_string+1) == '2')
11697 {
11698 bcst_type = 32;
11699 op_string++;
11700 }
43234a1e
L
11701 else
11702 {
11703 as_bad (_("Unsupported broadcast: `%s'"), saved);
11704 return NULL;
11705 }
11706 op_string++;
11707
5273a3cd
JB
11708 i.broadcast.type = bcst_type;
11709 i.broadcast.operand = this_operand;
f7377a91
JB
11710
11711 /* For .insn a data size specifier may be appended. */
11712 if (dot_insn () && *op_string == ':')
11713 goto dot_insn_modifier;
11714 }
11715 /* Check .insn special cases. */
11716 else if (dot_insn () && *op_string == ':')
11717 {
11718 dot_insn_modifier:
c032bc4f 11719 switch (op_string[1])
f7377a91
JB
11720 {
11721 unsigned long n;
11722
c032bc4f 11723 case 'd':
f7377a91
JB
11724 if (i.memshift < 32)
11725 goto duplicated_vec_op;
11726
11727 n = strtoul (op_string + 2, &end_op, 0);
11728 if (n)
11729 for (i.memshift = 0; !(n & 1); n >>= 1)
11730 ++i.memshift;
11731 if (i.memshift < 32 && n == 1)
11732 op_string = end_op;
c032bc4f
JB
11733 break;
11734
11735 case 's': case 'u':
11736 /* This isn't really a "vector" operation, but a sign/size
11737 specifier for immediate operands of .insn. Note that AT&T
11738 syntax handles the same in i386_immediate(). */
11739 if (!intel_syntax)
11740 break;
11741
11742 if (i.imm_bits[this_operand])
11743 goto duplicated_vec_op;
11744
11745 n = strtoul (op_string + 2, &end_op, 0);
11746 if (n && n <= (flag_code == CODE_64BIT ? 64 : 32))
11747 {
11748 i.imm_bits[this_operand] = n;
11749 if (op_string[1] == 's')
11750 i.flags[this_operand] |= Operand_Signed;
11751 op_string = end_op;
11752 }
11753 break;
f7377a91 11754 }
43234a1e
L
11755 }
11756 /* Check masking operation. */
11757 else if ((mask = parse_register (op_string, &end_op)) != NULL)
11758 {
8a6fb3f9
JB
11759 if (mask == &bad_reg)
11760 return NULL;
11761
43234a1e 11762 /* k0 can't be used for write mask. */
f74a6307 11763 if (mask->reg_type.bitfield.class != RegMask || !mask->reg_num)
43234a1e 11764 {
6d2cd6b2
JB
11765 as_bad (_("`%s%s' can't be used for write mask"),
11766 register_prefix, mask->reg_name);
43234a1e
L
11767 return NULL;
11768 }
11769
6225c532 11770 if (!i.mask.reg)
43234a1e 11771 {
6225c532
JB
11772 i.mask.reg = mask;
11773 i.mask.operand = this_operand;
43234a1e 11774 }
6225c532
JB
11775 else if (i.mask.reg->reg_num)
11776 goto duplicated_vec_op;
43234a1e
L
11777 else
11778 {
6225c532 11779 i.mask.reg = mask;
43234a1e
L
11780
11781 /* Only "{z}" is allowed here. No need to check
11782 zeroing mask explicitly. */
6225c532 11783 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
11784 {
11785 as_bad (_("invalid write mask `%s'"), saved);
11786 return NULL;
11787 }
11788 }
11789
11790 op_string = end_op;
11791 }
11792 /* Check zeroing-flag for masking operation. */
11793 else if (*op_string == 'z')
11794 {
6225c532 11795 if (!i.mask.reg)
43234a1e 11796 {
6225c532
JB
11797 i.mask.reg = reg_k0;
11798 i.mask.zeroing = 1;
11799 i.mask.operand = this_operand;
43234a1e
L
11800 }
11801 else
11802 {
6225c532 11803 if (i.mask.zeroing)
43234a1e
L
11804 {
11805 duplicated_vec_op:
11806 as_bad (_("duplicated `%s'"), saved);
11807 return NULL;
11808 }
11809
6225c532 11810 i.mask.zeroing = 1;
43234a1e
L
11811
11812 /* Only "{%k}" is allowed here. No need to check mask
11813 register explicitly. */
6225c532 11814 if (i.mask.operand != (unsigned int) this_operand)
43234a1e
L
11815 {
11816 as_bad (_("invalid zeroing-masking `%s'"),
11817 saved);
11818 return NULL;
11819 }
11820 }
11821
11822 op_string++;
11823 }
7063667e
JB
11824 else if (intel_syntax
11825 && (op_string = RC_SAE_specifier (op_string)) != NULL)
11826 i.rounding.modifier = true;
43234a1e
L
11827 else
11828 goto unknown_vec_op;
11829
11830 if (*op_string != '}')
11831 {
11832 as_bad (_("missing `}' in `%s'"), saved);
11833 return NULL;
11834 }
11835 op_string++;
0ba3a731
L
11836
11837 /* Strip whitespace since the addition of pseudo prefixes
11838 changed how the scrubber treats '{'. */
11839 if (is_space_char (*op_string))
11840 ++op_string;
11841
43234a1e
L
11842 continue;
11843 }
11844 unknown_vec_op:
11845 /* We don't know this one. */
11846 as_bad (_("unknown vector operation: `%s'"), saved);
11847 return NULL;
11848 }
11849
6225c532 11850 if (i.mask.reg && i.mask.zeroing && !i.mask.reg->reg_num)
6d2cd6b2
JB
11851 {
11852 as_bad (_("zeroing-masking only allowed with write mask"));
11853 return NULL;
11854 }
11855
43234a1e
L
11856 return op_string;
11857}
11858
252b5132 11859static int
70e41ade 11860i386_immediate (char *imm_start)
252b5132
RH
11861{
11862 char *save_input_line_pointer;
f3c180ae 11863 char *gotfree_input_line;
252b5132 11864 segT exp_seg = 0;
47926f60 11865 expressionS *exp;
40fb9820
L
11866 i386_operand_type types;
11867
0dfbf9d7 11868 operand_type_set (&types, ~0);
252b5132
RH
11869
11870 if (i.imm_operands == MAX_IMMEDIATE_OPERANDS)
11871 {
31b2323c
L
11872 as_bad (_("at most %d immediate operands are allowed"),
11873 MAX_IMMEDIATE_OPERANDS);
252b5132
RH
11874 return 0;
11875 }
11876
11877 exp = &im_expressions[i.imm_operands++];
520dc8e8 11878 i.op[this_operand].imms = exp;
252b5132
RH
11879
11880 if (is_space_char (*imm_start))
11881 ++imm_start;
11882
11883 save_input_line_pointer = input_line_pointer;
11884 input_line_pointer = imm_start;
11885
d258b828 11886 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
11887 if (gotfree_input_line)
11888 input_line_pointer = gotfree_input_line;
252b5132 11889
5cc00775 11890 expr_mode = expr_operator_none;
252b5132
RH
11891 exp_seg = expression (exp);
11892
c032bc4f
JB
11893 /* For .insn immediates there may be a size specifier. */
11894 if (dot_insn () && *input_line_pointer == '{' && input_line_pointer[1] == ':'
11895 && (input_line_pointer[2] == 's' || input_line_pointer[2] == 'u'))
11896 {
11897 char *e;
11898 unsigned long n = strtoul (input_line_pointer + 3, &e, 0);
11899
11900 if (*e == '}' && n && n <= (flag_code == CODE_64BIT ? 64 : 32))
11901 {
11902 i.imm_bits[this_operand] = n;
11903 if (input_line_pointer[2] == 's')
11904 i.flags[this_operand] |= Operand_Signed;
11905 input_line_pointer = e + 1;
11906 }
11907 }
11908
83183c0c 11909 SKIP_WHITESPACE ();
252b5132 11910 if (*input_line_pointer)
f3c180ae 11911 as_bad (_("junk `%s' after expression"), input_line_pointer);
252b5132
RH
11912
11913 input_line_pointer = save_input_line_pointer;
f3c180ae 11914 if (gotfree_input_line)
ee86248c
JB
11915 {
11916 free (gotfree_input_line);
11917
9aac24b1 11918 if (exp->X_op == O_constant)
ee86248c
JB
11919 exp->X_op = O_illegal;
11920 }
11921
9aac24b1
JB
11922 if (exp_seg == reg_section)
11923 {
11924 as_bad (_("illegal immediate register operand %s"), imm_start);
11925 return 0;
11926 }
11927
ee86248c
JB
11928 return i386_finalize_immediate (exp_seg, exp, types, imm_start);
11929}
252b5132 11930
ee86248c
JB
11931static int
11932i386_finalize_immediate (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
11933 i386_operand_type types, const char *imm_start)
11934{
11935 if (exp->X_op == O_absent || exp->X_op == O_illegal || exp->X_op == O_big)
252b5132 11936 {
313c53d1
L
11937 if (imm_start)
11938 as_bad (_("missing or invalid immediate expression `%s'"),
11939 imm_start);
3992d3b7 11940 return 0;
252b5132 11941 }
3e73aa7c 11942 else if (exp->X_op == O_constant)
252b5132 11943 {
47926f60 11944 /* Size it properly later. */
40fb9820 11945 i.types[this_operand].bitfield.imm64 = 1;
a442cac5
JB
11946
11947 /* If not 64bit, sign/zero extend val, to account for wraparound
11948 when !BFD64. */
5cc00775
JB
11949 if (expr_mode == expr_operator_present
11950 && flag_code != CODE_64BIT && !object_64bit)
a442cac5 11951 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
252b5132 11952 }
4c63da97 11953#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
f86103b7 11954 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
31312f95 11955 && exp_seg != absolute_section
47926f60 11956 && exp_seg != text_section
24eab124
AM
11957 && exp_seg != data_section
11958 && exp_seg != bss_section
11959 && exp_seg != undefined_section
f86103b7 11960 && !bfd_is_com_section (exp_seg))
252b5132 11961 {
d0b47220 11962 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
252b5132
RH
11963 return 0;
11964 }
11965#endif
11966 else
11967 {
11968 /* This is an address. The size of the address will be
24eab124 11969 determined later, depending on destination register,
3e73aa7c 11970 suffix, or the default for the section. */
40fb9820
L
11971 i.types[this_operand].bitfield.imm8 = 1;
11972 i.types[this_operand].bitfield.imm16 = 1;
11973 i.types[this_operand].bitfield.imm32 = 1;
11974 i.types[this_operand].bitfield.imm32s = 1;
11975 i.types[this_operand].bitfield.imm64 = 1;
c6fb90c8
L
11976 i.types[this_operand] = operand_type_and (i.types[this_operand],
11977 types);
252b5132
RH
11978 }
11979
11980 return 1;
11981}
11982
551c1ca1 11983static char *
e3bb37b5 11984i386_scale (char *scale)
252b5132 11985{
551c1ca1
AM
11986 offsetT val;
11987 char *save = input_line_pointer;
252b5132 11988
551c1ca1
AM
11989 input_line_pointer = scale;
11990 val = get_absolute_expression ();
11991
11992 switch (val)
252b5132 11993 {
551c1ca1 11994 case 1:
252b5132
RH
11995 i.log2_scale_factor = 0;
11996 break;
551c1ca1 11997 case 2:
252b5132
RH
11998 i.log2_scale_factor = 1;
11999 break;
551c1ca1 12000 case 4:
252b5132
RH
12001 i.log2_scale_factor = 2;
12002 break;
551c1ca1 12003 case 8:
252b5132
RH
12004 i.log2_scale_factor = 3;
12005 break;
12006 default:
a724f0f4
JB
12007 {
12008 char sep = *input_line_pointer;
12009
12010 *input_line_pointer = '\0';
12011 as_bad (_("expecting scale factor of 1, 2, 4, or 8: got `%s'"),
12012 scale);
12013 *input_line_pointer = sep;
12014 input_line_pointer = save;
12015 return NULL;
12016 }
252b5132 12017 }
29b0f896 12018 if (i.log2_scale_factor != 0 && i.index_reg == 0)
252b5132
RH
12019 {
12020 as_warn (_("scale factor of %d without an index register"),
24eab124 12021 1 << i.log2_scale_factor);
252b5132 12022 i.log2_scale_factor = 0;
252b5132 12023 }
551c1ca1
AM
12024 scale = input_line_pointer;
12025 input_line_pointer = save;
12026 return scale;
252b5132
RH
12027}
12028
252b5132 12029static int
e3bb37b5 12030i386_displacement (char *disp_start, char *disp_end)
252b5132 12031{
29b0f896 12032 expressionS *exp;
252b5132
RH
12033 segT exp_seg = 0;
12034 char *save_input_line_pointer;
f3c180ae 12035 char *gotfree_input_line;
40fb9820
L
12036 int override;
12037 i386_operand_type bigdisp, types = anydisp;
3992d3b7 12038 int ret;
252b5132 12039
31b2323c
L
12040 if (i.disp_operands == MAX_MEMORY_OPERANDS)
12041 {
12042 as_bad (_("at most %d displacement operands are allowed"),
12043 MAX_MEMORY_OPERANDS);
12044 return 0;
12045 }
12046
0dfbf9d7 12047 operand_type_set (&bigdisp, 0);
6f2f06be 12048 if (i.jumpabsolute
48bcea9f 12049 || i.types[this_operand].bitfield.baseindex
0cfa3eb3
JB
12050 || (current_templates->start->opcode_modifier.jump != JUMP
12051 && current_templates->start->opcode_modifier.jump != JUMP_DWORD))
e05278af 12052 {
48bcea9f 12053 i386_addressing_mode ();
e05278af 12054 override = (i.prefix[ADDR_PREFIX] != 0);
40fb9820
L
12055 if (flag_code == CODE_64BIT)
12056 {
a775efc8 12057 bigdisp.bitfield.disp32 = 1;
40fb9820 12058 if (!override)
a775efc8 12059 bigdisp.bitfield.disp64 = 1;
40fb9820
L
12060 }
12061 else if ((flag_code == CODE_16BIT) ^ override)
40fb9820 12062 bigdisp.bitfield.disp16 = 1;
48bcea9f
JB
12063 else
12064 bigdisp.bitfield.disp32 = 1;
e05278af
JB
12065 }
12066 else
12067 {
376cd056
JB
12068 /* For PC-relative branches, the width of the displacement may be
12069 dependent upon data size, but is never dependent upon address size.
12070 Also make sure to not unintentionally match against a non-PC-relative
12071 branch template. */
12072 static templates aux_templates;
12073 const insn_template *t = current_templates->start;
5b7c81bd 12074 bool has_intel64 = false;
376cd056
JB
12075
12076 aux_templates.start = t;
12077 while (++t < current_templates->end)
12078 {
12079 if (t->opcode_modifier.jump
12080 != current_templates->start->opcode_modifier.jump)
12081 break;
4b5aaf5f 12082 if ((t->opcode_modifier.isa64 >= INTEL64))
5b7c81bd 12083 has_intel64 = true;
376cd056
JB
12084 }
12085 if (t < current_templates->end)
12086 {
12087 aux_templates.end = t;
12088 current_templates = &aux_templates;
12089 }
12090
e05278af 12091 override = (i.prefix[DATA_PREFIX] != 0);
40fb9820
L
12092 if (flag_code == CODE_64BIT)
12093 {
376cd056
JB
12094 if ((override || i.suffix == WORD_MNEM_SUFFIX)
12095 && (!intel64 || !has_intel64))
40fb9820
L
12096 bigdisp.bitfield.disp16 = 1;
12097 else
a775efc8 12098 bigdisp.bitfield.disp32 = 1;
40fb9820
L
12099 }
12100 else
e05278af
JB
12101 {
12102 if (!override)
12103 override = (i.suffix == (flag_code != CODE_16BIT
12104 ? WORD_MNEM_SUFFIX
12105 : LONG_MNEM_SUFFIX));
40fb9820
L
12106 bigdisp.bitfield.disp32 = 1;
12107 if ((flag_code == CODE_16BIT) ^ override)
12108 {
12109 bigdisp.bitfield.disp32 = 0;
12110 bigdisp.bitfield.disp16 = 1;
12111 }
e05278af 12112 }
e05278af 12113 }
c6fb90c8
L
12114 i.types[this_operand] = operand_type_or (i.types[this_operand],
12115 bigdisp);
252b5132
RH
12116
12117 exp = &disp_expressions[i.disp_operands];
520dc8e8 12118 i.op[this_operand].disps = exp;
252b5132
RH
12119 i.disp_operands++;
12120 save_input_line_pointer = input_line_pointer;
12121 input_line_pointer = disp_start;
12122 END_STRING_AND_SAVE (disp_end);
12123
12124#ifndef GCC_ASM_O_HACK
12125#define GCC_ASM_O_HACK 0
12126#endif
12127#if GCC_ASM_O_HACK
12128 END_STRING_AND_SAVE (disp_end + 1);
40fb9820 12129 if (i.types[this_operand].bitfield.baseIndex
24eab124 12130 && displacement_string_end[-1] == '+')
252b5132
RH
12131 {
12132 /* This hack is to avoid a warning when using the "o"
24eab124
AM
12133 constraint within gcc asm statements.
12134 For instance:
12135
12136 #define _set_tssldt_desc(n,addr,limit,type) \
12137 __asm__ __volatile__ ( \
12138 "movw %w2,%0\n\t" \
12139 "movw %w1,2+%0\n\t" \
12140 "rorl $16,%1\n\t" \
12141 "movb %b1,4+%0\n\t" \
12142 "movb %4,5+%0\n\t" \
12143 "movb $0,6+%0\n\t" \
12144 "movb %h1,7+%0\n\t" \
12145 "rorl $16,%1" \
12146 : "=o"(*(n)) : "q" (addr), "ri"(limit), "i"(type))
12147
12148 This works great except that the output assembler ends
12149 up looking a bit weird if it turns out that there is
12150 no offset. You end up producing code that looks like:
12151
12152 #APP
12153 movw $235,(%eax)
12154 movw %dx,2+(%eax)
12155 rorl $16,%edx
12156 movb %dl,4+(%eax)
12157 movb $137,5+(%eax)
12158 movb $0,6+(%eax)
12159 movb %dh,7+(%eax)
12160 rorl $16,%edx
12161 #NO_APP
12162
47926f60 12163 So here we provide the missing zero. */
24eab124
AM
12164
12165 *displacement_string_end = '0';
252b5132
RH
12166 }
12167#endif
d258b828 12168 gotfree_input_line = lex_got (&i.reloc[this_operand], NULL, &types);
f3c180ae
AM
12169 if (gotfree_input_line)
12170 input_line_pointer = gotfree_input_line;
252b5132 12171
5cc00775 12172 expr_mode = expr_operator_none;
24eab124 12173 exp_seg = expression (exp);
252b5132 12174
636c26b0
AM
12175 SKIP_WHITESPACE ();
12176 if (*input_line_pointer)
12177 as_bad (_("junk `%s' after expression"), input_line_pointer);
12178#if GCC_ASM_O_HACK
12179 RESTORE_END_STRING (disp_end + 1);
12180#endif
636c26b0 12181 input_line_pointer = save_input_line_pointer;
636c26b0 12182 if (gotfree_input_line)
ee86248c
JB
12183 {
12184 free (gotfree_input_line);
12185
12186 if (exp->X_op == O_constant || exp->X_op == O_register)
12187 exp->X_op = O_illegal;
12188 }
12189
12190 ret = i386_finalize_displacement (exp_seg, exp, types, disp_start);
12191
12192 RESTORE_END_STRING (disp_end);
12193
12194 return ret;
12195}
12196
12197static int
12198i386_finalize_displacement (segT exp_seg ATTRIBUTE_UNUSED, expressionS *exp,
12199 i386_operand_type types, const char *disp_start)
12200{
ee86248c 12201 int ret = 1;
636c26b0 12202
24eab124
AM
12203 /* We do this to make sure that the section symbol is in
12204 the symbol table. We will ultimately change the relocation
47926f60 12205 to be relative to the beginning of the section. */
1ae12ab7 12206 if (i.reloc[this_operand] == BFD_RELOC_386_GOTOFF
d6ab8113
JB
12207 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL
12208 || i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
24eab124 12209 {
636c26b0 12210 if (exp->X_op != O_symbol)
3992d3b7 12211 goto inv_disp;
636c26b0 12212
e5cb08ac 12213 if (S_IS_LOCAL (exp->X_add_symbol)
c64efb4b
L
12214 && S_GET_SEGMENT (exp->X_add_symbol) != undefined_section
12215 && S_GET_SEGMENT (exp->X_add_symbol) != expr_section)
24eab124 12216 section_symbol (S_GET_SEGMENT (exp->X_add_symbol));
24eab124
AM
12217 exp->X_op = O_subtract;
12218 exp->X_op_symbol = GOT_symbol;
1ae12ab7 12219 if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTPCREL)
29b0f896 12220 i.reloc[this_operand] = BFD_RELOC_32_PCREL;
d6ab8113
JB
12221 else if (i.reloc[this_operand] == BFD_RELOC_X86_64_GOTOFF64)
12222 i.reloc[this_operand] = BFD_RELOC_64;
23df1078 12223 else
29b0f896 12224 i.reloc[this_operand] = BFD_RELOC_32;
24eab124 12225 }
252b5132 12226
3992d3b7
AM
12227 else if (exp->X_op == O_absent
12228 || exp->X_op == O_illegal
ee86248c 12229 || exp->X_op == O_big)
2daf4fd8 12230 {
3992d3b7
AM
12231 inv_disp:
12232 as_bad (_("missing or invalid displacement expression `%s'"),
2daf4fd8 12233 disp_start);
3992d3b7 12234 ret = 0;
2daf4fd8
AM
12235 }
12236
a50187b2
JB
12237 else if (exp->X_op == O_constant)
12238 {
12239 /* Sizing gets taken care of by optimize_disp().
12240
12241 If not 64bit, sign/zero extend val, to account for wraparound
12242 when !BFD64. */
5cc00775
JB
12243 if (expr_mode == expr_operator_present
12244 && flag_code != CODE_64BIT && !object_64bit)
a50187b2
JB
12245 exp->X_add_number = extend_to_32bit_address (exp->X_add_number);
12246 }
12247
4c63da97 12248#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
a50187b2 12249 else if (OUTPUT_FLAVOR == bfd_target_aout_flavour
3992d3b7
AM
12250 && exp_seg != absolute_section
12251 && exp_seg != text_section
12252 && exp_seg != data_section
12253 && exp_seg != bss_section
12254 && exp_seg != undefined_section
12255 && !bfd_is_com_section (exp_seg))
24eab124 12256 {
d0b47220 12257 as_bad (_("unimplemented segment %s in operand"), exp_seg->name);
3992d3b7 12258 ret = 0;
24eab124 12259 }
252b5132 12260#endif
3956db08 12261
a50187b2 12262 else if (current_templates->start->opcode_modifier.jump == JUMP_BYTE)
48bcea9f
JB
12263 i.types[this_operand].bitfield.disp8 = 1;
12264
40fb9820 12265 /* Check if this is a displacement only operand. */
02b83698
JB
12266 if (!i.types[this_operand].bitfield.baseindex)
12267 i.types[this_operand] =
12268 operand_type_or (operand_type_and_not (i.types[this_operand], anydisp),
12269 operand_type_and (i.types[this_operand], types));
3956db08 12270
3992d3b7 12271 return ret;
252b5132
RH
12272}
12273
2abc2bec
JB
12274/* Return the active addressing mode, taking address override and
12275 registers forming the address into consideration. Update the
12276 address override prefix if necessary. */
47926f60 12277
2abc2bec
JB
12278static enum flag_code
12279i386_addressing_mode (void)
252b5132 12280{
be05d201
L
12281 enum flag_code addr_mode;
12282
12283 if (i.prefix[ADDR_PREFIX])
12284 addr_mode = flag_code == CODE_32BIT ? CODE_16BIT : CODE_32BIT;
a23b33b3 12285 else if (flag_code == CODE_16BIT
734dfd1c 12286 && is_cpu (current_templates->start, CpuMPX)
a23b33b3
JB
12287 /* Avoid replacing the "16-bit addressing not allowed" diagnostic
12288 from md_assemble() by "is not a valid base/index expression"
12289 when there is a base and/or index. */
12290 && !i.types[this_operand].bitfield.baseindex)
12291 {
12292 /* MPX insn memory operands with neither base nor index must be forced
12293 to use 32-bit addressing in 16-bit mode. */
12294 addr_mode = CODE_32BIT;
12295 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
12296 ++i.prefixes;
12297 gas_assert (!i.types[this_operand].bitfield.disp16);
12298 gas_assert (!i.types[this_operand].bitfield.disp32);
12299 }
be05d201
L
12300 else
12301 {
12302 addr_mode = flag_code;
12303
24eab124 12304#if INFER_ADDR_PREFIX
be05d201
L
12305 if (i.mem_operands == 0)
12306 {
12307 /* Infer address prefix from the first memory operand. */
12308 const reg_entry *addr_reg = i.base_reg;
12309
12310 if (addr_reg == NULL)
12311 addr_reg = i.index_reg;
eecb386c 12312
be05d201
L
12313 if (addr_reg)
12314 {
e968fc9b 12315 if (addr_reg->reg_type.bitfield.dword)
be05d201
L
12316 addr_mode = CODE_32BIT;
12317 else if (flag_code != CODE_64BIT
dc821c5f 12318 && addr_reg->reg_type.bitfield.word)
be05d201
L
12319 addr_mode = CODE_16BIT;
12320
12321 if (addr_mode != flag_code)
12322 {
12323 i.prefix[ADDR_PREFIX] = ADDR_PREFIX_OPCODE;
12324 i.prefixes += 1;
12325 /* Change the size of any displacement too. At most one
12326 of Disp16 or Disp32 is set.
12327 FIXME. There doesn't seem to be any real need for
12328 separate Disp16 and Disp32 flags. The same goes for
12329 Imm16 and Imm32. Removing them would probably clean
12330 up the code quite a lot. */
12331 if (flag_code != CODE_64BIT
12332 && (i.types[this_operand].bitfield.disp16
12333 || i.types[this_operand].bitfield.disp32))
05909f23
JB
12334 {
12335 static const i386_operand_type disp16_32 = {
12336 .bitfield = { .disp16 = 1, .disp32 = 1 }
12337 };
12338
12339 i.types[this_operand]
12340 = operand_type_xor (i.types[this_operand], disp16_32);
12341 }
be05d201
L
12342 }
12343 }
12344 }
24eab124 12345#endif
be05d201
L
12346 }
12347
2abc2bec
JB
12348 return addr_mode;
12349}
12350
12351/* Make sure the memory operand we've been dealt is valid.
12352 Return 1 on success, 0 on a failure. */
12353
12354static int
12355i386_index_check (const char *operand_string)
12356{
12357 const char *kind = "base/index";
12358 enum flag_code addr_mode = i386_addressing_mode ();
77a19f0e 12359 const insn_template *t = current_templates->end - 1;
2abc2bec 12360
77a19f0e 12361 if (t->opcode_modifier.isstring)
fc0763e6
JB
12362 {
12363 /* Memory operands of string insns are special in that they only allow
12364 a single register (rDI, rSI, or rBX) as their memory address. */
be05d201 12365 const reg_entry *expected_reg;
069a1561 12366 static const char di_si[][2][4] =
be05d201
L
12367 {
12368 { "esi", "edi" },
12369 { "si", "di" },
12370 { "rsi", "rdi" }
12371 };
069a1561 12372 static const char bx[][4] = { "ebx", "bx", "rbx" };
fc0763e6
JB
12373
12374 kind = "string address";
12375
a152332d 12376 if (t->opcode_modifier.prefixok == PrefixRep)
fc0763e6 12377 {
77a19f0e 12378 int es_op = t->opcode_modifier.isstring - IS_STRING_ES_OP0;
51c8edf6 12379 int op = 0;
fc0763e6 12380
77a19f0e 12381 if (!t->operand_types[0].bitfield.baseindex
fc0763e6 12382 || ((!i.mem_operands != !intel_syntax)
77a19f0e 12383 && t->operand_types[1].bitfield.baseindex))
51c8edf6 12384 op = 1;
fe0e921f
AM
12385 expected_reg
12386 = (const reg_entry *) str_hash_find (reg_hash,
12387 di_si[addr_mode][op == es_op]);
fc0763e6
JB
12388 }
12389 else
fe0e921f
AM
12390 expected_reg
12391 = (const reg_entry *)str_hash_find (reg_hash, bx[addr_mode]);
fc0763e6 12392
be05d201
L
12393 if (i.base_reg != expected_reg
12394 || i.index_reg
fc0763e6 12395 || operand_type_check (i.types[this_operand], disp))
fc0763e6 12396 {
be05d201
L
12397 /* The second memory operand must have the same size as
12398 the first one. */
12399 if (i.mem_operands
12400 && i.base_reg
12401 && !((addr_mode == CODE_64BIT
dc821c5f 12402 && i.base_reg->reg_type.bitfield.qword)
be05d201 12403 || (addr_mode == CODE_32BIT
dc821c5f
JB
12404 ? i.base_reg->reg_type.bitfield.dword
12405 : i.base_reg->reg_type.bitfield.word)))
be05d201
L
12406 goto bad_address;
12407
fc0763e6
JB
12408 as_warn (_("`%s' is not valid here (expected `%c%s%s%c')"),
12409 operand_string,
12410 intel_syntax ? '[' : '(',
12411 register_prefix,
be05d201 12412 expected_reg->reg_name,
fc0763e6 12413 intel_syntax ? ']' : ')');
be05d201 12414 return 1;
fc0763e6 12415 }
be05d201
L
12416 else
12417 return 1;
12418
dc1e8a47 12419 bad_address:
be05d201
L
12420 as_bad (_("`%s' is not a valid %s expression"),
12421 operand_string, kind);
12422 return 0;
3e73aa7c
JH
12423 }
12424 else
12425 {
77a19f0e
JB
12426 t = current_templates->start;
12427
be05d201
L
12428 if (addr_mode != CODE_16BIT)
12429 {
12430 /* 32-bit/64-bit checks. */
41eb8e88
L
12431 if (i.disp_encoding == disp_encoding_16bit)
12432 {
12433 bad_disp:
12434 as_bad (_("invalid `%s' prefix"),
12435 addr_mode == CODE_16BIT ? "{disp32}" : "{disp16}");
12436 return 0;
12437 }
12438
be05d201 12439 if ((i.base_reg
e968fc9b
JB
12440 && ((addr_mode == CODE_64BIT
12441 ? !i.base_reg->reg_type.bitfield.qword
12442 : !i.base_reg->reg_type.bitfield.dword)
12443 || (i.index_reg && i.base_reg->reg_num == RegIP)
12444 || i.base_reg->reg_num == RegIZ))
be05d201 12445 || (i.index_reg
1b54b8d7
JB
12446 && !i.index_reg->reg_type.bitfield.xmmword
12447 && !i.index_reg->reg_type.bitfield.ymmword
12448 && !i.index_reg->reg_type.bitfield.zmmword
be05d201 12449 && ((addr_mode == CODE_64BIT
e968fc9b
JB
12450 ? !i.index_reg->reg_type.bitfield.qword
12451 : !i.index_reg->reg_type.bitfield.dword)
be05d201
L
12452 || !i.index_reg->reg_type.bitfield.baseindex)))
12453 goto bad_address;
8178be5b 12454
260cd341 12455 /* bndmk, bndldx, bndstx and mandatory non-vector SIB have special restrictions. */
7fc69528
JB
12456 if (t->mnem_off == MN_bndmk
12457 || t->mnem_off == MN_bndldx
12458 || t->mnem_off == MN_bndstx
a152332d 12459 || t->opcode_modifier.sib == SIBMEM)
8178be5b
JB
12460 {
12461 /* They cannot use RIP-relative addressing. */
e968fc9b 12462 if (i.base_reg && i.base_reg->reg_num == RegIP)
8178be5b
JB
12463 {
12464 as_bad (_("`%s' cannot be used here"), operand_string);
12465 return 0;
12466 }
12467
12468 /* bndldx and bndstx ignore their scale factor. */
7fc69528 12469 if ((t->mnem_off == MN_bndldx || t->mnem_off == MN_bndstx)
8178be5b
JB
12470 && i.log2_scale_factor)
12471 as_warn (_("register scaling is being ignored here"));
12472 }
be05d201
L
12473 }
12474 else
3e73aa7c 12475 {
be05d201 12476 /* 16-bit checks. */
41eb8e88
L
12477 if (i.disp_encoding == disp_encoding_32bit)
12478 goto bad_disp;
12479
3e73aa7c 12480 if ((i.base_reg
dc821c5f 12481 && (!i.base_reg->reg_type.bitfield.word
40fb9820 12482 || !i.base_reg->reg_type.bitfield.baseindex))
3e73aa7c 12483 || (i.index_reg
dc821c5f 12484 && (!i.index_reg->reg_type.bitfield.word
40fb9820 12485 || !i.index_reg->reg_type.bitfield.baseindex
29b0f896
AM
12486 || !(i.base_reg
12487 && i.base_reg->reg_num < 6
12488 && i.index_reg->reg_num >= 6
12489 && i.log2_scale_factor == 0))))
be05d201 12490 goto bad_address;
3e73aa7c
JH
12491 }
12492 }
be05d201 12493 return 1;
24eab124 12494}
252b5132 12495
43234a1e
L
12496/* Handle vector immediates. */
12497
12498static int
12499RC_SAE_immediate (const char *imm_start)
12500{
43234a1e 12501 const char *pstr = imm_start;
43234a1e
L
12502
12503 if (*pstr != '{')
12504 return 0;
12505
7063667e
JB
12506 pstr = RC_SAE_specifier (pstr + 1);
12507 if (pstr == NULL)
43234a1e
L
12508 return 0;
12509
12510 if (*pstr++ != '}')
12511 {
12512 as_bad (_("Missing '}': '%s'"), imm_start);
12513 return 0;
12514 }
12515 /* RC/SAE immediate string should contain nothing more. */;
12516 if (*pstr != 0)
12517 {
12518 as_bad (_("Junk after '}': '%s'"), imm_start);
12519 return 0;
12520 }
12521
cf665fee
JB
12522 /* Internally this doesn't count as an operand. */
12523 --i.operands;
43234a1e 12524
43234a1e
L
12525 return 1;
12526}
12527
9d299bea
JB
12528static INLINE bool starts_memory_operand (char c)
12529{
014fbcda 12530 return ISDIGIT (c)
4795cd4a 12531 || is_name_beginner (c)
014fbcda 12532 || strchr ("([\"+-!~", c);
9d299bea
JB
12533}
12534
fc0763e6 12535/* Parse OPERAND_STRING into the i386_insn structure I. Returns zero
47926f60 12536 on error. */
252b5132 12537
252b5132 12538static int
a7619375 12539i386_att_operand (char *operand_string)
252b5132 12540{
af6bdddf
AM
12541 const reg_entry *r;
12542 char *end_op;
24eab124 12543 char *op_string = operand_string;
252b5132 12544
24eab124 12545 if (is_space_char (*op_string))
252b5132
RH
12546 ++op_string;
12547
24eab124 12548 /* We check for an absolute prefix (differentiating,
47926f60 12549 for example, 'jmp pc_relative_label' from 'jmp *absolute_label'. */
ccb05c9c
JB
12550 if (*op_string == ABSOLUTE_PREFIX
12551 && current_templates->start->opcode_modifier.jump)
24eab124
AM
12552 {
12553 ++op_string;
12554 if (is_space_char (*op_string))
12555 ++op_string;
5b7c81bd 12556 i.jumpabsolute = true;
24eab124 12557 }
252b5132 12558
47926f60 12559 /* Check if operand is a register. */
4d1bb795 12560 if ((r = parse_register (op_string, &end_op)) != NULL)
24eab124 12561 {
40fb9820
L
12562 i386_operand_type temp;
12563
8a6fb3f9
JB
12564 if (r == &bad_reg)
12565 return 0;
12566
24eab124
AM
12567 /* Check for a segment override by searching for ':' after a
12568 segment register. */
12569 op_string = end_op;
12570 if (is_space_char (*op_string))
12571 ++op_string;
00cee14f 12572 if (*op_string == ':' && r->reg_type.bitfield.class == SReg)
24eab124 12573 {
5e042380 12574 i.seg[i.mem_operands] = r;
252b5132 12575
24eab124 12576 /* Skip the ':' and whitespace. */
252b5132
RH
12577 ++op_string;
12578 if (is_space_char (*op_string))
24eab124 12579 ++op_string;
252b5132 12580
47926f60 12581 /* Handle case of %es:*foo. */
ccb05c9c
JB
12582 if (!i.jumpabsolute && *op_string == ABSOLUTE_PREFIX
12583 && current_templates->start->opcode_modifier.jump)
24eab124
AM
12584 {
12585 ++op_string;
12586 if (is_space_char (*op_string))
12587 ++op_string;
5b7c81bd 12588 i.jumpabsolute = true;
24eab124 12589 }
c8d541e2 12590
9d299bea 12591 if (!starts_memory_operand (*op_string))
c8d541e2
JB
12592 {
12593 as_bad (_("bad memory operand `%s'"), op_string);
12594 return 0;
12595 }
24eab124
AM
12596 goto do_memory_reference;
12597 }
43234a1e
L
12598
12599 /* Handle vector operations. */
12600 if (*op_string == '{')
12601 {
f70c6814 12602 op_string = check_VecOperations (op_string);
43234a1e
L
12603 if (op_string == NULL)
12604 return 0;
12605 }
12606
24eab124
AM
12607 if (*op_string)
12608 {
d0b47220 12609 as_bad (_("junk `%s' after register"), op_string);
24eab124
AM
12610 return 0;
12611 }
0ff3b7d0
JB
12612
12613 /* Reject pseudo registers for .insn. */
12614 if (dot_insn () && r->reg_type.bitfield.class == ClassNone)
12615 {
12616 as_bad (_("`%s%s' cannot be used here"),
12617 register_prefix, r->reg_name);
12618 return 0;
12619 }
12620
40fb9820
L
12621 temp = r->reg_type;
12622 temp.bitfield.baseindex = 0;
c6fb90c8
L
12623 i.types[this_operand] = operand_type_or (i.types[this_operand],
12624 temp);
7d5e4556 12625 i.types[this_operand].bitfield.unspecified = 0;
520dc8e8 12626 i.op[this_operand].regs = r;
24eab124 12627 i.reg_operands++;
cf665fee
JB
12628
12629 /* A GPR may follow an RC or SAE immediate only if a (vector) register
12630 operand was also present earlier on. */
12631 if (i.rounding.type != rc_none && temp.bitfield.class == Reg
12632 && i.reg_operands == 1)
12633 {
12634 unsigned int j;
12635
12636 for (j = 0; j < ARRAY_SIZE (RC_NamesTable); ++j)
12637 if (i.rounding.type == RC_NamesTable[j].type)
12638 break;
12639 as_bad (_("`%s': misplaced `{%s}'"),
76d3f746 12640 insn_name (current_templates->start), RC_NamesTable[j].name);
cf665fee
JB
12641 return 0;
12642 }
24eab124 12643 }
af6bdddf
AM
12644 else if (*op_string == REGISTER_PREFIX)
12645 {
12646 as_bad (_("bad register name `%s'"), op_string);
12647 return 0;
12648 }
24eab124 12649 else if (*op_string == IMMEDIATE_PREFIX)
ce8a8b2f 12650 {
24eab124 12651 ++op_string;
6f2f06be 12652 if (i.jumpabsolute)
24eab124 12653 {
d0b47220 12654 as_bad (_("immediate operand illegal with absolute jump"));
24eab124
AM
12655 return 0;
12656 }
12657 if (!i386_immediate (op_string))
12658 return 0;
cf665fee
JB
12659 if (i.rounding.type != rc_none)
12660 {
12661 as_bad (_("`%s': RC/SAE operand must follow immediate operands"),
76d3f746 12662 insn_name (current_templates->start));
cf665fee
JB
12663 return 0;
12664 }
24eab124 12665 }
43234a1e
L
12666 else if (RC_SAE_immediate (operand_string))
12667 {
cf665fee
JB
12668 /* If it is a RC or SAE immediate, do the necessary placement check:
12669 Only another immediate or a GPR may precede it. */
12670 if (i.mem_operands || i.reg_operands + i.imm_operands > 1
12671 || (i.reg_operands == 1
12672 && i.op[0].regs->reg_type.bitfield.class != Reg))
12673 {
12674 as_bad (_("`%s': misplaced `%s'"),
76d3f746 12675 insn_name (current_templates->start), operand_string);
cf665fee
JB
12676 return 0;
12677 }
43234a1e 12678 }
9d299bea 12679 else if (starts_memory_operand (*op_string))
24eab124 12680 {
47926f60 12681 /* This is a memory reference of some sort. */
af6bdddf 12682 char *base_string;
252b5132 12683
47926f60 12684 /* Start and end of displacement string expression (if found). */
eecb386c
AM
12685 char *displacement_string_start;
12686 char *displacement_string_end;
252b5132 12687
24eab124 12688 do_memory_reference:
24eab124
AM
12689 /* Check for base index form. We detect the base index form by
12690 looking for an ')' at the end of the operand, searching
12691 for the '(' matching it, and finding a REGISTER_PREFIX or ','
12692 after the '('. */
af6bdddf 12693 base_string = op_string + strlen (op_string);
c3332e24 12694
43234a1e 12695 /* Handle vector operations. */
6b5ba0d4
JB
12696 --base_string;
12697 if (is_space_char (*base_string))
12698 --base_string;
12699
12700 if (*base_string == '}')
43234a1e 12701 {
6b5ba0d4
JB
12702 char *vop_start = NULL;
12703
12704 while (base_string-- > op_string)
12705 {
12706 if (*base_string == '"')
12707 break;
12708 if (*base_string != '{')
12709 continue;
12710
12711 vop_start = base_string;
12712
12713 --base_string;
12714 if (is_space_char (*base_string))
12715 --base_string;
12716
12717 if (*base_string != '}')
12718 break;
12719
12720 vop_start = NULL;
12721 }
12722
12723 if (!vop_start)
12724 {
12725 as_bad (_("unbalanced figure braces"));
12726 return 0;
12727 }
12728
f70c6814 12729 if (check_VecOperations (vop_start) == NULL)
43234a1e 12730 return 0;
43234a1e
L
12731 }
12732
47926f60 12733 /* If we only have a displacement, set-up for it to be parsed later. */
af6bdddf
AM
12734 displacement_string_start = op_string;
12735 displacement_string_end = base_string + 1;
252b5132 12736
24eab124
AM
12737 if (*base_string == ')')
12738 {
af6bdddf 12739 char *temp_string;
e87fb6a6
JB
12740 unsigned int parens_not_balanced = 0;
12741 bool in_quotes = false;
e68c3d59 12742
24eab124 12743 /* We've already checked that the number of left & right ()'s are
e87fb6a6
JB
12744 equal, and that there's a matching set of double quotes. */
12745 end_op = base_string;
12746 for (temp_string = op_string; temp_string < end_op; temp_string++)
24eab124 12747 {
e87fb6a6
JB
12748 if (*temp_string == '\\' && temp_string[1] == '"')
12749 ++temp_string;
12750 else if (*temp_string == '"')
12751 in_quotes = !in_quotes;
12752 else if (!in_quotes)
12753 {
12754 if (*temp_string == '(' && !parens_not_balanced++)
12755 base_string = temp_string;
12756 if (*temp_string == ')')
12757 --parens_not_balanced;
12758 }
24eab124 12759 }
c3332e24 12760
af6bdddf 12761 temp_string = base_string;
c3332e24 12762
24eab124 12763 /* Skip past '(' and whitespace. */
4f081312 12764 gas_assert (*base_string == '(');
e87fb6a6 12765 ++base_string;
252b5132 12766 if (is_space_char (*base_string))
24eab124 12767 ++base_string;
252b5132 12768
af6bdddf 12769 if (*base_string == ','
4eed87de
AM
12770 || ((i.base_reg = parse_register (base_string, &end_op))
12771 != NULL))
252b5132 12772 {
af6bdddf 12773 displacement_string_end = temp_string;
252b5132 12774
40fb9820 12775 i.types[this_operand].bitfield.baseindex = 1;
252b5132 12776
af6bdddf 12777 if (i.base_reg)
24eab124 12778 {
8a6fb3f9
JB
12779 if (i.base_reg == &bad_reg)
12780 return 0;
24eab124
AM
12781 base_string = end_op;
12782 if (is_space_char (*base_string))
12783 ++base_string;
af6bdddf
AM
12784 }
12785
12786 /* There may be an index reg or scale factor here. */
12787 if (*base_string == ',')
12788 {
12789 ++base_string;
12790 if (is_space_char (*base_string))
12791 ++base_string;
12792
4eed87de
AM
12793 if ((i.index_reg = parse_register (base_string, &end_op))
12794 != NULL)
24eab124 12795 {
8a6fb3f9
JB
12796 if (i.index_reg == &bad_reg)
12797 return 0;
af6bdddf 12798 base_string = end_op;
24eab124
AM
12799 if (is_space_char (*base_string))
12800 ++base_string;
af6bdddf
AM
12801 if (*base_string == ',')
12802 {
12803 ++base_string;
12804 if (is_space_char (*base_string))
12805 ++base_string;
12806 }
e5cb08ac 12807 else if (*base_string != ')')
af6bdddf 12808 {
4eed87de
AM
12809 as_bad (_("expecting `,' or `)' "
12810 "after index register in `%s'"),
af6bdddf
AM
12811 operand_string);
12812 return 0;
12813 }
24eab124 12814 }
af6bdddf 12815 else if (*base_string == REGISTER_PREFIX)
24eab124 12816 {
f76bf5e0
L
12817 end_op = strchr (base_string, ',');
12818 if (end_op)
12819 *end_op = '\0';
af6bdddf 12820 as_bad (_("bad register name `%s'"), base_string);
24eab124
AM
12821 return 0;
12822 }
252b5132 12823
47926f60 12824 /* Check for scale factor. */
551c1ca1 12825 if (*base_string != ')')
af6bdddf 12826 {
551c1ca1
AM
12827 char *end_scale = i386_scale (base_string);
12828
12829 if (!end_scale)
af6bdddf 12830 return 0;
24eab124 12831
551c1ca1 12832 base_string = end_scale;
af6bdddf
AM
12833 if (is_space_char (*base_string))
12834 ++base_string;
12835 if (*base_string != ')')
12836 {
4eed87de
AM
12837 as_bad (_("expecting `)' "
12838 "after scale factor in `%s'"),
af6bdddf
AM
12839 operand_string);
12840 return 0;
12841 }
12842 }
12843 else if (!i.index_reg)
24eab124 12844 {
4eed87de
AM
12845 as_bad (_("expecting index register or scale factor "
12846 "after `,'; got '%c'"),
af6bdddf 12847 *base_string);
24eab124
AM
12848 return 0;
12849 }
12850 }
af6bdddf 12851 else if (*base_string != ')')
24eab124 12852 {
4eed87de
AM
12853 as_bad (_("expecting `,' or `)' "
12854 "after base register in `%s'"),
af6bdddf 12855 operand_string);
24eab124
AM
12856 return 0;
12857 }
c3332e24 12858 }
af6bdddf 12859 else if (*base_string == REGISTER_PREFIX)
c3332e24 12860 {
f76bf5e0
L
12861 end_op = strchr (base_string, ',');
12862 if (end_op)
12863 *end_op = '\0';
af6bdddf 12864 as_bad (_("bad register name `%s'"), base_string);
24eab124 12865 return 0;
c3332e24 12866 }
24eab124
AM
12867 }
12868
12869 /* If there's an expression beginning the operand, parse it,
12870 assuming displacement_string_start and
12871 displacement_string_end are meaningful. */
12872 if (displacement_string_start != displacement_string_end)
12873 {
12874 if (!i386_displacement (displacement_string_start,
12875 displacement_string_end))
12876 return 0;
12877 }
12878
12879 /* Special case for (%dx) while doing input/output op. */
12880 if (i.base_reg
75e5731b
JB
12881 && i.base_reg->reg_type.bitfield.instance == RegD
12882 && i.base_reg->reg_type.bitfield.word
24eab124
AM
12883 && i.index_reg == 0
12884 && i.log2_scale_factor == 0
12885 && i.seg[i.mem_operands] == 0
40fb9820 12886 && !operand_type_check (i.types[this_operand], disp))
24eab124 12887 {
2fb5be8d 12888 i.types[this_operand] = i.base_reg->reg_type;
9373f275 12889 i.input_output_operand = true;
24eab124
AM
12890 return 1;
12891 }
12892
eecb386c
AM
12893 if (i386_index_check (operand_string) == 0)
12894 return 0;
c48dadc9 12895 i.flags[this_operand] |= Operand_Mem;
24eab124
AM
12896 i.mem_operands++;
12897 }
12898 else
ce8a8b2f
AM
12899 {
12900 /* It's not a memory operand; argh! */
24eab124
AM
12901 as_bad (_("invalid char %s beginning operand %d `%s'"),
12902 output_invalid (*op_string),
12903 this_operand + 1,
12904 op_string);
12905 return 0;
12906 }
47926f60 12907 return 1; /* Normal return. */
252b5132
RH
12908}
12909\f
fa94de6b
RM
12910/* Calculate the maximum variable size (i.e., excluding fr_fix)
12911 that an rs_machine_dependent frag may reach. */
12912
12913unsigned int
12914i386_frag_max_var (fragS *frag)
12915{
12916 /* The only relaxable frags are for jumps.
12917 Unconditional jumps can grow by 4 bytes and others by 5 bytes. */
12918 gas_assert (frag->fr_type == rs_machine_dependent);
12919 return TYPE_FROM_RELAX_STATE (frag->fr_subtype) == UNCOND_JUMP ? 4 : 5;
12920}
12921
b084df0b
L
12922#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
12923static int
8dcea932 12924elf_symbol_resolved_in_segment_p (symbolS *fr_symbol, offsetT fr_var)
b084df0b
L
12925{
12926 /* STT_GNU_IFUNC symbol must go through PLT. */
12927 if ((symbol_get_bfdsym (fr_symbol)->flags
12928 & BSF_GNU_INDIRECT_FUNCTION) != 0)
12929 return 0;
12930
12931 if (!S_IS_EXTERNAL (fr_symbol))
12932 /* Symbol may be weak or local. */
12933 return !S_IS_WEAK (fr_symbol);
12934
8dcea932
L
12935 /* Global symbols with non-default visibility can't be preempted. */
12936 if (ELF_ST_VISIBILITY (S_GET_OTHER (fr_symbol)) != STV_DEFAULT)
12937 return 1;
12938
12939 if (fr_var != NO_RELOC)
12940 switch ((enum bfd_reloc_code_real) fr_var)
12941 {
12942 case BFD_RELOC_386_PLT32:
12943 case BFD_RELOC_X86_64_PLT32:
33eaf5de 12944 /* Symbol with PLT relocation may be preempted. */
8dcea932
L
12945 return 0;
12946 default:
12947 abort ();
12948 }
12949
b084df0b
L
12950 /* Global symbols with default visibility in a shared library may be
12951 preempted by another definition. */
8dcea932 12952 return !shared;
b084df0b
L
12953}
12954#endif
12955
79d72f45
HL
12956/* Table 3-2. Macro-Fusible Instructions in Haswell Microarchitecture
12957 Note also work for Skylake and Cascadelake.
12958---------------------------------------------------------------------
12959| JCC | ADD/SUB/CMP | INC/DEC | TEST/AND |
12960| ------ | ----------- | ------- | -------- |
12961| Jo | N | N | Y |
12962| Jno | N | N | Y |
12963| Jc/Jb | Y | N | Y |
12964| Jae/Jnb | Y | N | Y |
12965| Je/Jz | Y | Y | Y |
12966| Jne/Jnz | Y | Y | Y |
12967| Jna/Jbe | Y | N | Y |
12968| Ja/Jnbe | Y | N | Y |
12969| Js | N | N | Y |
12970| Jns | N | N | Y |
12971| Jp/Jpe | N | N | Y |
12972| Jnp/Jpo | N | N | Y |
12973| Jl/Jnge | Y | Y | Y |
12974| Jge/Jnl | Y | Y | Y |
12975| Jle/Jng | Y | Y | Y |
12976| Jg/Jnle | Y | Y | Y |
12977--------------------------------------------------------------------- */
12978static int
12979i386_macro_fusible_p (enum mf_cmp_kind mf_cmp, enum mf_jcc_kind mf_jcc)
12980{
12981 if (mf_cmp == mf_cmp_alu_cmp)
12982 return ((mf_jcc >= mf_jcc_jc && mf_jcc <= mf_jcc_jna)
12983 || mf_jcc == mf_jcc_jl || mf_jcc == mf_jcc_jle);
12984 if (mf_cmp == mf_cmp_incdec)
12985 return (mf_jcc == mf_jcc_je || mf_jcc == mf_jcc_jl
12986 || mf_jcc == mf_jcc_jle);
12987 if (mf_cmp == mf_cmp_test_and)
12988 return 1;
12989 return 0;
12990}
12991
e379e5f3
L
12992/* Return the next non-empty frag. */
12993
12994static fragS *
12995i386_next_non_empty_frag (fragS *fragP)
12996{
12997 /* There may be a frag with a ".fill 0" when there is no room in
12998 the current frag for frag_grow in output_insn. */
12999 for (fragP = fragP->fr_next;
13000 (fragP != NULL
13001 && fragP->fr_type == rs_fill
13002 && fragP->fr_fix == 0);
13003 fragP = fragP->fr_next)
13004 ;
13005 return fragP;
13006}
13007
13008/* Return the next jcc frag after BRANCH_PADDING. */
13009
13010static fragS *
79d72f45 13011i386_next_fusible_jcc_frag (fragS *maybe_cmp_fragP, fragS *pad_fragP)
e379e5f3 13012{
79d72f45
HL
13013 fragS *branch_fragP;
13014 if (!pad_fragP)
e379e5f3
L
13015 return NULL;
13016
79d72f45
HL
13017 if (pad_fragP->fr_type == rs_machine_dependent
13018 && (TYPE_FROM_RELAX_STATE (pad_fragP->fr_subtype)
e379e5f3
L
13019 == BRANCH_PADDING))
13020 {
79d72f45
HL
13021 branch_fragP = i386_next_non_empty_frag (pad_fragP);
13022 if (branch_fragP->fr_type != rs_machine_dependent)
e379e5f3 13023 return NULL;
79d72f45
HL
13024 if (TYPE_FROM_RELAX_STATE (branch_fragP->fr_subtype) == COND_JUMP
13025 && i386_macro_fusible_p (maybe_cmp_fragP->tc_frag_data.mf_type,
13026 pad_fragP->tc_frag_data.mf_type))
13027 return branch_fragP;
e379e5f3
L
13028 }
13029
13030 return NULL;
13031}
13032
13033/* Classify BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags. */
13034
13035static void
13036i386_classify_machine_dependent_frag (fragS *fragP)
13037{
13038 fragS *cmp_fragP;
13039 fragS *pad_fragP;
13040 fragS *branch_fragP;
13041 fragS *next_fragP;
13042 unsigned int max_prefix_length;
13043
13044 if (fragP->tc_frag_data.classified)
13045 return;
13046
13047 /* First scan for BRANCH_PADDING and FUSED_JCC_PADDING. Convert
13048 FUSED_JCC_PADDING and merge BRANCH_PADDING. */
13049 for (next_fragP = fragP;
13050 next_fragP != NULL;
13051 next_fragP = next_fragP->fr_next)
13052 {
13053 next_fragP->tc_frag_data.classified = 1;
13054 if (next_fragP->fr_type == rs_machine_dependent)
13055 switch (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype))
13056 {
13057 case BRANCH_PADDING:
13058 /* The BRANCH_PADDING frag must be followed by a branch
13059 frag. */
13060 branch_fragP = i386_next_non_empty_frag (next_fragP);
13061 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
13062 break;
13063 case FUSED_JCC_PADDING:
13064 /* Check if this is a fused jcc:
13065 FUSED_JCC_PADDING
13066 CMP like instruction
13067 BRANCH_PADDING
13068 COND_JUMP
13069 */
13070 cmp_fragP = i386_next_non_empty_frag (next_fragP);
13071 pad_fragP = i386_next_non_empty_frag (cmp_fragP);
79d72f45 13072 branch_fragP = i386_next_fusible_jcc_frag (next_fragP, pad_fragP);
e379e5f3
L
13073 if (branch_fragP)
13074 {
13075 /* The BRANCH_PADDING frag is merged with the
13076 FUSED_JCC_PADDING frag. */
13077 next_fragP->tc_frag_data.u.branch_fragP = branch_fragP;
13078 /* CMP like instruction size. */
13079 next_fragP->tc_frag_data.cmp_size = cmp_fragP->fr_fix;
13080 frag_wane (pad_fragP);
13081 /* Skip to branch_fragP. */
13082 next_fragP = branch_fragP;
13083 }
13084 else if (next_fragP->tc_frag_data.max_prefix_length)
13085 {
13086 /* Turn FUSED_JCC_PADDING into BRANCH_PREFIX if it isn't
13087 a fused jcc. */
13088 next_fragP->fr_subtype
13089 = ENCODE_RELAX_STATE (BRANCH_PREFIX, 0);
13090 next_fragP->tc_frag_data.max_bytes
13091 = next_fragP->tc_frag_data.max_prefix_length;
13092 /* This will be updated in the BRANCH_PREFIX scan. */
13093 next_fragP->tc_frag_data.max_prefix_length = 0;
13094 }
13095 else
13096 frag_wane (next_fragP);
13097 break;
13098 }
13099 }
13100
13101 /* Stop if there is no BRANCH_PREFIX. */
13102 if (!align_branch_prefix_size)
13103 return;
13104
13105 /* Scan for BRANCH_PREFIX. */
13106 for (; fragP != NULL; fragP = fragP->fr_next)
13107 {
13108 if (fragP->fr_type != rs_machine_dependent
13109 || (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
13110 != BRANCH_PREFIX))
13111 continue;
13112
13113 /* Count all BRANCH_PREFIX frags before BRANCH_PADDING and
13114 COND_JUMP_PREFIX. */
13115 max_prefix_length = 0;
13116 for (next_fragP = fragP;
13117 next_fragP != NULL;
13118 next_fragP = next_fragP->fr_next)
13119 {
13120 if (next_fragP->fr_type == rs_fill)
13121 /* Skip rs_fill frags. */
13122 continue;
13123 else if (next_fragP->fr_type != rs_machine_dependent)
13124 /* Stop for all other frags. */
13125 break;
13126
13127 /* rs_machine_dependent frags. */
13128 if (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13129 == BRANCH_PREFIX)
13130 {
13131 /* Count BRANCH_PREFIX frags. */
13132 if (max_prefix_length >= MAX_FUSED_JCC_PADDING_SIZE)
13133 {
13134 max_prefix_length = MAX_FUSED_JCC_PADDING_SIZE;
13135 frag_wane (next_fragP);
13136 }
13137 else
13138 max_prefix_length
13139 += next_fragP->tc_frag_data.max_bytes;
13140 }
13141 else if ((TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13142 == BRANCH_PADDING)
13143 || (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13144 == FUSED_JCC_PADDING))
13145 {
13146 /* Stop at BRANCH_PADDING and FUSED_JCC_PADDING. */
13147 fragP->tc_frag_data.u.padding_fragP = next_fragP;
13148 break;
13149 }
13150 else
13151 /* Stop for other rs_machine_dependent frags. */
13152 break;
13153 }
13154
13155 fragP->tc_frag_data.max_prefix_length = max_prefix_length;
13156
13157 /* Skip to the next frag. */
13158 fragP = next_fragP;
13159 }
13160}
13161
13162/* Compute padding size for
13163
13164 FUSED_JCC_PADDING
13165 CMP like instruction
13166 BRANCH_PADDING
13167 COND_JUMP/UNCOND_JUMP
13168
13169 or
13170
13171 BRANCH_PADDING
13172 COND_JUMP/UNCOND_JUMP
13173 */
13174
13175static int
13176i386_branch_padding_size (fragS *fragP, offsetT address)
13177{
13178 unsigned int offset, size, padding_size;
13179 fragS *branch_fragP = fragP->tc_frag_data.u.branch_fragP;
13180
13181 /* The start address of the BRANCH_PADDING or FUSED_JCC_PADDING frag. */
13182 if (!address)
13183 address = fragP->fr_address;
13184 address += fragP->fr_fix;
13185
13186 /* CMP like instrunction size. */
13187 size = fragP->tc_frag_data.cmp_size;
13188
13189 /* The base size of the branch frag. */
13190 size += branch_fragP->fr_fix;
13191
13192 /* Add opcode and displacement bytes for the rs_machine_dependent
13193 branch frag. */
13194 if (branch_fragP->fr_type == rs_machine_dependent)
13195 size += md_relax_table[branch_fragP->fr_subtype].rlx_length;
13196
13197 /* Check if branch is within boundary and doesn't end at the last
13198 byte. */
13199 offset = address & ((1U << align_branch_power) - 1);
13200 if ((offset + size) >= (1U << align_branch_power))
13201 /* Padding needed to avoid crossing boundary. */
13202 padding_size = (1U << align_branch_power) - offset;
13203 else
13204 /* No padding needed. */
13205 padding_size = 0;
13206
13207 /* The return value may be saved in tc_frag_data.length which is
13208 unsigned byte. */
13209 if (!fits_in_unsigned_byte (padding_size))
13210 abort ();
13211
13212 return padding_size;
13213}
13214
13215/* i386_generic_table_relax_frag()
13216
13217 Handle BRANCH_PADDING, BRANCH_PREFIX and FUSED_JCC_PADDING frags to
13218 grow/shrink padding to align branch frags. Hand others to
13219 relax_frag(). */
13220
13221long
13222i386_generic_table_relax_frag (segT segment, fragS *fragP, long stretch)
13223{
13224 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
13225 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
13226 {
13227 long padding_size = i386_branch_padding_size (fragP, 0);
13228 long grow = padding_size - fragP->tc_frag_data.length;
13229
13230 /* When the BRANCH_PREFIX frag is used, the computed address
13231 must match the actual address and there should be no padding. */
13232 if (fragP->tc_frag_data.padding_address
13233 && (fragP->tc_frag_data.padding_address != fragP->fr_address
13234 || padding_size))
13235 abort ();
13236
13237 /* Update the padding size. */
13238 if (grow)
13239 fragP->tc_frag_data.length = padding_size;
13240
13241 return grow;
13242 }
13243 else if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
13244 {
13245 fragS *padding_fragP, *next_fragP;
13246 long padding_size, left_size, last_size;
13247
13248 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
13249 if (!padding_fragP)
13250 /* Use the padding set by the leading BRANCH_PREFIX frag. */
13251 return (fragP->tc_frag_data.length
13252 - fragP->tc_frag_data.last_length);
13253
13254 /* Compute the relative address of the padding frag in the very
13255 first time where the BRANCH_PREFIX frag sizes are zero. */
13256 if (!fragP->tc_frag_data.padding_address)
13257 fragP->tc_frag_data.padding_address
13258 = padding_fragP->fr_address - (fragP->fr_address - stretch);
13259
13260 /* First update the last length from the previous interation. */
13261 left_size = fragP->tc_frag_data.prefix_length;
13262 for (next_fragP = fragP;
13263 next_fragP != padding_fragP;
13264 next_fragP = next_fragP->fr_next)
13265 if (next_fragP->fr_type == rs_machine_dependent
13266 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13267 == BRANCH_PREFIX))
13268 {
13269 if (left_size)
13270 {
13271 int max = next_fragP->tc_frag_data.max_bytes;
13272 if (max)
13273 {
13274 int size;
13275 if (max > left_size)
13276 size = left_size;
13277 else
13278 size = max;
13279 left_size -= size;
13280 next_fragP->tc_frag_data.last_length = size;
13281 }
13282 }
13283 else
13284 next_fragP->tc_frag_data.last_length = 0;
13285 }
13286
13287 /* Check the padding size for the padding frag. */
13288 padding_size = i386_branch_padding_size
13289 (padding_fragP, (fragP->fr_address
13290 + fragP->tc_frag_data.padding_address));
13291
13292 last_size = fragP->tc_frag_data.prefix_length;
13293 /* Check if there is change from the last interation. */
13294 if (padding_size == last_size)
13295 {
13296 /* Update the expected address of the padding frag. */
13297 padding_fragP->tc_frag_data.padding_address
13298 = (fragP->fr_address + padding_size
13299 + fragP->tc_frag_data.padding_address);
13300 return 0;
13301 }
13302
13303 if (padding_size > fragP->tc_frag_data.max_prefix_length)
13304 {
13305 /* No padding if there is no sufficient room. Clear the
13306 expected address of the padding frag. */
13307 padding_fragP->tc_frag_data.padding_address = 0;
13308 padding_size = 0;
13309 }
13310 else
13311 /* Store the expected address of the padding frag. */
13312 padding_fragP->tc_frag_data.padding_address
13313 = (fragP->fr_address + padding_size
13314 + fragP->tc_frag_data.padding_address);
13315
13316 fragP->tc_frag_data.prefix_length = padding_size;
13317
13318 /* Update the length for the current interation. */
13319 left_size = padding_size;
13320 for (next_fragP = fragP;
13321 next_fragP != padding_fragP;
13322 next_fragP = next_fragP->fr_next)
13323 if (next_fragP->fr_type == rs_machine_dependent
13324 && (TYPE_FROM_RELAX_STATE (next_fragP->fr_subtype)
13325 == BRANCH_PREFIX))
13326 {
13327 if (left_size)
13328 {
13329 int max = next_fragP->tc_frag_data.max_bytes;
13330 if (max)
13331 {
13332 int size;
13333 if (max > left_size)
13334 size = left_size;
13335 else
13336 size = max;
13337 left_size -= size;
13338 next_fragP->tc_frag_data.length = size;
13339 }
13340 }
13341 else
13342 next_fragP->tc_frag_data.length = 0;
13343 }
13344
13345 return (fragP->tc_frag_data.length
13346 - fragP->tc_frag_data.last_length);
13347 }
13348 return relax_frag (segment, fragP, stretch);
13349}
13350
ee7fcc42
AM
13351/* md_estimate_size_before_relax()
13352
13353 Called just before relax() for rs_machine_dependent frags. The x86
13354 assembler uses these frags to handle variable size jump
13355 instructions.
13356
13357 Any symbol that is now undefined will not become defined.
13358 Return the correct fr_subtype in the frag.
13359 Return the initial "guess for variable size of frag" to caller.
13360 The guess is actually the growth beyond the fixed part. Whatever
13361 we do to grow the fixed or variable part contributes to our
13362 returned value. */
13363
252b5132 13364int
7016a5d5 13365md_estimate_size_before_relax (fragS *fragP, segT segment)
252b5132 13366{
e379e5f3
L
13367 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
13368 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX
13369 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING)
13370 {
13371 i386_classify_machine_dependent_frag (fragP);
13372 return fragP->tc_frag_data.length;
13373 }
13374
252b5132 13375 /* We've already got fragP->fr_subtype right; all we have to do is
b98ef147
AM
13376 check for un-relaxable symbols. On an ELF system, we can't relax
13377 an externally visible symbol, because it may be overridden by a
13378 shared library. */
13379 if (S_GET_SEGMENT (fragP->fr_symbol) != segment
6d249963 13380#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 13381 || (IS_ELF
8dcea932
L
13382 && !elf_symbol_resolved_in_segment_p (fragP->fr_symbol,
13383 fragP->fr_var))
fbeb56a4
DK
13384#endif
13385#if defined (OBJ_COFF) && defined (TE_PE)
7ab9ffdd 13386 || (OUTPUT_FLAVOR == bfd_target_coff_flavour
fbeb56a4 13387 && S_IS_WEAK (fragP->fr_symbol))
b98ef147
AM
13388#endif
13389 )
252b5132 13390 {
b98ef147
AM
13391 /* Symbol is undefined in this segment, or we need to keep a
13392 reloc so that weak symbols can be overridden. */
13393 int size = (fragP->fr_subtype & CODE16) ? 2 : 4;
f86103b7 13394 enum bfd_reloc_code_real reloc_type;
ee7fcc42
AM
13395 unsigned char *opcode;
13396 int old_fr_fix;
eb19308f 13397 fixS *fixP = NULL;
f6af82bd 13398
ee7fcc42 13399 if (fragP->fr_var != NO_RELOC)
1e9cc1c2 13400 reloc_type = (enum bfd_reloc_code_real) fragP->fr_var;
b98ef147 13401 else if (size == 2)
f6af82bd 13402 reloc_type = BFD_RELOC_16_PCREL;
bd7ab16b 13403#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
1ef3994a
JB
13404 else if (fragP->tc_frag_data.code64 && fragP->fr_offset == 0
13405 && need_plt32_p (fragP->fr_symbol))
bd7ab16b
L
13406 reloc_type = BFD_RELOC_X86_64_PLT32;
13407#endif
f6af82bd
AM
13408 else
13409 reloc_type = BFD_RELOC_32_PCREL;
252b5132 13410
ee7fcc42
AM
13411 old_fr_fix = fragP->fr_fix;
13412 opcode = (unsigned char *) fragP->fr_opcode;
13413
fddf5b5b 13414 switch (TYPE_FROM_RELAX_STATE (fragP->fr_subtype))
252b5132 13415 {
fddf5b5b
AM
13416 case UNCOND_JUMP:
13417 /* Make jmp (0xeb) a (d)word displacement jump. */
47926f60 13418 opcode[0] = 0xe9;
252b5132 13419 fragP->fr_fix += size;
eb19308f
JB
13420 fixP = fix_new (fragP, old_fr_fix, size,
13421 fragP->fr_symbol,
13422 fragP->fr_offset, 1,
13423 reloc_type);
252b5132
RH
13424 break;
13425
fddf5b5b 13426 case COND_JUMP86:
412167cb
AM
13427 if (size == 2
13428 && (!no_cond_jump_promotion || fragP->fr_var != NO_RELOC))
fddf5b5b
AM
13429 {
13430 /* Negate the condition, and branch past an
13431 unconditional jump. */
13432 opcode[0] ^= 1;
13433 opcode[1] = 3;
13434 /* Insert an unconditional jump. */
13435 opcode[2] = 0xe9;
13436 /* We added two extra opcode bytes, and have a two byte
13437 offset. */
13438 fragP->fr_fix += 2 + 2;
062cd5e7
AS
13439 fix_new (fragP, old_fr_fix + 2, 2,
13440 fragP->fr_symbol,
13441 fragP->fr_offset, 1,
13442 reloc_type);
fddf5b5b
AM
13443 break;
13444 }
13445 /* Fall through. */
13446
13447 case COND_JUMP:
412167cb
AM
13448 if (no_cond_jump_promotion && fragP->fr_var == NO_RELOC)
13449 {
13450 fragP->fr_fix += 1;
3e02c1cc
AM
13451 fixP = fix_new (fragP, old_fr_fix, 1,
13452 fragP->fr_symbol,
13453 fragP->fr_offset, 1,
13454 BFD_RELOC_8_PCREL);
13455 fixP->fx_signed = 1;
412167cb
AM
13456 break;
13457 }
93c2a809 13458
24eab124 13459 /* This changes the byte-displacement jump 0x7N
fddf5b5b 13460 to the (d)word-displacement jump 0x0f,0x8N. */
252b5132 13461 opcode[1] = opcode[0] + 0x10;
f6af82bd 13462 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
47926f60
KH
13463 /* We've added an opcode byte. */
13464 fragP->fr_fix += 1 + size;
eb19308f
JB
13465 fixP = fix_new (fragP, old_fr_fix + 1, size,
13466 fragP->fr_symbol,
13467 fragP->fr_offset, 1,
13468 reloc_type);
252b5132 13469 break;
fddf5b5b
AM
13470
13471 default:
13472 BAD_CASE (fragP->fr_subtype);
13473 break;
252b5132 13474 }
eb19308f
JB
13475
13476 /* All jumps handled here are signed, but don't unconditionally use a
13477 signed limit check for 32 and 16 bit jumps as we want to allow wrap
13478 around at 4G (outside of 64-bit mode) and 64k. */
13479 if (size == 4 && flag_code == CODE_64BIT)
13480 fixP->fx_signed = 1;
13481
252b5132 13482 frag_wane (fragP);
ee7fcc42 13483 return fragP->fr_fix - old_fr_fix;
252b5132 13484 }
93c2a809 13485
93c2a809
AM
13486 /* Guess size depending on current relax state. Initially the relax
13487 state will correspond to a short jump and we return 1, because
13488 the variable part of the frag (the branch offset) is one byte
13489 long. However, we can relax a section more than once and in that
13490 case we must either set fr_subtype back to the unrelaxed state,
13491 or return the value for the appropriate branch. */
13492 return md_relax_table[fragP->fr_subtype].rlx_length;
ee7fcc42
AM
13493}
13494
47926f60
KH
13495/* Called after relax() is finished.
13496
13497 In: Address of frag.
13498 fr_type == rs_machine_dependent.
13499 fr_subtype is what the address relaxed to.
13500
13501 Out: Any fixSs and constants are set up.
13502 Caller will turn frag into a ".space 0". */
13503
252b5132 13504void
7016a5d5
TG
13505md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec ATTRIBUTE_UNUSED,
13506 fragS *fragP)
252b5132 13507{
29b0f896 13508 unsigned char *opcode;
252b5132 13509 unsigned char *where_to_put_displacement = NULL;
847f7ad4
AM
13510 offsetT target_address;
13511 offsetT opcode_address;
252b5132 13512 unsigned int extension = 0;
847f7ad4 13513 offsetT displacement_from_opcode_start;
252b5132 13514
e379e5f3
L
13515 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PADDING
13516 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == FUSED_JCC_PADDING
13517 || TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
13518 {
13519 /* Generate nop padding. */
13520 unsigned int size = fragP->tc_frag_data.length;
13521 if (size)
13522 {
13523 if (size > fragP->tc_frag_data.max_bytes)
13524 abort ();
13525
13526 if (flag_debug)
13527 {
13528 const char *msg;
13529 const char *branch = "branch";
13530 const char *prefix = "";
13531 fragS *padding_fragP;
13532 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype)
13533 == BRANCH_PREFIX)
13534 {
13535 padding_fragP = fragP->tc_frag_data.u.padding_fragP;
13536 switch (fragP->tc_frag_data.default_prefix)
13537 {
13538 default:
13539 abort ();
13540 break;
13541 case CS_PREFIX_OPCODE:
13542 prefix = " cs";
13543 break;
13544 case DS_PREFIX_OPCODE:
13545 prefix = " ds";
13546 break;
13547 case ES_PREFIX_OPCODE:
13548 prefix = " es";
13549 break;
13550 case FS_PREFIX_OPCODE:
13551 prefix = " fs";
13552 break;
13553 case GS_PREFIX_OPCODE:
13554 prefix = " gs";
13555 break;
13556 case SS_PREFIX_OPCODE:
13557 prefix = " ss";
13558 break;
13559 }
13560 if (padding_fragP)
13561 msg = _("%s:%u: add %d%s at 0x%llx to align "
13562 "%s within %d-byte boundary\n");
13563 else
13564 msg = _("%s:%u: add additional %d%s at 0x%llx to "
13565 "align %s within %d-byte boundary\n");
13566 }
13567 else
13568 {
13569 padding_fragP = fragP;
13570 msg = _("%s:%u: add %d%s-byte nop at 0x%llx to align "
13571 "%s within %d-byte boundary\n");
13572 }
13573
13574 if (padding_fragP)
13575 switch (padding_fragP->tc_frag_data.branch_type)
13576 {
13577 case align_branch_jcc:
13578 branch = "jcc";
13579 break;
13580 case align_branch_fused:
13581 branch = "fused jcc";
13582 break;
13583 case align_branch_jmp:
13584 branch = "jmp";
13585 break;
13586 case align_branch_call:
13587 branch = "call";
13588 break;
13589 case align_branch_indirect:
13590 branch = "indiret branch";
13591 break;
13592 case align_branch_ret:
13593 branch = "ret";
13594 break;
13595 default:
13596 break;
13597 }
13598
13599 fprintf (stdout, msg,
13600 fragP->fr_file, fragP->fr_line, size, prefix,
13601 (long long) fragP->fr_address, branch,
13602 1 << align_branch_power);
13603 }
13604 if (TYPE_FROM_RELAX_STATE (fragP->fr_subtype) == BRANCH_PREFIX)
13605 memset (fragP->fr_opcode,
13606 fragP->tc_frag_data.default_prefix, size);
13607 else
13608 i386_generate_nops (fragP, (char *) fragP->fr_opcode,
13609 size, 0);
13610 fragP->fr_fix += size;
13611 }
13612 return;
13613 }
13614
252b5132
RH
13615 opcode = (unsigned char *) fragP->fr_opcode;
13616
47926f60 13617 /* Address we want to reach in file space. */
252b5132 13618 target_address = S_GET_VALUE (fragP->fr_symbol) + fragP->fr_offset;
252b5132 13619
47926f60 13620 /* Address opcode resides at in file space. */
252b5132
RH
13621 opcode_address = fragP->fr_address + fragP->fr_fix;
13622
47926f60 13623 /* Displacement from opcode start to fill into instruction. */
252b5132
RH
13624 displacement_from_opcode_start = target_address - opcode_address;
13625
fddf5b5b 13626 if ((fragP->fr_subtype & BIG) == 0)
252b5132 13627 {
47926f60
KH
13628 /* Don't have to change opcode. */
13629 extension = 1; /* 1 opcode + 1 displacement */
252b5132 13630 where_to_put_displacement = &opcode[1];
fddf5b5b
AM
13631 }
13632 else
13633 {
13634 if (no_cond_jump_promotion
13635 && TYPE_FROM_RELAX_STATE (fragP->fr_subtype) != UNCOND_JUMP)
4eed87de
AM
13636 as_warn_where (fragP->fr_file, fragP->fr_line,
13637 _("long jump required"));
252b5132 13638
fddf5b5b
AM
13639 switch (fragP->fr_subtype)
13640 {
13641 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG):
13642 extension = 4; /* 1 opcode + 4 displacement */
13643 opcode[0] = 0xe9;
13644 where_to_put_displacement = &opcode[1];
13645 break;
252b5132 13646
fddf5b5b
AM
13647 case ENCODE_RELAX_STATE (UNCOND_JUMP, BIG16):
13648 extension = 2; /* 1 opcode + 2 displacement */
13649 opcode[0] = 0xe9;
13650 where_to_put_displacement = &opcode[1];
13651 break;
252b5132 13652
fddf5b5b
AM
13653 case ENCODE_RELAX_STATE (COND_JUMP, BIG):
13654 case ENCODE_RELAX_STATE (COND_JUMP86, BIG):
13655 extension = 5; /* 2 opcode + 4 displacement */
13656 opcode[1] = opcode[0] + 0x10;
13657 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
13658 where_to_put_displacement = &opcode[2];
13659 break;
252b5132 13660
fddf5b5b
AM
13661 case ENCODE_RELAX_STATE (COND_JUMP, BIG16):
13662 extension = 3; /* 2 opcode + 2 displacement */
13663 opcode[1] = opcode[0] + 0x10;
13664 opcode[0] = TWO_BYTE_OPCODE_ESCAPE;
13665 where_to_put_displacement = &opcode[2];
13666 break;
252b5132 13667
fddf5b5b
AM
13668 case ENCODE_RELAX_STATE (COND_JUMP86, BIG16):
13669 extension = 4;
13670 opcode[0] ^= 1;
13671 opcode[1] = 3;
13672 opcode[2] = 0xe9;
13673 where_to_put_displacement = &opcode[3];
13674 break;
13675
13676 default:
13677 BAD_CASE (fragP->fr_subtype);
13678 break;
13679 }
252b5132 13680 }
fddf5b5b 13681
7b81dfbb
AJ
13682 /* If size if less then four we are sure that the operand fits,
13683 but if it's 4, then it could be that the displacement is larger
13684 then -/+ 2GB. */
13685 if (DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype) == 4
13686 && object_64bit
13687 && ((addressT) (displacement_from_opcode_start - extension
4eed87de
AM
13688 + ((addressT) 1 << 31))
13689 > (((addressT) 2 << 31) - 1)))
7b81dfbb
AJ
13690 {
13691 as_bad_where (fragP->fr_file, fragP->fr_line,
13692 _("jump target out of range"));
13693 /* Make us emit 0. */
13694 displacement_from_opcode_start = extension;
13695 }
47926f60 13696 /* Now put displacement after opcode. */
252b5132
RH
13697 md_number_to_chars ((char *) where_to_put_displacement,
13698 (valueT) (displacement_from_opcode_start - extension),
fddf5b5b 13699 DISP_SIZE_FROM_RELAX_STATE (fragP->fr_subtype));
252b5132
RH
13700 fragP->fr_fix += extension;
13701}
13702\f
7016a5d5 13703/* Apply a fixup (fixP) to segment data, once it has been determined
252b5132
RH
13704 by our caller that we have all the info we need to fix it up.
13705
7016a5d5
TG
13706 Parameter valP is the pointer to the value of the bits.
13707
252b5132
RH
13708 On the 386, immediates, displacements, and data pointers are all in
13709 the same (little-endian) format, so we don't need to care about which
13710 we are handling. */
13711
94f592af 13712void
7016a5d5 13713md_apply_fix (fixS *fixP, valueT *valP, segT seg ATTRIBUTE_UNUSED)
252b5132 13714{
94f592af 13715 char *p = fixP->fx_where + fixP->fx_frag->fr_literal;
c6682705 13716 valueT value = *valP;
252b5132 13717
f86103b7 13718#if !defined (TE_Mach)
93382f6d
AM
13719 if (fixP->fx_pcrel)
13720 {
13721 switch (fixP->fx_r_type)
13722 {
5865bb77
ILT
13723 default:
13724 break;
13725
d6ab8113
JB
13726 case BFD_RELOC_64:
13727 fixP->fx_r_type = BFD_RELOC_64_PCREL;
13728 break;
93382f6d 13729 case BFD_RELOC_32:
ae8887b5 13730 case BFD_RELOC_X86_64_32S:
93382f6d
AM
13731 fixP->fx_r_type = BFD_RELOC_32_PCREL;
13732 break;
13733 case BFD_RELOC_16:
13734 fixP->fx_r_type = BFD_RELOC_16_PCREL;
13735 break;
13736 case BFD_RELOC_8:
13737 fixP->fx_r_type = BFD_RELOC_8_PCREL;
13738 break;
13739 }
13740 }
252b5132 13741
a161fe53 13742 if (fixP->fx_addsy != NULL
31312f95 13743 && (fixP->fx_r_type == BFD_RELOC_32_PCREL
d6ab8113 13744 || fixP->fx_r_type == BFD_RELOC_64_PCREL
31312f95 13745 || fixP->fx_r_type == BFD_RELOC_16_PCREL
d258b828 13746 || fixP->fx_r_type == BFD_RELOC_8_PCREL)
31312f95 13747 && !use_rela_relocations)
252b5132 13748 {
31312f95
AM
13749 /* This is a hack. There should be a better way to handle this.
13750 This covers for the fact that bfd_install_relocation will
13751 subtract the current location (for partial_inplace, PC relative
13752 relocations); see more below. */
252b5132 13753#ifndef OBJ_AOUT
718ddfc0 13754 if (IS_ELF
252b5132
RH
13755#ifdef TE_PE
13756 || OUTPUT_FLAVOR == bfd_target_coff_flavour
13757#endif
13758 )
13759 value += fixP->fx_where + fixP->fx_frag->fr_address;
13760#endif
13761#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 13762 if (IS_ELF)
252b5132 13763 {
6539b54b 13764 segT sym_seg = S_GET_SEGMENT (fixP->fx_addsy);
2f66722d 13765
6539b54b 13766 if ((sym_seg == seg
2f66722d 13767 || (symbol_section_p (fixP->fx_addsy)
6539b54b 13768 && sym_seg != absolute_section))
af65af87 13769 && !generic_force_reloc (fixP))
2f66722d
AM
13770 {
13771 /* Yes, we add the values in twice. This is because
6539b54b
AM
13772 bfd_install_relocation subtracts them out again. I think
13773 bfd_install_relocation is broken, but I don't dare change
2f66722d
AM
13774 it. FIXME. */
13775 value += fixP->fx_where + fixP->fx_frag->fr_address;
13776 }
252b5132
RH
13777 }
13778#endif
13779#if defined (OBJ_COFF) && defined (TE_PE)
977cdf5a
NC
13780 /* For some reason, the PE format does not store a
13781 section address offset for a PC relative symbol. */
13782 if (S_GET_SEGMENT (fixP->fx_addsy) != seg
7be1c489 13783 || S_IS_WEAK (fixP->fx_addsy))
252b5132
RH
13784 value += md_pcrel_from (fixP);
13785#endif
13786 }
fbeb56a4 13787#if defined (OBJ_COFF) && defined (TE_PE)
f01c1a09
NC
13788 if (fixP->fx_addsy != NULL
13789 && S_IS_WEAK (fixP->fx_addsy)
13790 /* PR 16858: Do not modify weak function references. */
13791 && ! fixP->fx_pcrel)
fbeb56a4 13792 {
296a8689
NC
13793#if !defined (TE_PEP)
13794 /* For x86 PE weak function symbols are neither PC-relative
13795 nor do they set S_IS_FUNCTION. So the only reliable way
13796 to detect them is to check the flags of their containing
13797 section. */
13798 if (S_GET_SEGMENT (fixP->fx_addsy) != NULL
13799 && S_GET_SEGMENT (fixP->fx_addsy)->flags & SEC_CODE)
13800 ;
13801 else
13802#endif
fbeb56a4
DK
13803 value -= S_GET_VALUE (fixP->fx_addsy);
13804 }
13805#endif
252b5132
RH
13806
13807 /* Fix a few things - the dynamic linker expects certain values here,
0234cb7c 13808 and we must not disappoint it. */
252b5132 13809#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
718ddfc0 13810 if (IS_ELF && fixP->fx_addsy)
47926f60
KH
13811 switch (fixP->fx_r_type)
13812 {
13813 case BFD_RELOC_386_PLT32:
3e73aa7c 13814 case BFD_RELOC_X86_64_PLT32:
b9519cfe
L
13815 /* Make the jump instruction point to the address of the operand.
13816 At runtime we merely add the offset to the actual PLT entry.
13817 NB: Subtract the offset size only for jump instructions. */
13818 if (fixP->fx_pcrel)
13819 value = -4;
47926f60 13820 break;
31312f95 13821
13ae64f3
JJ
13822 case BFD_RELOC_386_TLS_GD:
13823 case BFD_RELOC_386_TLS_LDM:
13ae64f3 13824 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
13825 case BFD_RELOC_386_TLS_IE:
13826 case BFD_RELOC_386_TLS_GOTIE:
67a4f2b7 13827 case BFD_RELOC_386_TLS_GOTDESC:
bffbf940
JJ
13828 case BFD_RELOC_X86_64_TLSGD:
13829 case BFD_RELOC_X86_64_TLSLD:
13830 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7 13831 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
00f7efb6
JJ
13832 value = 0; /* Fully resolved at runtime. No addend. */
13833 /* Fallthrough */
13834 case BFD_RELOC_386_TLS_LE:
13835 case BFD_RELOC_386_TLS_LDO_32:
13836 case BFD_RELOC_386_TLS_LE_32:
13837 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 13838 case BFD_RELOC_X86_64_DTPOFF64:
00f7efb6 13839 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113 13840 case BFD_RELOC_X86_64_TPOFF64:
00f7efb6
JJ
13841 S_SET_THREAD_LOCAL (fixP->fx_addsy);
13842 break;
13843
67a4f2b7
AO
13844 case BFD_RELOC_386_TLS_DESC_CALL:
13845 case BFD_RELOC_X86_64_TLSDESC_CALL:
13846 value = 0; /* Fully resolved at runtime. No addend. */
13847 S_SET_THREAD_LOCAL (fixP->fx_addsy);
13848 fixP->fx_done = 0;
13849 return;
13850
47926f60
KH
13851 case BFD_RELOC_VTABLE_INHERIT:
13852 case BFD_RELOC_VTABLE_ENTRY:
13853 fixP->fx_done = 0;
94f592af 13854 return;
47926f60
KH
13855
13856 default:
13857 break;
13858 }
13859#endif /* defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) */
a442cac5
JB
13860
13861 /* If not 64bit, massage value, to account for wraparound when !BFD64. */
13862 if (!object_64bit)
13863 value = extend_to_32bit_address (value);
13864
c6682705 13865 *valP = value;
f86103b7 13866#endif /* !defined (TE_Mach) */
3e73aa7c 13867
3e73aa7c 13868 /* Are we finished with this relocation now? */
c6682705 13869 if (fixP->fx_addsy == NULL)
b8188555
JB
13870 {
13871 fixP->fx_done = 1;
13872 switch (fixP->fx_r_type)
13873 {
13874 case BFD_RELOC_X86_64_32S:
13875 fixP->fx_signed = 1;
13876 break;
13877
13878 default:
13879 break;
13880 }
13881 }
fbeb56a4
DK
13882#if defined (OBJ_COFF) && defined (TE_PE)
13883 else if (fixP->fx_addsy != NULL && S_IS_WEAK (fixP->fx_addsy))
13884 {
13885 fixP->fx_done = 0;
13886 /* Remember value for tc_gen_reloc. */
13887 fixP->fx_addnumber = value;
13888 /* Clear out the frag for now. */
13889 value = 0;
13890 }
13891#endif
3e73aa7c
JH
13892 else if (use_rela_relocations)
13893 {
46fb6d5a
JB
13894 if (!disallow_64bit_reloc || fixP->fx_r_type == NO_RELOC)
13895 fixP->fx_no_overflow = 1;
062cd5e7
AS
13896 /* Remember value for tc_gen_reloc. */
13897 fixP->fx_addnumber = value;
3e73aa7c
JH
13898 value = 0;
13899 }
f86103b7 13900
94f592af 13901 md_number_to_chars (p, value, fixP->fx_size);
252b5132 13902}
252b5132 13903\f
6d4af3c2 13904const char *
499ac353 13905md_atof (int type, char *litP, int *sizeP)
252b5132 13906{
499ac353
NC
13907 /* This outputs the LITTLENUMs in REVERSE order;
13908 in accord with the bigendian 386. */
5b7c81bd 13909 return ieee_md_atof (type, litP, sizeP, false);
252b5132
RH
13910}
13911\f
2d545b82 13912static char output_invalid_buf[sizeof (unsigned char) * 2 + 6];
252b5132 13913
252b5132 13914static char *
e3bb37b5 13915output_invalid (int c)
252b5132 13916{
3882b010 13917 if (ISPRINT (c))
f9f21a03
L
13918 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
13919 "'%c'", c);
252b5132 13920 else
f9f21a03 13921 snprintf (output_invalid_buf, sizeof (output_invalid_buf),
2d545b82 13922 "(0x%x)", (unsigned char) c);
252b5132
RH
13923 return output_invalid_buf;
13924}
13925
8a6fb3f9
JB
13926/* Verify that @r can be used in the current context. */
13927
5b7c81bd 13928static bool check_register (const reg_entry *r)
8a6fb3f9
JB
13929{
13930 if (allow_pseudo_reg)
5b7c81bd 13931 return true;
8a6fb3f9
JB
13932
13933 if (operand_type_all_zero (&r->reg_type))
5b7c81bd 13934 return false;
8a6fb3f9
JB
13935
13936 if ((r->reg_type.bitfield.dword
13937 || (r->reg_type.bitfield.class == SReg && r->reg_num > 3)
13938 || r->reg_type.bitfield.class == RegCR
22e00a3f 13939 || r->reg_type.bitfield.class == RegDR)
8a6fb3f9 13940 && !cpu_arch_flags.bitfield.cpui386)
5b7c81bd 13941 return false;
8a6fb3f9 13942
22e00a3f
JB
13943 if (r->reg_type.bitfield.class == RegTR
13944 && (flag_code == CODE_64BIT
13945 || !cpu_arch_flags.bitfield.cpui386
13946 || cpu_arch_isa_flags.bitfield.cpui586
13947 || cpu_arch_isa_flags.bitfield.cpui686))
5b7c81bd 13948 return false;
22e00a3f 13949
8a6fb3f9 13950 if (r->reg_type.bitfield.class == RegMMX && !cpu_arch_flags.bitfield.cpummx)
5b7c81bd 13951 return false;
8a6fb3f9
JB
13952
13953 if (!cpu_arch_flags.bitfield.cpuavx512f)
13954 {
13955 if (r->reg_type.bitfield.zmmword
13956 || r->reg_type.bitfield.class == RegMask)
5b7c81bd 13957 return false;
8a6fb3f9
JB
13958
13959 if (!cpu_arch_flags.bitfield.cpuavx)
13960 {
13961 if (r->reg_type.bitfield.ymmword)
5b7c81bd 13962 return false;
8a6fb3f9
JB
13963
13964 if (!cpu_arch_flags.bitfield.cpusse && r->reg_type.bitfield.xmmword)
5b7c81bd 13965 return false;
8a6fb3f9
JB
13966 }
13967 }
13968
4fc85f37
JB
13969 if (vector_size < VSZ512 && r->reg_type.bitfield.zmmword)
13970 return false;
13971
13972 if (vector_size < VSZ256 && r->reg_type.bitfield.ymmword)
13973 return false;
13974
260cd341
LC
13975 if (r->reg_type.bitfield.tmmword
13976 && (!cpu_arch_flags.bitfield.cpuamx_tile
13977 || flag_code != CODE_64BIT))
5b7c81bd 13978 return false;
260cd341 13979
8a6fb3f9 13980 if (r->reg_type.bitfield.class == RegBND && !cpu_arch_flags.bitfield.cpumpx)
5b7c81bd 13981 return false;
8a6fb3f9
JB
13982
13983 /* Don't allow fake index register unless allow_index_reg isn't 0. */
13984 if (!allow_index_reg && r->reg_num == RegIZ)
5b7c81bd 13985 return false;
8a6fb3f9
JB
13986
13987 /* Upper 16 vector registers are only available with VREX in 64bit
13988 mode, and require EVEX encoding. */
13989 if (r->reg_flags & RegVRex)
13990 {
13991 if (!cpu_arch_flags.bitfield.cpuavx512f
13992 || flag_code != CODE_64BIT)
5b7c81bd 13993 return false;
8a6fb3f9 13994
da4977e0
JB
13995 if (i.vec_encoding == vex_encoding_default)
13996 i.vec_encoding = vex_encoding_evex;
13997 else if (i.vec_encoding != vex_encoding_evex)
13998 i.vec_encoding = vex_encoding_error;
8a6fb3f9
JB
13999 }
14000
14001 if (((r->reg_flags & (RegRex64 | RegRex)) || r->reg_type.bitfield.qword)
0ff3b7d0
JB
14002 && (!cpu_arch_flags.bitfield.cpulm
14003 || r->reg_type.bitfield.class != RegCR
14004 || dot_insn ())
8a6fb3f9 14005 && flag_code != CODE_64BIT)
5b7c81bd 14006 return false;
8a6fb3f9
JB
14007
14008 if (r->reg_type.bitfield.class == SReg && r->reg_num == RegFlat
14009 && !intel_syntax)
5b7c81bd 14010 return false;
8a6fb3f9 14011
5b7c81bd 14012 return true;
8a6fb3f9
JB
14013}
14014
af6bdddf 14015/* REG_STRING starts *before* REGISTER_PREFIX. */
252b5132
RH
14016
14017static const reg_entry *
74e05e01 14018parse_real_register (const char *reg_string, char **end_op)
252b5132 14019{
74e05e01 14020 const char *s = reg_string;
af6bdddf 14021 char *p;
252b5132
RH
14022 char reg_name_given[MAX_REG_NAME_SIZE + 1];
14023 const reg_entry *r;
14024
14025 /* Skip possible REGISTER_PREFIX and possible whitespace. */
14026 if (*s == REGISTER_PREFIX)
14027 ++s;
14028
14029 if (is_space_char (*s))
14030 ++s;
14031
14032 p = reg_name_given;
af6bdddf 14033 while ((*p++ = register_chars[(unsigned char) *s]) != '\0')
252b5132
RH
14034 {
14035 if (p >= reg_name_given + MAX_REG_NAME_SIZE)
af6bdddf
AM
14036 return (const reg_entry *) NULL;
14037 s++;
252b5132
RH
14038 }
14039
4795cd4a 14040 if (is_part_of_name (*s))
6588847e
DN
14041 return (const reg_entry *) NULL;
14042
74e05e01 14043 *end_op = (char *) s;
252b5132 14044
629310ab 14045 r = (const reg_entry *) str_hash_find (reg_hash, reg_name_given);
252b5132 14046
5f47d35b 14047 /* Handle floating point regs, allowing spaces in the (i) part. */
6288d05f 14048 if (r == reg_st0)
5f47d35b 14049 {
0e0eea78
JB
14050 if (!cpu_arch_flags.bitfield.cpu8087
14051 && !cpu_arch_flags.bitfield.cpu287
af32b722
JB
14052 && !cpu_arch_flags.bitfield.cpu387
14053 && !allow_pseudo_reg)
0e0eea78
JB
14054 return (const reg_entry *) NULL;
14055
5f47d35b
AM
14056 if (is_space_char (*s))
14057 ++s;
14058 if (*s == '(')
14059 {
af6bdddf 14060 ++s;
5f47d35b
AM
14061 if (is_space_char (*s))
14062 ++s;
14063 if (*s >= '0' && *s <= '7')
14064 {
db557034 14065 int fpr = *s - '0';
af6bdddf 14066 ++s;
5f47d35b
AM
14067 if (is_space_char (*s))
14068 ++s;
14069 if (*s == ')')
14070 {
74e05e01 14071 *end_op = (char *) s + 1;
6288d05f 14072 know (r[fpr].reg_num == fpr);
db557034 14073 return r + fpr;
5f47d35b 14074 }
5f47d35b 14075 }
47926f60 14076 /* We have "%st(" then garbage. */
5f47d35b
AM
14077 return (const reg_entry *) NULL;
14078 }
14079 }
14080
8a6fb3f9 14081 return r && check_register (r) ? r : NULL;
252b5132 14082}
4d1bb795
JB
14083
14084/* REG_STRING starts *before* REGISTER_PREFIX. */
14085
14086static const reg_entry *
4f081312 14087parse_register (const char *reg_string, char **end_op)
4d1bb795
JB
14088{
14089 const reg_entry *r;
14090
14091 if (*reg_string == REGISTER_PREFIX || allow_naked_reg)
14092 r = parse_real_register (reg_string, end_op);
14093 else
14094 r = NULL;
14095 if (!r)
14096 {
14097 char *save = input_line_pointer;
4f081312 14098 char *buf = xstrdup (reg_string), *name;
4d1bb795
JB
14099 symbolS *symbolP;
14100
4f081312
JB
14101 input_line_pointer = buf;
14102 get_symbol_name (&name);
14103 symbolP = symbol_find (name);
d50c498a 14104 while (symbolP && symbol_equated_p (symbolP))
64d23078
JB
14105 {
14106 const expressionS *e = symbol_get_value_expression(symbolP);
14107
d50c498a 14108 if (e->X_add_number)
64d23078
JB
14109 break;
14110 symbolP = e->X_add_symbol;
14111 }
4d1bb795
JB
14112 if (symbolP && S_GET_SEGMENT (symbolP) == reg_section)
14113 {
14114 const expressionS *e = symbol_get_value_expression (symbolP);
14115
0ccade1a
JB
14116 if (e->X_op == O_register)
14117 {
14118 know (e->X_add_number >= 0
14119 && (valueT) e->X_add_number < i386_regtab_size);
14120 r = i386_regtab + e->X_add_number;
4f081312 14121 *end_op = (char *) reg_string + (input_line_pointer - buf);
0ccade1a
JB
14122 }
14123 if (r && !check_register (r))
8a6fb3f9 14124 {
3b55a1d0
JB
14125 as_bad (_("register '%s%s' cannot be used here"),
14126 register_prefix, r->reg_name);
14127 r = &bad_reg;
8a6fb3f9 14128 }
4d1bb795 14129 }
4d1bb795 14130 input_line_pointer = save;
4f081312 14131 free (buf);
4d1bb795
JB
14132 }
14133 return r;
14134}
14135
14136int
14137i386_parse_name (char *name, expressionS *e, char *nextcharP)
14138{
4faaa10f 14139 const reg_entry *r = NULL;
4d1bb795
JB
14140 char *end = input_line_pointer;
14141
6acf9130
JB
14142 /* We only know the terminating character here. It being double quote could
14143 be the closing one of a quoted symbol name, or an opening one from a
14144 following string (or another quoted symbol name). Since the latter can't
14145 be valid syntax for anything, bailing in either case is good enough. */
14146 if (*nextcharP == '"')
14147 return 0;
14148
4d1bb795 14149 *end = *nextcharP;
4faaa10f
JB
14150 if (*name == REGISTER_PREFIX || allow_naked_reg)
14151 r = parse_real_register (name, &input_line_pointer);
4d1bb795
JB
14152 if (r && end <= input_line_pointer)
14153 {
14154 *nextcharP = *input_line_pointer;
14155 *input_line_pointer = 0;
f847749a
JB
14156 e->X_op = O_register;
14157 e->X_add_number = r - i386_regtab;
4d1bb795
JB
14158 return 1;
14159 }
14160 input_line_pointer = end;
14161 *end = 0;
ee86248c 14162 return intel_syntax ? i386_intel_parse_name (name, e) : 0;
4d1bb795
JB
14163}
14164
14165void
14166md_operand (expressionS *e)
14167{
ee86248c
JB
14168 char *end;
14169 const reg_entry *r;
4d1bb795 14170
ee86248c
JB
14171 switch (*input_line_pointer)
14172 {
14173 case REGISTER_PREFIX:
14174 r = parse_real_register (input_line_pointer, &end);
4d1bb795
JB
14175 if (r)
14176 {
14177 e->X_op = O_register;
14178 e->X_add_number = r - i386_regtab;
14179 input_line_pointer = end;
14180 }
ee86248c
JB
14181 break;
14182
14183 case '[':
9c2799c2 14184 gas_assert (intel_syntax);
ee86248c
JB
14185 end = input_line_pointer++;
14186 expression (e);
14187 if (*input_line_pointer == ']')
14188 {
14189 ++input_line_pointer;
14190 e->X_op_symbol = make_expr_symbol (e);
14191 e->X_add_symbol = NULL;
14192 e->X_add_number = 0;
14193 e->X_op = O_index;
14194 }
14195 else
14196 {
14197 e->X_op = O_absent;
14198 input_line_pointer = end;
14199 }
14200 break;
4d1bb795
JB
14201 }
14202}
14203
5cc00775
JB
14204#ifdef BFD64
14205/* To maintain consistency with !BFD64 builds of gas record, whether any
14206 (binary) operator was involved in an expression. As expressions are
14207 evaluated in only 32 bits when !BFD64, we use this to decide whether to
14208 truncate results. */
14209bool i386_record_operator (operatorT op,
14210 const expressionS *left,
14211 const expressionS *right)
14212{
14213 if (op == O_absent)
14214 return false;
14215
14216 if (!left)
14217 {
14218 /* Since the expression parser applies unary operators fine to bignum
14219 operands, we don't need to be concerned of respective operands not
14220 fitting in 32 bits. */
14221 if (right->X_op == O_constant && right->X_unsigned
14222 && !fits_in_unsigned_long (right->X_add_number))
14223 return false;
14224 }
14225 /* This isn't entirely right: The pattern can also result when constant
14226 expressions are folded (e.g. 0xffffffff + 1). */
14227 else if ((left->X_op == O_constant && left->X_unsigned
14228 && !fits_in_unsigned_long (left->X_add_number))
14229 || (right->X_op == O_constant && right->X_unsigned
14230 && !fits_in_unsigned_long (right->X_add_number)))
14231 expr_mode = expr_large_value;
14232
14233 if (expr_mode != expr_large_value)
14234 expr_mode = expr_operator_present;
14235
14236 return false;
14237}
14238#endif
252b5132 14239\f
4cc782b5 14240#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
b6f8c7c4 14241const char *md_shortopts = "kVQ:sqnO::";
252b5132 14242#else
b6f8c7c4 14243const char *md_shortopts = "qnO::";
252b5132 14244#endif
6e0b89ee 14245
3e73aa7c 14246#define OPTION_32 (OPTION_MD_BASE + 0)
b3b91714
AM
14247#define OPTION_64 (OPTION_MD_BASE + 1)
14248#define OPTION_DIVIDE (OPTION_MD_BASE + 2)
9103f4f4
L
14249#define OPTION_MARCH (OPTION_MD_BASE + 3)
14250#define OPTION_MTUNE (OPTION_MD_BASE + 4)
1efbbeb4
L
14251#define OPTION_MMNEMONIC (OPTION_MD_BASE + 5)
14252#define OPTION_MSYNTAX (OPTION_MD_BASE + 6)
14253#define OPTION_MINDEX_REG (OPTION_MD_BASE + 7)
14254#define OPTION_MNAKED_REG (OPTION_MD_BASE + 8)
bd5dea88 14255#define OPTION_MRELAX_RELOCATIONS (OPTION_MD_BASE + 9)
c0f3af97 14256#define OPTION_MSSE2AVX (OPTION_MD_BASE + 10)
daf50ae7 14257#define OPTION_MSSE_CHECK (OPTION_MD_BASE + 11)
7bab8ab5
JB
14258#define OPTION_MOPERAND_CHECK (OPTION_MD_BASE + 12)
14259#define OPTION_MAVXSCALAR (OPTION_MD_BASE + 13)
14260#define OPTION_X32 (OPTION_MD_BASE + 14)
7e8b059b 14261#define OPTION_MADD_BND_PREFIX (OPTION_MD_BASE + 15)
43234a1e
L
14262#define OPTION_MEVEXLIG (OPTION_MD_BASE + 16)
14263#define OPTION_MEVEXWIG (OPTION_MD_BASE + 17)
167ad85b 14264#define OPTION_MBIG_OBJ (OPTION_MD_BASE + 18)
d1982f93 14265#define OPTION_MOMIT_LOCK_PREFIX (OPTION_MD_BASE + 19)
d3d3c6db 14266#define OPTION_MEVEXRCIG (OPTION_MD_BASE + 20)
8dcea932 14267#define OPTION_MSHARED (OPTION_MD_BASE + 21)
5db04b09
L
14268#define OPTION_MAMD64 (OPTION_MD_BASE + 22)
14269#define OPTION_MINTEL64 (OPTION_MD_BASE + 23)
e4e00185 14270#define OPTION_MFENCE_AS_LOCK_ADD (OPTION_MD_BASE + 24)
b4a3a7b4 14271#define OPTION_X86_USED_NOTE (OPTION_MD_BASE + 25)
03751133 14272#define OPTION_MVEXWIG (OPTION_MD_BASE + 26)
e379e5f3
L
14273#define OPTION_MALIGN_BRANCH_BOUNDARY (OPTION_MD_BASE + 27)
14274#define OPTION_MALIGN_BRANCH_PREFIX_SIZE (OPTION_MD_BASE + 28)
14275#define OPTION_MALIGN_BRANCH (OPTION_MD_BASE + 29)
76cf450b 14276#define OPTION_MBRANCHES_WITH_32B_BOUNDARIES (OPTION_MD_BASE + 30)
ae531041
L
14277#define OPTION_MLFENCE_AFTER_LOAD (OPTION_MD_BASE + 31)
14278#define OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH (OPTION_MD_BASE + 32)
14279#define OPTION_MLFENCE_BEFORE_RET (OPTION_MD_BASE + 33)
c8480b58 14280#define OPTION_MUSE_UNALIGNED_VECTOR_MOVE (OPTION_MD_BASE + 34)
b3b91714 14281
99ad8390
NC
14282struct option md_longopts[] =
14283{
3e73aa7c 14284 {"32", no_argument, NULL, OPTION_32},
321098a5 14285#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 14286 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c 14287 {"64", no_argument, NULL, OPTION_64},
351f65ca
L
14288#endif
14289#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 14290 {"x32", no_argument, NULL, OPTION_X32},
8dcea932 14291 {"mshared", no_argument, NULL, OPTION_MSHARED},
b4a3a7b4 14292 {"mx86-used-note", required_argument, NULL, OPTION_X86_USED_NOTE},
6e0b89ee 14293#endif
b3b91714 14294 {"divide", no_argument, NULL, OPTION_DIVIDE},
9103f4f4
L
14295 {"march", required_argument, NULL, OPTION_MARCH},
14296 {"mtune", required_argument, NULL, OPTION_MTUNE},
1efbbeb4
L
14297 {"mmnemonic", required_argument, NULL, OPTION_MMNEMONIC},
14298 {"msyntax", required_argument, NULL, OPTION_MSYNTAX},
14299 {"mindex-reg", no_argument, NULL, OPTION_MINDEX_REG},
14300 {"mnaked-reg", no_argument, NULL, OPTION_MNAKED_REG},
c0f3af97 14301 {"msse2avx", no_argument, NULL, OPTION_MSSE2AVX},
c8480b58 14302 {"muse-unaligned-vector-move", no_argument, NULL, OPTION_MUSE_UNALIGNED_VECTOR_MOVE},
daf50ae7 14303 {"msse-check", required_argument, NULL, OPTION_MSSE_CHECK},
7bab8ab5 14304 {"moperand-check", required_argument, NULL, OPTION_MOPERAND_CHECK},
539f890d 14305 {"mavxscalar", required_argument, NULL, OPTION_MAVXSCALAR},
03751133 14306 {"mvexwig", required_argument, NULL, OPTION_MVEXWIG},
7e8b059b 14307 {"madd-bnd-prefix", no_argument, NULL, OPTION_MADD_BND_PREFIX},
43234a1e
L
14308 {"mevexlig", required_argument, NULL, OPTION_MEVEXLIG},
14309 {"mevexwig", required_argument, NULL, OPTION_MEVEXWIG},
167ad85b
TG
14310# if defined (TE_PE) || defined (TE_PEP)
14311 {"mbig-obj", no_argument, NULL, OPTION_MBIG_OBJ},
14312#endif
d1982f93 14313 {"momit-lock-prefix", required_argument, NULL, OPTION_MOMIT_LOCK_PREFIX},
e4e00185 14314 {"mfence-as-lock-add", required_argument, NULL, OPTION_MFENCE_AS_LOCK_ADD},
0cb4071e 14315 {"mrelax-relocations", required_argument, NULL, OPTION_MRELAX_RELOCATIONS},
d3d3c6db 14316 {"mevexrcig", required_argument, NULL, OPTION_MEVEXRCIG},
e379e5f3
L
14317 {"malign-branch-boundary", required_argument, NULL, OPTION_MALIGN_BRANCH_BOUNDARY},
14318 {"malign-branch-prefix-size", required_argument, NULL, OPTION_MALIGN_BRANCH_PREFIX_SIZE},
14319 {"malign-branch", required_argument, NULL, OPTION_MALIGN_BRANCH},
76cf450b 14320 {"mbranches-within-32B-boundaries", no_argument, NULL, OPTION_MBRANCHES_WITH_32B_BOUNDARIES},
ae531041
L
14321 {"mlfence-after-load", required_argument, NULL, OPTION_MLFENCE_AFTER_LOAD},
14322 {"mlfence-before-indirect-branch", required_argument, NULL,
14323 OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH},
14324 {"mlfence-before-ret", required_argument, NULL, OPTION_MLFENCE_BEFORE_RET},
5db04b09
L
14325 {"mamd64", no_argument, NULL, OPTION_MAMD64},
14326 {"mintel64", no_argument, NULL, OPTION_MINTEL64},
252b5132
RH
14327 {NULL, no_argument, NULL, 0}
14328};
14329size_t md_longopts_size = sizeof (md_longopts);
14330
14331int
17b9d67d 14332md_parse_option (int c, const char *arg)
252b5132 14333{
91d6fa6a 14334 unsigned int j;
e379e5f3 14335 char *arch, *next, *saved, *type;
9103f4f4 14336
252b5132
RH
14337 switch (c)
14338 {
12b55ccc
L
14339 case 'n':
14340 optimize_align_code = 0;
14341 break;
14342
a38cf1db
AM
14343 case 'q':
14344 quiet_warnings = 1;
252b5132
RH
14345 break;
14346
14347#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
a38cf1db
AM
14348 /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
14349 should be emitted or not. FIXME: Not implemented. */
14350 case 'Q':
d4693039
JB
14351 if ((arg[0] != 'y' && arg[0] != 'n') || arg[1])
14352 return 0;
252b5132
RH
14353 break;
14354
14355 /* -V: SVR4 argument to print version ID. */
14356 case 'V':
14357 print_version_id ();
14358 break;
14359
a38cf1db
AM
14360 /* -k: Ignore for FreeBSD compatibility. */
14361 case 'k':
252b5132 14362 break;
4cc782b5
ILT
14363
14364 case 's':
14365 /* -s: On i386 Solaris, this tells the native assembler to use
29b0f896 14366 .stab instead of .stab.excl. We always use .stab anyhow. */
4cc782b5 14367 break;
8dcea932
L
14368
14369 case OPTION_MSHARED:
14370 shared = 1;
14371 break;
b4a3a7b4
L
14372
14373 case OPTION_X86_USED_NOTE:
14374 if (strcasecmp (arg, "yes") == 0)
14375 x86_used_note = 1;
14376 else if (strcasecmp (arg, "no") == 0)
14377 x86_used_note = 0;
14378 else
14379 as_fatal (_("invalid -mx86-used-note= option: `%s'"), arg);
14380 break;
14381
14382
99ad8390 14383#endif
321098a5 14384#if (defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
d382c579 14385 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
3e73aa7c
JH
14386 case OPTION_64:
14387 {
14388 const char **list, **l;
14389
3e73aa7c
JH
14390 list = bfd_target_list ();
14391 for (l = list; *l != NULL; l++)
08dedd66 14392 if (startswith (*l, "elf64-x86-64")
99ad8390
NC
14393 || strcmp (*l, "coff-x86-64") == 0
14394 || strcmp (*l, "pe-x86-64") == 0
d382c579
TG
14395 || strcmp (*l, "pei-x86-64") == 0
14396 || strcmp (*l, "mach-o-x86-64") == 0)
6e0b89ee
AM
14397 {
14398 default_arch = "x86_64";
14399 break;
14400 }
3e73aa7c 14401 if (*l == NULL)
2b5d6a91 14402 as_fatal (_("no compiled in support for x86_64"));
3e73aa7c
JH
14403 free (list);
14404 }
14405 break;
14406#endif
252b5132 14407
351f65ca 14408#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
570561f7 14409 case OPTION_X32:
351f65ca
L
14410 if (IS_ELF)
14411 {
14412 const char **list, **l;
14413
14414 list = bfd_target_list ();
14415 for (l = list; *l != NULL; l++)
08dedd66 14416 if (startswith (*l, "elf32-x86-64"))
351f65ca
L
14417 {
14418 default_arch = "x86_64:32";
14419 break;
14420 }
14421 if (*l == NULL)
2b5d6a91 14422 as_fatal (_("no compiled in support for 32bit x86_64"));
351f65ca
L
14423 free (list);
14424 }
14425 else
14426 as_fatal (_("32bit x86_64 is only supported for ELF"));
14427 break;
14428#endif
14429
6e0b89ee 14430 case OPTION_32:
590a0fed
JB
14431 {
14432 const char **list, **l;
14433
14434 list = bfd_target_list ();
14435 for (l = list; *l != NULL; l++)
14436 if (strstr (*l, "-i386")
14437 || strstr (*l, "-go32"))
14438 {
14439 default_arch = "i386";
14440 break;
14441 }
14442 if (*l == NULL)
14443 as_fatal (_("no compiled in support for ix86"));
14444 free (list);
14445 }
6e0b89ee
AM
14446 break;
14447
b3b91714
AM
14448 case OPTION_DIVIDE:
14449#ifdef SVR4_COMMENT_CHARS
14450 {
14451 char *n, *t;
14452 const char *s;
14453
add39d23 14454 n = XNEWVEC (char, strlen (i386_comment_chars) + 1);
b3b91714
AM
14455 t = n;
14456 for (s = i386_comment_chars; *s != '\0'; s++)
14457 if (*s != '/')
14458 *t++ = *s;
14459 *t = '\0';
14460 i386_comment_chars = n;
14461 }
14462#endif
14463 break;
14464
9103f4f4 14465 case OPTION_MARCH:
293f5f65
L
14466 saved = xstrdup (arg);
14467 arch = saved;
14468 /* Allow -march=+nosse. */
14469 if (*arch == '+')
14470 arch++;
6305a203 14471 do
9103f4f4 14472 {
4fc85f37
JB
14473 char *vsz;
14474
6305a203 14475 if (*arch == '.')
2b5d6a91 14476 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
14477 next = strchr (arch, '+');
14478 if (next)
14479 *next++ = '\0';
4fc85f37
JB
14480 vsz = strchr (arch, '/');
14481 if (vsz)
14482 *vsz++ = '\0';
91d6fa6a 14483 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 14484 {
4fc85f37
JB
14485 if (vsz && cpu_arch[j].vsz != vsz_set)
14486 continue;
14487
ae89daec
JB
14488 if (arch == saved && cpu_arch[j].type != PROCESSOR_NONE
14489 && strcmp (arch, cpu_arch[j].name) == 0)
ccc9c027 14490 {
6305a203 14491 /* Processor. */
ae89daec 14492 if (! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
14493 continue;
14494
91d6fa6a 14495 cpu_arch_name = cpu_arch[j].name;
d92c7521 14496 free (cpu_sub_arch_name);
6305a203 14497 cpu_sub_arch_name = NULL;
ae89daec 14498 cpu_arch_flags = cpu_arch[j].enable;
91d6fa6a 14499 cpu_arch_isa = cpu_arch[j].type;
ae89daec 14500 cpu_arch_isa_flags = cpu_arch[j].enable;
6305a203
L
14501 if (!cpu_arch_tune_set)
14502 {
14503 cpu_arch_tune = cpu_arch_isa;
14504 cpu_arch_tune_flags = cpu_arch_isa_flags;
14505 }
4fc85f37 14506 vector_size = VSZ_DEFAULT;
6305a203
L
14507 break;
14508 }
ae89daec
JB
14509 else if (cpu_arch[j].type == PROCESSOR_NONE
14510 && strcmp (arch, cpu_arch[j].name) == 0
14511 && !cpu_flags_all_zero (&cpu_arch[j].enable))
6305a203 14512 {
33eaf5de 14513 /* ISA extension. */
6305a203 14514 i386_cpu_flags flags;
309d3373 14515
293f5f65 14516 flags = cpu_flags_or (cpu_arch_flags,
ae89daec 14517 cpu_arch[j].enable);
81486035 14518
5b64d091 14519 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
6305a203 14520 {
ae89daec 14521 extend_cpu_sub_arch_name (arch);
6305a203 14522 cpu_arch_flags = flags;
a586129e 14523 cpu_arch_isa_flags = flags;
6305a203 14524 }
0089dace
L
14525 else
14526 cpu_arch_isa_flags
14527 = cpu_flags_or (cpu_arch_isa_flags,
ae89daec 14528 cpu_arch[j].enable);
4fc85f37
JB
14529
14530 switch (cpu_arch[j].vsz)
14531 {
14532 default:
14533 break;
14534
14535 case vsz_set:
14536 if (vsz)
14537 {
14538 char *end;
14539 unsigned long val = strtoul (vsz, &end, 0);
14540
14541 if (*end)
14542 val = 0;
14543 switch (val)
14544 {
14545 case 512: vector_size = VSZ512; break;
14546 case 256: vector_size = VSZ256; break;
14547 case 128: vector_size = VSZ128; break;
14548 default:
14549 as_warn (_("Unrecognized vector size specifier ignored"));
14550 break;
14551 }
14552 break;
14553 }
14554 /* Fall through. */
14555 case vsz_reset:
14556 vector_size = VSZ_DEFAULT;
14557 break;
14558 }
14559
6305a203 14560 break;
ccc9c027 14561 }
9103f4f4 14562 }
6305a203 14563
ae89daec 14564 if (j >= ARRAY_SIZE (cpu_arch) && startswith (arch, "no"))
293f5f65 14565 {
33eaf5de 14566 /* Disable an ISA extension. */
ae89daec
JB
14567 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
14568 if (cpu_arch[j].type == PROCESSOR_NONE
14569 && strcmp (arch + 2, cpu_arch[j].name) == 0)
293f5f65
L
14570 {
14571 i386_cpu_flags flags;
14572
14573 flags = cpu_flags_and_not (cpu_arch_flags,
ae89daec 14574 cpu_arch[j].disable);
293f5f65
L
14575 if (!cpu_flags_equal (&flags, &cpu_arch_flags))
14576 {
8180707f 14577 extend_cpu_sub_arch_name (arch);
293f5f65
L
14578 cpu_arch_flags = flags;
14579 cpu_arch_isa_flags = flags;
14580 }
4fc85f37
JB
14581 if (cpu_arch[j].vsz == vsz_set)
14582 vector_size = VSZ_DEFAULT;
293f5f65
L
14583 break;
14584 }
293f5f65
L
14585 }
14586
91d6fa6a 14587 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 14588 as_fatal (_("invalid -march= option: `%s'"), arg);
6305a203
L
14589
14590 arch = next;
9103f4f4 14591 }
293f5f65
L
14592 while (next != NULL);
14593 free (saved);
9103f4f4
L
14594 break;
14595
14596 case OPTION_MTUNE:
14597 if (*arg == '.')
2b5d6a91 14598 as_fatal (_("invalid -mtune= option: `%s'"), arg);
91d6fa6a 14599 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
9103f4f4 14600 {
ae89daec
JB
14601 if (cpu_arch[j].type != PROCESSOR_NONE
14602 && strcmp (arg, cpu_arch[j].name) == 0)
9103f4f4 14603 {
ccc9c027 14604 cpu_arch_tune_set = 1;
91d6fa6a 14605 cpu_arch_tune = cpu_arch [j].type;
ae89daec 14606 cpu_arch_tune_flags = cpu_arch[j].enable;
9103f4f4
L
14607 break;
14608 }
14609 }
91d6fa6a 14610 if (j >= ARRAY_SIZE (cpu_arch))
2b5d6a91 14611 as_fatal (_("invalid -mtune= option: `%s'"), arg);
9103f4f4
L
14612 break;
14613
1efbbeb4
L
14614 case OPTION_MMNEMONIC:
14615 if (strcasecmp (arg, "att") == 0)
14616 intel_mnemonic = 0;
14617 else if (strcasecmp (arg, "intel") == 0)
14618 intel_mnemonic = 1;
14619 else
2b5d6a91 14620 as_fatal (_("invalid -mmnemonic= option: `%s'"), arg);
1efbbeb4
L
14621 break;
14622
14623 case OPTION_MSYNTAX:
14624 if (strcasecmp (arg, "att") == 0)
14625 intel_syntax = 0;
14626 else if (strcasecmp (arg, "intel") == 0)
14627 intel_syntax = 1;
14628 else
2b5d6a91 14629 as_fatal (_("invalid -msyntax= option: `%s'"), arg);
1efbbeb4
L
14630 break;
14631
14632 case OPTION_MINDEX_REG:
14633 allow_index_reg = 1;
14634 break;
14635
14636 case OPTION_MNAKED_REG:
14637 allow_naked_reg = 1;
14638 break;
14639
c0f3af97
L
14640 case OPTION_MSSE2AVX:
14641 sse2avx = 1;
14642 break;
14643
c8480b58
L
14644 case OPTION_MUSE_UNALIGNED_VECTOR_MOVE:
14645 use_unaligned_vector_move = 1;
14646 break;
14647
daf50ae7
L
14648 case OPTION_MSSE_CHECK:
14649 if (strcasecmp (arg, "error") == 0)
7bab8ab5 14650 sse_check = check_error;
daf50ae7 14651 else if (strcasecmp (arg, "warning") == 0)
7bab8ab5 14652 sse_check = check_warning;
daf50ae7 14653 else if (strcasecmp (arg, "none") == 0)
7bab8ab5 14654 sse_check = check_none;
daf50ae7 14655 else
2b5d6a91 14656 as_fatal (_("invalid -msse-check= option: `%s'"), arg);
daf50ae7
L
14657 break;
14658
7bab8ab5
JB
14659 case OPTION_MOPERAND_CHECK:
14660 if (strcasecmp (arg, "error") == 0)
14661 operand_check = check_error;
14662 else if (strcasecmp (arg, "warning") == 0)
14663 operand_check = check_warning;
14664 else if (strcasecmp (arg, "none") == 0)
14665 operand_check = check_none;
14666 else
14667 as_fatal (_("invalid -moperand-check= option: `%s'"), arg);
14668 break;
14669
539f890d
L
14670 case OPTION_MAVXSCALAR:
14671 if (strcasecmp (arg, "128") == 0)
14672 avxscalar = vex128;
14673 else if (strcasecmp (arg, "256") == 0)
14674 avxscalar = vex256;
14675 else
2b5d6a91 14676 as_fatal (_("invalid -mavxscalar= option: `%s'"), arg);
539f890d
L
14677 break;
14678
03751133
L
14679 case OPTION_MVEXWIG:
14680 if (strcmp (arg, "0") == 0)
40c9c8de 14681 vexwig = vexw0;
03751133 14682 else if (strcmp (arg, "1") == 0)
40c9c8de 14683 vexwig = vexw1;
03751133
L
14684 else
14685 as_fatal (_("invalid -mvexwig= option: `%s'"), arg);
14686 break;
14687
7e8b059b
L
14688 case OPTION_MADD_BND_PREFIX:
14689 add_bnd_prefix = 1;
14690 break;
14691
43234a1e
L
14692 case OPTION_MEVEXLIG:
14693 if (strcmp (arg, "128") == 0)
14694 evexlig = evexl128;
14695 else if (strcmp (arg, "256") == 0)
14696 evexlig = evexl256;
14697 else if (strcmp (arg, "512") == 0)
14698 evexlig = evexl512;
14699 else
14700 as_fatal (_("invalid -mevexlig= option: `%s'"), arg);
14701 break;
14702
d3d3c6db
IT
14703 case OPTION_MEVEXRCIG:
14704 if (strcmp (arg, "rne") == 0)
14705 evexrcig = rne;
14706 else if (strcmp (arg, "rd") == 0)
14707 evexrcig = rd;
14708 else if (strcmp (arg, "ru") == 0)
14709 evexrcig = ru;
14710 else if (strcmp (arg, "rz") == 0)
14711 evexrcig = rz;
14712 else
14713 as_fatal (_("invalid -mevexrcig= option: `%s'"), arg);
14714 break;
14715
43234a1e
L
14716 case OPTION_MEVEXWIG:
14717 if (strcmp (arg, "0") == 0)
14718 evexwig = evexw0;
14719 else if (strcmp (arg, "1") == 0)
14720 evexwig = evexw1;
14721 else
14722 as_fatal (_("invalid -mevexwig= option: `%s'"), arg);
14723 break;
14724
167ad85b
TG
14725# if defined (TE_PE) || defined (TE_PEP)
14726 case OPTION_MBIG_OBJ:
14727 use_big_obj = 1;
14728 break;
14729#endif
14730
d1982f93 14731 case OPTION_MOMIT_LOCK_PREFIX:
d022bddd
IT
14732 if (strcasecmp (arg, "yes") == 0)
14733 omit_lock_prefix = 1;
14734 else if (strcasecmp (arg, "no") == 0)
14735 omit_lock_prefix = 0;
14736 else
14737 as_fatal (_("invalid -momit-lock-prefix= option: `%s'"), arg);
14738 break;
14739
e4e00185
AS
14740 case OPTION_MFENCE_AS_LOCK_ADD:
14741 if (strcasecmp (arg, "yes") == 0)
14742 avoid_fence = 1;
14743 else if (strcasecmp (arg, "no") == 0)
14744 avoid_fence = 0;
14745 else
14746 as_fatal (_("invalid -mfence-as-lock-add= option: `%s'"), arg);
14747 break;
14748
ae531041
L
14749 case OPTION_MLFENCE_AFTER_LOAD:
14750 if (strcasecmp (arg, "yes") == 0)
14751 lfence_after_load = 1;
14752 else if (strcasecmp (arg, "no") == 0)
14753 lfence_after_load = 0;
14754 else
14755 as_fatal (_("invalid -mlfence-after-load= option: `%s'"), arg);
14756 break;
14757
14758 case OPTION_MLFENCE_BEFORE_INDIRECT_BRANCH:
14759 if (strcasecmp (arg, "all") == 0)
a09f656b 14760 {
14761 lfence_before_indirect_branch = lfence_branch_all;
14762 if (lfence_before_ret == lfence_before_ret_none)
14763 lfence_before_ret = lfence_before_ret_shl;
14764 }
ae531041
L
14765 else if (strcasecmp (arg, "memory") == 0)
14766 lfence_before_indirect_branch = lfence_branch_memory;
14767 else if (strcasecmp (arg, "register") == 0)
14768 lfence_before_indirect_branch = lfence_branch_register;
14769 else if (strcasecmp (arg, "none") == 0)
14770 lfence_before_indirect_branch = lfence_branch_none;
14771 else
14772 as_fatal (_("invalid -mlfence-before-indirect-branch= option: `%s'"),
14773 arg);
14774 break;
14775
14776 case OPTION_MLFENCE_BEFORE_RET:
14777 if (strcasecmp (arg, "or") == 0)
14778 lfence_before_ret = lfence_before_ret_or;
14779 else if (strcasecmp (arg, "not") == 0)
14780 lfence_before_ret = lfence_before_ret_not;
a09f656b 14781 else if (strcasecmp (arg, "shl") == 0 || strcasecmp (arg, "yes") == 0)
14782 lfence_before_ret = lfence_before_ret_shl;
ae531041
L
14783 else if (strcasecmp (arg, "none") == 0)
14784 lfence_before_ret = lfence_before_ret_none;
14785 else
14786 as_fatal (_("invalid -mlfence-before-ret= option: `%s'"),
14787 arg);
14788 break;
14789
0cb4071e
L
14790 case OPTION_MRELAX_RELOCATIONS:
14791 if (strcasecmp (arg, "yes") == 0)
14792 generate_relax_relocations = 1;
14793 else if (strcasecmp (arg, "no") == 0)
14794 generate_relax_relocations = 0;
14795 else
14796 as_fatal (_("invalid -mrelax-relocations= option: `%s'"), arg);
14797 break;
14798
e379e5f3
L
14799 case OPTION_MALIGN_BRANCH_BOUNDARY:
14800 {
14801 char *end;
14802 long int align = strtoul (arg, &end, 0);
14803 if (*end == '\0')
14804 {
14805 if (align == 0)
14806 {
14807 align_branch_power = 0;
14808 break;
14809 }
14810 else if (align >= 16)
14811 {
14812 int align_power;
14813 for (align_power = 0;
14814 (align & 1) == 0;
14815 align >>= 1, align_power++)
14816 continue;
14817 /* Limit alignment power to 31. */
14818 if (align == 1 && align_power < 32)
14819 {
14820 align_branch_power = align_power;
14821 break;
14822 }
14823 }
14824 }
14825 as_fatal (_("invalid -malign-branch-boundary= value: %s"), arg);
14826 }
14827 break;
14828
14829 case OPTION_MALIGN_BRANCH_PREFIX_SIZE:
14830 {
14831 char *end;
14832 int align = strtoul (arg, &end, 0);
14833 /* Some processors only support 5 prefixes. */
14834 if (*end == '\0' && align >= 0 && align < 6)
14835 {
14836 align_branch_prefix_size = align;
14837 break;
14838 }
14839 as_fatal (_("invalid -malign-branch-prefix-size= value: %s"),
14840 arg);
14841 }
14842 break;
14843
14844 case OPTION_MALIGN_BRANCH:
14845 align_branch = 0;
14846 saved = xstrdup (arg);
14847 type = saved;
14848 do
14849 {
14850 next = strchr (type, '+');
14851 if (next)
14852 *next++ = '\0';
14853 if (strcasecmp (type, "jcc") == 0)
14854 align_branch |= align_branch_jcc_bit;
14855 else if (strcasecmp (type, "fused") == 0)
14856 align_branch |= align_branch_fused_bit;
14857 else if (strcasecmp (type, "jmp") == 0)
14858 align_branch |= align_branch_jmp_bit;
14859 else if (strcasecmp (type, "call") == 0)
14860 align_branch |= align_branch_call_bit;
14861 else if (strcasecmp (type, "ret") == 0)
14862 align_branch |= align_branch_ret_bit;
14863 else if (strcasecmp (type, "indirect") == 0)
14864 align_branch |= align_branch_indirect_bit;
14865 else
14866 as_fatal (_("invalid -malign-branch= option: `%s'"), arg);
14867 type = next;
14868 }
14869 while (next != NULL);
14870 free (saved);
14871 break;
14872
76cf450b
L
14873 case OPTION_MBRANCHES_WITH_32B_BOUNDARIES:
14874 align_branch_power = 5;
14875 align_branch_prefix_size = 5;
14876 align_branch = (align_branch_jcc_bit
14877 | align_branch_fused_bit
14878 | align_branch_jmp_bit);
14879 break;
14880
5db04b09 14881 case OPTION_MAMD64:
4b5aaf5f 14882 isa64 = amd64;
5db04b09
L
14883 break;
14884
14885 case OPTION_MINTEL64:
4b5aaf5f 14886 isa64 = intel64;
5db04b09
L
14887 break;
14888
b6f8c7c4
L
14889 case 'O':
14890 if (arg == NULL)
14891 {
14892 optimize = 1;
14893 /* Turn off -Os. */
14894 optimize_for_space = 0;
14895 }
14896 else if (*arg == 's')
14897 {
14898 optimize_for_space = 1;
14899 /* Turn on all encoding optimizations. */
41fd2579 14900 optimize = INT_MAX;
b6f8c7c4
L
14901 }
14902 else
14903 {
14904 optimize = atoi (arg);
14905 /* Turn off -Os. */
14906 optimize_for_space = 0;
14907 }
14908 break;
14909
252b5132
RH
14910 default:
14911 return 0;
14912 }
14913 return 1;
14914}
14915
8a2c8fef
L
14916#define MESSAGE_TEMPLATE \
14917" "
14918
293f5f65
L
14919static char *
14920output_message (FILE *stream, char *p, char *message, char *start,
14921 int *left_p, const char *name, int len)
14922{
14923 int size = sizeof (MESSAGE_TEMPLATE);
14924 int left = *left_p;
14925
14926 /* Reserve 2 spaces for ", " or ",\0" */
14927 left -= len + 2;
14928
14929 /* Check if there is any room. */
14930 if (left >= 0)
14931 {
14932 if (p != start)
14933 {
14934 *p++ = ',';
14935 *p++ = ' ';
14936 }
14937 p = mempcpy (p, name, len);
14938 }
14939 else
14940 {
14941 /* Output the current message now and start a new one. */
14942 *p++ = ',';
14943 *p = '\0';
14944 fprintf (stream, "%s\n", message);
14945 p = start;
14946 left = size - (start - message) - len - 2;
14947
14948 gas_assert (left >= 0);
14949
14950 p = mempcpy (p, name, len);
14951 }
14952
14953 *left_p = left;
14954 return p;
14955}
14956
8a2c8fef 14957static void
1ded5609 14958show_arch (FILE *stream, int ext, int check)
8a2c8fef
L
14959{
14960 static char message[] = MESSAGE_TEMPLATE;
14961 char *start = message + 27;
14962 char *p;
14963 int size = sizeof (MESSAGE_TEMPLATE);
14964 int left;
14965 const char *name;
14966 int len;
14967 unsigned int j;
14968
14969 p = start;
14970 left = size - (start - message);
3ce2ebcf
JB
14971
14972 if (!ext && check)
14973 {
14974 p = output_message (stream, p, message, start, &left,
14975 STRING_COMMA_LEN ("default"));
f68697e8
JB
14976 p = output_message (stream, p, message, start, &left,
14977 STRING_COMMA_LEN ("push"));
14978 p = output_message (stream, p, message, start, &left,
14979 STRING_COMMA_LEN ("pop"));
3ce2ebcf
JB
14980 }
14981
8a2c8fef
L
14982 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
14983 {
14984 /* Should it be skipped? */
14985 if (cpu_arch [j].skip)
14986 continue;
14987
14988 name = cpu_arch [j].name;
14989 len = cpu_arch [j].len;
ae89daec 14990 if (cpu_arch[j].type == PROCESSOR_NONE)
8a2c8fef
L
14991 {
14992 /* It is an extension. Skip if we aren't asked to show it. */
ae89daec 14993 if (!ext || cpu_flags_all_zero (&cpu_arch[j].enable))
8a2c8fef
L
14994 continue;
14995 }
14996 else if (ext)
14997 {
14998 /* It is an processor. Skip if we show only extension. */
14999 continue;
15000 }
ae89daec 15001 else if (check && ! cpu_arch[j].enable.bitfield.cpui386)
1ded5609
JB
15002 {
15003 /* It is an impossible processor - skip. */
15004 continue;
15005 }
8a2c8fef 15006
293f5f65 15007 p = output_message (stream, p, message, start, &left, name, len);
8a2c8fef
L
15008 }
15009
293f5f65
L
15010 /* Display disabled extensions. */
15011 if (ext)
ae89daec 15012 for (j = 0; j < ARRAY_SIZE (cpu_arch); j++)
293f5f65 15013 {
ae89daec
JB
15014 char *str;
15015
15016 if (cpu_arch[j].type != PROCESSOR_NONE
15017 || !cpu_flags_all_zero (&cpu_arch[j].enable))
15018 continue;
15019 str = xasprintf ("no%s", cpu_arch[j].name);
15020 p = output_message (stream, p, message, start, &left, str,
15021 strlen (str));
15022 free (str);
293f5f65
L
15023 }
15024
8a2c8fef
L
15025 *p = '\0';
15026 fprintf (stream, "%s\n", message);
15027}
15028
252b5132 15029void
8a2c8fef 15030md_show_usage (FILE *stream)
252b5132 15031{
4cc782b5
ILT
15032#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15033 fprintf (stream, _("\
d4693039 15034 -Qy, -Qn ignored\n\
a38cf1db 15035 -V print assembler version number\n\
b3b91714
AM
15036 -k ignored\n"));
15037#endif
15038 fprintf (stream, _("\
7ebd68d1
NC
15039 -n do not optimize code alignment\n\
15040 -O{012s} attempt some code optimizations\n\
b3b91714
AM
15041 -q quieten some warnings\n"));
15042#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15043 fprintf (stream, _("\
a38cf1db 15044 -s ignored\n"));
b3b91714 15045#endif
b00af7c8
JB
15046#ifdef BFD64
15047# if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15048 fprintf (stream, _("\
15049 --32/--64/--x32 generate 32bit/64bit/x32 object\n"));
15050# elif defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O)
751d281c 15051 fprintf (stream, _("\
b00af7c8
JB
15052 --32/--64 generate 32bit/64bit object\n"));
15053# endif
751d281c 15054#endif
b3b91714
AM
15055#ifdef SVR4_COMMENT_CHARS
15056 fprintf (stream, _("\
15057 --divide do not treat `/' as a comment character\n"));
a38cf1db
AM
15058#else
15059 fprintf (stream, _("\
b3b91714 15060 --divide ignored\n"));
4cc782b5 15061#endif
9103f4f4 15062 fprintf (stream, _("\
6305a203 15063 -march=CPU[,+EXTENSION...]\n\
8a2c8fef 15064 generate code for CPU and EXTENSION, CPU is one of:\n"));
1ded5609 15065 show_arch (stream, 0, 1);
8a2c8fef 15066 fprintf (stream, _("\
ae89daec 15067 EXTENSION is combination of (possibly \"no\"-prefixed):\n"));
1ded5609 15068 show_arch (stream, 1, 0);
6305a203 15069 fprintf (stream, _("\
8a2c8fef 15070 -mtune=CPU optimize for CPU, CPU is one of:\n"));
1ded5609 15071 show_arch (stream, 0, 0);
ba104c83 15072 fprintf (stream, _("\
c0f3af97
L
15073 -msse2avx encode SSE instructions with VEX prefix\n"));
15074 fprintf (stream, _("\
c8480b58
L
15075 -muse-unaligned-vector-move\n\
15076 encode aligned vector move as unaligned vector move\n"));
15077 fprintf (stream, _("\
7c5c05ef 15078 -msse-check=[none|error|warning] (default: warning)\n\
daf50ae7
L
15079 check SSE instructions\n"));
15080 fprintf (stream, _("\
7c5c05ef 15081 -moperand-check=[none|error|warning] (default: warning)\n\
7bab8ab5
JB
15082 check operand combinations for validity\n"));
15083 fprintf (stream, _("\
7c5c05ef
L
15084 -mavxscalar=[128|256] (default: 128)\n\
15085 encode scalar AVX instructions with specific vector\n\
539f890d
L
15086 length\n"));
15087 fprintf (stream, _("\
03751133
L
15088 -mvexwig=[0|1] (default: 0)\n\
15089 encode VEX instructions with specific VEX.W value\n\
15090 for VEX.W bit ignored instructions\n"));
15091 fprintf (stream, _("\
7c5c05ef
L
15092 -mevexlig=[128|256|512] (default: 128)\n\
15093 encode scalar EVEX instructions with specific vector\n\
43234a1e
L
15094 length\n"));
15095 fprintf (stream, _("\
7c5c05ef
L
15096 -mevexwig=[0|1] (default: 0)\n\
15097 encode EVEX instructions with specific EVEX.W value\n\
43234a1e
L
15098 for EVEX.W bit ignored instructions\n"));
15099 fprintf (stream, _("\
7c5c05ef 15100 -mevexrcig=[rne|rd|ru|rz] (default: rne)\n\
d3d3c6db
IT
15101 encode EVEX instructions with specific EVEX.RC value\n\
15102 for SAE-only ignored instructions\n"));
15103 fprintf (stream, _("\
7c5c05ef
L
15104 -mmnemonic=[att|intel] "));
15105 if (SYSV386_COMPAT)
15106 fprintf (stream, _("(default: att)\n"));
15107 else
15108 fprintf (stream, _("(default: intel)\n"));
15109 fprintf (stream, _("\
15110 use AT&T/Intel mnemonic\n"));
ba104c83 15111 fprintf (stream, _("\
7c5c05ef
L
15112 -msyntax=[att|intel] (default: att)\n\
15113 use AT&T/Intel syntax\n"));
ba104c83
L
15114 fprintf (stream, _("\
15115 -mindex-reg support pseudo index registers\n"));
15116 fprintf (stream, _("\
15117 -mnaked-reg don't require `%%' prefix for registers\n"));
15118 fprintf (stream, _("\
7e8b059b 15119 -madd-bnd-prefix add BND prefix for all valid branches\n"));
b4a3a7b4 15120#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
8dcea932
L
15121 fprintf (stream, _("\
15122 -mshared disable branch optimization for shared code\n"));
b4a3a7b4
L
15123 fprintf (stream, _("\
15124 -mx86-used-note=[no|yes] "));
15125 if (DEFAULT_X86_USED_NOTE)
15126 fprintf (stream, _("(default: yes)\n"));
15127 else
15128 fprintf (stream, _("(default: no)\n"));
15129 fprintf (stream, _("\
15130 generate x86 used ISA and feature properties\n"));
15131#endif
15132#if defined (TE_PE) || defined (TE_PEP)
167ad85b
TG
15133 fprintf (stream, _("\
15134 -mbig-obj generate big object files\n"));
15135#endif
d022bddd 15136 fprintf (stream, _("\
7c5c05ef 15137 -momit-lock-prefix=[no|yes] (default: no)\n\
d022bddd 15138 strip all lock prefixes\n"));
5db04b09 15139 fprintf (stream, _("\
7c5c05ef 15140 -mfence-as-lock-add=[no|yes] (default: no)\n\
e4e00185
AS
15141 encode lfence, mfence and sfence as\n\
15142 lock addl $0x0, (%%{re}sp)\n"));
15143 fprintf (stream, _("\
7c5c05ef
L
15144 -mrelax-relocations=[no|yes] "));
15145 if (DEFAULT_GENERATE_X86_RELAX_RELOCATIONS)
15146 fprintf (stream, _("(default: yes)\n"));
15147 else
15148 fprintf (stream, _("(default: no)\n"));
15149 fprintf (stream, _("\
0cb4071e
L
15150 generate relax relocations\n"));
15151 fprintf (stream, _("\
e379e5f3
L
15152 -malign-branch-boundary=NUM (default: 0)\n\
15153 align branches within NUM byte boundary\n"));
15154 fprintf (stream, _("\
15155 -malign-branch=TYPE[+TYPE...] (default: jcc+fused+jmp)\n\
15156 TYPE is combination of jcc, fused, jmp, call, ret,\n\
15157 indirect\n\
15158 specify types of branches to align\n"));
15159 fprintf (stream, _("\
15160 -malign-branch-prefix-size=NUM (default: 5)\n\
15161 align branches with NUM prefixes per instruction\n"));
15162 fprintf (stream, _("\
76cf450b
L
15163 -mbranches-within-32B-boundaries\n\
15164 align branches within 32 byte boundary\n"));
15165 fprintf (stream, _("\
ae531041
L
15166 -mlfence-after-load=[no|yes] (default: no)\n\
15167 generate lfence after load\n"));
15168 fprintf (stream, _("\
15169 -mlfence-before-indirect-branch=[none|all|register|memory] (default: none)\n\
15170 generate lfence before indirect near branch\n"));
15171 fprintf (stream, _("\
a09f656b 15172 -mlfence-before-ret=[none|or|not|shl|yes] (default: none)\n\
ae531041
L
15173 generate lfence before ret\n"));
15174 fprintf (stream, _("\
7c5c05ef 15175 -mamd64 accept only AMD64 ISA [default]\n"));
5db04b09
L
15176 fprintf (stream, _("\
15177 -mintel64 accept only Intel64 ISA\n"));
252b5132
RH
15178}
15179
3e73aa7c 15180#if ((defined (OBJ_MAYBE_COFF) && defined (OBJ_MAYBE_AOUT)) \
321098a5 15181 || defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF) \
e57f8c65 15182 || defined (TE_PE) || defined (TE_PEP) || defined (OBJ_MACH_O))
252b5132
RH
15183
15184/* Pick the target format to use. */
15185
47926f60 15186const char *
e3bb37b5 15187i386_target_format (void)
252b5132 15188{
d34049e8 15189 if (startswith (default_arch, "x86_64"))
351f65ca
L
15190 {
15191 update_code_flag (CODE_64BIT, 1);
15192 if (default_arch[6] == '\0')
7f56bc95 15193 x86_elf_abi = X86_64_ABI;
351f65ca 15194 else
7f56bc95 15195 x86_elf_abi = X86_64_X32_ABI;
351f65ca 15196 }
3e73aa7c 15197 else if (!strcmp (default_arch, "i386"))
78f12dd3 15198 update_code_flag (CODE_32BIT, 1);
5197d474
L
15199 else if (!strcmp (default_arch, "iamcu"))
15200 {
15201 update_code_flag (CODE_32BIT, 1);
15202 if (cpu_arch_isa == PROCESSOR_UNKNOWN)
15203 {
15204 static const i386_cpu_flags iamcu_flags = CPU_IAMCU_FLAGS;
15205 cpu_arch_name = "iamcu";
d92c7521 15206 free (cpu_sub_arch_name);
5197d474
L
15207 cpu_sub_arch_name = NULL;
15208 cpu_arch_flags = iamcu_flags;
15209 cpu_arch_isa = PROCESSOR_IAMCU;
15210 cpu_arch_isa_flags = iamcu_flags;
15211 if (!cpu_arch_tune_set)
15212 {
15213 cpu_arch_tune = cpu_arch_isa;
15214 cpu_arch_tune_flags = cpu_arch_isa_flags;
15215 }
15216 }
8d471ec1 15217 else if (cpu_arch_isa != PROCESSOR_IAMCU)
5197d474
L
15218 as_fatal (_("Intel MCU doesn't support `%s' architecture"),
15219 cpu_arch_name);
15220 }
3e73aa7c 15221 else
2b5d6a91 15222 as_fatal (_("unknown architecture"));
89507696
JB
15223
15224 if (cpu_flags_all_zero (&cpu_arch_isa_flags))
ae89daec 15225 cpu_arch_isa_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 15226 if (cpu_flags_all_zero (&cpu_arch_tune_flags))
ae89daec 15227 cpu_arch_tune_flags = cpu_arch[flag_code == CODE_64BIT].enable;
89507696 15228
252b5132
RH
15229 switch (OUTPUT_FLAVOR)
15230 {
9384f2ff 15231#if defined (OBJ_MAYBE_AOUT) || defined (OBJ_AOUT)
4c63da97 15232 case bfd_target_aout_flavour:
47926f60 15233 return AOUT_TARGET_FORMAT;
4c63da97 15234#endif
9384f2ff
AM
15235#if defined (OBJ_MAYBE_COFF) || defined (OBJ_COFF)
15236# if defined (TE_PE) || defined (TE_PEP)
15237 case bfd_target_coff_flavour:
167ad85b 15238 if (flag_code == CODE_64BIT)
eb19308f
JB
15239 {
15240 object_64bit = 1;
15241 return use_big_obj ? "pe-bigobj-x86-64" : "pe-x86-64";
15242 }
15243 return use_big_obj ? "pe-bigobj-i386" : "pe-i386";
9384f2ff 15244# elif defined (TE_GO32)
0561d57c
JK
15245 case bfd_target_coff_flavour:
15246 return "coff-go32";
9384f2ff 15247# else
252b5132
RH
15248 case bfd_target_coff_flavour:
15249 return "coff-i386";
9384f2ff 15250# endif
4c63da97 15251#endif
3e73aa7c 15252#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
252b5132 15253 case bfd_target_elf_flavour:
3e73aa7c 15254 {
351f65ca
L
15255 const char *format;
15256
15257 switch (x86_elf_abi)
4fa24527 15258 {
351f65ca
L
15259 default:
15260 format = ELF_TARGET_FORMAT;
e379e5f3
L
15261#ifndef TE_SOLARIS
15262 tls_get_addr = "___tls_get_addr";
15263#endif
351f65ca 15264 break;
7f56bc95 15265 case X86_64_ABI:
351f65ca 15266 use_rela_relocations = 1;
4fa24527 15267 object_64bit = 1;
e379e5f3
L
15268#ifndef TE_SOLARIS
15269 tls_get_addr = "__tls_get_addr";
15270#endif
351f65ca
L
15271 format = ELF_TARGET_FORMAT64;
15272 break;
7f56bc95 15273 case X86_64_X32_ABI:
4fa24527 15274 use_rela_relocations = 1;
351f65ca 15275 object_64bit = 1;
e379e5f3
L
15276#ifndef TE_SOLARIS
15277 tls_get_addr = "__tls_get_addr";
15278#endif
862be3fb 15279 disallow_64bit_reloc = 1;
351f65ca
L
15280 format = ELF_TARGET_FORMAT32;
15281 break;
4fa24527 15282 }
c085ab00 15283 if (cpu_arch_isa == PROCESSOR_IAMCU)
81486035
L
15284 {
15285 if (x86_elf_abi != I386_ABI)
15286 as_fatal (_("Intel MCU is 32bit only"));
15287 return ELF_TARGET_IAMCU_FORMAT;
15288 }
8a9036a4 15289 else
351f65ca 15290 return format;
3e73aa7c 15291 }
e57f8c65
TG
15292#endif
15293#if defined (OBJ_MACH_O)
15294 case bfd_target_mach_o_flavour:
d382c579
TG
15295 if (flag_code == CODE_64BIT)
15296 {
15297 use_rela_relocations = 1;
15298 object_64bit = 1;
15299 return "mach-o-x86-64";
15300 }
15301 else
15302 return "mach-o-i386";
4c63da97 15303#endif
252b5132
RH
15304 default:
15305 abort ();
15306 return NULL;
15307 }
15308}
15309
47926f60 15310#endif /* OBJ_MAYBE_ more than one */
252b5132 15311\f
252b5132 15312symbolS *
7016a5d5 15313md_undefined_symbol (char *name)
252b5132 15314{
18dc2407
ILT
15315 if (name[0] == GLOBAL_OFFSET_TABLE_NAME[0]
15316 && name[1] == GLOBAL_OFFSET_TABLE_NAME[1]
15317 && name[2] == GLOBAL_OFFSET_TABLE_NAME[2]
15318 && strcmp (name, GLOBAL_OFFSET_TABLE_NAME) == 0)
24eab124
AM
15319 {
15320 if (!GOT_symbol)
15321 {
15322 if (symbol_find (name))
15323 as_bad (_("GOT already in symbol table"));
15324 GOT_symbol = symbol_new (name, undefined_section,
e01e1cee 15325 &zero_address_frag, 0);
24eab124
AM
15326 };
15327 return GOT_symbol;
15328 }
252b5132
RH
15329 return 0;
15330}
15331
15332/* Round up a section size to the appropriate boundary. */
47926f60 15333
252b5132 15334valueT
7016a5d5 15335md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
252b5132 15336{
4c63da97
AM
15337#if (defined (OBJ_AOUT) || defined (OBJ_MAYBE_AOUT))
15338 if (OUTPUT_FLAVOR == bfd_target_aout_flavour)
15339 {
15340 /* For a.out, force the section size to be aligned. If we don't do
15341 this, BFD will align it for us, but it will not write out the
15342 final bytes of the section. This may be a bug in BFD, but it is
15343 easier to fix it here since that is how the other a.out targets
15344 work. */
15345 int align;
15346
fd361982 15347 align = bfd_section_alignment (segment);
8d3842cd 15348 size = ((size + (1 << align) - 1) & (-((valueT) 1 << align)));
4c63da97 15349 }
252b5132
RH
15350#endif
15351
15352 return size;
15353}
15354
15355/* On the i386, PC-relative offsets are relative to the start of the
15356 next instruction. That is, the address of the offset, plus its
15357 size, since the offset is always the last part of the insn. */
15358
15359long
e3bb37b5 15360md_pcrel_from (fixS *fixP)
252b5132
RH
15361{
15362 return fixP->fx_size + fixP->fx_where + fixP->fx_frag->fr_address;
15363}
15364
15365#ifndef I386COFF
15366
15367static void
e3bb37b5 15368s_bss (int ignore ATTRIBUTE_UNUSED)
252b5132 15369{
29b0f896 15370 int temp;
252b5132 15371
8a75718c
JB
15372#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15373 if (IS_ELF)
15374 obj_elf_section_change_hook ();
15375#endif
252b5132
RH
15376 temp = get_absolute_expression ();
15377 subseg_set (bss_section, (subsegT) temp);
15378 demand_empty_rest_of_line ();
15379}
15380
15381#endif
15382
e379e5f3
L
15383/* Remember constant directive. */
15384
15385void
15386i386_cons_align (int ignore ATTRIBUTE_UNUSED)
15387{
15388 if (last_insn.kind != last_insn_directive
15389 && (bfd_section_flags (now_seg) & SEC_CODE))
15390 {
15391 last_insn.seg = now_seg;
15392 last_insn.kind = last_insn_directive;
15393 last_insn.name = "constant directive";
15394 last_insn.file = as_where (&last_insn.line);
ae531041
L
15395 if (lfence_before_ret != lfence_before_ret_none)
15396 {
15397 if (lfence_before_indirect_branch != lfence_branch_none)
15398 as_warn (_("constant directive skips -mlfence-before-ret "
15399 "and -mlfence-before-indirect-branch"));
15400 else
15401 as_warn (_("constant directive skips -mlfence-before-ret"));
15402 }
15403 else if (lfence_before_indirect_branch != lfence_branch_none)
15404 as_warn (_("constant directive skips -mlfence-before-indirect-branch"));
e379e5f3
L
15405 }
15406}
15407
3abbafc2 15408int
e3bb37b5 15409i386_validate_fix (fixS *fixp)
252b5132 15410{
e52a16f2
JB
15411 if (fixp->fx_addsy && S_GET_SEGMENT(fixp->fx_addsy) == reg_section)
15412 {
15413 reloc_howto_type *howto;
15414
15415 howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
15416 as_bad_where (fixp->fx_file, fixp->fx_line,
15417 _("invalid %s relocation against register"),
15418 howto ? howto->name : "<unknown>");
15419 return 0;
15420 }
15421
3abbafc2
JB
15422#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15423 if (fixp->fx_r_type == BFD_RELOC_SIZE32
15424 || fixp->fx_r_type == BFD_RELOC_SIZE64)
15425 return IS_ELF && fixp->fx_addsy
15426 && (!S_IS_DEFINED (fixp->fx_addsy)
15427 || S_IS_EXTERNAL (fixp->fx_addsy));
15428#endif
15429
02a86693 15430 if (fixp->fx_subsy)
252b5132 15431 {
02a86693 15432 if (fixp->fx_subsy == GOT_symbol)
23df1078 15433 {
02a86693
L
15434 if (fixp->fx_r_type == BFD_RELOC_32_PCREL)
15435 {
15436 if (!object_64bit)
15437 abort ();
15438#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15439 if (fixp->fx_tcbit2)
56ceb5b5
L
15440 fixp->fx_r_type = (fixp->fx_tcbit
15441 ? BFD_RELOC_X86_64_REX_GOTPCRELX
15442 : BFD_RELOC_X86_64_GOTPCRELX);
02a86693
L
15443 else
15444#endif
15445 fixp->fx_r_type = BFD_RELOC_X86_64_GOTPCREL;
15446 }
d6ab8113 15447 else
02a86693
L
15448 {
15449 if (!object_64bit)
15450 fixp->fx_r_type = BFD_RELOC_386_GOTOFF;
15451 else
15452 fixp->fx_r_type = BFD_RELOC_X86_64_GOTOFF64;
15453 }
15454 fixp->fx_subsy = 0;
23df1078 15455 }
252b5132 15456 }
02a86693 15457#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
2585b7a5 15458 else
02a86693 15459 {
2585b7a5
L
15460 /* NB: Commit 292676c1 resolved PLT32 reloc aganst local symbol
15461 to section. Since PLT32 relocation must be against symbols,
15462 turn such PLT32 relocation into PC32 relocation. */
15463 if (fixp->fx_addsy
15464 && (fixp->fx_r_type == BFD_RELOC_386_PLT32
15465 || fixp->fx_r_type == BFD_RELOC_X86_64_PLT32)
15466 && symbol_section_p (fixp->fx_addsy))
15467 fixp->fx_r_type = BFD_RELOC_32_PCREL;
15468 if (!object_64bit)
15469 {
15470 if (fixp->fx_r_type == BFD_RELOC_386_GOT32
15471 && fixp->fx_tcbit2)
15472 fixp->fx_r_type = BFD_RELOC_386_GOT32X;
15473 }
02a86693
L
15474 }
15475#endif
3abbafc2
JB
15476
15477 return 1;
252b5132
RH
15478}
15479
252b5132 15480arelent *
7016a5d5 15481tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
252b5132
RH
15482{
15483 arelent *rel;
15484 bfd_reloc_code_real_type code;
15485
15486 switch (fixp->fx_r_type)
15487 {
8ce3d284 15488#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
3abbafc2
JB
15489 symbolS *sym;
15490
8fd4256d
L
15491 case BFD_RELOC_SIZE32:
15492 case BFD_RELOC_SIZE64:
3abbafc2
JB
15493 if (fixp->fx_addsy
15494 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))
15495 && (!fixp->fx_subsy
15496 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))))
15497 sym = fixp->fx_addsy;
15498 else if (fixp->fx_subsy
15499 && !bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_subsy))
15500 && (!fixp->fx_addsy
15501 || bfd_is_abs_section (S_GET_SEGMENT (fixp->fx_addsy))))
15502 sym = fixp->fx_subsy;
15503 else
15504 sym = NULL;
15505 if (IS_ELF && sym && S_IS_DEFINED (sym) && !S_IS_EXTERNAL (sym))
8fd4256d
L
15506 {
15507 /* Resolve size relocation against local symbol to size of
15508 the symbol plus addend. */
3abbafc2 15509 valueT value = S_GET_SIZE (sym);
44f87162 15510
3abbafc2
JB
15511 if (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM)
15512 value = bfd_section_size (S_GET_SEGMENT (sym));
15513 if (sym == fixp->fx_subsy)
15514 {
15515 value = -value;
15516 if (fixp->fx_addsy)
15517 value += S_GET_VALUE (fixp->fx_addsy);
15518 }
15519 else if (fixp->fx_subsy)
15520 value -= S_GET_VALUE (fixp->fx_subsy);
44f87162 15521 value += fixp->fx_offset;
8fd4256d 15522 if (fixp->fx_r_type == BFD_RELOC_SIZE32
d965814f 15523 && object_64bit
8fd4256d
L
15524 && !fits_in_unsigned_long (value))
15525 as_bad_where (fixp->fx_file, fixp->fx_line,
15526 _("symbol size computation overflow"));
15527 fixp->fx_addsy = NULL;
15528 fixp->fx_subsy = NULL;
15529 md_apply_fix (fixp, (valueT *) &value, NULL);
15530 return NULL;
15531 }
3abbafc2
JB
15532 if (!fixp->fx_addsy || fixp->fx_subsy)
15533 {
15534 as_bad_where (fixp->fx_file, fixp->fx_line,
15535 "unsupported expression involving @size");
15536 return NULL;
15537 }
8ce3d284 15538#endif
1a0670f3 15539 /* Fall through. */
8fd4256d 15540
3e73aa7c
JH
15541 case BFD_RELOC_X86_64_PLT32:
15542 case BFD_RELOC_X86_64_GOT32:
15543 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
15544 case BFD_RELOC_X86_64_GOTPCRELX:
15545 case BFD_RELOC_X86_64_REX_GOTPCRELX:
252b5132
RH
15546 case BFD_RELOC_386_PLT32:
15547 case BFD_RELOC_386_GOT32:
02a86693 15548 case BFD_RELOC_386_GOT32X:
252b5132
RH
15549 case BFD_RELOC_386_GOTOFF:
15550 case BFD_RELOC_386_GOTPC:
13ae64f3
JJ
15551 case BFD_RELOC_386_TLS_GD:
15552 case BFD_RELOC_386_TLS_LDM:
15553 case BFD_RELOC_386_TLS_LDO_32:
15554 case BFD_RELOC_386_TLS_IE_32:
37e55690
JJ
15555 case BFD_RELOC_386_TLS_IE:
15556 case BFD_RELOC_386_TLS_GOTIE:
13ae64f3
JJ
15557 case BFD_RELOC_386_TLS_LE_32:
15558 case BFD_RELOC_386_TLS_LE:
67a4f2b7
AO
15559 case BFD_RELOC_386_TLS_GOTDESC:
15560 case BFD_RELOC_386_TLS_DESC_CALL:
bffbf940
JJ
15561 case BFD_RELOC_X86_64_TLSGD:
15562 case BFD_RELOC_X86_64_TLSLD:
15563 case BFD_RELOC_X86_64_DTPOFF32:
d6ab8113 15564 case BFD_RELOC_X86_64_DTPOFF64:
bffbf940
JJ
15565 case BFD_RELOC_X86_64_GOTTPOFF:
15566 case BFD_RELOC_X86_64_TPOFF32:
d6ab8113
JB
15567 case BFD_RELOC_X86_64_TPOFF64:
15568 case BFD_RELOC_X86_64_GOTOFF64:
15569 case BFD_RELOC_X86_64_GOTPC32:
7b81dfbb
AJ
15570 case BFD_RELOC_X86_64_GOT64:
15571 case BFD_RELOC_X86_64_GOTPCREL64:
15572 case BFD_RELOC_X86_64_GOTPC64:
15573 case BFD_RELOC_X86_64_GOTPLT64:
15574 case BFD_RELOC_X86_64_PLTOFF64:
67a4f2b7
AO
15575 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
15576 case BFD_RELOC_X86_64_TLSDESC_CALL:
252b5132
RH
15577 case BFD_RELOC_RVA:
15578 case BFD_RELOC_VTABLE_ENTRY:
15579 case BFD_RELOC_VTABLE_INHERIT:
6482c264
NC
15580#ifdef TE_PE
15581 case BFD_RELOC_32_SECREL:
145667f8 15582 case BFD_RELOC_16_SECIDX:
6482c264 15583#endif
252b5132
RH
15584 code = fixp->fx_r_type;
15585 break;
dbbaec26
L
15586 case BFD_RELOC_X86_64_32S:
15587 if (!fixp->fx_pcrel)
15588 {
15589 /* Don't turn BFD_RELOC_X86_64_32S into BFD_RELOC_32. */
15590 code = fixp->fx_r_type;
15591 break;
15592 }
1a0670f3 15593 /* Fall through. */
252b5132 15594 default:
93382f6d 15595 if (fixp->fx_pcrel)
252b5132 15596 {
93382f6d
AM
15597 switch (fixp->fx_size)
15598 {
15599 default:
b091f402
AM
15600 as_bad_where (fixp->fx_file, fixp->fx_line,
15601 _("can not do %d byte pc-relative relocation"),
15602 fixp->fx_size);
93382f6d
AM
15603 code = BFD_RELOC_32_PCREL;
15604 break;
15605 case 1: code = BFD_RELOC_8_PCREL; break;
15606 case 2: code = BFD_RELOC_16_PCREL; break;
d258b828 15607 case 4: code = BFD_RELOC_32_PCREL; break;
d6ab8113
JB
15608#ifdef BFD64
15609 case 8: code = BFD_RELOC_64_PCREL; break;
15610#endif
93382f6d
AM
15611 }
15612 }
15613 else
15614 {
15615 switch (fixp->fx_size)
15616 {
15617 default:
b091f402
AM
15618 as_bad_where (fixp->fx_file, fixp->fx_line,
15619 _("can not do %d byte relocation"),
15620 fixp->fx_size);
93382f6d
AM
15621 code = BFD_RELOC_32;
15622 break;
15623 case 1: code = BFD_RELOC_8; break;
15624 case 2: code = BFD_RELOC_16; break;
15625 case 4: code = BFD_RELOC_32; break;
937149dd 15626#ifdef BFD64
3e73aa7c 15627 case 8: code = BFD_RELOC_64; break;
937149dd 15628#endif
93382f6d 15629 }
252b5132
RH
15630 }
15631 break;
15632 }
252b5132 15633
d182319b
JB
15634 if ((code == BFD_RELOC_32
15635 || code == BFD_RELOC_32_PCREL
15636 || code == BFD_RELOC_X86_64_32S)
252b5132
RH
15637 && GOT_symbol
15638 && fixp->fx_addsy == GOT_symbol)
3e73aa7c 15639 {
4fa24527 15640 if (!object_64bit)
d6ab8113
JB
15641 code = BFD_RELOC_386_GOTPC;
15642 else
15643 code = BFD_RELOC_X86_64_GOTPC32;
3e73aa7c 15644 }
7b81dfbb
AJ
15645 if ((code == BFD_RELOC_64 || code == BFD_RELOC_64_PCREL)
15646 && GOT_symbol
15647 && fixp->fx_addsy == GOT_symbol)
15648 {
15649 code = BFD_RELOC_X86_64_GOTPC64;
15650 }
252b5132 15651
add39d23
TS
15652 rel = XNEW (arelent);
15653 rel->sym_ptr_ptr = XNEW (asymbol *);
49309057 15654 *rel->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
252b5132
RH
15655
15656 rel->address = fixp->fx_frag->fr_address + fixp->fx_where;
c87db184 15657
3e73aa7c
JH
15658 if (!use_rela_relocations)
15659 {
15660 /* HACK: Since i386 ELF uses Rel instead of Rela, encode the
15661 vtable entry to be used in the relocation's section offset. */
15662 if (fixp->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
15663 rel->address = fixp->fx_offset;
fbeb56a4
DK
15664#if defined (OBJ_COFF) && defined (TE_PE)
15665 else if (fixp->fx_addsy && S_IS_WEAK (fixp->fx_addsy))
15666 rel->addend = fixp->fx_addnumber - (S_GET_VALUE (fixp->fx_addsy) * 2);
15667 else
15668#endif
c6682705 15669 rel->addend = 0;
3e73aa7c
JH
15670 }
15671 /* Use the rela in 64bit mode. */
252b5132 15672 else
3e73aa7c 15673 {
862be3fb
L
15674 if (disallow_64bit_reloc)
15675 switch (code)
15676 {
862be3fb
L
15677 case BFD_RELOC_X86_64_DTPOFF64:
15678 case BFD_RELOC_X86_64_TPOFF64:
15679 case BFD_RELOC_64_PCREL:
15680 case BFD_RELOC_X86_64_GOTOFF64:
15681 case BFD_RELOC_X86_64_GOT64:
15682 case BFD_RELOC_X86_64_GOTPCREL64:
15683 case BFD_RELOC_X86_64_GOTPC64:
15684 case BFD_RELOC_X86_64_GOTPLT64:
15685 case BFD_RELOC_X86_64_PLTOFF64:
15686 as_bad_where (fixp->fx_file, fixp->fx_line,
15687 _("cannot represent relocation type %s in x32 mode"),
15688 bfd_get_reloc_code_name (code));
15689 break;
15690 default:
15691 break;
15692 }
15693
062cd5e7
AS
15694 if (!fixp->fx_pcrel)
15695 rel->addend = fixp->fx_offset;
15696 else
15697 switch (code)
15698 {
15699 case BFD_RELOC_X86_64_PLT32:
15700 case BFD_RELOC_X86_64_GOT32:
15701 case BFD_RELOC_X86_64_GOTPCREL:
56ceb5b5
L
15702 case BFD_RELOC_X86_64_GOTPCRELX:
15703 case BFD_RELOC_X86_64_REX_GOTPCRELX:
bffbf940
JJ
15704 case BFD_RELOC_X86_64_TLSGD:
15705 case BFD_RELOC_X86_64_TLSLD:
15706 case BFD_RELOC_X86_64_GOTTPOFF:
67a4f2b7
AO
15707 case BFD_RELOC_X86_64_GOTPC32_TLSDESC:
15708 case BFD_RELOC_X86_64_TLSDESC_CALL:
062cd5e7
AS
15709 rel->addend = fixp->fx_offset - fixp->fx_size;
15710 break;
15711 default:
15712 rel->addend = (section->vma
15713 - fixp->fx_size
15714 + fixp->fx_addnumber
15715 + md_pcrel_from (fixp));
15716 break;
15717 }
3e73aa7c
JH
15718 }
15719
252b5132
RH
15720 rel->howto = bfd_reloc_type_lookup (stdoutput, code);
15721 if (rel->howto == NULL)
15722 {
15723 as_bad_where (fixp->fx_file, fixp->fx_line,
d0b47220 15724 _("cannot represent relocation type %s"),
252b5132
RH
15725 bfd_get_reloc_code_name (code));
15726 /* Set howto to a garbage value so that we can keep going. */
15727 rel->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_32);
9c2799c2 15728 gas_assert (rel->howto != NULL);
252b5132
RH
15729 }
15730
15731 return rel;
15732}
15733
ee86248c 15734#include "tc-i386-intel.c"
54cfded0 15735
a60de03c
JB
15736void
15737tc_x86_parse_to_dw2regnum (expressionS *exp)
54cfded0 15738{
a60de03c
JB
15739 int saved_naked_reg;
15740 char saved_register_dot;
54cfded0 15741
a60de03c
JB
15742 saved_naked_reg = allow_naked_reg;
15743 allow_naked_reg = 1;
15744 saved_register_dot = register_chars['.'];
15745 register_chars['.'] = '.';
15746 allow_pseudo_reg = 1;
15747 expression_and_evaluate (exp);
15748 allow_pseudo_reg = 0;
15749 register_chars['.'] = saved_register_dot;
15750 allow_naked_reg = saved_naked_reg;
15751
e96d56a1 15752 if (exp->X_op == O_register && exp->X_add_number >= 0)
54cfded0 15753 {
a60de03c
JB
15754 if ((addressT) exp->X_add_number < i386_regtab_size)
15755 {
15756 exp->X_op = O_constant;
15757 exp->X_add_number = i386_regtab[exp->X_add_number]
15758 .dw2_regnum[flag_code >> 1];
15759 }
15760 else
15761 exp->X_op = O_illegal;
54cfded0 15762 }
54cfded0
AM
15763}
15764
15765void
15766tc_x86_frame_initial_instructions (void)
15767{
a60de03c
JB
15768 static unsigned int sp_regno[2];
15769
15770 if (!sp_regno[flag_code >> 1])
15771 {
15772 char *saved_input = input_line_pointer;
15773 char sp[][4] = {"esp", "rsp"};
15774 expressionS exp;
a4447b93 15775
a60de03c
JB
15776 input_line_pointer = sp[flag_code >> 1];
15777 tc_x86_parse_to_dw2regnum (&exp);
9c2799c2 15778 gas_assert (exp.X_op == O_constant);
a60de03c
JB
15779 sp_regno[flag_code >> 1] = exp.X_add_number;
15780 input_line_pointer = saved_input;
15781 }
a4447b93 15782
61ff971f
L
15783 cfi_add_CFA_def_cfa (sp_regno[flag_code >> 1], -x86_cie_data_alignment);
15784 cfi_add_CFA_offset (x86_dwarf2_return_column, x86_cie_data_alignment);
54cfded0 15785}
d2b2c203 15786
d7921315
L
15787int
15788x86_dwarf2_addr_size (void)
15789{
15790#if defined (OBJ_MAYBE_ELF) || defined (OBJ_ELF)
15791 if (x86_elf_abi == X86_64_X32_ABI)
15792 return 4;
15793#endif
15794 return bfd_arch_bits_per_address (stdoutput) / 8;
15795}
15796
d2b2c203
DJ
15797int
15798i386_elf_section_type (const char *str, size_t len)
15799{
15800 if (flag_code == CODE_64BIT
15801 && len == sizeof ("unwind") - 1
d34049e8 15802 && startswith (str, "unwind"))
d2b2c203
DJ
15803 return SHT_X86_64_UNWIND;
15804
15805 return -1;
15806}
bb41ade5 15807
ad5fec3b
EB
15808#ifdef TE_SOLARIS
15809void
15810i386_solaris_fix_up_eh_frame (segT sec)
15811{
15812 if (flag_code == CODE_64BIT)
15813 elf_section_type (sec) = SHT_X86_64_UNWIND;
15814}
15815#endif
15816
bb41ade5
AM
15817#ifdef TE_PE
15818void
15819tc_pe_dwarf2_emit_offset (symbolS *symbol, unsigned int size)
15820{
91d6fa6a 15821 expressionS exp;
bb41ade5 15822
91d6fa6a
NC
15823 exp.X_op = O_secrel;
15824 exp.X_add_symbol = symbol;
15825 exp.X_add_number = 0;
15826 emit_expr (&exp, size);
bb41ade5
AM
15827}
15828#endif
3b22753a
L
15829
15830#if defined (OBJ_ELF) || defined (OBJ_MAYBE_ELF)
15831/* For ELF on x86-64, add support for SHF_X86_64_LARGE. */
15832
01e1a5bc 15833bfd_vma
6d4af3c2 15834x86_64_section_letter (int letter, const char **ptr_msg)
3b22753a
L
15835{
15836 if (flag_code == CODE_64BIT)
15837 {
15838 if (letter == 'l')
15839 return SHF_X86_64_LARGE;
15840
8f3bae45 15841 *ptr_msg = _("bad .section directive: want a,l,w,x,M,S,G,T in string");
64e74474 15842 }
3b22753a 15843 else
8f3bae45 15844 *ptr_msg = _("bad .section directive: want a,w,x,M,S,G,T in string");
3b22753a
L
15845 return -1;
15846}
15847
3b22753a
L
15848static void
15849handle_large_common (int small ATTRIBUTE_UNUSED)
15850{
15851 if (flag_code != CODE_64BIT)
15852 {
15853 s_comm_internal (0, elf_common_parse);
15854 as_warn (_(".largecomm supported only in 64bit mode, producing .comm"));
15855 }
15856 else
15857 {
15858 static segT lbss_section;
15859 asection *saved_com_section_ptr = elf_com_section_ptr;
15860 asection *saved_bss_section = bss_section;
15861
15862 if (lbss_section == NULL)
15863 {
15864 flagword applicable;
15865 segT seg = now_seg;
15866 subsegT subseg = now_subseg;
15867
15868 /* The .lbss section is for local .largecomm symbols. */
15869 lbss_section = subseg_new (".lbss", 0);
15870 applicable = bfd_applicable_section_flags (stdoutput);
fd361982 15871 bfd_set_section_flags (lbss_section, applicable & SEC_ALLOC);
3b22753a
L
15872 seg_info (lbss_section)->bss = 1;
15873
15874 subseg_set (seg, subseg);
15875 }
15876
15877 elf_com_section_ptr = &_bfd_elf_large_com_section;
15878 bss_section = lbss_section;
15879
15880 s_comm_internal (0, elf_common_parse);
15881
15882 elf_com_section_ptr = saved_com_section_ptr;
15883 bss_section = saved_bss_section;
15884 }
15885}
15886#endif /* OBJ_ELF || OBJ_MAYBE_ELF */